istereo2

changeset 2:81d35769f546

added the tunnel effect source
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 19 Sep 2015 05:51:51 +0300
parents 2d5abf441307
children dc735bdeeb8a
files istereo.xcodeproj/project.pbxproj libs/Makefile libs/imago2/COPYING libs/imago2/COPYING.LESSER libs/imago2/conv.c libs/imago2/file_jpeg.c libs/imago2/file_png.c libs/imago2/file_ppm.c libs/imago2/ftype_module.c libs/imago2/ftype_module.h libs/imago2/imago2.c libs/imago2/imago2.h libs/imago2/modules.c libs/libjpeg/README libs/libjpeg/cderror.h libs/libjpeg/jcapimin.c libs/libjpeg/jcapistd.c libs/libjpeg/jccoefct.c libs/libjpeg/jccolor.c libs/libjpeg/jcdctmgr.c libs/libjpeg/jchuff.c libs/libjpeg/jchuff.h libs/libjpeg/jcinit.c libs/libjpeg/jcmainct.c libs/libjpeg/jcmarker.c libs/libjpeg/jcmaster.c libs/libjpeg/jcomapi.c libs/libjpeg/jconfig.h libs/libjpeg/jcparam.c libs/libjpeg/jcphuff.c libs/libjpeg/jcprepct.c libs/libjpeg/jcsample.c libs/libjpeg/jctrans.c libs/libjpeg/jdapimin.c libs/libjpeg/jdapistd.c libs/libjpeg/jdatadst.c libs/libjpeg/jdatasrc.c libs/libjpeg/jdcoefct.c libs/libjpeg/jdcolor.c libs/libjpeg/jdct.h libs/libjpeg/jddctmgr.c libs/libjpeg/jdhuff.c libs/libjpeg/jdhuff.h libs/libjpeg/jdinput.c libs/libjpeg/jdmainct.c libs/libjpeg/jdmarker.c libs/libjpeg/jdmaster.c libs/libjpeg/jdmerge.c libs/libjpeg/jdphuff.c libs/libjpeg/jdpostct.c libs/libjpeg/jdsample.c libs/libjpeg/jdtrans.c libs/libjpeg/jerror.c libs/libjpeg/jerror.h libs/libjpeg/jfdctflt.c libs/libjpeg/jfdctfst.c libs/libjpeg/jfdctint.c libs/libjpeg/jidctflt.c libs/libjpeg/jidctfst.c libs/libjpeg/jidctint.c libs/libjpeg/jidctred.c libs/libjpeg/jinclude.h libs/libjpeg/jmemmgr.c libs/libjpeg/jmemnobs.c libs/libjpeg/jmemsys.h libs/libjpeg/jmorecfg.h libs/libjpeg/jpegint.h libs/libjpeg/jpeglib.h libs/libjpeg/jquant1.c libs/libjpeg/jquant2.c libs/libjpeg/jutils.c libs/libjpeg/jversion.h libs/libpng/LICENSE libs/libpng/png.c libs/libpng/png.h libs/libpng/pngconf.h libs/libpng/pngerror.c libs/libpng/pnggccrd.c libs/libpng/pngget.c libs/libpng/pngmem.c libs/libpng/pngpread.c libs/libpng/pngread.c libs/libpng/pngrio.c libs/libpng/pngrtran.c libs/libpng/pngrutil.c libs/libpng/pngset.c libs/libpng/pngtrans.c libs/libpng/pngvcrd.c libs/libpng/pngwio.c libs/libpng/pngwrite.c libs/libpng/pngwtran.c libs/libpng/pngwutil.c libs/vmath/COPYING libs/vmath/COPYING.LESSER libs/vmath/basis.h libs/vmath/basis_c.c libs/vmath/geom.c libs/vmath/geom.h libs/vmath/matrix.h libs/vmath/matrix.inl libs/vmath/matrix_c.c libs/vmath/quat.h libs/vmath/quat.inl libs/vmath/quat_c.c libs/vmath/ray.h libs/vmath/ray.inl libs/vmath/ray_c.c libs/vmath/sphvec.h libs/vmath/vector.h libs/vmath/vector.inl libs/vmath/vmath.c libs/vmath/vmath.h libs/vmath/vmath.inl libs/vmath/vmath_config.h libs/vmath/vmath_types.h libs/zlib/LICENSE libs/zlib/adler32.c libs/zlib/compress.c libs/zlib/crc32.c libs/zlib/crc32.h libs/zlib/deflate.c libs/zlib/deflate.h libs/zlib/gzio.c libs/zlib/infback.c libs/zlib/inffast.c libs/zlib/inffast.h libs/zlib/inffixed.h libs/zlib/inflate.c libs/zlib/inflate.h libs/zlib/inftrees.c libs/zlib/inftrees.h libs/zlib/trees.c libs/zlib/trees.h libs/zlib/uncompr.c libs/zlib/zconf.h libs/zlib/zlib.h libs/zlib/zutil.c libs/zlib/zutil.h sdr/test.p.glsl sdr/test.v.glsl sdr/text.p.glsl sdr/text.v.glsl sdr/tunnel.p.glsl sdr/tunnel.v.glsl src/cam.c src/cam.h src/config.h src/glutmain.c src/ios/viewctl.m src/istereo.c src/istereo.h src/opengl.h src/respath.c src/respath.h src/sanegl.c src/sanegl.h src/sdr.c src/sdr.h src/tex.c src/tex.h src/ui.h src/ui.xib
diffstat 162 files changed, 70872 insertions(+), 29 deletions(-) [+]
line diff
     1.1 --- a/istereo.xcodeproj/project.pbxproj	Fri Sep 18 23:02:50 2015 +0300
     1.2 +++ b/istereo.xcodeproj/project.pbxproj	Sat Sep 19 05:51:51 2015 +0300
     1.3 @@ -11,6 +11,110 @@
     1.4  		0669D67F1BA85DDE00611CFA /* app_delegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 0669D67B1BA85DDE00611CFA /* app_delegate.m */; };
     1.5  		0669D6801BA85DDE00611CFA /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 0669D67C1BA85DDE00611CFA /* main.m */; };
     1.6  		0669D6811BA85DDE00611CFA /* viewctl.m in Sources */ = {isa = PBXBuildFile; fileRef = 0669D67E1BA85DDE00611CFA /* viewctl.m */; };
     1.7 +		0669D6911BACDF6C00611CFA /* cam.c in Sources */ = {isa = PBXBuildFile; fileRef = 0669D6821BACDF6C00611CFA /* cam.c */; };
     1.8 +		0669D6921BACDF6C00611CFA /* istereo.c in Sources */ = {isa = PBXBuildFile; fileRef = 0669D6851BACDF6C00611CFA /* istereo.c */; };
     1.9 +		0669D6931BACDF6C00611CFA /* respath.c in Sources */ = {isa = PBXBuildFile; fileRef = 0669D6881BACDF6C00611CFA /* respath.c */; };
    1.10 +		0669D6941BACDF6C00611CFA /* sanegl.c in Sources */ = {isa = PBXBuildFile; fileRef = 0669D68A1BACDF6C00611CFA /* sanegl.c */; };
    1.11 +		0669D6951BACDF6C00611CFA /* sdr.c in Sources */ = {isa = PBXBuildFile; fileRef = 0669D68C1BACDF6C00611CFA /* sdr.c */; };
    1.12 +		0669D6961BACDF6C00611CFA /* tex.c in Sources */ = {isa = PBXBuildFile; fileRef = 0669D68E1BACDF6C00611CFA /* tex.c */; };
    1.13 +		0669D6B21BAD020B00611CFA /* basis_c.c in Sources */ = {isa = PBXBuildFile; fileRef = 0669D69D1BAD020B00611CFA /* basis_c.c */; };
    1.14 +		0669D6B31BAD020B00611CFA /* geom.c in Sources */ = {isa = PBXBuildFile; fileRef = 0669D69F1BAD020B00611CFA /* geom.c */; };
    1.15 +		0669D6B41BAD020B00611CFA /* matrix_c.c in Sources */ = {isa = PBXBuildFile; fileRef = 0669D6A11BAD020B00611CFA /* matrix_c.c */; };
    1.16 +		0669D6B61BAD020B00611CFA /* quat_c.c in Sources */ = {isa = PBXBuildFile; fileRef = 0669D6A41BAD020B00611CFA /* quat_c.c */; };
    1.17 +		0669D6B81BAD020B00611CFA /* ray_c.c in Sources */ = {isa = PBXBuildFile; fileRef = 0669D6A71BAD020B00611CFA /* ray_c.c */; };
    1.18 +		0669D6BB1BAD020B00611CFA /* vmath.c in Sources */ = {isa = PBXBuildFile; fileRef = 0669D6AF1BAD020B00611CFA /* vmath.c */; };
    1.19 +		0669D6C61BAD022C00611CFA /* conv.c in Sources */ = {isa = PBXBuildFile; fileRef = 0669D6BD1BAD022C00611CFA /* conv.c */; };
    1.20 +		0669D6C71BAD022C00611CFA /* file_jpeg.c in Sources */ = {isa = PBXBuildFile; fileRef = 0669D6BE1BAD022C00611CFA /* file_jpeg.c */; };
    1.21 +		0669D6C81BAD022C00611CFA /* file_png.c in Sources */ = {isa = PBXBuildFile; fileRef = 0669D6BF1BAD022C00611CFA /* file_png.c */; };
    1.22 +		0669D6C91BAD022C00611CFA /* file_ppm.c in Sources */ = {isa = PBXBuildFile; fileRef = 0669D6C01BAD022C00611CFA /* file_ppm.c */; };
    1.23 +		0669D6CA1BAD022C00611CFA /* ftype_module.c in Sources */ = {isa = PBXBuildFile; fileRef = 0669D6C11BAD022C00611CFA /* ftype_module.c */; };
    1.24 +		0669D6CB1BAD022C00611CFA /* imago2.c in Sources */ = {isa = PBXBuildFile; fileRef = 0669D6C31BAD022C00611CFA /* imago2.c */; };
    1.25 +		0669D6CC1BAD022C00611CFA /* modules.c in Sources */ = {isa = PBXBuildFile; fileRef = 0669D6C51BAD022C00611CFA /* modules.c */; };
    1.26 +		0669D7071BAD024700611CFA /* jcapimin.c in Sources */ = {isa = PBXBuildFile; fileRef = 0669D6CE1BAD024700611CFA /* jcapimin.c */; };
    1.27 +		0669D7081BAD024700611CFA /* jcapistd.c in Sources */ = {isa = PBXBuildFile; fileRef = 0669D6CF1BAD024700611CFA /* jcapistd.c */; };
    1.28 +		0669D7091BAD024700611CFA /* jccoefct.c in Sources */ = {isa = PBXBuildFile; fileRef = 0669D6D01BAD024700611CFA /* jccoefct.c */; };
    1.29 +		0669D70A1BAD024700611CFA /* jccolor.c in Sources */ = {isa = PBXBuildFile; fileRef = 0669D6D11BAD024700611CFA /* jccolor.c */; };
    1.30 +		0669D70B1BAD024700611CFA /* jcdctmgr.c in Sources */ = {isa = PBXBuildFile; fileRef = 0669D6D21BAD024700611CFA /* jcdctmgr.c */; };
    1.31 +		0669D70C1BAD024700611CFA /* jchuff.c in Sources */ = {isa = PBXBuildFile; fileRef = 0669D6D31BAD024700611CFA /* jchuff.c */; };
    1.32 +		0669D70D1BAD024700611CFA /* jcinit.c in Sources */ = {isa = PBXBuildFile; fileRef = 0669D6D51BAD024700611CFA /* jcinit.c */; };
    1.33 +		0669D70E1BAD024700611CFA /* jcmainct.c in Sources */ = {isa = PBXBuildFile; fileRef = 0669D6D61BAD024700611CFA /* jcmainct.c */; };
    1.34 +		0669D70F1BAD024700611CFA /* jcmarker.c in Sources */ = {isa = PBXBuildFile; fileRef = 0669D6D71BAD024700611CFA /* jcmarker.c */; };
    1.35 +		0669D7101BAD024700611CFA /* jcmaster.c in Sources */ = {isa = PBXBuildFile; fileRef = 0669D6D81BAD024700611CFA /* jcmaster.c */; };
    1.36 +		0669D7111BAD024700611CFA /* jcomapi.c in Sources */ = {isa = PBXBuildFile; fileRef = 0669D6D91BAD024700611CFA /* jcomapi.c */; };
    1.37 +		0669D7121BAD024700611CFA /* jcparam.c in Sources */ = {isa = PBXBuildFile; fileRef = 0669D6DB1BAD024700611CFA /* jcparam.c */; };
    1.38 +		0669D7131BAD024700611CFA /* jcphuff.c in Sources */ = {isa = PBXBuildFile; fileRef = 0669D6DC1BAD024700611CFA /* jcphuff.c */; };
    1.39 +		0669D7141BAD024700611CFA /* jcprepct.c in Sources */ = {isa = PBXBuildFile; fileRef = 0669D6DD1BAD024700611CFA /* jcprepct.c */; };
    1.40 +		0669D7151BAD024700611CFA /* jcsample.c in Sources */ = {isa = PBXBuildFile; fileRef = 0669D6DE1BAD024700611CFA /* jcsample.c */; };
    1.41 +		0669D7161BAD024700611CFA /* jctrans.c in Sources */ = {isa = PBXBuildFile; fileRef = 0669D6DF1BAD024700611CFA /* jctrans.c */; };
    1.42 +		0669D7171BAD024700611CFA /* jdapimin.c in Sources */ = {isa = PBXBuildFile; fileRef = 0669D6E01BAD024700611CFA /* jdapimin.c */; };
    1.43 +		0669D7181BAD024700611CFA /* jdapistd.c in Sources */ = {isa = PBXBuildFile; fileRef = 0669D6E11BAD024700611CFA /* jdapistd.c */; };
    1.44 +		0669D7191BAD024700611CFA /* jdatadst.c in Sources */ = {isa = PBXBuildFile; fileRef = 0669D6E21BAD024700611CFA /* jdatadst.c */; };
    1.45 +		0669D71A1BAD024700611CFA /* jdatasrc.c in Sources */ = {isa = PBXBuildFile; fileRef = 0669D6E31BAD024700611CFA /* jdatasrc.c */; };
    1.46 +		0669D71B1BAD024700611CFA /* jdcoefct.c in Sources */ = {isa = PBXBuildFile; fileRef = 0669D6E41BAD024700611CFA /* jdcoefct.c */; };
    1.47 +		0669D71C1BAD024700611CFA /* jdcolor.c in Sources */ = {isa = PBXBuildFile; fileRef = 0669D6E51BAD024700611CFA /* jdcolor.c */; };
    1.48 +		0669D71D1BAD024700611CFA /* jddctmgr.c in Sources */ = {isa = PBXBuildFile; fileRef = 0669D6E71BAD024700611CFA /* jddctmgr.c */; };
    1.49 +		0669D71E1BAD024700611CFA /* jdhuff.c in Sources */ = {isa = PBXBuildFile; fileRef = 0669D6E81BAD024700611CFA /* jdhuff.c */; };
    1.50 +		0669D71F1BAD024700611CFA /* jdinput.c in Sources */ = {isa = PBXBuildFile; fileRef = 0669D6EA1BAD024700611CFA /* jdinput.c */; };
    1.51 +		0669D7201BAD024700611CFA /* jdmainct.c in Sources */ = {isa = PBXBuildFile; fileRef = 0669D6EB1BAD024700611CFA /* jdmainct.c */; };
    1.52 +		0669D7211BAD024700611CFA /* jdmarker.c in Sources */ = {isa = PBXBuildFile; fileRef = 0669D6EC1BAD024700611CFA /* jdmarker.c */; };
    1.53 +		0669D7221BAD024700611CFA /* jdmaster.c in Sources */ = {isa = PBXBuildFile; fileRef = 0669D6ED1BAD024700611CFA /* jdmaster.c */; };
    1.54 +		0669D7231BAD024700611CFA /* jdmerge.c in Sources */ = {isa = PBXBuildFile; fileRef = 0669D6EE1BAD024700611CFA /* jdmerge.c */; };
    1.55 +		0669D7241BAD024700611CFA /* jdphuff.c in Sources */ = {isa = PBXBuildFile; fileRef = 0669D6EF1BAD024700611CFA /* jdphuff.c */; };
    1.56 +		0669D7251BAD024700611CFA /* jdpostct.c in Sources */ = {isa = PBXBuildFile; fileRef = 0669D6F01BAD024700611CFA /* jdpostct.c */; };
    1.57 +		0669D7261BAD024700611CFA /* jdsample.c in Sources */ = {isa = PBXBuildFile; fileRef = 0669D6F11BAD024700611CFA /* jdsample.c */; };
    1.58 +		0669D7271BAD024700611CFA /* jdtrans.c in Sources */ = {isa = PBXBuildFile; fileRef = 0669D6F21BAD024700611CFA /* jdtrans.c */; };
    1.59 +		0669D7281BAD024700611CFA /* jerror.c in Sources */ = {isa = PBXBuildFile; fileRef = 0669D6F31BAD024700611CFA /* jerror.c */; };
    1.60 +		0669D7291BAD024700611CFA /* jfdctflt.c in Sources */ = {isa = PBXBuildFile; fileRef = 0669D6F51BAD024700611CFA /* jfdctflt.c */; };
    1.61 +		0669D72A1BAD024700611CFA /* jfdctfst.c in Sources */ = {isa = PBXBuildFile; fileRef = 0669D6F61BAD024700611CFA /* jfdctfst.c */; };
    1.62 +		0669D72B1BAD024700611CFA /* jfdctint.c in Sources */ = {isa = PBXBuildFile; fileRef = 0669D6F71BAD024700611CFA /* jfdctint.c */; };
    1.63 +		0669D72C1BAD024700611CFA /* jidctflt.c in Sources */ = {isa = PBXBuildFile; fileRef = 0669D6F81BAD024700611CFA /* jidctflt.c */; };
    1.64 +		0669D72D1BAD024700611CFA /* jidctfst.c in Sources */ = {isa = PBXBuildFile; fileRef = 0669D6F91BAD024700611CFA /* jidctfst.c */; };
    1.65 +		0669D72E1BAD024700611CFA /* jidctint.c in Sources */ = {isa = PBXBuildFile; fileRef = 0669D6FA1BAD024700611CFA /* jidctint.c */; };
    1.66 +		0669D72F1BAD024700611CFA /* jidctred.c in Sources */ = {isa = PBXBuildFile; fileRef = 0669D6FB1BAD024700611CFA /* jidctred.c */; };
    1.67 +		0669D7301BAD024700611CFA /* jmemmgr.c in Sources */ = {isa = PBXBuildFile; fileRef = 0669D6FD1BAD024700611CFA /* jmemmgr.c */; };
    1.68 +		0669D7311BAD024700611CFA /* jmemnobs.c in Sources */ = {isa = PBXBuildFile; fileRef = 0669D6FE1BAD024700611CFA /* jmemnobs.c */; };
    1.69 +		0669D7321BAD024700611CFA /* jquant1.c in Sources */ = {isa = PBXBuildFile; fileRef = 0669D7031BAD024700611CFA /* jquant1.c */; };
    1.70 +		0669D7331BAD024700611CFA /* jquant2.c in Sources */ = {isa = PBXBuildFile; fileRef = 0669D7041BAD024700611CFA /* jquant2.c */; };
    1.71 +		0669D7341BAD024700611CFA /* jutils.c in Sources */ = {isa = PBXBuildFile; fileRef = 0669D7051BAD024700611CFA /* jutils.c */; };
    1.72 +		0669D7481BAD025B00611CFA /* png.c in Sources */ = {isa = PBXBuildFile; fileRef = 0669D7351BAD025B00611CFA /* png.c */; };
    1.73 +		0669D7491BAD025B00611CFA /* pngerror.c in Sources */ = {isa = PBXBuildFile; fileRef = 0669D7381BAD025B00611CFA /* pngerror.c */; };
    1.74 +		0669D74A1BAD025B00611CFA /* pnggccrd.c in Sources */ = {isa = PBXBuildFile; fileRef = 0669D7391BAD025B00611CFA /* pnggccrd.c */; };
    1.75 +		0669D74B1BAD025B00611CFA /* pngget.c in Sources */ = {isa = PBXBuildFile; fileRef = 0669D73A1BAD025B00611CFA /* pngget.c */; };
    1.76 +		0669D74C1BAD025B00611CFA /* pngmem.c in Sources */ = {isa = PBXBuildFile; fileRef = 0669D73B1BAD025B00611CFA /* pngmem.c */; };
    1.77 +		0669D74D1BAD025B00611CFA /* pngpread.c in Sources */ = {isa = PBXBuildFile; fileRef = 0669D73C1BAD025B00611CFA /* pngpread.c */; };
    1.78 +		0669D74E1BAD025B00611CFA /* pngread.c in Sources */ = {isa = PBXBuildFile; fileRef = 0669D73D1BAD025B00611CFA /* pngread.c */; };
    1.79 +		0669D74F1BAD025B00611CFA /* pngrio.c in Sources */ = {isa = PBXBuildFile; fileRef = 0669D73E1BAD025B00611CFA /* pngrio.c */; };
    1.80 +		0669D7501BAD025B00611CFA /* pngrtran.c in Sources */ = {isa = PBXBuildFile; fileRef = 0669D73F1BAD025B00611CFA /* pngrtran.c */; };
    1.81 +		0669D7511BAD025B00611CFA /* pngrutil.c in Sources */ = {isa = PBXBuildFile; fileRef = 0669D7401BAD025B00611CFA /* pngrutil.c */; };
    1.82 +		0669D7521BAD025B00611CFA /* pngset.c in Sources */ = {isa = PBXBuildFile; fileRef = 0669D7411BAD025B00611CFA /* pngset.c */; };
    1.83 +		0669D7531BAD025B00611CFA /* pngtrans.c in Sources */ = {isa = PBXBuildFile; fileRef = 0669D7421BAD025B00611CFA /* pngtrans.c */; };
    1.84 +		0669D7541BAD025B00611CFA /* pngvcrd.c in Sources */ = {isa = PBXBuildFile; fileRef = 0669D7431BAD025B00611CFA /* pngvcrd.c */; };
    1.85 +		0669D7551BAD025B00611CFA /* pngwio.c in Sources */ = {isa = PBXBuildFile; fileRef = 0669D7441BAD025B00611CFA /* pngwio.c */; };
    1.86 +		0669D7561BAD025B00611CFA /* pngwrite.c in Sources */ = {isa = PBXBuildFile; fileRef = 0669D7451BAD025B00611CFA /* pngwrite.c */; };
    1.87 +		0669D7571BAD025B00611CFA /* pngwtran.c in Sources */ = {isa = PBXBuildFile; fileRef = 0669D7461BAD025B00611CFA /* pngwtran.c */; };
    1.88 +		0669D7581BAD025B00611CFA /* pngwutil.c in Sources */ = {isa = PBXBuildFile; fileRef = 0669D7471BAD025B00611CFA /* pngwutil.c */; };
    1.89 +		0669D76F1BAD027000611CFA /* adler32.c in Sources */ = {isa = PBXBuildFile; fileRef = 0669D7591BAD027000611CFA /* adler32.c */; };
    1.90 +		0669D7701BAD027000611CFA /* compress.c in Sources */ = {isa = PBXBuildFile; fileRef = 0669D75A1BAD027000611CFA /* compress.c */; };
    1.91 +		0669D7711BAD027000611CFA /* crc32.c in Sources */ = {isa = PBXBuildFile; fileRef = 0669D75B1BAD027000611CFA /* crc32.c */; };
    1.92 +		0669D7721BAD027000611CFA /* deflate.c in Sources */ = {isa = PBXBuildFile; fileRef = 0669D75D1BAD027000611CFA /* deflate.c */; };
    1.93 +		0669D7731BAD027000611CFA /* gzio.c in Sources */ = {isa = PBXBuildFile; fileRef = 0669D75F1BAD027000611CFA /* gzio.c */; };
    1.94 +		0669D7741BAD027000611CFA /* infback.c in Sources */ = {isa = PBXBuildFile; fileRef = 0669D7601BAD027000611CFA /* infback.c */; };
    1.95 +		0669D7751BAD027000611CFA /* inffast.c in Sources */ = {isa = PBXBuildFile; fileRef = 0669D7611BAD027000611CFA /* inffast.c */; };
    1.96 +		0669D7761BAD027000611CFA /* inflate.c in Sources */ = {isa = PBXBuildFile; fileRef = 0669D7641BAD027000611CFA /* inflate.c */; };
    1.97 +		0669D7771BAD027000611CFA /* inftrees.c in Sources */ = {isa = PBXBuildFile; fileRef = 0669D7661BAD027000611CFA /* inftrees.c */; };
    1.98 +		0669D7781BAD027000611CFA /* trees.c in Sources */ = {isa = PBXBuildFile; fileRef = 0669D7681BAD027000611CFA /* trees.c */; };
    1.99 +		0669D7791BAD027000611CFA /* uncompr.c in Sources */ = {isa = PBXBuildFile; fileRef = 0669D76A1BAD027000611CFA /* uncompr.c */; };
   1.100 +		0669D77A1BAD027000611CFA /* zutil.c in Sources */ = {isa = PBXBuildFile; fileRef = 0669D76D1BAD027000611CFA /* zutil.c */; };
   1.101 +		0669D7821BAD02A200611CFA /* test.p.glsl in Resources */ = {isa = PBXBuildFile; fileRef = 0669D77C1BAD02A200611CFA /* test.p.glsl */; };
   1.102 +		0669D7831BAD02A200611CFA /* test.v.glsl in Resources */ = {isa = PBXBuildFile; fileRef = 0669D77D1BAD02A200611CFA /* test.v.glsl */; };
   1.103 +		0669D7841BAD02A200611CFA /* text.p.glsl in Resources */ = {isa = PBXBuildFile; fileRef = 0669D77E1BAD02A200611CFA /* text.p.glsl */; };
   1.104 +		0669D7851BAD02A200611CFA /* text.v.glsl in Resources */ = {isa = PBXBuildFile; fileRef = 0669D77F1BAD02A200611CFA /* text.v.glsl */; };
   1.105 +		0669D7861BAD02A200611CFA /* tunnel.p.glsl in Resources */ = {isa = PBXBuildFile; fileRef = 0669D7801BAD02A200611CFA /* tunnel.p.glsl */; };
   1.106 +		0669D7871BAD02A200611CFA /* tunnel.v.glsl in Resources */ = {isa = PBXBuildFile; fileRef = 0669D7811BAD02A200611CFA /* tunnel.v.glsl */; };
   1.107 +		0669D78D1BAD02DC00611CFA /* stonewall_normal.jpg in Resources */ = {isa = PBXBuildFile; fileRef = 0669D7891BAD02DC00611CFA /* stonewall_normal.jpg */; };
   1.108 +		0669D78E1BAD02DC00611CFA /* stonewall.jpg in Resources */ = {isa = PBXBuildFile; fileRef = 0669D78A1BAD02DC00611CFA /* stonewall.jpg */; };
   1.109 +		0669D78F1BAD02DC00611CFA /* text.png in Resources */ = {isa = PBXBuildFile; fileRef = 0669D78B1BAD02DC00611CFA /* text.png */; };
   1.110 +		0669D7901BAD02DC00611CFA /* tiles.jpg in Resources */ = {isa = PBXBuildFile; fileRef = 0669D78C1BAD02DC00611CFA /* tiles.jpg */; };
   1.111  /* End PBXBuildFile section */
   1.112  
   1.113  /* Begin PBXFileReference section */
   1.114 @@ -21,6 +125,160 @@
   1.115  		0669D67C1BA85DDE00611CFA /* main.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = main.m; path = src/ios/main.m; sourceTree = "<group>"; };
   1.116  		0669D67D1BA85DDE00611CFA /* viewctl.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = viewctl.h; path = src/ios/viewctl.h; sourceTree = "<group>"; };
   1.117  		0669D67E1BA85DDE00611CFA /* viewctl.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = viewctl.m; path = src/ios/viewctl.m; sourceTree = "<group>"; };
   1.118 +		0669D6821BACDF6C00611CFA /* cam.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = cam.c; path = src/cam.c; sourceTree = "<group>"; };
   1.119 +		0669D6831BACDF6C00611CFA /* cam.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = cam.h; path = src/cam.h; sourceTree = "<group>"; };
   1.120 +		0669D6841BACDF6C00611CFA /* config.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = config.h; path = src/config.h; sourceTree = "<group>"; };
   1.121 +		0669D6851BACDF6C00611CFA /* istereo.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = istereo.c; path = src/istereo.c; sourceTree = "<group>"; };
   1.122 +		0669D6861BACDF6C00611CFA /* istereo.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = istereo.h; path = src/istereo.h; sourceTree = "<group>"; };
   1.123 +		0669D6871BACDF6C00611CFA /* opengl.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = opengl.h; path = src/opengl.h; sourceTree = "<group>"; };
   1.124 +		0669D6881BACDF6C00611CFA /* respath.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = respath.c; path = src/respath.c; sourceTree = "<group>"; };
   1.125 +		0669D6891BACDF6C00611CFA /* respath.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = respath.h; path = src/respath.h; sourceTree = "<group>"; };
   1.126 +		0669D68A1BACDF6C00611CFA /* sanegl.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = sanegl.c; path = src/sanegl.c; sourceTree = "<group>"; };
   1.127 +		0669D68B1BACDF6C00611CFA /* sanegl.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = sanegl.h; path = src/sanegl.h; sourceTree = "<group>"; };
   1.128 +		0669D68C1BACDF6C00611CFA /* sdr.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = sdr.c; path = src/sdr.c; sourceTree = "<group>"; };
   1.129 +		0669D68D1BACDF6C00611CFA /* sdr.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = sdr.h; path = src/sdr.h; sourceTree = "<group>"; };
   1.130 +		0669D68E1BACDF6C00611CFA /* tex.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = tex.c; path = src/tex.c; sourceTree = "<group>"; };
   1.131 +		0669D68F1BACDF6C00611CFA /* tex.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = tex.h; path = src/tex.h; sourceTree = "<group>"; };
   1.132 +		0669D6901BACDF6C00611CFA /* ui.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = ui.h; path = src/ui.h; sourceTree = "<group>"; };
   1.133 +		0669D69D1BAD020B00611CFA /* basis_c.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = basis_c.c; path = libs/vmath/basis_c.c; sourceTree = "<group>"; };
   1.134 +		0669D69E1BAD020B00611CFA /* basis.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = basis.h; path = libs/vmath/basis.h; sourceTree = "<group>"; };
   1.135 +		0669D69F1BAD020B00611CFA /* geom.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = geom.c; path = libs/vmath/geom.c; sourceTree = "<group>"; };
   1.136 +		0669D6A01BAD020B00611CFA /* geom.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = geom.h; path = libs/vmath/geom.h; sourceTree = "<group>"; };
   1.137 +		0669D6A11BAD020B00611CFA /* matrix_c.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = matrix_c.c; path = libs/vmath/matrix_c.c; sourceTree = "<group>"; };
   1.138 +		0669D6A21BAD020B00611CFA /* matrix.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = matrix.h; path = libs/vmath/matrix.h; sourceTree = "<group>"; };
   1.139 +		0669D6A31BAD020B00611CFA /* matrix.inl */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; name = matrix.inl; path = libs/vmath/matrix.inl; sourceTree = "<group>"; };
   1.140 +		0669D6A41BAD020B00611CFA /* quat_c.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = quat_c.c; path = libs/vmath/quat_c.c; sourceTree = "<group>"; };
   1.141 +		0669D6A51BAD020B00611CFA /* quat.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = quat.h; path = libs/vmath/quat.h; sourceTree = "<group>"; };
   1.142 +		0669D6A61BAD020B00611CFA /* quat.inl */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; name = quat.inl; path = libs/vmath/quat.inl; sourceTree = "<group>"; };
   1.143 +		0669D6A71BAD020B00611CFA /* ray_c.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = ray_c.c; path = libs/vmath/ray_c.c; sourceTree = "<group>"; };
   1.144 +		0669D6A81BAD020B00611CFA /* ray.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = ray.h; path = libs/vmath/ray.h; sourceTree = "<group>"; };
   1.145 +		0669D6A91BAD020B00611CFA /* ray.inl */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; name = ray.inl; path = libs/vmath/ray.inl; sourceTree = "<group>"; };
   1.146 +		0669D6AA1BAD020B00611CFA /* sphvec.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = sphvec.h; path = libs/vmath/sphvec.h; sourceTree = "<group>"; };
   1.147 +		0669D6AB1BAD020B00611CFA /* vector.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = vector.h; path = libs/vmath/vector.h; sourceTree = "<group>"; };
   1.148 +		0669D6AC1BAD020B00611CFA /* vector.inl */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; name = vector.inl; path = libs/vmath/vector.inl; sourceTree = "<group>"; };
   1.149 +		0669D6AD1BAD020B00611CFA /* vmath_config.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = vmath_config.h; path = libs/vmath/vmath_config.h; sourceTree = "<group>"; };
   1.150 +		0669D6AE1BAD020B00611CFA /* vmath_types.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = vmath_types.h; path = libs/vmath/vmath_types.h; sourceTree = "<group>"; };
   1.151 +		0669D6AF1BAD020B00611CFA /* vmath.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = vmath.c; path = libs/vmath/vmath.c; sourceTree = "<group>"; };
   1.152 +		0669D6B01BAD020B00611CFA /* vmath.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = vmath.h; path = libs/vmath/vmath.h; sourceTree = "<group>"; };
   1.153 +		0669D6B11BAD020B00611CFA /* vmath.inl */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; name = vmath.inl; path = libs/vmath/vmath.inl; sourceTree = "<group>"; };
   1.154 +		0669D6BD1BAD022C00611CFA /* conv.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = conv.c; path = libs/imago2/conv.c; sourceTree = "<group>"; };
   1.155 +		0669D6BE1BAD022C00611CFA /* file_jpeg.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = file_jpeg.c; path = libs/imago2/file_jpeg.c; sourceTree = "<group>"; };
   1.156 +		0669D6BF1BAD022C00611CFA /* file_png.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = file_png.c; path = libs/imago2/file_png.c; sourceTree = "<group>"; };
   1.157 +		0669D6C01BAD022C00611CFA /* file_ppm.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = file_ppm.c; path = libs/imago2/file_ppm.c; sourceTree = "<group>"; };
   1.158 +		0669D6C11BAD022C00611CFA /* ftype_module.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = ftype_module.c; path = libs/imago2/ftype_module.c; sourceTree = "<group>"; };
   1.159 +		0669D6C21BAD022C00611CFA /* ftype_module.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = ftype_module.h; path = libs/imago2/ftype_module.h; sourceTree = "<group>"; };
   1.160 +		0669D6C31BAD022C00611CFA /* imago2.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = imago2.c; path = libs/imago2/imago2.c; sourceTree = "<group>"; };
   1.161 +		0669D6C41BAD022C00611CFA /* imago2.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = imago2.h; path = libs/imago2/imago2.h; sourceTree = "<group>"; };
   1.162 +		0669D6C51BAD022C00611CFA /* modules.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = modules.c; path = libs/imago2/modules.c; sourceTree = "<group>"; };
   1.163 +		0669D6CD1BAD024700611CFA /* cderror.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = cderror.h; path = libs/libjpeg/cderror.h; sourceTree = "<group>"; };
   1.164 +		0669D6CE1BAD024700611CFA /* jcapimin.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = jcapimin.c; path = libs/libjpeg/jcapimin.c; sourceTree = "<group>"; };
   1.165 +		0669D6CF1BAD024700611CFA /* jcapistd.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = jcapistd.c; path = libs/libjpeg/jcapistd.c; sourceTree = "<group>"; };
   1.166 +		0669D6D01BAD024700611CFA /* jccoefct.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = jccoefct.c; path = libs/libjpeg/jccoefct.c; sourceTree = "<group>"; };
   1.167 +		0669D6D11BAD024700611CFA /* jccolor.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = jccolor.c; path = libs/libjpeg/jccolor.c; sourceTree = "<group>"; };
   1.168 +		0669D6D21BAD024700611CFA /* jcdctmgr.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = jcdctmgr.c; path = libs/libjpeg/jcdctmgr.c; sourceTree = "<group>"; };
   1.169 +		0669D6D31BAD024700611CFA /* jchuff.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = jchuff.c; path = libs/libjpeg/jchuff.c; sourceTree = "<group>"; };
   1.170 +		0669D6D41BAD024700611CFA /* jchuff.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = jchuff.h; path = libs/libjpeg/jchuff.h; sourceTree = "<group>"; };
   1.171 +		0669D6D51BAD024700611CFA /* jcinit.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = jcinit.c; path = libs/libjpeg/jcinit.c; sourceTree = "<group>"; };
   1.172 +		0669D6D61BAD024700611CFA /* jcmainct.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = jcmainct.c; path = libs/libjpeg/jcmainct.c; sourceTree = "<group>"; };
   1.173 +		0669D6D71BAD024700611CFA /* jcmarker.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = jcmarker.c; path = libs/libjpeg/jcmarker.c; sourceTree = "<group>"; };
   1.174 +		0669D6D81BAD024700611CFA /* jcmaster.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = jcmaster.c; path = libs/libjpeg/jcmaster.c; sourceTree = "<group>"; };
   1.175 +		0669D6D91BAD024700611CFA /* jcomapi.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = jcomapi.c; path = libs/libjpeg/jcomapi.c; sourceTree = "<group>"; };
   1.176 +		0669D6DA1BAD024700611CFA /* jconfig.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = jconfig.h; path = libs/libjpeg/jconfig.h; sourceTree = "<group>"; };
   1.177 +		0669D6DB1BAD024700611CFA /* jcparam.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = jcparam.c; path = libs/libjpeg/jcparam.c; sourceTree = "<group>"; };
   1.178 +		0669D6DC1BAD024700611CFA /* jcphuff.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = jcphuff.c; path = libs/libjpeg/jcphuff.c; sourceTree = "<group>"; };
   1.179 +		0669D6DD1BAD024700611CFA /* jcprepct.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = jcprepct.c; path = libs/libjpeg/jcprepct.c; sourceTree = "<group>"; };
   1.180 +		0669D6DE1BAD024700611CFA /* jcsample.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = jcsample.c; path = libs/libjpeg/jcsample.c; sourceTree = "<group>"; };
   1.181 +		0669D6DF1BAD024700611CFA /* jctrans.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = jctrans.c; path = libs/libjpeg/jctrans.c; sourceTree = "<group>"; };
   1.182 +		0669D6E01BAD024700611CFA /* jdapimin.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = jdapimin.c; path = libs/libjpeg/jdapimin.c; sourceTree = "<group>"; };
   1.183 +		0669D6E11BAD024700611CFA /* jdapistd.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = jdapistd.c; path = libs/libjpeg/jdapistd.c; sourceTree = "<group>"; };
   1.184 +		0669D6E21BAD024700611CFA /* jdatadst.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = jdatadst.c; path = libs/libjpeg/jdatadst.c; sourceTree = "<group>"; };
   1.185 +		0669D6E31BAD024700611CFA /* jdatasrc.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = jdatasrc.c; path = libs/libjpeg/jdatasrc.c; sourceTree = "<group>"; };
   1.186 +		0669D6E41BAD024700611CFA /* jdcoefct.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = jdcoefct.c; path = libs/libjpeg/jdcoefct.c; sourceTree = "<group>"; };
   1.187 +		0669D6E51BAD024700611CFA /* jdcolor.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = jdcolor.c; path = libs/libjpeg/jdcolor.c; sourceTree = "<group>"; };
   1.188 +		0669D6E61BAD024700611CFA /* jdct.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = jdct.h; path = libs/libjpeg/jdct.h; sourceTree = "<group>"; };
   1.189 +		0669D6E71BAD024700611CFA /* jddctmgr.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = jddctmgr.c; path = libs/libjpeg/jddctmgr.c; sourceTree = "<group>"; };
   1.190 +		0669D6E81BAD024700611CFA /* jdhuff.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = jdhuff.c; path = libs/libjpeg/jdhuff.c; sourceTree = "<group>"; };
   1.191 +		0669D6E91BAD024700611CFA /* jdhuff.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = jdhuff.h; path = libs/libjpeg/jdhuff.h; sourceTree = "<group>"; };
   1.192 +		0669D6EA1BAD024700611CFA /* jdinput.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = jdinput.c; path = libs/libjpeg/jdinput.c; sourceTree = "<group>"; };
   1.193 +		0669D6EB1BAD024700611CFA /* jdmainct.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = jdmainct.c; path = libs/libjpeg/jdmainct.c; sourceTree = "<group>"; };
   1.194 +		0669D6EC1BAD024700611CFA /* jdmarker.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = jdmarker.c; path = libs/libjpeg/jdmarker.c; sourceTree = "<group>"; };
   1.195 +		0669D6ED1BAD024700611CFA /* jdmaster.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = jdmaster.c; path = libs/libjpeg/jdmaster.c; sourceTree = "<group>"; };
   1.196 +		0669D6EE1BAD024700611CFA /* jdmerge.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = jdmerge.c; path = libs/libjpeg/jdmerge.c; sourceTree = "<group>"; };
   1.197 +		0669D6EF1BAD024700611CFA /* jdphuff.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = jdphuff.c; path = libs/libjpeg/jdphuff.c; sourceTree = "<group>"; };
   1.198 +		0669D6F01BAD024700611CFA /* jdpostct.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = jdpostct.c; path = libs/libjpeg/jdpostct.c; sourceTree = "<group>"; };
   1.199 +		0669D6F11BAD024700611CFA /* jdsample.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = jdsample.c; path = libs/libjpeg/jdsample.c; sourceTree = "<group>"; };
   1.200 +		0669D6F21BAD024700611CFA /* jdtrans.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = jdtrans.c; path = libs/libjpeg/jdtrans.c; sourceTree = "<group>"; };
   1.201 +		0669D6F31BAD024700611CFA /* jerror.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = jerror.c; path = libs/libjpeg/jerror.c; sourceTree = "<group>"; };
   1.202 +		0669D6F41BAD024700611CFA /* jerror.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = jerror.h; path = libs/libjpeg/jerror.h; sourceTree = "<group>"; };
   1.203 +		0669D6F51BAD024700611CFA /* jfdctflt.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = jfdctflt.c; path = libs/libjpeg/jfdctflt.c; sourceTree = "<group>"; };
   1.204 +		0669D6F61BAD024700611CFA /* jfdctfst.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = jfdctfst.c; path = libs/libjpeg/jfdctfst.c; sourceTree = "<group>"; };
   1.205 +		0669D6F71BAD024700611CFA /* jfdctint.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = jfdctint.c; path = libs/libjpeg/jfdctint.c; sourceTree = "<group>"; };
   1.206 +		0669D6F81BAD024700611CFA /* jidctflt.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = jidctflt.c; path = libs/libjpeg/jidctflt.c; sourceTree = "<group>"; };
   1.207 +		0669D6F91BAD024700611CFA /* jidctfst.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = jidctfst.c; path = libs/libjpeg/jidctfst.c; sourceTree = "<group>"; };
   1.208 +		0669D6FA1BAD024700611CFA /* jidctint.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = jidctint.c; path = libs/libjpeg/jidctint.c; sourceTree = "<group>"; };
   1.209 +		0669D6FB1BAD024700611CFA /* jidctred.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = jidctred.c; path = libs/libjpeg/jidctred.c; sourceTree = "<group>"; };
   1.210 +		0669D6FC1BAD024700611CFA /* jinclude.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = jinclude.h; path = libs/libjpeg/jinclude.h; sourceTree = "<group>"; };
   1.211 +		0669D6FD1BAD024700611CFA /* jmemmgr.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = jmemmgr.c; path = libs/libjpeg/jmemmgr.c; sourceTree = "<group>"; };
   1.212 +		0669D6FE1BAD024700611CFA /* jmemnobs.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = jmemnobs.c; path = libs/libjpeg/jmemnobs.c; sourceTree = "<group>"; };
   1.213 +		0669D6FF1BAD024700611CFA /* jmemsys.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = jmemsys.h; path = libs/libjpeg/jmemsys.h; sourceTree = "<group>"; };
   1.214 +		0669D7001BAD024700611CFA /* jmorecfg.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = jmorecfg.h; path = libs/libjpeg/jmorecfg.h; sourceTree = "<group>"; };
   1.215 +		0669D7011BAD024700611CFA /* jpegint.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = jpegint.h; path = libs/libjpeg/jpegint.h; sourceTree = "<group>"; };
   1.216 +		0669D7021BAD024700611CFA /* jpeglib.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = jpeglib.h; path = libs/libjpeg/jpeglib.h; sourceTree = "<group>"; };
   1.217 +		0669D7031BAD024700611CFA /* jquant1.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = jquant1.c; path = libs/libjpeg/jquant1.c; sourceTree = "<group>"; };
   1.218 +		0669D7041BAD024700611CFA /* jquant2.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = jquant2.c; path = libs/libjpeg/jquant2.c; sourceTree = "<group>"; };
   1.219 +		0669D7051BAD024700611CFA /* jutils.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = jutils.c; path = libs/libjpeg/jutils.c; sourceTree = "<group>"; };
   1.220 +		0669D7061BAD024700611CFA /* jversion.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = jversion.h; path = libs/libjpeg/jversion.h; sourceTree = "<group>"; };
   1.221 +		0669D7351BAD025B00611CFA /* png.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = png.c; path = libs/libpng/png.c; sourceTree = "<group>"; };
   1.222 +		0669D7361BAD025B00611CFA /* png.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = png.h; path = libs/libpng/png.h; sourceTree = "<group>"; };
   1.223 +		0669D7371BAD025B00611CFA /* pngconf.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = pngconf.h; path = libs/libpng/pngconf.h; sourceTree = "<group>"; };
   1.224 +		0669D7381BAD025B00611CFA /* pngerror.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = pngerror.c; path = libs/libpng/pngerror.c; sourceTree = "<group>"; };
   1.225 +		0669D7391BAD025B00611CFA /* pnggccrd.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = pnggccrd.c; path = libs/libpng/pnggccrd.c; sourceTree = "<group>"; };
   1.226 +		0669D73A1BAD025B00611CFA /* pngget.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = pngget.c; path = libs/libpng/pngget.c; sourceTree = "<group>"; };
   1.227 +		0669D73B1BAD025B00611CFA /* pngmem.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = pngmem.c; path = libs/libpng/pngmem.c; sourceTree = "<group>"; };
   1.228 +		0669D73C1BAD025B00611CFA /* pngpread.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = pngpread.c; path = libs/libpng/pngpread.c; sourceTree = "<group>"; };
   1.229 +		0669D73D1BAD025B00611CFA /* pngread.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = pngread.c; path = libs/libpng/pngread.c; sourceTree = "<group>"; };
   1.230 +		0669D73E1BAD025B00611CFA /* pngrio.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = pngrio.c; path = libs/libpng/pngrio.c; sourceTree = "<group>"; };
   1.231 +		0669D73F1BAD025B00611CFA /* pngrtran.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = pngrtran.c; path = libs/libpng/pngrtran.c; sourceTree = "<group>"; };
   1.232 +		0669D7401BAD025B00611CFA /* pngrutil.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = pngrutil.c; path = libs/libpng/pngrutil.c; sourceTree = "<group>"; };
   1.233 +		0669D7411BAD025B00611CFA /* pngset.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = pngset.c; path = libs/libpng/pngset.c; sourceTree = "<group>"; };
   1.234 +		0669D7421BAD025B00611CFA /* pngtrans.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = pngtrans.c; path = libs/libpng/pngtrans.c; sourceTree = "<group>"; };
   1.235 +		0669D7431BAD025B00611CFA /* pngvcrd.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = pngvcrd.c; path = libs/libpng/pngvcrd.c; sourceTree = "<group>"; };
   1.236 +		0669D7441BAD025B00611CFA /* pngwio.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = pngwio.c; path = libs/libpng/pngwio.c; sourceTree = "<group>"; };
   1.237 +		0669D7451BAD025B00611CFA /* pngwrite.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = pngwrite.c; path = libs/libpng/pngwrite.c; sourceTree = "<group>"; };
   1.238 +		0669D7461BAD025B00611CFA /* pngwtran.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = pngwtran.c; path = libs/libpng/pngwtran.c; sourceTree = "<group>"; };
   1.239 +		0669D7471BAD025B00611CFA /* pngwutil.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = pngwutil.c; path = libs/libpng/pngwutil.c; sourceTree = "<group>"; };
   1.240 +		0669D7591BAD027000611CFA /* adler32.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = adler32.c; path = libs/zlib/adler32.c; sourceTree = "<group>"; };
   1.241 +		0669D75A1BAD027000611CFA /* compress.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = compress.c; path = libs/zlib/compress.c; sourceTree = "<group>"; };
   1.242 +		0669D75B1BAD027000611CFA /* crc32.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = crc32.c; path = libs/zlib/crc32.c; sourceTree = "<group>"; };
   1.243 +		0669D75C1BAD027000611CFA /* crc32.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = crc32.h; path = libs/zlib/crc32.h; sourceTree = "<group>"; };
   1.244 +		0669D75D1BAD027000611CFA /* deflate.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = deflate.c; path = libs/zlib/deflate.c; sourceTree = "<group>"; };
   1.245 +		0669D75E1BAD027000611CFA /* deflate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = deflate.h; path = libs/zlib/deflate.h; sourceTree = "<group>"; };
   1.246 +		0669D75F1BAD027000611CFA /* gzio.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = gzio.c; path = libs/zlib/gzio.c; sourceTree = "<group>"; };
   1.247 +		0669D7601BAD027000611CFA /* infback.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = infback.c; path = libs/zlib/infback.c; sourceTree = "<group>"; };
   1.248 +		0669D7611BAD027000611CFA /* inffast.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = inffast.c; path = libs/zlib/inffast.c; sourceTree = "<group>"; };
   1.249 +		0669D7621BAD027000611CFA /* inffast.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = inffast.h; path = libs/zlib/inffast.h; sourceTree = "<group>"; };
   1.250 +		0669D7631BAD027000611CFA /* inffixed.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = inffixed.h; path = libs/zlib/inffixed.h; sourceTree = "<group>"; };
   1.251 +		0669D7641BAD027000611CFA /* inflate.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = inflate.c; path = libs/zlib/inflate.c; sourceTree = "<group>"; };
   1.252 +		0669D7651BAD027000611CFA /* inflate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = inflate.h; path = libs/zlib/inflate.h; sourceTree = "<group>"; };
   1.253 +		0669D7661BAD027000611CFA /* inftrees.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = inftrees.c; path = libs/zlib/inftrees.c; sourceTree = "<group>"; };
   1.254 +		0669D7671BAD027000611CFA /* inftrees.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = inftrees.h; path = libs/zlib/inftrees.h; sourceTree = "<group>"; };
   1.255 +		0669D7681BAD027000611CFA /* trees.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = trees.c; path = libs/zlib/trees.c; sourceTree = "<group>"; };
   1.256 +		0669D7691BAD027000611CFA /* trees.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = trees.h; path = libs/zlib/trees.h; sourceTree = "<group>"; };
   1.257 +		0669D76A1BAD027000611CFA /* uncompr.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = uncompr.c; path = libs/zlib/uncompr.c; sourceTree = "<group>"; };
   1.258 +		0669D76B1BAD027000611CFA /* zconf.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = zconf.h; path = libs/zlib/zconf.h; sourceTree = "<group>"; };
   1.259 +		0669D76C1BAD027000611CFA /* zlib.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = zlib.h; path = libs/zlib/zlib.h; sourceTree = "<group>"; };
   1.260 +		0669D76D1BAD027000611CFA /* zutil.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = zutil.c; path = libs/zlib/zutil.c; sourceTree = "<group>"; };
   1.261 +		0669D76E1BAD027000611CFA /* zutil.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = zutil.h; path = libs/zlib/zutil.h; sourceTree = "<group>"; };
   1.262 +		0669D77C1BAD02A200611CFA /* test.p.glsl */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; name = test.p.glsl; path = sdr/test.p.glsl; sourceTree = "<group>"; };
   1.263 +		0669D77D1BAD02A200611CFA /* test.v.glsl */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; name = test.v.glsl; path = sdr/test.v.glsl; sourceTree = "<group>"; };
   1.264 +		0669D77E1BAD02A200611CFA /* text.p.glsl */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; name = text.p.glsl; path = sdr/text.p.glsl; sourceTree = "<group>"; };
   1.265 +		0669D77F1BAD02A200611CFA /* text.v.glsl */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; name = text.v.glsl; path = sdr/text.v.glsl; sourceTree = "<group>"; };
   1.266 +		0669D7801BAD02A200611CFA /* tunnel.p.glsl */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; name = tunnel.p.glsl; path = sdr/tunnel.p.glsl; sourceTree = "<group>"; };
   1.267 +		0669D7811BAD02A200611CFA /* tunnel.v.glsl */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; name = tunnel.v.glsl; path = sdr/tunnel.v.glsl; sourceTree = "<group>"; };
   1.268 +		0669D7891BAD02DC00611CFA /* stonewall_normal.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; name = stonewall_normal.jpg; path = data/stonewall_normal.jpg; sourceTree = "<group>"; };
   1.269 +		0669D78A1BAD02DC00611CFA /* stonewall.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; name = stonewall.jpg; path = data/stonewall.jpg; sourceTree = "<group>"; };
   1.270 +		0669D78B1BAD02DC00611CFA /* text.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = text.png; path = data/text.png; sourceTree = "<group>"; };
   1.271 +		0669D78C1BAD02DC00611CFA /* tiles.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; name = tiles.jpg; path = data/tiles.jpg; sourceTree = "<group>"; };
   1.272  /* End PBXFileReference section */
   1.273  
   1.274  /* Begin PBXFrameworksBuildPhase section */
   1.275 @@ -37,6 +295,9 @@
   1.276  		0669D63E1BA851BE00611CFA = {
   1.277  			isa = PBXGroup;
   1.278  			children = (
   1.279 +				0669D7881BAD02B000611CFA /* data */,
   1.280 +				0669D77B1BAD028300611CFA /* sdr */,
   1.281 +				0669D6971BAD01C200611CFA /* libs */,
   1.282  				0669D6761BA858D600611CFA /* src */,
   1.283  				0669D6741BA858BF00611CFA /* Info.plist */,
   1.284  				0669D6481BA851BE00611CFA /* Products */,
   1.285 @@ -54,6 +315,21 @@
   1.286  		0669D6761BA858D600611CFA /* src */ = {
   1.287  			isa = PBXGroup;
   1.288  			children = (
   1.289 +				0669D6821BACDF6C00611CFA /* cam.c */,
   1.290 +				0669D6831BACDF6C00611CFA /* cam.h */,
   1.291 +				0669D6841BACDF6C00611CFA /* config.h */,
   1.292 +				0669D6851BACDF6C00611CFA /* istereo.c */,
   1.293 +				0669D6861BACDF6C00611CFA /* istereo.h */,
   1.294 +				0669D6871BACDF6C00611CFA /* opengl.h */,
   1.295 +				0669D6881BACDF6C00611CFA /* respath.c */,
   1.296 +				0669D6891BACDF6C00611CFA /* respath.h */,
   1.297 +				0669D68A1BACDF6C00611CFA /* sanegl.c */,
   1.298 +				0669D68B1BACDF6C00611CFA /* sanegl.h */,
   1.299 +				0669D68C1BACDF6C00611CFA /* sdr.c */,
   1.300 +				0669D68D1BACDF6C00611CFA /* sdr.h */,
   1.301 +				0669D68E1BACDF6C00611CFA /* tex.c */,
   1.302 +				0669D68F1BACDF6C00611CFA /* tex.h */,
   1.303 +				0669D6901BACDF6C00611CFA /* ui.h */,
   1.304  				0669D6791BA85DCC00611CFA /* ios */,
   1.305  			);
   1.306  			name = src;
   1.307 @@ -71,6 +347,206 @@
   1.308  			name = ios;
   1.309  			sourceTree = "<group>";
   1.310  		};
   1.311 +		0669D6971BAD01C200611CFA /* libs */ = {
   1.312 +			isa = PBXGroup;
   1.313 +			children = (
   1.314 +				0669D69C1BAD01EA00611CFA /* vmath */,
   1.315 +				0669D69B1BAD01E300611CFA /* imago2 */,
   1.316 +				0669D69A1BAD01DD00611CFA /* jpeglib */,
   1.317 +				0669D6991BAD01D500611CFA /* libpng */,
   1.318 +				0669D6981BAD01C900611CFA /* zlib */,
   1.319 +			);
   1.320 +			name = libs;
   1.321 +			sourceTree = "<group>";
   1.322 +		};
   1.323 +		0669D6981BAD01C900611CFA /* zlib */ = {
   1.324 +			isa = PBXGroup;
   1.325 +			children = (
   1.326 +				0669D7591BAD027000611CFA /* adler32.c */,
   1.327 +				0669D75A1BAD027000611CFA /* compress.c */,
   1.328 +				0669D75B1BAD027000611CFA /* crc32.c */,
   1.329 +				0669D75C1BAD027000611CFA /* crc32.h */,
   1.330 +				0669D75D1BAD027000611CFA /* deflate.c */,
   1.331 +				0669D75E1BAD027000611CFA /* deflate.h */,
   1.332 +				0669D75F1BAD027000611CFA /* gzio.c */,
   1.333 +				0669D7601BAD027000611CFA /* infback.c */,
   1.334 +				0669D7611BAD027000611CFA /* inffast.c */,
   1.335 +				0669D7621BAD027000611CFA /* inffast.h */,
   1.336 +				0669D7631BAD027000611CFA /* inffixed.h */,
   1.337 +				0669D7641BAD027000611CFA /* inflate.c */,
   1.338 +				0669D7651BAD027000611CFA /* inflate.h */,
   1.339 +				0669D7661BAD027000611CFA /* inftrees.c */,
   1.340 +				0669D7671BAD027000611CFA /* inftrees.h */,
   1.341 +				0669D7681BAD027000611CFA /* trees.c */,
   1.342 +				0669D7691BAD027000611CFA /* trees.h */,
   1.343 +				0669D76A1BAD027000611CFA /* uncompr.c */,
   1.344 +				0669D76B1BAD027000611CFA /* zconf.h */,
   1.345 +				0669D76C1BAD027000611CFA /* zlib.h */,
   1.346 +				0669D76D1BAD027000611CFA /* zutil.c */,
   1.347 +				0669D76E1BAD027000611CFA /* zutil.h */,
   1.348 +			);
   1.349 +			name = zlib;
   1.350 +			sourceTree = "<group>";
   1.351 +		};
   1.352 +		0669D6991BAD01D500611CFA /* libpng */ = {
   1.353 +			isa = PBXGroup;
   1.354 +			children = (
   1.355 +				0669D7351BAD025B00611CFA /* png.c */,
   1.356 +				0669D7361BAD025B00611CFA /* png.h */,
   1.357 +				0669D7371BAD025B00611CFA /* pngconf.h */,
   1.358 +				0669D7381BAD025B00611CFA /* pngerror.c */,
   1.359 +				0669D7391BAD025B00611CFA /* pnggccrd.c */,
   1.360 +				0669D73A1BAD025B00611CFA /* pngget.c */,
   1.361 +				0669D73B1BAD025B00611CFA /* pngmem.c */,
   1.362 +				0669D73C1BAD025B00611CFA /* pngpread.c */,
   1.363 +				0669D73D1BAD025B00611CFA /* pngread.c */,
   1.364 +				0669D73E1BAD025B00611CFA /* pngrio.c */,
   1.365 +				0669D73F1BAD025B00611CFA /* pngrtran.c */,
   1.366 +				0669D7401BAD025B00611CFA /* pngrutil.c */,
   1.367 +				0669D7411BAD025B00611CFA /* pngset.c */,
   1.368 +				0669D7421BAD025B00611CFA /* pngtrans.c */,
   1.369 +				0669D7431BAD025B00611CFA /* pngvcrd.c */,
   1.370 +				0669D7441BAD025B00611CFA /* pngwio.c */,
   1.371 +				0669D7451BAD025B00611CFA /* pngwrite.c */,
   1.372 +				0669D7461BAD025B00611CFA /* pngwtran.c */,
   1.373 +				0669D7471BAD025B00611CFA /* pngwutil.c */,
   1.374 +			);
   1.375 +			name = libpng;
   1.376 +			sourceTree = "<group>";
   1.377 +		};
   1.378 +		0669D69A1BAD01DD00611CFA /* jpeglib */ = {
   1.379 +			isa = PBXGroup;
   1.380 +			children = (
   1.381 +				0669D6CD1BAD024700611CFA /* cderror.h */,
   1.382 +				0669D6CE1BAD024700611CFA /* jcapimin.c */,
   1.383 +				0669D6CF1BAD024700611CFA /* jcapistd.c */,
   1.384 +				0669D6D01BAD024700611CFA /* jccoefct.c */,
   1.385 +				0669D6D11BAD024700611CFA /* jccolor.c */,
   1.386 +				0669D6D21BAD024700611CFA /* jcdctmgr.c */,
   1.387 +				0669D6D31BAD024700611CFA /* jchuff.c */,
   1.388 +				0669D6D41BAD024700611CFA /* jchuff.h */,
   1.389 +				0669D6D51BAD024700611CFA /* jcinit.c */,
   1.390 +				0669D6D61BAD024700611CFA /* jcmainct.c */,
   1.391 +				0669D6D71BAD024700611CFA /* jcmarker.c */,
   1.392 +				0669D6D81BAD024700611CFA /* jcmaster.c */,
   1.393 +				0669D6D91BAD024700611CFA /* jcomapi.c */,
   1.394 +				0669D6DA1BAD024700611CFA /* jconfig.h */,
   1.395 +				0669D6DB1BAD024700611CFA /* jcparam.c */,
   1.396 +				0669D6DC1BAD024700611CFA /* jcphuff.c */,
   1.397 +				0669D6DD1BAD024700611CFA /* jcprepct.c */,
   1.398 +				0669D6DE1BAD024700611CFA /* jcsample.c */,
   1.399 +				0669D6DF1BAD024700611CFA /* jctrans.c */,
   1.400 +				0669D6E01BAD024700611CFA /* jdapimin.c */,
   1.401 +				0669D6E11BAD024700611CFA /* jdapistd.c */,
   1.402 +				0669D6E21BAD024700611CFA /* jdatadst.c */,
   1.403 +				0669D6E31BAD024700611CFA /* jdatasrc.c */,
   1.404 +				0669D6E41BAD024700611CFA /* jdcoefct.c */,
   1.405 +				0669D6E51BAD024700611CFA /* jdcolor.c */,
   1.406 +				0669D6E61BAD024700611CFA /* jdct.h */,
   1.407 +				0669D6E71BAD024700611CFA /* jddctmgr.c */,
   1.408 +				0669D6E81BAD024700611CFA /* jdhuff.c */,
   1.409 +				0669D6E91BAD024700611CFA /* jdhuff.h */,
   1.410 +				0669D6EA1BAD024700611CFA /* jdinput.c */,
   1.411 +				0669D6EB1BAD024700611CFA /* jdmainct.c */,
   1.412 +				0669D6EC1BAD024700611CFA /* jdmarker.c */,
   1.413 +				0669D6ED1BAD024700611CFA /* jdmaster.c */,
   1.414 +				0669D6EE1BAD024700611CFA /* jdmerge.c */,
   1.415 +				0669D6EF1BAD024700611CFA /* jdphuff.c */,
   1.416 +				0669D6F01BAD024700611CFA /* jdpostct.c */,
   1.417 +				0669D6F11BAD024700611CFA /* jdsample.c */,
   1.418 +				0669D6F21BAD024700611CFA /* jdtrans.c */,
   1.419 +				0669D6F31BAD024700611CFA /* jerror.c */,
   1.420 +				0669D6F41BAD024700611CFA /* jerror.h */,
   1.421 +				0669D6F51BAD024700611CFA /* jfdctflt.c */,
   1.422 +				0669D6F61BAD024700611CFA /* jfdctfst.c */,
   1.423 +				0669D6F71BAD024700611CFA /* jfdctint.c */,
   1.424 +				0669D6F81BAD024700611CFA /* jidctflt.c */,
   1.425 +				0669D6F91BAD024700611CFA /* jidctfst.c */,
   1.426 +				0669D6FA1BAD024700611CFA /* jidctint.c */,
   1.427 +				0669D6FB1BAD024700611CFA /* jidctred.c */,
   1.428 +				0669D6FC1BAD024700611CFA /* jinclude.h */,
   1.429 +				0669D6FD1BAD024700611CFA /* jmemmgr.c */,
   1.430 +				0669D6FE1BAD024700611CFA /* jmemnobs.c */,
   1.431 +				0669D6FF1BAD024700611CFA /* jmemsys.h */,
   1.432 +				0669D7001BAD024700611CFA /* jmorecfg.h */,
   1.433 +				0669D7011BAD024700611CFA /* jpegint.h */,
   1.434 +				0669D7021BAD024700611CFA /* jpeglib.h */,
   1.435 +				0669D7031BAD024700611CFA /* jquant1.c */,
   1.436 +				0669D7041BAD024700611CFA /* jquant2.c */,
   1.437 +				0669D7051BAD024700611CFA /* jutils.c */,
   1.438 +				0669D7061BAD024700611CFA /* jversion.h */,
   1.439 +			);
   1.440 +			name = jpeglib;
   1.441 +			sourceTree = "<group>";
   1.442 +		};
   1.443 +		0669D69B1BAD01E300611CFA /* imago2 */ = {
   1.444 +			isa = PBXGroup;
   1.445 +			children = (
   1.446 +				0669D6BD1BAD022C00611CFA /* conv.c */,
   1.447 +				0669D6BE1BAD022C00611CFA /* file_jpeg.c */,
   1.448 +				0669D6BF1BAD022C00611CFA /* file_png.c */,
   1.449 +				0669D6C01BAD022C00611CFA /* file_ppm.c */,
   1.450 +				0669D6C11BAD022C00611CFA /* ftype_module.c */,
   1.451 +				0669D6C21BAD022C00611CFA /* ftype_module.h */,
   1.452 +				0669D6C31BAD022C00611CFA /* imago2.c */,
   1.453 +				0669D6C41BAD022C00611CFA /* imago2.h */,
   1.454 +				0669D6C51BAD022C00611CFA /* modules.c */,
   1.455 +			);
   1.456 +			name = imago2;
   1.457 +			sourceTree = "<group>";
   1.458 +		};
   1.459 +		0669D69C1BAD01EA00611CFA /* vmath */ = {
   1.460 +			isa = PBXGroup;
   1.461 +			children = (
   1.462 +				0669D69D1BAD020B00611CFA /* basis_c.c */,
   1.463 +				0669D69E1BAD020B00611CFA /* basis.h */,
   1.464 +				0669D69F1BAD020B00611CFA /* geom.c */,
   1.465 +				0669D6A01BAD020B00611CFA /* geom.h */,
   1.466 +				0669D6A11BAD020B00611CFA /* matrix_c.c */,
   1.467 +				0669D6A21BAD020B00611CFA /* matrix.h */,
   1.468 +				0669D6A31BAD020B00611CFA /* matrix.inl */,
   1.469 +				0669D6A41BAD020B00611CFA /* quat_c.c */,
   1.470 +				0669D6A51BAD020B00611CFA /* quat.h */,
   1.471 +				0669D6A61BAD020B00611CFA /* quat.inl */,
   1.472 +				0669D6A71BAD020B00611CFA /* ray_c.c */,
   1.473 +				0669D6A81BAD020B00611CFA /* ray.h */,
   1.474 +				0669D6A91BAD020B00611CFA /* ray.inl */,
   1.475 +				0669D6AA1BAD020B00611CFA /* sphvec.h */,
   1.476 +				0669D6AB1BAD020B00611CFA /* vector.h */,
   1.477 +				0669D6AC1BAD020B00611CFA /* vector.inl */,
   1.478 +				0669D6AD1BAD020B00611CFA /* vmath_config.h */,
   1.479 +				0669D6AE1BAD020B00611CFA /* vmath_types.h */,
   1.480 +				0669D6AF1BAD020B00611CFA /* vmath.c */,
   1.481 +				0669D6B01BAD020B00611CFA /* vmath.h */,
   1.482 +				0669D6B11BAD020B00611CFA /* vmath.inl */,
   1.483 +			);
   1.484 +			name = vmath;
   1.485 +			sourceTree = "<group>";
   1.486 +		};
   1.487 +		0669D77B1BAD028300611CFA /* sdr */ = {
   1.488 +			isa = PBXGroup;
   1.489 +			children = (
   1.490 +				0669D77C1BAD02A200611CFA /* test.p.glsl */,
   1.491 +				0669D77D1BAD02A200611CFA /* test.v.glsl */,
   1.492 +				0669D77E1BAD02A200611CFA /* text.p.glsl */,
   1.493 +				0669D77F1BAD02A200611CFA /* text.v.glsl */,
   1.494 +				0669D7801BAD02A200611CFA /* tunnel.p.glsl */,
   1.495 +				0669D7811BAD02A200611CFA /* tunnel.v.glsl */,
   1.496 +			);
   1.497 +			name = sdr;
   1.498 +			sourceTree = "<group>";
   1.499 +		};
   1.500 +		0669D7881BAD02B000611CFA /* data */ = {
   1.501 +			isa = PBXGroup;
   1.502 +			children = (
   1.503 +				0669D7891BAD02DC00611CFA /* stonewall_normal.jpg */,
   1.504 +				0669D78A1BAD02DC00611CFA /* stonewall.jpg */,
   1.505 +				0669D78B1BAD02DC00611CFA /* text.png */,
   1.506 +				0669D78C1BAD02DC00611CFA /* tiles.jpg */,
   1.507 +			);
   1.508 +			name = data;
   1.509 +			sourceTree = "<group>";
   1.510 +		};
   1.511  /* End PBXGroup section */
   1.512  
   1.513  /* Begin PBXNativeTarget section */
   1.514 @@ -129,7 +605,17 @@
   1.515  			isa = PBXResourcesBuildPhase;
   1.516  			buildActionMask = 2147483647;
   1.517  			files = (
   1.518 +				0669D7861BAD02A200611CFA /* tunnel.p.glsl in Resources */,
   1.519  				0669D6751BA858BF00611CFA /* Info.plist in Resources */,
   1.520 +				0669D7821BAD02A200611CFA /* test.p.glsl in Resources */,
   1.521 +				0669D78D1BAD02DC00611CFA /* stonewall_normal.jpg in Resources */,
   1.522 +				0669D78E1BAD02DC00611CFA /* stonewall.jpg in Resources */,
   1.523 +				0669D7841BAD02A200611CFA /* text.p.glsl in Resources */,
   1.524 +				0669D7901BAD02DC00611CFA /* tiles.jpg in Resources */,
   1.525 +				0669D7871BAD02A200611CFA /* tunnel.v.glsl in Resources */,
   1.526 +				0669D78F1BAD02DC00611CFA /* text.png in Resources */,
   1.527 +				0669D7851BAD02A200611CFA /* text.v.glsl in Resources */,
   1.528 +				0669D7831BAD02A200611CFA /* test.v.glsl in Resources */,
   1.529  			);
   1.530  			runOnlyForDeploymentPostprocessing = 0;
   1.531  		};
   1.532 @@ -140,9 +626,103 @@
   1.533  			isa = PBXSourcesBuildPhase;
   1.534  			buildActionMask = 2147483647;
   1.535  			files = (
   1.536 +				0669D7121BAD024700611CFA /* jcparam.c in Sources */,
   1.537 +				0669D7551BAD025B00611CFA /* pngwio.c in Sources */,
   1.538 +				0669D70F1BAD024700611CFA /* jcmarker.c in Sources */,
   1.539 +				0669D6CB1BAD022C00611CFA /* imago2.c in Sources */,
   1.540 +				0669D70B1BAD024700611CFA /* jcdctmgr.c in Sources */,
   1.541 +				0669D6921BACDF6C00611CFA /* istereo.c in Sources */,
   1.542 +				0669D72F1BAD024700611CFA /* jidctred.c in Sources */,
   1.543 +				0669D7481BAD025B00611CFA /* png.c in Sources */,
   1.544 +				0669D7201BAD024700611CFA /* jdmainct.c in Sources */,
   1.545 +				0669D7531BAD025B00611CFA /* pngtrans.c in Sources */,
   1.546 +				0669D6931BACDF6C00611CFA /* respath.c in Sources */,
   1.547 +				0669D7771BAD027000611CFA /* inftrees.c in Sources */,
   1.548 +				0669D7761BAD027000611CFA /* inflate.c in Sources */,
   1.549 +				0669D6CC1BAD022C00611CFA /* modules.c in Sources */,
   1.550 +				0669D7301BAD024700611CFA /* jmemmgr.c in Sources */,
   1.551 +				0669D72E1BAD024700611CFA /* jidctint.c in Sources */,
   1.552 +				0669D6B81BAD020B00611CFA /* ray_c.c in Sources */,
   1.553 +				0669D71D1BAD024700611CFA /* jddctmgr.c in Sources */,
   1.554 +				0669D7571BAD025B00611CFA /* pngwtran.c in Sources */,
   1.555 +				0669D7081BAD024700611CFA /* jcapistd.c in Sources */,
   1.556 +				0669D72C1BAD024700611CFA /* jidctflt.c in Sources */,
   1.557 +				0669D7511BAD025B00611CFA /* pngrutil.c in Sources */,
   1.558 +				0669D6BB1BAD020B00611CFA /* vmath.c in Sources */,
   1.559 +				0669D7341BAD024700611CFA /* jutils.c in Sources */,
   1.560 +				0669D6CA1BAD022C00611CFA /* ftype_module.c in Sources */,
   1.561 +				0669D7281BAD024700611CFA /* jerror.c in Sources */,
   1.562 +				0669D7221BAD024700611CFA /* jdmaster.c in Sources */,
   1.563 +				0669D76F1BAD027000611CFA /* adler32.c in Sources */,
   1.564 +				0669D7541BAD025B00611CFA /* pngvcrd.c in Sources */,
   1.565 +				0669D72A1BAD024700611CFA /* jfdctfst.c in Sources */,
   1.566 +				0669D7791BAD027000611CFA /* uncompr.c in Sources */,
   1.567 +				0669D6941BACDF6C00611CFA /* sanegl.c in Sources */,
   1.568 +				0669D70D1BAD024700611CFA /* jcinit.c in Sources */,
   1.569 +				0669D6961BACDF6C00611CFA /* tex.c in Sources */,
   1.570 +				0669D7241BAD024700611CFA /* jdphuff.c in Sources */,
   1.571 +				0669D70C1BAD024700611CFA /* jchuff.c in Sources */,
   1.572 +				0669D71B1BAD024700611CFA /* jdcoefct.c in Sources */,
   1.573 +				0669D7091BAD024700611CFA /* jccoefct.c in Sources */,
   1.574  				0669D6801BA85DDE00611CFA /* main.m in Sources */,
   1.575 +				0669D7521BAD025B00611CFA /* pngset.c in Sources */,
   1.576 +				0669D7561BAD025B00611CFA /* pngwrite.c in Sources */,
   1.577 +				0669D7131BAD024700611CFA /* jcphuff.c in Sources */,
   1.578 +				0669D70A1BAD024700611CFA /* jccolor.c in Sources */,
   1.579 +				0669D74F1BAD025B00611CFA /* pngrio.c in Sources */,
   1.580 +				0669D6C81BAD022C00611CFA /* file_png.c in Sources */,
   1.581 +				0669D7231BAD024700611CFA /* jdmerge.c in Sources */,
   1.582 +				0669D77A1BAD027000611CFA /* zutil.c in Sources */,
   1.583 +				0669D7721BAD027000611CFA /* deflate.c in Sources */,
   1.584 +				0669D71F1BAD024700611CFA /* jdinput.c in Sources */,
   1.585 +				0669D7581BAD025B00611CFA /* pngwutil.c in Sources */,
   1.586 +				0669D7261BAD024700611CFA /* jdsample.c in Sources */,
   1.587 +				0669D7701BAD027000611CFA /* compress.c in Sources */,
   1.588 +				0669D7111BAD024700611CFA /* jcomapi.c in Sources */,
   1.589 +				0669D71A1BAD024700611CFA /* jdatasrc.c in Sources */,
   1.590 +				0669D74B1BAD025B00611CFA /* pngget.c in Sources */,
   1.591 +				0669D7211BAD024700611CFA /* jdmarker.c in Sources */,
   1.592 +				0669D7251BAD024700611CFA /* jdpostct.c in Sources */,
   1.593  				0669D6811BA85DDE00611CFA /* viewctl.m in Sources */,
   1.594  				0669D67F1BA85DDE00611CFA /* app_delegate.m in Sources */,
   1.595 +				0669D7101BAD024700611CFA /* jcmaster.c in Sources */,
   1.596 +				0669D72B1BAD024700611CFA /* jfdctint.c in Sources */,
   1.597 +				0669D7291BAD024700611CFA /* jfdctflt.c in Sources */,
   1.598 +				0669D6911BACDF6C00611CFA /* cam.c in Sources */,
   1.599 +				0669D7191BAD024700611CFA /* jdatadst.c in Sources */,
   1.600 +				0669D7731BAD027000611CFA /* gzio.c in Sources */,
   1.601 +				0669D6B61BAD020B00611CFA /* quat_c.c in Sources */,
   1.602 +				0669D6C71BAD022C00611CFA /* file_jpeg.c in Sources */,
   1.603 +				0669D7711BAD027000611CFA /* crc32.c in Sources */,
   1.604 +				0669D6B31BAD020B00611CFA /* geom.c in Sources */,
   1.605 +				0669D7741BAD027000611CFA /* infback.c in Sources */,
   1.606 +				0669D74C1BAD025B00611CFA /* pngmem.c in Sources */,
   1.607 +				0669D7271BAD024700611CFA /* jdtrans.c in Sources */,
   1.608 +				0669D6B21BAD020B00611CFA /* basis_c.c in Sources */,
   1.609 +				0669D7161BAD024700611CFA /* jctrans.c in Sources */,
   1.610 +				0669D7311BAD024700611CFA /* jmemnobs.c in Sources */,
   1.611 +				0669D74E1BAD025B00611CFA /* pngread.c in Sources */,
   1.612 +				0669D71E1BAD024700611CFA /* jdhuff.c in Sources */,
   1.613 +				0669D74A1BAD025B00611CFA /* pnggccrd.c in Sources */,
   1.614 +				0669D70E1BAD024700611CFA /* jcmainct.c in Sources */,
   1.615 +				0669D7151BAD024700611CFA /* jcsample.c in Sources */,
   1.616 +				0669D7141BAD024700611CFA /* jcprepct.c in Sources */,
   1.617 +				0669D7751BAD027000611CFA /* inffast.c in Sources */,
   1.618 +				0669D7491BAD025B00611CFA /* pngerror.c in Sources */,
   1.619 +				0669D7171BAD024700611CFA /* jdapimin.c in Sources */,
   1.620 +				0669D7331BAD024700611CFA /* jquant2.c in Sources */,
   1.621 +				0669D74D1BAD025B00611CFA /* pngpread.c in Sources */,
   1.622 +				0669D7071BAD024700611CFA /* jcapimin.c in Sources */,
   1.623 +				0669D6C61BAD022C00611CFA /* conv.c in Sources */,
   1.624 +				0669D6B41BAD020B00611CFA /* matrix_c.c in Sources */,
   1.625 +				0669D7321BAD024700611CFA /* jquant1.c in Sources */,
   1.626 +				0669D6C91BAD022C00611CFA /* file_ppm.c in Sources */,
   1.627 +				0669D72D1BAD024700611CFA /* jidctfst.c in Sources */,
   1.628 +				0669D7501BAD025B00611CFA /* pngrtran.c in Sources */,
   1.629 +				0669D7181BAD024700611CFA /* jdapistd.c in Sources */,
   1.630 +				0669D6951BACDF6C00611CFA /* sdr.c in Sources */,
   1.631 +				0669D7781BAD027000611CFA /* trees.c in Sources */,
   1.632 +				0669D71C1BAD024700611CFA /* jdcolor.c in Sources */,
   1.633  			);
   1.634  			runOnlyForDeploymentPostprocessing = 0;
   1.635  		};
   1.636 @@ -234,6 +814,7 @@
   1.637  		0669D66F1BA851BE00611CFA /* Debug */ = {
   1.638  			isa = XCBuildConfiguration;
   1.639  			buildSettings = {
   1.640 +				ALWAYS_SEARCH_USER_PATHS = YES;
   1.641  				ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
   1.642  				CODE_SIGN_IDENTITY = "iPhone Developer";
   1.643  				INFOPLIST_FILE = "$(SRCROOT)/ios/Info.plist";
   1.644 @@ -246,6 +827,7 @@
   1.645  		0669D6701BA851BE00611CFA /* Release */ = {
   1.646  			isa = XCBuildConfiguration;
   1.647  			buildSettings = {
   1.648 +				ALWAYS_SEARCH_USER_PATHS = YES;
   1.649  				ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
   1.650  				CODE_SIGN_IDENTITY = "iPhone Developer";
   1.651  				INFOPLIST_FILE = "$(SRCROOT)/ios/Info.plist";
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/libs/Makefile	Sat Sep 19 05:51:51 2015 +0300
     2.3 @@ -0,0 +1,7 @@
     2.4 +src += $(wildcard libs/zlib/*.c) \
     2.5 +	   $(wildcard libs/libpng/*.c) \
     2.6 +	   $(wildcard libs/libjpeg/*.c) \
     2.7 +	   $(wildcard libs/imago2/*.c) \
     2.8 +	   $(wildcard libs/vmath/*.c)
     2.9 +
    2.10 +incdir += -Ilibs/imago2 -Ilibs/zlib -Ilibs/libpng -Ilibs/libjpeg -Ilibs/vmath
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/libs/imago2/COPYING	Sat Sep 19 05:51:51 2015 +0300
     3.3 @@ -0,0 +1,674 @@
     3.4 +                    GNU GENERAL PUBLIC LICENSE
     3.5 +                       Version 3, 29 June 2007
     3.6 +
     3.7 + Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
     3.8 + Everyone is permitted to copy and distribute verbatim copies
     3.9 + of this license document, but changing it is not allowed.
    3.10 +
    3.11 +                            Preamble
    3.12 +
    3.13 +  The GNU General Public License is a free, copyleft license for
    3.14 +software and other kinds of works.
    3.15 +
    3.16 +  The licenses for most software and other practical works are designed
    3.17 +to take away your freedom to share and change the works.  By contrast,
    3.18 +the GNU General Public License is intended to guarantee your freedom to
    3.19 +share and change all versions of a program--to make sure it remains free
    3.20 +software for all its users.  We, the Free Software Foundation, use the
    3.21 +GNU General Public License for most of our software; it applies also to
    3.22 +any other work released this way by its authors.  You can apply it to
    3.23 +your programs, too.
    3.24 +
    3.25 +  When we speak of free software, we are referring to freedom, not
    3.26 +price.  Our General Public Licenses are designed to make sure that you
    3.27 +have the freedom to distribute copies of free software (and charge for
    3.28 +them if you wish), that you receive source code or can get it if you
    3.29 +want it, that you can change the software or use pieces of it in new
    3.30 +free programs, and that you know you can do these things.
    3.31 +
    3.32 +  To protect your rights, we need to prevent others from denying you
    3.33 +these rights or asking you to surrender the rights.  Therefore, you have
    3.34 +certain responsibilities if you distribute copies of the software, or if
    3.35 +you modify it: responsibilities to respect the freedom of others.
    3.36 +
    3.37 +  For example, if you distribute copies of such a program, whether
    3.38 +gratis or for a fee, you must pass on to the recipients the same
    3.39 +freedoms that you received.  You must make sure that they, too, receive
    3.40 +or can get the source code.  And you must show them these terms so they
    3.41 +know their rights.
    3.42 +
    3.43 +  Developers that use the GNU GPL protect your rights with two steps:
    3.44 +(1) assert copyright on the software, and (2) offer you this License
    3.45 +giving you legal permission to copy, distribute and/or modify it.
    3.46 +
    3.47 +  For the developers' and authors' protection, the GPL clearly explains
    3.48 +that there is no warranty for this free software.  For both users' and
    3.49 +authors' sake, the GPL requires that modified versions be marked as
    3.50 +changed, so that their problems will not be attributed erroneously to
    3.51 +authors of previous versions.
    3.52 +
    3.53 +  Some devices are designed to deny users access to install or run
    3.54 +modified versions of the software inside them, although the manufacturer
    3.55 +can do so.  This is fundamentally incompatible with the aim of
    3.56 +protecting users' freedom to change the software.  The systematic
    3.57 +pattern of such abuse occurs in the area of products for individuals to
    3.58 +use, which is precisely where it is most unacceptable.  Therefore, we
    3.59 +have designed this version of the GPL to prohibit the practice for those
    3.60 +products.  If such problems arise substantially in other domains, we
    3.61 +stand ready to extend this provision to those domains in future versions
    3.62 +of the GPL, as needed to protect the freedom of users.
    3.63 +
    3.64 +  Finally, every program is threatened constantly by software patents.
    3.65 +States should not allow patents to restrict development and use of
    3.66 +software on general-purpose computers, but in those that do, we wish to
    3.67 +avoid the special danger that patents applied to a free program could
    3.68 +make it effectively proprietary.  To prevent this, the GPL assures that
    3.69 +patents cannot be used to render the program non-free.
    3.70 +
    3.71 +  The precise terms and conditions for copying, distribution and
    3.72 +modification follow.
    3.73 +
    3.74 +                       TERMS AND CONDITIONS
    3.75 +
    3.76 +  0. Definitions.
    3.77 +
    3.78 +  "This License" refers to version 3 of the GNU General Public License.
    3.79 +
    3.80 +  "Copyright" also means copyright-like laws that apply to other kinds of
    3.81 +works, such as semiconductor masks.
    3.82 +
    3.83 +  "The Program" refers to any copyrightable work licensed under this
    3.84 +License.  Each licensee is addressed as "you".  "Licensees" and
    3.85 +"recipients" may be individuals or organizations.
    3.86 +
    3.87 +  To "modify" a work means to copy from or adapt all or part of the work
    3.88 +in a fashion requiring copyright permission, other than the making of an
    3.89 +exact copy.  The resulting work is called a "modified version" of the
    3.90 +earlier work or a work "based on" the earlier work.
    3.91 +
    3.92 +  A "covered work" means either the unmodified Program or a work based
    3.93 +on the Program.
    3.94 +
    3.95 +  To "propagate" a work means to do anything with it that, without
    3.96 +permission, would make you directly or secondarily liable for
    3.97 +infringement under applicable copyright law, except executing it on a
    3.98 +computer or modifying a private copy.  Propagation includes copying,
    3.99 +distribution (with or without modification), making available to the
   3.100 +public, and in some countries other activities as well.
   3.101 +
   3.102 +  To "convey" a work means any kind of propagation that enables other
   3.103 +parties to make or receive copies.  Mere interaction with a user through
   3.104 +a computer network, with no transfer of a copy, is not conveying.
   3.105 +
   3.106 +  An interactive user interface displays "Appropriate Legal Notices"
   3.107 +to the extent that it includes a convenient and prominently visible
   3.108 +feature that (1) displays an appropriate copyright notice, and (2)
   3.109 +tells the user that there is no warranty for the work (except to the
   3.110 +extent that warranties are provided), that licensees may convey the
   3.111 +work under this License, and how to view a copy of this License.  If
   3.112 +the interface presents a list of user commands or options, such as a
   3.113 +menu, a prominent item in the list meets this criterion.
   3.114 +
   3.115 +  1. Source Code.
   3.116 +
   3.117 +  The "source code" for a work means the preferred form of the work
   3.118 +for making modifications to it.  "Object code" means any non-source
   3.119 +form of a work.
   3.120 +
   3.121 +  A "Standard Interface" means an interface that either is an official
   3.122 +standard defined by a recognized standards body, or, in the case of
   3.123 +interfaces specified for a particular programming language, one that
   3.124 +is widely used among developers working in that language.
   3.125 +
   3.126 +  The "System Libraries" of an executable work include anything, other
   3.127 +than the work as a whole, that (a) is included in the normal form of
   3.128 +packaging a Major Component, but which is not part of that Major
   3.129 +Component, and (b) serves only to enable use of the work with that
   3.130 +Major Component, or to implement a Standard Interface for which an
   3.131 +implementation is available to the public in source code form.  A
   3.132 +"Major Component", in this context, means a major essential component
   3.133 +(kernel, window system, and so on) of the specific operating system
   3.134 +(if any) on which the executable work runs, or a compiler used to
   3.135 +produce the work, or an object code interpreter used to run it.
   3.136 +
   3.137 +  The "Corresponding Source" for a work in object code form means all
   3.138 +the source code needed to generate, install, and (for an executable
   3.139 +work) run the object code and to modify the work, including scripts to
   3.140 +control those activities.  However, it does not include the work's
   3.141 +System Libraries, or general-purpose tools or generally available free
   3.142 +programs which are used unmodified in performing those activities but
   3.143 +which are not part of the work.  For example, Corresponding Source
   3.144 +includes interface definition files associated with source files for
   3.145 +the work, and the source code for shared libraries and dynamically
   3.146 +linked subprograms that the work is specifically designed to require,
   3.147 +such as by intimate data communication or control flow between those
   3.148 +subprograms and other parts of the work.
   3.149 +
   3.150 +  The Corresponding Source need not include anything that users
   3.151 +can regenerate automatically from other parts of the Corresponding
   3.152 +Source.
   3.153 +
   3.154 +  The Corresponding Source for a work in source code form is that
   3.155 +same work.
   3.156 +
   3.157 +  2. Basic Permissions.
   3.158 +
   3.159 +  All rights granted under this License are granted for the term of
   3.160 +copyright on the Program, and are irrevocable provided the stated
   3.161 +conditions are met.  This License explicitly affirms your unlimited
   3.162 +permission to run the unmodified Program.  The output from running a
   3.163 +covered work is covered by this License only if the output, given its
   3.164 +content, constitutes a covered work.  This License acknowledges your
   3.165 +rights of fair use or other equivalent, as provided by copyright law.
   3.166 +
   3.167 +  You may make, run and propagate covered works that you do not
   3.168 +convey, without conditions so long as your license otherwise remains
   3.169 +in force.  You may convey covered works to others for the sole purpose
   3.170 +of having them make modifications exclusively for you, or provide you
   3.171 +with facilities for running those works, provided that you comply with
   3.172 +the terms of this License in conveying all material for which you do
   3.173 +not control copyright.  Those thus making or running the covered works
   3.174 +for you must do so exclusively on your behalf, under your direction
   3.175 +and control, on terms that prohibit them from making any copies of
   3.176 +your copyrighted material outside their relationship with you.
   3.177 +
   3.178 +  Conveying under any other circumstances is permitted solely under
   3.179 +the conditions stated below.  Sublicensing is not allowed; section 10
   3.180 +makes it unnecessary.
   3.181 +
   3.182 +  3. Protecting Users' Legal Rights From Anti-Circumvention Law.
   3.183 +
   3.184 +  No covered work shall be deemed part of an effective technological
   3.185 +measure under any applicable law fulfilling obligations under article
   3.186 +11 of the WIPO copyright treaty adopted on 20 December 1996, or
   3.187 +similar laws prohibiting or restricting circumvention of such
   3.188 +measures.
   3.189 +
   3.190 +  When you convey a covered work, you waive any legal power to forbid
   3.191 +circumvention of technological measures to the extent such circumvention
   3.192 +is effected by exercising rights under this License with respect to
   3.193 +the covered work, and you disclaim any intention to limit operation or
   3.194 +modification of the work as a means of enforcing, against the work's
   3.195 +users, your or third parties' legal rights to forbid circumvention of
   3.196 +technological measures.
   3.197 +
   3.198 +  4. Conveying Verbatim Copies.
   3.199 +
   3.200 +  You may convey verbatim copies of the Program's source code as you
   3.201 +receive it, in any medium, provided that you conspicuously and
   3.202 +appropriately publish on each copy an appropriate copyright notice;
   3.203 +keep intact all notices stating that this License and any
   3.204 +non-permissive terms added in accord with section 7 apply to the code;
   3.205 +keep intact all notices of the absence of any warranty; and give all
   3.206 +recipients a copy of this License along with the Program.
   3.207 +
   3.208 +  You may charge any price or no price for each copy that you convey,
   3.209 +and you may offer support or warranty protection for a fee.
   3.210 +
   3.211 +  5. Conveying Modified Source Versions.
   3.212 +
   3.213 +  You may convey a work based on the Program, or the modifications to
   3.214 +produce it from the Program, in the form of source code under the
   3.215 +terms of section 4, provided that you also meet all of these conditions:
   3.216 +
   3.217 +    a) The work must carry prominent notices stating that you modified
   3.218 +    it, and giving a relevant date.
   3.219 +
   3.220 +    b) The work must carry prominent notices stating that it is
   3.221 +    released under this License and any conditions added under section
   3.222 +    7.  This requirement modifies the requirement in section 4 to
   3.223 +    "keep intact all notices".
   3.224 +
   3.225 +    c) You must license the entire work, as a whole, under this
   3.226 +    License to anyone who comes into possession of a copy.  This
   3.227 +    License will therefore apply, along with any applicable section 7
   3.228 +    additional terms, to the whole of the work, and all its parts,
   3.229 +    regardless of how they are packaged.  This License gives no
   3.230 +    permission to license the work in any other way, but it does not
   3.231 +    invalidate such permission if you have separately received it.
   3.232 +
   3.233 +    d) If the work has interactive user interfaces, each must display
   3.234 +    Appropriate Legal Notices; however, if the Program has interactive
   3.235 +    interfaces that do not display Appropriate Legal Notices, your
   3.236 +    work need not make them do so.
   3.237 +
   3.238 +  A compilation of a covered work with other separate and independent
   3.239 +works, which are not by their nature extensions of the covered work,
   3.240 +and which are not combined with it such as to form a larger program,
   3.241 +in or on a volume of a storage or distribution medium, is called an
   3.242 +"aggregate" if the compilation and its resulting copyright are not
   3.243 +used to limit the access or legal rights of the compilation's users
   3.244 +beyond what the individual works permit.  Inclusion of a covered work
   3.245 +in an aggregate does not cause this License to apply to the other
   3.246 +parts of the aggregate.
   3.247 +
   3.248 +  6. Conveying Non-Source Forms.
   3.249 +
   3.250 +  You may convey a covered work in object code form under the terms
   3.251 +of sections 4 and 5, provided that you also convey the
   3.252 +machine-readable Corresponding Source under the terms of this License,
   3.253 +in one of these ways:
   3.254 +
   3.255 +    a) Convey the object code in, or embodied in, a physical product
   3.256 +    (including a physical distribution medium), accompanied by the
   3.257 +    Corresponding Source fixed on a durable physical medium
   3.258 +    customarily used for software interchange.
   3.259 +
   3.260 +    b) Convey the object code in, or embodied in, a physical product
   3.261 +    (including a physical distribution medium), accompanied by a
   3.262 +    written offer, valid for at least three years and valid for as
   3.263 +    long as you offer spare parts or customer support for that product
   3.264 +    model, to give anyone who possesses the object code either (1) a
   3.265 +    copy of the Corresponding Source for all the software in the
   3.266 +    product that is covered by this License, on a durable physical
   3.267 +    medium customarily used for software interchange, for a price no
   3.268 +    more than your reasonable cost of physically performing this
   3.269 +    conveying of source, or (2) access to copy the
   3.270 +    Corresponding Source from a network server at no charge.
   3.271 +
   3.272 +    c) Convey individual copies of the object code with a copy of the
   3.273 +    written offer to provide the Corresponding Source.  This
   3.274 +    alternative is allowed only occasionally and noncommercially, and
   3.275 +    only if you received the object code with such an offer, in accord
   3.276 +    with subsection 6b.
   3.277 +
   3.278 +    d) Convey the object code by offering access from a designated
   3.279 +    place (gratis or for a charge), and offer equivalent access to the
   3.280 +    Corresponding Source in the same way through the same place at no
   3.281 +    further charge.  You need not require recipients to copy the
   3.282 +    Corresponding Source along with the object code.  If the place to
   3.283 +    copy the object code is a network server, the Corresponding Source
   3.284 +    may be on a different server (operated by you or a third party)
   3.285 +    that supports equivalent copying facilities, provided you maintain
   3.286 +    clear directions next to the object code saying where to find the
   3.287 +    Corresponding Source.  Regardless of what server hosts the
   3.288 +    Corresponding Source, you remain obligated to ensure that it is
   3.289 +    available for as long as needed to satisfy these requirements.
   3.290 +
   3.291 +    e) Convey the object code using peer-to-peer transmission, provided
   3.292 +    you inform other peers where the object code and Corresponding
   3.293 +    Source of the work are being offered to the general public at no
   3.294 +    charge under subsection 6d.
   3.295 +
   3.296 +  A separable portion of the object code, whose source code is excluded
   3.297 +from the Corresponding Source as a System Library, need not be
   3.298 +included in conveying the object code work.
   3.299 +
   3.300 +  A "User Product" is either (1) a "consumer product", which means any
   3.301 +tangible personal property which is normally used for personal, family,
   3.302 +or household purposes, or (2) anything designed or sold for incorporation
   3.303 +into a dwelling.  In determining whether a product is a consumer product,
   3.304 +doubtful cases shall be resolved in favor of coverage.  For a particular
   3.305 +product received by a particular user, "normally used" refers to a
   3.306 +typical or common use of that class of product, regardless of the status
   3.307 +of the particular user or of the way in which the particular user
   3.308 +actually uses, or expects or is expected to use, the product.  A product
   3.309 +is a consumer product regardless of whether the product has substantial
   3.310 +commercial, industrial or non-consumer uses, unless such uses represent
   3.311 +the only significant mode of use of the product.
   3.312 +
   3.313 +  "Installation Information" for a User Product means any methods,
   3.314 +procedures, authorization keys, or other information required to install
   3.315 +and execute modified versions of a covered work in that User Product from
   3.316 +a modified version of its Corresponding Source.  The information must
   3.317 +suffice to ensure that the continued functioning of the modified object
   3.318 +code is in no case prevented or interfered with solely because
   3.319 +modification has been made.
   3.320 +
   3.321 +  If you convey an object code work under this section in, or with, or
   3.322 +specifically for use in, a User Product, and the conveying occurs as
   3.323 +part of a transaction in which the right of possession and use of the
   3.324 +User Product is transferred to the recipient in perpetuity or for a
   3.325 +fixed term (regardless of how the transaction is characterized), the
   3.326 +Corresponding Source conveyed under this section must be accompanied
   3.327 +by the Installation Information.  But this requirement does not apply
   3.328 +if neither you nor any third party retains the ability to install
   3.329 +modified object code on the User Product (for example, the work has
   3.330 +been installed in ROM).
   3.331 +
   3.332 +  The requirement to provide Installation Information does not include a
   3.333 +requirement to continue to provide support service, warranty, or updates
   3.334 +for a work that has been modified or installed by the recipient, or for
   3.335 +the User Product in which it has been modified or installed.  Access to a
   3.336 +network may be denied when the modification itself materially and
   3.337 +adversely affects the operation of the network or violates the rules and
   3.338 +protocols for communication across the network.
   3.339 +
   3.340 +  Corresponding Source conveyed, and Installation Information provided,
   3.341 +in accord with this section must be in a format that is publicly
   3.342 +documented (and with an implementation available to the public in
   3.343 +source code form), and must require no special password or key for
   3.344 +unpacking, reading or copying.
   3.345 +
   3.346 +  7. Additional Terms.
   3.347 +
   3.348 +  "Additional permissions" are terms that supplement the terms of this
   3.349 +License by making exceptions from one or more of its conditions.
   3.350 +Additional permissions that are applicable to the entire Program shall
   3.351 +be treated as though they were included in this License, to the extent
   3.352 +that they are valid under applicable law.  If additional permissions
   3.353 +apply only to part of the Program, that part may be used separately
   3.354 +under those permissions, but the entire Program remains governed by
   3.355 +this License without regard to the additional permissions.
   3.356 +
   3.357 +  When you convey a copy of a covered work, you may at your option
   3.358 +remove any additional permissions from that copy, or from any part of
   3.359 +it.  (Additional permissions may be written to require their own
   3.360 +removal in certain cases when you modify the work.)  You may place
   3.361 +additional permissions on material, added by you to a covered work,
   3.362 +for which you have or can give appropriate copyright permission.
   3.363 +
   3.364 +  Notwithstanding any other provision of this License, for material you
   3.365 +add to a covered work, you may (if authorized by the copyright holders of
   3.366 +that material) supplement the terms of this License with terms:
   3.367 +
   3.368 +    a) Disclaiming warranty or limiting liability differently from the
   3.369 +    terms of sections 15 and 16 of this License; or
   3.370 +
   3.371 +    b) Requiring preservation of specified reasonable legal notices or
   3.372 +    author attributions in that material or in the Appropriate Legal
   3.373 +    Notices displayed by works containing it; or
   3.374 +
   3.375 +    c) Prohibiting misrepresentation of the origin of that material, or
   3.376 +    requiring that modified versions of such material be marked in
   3.377 +    reasonable ways as different from the original version; or
   3.378 +
   3.379 +    d) Limiting the use for publicity purposes of names of licensors or
   3.380 +    authors of the material; or
   3.381 +
   3.382 +    e) Declining to grant rights under trademark law for use of some
   3.383 +    trade names, trademarks, or service marks; or
   3.384 +
   3.385 +    f) Requiring indemnification of licensors and authors of that
   3.386 +    material by anyone who conveys the material (or modified versions of
   3.387 +    it) with contractual assumptions of liability to the recipient, for
   3.388 +    any liability that these contractual assumptions directly impose on
   3.389 +    those licensors and authors.
   3.390 +
   3.391 +  All other non-permissive additional terms are considered "further
   3.392 +restrictions" within the meaning of section 10.  If the Program as you
   3.393 +received it, or any part of it, contains a notice stating that it is
   3.394 +governed by this License along with a term that is a further
   3.395 +restriction, you may remove that term.  If a license document contains
   3.396 +a further restriction but permits relicensing or conveying under this
   3.397 +License, you may add to a covered work material governed by the terms
   3.398 +of that license document, provided that the further restriction does
   3.399 +not survive such relicensing or conveying.
   3.400 +
   3.401 +  If you add terms to a covered work in accord with this section, you
   3.402 +must place, in the relevant source files, a statement of the
   3.403 +additional terms that apply to those files, or a notice indicating
   3.404 +where to find the applicable terms.
   3.405 +
   3.406 +  Additional terms, permissive or non-permissive, may be stated in the
   3.407 +form of a separately written license, or stated as exceptions;
   3.408 +the above requirements apply either way.
   3.409 +
   3.410 +  8. Termination.
   3.411 +
   3.412 +  You may not propagate or modify a covered work except as expressly
   3.413 +provided under this License.  Any attempt otherwise to propagate or
   3.414 +modify it is void, and will automatically terminate your rights under
   3.415 +this License (including any patent licenses granted under the third
   3.416 +paragraph of section 11).
   3.417 +
   3.418 +  However, if you cease all violation of this License, then your
   3.419 +license from a particular copyright holder is reinstated (a)
   3.420 +provisionally, unless and until the copyright holder explicitly and
   3.421 +finally terminates your license, and (b) permanently, if the copyright
   3.422 +holder fails to notify you of the violation by some reasonable means
   3.423 +prior to 60 days after the cessation.
   3.424 +
   3.425 +  Moreover, your license from a particular copyright holder is
   3.426 +reinstated permanently if the copyright holder notifies you of the
   3.427 +violation by some reasonable means, this is the first time you have
   3.428 +received notice of violation of this License (for any work) from that
   3.429 +copyright holder, and you cure the violation prior to 30 days after
   3.430 +your receipt of the notice.
   3.431 +
   3.432 +  Termination of your rights under this section does not terminate the
   3.433 +licenses of parties who have received copies or rights from you under
   3.434 +this License.  If your rights have been terminated and not permanently
   3.435 +reinstated, you do not qualify to receive new licenses for the same
   3.436 +material under section 10.
   3.437 +
   3.438 +  9. Acceptance Not Required for Having Copies.
   3.439 +
   3.440 +  You are not required to accept this License in order to receive or
   3.441 +run a copy of the Program.  Ancillary propagation of a covered work
   3.442 +occurring solely as a consequence of using peer-to-peer transmission
   3.443 +to receive a copy likewise does not require acceptance.  However,
   3.444 +nothing other than this License grants you permission to propagate or
   3.445 +modify any covered work.  These actions infringe copyright if you do
   3.446 +not accept this License.  Therefore, by modifying or propagating a
   3.447 +covered work, you indicate your acceptance of this License to do so.
   3.448 +
   3.449 +  10. Automatic Licensing of Downstream Recipients.
   3.450 +
   3.451 +  Each time you convey a covered work, the recipient automatically
   3.452 +receives a license from the original licensors, to run, modify and
   3.453 +propagate that work, subject to this License.  You are not responsible
   3.454 +for enforcing compliance by third parties with this License.
   3.455 +
   3.456 +  An "entity transaction" is a transaction transferring control of an
   3.457 +organization, or substantially all assets of one, or subdividing an
   3.458 +organization, or merging organizations.  If propagation of a covered
   3.459 +work results from an entity transaction, each party to that
   3.460 +transaction who receives a copy of the work also receives whatever
   3.461 +licenses to the work the party's predecessor in interest had or could
   3.462 +give under the previous paragraph, plus a right to possession of the
   3.463 +Corresponding Source of the work from the predecessor in interest, if
   3.464 +the predecessor has it or can get it with reasonable efforts.
   3.465 +
   3.466 +  You may not impose any further restrictions on the exercise of the
   3.467 +rights granted or affirmed under this License.  For example, you may
   3.468 +not impose a license fee, royalty, or other charge for exercise of
   3.469 +rights granted under this License, and you may not initiate litigation
   3.470 +(including a cross-claim or counterclaim in a lawsuit) alleging that
   3.471 +any patent claim is infringed by making, using, selling, offering for
   3.472 +sale, or importing the Program or any portion of it.
   3.473 +
   3.474 +  11. Patents.
   3.475 +
   3.476 +  A "contributor" is a copyright holder who authorizes use under this
   3.477 +License of the Program or a work on which the Program is based.  The
   3.478 +work thus licensed is called the contributor's "contributor version".
   3.479 +
   3.480 +  A contributor's "essential patent claims" are all patent claims
   3.481 +owned or controlled by the contributor, whether already acquired or
   3.482 +hereafter acquired, that would be infringed by some manner, permitted
   3.483 +by this License, of making, using, or selling its contributor version,
   3.484 +but do not include claims that would be infringed only as a
   3.485 +consequence of further modification of the contributor version.  For
   3.486 +purposes of this definition, "control" includes the right to grant
   3.487 +patent sublicenses in a manner consistent with the requirements of
   3.488 +this License.
   3.489 +
   3.490 +  Each contributor grants you a non-exclusive, worldwide, royalty-free
   3.491 +patent license under the contributor's essential patent claims, to
   3.492 +make, use, sell, offer for sale, import and otherwise run, modify and
   3.493 +propagate the contents of its contributor version.
   3.494 +
   3.495 +  In the following three paragraphs, a "patent license" is any express
   3.496 +agreement or commitment, however denominated, not to enforce a patent
   3.497 +(such as an express permission to practice a patent or covenant not to
   3.498 +sue for patent infringement).  To "grant" such a patent license to a
   3.499 +party means to make such an agreement or commitment not to enforce a
   3.500 +patent against the party.
   3.501 +
   3.502 +  If you convey a covered work, knowingly relying on a patent license,
   3.503 +and the Corresponding Source of the work is not available for anyone
   3.504 +to copy, free of charge and under the terms of this License, through a
   3.505 +publicly available network server or other readily accessible means,
   3.506 +then you must either (1) cause the Corresponding Source to be so
   3.507 +available, or (2) arrange to deprive yourself of the benefit of the
   3.508 +patent license for this particular work, or (3) arrange, in a manner
   3.509 +consistent with the requirements of this License, to extend the patent
   3.510 +license to downstream recipients.  "Knowingly relying" means you have
   3.511 +actual knowledge that, but for the patent license, your conveying the
   3.512 +covered work in a country, or your recipient's use of the covered work
   3.513 +in a country, would infringe one or more identifiable patents in that
   3.514 +country that you have reason to believe are valid.
   3.515 +
   3.516 +  If, pursuant to or in connection with a single transaction or
   3.517 +arrangement, you convey, or propagate by procuring conveyance of, a
   3.518 +covered work, and grant a patent license to some of the parties
   3.519 +receiving the covered work authorizing them to use, propagate, modify
   3.520 +or convey a specific copy of the covered work, then the patent license
   3.521 +you grant is automatically extended to all recipients of the covered
   3.522 +work and works based on it.
   3.523 +
   3.524 +  A patent license is "discriminatory" if it does not include within
   3.525 +the scope of its coverage, prohibits the exercise of, or is
   3.526 +conditioned on the non-exercise of one or more of the rights that are
   3.527 +specifically granted under this License.  You may not convey a covered
   3.528 +work if you are a party to an arrangement with a third party that is
   3.529 +in the business of distributing software, under which you make payment
   3.530 +to the third party based on the extent of your activity of conveying
   3.531 +the work, and under which the third party grants, to any of the
   3.532 +parties who would receive the covered work from you, a discriminatory
   3.533 +patent license (a) in connection with copies of the covered work
   3.534 +conveyed by you (or copies made from those copies), or (b) primarily
   3.535 +for and in connection with specific products or compilations that
   3.536 +contain the covered work, unless you entered into that arrangement,
   3.537 +or that patent license was granted, prior to 28 March 2007.
   3.538 +
   3.539 +  Nothing in this License shall be construed as excluding or limiting
   3.540 +any implied license or other defenses to infringement that may
   3.541 +otherwise be available to you under applicable patent law.
   3.542 +
   3.543 +  12. No Surrender of Others' Freedom.
   3.544 +
   3.545 +  If conditions are imposed on you (whether by court order, agreement or
   3.546 +otherwise) that contradict the conditions of this License, they do not
   3.547 +excuse you from the conditions of this License.  If you cannot convey a
   3.548 +covered work so as to satisfy simultaneously your obligations under this
   3.549 +License and any other pertinent obligations, then as a consequence you may
   3.550 +not convey it at all.  For example, if you agree to terms that obligate you
   3.551 +to collect a royalty for further conveying from those to whom you convey
   3.552 +the Program, the only way you could satisfy both those terms and this
   3.553 +License would be to refrain entirely from conveying the Program.
   3.554 +
   3.555 +  13. Use with the GNU Affero General Public License.
   3.556 +
   3.557 +  Notwithstanding any other provision of this License, you have
   3.558 +permission to link or combine any covered work with a work licensed
   3.559 +under version 3 of the GNU Affero General Public License into a single
   3.560 +combined work, and to convey the resulting work.  The terms of this
   3.561 +License will continue to apply to the part which is the covered work,
   3.562 +but the special requirements of the GNU Affero General Public License,
   3.563 +section 13, concerning interaction through a network will apply to the
   3.564 +combination as such.
   3.565 +
   3.566 +  14. Revised Versions of this License.
   3.567 +
   3.568 +  The Free Software Foundation may publish revised and/or new versions of
   3.569 +the GNU General Public License from time to time.  Such new versions will
   3.570 +be similar in spirit to the present version, but may differ in detail to
   3.571 +address new problems or concerns.
   3.572 +
   3.573 +  Each version is given a distinguishing version number.  If the
   3.574 +Program specifies that a certain numbered version of the GNU General
   3.575 +Public License "or any later version" applies to it, you have the
   3.576 +option of following the terms and conditions either of that numbered
   3.577 +version or of any later version published by the Free Software
   3.578 +Foundation.  If the Program does not specify a version number of the
   3.579 +GNU General Public License, you may choose any version ever published
   3.580 +by the Free Software Foundation.
   3.581 +
   3.582 +  If the Program specifies that a proxy can decide which future
   3.583 +versions of the GNU General Public License can be used, that proxy's
   3.584 +public statement of acceptance of a version permanently authorizes you
   3.585 +to choose that version for the Program.
   3.586 +
   3.587 +  Later license versions may give you additional or different
   3.588 +permissions.  However, no additional obligations are imposed on any
   3.589 +author or copyright holder as a result of your choosing to follow a
   3.590 +later version.
   3.591 +
   3.592 +  15. Disclaimer of Warranty.
   3.593 +
   3.594 +  THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
   3.595 +APPLICABLE LAW.  EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
   3.596 +HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
   3.597 +OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
   3.598 +THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
   3.599 +PURPOSE.  THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
   3.600 +IS WITH YOU.  SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
   3.601 +ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
   3.602 +
   3.603 +  16. Limitation of Liability.
   3.604 +
   3.605 +  IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
   3.606 +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
   3.607 +THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
   3.608 +GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
   3.609 +USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
   3.610 +DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
   3.611 +PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
   3.612 +EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
   3.613 +SUCH DAMAGES.
   3.614 +
   3.615 +  17. Interpretation of Sections 15 and 16.
   3.616 +
   3.617 +  If the disclaimer of warranty and limitation of liability provided
   3.618 +above cannot be given local legal effect according to their terms,
   3.619 +reviewing courts shall apply local law that most closely approximates
   3.620 +an absolute waiver of all civil liability in connection with the
   3.621 +Program, unless a warranty or assumption of liability accompanies a
   3.622 +copy of the Program in return for a fee.
   3.623 +
   3.624 +                     END OF TERMS AND CONDITIONS
   3.625 +
   3.626 +            How to Apply These Terms to Your New Programs
   3.627 +
   3.628 +  If you develop a new program, and you want it to be of the greatest
   3.629 +possible use to the public, the best way to achieve this is to make it
   3.630 +free software which everyone can redistribute and change under these terms.
   3.631 +
   3.632 +  To do so, attach the following notices to the program.  It is safest
   3.633 +to attach them to the start of each source file to most effectively
   3.634 +state the exclusion of warranty; and each file should have at least
   3.635 +the "copyright" line and a pointer to where the full notice is found.
   3.636 +
   3.637 +    <one line to give the program's name and a brief idea of what it does.>
   3.638 +    Copyright (C) <year>  <name of author>
   3.639 +
   3.640 +    This program is free software: you can redistribute it and/or modify
   3.641 +    it under the terms of the GNU General Public License as published by
   3.642 +    the Free Software Foundation, either version 3 of the License, or
   3.643 +    (at your option) any later version.
   3.644 +
   3.645 +    This program is distributed in the hope that it will be useful,
   3.646 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
   3.647 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   3.648 +    GNU General Public License for more details.
   3.649 +
   3.650 +    You should have received a copy of the GNU General Public License
   3.651 +    along with this program.  If not, see <http://www.gnu.org/licenses/>.
   3.652 +
   3.653 +Also add information on how to contact you by electronic and paper mail.
   3.654 +
   3.655 +  If the program does terminal interaction, make it output a short
   3.656 +notice like this when it starts in an interactive mode:
   3.657 +
   3.658 +    <program>  Copyright (C) <year>  <name of author>
   3.659 +    This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
   3.660 +    This is free software, and you are welcome to redistribute it
   3.661 +    under certain conditions; type `show c' for details.
   3.662 +
   3.663 +The hypothetical commands `show w' and `show c' should show the appropriate
   3.664 +parts of the General Public License.  Of course, your program's commands
   3.665 +might be different; for a GUI interface, you would use an "about box".
   3.666 +
   3.667 +  You should also get your employer (if you work as a programmer) or school,
   3.668 +if any, to sign a "copyright disclaimer" for the program, if necessary.
   3.669 +For more information on this, and how to apply and follow the GNU GPL, see
   3.670 +<http://www.gnu.org/licenses/>.
   3.671 +
   3.672 +  The GNU General Public License does not permit incorporating your program
   3.673 +into proprietary programs.  If your program is a subroutine library, you
   3.674 +may consider it more useful to permit linking proprietary applications with
   3.675 +the library.  If this is what you want to do, use the GNU Lesser General
   3.676 +Public License instead of this License.  But first, please read
   3.677 +<http://www.gnu.org/philosophy/why-not-lgpl.html>.
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/libs/imago2/COPYING.LESSER	Sat Sep 19 05:51:51 2015 +0300
     4.3 @@ -0,0 +1,165 @@
     4.4 +                   GNU LESSER GENERAL PUBLIC LICENSE
     4.5 +                       Version 3, 29 June 2007
     4.6 +
     4.7 + Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
     4.8 + Everyone is permitted to copy and distribute verbatim copies
     4.9 + of this license document, but changing it is not allowed.
    4.10 +
    4.11 +
    4.12 +  This version of the GNU Lesser General Public License incorporates
    4.13 +the terms and conditions of version 3 of the GNU General Public
    4.14 +License, supplemented by the additional permissions listed below.
    4.15 +
    4.16 +  0. Additional Definitions.
    4.17 +
    4.18 +  As used herein, "this License" refers to version 3 of the GNU Lesser
    4.19 +General Public License, and the "GNU GPL" refers to version 3 of the GNU
    4.20 +General Public License.
    4.21 +
    4.22 +  "The Library" refers to a covered work governed by this License,
    4.23 +other than an Application or a Combined Work as defined below.
    4.24 +
    4.25 +  An "Application" is any work that makes use of an interface provided
    4.26 +by the Library, but which is not otherwise based on the Library.
    4.27 +Defining a subclass of a class defined by the Library is deemed a mode
    4.28 +of using an interface provided by the Library.
    4.29 +
    4.30 +  A "Combined Work" is a work produced by combining or linking an
    4.31 +Application with the Library.  The particular version of the Library
    4.32 +with which the Combined Work was made is also called the "Linked
    4.33 +Version".
    4.34 +
    4.35 +  The "Minimal Corresponding Source" for a Combined Work means the
    4.36 +Corresponding Source for the Combined Work, excluding any source code
    4.37 +for portions of the Combined Work that, considered in isolation, are
    4.38 +based on the Application, and not on the Linked Version.
    4.39 +
    4.40 +  The "Corresponding Application Code" for a Combined Work means the
    4.41 +object code and/or source code for the Application, including any data
    4.42 +and utility programs needed for reproducing the Combined Work from the
    4.43 +Application, but excluding the System Libraries of the Combined Work.
    4.44 +
    4.45 +  1. Exception to Section 3 of the GNU GPL.
    4.46 +
    4.47 +  You may convey a covered work under sections 3 and 4 of this License
    4.48 +without being bound by section 3 of the GNU GPL.
    4.49 +
    4.50 +  2. Conveying Modified Versions.
    4.51 +
    4.52 +  If you modify a copy of the Library, and, in your modifications, a
    4.53 +facility refers to a function or data to be supplied by an Application
    4.54 +that uses the facility (other than as an argument passed when the
    4.55 +facility is invoked), then you may convey a copy of the modified
    4.56 +version:
    4.57 +
    4.58 +   a) under this License, provided that you make a good faith effort to
    4.59 +   ensure that, in the event an Application does not supply the
    4.60 +   function or data, the facility still operates, and performs
    4.61 +   whatever part of its purpose remains meaningful, or
    4.62 +
    4.63 +   b) under the GNU GPL, with none of the additional permissions of
    4.64 +   this License applicable to that copy.
    4.65 +
    4.66 +  3. Object Code Incorporating Material from Library Header Files.
    4.67 +
    4.68 +  The object code form of an Application may incorporate material from
    4.69 +a header file that is part of the Library.  You may convey such object
    4.70 +code under terms of your choice, provided that, if the incorporated
    4.71 +material is not limited to numerical parameters, data structure
    4.72 +layouts and accessors, or small macros, inline functions and templates
    4.73 +(ten or fewer lines in length), you do both of the following:
    4.74 +
    4.75 +   a) Give prominent notice with each copy of the object code that the
    4.76 +   Library is used in it and that the Library and its use are
    4.77 +   covered by this License.
    4.78 +
    4.79 +   b) Accompany the object code with a copy of the GNU GPL and this license
    4.80 +   document.
    4.81 +
    4.82 +  4. Combined Works.
    4.83 +
    4.84 +  You may convey a Combined Work under terms of your choice that,
    4.85 +taken together, effectively do not restrict modification of the
    4.86 +portions of the Library contained in the Combined Work and reverse
    4.87 +engineering for debugging such modifications, if you also do each of
    4.88 +the following:
    4.89 +
    4.90 +   a) Give prominent notice with each copy of the Combined Work that
    4.91 +   the Library is used in it and that the Library and its use are
    4.92 +   covered by this License.
    4.93 +
    4.94 +   b) Accompany the Combined Work with a copy of the GNU GPL and this license
    4.95 +   document.
    4.96 +
    4.97 +   c) For a Combined Work that displays copyright notices during
    4.98 +   execution, include the copyright notice for the Library among
    4.99 +   these notices, as well as a reference directing the user to the
   4.100 +   copies of the GNU GPL and this license document.
   4.101 +
   4.102 +   d) Do one of the following:
   4.103 +
   4.104 +       0) Convey the Minimal Corresponding Source under the terms of this
   4.105 +       License, and the Corresponding Application Code in a form
   4.106 +       suitable for, and under terms that permit, the user to
   4.107 +       recombine or relink the Application with a modified version of
   4.108 +       the Linked Version to produce a modified Combined Work, in the
   4.109 +       manner specified by section 6 of the GNU GPL for conveying
   4.110 +       Corresponding Source.
   4.111 +
   4.112 +       1) Use a suitable shared library mechanism for linking with the
   4.113 +       Library.  A suitable mechanism is one that (a) uses at run time
   4.114 +       a copy of the Library already present on the user's computer
   4.115 +       system, and (b) will operate properly with a modified version
   4.116 +       of the Library that is interface-compatible with the Linked
   4.117 +       Version.
   4.118 +
   4.119 +   e) Provide Installation Information, but only if you would otherwise
   4.120 +   be required to provide such information under section 6 of the
   4.121 +   GNU GPL, and only to the extent that such information is
   4.122 +   necessary to install and execute a modified version of the
   4.123 +   Combined Work produced by recombining or relinking the
   4.124 +   Application with a modified version of the Linked Version. (If
   4.125 +   you use option 4d0, the Installation Information must accompany
   4.126 +   the Minimal Corresponding Source and Corresponding Application
   4.127 +   Code. If you use option 4d1, you must provide the Installation
   4.128 +   Information in the manner specified by section 6 of the GNU GPL
   4.129 +   for conveying Corresponding Source.)
   4.130 +
   4.131 +  5. Combined Libraries.
   4.132 +
   4.133 +  You may place library facilities that are a work based on the
   4.134 +Library side by side in a single library together with other library
   4.135 +facilities that are not Applications and are not covered by this
   4.136 +License, and convey such a combined library under terms of your
   4.137 +choice, if you do both of the following:
   4.138 +
   4.139 +   a) Accompany the combined library with a copy of the same work based
   4.140 +   on the Library, uncombined with any other library facilities,
   4.141 +   conveyed under the terms of this License.
   4.142 +
   4.143 +   b) Give prominent notice with the combined library that part of it
   4.144 +   is a work based on the Library, and explaining where to find the
   4.145 +   accompanying uncombined form of the same work.
   4.146 +
   4.147 +  6. Revised Versions of the GNU Lesser General Public License.
   4.148 +
   4.149 +  The Free Software Foundation may publish revised and/or new versions
   4.150 +of the GNU Lesser General Public License from time to time. Such new
   4.151 +versions will be similar in spirit to the present version, but may
   4.152 +differ in detail to address new problems or concerns.
   4.153 +
   4.154 +  Each version is given a distinguishing version number. If the
   4.155 +Library as you received it specifies that a certain numbered version
   4.156 +of the GNU Lesser General Public License "or any later version"
   4.157 +applies to it, you have the option of following the terms and
   4.158 +conditions either of that published version or of any later version
   4.159 +published by the Free Software Foundation. If the Library as you
   4.160 +received it does not specify a version number of the GNU Lesser
   4.161 +General Public License, you may choose any version of the GNU Lesser
   4.162 +General Public License ever published by the Free Software Foundation.
   4.163 +
   4.164 +  If the Library as you received it specifies that a proxy can decide
   4.165 +whether future versions of the GNU Lesser General Public License shall
   4.166 +apply, that proxy's public statement of acceptance of any version is
   4.167 +permanent authorization for you to choose that version for the
   4.168 +Library.
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/libs/imago2/conv.c	Sat Sep 19 05:51:51 2015 +0300
     5.3 @@ -0,0 +1,251 @@
     5.4 +/*
     5.5 +libimago - a multi-format image file input/output library.
     5.6 +Copyright (C) 2010 John Tsiombikas <nuclear@member.fsf.org>
     5.7 +
     5.8 +This program is free software: you can redistribute it and/or modify
     5.9 +it under the terms of the GNU Lesser General Public License as published
    5.10 +by the Free Software Foundation, either version 3 of the License, or
    5.11 +(at your option) any later version.
    5.12 +
    5.13 +This program is distributed in the hope that it will be useful,
    5.14 +but WITHOUT ANY WARRANTY; without even the implied warranty of
    5.15 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    5.16 +GNU Lesser General Public License for more details.
    5.17 +
    5.18 +You should have received a copy of the GNU Lesser General Public License
    5.19 +along with this program.  If not, see <http://www.gnu.org/licenses/>.
    5.20 +*/
    5.21 +#include <string.h>
    5.22 +#include "imago2.h"
    5.23 +
    5.24 +/* pixel-format conversions are sub-optimal at the moment to avoid
    5.25 + * writing a lot of code. optimize at some point ?
    5.26 + */
    5.27 +
    5.28 +#define CLAMP(x, a, b)	((x) < (a) ? (a) : ((x) > (b) ? (b) : (x)))
    5.29 +
    5.30 +struct pixel {
    5.31 +	float r, g, b, a;
    5.32 +};
    5.33 +
    5.34 +static void unpack_grey8(struct pixel *unp, void *pptr, int count);
    5.35 +static void unpack_rgb24(struct pixel *unp, void *pptr, int count);
    5.36 +static void unpack_rgba32(struct pixel *unp, void *pptr, int count);
    5.37 +static void unpack_greyf(struct pixel *unp, void *pptr, int count);
    5.38 +static void unpack_rgbf(struct pixel *unp, void *pptr, int count);
    5.39 +static void unpack_rgbaf(struct pixel *unp, void *pptr, int count);
    5.40 +
    5.41 +static void pack_grey8(void *pptr, struct pixel *unp, int count);
    5.42 +static void pack_rgb24(void *pptr, struct pixel *unp, int count);
    5.43 +static void pack_rgba32(void *pptr, struct pixel *unp, int count);
    5.44 +static void pack_greyf(void *pptr, struct pixel *unp, int count);
    5.45 +static void pack_rgbf(void *pptr, struct pixel *unp, int count);
    5.46 +static void pack_rgbaf(void *pptr, struct pixel *unp, int count);
    5.47 +
    5.48 +/* XXX keep in sync with enum img_fmt at imago2.h */
    5.49 +static void (*unpack[])(struct pixel*, void*, int) = {
    5.50 +	unpack_grey8,
    5.51 +	unpack_rgb24,
    5.52 +	unpack_rgba32,
    5.53 +	unpack_greyf,
    5.54 +	unpack_rgbf,
    5.55 +	unpack_rgbaf
    5.56 +};
    5.57 +
    5.58 +/* XXX keep in sync with enum img_fmt at imago2.h */
    5.59 +static void (*pack[])(void*, struct pixel*, int) = {
    5.60 +	pack_grey8,
    5.61 +	pack_rgb24,
    5.62 +	pack_rgba32,
    5.63 +	pack_greyf,
    5.64 +	pack_rgbf,
    5.65 +	pack_rgbaf
    5.66 +};
    5.67 +
    5.68 +
    5.69 +int img_convert(struct img_pixmap *img, enum img_fmt tofmt)
    5.70 +{
    5.71 +	struct pixel pbuf[8];
    5.72 +	int bufsz = (img->width & 7) == 0 ? 8 : ((img->width & 3) == 0 ? 4 : 1);
    5.73 +	int i, num_pix = img->width * img->height;
    5.74 +	int num_iter = num_pix / bufsz;
    5.75 +	char *sptr, *dptr;
    5.76 +	struct img_pixmap nimg;
    5.77 +
    5.78 +	if(img->fmt == tofmt) {
    5.79 +		return 0;	/* nothing to do */
    5.80 +	}
    5.81 +
    5.82 +	img_init(&nimg);
    5.83 +	if(img_set_pixels(&nimg, img->width, img->height, tofmt, 0) == -1) {
    5.84 +		img_destroy(&nimg);
    5.85 +		return -1;
    5.86 +	}
    5.87 +
    5.88 +	sptr = img->pixels;
    5.89 +	dptr = nimg.pixels;
    5.90 +
    5.91 +	for(i=0; i<num_iter; i++) {
    5.92 +		unpack[img->fmt](pbuf, sptr, bufsz);
    5.93 +		pack[tofmt](dptr, pbuf, bufsz);
    5.94 +
    5.95 +		sptr += bufsz * img->pixelsz;
    5.96 +		dptr += bufsz * nimg.pixelsz;
    5.97 +	}
    5.98 +
    5.99 +	img_copy(img, &nimg);
   5.100 +	img_destroy(&nimg);
   5.101 +	return 0;
   5.102 +}
   5.103 +
   5.104 +/* the following functions *could* benefit from SIMD */
   5.105 +
   5.106 +static void unpack_grey8(struct pixel *unp, void *pptr, int count)
   5.107 +{
   5.108 +	int i;
   5.109 +	unsigned char *pix = pptr;
   5.110 +
   5.111 +	for(i=0; i<count; i++) {
   5.112 +		unp->r = unp->g = unp->b = (float)*pix++ / 255.0;
   5.113 +		unp->a = 1.0;
   5.114 +		unp++;
   5.115 +	}
   5.116 +}
   5.117 +
   5.118 +static void unpack_rgb24(struct pixel *unp, void *pptr, int count)
   5.119 +{
   5.120 +	int i;
   5.121 +	unsigned char *pix = pptr;
   5.122 +
   5.123 +	for(i=0; i<count; i++) {
   5.124 +		unp->r = (float)*pix++ / 255.0;
   5.125 +		unp->g = (float)*pix++ / 255.0;
   5.126 +		unp->b = (float)*pix++ / 255.0;
   5.127 +		unp->a = 1.0;
   5.128 +		unp++;
   5.129 +	}
   5.130 +}
   5.131 +
   5.132 +static void unpack_rgba32(struct pixel *unp, void *pptr, int count)
   5.133 +{
   5.134 +	memcpy(unp, pptr, count * sizeof *unp);
   5.135 +}
   5.136 +
   5.137 +static void unpack_greyf(struct pixel *unp, void *pptr, int count)
   5.138 +{
   5.139 +	int i;
   5.140 +	float *pix = pptr;
   5.141 +
   5.142 +	for(i=0; i<count; i++) {
   5.143 +		unp->r = unp->g = unp->b = *pix++;
   5.144 +		unp->a = 1.0;
   5.145 +		unp++;
   5.146 +	}
   5.147 +}
   5.148 +
   5.149 +static void unpack_rgbf(struct pixel *unp, void *pptr, int count)
   5.150 +{
   5.151 +	int i;
   5.152 +	float *pix = pptr;
   5.153 +
   5.154 +	for(i=0; i<count; i++) {
   5.155 +		unp->r = *pix++;
   5.156 +		unp->g = *pix++;
   5.157 +		unp->b = *pix++;
   5.158 +		unp->a = 1.0;
   5.159 +		unp++;
   5.160 +	}
   5.161 +}
   5.162 +
   5.163 +static void unpack_rgbaf(struct pixel *unp, void *pptr, int count)
   5.164 +{
   5.165 +	int i;
   5.166 +	float *pix = pptr;
   5.167 +
   5.168 +	for(i=0; i<count; i++) {
   5.169 +		unp->r = *pix++;
   5.170 +		unp->g = *pix++;
   5.171 +		unp->b = *pix++;
   5.172 +		unp->a = *pix++;
   5.173 +		unp++;
   5.174 +	}
   5.175 +}
   5.176 +
   5.177 +
   5.178 +static void pack_grey8(void *pptr, struct pixel *unp, int count)
   5.179 +{
   5.180 +	int i;
   5.181 +	unsigned char *pix = pptr;
   5.182 +
   5.183 +	for(i=0; i<count; i++) {
   5.184 +		int lum = (int)(255.0 * (unp->r + unp->g + unp->b) / 3.0);
   5.185 +		*pix++ = CLAMP(lum, 0, 255);
   5.186 +		unp++;
   5.187 +	}
   5.188 +}
   5.189 +
   5.190 +static void pack_rgb24(void *pptr, struct pixel *unp, int count)
   5.191 +{
   5.192 +	int i;
   5.193 +	unsigned char *pix = pptr;
   5.194 +
   5.195 +	for(i=0; i<count; i++) {
   5.196 +		int r = (int)(unp->r * 255.0);
   5.197 +		int g = (int)(unp->g * 255.0);
   5.198 +		int b = (int)(unp->b * 255.0);
   5.199 +
   5.200 +		*pix++ = CLAMP(r, 0, 255);
   5.201 +		*pix++ = CLAMP(g, 0, 255);
   5.202 +		*pix++ = CLAMP(b, 0, 255);
   5.203 +		unp++;
   5.204 +	}
   5.205 +}
   5.206 +
   5.207 +static void pack_rgba32(void *pptr, struct pixel *unp, int count)
   5.208 +{
   5.209 +	int i;
   5.210 +	unsigned char *pix = pptr;
   5.211 +
   5.212 +	for(i=0; i<count; i++) {
   5.213 +		int r = (int)(unp->r * 255.0);
   5.214 +		int g = (int)(unp->g * 255.0);
   5.215 +		int b = (int)(unp->b * 255.0);
   5.216 +		int a = (int)(unp->a * 255.0);
   5.217 +
   5.218 +		*pix++ = CLAMP(r, 0, 255);
   5.219 +		*pix++ = CLAMP(g, 0, 255);
   5.220 +		*pix++ = CLAMP(b, 0, 255);
   5.221 +		*pix++ = CLAMP(a, 0, 255);
   5.222 +		unp++;
   5.223 +	}
   5.224 +}
   5.225 +
   5.226 +static void pack_greyf(void *pptr, struct pixel *unp, int count)
   5.227 +{
   5.228 +	int i;
   5.229 +	float *pix = pptr;
   5.230 +
   5.231 +	for(i=0; i<count; i++) {
   5.232 +		*pix++ = (unp->r + unp->g + unp->b) / 3.0;
   5.233 +		unp++;
   5.234 +	}
   5.235 +}
   5.236 +
   5.237 +static void pack_rgbf(void *pptr, struct pixel *unp, int count)
   5.238 +{
   5.239 +	int i;
   5.240 +	float *pix = pptr;
   5.241 +
   5.242 +	for(i=0; i<count; i++) {
   5.243 +		*pix++ = unp->r;
   5.244 +		*pix++ = unp->g;
   5.245 +		*pix++ = unp->b;
   5.246 +		unp++;
   5.247 +	}
   5.248 +}
   5.249 +
   5.250 +static void pack_rgbaf(void *pptr, struct pixel *unp, int count)
   5.251 +{
   5.252 +	memcpy(pptr, unp, count * sizeof *unp);
   5.253 +}
   5.254 +
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/libs/imago2/file_jpeg.c	Sat Sep 19 05:51:51 2015 +0300
     6.3 @@ -0,0 +1,291 @@
     6.4 +/*
     6.5 +libimago - a multi-format image file input/output library.
     6.6 +Copyright (C) 2010 John Tsiombikas <nuclear@member.fsf.org>
     6.7 +
     6.8 +This program is free software: you can redistribute it and/or modify
     6.9 +it under the terms of the GNU Lesser General Public License as published
    6.10 +by the Free Software Foundation, either version 3 of the License, or
    6.11 +(at your option) any later version.
    6.12 +
    6.13 +This program is distributed in the hope that it will be useful,
    6.14 +but WITHOUT ANY WARRANTY; without even the implied warranty of
    6.15 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    6.16 +GNU Lesser General Public License for more details.
    6.17 +
    6.18 +You should have received a copy of the GNU Lesser General Public License
    6.19 +along with this program.  If not, see <http://www.gnu.org/licenses/>.
    6.20 +*/
    6.21 +
    6.22 +/* -- JPEG module -- */
    6.23 +
    6.24 +#include <stdio.h>
    6.25 +#include <stdlib.h>
    6.26 +#include <string.h>
    6.27 +
    6.28 +#ifdef WIN32
    6.29 +#include <windows.h>
    6.30 +#define HAVE_BOOLEAN
    6.31 +#endif
    6.32 +
    6.33 +#include <jpeglib.h>
    6.34 +#include "imago2.h"
    6.35 +#include "ftype_module.h"
    6.36 +
    6.37 +#define INPUT_BUF_SIZE	512
    6.38 +#define OUTPUT_BUF_SIZE	512
    6.39 +
    6.40 +/* data source manager: adapted from jdatasrc.c */
    6.41 +struct src_mgr {
    6.42 +	struct jpeg_source_mgr pub;
    6.43 +
    6.44 +	struct img_io *io;
    6.45 +	unsigned char buffer[INPUT_BUF_SIZE];
    6.46 +	int start_of_file;
    6.47 +};
    6.48 +
    6.49 +/* data destination manager: adapted from jdatadst.c */
    6.50 +struct dst_mgr {
    6.51 +	struct jpeg_destination_mgr pub;
    6.52 +
    6.53 +	struct img_io *io;
    6.54 +	unsigned char buffer[OUTPUT_BUF_SIZE];
    6.55 +};
    6.56 +
    6.57 +static int check(struct img_io *io);
    6.58 +static int readjpeg(struct img_pixmap *img, struct img_io *io);
    6.59 +static int writejpeg(struct img_pixmap *img, struct img_io *io);
    6.60 +
    6.61 +/* read source functions */
    6.62 +static void init_source(j_decompress_ptr jd);
    6.63 +static int fill_input_buffer(j_decompress_ptr jd);
    6.64 +static void skip_input_data(j_decompress_ptr jd, long num_bytes);
    6.65 +static void term_source(j_decompress_ptr jd);
    6.66 +
    6.67 +/* write destination functions */
    6.68 +static void init_destination(j_compress_ptr jc);
    6.69 +static int empty_output_buffer(j_compress_ptr jc);
    6.70 +static void term_destination(j_compress_ptr jc);
    6.71 +
    6.72 +int img_register_jpeg(void)
    6.73 +{
    6.74 +	static struct ftype_module mod = {".jpg", check, readjpeg, writejpeg};
    6.75 +	return img_register_module(&mod);
    6.76 +}
    6.77 +
    6.78 +
    6.79 +static int check(struct img_io *io)
    6.80 +{
    6.81 +	unsigned char sig[10];
    6.82 +
    6.83 +	long pos = io->seek(0, SEEK_CUR, io->uptr);
    6.84 +
    6.85 +	if(io->read(sig, 10, io->uptr) < 10) {
    6.86 +		io->seek(pos, SEEK_SET, io->uptr);
    6.87 +		return -1;
    6.88 +	}
    6.89 +
    6.90 +	if(memcmp(sig, "\xff\xd8\xff\xe0", 4) != 0 || memcmp(sig + 6, "JFIF", 4) != 0) {
    6.91 +		io->seek(pos, SEEK_SET, io->uptr);
    6.92 +		return -1;
    6.93 +	}
    6.94 +	io->seek(pos, SEEK_SET, io->uptr);
    6.95 +	return 0;
    6.96 +}
    6.97 +
    6.98 +static int readjpeg(struct img_pixmap *img, struct img_io *io)
    6.99 +{
   6.100 +	int i, nlines = 0;
   6.101 +	struct jpeg_decompress_struct cinfo;
   6.102 +	struct jpeg_error_mgr jerr;
   6.103 +	struct src_mgr src;
   6.104 +	unsigned char **scanlines;
   6.105 +
   6.106 +	cinfo.err = jpeg_std_error(&jerr);	/* XXX change... */
   6.107 +	jpeg_create_decompress(&cinfo);
   6.108 +
   6.109 +	src.pub.init_source = init_source;
   6.110 +	src.pub.fill_input_buffer = fill_input_buffer;
   6.111 +	src.pub.skip_input_data = skip_input_data;
   6.112 +	src.pub.resync_to_restart = jpeg_resync_to_restart;
   6.113 +	src.pub.term_source = term_source;
   6.114 +	src.pub.next_input_byte = 0;
   6.115 +	src.pub.bytes_in_buffer = 0;
   6.116 +	src.io = io;
   6.117 +	cinfo.src = (struct jpeg_source_mgr*)&src;
   6.118 +
   6.119 +	jpeg_read_header(&cinfo, 1);
   6.120 +	cinfo.out_color_space = JCS_RGB;
   6.121 +
   6.122 +	if(img_set_pixels(img, cinfo.image_width, cinfo.image_height, IMG_FMT_RGB24, 0) == -1) {
   6.123 +		jpeg_destroy_decompress(&cinfo);
   6.124 +		return -1;
   6.125 +	}
   6.126 +
   6.127 +	if(!(scanlines = malloc(img->height * sizeof *scanlines))) {
   6.128 +		jpeg_destroy_decompress(&cinfo);
   6.129 +		return -1;
   6.130 +	}
   6.131 +	scanlines[0] = img->pixels;
   6.132 +	for(i=1; i<img->height; i++) {
   6.133 +		scanlines[i] = scanlines[i - 1] + img->width * img->pixelsz;
   6.134 +	}
   6.135 +
   6.136 +	jpeg_start_decompress(&cinfo);
   6.137 +	while(nlines < img->height) {
   6.138 +		int res = jpeg_read_scanlines(&cinfo, scanlines + nlines, img->height - nlines);
   6.139 +		nlines += res;
   6.140 +	}
   6.141 +	jpeg_finish_decompress(&cinfo);
   6.142 +	jpeg_destroy_decompress(&cinfo);
   6.143 +
   6.144 +	free(scanlines);
   6.145 +	return 0;
   6.146 +}
   6.147 +
   6.148 +static int writejpeg(struct img_pixmap *img, struct img_io *io)
   6.149 +{
   6.150 +	int i, nlines = 0;
   6.151 +	struct jpeg_compress_struct cinfo;
   6.152 +	struct jpeg_error_mgr jerr;
   6.153 +	struct dst_mgr dest;
   6.154 +	struct img_pixmap tmpimg;
   6.155 +	unsigned char **scanlines;
   6.156 +
   6.157 +	img_init(&tmpimg);
   6.158 +
   6.159 +	if(img->fmt != IMG_FMT_RGB24) {
   6.160 +		if(img_copy(&tmpimg, img) == -1) {
   6.161 +			return -1;
   6.162 +		}
   6.163 +		if(img_convert(&tmpimg, IMG_FMT_RGB24) == -1) {
   6.164 +			img_destroy(&tmpimg);
   6.165 +			return -1;
   6.166 +		}
   6.167 +		img = &tmpimg;
   6.168 +	}
   6.169 +
   6.170 +	if(!(scanlines = malloc(img->height * sizeof *scanlines))) {
   6.171 +		img_destroy(&tmpimg);
   6.172 +		return -1;
   6.173 +	}
   6.174 +	scanlines[0] = img->pixels;
   6.175 +	for(i=1; i<img->height; i++) {
   6.176 +		scanlines[i] = scanlines[i - 1] + img->width * img->pixelsz;
   6.177 +	}
   6.178 +
   6.179 +	cinfo.err = jpeg_std_error(&jerr);	/* XXX */
   6.180 +	jpeg_create_compress(&cinfo);
   6.181 +
   6.182 +	dest.pub.init_destination = init_destination;
   6.183 +	dest.pub.empty_output_buffer = empty_output_buffer;
   6.184 +	dest.pub.term_destination = term_destination;
   6.185 +	dest.io = io;
   6.186 +	cinfo.dest = (struct jpeg_destination_mgr*)&dest;
   6.187 +
   6.188 +	cinfo.image_width = img->width;
   6.189 +	cinfo.image_height = img->height;
   6.190 +	cinfo.input_components = 3;
   6.191 +	cinfo.in_color_space = JCS_RGB;
   6.192 +
   6.193 +	jpeg_set_defaults(&cinfo);
   6.194 +
   6.195 +	jpeg_start_compress(&cinfo, 1);
   6.196 +	while(nlines < img->height) {
   6.197 +		int res = jpeg_write_scanlines(&cinfo, scanlines + nlines, img->height - nlines);
   6.198 +		nlines += res;
   6.199 +	}
   6.200 +	jpeg_finish_compress(&cinfo);
   6.201 +	jpeg_destroy_compress(&cinfo);
   6.202 +
   6.203 +	free(scanlines);
   6.204 +	img_destroy(&tmpimg);
   6.205 +	return 0;
   6.206 +}
   6.207 +
   6.208 +/* -- read source functions --
   6.209 + * the following functions are adapted from jdatasrc.c in jpeglib
   6.210 + */
   6.211 +static void init_source(j_decompress_ptr jd)
   6.212 +{
   6.213 +	struct src_mgr *src = (struct src_mgr*)jd->src;
   6.214 +	src->start_of_file = 1;
   6.215 +}
   6.216 +
   6.217 +static int fill_input_buffer(j_decompress_ptr jd)
   6.218 +{
   6.219 +	struct src_mgr *src = (struct src_mgr*)jd->src;
   6.220 +	size_t nbytes;
   6.221 +
   6.222 +	nbytes = src->io->read(src->buffer, INPUT_BUF_SIZE, src->io->uptr);
   6.223 +
   6.224 +	if(nbytes <= 0) {
   6.225 +		if(src->start_of_file) {
   6.226 +			return 0;
   6.227 +		}
   6.228 +		/* insert a fake EOI marker */
   6.229 +		src->buffer[0] = 0xff;
   6.230 +		src->buffer[1] = JPEG_EOI;
   6.231 +		nbytes = 2;
   6.232 +	}
   6.233 +
   6.234 +	src->pub.next_input_byte = src->buffer;
   6.235 +	src->pub.bytes_in_buffer = nbytes;
   6.236 +	src->start_of_file = 0;
   6.237 +	return 1;
   6.238 +}
   6.239 +
   6.240 +static void skip_input_data(j_decompress_ptr jd, long num_bytes)
   6.241 +{
   6.242 +	struct src_mgr *src = (struct src_mgr*)jd->src;
   6.243 +
   6.244 +	if(num_bytes > 0) {
   6.245 +		while(num_bytes > (long)src->pub.bytes_in_buffer) {
   6.246 +			num_bytes -= (long)src->pub.bytes_in_buffer;
   6.247 +			fill_input_buffer(jd);
   6.248 +		}
   6.249 +		src->pub.next_input_byte += (size_t)num_bytes;
   6.250 +		src->pub.bytes_in_buffer -= (size_t)num_bytes;
   6.251 +	}
   6.252 +}
   6.253 +
   6.254 +static void term_source(j_decompress_ptr jd)
   6.255 +{
   6.256 +	/* nothing to see here, move along */
   6.257 +}
   6.258 +
   6.259 +
   6.260 +/* -- write destination functions --
   6.261 + * the following functions are adapted from jdatadst.c in jpeglib
   6.262 + */
   6.263 +static void init_destination(j_compress_ptr jc)
   6.264 +{
   6.265 +	struct dst_mgr *dest = (struct dst_mgr*)jc->dest;
   6.266 +
   6.267 +	dest->pub.next_output_byte = dest->buffer;
   6.268 +	dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
   6.269 +}
   6.270 +
   6.271 +static int empty_output_buffer(j_compress_ptr jc)
   6.272 +{
   6.273 +	struct dst_mgr *dest = (struct dst_mgr*)jc->dest;
   6.274 +
   6.275 +	if(dest->io->write(dest->buffer, OUTPUT_BUF_SIZE, dest->io->uptr) != OUTPUT_BUF_SIZE) {
   6.276 +		return 0;
   6.277 +	}
   6.278 +
   6.279 +	dest->pub.next_output_byte = dest->buffer;
   6.280 +	dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
   6.281 +	return 1;
   6.282 +}
   6.283 +
   6.284 +static void term_destination(j_compress_ptr jc)
   6.285 +{
   6.286 +	struct dst_mgr *dest = (struct dst_mgr*)jc->dest;
   6.287 +	size_t datacount = OUTPUT_BUF_SIZE - dest->pub.free_in_buffer;
   6.288 +
   6.289 +	/* write any remaining data in the buffer */
   6.290 +	if(datacount > 0) {
   6.291 +		dest->io->write(dest->buffer, datacount, dest->io->uptr);
   6.292 +	}
   6.293 +	/* XXX flush? ... */
   6.294 +}
     7.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.2 +++ b/libs/imago2/file_png.c	Sat Sep 19 05:51:51 2015 +0300
     7.3 @@ -0,0 +1,244 @@
     7.4 +/*
     7.5 +libimago - a multi-format image file input/output library.
     7.6 +Copyright (C) 2010 John Tsiombikas <nuclear@member.fsf.org>
     7.7 +
     7.8 +This program is free software: you can redistribute it and/or modify
     7.9 +it under the terms of the GNU Lesser General Public License as published
    7.10 +by the Free Software Foundation, either version 3 of the License, or
    7.11 +(at your option) any later version.
    7.12 +
    7.13 +This program is distributed in the hope that it will be useful,
    7.14 +but WITHOUT ANY WARRANTY; without even the implied warranty of
    7.15 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    7.16 +GNU Lesser General Public License for more details.
    7.17 +
    7.18 +You should have received a copy of the GNU Lesser General Public License
    7.19 +along with this program.  If not, see <http://www.gnu.org/licenses/>.
    7.20 +*/
    7.21 +
    7.22 +/* -- PNG module -- */
    7.23 +
    7.24 +#include <stdlib.h>
    7.25 +#include <png.h>
    7.26 +#include "imago2.h"
    7.27 +#include "ftype_module.h"
    7.28 +
    7.29 +static int check_file(struct img_io *io);
    7.30 +static int read_file(struct img_pixmap *img, struct img_io *io);
    7.31 +static int write_file(struct img_pixmap *img, struct img_io *io);
    7.32 +
    7.33 +static void read_func(png_struct *png, unsigned char *data, size_t len);
    7.34 +static void write_func(png_struct *png, unsigned char *data, size_t len);
    7.35 +static void flush_func(png_struct *png);
    7.36 +
    7.37 +static int png_type_to_fmt(int color_type, int channel_bits);
    7.38 +static int fmt_to_png_type(enum img_fmt fmt);
    7.39 +
    7.40 +
    7.41 +int img_register_png(void)
    7.42 +{
    7.43 +	static struct ftype_module mod = {".png", check_file, read_file, write_file};
    7.44 +	return img_register_module(&mod);
    7.45 +}
    7.46 +
    7.47 +static int check_file(struct img_io *io)
    7.48 +{
    7.49 +	unsigned char sig[8];
    7.50 +	int res;
    7.51 +	long pos = io->seek(0, SEEK_CUR, io->uptr);
    7.52 +
    7.53 +	if(io->read(sig, 8, io->uptr) < 8) {
    7.54 +		io->seek(pos, SEEK_SET, io->uptr);
    7.55 +		return -1;
    7.56 +	}
    7.57 +
    7.58 +	res = png_sig_cmp(sig, 0, 8) == 0 ? 0 : -1;
    7.59 +	io->seek(pos, SEEK_SET, io->uptr);
    7.60 +	return res;
    7.61 +}
    7.62 +
    7.63 +static int read_file(struct img_pixmap *img, struct img_io *io)
    7.64 +{
    7.65 +	png_struct *png;
    7.66 +	png_info *info;
    7.67 +	unsigned char **lineptr, *dest;
    7.68 +	int i, channel_bits, color_type, ilace_type, compression, filtering, fmt;
    7.69 +	unsigned long xsz, ysz;
    7.70 +
    7.71 +	if(!(png = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0))) {
    7.72 +		return -1;
    7.73 +	}
    7.74 +
    7.75 +	if(!(info = png_create_info_struct(png))) {
    7.76 +		png_destroy_read_struct(&png, 0, 0);
    7.77 +		return -1;
    7.78 +	}
    7.79 +
    7.80 +	if(setjmp(png_jmpbuf(png))) {
    7.81 +		png_destroy_read_struct(&png, &info, 0);
    7.82 +		return -1;
    7.83 +	}
    7.84 +
    7.85 +	png_set_read_fn(png, io, read_func);
    7.86 +	png_set_sig_bytes(png, 0);
    7.87 +	png_read_png(png, info, 0, 0);
    7.88 +
    7.89 +	png_get_IHDR(png, info, &xsz, &ysz, &channel_bits, &color_type, &ilace_type,
    7.90 +			&compression, &filtering);
    7.91 +	if((fmt = png_type_to_fmt(color_type, channel_bits)) == -1) {
    7.92 +		png_destroy_read_struct(&png, &info, 0);
    7.93 +		return -1;
    7.94 +	}
    7.95 +
    7.96 +	if(img_set_pixels(img, xsz, ysz, fmt, 0) == -1) {
    7.97 +		png_destroy_read_struct(&png, &info, 0);
    7.98 +		return -1;
    7.99 +	}
   7.100 +
   7.101 +	lineptr = (unsigned char**)png_get_rows(png, info);
   7.102 +
   7.103 +	dest = img->pixels;
   7.104 +	for(i=0; i<ysz; i++) {
   7.105 +		memcpy(dest, lineptr[i], xsz * img->pixelsz);
   7.106 +		dest += xsz * img->pixelsz;
   7.107 +	}
   7.108 +	png_destroy_read_struct(&png, &info, 0);
   7.109 +	return 0;
   7.110 +}
   7.111 +
   7.112 +
   7.113 +static int write_file(struct img_pixmap *img, struct img_io *io)
   7.114 +{
   7.115 +	png_struct *png;
   7.116 +	png_info *info;
   7.117 +	png_text txt;
   7.118 +	struct img_pixmap tmpimg;
   7.119 +	unsigned char **rows;
   7.120 +	unsigned char *pixptr;
   7.121 +	int i, coltype;
   7.122 +
   7.123 +	img_init(&tmpimg);
   7.124 +
   7.125 +	if(!(png = png_create_write_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0))) {
   7.126 +		return -1;
   7.127 +	}
   7.128 +	if(!(info = png_create_info_struct(png))) {
   7.129 +		png_destroy_write_struct(&png, 0);
   7.130 +		return -1;
   7.131 +	}
   7.132 +
   7.133 +	/* if the input image is floating-point, we need to convert it to integer */
   7.134 +	if(img_is_float(img)) {
   7.135 +		if(img_copy(&tmpimg, img) == -1) {
   7.136 +			return -1;
   7.137 +		}
   7.138 +		if(img_to_integer(&tmpimg) == -1) {
   7.139 +			img_destroy(&tmpimg);
   7.140 +			return -1;
   7.141 +		}
   7.142 +		img = &tmpimg;
   7.143 +	}
   7.144 +
   7.145 +	txt.compression = PNG_TEXT_COMPRESSION_NONE;
   7.146 +	txt.key = "Software";
   7.147 +	txt.text = "libimago2";
   7.148 +	txt.text_length = 0;
   7.149 +
   7.150 +	if(setjmp(png_jmpbuf(png))) {
   7.151 +		png_destroy_write_struct(&png, &info);
   7.152 +		img_destroy(&tmpimg);
   7.153 +		return -1;
   7.154 +	}
   7.155 +	png_set_write_fn(png, io, write_func, flush_func);
   7.156 +
   7.157 +	coltype = fmt_to_png_type(img->fmt);
   7.158 +	png_set_IHDR(png, info, img->width, img->height, 8, coltype, PNG_INTERLACE_NONE,
   7.159 +			PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
   7.160 +	png_set_text(png, info, &txt, 1);
   7.161 +
   7.162 +	if(!(rows = malloc(img->height * sizeof *rows))) {
   7.163 +		png_destroy_write_struct(&png, &info);
   7.164 +		img_destroy(&tmpimg);
   7.165 +		return -1;
   7.166 +	}
   7.167 +
   7.168 +	pixptr = img->pixels;
   7.169 +	for(i=0; i<img->height; i++) {
   7.170 +		rows[i] = pixptr;
   7.171 +		pixptr += img->width * img->pixelsz;
   7.172 +	}
   7.173 +	png_set_rows(png, info, rows);
   7.174 +
   7.175 +	png_write_png(png, info, 0, 0);
   7.176 +	png_write_end(png, info);
   7.177 +	png_destroy_write_struct(&png, &info);
   7.178 +
   7.179 +	free(rows);
   7.180 +
   7.181 +	img_destroy(&tmpimg);
   7.182 +	return 0;
   7.183 +}
   7.184 +
   7.185 +static void read_func(png_struct *png, unsigned char *data, size_t len)
   7.186 +{
   7.187 +	struct img_io *io = (struct img_io*)png_get_io_ptr(png);
   7.188 +
   7.189 +	if(io->read(data, len, io->uptr) == -1) {
   7.190 +		longjmp(png_jmpbuf(png), 1);
   7.191 +	}
   7.192 +}
   7.193 +
   7.194 +static void write_func(png_struct *png, unsigned char *data, size_t len)
   7.195 +{
   7.196 +	struct img_io *io = (struct img_io*)png_get_io_ptr(png);
   7.197 +
   7.198 +	if(io->write(data, len, io->uptr) == -1) {
   7.199 +		longjmp(png_jmpbuf(png), 1);
   7.200 +	}
   7.201 +}
   7.202 +
   7.203 +static void flush_func(png_struct *png)
   7.204 +{
   7.205 +	/* XXX does it matter that we can't flush? */
   7.206 +}
   7.207 +
   7.208 +static int png_type_to_fmt(int color_type, int channel_bits)
   7.209 +{
   7.210 +	/* we don't support non-8bit-per-channel images yet */
   7.211 +	if(channel_bits > 8) {
   7.212 +		return -1;
   7.213 +	}
   7.214 +
   7.215 +	switch(color_type) {
   7.216 +	case PNG_COLOR_TYPE_RGB:
   7.217 +		return IMG_FMT_RGB24;
   7.218 +
   7.219 +	case PNG_COLOR_TYPE_RGB_ALPHA:
   7.220 +		return IMG_FMT_RGBA32;
   7.221 +
   7.222 +	case PNG_COLOR_TYPE_GRAY:
   7.223 +		return IMG_FMT_GREY8;
   7.224 +
   7.225 +	default:
   7.226 +		break;
   7.227 +	}
   7.228 +	return -1;
   7.229 +}
   7.230 +
   7.231 +static int fmt_to_png_type(enum img_fmt fmt)
   7.232 +{
   7.233 +	switch(fmt) {
   7.234 +	case IMG_FMT_GREY8:
   7.235 +		return PNG_COLOR_TYPE_GRAY;
   7.236 +
   7.237 +	case IMG_FMT_RGB24:
   7.238 +		return PNG_COLOR_TYPE_RGB;
   7.239 +
   7.240 +	case IMG_FMT_RGBA32:
   7.241 +		return PNG_COLOR_TYPE_RGBA;
   7.242 +
   7.243 +	default:
   7.244 +		break;
   7.245 +	}
   7.246 +	return -1;
   7.247 +}
     8.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     8.2 +++ b/libs/imago2/file_ppm.c	Sat Sep 19 05:51:51 2015 +0300
     8.3 @@ -0,0 +1,153 @@
     8.4 +/*
     8.5 +libimago - a multi-format image file input/output library.
     8.6 +Copyright (C) 2010 John Tsiombikas <nuclear@member.fsf.org>
     8.7 +
     8.8 +This program is free software: you can redistribute it and/or modify
     8.9 +it under the terms of the GNU Lesser General Public License as published
    8.10 +by the Free Software Foundation, either version 3 of the License, or
    8.11 +(at your option) any later version.
    8.12 +
    8.13 +This program is distributed in the hope that it will be useful,
    8.14 +but WITHOUT ANY WARRANTY; without even the implied warranty of
    8.15 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    8.16 +GNU Lesser General Public License for more details.
    8.17 +
    8.18 +You should have received a copy of the GNU Lesser General Public License
    8.19 +along with this program.  If not, see <http://www.gnu.org/licenses/>.
    8.20 +*/
    8.21 +
    8.22 +/* -- Portable Pixmap (PPM) module -- */
    8.23 +
    8.24 +#include <string.h>
    8.25 +#include "imago2.h"
    8.26 +#include "ftype_module.h"
    8.27 +
    8.28 +static int check(struct img_io *io);
    8.29 +static int readppm(struct img_pixmap *img, struct img_io *io);
    8.30 +static int writeppm(struct img_pixmap *img, struct img_io *io);
    8.31 +
    8.32 +int img_register_ppm(void)
    8.33 +{
    8.34 +	static struct ftype_module mod = {".ppm", check, readppm, writeppm};
    8.35 +	return img_register_module(&mod);
    8.36 +}
    8.37 +
    8.38 +
    8.39 +static int check(struct img_io *io)
    8.40 +{
    8.41 +	char id[2];
    8.42 +	int res = -1;
    8.43 +	long pos = io->seek(0, SEEK_CUR, io->uptr);
    8.44 +
    8.45 +	if(io->read(id, 2, io->uptr) < 2) {
    8.46 +		io->seek(pos, SEEK_SET, io->uptr);
    8.47 +		return -1;
    8.48 +	}
    8.49 +
    8.50 +	if(id[0] == 'P' && (id[1] == '6' || id[1] == '3')) {
    8.51 +		res = 0;
    8.52 +	}
    8.53 +	io->seek(pos, SEEK_SET, io->uptr);
    8.54 +	return res;
    8.55 +}
    8.56 +
    8.57 +static int iofgetc(struct img_io *io)
    8.58 +{
    8.59 +	char c;
    8.60 +	return io->read(&c, 1, io->uptr) < 1 ? -1 : c;
    8.61 +}
    8.62 +
    8.63 +static char *iofgets(char *buf, int size, struct img_io *io)
    8.64 +{
    8.65 +	int c;
    8.66 +	char *ptr = buf;
    8.67 +
    8.68 +	while(--size > 0 && (c = iofgetc(io)) != -1) {
    8.69 +		*ptr++ = c;
    8.70 +		if(c == '\n') break;
    8.71 +	}
    8.72 +	*ptr = 0;
    8.73 +
    8.74 +	return ptr == buf ? 0 : buf;
    8.75 +}
    8.76 +
    8.77 +/* TODO: implement P3 reading */
    8.78 +static int readppm(struct img_pixmap *img, struct img_io *io)
    8.79 +{
    8.80 +	char buf[256];
    8.81 +	int xsz, ysz, maxval, got_hdrlines = 1;
    8.82 +
    8.83 +	if(!iofgets(buf, sizeof buf, io)) {
    8.84 +		return -1;
    8.85 +	}
    8.86 +	if(!(buf[0] == 'P' && (buf[1] == '6' || buf[1] == '3'))) {
    8.87 +		return -1;
    8.88 +	}
    8.89 +
    8.90 +	while(got_hdrlines < 3 && iofgets(buf, sizeof buf, io)) {
    8.91 +		if(buf[0] == '#') continue;
    8.92 +
    8.93 +		switch(got_hdrlines) {
    8.94 +		case 1:
    8.95 +			if(sscanf(buf, "%d %d\n", &xsz, &ysz) < 2) {
    8.96 +				return -1;
    8.97 +			}
    8.98 +			break;
    8.99 +
   8.100 +		case 2:
   8.101 +			if(sscanf(buf, "%d\n", &maxval) < 1) {
   8.102 +				return -1;
   8.103 +			}
   8.104 +		default:
   8.105 +			break;
   8.106 +		}
   8.107 +		got_hdrlines++;
   8.108 +	}
   8.109 +
   8.110 +	if(xsz < 1 || ysz < 1 || maxval != 255) {
   8.111 +		return -1;
   8.112 +	}
   8.113 +
   8.114 +	if(img_set_pixels(img, xsz, ysz, IMG_FMT_RGB24, 0) == -1) {
   8.115 +		return -1;
   8.116 +	}
   8.117 +
   8.118 +	if(io->read(img->pixels, xsz * ysz * 3, io->uptr) < xsz * ysz * 3) {
   8.119 +		return -1;
   8.120 +	}
   8.121 +	return 0;
   8.122 +}
   8.123 +
   8.124 +static int writeppm(struct img_pixmap *img, struct img_io *io)
   8.125 +{
   8.126 +	int sz;
   8.127 +	char buf[256];
   8.128 +	struct img_pixmap tmpimg;
   8.129 +
   8.130 +	img_init(&tmpimg);
   8.131 +
   8.132 +	if(img->fmt != IMG_FMT_RGB24) {
   8.133 +		if(img_copy(&tmpimg, img) == -1) {
   8.134 +			return -1;
   8.135 +		}
   8.136 +		if(img_convert(&tmpimg, IMG_FMT_RGB24) == -1) {
   8.137 +			return -1;
   8.138 +		}
   8.139 +		img = &tmpimg;
   8.140 +	}
   8.141 +
   8.142 +	sprintf(buf, "P6\n#written by libimago2\n%d %d\n255\n", img->width, img->height);
   8.143 +	if(io->write(buf, strlen(buf), io->uptr) < strlen(buf)) {
   8.144 +		img_destroy(&tmpimg);
   8.145 +		return -1;
   8.146 +	}
   8.147 +
   8.148 +	sz = img->width * img->height * 3;
   8.149 +	if(io->write(img->pixels, sz, io->uptr) < sz) {
   8.150 +		img_destroy(&tmpimg);
   8.151 +		return -1;
   8.152 +	}
   8.153 +
   8.154 +	img_destroy(&tmpimg);
   8.155 +	return 0;
   8.156 +}
     9.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     9.2 +++ b/libs/imago2/ftype_module.c	Sat Sep 19 05:51:51 2015 +0300
     9.3 @@ -0,0 +1,118 @@
     9.4 +/*
     9.5 +libimago - a multi-format image file input/output library.
     9.6 +Copyright (C) 2010 John Tsiombikas <nuclear@member.fsf.org>
     9.7 +
     9.8 +This program is free software: you can redistribute it and/or modify
     9.9 +it under the terms of the GNU Lesser General Public License as published
    9.10 +by the Free Software Foundation, either version 3 of the License, or
    9.11 +(at your option) any later version.
    9.12 +
    9.13 +This program is distributed in the hope that it will be useful,
    9.14 +but WITHOUT ANY WARRANTY; without even the implied warranty of
    9.15 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    9.16 +GNU Lesser General Public License for more details.
    9.17 +
    9.18 +You should have received a copy of the GNU Lesser General Public License
    9.19 +along with this program.  If not, see <http://www.gnu.org/licenses/>.
    9.20 +*/
    9.21 +
    9.22 +#include <stdlib.h>
    9.23 +#include <string.h>
    9.24 +#include "ftype_module.h"
    9.25 +
    9.26 +static struct list_node {
    9.27 +	struct ftype_module *module;
    9.28 +	struct list_node *next;
    9.29 +} *modules;
    9.30 +
    9.31 +/* defined in modules.c which is generated by configure */
    9.32 +void img_modules_init();
    9.33 +
    9.34 +static int done_init;
    9.35 +
    9.36 +int img_register_module(struct ftype_module *mod)
    9.37 +{
    9.38 +	struct list_node *node;
    9.39 +
    9.40 +	if(!(node = malloc(sizeof *node))) {
    9.41 +		return -1;
    9.42 +	}
    9.43 +
    9.44 +	node->module = mod;
    9.45 +	node->next = modules;
    9.46 +	modules = node;
    9.47 +	return 0;
    9.48 +}
    9.49 +
    9.50 +struct ftype_module *img_find_format_module(struct img_io *io)
    9.51 +{
    9.52 +	struct list_node *node;
    9.53 +
    9.54 +	if(!done_init) {
    9.55 +		img_modules_init();
    9.56 +		done_init = 1;
    9.57 +	}
    9.58 +
    9.59 +	node = modules;
    9.60 +	while(node) {
    9.61 +		if(node->module->check(io) != -1) {
    9.62 +			return node->module;
    9.63 +		}
    9.64 +		node = node->next;
    9.65 +	}
    9.66 +	return 0;
    9.67 +}
    9.68 +
    9.69 +struct ftype_module *img_guess_format(const char *fname)
    9.70 +{
    9.71 +	struct list_node *node;
    9.72 +	char *suffix;
    9.73 +	int suffix_len;
    9.74 +
    9.75 +	if(!done_init) {
    9.76 +		img_modules_init();
    9.77 +		done_init = 1;
    9.78 +	}
    9.79 +
    9.80 +	if(!(suffix = strrchr(fname, '.'))) {
    9.81 +		return 0;	/* no suffix, can't guess ... */
    9.82 +	}
    9.83 +	suffix_len = strlen(suffix);
    9.84 +
    9.85 +	node = modules;
    9.86 +	while(node) {
    9.87 +		char *suflist = node->module->suffix;
    9.88 +		char *start, *end;
    9.89 +
    9.90 +		while(*suflist) {
    9.91 +			if(!(start = strstr(suflist, suffix))) {
    9.92 +				break;
    9.93 +			}
    9.94 +			end = start + suffix_len;
    9.95 +
    9.96 +			if(*end == ':' || *end == 0) {
    9.97 +				return node->module;	/* found it */
    9.98 +			}
    9.99 +			suflist = end;
   9.100 +		}
   9.101 +
   9.102 +		node = node->next;
   9.103 +	}
   9.104 +	return 0;
   9.105 +}
   9.106 +
   9.107 +struct ftype_module *img_get_module(int idx)
   9.108 +{
   9.109 +	struct list_node *node;
   9.110 +
   9.111 +	if(!done_init) {
   9.112 +		img_modules_init();
   9.113 +		done_init = 1;
   9.114 +	}
   9.115 +
   9.116 +	node = modules;
   9.117 +	while(node && idx--) {
   9.118 +		node = node->next;
   9.119 +	}
   9.120 +	return node->module;
   9.121 +}
    10.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    10.2 +++ b/libs/imago2/ftype_module.h	Sat Sep 19 05:51:51 2015 +0300
    10.3 @@ -0,0 +1,39 @@
    10.4 +/*
    10.5 +libimago - a multi-format image file input/output library.
    10.6 +Copyright (C) 2010 John Tsiombikas <nuclear@member.fsf.org>
    10.7 +
    10.8 +This program is free software: you can redistribute it and/or modify
    10.9 +it under the terms of the GNU Lesser General Public License as published
   10.10 +by the Free Software Foundation, either version 3 of the License, or
   10.11 +(at your option) any later version.
   10.12 +
   10.13 +This program is distributed in the hope that it will be useful,
   10.14 +but WITHOUT ANY WARRANTY; without even the implied warranty of
   10.15 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   10.16 +GNU Lesser General Public License for more details.
   10.17 +
   10.18 +You should have received a copy of the GNU Lesser General Public License
   10.19 +along with this program.  If not, see <http://www.gnu.org/licenses/>.
   10.20 +*/
   10.21 +
   10.22 +#ifndef FTYPE_MODULE_H_
   10.23 +#define FTYPE_MODULE_H_
   10.24 +
   10.25 +#include "imago2.h"
   10.26 +
   10.27 +struct ftype_module {
   10.28 +	char *suffix;	/* used for format autodetection during saving only */
   10.29 +
   10.30 +	int (*check)(struct img_io *io);
   10.31 +	int (*read)(struct img_pixmap *img, struct img_io *io);
   10.32 +	int (*write)(struct img_pixmap *img, struct img_io *io);
   10.33 +};
   10.34 +
   10.35 +int img_register_module(struct ftype_module *mod);
   10.36 +
   10.37 +struct ftype_module *img_find_format_module(struct img_io *io);
   10.38 +struct ftype_module *img_guess_format(const char *fname);
   10.39 +struct ftype_module *img_get_module(int idx);
   10.40 +
   10.41 +
   10.42 +#endif	/* FTYPE_MODULE_H_ */
    11.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    11.2 +++ b/libs/imago2/imago2.c	Sat Sep 19 05:51:51 2015 +0300
    11.3 @@ -0,0 +1,343 @@
    11.4 +/*
    11.5 +libimago - a multi-format image file input/output library.
    11.6 +Copyright (C) 2010 John Tsiombikas <nuclear@member.fsf.org>
    11.7 +
    11.8 +This program is free software: you can redistribute it and/or modify
    11.9 +it under the terms of the GNU Lesser General Public License as published
   11.10 +by the Free Software Foundation, either version 3 of the License, or
   11.11 +(at your option) any later version.
   11.12 +
   11.13 +This program is distributed in the hope that it will be useful,
   11.14 +but WITHOUT ANY WARRANTY; without even the implied warranty of
   11.15 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   11.16 +GNU Lesser General Public License for more details.
   11.17 +
   11.18 +You should have received a copy of the GNU Lesser General Public License
   11.19 +along with this program.  If not, see <http://www.gnu.org/licenses/>.
   11.20 +*/
   11.21 +
   11.22 +#include <stdio.h>
   11.23 +#include <stdlib.h>
   11.24 +#include <string.h>
   11.25 +#include "imago2.h"
   11.26 +#include "ftype_module.h"
   11.27 +
   11.28 +static int pixel_size(enum img_fmt fmt);
   11.29 +static size_t def_read(void *buf, size_t bytes, void *uptr);
   11.30 +static size_t def_write(void *buf, size_t bytes, void *uptr);
   11.31 +static long def_seek(long offset, int whence, void *uptr);
   11.32 +
   11.33 +
   11.34 +void img_init(struct img_pixmap *img)
   11.35 +{
   11.36 +	img->pixels = 0;
   11.37 +	img->width = img->height = 0;
   11.38 +	img->fmt = IMG_FMT_RGBA32;
   11.39 +	img->pixelsz = pixel_size(img->fmt);
   11.40 +	img->name = 0;
   11.41 +}
   11.42 +
   11.43 +
   11.44 +void img_destroy(struct img_pixmap *img)
   11.45 +{
   11.46 +	free(img->pixels);
   11.47 +	img->pixels = 0;	/* just in case... */
   11.48 +	img->width = img->height = 0xbadbeef;
   11.49 +	free(img->name);
   11.50 +}
   11.51 +
   11.52 +struct img_pixmap *img_create(void)
   11.53 +{
   11.54 +	struct img_pixmap *p;
   11.55 +
   11.56 +	if(!(p = malloc(sizeof *p))) {
   11.57 +		return 0;
   11.58 +	}
   11.59 +	img_init(p);
   11.60 +	return p;
   11.61 +}
   11.62 +
   11.63 +void img_free(struct img_pixmap *img)
   11.64 +{
   11.65 +	img_destroy(img);
   11.66 +	free(img);
   11.67 +}
   11.68 +
   11.69 +int img_set_name(struct img_pixmap *img, const char *name)
   11.70 +{
   11.71 +	char *tmp;
   11.72 +
   11.73 +	if(!(tmp = malloc(strlen(name) + 1))) {
   11.74 +		return -1;
   11.75 +	}
   11.76 +	strcpy(tmp, name);
   11.77 +	img->name = tmp;
   11.78 +	return 0;
   11.79 +}
   11.80 +
   11.81 +int img_set_format(struct img_pixmap *img, enum img_fmt fmt)
   11.82 +{
   11.83 +	if(img->pixels) {
   11.84 +		return img_convert(img, fmt);
   11.85 +	}
   11.86 +	img->fmt = fmt;
   11.87 +	return 0;
   11.88 +}
   11.89 +
   11.90 +int img_copy(struct img_pixmap *dest, struct img_pixmap *src)
   11.91 +{
   11.92 +	return img_set_pixels(dest, src->width, src->height, src->fmt, src->pixels);
   11.93 +}
   11.94 +
   11.95 +int img_set_pixels(struct img_pixmap *img, int w, int h, enum img_fmt fmt, void *pix)
   11.96 +{
   11.97 +	void *newpix;
   11.98 +	int pixsz = pixel_size(fmt);
   11.99 +
  11.100 +	if(!(newpix = malloc(w * h * pixsz))) {
  11.101 +		return -1;
  11.102 +	}
  11.103 +
  11.104 +	if(pix) {
  11.105 +		memcpy(newpix, pix, w * h * pixsz);
  11.106 +	} else {
  11.107 +		memset(newpix, 0, w * h * pixsz);
  11.108 +	}
  11.109 +
  11.110 +	free(img->pixels);
  11.111 +	img->pixels = newpix;
  11.112 +	img->width = w;
  11.113 +	img->height = h;
  11.114 +	img->pixelsz = pixsz;
  11.115 +	img->fmt = fmt;
  11.116 +	return 0;
  11.117 +}
  11.118 +
  11.119 +void *img_load_pixels(const char *fname, int *xsz, int *ysz, enum img_fmt fmt)
  11.120 +{
  11.121 +	struct img_pixmap img;
  11.122 +
  11.123 +	img_init(&img);
  11.124 +
  11.125 +	if(img_load(&img, fname) == -1) {
  11.126 +		return 0;
  11.127 +	}
  11.128 +	if(img.fmt != fmt) {
  11.129 +		if(img_convert(&img, fmt) == -1) {
  11.130 +			img_destroy(&img);
  11.131 +			return 0;
  11.132 +		}
  11.133 +	}
  11.134 +
  11.135 +	*xsz = img.width;
  11.136 +	*ysz = img.height;
  11.137 +	return img.pixels;
  11.138 +}
  11.139 +
  11.140 +int img_save_pixels(const char *fname, void *pix, int xsz, int ysz, enum img_fmt fmt)
  11.141 +{
  11.142 +	struct img_pixmap img;
  11.143 +
  11.144 +	img_init(&img);
  11.145 +	img.fmt = fmt;
  11.146 +	img.name = (char*)fname;
  11.147 +	img.width = xsz;
  11.148 +	img.height = ysz;
  11.149 +	img.pixels = pix;
  11.150 +
  11.151 +	return img_save(&img, fname);
  11.152 +}
  11.153 +
  11.154 +void img_free_pixels(void *pix)
  11.155 +{
  11.156 +	free(pix);
  11.157 +}
  11.158 +
  11.159 +int img_load(struct img_pixmap *img, const char *fname)
  11.160 +{
  11.161 +	int res;
  11.162 +	FILE *fp;
  11.163 +
  11.164 +	if(!(fp = fopen(fname, "rb"))) {
  11.165 +		return -1;
  11.166 +	}
  11.167 +	res = img_read_file(img, fp);
  11.168 +	fclose(fp);
  11.169 +	return res;
  11.170 +}
  11.171 +
  11.172 +/* TODO implement filetype selection */
  11.173 +int img_save(struct img_pixmap *img, const char *fname)
  11.174 +{
  11.175 +	int res;
  11.176 +	FILE *fp;
  11.177 +
  11.178 +	img_set_name(img, fname);
  11.179 +
  11.180 +	if(!(fp = fopen(fname, "wb"))) {
  11.181 +		return -1;
  11.182 +	}
  11.183 +	res = img_write_file(img, fp);
  11.184 +	fclose(fp);
  11.185 +	return res;
  11.186 +}
  11.187 +
  11.188 +int img_read_file(struct img_pixmap *img, FILE *fp)
  11.189 +{
  11.190 +	struct img_io io = {0, def_read, def_write, def_seek};
  11.191 +
  11.192 +	io.uptr = fp;
  11.193 +	return img_read(img, &io);
  11.194 +}
  11.195 +
  11.196 +int img_write_file(struct img_pixmap *img, FILE *fp)
  11.197 +{
  11.198 +	struct img_io io = {0, def_read, def_write, def_seek};
  11.199 +
  11.200 +	io.uptr = fp;
  11.201 +	return img_write(img, &io);
  11.202 +}
  11.203 +
  11.204 +int img_read(struct img_pixmap *img, struct img_io *io)
  11.205 +{
  11.206 +	struct ftype_module *mod;
  11.207 +
  11.208 +	if((mod = img_find_format_module(io))) {
  11.209 +		return mod->read(img, io);
  11.210 +	}
  11.211 +	return -1;
  11.212 +}
  11.213 +
  11.214 +int img_write(struct img_pixmap *img, struct img_io *io)
  11.215 +{
  11.216 +	struct ftype_module *mod;
  11.217 +
  11.218 +	if(!img->name || !(mod = img_guess_format(img->name))) {
  11.219 +		/* TODO throw some sort of warning? */
  11.220 +		/* TODO implement some sort of module priority or let the user specify? */
  11.221 +		if(!(mod = img_get_module(0))) {
  11.222 +			return -1;
  11.223 +		}
  11.224 +	}
  11.225 +
  11.226 +	return mod->write(img, io);
  11.227 +}
  11.228 +
  11.229 +int img_to_float(struct img_pixmap *img)
  11.230 +{
  11.231 +	enum img_fmt targ_fmt;
  11.232 +
  11.233 +	switch(img->fmt) {
  11.234 +	case IMG_FMT_GREY8:
  11.235 +		targ_fmt = IMG_FMT_GREYF;
  11.236 +		break;
  11.237 +
  11.238 +	case IMG_FMT_RGB24:
  11.239 +		targ_fmt = IMG_FMT_RGBF;
  11.240 +		break;
  11.241 +
  11.242 +	case IMG_FMT_RGBA32:
  11.243 +		targ_fmt = IMG_FMT_RGBAF;
  11.244 +		break;
  11.245 +
  11.246 +	default:
  11.247 +		return 0;	/* already float */
  11.248 +	}
  11.249 +
  11.250 +	return img_convert(img, targ_fmt);
  11.251 +}
  11.252 +
  11.253 +int img_to_integer(struct img_pixmap *img)
  11.254 +{
  11.255 +	enum img_fmt targ_fmt;
  11.256 +
  11.257 +	switch(img->fmt) {
  11.258 +	case IMG_FMT_GREYF:
  11.259 +		targ_fmt = IMG_FMT_GREY8;
  11.260 +		break;
  11.261 +
  11.262 +	case IMG_FMT_RGBF:
  11.263 +		targ_fmt = IMG_FMT_RGB24;
  11.264 +		break;
  11.265 +
  11.266 +	case IMG_FMT_RGBAF:
  11.267 +		targ_fmt = IMG_FMT_RGBA32;
  11.268 +		break;
  11.269 +
  11.270 +	default:
  11.271 +		return 0;	/* already integer */
  11.272 +	}
  11.273 +
  11.274 +	return img_convert(img, targ_fmt);
  11.275 +}
  11.276 +
  11.277 +int img_is_float(struct img_pixmap *img)
  11.278 +{
  11.279 +	return img->fmt >= IMG_FMT_GREYF && img->fmt <= IMG_FMT_RGBAF;
  11.280 +}
  11.281 +
  11.282 +int img_has_alpha(struct img_pixmap *img)
  11.283 +{
  11.284 +	return img->fmt >= IMG_FMT_GREY8 && img->fmt <= IMG_FMT_RGBA32;
  11.285 +}
  11.286 +
  11.287 +void img_io_set_user_data(struct img_io *io, void *uptr)
  11.288 +{
  11.289 +	io->uptr = uptr;
  11.290 +}
  11.291 +
  11.292 +void img_io_set_read_func(struct img_io *io, size_t (*read)(void*, size_t, void*))
  11.293 +{
  11.294 +	io->read = read;
  11.295 +}
  11.296 +
  11.297 +void img_io_set_write_func(struct img_io *io, size_t (*write)(void*, size_t, void*))
  11.298 +{
  11.299 +	io->write = write;
  11.300 +}
  11.301 +
  11.302 +void img_io_set_seek_func(struct img_io *io, long (*seek)(long, int, void*))
  11.303 +{
  11.304 +	io->seek = seek;
  11.305 +}
  11.306 +
  11.307 +
  11.308 +static int pixel_size(enum img_fmt fmt)
  11.309 +{
  11.310 +	switch(fmt) {
  11.311 +	case IMG_FMT_GREY8:
  11.312 +		return 1;
  11.313 +	case IMG_FMT_RGB24:
  11.314 +		return 3;
  11.315 +	case IMG_FMT_RGBA32:
  11.316 +		return 4;
  11.317 +	case IMG_FMT_GREYF:
  11.318 +		return sizeof(float);
  11.319 +	case IMG_FMT_RGBF:
  11.320 +		return 3 * sizeof(float);
  11.321 +	case IMG_FMT_RGBAF:
  11.322 +		return 4 * sizeof(float);
  11.323 +	default:
  11.324 +		break;
  11.325 +	}
  11.326 +	return 0;
  11.327 +}
  11.328 +
  11.329 +static size_t def_read(void *buf, size_t bytes, void *uptr)
  11.330 +{
  11.331 +	return uptr ? fread(buf, 1, bytes, uptr) : 0;
  11.332 +}
  11.333 +
  11.334 +static size_t def_write(void *buf, size_t bytes, void *uptr)
  11.335 +{
  11.336 +	return uptr ? fwrite(buf, 1, bytes, uptr) : 0;
  11.337 +}
  11.338 +
  11.339 +static long def_seek(long offset, int whence, void *uptr)
  11.340 +{
  11.341 +	if(!uptr || fseek(uptr, offset, whence) == -1) {
  11.342 +		return -1;
  11.343 +	}
  11.344 +	return ftell(uptr);
  11.345 +}
  11.346 +
    12.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    12.2 +++ b/libs/imago2/imago2.h	Sat Sep 19 05:51:51 2015 +0300
    12.3 @@ -0,0 +1,185 @@
    12.4 +/*
    12.5 +libimago - a multi-format image file input/output library.
    12.6 +Copyright (C) 2010 John Tsiombikas <nuclear@member.fsf.org>
    12.7 +
    12.8 +This program is free software: you can redistribute it and/or modify
    12.9 +it under the terms of the GNU Lesser General Public License as published
   12.10 +by the Free Software Foundation, either version 3 of the License, or
   12.11 +(at your option) any later version.
   12.12 +
   12.13 +This program is distributed in the hope that it will be useful,
   12.14 +but WITHOUT ANY WARRANTY; without even the implied warranty of
   12.15 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   12.16 +GNU Lesser General Public License for more details.
   12.17 +
   12.18 +You should have received a copy of the GNU Lesser General Public License
   12.19 +along with this program.  If not, see <http://www.gnu.org/licenses/>.
   12.20 +*/
   12.21 +
   12.22 +#ifndef IMAGO2_H_
   12.23 +#define IMAGO2_H_
   12.24 +
   12.25 +#include <stdio.h>
   12.26 +
   12.27 +#ifdef __cplusplus
   12.28 +#define IMG_OPTARG(arg, val)	arg = val
   12.29 +#else
   12.30 +#define IMG_OPTARG(arg, val)	arg
   12.31 +#endif
   12.32 +
   12.33 +/* XXX if you change this make sure to also change pack/unpack arrays in conv.c */
   12.34 +enum img_fmt {
   12.35 +	IMG_FMT_GREY8,
   12.36 +	IMG_FMT_RGB24,
   12.37 +	IMG_FMT_RGBA32,
   12.38 +	IMG_FMT_GREYF,
   12.39 +	IMG_FMT_RGBF,
   12.40 +	IMG_FMT_RGBAF,
   12.41 +
   12.42 +	NUM_IMG_FMT
   12.43 +};
   12.44 +
   12.45 +struct img_pixmap {
   12.46 +	void *pixels;
   12.47 +	int width, height;
   12.48 +	enum img_fmt fmt;
   12.49 +	int pixelsz;
   12.50 +	char *name;
   12.51 +};
   12.52 +
   12.53 +struct img_io {
   12.54 +	void *uptr;	/* user-data */
   12.55 +
   12.56 +	size_t (*read)(void *buf, size_t bytes, void *uptr);
   12.57 +	size_t (*write)(void *buf, size_t bytes, void *uptr);
   12.58 +	long (*seek)(long offs, int whence, void *uptr);
   12.59 +};
   12.60 +
   12.61 +#ifdef __cplusplus
   12.62 +extern "C" {
   12.63 +#endif
   12.64 +
   12.65 +/* initialize the img_pixmap structure */
   12.66 +void img_init(struct img_pixmap *img);
   12.67 +/* destroys the img_pixmap structure, freeing the pixel buffer (if available)
   12.68 + * and any other memory held by the pixmap.
   12.69 + */
   12.70 +void img_destroy(struct img_pixmap *img);
   12.71 +
   12.72 +/* convenience function that allocates an img_pixmap struct and then initializes it.
   12.73 + * returns null if the malloc fails.
   12.74 + */
   12.75 +struct img_pixmap *img_create(void);
   12.76 +/* frees a pixmap previously allocated with img_create (free followed by img_destroy) */
   12.77 +void img_free(struct img_pixmap *img);
   12.78 +
   12.79 +int img_set_name(struct img_pixmap *img, const char *name);
   12.80 +
   12.81 +/* set the image pixel format */
   12.82 +int img_set_format(struct img_pixmap *img, enum img_fmt fmt);
   12.83 +
   12.84 +/* copies one pixmap to another.
   12.85 + * equivalent to: img_set_pixels(dest, src->width, src->height, src->fmt, src->pixels)
   12.86 + */
   12.87 +int img_copy(struct img_pixmap *dest, struct img_pixmap *src);
   12.88 +
   12.89 +/* allocates a pixel buffer of the specified dimensions and format, and copies the
   12.90 + * pixels given through the pix pointer into it.
   12.91 + * the pix pointer can be null, in which case there's no copy, just allocation.
   12.92 + *
   12.93 + * C++: fmt and pix have default parameters IMG_FMT_RGBA32 and null respectively.
   12.94 + */
   12.95 +int img_set_pixels(struct img_pixmap *img, int w, int h, IMG_OPTARG(enum img_fmt fmt, IMG_FMT_RGBA32), IMG_OPTARG(void *pix, 0));
   12.96 +
   12.97 +/* Simplified image loading
   12.98 + * Loads the specified file, and returns a pointer to an array of pixels of the
   12.99 + * requested pixel format. The width and height of the image are returned through
  12.100 + * the xsz and ysz pointers.
  12.101 + * If the image cannot be loaded, the function returns null.
  12.102 + *
  12.103 + * C++: the format argument is optional and defaults to IMG_FMT_RGBA32
  12.104 + */
  12.105 +void *img_load_pixels(const char *fname, int *xsz, int *ysz, IMG_OPTARG(enum img_fmt fmt, IMG_FMT_RGBA32));
  12.106 +
  12.107 +/* Simplified image saving
  12.108 + * Reads an array of pixels supplied through the pix pointer, of dimensions xsz
  12.109 + * and ysz, and pixel-format fmt, and saves it to a file.
  12.110 + * The output filetype is guessed by the filename suffix.
  12.111 + *
  12.112 + * C++: the format argument is optional and defaults to IMG_FMT_RGBA32
  12.113 + */
  12.114 +int img_save_pixels(const char *fname, void *pix, int xsz, int ysz, IMG_OPTARG(enum img_fmt fmt, IMG_FMT_RGBA32));
  12.115 +
  12.116 +/* Frees the memory allocated by img_load_pixels */
  12.117 +void img_free_pixels(void *pix);
  12.118 +
  12.119 +/* Loads an image file into the supplied pixmap */
  12.120 +int img_load(struct img_pixmap *img, const char *fname);
  12.121 +/* Saves the supplied pixmap to a file. The output filetype is guessed by the filename suffix */
  12.122 +int img_save(struct img_pixmap *img, const char *fname);
  12.123 +
  12.124 +/* Reads an image from an open FILE* into the supplied pixmap */
  12.125 +int img_read_file(struct img_pixmap *img, FILE *fp);
  12.126 +/* Writes the supplied pixmap to an open FILE* */
  12.127 +int img_write_file(struct img_pixmap *img, FILE *fp);
  12.128 +
  12.129 +/* Reads an image using user-defined file-i/o functions (see img_io_set_*) */
  12.130 +int img_read(struct img_pixmap *img, struct img_io *io);
  12.131 +/* Writes an image using user-defined file-i/o functions (see img_io_set_*) */
  12.132 +int img_write(struct img_pixmap *img, struct img_io *io);
  12.133 +
  12.134 +/* Converts an image to the specified pixel format */
  12.135 +int img_convert(struct img_pixmap *img, enum img_fmt tofmt);
  12.136 +
  12.137 +/* Converts an image from an integer pixel format to the corresponding floating point one */
  12.138 +int img_to_float(struct img_pixmap *img);
  12.139 +/* Converts an image from a floating point pixel format to the corresponding integer one */
  12.140 +int img_to_integer(struct img_pixmap *img);
  12.141 +
  12.142 +/* Returns non-zero (true) if the supplied image is in a floating point pixel format */
  12.143 +int img_is_float(struct img_pixmap *img);
  12.144 +/* Returns non-zero (true) if the supplied image has an alpha channel */
  12.145 +int img_has_alpha(struct img_pixmap *img);
  12.146 +
  12.147 +
  12.148 +/* These functions can be used to fill an img_io struct before it's passed to
  12.149 + * one of the user-defined i/o image reading/writing functions (img_read/img_write).
  12.150 + *
  12.151 + * User-defined i/o functions:
  12.152 + *
  12.153 + * - size_t read_func(void *buffer, size_t bytes, void *user_ptr)
  12.154 + * Must try to fill the buffer with the specified number of bytes, and return
  12.155 + * the number of bytes actually read.
  12.156 + *
  12.157 + * - size_t write_func(void *buffer, size_t bytes, void *user_ptr)
  12.158 + * Must write the specified number of bytes from the supplied buffer and return
  12.159 + * the number of bytes actually written.
  12.160 + *
  12.161 + * - long seek_func(long offset, int whence, void *user_ptr)
  12.162 + * Must seek offset bytes from: the beginning of the file if whence is SEEK_SET,
  12.163 + * the current position if whence is SEEK_CUR, or the end of the file if whence is
  12.164 + * SEEK_END, and return the resulting file offset from the beginning of the file.
  12.165 + * (i.e. seek_func(0, SEEK_CUR, user_ptr); must be equivalent to an ftell).
  12.166 + *
  12.167 + * All three functions get the user-data pointer set through img_io_set_user_data
  12.168 + * as their last argument.
  12.169 + *
  12.170 + * Note: obviously you don't need to set a write function if you're only going
  12.171 + * to call img_read, or the read and seek function if you're only going to call
  12.172 + * img_write.
  12.173 + *
  12.174 + * Note: if the user-supplied write function is buffered, make sure to flush
  12.175 + * (or close the file) after img_write returns.
  12.176 + */
  12.177 +void img_io_set_user_data(struct img_io *io, void *uptr);
  12.178 +void img_io_set_read_func(struct img_io *io, size_t (*read)(void*, size_t, void*));
  12.179 +void img_io_set_write_func(struct img_io *io, size_t (*write)(void*, size_t, void*));
  12.180 +void img_io_set_seek_func(struct img_io *io, long (*seek)(long, int, void*));
  12.181 +
  12.182 +
  12.183 +#ifdef __cplusplus
  12.184 +}
  12.185 +#endif
  12.186 +
  12.187 +
  12.188 +#endif	/* IMAGO_H_ */
    13.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    13.2 +++ b/libs/imago2/modules.c	Sat Sep 19 05:51:51 2015 +0300
    13.3 @@ -0,0 +1,11 @@
    13.4 +/* this file is generated by ./configure, do not edit */
    13.5 +int img_register_jpeg();
    13.6 +int img_register_png();
    13.7 +int img_register_ppm();
    13.8 +
    13.9 +void img_modules_init(void)
   13.10 +{
   13.11 +	img_register_jpeg();
   13.12 +	img_register_png();
   13.13 +	img_register_ppm();
   13.14 +}
    14.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    14.2 +++ b/libs/libjpeg/README	Sat Sep 19 05:51:51 2015 +0300
    14.3 @@ -0,0 +1,326 @@
    14.4 +The Independent JPEG Group's JPEG software
    14.5 +==========================================
    14.6 +
    14.7 +README for release 8c of 16-Jan-2011
    14.8 +====================================
    14.9 +
   14.10 +This distribution contains the eighth public release of the Independent JPEG
   14.11 +Group's free JPEG software.  You are welcome to redistribute this software and
   14.12 +to use it for any purpose, subject to the conditions under LEGAL ISSUES, below.
   14.13 +
   14.14 +This software is the work of Tom Lane, Guido Vollbeding, Philip Gladstone,
   14.15 +Bill Allombert, Jim Boucher, Lee Crocker, Bob Friesenhahn, Ben Jackson,
   14.16 +Julian Minguillon, Luis Ortiz, George Phillips, Davide Rossi, Ge' Weijers,
   14.17 +and other members of the Independent JPEG Group.
   14.18 +
   14.19 +IJG is not affiliated with the official ISO JPEG standards committee.
   14.20 +
   14.21 +
   14.22 +DOCUMENTATION ROADMAP
   14.23 +=====================
   14.24 +
   14.25 +This file contains the following sections:
   14.26 +
   14.27 +OVERVIEW            General description of JPEG and the IJG software.
   14.28 +LEGAL ISSUES        Copyright, lack of warranty, terms of distribution.
   14.29 +REFERENCES          Where to learn more about JPEG.
   14.30 +ARCHIVE LOCATIONS   Where to find newer versions of this software.
   14.31 +ACKNOWLEDGMENTS     Special thanks.
   14.32 +FILE FORMAT WARS    Software *not* to get.
   14.33 +TO DO               Plans for future IJG releases.
   14.34 +
   14.35 +Other documentation files in the distribution are:
   14.36 +
   14.37 +User documentation:
   14.38 +  install.txt       How to configure and install the IJG software.
   14.39 +  usage.txt         Usage instructions for cjpeg, djpeg, jpegtran,
   14.40 +                    rdjpgcom, and wrjpgcom.
   14.41 +  *.1               Unix-style man pages for programs (same info as usage.txt).
   14.42 +  wizard.txt        Advanced usage instructions for JPEG wizards only.
   14.43 +  change.log        Version-to-version change highlights.
   14.44 +Programmer and internal documentation:
   14.45 +  libjpeg.txt       How to use the JPEG library in your own programs.
   14.46 +  example.c         Sample code for calling the JPEG library.
   14.47 +  structure.txt     Overview of the JPEG library's internal structure.
   14.48 +  filelist.txt      Road map of IJG files.
   14.49 +  coderules.txt     Coding style rules --- please read if you contribute code.
   14.50 +
   14.51 +Please read at least the files install.txt and usage.txt.  Some information
   14.52 +can also be found in the JPEG FAQ (Frequently Asked Questions) article.  See
   14.53 +ARCHIVE LOCATIONS below to find out where to obtain the FAQ article.
   14.54 +
   14.55 +If you want to understand how the JPEG code works, we suggest reading one or
   14.56 +more of the REFERENCES, then looking at the documentation files (in roughly
   14.57 +the order listed) before diving into the code.
   14.58 +
   14.59 +
   14.60 +OVERVIEW
   14.61 +========
   14.62 +
   14.63 +This package contains C software to implement JPEG image encoding, decoding,
   14.64 +and transcoding.  JPEG (pronounced "jay-peg") is a standardized compression
   14.65 +method for full-color and gray-scale images.
   14.66 +
   14.67 +This software implements JPEG baseline, extended-sequential, and progressive
   14.68 +compression processes.  Provision is made for supporting all variants of these
   14.69 +processes, although some uncommon parameter settings aren't implemented yet.
   14.70 +We have made no provision for supporting the hierarchical or lossless
   14.71 +processes defined in the standard.
   14.72 +
   14.73 +We provide a set of library routines for reading and writing JPEG image files,
   14.74 +plus two sample applications "cjpeg" and "djpeg", which use the library to
   14.75 +perform conversion between JPEG and some other popular image file formats.
   14.76 +The library is intended to be reused in other applications.
   14.77 +
   14.78 +In order to support file conversion and viewing software, we have included
   14.79 +considerable functionality beyond the bare JPEG coding/decoding capability;
   14.80 +for example, the color quantization modules are not strictly part of JPEG
   14.81 +decoding, but they are essential for output to colormapped file formats or
   14.82 +colormapped displays.  These extra functions can be compiled out of the
   14.83 +library if not required for a particular application.
   14.84 +
   14.85 +We have also included "jpegtran", a utility for lossless transcoding between
   14.86 +different JPEG processes, and "rdjpgcom" and "wrjpgcom", two simple
   14.87 +applications for inserting and extracting textual comments in JFIF files.
   14.88 +
   14.89 +The emphasis in designing this software has been on achieving portability and
   14.90 +flexibility, while also making it fast enough to be useful.  In particular,
   14.91 +the software is not intended to be read as a tutorial on JPEG.  (See the
   14.92 +REFERENCES section for introductory material.)  Rather, it is intended to
   14.93 +be reliable, portable, industrial-strength code.  We do not claim to have
   14.94 +achieved that goal in every aspect of the software, but we strive for it.
   14.95 +
   14.96 +We welcome the use of this software as a component of commercial products.
   14.97 +No royalty is required, but we do ask for an acknowledgement in product
   14.98 +documentation, as described under LEGAL ISSUES.
   14.99 +
  14.100 +
  14.101 +LEGAL ISSUES
  14.102 +============
  14.103 +
  14.104 +In plain English:
  14.105 +
  14.106 +1. We don't promise that this software works.  (But if you find any bugs,
  14.107 +   please let us know!)
  14.108 +2. You can use this software for whatever you want.  You don't have to pay us.
  14.109 +3. You may not pretend that you wrote this software.  If you use it in a
  14.110 +   program, you must acknowledge somewhere in your documentation that
  14.111 +   you've used the IJG code.
  14.112 +
  14.113 +In legalese:
  14.114 +
  14.115 +The authors make NO WARRANTY or representation, either express or implied,
  14.116 +with respect to this software, its quality, accuracy, merchantability, or
  14.117 +fitness for a particular purpose.  This software is provided "AS IS", and you,
  14.118 +its user, assume the entire risk as to its quality and accuracy.
  14.119 +
  14.120 +This software is copyright (C) 1991-2011, Thomas G. Lane, Guido Vollbeding.
  14.121 +All Rights Reserved except as specified below.
  14.122 +
  14.123 +Permission is hereby granted to use, copy, modify, and distribute this
  14.124 +software (or portions thereof) for any purpose, without fee, subject to these
  14.125 +conditions:
  14.126 +(1) If any part of the source code for this software is distributed, then this
  14.127 +README file must be included, with this copyright and no-warranty notice
  14.128 +unaltered; and any additions, deletions, or changes to the original files
  14.129 +must be clearly indicated in accompanying documentation.
  14.130 +(2) If only executable code is distributed, then the accompanying
  14.131 +documentation must state that "this software is based in part on the work of
  14.132 +the Independent JPEG Group".
  14.133 +(3) Permission for use of this software is granted only if the user accepts
  14.134 +full responsibility for any undesirable consequences; the authors accept
  14.135 +NO LIABILITY for damages of any kind.
  14.136 +
  14.137 +These conditions apply to any software derived from or based on the IJG code,
  14.138 +not just to the unmodified library.  If you use our work, you ought to
  14.139 +acknowledge us.
  14.140 +
  14.141 +Permission is NOT granted for the use of any IJG author's name or company name
  14.142 +in advertising or publicity relating to this software or products derived from
  14.143 +it.  This software may be referred to only as "the Independent JPEG Group's
  14.144 +software".
  14.145 +
  14.146 +We specifically permit and encourage the use of this software as the basis of
  14.147 +commercial products, provided that all warranty or liability claims are
  14.148 +assumed by the product vendor.
  14.149 +
  14.150 +
  14.151 +ansi2knr.c is included in this distribution by permission of L. Peter Deutsch,
  14.152 +sole proprietor of its copyright holder, Aladdin Enterprises of Menlo Park, CA.
  14.153 +ansi2knr.c is NOT covered by the above copyright and conditions, but instead
  14.154 +by the usual distribution terms of the Free Software Foundation; principally,
  14.155 +that you must include source code if you redistribute it.  (See the file
  14.156 +ansi2knr.c for full details.)  However, since ansi2knr.c is not needed as part
  14.157 +of any program generated from the IJG code, this does not limit you more than
  14.158 +the foregoing paragraphs do.
  14.159 +
  14.160 +The Unix configuration script "configure" was produced with GNU Autoconf.
  14.161 +It is copyright by the Free Software Foundation but is freely distributable.
  14.162 +The same holds for its supporting scripts (config.guess, config.sub,
  14.163 +ltmain.sh).  Another support script, install-sh, is copyright by X Consortium
  14.164 +but is also freely distributable.
  14.165 +
  14.166 +The IJG distribution formerly included code to read and write GIF files.
  14.167 +To avoid entanglement with the Unisys LZW patent, GIF reading support has
  14.168 +been removed altogether, and the GIF writer has been simplified to produce
  14.169 +"uncompressed GIFs".  This technique does not use the LZW algorithm; the
  14.170 +resulting GIF files are larger than usual, but are readable by all standard
  14.171 +GIF decoders.
  14.172 +
  14.173 +We are required to state that
  14.174 +    "The Graphics Interchange Format(c) is the Copyright property of
  14.175 +    CompuServe Incorporated.  GIF(sm) is a Service Mark property of
  14.176 +    CompuServe Incorporated."
  14.177 +
  14.178 +
  14.179 +REFERENCES
  14.180 +==========
  14.181 +
  14.182 +We recommend reading one or more of these references before trying to
  14.183 +understand the innards of the JPEG software.
  14.184 +
  14.185 +The best short technical introduction to the JPEG compression algorithm is
  14.186 +	Wallace, Gregory K.  "The JPEG Still Picture Compression Standard",
  14.187 +	Communications of the ACM, April 1991 (vol. 34 no. 4), pp. 30-44.
  14.188 +(Adjacent articles in that issue discuss MPEG motion picture compression,
  14.189 +applications of JPEG, and related topics.)  If you don't have the CACM issue
  14.190 +handy, a PostScript file containing a revised version of Wallace's article is
  14.191 +available at http://www.ijg.org/files/wallace.ps.gz.  The file (actually
  14.192 +a preprint for an article that appeared in IEEE Trans. Consumer Electronics)
  14.193 +omits the sample images that appeared in CACM, but it includes corrections
  14.194 +and some added material.  Note: the Wallace article is copyright ACM and IEEE,
  14.195 +and it may not be used for commercial purposes.
  14.196 +
  14.197 +A somewhat less technical, more leisurely introduction to JPEG can be found in
  14.198 +"The Data Compression Book" by Mark Nelson and Jean-loup Gailly, published by
  14.199 +M&T Books (New York), 2nd ed. 1996, ISBN 1-55851-434-1.  This book provides
  14.200 +good explanations and example C code for a multitude of compression methods
  14.201 +including JPEG.  It is an excellent source if you are comfortable reading C
  14.202 +code but don't know much about data compression in general.  The book's JPEG
  14.203 +sample code is far from industrial-strength, but when you are ready to look
  14.204 +at a full implementation, you've got one here...
  14.205 +
  14.206 +The best currently available description of JPEG is the textbook "JPEG Still
  14.207 +Image Data Compression Standard" by William B. Pennebaker and Joan L.
  14.208 +Mitchell, published by Van Nostrand Reinhold, 1993, ISBN 0-442-01272-1.
  14.209 +Price US$59.95, 638 pp.  The book includes the complete text of the ISO JPEG
  14.210 +standards (DIS 10918-1 and draft DIS 10918-2).
  14.211 +Although this is by far the most detailed and comprehensive exposition of
  14.212 +JPEG publicly available, we point out that it is still missing an explanation
  14.213 +of the most essential properties and algorithms of the underlying DCT
  14.214 +technology.
  14.215 +If you think that you know about DCT-based JPEG after reading this book,
  14.216 +then you are in delusion.  The real fundamentals and corresponding potential
  14.217 +of DCT-based JPEG are not publicly known so far, and that is the reason for
  14.218 +all the mistaken developments taking place in the image coding domain.
  14.219 +
  14.220 +The original JPEG standard is divided into two parts, Part 1 being the actual
  14.221 +specification, while Part 2 covers compliance testing methods.  Part 1 is
  14.222 +titled "Digital Compression and Coding of Continuous-tone Still Images,
  14.223 +Part 1: Requirements and guidelines" and has document numbers ISO/IEC IS
  14.224 +10918-1, ITU-T T.81.  Part 2 is titled "Digital Compression and Coding of
  14.225 +Continuous-tone Still Images, Part 2: Compliance testing" and has document
  14.226 +numbers ISO/IEC IS 10918-2, ITU-T T.83.
  14.227 +IJG JPEG 8 introduces an implementation of the JPEG SmartScale extension
  14.228 +which is specified in a contributed document at ITU and ISO with title "ITU-T
  14.229 +JPEG-Plus Proposal for Extending ITU-T T.81 for Advanced Image Coding", April
  14.230 +2006, Geneva, Switzerland.  The latest version of the document is Revision 3.
  14.231 +
  14.232 +The JPEG standard does not specify all details of an interchangeable file
  14.233 +format.  For the omitted details we follow the "JFIF" conventions, revision
  14.234 +1.02.  JFIF 1.02 has been adopted as an Ecma International Technical Report
  14.235 +and thus received a formal publication status.  It is available as a free
  14.236 +download in PDF format from
  14.237 +http://www.ecma-international.org/publications/techreports/E-TR-098.htm.
  14.238 +A PostScript version of the JFIF document is available at
  14.239 +http://www.ijg.org/files/jfif.ps.gz.  There is also a plain text version at
  14.240 +http://www.ijg.org/files/jfif.txt.gz, but it is missing the figures.
  14.241 +
  14.242 +The TIFF 6.0 file format specification can be obtained by FTP from
  14.243 +ftp://ftp.sgi.com/graphics/tiff/TIFF6.ps.gz.  The JPEG incorporation scheme
  14.244 +found in the TIFF 6.0 spec of 3-June-92 has a number of serious problems.
  14.245 +IJG does not recommend use of the TIFF 6.0 design (TIFF Compression tag 6).
  14.246 +Instead, we recommend the JPEG design proposed by TIFF Technical Note #2
  14.247 +(Compression tag 7).  Copies of this Note can be obtained from
  14.248 +http://www.ijg.org/files/.  It is expected that the next revision
  14.249 +of the TIFF spec will replace the 6.0 JPEG design with the Note's design.
  14.250 +Although IJG's own code does not support TIFF/JPEG, the free libtiff library
  14.251 +uses our library to implement TIFF/JPEG per the Note.
  14.252 +
  14.253 +
  14.254 +ARCHIVE LOCATIONS
  14.255 +=================
  14.256 +
  14.257 +The "official" archive site for this software is www.ijg.org.
  14.258 +The most recent released version can always be found there in
  14.259 +directory "files".  This particular version will be archived as
  14.260 +http://www.ijg.org/files/jpegsrc.v8c.tar.gz, and in Windows-compatible
  14.261 +"zip" archive format as http://www.ijg.org/files/jpegsr8c.zip.
  14.262 +
  14.263 +The JPEG FAQ (Frequently Asked Questions) article is a source of some
  14.264 +general information about JPEG.
  14.265 +It is available on the World Wide Web at http://www.faqs.org/faqs/jpeg-faq/
  14.266 +and other news.answers archive sites, including the official news.answers
  14.267 +archive at rtfm.mit.edu: ftp://rtfm.mit.edu/pub/usenet/news.answers/jpeg-faq/.
  14.268 +If you don't have Web or FTP access, send e-mail to mail-server@rtfm.mit.edu
  14.269 +with body
  14.270 +	send usenet/news.answers/jpeg-faq/part1
  14.271 +	send usenet/news.answers/jpeg-faq/part2
  14.272 +
  14.273 +
  14.274 +ACKNOWLEDGMENTS
  14.275 +===============
  14.276 +
  14.277 +Thank to Juergen Bruder for providing me with a copy of the common DCT
  14.278 +algorithm article, only to find out that I had come to the same result
  14.279 +in a more direct and comprehensible way with a more generative approach.
  14.280 +
  14.281 +Thank to Istvan Sebestyen and Joan L. Mitchell for inviting me to the
  14.282 +ITU JPEG (Study Group 16) meeting in Geneva, Switzerland.
  14.283 +
  14.284 +Thank to Thomas Wiegand and Gary Sullivan for inviting me to the
  14.285 +Joint Video Team (MPEG & ITU) meeting in Geneva, Switzerland.
  14.286 +
  14.287 +Thank to John Korejwa and Massimo Ballerini for inviting me to
  14.288 +fruitful consultations in Boston, MA and Milan, Italy.
  14.289 +
  14.290 +Thank to Hendrik Elstner, Roland Fassauer, Simone Zuck, Guenther
  14.291 +Maier-Gerber, Walter Stoeber, Fred Schmitz, and Norbert Braunagel
  14.292 +for corresponding business development.
  14.293 +
  14.294 +Thank to Nico Zschach and Dirk Stelling of the technical support team
  14.295 +at the Digital Images company in Halle for providing me with extra
  14.296 +equipment for configuration tests.
  14.297 +
  14.298 +Thank to Richard F. Lyon (then of Foveon Inc.) for fruitful
  14.299 +communication about JPEG configuration in Sigma Photo Pro software.
  14.300 +
  14.301 +Thank to Andrew Finkenstadt for hosting the ijg.org site.
  14.302 +
  14.303 +Last but not least special thank to Thomas G. Lane for the original
  14.304 +design and development of this singular software package.
  14.305 +
  14.306 +
  14.307 +FILE FORMAT WARS
  14.308 +================
  14.309 +
  14.310 +The ISO JPEG standards committee actually promotes different formats like
  14.311 +"JPEG 2000" or "JPEG XR" which are incompatible with original DCT-based
  14.312 +JPEG and which are based on faulty technologies.  IJG therefore does not
  14.313 +and will not support such momentary mistakes (see REFERENCES).
  14.314 +We have little or no sympathy for the promotion of these formats.  Indeed,
  14.315 +one of the original reasons for developing this free software was to help
  14.316 +force convergence on common, interoperable format standards for JPEG files.
  14.317 +Don't use an incompatible file format!
  14.318 +(In any case, our decoder will remain capable of reading existing JPEG
  14.319 +image files indefinitely.)
  14.320 +
  14.321 +
  14.322 +TO DO
  14.323 +=====
  14.324 +
  14.325 +Version 8 is the first release of a new generation JPEG standard
  14.326 +to overcome the limitations of the original JPEG specification.
  14.327 +More features are being prepared for coming releases...
  14.328 +
  14.329 +Please send bug reports, offers of help, etc. to jpeg-info@uc.ag.
    15.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    15.2 +++ b/libs/libjpeg/cderror.h	Sat Sep 19 05:51:51 2015 +0300
    15.3 @@ -0,0 +1,132 @@
    15.4 +/*
    15.5 + * cderror.h
    15.6 + *
    15.7 + * Copyright (C) 1994-1997, Thomas G. Lane.
    15.8 + * This file is part of the Independent JPEG Group's software.
    15.9 + * For conditions of distribution and use, see the accompanying README file.
   15.10 + *
   15.11 + * This file defines the error and message codes for the cjpeg/djpeg
   15.12 + * applications.  These strings are not needed as part of the JPEG library
   15.13 + * proper.
   15.14 + * Edit this file to add new codes, or to translate the message strings to
   15.15 + * some other language.
   15.16 + */
   15.17 +
   15.18 +/*
   15.19 + * To define the enum list of message codes, include this file without
   15.20 + * defining macro JMESSAGE.  To create a message string table, include it
   15.21 + * again with a suitable JMESSAGE definition (see jerror.c for an example).
   15.22 + */
   15.23 +#ifndef JMESSAGE
   15.24 +#ifndef CDERROR_H
   15.25 +#define CDERROR_H
   15.26 +/* First time through, define the enum list */
   15.27 +#define JMAKE_ENUM_LIST
   15.28 +#else
   15.29 +/* Repeated inclusions of this file are no-ops unless JMESSAGE is defined */
   15.30 +#define JMESSAGE(code,string)
   15.31 +#endif /* CDERROR_H */
   15.32 +#endif /* JMESSAGE */
   15.33 +
   15.34 +#ifdef JMAKE_ENUM_LIST
   15.35 +
   15.36 +typedef enum {
   15.37 +
   15.38 +#define JMESSAGE(code,string)	code ,
   15.39 +
   15.40 +#endif /* JMAKE_ENUM_LIST */
   15.41 +
   15.42 +JMESSAGE(JMSG_FIRSTADDONCODE=1000, NULL) /* Must be first entry! */
   15.43 +
   15.44 +#ifdef BMP_SUPPORTED
   15.45 +JMESSAGE(JERR_BMP_BADCMAP, "Unsupported BMP colormap format")
   15.46 +JMESSAGE(JERR_BMP_BADDEPTH, "Only 8- and 24-bit BMP files are supported")
   15.47 +JMESSAGE(JERR_BMP_BADHEADER, "Invalid BMP file: bad header length")
   15.48 +JMESSAGE(JERR_BMP_BADPLANES, "Invalid BMP file: biPlanes not equal to 1")
   15.49 +JMESSAGE(JERR_BMP_COLORSPACE, "BMP output must be grayscale or RGB")
   15.50 +JMESSAGE(JERR_BMP_COMPRESSED, "Sorry, compressed BMPs not yet supported")
   15.51 +JMESSAGE(JERR_BMP_NOT, "Not a BMP file - does not start with BM")
   15.52 +JMESSAGE(JTRC_BMP, "%ux%u 24-bit BMP image")
   15.53 +JMESSAGE(JTRC_BMP_MAPPED, "%ux%u 8-bit colormapped BMP image")
   15.54 +JMESSAGE(JTRC_BMP_OS2, "%ux%u 24-bit OS2 BMP image")
   15.55 +JMESSAGE(JTRC_BMP_OS2_MAPPED, "%ux%u 8-bit colormapped OS2 BMP image")
   15.56 +#endif /* BMP_SUPPORTED */
   15.57 +
   15.58 +#ifdef GIF_SUPPORTED
   15.59 +JMESSAGE(JERR_GIF_BUG, "GIF output got confused")
   15.60 +JMESSAGE(JERR_GIF_CODESIZE, "Bogus GIF codesize %d")
   15.61 +JMESSAGE(JERR_GIF_COLORSPACE, "GIF output must be grayscale or RGB")
   15.62 +JMESSAGE(JERR_GIF_IMAGENOTFOUND, "Too few images in GIF file")
   15.63 +JMESSAGE(JERR_GIF_NOT, "Not a GIF file")
   15.64 +JMESSAGE(JTRC_GIF, "%ux%ux%d GIF image")
   15.65 +JMESSAGE(JTRC_GIF_BADVERSION,
   15.66 +	 "Warning: unexpected GIF version number '%c%c%c'")
   15.67 +JMESSAGE(JTRC_GIF_EXTENSION, "Ignoring GIF extension block of type 0x%02x")
   15.68 +JMESSAGE(JTRC_GIF_NONSQUARE, "Caution: nonsquare pixels in input")
   15.69 +JMESSAGE(JWRN_GIF_BADDATA, "Corrupt data in GIF file")
   15.70 +JMESSAGE(JWRN_GIF_CHAR, "Bogus char 0x%02x in GIF file, ignoring")
   15.71 +JMESSAGE(JWRN_GIF_ENDCODE, "Premature end of GIF image")
   15.72 +JMESSAGE(JWRN_GIF_NOMOREDATA, "Ran out of GIF bits")
   15.73 +#endif /* GIF_SUPPORTED */
   15.74 +
   15.75 +#ifdef PPM_SUPPORTED
   15.76 +JMESSAGE(JERR_PPM_COLORSPACE, "PPM output must be grayscale or RGB")
   15.77 +JMESSAGE(JERR_PPM_NONNUMERIC, "Nonnumeric data in PPM file")
   15.78 +JMESSAGE(JERR_PPM_NOT, "Not a PPM/PGM file")
   15.79 +JMESSAGE(JTRC_PGM, "%ux%u PGM image")
   15.80 +JMESSAGE(JTRC_PGM_TEXT, "%ux%u text PGM image")
   15.81 +JMESSAGE(JTRC_PPM, "%ux%u PPM image")
   15.82 +JMESSAGE(JTRC_PPM_TEXT, "%ux%u text PPM image")
   15.83 +#endif /* PPM_SUPPORTED */
   15.84 +
   15.85 +#ifdef RLE_SUPPORTED
   15.86 +JMESSAGE(JERR_RLE_BADERROR, "Bogus error code from RLE library")
   15.87 +JMESSAGE(JERR_RLE_COLORSPACE, "RLE output must be grayscale or RGB")
   15.88 +JMESSAGE(JERR_RLE_DIMENSIONS, "Image dimensions (%ux%u) too large for RLE")
   15.89 +JMESSAGE(JERR_RLE_EMPTY, "Empty RLE file")
   15.90 +JMESSAGE(JERR_RLE_EOF, "Premature EOF in RLE header")
   15.91 +JMESSAGE(JERR_RLE_MEM, "Insufficient memory for RLE header")
   15.92 +JMESSAGE(JERR_RLE_NOT, "Not an RLE file")
   15.93 +JMESSAGE(JERR_RLE_TOOMANYCHANNELS, "Cannot handle %d output channels for RLE")
   15.94 +JMESSAGE(JERR_RLE_UNSUPPORTED, "Cannot handle this RLE setup")
   15.95 +JMESSAGE(JTRC_RLE, "%ux%u full-color RLE file")
   15.96 +JMESSAGE(JTRC_RLE_FULLMAP, "%ux%u full-color RLE file with map of length %d")
   15.97 +JMESSAGE(JTRC_RLE_GRAY, "%ux%u grayscale RLE file")
   15.98 +JMESSAGE(JTRC_RLE_MAPGRAY, "%ux%u grayscale RLE file with map of length %d")
   15.99 +JMESSAGE(JTRC_RLE_MAPPED, "%ux%u colormapped RLE file with map of length %d")
  15.100 +#endif /* RLE_SUPPORTED */
  15.101 +
  15.102 +#ifdef TARGA_SUPPORTED
  15.103 +JMESSAGE(JERR_TGA_BADCMAP, "Unsupported Targa colormap format")
  15.104 +JMESSAGE(JERR_TGA_BADPARMS, "Invalid or unsupported Targa file")
  15.105 +JMESSAGE(JERR_TGA_COLORSPACE, "Targa output must be grayscale or RGB")
  15.106 +JMESSAGE(JTRC_TGA, "%ux%u RGB Targa image")
  15.107 +JMESSAGE(JTRC_TGA_GRAY, "%ux%u grayscale Targa image")
  15.108 +JMESSAGE(JTRC_TGA_MAPPED, "%ux%u colormapped Targa image")
  15.109 +#else
  15.110 +JMESSAGE(JERR_TGA_NOTCOMP, "Targa support was not compiled")
  15.111 +#endif /* TARGA_SUPPORTED */
  15.112 +
  15.113 +JMESSAGE(JERR_BAD_CMAP_FILE,
  15.114 +	 "Color map file is invalid or of unsupported format")
  15.115 +JMESSAGE(JERR_TOO_MANY_COLORS,
  15.116 +	 "Output file format cannot handle %d colormap entries")
  15.117 +JMESSAGE(JERR_UNGETC_FAILED, "ungetc failed")
  15.118 +#ifdef TARGA_SUPPORTED
  15.119 +JMESSAGE(JERR_UNKNOWN_FORMAT,
  15.120 +	 "Unrecognized input file format --- perhaps you need -targa")
  15.121 +#else
  15.122 +JMESSAGE(JERR_UNKNOWN_FORMAT, "Unrecognized input file format")
  15.123 +#endif
  15.124 +JMESSAGE(JERR_UNSUPPORTED_FORMAT, "Unsupported output file format")
  15.125 +
  15.126 +#ifdef JMAKE_ENUM_LIST
  15.127 +
  15.128 +  JMSG_LASTADDONCODE
  15.129 +} ADDON_MESSAGE_CODE;
  15.130 +
  15.131 +#undef JMAKE_ENUM_LIST
  15.132 +#endif /* JMAKE_ENUM_LIST */
  15.133 +
  15.134 +/* Zap JMESSAGE macro so that future re-inclusions do nothing by default */
  15.135 +#undef JMESSAGE
    16.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    16.2 +++ b/libs/libjpeg/jcapimin.c	Sat Sep 19 05:51:51 2015 +0300
    16.3 @@ -0,0 +1,280 @@
    16.4 +/*
    16.5 + * jcapimin.c
    16.6 + *
    16.7 + * Copyright (C) 1994-1998, Thomas G. Lane.
    16.8 + * This file is part of the Independent JPEG Group's software.
    16.9 + * For conditions of distribution and use, see the accompanying README file.
   16.10 + *
   16.11 + * This file contains application interface code for the compression half
   16.12 + * of the JPEG library.  These are the "minimum" API routines that may be
   16.13 + * needed in either the normal full-compression case or the transcoding-only
   16.14 + * case.
   16.15 + *
   16.16 + * Most of the routines intended to be called directly by an application
   16.17 + * are in this file or in jcapistd.c.  But also see jcparam.c for
   16.18 + * parameter-setup helper routines, jcomapi.c for routines shared by
   16.19 + * compression and decompression, and jctrans.c for the transcoding case.
   16.20 + */
   16.21 +
   16.22 +#define JPEG_INTERNALS
   16.23 +#include "jinclude.h"
   16.24 +#include "jpeglib.h"
   16.25 +
   16.26 +
   16.27 +/*
   16.28 + * Initialization of a JPEG compression object.
   16.29 + * The error manager must already be set up (in case memory manager fails).
   16.30 + */
   16.31 +
   16.32 +GLOBAL(void)
   16.33 +jpeg_CreateCompress (j_compress_ptr cinfo, int version, size_t structsize)
   16.34 +{
   16.35 +  int i;
   16.36 +
   16.37 +  /* Guard against version mismatches between library and caller. */
   16.38 +  cinfo->mem = NULL;		/* so jpeg_destroy knows mem mgr not called */
   16.39 +  if (version != JPEG_LIB_VERSION)
   16.40 +    ERREXIT2(cinfo, JERR_BAD_LIB_VERSION, JPEG_LIB_VERSION, version);
   16.41 +  if (structsize != SIZEOF(struct jpeg_compress_struct))
   16.42 +    ERREXIT2(cinfo, JERR_BAD_STRUCT_SIZE, 
   16.43 +	     (int) SIZEOF(struct jpeg_compress_struct), (int) structsize);
   16.44 +
   16.45 +  /* For debugging purposes, we zero the whole master structure.
   16.46 +   * But the application has already set the err pointer, and may have set
   16.47 +   * client_data, so we have to save and restore those fields.
   16.48 +   * Note: if application hasn't set client_data, tools like Purify may
   16.49 +   * complain here.
   16.50 +   */
   16.51 +  {
   16.52 +    struct jpeg_error_mgr * err = cinfo->err;
   16.53 +    void * client_data = cinfo->client_data; /* ignore Purify complaint here */
   16.54 +    MEMZERO(cinfo, SIZEOF(struct jpeg_compress_struct));
   16.55 +    cinfo->err = err;
   16.56 +    cinfo->client_data = client_data;
   16.57 +  }
   16.58 +  cinfo->is_decompressor = FALSE;
   16.59 +
   16.60 +  /* Initialize a memory manager instance for this object */
   16.61 +  jinit_memory_mgr((j_common_ptr) cinfo);
   16.62 +
   16.63 +  /* Zero out pointers to permanent structures. */
   16.64 +  cinfo->progress = NULL;
   16.65 +  cinfo->dest = NULL;
   16.66 +
   16.67 +  cinfo->comp_info = NULL;
   16.68 +
   16.69 +  for (i = 0; i < NUM_QUANT_TBLS; i++)
   16.70 +    cinfo->quant_tbl_ptrs[i] = NULL;
   16.71 +
   16.72 +  for (i = 0; i < NUM_HUFF_TBLS; i++) {
   16.73 +    cinfo->dc_huff_tbl_ptrs[i] = NULL;
   16.74 +    cinfo->ac_huff_tbl_ptrs[i] = NULL;
   16.75 +  }
   16.76 +
   16.77 +  cinfo->script_space = NULL;
   16.78 +
   16.79 +  cinfo->input_gamma = 1.0;	/* in case application forgets */
   16.80 +
   16.81 +  /* OK, I'm ready */
   16.82 +  cinfo->global_state = CSTATE_START;
   16.83 +}
   16.84 +
   16.85 +
   16.86 +/*
   16.87 + * Destruction of a JPEG compression object
   16.88 + */
   16.89 +
   16.90 +GLOBAL(void)
   16.91 +jpeg_destroy_compress (j_compress_ptr cinfo)
   16.92 +{
   16.93 +  jpeg_destroy((j_common_ptr) cinfo); /* use common routine */
   16.94 +}
   16.95 +
   16.96 +
   16.97 +/*
   16.98 + * Abort processing of a JPEG compression operation,
   16.99 + * but don't destroy the object itself.
  16.100 + */
  16.101 +
  16.102 +GLOBAL(void)
  16.103 +jpeg_abort_compress (j_compress_ptr cinfo)
  16.104 +{
  16.105 +  jpeg_abort((j_common_ptr) cinfo); /* use common routine */
  16.106 +}
  16.107 +
  16.108 +
  16.109 +/*
  16.110 + * Forcibly suppress or un-suppress all quantization and Huffman tables.
  16.111 + * Marks all currently defined tables as already written (if suppress)
  16.112 + * or not written (if !suppress).  This will control whether they get emitted
  16.113 + * by a subsequent jpeg_start_compress call.
  16.114 + *
  16.115 + * This routine is exported for use by applications that want to produce
  16.116 + * abbreviated JPEG datastreams.  It logically belongs in jcparam.c, but
  16.117 + * since it is called by jpeg_start_compress, we put it here --- otherwise
  16.118 + * jcparam.o would be linked whether the application used it or not.
  16.119 + */
  16.120 +
  16.121 +GLOBAL(void)
  16.122 +jpeg_suppress_tables (j_compress_ptr cinfo, boolean suppress)
  16.123 +{
  16.124 +  int i;
  16.125 +  JQUANT_TBL * qtbl;
  16.126 +  JHUFF_TBL * htbl;
  16.127 +
  16.128 +  for (i = 0; i < NUM_QUANT_TBLS; i++) {
  16.129 +    if ((qtbl = cinfo->quant_tbl_ptrs[i]) != NULL)
  16.130 +      qtbl->sent_table = suppress;
  16.131 +  }
  16.132 +
  16.133 +  for (i = 0; i < NUM_HUFF_TBLS; i++) {
  16.134 +    if ((htbl = cinfo->dc_huff_tbl_ptrs[i]) != NULL)
  16.135 +      htbl->sent_table = suppress;
  16.136 +    if ((htbl = cinfo->ac_huff_tbl_ptrs[i]) != NULL)
  16.137 +      htbl->sent_table = suppress;
  16.138 +  }
  16.139 +}
  16.140 +
  16.141 +
  16.142 +/*
  16.143 + * Finish JPEG compression.
  16.144 + *
  16.145 + * If a multipass operating mode was selected, this may do a great deal of
  16.146 + * work including most of the actual output.
  16.147 + */
  16.148 +
  16.149 +GLOBAL(void)
  16.150 +jpeg_finish_compress (j_compress_ptr cinfo)
  16.151 +{
  16.152 +  JDIMENSION iMCU_row;
  16.153 +
  16.154 +  if (cinfo->global_state == CSTATE_SCANNING ||
  16.155 +      cinfo->global_state == CSTATE_RAW_OK) {
  16.156 +    /* Terminate first pass */
  16.157 +    if (cinfo->next_scanline < cinfo->image_height)
  16.158 +      ERREXIT(cinfo, JERR_TOO_LITTLE_DATA);
  16.159 +    (*cinfo->master->finish_pass) (cinfo);
  16.160 +  } else if (cinfo->global_state != CSTATE_WRCOEFS)
  16.161 +    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  16.162 +  /* Perform any remaining passes */
  16.163 +  while (! cinfo->master->is_last_pass) {
  16.164 +    (*cinfo->master->prepare_for_pass) (cinfo);
  16.165 +    for (iMCU_row = 0; iMCU_row < cinfo->total_iMCU_rows; iMCU_row++) {
  16.166 +      if (cinfo->progress != NULL) {
  16.167 +	cinfo->progress->pass_counter = (long) iMCU_row;
  16.168 +	cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows;
  16.169 +	(*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
  16.170 +      }
  16.171 +      /* We bypass the main controller and invoke coef controller directly;
  16.172 +       * all work is being done from the coefficient buffer.
  16.173 +       */
  16.174 +      if (! (*cinfo->coef->compress_data) (cinfo, (JSAMPIMAGE) NULL))
  16.175 +	ERREXIT(cinfo, JERR_CANT_SUSPEND);
  16.176 +    }
  16.177 +    (*cinfo->master->finish_pass) (cinfo);
  16.178 +  }
  16.179 +  /* Write EOI, do final cleanup */
  16.180 +  (*cinfo->marker->write_file_trailer) (cinfo);
  16.181 +  (*cinfo->dest->term_destination) (cinfo);
  16.182 +  /* We can use jpeg_abort to release memory and reset global_state */
  16.183 +  jpeg_abort((j_common_ptr) cinfo);
  16.184 +}
  16.185 +
  16.186 +
  16.187 +/*
  16.188 + * Write a special marker.
  16.189 + * This is only recommended for writing COM or APPn markers.
  16.190 + * Must be called after jpeg_start_compress() and before
  16.191 + * first call to jpeg_write_scanlines() or jpeg_write_raw_data().
  16.192 + */
  16.193 +
  16.194 +GLOBAL(void)
  16.195 +jpeg_write_marker (j_compress_ptr cinfo, int marker,
  16.196 +		   const JOCTET *dataptr, unsigned int datalen)
  16.197 +{
  16.198 +  JMETHOD(void, write_marker_byte, (j_compress_ptr info, int val));
  16.199 +
  16.200 +  if (cinfo->next_scanline != 0 ||
  16.201 +      (cinfo->global_state != CSTATE_SCANNING &&
  16.202 +       cinfo->global_state != CSTATE_RAW_OK &&
  16.203 +       cinfo->global_state != CSTATE_WRCOEFS))
  16.204 +    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  16.205 +
  16.206 +  (*cinfo->marker->write_marker_header) (cinfo, marker, datalen);
  16.207 +  write_marker_byte = cinfo->marker->write_marker_byte;	/* copy for speed */
  16.208 +  while (datalen--) {
  16.209 +    (*write_marker_byte) (cinfo, *dataptr);
  16.210 +    dataptr++;
  16.211 +  }
  16.212 +}
  16.213 +
  16.214 +/* Same, but piecemeal. */
  16.215 +
  16.216 +GLOBAL(void)
  16.217 +jpeg_write_m_header (j_compress_ptr cinfo, int marker, unsigned int datalen)
  16.218 +{
  16.219 +  if (cinfo->next_scanline != 0 ||
  16.220 +      (cinfo->global_state != CSTATE_SCANNING &&
  16.221 +       cinfo->global_state != CSTATE_RAW_OK &&
  16.222 +       cinfo->global_state != CSTATE_WRCOEFS))
  16.223 +    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  16.224 +
  16.225 +  (*cinfo->marker->write_marker_header) (cinfo, marker, datalen);
  16.226 +}
  16.227 +
  16.228 +GLOBAL(void)
  16.229 +jpeg_write_m_byte (j_compress_ptr cinfo, int val)
  16.230 +{
  16.231 +  (*cinfo->marker->write_marker_byte) (cinfo, val);
  16.232 +}
  16.233 +
  16.234 +
  16.235 +/*
  16.236 + * Alternate compression function: just write an abbreviated table file.
  16.237 + * Before calling this, all parameters and a data destination must be set up.
  16.238 + *
  16.239 + * To produce a pair of files containing abbreviated tables and abbreviated
  16.240 + * image data, one would proceed as follows:
  16.241 + *
  16.242 + *		initialize JPEG object
  16.243 + *		set JPEG parameters
  16.244 + *		set destination to table file
  16.245 + *		jpeg_write_tables(cinfo);
  16.246 + *		set destination to image file
  16.247 + *		jpeg_start_compress(cinfo, FALSE);
  16.248 + *		write data...
  16.249 + *		jpeg_finish_compress(cinfo);
  16.250 + *
  16.251 + * jpeg_write_tables has the side effect of marking all tables written
  16.252 + * (same as jpeg_suppress_tables(..., TRUE)).  Thus a subsequent start_compress
  16.253 + * will not re-emit the tables unless it is passed write_all_tables=TRUE.
  16.254 + */
  16.255 +
  16.256 +GLOBAL(void)
  16.257 +jpeg_write_tables (j_compress_ptr cinfo)
  16.258 +{
  16.259 +  if (cinfo->global_state != CSTATE_START)
  16.260 +    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  16.261 +
  16.262 +  /* (Re)initialize error mgr and destination modules */
  16.263 +  (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);
  16.264 +  (*cinfo->dest->init_destination) (cinfo);
  16.265 +  /* Initialize the marker writer ... bit of a crock to do it here. */
  16.266 +  jinit_marker_writer(cinfo);
  16.267 +  /* Write them tables! */
  16.268 +  (*cinfo->marker->write_tables_only) (cinfo);
  16.269 +  /* And clean up. */
  16.270 +  (*cinfo->dest->term_destination) (cinfo);
  16.271 +  /*
  16.272 +   * In library releases up through v6a, we called jpeg_abort() here to free
  16.273 +   * any working memory allocated by the destination manager and marker
  16.274 +   * writer.  Some applications had a problem with that: they allocated space
  16.275 +   * of their own from the library memory manager, and didn't want it to go
  16.276 +   * away during write_tables.  So now we do nothing.  This will cause a
  16.277 +   * memory leak if an app calls write_tables repeatedly without doing a full
  16.278 +   * compression cycle or otherwise resetting the JPEG object.  However, that
  16.279 +   * seems less bad than unexpectedly freeing memory in the normal case.
  16.280 +   * An app that prefers the old behavior can call jpeg_abort for itself after
  16.281 +   * each call to jpeg_write_tables().
  16.282 +   */
  16.283 +}
    17.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    17.2 +++ b/libs/libjpeg/jcapistd.c	Sat Sep 19 05:51:51 2015 +0300
    17.3 @@ -0,0 +1,161 @@
    17.4 +/*
    17.5 + * jcapistd.c
    17.6 + *
    17.7 + * Copyright (C) 1994-1996, Thomas G. Lane.
    17.8 + * This file is part of the Independent JPEG Group's software.
    17.9 + * For conditions of distribution and use, see the accompanying README file.
   17.10 + *
   17.11 + * This file contains application interface code for the compression half
   17.12 + * of the JPEG library.  These are the "standard" API routines that are
   17.13 + * used in the normal full-compression case.  They are not used by a
   17.14 + * transcoding-only application.  Note that if an application links in
   17.15 + * jpeg_start_compress, it will end up linking in the entire compressor.
   17.16 + * We thus must separate this file from jcapimin.c to avoid linking the
   17.17 + * whole compression library into a transcoder.
   17.18 + */
   17.19 +
   17.20 +#define JPEG_INTERNALS
   17.21 +#include "jinclude.h"
   17.22 +#include "jpeglib.h"
   17.23 +
   17.24 +
   17.25 +/*
   17.26 + * Compression initialization.
   17.27 + * Before calling this, all parameters and a data destination must be set up.
   17.28 + *
   17.29 + * We require a write_all_tables parameter as a failsafe check when writing
   17.30 + * multiple datastreams from the same compression object.  Since prior runs
   17.31 + * will have left all the tables marked sent_table=TRUE, a subsequent run
   17.32 + * would emit an abbreviated stream (no tables) by default.  This may be what
   17.33 + * is wanted, but for safety's sake it should not be the default behavior:
   17.34 + * programmers should have to make a deliberate choice to emit abbreviated
   17.35 + * images.  Therefore the documentation and examples should encourage people
   17.36 + * to pass write_all_tables=TRUE; then it will take active thought to do the
   17.37 + * wrong thing.
   17.38 + */
   17.39 +
   17.40 +GLOBAL(void)
   17.41 +jpeg_start_compress (j_compress_ptr cinfo, boolean write_all_tables)
   17.42 +{
   17.43 +  if (cinfo->global_state != CSTATE_START)
   17.44 +    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
   17.45 +
   17.46 +  if (write_all_tables)
   17.47 +    jpeg_suppress_tables(cinfo, FALSE);	/* mark all tables to be written */
   17.48 +
   17.49 +  /* (Re)initialize error mgr and destination modules */
   17.50 +  (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);
   17.51 +  (*cinfo->dest->init_destination) (cinfo);
   17.52 +  /* Perform master selection of active modules */
   17.53 +  jinit_compress_master(cinfo);
   17.54 +  /* Set up for the first pass */
   17.55 +  (*cinfo->master->prepare_for_pass) (cinfo);
   17.56 +  /* Ready for application to drive first pass through jpeg_write_scanlines
   17.57 +   * or jpeg_write_raw_data.
   17.58 +   */
   17.59 +  cinfo->next_scanline = 0;
   17.60 +  cinfo->global_state = (cinfo->raw_data_in ? CSTATE_RAW_OK : CSTATE_SCANNING);
   17.61 +}
   17.62 +
   17.63 +
   17.64 +/*
   17.65 + * Write some scanlines of data to the JPEG compressor.
   17.66 + *
   17.67 + * The return value will be the number of lines actually written.
   17.68 + * This should be less than the supplied num_lines only in case that
   17.69 + * the data destination module has requested suspension of the compressor,
   17.70 + * or if more than image_height scanlines are passed in.
   17.71 + *
   17.72 + * Note: we warn about excess calls to jpeg_write_scanlines() since
   17.73 + * this likely signals an application programmer error.  However,
   17.74 + * excess scanlines passed in the last valid call are *silently* ignored,
   17.75 + * so that the application need not adjust num_lines for end-of-image
   17.76 + * when using a multiple-scanline buffer.
   17.77 + */
   17.78 +
   17.79 +GLOBAL(JDIMENSION)
   17.80 +jpeg_write_scanlines (j_compress_ptr cinfo, JSAMPARRAY scanlines,
   17.81 +		      JDIMENSION num_lines)
   17.82 +{
   17.83 +  JDIMENSION row_ctr, rows_left;
   17.84 +
   17.85 +  if (cinfo->global_state != CSTATE_SCANNING)
   17.86 +    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
   17.87 +  if (cinfo->next_scanline >= cinfo->image_height)
   17.88 +    WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
   17.89 +
   17.90 +  /* Call progress monitor hook if present */
   17.91 +  if (cinfo->progress != NULL) {
   17.92 +    cinfo->progress->pass_counter = (long) cinfo->next_scanline;
   17.93 +    cinfo->progress->pass_limit = (long) cinfo->image_height;
   17.94 +    (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
   17.95 +  }
   17.96 +
   17.97 +  /* Give master control module another chance if this is first call to
   17.98 +   * jpeg_write_scanlines.  This lets output of the frame/scan headers be
   17.99 +   * delayed so that application can write COM, etc, markers between
  17.100 +   * jpeg_start_compress and jpeg_write_scanlines.
  17.101 +   */
  17.102 +  if (cinfo->master->call_pass_startup)
  17.103 +    (*cinfo->master->pass_startup) (cinfo);
  17.104 +
  17.105 +  /* Ignore any extra scanlines at bottom of image. */
  17.106 +  rows_left = cinfo->image_height - cinfo->next_scanline;
  17.107 +  if (num_lines > rows_left)
  17.108 +    num_lines = rows_left;
  17.109 +
  17.110 +  row_ctr = 0;
  17.111 +  (*cinfo->main->process_data) (cinfo, scanlines, &row_ctr, num_lines);
  17.112 +  cinfo->next_scanline += row_ctr;
  17.113 +  return row_ctr;
  17.114 +}
  17.115 +
  17.116 +
  17.117 +/*
  17.118 + * Alternate entry point to write raw data.
  17.119 + * Processes exactly one iMCU row per call, unless suspended.
  17.120 + */
  17.121 +
  17.122 +GLOBAL(JDIMENSION)
  17.123 +jpeg_write_raw_data (j_compress_ptr cinfo, JSAMPIMAGE data,
  17.124 +		     JDIMENSION num_lines)
  17.125 +{
  17.126 +  JDIMENSION lines_per_iMCU_row;
  17.127 +
  17.128 +  if (cinfo->global_state != CSTATE_RAW_OK)
  17.129 +    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  17.130 +  if (cinfo->next_scanline >= cinfo->image_height) {
  17.131 +    WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
  17.132 +    return 0;
  17.133 +  }
  17.134 +
  17.135 +  /* Call progress monitor hook if present */
  17.136 +  if (cinfo->progress != NULL) {
  17.137 +    cinfo->progress->pass_counter = (long) cinfo->next_scanline;
  17.138 +    cinfo->progress->pass_limit = (long) cinfo->image_height;
  17.139 +    (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
  17.140 +  }
  17.141 +
  17.142 +  /* Give master control module another chance if this is first call to
  17.143 +   * jpeg_write_raw_data.  This lets output of the frame/scan headers be
  17.144 +   * delayed so that application can write COM, etc, markers between
  17.145 +   * jpeg_start_compress and jpeg_write_raw_data.
  17.146 +   */
  17.147 +  if (cinfo->master->call_pass_startup)
  17.148 +    (*cinfo->master->pass_startup) (cinfo);
  17.149 +
  17.150 +  /* Verify that at least one iMCU row has been passed. */
  17.151 +  lines_per_iMCU_row = cinfo->max_v_samp_factor * DCTSIZE;
  17.152 +  if (num_lines < lines_per_iMCU_row)
  17.153 +    ERREXIT(cinfo, JERR_BUFFER_SIZE);
  17.154 +
  17.155 +  /* Directly compress the row. */
  17.156 +  if (! (*cinfo->coef->compress_data) (cinfo, data)) {
  17.157 +    /* If compressor did not consume the whole row, suspend processing. */
  17.158 +    return 0;
  17.159 +  }
  17.160 +
  17.161 +  /* OK, we processed one iMCU row. */
  17.162 +  cinfo->next_scanline += lines_per_iMCU_row;
  17.163 +  return lines_per_iMCU_row;
  17.164 +}
    18.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    18.2 +++ b/libs/libjpeg/jccoefct.c	Sat Sep 19 05:51:51 2015 +0300
    18.3 @@ -0,0 +1,449 @@
    18.4 +/*
    18.5 + * jccoefct.c
    18.6 + *
    18.7 + * Copyright (C) 1994-1997, Thomas G. Lane.
    18.8 + * This file is part of the Independent JPEG Group's software.
    18.9 + * For conditions of distribution and use, see the accompanying README file.
   18.10 + *
   18.11 + * This file contains the coefficient buffer controller for compression.
   18.12 + * This controller is the top level of the JPEG compressor proper.
   18.13 + * The coefficient buffer lies between forward-DCT and entropy encoding steps.
   18.14 + */
   18.15 +
   18.16 +#define JPEG_INTERNALS
   18.17 +#include "jinclude.h"
   18.18 +#include "jpeglib.h"
   18.19 +
   18.20 +
   18.21 +/* We use a full-image coefficient buffer when doing Huffman optimization,
   18.22 + * and also for writing multiple-scan JPEG files.  In all cases, the DCT
   18.23 + * step is run during the first pass, and subsequent passes need only read
   18.24 + * the buffered coefficients.
   18.25 + */
   18.26 +#ifdef ENTROPY_OPT_SUPPORTED
   18.27 +#define FULL_COEF_BUFFER_SUPPORTED
   18.28 +#else
   18.29 +#ifdef C_MULTISCAN_FILES_SUPPORTED
   18.30 +#define FULL_COEF_BUFFER_SUPPORTED
   18.31 +#endif
   18.32 +#endif
   18.33 +
   18.34 +
   18.35 +/* Private buffer controller object */
   18.36 +
   18.37 +typedef struct {
   18.38 +  struct jpeg_c_coef_controller pub; /* public fields */
   18.39 +
   18.40 +  JDIMENSION iMCU_row_num;	/* iMCU row # within image */
   18.41 +  JDIMENSION mcu_ctr;		/* counts MCUs processed in current row */
   18.42 +  int MCU_vert_offset;		/* counts MCU rows within iMCU row */
   18.43 +  int MCU_rows_per_iMCU_row;	/* number of such rows needed */
   18.44 +
   18.45 +  /* For single-pass compression, it's sufficient to buffer just one MCU
   18.46 +   * (although this may prove a bit slow in practice).  We allocate a
   18.47 +   * workspace of C_MAX_BLOCKS_IN_MCU coefficient blocks, and reuse it for each
   18.48 +   * MCU constructed and sent.  (On 80x86, the workspace is FAR even though
   18.49 +   * it's not really very big; this is to keep the module interfaces unchanged
   18.50 +   * when a large coefficient buffer is necessary.)
   18.51 +   * In multi-pass modes, this array points to the current MCU's blocks
   18.52 +   * within the virtual arrays.
   18.53 +   */
   18.54 +  JBLOCKROW MCU_buffer[C_MAX_BLOCKS_IN_MCU];
   18.55 +
   18.56 +  /* In multi-pass modes, we need a virtual block array for each component. */
   18.57 +  jvirt_barray_ptr whole_image[MAX_COMPONENTS];
   18.58 +} my_coef_controller;
   18.59 +
   18.60 +typedef my_coef_controller * my_coef_ptr;
   18.61 +
   18.62 +
   18.63 +/* Forward declarations */
   18.64 +METHODDEF(boolean) compress_data
   18.65 +    JPP((j_compress_ptr cinfo, JSAMPIMAGE input_buf));
   18.66 +#ifdef FULL_COEF_BUFFER_SUPPORTED
   18.67 +METHODDEF(boolean) compress_first_pass
   18.68 +    JPP((j_compress_ptr cinfo, JSAMPIMAGE input_buf));
   18.69 +METHODDEF(boolean) compress_output
   18.70 +    JPP((j_compress_ptr cinfo, JSAMPIMAGE input_buf));
   18.71 +#endif
   18.72 +
   18.73 +
   18.74 +LOCAL(void)
   18.75 +start_iMCU_row (j_compress_ptr cinfo)
   18.76 +/* Reset within-iMCU-row counters for a new row */
   18.77 +{
   18.78 +  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
   18.79 +
   18.80 +  /* In an interleaved scan, an MCU row is the same as an iMCU row.
   18.81 +   * In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows.
   18.82 +   * But at the bottom of the image, process only what's left.
   18.83 +   */
   18.84 +  if (cinfo->comps_in_scan > 1) {
   18.85 +    coef->MCU_rows_per_iMCU_row = 1;
   18.86 +  } else {
   18.87 +    if (coef->iMCU_row_num < (cinfo->total_iMCU_rows-1))
   18.88 +      coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor;
   18.89 +    else
   18.90 +      coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height;
   18.91 +  }
   18.92 +
   18.93 +  coef->mcu_ctr = 0;
   18.94 +  coef->MCU_vert_offset = 0;
   18.95 +}
   18.96 +
   18.97 +
   18.98 +/*
   18.99 + * Initialize for a processing pass.
  18.100 + */
  18.101 +
  18.102 +METHODDEF(void)
  18.103 +start_pass_coef (j_compress_ptr cinfo, J_BUF_MODE pass_mode)
  18.104 +{
  18.105 +  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
  18.106 +
  18.107 +  coef->iMCU_row_num = 0;
  18.108 +  start_iMCU_row(cinfo);
  18.109 +
  18.110 +  switch (pass_mode) {
  18.111 +  case JBUF_PASS_THRU:
  18.112 +    if (coef->whole_image[0] != NULL)
  18.113 +      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  18.114 +    coef->pub.compress_data = compress_data;
  18.115 +    break;
  18.116 +#ifdef FULL_COEF_BUFFER_SUPPORTED
  18.117 +  case JBUF_SAVE_AND_PASS:
  18.118 +    if (coef->whole_image[0] == NULL)
  18.119 +      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  18.120 +    coef->pub.compress_data = compress_first_pass;
  18.121 +    break;
  18.122 +  case JBUF_CRANK_DEST:
  18.123 +    if (coef->whole_image[0] == NULL)
  18.124 +      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  18.125 +    coef->pub.compress_data = compress_output;
  18.126 +    break;
  18.127 +#endif
  18.128 +  default:
  18.129 +    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  18.130 +    break;
  18.131 +  }
  18.132 +}
  18.133 +
  18.134 +
  18.135 +/*
  18.136 + * Process some data in the single-pass case.
  18.137 + * We process the equivalent of one fully interleaved MCU row ("iMCU" row)
  18.138 + * per call, ie, v_samp_factor block rows for each component in the image.
  18.139 + * Returns TRUE if the iMCU row is completed, FALSE if suspended.
  18.140 + *
  18.141 + * NB: input_buf contains a plane for each component in image,
  18.142 + * which we index according to the component's SOF position.
  18.143 + */
  18.144 +
  18.145 +METHODDEF(boolean)
  18.146 +compress_data (j_compress_ptr cinfo, JSAMPIMAGE input_buf)
  18.147 +{
  18.148 +  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
  18.149 +  JDIMENSION MCU_col_num;	/* index of current MCU within row */
  18.150 +  JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1;
  18.151 +  JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
  18.152 +  int blkn, bi, ci, yindex, yoffset, blockcnt;
  18.153 +  JDIMENSION ypos, xpos;
  18.154 +  jpeg_component_info *compptr;
  18.155 +
  18.156 +  /* Loop to write as much as one whole iMCU row */
  18.157 +  for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
  18.158 +       yoffset++) {
  18.159 +    for (MCU_col_num = coef->mcu_ctr; MCU_col_num <= last_MCU_col;
  18.160 +	 MCU_col_num++) {
  18.161 +      /* Determine where data comes from in input_buf and do the DCT thing.
  18.162 +       * Each call on forward_DCT processes a horizontal row of DCT blocks
  18.163 +       * as wide as an MCU; we rely on having allocated the MCU_buffer[] blocks
  18.164 +       * sequentially.  Dummy blocks at the right or bottom edge are filled in
  18.165 +       * specially.  The data in them does not matter for image reconstruction,
  18.166 +       * so we fill them with values that will encode to the smallest amount of
  18.167 +       * data, viz: all zeroes in the AC entries, DC entries equal to previous
  18.168 +       * block's DC value.  (Thanks to Thomas Kinsman for this idea.)
  18.169 +       */
  18.170 +      blkn = 0;
  18.171 +      for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  18.172 +	compptr = cinfo->cur_comp_info[ci];
  18.173 +	blockcnt = (MCU_col_num < last_MCU_col) ? compptr->MCU_width
  18.174 +						: compptr->last_col_width;
  18.175 +	xpos = MCU_col_num * compptr->MCU_sample_width;
  18.176 +	ypos = yoffset * DCTSIZE; /* ypos == (yoffset+yindex) * DCTSIZE */
  18.177 +	for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
  18.178 +	  if (coef->iMCU_row_num < last_iMCU_row ||
  18.179 +	      yoffset+yindex < compptr->last_row_height) {
  18.180 +	    (*cinfo->fdct->forward_DCT) (cinfo, compptr,
  18.181 +					 input_buf[compptr->component_index],
  18.182 +					 coef->MCU_buffer[blkn],
  18.183 +					 ypos, xpos, (JDIMENSION) blockcnt);
  18.184 +	    if (blockcnt < compptr->MCU_width) {
  18.185 +	      /* Create some dummy blocks at the right edge of the image. */
  18.186 +	      jzero_far((void FAR *) coef->MCU_buffer[blkn + blockcnt],
  18.187 +			(compptr->MCU_width - blockcnt) * SIZEOF(JBLOCK));
  18.188 +	      for (bi = blockcnt; bi < compptr->MCU_width; bi++) {
  18.189 +		coef->MCU_buffer[blkn+bi][0][0] = coef->MCU_buffer[blkn+bi-1][0][0];
  18.190 +	      }
  18.191 +	    }
  18.192 +	  } else {
  18.193 +	    /* Create a row of dummy blocks at the bottom of the image. */
  18.194 +	    jzero_far((void FAR *) coef->MCU_buffer[blkn],
  18.195 +		      compptr->MCU_width * SIZEOF(JBLOCK));
  18.196 +	    for (bi = 0; bi < compptr->MCU_width; bi++) {
  18.197 +	      coef->MCU_buffer[blkn+bi][0][0] = coef->MCU_buffer[blkn-1][0][0];
  18.198 +	    }
  18.199 +	  }
  18.200 +	  blkn += compptr->MCU_width;
  18.201 +	  ypos += DCTSIZE;
  18.202 +	}
  18.203 +      }
  18.204 +      /* Try to write the MCU.  In event of a suspension failure, we will
  18.205 +       * re-DCT the MCU on restart (a bit inefficient, could be fixed...)
  18.206 +       */
  18.207 +      if (! (*cinfo->entropy->encode_mcu) (cinfo, coef->MCU_buffer)) {
  18.208 +	/* Suspension forced; update state counters and exit */
  18.209 +	coef->MCU_vert_offset = yoffset;
  18.210 +	coef->mcu_ctr = MCU_col_num;
  18.211 +	return FALSE;
  18.212 +      }
  18.213 +    }
  18.214 +    /* Completed an MCU row, but perhaps not an iMCU row */
  18.215 +    coef->mcu_ctr = 0;
  18.216 +  }
  18.217 +  /* Completed the iMCU row, advance counters for next one */
  18.218 +  coef->iMCU_row_num++;
  18.219 +  start_iMCU_row(cinfo);
  18.220 +  return TRUE;
  18.221 +}
  18.222 +
  18.223 +
  18.224 +#ifdef FULL_COEF_BUFFER_SUPPORTED
  18.225 +
  18.226 +/*
  18.227 + * Process some data in the first pass of a multi-pass case.
  18.228 + * We process the equivalent of one fully interleaved MCU row ("iMCU" row)
  18.229 + * per call, ie, v_samp_factor block rows for each component in the image.
  18.230 + * This amount of data is read from the source buffer, DCT'd and quantized,
  18.231 + * and saved into the virtual arrays.  We also generate suitable dummy blocks
  18.232 + * as needed at the right and lower edges.  (The dummy blocks are constructed
  18.233 + * in the virtual arrays, which have been padded appropriately.)  This makes
  18.234 + * it possible for subsequent passes not to worry about real vs. dummy blocks.
  18.235 + *
  18.236 + * We must also emit the data to the entropy encoder.  This is conveniently
  18.237 + * done by calling compress_output() after we've loaded the current strip
  18.238 + * of the virtual arrays.
  18.239 + *
  18.240 + * NB: input_buf contains a plane for each component in image.  All
  18.241 + * components are DCT'd and loaded into the virtual arrays in this pass.
  18.242 + * However, it may be that only a subset of the components are emitted to
  18.243 + * the entropy encoder during this first pass; be careful about looking
  18.244 + * at the scan-dependent variables (MCU dimensions, etc).
  18.245 + */
  18.246 +
  18.247 +METHODDEF(boolean)
  18.248 +compress_first_pass (j_compress_ptr cinfo, JSAMPIMAGE input_buf)
  18.249 +{
  18.250 +  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
  18.251 +  JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
  18.252 +  JDIMENSION blocks_across, MCUs_across, MCUindex;
  18.253 +  int bi, ci, h_samp_factor, block_row, block_rows, ndummy;
  18.254 +  JCOEF lastDC;
  18.255 +  jpeg_component_info *compptr;
  18.256 +  JBLOCKARRAY buffer;
  18.257 +  JBLOCKROW thisblockrow, lastblockrow;
  18.258 +
  18.259 +  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  18.260 +       ci++, compptr++) {
  18.261 +    /* Align the virtual buffer for this component. */
  18.262 +    buffer = (*cinfo->mem->access_virt_barray)
  18.263 +      ((j_common_ptr) cinfo, coef->whole_image[ci],
  18.264 +       coef->iMCU_row_num * compptr->v_samp_factor,
  18.265 +       (JDIMENSION) compptr->v_samp_factor, TRUE);
  18.266 +    /* Count non-dummy DCT block rows in this iMCU row. */
  18.267 +    if (coef->iMCU_row_num < last_iMCU_row)
  18.268 +      block_rows = compptr->v_samp_factor;
  18.269 +    else {
  18.270 +      /* NB: can't use last_row_height here, since may not be set! */
  18.271 +      block_rows = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
  18.272 +      if (block_rows == 0) block_rows = compptr->v_samp_factor;
  18.273 +    }
  18.274 +    blocks_across = compptr->width_in_blocks;
  18.275 +    h_samp_factor = compptr->h_samp_factor;
  18.276 +    /* Count number of dummy blocks to be added at the right margin. */
  18.277 +    ndummy = (int) (blocks_across % h_samp_factor);
  18.278 +    if (ndummy > 0)
  18.279 +      ndummy = h_samp_factor - ndummy;
  18.280 +    /* Perform DCT for all non-dummy blocks in this iMCU row.  Each call
  18.281 +     * on forward_DCT processes a complete horizontal row of DCT blocks.
  18.282 +     */
  18.283 +    for (block_row = 0; block_row < block_rows; block_row++) {
  18.284 +      thisblockrow = buffer[block_row];
  18.285 +      (*cinfo->fdct->forward_DCT) (cinfo, compptr,
  18.286 +				   input_buf[ci], thisblockrow,
  18.287 +				   (JDIMENSION) (block_row * DCTSIZE),
  18.288 +				   (JDIMENSION) 0, blocks_across);
  18.289 +      if (ndummy > 0) {
  18.290 +	/* Create dummy blocks at the right edge of the image. */
  18.291 +	thisblockrow += blocks_across; /* => first dummy block */
  18.292 +	jzero_far((void FAR *) thisblockrow, ndummy * SIZEOF(JBLOCK));
  18.293 +	lastDC = thisblockrow[-1][0];
  18.294 +	for (bi = 0; bi < ndummy; bi++) {
  18.295 +	  thisblockrow[bi][0] = lastDC;
  18.296 +	}
  18.297 +      }
  18.298 +    }
  18.299 +    /* If at end of image, create dummy block rows as needed.
  18.300 +     * The tricky part here is that within each MCU, we want the DC values
  18.301 +     * of the dummy blocks to match the last real block's DC value.
  18.302 +     * This squeezes a few more bytes out of the resulting file...
  18.303 +     */
  18.304 +    if (coef->iMCU_row_num == last_iMCU_row) {
  18.305 +      blocks_across += ndummy;	/* include lower right corner */
  18.306 +      MCUs_across = blocks_across / h_samp_factor;
  18.307 +      for (block_row = block_rows; block_row < compptr->v_samp_factor;
  18.308 +	   block_row++) {
  18.309 +	thisblockrow = buffer[block_row];
  18.310 +	lastblockrow = buffer[block_row-1];
  18.311 +	jzero_far((void FAR *) thisblockrow,
  18.312 +		  (size_t) (blocks_across * SIZEOF(JBLOCK)));
  18.313 +	for (MCUindex = 0; MCUindex < MCUs_across; MCUindex++) {
  18.314 +	  lastDC = lastblockrow[h_samp_factor-1][0];
  18.315 +	  for (bi = 0; bi < h_samp_factor; bi++) {
  18.316 +	    thisblockrow[bi][0] = lastDC;
  18.317 +	  }
  18.318 +	  thisblockrow += h_samp_factor; /* advance to next MCU in row */
  18.319 +	  lastblockrow += h_samp_factor;
  18.320 +	}
  18.321 +      }
  18.322 +    }
  18.323 +  }
  18.324 +  /* NB: compress_output will increment iMCU_row_num if successful.
  18.325 +   * A suspension return will result in redoing all the work above next time.
  18.326 +   */
  18.327 +
  18.328 +  /* Emit data to the entropy encoder, sharing code with subsequent passes */
  18.329 +  return compress_output(cinfo, input_buf);
  18.330 +}
  18.331 +
  18.332 +
  18.333 +/*
  18.334 + * Process some data in subsequent passes of a multi-pass case.
  18.335 + * We process the equivalent of one fully interleaved MCU row ("iMCU" row)
  18.336 + * per call, ie, v_samp_factor block rows for each component in the scan.
  18.337 + * The data is obtained from the virtual arrays and fed to the entropy coder.
  18.338 + * Returns TRUE if the iMCU row is completed, FALSE if suspended.
  18.339 + *
  18.340 + * NB: input_buf is ignored; it is likely to be a NULL pointer.
  18.341 + */
  18.342 +
  18.343 +METHODDEF(boolean)
  18.344 +compress_output (j_compress_ptr cinfo, JSAMPIMAGE input_buf)
  18.345 +{
  18.346 +  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
  18.347 +  JDIMENSION MCU_col_num;	/* index of current MCU within row */
  18.348 +  int blkn, ci, xindex, yindex, yoffset;
  18.349 +  JDIMENSION start_col;
  18.350 +  JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];
  18.351 +  JBLOCKROW buffer_ptr;
  18.352 +  jpeg_component_info *compptr;
  18.353 +
  18.354 +  /* Align the virtual buffers for the components used in this scan.
  18.355 +   * NB: during first pass, this is safe only because the buffers will
  18.356 +   * already be aligned properly, so jmemmgr.c won't need to do any I/O.
  18.357 +   */
  18.358 +  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  18.359 +    compptr = cinfo->cur_comp_info[ci];
  18.360 +    buffer[ci] = (*cinfo->mem->access_virt_barray)
  18.361 +      ((j_common_ptr) cinfo, coef->whole_image[compptr->component_index],
  18.362 +       coef->iMCU_row_num * compptr->v_samp_factor,
  18.363 +       (JDIMENSION) compptr->v_samp_factor, FALSE);
  18.364 +  }
  18.365 +
  18.366 +  /* Loop to process one whole iMCU row */
  18.367 +  for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
  18.368 +       yoffset++) {
  18.369 +    for (MCU_col_num = coef->mcu_ctr; MCU_col_num < cinfo->MCUs_per_row;
  18.370 +	 MCU_col_num++) {
  18.371 +      /* Construct list of pointers to DCT blocks belonging to this MCU */
  18.372 +      blkn = 0;			/* index of current DCT block within MCU */
  18.373 +      for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  18.374 +	compptr = cinfo->cur_comp_info[ci];
  18.375 +	start_col = MCU_col_num * compptr->MCU_width;
  18.376 +	for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
  18.377 +	  buffer_ptr = buffer[ci][yindex+yoffset] + start_col;
  18.378 +	  for (xindex = 0; xindex < compptr->MCU_width; xindex++) {
  18.379 +	    coef->MCU_buffer[blkn++] = buffer_ptr++;
  18.380 +	  }
  18.381 +	}
  18.382 +      }
  18.383 +      /* Try to write the MCU. */
  18.384 +      if (! (*cinfo->entropy->encode_mcu) (cinfo, coef->MCU_buffer)) {
  18.385 +	/* Suspension forced; update state counters and exit */
  18.386 +	coef->MCU_vert_offset = yoffset;
  18.387 +	coef->mcu_ctr = MCU_col_num;
  18.388 +	return FALSE;
  18.389 +      }
  18.390 +    }
  18.391 +    /* Completed an MCU row, but perhaps not an iMCU row */
  18.392 +    coef->mcu_ctr = 0;
  18.393 +  }
  18.394 +  /* Completed the iMCU row, advance counters for next one */
  18.395 +  coef->iMCU_row_num++;
  18.396 +  start_iMCU_row(cinfo);
  18.397 +  return TRUE;
  18.398 +}
  18.399 +
  18.400 +#endif /* FULL_COEF_BUFFER_SUPPORTED */
  18.401 +
  18.402 +
  18.403 +/*
  18.404 + * Initialize coefficient buffer controller.
  18.405 + */
  18.406 +
  18.407 +GLOBAL(void)
  18.408 +jinit_c_coef_controller (j_compress_ptr cinfo, boolean need_full_buffer)
  18.409 +{
  18.410 +  my_coef_ptr coef;
  18.411 +
  18.412 +  coef = (my_coef_ptr)
  18.413 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  18.414 +				SIZEOF(my_coef_controller));
  18.415 +  cinfo->coef = (struct jpeg_c_coef_controller *) coef;
  18.416 +  coef->pub.start_pass = start_pass_coef;
  18.417 +
  18.418 +  /* Create the coefficient buffer. */
  18.419 +  if (need_full_buffer) {
  18.420 +#ifdef FULL_COEF_BUFFER_SUPPORTED
  18.421 +    /* Allocate a full-image virtual array for each component, */
  18.422 +    /* padded to a multiple of samp_factor DCT blocks in each direction. */
  18.423 +    int ci;
  18.424 +    jpeg_component_info *compptr;
  18.425 +
  18.426 +    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  18.427 +	 ci++, compptr++) {
  18.428 +      coef->whole_image[ci] = (*cinfo->mem->request_virt_barray)
  18.429 +	((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE,
  18.430 +	 (JDIMENSION) jround_up((long) compptr->width_in_blocks,
  18.431 +				(long) compptr->h_samp_factor),
  18.432 +	 (JDIMENSION) jround_up((long) compptr->height_in_blocks,
  18.433 +				(long) compptr->v_samp_factor),
  18.434 +	 (JDIMENSION) compptr->v_samp_factor);
  18.435 +    }
  18.436 +#else
  18.437 +    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  18.438 +#endif
  18.439 +  } else {
  18.440 +    /* We only need a single-MCU buffer. */
  18.441 +    JBLOCKROW buffer;
  18.442 +    int i;
  18.443 +
  18.444 +    buffer = (JBLOCKROW)
  18.445 +      (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  18.446 +				  C_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK));
  18.447 +    for (i = 0; i < C_MAX_BLOCKS_IN_MCU; i++) {
  18.448 +      coef->MCU_buffer[i] = buffer + i;
  18.449 +    }
  18.450 +    coef->whole_image[0] = NULL; /* flag for no virtual arrays */
  18.451 +  }
  18.452 +}
    19.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    19.2 +++ b/libs/libjpeg/jccolor.c	Sat Sep 19 05:51:51 2015 +0300
    19.3 @@ -0,0 +1,459 @@
    19.4 +/*
    19.5 + * jccolor.c
    19.6 + *
    19.7 + * Copyright (C) 1991-1996, Thomas G. Lane.
    19.8 + * This file is part of the Independent JPEG Group's software.
    19.9 + * For conditions of distribution and use, see the accompanying README file.
   19.10 + *
   19.11 + * This file contains input colorspace conversion routines.
   19.12 + */
   19.13 +
   19.14 +#define JPEG_INTERNALS
   19.15 +#include "jinclude.h"
   19.16 +#include "jpeglib.h"
   19.17 +
   19.18 +
   19.19 +/* Private subobject */
   19.20 +
   19.21 +typedef struct {
   19.22 +  struct jpeg_color_converter pub; /* public fields */
   19.23 +
   19.24 +  /* Private state for RGB->YCC conversion */
   19.25 +  INT32 * rgb_ycc_tab;		/* => table for RGB to YCbCr conversion */
   19.26 +} my_color_converter;
   19.27 +
   19.28 +typedef my_color_converter * my_cconvert_ptr;
   19.29 +
   19.30 +
   19.31 +/**************** RGB -> YCbCr conversion: most common case **************/
   19.32 +
   19.33 +/*
   19.34 + * YCbCr is defined per CCIR 601-1, except that Cb and Cr are
   19.35 + * normalized to the range 0..MAXJSAMPLE rather than -0.5 .. 0.5.
   19.36 + * The conversion equations to be implemented are therefore
   19.37 + *	Y  =  0.29900 * R + 0.58700 * G + 0.11400 * B
   19.38 + *	Cb = -0.16874 * R - 0.33126 * G + 0.50000 * B  + CENTERJSAMPLE
   19.39 + *	Cr =  0.50000 * R - 0.41869 * G - 0.08131 * B  + CENTERJSAMPLE
   19.40 + * (These numbers are derived from TIFF 6.0 section 21, dated 3-June-92.)
   19.41 + * Note: older versions of the IJG code used a zero offset of MAXJSAMPLE/2,
   19.42 + * rather than CENTERJSAMPLE, for Cb and Cr.  This gave equal positive and
   19.43 + * negative swings for Cb/Cr, but meant that grayscale values (Cb=Cr=0)
   19.44 + * were not represented exactly.  Now we sacrifice exact representation of
   19.45 + * maximum red and maximum blue in order to get exact grayscales.
   19.46 + *
   19.47 + * To avoid floating-point arithmetic, we represent the fractional constants
   19.48 + * as integers scaled up by 2^16 (about 4 digits precision); we have to divide
   19.49 + * the products by 2^16, with appropriate rounding, to get the correct answer.
   19.50 + *
   19.51 + * For even more speed, we avoid doing any multiplications in the inner loop
   19.52 + * by precalculating the constants times R,G,B for all possible values.
   19.53 + * For 8-bit JSAMPLEs this is very reasonable (only 256 entries per table);
   19.54 + * for 12-bit samples it is still acceptable.  It's not very reasonable for
   19.55 + * 16-bit samples, but if you want lossless storage you shouldn't be changing
   19.56 + * colorspace anyway.
   19.57 + * The CENTERJSAMPLE offsets and the rounding fudge-factor of 0.5 are included
   19.58 + * in the tables to save adding them separately in the inner loop.
   19.59 + */
   19.60 +
   19.61 +#define SCALEBITS	16	/* speediest right-shift on some machines */
   19.62 +#define CBCR_OFFSET	((INT32) CENTERJSAMPLE << SCALEBITS)
   19.63 +#define ONE_HALF	((INT32) 1 << (SCALEBITS-1))
   19.64 +#define FIX(x)		((INT32) ((x) * (1L<<SCALEBITS) + 0.5))
   19.65 +
   19.66 +/* We allocate one big table and divide it up into eight parts, instead of
   19.67 + * doing eight alloc_small requests.  This lets us use a single table base
   19.68 + * address, which can be held in a register in the inner loops on many
   19.69 + * machines (more than can hold all eight addresses, anyway).
   19.70 + */
   19.71 +
   19.72 +#define R_Y_OFF		0			/* offset to R => Y section */
   19.73 +#define G_Y_OFF		(1*(MAXJSAMPLE+1))	/* offset to G => Y section */
   19.74 +#define B_Y_OFF		(2*(MAXJSAMPLE+1))	/* etc. */
   19.75 +#define R_CB_OFF	(3*(MAXJSAMPLE+1))
   19.76 +#define G_CB_OFF	(4*(MAXJSAMPLE+1))
   19.77 +#define B_CB_OFF	(5*(MAXJSAMPLE+1))
   19.78 +#define R_CR_OFF	B_CB_OFF		/* B=>Cb, R=>Cr are the same */
   19.79 +#define G_CR_OFF	(6*(MAXJSAMPLE+1))
   19.80 +#define B_CR_OFF	(7*(MAXJSAMPLE+1))
   19.81 +#define TABLE_SIZE	(8*(MAXJSAMPLE+1))
   19.82 +
   19.83 +
   19.84 +/*
   19.85 + * Initialize for RGB->YCC colorspace conversion.
   19.86 + */
   19.87 +
   19.88 +METHODDEF(void)
   19.89 +rgb_ycc_start (j_compress_ptr cinfo)
   19.90 +{
   19.91 +  my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
   19.92 +  INT32 * rgb_ycc_tab;
   19.93 +  INT32 i;
   19.94 +
   19.95 +  /* Allocate and fill in the conversion tables. */
   19.96 +  cconvert->rgb_ycc_tab = rgb_ycc_tab = (INT32 *)
   19.97 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
   19.98 +				(TABLE_SIZE * SIZEOF(INT32)));
   19.99 +
  19.100 +  for (i = 0; i <= MAXJSAMPLE; i++) {
  19.101 +    rgb_ycc_tab[i+R_Y_OFF] = FIX(0.29900) * i;
  19.102 +    rgb_ycc_tab[i+G_Y_OFF] = FIX(0.58700) * i;
  19.103 +    rgb_ycc_tab[i+B_Y_OFF] = FIX(0.11400) * i     + ONE_HALF;
  19.104 +    rgb_ycc_tab[i+R_CB_OFF] = (-FIX(0.16874)) * i;
  19.105 +    rgb_ycc_tab[i+G_CB_OFF] = (-FIX(0.33126)) * i;
  19.106 +    /* We use a rounding fudge-factor of 0.5-epsilon for Cb and Cr.
  19.107 +     * This ensures that the maximum output will round to MAXJSAMPLE
  19.108 +     * not MAXJSAMPLE+1, and thus that we don't have to range-limit.
  19.109 +     */
  19.110 +    rgb_ycc_tab[i+B_CB_OFF] = FIX(0.50000) * i    + CBCR_OFFSET + ONE_HALF-1;
  19.111 +/*  B=>Cb and R=>Cr tables are the same
  19.112 +    rgb_ycc_tab[i+R_CR_OFF] = FIX(0.50000) * i    + CBCR_OFFSET + ONE_HALF-1;
  19.113 +*/
  19.114 +    rgb_ycc_tab[i+G_CR_OFF] = (-FIX(0.41869)) * i;
  19.115 +    rgb_ycc_tab[i+B_CR_OFF] = (-FIX(0.08131)) * i;
  19.116 +  }
  19.117 +}
  19.118 +
  19.119 +
  19.120 +/*
  19.121 + * Convert some rows of samples to the JPEG colorspace.
  19.122 + *
  19.123 + * Note that we change from the application's interleaved-pixel format
  19.124 + * to our internal noninterleaved, one-plane-per-component format.
  19.125 + * The input buffer is therefore three times as wide as the output buffer.
  19.126 + *
  19.127 + * A starting row offset is provided only for the output buffer.  The caller
  19.128 + * can easily adjust the passed input_buf value to accommodate any row
  19.129 + * offset required on that side.
  19.130 + */
  19.131 +
  19.132 +METHODDEF(void)
  19.133 +rgb_ycc_convert (j_compress_ptr cinfo,
  19.134 +		 JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
  19.135 +		 JDIMENSION output_row, int num_rows)
  19.136 +{
  19.137 +  my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
  19.138 +  register int r, g, b;
  19.139 +  register INT32 * ctab = cconvert->rgb_ycc_tab;
  19.140 +  register JSAMPROW inptr;
  19.141 +  register JSAMPROW outptr0, outptr1, outptr2;
  19.142 +  register JDIMENSION col;
  19.143 +  JDIMENSION num_cols = cinfo->image_width;
  19.144 +
  19.145 +  while (--num_rows >= 0) {
  19.146 +    inptr = *input_buf++;
  19.147 +    outptr0 = output_buf[0][output_row];
  19.148 +    outptr1 = output_buf[1][output_row];
  19.149 +    outptr2 = output_buf[2][output_row];
  19.150 +    output_row++;
  19.151 +    for (col = 0; col < num_cols; col++) {
  19.152 +      r = GETJSAMPLE(inptr[RGB_RED]);
  19.153 +      g = GETJSAMPLE(inptr[RGB_GREEN]);
  19.154 +      b = GETJSAMPLE(inptr[RGB_BLUE]);
  19.155 +      inptr += RGB_PIXELSIZE;
  19.156 +      /* If the inputs are 0..MAXJSAMPLE, the outputs of these equations
  19.157 +       * must be too; we do not need an explicit range-limiting operation.
  19.158 +       * Hence the value being shifted is never negative, and we don't
  19.159 +       * need the general RIGHT_SHIFT macro.
  19.160 +       */
  19.161 +      /* Y */
  19.162 +      outptr0[col] = (JSAMPLE)
  19.163 +		((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
  19.164 +		 >> SCALEBITS);
  19.165 +      /* Cb */
  19.166 +      outptr1[col] = (JSAMPLE)
  19.167 +		((ctab[r+R_CB_OFF] + ctab[g+G_CB_OFF] + ctab[b+B_CB_OFF])
  19.168 +		 >> SCALEBITS);
  19.169 +      /* Cr */
  19.170 +      outptr2[col] = (JSAMPLE)
  19.171 +		((ctab[r+R_CR_OFF] + ctab[g+G_CR_OFF] + ctab[b+B_CR_OFF])
  19.172 +		 >> SCALEBITS);
  19.173 +    }
  19.174 +  }
  19.175 +}
  19.176 +
  19.177 +
  19.178 +/**************** Cases other than RGB -> YCbCr **************/
  19.179 +
  19.180 +
  19.181 +/*
  19.182 + * Convert some rows of samples to the JPEG colorspace.
  19.183 + * This version handles RGB->grayscale conversion, which is the same
  19.184 + * as the RGB->Y portion of RGB->YCbCr.
  19.185 + * We assume rgb_ycc_start has been called (we only use the Y tables).
  19.186 + */
  19.187 +
  19.188 +METHODDEF(void)
  19.189 +rgb_gray_convert (j_compress_ptr cinfo,
  19.190 +		  JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
  19.191 +		  JDIMENSION output_row, int num_rows)
  19.192 +{
  19.193 +  my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
  19.194 +  register int r, g, b;
  19.195 +  register INT32 * ctab = cconvert->rgb_ycc_tab;
  19.196 +  register JSAMPROW inptr;
  19.197 +  register JSAMPROW outptr;
  19.198 +  register JDIMENSION col;
  19.199 +  JDIMENSION num_cols = cinfo->image_width;
  19.200 +
  19.201 +  while (--num_rows >= 0) {
  19.202 +    inptr = *input_buf++;
  19.203 +    outptr = output_buf[0][output_row];
  19.204 +    output_row++;
  19.205 +    for (col = 0; col < num_cols; col++) {
  19.206 +      r = GETJSAMPLE(inptr[RGB_RED]);
  19.207 +      g = GETJSAMPLE(inptr[RGB_GREEN]);
  19.208 +      b = GETJSAMPLE(inptr[RGB_BLUE]);
  19.209 +      inptr += RGB_PIXELSIZE;
  19.210 +      /* Y */
  19.211 +      outptr[col] = (JSAMPLE)
  19.212 +		((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
  19.213 +		 >> SCALEBITS);
  19.214 +    }
  19.215 +  }
  19.216 +}
  19.217 +
  19.218 +
  19.219 +/*
  19.220 + * Convert some rows of samples to the JPEG colorspace.
  19.221 + * This version handles Adobe-style CMYK->YCCK conversion,
  19.222 + * where we convert R=1-C, G=1-M, and B=1-Y to YCbCr using the same
  19.223 + * conversion as above, while passing K (black) unchanged.
  19.224 + * We assume rgb_ycc_start has been called.
  19.225 + */
  19.226 +
  19.227 +METHODDEF(void)
  19.228 +cmyk_ycck_convert (j_compress_ptr cinfo,
  19.229 +		   JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
  19.230 +		   JDIMENSION output_row, int num_rows)
  19.231 +{
  19.232 +  my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
  19.233 +  register int r, g, b;
  19.234 +  register INT32 * ctab = cconvert->rgb_ycc_tab;
  19.235 +  register JSAMPROW inptr;
  19.236 +  register JSAMPROW outptr0, outptr1, outptr2, outptr3;
  19.237 +  register JDIMENSION col;
  19.238 +  JDIMENSION num_cols = cinfo->image_width;
  19.239 +
  19.240 +  while (--num_rows >= 0) {
  19.241 +    inptr = *input_buf++;
  19.242 +    outptr0 = output_buf[0][output_row];
  19.243 +    outptr1 = output_buf[1][output_row];
  19.244 +    outptr2 = output_buf[2][output_row];
  19.245 +    outptr3 = output_buf[3][output_row];
  19.246 +    output_row++;
  19.247 +    for (col = 0; col < num_cols; col++) {
  19.248 +      r = MAXJSAMPLE - GETJSAMPLE(inptr[0]);
  19.249 +      g = MAXJSAMPLE - GETJSAMPLE(inptr[1]);
  19.250 +      b = MAXJSAMPLE - GETJSAMPLE(inptr[2]);
  19.251 +      /* K passes through as-is */
  19.252 +      outptr3[col] = inptr[3];	/* don't need GETJSAMPLE here */
  19.253 +      inptr += 4;
  19.254 +      /* If the inputs are 0..MAXJSAMPLE, the outputs of these equations
  19.255 +       * must be too; we do not need an explicit range-limiting operation.
  19.256 +       * Hence the value being shifted is never negative, and we don't
  19.257 +       * need the general RIGHT_SHIFT macro.
  19.258 +       */
  19.259 +      /* Y */
  19.260 +      outptr0[col] = (JSAMPLE)
  19.261 +		((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
  19.262 +		 >> SCALEBITS);
  19.263 +      /* Cb */
  19.264 +      outptr1[col] = (JSAMPLE)
  19.265 +		((ctab[r+R_CB_OFF] + ctab[g+G_CB_OFF] + ctab[b+B_CB_OFF])
  19.266 +		 >> SCALEBITS);
  19.267 +      /* Cr */
  19.268 +      outptr2[col] = (JSAMPLE)
  19.269 +		((ctab[r+R_CR_OFF] + ctab[g+G_CR_OFF] + ctab[b+B_CR_OFF])
  19.270 +		 >> SCALEBITS);
  19.271 +    }
  19.272 +  }
  19.273 +}
  19.274 +
  19.275 +
  19.276 +/*
  19.277 + * Convert some rows of samples to the JPEG colorspace.
  19.278 + * This version handles grayscale output with no conversion.
  19.279 + * The source can be either plain grayscale or YCbCr (since Y == gray).
  19.280 + */
  19.281 +
  19.282 +METHODDEF(void)
  19.283 +grayscale_convert (j_compress_ptr cinfo,
  19.284 +		   JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
  19.285 +		   JDIMENSION output_row, int num_rows)
  19.286 +{
  19.287 +  register JSAMPROW inptr;
  19.288 +  register JSAMPROW outptr;
  19.289 +  register JDIMENSION col;
  19.290 +  JDIMENSION num_cols = cinfo->image_width;
  19.291 +  int instride = cinfo->input_components;
  19.292 +
  19.293 +  while (--num_rows >= 0) {
  19.294 +    inptr = *input_buf++;
  19.295 +    outptr = output_buf[0][output_row];
  19.296 +    output_row++;
  19.297 +    for (col = 0; col < num_cols; col++) {
  19.298 +      outptr[col] = inptr[0];	/* don't need GETJSAMPLE() here */
  19.299 +      inptr += instride;
  19.300 +    }
  19.301 +  }
  19.302 +}
  19.303 +
  19.304 +
  19.305 +/*
  19.306 + * Convert some rows of samples to the JPEG colorspace.
  19.307 + * This version handles multi-component colorspaces without conversion.
  19.308 + * We assume input_components == num_components.
  19.309 + */
  19.310 +
  19.311 +METHODDEF(void)
  19.312 +null_convert (j_compress_ptr cinfo,
  19.313 +	      JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
  19.314 +	      JDIMENSION output_row, int num_rows)
  19.315 +{
  19.316 +  register JSAMPROW inptr;
  19.317 +  register JSAMPROW outptr;
  19.318 +  register JDIMENSION col;
  19.319 +  register int ci;
  19.320 +  int nc = cinfo->num_components;
  19.321 +  JDIMENSION num_cols = cinfo->image_width;
  19.322 +
  19.323 +  while (--num_rows >= 0) {
  19.324 +    /* It seems fastest to make a separate pass for each component. */
  19.325 +    for (ci = 0; ci < nc; ci++) {
  19.326 +      inptr = *input_buf;
  19.327 +      outptr = output_buf[ci][output_row];
  19.328 +      for (col = 0; col < num_cols; col++) {
  19.329 +	outptr[col] = inptr[ci]; /* don't need GETJSAMPLE() here */
  19.330 +	inptr += nc;
  19.331 +      }
  19.332 +    }
  19.333 +    input_buf++;
  19.334 +    output_row++;
  19.335 +  }
  19.336 +}
  19.337 +
  19.338 +
  19.339 +/*
  19.340 + * Empty method for start_pass.
  19.341 + */
  19.342 +
  19.343 +METHODDEF(void)
  19.344 +null_method (j_compress_ptr cinfo)
  19.345 +{
  19.346 +  /* no work needed */
  19.347 +}
  19.348 +
  19.349 +
  19.350 +/*
  19.351 + * Module initialization routine for input colorspace conversion.
  19.352 + */
  19.353 +
  19.354 +GLOBAL(void)
  19.355 +jinit_color_converter (j_compress_ptr cinfo)
  19.356 +{
  19.357 +  my_cconvert_ptr cconvert;
  19.358 +
  19.359 +  cconvert = (my_cconvert_ptr)
  19.360 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  19.361 +				SIZEOF(my_color_converter));
  19.362 +  cinfo->cconvert = (struct jpeg_color_converter *) cconvert;
  19.363 +  /* set start_pass to null method until we find out differently */
  19.364 +  cconvert->pub.start_pass = null_method;
  19.365 +
  19.366 +  /* Make sure input_components agrees with in_color_space */
  19.367 +  switch (cinfo->in_color_space) {
  19.368 +  case JCS_GRAYSCALE:
  19.369 +    if (cinfo->input_components != 1)
  19.370 +      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
  19.371 +    break;
  19.372 +
  19.373 +  case JCS_RGB:
  19.374 +#if RGB_PIXELSIZE != 3
  19.375 +    if (cinfo->input_components != RGB_PIXELSIZE)
  19.376 +      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
  19.377 +    break;
  19.378 +#endif /* else share code with YCbCr */
  19.379 +
  19.380 +  case JCS_YCbCr:
  19.381 +    if (cinfo->input_components != 3)
  19.382 +      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
  19.383 +    break;
  19.384 +
  19.385 +  case JCS_CMYK:
  19.386 +  case JCS_YCCK:
  19.387 +    if (cinfo->input_components != 4)
  19.388 +      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
  19.389 +    break;
  19.390 +
  19.391 +  default:			/* JCS_UNKNOWN can be anything */
  19.392 +    if (cinfo->input_components < 1)
  19.393 +      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
  19.394 +    break;
  19.395 +  }
  19.396 +
  19.397 +  /* Check num_components, set conversion method based on requested space */
  19.398 +  switch (cinfo->jpeg_color_space) {
  19.399 +  case JCS_GRAYSCALE:
  19.400 +    if (cinfo->num_components != 1)
  19.401 +      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  19.402 +    if (cinfo->in_color_space == JCS_GRAYSCALE)
  19.403 +      cconvert->pub.color_convert = grayscale_convert;
  19.404 +    else if (cinfo->in_color_space == JCS_RGB) {
  19.405 +      cconvert->pub.start_pass = rgb_ycc_start;
  19.406 +      cconvert->pub.color_convert = rgb_gray_convert;
  19.407 +    } else if (cinfo->in_color_space == JCS_YCbCr)
  19.408 +      cconvert->pub.color_convert = grayscale_convert;
  19.409 +    else
  19.410 +      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  19.411 +    break;
  19.412 +
  19.413 +  case JCS_RGB:
  19.414 +    if (cinfo->num_components != 3)
  19.415 +      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  19.416 +    if (cinfo->in_color_space == JCS_RGB && RGB_PIXELSIZE == 3)
  19.417 +      cconvert->pub.color_convert = null_convert;
  19.418 +    else
  19.419 +      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  19.420 +    break;
  19.421 +
  19.422 +  case JCS_YCbCr:
  19.423 +    if (cinfo->num_components != 3)
  19.424 +      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  19.425 +    if (cinfo->in_color_space == JCS_RGB) {
  19.426 +      cconvert->pub.start_pass = rgb_ycc_start;
  19.427 +      cconvert->pub.color_convert = rgb_ycc_convert;
  19.428 +    } else if (cinfo->in_color_space == JCS_YCbCr)
  19.429 +      cconvert->pub.color_convert = null_convert;
  19.430 +    else
  19.431 +      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  19.432 +    break;
  19.433 +
  19.434 +  case JCS_CMYK:
  19.435 +    if (cinfo->num_components != 4)
  19.436 +      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  19.437 +    if (cinfo->in_color_space == JCS_CMYK)
  19.438 +      cconvert->pub.color_convert = null_convert;
  19.439 +    else
  19.440 +      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  19.441 +    break;
  19.442 +
  19.443 +  case JCS_YCCK:
  19.444 +    if (cinfo->num_components != 4)
  19.445 +      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  19.446 +    if (cinfo->in_color_space == JCS_CMYK) {
  19.447 +      cconvert->pub.start_pass = rgb_ycc_start;
  19.448 +      cconvert->pub.color_convert = cmyk_ycck_convert;
  19.449 +    } else if (cinfo->in_color_space == JCS_YCCK)
  19.450 +      cconvert->pub.color_convert = null_convert;
  19.451 +    else
  19.452 +      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  19.453 +    break;
  19.454 +
  19.455 +  default:			/* allow null conversion of JCS_UNKNOWN */
  19.456 +    if (cinfo->jpeg_color_space != cinfo->in_color_space ||
  19.457 +	cinfo->num_components != cinfo->input_components)
  19.458 +      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  19.459 +    cconvert->pub.color_convert = null_convert;
  19.460 +    break;
  19.461 +  }
  19.462 +}
    20.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    20.2 +++ b/libs/libjpeg/jcdctmgr.c	Sat Sep 19 05:51:51 2015 +0300
    20.3 @@ -0,0 +1,387 @@
    20.4 +/*
    20.5 + * jcdctmgr.c
    20.6 + *
    20.7 + * Copyright (C) 1994-1996, Thomas G. Lane.
    20.8 + * This file is part of the Independent JPEG Group's software.
    20.9 + * For conditions of distribution and use, see the accompanying README file.
   20.10 + *
   20.11 + * This file contains the forward-DCT management logic.
   20.12 + * This code selects a particular DCT implementation to be used,
   20.13 + * and it performs related housekeeping chores including coefficient
   20.14 + * quantization.
   20.15 + */
   20.16 +
   20.17 +#define JPEG_INTERNALS
   20.18 +#include "jinclude.h"
   20.19 +#include "jpeglib.h"
   20.20 +#include "jdct.h"		/* Private declarations for DCT subsystem */
   20.21 +
   20.22 +
   20.23 +/* Private subobject for this module */
   20.24 +
   20.25 +typedef struct {
   20.26 +  struct jpeg_forward_dct pub;	/* public fields */
   20.27 +
   20.28 +  /* Pointer to the DCT routine actually in use */
   20.29 +  forward_DCT_method_ptr do_dct;
   20.30 +
   20.31 +  /* The actual post-DCT divisors --- not identical to the quant table
   20.32 +   * entries, because of scaling (especially for an unnormalized DCT).
   20.33 +   * Each table is given in normal array order.
   20.34 +   */
   20.35 +  DCTELEM * divisors[NUM_QUANT_TBLS];
   20.36 +
   20.37 +#ifdef DCT_FLOAT_SUPPORTED
   20.38 +  /* Same as above for the floating-point case. */
   20.39 +  float_DCT_method_ptr do_float_dct;
   20.40 +  FAST_FLOAT * float_divisors[NUM_QUANT_TBLS];
   20.41 +#endif
   20.42 +} my_fdct_controller;
   20.43 +
   20.44 +typedef my_fdct_controller * my_fdct_ptr;
   20.45 +
   20.46 +
   20.47 +/*
   20.48 + * Initialize for a processing pass.
   20.49 + * Verify that all referenced Q-tables are present, and set up
   20.50 + * the divisor table for each one.
   20.51 + * In the current implementation, DCT of all components is done during
   20.52 + * the first pass, even if only some components will be output in the
   20.53 + * first scan.  Hence all components should be examined here.
   20.54 + */
   20.55 +
   20.56 +METHODDEF(void)
   20.57 +start_pass_fdctmgr (j_compress_ptr cinfo)
   20.58 +{
   20.59 +  my_fdct_ptr fdct = (my_fdct_ptr) cinfo->fdct;
   20.60 +  int ci, qtblno, i;
   20.61 +  jpeg_component_info *compptr;
   20.62 +  JQUANT_TBL * qtbl;
   20.63 +  DCTELEM * dtbl;
   20.64 +
   20.65 +  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
   20.66 +       ci++, compptr++) {
   20.67 +    qtblno = compptr->quant_tbl_no;
   20.68 +    /* Make sure specified quantization table is present */
   20.69 +    if (qtblno < 0 || qtblno >= NUM_QUANT_TBLS ||
   20.70 +	cinfo->quant_tbl_ptrs[qtblno] == NULL)
   20.71 +      ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, qtblno);
   20.72 +    qtbl = cinfo->quant_tbl_ptrs[qtblno];
   20.73 +    /* Compute divisors for this quant table */
   20.74 +    /* We may do this more than once for same table, but it's not a big deal */
   20.75 +    switch (cinfo->dct_method) {
   20.76 +#ifdef DCT_ISLOW_SUPPORTED
   20.77 +    case JDCT_ISLOW:
   20.78 +      /* For LL&M IDCT method, divisors are equal to raw quantization
   20.79 +       * coefficients multiplied by 8 (to counteract scaling).
   20.80 +       */
   20.81 +      if (fdct->divisors[qtblno] == NULL) {
   20.82 +	fdct->divisors[qtblno] = (DCTELEM *)
   20.83 +	  (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
   20.84 +				      DCTSIZE2 * SIZEOF(DCTELEM));
   20.85 +      }
   20.86 +      dtbl = fdct->divisors[qtblno];
   20.87 +      for (i = 0; i < DCTSIZE2; i++) {
   20.88 +	dtbl[i] = ((DCTELEM) qtbl->quantval[i]) << 3;
   20.89 +      }
   20.90 +      break;
   20.91 +#endif
   20.92 +#ifdef DCT_IFAST_SUPPORTED
   20.93 +    case JDCT_IFAST:
   20.94 +      {
   20.95 +	/* For AA&N IDCT method, divisors are equal to quantization
   20.96 +	 * coefficients scaled by scalefactor[row]*scalefactor[col], where
   20.97 +	 *   scalefactor[0] = 1
   20.98 +	 *   scalefactor[k] = cos(k*PI/16) * sqrt(2)    for k=1..7
   20.99 +	 * We apply a further scale factor of 8.
  20.100 +	 */
  20.101 +#define CONST_BITS 14
  20.102 +	static const INT16 aanscales[DCTSIZE2] = {
  20.103 +	  /* precomputed values scaled up by 14 bits */
  20.104 +	  16384, 22725, 21407, 19266, 16384, 12873,  8867,  4520,
  20.105 +	  22725, 31521, 29692, 26722, 22725, 17855, 12299,  6270,
  20.106 +	  21407, 29692, 27969, 25172, 21407, 16819, 11585,  5906,
  20.107 +	  19266, 26722, 25172, 22654, 19266, 15137, 10426,  5315,
  20.108 +	  16384, 22725, 21407, 19266, 16384, 12873,  8867,  4520,
  20.109 +	  12873, 17855, 16819, 15137, 12873, 10114,  6967,  3552,
  20.110 +	   8867, 12299, 11585, 10426,  8867,  6967,  4799,  2446,
  20.111 +	   4520,  6270,  5906,  5315,  4520,  3552,  2446,  1247
  20.112 +	};
  20.113 +	SHIFT_TEMPS
  20.114 +
  20.115 +	if (fdct->divisors[qtblno] == NULL) {
  20.116 +	  fdct->divisors[qtblno] = (DCTELEM *)
  20.117 +	    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  20.118 +					DCTSIZE2 * SIZEOF(DCTELEM));
  20.119 +	}
  20.120 +	dtbl = fdct->divisors[qtblno];
  20.121 +	for (i = 0; i < DCTSIZE2; i++) {
  20.122 +	  dtbl[i] = (DCTELEM)
  20.123 +	    DESCALE(MULTIPLY16V16((INT32) qtbl->quantval[i],
  20.124 +				  (INT32) aanscales[i]),
  20.125 +		    CONST_BITS-3);
  20.126 +	}
  20.127 +      }
  20.128 +      break;
  20.129 +#endif
  20.130 +#ifdef DCT_FLOAT_SUPPORTED
  20.131 +    case JDCT_FLOAT:
  20.132 +      {
  20.133 +	/* For float AA&N IDCT method, divisors are equal to quantization
  20.134 +	 * coefficients scaled by scalefactor[row]*scalefactor[col], where
  20.135 +	 *   scalefactor[0] = 1
  20.136 +	 *   scalefactor[k] = cos(k*PI/16) * sqrt(2)    for k=1..7
  20.137 +	 * We apply a further scale factor of 8.
  20.138 +	 * What's actually stored is 1/divisor so that the inner loop can
  20.139 +	 * use a multiplication rather than a division.
  20.140 +	 */
  20.141 +	FAST_FLOAT * fdtbl;
  20.142 +	int row, col;
  20.143 +	static const double aanscalefactor[DCTSIZE] = {
  20.144 +	  1.0, 1.387039845, 1.306562965, 1.175875602,
  20.145 +	  1.0, 0.785694958, 0.541196100, 0.275899379
  20.146 +	};
  20.147 +
  20.148 +	if (fdct->float_divisors[qtblno] == NULL) {
  20.149 +	  fdct->float_divisors[qtblno] = (FAST_FLOAT *)
  20.150 +	    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  20.151 +					DCTSIZE2 * SIZEOF(FAST_FLOAT));
  20.152 +	}
  20.153 +	fdtbl = fdct->float_divisors[qtblno];
  20.154 +	i = 0;
  20.155 +	for (row = 0; row < DCTSIZE; row++) {
  20.156 +	  for (col = 0; col < DCTSIZE; col++) {
  20.157 +	    fdtbl[i] = (FAST_FLOAT)
  20.158 +	      (1.0 / (((double) qtbl->quantval[i] *
  20.159 +		       aanscalefactor[row] * aanscalefactor[col] * 8.0)));
  20.160 +	    i++;
  20.161 +	  }
  20.162 +	}
  20.163 +      }
  20.164 +      break;
  20.165 +#endif
  20.166 +    default:
  20.167 +      ERREXIT(cinfo, JERR_NOT_COMPILED);
  20.168 +      break;
  20.169 +    }
  20.170 +  }
  20.171 +}
  20.172 +
  20.173 +
  20.174 +/*
  20.175 + * Perform forward DCT on one or more blocks of a component.
  20.176 + *
  20.177 + * The input samples are taken from the sample_data[] array starting at
  20.178 + * position start_row/start_col, and moving to the right for any additional
  20.179 + * blocks. The quantized coefficients are returned in coef_blocks[].
  20.180 + */
  20.181 +
  20.182 +METHODDEF(void)
  20.183 +forward_DCT (j_compress_ptr cinfo, jpeg_component_info * compptr,
  20.184 +	     JSAMPARRAY sample_data, JBLOCKROW coef_blocks,
  20.185 +	     JDIMENSION start_row, JDIMENSION start_col,
  20.186 +	     JDIMENSION num_blocks)
  20.187 +/* This version is used for integer DCT implementations. */
  20.188 +{
  20.189 +  /* This routine is heavily used, so it's worth coding it tightly. */
  20.190 +  my_fdct_ptr fdct = (my_fdct_ptr) cinfo->fdct;
  20.191 +  forward_DCT_method_ptr do_dct = fdct->do_dct;
  20.192 +  DCTELEM * divisors = fdct->divisors[compptr->quant_tbl_no];
  20.193 +  DCTELEM workspace[DCTSIZE2];	/* work area for FDCT subroutine */
  20.194 +  JDIMENSION bi;
  20.195 +
  20.196 +  sample_data += start_row;	/* fold in the vertical offset once */
  20.197 +
  20.198 +  for (bi = 0; bi < num_blocks; bi++, start_col += DCTSIZE) {
  20.199 +    /* Load data into workspace, applying unsigned->signed conversion */
  20.200 +    { register DCTELEM *workspaceptr;
  20.201 +      register JSAMPROW elemptr;
  20.202 +      register int elemr;
  20.203 +
  20.204 +      workspaceptr = workspace;
  20.205 +      for (elemr = 0; elemr < DCTSIZE; elemr++) {
  20.206 +	elemptr = sample_data[elemr] + start_col;
  20.207 +#if DCTSIZE == 8		/* unroll the inner loop */
  20.208 +	*workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
  20.209 +	*workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
  20.210 +	*workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
  20.211 +	*workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
  20.212 +	*workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
  20.213 +	*workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
  20.214 +	*workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
  20.215 +	*workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
  20.216 +#else
  20.217 +	{ register int elemc;
  20.218 +	  for (elemc = DCTSIZE; elemc > 0; elemc--) {
  20.219 +	    *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
  20.220 +	  }
  20.221 +	}
  20.222 +#endif
  20.223 +      }
  20.224 +    }
  20.225 +
  20.226 +    /* Perform the DCT */
  20.227 +    (*do_dct) (workspace);
  20.228 +
  20.229 +    /* Quantize/descale the coefficients, and store into coef_blocks[] */
  20.230 +    { register DCTELEM temp, qval;
  20.231 +      register int i;
  20.232 +      register JCOEFPTR output_ptr = coef_blocks[bi];
  20.233 +
  20.234 +      for (i = 0; i < DCTSIZE2; i++) {
  20.235 +	qval = divisors[i];
  20.236 +	temp = workspace[i];
  20.237 +	/* Divide the coefficient value by qval, ensuring proper rounding.
  20.238 +	 * Since C does not specify the direction of rounding for negative
  20.239 +	 * quotients, we have to force the dividend positive for portability.
  20.240 +	 *
  20.241 +	 * In most files, at least half of the output values will be zero
  20.242 +	 * (at default quantization settings, more like three-quarters...)
  20.243 +	 * so we should ensure that this case is fast.  On many machines,
  20.244 +	 * a comparison is enough cheaper than a divide to make a special test
  20.245 +	 * a win.  Since both inputs will be nonnegative, we need only test
  20.246 +	 * for a < b to discover whether a/b is 0.
  20.247 +	 * If your machine's division is fast enough, define FAST_DIVIDE.
  20.248 +	 */
  20.249 +#ifdef FAST_DIVIDE
  20.250 +#define DIVIDE_BY(a,b)	a /= b
  20.251 +#else
  20.252 +#define DIVIDE_BY(a,b)	if (a >= b) a /= b; else a = 0
  20.253 +#endif
  20.254 +	if (temp < 0) {
  20.255 +	  temp = -temp;
  20.256 +	  temp += qval>>1;	/* for rounding */
  20.257 +	  DIVIDE_BY(temp, qval);
  20.258 +	  temp = -temp;
  20.259 +	} else {
  20.260 +	  temp += qval>>1;	/* for rounding */
  20.261 +	  DIVIDE_BY(temp, qval);
  20.262 +	}
  20.263 +	output_ptr[i] = (JCOEF) temp;
  20.264 +      }
  20.265 +    }
  20.266 +  }
  20.267 +}
  20.268 +
  20.269 +
  20.270 +#ifdef DCT_FLOAT_SUPPORTED
  20.271 +
  20.272 +METHODDEF(void)
  20.273 +forward_DCT_float (j_compress_ptr cinfo, jpeg_component_info * compptr,
  20.274 +		   JSAMPARRAY sample_data, JBLOCKROW coef_blocks,
  20.275 +		   JDIMENSION start_row, JDIMENSION start_col,
  20.276 +		   JDIMENSION num_blocks)
  20.277 +/* This version is used for floating-point DCT implementations. */
  20.278 +{
  20.279 +  /* This routine is heavily used, so it's worth coding it tightly. */
  20.280 +  my_fdct_ptr fdct = (my_fdct_ptr) cinfo->fdct;
  20.281 +  float_DCT_method_ptr do_dct = fdct->do_float_dct;
  20.282 +  FAST_FLOAT * divisors = fdct->float_divisors[compptr->quant_tbl_no];
  20.283 +  FAST_FLOAT workspace[DCTSIZE2]; /* work area for FDCT subroutine */
  20.284 +  JDIMENSION bi;
  20.285 +
  20.286 +  sample_data += start_row;	/* fold in the vertical offset once */
  20.287 +
  20.288 +  for (bi = 0; bi < num_blocks; bi++, start_col += DCTSIZE) {
  20.289 +    /* Load data into workspace, applying unsigned->signed conversion */
  20.290 +    { register FAST_FLOAT *workspaceptr;
  20.291 +      register JSAMPROW elemptr;
  20.292 +      register int elemr;
  20.293 +
  20.294 +      workspaceptr = workspace;
  20.295 +      for (elemr = 0; elemr < DCTSIZE; elemr++) {
  20.296 +	elemptr = sample_data[elemr] + start_col;
  20.297 +#if DCTSIZE == 8		/* unroll the inner loop */
  20.298 +	*workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
  20.299 +	*workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
  20.300 +	*workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
  20.301 +	*workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
  20.302 +	*workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
  20.303 +	*workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
  20.304 +	*workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
  20.305 +	*workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
  20.306 +#else
  20.307 +	{ register int elemc;
  20.308 +	  for (elemc = DCTSIZE; elemc > 0; elemc--) {
  20.309 +	    *workspaceptr++ = (FAST_FLOAT)
  20.310 +	      (GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
  20.311 +	  }
  20.312 +	}
  20.313 +#endif
  20.314 +      }
  20.315 +    }
  20.316 +
  20.317 +    /* Perform the DCT */
  20.318 +    (*do_dct) (workspace);
  20.319 +
  20.320 +    /* Quantize/descale the coefficients, and store into coef_blocks[] */
  20.321 +    { register FAST_FLOAT temp;
  20.322 +      register int i;
  20.323 +      register JCOEFPTR output_ptr = coef_blocks[bi];
  20.324 +
  20.325 +      for (i = 0; i < DCTSIZE2; i++) {
  20.326 +	/* Apply the quantization and scaling factor */
  20.327 +	temp = workspace[i] * divisors[i];
  20.328 +	/* Round to nearest integer.
  20.329 +	 * Since C does not specify the direction of rounding for negative
  20.330 +	 * quotients, we have to force the dividend positive for portability.
  20.331 +	 * The maximum coefficient size is +-16K (for 12-bit data), so this
  20.332 +	 * code should work for either 16-bit or 32-bit ints.
  20.333 +	 */
  20.334 +	output_ptr[i] = (JCOEF) ((int) (temp + (FAST_FLOAT) 16384.5) - 16384);
  20.335 +      }
  20.336 +    }
  20.337 +  }
  20.338 +}
  20.339 +
  20.340 +#endif /* DCT_FLOAT_SUPPORTED */
  20.341 +
  20.342 +
  20.343 +/*
  20.344 + * Initialize FDCT manager.
  20.345 + */
  20.346 +
  20.347 +GLOBAL(void)
  20.348 +jinit_forward_dct (j_compress_ptr cinfo)
  20.349 +{
  20.350 +  my_fdct_ptr fdct;
  20.351 +  int i;
  20.352 +
  20.353 +  fdct = (my_fdct_ptr)
  20.354 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  20.355 +				SIZEOF(my_fdct_controller));
  20.356 +  cinfo->fdct = (struct jpeg_forward_dct *) fdct;
  20.357 +  fdct->pub.start_pass = start_pass_fdctmgr;
  20.358 +
  20.359 +  switch (cinfo->dct_method) {
  20.360 +#ifdef DCT_ISLOW_SUPPORTED
  20.361 +  case JDCT_ISLOW:
  20.362 +    fdct->pub.forward_DCT = forward_DCT;
  20.363 +    fdct->do_dct = jpeg_fdct_islow;
  20.364 +    break;
  20.365 +#endif
  20.366 +#ifdef DCT_IFAST_SUPPORTED
  20.367 +  case JDCT_IFAST:
  20.368 +    fdct->pub.forward_DCT = forward_DCT;
  20.369 +    fdct->do_dct = jpeg_fdct_ifast;
  20.370 +    break;
  20.371 +#endif
  20.372 +#ifdef DCT_FLOAT_SUPPORTED
  20.373 +  case JDCT_FLOAT:
  20.374 +    fdct->pub.forward_DCT = forward_DCT_float;
  20.375 +    fdct->do_float_dct = jpeg_fdct_float;
  20.376 +    break;
  20.377 +#endif
  20.378 +  default:
  20.379 +    ERREXIT(cinfo, JERR_NOT_COMPILED);
  20.380 +    break;
  20.381 +  }
  20.382 +
  20.383 +  /* Mark divisor tables unallocated */
  20.384 +  for (i = 0; i < NUM_QUANT_TBLS; i++) {
  20.385 +    fdct->divisors[i] = NULL;
  20.386 +#ifdef DCT_FLOAT_SUPPORTED
  20.387 +    fdct->float_divisors[i] = NULL;
  20.388 +#endif
  20.389 +  }
  20.390 +}
    21.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    21.2 +++ b/libs/libjpeg/jchuff.c	Sat Sep 19 05:51:51 2015 +0300
    21.3 @@ -0,0 +1,909 @@
    21.4 +/*
    21.5 + * jchuff.c
    21.6 + *
    21.7 + * Copyright (C) 1991-1997, Thomas G. Lane.
    21.8 + * This file is part of the Independent JPEG Group's software.
    21.9 + * For conditions of distribution and use, see the accompanying README file.
   21.10 + *
   21.11 + * This file contains Huffman entropy encoding routines.
   21.12 + *
   21.13 + * Much of the complexity here has to do with supporting output suspension.
   21.14 + * If the data destination module demands suspension, we want to be able to
   21.15 + * back up to the start of the current MCU.  To do this, we copy state
   21.16 + * variables into local working storage, and update them back to the
   21.17 + * permanent JPEG objects only upon successful completion of an MCU.
   21.18 + */
   21.19 +
   21.20 +#define JPEG_INTERNALS
   21.21 +#include "jinclude.h"
   21.22 +#include "jpeglib.h"
   21.23 +#include "jchuff.h"		/* Declarations shared with jcphuff.c */
   21.24 +
   21.25 +
   21.26 +/* Expanded entropy encoder object for Huffman encoding.
   21.27 + *
   21.28 + * The savable_state subrecord contains fields that change within an MCU,
   21.29 + * but must not be updated permanently until we complete the MCU.
   21.30 + */
   21.31 +
   21.32 +typedef struct {
   21.33 +  INT32 put_buffer;		/* current bit-accumulation buffer */
   21.34 +  int put_bits;			/* # of bits now in it */
   21.35 +  int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */
   21.36 +} savable_state;
   21.37 +
   21.38 +/* This macro is to work around compilers with missing or broken
   21.39 + * structure assignment.  You'll need to fix this code if you have
   21.40 + * such a compiler and you change MAX_COMPS_IN_SCAN.
   21.41 + */
   21.42 +
   21.43 +#ifndef NO_STRUCT_ASSIGN
   21.44 +#define ASSIGN_STATE(dest,src)  ((dest) = (src))
   21.45 +#else
   21.46 +#if MAX_COMPS_IN_SCAN == 4
   21.47 +#define ASSIGN_STATE(dest,src)  \
   21.48 +	((dest).put_buffer = (src).put_buffer, \
   21.49 +	 (dest).put_bits = (src).put_bits, \
   21.50 +	 (dest).last_dc_val[0] = (src).last_dc_val[0], \
   21.51 +	 (dest).last_dc_val[1] = (src).last_dc_val[1], \
   21.52 +	 (dest).last_dc_val[2] = (src).last_dc_val[2], \
   21.53 +	 (dest).last_dc_val[3] = (src).last_dc_val[3])
   21.54 +#endif
   21.55 +#endif
   21.56 +
   21.57 +
   21.58 +typedef struct {
   21.59 +  struct jpeg_entropy_encoder pub; /* public fields */
   21.60 +
   21.61 +  savable_state saved;		/* Bit buffer & DC state at start of MCU */
   21.62 +
   21.63 +  /* These fields are NOT loaded into local working state. */
   21.64 +  unsigned int restarts_to_go;	/* MCUs left in this restart interval */
   21.65 +  int next_restart_num;		/* next restart number to write (0-7) */
   21.66 +
   21.67 +  /* Pointers to derived tables (these workspaces have image lifespan) */
   21.68 +  c_derived_tbl * dc_derived_tbls[NUM_HUFF_TBLS];
   21.69 +  c_derived_tbl * ac_derived_tbls[NUM_HUFF_TBLS];
   21.70 +
   21.71 +#ifdef ENTROPY_OPT_SUPPORTED	/* Statistics tables for optimization */
   21.72 +  long * dc_count_ptrs[NUM_HUFF_TBLS];
   21.73 +  long * ac_count_ptrs[NUM_HUFF_TBLS];
   21.74 +#endif
   21.75 +} huff_entropy_encoder;
   21.76 +
   21.77 +typedef huff_entropy_encoder * huff_entropy_ptr;
   21.78 +
   21.79 +/* Working state while writing an MCU.
   21.80 + * This struct contains all the fields that are needed by subroutines.
   21.81 + */
   21.82 +
   21.83 +typedef struct {
   21.84 +  JOCTET * next_output_byte;	/* => next byte to write in buffer */
   21.85 +  size_t free_in_buffer;	/* # of byte spaces remaining in buffer */
   21.86 +  savable_state cur;		/* Current bit buffer & DC state */
   21.87 +  j_compress_ptr cinfo;		/* dump_buffer needs access to this */
   21.88 +} working_state;
   21.89 +
   21.90 +
   21.91 +/* Forward declarations */
   21.92 +METHODDEF(boolean) encode_mcu_huff JPP((j_compress_ptr cinfo,
   21.93 +					JBLOCKROW *MCU_data));
   21.94 +METHODDEF(void) finish_pass_huff JPP((j_compress_ptr cinfo));
   21.95 +#ifdef ENTROPY_OPT_SUPPORTED
   21.96 +METHODDEF(boolean) encode_mcu_gather JPP((j_compress_ptr cinfo,
   21.97 +					  JBLOCKROW *MCU_data));
   21.98 +METHODDEF(void) finish_pass_gather JPP((j_compress_ptr cinfo));
   21.99 +#endif
  21.100 +
  21.101 +
  21.102 +/*
  21.103 + * Initialize for a Huffman-compressed scan.
  21.104 + * If gather_statistics is TRUE, we do not output anything during the scan,
  21.105 + * just count the Huffman symbols used and generate Huffman code tables.
  21.106 + */
  21.107 +
  21.108 +METHODDEF(void)
  21.109 +start_pass_huff (j_compress_ptr cinfo, boolean gather_statistics)
  21.110 +{
  21.111 +  huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
  21.112 +  int ci, dctbl, actbl;
  21.113 +  jpeg_component_info * compptr;
  21.114 +
  21.115 +  if (gather_statistics) {
  21.116 +#ifdef ENTROPY_OPT_SUPPORTED
  21.117 +    entropy->pub.encode_mcu = encode_mcu_gather;
  21.118 +    entropy->pub.finish_pass = finish_pass_gather;
  21.119 +#else
  21.120 +    ERREXIT(cinfo, JERR_NOT_COMPILED);
  21.121 +#endif
  21.122 +  } else {
  21.123 +    entropy->pub.encode_mcu = encode_mcu_huff;
  21.124 +    entropy->pub.finish_pass = finish_pass_huff;
  21.125 +  }
  21.126 +
  21.127 +  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  21.128 +    compptr = cinfo->cur_comp_info[ci];
  21.129 +    dctbl = compptr->dc_tbl_no;
  21.130 +    actbl = compptr->ac_tbl_no;
  21.131 +    if (gather_statistics) {
  21.132 +#ifdef ENTROPY_OPT_SUPPORTED
  21.133 +      /* Check for invalid table indexes */
  21.134 +      /* (make_c_derived_tbl does this in the other path) */
  21.135 +      if (dctbl < 0 || dctbl >= NUM_HUFF_TBLS)
  21.136 +	ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, dctbl);
  21.137 +      if (actbl < 0 || actbl >= NUM_HUFF_TBLS)
  21.138 +	ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, actbl);
  21.139 +      /* Allocate and zero the statistics tables */
  21.140 +      /* Note that jpeg_gen_optimal_table expects 257 entries in each table! */
  21.141 +      if (entropy->dc_count_ptrs[dctbl] == NULL)
  21.142 +	entropy->dc_count_ptrs[dctbl] = (long *)
  21.143 +	  (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  21.144 +				      257 * SIZEOF(long));
  21.145 +      MEMZERO(entropy->dc_count_ptrs[dctbl], 257 * SIZEOF(long));
  21.146 +      if (entropy->ac_count_ptrs[actbl] == NULL)
  21.147 +	entropy->ac_count_ptrs[actbl] = (long *)
  21.148 +	  (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  21.149 +				      257 * SIZEOF(long));
  21.150 +      MEMZERO(entropy->ac_count_ptrs[actbl], 257 * SIZEOF(long));
  21.151 +#endif
  21.152 +    } else {
  21.153 +      /* Compute derived values for Huffman tables */
  21.154 +      /* We may do this more than once for a table, but it's not expensive */
  21.155 +      jpeg_make_c_derived_tbl(cinfo, TRUE, dctbl,
  21.156 +			      & entropy->dc_derived_tbls[dctbl]);
  21.157 +      jpeg_make_c_derived_tbl(cinfo, FALSE, actbl,
  21.158 +			      & entropy->ac_derived_tbls[actbl]);
  21.159 +    }
  21.160 +    /* Initialize DC predictions to 0 */
  21.161 +    entropy->saved.last_dc_val[ci] = 0;
  21.162 +  }
  21.163 +
  21.164 +  /* Initialize bit buffer to empty */
  21.165 +  entropy->saved.put_buffer = 0;
  21.166 +  entropy->saved.put_bits = 0;
  21.167 +
  21.168 +  /* Initialize restart stuff */
  21.169 +  entropy->restarts_to_go = cinfo->restart_interval;
  21.170 +  entropy->next_restart_num = 0;
  21.171 +}
  21.172 +
  21.173 +
  21.174 +/*
  21.175 + * Compute the derived values for a Huffman table.
  21.176 + * This routine also performs some validation checks on the table.
  21.177 + *
  21.178 + * Note this is also used by jcphuff.c.
  21.179 + */
  21.180 +
  21.181 +GLOBAL(void)
  21.182 +jpeg_make_c_derived_tbl (j_compress_ptr cinfo, boolean isDC, int tblno,
  21.183 +			 c_derived_tbl ** pdtbl)
  21.184 +{
  21.185 +  JHUFF_TBL *htbl;
  21.186 +  c_derived_tbl *dtbl;
  21.187 +  int p, i, l, lastp, si, maxsymbol;
  21.188 +  char huffsize[257];
  21.189 +  unsigned int huffcode[257];
  21.190 +  unsigned int code;
  21.191 +
  21.192 +  /* Note that huffsize[] and huffcode[] are filled in code-length order,
  21.193 +   * paralleling the order of the symbols themselves in htbl->huffval[].
  21.194 +   */
  21.195 +
  21.196 +  /* Find the input Huffman table */
  21.197 +  if (tblno < 0 || tblno >= NUM_HUFF_TBLS)
  21.198 +    ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
  21.199 +  htbl =
  21.200 +    isDC ? cinfo->dc_huff_tbl_ptrs[tblno] : cinfo->ac_huff_tbl_ptrs[tblno];
  21.201 +  if (htbl == NULL)
  21.202 +    ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
  21.203 +
  21.204 +  /* Allocate a workspace if we haven't already done so. */
  21.205 +  if (*pdtbl == NULL)
  21.206 +    *pdtbl = (c_derived_tbl *)
  21.207 +      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  21.208 +				  SIZEOF(c_derived_tbl));
  21.209 +  dtbl = *pdtbl;
  21.210 +  
  21.211 +  /* Figure C.1: make table of Huffman code length for each symbol */
  21.212 +
  21.213 +  p = 0;
  21.214 +  for (l = 1; l <= 16; l++) {
  21.215 +    i = (int) htbl->bits[l];
  21.216 +    if (i < 0 || p + i > 256)	/* protect against table overrun */
  21.217 +      ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
  21.218 +    while (i--)
  21.219 +      huffsize[p++] = (char) l;
  21.220 +  }
  21.221 +  huffsize[p] = 0;
  21.222 +  lastp = p;
  21.223 +  
  21.224 +  /* Figure C.2: generate the codes themselves */
  21.225 +  /* We also validate that the counts represent a legal Huffman code tree. */
  21.226 +
  21.227 +  code = 0;
  21.228 +  si = huffsize[0];
  21.229 +  p = 0;
  21.230 +  while (huffsize[p]) {
  21.231 +    while (((int) huffsize[p]) == si) {
  21.232 +      huffcode[p++] = code;
  21.233 +      code++;
  21.234 +    }
  21.235 +    /* code is now 1 more than the last code used for codelength si; but
  21.236 +     * it must still fit in si bits, since no code is allowed to be all ones.
  21.237 +     */
  21.238 +    if (((INT32) code) >= (((INT32) 1) << si))
  21.239 +      ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
  21.240 +    code <<= 1;
  21.241 +    si++;
  21.242 +  }
  21.243 +  
  21.244 +  /* Figure C.3: generate encoding tables */
  21.245 +  /* These are code and size indexed by symbol value */
  21.246 +
  21.247 +  /* Set all codeless symbols to have code length 0;
  21.248 +   * this lets us detect duplicate VAL entries here, and later
  21.249 +   * allows emit_bits to detect any attempt to emit such symbols.
  21.250 +   */
  21.251 +  MEMZERO(dtbl->ehufsi, SIZEOF(dtbl->ehufsi));
  21.252 +
  21.253 +  /* This is also a convenient place to check for out-of-range
  21.254 +   * and duplicated VAL entries.  We allow 0..255 for AC symbols
  21.255 +   * but only 0..15 for DC.  (We could constrain them further
  21.256 +   * based on data depth and mode, but this seems enough.)
  21.257 +   */
  21.258 +  maxsymbol = isDC ? 15 : 255;
  21.259 +
  21.260 +  for (p = 0; p < lastp; p++) {
  21.261 +    i = htbl->huffval[p];
  21.262 +    if (i < 0 || i > maxsymbol || dtbl->ehufsi[i])
  21.263 +      ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
  21.264 +    dtbl->ehufco[i] = huffcode[p];
  21.265 +    dtbl->ehufsi[i] = huffsize[p];
  21.266 +  }
  21.267 +}
  21.268 +
  21.269 +
  21.270 +/* Outputting bytes to the file */
  21.271 +
  21.272 +/* Emit a byte, taking 'action' if must suspend. */
  21.273 +#define emit_byte(state,val,action)  \
  21.274 +	{ *(state)->next_output_byte++ = (JOCTET) (val);  \
  21.275 +	  if (--(state)->free_in_buffer == 0)  \
  21.276 +	    if (! dump_buffer(state))  \
  21.277 +	      { action; } }
  21.278 +
  21.279 +
  21.280 +LOCAL(boolean)
  21.281 +dump_buffer (working_state * state)
  21.282 +/* Empty the output buffer; return TRUE if successful, FALSE if must suspend */
  21.283 +{
  21.284 +  struct jpeg_destination_mgr * dest = state->cinfo->dest;
  21.285 +
  21.286 +  if (! (*dest->empty_output_buffer) (state->cinfo))
  21.287 +    return FALSE;
  21.288 +  /* After a successful buffer dump, must reset buffer pointers */
  21.289 +  state->next_output_byte = dest->next_output_byte;
  21.290 +  state->free_in_buffer = dest->free_in_buffer;
  21.291 +  return TRUE;
  21.292 +}
  21.293 +
  21.294 +
  21.295 +/* Outputting bits to the file */
  21.296 +
  21.297 +/* Only the right 24 bits of put_buffer are used; the valid bits are
  21.298 + * left-justified in this part.  At most 16 bits can be passed to emit_bits
  21.299 + * in one call, and we never retain more than 7 bits in put_buffer
  21.300 + * between calls, so 24 bits are sufficient.
  21.301 + */
  21.302 +
  21.303 +INLINE
  21.304 +LOCAL(boolean)
  21.305 +emit_bits (working_state * state, unsigned int code, int size)
  21.306 +/* Emit some bits; return TRUE if successful, FALSE if must suspend */
  21.307 +{
  21.308 +  /* This routine is heavily used, so it's worth coding tightly. */
  21.309 +  register INT32 put_buffer = (INT32) code;
  21.310 +  register int put_bits = state->cur.put_bits;
  21.311 +
  21.312 +  /* if size is 0, caller used an invalid Huffman table entry */
  21.313 +  if (size == 0)
  21.314 +    ERREXIT(state->cinfo, JERR_HUFF_MISSING_CODE);
  21.315 +
  21.316 +  put_buffer &= (((INT32) 1)<<size) - 1; /* mask off any extra bits in code */
  21.317 +  
  21.318 +  put_bits += size;		/* new number of bits in buffer */
  21.319 +  
  21.320 +  put_buffer <<= 24 - put_bits; /* align incoming bits */
  21.321 +
  21.322 +  put_buffer |= state->cur.put_buffer; /* and merge with old buffer contents */
  21.323 +  
  21.324 +  while (put_bits >= 8) {
  21.325 +    int c = (int) ((put_buffer >> 16) & 0xFF);
  21.326 +    
  21.327 +    emit_byte(state, c, return FALSE);
  21.328 +    if (c == 0xFF) {		/* need to stuff a zero byte? */
  21.329 +      emit_byte(state, 0, return FALSE);
  21.330 +    }
  21.331 +    put_buffer <<= 8;
  21.332 +    put_bits -= 8;
  21.333 +  }
  21.334 +
  21.335 +  state->cur.put_buffer = put_buffer; /* update state variables */
  21.336 +  state->cur.put_bits = put_bits;
  21.337 +
  21.338 +  return TRUE;
  21.339 +}
  21.340 +
  21.341 +
  21.342 +LOCAL(boolean)
  21.343 +flush_bits (working_state * state)
  21.344 +{
  21.345 +  if (! emit_bits(state, 0x7F, 7)) /* fill any partial byte with ones */
  21.346 +    return FALSE;
  21.347 +  state->cur.put_buffer = 0;	/* and reset bit-buffer to empty */
  21.348 +  state->cur.put_bits = 0;
  21.349 +  return TRUE;
  21.350 +}
  21.351 +
  21.352 +
  21.353 +/* Encode a single block's worth of coefficients */
  21.354 +
  21.355 +LOCAL(boolean)
  21.356 +encode_one_block (working_state * state, JCOEFPTR block, int last_dc_val,
  21.357 +		  c_derived_tbl *dctbl, c_derived_tbl *actbl)
  21.358 +{
  21.359 +  register int temp, temp2;
  21.360 +  register int nbits;
  21.361 +  register int k, r, i;
  21.362 +  
  21.363 +  /* Encode the DC coefficient difference per section F.1.2.1 */
  21.364 +  
  21.365 +  temp = temp2 = block[0] - last_dc_val;
  21.366 +
  21.367 +  if (temp < 0) {
  21.368 +    temp = -temp;		/* temp is abs value of input */
  21.369 +    /* For a negative input, want temp2 = bitwise complement of abs(input) */
  21.370 +    /* This code assumes we are on a two's complement machine */
  21.371 +    temp2--;
  21.372 +  }
  21.373 +  
  21.374 +  /* Find the number of bits needed for the magnitude of the coefficient */
  21.375 +  nbits = 0;
  21.376 +  while (temp) {
  21.377 +    nbits++;
  21.378 +    temp >>= 1;
  21.379 +  }
  21.380 +  /* Check for out-of-range coefficient values.
  21.381 +   * Since we're encoding a difference, the range limit is twice as much.
  21.382 +   */
  21.383 +  if (nbits > MAX_COEF_BITS+1)
  21.384 +    ERREXIT(state->cinfo, JERR_BAD_DCT_COEF);
  21.385 +  
  21.386 +  /* Emit the Huffman-coded symbol for the number of bits */
  21.387 +  if (! emit_bits(state, dctbl->ehufco[nbits], dctbl->ehufsi[nbits]))
  21.388 +    return FALSE;
  21.389 +
  21.390 +  /* Emit that number of bits of the value, if positive, */
  21.391 +  /* or the complement of its magnitude, if negative. */
  21.392 +  if (nbits)			/* emit_bits rejects calls with size 0 */
  21.393 +    if (! emit_bits(state, (unsigned int) temp2, nbits))
  21.394 +      return FALSE;
  21.395 +
  21.396 +  /* Encode the AC coefficients per section F.1.2.2 */
  21.397 +  
  21.398 +  r = 0;			/* r = run length of zeros */
  21.399 +  
  21.400 +  for (k = 1; k < DCTSIZE2; k++) {
  21.401 +    if ((temp = block[jpeg_natural_order[k]]) == 0) {
  21.402 +      r++;
  21.403 +    } else {
  21.404 +      /* if run length > 15, must emit special run-length-16 codes (0xF0) */
  21.405 +      while (r > 15) {
  21.406 +	if (! emit_bits(state, actbl->ehufco[0xF0], actbl->ehufsi[0xF0]))
  21.407 +	  return FALSE;
  21.408 +	r -= 16;
  21.409 +      }
  21.410 +
  21.411 +      temp2 = temp;
  21.412 +      if (temp < 0) {
  21.413 +	temp = -temp;		/* temp is abs value of input */
  21.414 +	/* This code assumes we are on a two's complement machine */
  21.415 +	temp2--;
  21.416 +      }
  21.417 +      
  21.418 +      /* Find the number of bits needed for the magnitude of the coefficient */
  21.419 +      nbits = 1;		/* there must be at least one 1 bit */
  21.420 +      while ((temp >>= 1))
  21.421 +	nbits++;
  21.422 +      /* Check for out-of-range coefficient values */
  21.423 +      if (nbits > MAX_COEF_BITS)
  21.424 +	ERREXIT(state->cinfo, JERR_BAD_DCT_COEF);
  21.425 +      
  21.426 +      /* Emit Huffman symbol for run length / number of bits */
  21.427 +      i = (r << 4) + nbits;
  21.428 +      if (! emit_bits(state, actbl->ehufco[i], actbl->ehufsi[i]))
  21.429 +	return FALSE;
  21.430 +
  21.431 +      /* Emit that number of bits of the value, if positive, */
  21.432 +      /* or the complement of its magnitude, if negative. */
  21.433 +      if (! emit_bits(state, (unsigned int) temp2, nbits))
  21.434 +	return FALSE;
  21.435 +      
  21.436 +      r = 0;
  21.437 +    }
  21.438 +  }
  21.439 +
  21.440 +  /* If the last coef(s) were zero, emit an end-of-block code */
  21.441 +  if (r > 0)
  21.442 +    if (! emit_bits(state, actbl->ehufco[0], actbl->ehufsi[0]))
  21.443 +      return FALSE;
  21.444 +
  21.445 +  return TRUE;
  21.446 +}
  21.447 +
  21.448 +
  21.449 +/*
  21.450 + * Emit a restart marker & resynchronize predictions.
  21.451 + */
  21.452 +
  21.453 +LOCAL(boolean)
  21.454 +emit_restart (working_state * state, int restart_num)
  21.455 +{
  21.456 +  int ci;
  21.457 +
  21.458 +  if (! flush_bits(state))
  21.459 +    return FALSE;
  21.460 +
  21.461 +  emit_byte(state, 0xFF, return FALSE);
  21.462 +  emit_byte(state, JPEG_RST0 + restart_num, return FALSE);
  21.463 +
  21.464 +  /* Re-initialize DC predictions to 0 */
  21.465 +  for (ci = 0; ci < state->cinfo->comps_in_scan; ci++)
  21.466 +    state->cur.last_dc_val[ci] = 0;
  21.467 +
  21.468 +  /* The restart counter is not updated until we successfully write the MCU. */
  21.469 +
  21.470 +  return TRUE;
  21.471 +}
  21.472 +
  21.473 +
  21.474 +/*
  21.475 + * Encode and output one MCU's worth of Huffman-compressed coefficients.
  21.476 + */
  21.477 +
  21.478 +METHODDEF(boolean)
  21.479 +encode_mcu_huff (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
  21.480 +{
  21.481 +  huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
  21.482 +  working_state state;
  21.483 +  int blkn, ci;
  21.484 +  jpeg_component_info * compptr;
  21.485 +
  21.486 +  /* Load up working state */
  21.487 +  state.next_output_byte = cinfo->dest->next_output_byte;
  21.488 +  state.free_in_buffer = cinfo->dest->free_in_buffer;
  21.489 +  ASSIGN_STATE(state.cur, entropy->saved);
  21.490 +  state.cinfo = cinfo;
  21.491 +
  21.492 +  /* Emit restart marker if needed */
  21.493 +  if (cinfo->restart_interval) {
  21.494 +    if (entropy->restarts_to_go == 0)
  21.495 +      if (! emit_restart(&state, entropy->next_restart_num))
  21.496 +	return FALSE;
  21.497 +  }
  21.498 +
  21.499 +  /* Encode the MCU data blocks */
  21.500 +  for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
  21.501 +    ci = cinfo->MCU_membership[blkn];
  21.502 +    compptr = cinfo->cur_comp_info[ci];
  21.503 +    if (! encode_one_block(&state,
  21.504 +			   MCU_data[blkn][0], state.cur.last_dc_val[ci],
  21.505 +			   entropy->dc_derived_tbls[compptr->dc_tbl_no],
  21.506 +			   entropy->ac_derived_tbls[compptr->ac_tbl_no]))
  21.507 +      return FALSE;
  21.508 +    /* Update last_dc_val */
  21.509 +    state.cur.last_dc_val[ci] = MCU_data[blkn][0][0];
  21.510 +  }
  21.511 +
  21.512 +  /* Completed MCU, so update state */
  21.513 +  cinfo->dest->next_output_byte = state.next_output_byte;
  21.514 +  cinfo->dest->free_in_buffer = state.free_in_buffer;
  21.515 +  ASSIGN_STATE(entropy->saved, state.cur);
  21.516 +
  21.517 +  /* Update restart-interval state too */
  21.518 +  if (cinfo->restart_interval) {
  21.519 +    if (entropy->restarts_to_go == 0) {
  21.520 +      entropy->restarts_to_go = cinfo->restart_interval;
  21.521 +      entropy->next_restart_num++;
  21.522 +      entropy->next_restart_num &= 7;
  21.523 +    }
  21.524 +    entropy->restarts_to_go--;
  21.525 +  }
  21.526 +
  21.527 +  return TRUE;
  21.528 +}
  21.529 +
  21.530 +
  21.531 +/*
  21.532 + * Finish up at the end of a Huffman-compressed scan.
  21.533 + */
  21.534 +
  21.535 +METHODDEF(void)
  21.536 +finish_pass_huff (j_compress_ptr cinfo)
  21.537 +{
  21.538 +  huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
  21.539 +  working_state state;
  21.540 +
  21.541 +  /* Load up working state ... flush_bits needs it */
  21.542 +  state.next_output_byte = cinfo->dest->next_output_byte;
  21.543 +  state.free_in_buffer = cinfo->dest->free_in_buffer;
  21.544 +  ASSIGN_STATE(state.cur, entropy->saved);
  21.545 +  state.cinfo = cinfo;
  21.546 +
  21.547 +  /* Flush out the last data */
  21.548 +  if (! flush_bits(&state))
  21.549 +    ERREXIT(cinfo, JERR_CANT_SUSPEND);
  21.550 +
  21.551 +  /* Update state */
  21.552 +  cinfo->dest->next_output_byte = state.next_output_byte;
  21.553 +  cinfo->dest->free_in_buffer = state.free_in_buffer;
  21.554 +  ASSIGN_STATE(entropy->saved, state.cur);
  21.555 +}
  21.556 +
  21.557 +
  21.558 +/*
  21.559 + * Huffman coding optimization.
  21.560 + *
  21.561 + * We first scan the supplied data and count the number of uses of each symbol
  21.562 + * that is to be Huffman-coded. (This process MUST agree with the code above.)
  21.563 + * Then we build a Huffman coding tree for the observed counts.
  21.564 + * Symbols which are not needed at all for the particular image are not
  21.565 + * assigned any code, which saves space in the DHT marker as well as in
  21.566 + * the compressed data.
  21.567 + */
  21.568 +
  21.569 +#ifdef ENTROPY_OPT_SUPPORTED
  21.570 +
  21.571 +
  21.572 +/* Process a single block's worth of coefficients */
  21.573 +
  21.574 +LOCAL(void)
  21.575 +htest_one_block (j_compress_ptr cinfo, JCOEFPTR block, int last_dc_val,
  21.576 +		 long dc_counts[], long ac_counts[])
  21.577 +{
  21.578 +  register int temp;
  21.579 +  register int nbits;
  21.580 +  register int k, r;
  21.581 +  
  21.582 +  /* Encode the DC coefficient difference per section F.1.2.1 */
  21.583 +  
  21.584 +  temp = block[0] - last_dc_val;
  21.585 +  if (temp < 0)
  21.586 +    temp = -temp;
  21.587 +  
  21.588 +  /* Find the number of bits needed for the magnitude of the coefficient */
  21.589 +  nbits = 0;
  21.590 +  while (temp) {
  21.591 +    nbits++;
  21.592 +    temp >>= 1;
  21.593 +  }
  21.594 +  /* Check for out-of-range coefficient values.
  21.595 +   * Since we're encoding a difference, the range limit is twice as much.
  21.596 +   */
  21.597 +  if (nbits > MAX_COEF_BITS+1)
  21.598 +    ERREXIT(cinfo, JERR_BAD_DCT_COEF);
  21.599 +
  21.600 +  /* Count the Huffman symbol for the number of bits */
  21.601 +  dc_counts[nbits]++;
  21.602 +  
  21.603 +  /* Encode the AC coefficients per section F.1.2.2 */
  21.604 +  
  21.605 +  r = 0;			/* r = run length of zeros */
  21.606 +  
  21.607 +  for (k = 1; k < DCTSIZE2; k++) {
  21.608 +    if ((temp = block[jpeg_natural_order[k]]) == 0) {
  21.609 +      r++;
  21.610 +    } else {
  21.611 +      /* if run length > 15, must emit special run-length-16 codes (0xF0) */
  21.612 +      while (r > 15) {
  21.613 +	ac_counts[0xF0]++;
  21.614 +	r -= 16;
  21.615 +      }
  21.616 +      
  21.617 +      /* Find the number of bits needed for the magnitude of the coefficient */
  21.618 +      if (temp < 0)
  21.619 +	temp = -temp;
  21.620 +      
  21.621 +      /* Find the number of bits needed for the magnitude of the coefficient */
  21.622 +      nbits = 1;		/* there must be at least one 1 bit */
  21.623 +      while ((temp >>= 1))
  21.624 +	nbits++;
  21.625 +      /* Check for out-of-range coefficient values */
  21.626 +      if (nbits > MAX_COEF_BITS)
  21.627 +	ERREXIT(cinfo, JERR_BAD_DCT_COEF);
  21.628 +      
  21.629 +      /* Count Huffman symbol for run length / number of bits */
  21.630 +      ac_counts[(r << 4) + nbits]++;
  21.631 +      
  21.632 +      r = 0;
  21.633 +    }
  21.634 +  }
  21.635 +
  21.636 +  /* If the last coef(s) were zero, emit an end-of-block code */
  21.637 +  if (r > 0)
  21.638 +    ac_counts[0]++;
  21.639 +}
  21.640 +
  21.641 +
  21.642 +/*
  21.643 + * Trial-encode one MCU's worth of Huffman-compressed coefficients.
  21.644 + * No data is actually output, so no suspension return is possible.
  21.645 + */
  21.646 +
  21.647 +METHODDEF(boolean)
  21.648 +encode_mcu_gather (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
  21.649 +{
  21.650 +  huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
  21.651 +  int blkn, ci;
  21.652 +  jpeg_component_info * compptr;
  21.653 +
  21.654 +  /* Take care of restart intervals if needed */
  21.655 +  if (cinfo->restart_interval) {
  21.656 +    if (entropy->restarts_to_go == 0) {
  21.657 +      /* Re-initialize DC predictions to 0 */
  21.658 +      for (ci = 0; ci < cinfo->comps_in_scan; ci++)
  21.659 +	entropy->saved.last_dc_val[ci] = 0;
  21.660 +      /* Update restart state */
  21.661 +      entropy->restarts_to_go = cinfo->restart_interval;
  21.662 +    }
  21.663 +    entropy->restarts_to_go--;
  21.664 +  }
  21.665 +
  21.666 +  for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
  21.667 +    ci = cinfo->MCU_membership[blkn];
  21.668 +    compptr = cinfo->cur_comp_info[ci];
  21.669 +    htest_one_block(cinfo, MCU_data[blkn][0], entropy->saved.last_dc_val[ci],
  21.670 +		    entropy->dc_count_ptrs[compptr->dc_tbl_no],
  21.671 +		    entropy->ac_count_ptrs[compptr->ac_tbl_no]);
  21.672 +    entropy->saved.last_dc_val[ci] = MCU_data[blkn][0][0];
  21.673 +  }
  21.674 +
  21.675 +  return TRUE;
  21.676 +}
  21.677 +
  21.678 +
  21.679 +/*
  21.680 + * Generate the best Huffman code table for the given counts, fill htbl.
  21.681 + * Note this is also used by jcphuff.c.
  21.682 + *
  21.683 + * The JPEG standard requires that no symbol be assigned a codeword of all
  21.684 + * one bits (so that padding bits added at the end of a compressed segment
  21.685 + * can't look like a valid code).  Because of the canonical ordering of
  21.686 + * codewords, this just means that there must be an unused slot in the
  21.687 + * longest codeword length category.  Section K.2 of the JPEG spec suggests
  21.688 + * reserving such a slot by pretending that symbol 256 is a valid symbol
  21.689 + * with count 1.  In theory that's not optimal; giving it count zero but
  21.690 + * including it in the symbol set anyway should give a better Huffman code.
  21.691 + * But the theoretically better code actually seems to come out worse in
  21.692 + * practice, because it produces more all-ones bytes (which incur stuffed
  21.693 + * zero bytes in the final file).  In any case the difference is tiny.
  21.694 + *
  21.695 + * The JPEG standard requires Huffman codes to be no more than 16 bits long.
  21.696 + * If some symbols have a very small but nonzero probability, the Huffman tree
  21.697 + * must be adjusted to meet the code length restriction.  We currently use
  21.698 + * the adjustment method suggested in JPEG section K.2.  This method is *not*
  21.699 + * optimal; it may not choose the best possible limited-length code.  But
  21.700 + * typically only very-low-frequency symbols will be given less-than-optimal
  21.701 + * lengths, so the code is almost optimal.  Experimental comparisons against
  21.702 + * an optimal limited-length-code algorithm indicate that the difference is
  21.703 + * microscopic --- usually less than a hundredth of a percent of total size.
  21.704 + * So the extra complexity of an optimal algorithm doesn't seem worthwhile.
  21.705 + */
  21.706 +
  21.707 +GLOBAL(void)
  21.708 +jpeg_gen_optimal_table (j_compress_ptr cinfo, JHUFF_TBL * htbl, long freq[])
  21.709 +{
  21.710 +#define MAX_CLEN 32		/* assumed maximum initial code length */
  21.711 +  UINT8 bits[MAX_CLEN+1];	/* bits[k] = # of symbols with code length k */
  21.712 +  int codesize[257];		/* codesize[k] = code length of symbol k */
  21.713 +  int others[257];		/* next symbol in current branch of tree */
  21.714 +  int c1, c2;
  21.715 +  int p, i, j;
  21.716 +  long v;
  21.717 +
  21.718 +  /* This algorithm is explained in section K.2 of the JPEG standard */
  21.719 +
  21.720 +  MEMZERO(bits, SIZEOF(bits));
  21.721 +  MEMZERO(codesize, SIZEOF(codesize));
  21.722 +  for (i = 0; i < 257; i++)
  21.723 +    others[i] = -1;		/* init links to empty */
  21.724 +  
  21.725 +  freq[256] = 1;		/* make sure 256 has a nonzero count */
  21.726 +  /* Including the pseudo-symbol 256 in the Huffman procedure guarantees
  21.727 +   * that no real symbol is given code-value of all ones, because 256
  21.728 +   * will be placed last in the largest codeword category.
  21.729 +   */
  21.730 +
  21.731 +  /* Huffman's basic algorithm to assign optimal code lengths to symbols */
  21.732 +
  21.733 +  for (;;) {
  21.734 +    /* Find the smallest nonzero frequency, set c1 = its symbol */
  21.735 +    /* In case of ties, take the larger symbol number */
  21.736 +    c1 = -1;
  21.737 +    v = 1000000000L;
  21.738 +    for (i = 0; i <= 256; i++) {
  21.739 +      if (freq[i] && freq[i] <= v) {
  21.740 +	v = freq[i];
  21.741 +	c1 = i;
  21.742 +      }
  21.743 +    }
  21.744 +
  21.745 +    /* Find the next smallest nonzero frequency, set c2 = its symbol */
  21.746 +    /* In case of ties, take the larger symbol number */
  21.747 +    c2 = -1;
  21.748 +    v = 1000000000L;
  21.749 +    for (i = 0; i <= 256; i++) {
  21.750 +      if (freq[i] && freq[i] <= v && i != c1) {
  21.751 +	v = freq[i];
  21.752 +	c2 = i;
  21.753 +      }
  21.754 +    }
  21.755 +
  21.756 +    /* Done if we've merged everything into one frequency */
  21.757 +    if (c2 < 0)
  21.758 +      break;
  21.759 +    
  21.760 +    /* Else merge the two counts/trees */
  21.761 +    freq[c1] += freq[c2];
  21.762 +    freq[c2] = 0;
  21.763 +
  21.764 +    /* Increment the codesize of everything in c1's tree branch */
  21.765 +    codesize[c1]++;
  21.766 +    while (others[c1] >= 0) {
  21.767 +      c1 = others[c1];
  21.768 +      codesize[c1]++;
  21.769 +    }
  21.770 +    
  21.771 +    others[c1] = c2;		/* chain c2 onto c1's tree branch */
  21.772 +    
  21.773 +    /* Increment the codesize of everything in c2's tree branch */
  21.774 +    codesize[c2]++;
  21.775 +    while (others[c2] >= 0) {
  21.776 +      c2 = others[c2];
  21.777 +      codesize[c2]++;
  21.778 +    }
  21.779 +  }
  21.780 +
  21.781 +  /* Now count the number of symbols of each code length */
  21.782 +  for (i = 0; i <= 256; i++) {
  21.783 +    if (codesize[i]) {
  21.784 +      /* The JPEG standard seems to think that this can't happen, */
  21.785 +      /* but I'm paranoid... */
  21.786 +      if (codesize[i] > MAX_CLEN)
  21.787 +	ERREXIT(cinfo, JERR_HUFF_CLEN_OVERFLOW);
  21.788 +
  21.789 +      bits[codesize[i]]++;
  21.790 +    }
  21.791 +  }
  21.792 +
  21.793 +  /* JPEG doesn't allow symbols with code lengths over 16 bits, so if the pure
  21.794 +   * Huffman procedure assigned any such lengths, we must adjust the coding.
  21.795 +   * Here is what the JPEG spec says about how this next bit works:
  21.796 +   * Since symbols are paired for the longest Huffman code, the symbols are
  21.797 +   * removed from this length category two at a time.  The prefix for the pair
  21.798 +   * (which is one bit shorter) is allocated to one of the pair; then,
  21.799 +   * skipping the BITS entry for that prefix length, a code word from the next
  21.800 +   * shortest nonzero BITS entry is converted into a prefix for two code words
  21.801 +   * one bit longer.
  21.802 +   */
  21.803 +  
  21.804 +  for (i = MAX_CLEN; i > 16; i--) {
  21.805 +    while (bits[i] > 0) {
  21.806 +      j = i - 2;		/* find length of new prefix to be used */
  21.807 +      while (bits[j] == 0)
  21.808 +	j--;
  21.809 +      
  21.810 +      bits[i] -= 2;		/* remove two symbols */
  21.811 +      bits[i-1]++;		/* one goes in this length */
  21.812 +      bits[j+1] += 2;		/* two new symbols in this length */
  21.813 +      bits[j]--;		/* symbol of this length is now a prefix */
  21.814 +    }
  21.815 +  }
  21.816 +
  21.817 +  /* Remove the count for the pseudo-symbol 256 from the largest codelength */
  21.818 +  while (bits[i] == 0)		/* find largest codelength still in use */
  21.819 +    i--;
  21.820 +  bits[i]--;
  21.821 +  
  21.822 +  /* Return final symbol counts (only for lengths 0..16) */
  21.823 +  MEMCOPY(htbl->bits, bits, SIZEOF(htbl->bits));
  21.824 +  
  21.825 +  /* Return a list of the symbols sorted by code length */
  21.826 +  /* It's not real clear to me why we don't need to consider the codelength
  21.827 +   * changes made above, but the JPEG spec seems to think this works.
  21.828 +   */
  21.829 +  p = 0;
  21.830 +  for (i = 1; i <= MAX_CLEN; i++) {
  21.831 +    for (j = 0; j <= 255; j++) {
  21.832 +      if (codesize[j] == i) {
  21.833 +	htbl->huffval[p] = (UINT8) j;
  21.834 +	p++;
  21.835 +      }
  21.836 +    }
  21.837 +  }
  21.838 +
  21.839 +  /* Set sent_table FALSE so updated table will be written to JPEG file. */
  21.840 +  htbl->sent_table = FALSE;
  21.841 +}
  21.842 +
  21.843 +
  21.844 +/*
  21.845 + * Finish up a statistics-gathering pass and create the new Huffman tables.
  21.846 + */
  21.847 +
  21.848 +METHODDEF(void)
  21.849 +finish_pass_gather (j_compress_ptr cinfo)
  21.850 +{
  21.851 +  huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
  21.852 +  int ci, dctbl, actbl;
  21.853 +  jpeg_component_info * compptr;
  21.854 +  JHUFF_TBL **htblptr;
  21.855 +  boolean did_dc[NUM_HUFF_TBLS];
  21.856 +  boolean did_ac[NUM_HUFF_TBLS];
  21.857 +
  21.858 +  /* It's important not to apply jpeg_gen_optimal_table more than once
  21.859 +   * per table, because it clobbers the input frequency counts!
  21.860 +   */
  21.861 +  MEMZERO(did_dc, SIZEOF(did_dc));
  21.862 +  MEMZERO(did_ac, SIZEOF(did_ac));
  21.863 +
  21.864 +  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  21.865 +    compptr = cinfo->cur_comp_info[ci];
  21.866 +    dctbl = compptr->dc_tbl_no;
  21.867 +    actbl = compptr->ac_tbl_no;
  21.868 +    if (! did_dc[dctbl]) {
  21.869 +      htblptr = & cinfo->dc_huff_tbl_ptrs[dctbl];
  21.870 +      if (*htblptr == NULL)
  21.871 +	*htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
  21.872 +      jpeg_gen_optimal_table(cinfo, *htblptr, entropy->dc_count_ptrs[dctbl]);
  21.873 +      did_dc[dctbl] = TRUE;
  21.874 +    }
  21.875 +    if (! did_ac[actbl]) {
  21.876 +      htblptr = & cinfo->ac_huff_tbl_ptrs[actbl];
  21.877 +      if (*htblptr == NULL)
  21.878 +	*htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
  21.879 +      jpeg_gen_optimal_table(cinfo, *htblptr, entropy->ac_count_ptrs[actbl]);
  21.880 +      did_ac[actbl] = TRUE;
  21.881 +    }
  21.882 +  }
  21.883 +}
  21.884 +
  21.885 +
  21.886 +#endif /* ENTROPY_OPT_SUPPORTED */
  21.887 +
  21.888 +
  21.889 +/*
  21.890 + * Module initialization routine for Huffman entropy encoding.
  21.891 + */
  21.892 +
  21.893 +GLOBAL(void)
  21.894 +jinit_huff_encoder (j_compress_ptr cinfo)
  21.895 +{
  21.896 +  huff_entropy_ptr entropy;
  21.897 +  int i;
  21.898 +
  21.899 +  entropy = (huff_entropy_ptr)
  21.900 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  21.901 +				SIZEOF(huff_entropy_encoder));
  21.902 +  cinfo->entropy = (struct jpeg_entropy_encoder *) entropy;
  21.903 +  entropy->pub.start_pass = start_pass_huff;
  21.904 +
  21.905 +  /* Mark tables unallocated */
  21.906 +  for (i = 0; i < NUM_HUFF_TBLS; i++) {
  21.907 +    entropy->dc_derived_tbls[i] = entropy->ac_derived_tbls[i] = NULL;
  21.908 +#ifdef ENTROPY_OPT_SUPPORTED
  21.909 +    entropy->dc_count_ptrs[i] = entropy->ac_count_ptrs[i] = NULL;
  21.910 +#endif
  21.911 +  }
  21.912 +}
    22.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    22.2 +++ b/libs/libjpeg/jchuff.h	Sat Sep 19 05:51:51 2015 +0300
    22.3 @@ -0,0 +1,47 @@
    22.4 +/*
    22.5 + * jchuff.h
    22.6 + *
    22.7 + * Copyright (C) 1991-1997, Thomas G. Lane.
    22.8 + * This file is part of the Independent JPEG Group's software.
    22.9 + * For conditions of distribution and use, see the accompanying README file.
   22.10 + *
   22.11 + * This file contains declarations for Huffman entropy encoding routines
   22.12 + * that are shared between the sequential encoder (jchuff.c) and the
   22.13 + * progressive encoder (jcphuff.c).  No other modules need to see these.
   22.14 + */
   22.15 +
   22.16 +/* The legal range of a DCT coefficient is
   22.17 + *  -1024 .. +1023  for 8-bit data;
   22.18 + * -16384 .. +16383 for 12-bit data.
   22.19 + * Hence the magnitude should always fit in 10 or 14 bits respectively.
   22.20 + */
   22.21 +
   22.22 +#if BITS_IN_JSAMPLE == 8
   22.23 +#define MAX_COEF_BITS 10
   22.24 +#else
   22.25 +#define MAX_COEF_BITS 14
   22.26 +#endif
   22.27 +
   22.28 +/* Derived data constructed for each Huffman table */
   22.29 +
   22.30 +typedef struct {
   22.31 +  unsigned int ehufco[256];	/* code for each symbol */
   22.32 +  char ehufsi[256];		/* length of code for each symbol */
   22.33 +  /* If no code has been allocated for a symbol S, ehufsi[S] contains 0 */
   22.34 +} c_derived_tbl;
   22.35 +
   22.36 +/* Short forms of external names for systems with brain-damaged linkers. */
   22.37 +
   22.38 +#ifdef NEED_SHORT_EXTERNAL_NAMES
   22.39 +#define jpeg_make_c_derived_tbl	jMkCDerived
   22.40 +#define jpeg_gen_optimal_table	jGenOptTbl
   22.41 +#endif /* NEED_SHORT_EXTERNAL_NAMES */
   22.42 +
   22.43 +/* Expand a Huffman table definition into the derived format */
   22.44 +EXTERN(void) jpeg_make_c_derived_tbl
   22.45 +	JPP((j_compress_ptr cinfo, boolean isDC, int tblno,
   22.46 +	     c_derived_tbl ** pdtbl));
   22.47 +
   22.48 +/* Generate an optimal table definition given the specified counts */
   22.49 +EXTERN(void) jpeg_gen_optimal_table
   22.50 +	JPP((j_compress_ptr cinfo, JHUFF_TBL * htbl, long freq[]));
    23.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    23.2 +++ b/libs/libjpeg/jcinit.c	Sat Sep 19 05:51:51 2015 +0300
    23.3 @@ -0,0 +1,72 @@
    23.4 +/*
    23.5 + * jcinit.c
    23.6 + *
    23.7 + * Copyright (C) 1991-1997, Thomas G. Lane.
    23.8 + * This file is part of the Independent JPEG Group's software.
    23.9 + * For conditions of distribution and use, see the accompanying README file.
   23.10 + *
   23.11 + * This file contains initialization logic for the JPEG compressor.
   23.12 + * This routine is in charge of selecting the modules to be executed and
   23.13 + * making an initialization call to each one.
   23.14 + *
   23.15 + * Logically, this code belongs in jcmaster.c.  It's split out because
   23.16 + * linking this routine implies linking the entire compression library.
   23.17 + * For a transcoding-only application, we want to be able to use jcmaster.c
   23.18 + * without linking in the whole library.
   23.19 + */
   23.20 +
   23.21 +#define JPEG_INTERNALS
   23.22 +#include "jinclude.h"
   23.23 +#include "jpeglib.h"
   23.24 +
   23.25 +
   23.26 +/*
   23.27 + * Master selection of compression modules.
   23.28 + * This is done once at the start of processing an image.  We determine
   23.29 + * which modules will be used and give them appropriate initialization calls.
   23.30 + */
   23.31 +
   23.32 +GLOBAL(void)
   23.33 +jinit_compress_master (j_compress_ptr cinfo)
   23.34 +{
   23.35 +  /* Initialize master control (includes parameter checking/processing) */
   23.36 +  jinit_c_master_control(cinfo, FALSE /* full compression */);
   23.37 +
   23.38 +  /* Preprocessing */
   23.39 +  if (! cinfo->raw_data_in) {
   23.40 +    jinit_color_converter(cinfo);
   23.41 +    jinit_downsampler(cinfo);
   23.42 +    jinit_c_prep_controller(cinfo, FALSE /* never need full buffer here */);
   23.43 +  }
   23.44 +  /* Forward DCT */
   23.45 +  jinit_forward_dct(cinfo);
   23.46 +  /* Entropy encoding: either Huffman or arithmetic coding. */
   23.47 +  if (cinfo->arith_code) {
   23.48 +    ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
   23.49 +  } else {
   23.50 +    if (cinfo->progressive_mode) {
   23.51 +#ifdef C_PROGRESSIVE_SUPPORTED
   23.52 +      jinit_phuff_encoder(cinfo);
   23.53 +#else
   23.54 +      ERREXIT(cinfo, JERR_NOT_COMPILED);
   23.55 +#endif
   23.56 +    } else
   23.57 +      jinit_huff_encoder(cinfo);
   23.58 +  }
   23.59 +
   23.60 +  /* Need a full-image coefficient buffer in any multi-pass mode. */
   23.61 +  jinit_c_coef_controller(cinfo,
   23.62 +		(boolean) (cinfo->num_scans > 1 || cinfo->optimize_coding));
   23.63 +  jinit_c_main_controller(cinfo, FALSE /* never need full buffer here */);
   23.64 +
   23.65 +  jinit_marker_writer(cinfo);
   23.66 +
   23.67 +  /* We can now tell the memory manager to allocate virtual arrays. */
   23.68 +  (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);
   23.69 +
   23.70 +  /* Write the datastream header (SOI) immediately.
   23.71 +   * Frame and scan headers are postponed till later.
   23.72 +   * This lets application insert special markers after the SOI.
   23.73 +   */
   23.74 +  (*cinfo->marker->write_file_header) (cinfo);
   23.75 +}
    24.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    24.2 +++ b/libs/libjpeg/jcmainct.c	Sat Sep 19 05:51:51 2015 +0300
    24.3 @@ -0,0 +1,293 @@
    24.4 +/*
    24.5 + * jcmainct.c
    24.6 + *
    24.7 + * Copyright (C) 1994-1996, Thomas G. Lane.
    24.8 + * This file is part of the Independent JPEG Group's software.
    24.9 + * For conditions of distribution and use, see the accompanying README file.
   24.10 + *
   24.11 + * This file contains the main buffer controller for compression.
   24.12 + * The main buffer lies between the pre-processor and the JPEG
   24.13 + * compressor proper; it holds downsampled data in the JPEG colorspace.
   24.14 + */
   24.15 +
   24.16 +#define JPEG_INTERNALS
   24.17 +#include "jinclude.h"
   24.18 +#include "jpeglib.h"
   24.19 +
   24.20 +
   24.21 +/* Note: currently, there is no operating mode in which a full-image buffer
   24.22 + * is needed at this step.  If there were, that mode could not be used with
   24.23 + * "raw data" input, since this module is bypassed in that case.  However,
   24.24 + * we've left the code here for possible use in special applications.
   24.25 + */
   24.26 +#undef FULL_MAIN_BUFFER_SUPPORTED
   24.27 +
   24.28 +
   24.29 +/* Private buffer controller object */
   24.30 +
   24.31 +typedef struct {
   24.32 +  struct jpeg_c_main_controller pub; /* public fields */
   24.33 +
   24.34 +  JDIMENSION cur_iMCU_row;	/* number of current iMCU row */
   24.35 +  JDIMENSION rowgroup_ctr;	/* counts row groups received in iMCU row */
   24.36 +  boolean suspended;		/* remember if we suspended output */
   24.37 +  J_BUF_MODE pass_mode;		/* current operating mode */
   24.38 +
   24.39 +  /* If using just a strip buffer, this points to the entire set of buffers
   24.40 +   * (we allocate one for each component).  In the full-image case, this
   24.41 +   * points to the currently accessible strips of the virtual arrays.
   24.42 +   */
   24.43 +  JSAMPARRAY buffer[MAX_COMPONENTS];
   24.44 +
   24.45 +#ifdef FULL_MAIN_BUFFER_SUPPORTED
   24.46 +  /* If using full-image storage, this array holds pointers to virtual-array
   24.47 +   * control blocks for each component.  Unused if not full-image storage.
   24.48 +   */
   24.49 +  jvirt_sarray_ptr whole_image[MAX_COMPONENTS];
   24.50 +#endif
   24.51 +} my_main_controller;
   24.52 +
   24.53 +typedef my_main_controller * my_main_ptr;
   24.54 +
   24.55 +
   24.56 +/* Forward declarations */
   24.57 +METHODDEF(void) process_data_simple_main
   24.58 +	JPP((j_compress_ptr cinfo, JSAMPARRAY input_buf,
   24.59 +	     JDIMENSION *in_row_ctr, JDIMENSION in_rows_avail));
   24.60 +#ifdef FULL_MAIN_BUFFER_SUPPORTED
   24.61 +METHODDEF(void) process_data_buffer_main
   24.62 +	JPP((j_compress_ptr cinfo, JSAMPARRAY input_buf,
   24.63 +	     JDIMENSION *in_row_ctr, JDIMENSION in_rows_avail));
   24.64 +#endif
   24.65 +
   24.66 +
   24.67 +/*
   24.68 + * Initialize for a processing pass.
   24.69 + */
   24.70 +
   24.71 +METHODDEF(void)
   24.72 +start_pass_main (j_compress_ptr cinfo, J_BUF_MODE pass_mode)
   24.73 +{
   24.74 +  my_main_ptr main = (my_main_ptr) cinfo->main;
   24.75 +
   24.76 +  /* Do nothing in raw-data mode. */
   24.77 +  if (cinfo->raw_data_in)
   24.78 +    return;
   24.79 +
   24.80 +  main->cur_iMCU_row = 0;	/* initialize counters */
   24.81 +  main->rowgroup_ctr = 0;
   24.82 +  main->suspended = FALSE;
   24.83 +  main->pass_mode = pass_mode;	/* save mode for use by process_data */
   24.84 +
   24.85 +  switch (pass_mode) {
   24.86 +  case JBUF_PASS_THRU:
   24.87 +#ifdef FULL_MAIN_BUFFER_SUPPORTED
   24.88 +    if (main->whole_image[0] != NULL)
   24.89 +      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
   24.90 +#endif
   24.91 +    main->pub.process_data = process_data_simple_main;
   24.92 +    break;
   24.93 +#ifdef FULL_MAIN_BUFFER_SUPPORTED
   24.94 +  case JBUF_SAVE_SOURCE:
   24.95 +  case JBUF_CRANK_DEST:
   24.96 +  case JBUF_SAVE_AND_PASS:
   24.97 +    if (main->whole_image[0] == NULL)
   24.98 +      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
   24.99 +    main->pub.process_data = process_data_buffer_main;
  24.100 +    break;
  24.101 +#endif
  24.102 +  default:
  24.103 +    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  24.104 +    break;
  24.105 +  }
  24.106 +}
  24.107 +
  24.108 +
  24.109 +/*
  24.110 + * Process some data.
  24.111 + * This routine handles the simple pass-through mode,
  24.112 + * where we have only a strip buffer.
  24.113 + */
  24.114 +
  24.115 +METHODDEF(void)
  24.116 +process_data_simple_main (j_compress_ptr cinfo,
  24.117 +			  JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
  24.118 +			  JDIMENSION in_rows_avail)
  24.119 +{
  24.120 +  my_main_ptr main = (my_main_ptr) cinfo->main;
  24.121 +
  24.122 +  while (main->cur_iMCU_row < cinfo->total_iMCU_rows) {
  24.123 +    /* Read input data if we haven't filled the main buffer yet */
  24.124 +    if (main->rowgroup_ctr < DCTSIZE)
  24.125 +      (*cinfo->prep->pre_process_data) (cinfo,
  24.126 +					input_buf, in_row_ctr, in_rows_avail,
  24.127 +					main->buffer, &main->rowgroup_ctr,
  24.128 +					(JDIMENSION) DCTSIZE);
  24.129 +
  24.130 +    /* If we don't have a full iMCU row buffered, return to application for
  24.131 +     * more data.  Note that preprocessor will always pad to fill the iMCU row
  24.132 +     * at the bottom of the image.
  24.133 +     */
  24.134 +    if (main->rowgroup_ctr != DCTSIZE)
  24.135 +      return;
  24.136 +
  24.137 +    /* Send the completed row to the compressor */
  24.138 +    if (! (*cinfo->coef->compress_data) (cinfo, main->buffer)) {
  24.139 +      /* If compressor did not consume the whole row, then we must need to
  24.140 +       * suspend processing and return to the application.  In this situation
  24.141 +       * we pretend we didn't yet consume the last input row; otherwise, if
  24.142 +       * it happened to be the last row of the image, the application would
  24.143 +       * think we were done.
  24.144 +       */
  24.145 +      if (! main->suspended) {
  24.146 +	(*in_row_ctr)--;
  24.147 +	main->suspended = TRUE;
  24.148 +      }
  24.149 +      return;
  24.150 +    }
  24.151 +    /* We did finish the row.  Undo our little suspension hack if a previous
  24.152 +     * call suspended; then mark the main buffer empty.
  24.153 +     */
  24.154 +    if (main->suspended) {
  24.155 +      (*in_row_ctr)++;
  24.156 +      main->suspended = FALSE;
  24.157 +    }
  24.158 +    main->rowgroup_ctr = 0;
  24.159 +    main->cur_iMCU_row++;
  24.160 +  }
  24.161 +}
  24.162 +
  24.163 +
  24.164 +#ifdef FULL_MAIN_BUFFER_SUPPORTED
  24.165 +
  24.166 +/*
  24.167 + * Process some data.
  24.168 + * This routine handles all of the modes that use a full-size buffer.
  24.169 + */
  24.170 +
  24.171 +METHODDEF(void)
  24.172 +process_data_buffer_main (j_compress_ptr cinfo,
  24.173 +			  JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
  24.174 +			  JDIMENSION in_rows_avail)
  24.175 +{
  24.176 +  my_main_ptr main = (my_main_ptr) cinfo->main;
  24.177 +  int ci;
  24.178 +  jpeg_component_info *compptr;
  24.179 +  boolean writing = (main->pass_mode != JBUF_CRANK_DEST);
  24.180 +
  24.181 +  while (main->cur_iMCU_row < cinfo->total_iMCU_rows) {
  24.182 +    /* Realign the virtual buffers if at the start of an iMCU row. */
  24.183 +    if (main->rowgroup_ctr == 0) {
  24.184 +      for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  24.185 +	   ci++, compptr++) {
  24.186 +	main->buffer[ci] = (*cinfo->mem->access_virt_sarray)
  24.187 +	  ((j_common_ptr) cinfo, main->whole_image[ci],
  24.188 +	   main->cur_iMCU_row * (compptr->v_samp_factor * DCTSIZE),
  24.189 +	   (JDIMENSION) (compptr->v_samp_factor * DCTSIZE), writing);
  24.190 +      }
  24.191 +      /* In a read pass, pretend we just read some source data. */
  24.192 +      if (! writing) {
  24.193 +	*in_row_ctr += cinfo->max_v_samp_factor * DCTSIZE;
  24.194 +	main->rowgroup_ctr = DCTSIZE;
  24.195 +      }
  24.196 +    }
  24.197 +
  24.198 +    /* If a write pass, read input data until the current iMCU row is full. */
  24.199 +    /* Note: preprocessor will pad if necessary to fill the last iMCU row. */
  24.200 +    if (writing) {
  24.201 +      (*cinfo->prep->pre_process_data) (cinfo,
  24.202 +					input_buf, in_row_ctr, in_rows_avail,
  24.203 +					main->buffer, &main->rowgroup_ctr,
  24.204 +					(JDIMENSION) DCTSIZE);
  24.205 +      /* Return to application if we need more data to fill the iMCU row. */
  24.206 +      if (main->rowgroup_ctr < DCTSIZE)
  24.207 +	return;
  24.208 +    }
  24.209 +
  24.210 +    /* Emit data, unless this is a sink-only pass. */
  24.211 +    if (main->pass_mode != JBUF_SAVE_SOURCE) {
  24.212 +      if (! (*cinfo->coef->compress_data) (cinfo, main->buffer)) {
  24.213 +	/* If compressor did not consume the whole row, then we must need to
  24.214 +	 * suspend processing and return to the application.  In this situation
  24.215 +	 * we pretend we didn't yet consume the last input row; otherwise, if
  24.216 +	 * it happened to be the last row of the image, the application would
  24.217 +	 * think we were done.
  24.218 +	 */
  24.219 +	if (! main->suspended) {
  24.220 +	  (*in_row_ctr)--;
  24.221 +	  main->suspended = TRUE;
  24.222 +	}
  24.223 +	return;
  24.224 +      }
  24.225 +      /* We did finish the row.  Undo our little suspension hack if a previous
  24.226 +       * call suspended; then mark the main buffer empty.
  24.227 +       */
  24.228 +      if (main->suspended) {
  24.229 +	(*in_row_ctr)++;
  24.230 +	main->suspended = FALSE;
  24.231 +      }
  24.232 +    }
  24.233 +
  24.234 +    /* If get here, we are done with this iMCU row.  Mark buffer empty. */
  24.235 +    main->rowgroup_ctr = 0;
  24.236 +    main->cur_iMCU_row++;
  24.237 +  }
  24.238 +}
  24.239 +
  24.240 +#endif /* FULL_MAIN_BUFFER_SUPPORTED */
  24.241 +
  24.242 +
  24.243 +/*
  24.244 + * Initialize main buffer controller.
  24.245 + */
  24.246 +
  24.247 +GLOBAL(void)
  24.248 +jinit_c_main_controller (j_compress_ptr cinfo, boolean need_full_buffer)
  24.249 +{
  24.250 +  my_main_ptr main;
  24.251 +  int ci;
  24.252 +  jpeg_component_info *compptr;
  24.253 +
  24.254 +  main = (my_main_ptr)
  24.255 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  24.256 +				SIZEOF(my_main_controller));
  24.257 +  cinfo->main = (struct jpeg_c_main_controller *) main;
  24.258 +  main->pub.start_pass = start_pass_main;
  24.259 +
  24.260 +  /* We don't need to create a buffer in raw-data mode. */
  24.261 +  if (cinfo->raw_data_in)
  24.262 +    return;
  24.263 +
  24.264 +  /* Create the buffer.  It holds downsampled data, so each component
  24.265 +   * may be of a different size.
  24.266 +   */
  24.267 +  if (need_full_buffer) {
  24.268 +#ifdef FULL_MAIN_BUFFER_SUPPORTED
  24.269 +    /* Allocate a full-image virtual array for each component */
  24.270 +    /* Note we pad the bottom to a multiple of the iMCU height */
  24.271 +    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  24.272 +	 ci++, compptr++) {
  24.273 +      main->whole_image[ci] = (*cinfo->mem->request_virt_sarray)
  24.274 +	((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE,
  24.275 +	 compptr->width_in_blocks * DCTSIZE,
  24.276 +	 (JDIMENSION) jround_up((long) compptr->height_in_blocks,
  24.277 +				(long) compptr->v_samp_factor) * DCTSIZE,
  24.278 +	 (JDIMENSION) (compptr->v_samp_factor * DCTSIZE));
  24.279 +    }
  24.280 +#else
  24.281 +    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  24.282 +#endif
  24.283 +  } else {
  24.284 +#ifdef FULL_MAIN_BUFFER_SUPPORTED
  24.285 +    main->whole_image[0] = NULL; /* flag for no virtual arrays */
  24.286 +#endif
  24.287 +    /* Allocate a strip buffer for each component */
  24.288 +    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  24.289 +	 ci++, compptr++) {
  24.290 +      main->buffer[ci] = (*cinfo->mem->alloc_sarray)
  24.291 +	((j_common_ptr) cinfo, JPOOL_IMAGE,
  24.292 +	 compptr->width_in_blocks * DCTSIZE,
  24.293 +	 (JDIMENSION) (compptr->v_samp_factor * DCTSIZE));
  24.294 +    }
  24.295 +  }
  24.296 +}
    25.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    25.2 +++ b/libs/libjpeg/jcmarker.c	Sat Sep 19 05:51:51 2015 +0300
    25.3 @@ -0,0 +1,664 @@
    25.4 +/*
    25.5 + * jcmarker.c
    25.6 + *
    25.7 + * Copyright (C) 1991-1998, Thomas G. Lane.
    25.8 + * This file is part of the Independent JPEG Group's software.
    25.9 + * For conditions of distribution and use, see the accompanying README file.
   25.10 + *
   25.11 + * This file contains routines to write JPEG datastream markers.
   25.12 + */
   25.13 +
   25.14 +#define JPEG_INTERNALS
   25.15 +#include "jinclude.h"
   25.16 +#include "jpeglib.h"
   25.17 +
   25.18 +
   25.19 +typedef enum {			/* JPEG marker codes */
   25.20 +  M_SOF0  = 0xc0,
   25.21 +  M_SOF1  = 0xc1,
   25.22 +  M_SOF2  = 0xc2,
   25.23 +  M_SOF3  = 0xc3,
   25.24 +  
   25.25 +  M_SOF5  = 0xc5,
   25.26 +  M_SOF6  = 0xc6,
   25.27 +  M_SOF7  = 0xc7,
   25.28 +  
   25.29 +  M_JPG   = 0xc8,
   25.30 +  M_SOF9  = 0xc9,
   25.31 +  M_SOF10 = 0xca,
   25.32 +  M_SOF11 = 0xcb,
   25.33 +  
   25.34 +  M_SOF13 = 0xcd,
   25.35 +  M_SOF14 = 0xce,
   25.36 +  M_SOF15 = 0xcf,
   25.37 +  
   25.38 +  M_DHT   = 0xc4,
   25.39 +  
   25.40 +  M_DAC   = 0xcc,
   25.41 +  
   25.42 +  M_RST0  = 0xd0,
   25.43 +  M_RST1  = 0xd1,
   25.44 +  M_RST2  = 0xd2,
   25.45 +  M_RST3  = 0xd3,
   25.46 +  M_RST4  = 0xd4,
   25.47 +  M_RST5  = 0xd5,
   25.48 +  M_RST6  = 0xd6,
   25.49 +  M_RST7  = 0xd7,
   25.50 +  
   25.51 +  M_SOI   = 0xd8,
   25.52 +  M_EOI   = 0xd9,
   25.53 +  M_SOS   = 0xda,
   25.54 +  M_DQT   = 0xdb,
   25.55 +  M_DNL   = 0xdc,
   25.56 +  M_DRI   = 0xdd,
   25.57 +  M_DHP   = 0xde,
   25.58 +  M_EXP   = 0xdf,
   25.59 +  
   25.60 +  M_APP0  = 0xe0,
   25.61 +  M_APP1  = 0xe1,
   25.62 +  M_APP2  = 0xe2,
   25.63 +  M_APP3  = 0xe3,
   25.64 +  M_APP4  = 0xe4,
   25.65 +  M_APP5  = 0xe5,
   25.66 +  M_APP6  = 0xe6,
   25.67 +  M_APP7  = 0xe7,
   25.68 +  M_APP8  = 0xe8,
   25.69 +  M_APP9  = 0xe9,
   25.70 +  M_APP10 = 0xea,
   25.71 +  M_APP11 = 0xeb,
   25.72 +  M_APP12 = 0xec,
   25.73 +  M_APP13 = 0xed,
   25.74 +  M_APP14 = 0xee,
   25.75 +  M_APP15 = 0xef,
   25.76 +  
   25.77 +  M_JPG0  = 0xf0,
   25.78 +  M_JPG13 = 0xfd,
   25.79 +  M_COM   = 0xfe,
   25.80 +  
   25.81 +  M_TEM   = 0x01,
   25.82 +  
   25.83 +  M_ERROR = 0x100
   25.84 +} JPEG_MARKER;
   25.85 +
   25.86 +
   25.87 +/* Private state */
   25.88 +
   25.89 +typedef struct {
   25.90 +  struct jpeg_marker_writer pub; /* public fields */
   25.91 +
   25.92 +  unsigned int last_restart_interval; /* last DRI value emitted; 0 after SOI */
   25.93 +} my_marker_writer;
   25.94 +
   25.95 +typedef my_marker_writer * my_marker_ptr;
   25.96 +
   25.97 +
   25.98 +/*
   25.99 + * Basic output routines.
  25.100 + *
  25.101 + * Note that we do not support suspension while writing a marker.
  25.102 + * Therefore, an application using suspension must ensure that there is
  25.103 + * enough buffer space for the initial markers (typ. 600-700 bytes) before
  25.104 + * calling jpeg_start_compress, and enough space to write the trailing EOI
  25.105 + * (a few bytes) before calling jpeg_finish_compress.  Multipass compression
  25.106 + * modes are not supported at all with suspension, so those two are the only
  25.107 + * points where markers will be written.
  25.108 + */
  25.109 +
  25.110 +LOCAL(void)
  25.111 +emit_byte (j_compress_ptr cinfo, int val)
  25.112 +/* Emit a byte */
  25.113 +{
  25.114 +  struct jpeg_destination_mgr * dest = cinfo->dest;
  25.115 +
  25.116 +  *(dest->next_output_byte)++ = (JOCTET) val;
  25.117 +  if (--dest->free_in_buffer == 0) {
  25.118 +    if (! (*dest->empty_output_buffer) (cinfo))
  25.119 +      ERREXIT(cinfo, JERR_CANT_SUSPEND);
  25.120 +  }
  25.121 +}
  25.122 +
  25.123 +
  25.124 +LOCAL(void)
  25.125 +emit_marker (j_compress_ptr cinfo, JPEG_MARKER mark)
  25.126 +/* Emit a marker code */
  25.127 +{
  25.128 +  emit_byte(cinfo, 0xFF);
  25.129 +  emit_byte(cinfo, (int) mark);
  25.130 +}
  25.131 +
  25.132 +
  25.133 +LOCAL(void)
  25.134 +emit_2bytes (j_compress_ptr cinfo, int value)
  25.135 +/* Emit a 2-byte integer; these are always MSB first in JPEG files */
  25.136 +{
  25.137 +  emit_byte(cinfo, (value >> 8) & 0xFF);
  25.138 +  emit_byte(cinfo, value & 0xFF);
  25.139 +}
  25.140 +
  25.141 +
  25.142 +/*
  25.143 + * Routines to write specific marker types.
  25.144 + */
  25.145 +
  25.146 +LOCAL(int)
  25.147 +emit_dqt (j_compress_ptr cinfo, int index)
  25.148 +/* Emit a DQT marker */
  25.149 +/* Returns the precision used (0 = 8bits, 1 = 16bits) for baseline checking */
  25.150 +{
  25.151 +  JQUANT_TBL * qtbl = cinfo->quant_tbl_ptrs[index];
  25.152 +  int prec;
  25.153 +  int i;
  25.154 +
  25.155 +  if (qtbl == NULL)
  25.156 +    ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, index);
  25.157 +
  25.158 +  prec = 0;
  25.159 +  for (i = 0; i < DCTSIZE2; i++) {
  25.160 +    if (qtbl->quantval[i] > 255)
  25.161 +      prec = 1;
  25.162 +  }
  25.163 +
  25.164 +  if (! qtbl->sent_table) {
  25.165 +    emit_marker(cinfo, M_DQT);
  25.166 +
  25.167 +    emit_2bytes(cinfo, prec ? DCTSIZE2*2 + 1 + 2 : DCTSIZE2 + 1 + 2);
  25.168 +
  25.169 +    emit_byte(cinfo, index + (prec<<4));
  25.170 +
  25.171 +    for (i = 0; i < DCTSIZE2; i++) {
  25.172 +      /* The table entries must be emitted in zigzag order. */
  25.173 +      unsigned int qval = qtbl->quantval[jpeg_natural_order[i]];
  25.174 +      if (prec)
  25.175 +	emit_byte(cinfo, (int) (qval >> 8));
  25.176 +      emit_byte(cinfo, (int) (qval & 0xFF));
  25.177 +    }
  25.178 +
  25.179 +    qtbl->sent_table = TRUE;
  25.180 +  }
  25.181 +
  25.182 +  return prec;
  25.183 +}
  25.184 +
  25.185 +
  25.186 +LOCAL(void)
  25.187 +emit_dht (j_compress_ptr cinfo, int index, boolean is_ac)
  25.188 +/* Emit a DHT marker */
  25.189 +{
  25.190 +  JHUFF_TBL * htbl;
  25.191 +  int length, i;
  25.192 +  
  25.193 +  if (is_ac) {
  25.194 +    htbl = cinfo->ac_huff_tbl_ptrs[index];
  25.195 +    index += 0x10;		/* output index has AC bit set */
  25.196 +  } else {
  25.197 +    htbl = cinfo->dc_huff_tbl_ptrs[index];
  25.198 +  }
  25.199 +
  25.200 +  if (htbl == NULL)
  25.201 +    ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, index);
  25.202 +  
  25.203 +  if (! htbl->sent_table) {
  25.204 +    emit_marker(cinfo, M_DHT);
  25.205 +    
  25.206 +    length = 0;
  25.207 +    for (i = 1; i <= 16; i++)
  25.208 +      length += htbl->bits[i];
  25.209 +    
  25.210 +    emit_2bytes(cinfo, length + 2 + 1 + 16);
  25.211 +    emit_byte(cinfo, index);
  25.212 +    
  25.213 +    for (i = 1; i <= 16; i++)
  25.214 +      emit_byte(cinfo, htbl->bits[i]);
  25.215 +    
  25.216 +    for (i = 0; i < length; i++)
  25.217 +      emit_byte(cinfo, htbl->huffval[i]);
  25.218 +    
  25.219 +    htbl->sent_table = TRUE;
  25.220 +  }
  25.221 +}
  25.222 +
  25.223 +
  25.224 +LOCAL(void)
  25.225 +emit_dac (j_compress_ptr cinfo)
  25.226 +/* Emit a DAC marker */
  25.227 +/* Since the useful info is so small, we want to emit all the tables in */
  25.228 +/* one DAC marker.  Therefore this routine does its own scan of the table. */
  25.229 +{
  25.230 +#ifdef C_ARITH_CODING_SUPPORTED
  25.231 +  char dc_in_use[NUM_ARITH_TBLS];
  25.232 +  char ac_in_use[NUM_ARITH_TBLS];
  25.233 +  int length, i;
  25.234 +  jpeg_component_info *compptr;
  25.235 +  
  25.236 +  for (i = 0; i < NUM_ARITH_TBLS; i++)
  25.237 +    dc_in_use[i] = ac_in_use[i] = 0;
  25.238 +  
  25.239 +  for (i = 0; i < cinfo->comps_in_scan; i++) {
  25.240 +    compptr = cinfo->cur_comp_info[i];
  25.241 +    dc_in_use[compptr->dc_tbl_no] = 1;
  25.242 +    ac_in_use[compptr->ac_tbl_no] = 1;
  25.243 +  }
  25.244 +  
  25.245 +  length = 0;
  25.246 +  for (i = 0; i < NUM_ARITH_TBLS; i++)
  25.247 +    length += dc_in_use[i] + ac_in_use[i];
  25.248 +  
  25.249 +  emit_marker(cinfo, M_DAC);
  25.250 +  
  25.251 +  emit_2bytes(cinfo, length*2 + 2);
  25.252 +  
  25.253 +  for (i = 0; i < NUM_ARITH_TBLS; i++) {
  25.254 +    if (dc_in_use[i]) {
  25.255 +      emit_byte(cinfo, i);
  25.256 +      emit_byte(cinfo, cinfo->arith_dc_L[i] + (cinfo->arith_dc_U[i]<<4));
  25.257 +    }
  25.258 +    if (ac_in_use[i]) {
  25.259 +      emit_byte(cinfo, i + 0x10);
  25.260 +      emit_byte(cinfo, cinfo->arith_ac_K[i]);
  25.261 +    }
  25.262 +  }
  25.263 +#endif /* C_ARITH_CODING_SUPPORTED */
  25.264 +}
  25.265 +
  25.266 +
  25.267 +LOCAL(void)
  25.268 +emit_dri (j_compress_ptr cinfo)
  25.269 +/* Emit a DRI marker */
  25.270 +{
  25.271 +  emit_marker(cinfo, M_DRI);
  25.272 +  
  25.273 +  emit_2bytes(cinfo, 4);	/* fixed length */
  25.274 +
  25.275 +  emit_2bytes(cinfo, (int) cinfo->restart_interval);
  25.276 +}
  25.277 +
  25.278 +
  25.279 +LOCAL(void)
  25.280 +emit_sof (j_compress_ptr cinfo, JPEG_MARKER code)
  25.281 +/* Emit a SOF marker */
  25.282 +{
  25.283 +  int ci;
  25.284 +  jpeg_component_info *compptr;
  25.285 +  
  25.286 +  emit_marker(cinfo, code);
  25.287 +  
  25.288 +  emit_2bytes(cinfo, 3 * cinfo->num_components + 2 + 5 + 1); /* length */
  25.289 +
  25.290 +  /* Make sure image isn't bigger than SOF field can handle */
  25.291 +  if ((long) cinfo->image_height > 65535L ||
  25.292 +      (long) cinfo->image_width > 65535L)
  25.293 +    ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) 65535);
  25.294 +
  25.295 +  emit_byte(cinfo, cinfo->data_precision);
  25.296 +  emit_2bytes(cinfo, (int) cinfo->image_height);
  25.297 +  emit_2bytes(cinfo, (int) cinfo->image_width);
  25.298 +
  25.299 +  emit_byte(cinfo, cinfo->num_components);
  25.300 +
  25.301 +  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  25.302 +       ci++, compptr++) {
  25.303 +    emit_byte(cinfo, compptr->component_id);
  25.304 +    emit_byte(cinfo, (compptr->h_samp_factor << 4) + compptr->v_samp_factor);
  25.305 +    emit_byte(cinfo, compptr->quant_tbl_no);
  25.306 +  }
  25.307 +}
  25.308 +
  25.309 +
  25.310 +LOCAL(void)
  25.311 +emit_sos (j_compress_ptr cinfo)
  25.312 +/* Emit a SOS marker */
  25.313 +{
  25.314 +  int i, td, ta;
  25.315 +  jpeg_component_info *compptr;
  25.316 +  
  25.317 +  emit_marker(cinfo, M_SOS);
  25.318 +  
  25.319 +  emit_2bytes(cinfo, 2 * cinfo->comps_in_scan + 2 + 1 + 3); /* length */
  25.320 +  
  25.321 +  emit_byte(cinfo, cinfo->comps_in_scan);
  25.322 +  
  25.323 +  for (i = 0; i < cinfo->comps_in_scan; i++) {
  25.324 +    compptr = cinfo->cur_comp_info[i];
  25.325 +    emit_byte(cinfo, compptr->component_id);
  25.326 +    td = compptr->dc_tbl_no;
  25.327 +    ta = compptr->ac_tbl_no;
  25.328 +    if (cinfo->progressive_mode) {
  25.329 +      /* Progressive mode: only DC or only AC tables are used in one scan;
  25.330 +       * furthermore, Huffman coding of DC refinement uses no table at all.
  25.331 +       * We emit 0 for unused field(s); this is recommended by the P&M text
  25.332 +       * but does not seem to be specified in the standard.
  25.333 +       */
  25.334 +      if (cinfo->Ss == 0) {
  25.335 +	ta = 0;			/* DC scan */
  25.336 +	if (cinfo->Ah != 0 && !cinfo->arith_code)
  25.337 +	  td = 0;		/* no DC table either */
  25.338 +      } else {
  25.339 +	td = 0;			/* AC scan */
  25.340 +      }
  25.341 +    }
  25.342 +    emit_byte(cinfo, (td << 4) + ta);
  25.343 +  }
  25.344 +
  25.345 +  emit_byte(cinfo, cinfo->Ss);
  25.346 +  emit_byte(cinfo, cinfo->Se);
  25.347 +  emit_byte(cinfo, (cinfo->Ah << 4) + cinfo->Al);
  25.348 +}
  25.349 +
  25.350 +
  25.351 +LOCAL(void)
  25.352 +emit_jfif_app0 (j_compress_ptr cinfo)
  25.353 +/* Emit a JFIF-compliant APP0 marker */
  25.354 +{
  25.355 +  /*
  25.356 +   * Length of APP0 block	(2 bytes)
  25.357 +   * Block ID			(4 bytes - ASCII "JFIF")
  25.358 +   * Zero byte			(1 byte to terminate the ID string)
  25.359 +   * Version Major, Minor	(2 bytes - major first)
  25.360 +   * Units			(1 byte - 0x00 = none, 0x01 = inch, 0x02 = cm)
  25.361 +   * Xdpu			(2 bytes - dots per unit horizontal)
  25.362 +   * Ydpu			(2 bytes - dots per unit vertical)
  25.363 +   * Thumbnail X size		(1 byte)
  25.364 +   * Thumbnail Y size		(1 byte)
  25.365 +   */
  25.366 +  
  25.367 +  emit_marker(cinfo, M_APP0);
  25.368 +  
  25.369 +  emit_2bytes(cinfo, 2 + 4 + 1 + 2 + 1 + 2 + 2 + 1 + 1); /* length */
  25.370 +
  25.371 +  emit_byte(cinfo, 0x4A);	/* Identifier: ASCII "JFIF" */
  25.372 +  emit_byte(cinfo, 0x46);
  25.373 +  emit_byte(cinfo, 0x49);
  25.374 +  emit_byte(cinfo, 0x46);
  25.375 +  emit_byte(cinfo, 0);
  25.376 +  emit_byte(cinfo, cinfo->JFIF_major_version); /* Version fields */
  25.377 +  emit_byte(cinfo, cinfo->JFIF_minor_version);
  25.378 +  emit_byte(cinfo, cinfo->density_unit); /* Pixel size information */
  25.379 +  emit_2bytes(cinfo, (int) cinfo->X_density);
  25.380 +  emit_2bytes(cinfo, (int) cinfo->Y_density);
  25.381 +  emit_byte(cinfo, 0);		/* No thumbnail image */
  25.382 +  emit_byte(cinfo, 0);
  25.383 +}
  25.384 +
  25.385 +
  25.386 +LOCAL(void)
  25.387 +emit_adobe_app14 (j_compress_ptr cinfo)
  25.388 +/* Emit an Adobe APP14 marker */
  25.389 +{
  25.390 +  /*
  25.391 +   * Length of APP14 block	(2 bytes)
  25.392 +   * Block ID			(5 bytes - ASCII "Adobe")
  25.393 +   * Version Number		(2 bytes - currently 100)
  25.394 +   * Flags0			(2 bytes - currently 0)
  25.395 +   * Flags1			(2 bytes - currently 0)
  25.396 +   * Color transform		(1 byte)
  25.397 +   *
  25.398 +   * Although Adobe TN 5116 mentions Version = 101, all the Adobe files
  25.399 +   * now in circulation seem to use Version = 100, so that's what we write.
  25.400 +   *
  25.401 +   * We write the color transform byte as 1 if the JPEG color space is
  25.402 +   * YCbCr, 2 if it's YCCK, 0 otherwise.  Adobe's definition has to do with
  25.403 +   * whether the encoder performed a transformation, which is pretty useless.
  25.404 +   */
  25.405 +  
  25.406 +  emit_marker(cinfo, M_APP14);
  25.407 +  
  25.408 +  emit_2bytes(cinfo, 2 + 5 + 2 + 2 + 2 + 1); /* length */
  25.409 +
  25.410 +  emit_byte(cinfo, 0x41);	/* Identifier: ASCII "Adobe" */
  25.411 +  emit_byte(cinfo, 0x64);
  25.412 +  emit_byte(cinfo, 0x6F);
  25.413 +  emit_byte(cinfo, 0x62);
  25.414 +  emit_byte(cinfo, 0x65);
  25.415 +  emit_2bytes(cinfo, 100);	/* Version */
  25.416 +  emit_2bytes(cinfo, 0);	/* Flags0 */
  25.417 +  emit_2bytes(cinfo, 0);	/* Flags1 */
  25.418 +  switch (cinfo->jpeg_color_space) {
  25.419 +  case JCS_YCbCr:
  25.420 +    emit_byte(cinfo, 1);	/* Color transform = 1 */
  25.421 +    break;
  25.422 +  case JCS_YCCK:
  25.423 +    emit_byte(cinfo, 2);	/* Color transform = 2 */
  25.424 +    break;
  25.425 +  default:
  25.426 +    emit_byte(cinfo, 0);	/* Color transform = 0 */
  25.427 +    break;
  25.428 +  }
  25.429 +}
  25.430 +
  25.431 +
  25.432 +/*
  25.433 + * These routines allow writing an arbitrary marker with parameters.
  25.434 + * The only intended use is to emit COM or APPn markers after calling
  25.435 + * write_file_header and before calling write_frame_header.
  25.436 + * Other uses are not guaranteed to produce desirable results.
  25.437 + * Counting the parameter bytes properly is the caller's responsibility.
  25.438 + */
  25.439 +
  25.440 +METHODDEF(void)
  25.441 +write_marker_header (j_compress_ptr cinfo, int marker, unsigned int datalen)
  25.442 +/* Emit an arbitrary marker header */
  25.443 +{
  25.444 +  if (datalen > (unsigned int) 65533)		/* safety check */
  25.445 +    ERREXIT(cinfo, JERR_BAD_LENGTH);
  25.446 +
  25.447 +  emit_marker(cinfo, (JPEG_MARKER) marker);
  25.448 +
  25.449 +  emit_2bytes(cinfo, (int) (datalen + 2));	/* total length */
  25.450 +}
  25.451 +
  25.452 +METHODDEF(void)
  25.453 +write_marker_byte (j_compress_ptr cinfo, int val)
  25.454 +/* Emit one byte of marker parameters following write_marker_header */
  25.455 +{
  25.456 +  emit_byte(cinfo, val);
  25.457 +}
  25.458 +
  25.459 +
  25.460 +/*
  25.461 + * Write datastream header.
  25.462 + * This consists of an SOI and optional APPn markers.
  25.463 + * We recommend use of the JFIF marker, but not the Adobe marker,
  25.464 + * when using YCbCr or grayscale data.  The JFIF marker should NOT
  25.465 + * be used for any other JPEG colorspace.  The Adobe marker is helpful
  25.466 + * to distinguish RGB, CMYK, and YCCK colorspaces.
  25.467 + * Note that an application can write additional header markers after
  25.468 + * jpeg_start_compress returns.
  25.469 + */
  25.470 +
  25.471 +METHODDEF(void)
  25.472 +write_file_header (j_compress_ptr cinfo)
  25.473 +{
  25.474 +  my_marker_ptr marker = (my_marker_ptr) cinfo->marker;
  25.475 +
  25.476 +  emit_marker(cinfo, M_SOI);	/* first the SOI */
  25.477 +
  25.478 +  /* SOI is defined to reset restart interval to 0 */
  25.479 +  marker->last_restart_interval = 0;
  25.480 +
  25.481 +  if (cinfo->write_JFIF_header)	/* next an optional JFIF APP0 */
  25.482 +    emit_jfif_app0(cinfo);
  25.483 +  if (cinfo->write_Adobe_marker) /* next an optional Adobe APP14 */
  25.484 +    emit_adobe_app14(cinfo);
  25.485 +}
  25.486 +
  25.487 +
  25.488 +/*
  25.489 + * Write frame header.
  25.490 + * This consists of DQT and SOFn markers.
  25.491 + * Note that we do not emit the SOF until we have emitted the DQT(s).
  25.492 + * This avoids compatibility problems with incorrect implementations that
  25.493 + * try to error-check the quant table numbers as soon as they see the SOF.
  25.494 + */
  25.495 +
  25.496 +METHODDEF(void)
  25.497 +write_frame_header (j_compress_ptr cinfo)
  25.498 +{
  25.499 +  int ci, prec;
  25.500 +  boolean is_baseline;
  25.501 +  jpeg_component_info *compptr;
  25.502 +  
  25.503 +  /* Emit DQT for each quantization table.
  25.504 +   * Note that emit_dqt() suppresses any duplicate tables.
  25.505 +   */
  25.506 +  prec = 0;
  25.507 +  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  25.508 +       ci++, compptr++) {
  25.509 +    prec += emit_dqt(cinfo, compptr->quant_tbl_no);
  25.510 +  }
  25.511 +  /* now prec is nonzero iff there are any 16-bit quant tables. */
  25.512 +
  25.513 +  /* Check for a non-baseline specification.
  25.514 +   * Note we assume that Huffman table numbers won't be changed later.
  25.515 +   */
  25.516 +  if (cinfo->arith_code || cinfo->progressive_mode ||
  25.517 +      cinfo->data_precision != 8) {
  25.518 +    is_baseline = FALSE;
  25.519 +  } else {
  25.520 +    is_baseline = TRUE;
  25.521 +    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  25.522 +	 ci++, compptr++) {
  25.523 +      if (compptr->dc_tbl_no > 1 || compptr->ac_tbl_no > 1)
  25.524 +	is_baseline = FALSE;
  25.525 +    }
  25.526 +    if (prec && is_baseline) {
  25.527 +      is_baseline = FALSE;
  25.528 +      /* If it's baseline except for quantizer size, warn the user */
  25.529 +      TRACEMS(cinfo, 0, JTRC_16BIT_TABLES);
  25.530 +    }
  25.531 +  }
  25.532 +
  25.533 +  /* Emit the proper SOF marker */
  25.534 +  if (cinfo->arith_code) {
  25.535 +    emit_sof(cinfo, M_SOF9);	/* SOF code for arithmetic coding */
  25.536 +  } else {
  25.537 +    if (cinfo->progressive_mode)
  25.538 +      emit_sof(cinfo, M_SOF2);	/* SOF code for progressive Huffman */
  25.539 +    else if (is_baseline)
  25.540 +      emit_sof(cinfo, M_SOF0);	/* SOF code for baseline implementation */
  25.541 +    else
  25.542 +      emit_sof(cinfo, M_SOF1);	/* SOF code for non-baseline Huffman file */
  25.543 +  }
  25.544 +}
  25.545 +
  25.546 +
  25.547 +/*
  25.548 + * Write scan header.
  25.549 + * This consists of DHT or DAC markers, optional DRI, and SOS.
  25.550 + * Compressed data will be written following the SOS.
  25.551 + */
  25.552 +
  25.553 +METHODDEF(void)
  25.554 +write_scan_header (j_compress_ptr cinfo)
  25.555 +{
  25.556 +  my_marker_ptr marker = (my_marker_ptr) cinfo->marker;
  25.557 +  int i;
  25.558 +  jpeg_component_info *compptr;
  25.559 +
  25.560 +  if (cinfo->arith_code) {
  25.561 +    /* Emit arith conditioning info.  We may have some duplication
  25.562 +     * if the file has multiple scans, but it's so small it's hardly
  25.563 +     * worth worrying about.
  25.564 +     */
  25.565 +    emit_dac(cinfo);
  25.566 +  } else {
  25.567 +    /* Emit Huffman tables.
  25.568 +     * Note that emit_dht() suppresses any duplicate tables.
  25.569 +     */
  25.570 +    for (i = 0; i < cinfo->comps_in_scan; i++) {
  25.571 +      compptr = cinfo->cur_comp_info[i];
  25.572 +      if (cinfo->progressive_mode) {
  25.573 +	/* Progressive mode: only DC or only AC tables are used in one scan */
  25.574 +	if (cinfo->Ss == 0) {
  25.575 +	  if (cinfo->Ah == 0)	/* DC needs no table for refinement scan */
  25.576 +	    emit_dht(cinfo, compptr->dc_tbl_no, FALSE);
  25.577 +	} else {
  25.578 +	  emit_dht(cinfo, compptr->ac_tbl_no, TRUE);
  25.579 +	}
  25.580 +      } else {
  25.581 +	/* Sequential mode: need both DC and AC tables */
  25.582 +	emit_dht(cinfo, compptr->dc_tbl_no, FALSE);
  25.583 +	emit_dht(cinfo, compptr->ac_tbl_no, TRUE);
  25.584 +      }
  25.585 +    }
  25.586 +  }
  25.587 +
  25.588 +  /* Emit DRI if required --- note that DRI value could change for each scan.
  25.589 +   * We avoid wasting space with unnecessary DRIs, however.
  25.590 +   */
  25.591 +  if (cinfo->restart_interval != marker->last_restart_interval) {
  25.592 +    emit_dri(cinfo);
  25.593 +    marker->last_restart_interval = cinfo->restart_interval;
  25.594 +  }
  25.595 +
  25.596 +  emit_sos(cinfo);
  25.597 +}
  25.598 +
  25.599 +
  25.600 +/*
  25.601 + * Write datastream trailer.
  25.602 + */
  25.603 +
  25.604 +METHODDEF(void)
  25.605 +write_file_trailer (j_compress_ptr cinfo)
  25.606 +{
  25.607 +  emit_marker(cinfo, M_EOI);
  25.608 +}
  25.609 +
  25.610 +
  25.611 +/*
  25.612 + * Write an abbreviated table-specification datastream.
  25.613 + * This consists of SOI, DQT and DHT tables, and EOI.
  25.614 + * Any table that is defined and not marked sent_table = TRUE will be
  25.615 + * emitted.  Note that all tables will be marked sent_table = TRUE at exit.
  25.616 + */
  25.617 +
  25.618 +METHODDEF(void)
  25.619 +write_tables_only (j_compress_ptr cinfo)
  25.620 +{
  25.621 +  int i;
  25.622 +
  25.623 +  emit_marker(cinfo, M_SOI);
  25.624 +
  25.625 +  for (i = 0; i < NUM_QUANT_TBLS; i++) {
  25.626 +    if (cinfo->quant_tbl_ptrs[i] != NULL)
  25.627 +      (void) emit_dqt(cinfo, i);
  25.628 +  }
  25.629 +
  25.630 +  if (! cinfo->arith_code) {
  25.631 +    for (i = 0; i < NUM_HUFF_TBLS; i++) {
  25.632 +      if (cinfo->dc_huff_tbl_ptrs[i] != NULL)
  25.633 +	emit_dht(cinfo, i, FALSE);
  25.634 +      if (cinfo->ac_huff_tbl_ptrs[i] != NULL)
  25.635 +	emit_dht(cinfo, i, TRUE);
  25.636 +    }
  25.637 +  }
  25.638 +
  25.639 +  emit_marker(cinfo, M_EOI);
  25.640 +}
  25.641 +
  25.642 +
  25.643 +/*
  25.644 + * Initialize the marker writer module.
  25.645 + */
  25.646 +
  25.647 +GLOBAL(void)
  25.648 +jinit_marker_writer (j_compress_ptr cinfo)
  25.649 +{
  25.650 +  my_marker_ptr marker;
  25.651 +
  25.652 +  /* Create the subobject */
  25.653 +  marker = (my_marker_ptr)
  25.654 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  25.655 +				SIZEOF(my_marker_writer));
  25.656 +  cinfo->marker = (struct jpeg_marker_writer *) marker;
  25.657 +  /* Initialize method pointers */
  25.658 +  marker->pub.write_file_header = write_file_header;
  25.659 +  marker->pub.write_frame_header = write_frame_header;
  25.660 +  marker->pub.write_scan_header = write_scan_header;
  25.661 +  marker->pub.write_file_trailer = write_file_trailer;
  25.662 +  marker->pub.write_tables_only = write_tables_only;
  25.663 +  marker->pub.write_marker_header = write_marker_header;
  25.664 +  marker->pub.write_marker_byte = write_marker_byte;
  25.665 +  /* Initialize private state */
  25.666 +  marker->last_restart_interval = 0;
  25.667 +}
    26.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    26.2 +++ b/libs/libjpeg/jcmaster.c	Sat Sep 19 05:51:51 2015 +0300
    26.3 @@ -0,0 +1,590 @@
    26.4 +/*
    26.5 + * jcmaster.c
    26.6 + *
    26.7 + * Copyright (C) 1991-1997, Thomas G. Lane.
    26.8 + * This file is part of the Independent JPEG Group's software.
    26.9 + * For conditions of distribution and use, see the accompanying README file.
   26.10 + *
   26.11 + * This file contains master control logic for the JPEG compressor.
   26.12 + * These routines are concerned with parameter validation, initial setup,
   26.13 + * and inter-pass control (determining the number of passes and the work 
   26.14 + * to be done in each pass).
   26.15 + */
   26.16 +
   26.17 +#define JPEG_INTERNALS
   26.18 +#include "jinclude.h"
   26.19 +#include "jpeglib.h"
   26.20 +
   26.21 +
   26.22 +/* Private state */
   26.23 +
   26.24 +typedef enum {
   26.25 +	main_pass,		/* input data, also do first output step */
   26.26 +	huff_opt_pass,		/* Huffman code optimization pass */
   26.27 +	output_pass		/* data output pass */
   26.28 +} c_pass_type;
   26.29 +
   26.30 +typedef struct {
   26.31 +  struct jpeg_comp_master pub;	/* public fields */
   26.32 +
   26.33 +  c_pass_type pass_type;	/* the type of the current pass */
   26.34 +
   26.35 +  int pass_number;		/* # of passes completed */
   26.36 +  int total_passes;		/* total # of passes needed */
   26.37 +
   26.38 +  int scan_number;		/* current index in scan_info[] */
   26.39 +} my_comp_master;
   26.40 +
   26.41 +typedef my_comp_master * my_master_ptr;
   26.42 +
   26.43 +
   26.44 +/*
   26.45 + * Support routines that do various essential calculations.
   26.46 + */
   26.47 +
   26.48 +LOCAL(void)
   26.49 +initial_setup (j_compress_ptr cinfo)
   26.50 +/* Do computations that are needed before master selection phase */
   26.51 +{
   26.52 +  int ci;
   26.53 +  jpeg_component_info *compptr;
   26.54 +  long samplesperrow;
   26.55 +  JDIMENSION jd_samplesperrow;
   26.56 +
   26.57 +  /* Sanity check on image dimensions */
   26.58 +  if (cinfo->image_height <= 0 || cinfo->image_width <= 0
   26.59 +      || cinfo->num_components <= 0 || cinfo->input_components <= 0)
   26.60 +    ERREXIT(cinfo, JERR_EMPTY_IMAGE);
   26.61 +
   26.62 +  /* Make sure image isn't bigger than I can handle */
   26.63 +  if ((long) cinfo->image_height > (long) JPEG_MAX_DIMENSION ||
   26.64 +      (long) cinfo->image_width > (long) JPEG_MAX_DIMENSION)
   26.65 +    ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION);
   26.66 +
   26.67 +  /* Width of an input scanline must be representable as JDIMENSION. */
   26.68 +  samplesperrow = (long) cinfo->image_width * (long) cinfo->input_components;
   26.69 +  jd_samplesperrow = (JDIMENSION) samplesperrow;
   26.70 +  if ((long) jd_samplesperrow != samplesperrow)
   26.71 +    ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
   26.72 +
   26.73 +  /* For now, precision must match compiled-in value... */
   26.74 +  if (cinfo->data_precision != BITS_IN_JSAMPLE)
   26.75 +    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
   26.76 +
   26.77 +  /* Check that number of components won't exceed internal array sizes */
   26.78 +  if (cinfo->num_components > MAX_COMPONENTS)
   26.79 +    ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
   26.80 +	     MAX_COMPONENTS);
   26.81 +
   26.82 +  /* Compute maximum sampling factors; check factor validity */
   26.83 +  cinfo->max_h_samp_factor = 1;
   26.84 +  cinfo->max_v_samp_factor = 1;
   26.85 +  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
   26.86 +       ci++, compptr++) {
   26.87 +    if (compptr->h_samp_factor<=0 || compptr->h_samp_factor>MAX_SAMP_FACTOR ||
   26.88 +	compptr->v_samp_factor<=0 || compptr->v_samp_factor>MAX_SAMP_FACTOR)
   26.89 +      ERREXIT(cinfo, JERR_BAD_SAMPLING);
   26.90 +    cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor,
   26.91 +				   compptr->h_samp_factor);
   26.92 +    cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor,
   26.93 +				   compptr->v_samp_factor);
   26.94 +  }
   26.95 +
   26.96 +  /* Compute dimensions of components */
   26.97 +  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
   26.98 +       ci++, compptr++) {
   26.99 +    /* Fill in the correct component_index value; don't rely on application */
  26.100 +    compptr->component_index = ci;
  26.101 +    /* For compression, we never do DCT scaling. */
  26.102 +    compptr->DCT_scaled_size = DCTSIZE;
  26.103 +    /* Size in DCT blocks */
  26.104 +    compptr->width_in_blocks = (JDIMENSION)
  26.105 +      jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
  26.106 +		    (long) (cinfo->max_h_samp_factor * DCTSIZE));
  26.107 +    compptr->height_in_blocks = (JDIMENSION)
  26.108 +      jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
  26.109 +		    (long) (cinfo->max_v_samp_factor * DCTSIZE));
  26.110 +    /* Size in samples */
  26.111 +    compptr->downsampled_width = (JDIMENSION)
  26.112 +      jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
  26.113 +		    (long) cinfo->max_h_samp_factor);
  26.114 +    compptr->downsampled_height = (JDIMENSION)
  26.115 +      jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
  26.116 +		    (long) cinfo->max_v_samp_factor);
  26.117 +    /* Mark component needed (this flag isn't actually used for compression) */
  26.118 +    compptr->component_needed = TRUE;
  26.119 +  }
  26.120 +
  26.121 +  /* Compute number of fully interleaved MCU rows (number of times that
  26.122 +   * main controller will call coefficient controller).
  26.123 +   */
  26.124 +  cinfo->total_iMCU_rows = (JDIMENSION)
  26.125 +    jdiv_round_up((long) cinfo->image_height,
  26.126 +		  (long) (cinfo->max_v_samp_factor*DCTSIZE));
  26.127 +}
  26.128 +
  26.129 +
  26.130 +#ifdef C_MULTISCAN_FILES_SUPPORTED
  26.131 +
  26.132 +LOCAL(void)
  26.133 +validate_script (j_compress_ptr cinfo)
  26.134 +/* Verify that the scan script in cinfo->scan_info[] is valid; also
  26.135 + * determine whether it uses progressive JPEG, and set cinfo->progressive_mode.
  26.136 + */
  26.137 +{
  26.138 +  const jpeg_scan_info * scanptr;
  26.139 +  int scanno, ncomps, ci, coefi, thisi;
  26.140 +  int Ss, Se, Ah, Al;
  26.141 +  boolean component_sent[MAX_COMPONENTS];
  26.142 +#ifdef C_PROGRESSIVE_SUPPORTED
  26.143 +  int * last_bitpos_ptr;
  26.144 +  int last_bitpos[MAX_COMPONENTS][DCTSIZE2];
  26.145 +  /* -1 until that coefficient has been seen; then last Al for it */
  26.146 +#endif
  26.147 +
  26.148 +  if (cinfo->num_scans <= 0)
  26.149 +    ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, 0);
  26.150 +
  26.151 +  /* For sequential JPEG, all scans must have Ss=0, Se=DCTSIZE2-1;
  26.152 +   * for progressive JPEG, no scan can have this.
  26.153 +   */
  26.154 +  scanptr = cinfo->scan_info;
  26.155 +  if (scanptr->Ss != 0 || scanptr->Se != DCTSIZE2-1) {
  26.156 +#ifdef C_PROGRESSIVE_SUPPORTED
  26.157 +    cinfo->progressive_mode = TRUE;
  26.158 +    last_bitpos_ptr = & last_bitpos[0][0];
  26.159 +    for (ci = 0; ci < cinfo->num_components; ci++) 
  26.160 +      for (coefi = 0; coefi < DCTSIZE2; coefi++)
  26.161 +	*last_bitpos_ptr++ = -1;
  26.162 +#else
  26.163 +    ERREXIT(cinfo, JERR_NOT_COMPILED);
  26.164 +#endif
  26.165 +  } else {
  26.166 +    cinfo->progressive_mode = FALSE;
  26.167 +    for (ci = 0; ci < cinfo->num_components; ci++) 
  26.168 +      component_sent[ci] = FALSE;
  26.169 +  }
  26.170 +
  26.171 +  for (scanno = 1; scanno <= cinfo->num_scans; scanptr++, scanno++) {
  26.172 +    /* Validate component indexes */
  26.173 +    ncomps = scanptr->comps_in_scan;
  26.174 +    if (ncomps <= 0 || ncomps > MAX_COMPS_IN_SCAN)
  26.175 +      ERREXIT2(cinfo, JERR_COMPONENT_COUNT, ncomps, MAX_COMPS_IN_SCAN);
  26.176 +    for (ci = 0; ci < ncomps; ci++) {
  26.177 +      thisi = scanptr->component_index[ci];
  26.178 +      if (thisi < 0 || thisi >= cinfo->num_components)
  26.179 +	ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);
  26.180 +      /* Components must appear in SOF order within each scan */
  26.181 +      if (ci > 0 && thisi <= scanptr->component_index[ci-1])
  26.182 +	ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);
  26.183 +    }
  26.184 +    /* Validate progression parameters */
  26.185 +    Ss = scanptr->Ss;
  26.186 +    Se = scanptr->Se;
  26.187 +    Ah = scanptr->Ah;
  26.188 +    Al = scanptr->Al;
  26.189 +    if (cinfo->progressive_mode) {
  26.190 +#ifdef C_PROGRESSIVE_SUPPORTED
  26.191 +      /* The JPEG spec simply gives the ranges 0..13 for Ah and Al, but that
  26.192 +       * seems wrong: the upper bound ought to depend on data precision.
  26.193 +       * Perhaps they really meant 0..N+1 for N-bit precision.
  26.194 +       * Here we allow 0..10 for 8-bit data; Al larger than 10 results in
  26.195 +       * out-of-range reconstructed DC values during the first DC scan,
  26.196 +       * which might cause problems for some decoders.
  26.197 +       */
  26.198 +#if BITS_IN_JSAMPLE == 8
  26.199 +#define MAX_AH_AL 10
  26.200 +#else
  26.201 +#define MAX_AH_AL 13
  26.202 +#endif
  26.203 +      if (Ss < 0 || Ss >= DCTSIZE2 || Se < Ss || Se >= DCTSIZE2 ||
  26.204 +	  Ah < 0 || Ah > MAX_AH_AL || Al < 0 || Al > MAX_AH_AL)
  26.205 +	ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
  26.206 +      if (Ss == 0) {
  26.207 +	if (Se != 0)		/* DC and AC together not OK */
  26.208 +	  ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
  26.209 +      } else {
  26.210 +	if (ncomps != 1)	/* AC scans must be for only one component */
  26.211 +	  ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
  26.212 +      }
  26.213 +      for (ci = 0; ci < ncomps; ci++) {
  26.214 +	last_bitpos_ptr = & last_bitpos[scanptr->component_index[ci]][0];
  26.215 +	if (Ss != 0 && last_bitpos_ptr[0] < 0) /* AC without prior DC scan */
  26.216 +	  ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
  26.217 +	for (coefi = Ss; coefi <= Se; coefi++) {
  26.218 +	  if (last_bitpos_ptr[coefi] < 0) {
  26.219 +	    /* first scan of this coefficient */
  26.220 +	    if (Ah != 0)
  26.221 +	      ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
  26.222 +	  } else {
  26.223 +	    /* not first scan */
  26.224 +	    if (Ah != last_bitpos_ptr[coefi] || Al != Ah-1)
  26.225 +	      ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
  26.226 +	  }
  26.227 +	  last_bitpos_ptr[coefi] = Al;
  26.228 +	}
  26.229 +      }
  26.230 +#endif
  26.231 +    } else {
  26.232 +      /* For sequential JPEG, all progression parameters must be these: */
  26.233 +      if (Ss != 0 || Se != DCTSIZE2-1 || Ah != 0 || Al != 0)
  26.234 +	ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
  26.235 +      /* Make sure components are not sent twice */
  26.236 +      for (ci = 0; ci < ncomps; ci++) {
  26.237 +	thisi = scanptr->component_index[ci];
  26.238 +	if (component_sent[thisi])
  26.239 +	  ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);
  26.240 +	component_sent[thisi] = TRUE;
  26.241 +      }
  26.242 +    }
  26.243 +  }
  26.244 +
  26.245 +  /* Now verify that everything got sent. */
  26.246 +  if (cinfo->progressive_mode) {
  26.247 +#ifdef C_PROGRESSIVE_SUPPORTED
  26.248 +    /* For progressive mode, we only check that at least some DC data
  26.249 +     * got sent for each component; the spec does not require that all bits
  26.250 +     * of all coefficients be transmitted.  Would it be wiser to enforce
  26.251 +     * transmission of all coefficient bits??
  26.252 +     */
  26.253 +    for (ci = 0; ci < cinfo->num_components; ci++) {
  26.254 +      if (last_bitpos[ci][0] < 0)
  26.255 +	ERREXIT(cinfo, JERR_MISSING_DATA);
  26.256 +    }
  26.257 +#endif
  26.258 +  } else {
  26.259 +    for (ci = 0; ci < cinfo->num_components; ci++) {
  26.260 +      if (! component_sent[ci])
  26.261 +	ERREXIT(cinfo, JERR_MISSING_DATA);
  26.262 +    }
  26.263 +  }
  26.264 +}
  26.265 +
  26.266 +#endif /* C_MULTISCAN_FILES_SUPPORTED */
  26.267 +
  26.268 +
  26.269 +LOCAL(void)
  26.270 +select_scan_parameters (j_compress_ptr cinfo)
  26.271 +/* Set up the scan parameters for the current scan */
  26.272 +{
  26.273 +  int ci;
  26.274 +
  26.275 +#ifdef C_MULTISCAN_FILES_SUPPORTED
  26.276 +  if (cinfo->scan_info != NULL) {
  26.277 +    /* Prepare for current scan --- the script is already validated */
  26.278 +    my_master_ptr master = (my_master_ptr) cinfo->master;
  26.279 +    const jpeg_scan_info * scanptr = cinfo->scan_info + master->scan_number;
  26.280 +
  26.281 +    cinfo->comps_in_scan = scanptr->comps_in_scan;
  26.282 +    for (ci = 0; ci < scanptr->comps_in_scan; ci++) {
  26.283 +      cinfo->cur_comp_info[ci] =
  26.284 +	&cinfo->comp_info[scanptr->component_index[ci]];
  26.285 +    }
  26.286 +    cinfo->Ss = scanptr->Ss;
  26.287 +    cinfo->Se = scanptr->Se;
  26.288 +    cinfo->Ah = scanptr->Ah;
  26.289 +    cinfo->Al = scanptr->Al;
  26.290 +  }
  26.291 +  else
  26.292 +#endif
  26.293 +  {
  26.294 +    /* Prepare for single sequential-JPEG scan containing all components */
  26.295 +    if (cinfo->num_components > MAX_COMPS_IN_SCAN)
  26.296 +      ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
  26.297 +	       MAX_COMPS_IN_SCAN);
  26.298 +    cinfo->comps_in_scan = cinfo->num_components;
  26.299 +    for (ci = 0; ci < cinfo->num_components; ci++) {
  26.300 +      cinfo->cur_comp_info[ci] = &cinfo->comp_info[ci];
  26.301 +    }
  26.302 +    cinfo->Ss = 0;
  26.303 +    cinfo->Se = DCTSIZE2-1;
  26.304 +    cinfo->Ah = 0;
  26.305 +    cinfo->Al = 0;
  26.306 +  }
  26.307 +}
  26.308 +
  26.309 +
  26.310 +LOCAL(void)
  26.311 +per_scan_setup (j_compress_ptr cinfo)
  26.312 +/* Do computations that are needed before processing a JPEG scan */
  26.313 +/* cinfo->comps_in_scan and cinfo->cur_comp_info[] are already set */
  26.314 +{
  26.315 +  int ci, mcublks, tmp;
  26.316 +  jpeg_component_info *compptr;
  26.317 +  
  26.318 +  if (cinfo->comps_in_scan == 1) {
  26.319 +    
  26.320 +    /* Noninterleaved (single-component) scan */
  26.321 +    compptr = cinfo->cur_comp_info[0];
  26.322 +    
  26.323 +    /* Overall image size in MCUs */
  26.324 +    cinfo->MCUs_per_row = compptr->width_in_blocks;
  26.325 +    cinfo->MCU_rows_in_scan = compptr->height_in_blocks;
  26.326 +    
  26.327 +    /* For noninterleaved scan, always one block per MCU */
  26.328 +    compptr->MCU_width = 1;
  26.329 +    compptr->MCU_height = 1;
  26.330 +    compptr->MCU_blocks = 1;
  26.331 +    compptr->MCU_sample_width = DCTSIZE;
  26.332 +    compptr->last_col_width = 1;
  26.333 +    /* For noninterleaved scans, it is convenient to define last_row_height
  26.334 +     * as the number of block rows present in the last iMCU row.
  26.335 +     */
  26.336 +    tmp = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
  26.337 +    if (tmp == 0) tmp = compptr->v_samp_factor;
  26.338 +    compptr->last_row_height = tmp;
  26.339 +    
  26.340 +    /* Prepare array describing MCU composition */
  26.341 +    cinfo->blocks_in_MCU = 1;
  26.342 +    cinfo->MCU_membership[0] = 0;
  26.343 +    
  26.344 +  } else {
  26.345 +    
  26.346 +    /* Interleaved (multi-component) scan */
  26.347 +    if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN)
  26.348 +      ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan,
  26.349 +	       MAX_COMPS_IN_SCAN);
  26.350 +    
  26.351 +    /* Overall image size in MCUs */
  26.352 +    cinfo->MCUs_per_row = (JDIMENSION)
  26.353 +      jdiv_round_up((long) cinfo->image_width,
  26.354 +		    (long) (cinfo->max_h_samp_factor*DCTSIZE));
  26.355 +    cinfo->MCU_rows_in_scan = (JDIMENSION)
  26.356 +      jdiv_round_up((long) cinfo->image_height,
  26.357 +		    (long) (cinfo->max_v_samp_factor*DCTSIZE));
  26.358 +    
  26.359 +    cinfo->blocks_in_MCU = 0;
  26.360 +    
  26.361 +    for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  26.362 +      compptr = cinfo->cur_comp_info[ci];
  26.363 +      /* Sampling factors give # of blocks of component in each MCU */
  26.364 +      compptr->MCU_width = compptr->h_samp_factor;
  26.365 +      compptr->MCU_height = compptr->v_samp_factor;
  26.366 +      compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height;
  26.367 +      compptr->MCU_sample_width = compptr->MCU_width * DCTSIZE;
  26.368 +      /* Figure number of non-dummy blocks in last MCU column & row */
  26.369 +      tmp = (int) (compptr->width_in_blocks % compptr->MCU_width);
  26.370 +      if (tmp == 0) tmp = compptr->MCU_width;
  26.371 +      compptr->last_col_width = tmp;
  26.372 +      tmp = (int) (compptr->height_in_blocks % compptr->MCU_height);
  26.373 +      if (tmp == 0) tmp = compptr->MCU_height;
  26.374 +      compptr->last_row_height = tmp;
  26.375 +      /* Prepare array describing MCU composition */
  26.376 +      mcublks = compptr->MCU_blocks;
  26.377 +      if (cinfo->blocks_in_MCU + mcublks > C_MAX_BLOCKS_IN_MCU)
  26.378 +	ERREXIT(cinfo, JERR_BAD_MCU_SIZE);
  26.379 +      while (mcublks-- > 0) {
  26.380 +	cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci;
  26.381 +      }
  26.382 +    }
  26.383 +    
  26.384 +  }
  26.385 +
  26.386 +  /* Convert restart specified in rows to actual MCU count. */
  26.387 +  /* Note that count must fit in 16 bits, so we provide limiting. */
  26.388 +  if (cinfo->restart_in_rows > 0) {
  26.389 +    long nominal = (long) cinfo->restart_in_rows * (long) cinfo->MCUs_per_row;
  26.390 +    cinfo->restart_interval = (unsigned int) MIN(nominal, 65535L);
  26.391 +  }
  26.392 +}
  26.393 +
  26.394 +
  26.395 +/*
  26.396 + * Per-pass setup.
  26.397 + * This is called at the beginning of each pass.  We determine which modules
  26.398 + * will be active during this pass and give them appropriate start_pass calls.
  26.399 + * We also set is_last_pass to indicate whether any more passes will be
  26.400 + * required.
  26.401 + */
  26.402 +
  26.403 +METHODDEF(void)
  26.404 +prepare_for_pass (j_compress_ptr cinfo)
  26.405 +{
  26.406 +  my_master_ptr master = (my_master_ptr) cinfo->master;
  26.407 +
  26.408 +  switch (master->pass_type) {
  26.409 +  case main_pass:
  26.410 +    /* Initial pass: will collect input data, and do either Huffman
  26.411 +     * optimization or data output for the first scan.
  26.412 +     */
  26.413 +    select_scan_parameters(cinfo);
  26.414 +    per_scan_setup(cinfo);
  26.415 +    if (! cinfo->raw_data_in) {
  26.416 +      (*cinfo->cconvert->start_pass) (cinfo);
  26.417 +      (*cinfo->downsample->start_pass) (cinfo);
  26.418 +      (*cinfo->prep->start_pass) (cinfo, JBUF_PASS_THRU);
  26.419 +    }
  26.420 +    (*cinfo->fdct->start_pass) (cinfo);
  26.421 +    (*cinfo->entropy->start_pass) (cinfo, cinfo->optimize_coding);
  26.422 +    (*cinfo->coef->start_pass) (cinfo,
  26.423 +				(master->total_passes > 1 ?
  26.424 +				 JBUF_SAVE_AND_PASS : JBUF_PASS_THRU));
  26.425 +    (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU);
  26.426 +    if (cinfo->optimize_coding) {
  26.427 +      /* No immediate data output; postpone writing frame/scan headers */
  26.428 +      master->pub.call_pass_startup = FALSE;
  26.429 +    } else {
  26.430 +      /* Will write frame/scan headers at first jpeg_write_scanlines call */
  26.431 +      master->pub.call_pass_startup = TRUE;
  26.432 +    }
  26.433 +    break;
  26.434 +#ifdef ENTROPY_OPT_SUPPORTED
  26.435 +  case huff_opt_pass:
  26.436 +    /* Do Huffman optimization for a scan after the first one. */
  26.437 +    select_scan_parameters(cinfo);
  26.438 +    per_scan_setup(cinfo);
  26.439 +    if (cinfo->Ss != 0 || cinfo->Ah == 0 || cinfo->arith_code) {
  26.440 +      (*cinfo->entropy->start_pass) (cinfo, TRUE);
  26.441 +      (*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST);
  26.442 +      master->pub.call_pass_startup = FALSE;
  26.443 +      break;
  26.444 +    }
  26.445 +    /* Special case: Huffman DC refinement scans need no Huffman table
  26.446 +     * and therefore we can skip the optimization pass for them.
  26.447 +     */
  26.448 +    master->pass_type = output_pass;
  26.449 +    master->pass_number++;
  26.450 +    /*FALLTHROUGH*/
  26.451 +#endif
  26.452 +  case output_pass:
  26.453 +    /* Do a data-output pass. */
  26.454 +    /* We need not repeat per-scan setup if prior optimization pass did it. */
  26.455 +    if (! cinfo->optimize_coding) {
  26.456 +      select_scan_parameters(cinfo);
  26.457 +      per_scan_setup(cinfo);
  26.458 +    }
  26.459 +    (*cinfo->entropy->start_pass) (cinfo, FALSE);
  26.460 +    (*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST);
  26.461 +    /* We emit frame/scan headers now */
  26.462 +    if (master->scan_number == 0)
  26.463 +      (*cinfo->marker->write_frame_header) (cinfo);
  26.464 +    (*cinfo->marker->write_scan_header) (cinfo);
  26.465 +    master->pub.call_pass_startup = FALSE;
  26.466 +    break;
  26.467 +  default:
  26.468 +    ERREXIT(cinfo, JERR_NOT_COMPILED);
  26.469 +  }
  26.470 +
  26.471 +  master->pub.is_last_pass = (master->pass_number == master->total_passes-1);
  26.472 +
  26.473 +  /* Set up progress monitor's pass info if present */
  26.474 +  if (cinfo->progress != NULL) {
  26.475 +    cinfo->progress->completed_passes = master->pass_number;
  26.476 +    cinfo->progress->total_passes = master->total_passes;
  26.477 +  }
  26.478 +}
  26.479 +
  26.480 +
  26.481 +/*
  26.482 + * Special start-of-pass hook.
  26.483 + * This is called by jpeg_write_scanlines if call_pass_startup is TRUE.
  26.484 + * In single-pass processing, we need this hook because we don't want to
  26.485 + * write frame/scan headers during jpeg_start_compress; we want to let the
  26.486 + * application write COM markers etc. between jpeg_start_compress and the
  26.487 + * jpeg_write_scanlines loop.
  26.488 + * In multi-pass processing, this routine is not used.
  26.489 + */
  26.490 +
  26.491 +METHODDEF(void)
  26.492 +pass_startup (j_compress_ptr cinfo)
  26.493 +{
  26.494 +  cinfo->master->call_pass_startup = FALSE; /* reset flag so call only once */
  26.495 +
  26.496 +  (*cinfo->marker->write_frame_header) (cinfo);
  26.497 +  (*cinfo->marker->write_scan_header) (cinfo);
  26.498 +}
  26.499 +
  26.500 +
  26.501 +/*
  26.502 + * Finish up at end of pass.
  26.503 + */
  26.504 +
  26.505 +METHODDEF(void)
  26.506 +finish_pass_master (j_compress_ptr cinfo)
  26.507 +{
  26.508 +  my_master_ptr master = (my_master_ptr) cinfo->master;
  26.509 +
  26.510 +  /* The entropy coder always needs an end-of-pass call,
  26.511 +   * either to analyze statistics or to flush its output buffer.
  26.512 +   */
  26.513 +  (*cinfo->entropy->finish_pass) (cinfo);
  26.514 +
  26.515 +  /* Update state for next pass */
  26.516 +  switch (master->pass_type) {
  26.517 +  case main_pass:
  26.518 +    /* next pass is either output of scan 0 (after optimization)
  26.519 +     * or output of scan 1 (if no optimization).
  26.520 +     */
  26.521 +    master->pass_type = output_pass;
  26.522 +    if (! cinfo->optimize_coding)
  26.523 +      master->scan_number++;
  26.524 +    break;
  26.525 +  case huff_opt_pass:
  26.526 +    /* next pass is always output of current scan */
  26.527 +    master->pass_type = output_pass;
  26.528 +    break;
  26.529 +  case output_pass:
  26.530 +    /* next pass is either optimization or output of next scan */
  26.531 +    if (cinfo->optimize_coding)
  26.532 +      master->pass_type = huff_opt_pass;
  26.533 +    master->scan_number++;
  26.534 +    break;
  26.535 +  }
  26.536 +
  26.537 +  master->pass_number++;
  26.538 +}
  26.539 +
  26.540 +
  26.541 +/*
  26.542 + * Initialize master compression control.
  26.543 + */
  26.544 +
  26.545 +GLOBAL(void)
  26.546 +jinit_c_master_control (j_compress_ptr cinfo, boolean transcode_only)
  26.547 +{
  26.548 +  my_master_ptr master;
  26.549 +
  26.550 +  master = (my_master_ptr)
  26.551 +      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  26.552 +				  SIZEOF(my_comp_master));
  26.553 +  cinfo->master = (struct jpeg_comp_master *) master;
  26.554 +  master->pub.prepare_for_pass = prepare_for_pass;
  26.555 +  master->pub.pass_startup = pass_startup;
  26.556 +  master->pub.finish_pass = finish_pass_master;
  26.557 +  master->pub.is_last_pass = FALSE;
  26.558 +
  26.559 +  /* Validate parameters, determine derived values */
  26.560 +  initial_setup(cinfo);
  26.561 +
  26.562 +  if (cinfo->scan_info != NULL) {
  26.563 +#ifdef C_MULTISCAN_FILES_SUPPORTED
  26.564 +    validate_script(cinfo);
  26.565 +#else
  26.566 +    ERREXIT(cinfo, JERR_NOT_COMPILED);
  26.567 +#endif
  26.568 +  } else {
  26.569 +    cinfo->progressive_mode = FALSE;
  26.570 +    cinfo->num_scans = 1;
  26.571 +  }
  26.572 +
  26.573 +  if (cinfo->progressive_mode)	/*  TEMPORARY HACK ??? */
  26.574 +    cinfo->optimize_coding = TRUE; /* assume default tables no good for progressive mode */
  26.575 +
  26.576 +  /* Initialize my private state */
  26.577 +  if (transcode_only) {
  26.578 +    /* no main pass in transcoding */
  26.579 +    if (cinfo->optimize_coding)
  26.580 +      master->pass_type = huff_opt_pass;
  26.581 +    else
  26.582 +      master->pass_type = output_pass;
  26.583 +  } else {
  26.584 +    /* for normal compression, first pass is always this type: */
  26.585 +    master->pass_type = main_pass;
  26.586 +  }
  26.587 +  master->scan_number = 0;
  26.588 +  master->pass_number = 0;
  26.589 +  if (cinfo->optimize_coding)
  26.590 +    master->total_passes = cinfo->num_scans * 2;
  26.591 +  else
  26.592 +    master->total_passes = cinfo->num_scans;
  26.593 +}
    27.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    27.2 +++ b/libs/libjpeg/jcomapi.c	Sat Sep 19 05:51:51 2015 +0300
    27.3 @@ -0,0 +1,106 @@
    27.4 +/*
    27.5 + * jcomapi.c
    27.6 + *
    27.7 + * Copyright (C) 1994-1997, Thomas G. Lane.
    27.8 + * This file is part of the Independent JPEG Group's software.
    27.9 + * For conditions of distribution and use, see the accompanying README file.
   27.10 + *
   27.11 + * This file contains application interface routines that are used for both
   27.12 + * compression and decompression.
   27.13 + */
   27.14 +
   27.15 +#define JPEG_INTERNALS
   27.16 +#include "jinclude.h"
   27.17 +#include "jpeglib.h"
   27.18 +
   27.19 +
   27.20 +/*
   27.21 + * Abort processing of a JPEG compression or decompression operation,
   27.22 + * but don't destroy the object itself.
   27.23 + *
   27.24 + * For this, we merely clean up all the nonpermanent memory pools.
   27.25 + * Note that temp files (virtual arrays) are not allowed to belong to
   27.26 + * the permanent pool, so we will be able to close all temp files here.
   27.27 + * Closing a data source or destination, if necessary, is the application's
   27.28 + * responsibility.
   27.29 + */
   27.30 +
   27.31 +GLOBAL(void)
   27.32 +jpeg_abort (j_common_ptr cinfo)
   27.33 +{
   27.34 +  int pool;
   27.35 +
   27.36 +  /* Do nothing if called on a not-initialized or destroyed JPEG object. */
   27.37 +  if (cinfo->mem == NULL)
   27.38 +    return;
   27.39 +
   27.40 +  /* Releasing pools in reverse order might help avoid fragmentation
   27.41 +   * with some (brain-damaged) malloc libraries.
   27.42 +   */
   27.43 +  for (pool = JPOOL_NUMPOOLS-1; pool > JPOOL_PERMANENT; pool--) {
   27.44 +    (*cinfo->mem->free_pool) (cinfo, pool);
   27.45 +  }
   27.46 +
   27.47 +  /* Reset overall state for possible reuse of object */
   27.48 +  if (cinfo->is_decompressor) {
   27.49 +    cinfo->global_state = DSTATE_START;
   27.50 +    /* Try to keep application from accessing now-deleted marker list.
   27.51 +     * A bit kludgy to do it here, but this is the most central place.
   27.52 +     */
   27.53 +    ((j_decompress_ptr) cinfo)->marker_list = NULL;
   27.54 +  } else {
   27.55 +    cinfo->global_state = CSTATE_START;
   27.56 +  }
   27.57 +}
   27.58 +
   27.59 +
   27.60 +/*
   27.61 + * Destruction of a JPEG object.
   27.62 + *
   27.63 + * Everything gets deallocated except the master jpeg_compress_struct itself
   27.64 + * and the error manager struct.  Both of these are supplied by the application
   27.65 + * and must be freed, if necessary, by the application.  (Often they are on
   27.66 + * the stack and so don't need to be freed anyway.)
   27.67 + * Closing a data source or destination, if necessary, is the application's
   27.68 + * responsibility.
   27.69 + */
   27.70 +
   27.71 +GLOBAL(void)
   27.72 +jpeg_destroy (j_common_ptr cinfo)
   27.73 +{
   27.74 +  /* We need only tell the memory manager to release everything. */
   27.75 +  /* NB: mem pointer is NULL if memory mgr failed to initialize. */
   27.76 +  if (cinfo->mem != NULL)
   27.77 +    (*cinfo->mem->self_destruct) (cinfo);
   27.78 +  cinfo->mem = NULL;		/* be safe if jpeg_destroy is called twice */
   27.79 +  cinfo->global_state = 0;	/* mark it destroyed */
   27.80 +}
   27.81 +
   27.82 +
   27.83 +/*
   27.84 + * Convenience routines for allocating quantization and Huffman tables.
   27.85 + * (Would jutils.c be a more reasonable place to put these?)
   27.86 + */
   27.87 +
   27.88 +GLOBAL(JQUANT_TBL *)
   27.89 +jpeg_alloc_quant_table (j_common_ptr cinfo)
   27.90 +{
   27.91 +  JQUANT_TBL *tbl;
   27.92 +
   27.93 +  tbl = (JQUANT_TBL *)
   27.94 +    (*cinfo->mem->alloc_small) (cinfo, JPOOL_PERMANENT, SIZEOF(JQUANT_TBL));
   27.95 +  tbl->sent_table = FALSE;	/* make sure this is false in any new table */
   27.96 +  return tbl;
   27.97 +}
   27.98 +
   27.99 +
  27.100 +GLOBAL(JHUFF_TBL *)
  27.101 +jpeg_alloc_huff_table (j_common_ptr cinfo)
  27.102 +{
  27.103 +  JHUFF_TBL *tbl;
  27.104 +
  27.105 +  tbl = (JHUFF_TBL *)
  27.106 +    (*cinfo->mem->alloc_small) (cinfo, JPOOL_PERMANENT, SIZEOF(JHUFF_TBL));
  27.107 +  tbl->sent_table = FALSE;	/* make sure this is false in any new table */
  27.108 +  return tbl;
  27.109 +}
    28.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    28.2 +++ b/libs/libjpeg/jconfig.h	Sat Sep 19 05:51:51 2015 +0300
    28.3 @@ -0,0 +1,45 @@
    28.4 +/* jconfig.vc --- jconfig.h for Microsoft Visual C++ on Windows 95 or NT. */
    28.5 +/* see jconfig.doc for explanations */
    28.6 +
    28.7 +#define HAVE_PROTOTYPES
    28.8 +#define HAVE_UNSIGNED_CHAR
    28.9 +#define HAVE_UNSIGNED_SHORT
   28.10 +/* #define void char */
   28.11 +/* #define const */
   28.12 +#undef CHAR_IS_UNSIGNED
   28.13 +#define HAVE_STDDEF_H
   28.14 +#define HAVE_STDLIB_H
   28.15 +#undef NEED_BSD_STRINGS
   28.16 +#undef NEED_SYS_TYPES_H
   28.17 +#undef NEED_FAR_POINTERS	/* we presume a 32-bit flat memory model */
   28.18 +#undef NEED_SHORT_EXTERNAL_NAMES
   28.19 +#undef INCOMPLETE_TYPES_BROKEN
   28.20 +
   28.21 +/* Define "boolean" as unsigned char, not int, per Windows custom */
   28.22 +#ifndef __RPCNDR_H__		/* don't conflict if rpcndr.h already read */
   28.23 +typedef unsigned char boolean;
   28.24 +#endif
   28.25 +#define HAVE_BOOLEAN		/* prevent jmorecfg.h from redefining it */
   28.26 +
   28.27 +
   28.28 +#ifdef JPEG_INTERNALS
   28.29 +
   28.30 +#undef RIGHT_SHIFT_IS_UNSIGNED
   28.31 +
   28.32 +#endif /* JPEG_INTERNALS */
   28.33 +
   28.34 +#ifdef JPEG_CJPEG_DJPEG
   28.35 +
   28.36 +#define BMP_SUPPORTED		/* BMP image file format */
   28.37 +#define GIF_SUPPORTED		/* GIF image file format */
   28.38 +#define PPM_SUPPORTED		/* PBMPLUS PPM/PGM image file format */
   28.39 +#undef RLE_SUPPORTED		/* Utah RLE image file format */
   28.40 +#define TARGA_SUPPORTED		/* Targa image file format */
   28.41 +
   28.42 +#define TWO_FILE_COMMANDLINE	/* optional */
   28.43 +#define USE_SETMODE		/* Microsoft has setmode() */
   28.44 +#undef NEED_SIGNAL_CATCHER
   28.45 +#undef DONT_USE_B_MODE
   28.46 +#undef PROGRESS_REPORT		/* optional */
   28.47 +
   28.48 +#endif /* JPEG_CJPEG_DJPEG */
    29.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    29.2 +++ b/libs/libjpeg/jcparam.c	Sat Sep 19 05:51:51 2015 +0300
    29.3 @@ -0,0 +1,610 @@
    29.4 +/*
    29.5 + * jcparam.c
    29.6 + *
    29.7 + * Copyright (C) 1991-1998, Thomas G. Lane.
    29.8 + * This file is part of the Independent JPEG Group's software.
    29.9 + * For conditions of distribution and use, see the accompanying README file.
   29.10 + *
   29.11 + * This file contains optional default-setting code for the JPEG compressor.
   29.12 + * Applications do not have to use this file, but those that don't use it
   29.13 + * must know a lot more about the innards of the JPEG code.
   29.14 + */
   29.15 +
   29.16 +#define JPEG_INTERNALS
   29.17 +#include "jinclude.h"
   29.18 +#include "jpeglib.h"
   29.19 +
   29.20 +
   29.21 +/*
   29.22 + * Quantization table setup routines
   29.23 + */
   29.24 +
   29.25 +GLOBAL(void)
   29.26 +jpeg_add_quant_table (j_compress_ptr cinfo, int which_tbl,
   29.27 +		      const unsigned int *basic_table,
   29.28 +		      int scale_factor, boolean force_baseline)
   29.29 +/* Define a quantization table equal to the basic_table times
   29.30 + * a scale factor (given as a percentage).
   29.31 + * If force_baseline is TRUE, the computed quantization table entries
   29.32 + * are limited to 1..255 for JPEG baseline compatibility.
   29.33 + */
   29.34 +{
   29.35 +  JQUANT_TBL ** qtblptr;
   29.36 +  int i;
   29.37 +  long temp;
   29.38 +
   29.39 +  /* Safety check to ensure start_compress not called yet. */
   29.40 +  if (cinfo->global_state != CSTATE_START)
   29.41 +    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
   29.42 +
   29.43 +  if (which_tbl < 0 || which_tbl >= NUM_QUANT_TBLS)
   29.44 +    ERREXIT1(cinfo, JERR_DQT_INDEX, which_tbl);
   29.45 +
   29.46 +  qtblptr = & cinfo->quant_tbl_ptrs[which_tbl];
   29.47 +
   29.48 +  if (*qtblptr == NULL)
   29.49 +    *qtblptr = jpeg_alloc_quant_table((j_common_ptr) cinfo);
   29.50 +
   29.51 +  for (i = 0; i < DCTSIZE2; i++) {
   29.52 +    temp = ((long) basic_table[i] * scale_factor + 50L) / 100L;
   29.53 +    /* limit the values to the valid range */
   29.54 +    if (temp <= 0L) temp = 1L;
   29.55 +    if (temp > 32767L) temp = 32767L; /* max quantizer needed for 12 bits */
   29.56 +    if (force_baseline && temp > 255L)
   29.57 +      temp = 255L;		/* limit to baseline range if requested */
   29.58 +    (*qtblptr)->quantval[i] = (UINT16) temp;
   29.59 +  }
   29.60 +
   29.61 +  /* Initialize sent_table FALSE so table will be written to JPEG file. */
   29.62 +  (*qtblptr)->sent_table = FALSE;
   29.63 +}
   29.64 +
   29.65 +
   29.66 +GLOBAL(void)
   29.67 +jpeg_set_linear_quality (j_compress_ptr cinfo, int scale_factor,
   29.68 +			 boolean force_baseline)
   29.69 +/* Set or change the 'quality' (quantization) setting, using default tables
   29.70 + * and a straight percentage-scaling quality scale.  In most cases it's better
   29.71 + * to use jpeg_set_quality (below); this entry point is provided for
   29.72 + * applications that insist on a linear percentage scaling.
   29.73 + */
   29.74 +{
   29.75 +  /* These are the sample quantization tables given in JPEG spec section K.1.
   29.76 +   * The spec says that the values given produce "good" quality, and
   29.77 +   * when divided by 2, "very good" quality.
   29.78 +   */
   29.79 +  static const unsigned int std_luminance_quant_tbl[DCTSIZE2] = {
   29.80 +    16,  11,  10,  16,  24,  40,  51,  61,
   29.81 +    12,  12,  14,  19,  26,  58,  60,  55,
   29.82 +    14,  13,  16,  24,  40,  57,  69,  56,
   29.83 +    14,  17,  22,  29,  51,  87,  80,  62,
   29.84 +    18,  22,  37,  56,  68, 109, 103,  77,
   29.85 +    24,  35,  55,  64,  81, 104, 113,  92,
   29.86 +    49,  64,  78,  87, 103, 121, 120, 101,
   29.87 +    72,  92,  95,  98, 112, 100, 103,  99
   29.88 +  };
   29.89 +  static const unsigned int std_chrominance_quant_tbl[DCTSIZE2] = {
   29.90 +    17,  18,  24,  47,  99,  99,  99,  99,
   29.91 +    18,  21,  26,  66,  99,  99,  99,  99,
   29.92 +    24,  26,  56,  99,  99,  99,  99,  99,
   29.93 +    47,  66,  99,  99,  99,  99,  99,  99,
   29.94 +    99,  99,  99,  99,  99,  99,  99,  99,
   29.95 +    99,  99,  99,  99,  99,  99,  99,  99,
   29.96 +    99,  99,  99,  99,  99,  99,  99,  99,
   29.97 +    99,  99,  99,  99,  99,  99,  99,  99
   29.98 +  };
   29.99 +
  29.100 +  /* Set up two quantization tables using the specified scaling */
  29.101 +  jpeg_add_quant_table(cinfo, 0, std_luminance_quant_tbl,
  29.102 +		       scale_factor, force_baseline);
  29.103 +  jpeg_add_quant_table(cinfo, 1, std_chrominance_quant_tbl,
  29.104 +		       scale_factor, force_baseline);
  29.105 +}
  29.106 +
  29.107 +
  29.108 +GLOBAL(int)
  29.109 +jpeg_quality_scaling (int quality)
  29.110 +/* Convert a user-specified quality rating to a percentage scaling factor
  29.111 + * for an underlying quantization table, using our recommended scaling curve.
  29.112 + * The input 'quality' factor should be 0 (terrible) to 100 (very good).
  29.113 + */
  29.114 +{
  29.115 +  /* Safety limit on quality factor.  Convert 0 to 1 to avoid zero divide. */
  29.116 +  if (quality <= 0) quality = 1;
  29.117 +  if (quality > 100) quality = 100;
  29.118 +
  29.119 +  /* The basic table is used as-is (scaling 100) for a quality of 50.
  29.120 +   * Qualities 50..100 are converted to scaling percentage 200 - 2*Q;
  29.121 +   * note that at Q=100 the scaling is 0, which will cause jpeg_add_quant_table
  29.122 +   * to make all the table entries 1 (hence, minimum quantization loss).
  29.123 +   * Qualities 1..50 are converted to scaling percentage 5000/Q.
  29.124 +   */
  29.125 +  if (quality < 50)
  29.126 +    quality = 5000 / quality;
  29.127 +  else
  29.128 +    quality = 200 - quality*2;
  29.129 +
  29.130 +  return quality;
  29.131 +}
  29.132 +
  29.133 +
  29.134 +GLOBAL(void)
  29.135 +jpeg_set_quality (j_compress_ptr cinfo, int quality, boolean force_baseline)
  29.136 +/* Set or change the 'quality' (quantization) setting, using default tables.
  29.137 + * This is the standard quality-adjusting entry point for typical user
  29.138 + * interfaces; only those who want detailed control over quantization tables
  29.139 + * would use the preceding three routines directly.
  29.140 + */
  29.141 +{
  29.142 +  /* Convert user 0-100 rating to percentage scaling */
  29.143 +  quality = jpeg_quality_scaling(quality);
  29.144 +
  29.145 +  /* Set up standard quality tables */
  29.146 +  jpeg_set_linear_quality(cinfo, quality, force_baseline);
  29.147 +}
  29.148 +
  29.149 +
  29.150 +/*
  29.151 + * Huffman table setup routines
  29.152 + */
  29.153 +
  29.154 +LOCAL(void)
  29.155 +add_huff_table (j_compress_ptr cinfo,
  29.156 +		JHUFF_TBL **htblptr, const UINT8 *bits, const UINT8 *val)
  29.157 +/* Define a Huffman table */
  29.158 +{
  29.159 +  int nsymbols, len;
  29.160 +
  29.161 +  if (*htblptr == NULL)
  29.162 +    *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
  29.163 +
  29.164 +  /* Copy the number-of-symbols-of-each-code-length counts */
  29.165 +  MEMCOPY((*htblptr)->bits, bits, SIZEOF((*htblptr)->bits));
  29.166 +
  29.167 +  /* Validate the counts.  We do this here mainly so we can copy the right
  29.168 +   * number of symbols from the val[] array, without risking marching off
  29.169 +   * the end of memory.  jchuff.c will do a more thorough test later.
  29.170 +   */
  29.171 +  nsymbols = 0;
  29.172 +  for (len = 1; len <= 16; len++)
  29.173 +    nsymbols += bits[len];
  29.174 +  if (nsymbols < 1 || nsymbols > 256)
  29.175 +    ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
  29.176 +
  29.177 +  MEMCOPY((*htblptr)->huffval, val, nsymbols * SIZEOF(UINT8));
  29.178 +
  29.179 +  /* Initialize sent_table FALSE so table will be written to JPEG file. */
  29.180 +  (*htblptr)->sent_table = FALSE;
  29.181 +}
  29.182 +
  29.183 +
  29.184 +LOCAL(void)
  29.185 +std_huff_tables (j_compress_ptr cinfo)
  29.186 +/* Set up the standard Huffman tables (cf. JPEG standard section K.3) */
  29.187 +/* IMPORTANT: these are only valid for 8-bit data precision! */
  29.188 +{
  29.189 +  static const UINT8 bits_dc_luminance[17] =
  29.190 +    { /* 0-base */ 0, 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0 };
  29.191 +  static const UINT8 val_dc_luminance[] =
  29.192 +    { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
  29.193 +  
  29.194 +  static const UINT8 bits_dc_chrominance[17] =
  29.195 +    { /* 0-base */ 0, 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 };
  29.196 +  static const UINT8 val_dc_chrominance[] =
  29.197 +    { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
  29.198 +  
  29.199 +  static const UINT8 bits_ac_luminance[17] =
  29.200 +    { /* 0-base */ 0, 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d };
  29.201 +  static const UINT8 val_ac_luminance[] =
  29.202 +    { 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12,
  29.203 +      0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
  29.204 +      0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,
  29.205 +      0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,
  29.206 +      0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16,
  29.207 +      0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
  29.208 +      0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
  29.209 +      0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
  29.210 +      0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
  29.211 +      0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
  29.212 +      0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
  29.213 +      0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
  29.214 +      0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
  29.215 +      0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
  29.216 +      0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
  29.217 +      0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,
  29.218 +      0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4,
  29.219 +      0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
  29.220 +      0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,
  29.221 +      0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
  29.222 +      0xf9, 0xfa };
  29.223 +  
  29.224 +  static const UINT8 bits_ac_chrominance[17] =
  29.225 +    { /* 0-base */ 0, 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77 };
  29.226 +  static const UINT8 val_ac_chrominance[] =
  29.227 +    { 0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21,
  29.228 +      0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
  29.229 +      0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,
  29.230 +      0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0,
  29.231 +      0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34,
  29.232 +      0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26,
  29.233 +      0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38,
  29.234 +      0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
  29.235 +      0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
  29.236 +      0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
  29.237 +      0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
  29.238 +      0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  29.239 +      0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96,
  29.240 +      0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5,
  29.241 +      0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4,
  29.242 +      0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3,
  29.243 +      0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2,
  29.244 +      0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda,
  29.245 +      0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9,
  29.246 +      0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
  29.247 +      0xf9, 0xfa };
  29.248 +  
  29.249 +  add_huff_table(cinfo, &cinfo->dc_huff_tbl_ptrs[0],
  29.250 +		 bits_dc_luminance, val_dc_luminance);
  29.251 +  add_huff_table(cinfo, &cinfo->ac_huff_tbl_ptrs[0],
  29.252 +		 bits_ac_luminance, val_ac_luminance);
  29.253 +  add_huff_table(cinfo, &cinfo->dc_huff_tbl_ptrs[1],
  29.254 +		 bits_dc_chrominance, val_dc_chrominance);
  29.255 +  add_huff_table(cinfo, &cinfo->ac_huff_tbl_ptrs[1],
  29.256 +		 bits_ac_chrominance, val_ac_chrominance);
  29.257 +}
  29.258 +
  29.259 +
  29.260 +/*
  29.261 + * Default parameter setup for compression.
  29.262 + *
  29.263 + * Applications that don't choose to use this routine must do their
  29.264 + * own setup of all these parameters.  Alternately, you can call this
  29.265 + * to establish defaults and then alter parameters selectively.  This
  29.266 + * is the recommended approach since, if we add any new parameters,
  29.267 + * your code will still work (they'll be set to reasonable defaults).
  29.268 + */
  29.269 +
  29.270 +GLOBAL(void)
  29.271 +jpeg_set_defaults (j_compress_ptr cinfo)
  29.272 +{
  29.273 +  int i;
  29.274 +
  29.275 +  /* Safety check to ensure start_compress not called yet. */
  29.276 +  if (cinfo->global_state != CSTATE_START)
  29.277 +    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  29.278 +
  29.279 +  /* Allocate comp_info array large enough for maximum component count.
  29.280 +   * Array is made permanent in case application wants to compress
  29.281 +   * multiple images at same param settings.
  29.282 +   */
  29.283 +  if (cinfo->comp_info == NULL)
  29.284 +    cinfo->comp_info = (jpeg_component_info *)
  29.285 +      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
  29.286 +				  MAX_COMPONENTS * SIZEOF(jpeg_component_info));
  29.287 +
  29.288 +  /* Initialize everything not dependent on the color space */
  29.289 +
  29.290 +  cinfo->data_precision = BITS_IN_JSAMPLE;
  29.291 +  /* Set up two quantization tables using default quality of 75 */
  29.292 +  jpeg_set_quality(cinfo, 75, TRUE);
  29.293 +  /* Set up two Huffman tables */
  29.294 +  std_huff_tables(cinfo);
  29.295 +
  29.296 +  /* Initialize default arithmetic coding conditioning */
  29.297 +  for (i = 0; i < NUM_ARITH_TBLS; i++) {
  29.298 +    cinfo->arith_dc_L[i] = 0;
  29.299 +    cinfo->arith_dc_U[i] = 1;
  29.300 +    cinfo->arith_ac_K[i] = 5;
  29.301 +  }
  29.302 +
  29.303 +  /* Default is no multiple-scan output */
  29.304 +  cinfo->scan_info = NULL;
  29.305 +  cinfo->num_scans = 0;
  29.306 +
  29.307 +  /* Expect normal source image, not raw downsampled data */
  29.308 +  cinfo->raw_data_in = FALSE;
  29.309 +
  29.310 +  /* Use Huffman coding, not arithmetic coding, by default */
  29.311 +  cinfo->arith_code = FALSE;
  29.312 +
  29.313 +  /* By default, don't do extra passes to optimize entropy coding */
  29.314 +  cinfo->optimize_coding = FALSE;
  29.315 +  /* The standard Huffman tables are only valid for 8-bit data precision.
  29.316 +   * If the precision is higher, force optimization on so that usable
  29.317 +   * tables will be computed.  This test can be removed if default tables
  29.318 +   * are supplied that are valid for the desired precision.
  29.319 +   */
  29.320 +  if (cinfo->data_precision > 8)
  29.321 +    cinfo->optimize_coding = TRUE;
  29.322 +
  29.323 +  /* By default, use the simpler non-cosited sampling alignment */
  29.324 +  cinfo->CCIR601_sampling = FALSE;
  29.325 +
  29.326 +  /* No input smoothing */
  29.327 +  cinfo->smoothing_factor = 0;
  29.328 +
  29.329 +  /* DCT algorithm preference */
  29.330 +  cinfo->dct_method = JDCT_DEFAULT;
  29.331 +
  29.332 +  /* No restart markers */
  29.333 +  cinfo->restart_interval = 0;
  29.334 +  cinfo->restart_in_rows = 0;
  29.335 +
  29.336 +  /* Fill in default JFIF marker parameters.  Note that whether the marker
  29.337 +   * will actually be written is determined by jpeg_set_colorspace.
  29.338 +   *
  29.339 +   * By default, the library emits JFIF version code 1.01.
  29.340 +   * An application that wants to emit JFIF 1.02 extension markers should set
  29.341 +   * JFIF_minor_version to 2.  We could probably get away with just defaulting
  29.342 +   * to 1.02, but there may still be some decoders in use that will complain
  29.343 +   * about that; saying 1.01 should minimize compatibility problems.
  29.344 +   */
  29.345 +  cinfo->JFIF_major_version = 1; /* Default JFIF version = 1.01 */
  29.346 +  cinfo->JFIF_minor_version = 1;
  29.347 +  cinfo->density_unit = 0;	/* Pixel size is unknown by default */
  29.348 +  cinfo->X_density = 1;		/* Pixel aspect ratio is square by default */
  29.349 +  cinfo->Y_density = 1;
  29.350 +
  29.351 +  /* Choose JPEG colorspace based on input space, set defaults accordingly */
  29.352 +
  29.353 +  jpeg_default_colorspace(cinfo);
  29.354 +}
  29.355 +
  29.356 +
  29.357 +/*
  29.358 + * Select an appropriate JPEG colorspace for in_color_space.
  29.359 + */
  29.360 +
  29.361 +GLOBAL(void)
  29.362 +jpeg_default_colorspace (j_compress_ptr cinfo)
  29.363 +{
  29.364 +  switch (cinfo->in_color_space) {
  29.365 +  case JCS_GRAYSCALE:
  29.366 +    jpeg_set_colorspace(cinfo, JCS_GRAYSCALE);
  29.367 +    break;
  29.368 +  case JCS_RGB:
  29.369 +    jpeg_set_colorspace(cinfo, JCS_YCbCr);
  29.370 +    break;
  29.371 +  case JCS_YCbCr:
  29.372 +    jpeg_set_colorspace(cinfo, JCS_YCbCr);
  29.373 +    break;
  29.374 +  case JCS_CMYK:
  29.375 +    jpeg_set_colorspace(cinfo, JCS_CMYK); /* By default, no translation */
  29.376 +    break;
  29.377 +  case JCS_YCCK:
  29.378 +    jpeg_set_colorspace(cinfo, JCS_YCCK);
  29.379 +    break;
  29.380 +  case JCS_UNKNOWN:
  29.381 +    jpeg_set_colorspace(cinfo, JCS_UNKNOWN);
  29.382 +    break;
  29.383 +  default:
  29.384 +    ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
  29.385 +  }
  29.386 +}
  29.387 +
  29.388 +
  29.389 +/*
  29.390 + * Set the JPEG colorspace, and choose colorspace-dependent default values.
  29.391 + */
  29.392 +
  29.393 +GLOBAL(void)
  29.394 +jpeg_set_colorspace (j_compress_ptr cinfo, J_COLOR_SPACE colorspace)
  29.395 +{
  29.396 +  jpeg_component_info * compptr;
  29.397 +  int ci;
  29.398 +
  29.399 +#define SET_COMP(index,id,hsamp,vsamp,quant,dctbl,actbl)  \
  29.400 +  (compptr = &cinfo->comp_info[index], \
  29.401 +   compptr->component_id = (id), \
  29.402 +   compptr->h_samp_factor = (hsamp), \
  29.403 +   compptr->v_samp_factor = (vsamp), \
  29.404 +   compptr->quant_tbl_no = (quant), \
  29.405 +   compptr->dc_tbl_no = (dctbl), \
  29.406 +   compptr->ac_tbl_no = (actbl) )
  29.407 +
  29.408 +  /* Safety check to ensure start_compress not called yet. */
  29.409 +  if (cinfo->global_state != CSTATE_START)
  29.410 +    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  29.411 +
  29.412 +  /* For all colorspaces, we use Q and Huff tables 0 for luminance components,
  29.413 +   * tables 1 for chrominance components.
  29.414 +   */
  29.415 +
  29.416 +  cinfo->jpeg_color_space = colorspace;
  29.417 +
  29.418 +  cinfo->write_JFIF_header = FALSE; /* No marker for non-JFIF colorspaces */
  29.419 +  cinfo->write_Adobe_marker = FALSE; /* write no Adobe marker by default */
  29.420 +
  29.421 +  switch (colorspace) {
  29.422 +  case JCS_GRAYSCALE:
  29.423 +    cinfo->write_JFIF_header = TRUE; /* Write a JFIF marker */
  29.424 +    cinfo->num_components = 1;
  29.425 +    /* JFIF specifies component ID 1 */
  29.426 +    SET_COMP(0, 1, 1,1, 0, 0,0);
  29.427 +    break;
  29.428 +  case JCS_RGB:
  29.429 +    cinfo->write_Adobe_marker = TRUE; /* write Adobe marker to flag RGB */
  29.430 +    cinfo->num_components = 3;
  29.431 +    SET_COMP(0, 0x52 /* 'R' */, 1,1, 0, 0,0);
  29.432 +    SET_COMP(1, 0x47 /* 'G' */, 1,1, 0, 0,0);
  29.433 +    SET_COMP(2, 0x42 /* 'B' */, 1,1, 0, 0,0);
  29.434 +    break;
  29.435 +  case JCS_YCbCr:
  29.436 +    cinfo->write_JFIF_header = TRUE; /* Write a JFIF marker */
  29.437 +    cinfo->num_components = 3;
  29.438 +    /* JFIF specifies component IDs 1,2,3 */
  29.439 +    /* We default to 2x2 subsamples of chrominance */
  29.440 +    SET_COMP(0, 1, 2,2, 0, 0,0);
  29.441 +    SET_COMP(1, 2, 1,1, 1, 1,1);
  29.442 +    SET_COMP(2, 3, 1,1, 1, 1,1);
  29.443 +    break;
  29.444 +  case JCS_CMYK:
  29.445 +    cinfo->write_Adobe_marker = TRUE; /* write Adobe marker to flag CMYK */
  29.446 +    cinfo->num_components = 4;
  29.447 +    SET_COMP(0, 0x43 /* 'C' */, 1,1, 0, 0,0);
  29.448 +    SET_COMP(1, 0x4D /* 'M' */, 1,1, 0, 0,0);
  29.449 +    SET_COMP(2, 0x59 /* 'Y' */, 1,1, 0, 0,0);
  29.450 +    SET_COMP(3, 0x4B /* 'K' */, 1,1, 0, 0,0);
  29.451 +    break;
  29.452 +  case JCS_YCCK:
  29.453 +    cinfo->write_Adobe_marker = TRUE; /* write Adobe marker to flag YCCK */
  29.454 +    cinfo->num_components = 4;
  29.455 +    SET_COMP(0, 1, 2,2, 0, 0,0);
  29.456 +    SET_COMP(1, 2, 1,1, 1, 1,1);
  29.457 +    SET_COMP(2, 3, 1,1, 1, 1,1);
  29.458 +    SET_COMP(3, 4, 2,2, 0, 0,0);
  29.459 +    break;
  29.460 +  case JCS_UNKNOWN:
  29.461 +    cinfo->num_components = cinfo->input_components;
  29.462 +    if (cinfo->num_components < 1 || cinfo->num_components > MAX_COMPONENTS)
  29.463 +      ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
  29.464 +	       MAX_COMPONENTS);
  29.465 +    for (ci = 0; ci < cinfo->num_components; ci++) {
  29.466 +      SET_COMP(ci, ci, 1,1, 0, 0,0);
  29.467 +    }
  29.468 +    break;
  29.469 +  default:
  29.470 +    ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  29.471 +  }
  29.472 +}
  29.473 +
  29.474 +
  29.475 +#ifdef C_PROGRESSIVE_SUPPORTED
  29.476 +
  29.477 +LOCAL(jpeg_scan_info *)
  29.478 +fill_a_scan (jpeg_scan_info * scanptr, int ci,
  29.479 +	     int Ss, int Se, int Ah, int Al)
  29.480 +/* Support routine: generate one scan for specified component */
  29.481 +{
  29.482 +  scanptr->comps_in_scan = 1;
  29.483 +  scanptr->component_index[0] = ci;
  29.484 +  scanptr->Ss = Ss;
  29.485 +  scanptr->Se = Se;
  29.486 +  scanptr->Ah = Ah;
  29.487 +  scanptr->Al = Al;
  29.488 +  scanptr++;
  29.489 +  return scanptr;
  29.490 +}
  29.491 +
  29.492 +LOCAL(jpeg_scan_info *)
  29.493 +fill_scans (jpeg_scan_info * scanptr, int ncomps,
  29.494 +	    int Ss, int Se, int Ah, int Al)
  29.495 +/* Support routine: generate one scan for each component */
  29.496 +{
  29.497 +  int ci;
  29.498 +
  29.499 +  for (ci = 0; ci < ncomps; ci++) {
  29.500 +    scanptr->comps_in_scan = 1;
  29.501 +    scanptr->component_index[0] = ci;
  29.502 +    scanptr->Ss = Ss;
  29.503 +    scanptr->Se = Se;
  29.504 +    scanptr->Ah = Ah;
  29.505 +    scanptr->Al = Al;
  29.506 +    scanptr++;
  29.507 +  }
  29.508 +  return scanptr;
  29.509 +}
  29.510 +
  29.511 +LOCAL(jpeg_scan_info *)
  29.512 +fill_dc_scans (jpeg_scan_info * scanptr, int ncomps, int Ah, int Al)
  29.513 +/* Support routine: generate interleaved DC scan if possible, else N scans */
  29.514 +{
  29.515 +  int ci;
  29.516 +
  29.517 +  if (ncomps <= MAX_COMPS_IN_SCAN) {
  29.518 +    /* Single interleaved DC scan */
  29.519 +    scanptr->comps_in_scan = ncomps;
  29.520 +    for (ci = 0; ci < ncomps; ci++)
  29.521 +      scanptr->component_index[ci] = ci;
  29.522 +    scanptr->Ss = scanptr->Se = 0;
  29.523 +    scanptr->Ah = Ah;
  29.524 +    scanptr->Al = Al;
  29.525 +    scanptr++;
  29.526 +  } else {
  29.527 +    /* Noninterleaved DC scan for each component */
  29.528 +    scanptr = fill_scans(scanptr, ncomps, 0, 0, Ah, Al);
  29.529 +  }
  29.530 +  return scanptr;
  29.531 +}
  29.532 +
  29.533 +
  29.534 +/*
  29.535 + * Create a recommended progressive-JPEG script.
  29.536 + * cinfo->num_components and cinfo->jpeg_color_space must be correct.
  29.537 + */
  29.538 +
  29.539 +GLOBAL(void)
  29.540 +jpeg_simple_progression (j_compress_ptr cinfo)
  29.541 +{
  29.542 +  int ncomps = cinfo->num_components;
  29.543 +  int nscans;
  29.544 +  jpeg_scan_info * scanptr;
  29.545 +
  29.546 +  /* Safety check to ensure start_compress not called yet. */
  29.547 +  if (cinfo->global_state != CSTATE_START)
  29.548 +    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  29.549 +
  29.550 +  /* Figure space needed for script.  Calculation must match code below! */
  29.551 +  if (ncomps == 3 && cinfo->jpeg_color_space == JCS_YCbCr) {
  29.552 +    /* Custom script for YCbCr color images. */
  29.553 +    nscans = 10;
  29.554 +  } else {
  29.555 +    /* All-purpose script for other color spaces. */
  29.556 +    if (ncomps > MAX_COMPS_IN_SCAN)
  29.557 +      nscans = 6 * ncomps;	/* 2 DC + 4 AC scans per component */
  29.558 +    else
  29.559 +      nscans = 2 + 4 * ncomps;	/* 2 DC scans; 4 AC scans per component */
  29.560 +  }
  29.561 +
  29.562 +  /* Allocate space for script.
  29.563 +   * We need to put it in the permanent pool in case the application performs
  29.564 +   * multiple compressions without changing the settings.  To avoid a memory
  29.565 +   * leak if jpeg_simple_progression is called repeatedly for the same JPEG
  29.566 +   * object, we try to re-use previously allocated space, and we allocate
  29.567 +   * enough space to handle YCbCr even if initially asked for grayscale.
  29.568 +   */
  29.569 +  if (cinfo->script_space == NULL || cinfo->script_space_size < nscans) {
  29.570 +    cinfo->script_space_size = MAX(nscans, 10);
  29.571 +    cinfo->script_space = (jpeg_scan_info *)
  29.572 +      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
  29.573 +			cinfo->script_space_size * SIZEOF(jpeg_scan_info));
  29.574 +  }
  29.575 +  scanptr = cinfo->script_space;
  29.576 +  cinfo->scan_info = scanptr;
  29.577 +  cinfo->num_scans = nscans;
  29.578 +
  29.579 +  if (ncomps == 3 && cinfo->jpeg_color_space == JCS_YCbCr) {
  29.580 +    /* Custom script for YCbCr color images. */
  29.581 +    /* Initial DC scan */
  29.582 +    scanptr = fill_dc_scans(scanptr, ncomps, 0, 1);
  29.583 +    /* Initial AC scan: get some luma data out in a hurry */
  29.584 +    scanptr = fill_a_scan(scanptr, 0, 1, 5, 0, 2);
  29.585 +    /* Chroma data is too small to be worth expending many scans on */
  29.586 +    scanptr = fill_a_scan(scanptr, 2, 1, 63, 0, 1);
  29.587 +    scanptr = fill_a_scan(scanptr, 1, 1, 63, 0, 1);
  29.588 +    /* Complete spectral selection for luma AC */
  29.589 +    scanptr = fill_a_scan(scanptr, 0, 6, 63, 0, 2);
  29.590 +    /* Refine next bit of luma AC */
  29.591 +    scanptr = fill_a_scan(scanptr, 0, 1, 63, 2, 1);
  29.592 +    /* Finish DC successive approximation */
  29.593 +    scanptr = fill_dc_scans(scanptr, ncomps, 1, 0);
  29.594 +    /* Finish AC successive approximation */
  29.595 +    scanptr = fill_a_scan(scanptr, 2, 1, 63, 1, 0);
  29.596 +    scanptr = fill_a_scan(scanptr, 1, 1, 63, 1, 0);
  29.597 +    /* Luma bottom bit comes last since it's usually largest scan */
  29.598 +    scanptr = fill_a_scan(scanptr, 0, 1, 63, 1, 0);
  29.599 +  } else {
  29.600 +    /* All-purpose script for other color spaces. */
  29.601 +    /* Successive approximation first pass */
  29.602 +    scanptr = fill_dc_scans(scanptr, ncomps, 0, 1);
  29.603 +    scanptr = fill_scans(scanptr, ncomps, 1, 5, 0, 2);
  29.604 +    scanptr = fill_scans(scanptr, ncomps, 6, 63, 0, 2);
  29.605 +    /* Successive approximation second pass */
  29.606 +    scanptr = fill_scans(scanptr, ncomps, 1, 63, 2, 1);
  29.607 +    /* Successive approximation final pass */
  29.608 +    scanptr = fill_dc_scans(scanptr, ncomps, 1, 0);
  29.609 +    scanptr = fill_scans(scanptr, ncomps, 1, 63, 1, 0);
  29.610 +  }
  29.611 +}
  29.612 +
  29.613 +#endif /* C_PROGRESSIVE_SUPPORTED */
    30.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    30.2 +++ b/libs/libjpeg/jcphuff.c	Sat Sep 19 05:51:51 2015 +0300
    30.3 @@ -0,0 +1,833 @@
    30.4 +/*
    30.5 + * jcphuff.c
    30.6 + *
    30.7 + * Copyright (C) 1995-1997, Thomas G. Lane.
    30.8 + * This file is part of the Independent JPEG Group's software.
    30.9 + * For conditions of distribution and use, see the accompanying README file.
   30.10 + *
   30.11 + * This file contains Huffman entropy encoding routines for progressive JPEG.
   30.12 + *
   30.13 + * We do not support output suspension in this module, since the library
   30.14 + * currently does not allow multiple-scan files to be written with output
   30.15 + * suspension.
   30.16 + */
   30.17 +
   30.18 +#define JPEG_INTERNALS
   30.19 +#include "jinclude.h"
   30.20 +#include "jpeglib.h"
   30.21 +#include "jchuff.h"		/* Declarations shared with jchuff.c */
   30.22 +
   30.23 +#ifdef C_PROGRESSIVE_SUPPORTED
   30.24 +
   30.25 +/* Expanded entropy encoder object for progressive Huffman encoding. */
   30.26 +
   30.27 +typedef struct {
   30.28 +  struct jpeg_entropy_encoder pub; /* public fields */
   30.29 +
   30.30 +  /* Mode flag: TRUE for optimization, FALSE for actual data output */
   30.31 +  boolean gather_statistics;
   30.32 +
   30.33 +  /* Bit-level coding status.
   30.34 +   * next_output_byte/free_in_buffer are local copies of cinfo->dest fields.
   30.35 +   */
   30.36 +  JOCTET * next_output_byte;	/* => next byte to write in buffer */
   30.37 +  size_t free_in_buffer;	/* # of byte spaces remaining in buffer */
   30.38 +  INT32 put_buffer;		/* current bit-accumulation buffer */
   30.39 +  int put_bits;			/* # of bits now in it */
   30.40 +  j_compress_ptr cinfo;		/* link to cinfo (needed for dump_buffer) */
   30.41 +
   30.42 +  /* Coding status for DC components */
   30.43 +  int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */
   30.44 +
   30.45 +  /* Coding status for AC components */
   30.46 +  int ac_tbl_no;		/* the table number of the single component */
   30.47 +  unsigned int EOBRUN;		/* run length of EOBs */
   30.48 +  unsigned int BE;		/* # of buffered correction bits before MCU */
   30.49 +  char * bit_buffer;		/* buffer for correction bits (1 per char) */
   30.50 +  /* packing correction bits tightly would save some space but cost time... */
   30.51 +
   30.52 +  unsigned int restarts_to_go;	/* MCUs left in this restart interval */
   30.53 +  int next_restart_num;		/* next restart number to write (0-7) */
   30.54 +
   30.55 +  /* Pointers to derived tables (these workspaces have image lifespan).
   30.56 +   * Since any one scan codes only DC or only AC, we only need one set
   30.57 +   * of tables, not one for DC and one for AC.
   30.58 +   */
   30.59 +  c_derived_tbl * derived_tbls[NUM_HUFF_TBLS];
   30.60 +
   30.61 +  /* Statistics tables for optimization; again, one set is enough */
   30.62 +  long * count_ptrs[NUM_HUFF_TBLS];
   30.63 +} phuff_entropy_encoder;
   30.64 +
   30.65 +typedef phuff_entropy_encoder * phuff_entropy_ptr;
   30.66 +
   30.67 +/* MAX_CORR_BITS is the number of bits the AC refinement correction-bit
   30.68 + * buffer can hold.  Larger sizes may slightly improve compression, but
   30.69 + * 1000 is already well into the realm of overkill.
   30.70 + * The minimum safe size is 64 bits.
   30.71 + */
   30.72 +
   30.73 +#define MAX_CORR_BITS  1000	/* Max # of correction bits I can buffer */
   30.74 +
   30.75 +/* IRIGHT_SHIFT is like RIGHT_SHIFT, but works on int rather than INT32.
   30.76 + * We assume that int right shift is unsigned if INT32 right shift is,
   30.77 + * which should be safe.
   30.78 + */
   30.79 +
   30.80 +#ifdef RIGHT_SHIFT_IS_UNSIGNED
   30.81 +#define ISHIFT_TEMPS	int ishift_temp;
   30.82 +#define IRIGHT_SHIFT(x,shft)  \
   30.83 +	((ishift_temp = (x)) < 0 ? \
   30.84 +	 (ishift_temp >> (shft)) | ((~0) << (16-(shft))) : \
   30.85 +	 (ishift_temp >> (shft)))
   30.86 +#else
   30.87 +#define ISHIFT_TEMPS
   30.88 +#define IRIGHT_SHIFT(x,shft)	((x) >> (shft))
   30.89 +#endif
   30.90 +
   30.91 +/* Forward declarations */
   30.92 +METHODDEF(boolean) encode_mcu_DC_first JPP((j_compress_ptr cinfo,
   30.93 +					    JBLOCKROW *MCU_data));
   30.94 +METHODDEF(boolean) encode_mcu_AC_first JPP((j_compress_ptr cinfo,
   30.95 +					    JBLOCKROW *MCU_data));
   30.96 +METHODDEF(boolean) encode_mcu_DC_refine JPP((j_compress_ptr cinfo,
   30.97 +					     JBLOCKROW *MCU_data));
   30.98 +METHODDEF(boolean) encode_mcu_AC_refine JPP((j_compress_ptr cinfo,
   30.99 +					     JBLOCKROW *MCU_data));
  30.100 +METHODDEF(void) finish_pass_phuff JPP((j_compress_ptr cinfo));
  30.101 +METHODDEF(void) finish_pass_gather_phuff JPP((j_compress_ptr cinfo));
  30.102 +
  30.103 +
  30.104 +/*
  30.105 + * Initialize for a Huffman-compressed scan using progressive JPEG.
  30.106 + */
  30.107 +
  30.108 +METHODDEF(void)
  30.109 +start_pass_phuff (j_compress_ptr cinfo, boolean gather_statistics)
  30.110 +{  
  30.111 +  phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
  30.112 +  boolean is_DC_band;
  30.113 +  int ci, tbl;
  30.114 +  jpeg_component_info * compptr;
  30.115 +
  30.116 +  entropy->cinfo = cinfo;
  30.117 +  entropy->gather_statistics = gather_statistics;
  30.118 +
  30.119 +  is_DC_band = (cinfo->Ss == 0);
  30.120 +
  30.121 +  /* We assume jcmaster.c already validated the scan parameters. */
  30.122 +
  30.123 +  /* Select execution routines */
  30.124 +  if (cinfo->Ah == 0) {
  30.125 +    if (is_DC_band)
  30.126 +      entropy->pub.encode_mcu = encode_mcu_DC_first;
  30.127 +    else
  30.128 +      entropy->pub.encode_mcu = encode_mcu_AC_first;
  30.129 +  } else {
  30.130 +    if (is_DC_band)
  30.131 +      entropy->pub.encode_mcu = encode_mcu_DC_refine;
  30.132 +    else {
  30.133 +      entropy->pub.encode_mcu = encode_mcu_AC_refine;
  30.134 +      /* AC refinement needs a correction bit buffer */
  30.135 +      if (entropy->bit_buffer == NULL)
  30.136 +	entropy->bit_buffer = (char *)
  30.137 +	  (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  30.138 +				      MAX_CORR_BITS * SIZEOF(char));
  30.139 +    }
  30.140 +  }
  30.141 +  if (gather_statistics)
  30.142 +    entropy->pub.finish_pass = finish_pass_gather_phuff;
  30.143 +  else
  30.144 +    entropy->pub.finish_pass = finish_pass_phuff;
  30.145 +
  30.146 +  /* Only DC coefficients may be interleaved, so cinfo->comps_in_scan = 1
  30.147 +   * for AC coefficients.
  30.148 +   */
  30.149 +  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  30.150 +    compptr = cinfo->cur_comp_info[ci];
  30.151 +    /* Initialize DC predictions to 0 */
  30.152 +    entropy->last_dc_val[ci] = 0;
  30.153 +    /* Get table index */
  30.154 +    if (is_DC_band) {
  30.155 +      if (cinfo->Ah != 0)	/* DC refinement needs no table */
  30.156 +	continue;
  30.157 +      tbl = compptr->dc_tbl_no;
  30.158 +    } else {
  30.159 +      entropy->ac_tbl_no = tbl = compptr->ac_tbl_no;
  30.160 +    }
  30.161 +    if (gather_statistics) {
  30.162 +      /* Check for invalid table index */
  30.163 +      /* (make_c_derived_tbl does this in the other path) */
  30.164 +      if (tbl < 0 || tbl >= NUM_HUFF_TBLS)
  30.165 +        ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tbl);
  30.166 +      /* Allocate and zero the statistics tables */
  30.167 +      /* Note that jpeg_gen_optimal_table expects 257 entries in each table! */
  30.168 +      if (entropy->count_ptrs[tbl] == NULL)
  30.169 +	entropy->count_ptrs[tbl] = (long *)
  30.170 +	  (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  30.171 +				      257 * SIZEOF(long));
  30.172 +      MEMZERO(entropy->count_ptrs[tbl], 257 * SIZEOF(long));
  30.173 +    } else {
  30.174 +      /* Compute derived values for Huffman table */
  30.175 +      /* We may do this more than once for a table, but it's not expensive */
  30.176 +      jpeg_make_c_derived_tbl(cinfo, is_DC_band, tbl,
  30.177 +			      & entropy->derived_tbls[tbl]);
  30.178 +    }
  30.179 +  }
  30.180 +
  30.181 +  /* Initialize AC stuff */
  30.182 +  entropy->EOBRUN = 0;
  30.183 +  entropy->BE = 0;
  30.184 +
  30.185 +  /* Initialize bit buffer to empty */
  30.186 +  entropy->put_buffer = 0;
  30.187 +  entropy->put_bits = 0;
  30.188 +
  30.189 +  /* Initialize restart stuff */
  30.190 +  entropy->restarts_to_go = cinfo->restart_interval;
  30.191 +  entropy->next_restart_num = 0;
  30.192 +}
  30.193 +
  30.194 +
  30.195 +/* Outputting bytes to the file.
  30.196 + * NB: these must be called only when actually outputting,
  30.197 + * that is, entropy->gather_statistics == FALSE.
  30.198 + */
  30.199 +
  30.200 +/* Emit a byte */
  30.201 +#define emit_byte(entropy,val)  \
  30.202 +	{ *(entropy)->next_output_byte++ = (JOCTET) (val);  \
  30.203 +	  if (--(entropy)->free_in_buffer == 0)  \
  30.204 +	    dump_buffer(entropy); }
  30.205 +
  30.206 +
  30.207 +LOCAL(void)
  30.208 +dump_buffer (phuff_entropy_ptr entropy)
  30.209 +/* Empty the output buffer; we do not support suspension in this module. */
  30.210 +{
  30.211 +  struct jpeg_destination_mgr * dest = entropy->cinfo->dest;
  30.212 +
  30.213 +  if (! (*dest->empty_output_buffer) (entropy->cinfo))
  30.214 +    ERREXIT(entropy->cinfo, JERR_CANT_SUSPEND);
  30.215 +  /* After a successful buffer dump, must reset buffer pointers */
  30.216 +  entropy->next_output_byte = dest->next_output_byte;
  30.217 +  entropy->free_in_buffer = dest->free_in_buffer;
  30.218 +}
  30.219 +
  30.220 +
  30.221 +/* Outputting bits to the file */
  30.222 +
  30.223 +/* Only the right 24 bits of put_buffer are used; the valid bits are
  30.224 + * left-justified in this part.  At most 16 bits can be passed to emit_bits
  30.225 + * in one call, and we never retain more than 7 bits in put_buffer
  30.226 + * between calls, so 24 bits are sufficient.
  30.227 + */
  30.228 +
  30.229 +INLINE
  30.230 +LOCAL(void)
  30.231 +emit_bits (phuff_entropy_ptr entropy, unsigned int code, int size)
  30.232 +/* Emit some bits, unless we are in gather mode */
  30.233 +{
  30.234 +  /* This routine is heavily used, so it's worth coding tightly. */
  30.235 +  register INT32 put_buffer = (INT32) code;
  30.236 +  register int put_bits = entropy->put_bits;
  30.237 +
  30.238 +  /* if size is 0, caller used an invalid Huffman table entry */
  30.239 +  if (size == 0)
  30.240 +    ERREXIT(entropy->cinfo, JERR_HUFF_MISSING_CODE);
  30.241 +
  30.242 +  if (entropy->gather_statistics)
  30.243 +    return;			/* do nothing if we're only getting stats */
  30.244 +
  30.245 +  put_buffer &= (((INT32) 1)<<size) - 1; /* mask off any extra bits in code */
  30.246 +  
  30.247 +  put_bits += size;		/* new number of bits in buffer */
  30.248 +  
  30.249 +  put_buffer <<= 24 - put_bits; /* align incoming bits */
  30.250 +
  30.251 +  put_buffer |= entropy->put_buffer; /* and merge with old buffer contents */
  30.252 +
  30.253 +  while (put_bits >= 8) {
  30.254 +    int c = (int) ((put_buffer >> 16) & 0xFF);
  30.255 +    
  30.256 +    emit_byte(entropy, c);
  30.257 +    if (c == 0xFF) {		/* need to stuff a zero byte? */
  30.258 +      emit_byte(entropy, 0);
  30.259 +    }
  30.260 +    put_buffer <<= 8;
  30.261 +    put_bits -= 8;
  30.262 +  }
  30.263 +
  30.264 +  entropy->put_buffer = put_buffer; /* update variables */
  30.265 +  entropy->put_bits = put_bits;
  30.266 +}
  30.267 +
  30.268 +
  30.269 +LOCAL(void)
  30.270 +flush_bits (phuff_entropy_ptr entropy)
  30.271 +{
  30.272 +  emit_bits(entropy, 0x7F, 7); /* fill any partial byte with ones */
  30.273 +  entropy->put_buffer = 0;     /* and reset bit-buffer to empty */
  30.274 +  entropy->put_bits = 0;
  30.275 +}
  30.276 +
  30.277 +
  30.278 +/*
  30.279 + * Emit (or just count) a Huffman symbol.
  30.280 + */
  30.281 +
  30.282 +INLINE
  30.283 +LOCAL(void)
  30.284 +emit_symbol (phuff_entropy_ptr entropy, int tbl_no, int symbol)
  30.285 +{
  30.286 +  if (entropy->gather_statistics)
  30.287 +    entropy->count_ptrs[tbl_no][symbol]++;
  30.288 +  else {
  30.289 +    c_derived_tbl * tbl = entropy->derived_tbls[tbl_no];
  30.290 +    emit_bits(entropy, tbl->ehufco[symbol], tbl->ehufsi[symbol]);
  30.291 +  }
  30.292 +}
  30.293 +
  30.294 +
  30.295 +/*
  30.296 + * Emit bits from a correction bit buffer.
  30.297 + */
  30.298 +
  30.299 +LOCAL(void)
  30.300 +emit_buffered_bits (phuff_entropy_ptr entropy, char * bufstart,
  30.301 +		    unsigned int nbits)
  30.302 +{
  30.303 +  if (entropy->gather_statistics)
  30.304 +    return;			/* no real work */
  30.305 +
  30.306 +  while (nbits > 0) {
  30.307 +    emit_bits(entropy, (unsigned int) (*bufstart), 1);
  30.308 +    bufstart++;
  30.309 +    nbits--;
  30.310 +  }
  30.311 +}
  30.312 +
  30.313 +
  30.314 +/*
  30.315 + * Emit any pending EOBRUN symbol.
  30.316 + */
  30.317 +
  30.318 +LOCAL(void)
  30.319 +emit_eobrun (phuff_entropy_ptr entropy)
  30.320 +{
  30.321 +  register int temp, nbits;
  30.322 +
  30.323 +  if (entropy->EOBRUN > 0) {	/* if there is any pending EOBRUN */
  30.324 +    temp = entropy->EOBRUN;
  30.325 +    nbits = 0;
  30.326 +    while ((temp >>= 1))
  30.327 +      nbits++;
  30.328 +    /* safety check: shouldn't happen given limited correction-bit buffer */
  30.329 +    if (nbits > 14)
  30.330 +      ERREXIT(entropy->cinfo, JERR_HUFF_MISSING_CODE);
  30.331 +
  30.332 +    emit_symbol(entropy, entropy->ac_tbl_no, nbits << 4);
  30.333 +    if (nbits)
  30.334 +      emit_bits(entropy, entropy->EOBRUN, nbits);
  30.335 +
  30.336 +    entropy->EOBRUN = 0;
  30.337 +
  30.338 +    /* Emit any buffered correction bits */
  30.339 +    emit_buffered_bits(entropy, entropy->bit_buffer, entropy->BE);
  30.340 +    entropy->BE = 0;
  30.341 +  }
  30.342 +}
  30.343 +
  30.344 +
  30.345 +/*
  30.346 + * Emit a restart marker & resynchronize predictions.
  30.347 + */
  30.348 +
  30.349 +LOCAL(void)
  30.350 +emit_restart (phuff_entropy_ptr entropy, int restart_num)
  30.351 +{
  30.352 +  int ci;
  30.353 +
  30.354 +  emit_eobrun(entropy);
  30.355 +
  30.356 +  if (! entropy->gather_statistics) {
  30.357 +    flush_bits(entropy);
  30.358 +    emit_byte(entropy, 0xFF);
  30.359 +    emit_byte(entropy, JPEG_RST0 + restart_num);
  30.360 +  }
  30.361 +
  30.362 +  if (entropy->cinfo->Ss == 0) {
  30.363 +    /* Re-initialize DC predictions to 0 */
  30.364 +    for (ci = 0; ci < entropy->cinfo->comps_in_scan; ci++)
  30.365 +      entropy->last_dc_val[ci] = 0;
  30.366 +  } else {
  30.367 +    /* Re-initialize all AC-related fields to 0 */
  30.368 +    entropy->EOBRUN = 0;
  30.369 +    entropy->BE = 0;
  30.370 +  }
  30.371 +}
  30.372 +
  30.373 +
  30.374 +/*
  30.375 + * MCU encoding for DC initial scan (either spectral selection,
  30.376 + * or first pass of successive approximation).
  30.377 + */
  30.378 +
  30.379 +METHODDEF(boolean)
  30.380 +encode_mcu_DC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
  30.381 +{
  30.382 +  phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
  30.383 +  register int temp, temp2;
  30.384 +  register int nbits;
  30.385 +  int blkn, ci;
  30.386 +  int Al = cinfo->Al;
  30.387 +  JBLOCKROW block;
  30.388 +  jpeg_component_info * compptr;
  30.389 +  ISHIFT_TEMPS
  30.390 +
  30.391 +  entropy->next_output_byte = cinfo->dest->next_output_byte;
  30.392 +  entropy->free_in_buffer = cinfo->dest->free_in_buffer;
  30.393 +
  30.394 +  /* Emit restart marker if needed */
  30.395 +  if (cinfo->restart_interval)
  30.396 +    if (entropy->restarts_to_go == 0)
  30.397 +      emit_restart(entropy, entropy->next_restart_num);
  30.398 +
  30.399 +  /* Encode the MCU data blocks */
  30.400 +  for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
  30.401 +    block = MCU_data[blkn];
  30.402 +    ci = cinfo->MCU_membership[blkn];
  30.403 +    compptr = cinfo->cur_comp_info[ci];
  30.404 +
  30.405 +    /* Compute the DC value after the required point transform by Al.
  30.406 +     * This is simply an arithmetic right shift.
  30.407 +     */
  30.408 +    temp2 = IRIGHT_SHIFT((int) ((*block)[0]), Al);
  30.409 +
  30.410 +    /* DC differences are figured on the point-transformed values. */
  30.411 +    temp = temp2 - entropy->last_dc_val[ci];
  30.412 +    entropy->last_dc_val[ci] = temp2;
  30.413 +
  30.414 +    /* Encode the DC coefficient difference per section G.1.2.1 */
  30.415 +    temp2 = temp;
  30.416 +    if (temp < 0) {
  30.417 +      temp = -temp;		/* temp is abs value of input */
  30.418 +      /* For a negative input, want temp2 = bitwise complement of abs(input) */
  30.419 +      /* This code assumes we are on a two's complement machine */
  30.420 +      temp2--;
  30.421 +    }
  30.422 +    
  30.423 +    /* Find the number of bits needed for the magnitude of the coefficient */
  30.424 +    nbits = 0;
  30.425 +    while (temp) {
  30.426 +      nbits++;
  30.427 +      temp >>= 1;
  30.428 +    }
  30.429 +    /* Check for out-of-range coefficient values.
  30.430 +     * Since we're encoding a difference, the range limit is twice as much.
  30.431 +     */
  30.432 +    if (nbits > MAX_COEF_BITS+1)
  30.433 +      ERREXIT(cinfo, JERR_BAD_DCT_COEF);
  30.434 +    
  30.435 +    /* Count/emit the Huffman-coded symbol for the number of bits */
  30.436 +    emit_symbol(entropy, compptr->dc_tbl_no, nbits);
  30.437 +    
  30.438 +    /* Emit that number of bits of the value, if positive, */
  30.439 +    /* or the complement of its magnitude, if negative. */
  30.440 +    if (nbits)			/* emit_bits rejects calls with size 0 */
  30.441 +      emit_bits(entropy, (unsigned int) temp2, nbits);
  30.442 +  }
  30.443 +
  30.444 +  cinfo->dest->next_output_byte = entropy->next_output_byte;
  30.445 +  cinfo->dest->free_in_buffer = entropy->free_in_buffer;
  30.446 +
  30.447 +  /* Update restart-interval state too */
  30.448 +  if (cinfo->restart_interval) {
  30.449 +    if (entropy->restarts_to_go == 0) {
  30.450 +      entropy->restarts_to_go = cinfo->restart_interval;
  30.451 +      entropy->next_restart_num++;
  30.452 +      entropy->next_restart_num &= 7;
  30.453 +    }
  30.454 +    entropy->restarts_to_go--;
  30.455 +  }
  30.456 +
  30.457 +  return TRUE;
  30.458 +}
  30.459 +
  30.460 +
  30.461 +/*
  30.462 + * MCU encoding for AC initial scan (either spectral selection,
  30.463 + * or first pass of successive approximation).
  30.464 + */
  30.465 +
  30.466 +METHODDEF(boolean)
  30.467 +encode_mcu_AC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
  30.468 +{
  30.469 +  phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
  30.470 +  register int temp, temp2;
  30.471 +  register int nbits;
  30.472 +  register int r, k;
  30.473 +  int Se = cinfo->Se;
  30.474 +  int Al = cinfo->Al;
  30.475 +  JBLOCKROW block;
  30.476 +
  30.477 +  entropy->next_output_byte = cinfo->dest->next_output_byte;
  30.478 +  entropy->free_in_buffer = cinfo->dest->free_in_buffer;
  30.479 +
  30.480 +  /* Emit restart marker if needed */
  30.481 +  if (cinfo->restart_interval)
  30.482 +    if (entropy->restarts_to_go == 0)
  30.483 +      emit_restart(entropy, entropy->next_restart_num);
  30.484 +
  30.485 +  /* Encode the MCU data block */
  30.486 +  block = MCU_data[0];
  30.487 +
  30.488 +  /* Encode the AC coefficients per section G.1.2.2, fig. G.3 */
  30.489 +  
  30.490 +  r = 0;			/* r = run length of zeros */
  30.491 +   
  30.492 +  for (k = cinfo->Ss; k <= Se; k++) {
  30.493 +    if ((temp = (*block)[jpeg_natural_order[k]]) == 0) {
  30.494 +      r++;
  30.495 +      continue;
  30.496 +    }
  30.497 +    /* We must apply the point transform by Al.  For AC coefficients this
  30.498 +     * is an integer division with rounding towards 0.  To do this portably
  30.499 +     * in C, we shift after obtaining the absolute value; so the code is
  30.500 +     * interwoven with finding the abs value (temp) and output bits (temp2).
  30.501 +     */
  30.502 +    if (temp < 0) {
  30.503 +      temp = -temp;		/* temp is abs value of input */
  30.504 +      temp >>= Al;		/* apply the point transform */
  30.505 +      /* For a negative coef, want temp2 = bitwise complement of abs(coef) */
  30.506 +      temp2 = ~temp;
  30.507 +    } else {
  30.508 +      temp >>= Al;		/* apply the point transform */
  30.509 +      temp2 = temp;
  30.510 +    }
  30.511 +    /* Watch out for case that nonzero coef is zero after point transform */
  30.512 +    if (temp == 0) {
  30.513 +      r++;
  30.514 +      continue;
  30.515 +    }
  30.516 +
  30.517 +    /* Emit any pending EOBRUN */
  30.518 +    if (entropy->EOBRUN > 0)
  30.519 +      emit_eobrun(entropy);
  30.520 +    /* if run length > 15, must emit special run-length-16 codes (0xF0) */
  30.521 +    while (r > 15) {
  30.522 +      emit_symbol(entropy, entropy->ac_tbl_no, 0xF0);
  30.523 +      r -= 16;
  30.524 +    }
  30.525 +
  30.526 +    /* Find the number of bits needed for the magnitude of the coefficient */
  30.527 +    nbits = 1;			/* there must be at least one 1 bit */
  30.528 +    while ((temp >>= 1))
  30.529 +      nbits++;
  30.530 +    /* Check for out-of-range coefficient values */
  30.531 +    if (nbits > MAX_COEF_BITS)
  30.532 +      ERREXIT(cinfo, JERR_BAD_DCT_COEF);
  30.533 +
  30.534 +    /* Count/emit Huffman symbol for run length / number of bits */
  30.535 +    emit_symbol(entropy, entropy->ac_tbl_no, (r << 4) + nbits);
  30.536 +
  30.537 +    /* Emit that number of bits of the value, if positive, */
  30.538 +    /* or the complement of its magnitude, if negative. */
  30.539 +    emit_bits(entropy, (unsigned int) temp2, nbits);
  30.540 +
  30.541 +    r = 0;			/* reset zero run length */
  30.542 +  }
  30.543 +
  30.544 +  if (r > 0) {			/* If there are trailing zeroes, */
  30.545 +    entropy->EOBRUN++;		/* count an EOB */
  30.546 +    if (entropy->EOBRUN == 0x7FFF)
  30.547 +      emit_eobrun(entropy);	/* force it out to avoid overflow */
  30.548 +  }
  30.549 +
  30.550 +  cinfo->dest->next_output_byte = entropy->next_output_byte;
  30.551 +  cinfo->dest->free_in_buffer = entropy->free_in_buffer;
  30.552 +
  30.553 +  /* Update restart-interval state too */
  30.554 +  if (cinfo->restart_interval) {
  30.555 +    if (entropy->restarts_to_go == 0) {
  30.556 +      entropy->restarts_to_go = cinfo->restart_interval;
  30.557 +      entropy->next_restart_num++;
  30.558 +      entropy->next_restart_num &= 7;
  30.559 +    }
  30.560 +    entropy->restarts_to_go--;
  30.561 +  }
  30.562 +
  30.563 +  return TRUE;
  30.564 +}
  30.565 +
  30.566 +
  30.567 +/*
  30.568 + * MCU encoding for DC successive approximation refinement scan.
  30.569 + * Note: we assume such scans can be multi-component, although the spec
  30.570 + * is not very clear on the point.
  30.571 + */
  30.572 +
  30.573 +METHODDEF(boolean)
  30.574 +encode_mcu_DC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
  30.575 +{
  30.576 +  phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
  30.577 +  register int temp;
  30.578 +  int blkn;
  30.579 +  int Al = cinfo->Al;
  30.580 +  JBLOCKROW block;
  30.581 +
  30.582 +  entropy->next_output_byte = cinfo->dest->next_output_byte;
  30.583 +  entropy->free_in_buffer = cinfo->dest->free_in_buffer;
  30.584 +
  30.585 +  /* Emit restart marker if needed */
  30.586 +  if (cinfo->restart_interval)
  30.587 +    if (entropy->restarts_to_go == 0)
  30.588 +      emit_restart(entropy, entropy->next_restart_num);
  30.589 +
  30.590 +  /* Encode the MCU data blocks */
  30.591 +  for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
  30.592 +    block = MCU_data[blkn];
  30.593 +
  30.594 +    /* We simply emit the Al'th bit of the DC coefficient value. */
  30.595 +    temp = (*block)[0];
  30.596 +    emit_bits(entropy, (unsigned int) (temp >> Al), 1);
  30.597 +  }
  30.598 +
  30.599 +  cinfo->dest->next_output_byte = entropy->next_output_byte;
  30.600 +  cinfo->dest->free_in_buffer = entropy->free_in_buffer;
  30.601 +
  30.602 +  /* Update restart-interval state too */
  30.603 +  if (cinfo->restart_interval) {
  30.604 +    if (entropy->restarts_to_go == 0) {
  30.605 +      entropy->restarts_to_go = cinfo->restart_interval;
  30.606 +      entropy->next_restart_num++;
  30.607 +      entropy->next_restart_num &= 7;
  30.608 +    }
  30.609 +    entropy->restarts_to_go--;
  30.610 +  }
  30.611 +
  30.612 +  return TRUE;
  30.613 +}
  30.614 +
  30.615 +
  30.616 +/*
  30.617 + * MCU encoding for AC successive approximation refinement scan.
  30.618 + */
  30.619 +
  30.620 +METHODDEF(boolean)
  30.621 +encode_mcu_AC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
  30.622 +{
  30.623 +  phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
  30.624 +  register int temp;
  30.625 +  register int r, k;
  30.626 +  int EOB;
  30.627 +  char *BR_buffer;
  30.628 +  unsigned int BR;
  30.629 +  int Se = cinfo->Se;
  30.630 +  int Al = cinfo->Al;
  30.631 +  JBLOCKROW block;
  30.632 +  int absvalues[DCTSIZE2];
  30.633 +
  30.634 +  entropy->next_output_byte = cinfo->dest->next_output_byte;
  30.635 +  entropy->free_in_buffer = cinfo->dest->free_in_buffer;
  30.636 +
  30.637 +  /* Emit restart marker if needed */
  30.638 +  if (cinfo->restart_interval)
  30.639 +    if (entropy->restarts_to_go == 0)
  30.640 +      emit_restart(entropy, entropy->next_restart_num);
  30.641 +
  30.642 +  /* Encode the MCU data block */
  30.643 +  block = MCU_data[0];
  30.644 +
  30.645 +  /* It is convenient to make a pre-pass to determine the transformed
  30.646 +   * coefficients' absolute values and the EOB position.
  30.647 +   */
  30.648 +  EOB = 0;
  30.649 +  for (k = cinfo->Ss; k <= Se; k++) {
  30.650 +    temp = (*block)[jpeg_natural_order[k]];
  30.651 +    /* We must apply the point transform by Al.  For AC coefficients this
  30.652 +     * is an integer division with rounding towards 0.  To do this portably
  30.653 +     * in C, we shift after obtaining the absolute value.
  30.654 +     */
  30.655 +    if (temp < 0)
  30.656 +      temp = -temp;		/* temp is abs value of input */
  30.657 +    temp >>= Al;		/* apply the point transform */
  30.658 +    absvalues[k] = temp;	/* save abs value for main pass */
  30.659 +    if (temp == 1)
  30.660 +      EOB = k;			/* EOB = index of last newly-nonzero coef */
  30.661 +  }
  30.662 +
  30.663 +  /* Encode the AC coefficients per section G.1.2.3, fig. G.7 */
  30.664 +  
  30.665 +  r = 0;			/* r = run length of zeros */
  30.666 +  BR = 0;			/* BR = count of buffered bits added now */
  30.667 +  BR_buffer = entropy->bit_buffer + entropy->BE; /* Append bits to buffer */
  30.668 +
  30.669 +  for (k = cinfo->Ss; k <= Se; k++) {
  30.670 +    if ((temp = absvalues[k]) == 0) {
  30.671 +      r++;
  30.672 +      continue;
  30.673 +    }
  30.674 +
  30.675 +    /* Emit any required ZRLs, but not if they can be folded into EOB */
  30.676 +    while (r > 15 && k <= EOB) {
  30.677 +      /* emit any pending EOBRUN and the BE correction bits */
  30.678 +      emit_eobrun(entropy);
  30.679 +      /* Emit ZRL */
  30.680 +      emit_symbol(entropy, entropy->ac_tbl_no, 0xF0);
  30.681 +      r -= 16;
  30.682 +      /* Emit buffered correction bits that must be associated with ZRL */
  30.683 +      emit_buffered_bits(entropy, BR_buffer, BR);
  30.684 +      BR_buffer = entropy->bit_buffer; /* BE bits are gone now */
  30.685 +      BR = 0;
  30.686 +    }
  30.687 +
  30.688 +    /* If the coef was previously nonzero, it only needs a correction bit.
  30.689 +     * NOTE: a straight translation of the spec's figure G.7 would suggest
  30.690 +     * that we also need to test r > 15.  But if r > 15, we can only get here
  30.691 +     * if k > EOB, which implies that this coefficient is not 1.
  30.692 +     */
  30.693 +    if (temp > 1) {
  30.694 +      /* The correction bit is the next bit of the absolute value. */
  30.695 +      BR_buffer[BR++] = (char) (temp & 1);
  30.696 +      continue;
  30.697 +    }
  30.698 +
  30.699 +    /* Emit any pending EOBRUN and the BE correction bits */
  30.700 +    emit_eobrun(entropy);
  30.701 +
  30.702 +    /* Count/emit Huffman symbol for run length / number of bits */
  30.703 +    emit_symbol(entropy, entropy->ac_tbl_no, (r << 4) + 1);
  30.704 +
  30.705 +    /* Emit output bit for newly-nonzero coef */
  30.706 +    temp = ((*block)[jpeg_natural_order[k]] < 0) ? 0 : 1;
  30.707 +    emit_bits(entropy, (unsigned int) temp, 1);
  30.708 +
  30.709 +    /* Emit buffered correction bits that must be associated with this code */
  30.710 +    emit_buffered_bits(entropy, BR_buffer, BR);
  30.711 +    BR_buffer = entropy->bit_buffer; /* BE bits are gone now */
  30.712 +    BR = 0;
  30.713 +    r = 0;			/* reset zero run length */
  30.714 +  }
  30.715 +
  30.716 +  if (r > 0 || BR > 0) {	/* If there are trailing zeroes, */
  30.717 +    entropy->EOBRUN++;		/* count an EOB */
  30.718 +    entropy->BE += BR;		/* concat my correction bits to older ones */
  30.719 +    /* We force out the EOB if we risk either:
  30.720 +     * 1. overflow of the EOB counter;
  30.721 +     * 2. overflow of the correction bit buffer during the next MCU.
  30.722 +     */
  30.723 +    if (entropy->EOBRUN == 0x7FFF || entropy->BE > (MAX_CORR_BITS-DCTSIZE2+1))
  30.724 +      emit_eobrun(entropy);
  30.725 +  }
  30.726 +
  30.727 +  cinfo->dest->next_output_byte = entropy->next_output_byte;
  30.728 +  cinfo->dest->free_in_buffer = entropy->free_in_buffer;
  30.729 +
  30.730 +  /* Update restart-interval state too */
  30.731 +  if (cinfo->restart_interval) {
  30.732 +    if (entropy->restarts_to_go == 0) {
  30.733 +      entropy->restarts_to_go = cinfo->restart_interval;
  30.734 +      entropy->next_restart_num++;
  30.735 +      entropy->next_restart_num &= 7;
  30.736 +    }
  30.737 +    entropy->restarts_to_go--;
  30.738 +  }
  30.739 +
  30.740 +  return TRUE;
  30.741 +}
  30.742 +
  30.743 +
  30.744 +/*
  30.745 + * Finish up at the end of a Huffman-compressed progressive scan.
  30.746 + */
  30.747 +
  30.748 +METHODDEF(void)
  30.749 +finish_pass_phuff (j_compress_ptr cinfo)
  30.750 +{   
  30.751 +  phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
  30.752 +
  30.753 +  entropy->next_output_byte = cinfo->dest->next_output_byte;
  30.754 +  entropy->free_in_buffer = cinfo->dest->free_in_buffer;
  30.755 +
  30.756 +  /* Flush out any buffered data */
  30.757 +  emit_eobrun(entropy);
  30.758 +  flush_bits(entropy);
  30.759 +
  30.760 +  cinfo->dest->next_output_byte = entropy->next_output_byte;
  30.761 +  cinfo->dest->free_in_buffer = entropy->free_in_buffer;
  30.762 +}
  30.763 +
  30.764 +
  30.765 +/*
  30.766 + * Finish up a statistics-gathering pass and create the new Huffman tables.
  30.767 + */
  30.768 +
  30.769 +METHODDEF(void)
  30.770 +finish_pass_gather_phuff (j_compress_ptr cinfo)
  30.771 +{
  30.772 +  phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
  30.773 +  boolean is_DC_band;
  30.774 +  int ci, tbl;
  30.775 +  jpeg_component_info * compptr;
  30.776 +  JHUFF_TBL **htblptr;
  30.777 +  boolean did[NUM_HUFF_TBLS];
  30.778 +
  30.779 +  /* Flush out buffered data (all we care about is counting the EOB symbol) */
  30.780 +  emit_eobrun(entropy);
  30.781 +
  30.782 +  is_DC_band = (cinfo->Ss == 0);
  30.783 +
  30.784 +  /* It's important not to apply jpeg_gen_optimal_table more than once
  30.785 +   * per table, because it clobbers the input frequency counts!
  30.786 +   */
  30.787 +  MEMZERO(did, SIZEOF(did));
  30.788 +
  30.789 +  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  30.790 +    compptr = cinfo->cur_comp_info[ci];
  30.791 +    if (is_DC_band) {
  30.792 +      if (cinfo->Ah != 0)	/* DC refinement needs no table */
  30.793 +	continue;
  30.794 +      tbl = compptr->dc_tbl_no;
  30.795 +    } else {
  30.796 +      tbl = compptr->ac_tbl_no;
  30.797 +    }
  30.798 +    if (! did[tbl]) {
  30.799 +      if (is_DC_band)
  30.800 +        htblptr = & cinfo->dc_huff_tbl_ptrs[tbl];
  30.801 +      else
  30.802 +        htblptr = & cinfo->ac_huff_tbl_ptrs[tbl];
  30.803 +      if (*htblptr == NULL)
  30.804 +        *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
  30.805 +      jpeg_gen_optimal_table(cinfo, *htblptr, entropy->count_ptrs[tbl]);
  30.806 +      did[tbl] = TRUE;
  30.807 +    }
  30.808 +  }
  30.809 +}
  30.810 +
  30.811 +
  30.812 +/*
  30.813 + * Module initialization routine for progressive Huffman entropy encoding.
  30.814 + */
  30.815 +
  30.816 +GLOBAL(void)
  30.817 +jinit_phuff_encoder (j_compress_ptr cinfo)
  30.818 +{
  30.819 +  phuff_entropy_ptr entropy;
  30.820 +  int i;
  30.821 +
  30.822 +  entropy = (phuff_entropy_ptr)
  30.823 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  30.824 +				SIZEOF(phuff_entropy_encoder));
  30.825 +  cinfo->entropy = (struct jpeg_entropy_encoder *) entropy;
  30.826 +  entropy->pub.start_pass = start_pass_phuff;
  30.827 +
  30.828 +  /* Mark tables unallocated */
  30.829 +  for (i = 0; i < NUM_HUFF_TBLS; i++) {
  30.830 +    entropy->derived_tbls[i] = NULL;
  30.831 +    entropy->count_ptrs[i] = NULL;
  30.832 +  }
  30.833 +  entropy->bit_buffer = NULL;	/* needed only in AC refinement scan */
  30.834 +}
  30.835 +
  30.836 +#endif /* C_PROGRESSIVE_SUPPORTED */
    31.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    31.2 +++ b/libs/libjpeg/jcprepct.c	Sat Sep 19 05:51:51 2015 +0300
    31.3 @@ -0,0 +1,354 @@
    31.4 +/*
    31.5 + * jcprepct.c
    31.6 + *
    31.7 + * Copyright (C) 1994-1996, Thomas G. Lane.
    31.8 + * This file is part of the Independent JPEG Group's software.
    31.9 + * For conditions of distribution and use, see the accompanying README file.
   31.10 + *
   31.11 + * This file contains the compression preprocessing controller.
   31.12 + * This controller manages the color conversion, downsampling,
   31.13 + * and edge expansion steps.
   31.14 + *
   31.15 + * Most of the complexity here is associated with buffering input rows
   31.16 + * as required by the downsampler.  See the comments at the head of
   31.17 + * jcsample.c for the downsampler's needs.
   31.18 + */
   31.19 +
   31.20 +#define JPEG_INTERNALS
   31.21 +#include "jinclude.h"
   31.22 +#include "jpeglib.h"
   31.23 +
   31.24 +
   31.25 +/* At present, jcsample.c can request context rows only for smoothing.
   31.26 + * In the future, we might also need context rows for CCIR601 sampling
   31.27 + * or other more-complex downsampling procedures.  The code to support
   31.28 + * context rows should be compiled only if needed.
   31.29 + */
   31.30 +#ifdef INPUT_SMOOTHING_SUPPORTED
   31.31 +#define CONTEXT_ROWS_SUPPORTED
   31.32 +#endif
   31.33 +
   31.34 +
   31.35 +/*
   31.36 + * For the simple (no-context-row) case, we just need to buffer one
   31.37 + * row group's worth of pixels for the downsampling step.  At the bottom of
   31.38 + * the image, we pad to a full row group by replicating the last pixel row.
   31.39 + * The downsampler's last output row is then replicated if needed to pad
   31.40 + * out to a full iMCU row.
   31.41 + *
   31.42 + * When providing context rows, we must buffer three row groups' worth of
   31.43 + * pixels.  Three row groups are physically allocated, but the row pointer
   31.44 + * arrays are made five row groups high, with the extra pointers above and
   31.45 + * below "wrapping around" to point to the last and first real row groups.
   31.46 + * This allows the downsampler to access the proper context rows.
   31.47 + * At the top and bottom of the image, we create dummy context rows by
   31.48 + * copying the first or last real pixel row.  This copying could be avoided
   31.49 + * by pointer hacking as is done in jdmainct.c, but it doesn't seem worth the
   31.50 + * trouble on the compression side.
   31.51 + */
   31.52 +
   31.53 +
   31.54 +/* Private buffer controller object */
   31.55 +
   31.56 +typedef struct {
   31.57 +  struct jpeg_c_prep_controller pub; /* public fields */
   31.58 +
   31.59 +  /* Downsampling input buffer.  This buffer holds color-converted data
   31.60 +   * until we have enough to do a downsample step.
   31.61 +   */
   31.62 +  JSAMPARRAY color_buf[MAX_COMPONENTS];
   31.63 +
   31.64 +  JDIMENSION rows_to_go;	/* counts rows remaining in source image */
   31.65 +  int next_buf_row;		/* index of next row to store in color_buf */
   31.66 +
   31.67 +#ifdef CONTEXT_ROWS_SUPPORTED	/* only needed for context case */
   31.68 +  int this_row_group;		/* starting row index of group to process */
   31.69 +  int next_buf_stop;		/* downsample when we reach this index */
   31.70 +#endif
   31.71 +} my_prep_controller;
   31.72 +
   31.73 +typedef my_prep_controller * my_prep_ptr;
   31.74 +
   31.75 +
   31.76 +/*
   31.77 + * Initialize for a processing pass.
   31.78 + */
   31.79 +
   31.80 +METHODDEF(void)
   31.81 +start_pass_prep (j_compress_ptr cinfo, J_BUF_MODE pass_mode)
   31.82 +{
   31.83 +  my_prep_ptr prep = (my_prep_ptr) cinfo->prep;
   31.84 +
   31.85 +  if (pass_mode != JBUF_PASS_THRU)
   31.86 +    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
   31.87 +
   31.88 +  /* Initialize total-height counter for detecting bottom of image */
   31.89 +  prep->rows_to_go = cinfo->image_height;
   31.90 +  /* Mark the conversion buffer empty */
   31.91 +  prep->next_buf_row = 0;
   31.92 +#ifdef CONTEXT_ROWS_SUPPORTED
   31.93 +  /* Preset additional state variables for context mode.
   31.94 +   * These aren't used in non-context mode, so we needn't test which mode.
   31.95 +   */
   31.96 +  prep->this_row_group = 0;
   31.97 +  /* Set next_buf_stop to stop after two row groups have been read in. */
   31.98 +  prep->next_buf_stop = 2 * cinfo->max_v_samp_factor;
   31.99 +#endif
  31.100 +}
  31.101 +
  31.102 +
  31.103 +/*
  31.104 + * Expand an image vertically from height input_rows to height output_rows,
  31.105 + * by duplicating the bottom row.
  31.106 + */
  31.107 +
  31.108 +LOCAL(void)
  31.109 +expand_bottom_edge (JSAMPARRAY image_data, JDIMENSION num_cols,
  31.110 +		    int input_rows, int output_rows)
  31.111 +{
  31.112 +  register int row;
  31.113 +
  31.114 +  for (row = input_rows; row < output_rows; row++) {
  31.115 +    jcopy_sample_rows(image_data, input_rows-1, image_data, row,
  31.116 +		      1, num_cols);
  31.117 +  }
  31.118 +}
  31.119 +
  31.120 +
  31.121 +/*
  31.122 + * Process some data in the simple no-context case.
  31.123 + *
  31.124 + * Preprocessor output data is counted in "row groups".  A row group
  31.125 + * is defined to be v_samp_factor sample rows of each component.
  31.126 + * Downsampling will produce this much data from each max_v_samp_factor
  31.127 + * input rows.
  31.128 + */
  31.129 +
  31.130 +METHODDEF(void)
  31.131 +pre_process_data (j_compress_ptr cinfo,
  31.132 +		  JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
  31.133 +		  JDIMENSION in_rows_avail,
  31.134 +		  JSAMPIMAGE output_buf, JDIMENSION *out_row_group_ctr,
  31.135 +		  JDIMENSION out_row_groups_avail)
  31.136 +{
  31.137 +  my_prep_ptr prep = (my_prep_ptr) cinfo->prep;
  31.138 +  int numrows, ci;
  31.139 +  JDIMENSION inrows;
  31.140 +  jpeg_component_info * compptr;
  31.141 +
  31.142 +  while (*in_row_ctr < in_rows_avail &&
  31.143 +	 *out_row_group_ctr < out_row_groups_avail) {
  31.144 +    /* Do color conversion to fill the conversion buffer. */
  31.145 +    inrows = in_rows_avail - *in_row_ctr;
  31.146 +    numrows = cinfo->max_v_samp_factor - prep->next_buf_row;
  31.147 +    numrows = (int) MIN((JDIMENSION) numrows, inrows);
  31.148 +    (*cinfo->cconvert->color_convert) (cinfo, input_buf + *in_row_ctr,
  31.149 +				       prep->color_buf,
  31.150 +				       (JDIMENSION) prep->next_buf_row,
  31.151 +				       numrows);
  31.152 +    *in_row_ctr += numrows;
  31.153 +    prep->next_buf_row += numrows;
  31.154 +    prep->rows_to_go -= numrows;
  31.155 +    /* If at bottom of image, pad to fill the conversion buffer. */
  31.156 +    if (prep->rows_to_go == 0 &&
  31.157 +	prep->next_buf_row < cinfo->max_v_samp_factor) {
  31.158 +      for (ci = 0; ci < cinfo->num_components; ci++) {
  31.159 +	expand_bottom_edge(prep->color_buf[ci], cinfo->image_width,
  31.160 +			   prep->next_buf_row, cinfo->max_v_samp_factor);
  31.161 +      }
  31.162 +      prep->next_buf_row = cinfo->max_v_samp_factor;
  31.163 +    }
  31.164 +    /* If we've filled the conversion buffer, empty it. */
  31.165 +    if (prep->next_buf_row == cinfo->max_v_samp_factor) {
  31.166 +      (*cinfo->downsample->downsample) (cinfo,
  31.167 +					prep->color_buf, (JDIMENSION) 0,
  31.168 +					output_buf, *out_row_group_ctr);
  31.169 +      prep->next_buf_row = 0;
  31.170 +      (*out_row_group_ctr)++;
  31.171 +    }
  31.172 +    /* If at bottom of image, pad the output to a full iMCU height.
  31.173 +     * Note we assume the caller is providing a one-iMCU-height output buffer!
  31.174 +     */
  31.175 +    if (prep->rows_to_go == 0 &&
  31.176 +	*out_row_group_ctr < out_row_groups_avail) {
  31.177 +      for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  31.178 +	   ci++, compptr++) {
  31.179 +	expand_bottom_edge(output_buf[ci],
  31.180 +			   compptr->width_in_blocks * DCTSIZE,
  31.181 +			   (int) (*out_row_group_ctr * compptr->v_samp_factor),
  31.182 +			   (int) (out_row_groups_avail * compptr->v_samp_factor));
  31.183 +      }
  31.184 +      *out_row_group_ctr = out_row_groups_avail;
  31.185 +      break;			/* can exit outer loop without test */
  31.186 +    }
  31.187 +  }
  31.188 +}
  31.189 +
  31.190 +
  31.191 +#ifdef CONTEXT_ROWS_SUPPORTED
  31.192 +
  31.193 +/*
  31.194 + * Process some data in the context case.
  31.195 + */
  31.196 +
  31.197 +METHODDEF(void)
  31.198 +pre_process_context (j_compress_ptr cinfo,
  31.199 +		     JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
  31.200 +		     JDIMENSION in_rows_avail,
  31.201 +		     JSAMPIMAGE output_buf, JDIMENSION *out_row_group_ctr,
  31.202 +		     JDIMENSION out_row_groups_avail)
  31.203 +{
  31.204 +  my_prep_ptr prep = (my_prep_ptr) cinfo->prep;
  31.205 +  int numrows, ci;
  31.206 +  int buf_height = cinfo->max_v_samp_factor * 3;
  31.207 +  JDIMENSION inrows;
  31.208 +
  31.209 +  while (*out_row_group_ctr < out_row_groups_avail) {
  31.210 +    if (*in_row_ctr < in_rows_avail) {
  31.211 +      /* Do color conversion to fill the conversion buffer. */
  31.212 +      inrows = in_rows_avail - *in_row_ctr;
  31.213 +      numrows = prep->next_buf_stop - prep->next_buf_row;
  31.214 +      numrows = (int) MIN((JDIMENSION) numrows, inrows);
  31.215 +      (*cinfo->cconvert->color_convert) (cinfo, input_buf + *in_row_ctr,
  31.216 +					 prep->color_buf,
  31.217 +					 (JDIMENSION) prep->next_buf_row,
  31.218 +					 numrows);
  31.219 +      /* Pad at top of image, if first time through */
  31.220 +      if (prep->rows_to_go == cinfo->image_height) {
  31.221 +	for (ci = 0; ci < cinfo->num_components; ci++) {
  31.222 +	  int row;
  31.223 +	  for (row = 1; row <= cinfo->max_v_samp_factor; row++) {
  31.224 +	    jcopy_sample_rows(prep->color_buf[ci], 0,
  31.225 +			      prep->color_buf[ci], -row,
  31.226 +			      1, cinfo->image_width);
  31.227 +	  }
  31.228 +	}
  31.229 +      }
  31.230 +      *in_row_ctr += numrows;
  31.231 +      prep->next_buf_row += numrows;
  31.232 +      prep->rows_to_go -= numrows;
  31.233 +    } else {
  31.234 +      /* Return for more data, unless we are at the bottom of the image. */
  31.235 +      if (prep->rows_to_go != 0)
  31.236 +	break;
  31.237 +      /* When at bottom of image, pad to fill the conversion buffer. */
  31.238 +      if (prep->next_buf_row < prep->next_buf_stop) {
  31.239 +	for (ci = 0; ci < cinfo->num_components; ci++) {
  31.240 +	  expand_bottom_edge(prep->color_buf[ci], cinfo->image_width,
  31.241 +			     prep->next_buf_row, prep->next_buf_stop);
  31.242 +	}
  31.243 +	prep->next_buf_row = prep->next_buf_stop;
  31.244 +      }
  31.245 +    }
  31.246 +    /* If we've gotten enough data, downsample a row group. */
  31.247 +    if (prep->next_buf_row == prep->next_buf_stop) {
  31.248 +      (*cinfo->downsample->downsample) (cinfo,
  31.249 +					prep->color_buf,
  31.250 +					(JDIMENSION) prep->this_row_group,
  31.251 +					output_buf, *out_row_group_ctr);
  31.252 +      (*out_row_group_ctr)++;
  31.253 +      /* Advance pointers with wraparound as necessary. */
  31.254 +      prep->this_row_group += cinfo->max_v_samp_factor;
  31.255 +      if (prep->this_row_group >= buf_height)
  31.256 +	prep->this_row_group = 0;
  31.257 +      if (prep->next_buf_row >= buf_height)
  31.258 +	prep->next_buf_row = 0;
  31.259 +      prep->next_buf_stop = prep->next_buf_row + cinfo->max_v_samp_factor;
  31.260 +    }
  31.261 +  }
  31.262 +}
  31.263 +
  31.264 +
  31.265 +/*
  31.266 + * Create the wrapped-around downsampling input buffer needed for context mode.
  31.267 + */
  31.268 +
  31.269 +LOCAL(void)
  31.270 +create_context_buffer (j_compress_ptr cinfo)
  31.271 +{
  31.272 +  my_prep_ptr prep = (my_prep_ptr) cinfo->prep;
  31.273 +  int rgroup_height = cinfo->max_v_samp_factor;
  31.274 +  int ci, i;
  31.275 +  jpeg_component_info * compptr;
  31.276 +  JSAMPARRAY true_buffer, fake_buffer;
  31.277 +
  31.278 +  /* Grab enough space for fake row pointers for all the components;
  31.279 +   * we need five row groups' worth of pointers for each component.
  31.280 +   */
  31.281 +  fake_buffer = (JSAMPARRAY)
  31.282 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  31.283 +				(cinfo->num_components * 5 * rgroup_height) *
  31.284 +				SIZEOF(JSAMPROW));
  31.285 +
  31.286 +  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  31.287 +       ci++, compptr++) {
  31.288 +    /* Allocate the actual buffer space (3 row groups) for this component.
  31.289 +     * We make the buffer wide enough to allow the downsampler to edge-expand
  31.290 +     * horizontally within the buffer, if it so chooses.
  31.291 +     */
  31.292 +    true_buffer = (*cinfo->mem->alloc_sarray)
  31.293 +      ((j_common_ptr) cinfo, JPOOL_IMAGE,
  31.294 +       (JDIMENSION) (((long) compptr->width_in_blocks * DCTSIZE *
  31.295 +		      cinfo->max_h_samp_factor) / compptr->h_samp_factor),
  31.296 +       (JDIMENSION) (3 * rgroup_height));
  31.297 +    /* Copy true buffer row pointers into the middle of the fake row array */
  31.298 +    MEMCOPY(fake_buffer + rgroup_height, true_buffer,
  31.299 +	    3 * rgroup_height * SIZEOF(JSAMPROW));
  31.300 +    /* Fill in the above and below wraparound pointers */
  31.301 +    for (i = 0; i < rgroup_height; i++) {
  31.302 +      fake_buffer[i] = true_buffer[2 * rgroup_height + i];
  31.303 +      fake_buffer[4 * rgroup_height + i] = true_buffer[i];
  31.304 +    }
  31.305 +    prep->color_buf[ci] = fake_buffer + rgroup_height;
  31.306 +    fake_buffer += 5 * rgroup_height; /* point to space for next component */
  31.307 +  }
  31.308 +}
  31.309 +
  31.310 +#endif /* CONTEXT_ROWS_SUPPORTED */
  31.311 +
  31.312 +
  31.313 +/*
  31.314 + * Initialize preprocessing controller.
  31.315 + */
  31.316 +
  31.317 +GLOBAL(void)
  31.318 +jinit_c_prep_controller (j_compress_ptr cinfo, boolean need_full_buffer)
  31.319 +{
  31.320 +  my_prep_ptr prep;
  31.321 +  int ci;
  31.322 +  jpeg_component_info * compptr;
  31.323 +
  31.324 +  if (need_full_buffer)		/* safety check */
  31.325 +    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  31.326 +
  31.327 +  prep = (my_prep_ptr)
  31.328 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  31.329 +				SIZEOF(my_prep_controller));
  31.330 +  cinfo->prep = (struct jpeg_c_prep_controller *) prep;
  31.331 +  prep->pub.start_pass = start_pass_prep;
  31.332 +
  31.333 +  /* Allocate the color conversion buffer.
  31.334 +   * We make the buffer wide enough to allow the downsampler to edge-expand
  31.335 +   * horizontally within the buffer, if it so chooses.
  31.336 +   */
  31.337 +  if (cinfo->downsample->need_context_rows) {
  31.338 +    /* Set up to provide context rows */
  31.339 +#ifdef CONTEXT_ROWS_SUPPORTED
  31.340 +    prep->pub.pre_process_data = pre_process_context;
  31.341 +    create_context_buffer(cinfo);
  31.342 +#else
  31.343 +    ERREXIT(cinfo, JERR_NOT_COMPILED);
  31.344 +#endif
  31.345 +  } else {
  31.346 +    /* No context, just make it tall enough for one row group */
  31.347 +    prep->pub.pre_process_data = pre_process_data;
  31.348 +    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  31.349 +	 ci++, compptr++) {
  31.350 +      prep->color_buf[ci] = (*cinfo->mem->alloc_sarray)
  31.351 +	((j_common_ptr) cinfo, JPOOL_IMAGE,
  31.352 +	 (JDIMENSION) (((long) compptr->width_in_blocks * DCTSIZE *
  31.353 +			cinfo->max_h_samp_factor) / compptr->h_samp_factor),
  31.354 +	 (JDIMENSION) cinfo->max_v_samp_factor);
  31.355 +    }
  31.356 +  }
  31.357 +}
    32.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    32.2 +++ b/libs/libjpeg/jcsample.c	Sat Sep 19 05:51:51 2015 +0300
    32.3 @@ -0,0 +1,519 @@
    32.4 +/*
    32.5 + * jcsample.c
    32.6 + *
    32.7 + * Copyright (C) 1991-1996, Thomas G. Lane.
    32.8 + * This file is part of the Independent JPEG Group's software.
    32.9 + * For conditions of distribution and use, see the accompanying README file.
   32.10 + *
   32.11 + * This file contains downsampling routines.
   32.12 + *
   32.13 + * Downsampling input data is counted in "row groups".  A row group
   32.14 + * is defined to be max_v_samp_factor pixel rows of each component,
   32.15 + * from which the downsampler produces v_samp_factor sample rows.
   32.16 + * A single row group is processed in each call to the downsampler module.
   32.17 + *
   32.18 + * The downsampler is responsible for edge-expansion of its output data
   32.19 + * to fill an integral number of DCT blocks horizontally.  The source buffer
   32.20 + * may be modified if it is helpful for this purpose (the source buffer is
   32.21 + * allocated wide enough to correspond to the desired output width).
   32.22 + * The caller (the prep controller) is responsible for vertical padding.
   32.23 + *
   32.24 + * The downsampler may request "context rows" by setting need_context_rows
   32.25 + * during startup.  In this case, the input arrays will contain at least
   32.26 + * one row group's worth of pixels above and below the passed-in data;
   32.27 + * the caller will create dummy rows at image top and bottom by replicating
   32.28 + * the first or last real pixel row.
   32.29 + *
   32.30 + * An excellent reference for image resampling is
   32.31 + *   Digital Image Warping, George Wolberg, 1990.
   32.32 + *   Pub. by IEEE Computer Society Press, Los Alamitos, CA. ISBN 0-8186-8944-7.
   32.33 + *
   32.34 + * The downsampling algorithm used here is a simple average of the source
   32.35 + * pixels covered by the output pixel.  The hi-falutin sampling literature
   32.36 + * refers to this as a "box filter".  In general the characteristics of a box
   32.37 + * filter are not very good, but for the specific cases we normally use (1:1
   32.38 + * and 2:1 ratios) the box is equivalent to a "triangle filter" which is not
   32.39 + * nearly so bad.  If you intend to use other sampling ratios, you'd be well
   32.40 + * advised to improve this code.
   32.41 + *
   32.42 + * A simple input-smoothing capability is provided.  This is mainly intended
   32.43 + * for cleaning up color-dithered GIF input files (if you find it inadequate,
   32.44 + * we suggest using an external filtering program such as pnmconvol).  When
   32.45 + * enabled, each input pixel P is replaced by a weighted sum of itself and its
   32.46 + * eight neighbors.  P's weight is 1-8*SF and each neighbor's weight is SF,
   32.47 + * where SF = (smoothing_factor / 1024).
   32.48 + * Currently, smoothing is only supported for 2h2v sampling factors.
   32.49 + */
   32.50 +
   32.51 +#define JPEG_INTERNALS
   32.52 +#include "jinclude.h"
   32.53 +#include "jpeglib.h"
   32.54 +
   32.55 +
   32.56 +/* Pointer to routine to downsample a single component */
   32.57 +typedef JMETHOD(void, downsample1_ptr,
   32.58 +		(j_compress_ptr cinfo, jpeg_component_info * compptr,
   32.59 +		 JSAMPARRAY input_data, JSAMPARRAY output_data));
   32.60 +
   32.61 +/* Private subobject */
   32.62 +
   32.63 +typedef struct {
   32.64 +  struct jpeg_downsampler pub;	/* public fields */
   32.65 +
   32.66 +  /* Downsampling method pointers, one per component */
   32.67 +  downsample1_ptr methods[MAX_COMPONENTS];
   32.68 +} my_downsampler;
   32.69 +
   32.70 +typedef my_downsampler * my_downsample_ptr;
   32.71 +
   32.72 +
   32.73 +/*
   32.74 + * Initialize for a downsampling pass.
   32.75 + */
   32.76 +
   32.77 +METHODDEF(void)
   32.78 +start_pass_downsample (j_compress_ptr cinfo)
   32.79 +{
   32.80 +  /* no work for now */
   32.81 +}
   32.82 +
   32.83 +
   32.84 +/*
   32.85 + * Expand a component horizontally from width input_cols to width output_cols,
   32.86 + * by duplicating the rightmost samples.
   32.87 + */
   32.88 +
   32.89 +LOCAL(void)
   32.90 +expand_right_edge (JSAMPARRAY image_data, int num_rows,
   32.91 +		   JDIMENSION input_cols, JDIMENSION output_cols)
   32.92 +{
   32.93 +  register JSAMPROW ptr;
   32.94 +  register JSAMPLE pixval;
   32.95 +  register int count;
   32.96 +  int row;
   32.97 +  int numcols = (int) (output_cols - input_cols);
   32.98 +
   32.99 +  if (numcols > 0) {
  32.100 +    for (row = 0; row < num_rows; row++) {
  32.101 +      ptr = image_data[row] + input_cols;
  32.102 +      pixval = ptr[-1];		/* don't need GETJSAMPLE() here */
  32.103 +      for (count = numcols; count > 0; count--)
  32.104 +	*ptr++ = pixval;
  32.105 +    }
  32.106 +  }
  32.107 +}
  32.108 +
  32.109 +
  32.110 +/*
  32.111 + * Do downsampling for a whole row group (all components).
  32.112 + *
  32.113 + * In this version we simply downsample each component independently.
  32.114 + */
  32.115 +
  32.116 +METHODDEF(void)
  32.117 +sep_downsample (j_compress_ptr cinfo,
  32.118 +		JSAMPIMAGE input_buf, JDIMENSION in_row_index,
  32.119 +		JSAMPIMAGE output_buf, JDIMENSION out_row_group_index)
  32.120 +{
  32.121 +  my_downsample_ptr downsample = (my_downsample_ptr) cinfo->downsample;
  32.122 +  int ci;
  32.123 +  jpeg_component_info * compptr;
  32.124 +  JSAMPARRAY in_ptr, out_ptr;
  32.125 +
  32.126 +  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  32.127 +       ci++, compptr++) {
  32.128 +    in_ptr = input_buf[ci] + in_row_index;
  32.129 +    out_ptr = output_buf[ci] + (out_row_group_index * compptr->v_samp_factor);
  32.130 +    (*downsample->methods[ci]) (cinfo, compptr, in_ptr, out_ptr);
  32.131 +  }
  32.132 +}
  32.133 +
  32.134 +
  32.135 +/*
  32.136 + * Downsample pixel values of a single component.
  32.137 + * One row group is processed per call.
  32.138 + * This version handles arbitrary integral sampling ratios, without smoothing.
  32.139 + * Note that this version is not actually used for customary sampling ratios.
  32.140 + */
  32.141 +
  32.142 +METHODDEF(void)
  32.143 +int_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr,
  32.144 +		JSAMPARRAY input_data, JSAMPARRAY output_data)
  32.145 +{
  32.146 +  int inrow, outrow, h_expand, v_expand, numpix, numpix2, h, v;
  32.147 +  JDIMENSION outcol, outcol_h;	/* outcol_h == outcol*h_expand */
  32.148 +  JDIMENSION output_cols = compptr->width_in_blocks * DCTSIZE;
  32.149 +  JSAMPROW inptr, outptr;
  32.150 +  INT32 outvalue;
  32.151 +
  32.152 +  h_expand = cinfo->max_h_samp_factor / compptr->h_samp_factor;
  32.153 +  v_expand = cinfo->max_v_samp_factor / compptr->v_samp_factor;
  32.154 +  numpix = h_expand * v_expand;
  32.155 +  numpix2 = numpix/2;
  32.156 +
  32.157 +  /* Expand input data enough to let all the output samples be generated
  32.158 +   * by the standard loop.  Special-casing padded output would be more
  32.159 +   * efficient.
  32.160 +   */
  32.161 +  expand_right_edge(input_data, cinfo->max_v_samp_factor,
  32.162 +		    cinfo->image_width, output_cols * h_expand);
  32.163 +
  32.164 +  inrow = 0;
  32.165 +  for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) {
  32.166 +    outptr = output_data[outrow];
  32.167 +    for (outcol = 0, outcol_h = 0; outcol < output_cols;
  32.168 +	 outcol++, outcol_h += h_expand) {
  32.169 +      outvalue = 0;
  32.170 +      for (v = 0; v < v_expand; v++) {
  32.171 +	inptr = input_data[inrow+v] + outcol_h;
  32.172 +	for (h = 0; h < h_expand; h++) {
  32.173 +	  outvalue += (INT32) GETJSAMPLE(*inptr++);
  32.174 +	}
  32.175 +      }
  32.176 +      *outptr++ = (JSAMPLE) ((outvalue + numpix2) / numpix);
  32.177 +    }
  32.178 +    inrow += v_expand;
  32.179 +  }
  32.180 +}
  32.181 +
  32.182 +
  32.183 +/*
  32.184 + * Downsample pixel values of a single component.
  32.185 + * This version handles the special case of a full-size component,
  32.186 + * without smoothing.
  32.187 + */
  32.188 +
  32.189 +METHODDEF(void)
  32.190 +fullsize_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr,
  32.191 +		     JSAMPARRAY input_data, JSAMPARRAY output_data)
  32.192 +{
  32.193 +  /* Copy the data */
  32.194 +  jcopy_sample_rows(input_data, 0, output_data, 0,
  32.195 +		    cinfo->max_v_samp_factor, cinfo->image_width);
  32.196 +  /* Edge-expand */
  32.197 +  expand_right_edge(output_data, cinfo->max_v_samp_factor,
  32.198 +		    cinfo->image_width, compptr->width_in_blocks * DCTSIZE);
  32.199 +}
  32.200 +
  32.201 +
  32.202 +/*
  32.203 + * Downsample pixel values of a single component.
  32.204 + * This version handles the common case of 2:1 horizontal and 1:1 vertical,
  32.205 + * without smoothing.
  32.206 + *
  32.207 + * A note about the "bias" calculations: when rounding fractional values to
  32.208 + * integer, we do not want to always round 0.5 up to the next integer.
  32.209 + * If we did that, we'd introduce a noticeable bias towards larger values.
  32.210 + * Instead, this code is arranged so that 0.5 will be rounded up or down at
  32.211 + * alternate pixel locations (a simple ordered dither pattern).
  32.212 + */
  32.213 +
  32.214 +METHODDEF(void)
  32.215 +h2v1_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr,
  32.216 +		 JSAMPARRAY input_data, JSAMPARRAY output_data)
  32.217 +{
  32.218 +  int outrow;
  32.219 +  JDIMENSION outcol;
  32.220 +  JDIMENSION output_cols = compptr->width_in_blocks * DCTSIZE;
  32.221 +  register JSAMPROW inptr, outptr;
  32.222 +  register int bias;
  32.223 +
  32.224 +  /* Expand input data enough to let all the output samples be generated
  32.225 +   * by the standard loop.  Special-casing padded output would be more
  32.226 +   * efficient.
  32.227 +   */
  32.228 +  expand_right_edge(input_data, cinfo->max_v_samp_factor,
  32.229 +		    cinfo->image_width, output_cols * 2);
  32.230 +
  32.231 +  for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) {
  32.232 +    outptr = output_data[outrow];
  32.233 +    inptr = input_data[outrow];
  32.234 +    bias = 0;			/* bias = 0,1,0,1,... for successive samples */
  32.235 +    for (outcol = 0; outcol < output_cols; outcol++) {
  32.236 +      *outptr++ = (JSAMPLE) ((GETJSAMPLE(*inptr) + GETJSAMPLE(inptr[1])
  32.237 +			      + bias) >> 1);
  32.238 +      bias ^= 1;		/* 0=>1, 1=>0 */
  32.239 +      inptr += 2;
  32.240 +    }
  32.241 +  }
  32.242 +}
  32.243 +
  32.244 +
  32.245 +/*
  32.246 + * Downsample pixel values of a single component.
  32.247 + * This version handles the standard case of 2:1 horizontal and 2:1 vertical,
  32.248 + * without smoothing.
  32.249 + */
  32.250 +
  32.251 +METHODDEF(void)
  32.252 +h2v2_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr,
  32.253 +		 JSAMPARRAY input_data, JSAMPARRAY output_data)
  32.254 +{
  32.255 +  int inrow, outrow;
  32.256 +  JDIMENSION outcol;
  32.257 +  JDIMENSION output_cols = compptr->width_in_blocks * DCTSIZE;
  32.258 +  register JSAMPROW inptr0, inptr1, outptr;
  32.259 +  register int bias;
  32.260 +
  32.261 +  /* Expand input data enough to let all the output samples be generated
  32.262 +   * by the standard loop.  Special-casing padded output would be more
  32.263 +   * efficient.
  32.264 +   */
  32.265 +  expand_right_edge(input_data, cinfo->max_v_samp_factor,
  32.266 +		    cinfo->image_width, output_cols * 2);
  32.267 +
  32.268 +  inrow = 0;
  32.269 +  for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) {
  32.270 +    outptr = output_data[outrow];
  32.271 +    inptr0 = input_data[inrow];
  32.272 +    inptr1 = input_data[inrow+1];
  32.273 +    bias = 1;			/* bias = 1,2,1,2,... for successive samples */
  32.274 +    for (outcol = 0; outcol < output_cols; outcol++) {
  32.275 +      *outptr++ = (JSAMPLE) ((GETJSAMPLE(*inptr0) + GETJSAMPLE(inptr0[1]) +
  32.276 +			      GETJSAMPLE(*inptr1) + GETJSAMPLE(inptr1[1])
  32.277 +			      + bias) >> 2);
  32.278 +      bias ^= 3;		/* 1=>2, 2=>1 */
  32.279 +      inptr0 += 2; inptr1 += 2;
  32.280 +    }
  32.281 +    inrow += 2;
  32.282 +  }
  32.283 +}
  32.284 +
  32.285 +
  32.286 +#ifdef INPUT_SMOOTHING_SUPPORTED
  32.287 +
  32.288 +/*
  32.289 + * Downsample pixel values of a single component.
  32.290 + * This version handles the standard case of 2:1 horizontal and 2:1 vertical,
  32.291 + * with smoothing.  One row of context is required.
  32.292 + */
  32.293 +
  32.294 +METHODDEF(void)
  32.295 +h2v2_smooth_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr,
  32.296 +			JSAMPARRAY input_data, JSAMPARRAY output_data)
  32.297 +{
  32.298 +  int inrow, outrow;
  32.299 +  JDIMENSION colctr;
  32.300 +  JDIMENSION output_cols = compptr->width_in_blocks * DCTSIZE;
  32.301 +  register JSAMPROW inptr0, inptr1, above_ptr, below_ptr, outptr;
  32.302 +  INT32 membersum, neighsum, memberscale, neighscale;
  32.303 +
  32.304 +  /* Expand input data enough to let all the output samples be generated
  32.305 +   * by the standard loop.  Special-casing padded output would be more
  32.306 +   * efficient.
  32.307 +   */
  32.308 +  expand_right_edge(input_data - 1, cinfo->max_v_samp_factor + 2,
  32.309 +		    cinfo->image_width, output_cols * 2);
  32.310 +
  32.311 +  /* We don't bother to form the individual "smoothed" input pixel values;
  32.312 +   * we can directly compute the output which is the average of the four
  32.313 +   * smoothed values.  Each of the four member pixels contributes a fraction
  32.314 +   * (1-8*SF) to its own smoothed image and a fraction SF to each of the three
  32.315 +   * other smoothed pixels, therefore a total fraction (1-5*SF)/4 to the final
  32.316 +   * output.  The four corner-adjacent neighbor pixels contribute a fraction
  32.317 +   * SF to just one smoothed pixel, or SF/4 to the final output; while the
  32.318 +   * eight edge-adjacent neighbors contribute SF to each of two smoothed
  32.319 +   * pixels, or SF/2 overall.  In order to use integer arithmetic, these
  32.320 +   * factors are scaled by 2^16 = 65536.
  32.321 +   * Also recall that SF = smoothing_factor / 1024.
  32.322 +   */
  32.323 +
  32.324 +  memberscale = 16384 - cinfo->smoothing_factor * 80; /* scaled (1-5*SF)/4 */
  32.325 +  neighscale = cinfo->smoothing_factor * 16; /* scaled SF/4 */
  32.326 +
  32.327 +  inrow = 0;
  32.328 +  for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) {
  32.329 +    outptr = output_data[outrow];
  32.330 +    inptr0 = input_data[inrow];
  32.331 +    inptr1 = input_data[inrow+1];
  32.332 +    above_ptr = input_data[inrow-1];
  32.333 +    below_ptr = input_data[inrow+2];
  32.334 +
  32.335 +    /* Special case for first column: pretend column -1 is same as column 0 */
  32.336 +    membersum = GETJSAMPLE(*inptr0) + GETJSAMPLE(inptr0[1]) +
  32.337 +		GETJSAMPLE(*inptr1) + GETJSAMPLE(inptr1[1]);
  32.338 +    neighsum = GETJSAMPLE(*above_ptr) + GETJSAMPLE(above_ptr[1]) +
  32.339 +	       GETJSAMPLE(*below_ptr) + GETJSAMPLE(below_ptr[1]) +
  32.340 +	       GETJSAMPLE(*inptr0) + GETJSAMPLE(inptr0[2]) +
  32.341 +	       GETJSAMPLE(*inptr1) + GETJSAMPLE(inptr1[2]);
  32.342 +    neighsum += neighsum;
  32.343 +    neighsum += GETJSAMPLE(*above_ptr) + GETJSAMPLE(above_ptr[2]) +
  32.344 +		GETJSAMPLE(*below_ptr) + GETJSAMPLE(below_ptr[2]);
  32.345 +    membersum = membersum * memberscale + neighsum * neighscale;
  32.346 +    *outptr++ = (JSAMPLE) ((membersum + 32768) >> 16);
  32.347 +    inptr0 += 2; inptr1 += 2; above_ptr += 2; below_ptr += 2;
  32.348 +
  32.349 +    for (colctr = output_cols - 2; colctr > 0; colctr--) {
  32.350 +      /* sum of pixels directly mapped to this output element */
  32.351 +      membersum = GETJSAMPLE(*inptr0) + GETJSAMPLE(inptr0[1]) +
  32.352 +		  GETJSAMPLE(*inptr1) + GETJSAMPLE(inptr1[1]);
  32.353 +      /* sum of edge-neighbor pixels */
  32.354 +      neighsum = GETJSAMPLE(*above_ptr) + GETJSAMPLE(above_ptr[1]) +
  32.355 +		 GETJSAMPLE(*below_ptr) + GETJSAMPLE(below_ptr[1]) +
  32.356 +		 GETJSAMPLE(inptr0[-1]) + GETJSAMPLE(inptr0[2]) +
  32.357 +		 GETJSAMPLE(inptr1[-1]) + GETJSAMPLE(inptr1[2]);
  32.358 +      /* The edge-neighbors count twice as much as corner-neighbors */
  32.359 +      neighsum += neighsum;
  32.360 +      /* Add in the corner-neighbors */
  32.361 +      neighsum += GETJSAMPLE(above_ptr[-1]) + GETJSAMPLE(above_ptr[2]) +
  32.362 +		  GETJSAMPLE(below_ptr[-1]) + GETJSAMPLE(below_ptr[2]);
  32.363 +      /* form final output scaled up by 2^16 */
  32.364 +      membersum = membersum * memberscale + neighsum * neighscale;
  32.365 +      /* round, descale and output it */
  32.366 +      *outptr++ = (JSAMPLE) ((membersum + 32768) >> 16);
  32.367 +      inptr0 += 2; inptr1 += 2; above_ptr += 2; below_ptr += 2;
  32.368 +    }
  32.369 +
  32.370 +    /* Special case for last column */
  32.371 +    membersum = GETJSAMPLE(*inptr0) + GETJSAMPLE(inptr0[1]) +
  32.372 +		GETJSAMPLE(*inptr1) + GETJSAMPLE(inptr1[1]);
  32.373 +    neighsum = GETJSAMPLE(*above_ptr) + GETJSAMPLE(above_ptr[1]) +
  32.374 +	       GETJSAMPLE(*below_ptr) + GETJSAMPLE(below_ptr[1]) +
  32.375 +	       GETJSAMPLE(inptr0[-1]) + GETJSAMPLE(inptr0[1]) +
  32.376 +	       GETJSAMPLE(inptr1[-1]) + GETJSAMPLE(inptr1[1]);
  32.377 +    neighsum += neighsum;
  32.378 +    neighsum += GETJSAMPLE(above_ptr[-1]) + GETJSAMPLE(above_ptr[1]) +
  32.379 +		GETJSAMPLE(below_ptr[-1]) + GETJSAMPLE(below_ptr[1]);
  32.380 +    membersum = membersum * memberscale + neighsum * neighscale;
  32.381 +    *outptr = (JSAMPLE) ((membersum + 32768) >> 16);
  32.382 +
  32.383 +    inrow += 2;
  32.384 +  }
  32.385 +}
  32.386 +
  32.387 +
  32.388 +/*
  32.389 + * Downsample pixel values of a single component.
  32.390 + * This version handles the special case of a full-size component,
  32.391 + * with smoothing.  One row of context is required.
  32.392 + */
  32.393 +
  32.394 +METHODDEF(void)
  32.395 +fullsize_smooth_downsample (j_compress_ptr cinfo, jpeg_component_info *compptr,
  32.396 +			    JSAMPARRAY input_data, JSAMPARRAY output_data)
  32.397 +{
  32.398 +  int outrow;
  32.399 +  JDIMENSION colctr;
  32.400 +  JDIMENSION output_cols = compptr->width_in_blocks * DCTSIZE;
  32.401 +  register JSAMPROW inptr, above_ptr, below_ptr, outptr;
  32.402 +  INT32 membersum, neighsum, memberscale, neighscale;
  32.403 +  int colsum, lastcolsum, nextcolsum;
  32.404 +
  32.405 +  /* Expand input data enough to let all the output samples be generated
  32.406 +   * by the standard loop.  Special-casing padded output would be more
  32.407 +   * efficient.
  32.408 +   */
  32.409 +  expand_right_edge(input_data - 1, cinfo->max_v_samp_factor + 2,
  32.410 +		    cinfo->image_width, output_cols);
  32.411 +
  32.412 +  /* Each of the eight neighbor pixels contributes a fraction SF to the
  32.413 +   * smoothed pixel, while the main pixel contributes (1-8*SF).  In order
  32.414 +   * to use integer arithmetic, these factors are multiplied by 2^16 = 65536.
  32.415 +   * Also recall that SF = smoothing_factor / 1024.
  32.416 +   */
  32.417 +
  32.418 +  memberscale = 65536L - cinfo->smoothing_factor * 512L; /* scaled 1-8*SF */
  32.419 +  neighscale = cinfo->smoothing_factor * 64; /* scaled SF */
  32.420 +
  32.421 +  for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) {
  32.422 +    outptr = output_data[outrow];
  32.423 +    inptr = input_data[outrow];
  32.424 +    above_ptr = input_data[outrow-1];
  32.425 +    below_ptr = input_data[outrow+1];
  32.426 +
  32.427 +    /* Special case for first column */
  32.428 +    colsum = GETJSAMPLE(*above_ptr++) + GETJSAMPLE(*below_ptr++) +
  32.429 +	     GETJSAMPLE(*inptr);
  32.430 +    membersum = GETJSAMPLE(*inptr++);
  32.431 +    nextcolsum = GETJSAMPLE(*above_ptr) + GETJSAMPLE(*below_ptr) +
  32.432 +		 GETJSAMPLE(*inptr);
  32.433 +    neighsum = colsum + (colsum - membersum) + nextcolsum;
  32.434 +    membersum = membersum * memberscale + neighsum * neighscale;
  32.435 +    *outptr++ = (JSAMPLE) ((membersum + 32768) >> 16);
  32.436 +    lastcolsum = colsum; colsum = nextcolsum;
  32.437 +
  32.438 +    for (colctr = output_cols - 2; colctr > 0; colctr--) {
  32.439 +      membersum = GETJSAMPLE(*inptr++);
  32.440 +      above_ptr++; below_ptr++;
  32.441 +      nextcolsum = GETJSAMPLE(*above_ptr) + GETJSAMPLE(*below_ptr) +
  32.442 +		   GETJSAMPLE(*inptr);
  32.443 +      neighsum = lastcolsum + (colsum - membersum) + nextcolsum;
  32.444 +      membersum = membersum * memberscale + neighsum * neighscale;
  32.445 +      *outptr++ = (JSAMPLE) ((membersum + 32768) >> 16);
  32.446 +      lastcolsum = colsum; colsum = nextcolsum;
  32.447 +    }
  32.448 +
  32.449 +    /* Special case for last column */
  32.450 +    membersum = GETJSAMPLE(*inptr);
  32.451 +    neighsum = lastcolsum + (colsum - membersum) + colsum;
  32.452 +    membersum = membersum * memberscale + neighsum * neighscale;
  32.453 +    *outptr = (JSAMPLE) ((membersum + 32768) >> 16);
  32.454 +
  32.455 +  }
  32.456 +}
  32.457 +
  32.458 +#endif /* INPUT_SMOOTHING_SUPPORTED */
  32.459 +
  32.460 +
  32.461 +/*
  32.462 + * Module initialization routine for downsampling.
  32.463 + * Note that we must select a routine for each component.
  32.464 + */
  32.465 +
  32.466 +GLOBAL(void)
  32.467 +jinit_downsampler (j_compress_ptr cinfo)
  32.468 +{
  32.469 +  my_downsample_ptr downsample;
  32.470 +  int ci;
  32.471 +  jpeg_component_info * compptr;
  32.472 +  boolean smoothok = TRUE;
  32.473 +
  32.474 +  downsample = (my_downsample_ptr)
  32.475 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  32.476 +				SIZEOF(my_downsampler));
  32.477 +  cinfo->downsample = (struct jpeg_downsampler *) downsample;
  32.478 +  downsample->pub.start_pass = start_pass_downsample;
  32.479 +  downsample->pub.downsample = sep_downsample;
  32.480 +  downsample->pub.need_context_rows = FALSE;
  32.481 +
  32.482 +  if (cinfo->CCIR601_sampling)
  32.483 +    ERREXIT(cinfo, JERR_CCIR601_NOTIMPL);
  32.484 +
  32.485 +  /* Verify we can handle the sampling factors, and set up method pointers */
  32.486 +  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  32.487 +       ci++, compptr++) {
  32.488 +    if (compptr->h_samp_factor == cinfo->max_h_samp_factor &&
  32.489 +	compptr->v_samp_factor == cinfo->max_v_samp_factor) {
  32.490 +#ifdef INPUT_SMOOTHING_SUPPORTED
  32.491 +      if (cinfo->smoothing_factor) {
  32.492 +	downsample->methods[ci] = fullsize_smooth_downsample;
  32.493 +	downsample->pub.need_context_rows = TRUE;
  32.494 +      } else
  32.495 +#endif
  32.496 +	downsample->methods[ci] = fullsize_downsample;
  32.497 +    } else if (compptr->h_samp_factor * 2 == cinfo->max_h_samp_factor &&
  32.498 +	       compptr->v_samp_factor == cinfo->max_v_samp_factor) {
  32.499 +      smoothok = FALSE;
  32.500 +      downsample->methods[ci] = h2v1_downsample;
  32.501 +    } else if (compptr->h_samp_factor * 2 == cinfo->max_h_samp_factor &&
  32.502 +	       compptr->v_samp_factor * 2 == cinfo->max_v_samp_factor) {
  32.503 +#ifdef INPUT_SMOOTHING_SUPPORTED
  32.504 +      if (cinfo->smoothing_factor) {
  32.505 +	downsample->methods[ci] = h2v2_smooth_downsample;
  32.506 +	downsample->pub.need_context_rows = TRUE;
  32.507 +      } else
  32.508 +#endif
  32.509 +	downsample->methods[ci] = h2v2_downsample;
  32.510 +    } else if ((cinfo->max_h_samp_factor % compptr->h_samp_factor) == 0 &&
  32.511 +	       (cinfo->max_v_samp_factor % compptr->v_samp_factor) == 0) {
  32.512 +      smoothok = FALSE;
  32.513 +      downsample->methods[ci] = int_downsample;
  32.514 +    } else
  32.515 +      ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);
  32.516 +  }
  32.517 +
  32.518 +#ifdef INPUT_SMOOTHING_SUPPORTED
  32.519 +  if (cinfo->smoothing_factor && !smoothok)
  32.520 +    TRACEMS(cinfo, 0, JTRC_SMOOTH_NOTIMPL);
  32.521 +#endif
  32.522 +}
    33.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    33.2 +++ b/libs/libjpeg/jctrans.c	Sat Sep 19 05:51:51 2015 +0300
    33.3 @@ -0,0 +1,388 @@
    33.4 +/*
    33.5 + * jctrans.c
    33.6 + *
    33.7 + * Copyright (C) 1995-1998, Thomas G. Lane.
    33.8 + * This file is part of the Independent JPEG Group's software.
    33.9 + * For conditions of distribution and use, see the accompanying README file.
   33.10 + *
   33.11 + * This file contains library routines for transcoding compression,
   33.12 + * that is, writing raw DCT coefficient arrays to an output JPEG file.
   33.13 + * The routines in jcapimin.c will also be needed by a transcoder.
   33.14 + */
   33.15 +
   33.16 +#define JPEG_INTERNALS
   33.17 +#include "jinclude.h"
   33.18 +#include "jpeglib.h"
   33.19 +
   33.20 +
   33.21 +/* Forward declarations */
   33.22 +LOCAL(void) transencode_master_selection
   33.23 +	JPP((j_compress_ptr cinfo, jvirt_barray_ptr * coef_arrays));
   33.24 +LOCAL(void) transencode_coef_controller
   33.25 +	JPP((j_compress_ptr cinfo, jvirt_barray_ptr * coef_arrays));
   33.26 +
   33.27 +
   33.28 +/*
   33.29 + * Compression initialization for writing raw-coefficient data.
   33.30 + * Before calling this, all parameters and a data destination must be set up.
   33.31 + * Call jpeg_finish_compress() to actually write the data.
   33.32 + *
   33.33 + * The number of passed virtual arrays must match cinfo->num_components.
   33.34 + * Note that the virtual arrays need not be filled or even realized at
   33.35 + * the time write_coefficients is called; indeed, if the virtual arrays
   33.36 + * were requested from this compression object's memory manager, they
   33.37 + * typically will be realized during this routine and filled afterwards.
   33.38 + */
   33.39 +
   33.40 +GLOBAL(void)
   33.41 +jpeg_write_coefficients (j_compress_ptr cinfo, jvirt_barray_ptr * coef_arrays)
   33.42 +{
   33.43 +  if (cinfo->global_state != CSTATE_START)
   33.44 +    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
   33.45 +  /* Mark all tables to be written */
   33.46 +  jpeg_suppress_tables(cinfo, FALSE);
   33.47 +  /* (Re)initialize error mgr and destination modules */
   33.48 +  (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);
   33.49 +  (*cinfo->dest->init_destination) (cinfo);
   33.50 +  /* Perform master selection of active modules */
   33.51 +  transencode_master_selection(cinfo, coef_arrays);
   33.52 +  /* Wait for jpeg_finish_compress() call */
   33.53 +  cinfo->next_scanline = 0;	/* so jpeg_write_marker works */
   33.54 +  cinfo->global_state = CSTATE_WRCOEFS;
   33.55 +}
   33.56 +
   33.57 +
   33.58 +/*
   33.59 + * Initialize the compression object with default parameters,
   33.60 + * then copy from the source object all parameters needed for lossless
   33.61 + * transcoding.  Parameters that can be varied without loss (such as
   33.62 + * scan script and Huffman optimization) are left in their default states.
   33.63 + */
   33.64 +
   33.65 +GLOBAL(void)
   33.66 +jpeg_copy_critical_parameters (j_decompress_ptr srcinfo,
   33.67 +			       j_compress_ptr dstinfo)
   33.68 +{
   33.69 +  JQUANT_TBL ** qtblptr;
   33.70 +  jpeg_component_info *incomp, *outcomp;
   33.71 +  JQUANT_TBL *c_quant, *slot_quant;
   33.72 +  int tblno, ci, coefi;
   33.73 +
   33.74 +  /* Safety check to ensure start_compress not called yet. */
   33.75 +  if (dstinfo->global_state != CSTATE_START)
   33.76 +    ERREXIT1(dstinfo, JERR_BAD_STATE, dstinfo->global_state);
   33.77 +  /* Copy fundamental image dimensions */
   33.78 +  dstinfo->image_width = srcinfo->image_width;
   33.79 +  dstinfo->image_height = srcinfo->image_height;
   33.80 +  dstinfo->input_components = srcinfo->num_components;
   33.81 +  dstinfo->in_color_space = srcinfo->jpeg_color_space;
   33.82 +  /* Initialize all parameters to default values */
   33.83 +  jpeg_set_defaults(dstinfo);
   33.84 +  /* jpeg_set_defaults may choose wrong colorspace, eg YCbCr if input is RGB.
   33.85 +   * Fix it to get the right header markers for the image colorspace.
   33.86 +   */
   33.87 +  jpeg_set_colorspace(dstinfo, srcinfo->jpeg_color_space);
   33.88 +  dstinfo->data_precision = srcinfo->data_precision;
   33.89 +  dstinfo->CCIR601_sampling = srcinfo->CCIR601_sampling;
   33.90 +  /* Copy the source's quantization tables. */
   33.91 +  for (tblno = 0; tblno < NUM_QUANT_TBLS; tblno++) {
   33.92 +    if (srcinfo->quant_tbl_ptrs[tblno] != NULL) {
   33.93 +      qtblptr = & dstinfo->quant_tbl_ptrs[tblno];
   33.94 +      if (*qtblptr == NULL)
   33.95 +	*qtblptr = jpeg_alloc_quant_table((j_common_ptr) dstinfo);
   33.96 +      MEMCOPY((*qtblptr)->quantval,
   33.97 +	      srcinfo->quant_tbl_ptrs[tblno]->quantval,
   33.98 +	      SIZEOF((*qtblptr)->quantval));
   33.99 +      (*qtblptr)->sent_table = FALSE;
  33.100 +    }
  33.101 +  }
  33.102 +  /* Copy the source's per-component info.
  33.103 +   * Note we assume jpeg_set_defaults has allocated the dest comp_info array.
  33.104 +   */
  33.105 +  dstinfo->num_components = srcinfo->num_components;
  33.106 +  if (dstinfo->num_components < 1 || dstinfo->num_components > MAX_COMPONENTS)
  33.107 +    ERREXIT2(dstinfo, JERR_COMPONENT_COUNT, dstinfo->num_components,
  33.108 +	     MAX_COMPONENTS);
  33.109 +  for (ci = 0, incomp = srcinfo->comp_info, outcomp = dstinfo->comp_info;
  33.110 +       ci < dstinfo->num_components; ci++, incomp++, outcomp++) {
  33.111 +    outcomp->component_id = incomp->component_id;
  33.112 +    outcomp->h_samp_factor = incomp->h_samp_factor;
  33.113 +    outcomp->v_samp_factor = incomp->v_samp_factor;
  33.114 +    outcomp->quant_tbl_no = incomp->quant_tbl_no;
  33.115 +    /* Make sure saved quantization table for component matches the qtable
  33.116 +     * slot.  If not, the input file re-used this qtable slot.
  33.117 +     * IJG encoder currently cannot duplicate this.
  33.118 +     */
  33.119 +    tblno = outcomp->quant_tbl_no;
  33.120 +    if (tblno < 0 || tblno >= NUM_QUANT_TBLS ||
  33.121 +	srcinfo->quant_tbl_ptrs[tblno] == NULL)
  33.122 +      ERREXIT1(dstinfo, JERR_NO_QUANT_TABLE, tblno);
  33.123 +    slot_quant = srcinfo->quant_tbl_ptrs[tblno];
  33.124 +    c_quant = incomp->quant_table;
  33.125 +    if (c_quant != NULL) {
  33.126 +      for (coefi = 0; coefi < DCTSIZE2; coefi++) {
  33.127 +	if (c_quant->quantval[coefi] != slot_quant->quantval[coefi])
  33.128 +	  ERREXIT1(dstinfo, JERR_MISMATCHED_QUANT_TABLE, tblno);
  33.129 +      }
  33.130 +    }
  33.131 +    /* Note: we do not copy the source's Huffman table assignments;
  33.132 +     * instead we rely on jpeg_set_colorspace to have made a suitable choice.
  33.133 +     */
  33.134 +  }
  33.135 +  /* Also copy JFIF version and resolution information, if available.
  33.136 +   * Strictly speaking this isn't "critical" info, but it's nearly
  33.137 +   * always appropriate to copy it if available.  In particular,
  33.138 +   * if the application chooses to copy JFIF 1.02 extension markers from
  33.139 +   * the source file, we need to copy the version to make sure we don't
  33.140 +   * emit a file that has 1.02 extensions but a claimed version of 1.01.
  33.141 +   * We will *not*, however, copy version info from mislabeled "2.01" files.
  33.142 +   */
  33.143 +  if (srcinfo->saw_JFIF_marker) {
  33.144 +    if (srcinfo->JFIF_major_version == 1) {
  33.145 +      dstinfo->JFIF_major_version = srcinfo->JFIF_major_version;
  33.146 +      dstinfo->JFIF_minor_version = srcinfo->JFIF_minor_version;
  33.147 +    }
  33.148 +    dstinfo->density_unit = srcinfo->density_unit;
  33.149 +    dstinfo->X_density = srcinfo->X_density;
  33.150 +    dstinfo->Y_density = srcinfo->Y_density;
  33.151 +  }
  33.152 +}
  33.153 +
  33.154 +
  33.155 +/*
  33.156 + * Master selection of compression modules for transcoding.
  33.157 + * This substitutes for jcinit.c's initialization of the full compressor.
  33.158 + */
  33.159 +
  33.160 +LOCAL(void)
  33.161 +transencode_master_selection (j_compress_ptr cinfo,
  33.162 +			      jvirt_barray_ptr * coef_arrays)
  33.163 +{
  33.164 +  /* Although we don't actually use input_components for transcoding,
  33.165 +   * jcmaster.c's initial_setup will complain if input_components is 0.
  33.166 +   */
  33.167 +  cinfo->input_components = 1;
  33.168 +  /* Initialize master control (includes parameter checking/processing) */
  33.169 +  jinit_c_master_control(cinfo, TRUE /* transcode only */);
  33.170 +
  33.171 +  /* Entropy encoding: either Huffman or arithmetic coding. */
  33.172 +  if (cinfo->arith_code) {
  33.173 +    ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
  33.174 +  } else {
  33.175 +    if (cinfo->progressive_mode) {
  33.176 +#ifdef C_PROGRESSIVE_SUPPORTED
  33.177 +      jinit_phuff_encoder(cinfo);
  33.178 +#else
  33.179 +      ERREXIT(cinfo, JERR_NOT_COMPILED);
  33.180 +#endif
  33.181 +    } else
  33.182 +      jinit_huff_encoder(cinfo);
  33.183 +  }
  33.184 +
  33.185 +  /* We need a special coefficient buffer controller. */
  33.186 +  transencode_coef_controller(cinfo, coef_arrays);
  33.187 +
  33.188 +  jinit_marker_writer(cinfo);
  33.189 +
  33.190 +  /* We can now tell the memory manager to allocate virtual arrays. */
  33.191 +  (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);
  33.192 +
  33.193 +  /* Write the datastream header (SOI, JFIF) immediately.
  33.194 +   * Frame and scan headers are postponed till later.
  33.195 +   * This lets application insert special markers after the SOI.
  33.196 +   */
  33.197 +  (*cinfo->marker->write_file_header) (cinfo);
  33.198 +}
  33.199 +
  33.200 +
  33.201 +/*
  33.202 + * The rest of this file is a special implementation of the coefficient
  33.203 + * buffer controller.  This is similar to jccoefct.c, but it handles only
  33.204 + * output from presupplied virtual arrays.  Furthermore, we generate any
  33.205 + * dummy padding blocks on-the-fly rather than expecting them to be present
  33.206 + * in the arrays.
  33.207 + */
  33.208 +
  33.209 +/* Private buffer controller object */
  33.210 +
  33.211 +typedef struct {
  33.212 +  struct jpeg_c_coef_controller pub; /* public fields */
  33.213 +
  33.214 +  JDIMENSION iMCU_row_num;	/* iMCU row # within image */
  33.215 +  JDIMENSION mcu_ctr;		/* counts MCUs processed in current row */
  33.216 +  int MCU_vert_offset;		/* counts MCU rows within iMCU row */
  33.217 +  int MCU_rows_per_iMCU_row;	/* number of such rows needed */
  33.218 +
  33.219 +  /* Virtual block array for each component. */
  33.220 +  jvirt_barray_ptr * whole_image;
  33.221 +
  33.222 +  /* Workspace for constructing dummy blocks at right/bottom edges. */
  33.223 +  JBLOCKROW dummy_buffer[C_MAX_BLOCKS_IN_MCU];
  33.224 +} my_coef_controller;
  33.225 +
  33.226 +typedef my_coef_controller * my_coef_ptr;
  33.227 +
  33.228 +
  33.229 +LOCAL(void)
  33.230 +start_iMCU_row (j_compress_ptr cinfo)
  33.231 +/* Reset within-iMCU-row counters for a new row */
  33.232 +{
  33.233 +  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
  33.234 +
  33.235 +  /* In an interleaved scan, an MCU row is the same as an iMCU row.
  33.236 +   * In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows.
  33.237 +   * But at the bottom of the image, process only what's left.
  33.238 +   */
  33.239 +  if (cinfo->comps_in_scan > 1) {
  33.240 +    coef->MCU_rows_per_iMCU_row = 1;
  33.241 +  } else {
  33.242 +    if (coef->iMCU_row_num < (cinfo->total_iMCU_rows-1))
  33.243 +      coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor;
  33.244 +    else
  33.245 +      coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height;
  33.246 +  }
  33.247 +
  33.248 +  coef->mcu_ctr = 0;
  33.249 +  coef->MCU_vert_offset = 0;
  33.250 +}
  33.251 +
  33.252 +
  33.253 +/*
  33.254 + * Initialize for a processing pass.
  33.255 + */
  33.256 +
  33.257 +METHODDEF(void)
  33.258 +start_pass_coef (j_compress_ptr cinfo, J_BUF_MODE pass_mode)
  33.259 +{
  33.260 +  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
  33.261 +
  33.262 +  if (pass_mode != JBUF_CRANK_DEST)
  33.263 +    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  33.264 +
  33.265 +  coef->iMCU_row_num = 0;
  33.266 +  start_iMCU_row(cinfo);
  33.267 +}
  33.268 +
  33.269 +
  33.270 +/*
  33.271 + * Process some data.
  33.272 + * We process the equivalent of one fully interleaved MCU row ("iMCU" row)
  33.273 + * per call, ie, v_samp_factor block rows for each component in the scan.
  33.274 + * The data is obtained from the virtual arrays and fed to the entropy coder.
  33.275 + * Returns TRUE if the iMCU row is completed, FALSE if suspended.
  33.276 + *
  33.277 + * NB: input_buf is ignored; it is likely to be a NULL pointer.
  33.278 + */
  33.279 +
  33.280 +METHODDEF(boolean)
  33.281 +compress_output (j_compress_ptr cinfo, JSAMPIMAGE input_buf)
  33.282 +{
  33.283 +  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
  33.284 +  JDIMENSION MCU_col_num;	/* index of current MCU within row */
  33.285 +  JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1;
  33.286 +  JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
  33.287 +  int blkn, ci, xindex, yindex, yoffset, blockcnt;
  33.288 +  JDIMENSION start_col;
  33.289 +  JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];
  33.290 +  JBLOCKROW MCU_buffer[C_MAX_BLOCKS_IN_MCU];
  33.291 +  JBLOCKROW buffer_ptr;
  33.292 +  jpeg_component_info *compptr;
  33.293 +
  33.294 +  /* Align the virtual buffers for the components used in this scan. */
  33.295 +  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  33.296 +    compptr = cinfo->cur_comp_info[ci];
  33.297 +    buffer[ci] = (*cinfo->mem->access_virt_barray)
  33.298 +      ((j_common_ptr) cinfo, coef->whole_image[compptr->component_index],
  33.299 +       coef->iMCU_row_num * compptr->v_samp_factor,
  33.300 +       (JDIMENSION) compptr->v_samp_factor, FALSE);
  33.301 +  }
  33.302 +
  33.303 +  /* Loop to process one whole iMCU row */
  33.304 +  for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
  33.305 +       yoffset++) {
  33.306 +    for (MCU_col_num = coef->mcu_ctr; MCU_col_num < cinfo->MCUs_per_row;
  33.307 +	 MCU_col_num++) {
  33.308 +      /* Construct list of pointers to DCT blocks belonging to this MCU */
  33.309 +      blkn = 0;			/* index of current DCT block within MCU */
  33.310 +      for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  33.311 +	compptr = cinfo->cur_comp_info[ci];
  33.312 +	start_col = MCU_col_num * compptr->MCU_width;
  33.313 +	blockcnt = (MCU_col_num < last_MCU_col) ? compptr->MCU_width
  33.314 +						: compptr->last_col_width;
  33.315 +	for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
  33.316 +	  if (coef->iMCU_row_num < last_iMCU_row ||
  33.317 +	      yindex+yoffset < compptr->last_row_height) {
  33.318 +	    /* Fill in pointers to real blocks in this row */
  33.319 +	    buffer_ptr = buffer[ci][yindex+yoffset] + start_col;
  33.320 +	    for (xindex = 0; xindex < blockcnt; xindex++)
  33.321 +	      MCU_buffer[blkn++] = buffer_ptr++;
  33.322 +	  } else {
  33.323 +	    /* At bottom of image, need a whole row of dummy blocks */
  33.324 +	    xindex = 0;
  33.325 +	  }
  33.326 +	  /* Fill in any dummy blocks needed in this row.
  33.327 +	   * Dummy blocks are filled in the same way as in jccoefct.c:
  33.328 +	   * all zeroes in the AC entries, DC entries equal to previous
  33.329 +	   * block's DC value.  The init routine has already zeroed the
  33.330 +	   * AC entries, so we need only set the DC entries correctly.
  33.331 +	   */
  33.332 +	  for (; xindex < compptr->MCU_width; xindex++) {
  33.333 +	    MCU_buffer[blkn] = coef->dummy_buffer[blkn];
  33.334 +	    MCU_buffer[blkn][0][0] = MCU_buffer[blkn-1][0][0];
  33.335 +	    blkn++;
  33.336 +	  }
  33.337 +	}
  33.338 +      }
  33.339 +      /* Try to write the MCU. */
  33.340 +      if (! (*cinfo->entropy->encode_mcu) (cinfo, MCU_buffer)) {
  33.341 +	/* Suspension forced; update state counters and exit */
  33.342 +	coef->MCU_vert_offset = yoffset;
  33.343 +	coef->mcu_ctr = MCU_col_num;
  33.344 +	return FALSE;
  33.345 +      }
  33.346 +    }
  33.347 +    /* Completed an MCU row, but perhaps not an iMCU row */
  33.348 +    coef->mcu_ctr = 0;
  33.349 +  }
  33.350 +  /* Completed the iMCU row, advance counters for next one */
  33.351 +  coef->iMCU_row_num++;
  33.352 +  start_iMCU_row(cinfo);
  33.353 +  return TRUE;
  33.354 +}
  33.355 +
  33.356 +
  33.357 +/*
  33.358 + * Initialize coefficient buffer controller.
  33.359 + *
  33.360 + * Each passed coefficient array must be the right size for that
  33.361 + * coefficient: width_in_blocks wide and height_in_blocks high,
  33.362 + * with unitheight at least v_samp_factor.
  33.363 + */
  33.364 +
  33.365 +LOCAL(void)
  33.366 +transencode_coef_controller (j_compress_ptr cinfo,
  33.367 +			     jvirt_barray_ptr * coef_arrays)
  33.368 +{
  33.369 +  my_coef_ptr coef;
  33.370 +  JBLOCKROW buffer;
  33.371 +  int i;
  33.372 +
  33.373 +  coef = (my_coef_ptr)
  33.374 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  33.375 +				SIZEOF(my_coef_controller));
  33.376 +  cinfo->coef = (struct jpeg_c_coef_controller *) coef;
  33.377 +  coef->pub.start_pass = start_pass_coef;
  33.378 +  coef->pub.compress_data = compress_output;
  33.379 +
  33.380 +  /* Save pointer to virtual arrays */
  33.381 +  coef->whole_image = coef_arrays;
  33.382 +
  33.383 +  /* Allocate and pre-zero space for dummy DCT blocks. */
  33.384 +  buffer = (JBLOCKROW)
  33.385 +    (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  33.386 +				C_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK));
  33.387 +  jzero_far((void FAR *) buffer, C_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK));
  33.388 +  for (i = 0; i < C_MAX_BLOCKS_IN_MCU; i++) {
  33.389 +    coef->dummy_buffer[i] = buffer + i;
  33.390 +  }
  33.391 +}
    34.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    34.2 +++ b/libs/libjpeg/jdapimin.c	Sat Sep 19 05:51:51 2015 +0300
    34.3 @@ -0,0 +1,395 @@
    34.4 +/*
    34.5 + * jdapimin.c
    34.6 + *
    34.7 + * Copyright (C) 1994-1998, Thomas G. Lane.
    34.8 + * This file is part of the Independent JPEG Group's software.
    34.9 + * For conditions of distribution and use, see the accompanying README file.
   34.10 + *
   34.11 + * This file contains application interface code for the decompression half
   34.12 + * of the JPEG library.  These are the "minimum" API routines that may be
   34.13 + * needed in either the normal full-decompression case or the
   34.14 + * transcoding-only case.
   34.15 + *
   34.16 + * Most of the routines intended to be called directly by an application
   34.17 + * are in this file or in jdapistd.c.  But also see jcomapi.c for routines
   34.18 + * shared by compression and decompression, and jdtrans.c for the transcoding
   34.19 + * case.
   34.20 + */
   34.21 +
   34.22 +#define JPEG_INTERNALS
   34.23 +#include "jinclude.h"
   34.24 +#include "jpeglib.h"
   34.25 +
   34.26 +
   34.27 +/*
   34.28 + * Initialization of a JPEG decompression object.
   34.29 + * The error manager must already be set up (in case memory manager fails).
   34.30 + */
   34.31 +
   34.32 +GLOBAL(void)
   34.33 +jpeg_CreateDecompress (j_decompress_ptr cinfo, int version, size_t structsize)
   34.34 +{
   34.35 +  int i;
   34.36 +
   34.37 +  /* Guard against version mismatches between library and caller. */
   34.38 +  cinfo->mem = NULL;		/* so jpeg_destroy knows mem mgr not called */
   34.39 +  if (version != JPEG_LIB_VERSION)
   34.40 +    ERREXIT2(cinfo, JERR_BAD_LIB_VERSION, JPEG_LIB_VERSION, version);
   34.41 +  if (structsize != SIZEOF(struct jpeg_decompress_struct))
   34.42 +    ERREXIT2(cinfo, JERR_BAD_STRUCT_SIZE, 
   34.43 +	     (int) SIZEOF(struct jpeg_decompress_struct), (int) structsize);
   34.44 +
   34.45 +  /* For debugging purposes, we zero the whole master structure.
   34.46 +   * But the application has already set the err pointer, and may have set
   34.47 +   * client_data, so we have to save and restore those fields.
   34.48 +   * Note: if application hasn't set client_data, tools like Purify may
   34.49 +   * complain here.
   34.50 +   */
   34.51 +  {
   34.52 +    struct jpeg_error_mgr * err = cinfo->err;
   34.53 +    void * client_data = cinfo->client_data; /* ignore Purify complaint here */
   34.54 +    MEMZERO(cinfo, SIZEOF(struct jpeg_decompress_struct));
   34.55 +    cinfo->err = err;
   34.56 +    cinfo->client_data = client_data;
   34.57 +  }
   34.58 +  cinfo->is_decompressor = TRUE;
   34.59 +
   34.60 +  /* Initialize a memory manager instance for this object */
   34.61 +  jinit_memory_mgr((j_common_ptr) cinfo);
   34.62 +
   34.63 +  /* Zero out pointers to permanent structures. */
   34.64 +  cinfo->progress = NULL;
   34.65 +  cinfo->src = NULL;
   34.66 +
   34.67 +  for (i = 0; i < NUM_QUANT_TBLS; i++)
   34.68 +    cinfo->quant_tbl_ptrs[i] = NULL;
   34.69 +
   34.70 +  for (i = 0; i < NUM_HUFF_TBLS; i++) {
   34.71 +    cinfo->dc_huff_tbl_ptrs[i] = NULL;
   34.72 +    cinfo->ac_huff_tbl_ptrs[i] = NULL;
   34.73 +  }
   34.74 +
   34.75 +  /* Initialize marker processor so application can override methods
   34.76 +   * for COM, APPn markers before calling jpeg_read_header.
   34.77 +   */
   34.78 +  cinfo->marker_list = NULL;
   34.79 +  jinit_marker_reader(cinfo);
   34.80 +
   34.81 +  /* And initialize the overall input controller. */
   34.82 +  jinit_input_controller(cinfo);
   34.83 +
   34.84 +  /* OK, I'm ready */
   34.85 +  cinfo->global_state = DSTATE_START;
   34.86 +}
   34.87 +
   34.88 +
   34.89 +/*
   34.90 + * Destruction of a JPEG decompression object
   34.91 + */
   34.92 +
   34.93 +GLOBAL(void)
   34.94 +jpeg_destroy_decompress (j_decompress_ptr cinfo)
   34.95 +{
   34.96 +  jpeg_destroy((j_common_ptr) cinfo); /* use common routine */
   34.97 +}
   34.98 +
   34.99 +
  34.100 +/*
  34.101 + * Abort processing of a JPEG decompression operation,
  34.102 + * but don't destroy the object itself.
  34.103 + */
  34.104 +
  34.105 +GLOBAL(void)
  34.106 +jpeg_abort_decompress (j_decompress_ptr cinfo)
  34.107 +{
  34.108 +  jpeg_abort((j_common_ptr) cinfo); /* use common routine */
  34.109 +}
  34.110 +
  34.111 +
  34.112 +/*
  34.113 + * Set default decompression parameters.
  34.114 + */
  34.115 +
  34.116 +LOCAL(void)
  34.117 +default_decompress_parms (j_decompress_ptr cinfo)
  34.118 +{
  34.119 +  /* Guess the input colorspace, and set output colorspace accordingly. */
  34.120 +  /* (Wish JPEG committee had provided a real way to specify this...) */
  34.121 +  /* Note application may override our guesses. */
  34.122 +  switch (cinfo->num_components) {
  34.123 +  case 1:
  34.124 +    cinfo->jpeg_color_space = JCS_GRAYSCALE;
  34.125 +    cinfo->out_color_space = JCS_GRAYSCALE;
  34.126 +    break;
  34.127 +    
  34.128 +  case 3:
  34.129 +    if (cinfo->saw_JFIF_marker) {
  34.130 +      cinfo->jpeg_color_space = JCS_YCbCr; /* JFIF implies YCbCr */
  34.131 +    } else if (cinfo->saw_Adobe_marker) {
  34.132 +      switch (cinfo->Adobe_transform) {
  34.133 +      case 0:
  34.134 +	cinfo->jpeg_color_space = JCS_RGB;
  34.135 +	break;
  34.136 +      case 1:
  34.137 +	cinfo->jpeg_color_space = JCS_YCbCr;
  34.138 +	break;
  34.139 +      default:
  34.140 +	WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform);
  34.141 +	cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */
  34.142 +	break;
  34.143 +      }
  34.144 +    } else {
  34.145 +      /* Saw no special markers, try to guess from the component IDs */
  34.146 +      int cid0 = cinfo->comp_info[0].component_id;
  34.147 +      int cid1 = cinfo->comp_info[1].component_id;
  34.148 +      int cid2 = cinfo->comp_info[2].component_id;
  34.149 +
  34.150 +      if (cid0 == 1 && cid1 == 2 && cid2 == 3)
  34.151 +	cinfo->jpeg_color_space = JCS_YCbCr; /* assume JFIF w/out marker */
  34.152 +      else if (cid0 == 82 && cid1 == 71 && cid2 == 66)
  34.153 +	cinfo->jpeg_color_space = JCS_RGB; /* ASCII 'R', 'G', 'B' */
  34.154 +      else {
  34.155 +	TRACEMS3(cinfo, 1, JTRC_UNKNOWN_IDS, cid0, cid1, cid2);
  34.156 +	cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */
  34.157 +      }
  34.158 +    }
  34.159 +    /* Always guess RGB is proper output colorspace. */
  34.160 +    cinfo->out_color_space = JCS_RGB;
  34.161 +    break;
  34.162 +    
  34.163 +  case 4:
  34.164 +    if (cinfo->saw_Adobe_marker) {
  34.165 +      switch (cinfo->Adobe_transform) {
  34.166 +      case 0:
  34.167 +	cinfo->jpeg_color_space = JCS_CMYK;
  34.168 +	break;
  34.169 +      case 2:
  34.170 +	cinfo->jpeg_color_space = JCS_YCCK;
  34.171 +	break;
  34.172 +      default:
  34.173 +	WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform);
  34.174 +	cinfo->jpeg_color_space = JCS_YCCK; /* assume it's YCCK */
  34.175 +	break;
  34.176 +      }
  34.177 +    } else {
  34.178 +      /* No special markers, assume straight CMYK. */
  34.179 +      cinfo->jpeg_color_space = JCS_CMYK;
  34.180 +    }
  34.181 +    cinfo->out_color_space = JCS_CMYK;
  34.182 +    break;
  34.183 +    
  34.184 +  default:
  34.185 +    cinfo->jpeg_color_space = JCS_UNKNOWN;
  34.186 +    cinfo->out_color_space = JCS_UNKNOWN;
  34.187 +    break;
  34.188 +  }
  34.189 +
  34.190 +  /* Set defaults for other decompression parameters. */
  34.191 +  cinfo->scale_num = 1;		/* 1:1 scaling */
  34.192 +  cinfo->scale_denom = 1;
  34.193 +  cinfo->output_gamma = 1.0;
  34.194 +  cinfo->buffered_image = FALSE;
  34.195 +  cinfo->raw_data_out = FALSE;
  34.196 +  cinfo->dct_method = JDCT_DEFAULT;
  34.197 +  cinfo->do_fancy_upsampling = TRUE;
  34.198 +  cinfo->do_block_smoothing = TRUE;
  34.199 +  cinfo->quantize_colors = FALSE;
  34.200 +  /* We set these in case application only sets quantize_colors. */
  34.201 +  cinfo->dither_mode = JDITHER_FS;
  34.202 +#ifdef QUANT_2PASS_SUPPORTED
  34.203 +  cinfo->two_pass_quantize = TRUE;
  34.204 +#else
  34.205 +  cinfo->two_pass_quantize = FALSE;
  34.206 +#endif
  34.207 +  cinfo->desired_number_of_colors = 256;
  34.208 +  cinfo->colormap = NULL;
  34.209 +  /* Initialize for no mode change in buffered-image mode. */
  34.210 +  cinfo->enable_1pass_quant = FALSE;
  34.211 +  cinfo->enable_external_quant = FALSE;
  34.212 +  cinfo->enable_2pass_quant = FALSE;
  34.213 +}
  34.214 +
  34.215 +
  34.216 +/*
  34.217 + * Decompression startup: read start of JPEG datastream to see what's there.
  34.218 + * Need only initialize JPEG object and supply a data source before calling.
  34.219 + *
  34.220 + * This routine will read as far as the first SOS marker (ie, actual start of
  34.221 + * compressed data), and will save all tables and parameters in the JPEG
  34.222 + * object.  It will also initialize the decompression parameters to default
  34.223 + * values, and finally return JPEG_HEADER_OK.  On return, the application may
  34.224 + * adjust the decompression parameters and then call jpeg_start_decompress.
  34.225 + * (Or, if the application only wanted to determine the image parameters,
  34.226 + * the data need not be decompressed.  In that case, call jpeg_abort or
  34.227 + * jpeg_destroy to release any temporary space.)
  34.228 + * If an abbreviated (tables only) datastream is presented, the routine will
  34.229 + * return JPEG_HEADER_TABLES_ONLY upon reaching EOI.  The application may then
  34.230 + * re-use the JPEG object to read the abbreviated image datastream(s).
  34.231 + * It is unnecessary (but OK) to call jpeg_abort in this case.
  34.232 + * The JPEG_SUSPENDED return code only occurs if the data source module
  34.233 + * requests suspension of the decompressor.  In this case the application
  34.234 + * should load more source data and then re-call jpeg_read_header to resume
  34.235 + * processing.
  34.236 + * If a non-suspending data source is used and require_image is TRUE, then the
  34.237 + * return code need not be inspected since only JPEG_HEADER_OK is possible.
  34.238 + *
  34.239 + * This routine is now just a front end to jpeg_consume_input, with some
  34.240 + * extra error checking.
  34.241 + */
  34.242 +
  34.243 +GLOBAL(int)
  34.244 +jpeg_read_header (j_decompress_ptr cinfo, boolean require_image)
  34.245 +{
  34.246 +  int retcode;
  34.247 +
  34.248 +  if (cinfo->global_state != DSTATE_START &&
  34.249 +      cinfo->global_state != DSTATE_INHEADER)
  34.250 +    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  34.251 +
  34.252 +  retcode = jpeg_consume_input(cinfo);
  34.253 +
  34.254 +  switch (retcode) {
  34.255 +  case JPEG_REACHED_SOS:
  34.256 +    retcode = JPEG_HEADER_OK;
  34.257 +    break;
  34.258 +  case JPEG_REACHED_EOI:
  34.259 +    if (require_image)		/* Complain if application wanted an image */
  34.260 +      ERREXIT(cinfo, JERR_NO_IMAGE);
  34.261 +    /* Reset to start state; it would be safer to require the application to
  34.262 +     * call jpeg_abort, but we can't change it now for compatibility reasons.
  34.263 +     * A side effect is to free any temporary memory (there shouldn't be any).
  34.264 +     */
  34.265 +    jpeg_abort((j_common_ptr) cinfo); /* sets state = DSTATE_START */
  34.266 +    retcode = JPEG_HEADER_TABLES_ONLY;
  34.267 +    break;
  34.268 +  case JPEG_SUSPENDED:
  34.269 +    /* no work */
  34.270 +    break;
  34.271 +  }
  34.272 +
  34.273 +  return retcode;
  34.274 +}
  34.275 +
  34.276 +
  34.277 +/*
  34.278 + * Consume data in advance of what the decompressor requires.
  34.279 + * This can be called at any time once the decompressor object has
  34.280 + * been created and a data source has been set up.
  34.281 + *
  34.282 + * This routine is essentially a state machine that handles a couple
  34.283 + * of critical state-transition actions, namely initial setup and
  34.284 + * transition from header scanning to ready-for-start_decompress.
  34.285 + * All the actual input is done via the input controller's consume_input
  34.286 + * method.
  34.287 + */
  34.288 +
  34.289 +GLOBAL(int)
  34.290 +jpeg_consume_input (j_decompress_ptr cinfo)
  34.291 +{
  34.292 +  int retcode = JPEG_SUSPENDED;
  34.293 +
  34.294 +  /* NB: every possible DSTATE value should be listed in this switch */
  34.295 +  switch (cinfo->global_state) {
  34.296 +  case DSTATE_START:
  34.297 +    /* Start-of-datastream actions: reset appropriate modules */
  34.298 +    (*cinfo->inputctl->reset_input_controller) (cinfo);
  34.299 +    /* Initialize application's data source module */
  34.300 +    (*cinfo->src->init_source) (cinfo);
  34.301 +    cinfo->global_state = DSTATE_INHEADER;
  34.302 +    /*FALLTHROUGH*/
  34.303 +  case DSTATE_INHEADER:
  34.304 +    retcode = (*cinfo->inputctl->consume_input) (cinfo);
  34.305 +    if (retcode == JPEG_REACHED_SOS) { /* Found SOS, prepare to decompress */
  34.306 +      /* Set up default parameters based on header data */
  34.307 +      default_decompress_parms(cinfo);
  34.308 +      /* Set global state: ready for start_decompress */
  34.309 +      cinfo->global_state = DSTATE_READY;
  34.310 +    }
  34.311 +    break;
  34.312 +  case DSTATE_READY:
  34.313 +    /* Can't advance past first SOS until start_decompress is called */
  34.314 +    retcode = JPEG_REACHED_SOS;
  34.315 +    break;
  34.316 +  case DSTATE_PRELOAD:
  34.317 +  case DSTATE_PRESCAN:
  34.318 +  case DSTATE_SCANNING:
  34.319 +  case DSTATE_RAW_OK:
  34.320 +  case DSTATE_BUFIMAGE:
  34.321 +  case DSTATE_BUFPOST:
  34.322 +  case DSTATE_STOPPING:
  34.323 +    retcode = (*cinfo->inputctl->consume_input) (cinfo);
  34.324 +    break;
  34.325 +  default:
  34.326 +    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  34.327 +  }
  34.328 +  return retcode;
  34.329 +}
  34.330 +
  34.331 +
  34.332 +/*
  34.333 + * Have we finished reading the input file?
  34.334 + */
  34.335 +
  34.336 +GLOBAL(boolean)
  34.337 +jpeg_input_complete (j_decompress_ptr cinfo)
  34.338 +{
  34.339 +  /* Check for valid jpeg object */
  34.340 +  if (cinfo->global_state < DSTATE_START ||
  34.341 +      cinfo->global_state > DSTATE_STOPPING)
  34.342 +    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  34.343 +  return cinfo->inputctl->eoi_reached;
  34.344 +}
  34.345 +
  34.346 +
  34.347 +/*
  34.348 + * Is there more than one scan?
  34.349 + */
  34.350 +
  34.351 +GLOBAL(boolean)
  34.352 +jpeg_has_multiple_scans (j_decompress_ptr cinfo)
  34.353 +{
  34.354 +  /* Only valid after jpeg_read_header completes */
  34.355 +  if (cinfo->global_state < DSTATE_READY ||
  34.356 +      cinfo->global_state > DSTATE_STOPPING)
  34.357 +    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  34.358 +  return cinfo->inputctl->has_multiple_scans;
  34.359 +}
  34.360 +
  34.361 +
  34.362 +/*
  34.363 + * Finish JPEG decompression.
  34.364 + *
  34.365 + * This will normally just verify the file trailer and release temp storage.
  34.366 + *
  34.367 + * Returns FALSE if suspended.  The return value need be inspected only if
  34.368 + * a suspending data source is used.
  34.369 + */
  34.370 +
  34.371 +GLOBAL(boolean)
  34.372 +jpeg_finish_decompress (j_decompress_ptr cinfo)
  34.373 +{
  34.374 +  if ((cinfo->global_state == DSTATE_SCANNING ||
  34.375 +       cinfo->global_state == DSTATE_RAW_OK) && ! cinfo->buffered_image) {
  34.376 +    /* Terminate final pass of non-buffered mode */
  34.377 +    if (cinfo->output_scanline < cinfo->output_height)
  34.378 +      ERREXIT(cinfo, JERR_TOO_LITTLE_DATA);
  34.379 +    (*cinfo->master->finish_output_pass) (cinfo);
  34.380 +    cinfo->global_state = DSTATE_STOPPING;
  34.381 +  } else if (cinfo->global_state == DSTATE_BUFIMAGE) {
  34.382 +    /* Finishing after a buffered-image operation */
  34.383 +    cinfo->global_state = DSTATE_STOPPING;
  34.384 +  } else if (cinfo->global_state != DSTATE_STOPPING) {
  34.385 +    /* STOPPING = repeat call after a suspension, anything else is error */
  34.386 +    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  34.387 +  }
  34.388 +  /* Read until EOI */
  34.389 +  while (! cinfo->inputctl->eoi_reached) {
  34.390 +    if ((*cinfo->inputctl->consume_input) (cinfo) == JPEG_SUSPENDED)
  34.391 +      return FALSE;		/* Suspend, come back later */
  34.392 +  }
  34.393 +  /* Do final cleanup */
  34.394 +  (*cinfo->src->term_source) (cinfo);
  34.395 +  /* We can use jpeg_abort to release memory and reset global_state */
  34.396 +  jpeg_abort((j_common_ptr) cinfo);
  34.397 +  return TRUE;
  34.398 +}
    35.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    35.2 +++ b/libs/libjpeg/jdapistd.c	Sat Sep 19 05:51:51 2015 +0300
    35.3 @@ -0,0 +1,275 @@
    35.4 +/*
    35.5 + * jdapistd.c
    35.6 + *
    35.7 + * Copyright (C) 1994-1996, Thomas G. Lane.
    35.8 + * This file is part of the Independent JPEG Group's software.
    35.9 + * For conditions of distribution and use, see the accompanying README file.
   35.10 + *
   35.11 + * This file contains application interface code for the decompression half
   35.12 + * of the JPEG library.  These are the "standard" API routines that are
   35.13 + * used in the normal full-decompression case.  They are not used by a
   35.14 + * transcoding-only application.  Note that if an application links in
   35.15 + * jpeg_start_decompress, it will end up linking in the entire decompressor.
   35.16 + * We thus must separate this file from jdapimin.c to avoid linking the
   35.17 + * whole decompression library into a transcoder.
   35.18 + */
   35.19 +
   35.20 +#define JPEG_INTERNALS
   35.21 +#include "jinclude.h"
   35.22 +#include "jpeglib.h"
   35.23 +
   35.24 +
   35.25 +/* Forward declarations */
   35.26 +LOCAL(boolean) output_pass_setup JPP((j_decompress_ptr cinfo));
   35.27 +
   35.28 +
   35.29 +/*
   35.30 + * Decompression initialization.
   35.31 + * jpeg_read_header must be completed before calling this.
   35.32 + *
   35.33 + * If a multipass operating mode was selected, this will do all but the
   35.34 + * last pass, and thus may take a great deal of time.
   35.35 + *
   35.36 + * Returns FALSE if suspended.  The return value need be inspected only if
   35.37 + * a suspending data source is used.
   35.38 + */
   35.39 +
   35.40 +GLOBAL(boolean)
   35.41 +jpeg_start_decompress (j_decompress_ptr cinfo)
   35.42 +{
   35.43 +  if (cinfo->global_state == DSTATE_READY) {
   35.44 +    /* First call: initialize master control, select active modules */
   35.45 +    jinit_master_decompress(cinfo);
   35.46 +    if (cinfo->buffered_image) {
   35.47 +      /* No more work here; expecting jpeg_start_output next */
   35.48 +      cinfo->global_state = DSTATE_BUFIMAGE;
   35.49 +      return TRUE;
   35.50 +    }
   35.51 +    cinfo->global_state = DSTATE_PRELOAD;
   35.52 +  }
   35.53 +  if (cinfo->global_state == DSTATE_PRELOAD) {
   35.54 +    /* If file has multiple scans, absorb them all into the coef buffer */
   35.55 +    if (cinfo->inputctl->has_multiple_scans) {
   35.56 +#ifdef D_MULTISCAN_FILES_SUPPORTED
   35.57 +      for (;;) {
   35.58 +	int retcode;
   35.59 +	/* Call progress monitor hook if present */
   35.60 +	if (cinfo->progress != NULL)
   35.61 +	  (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
   35.62 +	/* Absorb some more input */
   35.63 +	retcode = (*cinfo->inputctl->consume_input) (cinfo);
   35.64 +	if (retcode == JPEG_SUSPENDED)
   35.65 +	  return FALSE;
   35.66 +	if (retcode == JPEG_REACHED_EOI)
   35.67 +	  break;
   35.68 +	/* Advance progress counter if appropriate */
   35.69 +	if (cinfo->progress != NULL &&
   35.70 +	    (retcode == JPEG_ROW_COMPLETED || retcode == JPEG_REACHED_SOS)) {
   35.71 +	  if (++cinfo->progress->pass_counter >= cinfo->progress->pass_limit) {
   35.72 +	    /* jdmaster underestimated number of scans; ratchet up one scan */
   35.73 +	    cinfo->progress->pass_limit += (long) cinfo->total_iMCU_rows;
   35.74 +	  }
   35.75 +	}
   35.76 +      }
   35.77 +#else
   35.78 +      ERREXIT(cinfo, JERR_NOT_COMPILED);
   35.79 +#endif /* D_MULTISCAN_FILES_SUPPORTED */
   35.80 +    }
   35.81 +    cinfo->output_scan_number = cinfo->input_scan_number;
   35.82 +  } else if (cinfo->global_state != DSTATE_PRESCAN)
   35.83 +    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
   35.84 +  /* Perform any dummy output passes, and set up for the final pass */
   35.85 +  return output_pass_setup(cinfo);
   35.86 +}
   35.87 +
   35.88 +
   35.89 +/*
   35.90 + * Set up for an output pass, and perform any dummy pass(es) needed.
   35.91 + * Common subroutine for jpeg_start_decompress and jpeg_start_output.
   35.92 + * Entry: global_state = DSTATE_PRESCAN only if previously suspended.
   35.93 + * Exit: If done, returns TRUE and sets global_state for proper output mode.
   35.94 + *       If suspended, returns FALSE and sets global_state = DSTATE_PRESCAN.
   35.95 + */
   35.96 +
   35.97 +LOCAL(boolean)
   35.98 +output_pass_setup (j_decompress_ptr cinfo)
   35.99 +{
  35.100 +  if (cinfo->global_state != DSTATE_PRESCAN) {
  35.101 +    /* First call: do pass setup */
  35.102 +    (*cinfo->master->prepare_for_output_pass) (cinfo);
  35.103 +    cinfo->output_scanline = 0;
  35.104 +    cinfo->global_state = DSTATE_PRESCAN;
  35.105 +  }
  35.106 +  /* Loop over any required dummy passes */
  35.107 +  while (cinfo->master->is_dummy_pass) {
  35.108 +#ifdef QUANT_2PASS_SUPPORTED
  35.109 +    /* Crank through the dummy pass */
  35.110 +    while (cinfo->output_scanline < cinfo->output_height) {
  35.111 +      JDIMENSION last_scanline;
  35.112 +      /* Call progress monitor hook if present */
  35.113 +      if (cinfo->progress != NULL) {
  35.114 +	cinfo->progress->pass_counter = (long) cinfo->output_scanline;
  35.115 +	cinfo->progress->pass_limit = (long) cinfo->output_height;
  35.116 +	(*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
  35.117 +      }
  35.118 +      /* Process some data */
  35.119 +      last_scanline = cinfo->output_scanline;
  35.120 +      (*cinfo->main->process_data) (cinfo, (JSAMPARRAY) NULL,
  35.121 +				    &cinfo->output_scanline, (JDIMENSION) 0);
  35.122 +      if (cinfo->output_scanline == last_scanline)
  35.123 +	return FALSE;		/* No progress made, must suspend */
  35.124 +    }
  35.125 +    /* Finish up dummy pass, and set up for another one */
  35.126 +    (*cinfo->master->finish_output_pass) (cinfo);
  35.127 +    (*cinfo->master->prepare_for_output_pass) (cinfo);
  35.128 +    cinfo->output_scanline = 0;
  35.129 +#else
  35.130 +    ERREXIT(cinfo, JERR_NOT_COMPILED);
  35.131 +#endif /* QUANT_2PASS_SUPPORTED */
  35.132 +  }
  35.133 +  /* Ready for application to drive output pass through
  35.134 +   * jpeg_read_scanlines or jpeg_read_raw_data.
  35.135 +   */
  35.136 +  cinfo->global_state = cinfo->raw_data_out ? DSTATE_RAW_OK : DSTATE_SCANNING;
  35.137 +  return TRUE;
  35.138 +}
  35.139 +
  35.140 +
  35.141 +/*
  35.142 + * Read some scanlines of data from the JPEG decompressor.
  35.143 + *
  35.144 + * The return value will be the number of lines actually read.
  35.145 + * This may be less than the number requested in several cases,
  35.146 + * including bottom of image, data source suspension, and operating
  35.147 + * modes that emit multiple scanlines at a time.
  35.148 + *
  35.149 + * Note: we warn about excess calls to jpeg_read_scanlines() since
  35.150 + * this likely signals an application programmer error.  However,
  35.151 + * an oversize buffer (max_lines > scanlines remaining) is not an error.
  35.152 + */
  35.153 +
  35.154 +GLOBAL(JDIMENSION)
  35.155 +jpeg_read_scanlines (j_decompress_ptr cinfo, JSAMPARRAY scanlines,
  35.156 +		     JDIMENSION max_lines)
  35.157 +{
  35.158 +  JDIMENSION row_ctr;
  35.159 +
  35.160 +  if (cinfo->global_state != DSTATE_SCANNING)
  35.161 +    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  35.162 +  if (cinfo->output_scanline >= cinfo->output_height) {
  35.163 +    WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
  35.164 +    return 0;
  35.165 +  }
  35.166 +
  35.167 +  /* Call progress monitor hook if present */
  35.168 +  if (cinfo->progress != NULL) {
  35.169 +    cinfo->progress->pass_counter = (long) cinfo->output_scanline;
  35.170 +    cinfo->progress->pass_limit = (long) cinfo->output_height;
  35.171 +    (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
  35.172 +  }
  35.173 +
  35.174 +  /* Process some data */
  35.175 +  row_ctr = 0;
  35.176 +  (*cinfo->main->process_data) (cinfo, scanlines, &row_ctr, max_lines);
  35.177 +  cinfo->output_scanline += row_ctr;
  35.178 +  return row_ctr;
  35.179 +}
  35.180 +
  35.181 +
  35.182 +/*
  35.183 + * Alternate entry point to read raw data.
  35.184 + * Processes exactly one iMCU row per call, unless suspended.
  35.185 + */
  35.186 +
  35.187 +GLOBAL(JDIMENSION)
  35.188 +jpeg_read_raw_data (j_decompress_ptr cinfo, JSAMPIMAGE data,
  35.189 +		    JDIMENSION max_lines)
  35.190 +{
  35.191 +  JDIMENSION lines_per_iMCU_row;
  35.192 +
  35.193 +  if (cinfo->global_state != DSTATE_RAW_OK)
  35.194 +    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  35.195 +  if (cinfo->output_scanline >= cinfo->output_height) {
  35.196 +    WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
  35.197 +    return 0;
  35.198 +  }
  35.199 +
  35.200 +  /* Call progress monitor hook if present */
  35.201 +  if (cinfo->progress != NULL) {
  35.202 +    cinfo->progress->pass_counter = (long) cinfo->output_scanline;
  35.203 +    cinfo->progress->pass_limit = (long) cinfo->output_height;
  35.204 +    (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
  35.205 +  }
  35.206 +
  35.207 +  /* Verify that at least one iMCU row can be returned. */
  35.208 +  lines_per_iMCU_row = cinfo->max_v_samp_factor * cinfo->min_DCT_scaled_size;
  35.209 +  if (max_lines < lines_per_iMCU_row)
  35.210 +    ERREXIT(cinfo, JERR_BUFFER_SIZE);
  35.211 +
  35.212 +  /* Decompress directly into user's buffer. */
  35.213 +  if (! (*cinfo->coef->decompress_data) (cinfo, data))
  35.214 +    return 0;			/* suspension forced, can do nothing more */
  35.215 +
  35.216 +  /* OK, we processed one iMCU row. */
  35.217 +  cinfo->output_scanline += lines_per_iMCU_row;
  35.218 +  return lines_per_iMCU_row;
  35.219 +}
  35.220 +
  35.221 +
  35.222 +/* Additional entry points for buffered-image mode. */
  35.223 +
  35.224 +#ifdef D_MULTISCAN_FILES_SUPPORTED
  35.225 +
  35.226 +/*
  35.227 + * Initialize for an output pass in buffered-image mode.
  35.228 + */
  35.229 +
  35.230 +GLOBAL(boolean)
  35.231 +jpeg_start_output (j_decompress_ptr cinfo, int scan_number)
  35.232 +{
  35.233 +  if (cinfo->global_state != DSTATE_BUFIMAGE &&
  35.234 +      cinfo->global_state != DSTATE_PRESCAN)
  35.235 +    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  35.236 +  /* Limit scan number to valid range */
  35.237 +  if (scan_number <= 0)
  35.238 +    scan_number = 1;
  35.239 +  if (cinfo->inputctl->eoi_reached &&
  35.240 +      scan_number > cinfo->input_scan_number)
  35.241 +    scan_number = cinfo->input_scan_number;
  35.242 +  cinfo->output_scan_number = scan_number;
  35.243 +  /* Perform any dummy output passes, and set up for the real pass */
  35.244 +  return output_pass_setup(cinfo);
  35.245 +}
  35.246 +
  35.247 +
  35.248 +/*
  35.249 + * Finish up after an output pass in buffered-image mode.
  35.250 + *
  35.251 + * Returns FALSE if suspended.  The return value need be inspected only if
  35.252 + * a suspending data source is used.
  35.253 + */
  35.254 +
  35.255 +GLOBAL(boolean)
  35.256 +jpeg_finish_output (j_decompress_ptr cinfo)
  35.257 +{
  35.258 +  if ((cinfo->global_state == DSTATE_SCANNING ||
  35.259 +       cinfo->global_state == DSTATE_RAW_OK) && cinfo->buffered_image) {
  35.260 +    /* Terminate this pass. */
  35.261 +    /* We do not require the whole pass to have been completed. */
  35.262 +    (*cinfo->master->finish_output_pass) (cinfo);
  35.263 +    cinfo->global_state = DSTATE_BUFPOST;
  35.264 +  } else if (cinfo->global_state != DSTATE_BUFPOST) {
  35.265 +    /* BUFPOST = repeat call after a suspension, anything else is error */
  35.266 +    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  35.267 +  }
  35.268 +  /* Read markers looking for SOS or EOI */
  35.269 +  while (cinfo->input_scan_number <= cinfo->output_scan_number &&
  35.270 +	 ! cinfo->inputctl->eoi_reached) {
  35.271 +    if ((*cinfo->inputctl->consume_input) (cinfo) == JPEG_SUSPENDED)
  35.272 +      return FALSE;		/* Suspend, come back later */
  35.273 +  }
  35.274 +  cinfo->global_state = DSTATE_BUFIMAGE;
  35.275 +  return TRUE;
  35.276 +}
  35.277 +
  35.278 +#endif /* D_MULTISCAN_FILES_SUPPORTED */
    36.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    36.2 +++ b/libs/libjpeg/jdatadst.c	Sat Sep 19 05:51:51 2015 +0300
    36.3 @@ -0,0 +1,151 @@
    36.4 +/*
    36.5 + * jdatadst.c
    36.6 + *
    36.7 + * Copyright (C) 1994-1996, Thomas G. Lane.
    36.8 + * This file is part of the Independent JPEG Group's software.
    36.9 + * For conditions of distribution and use, see the accompanying README file.
   36.10 + *
   36.11 + * This file contains compression data destination routines for the case of
   36.12 + * emitting JPEG data to a file (or any stdio stream).  While these routines
   36.13 + * are sufficient for most applications, some will want to use a different
   36.14 + * destination manager.
   36.15 + * IMPORTANT: we assume that fwrite() will correctly transcribe an array of
   36.16 + * JOCTETs into 8-bit-wide elements on external storage.  If char is wider
   36.17 + * than 8 bits on your machine, you may need to do some tweaking.
   36.18 + */
   36.19 +
   36.20 +/* this is not a core library module, so it doesn't define JPEG_INTERNALS */
   36.21 +#include "jinclude.h"
   36.22 +#include "jpeglib.h"
   36.23 +#include "jerror.h"
   36.24 +
   36.25 +
   36.26 +/* Expanded data destination object for stdio output */
   36.27 +
   36.28 +typedef struct {
   36.29 +  struct jpeg_destination_mgr pub; /* public fields */
   36.30 +
   36.31 +  FILE * outfile;		/* target stream */
   36.32 +  JOCTET * buffer;		/* start of buffer */
   36.33 +} my_destination_mgr;
   36.34 +
   36.35 +typedef my_destination_mgr * my_dest_ptr;
   36.36 +
   36.37 +#define OUTPUT_BUF_SIZE  4096	/* choose an efficiently fwrite'able size */
   36.38 +
   36.39 +
   36.40 +/*
   36.41 + * Initialize destination --- called by jpeg_start_compress
   36.42 + * before any data is actually written.
   36.43 + */
   36.44 +
   36.45 +METHODDEF(void)
   36.46 +init_destination (j_compress_ptr cinfo)
   36.47 +{
   36.48 +  my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
   36.49 +
   36.50 +  /* Allocate the output buffer --- it will be released when done with image */
   36.51 +  dest->buffer = (JOCTET *)
   36.52 +      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
   36.53 +				  OUTPUT_BUF_SIZE * SIZEOF(JOCTET));
   36.54 +
   36.55 +  dest->pub.next_output_byte = dest->buffer;
   36.56 +  dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
   36.57 +}
   36.58 +
   36.59 +
   36.60 +/*
   36.61 + * Empty the output buffer --- called whenever buffer fills up.
   36.62 + *
   36.63 + * In typical applications, this should write the entire output buffer
   36.64 + * (ignoring the current state of next_output_byte & free_in_buffer),
   36.65 + * reset the pointer & count to the start of the buffer, and return TRUE
   36.66 + * indicating that the buffer has been dumped.
   36.67 + *
   36.68 + * In applications that need to be able to suspend compression due to output
   36.69 + * overrun, a FALSE return indicates that the buffer cannot be emptied now.
   36.70 + * In this situation, the compressor will return to its caller (possibly with
   36.71 + * an indication that it has not accepted all the supplied scanlines).  The
   36.72 + * application should resume compression after it has made more room in the
   36.73 + * output buffer.  Note that there are substantial restrictions on the use of
   36.74 + * suspension --- see the documentation.
   36.75 + *
   36.76 + * When suspending, the compressor will back up to a convenient restart point
   36.77 + * (typically the start of the current MCU). next_output_byte & free_in_buffer
   36.78 + * indicate where the restart point will be if the current call returns FALSE.
   36.79 + * Data beyond this point will be regenerated after resumption, so do not
   36.80 + * write it out when emptying the buffer externally.
   36.81 + */
   36.82 +
   36.83 +METHODDEF(boolean)
   36.84 +empty_output_buffer (j_compress_ptr cinfo)
   36.85 +{
   36.86 +  my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
   36.87 +
   36.88 +  if (JFWRITE(dest->outfile, dest->buffer, OUTPUT_BUF_SIZE) !=
   36.89 +      (size_t) OUTPUT_BUF_SIZE)
   36.90 +    ERREXIT(cinfo, JERR_FILE_WRITE);
   36.91 +
   36.92 +  dest->pub.next_output_byte = dest->buffer;
   36.93 +  dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
   36.94 +
   36.95 +  return TRUE;
   36.96 +}
   36.97 +
   36.98 +
   36.99 +/*
  36.100 + * Terminate destination --- called by jpeg_finish_compress
  36.101 + * after all data has been written.  Usually needs to flush buffer.
  36.102 + *
  36.103 + * NB: *not* called by jpeg_abort or jpeg_destroy; surrounding
  36.104 + * application must deal with any cleanup that should happen even
  36.105 + * for error exit.
  36.106 + */
  36.107 +
  36.108 +METHODDEF(void)
  36.109 +term_destination (j_compress_ptr cinfo)
  36.110 +{
  36.111 +  my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
  36.112 +  size_t datacount = OUTPUT_BUF_SIZE - dest->pub.free_in_buffer;
  36.113 +
  36.114 +  /* Write any data remaining in the buffer */
  36.115 +  if (datacount > 0) {
  36.116 +    if (JFWRITE(dest->outfile, dest->buffer, datacount) != datacount)
  36.117 +      ERREXIT(cinfo, JERR_FILE_WRITE);
  36.118 +  }
  36.119 +  fflush(dest->outfile);
  36.120 +  /* Make sure we wrote the output file OK */
  36.121 +  if (ferror(dest->outfile))
  36.122 +    ERREXIT(cinfo, JERR_FILE_WRITE);
  36.123 +}
  36.124 +
  36.125 +
  36.126 +/*
  36.127 + * Prepare for output to a stdio stream.
  36.128 + * The caller must have already opened the stream, and is responsible
  36.129 + * for closing it after finishing compression.
  36.130 + */
  36.131 +
  36.132 +GLOBAL(void)
  36.133 +jpeg_stdio_dest (j_compress_ptr cinfo, FILE * outfile)
  36.134 +{
  36.135 +  my_dest_ptr dest;
  36.136 +
  36.137 +  /* The destination object is made permanent so that multiple JPEG images
  36.138 +   * can be written to the same file without re-executing jpeg_stdio_dest.
  36.139 +   * This makes it dangerous to use this manager and a different destination
  36.140 +   * manager serially with the same JPEG object, because their private object
  36.141 +   * sizes may be different.  Caveat programmer.
  36.142 +   */
  36.143 +  if (cinfo->dest == NULL) {	/* first time for this JPEG object? */
  36.144 +    cinfo->dest = (struct jpeg_destination_mgr *)
  36.145 +      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
  36.146 +				  SIZEOF(my_destination_mgr));
  36.147 +  }
  36.148 +
  36.149 +  dest = (my_dest_ptr) cinfo->dest;
  36.150 +  dest->pub.init_destination = init_destination;
  36.151 +  dest->pub.empty_output_buffer = empty_output_buffer;
  36.152 +  dest->pub.term_destination = term_destination;
  36.153 +  dest->outfile = outfile;
  36.154 +}
    37.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    37.2 +++ b/libs/libjpeg/jdatasrc.c	Sat Sep 19 05:51:51 2015 +0300
    37.3 @@ -0,0 +1,212 @@
    37.4 +/*
    37.5 + * jdatasrc.c
    37.6 + *
    37.7 + * Copyright (C) 1994-1996, Thomas G. Lane.
    37.8 + * This file is part of the Independent JPEG Group's software.
    37.9 + * For conditions of distribution and use, see the accompanying README file.
   37.10 + *
   37.11 + * This file contains decompression data source routines for the case of
   37.12 + * reading JPEG data from a file (or any stdio stream).  While these routines
   37.13 + * are sufficient for most applications, some will want to use a different
   37.14 + * source manager.
   37.15 + * IMPORTANT: we assume that fread() will correctly transcribe an array of
   37.16 + * JOCTETs from 8-bit-wide elements on external storage.  If char is wider
   37.17 + * than 8 bits on your machine, you may need to do some tweaking.
   37.18 + */
   37.19 +
   37.20 +/* this is not a core library module, so it doesn't define JPEG_INTERNALS */
   37.21 +#include "jinclude.h"
   37.22 +#include "jpeglib.h"
   37.23 +#include "jerror.h"
   37.24 +
   37.25 +
   37.26 +/* Expanded data source object for stdio input */
   37.27 +
   37.28 +typedef struct {
   37.29 +  struct jpeg_source_mgr pub;	/* public fields */
   37.30 +
   37.31 +  FILE * infile;		/* source stream */
   37.32 +  JOCTET * buffer;		/* start of buffer */
   37.33 +  boolean start_of_file;	/* have we gotten any data yet? */
   37.34 +} my_source_mgr;
   37.35 +
   37.36 +typedef my_source_mgr * my_src_ptr;
   37.37 +
   37.38 +#define INPUT_BUF_SIZE  4096	/* choose an efficiently fread'able size */
   37.39 +
   37.40 +
   37.41 +/*
   37.42 + * Initialize source --- called by jpeg_read_header
   37.43 + * before any data is actually read.
   37.44 + */
   37.45 +
   37.46 +METHODDEF(void)
   37.47 +init_source (j_decompress_ptr cinfo)
   37.48 +{
   37.49 +  my_src_ptr src = (my_src_ptr) cinfo->src;
   37.50 +
   37.51 +  /* We reset the empty-input-file flag for each image,
   37.52 +   * but we don't clear the input buffer.
   37.53 +   * This is correct behavior for reading a series of images from one source.
   37.54 +   */
   37.55 +  src->start_of_file = TRUE;
   37.56 +}
   37.57 +
   37.58 +
   37.59 +/*
   37.60 + * Fill the input buffer --- called whenever buffer is emptied.
   37.61 + *
   37.62 + * In typical applications, this should read fresh data into the buffer
   37.63 + * (ignoring the current state of next_input_byte & bytes_in_buffer),
   37.64 + * reset the pointer & count to the start of the buffer, and return TRUE
   37.65 + * indicating that the buffer has been reloaded.  It is not necessary to
   37.66 + * fill the buffer entirely, only to obtain at least one more byte.
   37.67 + *
   37.68 + * There is no such thing as an EOF return.  If the end of the file has been
   37.69 + * reached, the routine has a choice of ERREXIT() or inserting fake data into
   37.70 + * the buffer.  In most cases, generating a warning message and inserting a
   37.71 + * fake EOI marker is the best course of action --- this will allow the
   37.72 + * decompressor to output however much of the image is there.  However,
   37.73 + * the resulting error message is misleading if the real problem is an empty
   37.74 + * input file, so we handle that case specially.
   37.75 + *
   37.76 + * In applications that need to be able to suspend compression due to input
   37.77 + * not being available yet, a FALSE return indicates that no more data can be
   37.78 + * obtained right now, but more may be forthcoming later.  In this situation,
   37.79 + * the decompressor will return to its caller (with an indication of the
   37.80 + * number of scanlines it has read, if any).  The application should resume
   37.81 + * decompression after it has loaded more data into the input buffer.  Note
   37.82 + * that there are substantial restrictions on the use of suspension --- see
   37.83 + * the documentation.
   37.84 + *
   37.85 + * When suspending, the decompressor will back up to a convenient restart point
   37.86 + * (typically the start of the current MCU). next_input_byte & bytes_in_buffer
   37.87 + * indicate where the restart point will be if the current call returns FALSE.
   37.88 + * Data beyond this point must be rescanned after resumption, so move it to
   37.89 + * the front of the buffer rather than discarding it.
   37.90 + */
   37.91 +
   37.92 +METHODDEF(boolean)
   37.93 +fill_input_buffer (j_decompress_ptr cinfo)
   37.94 +{
   37.95 +  my_src_ptr src = (my_src_ptr) cinfo->src;
   37.96 +  size_t nbytes;
   37.97 +
   37.98 +  nbytes = JFREAD(src->infile, src->buffer, INPUT_BUF_SIZE);
   37.99 +
  37.100 +  if (nbytes <= 0) {
  37.101 +    if (src->start_of_file)	/* Treat empty input file as fatal error */
  37.102 +      ERREXIT(cinfo, JERR_INPUT_EMPTY);
  37.103 +    WARNMS(cinfo, JWRN_JPEG_EOF);
  37.104 +    /* Insert a fake EOI marker */
  37.105 +    src->buffer[0] = (JOCTET) 0xFF;
  37.106 +    src->buffer[1] = (JOCTET) JPEG_EOI;
  37.107 +    nbytes = 2;
  37.108 +  }
  37.109 +
  37.110 +  src->pub.next_input_byte = src->buffer;
  37.111 +  src->pub.bytes_in_buffer = nbytes;
  37.112 +  src->start_of_file = FALSE;
  37.113 +
  37.114 +  return TRUE;
  37.115 +}
  37.116 +
  37.117 +
  37.118 +/*
  37.119 + * Skip data --- used to skip over a potentially large amount of
  37.120 + * uninteresting data (such as an APPn marker).
  37.121 + *
  37.122 + * Writers of suspendable-input applications must note that skip_input_data
  37.123 + * is not granted the right to give a suspension return.  If the skip extends
  37.124 + * beyond the data currently in the buffer, the buffer can be marked empty so
  37.125 + * that the next read will cause a fill_input_buffer call that can suspend.
  37.126 + * Arranging for additional bytes to be discarded before reloading the input
  37.127 + * buffer is the application writer's problem.
  37.128 + */
  37.129 +
  37.130 +METHODDEF(void)
  37.131 +skip_input_data (j_decompress_ptr cinfo, long num_bytes)
  37.132 +{
  37.133 +  my_src_ptr src = (my_src_ptr) cinfo->src;
  37.134 +
  37.135 +  /* Just a dumb implementation for now.  Could use fseek() except
  37.136 +   * it doesn't work on pipes.  Not clear that being smart is worth
  37.137 +   * any trouble anyway --- large skips are infrequent.
  37.138 +   */
  37.139 +  if (num_bytes > 0) {
  37.140 +    while (num_bytes > (long) src->pub.bytes_in_buffer) {
  37.141 +      num_bytes -= (long) src->pub.bytes_in_buffer;
  37.142 +      (void) fill_input_buffer(cinfo);
  37.143 +      /* note we assume that fill_input_buffer will never return FALSE,
  37.144 +       * so suspension need not be handled.
  37.145 +       */
  37.146 +    }
  37.147 +    src->pub.next_input_byte += (size_t) num_bytes;
  37.148 +    src->pub.bytes_in_buffer -= (size_t) num_bytes;
  37.149 +  }
  37.150 +}
  37.151 +
  37.152 +
  37.153 +/*
  37.154 + * An additional method that can be provided by data source modules is the
  37.155 + * resync_to_restart method for error recovery in the presence of RST markers.
  37.156 + * For the moment, this source module just uses the default resync method
  37.157 + * provided by the JPEG library.  That method assumes that no backtracking
  37.158 + * is possible.
  37.159 + */
  37.160 +
  37.161 +
  37.162 +/*
  37.163 + * Terminate source --- called by jpeg_finish_decompress
  37.164 + * after all data has been read.  Often a no-op.
  37.165 + *
  37.166 + * NB: *not* called by jpeg_abort or jpeg_destroy; surrounding
  37.167 + * application must deal with any cleanup that should happen even
  37.168 + * for error exit.
  37.169 + */
  37.170 +
  37.171 +METHODDEF(void)
  37.172 +term_source (j_decompress_ptr cinfo)
  37.173 +{
  37.174 +  /* no work necessary here */
  37.175 +}
  37.176 +
  37.177 +
  37.178 +/*
  37.179 + * Prepare for input from a stdio stream.
  37.180 + * The caller must have already opened the stream, and is responsible
  37.181 + * for closing it after finishing decompression.
  37.182 + */
  37.183 +
  37.184 +GLOBAL(void)
  37.185 +jpeg_stdio_src (j_decompress_ptr cinfo, FILE * infile)
  37.186 +{
  37.187 +  my_src_ptr src;
  37.188 +
  37.189 +  /* The source object and input buffer are made permanent so that a series
  37.190 +   * of JPEG images can be read from the same file by calling jpeg_stdio_src
  37.191 +   * only before the first one.  (If we discarded the buffer at the end of
  37.192 +   * one image, we'd likely lose the start of the next one.)
  37.193 +   * This makes it unsafe to use this manager and a different source
  37.194 +   * manager serially with the same JPEG object.  Caveat programmer.
  37.195 +   */
  37.196 +  if (cinfo->src == NULL) {	/* first time for this JPEG object? */
  37.197 +    cinfo->src = (struct jpeg_source_mgr *)
  37.198 +      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
  37.199 +				  SIZEOF(my_source_mgr));
  37.200 +    src = (my_src_ptr) cinfo->src;
  37.201 +    src->buffer = (JOCTET *)
  37.202 +      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
  37.203 +				  INPUT_BUF_SIZE * SIZEOF(JOCTET));
  37.204 +  }
  37.205 +
  37.206 +  src = (my_src_ptr) cinfo->src;
  37.207 +  src->pub.init_source = init_source;
  37.208 +  src->pub.fill_input_buffer = fill_input_buffer;
  37.209 +  src->pub.skip_input_data = skip_input_data;
  37.210 +  src->pub.resync_to_restart = jpeg_resync_to_restart; /* use default method */
  37.211 +  src->pub.term_source = term_source;
  37.212 +  src->infile = infile;
  37.213 +  src->pub.bytes_in_buffer = 0; /* forces fill_input_buffer on first read */
  37.214 +  src->pub.next_input_byte = NULL; /* until buffer loaded */
  37.215 +}
    38.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    38.2 +++ b/libs/libjpeg/jdcoefct.c	Sat Sep 19 05:51:51 2015 +0300
    38.3 @@ -0,0 +1,736 @@
    38.4 +/*
    38.5 + * jdcoefct.c
    38.6 + *
    38.7 + * Copyright (C) 1994-1997, Thomas G. Lane.
    38.8 + * This file is part of the Independent JPEG Group's software.
    38.9 + * For conditions of distribution and use, see the accompanying README file.
   38.10 + *
   38.11 + * This file contains the coefficient buffer controller for decompression.
   38.12 + * This controller is the top level of the JPEG decompressor proper.
   38.13 + * The coefficient buffer lies between entropy decoding and inverse-DCT steps.
   38.14 + *
   38.15 + * In buffered-image mode, this controller is the interface between
   38.16 + * input-oriented processing and output-oriented processing.
   38.17 + * Also, the input side (only) is used when reading a file for transcoding.
   38.18 + */
   38.19 +
   38.20 +#define JPEG_INTERNALS
   38.21 +#include "jinclude.h"
   38.22 +#include "jpeglib.h"
   38.23 +
   38.24 +/* Block smoothing is only applicable for progressive JPEG, so: */
   38.25 +#ifndef D_PROGRESSIVE_SUPPORTED
   38.26 +#undef BLOCK_SMOOTHING_SUPPORTED
   38.27 +#endif
   38.28 +
   38.29 +/* Private buffer controller object */
   38.30 +
   38.31 +typedef struct {
   38.32 +  struct jpeg_d_coef_controller pub; /* public fields */
   38.33 +
   38.34 +  /* These variables keep track of the current location of the input side. */
   38.35 +  /* cinfo->input_iMCU_row is also used for this. */
   38.36 +  JDIMENSION MCU_ctr;		/* counts MCUs processed in current row */
   38.37 +  int MCU_vert_offset;		/* counts MCU rows within iMCU row */
   38.38 +  int MCU_rows_per_iMCU_row;	/* number of such rows needed */
   38.39 +
   38.40 +  /* The output side's location is represented by cinfo->output_iMCU_row. */
   38.41 +
   38.42 +  /* In single-pass modes, it's sufficient to buffer just one MCU.
   38.43 +   * We allocate a workspace of D_MAX_BLOCKS_IN_MCU coefficient blocks,
   38.44 +   * and let the entropy decoder write into that workspace each time.
   38.45 +   * (On 80x86, the workspace is FAR even though it's not really very big;
   38.46 +   * this is to keep the module interfaces unchanged when a large coefficient
   38.47 +   * buffer is necessary.)
   38.48 +   * In multi-pass modes, this array points to the current MCU's blocks
   38.49 +   * within the virtual arrays; it is used only by the input side.
   38.50 +   */
   38.51 +  JBLOCKROW MCU_buffer[D_MAX_BLOCKS_IN_MCU];
   38.52 +
   38.53 +#ifdef D_MULTISCAN_FILES_SUPPORTED
   38.54 +  /* In multi-pass modes, we need a virtual block array for each component. */
   38.55 +  jvirt_barray_ptr whole_image[MAX_COMPONENTS];
   38.56 +#endif
   38.57 +
   38.58 +#ifdef BLOCK_SMOOTHING_SUPPORTED
   38.59 +  /* When doing block smoothing, we latch coefficient Al values here */
   38.60 +  int * coef_bits_latch;
   38.61 +#define SAVED_COEFS  6		/* we save coef_bits[0..5] */
   38.62 +#endif
   38.63 +} my_coef_controller;
   38.64 +
   38.65 +typedef my_coef_controller * my_coef_ptr;
   38.66 +
   38.67 +/* Forward declarations */
   38.68 +METHODDEF(int) decompress_onepass
   38.69 +	JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf));
   38.70 +#ifdef D_MULTISCAN_FILES_SUPPORTED
   38.71 +METHODDEF(int) decompress_data
   38.72 +	JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf));
   38.73 +#endif
   38.74 +#ifdef BLOCK_SMOOTHING_SUPPORTED
   38.75 +LOCAL(boolean) smoothing_ok JPP((j_decompress_ptr cinfo));
   38.76 +METHODDEF(int) decompress_smooth_data
   38.77 +	JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf));
   38.78 +#endif
   38.79 +
   38.80 +
   38.81 +LOCAL(void)
   38.82 +start_iMCU_row (j_decompress_ptr cinfo)
   38.83 +/* Reset within-iMCU-row counters for a new row (input side) */
   38.84 +{
   38.85 +  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
   38.86 +
   38.87 +  /* In an interleaved scan, an MCU row is the same as an iMCU row.
   38.88 +   * In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows.
   38.89 +   * But at the bottom of the image, process only what's left.
   38.90 +   */
   38.91 +  if (cinfo->comps_in_scan > 1) {
   38.92 +    coef->MCU_rows_per_iMCU_row = 1;
   38.93 +  } else {
   38.94 +    if (cinfo->input_iMCU_row < (cinfo->total_iMCU_rows-1))
   38.95 +      coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor;
   38.96 +    else
   38.97 +      coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height;
   38.98 +  }
   38.99 +
  38.100 +  coef->MCU_ctr = 0;
  38.101 +  coef->MCU_vert_offset = 0;
  38.102 +}
  38.103 +
  38.104 +
  38.105 +/*
  38.106 + * Initialize for an input processing pass.
  38.107 + */
  38.108 +
  38.109 +METHODDEF(void)
  38.110 +start_input_pass (j_decompress_ptr cinfo)
  38.111 +{
  38.112 +  cinfo->input_iMCU_row = 0;
  38.113 +  start_iMCU_row(cinfo);
  38.114 +}
  38.115 +
  38.116 +
  38.117 +/*
  38.118 + * Initialize for an output processing pass.
  38.119 + */
  38.120 +
  38.121 +METHODDEF(void)
  38.122 +start_output_pass (j_decompress_ptr cinfo)
  38.123 +{
  38.124 +#ifdef BLOCK_SMOOTHING_SUPPORTED
  38.125 +  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
  38.126 +
  38.127 +  /* If multipass, check to see whether to use block smoothing on this pass */
  38.128 +  if (coef->pub.coef_arrays != NULL) {
  38.129 +    if (cinfo->do_block_smoothing && smoothing_ok(cinfo))
  38.130 +      coef->pub.decompress_data = decompress_smooth_data;
  38.131 +    else
  38.132 +      coef->pub.decompress_data = decompress_data;
  38.133 +  }
  38.134 +#endif
  38.135 +  cinfo->output_iMCU_row = 0;
  38.136 +}
  38.137 +
  38.138 +
  38.139 +/*
  38.140 + * Decompress and return some data in the single-pass case.
  38.141 + * Always attempts to emit one fully interleaved MCU row ("iMCU" row).
  38.142 + * Input and output must run in lockstep since we have only a one-MCU buffer.
  38.143 + * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
  38.144 + *
  38.145 + * NB: output_buf contains a plane for each component in image,
  38.146 + * which we index according to the component's SOF position.
  38.147 + */
  38.148 +
  38.149 +METHODDEF(int)
  38.150 +decompress_onepass (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
  38.151 +{
  38.152 +  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
  38.153 +  JDIMENSION MCU_col_num;	/* index of current MCU within row */
  38.154 +  JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1;
  38.155 +  JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
  38.156 +  int blkn, ci, xindex, yindex, yoffset, useful_width;
  38.157 +  JSAMPARRAY output_ptr;
  38.158 +  JDIMENSION start_col, output_col;
  38.159 +  jpeg_component_info *compptr;
  38.160 +  inverse_DCT_method_ptr inverse_DCT;
  38.161 +
  38.162 +  /* Loop to process as much as one whole iMCU row */
  38.163 +  for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
  38.164 +       yoffset++) {
  38.165 +    for (MCU_col_num = coef->MCU_ctr; MCU_col_num <= last_MCU_col;
  38.166 +	 MCU_col_num++) {
  38.167 +      /* Try to fetch an MCU.  Entropy decoder expects buffer to be zeroed. */
  38.168 +      jzero_far((void FAR *) coef->MCU_buffer[0],
  38.169 +		(size_t) (cinfo->blocks_in_MCU * SIZEOF(JBLOCK)));
  38.170 +      if (! (*cinfo->entropy->decode_mcu) (cinfo, coef->MCU_buffer)) {
  38.171 +	/* Suspension forced; update state counters and exit */
  38.172 +	coef->MCU_vert_offset = yoffset;
  38.173 +	coef->MCU_ctr = MCU_col_num;
  38.174 +	return JPEG_SUSPENDED;
  38.175 +      }
  38.176 +      /* Determine where data should go in output_buf and do the IDCT thing.
  38.177 +       * We skip dummy blocks at the right and bottom edges (but blkn gets
  38.178 +       * incremented past them!).  Note the inner loop relies on having
  38.179 +       * allocated the MCU_buffer[] blocks sequentially.
  38.180 +       */
  38.181 +      blkn = 0;			/* index of current DCT block within MCU */
  38.182 +      for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  38.183 +	compptr = cinfo->cur_comp_info[ci];
  38.184 +	/* Don't bother to IDCT an uninteresting component. */
  38.185 +	if (! compptr->component_needed) {
  38.186 +	  blkn += compptr->MCU_blocks;
  38.187 +	  continue;
  38.188 +	}
  38.189 +	inverse_DCT = cinfo->idct->inverse_DCT[compptr->component_index];
  38.190 +	useful_width = (MCU_col_num < last_MCU_col) ? compptr->MCU_width
  38.191 +						    : compptr->last_col_width;
  38.192 +	output_ptr = output_buf[compptr->component_index] +
  38.193 +	  yoffset * compptr->DCT_scaled_size;
  38.194 +	start_col = MCU_col_num * compptr->MCU_sample_width;
  38.195 +	for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
  38.196 +	  if (cinfo->input_iMCU_row < last_iMCU_row ||
  38.197 +	      yoffset+yindex < compptr->last_row_height) {
  38.198 +	    output_col = start_col;
  38.199 +	    for (xindex = 0; xindex < useful_width; xindex++) {
  38.200 +	      (*inverse_DCT) (cinfo, compptr,
  38.201 +			      (JCOEFPTR) coef->MCU_buffer[blkn+xindex],
  38.202 +			      output_ptr, output_col);
  38.203 +	      output_col += compptr->DCT_scaled_size;
  38.204 +	    }
  38.205 +	  }
  38.206 +	  blkn += compptr->MCU_width;
  38.207 +	  output_ptr += compptr->DCT_scaled_size;
  38.208 +	}
  38.209 +      }
  38.210 +    }
  38.211 +    /* Completed an MCU row, but perhaps not an iMCU row */
  38.212 +    coef->MCU_ctr = 0;
  38.213 +  }
  38.214 +  /* Completed the iMCU row, advance counters for next one */
  38.215 +  cinfo->output_iMCU_row++;
  38.216 +  if (++(cinfo->input_iMCU_row) < cinfo->total_iMCU_rows) {
  38.217 +    start_iMCU_row(cinfo);
  38.218 +    return JPEG_ROW_COMPLETED;
  38.219 +  }
  38.220 +  /* Completed the scan */
  38.221 +  (*cinfo->inputctl->finish_input_pass) (cinfo);
  38.222 +  return JPEG_SCAN_COMPLETED;
  38.223 +}
  38.224 +
  38.225 +
  38.226 +/*
  38.227 + * Dummy consume-input routine for single-pass operation.
  38.228 + */
  38.229 +
  38.230 +METHODDEF(int)
  38.231 +dummy_consume_data (j_decompress_ptr cinfo)
  38.232 +{
  38.233 +  return JPEG_SUSPENDED;	/* Always indicate nothing was done */
  38.234 +}
  38.235 +
  38.236 +
  38.237 +#ifdef D_MULTISCAN_FILES_SUPPORTED
  38.238 +
  38.239 +/*
  38.240 + * Consume input data and store it in the full-image coefficient buffer.
  38.241 + * We read as much as one fully interleaved MCU row ("iMCU" row) per call,
  38.242 + * ie, v_samp_factor block rows for each component in the scan.
  38.243 + * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
  38.244 + */
  38.245 +
  38.246 +METHODDEF(int)
  38.247 +consume_data (j_decompress_ptr cinfo)
  38.248 +{
  38.249 +  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
  38.250 +  JDIMENSION MCU_col_num;	/* index of current MCU within row */
  38.251 +  int blkn, ci, xindex, yindex, yoffset;
  38.252 +  JDIMENSION start_col;
  38.253 +  JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];
  38.254 +  JBLOCKROW buffer_ptr;
  38.255 +  jpeg_component_info *compptr;
  38.256 +
  38.257 +  /* Align the virtual buffers for the components used in this scan. */
  38.258 +  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  38.259 +    compptr = cinfo->cur_comp_info[ci];
  38.260 +    buffer[ci] = (*cinfo->mem->access_virt_barray)
  38.261 +      ((j_common_ptr) cinfo, coef->whole_image[compptr->component_index],
  38.262 +       cinfo->input_iMCU_row * compptr->v_samp_factor,
  38.263 +       (JDIMENSION) compptr->v_samp_factor, TRUE);
  38.264 +    /* Note: entropy decoder expects buffer to be zeroed,
  38.265 +     * but this is handled automatically by the memory manager
  38.266 +     * because we requested a pre-zeroed array.
  38.267 +     */
  38.268 +  }
  38.269 +
  38.270 +  /* Loop to process one whole iMCU row */
  38.271 +  for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
  38.272 +       yoffset++) {
  38.273 +    for (MCU_col_num = coef->MCU_ctr; MCU_col_num < cinfo->MCUs_per_row;
  38.274 +	 MCU_col_num++) {
  38.275 +      /* Construct list of pointers to DCT blocks belonging to this MCU */
  38.276 +      blkn = 0;			/* index of current DCT block within MCU */
  38.277 +      for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  38.278 +	compptr = cinfo->cur_comp_info[ci];
  38.279 +	start_col = MCU_col_num * compptr->MCU_width;
  38.280 +	for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
  38.281 +	  buffer_ptr = buffer[ci][yindex+yoffset] + start_col;
  38.282 +	  for (xindex = 0; xindex < compptr->MCU_width; xindex++) {
  38.283 +	    coef->MCU_buffer[blkn++] = buffer_ptr++;
  38.284 +	  }
  38.285 +	}
  38.286 +      }
  38.287 +      /* Try to fetch the MCU. */
  38.288 +      if (! (*cinfo->entropy->decode_mcu) (cinfo, coef->MCU_buffer)) {
  38.289 +	/* Suspension forced; update state counters and exit */
  38.290 +	coef->MCU_vert_offset = yoffset;
  38.291 +	coef->MCU_ctr = MCU_col_num;
  38.292 +	return JPEG_SUSPENDED;
  38.293 +      }
  38.294 +    }
  38.295 +    /* Completed an MCU row, but perhaps not an iMCU row */
  38.296 +    coef->MCU_ctr = 0;
  38.297 +  }
  38.298 +  /* Completed the iMCU row, advance counters for next one */
  38.299 +  if (++(cinfo->input_iMCU_row) < cinfo->total_iMCU_rows) {
  38.300 +    start_iMCU_row(cinfo);
  38.301 +    return JPEG_ROW_COMPLETED;
  38.302 +  }
  38.303 +  /* Completed the scan */
  38.304 +  (*cinfo->inputctl->finish_input_pass) (cinfo);
  38.305 +  return JPEG_SCAN_COMPLETED;
  38.306 +}
  38.307 +
  38.308 +
  38.309 +/*
  38.310 + * Decompress and return some data in the multi-pass case.
  38.311 + * Always attempts to emit one fully interleaved MCU row ("iMCU" row).
  38.312 + * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
  38.313 + *
  38.314 + * NB: output_buf contains a plane for each component in image.
  38.315 + */
  38.316 +
  38.317 +METHODDEF(int)
  38.318 +decompress_data (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
  38.319 +{
  38.320 +  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
  38.321 +  JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
  38.322 +  JDIMENSION block_num;
  38.323 +  int ci, block_row, block_rows;
  38.324 +  JBLOCKARRAY buffer;
  38.325 +  JBLOCKROW buffer_ptr;
  38.326 +  JSAMPARRAY output_ptr;
  38.327 +  JDIMENSION output_col;
  38.328 +  jpeg_component_info *compptr;
  38.329 +  inverse_DCT_method_ptr inverse_DCT;
  38.330 +
  38.331 +  /* Force some input to be done if we are getting ahead of the input. */
  38.332 +  while (cinfo->input_scan_number < cinfo->output_scan_number ||
  38.333 +	 (cinfo->input_scan_number == cinfo->output_scan_number &&
  38.334 +	  cinfo->input_iMCU_row <= cinfo->output_iMCU_row)) {
  38.335 +    if ((*cinfo->inputctl->consume_input)(cinfo) == JPEG_SUSPENDED)
  38.336 +      return JPEG_SUSPENDED;
  38.337 +  }
  38.338 +
  38.339 +  /* OK, output from the virtual arrays. */
  38.340 +  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  38.341 +       ci++, compptr++) {
  38.342 +    /* Don't bother to IDCT an uninteresting component. */
  38.343 +    if (! compptr->component_needed)
  38.344 +      continue;
  38.345 +    /* Align the virtual buffer for this component. */
  38.346 +    buffer = (*cinfo->mem->access_virt_barray)
  38.347 +      ((j_common_ptr) cinfo, coef->whole_image[ci],
  38.348 +       cinfo->output_iMCU_row * compptr->v_samp_factor,
  38.349 +       (JDIMENSION) compptr->v_samp_factor, FALSE);
  38.350 +    /* Count non-dummy DCT block rows in this iMCU row. */
  38.351 +    if (cinfo->output_iMCU_row < last_iMCU_row)
  38.352 +      block_rows = compptr->v_samp_factor;
  38.353 +    else {
  38.354 +      /* NB: can't use last_row_height here; it is input-side-dependent! */
  38.355 +      block_rows = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
  38.356 +      if (block_rows == 0) block_rows = compptr->v_samp_factor;
  38.357 +    }
  38.358 +    inverse_DCT = cinfo->idct->inverse_DCT[ci];
  38.359 +    output_ptr = output_buf[ci];
  38.360 +    /* Loop over all DCT blocks to be processed. */
  38.361 +    for (block_row = 0; block_row < block_rows; block_row++) {
  38.362 +      buffer_ptr = buffer[block_row];
  38.363 +      output_col = 0;
  38.364 +      for (block_num = 0; block_num < compptr->width_in_blocks; block_num++) {
  38.365 +	(*inverse_DCT) (cinfo, compptr, (JCOEFPTR) buffer_ptr,
  38.366 +			output_ptr, output_col);
  38.367 +	buffer_ptr++;
  38.368 +	output_col += compptr->DCT_scaled_size;
  38.369 +      }
  38.370 +      output_ptr += compptr->DCT_scaled_size;
  38.371 +    }
  38.372 +  }
  38.373 +
  38.374 +  if (++(cinfo->output_iMCU_row) < cinfo->total_iMCU_rows)
  38.375 +    return JPEG_ROW_COMPLETED;
  38.376 +  return JPEG_SCAN_COMPLETED;
  38.377 +}
  38.378 +
  38.379 +#endif /* D_MULTISCAN_FILES_SUPPORTED */
  38.380 +
  38.381 +
  38.382 +#ifdef BLOCK_SMOOTHING_SUPPORTED
  38.383 +
  38.384 +/*
  38.385 + * This code applies interblock smoothing as described by section K.8
  38.386 + * of the JPEG standard: the first 5 AC coefficients are estimated from
  38.387 + * the DC values of a DCT block and its 8 neighboring blocks.
  38.388 + * We apply smoothing only for progressive JPEG decoding, and only if
  38.389 + * the coefficients it can estimate are not yet known to full precision.
  38.390 + */
  38.391 +
  38.392 +/* Natural-order array positions of the first 5 zigzag-order coefficients */
  38.393 +#define Q01_POS  1
  38.394 +#define Q10_POS  8
  38.395 +#define Q20_POS  16
  38.396 +#define Q11_POS  9
  38.397 +#define Q02_POS  2
  38.398 +
  38.399 +/*
  38.400 + * Determine whether block smoothing is applicable and safe.
  38.401 + * We also latch the current states of the coef_bits[] entries for the
  38.402 + * AC coefficients; otherwise, if the input side of the decompressor
  38.403 + * advances into a new scan, we might think the coefficients are known
  38.404 + * more accurately than they really are.
  38.405 + */
  38.406 +
  38.407 +LOCAL(boolean)
  38.408 +smoothing_ok (j_decompress_ptr cinfo)
  38.409 +{
  38.410 +  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
  38.411 +  boolean smoothing_useful = FALSE;
  38.412 +  int ci, coefi;
  38.413 +  jpeg_component_info *compptr;
  38.414 +  JQUANT_TBL * qtable;
  38.415 +  int * coef_bits;
  38.416 +  int * coef_bits_latch;
  38.417 +
  38.418 +  if (! cinfo->progressive_mode || cinfo->coef_bits == NULL)
  38.419 +    return FALSE;
  38.420 +
  38.421 +  /* Allocate latch area if not already done */
  38.422 +  if (coef->coef_bits_latch == NULL)
  38.423 +    coef->coef_bits_latch = (int *)
  38.424 +      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  38.425 +				  cinfo->num_components *
  38.426 +				  (SAVED_COEFS * SIZEOF(int)));
  38.427 +  coef_bits_latch = coef->coef_bits_latch;
  38.428 +
  38.429 +  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  38.430 +       ci++, compptr++) {
  38.431 +    /* All components' quantization values must already be latched. */
  38.432 +    if ((qtable = compptr->quant_table) == NULL)
  38.433 +      return FALSE;
  38.434 +    /* Verify DC & first 5 AC quantizers are nonzero to avoid zero-divide. */
  38.435 +    if (qtable->quantval[0] == 0 ||
  38.436 +	qtable->quantval[Q01_POS] == 0 ||
  38.437 +	qtable->quantval[Q10_POS] == 0 ||
  38.438 +	qtable->quantval[Q20_POS] == 0 ||
  38.439 +	qtable->quantval[Q11_POS] == 0 ||
  38.440 +	qtable->quantval[Q02_POS] == 0)
  38.441 +      return FALSE;
  38.442 +    /* DC values must be at least partly known for all components. */
  38.443 +    coef_bits = cinfo->coef_bits[ci];
  38.444 +    if (coef_bits[0] < 0)
  38.445 +      return FALSE;
  38.446 +    /* Block smoothing is helpful if some AC coefficients remain inaccurate. */
  38.447 +    for (coefi = 1; coefi <= 5; coefi++) {
  38.448 +      coef_bits_latch[coefi] = coef_bits[coefi];
  38.449 +      if (coef_bits[coefi] != 0)
  38.450 +	smoothing_useful = TRUE;
  38.451 +    }
  38.452 +    coef_bits_latch += SAVED_COEFS;
  38.453 +  }
  38.454 +
  38.455 +  return smoothing_useful;
  38.456 +}
  38.457 +
  38.458 +
  38.459 +/*
  38.460 + * Variant of decompress_data for use when doing block smoothing.
  38.461 + */
  38.462 +
  38.463 +METHODDEF(int)
  38.464 +decompress_smooth_data (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
  38.465 +{
  38.466 +  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
  38.467 +  JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
  38.468 +  JDIMENSION block_num, last_block_column;
  38.469 +  int ci, block_row, block_rows, access_rows;
  38.470 +  JBLOCKARRAY buffer;
  38.471 +  JBLOCKROW buffer_ptr, prev_block_row, next_block_row;
  38.472 +  JSAMPARRAY output_ptr;
  38.473 +  JDIMENSION output_col;
  38.474 +  jpeg_component_info *compptr;
  38.475 +  inverse_DCT_method_ptr inverse_DCT;
  38.476 +  boolean first_row, last_row;
  38.477 +  JBLOCK workspace;
  38.478 +  int *coef_bits;
  38.479 +  JQUANT_TBL *quanttbl;
  38.480 +  INT32 Q00,Q01,Q02,Q10,Q11,Q20, num;
  38.481 +  int DC1,DC2,DC3,DC4,DC5,DC6,DC7,DC8,DC9;
  38.482 +  int Al, pred;
  38.483 +
  38.484 +  /* Force some input to be done if we are getting ahead of the input. */
  38.485 +  while (cinfo->input_scan_number <= cinfo->output_scan_number &&
  38.486 +	 ! cinfo->inputctl->eoi_reached) {
  38.487 +    if (cinfo->input_scan_number == cinfo->output_scan_number) {
  38.488 +      /* If input is working on current scan, we ordinarily want it to
  38.489 +       * have completed the current row.  But if input scan is DC,
  38.490 +       * we want it to keep one row ahead so that next block row's DC
  38.491 +       * values are up to date.
  38.492 +       */
  38.493 +      JDIMENSION delta = (cinfo->Ss == 0) ? 1 : 0;
  38.494 +      if (cinfo->input_iMCU_row > cinfo->output_iMCU_row+delta)
  38.495 +	break;
  38.496 +    }
  38.497 +    if ((*cinfo->inputctl->consume_input)(cinfo) == JPEG_SUSPENDED)
  38.498 +      return JPEG_SUSPENDED;
  38.499 +  }
  38.500 +
  38.501 +  /* OK, output from the virtual arrays. */
  38.502 +  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  38.503 +       ci++, compptr++) {
  38.504 +    /* Don't bother to IDCT an uninteresting component. */
  38.505 +    if (! compptr->component_needed)
  38.506 +      continue;
  38.507 +    /* Count non-dummy DCT block rows in this iMCU row. */
  38.508 +    if (cinfo->output_iMCU_row < last_iMCU_row) {
  38.509 +      block_rows = compptr->v_samp_factor;
  38.510 +      access_rows = block_rows * 2; /* this and next iMCU row */
  38.511 +      last_row = FALSE;
  38.512 +    } else {
  38.513 +      /* NB: can't use last_row_height here; it is input-side-dependent! */
  38.514 +      block_rows = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
  38.515 +      if (block_rows == 0) block_rows = compptr->v_samp_factor;
  38.516 +      access_rows = block_rows; /* this iMCU row only */
  38.517 +      last_row = TRUE;
  38.518 +    }
  38.519 +    /* Align the virtual buffer for this component. */
  38.520 +    if (cinfo->output_iMCU_row > 0) {
  38.521 +      access_rows += compptr->v_samp_factor; /* prior iMCU row too */
  38.522 +      buffer = (*cinfo->mem->access_virt_barray)
  38.523 +	((j_common_ptr) cinfo, coef->whole_image[ci],
  38.524 +	 (cinfo->output_iMCU_row - 1) * compptr->v_samp_factor,
  38.525 +	 (JDIMENSION) access_rows, FALSE);
  38.526 +      buffer += compptr->v_samp_factor;	/* point to current iMCU row */
  38.527 +      first_row = FALSE;
  38.528 +    } else {
  38.529 +      buffer = (*cinfo->mem->access_virt_barray)
  38.530 +	((j_common_ptr) cinfo, coef->whole_image[ci],
  38.531 +	 (JDIMENSION) 0, (JDIMENSION) access_rows, FALSE);
  38.532 +      first_row = TRUE;
  38.533 +    }
  38.534 +    /* Fetch component-dependent info */
  38.535 +    coef_bits = coef->coef_bits_latch + (ci * SAVED_COEFS);
  38.536 +    quanttbl = compptr->quant_table;
  38.537 +    Q00 = quanttbl->quantval[0];
  38.538 +    Q01 = quanttbl->quantval[Q01_POS];
  38.539 +    Q10 = quanttbl->quantval[Q10_POS];
  38.540 +    Q20 = quanttbl->quantval[Q20_POS];
  38.541 +    Q11 = quanttbl->quantval[Q11_POS];
  38.542 +    Q02 = quanttbl->quantval[Q02_POS];
  38.543 +    inverse_DCT = cinfo->idct->inverse_DCT[ci];
  38.544 +    output_ptr = output_buf[ci];
  38.545 +    /* Loop over all DCT blocks to be processed. */
  38.546 +    for (block_row = 0; block_row < block_rows; block_row++) {
  38.547 +      buffer_ptr = buffer[block_row];
  38.548 +      if (first_row && block_row == 0)
  38.549 +	prev_block_row = buffer_ptr;
  38.550 +      else
  38.551 +	prev_block_row = buffer[block_row-1];
  38.552 +      if (last_row && block_row == block_rows-1)
  38.553 +	next_block_row = buffer_ptr;
  38.554 +      else
  38.555 +	next_block_row = buffer[block_row+1];
  38.556 +      /* We fetch the surrounding DC values using a sliding-register approach.
  38.557 +       * Initialize all nine here so as to do the right thing on narrow pics.
  38.558 +       */
  38.559 +      DC1 = DC2 = DC3 = (int) prev_block_row[0][0];
  38.560 +      DC4 = DC5 = DC6 = (int) buffer_ptr[0][0];
  38.561 +      DC7 = DC8 = DC9 = (int) next_block_row[0][0];
  38.562 +      output_col = 0;
  38.563 +      last_block_column = compptr->width_in_blocks - 1;
  38.564 +      for (block_num = 0; block_num <= last_block_column; block_num++) {
  38.565 +	/* Fetch current DCT block into workspace so we can modify it. */
  38.566 +	jcopy_block_row(buffer_ptr, (JBLOCKROW) workspace, (JDIMENSION) 1);
  38.567 +	/* Update DC values */
  38.568 +	if (block_num < last_block_column) {
  38.569 +	  DC3 = (int) prev_block_row[1][0];
  38.570 +	  DC6 = (int) buffer_ptr[1][0];
  38.571 +	  DC9 = (int) next_block_row[1][0];
  38.572 +	}
  38.573 +	/* Compute coefficient estimates per K.8.
  38.574 +	 * An estimate is applied only if coefficient is still zero,
  38.575 +	 * and is not known to be fully accurate.
  38.576 +	 */
  38.577 +	/* AC01 */
  38.578 +	if ((Al=coef_bits[1]) != 0 && workspace[1] == 0) {
  38.579 +	  num = 36 * Q00 * (DC4 - DC6);
  38.580 +	  if (num >= 0) {
  38.581 +	    pred = (int) (((Q01<<7) + num) / (Q01<<8));
  38.582 +	    if (Al > 0 && pred >= (1<<Al))
  38.583 +	      pred = (1<<Al)-1;
  38.584 +	  } else {
  38.585 +	    pred = (int) (((Q01<<7) - num) / (Q01<<8));
  38.586 +	    if (Al > 0 && pred >= (1<<Al))
  38.587 +	      pred = (1<<Al)-1;
  38.588 +	    pred = -pred;
  38.589 +	  }
  38.590 +	  workspace[1] = (JCOEF) pred;
  38.591 +	}
  38.592 +	/* AC10 */
  38.593 +	if ((Al=coef_bits[2]) != 0 && workspace[8] == 0) {
  38.594 +	  num = 36 * Q00 * (DC2 - DC8);
  38.595 +	  if (num >= 0) {
  38.596 +	    pred = (int) (((Q10<<7) + num) / (Q10<<8));
  38.597 +	    if (Al > 0 && pred >= (1<<Al))
  38.598 +	      pred = (1<<Al)-1;
  38.599 +	  } else {
  38.600 +	    pred = (int) (((Q10<<7) - num) / (Q10<<8));
  38.601 +	    if (Al > 0 && pred >= (1<<Al))
  38.602 +	      pred = (1<<Al)-1;
  38.603 +	    pred = -pred;
  38.604 +	  }
  38.605 +	  workspace[8] = (JCOEF) pred;
  38.606 +	}
  38.607 +	/* AC20 */
  38.608 +	if ((Al=coef_bits[3]) != 0 && workspace[16] == 0) {
  38.609 +	  num = 9 * Q00 * (DC2 + DC8 - 2*DC5);
  38.610 +	  if (num >= 0) {
  38.611 +	    pred = (int) (((Q20<<7) + num) / (Q20<<8));
  38.612 +	    if (Al > 0 && pred >= (1<<Al))
  38.613 +	      pred = (1<<Al)-1;
  38.614 +	  } else {
  38.615 +	    pred = (int) (((Q20<<7) - num) / (Q20<<8));
  38.616 +	    if (Al > 0 && pred >= (1<<Al))
  38.617 +	      pred = (1<<Al)-1;
  38.618 +	    pred = -pred;
  38.619 +	  }
  38.620 +	  workspace[16] = (JCOEF) pred;
  38.621 +	}
  38.622 +	/* AC11 */
  38.623 +	if ((Al=coef_bits[4]) != 0 && workspace[9] == 0) {
  38.624 +	  num = 5 * Q00 * (DC1 - DC3 - DC7 + DC9);
  38.625 +	  if (num >= 0) {
  38.626 +	    pred = (int) (((Q11<<7) + num) / (Q11<<8));
  38.627 +	    if (Al > 0 && pred >= (1<<Al))
  38.628 +	      pred = (1<<Al)-1;
  38.629 +	  } else {
  38.630 +	    pred = (int) (((Q11<<7) - num) / (Q11<<8));
  38.631 +	    if (Al > 0 && pred >= (1<<Al))
  38.632 +	      pred = (1<<Al)-1;
  38.633 +	    pred = -pred;
  38.634 +	  }
  38.635 +	  workspace[9] = (JCOEF) pred;
  38.636 +	}
  38.637 +	/* AC02 */
  38.638 +	if ((Al=coef_bits[5]) != 0 && workspace[2] == 0) {
  38.639 +	  num = 9 * Q00 * (DC4 + DC6 - 2*DC5);
  38.640 +	  if (num >= 0) {
  38.641 +	    pred = (int) (((Q02<<7) + num) / (Q02<<8));
  38.642 +	    if (Al > 0 && pred >= (1<<Al))
  38.643 +	      pred = (1<<Al)-1;
  38.644 +	  } else {
  38.645 +	    pred = (int) (((Q02<<7) - num) / (Q02<<8));
  38.646 +	    if (Al > 0 && pred >= (1<<Al))
  38.647 +	      pred = (1<<Al)-1;
  38.648 +	    pred = -pred;
  38.649 +	  }
  38.650 +	  workspace[2] = (JCOEF) pred;
  38.651 +	}
  38.652 +	/* OK, do the IDCT */
  38.653 +	(*inverse_DCT) (cinfo, compptr, (JCOEFPTR) workspace,
  38.654 +			output_ptr, output_col);
  38.655 +	/* Advance for next column */
  38.656 +	DC1 = DC2; DC2 = DC3;
  38.657 +	DC4 = DC5; DC5 = DC6;
  38.658 +	DC7 = DC8; DC8 = DC9;
  38.659 +	buffer_ptr++, prev_block_row++, next_block_row++;
  38.660 +	output_col += compptr->DCT_scaled_size;
  38.661 +      }
  38.662 +      output_ptr += compptr->DCT_scaled_size;
  38.663 +    }
  38.664 +  }
  38.665 +
  38.666 +  if (++(cinfo->output_iMCU_row) < cinfo->total_iMCU_rows)
  38.667 +    return JPEG_ROW_COMPLETED;
  38.668 +  return JPEG_SCAN_COMPLETED;
  38.669 +}
  38.670 +
  38.671 +#endif /* BLOCK_SMOOTHING_SUPPORTED */
  38.672 +
  38.673 +
  38.674 +/*
  38.675 + * Initialize coefficient buffer controller.
  38.676 + */
  38.677 +
  38.678 +GLOBAL(void)
  38.679 +jinit_d_coef_controller (j_decompress_ptr cinfo, boolean need_full_buffer)
  38.680 +{
  38.681 +  my_coef_ptr coef;
  38.682 +
  38.683 +  coef = (my_coef_ptr)
  38.684 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  38.685 +				SIZEOF(my_coef_controller));
  38.686 +  cinfo->coef = (struct jpeg_d_coef_controller *) coef;
  38.687 +  coef->pub.start_input_pass = start_input_pass;
  38.688 +  coef->pub.start_output_pass = start_output_pass;
  38.689 +#ifdef BLOCK_SMOOTHING_SUPPORTED
  38.690 +  coef->coef_bits_latch = NULL;
  38.691 +#endif
  38.692 +
  38.693 +  /* Create the coefficient buffer. */
  38.694 +  if (need_full_buffer) {
  38.695 +#ifdef D_MULTISCAN_FILES_SUPPORTED
  38.696 +    /* Allocate a full-image virtual array for each component, */
  38.697 +    /* padded to a multiple of samp_factor DCT blocks in each direction. */
  38.698 +    /* Note we ask for a pre-zeroed array. */
  38.699 +    int ci, access_rows;
  38.700 +    jpeg_component_info *compptr;
  38.701 +
  38.702 +    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  38.703 +	 ci++, compptr++) {
  38.704 +      access_rows = compptr->v_samp_factor;
  38.705 +#ifdef BLOCK_SMOOTHING_SUPPORTED
  38.706 +      /* If block smoothing could be used, need a bigger window */
  38.707 +      if (cinfo->progressive_mode)
  38.708 +	access_rows *= 3;
  38.709 +#endif
  38.710 +      coef->whole_image[ci] = (*cinfo->mem->request_virt_barray)
  38.711 +	((j_common_ptr) cinfo, JPOOL_IMAGE, TRUE,
  38.712 +	 (JDIMENSION) jround_up((long) compptr->width_in_blocks,
  38.713 +				(long) compptr->h_samp_factor),
  38.714 +	 (JDIMENSION) jround_up((long) compptr->height_in_blocks,
  38.715 +				(long) compptr->v_samp_factor),
  38.716 +	 (JDIMENSION) access_rows);
  38.717 +    }
  38.718 +    coef->pub.consume_data = consume_data;
  38.719 +    coef->pub.decompress_data = decompress_data;
  38.720 +    coef->pub.coef_arrays = coef->whole_image; /* link to virtual arrays */
  38.721 +#else
  38.722 +    ERREXIT(cinfo, JERR_NOT_COMPILED);
  38.723 +#endif
  38.724 +  } else {
  38.725 +    /* We only need a single-MCU buffer. */
  38.726 +    JBLOCKROW buffer;
  38.727 +    int i;
  38.728 +
  38.729 +    buffer = (JBLOCKROW)
  38.730 +      (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  38.731 +				  D_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK));
  38.732 +    for (i = 0; i < D_MAX_BLOCKS_IN_MCU; i++) {
  38.733 +      coef->MCU_buffer[i] = buffer + i;
  38.734 +    }
  38.735 +    coef->pub.consume_data = dummy_consume_data;
  38.736 +    coef->pub.decompress_data = decompress_onepass;
  38.737 +    coef->pub.coef_arrays = NULL; /* flag for no virtual arrays */
  38.738 +  }
  38.739 +}
    39.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    39.2 +++ b/libs/libjpeg/jdcolor.c	Sat Sep 19 05:51:51 2015 +0300
    39.3 @@ -0,0 +1,396 @@
    39.4 +/*
    39.5 + * jdcolor.c
    39.6 + *
    39.7 + * Copyright (C) 1991-1997, Thomas G. Lane.
    39.8 + * This file is part of the Independent JPEG Group's software.
    39.9 + * For conditions of distribution and use, see the accompanying README file.
   39.10 + *
   39.11 + * This file contains output colorspace conversion routines.
   39.12 + */
   39.13 +
   39.14 +#define JPEG_INTERNALS
   39.15 +#include "jinclude.h"
   39.16 +#include "jpeglib.h"
   39.17 +
   39.18 +
   39.19 +/* Private subobject */
   39.20 +
   39.21 +typedef struct {
   39.22 +  struct jpeg_color_deconverter pub; /* public fields */
   39.23 +
   39.24 +  /* Private state for YCC->RGB conversion */
   39.25 +  int * Cr_r_tab;		/* => table for Cr to R conversion */
   39.26 +  int * Cb_b_tab;		/* => table for Cb to B conversion */
   39.27 +  INT32 * Cr_g_tab;		/* => table for Cr to G conversion */
   39.28 +  INT32 * Cb_g_tab;		/* => table for Cb to G conversion */
   39.29 +} my_color_deconverter;
   39.30 +
   39.31 +typedef my_color_deconverter * my_cconvert_ptr;
   39.32 +
   39.33 +
   39.34 +/**************** YCbCr -> RGB conversion: most common case **************/
   39.35 +
   39.36 +/*
   39.37 + * YCbCr is defined per CCIR 601-1, except that Cb and Cr are
   39.38 + * normalized to the range 0..MAXJSAMPLE rather than -0.5 .. 0.5.
   39.39 + * The conversion equations to be implemented are therefore
   39.40 + *	R = Y                + 1.40200 * Cr
   39.41 + *	G = Y - 0.34414 * Cb - 0.71414 * Cr
   39.42 + *	B = Y + 1.77200 * Cb
   39.43 + * where Cb and Cr represent the incoming values less CENTERJSAMPLE.
   39.44 + * (These numbers are derived from TIFF 6.0 section 21, dated 3-June-92.)
   39.45 + *
   39.46 + * To avoid floating-point arithmetic, we represent the fractional constants
   39.47 + * as integers scaled up by 2^16 (about 4 digits precision); we have to divide
   39.48 + * the products by 2^16, with appropriate rounding, to get the correct answer.
   39.49 + * Notice that Y, being an integral input, does not contribute any fraction
   39.50 + * so it need not participate in the rounding.
   39.51 + *
   39.52 + * For even more speed, we avoid doing any multiplications in the inner loop
   39.53 + * by precalculating the constants times Cb and Cr for all possible values.
   39.54 + * For 8-bit JSAMPLEs this is very reasonable (only 256 entries per table);
   39.55 + * for 12-bit samples it is still acceptable.  It's not very reasonable for
   39.56 + * 16-bit samples, but if you want lossless storage you shouldn't be changing
   39.57 + * colorspace anyway.
   39.58 + * The Cr=>R and Cb=>B values can be rounded to integers in advance; the
   39.59 + * values for the G calculation are left scaled up, since we must add them
   39.60 + * together before rounding.
   39.61 + */
   39.62 +
   39.63 +#define SCALEBITS	16	/* speediest right-shift on some machines */
   39.64 +#define ONE_HALF	((INT32) 1 << (SCALEBITS-1))
   39.65 +#define FIX(x)		((INT32) ((x) * (1L<<SCALEBITS) + 0.5))
   39.66 +
   39.67 +
   39.68 +/*
   39.69 + * Initialize tables for YCC->RGB colorspace conversion.
   39.70 + */
   39.71 +
   39.72 +LOCAL(void)
   39.73 +build_ycc_rgb_table (j_decompress_ptr cinfo)
   39.74 +{
   39.75 +  my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
   39.76 +  int i;
   39.77 +  INT32 x;
   39.78 +  SHIFT_TEMPS
   39.79 +
   39.80 +  cconvert->Cr_r_tab = (int *)
   39.81 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
   39.82 +				(MAXJSAMPLE+1) * SIZEOF(int));
   39.83 +  cconvert->Cb_b_tab = (int *)
   39.84 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
   39.85 +				(MAXJSAMPLE+1) * SIZEOF(int));
   39.86 +  cconvert->Cr_g_tab = (INT32 *)
   39.87 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
   39.88 +				(MAXJSAMPLE+1) * SIZEOF(INT32));
   39.89 +  cconvert->Cb_g_tab = (INT32 *)
   39.90 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
   39.91 +				(MAXJSAMPLE+1) * SIZEOF(INT32));
   39.92 +
   39.93 +  for (i = 0, x = -CENTERJSAMPLE; i <= MAXJSAMPLE; i++, x++) {
   39.94 +    /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */
   39.95 +    /* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */
   39.96 +    /* Cr=>R value is nearest int to 1.40200 * x */
   39.97 +    cconvert->Cr_r_tab[i] = (int)
   39.98 +		    RIGHT_SHIFT(FIX(1.40200) * x + ONE_HALF, SCALEBITS);
   39.99 +    /* Cb=>B value is nearest int to 1.77200 * x */
  39.100 +    cconvert->Cb_b_tab[i] = (int)
  39.101 +		    RIGHT_SHIFT(FIX(1.77200) * x + ONE_HALF, SCALEBITS);
  39.102 +    /* Cr=>G value is scaled-up -0.71414 * x */
  39.103 +    cconvert->Cr_g_tab[i] = (- FIX(0.71414)) * x;
  39.104 +    /* Cb=>G value is scaled-up -0.34414 * x */
  39.105 +    /* We also add in ONE_HALF so that need not do it in inner loop */
  39.106 +    cconvert->Cb_g_tab[i] = (- FIX(0.34414)) * x + ONE_HALF;
  39.107 +  }
  39.108 +}
  39.109 +
  39.110 +
  39.111 +/*
  39.112 + * Convert some rows of samples to the output colorspace.
  39.113 + *
  39.114 + * Note that we change from noninterleaved, one-plane-per-component format
  39.115 + * to interleaved-pixel format.  The output buffer is therefore three times
  39.116 + * as wide as the input buffer.
  39.117 + * A starting row offset is provided only for the input buffer.  The caller
  39.118 + * can easily adjust the passed output_buf value to accommodate any row
  39.119 + * offset required on that side.
  39.120 + */
  39.121 +
  39.122 +METHODDEF(void)
  39.123 +ycc_rgb_convert (j_decompress_ptr cinfo,
  39.124 +		 JSAMPIMAGE input_buf, JDIMENSION input_row,
  39.125 +		 JSAMPARRAY output_buf, int num_rows)
  39.126 +{
  39.127 +  my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
  39.128 +  register int y, cb, cr;
  39.129 +  register JSAMPROW outptr;
  39.130 +  register JSAMPROW inptr0, inptr1, inptr2;
  39.131 +  register JDIMENSION col;
  39.132 +  JDIMENSION num_cols = cinfo->output_width;
  39.133 +  /* copy these pointers into registers if possible */
  39.134 +  register JSAMPLE * range_limit = cinfo->sample_range_limit;
  39.135 +  register int * Crrtab = cconvert->Cr_r_tab;
  39.136 +  register int * Cbbtab = cconvert->Cb_b_tab;
  39.137 +  register INT32 * Crgtab = cconvert->Cr_g_tab;
  39.138 +  register INT32 * Cbgtab = cconvert->Cb_g_tab;
  39.139 +  SHIFT_TEMPS
  39.140 +
  39.141 +  while (--num_rows >= 0) {
  39.142 +    inptr0 = input_buf[0][input_row];
  39.143 +    inptr1 = input_buf[1][input_row];
  39.144 +    inptr2 = input_buf[2][input_row];
  39.145 +    input_row++;
  39.146 +    outptr = *output_buf++;
  39.147 +    for (col = 0; col < num_cols; col++) {
  39.148 +      y  = GETJSAMPLE(inptr0[col]);
  39.149 +      cb = GETJSAMPLE(inptr1[col]);
  39.150 +      cr = GETJSAMPLE(inptr2[col]);
  39.151 +      /* Range-limiting is essential due to noise introduced by DCT losses. */
  39.152 +      outptr[RGB_RED] =   range_limit[y + Crrtab[cr]];
  39.153 +      outptr[RGB_GREEN] = range_limit[y +
  39.154 +			      ((int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr],
  39.155 +						 SCALEBITS))];
  39.156 +      outptr[RGB_BLUE] =  range_limit[y + Cbbtab[cb]];
  39.157 +      outptr += RGB_PIXELSIZE;
  39.158 +    }
  39.159 +  }
  39.160 +}
  39.161 +
  39.162 +
  39.163 +/**************** Cases other than YCbCr -> RGB **************/
  39.164 +
  39.165 +
  39.166 +/*
  39.167 + * Color conversion for no colorspace change: just copy the data,
  39.168 + * converting from separate-planes to interleaved representation.
  39.169 + */
  39.170 +
  39.171 +METHODDEF(void)
  39.172 +null_convert (j_decompress_ptr cinfo,
  39.173 +	      JSAMPIMAGE input_buf, JDIMENSION input_row,
  39.174 +	      JSAMPARRAY output_buf, int num_rows)
  39.175 +{
  39.176 +  register JSAMPROW inptr, outptr;
  39.177 +  register JDIMENSION count;
  39.178 +  register int num_components = cinfo->num_components;
  39.179 +  JDIMENSION num_cols = cinfo->output_width;
  39.180 +  int ci;
  39.181 +
  39.182 +  while (--num_rows >= 0) {
  39.183 +    for (ci = 0; ci < num_components; ci++) {
  39.184 +      inptr = input_buf[ci][input_row];
  39.185 +      outptr = output_buf[0] + ci;
  39.186 +      for (count = num_cols; count > 0; count--) {
  39.187 +	*outptr = *inptr++;	/* needn't bother with GETJSAMPLE() here */
  39.188 +	outptr += num_components;
  39.189 +      }
  39.190 +    }
  39.191 +    input_row++;
  39.192 +    output_buf++;
  39.193 +  }
  39.194 +}
  39.195 +
  39.196 +
  39.197 +/*
  39.198 + * Color conversion for grayscale: just copy the data.
  39.199 + * This also works for YCbCr -> grayscale conversion, in which
  39.200 + * we just copy the Y (luminance) component and ignore chrominance.
  39.201 + */
  39.202 +
  39.203 +METHODDEF(void)
  39.204 +grayscale_convert (j_decompress_ptr cinfo,
  39.205 +		   JSAMPIMAGE input_buf, JDIMENSION input_row,
  39.206 +		   JSAMPARRAY output_buf, int num_rows)
  39.207 +{
  39.208 +  jcopy_sample_rows(input_buf[0], (int) input_row, output_buf, 0,
  39.209 +		    num_rows, cinfo->output_width);
  39.210 +}
  39.211 +
  39.212 +
  39.213 +/*
  39.214 + * Convert grayscale to RGB: just duplicate the graylevel three times.
  39.215 + * This is provided to support applications that don't want to cope
  39.216 + * with grayscale as a separate case.
  39.217 + */
  39.218 +
  39.219 +METHODDEF(void)
  39.220 +gray_rgb_convert (j_decompress_ptr cinfo,
  39.221 +		  JSAMPIMAGE input_buf, JDIMENSION input_row,
  39.222 +		  JSAMPARRAY output_buf, int num_rows)
  39.223 +{
  39.224 +  register JSAMPROW inptr, outptr;
  39.225 +  register JDIMENSION col;
  39.226 +  JDIMENSION num_cols = cinfo->output_width;
  39.227 +
  39.228 +  while (--num_rows >= 0) {
  39.229 +    inptr = input_buf[0][input_row++];
  39.230 +    outptr = *output_buf++;
  39.231 +    for (col = 0; col < num_cols; col++) {
  39.232 +      /* We can dispense with GETJSAMPLE() here */
  39.233 +      outptr[RGB_RED] = outptr[RGB_GREEN] = outptr[RGB_BLUE] = inptr[col];
  39.234 +      outptr += RGB_PIXELSIZE;
  39.235 +    }
  39.236 +  }
  39.237 +}
  39.238 +
  39.239 +
  39.240 +/*
  39.241 + * Adobe-style YCCK->CMYK conversion.
  39.242 + * We convert YCbCr to R=1-C, G=1-M, and B=1-Y using the same
  39.243 + * conversion as above, while passing K (black) unchanged.
  39.244 + * We assume build_ycc_rgb_table has been called.
  39.245 + */
  39.246 +
  39.247 +METHODDEF(void)
  39.248 +ycck_cmyk_convert (j_decompress_ptr cinfo,
  39.249 +		   JSAMPIMAGE input_buf, JDIMENSION input_row,
  39.250 +		   JSAMPARRAY output_buf, int num_rows)
  39.251 +{
  39.252 +  my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
  39.253 +  register int y, cb, cr;
  39.254 +  register JSAMPROW outptr;
  39.255 +  register JSAMPROW inptr0, inptr1, inptr2, inptr3;
  39.256 +  register JDIMENSION col;
  39.257 +  JDIMENSION num_cols = cinfo->output_width;
  39.258 +  /* copy these pointers into registers if possible */
  39.259 +  register JSAMPLE * range_limit = cinfo->sample_range_limit;
  39.260 +  register int * Crrtab = cconvert->Cr_r_tab;
  39.261 +  register int * Cbbtab = cconvert->Cb_b_tab;
  39.262 +  register INT32 * Crgtab = cconvert->Cr_g_tab;
  39.263 +  register INT32 * Cbgtab = cconvert->Cb_g_tab;
  39.264 +  SHIFT_TEMPS
  39.265 +
  39.266 +  while (--num_rows >= 0) {
  39.267 +    inptr0 = input_buf[0][input_row];
  39.268 +    inptr1 = input_buf[1][input_row];
  39.269 +    inptr2 = input_buf[2][input_row];
  39.270 +    inptr3 = input_buf[3][input_row];
  39.271 +    input_row++;
  39.272 +    outptr = *output_buf++;
  39.273 +    for (col = 0; col < num_cols; col++) {
  39.274 +      y  = GETJSAMPLE(inptr0[col]);
  39.275 +      cb = GETJSAMPLE(inptr1[col]);
  39.276 +      cr = GETJSAMPLE(inptr2[col]);
  39.277 +      /* Range-limiting is essential due to noise introduced by DCT losses. */
  39.278 +      outptr[0] = range_limit[MAXJSAMPLE - (y + Crrtab[cr])];	/* red */
  39.279 +      outptr[1] = range_limit[MAXJSAMPLE - (y +			/* green */
  39.280 +			      ((int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr],
  39.281 +						 SCALEBITS)))];
  39.282 +      outptr[2] = range_limit[MAXJSAMPLE - (y + Cbbtab[cb])];	/* blue */
  39.283 +      /* K passes through unchanged */
  39.284 +      outptr[3] = inptr3[col];	/* don't need GETJSAMPLE here */
  39.285 +      outptr += 4;
  39.286 +    }
  39.287 +  }
  39.288 +}
  39.289 +
  39.290 +
  39.291 +/*
  39.292 + * Empty method for start_pass.
  39.293 + */
  39.294 +
  39.295 +METHODDEF(void)
  39.296 +start_pass_dcolor (j_decompress_ptr cinfo)
  39.297 +{
  39.298 +  /* no work needed */
  39.299 +}
  39.300 +
  39.301 +
  39.302 +/*
  39.303 + * Module initialization routine for output colorspace conversion.
  39.304 + */
  39.305 +
  39.306 +GLOBAL(void)
  39.307 +jinit_color_deconverter (j_decompress_ptr cinfo)
  39.308 +{
  39.309 +  my_cconvert_ptr cconvert;
  39.310 +  int ci;
  39.311 +
  39.312 +  cconvert = (my_cconvert_ptr)
  39.313 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  39.314 +				SIZEOF(my_color_deconverter));
  39.315 +  cinfo->cconvert = (struct jpeg_color_deconverter *) cconvert;
  39.316 +  cconvert->pub.start_pass = start_pass_dcolor;
  39.317 +
  39.318 +  /* Make sure num_components agrees with jpeg_color_space */
  39.319 +  switch (cinfo->jpeg_color_space) {
  39.320 +  case JCS_GRAYSCALE:
  39.321 +    if (cinfo->num_components != 1)
  39.322 +      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  39.323 +    break;
  39.324 +
  39.325 +  case JCS_RGB:
  39.326 +  case JCS_YCbCr:
  39.327 +    if (cinfo->num_components != 3)
  39.328 +      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  39.329 +    break;
  39.330 +
  39.331 +  case JCS_CMYK:
  39.332 +  case JCS_YCCK:
  39.333 +    if (cinfo->num_components != 4)
  39.334 +      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  39.335 +    break;
  39.336 +
  39.337 +  default:			/* JCS_UNKNOWN can be anything */
  39.338 +    if (cinfo->num_components < 1)
  39.339 +      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
  39.340 +    break;
  39.341 +  }
  39.342 +
  39.343 +  /* Set out_color_components and conversion method based on requested space.
  39.344 +   * Also clear the component_needed flags for any unused components,
  39.345 +   * so that earlier pipeline stages can avoid useless computation.
  39.346 +   */
  39.347 +
  39.348 +  switch (cinfo->out_color_space) {
  39.349 +  case JCS_GRAYSCALE:
  39.350 +    cinfo->out_color_components = 1;
  39.351 +    if (cinfo->jpeg_color_space == JCS_GRAYSCALE ||
  39.352 +	cinfo->jpeg_color_space == JCS_YCbCr) {
  39.353 +      cconvert->pub.color_convert = grayscale_convert;
  39.354 +      /* For color->grayscale conversion, only the Y (0) component is needed */
  39.355 +      for (ci = 1; ci < cinfo->num_components; ci++)
  39.356 +	cinfo->comp_info[ci].component_needed = FALSE;
  39.357 +    } else
  39.358 +      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  39.359 +    break;
  39.360 +
  39.361 +  case JCS_RGB:
  39.362 +    cinfo->out_color_components = RGB_PIXELSIZE;
  39.363 +    if (cinfo->jpeg_color_space == JCS_YCbCr) {
  39.364 +      cconvert->pub.color_convert = ycc_rgb_convert;
  39.365 +      build_ycc_rgb_table(cinfo);
  39.366 +    } else if (cinfo->jpeg_color_space == JCS_GRAYSCALE) {
  39.367 +      cconvert->pub.color_convert = gray_rgb_convert;
  39.368 +    } else if (cinfo->jpeg_color_space == JCS_RGB && RGB_PIXELSIZE == 3) {
  39.369 +      cconvert->pub.color_convert = null_convert;
  39.370 +    } else
  39.371 +      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  39.372 +    break;
  39.373 +
  39.374 +  case JCS_CMYK:
  39.375 +    cinfo->out_color_components = 4;
  39.376 +    if (cinfo->jpeg_color_space == JCS_YCCK) {
  39.377 +      cconvert->pub.color_convert = ycck_cmyk_convert;
  39.378 +      build_ycc_rgb_table(cinfo);
  39.379 +    } else if (cinfo->jpeg_color_space == JCS_CMYK) {
  39.380 +      cconvert->pub.color_convert = null_convert;
  39.381 +    } else
  39.382 +      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  39.383 +    break;
  39.384 +
  39.385 +  default:
  39.386 +    /* Permit null conversion to same output space */
  39.387 +    if (cinfo->out_color_space == cinfo->jpeg_color_space) {
  39.388 +      cinfo->out_color_components = cinfo->num_components;
  39.389 +      cconvert->pub.color_convert = null_convert;
  39.390 +    } else			/* unsupported non-null conversion */
  39.391 +      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
  39.392 +    break;
  39.393 +  }
  39.394 +
  39.395 +  if (cinfo->quantize_colors)
  39.396 +    cinfo->output_components = 1; /* single colormapped output component */
  39.397 +  else
  39.398 +    cinfo->output_components = cinfo->out_color_components;
  39.399 +}
    40.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    40.2 +++ b/libs/libjpeg/jdct.h	Sat Sep 19 05:51:51 2015 +0300
    40.3 @@ -0,0 +1,176 @@
    40.4 +/*
    40.5 + * jdct.h
    40.6 + *
    40.7 + * Copyright (C) 1994-1996, Thomas G. Lane.
    40.8 + * This file is part of the Independent JPEG Group's software.
    40.9 + * For conditions of distribution and use, see the accompanying README file.
   40.10 + *
   40.11 + * This include file contains common declarations for the forward and
   40.12 + * inverse DCT modules.  These declarations are private to the DCT managers
   40.13 + * (jcdctmgr.c, jddctmgr.c) and the individual DCT algorithms.
   40.14 + * The individual DCT algorithms are kept in separate files to ease 
   40.15 + * machine-dependent tuning (e.g., assembly coding).
   40.16 + */
   40.17 +
   40.18 +
   40.19 +/*
   40.20 + * A forward DCT routine is given a pointer to a work area of type DCTELEM[];
   40.21 + * the DCT is to be performed in-place in that buffer.  Type DCTELEM is int
   40.22 + * for 8-bit samples, INT32 for 12-bit samples.  (NOTE: Floating-point DCT
   40.23 + * implementations use an array of type FAST_FLOAT, instead.)
   40.24 + * The DCT inputs are expected to be signed (range +-CENTERJSAMPLE).
   40.25 + * The DCT outputs are returned scaled up by a factor of 8; they therefore
   40.26 + * have a range of +-8K for 8-bit data, +-128K for 12-bit data.  This
   40.27 + * convention improves accuracy in integer implementations and saves some
   40.28 + * work in floating-point ones.
   40.29 + * Quantization of the output coefficients is done by jcdctmgr.c.
   40.30 + */
   40.31 +
   40.32 +#if BITS_IN_JSAMPLE == 8
   40.33 +typedef int DCTELEM;		/* 16 or 32 bits is fine */
   40.34 +#else
   40.35 +typedef INT32 DCTELEM;		/* must have 32 bits */
   40.36 +#endif
   40.37 +
   40.38 +typedef JMETHOD(void, forward_DCT_method_ptr, (DCTELEM * data));
   40.39 +typedef JMETHOD(void, float_DCT_method_ptr, (FAST_FLOAT * data));
   40.40 +
   40.41 +
   40.42 +/*
   40.43 + * An inverse DCT routine is given a pointer to the input JBLOCK and a pointer
   40.44 + * to an output sample array.  The routine must dequantize the input data as
   40.45 + * well as perform the IDCT; for dequantization, it uses the multiplier table
   40.46 + * pointed to by compptr->dct_table.  The output data is to be placed into the
   40.47 + * sample array starting at a specified column.  (Any row offset needed will
   40.48 + * be applied to the array pointer before it is passed to the IDCT code.)
   40.49 + * Note that the number of samples emitted by the IDCT routine is
   40.50 + * DCT_scaled_size * DCT_scaled_size.
   40.51 + */
   40.52 +
   40.53 +/* typedef inverse_DCT_method_ptr is declared in jpegint.h */
   40.54 +
   40.55 +/*
   40.56 + * Each IDCT routine has its own ideas about the best dct_table element type.
   40.57 + */
   40.58 +
   40.59 +typedef MULTIPLIER ISLOW_MULT_TYPE; /* short or int, whichever is faster */
   40.60 +#if BITS_IN_JSAMPLE == 8
   40.61 +typedef MULTIPLIER IFAST_MULT_TYPE; /* 16 bits is OK, use short if faster */
   40.62 +#define IFAST_SCALE_BITS  2	/* fractional bits in scale factors */
   40.63 +#else
   40.64 +typedef INT32 IFAST_MULT_TYPE;	/* need 32 bits for scaled quantizers */
   40.65 +#define IFAST_SCALE_BITS  13	/* fractional bits in scale factors */
   40.66 +#endif
   40.67 +typedef FAST_FLOAT FLOAT_MULT_TYPE; /* preferred floating type */
   40.68 +
   40.69 +
   40.70 +/*
   40.71 + * Each IDCT routine is responsible for range-limiting its results and
   40.72 + * converting them to unsigned form (0..MAXJSAMPLE).  The raw outputs could
   40.73 + * be quite far out of range if the input data is corrupt, so a bulletproof
   40.74 + * range-limiting step is required.  We use a mask-and-table-lookup method
   40.75 + * to do the combined operations quickly.  See the comments with
   40.76 + * prepare_range_limit_table (in jdmaster.c) for more info.
   40.77 + */
   40.78 +
   40.79 +#define IDCT_range_limit(cinfo)  ((cinfo)->sample_range_limit + CENTERJSAMPLE)
   40.80 +
   40.81 +#define RANGE_MASK  (MAXJSAMPLE * 4 + 3) /* 2 bits wider than legal samples */
   40.82 +
   40.83 +
   40.84 +/* Short forms of external names for systems with brain-damaged linkers. */
   40.85 +
   40.86 +#ifdef NEED_SHORT_EXTERNAL_NAMES
   40.87 +#define jpeg_fdct_islow		jFDislow
   40.88 +#define jpeg_fdct_ifast		jFDifast
   40.89 +#define jpeg_fdct_float		jFDfloat
   40.90 +#define jpeg_idct_islow		jRDislow
   40.91 +#define jpeg_idct_ifast		jRDifast
   40.92 +#define jpeg_idct_float		jRDfloat
   40.93 +#define jpeg_idct_4x4		jRD4x4
   40.94 +#define jpeg_idct_2x2		jRD2x2
   40.95 +#define jpeg_idct_1x1		jRD1x1
   40.96 +#endif /* NEED_SHORT_EXTERNAL_NAMES */
   40.97 +
   40.98 +/* Extern declarations for the forward and inverse DCT routines. */
   40.99 +
  40.100 +EXTERN(void) jpeg_fdct_islow JPP((DCTELEM * data));
  40.101 +EXTERN(void) jpeg_fdct_ifast JPP((DCTELEM * data));
  40.102 +EXTERN(void) jpeg_fdct_float JPP((FAST_FLOAT * data));
  40.103 +
  40.104 +EXTERN(void) jpeg_idct_islow
  40.105 +    JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr,
  40.106 +	 JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
  40.107 +EXTERN(void) jpeg_idct_ifast
  40.108 +    JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr,
  40.109 +	 JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
  40.110 +EXTERN(void) jpeg_idct_float
  40.111 +    JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr,
  40.112 +	 JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
  40.113 +EXTERN(void) jpeg_idct_4x4
  40.114 +    JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr,
  40.115 +	 JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
  40.116 +EXTERN(void) jpeg_idct_2x2
  40.117 +    JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr,
  40.118 +	 JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
  40.119 +EXTERN(void) jpeg_idct_1x1
  40.120 +    JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr,
  40.121 +	 JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
  40.122 +
  40.123 +
  40.124 +/*
  40.125 + * Macros for handling fixed-point arithmetic; these are used by many
  40.126 + * but not all of the DCT/IDCT modules.
  40.127 + *
  40.128 + * All values are expected to be of type INT32.
  40.129 + * Fractional constants are scaled left by CONST_BITS bits.
  40.130 + * CONST_BITS is defined within each module using these macros,
  40.131 + * and may differ from one module to the next.
  40.132 + */
  40.133 +
  40.134 +#define ONE	((INT32) 1)
  40.135 +#define CONST_SCALE (ONE << CONST_BITS)
  40.136 +
  40.137 +/* Convert a positive real constant to an integer scaled by CONST_SCALE.
  40.138 + * Caution: some C compilers fail to reduce "FIX(constant)" at compile time,
  40.139 + * thus causing a lot of useless floating-point operations at run time.
  40.140 + */
  40.141 +
  40.142 +#define FIX(x)	((INT32) ((x) * CONST_SCALE + 0.5))
  40.143 +
  40.144 +/* Descale and correctly round an INT32 value that's scaled by N bits.
  40.145 + * We assume RIGHT_SHIFT rounds towards minus infinity, so adding
  40.146 + * the fudge factor is correct for either sign of X.
  40.147 + */
  40.148 +
  40.149 +#define DESCALE(x,n)  RIGHT_SHIFT((x) + (ONE << ((n)-1)), n)
  40.150 +
  40.151 +/* Multiply an INT32 variable by an INT32 constant to yield an INT32 result.
  40.152 + * This macro is used only when the two inputs will actually be no more than
  40.153 + * 16 bits wide, so that a 16x16->32 bit multiply can be used instead of a
  40.154 + * full 32x32 multiply.  This provides a useful speedup on many machines.
  40.155 + * Unfortunately there is no way to specify a 16x16->32 multiply portably
  40.156 + * in C, but some C compilers will do the right thing if you provide the
  40.157 + * correct combination of casts.
  40.158 + */
  40.159 +
  40.160 +#ifdef SHORTxSHORT_32		/* may work if 'int' is 32 bits */
  40.161 +#define MULTIPLY16C16(var,const)  (((INT16) (var)) * ((INT16) (const)))
  40.162 +#endif
  40.163 +#ifdef SHORTxLCONST_32		/* known to work with Microsoft C 6.0 */
  40.164 +#define MULTIPLY16C16(var,const)  (((INT16) (var)) * ((INT32) (const)))
  40.165 +#endif
  40.166 +
  40.167 +#ifndef MULTIPLY16C16		/* default definition */
  40.168 +#define MULTIPLY16C16(var,const)  ((var) * (const))
  40.169 +#endif
  40.170 +
  40.171 +/* Same except both inputs are variables. */
  40.172 +
  40.173 +#ifdef SHORTxSHORT_32		/* may work if 'int' is 32 bits */
  40.174 +#define MULTIPLY16V16(var1,var2)  (((INT16) (var1)) * ((INT16) (var2)))
  40.175 +#endif
  40.176 +
  40.177 +#ifndef MULTIPLY16V16		/* default definition */
  40.178 +#define MULTIPLY16V16(var1,var2)  ((var1) * (var2))
  40.179 +#endif
    41.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    41.2 +++ b/libs/libjpeg/jddctmgr.c	Sat Sep 19 05:51:51 2015 +0300
    41.3 @@ -0,0 +1,269 @@
    41.4 +/*
    41.5 + * jddctmgr.c
    41.6 + *
    41.7 + * Copyright (C) 1994-1996, Thomas G. Lane.
    41.8 + * This file is part of the Independent JPEG Group's software.
    41.9 + * For conditions of distribution and use, see the accompanying README file.
   41.10 + *
   41.11 + * This file contains the inverse-DCT management logic.
   41.12 + * This code selects a particular IDCT implementation to be used,
   41.13 + * and it performs related housekeeping chores.  No code in this file
   41.14 + * is executed per IDCT step, only during output pass setup.
   41.15 + *
   41.16 + * Note that the IDCT routines are responsible for performing coefficient
   41.17 + * dequantization as well as the IDCT proper.  This module sets up the
   41.18 + * dequantization multiplier table needed by the IDCT routine.
   41.19 + */
   41.20 +
   41.21 +#define JPEG_INTERNALS
   41.22 +#include "jinclude.h"
   41.23 +#include "jpeglib.h"
   41.24 +#include "jdct.h"		/* Private declarations for DCT subsystem */
   41.25 +
   41.26 +
   41.27 +/*
   41.28 + * The decompressor input side (jdinput.c) saves away the appropriate
   41.29 + * quantization table for each component at the start of the first scan
   41.30 + * involving that component.  (This is necessary in order to correctly
   41.31 + * decode files that reuse Q-table slots.)
   41.32 + * When we are ready to make an output pass, the saved Q-table is converted
   41.33 + * to a multiplier table that will actually be used by the IDCT routine.
   41.34 + * The multiplier table contents are IDCT-method-dependent.  To support
   41.35 + * application changes in IDCT method between scans, we can remake the
   41.36 + * multiplier tables if necessary.
   41.37 + * In buffered-image mode, the first output pass may occur before any data
   41.38 + * has been seen for some components, and thus before their Q-tables have
   41.39 + * been saved away.  To handle this case, multiplier tables are preset
   41.40 + * to zeroes; the result of the IDCT will be a neutral gray level.
   41.41 + */
   41.42 +
   41.43 +
   41.44 +/* Private subobject for this module */
   41.45 +
   41.46 +typedef struct {
   41.47 +  struct jpeg_inverse_dct pub;	/* public fields */
   41.48 +
   41.49 +  /* This array contains the IDCT method code that each multiplier table
   41.50 +   * is currently set up for, or -1 if it's not yet set up.
   41.51 +   * The actual multiplier tables are pointed to by dct_table in the
   41.52 +   * per-component comp_info structures.
   41.53 +   */
   41.54 +  int cur_method[MAX_COMPONENTS];
   41.55 +} my_idct_controller;
   41.56 +
   41.57 +typedef my_idct_controller * my_idct_ptr;
   41.58 +
   41.59 +
   41.60 +/* Allocated multiplier tables: big enough for any supported variant */
   41.61 +
   41.62 +typedef union {
   41.63 +  ISLOW_MULT_TYPE islow_array[DCTSIZE2];
   41.64 +#ifdef DCT_IFAST_SUPPORTED
   41.65 +  IFAST_MULT_TYPE ifast_array[DCTSIZE2];
   41.66 +#endif
   41.67 +#ifdef DCT_FLOAT_SUPPORTED
   41.68 +  FLOAT_MULT_TYPE float_array[DCTSIZE2];
   41.69 +#endif
   41.70 +} multiplier_table;
   41.71 +
   41.72 +
   41.73 +/* The current scaled-IDCT routines require ISLOW-style multiplier tables,
   41.74 + * so be sure to compile that code if either ISLOW or SCALING is requested.
   41.75 + */
   41.76 +#ifdef DCT_ISLOW_SUPPORTED
   41.77 +#define PROVIDE_ISLOW_TABLES
   41.78 +#else
   41.79 +#ifdef IDCT_SCALING_SUPPORTED
   41.80 +#define PROVIDE_ISLOW_TABLES
   41.81 +#endif
   41.82 +#endif
   41.83 +
   41.84 +
   41.85 +/*
   41.86 + * Prepare for an output pass.
   41.87 + * Here we select the proper IDCT routine for each component and build
   41.88 + * a matching multiplier table.
   41.89 + */
   41.90 +
   41.91 +METHODDEF(void)
   41.92 +start_pass (j_decompress_ptr cinfo)
   41.93 +{
   41.94 +  my_idct_ptr idct = (my_idct_ptr) cinfo->idct;
   41.95 +  int ci, i;
   41.96 +  jpeg_component_info *compptr;
   41.97 +  int method = 0;
   41.98 +  inverse_DCT_method_ptr method_ptr = NULL;
   41.99 +  JQUANT_TBL * qtbl;
  41.100 +
  41.101 +  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  41.102 +       ci++, compptr++) {
  41.103 +    /* Select the proper IDCT routine for this component's scaling */
  41.104 +    switch (compptr->DCT_scaled_size) {
  41.105 +#ifdef IDCT_SCALING_SUPPORTED
  41.106 +    case 1:
  41.107 +      method_ptr = jpeg_idct_1x1;
  41.108 +      method = JDCT_ISLOW;	/* jidctred uses islow-style table */
  41.109 +      break;
  41.110 +    case 2:
  41.111 +      method_ptr = jpeg_idct_2x2;
  41.112 +      method = JDCT_ISLOW;	/* jidctred uses islow-style table */
  41.113 +      break;
  41.114 +    case 4:
  41.115 +      method_ptr = jpeg_idct_4x4;
  41.116 +      method = JDCT_ISLOW;	/* jidctred uses islow-style table */
  41.117 +      break;
  41.118 +#endif
  41.119 +    case DCTSIZE:
  41.120 +      switch (cinfo->dct_method) {
  41.121 +#ifdef DCT_ISLOW_SUPPORTED
  41.122 +      case JDCT_ISLOW:
  41.123 +	method_ptr = jpeg_idct_islow;
  41.124 +	method = JDCT_ISLOW;
  41.125 +	break;
  41.126 +#endif
  41.127 +#ifdef DCT_IFAST_SUPPORTED
  41.128 +      case JDCT_IFAST:
  41.129 +	method_ptr = jpeg_idct_ifast;
  41.130 +	method = JDCT_IFAST;
  41.131 +	break;
  41.132 +#endif
  41.133 +#ifdef DCT_FLOAT_SUPPORTED
  41.134 +      case JDCT_FLOAT:
  41.135 +	method_ptr = jpeg_idct_float;
  41.136 +	method = JDCT_FLOAT;
  41.137 +	break;
  41.138 +#endif
  41.139 +      default:
  41.140 +	ERREXIT(cinfo, JERR_NOT_COMPILED);
  41.141 +	break;
  41.142 +      }
  41.143 +      break;
  41.144 +    default:
  41.145 +      ERREXIT1(cinfo, JERR_BAD_DCTSIZE, compptr->DCT_scaled_size);
  41.146 +      break;
  41.147 +    }
  41.148 +    idct->pub.inverse_DCT[ci] = method_ptr;
  41.149 +    /* Create multiplier table from quant table.
  41.150 +     * However, we can skip this if the component is uninteresting
  41.151 +     * or if we already built the table.  Also, if no quant table
  41.152 +     * has yet been saved for the component, we leave the
  41.153 +     * multiplier table all-zero; we'll be reading zeroes from the
  41.154 +     * coefficient controller's buffer anyway.
  41.155 +     */
  41.156 +    if (! compptr->component_needed || idct->cur_method[ci] == method)
  41.157 +      continue;
  41.158 +    qtbl = compptr->quant_table;
  41.159 +    if (qtbl == NULL)		/* happens if no data yet for component */
  41.160 +      continue;
  41.161 +    idct->cur_method[ci] = method;
  41.162 +    switch (method) {
  41.163 +#ifdef PROVIDE_ISLOW_TABLES
  41.164 +    case JDCT_ISLOW:
  41.165 +      {
  41.166 +	/* For LL&M IDCT method, multipliers are equal to raw quantization
  41.167 +	 * coefficients, but are stored as ints to ensure access efficiency.
  41.168 +	 */
  41.169 +	ISLOW_MULT_TYPE * ismtbl = (ISLOW_MULT_TYPE *) compptr->dct_table;
  41.170 +	for (i = 0; i < DCTSIZE2; i++) {
  41.171 +	  ismtbl[i] = (ISLOW_MULT_TYPE) qtbl->quantval[i];
  41.172 +	}
  41.173 +      }
  41.174 +      break;
  41.175 +#endif
  41.176 +#ifdef DCT_IFAST_SUPPORTED
  41.177 +    case JDCT_IFAST:
  41.178 +      {
  41.179 +	/* For AA&N IDCT method, multipliers are equal to quantization
  41.180 +	 * coefficients scaled by scalefactor[row]*scalefactor[col], where
  41.181 +	 *   scalefactor[0] = 1
  41.182 +	 *   scalefactor[k] = cos(k*PI/16) * sqrt(2)    for k=1..7
  41.183 +	 * For integer operation, the multiplier table is to be scaled by
  41.184 +	 * IFAST_SCALE_BITS.
  41.185 +	 */
  41.186 +	IFAST_MULT_TYPE * ifmtbl = (IFAST_MULT_TYPE *) compptr->dct_table;
  41.187 +#define CONST_BITS 14
  41.188 +	static const INT16 aanscales[DCTSIZE2] = {
  41.189 +	  /* precomputed values scaled up by 14 bits */
  41.190 +	  16384, 22725, 21407, 19266, 16384, 12873,  8867,  4520,
  41.191 +	  22725, 31521, 29692, 26722, 22725, 17855, 12299,  6270,
  41.192 +	  21407, 29692, 27969, 25172, 21407, 16819, 11585,  5906,
  41.193 +	  19266, 26722, 25172, 22654, 19266, 15137, 10426,  5315,
  41.194 +	  16384, 22725, 21407, 19266, 16384, 12873,  8867,  4520,
  41.195 +	  12873, 17855, 16819, 15137, 12873, 10114,  6967,  3552,
  41.196 +	   8867, 12299, 11585, 10426,  8867,  6967,  4799,  2446,
  41.197 +	   4520,  6270,  5906,  5315,  4520,  3552,  2446,  1247
  41.198 +	};
  41.199 +	SHIFT_TEMPS
  41.200 +
  41.201 +	for (i = 0; i < DCTSIZE2; i++) {
  41.202 +	  ifmtbl[i] = (IFAST_MULT_TYPE)
  41.203 +	    DESCALE(MULTIPLY16V16((INT32) qtbl->quantval[i],
  41.204 +				  (INT32) aanscales[i]),
  41.205 +		    CONST_BITS-IFAST_SCALE_BITS);
  41.206 +	}
  41.207 +      }
  41.208 +      break;
  41.209 +#endif
  41.210 +#ifdef DCT_FLOAT_SUPPORTED
  41.211 +    case JDCT_FLOAT:
  41.212 +      {
  41.213 +	/* For float AA&N IDCT method, multipliers are equal to quantization
  41.214 +	 * coefficients scaled by scalefactor[row]*scalefactor[col], where
  41.215 +	 *   scalefactor[0] = 1
  41.216 +	 *   scalefactor[k] = cos(k*PI/16) * sqrt(2)    for k=1..7
  41.217 +	 */
  41.218 +	FLOAT_MULT_TYPE * fmtbl = (FLOAT_MULT_TYPE *) compptr->dct_table;
  41.219 +	int row, col;
  41.220 +	static const double aanscalefactor[DCTSIZE] = {
  41.221 +	  1.0, 1.387039845, 1.306562965, 1.175875602,
  41.222 +	  1.0, 0.785694958, 0.541196100, 0.275899379
  41.223 +	};
  41.224 +
  41.225 +	i = 0;
  41.226 +	for (row = 0; row < DCTSIZE; row++) {
  41.227 +	  for (col = 0; col < DCTSIZE; col++) {
  41.228 +	    fmtbl[i] = (FLOAT_MULT_TYPE)
  41.229 +	      ((double) qtbl->quantval[i] *
  41.230 +	       aanscalefactor[row] * aanscalefactor[col]);
  41.231 +	    i++;
  41.232 +	  }
  41.233 +	}
  41.234 +      }
  41.235 +      break;
  41.236 +#endif
  41.237 +    default:
  41.238 +      ERREXIT(cinfo, JERR_NOT_COMPILED);
  41.239 +      break;
  41.240 +    }
  41.241 +  }
  41.242 +}
  41.243 +
  41.244 +
  41.245 +/*
  41.246 + * Initialize IDCT manager.
  41.247 + */
  41.248 +
  41.249 +GLOBAL(void)
  41.250 +jinit_inverse_dct (j_decompress_ptr cinfo)
  41.251 +{
  41.252 +  my_idct_ptr idct;
  41.253 +  int ci;
  41.254 +  jpeg_component_info *compptr;
  41.255 +
  41.256 +  idct = (my_idct_ptr)
  41.257 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  41.258 +				SIZEOF(my_idct_controller));
  41.259 +  cinfo->idct = (struct jpeg_inverse_dct *) idct;
  41.260 +  idct->pub.start_pass = start_pass;
  41.261 +
  41.262 +  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  41.263 +       ci++, compptr++) {
  41.264 +    /* Allocate and pre-zero a multiplier table for each component */
  41.265 +    compptr->dct_table =
  41.266 +      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  41.267 +				  SIZEOF(multiplier_table));
  41.268 +    MEMZERO(compptr->dct_table, SIZEOF(multiplier_table));
  41.269 +    /* Mark multiplier table not yet set up for any method */
  41.270 +    idct->cur_method[ci] = -1;
  41.271 +  }
  41.272 +}
    42.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    42.2 +++ b/libs/libjpeg/jdhuff.c	Sat Sep 19 05:51:51 2015 +0300
    42.3 @@ -0,0 +1,651 @@
    42.4 +/*
    42.5 + * jdhuff.c
    42.6 + *
    42.7 + * Copyright (C) 1991-1997, Thomas G. Lane.
    42.8 + * This file is part of the Independent JPEG Group's software.
    42.9 + * For conditions of distribution and use, see the accompanying README file.
   42.10 + *
   42.11 + * This file contains Huffman entropy decoding routines.
   42.12 + *
   42.13 + * Much of the complexity here has to do with supporting input suspension.
   42.14 + * If the data source module demands suspension, we want to be able to back
   42.15 + * up to the start of the current MCU.  To do this, we copy state variables
   42.16 + * into local working storage, and update them back to the permanent
   42.17 + * storage only upon successful completion of an MCU.
   42.18 + */
   42.19 +
   42.20 +#define JPEG_INTERNALS
   42.21 +#include "jinclude.h"
   42.22 +#include "jpeglib.h"
   42.23 +#include "jdhuff.h"		/* Declarations shared with jdphuff.c */
   42.24 +
   42.25 +
   42.26 +/*
   42.27 + * Expanded entropy decoder object for Huffman decoding.
   42.28 + *
   42.29 + * The savable_state subrecord contains fields that change within an MCU,
   42.30 + * but must not be updated permanently until we complete the MCU.
   42.31 + */
   42.32 +
   42.33 +typedef struct {
   42.34 +  int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */
   42.35 +} savable_state;
   42.36 +
   42.37 +/* This macro is to work around compilers with missing or broken
   42.38 + * structure assignment.  You'll need to fix this code if you have
   42.39 + * such a compiler and you change MAX_COMPS_IN_SCAN.
   42.40 + */
   42.41 +
   42.42 +#ifndef NO_STRUCT_ASSIGN
   42.43 +#define ASSIGN_STATE(dest,src)  ((dest) = (src))
   42.44 +#else
   42.45 +#if MAX_COMPS_IN_SCAN == 4
   42.46 +#define ASSIGN_STATE(dest,src)  \
   42.47 +	((dest).last_dc_val[0] = (src).last_dc_val[0], \
   42.48 +	 (dest).last_dc_val[1] = (src).last_dc_val[1], \
   42.49 +	 (dest).last_dc_val[2] = (src).last_dc_val[2], \
   42.50 +	 (dest).last_dc_val[3] = (src).last_dc_val[3])
   42.51 +#endif
   42.52 +#endif
   42.53 +
   42.54 +
   42.55 +typedef struct {
   42.56 +  struct jpeg_entropy_decoder pub; /* public fields */
   42.57 +
   42.58 +  /* These fields are loaded into local variables at start of each MCU.
   42.59 +   * In case of suspension, we exit WITHOUT updating them.
   42.60 +   */
   42.61 +  bitread_perm_state bitstate;	/* Bit buffer at start of MCU */
   42.62 +  savable_state saved;		/* Other state at start of MCU */
   42.63 +
   42.64 +  /* These fields are NOT loaded into local working state. */
   42.65 +  unsigned int restarts_to_go;	/* MCUs left in this restart interval */
   42.66 +
   42.67 +  /* Pointers to derived tables (these workspaces have image lifespan) */
   42.68 +  d_derived_tbl * dc_derived_tbls[NUM_HUFF_TBLS];
   42.69 +  d_derived_tbl * ac_derived_tbls[NUM_HUFF_TBLS];
   42.70 +
   42.71 +  /* Precalculated info set up by start_pass for use in decode_mcu: */
   42.72 +
   42.73 +  /* Pointers to derived tables to be used for each block within an MCU */
   42.74 +  d_derived_tbl * dc_cur_tbls[D_MAX_BLOCKS_IN_MCU];
   42.75 +  d_derived_tbl * ac_cur_tbls[D_MAX_BLOCKS_IN_MCU];
   42.76 +  /* Whether we care about the DC and AC coefficient values for each block */
   42.77 +  boolean dc_needed[D_MAX_BLOCKS_IN_MCU];
   42.78 +  boolean ac_needed[D_MAX_BLOCKS_IN_MCU];
   42.79 +} huff_entropy_decoder;
   42.80 +
   42.81 +typedef huff_entropy_decoder * huff_entropy_ptr;
   42.82 +
   42.83 +
   42.84 +/*
   42.85 + * Initialize for a Huffman-compressed scan.
   42.86 + */
   42.87 +
   42.88 +METHODDEF(void)
   42.89 +start_pass_huff_decoder (j_decompress_ptr cinfo)
   42.90 +{
   42.91 +  huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
   42.92 +  int ci, blkn, dctbl, actbl;
   42.93 +  jpeg_component_info * compptr;
   42.94 +
   42.95 +  /* Check that the scan parameters Ss, Se, Ah/Al are OK for sequential JPEG.
   42.96 +   * This ought to be an error condition, but we make it a warning because
   42.97 +   * there are some baseline files out there with all zeroes in these bytes.
   42.98 +   */
   42.99 +  if (cinfo->Ss != 0 || cinfo->Se != DCTSIZE2-1 ||
  42.100 +      cinfo->Ah != 0 || cinfo->Al != 0)
  42.101 +    WARNMS(cinfo, JWRN_NOT_SEQUENTIAL);
  42.102 +
  42.103 +  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  42.104 +    compptr = cinfo->cur_comp_info[ci];
  42.105 +    dctbl = compptr->dc_tbl_no;
  42.106 +    actbl = compptr->ac_tbl_no;
  42.107 +    /* Compute derived values for Huffman tables */
  42.108 +    /* We may do this more than once for a table, but it's not expensive */
  42.109 +    jpeg_make_d_derived_tbl(cinfo, TRUE, dctbl,
  42.110 +			    & entropy->dc_derived_tbls[dctbl]);
  42.111 +    jpeg_make_d_derived_tbl(cinfo, FALSE, actbl,
  42.112 +			    & entropy->ac_derived_tbls[actbl]);
  42.113 +    /* Initialize DC predictions to 0 */
  42.114 +    entropy->saved.last_dc_val[ci] = 0;
  42.115 +  }
  42.116 +
  42.117 +  /* Precalculate decoding info for each block in an MCU of this scan */
  42.118 +  for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
  42.119 +    ci = cinfo->MCU_membership[blkn];
  42.120 +    compptr = cinfo->cur_comp_info[ci];
  42.121 +    /* Precalculate which table to use for each block */
  42.122 +    entropy->dc_cur_tbls[blkn] = entropy->dc_derived_tbls[compptr->dc_tbl_no];
  42.123 +    entropy->ac_cur_tbls[blkn] = entropy->ac_derived_tbls[compptr->ac_tbl_no];
  42.124 +    /* Decide whether we really care about the coefficient values */
  42.125 +    if (compptr->component_needed) {
  42.126 +      entropy->dc_needed[blkn] = TRUE;
  42.127 +      /* we don't need the ACs if producing a 1/8th-size image */
  42.128 +      entropy->ac_needed[blkn] = (compptr->DCT_scaled_size > 1);
  42.129 +    } else {
  42.130 +      entropy->dc_needed[blkn] = entropy->ac_needed[blkn] = FALSE;
  42.131 +    }
  42.132 +  }
  42.133 +
  42.134 +  /* Initialize bitread state variables */
  42.135 +  entropy->bitstate.bits_left = 0;
  42.136 +  entropy->bitstate.get_buffer = 0; /* unnecessary, but keeps Purify quiet */
  42.137 +  entropy->pub.insufficient_data = FALSE;
  42.138 +
  42.139 +  /* Initialize restart counter */
  42.140 +  entropy->restarts_to_go = cinfo->restart_interval;
  42.141 +}
  42.142 +
  42.143 +
  42.144 +/*
  42.145 + * Compute the derived values for a Huffman table.
  42.146 + * This routine also performs some validation checks on the table.
  42.147 + *
  42.148 + * Note this is also used by jdphuff.c.
  42.149 + */
  42.150 +
  42.151 +GLOBAL(void)
  42.152 +jpeg_make_d_derived_tbl (j_decompress_ptr cinfo, boolean isDC, int tblno,
  42.153 +			 d_derived_tbl ** pdtbl)
  42.154 +{
  42.155 +  JHUFF_TBL *htbl;
  42.156 +  d_derived_tbl *dtbl;
  42.157 +  int p, i, l, si, numsymbols;
  42.158 +  int lookbits, ctr;
  42.159 +  char huffsize[257];
  42.160 +  unsigned int huffcode[257];
  42.161 +  unsigned int code;
  42.162 +
  42.163 +  /* Note that huffsize[] and huffcode[] are filled in code-length order,
  42.164 +   * paralleling the order of the symbols themselves in htbl->huffval[].
  42.165 +   */
  42.166 +
  42.167 +  /* Find the input Huffman table */
  42.168 +  if (tblno < 0 || tblno >= NUM_HUFF_TBLS)
  42.169 +    ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
  42.170 +  htbl =
  42.171 +    isDC ? cinfo->dc_huff_tbl_ptrs[tblno] : cinfo->ac_huff_tbl_ptrs[tblno];
  42.172 +  if (htbl == NULL)
  42.173 +    ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
  42.174 +
  42.175 +  /* Allocate a workspace if we haven't already done so. */
  42.176 +  if (*pdtbl == NULL)
  42.177 +    *pdtbl = (d_derived_tbl *)
  42.178 +      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  42.179 +				  SIZEOF(d_derived_tbl));
  42.180 +  dtbl = *pdtbl;
  42.181 +  dtbl->pub = htbl;		/* fill in back link */
  42.182 +  
  42.183 +  /* Figure C.1: make table of Huffman code length for each symbol */
  42.184 +
  42.185 +  p = 0;
  42.186 +  for (l = 1; l <= 16; l++) {
  42.187 +    i = (int) htbl->bits[l];
  42.188 +    if (i < 0 || p + i > 256)	/* protect against table overrun */
  42.189 +      ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
  42.190 +    while (i--)
  42.191 +      huffsize[p++] = (char) l;
  42.192 +  }
  42.193 +  huffsize[p] = 0;
  42.194 +  numsymbols = p;
  42.195 +  
  42.196 +  /* Figure C.2: generate the codes themselves */
  42.197 +  /* We also validate that the counts represent a legal Huffman code tree. */
  42.198 +  
  42.199 +  code = 0;
  42.200 +  si = huffsize[0];
  42.201 +  p = 0;
  42.202 +  while (huffsize[p]) {
  42.203 +    while (((int) huffsize[p]) == si) {
  42.204 +      huffcode[p++] = code;
  42.205 +      code++;
  42.206 +    }
  42.207 +    /* code is now 1 more than the last code used for codelength si; but
  42.208 +     * it must still fit in si bits, since no code is allowed to be all ones.
  42.209 +     */
  42.210 +    if (((INT32) code) >= (((INT32) 1) << si))
  42.211 +      ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
  42.212 +    code <<= 1;
  42.213 +    si++;
  42.214 +  }
  42.215 +
  42.216 +  /* Figure F.15: generate decoding tables for bit-sequential decoding */
  42.217 +
  42.218 +  p = 0;
  42.219 +  for (l = 1; l <= 16; l++) {
  42.220 +    if (htbl->bits[l]) {
  42.221 +      /* valoffset[l] = huffval[] index of 1st symbol of code length l,
  42.222 +       * minus the minimum code of length l
  42.223 +       */
  42.224 +      dtbl->valoffset[l] = (INT32) p - (INT32) huffcode[p];
  42.225 +      p += htbl->bits[l];
  42.226 +      dtbl->maxcode[l] = huffcode[p-1]; /* maximum code of length l */
  42.227 +    } else {
  42.228 +      dtbl->maxcode[l] = -1;	/* -1 if no codes of this length */
  42.229 +    }
  42.230 +  }
  42.231 +  dtbl->maxcode[17] = 0xFFFFFL; /* ensures jpeg_huff_decode terminates */
  42.232 +
  42.233 +  /* Compute lookahead tables to speed up decoding.
  42.234 +   * First we set all the table entries to 0, indicating "too long";
  42.235 +   * then we iterate through the Huffman codes that are short enough and
  42.236 +   * fill in all the entries that correspond to bit sequences starting
  42.237 +   * with that code.
  42.238 +   */
  42.239 +
  42.240 +  MEMZERO(dtbl->look_nbits, SIZEOF(dtbl->look_nbits));
  42.241 +
  42.242 +  p = 0;
  42.243 +  for (l = 1; l <= HUFF_LOOKAHEAD; l++) {
  42.244 +    for (i = 1; i <= (int) htbl->bits[l]; i++, p++) {
  42.245 +      /* l = current code's length, p = its index in huffcode[] & huffval[]. */
  42.246 +      /* Generate left-justified code followed by all possible bit sequences */
  42.247 +      lookbits = huffcode[p] << (HUFF_LOOKAHEAD-l);
  42.248 +      for (ctr = 1 << (HUFF_LOOKAHEAD-l); ctr > 0; ctr--) {
  42.249 +	dtbl->look_nbits[lookbits] = l;
  42.250 +	dtbl->look_sym[lookbits] = htbl->huffval[p];
  42.251 +	lookbits++;
  42.252 +      }
  42.253 +    }
  42.254 +  }
  42.255 +
  42.256 +  /* Validate symbols as being reasonable.
  42.257 +   * For AC tables, we make no check, but accept all byte values 0..255.
  42.258 +   * For DC tables, we require the symbols to be in range 0..15.
  42.259 +   * (Tighter bounds could be applied depending on the data depth and mode,
  42.260 +   * but this is sufficient to ensure safe decoding.)
  42.261 +   */
  42.262 +  if (isDC) {
  42.263 +    for (i = 0; i < numsymbols; i++) {
  42.264 +      int sym = htbl->huffval[i];
  42.265 +      if (sym < 0 || sym > 15)
  42.266 +	ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
  42.267 +    }
  42.268 +  }
  42.269 +}
  42.270 +
  42.271 +
  42.272 +/*
  42.273 + * Out-of-line code for bit fetching (shared with jdphuff.c).
  42.274 + * See jdhuff.h for info about usage.
  42.275 + * Note: current values of get_buffer and bits_left are passed as parameters,
  42.276 + * but are returned in the corresponding fields of the state struct.
  42.277 + *
  42.278 + * On most machines MIN_GET_BITS should be 25 to allow the full 32-bit width
  42.279 + * of get_buffer to be used.  (On machines with wider words, an even larger
  42.280 + * buffer could be used.)  However, on some machines 32-bit shifts are
  42.281 + * quite slow and take time proportional to the number of places shifted.
  42.282 + * (This is true with most PC compilers, for instance.)  In this case it may
  42.283 + * be a win to set MIN_GET_BITS to the minimum value of 15.  This reduces the
  42.284 + * average shift distance at the cost of more calls to jpeg_fill_bit_buffer.
  42.285 + */
  42.286 +
  42.287 +#ifdef SLOW_SHIFT_32
  42.288 +#define MIN_GET_BITS  15	/* minimum allowable value */
  42.289 +#else
  42.290 +#define MIN_GET_BITS  (BIT_BUF_SIZE-7)
  42.291 +#endif
  42.292 +
  42.293 +
  42.294 +GLOBAL(boolean)
  42.295 +jpeg_fill_bit_buffer (bitread_working_state * state,
  42.296 +		      register bit_buf_type get_buffer, register int bits_left,
  42.297 +		      int nbits)
  42.298 +/* Load up the bit buffer to a depth of at least nbits */
  42.299 +{
  42.300 +  /* Copy heavily used state fields into locals (hopefully registers) */
  42.301 +  register const JOCTET * next_input_byte = state->next_input_byte;
  42.302 +  register size_t bytes_in_buffer = state->bytes_in_buffer;
  42.303 +  j_decompress_ptr cinfo = state->cinfo;
  42.304 +
  42.305 +  /* Attempt to load at least MIN_GET_BITS bits into get_buffer. */
  42.306 +  /* (It is assumed that no request will be for more than that many bits.) */
  42.307 +  /* We fail to do so only if we hit a marker or are forced to suspend. */
  42.308 +
  42.309 +  if (cinfo->unread_marker == 0) {	/* cannot advance past a marker */
  42.310 +    while (bits_left < MIN_GET_BITS) {
  42.311 +      register int c;
  42.312 +
  42.313 +      /* Attempt to read a byte */
  42.314 +      if (bytes_in_buffer == 0) {
  42.315 +	if (! (*cinfo->src->fill_input_buffer) (cinfo))
  42.316 +	  return FALSE;
  42.317 +	next_input_byte = cinfo->src->next_input_byte;
  42.318 +	bytes_in_buffer = cinfo->src->bytes_in_buffer;
  42.319 +      }
  42.320 +      bytes_in_buffer--;
  42.321 +      c = GETJOCTET(*next_input_byte++);
  42.322 +
  42.323 +      /* If it's 0xFF, check and discard stuffed zero byte */
  42.324 +      if (c == 0xFF) {
  42.325 +	/* Loop here to discard any padding FF's on terminating marker,
  42.326 +	 * so that we can save a valid unread_marker value.  NOTE: we will
  42.327 +	 * accept multiple FF's followed by a 0 as meaning a single FF data
  42.328 +	 * byte.  This data pattern is not valid according to the standard.
  42.329 +	 */
  42.330 +	do {
  42.331 +	  if (bytes_in_buffer == 0) {
  42.332 +	    if (! (*cinfo->src->fill_input_buffer) (cinfo))
  42.333 +	      return FALSE;
  42.334 +	    next_input_byte = cinfo->src->next_input_byte;
  42.335 +	    bytes_in_buffer = cinfo->src->bytes_in_buffer;
  42.336 +	  }
  42.337 +	  bytes_in_buffer--;
  42.338 +	  c = GETJOCTET(*next_input_byte++);
  42.339 +	} while (c == 0xFF);
  42.340 +
  42.341 +	if (c == 0) {
  42.342 +	  /* Found FF/00, which represents an FF data byte */
  42.343 +	  c = 0xFF;
  42.344 +	} else {
  42.345 +	  /* Oops, it's actually a marker indicating end of compressed data.
  42.346 +	   * Save the marker code for later use.
  42.347 +	   * Fine point: it might appear that we should save the marker into
  42.348 +	   * bitread working state, not straight into permanent state.  But
  42.349 +	   * once we have hit a marker, we cannot need to suspend within the
  42.350 +	   * current MCU, because we will read no more bytes from the data
  42.351 +	   * source.  So it is OK to update permanent state right away.
  42.352 +	   */
  42.353 +	  cinfo->unread_marker = c;
  42.354 +	  /* See if we need to insert some fake zero bits. */
  42.355 +	  goto no_more_bytes;
  42.356 +	}
  42.357 +      }
  42.358 +
  42.359 +      /* OK, load c into get_buffer */
  42.360 +      get_buffer = (get_buffer << 8) | c;
  42.361 +      bits_left += 8;
  42.362 +    } /* end while */
  42.363 +  } else {
  42.364 +  no_more_bytes:
  42.365 +    /* We get here if we've read the marker that terminates the compressed
  42.366 +     * data segment.  There should be enough bits in the buffer register
  42.367 +     * to satisfy the request; if so, no problem.
  42.368 +     */
  42.369 +    if (nbits > bits_left) {
  42.370 +      /* Uh-oh.  Report corrupted data to user and stuff zeroes into
  42.371 +       * the data stream, so that we can produce some kind of image.
  42.372 +       * We use a nonvolatile flag to ensure that only one warning message
  42.373 +       * appears per data segment.
  42.374 +       */
  42.375 +      if (! cinfo->entropy->insufficient_data) {
  42.376 +	WARNMS(cinfo, JWRN_HIT_MARKER);
  42.377 +	cinfo->entropy->insufficient_data = TRUE;
  42.378 +      }
  42.379 +      /* Fill the buffer with zero bits */
  42.380 +      get_buffer <<= MIN_GET_BITS - bits_left;
  42.381 +      bits_left = MIN_GET_BITS;
  42.382 +    }
  42.383 +  }
  42.384 +
  42.385 +  /* Unload the local registers */
  42.386 +  state->next_input_byte = next_input_byte;
  42.387 +  state->bytes_in_buffer = bytes_in_buffer;
  42.388 +  state->get_buffer = get_buffer;
  42.389 +  state->bits_left = bits_left;
  42.390 +
  42.391 +  return TRUE;
  42.392 +}
  42.393 +
  42.394 +
  42.395 +/*
  42.396 + * Out-of-line code for Huffman code decoding.
  42.397 + * See jdhuff.h for info about usage.
  42.398 + */
  42.399 +
  42.400 +GLOBAL(int)
  42.401 +jpeg_huff_decode (bitread_working_state * state,
  42.402 +		  register bit_buf_type get_buffer, register int bits_left,
  42.403 +		  d_derived_tbl * htbl, int min_bits)
  42.404 +{
  42.405 +  register int l = min_bits;
  42.406 +  register INT32 code;
  42.407 +
  42.408 +  /* HUFF_DECODE has determined that the code is at least min_bits */
  42.409 +  /* bits long, so fetch that many bits in one swoop. */
  42.410 +
  42.411 +  CHECK_BIT_BUFFER(*state, l, return -1);
  42.412 +  code = GET_BITS(l);
  42.413 +
  42.414 +  /* Collect the rest of the Huffman code one bit at a time. */
  42.415 +  /* This is per Figure F.16 in the JPEG spec. */
  42.416 +
  42.417 +  while (code > htbl->maxcode[l]) {
  42.418 +    code <<= 1;
  42.419 +    CHECK_BIT_BUFFER(*state, 1, return -1);
  42.420 +    code |= GET_BITS(1);
  42.421 +    l++;
  42.422 +  }
  42.423 +
  42.424 +  /* Unload the local registers */
  42.425 +  state->get_buffer = get_buffer;
  42.426 +  state->bits_left = bits_left;
  42.427 +
  42.428 +  /* With garbage input we may reach the sentinel value l = 17. */
  42.429 +
  42.430 +  if (l > 16) {
  42.431 +    WARNMS(state->cinfo, JWRN_HUFF_BAD_CODE);
  42.432 +    return 0;			/* fake a zero as the safest result */
  42.433 +  }
  42.434 +
  42.435 +  return htbl->pub->huffval[ (int) (code + htbl->valoffset[l]) ];
  42.436 +}
  42.437 +
  42.438 +
  42.439 +/*
  42.440 + * Figure F.12: extend sign bit.
  42.441 + * On some machines, a shift and add will be faster than a table lookup.
  42.442 + */
  42.443 +
  42.444 +#ifdef AVOID_TABLES
  42.445 +
  42.446 +#define HUFF_EXTEND(x,s)  ((x) < (1<<((s)-1)) ? (x) + (((-1)<<(s)) + 1) : (x))
  42.447 +
  42.448 +#else
  42.449 +
  42.450 +#define HUFF_EXTEND(x,s)  ((x) < extend_test[s] ? (x) + extend_offset[s] : (x))
  42.451 +
  42.452 +static const int extend_test[16] =   /* entry n is 2**(n-1) */
  42.453 +  { 0, 0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080,
  42.454 +    0x0100, 0x0200, 0x0400, 0x0800, 0x1000, 0x2000, 0x4000 };
  42.455 +
  42.456 +static const int extend_offset[16] = /* entry n is (-1 << n) + 1 */
  42.457 +  { 0, ((-1)<<1) + 1, ((-1)<<2) + 1, ((-1)<<3) + 1, ((-1)<<4) + 1,
  42.458 +    ((-1)<<5) + 1, ((-1)<<6) + 1, ((-1)<<7) + 1, ((-1)<<8) + 1,
  42.459 +    ((-1)<<9) + 1, ((-1)<<10) + 1, ((-1)<<11) + 1, ((-1)<<12) + 1,
  42.460 +    ((-1)<<13) + 1, ((-1)<<14) + 1, ((-1)<<15) + 1 };
  42.461 +
  42.462 +#endif /* AVOID_TABLES */
  42.463 +
  42.464 +
  42.465 +/*
  42.466 + * Check for a restart marker & resynchronize decoder.
  42.467 + * Returns FALSE if must suspend.
  42.468 + */
  42.469 +
  42.470 +LOCAL(boolean)
  42.471 +process_restart (j_decompress_ptr cinfo)
  42.472 +{
  42.473 +  huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
  42.474 +  int ci;
  42.475 +
  42.476 +  /* Throw away any unused bits remaining in bit buffer; */
  42.477 +  /* include any full bytes in next_marker's count of discarded bytes */
  42.478 +  cinfo->marker->discarded_bytes += entropy->bitstate.bits_left / 8;
  42.479 +  entropy->bitstate.bits_left = 0;
  42.480 +
  42.481 +  /* Advance past the RSTn marker */
  42.482 +  if (! (*cinfo->marker->read_restart_marker) (cinfo))
  42.483 +    return FALSE;
  42.484 +
  42.485 +  /* Re-initialize DC predictions to 0 */
  42.486 +  for (ci = 0; ci < cinfo->comps_in_scan; ci++)
  42.487 +    entropy->saved.last_dc_val[ci] = 0;
  42.488 +
  42.489 +  /* Reset restart counter */
  42.490 +  entropy->restarts_to_go = cinfo->restart_interval;
  42.491 +
  42.492 +  /* Reset out-of-data flag, unless read_restart_marker left us smack up
  42.493 +   * against a marker.  In that case we will end up treating the next data
  42.494 +   * segment as empty, and we can avoid producing bogus output pixels by
  42.495 +   * leaving the flag set.
  42.496 +   */
  42.497 +  if (cinfo->unread_marker == 0)
  42.498 +    entropy->pub.insufficient_data = FALSE;
  42.499 +
  42.500 +  return TRUE;
  42.501 +}
  42.502 +
  42.503 +
  42.504 +/*
  42.505 + * Decode and return one MCU's worth of Huffman-compressed coefficients.
  42.506 + * The coefficients are reordered from zigzag order into natural array order,
  42.507 + * but are not dequantized.
  42.508 + *
  42.509 + * The i'th block of the MCU is stored into the block pointed to by
  42.510 + * MCU_data[i].  WE ASSUME THIS AREA HAS BEEN ZEROED BY THE CALLER.
  42.511 + * (Wholesale zeroing is usually a little faster than retail...)
  42.512 + *
  42.513 + * Returns FALSE if data source requested suspension.  In that case no
  42.514 + * changes have been made to permanent state.  (Exception: some output
  42.515 + * coefficients may already have been assigned.  This is harmless for
  42.516 + * this module, since we'll just re-assign them on the next call.)
  42.517 + */
  42.518 +
  42.519 +METHODDEF(boolean)
  42.520 +decode_mcu (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
  42.521 +{
  42.522 +  huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
  42.523 +  int blkn;
  42.524 +  BITREAD_STATE_VARS;
  42.525 +  savable_state state;
  42.526 +
  42.527 +  /* Process restart marker if needed; may have to suspend */
  42.528 +  if (cinfo->restart_interval) {
  42.529 +    if (entropy->restarts_to_go == 0)
  42.530 +      if (! process_restart(cinfo))
  42.531 +	return FALSE;
  42.532 +  }
  42.533 +
  42.534 +  /* If we've run out of data, just leave the MCU set to zeroes.
  42.535 +   * This way, we return uniform gray for the remainder of the segment.
  42.536 +   */
  42.537 +  if (! entropy->pub.insufficient_data) {
  42.538 +
  42.539 +    /* Load up working state */
  42.540 +    BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
  42.541 +    ASSIGN_STATE(state, entropy->saved);
  42.542 +
  42.543 +    /* Outer loop handles each block in the MCU */
  42.544 +
  42.545 +    for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
  42.546 +      JBLOCKROW block = MCU_data[blkn];
  42.547 +      d_derived_tbl * dctbl = entropy->dc_cur_tbls[blkn];
  42.548 +      d_derived_tbl * actbl = entropy->ac_cur_tbls[blkn];
  42.549 +      register int s, k, r;
  42.550 +
  42.551 +      /* Decode a single block's worth of coefficients */
  42.552 +
  42.553 +      /* Section F.2.2.1: decode the DC coefficient difference */
  42.554 +      HUFF_DECODE(s, br_state, dctbl, return FALSE, label1);
  42.555 +      if (s) {
  42.556 +	CHECK_BIT_BUFFER(br_state, s, return FALSE);
  42.557 +	r = GET_BITS(s);
  42.558 +	s = HUFF_EXTEND(r, s);
  42.559 +      }
  42.560 +
  42.561 +      if (entropy->dc_needed[blkn]) {
  42.562 +	/* Convert DC difference to actual value, update last_dc_val */
  42.563 +	int ci = cinfo->MCU_membership[blkn];
  42.564 +	s += state.last_dc_val[ci];
  42.565 +	state.last_dc_val[ci] = s;
  42.566 +	/* Output the DC coefficient (assumes jpeg_natural_order[0] = 0) */
  42.567 +	(*block)[0] = (JCOEF) s;
  42.568 +      }
  42.569 +
  42.570 +      if (entropy->ac_needed[blkn]) {
  42.571 +
  42.572 +	/* Section F.2.2.2: decode the AC coefficients */
  42.573 +	/* Since zeroes are skipped, output area must be cleared beforehand */
  42.574 +	for (k = 1; k < DCTSIZE2; k++) {
  42.575 +	  HUFF_DECODE(s, br_state, actbl, return FALSE, label2);
  42.576 +      
  42.577 +	  r = s >> 4;
  42.578 +	  s &= 15;
  42.579 +      
  42.580 +	  if (s) {
  42.581 +	    k += r;
  42.582 +	    CHECK_BIT_BUFFER(br_state, s, return FALSE);
  42.583 +	    r = GET_BITS(s);
  42.584 +	    s = HUFF_EXTEND(r, s);
  42.585 +	    /* Output coefficient in natural (dezigzagged) order.
  42.586 +	     * Note: the extra entries in jpeg_natural_order[] will save us
  42.587 +	     * if k >= DCTSIZE2, which could happen if the data is corrupted.
  42.588 +	     */
  42.589 +	    (*block)[jpeg_natural_order[k]] = (JCOEF) s;
  42.590 +	  } else {
  42.591 +	    if (r != 15)
  42.592 +	      break;
  42.593 +	    k += 15;
  42.594 +	  }
  42.595 +	}
  42.596 +
  42.597 +      } else {
  42.598 +
  42.599 +	/* Section F.2.2.2: decode the AC coefficients */
  42.600 +	/* In this path we just discard the values */
  42.601 +	for (k = 1; k < DCTSIZE2; k++) {
  42.602 +	  HUFF_DECODE(s, br_state, actbl, return FALSE, label3);
  42.603 +      
  42.604 +	  r = s >> 4;
  42.605 +	  s &= 15;
  42.606 +      
  42.607 +	  if (s) {
  42.608 +	    k += r;
  42.609 +	    CHECK_BIT_BUFFER(br_state, s, return FALSE);
  42.610 +	    DROP_BITS(s);
  42.611 +	  } else {
  42.612 +	    if (r != 15)
  42.613 +	      break;
  42.614 +	    k += 15;
  42.615 +	  }
  42.616 +	}
  42.617 +
  42.618 +      }
  42.619 +    }
  42.620 +
  42.621 +    /* Completed MCU, so update state */
  42.622 +    BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
  42.623 +    ASSIGN_STATE(entropy->saved, state);
  42.624 +  }
  42.625 +
  42.626 +  /* Account for restart interval (no-op if not using restarts) */
  42.627 +  entropy->restarts_to_go--;
  42.628 +
  42.629 +  return TRUE;
  42.630 +}
  42.631 +
  42.632 +
  42.633 +/*
  42.634 + * Module initialization routine for Huffman entropy decoding.
  42.635 + */
  42.636 +
  42.637 +GLOBAL(void)
  42.638 +jinit_huff_decoder (j_decompress_ptr cinfo)
  42.639 +{
  42.640 +  huff_entropy_ptr entropy;
  42.641 +  int i;
  42.642 +
  42.643 +  entropy = (huff_entropy_ptr)
  42.644 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  42.645 +				SIZEOF(huff_entropy_decoder));
  42.646 +  cinfo->entropy = (struct jpeg_entropy_decoder *) entropy;
  42.647 +  entropy->pub.start_pass = start_pass_huff_decoder;
  42.648 +  entropy->pub.decode_mcu = decode_mcu;
  42.649 +
  42.650 +  /* Mark tables unallocated */
  42.651 +  for (i = 0; i < NUM_HUFF_TBLS; i++) {
  42.652 +    entropy->dc_derived_tbls[i] = entropy->ac_derived_tbls[i] = NULL;
  42.653 +  }
  42.654 +}
    43.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    43.2 +++ b/libs/libjpeg/jdhuff.h	Sat Sep 19 05:51:51 2015 +0300
    43.3 @@ -0,0 +1,201 @@
    43.4 +/*
    43.5 + * jdhuff.h
    43.6 + *
    43.7 + * Copyright (C) 1991-1997, Thomas G. Lane.
    43.8 + * This file is part of the Independent JPEG Group's software.
    43.9 + * For conditions of distribution and use, see the accompanying README file.
   43.10 + *
   43.11 + * This file contains declarations for Huffman entropy decoding routines
   43.12 + * that are shared between the sequential decoder (jdhuff.c) and the
   43.13 + * progressive decoder (jdphuff.c).  No other modules need to see these.
   43.14 + */
   43.15 +
   43.16 +/* Short forms of external names for systems with brain-damaged linkers. */
   43.17 +
   43.18 +#ifdef NEED_SHORT_EXTERNAL_NAMES
   43.19 +#define jpeg_make_d_derived_tbl	jMkDDerived
   43.20 +#define jpeg_fill_bit_buffer	jFilBitBuf
   43.21 +#define jpeg_huff_decode	jHufDecode
   43.22 +#endif /* NEED_SHORT_EXTERNAL_NAMES */
   43.23 +
   43.24 +
   43.25 +/* Derived data constructed for each Huffman table */
   43.26 +
   43.27 +#define HUFF_LOOKAHEAD	8	/* # of bits of lookahead */
   43.28 +
   43.29 +typedef struct {
   43.30 +  /* Basic tables: (element [0] of each array is unused) */
   43.31 +  INT32 maxcode[18];		/* largest code of length k (-1 if none) */
   43.32 +  /* (maxcode[17] is a sentinel to ensure jpeg_huff_decode terminates) */
   43.33 +  INT32 valoffset[17];		/* huffval[] offset for codes of length k */
   43.34 +  /* valoffset[k] = huffval[] index of 1st symbol of code length k, less
   43.35 +   * the smallest code of length k; so given a code of length k, the
   43.36 +   * corresponding symbol is huffval[code + valoffset[k]]
   43.37 +   */
   43.38 +
   43.39 +  /* Link to public Huffman table (needed only in jpeg_huff_decode) */
   43.40 +  JHUFF_TBL *pub;
   43.41 +
   43.42 +  /* Lookahead tables: indexed by the next HUFF_LOOKAHEAD bits of
   43.43 +   * the input data stream.  If the next Huffman code is no more
   43.44 +   * than HUFF_LOOKAHEAD bits long, we can obtain its length and
   43.45 +   * the corresponding symbol directly from these tables.
   43.46 +   */
   43.47 +  int look_nbits[1<<HUFF_LOOKAHEAD]; /* # bits, or 0 if too long */
   43.48 +  UINT8 look_sym[1<<HUFF_LOOKAHEAD]; /* symbol, or unused */
   43.49 +} d_derived_tbl;
   43.50 +
   43.51 +/* Expand a Huffman table definition into the derived format */
   43.52 +EXTERN(void) jpeg_make_d_derived_tbl
   43.53 +	JPP((j_decompress_ptr cinfo, boolean isDC, int tblno,
   43.54 +	     d_derived_tbl ** pdtbl));
   43.55 +
   43.56 +
   43.57 +/*
   43.58 + * Fetching the next N bits from the input stream is a time-critical operation
   43.59 + * for the Huffman decoders.  We implement it with a combination of inline
   43.60 + * macros and out-of-line subroutines.  Note that N (the number of bits
   43.61 + * demanded at one time) never exceeds 15 for JPEG use.
   43.62 + *
   43.63 + * We read source bytes into get_buffer and dole out bits as needed.
   43.64 + * If get_buffer already contains enough bits, they are fetched in-line
   43.65 + * by the macros CHECK_BIT_BUFFER and GET_BITS.  When there aren't enough
   43.66 + * bits, jpeg_fill_bit_buffer is called; it will attempt to fill get_buffer
   43.67 + * as full as possible (not just to the number of bits needed; this
   43.68 + * prefetching reduces the overhead cost of calling jpeg_fill_bit_buffer).
   43.69 + * Note that jpeg_fill_bit_buffer may return FALSE to indicate suspension.
   43.70 + * On TRUE return, jpeg_fill_bit_buffer guarantees that get_buffer contains
   43.71 + * at least the requested number of bits --- dummy zeroes are inserted if
   43.72 + * necessary.
   43.73 + */
   43.74 +
   43.75 +typedef INT32 bit_buf_type;	/* type of bit-extraction buffer */
   43.76 +#define BIT_BUF_SIZE  32	/* size of buffer in bits */
   43.77 +
   43.78 +/* If long is > 32 bits on your machine, and shifting/masking longs is
   43.79 + * reasonably fast, making bit_buf_type be long and setting BIT_BUF_SIZE
   43.80 + * appropriately should be a win.  Unfortunately we can't define the size
   43.81 + * with something like  #define BIT_BUF_SIZE (sizeof(bit_buf_type)*8)
   43.82 + * because not all machines measure sizeof in 8-bit bytes.
   43.83 + */
   43.84 +
   43.85 +typedef struct {		/* Bitreading state saved across MCUs */
   43.86 +  bit_buf_type get_buffer;	/* current bit-extraction buffer */
   43.87 +  int bits_left;		/* # of unused bits in it */
   43.88 +} bitread_perm_state;
   43.89 +
   43.90 +typedef struct {		/* Bitreading working state within an MCU */
   43.91 +  /* Current data source location */
   43.92 +  /* We need a copy, rather than munging the original, in case of suspension */
   43.93 +  const JOCTET * next_input_byte; /* => next byte to read from source */
   43.94 +  size_t bytes_in_buffer;	/* # of bytes remaining in source buffer */
   43.95 +  /* Bit input buffer --- note these values are kept in register variables,
   43.96 +   * not in this struct, inside the inner loops.
   43.97 +   */
   43.98 +  bit_buf_type get_buffer;	/* current bit-extraction buffer */
   43.99 +  int bits_left;		/* # of unused bits in it */
  43.100 +  /* Pointer needed by jpeg_fill_bit_buffer. */
  43.101 +  j_decompress_ptr cinfo;	/* back link to decompress master record */
  43.102 +} bitread_working_state;
  43.103 +
  43.104 +/* Macros to declare and load/save bitread local variables. */
  43.105 +#define BITREAD_STATE_VARS  \
  43.106 +	register bit_buf_type get_buffer;  \
  43.107 +	register int bits_left;  \
  43.108 +	bitread_working_state br_state
  43.109 +
  43.110 +#define BITREAD_LOAD_STATE(cinfop,permstate)  \
  43.111 +	br_state.cinfo = cinfop; \
  43.112 +	br_state.next_input_byte = cinfop->src->next_input_byte; \
  43.113 +	br_state.bytes_in_buffer = cinfop->src->bytes_in_buffer; \
  43.114 +	get_buffer = permstate.get_buffer; \
  43.115 +	bits_left = permstate.bits_left;
  43.116 +
  43.117 +#define BITREAD_SAVE_STATE(cinfop,permstate)  \
  43.118 +	cinfop->src->next_input_byte = br_state.next_input_byte; \
  43.119 +	cinfop->src->bytes_in_buffer = br_state.bytes_in_buffer; \
  43.120 +	permstate.get_buffer = get_buffer; \
  43.121 +	permstate.bits_left = bits_left
  43.122 +
  43.123 +/*
  43.124 + * These macros provide the in-line portion of bit fetching.
  43.125 + * Use CHECK_BIT_BUFFER to ensure there are N bits in get_buffer
  43.126 + * before using GET_BITS, PEEK_BITS, or DROP_BITS.
  43.127 + * The variables get_buffer and bits_left are assumed to be locals,
  43.128 + * but the state struct might not be (jpeg_huff_decode needs this).
  43.129 + *	CHECK_BIT_BUFFER(state,n,action);
  43.130 + *		Ensure there are N bits in get_buffer; if suspend, take action.
  43.131 + *      val = GET_BITS(n);
  43.132 + *		Fetch next N bits.
  43.133 + *      val = PEEK_BITS(n);
  43.134 + *		Fetch next N bits without removing them from the buffer.
  43.135 + *	DROP_BITS(n);
  43.136 + *		Discard next N bits.
  43.137 + * The value N should be a simple variable, not an expression, because it
  43.138 + * is evaluated multiple times.
  43.139 + */
  43.140 +
  43.141 +#define CHECK_BIT_BUFFER(state,nbits,action) \
  43.142 +	{ if (bits_left < (nbits)) {  \
  43.143 +	    if (! jpeg_fill_bit_buffer(&(state),get_buffer,bits_left,nbits))  \
  43.144 +	      { action; }  \
  43.145 +	    get_buffer = (state).get_buffer; bits_left = (state).bits_left; } }
  43.146 +
  43.147 +#define GET_BITS(nbits) \
  43.148 +	(((int) (get_buffer >> (bits_left -= (nbits)))) & ((1<<(nbits))-1))
  43.149 +
  43.150 +#define PEEK_BITS(nbits) \
  43.151 +	(((int) (get_buffer >> (bits_left -  (nbits)))) & ((1<<(nbits))-1))
  43.152 +
  43.153 +#define DROP_BITS(nbits) \
  43.154 +	(bits_left -= (nbits))
  43.155 +
  43.156 +/* Load up the bit buffer to a depth of at least nbits */
  43.157 +EXTERN(boolean) jpeg_fill_bit_buffer
  43.158 +	JPP((bitread_working_state * state, register bit_buf_type get_buffer,
  43.159 +	     register int bits_left, int nbits));
  43.160 +
  43.161 +
  43.162 +/*
  43.163 + * Code for extracting next Huffman-coded symbol from input bit stream.
  43.164 + * Again, this is time-critical and we make the main paths be macros.
  43.165 + *
  43.166 + * We use a lookahead table to process codes of up to HUFF_LOOKAHEAD bits
  43.167 + * without looping.  Usually, more than 95% of the Huffman codes will be 8
  43.168 + * or fewer bits long.  The few overlength codes are handled with a loop,
  43.169 + * which need not be inline code.
  43.170 + *
  43.171 + * Notes about the HUFF_DECODE macro:
  43.172 + * 1. Near the end of the data segment, we may fail to get enough bits
  43.173 + *    for a lookahead.  In that case, we do it the hard way.
  43.174 + * 2. If the lookahead table contains no entry, the next code must be
  43.175 + *    more than HUFF_LOOKAHEAD bits long.
  43.176 + * 3. jpeg_huff_decode returns -1 if forced to suspend.
  43.177 + */
  43.178 +
  43.179 +#define HUFF_DECODE(result,state,htbl,failaction,slowlabel) \
  43.180 +{ register int nb, look; \
  43.181 +  if (bits_left < HUFF_LOOKAHEAD) { \
  43.182 +    if (! jpeg_fill_bit_buffer(&state,get_buffer,bits_left, 0)) {failaction;} \
  43.183 +    get_buffer = state.get_buffer; bits_left = state.bits_left; \
  43.184 +    if (bits_left < HUFF_LOOKAHEAD) { \
  43.185 +      nb = 1; goto slowlabel; \
  43.186 +    } \
  43.187 +  } \
  43.188 +  look = PEEK_BITS(HUFF_LOOKAHEAD); \
  43.189 +  if ((nb = htbl->look_nbits[look]) != 0) { \
  43.190 +    DROP_BITS(nb); \
  43.191 +    result = htbl->look_sym[look]; \
  43.192 +  } else { \
  43.193 +    nb = HUFF_LOOKAHEAD+1; \
  43.194 +slowlabel: \
  43.195 +    if ((result=jpeg_huff_decode(&state,get_buffer,bits_left,htbl,nb)) < 0) \
  43.196 +	{ failaction; } \
  43.197 +    get_buffer = state.get_buffer; bits_left = state.bits_left; \
  43.198 +  } \
  43.199 +}
  43.200 +
  43.201 +/* Out-of-line case for Huffman code fetching */
  43.202 +EXTERN(int) jpeg_huff_decode
  43.203 +	JPP((bitread_working_state * state, register bit_buf_type get_buffer,
  43.204 +	     register int bits_left, d_derived_tbl * htbl, int min_bits));
    44.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    44.2 +++ b/libs/libjpeg/jdinput.c	Sat Sep 19 05:51:51 2015 +0300
    44.3 @@ -0,0 +1,381 @@
    44.4 +/*
    44.5 + * jdinput.c
    44.6 + *
    44.7 + * Copyright (C) 1991-1997, Thomas G. Lane.
    44.8 + * This file is part of the Independent JPEG Group's software.
    44.9 + * For conditions of distribution and use, see the accompanying README file.
   44.10 + *
   44.11 + * This file contains input control logic for the JPEG decompressor.
   44.12 + * These routines are concerned with controlling the decompressor's input
   44.13 + * processing (marker reading and coefficient decoding).  The actual input
   44.14 + * reading is done in jdmarker.c, jdhuff.c, and jdphuff.c.
   44.15 + */
   44.16 +
   44.17 +#define JPEG_INTERNALS
   44.18 +#include "jinclude.h"
   44.19 +#include "jpeglib.h"
   44.20 +
   44.21 +
   44.22 +/* Private state */
   44.23 +
   44.24 +typedef struct {
   44.25 +  struct jpeg_input_controller pub; /* public fields */
   44.26 +
   44.27 +  boolean inheaders;		/* TRUE until first SOS is reached */
   44.28 +} my_input_controller;
   44.29 +
   44.30 +typedef my_input_controller * my_inputctl_ptr;
   44.31 +
   44.32 +
   44.33 +/* Forward declarations */
   44.34 +METHODDEF(int) consume_markers JPP((j_decompress_ptr cinfo));
   44.35 +
   44.36 +
   44.37 +/*
   44.38 + * Routines to calculate various quantities related to the size of the image.
   44.39 + */
   44.40 +
   44.41 +LOCAL(void)
   44.42 +initial_setup (j_decompress_ptr cinfo)
   44.43 +/* Called once, when first SOS marker is reached */
   44.44 +{
   44.45 +  int ci;
   44.46 +  jpeg_component_info *compptr;
   44.47 +
   44.48 +  /* Make sure image isn't bigger than I can handle */
   44.49 +  if ((long) cinfo->image_height > (long) JPEG_MAX_DIMENSION ||
   44.50 +      (long) cinfo->image_width > (long) JPEG_MAX_DIMENSION)
   44.51 +    ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION);
   44.52 +
   44.53 +  /* For now, precision must match compiled-in value... */
   44.54 +  if (cinfo->data_precision != BITS_IN_JSAMPLE)
   44.55 +    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
   44.56 +
   44.57 +  /* Check that number of components won't exceed internal array sizes */
   44.58 +  if (cinfo->num_components > MAX_COMPONENTS)
   44.59 +    ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
   44.60 +	     MAX_COMPONENTS);
   44.61 +
   44.62 +  /* Compute maximum sampling factors; check factor validity */
   44.63 +  cinfo->max_h_samp_factor = 1;
   44.64 +  cinfo->max_v_samp_factor = 1;
   44.65 +  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
   44.66 +       ci++, compptr++) {
   44.67 +    if (compptr->h_samp_factor<=0 || compptr->h_samp_factor>MAX_SAMP_FACTOR ||
   44.68 +	compptr->v_samp_factor<=0 || compptr->v_samp_factor>MAX_SAMP_FACTOR)
   44.69 +      ERREXIT(cinfo, JERR_BAD_SAMPLING);
   44.70 +    cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor,
   44.71 +				   compptr->h_samp_factor);
   44.72 +    cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor,
   44.73 +				   compptr->v_samp_factor);
   44.74 +  }
   44.75 +
   44.76 +  /* We initialize DCT_scaled_size and min_DCT_scaled_size to DCTSIZE.
   44.77 +   * In the full decompressor, this will be overridden by jdmaster.c;
   44.78 +   * but in the transcoder, jdmaster.c is not used, so we must do it here.
   44.79 +   */
   44.80 +  cinfo->min_DCT_scaled_size = DCTSIZE;
   44.81 +
   44.82 +  /* Compute dimensions of components */
   44.83 +  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
   44.84 +       ci++, compptr++) {
   44.85 +    compptr->DCT_scaled_size = DCTSIZE;
   44.86 +    /* Size in DCT blocks */
   44.87 +    compptr->width_in_blocks = (JDIMENSION)
   44.88 +      jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
   44.89 +		    (long) (cinfo->max_h_samp_factor * DCTSIZE));
   44.90 +    compptr->height_in_blocks = (JDIMENSION)
   44.91 +      jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
   44.92 +		    (long) (cinfo->max_v_samp_factor * DCTSIZE));
   44.93 +    /* downsampled_width and downsampled_height will also be overridden by
   44.94 +     * jdmaster.c if we are doing full decompression.  The transcoder library
   44.95 +     * doesn't use these values, but the calling application might.
   44.96 +     */
   44.97 +    /* Size in samples */
   44.98 +    compptr->downsampled_width = (JDIMENSION)
   44.99 +      jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
  44.100 +		    (long) cinfo->max_h_samp_factor);
  44.101 +    compptr->downsampled_height = (JDIMENSION)
  44.102 +      jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
  44.103 +		    (long) cinfo->max_v_samp_factor);
  44.104 +    /* Mark component needed, until color conversion says otherwise */
  44.105 +    compptr->component_needed = TRUE;
  44.106 +    /* Mark no quantization table yet saved for component */
  44.107 +    compptr->quant_table = NULL;
  44.108 +  }
  44.109 +
  44.110 +  /* Compute number of fully interleaved MCU rows. */
  44.111 +  cinfo->total_iMCU_rows = (JDIMENSION)
  44.112 +    jdiv_round_up((long) cinfo->image_height,
  44.113 +		  (long) (cinfo->max_v_samp_factor*DCTSIZE));
  44.114 +
  44.115 +  /* Decide whether file contains multiple scans */
  44.116 +  if (cinfo->comps_in_scan < cinfo->num_components || cinfo->progressive_mode)
  44.117 +    cinfo->inputctl->has_multiple_scans = TRUE;
  44.118 +  else
  44.119 +    cinfo->inputctl->has_multiple_scans = FALSE;
  44.120 +}
  44.121 +
  44.122 +
  44.123 +LOCAL(void)
  44.124 +per_scan_setup (j_decompress_ptr cinfo)
  44.125 +/* Do computations that are needed before processing a JPEG scan */
  44.126 +/* cinfo->comps_in_scan and cinfo->cur_comp_info[] were set from SOS marker */
  44.127 +{
  44.128 +  int ci, mcublks, tmp;
  44.129 +  jpeg_component_info *compptr;
  44.130 +  
  44.131 +  if (cinfo->comps_in_scan == 1) {
  44.132 +    
  44.133 +    /* Noninterleaved (single-component) scan */
  44.134 +    compptr = cinfo->cur_comp_info[0];
  44.135 +    
  44.136 +    /* Overall image size in MCUs */
  44.137 +    cinfo->MCUs_per_row = compptr->width_in_blocks;
  44.138 +    cinfo->MCU_rows_in_scan = compptr->height_in_blocks;
  44.139 +    
  44.140 +    /* For noninterleaved scan, always one block per MCU */
  44.141 +    compptr->MCU_width = 1;
  44.142 +    compptr->MCU_height = 1;
  44.143 +    compptr->MCU_blocks = 1;
  44.144 +    compptr->MCU_sample_width = compptr->DCT_scaled_size;
  44.145 +    compptr->last_col_width = 1;
  44.146 +    /* For noninterleaved scans, it is convenient to define last_row_height
  44.147 +     * as the number of block rows present in the last iMCU row.
  44.148 +     */
  44.149 +    tmp = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
  44.150 +    if (tmp == 0) tmp = compptr->v_samp_factor;
  44.151 +    compptr->last_row_height = tmp;
  44.152 +    
  44.153 +    /* Prepare array describing MCU composition */
  44.154 +    cinfo->blocks_in_MCU = 1;
  44.155 +    cinfo->MCU_membership[0] = 0;
  44.156 +    
  44.157 +  } else {
  44.158 +    
  44.159 +    /* Interleaved (multi-component) scan */
  44.160 +    if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN)
  44.161 +      ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan,
  44.162 +	       MAX_COMPS_IN_SCAN);
  44.163 +    
  44.164 +    /* Overall image size in MCUs */
  44.165 +    cinfo->MCUs_per_row = (JDIMENSION)
  44.166 +      jdiv_round_up((long) cinfo->image_width,
  44.167 +		    (long) (cinfo->max_h_samp_factor*DCTSIZE));
  44.168 +    cinfo->MCU_rows_in_scan = (JDIMENSION)
  44.169 +      jdiv_round_up((long) cinfo->image_height,
  44.170 +		    (long) (cinfo->max_v_samp_factor*DCTSIZE));
  44.171 +    
  44.172 +    cinfo->blocks_in_MCU = 0;
  44.173 +    
  44.174 +    for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  44.175 +      compptr = cinfo->cur_comp_info[ci];
  44.176 +      /* Sampling factors give # of blocks of component in each MCU */
  44.177 +      compptr->MCU_width = compptr->h_samp_factor;
  44.178 +      compptr->MCU_height = compptr->v_samp_factor;
  44.179 +      compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height;
  44.180 +      compptr->MCU_sample_width = compptr->MCU_width * compptr->DCT_scaled_size;
  44.181 +      /* Figure number of non-dummy blocks in last MCU column & row */
  44.182 +      tmp = (int) (compptr->width_in_blocks % compptr->MCU_width);
  44.183 +      if (tmp == 0) tmp = compptr->MCU_width;
  44.184 +      compptr->last_col_width = tmp;
  44.185 +      tmp = (int) (compptr->height_in_blocks % compptr->MCU_height);
  44.186 +      if (tmp == 0) tmp = compptr->MCU_height;
  44.187 +      compptr->last_row_height = tmp;
  44.188 +      /* Prepare array describing MCU composition */
  44.189 +      mcublks = compptr->MCU_blocks;
  44.190 +      if (cinfo->blocks_in_MCU + mcublks > D_MAX_BLOCKS_IN_MCU)
  44.191 +	ERREXIT(cinfo, JERR_BAD_MCU_SIZE);
  44.192 +      while (mcublks-- > 0) {
  44.193 +	cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci;
  44.194 +      }
  44.195 +    }
  44.196 +    
  44.197 +  }
  44.198 +}
  44.199 +
  44.200 +
  44.201 +/*
  44.202 + * Save away a copy of the Q-table referenced by each component present
  44.203 + * in the current scan, unless already saved during a prior scan.
  44.204 + *
  44.205 + * In a multiple-scan JPEG file, the encoder could assign different components
  44.206 + * the same Q-table slot number, but change table definitions between scans
  44.207 + * so that each component uses a different Q-table.  (The IJG encoder is not
  44.208 + * currently capable of doing this, but other encoders might.)  Since we want
  44.209 + * to be able to dequantize all the components at the end of the file, this
  44.210 + * means that we have to save away the table actually used for each component.
  44.211 + * We do this by copying the table at the start of the first scan containing
  44.212 + * the component.
  44.213 + * The JPEG spec prohibits the encoder from changing the contents of a Q-table
  44.214 + * slot between scans of a component using that slot.  If the encoder does so
  44.215 + * anyway, this decoder will simply use the Q-table values that were current
  44.216 + * at the start of the first scan for the component.
  44.217 + *
  44.218 + * The decompressor output side looks only at the saved quant tables,
  44.219 + * not at the current Q-table slots.
  44.220 + */
  44.221 +
  44.222 +LOCAL(void)
  44.223 +latch_quant_tables (j_decompress_ptr cinfo)
  44.224 +{
  44.225 +  int ci, qtblno;
  44.226 +  jpeg_component_info *compptr;
  44.227 +  JQUANT_TBL * qtbl;
  44.228 +
  44.229 +  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  44.230 +    compptr = cinfo->cur_comp_info[ci];
  44.231 +    /* No work if we already saved Q-table for this component */
  44.232 +    if (compptr->quant_table != NULL)
  44.233 +      continue;
  44.234 +    /* Make sure specified quantization table is present */
  44.235 +    qtblno = compptr->quant_tbl_no;
  44.236 +    if (qtblno < 0 || qtblno >= NUM_QUANT_TBLS ||
  44.237 +	cinfo->quant_tbl_ptrs[qtblno] == NULL)
  44.238 +      ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, qtblno);
  44.239 +    /* OK, save away the quantization table */
  44.240 +    qtbl = (JQUANT_TBL *)
  44.241 +      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  44.242 +				  SIZEOF(JQUANT_TBL));
  44.243 +    MEMCOPY(qtbl, cinfo->quant_tbl_ptrs[qtblno], SIZEOF(JQUANT_TBL));
  44.244 +    compptr->quant_table = qtbl;
  44.245 +  }
  44.246 +}
  44.247 +
  44.248 +
  44.249 +/*
  44.250 + * Initialize the input modules to read a scan of compressed data.
  44.251 + * The first call to this is done by jdmaster.c after initializing
  44.252 + * the entire decompressor (during jpeg_start_decompress).
  44.253 + * Subsequent calls come from consume_markers, below.
  44.254 + */
  44.255 +
  44.256 +METHODDEF(void)
  44.257 +start_input_pass (j_decompress_ptr cinfo)
  44.258 +{
  44.259 +  per_scan_setup(cinfo);
  44.260 +  latch_quant_tables(cinfo);
  44.261 +  (*cinfo->entropy->start_pass) (cinfo);
  44.262 +  (*cinfo->coef->start_input_pass) (cinfo);
  44.263 +  cinfo->inputctl->consume_input = cinfo->coef->consume_data;
  44.264 +}
  44.265 +
  44.266 +
  44.267 +/*
  44.268 + * Finish up after inputting a compressed-data scan.
  44.269 + * This is called by the coefficient controller after it's read all
  44.270 + * the expected data of the scan.
  44.271 + */
  44.272 +
  44.273 +METHODDEF(void)
  44.274 +finish_input_pass (j_decompress_ptr cinfo)
  44.275 +{
  44.276 +  cinfo->inputctl->consume_input = consume_markers;
  44.277 +}
  44.278 +
  44.279 +
  44.280 +/*
  44.281 + * Read JPEG markers before, between, or after compressed-data scans.
  44.282 + * Change state as necessary when a new scan is reached.
  44.283 + * Return value is JPEG_SUSPENDED, JPEG_REACHED_SOS, or JPEG_REACHED_EOI.
  44.284 + *
  44.285 + * The consume_input method pointer points either here or to the
  44.286 + * coefficient controller's consume_data routine, depending on whether
  44.287 + * we are reading a compressed data segment or inter-segment markers.
  44.288 + */
  44.289 +
  44.290 +METHODDEF(int)
  44.291 +consume_markers (j_decompress_ptr cinfo)
  44.292 +{
  44.293 +  my_inputctl_ptr inputctl = (my_inputctl_ptr) cinfo->inputctl;
  44.294 +  int val;
  44.295 +
  44.296 +  if (inputctl->pub.eoi_reached) /* After hitting EOI, read no further */
  44.297 +    return JPEG_REACHED_EOI;
  44.298 +
  44.299 +  val = (*cinfo->marker->read_markers) (cinfo);
  44.300 +
  44.301 +  switch (val) {
  44.302 +  case JPEG_REACHED_SOS:	/* Found SOS */
  44.303 +    if (inputctl->inheaders) {	/* 1st SOS */
  44.304 +      initial_setup(cinfo);
  44.305 +      inputctl->inheaders = FALSE;
  44.306 +      /* Note: start_input_pass must be called by jdmaster.c
  44.307 +       * before any more input can be consumed.  jdapimin.c is
  44.308 +       * responsible for enforcing this sequencing.
  44.309 +       */
  44.310 +    } else {			/* 2nd or later SOS marker */
  44.311 +      if (! inputctl->pub.has_multiple_scans)
  44.312 +	ERREXIT(cinfo, JERR_EOI_EXPECTED); /* Oops, I wasn't expecting this! */
  44.313 +      start_input_pass(cinfo);
  44.314 +    }
  44.315 +    break;
  44.316 +  case JPEG_REACHED_EOI:	/* Found EOI */
  44.317 +    inputctl->pub.eoi_reached = TRUE;
  44.318 +    if (inputctl->inheaders) {	/* Tables-only datastream, apparently */
  44.319 +      if (cinfo->marker->saw_SOF)
  44.320 +	ERREXIT(cinfo, JERR_SOF_NO_SOS);
  44.321 +    } else {
  44.322 +      /* Prevent infinite loop in coef ctlr's decompress_data routine
  44.323 +       * if user set output_scan_number larger than number of scans.
  44.324 +       */
  44.325 +      if (cinfo->output_scan_number > cinfo->input_scan_number)
  44.326 +	cinfo->output_scan_number = cinfo->input_scan_number;
  44.327 +    }
  44.328 +    break;
  44.329 +  case JPEG_SUSPENDED:
  44.330 +    break;
  44.331 +  }
  44.332 +
  44.333 +  return val;
  44.334 +}
  44.335 +
  44.336 +
  44.337 +/*
  44.338 + * Reset state to begin a fresh datastream.
  44.339 + */
  44.340 +
  44.341 +METHODDEF(void)
  44.342 +reset_input_controller (j_decompress_ptr cinfo)
  44.343 +{
  44.344 +  my_inputctl_ptr inputctl = (my_inputctl_ptr) cinfo->inputctl;
  44.345 +
  44.346 +  inputctl->pub.consume_input = consume_markers;
  44.347 +  inputctl->pub.has_multiple_scans = FALSE; /* "unknown" would be better */
  44.348 +  inputctl->pub.eoi_reached = FALSE;
  44.349 +  inputctl->inheaders = TRUE;
  44.350 +  /* Reset other modules */
  44.351 +  (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);
  44.352 +  (*cinfo->marker->reset_marker_reader) (cinfo);
  44.353 +  /* Reset progression state -- would be cleaner if entropy decoder did this */
  44.354 +  cinfo->coef_bits = NULL;
  44.355 +}
  44.356 +
  44.357 +
  44.358 +/*
  44.359 + * Initialize the input controller module.
  44.360 + * This is called only once, when the decompression object is created.
  44.361 + */
  44.362 +
  44.363 +GLOBAL(void)
  44.364 +jinit_input_controller (j_decompress_ptr cinfo)
  44.365 +{
  44.366 +  my_inputctl_ptr inputctl;
  44.367 +
  44.368 +  /* Create subobject in permanent pool */
  44.369 +  inputctl = (my_inputctl_ptr)
  44.370 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
  44.371 +				SIZEOF(my_input_controller));
  44.372 +  cinfo->inputctl = (struct jpeg_input_controller *) inputctl;
  44.373 +  /* Initialize method pointers */
  44.374 +  inputctl->pub.consume_input = consume_markers;
  44.375 +  inputctl->pub.reset_input_controller = reset_input_controller;
  44.376 +  inputctl->pub.start_input_pass = start_input_pass;
  44.377 +  inputctl->pub.finish_input_pass = finish_input_pass;
  44.378 +  /* Initialize state: can't use reset_input_controller since we don't
  44.379 +   * want to try to reset other modules yet.
  44.380 +   */
  44.381 +  inputctl->pub.has_multiple_scans = FALSE; /* "unknown" would be better */
  44.382 +  inputctl->pub.eoi_reached = FALSE;
  44.383 +  inputctl->inheaders = TRUE;
  44.384 +}
    45.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    45.2 +++ b/libs/libjpeg/jdmainct.c	Sat Sep 19 05:51:51 2015 +0300
    45.3 @@ -0,0 +1,512 @@
    45.4 +/*
    45.5 + * jdmainct.c
    45.6 + *
    45.7 + * Copyright (C) 1994-1996, Thomas G. Lane.
    45.8 + * This file is part of the Independent JPEG Group's software.
    45.9 + * For conditions of distribution and use, see the accompanying README file.
   45.10 + *
   45.11 + * This file contains the main buffer controller for decompression.
   45.12 + * The main buffer lies between the JPEG decompressor proper and the
   45.13 + * post-processor; it holds downsampled data in the JPEG colorspace.
   45.14 + *
   45.15 + * Note that this code is bypassed in raw-data mode, since the application
   45.16 + * supplies the equivalent of the main buffer in that case.
   45.17 + */
   45.18 +
   45.19 +#define JPEG_INTERNALS
   45.20 +#include "jinclude.h"
   45.21 +#include "jpeglib.h"
   45.22 +
   45.23 +
   45.24 +/*
   45.25 + * In the current system design, the main buffer need never be a full-image
   45.26 + * buffer; any full-height buffers will be found inside the coefficient or
   45.27 + * postprocessing controllers.  Nonetheless, the main controller is not
   45.28 + * trivial.  Its responsibility is to provide context rows for upsampling/
   45.29 + * rescaling, and doing this in an efficient fashion is a bit tricky.
   45.30 + *
   45.31 + * Postprocessor input data is counted in "row groups".  A row group
   45.32 + * is defined to be (v_samp_factor * DCT_scaled_size / min_DCT_scaled_size)
   45.33 + * sample rows of each component.  (We require DCT_scaled_size values to be
   45.34 + * chosen such that these numbers are integers.  In practice DCT_scaled_size
   45.35 + * values will likely be powers of two, so we actually have the stronger
   45.36 + * condition that DCT_scaled_size / min_DCT_scaled_size is an integer.)
   45.37 + * Upsampling will typically produce max_v_samp_factor pixel rows from each
   45.38 + * row group (times any additional scale factor that the upsampler is
   45.39 + * applying).
   45.40 + *
   45.41 + * The coefficient controller will deliver data to us one iMCU row at a time;
   45.42 + * each iMCU row contains v_samp_factor * DCT_scaled_size sample rows, or
   45.43 + * exactly min_DCT_scaled_size row groups.  (This amount of data corresponds
   45.44 + * to one row of MCUs when the image is fully interleaved.)  Note that the
   45.45 + * number of sample rows varies across components, but the number of row
   45.46 + * groups does not.  Some garbage sample rows may be included in the last iMCU
   45.47 + * row at the bottom of the image.
   45.48 + *
   45.49 + * Depending on the vertical scaling algorithm used, the upsampler may need
   45.50 + * access to the sample row(s) above and below its current input row group.
   45.51 + * The upsampler is required to set need_context_rows TRUE at global selection
   45.52 + * time if so.  When need_context_rows is FALSE, this controller can simply
   45.53 + * obtain one iMCU row at a time from the coefficient controller and dole it
   45.54 + * out as row groups to the postprocessor.
   45.55 + *
   45.56 + * When need_context_rows is TRUE, this controller guarantees that the buffer
   45.57 + * passed to postprocessing contains at least one row group's worth of samples
   45.58 + * above and below the row group(s) being processed.  Note that the context
   45.59 + * rows "above" the first passed row group appear at negative row offsets in
   45.60 + * the passed buffer.  At the top and bottom of the image, the required
   45.61 + * context rows are manufactured by duplicating the first or last real sample
   45.62 + * row; this avoids having special cases in the upsampling inner loops.
   45.63 + *
   45.64 + * The amount of context is fixed at one row group just because that's a
   45.65 + * convenient number for this controller to work with.  The existing
   45.66 + * upsamplers really only need one sample row of context.  An upsampler
   45.67 + * supporting arbitrary output rescaling might wish for more than one row
   45.68 + * group of context when shrinking the image; tough, we don't handle that.
   45.69 + * (This is justified by the assumption that downsizing will be handled mostly
   45.70 + * by adjusting the DCT_scaled_size values, so that the actual scale factor at
   45.71 + * the upsample step needn't be much less than one.)
   45.72 + *
   45.73 + * To provide the desired context, we have to retain the last two row groups
   45.74 + * of one iMCU row while reading in the next iMCU row.  (The last row group
   45.75 + * can't be processed until we have another row group for its below-context,
   45.76 + * and so we have to save the next-to-last group too for its above-context.)
   45.77 + * We could do this most simply by copying data around in our buffer, but
   45.78 + * that'd be very slow.  We can avoid copying any data by creating a rather
   45.79 + * strange pointer structure.  Here's how it works.  We allocate a workspace
   45.80 + * consisting of M+2 row groups (where M = min_DCT_scaled_size is the number
   45.81 + * of row groups per iMCU row).  We create two sets of redundant pointers to
   45.82 + * the workspace.  Labeling the physical row groups 0 to M+1, the synthesized
   45.83 + * pointer lists look like this:
   45.84 + *                   M+1                          M-1
   45.85 + * master pointer --> 0         master pointer --> 0
   45.86 + *                    1                            1
   45.87 + *                   ...                          ...
   45.88 + *                   M-3                          M-3
   45.89 + *                   M-2                           M
   45.90 + *                   M-1                          M+1
   45.91 + *                    M                           M-2
   45.92 + *                   M+1                          M-1
   45.93 + *                    0                            0
   45.94 + * We read alternate iMCU rows using each master pointer; thus the last two
   45.95 + * row groups of the previous iMCU row remain un-overwritten in the workspace.
   45.96 + * The pointer lists are set up so that the required context rows appear to
   45.97 + * be adjacent to the proper places when we pass the pointer lists to the
   45.98 + * upsampler.
   45.99 + *
  45.100 + * The above pictures describe the normal state of the pointer lists.
  45.101 + * At top and bottom of the image, we diddle the pointer lists to duplicate
  45.102 + * the first or last sample row as necessary (this is cheaper than copying
  45.103 + * sample rows around).
  45.104 + *
  45.105 + * This scheme breaks down if M < 2, ie, min_DCT_scaled_size is 1.  In that
  45.106 + * situation each iMCU row provides only one row group so the buffering logic
  45.107 + * must be different (eg, we must read two iMCU rows before we can emit the
  45.108 + * first row group).  For now, we simply do not support providing context
  45.109 + * rows when min_DCT_scaled_size is 1.  That combination seems unlikely to
  45.110 + * be worth providing --- if someone wants a 1/8th-size preview, they probably
  45.111 + * want it quick and dirty, so a context-free upsampler is sufficient.
  45.112 + */
  45.113 +
  45.114 +
  45.115 +/* Private buffer controller object */
  45.116 +
  45.117 +typedef struct {
  45.118 +  struct jpeg_d_main_controller pub; /* public fields */
  45.119 +
  45.120 +  /* Pointer to allocated workspace (M or M+2 row groups). */
  45.121 +  JSAMPARRAY buffer[MAX_COMPONENTS];
  45.122 +
  45.123 +  boolean buffer_full;		/* Have we gotten an iMCU row from decoder? */
  45.124 +  JDIMENSION rowgroup_ctr;	/* counts row groups output to postprocessor */
  45.125 +
  45.126 +  /* Remaining fields are only used in the context case. */
  45.127 +
  45.128 +  /* These are the master pointers to the funny-order pointer lists. */
  45.129 +  JSAMPIMAGE xbuffer[2];	/* pointers to weird pointer lists */
  45.130 +
  45.131 +  int whichptr;			/* indicates which pointer set is now in use */
  45.132 +  int context_state;		/* process_data state machine status */
  45.133 +  JDIMENSION rowgroups_avail;	/* row groups available to postprocessor */
  45.134 +  JDIMENSION iMCU_row_ctr;	/* counts iMCU rows to detect image top/bot */
  45.135 +} my_main_controller;
  45.136 +
  45.137 +typedef my_main_controller * my_main_ptr;
  45.138 +
  45.139 +/* context_state values: */
  45.140 +#define CTX_PREPARE_FOR_IMCU	0	/* need to prepare for MCU row */
  45.141 +#define CTX_PROCESS_IMCU	1	/* feeding iMCU to postprocessor */
  45.142 +#define CTX_POSTPONED_ROW	2	/* feeding postponed row group */
  45.143 +
  45.144 +
  45.145 +/* Forward declarations */
  45.146 +METHODDEF(void) process_data_simple_main
  45.147 +	JPP((j_decompress_ptr cinfo, JSAMPARRAY output_buf,
  45.148 +	     JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail));
  45.149 +METHODDEF(void) process_data_context_main
  45.150 +	JPP((j_decompress_ptr cinfo, JSAMPARRAY output_buf,
  45.151 +	     JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail));
  45.152 +#ifdef QUANT_2PASS_SUPPORTED
  45.153 +METHODDEF(void) process_data_crank_post
  45.154 +	JPP((j_decompress_ptr cinfo, JSAMPARRAY output_buf,
  45.155 +	     JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail));
  45.156 +#endif
  45.157 +
  45.158 +
  45.159 +LOCAL(void)
  45.160 +alloc_funny_pointers (j_decompress_ptr cinfo)
  45.161 +/* Allocate space for the funny pointer lists.
  45.162 + * This is done only once, not once per pass.
  45.163 + */
  45.164 +{
  45.165 +  my_main_ptr main = (my_main_ptr) cinfo->main;
  45.166 +  int ci, rgroup;
  45.167 +  int M = cinfo->min_DCT_scaled_size;
  45.168 +  jpeg_component_info *compptr;
  45.169 +  JSAMPARRAY xbuf;
  45.170 +
  45.171 +  /* Get top-level space for component array pointers.
  45.172 +   * We alloc both arrays with one call to save a few cycles.
  45.173 +   */
  45.174 +  main->xbuffer[0] = (JSAMPIMAGE)
  45.175 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  45.176 +				cinfo->num_components * 2 * SIZEOF(JSAMPARRAY));
  45.177 +  main->xbuffer[1] = main->xbuffer[0] + cinfo->num_components;
  45.178 +
  45.179 +  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  45.180 +       ci++, compptr++) {
  45.181 +    rgroup = (compptr->v_samp_factor * compptr->DCT_scaled_size) /
  45.182 +      cinfo->min_DCT_scaled_size; /* height of a row group of component */
  45.183 +    /* Get space for pointer lists --- M+4 row groups in each list.
  45.184 +     * We alloc both pointer lists with one call to save a few cycles.
  45.185 +     */
  45.186 +    xbuf = (JSAMPARRAY)
  45.187 +      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  45.188 +				  2 * (rgroup * (M + 4)) * SIZEOF(JSAMPROW));
  45.189 +    xbuf += rgroup;		/* want one row group at negative offsets */
  45.190 +    main->xbuffer[0][ci] = xbuf;
  45.191 +    xbuf += rgroup * (M + 4);
  45.192 +    main->xbuffer[1][ci] = xbuf;
  45.193 +  }
  45.194 +}
  45.195 +
  45.196 +
  45.197 +LOCAL(void)
  45.198 +make_funny_pointers (j_decompress_ptr cinfo)
  45.199 +/* Create the funny pointer lists discussed in the comments above.
  45.200 + * The actual workspace is already allocated (in main->buffer),
  45.201 + * and the space for the pointer lists is allocated too.
  45.202 + * This routine just fills in the curiously ordered lists.
  45.203 + * This will be repeated at the beginning of each pass.
  45.204 + */
  45.205 +{
  45.206 +  my_main_ptr main = (my_main_ptr) cinfo->main;
  45.207 +  int ci, i, rgroup;
  45.208 +  int M = cinfo->min_DCT_scaled_size;
  45.209 +  jpeg_component_info *compptr;
  45.210 +  JSAMPARRAY buf, xbuf0, xbuf1;
  45.211 +
  45.212 +  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  45.213 +       ci++, compptr++) {
  45.214 +    rgroup = (compptr->v_samp_factor * compptr->DCT_scaled_size) /
  45.215 +      cinfo->min_DCT_scaled_size; /* height of a row group of component */
  45.216 +    xbuf0 = main->xbuffer[0][ci];
  45.217 +    xbuf1 = main->xbuffer[1][ci];
  45.218 +    /* First copy the workspace pointers as-is */
  45.219 +    buf = main->buffer[ci];
  45.220 +    for (i = 0; i < rgroup * (M + 2); i++) {
  45.221 +      xbuf0[i] = xbuf1[i] = buf[i];
  45.222 +    }
  45.223 +    /* In the second list, put the last four row groups in swapped order */
  45.224 +    for (i = 0; i < rgroup * 2; i++) {
  45.225 +      xbuf1[rgroup*(M-2) + i] = buf[rgroup*M + i];
  45.226 +      xbuf1[rgroup*M + i] = buf[rgroup*(M-2) + i];
  45.227 +    }
  45.228 +    /* The wraparound pointers at top and bottom will be filled later
  45.229 +     * (see set_wraparound_pointers, below).  Initially we want the "above"
  45.230 +     * pointers to duplicate the first actual data line.  This only needs
  45.231 +     * to happen in xbuffer[0].
  45.232 +     */
  45.233 +    for (i = 0; i < rgroup; i++) {
  45.234 +      xbuf0[i - rgroup] = xbuf0[0];
  45.235 +    }
  45.236 +  }
  45.237 +}
  45.238 +
  45.239 +
  45.240 +LOCAL(void)
  45.241 +set_wraparound_pointers (j_decompress_ptr cinfo)
  45.242 +/* Set up the "wraparound" pointers at top and bottom of the pointer lists.
  45.243 + * This changes the pointer list state from top-of-image to the normal state.
  45.244 + */
  45.245 +{
  45.246 +  my_main_ptr main = (my_main_ptr) cinfo->main;
  45.247 +  int ci, i, rgroup;
  45.248 +  int M = cinfo->min_DCT_scaled_size;
  45.249 +  jpeg_component_info *compptr;
  45.250 +  JSAMPARRAY xbuf0, xbuf1;
  45.251 +
  45.252 +  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  45.253 +       ci++, compptr++) {
  45.254 +    rgroup = (compptr->v_samp_factor * compptr->DCT_scaled_size) /
  45.255 +      cinfo->min_DCT_scaled_size; /* height of a row group of component */
  45.256 +    xbuf0 = main->xbuffer[0][ci];
  45.257 +    xbuf1 = main->xbuffer[1][ci];
  45.258 +    for (i = 0; i < rgroup; i++) {
  45.259 +      xbuf0[i - rgroup] = xbuf0[rgroup*(M+1) + i];
  45.260 +      xbuf1[i - rgroup] = xbuf1[rgroup*(M+1) + i];
  45.261 +      xbuf0[rgroup*(M+2) + i] = xbuf0[i];
  45.262 +      xbuf1[rgroup*(M+2) + i] = xbuf1[i];
  45.263 +    }
  45.264 +  }
  45.265 +}
  45.266 +
  45.267 +
  45.268 +LOCAL(void)
  45.269 +set_bottom_pointers (j_decompress_ptr cinfo)
  45.270 +/* Change the pointer lists to duplicate the last sample row at the bottom
  45.271 + * of the image.  whichptr indicates which xbuffer holds the final iMCU row.
  45.272 + * Also sets rowgroups_avail to indicate number of nondummy row groups in row.
  45.273 + */
  45.274 +{
  45.275 +  my_main_ptr main = (my_main_ptr) cinfo->main;
  45.276 +  int ci, i, rgroup, iMCUheight, rows_left;
  45.277 +  jpeg_component_info *compptr;
  45.278 +  JSAMPARRAY xbuf;
  45.279 +
  45.280 +  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  45.281 +       ci++, compptr++) {
  45.282 +    /* Count sample rows in one iMCU row and in one row group */
  45.283 +    iMCUheight = compptr->v_samp_factor * compptr->DCT_scaled_size;
  45.284 +    rgroup = iMCUheight / cinfo->min_DCT_scaled_size;
  45.285 +    /* Count nondummy sample rows remaining for this component */
  45.286 +    rows_left = (int) (compptr->downsampled_height % (JDIMENSION) iMCUheight);
  45.287 +    if (rows_left == 0) rows_left = iMCUheight;
  45.288 +    /* Count nondummy row groups.  Should get same answer for each component,
  45.289 +     * so we need only do it once.
  45.290 +     */
  45.291 +    if (ci == 0) {
  45.292 +      main->rowgroups_avail = (JDIMENSION) ((rows_left-1) / rgroup + 1);
  45.293 +    }
  45.294 +    /* Duplicate the last real sample row rgroup*2 times; this pads out the
  45.295 +     * last partial rowgroup and ensures at least one full rowgroup of context.
  45.296 +     */
  45.297 +    xbuf = main->xbuffer[main->whichptr][ci];
  45.298 +    for (i = 0; i < rgroup * 2; i++) {
  45.299 +      xbuf[rows_left + i] = xbuf[rows_left-1];
  45.300 +    }
  45.301 +  }
  45.302 +}
  45.303 +
  45.304 +
  45.305 +/*
  45.306 + * Initialize for a processing pass.
  45.307 + */
  45.308 +
  45.309 +METHODDEF(void)
  45.310 +start_pass_main (j_decompress_ptr cinfo, J_BUF_MODE pass_mode)
  45.311 +{
  45.312 +  my_main_ptr main = (my_main_ptr) cinfo->main;
  45.313 +
  45.314 +  switch (pass_mode) {
  45.315 +  case JBUF_PASS_THRU:
  45.316 +    if (cinfo->upsample->need_context_rows) {
  45.317 +      main->pub.process_data = process_data_context_main;
  45.318 +      make_funny_pointers(cinfo); /* Create the xbuffer[] lists */
  45.319 +      main->whichptr = 0;	/* Read first iMCU row into xbuffer[0] */
  45.320 +      main->context_state = CTX_PREPARE_FOR_IMCU;
  45.321 +      main->iMCU_row_ctr = 0;
  45.322 +    } else {
  45.323 +      /* Simple case with no context needed */
  45.324 +      main->pub.process_data = process_data_simple_main;
  45.325 +    }
  45.326 +    main->buffer_full = FALSE;	/* Mark buffer empty */
  45.327 +    main->rowgroup_ctr = 0;
  45.328 +    break;
  45.329 +#ifdef QUANT_2PASS_SUPPORTED
  45.330 +  case JBUF_CRANK_DEST:
  45.331 +    /* For last pass of 2-pass quantization, just crank the postprocessor */
  45.332 +    main->pub.process_data = process_data_crank_post;
  45.333 +    break;
  45.334 +#endif
  45.335 +  default:
  45.336 +    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  45.337 +    break;
  45.338 +  }
  45.339 +}
  45.340 +
  45.341 +
  45.342 +/*
  45.343 + * Process some data.
  45.344 + * This handles the simple case where no context is required.
  45.345 + */
  45.346 +
  45.347 +METHODDEF(void)
  45.348 +process_data_simple_main (j_decompress_ptr cinfo,
  45.349 +			  JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
  45.350 +			  JDIMENSION out_rows_avail)
  45.351 +{
  45.352 +  my_main_ptr main = (my_main_ptr) cinfo->main;
  45.353 +  JDIMENSION rowgroups_avail;
  45.354 +
  45.355 +  /* Read input data if we haven't filled the main buffer yet */
  45.356 +  if (! main->buffer_full) {
  45.357 +    if (! (*cinfo->coef->decompress_data) (cinfo, main->buffer))
  45.358 +      return;			/* suspension forced, can do nothing more */
  45.359 +    main->buffer_full = TRUE;	/* OK, we have an iMCU row to work with */
  45.360 +  }
  45.361 +
  45.362 +  /* There are always min_DCT_scaled_size row groups in an iMCU row. */
  45.363 +  rowgroups_avail = (JDIMENSION) cinfo->min_DCT_scaled_size;
  45.364 +  /* Note: at the bottom of the image, we may pass extra garbage row groups
  45.365 +   * to the postprocessor.  The postprocessor has to check for bottom
  45.366 +   * of image anyway (at row resolution), so no point in us doing it too.
  45.367 +   */
  45.368 +
  45.369 +  /* Feed the postprocessor */
  45.370 +  (*cinfo->post->post_process_data) (cinfo, main->buffer,
  45.371 +				     &main->rowgroup_ctr, rowgroups_avail,
  45.372 +				     output_buf, out_row_ctr, out_rows_avail);
  45.373 +
  45.374 +  /* Has postprocessor consumed all the data yet? If so, mark buffer empty */
  45.375 +  if (main->rowgroup_ctr >= rowgroups_avail) {
  45.376 +    main->buffer_full = FALSE;
  45.377 +    main->rowgroup_ctr = 0;
  45.378 +  }
  45.379 +}
  45.380 +
  45.381 +
  45.382 +/*
  45.383 + * Process some data.
  45.384 + * This handles the case where context rows must be provided.
  45.385 + */
  45.386 +
  45.387 +METHODDEF(void)
  45.388 +process_data_context_main (j_decompress_ptr cinfo,
  45.389 +			   JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
  45.390 +			   JDIMENSION out_rows_avail)
  45.391 +{
  45.392 +  my_main_ptr main = (my_main_ptr) cinfo->main;
  45.393 +
  45.394 +  /* Read input data if we haven't filled the main buffer yet */
  45.395 +  if (! main->buffer_full) {
  45.396 +    if (! (*cinfo->coef->decompress_data) (cinfo,
  45.397 +					   main->xbuffer[main->whichptr]))
  45.398 +      return;			/* suspension forced, can do nothing more */
  45.399 +    main->buffer_full = TRUE;	/* OK, we have an iMCU row to work with */
  45.400 +    main->iMCU_row_ctr++;	/* count rows received */
  45.401 +  }
  45.402 +
  45.403 +  /* Postprocessor typically will not swallow all the input data it is handed
  45.404 +   * in one call (due to filling the output buffer first).  Must be prepared
  45.405 +   * to exit and restart.  This switch lets us keep track of how far we got.
  45.406 +   * Note that each case falls through to the next on successful completion.
  45.407 +   */
  45.408 +  switch (main->context_state) {
  45.409 +  case CTX_POSTPONED_ROW:
  45.410 +    /* Call postprocessor using previously set pointers for postponed row */
  45.411 +    (*cinfo->post->post_process_data) (cinfo, main->xbuffer[main->whichptr],
  45.412 +			&main->rowgroup_ctr, main->rowgroups_avail,
  45.413 +			output_buf, out_row_ctr, out_rows_avail);
  45.414 +    if (main->rowgroup_ctr < main->rowgroups_avail)
  45.415 +      return;			/* Need to suspend */
  45.416 +    main->context_state = CTX_PREPARE_FOR_IMCU;
  45.417 +    if (*out_row_ctr >= out_rows_avail)
  45.418 +      return;			/* Postprocessor exactly filled output buf */
  45.419 +    /*FALLTHROUGH*/
  45.420 +  case CTX_PREPARE_FOR_IMCU:
  45.421 +    /* Prepare to process first M-1 row groups of this iMCU row */
  45.422 +    main->rowgroup_ctr = 0;
  45.423 +    main->rowgroups_avail = (JDIMENSION) (cinfo->min_DCT_scaled_size - 1);
  45.424 +    /* Check for bottom of image: if so, tweak pointers to "duplicate"
  45.425 +     * the last sample row, and adjust rowgroups_avail to ignore padding rows.
  45.426 +     */
  45.427 +    if (main->iMCU_row_ctr == cinfo->total_iMCU_rows)
  45.428 +      set_bottom_pointers(cinfo);
  45.429 +    main->context_state = CTX_PROCESS_IMCU;
  45.430 +    /*FALLTHROUGH*/
  45.431 +  case CTX_PROCESS_IMCU:
  45.432 +    /* Call postprocessor using previously set pointers */
  45.433 +    (*cinfo->post->post_process_data) (cinfo, main->xbuffer[main->whichptr],
  45.434 +			&main->rowgroup_ctr, main->rowgroups_avail,
  45.435 +			output_buf, out_row_ctr, out_rows_avail);
  45.436 +    if (main->rowgroup_ctr < main->rowgroups_avail)
  45.437 +      return;			/* Need to suspend */
  45.438 +    /* After the first iMCU, change wraparound pointers to normal state */
  45.439 +    if (main->iMCU_row_ctr == 1)
  45.440 +      set_wraparound_pointers(cinfo);
  45.441 +    /* Prepare to load new iMCU row using other xbuffer list */
  45.442 +    main->whichptr ^= 1;	/* 0=>1 or 1=>0 */
  45.443 +    main->buffer_full = FALSE;
  45.444 +    /* Still need to process last row group of this iMCU row, */
  45.445 +    /* which is saved at index M+1 of the other xbuffer */
  45.446 +    main->rowgroup_ctr = (JDIMENSION) (cinfo->min_DCT_scaled_size + 1);
  45.447 +    main->rowgroups_avail = (JDIMENSION) (cinfo->min_DCT_scaled_size + 2);
  45.448 +    main->context_state = CTX_POSTPONED_ROW;
  45.449 +  }
  45.450 +}
  45.451 +
  45.452 +
  45.453 +/*
  45.454 + * Process some data.
  45.455 + * Final pass of two-pass quantization: just call the postprocessor.
  45.456 + * Source data will be the postprocessor controller's internal buffer.
  45.457 + */
  45.458 +
  45.459 +#ifdef QUANT_2PASS_SUPPORTED
  45.460 +
  45.461 +METHODDEF(void)
  45.462 +process_data_crank_post (j_decompress_ptr cinfo,
  45.463 +			 JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
  45.464 +			 JDIMENSION out_rows_avail)
  45.465 +{
  45.466 +  (*cinfo->post->post_process_data) (cinfo, (JSAMPIMAGE) NULL,
  45.467 +				     (JDIMENSION *) NULL, (JDIMENSION) 0,
  45.468 +				     output_buf, out_row_ctr, out_rows_avail);
  45.469 +}
  45.470 +
  45.471 +#endif /* QUANT_2PASS_SUPPORTED */
  45.472 +
  45.473 +
  45.474 +/*
  45.475 + * Initialize main buffer controller.
  45.476 + */
  45.477 +
  45.478 +GLOBAL(void)
  45.479 +jinit_d_main_controller (j_decompress_ptr cinfo, boolean need_full_buffer)
  45.480 +{
  45.481 +  my_main_ptr main;
  45.482 +  int ci, rgroup, ngroups;
  45.483 +  jpeg_component_info *compptr;
  45.484 +
  45.485 +  main = (my_main_ptr)
  45.486 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  45.487 +				SIZEOF(my_main_controller));
  45.488 +  cinfo->main = (struct jpeg_d_main_controller *) main;
  45.489 +  main->pub.start_pass = start_pass_main;
  45.490 +
  45.491 +  if (need_full_buffer)		/* shouldn't happen */
  45.492 +    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  45.493 +
  45.494 +  /* Allocate the workspace.
  45.495 +   * ngroups is the number of row groups we need.
  45.496 +   */
  45.497 +  if (cinfo->upsample->need_context_rows) {
  45.498 +    if (cinfo->min_DCT_scaled_size < 2) /* unsupported, see comments above */
  45.499 +      ERREXIT(cinfo, JERR_NOTIMPL);
  45.500 +    alloc_funny_pointers(cinfo); /* Alloc space for xbuffer[] lists */
  45.501 +    ngroups = cinfo->min_DCT_scaled_size + 2;
  45.502 +  } else {
  45.503 +    ngroups = cinfo->min_DCT_scaled_size;
  45.504 +  }
  45.505 +
  45.506 +  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  45.507 +       ci++, compptr++) {
  45.508 +    rgroup = (compptr->v_samp_factor * compptr->DCT_scaled_size) /
  45.509 +      cinfo->min_DCT_scaled_size; /* height of a row group of component */
  45.510 +    main->buffer[ci] = (*cinfo->mem->alloc_sarray)
  45.511 +			((j_common_ptr) cinfo, JPOOL_IMAGE,
  45.512 +			 compptr->width_in_blocks * compptr->DCT_scaled_size,
  45.513 +			 (JDIMENSION) (rgroup * ngroups));
  45.514 +  }
  45.515 +}
    46.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    46.2 +++ b/libs/libjpeg/jdmarker.c	Sat Sep 19 05:51:51 2015 +0300
    46.3 @@ -0,0 +1,1360 @@
    46.4 +/*
    46.5 + * jdmarker.c
    46.6 + *
    46.7 + * Copyright (C) 1991-1998, Thomas G. Lane.
    46.8 + * This file is part of the Independent JPEG Group's software.
    46.9 + * For conditions of distribution and use, see the accompanying README file.
   46.10 + *
   46.11 + * This file contains routines to decode JPEG datastream markers.
   46.12 + * Most of the complexity arises from our desire to support input
   46.13 + * suspension: if not all of the data for a marker is available,
   46.14 + * we must exit back to the application.  On resumption, we reprocess
   46.15 + * the marker.
   46.16 + */
   46.17 +
   46.18 +#define JPEG_INTERNALS
   46.19 +#include "jinclude.h"
   46.20 +#include "jpeglib.h"
   46.21 +
   46.22 +
   46.23 +typedef enum {			/* JPEG marker codes */
   46.24 +  M_SOF0  = 0xc0,
   46.25 +  M_SOF1  = 0xc1,
   46.26 +  M_SOF2  = 0xc2,
   46.27 +  M_SOF3  = 0xc3,
   46.28 +  
   46.29 +  M_SOF5  = 0xc5,
   46.30 +  M_SOF6  = 0xc6,
   46.31 +  M_SOF7  = 0xc7,
   46.32 +  
   46.33 +  M_JPG   = 0xc8,
   46.34 +  M_SOF9  = 0xc9,
   46.35 +  M_SOF10 = 0xca,
   46.36 +  M_SOF11 = 0xcb,
   46.37 +  
   46.38 +  M_SOF13 = 0xcd,
   46.39 +  M_SOF14 = 0xce,
   46.40 +  M_SOF15 = 0xcf,
   46.41 +  
   46.42 +  M_DHT   = 0xc4,
   46.43 +  
   46.44 +  M_DAC   = 0xcc,
   46.45 +  
   46.46 +  M_RST0  = 0xd0,
   46.47 +  M_RST1  = 0xd1,
   46.48 +  M_RST2  = 0xd2,
   46.49 +  M_RST3  = 0xd3,
   46.50 +  M_RST4  = 0xd4,
   46.51 +  M_RST5  = 0xd5,
   46.52 +  M_RST6  = 0xd6,
   46.53 +  M_RST7  = 0xd7,
   46.54 +  
   46.55 +  M_SOI   = 0xd8,
   46.56 +  M_EOI   = 0xd9,
   46.57 +  M_SOS   = 0xda,
   46.58 +  M_DQT   = 0xdb,
   46.59 +  M_DNL   = 0xdc,
   46.60 +  M_DRI   = 0xdd,
   46.61 +  M_DHP   = 0xde,
   46.62 +  M_EXP   = 0xdf,
   46.63 +  
   46.64 +  M_APP0  = 0xe0,
   46.65 +  M_APP1  = 0xe1,
   46.66 +  M_APP2  = 0xe2,
   46.67 +  M_APP3  = 0xe3,
   46.68 +  M_APP4  = 0xe4,
   46.69 +  M_APP5  = 0xe5,
   46.70 +  M_APP6  = 0xe6,
   46.71 +  M_APP7  = 0xe7,
   46.72 +  M_APP8  = 0xe8,
   46.73 +  M_APP9  = 0xe9,
   46.74 +  M_APP10 = 0xea,
   46.75 +  M_APP11 = 0xeb,
   46.76 +  M_APP12 = 0xec,
   46.77 +  M_APP13 = 0xed,
   46.78 +  M_APP14 = 0xee,
   46.79 +  M_APP15 = 0xef,
   46.80 +  
   46.81 +  M_JPG0  = 0xf0,
   46.82 +  M_JPG13 = 0xfd,
   46.83 +  M_COM   = 0xfe,
   46.84 +  
   46.85 +  M_TEM   = 0x01,
   46.86 +  
   46.87 +  M_ERROR = 0x100
   46.88 +} JPEG_MARKER;
   46.89 +
   46.90 +
   46.91 +/* Private state */
   46.92 +
   46.93 +typedef struct {
   46.94 +  struct jpeg_marker_reader pub; /* public fields */
   46.95 +
   46.96 +  /* Application-overridable marker processing methods */
   46.97 +  jpeg_marker_parser_method process_COM;
   46.98 +  jpeg_marker_parser_method process_APPn[16];
   46.99 +
  46.100 +  /* Limit on marker data length to save for each marker type */
  46.101 +  unsigned int length_limit_COM;
  46.102 +  unsigned int length_limit_APPn[16];
  46.103 +
  46.104 +  /* Status of COM/APPn marker saving */
  46.105 +  jpeg_saved_marker_ptr cur_marker;	/* NULL if not processing a marker */
  46.106 +  unsigned int bytes_read;		/* data bytes read so far in marker */
  46.107 +  /* Note: cur_marker is not linked into marker_list until it's all read. */
  46.108 +} my_marker_reader;
  46.109 +
  46.110 +typedef my_marker_reader * my_marker_ptr;
  46.111 +
  46.112 +
  46.113 +/*
  46.114 + * Macros for fetching data from the data source module.
  46.115 + *
  46.116 + * At all times, cinfo->src->next_input_byte and ->bytes_in_buffer reflect
  46.117 + * the current restart point; we update them only when we have reached a
  46.118 + * suitable place to restart if a suspension occurs.
  46.119 + */
  46.120 +
  46.121 +/* Declare and initialize local copies of input pointer/count */
  46.122 +#define INPUT_VARS(cinfo)  \
  46.123 +	struct jpeg_source_mgr * datasrc = (cinfo)->src;  \
  46.124 +	const JOCTET * next_input_byte = datasrc->next_input_byte;  \
  46.125 +	size_t bytes_in_buffer = datasrc->bytes_in_buffer
  46.126 +
  46.127 +/* Unload the local copies --- do this only at a restart boundary */
  46.128 +#define INPUT_SYNC(cinfo)  \
  46.129 +	( datasrc->next_input_byte = next_input_byte,  \
  46.130 +	  datasrc->bytes_in_buffer = bytes_in_buffer )
  46.131 +
  46.132 +/* Reload the local copies --- used only in MAKE_BYTE_AVAIL */
  46.133 +#define INPUT_RELOAD(cinfo)  \
  46.134 +	( next_input_byte = datasrc->next_input_byte,  \
  46.135 +	  bytes_in_buffer = datasrc->bytes_in_buffer )
  46.136 +
  46.137 +/* Internal macro for INPUT_BYTE and INPUT_2BYTES: make a byte available.
  46.138 + * Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
  46.139 + * but we must reload the local copies after a successful fill.
  46.140 + */
  46.141 +#define MAKE_BYTE_AVAIL(cinfo,action)  \
  46.142 +	if (bytes_in_buffer == 0) {  \
  46.143 +	  if (! (*datasrc->fill_input_buffer) (cinfo))  \
  46.144 +	    { action; }  \
  46.145 +	  INPUT_RELOAD(cinfo);  \
  46.146 +	}
  46.147 +
  46.148 +/* Read a byte into variable V.
  46.149 + * If must suspend, take the specified action (typically "return FALSE").
  46.150 + */
  46.151 +#define INPUT_BYTE(cinfo,V,action)  \
  46.152 +	MAKESTMT( MAKE_BYTE_AVAIL(cinfo,action); \
  46.153 +		  bytes_in_buffer--; \
  46.154 +		  V = GETJOCTET(*next_input_byte++); )
  46.155 +
  46.156 +/* As above, but read two bytes interpreted as an unsigned 16-bit integer.
  46.157 + * V should be declared unsigned int or perhaps INT32.
  46.158 + */
  46.159 +#define INPUT_2BYTES(cinfo,V,action)  \
  46.160 +	MAKESTMT( MAKE_BYTE_AVAIL(cinfo,action); \
  46.161 +		  bytes_in_buffer--; \
  46.162 +		  V = ((unsigned int) GETJOCTET(*next_input_byte++)) << 8; \
  46.163 +		  MAKE_BYTE_AVAIL(cinfo,action); \
  46.164 +		  bytes_in_buffer--; \
  46.165 +		  V += GETJOCTET(*next_input_byte++); )
  46.166 +
  46.167 +
  46.168 +/*
  46.169 + * Routines to process JPEG markers.
  46.170 + *
  46.171 + * Entry condition: JPEG marker itself has been read and its code saved
  46.172 + *   in cinfo->unread_marker; input restart point is just after the marker.
  46.173 + *
  46.174 + * Exit: if return TRUE, have read and processed any parameters, and have
  46.175 + *   updated the restart point to point after the parameters.
  46.176 + *   If return FALSE, was forced to suspend before reaching end of
  46.177 + *   marker parameters; restart point has not been moved.  Same routine
  46.178 + *   will be called again after application supplies more input data.
  46.179 + *
  46.180 + * This approach to suspension assumes that all of a marker's parameters
  46.181 + * can fit into a single input bufferload.  This should hold for "normal"
  46.182 + * markers.  Some COM/APPn markers might have large parameter segments
  46.183 + * that might not fit.  If we are simply dropping such a marker, we use
  46.184 + * skip_input_data to get past it, and thereby put the problem on the
  46.185 + * source manager's shoulders.  If we are saving the marker's contents
  46.186 + * into memory, we use a slightly different convention: when forced to
  46.187 + * suspend, the marker processor updates the restart point to the end of
  46.188 + * what it's consumed (ie, the end of the buffer) before returning FALSE.
  46.189 + * On resumption, cinfo->unread_marker still contains the marker code,
  46.190 + * but the data source will point to the next chunk of marker data.
  46.191 + * The marker processor must retain internal state to deal with this.
  46.192 + *
  46.193 + * Note that we don't bother to avoid duplicate trace messages if a
  46.194 + * suspension occurs within marker parameters.  Other side effects
  46.195 + * require more care.
  46.196 + */
  46.197 +
  46.198 +
  46.199 +LOCAL(boolean)
  46.200 +get_soi (j_decompress_ptr cinfo)
  46.201 +/* Process an SOI marker */
  46.202 +{
  46.203 +  int i;
  46.204 +  
  46.205 +  TRACEMS(cinfo, 1, JTRC_SOI);
  46.206 +
  46.207 +  if (cinfo->marker->saw_SOI)
  46.208 +    ERREXIT(cinfo, JERR_SOI_DUPLICATE);
  46.209 +
  46.210 +  /* Reset all parameters that are defined to be reset by SOI */
  46.211 +
  46.212 +  for (i = 0; i < NUM_ARITH_TBLS; i++) {
  46.213 +    cinfo->arith_dc_L[i] = 0;
  46.214 +    cinfo->arith_dc_U[i] = 1;
  46.215 +    cinfo->arith_ac_K[i] = 5;
  46.216 +  }
  46.217 +  cinfo->restart_interval = 0;
  46.218 +
  46.219 +  /* Set initial assumptions for colorspace etc */
  46.220 +
  46.221 +  cinfo->jpeg_color_space = JCS_UNKNOWN;
  46.222 +  cinfo->CCIR601_sampling = FALSE; /* Assume non-CCIR sampling??? */
  46.223 +
  46.224 +  cinfo->saw_JFIF_marker = FALSE;
  46.225 +  cinfo->JFIF_major_version = 1; /* set default JFIF APP0 values */
  46.226 +  cinfo->JFIF_minor_version = 1;
  46.227 +  cinfo->density_unit = 0;
  46.228 +  cinfo->X_density = 1;
  46.229 +  cinfo->Y_density = 1;
  46.230 +  cinfo->saw_Adobe_marker = FALSE;
  46.231 +  cinfo->Adobe_transform = 0;
  46.232 +
  46.233 +  cinfo->marker->saw_SOI = TRUE;
  46.234 +
  46.235 +  return TRUE;
  46.236 +}
  46.237 +
  46.238 +
  46.239 +LOCAL(boolean)
  46.240 +get_sof (j_decompress_ptr cinfo, boolean is_prog, boolean is_arith)
  46.241 +/* Process a SOFn marker */
  46.242 +{
  46.243 +  INT32 length;
  46.244 +  int c, ci;
  46.245 +  jpeg_component_info * compptr;
  46.246 +  INPUT_VARS(cinfo);
  46.247 +
  46.248 +  cinfo->progressive_mode = is_prog;
  46.249 +  cinfo->arith_code = is_arith;
  46.250 +
  46.251 +  INPUT_2BYTES(cinfo, length, return FALSE);
  46.252 +
  46.253 +  INPUT_BYTE(cinfo, cinfo->data_precision, return FALSE);
  46.254 +  INPUT_2BYTES(cinfo, cinfo->image_height, return FALSE);
  46.255 +  INPUT_2BYTES(cinfo, cinfo->image_width, return FALSE);
  46.256 +  INPUT_BYTE(cinfo, cinfo->num_components, return FALSE);
  46.257 +
  46.258 +  length -= 8;
  46.259 +
  46.260 +  TRACEMS4(cinfo, 1, JTRC_SOF, cinfo->unread_marker,
  46.261 +	   (int) cinfo->image_width, (int) cinfo->image_height,
  46.262 +	   cinfo->num_components);
  46.263 +
  46.264 +  if (cinfo->marker->saw_SOF)
  46.265 +    ERREXIT(cinfo, JERR_SOF_DUPLICATE);
  46.266 +
  46.267 +  /* We don't support files in which the image height is initially specified */
  46.268 +  /* as 0 and is later redefined by DNL.  As long as we have to check that,  */
  46.269 +  /* might as well have a general sanity check. */
  46.270 +  if (cinfo->image_height <= 0 || cinfo->image_width <= 0
  46.271 +      || cinfo->num_components <= 0)
  46.272 +    ERREXIT(cinfo, JERR_EMPTY_IMAGE);
  46.273 +
  46.274 +  if (length != (cinfo->num_components * 3))
  46.275 +    ERREXIT(cinfo, JERR_BAD_LENGTH);
  46.276 +
  46.277 +  if (cinfo->comp_info == NULL)	/* do only once, even if suspend */
  46.278 +    cinfo->comp_info = (jpeg_component_info *) (*cinfo->mem->alloc_small)
  46.279 +			((j_common_ptr) cinfo, JPOOL_IMAGE,
  46.280 +			 cinfo->num_components * SIZEOF(jpeg_component_info));
  46.281 +  
  46.282 +  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  46.283 +       ci++, compptr++) {
  46.284 +    compptr->component_index = ci;
  46.285 +    INPUT_BYTE(cinfo, compptr->component_id, return FALSE);
  46.286 +    INPUT_BYTE(cinfo, c, return FALSE);
  46.287 +    compptr->h_samp_factor = (c >> 4) & 15;
  46.288 +    compptr->v_samp_factor = (c     ) & 15;
  46.289 +    INPUT_BYTE(cinfo, compptr->quant_tbl_no, return FALSE);
  46.290 +
  46.291 +    TRACEMS4(cinfo, 1, JTRC_SOF_COMPONENT,
  46.292 +	     compptr->component_id, compptr->h_samp_factor,
  46.293 +	     compptr->v_samp_factor, compptr->quant_tbl_no);
  46.294 +  }
  46.295 +
  46.296 +  cinfo->marker->saw_SOF = TRUE;
  46.297 +
  46.298 +  INPUT_SYNC(cinfo);
  46.299 +  return TRUE;
  46.300 +}
  46.301 +
  46.302 +
  46.303 +LOCAL(boolean)
  46.304 +get_sos (j_decompress_ptr cinfo)
  46.305 +/* Process a SOS marker */
  46.306 +{
  46.307 +  INT32 length;
  46.308 +  int i, ci, n, c, cc;
  46.309 +  jpeg_component_info * compptr;
  46.310 +  INPUT_VARS(cinfo);
  46.311 +
  46.312 +  if (! cinfo->marker->saw_SOF)
  46.313 +    ERREXIT(cinfo, JERR_SOS_NO_SOF);
  46.314 +
  46.315 +  INPUT_2BYTES(cinfo, length, return FALSE);
  46.316 +
  46.317 +  INPUT_BYTE(cinfo, n, return FALSE); /* Number of components */
  46.318 +
  46.319 +  TRACEMS1(cinfo, 1, JTRC_SOS, n);
  46.320 +
  46.321 +  if (length != (n * 2 + 6) || n < 1 || n > MAX_COMPS_IN_SCAN)
  46.322 +    ERREXIT(cinfo, JERR_BAD_LENGTH);
  46.323 +
  46.324 +  cinfo->comps_in_scan = n;
  46.325 +
  46.326 +  /* Collect the component-spec parameters */
  46.327 +
  46.328 +  for (i = 0; i < n; i++) {
  46.329 +    INPUT_BYTE(cinfo, cc, return FALSE);
  46.330 +    INPUT_BYTE(cinfo, c, return FALSE);
  46.331 +    
  46.332 +    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  46.333 +	 ci++, compptr++) {
  46.334 +      if (cc == compptr->component_id)
  46.335 +	goto id_found;
  46.336 +    }
  46.337 +
  46.338 +    ERREXIT1(cinfo, JERR_BAD_COMPONENT_ID, cc);
  46.339 +
  46.340 +  id_found:
  46.341 +
  46.342 +    cinfo->cur_comp_info[i] = compptr;
  46.343 +    compptr->dc_tbl_no = (c >> 4) & 15;
  46.344 +    compptr->ac_tbl_no = (c     ) & 15;
  46.345 +    
  46.346 +    TRACEMS3(cinfo, 1, JTRC_SOS_COMPONENT, cc,
  46.347 +	     compptr->dc_tbl_no, compptr->ac_tbl_no);
  46.348 +  }
  46.349 +
  46.350 +  /* Collect the additional scan parameters Ss, Se, Ah/Al. */
  46.351 +  INPUT_BYTE(cinfo, c, return FALSE);
  46.352 +  cinfo->Ss = c;
  46.353 +  INPUT_BYTE(cinfo, c, return FALSE);
  46.354 +  cinfo->Se = c;
  46.355 +  INPUT_BYTE(cinfo, c, return FALSE);
  46.356 +  cinfo->Ah = (c >> 4) & 15;
  46.357 +  cinfo->Al = (c     ) & 15;
  46.358 +
  46.359 +  TRACEMS4(cinfo, 1, JTRC_SOS_PARAMS, cinfo->Ss, cinfo->Se,
  46.360 +	   cinfo->Ah, cinfo->Al);
  46.361 +
  46.362 +  /* Prepare to scan data & restart markers */
  46.363 +  cinfo->marker->next_restart_num = 0;
  46.364 +
  46.365 +  /* Count another SOS marker */
  46.366 +  cinfo->input_scan_number++;
  46.367 +
  46.368 +  INPUT_SYNC(cinfo);
  46.369 +  return TRUE;
  46.370 +}
  46.371 +
  46.372 +
  46.373 +#ifdef D_ARITH_CODING_SUPPORTED
  46.374 +
  46.375 +LOCAL(boolean)
  46.376 +get_dac (j_decompress_ptr cinfo)
  46.377 +/* Process a DAC marker */
  46.378 +{
  46.379 +  INT32 length;
  46.380 +  int index, val;
  46.381 +  INPUT_VARS(cinfo);
  46.382 +
  46.383 +  INPUT_2BYTES(cinfo, length, return FALSE);
  46.384 +  length -= 2;
  46.385 +  
  46.386 +  while (length > 0) {
  46.387 +    INPUT_BYTE(cinfo, index, return FALSE);
  46.388 +    INPUT_BYTE(cinfo, val, return FALSE);
  46.389 +
  46.390 +    length -= 2;
  46.391 +
  46.392 +    TRACEMS2(cinfo, 1, JTRC_DAC, index, val);
  46.393 +
  46.394 +    if (index < 0 || index >= (2*NUM_ARITH_TBLS))
  46.395 +      ERREXIT1(cinfo, JERR_DAC_INDEX, index);
  46.396 +
  46.397 +    if (index >= NUM_ARITH_TBLS) { /* define AC table */
  46.398 +      cinfo->arith_ac_K[index-NUM_ARITH_TBLS] = (UINT8) val;
  46.399 +    } else {			/* define DC table */
  46.400 +      cinfo->arith_dc_L[index] = (UINT8) (val & 0x0F);
  46.401 +      cinfo->arith_dc_U[index] = (UINT8) (val >> 4);
  46.402 +      if (cinfo->arith_dc_L[index] > cinfo->arith_dc_U[index])
  46.403 +	ERREXIT1(cinfo, JERR_DAC_VALUE, val);
  46.404 +    }
  46.405 +  }
  46.406 +
  46.407 +  if (length != 0)
  46.408 +    ERREXIT(cinfo, JERR_BAD_LENGTH);
  46.409 +
  46.410 +  INPUT_SYNC(cinfo);
  46.411 +  return TRUE;
  46.412 +}
  46.413 +
  46.414 +#else /* ! D_ARITH_CODING_SUPPORTED */
  46.415 +
  46.416 +#define get_dac(cinfo)  skip_variable(cinfo)
  46.417 +
  46.418 +#endif /* D_ARITH_CODING_SUPPORTED */
  46.419 +
  46.420 +
  46.421 +LOCAL(boolean)
  46.422 +get_dht (j_decompress_ptr cinfo)
  46.423 +/* Process a DHT marker */
  46.424 +{
  46.425 +  INT32 length;
  46.426 +  UINT8 bits[17];
  46.427 +  UINT8 huffval[256];
  46.428 +  int i, index, count;
  46.429 +  JHUFF_TBL **htblptr;
  46.430 +  INPUT_VARS(cinfo);
  46.431 +
  46.432 +  INPUT_2BYTES(cinfo, length, return FALSE);
  46.433 +  length -= 2;
  46.434 +  
  46.435 +  while (length > 16) {
  46.436 +    INPUT_BYTE(cinfo, index, return FALSE);
  46.437 +
  46.438 +    TRACEMS1(cinfo, 1, JTRC_DHT, index);
  46.439 +      
  46.440 +    bits[0] = 0;
  46.441 +    count = 0;
  46.442 +    for (i = 1; i <= 16; i++) {
  46.443 +      INPUT_BYTE(cinfo, bits[i], return FALSE);
  46.444 +      count += bits[i];
  46.445 +    }
  46.446 +
  46.447 +    length -= 1 + 16;
  46.448 +
  46.449 +    TRACEMS8(cinfo, 2, JTRC_HUFFBITS,
  46.450 +	     bits[1], bits[2], bits[3], bits[4],
  46.451 +	     bits[5], bits[6], bits[7], bits[8]);
  46.452 +    TRACEMS8(cinfo, 2, JTRC_HUFFBITS,
  46.453 +	     bits[9], bits[10], bits[11], bits[12],
  46.454 +	     bits[13], bits[14], bits[15], bits[16]);
  46.455 +
  46.456 +    /* Here we just do minimal validation of the counts to avoid walking
  46.457 +     * off the end of our table space.  jdhuff.c will check more carefully.
  46.458 +     */
  46.459 +    if (count > 256 || ((INT32) count) > length)
  46.460 +      ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
  46.461 +
  46.462 +    for (i = 0; i < count; i++)
  46.463 +      INPUT_BYTE(cinfo, huffval[i], return FALSE);
  46.464 +
  46.465 +    length -= count;
  46.466 +
  46.467 +    if (index & 0x10) {		/* AC table definition */
  46.468 +      index -= 0x10;
  46.469 +      htblptr = &cinfo->ac_huff_tbl_ptrs[index];
  46.470 +    } else {			/* DC table definition */
  46.471 +      htblptr = &cinfo->dc_huff_tbl_ptrs[index];
  46.472 +    }
  46.473 +
  46.474 +    if (index < 0 || index >= NUM_HUFF_TBLS)
  46.475 +      ERREXIT1(cinfo, JERR_DHT_INDEX, index);
  46.476 +
  46.477 +    if (*htblptr == NULL)
  46.478 +      *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
  46.479 +  
  46.480 +    MEMCOPY((*htblptr)->bits, bits, SIZEOF((*htblptr)->bits));
  46.481 +    MEMCOPY((*htblptr)->huffval, huffval, SIZEOF((*htblptr)->huffval));
  46.482 +  }
  46.483 +
  46.484 +  if (length != 0)
  46.485 +    ERREXIT(cinfo, JERR_BAD_LENGTH);
  46.486 +
  46.487 +  INPUT_SYNC(cinfo);
  46.488 +  return TRUE;
  46.489 +}
  46.490 +
  46.491 +
  46.492 +LOCAL(boolean)
  46.493 +get_dqt (j_decompress_ptr cinfo)
  46.494 +/* Process a DQT marker */
  46.495 +{
  46.496 +  INT32 length;
  46.497 +  int n, i, prec;
  46.498 +  unsigned int tmp;
  46.499 +  JQUANT_TBL *quant_ptr;
  46.500 +  INPUT_VARS(cinfo);
  46.501 +
  46.502 +  INPUT_2BYTES(cinfo, length, return FALSE);
  46.503 +  length -= 2;
  46.504 +
  46.505 +  while (length > 0) {
  46.506 +    INPUT_BYTE(cinfo, n, return FALSE);
  46.507 +    prec = n >> 4;
  46.508 +    n &= 0x0F;
  46.509 +
  46.510 +    TRACEMS2(cinfo, 1, JTRC_DQT, n, prec);
  46.511 +
  46.512 +    if (n >= NUM_QUANT_TBLS)
  46.513 +      ERREXIT1(cinfo, JERR_DQT_INDEX, n);
  46.514 +      
  46.515 +    if (cinfo->quant_tbl_ptrs[n] == NULL)
  46.516 +      cinfo->quant_tbl_ptrs[n] = jpeg_alloc_quant_table((j_common_ptr) cinfo);
  46.517 +    quant_ptr = cinfo->quant_tbl_ptrs[n];
  46.518 +
  46.519 +    for (i = 0; i < DCTSIZE2; i++) {
  46.520 +      if (prec)
  46.521 +	INPUT_2BYTES(cinfo, tmp, return FALSE);
  46.522 +      else
  46.523 +	INPUT_BYTE(cinfo, tmp, return FALSE);
  46.524 +      /* We convert the zigzag-order table to natural array order. */
  46.525 +      quant_ptr->quantval[jpeg_natural_order[i]] = (UINT16) tmp;
  46.526 +    }
  46.527 +
  46.528 +    if (cinfo->err->trace_level >= 2) {
  46.529 +      for (i = 0; i < DCTSIZE2; i += 8) {
  46.530 +	TRACEMS8(cinfo, 2, JTRC_QUANTVALS,
  46.531 +		 quant_ptr->quantval[i],   quant_ptr->quantval[i+1],
  46.532 +		 quant_ptr->quantval[i+2], quant_ptr->quantval[i+3],
  46.533 +		 quant_ptr->quantval[i+4], quant_ptr->quantval[i+5],
  46.534 +		 quant_ptr->quantval[i+6], quant_ptr->quantval[i+7]);
  46.535 +      }
  46.536 +    }
  46.537 +
  46.538 +    length -= DCTSIZE2+1;
  46.539 +    if (prec) length -= DCTSIZE2;
  46.540 +  }
  46.541 +
  46.542 +  if (length != 0)
  46.543 +    ERREXIT(cinfo, JERR_BAD_LENGTH);
  46.544 +
  46.545 +  INPUT_SYNC(cinfo);
  46.546 +  return TRUE;
  46.547 +}
  46.548 +
  46.549 +
  46.550 +LOCAL(boolean)
  46.551 +get_dri (j_decompress_ptr cinfo)
  46.552 +/* Process a DRI marker */
  46.553 +{
  46.554 +  INT32 length;
  46.555 +  unsigned int tmp;
  46.556 +  INPUT_VARS(cinfo);
  46.557 +
  46.558 +  INPUT_2BYTES(cinfo, length, return FALSE);
  46.559 +  
  46.560 +  if (length != 4)
  46.561 +    ERREXIT(cinfo, JERR_BAD_LENGTH);
  46.562 +
  46.563 +  INPUT_2BYTES(cinfo, tmp, return FALSE);
  46.564 +
  46.565 +  TRACEMS1(cinfo, 1, JTRC_DRI, tmp);
  46.566 +
  46.567 +  cinfo->restart_interval = tmp;
  46.568 +
  46.569 +  INPUT_SYNC(cinfo);
  46.570 +  return TRUE;
  46.571 +}
  46.572 +
  46.573 +
  46.574 +/*
  46.575 + * Routines for processing APPn and COM markers.
  46.576 + * These are either saved in memory or discarded, per application request.
  46.577 + * APP0 and APP14 are specially checked to see if they are
  46.578 + * JFIF and Adobe markers, respectively.
  46.579 + */
  46.580 +
  46.581 +#define APP0_DATA_LEN	14	/* Length of interesting data in APP0 */
  46.582 +#define APP14_DATA_LEN	12	/* Length of interesting data in APP14 */
  46.583 +#define APPN_DATA_LEN	14	/* Must be the largest of the above!! */
  46.584 +
  46.585 +
  46.586 +LOCAL(void)
  46.587 +examine_app0 (j_decompress_ptr cinfo, JOCTET FAR * data,
  46.588 +	      unsigned int datalen, INT32 remaining)
  46.589 +/* Examine first few bytes from an APP0.
  46.590 + * Take appropriate action if it is a JFIF marker.
  46.591 + * datalen is # of bytes at data[], remaining is length of rest of marker data.
  46.592 + */
  46.593 +{
  46.594 +  INT32 totallen = (INT32) datalen + remaining;
  46.595 +
  46.596 +  if (datalen >= APP0_DATA_LEN &&
  46.597 +      GETJOCTET(data[0]) == 0x4A &&
  46.598 +      GETJOCTET(data[1]) == 0x46 &&
  46.599 +      GETJOCTET(data[2]) == 0x49 &&
  46.600 +      GETJOCTET(data[3]) == 0x46 &&
  46.601 +      GETJOCTET(data[4]) == 0) {
  46.602 +    /* Found JFIF APP0 marker: save info */
  46.603 +    cinfo->saw_JFIF_marker = TRUE;
  46.604 +    cinfo->JFIF_major_version = GETJOCTET(data[5]);
  46.605 +    cinfo->JFIF_minor_version = GETJOCTET(data[6]);
  46.606 +    cinfo->density_unit = GETJOCTET(data[7]);
  46.607 +    cinfo->X_density = (GETJOCTET(data[8]) << 8) + GETJOCTET(data[9]);
  46.608 +    cinfo->Y_density = (GETJOCTET(data[10]) << 8) + GETJOCTET(data[11]);
  46.609 +    /* Check version.
  46.610 +     * Major version must be 1, anything else signals an incompatible change.
  46.611 +     * (We used to treat this as an error, but now it's a nonfatal warning,
  46.612 +     * because some bozo at Hijaak couldn't read the spec.)
  46.613 +     * Minor version should be 0..2, but process anyway if newer.
  46.614 +     */
  46.615 +    if (cinfo->JFIF_major_version != 1)
  46.616 +      WARNMS2(cinfo, JWRN_JFIF_MAJOR,
  46.617 +	      cinfo->JFIF_major_version, cinfo->JFIF_minor_version);
  46.618 +    /* Generate trace messages */
  46.619 +    TRACEMS5(cinfo, 1, JTRC_JFIF,
  46.620 +	     cinfo->JFIF_major_version, cinfo->JFIF_minor_version,
  46.621 +	     cinfo->X_density, cinfo->Y_density, cinfo->density_unit);
  46.622 +    /* Validate thumbnail dimensions and issue appropriate messages */
  46.623 +    if (GETJOCTET(data[12]) | GETJOCTET(data[13]))
  46.624 +      TRACEMS2(cinfo, 1, JTRC_JFIF_THUMBNAIL,
  46.625 +	       GETJOCTET(data[12]), GETJOCTET(data[13]));
  46.626 +    totallen -= APP0_DATA_LEN;
  46.627 +    if (totallen !=
  46.628 +	((INT32)GETJOCTET(data[12]) * (INT32)GETJOCTET(data[13]) * (INT32) 3))
  46.629 +      TRACEMS1(cinfo, 1, JTRC_JFIF_BADTHUMBNAILSIZE, (int) totallen);
  46.630 +  } else if (datalen >= 6 &&
  46.631 +      GETJOCTET(data[0]) == 0x4A &&
  46.632 +      GETJOCTET(data[1]) == 0x46 &&
  46.633 +      GETJOCTET(data[2]) == 0x58 &&
  46.634 +      GETJOCTET(data[3]) == 0x58 &&
  46.635 +      GETJOCTET(data[4]) == 0) {
  46.636 +    /* Found JFIF "JFXX" extension APP0 marker */
  46.637 +    /* The library doesn't actually do anything with these,
  46.638 +     * but we try to produce a helpful trace message.
  46.639 +     */
  46.640 +    switch (GETJOCTET(data[5])) {
  46.641 +    case 0x10:
  46.642 +      TRACEMS1(cinfo, 1, JTRC_THUMB_JPEG, (int) totallen);
  46.643 +      break;
  46.644 +    case 0x11:
  46.645 +      TRACEMS1(cinfo, 1, JTRC_THUMB_PALETTE, (int) totallen);
  46.646 +      break;
  46.647 +    case 0x13:
  46.648 +      TRACEMS1(cinfo, 1, JTRC_THUMB_RGB, (int) totallen);
  46.649 +      break;
  46.650 +    default:
  46.651 +      TRACEMS2(cinfo, 1, JTRC_JFIF_EXTENSION,
  46.652 +	       GETJOCTET(data[5]), (int) totallen);
  46.653 +      break;
  46.654 +    }
  46.655 +  } else {
  46.656 +    /* Start of APP0 does not match "JFIF" or "JFXX", or too short */
  46.657 +    TRACEMS1(cinfo, 1, JTRC_APP0, (int) totallen);
  46.658 +  }
  46.659 +}
  46.660 +
  46.661 +
  46.662 +LOCAL(void)
  46.663 +examine_app14 (j_decompress_ptr cinfo, JOCTET FAR * data,
  46.664 +	       unsigned int datalen, INT32 remaining)
  46.665 +/* Examine first few bytes from an APP14.
  46.666 + * Take appropriate action if it is an Adobe marker.
  46.667 + * datalen is # of bytes at data[], remaining is length of rest of marker data.
  46.668 + */
  46.669 +{
  46.670 +  unsigned int version, flags0, flags1, transform;
  46.671 +
  46.672 +  if (datalen >= APP14_DATA_LEN &&
  46.673 +      GETJOCTET(data[0]) == 0x41 &&
  46.674 +      GETJOCTET(data[1]) == 0x64 &&
  46.675 +      GETJOCTET(data[2]) == 0x6F &&
  46.676 +      GETJOCTET(data[3]) == 0x62 &&
  46.677 +      GETJOCTET(data[4]) == 0x65) {
  46.678 +    /* Found Adobe APP14 marker */
  46.679 +    version = (GETJOCTET(data[5]) << 8) + GETJOCTET(data[6]);
  46.680 +    flags0 = (GETJOCTET(data[7]) << 8) + GETJOCTET(data[8]);
  46.681 +    flags1 = (GETJOCTET(data[9]) << 8) + GETJOCTET(data[10]);
  46.682 +    transform = GETJOCTET(data[11]);
  46.683 +    TRACEMS4(cinfo, 1, JTRC_ADOBE, version, flags0, flags1, transform);
  46.684 +    cinfo->saw_Adobe_marker = TRUE;
  46.685 +    cinfo->Adobe_transform = (UINT8) transform;
  46.686 +  } else {
  46.687 +    /* Start of APP14 does not match "Adobe", or too short */
  46.688 +    TRACEMS1(cinfo, 1, JTRC_APP14, (int) (datalen + remaining));
  46.689 +  }
  46.690 +}
  46.691 +
  46.692 +
  46.693 +METHODDEF(boolean)
  46.694 +get_interesting_appn (j_decompress_ptr cinfo)
  46.695 +/* Process an APP0 or APP14 marker without saving it */
  46.696 +{
  46.697 +  INT32 length;
  46.698 +  JOCTET b[APPN_DATA_LEN];
  46.699 +  unsigned int i, numtoread;
  46.700 +  INPUT_VARS(cinfo);
  46.701 +
  46.702 +  INPUT_2BYTES(cinfo, length, return FALSE);
  46.703 +  length -= 2;
  46.704 +
  46.705 +  /* get the interesting part of the marker data */
  46.706 +  if (length >= APPN_DATA_LEN)
  46.707 +    numtoread = APPN_DATA_LEN;
  46.708 +  else if (length > 0)
  46.709 +    numtoread = (unsigned int) length;
  46.710 +  else
  46.711 +    numtoread = 0;
  46.712 +  for (i = 0; i < numtoread; i++)
  46.713 +    INPUT_BYTE(cinfo, b[i], return FALSE);
  46.714 +  length -= numtoread;
  46.715 +
  46.716 +  /* process it */
  46.717 +  switch (cinfo->unread_marker) {
  46.718 +  case M_APP0:
  46.719 +    examine_app0(cinfo, (JOCTET FAR *) b, numtoread, length);
  46.720 +    break;
  46.721 +  case M_APP14:
  46.722 +    examine_app14(cinfo, (JOCTET FAR *) b, numtoread, length);
  46.723 +    break;
  46.724 +  default:
  46.725 +    /* can't get here unless jpeg_save_markers chooses wrong processor */
  46.726 +    ERREXIT1(cinfo, JERR_UNKNOWN_MARKER, cinfo->unread_marker);
  46.727 +    break;
  46.728 +  }
  46.729 +
  46.730 +  /* skip any remaining data -- could be lots */
  46.731 +  INPUT_SYNC(cinfo);
  46.732 +  if (length > 0)
  46.733 +    (*cinfo->src->skip_input_data) (cinfo, (long) length);
  46.734 +
  46.735 +  return TRUE;
  46.736 +}
  46.737 +
  46.738 +
  46.739 +#ifdef SAVE_MARKERS_SUPPORTED
  46.740 +
  46.741 +METHODDEF(boolean)
  46.742 +save_marker (j_decompress_ptr cinfo)
  46.743 +/* Save an APPn or COM marker into the marker list */
  46.744 +{
  46.745 +  my_marker_ptr marker = (my_marker_ptr) cinfo->marker;
  46.746 +  jpeg_saved_marker_ptr cur_marker = marker->cur_marker;
  46.747 +  unsigned int bytes_read, data_length;
  46.748 +  JOCTET FAR * data;
  46.749 +  INT32 length = 0;
  46.750 +  INPUT_VARS(cinfo);
  46.751 +
  46.752 +  if (cur_marker == NULL) {
  46.753 +    /* begin reading a marker */
  46.754 +    INPUT_2BYTES(cinfo, length, return FALSE);
  46.755 +    length -= 2;
  46.756 +    if (length >= 0) {		/* watch out for bogus length word */
  46.757 +      /* figure out how much we want to save */
  46.758 +      unsigned int limit;
  46.759 +      if (cinfo->unread_marker == (int) M_COM)
  46.760 +	limit = marker->length_limit_COM;
  46.761 +      else
  46.762 +	limit = marker->length_limit_APPn[cinfo->unread_marker - (int) M_APP0];
  46.763 +      if ((unsigned int) length < limit)
  46.764 +	limit = (unsigned int) length;
  46.765 +      /* allocate and initialize the marker item */
  46.766 +      cur_marker = (jpeg_saved_marker_ptr)
  46.767 +	(*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  46.768 +				    SIZEOF(struct jpeg_marker_struct) + limit);
  46.769 +      cur_marker->next = NULL;
  46.770 +      cur_marker->marker = (UINT8) cinfo->unread_marker;
  46.771 +      cur_marker->original_length = (unsigned int) length;
  46.772 +      cur_marker->data_length = limit;
  46.773 +      /* data area is just beyond the jpeg_marker_struct */
  46.774 +      data = cur_marker->data = (JOCTET FAR *) (cur_marker + 1);
  46.775 +      marker->cur_marker = cur_marker;
  46.776 +      marker->bytes_read = 0;
  46.777 +      bytes_read = 0;
  46.778 +      data_length = limit;
  46.779 +    } else {
  46.780 +      /* deal with bogus length word */
  46.781 +      bytes_read = data_length = 0;
  46.782 +      data = NULL;
  46.783 +    }
  46.784 +  } else {
  46.785 +    /* resume reading a marker */
  46.786 +    bytes_read = marker->bytes_read;
  46.787 +    data_length = cur_marker->data_length;
  46.788 +    data = cur_marker->data + bytes_read;
  46.789 +  }
  46.790 +
  46.791 +  while (bytes_read < data_length) {
  46.792 +    INPUT_SYNC(cinfo);		/* move the restart point to here */
  46.793 +    marker->bytes_read = bytes_read;
  46.794 +    /* If there's not at least one byte in buffer, suspend */
  46.795 +    MAKE_BYTE_AVAIL(cinfo, return FALSE);
  46.796 +    /* Copy bytes with reasonable rapidity */
  46.797 +    while (bytes_read < data_length && bytes_in_buffer > 0) {
  46.798 +      *data++ = *next_input_byte++;
  46.799 +      bytes_in_buffer--;
  46.800 +      bytes_read++;
  46.801 +    }
  46.802 +  }
  46.803 +
  46.804 +  /* Done reading what we want to read */
  46.805 +  if (cur_marker != NULL) {	/* will be NULL if bogus length word */
  46.806 +    /* Add new marker to end of list */
  46.807 +    if (cinfo->marker_list == NULL) {
  46.808 +      cinfo->marker_list = cur_marker;
  46.809 +    } else {
  46.810 +      jpeg_saved_marker_ptr prev = cinfo->marker_list;
  46.811 +      while (prev->next != NULL)
  46.812 +	prev = prev->next;
  46.813 +      prev->next = cur_marker;
  46.814 +    }
  46.815 +    /* Reset pointer & calc remaining data length */
  46.816 +    data = cur_marker->data;
  46.817 +    length = cur_marker->original_length - data_length;
  46.818 +  }
  46.819 +  /* Reset to initial state for next marker */
  46.820 +  marker->cur_marker = NULL;
  46.821 +
  46.822 +  /* Process the marker if interesting; else just make a generic trace msg */
  46.823 +  switch (cinfo->unread_marker) {
  46.824 +  case M_APP0:
  46.825 +    examine_app0(cinfo, data, data_length, length);
  46.826 +    break;
  46.827 +  case M_APP14:
  46.828 +    examine_app14(cinfo, data, data_length, length);
  46.829 +    break;
  46.830 +  default:
  46.831 +    TRACEMS2(cinfo, 1, JTRC_MISC_MARKER, cinfo->unread_marker,
  46.832 +	     (int) (data_length + length));
  46.833 +    break;
  46.834 +  }
  46.835 +
  46.836 +  /* skip any remaining data -- could be lots */
  46.837 +  INPUT_SYNC(cinfo);		/* do before skip_input_data */
  46.838 +  if (length > 0)
  46.839 +    (*cinfo->src->skip_input_data) (cinfo, (long) length);
  46.840 +
  46.841 +  return TRUE;
  46.842 +}
  46.843 +
  46.844 +#endif /* SAVE_MARKERS_SUPPORTED */
  46.845 +
  46.846 +
  46.847 +METHODDEF(boolean)
  46.848 +skip_variable (j_decompress_ptr cinfo)
  46.849 +/* Skip over an unknown or uninteresting variable-length marker */
  46.850 +{
  46.851 +  INT32 length;
  46.852 +  INPUT_VARS(cinfo);
  46.853 +
  46.854 +  INPUT_2BYTES(cinfo, length, return FALSE);
  46.855 +  length -= 2;
  46.856 +  
  46.857 +  TRACEMS2(cinfo, 1, JTRC_MISC_MARKER, cinfo->unread_marker, (int) length);
  46.858 +
  46.859 +  INPUT_SYNC(cinfo);		/* do before skip_input_data */
  46.860 +  if (length > 0)
  46.861 +    (*cinfo->src->skip_input_data) (cinfo, (long) length);
  46.862 +
  46.863 +  return TRUE;
  46.864 +}
  46.865 +
  46.866 +
  46.867 +/*
  46.868 + * Find the next JPEG marker, save it in cinfo->unread_marker.
  46.869 + * Returns FALSE if had to suspend before reaching a marker;
  46.870 + * in that case cinfo->unread_marker is unchanged.
  46.871 + *
  46.872 + * Note that the result might not be a valid marker code,
  46.873 + * but it will never be 0 or FF.
  46.874 + */
  46.875 +
  46.876 +LOCAL(boolean)
  46.877 +next_marker (j_decompress_ptr cinfo)
  46.878 +{
  46.879 +  int c;
  46.880 +  INPUT_VARS(cinfo);
  46.881 +
  46.882 +  for (;;) {
  46.883 +    INPUT_BYTE(cinfo, c, return FALSE);
  46.884 +    /* Skip any non-FF bytes.
  46.885 +     * This may look a bit inefficient, but it will not occur in a valid file.
  46.886 +     * We sync after each discarded byte so that a suspending data source
  46.887 +     * can discard the byte from its buffer.
  46.888 +     */
  46.889 +    while (c != 0xFF) {
  46.890 +      cinfo->marker->discarded_bytes++;
  46.891 +      INPUT_SYNC(cinfo);
  46.892 +      INPUT_BYTE(cinfo, c, return FALSE);
  46.893 +    }
  46.894 +    /* This loop swallows any duplicate FF bytes.  Extra FFs are legal as
  46.895 +     * pad bytes, so don't count them in discarded_bytes.  We assume there
  46.896 +     * will not be so many consecutive FF bytes as to overflow a suspending
  46.897 +     * data source's input buffer.
  46.898 +     */
  46.899 +    do {
  46.900 +      INPUT_BYTE(cinfo, c, return FALSE);
  46.901 +    } while (c == 0xFF);
  46.902 +    if (c != 0)
  46.903 +      break;			/* found a valid marker, exit loop */
  46.904 +    /* Reach here if we found a stuffed-zero data sequence (FF/00).
  46.905 +     * Discard it and loop back to try again.
  46.906 +     */
  46.907 +    cinfo->marker->discarded_bytes += 2;
  46.908 +    INPUT_SYNC(cinfo);
  46.909 +  }
  46.910 +
  46.911 +  if (cinfo->marker->discarded_bytes != 0) {
  46.912 +    WARNMS2(cinfo, JWRN_EXTRANEOUS_DATA, cinfo->marker->discarded_bytes, c);
  46.913 +    cinfo->marker->discarded_bytes = 0;
  46.914 +  }
  46.915 +
  46.916 +  cinfo->unread_marker = c;
  46.917 +
  46.918 +  INPUT_SYNC(cinfo);
  46.919 +  return TRUE;
  46.920 +}
  46.921 +
  46.922 +
  46.923 +LOCAL(boolean)
  46.924 +first_marker (j_decompress_ptr cinfo)
  46.925 +/* Like next_marker, but used to obtain the initial SOI marker. */
  46.926 +/* For this marker, we do not allow preceding garbage or fill; otherwise,
  46.927 + * we might well scan an entire input file before realizing it ain't JPEG.
  46.928 + * If an application wants to process non-JFIF files, it must seek to the
  46.929 + * SOI before calling the JPEG library.
  46.930 + */
  46.931 +{
  46.932 +  int c, c2;
  46.933 +  INPUT_VARS(cinfo);
  46.934 +
  46.935 +  INPUT_BYTE(cinfo, c, return FALSE);
  46.936 +  INPUT_BYTE(cinfo, c2, return FALSE);
  46.937 +  if (c != 0xFF || c2 != (int) M_SOI)
  46.938 +    ERREXIT2(cinfo, JERR_NO_SOI, c, c2);
  46.939 +
  46.940 +  cinfo->unread_marker = c2;
  46.941 +
  46.942 +  INPUT_SYNC(cinfo);
  46.943 +  return TRUE;
  46.944 +}
  46.945 +
  46.946 +
  46.947 +/*
  46.948 + * Read markers until SOS or EOI.
  46.949 + *
  46.950 + * Returns same codes as are defined for jpeg_consume_input:
  46.951 + * JPEG_SUSPENDED, JPEG_REACHED_SOS, or JPEG_REACHED_EOI.
  46.952 + */
  46.953 +
  46.954 +METHODDEF(int)
  46.955 +read_markers (j_decompress_ptr cinfo)
  46.956 +{
  46.957 +  /* Outer loop repeats once for each marker. */
  46.958 +  for (;;) {
  46.959 +    /* Collect the marker proper, unless we already did. */
  46.960 +    /* NB: first_marker() enforces the requirement that SOI appear first. */
  46.961 +    if (cinfo->unread_marker == 0) {
  46.962 +      if (! cinfo->marker->saw_SOI) {
  46.963 +	if (! first_marker(cinfo))
  46.964 +	  return JPEG_SUSPENDED;
  46.965 +      } else {
  46.966 +	if (! next_marker(cinfo))
  46.967 +	  return JPEG_SUSPENDED;
  46.968 +      }
  46.969 +    }
  46.970 +    /* At this point cinfo->unread_marker contains the marker code and the
  46.971 +     * input point is just past the marker proper, but before any parameters.
  46.972 +     * A suspension will cause us to return with this state still true.
  46.973 +     */
  46.974 +    switch (cinfo->unread_marker) {
  46.975 +    case M_SOI:
  46.976 +      if (! get_soi(cinfo))
  46.977 +	return JPEG_SUSPENDED;
  46.978 +      break;
  46.979 +
  46.980 +    case M_SOF0:		/* Baseline */
  46.981 +    case M_SOF1:		/* Extended sequential, Huffman */
  46.982 +      if (! get_sof(cinfo, FALSE, FALSE))
  46.983 +	return JPEG_SUSPENDED;
  46.984 +      break;
  46.985 +
  46.986 +    case M_SOF2:		/* Progressive, Huffman */
  46.987 +      if (! get_sof(cinfo, TRUE, FALSE))
  46.988 +	return JPEG_SUSPENDED;
  46.989 +      break;
  46.990 +
  46.991 +    case M_SOF9:		/* Extended sequential, arithmetic */
  46.992 +      if (! get_sof(cinfo, FALSE, TRUE))
  46.993 +	return JPEG_SUSPENDED;
  46.994 +      break;
  46.995 +
  46.996 +    case M_SOF10:		/* Progressive, arithmetic */
  46.997 +      if (! get_sof(cinfo, TRUE, TRUE))
  46.998 +	return JPEG_SUSPENDED;
  46.999 +      break;
 46.1000 +
 46.1001 +    /* Currently unsupported SOFn types */
 46.1002 +    case M_SOF3:		/* Lossless, Huffman */
 46.1003 +    case M_SOF5:		/* Differential sequential, Huffman */
 46.1004 +    case M_SOF6:		/* Differential progressive, Huffman */
 46.1005 +    case M_SOF7:		/* Differential lossless, Huffman */
 46.1006 +    case M_JPG:			/* Reserved for JPEG extensions */
 46.1007 +    case M_SOF11:		/* Lossless, arithmetic */
 46.1008 +    case M_SOF13:		/* Differential sequential, arithmetic */
 46.1009 +    case M_SOF14:		/* Differential progressive, arithmetic */
 46.1010 +    case M_SOF15:		/* Differential lossless, arithmetic */
 46.1011 +      ERREXIT1(cinfo, JERR_SOF_UNSUPPORTED, cinfo->unread_marker);
 46.1012 +      break;
 46.1013 +
 46.1014 +    case M_SOS:
 46.1015 +      if (! get_sos(cinfo))
 46.1016 +	return JPEG_SUSPENDED;
 46.1017 +      cinfo->unread_marker = 0;	/* processed the marker */
 46.1018 +      return JPEG_REACHED_SOS;
 46.1019 +    
 46.1020 +    case M_EOI:
 46.1021 +      TRACEMS(cinfo, 1, JTRC_EOI);
 46.1022 +      cinfo->unread_marker = 0;	/* processed the marker */
 46.1023 +      return JPEG_REACHED_EOI;
 46.1024 +      
 46.1025 +    case M_DAC:
 46.1026 +      if (! get_dac(cinfo))
 46.1027 +	return JPEG_SUSPENDED;
 46.1028 +      break;
 46.1029 +      
 46.1030 +    case M_DHT:
 46.1031 +      if (! get_dht(cinfo))
 46.1032 +	return JPEG_SUSPENDED;
 46.1033 +      break;
 46.1034 +      
 46.1035 +    case M_DQT:
 46.1036 +      if (! get_dqt(cinfo))
 46.1037 +	return JPEG_SUSPENDED;
 46.1038 +      break;
 46.1039 +      
 46.1040 +    case M_DRI:
 46.1041 +      if (! get_dri(cinfo))
 46.1042 +	return JPEG_SUSPENDED;
 46.1043 +      break;
 46.1044 +      
 46.1045 +    case M_APP0:
 46.1046 +    case M_APP1:
 46.1047 +    case M_APP2:
 46.1048 +    case M_APP3:
 46.1049 +    case M_APP4:
 46.1050 +    case M_APP5:
 46.1051 +    case M_APP6:
 46.1052 +    case M_APP7:
 46.1053 +    case M_APP8:
 46.1054 +    case M_APP9:
 46.1055 +    case M_APP10:
 46.1056 +    case M_APP11:
 46.1057 +    case M_APP12:
 46.1058 +    case M_APP13:
 46.1059 +    case M_APP14:
 46.1060 +    case M_APP15:
 46.1061 +      if (! (*((my_marker_ptr) cinfo->marker)->process_APPn[
 46.1062 +		cinfo->unread_marker - (int) M_APP0]) (cinfo))
 46.1063 +	return JPEG_SUSPENDED;
 46.1064 +      break;
 46.1065 +      
 46.1066 +    case M_COM:
 46.1067 +      if (! (*((my_marker_ptr) cinfo->marker)->process_COM) (cinfo))
 46.1068 +	return JPEG_SUSPENDED;
 46.1069 +      break;
 46.1070 +
 46.1071 +    case M_RST0:		/* these are all parameterless */
 46.1072 +    case M_RST1:
 46.1073 +    case M_RST2:
 46.1074 +    case M_RST3:
 46.1075 +    case M_RST4:
 46.1076 +    case M_RST5:
 46.1077 +    case M_RST6:
 46.1078 +    case M_RST7:
 46.1079 +    case M_TEM:
 46.1080 +      TRACEMS1(cinfo, 1, JTRC_PARMLESS_MARKER, cinfo->unread_marker);
 46.1081 +      break;
 46.1082 +
 46.1083 +    case M_DNL:			/* Ignore DNL ... perhaps the wrong thing */
 46.1084 +      if (! skip_variable(cinfo))
 46.1085 +	return JPEG_SUSPENDED;
 46.1086 +      break;
 46.1087 +
 46.1088 +    default:			/* must be DHP, EXP, JPGn, or RESn */
 46.1089 +      /* For now, we treat the reserved markers as fatal errors since they are
 46.1090 +       * likely to be used to signal incompatible JPEG Part 3 extensions.
 46.1091 +       * Once the JPEG 3 version-number marker is well defined, this code
 46.1092 +       * ought to change!
 46.1093 +       */
 46.1094 +      ERREXIT1(cinfo, JERR_UNKNOWN_MARKER, cinfo->unread_marker);
 46.1095 +      break;
 46.1096 +    }
 46.1097 +    /* Successfully processed marker, so reset state variable */
 46.1098 +    cinfo->unread_marker = 0;
 46.1099 +  } /* end loop */
 46.1100 +}
 46.1101 +
 46.1102 +
 46.1103 +/*
 46.1104 + * Read a restart marker, which is expected to appear next in the datastream;
 46.1105 + * if the marker is not there, take appropriate recovery action.
 46.1106 + * Returns FALSE if suspension is required.
 46.1107 + *
 46.1108 + * This is called by the entropy decoder after it has read an appropriate
 46.1109 + * number of MCUs.  cinfo->unread_marker may be nonzero if the entropy decoder
 46.1110 + * has already read a marker from the data source.  Under normal conditions
 46.1111 + * cinfo->unread_marker will be reset to 0 before returning; if not reset,
 46.1112 + * it holds a marker which the decoder will be unable to read past.
 46.1113 + */
 46.1114 +
 46.1115 +METHODDEF(boolean)
 46.1116 +read_restart_marker (j_decompress_ptr cinfo)
 46.1117 +{
 46.1118 +  /* Obtain a marker unless we already did. */
 46.1119 +  /* Note that next_marker will complain if it skips any data. */
 46.1120 +  if (cinfo->unread_marker == 0) {
 46.1121 +    if (! next_marker(cinfo))
 46.1122 +      return FALSE;
 46.1123 +  }
 46.1124 +
 46.1125 +  if (cinfo->unread_marker ==
 46.1126 +      ((int) M_RST0 + cinfo->marker->next_restart_num)) {
 46.1127 +    /* Normal case --- swallow the marker and let entropy decoder continue */
 46.1128 +    TRACEMS1(cinfo, 3, JTRC_RST, cinfo->marker->next_restart_num);
 46.1129 +    cinfo->unread_marker = 0;
 46.1130 +  } else {
 46.1131 +    /* Uh-oh, the restart markers have been messed up. */
 46.1132 +    /* Let the data source manager determine how to resync. */
 46.1133 +    if (! (*cinfo->src->resync_to_restart) (cinfo,
 46.1134 +					    cinfo->marker->next_restart_num))
 46.1135 +      return FALSE;
 46.1136 +  }
 46.1137 +
 46.1138 +  /* Update next-restart state */
 46.1139 +  cinfo->marker->next_restart_num = (cinfo->marker->next_restart_num + 1) & 7;
 46.1140 +
 46.1141 +  return TRUE;
 46.1142 +}
 46.1143 +
 46.1144 +
 46.1145 +/*
 46.1146 + * This is the default resync_to_restart method for data source managers
 46.1147 + * to use if they don't have any better approach.  Some data source managers
 46.1148 + * may be able to back up, or may have additional knowledge about the data
 46.1149 + * which permits a more intelligent recovery strategy; such managers would
 46.1150 + * presumably supply their own resync method.
 46.1151 + *
 46.1152 + * read_restart_marker calls resync_to_restart if it finds a marker other than
 46.1153 + * the restart marker it was expecting.  (This code is *not* used unless
 46.1154 + * a nonzero restart interval has been declared.)  cinfo->unread_marker is
 46.1155 + * the marker code actually found (might be anything, except 0 or FF).
 46.1156 + * The desired restart marker number (0..7) is passed as a parameter.
 46.1157 + * This routine is supposed to apply whatever error recovery strategy seems
 46.1158 + * appropriate in order to position the input stream to the next data segment.
 46.1159 + * Note that cinfo->unread_marker is treated as a marker appearing before
 46.1160 + * the current data-source input point; usually it should be reset to zero
 46.1161 + * before returning.
 46.1162 + * Returns FALSE if suspension is required.
 46.1163 + *
 46.1164 + * This implementation is substantially constrained by wanting to treat the
 46.1165 + * input as a data stream; this means we can't back up.  Therefore, we have
 46.1166 + * only the following actions to work with:
 46.1167 + *   1. Simply discard the marker and let the entropy decoder resume at next
 46.1168 + *      byte of file.
 46.1169 + *   2. Read forward until we find another marker, discarding intervening
 46.1170 + *      data.  (In theory we could look ahead within the current bufferload,
 46.1171 + *      without having to discard data if we don't find the desired marker.
 46.1172 + *      This idea is not implemented here, in part because it makes behavior
 46.1173 + *      dependent on buffer size and chance buffer-boundary positions.)
 46.1174 + *   3. Leave the marker unread (by failing to zero cinfo->unread_marker).
 46.1175 + *      This will cause the entropy decoder to process an empty data segment,
 46.1176 + *      inserting dummy zeroes, and then we will reprocess the marker.
 46.1177 + *
 46.1178 + * #2 is appropriate if we think the desired marker lies ahead, while #3 is
 46.1179 + * appropriate if the found marker is a future restart marker (indicating
 46.1180 + * that we have missed the desired restart marker, probably because it got
 46.1181 + * corrupted).
 46.1182 + * We apply #2 or #3 if the found marker is a restart marker no more than
 46.1183 + * two counts behind or ahead of the expected one.  We also apply #2 if the
 46.1184 + * found marker is not a legal JPEG marker code (it's certainly bogus data).
 46.1185 + * If the found marker is a restart marker more than 2 counts away, we do #1
 46.1186 + * (too much risk that the marker is erroneous; with luck we will be able to
 46.1187 + * resync at some future point).
 46.1188 + * For any valid non-restart JPEG marker, we apply #3.  This keeps us from
 46.1189 + * overrunning the end of a scan.  An implementation limited to single-scan
 46.1190 + * files might find it better to apply #2 for markers other than EOI, since
 46.1191 + * any other marker would have to be bogus data in that case.
 46.1192 + */
 46.1193 +
 46.1194 +GLOBAL(boolean)
 46.1195 +jpeg_resync_to_restart (j_decompress_ptr cinfo, int desired)
 46.1196 +{
 46.1197 +  int marker = cinfo->unread_marker;
 46.1198 +  int action = 1;
 46.1199 +  
 46.1200 +  /* Always put up a warning. */
 46.1201 +  WARNMS2(cinfo, JWRN_MUST_RESYNC, marker, desired);
 46.1202 +  
 46.1203 +  /* Outer loop handles repeated decision after scanning forward. */
 46.1204 +  for (;;) {
 46.1205 +    if (marker < (int) M_SOF0)
 46.1206 +      action = 2;		/* invalid marker */
 46.1207 +    else if (marker < (int) M_RST0 || marker > (int) M_RST7)
 46.1208 +      action = 3;		/* valid non-restart marker */
 46.1209 +    else {
 46.1210 +      if (marker == ((int) M_RST0 + ((desired+1) & 7)) ||
 46.1211 +	  marker == ((int) M_RST0 + ((desired+2) & 7)))
 46.1212 +	action = 3;		/* one of the next two expected restarts */
 46.1213 +      else if (marker == ((int) M_RST0 + ((desired-1) & 7)) ||
 46.1214 +	       marker == ((int) M_RST0 + ((desired-2) & 7)))
 46.1215 +	action = 2;		/* a prior restart, so advance */
 46.1216 +      else
 46.1217 +	action = 1;		/* desired restart or too far away */
 46.1218 +    }
 46.1219 +    TRACEMS2(cinfo, 4, JTRC_RECOVERY_ACTION, marker, action);
 46.1220 +    switch (action) {
 46.1221 +    case 1:
 46.1222 +      /* Discard marker and let entropy decoder resume processing. */
 46.1223 +      cinfo->unread_marker = 0;
 46.1224 +      return TRUE;
 46.1225 +    case 2:
 46.1226 +      /* Scan to the next marker, and repeat the decision loop. */
 46.1227 +      if (! next_marker(cinfo))
 46.1228 +	return FALSE;
 46.1229 +      marker = cinfo->unread_marker;
 46.1230 +      break;
 46.1231 +    case 3:
 46.1232 +      /* Return without advancing past this marker. */
 46.1233 +      /* Entropy decoder will be forced to process an empty segment. */
 46.1234 +      return TRUE;
 46.1235 +    }
 46.1236 +  } /* end loop */
 46.1237 +}
 46.1238 +
 46.1239 +
 46.1240 +/*
 46.1241 + * Reset marker processing state to begin a fresh datastream.
 46.1242 + */
 46.1243 +
 46.1244 +METHODDEF(void)
 46.1245 +reset_marker_reader (j_decompress_ptr cinfo)
 46.1246 +{
 46.1247 +  my_marker_ptr marker = (my_marker_ptr) cinfo->marker;
 46.1248 +
 46.1249 +  cinfo->comp_info = NULL;		/* until allocated by get_sof */
 46.1250 +  cinfo->input_scan_number = 0;		/* no SOS seen yet */
 46.1251 +  cinfo->unread_marker = 0;		/* no pending marker */
 46.1252 +  marker->pub.saw_SOI = FALSE;		/* set internal state too */
 46.1253 +  marker->pub.saw_SOF = FALSE;
 46.1254 +  marker->pub.discarded_bytes = 0;
 46.1255 +  marker->cur_marker = NULL;
 46.1256 +}
 46.1257 +
 46.1258 +
 46.1259 +/*
 46.1260 + * Initialize the marker reader module.
 46.1261 + * This is called only once, when the decompression object is created.
 46.1262 + */
 46.1263 +
 46.1264 +GLOBAL(void)
 46.1265 +jinit_marker_reader (j_decompress_ptr cinfo)
 46.1266 +{
 46.1267 +  my_marker_ptr marker;
 46.1268 +  int i;
 46.1269 +
 46.1270 +  /* Create subobject in permanent pool */
 46.1271 +  marker = (my_marker_ptr)
 46.1272 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
 46.1273 +				SIZEOF(my_marker_reader));
 46.1274 +  cinfo->marker = (struct jpeg_marker_reader *) marker;
 46.1275 +  /* Initialize public method pointers */
 46.1276 +  marker->pub.reset_marker_reader = reset_marker_reader;
 46.1277 +  marker->pub.read_markers = read_markers;
 46.1278 +  marker->pub.read_restart_marker = read_restart_marker;
 46.1279 +  /* Initialize COM/APPn processing.
 46.1280 +   * By default, we examine and then discard APP0 and APP14,
 46.1281 +   * but simply discard COM and all other APPn.
 46.1282 +   */
 46.1283 +  marker->process_COM = skip_variable;
 46.1284 +  marker->length_limit_COM = 0;
 46.1285 +  for (i = 0; i < 16; i++) {
 46.1286 +    marker->process_APPn[i] = skip_variable;
 46.1287 +    marker->length_limit_APPn[i] = 0;
 46.1288 +  }
 46.1289 +  marker->process_APPn[0] = get_interesting_appn;
 46.1290 +  marker->process_APPn[14] = get_interesting_appn;
 46.1291 +  /* Reset marker processing state */
 46.1292 +  reset_marker_reader(cinfo);
 46.1293 +}
 46.1294 +
 46.1295 +
 46.1296 +/*
 46.1297 + * Control saving of COM and APPn markers into marker_list.
 46.1298 + */
 46.1299 +
 46.1300 +#ifdef SAVE_MARKERS_SUPPORTED
 46.1301 +
 46.1302 +GLOBAL(void)
 46.1303 +jpeg_save_markers (j_decompress_ptr cinfo, int marker_code,
 46.1304 +		   unsigned int length_limit)
 46.1305 +{
 46.1306 +  my_marker_ptr marker = (my_marker_ptr) cinfo->marker;
 46.1307 +  long maxlength;
 46.1308 +  jpeg_marker_parser_method processor;
 46.1309 +
 46.1310 +  /* Length limit mustn't be larger than what we can allocate
 46.1311 +   * (should only be a concern in a 16-bit environment).
 46.1312 +   */
 46.1313 +  maxlength = cinfo->mem->max_alloc_chunk - SIZEOF(struct jpeg_marker_struct);
 46.1314 +  if (((long) length_limit) > maxlength)
 46.1315 +    length_limit = (unsigned int) maxlength;
 46.1316 +
 46.1317 +  /* Choose processor routine to use.
 46.1318 +   * APP0/APP14 have special requirements.
 46.1319 +   */
 46.1320 +  if (length_limit) {
 46.1321 +    processor = save_marker;
 46.1322 +    /* If saving APP0/APP14, save at least enough for our internal use. */
 46.1323 +    if (marker_code == (int) M_APP0 && length_limit < APP0_DATA_LEN)
 46.1324 +      length_limit = APP0_DATA_LEN;
 46.1325 +    else if (marker_code == (int) M_APP14 && length_limit < APP14_DATA_LEN)
 46.1326 +      length_limit = APP14_DATA_LEN;
 46.1327 +  } else {
 46.1328 +    processor = skip_variable;
 46.1329 +    /* If discarding APP0/APP14, use our regular on-the-fly processor. */
 46.1330 +    if (marker_code == (int) M_APP0 || marker_code == (int) M_APP14)
 46.1331 +      processor = get_interesting_appn;
 46.1332 +  }
 46.1333 +
 46.1334 +  if (marker_code == (int) M_COM) {
 46.1335 +    marker->process_COM = processor;
 46.1336 +    marker->length_limit_COM = length_limit;
 46.1337 +  } else if (marker_code >= (int) M_APP0 && marker_code <= (int) M_APP15) {
 46.1338 +    marker->process_APPn[marker_code - (int) M_APP0] = processor;
 46.1339 +    marker->length_limit_APPn[marker_code - (int) M_APP0] = length_limit;
 46.1340 +  } else
 46.1341 +    ERREXIT1(cinfo, JERR_UNKNOWN_MARKER, marker_code);
 46.1342 +}
 46.1343 +
 46.1344 +#endif /* SAVE_MARKERS_SUPPORTED */
 46.1345 +
 46.1346 +
 46.1347 +/*
 46.1348 + * Install a special processing method for COM or APPn markers.
 46.1349 + */
 46.1350 +
 46.1351 +GLOBAL(void)
 46.1352 +jpeg_set_marker_processor (j_decompress_ptr cinfo, int marker_code,
 46.1353 +			   jpeg_marker_parser_method routine)
 46.1354 +{
 46.1355 +  my_marker_ptr marker = (my_marker_ptr) cinfo->marker;
 46.1356 +
 46.1357 +  if (marker_code == (int) M_COM)
 46.1358 +    marker->process_COM = routine;
 46.1359 +  else if (marker_code >= (int) M_APP0 && marker_code <= (int) M_APP15)
 46.1360 +    marker->process_APPn[marker_code - (int) M_APP0] = routine;
 46.1361 +  else
 46.1362 +    ERREXIT1(cinfo, JERR_UNKNOWN_MARKER, marker_code);
 46.1363 +}
    47.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    47.2 +++ b/libs/libjpeg/jdmaster.c	Sat Sep 19 05:51:51 2015 +0300
    47.3 @@ -0,0 +1,557 @@
    47.4 +/*
    47.5 + * jdmaster.c
    47.6 + *
    47.7 + * Copyright (C) 1991-1997, Thomas G. Lane.
    47.8 + * This file is part of the Independent JPEG Group's software.
    47.9 + * For conditions of distribution and use, see the accompanying README file.
   47.10 + *
   47.11 + * This file contains master control logic for the JPEG decompressor.
   47.12 + * These routines are concerned with selecting the modules to be executed
   47.13 + * and with determining the number of passes and the work to be done in each
   47.14 + * pass.
   47.15 + */
   47.16 +
   47.17 +#define JPEG_INTERNALS
   47.18 +#include "jinclude.h"
   47.19 +#include "jpeglib.h"
   47.20 +
   47.21 +
   47.22 +/* Private state */
   47.23 +
   47.24 +typedef struct {
   47.25 +  struct jpeg_decomp_master pub; /* public fields */
   47.26 +
   47.27 +  int pass_number;		/* # of passes completed */
   47.28 +
   47.29 +  boolean using_merged_upsample; /* TRUE if using merged upsample/cconvert */
   47.30 +
   47.31 +  /* Saved references to initialized quantizer modules,
   47.32 +   * in case we need to switch modes.
   47.33 +   */
   47.34 +  struct jpeg_color_quantizer * quantizer_1pass;
   47.35 +  struct jpeg_color_quantizer * quantizer_2pass;
   47.36 +} my_decomp_master;
   47.37 +
   47.38 +typedef my_decomp_master * my_master_ptr;
   47.39 +
   47.40 +
   47.41 +/*
   47.42 + * Determine whether merged upsample/color conversion should be used.
   47.43 + * CRUCIAL: this must match the actual capabilities of jdmerge.c!
   47.44 + */
   47.45 +
   47.46 +LOCAL(boolean)
   47.47 +use_merged_upsample (j_decompress_ptr cinfo)
   47.48 +{
   47.49 +#ifdef UPSAMPLE_MERGING_SUPPORTED
   47.50 +  /* Merging is the equivalent of plain box-filter upsampling */
   47.51 +  if (cinfo->do_fancy_upsampling || cinfo->CCIR601_sampling)
   47.52 +    return FALSE;
   47.53 +  /* jdmerge.c only supports YCC=>RGB color conversion */
   47.54 +  if (cinfo->jpeg_color_space != JCS_YCbCr || cinfo->num_components != 3 ||
   47.55 +      cinfo->out_color_space != JCS_RGB ||
   47.56 +      cinfo->out_color_components != RGB_PIXELSIZE)
   47.57 +    return FALSE;
   47.58 +  /* and it only handles 2h1v or 2h2v sampling ratios */
   47.59 +  if (cinfo->comp_info[0].h_samp_factor != 2 ||
   47.60 +      cinfo->comp_info[1].h_samp_factor != 1 ||
   47.61 +      cinfo->comp_info[2].h_samp_factor != 1 ||
   47.62 +      cinfo->comp_info[0].v_samp_factor >  2 ||
   47.63 +      cinfo->comp_info[1].v_samp_factor != 1 ||
   47.64 +      cinfo->comp_info[2].v_samp_factor != 1)
   47.65 +    return FALSE;
   47.66 +  /* furthermore, it doesn't work if we've scaled the IDCTs differently */
   47.67 +  if (cinfo->comp_info[0].DCT_scaled_size != cinfo->min_DCT_scaled_size ||
   47.68 +      cinfo->comp_info[1].DCT_scaled_size != cinfo->min_DCT_scaled_size ||
   47.69 +      cinfo->comp_info[2].DCT_scaled_size != cinfo->min_DCT_scaled_size)
   47.70 +    return FALSE;
   47.71 +  /* ??? also need to test for upsample-time rescaling, when & if supported */
   47.72 +  return TRUE;			/* by golly, it'll work... */
   47.73 +#else
   47.74 +  return FALSE;
   47.75 +#endif
   47.76 +}
   47.77 +
   47.78 +
   47.79 +/*
   47.80 + * Compute output image dimensions and related values.
   47.81 + * NOTE: this is exported for possible use by application.
   47.82 + * Hence it mustn't do anything that can't be done twice.
   47.83 + * Also note that it may be called before the master module is initialized!
   47.84 + */
   47.85 +
   47.86 +GLOBAL(void)
   47.87 +jpeg_calc_output_dimensions (j_decompress_ptr cinfo)
   47.88 +/* Do computations that are needed before master selection phase */
   47.89 +{
   47.90 +#ifdef IDCT_SCALING_SUPPORTED
   47.91 +  int ci;
   47.92 +  jpeg_component_info *compptr;
   47.93 +#endif
   47.94 +
   47.95 +  /* Prevent application from calling me at wrong times */
   47.96 +  if (cinfo->global_state != DSTATE_READY)
   47.97 +    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
   47.98 +
   47.99 +#ifdef IDCT_SCALING_SUPPORTED
  47.100 +
  47.101 +  /* Compute actual output image dimensions and DCT scaling choices. */
  47.102 +  if (cinfo->scale_num * 8 <= cinfo->scale_denom) {
  47.103 +    /* Provide 1/8 scaling */
  47.104 +    cinfo->output_width = (JDIMENSION)
  47.105 +      jdiv_round_up((long) cinfo->image_width, 8L);
  47.106 +    cinfo->output_height = (JDIMENSION)
  47.107 +      jdiv_round_up((long) cinfo->image_height, 8L);
  47.108 +    cinfo->min_DCT_scaled_size = 1;
  47.109 +  } else if (cinfo->scale_num * 4 <= cinfo->scale_denom) {
  47.110 +    /* Provide 1/4 scaling */
  47.111 +    cinfo->output_width = (JDIMENSION)
  47.112 +      jdiv_round_up((long) cinfo->image_width, 4L);
  47.113 +    cinfo->output_height = (JDIMENSION)
  47.114 +      jdiv_round_up((long) cinfo->image_height, 4L);
  47.115 +    cinfo->min_DCT_scaled_size = 2;
  47.116 +  } else if (cinfo->scale_num * 2 <= cinfo->scale_denom) {
  47.117 +    /* Provide 1/2 scaling */
  47.118 +    cinfo->output_width = (JDIMENSION)
  47.119 +      jdiv_round_up((long) cinfo->image_width, 2L);
  47.120 +    cinfo->output_height = (JDIMENSION)
  47.121 +      jdiv_round_up((long) cinfo->image_height, 2L);
  47.122 +    cinfo->min_DCT_scaled_size = 4;
  47.123 +  } else {
  47.124 +    /* Provide 1/1 scaling */
  47.125 +    cinfo->output_width = cinfo->image_width;
  47.126 +    cinfo->output_height = cinfo->image_height;
  47.127 +    cinfo->min_DCT_scaled_size = DCTSIZE;
  47.128 +  }
  47.129 +  /* In selecting the actual DCT scaling for each component, we try to
  47.130 +   * scale up the chroma components via IDCT scaling rather than upsampling.
  47.131 +   * This saves time if the upsampler gets to use 1:1 scaling.
  47.132 +   * Note this code assumes that the supported DCT scalings are powers of 2.
  47.133 +   */
  47.134 +  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  47.135 +       ci++, compptr++) {
  47.136 +    int ssize = cinfo->min_DCT_scaled_size;
  47.137 +    while (ssize < DCTSIZE &&
  47.138 +	   (compptr->h_samp_factor * ssize * 2 <=
  47.139 +	    cinfo->max_h_samp_factor * cinfo->min_DCT_scaled_size) &&
  47.140 +	   (compptr->v_samp_factor * ssize * 2 <=
  47.141 +	    cinfo->max_v_samp_factor * cinfo->min_DCT_scaled_size)) {
  47.142 +      ssize = ssize * 2;
  47.143 +    }
  47.144 +    compptr->DCT_scaled_size = ssize;
  47.145 +  }
  47.146 +
  47.147 +  /* Recompute downsampled dimensions of components;
  47.148 +   * application needs to know these if using raw downsampled data.
  47.149 +   */
  47.150 +  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  47.151 +       ci++, compptr++) {
  47.152 +    /* Size in samples, after IDCT scaling */
  47.153 +    compptr->downsampled_width = (JDIMENSION)
  47.154 +      jdiv_round_up((long) cinfo->image_width *
  47.155 +		    (long) (compptr->h_samp_factor * compptr->DCT_scaled_size),
  47.156 +		    (long) (cinfo->max_h_samp_factor * DCTSIZE));
  47.157 +    compptr->downsampled_height = (JDIMENSION)
  47.158 +      jdiv_round_up((long) cinfo->image_height *
  47.159 +		    (long) (compptr->v_samp_factor * compptr->DCT_scaled_size),
  47.160 +		    (long) (cinfo->max_v_samp_factor * DCTSIZE));
  47.161 +  }
  47.162 +
  47.163 +#else /* !IDCT_SCALING_SUPPORTED */
  47.164 +
  47.165 +  /* Hardwire it to "no scaling" */
  47.166 +  cinfo->output_width = cinfo->image_width;
  47.167 +  cinfo->output_height = cinfo->image_height;
  47.168 +  /* jdinput.c has already initialized DCT_scaled_size to DCTSIZE,
  47.169 +   * and has computed unscaled downsampled_width and downsampled_height.
  47.170 +   */
  47.171 +
  47.172 +#endif /* IDCT_SCALING_SUPPORTED */
  47.173 +
  47.174 +  /* Report number of components in selected colorspace. */
  47.175 +  /* Probably this should be in the color conversion module... */
  47.176 +  switch (cinfo->out_color_space) {
  47.177 +  case JCS_GRAYSCALE:
  47.178 +    cinfo->out_color_components = 1;
  47.179 +    break;
  47.180 +  case JCS_RGB:
  47.181 +#if RGB_PIXELSIZE != 3
  47.182 +    cinfo->out_color_components = RGB_PIXELSIZE;
  47.183 +    break;
  47.184 +#endif /* else share code with YCbCr */
  47.185 +  case JCS_YCbCr:
  47.186 +    cinfo->out_color_components = 3;
  47.187 +    break;
  47.188 +  case JCS_CMYK:
  47.189 +  case JCS_YCCK:
  47.190 +    cinfo->out_color_components = 4;
  47.191 +    break;
  47.192 +  default:			/* else must be same colorspace as in file */
  47.193 +    cinfo->out_color_components = cinfo->num_components;
  47.194 +    break;
  47.195 +  }
  47.196 +  cinfo->output_components = (cinfo->quantize_colors ? 1 :
  47.197 +			      cinfo->out_color_components);
  47.198 +
  47.199 +  /* See if upsampler will want to emit more than one row at a time */
  47.200 +  if (use_merged_upsample(cinfo))
  47.201 +    cinfo->rec_outbuf_height = cinfo->max_v_samp_factor;
  47.202 +  else
  47.203 +    cinfo->rec_outbuf_height = 1;
  47.204 +}
  47.205 +
  47.206 +
  47.207 +/*
  47.208 + * Several decompression processes need to range-limit values to the range
  47.209 + * 0..MAXJSAMPLE; the input value may fall somewhat outside this range
  47.210 + * due to noise introduced by quantization, roundoff error, etc.  These
  47.211 + * processes are inner loops and need to be as fast as possible.  On most
  47.212 + * machines, particularly CPUs with pipelines or instruction prefetch,
  47.213 + * a (subscript-check-less) C table lookup
  47.214 + *		x = sample_range_limit[x];
  47.215 + * is faster than explicit tests
  47.216 + *		if (x < 0)  x = 0;
  47.217 + *		else if (x > MAXJSAMPLE)  x = MAXJSAMPLE;
  47.218 + * These processes all use a common table prepared by the routine below.
  47.219 + *
  47.220 + * For most steps we can mathematically guarantee that the initial value
  47.221 + * of x is within MAXJSAMPLE+1 of the legal range, so a table running from
  47.222 + * -(MAXJSAMPLE+1) to 2*MAXJSAMPLE+1 is sufficient.  But for the initial
  47.223 + * limiting step (just after the IDCT), a wildly out-of-range value is 
  47.224 + * possible if the input data is corrupt.  To avoid any chance of indexing
  47.225 + * off the end of memory and getting a bad-pointer trap, we perform the
  47.226 + * post-IDCT limiting thus:
  47.227 + *		x = range_limit[x & MASK];
  47.228 + * where MASK is 2 bits wider than legal sample data, ie 10 bits for 8-bit
  47.229 + * samples.  Under normal circumstances this is more than enough range and
  47.230 + * a correct output will be generated; with bogus input data the mask will
  47.231 + * cause wraparound, and we will safely generate a bogus-but-in-range output.
  47.232 + * For the post-IDCT step, we want to convert the data from signed to unsigned
  47.233 + * representation by adding CENTERJSAMPLE at the same time that we limit it.
  47.234 + * So the post-IDCT limiting table ends up looking like this:
  47.235 + *   CENTERJSAMPLE,CENTERJSAMPLE+1,...,MAXJSAMPLE,
  47.236 + *   MAXJSAMPLE (repeat 2*(MAXJSAMPLE+1)-CENTERJSAMPLE times),
  47.237 + *   0          (repeat 2*(MAXJSAMPLE+1)-CENTERJSAMPLE times),
  47.238 + *   0,1,...,CENTERJSAMPLE-1
  47.239 + * Negative inputs select values from the upper half of the table after
  47.240 + * masking.
  47.241 + *
  47.242 + * We can save some space by overlapping the start of the post-IDCT table
  47.243 + * with the simpler range limiting table.  The post-IDCT table begins at
  47.244 + * sample_range_limit + CENTERJSAMPLE.
  47.245 + *
  47.246 + * Note that the table is allocated in near data space on PCs; it's small
  47.247 + * enough and used often enough to justify this.
  47.248 + */
  47.249 +
  47.250 +LOCAL(void)
  47.251 +prepare_range_limit_table (j_decompress_ptr cinfo)
  47.252 +/* Allocate and fill in the sample_range_limit table */
  47.253 +{
  47.254 +  JSAMPLE * table;
  47.255 +  int i;
  47.256 +
  47.257 +  table = (JSAMPLE *)
  47.258 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  47.259 +		(5 * (MAXJSAMPLE+1) + CENTERJSAMPLE) * SIZEOF(JSAMPLE));
  47.260 +  table += (MAXJSAMPLE+1);	/* allow negative subscripts of simple table */
  47.261 +  cinfo->sample_range_limit = table;
  47.262 +  /* First segment of "simple" table: limit[x] = 0 for x < 0 */
  47.263 +  MEMZERO(table - (MAXJSAMPLE+1), (MAXJSAMPLE+1) * SIZEOF(JSAMPLE));
  47.264 +  /* Main part of "simple" table: limit[x] = x */
  47.265 +  for (i = 0; i <= MAXJSAMPLE; i++)
  47.266 +    table[i] = (JSAMPLE) i;
  47.267 +  table += CENTERJSAMPLE;	/* Point to where post-IDCT table starts */
  47.268 +  /* End of simple table, rest of first half of post-IDCT table */
  47.269 +  for (i = CENTERJSAMPLE; i < 2*(MAXJSAMPLE+1); i++)
  47.270 +    table[i] = MAXJSAMPLE;
  47.271 +  /* Second half of post-IDCT table */
  47.272 +  MEMZERO(table + (2 * (MAXJSAMPLE+1)),
  47.273 +	  (2 * (MAXJSAMPLE+1) - CENTERJSAMPLE) * SIZEOF(JSAMPLE));
  47.274 +  MEMCOPY(table + (4 * (MAXJSAMPLE+1) - CENTERJSAMPLE),
  47.275 +	  cinfo->sample_range_limit, CENTERJSAMPLE * SIZEOF(JSAMPLE));
  47.276 +}
  47.277 +
  47.278 +
  47.279 +/*
  47.280 + * Master selection of decompression modules.
  47.281 + * This is done once at jpeg_start_decompress time.  We determine
  47.282 + * which modules will be used and give them appropriate initialization calls.
  47.283 + * We also initialize the decompressor input side to begin consuming data.
  47.284 + *
  47.285 + * Since jpeg_read_header has finished, we know what is in the SOF
  47.286 + * and (first) SOS markers.  We also have all the application parameter
  47.287 + * settings.
  47.288 + */
  47.289 +
  47.290 +LOCAL(void)
  47.291 +master_selection (j_decompress_ptr cinfo)
  47.292 +{
  47.293 +  my_master_ptr master = (my_master_ptr) cinfo->master;
  47.294 +  boolean use_c_buffer;
  47.295 +  long samplesperrow;
  47.296 +  JDIMENSION jd_samplesperrow;
  47.297 +
  47.298 +  /* Initialize dimensions and other stuff */
  47.299 +  jpeg_calc_output_dimensions(cinfo);
  47.300 +  prepare_range_limit_table(cinfo);
  47.301 +
  47.302 +  /* Width of an output scanline must be representable as JDIMENSION. */
  47.303 +  samplesperrow = (long) cinfo->output_width * (long) cinfo->out_color_components;
  47.304 +  jd_samplesperrow = (JDIMENSION) samplesperrow;
  47.305 +  if ((long) jd_samplesperrow != samplesperrow)
  47.306 +    ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
  47.307 +
  47.308 +  /* Initialize my private state */
  47.309 +  master->pass_number = 0;
  47.310 +  master->using_merged_upsample = use_merged_upsample(cinfo);
  47.311 +
  47.312 +  /* Color quantizer selection */
  47.313 +  master->quantizer_1pass = NULL;
  47.314 +  master->quantizer_2pass = NULL;
  47.315 +  /* No mode changes if not using buffered-image mode. */
  47.316 +  if (! cinfo->quantize_colors || ! cinfo->buffered_image) {
  47.317 +    cinfo->enable_1pass_quant = FALSE;
  47.318 +    cinfo->enable_external_quant = FALSE;
  47.319 +    cinfo->enable_2pass_quant = FALSE;
  47.320 +  }
  47.321 +  if (cinfo->quantize_colors) {
  47.322 +    if (cinfo->raw_data_out)
  47.323 +      ERREXIT(cinfo, JERR_NOTIMPL);
  47.324 +    /* 2-pass quantizer only works in 3-component color space. */
  47.325 +    if (cinfo->out_color_components != 3) {
  47.326 +      cinfo->enable_1pass_quant = TRUE;
  47.327 +      cinfo->enable_external_quant = FALSE;
  47.328 +      cinfo->enable_2pass_quant = FALSE;
  47.329 +      cinfo->colormap = NULL;
  47.330 +    } else if (cinfo->colormap != NULL) {
  47.331 +      cinfo->enable_external_quant = TRUE;
  47.332 +    } else if (cinfo->two_pass_quantize) {
  47.333 +      cinfo->enable_2pass_quant = TRUE;
  47.334 +    } else {
  47.335 +      cinfo->enable_1pass_quant = TRUE;
  47.336 +    }
  47.337 +
  47.338 +    if (cinfo->enable_1pass_quant) {
  47.339 +#ifdef QUANT_1PASS_SUPPORTED
  47.340 +      jinit_1pass_quantizer(cinfo);
  47.341 +      master->quantizer_1pass = cinfo->cquantize;
  47.342 +#else
  47.343 +      ERREXIT(cinfo, JERR_NOT_COMPILED);
  47.344 +#endif
  47.345 +    }
  47.346 +
  47.347 +    /* We use the 2-pass code to map to external colormaps. */
  47.348 +    if (cinfo->enable_2pass_quant || cinfo->enable_external_quant) {
  47.349 +#ifdef QUANT_2PASS_SUPPORTED
  47.350 +      jinit_2pass_quantizer(cinfo);
  47.351 +      master->quantizer_2pass = cinfo->cquantize;
  47.352 +#else
  47.353 +      ERREXIT(cinfo, JERR_NOT_COMPILED);
  47.354 +#endif
  47.355 +    }
  47.356 +    /* If both quantizers are initialized, the 2-pass one is left active;
  47.357 +     * this is necessary for starting with quantization to an external map.
  47.358 +     */
  47.359 +  }
  47.360 +
  47.361 +  /* Post-processing: in particular, color conversion first */
  47.362 +  if (! cinfo->raw_data_out) {
  47.363 +    if (master->using_merged_upsample) {
  47.364 +#ifdef UPSAMPLE_MERGING_SUPPORTED
  47.365 +      jinit_merged_upsampler(cinfo); /* does color conversion too */
  47.366 +#else
  47.367 +      ERREXIT(cinfo, JERR_NOT_COMPILED);
  47.368 +#endif
  47.369 +    } else {
  47.370 +      jinit_color_deconverter(cinfo);
  47.371 +      jinit_upsampler(cinfo);
  47.372 +    }
  47.373 +    jinit_d_post_controller(cinfo, cinfo->enable_2pass_quant);
  47.374 +  }
  47.375 +  /* Inverse DCT */
  47.376 +  jinit_inverse_dct(cinfo);
  47.377 +  /* Entropy decoding: either Huffman or arithmetic coding. */
  47.378 +  if (cinfo->arith_code) {
  47.379 +    ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
  47.380 +  } else {
  47.381 +    if (cinfo->progressive_mode) {
  47.382 +#ifdef D_PROGRESSIVE_SUPPORTED
  47.383 +      jinit_phuff_decoder(cinfo);
  47.384 +#else
  47.385 +      ERREXIT(cinfo, JERR_NOT_COMPILED);
  47.386 +#endif
  47.387 +    } else
  47.388 +      jinit_huff_decoder(cinfo);
  47.389 +  }
  47.390 +
  47.391 +  /* Initialize principal buffer controllers. */
  47.392 +  use_c_buffer = cinfo->inputctl->has_multiple_scans || cinfo->buffered_image;
  47.393 +  jinit_d_coef_controller(cinfo, use_c_buffer);
  47.394 +
  47.395 +  if (! cinfo->raw_data_out)
  47.396 +    jinit_d_main_controller(cinfo, FALSE /* never need full buffer here */);
  47.397 +
  47.398 +  /* We can now tell the memory manager to allocate virtual arrays. */
  47.399 +  (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);
  47.400 +
  47.401 +  /* Initialize input side of decompressor to consume first scan. */
  47.402 +  (*cinfo->inputctl->start_input_pass) (cinfo);
  47.403 +
  47.404 +#ifdef D_MULTISCAN_FILES_SUPPORTED
  47.405 +  /* If jpeg_start_decompress will read the whole file, initialize
  47.406 +   * progress monitoring appropriately.  The input step is counted
  47.407 +   * as one pass.
  47.408 +   */
  47.409 +  if (cinfo->progress != NULL && ! cinfo->buffered_image &&
  47.410 +      cinfo->inputctl->has_multiple_scans) {
  47.411 +    int nscans;
  47.412 +    /* Estimate number of scans to set pass_limit. */
  47.413 +    if (cinfo->progressive_mode) {
  47.414 +      /* Arbitrarily estimate 2 interleaved DC scans + 3 AC scans/component. */
  47.415 +      nscans = 2 + 3 * cinfo->num_components;
  47.416 +    } else {
  47.417 +      /* For a nonprogressive multiscan file, estimate 1 scan per component. */
  47.418 +      nscans = cinfo->num_components;
  47.419 +    }
  47.420 +    cinfo->progress->pass_counter = 0L;
  47.421 +    cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows * nscans;
  47.422 +    cinfo->progress->completed_passes = 0;
  47.423 +    cinfo->progress->total_passes = (cinfo->enable_2pass_quant ? 3 : 2);
  47.424 +    /* Count the input pass as done */
  47.425 +    master->pass_number++;
  47.426 +  }
  47.427 +#endif /* D_MULTISCAN_FILES_SUPPORTED */
  47.428 +}
  47.429 +
  47.430 +
  47.431 +/*
  47.432 + * Per-pass setup.
  47.433 + * This is called at the beginning of each output pass.  We determine which
  47.434 + * modules will be active during this pass and give them appropriate
  47.435 + * start_pass calls.  We also set is_dummy_pass to indicate whether this
  47.436 + * is a "real" output pass or a dummy pass for color quantization.
  47.437 + * (In the latter case, jdapistd.c will crank the pass to completion.)
  47.438 + */
  47.439 +
  47.440 +METHODDEF(void)
  47.441 +prepare_for_output_pass (j_decompress_ptr cinfo)
  47.442 +{
  47.443 +  my_master_ptr master = (my_master_ptr) cinfo->master;
  47.444 +
  47.445 +  if (master->pub.is_dummy_pass) {
  47.446 +#ifdef QUANT_2PASS_SUPPORTED
  47.447 +    /* Final pass of 2-pass quantization */
  47.448 +    master->pub.is_dummy_pass = FALSE;
  47.449 +    (*cinfo->cquantize->start_pass) (cinfo, FALSE);
  47.450 +    (*cinfo->post->start_pass) (cinfo, JBUF_CRANK_DEST);
  47.451 +    (*cinfo->main->start_pass) (cinfo, JBUF_CRANK_DEST);
  47.452 +#else
  47.453 +    ERREXIT(cinfo, JERR_NOT_COMPILED);
  47.454 +#endif /* QUANT_2PASS_SUPPORTED */
  47.455 +  } else {
  47.456 +    if (cinfo->quantize_colors && cinfo->colormap == NULL) {
  47.457 +      /* Select new quantization method */
  47.458 +      if (cinfo->two_pass_quantize && cinfo->enable_2pass_quant) {
  47.459 +	cinfo->cquantize = master->quantizer_2pass;
  47.460 +	master->pub.is_dummy_pass = TRUE;
  47.461 +      } else if (cinfo->enable_1pass_quant) {
  47.462 +	cinfo->cquantize = master->quantizer_1pass;
  47.463 +      } else {
  47.464 +	ERREXIT(cinfo, JERR_MODE_CHANGE);
  47.465 +      }
  47.466 +    }
  47.467 +    (*cinfo->idct->start_pass) (cinfo);
  47.468 +    (*cinfo->coef->start_output_pass) (cinfo);
  47.469 +    if (! cinfo->raw_data_out) {
  47.470 +      if (! master->using_merged_upsample)
  47.471 +	(*cinfo->cconvert->start_pass) (cinfo);
  47.472 +      (*cinfo->upsample->start_pass) (cinfo);
  47.473 +      if (cinfo->quantize_colors)
  47.474 +	(*cinfo->cquantize->start_pass) (cinfo, master->pub.is_dummy_pass);
  47.475 +      (*cinfo->post->start_pass) (cinfo,
  47.476 +	    (master->pub.is_dummy_pass ? JBUF_SAVE_AND_PASS : JBUF_PASS_THRU));
  47.477 +      (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU);
  47.478 +    }
  47.479 +  }
  47.480 +
  47.481 +  /* Set up progress monitor's pass info if present */
  47.482 +  if (cinfo->progress != NULL) {
  47.483 +    cinfo->progress->completed_passes = master->pass_number;
  47.484 +    cinfo->progress->total_passes = master->pass_number +
  47.485 +				    (master->pub.is_dummy_pass ? 2 : 1);
  47.486 +    /* In buffered-image mode, we assume one more output pass if EOI not
  47.487 +     * yet reached, but no more passes if EOI has been reached.
  47.488 +     */
  47.489 +    if (cinfo->buffered_image && ! cinfo->inputctl->eoi_reached) {
  47.490 +      cinfo->progress->total_passes += (cinfo->enable_2pass_quant ? 2 : 1);
  47.491 +    }
  47.492 +  }
  47.493 +}
  47.494 +
  47.495 +
  47.496 +/*
  47.497 + * Finish up at end of an output pass.
  47.498 + */
  47.499 +
  47.500 +METHODDEF(void)
  47.501 +finish_output_pass (j_decompress_ptr cinfo)
  47.502 +{
  47.503 +  my_master_ptr master = (my_master_ptr) cinfo->master;
  47.504 +
  47.505 +  if (cinfo->quantize_colors)
  47.506 +    (*cinfo->cquantize->finish_pass) (cinfo);
  47.507 +  master->pass_number++;
  47.508 +}
  47.509 +
  47.510 +
  47.511 +#ifdef D_MULTISCAN_FILES_SUPPORTED
  47.512 +
  47.513 +/*
  47.514 + * Switch to a new external colormap between output passes.
  47.515 + */
  47.516 +
  47.517 +GLOBAL(void)
  47.518 +jpeg_new_colormap (j_decompress_ptr cinfo)
  47.519 +{
  47.520 +  my_master_ptr master = (my_master_ptr) cinfo->master;
  47.521 +
  47.522 +  /* Prevent application from calling me at wrong times */
  47.523 +  if (cinfo->global_state != DSTATE_BUFIMAGE)
  47.524 +    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  47.525 +
  47.526 +  if (cinfo->quantize_colors && cinfo->enable_external_quant &&
  47.527 +      cinfo->colormap != NULL) {
  47.528 +    /* Select 2-pass quantizer for external colormap use */
  47.529 +    cinfo->cquantize = master->quantizer_2pass;
  47.530 +    /* Notify quantizer of colormap change */
  47.531 +    (*cinfo->cquantize->new_color_map) (cinfo);
  47.532 +    master->pub.is_dummy_pass = FALSE; /* just in case */
  47.533 +  } else
  47.534 +    ERREXIT(cinfo, JERR_MODE_CHANGE);
  47.535 +}
  47.536 +
  47.537 +#endif /* D_MULTISCAN_FILES_SUPPORTED */
  47.538 +
  47.539 +
  47.540 +/*
  47.541 + * Initialize master decompression control and select active modules.
  47.542 + * This is performed at the start of jpeg_start_decompress.
  47.543 + */
  47.544 +
  47.545 +GLOBAL(void)
  47.546 +jinit_master_decompress (j_decompress_ptr cinfo)
  47.547 +{
  47.548 +  my_master_ptr master;
  47.549 +
  47.550 +  master = (my_master_ptr)
  47.551 +      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  47.552 +				  SIZEOF(my_decomp_master));
  47.553 +  cinfo->master = (struct jpeg_decomp_master *) master;
  47.554 +  master->pub.prepare_for_output_pass = prepare_for_output_pass;
  47.555 +  master->pub.finish_output_pass = finish_output_pass;
  47.556 +
  47.557 +  master->pub.is_dummy_pass = FALSE;
  47.558 +
  47.559 +  master_selection(cinfo);
  47.560 +}
    48.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    48.2 +++ b/libs/libjpeg/jdmerge.c	Sat Sep 19 05:51:51 2015 +0300
    48.3 @@ -0,0 +1,400 @@
    48.4 +/*
    48.5 + * jdmerge.c
    48.6 + *
    48.7 + * Copyright (C) 1994-1996, Thomas G. Lane.
    48.8 + * This file is part of the Independent JPEG Group's software.
    48.9 + * For conditions of distribution and use, see the accompanying README file.
   48.10 + *
   48.11 + * This file contains code for merged upsampling/color conversion.
   48.12 + *
   48.13 + * This file combines functions from jdsample.c and jdcolor.c;
   48.14 + * read those files first to understand what's going on.
   48.15 + *
   48.16 + * When the chroma components are to be upsampled by simple replication
   48.17 + * (ie, box filtering), we can save some work in color conversion by
   48.18 + * calculating all the output pixels corresponding to a pair of chroma
   48.19 + * samples at one time.  In the conversion equations
   48.20 + *	R = Y           + K1 * Cr
   48.21 + *	G = Y + K2 * Cb + K3 * Cr
   48.22 + *	B = Y + K4 * Cb
   48.23 + * only the Y term varies among the group of pixels corresponding to a pair
   48.24 + * of chroma samples, so the rest of the terms can be calculated just once.
   48.25 + * At typical sampling ratios, this eliminates half or three-quarters of the
   48.26 + * multiplications needed for color conversion.
   48.27 + *
   48.28 + * This file currently provides implementations for the following cases:
   48.29 + *	YCbCr => RGB color conversion only.
   48.30 + *	Sampling ratios of 2h1v or 2h2v.
   48.31 + *	No scaling needed at upsample time.
   48.32 + *	Corner-aligned (non-CCIR601) sampling alignment.
   48.33 + * Other special cases could be added, but in most applications these are
   48.34 + * the only common cases.  (For uncommon cases we fall back on the more
   48.35 + * general code in jdsample.c and jdcolor.c.)
   48.36 + */
   48.37 +
   48.38 +#define JPEG_INTERNALS
   48.39 +#include "jinclude.h"
   48.40 +#include "jpeglib.h"
   48.41 +
   48.42 +#ifdef UPSAMPLE_MERGING_SUPPORTED
   48.43 +
   48.44 +
   48.45 +/* Private subobject */
   48.46 +
   48.47 +typedef struct {
   48.48 +  struct jpeg_upsampler pub;	/* public fields */
   48.49 +
   48.50 +  /* Pointer to routine to do actual upsampling/conversion of one row group */
   48.51 +  JMETHOD(void, upmethod, (j_decompress_ptr cinfo,
   48.52 +			   JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr,
   48.53 +			   JSAMPARRAY output_buf));
   48.54 +
   48.55 +  /* Private state for YCC->RGB conversion */
   48.56 +  int * Cr_r_tab;		/* => table for Cr to R conversion */
   48.57 +  int * Cb_b_tab;		/* => table for Cb to B conversion */
   48.58 +  INT32 * Cr_g_tab;		/* => table for Cr to G conversion */
   48.59 +  INT32 * Cb_g_tab;		/* => table for Cb to G conversion */
   48.60 +
   48.61 +  /* For 2:1 vertical sampling, we produce two output rows at a time.
   48.62 +   * We need a "spare" row buffer to hold the second output row if the
   48.63 +   * application provides just a one-row buffer; we also use the spare
   48.64 +   * to discard the dummy last row if the image height is odd.
   48.65 +   */
   48.66 +  JSAMPROW spare_row;
   48.67 +  boolean spare_full;		/* T if spare buffer is occupied */
   48.68 +
   48.69 +  JDIMENSION out_row_width;	/* samples per output row */
   48.70 +  JDIMENSION rows_to_go;	/* counts rows remaining in image */
   48.71 +} my_upsampler;
   48.72 +
   48.73 +typedef my_upsampler * my_upsample_ptr;
   48.74 +
   48.75 +#define SCALEBITS	16	/* speediest right-shift on some machines */
   48.76 +#define ONE_HALF	((INT32) 1 << (SCALEBITS-1))
   48.77 +#define FIX(x)		((INT32) ((x) * (1L<<SCALEBITS) + 0.5))
   48.78 +
   48.79 +
   48.80 +/*
   48.81 + * Initialize tables for YCC->RGB colorspace conversion.
   48.82 + * This is taken directly from jdcolor.c; see that file for more info.
   48.83 + */
   48.84 +
   48.85 +LOCAL(void)
   48.86 +build_ycc_rgb_table (j_decompress_ptr cinfo)
   48.87 +{
   48.88 +  my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
   48.89 +  int i;
   48.90 +  INT32 x;
   48.91 +  SHIFT_TEMPS
   48.92 +
   48.93 +  upsample->Cr_r_tab = (int *)
   48.94 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
   48.95 +				(MAXJSAMPLE+1) * SIZEOF(int));
   48.96 +  upsample->Cb_b_tab = (int *)
   48.97 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
   48.98 +				(MAXJSAMPLE+1) * SIZEOF(int));
   48.99 +  upsample->Cr_g_tab = (INT32 *)
  48.100 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  48.101 +				(MAXJSAMPLE+1) * SIZEOF(INT32));
  48.102 +  upsample->Cb_g_tab = (INT32 *)
  48.103 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  48.104 +				(MAXJSAMPLE+1) * SIZEOF(INT32));
  48.105 +
  48.106 +  for (i = 0, x = -CENTERJSAMPLE; i <= MAXJSAMPLE; i++, x++) {
  48.107 +    /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */
  48.108 +    /* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */
  48.109 +    /* Cr=>R value is nearest int to 1.40200 * x */
  48.110 +    upsample->Cr_r_tab[i] = (int)
  48.111 +		    RIGHT_SHIFT(FIX(1.40200) * x + ONE_HALF, SCALEBITS);
  48.112 +    /* Cb=>B value is nearest int to 1.77200 * x */
  48.113 +    upsample->Cb_b_tab[i] = (int)
  48.114 +		    RIGHT_SHIFT(FIX(1.77200) * x + ONE_HALF, SCALEBITS);
  48.115 +    /* Cr=>G value is scaled-up -0.71414 * x */
  48.116 +    upsample->Cr_g_tab[i] = (- FIX(0.71414)) * x;
  48.117 +    /* Cb=>G value is scaled-up -0.34414 * x */
  48.118 +    /* We also add in ONE_HALF so that need not do it in inner loop */
  48.119 +    upsample->Cb_g_tab[i] = (- FIX(0.34414)) * x + ONE_HALF;
  48.120 +  }
  48.121 +}
  48.122 +
  48.123 +
  48.124 +/*
  48.125 + * Initialize for an upsampling pass.
  48.126 + */
  48.127 +
  48.128 +METHODDEF(void)
  48.129 +start_pass_merged_upsample (j_decompress_ptr cinfo)
  48.130 +{
  48.131 +  my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
  48.132 +
  48.133 +  /* Mark the spare buffer empty */
  48.134 +  upsample->spare_full = FALSE;
  48.135 +  /* Initialize total-height counter for detecting bottom of image */
  48.136 +  upsample->rows_to_go = cinfo->output_height;
  48.137 +}
  48.138 +
  48.139 +
  48.140 +/*
  48.141 + * Control routine to do upsampling (and color conversion).
  48.142 + *
  48.143 + * The control routine just handles the row buffering considerations.
  48.144 + */
  48.145 +
  48.146 +METHODDEF(void)
  48.147 +merged_2v_upsample (j_decompress_ptr cinfo,
  48.148 +		    JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
  48.149 +		    JDIMENSION in_row_groups_avail,
  48.150 +		    JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
  48.151 +		    JDIMENSION out_rows_avail)
  48.152 +/* 2:1 vertical sampling case: may need a spare row. */
  48.153 +{
  48.154 +  my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
  48.155 +  JSAMPROW work_ptrs[2];
  48.156 +  JDIMENSION num_rows;		/* number of rows returned to caller */
  48.157 +
  48.158 +  if (upsample->spare_full) {
  48.159 +    /* If we have a spare row saved from a previous cycle, just return it. */
  48.160 +    jcopy_sample_rows(& upsample->spare_row, 0, output_buf + *out_row_ctr, 0,
  48.161 +		      1, upsample->out_row_width);
  48.162 +    num_rows = 1;
  48.163 +    upsample->spare_full = FALSE;
  48.164 +  } else {
  48.165 +    /* Figure number of rows to return to caller. */
  48.166 +    num_rows = 2;
  48.167 +    /* Not more than the distance to the end of the image. */
  48.168 +    if (num_rows > upsample->rows_to_go)
  48.169 +      num_rows = upsample->rows_to_go;
  48.170 +    /* And not more than what the client can accept: */
  48.171 +    out_rows_avail -= *out_row_ctr;
  48.172 +    if (num_rows > out_rows_avail)
  48.173 +      num_rows = out_rows_avail;
  48.174 +    /* Create output pointer array for upsampler. */
  48.175 +    work_ptrs[0] = output_buf[*out_row_ctr];
  48.176 +    if (num_rows > 1) {
  48.177 +      work_ptrs[1] = output_buf[*out_row_ctr + 1];
  48.178 +    } else {
  48.179 +      work_ptrs[1] = upsample->spare_row;
  48.180 +      upsample->spare_full = TRUE;
  48.181 +    }
  48.182 +    /* Now do the upsampling. */
  48.183 +    (*upsample->upmethod) (cinfo, input_buf, *in_row_group_ctr, work_ptrs);
  48.184 +  }
  48.185 +
  48.186 +  /* Adjust counts */
  48.187 +  *out_row_ctr += num_rows;
  48.188 +  upsample->rows_to_go -= num_rows;
  48.189 +  /* When the buffer is emptied, declare this input row group consumed */
  48.190 +  if (! upsample->spare_full)
  48.191 +    (*in_row_group_ctr)++;
  48.192 +}
  48.193 +
  48.194 +
  48.195 +METHODDEF(void)
  48.196 +merged_1v_upsample (j_decompress_ptr cinfo,
  48.197 +		    JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
  48.198 +		    JDIMENSION in_row_groups_avail,
  48.199 +		    JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
  48.200 +		    JDIMENSION out_rows_avail)
  48.201 +/* 1:1 vertical sampling case: much easier, never need a spare row. */
  48.202 +{
  48.203 +  my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
  48.204 +
  48.205 +  /* Just do the upsampling. */
  48.206 +  (*upsample->upmethod) (cinfo, input_buf, *in_row_group_ctr,
  48.207 +			 output_buf + *out_row_ctr);
  48.208 +  /* Adjust counts */
  48.209 +  (*out_row_ctr)++;
  48.210 +  (*in_row_group_ctr)++;
  48.211 +}
  48.212 +
  48.213 +
  48.214 +/*
  48.215 + * These are the routines invoked by the control routines to do
  48.216 + * the actual upsampling/conversion.  One row group is processed per call.
  48.217 + *
  48.218 + * Note: since we may be writing directly into application-supplied buffers,
  48.219 + * we have to be honest about the output width; we can't assume the buffer
  48.220 + * has been rounded up to an even width.
  48.221 + */
  48.222 +
  48.223 +
  48.224 +/*
  48.225 + * Upsample and color convert for the case of 2:1 horizontal and 1:1 vertical.
  48.226 + */
  48.227 +
  48.228 +METHODDEF(void)
  48.229 +h2v1_merged_upsample (j_decompress_ptr cinfo,
  48.230 +		      JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr,
  48.231 +		      JSAMPARRAY output_buf)
  48.232 +{
  48.233 +  my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
  48.234 +  register int y, cred, cgreen, cblue;
  48.235 +  int cb, cr;
  48.236 +  register JSAMPROW outptr;
  48.237 +  JSAMPROW inptr0, inptr1, inptr2;
  48.238 +  JDIMENSION col;
  48.239 +  /* copy these pointers into registers if possible */
  48.240 +  register JSAMPLE * range_limit = cinfo->sample_range_limit;
  48.241 +  int * Crrtab = upsample->Cr_r_tab;
  48.242 +  int * Cbbtab = upsample->Cb_b_tab;
  48.243 +  INT32 * Crgtab = upsample->Cr_g_tab;
  48.244 +  INT32 * Cbgtab = upsample->Cb_g_tab;
  48.245 +  SHIFT_TEMPS
  48.246 +
  48.247 +  inptr0 = input_buf[0][in_row_group_ctr];
  48.248 +  inptr1 = input_buf[1][in_row_group_ctr];
  48.249 +  inptr2 = input_buf[2][in_row_group_ctr];
  48.250 +  outptr = output_buf[0];
  48.251 +  /* Loop for each pair of output pixels */
  48.252 +  for (col = cinfo->output_width >> 1; col > 0; col--) {
  48.253 +    /* Do the chroma part of the calculation */
  48.254 +    cb = GETJSAMPLE(*inptr1++);
  48.255 +    cr = GETJSAMPLE(*inptr2++);
  48.256 +    cred = Crrtab[cr];
  48.257 +    cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS);
  48.258 +    cblue = Cbbtab[cb];
  48.259 +    /* Fetch 2 Y values and emit 2 pixels */
  48.260 +    y  = GETJSAMPLE(*inptr0++);
  48.261 +    outptr[RGB_RED] =   range_limit[y + cred];
  48.262 +    outptr[RGB_GREEN] = range_limit[y + cgreen];
  48.263 +    outptr[RGB_BLUE] =  range_limit[y + cblue];
  48.264 +    outptr += RGB_PIXELSIZE;
  48.265 +    y  = GETJSAMPLE(*inptr0++);
  48.266 +    outptr[RGB_RED] =   range_limit[y + cred];
  48.267 +    outptr[RGB_GREEN] = range_limit[y + cgreen];
  48.268 +    outptr[RGB_BLUE] =  range_limit[y + cblue];
  48.269 +    outptr += RGB_PIXELSIZE;
  48.270 +  }
  48.271 +  /* If image width is odd, do the last output column separately */
  48.272 +  if (cinfo->output_width & 1) {
  48.273 +    cb = GETJSAMPLE(*inptr1);
  48.274 +    cr = GETJSAMPLE(*inptr2);
  48.275 +    cred = Crrtab[cr];
  48.276 +    cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS);
  48.277 +    cblue = Cbbtab[cb];
  48.278 +    y  = GETJSAMPLE(*inptr0);
  48.279 +    outptr[RGB_RED] =   range_limit[y + cred];
  48.280 +    outptr[RGB_GREEN] = range_limit[y + cgreen];
  48.281 +    outptr[RGB_BLUE] =  range_limit[y + cblue];
  48.282 +  }
  48.283 +}
  48.284 +
  48.285 +
  48.286 +/*
  48.287 + * Upsample and color convert for the case of 2:1 horizontal and 2:1 vertical.
  48.288 + */
  48.289 +
  48.290 +METHODDEF(void)
  48.291 +h2v2_merged_upsample (j_decompress_ptr cinfo,
  48.292 +		      JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr,
  48.293 +		      JSAMPARRAY output_buf)
  48.294 +{
  48.295 +  my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
  48.296 +  register int y, cred, cgreen, cblue;
  48.297 +  int cb, cr;
  48.298 +  register JSAMPROW outptr0, outptr1;
  48.299 +  JSAMPROW inptr00, inptr01, inptr1, inptr2;
  48.300 +  JDIMENSION col;
  48.301 +  /* copy these pointers into registers if possible */
  48.302 +  register JSAMPLE * range_limit = cinfo->sample_range_limit;
  48.303 +  int * Crrtab = upsample->Cr_r_tab;
  48.304 +  int * Cbbtab = upsample->Cb_b_tab;
  48.305 +  INT32 * Crgtab = upsample->Cr_g_tab;
  48.306 +  INT32 * Cbgtab = upsample->Cb_g_tab;
  48.307 +  SHIFT_TEMPS
  48.308 +
  48.309 +  inptr00 = input_buf[0][in_row_group_ctr*2];
  48.310 +  inptr01 = input_buf[0][in_row_group_ctr*2 + 1];
  48.311 +  inptr1 = input_buf[1][in_row_group_ctr];
  48.312 +  inptr2 = input_buf[2][in_row_group_ctr];
  48.313 +  outptr0 = output_buf[0];
  48.314 +  outptr1 = output_buf[1];
  48.315 +  /* Loop for each group of output pixels */
  48.316 +  for (col = cinfo->output_width >> 1; col > 0; col--) {
  48.317 +    /* Do the chroma part of the calculation */
  48.318 +    cb = GETJSAMPLE(*inptr1++);
  48.319 +    cr = GETJSAMPLE(*inptr2++);
  48.320 +    cred = Crrtab[cr];
  48.321 +    cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS);
  48.322 +    cblue = Cbbtab[cb];
  48.323 +    /* Fetch 4 Y values and emit 4 pixels */
  48.324 +    y  = GETJSAMPLE(*inptr00++);
  48.325 +    outptr0[RGB_RED] =   range_limit[y + cred];
  48.326 +    outptr0[RGB_GREEN] = range_limit[y + cgreen];
  48.327 +    outptr0[RGB_BLUE] =  range_limit[y + cblue];
  48.328 +    outptr0 += RGB_PIXELSIZE;
  48.329 +    y  = GETJSAMPLE(*inptr00++);
  48.330 +    outptr0[RGB_RED] =   range_limit[y + cred];
  48.331 +    outptr0[RGB_GREEN] = range_limit[y + cgreen];
  48.332 +    outptr0[RGB_BLUE] =  range_limit[y + cblue];
  48.333 +    outptr0 += RGB_PIXELSIZE;
  48.334 +    y  = GETJSAMPLE(*inptr01++);
  48.335 +    outptr1[RGB_RED] =   range_limit[y + cred];
  48.336 +    outptr1[RGB_GREEN] = range_limit[y + cgreen];
  48.337 +    outptr1[RGB_BLUE] =  range_limit[y + cblue];
  48.338 +    outptr1 += RGB_PIXELSIZE;
  48.339 +    y  = GETJSAMPLE(*inptr01++);
  48.340 +    outptr1[RGB_RED] =   range_limit[y + cred];
  48.341 +    outptr1[RGB_GREEN] = range_limit[y + cgreen];
  48.342 +    outptr1[RGB_BLUE] =  range_limit[y + cblue];
  48.343 +    outptr1 += RGB_PIXELSIZE;
  48.344 +  }
  48.345 +  /* If image width is odd, do the last output column separately */
  48.346 +  if (cinfo->output_width & 1) {
  48.347 +    cb = GETJSAMPLE(*inptr1);
  48.348 +    cr = GETJSAMPLE(*inptr2);
  48.349 +    cred = Crrtab[cr];
  48.350 +    cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS);
  48.351 +    cblue = Cbbtab[cb];
  48.352 +    y  = GETJSAMPLE(*inptr00);
  48.353 +    outptr0[RGB_RED] =   range_limit[y + cred];
  48.354 +    outptr0[RGB_GREEN] = range_limit[y + cgreen];
  48.355 +    outptr0[RGB_BLUE] =  range_limit[y + cblue];
  48.356 +    y  = GETJSAMPLE(*inptr01);
  48.357 +    outptr1[RGB_RED] =   range_limit[y + cred];
  48.358 +    outptr1[RGB_GREEN] = range_limit[y + cgreen];
  48.359 +    outptr1[RGB_BLUE] =  range_limit[y + cblue];
  48.360 +  }
  48.361 +}
  48.362 +
  48.363 +
  48.364 +/*
  48.365 + * Module initialization routine for merged upsampling/color conversion.
  48.366 + *
  48.367 + * NB: this is called under the conditions determined by use_merged_upsample()
  48.368 + * in jdmaster.c.  That routine MUST correspond to the actual capabilities
  48.369 + * of this module; no safety checks are made here.
  48.370 + */
  48.371 +
  48.372 +GLOBAL(void)
  48.373 +jinit_merged_upsampler (j_decompress_ptr cinfo)
  48.374 +{
  48.375 +  my_upsample_ptr upsample;
  48.376 +
  48.377 +  upsample = (my_upsample_ptr)
  48.378 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  48.379 +				SIZEOF(my_upsampler));
  48.380 +  cinfo->upsample = (struct jpeg_upsampler *) upsample;
  48.381 +  upsample->pub.start_pass = start_pass_merged_upsample;
  48.382 +  upsample->pub.need_context_rows = FALSE;
  48.383 +
  48.384 +  upsample->out_row_width = cinfo->output_width * cinfo->out_color_components;
  48.385 +
  48.386 +  if (cinfo->max_v_samp_factor == 2) {
  48.387 +    upsample->pub.upsample = merged_2v_upsample;
  48.388 +    upsample->upmethod = h2v2_merged_upsample;
  48.389 +    /* Allocate a spare row buffer */
  48.390 +    upsample->spare_row = (JSAMPROW)
  48.391 +      (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  48.392 +		(size_t) (upsample->out_row_width * SIZEOF(JSAMPLE)));
  48.393 +  } else {
  48.394 +    upsample->pub.upsample = merged_1v_upsample;
  48.395 +    upsample->upmethod = h2v1_merged_upsample;
  48.396 +    /* No spare row needed */
  48.397 +    upsample->spare_row = NULL;
  48.398 +  }
  48.399 +
  48.400 +  build_ycc_rgb_table(cinfo);
  48.401 +}
  48.402 +
  48.403 +#endif /* UPSAMPLE_MERGING_SUPPORTED */
    49.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    49.2 +++ b/libs/libjpeg/jdphuff.c	Sat Sep 19 05:51:51 2015 +0300
    49.3 @@ -0,0 +1,668 @@
    49.4 +/*
    49.5 + * jdphuff.c
    49.6 + *
    49.7 + * Copyright (C) 1995-1997, Thomas G. Lane.
    49.8 + * This file is part of the Independent JPEG Group's software.
    49.9 + * For conditions of distribution and use, see the accompanying README file.
   49.10 + *
   49.11 + * This file contains Huffman entropy decoding routines for progressive JPEG.
   49.12 + *
   49.13 + * Much of the complexity here has to do with supporting input suspension.
   49.14 + * If the data source module demands suspension, we want to be able to back
   49.15 + * up to the start of the current MCU.  To do this, we copy state variables
   49.16 + * into local working storage, and update them back to the permanent
   49.17 + * storage only upon successful completion of an MCU.
   49.18 + */
   49.19 +
   49.20 +#define JPEG_INTERNALS
   49.21 +#include "jinclude.h"
   49.22 +#include "jpeglib.h"
   49.23 +#include "jdhuff.h"		/* Declarations shared with jdhuff.c */
   49.24 +
   49.25 +
   49.26 +#ifdef D_PROGRESSIVE_SUPPORTED
   49.27 +
   49.28 +/*
   49.29 + * Expanded entropy decoder object for progressive Huffman decoding.
   49.30 + *
   49.31 + * The savable_state subrecord contains fields that change within an MCU,
   49.32 + * but must not be updated permanently until we complete the MCU.
   49.33 + */
   49.34 +
   49.35 +typedef struct {
   49.36 +  unsigned int EOBRUN;			/* remaining EOBs in EOBRUN */
   49.37 +  int last_dc_val[MAX_COMPS_IN_SCAN];	/* last DC coef for each component */
   49.38 +} savable_state;
   49.39 +
   49.40 +/* This macro is to work around compilers with missing or broken
   49.41 + * structure assignment.  You'll need to fix this code if you have
   49.42 + * such a compiler and you change MAX_COMPS_IN_SCAN.
   49.43 + */
   49.44 +
   49.45 +#ifndef NO_STRUCT_ASSIGN
   49.46 +#define ASSIGN_STATE(dest,src)  ((dest) = (src))
   49.47 +#else
   49.48 +#if MAX_COMPS_IN_SCAN == 4
   49.49 +#define ASSIGN_STATE(dest,src)  \
   49.50 +	((dest).EOBRUN = (src).EOBRUN, \
   49.51 +	 (dest).last_dc_val[0] = (src).last_dc_val[0], \
   49.52 +	 (dest).last_dc_val[1] = (src).last_dc_val[1], \
   49.53 +	 (dest).last_dc_val[2] = (src).last_dc_val[2], \
   49.54 +	 (dest).last_dc_val[3] = (src).last_dc_val[3])
   49.55 +#endif
   49.56 +#endif
   49.57 +
   49.58 +
   49.59 +typedef struct {
   49.60 +  struct jpeg_entropy_decoder pub; /* public fields */
   49.61 +
   49.62 +  /* These fields are loaded into local variables at start of each MCU.
   49.63 +   * In case of suspension, we exit WITHOUT updating them.
   49.64 +   */
   49.65 +  bitread_perm_state bitstate;	/* Bit buffer at start of MCU */
   49.66 +  savable_state saved;		/* Other state at start of MCU */
   49.67 +
   49.68 +  /* These fields are NOT loaded into local working state. */
   49.69 +  unsigned int restarts_to_go;	/* MCUs left in this restart interval */
   49.70 +
   49.71 +  /* Pointers to derived tables (these workspaces have image lifespan) */
   49.72 +  d_derived_tbl * derived_tbls[NUM_HUFF_TBLS];
   49.73 +
   49.74 +  d_derived_tbl * ac_derived_tbl; /* active table during an AC scan */
   49.75 +} phuff_entropy_decoder;
   49.76 +
   49.77 +typedef phuff_entropy_decoder * phuff_entropy_ptr;
   49.78 +
   49.79 +/* Forward declarations */
   49.80 +METHODDEF(boolean) decode_mcu_DC_first JPP((j_decompress_ptr cinfo,
   49.81 +					    JBLOCKROW *MCU_data));
   49.82 +METHODDEF(boolean) decode_mcu_AC_first JPP((j_decompress_ptr cinfo,
   49.83 +					    JBLOCKROW *MCU_data));
   49.84 +METHODDEF(boolean) decode_mcu_DC_refine JPP((j_decompress_ptr cinfo,
   49.85 +					     JBLOCKROW *MCU_data));
   49.86 +METHODDEF(boolean) decode_mcu_AC_refine JPP((j_decompress_ptr cinfo,
   49.87 +					     JBLOCKROW *MCU_data));
   49.88 +
   49.89 +
   49.90 +/*
   49.91 + * Initialize for a Huffman-compressed scan.
   49.92 + */
   49.93 +
   49.94 +METHODDEF(void)
   49.95 +start_pass_phuff_decoder (j_decompress_ptr cinfo)
   49.96 +{
   49.97 +  phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
   49.98 +  boolean is_DC_band, bad;
   49.99 +  int ci, coefi, tbl;
  49.100 +  int *coef_bit_ptr;
  49.101 +  jpeg_component_info * compptr;
  49.102 +
  49.103 +  is_DC_band = (cinfo->Ss == 0);
  49.104 +
  49.105 +  /* Validate scan parameters */
  49.106 +  bad = FALSE;
  49.107 +  if (is_DC_band) {
  49.108 +    if (cinfo->Se != 0)
  49.109 +      bad = TRUE;
  49.110 +  } else {
  49.111 +    /* need not check Ss/Se < 0 since they came from unsigned bytes */
  49.112 +    if (cinfo->Ss > cinfo->Se || cinfo->Se >= DCTSIZE2)
  49.113 +      bad = TRUE;
  49.114 +    /* AC scans may have only one component */
  49.115 +    if (cinfo->comps_in_scan != 1)
  49.116 +      bad = TRUE;
  49.117 +  }
  49.118 +  if (cinfo->Ah != 0) {
  49.119 +    /* Successive approximation refinement scan: must have Al = Ah-1. */
  49.120 +    if (cinfo->Al != cinfo->Ah-1)
  49.121 +      bad = TRUE;
  49.122 +  }
  49.123 +  if (cinfo->Al > 13)		/* need not check for < 0 */
  49.124 +    bad = TRUE;
  49.125 +  /* Arguably the maximum Al value should be less than 13 for 8-bit precision,
  49.126 +   * but the spec doesn't say so, and we try to be liberal about what we
  49.127 +   * accept.  Note: large Al values could result in out-of-range DC
  49.128 +   * coefficients during early scans, leading to bizarre displays due to
  49.129 +   * overflows in the IDCT math.  But we won't crash.
  49.130 +   */
  49.131 +  if (bad)
  49.132 +    ERREXIT4(cinfo, JERR_BAD_PROGRESSION,
  49.133 +	     cinfo->Ss, cinfo->Se, cinfo->Ah, cinfo->Al);
  49.134 +  /* Update progression status, and verify that scan order is legal.
  49.135 +   * Note that inter-scan inconsistencies are treated as warnings
  49.136 +   * not fatal errors ... not clear if this is right way to behave.
  49.137 +   */
  49.138 +  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  49.139 +    int cindex = cinfo->cur_comp_info[ci]->component_index;
  49.140 +    coef_bit_ptr = & cinfo->coef_bits[cindex][0];
  49.141 +    if (!is_DC_band && coef_bit_ptr[0] < 0) /* AC without prior DC scan */
  49.142 +      WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, 0);
  49.143 +    for (coefi = cinfo->Ss; coefi <= cinfo->Se; coefi++) {
  49.144 +      int expected = (coef_bit_ptr[coefi] < 0) ? 0 : coef_bit_ptr[coefi];
  49.145 +      if (cinfo->Ah != expected)
  49.146 +	WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, coefi);
  49.147 +      coef_bit_ptr[coefi] = cinfo->Al;
  49.148 +    }
  49.149 +  }
  49.150 +
  49.151 +  /* Select MCU decoding routine */
  49.152 +  if (cinfo->Ah == 0) {
  49.153 +    if (is_DC_band)
  49.154 +      entropy->pub.decode_mcu = decode_mcu_DC_first;
  49.155 +    else
  49.156 +      entropy->pub.decode_mcu = decode_mcu_AC_first;
  49.157 +  } else {
  49.158 +    if (is_DC_band)
  49.159 +      entropy->pub.decode_mcu = decode_mcu_DC_refine;
  49.160 +    else
  49.161 +      entropy->pub.decode_mcu = decode_mcu_AC_refine;
  49.162 +  }
  49.163 +
  49.164 +  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  49.165 +    compptr = cinfo->cur_comp_info[ci];
  49.166 +    /* Make sure requested tables are present, and compute derived tables.
  49.167 +     * We may build same derived table more than once, but it's not expensive.
  49.168 +     */
  49.169 +    if (is_DC_band) {
  49.170 +      if (cinfo->Ah == 0) {	/* DC refinement needs no table */
  49.171 +	tbl = compptr->dc_tbl_no;
  49.172 +	jpeg_make_d_derived_tbl(cinfo, TRUE, tbl,
  49.173 +				& entropy->derived_tbls[tbl]);
  49.174 +      }
  49.175 +    } else {
  49.176 +      tbl = compptr->ac_tbl_no;
  49.177 +      jpeg_make_d_derived_tbl(cinfo, FALSE, tbl,
  49.178 +			      & entropy->derived_tbls[tbl]);
  49.179 +      /* remember the single active table */
  49.180 +      entropy->ac_derived_tbl = entropy->derived_tbls[tbl];
  49.181 +    }
  49.182 +    /* Initialize DC predictions to 0 */
  49.183 +    entropy->saved.last_dc_val[ci] = 0;
  49.184 +  }
  49.185 +
  49.186 +  /* Initialize bitread state variables */
  49.187 +  entropy->bitstate.bits_left = 0;
  49.188 +  entropy->bitstate.get_buffer = 0; /* unnecessary, but keeps Purify quiet */
  49.189 +  entropy->pub.insufficient_data = FALSE;
  49.190 +
  49.191 +  /* Initialize private state variables */
  49.192 +  entropy->saved.EOBRUN = 0;
  49.193 +
  49.194 +  /* Initialize restart counter */
  49.195 +  entropy->restarts_to_go = cinfo->restart_interval;
  49.196 +}
  49.197 +
  49.198 +
  49.199 +/*
  49.200 + * Figure F.12: extend sign bit.
  49.201 + * On some machines, a shift and add will be faster than a table lookup.
  49.202 + */
  49.203 +
  49.204 +#ifdef AVOID_TABLES
  49.205 +
  49.206 +#define HUFF_EXTEND(x,s)  ((x) < (1<<((s)-1)) ? (x) + (((-1)<<(s)) + 1) : (x))
  49.207 +
  49.208 +#else
  49.209 +
  49.210 +#define HUFF_EXTEND(x,s)  ((x) < extend_test[s] ? (x) + extend_offset[s] : (x))
  49.211 +
  49.212 +static const int extend_test[16] =   /* entry n is 2**(n-1) */
  49.213 +  { 0, 0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080,
  49.214 +    0x0100, 0x0200, 0x0400, 0x0800, 0x1000, 0x2000, 0x4000 };
  49.215 +
  49.216 +static const int extend_offset[16] = /* entry n is (-1 << n) + 1 */
  49.217 +  { 0, ((-1)<<1) + 1, ((-1)<<2) + 1, ((-1)<<3) + 1, ((-1)<<4) + 1,
  49.218 +    ((-1)<<5) + 1, ((-1)<<6) + 1, ((-1)<<7) + 1, ((-1)<<8) + 1,
  49.219 +    ((-1)<<9) + 1, ((-1)<<10) + 1, ((-1)<<11) + 1, ((-1)<<12) + 1,
  49.220 +    ((-1)<<13) + 1, ((-1)<<14) + 1, ((-1)<<15) + 1 };
  49.221 +
  49.222 +#endif /* AVOID_TABLES */
  49.223 +
  49.224 +
  49.225 +/*
  49.226 + * Check for a restart marker & resynchronize decoder.
  49.227 + * Returns FALSE if must suspend.
  49.228 + */
  49.229 +
  49.230 +LOCAL(boolean)
  49.231 +process_restart (j_decompress_ptr cinfo)
  49.232 +{
  49.233 +  phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
  49.234 +  int ci;
  49.235 +
  49.236 +  /* Throw away any unused bits remaining in bit buffer; */
  49.237 +  /* include any full bytes in next_marker's count of discarded bytes */
  49.238 +  cinfo->marker->discarded_bytes += entropy->bitstate.bits_left / 8;
  49.239 +  entropy->bitstate.bits_left = 0;
  49.240 +
  49.241 +  /* Advance past the RSTn marker */
  49.242 +  if (! (*cinfo->marker->read_restart_marker) (cinfo))
  49.243 +    return FALSE;
  49.244 +
  49.245 +  /* Re-initialize DC predictions to 0 */
  49.246 +  for (ci = 0; ci < cinfo->comps_in_scan; ci++)
  49.247 +    entropy->saved.last_dc_val[ci] = 0;
  49.248 +  /* Re-init EOB run count, too */
  49.249 +  entropy->saved.EOBRUN = 0;
  49.250 +
  49.251 +  /* Reset restart counter */
  49.252 +  entropy->restarts_to_go = cinfo->restart_interval;
  49.253 +
  49.254 +  /* Reset out-of-data flag, unless read_restart_marker left us smack up
  49.255 +   * against a marker.  In that case we will end up treating the next data
  49.256 +   * segment as empty, and we can avoid producing bogus output pixels by
  49.257 +   * leaving the flag set.
  49.258 +   */
  49.259 +  if (cinfo->unread_marker == 0)
  49.260 +    entropy->pub.insufficient_data = FALSE;
  49.261 +
  49.262 +  return TRUE;
  49.263 +}
  49.264 +
  49.265 +
  49.266 +/*
  49.267 + * Huffman MCU decoding.
  49.268 + * Each of these routines decodes and returns one MCU's worth of
  49.269 + * Huffman-compressed coefficients. 
  49.270 + * The coefficients are reordered from zigzag order into natural array order,
  49.271 + * but are not dequantized.
  49.272 + *
  49.273 + * The i'th block of the MCU is stored into the block pointed to by
  49.274 + * MCU_data[i].  WE ASSUME THIS AREA IS INITIALLY ZEROED BY THE CALLER.
  49.275 + *
  49.276 + * We return FALSE if data source requested suspension.  In that case no
  49.277 + * changes have been made to permanent state.  (Exception: some output
  49.278 + * coefficients may already have been assigned.  This is harmless for
  49.279 + * spectral selection, since we'll just re-assign them on the next call.
  49.280 + * Successive approximation AC refinement has to be more careful, however.)
  49.281 + */
  49.282 +
  49.283 +/*
  49.284 + * MCU decoding for DC initial scan (either spectral selection,
  49.285 + * or first pass of successive approximation).
  49.286 + */
  49.287 +
  49.288 +METHODDEF(boolean)
  49.289 +decode_mcu_DC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
  49.290 +{   
  49.291 +  phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
  49.292 +  int Al = cinfo->Al;
  49.293 +  register int s, r;
  49.294 +  int blkn, ci;
  49.295 +  JBLOCKROW block;
  49.296 +  BITREAD_STATE_VARS;
  49.297 +  savable_state state;
  49.298 +  d_derived_tbl * tbl;
  49.299 +  jpeg_component_info * compptr;
  49.300 +
  49.301 +  /* Process restart marker if needed; may have to suspend */
  49.302 +  if (cinfo->restart_interval) {
  49.303 +    if (entropy->restarts_to_go == 0)
  49.304 +      if (! process_restart(cinfo))
  49.305 +	return FALSE;
  49.306 +  }
  49.307 +
  49.308 +  /* If we've run out of data, just leave the MCU set to zeroes.
  49.309 +   * This way, we return uniform gray for the remainder of the segment.
  49.310 +   */
  49.311 +  if (! entropy->pub.insufficient_data) {
  49.312 +
  49.313 +    /* Load up working state */
  49.314 +    BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
  49.315 +    ASSIGN_STATE(state, entropy->saved);
  49.316 +
  49.317 +    /* Outer loop handles each block in the MCU */
  49.318 +
  49.319 +    for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
  49.320 +      block = MCU_data[blkn];
  49.321 +      ci = cinfo->MCU_membership[blkn];
  49.322 +      compptr = cinfo->cur_comp_info[ci];
  49.323 +      tbl = entropy->derived_tbls[compptr->dc_tbl_no];
  49.324 +
  49.325 +      /* Decode a single block's worth of coefficients */
  49.326 +
  49.327 +      /* Section F.2.2.1: decode the DC coefficient difference */
  49.328 +      HUFF_DECODE(s, br_state, tbl, return FALSE, label1);
  49.329 +      if (s) {
  49.330 +	CHECK_BIT_BUFFER(br_state, s, return FALSE);
  49.331 +	r = GET_BITS(s);
  49.332 +	s = HUFF_EXTEND(r, s);
  49.333 +      }
  49.334 +
  49.335 +      /* Convert DC difference to actual value, update last_dc_val */
  49.336 +      s += state.last_dc_val[ci];
  49.337 +      state.last_dc_val[ci] = s;
  49.338 +      /* Scale and output the coefficient (assumes jpeg_natural_order[0]=0) */
  49.339 +      (*block)[0] = (JCOEF) (s << Al);
  49.340 +    }
  49.341 +
  49.342 +    /* Completed MCU, so update state */
  49.343 +    BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
  49.344 +    ASSIGN_STATE(entropy->saved, state);
  49.345 +  }
  49.346 +
  49.347 +  /* Account for restart interval (no-op if not using restarts) */
  49.348 +  entropy->restarts_to_go--;
  49.349 +
  49.350 +  return TRUE;
  49.351 +}
  49.352 +
  49.353 +
  49.354 +/*
  49.355 + * MCU decoding for AC initial scan (either spectral selection,
  49.356 + * or first pass of successive approximation).
  49.357 + */
  49.358 +
  49.359 +METHODDEF(boolean)
  49.360 +decode_mcu_AC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
  49.361 +{   
  49.362 +  phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
  49.363 +  int Se = cinfo->Se;
  49.364 +  int Al = cinfo->Al;
  49.365 +  register int s, k, r;
  49.366 +  unsigned int EOBRUN;
  49.367 +  JBLOCKROW block;
  49.368 +  BITREAD_STATE_VARS;
  49.369 +  d_derived_tbl * tbl;
  49.370 +
  49.371 +  /* Process restart marker if needed; may have to suspend */
  49.372 +  if (cinfo->restart_interval) {
  49.373 +    if (entropy->restarts_to_go == 0)
  49.374 +      if (! process_restart(cinfo))
  49.375 +	return FALSE;
  49.376 +  }
  49.377 +
  49.378 +  /* If we've run out of data, just leave the MCU set to zeroes.
  49.379 +   * This way, we return uniform gray for the remainder of the segment.
  49.380 +   */
  49.381 +  if (! entropy->pub.insufficient_data) {
  49.382 +
  49.383 +    /* Load up working state.
  49.384 +     * We can avoid loading/saving bitread state if in an EOB run.
  49.385 +     */
  49.386 +    EOBRUN = entropy->saved.EOBRUN;	/* only part of saved state we need */
  49.387 +
  49.388 +    /* There is always only one block per MCU */
  49.389 +
  49.390 +    if (EOBRUN > 0)		/* if it's a band of zeroes... */
  49.391 +      EOBRUN--;			/* ...process it now (we do nothing) */
  49.392 +    else {
  49.393 +      BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
  49.394 +      block = MCU_data[0];
  49.395 +      tbl = entropy->ac_derived_tbl;
  49.396 +
  49.397 +      for (k = cinfo->Ss; k <= Se; k++) {
  49.398 +	HUFF_DECODE(s, br_state, tbl, return FALSE, label2);
  49.399 +	r = s >> 4;
  49.400 +	s &= 15;
  49.401 +	if (s) {
  49.402 +	  k += r;
  49.403 +	  CHECK_BIT_BUFFER(br_state, s, return FALSE);
  49.404 +	  r = GET_BITS(s);
  49.405 +	  s = HUFF_EXTEND(r, s);
  49.406 +	  /* Scale and output coefficient in natural (dezigzagged) order */
  49.407 +	  (*block)[jpeg_natural_order[k]] = (JCOEF) (s << Al);
  49.408 +	} else {
  49.409 +	  if (r == 15) {	/* ZRL */
  49.410 +	    k += 15;		/* skip 15 zeroes in band */
  49.411 +	  } else {		/* EOBr, run length is 2^r + appended bits */
  49.412 +	    EOBRUN = 1 << r;
  49.413 +	    if (r) {		/* EOBr, r > 0 */
  49.414 +	      CHECK_BIT_BUFFER(br_state, r, return FALSE);
  49.415 +	      r = GET_BITS(r);
  49.416 +	      EOBRUN += r;
  49.417 +	    }
  49.418 +	    EOBRUN--;		/* this band is processed at this moment */
  49.419 +	    break;		/* force end-of-band */
  49.420 +	  }
  49.421 +	}
  49.422 +      }
  49.423 +
  49.424 +      BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
  49.425 +    }
  49.426 +
  49.427 +    /* Completed MCU, so update state */
  49.428 +    entropy->saved.EOBRUN = EOBRUN;	/* only part of saved state we need */
  49.429 +  }
  49.430 +
  49.431 +  /* Account for restart interval (no-op if not using restarts) */
  49.432 +  entropy->restarts_to_go--;
  49.433 +
  49.434 +  return TRUE;
  49.435 +}
  49.436 +
  49.437 +
  49.438 +/*
  49.439 + * MCU decoding for DC successive approximation refinement scan.
  49.440 + * Note: we assume such scans can be multi-component, although the spec
  49.441 + * is not very clear on the point.
  49.442 + */
  49.443 +
  49.444 +METHODDEF(boolean)
  49.445 +decode_mcu_DC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
  49.446 +{   
  49.447 +  phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
  49.448 +  int p1 = 1 << cinfo->Al;	/* 1 in the bit position being coded */
  49.449 +  int blkn;
  49.450 +  JBLOCKROW block;
  49.451 +  BITREAD_STATE_VARS;
  49.452 +
  49.453 +  /* Process restart marker if needed; may have to suspend */
  49.454 +  if (cinfo->restart_interval) {
  49.455 +    if (entropy->restarts_to_go == 0)
  49.456 +      if (! process_restart(cinfo))
  49.457 +	return FALSE;
  49.458 +  }
  49.459 +
  49.460 +  /* Not worth the cycles to check insufficient_data here,
  49.461 +   * since we will not change the data anyway if we read zeroes.
  49.462 +   */
  49.463 +
  49.464 +  /* Load up working state */
  49.465 +  BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
  49.466 +
  49.467 +  /* Outer loop handles each block in the MCU */
  49.468 +
  49.469 +  for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
  49.470 +    block = MCU_data[blkn];
  49.471 +
  49.472 +    /* Encoded data is simply the next bit of the two's-complement DC value */
  49.473 +    CHECK_BIT_BUFFER(br_state, 1, return FALSE);
  49.474 +    if (GET_BITS(1))
  49.475 +      (*block)[0] |= p1;
  49.476 +    /* Note: since we use |=, repeating the assignment later is safe */
  49.477 +  }
  49.478 +
  49.479 +  /* Completed MCU, so update state */
  49.480 +  BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
  49.481 +
  49.482 +  /* Account for restart interval (no-op if not using restarts) */
  49.483 +  entropy->restarts_to_go--;
  49.484 +
  49.485 +  return TRUE;
  49.486 +}
  49.487 +
  49.488 +
  49.489 +/*
  49.490 + * MCU decoding for AC successive approximation refinement scan.
  49.491 + */
  49.492 +
  49.493 +METHODDEF(boolean)
  49.494 +decode_mcu_AC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
  49.495 +{   
  49.496 +  phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
  49.497 +  int Se = cinfo->Se;
  49.498 +  int p1 = 1 << cinfo->Al;	/* 1 in the bit position being coded */
  49.499 +  int m1 = (-1) << cinfo->Al;	/* -1 in the bit position being coded */
  49.500 +  register int s, k, r;
  49.501 +  unsigned int EOBRUN;
  49.502 +  JBLOCKROW block;
  49.503 +  JCOEFPTR thiscoef;
  49.504 +  BITREAD_STATE_VARS;
  49.505 +  d_derived_tbl * tbl;
  49.506 +  int num_newnz;
  49.507 +  int newnz_pos[DCTSIZE2];
  49.508 +
  49.509 +  /* Process restart marker if needed; may have to suspend */
  49.510 +  if (cinfo->restart_interval) {
  49.511 +    if (entropy->restarts_to_go == 0)
  49.512 +      if (! process_restart(cinfo))
  49.513 +	return FALSE;
  49.514 +  }
  49.515 +
  49.516 +  /* If we've run out of data, don't modify the MCU.
  49.517 +   */
  49.518 +  if (! entropy->pub.insufficient_data) {
  49.519 +
  49.520 +    /* Load up working state */
  49.521 +    BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
  49.522 +    EOBRUN = entropy->saved.EOBRUN; /* only part of saved state we need */
  49.523 +
  49.524 +    /* There is always only one block per MCU */
  49.525 +    block = MCU_data[0];
  49.526 +    tbl = entropy->ac_derived_tbl;
  49.527 +
  49.528 +    /* If we are forced to suspend, we must undo the assignments to any newly
  49.529 +     * nonzero coefficients in the block, because otherwise we'd get confused
  49.530 +     * next time about which coefficients were already nonzero.
  49.531 +     * But we need not undo addition of bits to already-nonzero coefficients;
  49.532 +     * instead, we can test the current bit to see if we already did it.
  49.533 +     */
  49.534 +    num_newnz = 0;
  49.535 +
  49.536 +    /* initialize coefficient loop counter to start of band */
  49.537 +    k = cinfo->Ss;
  49.538 +
  49.539 +    if (EOBRUN == 0) {
  49.540 +      for (; k <= Se; k++) {
  49.541 +	HUFF_DECODE(s, br_state, tbl, goto undoit, label3);
  49.542 +	r = s >> 4;
  49.543 +	s &= 15;
  49.544 +	if (s) {
  49.545 +	  if (s != 1)		/* size of new coef should always be 1 */
  49.546 +	    WARNMS(cinfo, JWRN_HUFF_BAD_CODE);
  49.547 +	  CHECK_BIT_BUFFER(br_state, 1, goto undoit);
  49.548 +	  if (GET_BITS(1))
  49.549 +	    s = p1;		/* newly nonzero coef is positive */
  49.550 +	  else
  49.551 +	    s = m1;		/* newly nonzero coef is negative */
  49.552 +	} else {
  49.553 +	  if (r != 15) {
  49.554 +	    EOBRUN = 1 << r;	/* EOBr, run length is 2^r + appended bits */
  49.555 +	    if (r) {
  49.556 +	      CHECK_BIT_BUFFER(br_state, r, goto undoit);
  49.557 +	      r = GET_BITS(r);
  49.558 +	      EOBRUN += r;
  49.559 +	    }
  49.560 +	    break;		/* rest of block is handled by EOB logic */
  49.561 +	  }
  49.562 +	  /* note s = 0 for processing ZRL */
  49.563 +	}
  49.564 +	/* Advance over already-nonzero coefs and r still-zero coefs,
  49.565 +	 * appending correction bits to the nonzeroes.  A correction bit is 1
  49.566 +	 * if the absolute value of the coefficient must be increased.
  49.567 +	 */
  49.568 +	do {
  49.569 +	  thiscoef = *block + jpeg_natural_order[k];
  49.570 +	  if (*thiscoef != 0) {
  49.571 +	    CHECK_BIT_BUFFER(br_state, 1, goto undoit);
  49.572 +	    if (GET_BITS(1)) {
  49.573 +	      if ((*thiscoef & p1) == 0) { /* do nothing if already set it */
  49.574 +		if (*thiscoef >= 0)
  49.575 +		  *thiscoef += p1;
  49.576 +		else
  49.577 +		  *thiscoef += m1;
  49.578 +	      }
  49.579 +	    }
  49.580 +	  } else {
  49.581 +	    if (--r < 0)
  49.582 +	      break;		/* reached target zero coefficient */
  49.583 +	  }
  49.584 +	  k++;
  49.585 +	} while (k <= Se);
  49.586 +	if (s) {
  49.587 +	  int pos = jpeg_natural_order[k];
  49.588 +	  /* Output newly nonzero coefficient */
  49.589 +	  (*block)[pos] = (JCOEF) s;
  49.590 +	  /* Remember its position in case we have to suspend */
  49.591 +	  newnz_pos[num_newnz++] = pos;
  49.592 +	}
  49.593 +      }
  49.594 +    }
  49.595 +
  49.596 +    if (EOBRUN > 0) {
  49.597 +      /* Scan any remaining coefficient positions after the end-of-band
  49.598 +       * (the last newly nonzero coefficient, if any).  Append a correction
  49.599 +       * bit to each already-nonzero coefficient.  A correction bit is 1
  49.600 +       * if the absolute value of the coefficient must be increased.
  49.601 +       */
  49.602 +      for (; k <= Se; k++) {
  49.603 +	thiscoef = *block + jpeg_natural_order[k];
  49.604 +	if (*thiscoef != 0) {
  49.605 +	  CHECK_BIT_BUFFER(br_state, 1, goto undoit);
  49.606 +	  if (GET_BITS(1)) {
  49.607 +	    if ((*thiscoef & p1) == 0) { /* do nothing if already changed it */
  49.608 +	      if (*thiscoef >= 0)
  49.609 +		*thiscoef += p1;
  49.610 +	      else
  49.611 +		*thiscoef += m1;
  49.612 +	    }
  49.613 +	  }
  49.614 +	}
  49.615 +      }
  49.616 +      /* Count one block completed in EOB run */
  49.617 +      EOBRUN--;
  49.618 +    }
  49.619 +
  49.620 +    /* Completed MCU, so update state */
  49.621 +    BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
  49.622 +    entropy->saved.EOBRUN = EOBRUN; /* only part of saved state we need */
  49.623 +  }
  49.624 +
  49.625 +  /* Account for restart interval (no-op if not using restarts) */
  49.626 +  entropy->restarts_to_go--;
  49.627 +
  49.628 +  return TRUE;
  49.629 +
  49.630 +undoit:
  49.631 +  /* Re-zero any output coefficients that we made newly nonzero */
  49.632 +  while (num_newnz > 0)
  49.633 +    (*block)[newnz_pos[--num_newnz]] = 0;
  49.634 +
  49.635 +  return FALSE;
  49.636 +}
  49.637 +
  49.638 +
  49.639 +/*
  49.640 + * Module initialization routine for progressive Huffman entropy decoding.
  49.641 + */
  49.642 +
  49.643 +GLOBAL(void)
  49.644 +jinit_phuff_decoder (j_decompress_ptr cinfo)
  49.645 +{
  49.646 +  phuff_entropy_ptr entropy;
  49.647 +  int *coef_bit_ptr;
  49.648 +  int ci, i;
  49.649 +
  49.650 +  entropy = (phuff_entropy_ptr)
  49.651 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  49.652 +				SIZEOF(phuff_entropy_decoder));
  49.653 +  cinfo->entropy = (struct jpeg_entropy_decoder *) entropy;
  49.654 +  entropy->pub.start_pass = start_pass_phuff_decoder;
  49.655 +
  49.656 +  /* Mark derived tables unallocated */
  49.657 +  for (i = 0; i < NUM_HUFF_TBLS; i++) {
  49.658 +    entropy->derived_tbls[i] = NULL;
  49.659 +  }
  49.660 +
  49.661 +  /* Create progression status table */
  49.662 +  cinfo->coef_bits = (int (*)[DCTSIZE2])
  49.663 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  49.664 +				cinfo->num_components*DCTSIZE2*SIZEOF(int));
  49.665 +  coef_bit_ptr = & cinfo->coef_bits[0][0];
  49.666 +  for (ci = 0; ci < cinfo->num_components; ci++) 
  49.667 +    for (i = 0; i < DCTSIZE2; i++)
  49.668 +      *coef_bit_ptr++ = -1;
  49.669 +}
  49.670 +
  49.671 +#endif /* D_PROGRESSIVE_SUPPORTED */
    50.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    50.2 +++ b/libs/libjpeg/jdpostct.c	Sat Sep 19 05:51:51 2015 +0300
    50.3 @@ -0,0 +1,290 @@
    50.4 +/*
    50.5 + * jdpostct.c
    50.6 + *
    50.7 + * Copyright (C) 1994-1996, Thomas G. Lane.
    50.8 + * This file is part of the Independent JPEG Group's software.
    50.9 + * For conditions of distribution and use, see the accompanying README file.
   50.10 + *
   50.11 + * This file contains the decompression postprocessing controller.
   50.12 + * This controller manages the upsampling, color conversion, and color
   50.13 + * quantization/reduction steps; specifically, it controls the buffering
   50.14 + * between upsample/color conversion and color quantization/reduction.
   50.15 + *
   50.16 + * If no color quantization/reduction is required, then this module has no
   50.17 + * work to do, and it just hands off to the upsample/color conversion code.
   50.18 + * An integrated upsample/convert/quantize process would replace this module
   50.19 + * entirely.
   50.20 + */
   50.21 +
   50.22 +#define JPEG_INTERNALS
   50.23 +#include "jinclude.h"
   50.24 +#include "jpeglib.h"
   50.25 +
   50.26 +
   50.27 +/* Private buffer controller object */
   50.28 +
   50.29 +typedef struct {
   50.30 +  struct jpeg_d_post_controller pub; /* public fields */
   50.31 +
   50.32 +  /* Color quantization source buffer: this holds output data from
   50.33 +   * the upsample/color conversion step to be passed to the quantizer.
   50.34 +   * For two-pass color quantization, we need a full-image buffer;
   50.35 +   * for one-pass operation, a strip buffer is sufficient.
   50.36 +   */
   50.37 +  jvirt_sarray_ptr whole_image;	/* virtual array, or NULL if one-pass */
   50.38 +  JSAMPARRAY buffer;		/* strip buffer, or current strip of virtual */
   50.39 +  JDIMENSION strip_height;	/* buffer size in rows */
   50.40 +  /* for two-pass mode only: */
   50.41 +  JDIMENSION starting_row;	/* row # of first row in current strip */
   50.42 +  JDIMENSION next_row;		/* index of next row to fill/empty in strip */
   50.43 +} my_post_controller;
   50.44 +
   50.45 +typedef my_post_controller * my_post_ptr;
   50.46 +
   50.47 +
   50.48 +/* Forward declarations */
   50.49 +METHODDEF(void) post_process_1pass
   50.50 +	JPP((j_decompress_ptr cinfo,
   50.51 +	     JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
   50.52 +	     JDIMENSION in_row_groups_avail,
   50.53 +	     JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
   50.54 +	     JDIMENSION out_rows_avail));
   50.55 +#ifdef QUANT_2PASS_SUPPORTED
   50.56 +METHODDEF(void) post_process_prepass
   50.57 +	JPP((j_decompress_ptr cinfo,
   50.58 +	     JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
   50.59 +	     JDIMENSION in_row_groups_avail,
   50.60 +	     JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
   50.61 +	     JDIMENSION out_rows_avail));
   50.62 +METHODDEF(void) post_process_2pass
   50.63 +	JPP((j_decompress_ptr cinfo,
   50.64 +	     JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
   50.65 +	     JDIMENSION in_row_groups_avail,
   50.66 +	     JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
   50.67 +	     JDIMENSION out_rows_avail));
   50.68 +#endif
   50.69 +
   50.70 +
   50.71 +/*
   50.72 + * Initialize for a processing pass.
   50.73 + */
   50.74 +
   50.75 +METHODDEF(void)
   50.76 +start_pass_dpost (j_decompress_ptr cinfo, J_BUF_MODE pass_mode)
   50.77 +{
   50.78 +  my_post_ptr post = (my_post_ptr) cinfo->post;
   50.79 +
   50.80 +  switch (pass_mode) {
   50.81 +  case JBUF_PASS_THRU:
   50.82 +    if (cinfo->quantize_colors) {
   50.83 +      /* Single-pass processing with color quantization. */
   50.84 +      post->pub.post_process_data = post_process_1pass;
   50.85 +      /* We could be doing buffered-image output before starting a 2-pass
   50.86 +       * color quantization; in that case, jinit_d_post_controller did not
   50.87 +       * allocate a strip buffer.  Use the virtual-array buffer as workspace.
   50.88 +       */
   50.89 +      if (post->buffer == NULL) {
   50.90 +	post->buffer = (*cinfo->mem->access_virt_sarray)
   50.91 +	  ((j_common_ptr) cinfo, post->whole_image,
   50.92 +	   (JDIMENSION) 0, post->strip_height, TRUE);
   50.93 +      }
   50.94 +    } else {
   50.95 +      /* For single-pass processing without color quantization,
   50.96 +       * I have no work to do; just call the upsampler directly.
   50.97 +       */
   50.98 +      post->pub.post_process_data = cinfo->upsample->upsample;
   50.99 +    }
  50.100 +    break;
  50.101 +#ifdef QUANT_2PASS_SUPPORTED
  50.102 +  case JBUF_SAVE_AND_PASS:
  50.103 +    /* First pass of 2-pass quantization */
  50.104 +    if (post->whole_image == NULL)
  50.105 +      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  50.106 +    post->pub.post_process_data = post_process_prepass;
  50.107 +    break;
  50.108 +  case JBUF_CRANK_DEST:
  50.109 +    /* Second pass of 2-pass quantization */
  50.110 +    if (post->whole_image == NULL)
  50.111 +      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  50.112 +    post->pub.post_process_data = post_process_2pass;
  50.113 +    break;
  50.114 +#endif /* QUANT_2PASS_SUPPORTED */
  50.115 +  default:
  50.116 +    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  50.117 +    break;
  50.118 +  }
  50.119 +  post->starting_row = post->next_row = 0;
  50.120 +}
  50.121 +
  50.122 +
  50.123 +/*
  50.124 + * Process some data in the one-pass (strip buffer) case.
  50.125 + * This is used for color precision reduction as well as one-pass quantization.
  50.126 + */
  50.127 +
  50.128 +METHODDEF(void)
  50.129 +post_process_1pass (j_decompress_ptr cinfo,
  50.130 +		    JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
  50.131 +		    JDIMENSION in_row_groups_avail,
  50.132 +		    JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
  50.133 +		    JDIMENSION out_rows_avail)
  50.134 +{
  50.135 +  my_post_ptr post = (my_post_ptr) cinfo->post;
  50.136 +  JDIMENSION num_rows, max_rows;
  50.137 +
  50.138 +  /* Fill the buffer, but not more than what we can dump out in one go. */
  50.139 +  /* Note we rely on the upsampler to detect bottom of image. */
  50.140 +  max_rows = out_rows_avail - *out_row_ctr;
  50.141 +  if (max_rows > post->strip_height)
  50.142 +    max_rows = post->strip_height;
  50.143 +  num_rows = 0;
  50.144 +  (*cinfo->upsample->upsample) (cinfo,
  50.145 +		input_buf, in_row_group_ctr, in_row_groups_avail,
  50.146 +		post->buffer, &num_rows, max_rows);
  50.147 +  /* Quantize and emit data. */
  50.148 +  (*cinfo->cquantize->color_quantize) (cinfo,
  50.149 +		post->buffer, output_buf + *out_row_ctr, (int) num_rows);
  50.150 +  *out_row_ctr += num_rows;
  50.151 +}
  50.152 +
  50.153 +
  50.154 +#ifdef QUANT_2PASS_SUPPORTED
  50.155 +
  50.156 +/*
  50.157 + * Process some data in the first pass of 2-pass quantization.
  50.158 + */
  50.159 +
  50.160 +METHODDEF(void)
  50.161 +post_process_prepass (j_decompress_ptr cinfo,
  50.162 +		      JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
  50.163 +		      JDIMENSION in_row_groups_avail,
  50.164 +		      JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
  50.165 +		      JDIMENSION out_rows_avail)
  50.166 +{
  50.167 +  my_post_ptr post = (my_post_ptr) cinfo->post;
  50.168 +  JDIMENSION old_next_row, num_rows;
  50.169 +
  50.170 +  /* Reposition virtual buffer if at start of strip. */
  50.171 +  if (post->next_row == 0) {
  50.172 +    post->buffer = (*cinfo->mem->access_virt_sarray)
  50.173 +	((j_common_ptr) cinfo, post->whole_image,
  50.174 +	 post->starting_row, post->strip_height, TRUE);
  50.175 +  }
  50.176 +
  50.177 +  /* Upsample some data (up to a strip height's worth). */
  50.178 +  old_next_row = post->next_row;
  50.179 +  (*cinfo->upsample->upsample) (cinfo,
  50.180 +		input_buf, in_row_group_ctr, in_row_groups_avail,
  50.181 +		post->buffer, &post->next_row, post->strip_height);
  50.182 +
  50.183 +  /* Allow quantizer to scan new data.  No data is emitted, */
  50.184 +  /* but we advance out_row_ctr so outer loop can tell when we're done. */
  50.185 +  if (post->next_row > old_next_row) {
  50.186 +    num_rows = post->next_row - old_next_row;
  50.187 +    (*cinfo->cquantize->color_quantize) (cinfo, post->buffer + old_next_row,
  50.188 +					 (JSAMPARRAY) NULL, (int) num_rows);
  50.189 +    *out_row_ctr += num_rows;
  50.190 +  }
  50.191 +
  50.192 +  /* Advance if we filled the strip. */
  50.193 +  if (post->next_row >= post->strip_height) {
  50.194 +    post->starting_row += post->strip_height;
  50.195 +    post->next_row = 0;
  50.196 +  }
  50.197 +}
  50.198 +
  50.199 +
  50.200 +/*
  50.201 + * Process some data in the second pass of 2-pass quantization.
  50.202 + */
  50.203 +
  50.204 +METHODDEF(void)
  50.205 +post_process_2pass (j_decompress_ptr cinfo,
  50.206 +		    JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
  50.207 +		    JDIMENSION in_row_groups_avail,
  50.208 +		    JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
  50.209 +		    JDIMENSION out_rows_avail)
  50.210 +{
  50.211 +  my_post_ptr post = (my_post_ptr) cinfo->post;
  50.212 +  JDIMENSION num_rows, max_rows;
  50.213 +
  50.214 +  /* Reposition virtual buffer if at start of strip. */
  50.215 +  if (post->next_row == 0) {
  50.216 +    post->buffer = (*cinfo->mem->access_virt_sarray)
  50.217 +	((j_common_ptr) cinfo, post->whole_image,
  50.218 +	 post->starting_row, post->strip_height, FALSE);
  50.219 +  }
  50.220 +
  50.221 +  /* Determine number of rows to emit. */
  50.222 +  num_rows = post->strip_height - post->next_row; /* available in strip */
  50.223 +  max_rows = out_rows_avail - *out_row_ctr; /* available in output area */
  50.224 +  if (num_rows > max_rows)
  50.225 +    num_rows = max_rows;
  50.226 +  /* We have to check bottom of image here, can't depend on upsampler. */
  50.227 +  max_rows = cinfo->output_height - post->starting_row;
  50.228 +  if (num_rows > max_rows)
  50.229 +    num_rows = max_rows;
  50.230 +
  50.231 +  /* Quantize and emit data. */
  50.232 +  (*cinfo->cquantize->color_quantize) (cinfo,
  50.233 +		post->buffer + post->next_row, output_buf + *out_row_ctr,
  50.234 +		(int) num_rows);
  50.235 +  *out_row_ctr += num_rows;
  50.236 +
  50.237 +  /* Advance if we filled the strip. */
  50.238 +  post->next_row += num_rows;
  50.239 +  if (post->next_row >= post->strip_height) {
  50.240 +    post->starting_row += post->strip_height;
  50.241 +    post->next_row = 0;
  50.242 +  }
  50.243 +}
  50.244 +
  50.245 +#endif /* QUANT_2PASS_SUPPORTED */
  50.246 +
  50.247 +
  50.248 +/*
  50.249 + * Initialize postprocessing controller.
  50.250 + */
  50.251 +
  50.252 +GLOBAL(void)
  50.253 +jinit_d_post_controller (j_decompress_ptr cinfo, boolean need_full_buffer)
  50.254 +{
  50.255 +  my_post_ptr post;
  50.256 +
  50.257 +  post = (my_post_ptr)
  50.258 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  50.259 +				SIZEOF(my_post_controller));
  50.260 +  cinfo->post = (struct jpeg_d_post_controller *) post;
  50.261 +  post->pub.start_pass = start_pass_dpost;
  50.262 +  post->whole_image = NULL;	/* flag for no virtual arrays */
  50.263 +  post->buffer = NULL;		/* flag for no strip buffer */
  50.264 +
  50.265 +  /* Create the quantization buffer, if needed */
  50.266 +  if (cinfo->quantize_colors) {
  50.267 +    /* The buffer strip height is max_v_samp_factor, which is typically
  50.268 +     * an efficient number of rows for upsampling to return.
  50.269 +     * (In the presence of output rescaling, we might want to be smarter?)
  50.270 +     */
  50.271 +    post->strip_height = (JDIMENSION) cinfo->max_v_samp_factor;
  50.272 +    if (need_full_buffer) {
  50.273 +      /* Two-pass color quantization: need full-image storage. */
  50.274 +      /* We round up the number of rows to a multiple of the strip height. */
  50.275 +#ifdef QUANT_2PASS_SUPPORTED
  50.276 +      post->whole_image = (*cinfo->mem->request_virt_sarray)
  50.277 +	((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE,
  50.278 +	 cinfo->output_width * cinfo->out_color_components,
  50.279 +	 (JDIMENSION) jround_up((long) cinfo->output_height,
  50.280 +				(long) post->strip_height),
  50.281 +	 post->strip_height);
  50.282 +#else
  50.283 +      ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  50.284 +#endif /* QUANT_2PASS_SUPPORTED */
  50.285 +    } else {
  50.286 +      /* One-pass color quantization: just make a strip buffer. */
  50.287 +      post->buffer = (*cinfo->mem->alloc_sarray)
  50.288 +	((j_common_ptr) cinfo, JPOOL_IMAGE,
  50.289 +	 cinfo->output_width * cinfo->out_color_components,
  50.290 +	 post->strip_height);
  50.291 +    }
  50.292 +  }
  50.293 +}
    51.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    51.2 +++ b/libs/libjpeg/jdsample.c	Sat Sep 19 05:51:51 2015 +0300
    51.3 @@ -0,0 +1,478 @@
    51.4 +/*
    51.5 + * jdsample.c
    51.6 + *
    51.7 + * Copyright (C) 1991-1996, Thomas G. Lane.
    51.8 + * This file is part of the Independent JPEG Group's software.
    51.9 + * For conditions of distribution and use, see the accompanying README file.
   51.10 + *
   51.11 + * This file contains upsampling routines.
   51.12 + *
   51.13 + * Upsampling input data is counted in "row groups".  A row group
   51.14 + * is defined to be (v_samp_factor * DCT_scaled_size / min_DCT_scaled_size)
   51.15 + * sample rows of each component.  Upsampling will normally produce
   51.16 + * max_v_samp_factor pixel rows from each row group (but this could vary
   51.17 + * if the upsampler is applying a scale factor of its own).
   51.18 + *
   51.19 + * An excellent reference for image resampling is
   51.20 + *   Digital Image Warping, George Wolberg, 1990.
   51.21 + *   Pub. by IEEE Computer Society Press, Los Alamitos, CA. ISBN 0-8186-8944-7.
   51.22 + */
   51.23 +
   51.24 +#define JPEG_INTERNALS
   51.25 +#include "jinclude.h"
   51.26 +#include "jpeglib.h"
   51.27 +
   51.28 +
   51.29 +/* Pointer to routine to upsample a single component */
   51.30 +typedef JMETHOD(void, upsample1_ptr,
   51.31 +		(j_decompress_ptr cinfo, jpeg_component_info * compptr,
   51.32 +		 JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr));
   51.33 +
   51.34 +/* Private subobject */
   51.35 +
   51.36 +typedef struct {
   51.37 +  struct jpeg_upsampler pub;	/* public fields */
   51.38 +
   51.39 +  /* Color conversion buffer.  When using separate upsampling and color
   51.40 +   * conversion steps, this buffer holds one upsampled row group until it
   51.41 +   * has been color converted and output.
   51.42 +   * Note: we do not allocate any storage for component(s) which are full-size,
   51.43 +   * ie do not need rescaling.  The corresponding entry of color_buf[] is
   51.44 +   * simply set to point to the input data array, thereby avoiding copying.
   51.45 +   */
   51.46 +  JSAMPARRAY color_buf[MAX_COMPONENTS];
   51.47 +
   51.48 +  /* Per-component upsampling method pointers */
   51.49 +  upsample1_ptr methods[MAX_COMPONENTS];
   51.50 +
   51.51 +  int next_row_out;		/* counts rows emitted from color_buf */
   51.52 +  JDIMENSION rows_to_go;	/* counts rows remaining in image */
   51.53 +
   51.54 +  /* Height of an input row group for each component. */
   51.55 +  int rowgroup_height[MAX_COMPONENTS];
   51.56 +
   51.57 +  /* These arrays save pixel expansion factors so that int_expand need not
   51.58 +   * recompute them each time.  They are unused for other upsampling methods.
   51.59 +   */
   51.60 +  UINT8 h_expand[MAX_COMPONENTS];
   51.61 +  UINT8 v_expand[MAX_COMPONENTS];
   51.62 +} my_upsampler;
   51.63 +
   51.64 +typedef my_upsampler * my_upsample_ptr;
   51.65 +
   51.66 +
   51.67 +/*
   51.68 + * Initialize for an upsampling pass.
   51.69 + */
   51.70 +
   51.71 +METHODDEF(void)
   51.72 +start_pass_upsample (j_decompress_ptr cinfo)
   51.73 +{
   51.74 +  my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
   51.75 +
   51.76 +  /* Mark the conversion buffer empty */
   51.77 +  upsample->next_row_out = cinfo->max_v_samp_factor;
   51.78 +  /* Initialize total-height counter for detecting bottom of image */
   51.79 +  upsample->rows_to_go = cinfo->output_height;
   51.80 +}
   51.81 +
   51.82 +
   51.83 +/*
   51.84 + * Control routine to do upsampling (and color conversion).
   51.85 + *
   51.86 + * In this version we upsample each component independently.
   51.87 + * We upsample one row group into the conversion buffer, then apply
   51.88 + * color conversion a row at a time.
   51.89 + */
   51.90 +
   51.91 +METHODDEF(void)
   51.92 +sep_upsample (j_decompress_ptr cinfo,
   51.93 +	      JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
   51.94 +	      JDIMENSION in_row_groups_avail,
   51.95 +	      JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
   51.96 +	      JDIMENSION out_rows_avail)
   51.97 +{
   51.98 +  my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
   51.99 +  int ci;
  51.100 +  jpeg_component_info * compptr;
  51.101 +  JDIMENSION num_rows;
  51.102 +
  51.103 +  /* Fill the conversion buffer, if it's empty */
  51.104 +  if (upsample->next_row_out >= cinfo->max_v_samp_factor) {
  51.105 +    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  51.106 +	 ci++, compptr++) {
  51.107 +      /* Invoke per-component upsample method.  Notice we pass a POINTER
  51.108 +       * to color_buf[ci], so that fullsize_upsample can change it.
  51.109 +       */
  51.110 +      (*upsample->methods[ci]) (cinfo, compptr,
  51.111 +	input_buf[ci] + (*in_row_group_ctr * upsample->rowgroup_height[ci]),
  51.112 +	upsample->color_buf + ci);
  51.113 +    }
  51.114 +    upsample->next_row_out = 0;
  51.115 +  }
  51.116 +
  51.117 +  /* Color-convert and emit rows */
  51.118 +
  51.119 +  /* How many we have in the buffer: */
  51.120 +  num_rows = (JDIMENSION) (cinfo->max_v_samp_factor - upsample->next_row_out);
  51.121 +  /* Not more than the distance to the end of the image.  Need this test
  51.122 +   * in case the image height is not a multiple of max_v_samp_factor:
  51.123 +   */
  51.124 +  if (num_rows > upsample->rows_to_go) 
  51.125 +    num_rows = upsample->rows_to_go;
  51.126 +  /* And not more than what the client can accept: */
  51.127 +  out_rows_avail -= *out_row_ctr;
  51.128 +  if (num_rows > out_rows_avail)
  51.129 +    num_rows = out_rows_avail;
  51.130 +
  51.131 +  (*cinfo->cconvert->color_convert) (cinfo, upsample->color_buf,
  51.132 +				     (JDIMENSION) upsample->next_row_out,
  51.133 +				     output_buf + *out_row_ctr,
  51.134 +				     (int) num_rows);
  51.135 +
  51.136 +  /* Adjust counts */
  51.137 +  *out_row_ctr += num_rows;
  51.138 +  upsample->rows_to_go -= num_rows;
  51.139 +  upsample->next_row_out += num_rows;
  51.140 +  /* When the buffer is emptied, declare this input row group consumed */
  51.141 +  if (upsample->next_row_out >= cinfo->max_v_samp_factor)
  51.142 +    (*in_row_group_ctr)++;
  51.143 +}
  51.144 +
  51.145 +
  51.146 +/*
  51.147 + * These are the routines invoked by sep_upsample to upsample pixel values
  51.148 + * of a single component.  One row group is processed per call.
  51.149 + */
  51.150 +
  51.151 +
  51.152 +/*
  51.153 + * For full-size components, we just make color_buf[ci] point at the
  51.154 + * input buffer, and thus avoid copying any data.  Note that this is
  51.155 + * safe only because sep_upsample doesn't declare the input row group
  51.156 + * "consumed" until we are done color converting and emitting it.
  51.157 + */
  51.158 +
  51.159 +METHODDEF(void)
  51.160 +fullsize_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
  51.161 +		   JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
  51.162 +{
  51.163 +  *output_data_ptr = input_data;
  51.164 +}
  51.165 +
  51.166 +
  51.167 +/*
  51.168 + * This is a no-op version used for "uninteresting" components.
  51.169 + * These components will not be referenced by color conversion.
  51.170 + */
  51.171 +
  51.172 +METHODDEF(void)
  51.173 +noop_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
  51.174 +	       JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
  51.175 +{
  51.176 +  *output_data_ptr = NULL;	/* safety check */
  51.177 +}
  51.178 +
  51.179 +
  51.180 +/*
  51.181 + * This version handles any integral sampling ratios.
  51.182 + * This is not used for typical JPEG files, so it need not be fast.
  51.183 + * Nor, for that matter, is it particularly accurate: the algorithm is
  51.184 + * simple replication of the input pixel onto the corresponding output
  51.185 + * pixels.  The hi-falutin sampling literature refers to this as a
  51.186 + * "box filter".  A box filter tends to introduce visible artifacts,
  51.187 + * so if you are actually going to use 3:1 or 4:1 sampling ratios
  51.188 + * you would be well advised to improve this code.
  51.189 + */
  51.190 +
  51.191 +METHODDEF(void)
  51.192 +int_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
  51.193 +	      JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
  51.194 +{
  51.195 +  my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
  51.196 +  JSAMPARRAY output_data = *output_data_ptr;
  51.197 +  register JSAMPROW inptr, outptr;
  51.198 +  register JSAMPLE invalue;
  51.199 +  register int h;
  51.200 +  JSAMPROW outend;
  51.201 +  int h_expand, v_expand;
  51.202 +  int inrow, outrow;
  51.203 +
  51.204 +  h_expand = upsample->h_expand[compptr->component_index];
  51.205 +  v_expand = upsample->v_expand[compptr->component_index];
  51.206 +
  51.207 +  inrow = outrow = 0;
  51.208 +  while (outrow < cinfo->max_v_samp_factor) {
  51.209 +    /* Generate one output row with proper horizontal expansion */
  51.210 +    inptr = input_data[inrow];
  51.211 +    outptr = output_data[outrow];
  51.212 +    outend = outptr + cinfo->output_width;
  51.213 +    while (outptr < outend) {
  51.214 +      invalue = *inptr++;	/* don't need GETJSAMPLE() here */
  51.215 +      for (h = h_expand; h > 0; h--) {
  51.216 +	*outptr++ = invalue;
  51.217 +      }
  51.218 +    }
  51.219 +    /* Generate any additional output rows by duplicating the first one */
  51.220 +    if (v_expand > 1) {
  51.221 +      jcopy_sample_rows(output_data, outrow, output_data, outrow+1,
  51.222 +			v_expand-1, cinfo->output_width);
  51.223 +    }
  51.224 +    inrow++;
  51.225 +    outrow += v_expand;
  51.226 +  }
  51.227 +}
  51.228 +
  51.229 +
  51.230 +/*
  51.231 + * Fast processing for the common case of 2:1 horizontal and 1:1 vertical.
  51.232 + * It's still a box filter.
  51.233 + */
  51.234 +
  51.235 +METHODDEF(void)
  51.236 +h2v1_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
  51.237 +	       JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
  51.238 +{
  51.239 +  JSAMPARRAY output_data = *output_data_ptr;
  51.240 +  register JSAMPROW inptr, outptr;
  51.241 +  register JSAMPLE invalue;
  51.242 +  JSAMPROW outend;
  51.243 +  int inrow;
  51.244 +
  51.245 +  for (inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++) {
  51.246 +    inptr = input_data[inrow];
  51.247 +    outptr = output_data[inrow];
  51.248 +    outend = outptr + cinfo->output_width;
  51.249 +    while (outptr < outend) {
  51.250 +      invalue = *inptr++;	/* don't need GETJSAMPLE() here */
  51.251 +      *outptr++ = invalue;
  51.252 +      *outptr++ = invalue;
  51.253 +    }
  51.254 +  }
  51.255 +}
  51.256 +
  51.257 +
  51.258 +/*
  51.259 + * Fast processing for the common case of 2:1 horizontal and 2:1 vertical.
  51.260 + * It's still a box filter.
  51.261 + */
  51.262 +
  51.263 +METHODDEF(void)
  51.264 +h2v2_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
  51.265 +	       JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
  51.266 +{
  51.267 +  JSAMPARRAY output_data = *output_data_ptr;
  51.268 +  register JSAMPROW inptr, outptr;
  51.269 +  register JSAMPLE invalue;
  51.270 +  JSAMPROW outend;
  51.271 +  int inrow, outrow;
  51.272 +
  51.273 +  inrow = outrow = 0;
  51.274 +  while (outrow < cinfo->max_v_samp_factor) {
  51.275 +    inptr = input_data[inrow];
  51.276 +    outptr = output_data[outrow];
  51.277 +    outend = outptr + cinfo->output_width;
  51.278 +    while (outptr < outend) {
  51.279 +      invalue = *inptr++;	/* don't need GETJSAMPLE() here */
  51.280 +      *outptr++ = invalue;
  51.281 +      *outptr++ = invalue;
  51.282 +    }
  51.283 +    jcopy_sample_rows(output_data, outrow, output_data, outrow+1,
  51.284 +		      1, cinfo->output_width);
  51.285 +    inrow++;
  51.286 +    outrow += 2;
  51.287 +  }
  51.288 +}
  51.289 +
  51.290 +
  51.291 +/*
  51.292 + * Fancy processing for the common case of 2:1 horizontal and 1:1 vertical.
  51.293 + *
  51.294 + * The upsampling algorithm is linear interpolation between pixel centers,
  51.295 + * also known as a "triangle filter".  This is a good compromise between
  51.296 + * speed and visual quality.  The centers of the output pixels are 1/4 and 3/4
  51.297 + * of the way between input pixel centers.
  51.298 + *
  51.299 + * A note about the "bias" calculations: when rounding fractional values to
  51.300 + * integer, we do not want to always round 0.5 up to the next integer.
  51.301 + * If we did that, we'd introduce a noticeable bias towards larger values.
  51.302 + * Instead, this code is arranged so that 0.5 will be rounded up or down at
  51.303 + * alternate pixel locations (a simple ordered dither pattern).
  51.304 + */
  51.305 +
  51.306 +METHODDEF(void)
  51.307 +h2v1_fancy_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
  51.308 +		     JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
  51.309 +{
  51.310 +  JSAMPARRAY output_data = *output_data_ptr;
  51.311 +  register JSAMPROW inptr, outptr;
  51.312 +  register int invalue;
  51.313 +  register JDIMENSION colctr;
  51.314 +  int inrow;
  51.315 +
  51.316 +  for (inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++) {
  51.317 +    inptr = input_data[inrow];
  51.318 +    outptr = output_data[inrow];
  51.319 +    /* Special case for first column */
  51.320 +    invalue = GETJSAMPLE(*inptr++);
  51.321 +    *outptr++ = (JSAMPLE) invalue;
  51.322 +    *outptr++ = (JSAMPLE) ((invalue * 3 + GETJSAMPLE(*inptr) + 2) >> 2);
  51.323 +
  51.324 +    for (colctr = compptr->downsampled_width - 2; colctr > 0; colctr--) {
  51.325 +      /* General case: 3/4 * nearer pixel + 1/4 * further pixel */
  51.326 +      invalue = GETJSAMPLE(*inptr++) * 3;
  51.327 +      *outptr++ = (JSAMPLE) ((invalue + GETJSAMPLE(inptr[-2]) + 1) >> 2);
  51.328 +      *outptr++ = (JSAMPLE) ((invalue + GETJSAMPLE(*inptr) + 2) >> 2);
  51.329 +    }
  51.330 +
  51.331 +    /* Special case for last column */
  51.332 +    invalue = GETJSAMPLE(*inptr);
  51.333 +    *outptr++ = (JSAMPLE) ((invalue * 3 + GETJSAMPLE(inptr[-1]) + 1) >> 2);
  51.334 +    *outptr++ = (JSAMPLE) invalue;
  51.335 +  }
  51.336 +}
  51.337 +
  51.338 +
  51.339 +/*
  51.340 + * Fancy processing for the common case of 2:1 horizontal and 2:1 vertical.
  51.341 + * Again a triangle filter; see comments for h2v1 case, above.
  51.342 + *
  51.343 + * It is OK for us to reference the adjacent input rows because we demanded
  51.344 + * context from the main buffer controller (see initialization code).
  51.345 + */
  51.346 +
  51.347 +METHODDEF(void)
  51.348 +h2v2_fancy_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
  51.349 +		     JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
  51.350 +{
  51.351 +  JSAMPARRAY output_data = *output_data_ptr;
  51.352 +  register JSAMPROW inptr0, inptr1, outptr;
  51.353 +#if BITS_IN_JSAMPLE == 8
  51.354 +  register int thiscolsum, lastcolsum, nextcolsum;
  51.355 +#else
  51.356 +  register INT32 thiscolsum, lastcolsum, nextcolsum;
  51.357 +#endif
  51.358 +  register JDIMENSION colctr;
  51.359 +  int inrow, outrow, v;
  51.360 +
  51.361 +  inrow = outrow = 0;
  51.362 +  while (outrow < cinfo->max_v_samp_factor) {
  51.363 +    for (v = 0; v < 2; v++) {
  51.364 +      /* inptr0 points to nearest input row, inptr1 points to next nearest */
  51.365 +      inptr0 = input_data[inrow];
  51.366 +      if (v == 0)		/* next nearest is row above */
  51.367 +	inptr1 = input_data[inrow-1];
  51.368 +      else			/* next nearest is row below */
  51.369 +	inptr1 = input_data[inrow+1];
  51.370 +      outptr = output_data[outrow++];
  51.371 +
  51.372 +      /* Special case for first column */
  51.373 +      thiscolsum = GETJSAMPLE(*inptr0++) * 3 + GETJSAMPLE(*inptr1++);
  51.374 +      nextcolsum = GETJSAMPLE(*inptr0++) * 3 + GETJSAMPLE(*inptr1++);
  51.375 +      *outptr++ = (JSAMPLE) ((thiscolsum * 4 + 8) >> 4);
  51.376 +      *outptr++ = (JSAMPLE) ((thiscolsum * 3 + nextcolsum + 7) >> 4);
  51.377 +      lastcolsum = thiscolsum; thiscolsum = nextcolsum;
  51.378 +
  51.379 +      for (colctr = compptr->downsampled_width - 2; colctr > 0; colctr--) {
  51.380 +	/* General case: 3/4 * nearer pixel + 1/4 * further pixel in each */
  51.381 +	/* dimension, thus 9/16, 3/16, 3/16, 1/16 overall */
  51.382 +	nextcolsum = GETJSAMPLE(*inptr0++) * 3 + GETJSAMPLE(*inptr1++);
  51.383 +	*outptr++ = (JSAMPLE) ((thiscolsum * 3 + lastcolsum + 8) >> 4);
  51.384 +	*outptr++ = (JSAMPLE) ((thiscolsum * 3 + nextcolsum + 7) >> 4);
  51.385 +	lastcolsum = thiscolsum; thiscolsum = nextcolsum;
  51.386 +      }
  51.387 +
  51.388 +      /* Special case for last column */
  51.389 +      *outptr++ = (JSAMPLE) ((thiscolsum * 3 + lastcolsum + 8) >> 4);
  51.390 +      *outptr++ = (JSAMPLE) ((thiscolsum * 4 + 7) >> 4);
  51.391 +    }
  51.392 +    inrow++;
  51.393 +  }
  51.394 +}
  51.395 +
  51.396 +
  51.397 +/*
  51.398 + * Module initialization routine for upsampling.
  51.399 + */
  51.400 +
  51.401 +GLOBAL(void)
  51.402 +jinit_upsampler (j_decompress_ptr cinfo)
  51.403 +{
  51.404 +  my_upsample_ptr upsample;
  51.405 +  int ci;
  51.406 +  jpeg_component_info * compptr;
  51.407 +  boolean need_buffer, do_fancy;
  51.408 +  int h_in_group, v_in_group, h_out_group, v_out_group;
  51.409 +
  51.410 +  upsample = (my_upsample_ptr)
  51.411 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  51.412 +				SIZEOF(my_upsampler));
  51.413 +  cinfo->upsample = (struct jpeg_upsampler *) upsample;
  51.414 +  upsample->pub.start_pass = start_pass_upsample;
  51.415 +  upsample->pub.upsample = sep_upsample;
  51.416 +  upsample->pub.need_context_rows = FALSE; /* until we find out differently */
  51.417 +
  51.418 +  if (cinfo->CCIR601_sampling)	/* this isn't supported */
  51.419 +    ERREXIT(cinfo, JERR_CCIR601_NOTIMPL);
  51.420 +
  51.421 +  /* jdmainct.c doesn't support context rows when min_DCT_scaled_size = 1,
  51.422 +   * so don't ask for it.
  51.423 +   */
  51.424 +  do_fancy = cinfo->do_fancy_upsampling && cinfo->min_DCT_scaled_size > 1;
  51.425 +
  51.426 +  /* Verify we can handle the sampling factors, select per-component methods,
  51.427 +   * and create storage as needed.
  51.428 +   */
  51.429 +  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  51.430 +       ci++, compptr++) {
  51.431 +    /* Compute size of an "input group" after IDCT scaling.  This many samples
  51.432 +     * are to be converted to max_h_samp_factor * max_v_samp_factor pixels.
  51.433 +     */
  51.434 +    h_in_group = (compptr->h_samp_factor * compptr->DCT_scaled_size) /
  51.435 +		 cinfo->min_DCT_scaled_size;
  51.436 +    v_in_group = (compptr->v_samp_factor * compptr->DCT_scaled_size) /
  51.437 +		 cinfo->min_DCT_scaled_size;
  51.438 +    h_out_group = cinfo->max_h_samp_factor;
  51.439 +    v_out_group = cinfo->max_v_samp_factor;
  51.440 +    upsample->rowgroup_height[ci] = v_in_group; /* save for use later */
  51.441 +    need_buffer = TRUE;
  51.442 +    if (! compptr->component_needed) {
  51.443 +      /* Don't bother to upsample an uninteresting component. */
  51.444 +      upsample->methods[ci] = noop_upsample;
  51.445 +      need_buffer = FALSE;
  51.446 +    } else if (h_in_group == h_out_group && v_in_group == v_out_group) {
  51.447 +      /* Fullsize components can be processed without any work. */
  51.448 +      upsample->methods[ci] = fullsize_upsample;
  51.449 +      need_buffer = FALSE;
  51.450 +    } else if (h_in_group * 2 == h_out_group &&
  51.451 +	       v_in_group == v_out_group) {
  51.452 +      /* Special cases for 2h1v upsampling */
  51.453 +      if (do_fancy && compptr->downsampled_width > 2)
  51.454 +	upsample->methods[ci] = h2v1_fancy_upsample;
  51.455 +      else
  51.456 +	upsample->methods[ci] = h2v1_upsample;
  51.457 +    } else if (h_in_group * 2 == h_out_group &&
  51.458 +	       v_in_group * 2 == v_out_group) {
  51.459 +      /* Special cases for 2h2v upsampling */
  51.460 +      if (do_fancy && compptr->downsampled_width > 2) {
  51.461 +	upsample->methods[ci] = h2v2_fancy_upsample;
  51.462 +	upsample->pub.need_context_rows = TRUE;
  51.463 +      } else
  51.464 +	upsample->methods[ci] = h2v2_upsample;
  51.465 +    } else if ((h_out_group % h_in_group) == 0 &&
  51.466 +	       (v_out_group % v_in_group) == 0) {
  51.467 +      /* Generic integral-factors upsampling method */
  51.468 +      upsample->methods[ci] = int_upsample;
  51.469 +      upsample->h_expand[ci] = (UINT8) (h_out_group / h_in_group);
  51.470 +      upsample->v_expand[ci] = (UINT8) (v_out_group / v_in_group);
  51.471 +    } else
  51.472 +      ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);
  51.473 +    if (need_buffer) {
  51.474 +      upsample->color_buf[ci] = (*cinfo->mem->alloc_sarray)
  51.475 +	((j_common_ptr) cinfo, JPOOL_IMAGE,
  51.476 +	 (JDIMENSION) jround_up((long) cinfo->output_width,
  51.477 +				(long) cinfo->max_h_samp_factor),
  51.478 +	 (JDIMENSION) cinfo->max_v_samp_factor);
  51.479 +    }
  51.480 +  }
  51.481 +}
    52.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    52.2 +++ b/libs/libjpeg/jdtrans.c	Sat Sep 19 05:51:51 2015 +0300
    52.3 @@ -0,0 +1,143 @@
    52.4 +/*
    52.5 + * jdtrans.c
    52.6 + *
    52.7 + * Copyright (C) 1995-1997, Thomas G. Lane.
    52.8 + * This file is part of the Independent JPEG Group's software.
    52.9 + * For conditions of distribution and use, see the accompanying README file.
   52.10 + *
   52.11 + * This file contains library routines for transcoding decompression,
   52.12 + * that is, reading raw DCT coefficient arrays from an input JPEG file.
   52.13 + * The routines in jdapimin.c will also be needed by a transcoder.
   52.14 + */
   52.15 +
   52.16 +#define JPEG_INTERNALS
   52.17 +#include "jinclude.h"
   52.18 +#include "jpeglib.h"
   52.19 +
   52.20 +
   52.21 +/* Forward declarations */
   52.22 +LOCAL(void) transdecode_master_selection JPP((j_decompress_ptr cinfo));
   52.23 +
   52.24 +
   52.25 +/*
   52.26 + * Read the coefficient arrays from a JPEG file.
   52.27 + * jpeg_read_header must be completed before calling this.
   52.28 + *
   52.29 + * The entire image is read into a set of virtual coefficient-block arrays,
   52.30 + * one per component.  The return value is a pointer to the array of
   52.31 + * virtual-array descriptors.  These can be manipulated directly via the
   52.32 + * JPEG memory manager, or handed off to jpeg_write_coefficients().
   52.33 + * To release the memory occupied by the virtual arrays, call
   52.34 + * jpeg_finish_decompress() when done with the data.
   52.35 + *
   52.36 + * An alternative usage is to simply obtain access to the coefficient arrays
   52.37 + * during a buffered-image-mode decompression operation.  This is allowed
   52.38 + * after any jpeg_finish_output() call.  The arrays can be accessed until
   52.39 + * jpeg_finish_decompress() is called.  (Note that any call to the library
   52.40 + * may reposition the arrays, so don't rely on access_virt_barray() results
   52.41 + * to stay valid across library calls.)
   52.42 + *
   52.43 + * Returns NULL if suspended.  This case need be checked only if
   52.44 + * a suspending data source is used.
   52.45 + */
   52.46 +
   52.47 +GLOBAL(jvirt_barray_ptr *)
   52.48 +jpeg_read_coefficients (j_decompress_ptr cinfo)
   52.49 +{
   52.50 +  if (cinfo->global_state == DSTATE_READY) {
   52.51 +    /* First call: initialize active modules */
   52.52 +    transdecode_master_selection(cinfo);
   52.53 +    cinfo->global_state = DSTATE_RDCOEFS;
   52.54 +  }
   52.55 +  if (cinfo->global_state == DSTATE_RDCOEFS) {
   52.56 +    /* Absorb whole file into the coef buffer */
   52.57 +    for (;;) {
   52.58 +      int retcode;
   52.59 +      /* Call progress monitor hook if present */
   52.60 +      if (cinfo->progress != NULL)
   52.61 +	(*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
   52.62 +      /* Absorb some more input */
   52.63 +      retcode = (*cinfo->inputctl->consume_input) (cinfo);
   52.64 +      if (retcode == JPEG_SUSPENDED)
   52.65 +	return NULL;
   52.66 +      if (retcode == JPEG_REACHED_EOI)
   52.67 +	break;
   52.68 +      /* Advance progress counter if appropriate */
   52.69 +      if (cinfo->progress != NULL &&
   52.70 +	  (retcode == JPEG_ROW_COMPLETED || retcode == JPEG_REACHED_SOS)) {
   52.71 +	if (++cinfo->progress->pass_counter >= cinfo->progress->pass_limit) {
   52.72 +	  /* startup underestimated number of scans; ratchet up one scan */
   52.73 +	  cinfo->progress->pass_limit += (long) cinfo->total_iMCU_rows;
   52.74 +	}
   52.75 +      }
   52.76 +    }
   52.77 +    /* Set state so that jpeg_finish_decompress does the right thing */
   52.78 +    cinfo->global_state = DSTATE_STOPPING;
   52.79 +  }
   52.80 +  /* At this point we should be in state DSTATE_STOPPING if being used
   52.81 +   * standalone, or in state DSTATE_BUFIMAGE if being invoked to get access
   52.82 +   * to the coefficients during a full buffered-image-mode decompression.
   52.83 +   */
   52.84 +  if ((cinfo->global_state == DSTATE_STOPPING ||
   52.85 +       cinfo->global_state == DSTATE_BUFIMAGE) && cinfo->buffered_image) {
   52.86 +    return cinfo->coef->coef_arrays;
   52.87 +  }
   52.88 +  /* Oops, improper usage */
   52.89 +  ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
   52.90 +  return NULL;			/* keep compiler happy */
   52.91 +}
   52.92 +
   52.93 +
   52.94 +/*
   52.95 + * Master selection of decompression modules for transcoding.
   52.96 + * This substitutes for jdmaster.c's initialization of the full decompressor.
   52.97 + */
   52.98 +
   52.99 +LOCAL(void)
  52.100 +transdecode_master_selection (j_decompress_ptr cinfo)
  52.101 +{
  52.102 +  /* This is effectively a buffered-image operation. */
  52.103 +  cinfo->buffered_image = TRUE;
  52.104 +
  52.105 +  /* Entropy decoding: either Huffman or arithmetic coding. */
  52.106 +  if (cinfo->arith_code) {
  52.107 +    ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
  52.108 +  } else {
  52.109 +    if (cinfo->progressive_mode) {
  52.110 +#ifdef D_PROGRESSIVE_SUPPORTED
  52.111 +      jinit_phuff_decoder(cinfo);
  52.112 +#else
  52.113 +      ERREXIT(cinfo, JERR_NOT_COMPILED);
  52.114 +#endif
  52.115 +    } else
  52.116 +      jinit_huff_decoder(cinfo);
  52.117 +  }
  52.118 +
  52.119 +  /* Always get a full-image coefficient buffer. */
  52.120 +  jinit_d_coef_controller(cinfo, TRUE);
  52.121 +
  52.122 +  /* We can now tell the memory manager to allocate virtual arrays. */
  52.123 +  (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);
  52.124 +
  52.125 +  /* Initialize input side of decompressor to consume first scan. */
  52.126 +  (*cinfo->inputctl->start_input_pass) (cinfo);
  52.127 +
  52.128 +  /* Initialize progress monitoring. */
  52.129 +  if (cinfo->progress != NULL) {
  52.130 +    int nscans;
  52.131 +    /* Estimate number of scans to set pass_limit. */
  52.132 +    if (cinfo->progressive_mode) {
  52.133 +      /* Arbitrarily estimate 2 interleaved DC scans + 3 AC scans/component. */
  52.134 +      nscans = 2 + 3 * cinfo->num_components;
  52.135 +    } else if (cinfo->inputctl->has_multiple_scans) {
  52.136 +      /* For a nonprogressive multiscan file, estimate 1 scan per component. */
  52.137 +      nscans = cinfo->num_components;
  52.138 +    } else {
  52.139 +      nscans = 1;
  52.140 +    }
  52.141 +    cinfo->progress->pass_counter = 0L;
  52.142 +    cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows * nscans;
  52.143 +    cinfo->progress->completed_passes = 0;
  52.144 +    cinfo->progress->total_passes = 1;
  52.145 +  }
  52.146 +}
    53.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    53.2 +++ b/libs/libjpeg/jerror.c	Sat Sep 19 05:51:51 2015 +0300
    53.3 @@ -0,0 +1,252 @@
    53.4 +/*
    53.5 + * jerror.c
    53.6 + *
    53.7 + * Copyright (C) 1991-1998, Thomas G. Lane.
    53.8 + * This file is part of the Independent JPEG Group's software.
    53.9 + * For conditions of distribution and use, see the accompanying README file.
   53.10 + *
   53.11 + * This file contains simple error-reporting and trace-message routines.
   53.12 + * These are suitable for Unix-like systems and others where writing to
   53.13 + * stderr is the right thing to do.  Many applications will want to replace
   53.14 + * some or all of these routines.
   53.15 + *
   53.16 + * If you define USE_WINDOWS_MESSAGEBOX in jconfig.h or in the makefile,
   53.17 + * you get a Windows-specific hack to display error messages in a dialog box.
   53.18 + * It ain't much, but it beats dropping error messages into the bit bucket,
   53.19 + * which is what happens to output to stderr under most Windows C compilers.
   53.20 + *
   53.21 + * These routines are used by both the compression and decompression code.
   53.22 + */
   53.23 +
   53.24 +/* this is not a core library module, so it doesn't define JPEG_INTERNALS */
   53.25 +#include "jinclude.h"
   53.26 +#include "jpeglib.h"
   53.27 +#include "jversion.h"
   53.28 +#include "jerror.h"
   53.29 +
   53.30 +#ifdef USE_WINDOWS_MESSAGEBOX
   53.31 +#include <windows.h>
   53.32 +#endif
   53.33 +
   53.34 +#ifndef EXIT_FAILURE		/* define exit() codes if not provided */
   53.35 +#define EXIT_FAILURE  1
   53.36 +#endif
   53.37 +
   53.38 +
   53.39 +/*
   53.40 + * Create the message string table.
   53.41 + * We do this from the master message list in jerror.h by re-reading
   53.42 + * jerror.h with a suitable definition for macro JMESSAGE.
   53.43 + * The message table is made an external symbol just in case any applications
   53.44 + * want to refer to it directly.
   53.45 + */
   53.46 +
   53.47 +#ifdef NEED_SHORT_EXTERNAL_NAMES
   53.48 +#define jpeg_std_message_table	jMsgTable
   53.49 +#endif
   53.50 +
   53.51 +#define JMESSAGE(code,string)	string ,
   53.52 +
   53.53 +const char * const jpeg_std_message_table[] = {
   53.54 +#include "jerror.h"
   53.55 +  NULL
   53.56 +};
   53.57 +
   53.58 +
   53.59 +/*
   53.60 + * Error exit handler: must not return to caller.
   53.61 + *
   53.62 + * Applications may override this if they want to get control back after
   53.63 + * an error.  Typically one would longjmp somewhere instead of exiting.
   53.64 + * The setjmp buffer can be made a private field within an expanded error
   53.65 + * handler object.  Note that the info needed to generate an error message
   53.66 + * is stored in the error object, so you can generate the message now or
   53.67 + * later, at your convenience.
   53.68 + * You should make sure that the JPEG object is cleaned up (with jpeg_abort
   53.69 + * or jpeg_destroy) at some point.
   53.70 + */
   53.71 +
   53.72 +METHODDEF(void)
   53.73 +error_exit (j_common_ptr cinfo)
   53.74 +{
   53.75 +  /* Always display the message */
   53.76 +  (*cinfo->err->output_message) (cinfo);
   53.77 +
   53.78 +  /* Let the memory manager delete any temp files before we die */
   53.79 +  jpeg_destroy(cinfo);
   53.80 +
   53.81 +  exit(EXIT_FAILURE);
   53.82 +}
   53.83 +
   53.84 +
   53.85 +/*
   53.86 + * Actual output of an error or trace message.
   53.87 + * Applications may override this method to send JPEG messages somewhere
   53.88 + * other than stderr.
   53.89 + *
   53.90 + * On Windows, printing to stderr is generally completely useless,
   53.91 + * so we provide optional code to produce an error-dialog popup.
   53.92 + * Most Windows applications will still prefer to override this routine,
   53.93 + * but if they don't, it'll do something at least marginally useful.
   53.94 + *
   53.95 + * NOTE: to use the library in an environment that doesn't support the
   53.96 + * C stdio library, you may have to delete the call to fprintf() entirely,
   53.97 + * not just not use this routine.
   53.98 + */
   53.99 +
  53.100 +METHODDEF(void)
  53.101 +output_message (j_common_ptr cinfo)
  53.102 +{
  53.103 +  char buffer[JMSG_LENGTH_MAX];
  53.104 +
  53.105 +  /* Create the message */
  53.106 +  (*cinfo->err->format_message) (cinfo, buffer);
  53.107 +
  53.108 +#ifdef USE_WINDOWS_MESSAGEBOX
  53.109 +  /* Display it in a message dialog box */
  53.110 +  MessageBox(GetActiveWindow(), buffer, "JPEG Library Error",
  53.111 +	     MB_OK | MB_ICONERROR);
  53.112 +#else
  53.113 +  /* Send it to stderr, adding a newline */
  53.114 +  fprintf(stderr, "%s\n", buffer);
  53.115 +#endif
  53.116 +}
  53.117 +
  53.118 +
  53.119 +/*
  53.120 + * Decide whether to emit a trace or warning message.
  53.121 + * msg_level is one of:
  53.122 + *   -1: recoverable corrupt-data warning, may want to abort.
  53.123 + *    0: important advisory messages (always display to user).
  53.124 + *    1: first level of tracing detail.
  53.125 + *    2,3,...: successively more detailed tracing messages.
  53.126 + * An application might override this method if it wanted to abort on warnings
  53.127 + * or change the policy about which messages to display.
  53.128 + */
  53.129 +
  53.130 +METHODDEF(void)
  53.131 +emit_message (j_common_ptr cinfo, int msg_level)
  53.132 +{
  53.133 +  struct jpeg_error_mgr * err = cinfo->err;
  53.134 +
  53.135 +  if (msg_level < 0) {
  53.136 +    /* It's a warning message.  Since corrupt files may generate many warnings,
  53.137 +     * the policy implemented here is to show only the first warning,
  53.138 +     * unless trace_level >= 3.
  53.139 +     */
  53.140 +    if (err->num_warnings == 0 || err->trace_level >= 3)
  53.141 +      (*err->output_message) (cinfo);
  53.142 +    /* Always count warnings in num_warnings. */
  53.143 +    err->num_warnings++;
  53.144 +  } else {
  53.145 +    /* It's a trace message.  Show it if trace_level >= msg_level. */
  53.146 +    if (err->trace_level >= msg_level)
  53.147 +      (*err->output_message) (cinfo);
  53.148 +  }
  53.149 +}
  53.150 +
  53.151 +
  53.152 +/*
  53.153 + * Format a message string for the most recent JPEG error or message.
  53.154 + * The message is stored into buffer, which should be at least JMSG_LENGTH_MAX
  53.155 + * characters.  Note that no '\n' character is added to the string.
  53.156 + * Few applications should need to override this method.
  53.157 + */
  53.158 +
  53.159 +METHODDEF(void)
  53.160 +format_message (j_common_ptr cinfo, char * buffer)
  53.161 +{
  53.162 +  struct jpeg_error_mgr * err = cinfo->err;
  53.163 +  int msg_code = err->msg_code;
  53.164 +  const char * msgtext = NULL;
  53.165 +  const char * msgptr;
  53.166 +  char ch;
  53.167 +  boolean isstring;
  53.168 +
  53.169 +  /* Look up message string in proper table */
  53.170 +  if (msg_code > 0 && msg_code <= err->last_jpeg_message) {
  53.171 +    msgtext = err->jpeg_message_table[msg_code];
  53.172 +  } else if (err->addon_message_table != NULL &&
  53.173 +	     msg_code >= err->first_addon_message &&
  53.174 +	     msg_code <= err->last_addon_message) {
  53.175 +    msgtext = err->addon_message_table[msg_code - err->first_addon_message];
  53.176 +  }
  53.177 +
  53.178 +  /* Defend against bogus message number */
  53.179 +  if (msgtext == NULL) {
  53.180 +    err->msg_parm.i[0] = msg_code;
  53.181 +    msgtext = err->jpeg_message_table[0];
  53.182 +  }
  53.183 +
  53.184 +  /* Check for string parameter, as indicated by %s in the message text */
  53.185 +  isstring = FALSE;
  53.186 +  msgptr = msgtext;
  53.187 +  while ((ch = *msgptr++) != '\0') {
  53.188 +    if (ch == '%') {
  53.189 +      if (*msgptr == 's') isstring = TRUE;
  53.190 +      break;
  53.191 +    }
  53.192 +  }
  53.193 +
  53.194 +  /* Format the message into the passed buffer */
  53.195 +  if (isstring)
  53.196 +    sprintf(buffer, msgtext, err->msg_parm.s);
  53.197 +  else
  53.198 +    sprintf(buffer, msgtext,
  53.199 +	    err->msg_parm.i[0], err->msg_parm.i[1],
  53.200 +	    err->msg_parm.i[2], err->msg_parm.i[3],
  53.201 +	    err->msg_parm.i[4], err->msg_parm.i[5],
  53.202 +	    err->msg_parm.i[6], err->msg_parm.i[7]);
  53.203 +}
  53.204 +
  53.205 +
  53.206 +/*
  53.207 + * Reset error state variables at start of a new image.
  53.208 + * This is called during compression startup to reset trace/error
  53.209 + * processing to default state, without losing any application-specific
  53.210 + * method pointers.  An application might possibly want to override
  53.211 + * this method if it has additional error processing state.
  53.212 + */
  53.213 +
  53.214 +METHODDEF(void)
  53.215 +reset_error_mgr (j_common_ptr cinfo)
  53.216 +{
  53.217 +  cinfo->err->num_warnings = 0;
  53.218 +  /* trace_level is not reset since it is an application-supplied parameter */
  53.219 +  cinfo->err->msg_code = 0;	/* may be useful as a flag for "no error" */
  53.220 +}
  53.221 +
  53.222 +
  53.223 +/*
  53.224 + * Fill in the standard error-handling methods in a jpeg_error_mgr object.
  53.225 + * Typical call is:
  53.226 + *	struct jpeg_compress_struct cinfo;
  53.227 + *	struct jpeg_error_mgr err;
  53.228 + *
  53.229 + *	cinfo.err = jpeg_std_error(&err);
  53.230 + * after which the application may override some of the methods.
  53.231 + */
  53.232 +
  53.233 +GLOBAL(struct jpeg_error_mgr *)
  53.234 +jpeg_std_error (struct jpeg_error_mgr * err)
  53.235 +{
  53.236 +  err->error_exit = error_exit;
  53.237 +  err->emit_message = emit_message;
  53.238 +  err->output_message = output_message;
  53.239 +  err->format_message = format_message;
  53.240 +  err->reset_error_mgr = reset_error_mgr;
  53.241 +
  53.242 +  err->trace_level = 0;		/* default = no tracing */
  53.243 +  err->num_warnings = 0;	/* no warnings emitted yet */
  53.244 +  err->msg_code = 0;		/* may be useful as a flag for "no error" */
  53.245 +
  53.246 +  /* Initialize message table pointers */
  53.247 +  err->jpeg_message_table = jpeg_std_message_table;
  53.248 +  err->last_jpeg_message = (int) JMSG_LASTMSGCODE - 1;
  53.249 +
  53.250 +  err->addon_message_table = NULL;
  53.251 +  err->first_addon_message = 0;	/* for safety */
  53.252 +  err->last_addon_message = 0;
  53.253 +
  53.254 +  return err;
  53.255 +}
    54.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    54.2 +++ b/libs/libjpeg/jerror.h	Sat Sep 19 05:51:51 2015 +0300
    54.3 @@ -0,0 +1,291 @@
    54.4 +/*
    54.5 + * jerror.h
    54.6 + *
    54.7 + * Copyright (C) 1994-1997, Thomas G. Lane.
    54.8 + * This file is part of the Independent JPEG Group's software.
    54.9 + * For conditions of distribution and use, see the accompanying README file.
   54.10 + *
   54.11 + * This file defines the error and message codes for the JPEG library.
   54.12 + * Edit this file to add new codes, or to translate the message strings to
   54.13 + * some other language.
   54.14 + * A set of error-reporting macros are defined too.  Some applications using
   54.15 + * the JPEG library may wish to include this file to get the error codes
   54.16 + * and/or the macros.
   54.17 + */
   54.18 +
   54.19 +/*
   54.20 + * To define the enum list of message codes, include this file without
   54.21 + * defining macro JMESSAGE.  To create a message string table, include it
   54.22 + * again with a suitable JMESSAGE definition (see jerror.c for an example).
   54.23 + */
   54.24 +#ifndef JMESSAGE
   54.25 +#ifndef JERROR_H
   54.26 +/* First time through, define the enum list */
   54.27 +#define JMAKE_ENUM_LIST
   54.28 +#else
   54.29 +/* Repeated inclusions of this file are no-ops unless JMESSAGE is defined */
   54.30 +#define JMESSAGE(code,string)
   54.31 +#endif /* JERROR_H */
   54.32 +#endif /* JMESSAGE */
   54.33 +
   54.34 +#ifdef JMAKE_ENUM_LIST
   54.35 +
   54.36 +typedef enum {
   54.37 +
   54.38 +#define JMESSAGE(code,string)	code ,
   54.39 +
   54.40 +#endif /* JMAKE_ENUM_LIST */
   54.41 +
   54.42 +JMESSAGE(JMSG_NOMESSAGE, "Bogus message code %d") /* Must be first entry! */
   54.43 +
   54.44 +/* For maintenance convenience, list is alphabetical by message code name */
   54.45 +JMESSAGE(JERR_ARITH_NOTIMPL,
   54.46 +	 "Sorry, there are legal restrictions on arithmetic coding")
   54.47 +JMESSAGE(JERR_BAD_ALIGN_TYPE, "ALIGN_TYPE is wrong, please fix")
   54.48 +JMESSAGE(JERR_BAD_ALLOC_CHUNK, "MAX_ALLOC_CHUNK is wrong, please fix")
   54.49 +JMESSAGE(JERR_BAD_BUFFER_MODE, "Bogus buffer control mode")
   54.50 +JMESSAGE(JERR_BAD_COMPONENT_ID, "Invalid component ID %d in SOS")
   54.51 +JMESSAGE(JERR_BAD_DCT_COEF, "DCT coefficient out of range")
   54.52 +JMESSAGE(JERR_BAD_DCTSIZE, "IDCT output block size %d not supported")
   54.53 +JMESSAGE(JERR_BAD_HUFF_TABLE, "Bogus Huffman table definition")
   54.54 +JMESSAGE(JERR_BAD_IN_COLORSPACE, "Bogus input colorspace")
   54.55 +JMESSAGE(JERR_BAD_J_COLORSPACE, "Bogus JPEG colorspace")
   54.56 +JMESSAGE(JERR_BAD_LENGTH, "Bogus marker length")
   54.57 +JMESSAGE(JERR_BAD_LIB_VERSION,
   54.58 +	 "Wrong JPEG library version: library is %d, caller expects %d")
   54.59 +JMESSAGE(JERR_BAD_MCU_SIZE, "Sampling factors too large for interleaved scan")
   54.60 +JMESSAGE(JERR_BAD_POOL_ID, "Invalid memory pool code %d")
   54.61 +JMESSAGE(JERR_BAD_PRECISION, "Unsupported JPEG data precision %d")
   54.62 +JMESSAGE(JERR_BAD_PROGRESSION,
   54.63 +	 "Invalid progressive parameters Ss=%d Se=%d Ah=%d Al=%d")
   54.64 +JMESSAGE(JERR_BAD_PROG_SCRIPT,
   54.65 +	 "Invalid progressive parameters at scan script entry %d")
   54.66 +JMESSAGE(JERR_BAD_SAMPLING, "Bogus sampling factors")
   54.67 +JMESSAGE(JERR_BAD_SCAN_SCRIPT, "Invalid scan script at entry %d")
   54.68 +JMESSAGE(JERR_BAD_STATE, "Improper call to JPEG library in state %d")
   54.69 +JMESSAGE(JERR_BAD_STRUCT_SIZE,
   54.70 +	 "JPEG parameter struct mismatch: library thinks size is %u, caller expects %u")
   54.71 +JMESSAGE(JERR_BAD_VIRTUAL_ACCESS, "Bogus virtual array access")
   54.72 +JMESSAGE(JERR_BUFFER_SIZE, "Buffer passed to JPEG library is too small")
   54.73 +JMESSAGE(JERR_CANT_SUSPEND, "Suspension not allowed here")
   54.74 +JMESSAGE(JERR_CCIR601_NOTIMPL, "CCIR601 sampling not implemented yet")
   54.75 +JMESSAGE(JERR_COMPONENT_COUNT, "Too many color components: %d, max %d")
   54.76 +JMESSAGE(JERR_CONVERSION_NOTIMPL, "Unsupported color conversion request")
   54.77 +JMESSAGE(JERR_DAC_INDEX, "Bogus DAC index %d")
   54.78 +JMESSAGE(JERR_DAC_VALUE, "Bogus DAC value 0x%x")
   54.79 +JMESSAGE(JERR_DHT_INDEX, "Bogus DHT index %d")
   54.80 +JMESSAGE(JERR_DQT_INDEX, "Bogus DQT index %d")
   54.81 +JMESSAGE(JERR_EMPTY_IMAGE, "Empty JPEG image (DNL not supported)")
   54.82 +JMESSAGE(JERR_EMS_READ, "Read from EMS failed")
   54.83 +JMESSAGE(JERR_EMS_WRITE, "Write to EMS failed")
   54.84 +JMESSAGE(JERR_EOI_EXPECTED, "Didn't expect more than one scan")
   54.85 +JMESSAGE(JERR_FILE_READ, "Input file read error")
   54.86 +JMESSAGE(JERR_FILE_WRITE, "Output file write error --- out of disk space?")
   54.87 +JMESSAGE(JERR_FRACT_SAMPLE_NOTIMPL, "Fractional sampling not implemented yet")
   54.88 +JMESSAGE(JERR_HUFF_CLEN_OVERFLOW, "Huffman code size table overflow")
   54.89 +JMESSAGE(JERR_HUFF_MISSING_CODE, "Missing Huffman code table entry")
   54.90 +JMESSAGE(JERR_IMAGE_TOO_BIG, "Maximum supported image dimension is %u pixels")
   54.91 +JMESSAGE(JERR_INPUT_EMPTY, "Empty input file")
   54.92 +JMESSAGE(JERR_INPUT_EOF, "Premature end of input file")
   54.93 +JMESSAGE(JERR_MISMATCHED_QUANT_TABLE,
   54.94 +	 "Cannot transcode due to multiple use of quantization table %d")
   54.95 +JMESSAGE(JERR_MISSING_DATA, "Scan script does not transmit all data")
   54.96 +JMESSAGE(JERR_MODE_CHANGE, "Invalid color quantization mode change")
   54.97 +JMESSAGE(JERR_NOTIMPL, "Not implemented yet")
   54.98 +JMESSAGE(JERR_NOT_COMPILED, "Requested feature was omitted at compile time")
   54.99 +JMESSAGE(JERR_NO_BACKING_STORE, "Backing store not supported")
  54.100 +JMESSAGE(JERR_NO_HUFF_TABLE, "Huffman table 0x%02x was not defined")
  54.101 +JMESSAGE(JERR_NO_IMAGE, "JPEG datastream contains no image")
  54.102 +JMESSAGE(JERR_NO_QUANT_TABLE, "Quantization table 0x%02x was not defined")
  54.103 +JMESSAGE(JERR_NO_SOI, "Not a JPEG file: starts with 0x%02x 0x%02x")
  54.104 +JMESSAGE(JERR_OUT_OF_MEMORY, "Insufficient memory (case %d)")
  54.105 +JMESSAGE(JERR_QUANT_COMPONENTS,
  54.106 +	 "Cannot quantize more than %d color components")
  54.107 +JMESSAGE(JERR_QUANT_FEW_COLORS, "Cannot quantize to fewer than %d colors")
  54.108 +JMESSAGE(JERR_QUANT_MANY_COLORS, "Cannot quantize to more than %d colors")
  54.109 +JMESSAGE(JERR_SOF_DUPLICATE, "Invalid JPEG file structure: two SOF markers")
  54.110 +JMESSAGE(JERR_SOF_NO_SOS, "Invalid JPEG file structure: missing SOS marker")
  54.111 +JMESSAGE(JERR_SOF_UNSUPPORTED, "Unsupported JPEG process: SOF type 0x%02x")
  54.112 +JMESSAGE(JERR_SOI_DUPLICATE, "Invalid JPEG file structure: two SOI markers")
  54.113 +JMESSAGE(JERR_SOS_NO_SOF, "Invalid JPEG file structure: SOS before SOF")
  54.114 +JMESSAGE(JERR_TFILE_CREATE, "Failed to create temporary file %s")
  54.115 +JMESSAGE(JERR_TFILE_READ, "Read failed on temporary file")
  54.116 +JMESSAGE(JERR_TFILE_SEEK, "Seek failed on temporary file")
  54.117 +JMESSAGE(JERR_TFILE_WRITE,
  54.118 +	 "Write failed on temporary file --- out of disk space?")
  54.119 +JMESSAGE(JERR_TOO_LITTLE_DATA, "Application transferred too few scanlines")
  54.120 +JMESSAGE(JERR_UNKNOWN_MARKER, "Unsupported marker type 0x%02x")
  54.121 +JMESSAGE(JERR_VIRTUAL_BUG, "Virtual array controller messed up")
  54.122 +JMESSAGE(JERR_WIDTH_OVERFLOW, "Image too wide for this implementation")
  54.123 +JMESSAGE(JERR_XMS_READ, "Read from XMS failed")
  54.124 +JMESSAGE(JERR_XMS_WRITE, "Write to XMS failed")
  54.125 +JMESSAGE(JMSG_COPYRIGHT, JCOPYRIGHT)
  54.126 +JMESSAGE(JMSG_VERSION, JVERSION)
  54.127 +JMESSAGE(JTRC_16BIT_TABLES,
  54.128 +	 "Caution: quantization tables are too coarse for baseline JPEG")
  54.129 +JMESSAGE(JTRC_ADOBE,
  54.130 +	 "Adobe APP14 marker: version %d, flags 0x%04x 0x%04x, transform %d")
  54.131 +JMESSAGE(JTRC_APP0, "Unknown APP0 marker (not JFIF), length %u")
  54.132 +JMESSAGE(JTRC_APP14, "Unknown APP14 marker (not Adobe), length %u")
  54.133 +JMESSAGE(JTRC_DAC, "Define Arithmetic Table 0x%02x: 0x%02x")
  54.134 +JMESSAGE(JTRC_DHT, "Define Huffman Table 0x%02x")
  54.135 +JMESSAGE(JTRC_DQT, "Define Quantization Table %d  precision %d")
  54.136 +JMESSAGE(JTRC_DRI, "Define Restart Interval %u")
  54.137 +JMESSAGE(JTRC_EMS_CLOSE, "Freed EMS handle %u")
  54.138 +JMESSAGE(JTRC_EMS_OPEN, "Obtained EMS handle %u")
  54.139 +JMESSAGE(JTRC_EOI, "End Of Image")
  54.140 +JMESSAGE(JTRC_HUFFBITS, "        %3d %3d %3d %3d %3d %3d %3d %3d")
  54.141 +JMESSAGE(JTRC_JFIF, "JFIF APP0 marker: version %d.%02d, density %dx%d  %d")
  54.142 +JMESSAGE(JTRC_JFIF_BADTHUMBNAILSIZE,
  54.143 +	 "Warning: thumbnail image size does not match data length %u")
  54.144 +JMESSAGE(JTRC_JFIF_EXTENSION,
  54.145 +	 "JFIF extension marker: type 0x%02x, length %u")
  54.146 +JMESSAGE(JTRC_JFIF_THUMBNAIL, "    with %d x %d thumbnail image")
  54.147 +JMESSAGE(JTRC_MISC_MARKER, "Miscellaneous marker 0x%02x, length %u")
  54.148 +JMESSAGE(JTRC_PARMLESS_MARKER, "Unexpected marker 0x%02x")
  54.149 +JMESSAGE(JTRC_QUANTVALS, "        %4u %4u %4u %4u %4u %4u %4u %4u")
  54.150 +JMESSAGE(JTRC_QUANT_3_NCOLORS, "Quantizing to %d = %d*%d*%d colors")
  54.151 +JMESSAGE(JTRC_QUANT_NCOLORS, "Quantizing to %d colors")
  54.152 +JMESSAGE(JTRC_QUANT_SELECTED, "Selected %d colors for quantization")
  54.153 +JMESSAGE(JTRC_RECOVERY_ACTION, "At marker 0x%02x, recovery action %d")
  54.154 +JMESSAGE(JTRC_RST, "RST%d")
  54.155 +JMESSAGE(JTRC_SMOOTH_NOTIMPL,
  54.156 +	 "Smoothing not supported with nonstandard sampling ratios")
  54.157 +JMESSAGE(JTRC_SOF, "Start Of Frame 0x%02x: width=%u, height=%u, components=%d")
  54.158 +JMESSAGE(JTRC_SOF_COMPONENT, "    Component %d: %dhx%dv q=%d")
  54.159 +JMESSAGE(JTRC_SOI, "Start of Image")
  54.160 +JMESSAGE(JTRC_SOS, "Start Of Scan: %d components")
  54.161 +JMESSAGE(JTRC_SOS_COMPONENT, "    Component %d: dc=%d ac=%d")
  54.162 +JMESSAGE(JTRC_SOS_PARAMS, "  Ss=%d, Se=%d, Ah=%d, Al=%d")
  54.163 +JMESSAGE(JTRC_TFILE_CLOSE, "Closed temporary file %s")
  54.164 +JMESSAGE(JTRC_TFILE_OPEN, "Opened temporary file %s")
  54.165 +JMESSAGE(JTRC_THUMB_JPEG,
  54.166 +	 "JFIF extension marker: JPEG-compressed thumbnail image, length %u")
  54.167 +JMESSAGE(JTRC_THUMB_PALETTE,
  54.168 +	 "JFIF extension marker: palette thumbnail image, length %u")
  54.169 +JMESSAGE(JTRC_THUMB_RGB,
  54.170 +	 "JFIF extension marker: RGB thumbnail image, length %u")
  54.171 +JMESSAGE(JTRC_UNKNOWN_IDS,
  54.172 +	 "Unrecognized component IDs %d %d %d, assuming YCbCr")
  54.173 +JMESSAGE(JTRC_XMS_CLOSE, "Freed XMS handle %u")
  54.174 +JMESSAGE(JTRC_XMS_OPEN, "Obtained XMS handle %u")
  54.175 +JMESSAGE(JWRN_ADOBE_XFORM, "Unknown Adobe color transform code %d")
  54.176 +JMESSAGE(JWRN_BOGUS_PROGRESSION,
  54.177 +	 "Inconsistent progression sequence for component %d coefficient %d")
  54.178 +JMESSAGE(JWRN_EXTRANEOUS_DATA,
  54.179 +	 "Corrupt JPEG data: %u extraneous bytes before marker 0x%02x")
  54.180 +JMESSAGE(JWRN_HIT_MARKER, "Corrupt JPEG data: premature end of data segment")
  54.181 +JMESSAGE(JWRN_HUFF_BAD_CODE, "Corrupt JPEG data: bad Huffman code")
  54.182 +JMESSAGE(JWRN_JFIF_MAJOR, "Warning: unknown JFIF revision number %d.%02d")
  54.183 +JMESSAGE(JWRN_JPEG_EOF, "Premature end of JPEG file")
  54.184 +JMESSAGE(JWRN_MUST_RESYNC,
  54.185 +	 "Corrupt JPEG data: found marker 0x%02x instead of RST%d")
  54.186 +JMESSAGE(JWRN_NOT_SEQUENTIAL, "Invalid SOS parameters for sequential JPEG")
  54.187 +JMESSAGE(JWRN_TOO_MUCH_DATA, "Application transferred too many scanlines")
  54.188 +
  54.189 +#ifdef JMAKE_ENUM_LIST
  54.190 +
  54.191 +  JMSG_LASTMSGCODE
  54.192 +} J_MESSAGE_CODE;
  54.193 +
  54.194 +#undef JMAKE_ENUM_LIST
  54.195 +#endif /* JMAKE_ENUM_LIST */
  54.196 +
  54.197 +/* Zap JMESSAGE macro so that future re-inclusions do nothing by default */
  54.198 +#undef JMESSAGE
  54.199 +
  54.200 +
  54.201 +#ifndef JERROR_H
  54.202 +#define JERROR_H
  54.203 +
  54.204 +/* Macros to simplify using the error and trace message stuff */
  54.205 +/* The first parameter is either type of cinfo pointer */
  54.206 +
  54.207 +/* Fatal errors (print message and exit) */
  54.208 +#define ERREXIT(cinfo,code)  \
  54.209 +  ((cinfo)->err->msg_code = (code), \
  54.210 +   (*(cinfo)->err->error_exit) ((j_common_ptr) (cinfo)))
  54.211 +#define ERREXIT1(cinfo,code,p1)  \
  54.212 +  ((cinfo)->err->msg_code = (code), \
  54.213 +   (cinfo)->err->msg_parm.i[0] = (p1), \
  54.214 +   (*(cinfo)->err->error_exit) ((j_common_ptr) (cinfo)))
  54.215 +#define ERREXIT2(cinfo,code,p1,p2)  \
  54.216 +  ((cinfo)->err->msg_code = (code), \
  54.217 +   (cinfo)->err->msg_parm.i[0] = (p1), \
  54.218 +   (cinfo)->err->msg_parm.i[1] = (p2), \
  54.219 +   (*(cinfo)->err->error_exit) ((j_common_ptr) (cinfo)))
  54.220 +#define ERREXIT3(cinfo,code,p1,p2,p3)  \
  54.221 +  ((cinfo)->err->msg_code = (code), \
  54.222 +   (cinfo)->err->msg_parm.i[0] = (p1), \
  54.223 +   (cinfo)->err->msg_parm.i[1] = (p2), \
  54.224 +   (cinfo)->err->msg_parm.i[2] = (p3), \
  54.225 +   (*(cinfo)->err->error_exit) ((j_common_ptr) (cinfo)))
  54.226 +#define ERREXIT4(cinfo,code,p1,p2,p3,p4)  \
  54.227 +  ((cinfo)->err->msg_code = (code), \
  54.228 +   (cinfo)->err->msg_parm.i[0] = (p1), \
  54.229 +   (cinfo)->err->msg_parm.i[1] = (p2), \
  54.230 +   (cinfo)->err->msg_parm.i[2] = (p3), \
  54.231 +   (cinfo)->err->msg_parm.i[3] = (p4), \
  54.232 +   (*(cinfo)->err->error_exit) ((j_common_ptr) (cinfo)))
  54.233 +#define ERREXITS(cinfo,code,str)  \
  54.234 +  ((cinfo)->err->msg_code = (code), \
  54.235 +   strncpy((cinfo)->err->msg_parm.s, (str), JMSG_STR_PARM_MAX), \
  54.236 +   (*(cinfo)->err->error_exit) ((j_common_ptr) (cinfo)))
  54.237 +
  54.238 +#define MAKESTMT(stuff)		do { stuff } while (0)
  54.239 +
  54.240 +/* Nonfatal errors (we can keep going, but the data is probably corrupt) */
  54.241 +#define WARNMS(cinfo,code)  \
  54.242 +  ((cinfo)->err->msg_code = (code), \
  54.243 +   (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), -1))
  54.244 +#define WARNMS1(cinfo,code,p1)  \
  54.245 +  ((cinfo)->err->msg_code = (code), \
  54.246 +   (cinfo)->err->msg_parm.i[0] = (p1), \
  54.247 +   (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), -1))
  54.248 +#define WARNMS2(cinfo,code,p1,p2)  \
  54.249 +  ((cinfo)->err->msg_code = (code), \
  54.250 +   (cinfo)->err->msg_parm.i[0] = (p1), \
  54.251 +   (cinfo)->err->msg_parm.i[1] = (p2), \
  54.252 +   (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), -1))
  54.253 +
  54.254 +/* Informational/debugging messages */
  54.255 +#define TRACEMS(cinfo,lvl,code)  \
  54.256 +  ((cinfo)->err->msg_code = (code), \
  54.257 +   (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), (lvl)))
  54.258 +#define TRACEMS1(cinfo,lvl,code,p1)  \
  54.259 +  ((cinfo)->err->msg_code = (code), \
  54.260 +   (cinfo)->err->msg_parm.i[0] = (p1), \
  54.261 +   (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), (lvl)))
  54.262 +#define TRACEMS2(cinfo,lvl,code,p1,p2)  \
  54.263 +  ((cinfo)->err->msg_code = (code), \
  54.264 +   (cinfo)->err->msg_parm.i[0] = (p1), \
  54.265 +   (cinfo)->err->msg_parm.i[1] = (p2), \
  54.266 +   (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), (lvl)))
  54.267 +#define TRACEMS3(cinfo,lvl,code,p1,p2,p3)  \
  54.268 +  MAKESTMT(int * _mp = (cinfo)->err->msg_parm.i; \
  54.269 +	   _mp[0] = (p1); _mp[1] = (p2); _mp[2] = (p3); \
  54.270 +	   (cinfo)->err->msg_code = (code); \
  54.271 +	   (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), (lvl)); )
  54.272 +#define TRACEMS4(cinfo,lvl,code,p1,p2,p3,p4)  \
  54.273 +  MAKESTMT(int * _mp = (cinfo)->err->msg_parm.i; \
  54.274 +	   _mp[0] = (p1); _mp[1] = (p2); _mp[2] = (p3); _mp[3] = (p4); \
  54.275 +	   (cinfo)->err->msg_code = (code); \
  54.276 +	   (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), (lvl)); )
  54.277 +#define TRACEMS5(cinfo,lvl,code,p1,p2,p3,p4,p5)  \
  54.278 +  MAKESTMT(int * _mp = (cinfo)->err->msg_parm.i; \
  54.279 +	   _mp[0] = (p1); _mp[1] = (p2); _mp[2] = (p3); _mp[3] = (p4); \
  54.280 +	   _mp[4] = (p5); \
  54.281 +	   (cinfo)->err->msg_code = (code); \
  54.282 +	   (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), (lvl)); )
  54.283 +#define TRACEMS8(cinfo,lvl,code,p1,p2,p3,p4,p5,p6,p7,p8)  \
  54.284 +  MAKESTMT(int * _mp = (cinfo)->err->msg_parm.i; \
  54.285 +	   _mp[0] = (p1); _mp[1] = (p2); _mp[2] = (p3); _mp[3] = (p4); \
  54.286 +	   _mp[4] = (p5); _mp[5] = (p6); _mp[6] = (p7); _mp[7] = (p8); \
  54.287 +	   (cinfo)->err->msg_code = (code); \
  54.288 +	   (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), (lvl)); )
  54.289 +#define TRACEMSS(cinfo,lvl,code,str)  \
  54.290 +  ((cinfo)->err->msg_code = (code), \
  54.291 +   strncpy((cinfo)->err->msg_parm.s, (str), JMSG_STR_PARM_MAX), \
  54.292 +   (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), (lvl)))
  54.293 +
  54.294 +#endif /* JERROR_H */
    55.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    55.2 +++ b/libs/libjpeg/jfdctflt.c	Sat Sep 19 05:51:51 2015 +0300
    55.3 @@ -0,0 +1,168 @@
    55.4 +/*
    55.5 + * jfdctflt.c
    55.6 + *
    55.7 + * Copyright (C) 1994-1996, Thomas G. Lane.
    55.8 + * This file is part of the Independent JPEG Group's software.
    55.9 + * For conditions of distribution and use, see the accompanying README file.
   55.10 + *
   55.11 + * This file contains a floating-point implementation of the
   55.12 + * forward DCT (Discrete Cosine Transform).
   55.13 + *
   55.14 + * This implementation should be more accurate than either of the integer
   55.15 + * DCT implementations.  However, it may not give the same results on all
   55.16 + * machines because of differences in roundoff behavior.  Speed will depend
   55.17 + * on the hardware's floating point capacity.
   55.18 + *
   55.19 + * A 2-D DCT can be done by 1-D DCT on each row followed by 1-D DCT
   55.20 + * on each column.  Direct algorithms are also available, but they are
   55.21 + * much more complex and seem not to be any faster when reduced to code.
   55.22 + *
   55.23 + * This implementation is based on Arai, Agui, and Nakajima's algorithm for
   55.24 + * scaled DCT.  Their original paper (Trans. IEICE E-71(11):1095) is in
   55.25 + * Japanese, but the algorithm is described in the Pennebaker & Mitchell
   55.26 + * JPEG textbook (see REFERENCES section in file README).  The following code
   55.27 + * is based directly on figure 4-8 in P&M.
   55.28 + * While an 8-point DCT cannot be done in less than 11 multiplies, it is
   55.29 + * possible to arrange the computation so that many of the multiplies are
   55.30 + * simple scalings of the final outputs.  These multiplies can then be
   55.31 + * folded into the multiplications or divisions by the JPEG quantization
   55.32 + * table entries.  The AA&N method leaves only 5 multiplies and 29 adds
   55.33 + * to be done in the DCT itself.
   55.34 + * The primary disadvantage of this method is that with a fixed-point
   55.35 + * implementation, accuracy is lost due to imprecise representation of the
   55.36 + * scaled quantization values.  However, that problem does not arise if
   55.37 + * we use floating point arithmetic.
   55.38 + */
   55.39 +
   55.40 +#define JPEG_INTERNALS
   55.41 +#include "jinclude.h"
   55.42 +#include "jpeglib.h"
   55.43 +#include "jdct.h"		/* Private declarations for DCT subsystem */
   55.44 +
   55.45 +#ifdef DCT_FLOAT_SUPPORTED
   55.46 +
   55.47 +
   55.48 +/*
   55.49 + * This module is specialized to the case DCTSIZE = 8.
   55.50 + */
   55.51 +
   55.52 +#if DCTSIZE != 8
   55.53 +  Sorry, this code only copes with 8x8 DCTs. /* deliberate syntax err */
   55.54 +#endif
   55.55 +
   55.56 +
   55.57 +/*
   55.58 + * Perform the forward DCT on one block of samples.
   55.59 + */
   55.60 +
   55.61 +GLOBAL(void)
   55.62 +jpeg_fdct_float (FAST_FLOAT * data)
   55.63 +{
   55.64 +  FAST_FLOAT tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
   55.65 +  FAST_FLOAT tmp10, tmp11, tmp12, tmp13;
   55.66 +  FAST_FLOAT z1, z2, z3, z4, z5, z11, z13;
   55.67 +  FAST_FLOAT *dataptr;
   55.68 +  int ctr;
   55.69 +
   55.70 +  /* Pass 1: process rows. */
   55.71 +
   55.72 +  dataptr = data;
   55.73 +  for (ctr = DCTSIZE-1; ctr >= 0; ctr--) {
   55.74 +    tmp0 = dataptr[0] + dataptr[7];
   55.75 +    tmp7 = dataptr[0] - dataptr[7];
   55.76 +    tmp1 = dataptr[1] + dataptr[6];
   55.77 +    tmp6 = dataptr[1] - dataptr[6];
   55.78 +    tmp2 = dataptr[2] + dataptr[5];
   55.79 +    tmp5 = dataptr[2] - dataptr[5];
   55.80 +    tmp3 = dataptr[3] + dataptr[4];
   55.81 +    tmp4 = dataptr[3] - dataptr[4];
   55.82 +    
   55.83 +    /* Even part */
   55.84 +    
   55.85 +    tmp10 = tmp0 + tmp3;	/* phase 2 */
   55.86 +    tmp13 = tmp0 - tmp3;
   55.87 +    tmp11 = tmp1 + tmp2;
   55.88 +    tmp12 = tmp1 - tmp2;
   55.89 +    
   55.90 +    dataptr[0] = tmp10 + tmp11; /* phase 3 */
   55.91 +    dataptr[4] = tmp10 - tmp11;
   55.92 +    
   55.93 +    z1 = (tmp12 + tmp13) * ((FAST_FLOAT) 0.707106781); /* c4 */
   55.94 +    dataptr[2] = tmp13 + z1;	/* phase 5 */
   55.95 +    dataptr[6] = tmp13 - z1;
   55.96 +    
   55.97 +    /* Odd part */
   55.98 +
   55.99 +    tmp10 = tmp4 + tmp5;	/* phase 2 */
  55.100 +    tmp11 = tmp5 + tmp6;
  55.101 +    tmp12 = tmp6 + tmp7;
  55.102 +
  55.103 +    /* The rotator is modified from fig 4-8 to avoid extra negations. */
  55.104 +    z5 = (tmp10 - tmp12) * ((FAST_FLOAT) 0.382683433); /* c6 */
  55.105 +    z2 = ((FAST_FLOAT) 0.541196100) * tmp10 + z5; /* c2-c6 */
  55.106 +    z4 = ((FAST_FLOAT) 1.306562965) * tmp12 + z5; /* c2+c6 */
  55.107 +    z3 = tmp11 * ((FAST_FLOAT) 0.707106781); /* c4 */
  55.108 +
  55.109 +    z11 = tmp7 + z3;		/* phase 5 */
  55.110 +    z13 = tmp7 - z3;
  55.111 +
  55.112 +    dataptr[5] = z13 + z2;	/* phase 6 */
  55.113 +    dataptr[3] = z13 - z2;
  55.114 +    dataptr[1] = z11 + z4;
  55.115 +    dataptr[7] = z11 - z4;
  55.116 +
  55.117 +    dataptr += DCTSIZE;		/* advance pointer to next row */
  55.118 +  }
  55.119 +
  55.120 +  /* Pass 2: process columns. */
  55.121 +
  55.122 +  dataptr = data;
  55.123 +  for (ctr = DCTSIZE-1; ctr >= 0; ctr--) {
  55.124 +    tmp0 = dataptr[DCTSIZE*0] + dataptr[DCTSIZE*7];
  55.125 +    tmp7 = dataptr[DCTSIZE*0] - dataptr[DCTSIZE*7];
  55.126 +    tmp1 = dataptr[DCTSIZE*1] + dataptr[DCTSIZE*6];
  55.127 +    tmp6 = dataptr[DCTSIZE*1] - dataptr[DCTSIZE*6];
  55.128 +    tmp2 = dataptr[DCTSIZE*2] + dataptr[DCTSIZE*5];
  55.129 +    tmp5 = dataptr[DCTSIZE*2] - dataptr[DCTSIZE*5];
  55.130 +    tmp3 = dataptr[DCTSIZE*3] + dataptr[DCTSIZE*4];
  55.131 +    tmp4 = dataptr[DCTSIZE*3] - dataptr[DCTSIZE*4];
  55.132 +    
  55.133 +    /* Even part */
  55.134 +    
  55.135 +    tmp10 = tmp0 + tmp3;	/* phase 2 */
  55.136 +    tmp13 = tmp0 - tmp3;
  55.137 +    tmp11 = tmp1 + tmp2;
  55.138 +    tmp12 = tmp1 - tmp2;
  55.139 +    
  55.140 +    dataptr[DCTSIZE*0] = tmp10 + tmp11; /* phase 3 */
  55.141 +    dataptr[DCTSIZE*4] = tmp10 - tmp11;
  55.142 +    
  55.143 +    z1 = (tmp12 + tmp13) * ((FAST_FLOAT) 0.707106781); /* c4 */
  55.144 +    dataptr[DCTSIZE*2] = tmp13 + z1; /* phase 5 */
  55.145 +    dataptr[DCTSIZE*6] = tmp13 - z1;
  55.146 +    
  55.147 +    /* Odd part */
  55.148 +
  55.149 +    tmp10 = tmp4 + tmp5;	/* phase 2 */
  55.150 +    tmp11 = tmp5 + tmp6;
  55.151 +    tmp12 = tmp6 + tmp7;
  55.152 +
  55.153 +    /* The rotator is modified from fig 4-8 to avoid extra negations. */
  55.154 +    z5 = (tmp10 - tmp12) * ((FAST_FLOAT) 0.382683433); /* c6 */
  55.155 +    z2 = ((FAST_FLOAT) 0.541196100) * tmp10 + z5; /* c2-c6 */
  55.156 +    z4 = ((FAST_FLOAT) 1.306562965) * tmp12 + z5; /* c2+c6 */
  55.157 +    z3 = tmp11 * ((FAST_FLOAT) 0.707106781); /* c4 */
  55.158 +
  55.159 +    z11 = tmp7 + z3;		/* phase 5 */
  55.160 +    z13 = tmp7 - z3;
  55.161 +
  55.162 +    dataptr[DCTSIZE*5] = z13 + z2; /* phase 6 */
  55.163 +    dataptr[DCTSIZE*3] = z13 - z2;
  55.164 +    dataptr[DCTSIZE*1] = z11 + z4;
  55.165 +    dataptr[DCTSIZE*7] = z11 - z4;
  55.166 +
  55.167 +    dataptr++;			/* advance pointer to next column */
  55.168 +  }
  55.169 +}
  55.170 +
  55.171 +#endif /* DCT_FLOAT_SUPPORTED */
    56.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    56.2 +++ b/libs/libjpeg/jfdctfst.c	Sat Sep 19 05:51:51 2015 +0300
    56.3 @@ -0,0 +1,224 @@
    56.4 +/*
    56.5 + * jfdctfst.c
    56.6 + *
    56.7 + * Copyright (C) 1994-1996, Thomas G. Lane.
    56.8 + * This file is part of the Independent JPEG Group's software.
    56.9 + * For conditions of distribution and use, see the accompanying README file.
   56.10 + *
   56.11 + * This file contains a fast, not so accurate integer implementation of the
   56.12 + * forward DCT (Discrete Cosine Transform).
   56.13 + *
   56.14 + * A 2-D DCT can be done by 1-D DCT on each row followed by 1-D DCT
   56.15 + * on each column.  Direct algorithms are also available, but they are
   56.16 + * much more complex and seem not to be any faster when reduced to code.
   56.17 + *
   56.18 + * This implementation is based on Arai, Agui, and Nakajima's algorithm for
   56.19 + * scaled DCT.  Their original paper (Trans. IEICE E-71(11):1095) is in
   56.20 + * Japanese, but the algorithm is described in the Pennebaker & Mitchell
   56.21 + * JPEG textbook (see REFERENCES section in file README).  The following code
   56.22 + * is based directly on figure 4-8 in P&M.
   56.23 + * While an 8-point DCT cannot be done in less than 11 multiplies, it is
   56.24 + * possible to arrange the computation so that many of the multiplies are
   56.25 + * simple scalings of the final outputs.  These multiplies can then be
   56.26 + * folded into the multiplications or divisions by the JPEG quantization
   56.27 + * table entries.  The AA&N method leaves only 5 multiplies and 29 adds
   56.28 + * to be done in the DCT itself.
   56.29 + * The primary disadvantage of this method is that with fixed-point math,
   56.30 + * accuracy is lost due to imprecise representation of the scaled
   56.31 + * quantization values.  The smaller the quantization table entry, the less
   56.32 + * precise the scaled value, so this implementation does worse with high-
   56.33 + * quality-setting files than with low-quality ones.
   56.34 + */
   56.35 +
   56.36 +#define JPEG_INTERNALS
   56.37 +#include "jinclude.h"
   56.38 +#include "jpeglib.h"
   56.39 +#include "jdct.h"		/* Private declarations for DCT subsystem */
   56.40 +
   56.41 +#ifdef DCT_IFAST_SUPPORTED
   56.42 +
   56.43 +
   56.44 +/*
   56.45 + * This module is specialized to the case DCTSIZE = 8.
   56.46 + */
   56.47 +
   56.48 +#if DCTSIZE != 8
   56.49 +  Sorry, this code only copes with 8x8 DCTs. /* deliberate syntax err */
   56.50 +#endif
   56.51 +
   56.52 +
   56.53 +/* Scaling decisions are generally the same as in the LL&M algorithm;
   56.54 + * see jfdctint.c for more details.  However, we choose to descale
   56.55 + * (right shift) multiplication products as soon as they are formed,
   56.56 + * rather than carrying additional fractional bits into subsequent additions.
   56.57 + * This compromises accuracy slightly, but it lets us save a few shifts.
   56.58 + * More importantly, 16-bit arithmetic is then adequate (for 8-bit samples)
   56.59 + * everywhere except in the multiplications proper; this saves a good deal
   56.60 + * of work on 16-bit-int machines.
   56.61 + *
   56.62 + * Again to save a few shifts, the intermediate results between pass 1 and
   56.63 + * pass 2 are not upscaled, but are represented only to integral precision.
   56.64 + *
   56.65 + * A final compromise is to represent the multiplicative constants to only
   56.66 + * 8 fractional bits, rather than 13.  This saves some shifting work on some
   56.67 + * machines, and may also reduce the cost of multiplication (since there
   56.68 + * are fewer one-bits in the constants).
   56.69 + */
   56.70 +
   56.71 +#define CONST_BITS  8
   56.72 +
   56.73 +
   56.74 +/* Some C compilers fail to reduce "FIX(constant)" at compile time, thus
   56.75 + * causing a lot of useless floating-point operations at run time.
   56.76 + * To get around this we use the following pre-calculated constants.
   56.77 + * If you change CONST_BITS you may want to add appropriate values.
   56.78 + * (With a reasonable C compiler, you can just rely on the FIX() macro...)
   56.79 + */
   56.80 +
   56.81 +#if CONST_BITS == 8
   56.82 +#define FIX_0_382683433  ((INT32)   98)		/* FIX(0.382683433) */
   56.83 +#define FIX_0_541196100  ((INT32)  139)		/* FIX(0.541196100) */
   56.84 +#define FIX_0_707106781  ((INT32)  181)		/* FIX(0.707106781) */
   56.85 +#define FIX_1_306562965  ((INT32)  334)		/* FIX(1.306562965) */
   56.86 +#else
   56.87 +#define FIX_0_382683433  FIX(0.382683433)
   56.88 +#define FIX_0_541196100  FIX(0.541196100)
   56.89 +#define FIX_0_707106781  FIX(0.707106781)
   56.90 +#define FIX_1_306562965  FIX(1.306562965)
   56.91 +#endif
   56.92 +
   56.93 +
   56.94 +/* We can gain a little more speed, with a further compromise in accuracy,
   56.95 + * by omitting the addition in a descaling shift.  This yields an incorrectly
   56.96 + * rounded result half the time...
   56.97 + */
   56.98 +
   56.99 +#ifndef USE_ACCURATE_ROUNDING
  56.100 +#undef DESCALE
  56.101 +#define DESCALE(x,n)  RIGHT_SHIFT(x, n)
  56.102 +#endif
  56.103 +
  56.104 +
  56.105 +/* Multiply a DCTELEM variable by an INT32 constant, and immediately
  56.106 + * descale to yield a DCTELEM result.
  56.107 + */
  56.108 +
  56.109 +#define MULTIPLY(var,const)  ((DCTELEM) DESCALE((var) * (const), CONST_BITS))
  56.110 +
  56.111 +
  56.112 +/*
  56.113 + * Perform the forward DCT on one block of samples.
  56.114 + */
  56.115 +
  56.116 +GLOBAL(void)
  56.117 +jpeg_fdct_ifast (DCTELEM * data)
  56.118 +{
  56.119 +  DCTELEM tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
  56.120 +  DCTELEM tmp10, tmp11, tmp12, tmp13;
  56.121 +  DCTELEM z1, z2, z3, z4, z5, z11, z13;
  56.122 +  DCTELEM *dataptr;
  56.123 +  int ctr;
  56.124 +  SHIFT_TEMPS
  56.125 +
  56.126 +  /* Pass 1: process rows. */
  56.127 +
  56.128 +  dataptr = data;
  56.129 +  for (ctr = DCTSIZE-1; ctr >= 0; ctr--) {
  56.130 +    tmp0 = dataptr[0] + dataptr[7];
  56.131 +    tmp7 = dataptr[0] - dataptr[7];
  56.132 +    tmp1 = dataptr[1] + dataptr[6];
  56.133 +    tmp6 = dataptr[1] - dataptr[6];
  56.134 +    tmp2 = dataptr[2] + dataptr[5];
  56.135 +    tmp5 = dataptr[2] - dataptr[5];
  56.136 +    tmp3 = dataptr[3] + dataptr[4];
  56.137 +    tmp4 = dataptr[3] - dataptr[4];
  56.138 +    
  56.139 +    /* Even part */
  56.140 +    
  56.141 +    tmp10 = tmp0 + tmp3;	/* phase 2 */
  56.142 +    tmp13 = tmp0 - tmp3;
  56.143 +    tmp11 = tmp1 + tmp2;
  56.144 +    tmp12 = tmp1 - tmp2;
  56.145 +    
  56.146 +    dataptr[0] = tmp10 + tmp11; /* phase 3 */
  56.147 +    dataptr[4] = tmp10 - tmp11;
  56.148 +    
  56.149 +    z1 = MULTIPLY(tmp12 + tmp13, FIX_0_707106781); /* c4 */
  56.150 +    dataptr[2] = tmp13 + z1;	/* phase 5 */
  56.151 +    dataptr[6] = tmp13 - z1;
  56.152 +    
  56.153 +    /* Odd part */
  56.154 +
  56.155 +    tmp10 = tmp4 + tmp5;	/* phase 2 */
  56.156 +    tmp11 = tmp5 + tmp6;
  56.157 +    tmp12 = tmp6 + tmp7;
  56.158 +
  56.159 +    /* The rotator is modified from fig 4-8 to avoid extra negations. */
  56.160 +    z5 = MULTIPLY(tmp10 - tmp12, FIX_0_382683433); /* c6 */
  56.161 +    z2 = MULTIPLY(tmp10, FIX_0_541196100) + z5; /* c2-c6 */
  56.162 +    z4 = MULTIPLY(tmp12, FIX_1_306562965) + z5; /* c2+c6 */
  56.163 +    z3 = MULTIPLY(tmp11, FIX_0_707106781); /* c4 */
  56.164 +
  56.165 +    z11 = tmp7 + z3;		/* phase 5 */
  56.166 +    z13 = tmp7 - z3;
  56.167 +
  56.168 +    dataptr[5] = z13 + z2;	/* phase 6 */
  56.169 +    dataptr[3] = z13 - z2;
  56.170 +    dataptr[1] = z11 + z4;
  56.171 +    dataptr[7] = z11 - z4;
  56.172 +
  56.173 +    dataptr += DCTSIZE;		/* advance pointer to next row */
  56.174 +  }
  56.175 +
  56.176 +  /* Pass 2: process columns. */
  56.177 +
  56.178 +  dataptr = data;
  56.179 +  for (ctr = DCTSIZE-1; ctr >= 0; ctr--) {
  56.180 +    tmp0 = dataptr[DCTSIZE*0] + dataptr[DCTSIZE*7];
  56.181 +    tmp7 = dataptr[DCTSIZE*0] - dataptr[DCTSIZE*7];
  56.182 +    tmp1 = dataptr[DCTSIZE*1] + dataptr[DCTSIZE*6];
  56.183 +    tmp6 = dataptr[DCTSIZE*1] - dataptr[DCTSIZE*6];
  56.184 +    tmp2 = dataptr[DCTSIZE*2] + dataptr[DCTSIZE*5];
  56.185 +    tmp5 = dataptr[DCTSIZE*2] - dataptr[DCTSIZE*5];
  56.186 +    tmp3 = dataptr[DCTSIZE*3] + dataptr[DCTSIZE*4];
  56.187 +    tmp4 = dataptr[DCTSIZE*3] - dataptr[DCTSIZE*4];
  56.188 +    
  56.189 +    /* Even part */
  56.190 +    
  56.191 +    tmp10 = tmp0 + tmp3;	/* phase 2 */
  56.192 +    tmp13 = tmp0 - tmp3;
  56.193 +    tmp11 = tmp1 + tmp2;
  56.194 +    tmp12 = tmp1 - tmp2;
  56.195 +    
  56.196 +    dataptr[DCTSIZE*0] = tmp10 + tmp11; /* phase 3 */
  56.197 +    dataptr[DCTSIZE*4] = tmp10 - tmp11;
  56.198 +    
  56.199 +    z1 = MULTIPLY(tmp12 + tmp13, FIX_0_707106781); /* c4 */
  56.200 +    dataptr[DCTSIZE*2] = tmp13 + z1; /* phase 5 */
  56.201 +    dataptr[DCTSIZE*6] = tmp13 - z1;
  56.202 +    
  56.203 +    /* Odd part */
  56.204 +
  56.205 +    tmp10 = tmp4 + tmp5;	/* phase 2 */
  56.206 +    tmp11 = tmp5 + tmp6;
  56.207 +    tmp12 = tmp6 + tmp7;
  56.208 +
  56.209 +    /* The rotator is modified from fig 4-8 to avoid extra negations. */
  56.210 +    z5 = MULTIPLY(tmp10 - tmp12, FIX_0_382683433); /* c6 */
  56.211 +    z2 = MULTIPLY(tmp10, FIX_0_541196100) + z5; /* c2-c6 */
  56.212 +    z4 = MULTIPLY(tmp12, FIX_1_306562965) + z5; /* c2+c6 */
  56.213 +    z3 = MULTIPLY(tmp11, FIX_0_707106781); /* c4 */
  56.214 +
  56.215 +    z11 = tmp7 + z3;		/* phase 5 */
  56.216 +    z13 = tmp7 - z3;
  56.217 +
  56.218 +    dataptr[DCTSIZE*5] = z13 + z2; /* phase 6 */
  56.219 +    dataptr[DCTSIZE*3] = z13 - z2;
  56.220 +    dataptr[DCTSIZE*1] = z11 + z4;
  56.221 +    dataptr[DCTSIZE*7] = z11 - z4;
  56.222 +
  56.223 +    dataptr++;			/* advance pointer to next column */
  56.224 +  }
  56.225 +}
  56.226 +
  56.227 +#endif /* DCT_IFAST_SUPPORTED */
    57.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    57.2 +++ b/libs/libjpeg/jfdctint.c	Sat Sep 19 05:51:51 2015 +0300
    57.3 @@ -0,0 +1,283 @@
    57.4 +/*
    57.5 + * jfdctint.c
    57.6 + *
    57.7 + * Copyright (C) 1991-1996, Thomas G. Lane.
    57.8 + * This file is part of the Independent JPEG Group's software.
    57.9 + * For conditions of distribution and use, see the accompanying README file.
   57.10 + *
   57.11 + * This file contains a slow-but-accurate integer implementation of the
   57.12 + * forward DCT (Discrete Cosine Transform).
   57.13 + *
   57.14 + * A 2-D DCT can be done by 1-D DCT on each row followed by 1-D DCT
   57.15 + * on each column.  Direct algorithms are also available, but they are
   57.16 + * much more complex and seem not to be any faster when reduced to code.
   57.17 + *
   57.18 + * This implementation is based on an algorithm described in
   57.19 + *   C. Loeffler, A. Ligtenberg and G. Moschytz, "Practical Fast 1-D DCT
   57.20 + *   Algorithms with 11 Multiplications", Proc. Int'l. Conf. on Acoustics,
   57.21 + *   Speech, and Signal Processing 1989 (ICASSP '89), pp. 988-991.
   57.22 + * The primary algorithm described there uses 11 multiplies and 29 adds.
   57.23 + * We use their alternate method with 12 multiplies and 32 adds.
   57.24 + * The advantage of this method is that no data path contains more than one
   57.25 + * multiplication; this allows a very simple and accurate implementation in
   57.26 + * scaled fixed-point arithmetic, with a minimal number of shifts.
   57.27 + */
   57.28 +
   57.29 +#define JPEG_INTERNALS
   57.30 +#include "jinclude.h"
   57.31 +#include "jpeglib.h"
   57.32 +#include "jdct.h"		/* Private declarations for DCT subsystem */
   57.33 +
   57.34 +#ifdef DCT_ISLOW_SUPPORTED
   57.35 +
   57.36 +
   57.37 +/*
   57.38 + * This module is specialized to the case DCTSIZE = 8.
   57.39 + */
   57.40 +
   57.41 +#if DCTSIZE != 8
   57.42 +  Sorry, this code only copes with 8x8 DCTs. /* deliberate syntax err */
   57.43 +#endif
   57.44 +
   57.45 +
   57.46 +/*
   57.47 + * The poop on this scaling stuff is as follows:
   57.48 + *
   57.49 + * Each 1-D DCT step produces outputs which are a factor of sqrt(N)
   57.50 + * larger than the true DCT outputs.  The final outputs are therefore
   57.51 + * a factor of N larger than desired; since N=8 this can be cured by
   57.52 + * a simple right shift at the end of the algorithm.  The advantage of
   57.53 + * this arrangement is that we save two multiplications per 1-D DCT,
   57.54 + * because the y0 and y4 outputs need not be divided by sqrt(N).
   57.55 + * In the IJG code, this factor of 8 is removed by the quantization step
   57.56 + * (in jcdctmgr.c), NOT in this module.
   57.57 + *
   57.58 + * We have to do addition and subtraction of the integer inputs, which
   57.59 + * is no problem, and multiplication by fractional constants, which is
   57.60 + * a problem to do in integer arithmetic.  We multiply all the constants
   57.61 + * by CONST_SCALE and convert them to integer constants (thus retaining
   57.62 + * CONST_BITS bits of precision in the constants).  After doing a
   57.63 + * multiplication we have to divide the product by CONST_SCALE, with proper
   57.64 + * rounding, to produce the correct output.  This division can be done
   57.65 + * cheaply as a right shift of CONST_BITS bits.  We postpone shifting
   57.66 + * as long as possible so that partial sums can be added together with
   57.67 + * full fractional precision.
   57.68 + *
   57.69 + * The outputs of the first pass are scaled up by PASS1_BITS bits so that
   57.70 + * they are represented to better-than-integral precision.  These outputs
   57.71 + * require BITS_IN_JSAMPLE + PASS1_BITS + 3 bits; this fits in a 16-bit word
   57.72 + * with the recommended scaling.  (For 12-bit sample data, the intermediate
   57.73 + * array is INT32 anyway.)
   57.74 + *
   57.75 + * To avoid overflow of the 32-bit intermediate results in pass 2, we must
   57.76 + * have BITS_IN_JSAMPLE + CONST_BITS + PASS1_BITS <= 26.  Error analysis
   57.77 + * shows that the values given below are the most effective.
   57.78 + */
   57.79 +
   57.80 +#if BITS_IN_JSAMPLE == 8
   57.81 +#define CONST_BITS  13
   57.82 +#define PASS1_BITS  2
   57.83 +#else
   57.84 +#define CONST_BITS  13
   57.85 +#define PASS1_BITS  1		/* lose a little precision to avoid overflow */
   57.86 +#endif
   57.87 +
   57.88 +/* Some C compilers fail to reduce "FIX(constant)" at compile time, thus
   57.89 + * causing a lot of useless floating-point operations at run time.
   57.90 + * To get around this we use the following pre-calculated constants.
   57.91 + * If you change CONST_BITS you may want to add appropriate values.
   57.92 + * (With a reasonable C compiler, you can just rely on the FIX() macro...)
   57.93 + */
   57.94 +
   57.95 +#if CONST_BITS == 13
   57.96 +#define FIX_0_298631336  ((INT32)  2446)	/* FIX(0.298631336) */
   57.97 +#define FIX_0_390180644  ((INT32)  3196)	/* FIX(0.390180644) */
   57.98 +#define FIX_0_541196100  ((INT32)  4433)	/* FIX(0.541196100) */
   57.99 +#define FIX_0_765366865  ((INT32)  6270)	/* FIX(0.765366865) */
  57.100 +#define FIX_0_899976223  ((INT32)  7373)	/* FIX(0.899976223) */
  57.101 +#define FIX_1_175875602  ((INT32)  9633)	/* FIX(1.175875602) */
  57.102 +#define FIX_1_501321110  ((INT32)  12299)	/* FIX(1.501321110) */
  57.103 +#define FIX_1_847759065  ((INT32)  15137)	/* FIX(1.847759065) */
  57.104 +#define FIX_1_961570560  ((INT32)  16069)	/* FIX(1.961570560) */
  57.105 +#define FIX_2_053119869  ((INT32)  16819)	/* FIX(2.053119869) */
  57.106 +#define FIX_2_562915447  ((INT32)  20995)	/* FIX(2.562915447) */
  57.107 +#define FIX_3_072711026  ((INT32)  25172)	/* FIX(3.072711026) */
  57.108 +#else
  57.109 +#define FIX_0_298631336  FIX(0.298631336)
  57.110 +#define FIX_0_390180644  FIX(0.390180644)
  57.111 +#define FIX_0_541196100  FIX(0.541196100)
  57.112 +#define FIX_0_765366865  FIX(0.765366865)
  57.113 +#define FIX_0_899976223  FIX(0.899976223)
  57.114 +#define FIX_1_175875602  FIX(1.175875602)
  57.115 +#define FIX_1_501321110  FIX(1.501321110)
  57.116 +#define FIX_1_847759065  FIX(1.847759065)
  57.117 +#define FIX_1_961570560  FIX(1.961570560)
  57.118 +#define FIX_2_053119869  FIX(2.053119869)
  57.119 +#define FIX_2_562915447  FIX(2.562915447)
  57.120 +#define FIX_3_072711026  FIX(3.072711026)
  57.121 +#endif
  57.122 +
  57.123 +
  57.124 +/* Multiply an INT32 variable by an INT32 constant to yield an INT32 result.
  57.125 + * For 8-bit samples with the recommended scaling, all the variable
  57.126 + * and constant values involved are no more than 16 bits wide, so a
  57.127 + * 16x16->32 bit multiply can be used instead of a full 32x32 multiply.
  57.128 + * For 12-bit samples, a full 32-bit multiplication will be needed.
  57.129 + */
  57.130 +
  57.131 +#if BITS_IN_JSAMPLE == 8
  57.132 +#define MULTIPLY(var,const)  MULTIPLY16C16(var,const)
  57.133 +#else
  57.134 +#define MULTIPLY(var,const)  ((var) * (const))
  57.135 +#endif
  57.136 +
  57.137 +
  57.138 +/*
  57.139 + * Perform the forward DCT on one block of samples.
  57.140 + */
  57.141 +
  57.142 +GLOBAL(void)
  57.143 +jpeg_fdct_islow (DCTELEM * data)
  57.144 +{
  57.145 +  INT32 tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
  57.146 +  INT32 tmp10, tmp11, tmp12, tmp13;
  57.147 +  INT32 z1, z2, z3, z4, z5;
  57.148 +  DCTELEM *dataptr;
  57.149 +  int ctr;
  57.150 +  SHIFT_TEMPS
  57.151 +
  57.152 +  /* Pass 1: process rows. */
  57.153 +  /* Note results are scaled up by sqrt(8) compared to a true DCT; */
  57.154 +  /* furthermore, we scale the results by 2**PASS1_BITS. */
  57.155 +
  57.156 +  dataptr = data;
  57.157 +  for (ctr = DCTSIZE-1; ctr >= 0; ctr--) {
  57.158 +    tmp0 = dataptr[0] + dataptr[7];
  57.159 +    tmp7 = dataptr[0] - dataptr[7];
  57.160 +    tmp1 = dataptr[1] + dataptr[6];
  57.161 +    tmp6 = dataptr[1] - dataptr[6];
  57.162 +    tmp2 = dataptr[2] + dataptr[5];
  57.163 +    tmp5 = dataptr[2] - dataptr[5];
  57.164 +    tmp3 = dataptr[3] + dataptr[4];
  57.165 +    tmp4 = dataptr[3] - dataptr[4];
  57.166 +    
  57.167 +    /* Even part per LL&M figure 1 --- note that published figure is faulty;
  57.168 +     * rotator "sqrt(2)*c1" should be "sqrt(2)*c6".
  57.169 +     */
  57.170 +    
  57.171 +    tmp10 = tmp0 + tmp3;
  57.172 +    tmp13 = tmp0 - tmp3;
  57.173 +    tmp11 = tmp1 + tmp2;
  57.174 +    tmp12 = tmp1 - tmp2;
  57.175 +    
  57.176 +    dataptr[0] = (DCTELEM) ((tmp10 + tmp11) << PASS1_BITS);
  57.177 +    dataptr[4] = (DCTELEM) ((tmp10 - tmp11) << PASS1_BITS);
  57.178 +    
  57.179 +    z1 = MULTIPLY(tmp12 + tmp13, FIX_0_541196100);
  57.180 +    dataptr[2] = (DCTELEM) DESCALE(z1 + MULTIPLY(tmp13, FIX_0_765366865),
  57.181 +				   CONST_BITS-PASS1_BITS);
  57.182 +    dataptr[6] = (DCTELEM) DESCALE(z1 + MULTIPLY(tmp12, - FIX_1_847759065),
  57.183 +				   CONST_BITS-PASS1_BITS);
  57.184 +    
  57.185 +    /* Odd part per figure 8 --- note paper omits factor of sqrt(2).
  57.186 +     * cK represents cos(K*pi/16).
  57.187 +     * i0..i3 in the paper are tmp4..tmp7 here.
  57.188 +     */
  57.189 +    
  57.190 +    z1 = tmp4 + tmp7;
  57.191 +    z2 = tmp5 + tmp6;
  57.192 +    z3 = tmp4 + tmp6;
  57.193 +    z4 = tmp5 + tmp7;
  57.194 +    z5 = MULTIPLY(z3 + z4, FIX_1_175875602); /* sqrt(2) * c3 */
  57.195 +    
  57.196 +    tmp4 = MULTIPLY(tmp4, FIX_0_298631336); /* sqrt(2) * (-c1+c3+c5-c7) */
  57.197 +    tmp5 = MULTIPLY(tmp5, FIX_2_053119869); /* sqrt(2) * ( c1+c3-c5+c7) */
  57.198 +    tmp6 = MULTIPLY(tmp6, FIX_3_072711026); /* sqrt(2) * ( c1+c3+c5-c7) */
  57.199 +    tmp7 = MULTIPLY(tmp7, FIX_1_501321110); /* sqrt(2) * ( c1+c3-c5-c7) */
  57.200 +    z1 = MULTIPLY(z1, - FIX_0_899976223); /* sqrt(2) * (c7-c3) */
  57.201 +    z2 = MULTIPLY(z2, - FIX_2_562915447); /* sqrt(2) * (-c1-c3) */
  57.202 +    z3 = MULTIPLY(z3, - FIX_1_961570560); /* sqrt(2) * (-c3-c5) */
  57.203 +    z4 = MULTIPLY(z4, - FIX_0_390180644); /* sqrt(2) * (c5-c3) */
  57.204 +    
  57.205 +    z3 += z5;
  57.206 +    z4 += z5;
  57.207 +    
  57.208 +    dataptr[7] = (DCTELEM) DESCALE(tmp4 + z1 + z3, CONST_BITS-PASS1_BITS);
  57.209 +    dataptr[5] = (DCTELEM) DESCALE(tmp5 + z2 + z4, CONST_BITS-PASS1_BITS);
  57.210 +    dataptr[3] = (DCTELEM) DESCALE(tmp6 + z2 + z3, CONST_BITS-PASS1_BITS);
  57.211 +    dataptr[1] = (DCTELEM) DESCALE(tmp7 + z1 + z4, CONST_BITS-PASS1_BITS);
  57.212 +    
  57.213 +    dataptr += DCTSIZE;		/* advance pointer to next row */
  57.214 +  }
  57.215 +
  57.216 +  /* Pass 2: process columns.
  57.217 +   * We remove the PASS1_BITS scaling, but leave the results scaled up
  57.218 +   * by an overall factor of 8.
  57.219 +   */
  57.220 +
  57.221 +  dataptr = data;
  57.222 +  for (ctr = DCTSIZE-1; ctr >= 0; ctr--) {
  57.223 +    tmp0 = dataptr[DCTSIZE*0] + dataptr[DCTSIZE*7];
  57.224 +    tmp7 = dataptr[DCTSIZE*0] - dataptr[DCTSIZE*7];
  57.225 +    tmp1 = dataptr[DCTSIZE*1] + dataptr[DCTSIZE*6];
  57.226 +    tmp6 = dataptr[DCTSIZE*1] - dataptr[DCTSIZE*6];
  57.227 +    tmp2 = dataptr[DCTSIZE*2] + dataptr[DCTSIZE*5];
  57.228 +    tmp5 = dataptr[DCTSIZE*2] - dataptr[DCTSIZE*5];
  57.229 +    tmp3 = dataptr[DCTSIZE*3] + dataptr[DCTSIZE*4];
  57.230 +    tmp4 = dataptr[DCTSIZE*3] - dataptr[DCTSIZE*4];
  57.231 +    
  57.232 +    /* Even part per LL&M figure 1 --- note that published figure is faulty;
  57.233 +     * rotator "sqrt(2)*c1" should be "sqrt(2)*c6".
  57.234 +     */
  57.235 +    
  57.236 +    tmp10 = tmp0 + tmp3;
  57.237 +    tmp13 = tmp0 - tmp3;
  57.238 +    tmp11 = tmp1 + tmp2;
  57.239 +    tmp12 = tmp1 - tmp2;
  57.240 +    
  57.241 +    dataptr[DCTSIZE*0] = (DCTELEM) DESCALE(tmp10 + tmp11, PASS1_BITS);
  57.242 +    dataptr[DCTSIZE*4] = (DCTELEM) DESCALE(tmp10 - tmp11, PASS1_BITS);
  57.243 +    
  57.244 +    z1 = MULTIPLY(tmp12 + tmp13, FIX_0_541196100);
  57.245 +    dataptr[DCTSIZE*2] = (DCTELEM) DESCALE(z1 + MULTIPLY(tmp13, FIX_0_765366865),
  57.246 +					   CONST_BITS+PASS1_BITS);
  57.247 +    dataptr[DCTSIZE*6] = (DCTELEM) DESCALE(z1 + MULTIPLY(tmp12, - FIX_1_847759065),
  57.248 +					   CONST_BITS+PASS1_BITS);
  57.249 +    
  57.250 +    /* Odd part per figure 8 --- note paper omits factor of sqrt(2).
  57.251 +     * cK represents cos(K*pi/16).
  57.252 +     * i0..i3 in the paper are tmp4..tmp7 here.
  57.253 +     */
  57.254 +    
  57.255 +    z1 = tmp4 + tmp7;
  57.256 +    z2 = tmp5 + tmp6;
  57.257 +    z3 = tmp4 + tmp6;
  57.258 +    z4 = tmp5 + tmp7;
  57.259 +    z5 = MULTIPLY(z3 + z4, FIX_1_175875602); /* sqrt(2) * c3 */
  57.260 +    
  57.261 +    tmp4 = MULTIPLY(tmp4, FIX_0_298631336); /* sqrt(2) * (-c1+c3+c5-c7) */
  57.262 +    tmp5 = MULTIPLY(tmp5, FIX_2_053119869); /* sqrt(2) * ( c1+c3-c5+c7) */
  57.263 +    tmp6 = MULTIPLY(tmp6, FIX_3_072711026); /* sqrt(2) * ( c1+c3+c5-c7) */
  57.264 +    tmp7 = MULTIPLY(tmp7, FIX_1_501321110); /* sqrt(2) * ( c1+c3-c5-c7) */
  57.265 +    z1 = MULTIPLY(z1, - FIX_0_899976223); /* sqrt(2) * (c7-c3) */
  57.266 +    z2 = MULTIPLY(z2, - FIX_2_562915447); /* sqrt(2) * (-c1-c3) */
  57.267 +    z3 = MULTIPLY(z3, - FIX_1_961570560); /* sqrt(2) * (-c3-c5) */
  57.268 +    z4 = MULTIPLY(z4, - FIX_0_390180644); /* sqrt(2) * (c5-c3) */
  57.269 +    
  57.270 +    z3 += z5;
  57.271 +    z4 += z5;
  57.272 +    
  57.273 +    dataptr[DCTSIZE*7] = (DCTELEM) DESCALE(tmp4 + z1 + z3,
  57.274 +					   CONST_BITS+PASS1_BITS);
  57.275 +    dataptr[DCTSIZE*5] = (DCTELEM) DESCALE(tmp5 + z2 + z4,
  57.276 +					   CONST_BITS+PASS1_BITS);
  57.277 +    dataptr[DCTSIZE*3] = (DCTELEM) DESCALE(tmp6 + z2 + z3,
  57.278 +					   CONST_BITS+PASS1_BITS);
  57.279 +    dataptr[DCTSIZE*1] = (DCTELEM) DESCALE(tmp7 + z1 + z4,
  57.280 +					   CONST_BITS+PASS1_BITS);
  57.281 +    
  57.282 +    dataptr++;			/* advance pointer to next column */
  57.283 +  }
  57.284 +}
  57.285 +
  57.286 +#endif /* DCT_ISLOW_SUPPORTED */
    58.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    58.2 +++ b/libs/libjpeg/jidctflt.c	Sat Sep 19 05:51:51 2015 +0300
    58.3 @@ -0,0 +1,242 @@
    58.4 +/*
    58.5 + * jidctflt.c
    58.6 + *
    58.7 + * Copyright (C) 1994-1998, Thomas G. Lane.
    58.8 + * This file is part of the Independent JPEG Group's software.
    58.9 + * For conditions of distribution and use, see the accompanying README file.
   58.10 + *
   58.11 + * This file contains a floating-point implementation of the
   58.12 + * inverse DCT (Discrete Cosine Transform).  In the IJG code, this routine
   58.13 + * must also perform dequantization of the input coefficients.
   58.14 + *
   58.15 + * This implementation should be more accurate than either of the integer
   58.16 + * IDCT implementations.  However, it may not give the same results on all
   58.17 + * machines because of differences in roundoff behavior.  Speed will depend
   58.18 + * on the hardware's floating point capacity.
   58.19 + *
   58.20 + * A 2-D IDCT can be done by 1-D IDCT on each column followed by 1-D IDCT
   58.21 + * on each row (or vice versa, but it's more convenient to emit a row at
   58.22 + * a time).  Direct algorithms are also available, but they are much more
   58.23 + * complex and seem not to be any faster when reduced to code.
   58.24 + *
   58.25 + * This implementation is based on Arai, Agui, and Nakajima's algorithm for
   58.26 + * scaled DCT.  Their original paper (Trans. IEICE E-71(11):1095) is in
   58.27 + * Japanese, but the algorithm is described in the Pennebaker & Mitchell
   58.28 + * JPEG textbook (see REFERENCES section in file README).  The following code
   58.29 + * is based directly on figure 4-8 in P&M.
   58.30 + * While an 8-point DCT cannot be done in less than 11 multiplies, it is
   58.31 + * possible to arrange the computation so that many of the multiplies are
   58.32 + * simple scalings of the final outputs.  These multiplies can then be
   58.33 + * folded into the multiplications or divisions by the JPEG quantization
   58.34 + * table entries.  The AA&N method leaves only 5 multiplies and 29 adds
   58.35 + * to be done in the DCT itself.
   58.36 + * The primary disadvantage of this method is that with a fixed-point
   58.37 + * implementation, accuracy is lost due to imprecise representation of the
   58.38 + * scaled quantization values.  However, that problem does not arise if
   58.39 + * we use floating point arithmetic.
   58.40 + */
   58.41 +
   58.42 +#define JPEG_INTERNALS
   58.43 +#include "jinclude.h"
   58.44 +#include "jpeglib.h"
   58.45 +#include "jdct.h"		/* Private declarations for DCT subsystem */
   58.46 +
   58.47 +#ifdef DCT_FLOAT_SUPPORTED
   58.48 +
   58.49 +
   58.50 +/*
   58.51 + * This module is specialized to the case DCTSIZE = 8.
   58.52 + */
   58.53 +
   58.54 +#if DCTSIZE != 8
   58.55 +  Sorry, this code only copes with 8x8 DCTs. /* deliberate syntax err */
   58.56 +#endif
   58.57 +
   58.58 +
   58.59 +/* Dequantize a coefficient by multiplying it by the multiplier-table
   58.60 + * entry; produce a float result.
   58.61 + */
   58.62 +
   58.63 +#define DEQUANTIZE(coef,quantval)  (((FAST_FLOAT) (coef)) * (quantval))
   58.64 +
   58.65 +
   58.66 +/*
   58.67 + * Perform dequantization and inverse DCT on one block of coefficients.
   58.68 + */
   58.69 +
   58.70 +GLOBAL(void)
   58.71 +jpeg_idct_float (j_decompress_ptr cinfo, jpeg_component_info * compptr,
   58.72 +		 JCOEFPTR coef_block,
   58.73 +		 JSAMPARRAY output_buf, JDIMENSION output_col)
   58.74 +{
   58.75 +  FAST_FLOAT tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
   58.76 +  FAST_FLOAT tmp10, tmp11, tmp12, tmp13;
   58.77 +  FAST_FLOAT z5, z10, z11, z12, z13;
   58.78 +  JCOEFPTR inptr;
   58.79 +  FLOAT_MULT_TYPE * quantptr;
   58.80 +  FAST_FLOAT * wsptr;
   58.81 +  JSAMPROW outptr;
   58.82 +  JSAMPLE *range_limit = IDCT_range_limit(cinfo);
   58.83 +  int ctr;
   58.84 +  FAST_FLOAT workspace[DCTSIZE2]; /* buffers data between passes */
   58.85 +  SHIFT_TEMPS
   58.86 +
   58.87 +  /* Pass 1: process columns from input, store into work array. */
   58.88 +
   58.89 +  inptr = coef_block;
   58.90 +  quantptr = (FLOAT_MULT_TYPE *) compptr->dct_table;
   58.91 +  wsptr = workspace;
   58.92 +  for (ctr = DCTSIZE; ctr > 0; ctr--) {
   58.93 +    /* Due to quantization, we will usually find that many of the input
   58.94 +     * coefficients are zero, especially the AC terms.  We can exploit this
   58.95 +     * by short-circuiting the IDCT calculation for any column in which all
   58.96 +     * the AC terms are zero.  In that case each output is equal to the
   58.97 +     * DC coefficient (with scale factor as needed).
   58.98 +     * With typical images and quantization tables, half or more of the
   58.99 +     * column DCT calculations can be simplified this way.
  58.100 +     */
  58.101 +    
  58.102 +    if (inptr[DCTSIZE*1] == 0 && inptr[DCTSIZE*2] == 0 &&
  58.103 +	inptr[DCTSIZE*3] == 0 && inptr[DCTSIZE*4] == 0 &&
  58.104 +	inptr[DCTSIZE*5] == 0 && inptr[DCTSIZE*6] == 0 &&
  58.105 +	inptr[DCTSIZE*7] == 0) {
  58.106 +      /* AC terms all zero */
  58.107 +      FAST_FLOAT dcval = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
  58.108 +      
  58.109 +      wsptr[DCTSIZE*0] = dcval;
  58.110 +      wsptr[DCTSIZE*1] = dcval;
  58.111 +      wsptr[DCTSIZE*2] = dcval;
  58.112 +      wsptr[DCTSIZE*3] = dcval;
  58.113 +      wsptr[DCTSIZE*4] = dcval;
  58.114 +      wsptr[DCTSIZE*5] = dcval;
  58.115 +      wsptr[DCTSIZE*6] = dcval;
  58.116 +      wsptr[DCTSIZE*7] = dcval;
  58.117 +      
  58.118 +      inptr++;			/* advance pointers to next column */
  58.119 +      quantptr++;
  58.120 +      wsptr++;
  58.121 +      continue;
  58.122 +    }
  58.123 +    
  58.124 +    /* Even part */
  58.125 +
  58.126 +    tmp0 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
  58.127 +    tmp1 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]);
  58.128 +    tmp2 = DEQUANTIZE(inptr[DCTSIZE*4], quantptr[DCTSIZE*4]);
  58.129 +    tmp3 = DEQUANTIZE(inptr[DCTSIZE*6], quantptr[DCTSIZE*6]);
  58.130 +
  58.131 +    tmp10 = tmp0 + tmp2;	/* phase 3 */
  58.132 +    tmp11 = tmp0 - tmp2;
  58.133 +
  58.134 +    tmp13 = tmp1 + tmp3;	/* phases 5-3 */
  58.135 +    tmp12 = (tmp1 - tmp3) * ((FAST_FLOAT) 1.414213562) - tmp13; /* 2*c4 */
  58.136 +
  58.137 +    tmp0 = tmp10 + tmp13;	/* phase 2 */
  58.138 +    tmp3 = tmp10 - tmp13;
  58.139 +    tmp1 = tmp11 + tmp12;
  58.140 +    tmp2 = tmp11 - tmp12;
  58.141 +    
  58.142 +    /* Odd part */
  58.143 +
  58.144 +    tmp4 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]);
  58.145 +    tmp5 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]);
  58.146 +    tmp6 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]);
  58.147 +    tmp7 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]);
  58.148 +
  58.149 +    z13 = tmp6 + tmp5;		/* phase 6 */
  58.150 +    z10 = tmp6 - tmp5;
  58.151 +    z11 = tmp4 + tmp7;
  58.152 +    z12 = tmp4 - tmp7;
  58.153 +
  58.154 +    tmp7 = z11 + z13;		/* phase 5 */
  58.155 +    tmp11 = (z11 - z13) * ((FAST_FLOAT) 1.414213562); /* 2*c4 */
  58.156 +
  58.157 +    z5 = (z10 + z12) * ((FAST_FLOAT) 1.847759065); /* 2*c2 */
  58.158 +    tmp10 = ((FAST_FLOAT) 1.082392200) * z12 - z5; /* 2*(c2-c6) */
  58.159 +    tmp12 = ((FAST_FLOAT) -2.613125930) * z10 + z5; /* -2*(c2+c6) */
  58.160 +
  58.161 +    tmp6 = tmp12 - tmp7;	/* phase 2 */
  58.162 +    tmp5 = tmp11 - tmp6;
  58.163 +    tmp4 = tmp10 + tmp5;
  58.164 +
  58.165 +    wsptr[DCTSIZE*0] = tmp0 + tmp7;
  58.166 +    wsptr[DCTSIZE*7] = tmp0 - tmp7;
  58.167 +    wsptr[DCTSIZE*1] = tmp1 + tmp6;
  58.168 +    wsptr[DCTSIZE*6] = tmp1 - tmp6;
  58.169 +    wsptr[DCTSIZE*2] = tmp2 + tmp5;
  58.170 +    wsptr[DCTSIZE*5] = tmp2 - tmp5;
  58.171 +    wsptr[DCTSIZE*4] = tmp3 + tmp4;
  58.172 +    wsptr[DCTSIZE*3] = tmp3 - tmp4;
  58.173 +
  58.174 +    inptr++;			/* advance pointers to next column */
  58.175 +    quantptr++;
  58.176 +    wsptr++;
  58.177 +  }
  58.178 +  
  58.179 +  /* Pass 2: process rows from work array, store into output array. */
  58.180 +  /* Note that we must descale the results by a factor of 8 == 2**3. */
  58.181 +
  58.182 +  wsptr = workspace;
  58.183 +  for (ctr = 0; ctr < DCTSIZE; ctr++) {
  58.184 +    outptr = output_buf[ctr] + output_col;
  58.185 +    /* Rows of zeroes can be exploited in the same way as we did with columns.
  58.186 +     * However, the column calculation has created many nonzero AC terms, so
  58.187 +     * the simplification applies less often (typically 5% to 10% of the time).
  58.188 +     * And testing floats for zero is relatively expensive, so we don't bother.
  58.189 +     */
  58.190 +    
  58.191 +    /* Even part */
  58.192 +
  58.193 +    tmp10 = wsptr[0] + wsptr[4];
  58.194 +    tmp11 = wsptr[0] - wsptr[4];
  58.195 +
  58.196 +    tmp13 = wsptr[2] + wsptr[6];
  58.197 +    tmp12 = (wsptr[2] - wsptr[6]) * ((FAST_FLOAT) 1.414213562) - tmp13;
  58.198 +
  58.199 +    tmp0 = tmp10 + tmp13;
  58.200 +    tmp3 = tmp10 - tmp13;
  58.201 +    tmp1 = tmp11 + tmp12;
  58.202 +    tmp2 = tmp11 - tmp12;
  58.203 +
  58.204 +    /* Odd part */
  58.205 +
  58.206 +    z13 = wsptr[5] + wsptr[3];
  58.207 +    z10 = wsptr[5] - wsptr[3];
  58.208 +    z11 = wsptr[1] + wsptr[7];
  58.209 +    z12 = wsptr[1] - wsptr[7];
  58.210 +
  58.211 +    tmp7 = z11 + z13;
  58.212 +    tmp11 = (z11 - z13) * ((FAST_FLOAT) 1.414213562);
  58.213 +
  58.214 +    z5 = (z10 + z12) * ((FAST_FLOAT) 1.847759065); /* 2*c2 */
  58.215 +    tmp10 = ((FAST_FLOAT) 1.082392200) * z12 - z5; /* 2*(c2-c6) */
  58.216 +    tmp12 = ((FAST_FLOAT) -2.613125930) * z10 + z5; /* -2*(c2+c6) */
  58.217 +
  58.218 +    tmp6 = tmp12 - tmp7;
  58.219 +    tmp5 = tmp11 - tmp6;
  58.220 +    tmp4 = tmp10 + tmp5;
  58.221 +
  58.222 +    /* Final output stage: scale down by a factor of 8 and range-limit */
  58.223 +
  58.224 +    outptr[0] = range_limit[(int) DESCALE((INT32) (tmp0 + tmp7), 3)
  58.225 +			    & RANGE_MASK];
  58.226 +    outptr[7] = range_limit[(int) DESCALE((INT32) (tmp0 - tmp7), 3)
  58.227 +			    & RANGE_MASK];
  58.228 +    outptr[1] = range_limit[(int) DESCALE((INT32) (tmp1 + tmp6), 3)
  58.229 +			    & RANGE_MASK];
  58.230 +    outptr[6] = range_limit[(int) DESCALE((INT32) (tmp1 - tmp6), 3)
  58.231 +			    & RANGE_MASK];
  58.232 +    outptr[2] = range_limit[(int) DESCALE((INT32) (tmp2 + tmp5), 3)
  58.233 +			    & RANGE_MASK];
  58.234 +    outptr[5] = range_limit[(int) DESCALE((INT32) (tmp2 - tmp5), 3)
  58.235 +			    & RANGE_MASK];
  58.236 +    outptr[4] = range_limit[(int) DESCALE((INT32) (tmp3 + tmp4), 3)
  58.237 +			    & RANGE_MASK];
  58.238 +    outptr[3] = range_limit[(int) DESCALE((INT32) (tmp3 - tmp4), 3)
  58.239 +			    & RANGE_MASK];
  58.240 +    
  58.241 +    wsptr += DCTSIZE;		/* advance pointer to next row */
  58.242 +  }
  58.243 +}
  58.244 +
  58.245 +#endif /* DCT_FLOAT_SUPPORTED */
    59.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    59.2 +++ b/libs/libjpeg/jidctfst.c	Sat Sep 19 05:51:51 2015 +0300
    59.3 @@ -0,0 +1,368 @@
    59.4 +/*
    59.5 + * jidctfst.c
    59.6 + *
    59.7 + * Copyright (C) 1994-1998, Thomas G. Lane.
    59.8 + * This file is part of the Independent JPEG Group's software.
    59.9 + * For conditions of distribution and use, see the accompanying README file.
   59.10 + *
   59.11 + * This file contains a fast, not so accurate integer implementation of the
   59.12 + * inverse DCT (Discrete Cosine Transform).  In the IJG code, this routine
   59.13 + * must also perform dequantization of the input coefficients.
   59.14 + *
   59.15 + * A 2-D IDCT can be done by 1-D IDCT on each column followed by 1-D IDCT
   59.16 + * on each row (or vice versa, but it's more convenient to emit a row at
   59.17 + * a time).  Direct algorithms are also available, but they are much more
   59.18 + * complex and seem not to be any faster when reduced to code.
   59.19 + *
   59.20 + * This implementation is based on Arai, Agui, and Nakajima's algorithm for
   59.21 + * scaled DCT.  Their original paper (Trans. IEICE E-71(11):1095) is in
   59.22 + * Japanese, but the algorithm is described in the Pennebaker & Mitchell
   59.23 + * JPEG textbook (see REFERENCES section in file README).  The following code
   59.24 + * is based directly on figure 4-8 in P&M.
   59.25 + * While an 8-point DCT cannot be done in less than 11 multiplies, it is
   59.26 + * possible to arrange the computation so that many of the multiplies are
   59.27 + * simple scalings of the final outputs.  These multiplies can then be
   59.28 + * folded into the multiplications or divisions by the JPEG quantization
   59.29 + * table entries.  The AA&N method leaves only 5 multiplies and 29 adds
   59.30 + * to be done in the DCT itself.
   59.31 + * The primary disadvantage of this method is that with fixed-point math,
   59.32 + * accuracy is lost due to imprecise representation of the scaled
   59.33 + * quantization values.  The smaller the quantization table entry, the less
   59.34 + * precise the scaled value, so this implementation does worse with high-
   59.35 + * quality-setting files than with low-quality ones.
   59.36 + */
   59.37 +
   59.38 +#define JPEG_INTERNALS
   59.39 +#include "jinclude.h"
   59.40 +#include "jpeglib.h"
   59.41 +#include "jdct.h"		/* Private declarations for DCT subsystem */
   59.42 +
   59.43 +#ifdef DCT_IFAST_SUPPORTED
   59.44 +
   59.45 +
   59.46 +/*
   59.47 + * This module is specialized to the case DCTSIZE = 8.
   59.48 + */
   59.49 +
   59.50 +#if DCTSIZE != 8
   59.51 +  Sorry, this code only copes with 8x8 DCTs. /* deliberate syntax err */
   59.52 +#endif
   59.53 +
   59.54 +
   59.55 +/* Scaling decisions are generally the same as in the LL&M algorithm;
   59.56 + * see jidctint.c for more details.  However, we choose to descale
   59.57 + * (right shift) multiplication products as soon as they are formed,
   59.58 + * rather than carrying additional fractional bits into subsequent additions.
   59.59 + * This compromises accuracy slightly, but it lets us save a few shifts.
   59.60 + * More importantly, 16-bit arithmetic is then adequate (for 8-bit samples)
   59.61 + * everywhere except in the multiplications proper; this saves a good deal
   59.62 + * of work on 16-bit-int machines.
   59.63 + *
   59.64 + * The dequantized coefficients are not integers because the AA&N scaling
   59.65 + * factors have been incorporated.  We represent them scaled up by PASS1_BITS,
   59.66 + * so that the first and second IDCT rounds have the same input scaling.
   59.67 + * For 8-bit JSAMPLEs, we choose IFAST_SCALE_BITS = PASS1_BITS so as to
   59.68 + * avoid a descaling shift; this compromises accuracy rather drastically
   59.69 + * for small quantization table entries, but it saves a lot of shifts.
   59.70 + * For 12-bit JSAMPLEs, there's no hope of using 16x16 multiplies anyway,
   59.71 + * so we use a much larger scaling factor to preserve accuracy.
   59.72 + *
   59.73 + * A final compromise is to represent the multiplicative constants to only
   59.74 + * 8 fractional bits, rather than 13.  This saves some shifting work on some
   59.75 + * machines, and may also reduce the cost of multiplication (since there
   59.76 + * are fewer one-bits in the constants).
   59.77 + */
   59.78 +
   59.79 +#if BITS_IN_JSAMPLE == 8
   59.80 +#define CONST_BITS  8
   59.81 +#define PASS1_BITS  2
   59.82 +#else
   59.83 +#define CONST_BITS  8
   59.84 +#define PASS1_BITS  1		/* lose a little precision to avoid overflow */
   59.85 +#endif
   59.86 +
   59.87 +/* Some C compilers fail to reduce "FIX(constant)" at compile time, thus
   59.88 + * causing a lot of useless floating-point operations at run time.
   59.89 + * To get around this we use the following pre-calculated constants.
   59.90 + * If you change CONST_BITS you may want to add appropriate values.
   59.91 + * (With a reasonable C compiler, you can just rely on the FIX() macro...)
   59.92 + */
   59.93 +
   59.94 +#if CONST_BITS == 8
   59.95 +#define FIX_1_082392200  ((INT32)  277)		/* FIX(1.082392200) */
   59.96 +#define FIX_1_414213562  ((INT32)  362)		/* FIX(1.414213562) */
   59.97 +#define FIX_1_847759065  ((INT32)  473)		/* FIX(1.847759065) */
   59.98 +#define FIX_2_613125930  ((INT32)  669)		/* FIX(2.613125930) */
   59.99 +#else
  59.100 +#define FIX_1_082392200  FIX(1.082392200)
  59.101 +#define FIX_1_414213562  FIX(1.414213562)
  59.102 +#define FIX_1_847759065  FIX(1.847759065)
  59.103 +#define FIX_2_613125930  FIX(2.613125930)
  59.104 +#endif
  59.105 +
  59.106 +
  59.107 +/* We can gain a little more speed, with a further compromise in accuracy,
  59.108 + * by omitting the addition in a descaling shift.  This yields an incorrectly
  59.109 + * rounded result half the time...
  59.110 + */
  59.111 +
  59.112 +#ifndef USE_ACCURATE_ROUNDING
  59.113 +#undef DESCALE
  59.114 +#define DESCALE(x,n)  RIGHT_SHIFT(x, n)
  59.115 +#endif
  59.116 +
  59.117 +
  59.118 +/* Multiply a DCTELEM variable by an INT32 constant, and immediately
  59.119 + * descale to yield a DCTELEM result.
  59.120 + */
  59.121 +
  59.122 +#define MULTIPLY(var,const)  ((DCTELEM) DESCALE((var) * (const), CONST_BITS))
  59.123 +
  59.124 +
  59.125 +/* Dequantize a coefficient by multiplying it by the multiplier-table
  59.126 + * entry; produce a DCTELEM result.  For 8-bit data a 16x16->16
  59.127 + * multiplication will do.  For 12-bit data, the multiplier table is
  59.128 + * declared INT32, so a 32-bit multiply will be used.
  59.129 + */
  59.130 +
  59.131 +#if BITS_IN_JSAMPLE == 8
  59.132 +#define DEQUANTIZE(coef,quantval)  (((IFAST_MULT_TYPE) (coef)) * (quantval))
  59.133 +#else
  59.134 +#define DEQUANTIZE(coef,quantval)  \
  59.135 +	DESCALE((coef)*(quantval), IFAST_SCALE_BITS-PASS1_BITS)
  59.136 +#endif
  59.137 +
  59.138 +
  59.139 +/* Like DESCALE, but applies to a DCTELEM and produces an int.
  59.140 + * We assume that int right shift is unsigned if INT32 right shift is.
  59.141 + */
  59.142 +
  59.143 +#ifdef RIGHT_SHIFT_IS_UNSIGNED
  59.144 +#define ISHIFT_TEMPS	DCTELEM ishift_temp;
  59.145 +#if BITS_IN_JSAMPLE == 8
  59.146 +#define DCTELEMBITS  16		/* DCTELEM may be 16 or 32 bits */
  59.147 +#else
  59.148 +#define DCTELEMBITS  32		/* DCTELEM must be 32 bits */
  59.149 +#endif
  59.150 +#define IRIGHT_SHIFT(x,shft)  \
  59.151 +    ((ishift_temp = (x)) < 0 ? \
  59.152 +     (ishift_temp >> (shft)) | ((~((DCTELEM) 0)) << (DCTELEMBITS-(shft))) : \
  59.153 +     (ishift_temp >> (shft)))
  59.154 +#else
  59.155 +#define ISHIFT_TEMPS
  59.156 +#define IRIGHT_SHIFT(x,shft)	((x) >> (shft))
  59.157 +#endif
  59.158 +
  59.159 +#ifdef USE_ACCURATE_ROUNDING
  59.160 +#define IDESCALE(x,n)  ((int) IRIGHT_SHIFT((x) + (1 << ((n)-1)), n))
  59.161 +#else
  59.162 +#define IDESCALE(x,n)  ((int) IRIGHT_SHIFT(x, n))
  59.163 +#endif
  59.164 +
  59.165 +
  59.166 +/*
  59.167 + * Perform dequantization and inverse DCT on one block of coefficients.
  59.168 + */
  59.169 +
  59.170 +GLOBAL(void)
  59.171 +jpeg_idct_ifast (j_decompress_ptr cinfo, jpeg_component_info * compptr,
  59.172 +		 JCOEFPTR coef_block,
  59.173 +		 JSAMPARRAY output_buf, JDIMENSION output_col)
  59.174 +{
  59.175 +  DCTELEM tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
  59.176 +  DCTELEM tmp10, tmp11, tmp12, tmp13;
  59.177 +  DCTELEM z5, z10, z11, z12, z13;
  59.178 +  JCOEFPTR inptr;
  59.179 +  IFAST_MULT_TYPE * quantptr;
  59.180 +  int * wsptr;
  59.181 +  JSAMPROW outptr;
  59.182 +  JSAMPLE *range_limit = IDCT_range_limit(cinfo);
  59.183 +  int ctr;
  59.184 +  int workspace[DCTSIZE2];	/* buffers data between passes */
  59.185 +  SHIFT_TEMPS			/* for DESCALE */
  59.186 +  ISHIFT_TEMPS			/* for IDESCALE */
  59.187 +
  59.188 +  /* Pass 1: process columns from input, store into work array. */
  59.189 +
  59.190 +  inptr = coef_block;
  59.191 +  quantptr = (IFAST_MULT_TYPE *) compptr->dct_table;
  59.192 +  wsptr = workspace;
  59.193 +  for (ctr = DCTSIZE; ctr > 0; ctr--) {
  59.194 +    /* Due to quantization, we will usually find that many of the input
  59.195 +     * coefficients are zero, especially the AC terms.  We can exploit this
  59.196 +     * by short-circuiting the IDCT calculation for any column in which all
  59.197 +     * the AC terms are zero.  In that case each output is equal to the
  59.198 +     * DC coefficient (with scale factor as needed).
  59.199 +     * With typical images and quantization tables, half or more of the
  59.200 +     * column DCT calculations can be simplified this way.
  59.201 +     */
  59.202 +    
  59.203 +    if (inptr[DCTSIZE*1] == 0 && inptr[DCTSIZE*2] == 0 &&
  59.204 +	inptr[DCTSIZE*3] == 0 && inptr[DCTSIZE*4] == 0 &&
  59.205 +	inptr[DCTSIZE*5] == 0 && inptr[DCTSIZE*6] == 0 &&
  59.206 +	inptr[DCTSIZE*7] == 0) {
  59.207 +      /* AC terms all zero */
  59.208 +      int dcval = (int) DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
  59.209 +
  59.210 +      wsptr[DCTSIZE*0] = dcval;
  59.211 +      wsptr[DCTSIZE*1] = dcval;
  59.212 +      wsptr[DCTSIZE*2] = dcval;
  59.213 +      wsptr[DCTSIZE*3] = dcval;
  59.214 +      wsptr[DCTSIZE*4] = dcval;
  59.215 +      wsptr[DCTSIZE*5] = dcval;
  59.216 +      wsptr[DCTSIZE*6] = dcval;
  59.217 +      wsptr[DCTSIZE*7] = dcval;
  59.218 +      
  59.219 +      inptr++;			/* advance pointers to next column */
  59.220 +      quantptr++;
  59.221 +      wsptr++;
  59.222 +      continue;
  59.223 +    }
  59.224 +    
  59.225 +    /* Even part */
  59.226 +
  59.227 +    tmp0 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
  59.228 +    tmp1 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]);
  59.229 +    tmp2 = DEQUANTIZE(inptr[DCTSIZE*4], quantptr[DCTSIZE*4]);
  59.230 +    tmp3 = DEQUANTIZE(inptr[DCTSIZE*6], quantptr[DCTSIZE*6]);
  59.231 +
  59.232 +    tmp10 = tmp0 + tmp2;	/* phase 3 */
  59.233 +    tmp11 = tmp0 - tmp2;
  59.234 +
  59.235 +    tmp13 = tmp1 + tmp3;	/* phases 5-3 */
  59.236 +    tmp12 = MULTIPLY(tmp1 - tmp3, FIX_1_414213562) - tmp13; /* 2*c4 */
  59.237 +
  59.238 +    tmp0 = tmp10 + tmp13;	/* phase 2 */
  59.239 +    tmp3 = tmp10 - tmp13;
  59.240 +    tmp1 = tmp11 + tmp12;
  59.241 +    tmp2 = tmp11 - tmp12;
  59.242 +    
  59.243 +    /* Odd part */
  59.244 +
  59.245 +    tmp4 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]);
  59.246 +    tmp5 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]);
  59.247 +    tmp6 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]);
  59.248 +    tmp7 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]);
  59.249 +
  59.250 +    z13 = tmp6 + tmp5;		/* phase 6 */
  59.251 +    z10 = tmp6 - tmp5;
  59.252 +    z11 = tmp4 + tmp7;
  59.253 +    z12 = tmp4 - tmp7;
  59.254 +
  59.255 +    tmp7 = z11 + z13;		/* phase 5 */
  59.256 +    tmp11 = MULTIPLY(z11 - z13, FIX_1_414213562); /* 2*c4 */
  59.257 +
  59.258 +    z5 = MULTIPLY(z10 + z12, FIX_1_847759065); /* 2*c2 */
  59.259 +    tmp10 = MULTIPLY(z12, FIX_1_082392200) - z5; /* 2*(c2-c6) */
  59.260 +    tmp12 = MULTIPLY(z10, - FIX_2_613125930) + z5; /* -2*(c2+c6) */
  59.261 +
  59.262 +    tmp6 = tmp12 - tmp7;	/* phase 2 */
  59.263 +    tmp5 = tmp11 - tmp6;
  59.264 +    tmp4 = tmp10 + tmp5;
  59.265 +
  59.266 +    wsptr[DCTSIZE*0] = (int) (tmp0 + tmp7);
  59.267 +    wsptr[DCTSIZE*7] = (int) (tmp0 - tmp7);
  59.268 +    wsptr[DCTSIZE*1] = (int) (tmp1 + tmp6);
  59.269 +    wsptr[DCTSIZE*6] = (int) (tmp1 - tmp6);
  59.270 +    wsptr[DCTSIZE*2] = (int) (tmp2 + tmp5);
  59.271 +    wsptr[DCTSIZE*5] = (int) (tmp2 - tmp5);
  59.272 +    wsptr[DCTSIZE*4] = (int) (tmp3 + tmp4);
  59.273 +    wsptr[DCTSIZE*3] = (int) (tmp3 - tmp4);
  59.274 +
  59.275 +    inptr++;			/* advance pointers to next column */
  59.276 +    quantptr++;
  59.277 +    wsptr++;
  59.278 +  }
  59.279 +  
  59.280 +  /* Pass 2: process rows from work array, store into output array. */
  59.281 +  /* Note that we must descale the results by a factor of 8 == 2**3, */
  59.282 +  /* and also undo the PASS1_BITS scaling. */
  59.283 +
  59.284 +  wsptr = workspace;
  59.285 +  for (ctr = 0; ctr < DCTSIZE; ctr++) {
  59.286 +    outptr = output_buf[ctr] + output_col;
  59.287 +    /* Rows of zeroes can be exploited in the same way as we did with columns.
  59.288 +     * However, the column calculation has created many nonzero AC terms, so
  59.289 +     * the simplification applies less often (typically 5% to 10% of the time).
  59.290 +     * On machines with very fast multiplication, it's possible that the
  59.291 +     * test takes more time than it's worth.  In that case this section
  59.292 +     * may be commented out.
  59.293 +     */
  59.294 +    
  59.295 +#ifndef NO_ZERO_ROW_TEST
  59.296 +    if (wsptr[1] == 0 && wsptr[2] == 0 && wsptr[3] == 0 && wsptr[4] == 0 &&
  59.297 +	wsptr[5] == 0 && wsptr[6] == 0 && wsptr[7] == 0) {
  59.298 +      /* AC terms all zero */
  59.299 +      JSAMPLE dcval = range_limit[IDESCALE(wsptr[0], PASS1_BITS+3)
  59.300 +				  & RANGE_MASK];
  59.301 +      
  59.302 +      outptr[0] = dcval;
  59.303 +      outptr[1] = dcval;
  59.304 +      outptr[2] = dcval;
  59.305 +      outptr[3] = dcval;
  59.306 +      outptr[4] = dcval;
  59.307 +      outptr[5] = dcval;
  59.308 +      outptr[6] = dcval;
  59.309 +      outptr[7] = dcval;
  59.310 +
  59.311 +      wsptr += DCTSIZE;		/* advance pointer to next row */
  59.312 +      continue;
  59.313 +    }
  59.314 +#endif
  59.315 +    
  59.316 +    /* Even part */
  59.317 +
  59.318 +    tmp10 = ((DCTELEM) wsptr[0] + (DCTELEM) wsptr[4]);
  59.319 +    tmp11 = ((DCTELEM) wsptr[0] - (DCTELEM) wsptr[4]);
  59.320 +
  59.321 +    tmp13 = ((DCTELEM) wsptr[2] + (DCTELEM) wsptr[6]);
  59.322 +    tmp12 = MULTIPLY((DCTELEM) wsptr[2] - (DCTELEM) wsptr[6], FIX_1_414213562)
  59.323 +	    - tmp13;
  59.324 +
  59.325 +    tmp0 = tmp10 + tmp13;
  59.326 +    tmp3 = tmp10 - tmp13;
  59.327 +    tmp1 = tmp11 + tmp12;
  59.328 +    tmp2 = tmp11 - tmp12;
  59.329 +
  59.330 +    /* Odd part */
  59.331 +
  59.332 +    z13 = (DCTELEM) wsptr[5] + (DCTELEM) wsptr[3];
  59.333 +    z10 = (DCTELEM) wsptr[5] - (DCTELEM) wsptr[3];
  59.334 +    z11 = (DCTELEM) wsptr[1] + (DCTELEM) wsptr[7];
  59.335 +    z12 = (DCTELEM) wsptr[1] - (DCTELEM) wsptr[7];
  59.336 +
  59.337 +    tmp7 = z11 + z13;		/* phase 5 */
  59.338 +    tmp11 = MULTIPLY(z11 - z13, FIX_1_414213562); /* 2*c4 */
  59.339 +
  59.340 +    z5 = MULTIPLY(z10 + z12, FIX_1_847759065); /* 2*c2 */
  59.341 +    tmp10 = MULTIPLY(z12, FIX_1_082392200) - z5; /* 2*(c2-c6) */
  59.342 +    tmp12 = MULTIPLY(z10, - FIX_2_613125930) + z5; /* -2*(c2+c6) */
  59.343 +
  59.344 +    tmp6 = tmp12 - tmp7;	/* phase 2 */
  59.345 +    tmp5 = tmp11 - tmp6;
  59.346 +    tmp4 = tmp10 + tmp5;
  59.347 +
  59.348 +    /* Final output stage: scale down by a factor of 8 and range-limit */
  59.349 +
  59.350 +    outptr[0] = range_limit[IDESCALE(tmp0 + tmp7, PASS1_BITS+3)
  59.351 +			    & RANGE_MASK];
  59.352 +    outptr[7] = range_limit[IDESCALE(tmp0 - tmp7, PASS1_BITS+3)
  59.353 +			    & RANGE_MASK];
  59.354 +    outptr[1] = range_limit[IDESCALE(tmp1 + tmp6, PASS1_BITS+3)
  59.355 +			    & RANGE_MASK];
  59.356 +    outptr[6] = range_limit[IDESCALE(tmp1 - tmp6, PASS1_BITS+3)
  59.357 +			    & RANGE_MASK];
  59.358 +    outptr[2] = range_limit[IDESCALE(tmp2 + tmp5, PASS1_BITS+3)
  59.359 +			    & RANGE_MASK];
  59.360 +    outptr[5] = range_limit[IDESCALE(tmp2 - tmp5, PASS1_BITS+3)
  59.361 +			    & RANGE_MASK];
  59.362 +    outptr[4] = range_limit[IDESCALE(tmp3 + tmp4, PASS1_BITS+3)
  59.363 +			    & RANGE_MASK];
  59.364 +    outptr[3] = range_limit[IDESCALE(tmp3 - tmp4, PASS1_BITS+3)
  59.365 +			    & RANGE_MASK];
  59.366 +
  59.367 +    wsptr += DCTSIZE;		/* advance pointer to next row */
  59.368 +  }
  59.369 +}
  59.370 +
  59.371 +#endif /* DCT_IFAST_SUPPORTED */
    60.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    60.2 +++ b/libs/libjpeg/jidctint.c	Sat Sep 19 05:51:51 2015 +0300
    60.3 @@ -0,0 +1,389 @@
    60.4 +/*
    60.5 + * jidctint.c
    60.6 + *
    60.7 + * Copyright (C) 1991-1998, Thomas G. Lane.
    60.8 + * This file is part of the Independent JPEG Group's software.
    60.9 + * For conditions of distribution and use, see the accompanying README file.
   60.10 + *
   60.11 + * This file contains a slow-but-accurate integer implementation of the
   60.12 + * inverse DCT (Discrete Cosine Transform).  In the IJG code, this routine
   60.13 + * must also perform dequantization of the input coefficients.
   60.14 + *
   60.15 + * A 2-D IDCT can be done by 1-D IDCT on each column followed by 1-D IDCT
   60.16 + * on each row (or vice versa, but it's more convenient to emit a row at
   60.17 + * a time).  Direct algorithms are also available, but they are much more
   60.18 + * complex and seem not to be any faster when reduced to code.
   60.19 + *
   60.20 + * This implementation is based on an algorithm described in
   60.21 + *   C. Loeffler, A. Ligtenberg and G. Moschytz, "Practical Fast 1-D DCT
   60.22 + *   Algorithms with 11 Multiplications", Proc. Int'l. Conf. on Acoustics,
   60.23 + *   Speech, and Signal Processing 1989 (ICASSP '89), pp. 988-991.
   60.24 + * The primary algorithm described there uses 11 multiplies and 29 adds.
   60.25 + * We use their alternate method with 12 multiplies and 32 adds.
   60.26 + * The advantage of this method is that no data path contains more than one
   60.27 + * multiplication; this allows a very simple and accurate implementation in
   60.28 + * scaled fixed-point arithmetic, with a minimal number of shifts.
   60.29 + */
   60.30 +
   60.31 +#define JPEG_INTERNALS
   60.32 +#include "jinclude.h"
   60.33 +#include "jpeglib.h"
   60.34 +#include "jdct.h"		/* Private declarations for DCT subsystem */
   60.35 +
   60.36 +#ifdef DCT_ISLOW_SUPPORTED
   60.37 +
   60.38 +
   60.39 +/*
   60.40 + * This module is specialized to the case DCTSIZE = 8.
   60.41 + */
   60.42 +
   60.43 +#if DCTSIZE != 8
   60.44 +  Sorry, this code only copes with 8x8 DCTs. /* deliberate syntax err */
   60.45 +#endif
   60.46 +
   60.47 +
   60.48 +/*
   60.49 + * The poop on this scaling stuff is as follows:
   60.50 + *
   60.51 + * Each 1-D IDCT step produces outputs which are a factor of sqrt(N)
   60.52 + * larger than the true IDCT outputs.  The final outputs are therefore
   60.53 + * a factor of N larger than desired; since N=8 this can be cured by
   60.54 + * a simple right shift at the end of the algorithm.  The advantage of
   60.55 + * this arrangement is that we save two multiplications per 1-D IDCT,
   60.56 + * because the y0 and y4 inputs need not be divided by sqrt(N).
   60.57 + *
   60.58 + * We have to do addition and subtraction of the integer inputs, which
   60.59 + * is no problem, and multiplication by fractional constants, which is
   60.60 + * a problem to do in integer arithmetic.  We multiply all the constants
   60.61 + * by CONST_SCALE and convert them to integer constants (thus retaining
   60.62 + * CONST_BITS bits of precision in the constants).  After doing a
   60.63 + * multiplication we have to divide the product by CONST_SCALE, with proper
   60.64 + * rounding, to produce the correct output.  This division can be done
   60.65 + * cheaply as a right shift of CONST_BITS bits.  We postpone shifting
   60.66 + * as long as possible so that partial sums can be added together with
   60.67 + * full fractional precision.
   60.68 + *
   60.69 + * The outputs of the first pass are scaled up by PASS1_BITS bits so that
   60.70 + * they are represented to better-than-integral precision.  These outputs
   60.71 + * require BITS_IN_JSAMPLE + PASS1_BITS + 3 bits; this fits in a 16-bit word
   60.72 + * with the recommended scaling.  (To scale up 12-bit sample data further, an
   60.73 + * intermediate INT32 array would be needed.)
   60.74 + *
   60.75 + * To avoid overflow of the 32-bit intermediate results in pass 2, we must
   60.76 + * have BITS_IN_JSAMPLE + CONST_BITS + PASS1_BITS <= 26.  Error analysis
   60.77 + * shows that the values given below are the most effective.
   60.78 + */
   60.79 +
   60.80 +#if BITS_IN_JSAMPLE == 8
   60.81 +#define CONST_BITS  13
   60.82 +#define PASS1_BITS  2
   60.83 +#else
   60.84 +#define CONST_BITS  13
   60.85 +#define PASS1_BITS  1		/* lose a little precision to avoid overflow */
   60.86 +#endif
   60.87 +
   60.88 +/* Some C compilers fail to reduce "FIX(constant)" at compile time, thus
   60.89 + * causing a lot of useless floating-point operations at run time.
   60.90 + * To get around this we use the following pre-calculated constants.
   60.91 + * If you change CONST_BITS you may want to add appropriate values.
   60.92 + * (With a reasonable C compiler, you can just rely on the FIX() macro...)
   60.93 + */
   60.94 +
   60.95 +#if CONST_BITS == 13
   60.96 +#define FIX_0_298631336  ((INT32)  2446)	/* FIX(0.298631336) */
   60.97 +#define FIX_0_390180644  ((INT32)  3196)	/* FIX(0.390180644) */
   60.98 +#define FIX_0_541196100  ((INT32)  4433)	/* FIX(0.541196100) */
   60.99 +#define FIX_0_765366865  ((INT32)  6270)	/* FIX(0.765366865) */
  60.100 +#define FIX_0_899976223  ((INT32)  7373)	/* FIX(0.899976223) */
  60.101 +#define FIX_1_175875602  ((INT32)  9633)	/* FIX(1.175875602) */
  60.102 +#define FIX_1_501321110  ((INT32)  12299)	/* FIX(1.501321110) */
  60.103 +#define FIX_1_847759065  ((INT32)  15137)	/* FIX(1.847759065) */
  60.104 +#define FIX_1_961570560  ((INT32)  16069)	/* FIX(1.961570560) */
  60.105 +#define FIX_2_053119869  ((INT32)  16819)	/* FIX(2.053119869) */
  60.106 +#define FIX_2_562915447  ((INT32)  20995)	/* FIX(2.562915447) */
  60.107 +#define FIX_3_072711026  ((INT32)  25172)	/* FIX(3.072711026) */
  60.108 +#else
  60.109 +#define FIX_0_298631336  FIX(0.298631336)
  60.110 +#define FIX_0_390180644  FIX(0.390180644)
  60.111 +#define FIX_0_541196100  FIX(0.541196100)
  60.112 +#define FIX_0_765366865  FIX(0.765366865)
  60.113 +#define FIX_0_899976223  FIX(0.899976223)
  60.114 +#define FIX_1_175875602  FIX(1.175875602)
  60.115 +#define FIX_1_501321110  FIX(1.501321110)
  60.116 +#define FIX_1_847759065  FIX(1.847759065)
  60.117 +#define FIX_1_961570560  FIX(1.961570560)
  60.118 +#define FIX_2_053119869  FIX(2.053119869)
  60.119 +#define FIX_2_562915447  FIX(2.562915447)
  60.120 +#define FIX_3_072711026  FIX(3.072711026)
  60.121 +#endif
  60.122 +
  60.123 +
  60.124 +/* Multiply an INT32 variable by an INT32 constant to yield an INT32 result.
  60.125 + * For 8-bit samples with the recommended scaling, all the variable
  60.126 + * and constant values involved are no more than 16 bits wide, so a
  60.127 + * 16x16->32 bit multiply can be used instead of a full 32x32 multiply.
  60.128 + * For 12-bit samples, a full 32-bit multiplication will be needed.
  60.129 + */
  60.130 +
  60.131 +#if BITS_IN_JSAMPLE == 8
  60.132 +#define MULTIPLY(var,const)  MULTIPLY16C16(var,const)
  60.133 +#else
  60.134 +#define MULTIPLY(var,const)  ((var) * (const))
  60.135 +#endif
  60.136 +
  60.137 +
  60.138 +/* Dequantize a coefficient by multiplying it by the multiplier-table
  60.139 + * entry; produce an int result.  In this module, both inputs and result
  60.140 + * are 16 bits or less, so either int or short multiply will work.
  60.141 + */
  60.142 +
  60.143 +#define DEQUANTIZE(coef,quantval)  (((ISLOW_MULT_TYPE) (coef)) * (quantval))
  60.144 +
  60.145 +
  60.146 +/*
  60.147 + * Perform dequantization and inverse DCT on one block of coefficients.
  60.148 + */
  60.149 +
  60.150 +GLOBAL(void)
  60.151 +jpeg_idct_islow (j_decompress_ptr cinfo, jpeg_component_info * compptr,
  60.152 +		 JCOEFPTR coef_block,
  60.153 +		 JSAMPARRAY output_buf, JDIMENSION output_col)
  60.154 +{
  60.155 +  INT32 tmp0, tmp1, tmp2, tmp3;
  60.156 +  INT32 tmp10, tmp11, tmp12, tmp13;
  60.157 +  INT32 z1, z2, z3, z4, z5;
  60.158 +  JCOEFPTR inptr;
  60.159 +  ISLOW_MULT_TYPE * quantptr;
  60.160 +  int * wsptr;
  60.161 +  JSAMPROW outptr;
  60.162 +  JSAMPLE *range_limit = IDCT_range_limit(cinfo);
  60.163 +  int ctr;
  60.164 +  int workspace[DCTSIZE2];	/* buffers data between passes */
  60.165 +  SHIFT_TEMPS
  60.166 +
  60.167 +  /* Pass 1: process columns from input, store into work array. */
  60.168 +  /* Note results are scaled up by sqrt(8) compared to a true IDCT; */
  60.169 +  /* furthermore, we scale the results by 2**PASS1_BITS. */
  60.170 +
  60.171 +  inptr = coef_block;
  60.172 +  quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table;
  60.173 +  wsptr = workspace;
  60.174 +  for (ctr = DCTSIZE; ctr > 0; ctr--) {
  60.175 +    /* Due to quantization, we will usually find that many of the input
  60.176 +     * coefficients are zero, especially the AC terms.  We can exploit this
  60.177 +     * by short-circuiting the IDCT calculation for any column in which all
  60.178 +     * the AC terms are zero.  In that case each output is equal to the
  60.179 +     * DC coefficient (with scale factor as needed).
  60.180 +     * With typical images and quantization tables, half or more of the
  60.181 +     * column DCT calculations can be simplified this way.
  60.182 +     */
  60.183 +    
  60.184 +    if (inptr[DCTSIZE*1] == 0 && inptr[DCTSIZE*2] == 0 &&
  60.185 +	inptr[DCTSIZE*3] == 0 && inptr[DCTSIZE*4] == 0 &&
  60.186 +	inptr[DCTSIZE*5] == 0 && inptr[DCTSIZE*6] == 0 &&
  60.187 +	inptr[DCTSIZE*7] == 0) {
  60.188 +      /* AC terms all zero */
  60.189 +      int dcval = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]) << PASS1_BITS;
  60.190 +      
  60.191 +      wsptr[DCTSIZE*0] = dcval;
  60.192 +      wsptr[DCTSIZE*1] = dcval;
  60.193 +      wsptr[DCTSIZE*2] = dcval;
  60.194 +      wsptr[DCTSIZE*3] = dcval;
  60.195 +      wsptr[DCTSIZE*4] = dcval;
  60.196 +      wsptr[DCTSIZE*5] = dcval;
  60.197 +      wsptr[DCTSIZE*6] = dcval;
  60.198 +      wsptr[DCTSIZE*7] = dcval;
  60.199 +      
  60.200 +      inptr++;			/* advance pointers to next column */
  60.201 +      quantptr++;
  60.202 +      wsptr++;
  60.203 +      continue;
  60.204 +    }
  60.205 +    
  60.206 +    /* Even part: reverse the even part of the forward DCT. */
  60.207 +    /* The rotator is sqrt(2)*c(-6). */
  60.208 +    
  60.209 +    z2 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]);
  60.210 +    z3 = DEQUANTIZE(inptr[DCTSIZE*6], quantptr[DCTSIZE*6]);
  60.211 +    
  60.212 +    z1 = MULTIPLY(z2 + z3, FIX_0_541196100);
  60.213 +    tmp2 = z1 + MULTIPLY(z3, - FIX_1_847759065);
  60.214 +    tmp3 = z1 + MULTIPLY(z2, FIX_0_765366865);
  60.215 +    
  60.216 +    z2 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
  60.217 +    z3 = DEQUANTIZE(inptr[DCTSIZE*4], quantptr[DCTSIZE*4]);
  60.218 +
  60.219 +    tmp0 = (z2 + z3) << CONST_BITS;
  60.220 +    tmp1 = (z2 - z3) << CONST_BITS;
  60.221 +    
  60.222 +    tmp10 = tmp0 + tmp3;
  60.223 +    tmp13 = tmp0 - tmp3;
  60.224 +    tmp11 = tmp1 + tmp2;
  60.225 +    tmp12 = tmp1 - tmp2;
  60.226 +    
  60.227 +    /* Odd part per figure 8; the matrix is unitary and hence its
  60.228 +     * transpose is its inverse.  i0..i3 are y7,y5,y3,y1 respectively.
  60.229 +     */
  60.230 +    
  60.231 +    tmp0 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]);
  60.232 +    tmp1 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]);
  60.233 +    tmp2 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]);
  60.234 +    tmp3 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]);
  60.235 +    
  60.236 +    z1 = tmp0 + tmp3;
  60.237 +    z2 = tmp1 + tmp2;
  60.238 +    z3 = tmp0 + tmp2;
  60.239 +    z4 = tmp1 + tmp3;
  60.240 +    z5 = MULTIPLY(z3 + z4, FIX_1_175875602); /* sqrt(2) * c3 */
  60.241 +    
  60.242 +    tmp0 = MULTIPLY(tmp0, FIX_0_298631336); /* sqrt(2) * (-c1+c3+c5-c7) */
  60.243 +    tmp1 = MULTIPLY(tmp1, FIX_2_053119869); /* sqrt(2) * ( c1+c3-c5+c7) */
  60.244 +    tmp2 = MULTIPLY(tmp2, FIX_3_072711026); /* sqrt(2) * ( c1+c3+c5-c7) */
  60.245 +    tmp3 = MULTIPLY(tmp3, FIX_1_501321110); /* sqrt(2) * ( c1+c3-c5-c7) */
  60.246 +    z1 = MULTIPLY(z1, - FIX_0_899976223); /* sqrt(2) * (c7-c3) */
  60.247 +    z2 = MULTIPLY(z2, - FIX_2_562915447); /* sqrt(2) * (-c1-c3) */
  60.248 +    z3 = MULTIPLY(z3, - FIX_1_961570560); /* sqrt(2) * (-c3-c5) */
  60.249 +    z4 = MULTIPLY(z4, - FIX_0_390180644); /* sqrt(2) * (c5-c3) */
  60.250 +    
  60.251 +    z3 += z5;
  60.252 +    z4 += z5;
  60.253 +    
  60.254 +    tmp0 += z1 + z3;
  60.255 +    tmp1 += z2 + z4;
  60.256 +    tmp2 += z2 + z3;
  60.257 +    tmp3 += z1 + z4;
  60.258 +    
  60.259 +    /* Final output stage: inputs are tmp10..tmp13, tmp0..tmp3 */
  60.260 +    
  60.261 +    wsptr[DCTSIZE*0] = (int) DESCALE(tmp10 + tmp3, CONST_BITS-PASS1_BITS);
  60.262 +    wsptr[DCTSIZE*7] = (int) DESCALE(tmp10 - tmp3, CONST_BITS-PASS1_BITS);
  60.263 +    wsptr[DCTSIZE*1] = (int) DESCALE(tmp11 + tmp2, CONST_BITS-PASS1_BITS);
  60.264 +    wsptr[DCTSIZE*6] = (int) DESCALE(tmp11 - tmp2, CONST_BITS-PASS1_BITS);
  60.265 +    wsptr[DCTSIZE*2] = (int) DESCALE(tmp12 + tmp1, CONST_BITS-PASS1_BITS);
  60.266 +    wsptr[DCTSIZE*5] = (int) DESCALE(tmp12 - tmp1, CONST_BITS-PASS1_BITS);
  60.267 +    wsptr[DCTSIZE*3] = (int) DESCALE(tmp13 + tmp0, CONST_BITS-PASS1_BITS);
  60.268 +    wsptr[DCTSIZE*4] = (int) DESCALE(tmp13 - tmp0, CONST_BITS-PASS1_BITS);
  60.269 +    
  60.270 +    inptr++;			/* advance pointers to next column */
  60.271 +    quantptr++;
  60.272 +    wsptr++;
  60.273 +  }
  60.274 +  
  60.275 +  /* Pass 2: process rows from work array, store into output array. */
  60.276 +  /* Note that we must descale the results by a factor of 8 == 2**3, */
  60.277 +  /* and also undo the PASS1_BITS scaling. */
  60.278 +
  60.279 +  wsptr = workspace;
  60.280 +  for (ctr = 0; ctr < DCTSIZE; ctr++) {
  60.281 +    outptr = output_buf[ctr] + output_col;
  60.282 +    /* Rows of zeroes can be exploited in the same way as we did with columns.
  60.283 +     * However, the column calculation has created many nonzero AC terms, so
  60.284 +     * the simplification applies less often (typically 5% to 10% of the time).
  60.285 +     * On machines with very fast multiplication, it's possible that the
  60.286 +     * test takes more time than it's worth.  In that case this section
  60.287 +     * may be commented out.
  60.288 +     */
  60.289 +    
  60.290 +#ifndef NO_ZERO_ROW_TEST
  60.291 +    if (wsptr[1] == 0 && wsptr[2] == 0 && wsptr[3] == 0 && wsptr[4] == 0 &&
  60.292 +	wsptr[5] == 0 && wsptr[6] == 0 && wsptr[7] == 0) {
  60.293 +      /* AC terms all zero */
  60.294 +      JSAMPLE dcval = range_limit[(int) DESCALE((INT32) wsptr[0], PASS1_BITS+3)
  60.295 +				  & RANGE_MASK];
  60.296 +      
  60.297 +      outptr[0] = dcval;
  60.298 +      outptr[1] = dcval;
  60.299 +      outptr[2] = dcval;
  60.300 +      outptr[3] = dcval;
  60.301 +      outptr[4] = dcval;
  60.302 +      outptr[5] = dcval;
  60.303 +      outptr[6] = dcval;
  60.304 +      outptr[7] = dcval;
  60.305 +
  60.306 +      wsptr += DCTSIZE;		/* advance pointer to next row */
  60.307 +      continue;
  60.308 +    }
  60.309 +#endif
  60.310 +    
  60.311 +    /* Even part: reverse the even part of the forward DCT. */
  60.312 +    /* The rotator is sqrt(2)*c(-6). */
  60.313 +    
  60.314 +    z2 = (INT32) wsptr[2];
  60.315 +    z3 = (INT32) wsptr[6];
  60.316 +    
  60.317 +    z1 = MULTIPLY(z2 + z3, FIX_0_541196100);
  60.318 +    tmp2 = z1 + MULTIPLY(z3, - FIX_1_847759065);
  60.319 +    tmp3 = z1 + MULTIPLY(z2, FIX_0_765366865);
  60.320 +    
  60.321 +    tmp0 = ((INT32) wsptr[0] + (INT32) wsptr[4]) << CONST_BITS;
  60.322 +    tmp1 = ((INT32) wsptr[0] - (INT32) wsptr[4]) << CONST_BITS;
  60.323 +    
  60.324 +    tmp10 = tmp0 + tmp3;
  60.325 +    tmp13 = tmp0 - tmp3;
  60.326 +    tmp11 = tmp1 + tmp2;
  60.327 +    tmp12 = tmp1 - tmp2;
  60.328 +    
  60.329 +    /* Odd part per figure 8; the matrix is unitary and hence its
  60.330 +     * transpose is its inverse.  i0..i3 are y7,y5,y3,y1 respectively.
  60.331 +     */
  60.332 +    
  60.333 +    tmp0 = (INT32) wsptr[7];
  60.334 +    tmp1 = (INT32) wsptr[5];
  60.335 +    tmp2 = (INT32) wsptr[3];
  60.336 +    tmp3 = (INT32) wsptr[1];
  60.337 +    
  60.338 +    z1 = tmp0 + tmp3;
  60.339 +    z2 = tmp1 + tmp2;
  60.340 +    z3 = tmp0 + tmp2;
  60.341 +    z4 = tmp1 + tmp3;
  60.342 +    z5 = MULTIPLY(z3 + z4, FIX_1_175875602); /* sqrt(2) * c3 */
  60.343 +    
  60.344 +    tmp0 = MULTIPLY(tmp0, FIX_0_298631336); /* sqrt(2) * (-c1+c3+c5-c7) */
  60.345 +    tmp1 = MULTIPLY(tmp1, FIX_2_053119869); /* sqrt(2) * ( c1+c3-c5+c7) */
  60.346 +    tmp2 = MULTIPLY(tmp2, FIX_3_072711026); /* sqrt(2) * ( c1+c3+c5-c7) */
  60.347 +    tmp3 = MULTIPLY(tmp3, FIX_1_501321110); /* sqrt(2) * ( c1+c3-c5-c7) */
  60.348 +    z1 = MULTIPLY(z1, - FIX_0_899976223); /* sqrt(2) * (c7-c3) */
  60.349 +    z2 = MULTIPLY(z2, - FIX_2_562915447); /* sqrt(2) * (-c1-c3) */
  60.350 +    z3 = MULTIPLY(z3, - FIX_1_961570560); /* sqrt(2) * (-c3-c5) */
  60.351 +    z4 = MULTIPLY(z4, - FIX_0_390180644); /* sqrt(2) * (c5-c3) */
  60.352 +    
  60.353 +    z3 += z5;
  60.354 +    z4 += z5;
  60.355 +    
  60.356 +    tmp0 += z1 + z3;
  60.357 +    tmp1 += z2 + z4;
  60.358 +    tmp2 += z2 + z3;
  60.359 +    tmp3 += z1 + z4;
  60.360 +    
  60.361 +    /* Final output stage: inputs are tmp10..tmp13, tmp0..tmp3 */
  60.362 +    
  60.363 +    outptr[0] = range_limit[(int) DESCALE(tmp10 + tmp3,
  60.364 +					  CONST_BITS+PASS1_BITS+3)
  60.365 +			    & RANGE_MASK];
  60.366 +    outptr[7] = range_limit[(int) DESCALE(tmp10 - tmp3,
  60.367 +					  CONST_BITS+PASS1_BITS+3)
  60.368 +			    & RANGE_MASK];
  60.369 +    outptr[1] = range_limit[(int) DESCALE(tmp11 + tmp2,
  60.370 +					  CONST_BITS+PASS1_BITS+3)
  60.371 +			    & RANGE_MASK];
  60.372 +    outptr[6] = range_limit[(int) DESCALE(tmp11 - tmp2,
  60.373 +					  CONST_BITS+PASS1_BITS+3)
  60.374 +			    & RANGE_MASK];
  60.375 +    outptr[2] = range_limit[(int) DESCALE(tmp12 + tmp1,
  60.376 +					  CONST_BITS+PASS1_BITS+3)
  60.377 +			    & RANGE_MASK];
  60.378 +    outptr[5] = range_limit[(int) DESCALE(tmp12 - tmp1,
  60.379 +					  CONST_BITS+PASS1_BITS+3)
  60.380 +			    & RANGE_MASK];
  60.381 +    outptr[3] = range_limit[(int) DESCALE(tmp13 + tmp0,
  60.382 +					  CONST_BITS+PASS1_BITS+3)
  60.383 +			    & RANGE_MASK];
  60.384 +    outptr[4] = range_limit[(int) DESCALE(tmp13 - tmp0,
  60.385 +					  CONST_BITS+PASS1_BITS+3)
  60.386 +			    & RANGE_MASK];
  60.387 +    
  60.388 +    wsptr += DCTSIZE;		/* advance pointer to next row */
  60.389 +  }
  60.390 +}
  60.391 +
  60.392 +#endif /* DCT_ISLOW_SUPPORTED */
    61.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    61.2 +++ b/libs/libjpeg/jidctred.c	Sat Sep 19 05:51:51 2015 +0300
    61.3 @@ -0,0 +1,398 @@
    61.4 +/*
    61.5 + * jidctred.c
    61.6 + *
    61.7 + * Copyright (C) 1994-1998, Thomas G. Lane.
    61.8 + * This file is part of the Independent JPEG Group's software.
    61.9 + * For conditions of distribution and use, see the accompanying README file.
   61.10 + *
   61.11 + * This file contains inverse-DCT routines that produce reduced-size output:
   61.12 + * either 4x4, 2x2, or 1x1 pixels from an 8x8 DCT block.
   61.13 + *
   61.14 + * The implementation is based on the Loeffler, Ligtenberg and Moschytz (LL&M)
   61.15 + * algorithm used in jidctint.c.  We simply replace each 8-to-8 1-D IDCT step
   61.16 + * with an 8-to-4 step that produces the four averages of two adjacent outputs
   61.17 + * (or an 8-to-2 step producing two averages of four outputs, for 2x2 output).
   61.18 + * These steps were derived by computing the corresponding values at the end
   61.19 + * of the normal LL&M code, then simplifying as much as possible.
   61.20 + *
   61.21 + * 1x1 is trivial: just take the DC coefficient divided by 8.
   61.22 + *
   61.23 + * See jidctint.c for additional comments.
   61.24 + */
   61.25 +
   61.26 +#define JPEG_INTERNALS
   61.27 +#include "jinclude.h"
   61.28 +#include "jpeglib.h"
   61.29 +#include "jdct.h"		/* Private declarations for DCT subsystem */
   61.30 +
   61.31 +#ifdef IDCT_SCALING_SUPPORTED
   61.32 +
   61.33 +
   61.34 +/*
   61.35 + * This module is specialized to the case DCTSIZE = 8.
   61.36 + */
   61.37 +
   61.38 +#if DCTSIZE != 8
   61.39 +  Sorry, this code only copes with 8x8 DCTs. /* deliberate syntax err */
   61.40 +#endif
   61.41 +
   61.42 +
   61.43 +/* Scaling is the same as in jidctint.c. */
   61.44 +
   61.45 +#if BITS_IN_JSAMPLE == 8
   61.46 +#define CONST_BITS  13
   61.47 +#define PASS1_BITS  2
   61.48 +#else
   61.49 +#define CONST_BITS  13
   61.50 +#define PASS1_BITS  1		/* lose a little precision to avoid overflow */
   61.51 +#endif
   61.52 +
   61.53 +/* Some C compilers fail to reduce "FIX(constant)" at compile time, thus
   61.54 + * causing a lot of useless floating-point operations at run time.
   61.55 + * To get around this we use the following pre-calculated constants.
   61.56 + * If you change CONST_BITS you may want to add appropriate values.
   61.57 + * (With a reasonable C compiler, you can just rely on the FIX() macro...)
   61.58 + */
   61.59 +
   61.60 +#if CONST_BITS == 13
   61.61 +#define FIX_0_211164243  ((INT32)  1730)	/* FIX(0.211164243) */
   61.62 +#define FIX_0_509795579  ((INT32)  4176)	/* FIX(0.509795579) */
   61.63 +#define FIX_0_601344887  ((INT32)  4926)	/* FIX(0.601344887) */
   61.64 +#define FIX_0_720959822  ((INT32)  5906)	/* FIX(0.720959822) */
   61.65 +#define FIX_0_765366865  ((INT32)  6270)	/* FIX(0.765366865) */
   61.66 +#define FIX_0_850430095  ((INT32)  6967)	/* FIX(0.850430095) */
   61.67 +#define FIX_0_899976223  ((INT32)  7373)	/* FIX(0.899976223) */
   61.68 +#define FIX_1_061594337  ((INT32)  8697)	/* FIX(1.061594337) */
   61.69 +#define FIX_1_272758580  ((INT32)  10426)	/* FIX(1.272758580) */
   61.70 +#define FIX_1_451774981  ((INT32)  11893)	/* FIX(1.451774981) */
   61.71 +#define FIX_1_847759065  ((INT32)  15137)	/* FIX(1.847759065) */
   61.72 +#define FIX_2_172734803  ((INT32)  17799)	/* FIX(2.172734803) */
   61.73 +#define FIX_2_562915447  ((INT32)  20995)	/* FIX(2.562915447) */
   61.74 +#define FIX_3_624509785  ((INT32)  29692)	/* FIX(3.624509785) */
   61.75 +#else
   61.76 +#define FIX_0_211164243  FIX(0.211164243)
   61.77 +#define FIX_0_509795579  FIX(0.509795579)
   61.78 +#define FIX_0_601344887  FIX(0.601344887)
   61.79 +#define FIX_0_720959822  FIX(0.720959822)
   61.80 +#define FIX_0_765366865  FIX(0.765366865)
   61.81 +#define FIX_0_850430095  FIX(0.850430095)
   61.82 +#define FIX_0_899976223  FIX(0.899976223)
   61.83 +#define FIX_1_061594337  FIX(1.061594337)
   61.84 +#define FIX_1_272758580  FIX(1.272758580)
   61.85 +#define FIX_1_451774981  FIX(1.451774981)
   61.86 +#define FIX_1_847759065  FIX(1.847759065)
   61.87 +#define FIX_2_172734803  FIX(2.172734803)
   61.88 +#define FIX_2_562915447  FIX(2.562915447)
   61.89 +#define FIX_3_624509785  FIX(3.624509785)
   61.90 +#endif
   61.91 +
   61.92 +
   61.93 +/* Multiply an INT32 variable by an INT32 constant to yield an INT32 result.
   61.94 + * For 8-bit samples with the recommended scaling, all the variable
   61.95 + * and constant values involved are no more than 16 bits wide, so a
   61.96 + * 16x16->32 bit multiply can be used instead of a full 32x32 multiply.
   61.97 + * For 12-bit samples, a full 32-bit multiplication will be needed.
   61.98 + */
   61.99 +
  61.100 +#if BITS_IN_JSAMPLE == 8
  61.101 +#define MULTIPLY(var,const)  MULTIPLY16C16(var,const)
  61.102 +#else
  61.103 +#define MULTIPLY(var,const)  ((var) * (const))
  61.104 +#endif
  61.105 +
  61.106 +
  61.107 +/* Dequantize a coefficient by multiplying it by the multiplier-table
  61.108 + * entry; produce an int result.  In this module, both inputs and result
  61.109 + * are 16 bits or less, so either int or short multiply will work.
  61.110 + */
  61.111 +
  61.112 +#define DEQUANTIZE(coef,quantval)  (((ISLOW_MULT_TYPE) (coef)) * (quantval))
  61.113 +
  61.114 +
  61.115 +/*
  61.116 + * Perform dequantization and inverse DCT on one block of coefficients,
  61.117 + * producing a reduced-size 4x4 output block.
  61.118 + */
  61.119 +
  61.120 +GLOBAL(void)
  61.121 +jpeg_idct_4x4 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
  61.122 +	       JCOEFPTR coef_block,
  61.123 +	       JSAMPARRAY output_buf, JDIMENSION output_col)
  61.124 +{
  61.125 +  INT32 tmp0, tmp2, tmp10, tmp12;
  61.126 +  INT32 z1, z2, z3, z4;
  61.127 +  JCOEFPTR inptr;
  61.128 +  ISLOW_MULT_TYPE * quantptr;
  61.129 +  int * wsptr;
  61.130 +  JSAMPROW outptr;
  61.131 +  JSAMPLE *range_limit = IDCT_range_limit(cinfo);
  61.132 +  int ctr;
  61.133 +  int workspace[DCTSIZE*4];	/* buffers data between passes */
  61.134 +  SHIFT_TEMPS
  61.135 +
  61.136 +  /* Pass 1: process columns from input, store into work array. */
  61.137 +
  61.138 +  inptr = coef_block;
  61.139 +  quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table;
  61.140 +  wsptr = workspace;
  61.141 +  for (ctr = DCTSIZE; ctr > 0; inptr++, quantptr++, wsptr++, ctr--) {
  61.142 +    /* Don't bother to process column 4, because second pass won't use it */
  61.143 +    if (ctr == DCTSIZE-4)
  61.144 +      continue;
  61.145 +    if (inptr[DCTSIZE*1] == 0 && inptr[DCTSIZE*2] == 0 &&
  61.146 +	inptr[DCTSIZE*3] == 0 && inptr[DCTSIZE*5] == 0 &&
  61.147 +	inptr[DCTSIZE*6] == 0 && inptr[DCTSIZE*7] == 0) {
  61.148 +      /* AC terms all zero; we need not examine term 4 for 4x4 output */
  61.149 +      int dcval = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]) << PASS1_BITS;
  61.150 +      
  61.151 +      wsptr[DCTSIZE*0] = dcval;
  61.152 +      wsptr[DCTSIZE*1] = dcval;
  61.153 +      wsptr[DCTSIZE*2] = dcval;
  61.154 +      wsptr[DCTSIZE*3] = dcval;
  61.155 +      
  61.156 +      continue;
  61.157 +    }
  61.158 +    
  61.159 +    /* Even part */
  61.160 +    
  61.161 +    tmp0 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
  61.162 +    tmp0 <<= (CONST_BITS+1);
  61.163 +    
  61.164 +    z2 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]);
  61.165 +    z3 = DEQUANTIZE(inptr[DCTSIZE*6], quantptr[DCTSIZE*6]);
  61.166 +
  61.167 +    tmp2 = MULTIPLY(z2, FIX_1_847759065) + MULTIPLY(z3, - FIX_0_765366865);
  61.168 +    
  61.169 +    tmp10 = tmp0 + tmp2;
  61.170 +    tmp12 = tmp0 - tmp2;
  61.171 +    
  61.172 +    /* Odd part */
  61.173 +    
  61.174 +    z1 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]);
  61.175 +    z2 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]);
  61.176 +    z3 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]);
  61.177 +    z4 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]);
  61.178 +    
  61.179 +    tmp0 = MULTIPLY(z1, - FIX_0_211164243) /* sqrt(2) * (c3-c1) */
  61.180 +	 + MULTIPLY(z2, FIX_1_451774981) /* sqrt(2) * (c3+c7) */
  61.181 +	 + MULTIPLY(z3, - FIX_2_172734803) /* sqrt(2) * (-c1-c5) */
  61.182 +	 + MULTIPLY(z4, FIX_1_061594337); /* sqrt(2) * (c5+c7) */
  61.183 +    
  61.184 +    tmp2 = MULTIPLY(z1, - FIX_0_509795579) /* sqrt(2) * (c7-c5) */
  61.185 +	 + MULTIPLY(z2, - FIX_0_601344887) /* sqrt(2) * (c5-c1) */
  61.186 +	 + MULTIPLY(z3, FIX_0_899976223) /* sqrt(2) * (c3-c7) */
  61.187 +	 + MULTIPLY(z4, FIX_2_562915447); /* sqrt(2) * (c1+c3) */
  61.188 +
  61.189 +    /* Final output stage */
  61.190 +    
  61.191 +    wsptr[DCTSIZE*0] = (int) DESCALE(tmp10 + tmp2, CONST_BITS-PASS1_BITS+1);
  61.192 +    wsptr[DCTSIZE*3] = (int) DESCALE(tmp10 - tmp2, CONST_BITS-PASS1_BITS+1);
  61.193 +    wsptr[DCTSIZE*1] = (int) DESCALE(tmp12 + tmp0, CONST_BITS-PASS1_BITS+1);
  61.194 +    wsptr[DCTSIZE*2] = (int) DESCALE(tmp12 - tmp0, CONST_BITS-PASS1_BITS+1);
  61.195 +  }
  61.196 +  
  61.197 +  /* Pass 2: process 4 rows from work array, store into output array. */
  61.198 +
  61.199 +  wsptr = workspace;
  61.200 +  for (ctr = 0; ctr < 4; ctr++) {
  61.201 +    outptr = output_buf[ctr] + output_col;
  61.202 +    /* It's not clear whether a zero row test is worthwhile here ... */
  61.203 +
  61.204 +#ifndef NO_ZERO_ROW_TEST
  61.205 +    if (wsptr[1] == 0 && wsptr[2] == 0 && wsptr[3] == 0 &&
  61.206 +	wsptr[5] == 0 && wsptr[6] == 0 && wsptr[7] == 0) {
  61.207 +      /* AC terms all zero */
  61.208 +      JSAMPLE dcval = range_limit[(int) DESCALE((INT32) wsptr[0], PASS1_BITS+3)
  61.209 +				  & RANGE_MASK];
  61.210 +      
  61.211 +      outptr[0] = dcval;
  61.212 +      outptr[1] = dcval;
  61.213 +      outptr[2] = dcval;
  61.214 +      outptr[3] = dcval;
  61.215 +      
  61.216 +      wsptr += DCTSIZE;		/* advance pointer to next row */
  61.217 +      continue;
  61.218 +    }
  61.219 +#endif
  61.220 +    
  61.221 +    /* Even part */
  61.222 +    
  61.223 +    tmp0 = ((INT32) wsptr[0]) << (CONST_BITS+1);
  61.224 +    
  61.225 +    tmp2 = MULTIPLY((INT32) wsptr[2], FIX_1_847759065)
  61.226 +	 + MULTIPLY((INT32) wsptr[6], - FIX_0_765366865);
  61.227 +    
  61.228 +    tmp10 = tmp0 + tmp2;
  61.229 +    tmp12 = tmp0 - tmp2;
  61.230 +    
  61.231 +    /* Odd part */
  61.232 +    
  61.233 +    z1 = (INT32) wsptr[7];
  61.234 +    z2 = (INT32) wsptr[5];
  61.235 +    z3 = (INT32) wsptr[3];
  61.236 +    z4 = (INT32) wsptr[1];
  61.237 +    
  61.238 +    tmp0 = MULTIPLY(z1, - FIX_0_211164243) /* sqrt(2) * (c3-c1) */
  61.239 +	 + MULTIPLY(z2, FIX_1_451774981) /* sqrt(2) * (c3+c7) */
  61.240 +	 + MULTIPLY(z3, - FIX_2_172734803) /* sqrt(2) * (-c1-c5) */
  61.241 +	 + MULTIPLY(z4, FIX_1_061594337); /* sqrt(2) * (c5+c7) */
  61.242 +    
  61.243 +    tmp2 = MULTIPLY(z1, - FIX_0_509795579) /* sqrt(2) * (c7-c5) */
  61.244 +	 + MULTIPLY(z2, - FIX_0_601344887) /* sqrt(2) * (c5-c1) */
  61.245 +	 + MULTIPLY(z3, FIX_0_899976223) /* sqrt(2) * (c3-c7) */
  61.246 +	 + MULTIPLY(z4, FIX_2_562915447); /* sqrt(2) * (c1+c3) */
  61.247 +
  61.248 +    /* Final output stage */
  61.249 +    
  61.250 +    outptr[0] = range_limit[(int) DESCALE(tmp10 + tmp2,
  61.251 +					  CONST_BITS+PASS1_BITS+3+1)
  61.252 +			    & RANGE_MASK];
  61.253 +    outptr[3] = range_limit[(int) DESCALE(tmp10 - tmp2,
  61.254 +					  CONST_BITS+PASS1_BITS+3+1)
  61.255 +			    & RANGE_MASK];
  61.256 +    outptr[1] = range_limit[(int) DESCALE(tmp12 + tmp0,
  61.257 +					  CONST_BITS+PASS1_BITS+3+1)
  61.258 +			    & RANGE_MASK];
  61.259 +    outptr[2] = range_limit[(int) DESCALE(tmp12 - tmp0,
  61.260 +					  CONST_BITS+PASS1_BITS+3+1)
  61.261 +			    & RANGE_MASK];
  61.262 +    
  61.263 +    wsptr += DCTSIZE;		/* advance pointer to next row */
  61.264 +  }
  61.265 +}
  61.266 +
  61.267 +
  61.268 +/*
  61.269 + * Perform dequantization and inverse DCT on one block of coefficients,
  61.270 + * producing a reduced-size 2x2 output block.
  61.271 + */
  61.272 +
  61.273 +GLOBAL(void)
  61.274 +jpeg_idct_2x2 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
  61.275 +	       JCOEFPTR coef_block,
  61.276 +	       JSAMPARRAY output_buf, JDIMENSION output_col)
  61.277 +{
  61.278 +  INT32 tmp0, tmp10, z1;
  61.279 +  JCOEFPTR inptr;
  61.280 +  ISLOW_MULT_TYPE * quantptr;
  61.281 +  int * wsptr;
  61.282 +  JSAMPROW outptr;
  61.283 +  JSAMPLE *range_limit = IDCT_range_limit(cinfo);
  61.284 +  int ctr;
  61.285 +  int workspace[DCTSIZE*2];	/* buffers data between passes */
  61.286 +  SHIFT_TEMPS
  61.287 +
  61.288 +  /* Pass 1: process columns from input, store into work array. */
  61.289 +
  61.290 +  inptr = coef_block;
  61.291 +  quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table;
  61.292 +  wsptr = workspace;
  61.293 +  for (ctr = DCTSIZE; ctr > 0; inptr++, quantptr++, wsptr++, ctr--) {
  61.294 +    /* Don't bother to process columns 2,4,6 */
  61.295 +    if (ctr == DCTSIZE-2 || ctr == DCTSIZE-4 || ctr == DCTSIZE-6)
  61.296 +      continue;
  61.297 +    if (inptr[DCTSIZE*1] == 0 && inptr[DCTSIZE*3] == 0 &&
  61.298 +	inptr[DCTSIZE*5] == 0 && inptr[DCTSIZE*7] == 0) {
  61.299 +      /* AC terms all zero; we need not examine terms 2,4,6 for 2x2 output */
  61.300 +      int dcval = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]) << PASS1_BITS;
  61.301 +      
  61.302 +      wsptr[DCTSIZE*0] = dcval;
  61.303 +      wsptr[DCTSIZE*1] = dcval;
  61.304 +      
  61.305 +      continue;
  61.306 +    }
  61.307 +    
  61.308 +    /* Even part */
  61.309 +    
  61.310 +    z1 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
  61.311 +    tmp10 = z1 << (CONST_BITS+2);
  61.312 +    
  61.313 +    /* Odd part */
  61.314 +
  61.315 +    z1 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]);
  61.316 +    tmp0 = MULTIPLY(z1, - FIX_0_720959822); /* sqrt(2) * (c7-c5+c3-c1) */
  61.317 +    z1 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]);
  61.318 +    tmp0 += MULTIPLY(z1, FIX_0_850430095); /* sqrt(2) * (-c1+c3+c5+c7) */
  61.319 +    z1 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]);
  61.320 +    tmp0 += MULTIPLY(z1, - FIX_1_272758580); /* sqrt(2) * (-c1+c3-c5-c7) */
  61.321 +    z1 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]);
  61.322 +    tmp0 += MULTIPLY(z1, FIX_3_624509785); /* sqrt(2) * (c1+c3+c5+c7) */
  61.323 +
  61.324 +    /* Final output stage */
  61.325 +    
  61.326 +    wsptr[DCTSIZE*0] = (int) DESCALE(tmp10 + tmp0, CONST_BITS-PASS1_BITS+2);
  61.327 +    wsptr[DCTSIZE*1] = (int) DESCALE(tmp10 - tmp0, CONST_BITS-PASS1_BITS+2);
  61.328 +  }
  61.329 +  
  61.330 +  /* Pass 2: process 2 rows from work array, store into output array. */
  61.331 +
  61.332 +  wsptr = workspace;
  61.333 +  for (ctr = 0; ctr < 2; ctr++) {
  61.334 +    outptr = output_buf[ctr] + output_col;
  61.335 +    /* It's not clear whether a zero row test is worthwhile here ... */
  61.336 +
  61.337 +#ifndef NO_ZERO_ROW_TEST
  61.338 +    if (wsptr[1] == 0 && wsptr[3] == 0 && wsptr[5] == 0 && wsptr[7] == 0) {
  61.339 +      /* AC terms all zero */
  61.340 +      JSAMPLE dcval = range_limit[(int) DESCALE((INT32) wsptr[0], PASS1_BITS+3)
  61.341 +				  & RANGE_MASK];
  61.342 +      
  61.343 +      outptr[0] = dcval;
  61.344 +      outptr[1] = dcval;
  61.345 +      
  61.346 +      wsptr += DCTSIZE;		/* advance pointer to next row */
  61.347 +      continue;
  61.348 +    }
  61.349 +#endif
  61.350 +    
  61.351 +    /* Even part */
  61.352 +    
  61.353 +    tmp10 = ((INT32) wsptr[0]) << (CONST_BITS+2);
  61.354 +    
  61.355 +    /* Odd part */
  61.356 +
  61.357 +    tmp0 = MULTIPLY((INT32) wsptr[7], - FIX_0_720959822) /* sqrt(2) * (c7-c5+c3-c1) */
  61.358 +	 + MULTIPLY((INT32) wsptr[5], FIX_0_850430095) /* sqrt(2) * (-c1+c3+c5+c7) */
  61.359 +	 + MULTIPLY((INT32) wsptr[3], - FIX_1_272758580) /* sqrt(2) * (-c1+c3-c5-c7) */
  61.360 +	 + MULTIPLY((INT32) wsptr[1], FIX_3_624509785); /* sqrt(2) * (c1+c3+c5+c7) */
  61.361 +
  61.362 +    /* Final output stage */
  61.363 +    
  61.364 +    outptr[0] = range_limit[(int) DESCALE(tmp10 + tmp0,
  61.365 +					  CONST_BITS+PASS1_BITS+3+2)
  61.366 +			    & RANGE_MASK];
  61.367 +    outptr[1] = range_limit[(int) DESCALE(tmp10 - tmp0,
  61.368 +					  CONST_BITS+PASS1_BITS+3+2)
  61.369 +			    & RANGE_MASK];
  61.370 +    
  61.371 +    wsptr += DCTSIZE;		/* advance pointer to next row */
  61.372 +  }
  61.373 +}
  61.374 +
  61.375 +
  61.376 +/*
  61.377 + * Perform dequantization and inverse DCT on one block of coefficients,
  61.378 + * producing a reduced-size 1x1 output block.
  61.379 + */
  61.380 +
  61.381 +GLOBAL(void)
  61.382 +jpeg_idct_1x1 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
  61.383 +	       JCOEFPTR coef_block,
  61.384 +	       JSAMPARRAY output_buf, JDIMENSION output_col)
  61.385 +{
  61.386 +  int dcval;
  61.387 +  ISLOW_MULT_TYPE * quantptr;
  61.388 +  JSAMPLE *range_limit = IDCT_range_limit(cinfo);
  61.389 +  SHIFT_TEMPS
  61.390 +
  61.391 +  /* We hardly need an inverse DCT routine for this: just take the
  61.392 +   * average pixel value, which is one-eighth of the DC coefficient.
  61.393 +   */
  61.394 +  quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table;
  61.395 +  dcval = DEQUANTIZE(coef_block[0], quantptr[0]);
  61.396 +  dcval = (int) DESCALE((INT32) dcval, 3);
  61.397 +
  61.398 +  output_buf[0][output_col] = range_limit[dcval & RANGE_MASK];
  61.399 +}
  61.400 +
  61.401 +#endif /* IDCT_SCALING_SUPPORTED */
    62.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    62.2 +++ b/libs/libjpeg/jinclude.h	Sat Sep 19 05:51:51 2015 +0300
    62.3 @@ -0,0 +1,91 @@
    62.4 +/*
    62.5 + * jinclude.h
    62.6 + *
    62.7 + * Copyright (C) 1991-1994, Thomas G. Lane.
    62.8 + * This file is part of the Independent JPEG Group's software.
    62.9 + * For conditions of distribution and use, see the accompanying README file.
   62.10 + *
   62.11 + * This file exists to provide a single place to fix any problems with
   62.12 + * including the wrong system include files.  (Common problems are taken
   62.13 + * care of by the standard jconfig symbols, but on really weird systems
   62.14 + * you may have to edit this file.)
   62.15 + *
   62.16 + * NOTE: this file is NOT intended to be included by applications using the
   62.17 + * JPEG library.  Most applications need only include jpeglib.h.
   62.18 + */
   62.19 +
   62.20 +
   62.21 +/* Include auto-config file to find out which system include files we need. */
   62.22 +
   62.23 +#include "jconfig.h"		/* auto configuration options */
   62.24 +#define JCONFIG_INCLUDED	/* so that jpeglib.h doesn't do it again */
   62.25 +
   62.26 +/*
   62.27 + * We need the NULL macro and size_t typedef.
   62.28 + * On an ANSI-conforming system it is sufficient to include <stddef.h>.
   62.29 + * Otherwise, we get them from <stdlib.h> or <stdio.h>; we may have to
   62.30 + * pull in <sys/types.h> as well.
   62.31 + * Note that the core JPEG library does not require <stdio.h>;
   62.32 + * only the default error handler and data source/destination modules do.
   62.33 + * But we must pull it in because of the references to FILE in jpeglib.h.
   62.34 + * You can remove those references if you want to compile without <stdio.h>.
   62.35 + */
   62.36 +
   62.37 +#ifdef HAVE_STDDEF_H
   62.38 +#include <stddef.h>
   62.39 +#endif
   62.40 +
   62.41 +#ifdef HAVE_STDLIB_H
   62.42 +#include <stdlib.h>
   62.43 +#endif
   62.44 +
   62.45 +#ifdef NEED_SYS_TYPES_H
   62.46 +#include <sys/types.h>
   62.47 +#endif
   62.48 +
   62.49 +#include <stdio.h>
   62.50 +
   62.51 +/*
   62.52 + * We need memory copying and zeroing functions, plus strncpy().
   62.53 + * ANSI and System V implementations declare these in <string.h>.
   62.54 + * BSD doesn't have the mem() functions, but it does have bcopy()/bzero().
   62.55 + * Some systems may declare memset and memcpy in <memory.h>.
   62.56 + *
   62.57 + * NOTE: we assume the size parameters to these functions are of type size_t.
   62.58 + * Change the casts in these macros if not!
   62.59 + */
   62.60 +
   62.61 +#ifdef NEED_BSD_STRINGS
   62.62 +
   62.63 +#include <strings.h>
   62.64 +#define MEMZERO(target,size)	bzero((void *)(target), (size_t)(size))
   62.65 +#define MEMCOPY(dest,src,size)	bcopy((const void *)(src), (void *)(dest), (size_t)(size))
   62.66 +
   62.67 +#else /* not BSD, assume ANSI/SysV string lib */
   62.68 +
   62.69 +#include <string.h>
   62.70 +#define MEMZERO(target,size)	memset((void *)(target), 0, (size_t)(size))
   62.71 +#define MEMCOPY(dest,src,size)	memcpy((void *)(dest), (const void *)(src), (size_t)(size))
   62.72 +
   62.73 +#endif
   62.74 +
   62.75 +/*
   62.76 + * In ANSI C, and indeed any rational implementation, size_t is also the
   62.77 + * type returned by sizeof().  However, it seems there are some irrational
   62.78 + * implementations out there, in which sizeof() returns an int even though
   62.79 + * size_t is defined as long or unsigned long.  To ensure consistent results
   62.80 + * we always use this SIZEOF() macro in place of using sizeof() directly.
   62.81 + */
   62.82 +
   62.83 +#define SIZEOF(object)	((size_t) sizeof(object))
   62.84 +
   62.85 +/*
   62.86 + * The modules that use fread() and fwrite() always invoke them through
   62.87 + * these macros.  On some systems you may need to twiddle the argument casts.
   62.88 + * CAUTION: argument order is different from underlying functions!
   62.89 + */
   62.90 +
   62.91 +#define JFREAD(file,buf,sizeofbuf)  \
   62.92 +  ((size_t) fread((void *) (buf), (size_t) 1, (size_t) (sizeofbuf), (file)))
   62.93 +#define JFWRITE(file,buf,sizeofbuf)  \
   62.94 +  ((size_t) fwrite((const void *) (buf), (size_t) 1, (size_t) (sizeofbuf), (file)))
    63.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    63.2 +++ b/libs/libjpeg/jmemmgr.c	Sat Sep 19 05:51:51 2015 +0300
    63.3 @@ -0,0 +1,1118 @@
    63.4 +/*
    63.5 + * jmemmgr.c
    63.6 + *
    63.7 + * Copyright (C) 1991-1997, Thomas G. Lane.
    63.8 + * This file is part of the Independent JPEG Group's software.
    63.9 + * For conditions of distribution and use, see the accompanying README file.
   63.10 + *
   63.11 + * This file contains the JPEG system-independent memory management
   63.12 + * routines.  This code is usable across a wide variety of machines; most
   63.13 + * of the system dependencies have been isolated in a separate file.
   63.14 + * The major functions provided here are:
   63.15 + *   * pool-based allocation and freeing of memory;
   63.16 + *   * policy decisions about how to divide available memory among the
   63.17 + *     virtual arrays;
   63.18 + *   * control logic for swapping virtual arrays between main memory and
   63.19 + *     backing storage.
   63.20 + * The separate system-dependent file provides the actual backing-storage
   63.21 + * access code, and it contains the policy decision about how much total
   63.22 + * main memory to use.
   63.23 + * This file is system-dependent in the sense that some of its functions
   63.24 + * are unnecessary in some systems.  For example, if there is enough virtual
   63.25 + * memory so that backing storage will never be used, much of the virtual
   63.26 + * array control logic could be removed.  (Of course, if you have that much
   63.27 + * memory then you shouldn't care about a little bit of unused code...)
   63.28 + */
   63.29 +
   63.30 +#define JPEG_INTERNALS
   63.31 +#define AM_MEMORY_MANAGER	/* we define jvirt_Xarray_control structs */
   63.32 +#include "jinclude.h"
   63.33 +#include "jpeglib.h"
   63.34 +#include "jmemsys.h"		/* import the system-dependent declarations */
   63.35 +
   63.36 +#ifndef NO_GETENV
   63.37 +#ifndef HAVE_STDLIB_H		/* <stdlib.h> should declare getenv() */
   63.38 +extern char * getenv JPP((const char * name));
   63.39 +#endif
   63.40 +#endif
   63.41 +
   63.42 +
   63.43 +/*
   63.44 + * Some important notes:
   63.45 + *   The allocation routines provided here must never return NULL.
   63.46 + *   They should exit to error_exit if unsuccessful.
   63.47 + *
   63.48 + *   It's not a good idea to try to merge the sarray and barray routines,
   63.49 + *   even though they are textually almost the same, because samples are
   63.50 + *   usually stored as bytes while coefficients are shorts or ints.  Thus,
   63.51 + *   in machines where byte pointers have a different representation from
   63.52 + *   word pointers, the resulting machine code could not be the same.
   63.53 + */
   63.54 +
   63.55 +
   63.56 +/*
   63.57 + * Many machines require storage alignment: longs must start on 4-byte
   63.58 + * boundaries, doubles on 8-byte boundaries, etc.  On such machines, malloc()
   63.59 + * always returns pointers that are multiples of the worst-case alignment
   63.60 + * requirement, and we had better do so too.
   63.61 + * There isn't any really portable way to determine the worst-case alignment
   63.62 + * requirement.  This module assumes that the alignment requirement is
   63.63 + * multiples of sizeof(ALIGN_TYPE).
   63.64 + * By default, we define ALIGN_TYPE as double.  This is necessary on some
   63.65 + * workstations (where doubles really do need 8-byte alignment) and will work
   63.66 + * fine on nearly everything.  If your machine has lesser alignment needs,
   63.67 + * you can save a few bytes by making ALIGN_TYPE smaller.
   63.68 + * The only place I know of where this will NOT work is certain Macintosh
   63.69 + * 680x0 compilers that define double as a 10-byte IEEE extended float.
   63.70 + * Doing 10-byte alignment is counterproductive because longwords won't be
   63.71 + * aligned well.  Put "#define ALIGN_TYPE long" in jconfig.h if you have
   63.72 + * such a compiler.
   63.73 + */
   63.74 +
   63.75 +#ifndef ALIGN_TYPE		/* so can override from jconfig.h */
   63.76 +#define ALIGN_TYPE  double
   63.77 +#endif
   63.78 +
   63.79 +
   63.80 +/*
   63.81 + * We allocate objects from "pools", where each pool is gotten with a single
   63.82 + * request to jpeg_get_small() or jpeg_get_large().  There is no per-object
   63.83 + * overhead within a pool, except for alignment padding.  Each pool has a
   63.84 + * header with a link to the next pool of the same class.
   63.85 + * Small and large pool headers are identical except that the latter's
   63.86 + * link pointer must be FAR on 80x86 machines.
   63.87 + * Notice that the "real" header fields are union'ed with a dummy ALIGN_TYPE
   63.88 + * field.  This forces the compiler to make SIZEOF(small_pool_hdr) a multiple
   63.89 + * of the alignment requirement of ALIGN_TYPE.
   63.90 + */
   63.91 +
   63.92 +typedef union small_pool_struct * small_pool_ptr;
   63.93 +
   63.94 +typedef union small_pool_struct {
   63.95 +  struct {
   63.96 +    small_pool_ptr next;	/* next in list of pools */
   63.97 +    size_t bytes_used;		/* how many bytes already used within pool */
   63.98 +    size_t bytes_left;		/* bytes still available in this pool */
   63.99 +  } hdr;
  63.100 +  ALIGN_TYPE dummy;		/* included in union to ensure alignment */
  63.101 +} small_pool_hdr;
  63.102 +
  63.103 +typedef union large_pool_struct FAR * large_pool_ptr;
  63.104 +
  63.105 +typedef union large_pool_struct {
  63.106 +  struct {
  63.107 +    large_pool_ptr next;	/* next in list of pools */
  63.108 +    size_t bytes_used;		/* how many bytes already used within pool */
  63.109 +    size_t bytes_left;		/* bytes still available in this pool */
  63.110 +  } hdr;
  63.111 +  ALIGN_TYPE dummy;		/* included in union to ensure alignment */
  63.112 +} large_pool_hdr;
  63.113 +
  63.114 +
  63.115 +/*
  63.116 + * Here is the full definition of a memory manager object.
  63.117 + */
  63.118 +
  63.119 +typedef struct {
  63.120 +  struct jpeg_memory_mgr pub;	/* public fields */
  63.121 +
  63.122 +  /* Each pool identifier (lifetime class) names a linked list of pools. */
  63.123 +  small_pool_ptr small_list[JPOOL_NUMPOOLS];
  63.124 +  large_pool_ptr large_list[JPOOL_NUMPOOLS];
  63.125 +
  63.126 +  /* Since we only have one lifetime class of virtual arrays, only one
  63.127 +   * linked list is necessary (for each datatype).  Note that the virtual
  63.128 +   * array control blocks being linked together are actually stored somewhere
  63.129 +   * in the small-pool list.
  63.130 +   */
  63.131 +  jvirt_sarray_ptr virt_sarray_list;
  63.132 +  jvirt_barray_ptr virt_barray_list;
  63.133 +
  63.134 +  /* This counts total space obtained from jpeg_get_small/large */
  63.135 +  long total_space_allocated;
  63.136 +
  63.137 +  /* alloc_sarray and alloc_barray set this value for use by virtual
  63.138 +   * array routines.
  63.139 +   */
  63.140 +  JDIMENSION last_rowsperchunk;	/* from most recent alloc_sarray/barray */
  63.141 +} my_memory_mgr;
  63.142 +
  63.143 +typedef my_memory_mgr * my_mem_ptr;
  63.144 +
  63.145 +
  63.146 +/*
  63.147 + * The control blocks for virtual arrays.
  63.148 + * Note that these blocks are allocated in the "small" pool area.
  63.149 + * System-dependent info for the associated backing store (if any) is hidden
  63.150 + * inside the backing_store_info struct.
  63.151 + */
  63.152 +
  63.153 +struct jvirt_sarray_control {
  63.154 +  JSAMPARRAY mem_buffer;	/* => the in-memory buffer */
  63.155 +  JDIMENSION rows_in_array;	/* total virtual array height */
  63.156 +  JDIMENSION samplesperrow;	/* width of array (and of memory buffer) */
  63.157 +  JDIMENSION maxaccess;		/* max rows accessed by access_virt_sarray */
  63.158 +  JDIMENSION rows_in_mem;	/* height of memory buffer */
  63.159 +  JDIMENSION rowsperchunk;	/* allocation chunk size in mem_buffer */
  63.160 +  JDIMENSION cur_start_row;	/* first logical row # in the buffer */
  63.161 +  JDIMENSION first_undef_row;	/* row # of first uninitialized row */
  63.162 +  boolean pre_zero;		/* pre-zero mode requested? */
  63.163 +  boolean dirty;		/* do current buffer contents need written? */
  63.164 +  boolean b_s_open;		/* is backing-store data valid? */
  63.165 +  jvirt_sarray_ptr next;	/* link to next virtual sarray control block */
  63.166 +  backing_store_info b_s_info;	/* System-dependent control info */
  63.167 +};
  63.168 +
  63.169 +struct jvirt_barray_control {
  63.170 +  JBLOCKARRAY mem_buffer;	/* => the in-memory buffer */
  63.171 +  JDIMENSION rows_in_array;	/* total virtual array height */
  63.172 +  JDIMENSION blocksperrow;	/* width of array (and of memory buffer) */
  63.173 +  JDIMENSION maxaccess;		/* max rows accessed by access_virt_barray */
  63.174 +  JDIMENSION rows_in_mem;	/* height of memory buffer */
  63.175 +  JDIMENSION rowsperchunk;	/* allocation chunk size in mem_buffer */
  63.176 +  JDIMENSION cur_start_row;	/* first logical row # in the buffer */
  63.177 +  JDIMENSION first_undef_row;	/* row # of first uninitialized row */
  63.178 +  boolean pre_zero;		/* pre-zero mode requested? */
  63.179 +  boolean dirty;		/* do current buffer contents need written? */
  63.180 +  boolean b_s_open;		/* is backing-store data valid? */
  63.181 +  jvirt_barray_ptr next;	/* link to next virtual barray control block */
  63.182 +  backing_store_info b_s_info;	/* System-dependent control info */
  63.183 +};
  63.184 +
  63.185 +
  63.186 +#ifdef MEM_STATS		/* optional extra stuff for statistics */
  63.187 +
  63.188 +LOCAL(void)
  63.189 +print_mem_stats (j_common_ptr cinfo, int pool_id)
  63.190 +{
  63.191 +  my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
  63.192 +  small_pool_ptr shdr_ptr;
  63.193 +  large_pool_ptr lhdr_ptr;
  63.194 +
  63.195 +  /* Since this is only a debugging stub, we can cheat a little by using
  63.196 +   * fprintf directly rather than going through the trace message code.
  63.197 +   * This is helpful because message parm array can't handle longs.
  63.198 +   */
  63.199 +  fprintf(stderr, "Freeing pool %d, total space = %ld\n",
  63.200 +	  pool_id, mem->total_space_allocated);
  63.201 +
  63.202 +  for (lhdr_ptr = mem->large_list[pool_id]; lhdr_ptr != NULL;
  63.203 +       lhdr_ptr = lhdr_ptr->hdr.next) {
  63.204 +    fprintf(stderr, "  Large chunk used %ld\n",
  63.205 +	    (long) lhdr_ptr->hdr.bytes_used);
  63.206 +  }
  63.207 +
  63.208 +  for (shdr_ptr = mem->small_list[pool_id]; shdr_ptr != NULL;
  63.209 +       shdr_ptr = shdr_ptr->hdr.next) {
  63.210 +    fprintf(stderr, "  Small chunk used %ld free %ld\n",
  63.211 +	    (long) shdr_ptr->hdr.bytes_used,
  63.212 +	    (long) shdr_ptr->hdr.bytes_left);
  63.213 +  }
  63.214 +}
  63.215 +
  63.216 +#endif /* MEM_STATS */
  63.217 +
  63.218 +
  63.219 +LOCAL(void)
  63.220 +out_of_memory (j_common_ptr cinfo, int which)
  63.221 +/* Report an out-of-memory error and stop execution */
  63.222 +/* If we compiled MEM_STATS support, report alloc requests before dying */
  63.223 +{
  63.224 +#ifdef MEM_STATS
  63.225 +  cinfo->err->trace_level = 2;	/* force self_destruct to report stats */
  63.226 +#endif
  63.227 +  ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, which);
  63.228 +}
  63.229 +
  63.230 +
  63.231 +/*
  63.232 + * Allocation of "small" objects.
  63.233 + *
  63.234 + * For these, we use pooled storage.  When a new pool must be created,
  63.235 + * we try to get enough space for the current request plus a "slop" factor,
  63.236 + * where the slop will be the amount of leftover space in the new pool.
  63.237 + * The speed vs. space tradeoff is largely determined by the slop values.
  63.238 + * A different slop value is provided for each pool class (lifetime),
  63.239 + * and we also distinguish the first pool of a class from later ones.
  63.240 + * NOTE: the values given work fairly well on both 16- and 32-bit-int
  63.241 + * machines, but may be too small if longs are 64 bits or more.
  63.242 + */
  63.243 +
  63.244 +static const size_t first_pool_slop[JPOOL_NUMPOOLS] = 
  63.245 +{
  63.246 +	1600,			/* first PERMANENT pool */
  63.247 +	16000			/* first IMAGE pool */
  63.248 +};
  63.249 +
  63.250 +static const size_t extra_pool_slop[JPOOL_NUMPOOLS] = 
  63.251 +{
  63.252 +	0,			/* additional PERMANENT pools */
  63.253 +	5000			/* additional IMAGE pools */
  63.254 +};
  63.255 +
  63.256 +#define MIN_SLOP  50		/* greater than 0 to avoid futile looping */
  63.257 +
  63.258 +
  63.259 +METHODDEF(void *)
  63.260 +alloc_small (j_common_ptr cinfo, int pool_id, size_t sizeofobject)
  63.261 +/* Allocate a "small" object */
  63.262 +{
  63.263 +  my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
  63.264 +  small_pool_ptr hdr_ptr, prev_hdr_ptr;
  63.265 +  char * data_ptr;
  63.266 +  size_t odd_bytes, min_request, slop;
  63.267 +
  63.268 +  /* Check for unsatisfiable request (do now to ensure no overflow below) */
  63.269 +  if (sizeofobject > (size_t) (MAX_ALLOC_CHUNK-SIZEOF(small_pool_hdr)))
  63.270 +    out_of_memory(cinfo, 1);	/* request exceeds malloc's ability */
  63.271 +
  63.272 +  /* Round up the requested size to a multiple of SIZEOF(ALIGN_TYPE) */
  63.273 +  odd_bytes = sizeofobject % SIZEOF(ALIGN_TYPE);
  63.274 +  if (odd_bytes > 0)
  63.275 +    sizeofobject += SIZEOF(ALIGN_TYPE) - odd_bytes;
  63.276 +
  63.277 +  /* See if space is available in any existing pool */
  63.278 +  if (pool_id < 0 || pool_id >= JPOOL_NUMPOOLS)
  63.279 +    ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id);	/* safety check */
  63.280 +  prev_hdr_ptr = NULL;
  63.281 +  hdr_ptr = mem->small_list[pool_id];
  63.282 +  while (hdr_ptr != NULL) {
  63.283 +    if (hdr_ptr->hdr.bytes_left >= sizeofobject)
  63.284 +      break;			/* found pool with enough space */
  63.285 +    prev_hdr_ptr = hdr_ptr;
  63.286 +    hdr_ptr = hdr_ptr->hdr.next;
  63.287 +  }
  63.288 +
  63.289 +  /* Time to make a new pool? */
  63.290 +  if (hdr_ptr == NULL) {
  63.291 +    /* min_request is what we need now, slop is what will be leftover */
  63.292 +    min_request = sizeofobject + SIZEOF(small_pool_hdr);
  63.293 +    if (prev_hdr_ptr == NULL)	/* first pool in class? */
  63.294 +      slop = first_pool_slop[pool_id];
  63.295 +    else
  63.296 +      slop = extra_pool_slop[pool_id];
  63.297 +    /* Don't ask for more than MAX_ALLOC_CHUNK */
  63.298 +    if (slop > (size_t) (MAX_ALLOC_CHUNK-min_request))
  63.299 +      slop = (size_t) (MAX_ALLOC_CHUNK-min_request);
  63.300 +    /* Try to get space, if fail reduce slop and try again */
  63.301 +    for (;;) {
  63.302 +      hdr_ptr = (small_pool_ptr) jpeg_get_small(cinfo, min_request + slop);
  63.303 +      if (hdr_ptr != NULL)
  63.304 +	break;
  63.305 +      slop /= 2;
  63.306 +      if (slop < MIN_SLOP)	/* give up when it gets real small */
  63.307 +	out_of_memory(cinfo, 2); /* jpeg_get_small failed */
  63.308 +    }
  63.309 +    mem->total_space_allocated += min_request + slop;
  63.310 +    /* Success, initialize the new pool header and add to end of list */
  63.311 +    hdr_ptr->hdr.next = NULL;
  63.312 +    hdr_ptr->hdr.bytes_used = 0;
  63.313 +    hdr_ptr->hdr.bytes_left = sizeofobject + slop;
  63.314 +    if (prev_hdr_ptr == NULL)	/* first pool in class? */
  63.315 +      mem->small_list[pool_id] = hdr_ptr;
  63.316 +    else
  63.317 +      prev_hdr_ptr->hdr.next = hdr_ptr;
  63.318 +  }
  63.319 +
  63.320 +  /* OK, allocate the object from the current pool */
  63.321 +  data_ptr = (char *) (hdr_ptr + 1); /* point to first data byte in pool */
  63.322 +  data_ptr += hdr_ptr->hdr.bytes_used; /* point to place for object */
  63.323 +  hdr_ptr->hdr.bytes_used += sizeofobject;
  63.324 +  hdr_ptr->hdr.bytes_left -= sizeofobject;
  63.325 +
  63.326 +  return (void *) data_ptr;
  63.327 +}
  63.328 +
  63.329 +
  63.330 +/*
  63.331 + * Allocation of "large" objects.
  63.332 + *
  63.333 + * The external semantics of these are the same as "small" objects,
  63.334 + * except that FAR pointers are used on 80x86.  However the pool
  63.335 + * management heuristics are quite different.  We assume that each
  63.336 + * request is large enough that it may as well be passed directly to
  63.337 + * jpeg_get_large; the pool management just links everything together
  63.338 + * so that we can free it all on demand.
  63.339 + * Note: the major use of "large" objects is in JSAMPARRAY and JBLOCKARRAY
  63.340 + * structures.  The routines that create these structures (see below)
  63.341 + * deliberately bunch rows together to ensure a large request size.
  63.342 + */
  63.343 +
  63.344 +METHODDEF(void FAR *)
  63.345 +alloc_large (j_common_ptr cinfo, int pool_id, size_t sizeofobject)
  63.346 +/* Allocate a "large" object */
  63.347 +{
  63.348 +  my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
  63.349 +  large_pool_ptr hdr_ptr;
  63.350 +  size_t odd_bytes;
  63.351 +
  63.352 +  /* Check for unsatisfiable request (do now to ensure no overflow below) */
  63.353 +  if (sizeofobject > (size_t) (MAX_ALLOC_CHUNK-SIZEOF(large_pool_hdr)))
  63.354 +    out_of_memory(cinfo, 3);	/* request exceeds malloc's ability */
  63.355 +
  63.356 +  /* Round up the requested size to a multiple of SIZEOF(ALIGN_TYPE) */
  63.357 +  odd_bytes = sizeofobject % SIZEOF(ALIGN_TYPE);
  63.358 +  if (odd_bytes > 0)
  63.359 +    sizeofobject += SIZEOF(ALIGN_TYPE) - odd_bytes;
  63.360 +
  63.361 +  /* Always make a new pool */
  63.362 +  if (pool_id < 0 || pool_id >= JPOOL_NUMPOOLS)
  63.363 +    ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id);	/* safety check */
  63.364 +
  63.365 +  hdr_ptr = (large_pool_ptr) jpeg_get_large(cinfo, sizeofobject +
  63.366 +					    SIZEOF(large_pool_hdr));
  63.367 +  if (hdr_ptr == NULL)
  63.368 +    out_of_memory(cinfo, 4);	/* jpeg_get_large failed */
  63.369 +  mem->total_space_allocated += sizeofobject + SIZEOF(large_pool_hdr);
  63.370 +
  63.371 +  /* Success, initialize the new pool header and add to list */
  63.372 +  hdr_ptr->hdr.next = mem->large_list[pool_id];
  63.373 +  /* We maintain space counts in each pool header for statistical purposes,
  63.374 +   * even though they are not needed for allocation.
  63.375 +   */
  63.376 +  hdr_ptr->hdr.bytes_used = sizeofobject;
  63.377 +  hdr_ptr->hdr.bytes_left = 0;
  63.378 +  mem->large_list[pool_id] = hdr_ptr;
  63.379 +
  63.380 +  return (void FAR *) (hdr_ptr + 1); /* point to first data byte in pool */
  63.381 +}
  63.382 +
  63.383 +
  63.384 +/*
  63.385 + * Creation of 2-D sample arrays.
  63.386 + * The pointers are in near heap, the samples themselves in FAR heap.
  63.387 + *
  63.388 + * To minimize allocation overhead and to allow I/O of large contiguous
  63.389 + * blocks, we allocate the sample rows in groups of as many rows as possible
  63.390 + * without exceeding MAX_ALLOC_CHUNK total bytes per allocation request.
  63.391 + * NB: the virtual array control routines, later in this file, know about
  63.392 + * this chunking of rows.  The rowsperchunk value is left in the mem manager
  63.393 + * object so that it can be saved away if this sarray is the workspace for
  63.394 + * a virtual array.
  63.395 + */
  63.396 +
  63.397 +METHODDEF(JSAMPARRAY)
  63.398 +alloc_sarray (j_common_ptr cinfo, int pool_id,
  63.399 +	      JDIMENSION samplesperrow, JDIMENSION numrows)
  63.400 +/* Allocate a 2-D sample array */
  63.401 +{
  63.402 +  my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
  63.403 +  JSAMPARRAY result;
  63.404 +  JSAMPROW workspace;
  63.405 +  JDIMENSION rowsperchunk, currow, i;
  63.406 +  long ltemp;
  63.407 +
  63.408 +  /* Calculate max # of rows allowed in one allocation chunk */
  63.409 +  ltemp = (MAX_ALLOC_CHUNK-SIZEOF(large_pool_hdr)) /
  63.410 +	  ((long) samplesperrow * SIZEOF(JSAMPLE));
  63.411 +  if (ltemp <= 0)
  63.412 +    ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
  63.413 +  if (ltemp < (long) numrows)
  63.414 +    rowsperchunk = (JDIMENSION) ltemp;
  63.415 +  else
  63.416 +    rowsperchunk = numrows;
  63.417 +  mem->last_rowsperchunk = rowsperchunk;
  63.418 +
  63.419 +  /* Get space for row pointers (small object) */
  63.420 +  result = (JSAMPARRAY) alloc_small(cinfo, pool_id,
  63.421 +				    (size_t) (numrows * SIZEOF(JSAMPROW)));
  63.422 +
  63.423 +  /* Get the rows themselves (large objects) */
  63.424 +  currow = 0;
  63.425 +  while (currow < numrows) {
  63.426 +    rowsperchunk = MIN(rowsperchunk, numrows - currow);
  63.427 +    workspace = (JSAMPROW) alloc_large(cinfo, pool_id,
  63.428 +	(size_t) ((size_t) rowsperchunk * (size_t) samplesperrow
  63.429 +		  * SIZEOF(JSAMPLE)));
  63.430 +    for (i = rowsperchunk; i > 0; i--) {
  63.431 +      result[currow++] = workspace;
  63.432 +      workspace += samplesperrow;
  63.433 +    }
  63.434 +  }
  63.435 +
  63.436 +  return result;
  63.437 +}
  63.438 +
  63.439 +
  63.440 +/*
  63.441 + * Creation of 2-D coefficient-block arrays.
  63.442 + * This is essentially the same as the code for sample arrays, above.
  63.443 + */
  63.444 +
  63.445 +METHODDEF(JBLOCKARRAY)
  63.446 +alloc_barray (j_common_ptr cinfo, int pool_id,
  63.447 +	      JDIMENSION blocksperrow, JDIMENSION numrows)
  63.448 +/* Allocate a 2-D coefficient-block array */
  63.449 +{
  63.450 +  my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
  63.451 +  JBLOCKARRAY result;
  63.452 +  JBLOCKROW workspace;
  63.453 +  JDIMENSION rowsperchunk, currow, i;
  63.454 +  long ltemp;
  63.455 +
  63.456 +  /* Calculate max # of rows allowed in one allocation chunk */
  63.457 +  ltemp = (MAX_ALLOC_CHUNK-SIZEOF(large_pool_hdr)) /
  63.458 +	  ((long) blocksperrow * SIZEOF(JBLOCK));
  63.459 +  if (ltemp <= 0)
  63.460 +    ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
  63.461 +  if (ltemp < (long) numrows)
  63.462 +    rowsperchunk = (JDIMENSION) ltemp;
  63.463 +  else
  63.464 +    rowsperchunk = numrows;
  63.465 +  mem->last_rowsperchunk = rowsperchunk;
  63.466 +
  63.467 +  /* Get space for row pointers (small object) */
  63.468 +  result = (JBLOCKARRAY) alloc_small(cinfo, pool_id,
  63.469 +				     (size_t) (numrows * SIZEOF(JBLOCKROW)));
  63.470 +
  63.471 +  /* Get the rows themselves (large objects) */
  63.472 +  currow = 0;
  63.473 +  while (currow < numrows) {
  63.474 +    rowsperchunk = MIN(rowsperchunk, numrows - currow);
  63.475 +    workspace = (JBLOCKROW) alloc_large(cinfo, pool_id,
  63.476 +	(size_t) ((size_t) rowsperchunk * (size_t) blocksperrow
  63.477 +		  * SIZEOF(JBLOCK)));
  63.478 +    for (i = rowsperchunk; i > 0; i--) {
  63.479 +      result[currow++] = workspace;
  63.480 +      workspace += blocksperrow;
  63.481 +    }
  63.482 +  }
  63.483 +
  63.484 +  return result;
  63.485 +}
  63.486 +
  63.487 +
  63.488 +/*
  63.489 + * About virtual array management:
  63.490 + *
  63.491 + * The above "normal" array routines are only used to allocate strip buffers
  63.492 + * (as wide as the image, but just a few rows high).  Full-image-sized buffers
  63.493 + * are handled as "virtual" arrays.  The array is still accessed a strip at a
  63.494 + * time, but the memory manager must save the whole array for repeated
  63.495 + * accesses.  The intended implementation is that there is a strip buffer in
  63.496 + * memory (as high as is possible given the desired memory limit), plus a
  63.497 + * backing file that holds the rest of the array.
  63.498 + *
  63.499 + * The request_virt_array routines are told the total size of the image and
  63.500 + * the maximum number of rows that will be accessed at once.  The in-memory
  63.501 + * buffer must be at least as large as the maxaccess value.
  63.502 + *
  63.503 + * The request routines create control blocks but not the in-memory buffers.
  63.504 + * That is postponed until realize_virt_arrays is called.  At that time the
  63.505 + * total amount of space needed is known (approximately, anyway), so free
  63.506 + * memory can be divided up fairly.
  63.507 + *
  63.508 + * The access_virt_array routines are responsible for making a specific strip
  63.509 + * area accessible (after reading or writing the backing file, if necessary).
  63.510 + * Note that the access routines are told whether the caller intends to modify
  63.511 + * the accessed strip; during a read-only pass this saves having to rewrite
  63.512 + * data to disk.  The access routines are also responsible for pre-zeroing
  63.513 + * any newly accessed rows, if pre-zeroing was requested.
  63.514 + *
  63.515 + * In current usage, the access requests are usually for nonoverlapping
  63.516 + * strips; that is, successive access start_row numbers differ by exactly
  63.517 + * num_rows = maxaccess.  This means we can get good performance with simple
  63.518 + * buffer dump/reload logic, by making the in-memory buffer be a multiple
  63.519 + * of the access height; then there will never be accesses across bufferload
  63.520 + * boundaries.  The code will still work with overlapping access requests,
  63.521 + * but it doesn't handle bufferload overlaps very efficiently.
  63.522 + */
  63.523 +
  63.524 +
  63.525 +METHODDEF(jvirt_sarray_ptr)
  63.526 +request_virt_sarray (j_common_ptr cinfo, int pool_id, boolean pre_zero,
  63.527 +		     JDIMENSION samplesperrow, JDIMENSION numrows,
  63.528 +		     JDIMENSION maxaccess)
  63.529 +/* Request a virtual 2-D sample array */
  63.530 +{
  63.531 +  my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
  63.532 +  jvirt_sarray_ptr result;
  63.533 +
  63.534 +  /* Only IMAGE-lifetime virtual arrays are currently supported */
  63.535 +  if (pool_id != JPOOL_IMAGE)
  63.536 +    ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id);	/* safety check */
  63.537 +
  63.538 +  /* get control block */
  63.539 +  result = (jvirt_sarray_ptr) alloc_small(cinfo, pool_id,
  63.540 +					  SIZEOF(struct jvirt_sarray_control));
  63.541 +
  63.542 +  result->mem_buffer = NULL;	/* marks array not yet realized */
  63.543 +  result->rows_in_array = numrows;
  63.544 +  result->samplesperrow = samplesperrow;
  63.545 +  result->maxaccess = maxaccess;
  63.546 +  result->pre_zero = pre_zero;
  63.547 +  result->b_s_open = FALSE;	/* no associated backing-store object */
  63.548 +  result->next = mem->virt_sarray_list; /* add to list of virtual arrays */
  63.549 +  mem->virt_sarray_list = result;
  63.550 +
  63.551 +  return result;
  63.552 +}
  63.553 +
  63.554 +
  63.555 +METHODDEF(jvirt_barray_ptr)
  63.556 +request_virt_barray (j_common_ptr cinfo, int pool_id, boolean pre_zero,
  63.557 +		     JDIMENSION blocksperrow, JDIMENSION numrows,
  63.558 +		     JDIMENSION maxaccess)
  63.559 +/* Request a virtual 2-D coefficient-block array */
  63.560 +{
  63.561 +  my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
  63.562 +  jvirt_barray_ptr result;
  63.563 +
  63.564 +  /* Only IMAGE-lifetime virtual arrays are currently supported */
  63.565 +  if (pool_id != JPOOL_IMAGE)
  63.566 +    ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id);	/* safety check */
  63.567 +
  63.568 +  /* get control block */
  63.569 +  result = (jvirt_barray_ptr) alloc_small(cinfo, pool_id,
  63.570 +					  SIZEOF(struct jvirt_barray_control));
  63.571 +
  63.572 +  result->mem_buffer = NULL;	/* marks array not yet realized */
  63.573 +  result->rows_in_array = numrows;
  63.574 +  result->blocksperrow = blocksperrow;
  63.575 +  result->maxaccess = maxaccess;
  63.576 +  result->pre_zero = pre_zero;
  63.577 +  result->b_s_open = FALSE;	/* no associated backing-store object */
  63.578 +  result->next = mem->virt_barray_list; /* add to list of virtual arrays */
  63.579 +  mem->virt_barray_list = result;
  63.580 +
  63.581 +  return result;
  63.582 +}
  63.583 +
  63.584 +
  63.585 +METHODDEF(void)
  63.586 +realize_virt_arrays (j_common_ptr cinfo)
  63.587 +/* Allocate the in-memory buffers for any unrealized virtual arrays */
  63.588 +{
  63.589 +  my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
  63.590 +  long space_per_minheight, maximum_space, avail_mem;
  63.591 +  long minheights, max_minheights;
  63.592 +  jvirt_sarray_ptr sptr;
  63.593 +  jvirt_barray_ptr bptr;
  63.594 +
  63.595 +  /* Compute the minimum space needed (maxaccess rows in each buffer)
  63.596 +   * and the maximum space needed (full image height in each buffer).
  63.597 +   * These may be of use to the system-dependent jpeg_mem_available routine.
  63.598 +   */
  63.599 +  space_per_minheight = 0;
  63.600 +  maximum_space = 0;
  63.601 +  for (sptr = mem->virt_sarray_list; sptr != NULL; sptr = sptr->next) {
  63.602 +    if (sptr->mem_buffer == NULL) { /* if not realized yet */
  63.603 +      space_per_minheight += (long) sptr->maxaccess *
  63.604 +			     (long) sptr->samplesperrow * SIZEOF(JSAMPLE);
  63.605 +      maximum_space += (long) sptr->rows_in_array *
  63.606 +		       (long) sptr->samplesperrow * SIZEOF(JSAMPLE);
  63.607 +    }
  63.608 +  }
  63.609 +  for (bptr = mem->virt_barray_list; bptr != NULL; bptr = bptr->next) {
  63.610 +    if (bptr->mem_buffer == NULL) { /* if not realized yet */
  63.611 +      space_per_minheight += (long) bptr->maxaccess *
  63.612 +			     (long) bptr->blocksperrow * SIZEOF(JBLOCK);
  63.613 +      maximum_space += (long) bptr->rows_in_array *
  63.614 +		       (long) bptr->blocksperrow * SIZEOF(JBLOCK);
  63.615 +    }
  63.616 +  }
  63.617 +
  63.618 +  if (space_per_minheight <= 0)
  63.619 +    return;			/* no unrealized arrays, no work */
  63.620 +
  63.621 +  /* Determine amount of memory to actually use; this is system-dependent. */
  63.622 +  avail_mem = jpeg_mem_available(cinfo, space_per_minheight, maximum_space,
  63.623 +				 mem->total_space_allocated);
  63.624 +
  63.625 +  /* If the maximum space needed is available, make all the buffers full
  63.626 +   * height; otherwise parcel it out with the same number of minheights
  63.627 +   * in each buffer.
  63.628 +   */
  63.629 +  if (avail_mem >= maximum_space)
  63.630 +    max_minheights = 1000000000L;
  63.631 +  else {
  63.632 +    max_minheights = avail_mem / space_per_minheight;
  63.633 +    /* If there doesn't seem to be enough space, try to get the minimum
  63.634 +     * anyway.  This allows a "stub" implementation of jpeg_mem_available().
  63.635 +     */
  63.636 +    if (max_minheights <= 0)
  63.637 +      max_minheights = 1;
  63.638 +  }
  63.639 +
  63.640 +  /* Allocate the in-memory buffers and initialize backing store as needed. */
  63.641 +
  63.642 +  for (sptr = mem->virt_sarray_list; sptr != NULL; sptr = sptr->next) {
  63.643 +    if (sptr->mem_buffer == NULL) { /* if not realized yet */
  63.644 +      minheights = ((long) sptr->rows_in_array - 1L) / sptr->maxaccess + 1L;
  63.645 +      if (minheights <= max_minheights) {
  63.646 +	/* This buffer fits in memory */
  63.647 +	sptr->rows_in_mem = sptr->rows_in_array;
  63.648 +      } else {
  63.649 +	/* It doesn't fit in memory, create backing store. */
  63.650 +	sptr->rows_in_mem = (JDIMENSION) (max_minheights * sptr->maxaccess);
  63.651 +	jpeg_open_backing_store(cinfo, & sptr->b_s_info,
  63.652 +				(long) sptr->rows_in_array *
  63.653 +				(long) sptr->samplesperrow *
  63.654 +				(long) SIZEOF(JSAMPLE));
  63.655 +	sptr->b_s_open = TRUE;
  63.656 +      }
  63.657 +      sptr->mem_buffer = alloc_sarray(cinfo, JPOOL_IMAGE,
  63.658 +				      sptr->samplesperrow, sptr->rows_in_mem);
  63.659 +      sptr->rowsperchunk = mem->last_rowsperchunk;
  63.660 +      sptr->cur_start_row = 0;
  63.661 +      sptr->first_undef_row = 0;
  63.662 +      sptr->dirty = FALSE;
  63.663 +    }
  63.664 +  }
  63.665 +
  63.666 +  for (bptr = mem->virt_barray_list; bptr != NULL; bptr = bptr->next) {
  63.667 +    if (bptr->mem_buffer == NULL) { /* if not realized yet */
  63.668 +      minheights = ((long) bptr->rows_in_array - 1L) / bptr->maxaccess + 1L;
  63.669 +      if (minheights <= max_minheights) {
  63.670 +	/* This buffer fits in memory */
  63.671 +	bptr->rows_in_mem = bptr->rows_in_array;
  63.672 +      } else {
  63.673 +	/* It doesn't fit in memory, create backing store. */
  63.674 +	bptr->rows_in_mem = (JDIMENSION) (max_minheights * bptr->maxaccess);
  63.675 +	jpeg_open_backing_store(cinfo, & bptr->b_s_info,
  63.676 +				(long) bptr->rows_in_array *
  63.677 +				(long) bptr->blocksperrow *
  63.678 +				(long) SIZEOF(JBLOCK));
  63.679 +	bptr->b_s_open = TRUE;
  63.680 +      }
  63.681 +      bptr->mem_buffer = alloc_barray(cinfo, JPOOL_IMAGE,
  63.682 +				      bptr->blocksperrow, bptr->rows_in_mem);
  63.683 +      bptr->rowsperchunk = mem->last_rowsperchunk;
  63.684 +      bptr->cur_start_row = 0;
  63.685 +      bptr->first_undef_row = 0;
  63.686 +      bptr->dirty = FALSE;
  63.687 +    }
  63.688 +  }
  63.689 +}
  63.690 +
  63.691 +
  63.692 +LOCAL(void)
  63.693 +do_sarray_io (j_common_ptr cinfo, jvirt_sarray_ptr ptr, boolean writing)
  63.694 +/* Do backing store read or write of a virtual sample array */
  63.695 +{
  63.696 +  long bytesperrow, file_offset, byte_count, rows, thisrow, i;
  63.697 +
  63.698 +  bytesperrow = (long) ptr->samplesperrow * SIZEOF(JSAMPLE);
  63.699 +  file_offset = ptr->cur_start_row * bytesperrow;
  63.700 +  /* Loop to read or write each allocation chunk in mem_buffer */
  63.701 +  for (i = 0; i < (long) ptr->rows_in_mem; i += ptr->rowsperchunk) {
  63.702 +    /* One chunk, but check for short chunk at end of buffer */
  63.703 +    rows = MIN((long) ptr->rowsperchunk, (long) ptr->rows_in_mem - i);
  63.704 +    /* Transfer no more than is currently defined */
  63.705 +    thisrow = (long) ptr->cur_start_row + i;
  63.706 +    rows = MIN(rows, (long) ptr->first_undef_row - thisrow);
  63.707 +    /* Transfer no more than fits in file */
  63.708 +    rows = MIN(rows, (long) ptr->rows_in_array - thisrow);
  63.709 +    if (rows <= 0)		/* this chunk might be past end of file! */
  63.710 +      break;
  63.711 +    byte_count = rows * bytesperrow;
  63.712 +    if (writing)
  63.713 +      (*ptr->b_s_info.write_backing_store) (cinfo, & ptr->b_s_info,
  63.714 +					    (void FAR *) ptr->mem_buffer[i],
  63.715 +					    file_offset, byte_count);
  63.716 +    else
  63.717 +      (*ptr->b_s_info.read_backing_store) (cinfo, & ptr->b_s_info,
  63.718 +					   (void FAR *) ptr->mem_buffer[i],
  63.719 +					   file_offset, byte_count);
  63.720 +    file_offset += byte_count;
  63.721 +  }
  63.722 +}
  63.723 +
  63.724 +
  63.725 +LOCAL(void)
  63.726 +do_barray_io (j_common_ptr cinfo, jvirt_barray_ptr ptr, boolean writing)
  63.727 +/* Do backing store read or write of a virtual coefficient-block array */
  63.728 +{
  63.729 +  long bytesperrow, file_offset, byte_count, rows, thisrow, i;
  63.730 +
  63.731 +  bytesperrow = (long) ptr->blocksperrow * SIZEOF(JBLOCK);
  63.732 +  file_offset = ptr->cur_start_row * bytesperrow;
  63.733 +  /* Loop to read or write each allocation chunk in mem_buffer */
  63.734 +  for (i = 0; i < (long) ptr->rows_in_mem; i += ptr->rowsperchunk) {
  63.735 +    /* One chunk, but check for short chunk at end of buffer */
  63.736 +    rows = MIN((long) ptr->rowsperchunk, (long) ptr->rows_in_mem - i);
  63.737 +    /* Transfer no more than is currently defined */
  63.738 +    thisrow = (long) ptr->cur_start_row + i;
  63.739 +    rows = MIN(rows, (long) ptr->first_undef_row - thisrow);
  63.740 +    /* Transfer no more than fits in file */
  63.741 +    rows = MIN(rows, (long) ptr->rows_in_array - thisrow);
  63.742 +    if (rows <= 0)		/* this chunk might be past end of file! */
  63.743 +      break;
  63.744 +    byte_count = rows * bytesperrow;
  63.745 +    if (writing)
  63.746 +      (*ptr->b_s_info.write_backing_store) (cinfo, & ptr->b_s_info,
  63.747 +					    (void FAR *) ptr->mem_buffer[i],
  63.748 +					    file_offset, byte_count);
  63.749 +    else
  63.750 +      (*ptr->b_s_info.read_backing_store) (cinfo, & ptr->b_s_info,
  63.751 +					   (void FAR *) ptr->mem_buffer[i],
  63.752 +					   file_offset, byte_count);
  63.753 +    file_offset += byte_count;
  63.754 +  }
  63.755 +}
  63.756 +
  63.757 +
  63.758 +METHODDEF(JSAMPARRAY)
  63.759 +access_virt_sarray (j_common_ptr cinfo, jvirt_sarray_ptr ptr,
  63.760 +		    JDIMENSION start_row, JDIMENSION num_rows,
  63.761 +		    boolean writable)
  63.762 +/* Access the part of a virtual sample array starting at start_row */
  63.763 +/* and extending for num_rows rows.  writable is true if  */
  63.764 +/* caller intends to modify the accessed area. */
  63.765 +{
  63.766 +  JDIMENSION end_row = start_row + num_rows;
  63.767 +  JDIMENSION undef_row;
  63.768 +
  63.769 +  /* debugging check */
  63.770 +  if (end_row > ptr->rows_in_array || num_rows > ptr->maxaccess ||
  63.771 +      ptr->mem_buffer == NULL)
  63.772 +    ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
  63.773 +
  63.774 +  /* Make the desired part of the virtual array accessible */
  63.775 +  if (start_row < ptr->cur_start_row ||
  63.776 +      end_row > ptr->cur_start_row+ptr->rows_in_mem) {
  63.777 +    if (! ptr->b_s_open)
  63.778 +      ERREXIT(cinfo, JERR_VIRTUAL_BUG);
  63.779 +    /* Flush old buffer contents if necessary */
  63.780 +    if (ptr->dirty) {
  63.781 +      do_sarray_io(cinfo, ptr, TRUE);
  63.782 +      ptr->dirty = FALSE;
  63.783 +    }
  63.784 +    /* Decide what part of virtual array to access.
  63.785 +     * Algorithm: if target address > current window, assume forward scan,
  63.786 +     * load starting at target address.  If target address < current window,
  63.787 +     * assume backward scan, load so that target area is top of window.
  63.788 +     * Note that when switching from forward write to forward read, will have
  63.789 +     * start_row = 0, so the limiting case applies and we load from 0 anyway.
  63.790 +     */
  63.791 +    if (start_row > ptr->cur_start_row) {
  63.792 +      ptr->cur_start_row = start_row;
  63.793 +    } else {
  63.794 +      /* use long arithmetic here to avoid overflow & unsigned problems */
  63.795 +      long ltemp;
  63.796 +
  63.797 +      ltemp = (long) end_row - (long) ptr->rows_in_mem;
  63.798 +      if (ltemp < 0)
  63.799 +	ltemp = 0;		/* don't fall off front end of file */
  63.800 +      ptr->cur_start_row = (JDIMENSION) ltemp;
  63.801 +    }
  63.802 +    /* Read in the selected part of the array.
  63.803 +     * During the initial write pass, we will do no actual read
  63.804 +     * because the selected part is all undefined.
  63.805 +     */
  63.806 +    do_sarray_io(cinfo, ptr, FALSE);
  63.807 +  }
  63.808 +  /* Ensure the accessed part of the array is defined; prezero if needed.
  63.809 +   * To improve locality of access, we only prezero the part of the array
  63.810 +   * that the caller is about to access, not the entire in-memory array.
  63.811 +   */
  63.812 +  if (ptr->first_undef_row < end_row) {
  63.813 +    if (ptr->first_undef_row < start_row) {
  63.814 +      if (writable)		/* writer skipped over a section of array */
  63.815 +	ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
  63.816 +      undef_row = start_row;	/* but reader is allowed to read ahead */
  63.817 +    } else {
  63.818 +      undef_row = ptr->first_undef_row;
  63.819 +    }
  63.820 +    if (writable)
  63.821 +      ptr->first_undef_row = end_row;
  63.822 +    if (ptr->pre_zero) {
  63.823 +      size_t bytesperrow = (size_t) ptr->samplesperrow * SIZEOF(JSAMPLE);
  63.824 +      undef_row -= ptr->cur_start_row; /* make indexes relative to buffer */
  63.825 +      end_row -= ptr->cur_start_row;
  63.826 +      while (undef_row < end_row) {
  63.827 +	jzero_far((void FAR *) ptr->mem_buffer[undef_row], bytesperrow);
  63.828 +	undef_row++;
  63.829 +      }
  63.830 +    } else {
  63.831 +      if (! writable)		/* reader looking at undefined data */
  63.832 +	ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
  63.833 +    }
  63.834 +  }
  63.835 +  /* Flag the buffer dirty if caller will write in it */
  63.836 +  if (writable)
  63.837 +    ptr->dirty = TRUE;
  63.838 +  /* Return address of proper part of the buffer */
  63.839 +  return ptr->mem_buffer + (start_row - ptr->cur_start_row);
  63.840 +}
  63.841 +
  63.842 +
  63.843 +METHODDEF(JBLOCKARRAY)
  63.844 +access_virt_barray (j_common_ptr cinfo, jvirt_barray_ptr ptr,
  63.845 +		    JDIMENSION start_row, JDIMENSION num_rows,
  63.846 +		    boolean writable)
  63.847 +/* Access the part of a virtual block array starting at start_row */
  63.848 +/* and extending for num_rows rows.  writable is true if  */
  63.849 +/* caller intends to modify the accessed area. */
  63.850 +{
  63.851 +  JDIMENSION end_row = start_row + num_rows;
  63.852 +  JDIMENSION undef_row;
  63.853 +
  63.854 +  /* debugging check */
  63.855 +  if (end_row > ptr->rows_in_array || num_rows > ptr->maxaccess ||
  63.856 +      ptr->mem_buffer == NULL)
  63.857 +    ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
  63.858 +
  63.859 +  /* Make the desired part of the virtual array accessible */
  63.860 +  if (start_row < ptr->cur_start_row ||
  63.861 +      end_row > ptr->cur_start_row+ptr->rows_in_mem) {
  63.862 +    if (! ptr->b_s_open)
  63.863 +      ERREXIT(cinfo, JERR_VIRTUAL_BUG);
  63.864 +    /* Flush old buffer contents if necessary */
  63.865 +    if (ptr->dirty) {
  63.866 +      do_barray_io(cinfo, ptr, TRUE);
  63.867 +      ptr->dirty = FALSE;
  63.868 +    }
  63.869 +    /* Decide what part of virtual array to access.
  63.870 +     * Algorithm: if target address > current window, assume forward scan,
  63.871 +     * load starting at target address.  If target address < current window,
  63.872 +     * assume backward scan, load so that target area is top of window.
  63.873 +     * Note that when switching from forward write to forward read, will have
  63.874 +     * start_row = 0, so the limiting case applies and we load from 0 anyway.
  63.875 +     */
  63.876 +    if (start_row > ptr->cur_start_row) {
  63.877 +      ptr->cur_start_row = start_row;
  63.878 +    } else {
  63.879 +      /* use long arithmetic here to avoid overflow & unsigned problems */
  63.880 +      long ltemp;
  63.881 +
  63.882 +      ltemp = (long) end_row - (long) ptr->rows_in_mem;
  63.883 +      if (ltemp < 0)
  63.884 +	ltemp = 0;		/* don't fall off front end of file */
  63.885 +      ptr->cur_start_row = (JDIMENSION) ltemp;
  63.886 +    }
  63.887 +    /* Read in the selected part of the array.
  63.888 +     * During the initial write pass, we will do no actual read
  63.889 +     * because the selected part is all undefined.
  63.890 +     */
  63.891 +    do_barray_io(cinfo, ptr, FALSE);
  63.892 +  }
  63.893 +  /* Ensure the accessed part of the array is defined; prezero if needed.
  63.894 +   * To improve locality of access, we only prezero the part of the array
  63.895 +   * that the caller is about to access, not the entire in-memory array.
  63.896 +   */
  63.897 +  if (ptr->first_undef_row < end_row) {
  63.898 +    if (ptr->first_undef_row < start_row) {
  63.899 +      if (writable)		/* writer skipped over a section of array */
  63.900 +	ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
  63.901 +      undef_row = start_row;	/* but reader is allowed to read ahead */
  63.902 +    } else {
  63.903 +      undef_row = ptr->first_undef_row;
  63.904 +    }
  63.905 +    if (writable)
  63.906 +      ptr->first_undef_row = end_row;
  63.907 +    if (ptr->pre_zero) {
  63.908 +      size_t bytesperrow = (size_t) ptr->blocksperrow * SIZEOF(JBLOCK);
  63.909 +      undef_row -= ptr->cur_start_row; /* make indexes relative to buffer */
  63.910 +      end_row -= ptr->cur_start_row;
  63.911 +      while (undef_row < end_row) {
  63.912 +	jzero_far((void FAR *) ptr->mem_buffer[undef_row], bytesperrow);
  63.913 +	undef_row++;
  63.914 +      }
  63.915 +    } else {
  63.916 +      if (! writable)		/* reader looking at undefined data */
  63.917 +	ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
  63.918 +    }
  63.919 +  }
  63.920 +  /* Flag the buffer dirty if caller will write in it */
  63.921 +  if (writable)
  63.922 +    ptr->dirty = TRUE;
  63.923 +  /* Return address of proper part of the buffer */
  63.924 +  return ptr->mem_buffer + (start_row - ptr->cur_start_row);
  63.925 +}
  63.926 +
  63.927 +
  63.928 +/*
  63.929 + * Release all objects belonging to a specified pool.
  63.930 + */
  63.931 +
  63.932 +METHODDEF(void)
  63.933 +free_pool (j_common_ptr cinfo, int pool_id)
  63.934 +{
  63.935 +  my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
  63.936 +  small_pool_ptr shdr_ptr;
  63.937 +  large_pool_ptr lhdr_ptr;
  63.938 +  size_t space_freed;
  63.939 +
  63.940 +  if (pool_id < 0 || pool_id >= JPOOL_NUMPOOLS)
  63.941 +    ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id);	/* safety check */
  63.942 +
  63.943 +#ifdef MEM_STATS
  63.944 +  if (cinfo->err->trace_level > 1)
  63.945 +    print_mem_stats(cinfo, pool_id); /* print pool's memory usage statistics */
  63.946 +#endif
  63.947 +
  63.948 +  /* If freeing IMAGE pool, close any virtual arrays first */
  63.949 +  if (pool_id == JPOOL_IMAGE) {
  63.950 +    jvirt_sarray_ptr sptr;
  63.951 +    jvirt_barray_ptr bptr;
  63.952 +
  63.953 +    for (sptr = mem->virt_sarray_list; sptr != NULL; sptr = sptr->next) {
  63.954 +      if (sptr->b_s_open) {	/* there may be no backing store */
  63.955 +	sptr->b_s_open = FALSE;	/* prevent recursive close if error */
  63.956 +	(*sptr->b_s_info.close_backing_store) (cinfo, & sptr->b_s_info);
  63.957 +      }
  63.958 +    }
  63.959 +    mem->virt_sarray_list = NULL;
  63.960 +    for (bptr = mem->virt_barray_list; bptr != NULL; bptr = bptr->next) {
  63.961 +      if (bptr->b_s_open) {	/* there may be no backing store */
  63.962 +	bptr->b_s_open = FALSE;	/* prevent recursive close if error */
  63.963 +	(*bptr->b_s_info.close_backing_store) (cinfo, & bptr->b_s_info);
  63.964 +      }
  63.965 +    }
  63.966 +    mem->virt_barray_list = NULL;
  63.967 +  }
  63.968 +
  63.969 +  /* Release large objects */
  63.970 +  lhdr_ptr = mem->large_list[pool_id];
  63.971 +  mem->large_list[pool_id] = NULL;
  63.972 +
  63.973 +  while (lhdr_ptr != NULL) {
  63.974 +    large_pool_ptr next_lhdr_ptr = lhdr_ptr->hdr.next;
  63.975 +    space_freed = lhdr_ptr->hdr.bytes_used +
  63.976 +		  lhdr_ptr->hdr.bytes_left +
  63.977 +		  SIZEOF(large_pool_hdr);
  63.978 +    jpeg_free_large(cinfo, (void FAR *) lhdr_ptr, space_freed);
  63.979 +    mem->total_space_allocated -= space_freed;
  63.980 +    lhdr_ptr = next_lhdr_ptr;
  63.981 +  }
  63.982 +
  63.983 +  /* Release small objects */
  63.984 +  shdr_ptr = mem->small_list[pool_id];
  63.985 +  mem->small_list[pool_id] = NULL;
  63.986 +
  63.987 +  while (shdr_ptr != NULL) {
  63.988 +    small_pool_ptr next_shdr_ptr = shdr_ptr->hdr.next;
  63.989 +    space_freed = shdr_ptr->hdr.bytes_used +
  63.990 +		  shdr_ptr->hdr.bytes_left +
  63.991 +		  SIZEOF(small_pool_hdr);
  63.992 +    jpeg_free_small(cinfo, (void *) shdr_ptr, space_freed);
  63.993 +    mem->total_space_allocated -= space_freed;
  63.994 +    shdr_ptr = next_shdr_ptr;
  63.995 +  }
  63.996 +}
  63.997 +
  63.998 +
  63.999 +/*
 63.1000 + * Close up shop entirely.
 63.1001 + * Note that this cannot be called unless cinfo->mem is non-NULL.
 63.1002 + */
 63.1003 +
 63.1004 +METHODDEF(void)
 63.1005 +self_destruct (j_common_ptr cinfo)
 63.1006 +{
 63.1007 +  int pool;
 63.1008 +
 63.1009 +  /* Close all backing store, release all memory.
 63.1010 +   * Releasing pools in reverse order might help avoid fragmentation
 63.1011 +   * with some (brain-damaged) malloc libraries.
 63.1012 +   */
 63.1013 +  for (pool = JPOOL_NUMPOOLS-1; pool >= JPOOL_PERMANENT; pool--) {
 63.1014 +    free_pool(cinfo, pool);
 63.1015 +  }
 63.1016 +
 63.1017 +  /* Release the memory manager control block too. */
 63.1018 +  jpeg_free_small(cinfo, (void *) cinfo->mem, SIZEOF(my_memory_mgr));
 63.1019 +  cinfo->mem = NULL;		/* ensures I will be called only once */
 63.1020 +
 63.1021 +  jpeg_mem_term(cinfo);		/* system-dependent cleanup */
 63.1022 +}
 63.1023 +
 63.1024 +
 63.1025 +/*
 63.1026 + * Memory manager initialization.
 63.1027 + * When this is called, only the error manager pointer is valid in cinfo!
 63.1028 + */
 63.1029 +
 63.1030 +GLOBAL(void)
 63.1031 +jinit_memory_mgr (j_common_ptr cinfo)
 63.1032 +{
 63.1033 +  my_mem_ptr mem;
 63.1034 +  long max_to_use;
 63.1035 +  int pool;
 63.1036 +  size_t test_mac;
 63.1037 +
 63.1038 +  cinfo->mem = NULL;		/* for safety if init fails */
 63.1039 +
 63.1040 +  /* Check for configuration errors.
 63.1041 +   * SIZEOF(ALIGN_TYPE) should be a power of 2; otherwise, it probably
 63.1042 +   * doesn't reflect any real hardware alignment requirement.
 63.1043 +   * The test is a little tricky: for X>0, X and X-1 have no one-bits
 63.1044 +   * in common if and only if X is a power of 2, ie has only one one-bit.
 63.1045 +   * Some compilers may give an "unreachable code" warning here; ignore it.
 63.1046 +   */
 63.1047 +  if ((SIZEOF(ALIGN_TYPE) & (SIZEOF(ALIGN_TYPE)-1)) != 0)
 63.1048 +    ERREXIT(cinfo, JERR_BAD_ALIGN_TYPE);
 63.1049 +  /* MAX_ALLOC_CHUNK must be representable as type size_t, and must be
 63.1050 +   * a multiple of SIZEOF(ALIGN_TYPE).
 63.1051 +   * Again, an "unreachable code" warning may be ignored here.
 63.1052 +   * But a "constant too large" warning means you need to fix MAX_ALLOC_CHUNK.
 63.1053 +   */
 63.1054 +  test_mac = (size_t) MAX_ALLOC_CHUNK;
 63.1055 +  if ((long) test_mac != MAX_ALLOC_CHUNK ||
 63.1056 +      (MAX_ALLOC_CHUNK % SIZEOF(ALIGN_TYPE)) != 0)
 63.1057 +    ERREXIT(cinfo, JERR_BAD_ALLOC_CHUNK);
 63.1058 +
 63.1059 +  max_to_use = jpeg_mem_init(cinfo); /* system-dependent initialization */
 63.1060 +
 63.1061 +  /* Attempt to allocate memory manager's control block */
 63.1062 +  mem = (my_mem_ptr) jpeg_get_small(cinfo, SIZEOF(my_memory_mgr));
 63.1063 +
 63.1064 +  if (mem == NULL) {
 63.1065 +    jpeg_mem_term(cinfo);	/* system-dependent cleanup */
 63.1066 +    ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 0);
 63.1067 +  }
 63.1068 +
 63.1069 +  /* OK, fill in the method pointers */
 63.1070 +  mem->pub.alloc_small = alloc_small;
 63.1071 +  mem->pub.alloc_large = alloc_large;
 63.1072 +  mem->pub.alloc_sarray = alloc_sarray;
 63.1073 +  mem->pub.alloc_barray = alloc_barray;
 63.1074 +  mem->pub.request_virt_sarray = request_virt_sarray;
 63.1075 +  mem->pub.request_virt_barray = request_virt_barray;
 63.1076 +  mem->pub.realize_virt_arrays = realize_virt_arrays;
 63.1077 +  mem->pub.access_virt_sarray = access_virt_sarray;
 63.1078 +  mem->pub.access_virt_barray = access_virt_barray;
 63.1079 +  mem->pub.free_pool = free_pool;
 63.1080 +  mem->pub.self_destruct = self_destruct;
 63.1081 +
 63.1082 +  /* Make MAX_ALLOC_CHUNK accessible to other modules */
 63.1083 +  mem->pub.max_alloc_chunk = MAX_ALLOC_CHUNK;
 63.1084 +
 63.1085 +  /* Initialize working state */
 63.1086 +  mem->pub.max_memory_to_use = max_to_use;
 63.1087 +
 63.1088 +  for (pool = JPOOL_NUMPOOLS-1; pool >= JPOOL_PERMANENT; pool--) {
 63.1089 +    mem->small_list[pool] = NULL;
 63.1090 +    mem->large_list[pool] = NULL;
 63.1091 +  }
 63.1092 +  mem->virt_sarray_list = NULL;
 63.1093 +  mem->virt_barray_list = NULL;
 63.1094 +
 63.1095 +  mem->total_space_allocated = SIZEOF(my_memory_mgr);
 63.1096 +
 63.1097 +  /* Declare ourselves open for business */
 63.1098 +  cinfo->mem = & mem->pub;
 63.1099 +
 63.1100 +  /* Check for an environment variable JPEGMEM; if found, override the
 63.1101 +   * default max_memory setting from jpeg_mem_init.  Note that the
 63.1102 +   * surrounding application may again override this value.
 63.1103 +   * If your system doesn't support getenv(), define NO_GETENV to disable
 63.1104 +   * this feature.
 63.1105 +   */
 63.1106 +#ifndef NO_GETENV
 63.1107 +  { char * memenv;
 63.1108 +
 63.1109 +    if ((memenv = getenv("JPEGMEM")) != NULL) {
 63.1110 +      char ch = 'x';
 63.1111 +
 63.1112 +      if (sscanf(memenv, "%ld%c", &max_to_use, &ch) > 0) {
 63.1113 +	if (ch == 'm' || ch == 'M')
 63.1114 +	  max_to_use *= 1000L;
 63.1115 +	mem->pub.max_memory_to_use = max_to_use * 1000L;
 63.1116 +      }
 63.1117 +    }
 63.1118 +  }
 63.1119 +#endif
 63.1120 +
 63.1121 +}
    64.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    64.2 +++ b/libs/libjpeg/jmemnobs.c	Sat Sep 19 05:51:51 2015 +0300
    64.3 @@ -0,0 +1,109 @@
    64.4 +/*
    64.5 + * jmemnobs.c
    64.6 + *
    64.7 + * Copyright (C) 1992-1996, Thomas G. Lane.
    64.8 + * This file is part of the Independent JPEG Group's software.
    64.9 + * For conditions of distribution and use, see the accompanying README file.
   64.10 + *
   64.11 + * This file provides a really simple implementation of the system-
   64.12 + * dependent portion of the JPEG memory manager.  This implementation
   64.13 + * assumes that no backing-store files are needed: all required space
   64.14 + * can be obtained from malloc().
   64.15 + * This is very portable in the sense that it'll compile on almost anything,
   64.16 + * but you'd better have lots of main memory (or virtual memory) if you want
   64.17 + * to process big images.
   64.18 + * Note that the max_memory_to_use option is ignored by this implementation.
   64.19 + */
   64.20 +
   64.21 +#define JPEG_INTERNALS
   64.22 +#include "jinclude.h"
   64.23 +#include "jpeglib.h"
   64.24 +#include "jmemsys.h"		/* import the system-dependent declarations */
   64.25 +
   64.26 +#ifndef HAVE_STDLIB_H		/* <stdlib.h> should declare malloc(),free() */
   64.27 +extern void * malloc JPP((size_t size));
   64.28 +extern void free JPP((void *ptr));
   64.29 +#endif
   64.30 +
   64.31 +
   64.32 +/*
   64.33 + * Memory allocation and freeing are controlled by the regular library
   64.34 + * routines malloc() and free().
   64.35 + */
   64.36 +
   64.37 +GLOBAL(void *)
   64.38 +jpeg_get_small (j_common_ptr cinfo, size_t sizeofobject)
   64.39 +{
   64.40 +  return (void *) malloc(sizeofobject);
   64.41 +}
   64.42 +
   64.43 +GLOBAL(void)
   64.44 +jpeg_free_small (j_common_ptr cinfo, void * object, size_t sizeofobject)
   64.45 +{
   64.46 +  free(object);
   64.47 +}
   64.48 +
   64.49 +
   64.50 +/*
   64.51 + * "Large" objects are treated the same as "small" ones.
   64.52 + * NB: although we include FAR keywords in the routine declarations,
   64.53 + * this file won't actually work in 80x86 small/medium model; at least,
   64.54 + * you probably won't be able to process useful-size images in only 64KB.
   64.55 + */
   64.56 +
   64.57 +GLOBAL(void FAR *)
   64.58 +jpeg_get_large (j_common_ptr cinfo, size_t sizeofobject)
   64.59 +{
   64.60 +  return (void FAR *) malloc(sizeofobject);
   64.61 +}
   64.62 +
   64.63 +GLOBAL(void)
   64.64 +jpeg_free_large (j_common_ptr cinfo, void FAR * object, size_t sizeofobject)
   64.65 +{
   64.66 +  free(object);
   64.67 +}
   64.68 +
   64.69 +
   64.70 +/*
   64.71 + * This routine computes the total memory space available for allocation.
   64.72 + * Here we always say, "we got all you want bud!"
   64.73 + */
   64.74 +
   64.75 +GLOBAL(long)
   64.76 +jpeg_mem_available (j_common_ptr cinfo, long min_bytes_needed,
   64.77 +		    long max_bytes_needed, long already_allocated)
   64.78 +{
   64.79 +  return max_bytes_needed;
   64.80 +}
   64.81 +
   64.82 +
   64.83 +/*
   64.84 + * Backing store (temporary file) management.
   64.85 + * Since jpeg_mem_available always promised the moon,
   64.86 + * this should never be called and we can just error out.
   64.87 + */
   64.88 +
   64.89 +GLOBAL(void)
   64.90 +jpeg_open_backing_store (j_common_ptr cinfo, backing_store_ptr info,
   64.91 +			 long total_bytes_needed)
   64.92 +{
   64.93 +  ERREXIT(cinfo, JERR_NO_BACKING_STORE);
   64.94 +}
   64.95 +
   64.96 +
   64.97 +/*
   64.98 + * These routines take care of any system-dependent initialization and
   64.99 + * cleanup required.  Here, there isn't any.
  64.100 + */
  64.101 +
  64.102 +GLOBAL(long)
  64.103 +jpeg_mem_init (j_common_ptr cinfo)
  64.104 +{
  64.105 +  return 0;			/* just set max_memory_to_use to 0 */
  64.106 +}
  64.107 +
  64.108 +GLOBAL(void)
  64.109 +jpeg_mem_term (j_common_ptr cinfo)
  64.110 +{
  64.111 +  /* no work */
  64.112 +}
    65.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    65.2 +++ b/libs/libjpeg/jmemsys.h	Sat Sep 19 05:51:51 2015 +0300
    65.3 @@ -0,0 +1,198 @@
    65.4 +/*
    65.5 + * jmemsys.h
    65.6 + *
    65.7 + * Copyright (C) 1992-1997, Thomas G. Lane.
    65.8 + * This file is part of the Independent JPEG Group's software.
    65.9 + * For conditions of distribution and use, see the accompanying README file.
   65.10 + *
   65.11 + * This include file defines the interface between the system-independent
   65.12 + * and system-dependent portions of the JPEG memory manager.  No other
   65.13 + * modules need include it.  (The system-independent portion is jmemmgr.c;
   65.14 + * there are several different versions of the system-dependent portion.)
   65.15 + *
   65.16 + * This file works as-is for the system-dependent memory managers supplied
   65.17 + * in the IJG distribution.  You may need to modify it if you write a
   65.18 + * custom memory manager.  If system-dependent changes are needed in
   65.19 + * this file, the best method is to #ifdef them based on a configuration
   65.20 + * symbol supplied in jconfig.h, as we have done with USE_MSDOS_MEMMGR
   65.21 + * and USE_MAC_MEMMGR.
   65.22 + */
   65.23 +
   65.24 +
   65.25 +/* Short forms of external names for systems with brain-damaged linkers. */
   65.26 +
   65.27 +#ifdef NEED_SHORT_EXTERNAL_NAMES
   65.28 +#define jpeg_get_small		jGetSmall
   65.29 +#define jpeg_free_small		jFreeSmall
   65.30 +#define jpeg_get_large		jGetLarge
   65.31 +#define jpeg_free_large		jFreeLarge
   65.32 +#define jpeg_mem_available	jMemAvail
   65.33 +#define jpeg_open_backing_store	jOpenBackStore
   65.34 +#define jpeg_mem_init		jMemInit
   65.35 +#define jpeg_mem_term		jMemTerm
   65.36 +#endif /* NEED_SHORT_EXTERNAL_NAMES */
   65.37 +
   65.38 +
   65.39 +/*
   65.40 + * These two functions are used to allocate and release small chunks of
   65.41 + * memory.  (Typically the total amount requested through jpeg_get_small is
   65.42 + * no more than 20K or so; this will be requested in chunks of a few K each.)
   65.43 + * Behavior should be the same as for the standard library functions malloc
   65.44 + * and free; in particular, jpeg_get_small must return NULL on failure.
   65.45 + * On most systems, these ARE malloc and free.  jpeg_free_small is passed the
   65.46 + * size of the object being freed, just in case it's needed.
   65.47 + * On an 80x86 machine using small-data memory model, these manage near heap.
   65.48 + */
   65.49 +
   65.50 +EXTERN(void *) jpeg_get_small JPP((j_common_ptr cinfo, size_t sizeofobject));
   65.51 +EXTERN(void) jpeg_free_small JPP((j_common_ptr cinfo, void * object,
   65.52 +				  size_t sizeofobject));
   65.53 +
   65.54 +/*
   65.55 + * These two functions are used to allocate and release large chunks of
   65.56 + * memory (up to the total free space designated by jpeg_mem_available).
   65.57 + * The interface is the same as above, except that on an 80x86 machine,
   65.58 + * far pointers are used.  On most other machines these are identical to
   65.59 + * the jpeg_get/free_small routines; but we keep them separate anyway,
   65.60 + * in case a different allocation strategy is desirable for large chunks.
   65.61 + */
   65.62 +
   65.63 +EXTERN(void FAR *) jpeg_get_large JPP((j_common_ptr cinfo,
   65.64 +				       size_t sizeofobject));
   65.65 +EXTERN(void) jpeg_free_large JPP((j_common_ptr cinfo, void FAR * object,
   65.66 +				  size_t sizeofobject));
   65.67 +
   65.68 +/*
   65.69 + * The macro MAX_ALLOC_CHUNK designates the maximum number of bytes that may
   65.70 + * be requested in a single call to jpeg_get_large (and jpeg_get_small for that
   65.71 + * matter, but that case should never come into play).  This macro is needed
   65.72 + * to model the 64Kb-segment-size limit of far addressing on 80x86 machines.
   65.73 + * On those machines, we expect that jconfig.h will provide a proper value.
   65.74 + * On machines with 32-bit flat address spaces, any large constant may be used.
   65.75 + *
   65.76 + * NB: jmemmgr.c expects that MAX_ALLOC_CHUNK will be representable as type
   65.77 + * size_t and will be a multiple of sizeof(align_type).
   65.78 + */
   65.79 +
   65.80 +#ifndef MAX_ALLOC_CHUNK		/* may be overridden in jconfig.h */
   65.81 +#define MAX_ALLOC_CHUNK  1000000000L
   65.82 +#endif
   65.83 +
   65.84 +/*
   65.85 + * This routine computes the total space still available for allocation by
   65.86 + * jpeg_get_large.  If more space than this is needed, backing store will be
   65.87 + * used.  NOTE: any memory already allocated must not be counted.
   65.88 + *
   65.89 + * There is a minimum space requirement, corresponding to the minimum
   65.90 + * feasible buffer sizes; jmemmgr.c will request that much space even if
   65.91 + * jpeg_mem_available returns zero.  The maximum space needed, enough to hold
   65.92 + * all working storage in memory, is also passed in case it is useful.
   65.93 + * Finally, the total space already allocated is passed.  If no better
   65.94 + * method is available, cinfo->mem->max_memory_to_use - already_allocated
   65.95 + * is often a suitable calculation.
   65.96 + *
   65.97 + * It is OK for jpeg_mem_available to underestimate the space available
   65.98 + * (that'll just lead to more backing-store access than is really necessary).
   65.99 + * However, an overestimate will lead to failure.  Hence it's wise to subtract
  65.100 + * a slop factor from the true available space.  5% should be enough.
  65.101 + *
  65.102 + * On machines with lots of virtual memory, any large constant may be returned.
  65.103 + * Conversely, zero may be returned to always use the minimum amount of memory.
  65.104 + */
  65.105 +
  65.106 +EXTERN(long) jpeg_mem_available JPP((j_common_ptr cinfo,
  65.107 +				     long min_bytes_needed,
  65.108 +				     long max_bytes_needed,
  65.109 +				     long already_allocated));
  65.110 +
  65.111 +
  65.112 +/*
  65.113 + * This structure holds whatever state is needed to access a single
  65.114 + * backing-store object.  The read/write/close method pointers are called
  65.115 + * by jmemmgr.c to manipulate the backing-store object; all other fields
  65.116 + * are private to the system-dependent backing store routines.
  65.117 + */
  65.118 +
  65.119 +#define TEMP_NAME_LENGTH   64	/* max length of a temporary file's name */
  65.120 +
  65.121 +
  65.122 +#ifdef USE_MSDOS_MEMMGR		/* DOS-specific junk */
  65.123 +
  65.124 +typedef unsigned short XMSH;	/* type of extended-memory handles */
  65.125 +typedef unsigned short EMSH;	/* type of expanded-memory handles */
  65.126 +
  65.127 +typedef union {
  65.128 +  short file_handle;		/* DOS file handle if it's a temp file */
  65.129 +  XMSH xms_handle;		/* handle if it's a chunk of XMS */
  65.130 +  EMSH ems_handle;		/* handle if it's a chunk of EMS */
  65.131 +} handle_union;
  65.132 +
  65.133 +#endif /* USE_MSDOS_MEMMGR */
  65.134 +
  65.135 +#ifdef USE_MAC_MEMMGR		/* Mac-specific junk */
  65.136 +#include <Files.h>
  65.137 +#endif /* USE_MAC_MEMMGR */
  65.138 +
  65.139 +
  65.140 +typedef struct backing_store_struct * backing_store_ptr;
  65.141 +
  65.142 +typedef struct backing_store_struct {
  65.143 +  /* Methods for reading/writing/closing this backing-store object */
  65.144 +  JMETHOD(void, read_backing_store, (j_common_ptr cinfo,
  65.145 +				     backing_store_ptr info,
  65.146 +				     void FAR * buffer_address,
  65.147 +				     long file_offset, long byte_count));
  65.148 +  JMETHOD(void, write_backing_store, (j_common_ptr cinfo,
  65.149 +				      backing_store_ptr info,
  65.150 +				      void FAR * buffer_address,
  65.151 +				      long file_offset, long byte_count));
  65.152 +  JMETHOD(void, close_backing_store, (j_common_ptr cinfo,
  65.153 +				      backing_store_ptr info));
  65.154 +
  65.155 +  /* Private fields for system-dependent backing-store management */
  65.156 +#ifdef USE_MSDOS_MEMMGR
  65.157 +  /* For the MS-DOS manager (jmemdos.c), we need: */
  65.158 +  handle_union handle;		/* reference to backing-store storage object */
  65.159 +  char temp_name[TEMP_NAME_LENGTH]; /* name if it's a file */
  65.160 +#else
  65.161 +#ifdef USE_MAC_MEMMGR
  65.162 +  /* For the Mac manager (jmemmac.c), we need: */
  65.163 +  short temp_file;		/* file reference number to temp file */
  65.164 +  FSSpec tempSpec;		/* the FSSpec for the temp file */
  65.165 +  char temp_name[TEMP_NAME_LENGTH]; /* name if it's a file */
  65.166 +#else
  65.167 +  /* For a typical implementation with temp files, we need: */
  65.168 +  FILE * temp_file;		/* stdio reference to temp file */
  65.169 +  char temp_name[TEMP_NAME_LENGTH]; /* name of temp file */
  65.170 +#endif
  65.171 +#endif
  65.172 +} backing_store_info;
  65.173 +
  65.174 +
  65.175 +/*
  65.176 + * Initial opening of a backing-store object.  This must fill in the
  65.177 + * read/write/close pointers in the object.  The read/write routines
  65.178 + * may take an error exit if the specified maximum file size is exceeded.
  65.179 + * (If jpeg_mem_available always returns a large value, this routine can
  65.180 + * just take an error exit.)
  65.181 + */
  65.182 +
  65.183 +EXTERN(void) jpeg_open_backing_store JPP((j_common_ptr cinfo,
  65.184 +					  backing_store_ptr info,
  65.185 +					  long total_bytes_needed));
  65.186 +
  65.187 +
  65.188 +/*
  65.189 + * These routines take care of any system-dependent initialization and
  65.190 + * cleanup required.  jpeg_mem_init will be called before anything is
  65.191 + * allocated (and, therefore, nothing in cinfo is of use except the error
  65.192 + * manager pointer).  It should return a suitable default value for
  65.193 + * max_memory_to_use; this may subsequently be overridden by the surrounding
  65.194 + * application.  (Note that max_memory_to_use is only important if
  65.195 + * jpeg_mem_available chooses to consult it ... no one else will.)
  65.196 + * jpeg_mem_term may assume that all requested memory has been freed and that
  65.197 + * all opened backing-store objects have been closed.
  65.198 + */
  65.199 +
  65.200 +EXTERN(long) jpeg_mem_init JPP((j_common_ptr cinfo));
  65.201 +EXTERN(void) jpeg_mem_term JPP((j_common_ptr cinfo));
    66.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    66.2 +++ b/libs/libjpeg/jmorecfg.h	Sat Sep 19 05:51:51 2015 +0300
    66.3 @@ -0,0 +1,367 @@
    66.4 +/*
    66.5 + * jmorecfg.h
    66.6 + *
    66.7 + * Copyright (C) 1991-1997, Thomas G. Lane.
    66.8 + * This file is part of the Independent JPEG Group's software.
    66.9 + * For conditions of distribution and use, see the accompanying README file.
   66.10 + *
   66.11 + * This file contains additional configuration options that customize the
   66.12 + * JPEG software for special applications or support machine-dependent
   66.13 + * optimizations.  Most users will not need to touch this file.
   66.14 + */
   66.15 +
   66.16 +
   66.17 +/*
   66.18 + * Define BITS_IN_JSAMPLE as either
   66.19 + *   8   for 8-bit sample values (the usual setting)
   66.20 + *   12  for 12-bit sample values
   66.21 + * Only 8 and 12 are legal data precisions for lossy JPEG according to the
   66.22 + * JPEG standard, and the IJG code does not support anything else!
   66.23 + * We do not support run-time selection of data precision, sorry.
   66.24 + */
   66.25 +
   66.26 +#define BITS_IN_JSAMPLE  8	/* use 8 or 12 */
   66.27 +
   66.28 +
   66.29 +/*
   66.30 + * Maximum number of components (color channels) allowed in JPEG image.
   66.31 + * To meet the letter of the JPEG spec, set this to 255.  However, darn
   66.32 + * few applications need more than 4 channels (maybe 5 for CMYK + alpha
   66.33 + * mask).  We recommend 10 as a reasonable compromise; use 4 if you are
   66.34 + * really short on memory.  (Each allowed component costs a hundred or so
   66.35 + * bytes of storage, whether actually used in an image or not.)
   66.36 + */
   66.37 +
   66.38 +#define MAX_COMPONENTS  10	/* maximum number of image components */
   66.39 +
   66.40 +
   66.41 +/*
   66.42 + * Basic data types.
   66.43 + * You may need to change these if you have a machine with unusual data
   66.44 + * type sizes; for example, "char" not 8 bits, "short" not 16 bits,
   66.45 + * or "long" not 32 bits.  We don't care whether "int" is 16 or 32 bits,
   66.46 + * but it had better be at least 16.
   66.47 + */
   66.48 +
   66.49 +/* Representation of a single sample (pixel element value).
   66.50 + * We frequently allocate large arrays of these, so it's important to keep
   66.51 + * them small.  But if you have memory to burn and access to char or short
   66.52 + * arrays is very slow on your hardware, you might want to change these.
   66.53 + */
   66.54 +
   66.55 +#if BITS_IN_JSAMPLE == 8
   66.56 +/* JSAMPLE should be the smallest type that will hold the values 0..255.
   66.57 + * You can use a signed char by having GETJSAMPLE mask it with 0xFF.
   66.58 + */
   66.59 +
   66.60 +#ifdef HAVE_UNSIGNED_CHAR
   66.61 +
   66.62 +typedef unsigned char JSAMPLE;
   66.63 +#define GETJSAMPLE(value)  ((int) (value))
   66.64 +
   66.65 +#else /* not HAVE_UNSIGNED_CHAR */
   66.66 +
   66.67 +typedef char JSAMPLE;
   66.68 +#ifdef CHAR_IS_UNSIGNED
   66.69 +#define GETJSAMPLE(value)  ((int) (value))
   66.70 +#else
   66.71 +#define GETJSAMPLE(value)  ((int) (value) & 0xFF)
   66.72 +#endif /* CHAR_IS_UNSIGNED */
   66.73 +
   66.74 +#endif /* HAVE_UNSIGNED_CHAR */
   66.75 +
   66.76 +#define MAXJSAMPLE	255
   66.77 +#define CENTERJSAMPLE	128
   66.78 +
   66.79 +#endif /* BITS_IN_JSAMPLE == 8 */
   66.80 +
   66.81 +
   66.82 +#if BITS_IN_JSAMPLE == 12
   66.83 +/* JSAMPLE should be the smallest type that will hold the values 0..4095.
   66.84 + * On nearly all machines "short" will do nicely.
   66.85 + */
   66.86 +
   66.87 +typedef short JSAMPLE;
   66.88 +#define GETJSAMPLE(value)  ((int) (value))
   66.89 +
   66.90 +#define MAXJSAMPLE	4095
   66.91 +#define CENTERJSAMPLE	2048
   66.92 +
   66.93 +#endif /* BITS_IN_JSAMPLE == 12 */
   66.94 +
   66.95 +
   66.96 +/* Representation of a DCT frequency coefficient.
   66.97 + * This should be a signed value of at least 16 bits; "short" is usually OK.
   66.98 + * Again, we allocate large arrays of these, but you can change to int
   66.99 + * if you have memory to burn and "short" is really slow.
  66.100 + */
  66.101 +
  66.102 +typedef short JCOEF;
  66.103 +
  66.104 +
  66.105 +/* Compressed datastreams are represented as arrays of JOCTET.
  66.106 + * These must be EXACTLY 8 bits wide, at least once they are written to
  66.107 + * external storage.  Note that when using the stdio data source/destination
  66.108 + * managers, this is also the data type passed to fread/fwrite.
  66.109 + */
  66.110 +
  66.111 +#ifdef HAVE_UNSIGNED_CHAR
  66.112 +
  66.113 +typedef unsigned char JOCTET;
  66.114 +#define GETJOCTET(value)  (value)
  66.115 +
  66.116 +#else /* not HAVE_UNSIGNED_CHAR */
  66.117 +
  66.118 +typedef char JOCTET;
  66.119 +#ifdef CHAR_IS_UNSIGNED
  66.120 +#define GETJOCTET(value)  (value)
  66.121 +#else
  66.122 +#define GETJOCTET(value)  ((value) & 0xFF)
  66.123 +#endif /* CHAR_IS_UNSIGNED */
  66.124 +
  66.125 +#endif /* HAVE_UNSIGNED_CHAR */
  66.126 +
  66.127 +
  66.128 +/* These typedefs are used for various table entries and so forth.
  66.129 + * They must be at least as wide as specified; but making them too big
  66.130 + * won't cost a huge amount of memory, so we don't provide special
  66.131 + * extraction code like we did for JSAMPLE.  (In other words, these
  66.132 + * typedefs live at a different point on the speed/space tradeoff curve.)
  66.133 + */
  66.134 +
  66.135 +/* UINT8 must hold at least the values 0..255. */
  66.136 +
  66.137 +#ifdef HAVE_UNSIGNED_CHAR
  66.138 +typedef unsigned char UINT8;
  66.139 +#else /* not HAVE_UNSIGNED_CHAR */
  66.140 +#ifdef CHAR_IS_UNSIGNED
  66.141 +typedef char UINT8;
  66.142 +#else /* not CHAR_IS_UNSIGNED */
  66.143 +typedef short UINT8;
  66.144 +#endif /* CHAR_IS_UNSIGNED */
  66.145 +#endif /* HAVE_UNSIGNED_CHAR */
  66.146 +
  66.147 +/* UINT16 must hold at least the values 0..65535. */
  66.148 +
  66.149 +#ifdef HAVE_UNSIGNED_SHORT
  66.150 +typedef unsigned short UINT16;
  66.151 +#else /* not HAVE_UNSIGNED_SHORT */
  66.152 +typedef unsigned int UINT16;
  66.153 +#endif /* HAVE_UNSIGNED_SHORT */
  66.154 +
  66.155 +/* INT16 must hold at least the values -32768..32767. */
  66.156 +
  66.157 +#ifndef XMD_H			/* X11/xmd.h correctly defines INT16 */
  66.158 +typedef short INT16;
  66.159 +#endif
  66.160 +
  66.161 +/* INT32 must hold at least signed 32-bit values. */
  66.162 +
  66.163 +#ifndef XMD_H			/* X11/xmd.h correctly defines INT32 */
  66.164 +typedef int INT32;
  66.165 +#endif
  66.166 +
  66.167 +/* Datatype used for image dimensions.  The JPEG standard only supports
  66.168 + * images up to 64K*64K due to 16-bit fields in SOF markers.  Therefore
  66.169 + * "unsigned int" is sufficient on all machines.  However, if you need to
  66.170 + * handle larger images and you don't mind deviating from the spec, you
  66.171 + * can change this datatype.
  66.172 + */
  66.173 +
  66.174 +typedef unsigned int JDIMENSION;
  66.175 +
  66.176 +#define JPEG_MAX_DIMENSION  65500L  /* a tad under 64K to prevent overflows */
  66.177 +
  66.178 +
  66.179 +/* These macros are used in all function definitions and extern declarations.
  66.180 + * You could modify them if you need to change function linkage conventions;
  66.181 + * in particular, you'll need to do that to make the library a Windows DLL.
  66.182 + * Another application is to make all functions global for use with debuggers
  66.183 + * or code profilers that require it.
  66.184 + */
  66.185 +
  66.186 +/* a function called through method pointers: */
  66.187 +#define METHODDEF(type)		static type
  66.188 +/* a function used only in its module: */
  66.189 +#define LOCAL(type)		static type
  66.190 +/* a function referenced thru EXTERNs: */
  66.191 +#define GLOBAL(type)		type
  66.192 +/* a reference to a GLOBAL function: */
  66.193 +#define EXTERN(type)		extern type
  66.194 +
  66.195 +
  66.196 +/* This macro is used to declare a "method", that is, a function pointer.
  66.197 + * We want to supply prototype parameters if the compiler can cope.
  66.198 + * Note that the arglist parameter must be parenthesized!
  66.199 + * Again, you can customize this if you need special linkage keywords.
  66.200 + */
  66.201 +
  66.202 +#ifdef HAVE_PROTOTYPES
  66.203 +#define JMETHOD(type,methodname,arglist)  type (*methodname) arglist
  66.204 +#else
  66.205 +#define JMETHOD(type,methodname,arglist)  type (*methodname) ()
  66.206 +#endif
  66.207 +
  66.208 +
  66.209 +/* Here is the pseudo-keyword for declaring pointers that must be "far"
  66.210 + * on 80x86 machines.  Most of the specialized coding for 80x86 is handled
  66.211 + * by just saying "FAR *" where such a pointer is needed.  In a few places
  66.212 + * explicit coding is needed; see uses of the NEED_FAR_POINTERS symbol.
  66.213 + */
  66.214 +
  66.215 +#ifdef FAR
  66.216 +#undef FAR
  66.217 +#endif
  66.218 +
  66.219 +#ifdef NEED_FAR_POINTERS
  66.220 +#define FAR  far
  66.221 +#else
  66.222 +#define FAR
  66.223 +#endif
  66.224 +
  66.225 +
  66.226 +/*
  66.227 + * On a few systems, type boolean and/or its values FALSE, TRUE may appear
  66.228 + * in standard header files.  Or you may have conflicts with application-
  66.229 + * specific header files that you want to include together with these files.
  66.230 + * Defining HAVE_BOOLEAN before including jpeglib.h should make it work.
  66.231 + */
  66.232 +
  66.233 +#ifndef HAVE_BOOLEAN
  66.234 +typedef int boolean;
  66.235 +#endif
  66.236 +#ifndef FALSE			/* in case these macros already exist */
  66.237 +#define FALSE	0		/* values of boolean */
  66.238 +#endif
  66.239 +#ifndef TRUE
  66.240 +#define TRUE	1
  66.241 +#endif
  66.242 +
  66.243 +
  66.244 +/*
  66.245 + * The remaining options affect code selection within the JPEG library,
  66.246 + * but they don't need to be visible to most applications using the library.
  66.247 + * To minimize application namespace pollution, the symbols won't be
  66.248 + * defined unless JPEG_INTERNALS or JPEG_INTERNAL_OPTIONS has been defined.
  66.249 + */
  66.250 +
  66.251 +#ifdef JPEG_INTERNALS
  66.252 +#define JPEG_INTERNAL_OPTIONS
  66.253 +#endif
  66.254 +
  66.255 +#ifdef JPEG_INTERNAL_OPTIONS
  66.256 +
  66.257 +
  66.258 +/*
  66.259 + * These defines indicate whether to include various optional functions.
  66.260 + * Undefining some of these symbols will produce a smaller but less capable
  66.261 + * library.  Note that you can leave certain source files out of the
  66.262 + * compilation/linking process if you've #undef'd the corresponding symbols.
  66.263 + * (You may HAVE to do that if your compiler doesn't like null source files.)
  66.264 + */
  66.265 +
  66.266 +/* Arithmetic coding is unsupported for legal reasons.  Complaints to IBM. */
  66.267 +
  66.268 +/* Capability options common to encoder and decoder: */
  66.269 +
  66.270 +#define DCT_ISLOW_SUPPORTED	/* slow but accurate integer algorithm */
  66.271 +#define DCT_IFAST_SUPPORTED	/* faster, less accurate integer method */
  66.272 +#define DCT_FLOAT_SUPPORTED	/* floating-point: accurate, fast on fast HW */
  66.273 +
  66.274 +/* Encoder capability options: */
  66.275 +
  66.276 +#undef  C_ARITH_CODING_SUPPORTED    /* Arithmetic coding back end? */
  66.277 +#define C_MULTISCAN_FILES_SUPPORTED /* Multiple-scan JPEG files? */
  66.278 +#define C_PROGRESSIVE_SUPPORTED	    /* Progressive JPEG? (Requires MULTISCAN)*/
  66.279 +#define ENTROPY_OPT_SUPPORTED	    /* Optimization of entropy coding parms? */
  66.280 +/* Note: if you selected 12-bit data precision, it is dangerous to turn off
  66.281 + * ENTROPY_OPT_SUPPORTED.  The standard Huffman tables are only good for 8-bit
  66.282 + * precision, so jchuff.c normally uses entropy optimization to compute
  66.283 + * usable tables for higher precision.  If you don't want to do optimization,
  66.284 + * you'll have to supply different default Huffman tables.
  66.285 + * The exact same statements apply for progressive JPEG: the default tables
  66.286 + * don't work for progressive mode.  (This may get fixed, however.)
  66.287 + */
  66.288 +#define INPUT_SMOOTHING_SUPPORTED   /* Input image smoothing option? */
  66.289 +
  66.290 +/* Decoder capability options: */
  66.291 +
  66.292 +#undef  D_ARITH_CODING_SUPPORTED    /* Arithmetic coding back end? */
  66.293 +#define D_MULTISCAN_FILES_SUPPORTED /* Multiple-scan JPEG files? */
  66.294 +#define D_PROGRESSIVE_SUPPORTED	    /* Progressive JPEG? (Requires MULTISCAN)*/
  66.295 +#define SAVE_MARKERS_SUPPORTED	    /* jpeg_save_markers() needed? */
  66.296 +#define BLOCK_SMOOTHING_SUPPORTED   /* Block smoothing? (Progressive only) */
  66.297 +#define IDCT_SCALING_SUPPORTED	    /* Output rescaling via IDCT? */
  66.298 +#undef  UPSAMPLE_SCALING_SUPPORTED  /* Output rescaling at upsample stage? */
  66.299 +#define UPSAMPLE_MERGING_SUPPORTED  /* Fast path for sloppy upsampling? */
  66.300 +#define QUANT_1PASS_SUPPORTED	    /* 1-pass color quantization? */
  66.301 +#define QUANT_2PASS_SUPPORTED	    /* 2-pass color quantization? */
  66.302 +
  66.303 +/* more capability options later, no doubt */
  66.304 +
  66.305 +
  66.306 +/*
  66.307 + * Ordering of RGB data in scanlines passed to or from the application.
  66.308 + * If your application wants to deal with data in the order B,G,R, just
  66.309 + * change these macros.  You can also deal with formats such as R,G,B,X
  66.310 + * (one extra byte per pixel) by changing RGB_PIXELSIZE.  Note that changing
  66.311 + * the offsets will also change the order in which colormap data is organized.
  66.312 + * RESTRICTIONS:
  66.313 + * 1. The sample applications cjpeg,djpeg do NOT support modified RGB formats.
  66.314 + * 2. These macros only affect RGB<=>YCbCr color conversion, so they are not
  66.315 + *    useful if you are using JPEG color spaces other than YCbCr or grayscale.
  66.316 + * 3. The color quantizer modules will not behave desirably if RGB_PIXELSIZE
  66.317 + *    is not 3 (they don't understand about dummy color components!).  So you
  66.318 + *    can't use color quantization if you change that value.
  66.319 + */
  66.320 +
  66.321 +#define RGB_RED		0	/* Offset of Red in an RGB scanline element */
  66.322 +#define RGB_GREEN	1	/* Offset of Green */
  66.323 +#define RGB_BLUE	2	/* Offset of Blue */
  66.324 +#define RGB_PIXELSIZE	3	/* JSAMPLEs per RGB scanline element */
  66.325 +
  66.326 +
  66.327 +/* Definitions for speed-related optimizations. */
  66.328 +
  66.329 +
  66.330 +/* If your compiler supports inline functions, define INLINE
  66.331 + * as the inline keyword; otherwise define it as empty.
  66.332 + */
  66.333 +
  66.334 +#ifndef INLINE
  66.335 +#ifdef __GNUC__			/* for instance, GNU C knows about inline */
  66.336 +#define INLINE __inline__
  66.337 +#endif
  66.338 +#ifndef INLINE
  66.339 +#define INLINE			/* default is to define it as empty */
  66.340 +#endif
  66.341 +#endif
  66.342 +
  66.343 +
  66.344 +/* On some machines (notably 68000 series) "int" is 32 bits, but multiplying
  66.345 + * two 16-bit shorts is faster than multiplying two ints.  Define MULTIPLIER
  66.346 + * as short on such a machine.  MULTIPLIER must be at least 16 bits wide.
  66.347 + */
  66.348 +
  66.349 +#ifndef MULTIPLIER
  66.350 +#define MULTIPLIER  int		/* type for fastest integer multiply */
  66.351 +#endif
  66.352 +
  66.353 +
  66.354 +/* FAST_FLOAT should be either float or double, whichever is done faster
  66.355 + * by your compiler.  (Note that this type is only used in the floating point
  66.356 + * DCT routines, so it only matters if you've defined DCT_FLOAT_SUPPORTED.)
  66.357 + * Typically, float is faster in ANSI C compilers, while double is faster in
  66.358 + * pre-ANSI compilers (because they insist on converting to double anyway).
  66.359 + * The code below therefore chooses float if we have ANSI-style prototypes.
  66.360 + */
  66.361 +
  66.362 +#ifndef FAST_FLOAT
  66.363 +#ifdef HAVE_PROTOTYPES
  66.364 +#define FAST_FLOAT  float
  66.365 +#else
  66.366 +#define FAST_FLOAT  double
  66.367 +#endif
  66.368 +#endif
  66.369 +
  66.370 +#endif /* JPEG_INTERNAL_OPTIONS */
    67.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    67.2 +++ b/libs/libjpeg/jpegint.h	Sat Sep 19 05:51:51 2015 +0300
    67.3 @@ -0,0 +1,392 @@
    67.4 +/*
    67.5 + * jpegint.h
    67.6 + *
    67.7 + * Copyright (C) 1991-1997, Thomas G. Lane.
    67.8 + * This file is part of the Independent JPEG Group's software.
    67.9 + * For conditions of distribution and use, see the accompanying README file.
   67.10 + *
   67.11 + * This file provides common declarations for the various JPEG modules.
   67.12 + * These declarations are considered internal to the JPEG library; most
   67.13 + * applications using the library shouldn't need to include this file.
   67.14 + */
   67.15 +
   67.16 +
   67.17 +/* Declarations for both compression & decompression */
   67.18 +
   67.19 +typedef enum {			/* Operating modes for buffer controllers */
   67.20 +	JBUF_PASS_THRU,		/* Plain stripwise operation */
   67.21 +	/* Remaining modes require a full-image buffer to have been created */
   67.22 +	JBUF_SAVE_SOURCE,	/* Run source subobject only, save output */
   67.23 +	JBUF_CRANK_DEST,	/* Run dest subobject only, using saved data */
   67.24 +	JBUF_SAVE_AND_PASS	/* Run both subobjects, save output */
   67.25 +} J_BUF_MODE;
   67.26 +
   67.27 +/* Values of global_state field (jdapi.c has some dependencies on ordering!) */
   67.28 +#define CSTATE_START	100	/* after create_compress */
   67.29 +#define CSTATE_SCANNING	101	/* start_compress done, write_scanlines OK */
   67.30 +#define CSTATE_RAW_OK	102	/* start_compress done, write_raw_data OK */
   67.31 +#define CSTATE_WRCOEFS	103	/* jpeg_write_coefficients done */
   67.32 +#define DSTATE_START	200	/* after create_decompress */
   67.33 +#define DSTATE_INHEADER	201	/* reading header markers, no SOS yet */
   67.34 +#define DSTATE_READY	202	/* found SOS, ready for start_decompress */
   67.35 +#define DSTATE_PRELOAD	203	/* reading multiscan file in start_decompress*/
   67.36 +#define DSTATE_PRESCAN	204	/* performing dummy pass for 2-pass quant */
   67.37 +#define DSTATE_SCANNING	205	/* start_decompress done, read_scanlines OK */
   67.38 +#define DSTATE_RAW_OK	206	/* start_decompress done, read_raw_data OK */
   67.39 +#define DSTATE_BUFIMAGE	207	/* expecting jpeg_start_output */
   67.40 +#define DSTATE_BUFPOST	208	/* looking for SOS/EOI in jpeg_finish_output */
   67.41 +#define DSTATE_RDCOEFS	209	/* reading file in jpeg_read_coefficients */
   67.42 +#define DSTATE_STOPPING	210	/* looking for EOI in jpeg_finish_decompress */
   67.43 +
   67.44 +
   67.45 +/* Declarations for compression modules */
   67.46 +
   67.47 +/* Master control module */
   67.48 +struct jpeg_comp_master {
   67.49 +  JMETHOD(void, prepare_for_pass, (j_compress_ptr cinfo));
   67.50 +  JMETHOD(void, pass_startup, (j_compress_ptr cinfo));
   67.51 +  JMETHOD(void, finish_pass, (j_compress_ptr cinfo));
   67.52 +
   67.53 +  /* State variables made visible to other modules */
   67.54 +  boolean call_pass_startup;	/* True if pass_startup must be called */
   67.55 +  boolean is_last_pass;		/* True during last pass */
   67.56 +};
   67.57 +
   67.58 +/* Main buffer control (downsampled-data buffer) */
   67.59 +struct jpeg_c_main_controller {
   67.60 +  JMETHOD(void, start_pass, (j_compress_ptr cinfo, J_BUF_MODE pass_mode));
   67.61 +  JMETHOD(void, process_data, (j_compress_ptr cinfo,
   67.62 +			       JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
   67.63 +			       JDIMENSION in_rows_avail));
   67.64 +};
   67.65 +
   67.66 +/* Compression preprocessing (downsampling input buffer control) */
   67.67 +struct jpeg_c_prep_controller {
   67.68 +  JMETHOD(void, start_pass, (j_compress_ptr cinfo, J_BUF_MODE pass_mode));
   67.69 +  JMETHOD(void, pre_process_data, (j_compress_ptr cinfo,
   67.70 +				   JSAMPARRAY input_buf,
   67.71 +				   JDIMENSION *in_row_ctr,
   67.72 +				   JDIMENSION in_rows_avail,
   67.73 +				   JSAMPIMAGE output_buf,
   67.74 +				   JDIMENSION *out_row_group_ctr,
   67.75 +				   JDIMENSION out_row_groups_avail));
   67.76 +};
   67.77 +
   67.78 +/* Coefficient buffer control */
   67.79 +struct jpeg_c_coef_controller {
   67.80 +  JMETHOD(void, start_pass, (j_compress_ptr cinfo, J_BUF_MODE pass_mode));
   67.81 +  JMETHOD(boolean, compress_data, (j_compress_ptr cinfo,
   67.82 +				   JSAMPIMAGE input_buf));
   67.83 +};
   67.84 +
   67.85 +/* Colorspace conversion */
   67.86 +struct jpeg_color_converter {
   67.87 +  JMETHOD(void, start_pass, (j_compress_ptr cinfo));
   67.88 +  JMETHOD(void, color_convert, (j_compress_ptr cinfo,
   67.89 +				JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
   67.90 +				JDIMENSION output_row, int num_rows));
   67.91 +};
   67.92 +
   67.93 +/* Downsampling */
   67.94 +struct jpeg_downsampler {
   67.95 +  JMETHOD(void, start_pass, (j_compress_ptr cinfo));
   67.96 +  JMETHOD(void, downsample, (j_compress_ptr cinfo,
   67.97 +			     JSAMPIMAGE input_buf, JDIMENSION in_row_index,
   67.98 +			     JSAMPIMAGE output_buf,
   67.99 +			     JDIMENSION out_row_group_index));
  67.100 +
  67.101 +  boolean need_context_rows;	/* TRUE if need rows above & below */
  67.102 +};
  67.103 +
  67.104 +/* Forward DCT (also controls coefficient quantization) */
  67.105 +struct jpeg_forward_dct {
  67.106 +  JMETHOD(void, start_pass, (j_compress_ptr cinfo));
  67.107 +  /* perhaps this should be an array??? */
  67.108 +  JMETHOD(void, forward_DCT, (j_compress_ptr cinfo,
  67.109 +			      jpeg_component_info * compptr,
  67.110 +			      JSAMPARRAY sample_data, JBLOCKROW coef_blocks,
  67.111 +			      JDIMENSION start_row, JDIMENSION start_col,
  67.112 +			      JDIMENSION num_blocks));
  67.113 +};
  67.114 +
  67.115 +/* Entropy encoding */
  67.116 +struct jpeg_entropy_encoder {
  67.117 +  JMETHOD(void, start_pass, (j_compress_ptr cinfo, boolean gather_statistics));
  67.118 +  JMETHOD(boolean, encode_mcu, (j_compress_ptr cinfo, JBLOCKROW *MCU_data));
  67.119 +  JMETHOD(void, finish_pass, (j_compress_ptr cinfo));
  67.120 +};
  67.121 +
  67.122 +/* Marker writing */
  67.123 +struct jpeg_marker_writer {
  67.124 +  JMETHOD(void, write_file_header, (j_compress_ptr cinfo));
  67.125 +  JMETHOD(void, write_frame_header, (j_compress_ptr cinfo));
  67.126 +  JMETHOD(void, write_scan_header, (j_compress_ptr cinfo));
  67.127 +  JMETHOD(void, write_file_trailer, (j_compress_ptr cinfo));
  67.128 +  JMETHOD(void, write_tables_only, (j_compress_ptr cinfo));
  67.129 +  /* These routines are exported to allow insertion of extra markers */
  67.130 +  /* Probably only COM and APPn markers should be written this way */
  67.131 +  JMETHOD(void, write_marker_header, (j_compress_ptr cinfo, int marker,
  67.132 +				      unsigned int datalen));
  67.133 +  JMETHOD(void, write_marker_byte, (j_compress_ptr cinfo, int val));
  67.134 +};
  67.135 +
  67.136 +
  67.137 +/* Declarations for decompression modules */
  67.138 +
  67.139 +/* Master control module */
  67.140 +struct jpeg_decomp_master {
  67.141 +  JMETHOD(void, prepare_for_output_pass, (j_decompress_ptr cinfo));
  67.142 +  JMETHOD(void, finish_output_pass, (j_decompress_ptr cinfo));
  67.143 +
  67.144 +  /* State variables made visible to other modules */
  67.145 +  boolean is_dummy_pass;	/* True during 1st pass for 2-pass quant */
  67.146 +};
  67.147 +
  67.148 +/* Input control module */
  67.149 +struct jpeg_input_controller {
  67.150 +  JMETHOD(int, consume_input, (j_decompress_ptr cinfo));
  67.151 +  JMETHOD(void, reset_input_controller, (j_decompress_ptr cinfo));
  67.152 +  JMETHOD(void, start_input_pass, (j_decompress_ptr cinfo));
  67.153 +  JMETHOD(void, finish_input_pass, (j_decompress_ptr cinfo));
  67.154 +
  67.155 +  /* State variables made visible to other modules */
  67.156 +  boolean has_multiple_scans;	/* True if file has multiple scans */
  67.157 +  boolean eoi_reached;		/* True when EOI has been consumed */
  67.158 +};
  67.159 +
  67.160 +/* Main buffer control (downsampled-data buffer) */
  67.161 +struct jpeg_d_main_controller {
  67.162 +  JMETHOD(void, start_pass, (j_decompress_ptr cinfo, J_BUF_MODE pass_mode));
  67.163 +  JMETHOD(void, process_data, (j_decompress_ptr cinfo,
  67.164 +			       JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
  67.165 +			       JDIMENSION out_rows_avail));
  67.166 +};
  67.167 +
  67.168 +/* Coefficient buffer control */
  67.169 +struct jpeg_d_coef_controller {
  67.170 +  JMETHOD(void, start_input_pass, (j_decompress_ptr cinfo));
  67.171 +  JMETHOD(int, consume_data, (j_decompress_ptr cinfo));
  67.172 +  JMETHOD(void, start_output_pass, (j_decompress_ptr cinfo));
  67.173 +  JMETHOD(int, decompress_data, (j_decompress_ptr cinfo,
  67.174 +				 JSAMPIMAGE output_buf));
  67.175 +  /* Pointer to array of coefficient virtual arrays, or NULL if none */
  67.176 +  jvirt_barray_ptr *coef_arrays;
  67.177 +};
  67.178 +
  67.179 +/* Decompression postprocessing (color quantization buffer control) */
  67.180 +struct jpeg_d_post_controller {
  67.181 +  JMETHOD(void, start_pass, (j_decompress_ptr cinfo, J_BUF_MODE pass_mode));
  67.182 +  JMETHOD(void, post_process_data, (j_decompress_ptr cinfo,
  67.183 +				    JSAMPIMAGE input_buf,
  67.184 +				    JDIMENSION *in_row_group_ctr,
  67.185 +				    JDIMENSION in_row_groups_avail,
  67.186 +				    JSAMPARRAY output_buf,
  67.187 +				    JDIMENSION *out_row_ctr,
  67.188 +				    JDIMENSION out_rows_avail));
  67.189 +};
  67.190 +
  67.191 +/* Marker reading & parsing */
  67.192 +struct jpeg_marker_reader {
  67.193 +  JMETHOD(void, reset_marker_reader, (j_decompress_ptr cinfo));
  67.194 +  /* Read markers until SOS or EOI.
  67.195 +   * Returns same codes as are defined for jpeg_consume_input:
  67.196 +   * JPEG_SUSPENDED, JPEG_REACHED_SOS, or JPEG_REACHED_EOI.
  67.197 +   */
  67.198 +  JMETHOD(int, read_markers, (j_decompress_ptr cinfo));
  67.199 +  /* Read a restart marker --- exported for use by entropy decoder only */
  67.200 +  jpeg_marker_parser_method read_restart_marker;
  67.201 +
  67.202 +  /* State of marker reader --- nominally internal, but applications
  67.203 +   * supplying COM or APPn handlers might like to know the state.
  67.204 +   */
  67.205 +  boolean saw_SOI;		/* found SOI? */
  67.206 +  boolean saw_SOF;		/* found SOF? */
  67.207 +  int next_restart_num;		/* next restart number expected (0-7) */
  67.208 +  unsigned int discarded_bytes;	/* # of bytes skipped looking for a marker */
  67.209 +};
  67.210 +
  67.211 +/* Entropy decoding */
  67.212 +struct jpeg_entropy_decoder {
  67.213 +  JMETHOD(void, start_pass, (j_decompress_ptr cinfo));
  67.214 +  JMETHOD(boolean, decode_mcu, (j_decompress_ptr cinfo,
  67.215 +				JBLOCKROW *MCU_data));
  67.216 +
  67.217 +  /* This is here to share code between baseline and progressive decoders; */
  67.218 +  /* other modules probably should not use it */
  67.219 +  boolean insufficient_data;	/* set TRUE after emitting warning */
  67.220 +};
  67.221 +
  67.222 +/* Inverse DCT (also performs dequantization) */
  67.223 +typedef JMETHOD(void, inverse_DCT_method_ptr,
  67.224 +		(j_decompress_ptr cinfo, jpeg_component_info * compptr,
  67.225 +		 JCOEFPTR coef_block,
  67.226 +		 JSAMPARRAY output_buf, JDIMENSION output_col));
  67.227 +
  67.228 +struct jpeg_inverse_dct {
  67.229 +  JMETHOD(void, start_pass, (j_decompress_ptr cinfo));
  67.230 +  /* It is useful to allow each component to have a separate IDCT method. */
  67.231 +  inverse_DCT_method_ptr inverse_DCT[MAX_COMPONENTS];
  67.232 +};
  67.233 +
  67.234 +/* Upsampling (note that upsampler must also call color converter) */
  67.235 +struct jpeg_upsampler {
  67.236 +  JMETHOD(void, start_pass, (j_decompress_ptr cinfo));
  67.237 +  JMETHOD(void, upsample, (j_decompress_ptr cinfo,
  67.238 +			   JSAMPIMAGE input_buf,
  67.239 +			   JDIMENSION *in_row_group_ctr,
  67.240 +			   JDIMENSION in_row_groups_avail,
  67.241 +			   JSAMPARRAY output_buf,
  67.242 +			   JDIMENSION *out_row_ctr,
  67.243 +			   JDIMENSION out_rows_avail));
  67.244 +
  67.245 +  boolean need_context_rows;	/* TRUE if need rows above & below */
  67.246 +};
  67.247 +
  67.248 +/* Colorspace conversion */
  67.249 +struct jpeg_color_deconverter {
  67.250 +  JMETHOD(void, start_pass, (j_decompress_ptr cinfo));
  67.251 +  JMETHOD(void, color_convert, (j_decompress_ptr cinfo,
  67.252 +				JSAMPIMAGE input_buf, JDIMENSION input_row,
  67.253 +				JSAMPARRAY output_buf, int num_rows));
  67.254 +};
  67.255 +
  67.256 +/* Color quantization or color precision reduction */
  67.257 +struct jpeg_color_quantizer {
  67.258 +  JMETHOD(void, start_pass, (j_decompress_ptr cinfo, boolean is_pre_scan));
  67.259 +  JMETHOD(void, color_quantize, (j_decompress_ptr cinfo,
  67.260 +				 JSAMPARRAY input_buf, JSAMPARRAY output_buf,
  67.261 +				 int num_rows));
  67.262 +  JMETHOD(void, finish_pass, (j_decompress_ptr cinfo));
  67.263 +  JMETHOD(void, new_color_map, (j_decompress_ptr cinfo));
  67.264 +};
  67.265 +
  67.266 +
  67.267 +/* Miscellaneous useful macros */
  67.268 +
  67.269 +#undef MAX
  67.270 +#define MAX(a,b)	((a) > (b) ? (a) : (b))
  67.271 +#undef MIN
  67.272 +#define MIN(a,b)	((a) < (b) ? (a) : (b))
  67.273 +
  67.274 +
  67.275 +/* We assume that right shift corresponds to signed division by 2 with
  67.276 + * rounding towards minus infinity.  This is correct for typical "arithmetic
  67.277 + * shift" instructions that shift in copies of the sign bit.  But some
  67.278 + * C compilers implement >> with an unsigned shift.  For these machines you
  67.279 + * must define RIGHT_SHIFT_IS_UNSIGNED.
  67.280 + * RIGHT_SHIFT provides a proper signed right shift of an INT32 quantity.
  67.281 + * It is only applied with constant shift counts.  SHIFT_TEMPS must be
  67.282 + * included in the variables of any routine using RIGHT_SHIFT.
  67.283 + */
  67.284 +
  67.285 +#ifdef RIGHT_SHIFT_IS_UNSIGNED
  67.286 +#define SHIFT_TEMPS	INT32 shift_temp;
  67.287 +#define RIGHT_SHIFT(x,shft)  \
  67.288 +	((shift_temp = (x)) < 0 ? \
  67.289 +	 (shift_temp >> (shft)) | ((~((INT32) 0)) << (32-(shft))) : \
  67.290 +	 (shift_temp >> (shft)))
  67.291 +#else
  67.292 +#define SHIFT_TEMPS
  67.293 +#define RIGHT_SHIFT(x,shft)	((x) >> (shft))
  67.294 +#endif
  67.295 +
  67.296 +
  67.297 +/* Short forms of external names for systems with brain-damaged linkers. */
  67.298 +
  67.299 +#ifdef NEED_SHORT_EXTERNAL_NAMES
  67.300 +#define jinit_compress_master	jICompress
  67.301 +#define jinit_c_master_control	jICMaster
  67.302 +#define jinit_c_main_controller	jICMainC
  67.303 +#define jinit_c_prep_controller	jICPrepC
  67.304 +#define jinit_c_coef_controller	jICCoefC
  67.305 +#define jinit_color_converter	jICColor
  67.306 +#define jinit_downsampler	jIDownsampler
  67.307 +#define jinit_forward_dct	jIFDCT
  67.308 +#define jinit_huff_encoder	jIHEncoder
  67.309 +#define jinit_phuff_encoder	jIPHEncoder
  67.310 +#define jinit_marker_writer	jIMWriter
  67.311 +#define jinit_master_decompress	jIDMaster
  67.312 +#define jinit_d_main_controller	jIDMainC
  67.313 +#define jinit_d_coef_controller	jIDCoefC
  67.314 +#define jinit_d_post_controller	jIDPostC
  67.315 +#define jinit_input_controller	jIInCtlr
  67.316 +#define jinit_marker_reader	jIMReader
  67.317 +#define jinit_huff_decoder	jIHDecoder
  67.318 +#define jinit_phuff_decoder	jIPHDecoder
  67.319 +#define jinit_inverse_dct	jIIDCT
  67.320 +#define jinit_upsampler		jIUpsampler
  67.321 +#define jinit_color_deconverter	jIDColor
  67.322 +#define jinit_1pass_quantizer	jI1Quant
  67.323 +#define jinit_2pass_quantizer	jI2Quant
  67.324 +#define jinit_merged_upsampler	jIMUpsampler
  67.325 +#define jinit_memory_mgr	jIMemMgr
  67.326 +#define jdiv_round_up		jDivRound
  67.327 +#define jround_up		jRound
  67.328 +#define jcopy_sample_rows	jCopySamples
  67.329 +#define jcopy_block_row		jCopyBlocks
  67.330 +#define jzero_far		jZeroFar
  67.331 +#define jpeg_zigzag_order	jZIGTable
  67.332 +#define jpeg_natural_order	jZAGTable
  67.333 +#endif /* NEED_SHORT_EXTERNAL_NAMES */
  67.334 +
  67.335 +
  67.336 +/* Compression module initialization routines */
  67.337 +EXTERN(void) jinit_compress_master JPP((j_compress_ptr cinfo));
  67.338 +EXTERN(void) jinit_c_master_control JPP((j_compress_ptr cinfo,
  67.339 +					 boolean transcode_only));
  67.340 +EXTERN(void) jinit_c_main_controller JPP((j_compress_ptr cinfo,
  67.341 +					  boolean need_full_buffer));
  67.342 +EXTERN(void) jinit_c_prep_controller JPP((j_compress_ptr cinfo,
  67.343 +					  boolean need_full_buffer));
  67.344 +EXTERN(void) jinit_c_coef_controller JPP((j_compress_ptr cinfo,
  67.345 +					  boolean need_full_buffer));
  67.346 +EXTERN(void) jinit_color_converter JPP((j_compress_ptr cinfo));
  67.347 +EXTERN(void) jinit_downsampler JPP((j_compress_ptr cinfo));
  67.348 +EXTERN(void) jinit_forward_dct JPP((j_compress_ptr cinfo));
  67.349 +EXTERN(void) jinit_huff_encoder JPP((j_compress_ptr cinfo));
  67.350 +EXTERN(void) jinit_phuff_encoder JPP((j_compress_ptr cinfo));
  67.351 +EXTERN(void) jinit_marker_writer JPP((j_compress_ptr cinfo));
  67.352 +/* Decompression module initialization routines */
  67.353 +EXTERN(void) jinit_master_decompress JPP((j_decompress_ptr cinfo));
  67.354 +EXTERN(void) jinit_d_main_controller JPP((j_decompress_ptr cinfo,
  67.355 +					  boolean need_full_buffer));
  67.356 +EXTERN(void) jinit_d_coef_controller JPP((j_decompress_ptr cinfo,
  67.357 +					  boolean need_full_buffer));
  67.358 +EXTERN(void) jinit_d_post_controller JPP((j_decompress_ptr cinfo,
  67.359 +					  boolean need_full_buffer));
  67.360 +EXTERN(void) jinit_input_controller JPP((j_decompress_ptr cinfo));
  67.361 +EXTERN(void) jinit_marker_reader JPP((j_decompress_ptr cinfo));
  67.362 +EXTERN(void) jinit_huff_decoder JPP((j_decompress_ptr cinfo));
  67.363 +EXTERN(void) jinit_phuff_decoder JPP((j_decompress_ptr cinfo));
  67.364 +EXTERN(void) jinit_inverse_dct JPP((j_decompress_ptr cinfo));
  67.365 +EXTERN(void) jinit_upsampler JPP((j_decompress_ptr cinfo));
  67.366 +EXTERN(void) jinit_color_deconverter JPP((j_decompress_ptr cinfo));
  67.367 +EXTERN(void) jinit_1pass_quantizer JPP((j_decompress_ptr cinfo));
  67.368 +EXTERN(void) jinit_2pass_quantizer JPP((j_decompress_ptr cinfo));
  67.369 +EXTERN(void) jinit_merged_upsampler JPP((j_decompress_ptr cinfo));
  67.370 +/* Memory manager initialization */
  67.371 +EXTERN(void) jinit_memory_mgr JPP((j_common_ptr cinfo));
  67.372 +
  67.373 +/* Utility routines in jutils.c */
  67.374 +EXTERN(long) jdiv_round_up JPP((long a, long b));
  67.375 +EXTERN(long) jround_up JPP((long a, long b));
  67.376 +EXTERN(void) jcopy_sample_rows JPP((JSAMPARRAY input_array, int source_row,
  67.377 +				    JSAMPARRAY output_array, int dest_row,
  67.378 +				    int num_rows, JDIMENSION num_cols));
  67.379 +EXTERN(void) jcopy_block_row JPP((JBLOCKROW input_row, JBLOCKROW output_row,
  67.380 +				  JDIMENSION num_blocks));
  67.381 +EXTERN(void) jzero_far JPP((void FAR * target, size_t bytestozero));
  67.382 +/* Constant tables in jutils.c */
  67.383 +#if 0				/* This table is not actually needed in v6a */
  67.384 +extern const int jpeg_zigzag_order[]; /* natural coef order to zigzag order */
  67.385 +#endif
  67.386 +extern const int jpeg_natural_order[]; /* zigzag coef order to natural order */
  67.387 +
  67.388 +/* Suppress undefined-structure complaints if necessary. */
  67.389 +
  67.390 +#ifdef INCOMPLETE_TYPES_BROKEN
  67.391 +#ifndef AM_MEMORY_MANAGER	/* only jmemmgr.c defines these */
  67.392 +struct jvirt_sarray_control { long dummy; };
  67.393 +struct jvirt_barray_control { long dummy; };
  67.394 +#endif
  67.395 +#endif /* INCOMPLETE_TYPES_BROKEN */
    68.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    68.2 +++ b/libs/libjpeg/jpeglib.h	Sat Sep 19 05:51:51 2015 +0300
    68.3 @@ -0,0 +1,1096 @@
    68.4 +/*
    68.5 + * jpeglib.h
    68.6 + *
    68.7 + * Copyright (C) 1991-1998, Thomas G. Lane.
    68.8 + * This file is part of the Independent JPEG Group's software.
    68.9 + * For conditions of distribution and use, see the accompanying README file.
   68.10 + *
   68.11 + * This file defines the application interface for the JPEG library.
   68.12 + * Most applications using the library need only include this file,
   68.13 + * and perhaps jerror.h if they want to know the exact error codes.
   68.14 + */
   68.15 +
   68.16 +#ifndef JPEGLIB_H
   68.17 +#define JPEGLIB_H
   68.18 +
   68.19 +/*
   68.20 + * First we include the configuration files that record how this
   68.21 + * installation of the JPEG library is set up.  jconfig.h can be
   68.22 + * generated automatically for many systems.  jmorecfg.h contains
   68.23 + * manual configuration options that most people need not worry about.
   68.24 + */
   68.25 +
   68.26 +#ifndef JCONFIG_INCLUDED	/* in case jinclude.h already did */
   68.27 +#include "jconfig.h"		/* widely used configuration options */
   68.28 +#endif
   68.29 +#include "jmorecfg.h"		/* seldom changed options */
   68.30 +
   68.31 +
   68.32 +/* Version ID for the JPEG library.
   68.33 + * Might be useful for tests like "#if JPEG_LIB_VERSION >= 60".
   68.34 + */
   68.35 +
   68.36 +#define JPEG_LIB_VERSION  62	/* Version 6b */
   68.37 +
   68.38 +
   68.39 +/* Various constants determining the sizes of things.
   68.40 + * All of these are specified by the JPEG standard, so don't change them
   68.41 + * if you want to be compatible.
   68.42 + */
   68.43 +
   68.44 +#define DCTSIZE		    8	/* The basic DCT block is 8x8 samples */
   68.45 +#define DCTSIZE2	    64	/* DCTSIZE squared; # of elements in a block */
   68.46 +#define NUM_QUANT_TBLS      4	/* Quantization tables are numbered 0..3 */
   68.47 +#define NUM_HUFF_TBLS       4	/* Huffman tables are numbered 0..3 */
   68.48 +#define NUM_ARITH_TBLS      16	/* Arith-coding tables are numbered 0..15 */
   68.49 +#define MAX_COMPS_IN_SCAN   4	/* JPEG limit on # of components in one scan */
   68.50 +#define MAX_SAMP_FACTOR     4	/* JPEG limit on sampling factors */
   68.51 +/* Unfortunately, some bozo at Adobe saw no reason to be bound by the standard;
   68.52 + * the PostScript DCT filter can emit files with many more than 10 blocks/MCU.
   68.53 + * If you happen to run across such a file, you can up D_MAX_BLOCKS_IN_MCU
   68.54 + * to handle it.  We even let you do this from the jconfig.h file.  However,
   68.55 + * we strongly discourage changing C_MAX_BLOCKS_IN_MCU; just because Adobe
   68.56 + * sometimes emits noncompliant files doesn't mean you should too.
   68.57 + */
   68.58 +#define C_MAX_BLOCKS_IN_MCU   10 /* compressor's limit on blocks per MCU */
   68.59 +#ifndef D_MAX_BLOCKS_IN_MCU
   68.60 +#define D_MAX_BLOCKS_IN_MCU   10 /* decompressor's limit on blocks per MCU */
   68.61 +#endif
   68.62 +
   68.63 +
   68.64 +/* Data structures for images (arrays of samples and of DCT coefficients).
   68.65 + * On 80x86 machines, the image arrays are too big for near pointers,
   68.66 + * but the pointer arrays can fit in near memory.
   68.67 + */
   68.68 +
   68.69 +typedef JSAMPLE FAR *JSAMPROW;	/* ptr to one image row of pixel samples. */
   68.70 +typedef JSAMPROW *JSAMPARRAY;	/* ptr to some rows (a 2-D sample array) */
   68.71 +typedef JSAMPARRAY *JSAMPIMAGE;	/* a 3-D sample array: top index is color */
   68.72 +
   68.73 +typedef JCOEF JBLOCK[DCTSIZE2];	/* one block of coefficients */
   68.74 +typedef JBLOCK FAR *JBLOCKROW;	/* pointer to one row of coefficient blocks */
   68.75 +typedef JBLOCKROW *JBLOCKARRAY;		/* a 2-D array of coefficient blocks */
   68.76 +typedef JBLOCKARRAY *JBLOCKIMAGE;	/* a 3-D array of coefficient blocks */
   68.77 +
   68.78 +typedef JCOEF FAR *JCOEFPTR;	/* useful in a couple of places */
   68.79 +
   68.80 +
   68.81 +/* Types for JPEG compression parameters and working tables. */
   68.82 +
   68.83 +
   68.84 +/* DCT coefficient quantization tables. */
   68.85 +
   68.86 +typedef struct {
   68.87 +  /* This array gives the coefficient quantizers in natural array order
   68.88 +   * (not the zigzag order in which they are stored in a JPEG DQT marker).
   68.89 +   * CAUTION: IJG versions prior to v6a kept this array in zigzag order.
   68.90 +   */
   68.91 +  UINT16 quantval[DCTSIZE2];	/* quantization step for each coefficient */
   68.92 +  /* This field is used only during compression.  It's initialized FALSE when
   68.93 +   * the table is created, and set TRUE when it's been output to the file.
   68.94 +   * You could suppress output of a table by setting this to TRUE.
   68.95 +   * (See jpeg_suppress_tables for an example.)
   68.96 +   */
   68.97 +  boolean sent_table;		/* TRUE when table has been output */
   68.98 +} JQUANT_TBL;
   68.99 +
  68.100 +
  68.101 +/* Huffman coding tables. */
  68.102 +
  68.103 +typedef struct {
  68.104 +  /* These two fields directly represent the contents of a JPEG DHT marker */
  68.105 +  UINT8 bits[17];		/* bits[k] = # of symbols with codes of */
  68.106 +				/* length k bits; bits[0] is unused */
  68.107 +  UINT8 huffval[256];		/* The symbols, in order of incr code length */
  68.108 +  /* This field is used only during compression.  It's initialized FALSE when
  68.109 +   * the table is created, and set TRUE when it's been output to the file.
  68.110 +   * You could suppress output of a table by setting this to TRUE.
  68.111 +   * (See jpeg_suppress_tables for an example.)
  68.112 +   */
  68.113 +  boolean sent_table;		/* TRUE when table has been output */
  68.114 +} JHUFF_TBL;
  68.115 +
  68.116 +
  68.117 +/* Basic info about one component (color channel). */
  68.118 +
  68.119 +typedef struct {
  68.120 +  /* These values are fixed over the whole image. */
  68.121 +  /* For compression, they must be supplied by parameter setup; */
  68.122 +  /* for decompression, they are read from the SOF marker. */
  68.123 +  int component_id;		/* identifier for this component (0..255) */
  68.124 +  int component_index;		/* its index in SOF or cinfo->comp_info[] */
  68.125 +  int h_samp_factor;		/* horizontal sampling factor (1..4) */
  68.126 +  int v_samp_factor;		/* vertical sampling factor (1..4) */
  68.127 +  int quant_tbl_no;		/* quantization table selector (0..3) */
  68.128 +  /* These values may vary between scans. */
  68.129 +  /* For compression, they must be supplied by parameter setup; */
  68.130 +  /* for decompression, they are read from the SOS marker. */
  68.131 +  /* The decompressor output side may not use these variables. */
  68.132 +  int dc_tbl_no;		/* DC entropy table selector (0..3) */
  68.133 +  int ac_tbl_no;		/* AC entropy table selector (0..3) */
  68.134 +  
  68.135 +  /* Remaining fields should be treated as private by applications. */
  68.136 +  
  68.137 +  /* These values are computed during compression or decompression startup: */
  68.138 +  /* Component's size in DCT blocks.
  68.139 +   * Any dummy blocks added to complete an MCU are not counted; therefore
  68.140 +   * these values do not depend on whether a scan is interleaved or not.
  68.141 +   */
  68.142 +  JDIMENSION width_in_blocks;
  68.143 +  JDIMENSION height_in_blocks;
  68.144 +  /* Size of a DCT block in samples.  Always DCTSIZE for compression.
  68.145 +   * For decompression this is the size of the output from one DCT block,
  68.146 +   * reflecting any scaling we choose to apply during the IDCT step.
  68.147 +   * Values of 1,2,4,8 are likely to be supported.  Note that different
  68.148 +   * components may receive different IDCT scalings.
  68.149 +   */
  68.150 +  int DCT_scaled_size;
  68.151 +  /* The downsampled dimensions are the component's actual, unpadded number
  68.152 +   * of samples at the main buffer (preprocessing/compression interface), thus
  68.153 +   * downsampled_width = ceil(image_width * Hi/Hmax)
  68.154 +   * and similarly for height.  For decompression, IDCT scaling is included, so
  68.155 +   * downsampled_width = ceil(image_width * Hi/Hmax * DCT_scaled_size/DCTSIZE)
  68.156 +   */
  68.157 +  JDIMENSION downsampled_width;	 /* actual width in samples */
  68.158 +  JDIMENSION downsampled_height; /* actual height in samples */
  68.159 +  /* This flag is used only for decompression.  In cases where some of the
  68.160 +   * components will be ignored (eg grayscale output from YCbCr image),
  68.161 +   * we can skip most computations for the unused components.
  68.162 +   */
  68.163 +  boolean component_needed;	/* do we need the value of this component? */
  68.164 +
  68.165 +  /* These values are computed before starting a scan of the component. */
  68.166 +  /* The decompressor output side may not use these variables. */
  68.167 +  int MCU_width;		/* number of blocks per MCU, horizontally */
  68.168 +  int MCU_height;		/* number of blocks per MCU, vertically */
  68.169 +  int MCU_blocks;		/* MCU_width * MCU_height */
  68.170 +  int MCU_sample_width;		/* MCU width in samples, MCU_width*DCT_scaled_size */
  68.171 +  int last_col_width;		/* # of non-dummy blocks across in last MCU */
  68.172 +  int last_row_height;		/* # of non-dummy blocks down in last MCU */
  68.173 +
  68.174 +  /* Saved quantization table for component; NULL if none yet saved.
  68.175 +   * See jdinput.c comments about the need for this information.
  68.176 +   * This field is currently used only for decompression.
  68.177 +   */
  68.178 +  JQUANT_TBL * quant_table;
  68.179 +
  68.180 +  /* Private per-component storage for DCT or IDCT subsystem. */
  68.181 +  void * dct_table;
  68.182 +} jpeg_component_info;
  68.183 +
  68.184 +
  68.185 +/* The script for encoding a multiple-scan file is an array of these: */
  68.186 +
  68.187 +typedef struct {
  68.188 +  int comps_in_scan;		/* number of components encoded in this scan */
  68.189 +  int component_index[MAX_COMPS_IN_SCAN]; /* their SOF/comp_info[] indexes */
  68.190 +  int Ss, Se;			/* progressive JPEG spectral selection parms */
  68.191 +  int Ah, Al;			/* progressive JPEG successive approx. parms */
  68.192 +} jpeg_scan_info;
  68.193 +
  68.194 +/* The decompressor can save APPn and COM markers in a list of these: */
  68.195 +
  68.196 +typedef struct jpeg_marker_struct FAR * jpeg_saved_marker_ptr;
  68.197 +
  68.198 +struct jpeg_marker_struct {
  68.199 +  jpeg_saved_marker_ptr next;	/* next in list, or NULL */
  68.200 +  UINT8 marker;			/* marker code: JPEG_COM, or JPEG_APP0+n */
  68.201 +  unsigned int original_length;	/* # bytes of data in the file */
  68.202 +  unsigned int data_length;	/* # bytes of data saved at data[] */
  68.203 +  JOCTET FAR * data;		/* the data contained in the marker */
  68.204 +  /* the marker length word is not counted in data_length or original_length */
  68.205 +};
  68.206 +
  68.207 +/* Known color spaces. */
  68.208 +
  68.209 +typedef enum {
  68.210 +	JCS_UNKNOWN,		/* error/unspecified */
  68.211 +	JCS_GRAYSCALE,		/* monochrome */
  68.212 +	JCS_RGB,		/* red/green/blue */
  68.213 +	JCS_YCbCr,		/* Y/Cb/Cr (also known as YUV) */
  68.214 +	JCS_CMYK,		/* C/M/Y/K */
  68.215 +	JCS_YCCK		/* Y/Cb/Cr/K */
  68.216 +} J_COLOR_SPACE;
  68.217 +
  68.218 +/* DCT/IDCT algorithm options. */
  68.219 +
  68.220 +typedef enum {
  68.221 +	JDCT_ISLOW,		/* slow but accurate integer algorithm */
  68.222 +	JDCT_IFAST,		/* faster, less accurate integer method */
  68.223 +	JDCT_FLOAT		/* floating-point: accurate, fast on fast HW */
  68.224 +} J_DCT_METHOD;
  68.225 +
  68.226 +#ifndef JDCT_DEFAULT		/* may be overridden in jconfig.h */
  68.227 +#define JDCT_DEFAULT  JDCT_ISLOW
  68.228 +#endif
  68.229 +#ifndef JDCT_FASTEST		/* may be overridden in jconfig.h */
  68.230 +#define JDCT_FASTEST  JDCT_IFAST
  68.231 +#endif
  68.232 +
  68.233 +/* Dithering options for decompression. */
  68.234 +
  68.235 +typedef enum {
  68.236 +	JDITHER_NONE,		/* no dithering */
  68.237 +	JDITHER_ORDERED,	/* simple ordered dither */
  68.238 +	JDITHER_FS		/* Floyd-Steinberg error diffusion dither */
  68.239 +} J_DITHER_MODE;
  68.240 +
  68.241 +
  68.242 +/* Common fields between JPEG compression and decompression master structs. */
  68.243 +
  68.244 +#define jpeg_common_fields \
  68.245 +  struct jpeg_error_mgr * err;	/* Error handler module */\
  68.246 +  struct jpeg_memory_mgr * mem;	/* Memory manager module */\
  68.247 +  struct jpeg_progress_mgr * progress; /* Progress monitor, or NULL if none */\
  68.248 +  void * client_data;		/* Available for use by application */\
  68.249 +  boolean is_decompressor;	/* So common code can tell which is which */\
  68.250 +  int global_state		/* For checking call sequence validity */
  68.251 +
  68.252 +/* Routines that are to be used by both halves of the library are declared
  68.253 + * to receive a pointer to this structure.  There are no actual instances of
  68.254 + * jpeg_common_struct, only of jpeg_compress_struct and jpeg_decompress_struct.
  68.255 + */
  68.256 +struct jpeg_common_struct {
  68.257 +  jpeg_common_fields;		/* Fields common to both master struct types */
  68.258 +  /* Additional fields follow in an actual jpeg_compress_struct or
  68.259 +   * jpeg_decompress_struct.  All three structs must agree on these
  68.260 +   * initial fields!  (This would be a lot cleaner in C++.)
  68.261 +   */
  68.262 +};
  68.263 +
  68.264 +typedef struct jpeg_common_struct * j_common_ptr;
  68.265 +typedef struct jpeg_compress_struct * j_compress_ptr;
  68.266 +typedef struct jpeg_decompress_struct * j_decompress_ptr;
  68.267 +
  68.268 +
  68.269 +/* Master record for a compression instance */
  68.270 +
  68.271 +struct jpeg_compress_struct {
  68.272 +  jpeg_common_fields;		/* Fields shared with jpeg_decompress_struct */
  68.273 +
  68.274 +  /* Destination for compressed data */
  68.275 +  struct jpeg_destination_mgr * dest;
  68.276 +
  68.277 +  /* Description of source image --- these fields must be filled in by
  68.278 +   * outer application before starting compression.  in_color_space must
  68.279 +   * be correct before you can even call jpeg_set_defaults().
  68.280 +   */
  68.281 +
  68.282 +  JDIMENSION image_width;	/* input image width */
  68.283 +  JDIMENSION image_height;	/* input image height */
  68.284 +  int input_components;		/* # of color components in input image */
  68.285 +  J_COLOR_SPACE in_color_space;	/* colorspace of input image */
  68.286 +
  68.287 +  double input_gamma;		/* image gamma of input image */
  68.288 +
  68.289 +  /* Compression parameters --- these fields must be set before calling
  68.290 +   * jpeg_start_compress().  We recommend calling jpeg_set_defaults() to
  68.291 +   * initialize everything to reasonable defaults, then changing anything
  68.292 +   * the application specifically wants to change.  That way you won't get
  68.293 +   * burnt when new parameters are added.  Also note that there are several
  68.294 +   * helper routines to simplify changing parameters.
  68.295 +   */
  68.296 +
  68.297 +  int data_precision;		/* bits of precision in image data */
  68.298 +
  68.299 +  int num_components;		/* # of color components in JPEG image */
  68.300 +  J_COLOR_SPACE jpeg_color_space; /* colorspace of JPEG image */
  68.301 +
  68.302 +  jpeg_component_info * comp_info;
  68.303 +  /* comp_info[i] describes component that appears i'th in SOF */
  68.304 +  
  68.305 +  JQUANT_TBL * quant_tbl_ptrs[NUM_QUANT_TBLS];
  68.306 +  /* ptrs to coefficient quantization tables, or NULL if not defined */
  68.307 +  
  68.308 +  JHUFF_TBL * dc_huff_tbl_ptrs[NUM_HUFF_TBLS];
  68.309 +  JHUFF_TBL * ac_huff_tbl_ptrs[NUM_HUFF_TBLS];
  68.310 +  /* ptrs to Huffman coding tables, or NULL if not defined */
  68.311 +  
  68.312 +  UINT8 arith_dc_L[NUM_ARITH_TBLS]; /* L values for DC arith-coding tables */
  68.313 +  UINT8 arith_dc_U[NUM_ARITH_TBLS]; /* U values for DC arith-coding tables */
  68.314 +  UINT8 arith_ac_K[NUM_ARITH_TBLS]; /* Kx values for AC arith-coding tables */
  68.315 +
  68.316 +  int num_scans;		/* # of entries in scan_info array */
  68.317 +  const jpeg_scan_info * scan_info; /* script for multi-scan file, or NULL */
  68.318 +  /* The default value of scan_info is NULL, which causes a single-scan
  68.319 +   * sequential JPEG file to be emitted.  To create a multi-scan file,
  68.320 +   * set num_scans and scan_info to point to an array of scan definitions.
  68.321 +   */
  68.322 +
  68.323 +  boolean raw_data_in;		/* TRUE=caller supplies downsampled data */
  68.324 +  boolean arith_code;		/* TRUE=arithmetic coding, FALSE=Huffman */
  68.325 +  boolean optimize_coding;	/* TRUE=optimize entropy encoding parms */
  68.326 +  boolean CCIR601_sampling;	/* TRUE=first samples are cosited */
  68.327 +  int smoothing_factor;		/* 1..100, or 0 for no input smoothing */
  68.328 +  J_DCT_METHOD dct_method;	/* DCT algorithm selector */
  68.329 +
  68.330 +  /* The restart interval can be specified in absolute MCUs by setting
  68.331 +   * restart_interval, or in MCU rows by setting restart_in_rows
  68.332 +   * (in which case the correct restart_interval will be figured
  68.333 +   * for each scan).
  68.334 +   */
  68.335 +  unsigned int restart_interval; /* MCUs per restart, or 0 for no restart */
  68.336 +  int restart_in_rows;		/* if > 0, MCU rows per restart interval */
  68.337 +
  68.338 +  /* Parameters controlling emission of special markers. */
  68.339 +
  68.340 +  boolean write_JFIF_header;	/* should a JFIF marker be written? */
  68.341 +  UINT8 JFIF_major_version;	/* What to write for the JFIF version number */
  68.342 +  UINT8 JFIF_minor_version;
  68.343 +  /* These three values are not used by the JPEG code, merely copied */
  68.344 +  /* into the JFIF APP0 marker.  density_unit can be 0 for unknown, */
  68.345 +  /* 1 for dots/inch, or 2 for dots/cm.  Note that the pixel aspect */
  68.346 +  /* ratio is defined by X_density/Y_density even when density_unit=0. */
  68.347 +  UINT8 density_unit;		/* JFIF code for pixel size units */
  68.348 +  UINT16 X_density;		/* Horizontal pixel density */
  68.349 +  UINT16 Y_density;		/* Vertical pixel density */
  68.350 +  boolean write_Adobe_marker;	/* should an Adobe marker be written? */
  68.351 +  
  68.352 +  /* State variable: index of next scanline to be written to
  68.353 +   * jpeg_write_scanlines().  Application may use this to control its
  68.354 +   * processing loop, e.g., "while (next_scanline < image_height)".
  68.355 +   */
  68.356 +
  68.357 +  JDIMENSION next_scanline;	/* 0 .. image_height-1  */
  68.358 +
  68.359 +  /* Remaining fields are known throughout compressor, but generally
  68.360 +   * should not be touched by a surrounding application.
  68.361 +   */
  68.362 +
  68.363 +  /*
  68.364 +   * These fields are computed during compression startup
  68.365 +   */
  68.366 +  boolean progressive_mode;	/* TRUE if scan script uses progressive mode */
  68.367 +  int max_h_samp_factor;	/* largest h_samp_factor */
  68.368 +  int max_v_samp_factor;	/* largest v_samp_factor */
  68.369 +
  68.370 +  JDIMENSION total_iMCU_rows;	/* # of iMCU rows to be input to coef ctlr */
  68.371 +  /* The coefficient controller receives data in units of MCU rows as defined
  68.372 +   * for fully interleaved scans (whether the JPEG file is interleaved or not).
  68.373 +   * There are v_samp_factor * DCTSIZE sample rows of each component in an
  68.374 +   * "iMCU" (interleaved MCU) row.
  68.375 +   */
  68.376 +  
  68.377 +  /*
  68.378 +   * These fields are valid during any one scan.
  68.379 +   * They describe the components and MCUs actually appearing in the scan.
  68.380 +   */
  68.381 +  int comps_in_scan;		/* # of JPEG components in this scan */
  68.382 +  jpeg_component_info * cur_comp_info[MAX_COMPS_IN_SCAN];
  68.383 +  /* *cur_comp_info[i] describes component that appears i'th in SOS */
  68.384 +  
  68.385 +  JDIMENSION MCUs_per_row;	/* # of MCUs across the image */
  68.386 +  JDIMENSION MCU_rows_in_scan;	/* # of MCU rows in the image */
  68.387 +  
  68.388 +  int blocks_in_MCU;		/* # of DCT blocks per MCU */
  68.389 +  int MCU_membership[C_MAX_BLOCKS_IN_MCU];
  68.390 +  /* MCU_membership[i] is index in cur_comp_info of component owning */
  68.391 +  /* i'th block in an MCU */
  68.392 +
  68.393 +  int Ss, Se, Ah, Al;		/* progressive JPEG parameters for scan */
  68.394 +
  68.395 +  /*
  68.396 +   * Links to compression subobjects (methods and private variables of modules)
  68.397 +   */
  68.398 +  struct jpeg_comp_master * master;
  68.399 +  struct jpeg_c_main_controller * main;
  68.400 +  struct jpeg_c_prep_controller * prep;
  68.401 +  struct jpeg_c_coef_controller * coef;
  68.402 +  struct jpeg_marker_writer * marker;
  68.403 +  struct jpeg_color_converter * cconvert;
  68.404 +  struct jpeg_downsampler * downsample;
  68.405 +  struct jpeg_forward_dct * fdct;
  68.406 +  struct jpeg_entropy_encoder * entropy;
  68.407 +  jpeg_scan_info * script_space; /* workspace for jpeg_simple_progression */
  68.408 +  int script_space_size;
  68.409 +};
  68.410 +
  68.411 +
  68.412 +/* Master record for a decompression instance */
  68.413 +
  68.414 +struct jpeg_decompress_struct {
  68.415 +  jpeg_common_fields;		/* Fields shared with jpeg_compress_struct */
  68.416 +
  68.417 +  /* Source of compressed data */
  68.418 +  struct jpeg_source_mgr * src;
  68.419 +
  68.420 +  /* Basic description of image --- filled in by jpeg_read_header(). */
  68.421 +  /* Application may inspect these values to decide how to process image. */
  68.422 +
  68.423 +  JDIMENSION image_width;	/* nominal image width (from SOF marker) */
  68.424 +  JDIMENSION image_height;	/* nominal image height */
  68.425 +  int num_components;		/* # of color components in JPEG image */
  68.426 +  J_COLOR_SPACE jpeg_color_space; /* colorspace of JPEG image */
  68.427 +
  68.428 +  /* Decompression processing parameters --- these fields must be set before
  68.429 +   * calling jpeg_start_decompress().  Note that jpeg_read_header() initializes
  68.430 +   * them to default values.
  68.431 +   */
  68.432 +
  68.433 +  J_COLOR_SPACE out_color_space; /* colorspace for output */
  68.434 +
  68.435 +  unsigned int scale_num, scale_denom; /* fraction by which to scale image */
  68.436 +
  68.437 +  double output_gamma;		/* image gamma wanted in output */
  68.438 +
  68.439 +  boolean buffered_image;	/* TRUE=multiple output passes */
  68.440 +  boolean raw_data_out;		/* TRUE=downsampled data wanted */
  68.441 +
  68.442 +  J_DCT_METHOD dct_method;	/* IDCT algorithm selector */
  68.443 +  boolean do_fancy_upsampling;	/* TRUE=apply fancy upsampling */
  68.444 +  boolean do_block_smoothing;	/* TRUE=apply interblock smoothing */
  68.445 +
  68.446 +  boolean quantize_colors;	/* TRUE=colormapped output wanted */
  68.447 +  /* the following are ignored if not quantize_colors: */
  68.448 +  J_DITHER_MODE dither_mode;	/* type of color dithering to use */
  68.449 +  boolean two_pass_quantize;	/* TRUE=use two-pass color quantization */
  68.450 +  int desired_number_of_colors;	/* max # colors to use in created colormap */
  68.451 +  /* these are significant only in buffered-image mode: */
  68.452 +  boolean enable_1pass_quant;	/* enable future use of 1-pass quantizer */
  68.453 +  boolean enable_external_quant;/* enable future use of external colormap */
  68.454 +  boolean enable_2pass_quant;	/* enable future use of 2-pass quantizer */
  68.455 +
  68.456 +  /* Description of actual output image that will be returned to application.
  68.457 +   * These fields are computed by jpeg_start_decompress().
  68.458 +   * You can also use jpeg_calc_output_dimensions() to determine these values
  68.459 +   * in advance of calling jpeg_start_decompress().
  68.460 +   */
  68.461 +
  68.462 +  JDIMENSION output_width;	/* scaled image width */
  68.463 +  JDIMENSION output_height;	/* scaled image height */
  68.464 +  int out_color_components;	/* # of color components in out_color_space */
  68.465 +  int output_components;	/* # of color components returned */
  68.466 +  /* output_components is 1 (a colormap index) when quantizing colors;
  68.467 +   * otherwise it equals out_color_components.
  68.468 +   */
  68.469 +  int rec_outbuf_height;	/* min recommended height of scanline buffer */
  68.470 +  /* If the buffer passed to jpeg_read_scanlines() is less than this many rows
  68.471 +   * high, space and time will be wasted due to unnecessary data copying.
  68.472 +   * Usually rec_outbuf_height will be 1 or 2, at most 4.
  68.473 +   */
  68.474 +
  68.475 +  /* When quantizing colors, the output colormap is described by these fields.
  68.476 +   * The application can supply a colormap by setting colormap non-NULL before
  68.477 +   * calling jpeg_start_decompress; otherwise a colormap is created during
  68.478 +   * jpeg_start_decompress or jpeg_start_output.
  68.479 +   * The map has out_color_components rows and actual_number_of_colors columns.
  68.480 +   */
  68.481 +  int actual_number_of_colors;	/* number of entries in use */
  68.482 +  JSAMPARRAY colormap;		/* The color map as a 2-D pixel array */
  68.483 +
  68.484 +  /* State variables: these variables indicate the progress of decompression.
  68.485 +   * The application may examine these but must not modify them.
  68.486 +   */
  68.487 +
  68.488 +  /* Row index of next scanline to be read from jpeg_read_scanlines().
  68.489 +   * Application may use this to control its processing loop, e.g.,
  68.490 +   * "while (output_scanline < output_height)".
  68.491 +   */
  68.492 +  JDIMENSION output_scanline;	/* 0 .. output_height-1  */
  68.493 +
  68.494 +  /* Current input scan number and number of iMCU rows completed in scan.
  68.495 +   * These indicate the progress of the decompressor input side.
  68.496 +   */
  68.497 +  int input_scan_number;	/* Number of SOS markers seen so far */
  68.498 +  JDIMENSION input_iMCU_row;	/* Number of iMCU rows completed */
  68.499 +
  68.500 +  /* The "output scan number" is the notional scan being displayed by the
  68.501 +   * output side.  The decompressor will not allow output scan/row number
  68.502 +   * to get ahead of input scan/row, but it can fall arbitrarily far behind.
  68.503 +   */
  68.504 +  int output_scan_number;	/* Nominal scan number being displayed */
  68.505 +  JDIMENSION output_iMCU_row;	/* Number of iMCU rows read */
  68.506 +
  68.507 +  /* Current progression status.  coef_bits[c][i] indicates the precision
  68.508 +   * with which component c's DCT coefficient i (in zigzag order) is known.
  68.509 +   * It is -1 when no data has yet been received, otherwise it is the point
  68.510 +   * transform (shift) value for the most recent scan of the coefficient
  68.511 +   * (thus, 0 at completion of the progression).
  68.512 +   * This pointer is NULL when reading a non-progressive file.
  68.513 +   */
  68.514 +  int (*coef_bits)[DCTSIZE2];	/* -1 or current Al value for each coef */
  68.515 +
  68.516 +  /* Internal JPEG parameters --- the application usually need not look at
  68.517 +   * these fields.  Note that the decompressor output side may not use
  68.518 +   * any parameters that can change between scans.
  68.519 +   */
  68.520 +
  68.521 +  /* Quantization and Huffman tables are carried forward across input
  68.522 +   * datastreams when processing abbreviated JPEG datastreams.
  68.523 +   */
  68.524 +
  68.525 +  JQUANT_TBL * quant_tbl_ptrs[NUM_QUANT_TBLS];
  68.526 +  /* ptrs to coefficient quantization tables, or NULL if not defined */
  68.527 +
  68.528 +  JHUFF_TBL * dc_huff_tbl_ptrs[NUM_HUFF_TBLS];
  68.529 +  JHUFF_TBL * ac_huff_tbl_ptrs[NUM_HUFF_TBLS];
  68.530 +  /* ptrs to Huffman coding tables, or NULL if not defined */
  68.531 +
  68.532 +  /* These parameters are never carried across datastreams, since they
  68.533 +   * are given in SOF/SOS markers or defined to be reset by SOI.
  68.534 +   */
  68.535 +
  68.536 +  int data_precision;		/* bits of precision in image data */
  68.537 +
  68.538 +  jpeg_component_info * comp_info;
  68.539 +  /* comp_info[i] describes component that appears i'th in SOF */
  68.540 +
  68.541 +  boolean progressive_mode;	/* TRUE if SOFn specifies progressive mode */
  68.542 +  boolean arith_code;		/* TRUE=arithmetic coding, FALSE=Huffman */
  68.543 +
  68.544 +  UINT8 arith_dc_L[NUM_ARITH_TBLS]; /* L values for DC arith-coding tables */
  68.545 +  UINT8 arith_dc_U[NUM_ARITH_TBLS]; /* U values for DC arith-coding tables */
  68.546 +  UINT8 arith_ac_K[NUM_ARITH_TBLS]; /* Kx values for AC arith-coding tables */
  68.547 +
  68.548 +  unsigned int restart_interval; /* MCUs per restart interval, or 0 for no restart */
  68.549 +
  68.550 +  /* These fields record data obtained from optional markers recognized by
  68.551 +   * the JPEG library.
  68.552 +   */
  68.553 +  boolean saw_JFIF_marker;	/* TRUE iff a JFIF APP0 marker was found */
  68.554 +  /* Data copied from JFIF marker; only valid if saw_JFIF_marker is TRUE: */
  68.555 +  UINT8 JFIF_major_version;	/* JFIF version number */
  68.556 +  UINT8 JFIF_minor_version;
  68.557 +  UINT8 density_unit;		/* JFIF code for pixel size units */
  68.558 +  UINT16 X_density;		/* Horizontal pixel density */
  68.559 +  UINT16 Y_density;		/* Vertical pixel density */
  68.560 +  boolean saw_Adobe_marker;	/* TRUE iff an Adobe APP14 marker was found */
  68.561 +  UINT8 Adobe_transform;	/* Color transform code from Adobe marker */
  68.562 +
  68.563 +  boolean CCIR601_sampling;	/* TRUE=first samples are cosited */
  68.564 +
  68.565 +  /* Aside from the specific data retained from APPn markers known to the
  68.566 +   * library, the uninterpreted contents of any or all APPn and COM markers
  68.567 +   * can be saved in a list for examination by the application.
  68.568 +   */
  68.569 +  jpeg_saved_marker_ptr marker_list; /* Head of list of saved markers */
  68.570 +
  68.571 +  /* Remaining fields are known throughout decompressor, but generally
  68.572 +   * should not be touched by a surrounding application.
  68.573 +   */
  68.574 +
  68.575 +  /*
  68.576 +   * These fields are computed during decompression startup
  68.577 +   */
  68.578 +  int max_h_samp_factor;	/* largest h_samp_factor */
  68.579 +  int max_v_samp_factor;	/* largest v_samp_factor */
  68.580 +
  68.581 +  int min_DCT_scaled_size;	/* smallest DCT_scaled_size of any component */
  68.582 +
  68.583 +  JDIMENSION total_iMCU_rows;	/* # of iMCU rows in image */
  68.584 +  /* The coefficient controller's input and output progress is measured in
  68.585 +   * units of "iMCU" (interleaved MCU) rows.  These are the same as MCU rows
  68.586 +   * in fully interleaved JPEG scans, but are used whether the scan is
  68.587 +   * interleaved or not.  We define an iMCU row as v_samp_factor DCT block
  68.588 +   * rows of each component.  Therefore, the IDCT output contains
  68.589 +   * v_samp_factor*DCT_scaled_size sample rows of a component per iMCU row.
  68.590 +   */
  68.591 +
  68.592 +  JSAMPLE * sample_range_limit; /* table for fast range-limiting */
  68.593 +
  68.594 +  /*
  68.595 +   * These fields are valid during any one scan.
  68.596 +   * They describe the components and MCUs actually appearing in the scan.
  68.597 +   * Note that the decompressor output side must not use these fields.
  68.598 +   */
  68.599 +  int comps_in_scan;		/* # of JPEG components in this scan */
  68.600 +  jpeg_component_info * cur_comp_info[MAX_COMPS_IN_SCAN];
  68.601 +  /* *cur_comp_info[i] describes component that appears i'th in SOS */
  68.602 +
  68.603 +  JDIMENSION MCUs_per_row;	/* # of MCUs across the image */
  68.604 +  JDIMENSION MCU_rows_in_scan;	/* # of MCU rows in the image */
  68.605 +
  68.606 +  int blocks_in_MCU;		/* # of DCT blocks per MCU */
  68.607 +  int MCU_membership[D_MAX_BLOCKS_IN_MCU];
  68.608 +  /* MCU_membership[i] is index in cur_comp_info of component owning */
  68.609 +  /* i'th block in an MCU */
  68.610 +
  68.611 +  int Ss, Se, Ah, Al;		/* progressive JPEG parameters for scan */
  68.612 +
  68.613 +  /* This field is shared between entropy decoder and marker parser.
  68.614 +   * It is either zero or the code of a JPEG marker that has been
  68.615 +   * read from the data source, but has not yet been processed.
  68.616 +   */
  68.617 +  int unread_marker;
  68.618 +
  68.619 +  /*
  68.620 +   * Links to decompression subobjects (methods, private variables of modules)
  68.621 +   */
  68.622 +  struct jpeg_decomp_master * master;
  68.623 +  struct jpeg_d_main_controller * main;
  68.624 +  struct jpeg_d_coef_controller * coef;
  68.625 +  struct jpeg_d_post_controller * post;
  68.626 +  struct jpeg_input_controller * inputctl;
  68.627 +  struct jpeg_marker_reader * marker;
  68.628 +  struct jpeg_entropy_decoder * entropy;
  68.629 +  struct jpeg_inverse_dct * idct;
  68.630 +  struct jpeg_upsampler * upsample;
  68.631 +  struct jpeg_color_deconverter * cconvert;
  68.632 +  struct jpeg_color_quantizer * cquantize;
  68.633 +};
  68.634 +
  68.635 +
  68.636 +/* "Object" declarations for JPEG modules that may be supplied or called
  68.637 + * directly by the surrounding application.
  68.638 + * As with all objects in the JPEG library, these structs only define the
  68.639 + * publicly visible methods and state variables of a module.  Additional
  68.640 + * private fields may exist after the public ones.
  68.641 + */
  68.642 +
  68.643 +
  68.644 +/* Error handler object */
  68.645 +
  68.646 +struct jpeg_error_mgr {
  68.647 +  /* Error exit handler: does not return to caller */
  68.648 +  JMETHOD(void, error_exit, (j_common_ptr cinfo));
  68.649 +  /* Conditionally emit a trace or warning message */
  68.650 +  JMETHOD(void, emit_message, (j_common_ptr cinfo, int msg_level));
  68.651 +  /* Routine that actually outputs a trace or error message */
  68.652 +  JMETHOD(void, output_message, (j_common_ptr cinfo));
  68.653 +  /* Format a message string for the most recent JPEG error or message */
  68.654 +  JMETHOD(void, format_message, (j_common_ptr cinfo, char * buffer));
  68.655 +#define JMSG_LENGTH_MAX  200	/* recommended size of format_message buffer */
  68.656 +  /* Reset error state variables at start of a new image */
  68.657 +  JMETHOD(void, reset_error_mgr, (j_common_ptr cinfo));
  68.658 +  
  68.659 +  /* The message ID code and any parameters are saved here.
  68.660 +   * A message can have one string parameter or up to 8 int parameters.
  68.661 +   */
  68.662 +  int msg_code;
  68.663 +#define JMSG_STR_PARM_MAX  80
  68.664 +  union {
  68.665 +    int i[8];
  68.666 +    char s[JMSG_STR_PARM_MAX];
  68.667 +  } msg_parm;
  68.668 +  
  68.669 +  /* Standard state variables for error facility */
  68.670 +  
  68.671 +  int trace_level;		/* max msg_level that will be displayed */
  68.672 +  
  68.673 +  /* For recoverable corrupt-data errors, we emit a warning message,
  68.674 +   * but keep going unless emit_message chooses to abort.  emit_message
  68.675 +   * should count warnings in num_warnings.  The surrounding application
  68.676 +   * can check for bad data by seeing if num_warnings is nonzero at the
  68.677 +   * end of processing.
  68.678 +   */
  68.679 +  long num_warnings;		/* number of corrupt-data warnings */
  68.680 +
  68.681 +  /* These fields point to the table(s) of error message strings.
  68.682 +   * An application can change the table pointer to switch to a different
  68.683 +   * message list (typically, to change the language in which errors are
  68.684 +   * reported).  Some applications may wish to add additional error codes
  68.685 +   * that will be handled by the JPEG library error mechanism; the second
  68.686 +   * table pointer is used for this purpose.
  68.687 +   *
  68.688 +   * First table includes all errors generated by JPEG library itself.
  68.689 +   * Error code 0 is reserved for a "no such error string" message.
  68.690 +   */
  68.691 +  const char * const * jpeg_message_table; /* Library errors */
  68.692 +  int last_jpeg_message;    /* Table contains strings 0..last_jpeg_message */
  68.693 +  /* Second table can be added by application (see cjpeg/djpeg for example).
  68.694 +   * It contains strings numbered first_addon_message..last_addon_message.
  68.695 +   */
  68.696 +  const char * const * addon_message_table; /* Non-library errors */
  68.697 +  int first_addon_message;	/* code for first string in addon table */
  68.698 +  int last_addon_message;	/* code for last string in addon table */
  68.699 +};
  68.700 +
  68.701 +
  68.702 +/* Progress monitor object */
  68.703 +
  68.704 +struct jpeg_progress_mgr {
  68.705 +  JMETHOD(void, progress_monitor, (j_common_ptr cinfo));
  68.706 +
  68.707 +  long pass_counter;		/* work units completed in this pass */
  68.708 +  long pass_limit;		/* total number of work units in this pass */
  68.709 +  int completed_passes;		/* passes completed so far */
  68.710 +  int total_passes;		/* total number of passes expected */
  68.711 +};
  68.712 +
  68.713 +
  68.714 +/* Data destination object for compression */
  68.715 +
  68.716 +struct jpeg_destination_mgr {
  68.717 +  JOCTET * next_output_byte;	/* => next byte to write in buffer */
  68.718 +  size_t free_in_buffer;	/* # of byte spaces remaining in buffer */
  68.719 +
  68.720 +  JMETHOD(void, init_destination, (j_compress_ptr cinfo));
  68.721 +  JMETHOD(boolean, empty_output_buffer, (j_compress_ptr cinfo));
  68.722 +  JMETHOD(void, term_destination, (j_compress_ptr cinfo));
  68.723 +};
  68.724 +
  68.725 +
  68.726 +/* Data source object for decompression */
  68.727 +
  68.728 +struct jpeg_source_mgr {
  68.729 +  const JOCTET * next_input_byte; /* => next byte to read from buffer */
  68.730 +  size_t bytes_in_buffer;	/* # of bytes remaining in buffer */
  68.731 +
  68.732 +  JMETHOD(void, init_source, (j_decompress_ptr cinfo));
  68.733 +  JMETHOD(boolean, fill_input_buffer, (j_decompress_ptr cinfo));
  68.734 +  JMETHOD(void, skip_input_data, (j_decompress_ptr cinfo, long num_bytes));
  68.735 +  JMETHOD(boolean, resync_to_restart, (j_decompress_ptr cinfo, int desired));
  68.736 +  JMETHOD(void, term_source, (j_decompress_ptr cinfo));
  68.737 +};
  68.738 +
  68.739 +
  68.740 +/* Memory manager object.
  68.741 + * Allocates "small" objects (a few K total), "large" objects (tens of K),
  68.742 + * and "really big" objects (virtual arrays with backing store if needed).
  68.743 + * The memory manager does not allow individual objects to be freed; rather,
  68.744 + * each created object is assigned to a pool, and whole pools can be freed
  68.745 + * at once.  This is faster and more convenient than remembering exactly what
  68.746 + * to free, especially where malloc()/free() are not too speedy.
  68.747 + * NB: alloc routines never return NULL.  They exit to error_exit if not
  68.748 + * successful.
  68.749 + */
  68.750 +
  68.751 +#define JPOOL_PERMANENT	0	/* lasts until master record is destroyed */
  68.752 +#define JPOOL_IMAGE	1	/* lasts until done with image/datastream */
  68.753 +#define JPOOL_NUMPOOLS	2
  68.754 +
  68.755 +typedef struct jvirt_sarray_control * jvirt_sarray_ptr;
  68.756 +typedef struct jvirt_barray_control * jvirt_barray_ptr;
  68.757 +
  68.758 +
  68.759 +struct jpeg_memory_mgr {
  68.760 +  /* Method pointers */
  68.761 +  JMETHOD(void *, alloc_small, (j_common_ptr cinfo, int pool_id,
  68.762 +				size_t sizeofobject));
  68.763 +  JMETHOD(void FAR *, alloc_large, (j_common_ptr cinfo, int pool_id,
  68.764 +				     size_t sizeofobject));
  68.765 +  JMETHOD(JSAMPARRAY, alloc_sarray, (j_common_ptr cinfo, int pool_id,
  68.766 +				     JDIMENSION samplesperrow,
  68.767 +				     JDIMENSION numrows));
  68.768 +  JMETHOD(JBLOCKARRAY, alloc_barray, (j_common_ptr cinfo, int pool_id,
  68.769 +				      JDIMENSION blocksperrow,
  68.770 +				      JDIMENSION numrows));
  68.771 +  JMETHOD(jvirt_sarray_ptr, request_virt_sarray, (j_common_ptr cinfo,
  68.772 +						  int pool_id,
  68.773 +						  boolean pre_zero,
  68.774 +						  JDIMENSION samplesperrow,
  68.775 +						  JDIMENSION numrows,
  68.776 +						  JDIMENSION maxaccess));
  68.777 +  JMETHOD(jvirt_barray_ptr, request_virt_barray, (j_common_ptr cinfo,
  68.778 +						  int pool_id,
  68.779 +						  boolean pre_zero,
  68.780 +						  JDIMENSION blocksperrow,
  68.781 +						  JDIMENSION numrows,
  68.782 +						  JDIMENSION maxaccess));
  68.783 +  JMETHOD(void, realize_virt_arrays, (j_common_ptr cinfo));
  68.784 +  JMETHOD(JSAMPARRAY, access_virt_sarray, (j_common_ptr cinfo,
  68.785 +					   jvirt_sarray_ptr ptr,
  68.786 +					   JDIMENSION start_row,
  68.787 +					   JDIMENSION num_rows,
  68.788 +					   boolean writable));
  68.789 +  JMETHOD(JBLOCKARRAY, access_virt_barray, (j_common_ptr cinfo,
  68.790 +					    jvirt_barray_ptr ptr,
  68.791 +					    JDIMENSION start_row,
  68.792 +					    JDIMENSION num_rows,
  68.793 +					    boolean writable));
  68.794 +  JMETHOD(void, free_pool, (j_common_ptr cinfo, int pool_id));
  68.795 +  JMETHOD(void, self_destruct, (j_common_ptr cinfo));
  68.796 +
  68.797 +  /* Limit on memory allocation for this JPEG object.  (Note that this is
  68.798 +   * merely advisory, not a guaranteed maximum; it only affects the space
  68.799 +   * used for virtual-array buffers.)  May be changed by outer application
  68.800 +   * after creating the JPEG object.
  68.801 +   */
  68.802 +  long max_memory_to_use;
  68.803 +
  68.804 +  /* Maximum allocation request accepted by alloc_large. */
  68.805 +  long max_alloc_chunk;
  68.806 +};
  68.807 +
  68.808 +
  68.809 +/* Routine signature for application-supplied marker processing methods.
  68.810 + * Need not pass marker code since it is stored in cinfo->unread_marker.
  68.811 + */
  68.812 +typedef JMETHOD(boolean, jpeg_marker_parser_method, (j_decompress_ptr cinfo));
  68.813 +
  68.814 +
  68.815 +/* Declarations for routines called by application.
  68.816 + * The JPP macro hides prototype parameters from compilers that can't cope.
  68.817 + * Note JPP requires double parentheses.
  68.818 + */
  68.819 +
  68.820 +#ifdef HAVE_PROTOTYPES
  68.821 +#define JPP(arglist)	arglist
  68.822 +#else
  68.823 +#define JPP(arglist)	()
  68.824 +#endif
  68.825 +
  68.826 +
  68.827 +/* Short forms of external names for systems with brain-damaged linkers.
  68.828 + * We shorten external names to be unique in the first six letters, which
  68.829 + * is good enough for all known systems.
  68.830 + * (If your compiler itself needs names to be unique in less than 15 
  68.831 + * characters, you are out of luck.  Get a better compiler.)
  68.832 + */
  68.833 +
  68.834 +#ifdef NEED_SHORT_EXTERNAL_NAMES
  68.835 +#define jpeg_std_error		jStdError
  68.836 +#define jpeg_CreateCompress	jCreaCompress
  68.837 +#define jpeg_CreateDecompress	jCreaDecompress
  68.838 +#define jpeg_destroy_compress	jDestCompress
  68.839 +#define jpeg_destroy_decompress	jDestDecompress
  68.840 +#define jpeg_stdio_dest		jStdDest
  68.841 +#define jpeg_stdio_src		jStdSrc
  68.842 +#define jpeg_set_defaults	jSetDefaults
  68.843 +#define jpeg_set_colorspace	jSetColorspace
  68.844 +#define jpeg_default_colorspace	jDefColorspace
  68.845 +#define jpeg_set_quality	jSetQuality
  68.846 +#define jpeg_set_linear_quality	jSetLQuality
  68.847 +#define jpeg_add_quant_table	jAddQuantTable
  68.848 +#define jpeg_quality_scaling	jQualityScaling
  68.849 +#define jpeg_simple_progression	jSimProgress
  68.850 +#define jpeg_suppress_tables	jSuppressTables
  68.851 +#define jpeg_alloc_quant_table	jAlcQTable
  68.852 +#define jpeg_alloc_huff_table	jAlcHTable
  68.853 +#define jpeg_start_compress	jStrtCompress
  68.854 +#define jpeg_write_scanlines	jWrtScanlines
  68.855 +#define jpeg_finish_compress	jFinCompress
  68.856 +#define jpeg_write_raw_data	jWrtRawData
  68.857 +#define jpeg_write_marker	jWrtMarker
  68.858 +#define jpeg_write_m_header	jWrtMHeader
  68.859 +#define jpeg_write_m_byte	jWrtMByte
  68.860 +#define jpeg_write_tables	jWrtTables
  68.861 +#define jpeg_read_header	jReadHeader
  68.862 +#define jpeg_start_decompress	jStrtDecompress
  68.863 +#define jpeg_read_scanlines	jReadScanlines
  68.864 +#define jpeg_finish_decompress	jFinDecompress
  68.865 +#define jpeg_read_raw_data	jReadRawData
  68.866 +#define jpeg_has_multiple_scans	jHasMultScn
  68.867 +#define jpeg_start_output	jStrtOutput
  68.868 +#define jpeg_finish_output	jFinOutput
  68.869 +#define jpeg_input_complete	jInComplete
  68.870 +#define jpeg_new_colormap	jNewCMap
  68.871 +#define jpeg_consume_input	jConsumeInput
  68.872 +#define jpeg_calc_output_dimensions	jCalcDimensions
  68.873 +#define jpeg_save_markers	jSaveMarkers
  68.874 +#define jpeg_set_marker_processor	jSetMarker
  68.875 +#define jpeg_read_coefficients	jReadCoefs
  68.876 +#define jpeg_write_coefficients	jWrtCoefs
  68.877 +#define jpeg_copy_critical_parameters	jCopyCrit
  68.878 +#define jpeg_abort_compress	jAbrtCompress
  68.879 +#define jpeg_abort_decompress	jAbrtDecompress
  68.880 +#define jpeg_abort		jAbort
  68.881 +#define jpeg_destroy		jDestroy
  68.882 +#define jpeg_resync_to_restart	jResyncRestart
  68.883 +#endif /* NEED_SHORT_EXTERNAL_NAMES */
  68.884 +
  68.885 +
  68.886 +/* Default error-management setup */
  68.887 +EXTERN(struct jpeg_error_mgr *) jpeg_std_error
  68.888 +	JPP((struct jpeg_error_mgr * err));
  68.889 +
  68.890 +/* Initialization of JPEG compression objects.
  68.891 + * jpeg_create_compress() and jpeg_create_decompress() are the exported
  68.892 + * names that applications should call.  These expand to calls on
  68.893 + * jpeg_CreateCompress and jpeg_CreateDecompress with additional information
  68.894 + * passed for version mismatch checking.
  68.895 + * NB: you must set up the error-manager BEFORE calling jpeg_create_xxx.
  68.896 + */
  68.897 +#define jpeg_create_compress(cinfo) \
  68.898 +    jpeg_CreateCompress((cinfo), JPEG_LIB_VERSION, \
  68.899 +			(size_t) sizeof(struct jpeg_compress_struct))
  68.900 +#define jpeg_create_decompress(cinfo) \
  68.901 +    jpeg_CreateDecompress((cinfo), JPEG_LIB_VERSION, \
  68.902 +			  (size_t) sizeof(struct jpeg_decompress_struct))
  68.903 +EXTERN(void) jpeg_CreateCompress JPP((j_compress_ptr cinfo,
  68.904 +				      int version, size_t structsize));
  68.905 +EXTERN(void) jpeg_CreateDecompress JPP((j_decompress_ptr cinfo,
  68.906 +					int version, size_t structsize));
  68.907 +/* Destruction of JPEG compression objects */
  68.908 +EXTERN(void) jpeg_destroy_compress JPP((j_compress_ptr cinfo));
  68.909 +EXTERN(void) jpeg_destroy_decompress JPP((j_decompress_ptr cinfo));
  68.910 +
  68.911 +/* Standard data source and destination managers: stdio streams. */
  68.912 +/* Caller is responsible for opening the file before and closing after. */
  68.913 +EXTERN(void) jpeg_stdio_dest JPP((j_compress_ptr cinfo, FILE * outfile));
  68.914 +EXTERN(void) jpeg_stdio_src JPP((j_decompress_ptr cinfo, FILE * infile));
  68.915 +
  68.916 +/* Default parameter setup for compression */
  68.917 +EXTERN(void) jpeg_set_defaults JPP((j_compress_ptr cinfo));
  68.918 +/* Compression parameter setup aids */
  68.919 +EXTERN(void) jpeg_set_colorspace JPP((j_compress_ptr cinfo,
  68.920 +				      J_COLOR_SPACE colorspace));
  68.921 +EXTERN(void) jpeg_default_colorspace JPP((j_compress_ptr cinfo));
  68.922 +EXTERN(void) jpeg_set_quality JPP((j_compress_ptr cinfo, int quality,
  68.923 +				   boolean force_baseline));
  68.924 +EXTERN(void) jpeg_set_linear_quality JPP((j_compress_ptr cinfo,
  68.925 +					  int scale_factor,
  68.926 +					  boolean force_baseline));
  68.927 +EXTERN(void) jpeg_add_quant_table JPP((j_compress_ptr cinfo, int which_tbl,
  68.928 +				       const unsigned int *basic_table,
  68.929 +				       int scale_factor,
  68.930 +				       boolean force_baseline));
  68.931 +EXTERN(int) jpeg_quality_scaling JPP((int quality));
  68.932 +EXTERN(void) jpeg_simple_progression JPP((j_compress_ptr cinfo));
  68.933 +EXTERN(void) jpeg_suppress_tables JPP((j_compress_ptr cinfo,
  68.934 +				       boolean suppress));
  68.935 +EXTERN(JQUANT_TBL *) jpeg_alloc_quant_table JPP((j_common_ptr cinfo));
  68.936 +EXTERN(JHUFF_TBL *) jpeg_alloc_huff_table JPP((j_common_ptr cinfo));
  68.937 +
  68.938 +/* Main entry points for compression */
  68.939 +EXTERN(void) jpeg_start_compress JPP((j_compress_ptr cinfo,
  68.940 +				      boolean write_all_tables));
  68.941 +EXTERN(JDIMENSION) jpeg_write_scanlines JPP((j_compress_ptr cinfo,
  68.942 +					     JSAMPARRAY scanlines,
  68.943 +					     JDIMENSION num_lines));
  68.944 +EXTERN(void) jpeg_finish_compress JPP((j_compress_ptr cinfo));
  68.945 +
  68.946 +/* Replaces jpeg_write_scanlines when writing raw downsampled data. */
  68.947 +EXTERN(JDIMENSION) jpeg_write_raw_data JPP((j_compress_ptr cinfo,
  68.948 +					    JSAMPIMAGE data,
  68.949 +					    JDIMENSION num_lines));
  68.950 +
  68.951 +/* Write a special marker.  See libjpeg.doc concerning safe usage. */
  68.952 +EXTERN(void) jpeg_write_marker
  68.953 +	JPP((j_compress_ptr cinfo, int marker,
  68.954 +	     const JOCTET * dataptr, unsigned int datalen));
  68.955 +/* Same, but piecemeal. */
  68.956 +EXTERN(void) jpeg_write_m_header
  68.957 +	JPP((j_compress_ptr cinfo, int marker, unsigned int datalen));
  68.958 +EXTERN(void) jpeg_write_m_byte
  68.959 +	JPP((j_compress_ptr cinfo, int val));
  68.960 +
  68.961 +/* Alternate compression function: just write an abbreviated table file */
  68.962 +EXTERN(void) jpeg_write_tables JPP((j_compress_ptr cinfo));
  68.963 +
  68.964 +/* Decompression startup: read start of JPEG datastream to see what's there */
  68.965 +EXTERN(int) jpeg_read_header JPP((j_decompress_ptr cinfo,
  68.966 +				  boolean require_image));
  68.967 +/* Return value is one of: */
  68.968 +#define JPEG_SUSPENDED		0 /* Suspended due to lack of input data */
  68.969 +#define JPEG_HEADER_OK		1 /* Found valid image datastream */
  68.970 +#define JPEG_HEADER_TABLES_ONLY	2 /* Found valid table-specs-only datastream */
  68.971 +/* If you pass require_image = TRUE (normal case), you need not check for
  68.972 + * a TABLES_ONLY return code; an abbreviated file will cause an error exit.
  68.973 + * JPEG_SUSPENDED is only possible if you use a data source module that can
  68.974 + * give a suspension return (the stdio source module doesn't).
  68.975 + */
  68.976 +
  68.977 +/* Main entry points for decompression */
  68.978 +EXTERN(boolean) jpeg_start_decompress JPP((j_decompress_ptr cinfo));
  68.979 +EXTERN(JDIMENSION) jpeg_read_scanlines JPP((j_decompress_ptr cinfo,
  68.980 +					    JSAMPARRAY scanlines,
  68.981 +					    JDIMENSION max_lines));
  68.982 +EXTERN(boolean) jpeg_finish_decompress JPP((j_decompress_ptr cinfo));
  68.983 +
  68.984 +/* Replaces jpeg_read_scanlines when reading raw downsampled data. */
  68.985 +EXTERN(JDIMENSION) jpeg_read_raw_data JPP((j_decompress_ptr cinfo,
  68.986 +					   JSAMPIMAGE data,
  68.987 +					   JDIMENSION max_lines));
  68.988 +
  68.989 +/* Additional entry points for buffered-image mode. */
  68.990 +EXTERN(boolean) jpeg_has_multiple_scans JPP((j_decompress_ptr cinfo));
  68.991 +EXTERN(boolean) jpeg_start_output JPP((j_decompress_ptr cinfo,
  68.992 +				       int scan_number));
  68.993 +EXTERN(boolean) jpeg_finish_output JPP((j_decompress_ptr cinfo));
  68.994 +EXTERN(boolean) jpeg_input_complete JPP((j_decompress_ptr cinfo));
  68.995 +EXTERN(void) jpeg_new_colormap JPP((j_decompress_ptr cinfo));
  68.996 +EXTERN(int) jpeg_consume_input JPP((j_decompress_ptr cinfo));
  68.997 +/* Return value is one of: */
  68.998 +/* #define JPEG_SUSPENDED	0    Suspended due to lack of input data */
  68.999 +#define JPEG_REACHED_SOS	1 /* Reached start of new scan */
 68.1000 +#define JPEG_REACHED_EOI	2 /* Reached end of image */
 68.1001 +#define JPEG_ROW_COMPLETED	3 /* Completed one iMCU row */
 68.1002 +#define JPEG_SCAN_COMPLETED	4 /* Completed last iMCU row of a scan */
 68.1003 +
 68.1004 +/* Precalculate output dimensions for current decompression parameters. */
 68.1005 +EXTERN(void) jpeg_calc_output_dimensions JPP((j_decompress_ptr cinfo));
 68.1006 +
 68.1007 +/* Control saving of COM and APPn markers into marker_list. */
 68.1008 +EXTERN(void) jpeg_save_markers
 68.1009 +	JPP((j_decompress_ptr cinfo, int marker_code,
 68.1010 +	     unsigned int length_limit));
 68.1011 +
 68.1012 +/* Install a special processing method for COM or APPn markers. */
 68.1013 +EXTERN(void) jpeg_set_marker_processor
 68.1014 +	JPP((j_decompress_ptr cinfo, int marker_code,
 68.1015 +	     jpeg_marker_parser_method routine));
 68.1016 +
 68.1017 +/* Read or write raw DCT coefficients --- useful for lossless transcoding. */
 68.1018 +EXTERN(jvirt_barray_ptr *) jpeg_read_coefficients JPP((j_decompress_ptr cinfo));
 68.1019 +EXTERN(void) jpeg_write_coefficients JPP((j_compress_ptr cinfo,
 68.1020 +					  jvirt_barray_ptr * coef_arrays));
 68.1021 +EXTERN(void) jpeg_copy_critical_parameters JPP((j_decompress_ptr srcinfo,
 68.1022 +						j_compress_ptr dstinfo));
 68.1023 +
 68.1024 +/* If you choose to abort compression or decompression before completing
 68.1025 + * jpeg_finish_(de)compress, then you need to clean up to release memory,
 68.1026 + * temporary files, etc.  You can just call jpeg_destroy_(de)compress
 68.1027 + * if you're done with the JPEG object, but if you want to clean it up and
 68.1028 + * reuse it, call this:
 68.1029 + */
 68.1030 +EXTERN(void) jpeg_abort_compress JPP((j_compress_ptr cinfo));
 68.1031 +EXTERN(void) jpeg_abort_decompress JPP((j_decompress_ptr cinfo));
 68.1032 +
 68.1033 +/* Generic versions of jpeg_abort and jpeg_destroy that work on either
 68.1034 + * flavor of JPEG object.  These may be more convenient in some places.
 68.1035 + */
 68.1036 +EXTERN(void) jpeg_abort JPP((j_common_ptr cinfo));
 68.1037 +EXTERN(void) jpeg_destroy JPP((j_common_ptr cinfo));
 68.1038 +
 68.1039 +/* Default restart-marker-resync procedure for use by data source modules */
 68.1040 +EXTERN(boolean) jpeg_resync_to_restart JPP((j_decompress_ptr cinfo,
 68.1041 +					    int desired));
 68.1042 +
 68.1043 +
 68.1044 +/* These marker codes are exported since applications and data source modules
 68.1045 + * are likely to want to use them.
 68.1046 + */
 68.1047 +
 68.1048 +#define JPEG_RST0	0xD0	/* RST0 marker code */
 68.1049 +#define JPEG_EOI	0xD9	/* EOI marker code */
 68.1050 +#define JPEG_APP0	0xE0	/* APP0 marker code */
 68.1051 +#define JPEG_COM	0xFE	/* COM marker code */
 68.1052 +
 68.1053 +
 68.1054 +/* If we have a brain-damaged compiler that emits warnings (or worse, errors)
 68.1055 + * for structure definitions that are never filled in, keep it quiet by
 68.1056 + * supplying dummy definitions for the various substructures.
 68.1057 + */
 68.1058 +
 68.1059 +#ifdef INCOMPLETE_TYPES_BROKEN
 68.1060 +#ifndef JPEG_INTERNALS		/* will be defined in jpegint.h */
 68.1061 +struct jvirt_sarray_control { long dummy; };
 68.1062 +struct jvirt_barray_control { long dummy; };
 68.1063 +struct jpeg_comp_master { long dummy; };
 68.1064 +struct jpeg_c_main_controller { long dummy; };
 68.1065 +struct jpeg_c_prep_controller { long dummy; };
 68.1066 +struct jpeg_c_coef_controller { long dummy; };
 68.1067 +struct jpeg_marker_writer { long dummy; };
 68.1068 +struct jpeg_color_converter { long dummy; };
 68.1069 +struct jpeg_downsampler { long dummy; };
 68.1070 +struct jpeg_forward_dct { long dummy; };
 68.1071 +struct jpeg_entropy_encoder { long dummy; };
 68.1072 +struct jpeg_decomp_master { long dummy; };
 68.1073 +struct jpeg_d_main_controller { long dummy; };
 68.1074 +struct jpeg_d_coef_controller { long dummy; };
 68.1075 +struct jpeg_d_post_controller { long dummy; };
 68.1076 +struct jpeg_input_controller { long dummy; };
 68.1077 +struct jpeg_marker_reader { long dummy; };
 68.1078 +struct jpeg_entropy_decoder { long dummy; };
 68.1079 +struct jpeg_inverse_dct { long dummy; };
 68.1080 +struct jpeg_upsampler { long dummy; };
 68.1081 +struct jpeg_color_deconverter { long dummy; };
 68.1082 +struct jpeg_color_quantizer { long dummy; };
 68.1083 +#endif /* JPEG_INTERNALS */
 68.1084 +#endif /* INCOMPLETE_TYPES_BROKEN */
 68.1085 +
 68.1086 +
 68.1087 +/*
 68.1088 + * The JPEG library modules define JPEG_INTERNALS before including this file.
 68.1089 + * The internal structure declarations are read only when that is true.
 68.1090 + * Applications using the library should not include jpegint.h, but may wish
 68.1091 + * to include jerror.h.
 68.1092 + */
 68.1093 +
 68.1094 +#ifdef JPEG_INTERNALS
 68.1095 +#include "jpegint.h"		/* fetch private declarations */
 68.1096 +#include "jerror.h"		/* fetch error codes too */
 68.1097 +#endif
 68.1098 +
 68.1099 +#endif /* JPEGLIB_H */
    69.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    69.2 +++ b/libs/libjpeg/jquant1.c	Sat Sep 19 05:51:51 2015 +0300
    69.3 @@ -0,0 +1,856 @@
    69.4 +/*
    69.5 + * jquant1.c
    69.6 + *
    69.7 + * Copyright (C) 1991-1996, Thomas G. Lane.
    69.8 + * This file is part of the Independent JPEG Group's software.
    69.9 + * For conditions of distribution and use, see the accompanying README file.
   69.10 + *
   69.11 + * This file contains 1-pass color quantization (color mapping) routines.
   69.12 + * These routines provide mapping to a fixed color map using equally spaced
   69.13 + * color values.  Optional Floyd-Steinberg or ordered dithering is available.
   69.14 + */
   69.15 +
   69.16 +#define JPEG_INTERNALS
   69.17 +#include "jinclude.h"
   69.18 +#include "jpeglib.h"
   69.19 +
   69.20 +#ifdef QUANT_1PASS_SUPPORTED
   69.21 +
   69.22 +
   69.23 +/*
   69.24 + * The main purpose of 1-pass quantization is to provide a fast, if not very
   69.25 + * high quality, colormapped output capability.  A 2-pass quantizer usually
   69.26 + * gives better visual quality; however, for quantized grayscale output this
   69.27 + * quantizer is perfectly adequate.  Dithering is highly recommended with this
   69.28 + * quantizer, though you can turn it off if you really want to.
   69.29 + *
   69.30 + * In 1-pass quantization the colormap must be chosen in advance of seeing the
   69.31 + * image.  We use a map consisting of all combinations of Ncolors[i] color
   69.32 + * values for the i'th component.  The Ncolors[] values are chosen so that
   69.33 + * their product, the total number of colors, is no more than that requested.
   69.34 + * (In most cases, the product will be somewhat less.)
   69.35 + *
   69.36 + * Since the colormap is orthogonal, the representative value for each color
   69.37 + * component can be determined without considering the other components;
   69.38 + * then these indexes can be combined into a colormap index by a standard
   69.39 + * N-dimensional-array-subscript calculation.  Most of the arithmetic involved
   69.40 + * can be precalculated and stored in the lookup table colorindex[].
   69.41 + * colorindex[i][j] maps pixel value j in component i to the nearest
   69.42 + * representative value (grid plane) for that component; this index is
   69.43 + * multiplied by the array stride for component i, so that the
   69.44 + * index of the colormap entry closest to a given pixel value is just
   69.45 + *    sum( colorindex[component-number][pixel-component-value] )
   69.46 + * Aside from being fast, this scheme allows for variable spacing between
   69.47 + * representative values with no additional lookup cost.
   69.48 + *
   69.49 + * If gamma correction has been applied in color conversion, it might be wise
   69.50 + * to adjust the color grid spacing so that the representative colors are
   69.51 + * equidistant in linear space.  At this writing, gamma correction is not
   69.52 + * implemented by jdcolor, so nothing is done here.
   69.53 + */
   69.54 +
   69.55 +
   69.56 +/* Declarations for ordered dithering.
   69.57 + *
   69.58 + * We use a standard 16x16 ordered dither array.  The basic concept of ordered
   69.59 + * dithering is described in many references, for instance Dale Schumacher's
   69.60 + * chapter II.2 of Graphics Gems II (James Arvo, ed. Academic Press, 1991).
   69.61 + * In place of Schumacher's comparisons against a "threshold" value, we add a
   69.62 + * "dither" value to the input pixel and then round the result to the nearest
   69.63 + * output value.  The dither value is equivalent to (0.5 - threshold) times
   69.64 + * the distance between output values.  For ordered dithering, we assume that
   69.65 + * the output colors are equally spaced; if not, results will probably be
   69.66 + * worse, since the dither may be too much or too little at a given point.
   69.67 + *
   69.68 + * The normal calculation would be to form pixel value + dither, range-limit
   69.69 + * this to 0..MAXJSAMPLE, and then index into the colorindex table as usual.
   69.70 + * We can skip the separate range-limiting step by extending the colorindex
   69.71 + * table in both directions.
   69.72 + */
   69.73 +
   69.74 +#define ODITHER_SIZE  16	/* dimension of dither matrix */
   69.75 +/* NB: if ODITHER_SIZE is not a power of 2, ODITHER_MASK uses will break */
   69.76 +#define ODITHER_CELLS (ODITHER_SIZE*ODITHER_SIZE)	/* # cells in matrix */
   69.77 +#define ODITHER_MASK  (ODITHER_SIZE-1) /* mask for wrapping around counters */
   69.78 +
   69.79 +typedef int ODITHER_MATRIX[ODITHER_SIZE][ODITHER_SIZE];
   69.80 +typedef int (*ODITHER_MATRIX_PTR)[ODITHER_SIZE];
   69.81 +
   69.82 +static const UINT8 base_dither_matrix[ODITHER_SIZE][ODITHER_SIZE] = {
   69.83 +  /* Bayer's order-4 dither array.  Generated by the code given in
   69.84 +   * Stephen Hawley's article "Ordered Dithering" in Graphics Gems I.
   69.85 +   * The values in this array must range from 0 to ODITHER_CELLS-1.
   69.86 +   */
   69.87 +  {   0,192, 48,240, 12,204, 60,252,  3,195, 51,243, 15,207, 63,255 },
   69.88 +  { 128, 64,176,112,140, 76,188,124,131, 67,179,115,143, 79,191,127 },
   69.89 +  {  32,224, 16,208, 44,236, 28,220, 35,227, 19,211, 47,239, 31,223 },
   69.90 +  { 160, 96,144, 80,172,108,156, 92,163, 99,147, 83,175,111,159, 95 },
   69.91 +  {   8,200, 56,248,  4,196, 52,244, 11,203, 59,251,  7,199, 55,247 },
   69.92 +  { 136, 72,184,120,132, 68,180,116,139, 75,187,123,135, 71,183,119 },
   69.93 +  {  40,232, 24,216, 36,228, 20,212, 43,235, 27,219, 39,231, 23,215 },
   69.94 +  { 168,104,152, 88,164,100,148, 84,171,107,155, 91,167,103,151, 87 },
   69.95 +  {   2,194, 50,242, 14,206, 62,254,  1,193, 49,241, 13,205, 61,253 },
   69.96 +  { 130, 66,178,114,142, 78,190,126,129, 65,177,113,141, 77,189,125 },
   69.97 +  {  34,226, 18,210, 46,238, 30,222, 33,225, 17,209, 45,237, 29,221 },
   69.98 +  { 162, 98,146, 82,174,110,158, 94,161, 97,145, 81,173,109,157, 93 },
   69.99 +  {  10,202, 58,250,  6,198, 54,246,  9,201, 57,249,  5,197, 53,245 },
  69.100 +  { 138, 74,186,122,134, 70,182,118,137, 73,185,121,133, 69,181,117 },
  69.101 +  {  42,234, 26,218, 38,230, 22,214, 41,233, 25,217, 37,229, 21,213 },
  69.102 +  { 170,106,154, 90,166,102,150, 86,169,105,153, 89,165,101,149, 85 }
  69.103 +};
  69.104 +
  69.105 +
  69.106 +/* Declarations for Floyd-Steinberg dithering.
  69.107 + *
  69.108 + * Errors are accumulated into the array fserrors[], at a resolution of
  69.109 + * 1/16th of a pixel count.  The error at a given pixel is propagated
  69.110 + * to its not-yet-processed neighbors using the standard F-S fractions,
  69.111 + *		...	(here)	7/16
  69.112 + *		3/16	5/16	1/16
  69.113 + * We work left-to-right on even rows, right-to-left on odd rows.
  69.114 + *
  69.115 + * We can get away with a single array (holding one row's worth of errors)
  69.116 + * by using it to store the current row's errors at pixel columns not yet
  69.117 + * processed, but the next row's errors at columns already processed.  We
  69.118 + * need only a few extra variables to hold the errors immediately around the
  69.119 + * current column.  (If we are lucky, those variables are in registers, but
  69.120 + * even if not, they're probably cheaper to access than array elements are.)
  69.121 + *
  69.122 + * The fserrors[] array is indexed [component#][position].
  69.123 + * We provide (#columns + 2) entries per component; the extra entry at each
  69.124 + * end saves us from special-casing the first and last pixels.
  69.125 + *
  69.126 + * Note: on a wide image, we might not have enough room in a PC's near data
  69.127 + * segment to hold the error array; so it is allocated with alloc_large.
  69.128 + */
  69.129 +
  69.130 +#if BITS_IN_JSAMPLE == 8
  69.131 +typedef INT16 FSERROR;		/* 16 bits should be enough */
  69.132 +typedef int LOCFSERROR;		/* use 'int' for calculation temps */
  69.133 +#else
  69.134 +typedef INT32 FSERROR;		/* may need more than 16 bits */
  69.135 +typedef INT32 LOCFSERROR;	/* be sure calculation temps are big enough */
  69.136 +#endif
  69.137 +
  69.138 +typedef FSERROR FAR *FSERRPTR;	/* pointer to error array (in FAR storage!) */
  69.139 +
  69.140 +
  69.141 +/* Private subobject */
  69.142 +
  69.143 +#define MAX_Q_COMPS 4		/* max components I can handle */
  69.144 +
  69.145 +typedef struct {
  69.146 +  struct jpeg_color_quantizer pub; /* public fields */
  69.147 +
  69.148 +  /* Initially allocated colormap is saved here */
  69.149 +  JSAMPARRAY sv_colormap;	/* The color map as a 2-D pixel array */
  69.150 +  int sv_actual;		/* number of entries in use */
  69.151 +
  69.152 +  JSAMPARRAY colorindex;	/* Precomputed mapping for speed */
  69.153 +  /* colorindex[i][j] = index of color closest to pixel value j in component i,
  69.154 +   * premultiplied as described above.  Since colormap indexes must fit into
  69.155 +   * JSAMPLEs, the entries of this array will too.
  69.156 +   */
  69.157 +  boolean is_padded;		/* is the colorindex padded for odither? */
  69.158 +
  69.159 +  int Ncolors[MAX_Q_COMPS];	/* # of values alloced to each component */
  69.160 +
  69.161 +  /* Variables for ordered dithering */
  69.162 +  int row_index;		/* cur row's vertical index in dither matrix */
  69.163 +  ODITHER_MATRIX_PTR odither[MAX_Q_COMPS]; /* one dither array per component */
  69.164 +
  69.165 +  /* Variables for Floyd-Steinberg dithering */
  69.166 +  FSERRPTR fserrors[MAX_Q_COMPS]; /* accumulated errors */
  69.167 +  boolean on_odd_row;		/* flag to remember which row we are on */
  69.168 +} my_cquantizer;
  69.169 +
  69.170 +typedef my_cquantizer * my_cquantize_ptr;
  69.171 +
  69.172 +
  69.173 +/*
  69.174 + * Policy-making subroutines for create_colormap and create_colorindex.
  69.175 + * These routines determine the colormap to be used.  The rest of the module
  69.176 + * only assumes that the colormap is orthogonal.
  69.177 + *
  69.178 + *  * select_ncolors decides how to divvy up the available colors
  69.179 + *    among the components.
  69.180 + *  * output_value defines the set of representative values for a component.
  69.181 + *  * largest_input_value defines the mapping from input values to
  69.182 + *    representative values for a component.
  69.183 + * Note that the latter two routines may impose different policies for
  69.184 + * different components, though this is not currently done.
  69.185 + */
  69.186 +
  69.187 +
  69.188 +LOCAL(int)
  69.189 +select_ncolors (j_decompress_ptr cinfo, int Ncolors[])
  69.190 +/* Determine allocation of desired colors to components, */
  69.191 +/* and fill in Ncolors[] array to indicate choice. */
  69.192 +/* Return value is total number of colors (product of Ncolors[] values). */
  69.193 +{
  69.194 +  int nc = cinfo->out_color_components; /* number of color components */
  69.195 +  int max_colors = cinfo->desired_number_of_colors;
  69.196 +  int total_colors, iroot, i, j;
  69.197 +  boolean changed;
  69.198 +  long temp;
  69.199 +  static const int RGB_order[3] = { RGB_GREEN, RGB_RED, RGB_BLUE };
  69.200 +
  69.201 +  /* We can allocate at least the nc'th root of max_colors per component. */
  69.202 +  /* Compute floor(nc'th root of max_colors). */
  69.203 +  iroot = 1;
  69.204 +  do {
  69.205 +    iroot++;
  69.206 +    temp = iroot;		/* set temp = iroot ** nc */
  69.207 +    for (i = 1; i < nc; i++)
  69.208 +      temp *= iroot;
  69.209 +  } while (temp <= (long) max_colors); /* repeat till iroot exceeds root */
  69.210 +  iroot--;			/* now iroot = floor(root) */
  69.211 +
  69.212 +  /* Must have at least 2 color values per component */
  69.213 +  if (iroot < 2)
  69.214 +    ERREXIT1(cinfo, JERR_QUANT_FEW_COLORS, (int) temp);
  69.215 +
  69.216 +  /* Initialize to iroot color values for each component */
  69.217 +  total_colors = 1;
  69.218 +  for (i = 0; i < nc; i++) {
  69.219 +    Ncolors[i] = iroot;
  69.220 +    total_colors *= iroot;
  69.221 +  }
  69.222 +  /* We may be able to increment the count for one or more components without
  69.223 +   * exceeding max_colors, though we know not all can be incremented.
  69.224 +   * Sometimes, the first component can be incremented more than once!
  69.225 +   * (Example: for 16 colors, we start at 2*2*2, go to 3*2*2, then 4*2*2.)
  69.226 +   * In RGB colorspace, try to increment G first, then R, then B.
  69.227 +   */
  69.228 +  do {
  69.229 +    changed = FALSE;
  69.230 +    for (i = 0; i < nc; i++) {
  69.231 +      j = (cinfo->out_color_space == JCS_RGB ? RGB_order[i] : i);
  69.232 +      /* calculate new total_colors if Ncolors[j] is incremented */
  69.233 +      temp = total_colors / Ncolors[j];
  69.234 +      temp *= Ncolors[j]+1;	/* done in long arith to avoid oflo */
  69.235 +      if (temp > (long) max_colors)
  69.236 +	break;			/* won't fit, done with this pass */
  69.237 +      Ncolors[j]++;		/* OK, apply the increment */
  69.238 +      total_colors = (int) temp;
  69.239 +      changed = TRUE;
  69.240 +    }
  69.241 +  } while (changed);
  69.242 +
  69.243 +  return total_colors;
  69.244 +}
  69.245 +
  69.246 +
  69.247 +LOCAL(int)
  69.248 +output_value (j_decompress_ptr cinfo, int ci, int j, int maxj)
  69.249 +/* Return j'th output value, where j will range from 0 to maxj */
  69.250 +/* The output values must fall in 0..MAXJSAMPLE in increasing order */
  69.251 +{
  69.252 +  /* We always provide values 0 and MAXJSAMPLE for each component;
  69.253 +   * any additional values are equally spaced between these limits.
  69.254 +   * (Forcing the upper and lower values to the limits ensures that
  69.255 +   * dithering can't produce a color outside the selected gamut.)
  69.256 +   */
  69.257 +  return (int) (((INT32) j * MAXJSAMPLE + maxj/2) / maxj);
  69.258 +}
  69.259 +
  69.260 +
  69.261 +LOCAL(int)
  69.262 +largest_input_value (j_decompress_ptr cinfo, int ci, int j, int maxj)
  69.263 +/* Return largest input value that should map to j'th output value */
  69.264 +/* Must have largest(j=0) >= 0, and largest(j=maxj) >= MAXJSAMPLE */
  69.265 +{
  69.266 +  /* Breakpoints are halfway between values returned by output_value */
  69.267 +  return (int) (((INT32) (2*j + 1) * MAXJSAMPLE + maxj) / (2*maxj));
  69.268 +}
  69.269 +
  69.270 +
  69.271 +/*
  69.272 + * Create the colormap.
  69.273 + */
  69.274 +
  69.275 +LOCAL(void)
  69.276 +create_colormap (j_decompress_ptr cinfo)
  69.277 +{
  69.278 +  my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
  69.279 +  JSAMPARRAY colormap;		/* Created colormap */
  69.280 +  int total_colors;		/* Number of distinct output colors */
  69.281 +  int i,j,k, nci, blksize, blkdist, ptr, val;
  69.282 +
  69.283 +  /* Select number of colors for each component */
  69.284 +  total_colors = select_ncolors(cinfo, cquantize->Ncolors);
  69.285 +
  69.286 +  /* Report selected color counts */
  69.287 +  if (cinfo->out_color_components == 3)
  69.288 +    TRACEMS4(cinfo, 1, JTRC_QUANT_3_NCOLORS,
  69.289 +	     total_colors, cquantize->Ncolors[0],
  69.290 +	     cquantize->Ncolors[1], cquantize->Ncolors[2]);
  69.291 +  else
  69.292 +    TRACEMS1(cinfo, 1, JTRC_QUANT_NCOLORS, total_colors);
  69.293 +
  69.294 +  /* Allocate and fill in the colormap. */
  69.295 +  /* The colors are ordered in the map in standard row-major order, */
  69.296 +  /* i.e. rightmost (highest-indexed) color changes most rapidly. */
  69.297 +
  69.298 +  colormap = (*cinfo->mem->alloc_sarray)
  69.299 +    ((j_common_ptr) cinfo, JPOOL_IMAGE,
  69.300 +     (JDIMENSION) total_colors, (JDIMENSION) cinfo->out_color_components);
  69.301 +
  69.302 +  /* blksize is number of adjacent repeated entries for a component */
  69.303 +  /* blkdist is distance between groups of identical entries for a component */
  69.304 +  blkdist = total_colors;
  69.305 +
  69.306 +  for (i = 0; i < cinfo->out_color_components; i++) {
  69.307 +    /* fill in colormap entries for i'th color component */
  69.308 +    nci = cquantize->Ncolors[i]; /* # of distinct values for this color */
  69.309 +    blksize = blkdist / nci;
  69.310 +    for (j = 0; j < nci; j++) {
  69.311 +      /* Compute j'th output value (out of nci) for component */
  69.312 +      val = output_value(cinfo, i, j, nci-1);
  69.313 +      /* Fill in all colormap entries that have this value of this component */
  69.314 +      for (ptr = j * blksize; ptr < total_colors; ptr += blkdist) {
  69.315 +	/* fill in blksize entries beginning at ptr */
  69.316 +	for (k = 0; k < blksize; k++)
  69.317 +	  colormap[i][ptr+k] = (JSAMPLE) val;
  69.318 +      }
  69.319 +    }
  69.320 +    blkdist = blksize;		/* blksize of this color is blkdist of next */
  69.321 +  }
  69.322 +
  69.323 +  /* Save the colormap in private storage,
  69.324 +   * where it will survive color quantization mode changes.
  69.325 +   */
  69.326 +  cquantize->sv_colormap = colormap;
  69.327 +  cquantize->sv_actual = total_colors;
  69.328 +}
  69.329 +
  69.330 +
  69.331 +/*
  69.332 + * Create the color index table.
  69.333 + */
  69.334 +
  69.335 +LOCAL(void)
  69.336 +create_colorindex (j_decompress_ptr cinfo)
  69.337 +{
  69.338 +  my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
  69.339 +  JSAMPROW indexptr;
  69.340 +  int i,j,k, nci, blksize, val, pad;
  69.341 +
  69.342 +  /* For ordered dither, we pad the color index tables by MAXJSAMPLE in
  69.343 +   * each direction (input index values can be -MAXJSAMPLE .. 2*MAXJSAMPLE).
  69.344 +   * This is not necessary in the other dithering modes.  However, we
  69.345 +   * flag whether it was done in case user changes dithering mode.
  69.346 +   */
  69.347 +  if (cinfo->dither_mode == JDITHER_ORDERED) {
  69.348 +    pad = MAXJSAMPLE*2;
  69.349 +    cquantize->is_padded = TRUE;
  69.350 +  } else {
  69.351 +    pad = 0;
  69.352 +    cquantize->is_padded = FALSE;
  69.353 +  }
  69.354 +
  69.355 +  cquantize->colorindex = (*cinfo->mem->alloc_sarray)
  69.356 +    ((j_common_ptr) cinfo, JPOOL_IMAGE,
  69.357 +     (JDIMENSION) (MAXJSAMPLE+1 + pad),
  69.358 +     (JDIMENSION) cinfo->out_color_components);
  69.359 +
  69.360 +  /* blksize is number of adjacent repeated entries for a component */
  69.361 +  blksize = cquantize->sv_actual;
  69.362 +
  69.363 +  for (i = 0; i < cinfo->out_color_components; i++) {
  69.364 +    /* fill in colorindex entries for i'th color component */
  69.365 +    nci = cquantize->Ncolors[i]; /* # of distinct values for this color */
  69.366 +    blksize = blksize / nci;
  69.367 +
  69.368 +    /* adjust colorindex pointers to provide padding at negative indexes. */
  69.369 +    if (pad)
  69.370 +      cquantize->colorindex[i] += MAXJSAMPLE;
  69.371 +
  69.372 +    /* in loop, val = index of current output value, */
  69.373 +    /* and k = largest j that maps to current val */
  69.374 +    indexptr = cquantize->colorindex[i];
  69.375 +    val = 0;
  69.376 +    k = largest_input_value(cinfo, i, 0, nci-1);
  69.377 +    for (j = 0; j <= MAXJSAMPLE; j++) {
  69.378 +      while (j > k)		/* advance val if past boundary */
  69.379 +	k = largest_input_value(cinfo, i, ++val, nci-1);
  69.380 +      /* premultiply so that no multiplication needed in main processing */
  69.381 +      indexptr[j] = (JSAMPLE) (val * blksize);
  69.382 +    }
  69.383 +    /* Pad at both ends if necessary */
  69.384 +    if (pad)
  69.385 +      for (j = 1; j <= MAXJSAMPLE; j++) {
  69.386 +	indexptr[-j] = indexptr[0];
  69.387 +	indexptr[MAXJSAMPLE+j] = indexptr[MAXJSAMPLE];
  69.388 +      }
  69.389 +  }
  69.390 +}
  69.391 +
  69.392 +
  69.393 +/*
  69.394 + * Create an ordered-dither array for a component having ncolors
  69.395 + * distinct output values.
  69.396 + */
  69.397 +
  69.398 +LOCAL(ODITHER_MATRIX_PTR)
  69.399 +make_odither_array (j_decompress_ptr cinfo, int ncolors)
  69.400 +{
  69.401 +  ODITHER_MATRIX_PTR odither;
  69.402 +  int j,k;
  69.403 +  INT32 num,den;
  69.404 +
  69.405 +  odither = (ODITHER_MATRIX_PTR)
  69.406 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  69.407 +				SIZEOF(ODITHER_MATRIX));
  69.408 +  /* The inter-value distance for this color is MAXJSAMPLE/(ncolors-1).
  69.409 +   * Hence the dither value for the matrix cell with fill order f
  69.410 +   * (f=0..N-1) should be (N-1-2*f)/(2*N) * MAXJSAMPLE/(ncolors-1).
  69.411 +   * On 16-bit-int machine, be careful to avoid overflow.
  69.412 +   */
  69.413 +  den = 2 * ODITHER_CELLS * ((INT32) (ncolors - 1));
  69.414 +  for (j = 0; j < ODITHER_SIZE; j++) {
  69.415 +    for (k = 0; k < ODITHER_SIZE; k++) {
  69.416 +      num = ((INT32) (ODITHER_CELLS-1 - 2*((int)base_dither_matrix[j][k])))
  69.417 +	    * MAXJSAMPLE;
  69.418 +      /* Ensure round towards zero despite C's lack of consistency
  69.419 +       * about rounding negative values in integer division...
  69.420 +       */
  69.421 +      odither[j][k] = (int) (num<0 ? -((-num)/den) : num/den);
  69.422 +    }
  69.423 +  }
  69.424 +  return odither;
  69.425 +}
  69.426 +
  69.427 +
  69.428 +/*
  69.429 + * Create the ordered-dither tables.
  69.430 + * Components having the same number of representative colors may 
  69.431 + * share a dither table.
  69.432 + */
  69.433 +
  69.434 +LOCAL(void)
  69.435 +create_odither_tables (j_decompress_ptr cinfo)
  69.436 +{
  69.437 +  my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
  69.438 +  ODITHER_MATRIX_PTR odither;
  69.439 +  int i, j, nci;
  69.440 +
  69.441 +  for (i = 0; i < cinfo->out_color_components; i++) {
  69.442 +    nci = cquantize->Ncolors[i]; /* # of distinct values for this color */
  69.443 +    odither = NULL;		/* search for matching prior component */
  69.444 +    for (j = 0; j < i; j++) {
  69.445 +      if (nci == cquantize->Ncolors[j]) {
  69.446 +	odither = cquantize->odither[j];
  69.447 +	break;
  69.448 +      }
  69.449 +    }
  69.450 +    if (odither == NULL)	/* need a new table? */
  69.451 +      odither = make_odither_array(cinfo, nci);
  69.452 +    cquantize->odither[i] = odither;
  69.453 +  }
  69.454 +}
  69.455 +
  69.456 +
  69.457 +/*
  69.458 + * Map some rows of pixels to the output colormapped representation.
  69.459 + */
  69.460 +
  69.461 +METHODDEF(void)
  69.462 +color_quantize (j_decompress_ptr cinfo, JSAMPARRAY input_buf,
  69.463 +		JSAMPARRAY output_buf, int num_rows)
  69.464 +/* General case, no dithering */
  69.465 +{
  69.466 +  my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
  69.467 +  JSAMPARRAY colorindex = cquantize->colorindex;
  69.468 +  register int pixcode, ci;
  69.469 +  register JSAMPROW ptrin, ptrout;
  69.470 +  int row;
  69.471 +  JDIMENSION col;
  69.472 +  JDIMENSION width = cinfo->output_width;
  69.473 +  register int nc = cinfo->out_color_components;
  69.474 +
  69.475 +  for (row = 0; row < num_rows; row++) {
  69.476 +    ptrin = input_buf[row];
  69.477 +    ptrout = output_buf[row];
  69.478 +    for (col = width; col > 0; col--) {
  69.479 +      pixcode = 0;
  69.480 +      for (ci = 0; ci < nc; ci++) {
  69.481 +	pixcode += GETJSAMPLE(colorindex[ci][GETJSAMPLE(*ptrin++)]);
  69.482 +      }
  69.483 +      *ptrout++ = (JSAMPLE) pixcode;
  69.484 +    }
  69.485 +  }
  69.486 +}
  69.487 +
  69.488 +
  69.489 +METHODDEF(void)
  69.490 +color_quantize3 (j_decompress_ptr cinfo, JSAMPARRAY input_buf,
  69.491 +		 JSAMPARRAY output_buf, int num_rows)
  69.492 +/* Fast path for out_color_components==3, no dithering */
  69.493 +{
  69.494 +  my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
  69.495 +  register int pixcode;
  69.496 +  register JSAMPROW ptrin, ptrout;
  69.497 +  JSAMPROW colorindex0 = cquantize->colorindex[0];
  69.498 +  JSAMPROW colorindex1 = cquantize->colorindex[1];
  69.499 +  JSAMPROW colorindex2 = cquantize->colorindex[2];
  69.500 +  int row;
  69.501 +  JDIMENSION col;
  69.502 +  JDIMENSION width = cinfo->output_width;
  69.503 +
  69.504 +  for (row = 0; row < num_rows; row++) {
  69.505 +    ptrin = input_buf[row];
  69.506 +    ptrout = output_buf[row];
  69.507 +    for (col = width; col > 0; col--) {
  69.508 +      pixcode  = GETJSAMPLE(colorindex0[GETJSAMPLE(*ptrin++)]);
  69.509 +      pixcode += GETJSAMPLE(colorindex1[GETJSAMPLE(*ptrin++)]);
  69.510 +      pixcode += GETJSAMPLE(colorindex2[GETJSAMPLE(*ptrin++)]);
  69.511 +      *ptrout++ = (JSAMPLE) pixcode;
  69.512 +    }
  69.513 +  }
  69.514 +}
  69.515 +
  69.516 +
  69.517 +METHODDEF(void)
  69.518 +quantize_ord_dither (j_decompress_ptr cinfo, JSAMPARRAY input_buf,
  69.519 +		     JSAMPARRAY output_buf, int num_rows)
  69.520 +/* General case, with ordered dithering */
  69.521 +{
  69.522 +  my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
  69.523 +  register JSAMPROW input_ptr;
  69.524 +  register JSAMPROW output_ptr;
  69.525 +  JSAMPROW colorindex_ci;
  69.526 +  int * dither;			/* points to active row of dither matrix */
  69.527 +  int row_index, col_index;	/* current indexes into dither matrix */
  69.528 +  int nc = cinfo->out_color_components;
  69.529 +  int ci;
  69.530 +  int row;
  69.531 +  JDIMENSION col;
  69.532 +  JDIMENSION width = cinfo->output_width;
  69.533 +
  69.534 +  for (row = 0; row < num_rows; row++) {
  69.535 +    /* Initialize output values to 0 so can process components separately */
  69.536 +    jzero_far((void FAR *) output_buf[row],
  69.537 +	      (size_t) (width * SIZEOF(JSAMPLE)));
  69.538 +    row_index = cquantize->row_index;
  69.539 +    for (ci = 0; ci < nc; ci++) {
  69.540 +      input_ptr = input_buf[row] + ci;
  69.541 +      output_ptr = output_buf[row];
  69.542 +      colorindex_ci = cquantize->colorindex[ci];
  69.543 +      dither = cquantize->odither[ci][row_index];
  69.544 +      col_index = 0;
  69.545 +
  69.546 +      for (col = width; col > 0; col--) {
  69.547 +	/* Form pixel value + dither, range-limit to 0..MAXJSAMPLE,
  69.548 +	 * select output value, accumulate into output code for this pixel.
  69.549 +	 * Range-limiting need not be done explicitly, as we have extended
  69.550 +	 * the colorindex table to produce the right answers for out-of-range
  69.551 +	 * inputs.  The maximum dither is +- MAXJSAMPLE; this sets the
  69.552 +	 * required amount of padding.
  69.553 +	 */
  69.554 +	*output_ptr += colorindex_ci[GETJSAMPLE(*input_ptr)+dither[col_index]];
  69.555 +	input_ptr += nc;
  69.556 +	output_ptr++;
  69.557 +	col_index = (col_index + 1) & ODITHER_MASK;
  69.558 +      }
  69.559 +    }
  69.560 +    /* Advance row index for next row */
  69.561 +    row_index = (row_index + 1) & ODITHER_MASK;
  69.562 +    cquantize->row_index = row_index;
  69.563 +  }
  69.564 +}
  69.565 +
  69.566 +
  69.567 +METHODDEF(void)
  69.568 +quantize3_ord_dither (j_decompress_ptr cinfo, JSAMPARRAY input_buf,
  69.569 +		      JSAMPARRAY output_buf, int num_rows)
  69.570 +/* Fast path for out_color_components==3, with ordered dithering */
  69.571 +{
  69.572 +  my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
  69.573 +  register int pixcode;
  69.574 +  register JSAMPROW input_ptr;
  69.575 +  register JSAMPROW output_ptr;
  69.576 +  JSAMPROW colorindex0 = cquantize->colorindex[0];
  69.577 +  JSAMPROW colorindex1 = cquantize->colorindex[1];
  69.578 +  JSAMPROW colorindex2 = cquantize->colorindex[2];
  69.579 +  int * dither0;		/* points to active row of dither matrix */
  69.580 +  int * dither1;
  69.581 +  int * dither2;
  69.582 +  int row_index, col_index;	/* current indexes into dither matrix */
  69.583 +  int row;
  69.584 +  JDIMENSION col;
  69.585 +  JDIMENSION width = cinfo->output_width;
  69.586 +
  69.587 +  for (row = 0; row < num_rows; row++) {
  69.588 +    row_index = cquantize->row_index;
  69.589 +    input_ptr = input_buf[row];
  69.590 +    output_ptr = output_buf[row];
  69.591 +    dither0 = cquantize->odither[0][row_index];
  69.592 +    dither1 = cquantize->odither[1][row_index];
  69.593 +    dither2 = cquantize->odither[2][row_index];
  69.594 +    col_index = 0;
  69.595 +
  69.596 +    for (col = width; col > 0; col--) {
  69.597 +      pixcode  = GETJSAMPLE(colorindex0[GETJSAMPLE(*input_ptr++) +
  69.598 +					dither0[col_index]]);
  69.599 +      pixcode += GETJSAMPLE(colorindex1[GETJSAMPLE(*input_ptr++) +
  69.600 +					dither1[col_index]]);
  69.601 +      pixcode += GETJSAMPLE(colorindex2[GETJSAMPLE(*input_ptr++) +
  69.602 +					dither2[col_index]]);
  69.603 +      *output_ptr++ = (JSAMPLE) pixcode;
  69.604 +      col_index = (col_index + 1) & ODITHER_MASK;
  69.605 +    }
  69.606 +    row_index = (row_index + 1) & ODITHER_MASK;
  69.607 +    cquantize->row_index = row_index;
  69.608 +  }
  69.609 +}
  69.610 +
  69.611 +
  69.612 +METHODDEF(void)
  69.613 +quantize_fs_dither (j_decompress_ptr cinfo, JSAMPARRAY input_buf,
  69.614 +		    JSAMPARRAY output_buf, int num_rows)
  69.615 +/* General case, with Floyd-Steinberg dithering */
  69.616 +{
  69.617 +  my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
  69.618 +  register LOCFSERROR cur;	/* current error or pixel value */
  69.619 +  LOCFSERROR belowerr;		/* error for pixel below cur */
  69.620 +  LOCFSERROR bpreverr;		/* error for below/prev col */
  69.621 +  LOCFSERROR bnexterr;		/* error for below/next col */
  69.622 +  LOCFSERROR delta;
  69.623 +  register FSERRPTR errorptr;	/* => fserrors[] at column before current */
  69.624 +  register JSAMPROW input_ptr;
  69.625 +  register JSAMPROW output_ptr;
  69.626 +  JSAMPROW colorindex_ci;
  69.627 +  JSAMPROW colormap_ci;
  69.628 +  int pixcode;
  69.629 +  int nc = cinfo->out_color_components;
  69.630 +  int dir;			/* 1 for left-to-right, -1 for right-to-left */
  69.631 +  int dirnc;			/* dir * nc */
  69.632 +  int ci;
  69.633 +  int row;
  69.634 +  JDIMENSION col;
  69.635 +  JDIMENSION width = cinfo->output_width;
  69.636 +  JSAMPLE *range_limit = cinfo->sample_range_limit;
  69.637 +  SHIFT_TEMPS
  69.638 +
  69.639 +  for (row = 0; row < num_rows; row++) {
  69.640 +    /* Initialize output values to 0 so can process components separately */
  69.641 +    jzero_far((void FAR *) output_buf[row],
  69.642 +	      (size_t) (width * SIZEOF(JSAMPLE)));
  69.643 +    for (ci = 0; ci < nc; ci++) {
  69.644 +      input_ptr = input_buf[row] + ci;
  69.645 +      output_ptr = output_buf[row];
  69.646 +      if (cquantize->on_odd_row) {
  69.647 +	/* work right to left in this row */
  69.648 +	input_ptr += (width-1) * nc; /* so point to rightmost pixel */
  69.649 +	output_ptr += width-1;
  69.650 +	dir = -1;
  69.651 +	dirnc = -nc;
  69.652 +	errorptr = cquantize->fserrors[ci] + (width+1); /* => entry after last column */
  69.653 +      } else {
  69.654 +	/* work left to right in this row */
  69.655 +	dir = 1;
  69.656 +	dirnc = nc;
  69.657 +	errorptr = cquantize->fserrors[ci]; /* => entry before first column */
  69.658 +      }
  69.659 +      colorindex_ci = cquantize->colorindex[ci];
  69.660 +      colormap_ci = cquantize->sv_colormap[ci];
  69.661 +      /* Preset error values: no error propagated to first pixel from left */
  69.662 +      cur = 0;
  69.663 +      /* and no error propagated to row below yet */
  69.664 +      belowerr = bpreverr = 0;
  69.665 +
  69.666 +      for (col = width; col > 0; col--) {
  69.667 +	/* cur holds the error propagated from the previous pixel on the
  69.668 +	 * current line.  Add the error propagated from the previous line
  69.669 +	 * to form the complete error correction term for this pixel, and
  69.670 +	 * round the error term (which is expressed * 16) to an integer.
  69.671 +	 * RIGHT_SHIFT rounds towards minus infinity, so adding 8 is correct
  69.672 +	 * for either sign of the error value.
  69.673 +	 * Note: errorptr points to *previous* column's array entry.
  69.674 +	 */
  69.675 +	cur = RIGHT_SHIFT(cur + errorptr[dir] + 8, 4);
  69.676 +	/* Form pixel value + error, and range-limit to 0..MAXJSAMPLE.
  69.677 +	 * The maximum error is +- MAXJSAMPLE; this sets the required size
  69.678 +	 * of the range_limit array.
  69.679 +	 */
  69.680 +	cur += GETJSAMPLE(*input_ptr);
  69.681 +	cur = GETJSAMPLE(range_limit[cur]);
  69.682 +	/* Select output value, accumulate into output code for this pixel */
  69.683 +	pixcode = GETJSAMPLE(colorindex_ci[cur]);
  69.684 +	*output_ptr += (JSAMPLE) pixcode;
  69.685 +	/* Compute actual representation error at this pixel */
  69.686 +	/* Note: we can do this even though we don't have the final */
  69.687 +	/* pixel code, because the colormap is orthogonal. */
  69.688 +	cur -= GETJSAMPLE(colormap_ci[pixcode]);
  69.689 +	/* Compute error fractions to be propagated to adjacent pixels.
  69.690 +	 * Add these into the running sums, and simultaneously shift the
  69.691 +	 * next-line error sums left by 1 column.
  69.692 +	 */
  69.693 +	bnexterr = cur;
  69.694 +	delta = cur * 2;
  69.695 +	cur += delta;		/* form error * 3 */
  69.696 +	errorptr[0] = (FSERROR) (bpreverr + cur);
  69.697 +	cur += delta;		/* form error * 5 */
  69.698 +	bpreverr = belowerr + cur;
  69.699 +	belowerr = bnexterr;
  69.700 +	cur += delta;		/* form error * 7 */
  69.701 +	/* At this point cur contains the 7/16 error value to be propagated
  69.702 +	 * to the next pixel on the current line, and all the errors for the
  69.703 +	 * next line have been shifted over. We are therefore ready to move on.
  69.704 +	 */
  69.705 +	input_ptr += dirnc;	/* advance input ptr to next column */
  69.706 +	output_ptr += dir;	/* advance output ptr to next column */
  69.707 +	errorptr += dir;	/* advance errorptr to current column */
  69.708 +      }
  69.709 +      /* Post-loop cleanup: we must unload the final error value into the
  69.710 +       * final fserrors[] entry.  Note we need not unload belowerr because
  69.711 +       * it is for the dummy column before or after the actual array.
  69.712 +       */
  69.713 +      errorptr[0] = (FSERROR) bpreverr; /* unload prev err into array */
  69.714 +    }
  69.715 +    cquantize->on_odd_row = (cquantize->on_odd_row ? FALSE : TRUE);
  69.716 +  }
  69.717 +}
  69.718 +
  69.719 +
  69.720 +/*
  69.721 + * Allocate workspace for Floyd-Steinberg errors.
  69.722 + */
  69.723 +
  69.724 +LOCAL(void)
  69.725 +alloc_fs_workspace (j_decompress_ptr cinfo)
  69.726 +{
  69.727 +  my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
  69.728 +  size_t arraysize;
  69.729 +  int i;
  69.730 +
  69.731 +  arraysize = (size_t) ((cinfo->output_width + 2) * SIZEOF(FSERROR));
  69.732 +  for (i = 0; i < cinfo->out_color_components; i++) {
  69.733 +    cquantize->fserrors[i] = (FSERRPTR)
  69.734 +      (*cinfo->mem->alloc_large)((j_common_ptr) cinfo, JPOOL_IMAGE, arraysize);
  69.735 +  }
  69.736 +}
  69.737 +
  69.738 +
  69.739 +/*
  69.740 + * Initialize for one-pass color quantization.
  69.741 + */
  69.742 +
  69.743 +METHODDEF(void)
  69.744 +start_pass_1_quant (j_decompress_ptr cinfo, boolean is_pre_scan)
  69.745 +{
  69.746 +  my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
  69.747 +  size_t arraysize;
  69.748 +  int i;
  69.749 +
  69.750 +  /* Install my colormap. */
  69.751 +  cinfo->colormap = cquantize->sv_colormap;
  69.752 +  cinfo->actual_number_of_colors = cquantize->sv_actual;
  69.753 +
  69.754 +  /* Initialize for desired dithering mode. */
  69.755 +  switch (cinfo->dither_mode) {
  69.756 +  case JDITHER_NONE:
  69.757 +    if (cinfo->out_color_components == 3)
  69.758 +      cquantize->pub.color_quantize = color_quantize3;
  69.759 +    else
  69.760 +      cquantize->pub.color_quantize = color_quantize;
  69.761 +    break;
  69.762 +  case JDITHER_ORDERED:
  69.763 +    if (cinfo->out_color_components == 3)
  69.764 +      cquantize->pub.color_quantize = quantize3_ord_dither;
  69.765 +    else
  69.766 +      cquantize->pub.color_quantize = quantize_ord_dither;
  69.767 +    cquantize->row_index = 0;	/* initialize state for ordered dither */
  69.768 +    /* If user changed to ordered dither from another mode,
  69.769 +     * we must recreate the color index table with padding.
  69.770 +     * This will cost extra space, but probably isn't very likely.
  69.771 +     */
  69.772 +    if (! cquantize->is_padded)
  69.773 +      create_colorindex(cinfo);
  69.774 +    /* Create ordered-dither tables if we didn't already. */
  69.775 +    if (cquantize->odither[0] == NULL)
  69.776 +      create_odither_tables(cinfo);
  69.777 +    break;
  69.778 +  case JDITHER_FS:
  69.779 +    cquantize->pub.color_quantize = quantize_fs_dither;
  69.780 +    cquantize->on_odd_row = FALSE; /* initialize state for F-S dither */
  69.781 +    /* Allocate Floyd-Steinberg workspace if didn't already. */
  69.782 +    if (cquantize->fserrors[0] == NULL)
  69.783 +      alloc_fs_workspace(cinfo);
  69.784 +    /* Initialize the propagated errors to zero. */
  69.785 +    arraysize = (size_t) ((cinfo->output_width + 2) * SIZEOF(FSERROR));
  69.786 +    for (i = 0; i < cinfo->out_color_components; i++)
  69.787 +      jzero_far((void FAR *) cquantize->fserrors[i], arraysize);
  69.788 +    break;
  69.789 +  default:
  69.790 +    ERREXIT(cinfo, JERR_NOT_COMPILED);
  69.791 +    break;
  69.792 +  }
  69.793 +}
  69.794 +
  69.795 +
  69.796 +/*
  69.797 + * Finish up at the end of the pass.
  69.798 + */
  69.799 +
  69.800 +METHODDEF(void)
  69.801 +finish_pass_1_quant (j_decompress_ptr cinfo)
  69.802 +{
  69.803 +  /* no work in 1-pass case */
  69.804 +}
  69.805 +
  69.806 +
  69.807 +/*
  69.808 + * Switch to a new external colormap between output passes.
  69.809 + * Shouldn't get to this module!
  69.810 + */
  69.811 +
  69.812 +METHODDEF(void)
  69.813 +new_color_map_1_quant (j_decompress_ptr cinfo)
  69.814 +{
  69.815 +  ERREXIT(cinfo, JERR_MODE_CHANGE);
  69.816 +}
  69.817 +
  69.818 +
  69.819 +/*
  69.820 + * Module initialization routine for 1-pass color quantization.
  69.821 + */
  69.822 +
  69.823 +GLOBAL(void)
  69.824 +jinit_1pass_quantizer (j_decompress_ptr cinfo)
  69.825 +{
  69.826 +  my_cquantize_ptr cquantize;
  69.827 +
  69.828 +  cquantize = (my_cquantize_ptr)
  69.829 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  69.830 +				SIZEOF(my_cquantizer));
  69.831 +  cinfo->cquantize = (struct jpeg_color_quantizer *) cquantize;
  69.832 +  cquantize->pub.start_pass = start_pass_1_quant;
  69.833 +  cquantize->pub.finish_pass = finish_pass_1_quant;
  69.834 +  cquantize->pub.new_color_map = new_color_map_1_quant;
  69.835 +  cquantize->fserrors[0] = NULL; /* Flag FS workspace not allocated */
  69.836 +  cquantize->odither[0] = NULL;	/* Also flag odither arrays not allocated */
  69.837 +
  69.838 +  /* Make sure my internal arrays won't overflow */
  69.839 +  if (cinfo->out_color_components > MAX_Q_COMPS)
  69.840 +    ERREXIT1(cinfo, JERR_QUANT_COMPONENTS, MAX_Q_COMPS);
  69.841 +  /* Make sure colormap indexes can be represented by JSAMPLEs */
  69.842 +  if (cinfo->desired_number_of_colors > (MAXJSAMPLE+1))
  69.843 +    ERREXIT1(cinfo, JERR_QUANT_MANY_COLORS, MAXJSAMPLE+1);
  69.844 +
  69.845 +  /* Create the colormap and color index table. */
  69.846 +  create_colormap(cinfo);
  69.847 +  create_colorindex(cinfo);
  69.848 +
  69.849 +  /* Allocate Floyd-Steinberg workspace now if requested.
  69.850 +   * We do this now since it is FAR storage and may affect the memory
  69.851 +   * manager's space calculations.  If the user changes to FS dither
  69.852 +   * mode in a later pass, we will allocate the space then, and will
  69.853 +   * possibly overrun the max_memory_to_use setting.
  69.854 +   */
  69.855 +  if (cinfo->dither_mode == JDITHER_FS)
  69.856 +    alloc_fs_workspace(cinfo);
  69.857 +}
  69.858 +
  69.859 +#endif /* QUANT_1PASS_SUPPORTED */
    70.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    70.2 +++ b/libs/libjpeg/jquant2.c	Sat Sep 19 05:51:51 2015 +0300
    70.3 @@ -0,0 +1,1310 @@
    70.4 +/*
    70.5 + * jquant2.c
    70.6 + *
    70.7 + * Copyright (C) 1991-1996, Thomas G. Lane.
    70.8 + * This file is part of the Independent JPEG Group's software.
    70.9 + * For conditions of distribution and use, see the accompanying README file.
   70.10 + *
   70.11 + * This file contains 2-pass color quantization (color mapping) routines.
   70.12 + * These routines provide selection of a custom color map for an image,
   70.13 + * followed by mapping of the image to that color map, with optional
   70.14 + * Floyd-Steinberg dithering.
   70.15 + * It is also possible to use just the second pass to map to an arbitrary
   70.16 + * externally-given color map.
   70.17 + *
   70.18 + * Note: ordered dithering is not supported, since there isn't any fast
   70.19 + * way to compute intercolor distances; it's unclear that ordered dither's
   70.20 + * fundamental assumptions even hold with an irregularly spaced color map.
   70.21 + */
   70.22 +
   70.23 +#define JPEG_INTERNALS
   70.24 +#include "jinclude.h"
   70.25 +#include "jpeglib.h"
   70.26 +
   70.27 +#ifdef QUANT_2PASS_SUPPORTED
   70.28 +
   70.29 +
   70.30 +/*
   70.31 + * This module implements the well-known Heckbert paradigm for color
   70.32 + * quantization.  Most of the ideas used here can be traced back to
   70.33 + * Heckbert's seminal paper
   70.34 + *   Heckbert, Paul.  "Color Image Quantization for Frame Buffer Display",
   70.35 + *   Proc. SIGGRAPH '82, Computer Graphics v.16 #3 (July 1982), pp 297-304.
   70.36 + *
   70.37 + * In the first pass over the image, we accumulate a histogram showing the
   70.38 + * usage count of each possible color.  To keep the histogram to a reasonable
   70.39 + * size, we reduce the precision of the input; typical practice is to retain
   70.40 + * 5 or 6 bits per color, so that 8 or 4 different input values are counted
   70.41 + * in the same histogram cell.
   70.42 + *
   70.43 + * Next, the color-selection step begins with a boxx representing the whole
   70.44 + * color space, and repeatedly splits the "largest" remaining boxx until we
   70.45 + * have as many boxxes as desired colors.  Then the mean color in each
   70.46 + * remaining boxx becomes one of the possible output colors.
   70.47 + * 
   70.48 + * The second pass over the image maps each input pixel to the closest output
   70.49 + * color (optionally after applying a Floyd-Steinberg dithering correction).
   70.50 + * This mapping is logically trivial, but making it go fast enough requires
   70.51 + * considerable care.
   70.52 + *
   70.53 + * Heckbert-style quantizers vary a good deal in their policies for choosing
   70.54 + * the "largest" boxx and deciding where to cut it.  The particular policies
   70.55 + * used here have proved out well in experimental comparisons, but better ones
   70.56 + * may yet be found.
   70.57 + *
   70.58 + * In earlier versions of the IJG code, this module quantized in YCbCr color
   70.59 + * space, processing the raw upsampled data without a color conversion step.
   70.60 + * This allowed the color conversion math to be done only once per colormap
   70.61 + * entry, not once per pixel.  However, that optimization precluded other
   70.62 + * useful optimizations (such as merging color conversion with upsampling)
   70.63 + * and it also interfered with desired capabilities such as quantizing to an
   70.64 + * externally-supplied colormap.  We have therefore abandoned that approach.
   70.65 + * The present code works in the post-conversion color space, typically RGB.
   70.66 + *
   70.67 + * To improve the visual quality of the results, we actually work in scaled
   70.68 + * RGB space, giving G distances more weight than R, and R in turn more than
   70.69 + * B.  To do everything in integer math, we must use integer scale factors.
   70.70 + * The 2/3/1 scale factors used here correspond loosely to the relative
   70.71 + * weights of the colors in the NTSC grayscale equation.
   70.72 + * If you want to use this code to quantize a non-RGB color space, you'll
   70.73 + * probably need to change these scale factors.
   70.74 + */
   70.75 +
   70.76 +#define R_SCALE 2		/* scale R distances by this much */
   70.77 +#define G_SCALE 3		/* scale G distances by this much */
   70.78 +#define B_SCALE 1		/* and B by this much */
   70.79 +
   70.80 +/* Relabel R/G/B as components 0/1/2, respecting the RGB ordering defined
   70.81 + * in jmorecfg.h.  As the code stands, it will do the right thing for R,G,B
   70.82 + * and B,G,R orders.  If you define some other weird order in jmorecfg.h,
   70.83 + * you'll get compile errors until you extend this logic.  In that case
   70.84 + * you'll probably want to tweak the histogram sizes too.
   70.85 + */
   70.86 +
   70.87 +#if RGB_RED == 0
   70.88 +#define C0_SCALE R_SCALE
   70.89 +#endif
   70.90 +#if RGB_BLUE == 0
   70.91 +#define C0_SCALE B_SCALE
   70.92 +#endif
   70.93 +#if RGB_GREEN == 1
   70.94 +#define C1_SCALE G_SCALE
   70.95 +#endif
   70.96 +#if RGB_RED == 2
   70.97 +#define C2_SCALE R_SCALE
   70.98 +#endif
   70.99 +#if RGB_BLUE == 2
  70.100 +#define C2_SCALE B_SCALE
  70.101 +#endif
  70.102 +
  70.103 +
  70.104 +/*
  70.105 + * First we have the histogram data structure and routines for creating it.
  70.106 + *
  70.107 + * The number of bits of precision can be adjusted by changing these symbols.
  70.108 + * We recommend keeping 6 bits for G and 5 each for R and B.
  70.109 + * If you have plenty of memory and cycles, 6 bits all around gives marginally
  70.110 + * better results; if you are short of memory, 5 bits all around will save
  70.111 + * some space but degrade the results.
  70.112 + * To maintain a fully accurate histogram, we'd need to allocate a "long"
  70.113 + * (preferably unsigned long) for each cell.  In practice this is overkill;
  70.114 + * we can get by with 16 bits per cell.  Few of the cell counts will overflow,
  70.115 + * and clamping those that do overflow to the maximum value will give close-
  70.116 + * enough results.  This reduces the recommended histogram size from 256Kb
  70.117 + * to 128Kb, which is a useful savings on PC-class machines.
  70.118 + * (In the second pass the histogram space is re-used for pixel mapping data;
  70.119 + * in that capacity, each cell must be able to store zero to the number of
  70.120 + * desired colors.  16 bits/cell is plenty for that too.)
  70.121 + * Since the JPEG code is intended to run in small memory model on 80x86
  70.122 + * machines, we can't just allocate the histogram in one chunk.  Instead
  70.123 + * of a true 3-D array, we use a row of pointers to 2-D arrays.  Each
  70.124 + * pointer corresponds to a C0 value (typically 2^5 = 32 pointers) and
  70.125 + * each 2-D array has 2^6*2^5 = 2048 or 2^6*2^6 = 4096 entries.  Note that
  70.126 + * on 80x86 machines, the pointer row is in near memory but the actual
  70.127 + * arrays are in far memory (same arrangement as we use for image arrays).
  70.128 + */
  70.129 +
  70.130 +#define MAXNUMCOLORS  (MAXJSAMPLE+1) /* maximum size of colormap */
  70.131 +
  70.132 +/* These will do the right thing for either R,G,B or B,G,R color order,
  70.133 + * but you may not like the results for other color orders.
  70.134 + */
  70.135 +#define HIST_C0_BITS  5		/* bits of precision in R/B histogram */
  70.136 +#define HIST_C1_BITS  6		/* bits of precision in G histogram */
  70.137 +#define HIST_C2_BITS  5		/* bits of precision in B/R histogram */
  70.138 +
  70.139 +/* Number of elements along histogram axes. */
  70.140 +#define HIST_C0_ELEMS  (1<<HIST_C0_BITS)
  70.141 +#define HIST_C1_ELEMS  (1<<HIST_C1_BITS)
  70.142 +#define HIST_C2_ELEMS  (1<<HIST_C2_BITS)
  70.143 +
  70.144 +/* These are the amounts to shift an input value to get a histogram index. */
  70.145 +#define C0_SHIFT  (BITS_IN_JSAMPLE-HIST_C0_BITS)
  70.146 +#define C1_SHIFT  (BITS_IN_JSAMPLE-HIST_C1_BITS)
  70.147 +#define C2_SHIFT  (BITS_IN_JSAMPLE-HIST_C2_BITS)
  70.148 +
  70.149 +
  70.150 +typedef UINT16 histcell;	/* histogram cell; prefer an unsigned type */
  70.151 +
  70.152 +typedef histcell FAR * histptr;	/* for pointers to histogram cells */
  70.153 +
  70.154 +typedef histcell hist1d[HIST_C2_ELEMS]; /* typedefs for the array */
  70.155 +typedef hist1d FAR * hist2d;	/* type for the 2nd-level pointers */
  70.156 +typedef hist2d * hist3d;	/* type for top-level pointer */
  70.157 +
  70.158 +
  70.159 +/* Declarations for Floyd-Steinberg dithering.
  70.160 + *
  70.161 + * Errors are accumulated into the array fserrors[], at a resolution of
  70.162 + * 1/16th of a pixel count.  The error at a given pixel is propagated
  70.163 + * to its not-yet-processed neighbors using the standard F-S fractions,
  70.164 + *		...	(here)	7/16
  70.165 + *		3/16	5/16	1/16
  70.166 + * We work left-to-right on even rows, right-to-left on odd rows.
  70.167 + *
  70.168 + * We can get away with a single array (holding one row's worth of errors)
  70.169 + * by using it to store the current row's errors at pixel columns not yet
  70.170 + * processed, but the next row's errors at columns already processed.  We
  70.171 + * need only a few extra variables to hold the errors immediately around the
  70.172 + * current column.  (If we are lucky, those variables are in registers, but
  70.173 + * even if not, they're probably cheaper to access than array elements are.)
  70.174 + *
  70.175 + * The fserrors[] array has (#columns + 2) entries; the extra entry at
  70.176 + * each end saves us from special-casing the first and last pixels.
  70.177 + * Each entry is three values long, one value for each color component.
  70.178 + *
  70.179 + * Note: on a wide image, we might not have enough room in a PC's near data
  70.180 + * segment to hold the error array; so it is allocated with alloc_large.
  70.181 + */
  70.182 +
  70.183 +#if BITS_IN_JSAMPLE == 8
  70.184 +typedef INT16 FSERROR;		/* 16 bits should be enough */
  70.185 +typedef int LOCFSERROR;		/* use 'int' for calculation temps */
  70.186 +#else
  70.187 +typedef INT32 FSERROR;		/* may need more than 16 bits */
  70.188 +typedef INT32 LOCFSERROR;	/* be sure calculation temps are big enough */
  70.189 +#endif
  70.190 +
  70.191 +typedef FSERROR FAR *FSERRPTR;	/* pointer to error array (in FAR storage!) */
  70.192 +
  70.193 +
  70.194 +/* Private subobject */
  70.195 +
  70.196 +typedef struct {
  70.197 +  struct jpeg_color_quantizer pub; /* public fields */
  70.198 +
  70.199 +  /* Space for the eventually created colormap is stashed here */
  70.200 +  JSAMPARRAY sv_colormap;	/* colormap allocated at init time */
  70.201 +  int desired;			/* desired # of colors = size of colormap */
  70.202 +
  70.203 +  /* Variables for accumulating image statistics */
  70.204 +  hist3d histogram;		/* pointer to the histogram */
  70.205 +
  70.206 +  boolean needs_zeroed;		/* TRUE if next pass must zero histogram */
  70.207 +
  70.208 +  /* Variables for Floyd-Steinberg dithering */
  70.209 +  FSERRPTR fserrors;		/* accumulated errors */
  70.210 +  boolean on_odd_row;		/* flag to remember which row we are on */
  70.211 +  int * error_limiter;		/* table for clamping the applied error */
  70.212 +} my_cquantizer;
  70.213 +
  70.214 +typedef my_cquantizer * my_cquantize_ptr;
  70.215 +
  70.216 +
  70.217 +/*
  70.218 + * Prescan some rows of pixels.
  70.219 + * In this module the prescan simply updates the histogram, which has been
  70.220 + * initialized to zeroes by start_pass.
  70.221 + * An output_buf parameter is required by the method signature, but no data
  70.222 + * is actually output (in fact the buffer controller is probably passing a
  70.223 + * NULL pointer).
  70.224 + */
  70.225 +
  70.226 +METHODDEF(void)
  70.227 +prescan_quantize (j_decompress_ptr cinfo, JSAMPARRAY input_buf,
  70.228 +		  JSAMPARRAY output_buf, int num_rows)
  70.229 +{
  70.230 +  my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
  70.231 +  register JSAMPROW ptr;
  70.232 +  register histptr histp;
  70.233 +  register hist3d histogram = cquantize->histogram;
  70.234 +  int row;
  70.235 +  JDIMENSION col;
  70.236 +  JDIMENSION width = cinfo->output_width;
  70.237 +
  70.238 +  for (row = 0; row < num_rows; row++) {
  70.239 +    ptr = input_buf[row];
  70.240 +    for (col = width; col > 0; col--) {
  70.241 +      /* get pixel value and index into the histogram */
  70.242 +      histp = & histogram[GETJSAMPLE(ptr[0]) >> C0_SHIFT]
  70.243 +			 [GETJSAMPLE(ptr[1]) >> C1_SHIFT]
  70.244 +			 [GETJSAMPLE(ptr[2]) >> C2_SHIFT];
  70.245 +      /* increment, check for overflow and undo increment if so. */
  70.246 +      if (++(*histp) <= 0)
  70.247 +	(*histp)--;
  70.248 +      ptr += 3;
  70.249 +    }
  70.250 +  }
  70.251 +}
  70.252 +
  70.253 +
  70.254 +/*
  70.255 + * Next we have the really interesting routines: selection of a colormap
  70.256 + * given the completed histogram.
  70.257 + * These routines work with a list of "boxxes", each representing a rectangular
  70.258 + * subset of the input color space (to histogram precision).
  70.259 + */
  70.260 +
  70.261 +typedef struct {
  70.262 +  /* The bounds of the boxx (inclusive); expressed as histogram indexes */
  70.263 +  int c0min, c0max;
  70.264 +  int c1min, c1max;
  70.265 +  int c2min, c2max;
  70.266 +  /* The volume (actually 2-norm) of the boxx */
  70.267 +  INT32 volume;
  70.268 +  /* The number of nonzero histogram cells within this boxx */
  70.269 +  long colorcount;
  70.270 +} boxx;
  70.271 +
  70.272 +typedef boxx * boxxptr;
  70.273 +
  70.274 +
  70.275 +LOCAL(boxxptr)
  70.276 +find_biggest_color_pop (boxxptr boxxlist, int numboxxes)
  70.277 +/* Find the splittable boxx with the largest color population */
  70.278 +/* Returns NULL if no splittable boxxes remain */
  70.279 +{
  70.280 +  register boxxptr boxxp;
  70.281 +  register int i;
  70.282 +  register long maxc = 0;
  70.283 +  boxxptr which = NULL;
  70.284 +  
  70.285 +  for (i = 0, boxxp = boxxlist; i < numboxxes; i++, boxxp++) {
  70.286 +    if (boxxp->colorcount > maxc && boxxp->volume > 0) {
  70.287 +      which = boxxp;
  70.288 +      maxc = boxxp->colorcount;
  70.289 +    }
  70.290 +  }
  70.291 +  return which;
  70.292 +}
  70.293 +
  70.294 +
  70.295 +LOCAL(boxxptr)
  70.296 +find_biggest_volume (boxxptr boxxlist, int numboxxes)
  70.297 +/* Find the splittable boxx with the largest (scaled) volume */
  70.298 +/* Returns NULL if no splittable boxxes remain */
  70.299 +{
  70.300 +  register boxxptr boxxp;
  70.301 +  register int i;
  70.302 +  register INT32 maxv = 0;
  70.303 +  boxxptr which = NULL;
  70.304 +  
  70.305 +  for (i = 0, boxxp = boxxlist; i < numboxxes; i++, boxxp++) {
  70.306 +    if (boxxp->volume > maxv) {
  70.307 +      which = boxxp;
  70.308 +      maxv = boxxp->volume;
  70.309 +    }
  70.310 +  }
  70.311 +  return which;
  70.312 +}
  70.313 +
  70.314 +
  70.315 +LOCAL(void)
  70.316 +update_boxx (j_decompress_ptr cinfo, boxxptr boxxp)
  70.317 +/* Shrink the min/max bounds of a boxx to enclose only nonzero elements, */
  70.318 +/* and recompute its volume and population */
  70.319 +{
  70.320 +  my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
  70.321 +  hist3d histogram = cquantize->histogram;
  70.322 +  histptr histp;
  70.323 +  int c0,c1,c2;
  70.324 +  int c0min,c0max,c1min,c1max,c2min,c2max;
  70.325 +  INT32 dist0,dist1,dist2;
  70.326 +  long ccount;
  70.327 +  
  70.328 +  c0min = boxxp->c0min;  c0max = boxxp->c0max;
  70.329 +  c1min = boxxp->c1min;  c1max = boxxp->c1max;
  70.330 +  c2min = boxxp->c2min;  c2max = boxxp->c2max;
  70.331 +  
  70.332 +  if (c0max > c0min)
  70.333 +    for (c0 = c0min; c0 <= c0max; c0++)
  70.334 +      for (c1 = c1min; c1 <= c1max; c1++) {
  70.335 +	histp = & histogram[c0][c1][c2min];
  70.336 +	for (c2 = c2min; c2 <= c2max; c2++)
  70.337 +	  if (*histp++ != 0) {
  70.338 +	    boxxp->c0min = c0min = c0;
  70.339 +	    goto have_c0min;
  70.340 +	  }
  70.341 +      }
  70.342 + have_c0min:
  70.343 +  if (c0max > c0min)
  70.344 +    for (c0 = c0max; c0 >= c0min; c0--)
  70.345 +      for (c1 = c1min; c1 <= c1max; c1++) {
  70.346 +	histp = & histogram[c0][c1][c2min];
  70.347 +	for (c2 = c2min; c2 <= c2max; c2++)
  70.348 +	  if (*histp++ != 0) {
  70.349 +	    boxxp->c0max = c0max = c0;
  70.350 +	    goto have_c0max;
  70.351 +	  }
  70.352 +      }
  70.353 + have_c0max:
  70.354 +  if (c1max > c1min)
  70.355 +    for (c1 = c1min; c1 <= c1max; c1++)
  70.356 +      for (c0 = c0min; c0 <= c0max; c0++) {
  70.357 +	histp = & histogram[c0][c1][c2min];
  70.358 +	for (c2 = c2min; c2 <= c2max; c2++)
  70.359 +	  if (*histp++ != 0) {
  70.360 +	    boxxp->c1min = c1min = c1;
  70.361 +	    goto have_c1min;
  70.362 +	  }
  70.363 +      }
  70.364 + have_c1min:
  70.365 +  if (c1max > c1min)
  70.366 +    for (c1 = c1max; c1 >= c1min; c1--)
  70.367 +      for (c0 = c0min; c0 <= c0max; c0++) {
  70.368 +	histp = & histogram[c0][c1][c2min];
  70.369 +	for (c2 = c2min; c2 <= c2max; c2++)
  70.370 +	  if (*histp++ != 0) {
  70.371 +	    boxxp->c1max = c1max = c1;
  70.372 +	    goto have_c1max;
  70.373 +	  }
  70.374 +      }
  70.375 + have_c1max:
  70.376 +  if (c2max > c2min)
  70.377 +    for (c2 = c2min; c2 <= c2max; c2++)
  70.378 +      for (c0 = c0min; c0 <= c0max; c0++) {
  70.379 +	histp = & histogram[c0][c1min][c2];
  70.380 +	for (c1 = c1min; c1 <= c1max; c1++, histp += HIST_C2_ELEMS)
  70.381 +	  if (*histp != 0) {
  70.382 +	    boxxp->c2min = c2min = c2;
  70.383 +	    goto have_c2min;
  70.384 +	  }
  70.385 +      }
  70.386 + have_c2min:
  70.387 +  if (c2max > c2min)
  70.388 +    for (c2 = c2max; c2 >= c2min; c2--)
  70.389 +      for (c0 = c0min; c0 <= c0max; c0++) {
  70.390 +	histp = & histogram[c0][c1min][c2];
  70.391 +	for (c1 = c1min; c1 <= c1max; c1++, histp += HIST_C2_ELEMS)
  70.392 +	  if (*histp != 0) {
  70.393 +	    boxxp->c2max = c2max = c2;
  70.394 +	    goto have_c2max;
  70.395 +	  }
  70.396 +      }
  70.397 + have_c2max:
  70.398 +
  70.399 +  /* Update boxx volume.
  70.400 +   * We use 2-norm rather than real volume here; this biases the method
  70.401 +   * against making long narrow boxxes, and it has the side benefit that
  70.402 +   * a boxx is splittable iff norm > 0.
  70.403 +   * Since the differences are expressed in histogram-cell units,
  70.404 +   * we have to shift back to JSAMPLE units to get consistent distances;
  70.405 +   * after which, we scale according to the selected distance scale factors.
  70.406 +   */
  70.407 +  dist0 = ((c0max - c0min) << C0_SHIFT) * C0_SCALE;
  70.408 +  dist1 = ((c1max - c1min) << C1_SHIFT) * C1_SCALE;
  70.409 +  dist2 = ((c2max - c2min) << C2_SHIFT) * C2_SCALE;
  70.410 +  boxxp->volume = dist0*dist0 + dist1*dist1 + dist2*dist2;
  70.411 +  
  70.412 +  /* Now scan remaining volume of boxx and compute population */
  70.413 +  ccount = 0;
  70.414 +  for (c0 = c0min; c0 <= c0max; c0++)
  70.415 +    for (c1 = c1min; c1 <= c1max; c1++) {
  70.416 +      histp = & histogram[c0][c1][c2min];
  70.417 +      for (c2 = c2min; c2 <= c2max; c2++, histp++)
  70.418 +	if (*histp != 0) {
  70.419 +	  ccount++;
  70.420 +	}
  70.421 +    }
  70.422 +  boxxp->colorcount = ccount;
  70.423 +}
  70.424 +
  70.425 +
  70.426 +LOCAL(int)
  70.427 +median_cut (j_decompress_ptr cinfo, boxxptr boxxlist, int numboxxes,
  70.428 +	    int desired_colors)
  70.429 +/* Repeatedly select and split the largest boxx until we have enough boxxes */
  70.430 +{
  70.431 +  int n,lb;
  70.432 +  int c0,c1,c2,cmax;
  70.433 +  register boxxptr b1,b2;
  70.434 +
  70.435 +  while (numboxxes < desired_colors) {
  70.436 +    /* Select boxx to split.
  70.437 +     * Current algorithm: by population for first half, then by volume.
  70.438 +     */
  70.439 +    if (numboxxes*2 <= desired_colors) {
  70.440 +      b1 = find_biggest_color_pop(boxxlist, numboxxes);
  70.441 +    } else {
  70.442 +      b1 = find_biggest_volume(boxxlist, numboxxes);
  70.443 +    }
  70.444 +    if (b1 == NULL)		/* no splittable boxxes left! */
  70.445 +      break;
  70.446 +    b2 = &boxxlist[numboxxes];	/* where new boxx will go */
  70.447 +    /* Copy the color bounds to the new boxx. */
  70.448 +    b2->c0max = b1->c0max; b2->c1max = b1->c1max; b2->c2max = b1->c2max;
  70.449 +    b2->c0min = b1->c0min; b2->c1min = b1->c1min; b2->c2min = b1->c2min;
  70.450 +    /* Choose which axis to split the boxx on.
  70.451 +     * Current algorithm: longest scaled axis.
  70.452 +     * See notes in update_boxx about scaling distances.
  70.453 +     */
  70.454 +    c0 = ((b1->c0max - b1->c0min) << C0_SHIFT) * C0_SCALE;
  70.455 +    c1 = ((b1->c1max - b1->c1min) << C1_SHIFT) * C1_SCALE;
  70.456 +    c2 = ((b1->c2max - b1->c2min) << C2_SHIFT) * C2_SCALE;
  70.457 +    /* We want to break any ties in favor of green, then red, blue last.
  70.458 +     * This code does the right thing for R,G,B or B,G,R color orders only.
  70.459 +     */
  70.460 +#if RGB_RED == 0
  70.461 +    cmax = c1; n = 1;
  70.462 +    if (c0 > cmax) { cmax = c0; n = 0; }
  70.463 +    if (c2 > cmax) { n = 2; }
  70.464 +#else
  70.465 +    cmax = c1; n = 1;
  70.466 +    if (c2 > cmax) { cmax = c2; n = 2; }
  70.467 +    if (c0 > cmax) { n = 0; }
  70.468 +#endif
  70.469 +    /* Choose split point along selected axis, and update boxx bounds.
  70.470 +     * Current algorithm: split at halfway point.
  70.471 +     * (Since the boxx has been shrunk to minimum volume,
  70.472 +     * any split will produce two nonempty subboxxes.)
  70.473 +     * Note that lb value is max for lower boxx, so must be < old max.
  70.474 +     */
  70.475 +    switch (n) {
  70.476 +    case 0:
  70.477 +      lb = (b1->c0max + b1->c0min) / 2;
  70.478 +      b1->c0max = lb;
  70.479 +      b2->c0min = lb+1;
  70.480 +      break;
  70.481 +    case 1:
  70.482 +      lb = (b1->c1max + b1->c1min) / 2;
  70.483 +      b1->c1max = lb;
  70.484 +      b2->c1min = lb+1;
  70.485 +      break;
  70.486 +    case 2:
  70.487 +      lb = (b1->c2max + b1->c2min) / 2;
  70.488 +      b1->c2max = lb;
  70.489 +      b2->c2min = lb+1;
  70.490 +      break;
  70.491 +    }
  70.492 +    /* Update stats for boxxes */
  70.493 +    update_boxx(cinfo, b1);
  70.494 +    update_boxx(cinfo, b2);
  70.495 +    numboxxes++;
  70.496 +  }
  70.497 +  return numboxxes;
  70.498 +}
  70.499 +
  70.500 +
  70.501 +LOCAL(void)
  70.502 +compute_color (j_decompress_ptr cinfo, boxxptr boxxp, int icolor)
  70.503 +/* Compute representative color for a boxx, put it in colormap[icolor] */
  70.504 +{
  70.505 +  /* Current algorithm: mean weighted by pixels (not colors) */
  70.506 +  /* Note it is important to get the rounding correct! */
  70.507 +  my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
  70.508 +  hist3d histogram = cquantize->histogram;
  70.509 +  histptr histp;
  70.510 +  int c0,c1,c2;
  70.511 +  int c0min,c0max,c1min,c1max,c2min,c2max;
  70.512 +  long count;
  70.513 +  long total = 0;
  70.514 +  long c0total = 0;
  70.515 +  long c1total = 0;
  70.516 +  long c2total = 0;
  70.517 +  
  70.518 +  c0min = boxxp->c0min;  c0max = boxxp->c0max;
  70.519 +  c1min = boxxp->c1min;  c1max = boxxp->c1max;
  70.520 +  c2min = boxxp->c2min;  c2max = boxxp->c2max;
  70.521 +  
  70.522 +  for (c0 = c0min; c0 <= c0max; c0++)
  70.523 +    for (c1 = c1min; c1 <= c1max; c1++) {
  70.524 +      histp = & histogram[c0][c1][c2min];
  70.525 +      for (c2 = c2min; c2 <= c2max; c2++) {
  70.526 +	if ((count = *histp++) != 0) {
  70.527 +	  total += count;
  70.528 +	  c0total += ((c0 << C0_SHIFT) + ((1<<C0_SHIFT)>>1)) * count;
  70.529 +	  c1total += ((c1 << C1_SHIFT) + ((1<<C1_SHIFT)>>1)) * count;
  70.530 +	  c2total += ((c2 << C2_SHIFT) + ((1<<C2_SHIFT)>>1)) * count;
  70.531 +	}
  70.532 +      }
  70.533 +    }
  70.534 +  
  70.535 +  cinfo->colormap[0][icolor] = (JSAMPLE) ((c0total + (total>>1)) / total);
  70.536 +  cinfo->colormap[1][icolor] = (JSAMPLE) ((c1total + (total>>1)) / total);
  70.537 +  cinfo->colormap[2][icolor] = (JSAMPLE) ((c2total + (total>>1)) / total);
  70.538 +}
  70.539 +
  70.540 +
  70.541 +LOCAL(void)
  70.542 +select_colors (j_decompress_ptr cinfo, int desired_colors)
  70.543 +/* Master routine for color selection */
  70.544 +{
  70.545 +  boxxptr boxxlist;
  70.546 +  int numboxxes;
  70.547 +  int i;
  70.548 +
  70.549 +  /* Allocate workspace for boxx list */
  70.550 +  boxxlist = (boxxptr) (*cinfo->mem->alloc_small)
  70.551 +    ((j_common_ptr) cinfo, JPOOL_IMAGE, desired_colors * SIZEOF(boxx));
  70.552 +  /* Initialize one boxx containing whole space */
  70.553 +  numboxxes = 1;
  70.554 +  boxxlist[0].c0min = 0;
  70.555 +  boxxlist[0].c0max = MAXJSAMPLE >> C0_SHIFT;
  70.556 +  boxxlist[0].c1min = 0;
  70.557 +  boxxlist[0].c1max = MAXJSAMPLE >> C1_SHIFT;
  70.558 +  boxxlist[0].c2min = 0;
  70.559 +  boxxlist[0].c2max = MAXJSAMPLE >> C2_SHIFT;
  70.560 +  /* Shrink it to actually-used volume and set its statistics */
  70.561 +  update_boxx(cinfo, & boxxlist[0]);
  70.562 +  /* Perform median-cut to produce final boxx list */
  70.563 +  numboxxes = median_cut(cinfo, boxxlist, numboxxes, desired_colors);
  70.564 +  /* Compute the representative color for each boxx, fill colormap */
  70.565 +  for (i = 0; i < numboxxes; i++)
  70.566 +    compute_color(cinfo, & boxxlist[i], i);
  70.567 +  cinfo->actual_number_of_colors = numboxxes;
  70.568 +  TRACEMS1(cinfo, 1, JTRC_QUANT_SELECTED, numboxxes);
  70.569 +}
  70.570 +
  70.571 +
  70.572 +/*
  70.573 + * These routines are concerned with the time-critical task of mapping input
  70.574 + * colors to the nearest color in the selected colormap.
  70.575 + *
  70.576 + * We re-use the histogram space as an "inverse color map", essentially a
  70.577 + * cache for the results of nearest-color searches.  All colors within a
  70.578 + * histogram cell will be mapped to the same colormap entry, namely the one
  70.579 + * closest to the cell's center.  This may not be quite the closest entry to
  70.580 + * the actual input color, but it's almost as good.  A zero in the cache
  70.581 + * indicates we haven't found the nearest color for that cell yet; the array
  70.582 + * is cleared to zeroes before starting the mapping pass.  When we find the
  70.583 + * nearest color for a cell, its colormap index plus one is recorded in the
  70.584 + * cache for future use.  The pass2 scanning routines call fill_inverse_cmap
  70.585 + * when they need to use an unfilled entry in the cache.
  70.586 + *
  70.587 + * Our method of efficiently finding nearest colors is based on the "locally
  70.588 + * sorted search" idea described by Heckbert and on the incremental distance
  70.589 + * calculation described by Spencer W. Thomas in chapter III.1 of Graphics
  70.590 + * Gems II (James Arvo, ed.  Academic Press, 1991).  Thomas points out that
  70.591 + * the distances from a given colormap entry to each cell of the histogram can
  70.592 + * be computed quickly using an incremental method: the differences between
  70.593 + * distances to adjacent cells themselves differ by a constant.  This allows a
  70.594 + * fairly fast implementation of the "brute force" approach of computing the
  70.595 + * distance from every colormap entry to every histogram cell.  Unfortunately,
  70.596 + * it needs a work array to hold the best-distance-so-far for each histogram
  70.597 + * cell (because the inner loop has to be over cells, not colormap entries).
  70.598 + * The work array elements have to be INT32s, so the work array would need
  70.599 + * 256Kb at our recommended precision.  This is not feasible in DOS machines.
  70.600 + *
  70.601 + * To get around these problems, we apply Thomas' method to compute the
  70.602 + * nearest colors for only the cells within a small subboxx of the histogram.
  70.603 + * The work array need be only as big as the subboxx, so the memory usage
  70.604 + * problem is solved.  Furthermore, we need not fill subboxxes that are never
  70.605 + * referenced in pass2; many images use only part of the color gamut, so a
  70.606 + * fair amount of work is saved.  An additional advantage of this
  70.607 + * approach is that we can apply Heckbert's locality criterion to quickly
  70.608 + * eliminate colormap entries that are far away from the subboxx; typically
  70.609 + * three-fourths of the colormap entries are rejected by Heckbert's criterion,
  70.610 + * and we need not compute their distances to individual cells in the subboxx.
  70.611 + * The speed of this approach is heavily influenced by the subboxx size: too
  70.612 + * small means too much overhead, too big loses because Heckbert's criterion
  70.613 + * can't eliminate as many colormap entries.  Empirically the best subboxx
  70.614 + * size seems to be about 1/512th of the histogram (1/8th in each direction).
  70.615 + *
  70.616 + * Thomas' article also describes a refined method which is asymptotically
  70.617 + * faster than the brute-force method, but it is also far more complex and
  70.618 + * cannot efficiently be applied to small subboxxes.  It is therefore not
  70.619 + * useful for programs intended to be portable to DOS machines.  On machines
  70.620 + * with plenty of memory, filling the whole histogram in one shot with Thomas'
  70.621 + * refined method might be faster than the present code --- but then again,
  70.622 + * it might not be any faster, and it's certainly more complicated.
  70.623 + */
  70.624 +
  70.625 +
  70.626 +/* log2(histogram cells in update boxx) for each axis; this can be adjusted */
  70.627 +#define BOX_C0_LOG  (HIST_C0_BITS-3)
  70.628 +#define BOX_C1_LOG  (HIST_C1_BITS-3)
  70.629 +#define BOX_C2_LOG  (HIST_C2_BITS-3)
  70.630 +
  70.631 +#define BOX_C0_ELEMS  (1<<BOX_C0_LOG) /* # of hist cells in update boxx */
  70.632 +#define BOX_C1_ELEMS  (1<<BOX_C1_LOG)
  70.633 +#define BOX_C2_ELEMS  (1<<BOX_C2_LOG)
  70.634 +
  70.635 +#define BOX_C0_SHIFT  (C0_SHIFT + BOX_C0_LOG)
  70.636 +#define BOX_C1_SHIFT  (C1_SHIFT + BOX_C1_LOG)
  70.637 +#define BOX_C2_SHIFT  (C2_SHIFT + BOX_C2_LOG)
  70.638 +
  70.639 +
  70.640 +/*
  70.641 + * The next three routines implement inverse colormap filling.  They could
  70.642 + * all be folded into one big routine, but splitting them up this way saves
  70.643 + * some stack space (the mindist[] and bestdist[] arrays need not coexist)
  70.644 + * and may allow some compilers to produce better code by registerizing more
  70.645 + * inner-loop variables.
  70.646 + */
  70.647 +
  70.648 +LOCAL(int)
  70.649 +find_nearby_colors (j_decompress_ptr cinfo, int minc0, int minc1, int minc2,
  70.650 +		    JSAMPLE colorlist[])
  70.651 +/* Locate the colormap entries close enough to an update boxx to be candidates
  70.652 + * for the nearest entry to some cell(s) in the update boxx.  The update boxx
  70.653 + * is specified by the center coordinates of its first cell.  The number of
  70.654 + * candidate colormap entries is returned, and their colormap indexes are
  70.655 + * placed in colorlist[].
  70.656 + * This routine uses Heckbert's "locally sorted search" criterion to select
  70.657 + * the colors that need further consideration.
  70.658 + */
  70.659 +{
  70.660 +  int numcolors = cinfo->actual_number_of_colors;
  70.661 +  int maxc0, maxc1, maxc2;
  70.662 +  int centerc0, centerc1, centerc2;
  70.663 +  int i, x, ncolors;
  70.664 +  INT32 minmaxdist, min_dist, max_dist, tdist;
  70.665 +  INT32 mindist[MAXNUMCOLORS];	/* min distance to colormap entry i */
  70.666 +
  70.667 +  /* Compute true coordinates of update boxx's upper corner and center.
  70.668 +   * Actually we compute the coordinates of the center of the upper-corner
  70.669 +   * histogram cell, which are the upper bounds of the volume we care about.
  70.670 +   * Note that since ">>" rounds down, the "center" values may be closer to
  70.671 +   * min than to max; hence comparisons to them must be "<=", not "<".
  70.672 +   */
  70.673 +  maxc0 = minc0 + ((1 << BOX_C0_SHIFT) - (1 << C0_SHIFT));
  70.674 +  centerc0 = (minc0 + maxc0) >> 1;
  70.675 +  maxc1 = minc1 + ((1 << BOX_C1_SHIFT) - (1 << C1_SHIFT));
  70.676 +  centerc1 = (minc1 + maxc1) >> 1;
  70.677 +  maxc2 = minc2 + ((1 << BOX_C2_SHIFT) - (1 << C2_SHIFT));
  70.678 +  centerc2 = (minc2 + maxc2) >> 1;
  70.679 +
  70.680 +  /* For each color in colormap, find:
  70.681 +   *  1. its minimum squared-distance to any point in the update boxx
  70.682 +   *     (zero if color is within update boxx);
  70.683 +   *  2. its maximum squared-distance to any point in the update boxx.
  70.684 +   * Both of these can be found by considering only the corners of the boxx.
  70.685 +   * We save the minimum distance for each color in mindist[];
  70.686 +   * only the smallest maximum distance is of interest.
  70.687 +   */
  70.688 +  minmaxdist = 0x7FFFFFFFL;
  70.689 +
  70.690 +  for (i = 0; i < numcolors; i++) {
  70.691 +    /* We compute the squared-c0-distance term, then add in the other two. */
  70.692 +    x = GETJSAMPLE(cinfo->colormap[0][i]);
  70.693 +    if (x < minc0) {
  70.694 +      tdist = (x - minc0) * C0_SCALE;
  70.695 +      min_dist = tdist*tdist;
  70.696 +      tdist = (x - maxc0) * C0_SCALE;
  70.697 +      max_dist = tdist*tdist;
  70.698 +    } else if (x > maxc0) {
  70.699 +      tdist = (x - maxc0) * C0_SCALE;
  70.700 +      min_dist = tdist*tdist;
  70.701 +      tdist = (x - minc0) * C0_SCALE;
  70.702 +      max_dist = tdist*tdist;
  70.703 +    } else {
  70.704 +      /* within cell range so no contribution to min_dist */
  70.705 +      min_dist = 0;
  70.706 +      if (x <= centerc0) {
  70.707 +	tdist = (x - maxc0) * C0_SCALE;
  70.708 +	max_dist = tdist*tdist;
  70.709 +      } else {
  70.710 +	tdist = (x - minc0) * C0_SCALE;
  70.711 +	max_dist = tdist*tdist;
  70.712 +      }
  70.713 +    }
  70.714 +
  70.715 +    x = GETJSAMPLE(cinfo->colormap[1][i]);
  70.716 +    if (x < minc1) {
  70.717 +      tdist = (x - minc1) * C1_SCALE;
  70.718 +      min_dist += tdist*tdist;
  70.719 +      tdist = (x - maxc1) * C1_SCALE;
  70.720 +      max_dist += tdist*tdist;
  70.721 +    } else if (x > maxc1) {
  70.722 +      tdist = (x - maxc1) * C1_SCALE;
  70.723 +      min_dist += tdist*tdist;
  70.724 +      tdist = (x - minc1) * C1_SCALE;
  70.725 +      max_dist += tdist*tdist;
  70.726 +    } else {
  70.727 +      /* within cell range so no contribution to min_dist */
  70.728 +      if (x <= centerc1) {
  70.729 +	tdist = (x - maxc1) * C1_SCALE;
  70.730 +	max_dist += tdist*tdist;
  70.731 +      } else {
  70.732 +	tdist = (x - minc1) * C1_SCALE;
  70.733 +	max_dist += tdist*tdist;
  70.734 +      }
  70.735 +    }
  70.736 +
  70.737 +    x = GETJSAMPLE(cinfo->colormap[2][i]);
  70.738 +    if (x < minc2) {
  70.739 +      tdist = (x - minc2) * C2_SCALE;
  70.740 +      min_dist += tdist*tdist;
  70.741 +      tdist = (x - maxc2) * C2_SCALE;
  70.742 +      max_dist += tdist*tdist;
  70.743 +    } else if (x > maxc2) {
  70.744 +      tdist = (x - maxc2) * C2_SCALE;
  70.745 +      min_dist += tdist*tdist;
  70.746 +      tdist = (x - minc2) * C2_SCALE;
  70.747 +      max_dist += tdist*tdist;
  70.748 +    } else {
  70.749 +      /* within cell range so no contribution to min_dist */
  70.750 +      if (x <= centerc2) {
  70.751 +	tdist = (x - maxc2) * C2_SCALE;
  70.752 +	max_dist += tdist*tdist;
  70.753 +      } else {
  70.754 +	tdist = (x - minc2) * C2_SCALE;
  70.755 +	max_dist += tdist*tdist;
  70.756 +      }
  70.757 +    }
  70.758 +
  70.759 +    mindist[i] = min_dist;	/* save away the results */
  70.760 +    if (max_dist < minmaxdist)
  70.761 +      minmaxdist = max_dist;
  70.762 +  }
  70.763 +
  70.764 +  /* Now we know that no cell in the update boxx is more than minmaxdist
  70.765 +   * away from some colormap entry.  Therefore, only colors that are
  70.766 +   * within minmaxdist of some part of the boxx need be considered.
  70.767 +   */
  70.768 +  ncolors = 0;
  70.769 +  for (i = 0; i < numcolors; i++) {
  70.770 +    if (mindist[i] <= minmaxdist)
  70.771 +      colorlist[ncolors++] = (JSAMPLE) i;
  70.772 +  }
  70.773 +  return ncolors;
  70.774 +}
  70.775 +
  70.776 +
  70.777 +LOCAL(void)
  70.778 +find_best_colors (j_decompress_ptr cinfo, int minc0, int minc1, int minc2,
  70.779 +		  int numcolors, JSAMPLE colorlist[], JSAMPLE bestcolor[])
  70.780 +/* Find the closest colormap entry for each cell in the update boxx,
  70.781 + * given the list of candidate colors prepared by find_nearby_colors.
  70.782 + * Return the indexes of the closest entries in the bestcolor[] array.
  70.783 + * This routine uses Thomas' incremental distance calculation method to
  70.784 + * find the distance from a colormap entry to successive cells in the boxx.
  70.785 + */
  70.786 +{
  70.787 +  int ic0, ic1, ic2;
  70.788 +  int i, icolor;
  70.789 +  register INT32 * bptr;	/* pointer into bestdist[] array */
  70.790 +  JSAMPLE * cptr;		/* pointer into bestcolor[] array */
  70.791 +  INT32 dist0, dist1;		/* initial distance values */
  70.792 +  register INT32 dist2;		/* current distance in inner loop */
  70.793 +  INT32 xx0, xx1;		/* distance increments */
  70.794 +  register INT32 xx2;
  70.795 +  INT32 inc0, inc1, inc2;	/* initial values for increments */
  70.796 +  /* This array holds the distance to the nearest-so-far color for each cell */
  70.797 +  INT32 bestdist[BOX_C0_ELEMS * BOX_C1_ELEMS * BOX_C2_ELEMS];
  70.798 +
  70.799 +  /* Initialize best-distance for each cell of the update boxx */
  70.800 +  bptr = bestdist;
  70.801 +  for (i = BOX_C0_ELEMS*BOX_C1_ELEMS*BOX_C2_ELEMS-1; i >= 0; i--)
  70.802 +    *bptr++ = 0x7FFFFFFFL;
  70.803 +  
  70.804 +  /* For each color selected by find_nearby_colors,
  70.805 +   * compute its distance to the center of each cell in the boxx.
  70.806 +   * If that's less than best-so-far, update best distance and color number.
  70.807 +   */
  70.808 +  
  70.809 +  /* Nominal steps between cell centers ("x" in Thomas article) */
  70.810 +#define STEP_C0  ((1 << C0_SHIFT) * C0_SCALE)
  70.811 +#define STEP_C1  ((1 << C1_SHIFT) * C1_SCALE)
  70.812 +#define STEP_C2  ((1 << C2_SHIFT) * C2_SCALE)
  70.813 +  
  70.814 +  for (i = 0; i < numcolors; i++) {
  70.815 +    icolor = GETJSAMPLE(colorlist[i]);
  70.816 +    /* Compute (square of) distance from minc0/c1/c2 to this color */
  70.817 +    inc0 = (minc0 - GETJSAMPLE(cinfo->colormap[0][icolor])) * C0_SCALE;
  70.818 +    dist0 = inc0*inc0;
  70.819 +    inc1 = (minc1 - GETJSAMPLE(cinfo->colormap[1][icolor])) * C1_SCALE;
  70.820 +    dist0 += inc1*inc1;
  70.821 +    inc2 = (minc2 - GETJSAMPLE(cinfo->colormap[2][icolor])) * C2_SCALE;
  70.822 +    dist0 += inc2*inc2;
  70.823 +    /* Form the initial difference increments */
  70.824 +    inc0 = inc0 * (2 * STEP_C0) + STEP_C0 * STEP_C0;
  70.825 +    inc1 = inc1 * (2 * STEP_C1) + STEP_C1 * STEP_C1;
  70.826 +    inc2 = inc2 * (2 * STEP_C2) + STEP_C2 * STEP_C2;
  70.827 +    /* Now loop over all cells in boxx, updating distance per Thomas method */
  70.828 +    bptr = bestdist;
  70.829 +    cptr = bestcolor;
  70.830 +    xx0 = inc0;
  70.831 +    for (ic0 = BOX_C0_ELEMS-1; ic0 >= 0; ic0--) {
  70.832 +      dist1 = dist0;
  70.833 +      xx1 = inc1;
  70.834 +      for (ic1 = BOX_C1_ELEMS-1; ic1 >= 0; ic1--) {
  70.835 +	dist2 = dist1;
  70.836 +	xx2 = inc2;
  70.837 +	for (ic2 = BOX_C2_ELEMS-1; ic2 >= 0; ic2--) {
  70.838 +	  if (dist2 < *bptr) {
  70.839 +	    *bptr = dist2;
  70.840 +	    *cptr = (JSAMPLE) icolor;
  70.841 +	  }
  70.842 +	  dist2 += xx2;
  70.843 +	  xx2 += 2 * STEP_C2 * STEP_C2;
  70.844 +	  bptr++;
  70.845 +	  cptr++;
  70.846 +	}
  70.847 +	dist1 += xx1;
  70.848 +	xx1 += 2 * STEP_C1 * STEP_C1;
  70.849 +      }
  70.850 +      dist0 += xx0;
  70.851 +      xx0 += 2 * STEP_C0 * STEP_C0;
  70.852 +    }
  70.853 +  }
  70.854 +}
  70.855 +
  70.856 +
  70.857 +LOCAL(void)
  70.858 +fill_inverse_cmap (j_decompress_ptr cinfo, int c0, int c1, int c2)
  70.859 +/* Fill the inverse-colormap entries in the update boxx that contains */
  70.860 +/* histogram cell c0/c1/c2.  (Only that one cell MUST be filled, but */
  70.861 +/* we can fill as many others as we wish.) */
  70.862 +{
  70.863 +  my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
  70.864 +  hist3d histogram = cquantize->histogram;
  70.865 +  int minc0, minc1, minc2;	/* lower left corner of update boxx */
  70.866 +  int ic0, ic1, ic2;
  70.867 +  register JSAMPLE * cptr;	/* pointer into bestcolor[] array */
  70.868 +  register histptr cachep;	/* pointer into main cache array */
  70.869 +  /* This array lists the candidate colormap indexes. */
  70.870 +  JSAMPLE colorlist[MAXNUMCOLORS];
  70.871 +  int numcolors;		/* number of candidate colors */
  70.872 +  /* This array holds the actually closest colormap index for each cell. */
  70.873 +  JSAMPLE bestcolor[BOX_C0_ELEMS * BOX_C1_ELEMS * BOX_C2_ELEMS];
  70.874 +
  70.875 +  /* Convert cell coordinates to update boxx ID */
  70.876 +  c0 >>= BOX_C0_LOG;
  70.877 +  c1 >>= BOX_C1_LOG;
  70.878 +  c2 >>= BOX_C2_LOG;
  70.879 +
  70.880 +  /* Compute true coordinates of update boxx's origin corner.
  70.881 +   * Actually we compute the coordinates of the center of the corner
  70.882 +   * histogram cell, which are the lower bounds of the volume we care about.
  70.883 +   */
  70.884 +  minc0 = (c0 << BOX_C0_SHIFT) + ((1 << C0_SHIFT) >> 1);
  70.885 +  minc1 = (c1 << BOX_C1_SHIFT) + ((1 << C1_SHIFT) >> 1);
  70.886 +  minc2 = (c2 << BOX_C2_SHIFT) + ((1 << C2_SHIFT) >> 1);
  70.887 +  
  70.888 +  /* Determine which colormap entries are close enough to be candidates
  70.889 +   * for the nearest entry to some cell in the update boxx.
  70.890 +   */
  70.891 +  numcolors = find_nearby_colors(cinfo, minc0, minc1, minc2, colorlist);
  70.892 +
  70.893 +  /* Determine the actually nearest colors. */
  70.894 +  find_best_colors(cinfo, minc0, minc1, minc2, numcolors, colorlist,
  70.895 +		   bestcolor);
  70.896 +
  70.897 +  /* Save the best color numbers (plus 1) in the main cache array */
  70.898 +  c0 <<= BOX_C0_LOG;		/* convert ID back to base cell indexes */
  70.899 +  c1 <<= BOX_C1_LOG;
  70.900 +  c2 <<= BOX_C2_LOG;
  70.901 +  cptr = bestcolor;
  70.902 +  for (ic0 = 0; ic0 < BOX_C0_ELEMS; ic0++) {
  70.903 +    for (ic1 = 0; ic1 < BOX_C1_ELEMS; ic1++) {
  70.904 +      cachep = & histogram[c0+ic0][c1+ic1][c2];
  70.905 +      for (ic2 = 0; ic2 < BOX_C2_ELEMS; ic2++) {
  70.906 +	*cachep++ = (histcell) (GETJSAMPLE(*cptr++) + 1);
  70.907 +      }
  70.908 +    }
  70.909 +  }
  70.910 +}
  70.911 +
  70.912 +
  70.913 +/*
  70.914 + * Map some rows of pixels to the output colormapped representation.
  70.915 + */
  70.916 +
  70.917 +METHODDEF(void)
  70.918 +pass2_no_dither (j_decompress_ptr cinfo,
  70.919 +		 JSAMPARRAY input_buf, JSAMPARRAY output_buf, int num_rows)
  70.920 +/* This version performs no dithering */
  70.921 +{
  70.922 +  my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
  70.923 +  hist3d histogram = cquantize->histogram;
  70.924 +  register JSAMPROW inptr, outptr;
  70.925 +  register histptr cachep;
  70.926 +  register int c0, c1, c2;
  70.927 +  int row;
  70.928 +  JDIMENSION col;
  70.929 +  JDIMENSION width = cinfo->output_width;
  70.930 +
  70.931 +  for (row = 0; row < num_rows; row++) {
  70.932 +    inptr = input_buf[row];
  70.933 +    outptr = output_buf[row];
  70.934 +    for (col = width; col > 0; col--) {
  70.935 +      /* get pixel value and index into the cache */
  70.936 +      c0 = GETJSAMPLE(*inptr++) >> C0_SHIFT;
  70.937 +      c1 = GETJSAMPLE(*inptr++) >> C1_SHIFT;
  70.938 +      c2 = GETJSAMPLE(*inptr++) >> C2_SHIFT;
  70.939 +      cachep = & histogram[c0][c1][c2];
  70.940 +      /* If we have not seen this color before, find nearest colormap entry */
  70.941 +      /* and update the cache */
  70.942 +      if (*cachep == 0)
  70.943 +	fill_inverse_cmap(cinfo, c0,c1,c2);
  70.944 +      /* Now emit the colormap index for this cell */
  70.945 +      *outptr++ = (JSAMPLE) (*cachep - 1);
  70.946 +    }
  70.947 +  }
  70.948 +}
  70.949 +
  70.950 +
  70.951 +METHODDEF(void)
  70.952 +pass2_fs_dither (j_decompress_ptr cinfo,
  70.953 +		 JSAMPARRAY input_buf, JSAMPARRAY output_buf, int num_rows)
  70.954 +/* This version performs Floyd-Steinberg dithering */
  70.955 +{
  70.956 +  my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
  70.957 +  hist3d histogram = cquantize->histogram;
  70.958 +  register LOCFSERROR cur0, cur1, cur2;	/* current error or pixel value */
  70.959 +  LOCFSERROR belowerr0, belowerr1, belowerr2; /* error for pixel below cur */
  70.960 +  LOCFSERROR bpreverr0, bpreverr1, bpreverr2; /* error for below/prev col */
  70.961 +  register FSERRPTR errorptr;	/* => fserrors[] at column before current */
  70.962 +  JSAMPROW inptr;		/* => current input pixel */
  70.963 +  JSAMPROW outptr;		/* => current output pixel */
  70.964 +  histptr cachep;
  70.965 +  int dir;			/* +1 or -1 depending on direction */
  70.966 +  int dir3;			/* 3*dir, for advancing inptr & errorptr */
  70.967 +  int row;
  70.968 +  JDIMENSION col;
  70.969 +  JDIMENSION width = cinfo->output_width;
  70.970 +  JSAMPLE *range_limit = cinfo->sample_range_limit;
  70.971 +  int *error_limit = cquantize->error_limiter;
  70.972 +  JSAMPROW colormap0 = cinfo->colormap[0];
  70.973 +  JSAMPROW colormap1 = cinfo->colormap[1];
  70.974 +  JSAMPROW colormap2 = cinfo->colormap[2];
  70.975 +  SHIFT_TEMPS
  70.976 +
  70.977 +  for (row = 0; row < num_rows; row++) {
  70.978 +    inptr = input_buf[row];
  70.979 +    outptr = output_buf[row];
  70.980 +    if (cquantize->on_odd_row) {
  70.981 +      /* work right to left in this row */
  70.982 +      inptr += (width-1) * 3;	/* so point to rightmost pixel */
  70.983 +      outptr += width-1;
  70.984 +      dir = -1;
  70.985 +      dir3 = -3;
  70.986 +      errorptr = cquantize->fserrors + (width+1)*3; /* => entry after last column */
  70.987 +      cquantize->on_odd_row = FALSE; /* flip for next time */
  70.988 +    } else {
  70.989 +      /* work left to right in this row */
  70.990 +      dir = 1;
  70.991 +      dir3 = 3;
  70.992 +      errorptr = cquantize->fserrors; /* => entry before first real column */
  70.993 +      cquantize->on_odd_row = TRUE; /* flip for next time */
  70.994 +    }
  70.995 +    /* Preset error values: no error propagated to first pixel from left */
  70.996 +    cur0 = cur1 = cur2 = 0;
  70.997 +    /* and no error propagated to row below yet */
  70.998 +    belowerr0 = belowerr1 = belowerr2 = 0;
  70.999 +    bpreverr0 = bpreverr1 = bpreverr2 = 0;
 70.1000 +
 70.1001 +    for (col = width; col > 0; col--) {
 70.1002 +      /* curN holds the error propagated from the previous pixel on the
 70.1003 +       * current line.  Add the error propagated from the previous line
 70.1004 +       * to form the complete error correction term for this pixel, and
 70.1005 +       * round the error term (which is expressed * 16) to an integer.
 70.1006 +       * RIGHT_SHIFT rounds towards minus infinity, so adding 8 is correct
 70.1007 +       * for either sign of the error value.
 70.1008 +       * Note: errorptr points to *previous* column's array entry.
 70.1009 +       */
 70.1010 +      cur0 = RIGHT_SHIFT(cur0 + errorptr[dir3+0] + 8, 4);
 70.1011 +      cur1 = RIGHT_SHIFT(cur1 + errorptr[dir3+1] + 8, 4);
 70.1012 +      cur2 = RIGHT_SHIFT(cur2 + errorptr[dir3+2] + 8, 4);
 70.1013 +      /* Limit the error using transfer function set by init_error_limit.
 70.1014 +       * See comments with init_error_limit for rationale.
 70.1015 +       */
 70.1016 +      cur0 = error_limit[cur0];
 70.1017 +      cur1 = error_limit[cur1];
 70.1018 +      cur2 = error_limit[cur2];
 70.1019 +      /* Form pixel value + error, and range-limit to 0..MAXJSAMPLE.
 70.1020 +       * The maximum error is +- MAXJSAMPLE (or less with error limiting);
 70.1021 +       * this sets the required size of the range_limit array.
 70.1022 +       */
 70.1023 +      cur0 += GETJSAMPLE(inptr[0]);
 70.1024 +      cur1 += GETJSAMPLE(inptr[1]);
 70.1025 +      cur2 += GETJSAMPLE(inptr[2]);
 70.1026 +      cur0 = GETJSAMPLE(range_limit[cur0]);
 70.1027 +      cur1 = GETJSAMPLE(range_limit[cur1]);
 70.1028 +      cur2 = GETJSAMPLE(range_limit[cur2]);
 70.1029 +      /* Index into the cache with adjusted pixel value */
 70.1030 +      cachep = & histogram[cur0>>C0_SHIFT][cur1>>C1_SHIFT][cur2>>C2_SHIFT];
 70.1031 +      /* If we have not seen this color before, find nearest colormap */
 70.1032 +      /* entry and update the cache */
 70.1033 +      if (*cachep == 0)
 70.1034 +	fill_inverse_cmap(cinfo, cur0>>C0_SHIFT,cur1>>C1_SHIFT,cur2>>C2_SHIFT);
 70.1035 +      /* Now emit the colormap index for this cell */
 70.1036 +      { register int pixcode = *cachep - 1;
 70.1037 +	*outptr = (JSAMPLE) pixcode;
 70.1038 +	/* Compute representation error for this pixel */
 70.1039 +	cur0 -= GETJSAMPLE(colormap0[pixcode]);
 70.1040 +	cur1 -= GETJSAMPLE(colormap1[pixcode]);
 70.1041 +	cur2 -= GETJSAMPLE(colormap2[pixcode]);
 70.1042 +      }
 70.1043 +      /* Compute error fractions to be propagated to adjacent pixels.
 70.1044 +       * Add these into the running sums, and simultaneously shift the
 70.1045 +       * next-line error sums left by 1 column.
 70.1046 +       */
 70.1047 +      { register LOCFSERROR bnexterr, delta;
 70.1048 +
 70.1049 +	bnexterr = cur0;	/* Process component 0 */
 70.1050 +	delta = cur0 * 2;
 70.1051 +	cur0 += delta;		/* form error * 3 */
 70.1052 +	errorptr[0] = (FSERROR) (bpreverr0 + cur0);
 70.1053 +	cur0 += delta;		/* form error * 5 */
 70.1054 +	bpreverr0 = belowerr0 + cur0;
 70.1055 +	belowerr0 = bnexterr;
 70.1056 +	cur0 += delta;		/* form error * 7 */
 70.1057 +	bnexterr = cur1;	/* Process component 1 */
 70.1058 +	delta = cur1 * 2;
 70.1059 +	cur1 += delta;		/* form error * 3 */
 70.1060 +	errorptr[1] = (FSERROR) (bpreverr1 + cur1);
 70.1061 +	cur1 += delta;		/* form error * 5 */
 70.1062 +	bpreverr1 = belowerr1 + cur1;
 70.1063 +	belowerr1 = bnexterr;
 70.1064 +	cur1 += delta;		/* form error * 7 */
 70.1065 +	bnexterr = cur2;	/* Process component 2 */
 70.1066 +	delta = cur2 * 2;
 70.1067 +	cur2 += delta;		/* form error * 3 */
 70.1068 +	errorptr[2] = (FSERROR) (bpreverr2 + cur2);
 70.1069 +	cur2 += delta;		/* form error * 5 */
 70.1070 +	bpreverr2 = belowerr2 + cur2;
 70.1071 +	belowerr2 = bnexterr;
 70.1072 +	cur2 += delta;		/* form error * 7 */
 70.1073 +      }
 70.1074 +      /* At this point curN contains the 7/16 error value to be propagated
 70.1075 +       * to the next pixel on the current line, and all the errors for the
 70.1076 +       * next line have been shifted over.  We are therefore ready to move on.
 70.1077 +       */
 70.1078 +      inptr += dir3;		/* Advance pixel pointers to next column */
 70.1079 +      outptr += dir;
 70.1080 +      errorptr += dir3;		/* advance errorptr to current column */
 70.1081 +    }
 70.1082 +    /* Post-loop cleanup: we must unload the final error values into the
 70.1083 +     * final fserrors[] entry.  Note we need not unload belowerrN because
 70.1084 +     * it is for the dummy column before or after the actual array.
 70.1085 +     */
 70.1086 +    errorptr[0] = (FSERROR) bpreverr0; /* unload prev errs into array */
 70.1087 +    errorptr[1] = (FSERROR) bpreverr1;
 70.1088 +    errorptr[2] = (FSERROR) bpreverr2;
 70.1089 +  }
 70.1090 +}
 70.1091 +
 70.1092 +
 70.1093 +/*
 70.1094 + * Initialize the error-limiting transfer function (lookup table).
 70.1095 + * The raw F-S error computation can potentially compute error values of up to
 70.1096 + * +- MAXJSAMPLE.  But we want the maximum correction applied to a pixel to be
 70.1097 + * much less, otherwise obviously wrong pixels will be created.  (Typical
 70.1098 + * effects include weird fringes at color-area boundaries, isolated bright
 70.1099 + * pixels in a dark area, etc.)  The standard advice for avoiding this problem
 70.1100 + * is to ensure that the "corners" of the color cube are allocated as output
 70.1101 + * colors; then repeated errors in the same direction cannot cause cascading
 70.1102 + * error buildup.  However, that only prevents the error from getting
 70.1103 + * completely out of hand; Aaron Giles reports that error limiting improves
 70.1104 + * the results even with corner colors allocated.
 70.1105 + * A simple clamping of the error values to about +- MAXJSAMPLE/8 works pretty
 70.1106 + * well, but the smoother transfer function used below is even better.  Thanks
 70.1107 + * to Aaron Giles for this idea.
 70.1108 + */
 70.1109 +
 70.1110 +LOCAL(void)
 70.1111 +init_error_limit (j_decompress_ptr cinfo)
 70.1112 +/* Allocate and fill in the error_limiter table */
 70.1113 +{
 70.1114 +  my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
 70.1115 +  int * table;
 70.1116 +  int in, out;
 70.1117 +
 70.1118 +  table = (int *) (*cinfo->mem->alloc_small)
 70.1119 +    ((j_common_ptr) cinfo, JPOOL_IMAGE, (MAXJSAMPLE*2+1) * SIZEOF(int));
 70.1120 +  table += MAXJSAMPLE;		/* so can index -MAXJSAMPLE .. +MAXJSAMPLE */
 70.1121 +  cquantize->error_limiter = table;
 70.1122 +
 70.1123 +#define STEPSIZE ((MAXJSAMPLE+1)/16)
 70.1124 +  /* Map errors 1:1 up to +- MAXJSAMPLE/16 */
 70.1125 +  out = 0;
 70.1126 +  for (in = 0; in < STEPSIZE; in++, out++) {
 70.1127 +    table[in] = out; table[-in] = -out;
 70.1128 +  }
 70.1129 +  /* Map errors 1:2 up to +- 3*MAXJSAMPLE/16 */
 70.1130 +  for (; in < STEPSIZE*3; in++, out += (in&1) ? 0 : 1) {
 70.1131 +    table[in] = out; table[-in] = -out;
 70.1132 +  }
 70.1133 +  /* Clamp the rest to final out value (which is (MAXJSAMPLE+1)/8) */
 70.1134 +  for (; in <= MAXJSAMPLE; in++) {
 70.1135 +    table[in] = out; table[-in] = -out;
 70.1136 +  }
 70.1137 +#undef STEPSIZE
 70.1138 +}
 70.1139 +
 70.1140 +
 70.1141 +/*
 70.1142 + * Finish up at the end of each pass.
 70.1143 + */
 70.1144 +
 70.1145 +METHODDEF(void)
 70.1146 +finish_pass1 (j_decompress_ptr cinfo)
 70.1147 +{
 70.1148 +  my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
 70.1149 +
 70.1150 +  /* Select the representative colors and fill in cinfo->colormap */
 70.1151 +  cinfo->colormap = cquantize->sv_colormap;
 70.1152 +  select_colors(cinfo, cquantize->desired);
 70.1153 +  /* Force next pass to zero the color index table */
 70.1154 +  cquantize->needs_zeroed = TRUE;
 70.1155 +}
 70.1156 +
 70.1157 +
 70.1158 +METHODDEF(void)
 70.1159 +finish_pass2 (j_decompress_ptr cinfo)
 70.1160 +{
 70.1161 +  /* no work */
 70.1162 +}
 70.1163 +
 70.1164 +
 70.1165 +/*
 70.1166 + * Initialize for each processing pass.
 70.1167 + */
 70.1168 +
 70.1169 +METHODDEF(void)
 70.1170 +start_pass_2_quant (j_decompress_ptr cinfo, boolean is_pre_scan)
 70.1171 +{
 70.1172 +  my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
 70.1173 +  hist3d histogram = cquantize->histogram;
 70.1174 +  int i;
 70.1175 +
 70.1176 +  /* Only F-S dithering or no dithering is supported. */
 70.1177 +  /* If user asks for ordered dither, give him F-S. */
 70.1178 +  if (cinfo->dither_mode != JDITHER_NONE)
 70.1179 +    cinfo->dither_mode = JDITHER_FS;
 70.1180 +
 70.1181 +  if (is_pre_scan) {
 70.1182 +    /* Set up method pointers */
 70.1183 +    cquantize->pub.color_quantize = prescan_quantize;
 70.1184 +    cquantize->pub.finish_pass = finish_pass1;
 70.1185 +    cquantize->needs_zeroed = TRUE; /* Always zero histogram */
 70.1186 +  } else {
 70.1187 +    /* Set up method pointers */
 70.1188 +    if (cinfo->dither_mode == JDITHER_FS)
 70.1189 +      cquantize->pub.color_quantize = pass2_fs_dither;
 70.1190 +    else
 70.1191 +      cquantize->pub.color_quantize = pass2_no_dither;
 70.1192 +    cquantize->pub.finish_pass = finish_pass2;
 70.1193 +
 70.1194 +    /* Make sure color count is acceptable */
 70.1195 +    i = cinfo->actual_number_of_colors;
 70.1196 +    if (i < 1)
 70.1197 +      ERREXIT1(cinfo, JERR_QUANT_FEW_COLORS, 1);
 70.1198 +    if (i > MAXNUMCOLORS)
 70.1199 +      ERREXIT1(cinfo, JERR_QUANT_MANY_COLORS, MAXNUMCOLORS);
 70.1200 +
 70.1201 +    if (cinfo->dither_mode == JDITHER_FS) {
 70.1202 +      size_t arraysize = (size_t) ((cinfo->output_width + 2) *
 70.1203 +				   (3 * SIZEOF(FSERROR)));
 70.1204 +      /* Allocate Floyd-Steinberg workspace if we didn't already. */
 70.1205 +      if (cquantize->fserrors == NULL)
 70.1206 +	cquantize->fserrors = (FSERRPTR) (*cinfo->mem->alloc_large)
 70.1207 +	  ((j_common_ptr) cinfo, JPOOL_IMAGE, arraysize);
 70.1208 +      /* Initialize the propagated errors to zero. */
 70.1209 +      jzero_far((void FAR *) cquantize->fserrors, arraysize);
 70.1210 +      /* Make the error-limit table if we didn't already. */
 70.1211 +      if (cquantize->error_limiter == NULL)
 70.1212 +	init_error_limit(cinfo);
 70.1213 +      cquantize->on_odd_row = FALSE;
 70.1214 +    }
 70.1215 +
 70.1216 +  }
 70.1217 +  /* Zero the histogram or inverse color map, if necessary */
 70.1218 +  if (cquantize->needs_zeroed) {
 70.1219 +    for (i = 0; i < HIST_C0_ELEMS; i++) {
 70.1220 +      jzero_far((void FAR *) histogram[i],
 70.1221 +		HIST_C1_ELEMS*HIST_C2_ELEMS * SIZEOF(histcell));
 70.1222 +    }
 70.1223 +    cquantize->needs_zeroed = FALSE;
 70.1224 +  }
 70.1225 +}
 70.1226 +
 70.1227 +
 70.1228 +/*
 70.1229 + * Switch to a new external colormap between output passes.
 70.1230 + */
 70.1231 +
 70.1232 +METHODDEF(void)
 70.1233 +new_color_map_2_quant (j_decompress_ptr cinfo)
 70.1234 +{
 70.1235 +  my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
 70.1236 +
 70.1237 +  /* Reset the inverse color map */
 70.1238 +  cquantize->needs_zeroed = TRUE;
 70.1239 +}
 70.1240 +
 70.1241 +
 70.1242 +/*
 70.1243 + * Module initialization routine for 2-pass color quantization.
 70.1244 + */
 70.1245 +
 70.1246 +GLOBAL(void)
 70.1247 +jinit_2pass_quantizer (j_decompress_ptr cinfo)
 70.1248 +{
 70.1249 +  my_cquantize_ptr cquantize;
 70.1250 +  int i;
 70.1251 +
 70.1252 +  cquantize = (my_cquantize_ptr)
 70.1253 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
 70.1254 +				SIZEOF(my_cquantizer));
 70.1255 +  cinfo->cquantize = (struct jpeg_color_quantizer *) cquantize;
 70.1256 +  cquantize->pub.start_pass = start_pass_2_quant;
 70.1257 +  cquantize->pub.new_color_map = new_color_map_2_quant;
 70.1258 +  cquantize->fserrors = NULL;	/* flag optional arrays not allocated */
 70.1259 +  cquantize->error_limiter = NULL;
 70.1260 +
 70.1261 +  /* Make sure jdmaster didn't give me a case I can't handle */
 70.1262 +  if (cinfo->out_color_components != 3)
 70.1263 +    ERREXIT(cinfo, JERR_NOTIMPL);
 70.1264 +
 70.1265 +  /* Allocate the histogram/inverse colormap storage */
 70.1266 +  cquantize->histogram = (hist3d) (*cinfo->mem->alloc_small)
 70.1267 +    ((j_common_ptr) cinfo, JPOOL_IMAGE, HIST_C0_ELEMS * SIZEOF(hist2d));
 70.1268 +  for (i = 0; i < HIST_C0_ELEMS; i++) {
 70.1269 +    cquantize->histogram[i] = (hist2d) (*cinfo->mem->alloc_large)
 70.1270 +      ((j_common_ptr) cinfo, JPOOL_IMAGE,
 70.1271 +       HIST_C1_ELEMS*HIST_C2_ELEMS * SIZEOF(histcell));
 70.1272 +  }
 70.1273 +  cquantize->needs_zeroed = TRUE; /* histogram is garbage now */
 70.1274 +
 70.1275 +  /* Allocate storage for the completed colormap, if required.
 70.1276 +   * We do this now since it is FAR storage and may affect
 70.1277 +   * the memory manager's space calculations.
 70.1278 +   */
 70.1279 +  if (cinfo->enable_2pass_quant) {
 70.1280 +    /* Make sure color count is acceptable */
 70.1281 +    int desired = cinfo->desired_number_of_colors;
 70.1282 +    /* Lower bound on # of colors ... somewhat arbitrary as long as > 0 */
 70.1283 +    if (desired < 8)
 70.1284 +      ERREXIT1(cinfo, JERR_QUANT_FEW_COLORS, 8);
 70.1285 +    /* Make sure colormap indexes can be represented by JSAMPLEs */
 70.1286 +    if (desired > MAXNUMCOLORS)
 70.1287 +      ERREXIT1(cinfo, JERR_QUANT_MANY_COLORS, MAXNUMCOLORS);
 70.1288 +    cquantize->sv_colormap = (*cinfo->mem->alloc_sarray)
 70.1289 +      ((j_common_ptr) cinfo,JPOOL_IMAGE, (JDIMENSION) desired, (JDIMENSION) 3);
 70.1290 +    cquantize->desired = desired;
 70.1291 +  } else
 70.1292 +    cquantize->sv_colormap = NULL;
 70.1293 +
 70.1294 +  /* Only F-S dithering or no dithering is supported. */
 70.1295 +  /* If user asks for ordered dither, give him F-S. */
 70.1296 +  if (cinfo->dither_mode != JDITHER_NONE)
 70.1297 +    cinfo->dither_mode = JDITHER_FS;
 70.1298 +
 70.1299 +  /* Allocate Floyd-Steinberg workspace if necessary.
 70.1300 +   * This isn't really needed until pass 2, but again it is FAR storage.
 70.1301 +   * Although we will cope with a later change in dither_mode,
 70.1302 +   * we do not promise to honor max_memory_to_use if dither_mode changes.
 70.1303 +   */
 70.1304 +  if (cinfo->dither_mode == JDITHER_FS) {
 70.1305 +    cquantize->fserrors = (FSERRPTR) (*cinfo->mem->alloc_large)
 70.1306 +      ((j_common_ptr) cinfo, JPOOL_IMAGE,
 70.1307 +       (size_t) ((cinfo->output_width + 2) * (3 * SIZEOF(FSERROR))));
 70.1308 +    /* Might as well create the error-limiting table too. */
 70.1309 +    init_error_limit(cinfo);
 70.1310 +  }
 70.1311 +}
 70.1312 +
 70.1313 +#endif /* QUANT_2PASS_SUPPORTED */
    71.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    71.2 +++ b/libs/libjpeg/jutils.c	Sat Sep 19 05:51:51 2015 +0300
    71.3 @@ -0,0 +1,179 @@
    71.4 +/*
    71.5 + * jutils.c
    71.6 + *
    71.7 + * Copyright (C) 1991-1996, Thomas G. Lane.
    71.8 + * This file is part of the Independent JPEG Group's software.
    71.9 + * For conditions of distribution and use, see the accompanying README file.
   71.10 + *
   71.11 + * This file contains tables and miscellaneous utility routines needed
   71.12 + * for both compression and decompression.
   71.13 + * Note we prefix all global names with "j" to minimize conflicts with
   71.14 + * a surrounding application.
   71.15 + */
   71.16 +
   71.17 +#define JPEG_INTERNALS
   71.18 +#include "jinclude.h"
   71.19 +#include "jpeglib.h"
   71.20 +
   71.21 +
   71.22 +/*
   71.23 + * jpeg_zigzag_order[i] is the zigzag-order position of the i'th element
   71.24 + * of a DCT block read in natural order (left to right, top to bottom).
   71.25 + */
   71.26 +
   71.27 +#if 0				/* This table is not actually needed in v6a */
   71.28 +
   71.29 +const int jpeg_zigzag_order[DCTSIZE2] = {
   71.30 +   0,  1,  5,  6, 14, 15, 27, 28,
   71.31 +   2,  4,  7, 13, 16, 26, 29, 42,
   71.32 +   3,  8, 12, 17, 25, 30, 41, 43,
   71.33 +   9, 11, 18, 24, 31, 40, 44, 53,
   71.34 +  10, 19, 23, 32, 39, 45, 52, 54,
   71.35 +  20, 22, 33, 38, 46, 51, 55, 60,
   71.36 +  21, 34, 37, 47, 50, 56, 59, 61,
   71.37 +  35, 36, 48, 49, 57, 58, 62, 63
   71.38 +};
   71.39 +
   71.40 +#endif
   71.41 +
   71.42 +/*
   71.43 + * jpeg_natural_order[i] is the natural-order position of the i'th element
   71.44 + * of zigzag order.
   71.45 + *
   71.46 + * When reading corrupted data, the Huffman decoders could attempt
   71.47 + * to reference an entry beyond the end of this array (if the decoded
   71.48 + * zero run length reaches past the end of the block).  To prevent
   71.49 + * wild stores without adding an inner-loop test, we put some extra
   71.50 + * "63"s after the real entries.  This will cause the extra coefficient
   71.51 + * to be stored in location 63 of the block, not somewhere random.
   71.52 + * The worst case would be a run-length of 15, which means we need 16
   71.53 + * fake entries.
   71.54 + */
   71.55 +
   71.56 +const int jpeg_natural_order[DCTSIZE2+16] = {
   71.57 +  0,  1,  8, 16,  9,  2,  3, 10,
   71.58 + 17, 24, 32, 25, 18, 11,  4,  5,
   71.59 + 12, 19, 26, 33, 40, 48, 41, 34,
   71.60 + 27, 20, 13,  6,  7, 14, 21, 28,
   71.61 + 35, 42, 49, 56, 57, 50, 43, 36,
   71.62 + 29, 22, 15, 23, 30, 37, 44, 51,
   71.63 + 58, 59, 52, 45, 38, 31, 39, 46,
   71.64 + 53, 60, 61, 54, 47, 55, 62, 63,
   71.65 + 63, 63, 63, 63, 63, 63, 63, 63, /* extra entries for safety in decoder */
   71.66 + 63, 63, 63, 63, 63, 63, 63, 63
   71.67 +};
   71.68 +
   71.69 +
   71.70 +/*
   71.71 + * Arithmetic utilities
   71.72 + */
   71.73 +
   71.74 +GLOBAL(long)
   71.75 +jdiv_round_up (long a, long b)
   71.76 +/* Compute a/b rounded up to next integer, ie, ceil(a/b) */
   71.77 +/* Assumes a >= 0, b > 0 */
   71.78 +{
   71.79 +  return (a + b - 1L) / b;
   71.80 +}
   71.81 +
   71.82 +
   71.83 +GLOBAL(long)
   71.84 +jround_up (long a, long b)
   71.85 +/* Compute a rounded up to next multiple of b, ie, ceil(a/b)*b */
   71.86 +/* Assumes a >= 0, b > 0 */
   71.87 +{
   71.88 +  a += b - 1L;
   71.89 +  return a - (a % b);
   71.90 +}
   71.91 +
   71.92 +
   71.93 +/* On normal machines we can apply MEMCOPY() and MEMZERO() to sample arrays
   71.94 + * and coefficient-block arrays.  This won't work on 80x86 because the arrays
   71.95 + * are FAR and we're assuming a small-pointer memory model.  However, some
   71.96 + * DOS compilers provide far-pointer versions of memcpy() and memset() even
   71.97 + * in the small-model libraries.  These will be used if USE_FMEM is defined.
   71.98 + * Otherwise, the routines below do it the hard way.  (The performance cost
   71.99 + * is not all that great, because these routines aren't very heavily used.)
  71.100 + */
  71.101 +
  71.102 +#ifndef NEED_FAR_POINTERS	/* normal case, same as regular macros */
  71.103 +#define FMEMCOPY(dest,src,size)	MEMCOPY(dest,src,size)
  71.104 +#define FMEMZERO(target,size)	MEMZERO(target,size)
  71.105 +#else				/* 80x86 case, define if we can */
  71.106 +#ifdef USE_FMEM
  71.107 +#define FMEMCOPY(dest,src,size)	_fmemcpy((void FAR *)(dest), (const void FAR *)(src), (size_t)(size))
  71.108 +#define FMEMZERO(target,size)	_fmemset((void FAR *)(target), 0, (size_t)(size))
  71.109 +#endif
  71.110 +#endif
  71.111 +
  71.112 +
  71.113 +GLOBAL(void)
  71.114 +jcopy_sample_rows (JSAMPARRAY input_array, int source_row,
  71.115 +		   JSAMPARRAY output_array, int dest_row,
  71.116 +		   int num_rows, JDIMENSION num_cols)
  71.117 +/* Copy some rows of samples from one place to another.
  71.118 + * num_rows rows are copied from input_array[source_row++]
  71.119 + * to output_array[dest_row++]; these areas may overlap for duplication.
  71.120 + * The source and destination arrays must be at least as wide as num_cols.
  71.121 + */
  71.122 +{
  71.123 +  register JSAMPROW inptr, outptr;
  71.124 +#ifdef FMEMCOPY
  71.125 +  register size_t count = (size_t) (num_cols * SIZEOF(JSAMPLE));
  71.126 +#else
  71.127 +  register JDIMENSION count;
  71.128 +#endif
  71.129 +  register int row;
  71.130 +
  71.131 +  input_array += source_row;
  71.132 +  output_array += dest_row;
  71.133 +
  71.134 +  for (row = num_rows; row > 0; row--) {
  71.135 +    inptr = *input_array++;
  71.136 +    outptr = *output_array++;
  71.137 +#ifdef FMEMCOPY
  71.138 +    FMEMCOPY(outptr, inptr, count);
  71.139 +#else
  71.140 +    for (count = num_cols; count > 0; count--)
  71.141 +      *outptr++ = *inptr++;	/* needn't bother with GETJSAMPLE() here */
  71.142 +#endif
  71.143 +  }
  71.144 +}
  71.145 +
  71.146 +
  71.147 +GLOBAL(void)
  71.148 +jcopy_block_row (JBLOCKROW input_row, JBLOCKROW output_row,
  71.149 +		 JDIMENSION num_blocks)
  71.150 +/* Copy a row of coefficient blocks from one place to another. */
  71.151 +{
  71.152 +#ifdef FMEMCOPY
  71.153 +  FMEMCOPY(output_row, input_row, num_blocks * (DCTSIZE2 * SIZEOF(JCOEF)));
  71.154 +#else
  71.155 +  register JCOEFPTR inptr, outptr;
  71.156 +  register long count;
  71.157 +
  71.158 +  inptr = (JCOEFPTR) input_row;
  71.159 +  outptr = (JCOEFPTR) output_row;
  71.160 +  for (count = (long) num_blocks * DCTSIZE2; count > 0; count--) {
  71.161 +    *outptr++ = *inptr++;
  71.162 +  }
  71.163 +#endif
  71.164 +}
  71.165 +
  71.166 +
  71.167 +GLOBAL(void)
  71.168 +jzero_far (void FAR * target, size_t bytestozero)
  71.169 +/* Zero out a chunk of FAR memory. */
  71.170 +/* This might be sample-array data, block-array data, or alloc_large data. */
  71.171 +{
  71.172 +#ifdef FMEMZERO
  71.173 +  FMEMZERO(target, bytestozero);
  71.174 +#else
  71.175 +  register char FAR * ptr = (char FAR *) target;
  71.176 +  register size_t count;
  71.177 +
  71.178 +  for (count = bytestozero; count > 0; count--) {
  71.179 +    *ptr++ = 0;
  71.180 +  }
  71.181 +#endif
  71.182 +}
    72.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    72.2 +++ b/libs/libjpeg/jversion.h	Sat Sep 19 05:51:51 2015 +0300
    72.3 @@ -0,0 +1,14 @@
    72.4 +/*
    72.5 + * jversion.h
    72.6 + *
    72.7 + * Copyright (C) 1991-1998, Thomas G. Lane.
    72.8 + * This file is part of the Independent JPEG Group's software.
    72.9 + * For conditions of distribution and use, see the accompanying README file.
   72.10 + *
   72.11 + * This file contains software version identification.
   72.12 + */
   72.13 +
   72.14 +
   72.15 +#define JVERSION	"6b  27-Mar-1998"
   72.16 +
   72.17 +#define JCOPYRIGHT	"Copyright (C) 1998, Thomas G. Lane"
    73.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    73.2 +++ b/libs/libpng/LICENSE	Sat Sep 19 05:51:51 2015 +0300
    73.3 @@ -0,0 +1,111 @@
    73.4 +
    73.5 +This copy of the libpng notices is provided for your convenience.  In case of
    73.6 +any discrepancy between this copy and the notices in the file png.h that is
    73.7 +included in the libpng distribution, the latter shall prevail.
    73.8 +
    73.9 +COPYRIGHT NOTICE, DISCLAIMER, and LICENSE:
   73.10 +
   73.11 +If you modify libpng you may insert additional notices immediately following
   73.12 +this sentence.
   73.13 +
   73.14 +This code is released under the libpng license.
   73.15 +
   73.16 +libpng versions 1.2.6, August 15, 2004, through 1.5.4, July 7, 2011, are
   73.17 +Copyright (c) 2004, 2006-2011 Glenn Randers-Pehrson, and are
   73.18 +distributed according to the same disclaimer and license as libpng-1.2.5
   73.19 +with the following individual added to the list of Contributing Authors
   73.20 +
   73.21 +   Cosmin Truta
   73.22 +
   73.23 +libpng versions 1.0.7, July 1, 2000, through 1.2.5 - October 3, 2002, are
   73.24 +Copyright (c) 2000-2002 Glenn Randers-Pehrson, and are
   73.25 +distributed according to the same disclaimer and license as libpng-1.0.6
   73.26 +with the following individuals added to the list of Contributing Authors
   73.27 +
   73.28 +   Simon-Pierre Cadieux
   73.29 +   Eric S. Raymond
   73.30 +   Gilles Vollant
   73.31 +
   73.32 +and with the following additions to the disclaimer:
   73.33 +
   73.34 +   There is no warranty against interference with your enjoyment of the
   73.35 +   library or against infringement.  There is no warranty that our
   73.36 +   efforts or the library will fulfill any of your particular purposes
   73.37 +   or needs.  This library is provided with all faults, and the entire
   73.38 +   risk of satisfactory quality, performance, accuracy, and effort is with
   73.39 +   the user.
   73.40 +
   73.41 +libpng versions 0.97, January 1998, through 1.0.6, March 20, 2000, are
   73.42 +Copyright (c) 1998, 1999 Glenn Randers-Pehrson, and are
   73.43 +distributed according to the same disclaimer and license as libpng-0.96,
   73.44 +with the following individuals added to the list of Contributing Authors:
   73.45 +
   73.46 +   Tom Lane
   73.47 +   Glenn Randers-Pehrson
   73.48 +   Willem van Schaik
   73.49 +
   73.50 +libpng versions 0.89, June 1996, through 0.96, May 1997, are
   73.51 +Copyright (c) 1996, 1997 Andreas Dilger
   73.52 +Distributed according to the same disclaimer and license as libpng-0.88,
   73.53 +with the following individuals added to the list of Contributing Authors:
   73.54 +
   73.55 +   John Bowler
   73.56 +   Kevin Bracey
   73.57 +   Sam Bushell
   73.58 +   Magnus Holmgren
   73.59 +   Greg Roelofs
   73.60 +   Tom Tanner
   73.61 +
   73.62 +libpng versions 0.5, May 1995, through 0.88, January 1996, are
   73.63 +Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
   73.64 +
   73.65 +For the purposes of this copyright and license, "Contributing Authors"
   73.66 +is defined as the following set of individuals:
   73.67 +
   73.68 +   Andreas Dilger
   73.69 +   Dave Martindale
   73.70 +   Guy Eric Schalnat
   73.71 +   Paul Schmidt
   73.72 +   Tim Wegner
   73.73 +
   73.74 +The PNG Reference Library is supplied "AS IS".  The Contributing Authors
   73.75 +and Group 42, Inc. disclaim all warranties, expressed or implied,
   73.76 +including, without limitation, the warranties of merchantability and of
   73.77 +fitness for any purpose.  The Contributing Authors and Group 42, Inc.
   73.78 +assume no liability for direct, indirect, incidental, special, exemplary,
   73.79 +or consequential damages, which may result from the use of the PNG
   73.80 +Reference Library, even if advised of the possibility of such damage.
   73.81 +
   73.82 +Permission is hereby granted to use, copy, modify, and distribute this
   73.83 +source code, or portions hereof, for any purpose, without fee, subject
   73.84 +to the following restrictions:
   73.85 +
   73.86 +1. The origin of this source code must not be misrepresented.
   73.87 +
   73.88 +2. Altered versions must be plainly marked as such and must not
   73.89 +   be misrepresented as being the original source.
   73.90 +
   73.91 +3. This Copyright notice may not be removed or altered from any
   73.92 +   source or altered source distribution.
   73.93 +
   73.94 +The Contributing Authors and Group 42, Inc. specifically permit, without
   73.95 +fee, and encourage the use of this source code as a component to
   73.96 +supporting the PNG file format in commercial products.  If you use this
   73.97 +source code in a product, acknowledgment is not required but would be
   73.98 +appreciated.
   73.99 +
  73.100 +
  73.101 +A "png_get_copyright" function is available, for convenient use in "about"
  73.102 +boxes and the like:
  73.103 +
  73.104 +   printf("%s",png_get_copyright(NULL));
  73.105 +
  73.106 +Also, the PNG logo (in PNG format, of course) is supplied in the
  73.107 +files "pngbar.png" and "pngbar.jpg (88x31) and "pngnow.png" (98x31).
  73.108 +
  73.109 +Libpng is OSI Certified Open Source Software.  OSI Certified Open Source is a
  73.110 +certification mark of the Open Source Initiative.
  73.111 +
  73.112 +Glenn Randers-Pehrson
  73.113 +glennrp at users.sourceforge.net
  73.114 +July 7, 2011
    74.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    74.2 +++ b/libs/libpng/png.c	Sat Sep 19 05:51:51 2015 +0300
    74.3 @@ -0,0 +1,799 @@
    74.4 +
    74.5 +/* png.c - location for general purpose libpng functions
    74.6 + *
    74.7 + * Last changed in libpng 1.2.30 [August 15, 2008]
    74.8 + * For conditions of distribution and use, see copyright notice in png.h
    74.9 + * Copyright (c) 1998-2008 Glenn Randers-Pehrson
   74.10 + * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
   74.11 + * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
   74.12 + */
   74.13 +
   74.14 +#define PNG_INTERNAL
   74.15 +#define PNG_NO_EXTERN
   74.16 +#include "png.h"
   74.17 +
   74.18 +/* Generate a compiler error if there is an old png.h in the search path. */
   74.19 +typedef version_1_2_33 Your_png_h_is_not_version_1_2_33;
   74.20 +
   74.21 +/* Version information for C files.  This had better match the version
   74.22 + * string defined in png.h.  */
   74.23 +
   74.24 +#ifdef PNG_USE_GLOBAL_ARRAYS
   74.25 +/* png_libpng_ver was changed to a function in version 1.0.5c */
   74.26 +PNG_CONST char png_libpng_ver[18] = PNG_LIBPNG_VER_STRING;
   74.27 +
   74.28 +#ifdef PNG_READ_SUPPORTED
   74.29 +
   74.30 +/* png_sig was changed to a function in version 1.0.5c */
   74.31 +/* Place to hold the signature string for a PNG file. */
   74.32 +PNG_CONST png_byte FARDATA png_sig[8] = {137, 80, 78, 71, 13, 10, 26, 10};
   74.33 +#endif /* PNG_READ_SUPPORTED */
   74.34 +
   74.35 +/* Invoke global declarations for constant strings for known chunk types */
   74.36 +PNG_IHDR;
   74.37 +PNG_IDAT;
   74.38 +PNG_IEND;
   74.39 +PNG_PLTE;
   74.40 +PNG_bKGD;
   74.41 +PNG_cHRM;
   74.42 +PNG_gAMA;
   74.43 +PNG_hIST;
   74.44 +PNG_iCCP;
   74.45 +PNG_iTXt;
   74.46 +PNG_oFFs;
   74.47 +PNG_pCAL;
   74.48 +PNG_sCAL;
   74.49 +PNG_pHYs;
   74.50 +PNG_sBIT;
   74.51 +PNG_sPLT;
   74.52 +PNG_sRGB;
   74.53 +PNG_tEXt;
   74.54 +PNG_tIME;
   74.55 +PNG_tRNS;
   74.56 +PNG_zTXt;
   74.57 +
   74.58 +#ifdef PNG_READ_SUPPORTED
   74.59 +/* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
   74.60 +
   74.61 +/* start of interlace block */
   74.62 +PNG_CONST int FARDATA png_pass_start[] = {0, 4, 0, 2, 0, 1, 0};
   74.63 +
   74.64 +/* offset to next interlace block */
   74.65 +PNG_CONST int FARDATA png_pass_inc[] = {8, 8, 4, 4, 2, 2, 1};
   74.66 +
   74.67 +/* start of interlace block in the y direction */
   74.68 +PNG_CONST int FARDATA png_pass_ystart[] = {0, 0, 4, 0, 2, 0, 1};
   74.69 +
   74.70 +/* offset to next interlace block in the y direction */
   74.71 +PNG_CONST int FARDATA png_pass_yinc[] = {8, 8, 8, 4, 4, 2, 2};
   74.72 +
   74.73 +/* Height of interlace block.  This is not currently used - if you need
   74.74 + * it, uncomment it here and in png.h
   74.75 +PNG_CONST int FARDATA png_pass_height[] = {8, 8, 4, 4, 2, 2, 1};
   74.76 +*/
   74.77 +
   74.78 +/* Mask to determine which pixels are valid in a pass */
   74.79 +PNG_CONST int FARDATA png_pass_mask[] = {0x80, 0x08, 0x88, 0x22, 0xaa, 0x55, 0xff};
   74.80 +
   74.81 +/* Mask to determine which pixels to overwrite while displaying */
   74.82 +PNG_CONST int FARDATA png_pass_dsp_mask[]
   74.83 +   = {0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff};
   74.84 +
   74.85 +#endif /* PNG_READ_SUPPORTED */
   74.86 +#endif /* PNG_USE_GLOBAL_ARRAYS */
   74.87 +
   74.88 +/* Tells libpng that we have already handled the first "num_bytes" bytes
   74.89 + * of the PNG file signature.  If the PNG data is embedded into another
   74.90 + * stream we can set num_bytes = 8 so that libpng will not attempt to read
   74.91 + * or write any of the magic bytes before it starts on the IHDR.
   74.92 + */
   74.93 +
   74.94 +#ifdef PNG_READ_SUPPORTED
   74.95 +void PNGAPI
   74.96 +png_set_sig_bytes(png_structp png_ptr, int num_bytes)
   74.97 +{
   74.98 +   if (png_ptr == NULL) return;
   74.99 +   png_debug(1, "in png_set_sig_bytes\n");
  74.100 +   if (num_bytes > 8)
  74.101 +      png_error(png_ptr, "Too many bytes for PNG signature.");
  74.102 +
  74.103 +   png_ptr->sig_bytes = (png_byte)(num_bytes < 0 ? 0 : num_bytes);
  74.104 +}
  74.105 +
  74.106 +/* Checks whether the supplied bytes match the PNG signature.  We allow
  74.107 + * checking less than the full 8-byte signature so that those apps that
  74.108 + * already read the first few bytes of a file to determine the file type
  74.109 + * can simply check the remaining bytes for extra assurance.  Returns
  74.110 + * an integer less than, equal to, or greater than zero if sig is found,
  74.111 + * respectively, to be less than, to match, or be greater than the correct
  74.112 + * PNG signature (this is the same behaviour as strcmp, memcmp, etc).
  74.113 + */
  74.114 +int PNGAPI
  74.115 +png_sig_cmp(png_bytep sig, png_size_t start, png_size_t num_to_check)
  74.116 +{
  74.117 +   png_byte png_signature[8] = {137, 80, 78, 71, 13, 10, 26, 10};
  74.118 +   if (num_to_check > 8)
  74.119 +      num_to_check = 8;
  74.120 +   else if (num_to_check < 1)
  74.121 +      return (-1);
  74.122 +
  74.123 +   if (start > 7)
  74.124 +      return (-1);
  74.125 +
  74.126 +   if (start + num_to_check > 8)
  74.127 +      num_to_check = 8 - start;
  74.128 +
  74.129 +   return ((int)(png_memcmp(&sig[start], &png_signature[start], num_to_check)));
  74.130 +}
  74.131 +
  74.132 +#if defined(PNG_1_0_X) || defined(PNG_1_2_X)
  74.133 +/* (Obsolete) function to check signature bytes.  It does not allow one
  74.134 + * to check a partial signature.  This function might be removed in the
  74.135 + * future - use png_sig_cmp().  Returns true (nonzero) if the file is PNG.
  74.136 + */
  74.137 +int PNGAPI
  74.138 +png_check_sig(png_bytep sig, int num)
  74.139 +{
  74.140 +  return ((int)!png_sig_cmp(sig, (png_size_t)0, (png_size_t)num));
  74.141 +}
  74.142 +#endif
  74.143 +#endif /* PNG_READ_SUPPORTED */
  74.144 +
  74.145 +#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
  74.146 +/* Function to allocate memory for zlib and clear it to 0. */
  74.147 +#ifdef PNG_1_0_X
  74.148 +voidpf PNGAPI
  74.149 +#else
  74.150 +voidpf /* private */
  74.151 +#endif
  74.152 +png_zalloc(voidpf png_ptr, uInt items, uInt size)
  74.153 +{
  74.154 +   png_voidp ptr;
  74.155 +   png_structp p=(png_structp)png_ptr;
  74.156 +   png_uint_32 save_flags=p->flags;
  74.157 +   png_uint_32 num_bytes;
  74.158 +
  74.159 +   if (png_ptr == NULL) return (NULL);
  74.160 +   if (items > PNG_UINT_32_MAX/size)
  74.161 +   {
  74.162 +     png_warning (p, "Potential overflow in png_zalloc()");
  74.163 +     return (NULL);
  74.164 +   }
  74.165 +   num_bytes = (png_uint_32)items * size;
  74.166 +
  74.167 +   p->flags|=PNG_FLAG_MALLOC_NULL_MEM_OK;
  74.168 +   ptr = (png_voidp)png_malloc((png_structp)png_ptr, num_bytes);
  74.169 +   p->flags=save_flags;
  74.170 +
  74.171 +#if defined(PNG_1_0_X) && !defined(PNG_NO_ZALLOC_ZERO)
  74.172 +   if (ptr == NULL)
  74.173 +       return ((voidpf)ptr);
  74.174 +
  74.175 +   if (num_bytes > (png_uint_32)0x8000L)
  74.176 +   {
  74.177 +      png_memset(ptr, 0, (png_size_t)0x8000L);
  74.178 +      png_memset((png_bytep)ptr + (png_size_t)0x8000L, 0,
  74.179 +         (png_size_t)(num_bytes - (png_uint_32)0x8000L));
  74.180 +   }
  74.181 +   else
  74.182 +   {
  74.183 +      png_memset(ptr, 0, (png_size_t)num_bytes);
  74.184 +   }
  74.185 +#endif
  74.186 +   return ((voidpf)ptr);
  74.187 +}
  74.188 +
  74.189 +/* function to free memory for zlib */
  74.190 +#ifdef PNG_1_0_X
  74.191 +void PNGAPI
  74.192 +#else
  74.193 +void /* private */
  74.194 +#endif
  74.195 +png_zfree(voidpf png_ptr, voidpf ptr)
  74.196 +{
  74.197 +   png_free((png_structp)png_ptr, (png_voidp)ptr);
  74.198 +}
  74.199 +
  74.200 +/* Reset the CRC variable to 32 bits of 1's.  Care must be taken
  74.201 + * in case CRC is > 32 bits to leave the top bits 0.
  74.202 + */
  74.203 +void /* PRIVATE */
  74.204 +png_reset_crc(png_structp png_ptr)
  74.205 +{
  74.206 +   png_ptr->crc = crc32(0, Z_NULL, 0);
  74.207 +}
  74.208 +
  74.209 +/* Calculate the CRC over a section of data.  We can only pass as
  74.210 + * much data to this routine as the largest single buffer size.  We
  74.211 + * also check that this data will actually be used before going to the
  74.212 + * trouble of calculating it.
  74.213 + */
  74.214 +void /* PRIVATE */
  74.215 +png_calculate_crc(png_structp png_ptr, png_bytep ptr, png_size_t length)
  74.216 +{
  74.217 +   int need_crc = 1;
  74.218 +
  74.219 +   if (png_ptr->chunk_name[0] & 0x20)                     /* ancillary */
  74.220 +   {
  74.221 +      if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_MASK) ==
  74.222 +          (PNG_FLAG_CRC_ANCILLARY_USE | PNG_FLAG_CRC_ANCILLARY_NOWARN))
  74.223 +         need_crc = 0;
  74.224 +   }
  74.225 +   else                                                    /* critical */
  74.226 +   {
  74.227 +      if (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_IGNORE)
  74.228 +         need_crc = 0;
  74.229 +   }
  74.230 +
  74.231 +   if (need_crc)
  74.232 +      png_ptr->crc = crc32(png_ptr->crc, ptr, (uInt)length);
  74.233 +}
  74.234 +
  74.235 +/* Allocate the memory for an info_struct for the application.  We don't
  74.236 + * really need the png_ptr, but it could potentially be useful in the
  74.237 + * future.  This should be used in favour of malloc(png_sizeof(png_info))
  74.238 + * and png_info_init() so that applications that want to use a shared
  74.239 + * libpng don't have to be recompiled if png_info changes size.
  74.240 + */
  74.241 +png_infop PNGAPI
  74.242 +png_create_info_struct(png_structp png_ptr)
  74.243 +{
  74.244 +   png_infop info_ptr;
  74.245 +
  74.246 +   png_debug(1, "in png_create_info_struct\n");
  74.247 +   if (png_ptr == NULL) return (NULL);
  74.248 +#ifdef PNG_USER_MEM_SUPPORTED
  74.249 +   info_ptr = (png_infop)png_create_struct_2(PNG_STRUCT_INFO,
  74.250 +      png_ptr->malloc_fn, png_ptr->mem_ptr);
  74.251 +#else
  74.252 +   info_ptr = (png_infop)png_create_struct(PNG_STRUCT_INFO);
  74.253 +#endif
  74.254 +   if (info_ptr != NULL)
  74.255 +      png_info_init_3(&info_ptr, png_sizeof(png_info));
  74.256 +
  74.257 +   return (info_ptr);
  74.258 +}
  74.259 +
  74.260 +/* This function frees the memory associated with a single info struct.
  74.261 + * Normally, one would use either png_destroy_read_struct() or
  74.262 + * png_destroy_write_struct() to free an info struct, but this may be
  74.263 + * useful for some applications.
  74.264 + */
  74.265 +void PNGAPI
  74.266 +png_destroy_info_struct(png_structp png_ptr, png_infopp info_ptr_ptr)
  74.267 +{
  74.268 +   png_infop info_ptr = NULL;
  74.269 +   if (png_ptr == NULL) return;
  74.270 +
  74.271 +   png_debug(1, "in png_destroy_info_struct\n");
  74.272 +   if (info_ptr_ptr != NULL)
  74.273 +      info_ptr = *info_ptr_ptr;
  74.274 +
  74.275 +   if (info_ptr != NULL)
  74.276 +   {
  74.277 +      png_info_destroy(png_ptr, info_ptr);
  74.278 +
  74.279 +#ifdef PNG_USER_MEM_SUPPORTED
  74.280 +      png_destroy_struct_2((png_voidp)info_ptr, png_ptr->free_fn,
  74.281 +          png_ptr->mem_ptr);
  74.282 +#else
  74.283 +      png_destroy_struct((png_voidp)info_ptr);
  74.284 +#endif
  74.285 +      *info_ptr_ptr = NULL;
  74.286 +   }
  74.287 +}
  74.288 +
  74.289 +/* Initialize the info structure.  This is now an internal function (0.89)
  74.290 + * and applications using it are urged to use png_create_info_struct()
  74.291 + * instead.
  74.292 + */
  74.293 +#if defined(PNG_1_0_X) || defined(PNG_1_2_X)
  74.294 +#undef png_info_init
  74.295 +void PNGAPI
  74.296 +png_info_init(png_infop info_ptr)
  74.297 +{
  74.298 +   /* We only come here via pre-1.0.12-compiled applications */
  74.299 +   png_info_init_3(&info_ptr, 0);
  74.300 +}
  74.301 +#endif
  74.302 +
  74.303 +void PNGAPI
  74.304 +png_info_init_3(png_infopp ptr_ptr, png_size_t png_info_struct_size)
  74.305 +{
  74.306 +   png_infop info_ptr = *ptr_ptr;
  74.307 +
  74.308 +   if (info_ptr == NULL) return;
  74.309 +
  74.310 +   png_debug(1, "in png_info_init_3\n");
  74.311 +
  74.312 +   if (png_sizeof(png_info) > png_info_struct_size)
  74.313 +     {
  74.314 +       png_destroy_struct(info_ptr);
  74.315 +       info_ptr = (png_infop)png_create_struct(PNG_STRUCT_INFO);
  74.316 +       *ptr_ptr = info_ptr;
  74.317 +     }
  74.318 +
  74.319 +   /* set everything to 0 */
  74.320 +   png_memset(info_ptr, 0, png_sizeof(png_info));
  74.321 +}
  74.322 +
  74.323 +#ifdef PNG_FREE_ME_SUPPORTED
  74.324 +void PNGAPI
  74.325 +png_data_freer(png_structp png_ptr, png_infop info_ptr,
  74.326 +   int freer, png_uint_32 mask)
  74.327 +{
  74.328 +   png_debug(1, "in png_data_freer\n");
  74.329 +   if (png_ptr == NULL || info_ptr == NULL)
  74.330 +      return;
  74.331 +   if (freer == PNG_DESTROY_WILL_FREE_DATA)
  74.332 +      info_ptr->free_me |= mask;
  74.333 +   else if (freer == PNG_USER_WILL_FREE_DATA)
  74.334 +      info_ptr->free_me &= ~mask;
  74.335 +   else
  74.336 +      png_warning(png_ptr,
  74.337 +         "Unknown freer parameter in png_data_freer.");
  74.338 +}
  74.339 +#endif
  74.340 +
  74.341 +void PNGAPI
  74.342 +png_free_data(png_structp png_ptr, png_infop info_ptr, png_uint_32 mask,
  74.343 +   int num)
  74.344 +{
  74.345 +   png_debug(1, "in png_free_data\n");
  74.346 +   if (png_ptr == NULL || info_ptr == NULL)
  74.347 +      return;
  74.348 +
  74.349 +#if defined(PNG_TEXT_SUPPORTED)
  74.350 +/* free text item num or (if num == -1) all text items */
  74.351 +#ifdef PNG_FREE_ME_SUPPORTED
  74.352 +if ((mask & PNG_FREE_TEXT) & info_ptr->free_me)
  74.353 +#else
  74.354 +if (mask & PNG_FREE_TEXT)
  74.355 +#endif
  74.356 +{
  74.357 +   if (num != -1)
  74.358 +   {
  74.359 +     if (info_ptr->text && info_ptr->text[num].key)
  74.360 +     {
  74.361 +         png_free(png_ptr, info_ptr->text[num].key);
  74.362 +         info_ptr->text[num].key = NULL;
  74.363 +     }
  74.364 +   }
  74.365 +   else
  74.366 +   {
  74.367 +       int i;
  74.368 +       for (i = 0; i < info_ptr->num_text; i++)
  74.369 +           png_free_data(png_ptr, info_ptr, PNG_FREE_TEXT, i);
  74.370 +       png_free(png_ptr, info_ptr->text);
  74.371 +       info_ptr->text = NULL;
  74.372 +       info_ptr->num_text=0;
  74.373 +   }
  74.374 +}
  74.375 +#endif
  74.376 +
  74.377 +#if defined(PNG_tRNS_SUPPORTED)
  74.378 +/* free any tRNS entry */
  74.379 +#ifdef PNG_FREE_ME_SUPPORTED
  74.380 +if ((mask & PNG_FREE_TRNS) & info_ptr->free_me)
  74.381 +#else
  74.382 +if ((mask & PNG_FREE_TRNS) && (png_ptr->flags & PNG_FLAG_FREE_TRNS))
  74.383 +#endif
  74.384 +{
  74.385 +    png_free(png_ptr, info_ptr->trans);
  74.386 +    info_ptr->trans = NULL;
  74.387 +    info_ptr->valid &= ~PNG_INFO_tRNS;
  74.388 +#ifndef PNG_FREE_ME_SUPPORTED
  74.389 +    png_ptr->flags &= ~PNG_FLAG_FREE_TRNS;
  74.390 +#endif
  74.391 +}
  74.392 +#endif
  74.393 +
  74.394 +#if defined(PNG_sCAL_SUPPORTED)
  74.395 +/* free any sCAL entry */
  74.396 +#ifdef PNG_FREE_ME_SUPPORTED
  74.397 +if ((mask & PNG_FREE_SCAL) & info_ptr->free_me)
  74.398 +#else
  74.399 +if (mask & PNG_FREE_SCAL)
  74.400 +#endif
  74.401 +{
  74.402 +#if defined(PNG_FIXED_POINT_SUPPORTED) && !defined(PNG_FLOATING_POINT_SUPPORTED)
  74.403 +    png_free(png_ptr, info_ptr->scal_s_width);
  74.404 +    png_free(png_ptr, info_ptr->scal_s_height);
  74.405 +    info_ptr->scal_s_width = NULL;
  74.406 +    info_ptr->scal_s_height = NULL;
  74.407 +#endif
  74.408 +    info_ptr->valid &= ~PNG_INFO_sCAL;
  74.409 +}
  74.410 +#endif
  74.411 +
  74.412 +#if defined(PNG_pCAL_SUPPORTED)
  74.413 +/* free any pCAL entry */
  74.414 +#ifdef PNG_FREE_ME_SUPPORTED
  74.415 +if ((mask & PNG_FREE_PCAL) & info_ptr->free_me)
  74.416 +#else
  74.417 +if (mask & PNG_FREE_PCAL)
  74.418 +#endif
  74.419 +{
  74.420 +    png_free(png_ptr, info_ptr->pcal_purpose);
  74.421 +    png_free(png_ptr, info_ptr->pcal_units);
  74.422 +    info_ptr->pcal_purpose = NULL;
  74.423 +    info_ptr->pcal_units = NULL;
  74.424 +    if (info_ptr->pcal_params != NULL)
  74.425 +    {
  74.426 +        int i;
  74.427 +        for (i = 0; i < (int)info_ptr->pcal_nparams; i++)
  74.428 +        {
  74.429 +          png_free(png_ptr, info_ptr->pcal_params[i]);
  74.430 +          info_ptr->pcal_params[i]=NULL;
  74.431 +        }
  74.432 +        png_free(png_ptr, info_ptr->pcal_params);
  74.433 +        info_ptr->pcal_params = NULL;
  74.434 +    }
  74.435 +    info_ptr->valid &= ~PNG_INFO_pCAL;
  74.436 +}
  74.437 +#endif
  74.438 +
  74.439 +#if defined(PNG_iCCP_SUPPORTED)
  74.440 +/* free any iCCP entry */
  74.441 +#ifdef PNG_FREE_ME_SUPPORTED
  74.442 +if ((mask & PNG_FREE_ICCP) & info_ptr->free_me)
  74.443 +#else
  74.444 +if (mask & PNG_FREE_ICCP)
  74.445 +#endif
  74.446 +{
  74.447 +    png_free(png_ptr, info_ptr->iccp_name);
  74.448 +    png_free(png_ptr, info_ptr->iccp_profile);
  74.449 +    info_ptr->iccp_name = NULL;
  74.450 +    info_ptr->iccp_profile = NULL;
  74.451 +    info_ptr->valid &= ~PNG_INFO_iCCP;
  74.452 +}
  74.453 +#endif
  74.454 +
  74.455 +#if defined(PNG_sPLT_SUPPORTED)
  74.456 +/* free a given sPLT entry, or (if num == -1) all sPLT entries */
  74.457 +#ifdef PNG_FREE_ME_SUPPORTED
  74.458 +if ((mask & PNG_FREE_SPLT) & info_ptr->free_me)
  74.459 +#else
  74.460 +if (mask & PNG_FREE_SPLT)
  74.461 +#endif
  74.462 +{
  74.463 +   if (num != -1)
  74.464 +   {
  74.465 +      if (info_ptr->splt_palettes)
  74.466 +      {
  74.467 +          png_free(png_ptr, info_ptr->splt_palettes[num].name);
  74.468 +          png_free(png_ptr, info_ptr->splt_palettes[num].entries);
  74.469 +          info_ptr->splt_palettes[num].name = NULL;
  74.470 +          info_ptr->splt_palettes[num].entries = NULL;
  74.471 +      }
  74.472 +   }
  74.473 +   else
  74.474 +   {
  74.475 +       if (info_ptr->splt_palettes_num)
  74.476 +       {
  74.477 +         int i;
  74.478 +         for (i = 0; i < (int)info_ptr->splt_palettes_num; i++)
  74.479 +            png_free_data(png_ptr, info_ptr, PNG_FREE_SPLT, i);
  74.480 +
  74.481 +         png_free(png_ptr, info_ptr->splt_palettes);
  74.482 +         info_ptr->splt_palettes = NULL;
  74.483 +         info_ptr->splt_palettes_num = 0;
  74.484 +       }
  74.485 +       info_ptr->valid &= ~PNG_INFO_sPLT;
  74.486 +   }
  74.487 +}
  74.488 +#endif
  74.489 +
  74.490 +#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
  74.491 +  if (png_ptr->unknown_chunk.data)
  74.492 +  {
  74.493 +    png_free(png_ptr, png_ptr->unknown_chunk.data);
  74.494 +    png_ptr->unknown_chunk.data = NULL;
  74.495 +  }
  74.496 +
  74.497 +#ifdef PNG_FREE_ME_SUPPORTED
  74.498 +if ((mask & PNG_FREE_UNKN) & info_ptr->free_me)
  74.499 +#else
  74.500 +if (mask & PNG_FREE_UNKN)
  74.501 +#endif
  74.502 +{
  74.503 +   if (num != -1)
  74.504 +   {
  74.505 +       if (info_ptr->unknown_chunks)
  74.506 +       {
  74.507 +          png_free(png_ptr, info_ptr->unknown_chunks[num].data);
  74.508 +          info_ptr->unknown_chunks[num].data = NULL;
  74.509 +       }
  74.510 +   }
  74.511 +   else
  74.512 +   {
  74.513 +       int i;
  74.514 +
  74.515 +       if (info_ptr->unknown_chunks_num)
  74.516 +       {
  74.517 +         for (i = 0; i < (int)info_ptr->unknown_chunks_num; i++)
  74.518 +            png_free_data(png_ptr, info_ptr, PNG_FREE_UNKN, i);
  74.519 +
  74.520 +         png_free(png_ptr, info_ptr->unknown_chunks);
  74.521 +         info_ptr->unknown_chunks = NULL;
  74.522 +         info_ptr->unknown_chunks_num = 0;
  74.523 +       }
  74.524 +   }
  74.525 +}
  74.526 +#endif
  74.527 +
  74.528 +#if defined(PNG_hIST_SUPPORTED)
  74.529 +/* free any hIST entry */
  74.530 +#ifdef PNG_FREE_ME_SUPPORTED
  74.531 +if ((mask & PNG_FREE_HIST)  & info_ptr->free_me)
  74.532 +#else
  74.533 +if ((mask & PNG_FREE_HIST) && (png_ptr->flags & PNG_FLAG_FREE_HIST))
  74.534 +#endif
  74.535 +{
  74.536 +    png_free(png_ptr, info_ptr->hist);
  74.537 +    info_ptr->hist = NULL;
  74.538 +    info_ptr->valid &= ~PNG_INFO_hIST;
  74.539 +#ifndef PNG_FREE_ME_SUPPORTED
  74.540 +    png_ptr->flags &= ~PNG_FLAG_FREE_HIST;
  74.541 +#endif
  74.542 +}
  74.543 +#endif
  74.544 +
  74.545 +/* free any PLTE entry that was internally allocated */
  74.546 +#ifdef PNG_FREE_ME_SUPPORTED
  74.547 +if ((mask & PNG_FREE_PLTE) & info_ptr->free_me)
  74.548 +#else
  74.549 +if ((mask & PNG_FREE_PLTE) && (png_ptr->flags & PNG_FLAG_FREE_PLTE))
  74.550 +#endif
  74.551 +{
  74.552 +    png_zfree(png_ptr, info_ptr->palette);
  74.553 +    info_ptr->palette = NULL;
  74.554 +    info_ptr->valid &= ~PNG_INFO_PLTE;
  74.555 +#ifndef PNG_FREE_ME_SUPPORTED
  74.556 +    png_ptr->flags &= ~PNG_FLAG_FREE_PLTE;
  74.557 +#endif
  74.558 +    info_ptr->num_palette = 0;
  74.559 +}
  74.560 +
  74.561 +#if defined(PNG_INFO_IMAGE_SUPPORTED)
  74.562 +/* free any image bits attached to the info structure */
  74.563 +#ifdef PNG_FREE_ME_SUPPORTED
  74.564 +if ((mask & PNG_FREE_ROWS) & info_ptr->free_me)
  74.565 +#else
  74.566 +if (mask & PNG_FREE_ROWS)
  74.567 +#endif
  74.568 +{
  74.569 +    if (info_ptr->row_pointers)
  74.570 +    {
  74.571 +       int row;
  74.572 +       for (row = 0; row < (int)info_ptr->height; row++)
  74.573 +       {
  74.574 +          png_free(png_ptr, info_ptr->row_pointers[row]);
  74.575 +          info_ptr->row_pointers[row]=NULL;
  74.576 +       }
  74.577 +       png_free(png_ptr, info_ptr->row_pointers);
  74.578 +       info_ptr->row_pointers=NULL;
  74.579 +    }
  74.580 +    info_ptr->valid &= ~PNG_INFO_IDAT;
  74.581 +}
  74.582 +#endif
  74.583 +
  74.584 +#ifdef PNG_FREE_ME_SUPPORTED
  74.585 +   if (num == -1)
  74.586 +     info_ptr->free_me &= ~mask;
  74.587 +   else
  74.588 +     info_ptr->free_me &= ~(mask & ~PNG_FREE_MUL);
  74.589 +#endif
  74.590 +}
  74.591 +
  74.592 +/* This is an internal routine to free any memory that the info struct is
  74.593 + * pointing to before re-using it or freeing the struct itself.  Recall
  74.594 + * that png_free() checks for NULL pointers for us.
  74.595 + */
  74.596 +void /* PRIVATE */
  74.597 +png_info_destroy(png_structp png_ptr, png_infop info_ptr)
  74.598 +{
  74.599 +   png_debug(1, "in png_info_destroy\n");
  74.600 +
  74.601 +   png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
  74.602 +
  74.603 +#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
  74.604 +   if (png_ptr->num_chunk_list)
  74.605 +   {
  74.606 +       png_free(png_ptr, png_ptr->chunk_list);
  74.607 +       png_ptr->chunk_list=NULL;
  74.608 +       png_ptr->num_chunk_list = 0;
  74.609 +   }
  74.610 +#endif
  74.611 +
  74.612 +   png_info_init_3(&info_ptr, png_sizeof(png_info));
  74.613 +}
  74.614 +#endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */
  74.615 +
  74.616 +/* This function returns a pointer to the io_ptr associated with the user
  74.617 + * functions.  The application should free any memory associated with this
  74.618 + * pointer before png_write_destroy() or png_read_destroy() are called.
  74.619 + */
  74.620 +png_voidp PNGAPI
  74.621 +png_get_io_ptr(png_structp png_ptr)
  74.622 +{
  74.623 +   if (png_ptr == NULL) return (NULL);
  74.624 +   return (png_ptr->io_ptr);
  74.625 +}
  74.626 +
  74.627 +#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
  74.628 +#if !defined(PNG_NO_STDIO)
  74.629 +/* Initialize the default input/output functions for the PNG file.  If you
  74.630 + * use your own read or write routines, you can call either png_set_read_fn()
  74.631 + * or png_set_write_fn() instead of png_init_io().  If you have defined
  74.632 + * PNG_NO_STDIO, you must use a function of your own because "FILE *" isn't
  74.633 + * necessarily available.
  74.634 + */
  74.635 +void PNGAPI
  74.636 +png_init_io(png_structp png_ptr, png_FILE_p fp)
  74.637 +{
  74.638 +   png_debug(1, "in png_init_io\n");
  74.639 +   if (png_ptr == NULL) return;
  74.640 +   png_ptr->io_ptr = (png_voidp)fp;
  74.641 +}
  74.642 +#endif
  74.643 +
  74.644 +#if defined(PNG_TIME_RFC1123_SUPPORTED)
  74.645 +/* Convert the supplied time into an RFC 1123 string suitable for use in
  74.646 + * a "Creation Time" or other text-based time string.
  74.647 + */
  74.648 +png_charp PNGAPI
  74.649 +png_convert_to_rfc1123(png_structp png_ptr, png_timep ptime)
  74.650 +{
  74.651 +   static PNG_CONST char short_months[12][4] =
  74.652 +        {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
  74.653 +         "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
  74.654 +
  74.655 +   if (png_ptr == NULL) return (NULL);
  74.656 +   if (png_ptr->time_buffer == NULL)
  74.657 +   {
  74.658 +      png_ptr->time_buffer = (png_charp)png_malloc(png_ptr, (png_uint_32)(29*
  74.659 +         png_sizeof(char)));
  74.660 +   }
  74.661 +
  74.662 +#if defined(_WIN32_WCE)
  74.663 +   {
  74.664 +      wchar_t time_buf[29];
  74.665 +      wsprintf(time_buf, TEXT("%d %S %d %02d:%02d:%02d +0000"),
  74.666 +          ptime->day % 32, short_months[(ptime->month - 1) % 12],
  74.667 +        ptime->year, ptime->hour % 24, ptime->minute % 60,
  74.668 +          ptime->second % 61);
  74.669 +      WideCharToMultiByte(CP_ACP, 0, time_buf, -1, png_ptr->time_buffer, 29,
  74.670 +          NULL, NULL);
  74.671 +   }
  74.672 +#else
  74.673 +#ifdef USE_FAR_KEYWORD
  74.674 +   {
  74.675 +      char near_time_buf[29];
  74.676 +      png_snprintf6(near_time_buf, 29, "%d %s %d %02d:%02d:%02d +0000",
  74.677 +          ptime->day % 32, short_months[(ptime->month - 1) % 12],
  74.678 +          ptime->year, ptime->hour % 24, ptime->minute % 60,
  74.679 +          ptime->second % 61);
  74.680 +      png_memcpy(png_ptr->time_buffer, near_time_buf,
  74.681 +          29*png_sizeof(char));
  74.682 +   }
  74.683 +#else
  74.684 +   png_snprintf6(png_ptr->time_buffer, 29, "%d %s %d %02d:%02d:%02d +0000",
  74.685 +       ptime->day % 32, short_months[(ptime->month - 1) % 12],
  74.686 +       ptime->year, ptime->hour % 24, ptime->minute % 60,
  74.687 +       ptime->second % 61);
  74.688 +#endif
  74.689 +#endif /* _WIN32_WCE */
  74.690 +   return ((png_charp)png_ptr->time_buffer);
  74.691 +}
  74.692 +#endif /* PNG_TIME_RFC1123_SUPPORTED */
  74.693 +
  74.694 +#endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */
  74.695 +
  74.696 +png_charp PNGAPI
  74.697 +png_get_copyright(png_structp png_ptr)
  74.698 +{
  74.699 +   png_ptr = png_ptr;  /* silence compiler warning about unused png_ptr */
  74.700 +   return ((png_charp) "\n libpng version 1.2.33 - October 31, 2008\n\
  74.701 +   Copyright (c) 1998-2008 Glenn Randers-Pehrson\n\
  74.702 +   Copyright (c) 1996-1997 Andreas Dilger\n\
  74.703 +   Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc.\n");
  74.704 +}
  74.705 +
  74.706 +/* The following return the library version as a short string in the
  74.707 + * format 1.0.0 through 99.99.99zz.  To get the version of *.h files
  74.708 + * used with your application, print out PNG_LIBPNG_VER_STRING, which
  74.709 + * is defined in png.h.
  74.710 + * Note: now there is no difference between png_get_libpng_ver() and
  74.711 + * png_get_header_ver().  Due to the version_nn_nn_nn typedef guard,
  74.712 + * it is guaranteed that png.c uses the correct version of png.h.
  74.713 + */
  74.714 +png_charp PNGAPI
  74.715 +png_get_libpng_ver(png_structp png_ptr)
  74.716 +{
  74.717 +   /* Version of *.c files used when building libpng */
  74.718 +   png_ptr = png_ptr;  /* silence compiler warning about unused png_ptr */
  74.719 +   return ((png_charp) PNG_LIBPNG_VER_STRING);
  74.720 +}
  74.721 +
  74.722 +png_charp PNGAPI
  74.723 +png_get_header_ver(png_structp png_ptr)
  74.724 +{
  74.725 +   /* Version of *.h files used when building libpng */
  74.726 +   png_ptr = png_ptr;  /* silence compiler warning about unused png_ptr */
  74.727 +   return ((png_charp) PNG_LIBPNG_VER_STRING);
  74.728 +}
  74.729 +
  74.730 +png_charp PNGAPI
  74.731 +png_get_header_version(png_structp png_ptr)
  74.732 +{
  74.733 +   /* Returns longer string containing both version and date */
  74.734 +   png_ptr = png_ptr;  /* silence compiler warning about unused png_ptr */
  74.735 +   return ((png_charp) PNG_HEADER_VERSION_STRING
  74.736 +#ifndef PNG_READ_SUPPORTED
  74.737 +   "     (NO READ SUPPORT)"
  74.738 +#endif
  74.739 +   "\n");
  74.740 +}
  74.741 +
  74.742 +#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
  74.743 +#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
  74.744 +int PNGAPI
  74.745 +png_handle_as_unknown(png_structp png_ptr, png_bytep chunk_name)
  74.746 +{
  74.747 +   /* check chunk_name and return "keep" value if it's on the list, else 0 */
  74.748 +   int i;
  74.749 +   png_bytep p;
  74.750 +   if (png_ptr == NULL || chunk_name == NULL || png_ptr->num_chunk_list<=0)
  74.751 +      return 0;
  74.752 +   p = png_ptr->chunk_list + png_ptr->num_chunk_list*5 - 5;
  74.753 +   for (i = png_ptr->num_chunk_list; i; i--, p -= 5)
  74.754 +      if (!png_memcmp(chunk_name, p, 4))
  74.755 +        return ((int)*(p + 4));
  74.756 +   return 0;
  74.757 +}
  74.758 +#endif
  74.759 +
  74.760 +/* This function, added to libpng-1.0.6g, is untested. */
  74.761 +int PNGAPI
  74.762 +png_reset_zstream(png_structp png_ptr)
  74.763 +{
  74.764 +   if (png_ptr == NULL) return Z_STREAM_ERROR;
  74.765 +   return (inflateReset(&png_ptr->zstream));
  74.766 +}
  74.767 +#endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */
  74.768 +
  74.769 +/* This function was added to libpng-1.0.7 */
  74.770 +png_uint_32 PNGAPI
  74.771 +png_access_version_number(void)
  74.772 +{
  74.773 +   /* Version of *.c files used when building libpng */
  74.774 +   return((png_uint_32) PNG_LIBPNG_VER);
  74.775 +}
  74.776 +
  74.777 +
  74.778 +#if defined(PNG_READ_SUPPORTED) && defined(PNG_ASSEMBLER_CODE_SUPPORTED)
  74.779 +#if !defined(PNG_1_0_X)
  74.780 +/* this function was added to libpng 1.2.0 */
  74.781 +int PNGAPI
  74.782 +png_mmx_support(void)
  74.783 +{
  74.784 +   /* obsolete, to be removed from libpng-1.4.0 */
  74.785 +    return -1;
  74.786 +}
  74.787 +#endif /* PNG_1_0_X */
  74.788 +#endif /* PNG_READ_SUPPORTED && PNG_ASSEMBLER_CODE_SUPPORTED */
  74.789 +
  74.790 +#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
  74.791 +#ifdef PNG_SIZE_T
  74.792 +/* Added at libpng version 1.2.6 */
  74.793 +   PNG_EXTERN png_size_t PNGAPI png_convert_size PNGARG((size_t size));
  74.794 +png_size_t PNGAPI
  74.795 +png_convert_size(size_t size)
  74.796 +{
  74.797 +  if (size > (png_size_t)-1)
  74.798 +     PNG_ABORT();  /* We haven't got access to png_ptr, so no png_error() */
  74.799 +  return ((png_size_t)size);
  74.800 +}
  74.801 +#endif /* PNG_SIZE_T */
  74.802 +#endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */
    75.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    75.2 +++ b/libs/libpng/png.h	Sat Sep 19 05:51:51 2015 +0300
    75.3 @@ -0,0 +1,3597 @@
    75.4 +/* png.h - header file for PNG reference library
    75.5 + *
    75.6 + * libpng version 1.2.33 - October 31, 2008
    75.7 + * Copyright (c) 1998-2008 Glenn Randers-Pehrson
    75.8 + * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
    75.9 + * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
   75.10 + *
   75.11 + * Authors and maintainers:
   75.12 + *  libpng versions 0.71, May 1995, through 0.88, January 1996: Guy Schalnat
   75.13 + *  libpng versions 0.89c, June 1996, through 0.96, May 1997: Andreas Dilger
   75.14 + *  libpng versions 0.97, January 1998, through 1.2.33 - October 31, 2008: Glenn
   75.15 + *  See also "Contributing Authors", below.
   75.16 + *
   75.17 + * Note about libpng version numbers:
   75.18 + *
   75.19 + *    Due to various miscommunications, unforeseen code incompatibilities
   75.20 + *    and occasional factors outside the authors' control, version numbering
   75.21 + *    on the library has not always been consistent and straightforward.
   75.22 + *    The following table summarizes matters since version 0.89c, which was
   75.23 + *    the first widely used release:
   75.24 + *
   75.25 + *    source                 png.h  png.h  shared-lib
   75.26 + *    version                string   int  version
   75.27 + *    -------                ------ -----  ----------
   75.28 + *    0.89c "1.0 beta 3"     0.89      89  1.0.89
   75.29 + *    0.90  "1.0 beta 4"     0.90      90  0.90  [should have been 2.0.90]
   75.30 + *    0.95  "1.0 beta 5"     0.95      95  0.95  [should have been 2.0.95]
   75.31 + *    0.96  "1.0 beta 6"     0.96      96  0.96  [should have been 2.0.96]
   75.32 + *    0.97b "1.00.97 beta 7" 1.00.97   97  1.0.1 [should have been 2.0.97]
   75.33 + *    0.97c                  0.97      97  2.0.97
   75.34 + *    0.98                   0.98      98  2.0.98
   75.35 + *    0.99                   0.99      98  2.0.99
   75.36 + *    0.99a-m                0.99      99  2.0.99
   75.37 + *    1.00                   1.00     100  2.1.0 [100 should be 10000]
   75.38 + *    1.0.0      (from here on, the   100  2.1.0 [100 should be 10000]
   75.39 + *    1.0.1       png.h string is   10001  2.1.0
   75.40 + *    1.0.1a-e    identical to the  10002  from here on, the shared library
   75.41 + *    1.0.2       source version)   10002  is 2.V where V is the source code
   75.42 + *    1.0.2a-b                      10003  version, except as noted.
   75.43 + *    1.0.3                         10003
   75.44 + *    1.0.3a-d                      10004
   75.45 + *    1.0.4                         10004
   75.46 + *    1.0.4a-f                      10005
   75.47 + *    1.0.5 (+ 2 patches)           10005
   75.48 + *    1.0.5a-d                      10006
   75.49 + *    1.0.5e-r                      10100 (not source compatible)
   75.50 + *    1.0.5s-v                      10006 (not binary compatible)
   75.51 + *    1.0.6 (+ 3 patches)           10006 (still binary incompatible)
   75.52 + *    1.0.6d-f                      10007 (still binary incompatible)
   75.53 + *    1.0.6g                        10007
   75.54 + *    1.0.6h                        10007  10.6h (testing xy.z so-numbering)
   75.55 + *    1.0.6i                        10007  10.6i
   75.56 + *    1.0.6j                        10007  2.1.0.6j (incompatible with 1.0.0)
   75.57 + *    1.0.7beta11-14        DLLNUM  10007  2.1.0.7beta11-14 (binary compatible)
   75.58 + *    1.0.7beta15-18           1    10007  2.1.0.7beta15-18 (binary compatible)
   75.59 + *    1.0.7rc1-2               1    10007  2.1.0.7rc1-2 (binary compatible)
   75.60 + *    1.0.7                    1    10007  (still compatible)
   75.61 + *    1.0.8beta1-4             1    10008  2.1.0.8beta1-4
   75.62 + *    1.0.8rc1                 1    10008  2.1.0.8rc1
   75.63 + *    1.0.8                    1    10008  2.1.0.8
   75.64 + *    1.0.9beta1-6             1    10009  2.1.0.9beta1-6
   75.65 + *    1.0.9rc1                 1    10009  2.1.0.9rc1
   75.66 + *    1.0.9beta7-10            1    10009  2.1.0.9beta7-10
   75.67 + *    1.0.9rc2                 1    10009  2.1.0.9rc2
   75.68 + *    1.0.9                    1    10009  2.1.0.9
   75.69 + *    1.0.10beta1              1    10010  2.1.0.10beta1
   75.70 + *    1.0.10rc1                1    10010  2.1.0.10rc1
   75.71 + *    1.0.10                   1    10010  2.1.0.10
   75.72 + *    1.0.11beta1-3            1    10011  2.1.0.11beta1-3
   75.73 + *    1.0.11rc1                1    10011  2.1.0.11rc1
   75.74 + *    1.0.11                   1    10011  2.1.0.11
   75.75 + *    1.0.12beta1-2            2    10012  2.1.0.12beta1-2
   75.76 + *    1.0.12rc1                2    10012  2.1.0.12rc1
   75.77 + *    1.0.12                   2    10012  2.1.0.12
   75.78 + *    1.1.0a-f                 -    10100  2.1.1.0a-f (branch abandoned)
   75.79 + *    1.2.0beta1-2             2    10200  2.1.2.0beta1-2
   75.80 + *    1.2.0beta3-5             3    10200  3.1.2.0beta3-5
   75.81 + *    1.2.0rc1                 3    10200  3.1.2.0rc1
   75.82 + *    1.2.0                    3    10200  3.1.2.0
   75.83 + *    1.2.1beta1-4             3    10201  3.1.2.1beta1-4
   75.84 + *    1.2.1rc1-2               3    10201  3.1.2.1rc1-2
   75.85 + *    1.2.1                    3    10201  3.1.2.1
   75.86 + *    1.2.2beta1-6            12    10202  12.so.0.1.2.2beta1-6
   75.87 + *    1.0.13beta1             10    10013  10.so.0.1.0.13beta1
   75.88 + *    1.0.13rc1               10    10013  10.so.0.1.0.13rc1
   75.89 + *    1.2.2rc1                12    10202  12.so.0.1.2.2rc1
   75.90 + *    1.0.13                  10    10013  10.so.0.1.0.13
   75.91 + *    1.2.2                   12    10202  12.so.0.1.2.2
   75.92 + *    1.2.3rc1-6              12    10203  12.so.0.1.2.3rc1-6
   75.93 + *    1.2.3                   12    10203  12.so.0.1.2.3
   75.94 + *    1.2.4beta1-3            13    10204  12.so.0.1.2.4beta1-3
   75.95 + *    1.0.14rc1               13    10014  10.so.0.1.0.14rc1
   75.96 + *    1.2.4rc1                13    10204  12.so.0.1.2.4rc1
   75.97 + *    1.0.14                  10    10014  10.so.0.1.0.14
   75.98 + *    1.2.4                   13    10204  12.so.0.1.2.4
   75.99 + *    1.2.5beta1-2            13    10205  12.so.0.1.2.5beta1-2
  75.100 + *    1.0.15rc1-3             10    10015  10.so.0.1.0.15rc1-3
  75.101 + *    1.2.5rc1-3              13    10205  12.so.0.1.2.5rc1-3
  75.102 + *    1.0.15                  10    10015  10.so.0.1.0.15
  75.103 + *    1.2.5                   13    10205  12.so.0.1.2.5
  75.104 + *    1.2.6beta1-4            13    10206  12.so.0.1.2.6beta1-4
  75.105 + *    1.0.16                  10    10016  10.so.0.1.0.16
  75.106 + *    1.2.6                   13    10206  12.so.0.1.2.6
  75.107 + *    1.2.7beta1-2            13    10207  12.so.0.1.2.7beta1-2
  75.108 + *    1.0.17rc1               10    10017  10.so.0.1.0.17rc1
  75.109 + *    1.2.7rc1                13    10207  12.so.0.1.2.7rc1
  75.110 + *    1.0.17                  10    10017  10.so.0.1.0.17
  75.111 + *    1.2.7                   13    10207  12.so.0.1.2.7
  75.112 + *    1.2.8beta1-5            13    10208  12.so.0.1.2.8beta1-5
  75.113 + *    1.0.18rc1-5             10    10018  10.so.0.1.0.18rc1-5
  75.114 + *    1.2.8rc1-5              13    10208  12.so.0.1.2.8rc1-5
  75.115 + *    1.0.18                  10    10018  10.so.0.1.0.18
  75.116 + *    1.2.8                   13    10208  12.so.0.1.2.8
  75.117 + *    1.2.9beta1-3            13    10209  12.so.0.1.2.9beta1-3
  75.118 + *    1.2.9beta4-11           13    10209  12.so.0.9[.0]
  75.119 + *    1.2.9rc1                13    10209  12.so.0.9[.0]
  75.120 + *    1.2.9                   13    10209  12.so.0.9[.0]
  75.121 + *    1.2.10beta1-8           13    10210  12.so.0.10[.0]
  75.122 + *    1.2.10rc1-3             13    10210  12.so.0.10[.0]
  75.123 + *    1.2.10                  13    10210  12.so.0.10[.0]
  75.124 + *    1.2.11beta1-4           13    10211  12.so.0.11[.0]
  75.125 + *    1.0.19rc1-5             10    10019  10.so.0.19[.0]
  75.126 + *    1.2.11rc1-5             13    10211  12.so.0.11[.0]
  75.127 + *    1.0.19                  10    10019  10.so.0.19[.0]
  75.128 + *    1.2.11                  13    10211  12.so.0.11[.0]
  75.129 + *    1.0.20                  10    10020  10.so.0.20[.0]
  75.130 + *    1.2.12                  13    10212  12.so.0.12[.0]
  75.131 + *    1.2.13beta1             13    10213  12.so.0.13[.0]
  75.132 + *    1.0.21                  10    10021  10.so.0.21[.0]
  75.133 + *    1.2.13                  13    10213  12.so.0.13[.0]
  75.134 + *    1.2.14beta1-2           13    10214  12.so.0.14[.0]
  75.135 + *    1.0.22rc1               10    10022  10.so.0.22[.0]
  75.136 + *    1.2.14rc1               13    10214  12.so.0.14[.0]
  75.137 + *    1.0.22                  10    10022  10.so.0.22[.0]
  75.138 + *    1.2.14                  13    10214  12.so.0.14[.0]
  75.139 + *    1.2.15beta1-6           13    10215  12.so.0.15[.0]
  75.140 + *    1.0.23rc1-5             10    10023  10.so.0.23[.0]
  75.141 + *    1.2.15rc1-5             13    10215  12.so.0.15[.0]
  75.142 + *    1.0.23                  10    10023  10.so.0.23[.0]
  75.143 + *    1.2.15                  13    10215  12.so.0.15[.0]
  75.144 + *    1.2.16beta1-2           13    10216  12.so.0.16[.0]
  75.145 + *    1.2.16rc1               13    10216  12.so.0.16[.0]
  75.146 + *    1.0.24                  10    10024  10.so.0.24[.0]
  75.147 + *    1.2.16                  13    10216  12.so.0.16[.0]
  75.148 + *    1.2.17beta1-2           13    10217  12.so.0.17[.0]
  75.149 + *    1.0.25rc1               10    10025  10.so.0.25[.0]
  75.150 + *    1.2.17rc1-3             13    10217  12.so.0.17[.0]
  75.151 + *    1.0.25                  10    10025  10.so.0.25[.0]
  75.152 + *    1.2.17                  13    10217  12.so.0.17[.0]
  75.153 + *    1.0.26                  10    10026  10.so.0.26[.0]
  75.154 + *    1.2.18                  13    10218  12.so.0.18[.0]
  75.155 + *    1.2.19beta1-31          13    10219  12.so.0.19[.0]
  75.156 + *    1.0.27rc1-6             10    10027  10.so.0.27[.0]
  75.157 + *    1.2.19rc1-6             13    10219  12.so.0.19[.0]
  75.158 + *    1.0.27                  10    10027  10.so.0.27[.0]
  75.159 + *    1.2.19                  13    10219  12.so.0.19[.0]
  75.160 + *    1.2.20beta01-04         13    10220  12.so.0.20[.0]
  75.161 + *    1.0.28rc1-6             10    10028  10.so.0.28[.0]
  75.162 + *    1.2.20rc1-6             13    10220  12.so.0.20[.0]
  75.163 + *    1.0.28                  10    10028  10.so.0.28[.0]
  75.164 + *    1.2.20                  13    10220  12.so.0.20[.0]
  75.165 + *    1.2.21beta1-2           13    10221  12.so.0.21[.0]
  75.166 + *    1.2.21rc1-3             13    10221  12.so.0.21[.0]
  75.167 + *    1.0.29                  10    10029  10.so.0.29[.0]
  75.168 + *    1.2.21                  13    10221  12.so.0.21[.0]
  75.169 + *    1.2.22beta1-4           13    10222  12.so.0.22[.0]
  75.170 + *    1.0.30rc1               10    10030  10.so.0.30[.0]
  75.171 + *    1.2.22rc1               13    10222  12.so.0.22[.0]
  75.172 + *    1.0.30                  10    10030  10.so.0.30[.0]
  75.173 + *    1.2.22                  13    10222  12.so.0.22[.0]
  75.174 + *    1.2.23beta01-05         13    10223  12.so.0.23[.0]
  75.175 + *    1.2.23rc01              13    10223  12.so.0.23[.0]
  75.176 + *    1.2.23                  13    10223  12.so.0.23[.0]
  75.177 + *    1.2.24beta01-02         13    10224  12.so.0.24[.0]
  75.178 + *    1.2.24rc01              13    10224  12.so.0.24[.0]
  75.179 + *    1.2.24                  13    10224  12.so.0.24[.0]
  75.180 + *    1.2.25beta01-06         13    10225  12.so.0.25[.0]
  75.181 + *    1.2.25rc01-02           13    10225  12.so.0.25[.0]
  75.182 + *    1.0.31                  10    10031  10.so.0.31[.0]
  75.183 + *    1.2.25                  13    10225  12.so.0.25[.0]
  75.184 + *    1.2.26beta01-06         13    10226  12.so.0.26[.0]
  75.185 + *    1.2.26rc01              13    10226  12.so.0.26[.0]
  75.186 + *    1.2.26                  13    10226  12.so.0.26[.0]
  75.187 + *    1.0.32                  10    10032  10.so.0.32[.0]
  75.188 + *    1.2.27beta01-06         13    10227  12.so.0.27[.0]
  75.189 + *    1.2.27rc01              13    10227  12.so.0.27[.0]
  75.190 + *    1.0.33                  10    10033  10.so.0.33[.0]
  75.191 + *    1.2.27                  13    10227  12.so.0.27[.0]
  75.192 + *    1.0.34                  10    10034  10.so.0.34[.0]
  75.193 + *    1.2.28                  13    10228  12.so.0.28[.0]
  75.194 + *    1.2.29beta01-03         13    10229  12.so.0.29[.0]
  75.195 + *    1.2.29rc01              13    10229  12.so.0.29[.0]
  75.196 + *    1.0.35                  10    10035  10.so.0.35[.0]
  75.197 + *    1.2.29                  13    10229  12.so.0.29[.0]
  75.198 + *    1.0.37                  10    10037  10.so.0.37[.0]
  75.199 + *    1.2.30beta01-04         13    10230  12.so.0.30[.0]
  75.200 + *    1.0.38rc01-08           10    10038  10.so.0.38[.0]
  75.201 + *    1.2.30rc01-08           13    10230  12.so.0.30[.0]
  75.202 + *    1.0.38                  10    10038  10.so.0.38[.0]
  75.203 + *    1.2.30                  13    10230  12.so.0.30[.0]
  75.204 + *    1.0.39rc01-03           10    10039  10.so.0.39[.0]
  75.205 + *    1.2.31rc01-03           13    10231  12.so.0.31[.0]
  75.206 + *    1.0.39                  10    10039  10.so.0.39[.0]
  75.207 + *    1.2.31                  13    10231  12.so.0.31[.0]
  75.208 + *    1.2.32beta01-02         13    10232  12.so.0.32[.0]
  75.209 + *    1.0.40rc01              10    10040  10.so.0.40[.0]
  75.210 + *    1.2.32rc01              13    10232  12.so.0.32[.0]
  75.211 + *    1.0.40                  10    10040  10.so.0.40[.0]
  75.212 + *    1.2.32                  13    10232  12.so.0.32[.0]
  75.213 + *    1.2.33beta01-02         13    10233  12.so.0.33[.0]
  75.214 + *    1.2.33rc01-02           13    10233  12.so.0.33[.0]
  75.215 + *    1.0.41rc01              10    10041  10.so.0.41[.0]
  75.216 + *    1.2.33                  13    10233  12.so.0.33[.0]
  75.217 + *    1.0.41                  10    10041  10.so.0.41[.0]
  75.218 + *
  75.219 + *    Henceforth the source version will match the shared-library major
  75.220 + *    and minor numbers; the shared-library major version number will be
  75.221 + *    used for changes in backward compatibility, as it is intended.  The
  75.222 + *    PNG_LIBPNG_VER macro, which is not used within libpng but is available
  75.223 + *    for applications, is an unsigned integer of the form xyyzz corresponding
  75.224 + *    to the source version x.y.z (leading zeros in y and z).  Beta versions
  75.225 + *    were given the previous public release number plus a letter, until
  75.226 + *    version 1.0.6j; from then on they were given the upcoming public
  75.227 + *    release number plus "betaNN" or "rcNN".
  75.228 + *
  75.229 + *    Binary incompatibility exists only when applications make direct access
  75.230 + *    to the info_ptr or png_ptr members through png.h, and the compiled
  75.231 + *    application is loaded with a different version of the library.
  75.232 + *
  75.233 + *    DLLNUM will change each time there are forward or backward changes
  75.234 + *    in binary compatibility (e.g., when a new feature is added).
  75.235 + *
  75.236 + * See libpng.txt or libpng.3 for more information.  The PNG specification
  75.237 + * is available as a W3C Recommendation and as an ISO Specification,
  75.238 + * <http://www.w3.org/TR/2003/REC-PNG-20031110/
  75.239 + */
  75.240 +
  75.241 +/*
  75.242 + * COPYRIGHT NOTICE, DISCLAIMER, and LICENSE:
  75.243 + *
  75.244 + * If you modify libpng you may insert additional notices immediately following
  75.245 + * this sentence.
  75.246 + *
  75.247 + * libpng versions 1.2.6, August 15, 2004, through 1.2.33, October 31, 2008, are
  75.248 + * Copyright (c) 2004, 2006-2008 Glenn Randers-Pehrson, and are
  75.249 + * distributed according to the same disclaimer and license as libpng-1.2.5
  75.250 + * with the following individual added to the list of Contributing Authors:
  75.251 + *
  75.252 + *    Cosmin Truta
  75.253 + *
  75.254 + * libpng versions 1.0.7, July 1, 2000, through 1.2.5, October 3, 2002, are
  75.255 + * Copyright (c) 2000-2002 Glenn Randers-Pehrson, and are
  75.256 + * distributed according to the same disclaimer and license as libpng-1.0.6
  75.257 + * with the following individuals added to the list of Contributing Authors:
  75.258 + *
  75.259 + *    Simon-Pierre Cadieux
  75.260 + *    Eric S. Raymond
  75.261 + *    Gilles Vollant
  75.262 + *
  75.263 + * and with the following additions to the disclaimer:
  75.264 + *
  75.265 + *    There is no warranty against interference with your enjoyment of the
  75.266 + *    library or against infringement.  There is no warranty that our
  75.267 + *    efforts or the library will fulfill any of your particular purposes
  75.268 + *    or needs.  This library is provided with all faults, and the entire
  75.269 + *    risk of satisfactory quality, performance, accuracy, and effort is with
  75.270 + *    the user.
  75.271 + *
  75.272 + * libpng versions 0.97, January 1998, through 1.0.6, March 20, 2000, are
  75.273 + * Copyright (c) 1998, 1999, 2000 Glenn Randers-Pehrson, and are
  75.274 + * distributed according to the same disclaimer and license as libpng-0.96,
  75.275 + * with the following individuals added to the list of Contributing Authors:
  75.276 + *
  75.277 + *    Tom Lane
  75.278 + *    Glenn Randers-Pehrson
  75.279 + *    Willem van Schaik
  75.280 + *
  75.281 + * libpng versions 0.89, June 1996, through 0.96, May 1997, are
  75.282 + * Copyright (c) 1996, 1997 Andreas Dilger
  75.283 + * Distributed according to the same disclaimer and license as libpng-0.88,
  75.284 + * with the following individuals added to the list of Contributing Authors:
  75.285 + *
  75.286 + *    John Bowler
  75.287 + *    Kevin Bracey
  75.288 + *    Sam Bushell
  75.289 + *    Magnus Holmgren
  75.290 + *    Greg Roelofs
  75.291 + *    Tom Tanner
  75.292 + *
  75.293 + * libpng versions 0.5, May 1995, through 0.88, January 1996, are
  75.294 + * Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
  75.295 + *
  75.296 + * For the purposes of this copyright and license, "Contributing Authors"
  75.297 + * is defined as the following set of individuals:
  75.298 + *
  75.299 + *    Andreas Dilger
  75.300 + *    Dave Martindale
  75.301 + *    Guy Eric Schalnat
  75.302 + *    Paul Schmidt
  75.303 + *    Tim Wegner
  75.304 + *
  75.305 + * The PNG Reference Library is supplied "AS IS".  The Contributing Authors
  75.306 + * and Group 42, Inc. disclaim all warranties, expressed or implied,
  75.307 + * including, without limitation, the warranties of merchantability and of
  75.308 + * fitness for any purpose.  The Contributing Authors and Group 42, Inc.
  75.309 + * assume no liability for direct, indirect, incidental, special, exemplary,
  75.310 + * or consequential damages, which may result from the use of the PNG
  75.311 + * Reference Library, even if advised of the possibility of such damage.
  75.312 + *
  75.313 + * Permission is hereby granted to use, copy, modify, and distribute this
  75.314 + * source code, or portions hereof, for any purpose, without fee, subject
  75.315 + * to the following restrictions:
  75.316 + *
  75.317 + * 1. The origin of this source code must not be misrepresented.
  75.318 + *
  75.319 + * 2. Altered versions must be plainly marked as such and
  75.320 + * must not be misrepresented as being the original source.
  75.321 + *
  75.322 + * 3. This Copyright notice may not be removed or altered from
  75.323 + *    any source or altered source distribution.
  75.324 + *
  75.325 + * The Contributing Authors and Group 42, Inc. specifically permit, without
  75.326 + * fee, and encourage the use of this source code as a component to
  75.327 + * supporting the PNG file format in commercial products.  If you use this
  75.328 + * source code in a product, acknowledgment is not required but would be
  75.329 + * appreciated.
  75.330 + */
  75.331 +
  75.332 +/*
  75.333 + * A "png_get_copyright" function is available, for convenient use in "about"
  75.334 + * boxes and the like:
  75.335 + *
  75.336 + * printf("%s",png_get_copyright(NULL));
  75.337 + *
  75.338 + * Also, the PNG logo (in PNG format, of course) is supplied in the
  75.339 + * files "pngbar.png" and "pngbar.jpg (88x31) and "pngnow.png" (98x31).
  75.340 + */
  75.341 +
  75.342 +/*
  75.343 + * Libpng is OSI Certified Open Source Software.  OSI Certified is a
  75.344 + * certification mark of the Open Source Initiative.
  75.345 + */
  75.346 +
  75.347 +/*
  75.348 + * The contributing authors would like to thank all those who helped
  75.349 + * with testing, bug fixes, and patience.  This wouldn't have been
  75.350 + * possible without all of you.
  75.351 + *
  75.352 + * Thanks to Frank J. T. Wojcik for helping with the documentation.
  75.353 + */
  75.354 +
  75.355 +/*
  75.356 + * Y2K compliance in libpng:
  75.357 + * =========================
  75.358 + *
  75.359 + *    October 31, 2008
  75.360 + *
  75.361 + *    Since the PNG Development group is an ad-hoc body, we can't make
  75.362 + *    an official declaration.
  75.363 + *
  75.364 + *    This is your unofficial assurance that libpng from version 0.71 and
  75.365 + *    upward through 1.2.33 are Y2K compliant.  It is my belief that earlier
  75.366 + *    versions were also Y2K compliant.
  75.367 + *
  75.368 + *    Libpng only has three year fields.  One is a 2-byte unsigned integer
  75.369 + *    that will hold years up to 65535.  The other two hold the date in text
  75.370 + *    format, and will hold years up to 9999.
  75.371 + *
  75.372 + *    The integer is
  75.373 + *        "png_uint_16 year" in png_time_struct.
  75.374 + *
  75.375 + *    The strings are
  75.376 + *        "png_charp time_buffer" in png_struct and
  75.377 + *        "near_time_buffer", which is a local character string in png.c.
  75.378 + *
  75.379 + *    There are seven time-related functions:
  75.380 + *        png.c: png_convert_to_rfc_1123() in png.c
  75.381 + *          (formerly png_convert_to_rfc_1152() in error)
  75.382 + *        png_convert_from_struct_tm() in pngwrite.c, called in pngwrite.c
  75.383 + *        png_convert_from_time_t() in pngwrite.c
  75.384 + *        png_get_tIME() in pngget.c
  75.385 + *        png_handle_tIME() in pngrutil.c, called in pngread.c
  75.386 + *        png_set_tIME() in pngset.c
  75.387 + *        png_write_tIME() in pngwutil.c, called in pngwrite.c
  75.388 + *
  75.389 + *    All handle dates properly in a Y2K environment.  The
  75.390 + *    png_convert_from_time_t() function calls gmtime() to convert from system
  75.391 + *    clock time, which returns (year - 1900), which we properly convert to
  75.392 + *    the full 4-digit year.  There is a possibility that applications using
  75.393 + *    libpng are not passing 4-digit years into the png_convert_to_rfc_1123()
  75.394 + *    function, or that they are incorrectly passing only a 2-digit year
  75.395 + *    instead of "year - 1900" into the png_convert_from_struct_tm() function,
  75.396 + *    but this is not under our control.  The libpng documentation has always
  75.397 + *    stated that it works with 4-digit years, and the APIs have been
  75.398 + *    documented as such.
  75.399 + *
  75.400 + *    The tIME chunk itself is also Y2K compliant.  It uses a 2-byte unsigned
  75.401 + *    integer to hold the year, and can hold years as large as 65535.
  75.402 + *
  75.403 + *    zlib, upon which libpng depends, is also Y2K compliant.  It contains
  75.404 + *    no date-related code.
  75.405 + *
  75.406 + *       Glenn Randers-Pehrson
  75.407 + *       libpng maintainer
  75.408 + *       PNG Development Group
  75.409 + */
  75.410 +
  75.411 +#ifndef PNG_H
  75.412 +#define PNG_H
  75.413 +
  75.414 +/* This is not the place to learn how to use libpng.  The file libpng.txt
  75.415 + * describes how to use libpng, and the file example.c summarizes it
  75.416 + * with some code on which to build.  This file is useful for looking
  75.417 + * at the actual function definitions and structure components.
  75.418 + */
  75.419 +
  75.420 +/* Version information for png.h - this should match the version in png.c */
  75.421 +#define PNG_LIBPNG_VER_STRING "1.2.33"
  75.422 +#define PNG_HEADER_VERSION_STRING \
  75.423 +   " libpng version 1.2.33 - October 31, 2008\n"
  75.424 +
  75.425 +#define PNG_LIBPNG_VER_SONUM   0
  75.426 +#define PNG_LIBPNG_VER_DLLNUM  13
  75.427 +
  75.428 +/* These should match the first 3 components of PNG_LIBPNG_VER_STRING: */
  75.429 +#define PNG_LIBPNG_VER_MAJOR   1
  75.430 +#define PNG_LIBPNG_VER_MINOR   2
  75.431 +#define PNG_LIBPNG_VER_RELEASE 33
  75.432 +/* This should match the numeric part of the final component of
  75.433 + * PNG_LIBPNG_VER_STRING, omitting any leading zero: */
  75.434 +
  75.435 +#define PNG_LIBPNG_VER_BUILD  0
  75.436 +
  75.437 +/* Release Status */
  75.438 +#define PNG_LIBPNG_BUILD_ALPHA    1
  75.439 +#define PNG_LIBPNG_BUILD_BETA     2
  75.440 +#define PNG_LIBPNG_BUILD_RC       3
  75.441 +#define PNG_LIBPNG_BUILD_STABLE   4
  75.442 +#define PNG_LIBPNG_BUILD_RELEASE_STATUS_MASK 7
  75.443 +  
  75.444 +/* Release-Specific Flags */
  75.445 +#define PNG_LIBPNG_BUILD_PATCH    8 /* Can be OR'ed with
  75.446 +                                       PNG_LIBPNG_BUILD_STABLE only */
  75.447 +#define PNG_LIBPNG_BUILD_PRIVATE 16 /* Cannot be OR'ed with
  75.448 +                                       PNG_LIBPNG_BUILD_SPECIAL */
  75.449 +#define PNG_LIBPNG_BUILD_SPECIAL 32 /* Cannot be OR'ed with
  75.450 +                                       PNG_LIBPNG_BUILD_PRIVATE */
  75.451 +
  75.452 +#define PNG_LIBPNG_BUILD_BASE_TYPE PNG_LIBPNG_BUILD_STABLE
  75.453 +
  75.454 +/* Careful here.  At one time, Guy wanted to use 082, but that would be octal.
  75.455 + * We must not include leading zeros.
  75.456 + * Versions 0.7 through 1.0.0 were in the range 0 to 100 here (only
  75.457 + * version 1.0.0 was mis-numbered 100 instead of 10000).  From
  75.458 + * version 1.0.1 it's    xxyyzz, where x=major, y=minor, z=release */
  75.459 +#define PNG_LIBPNG_VER 10233 /* 1.2.33 */
  75.460 +
  75.461 +#ifndef PNG_VERSION_INFO_ONLY
  75.462 +/* include the compression library's header */
  75.463 +#include "zlib.h"
  75.464 +#endif
  75.465 +
  75.466 +/* include all user configurable info, including optional assembler routines */
  75.467 +#include "pngconf.h"
  75.468 +
  75.469 +/*
  75.470 + * Added at libpng-1.2.8 */
  75.471 +/* Ref MSDN: Private as priority over Special
  75.472 + * VS_FF_PRIVATEBUILD File *was not* built using standard release
  75.473 + * procedures. If this value is given, the StringFileInfo block must
  75.474 + * contain a PrivateBuild string. 
  75.475 + *
  75.476 + * VS_FF_SPECIALBUILD File *was* built by the original company using
  75.477 + * standard release procedures but is a variation of the standard
  75.478 + * file of the same version number. If this value is given, the
  75.479 + * StringFileInfo block must contain a SpecialBuild string. 
  75.480 + */
  75.481 +
  75.482 +#if defined(PNG_USER_PRIVATEBUILD)
  75.483 +#  define PNG_LIBPNG_BUILD_TYPE \
  75.484 +          (PNG_LIBPNG_BUILD_BASE_TYPE | PNG_LIBPNG_BUILD_PRIVATE)
  75.485 +#else
  75.486 +#  if defined(PNG_LIBPNG_SPECIALBUILD)
  75.487 +#    define PNG_LIBPNG_BUILD_TYPE \
  75.488 +            (PNG_LIBPNG_BUILD_BASE_TYPE | PNG_LIBPNG_BUILD_SPECIAL)
  75.489 +#  else
  75.490 +#    define PNG_LIBPNG_BUILD_TYPE (PNG_LIBPNG_BUILD_BASE_TYPE)
  75.491 +#  endif
  75.492 +#endif
  75.493 +
  75.494 +#ifndef PNG_VERSION_INFO_ONLY
  75.495 +
  75.496 +/* Inhibit C++ name-mangling for libpng functions but not for system calls. */
  75.497 +#ifdef __cplusplus
  75.498 +extern "C" {
  75.499 +#endif /* __cplusplus */
  75.500 +
  75.501 +/* This file is arranged in several sections.  The first section contains
  75.502 + * structure and type definitions.  The second section contains the external
  75.503 + * library functions, while the third has the internal library functions,
  75.504 + * which applications aren't expected to use directly.
  75.505 + */
  75.506 +
  75.507 +#ifndef PNG_NO_TYPECAST_NULL
  75.508 +#define int_p_NULL                (int *)NULL
  75.509 +#define png_bytep_NULL            (png_bytep)NULL
  75.510 +#define png_bytepp_NULL           (png_bytepp)NULL
  75.511 +#define png_doublep_NULL          (png_doublep)NULL
  75.512 +#define png_error_ptr_NULL        (png_error_ptr)NULL
  75.513 +#define png_flush_ptr_NULL        (png_flush_ptr)NULL
  75.514 +#define png_free_ptr_NULL         (png_free_ptr)NULL
  75.515 +#define png_infopp_NULL           (png_infopp)NULL
  75.516 +#define png_malloc_ptr_NULL       (png_malloc_ptr)NULL
  75.517 +#define png_read_status_ptr_NULL  (png_read_status_ptr)NULL
  75.518 +#define png_rw_ptr_NULL           (png_rw_ptr)NULL
  75.519 +#define png_structp_NULL          (png_structp)NULL
  75.520 +#define png_uint_16p_NULL         (png_uint_16p)NULL
  75.521 +#define png_voidp_NULL            (png_voidp)NULL
  75.522 +#define png_write_status_ptr_NULL (png_write_status_ptr)NULL
  75.523 +#else
  75.524 +#define int_p_NULL                NULL
  75.525 +#define png_bytep_NULL            NULL
  75.526 +#define png_bytepp_NULL           NULL
  75.527 +#define png_doublep_NULL          NULL
  75.528 +#define png_error_ptr_NULL        NULL
  75.529 +#define png_flush_ptr_NULL        NULL
  75.530 +#define png_free_ptr_NULL         NULL
  75.531 +#define png_infopp_NULL           NULL
  75.532 +#define png_malloc_ptr_NULL       NULL
  75.533 +#define png_read_status_ptr_NULL  NULL
  75.534 +#define png_rw_ptr_NULL           NULL
  75.535 +#define png_structp_NULL          NULL
  75.536 +#define png_uint_16p_NULL         NULL
  75.537 +#define png_voidp_NULL            NULL
  75.538 +#define png_write_status_ptr_NULL NULL
  75.539 +#endif
  75.540 +
  75.541 +/* variables declared in png.c - only it needs to define PNG_NO_EXTERN */
  75.542 +#if !defined(PNG_NO_EXTERN) || defined(PNG_ALWAYS_EXTERN)
  75.543 +/* Version information for C files, stored in png.c.  This had better match
  75.544 + * the version above.
  75.545 + */
  75.546 +#ifdef PNG_USE_GLOBAL_ARRAYS
  75.547 +PNG_EXPORT_VAR (PNG_CONST char) png_libpng_ver[18];
  75.548 +  /* need room for 99.99.99beta99z */
  75.549 +#else
  75.550 +#define png_libpng_ver png_get_header_ver(NULL)
  75.551 +#endif
  75.552 +
  75.553 +#ifdef PNG_USE_GLOBAL_ARRAYS
  75.554 +/* This was removed in version 1.0.5c */
  75.555 +/* Structures to facilitate easy interlacing.  See png.c for more details */
  75.556 +PNG_EXPORT_VAR (PNG_CONST int FARDATA) png_pass_start[7];
  75.557 +PNG_EXPORT_VAR (PNG_CONST int FARDATA) png_pass_inc[7];
  75.558 +PNG_EXPORT_VAR (PNG_CONST int FARDATA) png_pass_ystart[7];
  75.559 +PNG_EXPORT_VAR (PNG_CONST int FARDATA) png_pass_yinc[7];
  75.560 +PNG_EXPORT_VAR (PNG_CONST int FARDATA) png_pass_mask[7];
  75.561 +PNG_EXPORT_VAR (PNG_CONST int FARDATA) png_pass_dsp_mask[7];
  75.562 +/* This isn't currently used.  If you need it, see png.c for more details.
  75.563 +PNG_EXPORT_VAR (PNG_CONST int FARDATA) png_pass_height[7];
  75.564 +*/
  75.565 +#endif
  75.566 +
  75.567 +#endif /* PNG_NO_EXTERN */
  75.568 +
  75.569 +/* Three color definitions.  The order of the red, green, and blue, (and the
  75.570 + * exact size) is not important, although the size of the fields need to
  75.571 + * be png_byte or png_uint_16 (as defined below).
  75.572 + */
  75.573 +typedef struct png_color_struct
  75.574 +{
  75.575 +   png_byte red;
  75.576 +   png_byte green;
  75.577 +   png_byte blue;
  75.578 +} png_color;
  75.579 +typedef png_color FAR * png_colorp;
  75.580 +typedef png_color FAR * FAR * png_colorpp;
  75.581 +
  75.582 +typedef struct png_color_16_struct
  75.583 +{
  75.584 +   png_byte index;    /* used for palette files */
  75.585 +   png_uint_16 red;   /* for use in red green blue files */
  75.586 +   png_uint_16 green;
  75.587 +   png_uint_16 blue;
  75.588 +   png_uint_16 gray;  /* for use in grayscale files */
  75.589 +} png_color_16;
  75.590 +typedef png_color_16 FAR * png_color_16p;
  75.591 +typedef png_color_16 FAR * FAR * png_color_16pp;
  75.592 +
  75.593 +typedef struct png_color_8_struct
  75.594 +{
  75.595 +   png_byte red;   /* for use in red green blue files */
  75.596 +   png_byte green;
  75.597 +   png_byte blue;
  75.598 +   png_byte gray;  /* for use in grayscale files */
  75.599 +   png_byte alpha; /* for alpha channel files */
  75.600 +} png_color_8;
  75.601 +typedef png_color_8 FAR * png_color_8p;
  75.602 +typedef png_color_8 FAR * FAR * png_color_8pp;
  75.603 +
  75.604 +/*
  75.605 + * The following two structures are used for the in-core representation
  75.606 + * of sPLT chunks.
  75.607 + */
  75.608 +typedef struct png_sPLT_entry_struct
  75.609 +{
  75.610 +   png_uint_16 red;
  75.611 +   png_uint_16 green;
  75.612 +   png_uint_16 blue;
  75.613 +   png_uint_16 alpha;
  75.614 +   png_uint_16 frequency;
  75.615 +} png_sPLT_entry;
  75.616 +typedef png_sPLT_entry FAR * png_sPLT_entryp;
  75.617 +typedef png_sPLT_entry FAR * FAR * png_sPLT_entrypp;
  75.618 +
  75.619 +/*  When the depth of the sPLT palette is 8 bits, the color and alpha samples
  75.620 + *  occupy the LSB of their respective members, and the MSB of each member
  75.621 + *  is zero-filled.  The frequency member always occupies the full 16 bits.
  75.622 + */
  75.623 +
  75.624 +typedef struct png_sPLT_struct
  75.625 +{
  75.626 +   png_charp name;           /* palette name */
  75.627 +   png_byte depth;           /* depth of palette samples */
  75.628 +   png_sPLT_entryp entries;  /* palette entries */
  75.629 +   png_int_32 nentries;      /* number of palette entries */
  75.630 +} png_sPLT_t;
  75.631 +typedef png_sPLT_t FAR * png_sPLT_tp;
  75.632 +typedef png_sPLT_t FAR * FAR * png_sPLT_tpp;
  75.633 +
  75.634 +#ifdef PNG_TEXT_SUPPORTED
  75.635 +/* png_text holds the contents of a text/ztxt/itxt chunk in a PNG file,
  75.636 + * and whether that contents is compressed or not.  The "key" field
  75.637 + * points to a regular zero-terminated C string.  The "text", "lang", and
  75.638 + * "lang_key" fields can be regular C strings, empty strings, or NULL pointers.
  75.639 + * However, the * structure returned by png_get_text() will always contain
  75.640 + * regular zero-terminated C strings (possibly empty), never NULL pointers,
  75.641 + * so they can be safely used in printf() and other string-handling functions.
  75.642 + */
  75.643 +typedef struct png_text_struct
  75.644 +{
  75.645 +   int  compression;       /* compression value:
  75.646 +                             -1: tEXt, none
  75.647 +                              0: zTXt, deflate
  75.648 +                              1: iTXt, none
  75.649 +                              2: iTXt, deflate  */
  75.650 +   png_charp key;          /* keyword, 1-79 character description of "text" */
  75.651 +   png_charp text;         /* comment, may be an empty string (ie "")
  75.652 +                              or a NULL pointer */
  75.653 +   png_size_t text_length; /* length of the text string */
  75.654 +#ifdef PNG_iTXt_SUPPORTED
  75.655 +   png_size_t itxt_length; /* length of the itxt string */
  75.656 +   png_charp lang;         /* language code, 0-79 characters
  75.657 +                              or a NULL pointer */
  75.658 +   png_charp lang_key;     /* keyword translated UTF-8 string, 0 or more
  75.659 +                              chars or a NULL pointer */
  75.660 +#endif
  75.661 +} png_text;
  75.662 +typedef png_text FAR * png_textp;
  75.663 +typedef png_text FAR * FAR * png_textpp;
  75.664 +#endif
  75.665 +
  75.666 +/* Supported compression types for text in PNG files (tEXt, and zTXt).
  75.667 + * The values of the PNG_TEXT_COMPRESSION_ defines should NOT be changed. */
  75.668 +#define PNG_TEXT_COMPRESSION_NONE_WR -3
  75.669 +#define PNG_TEXT_COMPRESSION_zTXt_WR -2
  75.670 +#define PNG_TEXT_COMPRESSION_NONE    -1
  75.671 +#define PNG_TEXT_COMPRESSION_zTXt     0
  75.672 +#define PNG_ITXT_COMPRESSION_NONE     1
  75.673 +#define PNG_ITXT_COMPRESSION_zTXt     2
  75.674 +#define PNG_TEXT_COMPRESSION_LAST     3  /* Not a valid value */
  75.675 +
  75.676 +/* png_time is a way to hold the time in an machine independent way.
  75.677 + * Two conversions are provided, both from time_t and struct tm.  There
  75.678 + * is no portable way to convert to either of these structures, as far
  75.679 + * as I know.  If you know of a portable way, send it to me.  As a side
  75.680 + * note - PNG has always been Year 2000 compliant!
  75.681 + */
  75.682 +typedef struct png_time_struct
  75.683 +{
  75.684 +   png_uint_16 year; /* full year, as in, 1995 */
  75.685 +   png_byte month;   /* month of year, 1 - 12 */
  75.686 +   png_byte day;     /* day of month, 1 - 31 */
  75.687 +   png_byte hour;    /* hour of day, 0 - 23 */
  75.688 +   png_byte minute;  /* minute of hour, 0 - 59 */
  75.689 +   png_byte second;  /* second of minute, 0 - 60 (for leap seconds) */
  75.690 +} png_time;
  75.691 +typedef png_time FAR * png_timep;
  75.692 +typedef png_time FAR * FAR * png_timepp;
  75.693 +
  75.694 +#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
  75.695 +/* png_unknown_chunk is a structure to hold queued chunks for which there is
  75.696 + * no specific support.  The idea is that we can use this to queue
  75.697 + * up private chunks for output even though the library doesn't actually
  75.698 + * know about their semantics.
  75.699 + */
  75.700 +#define PNG_CHUNK_NAME_LENGTH 5
  75.701 +typedef struct png_unknown_chunk_t
  75.702 +{
  75.703 +    png_byte name[PNG_CHUNK_NAME_LENGTH];
  75.704 +    png_byte *data;
  75.705 +    png_size_t size;
  75.706 +
  75.707 +    /* libpng-using applications should NOT directly modify this byte. */
  75.708 +    png_byte location; /* mode of operation at read time */
  75.709 +}
  75.710 +png_unknown_chunk;
  75.711 +typedef png_unknown_chunk FAR * png_unknown_chunkp;
  75.712 +typedef png_unknown_chunk FAR * FAR * png_unknown_chunkpp;
  75.713 +#endif
  75.714 +
  75.715 +/* png_info is a structure that holds the information in a PNG file so
  75.716 + * that the application can find out the characteristics of the image.
  75.717 + * If you are reading the file, this structure will tell you what is
  75.718 + * in the PNG file.  If you are writing the file, fill in the information
  75.719 + * you want to put into the PNG file, then call png_write_info().
  75.720 + * The names chosen should be very close to the PNG specification, so
  75.721 + * consult that document for information about the meaning of each field.
  75.722 + *
  75.723 + * With libpng < 0.95, it was only possible to directly set and read the
  75.724 + * the values in the png_info_struct, which meant that the contents and
  75.725 + * order of the values had to remain fixed.  With libpng 0.95 and later,
  75.726 + * however, there are now functions that abstract the contents of
  75.727 + * png_info_struct from the application, so this makes it easier to use
  75.728 + * libpng with dynamic libraries, and even makes it possible to use
  75.729 + * libraries that don't have all of the libpng ancillary chunk-handing
  75.730 + * functionality.
  75.731 + *
  75.732 + * In any case, the order of the parameters in png_info_struct should NOT
  75.733 + * be changed for as long as possible to keep compatibility with applications
  75.734 + * that use the old direct-access method with png_info_struct.
  75.735 + *
  75.736 + * The following members may have allocated storage attached that should be
  75.737 + * cleaned up before the structure is discarded: palette, trans, text,
  75.738 + * pcal_purpose, pcal_units, pcal_params, hist, iccp_name, iccp_profile,
  75.739 + * splt_palettes, scal_unit, row_pointers, and unknowns.   By default, these
  75.740 + * are automatically freed when the info structure is deallocated, if they were
  75.741 + * allocated internally by libpng.  This behavior can be changed by means
  75.742 + * of the png_data_freer() function.
  75.743 + *
  75.744 + * More allocation details: all the chunk-reading functions that
  75.745 + * change these members go through the corresponding png_set_*
  75.746 + * functions.  A function to clear these members is available: see
  75.747 + * png_free_data().  The png_set_* functions do not depend on being
  75.748 + * able to point info structure members to any of the storage they are
  75.749 + * passed (they make their own copies), EXCEPT that the png_set_text
  75.750 + * functions use the same storage passed to them in the text_ptr or
  75.751 + * itxt_ptr structure argument, and the png_set_rows and png_set_unknowns
  75.752 + * functions do not make their own copies.
  75.753 + */
  75.754 +typedef struct png_info_struct
  75.755 +{
  75.756 +   /* the following are necessary for every PNG file */
  75.757 +   png_uint_32 width;       /* width of image in pixels (from IHDR) */
  75.758 +   png_uint_32 height;      /* height of image in pixels (from IHDR) */
  75.759 +   png_uint_32 valid;       /* valid chunk data (see PNG_INFO_ below) */
  75.760 +   png_uint_32 rowbytes;    /* bytes needed to hold an untransformed row */
  75.761 +   png_colorp palette;      /* array of color values (valid & PNG_INFO_PLTE) */
  75.762 +   png_uint_16 num_palette; /* number of color entries in "palette" (PLTE) */
  75.763 +   png_uint_16 num_trans;   /* number of transparent palette color (tRNS) */
  75.764 +   png_byte bit_depth;      /* 1, 2, 4, 8, or 16 bits/channel (from IHDR) */
  75.765 +   png_byte color_type;     /* see PNG_COLOR_TYPE_ below (from IHDR) */
  75.766 +   /* The following three should have been named *_method not *_type */
  75.767 +   png_byte compression_type; /* must be PNG_COMPRESSION_TYPE_BASE (IHDR) */
  75.768 +   png_byte filter_type;    /* must be PNG_FILTER_TYPE_BASE (from IHDR) */
  75.769 +   png_byte interlace_type; /* One of PNG_INTERLACE_NONE, PNG_INTERLACE_ADAM7 */
  75.770 +
  75.771 +   /* The following is informational only on read, and not used on writes. */
  75.772 +   png_byte channels;       /* number of data channels per pixel (1, 2, 3, 4) */
  75.773 +   png_byte pixel_depth;    /* number of bits per pixel */
  75.774 +   png_byte spare_byte;     /* to align the data, and for future use */
  75.775 +   png_byte signature[8];   /* magic bytes read by libpng from start of file */
  75.776 +
  75.777 +   /* The rest of the data is optional.  If you are reading, check the
  75.778 +    * valid field to see if the information in these are valid.  If you
  75.779 +    * are writing, set the valid field to those chunks you want written,
  75.780 +    * and initialize the appropriate fields below.
  75.781 +    */
  75.782 +
  75.783 +#if defined(PNG_gAMA_SUPPORTED) && defined(PNG_FLOATING_POINT_SUPPORTED)
  75.784 +   /* The gAMA chunk describes the gamma characteristics of the system
  75.785 +    * on which the image was created, normally in the range [1.0, 2.5].
  75.786 +    * Data is valid if (valid & PNG_INFO_gAMA) is non-zero.
  75.787 +    */
  75.788 +   float gamma; /* gamma value of image, if (valid & PNG_INFO_gAMA) */
  75.789 +#endif
  75.790 +
  75.791 +#if defined(PNG_sRGB_SUPPORTED)
  75.792 +    /* GR-P, 0.96a */
  75.793 +    /* Data valid if (valid & PNG_INFO_sRGB) non-zero. */
  75.794 +   png_byte srgb_intent; /* sRGB rendering intent [0, 1, 2, or 3] */
  75.795 +#endif
  75.796 +
  75.797 +#if defined(PNG_TEXT_SUPPORTED)
  75.798 +   /* The tEXt, and zTXt chunks contain human-readable textual data in
  75.799 +    * uncompressed, compressed, and optionally compressed forms, respectively.
  75.800 +    * The data in "text" is an array of pointers to uncompressed,
  75.801 +    * null-terminated C strings. Each chunk has a keyword that describes the
  75.802 +    * textual data contained in that chunk.  Keywords are not required to be
  75.803 +    * unique, and the text string may be empty.  Any number of text chunks may
  75.804 +    * be in an image.
  75.805 +    */
  75.806 +   int num_text; /* number of comments read/to write */
  75.807 +   int max_text; /* current size of text array */
  75.808 +   png_textp text; /* array of comments read/to write */
  75.809 +#endif /* PNG_TEXT_SUPPORTED */
  75.810 +
  75.811 +#if defined(PNG_tIME_SUPPORTED)
  75.812 +   /* The tIME chunk holds the last time the displayed image data was
  75.813 +    * modified.  See the png_time struct for the contents of this struct.
  75.814 +    */
  75.815 +   png_time mod_time;
  75.816 +#endif
  75.817 +
  75.818 +#if defined(PNG_sBIT_SUPPORTED)
  75.819 +   /* The sBIT chunk specifies the number of significant high-order bits
  75.820 +    * in the pixel data.  Values are in the range [1, bit_depth], and are
  75.821 +    * only specified for the channels in the pixel data.  The contents of
  75.822 +    * the low-order bits is not specified.  Data is valid if
  75.823 +    * (valid & PNG_INFO_sBIT) is non-zero.
  75.824 +    */
  75.825 +   png_color_8 sig_bit; /* significant bits in color channels */
  75.826 +#endif
  75.827 +
  75.828 +#if defined(PNG_tRNS_SUPPORTED) || defined(PNG_READ_EXPAND_SUPPORTED) || \
  75.829 +defined(PNG_READ_BACKGROUND_SUPPORTED)
  75.830 +   /* The tRNS chunk supplies transparency data for paletted images and
  75.831 +    * other image types that don't need a full alpha channel.  There are
  75.832 +    * "num_trans" transparency values for a paletted image, stored in the
  75.833 +    * same order as the palette colors, starting from index 0.  Values
  75.834 +    * for the data are in the range [0, 255], ranging from fully transparent
  75.835 +    * to fully opaque, respectively.  For non-paletted images, there is a
  75.836 +    * single color specified that should be treated as fully transparent.
  75.837 +    * Data is valid if (valid & PNG_INFO_tRNS) is non-zero.
  75.838 +    */
  75.839 +   png_bytep trans; /* transparent values for paletted image */
  75.840 +   png_color_16 trans_values; /* transparent color for non-palette image */
  75.841 +#endif
  75.842 +
  75.843 +#if defined(PNG_bKGD_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
  75.844 +   /* The bKGD chunk gives the suggested image background color if the
  75.845 +    * display program does not have its own background color and the image
  75.846 +    * is needs to composited onto a background before display.  The colors
  75.847 +    * in "background" are normally in the same color space/depth as the
  75.848 +    * pixel data.  Data is valid if (valid & PNG_INFO_bKGD) is non-zero.
  75.849 +    */
  75.850 +   png_color_16 background;
  75.851 +#endif
  75.852 +
  75.853 +#if defined(PNG_oFFs_SUPPORTED)
  75.854 +   /* The oFFs chunk gives the offset in "offset_unit_type" units rightwards
  75.855 +    * and downwards from the top-left corner of the display, page, or other
  75.856 +    * application-specific co-ordinate space.  See the PNG_OFFSET_ defines
  75.857 +    * below for the unit types.  Valid if (valid & PNG_INFO_oFFs) non-zero.
  75.858 +    */
  75.859 +   png_int_32 x_offset; /* x offset on page */
  75.860 +   png_int_32 y_offset; /* y offset on page */
  75.861 +   png_byte offset_unit_type; /* offset units type */
  75.862 +#endif
  75.863 +
  75.864 +#if defined(PNG_pHYs_SUPPORTED)
  75.865 +   /* The pHYs chunk gives the physical pixel density of the image for
  75.866 +    * display or printing in "phys_unit_type" units (see PNG_RESOLUTION_
  75.867 +    * defines below).  Data is valid if (valid & PNG_INFO_pHYs) is non-zero.
  75.868 +    */
  75.869 +   png_uint_32 x_pixels_per_unit; /* horizontal pixel density */
  75.870 +   png_uint_32 y_pixels_per_unit; /* vertical pixel density */
  75.871 +   png_byte phys_unit_type; /* resolution type (see PNG_RESOLUTION_ below) */
  75.872 +#endif
  75.873 +
  75.874 +#if defined(PNG_hIST_SUPPORTED)
  75.875 +   /* The hIST chunk contains the relative frequency or importance of the
  75.876 +    * various palette entries, so that a viewer can intelligently select a
  75.877 +    * reduced-color palette, if required.  Data is an array of "num_palette"
  75.878 +    * values in the range [0,65535]. Data valid if (valid & PNG_INFO_hIST)
  75.879 +    * is non-zero.
  75.880 +    */
  75.881 +   png_uint_16p hist;
  75.882 +#endif
  75.883 +
  75.884 +#ifdef PNG_cHRM_SUPPORTED
  75.885 +   /* The cHRM chunk describes the CIE color characteristics of the monitor
  75.886 +    * on which the PNG was created.  This data allows the viewer to do gamut
  75.887 +    * mapping of the input image to ensure that the viewer sees the same
  75.888 +    * colors in the image as the creator.  Values are in the range
  75.889 +    * [0.0, 0.8].  Data valid if (valid & PNG_INFO_cHRM) non-zero.
  75.890 +    */
  75.891 +#ifdef PNG_FLOATING_POINT_SUPPORTED
  75.892 +   float x_white;
  75.893 +   float y_white;
  75.894 +   float x_red;
  75.895 +   float y_red;
  75.896 +   float x_green;
  75.897 +   float y_green;
  75.898 +   float x_blue;
  75.899 +   float y_blue;
  75.900 +#endif
  75.901 +#endif
  75.902 +
  75.903 +#if defined(PNG_pCAL_SUPPORTED)
  75.904 +   /* The pCAL chunk describes a transformation between the stored pixel
  75.905 +    * values and original physical data values used to create the image.
  75.906 +    * The integer range [0, 2^bit_depth - 1] maps to the floating-point
  75.907 +    * range given by [pcal_X0, pcal_X1], and are further transformed by a
  75.908 +    * (possibly non-linear) transformation function given by "pcal_type"
  75.909 +    * and "pcal_params" into "pcal_units".  Please see the PNG_EQUATION_
  75.910 +    * defines below, and the PNG-Group's PNG extensions document for a
  75.911 +    * complete description of the transformations and how they should be
  75.912 +    * implemented, and for a description of the ASCII parameter strings.
  75.913 +    * Data values are valid if (valid & PNG_INFO_pCAL) non-zero.
  75.914 +    */
  75.915 +   png_charp pcal_purpose;  /* pCAL chunk description string */
  75.916 +   png_int_32 pcal_X0;      /* minimum value */
  75.917 +   png_int_32 pcal_X1;      /* maximum value */
  75.918 +   png_charp pcal_units;    /* Latin-1 string giving physical units */
  75.919 +   png_charpp pcal_params;  /* ASCII strings containing parameter values */
  75.920 +   png_byte pcal_type;      /* equation type (see PNG_EQUATION_ below) */
  75.921 +   png_byte pcal_nparams;   /* number of parameters given in pcal_params */
  75.922 +#endif
  75.923 +
  75.924 +/* New members added in libpng-1.0.6 */
  75.925 +#ifdef PNG_FREE_ME_SUPPORTED
  75.926 +   png_uint_32 free_me;     /* flags items libpng is responsible for freeing */
  75.927 +#endif
  75.928 +
  75.929 +#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
  75.930 +   /* storage for unknown chunks that the library doesn't recognize. */
  75.931 +   png_unknown_chunkp unknown_chunks;
  75.932 +   png_size_t unknown_chunks_num;
  75.933 +#endif
  75.934 +
  75.935 +#if defined(PNG_iCCP_SUPPORTED)
  75.936 +   /* iCCP chunk data. */
  75.937 +   png_charp iccp_name;     /* profile name */
  75.938 +   png_charp iccp_profile;  /* International Color Consortium profile data */
  75.939 +                            /* Note to maintainer: should be png_bytep */
  75.940 +   png_uint_32 iccp_proflen;  /* ICC profile data length */
  75.941 +   png_byte iccp_compression; /* Always zero */
  75.942 +#endif
  75.943 +
  75.944 +#if defined(PNG_sPLT_SUPPORTED)
  75.945 +   /* data on sPLT chunks (there may be more than one). */
  75.946 +   png_sPLT_tp splt_palettes;
  75.947 +   png_uint_32 splt_palettes_num;
  75.948 +#endif
  75.949 +
  75.950 +#if defined(PNG_sCAL_SUPPORTED)
  75.951 +   /* The sCAL chunk describes the actual physical dimensions of the
  75.952 +    * subject matter of the graphic.  The chunk contains a unit specification
  75.953 +    * a byte value, and two ASCII strings representing floating-point
  75.954 +    * values.  The values are width and height corresponsing to one pixel
  75.955 +    * in the image.  This external representation is converted to double
  75.956 +    * here.  Data values are valid if (valid & PNG_INFO_sCAL) is non-zero.
  75.957 +    */
  75.958 +   png_byte scal_unit;         /* unit of physical scale */
  75.959 +#ifdef PNG_FLOATING_POINT_SUPPORTED
  75.960 +   double scal_pixel_width;    /* width of one pixel */
  75.961 +   double scal_pixel_height;   /* height of one pixel */
  75.962 +#endif
  75.963 +#ifdef PNG_FIXED_POINT_SUPPORTED
  75.964 +   png_charp scal_s_width;     /* string containing height */
  75.965 +   png_charp scal_s_height;    /* string containing width */
  75.966 +#endif
  75.967 +#endif
  75.968 +
  75.969 +#if defined(PNG_INFO_IMAGE_SUPPORTED)
  75.970 +   /* Memory has been allocated if (valid & PNG_ALLOCATED_INFO_ROWS) non-zero */
  75.971 +   /* Data valid if (valid & PNG_INFO_IDAT) non-zero */
  75.972 +   png_bytepp row_pointers;        /* the image bits */
  75.973 +#endif
  75.974 +
  75.975 +#if defined(PNG_FIXED_POINT_SUPPORTED) && defined(PNG_gAMA_SUPPORTED)
  75.976 +   png_fixed_point int_gamma; /* gamma of image, if (valid & PNG_INFO_gAMA) */
  75.977 +#endif
  75.978 +
  75.979 +#if defined(PNG_cHRM_SUPPORTED) && defined(PNG_FIXED_POINT_SUPPORTED)
  75.980 +   png_fixed_point int_x_white;
  75.981 +   png_fixed_point int_y_white;
  75.982 +   png_fixed_point int_x_red;
  75.983 +   png_fixed_point int_y_red;
  75.984 +   png_fixed_point int_x_green;
  75.985 +   png_fixed_point int_y_green;
  75.986 +   png_fixed_point int_x_blue;
  75.987 +   png_fixed_point int_y_blue;
  75.988 +#endif
  75.989 +
  75.990 +} png_info;
  75.991 +
  75.992 +typedef png_info FAR * png_infop;
  75.993 +typedef png_info FAR * FAR * png_infopp;
  75.994 +
  75.995 +/* Maximum positive integer used in PNG is (2^31)-1 */
  75.996 +#define PNG_UINT_31_MAX ((png_uint_32)0x7fffffffL)
  75.997 +#define PNG_UINT_32_MAX ((png_uint_32)(-1))
  75.998 +#define PNG_SIZE_MAX ((png_size_t)(-1))
  75.999 +#if defined(PNG_1_0_X) || defined (PNG_1_2_X)
 75.1000 +/* PNG_MAX_UINT is deprecated; use PNG_UINT_31_MAX instead. */
 75.1001 +#define PNG_MAX_UINT PNG_UINT_31_MAX
 75.1002 +#endif
 75.1003 +
 75.1004 +/* These describe the color_type field in png_info. */
 75.1005 +/* color type masks */
 75.1006 +#define PNG_COLOR_MASK_PALETTE    1
 75.1007 +#define PNG_COLOR_MASK_COLOR      2
 75.1008 +#define PNG_COLOR_MASK_ALPHA      4
 75.1009 +
 75.1010 +/* color types.  Note that not all combinations are legal */
 75.1011 +#define PNG_COLOR_TYPE_GRAY 0
 75.1012 +#define PNG_COLOR_TYPE_PALETTE  (PNG_COLOR_MASK_COLOR | PNG_COLOR_MASK_PALETTE)
 75.1013 +#define PNG_COLOR_TYPE_RGB        (PNG_COLOR_MASK_COLOR)
 75.1014 +#define PNG_COLOR_TYPE_RGB_ALPHA  (PNG_COLOR_MASK_COLOR | PNG_COLOR_MASK_ALPHA)
 75.1015 +#define PNG_COLOR_TYPE_GRAY_ALPHA (PNG_COLOR_MASK_ALPHA)
 75.1016 +/* aliases */
 75.1017 +#define PNG_COLOR_TYPE_RGBA  PNG_COLOR_TYPE_RGB_ALPHA
 75.1018 +#define PNG_COLOR_TYPE_GA  PNG_COLOR_TYPE_GRAY_ALPHA
 75.1019 +
 75.1020 +/* This is for compression type. PNG 1.0-1.2 only define the single type. */
 75.1021 +#define PNG_COMPRESSION_TYPE_BASE 0 /* Deflate method 8, 32K window */
 75.1022 +#define PNG_COMPRESSION_TYPE_DEFAULT PNG_COMPRESSION_TYPE_BASE
 75.1023 +
 75.1024 +/* This is for filter type. PNG 1.0-1.2 only define the single type. */
 75.1025 +#define PNG_FILTER_TYPE_BASE      0 /* Single row per-byte filtering */
 75.1026 +#define PNG_INTRAPIXEL_DIFFERENCING 64 /* Used only in MNG datastreams */
 75.1027 +#define PNG_FILTER_TYPE_DEFAULT   PNG_FILTER_TYPE_BASE
 75.1028 +
 75.1029 +/* These are for the interlacing type.  These values should NOT be changed. */
 75.1030 +#define PNG_INTERLACE_NONE        0 /* Non-interlaced image */
 75.1031 +#define PNG_INTERLACE_ADAM7       1 /* Adam7 interlacing */
 75.1032 +#define PNG_INTERLACE_LAST        2 /* Not a valid value */
 75.1033 +
 75.1034 +/* These are for the oFFs chunk.  These values should NOT be changed. */
 75.1035 +#define PNG_OFFSET_PIXEL          0 /* Offset in pixels */
 75.1036 +#define PNG_OFFSET_MICROMETER     1 /* Offset in micrometers (1/10^6 meter) */
 75.1037 +#define PNG_OFFSET_LAST           2 /* Not a valid value */
 75.1038 +
 75.1039 +/* These are for the pCAL chunk.  These values should NOT be changed. */
 75.1040 +#define PNG_EQUATION_LINEAR       0 /* Linear transformation */
 75.1041 +#define PNG_EQUATION_BASE_E       1 /* Exponential base e transform */
 75.1042 +#define PNG_EQUATION_ARBITRARY    2 /* Arbitrary base exponential transform */
 75.1043 +#define PNG_EQUATION_HYPERBOLIC   3 /* Hyperbolic sine transformation */
 75.1044 +#define PNG_EQUATION_LAST         4 /* Not a valid value */
 75.1045 +
 75.1046 +/* These are for the sCAL chunk.  These values should NOT be changed. */
 75.1047 +#define PNG_SCALE_UNKNOWN         0 /* unknown unit (image scale) */
 75.1048 +#define PNG_SCALE_METER           1 /* meters per pixel */
 75.1049 +#define PNG_SCALE_RADIAN          2 /* radians per pixel */
 75.1050 +#define PNG_SCALE_LAST            3 /* Not a valid value */
 75.1051 +
 75.1052 +/* These are for the pHYs chunk.  These values should NOT be changed. */
 75.1053 +#define PNG_RESOLUTION_UNKNOWN    0 /* pixels/unknown unit (aspect ratio) */
 75.1054 +#define PNG_RESOLUTION_METER      1 /* pixels/meter */
 75.1055 +#define PNG_RESOLUTION_LAST       2 /* Not a valid value */
 75.1056 +
 75.1057 +/* These are for the sRGB chunk.  These values should NOT be changed. */
 75.1058 +#define PNG_sRGB_INTENT_PERCEPTUAL 0
 75.1059 +#define PNG_sRGB_INTENT_RELATIVE   1
 75.1060 +#define PNG_sRGB_INTENT_SATURATION 2
 75.1061 +#define PNG_sRGB_INTENT_ABSOLUTE   3
 75.1062 +#define PNG_sRGB_INTENT_LAST       4 /* Not a valid value */
 75.1063 +
 75.1064 +/* This is for text chunks */
 75.1065 +#define PNG_KEYWORD_MAX_LENGTH     79
 75.1066 +
 75.1067 +/* Maximum number of entries in PLTE/sPLT/tRNS arrays */
 75.1068 +#define PNG_MAX_PALETTE_LENGTH    256
 75.1069 +
 75.1070 +/* These determine if an ancillary chunk's data has been successfully read
 75.1071 + * from the PNG header, or if the application has filled in the corresponding
 75.1072 + * data in the info_struct to be written into the output file.  The values
 75.1073 + * of the PNG_INFO_<chunk> defines should NOT be changed.
 75.1074 + */
 75.1075 +#define PNG_INFO_gAMA 0x0001
 75.1076 +#define PNG_INFO_sBIT 0x0002
 75.1077 +#define PNG_INFO_cHRM 0x0004
 75.1078 +#define PNG_INFO_PLTE 0x0008
 75.1079 +#define PNG_INFO_tRNS 0x0010
 75.1080 +#define PNG_INFO_bKGD 0x0020
 75.1081 +#define PNG_INFO_hIST 0x0040
 75.1082 +#define PNG_INFO_pHYs 0x0080
 75.1083 +#define PNG_INFO_oFFs 0x0100
 75.1084 +#define PNG_INFO_tIME 0x0200
 75.1085 +#define PNG_INFO_pCAL 0x0400
 75.1086 +#define PNG_INFO_sRGB 0x0800   /* GR-P, 0.96a */
 75.1087 +#define PNG_INFO_iCCP 0x1000   /* ESR, 1.0.6 */
 75.1088 +#define PNG_INFO_sPLT 0x2000   /* ESR, 1.0.6 */
 75.1089 +#define PNG_INFO_sCAL 0x4000   /* ESR, 1.0.6 */
 75.1090 +#define PNG_INFO_IDAT 0x8000L  /* ESR, 1.0.6 */
 75.1091 +
 75.1092 +/* This is used for the transformation routines, as some of them
 75.1093 + * change these values for the row.  It also should enable using
 75.1094 + * the routines for other purposes.
 75.1095 + */
 75.1096 +typedef struct png_row_info_struct
 75.1097 +{
 75.1098 +   png_uint_32 width; /* width of row */
 75.1099 +   png_uint_32 rowbytes; /* number of bytes in row */
 75.1100 +   png_byte color_type; /* color type of row */
 75.1101 +   png_byte bit_depth; /* bit depth of row */
 75.1102 +   png_byte channels; /* number of channels (1, 2, 3, or 4) */
 75.1103 +   png_byte pixel_depth; /* bits per pixel (depth * channels) */
 75.1104 +} png_row_info;
 75.1105 +
 75.1106 +typedef png_row_info FAR * png_row_infop;
 75.1107 +typedef png_row_info FAR * FAR * png_row_infopp;
 75.1108 +
 75.1109 +/* These are the function types for the I/O functions and for the functions
 75.1110 + * that allow the user to override the default I/O functions with his or her
 75.1111 + * own.  The png_error_ptr type should match that of user-supplied warning
 75.1112 + * and error functions, while the png_rw_ptr type should match that of the
 75.1113 + * user read/write data functions.
 75.1114 + */
 75.1115 +typedef struct png_struct_def png_struct;
 75.1116 +typedef png_struct FAR * png_structp;
 75.1117 +
 75.1118 +typedef void (PNGAPI *png_error_ptr) PNGARG((png_structp, png_const_charp));
 75.1119 +typedef void (PNGAPI *png_rw_ptr) PNGARG((png_structp, png_bytep, png_size_t));
 75.1120 +typedef void (PNGAPI *png_flush_ptr) PNGARG((png_structp));
 75.1121 +typedef void (PNGAPI *png_read_status_ptr) PNGARG((png_structp, png_uint_32,
 75.1122 +   int));
 75.1123 +typedef void (PNGAPI *png_write_status_ptr) PNGARG((png_structp, png_uint_32,
 75.1124 +   int));
 75.1125 +
 75.1126 +#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
 75.1127 +typedef void (PNGAPI *png_progressive_info_ptr) PNGARG((png_structp, png_infop));
 75.1128 +typedef void (PNGAPI *png_progressive_end_ptr) PNGARG((png_structp, png_infop));
 75.1129 +typedef void (PNGAPI *png_progressive_row_ptr) PNGARG((png_structp, png_bytep,
 75.1130 +   png_uint_32, int));
 75.1131 +#endif
 75.1132 +
 75.1133 +#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \
 75.1134 +    defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED) || \
 75.1135 +    defined(PNG_LEGACY_SUPPORTED)
 75.1136 +typedef void (PNGAPI *png_user_transform_ptr) PNGARG((png_structp,
 75.1137 +    png_row_infop, png_bytep));
 75.1138 +#endif
 75.1139 +
 75.1140 +#if defined(PNG_USER_CHUNKS_SUPPORTED)
 75.1141 +typedef int (PNGAPI *png_user_chunk_ptr) PNGARG((png_structp, png_unknown_chunkp));
 75.1142 +#endif
 75.1143 +#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
 75.1144 +typedef void (PNGAPI *png_unknown_chunk_ptr) PNGARG((png_structp));
 75.1145 +#endif
 75.1146 +
 75.1147 +/* Transform masks for the high-level interface */
 75.1148 +#define PNG_TRANSFORM_IDENTITY       0x0000    /* read and write */
 75.1149 +#define PNG_TRANSFORM_STRIP_16       0x0001    /* read only */
 75.1150 +#define PNG_TRANSFORM_STRIP_ALPHA    0x0002    /* read only */
 75.1151 +#define PNG_TRANSFORM_PACKING        0x0004    /* read and write */
 75.1152 +#define PNG_TRANSFORM_PACKSWAP       0x0008    /* read and write */
 75.1153 +#define PNG_TRANSFORM_EXPAND         0x0010    /* read only */
 75.1154 +#define PNG_TRANSFORM_INVERT_MONO    0x0020    /* read and write */
 75.1155 +#define PNG_TRANSFORM_SHIFT          0x0040    /* read and write */
 75.1156 +#define PNG_TRANSFORM_BGR            0x0080    /* read and write */
 75.1157 +#define PNG_TRANSFORM_SWAP_ALPHA     0x0100    /* read and write */
 75.1158 +#define PNG_TRANSFORM_SWAP_ENDIAN    0x0200    /* read and write */
 75.1159 +#define PNG_TRANSFORM_INVERT_ALPHA   0x0400    /* read and write */
 75.1160 +#define PNG_TRANSFORM_STRIP_FILLER   0x0800    /* WRITE only */
 75.1161 +
 75.1162 +/* Flags for MNG supported features */
 75.1163 +#define PNG_FLAG_MNG_EMPTY_PLTE     0x01
 75.1164 +#define PNG_FLAG_MNG_FILTER_64      0x04
 75.1165 +#define PNG_ALL_MNG_FEATURES        0x05
 75.1166 +
 75.1167 +typedef png_voidp (*png_malloc_ptr) PNGARG((png_structp, png_size_t));
 75.1168 +typedef void (*png_free_ptr) PNGARG((png_structp, png_voidp));
 75.1169 +
 75.1170 +/* The structure that holds the information to read and write PNG files.
 75.1171 + * The only people who need to care about what is inside of this are the
 75.1172 + * people who will be modifying the library for their own special needs.
 75.1173 + * It should NOT be accessed directly by an application, except to store
 75.1174 + * the jmp_buf.
 75.1175 + */
 75.1176 +
 75.1177 +struct png_struct_def
 75.1178 +{
 75.1179 +#ifdef PNG_SETJMP_SUPPORTED
 75.1180 +   jmp_buf jmpbuf;            /* used in png_error */
 75.1181 +#endif
 75.1182 +   png_error_ptr error_fn;    /* function for printing errors and aborting */
 75.1183 +   png_error_ptr warning_fn;  /* function for printing warnings */
 75.1184 +   png_voidp error_ptr;       /* user supplied struct for error functions */
 75.1185 +   png_rw_ptr write_data_fn;  /* function for writing output data */
 75.1186 +   png_rw_ptr read_data_fn;   /* function for reading input data */
 75.1187 +   png_voidp io_ptr;          /* ptr to application struct for I/O functions */
 75.1188 +
 75.1189 +#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
 75.1190 +   png_user_transform_ptr read_user_transform_fn; /* user read transform */
 75.1191 +#endif
 75.1192 +
 75.1193 +#if defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
 75.1194 +   png_user_transform_ptr write_user_transform_fn; /* user write transform */
 75.1195 +#endif
 75.1196 +
 75.1197 +/* These were added in libpng-1.0.2 */
 75.1198 +#if defined(PNG_USER_TRANSFORM_PTR_SUPPORTED)
 75.1199 +#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \
 75.1200 +    defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
 75.1201 +   png_voidp user_transform_ptr; /* user supplied struct for user transform */
 75.1202 +   png_byte user_transform_depth;    /* bit depth of user transformed pixels */
 75.1203 +   png_byte user_transform_channels; /* channels in user transformed pixels */
 75.1204 +#endif
 75.1205 +#endif
 75.1206 +
 75.1207 +   png_uint_32 mode;          /* tells us where we are in the PNG file */
 75.1208 +   png_uint_32 flags;         /* flags indicating various things to libpng */
 75.1209 +   png_uint_32 transformations; /* which transformations to perform */
 75.1210 +
 75.1211 +   z_stream zstream;          /* pointer to decompression structure (below) */
 75.1212 +   png_bytep zbuf;            /* buffer for zlib */
 75.1213 +   png_size_t zbuf_size;      /* size of zbuf */
 75.1214 +   int zlib_level;            /* holds zlib compression level */
 75.1215 +   int zlib_method;           /* holds zlib compression method */
 75.1216 +   int zlib_window_bits;      /* holds zlib compression window bits */
 75.1217 +   int zlib_mem_level;        /* holds zlib compression memory level */
 75.1218 +   int zlib_strategy;         /* holds zlib compression strategy */
 75.1219 +
 75.1220 +   png_uint_32 width;         /* width of image in pixels */
 75.1221 +   png_uint_32 height;        /* height of image in pixels */
 75.1222 +   png_uint_32 num_rows;      /* number of rows in current pass */
 75.1223 +   png_uint_32 usr_width;     /* width of row at start of write */
 75.1224 +   png_uint_32 rowbytes;      /* size of row in bytes */
 75.1225 +   png_uint_32 irowbytes;     /* size of current interlaced row in bytes */
 75.1226 +   png_uint_32 iwidth;        /* width of current interlaced row in pixels */
 75.1227 +   png_uint_32 row_number;    /* current row in interlace pass */
 75.1228 +   png_bytep prev_row;        /* buffer to save previous (unfiltered) row */
 75.1229 +   png_bytep row_buf;         /* buffer to save current (unfiltered) row */
 75.1230 +#ifndef PNG_NO_WRITE_FILTER
 75.1231 +   png_bytep sub_row;         /* buffer to save "sub" row when filtering */
 75.1232 +   png_bytep up_row;          /* buffer to save "up" row when filtering */
 75.1233 +   png_bytep avg_row;         /* buffer to save "avg" row when filtering */
 75.1234 +   png_bytep paeth_row;       /* buffer to save "Paeth" row when filtering */
 75.1235 +#endif
 75.1236 +   png_row_info row_info;     /* used for transformation routines */
 75.1237 +
 75.1238 +   png_uint_32 idat_size;     /* current IDAT size for read */
 75.1239 +   png_uint_32 crc;           /* current chunk CRC value */
 75.1240 +   png_colorp palette;        /* palette from the input file */
 75.1241 +   png_uint_16 num_palette;   /* number of color entries in palette */
 75.1242 +   png_uint_16 num_trans;     /* number of transparency values */
 75.1243 +   png_byte chunk_name[5];    /* null-terminated name of current chunk */
 75.1244 +   png_byte compression;      /* file compression type (always 0) */
 75.1245 +   png_byte filter;           /* file filter type (always 0) */
 75.1246 +   png_byte interlaced;       /* PNG_INTERLACE_NONE, PNG_INTERLACE_ADAM7 */
 75.1247 +   png_byte pass;             /* current interlace pass (0 - 6) */
 75.1248 +   png_byte do_filter;        /* row filter flags (see PNG_FILTER_ below ) */
 75.1249 +   png_byte color_type;       /* color type of file */
 75.1250 +   png_byte bit_depth;        /* bit depth of file */
 75.1251 +   png_byte usr_bit_depth;    /* bit depth of users row */
 75.1252 +   png_byte pixel_depth;      /* number of bits per pixel */
 75.1253 +   png_byte channels;         /* number of channels in file */
 75.1254 +   png_byte usr_channels;     /* channels at start of write */
 75.1255 +   png_byte sig_bytes;        /* magic bytes read/written from start of file */
 75.1256 +
 75.1257 +#if defined(PNG_READ_FILLER_SUPPORTED) || defined(PNG_WRITE_FILLER_SUPPORTED)
 75.1258 +#ifdef PNG_LEGACY_SUPPORTED
 75.1259 +   png_byte filler;           /* filler byte for pixel expansion */
 75.1260 +#else
 75.1261 +   png_uint_16 filler;           /* filler bytes for pixel expansion */
 75.1262 +#endif
 75.1263 +#endif
 75.1264 +
 75.1265 +#if defined(PNG_bKGD_SUPPORTED)
 75.1266 +   png_byte background_gamma_type;
 75.1267 +#  ifdef PNG_FLOATING_POINT_SUPPORTED
 75.1268 +   float background_gamma;
 75.1269 +#  endif
 75.1270 +   png_color_16 background;   /* background color in screen gamma space */
 75.1271 +#if defined(PNG_READ_GAMMA_SUPPORTED)
 75.1272 +   png_color_16 background_1; /* background normalized to gamma 1.0 */
 75.1273 +#endif
 75.1274 +#endif /* PNG_bKGD_SUPPORTED */
 75.1275 +
 75.1276 +#if defined(PNG_WRITE_FLUSH_SUPPORTED)
 75.1277 +   png_flush_ptr output_flush_fn;/* Function for flushing output */
 75.1278 +   png_uint_32 flush_dist;    /* how many rows apart to flush, 0 - no flush */
 75.1279 +   png_uint_32 flush_rows;    /* number of rows written since last flush */
 75.1280 +#endif
 75.1281 +
 75.1282 +#if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
 75.1283 +   int gamma_shift;      /* number of "insignificant" bits 16-bit gamma */
 75.1284 +#ifdef PNG_FLOATING_POINT_SUPPORTED
 75.1285 +   float gamma;          /* file gamma value */
 75.1286 +   float screen_gamma;   /* screen gamma value (display_exponent) */
 75.1287 +#endif
 75.1288 +#endif
 75.1289 +
 75.1290 +#if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
 75.1291 +   png_bytep gamma_table;     /* gamma table for 8-bit depth files */
 75.1292 +   png_bytep gamma_from_1;    /* converts from 1.0 to screen */
 75.1293 +   png_bytep gamma_to_1;      /* converts from file to 1.0 */
 75.1294 +   png_uint_16pp gamma_16_table; /* gamma table for 16-bit depth files */
 75.1295 +   png_uint_16pp gamma_16_from_1; /* converts from 1.0 to screen */
 75.1296 +   png_uint_16pp gamma_16_to_1; /* converts from file to 1.0 */
 75.1297 +#endif
 75.1298 +
 75.1299 +#if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_sBIT_SUPPORTED)
 75.1300 +   png_color_8 sig_bit;       /* significant bits in each available channel */
 75.1301 +#endif
 75.1302 +
 75.1303 +#if defined(PNG_READ_SHIFT_SUPPORTED) || defined(PNG_WRITE_SHIFT_SUPPORTED)
 75.1304 +   png_color_8 shift;         /* shift for significant bit tranformation */
 75.1305 +#endif
 75.1306 +
 75.1307 +#if defined(PNG_tRNS_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED) \
 75.1308 + || defined(PNG_READ_EXPAND_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
 75.1309 +   png_bytep trans;           /* transparency values for paletted files */
 75.1310 +   png_color_16 trans_values; /* transparency values for non-paletted files */
 75.1311 +#endif
 75.1312 +
 75.1313 +   png_read_status_ptr read_row_fn;   /* called after each row is decoded */
 75.1314 +   png_write_status_ptr write_row_fn; /* called after each row is encoded */
 75.1315 +#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
 75.1316 +   png_progressive_info_ptr info_fn; /* called after header data fully read */
 75.1317 +   png_progressive_row_ptr row_fn;   /* called after each prog. row is decoded */
 75.1318 +   png_progressive_end_ptr end_fn;   /* called after image is complete */
 75.1319 +   png_bytep save_buffer_ptr;        /* current location in save_buffer */
 75.1320 +   png_bytep save_buffer;            /* buffer for previously read data */
 75.1321 +   png_bytep current_buffer_ptr;     /* current location in current_buffer */
 75.1322 +   png_bytep current_buffer;         /* buffer for recently used data */
 75.1323 +   png_uint_32 push_length;          /* size of current input chunk */
 75.1324 +   png_uint_32 skip_length;          /* bytes to skip in input data */
 75.1325 +   png_size_t save_buffer_size;      /* amount of data now in save_buffer */
 75.1326 +   png_size_t save_buffer_max;       /* total size of save_buffer */
 75.1327 +   png_size_t buffer_size;           /* total amount of available input data */
 75.1328 +   png_size_t current_buffer_size;   /* amount of data now in current_buffer */
 75.1329 +   int process_mode;                 /* what push library is currently doing */
 75.1330 +   int cur_palette;                  /* current push library palette index */
 75.1331 +
 75.1332 +#  if defined(PNG_TEXT_SUPPORTED)
 75.1333 +     png_size_t current_text_size;   /* current size of text input data */
 75.1334 +     png_size_t current_text_left;   /* how much text left to read in input */
 75.1335 +     png_charp current_text;         /* current text chunk buffer */
 75.1336 +     png_charp current_text_ptr;     /* current location in current_text */
 75.1337 +#  endif /* PNG_TEXT_SUPPORTED */
 75.1338 +#endif /* PNG_PROGRESSIVE_READ_SUPPORTED */
 75.1339 +
 75.1340 +#if defined(__TURBOC__) && !defined(_Windows) && !defined(__FLAT__)
 75.1341 +/* for the Borland special 64K segment handler */
 75.1342 +   png_bytepp offset_table_ptr;
 75.1343 +   png_bytep offset_table;
 75.1344 +   png_uint_16 offset_table_number;
 75.1345 +   png_uint_16 offset_table_count;
 75.1346 +   png_uint_16 offset_table_count_free;
 75.1347 +#endif
 75.1348 +
 75.1349 +#if defined(PNG_READ_DITHER_SUPPORTED)
 75.1350 +   png_bytep palette_lookup;         /* lookup table for dithering */
 75.1351 +   png_bytep dither_index;           /* index translation for palette files */
 75.1352 +#endif
 75.1353 +
 75.1354 +#if defined(PNG_READ_DITHER_SUPPORTED) || defined(PNG_hIST_SUPPORTED)
 75.1355 +   png_uint_16p hist;                /* histogram */
 75.1356 +#endif
 75.1357 +
 75.1358 +#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
 75.1359 +   png_byte heuristic_method;        /* heuristic for row filter selection */
 75.1360 +   png_byte num_prev_filters;        /* number of weights for previous rows */
 75.1361 +   png_bytep prev_filters;           /* filter type(s) of previous row(s) */
 75.1362 +   png_uint_16p filter_weights;      /* weight(s) for previous line(s) */
 75.1363 +   png_uint_16p inv_filter_weights;  /* 1/weight(s) for previous line(s) */
 75.1364 +   png_uint_16p filter_costs;        /* relative filter calculation cost */
 75.1365 +   png_uint_16p inv_filter_costs;    /* 1/relative filter calculation cost */
 75.1366 +#endif
 75.1367 +
 75.1368 +#if defined(PNG_TIME_RFC1123_SUPPORTED)
 75.1369 +   png_charp time_buffer;            /* String to hold RFC 1123 time text */
 75.1370 +#endif
 75.1371 +
 75.1372 +/* New members added in libpng-1.0.6 */
 75.1373 +
 75.1374 +#ifdef PNG_FREE_ME_SUPPORTED
 75.1375 +   png_uint_32 free_me;       /* flags items libpng is responsible for freeing */
 75.1376 +#endif
 75.1377 +
 75.1378 +#if defined(PNG_USER_CHUNKS_SUPPORTED)
 75.1379 +   png_voidp user_chunk_ptr;
 75.1380 +   png_user_chunk_ptr read_user_chunk_fn; /* user read chunk handler */
 75.1381 +#endif
 75.1382 +
 75.1383 +#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
 75.1384 +   int num_chunk_list;
 75.1385 +   png_bytep chunk_list;
 75.1386 +#endif
 75.1387 +
 75.1388 +/* New members added in libpng-1.0.3 */
 75.1389 +#if defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
 75.1390 +   png_byte rgb_to_gray_status;
 75.1391 +   /* These were changed from png_byte in libpng-1.0.6 */
 75.1392 +   png_uint_16 rgb_to_gray_red_coeff;
 75.1393 +   png_uint_16 rgb_to_gray_green_coeff;
 75.1394 +   png_uint_16 rgb_to_gray_blue_coeff;
 75.1395 +#endif
 75.1396 +
 75.1397 +/* New member added in libpng-1.0.4 (renamed in 1.0.9) */
 75.1398 +#if defined(PNG_MNG_FEATURES_SUPPORTED) || \
 75.1399 +    defined(PNG_READ_EMPTY_PLTE_SUPPORTED) || \
 75.1400 +    defined(PNG_WRITE_EMPTY_PLTE_SUPPORTED)
 75.1401 +/* changed from png_byte to png_uint_32 at version 1.2.0 */
 75.1402 +#ifdef PNG_1_0_X
 75.1403 +   png_byte mng_features_permitted;
 75.1404 +#else
 75.1405 +   png_uint_32 mng_features_permitted;
 75.1406 +#endif /* PNG_1_0_X */
 75.1407 +#endif
 75.1408 +
 75.1409 +/* New member added in libpng-1.0.7 */
 75.1410 +#if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
 75.1411 +   png_fixed_point int_gamma;
 75.1412 +#endif
 75.1413 +
 75.1414 +/* New member added in libpng-1.0.9, ifdef'ed out in 1.0.12, enabled in 1.2.0 */
 75.1415 +#if defined(PNG_MNG_FEATURES_SUPPORTED)
 75.1416 +   png_byte filter_type;
 75.1417 +#endif
 75.1418 +
 75.1419 +#if defined(PNG_1_0_X)
 75.1420 +/* New member added in libpng-1.0.10, ifdef'ed out in 1.2.0 */
 75.1421 +   png_uint_32 row_buf_size;
 75.1422 +#endif
 75.1423 +
 75.1424 +/* New members added in libpng-1.2.0 */
 75.1425 +#if defined(PNG_ASSEMBLER_CODE_SUPPORTED)
 75.1426 +#  if !defined(PNG_1_0_X)
 75.1427 +#    if defined(PNG_MMX_CODE_SUPPORTED)
 75.1428 +   png_byte     mmx_bitdepth_threshold;
 75.1429 +   png_uint_32  mmx_rowbytes_threshold;
 75.1430 +#    endif
 75.1431 +   png_uint_32  asm_flags;
 75.1432 +#  endif
 75.1433 +#endif
 75.1434 +
 75.1435 +/* New members added in libpng-1.0.2 but first enabled by default in 1.2.0 */
 75.1436 +#ifdef PNG_USER_MEM_SUPPORTED
 75.1437 +   png_voidp mem_ptr;                /* user supplied struct for mem functions */
 75.1438 +   png_malloc_ptr malloc_fn;         /* function for allocating memory */
 75.1439 +   png_free_ptr free_fn;             /* function for freeing memory */
 75.1440 +#endif
 75.1441 +
 75.1442 +/* New member added in libpng-1.0.13 and 1.2.0 */
 75.1443 +   png_bytep big_row_buf;         /* buffer to save current (unfiltered) row */
 75.1444 +
 75.1445 +#if defined(PNG_READ_DITHER_SUPPORTED)
 75.1446 +/* The following three members were added at version 1.0.14 and 1.2.4 */
 75.1447 +   png_bytep dither_sort;            /* working sort array */
 75.1448 +   png_bytep index_to_palette;       /* where the original index currently is */
 75.1449 +                                     /* in the palette */
 75.1450 +   png_bytep palette_to_index;       /* which original index points to this */
 75.1451 +                                     /* palette color */
 75.1452 +#endif
 75.1453 +
 75.1454 +/* New members added in libpng-1.0.16 and 1.2.6 */
 75.1455 +   png_byte compression_type;
 75.1456 +
 75.1457 +#ifdef PNG_SET_USER_LIMITS_SUPPORTED
 75.1458 +   png_uint_32 user_width_max;
 75.1459 +   png_uint_32 user_height_max;
 75.1460 +#endif
 75.1461 +
 75.1462 +/* New member added in libpng-1.0.25 and 1.2.17 */
 75.1463 +#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
 75.1464 +   /* storage for unknown chunk that the library doesn't recognize. */
 75.1465 +   png_unknown_chunk unknown_chunk;
 75.1466 +#endif
 75.1467 +
 75.1468 +/* New members added in libpng-1.2.26 */
 75.1469 +  png_uint_32 old_big_row_buf_size, old_prev_row_size;
 75.1470 +
 75.1471 +/* New member added in libpng-1.2.30 */
 75.1472 +  png_charp chunkdata;  /* buffer for reading chunk data */
 75.1473 +
 75.1474 +};
 75.1475 +
 75.1476 +
 75.1477 +/* This triggers a compiler error in png.c, if png.c and png.h
 75.1478 + * do not agree upon the version number.
 75.1479 + */
 75.1480 +typedef png_structp version_1_2_33;
 75.1481 +
 75.1482 +typedef png_struct FAR * FAR * png_structpp;
 75.1483 +
 75.1484 +/* Here are the function definitions most commonly used.  This is not
 75.1485 + * the place to find out how to use libpng.  See libpng.txt for the
 75.1486 + * full explanation, see example.c for the summary.  This just provides
 75.1487 + * a simple one line description of the use of each function.
 75.1488 + */
 75.1489 +
 75.1490 +/* Returns the version number of the library */
 75.1491 +extern PNG_EXPORT(png_uint_32,png_access_version_number) PNGARG((void));
 75.1492 +
 75.1493 +/* Tell lib we have already handled the first <num_bytes> magic bytes.
 75.1494 + * Handling more than 8 bytes from the beginning of the file is an error.
 75.1495 + */
 75.1496 +extern PNG_EXPORT(void,png_set_sig_bytes) PNGARG((png_structp png_ptr,
 75.1497 +   int num_bytes));
 75.1498 +
 75.1499 +/* Check sig[start] through sig[start + num_to_check - 1] to see if it's a
 75.1500 + * PNG file.  Returns zero if the supplied bytes match the 8-byte PNG
 75.1501 + * signature, and non-zero otherwise.  Having num_to_check == 0 or
 75.1502 + * start > 7 will always fail (ie return non-zero).
 75.1503 + */
 75.1504 +extern PNG_EXPORT(int,png_sig_cmp) PNGARG((png_bytep sig, png_size_t start,
 75.1505 +   png_size_t num_to_check));
 75.1506 +
 75.1507 +/* Simple signature checking function.  This is the same as calling
 75.1508 + * png_check_sig(sig, n) := !png_sig_cmp(sig, 0, n).
 75.1509 + */
 75.1510 +extern PNG_EXPORT(int,png_check_sig) PNGARG((png_bytep sig, int num));
 75.1511 +
 75.1512 +/* Allocate and initialize png_ptr struct for reading, and any other memory. */
 75.1513 +extern PNG_EXPORT(png_structp,png_create_read_struct)
 75.1514 +   PNGARG((png_const_charp user_png_ver, png_voidp error_ptr,
 75.1515 +   png_error_ptr error_fn, png_error_ptr warn_fn));
 75.1516 +
 75.1517 +/* Allocate and initialize png_ptr struct for writing, and any other memory */
 75.1518 +extern PNG_EXPORT(png_structp,png_create_write_struct)
 75.1519 +   PNGARG((png_const_charp user_png_ver, png_voidp error_ptr,
 75.1520 +   png_error_ptr error_fn, png_error_ptr warn_fn));
 75.1521 +
 75.1522 +#ifdef PNG_WRITE_SUPPORTED
 75.1523 +extern PNG_EXPORT(png_uint_32,png_get_compression_buffer_size)
 75.1524 +   PNGARG((png_structp png_ptr));
 75.1525 +#endif
 75.1526 +
 75.1527 +#ifdef PNG_WRITE_SUPPORTED
 75.1528 +extern PNG_EXPORT(void,png_set_compression_buffer_size)
 75.1529 +   PNGARG((png_structp png_ptr, png_uint_32 size));
 75.1530 +#endif
 75.1531 +
 75.1532 +/* Reset the compression stream */
 75.1533 +extern PNG_EXPORT(int,png_reset_zstream) PNGARG((png_structp png_ptr));
 75.1534 +
 75.1535 +/* New functions added in libpng-1.0.2 (not enabled by default until 1.2.0) */
 75.1536 +#ifdef PNG_USER_MEM_SUPPORTED
 75.1537 +extern PNG_EXPORT(png_structp,png_create_read_struct_2)
 75.1538 +   PNGARG((png_const_charp user_png_ver, png_voidp error_ptr,
 75.1539 +   png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
 75.1540 +   png_malloc_ptr malloc_fn, png_free_ptr free_fn));
 75.1541 +extern PNG_EXPORT(png_structp,png_create_write_struct_2)
 75.1542 +   PNGARG((png_const_charp user_png_ver, png_voidp error_ptr,
 75.1543 +   png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
 75.1544 +   png_malloc_ptr malloc_fn, png_free_ptr free_fn));
 75.1545 +#endif
 75.1546 +
 75.1547 +/* Write a PNG chunk - size, type, (optional) data, CRC. */
 75.1548 +extern PNG_EXPORT(void,png_write_chunk) PNGARG((png_structp png_ptr,
 75.1549 +   png_bytep chunk_name, png_bytep data, png_size_t length));
 75.1550 +
 75.1551 +/* Write the start of a PNG chunk - length and chunk name. */
 75.1552 +extern PNG_EXPORT(void,png_write_chunk_start) PNGARG((png_structp png_ptr,
 75.1553 +   png_bytep chunk_name, png_uint_32 length));
 75.1554 +
 75.1555 +/* Write the data of a PNG chunk started with png_write_chunk_start(). */
 75.1556 +extern PNG_EXPORT(void,png_write_chunk_data) PNGARG((png_structp png_ptr,
 75.1557 +   png_bytep data, png_size_t length));
 75.1558 +
 75.1559 +/* Finish a chunk started with png_write_chunk_start() (includes CRC). */
 75.1560 +extern PNG_EXPORT(void,png_write_chunk_end) PNGARG((png_structp png_ptr));
 75.1561 +
 75.1562 +/* Allocate and initialize the info structure */
 75.1563 +extern PNG_EXPORT(png_infop,png_create_info_struct)
 75.1564 +   PNGARG((png_structp png_ptr));
 75.1565 +
 75.1566 +#if defined(PNG_1_0_X) || defined (PNG_1_2_X)
 75.1567 +/* Initialize the info structure (old interface - DEPRECATED) */
 75.1568 +extern PNG_EXPORT(void,png_info_init) PNGARG((png_infop info_ptr));
 75.1569 +#undef png_info_init
 75.1570 +#define png_info_init(info_ptr) png_info_init_3(&info_ptr,\
 75.1571 +    png_sizeof(png_info));
 75.1572 +#endif
 75.1573 +
 75.1574 +extern PNG_EXPORT(void,png_info_init_3) PNGARG((png_infopp info_ptr,
 75.1575 +    png_size_t png_info_struct_size));
 75.1576 +
 75.1577 +/* Writes all the PNG information before the image. */
 75.1578 +extern PNG_EXPORT(void,png_write_info_before_PLTE) PNGARG((png_structp png_ptr,
 75.1579 +   png_infop info_ptr));
 75.1580 +extern PNG_EXPORT(void,png_write_info) PNGARG((png_structp png_ptr,
 75.1581 +   png_infop info_ptr));
 75.1582 +
 75.1583 +#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
 75.1584 +/* read the information before the actual image data. */
 75.1585 +extern PNG_EXPORT(void,png_read_info) PNGARG((png_structp png_ptr,
 75.1586 +   png_infop info_ptr));
 75.1587 +#endif
 75.1588 +
 75.1589 +#if defined(PNG_TIME_RFC1123_SUPPORTED)
 75.1590 +extern PNG_EXPORT(png_charp,png_convert_to_rfc1123)
 75.1591 +   PNGARG((png_structp png_ptr, png_timep ptime));
 75.1592 +#endif
 75.1593 +
 75.1594 +#if !defined(_WIN32_WCE)
 75.1595 +/* "time.h" functions are not supported on WindowsCE */
 75.1596 +#if defined(PNG_WRITE_tIME_SUPPORTED)
 75.1597 +/* convert from a struct tm to png_time */
 75.1598 +extern PNG_EXPORT(void,png_convert_from_struct_tm) PNGARG((png_timep ptime,
 75.1599 +   struct tm FAR * ttime));
 75.1600 +
 75.1601 +/* convert from time_t to png_time.  Uses gmtime() */
 75.1602 +extern PNG_EXPORT(void,png_convert_from_time_t) PNGARG((png_timep ptime,
 75.1603 +   time_t ttime));
 75.1604 +#endif /* PNG_WRITE_tIME_SUPPORTED */
 75.1605 +#endif /* _WIN32_WCE */
 75.1606 +
 75.1607 +#if defined(PNG_READ_EXPAND_SUPPORTED)
 75.1608 +/* Expand data to 24-bit RGB, or 8-bit grayscale, with alpha if available. */
 75.1609 +extern PNG_EXPORT(void,png_set_expand) PNGARG((png_structp png_ptr));
 75.1610 +#if !defined(PNG_1_0_X)
 75.1611 +extern PNG_EXPORT(void,png_set_expand_gray_1_2_4_to_8) PNGARG((png_structp
 75.1612 +  png_ptr));
 75.1613 +#endif
 75.1614 +extern PNG_EXPORT(void,png_set_palette_to_rgb) PNGARG((png_structp png_ptr));
 75.1615 +extern PNG_EXPORT(void,png_set_tRNS_to_alpha) PNGARG((png_structp png_ptr));
 75.1616 +#if defined(PNG_1_0_X) || defined (PNG_1_2_X)
 75.1617 +/* Deprecated */
 75.1618 +extern PNG_EXPORT(void,png_set_gray_1_2_4_to_8) PNGARG((png_structp png_ptr));
 75.1619 +#endif
 75.1620 +#endif
 75.1621 +
 75.1622 +#if defined(PNG_READ_BGR_SUPPORTED) || defined(PNG_WRITE_BGR_SUPPORTED)
 75.1623 +/* Use blue, green, red order for pixels. */
 75.1624 +extern PNG_EXPORT(void,png_set_bgr) PNGARG((png_structp png_ptr));
 75.1625 +#endif
 75.1626 +
 75.1627 +#if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED)
 75.1628 +/* Expand the grayscale to 24-bit RGB if necessary. */
 75.1629 +extern PNG_EXPORT(void,png_set_gray_to_rgb) PNGARG((png_structp png_ptr));
 75.1630 +#endif
 75.1631 +
 75.1632 +#if defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
 75.1633 +/* Reduce RGB to grayscale. */
 75.1634 +#ifdef PNG_FLOATING_POINT_SUPPORTED
 75.1635 +extern PNG_EXPORT(void,png_set_rgb_to_gray) PNGARG((png_structp png_ptr,
 75.1636 +   int error_action, double red, double green ));
 75.1637 +#endif
 75.1638 +extern PNG_EXPORT(void,png_set_rgb_to_gray_fixed) PNGARG((png_structp png_ptr,
 75.1639 +   int error_action, png_fixed_point red, png_fixed_point green ));
 75.1640 +extern PNG_EXPORT(png_byte,png_get_rgb_to_gray_status) PNGARG((png_structp
 75.1641 +   png_ptr));
 75.1642 +#endif
 75.1643 +
 75.1644 +extern PNG_EXPORT(void,png_build_grayscale_palette) PNGARG((int bit_depth,
 75.1645 +   png_colorp palette));
 75.1646 +
 75.1647 +#if defined(PNG_READ_STRIP_ALPHA_SUPPORTED)
 75.1648 +extern PNG_EXPORT(void,png_set_strip_alpha) PNGARG((png_structp png_ptr));
 75.1649 +#endif
 75.1650 +
 75.1651 +#if defined(PNG_READ_SWAP_ALPHA_SUPPORTED) || \
 75.1652 +    defined(PNG_WRITE_SWAP_ALPHA_SUPPORTED)
 75.1653 +extern PNG_EXPORT(void,png_set_swap_alpha) PNGARG((png_structp png_ptr));
 75.1654 +#endif
 75.1655 +
 75.1656 +#if defined(PNG_READ_INVERT_ALPHA_SUPPORTED) || \
 75.1657 +    defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED)
 75.1658 +extern PNG_EXPORT(void,png_set_invert_alpha) PNGARG((png_structp png_ptr));
 75.1659 +#endif
 75.1660 +
 75.1661 +#if defined(PNG_READ_FILLER_SUPPORTED) || defined(PNG_WRITE_FILLER_SUPPORTED)
 75.1662 +/* Add a filler byte to 8-bit Gray or 24-bit RGB images. */
 75.1663 +extern PNG_EXPORT(void,png_set_filler) PNGARG((png_structp png_ptr,
 75.1664 +   png_uint_32 filler, int flags));
 75.1665 +/* The values of the PNG_FILLER_ defines should NOT be changed */
 75.1666 +#define PNG_FILLER_BEFORE 0
 75.1667 +#define PNG_FILLER_AFTER 1
 75.1668 +/* Add an alpha byte to 8-bit Gray or 24-bit RGB images. */
 75.1669 +#if !defined(PNG_1_0_X)
 75.1670 +extern PNG_EXPORT(void,png_set_add_alpha) PNGARG((png_structp png_ptr,
 75.1671 +   png_uint_32 filler, int flags));
 75.1672 +#endif
 75.1673 +#endif /* PNG_READ_FILLER_SUPPORTED || PNG_WRITE_FILLER_SUPPORTED */
 75.1674 +
 75.1675 +#if defined(PNG_READ_SWAP_SUPPORTED) || defined(PNG_WRITE_SWAP_SUPPORTED)
 75.1676 +/* Swap bytes in 16-bit depth files. */
 75.1677 +extern PNG_EXPORT(void,png_set_swap) PNGARG((png_structp png_ptr));
 75.1678 +#endif
 75.1679 +
 75.1680 +#if defined(PNG_READ_PACK_SUPPORTED) || defined(PNG_WRITE_PACK_SUPPORTED)
 75.1681 +/* Use 1 byte per pixel in 1, 2, or 4-bit depth files. */
 75.1682 +extern PNG_EXPORT(void,png_set_packing) PNGARG((png_structp png_ptr));
 75.1683 +#endif
 75.1684 +
 75.1685 +#if defined(PNG_READ_PACKSWAP_SUPPORTED) || defined(PNG_WRITE_PACKSWAP_SUPPORTED)
 75.1686 +/* Swap packing order of pixels in bytes. */
 75.1687 +extern PNG_EXPORT(void,png_set_packswap) PNGARG((png_structp png_ptr));
 75.1688 +#endif
 75.1689 +
 75.1690 +#if defined(PNG_READ_SHIFT_SUPPORTED) || defined(PNG_WRITE_SHIFT_SUPPORTED)
 75.1691 +/* Converts files to legal bit depths. */
 75.1692 +extern PNG_EXPORT(void,png_set_shift) PNGARG((png_structp png_ptr,
 75.1693 +   png_color_8p true_bits));
 75.1694 +#endif
 75.1695 +
 75.1696 +#if defined(PNG_READ_INTERLACING_SUPPORTED) || \
 75.1697 +    defined(PNG_WRITE_INTERLACING_SUPPORTED)
 75.1698 +/* Have the code handle the interlacing.  Returns the number of passes. */
 75.1699 +extern PNG_EXPORT(int,png_set_interlace_handling) PNGARG((png_structp png_ptr));
 75.1700 +#endif
 75.1701 +
 75.1702 +#if defined(PNG_READ_INVERT_SUPPORTED) || defined(PNG_WRITE_INVERT_SUPPORTED)
 75.1703 +/* Invert monochrome files */
 75.1704 +extern PNG_EXPORT(void,png_set_invert_mono) PNGARG((png_structp png_ptr));
 75.1705 +#endif
 75.1706 +
 75.1707 +#if defined(PNG_READ_BACKGROUND_SUPPORTED)
 75.1708 +/* Handle alpha and tRNS by replacing with a background color. */
 75.1709 +#ifdef PNG_FLOATING_POINT_SUPPORTED
 75.1710 +extern PNG_EXPORT(void,png_set_background) PNGARG((png_structp png_ptr,
 75.1711 +   png_color_16p background_color, int background_gamma_code,
 75.1712 +   int need_expand, double background_gamma));
 75.1713 +#endif
 75.1714 +#define PNG_BACKGROUND_GAMMA_UNKNOWN 0
 75.1715 +#define PNG_BACKGROUND_GAMMA_SCREEN  1
 75.1716 +#define PNG_BACKGROUND_GAMMA_FILE    2
 75.1717 +#define PNG_BACKGROUND_GAMMA_UNIQUE  3
 75.1718 +#endif
 75.1719 +
 75.1720 +#if defined(PNG_READ_16_TO_8_SUPPORTED)
 75.1721 +/* strip the second byte of information from a 16-bit depth file. */
 75.1722 +extern PNG_EXPORT(void,png_set_strip_16) PNGARG((png_structp png_ptr));
 75.1723 +#endif
 75.1724 +
 75.1725 +#if defined(PNG_READ_DITHER_SUPPORTED)
 75.1726 +/* Turn on dithering, and reduce the palette to the number of colors available. */
 75.1727 +extern PNG_EXPORT(void,png_set_dither) PNGARG((png_structp png_ptr,
 75.1728 +   png_colorp palette, int num_palette, int maximum_colors,
 75.1729 +   png_uint_16p histogram, int full_dither));
 75.1730 +#endif
 75.1731 +
 75.1732 +#if defined(PNG_READ_GAMMA_SUPPORTED)
 75.1733 +/* Handle gamma correction. Screen_gamma=(display_exponent) */
 75.1734 +#ifdef PNG_FLOATING_POINT_SUPPORTED
 75.1735 +extern PNG_EXPORT(void,png_set_gamma) PNGARG((png_structp png_ptr,
 75.1736 +   double screen_gamma, double default_file_gamma));
 75.1737 +#endif
 75.1738 +#endif
 75.1739 +
 75.1740 +#if defined(PNG_1_0_X) || defined (PNG_1_2_X)
 75.1741 +#if defined(PNG_READ_EMPTY_PLTE_SUPPORTED) || \
 75.1742 +    defined(PNG_WRITE_EMPTY_PLTE_SUPPORTED)
 75.1743 +/* Permit or disallow empty PLTE (0: not permitted, 1: permitted) */
 75.1744 +/* Deprecated and will be removed.  Use png_permit_mng_features() instead. */
 75.1745 +extern PNG_EXPORT(void,png_permit_empty_plte) PNGARG((png_structp png_ptr,
 75.1746 +   int empty_plte_permitted));
 75.1747 +#endif
 75.1748 +#endif
 75.1749 +
 75.1750 +#if defined(PNG_WRITE_FLUSH_SUPPORTED)
 75.1751 +/* Set how many lines between output flushes - 0 for no flushing */
 75.1752 +extern PNG_EXPORT(void,png_set_flush) PNGARG((png_structp png_ptr, int nrows));
 75.1753 +/* Flush the current PNG output buffer */
 75.1754 +extern PNG_EXPORT(void,png_write_flush) PNGARG((png_structp png_ptr));
 75.1755 +#endif
 75.1756 +
 75.1757 +/* optional update palette with requested transformations */
 75.1758 +extern PNG_EXPORT(void,png_start_read_image) PNGARG((png_structp png_ptr));
 75.1759 +
 75.1760 +/* optional call to update the users info structure */
 75.1761 +extern PNG_EXPORT(void,png_read_update_info) PNGARG((png_structp png_ptr,
 75.1762 +   png_infop info_ptr));
 75.1763 +
 75.1764 +#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
 75.1765 +/* read one or more rows of image data. */
 75.1766 +extern PNG_EXPORT(void,png_read_rows) PNGARG((png_structp png_ptr,
 75.1767 +   png_bytepp row, png_bytepp display_row, png_uint_32 num_rows));
 75.1768 +#endif
 75.1769 +
 75.1770 +#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
 75.1771 +/* read a row of data. */
 75.1772 +extern PNG_EXPORT(void,png_read_row) PNGARG((png_structp png_ptr,
 75.1773 +   png_bytep row,
 75.1774 +   png_bytep display_row));
 75.1775 +#endif
 75.1776 +
 75.1777 +#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
 75.1778 +/* read the whole image into memory at once. */
 75.1779 +extern PNG_EXPORT(void,png_read_image) PNGARG((png_structp png_ptr,
 75.1780 +   png_bytepp image));
 75.1781 +#endif
 75.1782 +
 75.1783 +/* write a row of image data */
 75.1784 +extern PNG_EXPORT(void,png_write_row) PNGARG((png_structp png_ptr,
 75.1785 +   png_bytep row));
 75.1786 +
 75.1787 +/* write a few rows of image data */
 75.1788 +extern PNG_EXPORT(void,png_write_rows) PNGARG((png_structp png_ptr,
 75.1789 +   png_bytepp row, png_uint_32 num_rows));
 75.1790 +
 75.1791 +/* write the image data */
 75.1792 +extern PNG_EXPORT(void,png_write_image) PNGARG((png_structp png_ptr,
 75.1793 +   png_bytepp image));
 75.1794 +
 75.1795 +/* writes the end of the PNG file. */
 75.1796 +extern PNG_EXPORT(void,png_write_end) PNGARG((png_structp png_ptr,
 75.1797 +   png_infop info_ptr));
 75.1798 +
 75.1799 +#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
 75.1800 +/* read the end of the PNG file. */
 75.1801 +extern PNG_EXPORT(void,png_read_end) PNGARG((png_structp png_ptr,
 75.1802 +   png_infop info_ptr));
 75.1803 +#endif
 75.1804 +
 75.1805 +/* free any memory associated with the png_info_struct */
 75.1806 +extern PNG_EXPORT(void,png_destroy_info_struct) PNGARG((png_structp png_ptr,
 75.1807 +   png_infopp info_ptr_ptr));
 75.1808 +
 75.1809 +/* free any memory associated with the png_struct and the png_info_structs */
 75.1810 +extern PNG_EXPORT(void,png_destroy_read_struct) PNGARG((png_structpp
 75.1811 +   png_ptr_ptr, png_infopp info_ptr_ptr, png_infopp end_info_ptr_ptr));
 75.1812 +
 75.1813 +/* free all memory used by the read (old method - NOT DLL EXPORTED) */
 75.1814 +extern void png_read_destroy PNGARG((png_structp png_ptr, png_infop info_ptr,
 75.1815 +   png_infop end_info_ptr));
 75.1816 +
 75.1817 +/* free any memory associated with the png_struct and the png_info_structs */
 75.1818 +extern PNG_EXPORT(void,png_destroy_write_struct)
 75.1819 +   PNGARG((png_structpp png_ptr_ptr, png_infopp info_ptr_ptr));
 75.1820 +
 75.1821 +/* free any memory used in png_ptr struct (old method - NOT DLL EXPORTED) */
 75.1822 +extern void png_write_destroy PNGARG((png_structp png_ptr));
 75.1823 +
 75.1824 +/* set the libpng method of handling chunk CRC errors */
 75.1825 +extern PNG_EXPORT(void,png_set_crc_action) PNGARG((png_structp png_ptr,
 75.1826 +   int crit_action, int ancil_action));
 75.1827 +
 75.1828 +/* Values for png_set_crc_action() to say how to handle CRC errors in
 75.1829 + * ancillary and critical chunks, and whether to use the data contained
 75.1830 + * therein.  Note that it is impossible to "discard" data in a critical
 75.1831 + * chunk.  For versions prior to 0.90, the action was always error/quit,
 75.1832 + * whereas in version 0.90 and later, the action for CRC errors in ancillary
 75.1833 + * chunks is warn/discard.  These values should NOT be changed.
 75.1834 + *
 75.1835 + *      value                       action:critical     action:ancillary
 75.1836 + */
 75.1837 +#define PNG_CRC_DEFAULT       0  /* error/quit          warn/discard data */
 75.1838 +#define PNG_CRC_ERROR_QUIT    1  /* error/quit          error/quit        */
 75.1839 +#define PNG_CRC_WARN_DISCARD  2  /* (INVALID)           warn/discard data */
 75.1840 +#define PNG_CRC_WARN_USE      3  /* warn/use data       warn/use data     */
 75.1841 +#define PNG_CRC_QUIET_USE     4  /* quiet/use data      quiet/use data    */
 75.1842 +#define PNG_CRC_NO_CHANGE     5  /* use current value   use current value */
 75.1843 +
 75.1844 +/* These functions give the user control over the scan-line filtering in
 75.1845 + * libpng and the compression methods used by zlib.  These functions are
 75.1846 + * mainly useful for testing, as the defaults should work with most users.
 75.1847 + * Those users who are tight on memory or want faster performance at the
 75.1848 + * expense of compression can modify them.  See the compression library
 75.1849 + * header file (zlib.h) for an explination of the compression functions.
 75.1850 + */
 75.1851 +
 75.1852 +/* set the filtering method(s) used by libpng.  Currently, the only valid
 75.1853 + * value for "method" is 0.
 75.1854 + */
 75.1855 +extern PNG_EXPORT(void,png_set_filter) PNGARG((png_structp png_ptr, int method,
 75.1856 +   int filters));
 75.1857 +
 75.1858 +/* Flags for png_set_filter() to say which filters to use.  The flags
 75.1859 + * are chosen so that they don't conflict with real filter types
 75.1860 + * below, in case they are supplied instead of the #defined constants.
 75.1861 + * These values should NOT be changed.
 75.1862 + */
 75.1863 +#define PNG_NO_FILTERS     0x00
 75.1864 +#define PNG_FILTER_NONE    0x08
 75.1865 +#define PNG_FILTER_SUB     0x10
 75.1866 +#define PNG_FILTER_UP      0x20
 75.1867 +#define PNG_FILTER_AVG     0x40
 75.1868 +#define PNG_FILTER_PAETH   0x80
 75.1869 +#define PNG_ALL_FILTERS (PNG_FILTER_NONE | PNG_FILTER_SUB | PNG_FILTER_UP | \
 75.1870 +                         PNG_FILTER_AVG | PNG_FILTER_PAETH)
 75.1871 +
 75.1872 +/* Filter values (not flags) - used in pngwrite.c, pngwutil.c for now.
 75.1873 + * These defines should NOT be changed.
 75.1874 + */
 75.1875 +#define PNG_FILTER_VALUE_NONE  0
 75.1876 +#define PNG_FILTER_VALUE_SUB   1
 75.1877 +#define PNG_FILTER_VALUE_UP    2
 75.1878 +#define PNG_FILTER_VALUE_AVG   3
 75.1879 +#define PNG_FILTER_VALUE_PAETH 4
 75.1880 +#define PNG_FILTER_VALUE_LAST  5
 75.1881 +
 75.1882 +#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED) /* EXPERIMENTAL */
 75.1883 +/* The "heuristic_method" is given by one of the PNG_FILTER_HEURISTIC_
 75.1884 + * defines, either the default (minimum-sum-of-absolute-differences), or
 75.1885 + * the experimental method (weighted-minimum-sum-of-absolute-differences).
 75.1886 + *
 75.1887 + * Weights are factors >= 1.0, indicating how important it is to keep the
 75.1888 + * filter type consistent between rows.  Larger numbers mean the current
 75.1889 + * filter is that many times as likely to be the same as the "num_weights"
 75.1890 + * previous filters.  This is cumulative for each previous row with a weight.
 75.1891 + * There needs to be "num_weights" values in "filter_weights", or it can be
 75.1892 + * NULL if the weights aren't being specified.  Weights have no influence on
 75.1893 + * the selection of the first row filter.  Well chosen weights can (in theory)
 75.1894 + * improve the compression for a given image.
 75.1895 + *
 75.1896 + * Costs are factors >= 1.0 indicating the relative decoding costs of a
 75.1897 + * filter type.  Higher costs indicate more decoding expense, and are
 75.1898 + * therefore less likely to be selected over a filter with lower computational
 75.1899 + * costs.  There needs to be a value in "filter_costs" for each valid filter
 75.1900 + * type (given by PNG_FILTER_VALUE_LAST), or it can be NULL if you aren't
 75.1901 + * setting the costs.  Costs try to improve the speed of decompression without
 75.1902 + * unduly increasing the compressed image size.
 75.1903 + *
 75.1904 + * A negative weight or cost indicates the default value is to be used, and
 75.1905 + * values in the range [0.0, 1.0) indicate the value is to remain unchanged.
 75.1906 + * The default values for both weights and costs are currently 1.0, but may
 75.1907 + * change if good general weighting/cost heuristics can be found.  If both
 75.1908 + * the weights and costs are set to 1.0, this degenerates the WEIGHTED method
 75.1909 + * to the UNWEIGHTED method, but with added encoding time/computation.
 75.1910 + */
 75.1911 +#ifdef PNG_FLOATING_POINT_SUPPORTED
 75.1912 +extern PNG_EXPORT(void,png_set_filter_heuristics) PNGARG((png_structp png_ptr,
 75.1913 +   int heuristic_method, int num_weights, png_doublep filter_weights,
 75.1914 +   png_doublep filter_costs));
 75.1915 +#endif
 75.1916 +#endif /*  PNG_WRITE_WEIGHTED_FILTER_SUPPORTED */
 75.1917 +
 75.1918 +/* Heuristic used for row filter selection.  These defines should NOT be
 75.1919 + * changed.
 75.1920 + */
 75.1921 +#define PNG_FILTER_HEURISTIC_DEFAULT    0  /* Currently "UNWEIGHTED" */
 75.1922 +#define PNG_FILTER_HEURISTIC_UNWEIGHTED 1  /* Used by libpng < 0.95 */
 75.1923 +#define PNG_FILTER_HEURISTIC_WEIGHTED   2  /* Experimental feature */
 75.1924 +#define PNG_FILTER_HEURISTIC_LAST       3  /* Not a valid value */
 75.1925 +
 75.1926 +/* Set the library compression level.  Currently, valid values range from
 75.1927 + * 0 - 9, corresponding directly to the zlib compression levels 0 - 9
 75.1928 + * (0 - no compression, 9 - "maximal" compression).  Note that tests have
 75.1929 + * shown that zlib compression levels 3-6 usually perform as well as level 9
 75.1930 + * for PNG images, and do considerably fewer caclulations.  In the future,
 75.1931 + * these values may not correspond directly to the zlib compression levels.
 75.1932 + */
 75.1933 +extern PNG_EXPORT(void,png_set_compression_level) PNGARG((png_structp png_ptr,
 75.1934 +   int level));
 75.1935 +
 75.1936 +extern PNG_EXPORT(void,png_set_compression_mem_level)
 75.1937 +   PNGARG((png_structp png_ptr, int mem_level));
 75.1938 +
 75.1939 +extern PNG_EXPORT(void,png_set_compression_strategy)
 75.1940 +   PNGARG((png_structp png_ptr, int strategy));
 75.1941 +
 75.1942 +extern PNG_EXPORT(void,png_set_compression_window_bits)
 75.1943 +   PNGARG((png_structp png_ptr, int window_bits));
 75.1944 +
 75.1945 +extern PNG_EXPORT(void,png_set_compression_method) PNGARG((png_structp png_ptr,
 75.1946 +   int method));
 75.1947 +
 75.1948 +/* These next functions are called for input/output, memory, and error
 75.1949 + * handling.  They are in the file pngrio.c, pngwio.c, and pngerror.c,
 75.1950 + * and call standard C I/O routines such as fread(), fwrite(), and
 75.1951 + * fprintf().  These functions can be made to use other I/O routines
 75.1952 + * at run time for those applications that need to handle I/O in a
 75.1953 + * different manner by calling png_set_???_fn().  See libpng.txt for
 75.1954 + * more information.
 75.1955 + */
 75.1956 +
 75.1957 +#if !defined(PNG_NO_STDIO)
 75.1958 +/* Initialize the input/output for the PNG file to the default functions. */
 75.1959 +extern PNG_EXPORT(void,png_init_io) PNGARG((png_structp png_ptr, png_FILE_p fp));
 75.1960 +#endif
 75.1961 +
 75.1962 +/* Replace the (error and abort), and warning functions with user
 75.1963 + * supplied functions.  If no messages are to be printed you must still
 75.1964 + * write and use replacement functions. The replacement error_fn should
 75.1965 + * still do a longjmp to the last setjmp location if you are using this
 75.1966 + * method of error handling.  If error_fn or warning_fn is NULL, the
 75.1967 + * default function will be used.
 75.1968 + */
 75.1969 +
 75.1970 +extern PNG_EXPORT(void,png_set_error_fn) PNGARG((png_structp png_ptr,
 75.1971 +   png_voidp error_ptr, png_error_ptr error_fn, png_error_ptr warning_fn));
 75.1972 +
 75.1973 +/* Return the user pointer associated with the error functions */
 75.1974 +extern PNG_EXPORT(png_voidp,png_get_error_ptr) PNGARG((png_structp png_ptr));
 75.1975 +
 75.1976 +/* Replace the default data output functions with a user supplied one(s).
 75.1977 + * If buffered output is not used, then output_flush_fn can be set to NULL.
 75.1978 + * If PNG_WRITE_FLUSH_SUPPORTED is not defined at libpng compile time
 75.1979 + * output_flush_fn will be ignored (and thus can be NULL).
 75.1980 + */
 75.1981 +extern PNG_EXPORT(void,png_set_write_fn) PNGARG((png_structp png_ptr,
 75.1982 +   png_voidp io_ptr, png_rw_ptr write_data_fn, png_flush_ptr output_flush_fn));
 75.1983 +
 75.1984 +/* Replace the default data input function with a user supplied one. */
 75.1985 +extern PNG_EXPORT(void,png_set_read_fn) PNGARG((png_structp png_ptr,
 75.1986 +   png_voidp io_ptr, png_rw_ptr read_data_fn));
 75.1987 +
 75.1988 +/* Return the user pointer associated with the I/O functions */
 75.1989 +extern PNG_EXPORT(png_voidp,png_get_io_ptr) PNGARG((png_structp png_ptr));
 75.1990 +
 75.1991 +extern PNG_EXPORT(void,png_set_read_status_fn) PNGARG((png_structp png_ptr,
 75.1992 +   png_read_status_ptr read_row_fn));
 75.1993 +
 75.1994 +extern PNG_EXPORT(void,png_set_write_status_fn) PNGARG((png_structp png_ptr,
 75.1995 +   png_write_status_ptr write_row_fn));
 75.1996 +
 75.1997 +#ifdef PNG_USER_MEM_SUPPORTED
 75.1998 +/* Replace the default memory allocation functions with user supplied one(s). */
 75.1999 +extern PNG_EXPORT(void,png_set_mem_fn) PNGARG((png_structp png_ptr,
 75.2000 +   png_voidp mem_ptr, png_malloc_ptr malloc_fn, png_free_ptr free_fn));
 75.2001 +/* Return the user pointer associated with the memory functions */
 75.2002 +extern PNG_EXPORT(png_voidp,png_get_mem_ptr) PNGARG((png_structp png_ptr));
 75.2003 +#endif
 75.2004 +
 75.2005 +#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \
 75.2006 +    defined(PNG_LEGACY_SUPPORTED)
 75.2007 +extern PNG_EXPORT(void,png_set_read_user_transform_fn) PNGARG((png_structp
 75.2008 +   png_ptr, png_user_transform_ptr read_user_transform_fn));
 75.2009 +#endif
 75.2010 +
 75.2011 +#if defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED) || \
 75.2012 +    defined(PNG_LEGACY_SUPPORTED)
 75.2013 +extern PNG_EXPORT(void,png_set_write_user_transform_fn) PNGARG((png_structp
 75.2014 +   png_ptr, png_user_transform_ptr write_user_transform_fn));
 75.2015 +#endif
 75.2016 +
 75.2017 +#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \
 75.2018 +    defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED) || \
 75.2019 +    defined(PNG_LEGACY_SUPPORTED)
 75.2020 +extern PNG_EXPORT(void,png_set_user_transform_info) PNGARG((png_structp
 75.2021 +   png_ptr, png_voidp user_transform_ptr, int user_transform_depth,
 75.2022 +   int user_transform_channels));
 75.2023 +/* Return the user pointer associated with the user transform functions */
 75.2024 +extern PNG_EXPORT(png_voidp,png_get_user_transform_ptr)
 75.2025 +   PNGARG((png_structp png_ptr));
 75.2026 +#endif
 75.2027 +
 75.2028 +#ifdef PNG_USER_CHUNKS_SUPPORTED
 75.2029 +extern PNG_EXPORT(void,png_set_read_user_chunk_fn) PNGARG((png_structp png_ptr,
 75.2030 +   png_voidp user_chunk_ptr, png_user_chunk_ptr read_user_chunk_fn));
 75.2031 +extern PNG_EXPORT(png_voidp,png_get_user_chunk_ptr) PNGARG((png_structp
 75.2032 +   png_ptr));
 75.2033 +#endif
 75.2034 +
 75.2035 +#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
 75.2036 +/* Sets the function callbacks for the push reader, and a pointer to a
 75.2037 + * user-defined structure available to the callback functions.
 75.2038 + */
 75.2039 +extern PNG_EXPORT(void,png_set_progressive_read_fn) PNGARG((png_structp png_ptr,
 75.2040 +   png_voidp progressive_ptr,
 75.2041 +   png_progressive_info_ptr info_fn, png_progressive_row_ptr row_fn,
 75.2042 +   png_progressive_end_ptr end_fn));
 75.2043 +
 75.2044 +/* returns the user pointer associated with the push read functions */
 75.2045 +extern PNG_EXPORT(png_voidp,png_get_progressive_ptr)
 75.2046 +   PNGARG((png_structp png_ptr));
 75.2047 +
 75.2048 +/* function to be called when data becomes available */
 75.2049 +extern PNG_EXPORT(void,png_process_data) PNGARG((png_structp png_ptr,
 75.2050 +   png_infop info_ptr, png_bytep buffer, png_size_t buffer_size));
 75.2051 +
 75.2052 +/* function that combines rows.  Not very much different than the
 75.2053 + * png_combine_row() call.  Is this even used?????
 75.2054 + */
 75.2055 +extern PNG_EXPORT(void,png_progressive_combine_row) PNGARG((png_structp png_ptr,
 75.2056 +   png_bytep old_row, png_bytep new_row));
 75.2057 +#endif /* PNG_PROGRESSIVE_READ_SUPPORTED */
 75.2058 +
 75.2059 +extern PNG_EXPORT(png_voidp,png_malloc) PNGARG((png_structp png_ptr,
 75.2060 +   png_uint_32 size));
 75.2061 +
 75.2062 +#if defined(PNG_1_0_X)
 75.2063 +#  define png_malloc_warn png_malloc
 75.2064 +#else
 75.2065 +/* Added at libpng version 1.2.4 */
 75.2066 +extern PNG_EXPORT(png_voidp,png_malloc_warn) PNGARG((png_structp png_ptr,
 75.2067 +   png_uint_32 size));
 75.2068 +#endif
 75.2069 +
 75.2070 +/* frees a pointer allocated by png_malloc() */
 75.2071 +extern PNG_EXPORT(void,png_free) PNGARG((png_structp png_ptr, png_voidp ptr));
 75.2072 +
 75.2073 +#if defined(PNG_1_0_X)
 75.2074 +/* Function to allocate memory for zlib. */
 75.2075 +extern PNG_EXPORT(voidpf,png_zalloc) PNGARG((voidpf png_ptr, uInt items,
 75.2076 +   uInt size));
 75.2077 +
 75.2078 +/* Function to free memory for zlib */
 75.2079 +extern PNG_EXPORT(void,png_zfree) PNGARG((voidpf png_ptr, voidpf ptr));
 75.2080 +#endif
 75.2081 +
 75.2082 +/* Free data that was allocated internally */
 75.2083 +extern PNG_EXPORT(void,png_free_data) PNGARG((png_structp png_ptr,
 75.2084 +   png_infop info_ptr, png_uint_32 free_me, int num));
 75.2085 +#ifdef PNG_FREE_ME_SUPPORTED
 75.2086 +/* Reassign responsibility for freeing existing data, whether allocated
 75.2087 + * by libpng or by the application */
 75.2088 +extern PNG_EXPORT(void,png_data_freer) PNGARG((png_structp png_ptr,
 75.2089 +   png_infop info_ptr, int freer, png_uint_32 mask));
 75.2090 +#endif
 75.2091 +/* assignments for png_data_freer */
 75.2092 +#define PNG_DESTROY_WILL_FREE_DATA 1
 75.2093 +#define PNG_SET_WILL_FREE_DATA 1
 75.2094 +#define PNG_USER_WILL_FREE_DATA 2
 75.2095 +/* Flags for png_ptr->free_me and info_ptr->free_me */
 75.2096 +#define PNG_FREE_HIST 0x0008
 75.2097 +#define PNG_FREE_ICCP 0x0010
 75.2098 +#define PNG_FREE_SPLT 0x0020
 75.2099 +#define PNG_FREE_ROWS 0x0040
 75.2100 +#define PNG_FREE_PCAL 0x0080
 75.2101 +#define PNG_FREE_SCAL 0x0100
 75.2102 +#define PNG_FREE_UNKN 0x0200
 75.2103 +#define PNG_FREE_LIST 0x0400
 75.2104 +#define PNG_FREE_PLTE 0x1000
 75.2105 +#define PNG_FREE_TRNS 0x2000
 75.2106 +#define PNG_FREE_TEXT 0x4000
 75.2107 +#define PNG_FREE_ALL  0x7fff
 75.2108 +#define PNG_FREE_MUL  0x4220 /* PNG_FREE_SPLT|PNG_FREE_TEXT|PNG_FREE_UNKN */
 75.2109 +
 75.2110 +#ifdef PNG_USER_MEM_SUPPORTED
 75.2111 +extern PNG_EXPORT(png_voidp,png_malloc_default) PNGARG((png_structp png_ptr,
 75.2112 +   png_uint_32 size));
 75.2113 +extern PNG_EXPORT(void,png_free_default) PNGARG((png_structp png_ptr,
 75.2114 +   png_voidp ptr));
 75.2115 +#endif
 75.2116 +
 75.2117 +extern PNG_EXPORT(png_voidp,png_memcpy_check) PNGARG((png_structp png_ptr,
 75.2118 +   png_voidp s1, png_voidp s2, png_uint_32 size));
 75.2119 +
 75.2120 +extern PNG_EXPORT(png_voidp,png_memset_check) PNGARG((png_structp png_ptr,
 75.2121 +   png_voidp s1, int value, png_uint_32 size));
 75.2122 +
 75.2123 +#if defined(USE_FAR_KEYWORD)  /* memory model conversion function */
 75.2124 +extern void *png_far_to_near PNGARG((png_structp png_ptr,png_voidp ptr,
 75.2125 +   int check));
 75.2126 +#endif /* USE_FAR_KEYWORD */
 75.2127 +
 75.2128 +#ifndef PNG_NO_ERROR_TEXT
 75.2129 +/* Fatal error in PNG image of libpng - can't continue */
 75.2130 +extern PNG_EXPORT(void,png_error) PNGARG((png_structp png_ptr,
 75.2131 +   png_const_charp error_message));
 75.2132 +
 75.2133 +/* The same, but the chunk name is prepended to the error string. */
 75.2134 +extern PNG_EXPORT(void,png_chunk_error) PNGARG((png_structp png_ptr,
 75.2135 +   png_const_charp error_message));
 75.2136 +#else
 75.2137 +/* Fatal error in PNG image of libpng - can't continue */
 75.2138 +extern PNG_EXPORT(void,png_err) PNGARG((png_structp png_ptr));
 75.2139 +#endif
 75.2140 +
 75.2141 +#ifndef PNG_NO_WARNINGS
 75.2142 +/* Non-fatal error in libpng.  Can continue, but may have a problem. */
 75.2143 +extern PNG_EXPORT(void,png_warning) PNGARG((png_structp png_ptr,
 75.2144 +   png_const_charp warning_message));
 75.2145 +
 75.2146 +#ifdef PNG_READ_SUPPORTED
 75.2147 +/* Non-fatal error in libpng, chunk name is prepended to message. */
 75.2148 +extern PNG_EXPORT(void,png_chunk_warning) PNGARG((png_structp png_ptr,
 75.2149 +   png_const_charp warning_message));
 75.2150 +#endif /* PNG_READ_SUPPORTED */
 75.2151 +#endif /* PNG_NO_WARNINGS */
 75.2152 +
 75.2153 +/* The png_set_<chunk> functions are for storing values in the png_info_struct.
 75.2154 + * Similarly, the png_get_<chunk> calls are used to read values from the
 75.2155 + * png_info_struct, either storing the parameters in the passed variables, or
 75.2156 + * setting pointers into the png_info_struct where the data is stored.  The
 75.2157 + * png_get_<chunk> functions return a non-zero value if the data was available
 75.2158 + * in info_ptr, or return zero and do not change any of the parameters if the
 75.2159 + * data was not available.
 75.2160 + *
 75.2161 + * These functions should be used instead of directly accessing png_info
 75.2162 + * to avoid problems with future changes in the size and internal layout of
 75.2163 + * png_info_struct.
 75.2164 + */
 75.2165 +/* Returns "flag" if chunk data is valid in info_ptr. */
 75.2166 +extern PNG_EXPORT(png_uint_32,png_get_valid) PNGARG((png_structp png_ptr,
 75.2167 +png_infop info_ptr, png_uint_32 flag));
 75.2168 +
 75.2169 +/* Returns number of bytes needed to hold a transformed row. */
 75.2170 +extern PNG_EXPORT(png_uint_32,png_get_rowbytes) PNGARG((png_structp png_ptr,
 75.2171 +png_infop info_ptr));
 75.2172 +
 75.2173 +#if defined(PNG_INFO_IMAGE_SUPPORTED)
 75.2174 +/* Returns row_pointers, which is an array of pointers to scanlines that was
 75.2175 +returned from png_read_png(). */
 75.2176 +extern PNG_EXPORT(png_bytepp,png_get_rows) PNGARG((png_structp png_ptr,
 75.2177 +png_infop info_ptr));
 75.2178 +/* Set row_pointers, which is an array of pointers to scanlines for use
 75.2179 +by png_write_png(). */
 75.2180 +extern PNG_EXPORT(void,png_set_rows) PNGARG((png_structp png_ptr,
 75.2181 +   png_infop info_ptr, png_bytepp row_pointers));
 75.2182 +#endif
 75.2183 +
 75.2184 +/* Returns number of color channels in image. */
 75.2185 +extern PNG_EXPORT(png_byte,png_get_channels) PNGARG((png_structp png_ptr,
 75.2186 +png_infop info_ptr));
 75.2187 +
 75.2188 +#ifdef PNG_EASY_ACCESS_SUPPORTED
 75.2189 +/* Returns image width in pixels. */
 75.2190 +extern PNG_EXPORT(png_uint_32, png_get_image_width) PNGARG((png_structp
 75.2191 +png_ptr, png_infop info_ptr));
 75.2192 +
 75.2193 +/* Returns image height in pixels. */
 75.2194 +extern PNG_EXPORT(png_uint_32, png_get_image_height) PNGARG((png_structp
 75.2195 +png_ptr, png_infop info_ptr));
 75.2196 +
 75.2197 +/* Returns image bit_depth. */
 75.2198 +extern PNG_EXPORT(png_byte, png_get_bit_depth) PNGARG((png_structp
 75.2199 +png_ptr, png_infop info_ptr));
 75.2200 +
 75.2201 +/* Returns image color_type. */
 75.2202 +extern PNG_EXPORT(png_byte, png_get_color_type) PNGARG((png_structp
 75.2203 +png_ptr, png_infop info_ptr));
 75.2204 +
 75.2205 +/* Returns image filter_type. */
 75.2206 +extern PNG_EXPORT(png_byte, png_get_filter_type) PNGARG((png_structp
 75.2207 +png_ptr, png_infop info_ptr));
 75.2208 +
 75.2209 +/* Returns image interlace_type. */
 75.2210 +extern PNG_EXPORT(png_byte, png_get_interlace_type) PNGARG((png_structp
 75.2211 +png_ptr, png_infop info_ptr));
 75.2212 +
 75.2213 +/* Returns image compression_type. */
 75.2214 +extern PNG_EXPORT(png_byte, png_get_compression_type) PNGARG((png_structp
 75.2215 +png_ptr, png_infop info_ptr));
 75.2216 +
 75.2217 +/* Returns image resolution in pixels per meter, from pHYs chunk data. */
 75.2218 +extern PNG_EXPORT(png_uint_32, png_get_pixels_per_meter) PNGARG((png_structp
 75.2219 +png_ptr, png_infop info_ptr));
 75.2220 +extern PNG_EXPORT(png_uint_32, png_get_x_pixels_per_meter) PNGARG((png_structp
 75.2221 +png_ptr, png_infop info_ptr));
 75.2222 +extern PNG_EXPORT(png_uint_32, png_get_y_pixels_per_meter) PNGARG((png_structp
 75.2223 +png_ptr, png_infop info_ptr));
 75.2224 +
 75.2225 +/* Returns pixel aspect ratio, computed from pHYs chunk data.  */
 75.2226 +#ifdef PNG_FLOATING_POINT_SUPPORTED
 75.2227 +extern PNG_EXPORT(float, png_get_pixel_aspect_ratio) PNGARG((png_structp
 75.2228 +png_ptr, png_infop info_ptr));
 75.2229 +#endif
 75.2230 +
 75.2231 +/* Returns image x, y offset in pixels or microns, from oFFs chunk data. */
 75.2232 +extern PNG_EXPORT(png_int_32, png_get_x_offset_pixels) PNGARG((png_structp
 75.2233 +png_ptr, png_infop info_ptr));
 75.2234 +extern PNG_EXPORT(png_int_32, png_get_y_offset_pixels) PNGARG((png_structp
 75.2235 +png_ptr, png_infop info_ptr));
 75.2236 +extern PNG_EXPORT(png_int_32, png_get_x_offset_microns) PNGARG((png_structp
 75.2237 +png_ptr, png_infop info_ptr));
 75.2238 +extern PNG_EXPORT(png_int_32, png_get_y_offset_microns) PNGARG((png_structp
 75.2239 +png_ptr, png_infop info_ptr));
 75.2240 +
 75.2241 +#endif /* PNG_EASY_ACCESS_SUPPORTED */
 75.2242 +
 75.2243 +/* Returns pointer to signature string read from PNG header */
 75.2244 +extern PNG_EXPORT(png_bytep,png_get_signature) PNGARG((png_structp png_ptr,
 75.2245 +png_infop info_ptr));
 75.2246 +
 75.2247 +#if defined(PNG_bKGD_SUPPORTED)
 75.2248 +extern PNG_EXPORT(png_uint_32,png_get_bKGD) PNGARG((png_structp png_ptr,
 75.2249 +   png_infop info_ptr, png_color_16p *background));
 75.2250 +#endif
 75.2251 +
 75.2252 +#if defined(PNG_bKGD_SUPPORTED)
 75.2253 +extern PNG_EXPORT(void,png_set_bKGD) PNGARG((png_structp png_ptr,
 75.2254 +   png_infop info_ptr, png_color_16p background));
 75.2255 +#endif
 75.2256 +
 75.2257 +#if defined(PNG_cHRM_SUPPORTED)
 75.2258 +#ifdef PNG_FLOATING_POINT_SUPPORTED
 75.2259 +extern PNG_EXPORT(png_uint_32,png_get_cHRM) PNGARG((png_structp png_ptr,
 75.2260 +   png_infop info_ptr, double *white_x, double *white_y, double *red_x,
 75.2261 +   double *red_y, double *green_x, double *green_y, double *blue_x,
 75.2262 +   double *blue_y));
 75.2263 +#endif
 75.2264 +#ifdef PNG_FIXED_POINT_SUPPORTED
 75.2265 +extern PNG_EXPORT(png_uint_32,png_get_cHRM_fixed) PNGARG((png_structp png_ptr,
 75.2266 +   png_infop info_ptr, png_fixed_point *int_white_x, png_fixed_point
 75.2267 +   *int_white_y, png_fixed_point *int_red_x, png_fixed_point *int_red_y,
 75.2268 +   png_fixed_point *int_green_x, png_fixed_point *int_green_y, png_fixed_point
 75.2269 +   *int_blue_x, png_fixed_point *int_blue_y));
 75.2270 +#endif
 75.2271 +#endif
 75.2272 +
 75.2273 +#if defined(PNG_cHRM_SUPPORTED)
 75.2274 +#ifdef PNG_FLOATING_POINT_SUPPORTED
 75.2275 +extern PNG_EXPORT(void,png_set_cHRM) PNGARG((png_structp png_ptr,
 75.2276 +   png_infop info_ptr, double white_x, double white_y, double red_x,
 75.2277 +   double red_y, double green_x, double green_y, double blue_x, double blue_y));
 75.2278 +#endif
 75.2279 +#ifdef PNG_FIXED_POINT_SUPPORTED
 75.2280 +extern PNG_EXPORT(void,png_set_cHRM_fixed) PNGARG((png_structp png_ptr,
 75.2281 +   png_infop info_ptr, png_fixed_point int_white_x, png_fixed_point int_white_y,
 75.2282 +   png_fixed_point int_red_x, png_fixed_point int_red_y, png_fixed_point
 75.2283 +   int_green_x, png_fixed_point int_green_y, png_fixed_point int_blue_x,
 75.2284 +   png_fixed_point int_blue_y));
 75.2285 +#endif
 75.2286 +#endif
 75.2287 +
 75.2288 +#if defined(PNG_gAMA_SUPPORTED)
 75.2289 +#ifdef PNG_FLOATING_POINT_SUPPORTED
 75.2290 +extern PNG_EXPORT(png_uint_32,png_get_gAMA) PNGARG((png_structp png_ptr,
 75.2291 +   png_infop info_ptr, double *file_gamma));
 75.2292 +#endif
 75.2293 +extern PNG_EXPORT(png_uint_32,png_get_gAMA_fixed) PNGARG((png_structp png_ptr,
 75.2294 +   png_infop info_ptr, png_fixed_point *int_file_gamma));
 75.2295 +#endif
 75.2296 +
 75.2297 +#if defined(PNG_gAMA_SUPPORTED)
 75.2298 +#ifdef PNG_FLOATING_POINT_SUPPORTED
 75.2299 +extern PNG_EXPORT(void,png_set_gAMA) PNGARG((png_structp png_ptr,
 75.2300 +   png_infop info_ptr, double file_gamma));
 75.2301 +#endif
 75.2302 +extern PNG_EXPORT(void,png_set_gAMA_fixed) PNGARG((png_structp png_ptr,
 75.2303 +   png_infop info_ptr, png_fixed_point int_file_gamma));
 75.2304 +#endif
 75.2305 +
 75.2306 +#if defined(PNG_hIST_SUPPORTED)
 75.2307 +extern PNG_EXPORT(png_uint_32,png_get_hIST) PNGARG((png_structp png_ptr,
 75.2308 +   png_infop info_ptr, png_uint_16p *hist));
 75.2309 +#endif
 75.2310 +
 75.2311 +#if defined(PNG_hIST_SUPPORTED)
 75.2312 +extern PNG_EXPORT(void,png_set_hIST) PNGARG((png_structp png_ptr,
 75.2313 +   png_infop info_ptr, png_uint_16p hist));
 75.2314 +#endif
 75.2315 +
 75.2316 +extern PNG_EXPORT(png_uint_32,png_get_IHDR) PNGARG((png_structp png_ptr,
 75.2317 +   png_infop info_ptr, png_uint_32 *width, png_uint_32 *height,
 75.2318 +   int *bit_depth, int *color_type, int *interlace_method,
 75.2319 +   int *compression_method, int *filter_method));
 75.2320 +
 75.2321 +extern PNG_EXPORT(void,png_set_IHDR) PNGARG((png_structp png_ptr,
 75.2322 +   png_infop info_ptr, png_uint_32 width, png_uint_32 height, int bit_depth,
 75.2323 +   int color_type, int interlace_method, int compression_method,
 75.2324 +   int filter_method));
 75.2325 +
 75.2326 +#if defined(PNG_oFFs_SUPPORTED)
 75.2327 +extern PNG_EXPORT(png_uint_32,png_get_oFFs) PNGARG((png_structp png_ptr,
 75.2328 +   png_infop info_ptr, png_int_32 *offset_x, png_int_32 *offset_y,
 75.2329 +   int *unit_type));
 75.2330 +#endif
 75.2331 +
 75.2332 +#if defined(PNG_oFFs_SUPPORTED)
 75.2333 +extern PNG_EXPORT(void,png_set_oFFs) PNGARG((png_structp png_ptr,
 75.2334 +   png_infop info_ptr, png_int_32 offset_x, png_int_32 offset_y,
 75.2335 +   int unit_type));
 75.2336 +#endif
 75.2337 +
 75.2338 +#if defined(PNG_pCAL_SUPPORTED)
 75.2339 +extern PNG_EXPORT(png_uint_32,png_get_pCAL) PNGARG((png_structp png_ptr,
 75.2340 +   png_infop info_ptr, png_charp *purpose, png_int_32 *X0, png_int_32 *X1,
 75.2341 +   int *type, int *nparams, png_charp *units, png_charpp *params));
 75.2342 +#endif
 75.2343 +
 75.2344 +#if defined(PNG_pCAL_SUPPORTED)
 75.2345 +extern PNG_EXPORT(void,png_set_pCAL) PNGARG((png_structp png_ptr,
 75.2346 +   png_infop info_ptr, png_charp purpose, png_int_32 X0, png_int_32 X1,
 75.2347 +   int type, int nparams, png_charp units, png_charpp params));
 75.2348 +#endif
 75.2349 +
 75.2350 +#if defined(PNG_pHYs_SUPPORTED)
 75.2351 +extern PNG_EXPORT(png_uint_32,png_get_pHYs) PNGARG((png_structp png_ptr,
 75.2352 +   png_infop info_ptr, png_uint_32 *res_x, png_uint_32 *res_y, int *unit_type));
 75.2353 +#endif
 75.2354 +
 75.2355 +#if defined(PNG_pHYs_SUPPORTED)
 75.2356 +extern PNG_EXPORT(void,png_set_pHYs) PNGARG((png_structp png_ptr,
 75.2357 +   png_infop info_ptr, png_uint_32 res_x, png_uint_32 res_y, int unit_type));
 75.2358 +#endif
 75.2359 +
 75.2360 +extern PNG_EXPORT(png_uint_32,png_get_PLTE) PNGARG((png_structp png_ptr,
 75.2361 +   png_infop info_ptr, png_colorp *palette, int *num_palette));
 75.2362 +
 75.2363 +extern PNG_EXPORT(void,png_set_PLTE) PNGARG((png_structp png_ptr,
 75.2364 +   png_infop info_ptr, png_colorp palette, int num_palette));
 75.2365 +
 75.2366 +#if defined(PNG_sBIT_SUPPORTED)
 75.2367 +extern PNG_EXPORT(png_uint_32,png_get_sBIT) PNGARG((png_structp png_ptr,
 75.2368 +   png_infop info_ptr, png_color_8p *sig_bit));
 75.2369 +#endif
 75.2370 +
 75.2371 +#if defined(PNG_sBIT_SUPPORTED)
 75.2372 +extern PNG_EXPORT(void,png_set_sBIT) PNGARG((png_structp png_ptr,
 75.2373 +   png_infop info_ptr, png_color_8p sig_bit));
 75.2374 +#endif
 75.2375 +
 75.2376 +#if defined(PNG_sRGB_SUPPORTED)
 75.2377 +extern PNG_EXPORT(png_uint_32,png_get_sRGB) PNGARG((png_structp png_ptr,
 75.2378 +   png_infop info_ptr, int *intent));
 75.2379 +#endif
 75.2380 +
 75.2381 +#if defined(PNG_sRGB_SUPPORTED)
 75.2382 +extern PNG_EXPORT(void,png_set_sRGB) PNGARG((png_structp png_ptr,
 75.2383 +   png_infop info_ptr, int intent));
 75.2384 +extern PNG_EXPORT(void,png_set_sRGB_gAMA_and_cHRM) PNGARG((png_structp png_ptr,
 75.2385 +   png_infop info_ptr, int intent));
 75.2386 +#endif
 75.2387 +
 75.2388 +#if defined(PNG_iCCP_SUPPORTED)
 75.2389 +extern PNG_EXPORT(png_uint_32,png_get_iCCP) PNGARG((png_structp png_ptr,
 75.2390 +   png_infop info_ptr, png_charpp name, int *compression_type,
 75.2391 +   png_charpp profile, png_uint_32 *proflen));
 75.2392 +   /* Note to maintainer: profile should be png_bytepp */
 75.2393 +#endif
 75.2394 +
 75.2395 +#if defined(PNG_iCCP_SUPPORTED)
 75.2396 +extern PNG_EXPORT(void,png_set_iCCP) PNGARG((png_structp png_ptr,
 75.2397 +   png_infop info_ptr, png_charp name, int compression_type,
 75.2398 +   png_charp profile, png_uint_32 proflen));
 75.2399 +   /* Note to maintainer: profile should be png_bytep */
 75.2400 +#endif
 75.2401 +
 75.2402 +#if defined(PNG_sPLT_SUPPORTED)
 75.2403 +extern PNG_EXPORT(png_uint_32,png_get_sPLT) PNGARG((png_structp png_ptr,
 75.2404 +   png_infop info_ptr, png_sPLT_tpp entries));
 75.2405 +#endif
 75.2406 +
 75.2407 +#if defined(PNG_sPLT_SUPPORTED)
 75.2408 +extern PNG_EXPORT(void,png_set_sPLT) PNGARG((png_structp png_ptr,
 75.2409 +   png_infop info_ptr, png_sPLT_tp entries, int nentries));
 75.2410 +#endif
 75.2411 +
 75.2412 +#if defined(PNG_TEXT_SUPPORTED)
 75.2413 +/* png_get_text also returns the number of text chunks in *num_text */
 75.2414 +extern PNG_EXPORT(png_uint_32,png_get_text) PNGARG((png_structp png_ptr,
 75.2415 +   png_infop info_ptr, png_textp *text_ptr, int *num_text));
 75.2416 +#endif
 75.2417 +
 75.2418 +/*
 75.2419 + *  Note while png_set_text() will accept a structure whose text,
 75.2420 + *  language, and  translated keywords are NULL pointers, the structure
 75.2421 + *  returned by png_get_text will always contain regular
 75.2422 + *  zero-terminated C strings.  They might be empty strings but
 75.2423 + *  they will never be NULL pointers.
 75.2424 + */
 75.2425 +
 75.2426 +#if defined(PNG_TEXT_SUPPORTED)
 75.2427 +extern PNG_EXPORT(void,png_set_text) PNGARG((png_structp png_ptr,
 75.2428 +   png_infop info_ptr, png_textp text_ptr, int num_text));
 75.2429 +#endif
 75.2430 +
 75.2431 +#if defined(PNG_tIME_SUPPORTED)
 75.2432 +extern PNG_EXPORT(png_uint_32,png_get_tIME) PNGARG((png_structp png_ptr,
 75.2433 +   png_infop info_ptr, png_timep *mod_time));
 75.2434 +#endif
 75.2435 +
 75.2436 +#if defined(PNG_tIME_SUPPORTED)
 75.2437 +extern PNG_EXPORT(void,png_set_tIME) PNGARG((png_structp png_ptr,
 75.2438 +   png_infop info_ptr, png_timep mod_time));
 75.2439 +#endif
 75.2440 +
 75.2441 +#if defined(PNG_tRNS_SUPPORTED)
 75.2442 +extern PNG_EXPORT(png_uint_32,png_get_tRNS) PNGARG((png_structp png_ptr,
 75.2443 +   png_infop info_ptr, png_bytep *trans, int *num_trans,
 75.2444 +   png_color_16p *trans_values));
 75.2445 +#endif
 75.2446 +
 75.2447 +#if defined(PNG_tRNS_SUPPORTED)
 75.2448 +extern PNG_EXPORT(void,png_set_tRNS) PNGARG((png_structp png_ptr,
 75.2449 +   png_infop info_ptr, png_bytep trans, int num_trans,
 75.2450 +   png_color_16p trans_values));
 75.2451 +#endif
 75.2452 +
 75.2453 +#if defined(PNG_tRNS_SUPPORTED)
 75.2454 +#endif
 75.2455 +
 75.2456 +#if defined(PNG_sCAL_SUPPORTED)
 75.2457 +#ifdef PNG_FLOATING_POINT_SUPPORTED
 75.2458 +extern PNG_EXPORT(png_uint_32,png_get_sCAL) PNGARG((png_structp png_ptr,
 75.2459 +   png_infop info_ptr, int *unit, double *width, double *height));
 75.2460 +#else
 75.2461 +#ifdef PNG_FIXED_POINT_SUPPORTED
 75.2462 +extern PNG_EXPORT(png_uint_32,png_get_sCAL_s) PNGARG((png_structp png_ptr,
 75.2463 +   png_infop info_ptr, int *unit, png_charpp swidth, png_charpp sheight));
 75.2464 +#endif
 75.2465 +#endif
 75.2466 +#endif /* PNG_sCAL_SUPPORTED */
 75.2467 +
 75.2468 +#if defined(PNG_sCAL_SUPPORTED)
 75.2469 +#ifdef PNG_FLOATING_POINT_SUPPORTED
 75.2470 +extern PNG_EXPORT(void,png_set_sCAL) PNGARG((png_structp png_ptr,
 75.2471 +   png_infop info_ptr, int unit, double width, double height));
 75.2472 +#else
 75.2473 +#ifdef PNG_FIXED_POINT_SUPPORTED
 75.2474 +extern PNG_EXPORT(void,png_set_sCAL_s) PNGARG((png_structp png_ptr,
 75.2475 +   png_infop info_ptr, int unit, png_charp swidth, png_charp sheight));
 75.2476 +#endif
 75.2477 +#endif
 75.2478 +#endif /* PNG_sCAL_SUPPORTED || PNG_WRITE_sCAL_SUPPORTED */
 75.2479 +
 75.2480 +#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
 75.2481 +/* provide a list of chunks and how they are to be handled, if the built-in
 75.2482 +   handling or default unknown chunk handling is not desired.  Any chunks not
 75.2483 +   listed will be handled in the default manner.  The IHDR and IEND chunks
 75.2484 +   must not be listed.
 75.2485 +      keep = 0: follow default behaviour
 75.2486 +           = 1: do not keep
 75.2487 +           = 2: keep only if safe-to-copy
 75.2488 +           = 3: keep even if unsafe-to-copy
 75.2489 +*/
 75.2490 +extern PNG_EXPORT(void, png_set_keep_unknown_chunks) PNGARG((png_structp
 75.2491 +   png_ptr, int keep, png_bytep chunk_list, int num_chunks));
 75.2492 +extern PNG_EXPORT(void, png_set_unknown_chunks) PNGARG((png_structp png_ptr,
 75.2493 +   png_infop info_ptr, png_unknown_chunkp unknowns, int num_unknowns));
 75.2494 +extern PNG_EXPORT(void, png_set_unknown_chunk_location)
 75.2495 +   PNGARG((png_structp png_ptr, png_infop info_ptr, int chunk, int location));
 75.2496 +extern PNG_EXPORT(png_uint_32,png_get_unknown_chunks) PNGARG((png_structp
 75.2497 +   png_ptr, png_infop info_ptr, png_unknown_chunkpp entries));
 75.2498 +#endif
 75.2499 +#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
 75.2500 +PNG_EXPORT(int,png_handle_as_unknown) PNGARG((png_structp png_ptr, png_bytep
 75.2501 +   chunk_name));
 75.2502 +#endif
 75.2503 +
 75.2504 +/* Png_free_data() will turn off the "valid" flag for anything it frees.
 75.2505 +   If you need to turn it off for a chunk that your application has freed,
 75.2506 +   you can use png_set_invalid(png_ptr, info_ptr, PNG_INFO_CHNK); */
 75.2507 +extern PNG_EXPORT(void, png_set_invalid) PNGARG((png_structp png_ptr,
 75.2508 +   png_infop info_ptr, int mask));
 75.2509 +
 75.2510 +#if defined(PNG_INFO_IMAGE_SUPPORTED)
 75.2511 +/* The "params" pointer is currently not used and is for future expansion. */
 75.2512 +extern PNG_EXPORT(void, png_read_png) PNGARG((png_structp png_ptr,
 75.2513 +                        png_infop info_ptr,
 75.2514 +                        int transforms,
 75.2515 +                        png_voidp params));
 75.2516 +extern PNG_EXPORT(void, png_write_png) PNGARG((png_structp png_ptr,
 75.2517 +                        png_infop info_ptr,
 75.2518 +                        int transforms,
 75.2519 +                        png_voidp params));
 75.2520 +#endif
 75.2521 +
 75.2522 +/* Define PNG_DEBUG at compile time for debugging information.  Higher
 75.2523 + * numbers for PNG_DEBUG mean more debugging information.  This has
 75.2524 + * only been added since version 0.95 so it is not implemented throughout
 75.2525 + * libpng yet, but more support will be added as needed.
 75.2526 + */
 75.2527 +#ifdef PNG_DEBUG
 75.2528 +#if (PNG_DEBUG > 0)
 75.2529 +#if !defined(PNG_DEBUG_FILE) && defined(_MSC_VER)
 75.2530 +#include <crtdbg.h>
 75.2531 +#if (PNG_DEBUG > 1)
 75.2532 +#define png_debug(l,m)  _RPT0(_CRT_WARN,m)
 75.2533 +#define png_debug1(l,m,p1)  _RPT1(_CRT_WARN,m,p1)
 75.2534 +#define png_debug2(l,m,p1,p2) _RPT2(_CRT_WARN,m,p1,p2)
 75.2535 +#endif
 75.2536 +#else /* PNG_DEBUG_FILE || !_MSC_VER */
 75.2537 +#ifndef PNG_DEBUG_FILE
 75.2538 +#define PNG_DEBUG_FILE stderr
 75.2539 +#endif /* PNG_DEBUG_FILE */
 75.2540 +#if (PNG_DEBUG > 1)
 75.2541 +#define png_debug(l,m) \
 75.2542 +{ \
 75.2543 +     int num_tabs=l; \
 75.2544 +     fprintf(PNG_DEBUG_FILE,"%s"m,(num_tabs==1 ? "\t" : \
 75.2545 +       (num_tabs==2 ? "\t\t":(num_tabs>2 ? "\t\t\t":"")))); \
 75.2546 +}
 75.2547 +#define png_debug1(l,m,p1) \
 75.2548 +{ \
 75.2549 +     int num_tabs=l; \
 75.2550 +     fprintf(PNG_DEBUG_FILE,"%s"m,(num_tabs==1 ? "\t" : \
 75.2551 +       (num_tabs==2 ? "\t\t":(num_tabs>2 ? "\t\t\t":""))),p1); \
 75.2552 +}
 75.2553 +#define png_debug2(l,m,p1,p2) \
 75.2554 +{ \
 75.2555 +     int num_tabs=l; \
 75.2556 +     fprintf(PNG_DEBUG_FILE,"%s"m,(num_tabs==1 ? "\t" : \
 75.2557 +       (num_tabs==2 ? "\t\t":(num_tabs>2 ? "\t\t\t":""))),p1,p2); \
 75.2558 +}
 75.2559 +#endif /* (PNG_DEBUG > 1) */
 75.2560 +#endif /* _MSC_VER */
 75.2561 +#endif /* (PNG_DEBUG > 0) */
 75.2562 +#endif /* PNG_DEBUG */
 75.2563 +#ifndef png_debug
 75.2564 +#define png_debug(l, m)
 75.2565 +#endif
 75.2566 +#ifndef png_debug1
 75.2567 +#define png_debug1(l, m, p1)
 75.2568 +#endif
 75.2569 +#ifndef png_debug2
 75.2570 +#define png_debug2(l, m, p1, p2)
 75.2571 +#endif
 75.2572 +
 75.2573 +extern PNG_EXPORT(png_charp,png_get_copyright) PNGARG((png_structp png_ptr));
 75.2574 +extern PNG_EXPORT(png_charp,png_get_header_ver) PNGARG((png_structp png_ptr));
 75.2575 +extern PNG_EXPORT(png_charp,png_get_header_version) PNGARG((png_structp png_ptr));
 75.2576 +extern PNG_EXPORT(png_charp,png_get_libpng_ver) PNGARG((png_structp png_ptr));
 75.2577 +
 75.2578 +#ifdef PNG_MNG_FEATURES_SUPPORTED
 75.2579 +extern PNG_EXPORT(png_uint_32,png_permit_mng_features) PNGARG((png_structp
 75.2580 +   png_ptr, png_uint_32 mng_features_permitted));
 75.2581 +#endif
 75.2582 +
 75.2583 +/* For use in png_set_keep_unknown, added to version 1.2.6 */
 75.2584 +#define PNG_HANDLE_CHUNK_AS_DEFAULT   0
 75.2585 +#define PNG_HANDLE_CHUNK_NEVER        1
 75.2586 +#define PNG_HANDLE_CHUNK_IF_SAFE      2
 75.2587 +#define PNG_HANDLE_CHUNK_ALWAYS       3
 75.2588 +
 75.2589 +/* Added to version 1.2.0 */
 75.2590 +#if defined(PNG_ASSEMBLER_CODE_SUPPORTED)
 75.2591 +#if defined(PNG_MMX_CODE_SUPPORTED)
 75.2592 +#define PNG_ASM_FLAG_MMX_SUPPORT_COMPILED  0x01  /* not user-settable */
 75.2593 +#define PNG_ASM_FLAG_MMX_SUPPORT_IN_CPU    0x02  /* not user-settable */
 75.2594 +#define PNG_ASM_FLAG_MMX_READ_COMBINE_ROW  0x04
 75.2595 +#define PNG_ASM_FLAG_MMX_READ_INTERLACE    0x08
 75.2596 +#define PNG_ASM_FLAG_MMX_READ_FILTER_SUB   0x10
 75.2597 +#define PNG_ASM_FLAG_MMX_READ_FILTER_UP    0x20
 75.2598 +#define PNG_ASM_FLAG_MMX_READ_FILTER_AVG   0x40
 75.2599 +#define PNG_ASM_FLAG_MMX_READ_FILTER_PAETH 0x80
 75.2600 +#define PNG_ASM_FLAGS_INITIALIZED          0x80000000  /* not user-settable */
 75.2601 +
 75.2602 +#define PNG_MMX_READ_FLAGS ( PNG_ASM_FLAG_MMX_READ_COMBINE_ROW  \
 75.2603 +                           | PNG_ASM_FLAG_MMX_READ_INTERLACE    \
 75.2604 +                           | PNG_ASM_FLAG_MMX_READ_FILTER_SUB   \
 75.2605 +                           | PNG_ASM_FLAG_MMX_READ_FILTER_UP    \
 75.2606 +                           | PNG_ASM_FLAG_MMX_READ_FILTER_AVG   \
 75.2607 +                           | PNG_ASM_FLAG_MMX_READ_FILTER_PAETH )
 75.2608 +#define PNG_MMX_WRITE_FLAGS ( 0 )
 75.2609 +
 75.2610 +#define PNG_MMX_FLAGS ( PNG_ASM_FLAG_MMX_SUPPORT_COMPILED \
 75.2611 +                      | PNG_ASM_FLAG_MMX_SUPPORT_IN_CPU   \
 75.2612 +                      | PNG_MMX_READ_FLAGS                \
 75.2613 +                      | PNG_MMX_WRITE_FLAGS )
 75.2614 +
 75.2615 +#define PNG_SELECT_READ   1
 75.2616 +#define PNG_SELECT_WRITE  2
 75.2617 +#endif /* PNG_MMX_CODE_SUPPORTED */
 75.2618 +
 75.2619 +#if !defined(PNG_1_0_X)
 75.2620 +/* pngget.c */
 75.2621 +extern PNG_EXPORT(png_uint_32,png_get_mmx_flagmask)
 75.2622 +   PNGARG((int flag_select, int *compilerID));
 75.2623 +
 75.2624 +/* pngget.c */
 75.2625 +extern PNG_EXPORT(png_uint_32,png_get_asm_flagmask)
 75.2626 +   PNGARG((int flag_select));
 75.2627 +
 75.2628 +/* pngget.c */
 75.2629 +extern PNG_EXPORT(png_uint_32,png_get_asm_flags)
 75.2630 +   PNGARG((png_structp png_ptr));
 75.2631 +
 75.2632 +/* pngget.c */
 75.2633 +extern PNG_EXPORT(png_byte,png_get_mmx_bitdepth_threshold)
 75.2634 +   PNGARG((png_structp png_ptr));
 75.2635 +
 75.2636 +/* pngget.c */
 75.2637 +extern PNG_EXPORT(png_uint_32,png_get_mmx_rowbytes_threshold)
 75.2638 +   PNGARG((png_structp png_ptr));
 75.2639 +
 75.2640 +/* pngset.c */
 75.2641 +extern PNG_EXPORT(void,png_set_asm_flags)
 75.2642 +   PNGARG((png_structp png_ptr, png_uint_32 asm_flags));
 75.2643 +
 75.2644 +/* pngset.c */
 75.2645 +extern PNG_EXPORT(void,png_set_mmx_thresholds)
 75.2646 +   PNGARG((png_structp png_ptr, png_byte mmx_bitdepth_threshold,
 75.2647 +   png_uint_32 mmx_rowbytes_threshold));
 75.2648 +
 75.2649 +#endif /* PNG_1_0_X */
 75.2650 +
 75.2651 +#if !defined(PNG_1_0_X)
 75.2652 +/* png.c, pnggccrd.c, or pngvcrd.c */
 75.2653 +extern PNG_EXPORT(int,png_mmx_support) PNGARG((void));
 75.2654 +#endif /* PNG_ASSEMBLER_CODE_SUPPORTED */
 75.2655 +
 75.2656 +/* Strip the prepended error numbers ("#nnn ") from error and warning
 75.2657 + * messages before passing them to the error or warning handler. */
 75.2658 +#ifdef PNG_ERROR_NUMBERS_SUPPORTED
 75.2659 +extern PNG_EXPORT(void,png_set_strip_error_numbers) PNGARG((png_structp
 75.2660 +   png_ptr, png_uint_32 strip_mode));
 75.2661 +#endif
 75.2662 +
 75.2663 +#endif /* PNG_1_0_X */
 75.2664 +
 75.2665 +/* Added at libpng-1.2.6 */
 75.2666 +#ifdef PNG_SET_USER_LIMITS_SUPPORTED
 75.2667 +extern PNG_EXPORT(void,png_set_user_limits) PNGARG((png_structp
 75.2668 +   png_ptr, png_uint_32 user_width_max, png_uint_32 user_height_max));
 75.2669 +extern PNG_EXPORT(png_uint_32,png_get_user_width_max) PNGARG((png_structp
 75.2670 +   png_ptr));
 75.2671 +extern PNG_EXPORT(png_uint_32,png_get_user_height_max) PNGARG((png_structp
 75.2672 +   png_ptr));
 75.2673 +#endif
 75.2674 +
 75.2675 +
 75.2676 +/* Maintainer: Put new public prototypes here ^, in libpng.3, and project defs */
 75.2677 +
 75.2678 +#ifdef PNG_READ_COMPOSITE_NODIV_SUPPORTED
 75.2679 +/* With these routines we avoid an integer divide, which will be slower on
 75.2680 + * most machines.  However, it does take more operations than the corresponding
 75.2681 + * divide method, so it may be slower on a few RISC systems.  There are two
 75.2682 + * shifts (by 8 or 16 bits) and an addition, versus a single integer divide.
 75.2683 + *
 75.2684 + * Note that the rounding factors are NOT supposed to be the same!  128 and
 75.2685 + * 32768 are correct for the NODIV code; 127 and 32767 are correct for the
 75.2686 + * standard method.
 75.2687 + *
 75.2688 + * [Optimized code by Greg Roelofs and Mark Adler...blame us for bugs. :-) ]
 75.2689 + */
 75.2690 +
 75.2691 + /* fg and bg should be in `gamma 1.0' space; alpha is the opacity          */
 75.2692 +
 75.2693 +#  define png_composite(composite, fg, alpha, bg)                            \
 75.2694 +     { png_uint_16 temp = (png_uint_16)((png_uint_16)(fg) * (png_uint_16)(alpha) \
 75.2695 +                        +        (png_uint_16)(bg)*(png_uint_16)(255 -       \
 75.2696 +                        (png_uint_16)(alpha)) + (png_uint_16)128);           \
 75.2697 +       (composite) = (png_byte)((temp + (temp >> 8)) >> 8); }
 75.2698 +
 75.2699 +#  define png_composite_16(composite, fg, alpha, bg)                         \
 75.2700 +     { png_uint_32 temp = (png_uint_32)((png_uint_32)(fg) * (png_uint_32)(alpha) \
 75.2701 +                        + (png_uint_32)(bg)*(png_uint_32)(65535L -           \
 75.2702 +                        (png_uint_32)(alpha)) + (png_uint_32)32768L);        \
 75.2703 +       (composite) = (png_uint_16)((temp + (temp >> 16)) >> 16); }
 75.2704 +
 75.2705 +#else  /* standard method using integer division */
 75.2706 +
 75.2707 +#  define png_composite(composite, fg, alpha, bg)                            \
 75.2708 +     (composite) = (png_byte)(((png_uint_16)(fg) * (png_uint_16)(alpha) +    \
 75.2709 +       (png_uint_16)(bg) * (png_uint_16)(255 - (png_uint_16)(alpha)) +       \
 75.2710 +       (png_uint_16)127) / 255)
 75.2711 +
 75.2712 +#  define png_composite_16(composite, fg, alpha, bg)                         \
 75.2713 +     (composite) = (png_uint_16)(((png_uint_32)(fg) * (png_uint_32)(alpha) + \
 75.2714 +       (png_uint_32)(bg)*(png_uint_32)(65535L - (png_uint_32)(alpha)) +      \
 75.2715 +       (png_uint_32)32767) / (png_uint_32)65535L)
 75.2716 +
 75.2717 +#endif /* PNG_READ_COMPOSITE_NODIV_SUPPORTED */
 75.2718 +
 75.2719 +/* Inline macros to do direct reads of bytes from the input buffer.  These
 75.2720 + * require that you are using an architecture that uses PNG byte ordering
 75.2721 + * (MSB first) and supports unaligned data storage.  I think that PowerPC
 75.2722 + * in big-endian mode and 680x0 are the only ones that will support this.
 75.2723 + * The x86 line of processors definitely do not.  The png_get_int_32()
 75.2724 + * routine also assumes we are using two's complement format for negative
 75.2725 + * values, which is almost certainly true.
 75.2726 + */
 75.2727 +#if defined(PNG_READ_BIG_ENDIAN_SUPPORTED)
 75.2728 +#  define png_get_uint_32(buf) ( *((png_uint_32p) (buf)))
 75.2729 +#  define png_get_uint_16(buf) ( *((png_uint_16p) (buf)))
 75.2730 +#  define png_get_int_32(buf)  ( *((png_int_32p)  (buf)))
 75.2731 +#else
 75.2732 +extern PNG_EXPORT(png_uint_32,png_get_uint_32) PNGARG((png_bytep buf));
 75.2733 +extern PNG_EXPORT(png_uint_16,png_get_uint_16) PNGARG((png_bytep buf));
 75.2734 +extern PNG_EXPORT(png_int_32,png_get_int_32) PNGARG((png_bytep buf));
 75.2735 +#endif /* !PNG_READ_BIG_ENDIAN_SUPPORTED */
 75.2736 +extern PNG_EXPORT(png_uint_32,png_get_uint_31)
 75.2737 +  PNGARG((png_structp png_ptr, png_bytep buf));
 75.2738 +/* No png_get_int_16 -- may be added if there's a real need for it. */
 75.2739 +
 75.2740 +/* Place a 32-bit number into a buffer in PNG byte order (big-endian).
 75.2741 + */
 75.2742 +extern PNG_EXPORT(void,png_save_uint_32)
 75.2743 +   PNGARG((png_bytep buf, png_uint_32 i));
 75.2744 +extern PNG_EXPORT(void,png_save_int_32)
 75.2745 +   PNGARG((png_bytep buf, png_int_32 i));
 75.2746 +
 75.2747 +/* Place a 16-bit number into a buffer in PNG byte order.
 75.2748 + * The parameter is declared unsigned int, not png_uint_16,
 75.2749 + * just to avoid potential problems on pre-ANSI C compilers.
 75.2750 + */
 75.2751 +extern PNG_EXPORT(void,png_save_uint_16)
 75.2752 +   PNGARG((png_bytep buf, unsigned int i));
 75.2753 +/* No png_save_int_16 -- may be added if there's a real need for it. */
 75.2754 +
 75.2755 +/* ************************************************************************* */
 75.2756 +
 75.2757 +/* These next functions are used internally in the code.  They generally
 75.2758 + * shouldn't be used unless you are writing code to add or replace some
 75.2759 + * functionality in libpng.  More information about most functions can
 75.2760 + * be found in the files where the functions are located.
 75.2761 + */
 75.2762 +
 75.2763 +
 75.2764 +/* Various modes of operation, that are visible to applications because
 75.2765 + * they are used for unknown chunk location.
 75.2766 + */
 75.2767 +#define PNG_HAVE_IHDR               0x01
 75.2768 +#define PNG_HAVE_PLTE               0x02
 75.2769 +#define PNG_HAVE_IDAT               0x04
 75.2770 +#define PNG_AFTER_IDAT              0x08 /* Have complete zlib datastream */
 75.2771 +#define PNG_HAVE_IEND               0x10
 75.2772 +
 75.2773 +#if defined(PNG_INTERNAL)
 75.2774 +
 75.2775 +/* More modes of operation.  Note that after an init, mode is set to
 75.2776 + * zero automatically when the structure is created.
 75.2777 + */
 75.2778 +#define PNG_HAVE_gAMA               0x20
 75.2779 +#define PNG_HAVE_cHRM               0x40
 75.2780 +#define PNG_HAVE_sRGB               0x80
 75.2781 +#define PNG_HAVE_CHUNK_HEADER      0x100
 75.2782 +#define PNG_WROTE_tIME             0x200
 75.2783 +#define PNG_WROTE_INFO_BEFORE_PLTE 0x400
 75.2784 +#define PNG_BACKGROUND_IS_GRAY     0x800
 75.2785 +#define PNG_HAVE_PNG_SIGNATURE    0x1000
 75.2786 +#define PNG_HAVE_CHUNK_AFTER_IDAT 0x2000 /* Have another chunk after IDAT */
 75.2787 +
 75.2788 +/* flags for the transformations the PNG library does on the image data */
 75.2789 +#define PNG_BGR                0x0001
 75.2790 +#define PNG_INTERLACE          0x0002
 75.2791 +#define PNG_PACK               0x0004
 75.2792 +#define PNG_SHIFT              0x0008
 75.2793 +#define PNG_SWAP_BYTES         0x0010
 75.2794 +#define PNG_INVERT_MONO        0x0020
 75.2795 +#define PNG_DITHER             0x0040
 75.2796 +#define PNG_BACKGROUND         0x0080
 75.2797 +#define PNG_BACKGROUND_EXPAND  0x0100
 75.2798 +                          /*   0x0200 unused */
 75.2799 +#define PNG_16_TO_8            0x0400
 75.2800 +#define PNG_RGBA               0x0800
 75.2801 +#define PNG_EXPAND             0x1000
 75.2802 +#define PNG_GAMMA              0x2000
 75.2803 +#define PNG_GRAY_TO_RGB        0x4000
 75.2804 +#define PNG_FILLER             0x8000L
 75.2805 +#define PNG_PACKSWAP          0x10000L
 75.2806 +#define PNG_SWAP_ALPHA        0x20000L
 75.2807 +#define PNG_STRIP_ALPHA       0x40000L
 75.2808 +#define PNG_INVERT_ALPHA      0x80000L
 75.2809 +#define PNG_USER_TRANSFORM   0x100000L
 75.2810 +#define PNG_RGB_TO_GRAY_ERR  0x200000L
 75.2811 +#define PNG_RGB_TO_GRAY_WARN 0x400000L
 75.2812 +#define PNG_RGB_TO_GRAY      0x600000L  /* two bits, RGB_TO_GRAY_ERR|WARN */
 75.2813 +                       /*    0x800000L     Unused */
 75.2814 +#define PNG_ADD_ALPHA       0x1000000L  /* Added to libpng-1.2.7 */
 75.2815 +#define PNG_EXPAND_tRNS     0x2000000L  /* Added to libpng-1.2.9 */
 75.2816 +                       /*   0x4000000L  unused */
 75.2817 +                       /*   0x8000000L  unused */
 75.2818 +                       /*  0x10000000L  unused */
 75.2819 +                       /*  0x20000000L  unused */
 75.2820 +                       /*  0x40000000L  unused */
 75.2821 +
 75.2822 +/* flags for png_create_struct */
 75.2823 +#define PNG_STRUCT_PNG   0x0001
 75.2824 +#define PNG_STRUCT_INFO  0x0002
 75.2825 +
 75.2826 +/* Scaling factor for filter heuristic weighting calculations */
 75.2827 +#define PNG_WEIGHT_SHIFT 8
 75.2828 +#define PNG_WEIGHT_FACTOR (1<<(PNG_WEIGHT_SHIFT))
 75.2829 +#define PNG_COST_SHIFT 3
 75.2830 +#define PNG_COST_FACTOR (1<<(PNG_COST_SHIFT))
 75.2831 +
 75.2832 +/* flags for the png_ptr->flags rather than declaring a byte for each one */
 75.2833 +#define PNG_FLAG_ZLIB_CUSTOM_STRATEGY     0x0001
 75.2834 +#define PNG_FLAG_ZLIB_CUSTOM_LEVEL        0x0002
 75.2835 +#define PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL    0x0004
 75.2836 +#define PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS  0x0008
 75.2837 +#define PNG_FLAG_ZLIB_CUSTOM_METHOD       0x0010
 75.2838 +#define PNG_FLAG_ZLIB_FINISHED            0x0020
 75.2839 +#define PNG_FLAG_ROW_INIT                 0x0040
 75.2840 +#define PNG_FLAG_FILLER_AFTER             0x0080
 75.2841 +#define PNG_FLAG_CRC_ANCILLARY_USE        0x0100
 75.2842 +#define PNG_FLAG_CRC_ANCILLARY_NOWARN     0x0200
 75.2843 +#define PNG_FLAG_CRC_CRITICAL_USE         0x0400
 75.2844 +#define PNG_FLAG_CRC_CRITICAL_IGNORE      0x0800
 75.2845 +#define PNG_FLAG_FREE_PLTE                0x1000
 75.2846 +#define PNG_FLAG_FREE_TRNS                0x2000
 75.2847 +#define PNG_FLAG_FREE_HIST                0x4000
 75.2848 +#define PNG_FLAG_KEEP_UNKNOWN_CHUNKS      0x8000L
 75.2849 +#define PNG_FLAG_KEEP_UNSAFE_CHUNKS       0x10000L
 75.2850 +#define PNG_FLAG_LIBRARY_MISMATCH         0x20000L
 75.2851 +#define PNG_FLAG_STRIP_ERROR_NUMBERS      0x40000L
 75.2852 +#define PNG_FLAG_STRIP_ERROR_TEXT         0x80000L
 75.2853 +#define PNG_FLAG_MALLOC_NULL_MEM_OK       0x100000L
 75.2854 +#define PNG_FLAG_ADD_ALPHA                0x200000L  /* Added to libpng-1.2.8 */
 75.2855 +#define PNG_FLAG_STRIP_ALPHA              0x400000L  /* Added to libpng-1.2.8 */
 75.2856 +                                  /*      0x800000L  unused */
 75.2857 +                                  /*     0x1000000L  unused */
 75.2858 +                                  /*     0x2000000L  unused */
 75.2859 +                                  /*     0x4000000L  unused */
 75.2860 +                                  /*     0x8000000L  unused */
 75.2861 +                                  /*    0x10000000L  unused */
 75.2862 +                                  /*    0x20000000L  unused */
 75.2863 +                                  /*    0x40000000L  unused */
 75.2864 +
 75.2865 +#define PNG_FLAG_CRC_ANCILLARY_MASK (PNG_FLAG_CRC_ANCILLARY_USE | \
 75.2866 +                                     PNG_FLAG_CRC_ANCILLARY_NOWARN)
 75.2867 +
 75.2868 +#define PNG_FLAG_CRC_CRITICAL_MASK  (PNG_FLAG_CRC_CRITICAL_USE | \
 75.2869 +                                     PNG_FLAG_CRC_CRITICAL_IGNORE)
 75.2870 +
 75.2871 +#define PNG_FLAG_CRC_MASK           (PNG_FLAG_CRC_ANCILLARY_MASK | \
 75.2872 +                                     PNG_FLAG_CRC_CRITICAL_MASK)
 75.2873 +
 75.2874 +/* save typing and make code easier to understand */
 75.2875 +
 75.2876 +#define PNG_COLOR_DIST(c1, c2) (abs((int)((c1).red) - (int)((c2).red)) + \
 75.2877 +   abs((int)((c1).green) - (int)((c2).green)) + \
 75.2878 +   abs((int)((c1).blue) - (int)((c2).blue)))
 75.2879 +
 75.2880 +/* Added to libpng-1.2.6 JB */
 75.2881 +#define PNG_ROWBYTES(pixel_bits, width) \
 75.2882 +    ((pixel_bits) >= 8 ? \
 75.2883 +    ((width) * (((png_uint_32)(pixel_bits)) >> 3)) : \
 75.2884 +    (( ((width) * ((png_uint_32)(pixel_bits))) + 7) >> 3) )
 75.2885 +
 75.2886 +/* PNG_OUT_OF_RANGE returns true if value is outside the range
 75.2887 +   ideal-delta..ideal+delta.  Each argument is evaluated twice.
 75.2888 +   "ideal" and "delta" should be constants, normally simple
 75.2889 +   integers, "value" a variable. Added to libpng-1.2.6 JB */
 75.2890 +#define PNG_OUT_OF_RANGE(value, ideal, delta) \
 75.2891 +        ( (value) < (ideal)-(delta) || (value) > (ideal)+(delta) )
 75.2892 +
 75.2893 +/* variables declared in png.c - only it needs to define PNG_NO_EXTERN */
 75.2894 +#if !defined(PNG_NO_EXTERN) || defined(PNG_ALWAYS_EXTERN)
 75.2895 +/* place to hold the signature string for a PNG file. */
 75.2896 +#ifdef PNG_USE_GLOBAL_ARRAYS
 75.2897 +   PNG_EXPORT_VAR (PNG_CONST png_byte FARDATA) png_sig[8];
 75.2898 +#else
 75.2899 +#endif
 75.2900 +#endif /* PNG_NO_EXTERN */
 75.2901 +
 75.2902 +/* Constant strings for known chunk types.  If you need to add a chunk,
 75.2903 + * define the name here, and add an invocation of the macro in png.c and
 75.2904 + * wherever it's needed.
 75.2905 + */
 75.2906 +#define PNG_IHDR png_byte png_IHDR[5] = { 73,  72,  68,  82, '\0'}
 75.2907 +#define PNG_IDAT png_byte png_IDAT[5] = { 73,  68,  65,  84, '\0'}
 75.2908 +#define PNG_IEND png_byte png_IEND[5] = { 73,  69,  78,  68, '\0'}
 75.2909 +#define PNG_PLTE png_byte png_PLTE[5] = { 80,  76,  84,  69, '\0'}
 75.2910 +#define PNG_bKGD png_byte png_bKGD[5] = { 98,  75,  71,  68, '\0'}
 75.2911 +#define PNG_cHRM png_byte png_cHRM[5] = { 99,  72,  82,  77, '\0'}
 75.2912 +#define PNG_gAMA png_byte png_gAMA[5] = {103,  65,  77,  65, '\0'}
 75.2913 +#define PNG_hIST png_byte png_hIST[5] = {104,  73,  83,  84, '\0'}
 75.2914 +#define PNG_iCCP png_byte png_iCCP[5] = {105,  67,  67,  80, '\0'}
 75.2915 +#define PNG_iTXt png_byte png_iTXt[5] = {105,  84,  88, 116, '\0'}
 75.2916 +#define PNG_oFFs png_byte png_oFFs[5] = {111,  70,  70, 115, '\0'}
 75.2917 +#define PNG_pCAL png_byte png_pCAL[5] = {112,  67,  65,  76, '\0'}
 75.2918 +#define PNG_sCAL png_byte png_sCAL[5] = {115,  67,  65,  76, '\0'}
 75.2919 +#define PNG_pHYs png_byte png_pHYs[5] = {112,  72,  89, 115, '\0'}
 75.2920 +#define PNG_sBIT png_byte png_sBIT[5] = {115,  66,  73,  84, '\0'}
 75.2921 +#define PNG_sPLT png_byte png_sPLT[5] = {115,  80,  76,  84, '\0'}
 75.2922 +#define PNG_sRGB png_byte png_sRGB[5] = {115,  82,  71,  66, '\0'}
 75.2923 +#define PNG_tEXt png_byte png_tEXt[5] = {116,  69,  88, 116, '\0'}
 75.2924 +#define PNG_tIME png_byte png_tIME[5] = {116,  73,  77,  69, '\0'}
 75.2925 +#define PNG_tRNS png_byte png_tRNS[5] = {116,  82,  78,  83, '\0'}
 75.2926 +#define PNG_zTXt png_byte png_zTXt[5] = {122,  84,  88, 116, '\0'}
 75.2927 +
 75.2928 +#ifdef PNG_USE_GLOBAL_ARRAYS
 75.2929 +PNG_EXPORT_VAR (png_byte FARDATA) png_IHDR[5];
 75.2930 +PNG_EXPORT_VAR (png_byte FARDATA) png_IDAT[5];
 75.2931 +PNG_EXPORT_VAR (png_byte FARDATA) png_IEND[5];
 75.2932 +PNG_EXPORT_VAR (png_byte FARDATA) png_PLTE[5];
 75.2933 +PNG_EXPORT_VAR (png_byte FARDATA) png_bKGD[5];
 75.2934 +PNG_EXPORT_VAR (png_byte FARDATA) png_cHRM[5];
 75.2935 +PNG_EXPORT_VAR (png_byte FARDATA) png_gAMA[5];
 75.2936 +PNG_EXPORT_VAR (png_byte FARDATA) png_hIST[5];
 75.2937 +PNG_EXPORT_VAR (png_byte FARDATA) png_iCCP[5];
 75.2938 +PNG_EXPORT_VAR (png_byte FARDATA) png_iTXt[5];
 75.2939 +PNG_EXPORT_VAR (png_byte FARDATA) png_oFFs[5];
 75.2940 +PNG_EXPORT_VAR (png_byte FARDATA) png_pCAL[5];
 75.2941 +PNG_EXPORT_VAR (png_byte FARDATA) png_sCAL[5];
 75.2942 +PNG_EXPORT_VAR (png_byte FARDATA) png_pHYs[5];
 75.2943 +PNG_EXPORT_VAR (png_byte FARDATA) png_sBIT[5];
 75.2944 +PNG_EXPORT_VAR (png_byte FARDATA) png_sPLT[5];
 75.2945 +PNG_EXPORT_VAR (png_byte FARDATA) png_sRGB[5];
 75.2946 +PNG_EXPORT_VAR (png_byte FARDATA) png_tEXt[5];
 75.2947 +PNG_EXPORT_VAR (png_byte FARDATA) png_tIME[5];
 75.2948 +PNG_EXPORT_VAR (png_byte FARDATA) png_tRNS[5];
 75.2949 +PNG_EXPORT_VAR (png_byte FARDATA) png_zTXt[5];
 75.2950 +#endif /* PNG_USE_GLOBAL_ARRAYS */
 75.2951 +
 75.2952 +#if defined(PNG_1_0_X) || defined (PNG_1_2_X)
 75.2953 +/* Initialize png_ptr struct for reading, and allocate any other memory.
 75.2954 + * (old interface - DEPRECATED - use png_create_read_struct instead).
 75.2955 + */
 75.2956 +extern PNG_EXPORT(void,png_read_init) PNGARG((png_structp png_ptr));
 75.2957 +#undef png_read_init
 75.2958 +#define png_read_init(png_ptr) png_read_init_3(&png_ptr, \
 75.2959 +    PNG_LIBPNG_VER_STRING,  png_sizeof(png_struct));
 75.2960 +#endif
 75.2961 +
 75.2962 +extern PNG_EXPORT(void,png_read_init_3) PNGARG((png_structpp ptr_ptr,
 75.2963 +    png_const_charp user_png_ver, png_size_t png_struct_size));
 75.2964 +#if defined(PNG_1_0_X) || defined (PNG_1_2_X)
 75.2965 +extern PNG_EXPORT(void,png_read_init_2) PNGARG((png_structp png_ptr,
 75.2966 +    png_const_charp user_png_ver, png_size_t png_struct_size, png_size_t
 75.2967 +    png_info_size));
 75.2968 +#endif
 75.2969 +
 75.2970 +#if defined(PNG_1_0_X) || defined (PNG_1_2_X)
 75.2971 +/* Initialize png_ptr struct for writing, and allocate any other memory.
 75.2972 + * (old interface - DEPRECATED - use png_create_write_struct instead).
 75.2973 + */
 75.2974 +extern PNG_EXPORT(void,png_write_init) PNGARG((png_structp png_ptr));
 75.2975 +#undef png_write_init
 75.2976 +#define png_write_init(png_ptr) png_write_init_3(&png_ptr, \
 75.2977 +    PNG_LIBPNG_VER_STRING, png_sizeof(png_struct));
 75.2978 +#endif
 75.2979 +
 75.2980 +extern PNG_EXPORT(void,png_write_init_3) PNGARG((png_structpp ptr_ptr,
 75.2981 +    png_const_charp user_png_ver, png_size_t png_struct_size));
 75.2982 +extern PNG_EXPORT(void,png_write_init_2) PNGARG((png_structp png_ptr,
 75.2983 +    png_const_charp user_png_ver, png_size_t png_struct_size, png_size_t
 75.2984 +    png_info_size));
 75.2985 +
 75.2986 +/* Allocate memory for an internal libpng struct */
 75.2987 +PNG_EXTERN png_voidp png_create_struct PNGARG((int type));
 75.2988 +
 75.2989 +/* Free memory from internal libpng struct */
 75.2990 +PNG_EXTERN void png_destroy_struct PNGARG((png_voidp struct_ptr));
 75.2991 +
 75.2992 +PNG_EXTERN png_voidp png_create_struct_2 PNGARG((int type, png_malloc_ptr
 75.2993 +  malloc_fn, png_voidp mem_ptr));
 75.2994 +PNG_EXTERN void png_destroy_struct_2 PNGARG((png_voidp struct_ptr,
 75.2995 +   png_free_ptr free_fn, png_voidp mem_ptr));
 75.2996 +
 75.2997 +/* Free any memory that info_ptr points to and reset struct. */
 75.2998 +PNG_EXTERN void png_info_destroy PNGARG((png_structp png_ptr,
 75.2999 +   png_infop info_ptr));
 75.3000 +
 75.3001 +#ifndef PNG_1_0_X
 75.3002 +/* Function to allocate memory for zlib. */
 75.3003 +PNG_EXTERN voidpf png_zalloc PNGARG((voidpf png_ptr, uInt items, uInt size));
 75.3004 +
 75.3005 +/* Function to free memory for zlib */
 75.3006 +PNG_EXTERN void png_zfree PNGARG((voidpf png_ptr, voidpf ptr));
 75.3007 +
 75.3008 +#ifdef PNG_SIZE_T
 75.3009 +/* Function to convert a sizeof an item to png_sizeof item */
 75.3010 +   PNG_EXTERN png_size_t PNGAPI png_convert_size PNGARG((size_t size));
 75.3011 +#endif
 75.3012 +
 75.3013 +/* Next four functions are used internally as callbacks.  PNGAPI is required
 75.3014 + * but not PNG_EXPORT.  PNGAPI added at libpng version 1.2.3. */
 75.3015 +
 75.3016 +PNG_EXTERN void PNGAPI png_default_read_data PNGARG((png_structp png_ptr,
 75.3017 +   png_bytep data, png_size_t length));
 75.3018 +
 75.3019 +#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
 75.3020 +PNG_EXTERN void PNGAPI png_push_fill_buffer PNGARG((png_structp png_ptr,
 75.3021 +   png_bytep buffer, png_size_t length));
 75.3022 +#endif
 75.3023 +
 75.3024 +PNG_EXTERN void PNGAPI png_default_write_data PNGARG((png_structp png_ptr,
 75.3025 +   png_bytep data, png_size_t length));
 75.3026 +
 75.3027 +#if defined(PNG_WRITE_FLUSH_SUPPORTED)
 75.3028 +#if !defined(PNG_NO_STDIO)
 75.3029 +PNG_EXTERN void PNGAPI png_default_flush PNGARG((png_structp png_ptr));
 75.3030 +#endif
 75.3031 +#endif
 75.3032 +#else /* PNG_1_0_X */
 75.3033 +#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
 75.3034 +PNG_EXTERN void png_push_fill_buffer PNGARG((png_structp png_ptr,
 75.3035 +   png_bytep buffer, png_size_t length));
 75.3036 +#endif
 75.3037 +#endif /* PNG_1_0_X */
 75.3038 +
 75.3039 +/* Reset the CRC variable */
 75.3040 +PNG_EXTERN void png_reset_crc PNGARG((png_structp png_ptr));
 75.3041 +
 75.3042 +/* Write the "data" buffer to whatever output you are using. */
 75.3043 +PNG_EXTERN void png_write_data PNGARG((png_structp png_ptr, png_bytep data,
 75.3044 +   png_size_t length));
 75.3045 +
 75.3046 +/* Read data from whatever input you are using into the "data" buffer */
 75.3047 +PNG_EXTERN void png_read_data PNGARG((png_structp png_ptr, png_bytep data,
 75.3048 +   png_size_t length));
 75.3049 +
 75.3050 +/* Read bytes into buf, and update png_ptr->crc */
 75.3051 +PNG_EXTERN void png_crc_read PNGARG((png_structp png_ptr, png_bytep buf,
 75.3052 +   png_size_t length));
 75.3053 +
 75.3054 +/* Decompress data in a chunk that uses compression */
 75.3055 +#if defined(PNG_zTXt_SUPPORTED) || defined(PNG_iTXt_SUPPORTED) || \
 75.3056 +    defined(PNG_iCCP_SUPPORTED) || defined(PNG_sPLT_SUPPORTED)
 75.3057 +PNG_EXTERN void png_decompress_chunk PNGARG((png_structp png_ptr,
 75.3058 +   int comp_type, png_size_t chunklength,
 75.3059 +   png_size_t prefix_length, png_size_t *data_length));
 75.3060 +#endif
 75.3061 +
 75.3062 +/* Read "skip" bytes, read the file crc, and (optionally) verify png_ptr->crc */
 75.3063 +PNG_EXTERN int png_crc_finish PNGARG((png_structp png_ptr, png_uint_32 skip));
 75.3064 +
 75.3065 +/* Read the CRC from the file and compare it to the libpng calculated CRC */
 75.3066 +PNG_EXTERN int png_crc_error PNGARG((png_structp png_ptr));
 75.3067 +
 75.3068 +/* Calculate the CRC over a section of data.  Note that we are only
 75.3069 + * passing a maximum of 64K on systems that have this as a memory limit,
 75.3070 + * since this is the maximum buffer size we can specify.
 75.3071 + */
 75.3072 +PNG_EXTERN void png_calculate_crc PNGARG((png_structp png_ptr, png_bytep ptr,
 75.3073 +   png_size_t length));
 75.3074 +
 75.3075 +#if defined(PNG_WRITE_FLUSH_SUPPORTED)
 75.3076 +PNG_EXTERN void png_flush PNGARG((png_structp png_ptr));
 75.3077 +#endif
 75.3078 +
 75.3079 +/* simple function to write the signature */
 75.3080 +PNG_EXTERN void png_write_sig PNGARG((png_structp png_ptr));
 75.3081 +
 75.3082 +/* write various chunks */
 75.3083 +
 75.3084 +/* Write the IHDR chunk, and update the png_struct with the necessary
 75.3085 + * information.
 75.3086 + */
 75.3087 +PNG_EXTERN void png_write_IHDR PNGARG((png_structp png_ptr, png_uint_32 width,
 75.3088 +   png_uint_32 height,
 75.3089 +   int bit_depth, int color_type, int compression_method, int filter_method,
 75.3090 +   int interlace_method));
 75.3091 +
 75.3092 +PNG_EXTERN void png_write_PLTE PNGARG((png_structp png_ptr, png_colorp palette,
 75.3093 +   png_uint_32 num_pal));
 75.3094 +
 75.3095 +PNG_EXTERN void png_write_IDAT PNGARG((png_structp png_ptr, png_bytep data,
 75.3096 +   png_size_t length));
 75.3097 +
 75.3098 +PNG_EXTERN void png_write_IEND PNGARG((png_structp png_ptr));
 75.3099 +
 75.3100 +#if defined(PNG_WRITE_gAMA_SUPPORTED)
 75.3101 +#ifdef PNG_FLOATING_POINT_SUPPORTED
 75.3102 +PNG_EXTERN void png_write_gAMA PNGARG((png_structp png_ptr, double file_gamma));
 75.3103 +#endif
 75.3104 +#ifdef PNG_FIXED_POINT_SUPPORTED
 75.3105 +PNG_EXTERN void png_write_gAMA_fixed PNGARG((png_structp png_ptr, png_fixed_point
 75.3106 +    file_gamma));
 75.3107 +#endif
 75.3108 +#endif
 75.3109 +
 75.3110 +#if defined(PNG_WRITE_sBIT_SUPPORTED)
 75.3111 +PNG_EXTERN void png_write_sBIT PNGARG((png_structp png_ptr, png_color_8p sbit,
 75.3112 +   int color_type));
 75.3113 +#endif
 75.3114 +
 75.3115 +#if defined(PNG_WRITE_cHRM_SUPPORTED)
 75.3116 +#ifdef PNG_FLOATING_POINT_SUPPORTED
 75.3117 +PNG_EXTERN void png_write_cHRM PNGARG((png_structp png_ptr,
 75.3118 +   double white_x, double white_y,
 75.3119 +   double red_x, double red_y, double green_x, double green_y,
 75.3120 +   double blue_x, double blue_y));
 75.3121 +#endif
 75.3122 +#ifdef PNG_FIXED_POINT_SUPPORTED
 75.3123 +PNG_EXTERN void png_write_cHRM_fixed PNGARG((png_structp png_ptr,
 75.3124 +   png_fixed_point int_white_x, png_fixed_point int_white_y,
 75.3125 +   png_fixed_point int_red_x, png_fixed_point int_red_y, png_fixed_point
 75.3126 +   int_green_x, png_fixed_point int_green_y, png_fixed_point int_blue_x,
 75.3127 +   png_fixed_point int_blue_y));
 75.3128 +#endif
 75.3129 +#endif
 75.3130 +
 75.3131 +#if defined(PNG_WRITE_sRGB_SUPPORTED)
 75.3132 +PNG_EXTERN void png_write_sRGB PNGARG((png_structp png_ptr,
 75.3133 +   int intent));
 75.3134 +#endif
 75.3135 +
 75.3136 +#if defined(PNG_WRITE_iCCP_SUPPORTED)
 75.3137 +PNG_EXTERN void png_write_iCCP PNGARG((png_structp png_ptr,
 75.3138 +   png_charp name, int compression_type,
 75.3139 +   png_charp profile, int proflen));
 75.3140 +   /* Note to maintainer: profile should be png_bytep */
 75.3141 +#endif
 75.3142 +
 75.3143 +#if defined(PNG_WRITE_sPLT_SUPPORTED)
 75.3144 +PNG_EXTERN void png_write_sPLT PNGARG((png_structp png_ptr,
 75.3145 +   png_sPLT_tp palette));
 75.3146 +#endif
 75.3147 +
 75.3148 +#if defined(PNG_WRITE_tRNS_SUPPORTED)
 75.3149 +PNG_EXTERN void png_write_tRNS PNGARG((png_structp png_ptr, png_bytep trans,
 75.3150 +   png_color_16p values, int number, int color_type));
 75.3151 +#endif
 75.3152 +
 75.3153 +#if defined(PNG_WRITE_bKGD_SUPPORTED)
 75.3154 +PNG_EXTERN void png_write_bKGD PNGARG((png_structp png_ptr,
 75.3155 +   png_color_16p values, int color_type));
 75.3156 +#endif
 75.3157 +
 75.3158 +#if defined(PNG_WRITE_hIST_SUPPORTED)
 75.3159 +PNG_EXTERN void png_write_hIST PNGARG((png_structp png_ptr, png_uint_16p hist,
 75.3160 +   int num_hist));
 75.3161 +#endif
 75.3162 +
 75.3163 +#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_pCAL_SUPPORTED) || \
 75.3164 +    defined(PNG_WRITE_iCCP_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
 75.3165 +PNG_EXTERN png_size_t png_check_keyword PNGARG((png_structp png_ptr,
 75.3166 +   png_charp key, png_charpp new_key));
 75.3167 +#endif
 75.3168 +
 75.3169 +#if defined(PNG_WRITE_tEXt_SUPPORTED)
 75.3170 +PNG_EXTERN void png_write_tEXt PNGARG((png_structp png_ptr, png_charp key,
 75.3171 +   png_charp text, png_size_t text_len));
 75.3172 +#endif
 75.3173 +
 75.3174 +#if defined(PNG_WRITE_zTXt_SUPPORTED)
 75.3175 +PNG_EXTERN void png_write_zTXt PNGARG((png_structp png_ptr, png_charp key,
 75.3176 +   png_charp text, png_size_t text_len, int compression));
 75.3177 +#endif
 75.3178 +
 75.3179 +#if defined(PNG_WRITE_iTXt_SUPPORTED)
 75.3180 +PNG_EXTERN void png_write_iTXt PNGARG((png_structp png_ptr,
 75.3181 +   int compression, png_charp key, png_charp lang, png_charp lang_key,
 75.3182 +   png_charp text));
 75.3183 +#endif
 75.3184 +
 75.3185 +#if defined(PNG_TEXT_SUPPORTED)  /* Added at version 1.0.14 and 1.2.4 */
 75.3186 +PNG_EXTERN int png_set_text_2 PNGARG((png_structp png_ptr,
 75.3187 +   png_infop info_ptr, png_textp text_ptr, int num_text));
 75.3188 +#endif
 75.3189 +
 75.3190 +#if defined(PNG_WRITE_oFFs_SUPPORTED)
 75.3191 +PNG_EXTERN void png_write_oFFs PNGARG((png_structp png_ptr,
 75.3192 +   png_int_32 x_offset, png_int_32 y_offset, int unit_type));
 75.3193 +#endif
 75.3194 +
 75.3195 +#if defined(PNG_WRITE_pCAL_SUPPORTED)
 75.3196 +PNG_EXTERN void png_write_pCAL PNGARG((png_structp png_ptr, png_charp purpose,
 75.3197 +   png_int_32 X0, png_int_32 X1, int type, int nparams,
 75.3198 +   png_charp units, png_charpp params));
 75.3199 +#endif
 75.3200 +
 75.3201 +#if defined(PNG_WRITE_pHYs_SUPPORTED)
 75.3202 +PNG_EXTERN void png_write_pHYs PNGARG((png_structp png_ptr,
 75.3203 +   png_uint_32 x_pixels_per_unit, png_uint_32 y_pixels_per_unit,
 75.3204 +   int unit_type));
 75.3205 +#endif
 75.3206 +
 75.3207 +#if defined(PNG_WRITE_tIME_SUPPORTED)
 75.3208 +PNG_EXTERN void png_write_tIME PNGARG((png_structp png_ptr,
 75.3209 +   png_timep mod_time));
 75.3210 +#endif
 75.3211 +
 75.3212 +#if defined(PNG_WRITE_sCAL_SUPPORTED)
 75.3213 +#if defined(PNG_FLOATING_POINT_SUPPORTED) && !defined(PNG_NO_STDIO)
 75.3214 +PNG_EXTERN void png_write_sCAL PNGARG((png_structp png_ptr,
 75.3215 +   int unit, double width, double height));
 75.3216 +#else
 75.3217 +#ifdef PNG_FIXED_POINT_SUPPORTED
 75.3218 +PNG_EXTERN void png_write_sCAL_s PNGARG((png_structp png_ptr,
 75.3219 +   int unit, png_charp width, png_charp height));
 75.3220 +#endif
 75.3221 +#endif
 75.3222 +#endif
 75.3223 +
 75.3224 +/* Called when finished processing a row of data */
 75.3225 +PNG_EXTERN void png_write_finish_row PNGARG((png_structp png_ptr));
 75.3226 +
 75.3227 +/* Internal use only.   Called before first row of data */
 75.3228 +PNG_EXTERN void png_write_start_row PNGARG((png_structp png_ptr));
 75.3229 +
 75.3230 +#if defined(PNG_READ_GAMMA_SUPPORTED)
 75.3231 +PNG_EXTERN void png_build_gamma_table PNGARG((png_structp png_ptr));
 75.3232 +#endif
 75.3233 +
 75.3234 +/* combine a row of data, dealing with alpha, etc. if requested */
 75.3235 +PNG_EXTERN void png_combine_row PNGARG((png_structp png_ptr, png_bytep row,
 75.3236 +   int mask));
 75.3237 +
 75.3238 +#if defined(PNG_READ_INTERLACING_SUPPORTED)
 75.3239 +/* expand an interlaced row */
 75.3240 +/* OLD pre-1.0.9 interface:
 75.3241 +PNG_EXTERN void png_do_read_interlace PNGARG((png_row_infop row_info,
 75.3242 +   png_bytep row, int pass, png_uint_32 transformations));
 75.3243 + */
 75.3244 +PNG_EXTERN void png_do_read_interlace PNGARG((png_structp png_ptr));
 75.3245 +#endif
 75.3246 +
 75.3247 +/* GRR TO DO (2.0 or whenever):  simplify other internal calling interfaces */
 75.3248 +
 75.3249 +#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
 75.3250 +/* grab pixels out of a row for an interlaced pass */
 75.3251 +PNG_EXTERN void png_do_write_interlace PNGARG((png_row_infop row_info,
 75.3252 +   png_bytep row, int pass));
 75.3253 +#endif
 75.3254 +
 75.3255 +/* unfilter a row */
 75.3256 +PNG_EXTERN void png_read_filter_row PNGARG((png_structp png_ptr,
 75.3257 +   png_row_infop row_info, png_bytep row, png_bytep prev_row, int filter));
 75.3258 +
 75.3259 +/* Choose the best filter to use and filter the row data */
 75.3260 +PNG_EXTERN void png_write_find_filter PNGARG((png_structp png_ptr,
 75.3261 +   png_row_infop row_info));
 75.3262 +
 75.3263 +/* Write out the filtered row. */
 75.3264 +PNG_EXTERN void png_write_filtered_row PNGARG((png_structp png_ptr,
 75.3265 +   png_bytep filtered_row));
 75.3266 +/* finish a row while reading, dealing with interlacing passes, etc. */
 75.3267 +PNG_EXTERN void png_read_finish_row PNGARG((png_structp png_ptr));
 75.3268 +
 75.3269 +/* initialize the row buffers, etc. */
 75.3270 +PNG_EXTERN void png_read_start_row PNGARG((png_structp png_ptr));
 75.3271 +/* optional call to update the users info structure */
 75.3272 +PNG_EXTERN void png_read_transform_info PNGARG((png_structp png_ptr,
 75.3273 +   png_infop info_ptr));
 75.3274 +
 75.3275 +/* these are the functions that do the transformations */
 75.3276 +#if defined(PNG_READ_FILLER_SUPPORTED)
 75.3277 +PNG_EXTERN void png_do_read_filler PNGARG((png_row_infop row_info,
 75.3278 +   png_bytep row, png_uint_32 filler, png_uint_32 flags));
 75.3279 +#endif
 75.3280 +
 75.3281 +#if defined(PNG_READ_SWAP_ALPHA_SUPPORTED)
 75.3282 +PNG_EXTERN void png_do_read_swap_alpha PNGARG((png_row_infop row_info,
 75.3283 +   png_bytep row));
 75.3284 +#endif
 75.3285 +
 75.3286 +#if defined(PNG_WRITE_SWAP_ALPHA_SUPPORTED)
 75.3287 +PNG_EXTERN void png_do_write_swap_alpha PNGARG((png_row_infop row_info,
 75.3288 +   png_bytep row));
 75.3289 +#endif
 75.3290 +
 75.3291 +#if defined(PNG_READ_INVERT_ALPHA_SUPPORTED)
 75.3292 +PNG_EXTERN void png_do_read_invert_alpha PNGARG((png_row_infop row_info,
 75.3293 +   png_bytep row));
 75.3294 +#endif
 75.3295 +
 75.3296 +#if defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED)
 75.3297 +PNG_EXTERN void png_do_write_invert_alpha PNGARG((png_row_infop row_info,
 75.3298 +   png_bytep row));
 75.3299 +#endif
 75.3300 +
 75.3301 +#if defined(PNG_WRITE_FILLER_SUPPORTED) || \
 75.3302 +    defined(PNG_READ_STRIP_ALPHA_SUPPORTED)
 75.3303 +PNG_EXTERN void png_do_strip_filler PNGARG((png_row_infop row_info,
 75.3304 +   png_bytep row, png_uint_32 flags));
 75.3305 +#endif
 75.3306 +
 75.3307 +#if defined(PNG_READ_SWAP_SUPPORTED) || defined(PNG_WRITE_SWAP_SUPPORTED)
 75.3308 +PNG_EXTERN void png_do_swap PNGARG((png_row_infop row_info, png_bytep row));
 75.3309 +#endif
 75.3310 +
 75.3311 +#if defined(PNG_READ_PACKSWAP_SUPPORTED) || defined(PNG_WRITE_PACKSWAP_SUPPORTED)
 75.3312 +PNG_EXTERN void png_do_packswap PNGARG((png_row_infop row_info, png_bytep row));
 75.3313 +#endif
 75.3314 +
 75.3315 +#if defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
 75.3316 +PNG_EXTERN int png_do_rgb_to_gray PNGARG((png_structp png_ptr, png_row_infop
 75.3317 +   row_info, png_bytep row));
 75.3318 +#endif
 75.3319 +
 75.3320 +#if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED)
 75.3321 +PNG_EXTERN void png_do_gray_to_rgb PNGARG((png_row_infop row_info,
 75.3322 +   png_bytep row));
 75.3323 +#endif
 75.3324 +
 75.3325 +#if defined(PNG_READ_PACK_SUPPORTED)
 75.3326 +PNG_EXTERN void png_do_unpack PNGARG((png_row_infop row_info, png_bytep row));
 75.3327 +#endif
 75.3328 +
 75.3329 +#if defined(PNG_READ_SHIFT_SUPPORTED)
 75.3330 +PNG_EXTERN void png_do_unshift PNGARG((png_row_infop row_info, png_bytep row,
 75.3331 +   png_color_8p sig_bits));
 75.3332 +#endif
 75.3333 +
 75.3334 +#if defined(PNG_READ_INVERT_SUPPORTED) || defined(PNG_WRITE_INVERT_SUPPORTED)
 75.3335 +PNG_EXTERN void png_do_invert PNGARG((png_row_infop row_info, png_bytep row));
 75.3336 +#endif
 75.3337 +
 75.3338 +#if defined(PNG_READ_16_TO_8_SUPPORTED)
 75.3339 +PNG_EXTERN void png_do_chop PNGARG((png_row_infop row_info, png_bytep row));
 75.3340 +#endif
 75.3341 +
 75.3342 +#if defined(PNG_READ_DITHER_SUPPORTED)
 75.3343 +PNG_EXTERN void png_do_dither PNGARG((png_row_infop row_info,
 75.3344 +   png_bytep row, png_bytep palette_lookup, png_bytep dither_lookup));
 75.3345 +
 75.3346 +#  if defined(PNG_CORRECT_PALETTE_SUPPORTED)
 75.3347 +PNG_EXTERN void png_correct_palette PNGARG((png_structp png_ptr,
 75.3348 +   png_colorp palette, int num_palette));
 75.3349 +#  endif
 75.3350 +#endif
 75.3351 +
 75.3352 +#if defined(PNG_READ_BGR_SUPPORTED) || defined(PNG_WRITE_BGR_SUPPORTED)
 75.3353 +PNG_EXTERN void png_do_bgr PNGARG((png_row_infop row_info, png_bytep row));
 75.3354 +#endif
 75.3355 +
 75.3356 +#if defined(PNG_WRITE_PACK_SUPPORTED)
 75.3357 +PNG_EXTERN void png_do_pack PNGARG((png_row_infop row_info,
 75.3358 +   png_bytep row, png_uint_32 bit_depth));
 75.3359 +#endif
 75.3360 +
 75.3361 +#if defined(PNG_WRITE_SHIFT_SUPPORTED)
 75.3362 +PNG_EXTERN void png_do_shift PNGARG((png_row_infop row_info, png_bytep row,
 75.3363 +   png_color_8p bit_depth));
 75.3364 +#endif
 75.3365 +
 75.3366 +#if defined(PNG_READ_BACKGROUND_SUPPORTED)
 75.3367 +#if defined(PNG_READ_GAMMA_SUPPORTED)
 75.3368 +PNG_EXTERN void png_do_background PNGARG((png_row_infop row_info, png_bytep row,
 75.3369 +   png_color_16p trans_values, png_color_16p background,
 75.3370 +   png_color_16p background_1,
 75.3371 +   png_bytep gamma_table, png_bytep gamma_from_1, png_bytep gamma_to_1,
 75.3372 +   png_uint_16pp gamma_16, png_uint_16pp gamma_16_from_1,
 75.3373 +   png_uint_16pp gamma_16_to_1, int gamma_shift));
 75.3374 +#else
 75.3375 +PNG_EXTERN void png_do_background PNGARG((png_row_infop row_info, png_bytep row,
 75.3376 +   png_color_16p trans_values, png_color_16p background));
 75.3377 +#endif
 75.3378 +#endif
 75.3379 +
 75.3380 +#if defined(PNG_READ_GAMMA_SUPPORTED)
 75.3381 +PNG_EXTERN void png_do_gamma PNGARG((png_row_infop row_info, png_bytep row,
 75.3382 +   png_bytep gamma_table, png_uint_16pp gamma_16_table,
 75.3383 +   int gamma_shift));
 75.3384 +#endif
 75.3385 +
 75.3386 +#if defined(PNG_READ_EXPAND_SUPPORTED)
 75.3387 +PNG_EXTERN void png_do_expand_palette PNGARG((png_row_infop row_info,
 75.3388 +   png_bytep row, png_colorp palette, png_bytep trans, int num_trans));
 75.3389 +PNG_EXTERN void png_do_expand PNGARG((png_row_infop row_info,
 75.3390 +   png_bytep row, png_color_16p trans_value));
 75.3391 +#endif
 75.3392 +
 75.3393 +/* The following decodes the appropriate chunks, and does error correction,
 75.3394 + * then calls the appropriate callback for the chunk if it is valid.
 75.3395 + */
 75.3396 +
 75.3397 +/* decode the IHDR chunk */
 75.3398 +PNG_EXTERN void png_handle_IHDR PNGARG((png_structp png_ptr, png_infop info_ptr,
 75.3399 +   png_uint_32 length));
 75.3400 +PNG_EXTERN void png_handle_PLTE PNGARG((png_structp png_ptr, png_infop info_ptr,
 75.3401 +   png_uint_32 length));
 75.3402 +PNG_EXTERN void png_handle_IEND PNGARG((png_structp png_ptr, png_infop info_ptr,
 75.3403 +   png_uint_32 length));
 75.3404 +
 75.3405 +#if defined(PNG_READ_bKGD_SUPPORTED)
 75.3406 +PNG_EXTERN void png_handle_bKGD PNGARG((png_structp png_ptr, png_infop info_ptr,
 75.3407 +   png_uint_32 length));
 75.3408 +#endif
 75.3409 +
 75.3410 +#if defined(PNG_READ_cHRM_SUPPORTED)
 75.3411 +PNG_EXTERN void png_handle_cHRM PNGARG((png_structp png_ptr, png_infop info_ptr,
 75.3412 +   png_uint_32 length));
 75.3413 +#endif
 75.3414 +
 75.3415 +#if defined(PNG_READ_gAMA_SUPPORTED)
 75.3416 +PNG_EXTERN void png_handle_gAMA PNGARG((png_structp png_ptr, png_infop info_ptr,
 75.3417 +   png_uint_32 length));
 75.3418 +#endif
 75.3419 +
 75.3420 +#if defined(PNG_READ_hIST_SUPPORTED)
 75.3421 +PNG_EXTERN void png_handle_hIST PNGARG((png_structp png_ptr, png_infop info_ptr,
 75.3422 +   png_uint_32 length));
 75.3423 +#endif
 75.3424 +
 75.3425 +#if defined(PNG_READ_iCCP_SUPPORTED)
 75.3426 +extern void png_handle_iCCP PNGARG((png_structp png_ptr, png_infop info_ptr,
 75.3427 +   png_uint_32 length));
 75.3428 +#endif /* PNG_READ_iCCP_SUPPORTED */
 75.3429 +
 75.3430 +#if defined(PNG_READ_iTXt_SUPPORTED)
 75.3431 +PNG_EXTERN void png_handle_iTXt PNGARG((png_structp png_ptr, png_infop info_ptr,
 75.3432 +   png_uint_32 length));
 75.3433 +#endif
 75.3434 +
 75.3435 +#if defined(PNG_READ_oFFs_SUPPORTED)
 75.3436 +PNG_EXTERN void png_handle_oFFs PNGARG((png_structp png_ptr, png_infop info_ptr,
 75.3437 +   png_uint_32 length));
 75.3438 +#endif
 75.3439 +
 75.3440 +#if defined(PNG_READ_pCAL_SUPPORTED)
 75.3441 +PNG_EXTERN void png_handle_pCAL PNGARG((png_structp png_ptr, png_infop info_ptr,
 75.3442 +   png_uint_32 length));
 75.3443 +#endif
 75.3444 +
 75.3445 +#if defined(PNG_READ_pHYs_SUPPORTED)
 75.3446 +PNG_EXTERN void png_handle_pHYs PNGARG((png_structp png_ptr, png_infop info_ptr,
 75.3447 +   png_uint_32 length));
 75.3448 +#endif
 75.3449 +
 75.3450 +#if defined(PNG_READ_sBIT_SUPPORTED)
 75.3451 +PNG_EXTERN void png_handle_sBIT PNGARG((png_structp png_ptr, png_infop info_ptr,
 75.3452 +   png_uint_32 length));
 75.3453 +#endif
 75.3454 +
 75.3455 +#if defined(PNG_READ_sCAL_SUPPORTED)
 75.3456 +PNG_EXTERN void png_handle_sCAL PNGARG((png_structp png_ptr, png_infop info_ptr,
 75.3457 +   png_uint_32 length));
 75.3458 +#endif
 75.3459 +
 75.3460 +#if defined(PNG_READ_sPLT_SUPPORTED)
 75.3461 +extern void png_handle_sPLT PNGARG((png_structp png_ptr, png_infop info_ptr,
 75.3462 +   png_uint_32 length));
 75.3463 +#endif /* PNG_READ_sPLT_SUPPORTED */
 75.3464 +
 75.3465 +#if defined(PNG_READ_sRGB_SUPPORTED)
 75.3466 +PNG_EXTERN void png_handle_sRGB PNGARG((png_structp png_ptr, png_infop info_ptr,
 75.3467 +   png_uint_32 length));
 75.3468 +#endif
 75.3469 +
 75.3470 +#if defined(PNG_READ_tEXt_SUPPORTED)
 75.3471 +PNG_EXTERN void png_handle_tEXt PNGARG((png_structp png_ptr, png_infop info_ptr,
 75.3472 +   png_uint_32 length));
 75.3473 +#endif
 75.3474 +
 75.3475 +#if defined(PNG_READ_tIME_SUPPORTED)
 75.3476 +PNG_EXTERN void png_handle_tIME PNGARG((png_structp png_ptr, png_infop info_ptr,
 75.3477 +   png_uint_32 length));
 75.3478 +#endif
 75.3479 +
 75.3480 +#if defined(PNG_READ_tRNS_SUPPORTED)
 75.3481 +PNG_EXTERN void png_handle_tRNS PNGARG((png_structp png_ptr, png_infop info_ptr,
 75.3482 +   png_uint_32 length));
 75.3483 +#endif
 75.3484 +
 75.3485 +#if defined(PNG_READ_zTXt_SUPPORTED)
 75.3486 +PNG_EXTERN void png_handle_zTXt PNGARG((png_structp png_ptr, png_infop info_ptr,
 75.3487 +   png_uint_32 length));
 75.3488 +#endif
 75.3489 +
 75.3490 +PNG_EXTERN void png_handle_unknown PNGARG((png_structp png_ptr,
 75.3491 +   png_infop info_ptr, png_uint_32 length));
 75.3492 +
 75.3493 +PNG_EXTERN void png_check_chunk_name PNGARG((png_structp png_ptr,
 75.3494 +   png_bytep chunk_name));
 75.3495 +
 75.3496 +/* handle the transformations for reading and writing */
 75.3497 +PNG_EXTERN void png_do_read_transformations PNGARG((png_structp png_ptr));
 75.3498 +PNG_EXTERN void png_do_write_transformations PNGARG((png_structp png_ptr));
 75.3499 +
 75.3500 +PNG_EXTERN void png_init_read_transformations PNGARG((png_structp png_ptr));
 75.3501 +
 75.3502 +#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
 75.3503 +PNG_EXTERN void png_push_read_chunk PNGARG((png_structp png_ptr,
 75.3504 +   png_infop info_ptr));
 75.3505 +PNG_EXTERN void png_push_read_sig PNGARG((png_structp png_ptr,
 75.3506 +   png_infop info_ptr));
 75.3507 +PNG_EXTERN void png_push_check_crc PNGARG((png_structp png_ptr));
 75.3508 +PNG_EXTERN void png_push_crc_skip PNGARG((png_structp png_ptr,
 75.3509 +   png_uint_32 length));
 75.3510 +PNG_EXTERN void png_push_crc_finish PNGARG((png_structp png_ptr));
 75.3511 +PNG_EXTERN void png_push_save_buffer PNGARG((png_structp png_ptr));
 75.3512 +PNG_EXTERN void png_push_restore_buffer PNGARG((png_structp png_ptr,
 75.3513 +   png_bytep buffer, png_size_t buffer_length));
 75.3514 +PNG_EXTERN void png_push_read_IDAT PNGARG((png_structp png_ptr));
 75.3515 +PNG_EXTERN void png_process_IDAT_data PNGARG((png_structp png_ptr,
 75.3516 +   png_bytep buffer, png_size_t buffer_length));
 75.3517 +PNG_EXTERN void png_push_process_row PNGARG((png_structp png_ptr));
 75.3518 +PNG_EXTERN void png_push_handle_unknown PNGARG((png_structp png_ptr,
 75.3519 +   png_infop info_ptr, png_uint_32 length));
 75.3520 +PNG_EXTERN void png_push_have_info PNGARG((png_structp png_ptr,
 75.3521 +   png_infop info_ptr));
 75.3522 +PNG_EXTERN void png_push_have_end PNGARG((png_structp png_ptr,
 75.3523 +   png_infop info_ptr));
 75.3524 +PNG_EXTERN void png_push_have_row PNGARG((png_structp png_ptr, png_bytep row));
 75.3525 +PNG_EXTERN void png_push_read_end PNGARG((png_structp png_ptr,
 75.3526 +   png_infop info_ptr));
 75.3527 +PNG_EXTERN void png_process_some_data PNGARG((png_structp png_ptr,
 75.3528 +   png_infop info_ptr));
 75.3529 +PNG_EXTERN void png_read_push_finish_row PNGARG((png_structp png_ptr));
 75.3530 +#if defined(PNG_READ_tEXt_SUPPORTED)
 75.3531 +PNG_EXTERN void png_push_handle_tEXt PNGARG((png_structp png_ptr,
 75.3532 +   png_infop info_ptr, png_uint_32 length));
 75.3533 +PNG_EXTERN void png_push_read_tEXt PNGARG((png_structp png_ptr,
 75.3534 +   png_infop info_ptr));
 75.3535 +#endif
 75.3536 +#if defined(PNG_READ_zTXt_SUPPORTED)
 75.3537 +PNG_EXTERN void png_push_handle_zTXt PNGARG((png_structp png_ptr,
 75.3538 +   png_infop info_ptr, png_uint_32 length));
 75.3539 +PNG_EXTERN void png_push_read_zTXt PNGARG((png_structp png_ptr,
 75.3540 +   png_infop info_ptr));
 75.3541 +#endif
 75.3542 +#if defined(PNG_READ_iTXt_SUPPORTED)
 75.3543 +PNG_EXTERN void png_push_handle_iTXt PNGARG((png_structp png_ptr,
 75.3544 +   png_infop info_ptr, png_uint_32 length));
 75.3545 +PNG_EXTERN void png_push_read_iTXt PNGARG((png_structp png_ptr,
 75.3546 +   png_infop info_ptr));
 75.3547 +#endif
 75.3548 +
 75.3549 +#endif /* PNG_PROGRESSIVE_READ_SUPPORTED */
 75.3550 +
 75.3551 +#ifdef PNG_MNG_FEATURES_SUPPORTED
 75.3552 +PNG_EXTERN void png_do_read_intrapixel PNGARG((png_row_infop row_info,
 75.3553 +   png_bytep row));
 75.3554 +PNG_EXTERN void png_do_write_intrapixel PNGARG((png_row_infop row_info,
 75.3555 +   png_bytep row));
 75.3556 +#endif
 75.3557 +
 75.3558 +#if defined(PNG_ASSEMBLER_CODE_SUPPORTED)
 75.3559 +#if defined(PNG_MMX_CODE_SUPPORTED)
 75.3560 +/* png.c */ /* PRIVATE */
 75.3561 +PNG_EXTERN void png_init_mmx_flags PNGARG((png_structp png_ptr));
 75.3562 +#endif
 75.3563 +#endif
 75.3564 +
 75.3565 +#if defined(PNG_INCH_CONVERSIONS) && defined(PNG_FLOATING_POINT_SUPPORTED)
 75.3566 +PNG_EXTERN png_uint_32 png_get_pixels_per_inch PNGARG((png_structp png_ptr,
 75.3567 +png_infop info_ptr));
 75.3568 +
 75.3569 +PNG_EXTERN png_uint_32 png_get_x_pixels_per_inch PNGARG((png_structp png_ptr,
 75.3570 +png_infop info_ptr));
 75.3571 +
 75.3572 +PNG_EXTERN png_uint_32 png_get_y_pixels_per_inch PNGARG((png_structp png_ptr,
 75.3573 +png_infop info_ptr));
 75.3574 +
 75.3575 +PNG_EXTERN float png_get_x_offset_inches PNGARG((png_structp png_ptr,
 75.3576 +png_infop info_ptr));
 75.3577 +
 75.3578 +PNG_EXTERN float png_get_y_offset_inches PNGARG((png_structp png_ptr,
 75.3579 +png_infop info_ptr));
 75.3580 +
 75.3581 +#if defined(PNG_pHYs_SUPPORTED)
 75.3582 +PNG_EXTERN png_uint_32 png_get_pHYs_dpi PNGARG((png_structp png_ptr,
 75.3583 +png_infop info_ptr, png_uint_32 *res_x, png_uint_32 *res_y, int *unit_type));
 75.3584 +#endif /* PNG_pHYs_SUPPORTED */
 75.3585 +#endif  /* PNG_INCH_CONVERSIONS && PNG_FLOATING_POINT_SUPPORTED */
 75.3586 +
 75.3587 +/* Read the chunk header (length + type name) */
 75.3588 +PNG_EXTERN png_uint_32 png_read_chunk_header PNGARG((png_structp png_ptr));
 75.3589 +
 75.3590 +/* Maintainer: Put new private prototypes here ^ and in libpngpf.3 */
 75.3591 +
 75.3592 +#endif /* PNG_INTERNAL */
 75.3593 +
 75.3594 +#ifdef __cplusplus
 75.3595 +}
 75.3596 +#endif
 75.3597 +
 75.3598 +#endif /* PNG_VERSION_INFO_ONLY */
 75.3599 +/* do not put anything past this line */
 75.3600 +#endif /* PNG_H */
    76.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    76.2 +++ b/libs/libpng/pngconf.h	Sat Sep 19 05:51:51 2015 +0300
    76.3 @@ -0,0 +1,1481 @@
    76.4 +
    76.5 +/* pngconf.h - machine configurable file for libpng
    76.6 + *
    76.7 + * libpng version 1.2.33 - October 31, 2008
    76.8 + * For conditions of distribution and use, see copyright notice in png.h
    76.9 + * Copyright (c) 1998-2008 Glenn Randers-Pehrson
   76.10 + * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
   76.11 + * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
   76.12 + */
   76.13 +
   76.14 +/* Any machine specific code is near the front of this file, so if you
   76.15 + * are configuring libpng for a machine, you may want to read the section
   76.16 + * starting here down to where it starts to typedef png_color, png_text,
   76.17 + * and png_info.
   76.18 + */
   76.19 +
   76.20 +#ifndef PNGCONF_H
   76.21 +#define PNGCONF_H
   76.22 +
   76.23 +#define PNG_1_2_X
   76.24 +
   76.25 +/* 
   76.26 + * PNG_USER_CONFIG has to be defined on the compiler command line. This
   76.27 + * includes the resource compiler for Windows DLL configurations.
   76.28 + */
   76.29 +#ifdef PNG_USER_CONFIG
   76.30 +#  ifndef PNG_USER_PRIVATEBUILD
   76.31 +#    define PNG_USER_PRIVATEBUILD
   76.32 +#  endif
   76.33 +#include "pngusr.h"
   76.34 +#endif
   76.35 +
   76.36 +/* PNG_CONFIGURE_LIBPNG is set by the "configure" script. */
   76.37 +#ifdef PNG_CONFIGURE_LIBPNG
   76.38 +#ifdef HAVE_CONFIG_H
   76.39 +#include "config.h"
   76.40 +#endif
   76.41 +#endif
   76.42 +
   76.43 +/*
   76.44 + * Added at libpng-1.2.8
   76.45 + *  
   76.46 + * If you create a private DLL you need to define in "pngusr.h" the followings:
   76.47 + * #define PNG_USER_PRIVATEBUILD <Describes by whom and why this version of
   76.48 + *        the DLL was built>
   76.49 + *  e.g. #define PNG_USER_PRIVATEBUILD "Build by MyCompany for xyz reasons."
   76.50 + * #define PNG_USER_DLLFNAME_POSTFIX <two-letter postfix that serve to
   76.51 + *        distinguish your DLL from those of the official release. These
   76.52 + *        correspond to the trailing letters that come after the version
   76.53 + *        number and must match your private DLL name>
   76.54 + *  e.g. // private DLL "libpng13gx.dll"
   76.55 + *       #define PNG_USER_DLLFNAME_POSTFIX "gx"
   76.56 + * 
   76.57 + * The following macros are also at your disposal if you want to complete the 
   76.58 + * DLL VERSIONINFO structure.
   76.59 + * - PNG_USER_VERSIONINFO_COMMENTS
   76.60 + * - PNG_USER_VERSIONINFO_COMPANYNAME
   76.61 + * - PNG_USER_VERSIONINFO_LEGALTRADEMARKS
   76.62 + */
   76.63 +
   76.64 +#ifdef __STDC__
   76.65 +#ifdef SPECIALBUILD
   76.66 +#  pragma message("PNG_LIBPNG_SPECIALBUILD (and deprecated SPECIALBUILD)\
   76.67 + are now LIBPNG reserved macros. Use PNG_USER_PRIVATEBUILD instead.")
   76.68 +#endif
   76.69 +
   76.70 +#ifdef PRIVATEBUILD
   76.71 +# pragma message("PRIVATEBUILD is deprecated.\
   76.72 + Use PNG_USER_PRIVATEBUILD instead.")
   76.73 +# define PNG_USER_PRIVATEBUILD PRIVATEBUILD
   76.74 +#endif
   76.75 +#endif /* __STDC__ */
   76.76 +
   76.77 +#ifndef PNG_VERSION_INFO_ONLY
   76.78 +
   76.79 +/* End of material added to libpng-1.2.8 */
   76.80 +
   76.81 +/* Added at libpng-1.2.19, removed at libpng-1.2.20 because it caused trouble
   76.82 +   Restored at libpng-1.2.21 */
   76.83 +#if !defined(PNG_NO_WARN_UNINITIALIZED_ROW) && \
   76.84 +    !defined(PNG_WARN_UNINITIALIZED_ROW)
   76.85 +#  define PNG_WARN_UNINITIALIZED_ROW 1
   76.86 +#endif
   76.87 +/* End of material added at libpng-1.2.19/1.2.21 */
   76.88 +
   76.89 +/* This is the size of the compression buffer, and thus the size of
   76.90 + * an IDAT chunk.  Make this whatever size you feel is best for your
   76.91 + * machine.  One of these will be allocated per png_struct.  When this
   76.92 + * is full, it writes the data to the disk, and does some other
   76.93 + * calculations.  Making this an extremely small size will slow
   76.94 + * the library down, but you may want to experiment to determine
   76.95 + * where it becomes significant, if you are concerned with memory
   76.96 + * usage.  Note that zlib allocates at least 32Kb also.  For readers,
   76.97 + * this describes the size of the buffer available to read the data in.
   76.98 + * Unless this gets smaller than the size of a row (compressed),
   76.99 + * it should not make much difference how big this is.
  76.100 + */
  76.101 +
  76.102 +#ifndef PNG_ZBUF_SIZE
  76.103 +#  define PNG_ZBUF_SIZE 8192
  76.104 +#endif
  76.105 +
  76.106 +/* Enable if you want a write-only libpng */
  76.107 +
  76.108 +#ifndef PNG_NO_READ_SUPPORTED
  76.109 +#  define PNG_READ_SUPPORTED
  76.110 +#endif
  76.111 +
  76.112 +/* Enable if you want a read-only libpng */
  76.113 +
  76.114 +#ifndef PNG_NO_WRITE_SUPPORTED
  76.115 +#  define PNG_WRITE_SUPPORTED
  76.116 +#endif
  76.117 +
  76.118 +/* Enabled by default in 1.2.0.  You can disable this if you don't need to
  76.119 +   support PNGs that are embedded in MNG datastreams */
  76.120 +#if !defined(PNG_1_0_X) && !defined(PNG_NO_MNG_FEATURES)
  76.121 +#  ifndef PNG_MNG_FEATURES_SUPPORTED
  76.122 +#    define PNG_MNG_FEATURES_SUPPORTED
  76.123 +#  endif
  76.124 +#endif
  76.125 +
  76.126 +#ifndef PNG_NO_FLOATING_POINT_SUPPORTED
  76.127 +#  ifndef PNG_FLOATING_POINT_SUPPORTED
  76.128 +#    define PNG_FLOATING_POINT_SUPPORTED
  76.129 +#  endif
  76.130 +#endif
  76.131 +
  76.132 +/* If you are running on a machine where you cannot allocate more
  76.133 + * than 64K of memory at once, uncomment this.  While libpng will not
  76.134 + * normally need that much memory in a chunk (unless you load up a very
  76.135 + * large file), zlib needs to know how big of a chunk it can use, and
  76.136 + * libpng thus makes sure to check any memory allocation to verify it
  76.137 + * will fit into memory.
  76.138 +#define PNG_MAX_MALLOC_64K
  76.139 + */
  76.140 +#if defined(MAXSEG_64K) && !defined(PNG_MAX_MALLOC_64K)
  76.141 +#  define PNG_MAX_MALLOC_64K
  76.142 +#endif
  76.143 +
  76.144 +/* Special munging to support doing things the 'cygwin' way:
  76.145 + * 'Normal' png-on-win32 defines/defaults:
  76.146 + *   PNG_BUILD_DLL -- building dll
  76.147 + *   PNG_USE_DLL   -- building an application, linking to dll
  76.148 + *   (no define)   -- building static library, or building an
  76.149 + *                    application and linking to the static lib
  76.150 + * 'Cygwin' defines/defaults:
  76.151 + *   PNG_BUILD_DLL -- (ignored) building the dll
  76.152 + *   (no define)   -- (ignored) building an application, linking to the dll
  76.153 + *   PNG_STATIC    -- (ignored) building the static lib, or building an 
  76.154 + *                    application that links to the static lib.
  76.155 + *   ALL_STATIC    -- (ignored) building various static libs, or building an 
  76.156 + *                    application that links to the static libs.
  76.157 + * Thus,
  76.158 + * a cygwin user should define either PNG_BUILD_DLL or PNG_STATIC, and
  76.159 + * this bit of #ifdefs will define the 'correct' config variables based on
  76.160 + * that. If a cygwin user *wants* to define 'PNG_USE_DLL' that's okay, but
  76.161 + * unnecessary.
  76.162 + *
  76.163 + * Also, the precedence order is:
  76.164 + *   ALL_STATIC (since we can't #undef something outside our namespace)
  76.165 + *   PNG_BUILD_DLL
  76.166 + *   PNG_STATIC
  76.167 + *   (nothing) == PNG_USE_DLL
  76.168 + * 
  76.169 + * CYGWIN (2002-01-20): The preceding is now obsolete. With the advent
  76.170 + *   of auto-import in binutils, we no longer need to worry about 
  76.171 + *   __declspec(dllexport) / __declspec(dllimport) and friends.  Therefore,
  76.172 + *   we don't need to worry about PNG_STATIC or ALL_STATIC when it comes
  76.173 + *   to __declspec() stuff.  However, we DO need to worry about 
  76.174 + *   PNG_BUILD_DLL and PNG_STATIC because those change some defaults
  76.175 + *   such as CONSOLE_IO and whether GLOBAL_ARRAYS are allowed.
  76.176 + */
  76.177 +#if defined(__CYGWIN__)
  76.178 +#  if defined(ALL_STATIC)
  76.179 +#    if defined(PNG_BUILD_DLL)
  76.180 +#      undef PNG_BUILD_DLL
  76.181 +#    endif
  76.182 +#    if defined(PNG_USE_DLL)
  76.183 +#      undef PNG_USE_DLL
  76.184 +#    endif
  76.185 +#    if defined(PNG_DLL)
  76.186 +#      undef PNG_DLL
  76.187 +#    endif
  76.188 +#    if !defined(PNG_STATIC)
  76.189 +#      define PNG_STATIC
  76.190 +#    endif
  76.191 +#  else
  76.192 +#    if defined (PNG_BUILD_DLL)
  76.193 +#      if defined(PNG_STATIC)
  76.194 +#        undef PNG_STATIC
  76.195 +#      endif
  76.196 +#      if defined(PNG_USE_DLL)
  76.197 +#        undef PNG_USE_DLL
  76.198 +#      endif
  76.199 +#      if !defined(PNG_DLL)
  76.200 +#        define PNG_DLL
  76.201 +#      endif
  76.202 +#    else
  76.203 +#      if defined(PNG_STATIC)
  76.204 +#        if defined(PNG_USE_DLL)
  76.205 +#          undef PNG_USE_DLL
  76.206 +#        endif
  76.207 +#        if defined(PNG_DLL)
  76.208 +#          undef PNG_DLL
  76.209 +#        endif
  76.210 +#      else
  76.211 +#        if !defined(PNG_USE_DLL)
  76.212 +#          define PNG_USE_DLL
  76.213 +#        endif
  76.214 +#        if !defined(PNG_DLL)
  76.215 +#          define PNG_DLL
  76.216 +#        endif
  76.217 +#      endif  
  76.218 +#    endif  
  76.219 +#  endif
  76.220 +#endif
  76.221 +
  76.222 +/* This protects us against compilers that run on a windowing system
  76.223 + * and thus don't have or would rather us not use the stdio types:
  76.224 + * stdin, stdout, and stderr.  The only one currently used is stderr
  76.225 + * in png_error() and png_warning().  #defining PNG_NO_CONSOLE_IO will
  76.226 + * prevent these from being compiled and used. #defining PNG_NO_STDIO
  76.227 + * will also prevent these, plus will prevent the entire set of stdio
  76.228 + * macros and functions (FILE *, printf, etc.) from being compiled and used,
  76.229 + * unless (PNG_DEBUG > 0) has been #defined.
  76.230 + *
  76.231 + * #define PNG_NO_CONSOLE_IO
  76.232 + * #define PNG_NO_STDIO
  76.233 + */
  76.234 +
  76.235 +#if defined(_WIN32_WCE)
  76.236 +#  include <windows.h>
  76.237 +   /* Console I/O functions are not supported on WindowsCE */
  76.238 +#  define PNG_NO_CONSOLE_IO
  76.239 +#  ifdef PNG_DEBUG
  76.240 +#    undef PNG_DEBUG
  76.241 +#  endif
  76.242 +#endif
  76.243 +
  76.244 +#ifdef PNG_BUILD_DLL
  76.245 +#  ifndef PNG_CONSOLE_IO_SUPPORTED
  76.246 +#    ifndef PNG_NO_CONSOLE_IO
  76.247 +#      define PNG_NO_CONSOLE_IO
  76.248 +#    endif
  76.249 +#  endif
  76.250 +#endif
  76.251 +
  76.252 +#  ifdef PNG_NO_STDIO
  76.253 +#    ifndef PNG_NO_CONSOLE_IO
  76.254 +#      define PNG_NO_CONSOLE_IO
  76.255 +#    endif
  76.256 +#    ifdef PNG_DEBUG
  76.257 +#      if (PNG_DEBUG > 0)
  76.258 +#        include <stdio.h>
  76.259 +#      endif
  76.260 +#    endif
  76.261 +#  else
  76.262 +#    if !defined(_WIN32_WCE)
  76.263 +/* "stdio.h" functions are not supported on WindowsCE */
  76.264 +#      include <stdio.h>
  76.265 +#    endif
  76.266 +#  endif
  76.267 +
  76.268 +/* This macro protects us against machines that don't have function
  76.269 + * prototypes (ie K&R style headers).  If your compiler does not handle
  76.270 + * function prototypes, define this macro and use the included ansi2knr.
  76.271 + * I've always been able to use _NO_PROTO as the indicator, but you may
  76.272 + * need to drag the empty declaration out in front of here, or change the
  76.273 + * ifdef to suit your own needs.
  76.274 + */
  76.275 +#ifndef PNGARG
  76.276 +
  76.277 +#ifdef OF /* zlib prototype munger */
  76.278 +#  define PNGARG(arglist) OF(arglist)
  76.279 +#else
  76.280 +
  76.281 +#ifdef _NO_PROTO
  76.282 +#  define PNGARG(arglist) ()
  76.283 +#  ifndef PNG_TYPECAST_NULL
  76.284 +#     define PNG_TYPECAST_NULL
  76.285 +#  endif
  76.286 +#else
  76.287 +#  define PNGARG(arglist) arglist
  76.288 +#endif /* _NO_PROTO */
  76.289 +
  76.290 +
  76.291 +#endif /* OF */
  76.292 +
  76.293 +#endif /* PNGARG */
  76.294 +
  76.295 +/* Try to determine if we are compiling on a Mac.  Note that testing for
  76.296 + * just __MWERKS__ is not good enough, because the Codewarrior is now used
  76.297 + * on non-Mac platforms.
  76.298 + */
  76.299 +#ifndef MACOS
  76.300 +#  if (defined(__MWERKS__) && defined(macintosh)) || defined(applec) || \
  76.301 +      defined(THINK_C) || defined(__SC__) || defined(TARGET_OS_MAC)
  76.302 +#    define MACOS
  76.303 +#  endif
  76.304 +#endif
  76.305 +
  76.306 +/* enough people need this for various reasons to include it here */
  76.307 +#if !defined(MACOS) && !defined(RISCOS) && !defined(_WIN32_WCE)
  76.308 +#  include <sys/types.h>
  76.309 +#endif
  76.310 +
  76.311 +#if !defined(PNG_SETJMP_NOT_SUPPORTED) && !defined(PNG_NO_SETJMP_SUPPORTED)
  76.312 +#  define PNG_SETJMP_SUPPORTED
  76.313 +#endif
  76.314 +
  76.315 +#ifdef PNG_SETJMP_SUPPORTED
  76.316 +/* This is an attempt to force a single setjmp behaviour on Linux.  If
  76.317 + * the X config stuff didn't define _BSD_SOURCE we wouldn't need this.
  76.318 + */
  76.319 +
  76.320 +#  ifdef __linux__
  76.321 +#    ifdef _BSD_SOURCE
  76.322 +#      define PNG_SAVE_BSD_SOURCE
  76.323 +#      undef _BSD_SOURCE
  76.324 +#    endif
  76.325 +#    ifdef _SETJMP_H
  76.326 +     /* If you encounter a compiler error here, see the explanation
  76.327 +      * near the end of INSTALL.
  76.328 +      */
  76.329 +         __pngconf.h__ already includes setjmp.h;
  76.330 +         __dont__ include it again.;
  76.331 +#    endif
  76.332 +#  endif /* __linux__ */
  76.333 +
  76.334 +   /* include setjmp.h for error handling */
  76.335 +#  include <setjmp.h>
  76.336 +
  76.337 +#  ifdef __linux__
  76.338 +#    ifdef PNG_SAVE_BSD_SOURCE
  76.339 +#      ifndef _BSD_SOURCE
  76.340 +#        define _BSD_SOURCE
  76.341 +#      endif
  76.342 +#      undef PNG_SAVE_BSD_SOURCE
  76.343 +#    endif
  76.344 +#  endif /* __linux__ */
  76.345 +#endif /* PNG_SETJMP_SUPPORTED */
  76.346 +
  76.347 +#ifdef BSD
  76.348 +#  include <strings.h>
  76.349 +#else
  76.350 +#  include <string.h>
  76.351 +#endif
  76.352 +
  76.353 +/* Other defines for things like memory and the like can go here.  */
  76.354 +#ifdef PNG_INTERNAL
  76.355 +
  76.356 +#include <stdlib.h>
  76.357 +
  76.358 +/* The functions exported by PNG_EXTERN are PNG_INTERNAL functions, which
  76.359 + * aren't usually used outside the library (as far as I know), so it is
  76.360 + * debatable if they should be exported at all.  In the future, when it is
  76.361 + * possible to have run-time registry of chunk-handling functions, some of
  76.362 + * these will be made available again.
  76.363 +#define PNG_EXTERN extern
  76.364 + */
  76.365 +#define PNG_EXTERN
  76.366 +
  76.367 +/* Other defines specific to compilers can go here.  Try to keep
  76.368 + * them inside an appropriate ifdef/endif pair for portability.
  76.369 + */
  76.370 +
  76.371 +#if defined(PNG_FLOATING_POINT_SUPPORTED)
  76.372 +#  if defined(MACOS)
  76.373 +     /* We need to check that <math.h> hasn't already been included earlier
  76.374 +      * as it seems it doesn't agree with <fp.h>, yet we should really use
  76.375 +      * <fp.h> if possible.
  76.376 +      */
  76.377 +#    if !defined(__MATH_H__) && !defined(__MATH_H) && !defined(__cmath__)
  76.378 +#      include <fp.h>
  76.379 +#    endif
  76.380 +#  else
  76.381 +#    include <math.h>
  76.382 +#  endif
  76.383 +#  if defined(_AMIGA) && defined(__SASC) && defined(_M68881)
  76.384 +     /* Amiga SAS/C: We must include builtin FPU functions when compiling using
  76.385 +      * MATH=68881
  76.386 +      */
  76.387 +#    include <m68881.h>
  76.388 +#  endif
  76.389 +#endif
  76.390 +
  76.391 +/* Codewarrior on NT has linking problems without this. */
  76.392 +#if (defined(__MWERKS__) && defined(WIN32)) || defined(__STDC__)
  76.393 +#  define PNG_ALWAYS_EXTERN
  76.394 +#endif
  76.395 +
  76.396 +/* This provides the non-ANSI (far) memory allocation routines. */
  76.397 +#if defined(__TURBOC__) && defined(__MSDOS__)
  76.398 +#  include <mem.h>
  76.399 +#  include <alloc.h>
  76.400 +#endif
  76.401 +
  76.402 +/* I have no idea why is this necessary... */
  76.403 +#if defined(_MSC_VER) && (defined(WIN32) || defined(_Windows) || \
  76.404 +    defined(_WINDOWS) || defined(_WIN32) || defined(__WIN32__))
  76.405 +#  include <malloc.h>
  76.406 +#endif
  76.407 +
  76.408 +/* This controls how fine the dithering gets.  As this allocates
  76.409 + * a largish chunk of memory (32K), those who are not as concerned
  76.410 + * with dithering quality can decrease some or all of these.
  76.411 + */
  76.412 +#ifndef PNG_DITHER_RED_BITS
  76.413 +#  define PNG_DITHER_RED_BITS 5
  76.414 +#endif
  76.415 +#ifndef PNG_DITHER_GREEN_BITS
  76.416 +#  define PNG_DITHER_GREEN_BITS 5
  76.417 +#endif
  76.418 +#ifndef PNG_DITHER_BLUE_BITS
  76.419 +#  define PNG_DITHER_BLUE_BITS 5
  76.420 +#endif
  76.421 +
  76.422 +/* This controls how fine the gamma correction becomes when you
  76.423 + * are only interested in 8 bits anyway.  Increasing this value
  76.424 + * results in more memory being used, and more pow() functions
  76.425 + * being called to fill in the gamma tables.  Don't set this value
  76.426 + * less then 8, and even that may not work (I haven't tested it).
  76.427 + */
  76.428 +
  76.429 +#ifndef PNG_MAX_GAMMA_8
  76.430 +#  define PNG_MAX_GAMMA_8 11
  76.431 +#endif
  76.432 +
  76.433 +/* This controls how much a difference in gamma we can tolerate before
  76.434 + * we actually start doing gamma conversion.
  76.435 + */
  76.436 +#ifndef PNG_GAMMA_THRESHOLD
  76.437 +#  define PNG_GAMMA_THRESHOLD 0.05
  76.438 +#endif
  76.439 +
  76.440 +#endif /* PNG_INTERNAL */
  76.441 +
  76.442 +/* The following uses const char * instead of char * for error
  76.443 + * and warning message functions, so some compilers won't complain.
  76.444 + * If you do not want to use const, define PNG_NO_CONST here.
  76.445 + */
  76.446 +
  76.447 +#ifndef PNG_NO_CONST
  76.448 +#  define PNG_CONST const
  76.449 +#else
  76.450 +#  define PNG_CONST
  76.451 +#endif
  76.452 +
  76.453 +/* The following defines give you the ability to remove code from the
  76.454 + * library that you will not be using.  I wish I could figure out how to
  76.455 + * automate this, but I can't do that without making it seriously hard
  76.456 + * on the users.  So if you are not using an ability, change the #define
  76.457 + * to and #undef, and that part of the library will not be compiled.  If
  76.458 + * your linker can't find a function, you may want to make sure the
  76.459 + * ability is defined here.  Some of these depend upon some others being
  76.460 + * defined.  I haven't figured out all the interactions here, so you may
  76.461 + * have to experiment awhile to get everything to compile.  If you are
  76.462 + * creating or using a shared library, you probably shouldn't touch this,
  76.463 + * as it will affect the size of the structures, and this will cause bad
  76.464 + * things to happen if the library and/or application ever change.
  76.465 + */
  76.466 +
  76.467 +/* Any features you will not be using can be undef'ed here */
  76.468 +
  76.469 +/* GR-P, 0.96a: Set "*TRANSFORMS_SUPPORTED as default but allow user
  76.470 + * to turn it off with "*TRANSFORMS_NOT_SUPPORTED" or *PNG_NO_*_TRANSFORMS
  76.471 + * on the compile line, then pick and choose which ones to define without
  76.472 + * having to edit this file. It is safe to use the *TRANSFORMS_NOT_SUPPORTED
  76.473 + * if you only want to have a png-compliant reader/writer but don't need
  76.474 + * any of the extra transformations.  This saves about 80 kbytes in a
  76.475 + * typical installation of the library. (PNG_NO_* form added in version
  76.476 + * 1.0.1c, for consistency)
  76.477 + */
  76.478 +
  76.479 +/* The size of the png_text structure changed in libpng-1.0.6 when
  76.480 + * iTXt support was added.  iTXt support was turned off by default through
  76.481 + * libpng-1.2.x, to support old apps that malloc the png_text structure
  76.482 + * instead of calling png_set_text() and letting libpng malloc it.  It
  76.483 + * was turned on by default in libpng-1.3.0.
  76.484 + */
  76.485 +
  76.486 +#if defined(PNG_1_0_X) || defined (PNG_1_2_X)
  76.487 +#  ifndef PNG_NO_iTXt_SUPPORTED
  76.488 +#    define PNG_NO_iTXt_SUPPORTED
  76.489 +#  endif
  76.490 +#  ifndef PNG_NO_READ_iTXt
  76.491 +#    define PNG_NO_READ_iTXt
  76.492 +#  endif
  76.493 +#  ifndef PNG_NO_WRITE_iTXt
  76.494 +#    define PNG_NO_WRITE_iTXt
  76.495 +#  endif
  76.496 +#endif
  76.497 +
  76.498 +#if !defined(PNG_NO_iTXt_SUPPORTED)
  76.499 +#  if !defined(PNG_READ_iTXt_SUPPORTED) && !defined(PNG_NO_READ_iTXt)
  76.500 +#    define PNG_READ_iTXt
  76.501 +#  endif
  76.502 +#  if !defined(PNG_WRITE_iTXt_SUPPORTED) && !defined(PNG_NO_WRITE_iTXt)
  76.503 +#    define PNG_WRITE_iTXt
  76.504 +#  endif
  76.505 +#endif
  76.506 +
  76.507 +/* The following support, added after version 1.0.0, can be turned off here en
  76.508 + * masse by defining PNG_LEGACY_SUPPORTED in case you need binary compatibility
  76.509 + * with old applications that require the length of png_struct and png_info
  76.510 + * to remain unchanged.
  76.511 + */
  76.512 +
  76.513 +#ifdef PNG_LEGACY_SUPPORTED
  76.514 +#  define PNG_NO_FREE_ME
  76.515 +#  define PNG_NO_READ_UNKNOWN_CHUNKS
  76.516 +#  define PNG_NO_WRITE_UNKNOWN_CHUNKS
  76.517 +#  define PNG_NO_READ_USER_CHUNKS
  76.518 +#  define PNG_NO_READ_iCCP
  76.519 +#  define PNG_NO_WRITE_iCCP
  76.520 +#  define PNG_NO_READ_iTXt
  76.521 +#  define PNG_NO_WRITE_iTXt
  76.522 +#  define PNG_NO_READ_sCAL
  76.523 +#  define PNG_NO_WRITE_sCAL
  76.524 +#  define PNG_NO_READ_sPLT
  76.525 +#  define PNG_NO_WRITE_sPLT
  76.526 +#  define PNG_NO_INFO_IMAGE
  76.527 +#  define PNG_NO_READ_RGB_TO_GRAY
  76.528 +#  define PNG_NO_READ_USER_TRANSFORM
  76.529 +#  define PNG_NO_WRITE_USER_TRANSFORM
  76.530 +#  define PNG_NO_USER_MEM
  76.531 +#  define PNG_NO_READ_EMPTY_PLTE
  76.532 +#  define PNG_NO_MNG_FEATURES
  76.533 +#  define PNG_NO_FIXED_POINT_SUPPORTED
  76.534 +#endif
  76.535 +
  76.536 +/* Ignore attempt to turn off both floating and fixed point support */
  76.537 +#if !defined(PNG_FLOATING_POINT_SUPPORTED) || \
  76.538 +    !defined(PNG_NO_FIXED_POINT_SUPPORTED)
  76.539 +#  define PNG_FIXED_POINT_SUPPORTED
  76.540 +#endif
  76.541 +
  76.542 +#ifndef PNG_NO_FREE_ME
  76.543 +#  define PNG_FREE_ME_SUPPORTED
  76.544 +#endif
  76.545 +
  76.546 +#if defined(PNG_READ_SUPPORTED)
  76.547 +
  76.548 +#if !defined(PNG_READ_TRANSFORMS_NOT_SUPPORTED) && \
  76.549 +      !defined(PNG_NO_READ_TRANSFORMS)
  76.550 +#  define PNG_READ_TRANSFORMS_SUPPORTED
  76.551 +#endif
  76.552 +
  76.553 +#ifdef PNG_READ_TRANSFORMS_SUPPORTED
  76.554 +#  ifndef PNG_NO_READ_EXPAND
  76.555 +#    define PNG_READ_EXPAND_SUPPORTED
  76.556 +#  endif
  76.557 +#  ifndef PNG_NO_READ_SHIFT
  76.558 +#    define PNG_READ_SHIFT_SUPPORTED
  76.559 +#  endif
  76.560 +#  ifndef PNG_NO_READ_PACK
  76.561 +#    define PNG_READ_PACK_SUPPORTED
  76.562 +#  endif
  76.563 +#  ifndef PNG_NO_READ_BGR
  76.564 +#    define PNG_READ_BGR_SUPPORTED
  76.565 +#  endif
  76.566 +#  ifndef PNG_NO_READ_SWAP
  76.567 +#    define PNG_READ_SWAP_SUPPORTED
  76.568 +#  endif
  76.569 +#  ifndef PNG_NO_READ_PACKSWAP
  76.570 +#    define PNG_READ_PACKSWAP_SUPPORTED
  76.571 +#  endif
  76.572 +#  ifndef PNG_NO_READ_INVERT
  76.573 +#    define PNG_READ_INVERT_SUPPORTED
  76.574 +#  endif
  76.575 +#  ifndef PNG_NO_READ_DITHER
  76.576 +#    define PNG_READ_DITHER_SUPPORTED
  76.577 +#  endif
  76.578 +#  ifndef PNG_NO_READ_BACKGROUND
  76.579 +#    define PNG_READ_BACKGROUND_SUPPORTED
  76.580 +#  endif
  76.581 +#  ifndef PNG_NO_READ_16_TO_8
  76.582 +#    define PNG_READ_16_TO_8_SUPPORTED
  76.583 +#  endif
  76.584 +#  ifndef PNG_NO_READ_FILLER
  76.585 +#    define PNG_READ_FILLER_SUPPORTED
  76.586 +#  endif
  76.587 +#  ifndef PNG_NO_READ_GAMMA
  76.588 +#    define PNG_READ_GAMMA_SUPPORTED
  76.589 +#  endif
  76.590 +#  ifndef PNG_NO_READ_GRAY_TO_RGB
  76.591 +#    define PNG_READ_GRAY_TO_RGB_SUPPORTED
  76.592 +#  endif
  76.593 +#  ifndef PNG_NO_READ_SWAP_ALPHA
  76.594 +#    define PNG_READ_SWAP_ALPHA_SUPPORTED
  76.595 +#  endif
  76.596 +#  ifndef PNG_NO_READ_INVERT_ALPHA
  76.597 +#    define PNG_READ_INVERT_ALPHA_SUPPORTED
  76.598 +#  endif
  76.599 +#  ifndef PNG_NO_READ_STRIP_ALPHA
  76.600 +#    define PNG_READ_STRIP_ALPHA_SUPPORTED
  76.601 +#  endif
  76.602 +#  ifndef PNG_NO_READ_USER_TRANSFORM
  76.603 +#    define PNG_READ_USER_TRANSFORM_SUPPORTED
  76.604 +#  endif
  76.605 +#  ifndef PNG_NO_READ_RGB_TO_GRAY
  76.606 +#    define PNG_READ_RGB_TO_GRAY_SUPPORTED
  76.607 +#  endif
  76.608 +#endif /* PNG_READ_TRANSFORMS_SUPPORTED */
  76.609 +
  76.610 +#if !defined(PNG_NO_PROGRESSIVE_READ) && \
  76.611 + !defined(PNG_PROGRESSIVE_READ_SUPPORTED) /* if you don't do progressive   */
  76.612 +#  define PNG_PROGRESSIVE_READ_SUPPORTED  /* reading.  This is not talking */
  76.613 +#endif                            /* about interlacing capability!  You'll */
  76.614 +           /* still have interlacing unless you change the following line: */
  76.615 +
  76.616 +#define PNG_READ_INTERLACING_SUPPORTED /* required in PNG-compliant decoders */
  76.617 +
  76.618 +#ifndef PNG_NO_READ_COMPOSITE_NODIV
  76.619 +#  ifndef PNG_NO_READ_COMPOSITED_NODIV  /* libpng-1.0.x misspelling */
  76.620 +#    define PNG_READ_COMPOSITE_NODIV_SUPPORTED  /* well tested on Intel, SGI */
  76.621 +#  endif
  76.622 +#endif
  76.623 +
  76.624 +#if defined(PNG_1_0_X) || defined (PNG_1_2_X)
  76.625 +/* Deprecated, will be removed from version 2.0.0.
  76.626 +   Use PNG_MNG_FEATURES_SUPPORTED instead. */
  76.627 +#ifndef PNG_NO_READ_EMPTY_PLTE
  76.628 +#  define PNG_READ_EMPTY_PLTE_SUPPORTED
  76.629 +#endif
  76.630 +#endif
  76.631 +
  76.632 +#endif /* PNG_READ_SUPPORTED */
  76.633 +
  76.634 +#if defined(PNG_WRITE_SUPPORTED)
  76.635 +
  76.636 +# if !defined(PNG_WRITE_TRANSFORMS_NOT_SUPPORTED) && \
  76.637 +    !defined(PNG_NO_WRITE_TRANSFORMS)
  76.638 +#  define PNG_WRITE_TRANSFORMS_SUPPORTED
  76.639 +#endif
  76.640 +
  76.641 +#ifdef PNG_WRITE_TRANSFORMS_SUPPORTED
  76.642 +#  ifndef PNG_NO_WRITE_SHIFT
  76.643 +#    define PNG_WRITE_SHIFT_SUPPORTED
  76.644 +#  endif
  76.645 +#  ifndef PNG_NO_WRITE_PACK
  76.646 +#    define PNG_WRITE_PACK_SUPPORTED
  76.647 +#  endif
  76.648 +#  ifndef PNG_NO_WRITE_BGR
  76.649 +#    define PNG_WRITE_BGR_SUPPORTED
  76.650 +#  endif
  76.651 +#  ifndef PNG_NO_WRITE_SWAP
  76.652 +#    define PNG_WRITE_SWAP_SUPPORTED
  76.653 +#  endif
  76.654 +#  ifndef PNG_NO_WRITE_PACKSWAP
  76.655 +#    define PNG_WRITE_PACKSWAP_SUPPORTED
  76.656 +#  endif
  76.657 +#  ifndef PNG_NO_WRITE_INVERT
  76.658 +#    define PNG_WRITE_INVERT_SUPPORTED
  76.659 +#  endif
  76.660 +#  ifndef PNG_NO_WRITE_FILLER
  76.661 +#    define PNG_WRITE_FILLER_SUPPORTED   /* same as WRITE_STRIP_ALPHA */
  76.662 +#  endif
  76.663 +#  ifndef PNG_NO_WRITE_SWAP_ALPHA
  76.664 +#    define PNG_WRITE_SWAP_ALPHA_SUPPORTED
  76.665 +#  endif
  76.666 +#  ifndef PNG_NO_WRITE_INVERT_ALPHA
  76.667 +#    define PNG_WRITE_INVERT_ALPHA_SUPPORTED
  76.668 +#  endif
  76.669 +#  ifndef PNG_NO_WRITE_USER_TRANSFORM
  76.670 +#    define PNG_WRITE_USER_TRANSFORM_SUPPORTED
  76.671 +#  endif
  76.672 +#endif /* PNG_WRITE_TRANSFORMS_SUPPORTED */
  76.673 +
  76.674 +#if !defined(PNG_NO_WRITE_INTERLACING_SUPPORTED) && \
  76.675 +    !defined(PNG_WRITE_INTERLACING_SUPPORTED)
  76.676 +#define PNG_WRITE_INTERLACING_SUPPORTED  /* not required for PNG-compliant
  76.677 +                                            encoders, but can cause trouble
  76.678 +                                            if left undefined */
  76.679 +#endif
  76.680 +
  76.681 +#if !defined(PNG_NO_WRITE_WEIGHTED_FILTER) && \
  76.682 +    !defined(PNG_WRITE_WEIGHTED_FILTER) && \
  76.683 +     defined(PNG_FLOATING_POINT_SUPPORTED)
  76.684 +#  define PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
  76.685 +#endif
  76.686 +
  76.687 +#ifndef PNG_NO_WRITE_FLUSH
  76.688 +#  define PNG_WRITE_FLUSH_SUPPORTED
  76.689 +#endif
  76.690 +
  76.691 +#if defined(PNG_1_0_X) || defined (PNG_1_2_X)
  76.692 +/* Deprecated, see PNG_MNG_FEATURES_SUPPORTED, above */
  76.693 +#ifndef PNG_NO_WRITE_EMPTY_PLTE
  76.694 +#  define PNG_WRITE_EMPTY_PLTE_SUPPORTED
  76.695 +#endif
  76.696 +#endif
  76.697 +
  76.698 +#endif /* PNG_WRITE_SUPPORTED */
  76.699 +
  76.700 +#ifndef PNG_1_0_X
  76.701 +#  ifndef PNG_NO_ERROR_NUMBERS
  76.702 +#    define PNG_ERROR_NUMBERS_SUPPORTED
  76.703 +#  endif
  76.704 +#endif /* PNG_1_0_X */
  76.705 +
  76.706 +#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \
  76.707 +    defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
  76.708 +#  ifndef PNG_NO_USER_TRANSFORM_PTR
  76.709 +#    define PNG_USER_TRANSFORM_PTR_SUPPORTED
  76.710 +#  endif
  76.711 +#endif
  76.712 +
  76.713 +#ifndef PNG_NO_STDIO
  76.714 +#  define PNG_TIME_RFC1123_SUPPORTED
  76.715 +#endif
  76.716 +
  76.717 +/* This adds extra functions in pngget.c for accessing data from the
  76.718 + * info pointer (added in version 0.99)
  76.719 + * png_get_image_width()
  76.720 + * png_get_image_height()
  76.721 + * png_get_bit_depth()
  76.722 + * png_get_color_type()
  76.723 + * png_get_compression_type()
  76.724 + * png_get_filter_type()
  76.725 + * png_get_interlace_type()
  76.726 + * png_get_pixel_aspect_ratio()
  76.727 + * png_get_pixels_per_meter()
  76.728 + * png_get_x_offset_pixels()
  76.729 + * png_get_y_offset_pixels()
  76.730 + * png_get_x_offset_microns()
  76.731 + * png_get_y_offset_microns()
  76.732 + */
  76.733 +#if !defined(PNG_NO_EASY_ACCESS) && !defined(PNG_EASY_ACCESS_SUPPORTED)
  76.734 +#  define PNG_EASY_ACCESS_SUPPORTED
  76.735 +#endif
  76.736 +
  76.737 +/* PNG_ASSEMBLER_CODE was enabled by default in version 1.2.0 
  76.738 + * and removed from version 1.2.20.  The following will be removed
  76.739 + * from libpng-1.4.0
  76.740 +*/
  76.741 +
  76.742 +#if defined(PNG_READ_SUPPORTED) && !defined(PNG_NO_OPTIMIZED_CODE)
  76.743 +#  ifndef PNG_OPTIMIZED_CODE_SUPPORTED
  76.744 +#    define PNG_OPTIMIZED_CODE_SUPPORTED
  76.745 +#  endif
  76.746 +#endif
  76.747 +
  76.748 +#if defined(PNG_READ_SUPPORTED) && !defined(PNG_NO_ASSEMBLER_CODE)
  76.749 +#  ifndef PNG_ASSEMBLER_CODE_SUPPORTED
  76.750 +#    define PNG_ASSEMBLER_CODE_SUPPORTED
  76.751 +#  endif
  76.752 +
  76.753 +#  if defined(__GNUC__) && defined(__x86_64__) && (__GNUC__ < 4)
  76.754 +     /* work around 64-bit gcc compiler bugs in gcc-3.x */
  76.755 +#    if !defined(PNG_MMX_CODE_SUPPORTED) && !defined(PNG_NO_MMX_CODE)
  76.756 +#      define PNG_NO_MMX_CODE
  76.757 +#    endif
  76.758 +#  endif
  76.759 +
  76.760 +#  if defined(__APPLE__)
  76.761 +#    if !defined(PNG_MMX_CODE_SUPPORTED) && !defined(PNG_NO_MMX_CODE)
  76.762 +#      define PNG_NO_MMX_CODE
  76.763 +#    endif
  76.764 +#  endif
  76.765 +
  76.766 +#  if (defined(__MWERKS__) && ((__MWERKS__ < 0x0900) || macintosh))
  76.767 +#    if !defined(PNG_MMX_CODE_SUPPORTED) && !defined(PNG_NO_MMX_CODE)
  76.768 +#      define PNG_NO_MMX_CODE
  76.769 +#    endif
  76.770 +#  endif
  76.771 +
  76.772 +#  if !defined(PNG_MMX_CODE_SUPPORTED) && !defined(PNG_NO_MMX_CODE)
  76.773 +#    define PNG_MMX_CODE_SUPPORTED
  76.774 +#  endif
  76.775 +
  76.776 +#endif
  76.777 +/* end of obsolete code to be removed from libpng-1.4.0 */
  76.778 +
  76.779 +#if !defined(PNG_1_0_X)
  76.780 +#if !defined(PNG_NO_USER_MEM) && !defined(PNG_USER_MEM_SUPPORTED)
  76.781 +#  define PNG_USER_MEM_SUPPORTED
  76.782 +#endif
  76.783 +#endif /* PNG_1_0_X */
  76.784 +
  76.785 +/* Added at libpng-1.2.6 */
  76.786 +#if !defined(PNG_1_0_X)
  76.787 +#ifndef PNG_SET_USER_LIMITS_SUPPORTED
  76.788 +#if !defined(PNG_NO_SET_USER_LIMITS) && !defined(PNG_SET_USER_LIMITS_SUPPORTED)
  76.789 +#  define PNG_SET_USER_LIMITS_SUPPORTED
  76.790 +#endif
  76.791 +#endif
  76.792 +#endif /* PNG_1_0_X */
  76.793 +
  76.794 +/* Added at libpng-1.0.16 and 1.2.6.  To accept all valid PNGS no matter
  76.795 + * how large, set these limits to 0x7fffffffL
  76.796 + */
  76.797 +#ifndef PNG_USER_WIDTH_MAX
  76.798 +#  define PNG_USER_WIDTH_MAX 1000000L
  76.799 +#endif
  76.800 +#ifndef PNG_USER_HEIGHT_MAX
  76.801 +#  define PNG_USER_HEIGHT_MAX 1000000L
  76.802 +#endif
  76.803 +
  76.804 +/* These are currently experimental features, define them if you want */
  76.805 +
  76.806 +/* very little testing */
  76.807 +/*
  76.808 +#ifdef PNG_READ_SUPPORTED
  76.809 +#  ifndef PNG_READ_16_TO_8_ACCURATE_SCALE_SUPPORTED
  76.810 +#    define PNG_READ_16_TO_8_ACCURATE_SCALE_SUPPORTED
  76.811 +#  endif
  76.812 +#endif
  76.813 +*/
  76.814 +
  76.815 +/* This is only for PowerPC big-endian and 680x0 systems */
  76.816 +/* some testing */
  76.817 +/*
  76.818 +#ifndef PNG_READ_BIG_ENDIAN_SUPPORTED
  76.819 +#  define PNG_READ_BIG_ENDIAN_SUPPORTED
  76.820 +#endif
  76.821 +*/
  76.822 +
  76.823 +/* Buggy compilers (e.g., gcc 2.7.2.2) need this */
  76.824 +/*
  76.825 +#define PNG_NO_POINTER_INDEXING
  76.826 +*/
  76.827 +
  76.828 +/* These functions are turned off by default, as they will be phased out. */
  76.829 +/*
  76.830 +#define  PNG_USELESS_TESTS_SUPPORTED
  76.831 +#define  PNG_CORRECT_PALETTE_SUPPORTED
  76.832 +*/
  76.833 +
  76.834 +/* Any chunks you are not interested in, you can undef here.  The
  76.835 + * ones that allocate memory may be expecially important (hIST,
  76.836 + * tEXt, zTXt, tRNS, pCAL).  Others will just save time and make png_info
  76.837 + * a bit smaller.
  76.838 + */
  76.839 +
  76.840 +#if defined(PNG_READ_SUPPORTED) && \
  76.841 +    !defined(PNG_READ_ANCILLARY_CHUNKS_NOT_SUPPORTED) && \
  76.842 +    !defined(PNG_NO_READ_ANCILLARY_CHUNKS)
  76.843 +#  define PNG_READ_ANCILLARY_CHUNKS_SUPPORTED
  76.844 +#endif
  76.845 +
  76.846 +#if defined(PNG_WRITE_SUPPORTED) && \
  76.847 +    !defined(PNG_WRITE_ANCILLARY_CHUNKS_NOT_SUPPORTED) && \
  76.848 +    !defined(PNG_NO_WRITE_ANCILLARY_CHUNKS)
  76.849 +#  define PNG_WRITE_ANCILLARY_CHUNKS_SUPPORTED
  76.850 +#endif
  76.851 +
  76.852 +#ifdef PNG_READ_ANCILLARY_CHUNKS_SUPPORTED
  76.853 +
  76.854 +#ifdef PNG_NO_READ_TEXT
  76.855 +#  define PNG_NO_READ_iTXt
  76.856 +#  define PNG_NO_READ_tEXt
  76.857 +#  define PNG_NO_READ_zTXt
  76.858 +#endif
  76.859 +#ifndef PNG_NO_READ_bKGD
  76.860 +#  define PNG_READ_bKGD_SUPPORTED
  76.861 +#  define PNG_bKGD_SUPPORTED
  76.862 +#endif
  76.863 +#ifndef PNG_NO_READ_cHRM
  76.864 +#  define PNG_READ_cHRM_SUPPORTED
  76.865 +#  define PNG_cHRM_SUPPORTED
  76.866 +#endif
  76.867 +#ifndef PNG_NO_READ_gAMA
  76.868 +#  define PNG_READ_gAMA_SUPPORTED
  76.869 +#  define PNG_gAMA_SUPPORTED
  76.870 +#endif
  76.871 +#ifndef PNG_NO_READ_hIST
  76.872 +#  define PNG_READ_hIST_SUPPORTED
  76.873 +#  define PNG_hIST_SUPPORTED
  76.874 +#endif
  76.875 +#ifndef PNG_NO_READ_iCCP
  76.876 +#  define PNG_READ_iCCP_SUPPORTED
  76.877 +#  define PNG_iCCP_SUPPORTED
  76.878 +#endif
  76.879 +#ifndef PNG_NO_READ_iTXt
  76.880 +#  ifndef PNG_READ_iTXt_SUPPORTED
  76.881 +#    define PNG_READ_iTXt_SUPPORTED
  76.882 +#  endif
  76.883 +#  ifndef PNG_iTXt_SUPPORTED
  76.884 +#    define PNG_iTXt_SUPPORTED
  76.885 +#  endif
  76.886 +#endif
  76.887 +#ifndef PNG_NO_READ_oFFs
  76.888 +#  define PNG_READ_oFFs_SUPPORTED
  76.889 +#  define PNG_oFFs_SUPPORTED
  76.890 +#endif
  76.891 +#ifndef PNG_NO_READ_pCAL
  76.892 +#  define PNG_READ_pCAL_SUPPORTED
  76.893 +#  define PNG_pCAL_SUPPORTED
  76.894 +#endif
  76.895 +#ifndef PNG_NO_READ_sCAL
  76.896 +#  define PNG_READ_sCAL_SUPPORTED
  76.897 +#  define PNG_sCAL_SUPPORTED
  76.898 +#endif
  76.899 +#ifndef PNG_NO_READ_pHYs
  76.900 +#  define PNG_READ_pHYs_SUPPORTED
  76.901 +#  define PNG_pHYs_SUPPORTED
  76.902 +#endif
  76.903 +#ifndef PNG_NO_READ_sBIT
  76.904 +#  define PNG_READ_sBIT_SUPPORTED
  76.905 +#  define PNG_sBIT_SUPPORTED
  76.906 +#endif
  76.907 +#ifndef PNG_NO_READ_sPLT
  76.908 +#  define PNG_READ_sPLT_SUPPORTED
  76.909 +#  define PNG_sPLT_SUPPORTED
  76.910 +#endif
  76.911 +#ifndef PNG_NO_READ_sRGB
  76.912 +#  define PNG_READ_sRGB_SUPPORTED
  76.913 +#  define PNG_sRGB_SUPPORTED
  76.914 +#endif
  76.915 +#ifndef PNG_NO_READ_tEXt
  76.916 +#  define PNG_READ_tEXt_SUPPORTED
  76.917 +#  define PNG_tEXt_SUPPORTED
  76.918 +#endif
  76.919 +#ifndef PNG_NO_READ_tIME
  76.920 +#  define PNG_READ_tIME_SUPPORTED
  76.921 +#  define PNG_tIME_SUPPORTED
  76.922 +#endif
  76.923 +#ifndef PNG_NO_READ_tRNS
  76.924 +#  define PNG_READ_tRNS_SUPPORTED
  76.925 +#  define PNG_tRNS_SUPPORTED
  76.926 +#endif
  76.927 +#ifndef PNG_NO_READ_zTXt
  76.928 +#  define PNG_READ_zTXt_SUPPORTED
  76.929 +#  define PNG_zTXt_SUPPORTED
  76.930 +#endif
  76.931 +#ifndef PNG_NO_READ_UNKNOWN_CHUNKS
  76.932 +#  define PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
  76.933 +#  ifndef PNG_UNKNOWN_CHUNKS_SUPPORTED
  76.934 +#    define PNG_UNKNOWN_CHUNKS_SUPPORTED
  76.935 +#  endif
  76.936 +#  ifndef PNG_NO_HANDLE_AS_UNKNOWN
  76.937 +#    define PNG_HANDLE_AS_UNKNOWN_SUPPORTED
  76.938 +#  endif
  76.939 +#endif
  76.940 +#if !defined(PNG_NO_READ_USER_CHUNKS) && \
  76.941 +     defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
  76.942 +#  define PNG_READ_USER_CHUNKS_SUPPORTED
  76.943 +#  define PNG_USER_CHUNKS_SUPPORTED
  76.944 +#  ifdef PNG_NO_READ_UNKNOWN_CHUNKS
  76.945 +#    undef PNG_NO_READ_UNKNOWN_CHUNKS
  76.946 +#  endif
  76.947 +#  ifdef PNG_NO_HANDLE_AS_UNKNOWN
  76.948 +#    undef PNG_NO_HANDLE_AS_UNKNOWN
  76.949 +#  endif
  76.950 +#endif
  76.951 +#ifndef PNG_NO_READ_OPT_PLTE
  76.952 +#  define PNG_READ_OPT_PLTE_SUPPORTED /* only affects support of the */
  76.953 +#endif                      /* optional PLTE chunk in RGB and RGBA images */
  76.954 +#if defined(PNG_READ_iTXt_SUPPORTED) || defined(PNG_READ_tEXt_SUPPORTED) || \
  76.955 +    defined(PNG_READ_zTXt_SUPPORTED)
  76.956 +#  define PNG_READ_TEXT_SUPPORTED
  76.957 +#  define PNG_TEXT_SUPPORTED
  76.958 +#endif
  76.959 +
  76.960 +#endif /* PNG_READ_ANCILLARY_CHUNKS_SUPPORTED */
  76.961 +
  76.962 +#ifdef PNG_WRITE_ANCILLARY_CHUNKS_SUPPORTED
  76.963 +
  76.964 +#ifdef PNG_NO_WRITE_TEXT
  76.965 +#  define PNG_NO_WRITE_iTXt
  76.966 +#  define PNG_NO_WRITE_tEXt
  76.967 +#  define PNG_NO_WRITE_zTXt
  76.968 +#endif
  76.969 +#ifndef PNG_NO_WRITE_bKGD
  76.970 +#  define PNG_WRITE_bKGD_SUPPORTED
  76.971 +#  ifndef PNG_bKGD_SUPPORTED
  76.972 +#    define PNG_bKGD_SUPPORTED
  76.973 +#  endif
  76.974 +#endif
  76.975 +#ifndef PNG_NO_WRITE_cHRM
  76.976 +#  define PNG_WRITE_cHRM_SUPPORTED
  76.977 +#  ifndef PNG_cHRM_SUPPORTED
  76.978 +#    define PNG_cHRM_SUPPORTED
  76.979 +#  endif
  76.980 +#endif
  76.981 +#ifndef PNG_NO_WRITE_gAMA
  76.982 +#  define PNG_WRITE_gAMA_SUPPORTED
  76.983 +#  ifndef PNG_gAMA_SUPPORTED
  76.984 +#    define PNG_gAMA_SUPPORTED
  76.985 +#  endif
  76.986 +#endif
  76.987 +#ifndef PNG_NO_WRITE_hIST
  76.988 +#  define PNG_WRITE_hIST_SUPPORTED
  76.989 +#  ifndef PNG_hIST_SUPPORTED
  76.990 +#    define PNG_hIST_SUPPORTED
  76.991 +#  endif
  76.992 +#endif
  76.993 +#ifndef PNG_NO_WRITE_iCCP
  76.994 +#  define PNG_WRITE_iCCP_SUPPORTED
  76.995 +#  ifndef PNG_iCCP_SUPPORTED
  76.996 +#    define PNG_iCCP_SUPPORTED
  76.997 +#  endif
  76.998 +#endif
  76.999 +#ifndef PNG_NO_WRITE_iTXt
 76.1000 +#  ifndef PNG_WRITE_iTXt_SUPPORTED
 76.1001 +#    define PNG_WRITE_iTXt_SUPPORTED
 76.1002 +#  endif
 76.1003 +#  ifndef PNG_iTXt_SUPPORTED
 76.1004 +#    define PNG_iTXt_SUPPORTED
 76.1005 +#  endif
 76.1006 +#endif
 76.1007 +#ifndef PNG_NO_WRITE_oFFs
 76.1008 +#  define PNG_WRITE_oFFs_SUPPORTED
 76.1009 +#  ifndef PNG_oFFs_SUPPORTED
 76.1010 +#    define PNG_oFFs_SUPPORTED
 76.1011 +#  endif
 76.1012 +#endif
 76.1013 +#ifndef PNG_NO_WRITE_pCAL
 76.1014 +#  define PNG_WRITE_pCAL_SUPPORTED
 76.1015 +#  ifndef PNG_pCAL_SUPPORTED
 76.1016 +#    define PNG_pCAL_SUPPORTED
 76.1017 +#  endif
 76.1018 +#endif
 76.1019 +#ifndef PNG_NO_WRITE_sCAL
 76.1020 +#  define PNG_WRITE_sCAL_SUPPORTED
 76.1021 +#  ifndef PNG_sCAL_SUPPORTED
 76.1022 +#    define PNG_sCAL_SUPPORTED
 76.1023 +#  endif
 76.1024 +#endif
 76.1025 +#ifndef PNG_NO_WRITE_pHYs
 76.1026 +#  define PNG_WRITE_pHYs_SUPPORTED
 76.1027 +#  ifndef PNG_pHYs_SUPPORTED
 76.1028 +#    define PNG_pHYs_SUPPORTED
 76.1029 +#  endif
 76.1030 +#endif
 76.1031 +#ifndef PNG_NO_WRITE_sBIT
 76.1032 +#  define PNG_WRITE_sBIT_SUPPORTED
 76.1033 +#  ifndef PNG_sBIT_SUPPORTED
 76.1034 +#    define PNG_sBIT_SUPPORTED
 76.1035 +#  endif
 76.1036 +#endif
 76.1037 +#ifndef PNG_NO_WRITE_sPLT
 76.1038 +#  define PNG_WRITE_sPLT_SUPPORTED
 76.1039 +#  ifndef PNG_sPLT_SUPPORTED
 76.1040 +#    define PNG_sPLT_SUPPORTED
 76.1041 +#  endif
 76.1042 +#endif
 76.1043 +#ifndef PNG_NO_WRITE_sRGB
 76.1044 +#  define PNG_WRITE_sRGB_SUPPORTED
 76.1045 +#  ifndef PNG_sRGB_SUPPORTED
 76.1046 +#    define PNG_sRGB_SUPPORTED
 76.1047 +#  endif
 76.1048 +#endif
 76.1049 +#ifndef PNG_NO_WRITE_tEXt
 76.1050 +#  define PNG_WRITE_tEXt_SUPPORTED
 76.1051 +#  ifndef PNG_tEXt_SUPPORTED
 76.1052 +#    define PNG_tEXt_SUPPORTED
 76.1053 +#  endif
 76.1054 +#endif
 76.1055 +#ifndef PNG_NO_WRITE_tIME
 76.1056 +#  define PNG_WRITE_tIME_SUPPORTED
 76.1057 +#  ifndef PNG_tIME_SUPPORTED
 76.1058 +#    define PNG_tIME_SUPPORTED
 76.1059 +#  endif
 76.1060 +#endif
 76.1061 +#ifndef PNG_NO_WRITE_tRNS
 76.1062 +#  define PNG_WRITE_tRNS_SUPPORTED
 76.1063 +#  ifndef PNG_tRNS_SUPPORTED
 76.1064 +#    define PNG_tRNS_SUPPORTED
 76.1065 +#  endif
 76.1066 +#endif
 76.1067 +#ifndef PNG_NO_WRITE_zTXt
 76.1068 +#  define PNG_WRITE_zTXt_SUPPORTED
 76.1069 +#  ifndef PNG_zTXt_SUPPORTED
 76.1070 +#    define PNG_zTXt_SUPPORTED
 76.1071 +#  endif
 76.1072 +#endif
 76.1073 +#ifndef PNG_NO_WRITE_UNKNOWN_CHUNKS
 76.1074 +#  define PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
 76.1075 +#  ifndef PNG_UNKNOWN_CHUNKS_SUPPORTED
 76.1076 +#    define PNG_UNKNOWN_CHUNKS_SUPPORTED
 76.1077 +#  endif
 76.1078 +#  ifndef PNG_NO_HANDLE_AS_UNKNOWN
 76.1079 +#     ifndef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
 76.1080 +#       define PNG_HANDLE_AS_UNKNOWN_SUPPORTED
 76.1081 +#     endif
 76.1082 +#  endif
 76.1083 +#endif
 76.1084 +#if defined(PNG_WRITE_iTXt_SUPPORTED) || defined(PNG_WRITE_tEXt_SUPPORTED) || \
 76.1085 +    defined(PNG_WRITE_zTXt_SUPPORTED)
 76.1086 +#  define PNG_WRITE_TEXT_SUPPORTED
 76.1087 +#  ifndef PNG_TEXT_SUPPORTED
 76.1088 +#    define PNG_TEXT_SUPPORTED
 76.1089 +#  endif
 76.1090 +#endif
 76.1091 +
 76.1092 +#endif /* PNG_WRITE_ANCILLARY_CHUNKS_SUPPORTED */
 76.1093 +
 76.1094 +/* Turn this off to disable png_read_png() and
 76.1095 + * png_write_png() and leave the row_pointers member
 76.1096 + * out of the info structure.
 76.1097 + */
 76.1098 +#ifndef PNG_NO_INFO_IMAGE
 76.1099 +#  define PNG_INFO_IMAGE_SUPPORTED
 76.1100 +#endif
 76.1101 +
 76.1102 +/* need the time information for reading tIME chunks */
 76.1103 +#if defined(PNG_tIME_SUPPORTED)
 76.1104 +#  if !defined(_WIN32_WCE)
 76.1105 +     /* "time.h" functions are not supported on WindowsCE */
 76.1106 +#    include <time.h>
 76.1107 +#  endif
 76.1108 +#endif
 76.1109 +
 76.1110 +/* Some typedefs to get us started.  These should be safe on most of the
 76.1111 + * common platforms.  The typedefs should be at least as large as the
 76.1112 + * numbers suggest (a png_uint_32 must be at least 32 bits long), but they
 76.1113 + * don't have to be exactly that size.  Some compilers dislike passing
 76.1114 + * unsigned shorts as function parameters, so you may be better off using
 76.1115 + * unsigned int for png_uint_16.  Likewise, for 64-bit systems, you may
 76.1116 + * want to have unsigned int for png_uint_32 instead of unsigned long.
 76.1117 + */
 76.1118 +
 76.1119 +typedef unsigned long png_uint_32;
 76.1120 +typedef long png_int_32;
 76.1121 +typedef unsigned short png_uint_16;
 76.1122 +typedef short png_int_16;
 76.1123 +typedef unsigned char png_byte;
 76.1124 +
 76.1125 +/* This is usually size_t.  It is typedef'ed just in case you need it to
 76.1126 +   change (I'm not sure if you will or not, so I thought I'd be safe) */
 76.1127 +#ifdef PNG_SIZE_T
 76.1128 +   typedef PNG_SIZE_T png_size_t;
 76.1129 +#  define png_sizeof(x) png_convert_size(sizeof(x))
 76.1130 +#else
 76.1131 +   typedef size_t png_size_t;
 76.1132 +#  define png_sizeof(x) sizeof(x)
 76.1133 +#endif
 76.1134 +
 76.1135 +/* The following is needed for medium model support.  It cannot be in the
 76.1136 + * PNG_INTERNAL section.  Needs modification for other compilers besides
 76.1137 + * MSC.  Model independent support declares all arrays and pointers to be
 76.1138 + * large using the far keyword.  The zlib version used must also support
 76.1139 + * model independent data.  As of version zlib 1.0.4, the necessary changes
 76.1140 + * have been made in zlib.  The USE_FAR_KEYWORD define triggers other
 76.1141 + * changes that are needed. (Tim Wegner)
 76.1142 + */
 76.1143 +
 76.1144 +/* Separate compiler dependencies (problem here is that zlib.h always
 76.1145 +   defines FAR. (SJT) */
 76.1146 +#ifdef __BORLANDC__
 76.1147 +#  if defined(__LARGE__) || defined(__HUGE__) || defined(__COMPACT__)
 76.1148 +#    define LDATA 1
 76.1149 +#  else
 76.1150 +#    define LDATA 0
 76.1151 +#  endif
 76.1152 +   /* GRR:  why is Cygwin in here?  Cygwin is not Borland C... */
 76.1153 +#  if !defined(__WIN32__) && !defined(__FLAT__) && !defined(__CYGWIN__)
 76.1154 +#    define PNG_MAX_MALLOC_64K
 76.1155 +#    if (LDATA != 1)
 76.1156 +#      ifndef FAR
 76.1157 +#        define FAR __far
 76.1158 +#      endif
 76.1159 +#      define USE_FAR_KEYWORD
 76.1160 +#    endif   /* LDATA != 1 */
 76.1161 +     /* Possibly useful for moving data out of default segment.
 76.1162 +      * Uncomment it if you want. Could also define FARDATA as
 76.1163 +      * const if your compiler supports it. (SJT)
 76.1164 +#    define FARDATA FAR
 76.1165 +      */
 76.1166 +#  endif  /* __WIN32__, __FLAT__, __CYGWIN__ */
 76.1167 +#endif   /* __BORLANDC__ */
 76.1168 +
 76.1169 +
 76.1170 +/* Suggest testing for specific compiler first before testing for
 76.1171 + * FAR.  The Watcom compiler defines both __MEDIUM__ and M_I86MM,
 76.1172 + * making reliance oncertain keywords suspect. (SJT)
 76.1173 + */
 76.1174 +
 76.1175 +/* MSC Medium model */
 76.1176 +#if defined(FAR)
 76.1177 +#  if defined(M_I86MM)
 76.1178 +#    define USE_FAR_KEYWORD
 76.1179 +#    define FARDATA FAR
 76.1180 +#    include <dos.h>
 76.1181 +#  endif
 76.1182 +#endif
 76.1183 +
 76.1184 +/* SJT: default case */
 76.1185 +#ifndef FAR
 76.1186 +#  define FAR
 76.1187 +#endif
 76.1188 +
 76.1189 +/* At this point FAR is always defined */
 76.1190 +#ifndef FARDATA
 76.1191 +#  define FARDATA
 76.1192 +#endif
 76.1193 +
 76.1194 +/* Typedef for floating-point numbers that are converted
 76.1195 +   to fixed-point with a multiple of 100,000, e.g., int_gamma */
 76.1196 +typedef png_int_32 png_fixed_point;
 76.1197 +
 76.1198 +/* Add typedefs for pointers */
 76.1199 +typedef void            FAR * png_voidp;
 76.1200 +typedef png_byte        FAR * png_bytep;
 76.1201 +typedef png_uint_32     FAR * png_uint_32p;
 76.1202 +typedef png_int_32      FAR * png_int_32p;
 76.1203 +typedef png_uint_16     FAR * png_uint_16p;
 76.1204 +typedef png_int_16      FAR * png_int_16p;
 76.1205 +typedef PNG_CONST char  FAR * png_const_charp;
 76.1206 +typedef char            FAR * png_charp;
 76.1207 +typedef png_fixed_point FAR * png_fixed_point_p;
 76.1208 +
 76.1209 +#ifndef PNG_NO_STDIO
 76.1210 +#if defined(_WIN32_WCE)
 76.1211 +typedef HANDLE                png_FILE_p;
 76.1212 +#else
 76.1213 +typedef FILE                * png_FILE_p;
 76.1214 +#endif
 76.1215 +#endif
 76.1216 +
 76.1217 +#ifdef PNG_FLOATING_POINT_SUPPORTED
 76.1218 +typedef double          FAR * png_doublep;
 76.1219 +#endif
 76.1220 +
 76.1221 +/* Pointers to pointers; i.e. arrays */
 76.1222 +typedef png_byte        FAR * FAR * png_bytepp;
 76.1223 +typedef png_uint_32     FAR * FAR * png_uint_32pp;
 76.1224 +typedef png_int_32      FAR * FAR * png_int_32pp;
 76.1225 +typedef png_uint_16     FAR * FAR * png_uint_16pp;
 76.1226 +typedef png_int_16      FAR * FAR * png_int_16pp;
 76.1227 +typedef PNG_CONST char  FAR * FAR * png_const_charpp;
 76.1228 +typedef char            FAR * FAR * png_charpp;
 76.1229 +typedef png_fixed_point FAR * FAR * png_fixed_point_pp;
 76.1230 +#ifdef PNG_FLOATING_POINT_SUPPORTED
 76.1231 +typedef double          FAR * FAR * png_doublepp;
 76.1232 +#endif
 76.1233 +
 76.1234 +/* Pointers to pointers to pointers; i.e., pointer to array */
 76.1235 +typedef char            FAR * FAR * FAR * png_charppp;
 76.1236 +
 76.1237 +#if defined(PNG_1_0_X) || defined(PNG_1_2_X)
 76.1238 +/* SPC -  Is this stuff deprecated? */
 76.1239 +/* It'll be removed as of libpng-1.3.0 - GR-P */
 76.1240 +/* libpng typedefs for types in zlib. If zlib changes
 76.1241 + * or another compression library is used, then change these.
 76.1242 + * Eliminates need to change all the source files.
 76.1243 + */
 76.1244 +typedef charf *         png_zcharp;
 76.1245 +typedef charf * FAR *   png_zcharpp;
 76.1246 +typedef z_stream FAR *  png_zstreamp;
 76.1247 +#endif /* (PNG_1_0_X) || defined(PNG_1_2_X) */
 76.1248 +
 76.1249 +/*
 76.1250 + * Define PNG_BUILD_DLL if the module being built is a Windows
 76.1251 + * LIBPNG DLL.
 76.1252 + *
 76.1253 + * Define PNG_USE_DLL if you want to *link* to the Windows LIBPNG DLL.
 76.1254 + * It is equivalent to Microsoft predefined macro _DLL that is
 76.1255 + * automatically defined when you compile using the share
 76.1256 + * version of the CRT (C Run-Time library)
 76.1257 + *
 76.1258 + * The cygwin mods make this behavior a little different:
 76.1259 + * Define PNG_BUILD_DLL if you are building a dll for use with cygwin
 76.1260 + * Define PNG_STATIC if you are building a static library for use with cygwin,
 76.1261 + *   -or- if you are building an application that you want to link to the
 76.1262 + *   static library.
 76.1263 + * PNG_USE_DLL is defined by default (no user action needed) unless one of
 76.1264 + *   the other flags is defined.
 76.1265 + */
 76.1266 +
 76.1267 +#if !defined(PNG_DLL) && (defined(PNG_BUILD_DLL) || defined(PNG_USE_DLL))
 76.1268 +#  define PNG_DLL
 76.1269 +#endif
 76.1270 +/* If CYGWIN, then disallow GLOBAL ARRAYS unless building a static lib.
 76.1271 + * When building a static lib, default to no GLOBAL ARRAYS, but allow
 76.1272 + * command-line override
 76.1273 + */
 76.1274 +#if defined(__CYGWIN__)
 76.1275 +#  if !defined(PNG_STATIC)
 76.1276 +#    if defined(PNG_USE_GLOBAL_ARRAYS)
 76.1277 +#      undef PNG_USE_GLOBAL_ARRAYS
 76.1278 +#    endif
 76.1279 +#    if !defined(PNG_USE_LOCAL_ARRAYS)
 76.1280 +#      define PNG_USE_LOCAL_ARRAYS
 76.1281 +#    endif
 76.1282 +#  else
 76.1283 +#    if defined(PNG_USE_LOCAL_ARRAYS) || defined(PNG_NO_GLOBAL_ARRAYS)
 76.1284 +#      if defined(PNG_USE_GLOBAL_ARRAYS)
 76.1285 +#        undef PNG_USE_GLOBAL_ARRAYS
 76.1286 +#      endif
 76.1287 +#    endif
 76.1288 +#  endif
 76.1289 +#  if !defined(PNG_USE_LOCAL_ARRAYS) && !defined(PNG_USE_GLOBAL_ARRAYS)
 76.1290 +#    define PNG_USE_LOCAL_ARRAYS
 76.1291 +#  endif
 76.1292 +#endif
 76.1293 +
 76.1294 +/* Do not use global arrays (helps with building DLL's)
 76.1295 + * They are no longer used in libpng itself, since version 1.0.5c,
 76.1296 + * but might be required for some pre-1.0.5c applications.
 76.1297 + */
 76.1298 +#if !defined(PNG_USE_LOCAL_ARRAYS) && !defined(PNG_USE_GLOBAL_ARRAYS)
 76.1299 +#  if defined(PNG_NO_GLOBAL_ARRAYS) || \
 76.1300 +      (defined(__GNUC__) && defined(PNG_DLL)) || defined(_MSC_VER)
 76.1301 +#    define PNG_USE_LOCAL_ARRAYS
 76.1302 +#  else
 76.1303 +#    define PNG_USE_GLOBAL_ARRAYS
 76.1304 +#  endif
 76.1305 +#endif
 76.1306 +
 76.1307 +#if defined(__CYGWIN__)
 76.1308 +#  undef PNGAPI
 76.1309 +#  define PNGAPI __cdecl
 76.1310 +#  undef PNG_IMPEXP
 76.1311 +#  define PNG_IMPEXP
 76.1312 +#endif  
 76.1313 +
 76.1314 +/* If you define PNGAPI, e.g., with compiler option "-DPNGAPI=__stdcall",
 76.1315 + * you may get warnings regarding the linkage of png_zalloc and png_zfree.
 76.1316 + * Don't ignore those warnings; you must also reset the default calling
 76.1317 + * convention in your compiler to match your PNGAPI, and you must build
 76.1318 + * zlib and your applications the same way you build libpng.
 76.1319 + */
 76.1320 +
 76.1321 +#if defined(__MINGW32__) && !defined(PNG_MODULEDEF)
 76.1322 +#  ifndef PNG_NO_MODULEDEF
 76.1323 +#    define PNG_NO_MODULEDEF
 76.1324 +#  endif
 76.1325 +#endif
 76.1326 +
 76.1327 +#if !defined(PNG_IMPEXP) && defined(PNG_BUILD_DLL) && !defined(PNG_NO_MODULEDEF)
 76.1328 +#  define PNG_IMPEXP
 76.1329 +#endif
 76.1330 +
 76.1331 +#if defined(PNG_DLL) || defined(_DLL) || defined(__DLL__ ) || \
 76.1332 +    (( defined(_Windows) || defined(_WINDOWS) || \
 76.1333 +       defined(WIN32) || defined(_WIN32) || defined(__WIN32__) ))
 76.1334 +
 76.1335 +#  ifndef PNGAPI
 76.1336 +#     if defined(__GNUC__) || (defined (_MSC_VER) && (_MSC_VER >= 800))
 76.1337 +#        define PNGAPI __cdecl
 76.1338 +#     else
 76.1339 +#        define PNGAPI _cdecl
 76.1340 +#     endif
 76.1341 +#  endif
 76.1342 +
 76.1343 +#  if !defined(PNG_IMPEXP) && (!defined(PNG_DLL) || \
 76.1344 +       0 /* WINCOMPILER_WITH_NO_SUPPORT_FOR_DECLIMPEXP */)
 76.1345 +#     define PNG_IMPEXP
 76.1346 +#  endif
 76.1347 +
 76.1348 +#  if !defined(PNG_IMPEXP)
 76.1349 +
 76.1350 +#     define PNG_EXPORT_TYPE1(type,symbol)  PNG_IMPEXP type PNGAPI symbol
 76.1351 +#     define PNG_EXPORT_TYPE2(type,symbol)  type PNG_IMPEXP PNGAPI symbol
 76.1352 +
 76.1353 +      /* Borland/Microsoft */
 76.1354 +#     if defined(_MSC_VER) || defined(__BORLANDC__)
 76.1355 +#        if (_MSC_VER >= 800) || (__BORLANDC__ >= 0x500)
 76.1356 +#           define PNG_EXPORT PNG_EXPORT_TYPE1
 76.1357 +#        else
 76.1358 +#           define PNG_EXPORT PNG_EXPORT_TYPE2
 76.1359 +#           if defined(PNG_BUILD_DLL)
 76.1360 +#              define PNG_IMPEXP __export
 76.1361 +#           else
 76.1362 +#              define PNG_IMPEXP /*__import */ /* doesn't exist AFAIK in
 76.1363 +                                                 VC++ */
 76.1364 +#           endif                             /* Exists in Borland C++ for
 76.1365 +                                                 C++ classes (== huge) */
 76.1366 +#        endif
 76.1367 +#     endif
 76.1368 +
 76.1369 +#     if !defined(PNG_IMPEXP)
 76.1370 +#        if defined(PNG_BUILD_DLL)
 76.1371 +#           define PNG_IMPEXP __declspec(dllexport)
 76.1372 +#        else
 76.1373 +#           define PNG_IMPEXP __declspec(dllimport)
 76.1374 +#        endif
 76.1375 +#     endif
 76.1376 +#  endif  /* PNG_IMPEXP */
 76.1377 +#else /* !(DLL || non-cygwin WINDOWS) */
 76.1378 +#   if (defined(__IBMC__) || defined(__IBMCPP__)) && defined(__OS2__)
 76.1379 +#      ifndef PNGAPI
 76.1380 +#         define PNGAPI _System
 76.1381 +#      endif
 76.1382 +#   else
 76.1383 +#      if 0 /* ... other platforms, with other meanings */
 76.1384 +#      endif
 76.1385 +#   endif
 76.1386 +#endif
 76.1387 +
 76.1388 +#ifndef PNGAPI
 76.1389 +#  define PNGAPI
 76.1390 +#endif
 76.1391 +#ifndef PNG_IMPEXP
 76.1392 +#  define PNG_IMPEXP
 76.1393 +#endif
 76.1394 +
 76.1395 +#ifdef PNG_BUILDSYMS
 76.1396 +#  ifndef PNG_EXPORT
 76.1397 +#    define PNG_EXPORT(type,symbol) PNG_FUNCTION_EXPORT symbol END
 76.1398 +#  endif
 76.1399 +#  ifdef PNG_USE_GLOBAL_ARRAYS
 76.1400 +#    ifndef PNG_EXPORT_VAR
 76.1401 +#      define PNG_EXPORT_VAR(type) PNG_DATA_EXPORT
 76.1402 +#    endif
 76.1403 +#  endif
 76.1404 +#endif
 76.1405 +
 76.1406 +#ifndef PNG_EXPORT
 76.1407 +#  define PNG_EXPORT(type,symbol) PNG_IMPEXP type PNGAPI symbol
 76.1408 +#endif
 76.1409 +
 76.1410 +#ifdef PNG_USE_GLOBAL_ARRAYS
 76.1411 +#  ifndef PNG_EXPORT_VAR
 76.1412 +#    define PNG_EXPORT_VAR(type) extern PNG_IMPEXP type
 76.1413 +#  endif
 76.1414 +#endif
 76.1415 +
 76.1416 +/* User may want to use these so they are not in PNG_INTERNAL. Any library
 76.1417 + * functions that are passed far data must be model independent.
 76.1418 + */
 76.1419 +
 76.1420 +#ifndef PNG_ABORT
 76.1421 +#  define PNG_ABORT() abort()
 76.1422 +#endif
 76.1423 +
 76.1424 +#ifdef PNG_SETJMP_SUPPORTED
 76.1425 +#  define png_jmpbuf(png_ptr) ((png_ptr)->jmpbuf)
 76.1426 +#else
 76.1427 +#  define png_jmpbuf(png_ptr) \
 76.1428 +   (LIBPNG_WAS_COMPILED_WITH__PNG_SETJMP_NOT_SUPPORTED)
 76.1429 +#endif
 76.1430 +
 76.1431 +#if defined(USE_FAR_KEYWORD)  /* memory model independent fns */
 76.1432 +/* use this to make far-to-near assignments */
 76.1433 +#  define CHECK   1
 76.1434 +#  define NOCHECK 0
 76.1435 +#  define CVT_PTR(ptr) (png_far_to_near(png_ptr,ptr,CHECK))
 76.1436 +#  define CVT_PTR_NOCHECK(ptr) (png_far_to_near(png_ptr,ptr,NOCHECK))
 76.1437 +#  define png_snprintf _fsnprintf   /* Added to v 1.2.19 */
 76.1438 +#  define png_strlen  _fstrlen
 76.1439 +#  define png_memcmp  _fmemcmp    /* SJT: added */
 76.1440 +#  define png_memcpy  _fmemcpy
 76.1441 +#  define png_memset  _fmemset
 76.1442 +#else /* use the usual functions */
 76.1443 +#  define CVT_PTR(ptr)         (ptr)
 76.1444 +#  define CVT_PTR_NOCHECK(ptr) (ptr)
 76.1445 +#  ifndef PNG_NO_SNPRINTF
 76.1446 +#    ifdef _MSC_VER
 76.1447 +#      define png_snprintf _snprintf   /* Added to v 1.2.19 */
 76.1448 +#      define png_snprintf2 _snprintf
 76.1449 +#      define png_snprintf6 _snprintf
 76.1450 +#    else
 76.1451 +#      define png_snprintf snprintf   /* Added to v 1.2.19 */
 76.1452 +#      define png_snprintf2 snprintf
 76.1453 +#      define png_snprintf6 snprintf
 76.1454 +#    endif
 76.1455 +#  else
 76.1456 +     /* You don't have or don't want to use snprintf().  Caution: Using
 76.1457 +      * sprintf instead of snprintf exposes your application to accidental
 76.1458 +      * or malevolent buffer overflows.  If you don't have snprintf()
 76.1459 +      * as a general rule you should provide one (you can get one from
 76.1460 +      * Portable OpenSSH). */
 76.1461 +#    define png_snprintf(s1,n,fmt,x1) sprintf(s1,fmt,x1)
 76.1462 +#    define png_snprintf2(s1,n,fmt,x1,x2) sprintf(s1,fmt,x1,x2)
 76.1463 +#    define png_snprintf6(s1,n,fmt,x1,x2,x3,x4,x5,x6) \
 76.1464 +        sprintf(s1,fmt,x1,x2,x3,x4,x5,x6)
 76.1465 +#  endif
 76.1466 +#  define png_strlen  strlen
 76.1467 +#  define png_memcmp  memcmp      /* SJT: added */
 76.1468 +#  define png_memcpy  memcpy
 76.1469 +#  define png_memset  memset
 76.1470 +#endif
 76.1471 +/* End of memory model independent support */
 76.1472 +
 76.1473 +/* Just a little check that someone hasn't tried to define something
 76.1474 + * contradictory.
 76.1475 + */
 76.1476 +#if (PNG_ZBUF_SIZE > 65536L) && defined(PNG_MAX_MALLOC_64K)
 76.1477 +#  undef PNG_ZBUF_SIZE
 76.1478 +#  define PNG_ZBUF_SIZE 65536L
 76.1479 +#endif
 76.1480 +
 76.1481 +/* Added at libpng-1.2.8 */
 76.1482 +#endif /* PNG_VERSION_INFO_ONLY */
 76.1483 +
 76.1484 +#endif /* PNGCONF_H */
    77.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    77.2 +++ b/libs/libpng/pngerror.c	Sat Sep 19 05:51:51 2015 +0300
    77.3 @@ -0,0 +1,345 @@
    77.4 +
    77.5 +/* pngerror.c - stub functions for i/o and memory allocation
    77.6 + *
    77.7 + * Last changed in libpng 1.2.30 [August 15, 2008]
    77.8 + * For conditions of distribution and use, see copyright notice in png.h
    77.9 + * Copyright (c) 1998-2008 Glenn Randers-Pehrson
   77.10 + * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
   77.11 + * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
   77.12 + *
   77.13 + * This file provides a location for all error handling.  Users who
   77.14 + * need special error handling are expected to write replacement functions
   77.15 + * and use png_set_error_fn() to use those functions.  See the instructions
   77.16 + * at each function.
   77.17 + */
   77.18 +
   77.19 +#define PNG_INTERNAL
   77.20 +#include "png.h"
   77.21 +#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
   77.22 +
   77.23 +static void /* PRIVATE */
   77.24 +png_default_error PNGARG((png_structp png_ptr,
   77.25 +  png_const_charp error_message));
   77.26 +#ifndef PNG_NO_WARNINGS
   77.27 +static void /* PRIVATE */
   77.28 +png_default_warning PNGARG((png_structp png_ptr,
   77.29 +  png_const_charp warning_message));
   77.30 +#endif /* PNG_NO_WARNINGS */
   77.31 +
   77.32 +/* This function is called whenever there is a fatal error.  This function
   77.33 + * should not be changed.  If there is a need to handle errors differently,
   77.34 + * you should supply a replacement error function and use png_set_error_fn()
   77.35 + * to replace the error function at run-time.
   77.36 + */
   77.37 +#ifndef PNG_NO_ERROR_TEXT
   77.38 +void PNGAPI
   77.39 +png_error(png_structp png_ptr, png_const_charp error_message)
   77.40 +{
   77.41 +#ifdef PNG_ERROR_NUMBERS_SUPPORTED
   77.42 +   char msg[16];
   77.43 +   if (png_ptr != NULL)
   77.44 +   {
   77.45 +     if (png_ptr->flags&
   77.46 +       (PNG_FLAG_STRIP_ERROR_NUMBERS|PNG_FLAG_STRIP_ERROR_TEXT))
   77.47 +     {
   77.48 +       if (*error_message == '#')
   77.49 +       {
   77.50 +         /* Strip "#nnnn " from beginning of error message. */
   77.51 +           int offset;
   77.52 +           for (offset = 1; offset<15; offset++)
   77.53 +              if (error_message[offset] == ' ')
   77.54 +                  break;
   77.55 +           if (png_ptr->flags&PNG_FLAG_STRIP_ERROR_TEXT)
   77.56 +           {
   77.57 +              int i;
   77.58 +              for (i = 0; i < offset - 1; i++)
   77.59 +                 msg[i] = error_message[i + 1];
   77.60 +              msg[i - 1] = '\0';
   77.61 +              error_message = msg;
   77.62 +           }
   77.63 +           else
   77.64 +              error_message += offset;
   77.65 +       }
   77.66 +       else
   77.67 +       {
   77.68 +           if (png_ptr->flags&PNG_FLAG_STRIP_ERROR_TEXT)
   77.69 +           {
   77.70 +              msg[0] = '0';
   77.71 +              msg[1] = '\0';
   77.72 +              error_message = msg;
   77.73 +           }
   77.74 +       }
   77.75 +     }
   77.76 +   }
   77.77 +#endif
   77.78 +   if (png_ptr != NULL && png_ptr->error_fn != NULL)
   77.79 +      (*(png_ptr->error_fn))(png_ptr, error_message);
   77.80 +
   77.81 +   /* If the custom handler doesn't exist, or if it returns,
   77.82 +      use the default handler, which will not return. */
   77.83 +   png_default_error(png_ptr, error_message);
   77.84 +}
   77.85 +#else
   77.86 +void PNGAPI
   77.87 +png_err(png_structp png_ptr)
   77.88 +{
   77.89 +   if (png_ptr != NULL && png_ptr->error_fn != NULL)
   77.90 +      (*(png_ptr->error_fn))(png_ptr, '\0');
   77.91 +
   77.92 +   /* If the custom handler doesn't exist, or if it returns,
   77.93 +      use the default handler, which will not return. */
   77.94 +   png_default_error(png_ptr, '\0');
   77.95 +}
   77.96 +#endif /* PNG_NO_ERROR_TEXT */
   77.97 +
   77.98 +#ifndef PNG_NO_WARNINGS
   77.99 +/* This function is called whenever there is a non-fatal error.  This function
  77.100 + * should not be changed.  If there is a need to handle warnings differently,
  77.101 + * you should supply a replacement warning function and use
  77.102 + * png_set_error_fn() to replace the warning function at run-time.
  77.103 + */
  77.104 +void PNGAPI
  77.105 +png_warning(png_structp png_ptr, png_const_charp warning_message)
  77.106 +{
  77.107 +   int offset = 0;
  77.108 +   if (png_ptr != NULL)
  77.109 +   {
  77.110 +#ifdef PNG_ERROR_NUMBERS_SUPPORTED
  77.111 +   if (png_ptr->flags&
  77.112 +     (PNG_FLAG_STRIP_ERROR_NUMBERS|PNG_FLAG_STRIP_ERROR_TEXT))
  77.113 +#endif
  77.114 +     {
  77.115 +       if (*warning_message == '#')
  77.116 +       {
  77.117 +           for (offset = 1; offset < 15; offset++)
  77.118 +              if (warning_message[offset] == ' ')
  77.119 +                  break;
  77.120 +       }
  77.121 +     }
  77.122 +     if (png_ptr != NULL && png_ptr->warning_fn != NULL)
  77.123 +        (*(png_ptr->warning_fn))(png_ptr, warning_message + offset);
  77.124 +   }
  77.125 +   else
  77.126 +      png_default_warning(png_ptr, warning_message + offset);
  77.127 +}
  77.128 +#endif /* PNG_NO_WARNINGS */
  77.129 +
  77.130 +
  77.131 +/* These utilities are used internally to build an error message that relates
  77.132 + * to the current chunk.  The chunk name comes from png_ptr->chunk_name,
  77.133 + * this is used to prefix the message.  The message is limited in length
  77.134 + * to 63 bytes, the name characters are output as hex digits wrapped in []
  77.135 + * if the character is invalid.
  77.136 + */
  77.137 +#define isnonalpha(c) ((c) < 65 || (c) > 122 || ((c) > 90 && (c) < 97))
  77.138 +static PNG_CONST char png_digit[16] = {
  77.139 +   '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  77.140 +   'A', 'B', 'C', 'D', 'E', 'F'
  77.141 +};
  77.142 +
  77.143 +#define PNG_MAX_ERROR_TEXT 64
  77.144 +
  77.145 +#if !defined(PNG_NO_WARNINGS) || !defined(PNG_NO_ERROR_TEXT)
  77.146 +static void /* PRIVATE */
  77.147 +png_format_buffer(png_structp png_ptr, png_charp buffer, png_const_charp
  77.148 +   error_message)
  77.149 +{
  77.150 +   int iout = 0, iin = 0;
  77.151 +
  77.152 +   while (iin < 4)
  77.153 +   {
  77.154 +      int c = png_ptr->chunk_name[iin++];
  77.155 +      if (isnonalpha(c))
  77.156 +      {
  77.157 +         buffer[iout++] = '[';
  77.158 +         buffer[iout++] = png_digit[(c & 0xf0) >> 4];
  77.159 +         buffer[iout++] = png_digit[c & 0x0f];
  77.160 +         buffer[iout++] = ']';
  77.161 +      }
  77.162 +      else
  77.163 +      {
  77.164 +         buffer[iout++] = (png_byte)c;
  77.165 +      }
  77.166 +   }
  77.167 +
  77.168 +   if (error_message == NULL)
  77.169 +      buffer[iout] = '\0';
  77.170 +   else
  77.171 +   {
  77.172 +      buffer[iout++] = ':';
  77.173 +      buffer[iout++] = ' ';
  77.174 +      png_memcpy(buffer + iout, error_message, PNG_MAX_ERROR_TEXT);
  77.175 +      buffer[iout + PNG_MAX_ERROR_TEXT - 1] = '\0';
  77.176 +   }
  77.177 +}
  77.178 +
  77.179 +#ifdef PNG_READ_SUPPORTED
  77.180 +void PNGAPI
  77.181 +png_chunk_error(png_structp png_ptr, png_const_charp error_message)
  77.182 +{
  77.183 +   char msg[18+PNG_MAX_ERROR_TEXT];
  77.184 +   if (png_ptr == NULL)
  77.185 +     png_error(png_ptr, error_message);
  77.186 +   else
  77.187 +   {
  77.188 +     png_format_buffer(png_ptr, msg, error_message);
  77.189 +     png_error(png_ptr, msg);
  77.190 +   }
  77.191 +}
  77.192 +#endif /* PNG_READ_SUPPORTED */
  77.193 +#endif /* !defined(PNG_NO_WARNINGS) || !defined(PNG_NO_ERROR_TEXT) */
  77.194 +
  77.195 +#ifndef PNG_NO_WARNINGS
  77.196 +void PNGAPI
  77.197 +png_chunk_warning(png_structp png_ptr, png_const_charp warning_message)
  77.198 +{
  77.199 +   char msg[18+PNG_MAX_ERROR_TEXT];
  77.200 +   if (png_ptr == NULL)
  77.201 +     png_warning(png_ptr, warning_message);
  77.202 +   else
  77.203 +   {
  77.204 +     png_format_buffer(png_ptr, msg, warning_message);
  77.205 +     png_warning(png_ptr, msg);
  77.206 +   }
  77.207 +}
  77.208 +#endif /* PNG_NO_WARNINGS */
  77.209 +
  77.210 +
  77.211 +/* This is the default error handling function.  Note that replacements for
  77.212 + * this function MUST NOT RETURN, or the program will likely crash.  This
  77.213 + * function is used by default, or if the program supplies NULL for the
  77.214 + * error function pointer in png_set_error_fn().
  77.215 + */
  77.216 +static void /* PRIVATE */
  77.217 +png_default_error(png_structp png_ptr, png_const_charp error_message)
  77.218 +{
  77.219 +#ifndef PNG_NO_CONSOLE_IO
  77.220 +#ifdef PNG_ERROR_NUMBERS_SUPPORTED
  77.221 +   if (*error_message == '#')
  77.222 +   {
  77.223 +     /* Strip "#nnnn " from beginning of warning message. */
  77.224 +     int offset;
  77.225 +     char error_number[16];
  77.226 +     for (offset = 0; offset<15; offset++)
  77.227 +     {
  77.228 +         error_number[offset] = error_message[offset + 1];
  77.229 +         if (error_message[offset] == ' ')
  77.230 +             break;
  77.231 +     }
  77.232 +     if ((offset > 1) && (offset < 15))
  77.233 +     {
  77.234 +       error_number[offset - 1] = '\0';
  77.235 +       fprintf(stderr, "libpng error no. %s: %s\n", error_number,
  77.236 +          error_message + offset + 1);
  77.237 +     }
  77.238 +     else
  77.239 +       fprintf(stderr, "libpng error: %s, offset=%d\n", error_message, offset);
  77.240 +   }
  77.241 +   else
  77.242 +#endif
  77.243 +   fprintf(stderr, "libpng error: %s\n", error_message);
  77.244 +#endif
  77.245 +
  77.246 +#ifdef PNG_SETJMP_SUPPORTED
  77.247 +   if (png_ptr)
  77.248 +   {
  77.249 +#  ifdef USE_FAR_KEYWORD
  77.250 +   {
  77.251 +      jmp_buf jmpbuf;
  77.252 +      png_memcpy(jmpbuf, png_ptr->jmpbuf, png_sizeof(jmp_buf));
  77.253 +      longjmp(jmpbuf, 1);
  77.254 +   }
  77.255 +#  else
  77.256 +   longjmp(png_ptr->jmpbuf, 1);
  77.257 +#  endif
  77.258 +   }
  77.259 +#else
  77.260 +   PNG_ABORT();
  77.261 +#endif
  77.262 +#ifdef PNG_NO_CONSOLE_IO
  77.263 +   error_message = error_message; /* make compiler happy */
  77.264 +#endif
  77.265 +}
  77.266 +
  77.267 +#ifndef PNG_NO_WARNINGS
  77.268 +/* This function is called when there is a warning, but the library thinks
  77.269 + * it can continue anyway.  Replacement functions don't have to do anything
  77.270 + * here if you don't want them to.  In the default configuration, png_ptr is
  77.271 + * not used, but it is passed in case it may be useful.
  77.272 + */
  77.273 +static void /* PRIVATE */
  77.274 +png_default_warning(png_structp png_ptr, png_const_charp warning_message)
  77.275 +{
  77.276 +#ifndef PNG_NO_CONSOLE_IO
  77.277 +#  ifdef PNG_ERROR_NUMBERS_SUPPORTED
  77.278 +   if (*warning_message == '#')
  77.279 +   {
  77.280 +     int offset;
  77.281 +     char warning_number[16];
  77.282 +     for (offset = 0; offset < 15; offset++)
  77.283 +     {
  77.284 +        warning_number[offset] = warning_message[offset + 1];
  77.285 +        if (warning_message[offset] == ' ')
  77.286 +            break;
  77.287 +     }
  77.288 +     if ((offset > 1) && (offset < 15))
  77.289 +     {
  77.290 +       warning_number[offset + 1] = '\0';
  77.291 +       fprintf(stderr, "libpng warning no. %s: %s\n", warning_number,
  77.292 +          warning_message + offset);
  77.293 +     }
  77.294 +     else
  77.295 +       fprintf(stderr, "libpng warning: %s\n", warning_message);
  77.296 +   }
  77.297 +   else
  77.298 +#  endif
  77.299 +     fprintf(stderr, "libpng warning: %s\n", warning_message);
  77.300 +#else
  77.301 +   warning_message = warning_message; /* make compiler happy */
  77.302 +#endif
  77.303 +   png_ptr = png_ptr; /* make compiler happy */
  77.304 +}
  77.305 +#endif /* PNG_NO_WARNINGS */
  77.306 +
  77.307 +/* This function is called when the application wants to use another method
  77.308 + * of handling errors and warnings.  Note that the error function MUST NOT
  77.309 + * return to the calling routine or serious problems will occur.  The return
  77.310 + * method used in the default routine calls longjmp(png_ptr->jmpbuf, 1)
  77.311 + */
  77.312 +void PNGAPI
  77.313 +png_set_error_fn(png_structp png_ptr, png_voidp error_ptr,
  77.314 +   png_error_ptr error_fn, png_error_ptr warning_fn)
  77.315 +{
  77.316 +   if (png_ptr == NULL)
  77.317 +      return;
  77.318 +   png_ptr->error_ptr = error_ptr;
  77.319 +   png_ptr->error_fn = error_fn;
  77.320 +   png_ptr->warning_fn = warning_fn;
  77.321 +}
  77.322 +
  77.323 +
  77.324 +/* This function returns a pointer to the error_ptr associated with the user
  77.325 + * functions.  The application should free any memory associated with this
  77.326 + * pointer before png_write_destroy and png_read_destroy are called.
  77.327 + */
  77.328 +png_voidp PNGAPI
  77.329 +png_get_error_ptr(png_structp png_ptr)
  77.330 +{
  77.331 +   if (png_ptr == NULL)
  77.332 +      return NULL;
  77.333 +   return ((png_voidp)png_ptr->error_ptr);
  77.334 +}
  77.335 +
  77.336 +
  77.337 +#ifdef PNG_ERROR_NUMBERS_SUPPORTED
  77.338 +void PNGAPI
  77.339 +png_set_strip_error_numbers(png_structp png_ptr, png_uint_32 strip_mode)
  77.340 +{
  77.341 +   if (png_ptr != NULL)
  77.342 +   {
  77.343 +     png_ptr->flags &=
  77.344 +       ((~(PNG_FLAG_STRIP_ERROR_NUMBERS|PNG_FLAG_STRIP_ERROR_TEXT))&strip_mode);
  77.345 +   }
  77.346 +}
  77.347 +#endif
  77.348 +#endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */
    78.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    78.2 +++ b/libs/libpng/pnggccrd.c	Sat Sep 19 05:51:51 2015 +0300
    78.3 @@ -0,0 +1,103 @@
    78.4 +/* pnggccrd.c was removed from libpng-1.2.20. */
    78.5 +
    78.6 +/* This code snippet is for use by configure's compilation test. */
    78.7 +
    78.8 +#if (!defined _MSC_VER) && \
    78.9 +    defined(PNG_ASSEMBLER_CODE_SUPPORTED) && \
   78.10 +    defined(PNG_MMX_CODE_SUPPORTED)
   78.11 +
   78.12 +int PNGAPI png_dummy_mmx_support(void);
   78.13 +
   78.14 +static int _mmx_supported = 2; // 0: no MMX; 1: MMX supported; 2: not tested
   78.15 +
   78.16 +int PNGAPI
   78.17 +png_dummy_mmx_support(void) __attribute__((noinline));
   78.18 +
   78.19 +int PNGAPI
   78.20 +png_dummy_mmx_support(void)
   78.21 +{
   78.22 +   int result;
   78.23 +#if defined(PNG_MMX_CODE_SUPPORTED)  // superfluous, but what the heck
   78.24 +    __asm__ __volatile__ (
   78.25 +#if defined(__x86_64__)
   78.26 +        "pushq %%rbx          \n\t"  // rbx gets clobbered by CPUID instruction
   78.27 +        "pushq %%rcx          \n\t"  // so does rcx...
   78.28 +        "pushq %%rdx          \n\t"  // ...and rdx (but rcx & rdx safe on Linux)
   78.29 +        "pushfq               \n\t"  // save Eflag to stack
   78.30 +        "popq %%rax           \n\t"  // get Eflag from stack into rax
   78.31 +        "movq %%rax, %%rcx    \n\t"  // make another copy of Eflag in rcx
   78.32 +        "xorl $0x200000, %%eax \n\t" // toggle ID bit in Eflag (i.e., bit 21)
   78.33 +        "pushq %%rax          \n\t"  // save modified Eflag back to stack
   78.34 +        "popfq                \n\t"  // restore modified value to Eflag reg
   78.35 +        "pushfq               \n\t"  // save Eflag to stack
   78.36 +        "popq %%rax           \n\t"  // get Eflag from stack
   78.37 +        "pushq %%rcx          \n\t"  // save original Eflag to stack
   78.38 +        "popfq                \n\t"  // restore original Eflag
   78.39 +#else
   78.40 +        "pushl %%ebx          \n\t"  // ebx gets clobbered by CPUID instruction
   78.41 +        "pushl %%ecx          \n\t"  // so does ecx...
   78.42 +        "pushl %%edx          \n\t"  // ...and edx (but ecx & edx safe on Linux)
   78.43 +        "pushfl               \n\t"  // save Eflag to stack
   78.44 +        "popl %%eax           \n\t"  // get Eflag from stack into eax
   78.45 +        "movl %%eax, %%ecx    \n\t"  // make another copy of Eflag in ecx
   78.46 +        "xorl $0x200000, %%eax \n\t" // toggle ID bit in Eflag (i.e., bit 21)
   78.47 +        "pushl %%eax          \n\t"  // save modified Eflag back to stack
   78.48 +        "popfl                \n\t"  // restore modified value to Eflag reg
   78.49 +        "pushfl               \n\t"  // save Eflag to stack
   78.50 +        "popl %%eax           \n\t"  // get Eflag from stack
   78.51 +        "pushl %%ecx          \n\t"  // save original Eflag to stack
   78.52 +        "popfl                \n\t"  // restore original Eflag
   78.53 +#endif
   78.54 +        "xorl %%ecx, %%eax    \n\t"  // compare new Eflag with original Eflag
   78.55 +        "jz 0f                \n\t"  // if same, CPUID instr. is not supported
   78.56 +
   78.57 +        "xorl %%eax, %%eax    \n\t"  // set eax to zero
   78.58 +//      ".byte  0x0f, 0xa2    \n\t"  // CPUID instruction (two-byte opcode)
   78.59 +        "cpuid                \n\t"  // get the CPU identification info
   78.60 +        "cmpl $1, %%eax       \n\t"  // make sure eax return non-zero value
   78.61 +        "jl 0f                \n\t"  // if eax is zero, MMX is not supported
   78.62 +
   78.63 +        "xorl %%eax, %%eax    \n\t"  // set eax to zero and...
   78.64 +        "incl %%eax           \n\t"  // ...increment eax to 1.  This pair is
   78.65 +                                     // faster than the instruction "mov eax, 1"
   78.66 +        "cpuid                \n\t"  // get the CPU identification info again
   78.67 +        "andl $0x800000, %%edx \n\t" // mask out all bits but MMX bit (23)
   78.68 +        "cmpl $0, %%edx       \n\t"  // 0 = MMX not supported
   78.69 +        "jz 0f                \n\t"  // non-zero = yes, MMX IS supported
   78.70 +
   78.71 +        "movl $1, %%eax       \n\t"  // set return value to 1
   78.72 +        "jmp  1f              \n\t"  // DONE:  have MMX support
   78.73 +
   78.74 +    "0:                       \n\t"  // .NOT_SUPPORTED: target label for jump instructions
   78.75 +        "movl $0, %%eax       \n\t"  // set return value to 0
   78.76 +    "1:                       \n\t"  // .RETURN: target label for jump instructions
   78.77 +#if defined(__x86_64__)
   78.78 +        "popq %%rdx           \n\t"  // restore rdx
   78.79 +        "popq %%rcx           \n\t"  // restore rcx
   78.80 +        "popq %%rbx           \n\t"  // restore rbx
   78.81 +#else
   78.82 +        "popl %%edx           \n\t"  // restore edx
   78.83 +        "popl %%ecx           \n\t"  // restore ecx
   78.84 +        "popl %%ebx           \n\t"  // restore ebx
   78.85 +#endif
   78.86 +
   78.87 +//      "ret                  \n\t"  // DONE:  no MMX support
   78.88 +                                     // (fall through to standard C "ret")
   78.89 +
   78.90 +        : "=a" (result)              // output list
   78.91 +
   78.92 +        :                            // any variables used on input (none)
   78.93 +
   78.94 +                                     // no clobber list
   78.95 +//      , "%ebx", "%ecx", "%edx"     // GRR:  we handle these manually
   78.96 +//      , "memory"   // if write to a variable gcc thought was in a reg
   78.97 +//      , "cc"       // "condition codes" (flag bits)
   78.98 +    );
   78.99 +    _mmx_supported = result;
  78.100 +#else
  78.101 +    _mmx_supported = 0;
  78.102 +#endif /* PNG_MMX_CODE_SUPPORTED */
  78.103 +
  78.104 +    return _mmx_supported;
  78.105 +}
  78.106 +#endif
    79.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    79.2 +++ b/libs/libpng/pngget.c	Sat Sep 19 05:51:51 2015 +0300
    79.3 @@ -0,0 +1,900 @@
    79.4 +
    79.5 +/* pngget.c - retrieval of values from info struct
    79.6 + *
    79.7 + * Last changed in libpng 1.2.30 [August 15, 2008]
    79.8 + * For conditions of distribution and use, see copyright notice in png.h
    79.9 + * Copyright (c) 1998-2008 Glenn Randers-Pehrson
   79.10 + * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
   79.11 + * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
   79.12 + */
   79.13 +
   79.14 +#define PNG_INTERNAL
   79.15 +#include "png.h"
   79.16 +#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
   79.17 +
   79.18 +png_uint_32 PNGAPI
   79.19 +png_get_valid(png_structp png_ptr, png_infop info_ptr, png_uint_32 flag)
   79.20 +{
   79.21 +   if (png_ptr != NULL && info_ptr != NULL)
   79.22 +      return(info_ptr->valid & flag);
   79.23 +   else
   79.24 +      return(0);
   79.25 +}
   79.26 +
   79.27 +png_uint_32 PNGAPI
   79.28 +png_get_rowbytes(png_structp png_ptr, png_infop info_ptr)
   79.29 +{
   79.30 +   if (png_ptr != NULL && info_ptr != NULL)
   79.31 +      return(info_ptr->rowbytes);
   79.32 +   else
   79.33 +      return(0);
   79.34 +}
   79.35 +
   79.36 +#if defined(PNG_INFO_IMAGE_SUPPORTED)
   79.37 +png_bytepp PNGAPI
   79.38 +png_get_rows(png_structp png_ptr, png_infop info_ptr)
   79.39 +{
   79.40 +   if (png_ptr != NULL && info_ptr != NULL)
   79.41 +      return(info_ptr->row_pointers);
   79.42 +   else
   79.43 +      return(0);
   79.44 +}
   79.45 +#endif
   79.46 +
   79.47 +#ifdef PNG_EASY_ACCESS_SUPPORTED
   79.48 +/* easy access to info, added in libpng-0.99 */
   79.49 +png_uint_32 PNGAPI
   79.50 +png_get_image_width(png_structp png_ptr, png_infop info_ptr)
   79.51 +{
   79.52 +   if (png_ptr != NULL && info_ptr != NULL)
   79.53 +   {
   79.54 +      return info_ptr->width;
   79.55 +   }
   79.56 +   return (0);
   79.57 +}
   79.58 +
   79.59 +png_uint_32 PNGAPI
   79.60 +png_get_image_height(png_structp png_ptr, png_infop info_ptr)
   79.61 +{
   79.62 +   if (png_ptr != NULL && info_ptr != NULL)
   79.63 +   {
   79.64 +      return info_ptr->height;
   79.65 +   }
   79.66 +   return (0);
   79.67 +}
   79.68 +
   79.69 +png_byte PNGAPI
   79.70 +png_get_bit_depth(png_structp png_ptr, png_infop info_ptr)
   79.71 +{
   79.72 +   if (png_ptr != NULL && info_ptr != NULL)
   79.73 +   {
   79.74 +      return info_ptr->bit_depth;
   79.75 +   }
   79.76 +   return (0);
   79.77 +}
   79.78 +
   79.79 +png_byte PNGAPI
   79.80 +png_get_color_type(png_structp png_ptr, png_infop info_ptr)
   79.81 +{
   79.82 +   if (png_ptr != NULL && info_ptr != NULL)
   79.83 +   {
   79.84 +      return info_ptr->color_type;
   79.85 +   }
   79.86 +   return (0);
   79.87 +}
   79.88 +
   79.89 +png_byte PNGAPI
   79.90 +png_get_filter_type(png_structp png_ptr, png_infop info_ptr)
   79.91 +{
   79.92 +   if (png_ptr != NULL && info_ptr != NULL)
   79.93 +   {
   79.94 +      return info_ptr->filter_type;
   79.95 +   }
   79.96 +   return (0);
   79.97 +}
   79.98 +
   79.99 +png_byte PNGAPI
  79.100 +png_get_interlace_type(png_structp png_ptr, png_infop info_ptr)
  79.101 +{
  79.102 +   if (png_ptr != NULL && info_ptr != NULL)
  79.103 +   {
  79.104 +      return info_ptr->interlace_type;
  79.105 +   }
  79.106 +   return (0);
  79.107 +}
  79.108 +
  79.109 +png_byte PNGAPI
  79.110 +png_get_compression_type(png_structp png_ptr, png_infop info_ptr)
  79.111 +{
  79.112 +   if (png_ptr != NULL && info_ptr != NULL)
  79.113 +   {
  79.114 +      return info_ptr->compression_type;
  79.115 +   }
  79.116 +   return (0);
  79.117 +}
  79.118 +
  79.119 +png_uint_32 PNGAPI
  79.120 +png_get_x_pixels_per_meter(png_structp png_ptr, png_infop info_ptr)
  79.121 +{
  79.122 +   if (png_ptr != NULL && info_ptr != NULL)
  79.123 +#if defined(PNG_pHYs_SUPPORTED)
  79.124 +   if (info_ptr->valid & PNG_INFO_pHYs)
  79.125 +   {
  79.126 +      png_debug1(1, "in %s retrieval function\n", "png_get_x_pixels_per_meter");
  79.127 +      if (info_ptr->phys_unit_type != PNG_RESOLUTION_METER)
  79.128 +          return (0);
  79.129 +      else return (info_ptr->x_pixels_per_unit);
  79.130 +   }
  79.131 +#else
  79.132 +   return (0);
  79.133 +#endif
  79.134 +   return (0);
  79.135 +}
  79.136 +
  79.137 +png_uint_32 PNGAPI
  79.138 +png_get_y_pixels_per_meter(png_structp png_ptr, png_infop info_ptr)
  79.139 +{
  79.140 +   if (png_ptr != NULL && info_ptr != NULL)
  79.141 +#if defined(PNG_pHYs_SUPPORTED)
  79.142 +   if (info_ptr->valid & PNG_INFO_pHYs)
  79.143 +   {
  79.144 +      png_debug1(1, "in %s retrieval function\n", "png_get_y_pixels_per_meter");
  79.145 +      if (info_ptr->phys_unit_type != PNG_RESOLUTION_METER)
  79.146 +          return (0);
  79.147 +      else return (info_ptr->y_pixels_per_unit);
  79.148 +   }
  79.149 +#else
  79.150 +   return (0);
  79.151 +#endif
  79.152 +   return (0);
  79.153 +}
  79.154 +
  79.155 +png_uint_32 PNGAPI
  79.156 +png_get_pixels_per_meter(png_structp png_ptr, png_infop info_ptr)
  79.157 +{
  79.158 +   if (png_ptr != NULL && info_ptr != NULL)
  79.159 +#if defined(PNG_pHYs_SUPPORTED)
  79.160 +   if (info_ptr->valid & PNG_INFO_pHYs)
  79.161 +   {
  79.162 +      png_debug1(1, "in %s retrieval function\n", "png_get_pixels_per_meter");
  79.163 +      if (info_ptr->phys_unit_type != PNG_RESOLUTION_METER ||
  79.164 +         info_ptr->x_pixels_per_unit != info_ptr->y_pixels_per_unit)
  79.165 +          return (0);
  79.166 +      else return (info_ptr->x_pixels_per_unit);
  79.167 +   }
  79.168 +#else
  79.169 +   return (0);
  79.170 +#endif
  79.171 +   return (0);
  79.172 +}
  79.173 +
  79.174 +#ifdef PNG_FLOATING_POINT_SUPPORTED
  79.175 +float PNGAPI
  79.176 +png_get_pixel_aspect_ratio(png_structp png_ptr, png_infop info_ptr)
  79.177 +   {
  79.178 +   if (png_ptr != NULL && info_ptr != NULL)
  79.179 +#if defined(PNG_pHYs_SUPPORTED)
  79.180 +   if (info_ptr->valid & PNG_INFO_pHYs)
  79.181 +   {
  79.182 +      png_debug1(1, "in %s retrieval function\n", "png_get_aspect_ratio");
  79.183 +      if (info_ptr->x_pixels_per_unit == 0)
  79.184 +         return ((float)0.0);
  79.185 +      else
  79.186 +         return ((float)((float)info_ptr->y_pixels_per_unit
  79.187 +            /(float)info_ptr->x_pixels_per_unit));
  79.188 +   }
  79.189 +#else
  79.190 +   return (0.0);
  79.191 +#endif
  79.192 +   return ((float)0.0);
  79.193 +}
  79.194 +#endif
  79.195 +
  79.196 +png_int_32 PNGAPI
  79.197 +png_get_x_offset_microns(png_structp png_ptr, png_infop info_ptr)
  79.198 +{
  79.199 +   if (png_ptr != NULL && info_ptr != NULL)
  79.200 +#if defined(PNG_oFFs_SUPPORTED)
  79.201 +   if (info_ptr->valid & PNG_INFO_oFFs)
  79.202 +   {
  79.203 +      png_debug1(1, "in %s retrieval function\n", "png_get_x_offset_microns");
  79.204 +      if (info_ptr->offset_unit_type != PNG_OFFSET_MICROMETER)
  79.205 +          return (0);
  79.206 +      else return (info_ptr->x_offset);
  79.207 +   }
  79.208 +#else
  79.209 +   return (0);
  79.210 +#endif
  79.211 +   return (0);
  79.212 +}
  79.213 +
  79.214 +png_int_32 PNGAPI
  79.215 +png_get_y_offset_microns(png_structp png_ptr, png_infop info_ptr)
  79.216 +{
  79.217 +   if (png_ptr != NULL && info_ptr != NULL)
  79.218 +#if defined(PNG_oFFs_SUPPORTED)
  79.219 +   if (info_ptr->valid & PNG_INFO_oFFs)
  79.220 +   {
  79.221 +      png_debug1(1, "in %s retrieval function\n", "png_get_y_offset_microns");
  79.222 +      if (info_ptr->offset_unit_type != PNG_OFFSET_MICROMETER)
  79.223 +          return (0);
  79.224 +      else return (info_ptr->y_offset);
  79.225 +   }
  79.226 +#else
  79.227 +   return (0);
  79.228 +#endif
  79.229 +   return (0);
  79.230 +}
  79.231 +
  79.232 +png_int_32 PNGAPI
  79.233 +png_get_x_offset_pixels(png_structp png_ptr, png_infop info_ptr)
  79.234 +{
  79.235 +   if (png_ptr != NULL && info_ptr != NULL)
  79.236 +#if defined(PNG_oFFs_SUPPORTED)
  79.237 +   if (info_ptr->valid & PNG_INFO_oFFs)
  79.238 +   {
  79.239 +      png_debug1(1, "in %s retrieval function\n", "png_get_x_offset_microns");
  79.240 +      if (info_ptr->offset_unit_type != PNG_OFFSET_PIXEL)
  79.241 +          return (0);
  79.242 +      else return (info_ptr->x_offset);
  79.243 +   }
  79.244 +#else
  79.245 +   return (0);
  79.246 +#endif
  79.247 +   return (0);
  79.248 +}
  79.249 +
  79.250 +png_int_32 PNGAPI
  79.251 +png_get_y_offset_pixels(png_structp png_ptr, png_infop info_ptr)
  79.252 +{
  79.253 +   if (png_ptr != NULL && info_ptr != NULL)
  79.254 +#if defined(PNG_oFFs_SUPPORTED)
  79.255 +   if (info_ptr->valid & PNG_INFO_oFFs)
  79.256 +   {
  79.257 +      png_debug1(1, "in %s retrieval function\n", "png_get_y_offset_microns");
  79.258 +      if (info_ptr->offset_unit_type != PNG_OFFSET_PIXEL)
  79.259 +          return (0);
  79.260 +      else return (info_ptr->y_offset);
  79.261 +   }
  79.262 +#else
  79.263 +   return (0);
  79.264 +#endif
  79.265 +   return (0);
  79.266 +}
  79.267 +
  79.268 +#if defined(PNG_INCH_CONVERSIONS) && defined(PNG_FLOATING_POINT_SUPPORTED)
  79.269 +png_uint_32 PNGAPI
  79.270 +png_get_pixels_per_inch(png_structp png_ptr, png_infop info_ptr)
  79.271 +{
  79.272 +   return ((png_uint_32)((float)png_get_pixels_per_meter(png_ptr, info_ptr)
  79.273 +     *.0254 +.5));
  79.274 +}
  79.275 +
  79.276 +png_uint_32 PNGAPI
  79.277 +png_get_x_pixels_per_inch(png_structp png_ptr, png_infop info_ptr)
  79.278 +{
  79.279 +   return ((png_uint_32)((float)png_get_x_pixels_per_meter(png_ptr, info_ptr)
  79.280 +     *.0254 +.5));
  79.281 +}
  79.282 +
  79.283 +png_uint_32 PNGAPI
  79.284 +png_get_y_pixels_per_inch(png_structp png_ptr, png_infop info_ptr)
  79.285 +{
  79.286 +   return ((png_uint_32)((float)png_get_y_pixels_per_meter(png_ptr, info_ptr)
  79.287 +     *.0254 +.5));
  79.288 +}
  79.289 +
  79.290 +float PNGAPI
  79.291 +png_get_x_offset_inches(png_structp png_ptr, png_infop info_ptr)
  79.292 +{
  79.293 +   return ((float)png_get_x_offset_microns(png_ptr, info_ptr)
  79.294 +     *.00003937);
  79.295 +}
  79.296 +
  79.297 +float PNGAPI
  79.298 +png_get_y_offset_inches(png_structp png_ptr, png_infop info_ptr)
  79.299 +{
  79.300 +   return ((float)png_get_y_offset_microns(png_ptr, info_ptr)
  79.301 +     *.00003937);
  79.302 +}
  79.303 +
  79.304 +#if defined(PNG_pHYs_SUPPORTED)
  79.305 +png_uint_32 PNGAPI
  79.306 +png_get_pHYs_dpi(png_structp png_ptr, png_infop info_ptr,
  79.307 +   png_uint_32 *res_x, png_uint_32 *res_y, int *unit_type)
  79.308 +{
  79.309 +   png_uint_32 retval = 0;
  79.310 +
  79.311 +   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_pHYs))
  79.312 +   {
  79.313 +      png_debug1(1, "in %s retrieval function\n", "pHYs");
  79.314 +      if (res_x != NULL)
  79.315 +      {
  79.316 +         *res_x = info_ptr->x_pixels_per_unit;
  79.317 +         retval |= PNG_INFO_pHYs;
  79.318 +      }
  79.319 +      if (res_y != NULL)
  79.320 +      {
  79.321 +         *res_y = info_ptr->y_pixels_per_unit;
  79.322 +         retval |= PNG_INFO_pHYs;
  79.323 +      }
  79.324 +      if (unit_type != NULL)
  79.325 +      {
  79.326 +         *unit_type = (int)info_ptr->phys_unit_type;
  79.327 +         retval |= PNG_INFO_pHYs;
  79.328 +         if (*unit_type == 1)
  79.329 +         {
  79.330 +            if (res_x != NULL) *res_x = (png_uint_32)(*res_x * .0254 + .50);
  79.331 +            if (res_y != NULL) *res_y = (png_uint_32)(*res_y * .0254 + .50);
  79.332 +         }
  79.333 +      }
  79.334 +   }
  79.335 +   return (retval);
  79.336 +}
  79.337 +#endif /* PNG_pHYs_SUPPORTED */
  79.338 +#endif  /* PNG_INCH_CONVERSIONS && PNG_FLOATING_POINT_SUPPORTED */
  79.339 +
  79.340 +/* png_get_channels really belongs in here, too, but it's been around longer */
  79.341 +
  79.342 +#endif  /* PNG_EASY_ACCESS_SUPPORTED */
  79.343 +
  79.344 +png_byte PNGAPI
  79.345 +png_get_channels(png_structp png_ptr, png_infop info_ptr)
  79.346 +{
  79.347 +   if (png_ptr != NULL && info_ptr != NULL)
  79.348 +      return(info_ptr->channels);
  79.349 +   else
  79.350 +      return (0);
  79.351 +}
  79.352 +
  79.353 +png_bytep PNGAPI
  79.354 +png_get_signature(png_structp png_ptr, png_infop info_ptr)
  79.355 +{
  79.356 +   if (png_ptr != NULL && info_ptr != NULL)
  79.357 +      return(info_ptr->signature);
  79.358 +   else
  79.359 +      return (NULL);
  79.360 +}
  79.361 +
  79.362 +#if defined(PNG_bKGD_SUPPORTED)
  79.363 +png_uint_32 PNGAPI
  79.364 +png_get_bKGD(png_structp png_ptr, png_infop info_ptr,
  79.365 +   png_color_16p *background)
  79.366 +{
  79.367 +   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_bKGD)
  79.368 +      && background != NULL)
  79.369 +   {
  79.370 +      png_debug1(1, "in %s retrieval function\n", "bKGD");
  79.371 +      *background = &(info_ptr->background);
  79.372 +      return (PNG_INFO_bKGD);
  79.373 +   }
  79.374 +   return (0);
  79.375 +}
  79.376 +#endif
  79.377 +
  79.378 +#if defined(PNG_cHRM_SUPPORTED)
  79.379 +#ifdef PNG_FLOATING_POINT_SUPPORTED
  79.380 +png_uint_32 PNGAPI
  79.381 +png_get_cHRM(png_structp png_ptr, png_infop info_ptr,
  79.382 +   double *white_x, double *white_y, double *red_x, double *red_y,
  79.383 +   double *green_x, double *green_y, double *blue_x, double *blue_y)
  79.384 +{
  79.385 +   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_cHRM))
  79.386 +   {
  79.387 +      png_debug1(1, "in %s retrieval function\n", "cHRM");
  79.388 +      if (white_x != NULL)
  79.389 +         *white_x = (double)info_ptr->x_white;
  79.390 +      if (white_y != NULL)
  79.391 +         *white_y = (double)info_ptr->y_white;
  79.392 +      if (red_x != NULL)
  79.393 +         *red_x = (double)info_ptr->x_red;
  79.394 +      if (red_y != NULL)
  79.395 +         *red_y = (double)info_ptr->y_red;
  79.396 +      if (green_x != NULL)
  79.397 +         *green_x = (double)info_ptr->x_green;
  79.398 +      if (green_y != NULL)
  79.399 +         *green_y = (double)info_ptr->y_green;
  79.400 +      if (blue_x != NULL)
  79.401 +         *blue_x = (double)info_ptr->x_blue;
  79.402 +      if (blue_y != NULL)
  79.403 +         *blue_y = (double)info_ptr->y_blue;
  79.404 +      return (PNG_INFO_cHRM);
  79.405 +   }
  79.406 +   return (0);
  79.407 +}
  79.408 +#endif
  79.409 +#ifdef PNG_FIXED_POINT_SUPPORTED
  79.410 +png_uint_32 PNGAPI
  79.411 +png_get_cHRM_fixed(png_structp png_ptr, png_infop info_ptr,
  79.412 +   png_fixed_point *white_x, png_fixed_point *white_y, png_fixed_point *red_x,
  79.413 +   png_fixed_point *red_y, png_fixed_point *green_x, png_fixed_point *green_y,
  79.414 +   png_fixed_point *blue_x, png_fixed_point *blue_y)
  79.415 +{
  79.416 +   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_cHRM))
  79.417 +   {
  79.418 +      png_debug1(1, "in %s retrieval function\n", "cHRM");
  79.419 +      if (white_x != NULL)
  79.420 +         *white_x = info_ptr->int_x_white;
  79.421 +      if (white_y != NULL)
  79.422 +         *white_y = info_ptr->int_y_white;
  79.423 +      if (red_x != NULL)
  79.424 +         *red_x = info_ptr->int_x_red;
  79.425 +      if (red_y != NULL)
  79.426 +         *red_y = info_ptr->int_y_red;
  79.427 +      if (green_x != NULL)
  79.428 +         *green_x = info_ptr->int_x_green;
  79.429 +      if (green_y != NULL)
  79.430 +         *green_y = info_ptr->int_y_green;
  79.431 +      if (blue_x != NULL)
  79.432 +         *blue_x = info_ptr->int_x_blue;
  79.433 +      if (blue_y != NULL)
  79.434 +         *blue_y = info_ptr->int_y_blue;
  79.435 +      return (PNG_INFO_cHRM);
  79.436 +   }
  79.437 +   return (0);
  79.438 +}
  79.439 +#endif
  79.440 +#endif
  79.441 +
  79.442 +#if defined(PNG_gAMA_SUPPORTED)
  79.443 +#ifdef PNG_FLOATING_POINT_SUPPORTED
  79.444 +png_uint_32 PNGAPI
  79.445 +png_get_gAMA(png_structp png_ptr, png_infop info_ptr, double *file_gamma)
  79.446 +{
  79.447 +   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_gAMA)
  79.448 +      && file_gamma != NULL)
  79.449 +   {
  79.450 +      png_debug1(1, "in %s retrieval function\n", "gAMA");
  79.451 +      *file_gamma = (double)info_ptr->gamma;
  79.452 +      return (PNG_INFO_gAMA);
  79.453 +   }
  79.454 +   return (0);
  79.455 +}
  79.456 +#endif
  79.457 +#ifdef PNG_FIXED_POINT_SUPPORTED
  79.458 +png_uint_32 PNGAPI
  79.459 +png_get_gAMA_fixed(png_structp png_ptr, png_infop info_ptr,
  79.460 +    png_fixed_point *int_file_gamma)
  79.461 +{
  79.462 +   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_gAMA)
  79.463 +      && int_file_gamma != NULL)
  79.464 +   {
  79.465 +      png_debug1(1, "in %s retrieval function\n", "gAMA");
  79.466 +      *int_file_gamma = info_ptr->int_gamma;
  79.467 +      return (PNG_INFO_gAMA);
  79.468 +   }
  79.469 +   return (0);
  79.470 +}
  79.471 +#endif
  79.472 +#endif
  79.473 +
  79.474 +#if defined(PNG_sRGB_SUPPORTED)
  79.475 +png_uint_32 PNGAPI
  79.476 +png_get_sRGB(png_structp png_ptr, png_infop info_ptr, int *file_srgb_intent)
  79.477 +{
  79.478 +   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_sRGB)
  79.479 +      && file_srgb_intent != NULL)
  79.480 +   {
  79.481 +      png_debug1(1, "in %s retrieval function\n", "sRGB");
  79.482 +      *file_srgb_intent = (int)info_ptr->srgb_intent;
  79.483 +      return (PNG_INFO_sRGB);
  79.484 +   }
  79.485 +   return (0);
  79.486 +}
  79.487 +#endif
  79.488 +
  79.489 +#if defined(PNG_iCCP_SUPPORTED)
  79.490 +png_uint_32 PNGAPI
  79.491 +png_get_iCCP(png_structp png_ptr, png_infop info_ptr,
  79.492 +             png_charpp name, int *compression_type,
  79.493 +             png_charpp profile, png_uint_32 *proflen)
  79.494 +{
  79.495 +   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_iCCP)
  79.496 +      && name != NULL && profile != NULL && proflen != NULL)
  79.497 +   {
  79.498 +      png_debug1(1, "in %s retrieval function\n", "iCCP");
  79.499 +      *name = info_ptr->iccp_name;
  79.500 +      *profile = info_ptr->iccp_profile;
  79.501 +      /* compression_type is a dummy so the API won't have to change
  79.502 +         if we introduce multiple compression types later. */
  79.503 +      *proflen = (int)info_ptr->iccp_proflen;
  79.504 +      *compression_type = (int)info_ptr->iccp_compression;
  79.505 +      return (PNG_INFO_iCCP);
  79.506 +   }
  79.507 +   return (0);
  79.508 +}
  79.509 +#endif
  79.510 +
  79.511 +#if defined(PNG_sPLT_SUPPORTED)
  79.512 +png_uint_32 PNGAPI
  79.513 +png_get_sPLT(png_structp png_ptr, png_infop info_ptr,
  79.514 +             png_sPLT_tpp spalettes)
  79.515 +{
  79.516 +   if (png_ptr != NULL && info_ptr != NULL && spalettes != NULL)
  79.517 +   {
  79.518 +     *spalettes = info_ptr->splt_palettes;
  79.519 +     return ((png_uint_32)info_ptr->splt_palettes_num);
  79.520 +   }
  79.521 +   return (0);
  79.522 +}
  79.523 +#endif
  79.524 +
  79.525 +#if defined(PNG_hIST_SUPPORTED)
  79.526 +png_uint_32 PNGAPI
  79.527 +png_get_hIST(png_structp png_ptr, png_infop info_ptr, png_uint_16p *hist)
  79.528 +{
  79.529 +   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_hIST)
  79.530 +      && hist != NULL)
  79.531 +   {
  79.532 +      png_debug1(1, "in %s retrieval function\n", "hIST");
  79.533 +      *hist = info_ptr->hist;
  79.534 +      return (PNG_INFO_hIST);
  79.535 +   }
  79.536 +   return (0);
  79.537 +}
  79.538 +#endif
  79.539 +
  79.540 +png_uint_32 PNGAPI
  79.541 +png_get_IHDR(png_structp png_ptr, png_infop info_ptr,
  79.542 +   png_uint_32 *width, png_uint_32 *height, int *bit_depth,
  79.543 +   int *color_type, int *interlace_type, int *compression_type,
  79.544 +   int *filter_type)
  79.545 +
  79.546 +{
  79.547 +   if (png_ptr != NULL && info_ptr != NULL && width != NULL && height != NULL &&
  79.548 +      bit_depth != NULL && color_type != NULL)
  79.549 +   {
  79.550 +      png_debug1(1, "in %s retrieval function\n", "IHDR");
  79.551 +      *width = info_ptr->width;
  79.552 +      *height = info_ptr->height;
  79.553 +      *bit_depth = info_ptr->bit_depth;
  79.554 +      if (info_ptr->bit_depth < 1 || info_ptr->bit_depth > 16)
  79.555 +        png_error(png_ptr, "Invalid bit depth");
  79.556 +      *color_type = info_ptr->color_type;
  79.557 +      if (info_ptr->color_type > 6)
  79.558 +        png_error(png_ptr, "Invalid color type");
  79.559 +      if (compression_type != NULL)
  79.560 +         *compression_type = info_ptr->compression_type;
  79.561 +      if (filter_type != NULL)
  79.562 +         *filter_type = info_ptr->filter_type;
  79.563 +      if (interlace_type != NULL)
  79.564 +         *interlace_type = info_ptr->interlace_type;
  79.565 +
  79.566 +      /* check for potential overflow of rowbytes */
  79.567 +      if (*width == 0 || *width > PNG_UINT_31_MAX)
  79.568 +        png_error(png_ptr, "Invalid image width");
  79.569 +      if (*height == 0 || *height > PNG_UINT_31_MAX)
  79.570 +        png_error(png_ptr, "Invalid image height");
  79.571 +      if (info_ptr->width > (PNG_UINT_32_MAX
  79.572 +                 >> 3)      /* 8-byte RGBA pixels */
  79.573 +                 - 64       /* bigrowbuf hack */
  79.574 +                 - 1        /* filter byte */
  79.575 +                 - 7*8      /* rounding of width to multiple of 8 pixels */
  79.576 +                 - 8)       /* extra max_pixel_depth pad */
  79.577 +      {
  79.578 +         png_warning(png_ptr,
  79.579 +            "Width too large for libpng to process image data.");
  79.580 +      }
  79.581 +      return (1);
  79.582 +   }
  79.583 +   return (0);
  79.584 +}
  79.585 +
  79.586 +#if defined(PNG_oFFs_SUPPORTED)
  79.587 +png_uint_32 PNGAPI
  79.588 +png_get_oFFs(png_structp png_ptr, png_infop info_ptr,
  79.589 +   png_int_32 *offset_x, png_int_32 *offset_y, int *unit_type)
  79.590 +{
  79.591 +   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_oFFs)
  79.592 +      && offset_x != NULL && offset_y != NULL && unit_type != NULL)
  79.593 +   {
  79.594 +      png_debug1(1, "in %s retrieval function\n", "oFFs");
  79.595 +      *offset_x = info_ptr->x_offset;
  79.596 +      *offset_y = info_ptr->y_offset;
  79.597 +      *unit_type = (int)info_ptr->offset_unit_type;
  79.598 +      return (PNG_INFO_oFFs);
  79.599 +   }
  79.600 +   return (0);
  79.601 +}
  79.602 +#endif
  79.603 +
  79.604 +#if defined(PNG_pCAL_SUPPORTED)
  79.605 +png_uint_32 PNGAPI
  79.606 +png_get_pCAL(png_structp png_ptr, png_infop info_ptr,
  79.607 +   png_charp *purpose, png_int_32 *X0, png_int_32 *X1, int *type, int *nparams,
  79.608 +   png_charp *units, png_charpp *params)
  79.609 +{
  79.610 +   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_pCAL)
  79.611 +      && purpose != NULL && X0 != NULL && X1 != NULL && type != NULL &&
  79.612 +      nparams != NULL && units != NULL && params != NULL)
  79.613 +   {
  79.614 +      png_debug1(1, "in %s retrieval function\n", "pCAL");
  79.615 +      *purpose = info_ptr->pcal_purpose;
  79.616 +      *X0 = info_ptr->pcal_X0;
  79.617 +      *X1 = info_ptr->pcal_X1;
  79.618 +      *type = (int)info_ptr->pcal_type;
  79.619 +      *nparams = (int)info_ptr->pcal_nparams;
  79.620 +      *units = info_ptr->pcal_units;
  79.621 +      *params = info_ptr->pcal_params;
  79.622 +      return (PNG_INFO_pCAL);
  79.623 +   }
  79.624 +   return (0);
  79.625 +}
  79.626 +#endif
  79.627 +
  79.628 +#if defined(PNG_sCAL_SUPPORTED)
  79.629 +#ifdef PNG_FLOATING_POINT_SUPPORTED
  79.630 +png_uint_32 PNGAPI
  79.631 +png_get_sCAL(png_structp png_ptr, png_infop info_ptr,
  79.632 +             int *unit, double *width, double *height)
  79.633 +{
  79.634 +    if (png_ptr != NULL && info_ptr != NULL &&
  79.635 +       (info_ptr->valid & PNG_INFO_sCAL))
  79.636 +    {
  79.637 +        *unit = info_ptr->scal_unit;
  79.638 +        *width = info_ptr->scal_pixel_width;
  79.639 +        *height = info_ptr->scal_pixel_height;
  79.640 +        return (PNG_INFO_sCAL);
  79.641 +    }
  79.642 +    return(0);
  79.643 +}
  79.644 +#else
  79.645 +#ifdef PNG_FIXED_POINT_SUPPORTED
  79.646 +png_uint_32 PNGAPI
  79.647 +png_get_sCAL_s(png_structp png_ptr, png_infop info_ptr,
  79.648 +             int *unit, png_charpp width, png_charpp height)
  79.649 +{
  79.650 +    if (png_ptr != NULL && info_ptr != NULL &&
  79.651 +       (info_ptr->valid & PNG_INFO_sCAL))
  79.652 +    {
  79.653 +        *unit = info_ptr->scal_unit;
  79.654 +        *width = info_ptr->scal_s_width;
  79.655 +        *height = info_ptr->scal_s_height;
  79.656 +        return (PNG_INFO_sCAL);
  79.657 +    }
  79.658 +    return(0);
  79.659 +}
  79.660 +#endif
  79.661 +#endif
  79.662 +#endif
  79.663 +
  79.664 +#if defined(PNG_pHYs_SUPPORTED)
  79.665 +png_uint_32 PNGAPI
  79.666 +png_get_pHYs(png_structp png_ptr, png_infop info_ptr,
  79.667 +   png_uint_32 *res_x, png_uint_32 *res_y, int *unit_type)
  79.668 +{
  79.669 +   png_uint_32 retval = 0;
  79.670 +
  79.671 +   if (png_ptr != NULL && info_ptr != NULL &&
  79.672 +      (info_ptr->valid & PNG_INFO_pHYs))
  79.673 +   {
  79.674 +      png_debug1(1, "in %s retrieval function\n", "pHYs");
  79.675 +      if (res_x != NULL)
  79.676 +      {
  79.677 +         *res_x = info_ptr->x_pixels_per_unit;
  79.678 +         retval |= PNG_INFO_pHYs;
  79.679 +      }
  79.680 +      if (res_y != NULL)
  79.681 +      {
  79.682 +         *res_y = info_ptr->y_pixels_per_unit;
  79.683 +         retval |= PNG_INFO_pHYs;
  79.684 +      }
  79.685 +      if (unit_type != NULL)
  79.686 +      {
  79.687 +         *unit_type = (int)info_ptr->phys_unit_type;
  79.688 +         retval |= PNG_INFO_pHYs;
  79.689 +      }
  79.690 +   }
  79.691 +   return (retval);
  79.692 +}
  79.693 +#endif
  79.694 +
  79.695 +png_uint_32 PNGAPI
  79.696 +png_get_PLTE(png_structp png_ptr, png_infop info_ptr, png_colorp *palette,
  79.697 +   int *num_palette)
  79.698 +{
  79.699 +   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_PLTE)
  79.700 +       && palette != NULL)
  79.701 +   {
  79.702 +      png_debug1(1, "in %s retrieval function\n", "PLTE");
  79.703 +      *palette = info_ptr->palette;
  79.704 +      *num_palette = info_ptr->num_palette;
  79.705 +      png_debug1(3, "num_palette = %d\n", *num_palette);
  79.706 +      return (PNG_INFO_PLTE);
  79.707 +   }
  79.708 +   return (0);
  79.709 +}
  79.710 +
  79.711 +#if defined(PNG_sBIT_SUPPORTED)
  79.712 +png_uint_32 PNGAPI
  79.713 +png_get_sBIT(png_structp png_ptr, png_infop info_ptr, png_color_8p *sig_bit)
  79.714 +{
  79.715 +   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_sBIT)
  79.716 +      && sig_bit != NULL)
  79.717 +   {
  79.718 +      png_debug1(1, "in %s retrieval function\n", "sBIT");
  79.719 +      *sig_bit = &(info_ptr->sig_bit);
  79.720 +      return (PNG_INFO_sBIT);
  79.721 +   }
  79.722 +   return (0);
  79.723 +}
  79.724 +#endif
  79.725 +
  79.726 +#if defined(PNG_TEXT_SUPPORTED)
  79.727 +png_uint_32 PNGAPI
  79.728 +png_get_text(png_structp png_ptr, png_infop info_ptr, png_textp *text_ptr,
  79.729 +   int *num_text)
  79.730 +{
  79.731 +   if (png_ptr != NULL && info_ptr != NULL && info_ptr->num_text > 0)
  79.732 +   {
  79.733 +      png_debug1(1, "in %s retrieval function\n",
  79.734 +         (png_ptr->chunk_name[0] == '\0' ? "text"
  79.735 +             : (png_const_charp)png_ptr->chunk_name));
  79.736 +      if (text_ptr != NULL)
  79.737 +         *text_ptr = info_ptr->text;
  79.738 +      if (num_text != NULL)
  79.739 +         *num_text = info_ptr->num_text;
  79.740 +      return ((png_uint_32)info_ptr->num_text);
  79.741 +   }
  79.742 +   if (num_text != NULL)
  79.743 +     *num_text = 0;
  79.744 +   return(0);
  79.745 +}
  79.746 +#endif
  79.747 +
  79.748 +#if defined(PNG_tIME_SUPPORTED)
  79.749 +png_uint_32 PNGAPI
  79.750 +png_get_tIME(png_structp png_ptr, png_infop info_ptr, png_timep *mod_time)
  79.751 +{
  79.752 +   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_tIME)
  79.753 +       && mod_time != NULL)
  79.754 +   {
  79.755 +      png_debug1(1, "in %s retrieval function\n", "tIME");
  79.756 +      *mod_time = &(info_ptr->mod_time);
  79.757 +      return (PNG_INFO_tIME);
  79.758 +   }
  79.759 +   return (0);
  79.760 +}
  79.761 +#endif
  79.762 +
  79.763 +#if defined(PNG_tRNS_SUPPORTED)
  79.764 +png_uint_32 PNGAPI
  79.765 +png_get_tRNS(png_structp png_ptr, png_infop info_ptr,
  79.766 +   png_bytep *trans, int *num_trans, png_color_16p *trans_values)
  79.767 +{
  79.768 +   png_uint_32 retval = 0;
  79.769 +   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS))
  79.770 +   {
  79.771 +      png_debug1(1, "in %s retrieval function\n", "tRNS");
  79.772 +      if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  79.773 +      {
  79.774 +          if (trans != NULL)
  79.775 +          {
  79.776 +             *trans = info_ptr->trans;
  79.777 +             retval |= PNG_INFO_tRNS;
  79.778 +          }
  79.779 +          if (trans_values != NULL)
  79.780 +             *trans_values = &(info_ptr->trans_values);
  79.781 +      }
  79.782 +      else /* if (info_ptr->color_type != PNG_COLOR_TYPE_PALETTE) */
  79.783 +      {
  79.784 +          if (trans_values != NULL)
  79.785 +          {
  79.786 +             *trans_values = &(info_ptr->trans_values);
  79.787 +             retval |= PNG_INFO_tRNS;
  79.788 +          }
  79.789 +          if (trans != NULL)
  79.790 +             *trans = NULL;
  79.791 +      }
  79.792 +      if (num_trans != NULL)
  79.793 +      {
  79.794 +         *num_trans = info_ptr->num_trans;
  79.795 +         retval |= PNG_INFO_tRNS;
  79.796 +      }
  79.797 +   }
  79.798 +   return (retval);
  79.799 +}
  79.800 +#endif
  79.801 +
  79.802 +#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
  79.803 +png_uint_32 PNGAPI
  79.804 +png_get_unknown_chunks(png_structp png_ptr, png_infop info_ptr,
  79.805 +             png_unknown_chunkpp unknowns)
  79.806 +{
  79.807 +   if (png_ptr != NULL && info_ptr != NULL && unknowns != NULL)
  79.808 +   {
  79.809 +     *unknowns = info_ptr->unknown_chunks;
  79.810 +     return ((png_uint_32)info_ptr->unknown_chunks_num);
  79.811 +   }
  79.812 +   return (0);
  79.813 +}
  79.814 +#endif
  79.815 +
  79.816 +#if defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
  79.817 +png_byte PNGAPI
  79.818 +png_get_rgb_to_gray_status (png_structp png_ptr)
  79.819 +{
  79.820 +   return (png_byte)(png_ptr? png_ptr->rgb_to_gray_status : 0);
  79.821 +}
  79.822 +#endif
  79.823 +
  79.824 +#if defined(PNG_USER_CHUNKS_SUPPORTED)
  79.825 +png_voidp PNGAPI
  79.826 +png_get_user_chunk_ptr(png_structp png_ptr)
  79.827 +{
  79.828 +   return (png_ptr? png_ptr->user_chunk_ptr : NULL);
  79.829 +}
  79.830 +#endif
  79.831 +
  79.832 +#ifdef PNG_WRITE_SUPPORTED
  79.833 +png_uint_32 PNGAPI
  79.834 +png_get_compression_buffer_size(png_structp png_ptr)
  79.835 +{
  79.836 +   return (png_uint_32)(png_ptr? png_ptr->zbuf_size : 0L);
  79.837 +}
  79.838 +#endif
  79.839 +
  79.840 +#ifdef PNG_ASSEMBLER_CODE_SUPPORTED
  79.841 +#ifndef PNG_1_0_X
  79.842 +/* this function was added to libpng 1.2.0 and should exist by default */
  79.843 +png_uint_32 PNGAPI
  79.844 +png_get_asm_flags (png_structp png_ptr)
  79.845 +{
  79.846 +    /* obsolete, to be removed from libpng-1.4.0 */
  79.847 +    return (png_ptr? 0L: 0L);
  79.848 +}
  79.849 +
  79.850 +/* this function was added to libpng 1.2.0 and should exist by default */
  79.851 +png_uint_32 PNGAPI
  79.852 +png_get_asm_flagmask (int flag_select)
  79.853 +{
  79.854 +    /* obsolete, to be removed from libpng-1.4.0 */
  79.855 +    flag_select=flag_select;
  79.856 +    return 0L;
  79.857 +}
  79.858 +
  79.859 +    /* GRR:  could add this:   && defined(PNG_MMX_CODE_SUPPORTED) */
  79.860 +/* this function was added to libpng 1.2.0 */
  79.861 +png_uint_32 PNGAPI
  79.862 +png_get_mmx_flagmask (int flag_select, int *compilerID)
  79.863 +{
  79.864 +    /* obsolete, to be removed from libpng-1.4.0 */
  79.865 +    flag_select=flag_select;
  79.866 +    *compilerID = -1;   /* unknown (i.e., no asm/MMX code compiled) */
  79.867 +    return 0L;
  79.868 +}
  79.869 +
  79.870 +/* this function was added to libpng 1.2.0 */
  79.871 +png_byte PNGAPI
  79.872 +png_get_mmx_bitdepth_threshold (png_structp png_ptr)
  79.873 +{
  79.874 +    /* obsolete, to be removed from libpng-1.4.0 */
  79.875 +    return (png_ptr? 0: 0);
  79.876 +}
  79.877 +
  79.878 +/* this function was added to libpng 1.2.0 */
  79.879 +png_uint_32 PNGAPI
  79.880 +png_get_mmx_rowbytes_threshold (png_structp png_ptr)
  79.881 +{
  79.882 +    /* obsolete, to be removed from libpng-1.4.0 */
  79.883 +    return (png_ptr? 0L: 0L);
  79.884 +}
  79.885 +#endif /* ?PNG_1_0_X */
  79.886 +#endif /* ?PNG_ASSEMBLER_CODE_SUPPORTED */
  79.887 +
  79.888 +#ifdef PNG_SET_USER_LIMITS_SUPPORTED
  79.889 +/* these functions were added to libpng 1.2.6 */
  79.890 +png_uint_32 PNGAPI
  79.891 +png_get_user_width_max (png_structp png_ptr)
  79.892 +{
  79.893 +    return (png_ptr? png_ptr->user_width_max : 0);
  79.894 +}
  79.895 +png_uint_32 PNGAPI
  79.896 +png_get_user_height_max (png_structp png_ptr)
  79.897 +{
  79.898 +    return (png_ptr? png_ptr->user_height_max : 0);
  79.899 +}
  79.900 +#endif /* ?PNG_SET_USER_LIMITS_SUPPORTED */
  79.901 + 
  79.902 +
  79.903 +#endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */
    80.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    80.2 +++ b/libs/libpng/pngmem.c	Sat Sep 19 05:51:51 2015 +0300
    80.3 @@ -0,0 +1,609 @@
    80.4 +
    80.5 +/* pngmem.c - stub functions for memory allocation
    80.6 + *
    80.7 + * Last changed in libpng 1.2.30 [August 15, 2008]
    80.8 + * For conditions of distribution and use, see copyright notice in png.h
    80.9 + * Copyright (c) 1998-2008 Glenn Randers-Pehrson
   80.10 + * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
   80.11 + * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
   80.12 + *
   80.13 + * This file provides a location for all memory allocation.  Users who
   80.14 + * need special memory handling are expected to supply replacement
   80.15 + * functions for png_malloc() and png_free(), and to use
   80.16 + * png_create_read_struct_2() and png_create_write_struct_2() to
   80.17 + * identify the replacement functions.
   80.18 + */
   80.19 +
   80.20 +#define PNG_INTERNAL
   80.21 +#include "png.h"
   80.22 +#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
   80.23 +
   80.24 +/* Borland DOS special memory handler */
   80.25 +#if defined(__TURBOC__) && !defined(_Windows) && !defined(__FLAT__)
   80.26 +/* if you change this, be sure to change the one in png.h also */
   80.27 +
   80.28 +/* Allocate memory for a png_struct.  The malloc and memset can be replaced
   80.29 +   by a single call to calloc() if this is thought to improve performance. */
   80.30 +png_voidp /* PRIVATE */
   80.31 +png_create_struct(int type)
   80.32 +{
   80.33 +#ifdef PNG_USER_MEM_SUPPORTED
   80.34 +   return (png_create_struct_2(type, png_malloc_ptr_NULL, png_voidp_NULL));
   80.35 +}
   80.36 +
   80.37 +/* Alternate version of png_create_struct, for use with user-defined malloc. */
   80.38 +png_voidp /* PRIVATE */
   80.39 +png_create_struct_2(int type, png_malloc_ptr malloc_fn, png_voidp mem_ptr)
   80.40 +{
   80.41 +#endif /* PNG_USER_MEM_SUPPORTED */
   80.42 +   png_size_t size;
   80.43 +   png_voidp struct_ptr;
   80.44 +
   80.45 +   if (type == PNG_STRUCT_INFO)
   80.46 +     size = png_sizeof(png_info);
   80.47 +   else if (type == PNG_STRUCT_PNG)
   80.48 +     size = png_sizeof(png_struct);
   80.49 +   else
   80.50 +     return (png_get_copyright(NULL));
   80.51 +
   80.52 +#ifdef PNG_USER_MEM_SUPPORTED
   80.53 +   if (malloc_fn != NULL)
   80.54 +   {
   80.55 +      png_struct dummy_struct;
   80.56 +      png_structp png_ptr = &dummy_struct;
   80.57 +      png_ptr->mem_ptr=mem_ptr;
   80.58 +      struct_ptr = (*(malloc_fn))(png_ptr, (png_uint_32)size);
   80.59 +   }
   80.60 +   else
   80.61 +#endif /* PNG_USER_MEM_SUPPORTED */
   80.62 +      struct_ptr = (png_voidp)farmalloc(size);
   80.63 +   if (struct_ptr != NULL)
   80.64 +      png_memset(struct_ptr, 0, size);
   80.65 +   return (struct_ptr);
   80.66 +}
   80.67 +
   80.68 +/* Free memory allocated by a png_create_struct() call */
   80.69 +void /* PRIVATE */
   80.70 +png_destroy_struct(png_voidp struct_ptr)
   80.71 +{
   80.72 +#ifdef PNG_USER_MEM_SUPPORTED
   80.73 +   png_destroy_struct_2(struct_ptr, png_free_ptr_NULL, png_voidp_NULL);
   80.74 +}
   80.75 +
   80.76 +/* Free memory allocated by a png_create_struct() call */
   80.77 +void /* PRIVATE */
   80.78 +png_destroy_struct_2(png_voidp struct_ptr, png_free_ptr free_fn,
   80.79 +    png_voidp mem_ptr)
   80.80 +{
   80.81 +#endif
   80.82 +   if (struct_ptr != NULL)
   80.83 +   {
   80.84 +#ifdef PNG_USER_MEM_SUPPORTED
   80.85 +      if (free_fn != NULL)
   80.86 +      {
   80.87 +         png_struct dummy_struct;
   80.88 +         png_structp png_ptr = &dummy_struct;
   80.89 +         png_ptr->mem_ptr=mem_ptr;
   80.90 +         (*(free_fn))(png_ptr, struct_ptr);
   80.91 +         return;
   80.92 +      }
   80.93 +#endif /* PNG_USER_MEM_SUPPORTED */
   80.94 +      farfree (struct_ptr);
   80.95 +   }
   80.96 +}
   80.97 +
   80.98 +/* Allocate memory.  For reasonable files, size should never exceed
   80.99 + * 64K.  However, zlib may allocate more then 64K if you don't tell
  80.100 + * it not to.  See zconf.h and png.h for more information. zlib does
  80.101 + * need to allocate exactly 64K, so whatever you call here must
  80.102 + * have the ability to do that.
  80.103 + *
  80.104 + * Borland seems to have a problem in DOS mode for exactly 64K.
  80.105 + * It gives you a segment with an offset of 8 (perhaps to store its
  80.106 + * memory stuff).  zlib doesn't like this at all, so we have to
  80.107 + * detect and deal with it.  This code should not be needed in
  80.108 + * Windows or OS/2 modes, and only in 16 bit mode.  This code has
  80.109 + * been updated by Alexander Lehmann for version 0.89 to waste less
  80.110 + * memory.
  80.111 + *
  80.112 + * Note that we can't use png_size_t for the "size" declaration,
  80.113 + * since on some systems a png_size_t is a 16-bit quantity, and as a
  80.114 + * result, we would be truncating potentially larger memory requests
  80.115 + * (which should cause a fatal error) and introducing major problems.
  80.116 + */
  80.117 +
  80.118 +png_voidp PNGAPI
  80.119 +png_malloc(png_structp png_ptr, png_uint_32 size)
  80.120 +{
  80.121 +   png_voidp ret;
  80.122 +
  80.123 +   if (png_ptr == NULL || size == 0)
  80.124 +      return (NULL);
  80.125 +
  80.126 +#ifdef PNG_USER_MEM_SUPPORTED
  80.127 +   if (png_ptr->malloc_fn != NULL)
  80.128 +       ret = ((png_voidp)(*(png_ptr->malloc_fn))(png_ptr, (png_size_t)size));
  80.129 +   else
  80.130 +       ret = (png_malloc_default(png_ptr, size));
  80.131 +   if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
  80.132 +       png_error(png_ptr, "Out of memory!");
  80.133 +   return (ret);
  80.134 +}
  80.135 +
  80.136 +png_voidp PNGAPI
  80.137 +png_malloc_default(png_structp png_ptr, png_uint_32 size)
  80.138 +{
  80.139 +   png_voidp ret;
  80.140 +#endif /* PNG_USER_MEM_SUPPORTED */
  80.141 +
  80.142 +   if (png_ptr == NULL || size == 0)
  80.143 +      return (NULL);
  80.144 +
  80.145 +#ifdef PNG_MAX_MALLOC_64K
  80.146 +   if (size > (png_uint_32)65536L)
  80.147 +   {
  80.148 +      png_warning(png_ptr, "Cannot Allocate > 64K");
  80.149 +      ret = NULL;
  80.150 +   }
  80.151 +   else
  80.152 +#endif
  80.153 +
  80.154 +   if (size != (size_t)size)
  80.155 +     ret = NULL;
  80.156 +   else if (size == (png_uint_32)65536L)
  80.157 +   {
  80.158 +      if (png_ptr->offset_table == NULL)
  80.159 +      {
  80.160 +         /* try to see if we need to do any of this fancy stuff */
  80.161 +         ret = farmalloc(size);
  80.162 +         if (ret == NULL || ((png_size_t)ret & 0xffff))
  80.163 +         {
  80.164 +            int num_blocks;
  80.165 +            png_uint_32 total_size;
  80.166 +            png_bytep table;
  80.167 +            int i;
  80.168 +            png_byte huge * hptr;
  80.169 +
  80.170 +            if (ret != NULL)
  80.171 +            {
  80.172 +               farfree(ret);
  80.173 +               ret = NULL;
  80.174 +            }
  80.175 +
  80.176 +            if (png_ptr->zlib_window_bits > 14)
  80.177 +               num_blocks = (int)(1 << (png_ptr->zlib_window_bits - 14));
  80.178 +            else
  80.179 +               num_blocks = 1;
  80.180 +            if (png_ptr->zlib_mem_level >= 7)
  80.181 +               num_blocks += (int)(1 << (png_ptr->zlib_mem_level - 7));
  80.182 +            else
  80.183 +               num_blocks++;
  80.184 +
  80.185 +            total_size = ((png_uint_32)65536L) * (png_uint_32)num_blocks+16;
  80.186 +
  80.187 +            table = farmalloc(total_size);
  80.188 +
  80.189 +            if (table == NULL)
  80.190 +            {
  80.191 +#ifndef PNG_USER_MEM_SUPPORTED
  80.192 +               if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
  80.193 +                  png_error(png_ptr, "Out Of Memory."); /* Note "O" and "M" */
  80.194 +               else
  80.195 +                  png_warning(png_ptr, "Out Of Memory.");
  80.196 +#endif
  80.197 +               return (NULL);
  80.198 +            }
  80.199 +
  80.200 +            if ((png_size_t)table & 0xfff0)
  80.201 +            {
  80.202 +#ifndef PNG_USER_MEM_SUPPORTED
  80.203 +               if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
  80.204 +                  png_error(png_ptr,
  80.205 +                    "Farmalloc didn't return normalized pointer");
  80.206 +               else
  80.207 +                  png_warning(png_ptr,
  80.208 +                    "Farmalloc didn't return normalized pointer");
  80.209 +#endif
  80.210 +               return (NULL);
  80.211 +            }
  80.212 +
  80.213 +            png_ptr->offset_table = table;
  80.214 +            png_ptr->offset_table_ptr = farmalloc(num_blocks *
  80.215 +               png_sizeof(png_bytep));
  80.216 +
  80.217 +            if (png_ptr->offset_table_ptr == NULL)
  80.218 +            {
  80.219 +#ifndef PNG_USER_MEM_SUPPORTED
  80.220 +               if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
  80.221 +                  png_error(png_ptr, "Out Of memory."); /* Note "O" and "M" */
  80.222 +               else
  80.223 +                  png_warning(png_ptr, "Out Of memory.");
  80.224 +#endif
  80.225 +               return (NULL);
  80.226 +            }
  80.227 +
  80.228 +            hptr = (png_byte huge *)table;
  80.229 +            if ((png_size_t)hptr & 0xf)
  80.230 +            {
  80.231 +               hptr = (png_byte huge *)((long)(hptr) & 0xfffffff0L);
  80.232 +               hptr = hptr + 16L;  /* "hptr += 16L" fails on Turbo C++ 3.0 */
  80.233 +            }
  80.234 +            for (i = 0; i < num_blocks; i++)
  80.235 +            {
  80.236 +               png_ptr->offset_table_ptr[i] = (png_bytep)hptr;
  80.237 +               hptr = hptr + (png_uint_32)65536L;  /* "+=" fails on TC++3.0 */
  80.238 +            }
  80.239 +
  80.240 +            png_ptr->offset_table_number = num_blocks;
  80.241 +            png_ptr->offset_table_count = 0;
  80.242 +            png_ptr->offset_table_count_free = 0;
  80.243 +         }
  80.244 +      }
  80.245 +
  80.246 +      if (png_ptr->offset_table_count >= png_ptr->offset_table_number)
  80.247 +      {
  80.248 +#ifndef PNG_USER_MEM_SUPPORTED
  80.249 +         if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
  80.250 +            png_error(png_ptr, "Out of Memory."); /* Note "o" and "M" */
  80.251 +         else
  80.252 +            png_warning(png_ptr, "Out of Memory.");
  80.253 +#endif
  80.254 +         return (NULL);
  80.255 +      }
  80.256 +
  80.257 +      ret = png_ptr->offset_table_ptr[png_ptr->offset_table_count++];
  80.258 +   }
  80.259 +   else
  80.260 +      ret = farmalloc(size);
  80.261 +
  80.262 +#ifndef PNG_USER_MEM_SUPPORTED
  80.263 +   if (ret == NULL)
  80.264 +   {
  80.265 +      if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
  80.266 +         png_error(png_ptr, "Out of memory."); /* Note "o" and "m" */
  80.267 +      else
  80.268 +         png_warning(png_ptr, "Out of memory."); /* Note "o" and "m" */
  80.269 +   }
  80.270 +#endif
  80.271 +
  80.272 +   return (ret);
  80.273 +}
  80.274 +
  80.275 +/* free a pointer allocated by png_malloc().  In the default
  80.276 +   configuration, png_ptr is not used, but is passed in case it
  80.277 +   is needed.  If ptr is NULL, return without taking any action. */
  80.278 +
  80.279 +void PNGAPI
  80.280 +png_free(png_structp png_ptr, png_voidp ptr)
  80.281 +{
  80.282 +   if (png_ptr == NULL || ptr == NULL)
  80.283 +      return;
  80.284 +
  80.285 +#ifdef PNG_USER_MEM_SUPPORTED
  80.286 +   if (png_ptr->free_fn != NULL)
  80.287 +   {
  80.288 +      (*(png_ptr->free_fn))(png_ptr, ptr);
  80.289 +      return;
  80.290 +   }
  80.291 +   else png_free_default(png_ptr, ptr);
  80.292 +}
  80.293 +
  80.294 +void PNGAPI
  80.295 +png_free_default(png_structp png_ptr, png_voidp ptr)
  80.296 +{
  80.297 +#endif /* PNG_USER_MEM_SUPPORTED */
  80.298 +
  80.299 +   if (png_ptr == NULL || ptr == NULL) return;
  80.300 +
  80.301 +   if (png_ptr->offset_table != NULL)
  80.302 +   {
  80.303 +      int i;
  80.304 +
  80.305 +      for (i = 0; i < png_ptr->offset_table_count; i++)
  80.306 +      {
  80.307 +         if (ptr == png_ptr->offset_table_ptr[i])
  80.308 +         {
  80.309 +            ptr = NULL;
  80.310 +            png_ptr->offset_table_count_free++;
  80.311 +            break;
  80.312 +         }
  80.313 +      }
  80.314 +      if (png_ptr->offset_table_count_free == png_ptr->offset_table_count)
  80.315 +      {
  80.316 +         farfree(png_ptr->offset_table);
  80.317 +         farfree(png_ptr->offset_table_ptr);
  80.318 +         png_ptr->offset_table = NULL;
  80.319 +         png_ptr->offset_table_ptr = NULL;
  80.320 +      }
  80.321 +   }
  80.322 +
  80.323 +   if (ptr != NULL)
  80.324 +   {
  80.325 +      farfree(ptr);
  80.326 +   }
  80.327 +}
  80.328 +
  80.329 +#else /* Not the Borland DOS special memory handler */
  80.330 +
  80.331 +/* Allocate memory for a png_struct or a png_info.  The malloc and
  80.332 +   memset can be replaced by a single call to calloc() if this is thought
  80.333 +   to improve performance noticably. */
  80.334 +png_voidp /* PRIVATE */
  80.335 +png_create_struct(int type)
  80.336 +{
  80.337 +#ifdef PNG_USER_MEM_SUPPORTED
  80.338 +   return (png_create_struct_2(type, png_malloc_ptr_NULL, png_voidp_NULL));
  80.339 +}
  80.340 +
  80.341 +/* Allocate memory for a png_struct or a png_info.  The malloc and
  80.342 +   memset can be replaced by a single call to calloc() if this is thought
  80.343 +   to improve performance noticably. */
  80.344 +png_voidp /* PRIVATE */
  80.345 +png_create_struct_2(int type, png_malloc_ptr malloc_fn, png_voidp mem_ptr)
  80.346 +{
  80.347 +#endif /* PNG_USER_MEM_SUPPORTED */
  80.348 +   png_size_t size;
  80.349 +   png_voidp struct_ptr;
  80.350 +
  80.351 +   if (type == PNG_STRUCT_INFO)
  80.352 +      size = png_sizeof(png_info);
  80.353 +   else if (type == PNG_STRUCT_PNG)
  80.354 +      size = png_sizeof(png_struct);
  80.355 +   else
  80.356 +      return (NULL);
  80.357 +
  80.358 +#ifdef PNG_USER_MEM_SUPPORTED
  80.359 +   if (malloc_fn != NULL)
  80.360 +   {
  80.361 +      png_struct dummy_struct;
  80.362 +      png_structp png_ptr = &dummy_struct;
  80.363 +      png_ptr->mem_ptr=mem_ptr;
  80.364 +      struct_ptr = (*(malloc_fn))(png_ptr, size);
  80.365 +      if (struct_ptr != NULL)
  80.366 +         png_memset(struct_ptr, 0, size);
  80.367 +      return (struct_ptr);
  80.368 +   }
  80.369 +#endif /* PNG_USER_MEM_SUPPORTED */
  80.370 +
  80.371 +#if defined(__TURBOC__) && !defined(__FLAT__)
  80.372 +   struct_ptr = (png_voidp)farmalloc(size);
  80.373 +#else
  80.374 +# if defined(_MSC_VER) && defined(MAXSEG_64K)
  80.375 +   struct_ptr = (png_voidp)halloc(size, 1);
  80.376 +# else
  80.377 +   struct_ptr = (png_voidp)malloc(size);
  80.378 +# endif
  80.379 +#endif
  80.380 +   if (struct_ptr != NULL)
  80.381 +      png_memset(struct_ptr, 0, size);
  80.382 +
  80.383 +   return (struct_ptr);
  80.384 +}
  80.385 +
  80.386 +
  80.387 +/* Free memory allocated by a png_create_struct() call */
  80.388 +void /* PRIVATE */
  80.389 +png_destroy_struct(png_voidp struct_ptr)
  80.390 +{
  80.391 +#ifdef PNG_USER_MEM_SUPPORTED
  80.392 +   png_destroy_struct_2(struct_ptr, png_free_ptr_NULL, png_voidp_NULL);
  80.393 +}
  80.394 +
  80.395 +/* Free memory allocated by a png_create_struct() call */
  80.396 +void /* PRIVATE */
  80.397 +png_destroy_struct_2(png_voidp struct_ptr, png_free_ptr free_fn,
  80.398 +    png_voidp mem_ptr)
  80.399 +{
  80.400 +#endif /* PNG_USER_MEM_SUPPORTED */
  80.401 +   if (struct_ptr != NULL)
  80.402 +   {
  80.403 +#ifdef PNG_USER_MEM_SUPPORTED
  80.404 +      if (free_fn != NULL)
  80.405 +      {
  80.406 +         png_struct dummy_struct;
  80.407 +         png_structp png_ptr = &dummy_struct;
  80.408 +         png_ptr->mem_ptr=mem_ptr;
  80.409 +         (*(free_fn))(png_ptr, struct_ptr);
  80.410 +         return;
  80.411 +      }
  80.412 +#endif /* PNG_USER_MEM_SUPPORTED */
  80.413 +#if defined(__TURBOC__) && !defined(__FLAT__)
  80.414 +      farfree(struct_ptr);
  80.415 +#else
  80.416 +# if defined(_MSC_VER) && defined(MAXSEG_64K)
  80.417 +      hfree(struct_ptr);
  80.418 +# else
  80.419 +      free(struct_ptr);
  80.420 +# endif
  80.421 +#endif
  80.422 +   }
  80.423 +}
  80.424 +
  80.425 +/* Allocate memory.  For reasonable files, size should never exceed
  80.426 +   64K.  However, zlib may allocate more then 64K if you don't tell
  80.427 +   it not to.  See zconf.h and png.h for more information.  zlib does
  80.428 +   need to allocate exactly 64K, so whatever you call here must
  80.429 +   have the ability to do that. */
  80.430 +
  80.431 +png_voidp PNGAPI
  80.432 +png_malloc(png_structp png_ptr, png_uint_32 size)
  80.433 +{
  80.434 +   png_voidp ret;
  80.435 +
  80.436 +#ifdef PNG_USER_MEM_SUPPORTED
  80.437 +   if (png_ptr == NULL || size == 0)
  80.438 +      return (NULL);
  80.439 +
  80.440 +   if (png_ptr->malloc_fn != NULL)
  80.441 +       ret = ((png_voidp)(*(png_ptr->malloc_fn))(png_ptr, (png_size_t)size));
  80.442 +   else
  80.443 +       ret = (png_malloc_default(png_ptr, size));
  80.444 +   if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
  80.445 +       png_error(png_ptr, "Out of Memory!");
  80.446 +   return (ret);
  80.447 +}
  80.448 +
  80.449 +png_voidp PNGAPI
  80.450 +png_malloc_default(png_structp png_ptr, png_uint_32 size)
  80.451 +{
  80.452 +   png_voidp ret;
  80.453 +#endif /* PNG_USER_MEM_SUPPORTED */
  80.454 +
  80.455 +   if (png_ptr == NULL || size == 0)
  80.456 +      return (NULL);
  80.457 +
  80.458 +#ifdef PNG_MAX_MALLOC_64K
  80.459 +   if (size > (png_uint_32)65536L)
  80.460 +   {
  80.461 +#ifndef PNG_USER_MEM_SUPPORTED
  80.462 +      if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
  80.463 +         png_error(png_ptr, "Cannot Allocate > 64K");
  80.464 +      else
  80.465 +#endif
  80.466 +         return NULL;
  80.467 +   }
  80.468 +#endif
  80.469 +
  80.470 + /* Check for overflow */
  80.471 +#if defined(__TURBOC__) && !defined(__FLAT__)
  80.472 + if (size != (unsigned long)size)
  80.473 +   ret = NULL;
  80.474 + else
  80.475 +   ret = farmalloc(size);
  80.476 +#else
  80.477 +# if defined(_MSC_VER) && defined(MAXSEG_64K)
  80.478 + if (size != (unsigned long)size)
  80.479 +   ret = NULL;
  80.480 + else
  80.481 +   ret = halloc(size, 1);
  80.482 +# else
  80.483 + if (size != (size_t)size)
  80.484 +   ret = NULL;
  80.485 + else
  80.486 +   ret = malloc((size_t)size);
  80.487 +# endif
  80.488 +#endif
  80.489 +
  80.490 +#ifndef PNG_USER_MEM_SUPPORTED
  80.491 +   if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
  80.492 +      png_error(png_ptr, "Out of Memory");
  80.493 +#endif
  80.494 +
  80.495 +   return (ret);
  80.496 +}
  80.497 +
  80.498 +/* Free a pointer allocated by png_malloc().  If ptr is NULL, return
  80.499 +   without taking any action. */
  80.500 +void PNGAPI
  80.501 +png_free(png_structp png_ptr, png_voidp ptr)
  80.502 +{
  80.503 +   if (png_ptr == NULL || ptr == NULL)
  80.504 +      return;
  80.505 +
  80.506 +#ifdef PNG_USER_MEM_SUPPORTED
  80.507 +   if (png_ptr->free_fn != NULL)
  80.508 +   {
  80.509 +      (*(png_ptr->free_fn))(png_ptr, ptr);
  80.510 +      return;
  80.511 +   }
  80.512 +   else png_free_default(png_ptr, ptr);
  80.513 +}
  80.514 +void PNGAPI
  80.515 +png_free_default(png_structp png_ptr, png_voidp ptr)
  80.516 +{
  80.517 +   if (png_ptr == NULL || ptr == NULL)
  80.518 +      return;
  80.519 +
  80.520 +#endif /* PNG_USER_MEM_SUPPORTED */
  80.521 +
  80.522 +#if defined(__TURBOC__) && !defined(__FLAT__)
  80.523 +   farfree(ptr);
  80.524 +#else
  80.525 +# if defined(_MSC_VER) && defined(MAXSEG_64K)
  80.526 +   hfree(ptr);
  80.527 +# else
  80.528 +   free(ptr);
  80.529 +# endif
  80.530 +#endif
  80.531 +}
  80.532 +
  80.533 +#endif /* Not Borland DOS special memory handler */
  80.534 +
  80.535 +#if defined(PNG_1_0_X)
  80.536 +#  define png_malloc_warn png_malloc
  80.537 +#else
  80.538 +/* This function was added at libpng version 1.2.3.  The png_malloc_warn()
  80.539 + * function will set up png_malloc() to issue a png_warning and return NULL
  80.540 + * instead of issuing a png_error, if it fails to allocate the requested
  80.541 + * memory.
  80.542 + */
  80.543 +png_voidp PNGAPI
  80.544 +png_malloc_warn(png_structp png_ptr, png_uint_32 size)
  80.545 +{
  80.546 +   png_voidp ptr;
  80.547 +   png_uint_32 save_flags;
  80.548 +   if (png_ptr == NULL) return (NULL);
  80.549 +
  80.550 +   save_flags = png_ptr->flags;
  80.551 +   png_ptr->flags|=PNG_FLAG_MALLOC_NULL_MEM_OK;
  80.552 +   ptr = (png_voidp)png_malloc((png_structp)png_ptr, size);
  80.553 +   png_ptr->flags=save_flags;
  80.554 +   return(ptr);
  80.555 +}
  80.556 +#endif
  80.557 +
  80.558 +png_voidp PNGAPI
  80.559 +png_memcpy_check (png_structp png_ptr, png_voidp s1, png_voidp s2,
  80.560 +   png_uint_32 length)
  80.561 +{
  80.562 +   png_size_t size;
  80.563 +
  80.564 +   size = (png_size_t)length;
  80.565 +   if ((png_uint_32)size != length)
  80.566 +      png_error(png_ptr, "Overflow in png_memcpy_check.");
  80.567 +
  80.568 +   return(png_memcpy (s1, s2, size));
  80.569 +}
  80.570 +
  80.571 +png_voidp PNGAPI
  80.572 +png_memset_check (png_structp png_ptr, png_voidp s1, int value,
  80.573 +   png_uint_32 length)
  80.574 +{
  80.575 +   png_size_t size;
  80.576 +
  80.577 +   size = (png_size_t)length;
  80.578 +   if ((png_uint_32)size != length)
  80.579 +      png_error(png_ptr, "Overflow in png_memset_check.");
  80.580 +
  80.581 +   return (png_memset (s1, value, size));
  80.582 +
  80.583 +}
  80.584 +
  80.585 +#ifdef PNG_USER_MEM_SUPPORTED
  80.586 +/* This function is called when the application wants to use another method
  80.587 + * of allocating and freeing memory.
  80.588 + */
  80.589 +void PNGAPI
  80.590 +png_set_mem_fn(png_structp png_ptr, png_voidp mem_ptr, png_malloc_ptr
  80.591 +  malloc_fn, png_free_ptr free_fn)
  80.592 +{
  80.593 +   if (png_ptr != NULL)
  80.594 +   {
  80.595 +      png_ptr->mem_ptr = mem_ptr;
  80.596 +      png_ptr->malloc_fn = malloc_fn;
  80.597 +      png_ptr->free_fn = free_fn;
  80.598 +   }
  80.599 +}
  80.600 +
  80.601 +/* This function returns a pointer to the mem_ptr associated with the user
  80.602 + * functions.  The application should free any memory associated with this
  80.603 + * pointer before png_write_destroy and png_read_destroy are called.
  80.604 + */
  80.605 +png_voidp PNGAPI
  80.606 +png_get_mem_ptr(png_structp png_ptr)
  80.607 +{
  80.608 +   if (png_ptr == NULL) return (NULL);
  80.609 +   return ((png_voidp)png_ptr->mem_ptr);
  80.610 +}
  80.611 +#endif /* PNG_USER_MEM_SUPPORTED */
  80.612 +#endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */
    81.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    81.2 +++ b/libs/libpng/pngpread.c	Sat Sep 19 05:51:51 2015 +0300
    81.3 @@ -0,0 +1,1594 @@
    81.4 +
    81.5 +/* pngpread.c - read a png file in push mode
    81.6 + *
    81.7 + * Last changed in libpng 1.2.32 [September 18, 2008]
    81.8 + * For conditions of distribution and use, see copyright notice in png.h
    81.9 + * Copyright (c) 1998-2008 Glenn Randers-Pehrson
   81.10 + * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
   81.11 + * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
   81.12 + */
   81.13 +
   81.14 +#define PNG_INTERNAL
   81.15 +#include "png.h"
   81.16 +#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
   81.17 +
   81.18 +/* push model modes */
   81.19 +#define PNG_READ_SIG_MODE   0
   81.20 +#define PNG_READ_CHUNK_MODE 1
   81.21 +#define PNG_READ_IDAT_MODE  2
   81.22 +#define PNG_SKIP_MODE       3
   81.23 +#define PNG_READ_tEXt_MODE  4
   81.24 +#define PNG_READ_zTXt_MODE  5
   81.25 +#define PNG_READ_DONE_MODE  6
   81.26 +#define PNG_READ_iTXt_MODE  7
   81.27 +#define PNG_ERROR_MODE      8
   81.28 +
   81.29 +void PNGAPI
   81.30 +png_process_data(png_structp png_ptr, png_infop info_ptr,
   81.31 +   png_bytep buffer, png_size_t buffer_size)
   81.32 +{
   81.33 +   if (png_ptr == NULL || info_ptr == NULL) return;
   81.34 +   png_push_restore_buffer(png_ptr, buffer, buffer_size);
   81.35 +
   81.36 +   while (png_ptr->buffer_size)
   81.37 +   {
   81.38 +      png_process_some_data(png_ptr, info_ptr);
   81.39 +   }
   81.40 +}
   81.41 +
   81.42 +/* What we do with the incoming data depends on what we were previously
   81.43 + * doing before we ran out of data...
   81.44 + */
   81.45 +void /* PRIVATE */
   81.46 +png_process_some_data(png_structp png_ptr, png_infop info_ptr)
   81.47 +{
   81.48 +   if (png_ptr == NULL) return;
   81.49 +   switch (png_ptr->process_mode)
   81.50 +   {
   81.51 +      case PNG_READ_SIG_MODE:
   81.52 +      {
   81.53 +         png_push_read_sig(png_ptr, info_ptr);
   81.54 +         break;
   81.55 +      }
   81.56 +      case PNG_READ_CHUNK_MODE:
   81.57 +      {
   81.58 +         png_push_read_chunk(png_ptr, info_ptr);
   81.59 +         break;
   81.60 +      }
   81.61 +      case PNG_READ_IDAT_MODE:
   81.62 +      {
   81.63 +         png_push_read_IDAT(png_ptr);
   81.64 +         break;
   81.65 +      }
   81.66 +#if defined(PNG_READ_tEXt_SUPPORTED)
   81.67 +      case PNG_READ_tEXt_MODE:
   81.68 +      {
   81.69 +         png_push_read_tEXt(png_ptr, info_ptr);
   81.70 +         break;
   81.71 +      }
   81.72 +#endif
   81.73 +#if defined(PNG_READ_zTXt_SUPPORTED)
   81.74 +      case PNG_READ_zTXt_MODE:
   81.75 +      {
   81.76 +         png_push_read_zTXt(png_ptr, info_ptr);
   81.77 +         break;
   81.78 +      }
   81.79 +#endif
   81.80 +#if defined(PNG_READ_iTXt_SUPPORTED)
   81.81 +      case PNG_READ_iTXt_MODE:
   81.82 +      {
   81.83 +         png_push_read_iTXt(png_ptr, info_ptr);
   81.84 +         break;
   81.85 +      }
   81.86 +#endif
   81.87 +      case PNG_SKIP_MODE:
   81.88 +      {
   81.89 +         png_push_crc_finish(png_ptr);
   81.90 +         break;
   81.91 +      }
   81.92 +      default:
   81.93 +      {
   81.94 +         png_ptr->buffer_size = 0;
   81.95 +         break;
   81.96 +      }
   81.97 +   }
   81.98 +}
   81.99 +
  81.100 +/* Read any remaining signature bytes from the stream and compare them with
  81.101 + * the correct PNG signature.  It is possible that this routine is called
  81.102 + * with bytes already read from the signature, either because they have been
  81.103 + * checked by the calling application, or because of multiple calls to this
  81.104 + * routine.
  81.105 + */
  81.106 +void /* PRIVATE */
  81.107 +png_push_read_sig(png_structp png_ptr, png_infop info_ptr)
  81.108 +{
  81.109 +   png_size_t num_checked = png_ptr->sig_bytes,
  81.110 +             num_to_check = 8 - num_checked;
  81.111 +
  81.112 +   if (png_ptr->buffer_size < num_to_check)
  81.113 +   {
  81.114 +      num_to_check = png_ptr->buffer_size;
  81.115 +   }
  81.116 +
  81.117 +   png_push_fill_buffer(png_ptr, &(info_ptr->signature[num_checked]),
  81.118 +      num_to_check);
  81.119 +   png_ptr->sig_bytes = (png_byte)(png_ptr->sig_bytes + num_to_check);
  81.120 +
  81.121 +   if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check))
  81.122 +   {
  81.123 +      if (num_checked < 4 &&
  81.124 +          png_sig_cmp(info_ptr->signature, num_checked, num_to_check - 4))
  81.125 +         png_error(png_ptr, "Not a PNG file");
  81.126 +      else
  81.127 +         png_error(png_ptr, "PNG file corrupted by ASCII conversion");
  81.128 +   }
  81.129 +   else
  81.130 +   {
  81.131 +      if (png_ptr->sig_bytes >= 8)
  81.132 +      {
  81.133 +         png_ptr->process_mode = PNG_READ_CHUNK_MODE;
  81.134 +      }
  81.135 +   }
  81.136 +}
  81.137 +
  81.138 +void /* PRIVATE */
  81.139 +png_push_read_chunk(png_structp png_ptr, png_infop info_ptr)
  81.140 +{
  81.141 +#ifdef PNG_USE_LOCAL_ARRAYS
  81.142 +      PNG_CONST PNG_IHDR;
  81.143 +      PNG_CONST PNG_IDAT;
  81.144 +      PNG_CONST PNG_IEND;
  81.145 +      PNG_CONST PNG_PLTE;
  81.146 +#if defined(PNG_READ_bKGD_SUPPORTED)
  81.147 +      PNG_CONST PNG_bKGD;
  81.148 +#endif
  81.149 +#if defined(PNG_READ_cHRM_SUPPORTED)
  81.150 +      PNG_CONST PNG_cHRM;
  81.151 +#endif
  81.152 +#if defined(PNG_READ_gAMA_SUPPORTED)
  81.153 +      PNG_CONST PNG_gAMA;
  81.154 +#endif
  81.155 +#if defined(PNG_READ_hIST_SUPPORTED)
  81.156 +      PNG_CONST PNG_hIST;
  81.157 +#endif
  81.158 +#if defined(PNG_READ_iCCP_SUPPORTED)
  81.159 +      PNG_CONST PNG_iCCP;
  81.160 +#endif
  81.161 +#if defined(PNG_READ_iTXt_SUPPORTED)
  81.162 +      PNG_CONST PNG_iTXt;
  81.163 +#endif
  81.164 +#if defined(PNG_READ_oFFs_SUPPORTED)
  81.165 +      PNG_CONST PNG_oFFs;
  81.166 +#endif
  81.167 +#if defined(PNG_READ_pCAL_SUPPORTED)
  81.168 +      PNG_CONST PNG_pCAL;
  81.169 +#endif
  81.170 +#if defined(PNG_READ_pHYs_SUPPORTED)
  81.171 +      PNG_CONST PNG_pHYs;
  81.172 +#endif
  81.173 +#if defined(PNG_READ_sBIT_SUPPORTED)
  81.174 +      PNG_CONST PNG_sBIT;
  81.175 +#endif
  81.176 +#if defined(PNG_READ_sCAL_SUPPORTED)
  81.177 +      PNG_CONST PNG_sCAL;
  81.178 +#endif
  81.179 +#if defined(PNG_READ_sRGB_SUPPORTED)
  81.180 +      PNG_CONST PNG_sRGB;
  81.181 +#endif
  81.182 +#if defined(PNG_READ_sPLT_SUPPORTED)
  81.183 +      PNG_CONST PNG_sPLT;
  81.184 +#endif
  81.185 +#if defined(PNG_READ_tEXt_SUPPORTED)
  81.186 +      PNG_CONST PNG_tEXt;
  81.187 +#endif
  81.188 +#if defined(PNG_READ_tIME_SUPPORTED)
  81.189 +      PNG_CONST PNG_tIME;
  81.190 +#endif
  81.191 +#if defined(PNG_READ_tRNS_SUPPORTED)
  81.192 +      PNG_CONST PNG_tRNS;
  81.193 +#endif
  81.194 +#if defined(PNG_READ_zTXt_SUPPORTED)
  81.195 +      PNG_CONST PNG_zTXt;
  81.196 +#endif
  81.197 +#endif /* PNG_USE_LOCAL_ARRAYS */
  81.198 +   /* First we make sure we have enough data for the 4 byte chunk name
  81.199 +    * and the 4 byte chunk length before proceeding with decoding the
  81.200 +    * chunk data.  To fully decode each of these chunks, we also make
  81.201 +    * sure we have enough data in the buffer for the 4 byte CRC at the
  81.202 +    * end of every chunk (except IDAT, which is handled separately).
  81.203 +    */
  81.204 +   if (!(png_ptr->mode & PNG_HAVE_CHUNK_HEADER))
  81.205 +   {
  81.206 +      png_byte chunk_length[4];
  81.207 +
  81.208 +      if (png_ptr->buffer_size < 8)
  81.209 +      {
  81.210 +         png_push_save_buffer(png_ptr);
  81.211 +         return;
  81.212 +      }
  81.213 +
  81.214 +      png_push_fill_buffer(png_ptr, chunk_length, 4);
  81.215 +      png_ptr->push_length = png_get_uint_31(png_ptr, chunk_length);
  81.216 +      png_reset_crc(png_ptr);
  81.217 +      png_crc_read(png_ptr, png_ptr->chunk_name, 4);
  81.218 +      png_check_chunk_name(png_ptr, png_ptr->chunk_name);
  81.219 +      png_ptr->mode |= PNG_HAVE_CHUNK_HEADER;
  81.220 +   }
  81.221 +
  81.222 +   if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
  81.223 +     if (png_ptr->mode & PNG_AFTER_IDAT)
  81.224 +        png_ptr->mode |= PNG_HAVE_CHUNK_AFTER_IDAT;
  81.225 +
  81.226 +   if (!png_memcmp(png_ptr->chunk_name, png_IHDR, 4))
  81.227 +   {
  81.228 +      if (png_ptr->push_length != 13)
  81.229 +         png_error(png_ptr, "Invalid IHDR length");
  81.230 +      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
  81.231 +      {
  81.232 +         png_push_save_buffer(png_ptr);
  81.233 +         return;
  81.234 +      }
  81.235 +      png_handle_IHDR(png_ptr, info_ptr, png_ptr->push_length);
  81.236 +   }
  81.237 +   else if (!png_memcmp(png_ptr->chunk_name, png_IEND, 4))
  81.238 +   {
  81.239 +      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
  81.240 +      {
  81.241 +         png_push_save_buffer(png_ptr);
  81.242 +         return;
  81.243 +      }
  81.244 +      png_handle_IEND(png_ptr, info_ptr, png_ptr->push_length);
  81.245 +
  81.246 +      png_ptr->process_mode = PNG_READ_DONE_MODE;
  81.247 +      png_push_have_end(png_ptr, info_ptr);
  81.248 +   }
  81.249 +#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
  81.250 +   else if (png_handle_as_unknown(png_ptr, png_ptr->chunk_name))
  81.251 +   {
  81.252 +      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
  81.253 +      {
  81.254 +         png_push_save_buffer(png_ptr);
  81.255 +         return;
  81.256 +      }
  81.257 +      if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
  81.258 +         png_ptr->mode |= PNG_HAVE_IDAT;
  81.259 +      png_handle_unknown(png_ptr, info_ptr, png_ptr->push_length);
  81.260 +      if (!png_memcmp(png_ptr->chunk_name, png_PLTE, 4))
  81.261 +         png_ptr->mode |= PNG_HAVE_PLTE;
  81.262 +      else if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
  81.263 +      {
  81.264 +         if (!(png_ptr->mode & PNG_HAVE_IHDR))
  81.265 +            png_error(png_ptr, "Missing IHDR before IDAT");
  81.266 +         else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
  81.267 +                  !(png_ptr->mode & PNG_HAVE_PLTE))
  81.268 +            png_error(png_ptr, "Missing PLTE before IDAT");
  81.269 +      }
  81.270 +   }
  81.271 +#endif
  81.272 +   else if (!png_memcmp(png_ptr->chunk_name, png_PLTE, 4))
  81.273 +   {
  81.274 +      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
  81.275 +      {
  81.276 +         png_push_save_buffer(png_ptr);
  81.277 +         return;
  81.278 +      }
  81.279 +      png_handle_PLTE(png_ptr, info_ptr, png_ptr->push_length);
  81.280 +   }
  81.281 +   else if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
  81.282 +   {
  81.283 +      /* If we reach an IDAT chunk, this means we have read all of the
  81.284 +       * header chunks, and we can start reading the image (or if this
  81.285 +       * is called after the image has been read - we have an error).
  81.286 +       */
  81.287 +     if (!(png_ptr->mode & PNG_HAVE_IHDR))
  81.288 +       png_error(png_ptr, "Missing IHDR before IDAT");
  81.289 +     else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
  81.290 +         !(png_ptr->mode & PNG_HAVE_PLTE))
  81.291 +       png_error(png_ptr, "Missing PLTE before IDAT");
  81.292 +
  81.293 +      if (png_ptr->mode & PNG_HAVE_IDAT)
  81.294 +      {
  81.295 +         if (!(png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT))
  81.296 +           if (png_ptr->push_length == 0)
  81.297 +              return;
  81.298 +
  81.299 +         if (png_ptr->mode & PNG_AFTER_IDAT)
  81.300 +            png_error(png_ptr, "Too many IDAT's found");
  81.301 +      }
  81.302 +
  81.303 +      png_ptr->idat_size = png_ptr->push_length;
  81.304 +      png_ptr->mode |= PNG_HAVE_IDAT;
  81.305 +      png_ptr->process_mode = PNG_READ_IDAT_MODE;
  81.306 +      png_push_have_info(png_ptr, info_ptr);
  81.307 +      png_ptr->zstream.avail_out = (uInt)png_ptr->irowbytes;
  81.308 +      png_ptr->zstream.next_out = png_ptr->row_buf;
  81.309 +      return;
  81.310 +   }
  81.311 +#if defined(PNG_READ_gAMA_SUPPORTED)
  81.312 +   else if (!png_memcmp(png_ptr->chunk_name, png_gAMA, 4))
  81.313 +   {
  81.314 +      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
  81.315 +      {
  81.316 +         png_push_save_buffer(png_ptr);
  81.317 +         return;
  81.318 +      }
  81.319 +      png_handle_gAMA(png_ptr, info_ptr, png_ptr->push_length);
  81.320 +   }
  81.321 +#endif
  81.322 +#if defined(PNG_READ_sBIT_SUPPORTED)
  81.323 +   else if (!png_memcmp(png_ptr->chunk_name, png_sBIT, 4))
  81.324 +   {
  81.325 +      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
  81.326 +      {
  81.327 +         png_push_save_buffer(png_ptr);
  81.328 +         return;
  81.329 +      }
  81.330 +      png_handle_sBIT(png_ptr, info_ptr, png_ptr->push_length);
  81.331 +   }
  81.332 +#endif
  81.333 +#if defined(PNG_READ_cHRM_SUPPORTED)
  81.334 +   else if (!png_memcmp(png_ptr->chunk_name, png_cHRM, 4))
  81.335 +   {
  81.336 +      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
  81.337 +      {
  81.338 +         png_push_save_buffer(png_ptr);
  81.339 +         return;
  81.340 +      }
  81.341 +      png_handle_cHRM(png_ptr, info_ptr, png_ptr->push_length);
  81.342 +   }
  81.343 +#endif
  81.344 +#if defined(PNG_READ_sRGB_SUPPORTED)
  81.345 +   else if (!png_memcmp(png_ptr->chunk_name, png_sRGB, 4))
  81.346 +   {
  81.347 +      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
  81.348 +      {
  81.349 +         png_push_save_buffer(png_ptr);
  81.350 +         return;
  81.351 +      }
  81.352 +      png_handle_sRGB(png_ptr, info_ptr, png_ptr->push_length);
  81.353 +   }
  81.354 +#endif
  81.355 +#if defined(PNG_READ_iCCP_SUPPORTED)
  81.356 +   else if (!png_memcmp(png_ptr->chunk_name, png_iCCP, 4))
  81.357 +   {
  81.358 +      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
  81.359 +      {
  81.360 +         png_push_save_buffer(png_ptr);
  81.361 +         return;
  81.362 +      }
  81.363 +      png_handle_iCCP(png_ptr, info_ptr, png_ptr->push_length);
  81.364 +   }
  81.365 +#endif
  81.366 +#if defined(PNG_READ_sPLT_SUPPORTED)
  81.367 +   else if (!png_memcmp(png_ptr->chunk_name, png_sPLT, 4))
  81.368 +   {
  81.369 +      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
  81.370 +      {
  81.371 +         png_push_save_buffer(png_ptr);
  81.372 +         return;
  81.373 +      }
  81.374 +      png_handle_sPLT(png_ptr, info_ptr, png_ptr->push_length);
  81.375 +   }
  81.376 +#endif
  81.377 +#if defined(PNG_READ_tRNS_SUPPORTED)
  81.378 +   else if (!png_memcmp(png_ptr->chunk_name, png_tRNS, 4))
  81.379 +   {
  81.380 +      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
  81.381 +      {
  81.382 +         png_push_save_buffer(png_ptr);
  81.383 +         return;
  81.384 +      }
  81.385 +      png_handle_tRNS(png_ptr, info_ptr, png_ptr->push_length);
  81.386 +   }
  81.387 +#endif
  81.388 +#if defined(PNG_READ_bKGD_SUPPORTED)
  81.389 +   else if (!png_memcmp(png_ptr->chunk_name, png_bKGD, 4))
  81.390 +   {
  81.391 +      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
  81.392 +      {
  81.393 +         png_push_save_buffer(png_ptr);
  81.394 +         return;
  81.395 +      }
  81.396 +      png_handle_bKGD(png_ptr, info_ptr, png_ptr->push_length);
  81.397 +   }
  81.398 +#endif
  81.399 +#if defined(PNG_READ_hIST_SUPPORTED)
  81.400 +   else if (!png_memcmp(png_ptr->chunk_name, png_hIST, 4))
  81.401 +   {
  81.402 +      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
  81.403 +      {
  81.404 +         png_push_save_buffer(png_ptr);
  81.405 +         return;
  81.406 +      }
  81.407 +      png_handle_hIST(png_ptr, info_ptr, png_ptr->push_length);
  81.408 +   }
  81.409 +#endif
  81.410 +#if defined(PNG_READ_pHYs_SUPPORTED)
  81.411 +   else if (!png_memcmp(png_ptr->chunk_name, png_pHYs, 4))
  81.412 +   {
  81.413 +      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
  81.414 +      {
  81.415 +         png_push_save_buffer(png_ptr);
  81.416 +         return;
  81.417 +      }
  81.418 +      png_handle_pHYs(png_ptr, info_ptr, png_ptr->push_length);
  81.419 +   }
  81.420 +#endif
  81.421 +#if defined(PNG_READ_oFFs_SUPPORTED)
  81.422 +   else if (!png_memcmp(png_ptr->chunk_name, png_oFFs, 4))
  81.423 +   {
  81.424 +      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
  81.425 +      {
  81.426 +         png_push_save_buffer(png_ptr);
  81.427 +         return;
  81.428 +      }
  81.429 +      png_handle_oFFs(png_ptr, info_ptr, png_ptr->push_length);
  81.430 +   }
  81.431 +#endif
  81.432 +#if defined(PNG_READ_pCAL_SUPPORTED)
  81.433 +   else if (!png_memcmp(png_ptr->chunk_name, png_pCAL, 4))
  81.434 +   {
  81.435 +      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
  81.436 +      {
  81.437 +         png_push_save_buffer(png_ptr);
  81.438 +         return;
  81.439 +      }
  81.440 +      png_handle_pCAL(png_ptr, info_ptr, png_ptr->push_length);
  81.441 +   }
  81.442 +#endif
  81.443 +#if defined(PNG_READ_sCAL_SUPPORTED)
  81.444 +   else if (!png_memcmp(png_ptr->chunk_name, png_sCAL, 4))
  81.445 +   {
  81.446 +      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
  81.447 +      {
  81.448 +         png_push_save_buffer(png_ptr);
  81.449 +         return;
  81.450 +      }
  81.451 +      png_handle_sCAL(png_ptr, info_ptr, png_ptr->push_length);
  81.452 +   }
  81.453 +#endif
  81.454 +#if defined(PNG_READ_tIME_SUPPORTED)
  81.455 +   else if (!png_memcmp(png_ptr->chunk_name, png_tIME, 4))
  81.456 +   {
  81.457 +      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
  81.458 +      {
  81.459 +         png_push_save_buffer(png_ptr);
  81.460 +         return;
  81.461 +      }
  81.462 +      png_handle_tIME(png_ptr, info_ptr, png_ptr->push_length);
  81.463 +   }
  81.464 +#endif
  81.465 +#if defined(PNG_READ_tEXt_SUPPORTED)
  81.466 +   else if (!png_memcmp(png_ptr->chunk_name, png_tEXt, 4))
  81.467 +   {
  81.468 +      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
  81.469 +      {
  81.470 +         png_push_save_buffer(png_ptr);
  81.471 +         return;
  81.472 +      }
  81.473 +      png_push_handle_tEXt(png_ptr, info_ptr, png_ptr->push_length);
  81.474 +   }
  81.475 +#endif
  81.476 +#if defined(PNG_READ_zTXt_SUPPORTED)
  81.477 +   else if (!png_memcmp(png_ptr->chunk_name, png_zTXt, 4))
  81.478 +   {
  81.479 +      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
  81.480 +      {
  81.481 +         png_push_save_buffer(png_ptr);
  81.482 +         return;
  81.483 +      }
  81.484 +      png_push_handle_zTXt(png_ptr, info_ptr, png_ptr->push_length);
  81.485 +   }
  81.486 +#endif
  81.487 +#if defined(PNG_READ_iTXt_SUPPORTED)
  81.488 +   else if (!png_memcmp(png_ptr->chunk_name, png_iTXt, 4))
  81.489 +   {
  81.490 +      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
  81.491 +      {
  81.492 +         png_push_save_buffer(png_ptr);
  81.493 +         return;
  81.494 +      }
  81.495 +      png_push_handle_iTXt(png_ptr, info_ptr, png_ptr->push_length);
  81.496 +   }
  81.497 +#endif
  81.498 +   else
  81.499 +   {
  81.500 +      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
  81.501 +      {
  81.502 +         png_push_save_buffer(png_ptr);
  81.503 +         return;
  81.504 +      }
  81.505 +      png_push_handle_unknown(png_ptr, info_ptr, png_ptr->push_length);
  81.506 +   }
  81.507 +
  81.508 +   png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
  81.509 +}
  81.510 +
  81.511 +void /* PRIVATE */
  81.512 +png_push_crc_skip(png_structp png_ptr, png_uint_32 skip)
  81.513 +{
  81.514 +   png_ptr->process_mode = PNG_SKIP_MODE;
  81.515 +   png_ptr->skip_length = skip;
  81.516 +}
  81.517 +
  81.518 +void /* PRIVATE */
  81.519 +png_push_crc_finish(png_structp png_ptr)
  81.520 +{
  81.521 +   if (png_ptr->skip_length && png_ptr->save_buffer_size)
  81.522 +   {
  81.523 +      png_size_t save_size;
  81.524 +
  81.525 +      if (png_ptr->skip_length < (png_uint_32)png_ptr->save_buffer_size)
  81.526 +         save_size = (png_size_t)png_ptr->skip_length;
  81.527 +      else
  81.528 +         save_size = png_ptr->save_buffer_size;
  81.529 +
  81.530 +      png_calculate_crc(png_ptr, png_ptr->save_buffer_ptr, save_size);
  81.531 +
  81.532 +      png_ptr->skip_length -= save_size;
  81.533 +      png_ptr->buffer_size -= save_size;
  81.534 +      png_ptr->save_buffer_size -= save_size;
  81.535 +      png_ptr->save_buffer_ptr += save_size;
  81.536 +   }
  81.537 +   if (png_ptr->skip_length && png_ptr->current_buffer_size)
  81.538 +   {
  81.539 +      png_size_t save_size;
  81.540 +
  81.541 +      if (png_ptr->skip_length < (png_uint_32)png_ptr->current_buffer_size)
  81.542 +         save_size = (png_size_t)png_ptr->skip_length;
  81.543 +      else
  81.544 +         save_size = png_ptr->current_buffer_size;
  81.545 +
  81.546 +      png_calculate_crc(png_ptr, png_ptr->current_buffer_ptr, save_size);
  81.547 +
  81.548 +      png_ptr->skip_length -= save_size;
  81.549 +      png_ptr->buffer_size -= save_size;
  81.550 +      png_ptr->current_buffer_size -= save_size;
  81.551 +      png_ptr->current_buffer_ptr += save_size;
  81.552 +   }
  81.553 +   if (!png_ptr->skip_length)
  81.554 +   {
  81.555 +      if (png_ptr->buffer_size < 4)
  81.556 +      {
  81.557 +         png_push_save_buffer(png_ptr);
  81.558 +         return;
  81.559 +      }
  81.560 +
  81.561 +      png_crc_finish(png_ptr, 0);
  81.562 +      png_ptr->process_mode = PNG_READ_CHUNK_MODE;
  81.563 +   }
  81.564 +}
  81.565 +
  81.566 +void PNGAPI
  81.567 +png_push_fill_buffer(png_structp png_ptr, png_bytep buffer, png_size_t length)
  81.568 +{
  81.569 +   png_bytep ptr;
  81.570 +
  81.571 +   if (png_ptr == NULL) return;
  81.572 +   ptr = buffer;
  81.573 +   if (png_ptr->save_buffer_size)
  81.574 +   {
  81.575 +      png_size_t save_size;
  81.576 +
  81.577 +      if (length < png_ptr->save_buffer_size)
  81.578 +         save_size = length;
  81.579 +      else
  81.580 +         save_size = png_ptr->save_buffer_size;
  81.581 +
  81.582 +      png_memcpy(ptr, png_ptr->save_buffer_ptr, save_size);
  81.583 +      length -= save_size;
  81.584 +      ptr += save_size;
  81.585 +      png_ptr->buffer_size -= save_size;
  81.586 +      png_ptr->save_buffer_size -= save_size;
  81.587 +      png_ptr->save_buffer_ptr += save_size;
  81.588 +   }
  81.589 +   if (length && png_ptr->current_buffer_size)
  81.590 +   {
  81.591 +      png_size_t save_size;
  81.592 +
  81.593 +      if (length < png_ptr->current_buffer_size)
  81.594 +         save_size = length;
  81.595 +      else
  81.596 +         save_size = png_ptr->current_buffer_size;
  81.597 +
  81.598 +      png_memcpy(ptr, png_ptr->current_buffer_ptr, save_size);
  81.599 +      png_ptr->buffer_size -= save_size;
  81.600 +      png_ptr->current_buffer_size -= save_size;
  81.601 +      png_ptr->current_buffer_ptr += save_size;
  81.602 +   }
  81.603 +}
  81.604 +
  81.605 +void /* PRIVATE */
  81.606 +png_push_save_buffer(png_structp png_ptr)
  81.607 +{
  81.608 +   if (png_ptr->save_buffer_size)
  81.609 +   {
  81.610 +      if (png_ptr->save_buffer_ptr != png_ptr->save_buffer)
  81.611 +      {
  81.612 +         png_size_t i, istop;
  81.613 +         png_bytep sp;
  81.614 +         png_bytep dp;
  81.615 +
  81.616 +         istop = png_ptr->save_buffer_size;
  81.617 +         for (i = 0, sp = png_ptr->save_buffer_ptr, dp = png_ptr->save_buffer;
  81.618 +            i < istop; i++, sp++, dp++)
  81.619 +         {
  81.620 +            *dp = *sp;
  81.621 +         }
  81.622 +      }
  81.623 +   }
  81.624 +   if (png_ptr->save_buffer_size + png_ptr->current_buffer_size >
  81.625 +      png_ptr->save_buffer_max)
  81.626 +   {
  81.627 +      png_size_t new_max;
  81.628 +      png_bytep old_buffer;
  81.629 +
  81.630 +      if (png_ptr->save_buffer_size > PNG_SIZE_MAX -
  81.631 +         (png_ptr->current_buffer_size + 256))
  81.632 +      {
  81.633 +        png_error(png_ptr, "Potential overflow of save_buffer");
  81.634 +      }
  81.635 +      new_max = png_ptr->save_buffer_size + png_ptr->current_buffer_size + 256;
  81.636 +      old_buffer = png_ptr->save_buffer;
  81.637 +      png_ptr->save_buffer = (png_bytep)png_malloc(png_ptr,
  81.638 +         (png_uint_32)new_max);
  81.639 +      png_memcpy(png_ptr->save_buffer, old_buffer, png_ptr->save_buffer_size);
  81.640 +      png_free(png_ptr, old_buffer);
  81.641 +      png_ptr->save_buffer_max = new_max;
  81.642 +   }
  81.643 +   if (png_ptr->current_buffer_size)
  81.644 +   {
  81.645 +      png_memcpy(png_ptr->save_buffer + png_ptr->save_buffer_size,
  81.646 +         png_ptr->current_buffer_ptr, png_ptr->current_buffer_size);
  81.647 +      png_ptr->save_buffer_size += png_ptr->current_buffer_size;
  81.648 +      png_ptr->current_buffer_size = 0;
  81.649 +   }
  81.650 +   png_ptr->save_buffer_ptr = png_ptr->save_buffer;
  81.651 +   png_ptr->buffer_size = 0;
  81.652 +}
  81.653 +
  81.654 +void /* PRIVATE */
  81.655 +png_push_restore_buffer(png_structp png_ptr, png_bytep buffer,
  81.656 +   png_size_t buffer_length)
  81.657 +{
  81.658 +   png_ptr->current_buffer = buffer;
  81.659 +   png_ptr->current_buffer_size = buffer_length;
  81.660 +   png_ptr->buffer_size = buffer_length + png_ptr->save_buffer_size;
  81.661 +   png_ptr->current_buffer_ptr = png_ptr->current_buffer;
  81.662 +}
  81.663 +
  81.664 +void /* PRIVATE */
  81.665 +png_push_read_IDAT(png_structp png_ptr)
  81.666 +{
  81.667 +#ifdef PNG_USE_LOCAL_ARRAYS
  81.668 +   PNG_CONST PNG_IDAT;
  81.669 +#endif
  81.670 +   if (!(png_ptr->mode & PNG_HAVE_CHUNK_HEADER))
  81.671 +   {
  81.672 +      png_byte chunk_length[4];
  81.673 +
  81.674 +      if (png_ptr->buffer_size < 8)
  81.675 +      {
  81.676 +         png_push_save_buffer(png_ptr);
  81.677 +         return;
  81.678 +      }
  81.679 +
  81.680 +      png_push_fill_buffer(png_ptr, chunk_length, 4);
  81.681 +      png_ptr->push_length = png_get_uint_31(png_ptr, chunk_length);
  81.682 +      png_reset_crc(png_ptr);
  81.683 +      png_crc_read(png_ptr, png_ptr->chunk_name, 4);
  81.684 +      png_ptr->mode |= PNG_HAVE_CHUNK_HEADER;
  81.685 +
  81.686 +      if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
  81.687 +      {
  81.688 +         png_ptr->process_mode = PNG_READ_CHUNK_MODE;
  81.689 +         if (!(png_ptr->flags & PNG_FLAG_ZLIB_FINISHED))
  81.690 +            png_error(png_ptr, "Not enough compressed data");
  81.691 +         return;
  81.692 +      }
  81.693 +
  81.694 +      png_ptr->idat_size = png_ptr->push_length;
  81.695 +   }
  81.696 +   if (png_ptr->idat_size && png_ptr->save_buffer_size)
  81.697 +   {
  81.698 +      png_size_t save_size;
  81.699 +
  81.700 +      if (png_ptr->idat_size < (png_uint_32)png_ptr->save_buffer_size)
  81.701 +      {
  81.702 +         save_size = (png_size_t)png_ptr->idat_size;
  81.703 +         /* check for overflow */
  81.704 +         if ((png_uint_32)save_size != png_ptr->idat_size)
  81.705 +            png_error(png_ptr, "save_size overflowed in pngpread");
  81.706 +      }
  81.707 +      else
  81.708 +         save_size = png_ptr->save_buffer_size;
  81.709 +
  81.710 +      png_calculate_crc(png_ptr, png_ptr->save_buffer_ptr, save_size);
  81.711 +      if (!(png_ptr->flags & PNG_FLAG_ZLIB_FINISHED))
  81.712 +         png_process_IDAT_data(png_ptr, png_ptr->save_buffer_ptr, save_size);
  81.713 +      png_ptr->idat_size -= save_size;
  81.714 +      png_ptr->buffer_size -= save_size;
  81.715 +      png_ptr->save_buffer_size -= save_size;
  81.716 +      png_ptr->save_buffer_ptr += save_size;
  81.717 +   }
  81.718 +   if (png_ptr->idat_size && png_ptr->current_buffer_size)
  81.719 +   {
  81.720 +      png_size_t save_size;
  81.721 +
  81.722 +      if (png_ptr->idat_size < (png_uint_32)png_ptr->current_buffer_size)
  81.723 +      {
  81.724 +         save_size = (png_size_t)png_ptr->idat_size;
  81.725 +         /* check for overflow */
  81.726 +         if ((png_uint_32)save_size != png_ptr->idat_size)
  81.727 +            png_error(png_ptr, "save_size overflowed in pngpread");
  81.728 +      }
  81.729 +      else
  81.730 +         save_size = png_ptr->current_buffer_size;
  81.731 +
  81.732 +      png_calculate_crc(png_ptr, png_ptr->current_buffer_ptr, save_size);
  81.733 +      if (!(png_ptr->flags & PNG_FLAG_ZLIB_FINISHED))
  81.734 +        png_process_IDAT_data(png_ptr, png_ptr->current_buffer_ptr, save_size);
  81.735 +
  81.736 +      png_ptr->idat_size -= save_size;
  81.737 +      png_ptr->buffer_size -= save_size;
  81.738 +      png_ptr->current_buffer_size -= save_size;
  81.739 +      png_ptr->current_buffer_ptr += save_size;
  81.740 +   }
  81.741 +   if (!png_ptr->idat_size)
  81.742 +   {
  81.743 +      if (png_ptr->buffer_size < 4)
  81.744 +      {
  81.745 +         png_push_save_buffer(png_ptr);
  81.746 +         return;
  81.747 +      }
  81.748 +
  81.749 +      png_crc_finish(png_ptr, 0);
  81.750 +      png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
  81.751 +      png_ptr->mode |= PNG_AFTER_IDAT;
  81.752 +   }
  81.753 +}
  81.754 +
  81.755 +void /* PRIVATE */
  81.756 +png_process_IDAT_data(png_structp png_ptr, png_bytep buffer,
  81.757 +   png_size_t buffer_length)
  81.758 +{
  81.759 +   int ret;
  81.760 +
  81.761 +   if ((png_ptr->flags & PNG_FLAG_ZLIB_FINISHED) && buffer_length)
  81.762 +      png_error(png_ptr, "Extra compression data");
  81.763 +
  81.764 +   png_ptr->zstream.next_in = buffer;
  81.765 +   png_ptr->zstream.avail_in = (uInt)buffer_length;
  81.766 +   for (;;)
  81.767 +   {
  81.768 +      ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);
  81.769 +      if (ret != Z_OK)
  81.770 +      {
  81.771 +         if (ret == Z_STREAM_END)
  81.772 +         {
  81.773 +            if (png_ptr->zstream.avail_in)
  81.774 +               png_error(png_ptr, "Extra compressed data");
  81.775 +            if (!(png_ptr->zstream.avail_out))
  81.776 +            {
  81.777 +               png_push_process_row(png_ptr);
  81.778 +            }
  81.779 +
  81.780 +            png_ptr->mode |= PNG_AFTER_IDAT;
  81.781 +            png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
  81.782 +            break;
  81.783 +         }
  81.784 +         else if (ret == Z_BUF_ERROR)
  81.785 +            break;
  81.786 +         else
  81.787 +            png_error(png_ptr, "Decompression Error");
  81.788 +      }
  81.789 +      if (!(png_ptr->zstream.avail_out))
  81.790 +      {
  81.791 +         if ((
  81.792 +#if defined(PNG_READ_INTERLACING_SUPPORTED)
  81.793 +             png_ptr->interlaced && png_ptr->pass > 6) ||
  81.794 +             (!png_ptr->interlaced &&
  81.795 +#endif
  81.796 +             png_ptr->row_number == png_ptr->num_rows))
  81.797 +         {
  81.798 +           if (png_ptr->zstream.avail_in)
  81.799 +             png_warning(png_ptr, "Too much data in IDAT chunks");
  81.800 +           png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
  81.801 +           break;
  81.802 +         }
  81.803 +         png_push_process_row(png_ptr);
  81.804 +         png_ptr->zstream.avail_out = (uInt)png_ptr->irowbytes;
  81.805 +         png_ptr->zstream.next_out = png_ptr->row_buf;
  81.806 +      }
  81.807 +      else
  81.808 +         break;
  81.809 +   }
  81.810 +}
  81.811 +
  81.812 +void /* PRIVATE */
  81.813 +png_push_process_row(png_structp png_ptr)
  81.814 +{
  81.815 +   png_ptr->row_info.color_type = png_ptr->color_type;
  81.816 +   png_ptr->row_info.width = png_ptr->iwidth;
  81.817 +   png_ptr->row_info.channels = png_ptr->channels;
  81.818 +   png_ptr->row_info.bit_depth = png_ptr->bit_depth;
  81.819 +   png_ptr->row_info.pixel_depth = png_ptr->pixel_depth;
  81.820 +
  81.821 +   png_ptr->row_info.rowbytes = PNG_ROWBYTES(png_ptr->row_info.pixel_depth,
  81.822 +       png_ptr->row_info.width);
  81.823 +
  81.824 +   png_read_filter_row(png_ptr, &(png_ptr->row_info),
  81.825 +      png_ptr->row_buf + 1, png_ptr->prev_row + 1,
  81.826 +      (int)(png_ptr->row_buf[0]));
  81.827 +
  81.828 +   png_memcpy_check(png_ptr, png_ptr->prev_row, png_ptr->row_buf,
  81.829 +      png_ptr->rowbytes + 1);
  81.830 +
  81.831 +   if (png_ptr->transformations || (png_ptr->flags&PNG_FLAG_STRIP_ALPHA))
  81.832 +      png_do_read_transformations(png_ptr);
  81.833 +
  81.834 +#if defined(PNG_READ_INTERLACING_SUPPORTED)
  81.835 +   /* blow up interlaced rows to full size */
  81.836 +   if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
  81.837 +   {
  81.838 +      if (png_ptr->pass < 6)
  81.839 +/*       old interface (pre-1.0.9):
  81.840 +         png_do_read_interlace(&(png_ptr->row_info),
  81.841 +            png_ptr->row_buf + 1, png_ptr->pass, png_ptr->transformations);
  81.842 + */
  81.843 +         png_do_read_interlace(png_ptr);
  81.844 +
  81.845 +    switch (png_ptr->pass)
  81.846 +    {
  81.847 +         case 0:
  81.848 +         {
  81.849 +            int i;
  81.850 +            for (i = 0; i < 8 && png_ptr->pass == 0; i++)
  81.851 +            {
  81.852 +               png_push_have_row(png_ptr, png_ptr->row_buf + 1);
  81.853 +               png_read_push_finish_row(png_ptr); /* updates png_ptr->pass */
  81.854 +            }
  81.855 +            if (png_ptr->pass == 2) /* pass 1 might be empty */
  81.856 +            {
  81.857 +               for (i = 0; i < 4 && png_ptr->pass == 2; i++)
  81.858 +               {
  81.859 +                  png_push_have_row(png_ptr, png_bytep_NULL);
  81.860 +                  png_read_push_finish_row(png_ptr);
  81.861 +               }
  81.862 +            }
  81.863 +            if (png_ptr->pass == 4 && png_ptr->height <= 4)
  81.864 +            {
  81.865 +               for (i = 0; i < 2 && png_ptr->pass == 4; i++)
  81.866 +               {
  81.867 +                  png_push_have_row(png_ptr, png_bytep_NULL);
  81.868 +                  png_read_push_finish_row(png_ptr);
  81.869 +               }
  81.870 +            }
  81.871 +            if (png_ptr->pass == 6 && png_ptr->height <= 4)
  81.872 +            {
  81.873 +                png_push_have_row(png_ptr, png_bytep_NULL);
  81.874 +                png_read_push_finish_row(png_ptr);
  81.875 +            }
  81.876 +            break;
  81.877 +         }
  81.878 +         case 1:
  81.879 +         {
  81.880 +            int i;
  81.881 +            for (i = 0; i < 8 && png_ptr->pass == 1; i++)
  81.882 +            {
  81.883 +               png_push_have_row(png_ptr, png_ptr->row_buf + 1);
  81.884 +               png_read_push_finish_row(png_ptr);
  81.885 +            }
  81.886 +            if (png_ptr->pass == 2) /* skip top 4 generated rows */
  81.887 +            {
  81.888 +               for (i = 0; i < 4 && png_ptr->pass == 2; i++)
  81.889 +               {
  81.890 +                  png_push_have_row(png_ptr, png_bytep_NULL);
  81.891 +                  png_read_push_finish_row(png_ptr);
  81.892 +               }
  81.893 +            }
  81.894 +            break;
  81.895 +         }
  81.896 +         case 2:
  81.897 +         {
  81.898 +            int i;
  81.899 +            for (i = 0; i < 4 && png_ptr->pass == 2; i++)
  81.900 +            {
  81.901 +               png_push_have_row(png_ptr, png_ptr->row_buf + 1);
  81.902 +               png_read_push_finish_row(png_ptr);
  81.903 +            }
  81.904 +            for (i = 0; i < 4 && png_ptr->pass == 2; i++)
  81.905 +            {
  81.906 +               png_push_have_row(png_ptr, png_bytep_NULL);
  81.907 +               png_read_push_finish_row(png_ptr);
  81.908 +            }
  81.909 +            if (png_ptr->pass == 4) /* pass 3 might be empty */
  81.910 +            {
  81.911 +               for (i = 0; i < 2 && png_ptr->pass == 4; i++)
  81.912 +               {
  81.913 +                  png_push_have_row(png_ptr, png_bytep_NULL);
  81.914 +                  png_read_push_finish_row(png_ptr);
  81.915 +               }
  81.916 +            }
  81.917 +            break;
  81.918 +         }
  81.919 +         case 3:
  81.920 +         {
  81.921 +            int i;
  81.922 +            for (i = 0; i < 4 && png_ptr->pass == 3; i++)
  81.923 +            {
  81.924 +               png_push_have_row(png_ptr, png_ptr->row_buf + 1);
  81.925 +               png_read_push_finish_row(png_ptr);
  81.926 +            }
  81.927 +            if (png_ptr->pass == 4) /* skip top two generated rows */
  81.928 +            {
  81.929 +               for (i = 0; i < 2 && png_ptr->pass == 4; i++)
  81.930 +               {
  81.931 +                  png_push_have_row(png_ptr, png_bytep_NULL);
  81.932 +                  png_read_push_finish_row(png_ptr);
  81.933 +               }
  81.934 +            }
  81.935 +            break;
  81.936 +         }
  81.937 +         case 4:
  81.938 +         {
  81.939 +            int i;
  81.940 +            for (i = 0; i < 2 && png_ptr->pass == 4; i++)
  81.941 +            {
  81.942 +               png_push_have_row(png_ptr, png_ptr->row_buf + 1);
  81.943 +               png_read_push_finish_row(png_ptr);
  81.944 +            }
  81.945 +            for (i = 0; i < 2 && png_ptr->pass == 4; i++)
  81.946 +            {
  81.947 +               png_push_have_row(png_ptr, png_bytep_NULL);
  81.948 +               png_read_push_finish_row(png_ptr);
  81.949 +            }
  81.950 +            if (png_ptr->pass == 6) /* pass 5 might be empty */
  81.951 +            {
  81.952 +               png_push_have_row(png_ptr, png_bytep_NULL);
  81.953 +               png_read_push_finish_row(png_ptr);
  81.954 +            }
  81.955 +            break;
  81.956 +         }
  81.957 +         case 5:
  81.958 +         {
  81.959 +            int i;
  81.960 +            for (i = 0; i < 2 && png_ptr->pass == 5; i++)
  81.961 +            {
  81.962 +               png_push_have_row(png_ptr, png_ptr->row_buf + 1);
  81.963 +               png_read_push_finish_row(png_ptr);
  81.964 +            }
  81.965 +            if (png_ptr->pass == 6) /* skip top generated row */
  81.966 +            {
  81.967 +               png_push_have_row(png_ptr, png_bytep_NULL);
  81.968 +               png_read_push_finish_row(png_ptr);
  81.969 +            }
  81.970 +            break;
  81.971 +         }
  81.972 +         case 6:
  81.973 +         {
  81.974 +            png_push_have_row(png_ptr, png_ptr->row_buf + 1);
  81.975 +            png_read_push_finish_row(png_ptr);
  81.976 +            if (png_ptr->pass != 6)
  81.977 +               break;
  81.978 +            png_push_have_row(png_ptr, png_bytep_NULL);
  81.979 +            png_read_push_finish_row(png_ptr);
  81.980 +         }
  81.981 +      }
  81.982 +   }
  81.983 +   else
  81.984 +#endif
  81.985 +   {
  81.986 +      png_push_have_row(png_ptr, png_ptr->row_buf + 1);
  81.987 +      png_read_push_finish_row(png_ptr);
  81.988 +   }
  81.989 +}
  81.990 +
  81.991 +void /* PRIVATE */
  81.992 +png_read_push_finish_row(png_structp png_ptr)
  81.993 +{
  81.994 +#ifdef PNG_USE_LOCAL_ARRAYS
  81.995 +   /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
  81.996 +
  81.997 +   /* start of interlace block */
  81.998 +   PNG_CONST int FARDATA png_pass_start[] = {0, 4, 0, 2, 0, 1, 0};
  81.999 +
 81.1000 +   /* offset to next interlace block */
 81.1001 +   PNG_CONST int FARDATA png_pass_inc[] = {8, 8, 4, 4, 2, 2, 1};
 81.1002 +
 81.1003 +   /* start of interlace block in the y direction */
 81.1004 +   PNG_CONST int FARDATA png_pass_ystart[] = {0, 0, 4, 0, 2, 0, 1};
 81.1005 +
 81.1006 +   /* offset to next interlace block in the y direction */
 81.1007 +   PNG_CONST int FARDATA png_pass_yinc[] = {8, 8, 8, 4, 4, 2, 2};
 81.1008 +
 81.1009 +   /* Height of interlace block.  This is not currently used - if you need
 81.1010 +    * it, uncomment it here and in png.h
 81.1011 +   PNG_CONST int FARDATA png_pass_height[] = {8, 8, 4, 4, 2, 2, 1};
 81.1012 +   */
 81.1013 +#endif
 81.1014 +
 81.1015 +   png_ptr->row_number++;
 81.1016 +   if (png_ptr->row_number < png_ptr->num_rows)
 81.1017 +      return;
 81.1018 +
 81.1019 +   if (png_ptr->interlaced)
 81.1020 +   {
 81.1021 +      png_ptr->row_number = 0;
 81.1022 +      png_memset_check(png_ptr, png_ptr->prev_row, 0,
 81.1023 +         png_ptr->rowbytes + 1);
 81.1024 +      do
 81.1025 +      {
 81.1026 +         png_ptr->pass++;
 81.1027 +         if ((png_ptr->pass == 1 && png_ptr->width < 5) ||
 81.1028 +             (png_ptr->pass == 3 && png_ptr->width < 3) ||
 81.1029 +             (png_ptr->pass == 5 && png_ptr->width < 2))
 81.1030 +           png_ptr->pass++;
 81.1031 +
 81.1032 +         if (png_ptr->pass > 7)
 81.1033 +            png_ptr->pass--;
 81.1034 +         if (png_ptr->pass >= 7)
 81.1035 +            break;
 81.1036 +
 81.1037 +         png_ptr->iwidth = (png_ptr->width +
 81.1038 +            png_pass_inc[png_ptr->pass] - 1 -
 81.1039 +            png_pass_start[png_ptr->pass]) /
 81.1040 +            png_pass_inc[png_ptr->pass];
 81.1041 +
 81.1042 +         png_ptr->irowbytes = PNG_ROWBYTES(png_ptr->pixel_depth,
 81.1043 +            png_ptr->iwidth) + 1;
 81.1044 +
 81.1045 +         if (png_ptr->transformations & PNG_INTERLACE)
 81.1046 +            break;
 81.1047 +
 81.1048 +         png_ptr->num_rows = (png_ptr->height +
 81.1049 +            png_pass_yinc[png_ptr->pass] - 1 -
 81.1050 +            png_pass_ystart[png_ptr->pass]) /
 81.1051 +            png_pass_yinc[png_ptr->pass];
 81.1052 +
 81.1053 +      } while (png_ptr->iwidth == 0 || png_ptr->num_rows == 0);
 81.1054 +   }
 81.1055 +}
 81.1056 +
 81.1057 +#if defined(PNG_READ_tEXt_SUPPORTED)
 81.1058 +void /* PRIVATE */
 81.1059 +png_push_handle_tEXt(png_structp png_ptr, png_infop info_ptr, png_uint_32
 81.1060 +   length)
 81.1061 +{
 81.1062 +   if (!(png_ptr->mode & PNG_HAVE_IHDR) || (png_ptr->mode & PNG_HAVE_IEND))
 81.1063 +      {
 81.1064 +         png_error(png_ptr, "Out of place tEXt");
 81.1065 +         info_ptr = info_ptr; /* to quiet some compiler warnings */
 81.1066 +      }
 81.1067 +
 81.1068 +#ifdef PNG_MAX_MALLOC_64K
 81.1069 +   png_ptr->skip_length = 0;  /* This may not be necessary */
 81.1070 +
 81.1071 +   if (length > (png_uint_32)65535L) /* Can't hold entire string in memory */
 81.1072 +   {
 81.1073 +      png_warning(png_ptr, "tEXt chunk too large to fit in memory");
 81.1074 +      png_ptr->skip_length = length - (png_uint_32)65535L;
 81.1075 +      length = (png_uint_32)65535L;
 81.1076 +   }
 81.1077 +#endif
 81.1078 +
 81.1079 +   png_ptr->current_text = (png_charp)png_malloc(png_ptr,
 81.1080 +      (png_uint_32)(length + 1));
 81.1081 +   png_ptr->current_text[length] = '\0';
 81.1082 +   png_ptr->current_text_ptr = png_ptr->current_text;
 81.1083 +   png_ptr->current_text_size = (png_size_t)length;
 81.1084 +   png_ptr->current_text_left = (png_size_t)length;
 81.1085 +   png_ptr->process_mode = PNG_READ_tEXt_MODE;
 81.1086 +}
 81.1087 +
 81.1088 +void /* PRIVATE */
 81.1089 +png_push_read_tEXt(png_structp png_ptr, png_infop info_ptr)
 81.1090 +{
 81.1091 +   if (png_ptr->buffer_size && png_ptr->current_text_left)
 81.1092 +   {
 81.1093 +      png_size_t text_size;
 81.1094 +
 81.1095 +      if (png_ptr->buffer_size < png_ptr->current_text_left)
 81.1096 +         text_size = png_ptr->buffer_size;
 81.1097 +      else
 81.1098 +         text_size = png_ptr->current_text_left;
 81.1099 +      png_crc_read(png_ptr, (png_bytep)png_ptr->current_text_ptr, text_size);
 81.1100 +      png_ptr->current_text_left -= text_size;
 81.1101 +      png_ptr->current_text_ptr += text_size;
 81.1102 +   }
 81.1103 +   if (!(png_ptr->current_text_left))
 81.1104 +   {
 81.1105 +      png_textp text_ptr;
 81.1106 +      png_charp text;
 81.1107 +      png_charp key;
 81.1108 +      int ret;
 81.1109 +
 81.1110 +      if (png_ptr->buffer_size < 4)
 81.1111 +      {
 81.1112 +         png_push_save_buffer(png_ptr);
 81.1113 +         return;
 81.1114 +      }
 81.1115 +
 81.1116 +      png_push_crc_finish(png_ptr);
 81.1117 +
 81.1118 +#if defined(PNG_MAX_MALLOC_64K)
 81.1119 +      if (png_ptr->skip_length)
 81.1120 +         return;
 81.1121 +#endif
 81.1122 +
 81.1123 +      key = png_ptr->current_text;
 81.1124 +
 81.1125 +      for (text = key; *text; text++)
 81.1126 +         /* empty loop */ ;
 81.1127 +
 81.1128 +      if (text < key + png_ptr->current_text_size)
 81.1129 +         text++;
 81.1130 +
 81.1131 +      text_ptr = (png_textp)png_malloc(png_ptr,
 81.1132 +         (png_uint_32)png_sizeof(png_text));
 81.1133 +      text_ptr->compression = PNG_TEXT_COMPRESSION_NONE;
 81.1134 +      text_ptr->key = key;
 81.1135 +#ifdef PNG_iTXt_SUPPORTED
 81.1136 +      text_ptr->lang = NULL;
 81.1137 +      text_ptr->lang_key = NULL;
 81.1138 +#endif
 81.1139 +      text_ptr->text = text;
 81.1140 +
 81.1141 +      ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1);
 81.1142 +
 81.1143 +      png_free(png_ptr, key);
 81.1144 +      png_free(png_ptr, text_ptr);
 81.1145 +      png_ptr->current_text = NULL;
 81.1146 +
 81.1147 +      if (ret)
 81.1148 +        png_warning(png_ptr, "Insufficient memory to store text chunk.");
 81.1149 +   }
 81.1150 +}
 81.1151 +#endif
 81.1152 +
 81.1153 +#if defined(PNG_READ_zTXt_SUPPORTED)
 81.1154 +void /* PRIVATE */
 81.1155 +png_push_handle_zTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32
 81.1156 +   length)
 81.1157 +{
 81.1158 +   if (!(png_ptr->mode & PNG_HAVE_IHDR) || (png_ptr->mode & PNG_HAVE_IEND))
 81.1159 +      {
 81.1160 +         png_error(png_ptr, "Out of place zTXt");
 81.1161 +         info_ptr = info_ptr; /* to quiet some compiler warnings */
 81.1162 +      }
 81.1163 +
 81.1164 +#ifdef PNG_MAX_MALLOC_64K
 81.1165 +   /* We can't handle zTXt chunks > 64K, since we don't have enough space
 81.1166 +    * to be able to store the uncompressed data.  Actually, the threshold
 81.1167 +    * is probably around 32K, but it isn't as definite as 64K is.
 81.1168 +    */
 81.1169 +   if (length > (png_uint_32)65535L)
 81.1170 +   {
 81.1171 +      png_warning(png_ptr, "zTXt chunk too large to fit in memory");
 81.1172 +      png_push_crc_skip(png_ptr, length);
 81.1173 +      return;
 81.1174 +   }
 81.1175 +#endif
 81.1176 +
 81.1177 +   png_ptr->current_text = (png_charp)png_malloc(png_ptr,
 81.1178 +      (png_uint_32)(length + 1));
 81.1179 +   png_ptr->current_text[length] = '\0';
 81.1180 +   png_ptr->current_text_ptr = png_ptr->current_text;
 81.1181 +   png_ptr->current_text_size = (png_size_t)length;
 81.1182 +   png_ptr->current_text_left = (png_size_t)length;
 81.1183 +   png_ptr->process_mode = PNG_READ_zTXt_MODE;
 81.1184 +}
 81.1185 +
 81.1186 +void /* PRIVATE */
 81.1187 +png_push_read_zTXt(png_structp png_ptr, png_infop info_ptr)
 81.1188 +{
 81.1189 +   if (png_ptr->buffer_size && png_ptr->current_text_left)
 81.1190 +   {
 81.1191 +      png_size_t text_size;
 81.1192 +
 81.1193 +      if (png_ptr->buffer_size < (png_uint_32)png_ptr->current_text_left)
 81.1194 +         text_size = png_ptr->buffer_size;
 81.1195 +      else
 81.1196 +         text_size = png_ptr->current_text_left;
 81.1197 +      png_crc_read(png_ptr, (png_bytep)png_ptr->current_text_ptr, text_size);
 81.1198 +      png_ptr->current_text_left -= text_size;
 81.1199 +      png_ptr->current_text_ptr += text_size;
 81.1200 +   }
 81.1201 +   if (!(png_ptr->current_text_left))
 81.1202 +   {
 81.1203 +      png_textp text_ptr;
 81.1204 +      png_charp text;
 81.1205 +      png_charp key;
 81.1206 +      int ret;
 81.1207 +      png_size_t text_size, key_size;
 81.1208 +
 81.1209 +      if (png_ptr->buffer_size < 4)
 81.1210 +      {
 81.1211 +         png_push_save_buffer(png_ptr);
 81.1212 +         return;
 81.1213 +      }
 81.1214 +
 81.1215 +      png_push_crc_finish(png_ptr);
 81.1216 +
 81.1217 +      key = png_ptr->current_text;
 81.1218 +
 81.1219 +      for (text = key; *text; text++)
 81.1220 +         /* empty loop */ ;
 81.1221 +
 81.1222 +      /* zTXt can't have zero text */
 81.1223 +      if (text >= key + png_ptr->current_text_size)
 81.1224 +      {
 81.1225 +         png_ptr->current_text = NULL;
 81.1226 +         png_free(png_ptr, key);
 81.1227 +         return;
 81.1228 +      }
 81.1229 +
 81.1230 +      text++;
 81.1231 +
 81.1232 +      if (*text != PNG_TEXT_COMPRESSION_zTXt) /* check compression byte */
 81.1233 +      {
 81.1234 +         png_ptr->current_text = NULL;
 81.1235 +         png_free(png_ptr, key);
 81.1236 +         return;
 81.1237 +      }
 81.1238 +
 81.1239 +      text++;
 81.1240 +
 81.1241 +      png_ptr->zstream.next_in = (png_bytep )text;
 81.1242 +      png_ptr->zstream.avail_in = (uInt)(png_ptr->current_text_size -
 81.1243 +         (text - key));
 81.1244 +      png_ptr->zstream.next_out = png_ptr->zbuf;
 81.1245 +      png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
 81.1246 +
 81.1247 +      key_size = text - key;
 81.1248 +      text_size = 0;
 81.1249 +      text = NULL;
 81.1250 +      ret = Z_STREAM_END;
 81.1251 +
 81.1252 +      while (png_ptr->zstream.avail_in)
 81.1253 +      {
 81.1254 +         ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);
 81.1255 +         if (ret != Z_OK && ret != Z_STREAM_END)
 81.1256 +         {
 81.1257 +            inflateReset(&png_ptr->zstream);
 81.1258 +            png_ptr->zstream.avail_in = 0;
 81.1259 +            png_ptr->current_text = NULL;
 81.1260 +            png_free(png_ptr, key);
 81.1261 +            png_free(png_ptr, text);
 81.1262 +            return;
 81.1263 +         }
 81.1264 +         if (!(png_ptr->zstream.avail_out) || ret == Z_STREAM_END)
 81.1265 +         {
 81.1266 +            if (text == NULL)
 81.1267 +            {
 81.1268 +               text = (png_charp)png_malloc(png_ptr,
 81.1269 +                     (png_uint_32)(png_ptr->zbuf_size
 81.1270 +                     - png_ptr->zstream.avail_out + key_size + 1));
 81.1271 +               png_memcpy(text + key_size, png_ptr->zbuf,
 81.1272 +                  png_ptr->zbuf_size - png_ptr->zstream.avail_out);
 81.1273 +               png_memcpy(text, key, key_size);
 81.1274 +               text_size = key_size + png_ptr->zbuf_size -
 81.1275 +                  png_ptr->zstream.avail_out;
 81.1276 +               *(text + text_size) = '\0';
 81.1277 +            }
 81.1278 +            else
 81.1279 +            {
 81.1280 +               png_charp tmp;
 81.1281 +
 81.1282 +               tmp = text;
 81.1283 +               text = (png_charp)png_malloc(png_ptr, text_size +
 81.1284 +                  (png_uint_32)(png_ptr->zbuf_size 
 81.1285 +                  - png_ptr->zstream.avail_out + 1));
 81.1286 +               png_memcpy(text, tmp, text_size);
 81.1287 +               png_free(png_ptr, tmp);
 81.1288 +               png_memcpy(text + text_size, png_ptr->zbuf,
 81.1289 +                  png_ptr->zbuf_size - png_ptr->zstream.avail_out);
 81.1290 +               text_size += png_ptr->zbuf_size - png_ptr->zstream.avail_out;
 81.1291 +               *(text + text_size) = '\0';
 81.1292 +            }
 81.1293 +            if (ret != Z_STREAM_END)
 81.1294 +            {
 81.1295 +               png_ptr->zstream.next_out = png_ptr->zbuf;
 81.1296 +               png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
 81.1297 +            }
 81.1298 +         }
 81.1299 +         else
 81.1300 +         {
 81.1301 +            break;
 81.1302 +         }
 81.1303 +
 81.1304 +         if (ret == Z_STREAM_END)
 81.1305 +            break;
 81.1306 +      }
 81.1307 +
 81.1308 +      inflateReset(&png_ptr->zstream);
 81.1309 +      png_ptr->zstream.avail_in = 0;
 81.1310 +
 81.1311 +      if (ret != Z_STREAM_END)
 81.1312 +      {
 81.1313 +         png_ptr->current_text = NULL;
 81.1314 +         png_free(png_ptr, key);
 81.1315 +         png_free(png_ptr, text);
 81.1316 +         return;
 81.1317 +      }
 81.1318 +
 81.1319 +      png_ptr->current_text = NULL;
 81.1320 +      png_free(png_ptr, key);
 81.1321 +      key = text;
 81.1322 +      text += key_size;
 81.1323 +
 81.1324 +      text_ptr = (png_textp)png_malloc(png_ptr,
 81.1325 +          (png_uint_32)png_sizeof(png_text));
 81.1326 +      text_ptr->compression = PNG_TEXT_COMPRESSION_zTXt;
 81.1327 +      text_ptr->key = key;
 81.1328 +#ifdef PNG_iTXt_SUPPORTED
 81.1329 +      text_ptr->lang = NULL;
 81.1330 +      text_ptr->lang_key = NULL;
 81.1331 +#endif
 81.1332 +      text_ptr->text = text;
 81.1333 +
 81.1334 +      ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1);
 81.1335 +
 81.1336 +      png_free(png_ptr, key);
 81.1337 +      png_free(png_ptr, text_ptr);
 81.1338 +
 81.1339 +      if (ret)
 81.1340 +        png_warning(png_ptr, "Insufficient memory to store text chunk.");
 81.1341 +   }
 81.1342 +}
 81.1343 +#endif
 81.1344 +
 81.1345 +#if defined(PNG_READ_iTXt_SUPPORTED)
 81.1346 +void /* PRIVATE */
 81.1347 +png_push_handle_iTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32
 81.1348 +   length)
 81.1349 +{
 81.1350 +   if (!(png_ptr->mode & PNG_HAVE_IHDR) || (png_ptr->mode & PNG_HAVE_IEND))
 81.1351 +      {
 81.1352 +         png_error(png_ptr, "Out of place iTXt");
 81.1353 +         info_ptr = info_ptr; /* to quiet some compiler warnings */
 81.1354 +      }
 81.1355 +
 81.1356 +#ifdef PNG_MAX_MALLOC_64K
 81.1357 +   png_ptr->skip_length = 0;  /* This may not be necessary */
 81.1358 +
 81.1359 +   if (length > (png_uint_32)65535L) /* Can't hold entire string in memory */
 81.1360 +   {
 81.1361 +      png_warning(png_ptr, "iTXt chunk too large to fit in memory");
 81.1362 +      png_ptr->skip_length = length - (png_uint_32)65535L;
 81.1363 +      length = (png_uint_32)65535L;
 81.1364 +   }
 81.1365 +#endif
 81.1366 +
 81.1367 +   png_ptr->current_text = (png_charp)png_malloc(png_ptr,
 81.1368 +      (png_uint_32)(length + 1));
 81.1369 +   png_ptr->current_text[length] = '\0';
 81.1370 +   png_ptr->current_text_ptr = png_ptr->current_text;
 81.1371 +   png_ptr->current_text_size = (png_size_t)length;
 81.1372 +   png_ptr->current_text_left = (png_size_t)length;
 81.1373 +   png_ptr->process_mode = PNG_READ_iTXt_MODE;
 81.1374 +}
 81.1375 +
 81.1376 +void /* PRIVATE */
 81.1377 +png_push_read_iTXt(png_structp png_ptr, png_infop info_ptr)
 81.1378 +{
 81.1379 +
 81.1380 +   if (png_ptr->buffer_size && png_ptr->current_text_left)
 81.1381 +   {
 81.1382 +      png_size_t text_size;
 81.1383 +
 81.1384 +      if (png_ptr->buffer_size < png_ptr->current_text_left)
 81.1385 +         text_size = png_ptr->buffer_size;
 81.1386 +      else
 81.1387 +         text_size = png_ptr->current_text_left;
 81.1388 +      png_crc_read(png_ptr, (png_bytep)png_ptr->current_text_ptr, text_size);
 81.1389 +      png_ptr->current_text_left -= text_size;
 81.1390 +      png_ptr->current_text_ptr += text_size;
 81.1391 +   }
 81.1392 +   if (!(png_ptr->current_text_left))
 81.1393 +   {
 81.1394 +      png_textp text_ptr;
 81.1395 +      png_charp key;
 81.1396 +      int comp_flag;
 81.1397 +      png_charp lang;
 81.1398 +      png_charp lang_key;
 81.1399 +      png_charp text;
 81.1400 +      int ret;
 81.1401 +
 81.1402 +      if (png_ptr->buffer_size < 4)
 81.1403 +      {
 81.1404 +         png_push_save_buffer(png_ptr);
 81.1405 +         return;
 81.1406 +      }
 81.1407 +
 81.1408 +      png_push_crc_finish(png_ptr);
 81.1409 +
 81.1410 +#if defined(PNG_MAX_MALLOC_64K)
 81.1411 +      if (png_ptr->skip_length)
 81.1412 +         return;
 81.1413 +#endif
 81.1414 +
 81.1415 +      key = png_ptr->current_text;
 81.1416 +
 81.1417 +      for (lang = key; *lang; lang++)
 81.1418 +         /* empty loop */ ;
 81.1419 +
 81.1420 +      if (lang < key + png_ptr->current_text_size - 3)
 81.1421 +         lang++;
 81.1422 +
 81.1423 +      comp_flag = *lang++;
 81.1424 +      lang++;     /* skip comp_type, always zero */
 81.1425 +
 81.1426 +      for (lang_key = lang; *lang_key; lang_key++)
 81.1427 +         /* empty loop */ ;
 81.1428 +      lang_key++;        /* skip NUL separator */
 81.1429 +
 81.1430 +      text=lang_key;
 81.1431 +      if (lang_key < key + png_ptr->current_text_size - 1)
 81.1432 +      {
 81.1433 +        for (; *text; text++)
 81.1434 +           /* empty loop */ ;
 81.1435 +      }
 81.1436 +
 81.1437 +      if (text < key + png_ptr->current_text_size)
 81.1438 +         text++;
 81.1439 +
 81.1440 +      text_ptr = (png_textp)png_malloc(png_ptr,
 81.1441 +         (png_uint_32)png_sizeof(png_text));
 81.1442 +      text_ptr->compression = comp_flag + 2;
 81.1443 +      text_ptr->key = key;
 81.1444 +      text_ptr->lang = lang;
 81.1445 +      text_ptr->lang_key = lang_key;
 81.1446 +      text_ptr->text = text;
 81.1447 +      text_ptr->text_length = 0;
 81.1448 +      text_ptr->itxt_length = png_strlen(text);
 81.1449 +
 81.1450 +      ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1);
 81.1451 +
 81.1452 +      png_ptr->current_text = NULL;
 81.1453 +
 81.1454 +      png_free(png_ptr, text_ptr);
 81.1455 +      if (ret)
 81.1456 +        png_warning(png_ptr, "Insufficient memory to store iTXt chunk.");
 81.1457 +   }
 81.1458 +}
 81.1459 +#endif
 81.1460 +
 81.1461 +/* This function is called when we haven't found a handler for this
 81.1462 + * chunk.  If there isn't a problem with the chunk itself (ie a bad chunk
 81.1463 + * name or a critical chunk), the chunk is (currently) silently ignored.
 81.1464 + */
 81.1465 +void /* PRIVATE */
 81.1466 +png_push_handle_unknown(png_structp png_ptr, png_infop info_ptr, png_uint_32
 81.1467 +   length)
 81.1468 +{
 81.1469 +   png_uint_32 skip = 0;
 81.1470 +
 81.1471 +   if (!(png_ptr->chunk_name[0] & 0x20))
 81.1472 +   {
 81.1473 +#if defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
 81.1474 +      if (png_handle_as_unknown(png_ptr, png_ptr->chunk_name) !=
 81.1475 +         PNG_HANDLE_CHUNK_ALWAYS
 81.1476 +#if defined(PNG_READ_USER_CHUNKS_SUPPORTED)
 81.1477 +         && png_ptr->read_user_chunk_fn == NULL
 81.1478 +#endif
 81.1479 +         )
 81.1480 +#endif
 81.1481 +         png_chunk_error(png_ptr, "unknown critical chunk");
 81.1482 +
 81.1483 +      info_ptr = info_ptr; /* to quiet some compiler warnings */
 81.1484 +   }
 81.1485 +
 81.1486 +#if defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
 81.1487 +   if (png_ptr->flags & PNG_FLAG_KEEP_UNKNOWN_CHUNKS)
 81.1488 +   {
 81.1489 +#ifdef PNG_MAX_MALLOC_64K
 81.1490 +      if (length > (png_uint_32)65535L)
 81.1491 +      {
 81.1492 +          png_warning(png_ptr, "unknown chunk too large to fit in memory");
 81.1493 +          skip = length - (png_uint_32)65535L;
 81.1494 +          length = (png_uint_32)65535L;
 81.1495 +      }
 81.1496 +#endif
 81.1497 +      png_memcpy((png_charp)png_ptr->unknown_chunk.name,
 81.1498 +                 (png_charp)png_ptr->chunk_name, 
 81.1499 +                 png_sizeof(png_ptr->unknown_chunk.name));
 81.1500 +      png_ptr->unknown_chunk.name[png_sizeof(png_ptr->unknown_chunk.name) - 1]
 81.1501 +        = '\0';
 81.1502 +
 81.1503 +      png_ptr->unknown_chunk.size = (png_size_t)length;
 81.1504 +      if (length == 0)
 81.1505 +         png_ptr->unknown_chunk.data = NULL;
 81.1506 +      else
 81.1507 +      {
 81.1508 +         png_ptr->unknown_chunk.data = (png_bytep)png_malloc(png_ptr,
 81.1509 +       (png_uint_32)length);
 81.1510 +         png_crc_read(png_ptr, (png_bytep)png_ptr->unknown_chunk.data, length);
 81.1511 +      }
 81.1512 +#if defined(PNG_READ_USER_CHUNKS_SUPPORTED)
 81.1513 +      if (png_ptr->read_user_chunk_fn != NULL)
 81.1514 +      {
 81.1515 +         /* callback to user unknown chunk handler */
 81.1516 +         int ret;
 81.1517 +         ret = (*(png_ptr->read_user_chunk_fn))
 81.1518 +           (png_ptr, &png_ptr->unknown_chunk);
 81.1519 +         if (ret < 0)
 81.1520 +            png_chunk_error(png_ptr, "error in user chunk");
 81.1521 +         if (ret == 0)
 81.1522 +         {
 81.1523 +            if (!(png_ptr->chunk_name[0] & 0x20))
 81.1524 +               if (png_handle_as_unknown(png_ptr, png_ptr->chunk_name) !=
 81.1525 +                    PNG_HANDLE_CHUNK_ALWAYS)
 81.1526 +                  png_chunk_error(png_ptr, "unknown critical chunk");
 81.1527 +            png_set_unknown_chunks(png_ptr, info_ptr,
 81.1528 +               &png_ptr->unknown_chunk, 1);
 81.1529 +         }
 81.1530 +      }
 81.1531 +      else
 81.1532 +#endif
 81.1533 +        png_set_unknown_chunks(png_ptr, info_ptr, &png_ptr->unknown_chunk, 1);
 81.1534 +      png_free(png_ptr, png_ptr->unknown_chunk.data);
 81.1535 +      png_ptr->unknown_chunk.data = NULL;
 81.1536 +   }
 81.1537 +   else
 81.1538 +#endif
 81.1539 +      skip=length;
 81.1540 +   png_push_crc_skip(png_ptr, skip);
 81.1541 +}
 81.1542 +
 81.1543 +void /* PRIVATE */
 81.1544 +png_push_have_info(png_structp png_ptr, png_infop info_ptr)
 81.1545 +{
 81.1546 +   if (png_ptr->info_fn != NULL)
 81.1547 +      (*(png_ptr->info_fn))(png_ptr, info_ptr);
 81.1548 +}
 81.1549 +
 81.1550 +void /* PRIVATE */
 81.1551 +png_push_have_end(png_structp png_ptr, png_infop info_ptr)
 81.1552 +{
 81.1553 +   if (png_ptr->end_fn != NULL)
 81.1554 +      (*(png_ptr->end_fn))(png_ptr, info_ptr);
 81.1555 +}
 81.1556 +
 81.1557 +void /* PRIVATE */
 81.1558 +png_push_have_row(png_structp png_ptr, png_bytep row)
 81.1559 +{
 81.1560 +   if (png_ptr->row_fn != NULL)
 81.1561 +      (*(png_ptr->row_fn))(png_ptr, row, png_ptr->row_number,
 81.1562 +         (int)png_ptr->pass);
 81.1563 +}
 81.1564 +
 81.1565 +void PNGAPI
 81.1566 +png_progressive_combine_row (png_structp png_ptr,
 81.1567 +   png_bytep old_row, png_bytep new_row)
 81.1568 +{
 81.1569 +#ifdef PNG_USE_LOCAL_ARRAYS
 81.1570 +   PNG_CONST int FARDATA png_pass_dsp_mask[7] =
 81.1571 +      {0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff};
 81.1572 +#endif
 81.1573 +   if (png_ptr == NULL) return;
 81.1574 +   if (new_row != NULL)    /* new_row must == png_ptr->row_buf here. */
 81.1575 +      png_combine_row(png_ptr, old_row, png_pass_dsp_mask[png_ptr->pass]);
 81.1576 +}
 81.1577 +
 81.1578 +void PNGAPI
 81.1579 +png_set_progressive_read_fn(png_structp png_ptr, png_voidp progressive_ptr,
 81.1580 +   png_progressive_info_ptr info_fn, png_progressive_row_ptr row_fn,
 81.1581 +   png_progressive_end_ptr end_fn)
 81.1582 +{
 81.1583 +   if (png_ptr == NULL) return;
 81.1584 +   png_ptr->info_fn = info_fn;
 81.1585 +   png_ptr->row_fn = row_fn;
 81.1586 +   png_ptr->end_fn = end_fn;
 81.1587 +
 81.1588 +   png_set_read_fn(png_ptr, progressive_ptr, png_push_fill_buffer);
 81.1589 +}
 81.1590 +
 81.1591 +png_voidp PNGAPI
 81.1592 +png_get_progressive_ptr(png_structp png_ptr)
 81.1593 +{
 81.1594 +   if (png_ptr == NULL) return (NULL);
 81.1595 +   return png_ptr->io_ptr;
 81.1596 +}
 81.1597 +#endif /* PNG_PROGRESSIVE_READ_SUPPORTED */
    82.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    82.2 +++ b/libs/libpng/pngread.c	Sat Sep 19 05:51:51 2015 +0300
    82.3 @@ -0,0 +1,1459 @@
    82.4 +
    82.5 +/* pngread.c - read a PNG file
    82.6 + *
    82.7 + * Last changed in libpng 1.2.30 [August 15, 2008]
    82.8 + * For conditions of distribution and use, see copyright notice in png.h
    82.9 + * Copyright (c) 1998-2008 Glenn Randers-Pehrson
   82.10 + * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
   82.11 + * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
   82.12 + *
   82.13 + * This file contains routines that an application calls directly to
   82.14 + * read a PNG file or stream.
   82.15 + */
   82.16 +
   82.17 +#define PNG_INTERNAL
   82.18 +#include "png.h"
   82.19 +#if defined(PNG_READ_SUPPORTED)
   82.20 +
   82.21 +/* Create a PNG structure for reading, and allocate any memory needed. */
   82.22 +png_structp PNGAPI
   82.23 +png_create_read_struct(png_const_charp user_png_ver, png_voidp error_ptr,
   82.24 +   png_error_ptr error_fn, png_error_ptr warn_fn)
   82.25 +{
   82.26 +
   82.27 +#ifdef PNG_USER_MEM_SUPPORTED
   82.28 +   return (png_create_read_struct_2(user_png_ver, error_ptr, error_fn,
   82.29 +      warn_fn, png_voidp_NULL, png_malloc_ptr_NULL, png_free_ptr_NULL));
   82.30 +}
   82.31 +
   82.32 +/* Alternate create PNG structure for reading, and allocate any memory needed. */
   82.33 +png_structp PNGAPI
   82.34 +png_create_read_struct_2(png_const_charp user_png_ver, png_voidp error_ptr,
   82.35 +   png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
   82.36 +   png_malloc_ptr malloc_fn, png_free_ptr free_fn)
   82.37 +{
   82.38 +#endif /* PNG_USER_MEM_SUPPORTED */
   82.39 +
   82.40 +#ifdef PNG_SETJMP_SUPPORTED
   82.41 +   volatile
   82.42 +#endif
   82.43 +   png_structp png_ptr;
   82.44 +
   82.45 +#ifdef PNG_SETJMP_SUPPORTED
   82.46 +#ifdef USE_FAR_KEYWORD
   82.47 +   jmp_buf jmpbuf;
   82.48 +#endif
   82.49 +#endif
   82.50 +
   82.51 +   int i;
   82.52 +
   82.53 +   png_debug(1, "in png_create_read_struct\n");
   82.54 +#ifdef PNG_USER_MEM_SUPPORTED
   82.55 +   png_ptr = (png_structp)png_create_struct_2(PNG_STRUCT_PNG,
   82.56 +      (png_malloc_ptr)malloc_fn, (png_voidp)mem_ptr);
   82.57 +#else
   82.58 +   png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG);
   82.59 +#endif
   82.60 +   if (png_ptr == NULL)
   82.61 +      return (NULL);
   82.62 +
   82.63 +   /* added at libpng-1.2.6 */
   82.64 +#ifdef PNG_SET_USER_LIMITS_SUPPORTED
   82.65 +   png_ptr->user_width_max=PNG_USER_WIDTH_MAX;
   82.66 +   png_ptr->user_height_max=PNG_USER_HEIGHT_MAX;
   82.67 +#endif
   82.68 +
   82.69 +#ifdef PNG_SETJMP_SUPPORTED
   82.70 +#ifdef USE_FAR_KEYWORD
   82.71 +   if (setjmp(jmpbuf))
   82.72 +#else
   82.73 +   if (setjmp(png_ptr->jmpbuf))
   82.74 +#endif
   82.75 +   {
   82.76 +      png_free(png_ptr, png_ptr->zbuf);
   82.77 +      png_ptr->zbuf = NULL;
   82.78 +#ifdef PNG_USER_MEM_SUPPORTED
   82.79 +      png_destroy_struct_2((png_voidp)png_ptr,
   82.80 +         (png_free_ptr)free_fn, (png_voidp)mem_ptr);
   82.81 +#else
   82.82 +      png_destroy_struct((png_voidp)png_ptr);
   82.83 +#endif
   82.84 +      return (NULL);
   82.85 +   }
   82.86 +#ifdef USE_FAR_KEYWORD
   82.87 +   png_memcpy(png_ptr->jmpbuf, jmpbuf, png_sizeof(jmp_buf));
   82.88 +#endif
   82.89 +#endif
   82.90 +
   82.91 +#ifdef PNG_USER_MEM_SUPPORTED
   82.92 +   png_set_mem_fn(png_ptr, mem_ptr, malloc_fn, free_fn);
   82.93 +#endif
   82.94 +
   82.95 +   png_set_error_fn(png_ptr, error_ptr, error_fn, warn_fn);
   82.96 +
   82.97 +   if (user_png_ver)
   82.98 +   {
   82.99 +     i = 0;
  82.100 +     do
  82.101 +     {
  82.102 +       if (user_png_ver[i] != png_libpng_ver[i])
  82.103 +          png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
  82.104 +     } while (png_libpng_ver[i++]);
  82.105 +   }
  82.106 +   else
  82.107 +        png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
  82.108 +   
  82.109 +
  82.110 +   if (png_ptr->flags & PNG_FLAG_LIBRARY_MISMATCH)
  82.111 +   {
  82.112 +     /* Libpng 0.90 and later are binary incompatible with libpng 0.89, so
  82.113 +      * we must recompile any applications that use any older library version.
  82.114 +      * For versions after libpng 1.0, we will be compatible, so we need
  82.115 +      * only check the first digit.
  82.116 +      */
  82.117 +     if (user_png_ver == NULL || user_png_ver[0] != png_libpng_ver[0] ||
  82.118 +         (user_png_ver[0] == '1' && user_png_ver[2] != png_libpng_ver[2]) ||
  82.119 +         (user_png_ver[0] == '0' && user_png_ver[2] < '9'))
  82.120 +     {
  82.121 +#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
  82.122 +        char msg[80];
  82.123 +        if (user_png_ver)
  82.124 +        {
  82.125 +          png_snprintf(msg, 80,
  82.126 +             "Application was compiled with png.h from libpng-%.20s",
  82.127 +             user_png_ver);
  82.128 +          png_warning(png_ptr, msg);
  82.129 +        }
  82.130 +        png_snprintf(msg, 80,
  82.131 +             "Application  is  running with png.c from libpng-%.20s",
  82.132 +           png_libpng_ver);
  82.133 +        png_warning(png_ptr, msg);
  82.134 +#endif
  82.135 +#ifdef PNG_ERROR_NUMBERS_SUPPORTED
  82.136 +        png_ptr->flags = 0;
  82.137 +#endif
  82.138 +        png_error(png_ptr,
  82.139 +           "Incompatible libpng version in application and library");
  82.140 +     }
  82.141 +   }
  82.142 +
  82.143 +   /* initialize zbuf - compression buffer */
  82.144 +   png_ptr->zbuf_size = PNG_ZBUF_SIZE;
  82.145 +   png_ptr->zbuf = (png_bytep)png_malloc(png_ptr,
  82.146 +     (png_uint_32)png_ptr->zbuf_size);
  82.147 +   png_ptr->zstream.zalloc = png_zalloc;
  82.148 +   png_ptr->zstream.zfree = png_zfree;
  82.149 +   png_ptr->zstream.opaque = (voidpf)png_ptr;
  82.150 +
  82.151 +   switch (inflateInit(&png_ptr->zstream))
  82.152 +   {
  82.153 +     case Z_OK: /* Do nothing */ break;
  82.154 +     case Z_MEM_ERROR:
  82.155 +     case Z_STREAM_ERROR: png_error(png_ptr, "zlib memory error"); break;
  82.156 +     case Z_VERSION_ERROR: png_error(png_ptr, "zlib version error"); break;
  82.157 +     default: png_error(png_ptr, "Unknown zlib error");
  82.158 +   }
  82.159 +
  82.160 +   png_ptr->zstream.next_out = png_ptr->zbuf;
  82.161 +   png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
  82.162 +
  82.163 +   png_set_read_fn(png_ptr, png_voidp_NULL, png_rw_ptr_NULL);
  82.164 +
  82.165 +#ifdef PNG_SETJMP_SUPPORTED
  82.166 +/* Applications that neglect to set up their own setjmp() and then encounter
  82.167 +   a png_error() will longjmp here.  Since the jmpbuf is then meaningless we
  82.168 +   abort instead of returning. */
  82.169 +#ifdef USE_FAR_KEYWORD
  82.170 +   if (setjmp(jmpbuf))
  82.171 +      PNG_ABORT();
  82.172 +   png_memcpy(png_ptr->jmpbuf, jmpbuf, png_sizeof(jmp_buf));
  82.173 +#else
  82.174 +   if (setjmp(png_ptr->jmpbuf))
  82.175 +      PNG_ABORT();
  82.176 +#endif
  82.177 +#endif
  82.178 +   return (png_ptr);
  82.179 +}
  82.180 +
  82.181 +#if defined(PNG_1_0_X) || defined(PNG_1_2_X)
  82.182 +/* Initialize PNG structure for reading, and allocate any memory needed.
  82.183 +   This interface is deprecated in favour of the png_create_read_struct(),
  82.184 +   and it will disappear as of libpng-1.3.0. */
  82.185 +#undef png_read_init
  82.186 +void PNGAPI
  82.187 +png_read_init(png_structp png_ptr)
  82.188 +{
  82.189 +   /* We only come here via pre-1.0.7-compiled applications */
  82.190 +   png_read_init_2(png_ptr, "1.0.6 or earlier", 0, 0);
  82.191 +}
  82.192 +
  82.193 +void PNGAPI
  82.194 +png_read_init_2(png_structp png_ptr, png_const_charp user_png_ver,
  82.195 +   png_size_t png_struct_size, png_size_t png_info_size)
  82.196 +{
  82.197 +   /* We only come here via pre-1.0.12-compiled applications */
  82.198 +   if (png_ptr == NULL) return;
  82.199 +#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
  82.200 +   if (png_sizeof(png_struct) > png_struct_size ||
  82.201 +      png_sizeof(png_info) > png_info_size)
  82.202 +   {
  82.203 +      char msg[80];
  82.204 +      png_ptr->warning_fn = NULL;
  82.205 +      if (user_png_ver)
  82.206 +      {
  82.207 +        png_snprintf(msg, 80,
  82.208 +           "Application was compiled with png.h from libpng-%.20s",
  82.209 +           user_png_ver);
  82.210 +        png_warning(png_ptr, msg);
  82.211 +      }
  82.212 +      png_snprintf(msg, 80,
  82.213 +         "Application  is  running with png.c from libpng-%.20s",
  82.214 +         png_libpng_ver);
  82.215 +      png_warning(png_ptr, msg);
  82.216 +   }
  82.217 +#endif
  82.218 +   if (png_sizeof(png_struct) > png_struct_size)
  82.219 +     {
  82.220 +       png_ptr->error_fn = NULL;
  82.221 +#ifdef PNG_ERROR_NUMBERS_SUPPORTED
  82.222 +       png_ptr->flags = 0;
  82.223 +#endif
  82.224 +       png_error(png_ptr,
  82.225 +       "The png struct allocated by the application for reading is too small.");
  82.226 +     }
  82.227 +   if (png_sizeof(png_info) > png_info_size)
  82.228 +     {
  82.229 +       png_ptr->error_fn = NULL;
  82.230 +#ifdef PNG_ERROR_NUMBERS_SUPPORTED
  82.231 +       png_ptr->flags = 0;
  82.232 +#endif
  82.233 +       png_error(png_ptr,
  82.234 +         "The info struct allocated by application for reading is too small.");
  82.235 +     }
  82.236 +   png_read_init_3(&png_ptr, user_png_ver, png_struct_size);
  82.237 +}
  82.238 +#endif /* PNG_1_0_X || PNG_1_2_X */
  82.239 +
  82.240 +void PNGAPI
  82.241 +png_read_init_3(png_structpp ptr_ptr, png_const_charp user_png_ver,
  82.242 +   png_size_t png_struct_size)
  82.243 +{
  82.244 +#ifdef PNG_SETJMP_SUPPORTED
  82.245 +   jmp_buf tmp_jmp;  /* to save current jump buffer */
  82.246 +#endif
  82.247 +
  82.248 +   int i = 0;
  82.249 +
  82.250 +   png_structp png_ptr=*ptr_ptr;
  82.251 +
  82.252 +   if (png_ptr == NULL) return;
  82.253 +
  82.254 +   do
  82.255 +   {
  82.256 +     if (user_png_ver[i] != png_libpng_ver[i])
  82.257 +     {
  82.258 +#ifdef PNG_LEGACY_SUPPORTED
  82.259 +       png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
  82.260 +#else
  82.261 +       png_ptr->warning_fn = NULL;
  82.262 +       png_warning(png_ptr,
  82.263 +        "Application uses deprecated png_read_init() and should be recompiled.");
  82.264 +       break;
  82.265 +#endif
  82.266 +     }
  82.267 +   } while (png_libpng_ver[i++]);
  82.268 +
  82.269 +   png_debug(1, "in png_read_init_3\n");
  82.270 +
  82.271 +#ifdef PNG_SETJMP_SUPPORTED
  82.272 +   /* save jump buffer and error functions */
  82.273 +   png_memcpy(tmp_jmp, png_ptr->jmpbuf, png_sizeof(jmp_buf));
  82.274 +#endif
  82.275 +
  82.276 +   if (png_sizeof(png_struct) > png_struct_size)
  82.277 +   {
  82.278 +      png_destroy_struct(png_ptr);
  82.279 +      *ptr_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG);
  82.280 +      png_ptr = *ptr_ptr;
  82.281 +   }
  82.282 +
  82.283 +   /* reset all variables to 0 */
  82.284 +   png_memset(png_ptr, 0, png_sizeof(png_struct));
  82.285 +
  82.286 +#ifdef PNG_SETJMP_SUPPORTED
  82.287 +   /* restore jump buffer */
  82.288 +   png_memcpy(png_ptr->jmpbuf, tmp_jmp, png_sizeof(jmp_buf));
  82.289 +#endif
  82.290 +
  82.291 +   /* added at libpng-1.2.6 */
  82.292 +#ifdef PNG_SET_USER_LIMITS_SUPPORTED
  82.293 +   png_ptr->user_width_max=PNG_USER_WIDTH_MAX;
  82.294 +   png_ptr->user_height_max=PNG_USER_HEIGHT_MAX;
  82.295 +#endif
  82.296 +
  82.297 +   /* initialize zbuf - compression buffer */
  82.298 +   png_ptr->zbuf_size = PNG_ZBUF_SIZE;
  82.299 +   png_ptr->zbuf = (png_bytep)png_malloc(png_ptr,
  82.300 +     (png_uint_32)png_ptr->zbuf_size);
  82.301 +   png_ptr->zstream.zalloc = png_zalloc;
  82.302 +   png_ptr->zstream.zfree = png_zfree;
  82.303 +   png_ptr->zstream.opaque = (voidpf)png_ptr;
  82.304 +
  82.305 +   switch (inflateInit(&png_ptr->zstream))
  82.306 +   {
  82.307 +     case Z_OK: /* Do nothing */ break;
  82.308 +     case Z_MEM_ERROR:
  82.309 +     case Z_STREAM_ERROR: png_error(png_ptr, "zlib memory"); break;
  82.310 +     case Z_VERSION_ERROR: png_error(png_ptr, "zlib version"); break;
  82.311 +     default: png_error(png_ptr, "Unknown zlib error");
  82.312 +   }
  82.313 +
  82.314 +   png_ptr->zstream.next_out = png_ptr->zbuf;
  82.315 +   png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
  82.316 +
  82.317 +   png_set_read_fn(png_ptr, png_voidp_NULL, png_rw_ptr_NULL);
  82.318 +}
  82.319 +
  82.320 +#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
  82.321 +/* Read the information before the actual image data.  This has been
  82.322 + * changed in v0.90 to allow reading a file that already has the magic
  82.323 + * bytes read from the stream.  You can tell libpng how many bytes have
  82.324 + * been read from the beginning of the stream (up to the maximum of 8)
  82.325 + * via png_set_sig_bytes(), and we will only check the remaining bytes
  82.326 + * here.  The application can then have access to the signature bytes we
  82.327 + * read if it is determined that this isn't a valid PNG file.
  82.328 + */
  82.329 +void PNGAPI
  82.330 +png_read_info(png_structp png_ptr, png_infop info_ptr)
  82.331 +{
  82.332 +   if (png_ptr == NULL || info_ptr == NULL) return;
  82.333 +   png_debug(1, "in png_read_info\n");
  82.334 +   /* If we haven't checked all of the PNG signature bytes, do so now. */
  82.335 +   if (png_ptr->sig_bytes < 8)
  82.336 +   {
  82.337 +      png_size_t num_checked = png_ptr->sig_bytes,
  82.338 +                 num_to_check = 8 - num_checked;
  82.339 +
  82.340 +      png_read_data(png_ptr, &(info_ptr->signature[num_checked]), num_to_check);
  82.341 +      png_ptr->sig_bytes = 8;
  82.342 +
  82.343 +      if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check))
  82.344 +      {
  82.345 +         if (num_checked < 4 &&
  82.346 +             png_sig_cmp(info_ptr->signature, num_checked, num_to_check - 4))
  82.347 +            png_error(png_ptr, "Not a PNG file");
  82.348 +         else
  82.349 +            png_error(png_ptr, "PNG file corrupted by ASCII conversion");
  82.350 +      }
  82.351 +      if (num_checked < 3)
  82.352 +         png_ptr->mode |= PNG_HAVE_PNG_SIGNATURE;
  82.353 +   }
  82.354 +
  82.355 +   for (;;)
  82.356 +   {
  82.357 +#ifdef PNG_USE_LOCAL_ARRAYS
  82.358 +      PNG_CONST PNG_IHDR;
  82.359 +      PNG_CONST PNG_IDAT;
  82.360 +      PNG_CONST PNG_IEND;
  82.361 +      PNG_CONST PNG_PLTE;
  82.362 +#if defined(PNG_READ_bKGD_SUPPORTED)
  82.363 +      PNG_CONST PNG_bKGD;
  82.364 +#endif
  82.365 +#if defined(PNG_READ_cHRM_SUPPORTED)
  82.366 +      PNG_CONST PNG_cHRM;
  82.367 +#endif
  82.368 +#if defined(PNG_READ_gAMA_SUPPORTED)
  82.369 +      PNG_CONST PNG_gAMA;
  82.370 +#endif
  82.371 +#if defined(PNG_READ_hIST_SUPPORTED)
  82.372 +      PNG_CONST PNG_hIST;
  82.373 +#endif
  82.374 +#if defined(PNG_READ_iCCP_SUPPORTED)
  82.375 +      PNG_CONST PNG_iCCP;
  82.376 +#endif
  82.377 +#if defined(PNG_READ_iTXt_SUPPORTED)
  82.378 +      PNG_CONST PNG_iTXt;
  82.379 +#endif
  82.380 +#if defined(PNG_READ_oFFs_SUPPORTED)
  82.381 +      PNG_CONST PNG_oFFs;
  82.382 +#endif
  82.383 +#if defined(PNG_READ_pCAL_SUPPORTED)
  82.384 +      PNG_CONST PNG_pCAL;
  82.385 +#endif
  82.386 +#if defined(PNG_READ_pHYs_SUPPORTED)
  82.387 +      PNG_CONST PNG_pHYs;
  82.388 +#endif
  82.389 +#if defined(PNG_READ_sBIT_SUPPORTED)
  82.390 +      PNG_CONST PNG_sBIT;
  82.391 +#endif
  82.392 +#if defined(PNG_READ_sCAL_SUPPORTED)
  82.393 +      PNG_CONST PNG_sCAL;
  82.394 +#endif
  82.395 +#if defined(PNG_READ_sPLT_SUPPORTED)
  82.396 +      PNG_CONST PNG_sPLT;
  82.397 +#endif
  82.398 +#if defined(PNG_READ_sRGB_SUPPORTED)
  82.399 +      PNG_CONST PNG_sRGB;
  82.400 +#endif
  82.401 +#if defined(PNG_READ_tEXt_SUPPORTED)
  82.402 +      PNG_CONST PNG_tEXt;
  82.403 +#endif
  82.404 +#if defined(PNG_READ_tIME_SUPPORTED)
  82.405 +      PNG_CONST PNG_tIME;
  82.406 +#endif
  82.407 +#if defined(PNG_READ_tRNS_SUPPORTED)
  82.408 +      PNG_CONST PNG_tRNS;
  82.409 +#endif
  82.410 +#if defined(PNG_READ_zTXt_SUPPORTED)
  82.411 +      PNG_CONST PNG_zTXt;
  82.412 +#endif
  82.413 +#endif /* PNG_USE_LOCAL_ARRAYS */
  82.414 +      png_uint_32 length = png_read_chunk_header(png_ptr);
  82.415 +      PNG_CONST png_bytep chunk_name = png_ptr->chunk_name;
  82.416 +
  82.417 +      /* This should be a binary subdivision search or a hash for
  82.418 +       * matching the chunk name rather than a linear search.
  82.419 +       */
  82.420 +      if (!png_memcmp(chunk_name, png_IDAT, 4))
  82.421 +        if (png_ptr->mode & PNG_AFTER_IDAT)
  82.422 +          png_ptr->mode |= PNG_HAVE_CHUNK_AFTER_IDAT;
  82.423 +
  82.424 +      if (!png_memcmp(chunk_name, png_IHDR, 4))
  82.425 +         png_handle_IHDR(png_ptr, info_ptr, length);
  82.426 +      else if (!png_memcmp(chunk_name, png_IEND, 4))
  82.427 +         png_handle_IEND(png_ptr, info_ptr, length);
  82.428 +#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
  82.429 +      else if (png_handle_as_unknown(png_ptr, chunk_name))
  82.430 +      {
  82.431 +         if (!png_memcmp(chunk_name, png_IDAT, 4))
  82.432 +            png_ptr->mode |= PNG_HAVE_IDAT;
  82.433 +         png_handle_unknown(png_ptr, info_ptr, length);
  82.434 +         if (!png_memcmp(chunk_name, png_PLTE, 4))
  82.435 +            png_ptr->mode |= PNG_HAVE_PLTE;
  82.436 +         else if (!png_memcmp(chunk_name, png_IDAT, 4))
  82.437 +         {
  82.438 +            if (!(png_ptr->mode & PNG_HAVE_IHDR))
  82.439 +               png_error(png_ptr, "Missing IHDR before IDAT");
  82.440 +            else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
  82.441 +                     !(png_ptr->mode & PNG_HAVE_PLTE))
  82.442 +               png_error(png_ptr, "Missing PLTE before IDAT");
  82.443 +            break;
  82.444 +         }
  82.445 +      }
  82.446 +#endif
  82.447 +      else if (!png_memcmp(chunk_name, png_PLTE, 4))
  82.448 +         png_handle_PLTE(png_ptr, info_ptr, length);
  82.449 +      else if (!png_memcmp(chunk_name, png_IDAT, 4))
  82.450 +      {
  82.451 +         if (!(png_ptr->mode & PNG_HAVE_IHDR))
  82.452 +            png_error(png_ptr, "Missing IHDR before IDAT");
  82.453 +         else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
  82.454 +                  !(png_ptr->mode & PNG_HAVE_PLTE))
  82.455 +            png_error(png_ptr, "Missing PLTE before IDAT");
  82.456 +
  82.457 +         png_ptr->idat_size = length;
  82.458 +         png_ptr->mode |= PNG_HAVE_IDAT;
  82.459 +         break;
  82.460 +      }
  82.461 +#if defined(PNG_READ_bKGD_SUPPORTED)
  82.462 +      else if (!png_memcmp(chunk_name, png_bKGD, 4))
  82.463 +         png_handle_bKGD(png_ptr, info_ptr, length);
  82.464 +#endif
  82.465 +#if defined(PNG_READ_cHRM_SUPPORTED)
  82.466 +      else if (!png_memcmp(chunk_name, png_cHRM, 4))
  82.467 +         png_handle_cHRM(png_ptr, info_ptr, length);
  82.468 +#endif
  82.469 +#if defined(PNG_READ_gAMA_SUPPORTED)
  82.470 +      else if (!png_memcmp(chunk_name, png_gAMA, 4))
  82.471 +         png_handle_gAMA(png_ptr, info_ptr, length);
  82.472 +#endif
  82.473 +#if defined(PNG_READ_hIST_SUPPORTED)
  82.474 +      else if (!png_memcmp(chunk_name, png_hIST, 4))
  82.475 +         png_handle_hIST(png_ptr, info_ptr, length);
  82.476 +#endif
  82.477 +#if defined(PNG_READ_oFFs_SUPPORTED)
  82.478 +      else if (!png_memcmp(chunk_name, png_oFFs, 4))
  82.479 +         png_handle_oFFs(png_ptr, info_ptr, length);
  82.480 +#endif
  82.481 +#if defined(PNG_READ_pCAL_SUPPORTED)
  82.482 +      else if (!png_memcmp(chunk_name, png_pCAL, 4))
  82.483 +         png_handle_pCAL(png_ptr, info_ptr, length);
  82.484 +#endif
  82.485 +#if defined(PNG_READ_sCAL_SUPPORTED)
  82.486 +      else if (!png_memcmp(chunk_name, png_sCAL, 4))
  82.487 +         png_handle_sCAL(png_ptr, info_ptr, length);
  82.488 +#endif
  82.489 +#if defined(PNG_READ_pHYs_SUPPORTED)
  82.490 +      else if (!png_memcmp(chunk_name, png_pHYs, 4))
  82.491 +         png_handle_pHYs(png_ptr, info_ptr, length);
  82.492 +#endif
  82.493 +#if defined(PNG_READ_sBIT_SUPPORTED)
  82.494 +      else if (!png_memcmp(chunk_name, png_sBIT, 4))
  82.495 +         png_handle_sBIT(png_ptr, info_ptr, length);
  82.496 +#endif
  82.497 +#if defined(PNG_READ_sRGB_SUPPORTED)
  82.498 +      else if (!png_memcmp(chunk_name, png_sRGB, 4))
  82.499 +         png_handle_sRGB(png_ptr, info_ptr, length);
  82.500 +#endif
  82.501 +#if defined(PNG_READ_iCCP_SUPPORTED)
  82.502 +      else if (!png_memcmp(chunk_name, png_iCCP, 4))
  82.503 +         png_handle_iCCP(png_ptr, info_ptr, length);
  82.504 +#endif
  82.505 +#if defined(PNG_READ_sPLT_SUPPORTED)
  82.506 +      else if (!png_memcmp(chunk_name, png_sPLT, 4))
  82.507 +         png_handle_sPLT(png_ptr, info_ptr, length);
  82.508 +#endif
  82.509 +#if defined(PNG_READ_tEXt_SUPPORTED)
  82.510 +      else if (!png_memcmp(chunk_name, png_tEXt, 4))
  82.511 +         png_handle_tEXt(png_ptr, info_ptr, length);
  82.512 +#endif
  82.513 +#if defined(PNG_READ_tIME_SUPPORTED)
  82.514 +      else if (!png_memcmp(chunk_name, png_tIME, 4))
  82.515 +         png_handle_tIME(png_ptr, info_ptr, length);
  82.516 +#endif
  82.517 +#if defined(PNG_READ_tRNS_SUPPORTED)
  82.518 +      else if (!png_memcmp(chunk_name, png_tRNS, 4))
  82.519 +         png_handle_tRNS(png_ptr, info_ptr, length);
  82.520 +#endif
  82.521 +#if defined(PNG_READ_zTXt_SUPPORTED)
  82.522 +      else if (!png_memcmp(chunk_name, png_zTXt, 4))
  82.523 +         png_handle_zTXt(png_ptr, info_ptr, length);
  82.524 +#endif
  82.525 +#if defined(PNG_READ_iTXt_SUPPORTED)
  82.526 +      else if (!png_memcmp(chunk_name, png_iTXt, 4))
  82.527 +         png_handle_iTXt(png_ptr, info_ptr, length);
  82.528 +#endif
  82.529 +      else
  82.530 +         png_handle_unknown(png_ptr, info_ptr, length);
  82.531 +   }
  82.532 +}
  82.533 +#endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
  82.534 +
  82.535 +/* optional call to update the users info_ptr structure */
  82.536 +void PNGAPI
  82.537 +png_read_update_info(png_structp png_ptr, png_infop info_ptr)
  82.538 +{
  82.539 +   png_debug(1, "in png_read_update_info\n");
  82.540 +   if (png_ptr == NULL) return;
  82.541 +   if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
  82.542 +      png_read_start_row(png_ptr);
  82.543 +   else
  82.544 +      png_warning(png_ptr,
  82.545 +      "Ignoring extra png_read_update_info() call; row buffer not reallocated");
  82.546 +   png_read_transform_info(png_ptr, info_ptr);
  82.547 +}
  82.548 +
  82.549 +#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
  82.550 +/* Initialize palette, background, etc, after transformations
  82.551 + * are set, but before any reading takes place.  This allows
  82.552 + * the user to obtain a gamma-corrected palette, for example.
  82.553 + * If the user doesn't call this, we will do it ourselves.
  82.554 + */
  82.555 +void PNGAPI
  82.556 +png_start_read_image(png_structp png_ptr)
  82.557 +{
  82.558 +   png_debug(1, "in png_start_read_image\n");
  82.559 +   if (png_ptr == NULL) return;
  82.560 +   if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
  82.561 +      png_read_start_row(png_ptr);
  82.562 +}
  82.563 +#endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
  82.564 +
  82.565 +#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
  82.566 +void PNGAPI
  82.567 +png_read_row(png_structp png_ptr, png_bytep row, png_bytep dsp_row)
  82.568 +{
  82.569 +#ifdef PNG_USE_LOCAL_ARRAYS
  82.570 +   PNG_CONST PNG_IDAT;
  82.571 +   PNG_CONST int png_pass_dsp_mask[7] = {0xff, 0x0f, 0xff, 0x33, 0xff, 0x55,
  82.572 +      0xff};
  82.573 +   PNG_CONST int png_pass_mask[7] = {0x80, 0x08, 0x88, 0x22, 0xaa, 0x55, 0xff};
  82.574 +#endif
  82.575 +   int ret;
  82.576 +   if (png_ptr == NULL) return;
  82.577 +   png_debug2(1, "in png_read_row (row %lu, pass %d)\n",
  82.578 +      png_ptr->row_number, png_ptr->pass);
  82.579 +   if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
  82.580 +      png_read_start_row(png_ptr);
  82.581 +   if (png_ptr->row_number == 0 && png_ptr->pass == 0)
  82.582 +   {
  82.583 +   /* check for transforms that have been set but were defined out */
  82.584 +#if defined(PNG_WRITE_INVERT_SUPPORTED) && !defined(PNG_READ_INVERT_SUPPORTED)
  82.585 +   if (png_ptr->transformations & PNG_INVERT_MONO)
  82.586 +      png_warning(png_ptr, "PNG_READ_INVERT_SUPPORTED is not defined.");
  82.587 +#endif
  82.588 +#if defined(PNG_WRITE_FILLER_SUPPORTED) && !defined(PNG_READ_FILLER_SUPPORTED)
  82.589 +   if (png_ptr->transformations & PNG_FILLER)
  82.590 +      png_warning(png_ptr, "PNG_READ_FILLER_SUPPORTED is not defined.");
  82.591 +#endif
  82.592 +#if defined(PNG_WRITE_PACKSWAP_SUPPORTED) && !defined(PNG_READ_PACKSWAP_SUPPORTED)
  82.593 +   if (png_ptr->transformations & PNG_PACKSWAP)
  82.594 +      png_warning(png_ptr, "PNG_READ_PACKSWAP_SUPPORTED is not defined.");
  82.595 +#endif
  82.596 +#if defined(PNG_WRITE_PACK_SUPPORTED) && !defined(PNG_READ_PACK_SUPPORTED)
  82.597 +   if (png_ptr->transformations & PNG_PACK)
  82.598 +      png_warning(png_ptr, "PNG_READ_PACK_SUPPORTED is not defined.");
  82.599 +#endif
  82.600 +#if defined(PNG_WRITE_SHIFT_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED)
  82.601 +   if (png_ptr->transformations & PNG_SHIFT)
  82.602 +      png_warning(png_ptr, "PNG_READ_SHIFT_SUPPORTED is not defined.");
  82.603 +#endif
  82.604 +#if defined(PNG_WRITE_BGR_SUPPORTED) && !defined(PNG_READ_BGR_SUPPORTED)
  82.605 +   if (png_ptr->transformations & PNG_BGR)
  82.606 +      png_warning(png_ptr, "PNG_READ_BGR_SUPPORTED is not defined.");
  82.607 +#endif
  82.608 +#if defined(PNG_WRITE_SWAP_SUPPORTED) && !defined(PNG_READ_SWAP_SUPPORTED)
  82.609 +   if (png_ptr->transformations & PNG_SWAP_BYTES)
  82.610 +      png_warning(png_ptr, "PNG_READ_SWAP_SUPPORTED is not defined.");
  82.611 +#endif
  82.612 +   }
  82.613 +
  82.614 +#if defined(PNG_READ_INTERLACING_SUPPORTED)
  82.615 +   /* if interlaced and we do not need a new row, combine row and return */
  82.616 +   if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
  82.617 +   {
  82.618 +      switch (png_ptr->pass)
  82.619 +      {
  82.620 +         case 0:
  82.621 +            if (png_ptr->row_number & 0x07)
  82.622 +            {
  82.623 +               if (dsp_row != NULL)
  82.624 +                  png_combine_row(png_ptr, dsp_row,
  82.625 +                     png_pass_dsp_mask[png_ptr->pass]);
  82.626 +               png_read_finish_row(png_ptr);
  82.627 +               return;
  82.628 +            }
  82.629 +            break;
  82.630 +         case 1:
  82.631 +            if ((png_ptr->row_number & 0x07) || png_ptr->width < 5)
  82.632 +            {
  82.633 +               if (dsp_row != NULL)
  82.634 +                  png_combine_row(png_ptr, dsp_row,
  82.635 +                     png_pass_dsp_mask[png_ptr->pass]);
  82.636 +               png_read_finish_row(png_ptr);
  82.637 +               return;
  82.638 +            }
  82.639 +            break;
  82.640 +         case 2:
  82.641 +            if ((png_ptr->row_number & 0x07) != 4)
  82.642 +            {
  82.643 +               if (dsp_row != NULL && (png_ptr->row_number & 4))
  82.644 +                  png_combine_row(png_ptr, dsp_row,
  82.645 +                     png_pass_dsp_mask[png_ptr->pass]);
  82.646 +               png_read_finish_row(png_ptr);
  82.647 +               return;
  82.648 +            }
  82.649 +            break;
  82.650 +         case 3:
  82.651 +            if ((png_ptr->row_number & 3) || png_ptr->width < 3)
  82.652 +            {
  82.653 +               if (dsp_row != NULL)
  82.654 +                  png_combine_row(png_ptr, dsp_row,
  82.655 +                     png_pass_dsp_mask[png_ptr->pass]);
  82.656 +               png_read_finish_row(png_ptr);
  82.657 +               return;
  82.658 +            }
  82.659 +            break;
  82.660 +         case 4:
  82.661 +            if ((png_ptr->row_number & 3) != 2)
  82.662 +            {
  82.663 +               if (dsp_row != NULL && (png_ptr->row_number & 2))
  82.664 +                  png_combine_row(png_ptr, dsp_row,
  82.665 +                     png_pass_dsp_mask[png_ptr->pass]);
  82.666 +               png_read_finish_row(png_ptr);
  82.667 +               return;
  82.668 +            }
  82.669 +            break;
  82.670 +         case 5:
  82.671 +            if ((png_ptr->row_number & 1) || png_ptr->width < 2)
  82.672 +            {
  82.673 +               if (dsp_row != NULL)
  82.674 +                  png_combine_row(png_ptr, dsp_row,
  82.675 +                     png_pass_dsp_mask[png_ptr->pass]);
  82.676 +               png_read_finish_row(png_ptr);
  82.677 +               return;
  82.678 +            }
  82.679 +            break;
  82.680 +         case 6:
  82.681 +            if (!(png_ptr->row_number & 1))
  82.682 +            {
  82.683 +               png_read_finish_row(png_ptr);
  82.684 +               return;
  82.685 +            }
  82.686 +            break;
  82.687 +      }
  82.688 +   }
  82.689 +#endif
  82.690 +
  82.691 +   if (!(png_ptr->mode & PNG_HAVE_IDAT))
  82.692 +      png_error(png_ptr, "Invalid attempt to read row data");
  82.693 +
  82.694 +   png_ptr->zstream.next_out = png_ptr->row_buf;
  82.695 +   png_ptr->zstream.avail_out = (uInt)png_ptr->irowbytes;
  82.696 +   do
  82.697 +   {
  82.698 +      if (!(png_ptr->zstream.avail_in))
  82.699 +      {
  82.700 +         while (!png_ptr->idat_size)
  82.701 +         {
  82.702 +            png_crc_finish(png_ptr, 0);
  82.703 +
  82.704 +            png_ptr->idat_size = png_read_chunk_header(png_ptr);
  82.705 +            if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
  82.706 +               png_error(png_ptr, "Not enough image data");
  82.707 +         }
  82.708 +         png_ptr->zstream.avail_in = (uInt)png_ptr->zbuf_size;
  82.709 +         png_ptr->zstream.next_in = png_ptr->zbuf;
  82.710 +         if (png_ptr->zbuf_size > png_ptr->idat_size)
  82.711 +            png_ptr->zstream.avail_in = (uInt)png_ptr->idat_size;
  82.712 +         png_crc_read(png_ptr, png_ptr->zbuf,
  82.713 +            (png_size_t)png_ptr->zstream.avail_in);
  82.714 +         png_ptr->idat_size -= png_ptr->zstream.avail_in;
  82.715 +      }
  82.716 +      ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);
  82.717 +      if (ret == Z_STREAM_END)
  82.718 +      {
  82.719 +         if (png_ptr->zstream.avail_out || png_ptr->zstream.avail_in ||
  82.720 +            png_ptr->idat_size)
  82.721 +            png_error(png_ptr, "Extra compressed data");
  82.722 +         png_ptr->mode |= PNG_AFTER_IDAT;
  82.723 +         png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
  82.724 +         break;
  82.725 +      }
  82.726 +      if (ret != Z_OK)
  82.727 +         png_error(png_ptr, png_ptr->zstream.msg ? png_ptr->zstream.msg :
  82.728 +                   "Decompression error");
  82.729 +
  82.730 +   } while (png_ptr->zstream.avail_out);
  82.731 +
  82.732 +   png_ptr->row_info.color_type = png_ptr->color_type;
  82.733 +   png_ptr->row_info.width = png_ptr->iwidth;
  82.734 +   png_ptr->row_info.channels = png_ptr->channels;
  82.735 +   png_ptr->row_info.bit_depth = png_ptr->bit_depth;
  82.736 +   png_ptr->row_info.pixel_depth = png_ptr->pixel_depth;
  82.737 +   png_ptr->row_info.rowbytes = PNG_ROWBYTES(png_ptr->row_info.pixel_depth,
  82.738 +       png_ptr->row_info.width);
  82.739 +
  82.740 +   if (png_ptr->row_buf[0])
  82.741 +   png_read_filter_row(png_ptr, &(png_ptr->row_info),
  82.742 +      png_ptr->row_buf + 1, png_ptr->prev_row + 1,
  82.743 +      (int)(png_ptr->row_buf[0]));
  82.744 +
  82.745 +   png_memcpy_check(png_ptr, png_ptr->prev_row, png_ptr->row_buf,
  82.746 +      png_ptr->rowbytes + 1);
  82.747 +
  82.748 +#if defined(PNG_MNG_FEATURES_SUPPORTED)
  82.749 +   if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
  82.750 +      (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING))
  82.751 +   {
  82.752 +      /* Intrapixel differencing */
  82.753 +      png_do_read_intrapixel(&(png_ptr->row_info), png_ptr->row_buf + 1);
  82.754 +   }
  82.755 +#endif
  82.756 +
  82.757 +
  82.758 +   if (png_ptr->transformations || (png_ptr->flags&PNG_FLAG_STRIP_ALPHA))
  82.759 +      png_do_read_transformations(png_ptr);
  82.760 +
  82.761 +#if defined(PNG_READ_INTERLACING_SUPPORTED)
  82.762 +   /* blow up interlaced rows to full size */
  82.763 +   if (png_ptr->interlaced &&
  82.764 +      (png_ptr->transformations & PNG_INTERLACE))
  82.765 +   {
  82.766 +      if (png_ptr->pass < 6)
  82.767 +/*       old interface (pre-1.0.9):
  82.768 +         png_do_read_interlace(&(png_ptr->row_info),
  82.769 +            png_ptr->row_buf + 1, png_ptr->pass, png_ptr->transformations);
  82.770 + */
  82.771 +         png_do_read_interlace(png_ptr);
  82.772 +
  82.773 +      if (dsp_row != NULL)
  82.774 +         png_combine_row(png_ptr, dsp_row,
  82.775 +            png_pass_dsp_mask[png_ptr->pass]);
  82.776 +      if (row != NULL)
  82.777 +         png_combine_row(png_ptr, row,
  82.778 +            png_pass_mask[png_ptr->pass]);
  82.779 +   }
  82.780 +   else
  82.781 +#endif
  82.782 +   {
  82.783 +      if (row != NULL)
  82.784 +         png_combine_row(png_ptr, row, 0xff);
  82.785 +      if (dsp_row != NULL)
  82.786 +         png_combine_row(png_ptr, dsp_row, 0xff);
  82.787 +   }
  82.788 +   png_read_finish_row(png_ptr);
  82.789 +
  82.790 +   if (png_ptr->read_row_fn != NULL)
  82.791 +      (*(png_ptr->read_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
  82.792 +}
  82.793 +#endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
  82.794 +
  82.795 +#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
  82.796 +/* Read one or more rows of image data.  If the image is interlaced,
  82.797 + * and png_set_interlace_handling() has been called, the rows need to
  82.798 + * contain the contents of the rows from the previous pass.  If the
  82.799 + * image has alpha or transparency, and png_handle_alpha()[*] has been
  82.800 + * called, the rows contents must be initialized to the contents of the
  82.801 + * screen.
  82.802 + *
  82.803 + * "row" holds the actual image, and pixels are placed in it
  82.804 + * as they arrive.  If the image is displayed after each pass, it will
  82.805 + * appear to "sparkle" in.  "display_row" can be used to display a
  82.806 + * "chunky" progressive image, with finer detail added as it becomes
  82.807 + * available.  If you do not want this "chunky" display, you may pass
  82.808 + * NULL for display_row.  If you do not want the sparkle display, and
  82.809 + * you have not called png_handle_alpha(), you may pass NULL for rows.
  82.810 + * If you have called png_handle_alpha(), and the image has either an
  82.811 + * alpha channel or a transparency chunk, you must provide a buffer for
  82.812 + * rows.  In this case, you do not have to provide a display_row buffer
  82.813 + * also, but you may.  If the image is not interlaced, or if you have
  82.814 + * not called png_set_interlace_handling(), the display_row buffer will
  82.815 + * be ignored, so pass NULL to it.
  82.816 + *
  82.817 + * [*] png_handle_alpha() does not exist yet, as of this version of libpng
  82.818 + */
  82.819 +
  82.820 +void PNGAPI
  82.821 +png_read_rows(png_structp png_ptr, png_bytepp row,
  82.822 +   png_bytepp display_row, png_uint_32 num_rows)
  82.823 +{
  82.824 +   png_uint_32 i;
  82.825 +   png_bytepp rp;
  82.826 +   png_bytepp dp;
  82.827 +
  82.828 +   png_debug(1, "in png_read_rows\n");
  82.829 +   if (png_ptr == NULL) return;
  82.830 +   rp = row;
  82.831 +   dp = display_row;
  82.832 +   if (rp != NULL && dp != NULL)
  82.833 +      for (i = 0; i < num_rows; i++)
  82.834 +      {
  82.835 +         png_bytep rptr = *rp++;
  82.836 +         png_bytep dptr = *dp++;
  82.837 +
  82.838 +         png_read_row(png_ptr, rptr, dptr);
  82.839 +      }
  82.840 +   else if (rp != NULL)
  82.841 +      for (i = 0; i < num_rows; i++)
  82.842 +      {
  82.843 +         png_bytep rptr = *rp;
  82.844 +         png_read_row(png_ptr, rptr, png_bytep_NULL);
  82.845 +         rp++;
  82.846 +      }
  82.847 +   else if (dp != NULL)
  82.848 +      for (i = 0; i < num_rows; i++)
  82.849 +      {
  82.850 +         png_bytep dptr = *dp;
  82.851 +         png_read_row(png_ptr, png_bytep_NULL, dptr);
  82.852 +         dp++;
  82.853 +      }
  82.854 +}
  82.855 +#endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
  82.856 +
  82.857 +#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
  82.858 +/* Read the entire image.  If the image has an alpha channel or a tRNS
  82.859 + * chunk, and you have called png_handle_alpha()[*], you will need to
  82.860 + * initialize the image to the current image that PNG will be overlaying.
  82.861 + * We set the num_rows again here, in case it was incorrectly set in
  82.862 + * png_read_start_row() by a call to png_read_update_info() or
  82.863 + * png_start_read_image() if png_set_interlace_handling() wasn't called
  82.864 + * prior to either of these functions like it should have been.  You can
  82.865 + * only call this function once.  If you desire to have an image for
  82.866 + * each pass of a interlaced image, use png_read_rows() instead.
  82.867 + *
  82.868 + * [*] png_handle_alpha() does not exist yet, as of this version of libpng
  82.869 + */
  82.870 +void PNGAPI
  82.871 +png_read_image(png_structp png_ptr, png_bytepp image)
  82.872 +{
  82.873 +   png_uint_32 i, image_height;
  82.874 +   int pass, j;
  82.875 +   png_bytepp rp;
  82.876 +
  82.877 +   png_debug(1, "in png_read_image\n");
  82.878 +   if (png_ptr == NULL) return;
  82.879 +
  82.880 +#ifdef PNG_READ_INTERLACING_SUPPORTED
  82.881 +   pass = png_set_interlace_handling(png_ptr);
  82.882 +#else
  82.883 +   if (png_ptr->interlaced)
  82.884 +      png_error(png_ptr,
  82.885 +        "Cannot read interlaced image -- interlace handler disabled.");
  82.886 +   pass = 1;
  82.887 +#endif
  82.888 +
  82.889 +
  82.890 +   image_height=png_ptr->height;
  82.891 +   png_ptr->num_rows = image_height; /* Make sure this is set correctly */
  82.892 +
  82.893 +   for (j = 0; j < pass; j++)
  82.894 +   {
  82.895 +      rp = image;
  82.896 +      for (i = 0; i < image_height; i++)
  82.897 +      {
  82.898 +         png_read_row(png_ptr, *rp, png_bytep_NULL);
  82.899 +         rp++;
  82.900 +      }
  82.901 +   }
  82.902 +}
  82.903 +#endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
  82.904 +
  82.905 +#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
  82.906 +/* Read the end of the PNG file.  Will not read past the end of the
  82.907 + * file, will verify the end is accurate, and will read any comments
  82.908 + * or time information at the end of the file, if info is not NULL.
  82.909 + */
  82.910 +void PNGAPI
  82.911 +png_read_end(png_structp png_ptr, png_infop info_ptr)
  82.912 +{
  82.913 +   png_debug(1, "in png_read_end\n");
  82.914 +   if (png_ptr == NULL) return;
  82.915 +   png_crc_finish(png_ptr, 0); /* Finish off CRC from last IDAT chunk */
  82.916 +
  82.917 +   do
  82.918 +   {
  82.919 +#ifdef PNG_USE_LOCAL_ARRAYS
  82.920 +      PNG_CONST PNG_IHDR;
  82.921 +      PNG_CONST PNG_IDAT;
  82.922 +      PNG_CONST PNG_IEND;
  82.923 +      PNG_CONST PNG_PLTE;
  82.924 +#if defined(PNG_READ_bKGD_SUPPORTED)
  82.925 +      PNG_CONST PNG_bKGD;
  82.926 +#endif
  82.927 +#if defined(PNG_READ_cHRM_SUPPORTED)
  82.928 +      PNG_CONST PNG_cHRM;
  82.929 +#endif
  82.930 +#if defined(PNG_READ_gAMA_SUPPORTED)
  82.931 +      PNG_CONST PNG_gAMA;
  82.932 +#endif
  82.933 +#if defined(PNG_READ_hIST_SUPPORTED)
  82.934 +      PNG_CONST PNG_hIST;
  82.935 +#endif
  82.936 +#if defined(PNG_READ_iCCP_SUPPORTED)
  82.937 +      PNG_CONST PNG_iCCP;
  82.938 +#endif
  82.939 +#if defined(PNG_READ_iTXt_SUPPORTED)
  82.940 +      PNG_CONST PNG_iTXt;
  82.941 +#endif
  82.942 +#if defined(PNG_READ_oFFs_SUPPORTED)
  82.943 +      PNG_CONST PNG_oFFs;
  82.944 +#endif
  82.945 +#if defined(PNG_READ_pCAL_SUPPORTED)
  82.946 +      PNG_CONST PNG_pCAL;
  82.947 +#endif
  82.948 +#if defined(PNG_READ_pHYs_SUPPORTED)
  82.949 +      PNG_CONST PNG_pHYs;
  82.950 +#endif
  82.951 +#if defined(PNG_READ_sBIT_SUPPORTED)
  82.952 +      PNG_CONST PNG_sBIT;
  82.953 +#endif
  82.954 +#if defined(PNG_READ_sCAL_SUPPORTED)
  82.955 +      PNG_CONST PNG_sCAL;
  82.956 +#endif
  82.957 +#if defined(PNG_READ_sPLT_SUPPORTED)
  82.958 +      PNG_CONST PNG_sPLT;
  82.959 +#endif
  82.960 +#if defined(PNG_READ_sRGB_SUPPORTED)
  82.961 +      PNG_CONST PNG_sRGB;
  82.962 +#endif
  82.963 +#if defined(PNG_READ_tEXt_SUPPORTED)
  82.964 +      PNG_CONST PNG_tEXt;
  82.965 +#endif
  82.966 +#if defined(PNG_READ_tIME_SUPPORTED)
  82.967 +      PNG_CONST PNG_tIME;
  82.968 +#endif
  82.969 +#if defined(PNG_READ_tRNS_SUPPORTED)
  82.970 +      PNG_CONST PNG_tRNS;
  82.971 +#endif
  82.972 +#if defined(PNG_READ_zTXt_SUPPORTED)
  82.973 +      PNG_CONST PNG_zTXt;
  82.974 +#endif
  82.975 +#endif /* PNG_USE_LOCAL_ARRAYS */
  82.976 +      png_uint_32 length = png_read_chunk_header(png_ptr);
  82.977 +      PNG_CONST png_bytep chunk_name = png_ptr->chunk_name;
  82.978 +
  82.979 +      if (!png_memcmp(chunk_name, png_IHDR, 4))
  82.980 +         png_handle_IHDR(png_ptr, info_ptr, length);
  82.981 +      else if (!png_memcmp(chunk_name, png_IEND, 4))
  82.982 +         png_handle_IEND(png_ptr, info_ptr, length);
  82.983 +#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
  82.984 +      else if (png_handle_as_unknown(png_ptr, chunk_name))
  82.985 +      {
  82.986 +         if (!png_memcmp(chunk_name, png_IDAT, 4))
  82.987 +         {
  82.988 +            if ((length > 0) || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT))
  82.989 +               png_error(png_ptr, "Too many IDAT's found");
  82.990 +         }
  82.991 +         png_handle_unknown(png_ptr, info_ptr, length);
  82.992 +         if (!png_memcmp(chunk_name, png_PLTE, 4))
  82.993 +            png_ptr->mode |= PNG_HAVE_PLTE;
  82.994 +      }
  82.995 +#endif
  82.996 +      else if (!png_memcmp(chunk_name, png_IDAT, 4))
  82.997 +      {
  82.998 +         /* Zero length IDATs are legal after the last IDAT has been
  82.999 +          * read, but not after other chunks have been read.
 82.1000 +          */
 82.1001 +         if ((length > 0) || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT))
 82.1002 +            png_error(png_ptr, "Too many IDAT's found");
 82.1003 +         png_crc_finish(png_ptr, length);
 82.1004 +      }
 82.1005 +      else if (!png_memcmp(chunk_name, png_PLTE, 4))
 82.1006 +         png_handle_PLTE(png_ptr, info_ptr, length);
 82.1007 +#if defined(PNG_READ_bKGD_SUPPORTED)
 82.1008 +      else if (!png_memcmp(chunk_name, png_bKGD, 4))
 82.1009 +         png_handle_bKGD(png_ptr, info_ptr, length);
 82.1010 +#endif
 82.1011 +#if defined(PNG_READ_cHRM_SUPPORTED)
 82.1012 +      else if (!png_memcmp(chunk_name, png_cHRM, 4))
 82.1013 +         png_handle_cHRM(png_ptr, info_ptr, length);
 82.1014 +#endif
 82.1015 +#if defined(PNG_READ_gAMA_SUPPORTED)
 82.1016 +      else if (!png_memcmp(chunk_name, png_gAMA, 4))
 82.1017 +         png_handle_gAMA(png_ptr, info_ptr, length);
 82.1018 +#endif
 82.1019 +#if defined(PNG_READ_hIST_SUPPORTED)
 82.1020 +      else if (!png_memcmp(chunk_name, png_hIST, 4))
 82.1021 +         png_handle_hIST(png_ptr, info_ptr, length);
 82.1022 +#endif
 82.1023 +#if defined(PNG_READ_oFFs_SUPPORTED)
 82.1024 +      else if (!png_memcmp(chunk_name, png_oFFs, 4))
 82.1025 +         png_handle_oFFs(png_ptr, info_ptr, length);
 82.1026 +#endif
 82.1027 +#if defined(PNG_READ_pCAL_SUPPORTED)
 82.1028 +      else if (!png_memcmp(chunk_name, png_pCAL, 4))
 82.1029 +         png_handle_pCAL(png_ptr, info_ptr, length);
 82.1030 +#endif
 82.1031 +#if defined(PNG_READ_sCAL_SUPPORTED)
 82.1032 +      else if (!png_memcmp(chunk_name, png_sCAL, 4))
 82.1033 +         png_handle_sCAL(png_ptr, info_ptr, length);
 82.1034 +#endif
 82.1035 +#if defined(PNG_READ_pHYs_SUPPORTED)
 82.1036 +      else if (!png_memcmp(chunk_name, png_pHYs, 4))
 82.1037 +         png_handle_pHYs(png_ptr, info_ptr, length);
 82.1038 +#endif
 82.1039 +#if defined(PNG_READ_sBIT_SUPPORTED)
 82.1040 +      else if (!png_memcmp(chunk_name, png_sBIT, 4))
 82.1041 +         png_handle_sBIT(png_ptr, info_ptr, length);
 82.1042 +#endif
 82.1043 +#if defined(PNG_READ_sRGB_SUPPORTED)
 82.1044 +      else if (!png_memcmp(chunk_name, png_sRGB, 4))
 82.1045 +         png_handle_sRGB(png_ptr, info_ptr, length);
 82.1046 +#endif
 82.1047 +#if defined(PNG_READ_iCCP_SUPPORTED)
 82.1048 +      else if (!png_memcmp(chunk_name, png_iCCP, 4))
 82.1049 +         png_handle_iCCP(png_ptr, info_ptr, length);
 82.1050 +#endif
 82.1051 +#if defined(PNG_READ_sPLT_SUPPORTED)
 82.1052 +      else if (!png_memcmp(chunk_name, png_sPLT, 4))
 82.1053 +         png_handle_sPLT(png_ptr, info_ptr, length);
 82.1054 +#endif
 82.1055 +#if defined(PNG_READ_tEXt_SUPPORTED)
 82.1056 +      else if (!png_memcmp(chunk_name, png_tEXt, 4))
 82.1057 +         png_handle_tEXt(png_ptr, info_ptr, length);
 82.1058 +#endif
 82.1059 +#if defined(PNG_READ_tIME_SUPPORTED)
 82.1060 +      else if (!png_memcmp(chunk_name, png_tIME, 4))
 82.1061 +         png_handle_tIME(png_ptr, info_ptr, length);
 82.1062 +#endif
 82.1063 +#if defined(PNG_READ_tRNS_SUPPORTED)
 82.1064 +      else if (!png_memcmp(chunk_name, png_tRNS, 4))
 82.1065 +         png_handle_tRNS(png_ptr, info_ptr, length);
 82.1066 +#endif
 82.1067 +#if defined(PNG_READ_zTXt_SUPPORTED)
 82.1068 +      else if (!png_memcmp(chunk_name, png_zTXt, 4))
 82.1069 +         png_handle_zTXt(png_ptr, info_ptr, length);
 82.1070 +#endif
 82.1071 +#if defined(PNG_READ_iTXt_SUPPORTED)
 82.1072 +      else if (!png_memcmp(chunk_name, png_iTXt, 4))
 82.1073 +         png_handle_iTXt(png_ptr, info_ptr, length);
 82.1074 +#endif
 82.1075 +      else
 82.1076 +         png_handle_unknown(png_ptr, info_ptr, length);
 82.1077 +   } while (!(png_ptr->mode & PNG_HAVE_IEND));
 82.1078 +}
 82.1079 +#endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
 82.1080 +
 82.1081 +/* free all memory used by the read */
 82.1082 +void PNGAPI
 82.1083 +png_destroy_read_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr,
 82.1084 +   png_infopp end_info_ptr_ptr)
 82.1085 +{
 82.1086 +   png_structp png_ptr = NULL;
 82.1087 +   png_infop info_ptr = NULL, end_info_ptr = NULL;
 82.1088 +#ifdef PNG_USER_MEM_SUPPORTED
 82.1089 +   png_free_ptr free_fn = NULL;
 82.1090 +   png_voidp mem_ptr = NULL;
 82.1091 +#endif
 82.1092 +
 82.1093 +   png_debug(1, "in png_destroy_read_struct\n");
 82.1094 +   if (png_ptr_ptr != NULL)
 82.1095 +      png_ptr = *png_ptr_ptr;
 82.1096 +   if (png_ptr == NULL)
 82.1097 +      return;
 82.1098 +
 82.1099 +#ifdef PNG_USER_MEM_SUPPORTED
 82.1100 +   free_fn = png_ptr->free_fn;
 82.1101 +   mem_ptr = png_ptr->mem_ptr;
 82.1102 +#endif
 82.1103 +
 82.1104 +   if (info_ptr_ptr != NULL)
 82.1105 +      info_ptr = *info_ptr_ptr;
 82.1106 +
 82.1107 +   if (end_info_ptr_ptr != NULL)
 82.1108 +      end_info_ptr = *end_info_ptr_ptr;
 82.1109 +
 82.1110 +   png_read_destroy(png_ptr, info_ptr, end_info_ptr);
 82.1111 +
 82.1112 +   if (info_ptr != NULL)
 82.1113 +   {
 82.1114 +#if defined(PNG_TEXT_SUPPORTED)
 82.1115 +      png_free_data(png_ptr, info_ptr, PNG_FREE_TEXT, -1);
 82.1116 +#endif
 82.1117 +
 82.1118 +#ifdef PNG_USER_MEM_SUPPORTED
 82.1119 +      png_destroy_struct_2((png_voidp)info_ptr, (png_free_ptr)free_fn,
 82.1120 +          (png_voidp)mem_ptr);
 82.1121 +#else
 82.1122 +      png_destroy_struct((png_voidp)info_ptr);
 82.1123 +#endif
 82.1124 +      *info_ptr_ptr = NULL;
 82.1125 +   }
 82.1126 +
 82.1127 +   if (end_info_ptr != NULL)
 82.1128 +   {
 82.1129 +#if defined(PNG_READ_TEXT_SUPPORTED)
 82.1130 +      png_free_data(png_ptr, end_info_ptr, PNG_FREE_TEXT, -1);
 82.1131 +#endif
 82.1132 +#ifdef PNG_USER_MEM_SUPPORTED
 82.1133 +      png_destroy_struct_2((png_voidp)end_info_ptr, (png_free_ptr)free_fn,
 82.1134 +         (png_voidp)mem_ptr);
 82.1135 +#else
 82.1136 +      png_destroy_struct((png_voidp)end_info_ptr);
 82.1137 +#endif
 82.1138 +      *end_info_ptr_ptr = NULL;
 82.1139 +   }
 82.1140 +
 82.1141 +   if (png_ptr != NULL)
 82.1142 +   {
 82.1143 +#ifdef PNG_USER_MEM_SUPPORTED
 82.1144 +      png_destroy_struct_2((png_voidp)png_ptr, (png_free_ptr)free_fn,
 82.1145 +          (png_voidp)mem_ptr);
 82.1146 +#else
 82.1147 +      png_destroy_struct((png_voidp)png_ptr);
 82.1148 +#endif
 82.1149 +      *png_ptr_ptr = NULL;
 82.1150 +   }
 82.1151 +}
 82.1152 +
 82.1153 +/* free all memory used by the read (old method) */
 82.1154 +void /* PRIVATE */
 82.1155 +png_read_destroy(png_structp png_ptr, png_infop info_ptr, png_infop end_info_ptr)
 82.1156 +{
 82.1157 +#ifdef PNG_SETJMP_SUPPORTED
 82.1158 +   jmp_buf tmp_jmp;
 82.1159 +#endif
 82.1160 +   png_error_ptr error_fn;
 82.1161 +   png_error_ptr warning_fn;
 82.1162 +   png_voidp error_ptr;
 82.1163 +#ifdef PNG_USER_MEM_SUPPORTED
 82.1164 +   png_free_ptr free_fn;
 82.1165 +#endif
 82.1166 +
 82.1167 +   png_debug(1, "in png_read_destroy\n");
 82.1168 +   if (info_ptr != NULL)
 82.1169 +      png_info_destroy(png_ptr, info_ptr);
 82.1170 +
 82.1171 +   if (end_info_ptr != NULL)
 82.1172 +      png_info_destroy(png_ptr, end_info_ptr);
 82.1173 +
 82.1174 +   png_free(png_ptr, png_ptr->zbuf);
 82.1175 +   png_free(png_ptr, png_ptr->big_row_buf);
 82.1176 +   png_free(png_ptr, png_ptr->prev_row);
 82.1177 +   png_free(png_ptr, png_ptr->chunkdata);
 82.1178 +#if defined(PNG_READ_DITHER_SUPPORTED)
 82.1179 +   png_free(png_ptr, png_ptr->palette_lookup);
 82.1180 +   png_free(png_ptr, png_ptr->dither_index);
 82.1181 +#endif
 82.1182 +#if defined(PNG_READ_GAMMA_SUPPORTED)
 82.1183 +   png_free(png_ptr, png_ptr->gamma_table);
 82.1184 +#endif
 82.1185 +#if defined(PNG_READ_BACKGROUND_SUPPORTED)
 82.1186 +   png_free(png_ptr, png_ptr->gamma_from_1);
 82.1187 +   png_free(png_ptr, png_ptr->gamma_to_1);
 82.1188 +#endif
 82.1189 +#ifdef PNG_FREE_ME_SUPPORTED
 82.1190 +   if (png_ptr->free_me & PNG_FREE_PLTE)
 82.1191 +      png_zfree(png_ptr, png_ptr->palette);
 82.1192 +   png_ptr->free_me &= ~PNG_FREE_PLTE;
 82.1193 +#else
 82.1194 +   if (png_ptr->flags & PNG_FLAG_FREE_PLTE)
 82.1195 +      png_zfree(png_ptr, png_ptr->palette);
 82.1196 +   png_ptr->flags &= ~PNG_FLAG_FREE_PLTE;
 82.1197 +#endif
 82.1198 +#if defined(PNG_tRNS_SUPPORTED) || \
 82.1199 +    defined(PNG_READ_EXPAND_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
 82.1200 +#ifdef PNG_FREE_ME_SUPPORTED
 82.1201 +   if (png_ptr->free_me & PNG_FREE_TRNS)
 82.1202 +      png_free(png_ptr, png_ptr->trans);
 82.1203 +   png_ptr->free_me &= ~PNG_FREE_TRNS;
 82.1204 +#else
 82.1205 +   if (png_ptr->flags & PNG_FLAG_FREE_TRNS)
 82.1206 +      png_free(png_ptr, png_ptr->trans);
 82.1207 +   png_ptr->flags &= ~PNG_FLAG_FREE_TRNS;
 82.1208 +#endif
 82.1209 +#endif
 82.1210 +#if defined(PNG_READ_hIST_SUPPORTED)
 82.1211 +#ifdef PNG_FREE_ME_SUPPORTED
 82.1212 +   if (png_ptr->free_me & PNG_FREE_HIST)
 82.1213 +      png_free(png_ptr, png_ptr->hist);
 82.1214 +   png_ptr->free_me &= ~PNG_FREE_HIST;
 82.1215 +#else
 82.1216 +   if (png_ptr->flags & PNG_FLAG_FREE_HIST)
 82.1217 +      png_free(png_ptr, png_ptr->hist);
 82.1218 +   png_ptr->flags &= ~PNG_FLAG_FREE_HIST;
 82.1219 +#endif
 82.1220 +#endif
 82.1221 +#if defined(PNG_READ_GAMMA_SUPPORTED)
 82.1222 +   if (png_ptr->gamma_16_table != NULL)
 82.1223 +   {
 82.1224 +      int i;
 82.1225 +      int istop = (1 << (8 - png_ptr->gamma_shift));
 82.1226 +      for (i = 0; i < istop; i++)
 82.1227 +      {
 82.1228 +         png_free(png_ptr, png_ptr->gamma_16_table[i]);
 82.1229 +      }
 82.1230 +   png_free(png_ptr, png_ptr->gamma_16_table);
 82.1231 +   }
 82.1232 +#if defined(PNG_READ_BACKGROUND_SUPPORTED)
 82.1233 +   if (png_ptr->gamma_16_from_1 != NULL)
 82.1234 +   {
 82.1235 +      int i;
 82.1236 +      int istop = (1 << (8 - png_ptr->gamma_shift));
 82.1237 +      for (i = 0; i < istop; i++)
 82.1238 +      {
 82.1239 +         png_free(png_ptr, png_ptr->gamma_16_from_1[i]);
 82.1240 +      }
 82.1241 +   png_free(png_ptr, png_ptr->gamma_16_from_1);
 82.1242 +   }
 82.1243 +   if (png_ptr->gamma_16_to_1 != NULL)
 82.1244 +   {
 82.1245 +      int i;
 82.1246 +      int istop = (1 << (8 - png_ptr->gamma_shift));
 82.1247 +      for (i = 0; i < istop; i++)
 82.1248 +      {
 82.1249 +         png_free(png_ptr, png_ptr->gamma_16_to_1[i]);
 82.1250 +      }
 82.1251 +   png_free(png_ptr, png_ptr->gamma_16_to_1);
 82.1252 +   }
 82.1253 +#endif
 82.1254 +#endif
 82.1255 +#if defined(PNG_TIME_RFC1123_SUPPORTED)
 82.1256 +   png_free(png_ptr, png_ptr->time_buffer);
 82.1257 +#endif
 82.1258 +
 82.1259 +   inflateEnd(&png_ptr->zstream);
 82.1260 +#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
 82.1261 +   png_free(png_ptr, png_ptr->save_buffer);
 82.1262 +#endif
 82.1263 +
 82.1264 +#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
 82.1265 +#ifdef PNG_TEXT_SUPPORTED
 82.1266 +   png_free(png_ptr, png_ptr->current_text);
 82.1267 +#endif /* PNG_TEXT_SUPPORTED */
 82.1268 +#endif /* PNG_PROGRESSIVE_READ_SUPPORTED */
 82.1269 +
 82.1270 +   /* Save the important info out of the png_struct, in case it is
 82.1271 +    * being used again.
 82.1272 +    */
 82.1273 +#ifdef PNG_SETJMP_SUPPORTED
 82.1274 +   png_memcpy(tmp_jmp, png_ptr->jmpbuf, png_sizeof(jmp_buf));
 82.1275 +#endif
 82.1276 +
 82.1277 +   error_fn = png_ptr->error_fn;
 82.1278 +   warning_fn = png_ptr->warning_fn;
 82.1279 +   error_ptr = png_ptr->error_ptr;
 82.1280 +#ifdef PNG_USER_MEM_SUPPORTED
 82.1281 +   free_fn = png_ptr->free_fn;
 82.1282 +#endif
 82.1283 +
 82.1284 +   png_memset(png_ptr, 0, png_sizeof(png_struct));
 82.1285 +
 82.1286 +   png_ptr->error_fn = error_fn;
 82.1287 +   png_ptr->warning_fn = warning_fn;
 82.1288 +   png_ptr->error_ptr = error_ptr;
 82.1289 +#ifdef PNG_USER_MEM_SUPPORTED
 82.1290 +   png_ptr->free_fn = free_fn;
 82.1291 +#endif
 82.1292 +
 82.1293 +#ifdef PNG_SETJMP_SUPPORTED
 82.1294 +   png_memcpy(png_ptr->jmpbuf, tmp_jmp, png_sizeof(jmp_buf));
 82.1295 +#endif
 82.1296 +
 82.1297 +}
 82.1298 +
 82.1299 +void PNGAPI
 82.1300 +png_set_read_status_fn(png_structp png_ptr, png_read_status_ptr read_row_fn)
 82.1301 +{
 82.1302 +   if (png_ptr == NULL) return;
 82.1303 +   png_ptr->read_row_fn = read_row_fn;
 82.1304 +}
 82.1305 +
 82.1306 +
 82.1307 +#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
 82.1308 +#if defined(PNG_INFO_IMAGE_SUPPORTED)
 82.1309 +void PNGAPI
 82.1310 +png_read_png(png_structp png_ptr, png_infop info_ptr,
 82.1311 +                           int transforms,
 82.1312 +                           voidp params)
 82.1313 +{
 82.1314 +   int row;
 82.1315 +
 82.1316 +   if (png_ptr == NULL) return;
 82.1317 +#if defined(PNG_READ_INVERT_ALPHA_SUPPORTED)
 82.1318 +   /* invert the alpha channel from opacity to transparency
 82.1319 +    */
 82.1320 +   if (transforms & PNG_TRANSFORM_INVERT_ALPHA)
 82.1321 +       png_set_invert_alpha(png_ptr);
 82.1322 +#endif
 82.1323 +
 82.1324 +   /* png_read_info() gives us all of the information from the
 82.1325 +    * PNG file before the first IDAT (image data chunk).
 82.1326 +    */
 82.1327 +   png_read_info(png_ptr, info_ptr);
 82.1328 +   if (info_ptr->height > PNG_UINT_32_MAX/png_sizeof(png_bytep))
 82.1329 +      png_error(png_ptr, "Image is too high to process with png_read_png()");
 82.1330 +
 82.1331 +   /* -------------- image transformations start here ------------------- */
 82.1332 +
 82.1333 +#if defined(PNG_READ_16_TO_8_SUPPORTED)
 82.1334 +   /* tell libpng to strip 16 bit/color files down to 8 bits per color
 82.1335 +    */
 82.1336 +   if (transforms & PNG_TRANSFORM_STRIP_16)
 82.1337 +       png_set_strip_16(png_ptr);
 82.1338 +#endif
 82.1339 +
 82.1340 +#if defined(PNG_READ_STRIP_ALPHA_SUPPORTED)
 82.1341 +   /* Strip alpha bytes from the input data without combining with
 82.1342 +    * the background (not recommended).
 82.1343 +    */
 82.1344 +   if (transforms & PNG_TRANSFORM_STRIP_ALPHA)
 82.1345 +       png_set_strip_alpha(png_ptr);
 82.1346 +#endif
 82.1347 +
 82.1348 +#if defined(PNG_READ_PACK_SUPPORTED) && !defined(PNG_READ_EXPAND_SUPPORTED)
 82.1349 +   /* Extract multiple pixels with bit depths of 1, 2, or 4 from a single
 82.1350 +    * byte into separate bytes (useful for paletted and grayscale images).
 82.1351 +    */
 82.1352 +   if (transforms & PNG_TRANSFORM_PACKING)
 82.1353 +       png_set_packing(png_ptr);
 82.1354 +#endif
 82.1355 +
 82.1356 +#if defined(PNG_READ_PACKSWAP_SUPPORTED)
 82.1357 +   /* Change the order of packed pixels to least significant bit first
 82.1358 +    * (not useful if you are using png_set_packing).
 82.1359 +    */
 82.1360 +   if (transforms & PNG_TRANSFORM_PACKSWAP)
 82.1361 +       png_set_packswap(png_ptr);
 82.1362 +#endif
 82.1363 +
 82.1364 +#if defined(PNG_READ_EXPAND_SUPPORTED)
 82.1365 +   /* Expand paletted colors into true RGB triplets
 82.1366 +    * Expand grayscale images to full 8 bits from 1, 2, or 4 bits/pixel
 82.1367 +    * Expand paletted or RGB images with transparency to full alpha
 82.1368 +    * channels so the data will be available as RGBA quartets.
 82.1369 +    */
 82.1370 +   if (transforms & PNG_TRANSFORM_EXPAND)
 82.1371 +       if ((png_ptr->bit_depth < 8) ||
 82.1372 +           (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) ||
 82.1373 +           (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)))
 82.1374 +         png_set_expand(png_ptr);
 82.1375 +#endif
 82.1376 +
 82.1377 +   /* We don't handle background color or gamma transformation or dithering.
 82.1378 +    */
 82.1379 +
 82.1380 +#if defined(PNG_READ_INVERT_SUPPORTED)
 82.1381 +   /* invert monochrome files to have 0 as white and 1 as black
 82.1382 +    */
 82.1383 +   if (transforms & PNG_TRANSFORM_INVERT_MONO)
 82.1384 +       png_set_invert_mono(png_ptr);
 82.1385 +#endif
 82.1386 +
 82.1387 +#if defined(PNG_READ_SHIFT_SUPPORTED)
 82.1388 +   /* If you want to shift the pixel values from the range [0,255] or
 82.1389 +    * [0,65535] to the original [0,7] or [0,31], or whatever range the
 82.1390 +    * colors were originally in:
 82.1391 +    */
 82.1392 +   if ((transforms & PNG_TRANSFORM_SHIFT)
 82.1393 +       && png_get_valid(png_ptr, info_ptr, PNG_INFO_sBIT))
 82.1394 +   {
 82.1395 +      png_color_8p sig_bit;
 82.1396 +
 82.1397 +      png_get_sBIT(png_ptr, info_ptr, &sig_bit);
 82.1398 +      png_set_shift(png_ptr, sig_bit);
 82.1399 +   }
 82.1400 +#endif
 82.1401 +
 82.1402 +#if defined(PNG_READ_BGR_SUPPORTED)
 82.1403 +   /* flip the RGB pixels to BGR (or RGBA to BGRA)
 82.1404 +    */
 82.1405 +   if (transforms & PNG_TRANSFORM_BGR)
 82.1406 +       png_set_bgr(png_ptr);
 82.1407 +#endif
 82.1408 +
 82.1409 +#if defined(PNG_READ_SWAP_ALPHA_SUPPORTED)
 82.1410 +   /* swap the RGBA or GA data to ARGB or AG (or BGRA to ABGR)
 82.1411 +    */
 82.1412 +   if (transforms & PNG_TRANSFORM_SWAP_ALPHA)
 82.1413 +       png_set_swap_alpha(png_ptr);
 82.1414 +#endif
 82.1415 +
 82.1416 +#if defined(PNG_READ_SWAP_SUPPORTED)
 82.1417 +   /* swap bytes of 16 bit files to least significant byte first
 82.1418 +    */
 82.1419 +   if (transforms & PNG_TRANSFORM_SWAP_ENDIAN)
 82.1420 +       png_set_swap(png_ptr);
 82.1421 +#endif
 82.1422 +
 82.1423 +   /* We don't handle adding filler bytes */
 82.1424 +
 82.1425 +   /* Optional call to gamma correct and add the background to the palette
 82.1426 +    * and update info structure.  REQUIRED if you are expecting libpng to
 82.1427 +    * update the palette for you (i.e., you selected such a transform above).
 82.1428 +    */
 82.1429 +   png_read_update_info(png_ptr, info_ptr);
 82.1430 +
 82.1431 +   /* -------------- image transformations end here ------------------- */
 82.1432 +
 82.1433 +#ifdef PNG_FREE_ME_SUPPORTED
 82.1434 +   png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0);
 82.1435 +#endif
 82.1436 +   if (info_ptr->row_pointers == NULL)
 82.1437 +   {
 82.1438 +      info_ptr->row_pointers = (png_bytepp)png_malloc(png_ptr,
 82.1439 +         info_ptr->height * png_sizeof(png_bytep));
 82.1440 +#ifdef PNG_FREE_ME_SUPPORTED
 82.1441 +      info_ptr->free_me |= PNG_FREE_ROWS;
 82.1442 +#endif
 82.1443 +      for (row = 0; row < (int)info_ptr->height; row++)
 82.1444 +      {
 82.1445 +         info_ptr->row_pointers[row] = (png_bytep)png_malloc(png_ptr,
 82.1446 +            png_get_rowbytes(png_ptr, info_ptr));
 82.1447 +      }
 82.1448 +   }
 82.1449 +
 82.1450 +   png_read_image(png_ptr, info_ptr->row_pointers);
 82.1451 +   info_ptr->valid |= PNG_INFO_IDAT;
 82.1452 +
 82.1453 +   /* read rest of file, and get additional chunks in info_ptr - REQUIRED */
 82.1454 +   png_read_end(png_ptr, info_ptr);
 82.1455 +
 82.1456 +   transforms = transforms; /* quiet compiler warnings */
 82.1457 +   params = params;
 82.1458 +
 82.1459 +}
 82.1460 +#endif /* PNG_INFO_IMAGE_SUPPORTED */
 82.1461 +#endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
 82.1462 +#endif /* PNG_READ_SUPPORTED */
    83.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    83.2 +++ b/libs/libpng/pngrio.c	Sat Sep 19 05:51:51 2015 +0300
    83.3 @@ -0,0 +1,166 @@
    83.4 +
    83.5 +/* pngrio.c - functions for data input
    83.6 + *
    83.7 + * Last changed in libpng 1.2.30 [August 15, 2008]
    83.8 + * For conditions of distribution and use, see copyright notice in png.h
    83.9 + * Copyright (c) 1998-2008 Glenn Randers-Pehrson
   83.10 + * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
   83.11 + * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
   83.12 + *
   83.13 + * This file provides a location for all input.  Users who need
   83.14 + * special handling are expected to write a function that has the same
   83.15 + * arguments as this and performs a similar function, but that possibly
   83.16 + * has a different input method.  Note that you shouldn't change this
   83.17 + * function, but rather write a replacement function and then make
   83.18 + * libpng use it at run time with png_set_read_fn(...).
   83.19 + */
   83.20 +
   83.21 +#define PNG_INTERNAL
   83.22 +#include "png.h"
   83.23 +#if defined(PNG_READ_SUPPORTED)
   83.24 +
   83.25 +/* Read the data from whatever input you are using.  The default routine
   83.26 +   reads from a file pointer.  Note that this routine sometimes gets called
   83.27 +   with very small lengths, so you should implement some kind of simple
   83.28 +   buffering if you are using unbuffered reads.  This should never be asked
   83.29 +   to read more then 64K on a 16 bit machine. */
   83.30 +void /* PRIVATE */
   83.31 +png_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
   83.32 +{
   83.33 +   png_debug1(4, "reading %d bytes\n", (int)length);
   83.34 +   if (png_ptr->read_data_fn != NULL)
   83.35 +      (*(png_ptr->read_data_fn))(png_ptr, data, length);
   83.36 +   else
   83.37 +      png_error(png_ptr, "Call to NULL read function");
   83.38 +}
   83.39 +
   83.40 +#if !defined(PNG_NO_STDIO)
   83.41 +/* This is the function that does the actual reading of data.  If you are
   83.42 +   not reading from a standard C stream, you should create a replacement
   83.43 +   read_data function and use it at run time with png_set_read_fn(), rather
   83.44 +   than changing the library. */
   83.45 +#ifndef USE_FAR_KEYWORD
   83.46 +void PNGAPI
   83.47 +png_default_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
   83.48 +{
   83.49 +   png_size_t check;
   83.50 +
   83.51 +   if (png_ptr == NULL) return;
   83.52 +   /* fread() returns 0 on error, so it is OK to store this in a png_size_t
   83.53 +    * instead of an int, which is what fread() actually returns.
   83.54 +    */
   83.55 +#if defined(_WIN32_WCE)
   83.56 +   if ( !ReadFile((HANDLE)(png_ptr->io_ptr), data, length, &check, NULL) )
   83.57 +      check = 0;
   83.58 +#else
   83.59 +   check = (png_size_t)fread(data, (png_size_t)1, length,
   83.60 +      (png_FILE_p)png_ptr->io_ptr);
   83.61 +#endif
   83.62 +
   83.63 +   if (check != length)
   83.64 +      png_error(png_ptr, "Read Error");
   83.65 +}
   83.66 +#else
   83.67 +/* this is the model-independent version. Since the standard I/O library
   83.68 +   can't handle far buffers in the medium and small models, we have to copy
   83.69 +   the data.
   83.70 +*/
   83.71 +
   83.72 +#define NEAR_BUF_SIZE 1024
   83.73 +#define MIN(a,b) (a <= b ? a : b)
   83.74 +
   83.75 +static void PNGAPI
   83.76 +png_default_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
   83.77 +{
   83.78 +   int check;
   83.79 +   png_byte *n_data;
   83.80 +   png_FILE_p io_ptr;
   83.81 +
   83.82 +   if (png_ptr == NULL) return;
   83.83 +   /* Check if data really is near. If so, use usual code. */
   83.84 +   n_data = (png_byte *)CVT_PTR_NOCHECK(data);
   83.85 +   io_ptr = (png_FILE_p)CVT_PTR(png_ptr->io_ptr);
   83.86 +   if ((png_bytep)n_data == data)
   83.87 +   {
   83.88 +#if defined(_WIN32_WCE)
   83.89 +      if ( !ReadFile((HANDLE)(png_ptr->io_ptr), data, length, &check, NULL) )
   83.90 +         check = 0;
   83.91 +#else
   83.92 +      check = fread(n_data, 1, length, io_ptr);
   83.93 +#endif
   83.94 +   }
   83.95 +   else
   83.96 +   {
   83.97 +      png_byte buf[NEAR_BUF_SIZE];
   83.98 +      png_size_t read, remaining, err;
   83.99 +      check = 0;
  83.100 +      remaining = length;
  83.101 +      do
  83.102 +      {
  83.103 +         read = MIN(NEAR_BUF_SIZE, remaining);
  83.104 +#if defined(_WIN32_WCE)
  83.105 +         if ( !ReadFile((HANDLE)(io_ptr), buf, read, &err, NULL) )
  83.106 +            err = 0;
  83.107 +#else
  83.108 +         err = fread(buf, (png_size_t)1, read, io_ptr);
  83.109 +#endif
  83.110 +         png_memcpy(data, buf, read); /* copy far buffer to near buffer */
  83.111 +         if (err != read)
  83.112 +            break;
  83.113 +         else
  83.114 +            check += err;
  83.115 +         data += read;
  83.116 +         remaining -= read;
  83.117 +      }
  83.118 +      while (remaining != 0);
  83.119 +   }
  83.120 +   if ((png_uint_32)check != (png_uint_32)length)
  83.121 +      png_error(png_ptr, "read Error");
  83.122 +}
  83.123 +#endif
  83.124 +#endif
  83.125 +
  83.126 +/* This function allows the application to supply a new input function
  83.127 +   for libpng if standard C streams aren't being used.
  83.128 +
  83.129 +   This function takes as its arguments:
  83.130 +   png_ptr      - pointer to a png input data structure
  83.131 +   io_ptr       - pointer to user supplied structure containing info about
  83.132 +                  the input functions.  May be NULL.
  83.133 +   read_data_fn - pointer to a new input function that takes as its
  83.134 +                  arguments a pointer to a png_struct, a pointer to
  83.135 +                  a location where input data can be stored, and a 32-bit
  83.136 +                  unsigned int that is the number of bytes to be read.
  83.137 +                  To exit and output any fatal error messages the new write
  83.138 +                  function should call png_error(png_ptr, "Error msg"). */
  83.139 +void PNGAPI
  83.140 +png_set_read_fn(png_structp png_ptr, png_voidp io_ptr,
  83.141 +   png_rw_ptr read_data_fn)
  83.142 +{
  83.143 +   if (png_ptr == NULL) return;
  83.144 +   png_ptr->io_ptr = io_ptr;
  83.145 +
  83.146 +#if !defined(PNG_NO_STDIO)
  83.147 +   if (read_data_fn != NULL)
  83.148 +      png_ptr->read_data_fn = read_data_fn;
  83.149 +   else
  83.150 +      png_ptr->read_data_fn = png_default_read_data;
  83.151 +#else
  83.152 +   png_ptr->read_data_fn = read_data_fn;
  83.153 +#endif
  83.154 +
  83.155 +   /* It is an error to write to a read device */
  83.156 +   if (png_ptr->write_data_fn != NULL)
  83.157 +   {
  83.158 +      png_ptr->write_data_fn = NULL;
  83.159 +      png_warning(png_ptr,
  83.160 +         "It's an error to set both read_data_fn and write_data_fn in the ");
  83.161 +      png_warning(png_ptr,
  83.162 +         "same structure.  Resetting write_data_fn to NULL.");
  83.163 +   }
  83.164 +
  83.165 +#if defined(PNG_WRITE_FLUSH_SUPPORTED)
  83.166 +   png_ptr->output_flush_fn = NULL;
  83.167 +#endif
  83.168 +}
  83.169 +#endif /* PNG_READ_SUPPORTED */
    84.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    84.2 +++ b/libs/libpng/pngrtran.c	Sat Sep 19 05:51:51 2015 +0300
    84.3 @@ -0,0 +1,4296 @@
    84.4 +
    84.5 +/* pngrtran.c - transforms the data in a row for PNG readers
    84.6 + *
    84.7 + * Last changed in libpng 1.2.30 [August 15, 2008]
    84.8 + * For conditions of distribution and use, see copyright notice in png.h
    84.9 + * Copyright (c) 1998-2008 Glenn Randers-Pehrson
   84.10 + * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
   84.11 + * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
   84.12 + *
   84.13 + * This file contains functions optionally called by an application
   84.14 + * in order to tell libpng how to handle data when reading a PNG.
   84.15 + * Transformations that are used in both reading and writing are
   84.16 + * in pngtrans.c.
   84.17 + */
   84.18 +
   84.19 +#define PNG_INTERNAL
   84.20 +#include "png.h"
   84.21 +#if defined(PNG_READ_SUPPORTED)
   84.22 +
   84.23 +/* Set the action on getting a CRC error for an ancillary or critical chunk. */
   84.24 +void PNGAPI
   84.25 +png_set_crc_action(png_structp png_ptr, int crit_action, int ancil_action)
   84.26 +{
   84.27 +   png_debug(1, "in png_set_crc_action\n");
   84.28 +   /* Tell libpng how we react to CRC errors in critical chunks */
   84.29 +   if (png_ptr == NULL) return;
   84.30 +   switch (crit_action)
   84.31 +   {
   84.32 +      case PNG_CRC_NO_CHANGE:                        /* leave setting as is */
   84.33 +         break;
   84.34 +      case PNG_CRC_WARN_USE:                               /* warn/use data */
   84.35 +         png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK;
   84.36 +         png_ptr->flags |= PNG_FLAG_CRC_CRITICAL_USE;
   84.37 +         break;
   84.38 +      case PNG_CRC_QUIET_USE:                             /* quiet/use data */
   84.39 +         png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK;
   84.40 +         png_ptr->flags |= PNG_FLAG_CRC_CRITICAL_USE |
   84.41 +                           PNG_FLAG_CRC_CRITICAL_IGNORE;
   84.42 +         break;
   84.43 +      case PNG_CRC_WARN_DISCARD:    /* not a valid action for critical data */
   84.44 +         png_warning(png_ptr,
   84.45 +            "Can't discard critical data on CRC error.");
   84.46 +      case PNG_CRC_ERROR_QUIT:                                /* error/quit */
   84.47 +      case PNG_CRC_DEFAULT:
   84.48 +      default:
   84.49 +         png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK;
   84.50 +         break;
   84.51 +   }
   84.52 +
   84.53 +   switch (ancil_action)
   84.54 +   {
   84.55 +      case PNG_CRC_NO_CHANGE:                       /* leave setting as is */
   84.56 +         break;
   84.57 +      case PNG_CRC_WARN_USE:                              /* warn/use data */
   84.58 +         png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK;
   84.59 +         png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_USE;
   84.60 +         break;
   84.61 +      case PNG_CRC_QUIET_USE:                            /* quiet/use data */
   84.62 +         png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK;
   84.63 +         png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_USE |
   84.64 +                           PNG_FLAG_CRC_ANCILLARY_NOWARN;
   84.65 +         break;
   84.66 +      case PNG_CRC_ERROR_QUIT:                               /* error/quit */
   84.67 +         png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK;
   84.68 +         png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_NOWARN;
   84.69 +         break;
   84.70 +      case PNG_CRC_WARN_DISCARD:                      /* warn/discard data */
   84.71 +      case PNG_CRC_DEFAULT:
   84.72 +      default:
   84.73 +         png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK;
   84.74 +         break;
   84.75 +   }
   84.76 +}
   84.77 +
   84.78 +#if defined(PNG_READ_BACKGROUND_SUPPORTED) && \
   84.79 +    defined(PNG_FLOATING_POINT_SUPPORTED)
   84.80 +/* handle alpha and tRNS via a background color */
   84.81 +void PNGAPI
   84.82 +png_set_background(png_structp png_ptr,
   84.83 +   png_color_16p background_color, int background_gamma_code,
   84.84 +   int need_expand, double background_gamma)
   84.85 +{
   84.86 +   png_debug(1, "in png_set_background\n");
   84.87 +   if (png_ptr == NULL) return;
   84.88 +   if (background_gamma_code == PNG_BACKGROUND_GAMMA_UNKNOWN)
   84.89 +   {
   84.90 +      png_warning(png_ptr, "Application must supply a known background gamma");
   84.91 +      return;
   84.92 +   }
   84.93 +
   84.94 +   png_ptr->transformations |= PNG_BACKGROUND;
   84.95 +   png_memcpy(&(png_ptr->background), background_color,
   84.96 +      png_sizeof(png_color_16));
   84.97 +   png_ptr->background_gamma = (float)background_gamma;
   84.98 +   png_ptr->background_gamma_type = (png_byte)(background_gamma_code);
   84.99 +   png_ptr->transformations |= (need_expand ? PNG_BACKGROUND_EXPAND : 0);
  84.100 +}
  84.101 +#endif
  84.102 +
  84.103 +#if defined(PNG_READ_16_TO_8_SUPPORTED)
  84.104 +/* strip 16 bit depth files to 8 bit depth */
  84.105 +void PNGAPI
  84.106 +png_set_strip_16(png_structp png_ptr)
  84.107 +{
  84.108 +   png_debug(1, "in png_set_strip_16\n");
  84.109 +   if (png_ptr == NULL) return;
  84.110 +   png_ptr->transformations |= PNG_16_TO_8;
  84.111 +}
  84.112 +#endif
  84.113 +
  84.114 +#if defined(PNG_READ_STRIP_ALPHA_SUPPORTED)
  84.115 +void PNGAPI
  84.116 +png_set_strip_alpha(png_structp png_ptr)
  84.117 +{
  84.118 +   png_debug(1, "in png_set_strip_alpha\n");
  84.119 +   if (png_ptr == NULL) return;
  84.120 +   png_ptr->flags |= PNG_FLAG_STRIP_ALPHA;
  84.121 +}
  84.122 +#endif
  84.123 +
  84.124 +#if defined(PNG_READ_DITHER_SUPPORTED)
  84.125 +/* Dither file to 8 bit.  Supply a palette, the current number
  84.126 + * of elements in the palette, the maximum number of elements
  84.127 + * allowed, and a histogram if possible.  If the current number
  84.128 + * of colors is greater then the maximum number, the palette will be
  84.129 + * modified to fit in the maximum number.  "full_dither" indicates
  84.130 + * whether we need a dithering cube set up for RGB images, or if we
  84.131 + * simply are reducing the number of colors in a paletted image.
  84.132 + */
  84.133 +
  84.134 +typedef struct png_dsort_struct
  84.135 +{
  84.136 +   struct png_dsort_struct FAR * next;
  84.137 +   png_byte left;
  84.138 +   png_byte right;
  84.139 +} png_dsort;
  84.140 +typedef png_dsort FAR *       png_dsortp;
  84.141 +typedef png_dsort FAR * FAR * png_dsortpp;
  84.142 +
  84.143 +void PNGAPI
  84.144 +png_set_dither(png_structp png_ptr, png_colorp palette,
  84.145 +   int num_palette, int maximum_colors, png_uint_16p histogram,
  84.146 +   int full_dither)
  84.147 +{
  84.148 +   png_debug(1, "in png_set_dither\n");
  84.149 +   if (png_ptr == NULL) return;
  84.150 +   png_ptr->transformations |= PNG_DITHER;
  84.151 +
  84.152 +   if (!full_dither)
  84.153 +   {
  84.154 +      int i;
  84.155 +
  84.156 +      png_ptr->dither_index = (png_bytep)png_malloc(png_ptr,
  84.157 +         (png_uint_32)(num_palette * png_sizeof(png_byte)));
  84.158 +      for (i = 0; i < num_palette; i++)
  84.159 +         png_ptr->dither_index[i] = (png_byte)i;
  84.160 +   }
  84.161 +
  84.162 +   if (num_palette > maximum_colors)
  84.163 +   {
  84.164 +      if (histogram != NULL)
  84.165 +      {
  84.166 +         /* This is easy enough, just throw out the least used colors.
  84.167 +            Perhaps not the best solution, but good enough. */
  84.168 +
  84.169 +         int i;
  84.170 +
  84.171 +         /* initialize an array to sort colors */
  84.172 +         png_ptr->dither_sort = (png_bytep)png_malloc(png_ptr,
  84.173 +            (png_uint_32)(num_palette * png_sizeof(png_byte)));
  84.174 +
  84.175 +         /* initialize the dither_sort array */
  84.176 +         for (i = 0; i < num_palette; i++)
  84.177 +            png_ptr->dither_sort[i] = (png_byte)i;
  84.178 +
  84.179 +         /* Find the least used palette entries by starting a
  84.180 +            bubble sort, and running it until we have sorted
  84.181 +            out enough colors.  Note that we don't care about
  84.182 +            sorting all the colors, just finding which are
  84.183 +            least used. */
  84.184 +
  84.185 +         for (i = num_palette - 1; i >= maximum_colors; i--)
  84.186 +         {
  84.187 +            int done; /* to stop early if the list is pre-sorted */
  84.188 +            int j;
  84.189 +
  84.190 +            done = 1;
  84.191 +            for (j = 0; j < i; j++)
  84.192 +            {
  84.193 +               if (histogram[png_ptr->dither_sort[j]]
  84.194 +                   < histogram[png_ptr->dither_sort[j + 1]])
  84.195 +               {
  84.196 +                  png_byte t;
  84.197 +
  84.198 +                  t = png_ptr->dither_sort[j];
  84.199 +                  png_ptr->dither_sort[j] = png_ptr->dither_sort[j + 1];
  84.200 +                  png_ptr->dither_sort[j + 1] = t;
  84.201 +                  done = 0;
  84.202 +               }
  84.203 +            }
  84.204 +            if (done)
  84.205 +               break;
  84.206 +         }
  84.207 +
  84.208 +         /* swap the palette around, and set up a table, if necessary */
  84.209 +         if (full_dither)
  84.210 +         {
  84.211 +            int j = num_palette;
  84.212 +
  84.213 +            /* put all the useful colors within the max, but don't
  84.214 +               move the others */
  84.215 +            for (i = 0; i < maximum_colors; i++)
  84.216 +            {
  84.217 +               if ((int)png_ptr->dither_sort[i] >= maximum_colors)
  84.218 +               {
  84.219 +                  do
  84.220 +                     j--;
  84.221 +                  while ((int)png_ptr->dither_sort[j] >= maximum_colors);
  84.222 +                  palette[i] = palette[j];
  84.223 +               }
  84.224 +            }
  84.225 +         }
  84.226 +         else
  84.227 +         {
  84.228 +            int j = num_palette;
  84.229 +
  84.230 +            /* move all the used colors inside the max limit, and
  84.231 +               develop a translation table */
  84.232 +            for (i = 0; i < maximum_colors; i++)
  84.233 +            {
  84.234 +               /* only move the colors we need to */
  84.235 +               if ((int)png_ptr->dither_sort[i] >= maximum_colors)
  84.236 +               {
  84.237 +                  png_color tmp_color;
  84.238 +
  84.239 +                  do
  84.240 +                     j--;
  84.241 +                  while ((int)png_ptr->dither_sort[j] >= maximum_colors);
  84.242 +
  84.243 +                  tmp_color = palette[j];
  84.244 +                  palette[j] = palette[i];
  84.245 +                  palette[i] = tmp_color;
  84.246 +                  /* indicate where the color went */
  84.247 +                  png_ptr->dither_index[j] = (png_byte)i;
  84.248 +                  png_ptr->dither_index[i] = (png_byte)j;
  84.249 +               }
  84.250 +            }
  84.251 +
  84.252 +            /* find closest color for those colors we are not using */
  84.253 +            for (i = 0; i < num_palette; i++)
  84.254 +            {
  84.255 +               if ((int)png_ptr->dither_index[i] >= maximum_colors)
  84.256 +               {
  84.257 +                  int min_d, k, min_k, d_index;
  84.258 +
  84.259 +                  /* find the closest color to one we threw out */
  84.260 +                  d_index = png_ptr->dither_index[i];
  84.261 +                  min_d = PNG_COLOR_DIST(palette[d_index], palette[0]);
  84.262 +                  for (k = 1, min_k = 0; k < maximum_colors; k++)
  84.263 +                  {
  84.264 +                     int d;
  84.265 +
  84.266 +                     d = PNG_COLOR_DIST(palette[d_index], palette[k]);
  84.267 +
  84.268 +                     if (d < min_d)
  84.269 +                     {
  84.270 +                        min_d = d;
  84.271 +                        min_k = k;
  84.272 +                     }
  84.273 +                  }
  84.274 +                  /* point to closest color */
  84.275 +                  png_ptr->dither_index[i] = (png_byte)min_k;
  84.276 +               }
  84.277 +            }
  84.278 +         }
  84.279 +         png_free(png_ptr, png_ptr->dither_sort);
  84.280 +         png_ptr->dither_sort = NULL;
  84.281 +      }
  84.282 +      else
  84.283 +      {
  84.284 +         /* This is much harder to do simply (and quickly).  Perhaps
  84.285 +            we need to go through a median cut routine, but those
  84.286 +            don't always behave themselves with only a few colors
  84.287 +            as input.  So we will just find the closest two colors,
  84.288 +            and throw out one of them (chosen somewhat randomly).
  84.289 +            [We don't understand this at all, so if someone wants to
  84.290 +             work on improving it, be our guest - AED, GRP]
  84.291 +            */
  84.292 +         int i;
  84.293 +         int max_d;
  84.294 +         int num_new_palette;
  84.295 +         png_dsortp t;
  84.296 +         png_dsortpp hash;
  84.297 +
  84.298 +         t = NULL;
  84.299 +
  84.300 +         /* initialize palette index arrays */
  84.301 +         png_ptr->index_to_palette = (png_bytep)png_malloc(png_ptr,
  84.302 +            (png_uint_32)(num_palette * png_sizeof(png_byte)));
  84.303 +         png_ptr->palette_to_index = (png_bytep)png_malloc(png_ptr,
  84.304 +            (png_uint_32)(num_palette * png_sizeof(png_byte)));
  84.305 +
  84.306 +         /* initialize the sort array */
  84.307 +         for (i = 0; i < num_palette; i++)
  84.308 +         {
  84.309 +            png_ptr->index_to_palette[i] = (png_byte)i;
  84.310 +            png_ptr->palette_to_index[i] = (png_byte)i;
  84.311 +         }
  84.312 +
  84.313 +         hash = (png_dsortpp)png_malloc(png_ptr, (png_uint_32)(769 *
  84.314 +            png_sizeof(png_dsortp)));
  84.315 +         for (i = 0; i < 769; i++)
  84.316 +            hash[i] = NULL;
  84.317 +/*         png_memset(hash, 0, 769 * png_sizeof(png_dsortp)); */
  84.318 +
  84.319 +         num_new_palette = num_palette;
  84.320 +
  84.321 +         /* initial wild guess at how far apart the farthest pixel
  84.322 +            pair we will be eliminating will be.  Larger
  84.323 +            numbers mean more areas will be allocated, Smaller
  84.324 +            numbers run the risk of not saving enough data, and
  84.325 +            having to do this all over again.
  84.326 +
  84.327 +            I have not done extensive checking on this number.
  84.328 +            */
  84.329 +         max_d = 96;
  84.330 +
  84.331 +         while (num_new_palette > maximum_colors)
  84.332 +         {
  84.333 +            for (i = 0; i < num_new_palette - 1; i++)
  84.334 +            {
  84.335 +               int j;
  84.336 +
  84.337 +               for (j = i + 1; j < num_new_palette; j++)
  84.338 +               {
  84.339 +                  int d;
  84.340 +
  84.341 +                  d = PNG_COLOR_DIST(palette[i], palette[j]);
  84.342 +
  84.343 +                  if (d <= max_d)
  84.344 +                  {
  84.345 +
  84.346 +                     t = (png_dsortp)png_malloc_warn(png_ptr,
  84.347 +                         (png_uint_32)(png_sizeof(png_dsort)));
  84.348 +                     if (t == NULL)
  84.349 +                         break;
  84.350 +                     t->next = hash[d];
  84.351 +                     t->left = (png_byte)i;
  84.352 +                     t->right = (png_byte)j;
  84.353 +                     hash[d] = t;
  84.354 +                  }
  84.355 +               }
  84.356 +               if (t == NULL)
  84.357 +                  break;
  84.358 +            }
  84.359 +
  84.360 +            if (t != NULL)
  84.361 +            for (i = 0; i <= max_d; i++)
  84.362 +            {
  84.363 +               if (hash[i] != NULL)
  84.364 +               {
  84.365 +                  png_dsortp p;
  84.366 +
  84.367 +                  for (p = hash[i]; p; p = p->next)
  84.368 +                  {
  84.369 +                     if ((int)png_ptr->index_to_palette[p->left]
  84.370 +                        < num_new_palette &&
  84.371 +                        (int)png_ptr->index_to_palette[p->right]
  84.372 +                        < num_new_palette)
  84.373 +                     {
  84.374 +                        int j, next_j;
  84.375 +
  84.376 +                        if (num_new_palette & 0x01)
  84.377 +                        {
  84.378 +                           j = p->left;
  84.379 +                           next_j = p->right;
  84.380 +                        }
  84.381 +                        else
  84.382 +                        {
  84.383 +                           j = p->right;
  84.384 +                           next_j = p->left;
  84.385 +                        }
  84.386 +
  84.387 +                        num_new_palette--;
  84.388 +                        palette[png_ptr->index_to_palette[j]]
  84.389 +                          = palette[num_new_palette];
  84.390 +                        if (!full_dither)
  84.391 +                        {
  84.392 +                           int k;
  84.393 +
  84.394 +                           for (k = 0; k < num_palette; k++)
  84.395 +                           {
  84.396 +                              if (png_ptr->dither_index[k] ==
  84.397 +                                 png_ptr->index_to_palette[j])
  84.398 +                                 png_ptr->dither_index[k] =
  84.399 +                                    png_ptr->index_to_palette[next_j];
  84.400 +                              if ((int)png_ptr->dither_index[k] ==
  84.401 +                                 num_new_palette)
  84.402 +                                 png_ptr->dither_index[k] =
  84.403 +                                    png_ptr->index_to_palette[j];
  84.404 +                           }
  84.405 +                        }
  84.406 +
  84.407 +                        png_ptr->index_to_palette[png_ptr->palette_to_index
  84.408 +                           [num_new_palette]] = png_ptr->index_to_palette[j];
  84.409 +                        png_ptr->palette_to_index[png_ptr->index_to_palette[j]]
  84.410 +                           = png_ptr->palette_to_index[num_new_palette];
  84.411 +
  84.412 +                        png_ptr->index_to_palette[j] = (png_byte)num_new_palette;
  84.413 +                        png_ptr->palette_to_index[num_new_palette] = (png_byte)j;
  84.414 +                     }
  84.415 +                     if (num_new_palette <= maximum_colors)
  84.416 +                        break;
  84.417 +                  }
  84.418 +                  if (num_new_palette <= maximum_colors)
  84.419 +                     break;
  84.420 +               }
  84.421 +            }
  84.422 +
  84.423 +            for (i = 0; i < 769; i++)
  84.424 +            {
  84.425 +               if (hash[i] != NULL)
  84.426 +               {
  84.427 +                  png_dsortp p = hash[i];
  84.428 +                  while (p)
  84.429 +                  {
  84.430 +                     t = p->next;
  84.431 +                     png_free(png_ptr, p);
  84.432 +                     p = t;
  84.433 +                  }
  84.434 +               }
  84.435 +               hash[i] = 0;
  84.436 +            }
  84.437 +            max_d += 96;
  84.438 +         }
  84.439 +         png_free(png_ptr, hash);
  84.440 +         png_free(png_ptr, png_ptr->palette_to_index);
  84.441 +         png_free(png_ptr, png_ptr->index_to_palette);
  84.442 +         png_ptr->palette_to_index = NULL;
  84.443 +         png_ptr->index_to_palette = NULL;
  84.444 +      }
  84.445 +      num_palette = maximum_colors;
  84.446 +   }
  84.447 +   if (png_ptr->palette == NULL)
  84.448 +   {
  84.449 +      png_ptr->palette = palette;
  84.450 +   }
  84.451 +   png_ptr->num_palette = (png_uint_16)num_palette;
  84.452 +
  84.453 +   if (full_dither)
  84.454 +   {
  84.455 +      int i;
  84.456 +      png_bytep distance;
  84.457 +      int total_bits = PNG_DITHER_RED_BITS + PNG_DITHER_GREEN_BITS +
  84.458 +         PNG_DITHER_BLUE_BITS;
  84.459 +      int num_red = (1 << PNG_DITHER_RED_BITS);
  84.460 +      int num_green = (1 << PNG_DITHER_GREEN_BITS);
  84.461 +      int num_blue = (1 << PNG_DITHER_BLUE_BITS);
  84.462 +      png_size_t num_entries = ((png_size_t)1 << total_bits);
  84.463 +
  84.464 +      png_ptr->palette_lookup = (png_bytep )png_malloc(png_ptr,
  84.465 +         (png_uint_32)(num_entries * png_sizeof(png_byte)));
  84.466 +
  84.467 +      png_memset(png_ptr->palette_lookup, 0, num_entries *
  84.468 +         png_sizeof(png_byte));
  84.469 +
  84.470 +      distance = (png_bytep)png_malloc(png_ptr, (png_uint_32)(num_entries *
  84.471 +         png_sizeof(png_byte)));
  84.472 +
  84.473 +      png_memset(distance, 0xff, num_entries * png_sizeof(png_byte));
  84.474 +
  84.475 +      for (i = 0; i < num_palette; i++)
  84.476 +      {
  84.477 +         int ir, ig, ib;
  84.478 +         int r = (palette[i].red >> (8 - PNG_DITHER_RED_BITS));
  84.479 +         int g = (palette[i].green >> (8 - PNG_DITHER_GREEN_BITS));
  84.480 +         int b = (palette[i].blue >> (8 - PNG_DITHER_BLUE_BITS));
  84.481 +
  84.482 +         for (ir = 0; ir < num_red; ir++)
  84.483 +         {
  84.484 +            /* int dr = abs(ir - r); */
  84.485 +            int dr = ((ir > r) ? ir - r : r - ir);
  84.486 +            int index_r = (ir << (PNG_DITHER_BLUE_BITS + PNG_DITHER_GREEN_BITS));
  84.487 +
  84.488 +            for (ig = 0; ig < num_green; ig++)
  84.489 +            {
  84.490 +               /* int dg = abs(ig - g); */
  84.491 +               int dg = ((ig > g) ? ig - g : g - ig);
  84.492 +               int dt = dr + dg;
  84.493 +               int dm = ((dr > dg) ? dr : dg);
  84.494 +               int index_g = index_r | (ig << PNG_DITHER_BLUE_BITS);
  84.495 +
  84.496 +               for (ib = 0; ib < num_blue; ib++)
  84.497 +               {
  84.498 +                  int d_index = index_g | ib;
  84.499 +                  /* int db = abs(ib - b); */
  84.500 +                  int db = ((ib > b) ? ib - b : b - ib);
  84.501 +                  int dmax = ((dm > db) ? dm : db);
  84.502 +                  int d = dmax + dt + db;
  84.503 +
  84.504 +                  if (d < (int)distance[d_index])
  84.505 +                  {
  84.506 +                     distance[d_index] = (png_byte)d;
  84.507 +                     png_ptr->palette_lookup[d_index] = (png_byte)i;
  84.508 +                  }
  84.509 +               }
  84.510 +            }
  84.511 +         }
  84.512 +      }
  84.513 +
  84.514 +      png_free(png_ptr, distance);
  84.515 +   }
  84.516 +}
  84.517 +#endif
  84.518 +
  84.519 +#if defined(PNG_READ_GAMMA_SUPPORTED) && defined(PNG_FLOATING_POINT_SUPPORTED)
  84.520 +/* Transform the image from the file_gamma to the screen_gamma.  We
  84.521 + * only do transformations on images where the file_gamma and screen_gamma
  84.522 + * are not close reciprocals, otherwise it slows things down slightly, and
  84.523 + * also needlessly introduces small errors.
  84.524 + *
  84.525 + * We will turn off gamma transformation later if no semitransparent entries
  84.526 + * are present in the tRNS array for palette images.  We can't do it here
  84.527 + * because we don't necessarily have the tRNS chunk yet.
  84.528 + */
  84.529 +void PNGAPI
  84.530 +png_set_gamma(png_structp png_ptr, double scrn_gamma, double file_gamma)
  84.531 +{
  84.532 +   png_debug(1, "in png_set_gamma\n");
  84.533 +   if (png_ptr == NULL) return;
  84.534 +   if ((fabs(scrn_gamma * file_gamma - 1.0) > PNG_GAMMA_THRESHOLD) ||
  84.535 +       (png_ptr->color_type & PNG_COLOR_MASK_ALPHA) ||
  84.536 +       (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE))
  84.537 +     png_ptr->transformations |= PNG_GAMMA;
  84.538 +   png_ptr->gamma = (float)file_gamma;
  84.539 +   png_ptr->screen_gamma = (float)scrn_gamma;
  84.540 +}
  84.541 +#endif
  84.542 +
  84.543 +#if defined(PNG_READ_EXPAND_SUPPORTED)
  84.544 +/* Expand paletted images to RGB, expand grayscale images of
  84.545 + * less than 8-bit depth to 8-bit depth, and expand tRNS chunks
  84.546 + * to alpha channels.
  84.547 + */
  84.548 +void PNGAPI
  84.549 +png_set_expand(png_structp png_ptr)
  84.550 +{
  84.551 +   png_debug(1, "in png_set_expand\n");
  84.552 +   if (png_ptr == NULL) return;
  84.553 +   png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS);
  84.554 +   png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
  84.555 +}
  84.556 +
  84.557 +/* GRR 19990627:  the following three functions currently are identical
  84.558 + *  to png_set_expand().  However, it is entirely reasonable that someone
  84.559 + *  might wish to expand an indexed image to RGB but *not* expand a single,
  84.560 + *  fully transparent palette entry to a full alpha channel--perhaps instead
  84.561 + *  convert tRNS to the grayscale/RGB format (16-bit RGB value), or replace
  84.562 + *  the transparent color with a particular RGB value, or drop tRNS entirely.
  84.563 + *  IOW, a future version of the library may make the transformations flag
  84.564 + *  a bit more fine-grained, with separate bits for each of these three
  84.565 + *  functions.
  84.566 + *
  84.567 + *  More to the point, these functions make it obvious what libpng will be
  84.568 + *  doing, whereas "expand" can (and does) mean any number of things.
  84.569 + *
  84.570 + *  GRP 20060307: In libpng-1.4.0, png_set_gray_1_2_4_to_8() was modified
  84.571 + *  to expand only the sample depth but not to expand the tRNS to alpha.
  84.572 + */
  84.573 +
  84.574 +/* Expand paletted images to RGB. */
  84.575 +void PNGAPI
  84.576 +png_set_palette_to_rgb(png_structp png_ptr)
  84.577 +{
  84.578 +   png_debug(1, "in png_set_palette_to_rgb\n");
  84.579 +   if (png_ptr == NULL) return;
  84.580 +   png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS);
  84.581 +   png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
  84.582 +}
  84.583 +
  84.584 +#if !defined(PNG_1_0_X)
  84.585 +/* Expand grayscale images of less than 8-bit depth to 8 bits. */
  84.586 +void PNGAPI
  84.587 +png_set_expand_gray_1_2_4_to_8(png_structp png_ptr)
  84.588 +{
  84.589 +   png_debug(1, "in png_set_expand_gray_1_2_4_to_8\n");
  84.590 +   if (png_ptr == NULL) return;
  84.591 +   png_ptr->transformations |= PNG_EXPAND;
  84.592 +   png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
  84.593 +}
  84.594 +#endif
  84.595 +
  84.596 +#if defined(PNG_1_0_X) || defined(PNG_1_2_X)
  84.597 +/* Expand grayscale images of less than 8-bit depth to 8 bits. */
  84.598 +/* Deprecated as of libpng-1.2.9 */
  84.599 +void PNGAPI
  84.600 +png_set_gray_1_2_4_to_8(png_structp png_ptr)
  84.601 +{
  84.602 +   png_debug(1, "in png_set_gray_1_2_4_to_8\n");
  84.603 +   if (png_ptr == NULL) return;
  84.604 +   png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS);
  84.605 +}
  84.606 +#endif
  84.607 +
  84.608 +
  84.609 +/* Expand tRNS chunks to alpha channels. */
  84.610 +void PNGAPI
  84.611 +png_set_tRNS_to_alpha(png_structp png_ptr)
  84.612 +{
  84.613 +   png_debug(1, "in png_set_tRNS_to_alpha\n");
  84.614 +   png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS);
  84.615 +   png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
  84.616 +}
  84.617 +#endif /* defined(PNG_READ_EXPAND_SUPPORTED) */
  84.618 +
  84.619 +#if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED)
  84.620 +void PNGAPI
  84.621 +png_set_gray_to_rgb(png_structp png_ptr)
  84.622 +{
  84.623 +   png_debug(1, "in png_set_gray_to_rgb\n");
  84.624 +   png_ptr->transformations |= PNG_GRAY_TO_RGB;
  84.625 +   png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
  84.626 +}
  84.627 +#endif
  84.628 +
  84.629 +#if defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
  84.630 +#if defined(PNG_FLOATING_POINT_SUPPORTED)
  84.631 +/* Convert a RGB image to a grayscale of the same width.  This allows us,
  84.632 + * for example, to convert a 24 bpp RGB image into an 8 bpp grayscale image.
  84.633 + */
  84.634 +
  84.635 +void PNGAPI
  84.636 +png_set_rgb_to_gray(png_structp png_ptr, int error_action, double red,
  84.637 +   double green)
  84.638 +{
  84.639 +      int red_fixed = (int)((float)red*100000.0 + 0.5);
  84.640 +      int green_fixed = (int)((float)green*100000.0 + 0.5);
  84.641 +      if (png_ptr == NULL) return;
  84.642 +      png_set_rgb_to_gray_fixed(png_ptr, error_action, red_fixed, green_fixed);
  84.643 +}
  84.644 +#endif
  84.645 +
  84.646 +void PNGAPI
  84.647 +png_set_rgb_to_gray_fixed(png_structp png_ptr, int error_action,
  84.648 +   png_fixed_point red, png_fixed_point green)
  84.649 +{
  84.650 +   png_debug(1, "in png_set_rgb_to_gray\n");
  84.651 +   if (png_ptr == NULL) return;
  84.652 +   switch(error_action)
  84.653 +   {
  84.654 +      case 1: png_ptr->transformations |= PNG_RGB_TO_GRAY;
  84.655 +              break;
  84.656 +      case 2: png_ptr->transformations |= PNG_RGB_TO_GRAY_WARN;
  84.657 +              break;
  84.658 +      case 3: png_ptr->transformations |= PNG_RGB_TO_GRAY_ERR;
  84.659 +   }
  84.660 +   if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  84.661 +#if defined(PNG_READ_EXPAND_SUPPORTED)
  84.662 +      png_ptr->transformations |= PNG_EXPAND;
  84.663 +#else
  84.664 +   {
  84.665 +      png_warning(png_ptr,
  84.666 +        "Cannot do RGB_TO_GRAY without EXPAND_SUPPORTED.");
  84.667 +      png_ptr->transformations &= ~PNG_RGB_TO_GRAY;
  84.668 +   }
  84.669 +#endif
  84.670 +   {
  84.671 +      png_uint_16 red_int, green_int;
  84.672 +      if (red < 0 || green < 0)
  84.673 +      {
  84.674 +         red_int   =  6968; /* .212671 * 32768 + .5 */
  84.675 +         green_int = 23434; /* .715160 * 32768 + .5 */
  84.676 +      }
  84.677 +      else if (red + green < 100000L)
  84.678 +      {
  84.679 +        red_int = (png_uint_16)(((png_uint_32)red*32768L)/100000L);
  84.680 +        green_int = (png_uint_16)(((png_uint_32)green*32768L)/100000L);
  84.681 +      }
  84.682 +      else
  84.683 +      {
  84.684 +         png_warning(png_ptr, "ignoring out of range rgb_to_gray coefficients");
  84.685 +         red_int   =  6968;
  84.686 +         green_int = 23434;
  84.687 +      }
  84.688 +      png_ptr->rgb_to_gray_red_coeff   = red_int;
  84.689 +      png_ptr->rgb_to_gray_green_coeff = green_int;
  84.690 +      png_ptr->rgb_to_gray_blue_coeff  = 
  84.691 +         (png_uint_16)(32768 - red_int - green_int);
  84.692 +   }
  84.693 +}
  84.694 +#endif
  84.695 +
  84.696 +#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \
  84.697 +    defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED) || \
  84.698 +    defined(PNG_LEGACY_SUPPORTED)
  84.699 +void PNGAPI
  84.700 +png_set_read_user_transform_fn(png_structp png_ptr, png_user_transform_ptr
  84.701 +   read_user_transform_fn)
  84.702 +{
  84.703 +   png_debug(1, "in png_set_read_user_transform_fn\n");
  84.704 +   if (png_ptr == NULL) return;
  84.705 +#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
  84.706 +   png_ptr->transformations |= PNG_USER_TRANSFORM;
  84.707 +   png_ptr->read_user_transform_fn = read_user_transform_fn;
  84.708 +#endif
  84.709 +#ifdef PNG_LEGACY_SUPPORTED
  84.710 +   if (read_user_transform_fn)
  84.711 +      png_warning(png_ptr,
  84.712 +        "This version of libpng does not support user transforms");
  84.713 +#endif
  84.714 +}
  84.715 +#endif
  84.716 +
  84.717 +/* Initialize everything needed for the read.  This includes modifying
  84.718 + * the palette.
  84.719 + */
  84.720 +void /* PRIVATE */
  84.721 +png_init_read_transformations(png_structp png_ptr)
  84.722 +{
  84.723 +   png_debug(1, "in png_init_read_transformations\n");
  84.724 +#if defined(PNG_USELESS_TESTS_SUPPORTED)
  84.725 +   if (png_ptr != NULL)
  84.726 +#endif
  84.727 +  {
  84.728 +#if defined(PNG_READ_BACKGROUND_SUPPORTED) || defined(PNG_READ_SHIFT_SUPPORTED) \
  84.729 + || defined(PNG_READ_GAMMA_SUPPORTED)
  84.730 +   int color_type = png_ptr->color_type;
  84.731 +#endif
  84.732 +
  84.733 +#if defined(PNG_READ_EXPAND_SUPPORTED) && defined(PNG_READ_BACKGROUND_SUPPORTED)
  84.734 +
  84.735 +#if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED)
  84.736 +   /* Detect gray background and attempt to enable optimization
  84.737 +    * for gray --> RGB case */
  84.738 +   /* Note:  if PNG_BACKGROUND_EXPAND is set and color_type is either RGB or
  84.739 +    * RGB_ALPHA (in which case need_expand is superfluous anyway), the
  84.740 +    * background color might actually be gray yet not be flagged as such.
  84.741 +    * This is not a problem for the current code, which uses
  84.742 +    * PNG_BACKGROUND_IS_GRAY only to decide when to do the
  84.743 +    * png_do_gray_to_rgb() transformation.
  84.744 +    */
  84.745 +   if ((png_ptr->transformations & PNG_BACKGROUND_EXPAND) &&
  84.746 +       !(color_type & PNG_COLOR_MASK_COLOR))
  84.747 +   {
  84.748 +          png_ptr->mode |= PNG_BACKGROUND_IS_GRAY;
  84.749 +   } else if ((png_ptr->transformations & PNG_BACKGROUND) &&
  84.750 +              !(png_ptr->transformations & PNG_BACKGROUND_EXPAND) &&
  84.751 +              (png_ptr->transformations & PNG_GRAY_TO_RGB) &&
  84.752 +              png_ptr->background.red == png_ptr->background.green &&
  84.753 +              png_ptr->background.red == png_ptr->background.blue)
  84.754 +   {
  84.755 +          png_ptr->mode |= PNG_BACKGROUND_IS_GRAY;
  84.756 +          png_ptr->background.gray = png_ptr->background.red;
  84.757 +   }
  84.758 +#endif
  84.759 +
  84.760 +   if ((png_ptr->transformations & PNG_BACKGROUND_EXPAND) &&
  84.761 +       (png_ptr->transformations & PNG_EXPAND))
  84.762 +   {
  84.763 +      if (!(color_type & PNG_COLOR_MASK_COLOR))  /* i.e., GRAY or GRAY_ALPHA */
  84.764 +      {
  84.765 +         /* expand background and tRNS chunks */
  84.766 +         switch (png_ptr->bit_depth)
  84.767 +         {
  84.768 +            case 1:
  84.769 +               png_ptr->background.gray *= (png_uint_16)0xff;
  84.770 +               png_ptr->background.red = png_ptr->background.green
  84.771 +                 =  png_ptr->background.blue = png_ptr->background.gray;
  84.772 +               if (!(png_ptr->transformations & PNG_EXPAND_tRNS))
  84.773 +               {
  84.774 +                 png_ptr->trans_values.gray *= (png_uint_16)0xff;
  84.775 +                 png_ptr->trans_values.red = png_ptr->trans_values.green
  84.776 +                   = png_ptr->trans_values.blue = png_ptr->trans_values.gray;
  84.777 +               }
  84.778 +               break;
  84.779 +            case 2:
  84.780 +               png_ptr->background.gray *= (png_uint_16)0x55;
  84.781 +               png_ptr->background.red = png_ptr->background.green
  84.782 +                 = png_ptr->background.blue = png_ptr->background.gray;
  84.783 +               if (!(png_ptr->transformations & PNG_EXPAND_tRNS))
  84.784 +               {
  84.785 +                 png_ptr->trans_values.gray *= (png_uint_16)0x55;
  84.786 +                 png_ptr->trans_values.red = png_ptr->trans_values.green
  84.787 +                   = png_ptr->trans_values.blue = png_ptr->trans_values.gray;
  84.788 +               }
  84.789 +               break;
  84.790 +            case 4:
  84.791 +               png_ptr->background.gray *= (png_uint_16)0x11;
  84.792 +               png_ptr->background.red = png_ptr->background.green
  84.793 +                 = png_ptr->background.blue = png_ptr->background.gray;
  84.794 +               if (!(png_ptr->transformations & PNG_EXPAND_tRNS))
  84.795 +               {
  84.796 +                 png_ptr->trans_values.gray *= (png_uint_16)0x11;
  84.797 +                 png_ptr->trans_values.red = png_ptr->trans_values.green
  84.798 +                   = png_ptr->trans_values.blue = png_ptr->trans_values.gray;
  84.799 +               }
  84.800 +               break;
  84.801 +            case 8:
  84.802 +            case 16:
  84.803 +               png_ptr->background.red = png_ptr->background.green
  84.804 +                 = png_ptr->background.blue = png_ptr->background.gray;
  84.805 +               break;
  84.806 +         }
  84.807 +      }
  84.808 +      else if (color_type == PNG_COLOR_TYPE_PALETTE)
  84.809 +      {
  84.810 +         png_ptr->background.red   =
  84.811 +            png_ptr->palette[png_ptr->background.index].red;
  84.812 +         png_ptr->background.green =
  84.813 +            png_ptr->palette[png_ptr->background.index].green;
  84.814 +         png_ptr->background.blue  =
  84.815 +            png_ptr->palette[png_ptr->background.index].blue;
  84.816 +
  84.817 +#if defined(PNG_READ_INVERT_ALPHA_SUPPORTED)
  84.818 +        if (png_ptr->transformations & PNG_INVERT_ALPHA)
  84.819 +        {
  84.820 +#if defined(PNG_READ_EXPAND_SUPPORTED)
  84.821 +           if (!(png_ptr->transformations & PNG_EXPAND_tRNS))
  84.822 +#endif
  84.823 +           {
  84.824 +           /* invert the alpha channel (in tRNS) unless the pixels are
  84.825 +              going to be expanded, in which case leave it for later */
  84.826 +              int i, istop;
  84.827 +              istop=(int)png_ptr->num_trans;
  84.828 +              for (i=0; i<istop; i++)
  84.829 +                 png_ptr->trans[i] = (png_byte)(255 - png_ptr->trans[i]);
  84.830 +           }
  84.831 +        }
  84.832 +#endif
  84.833 +
  84.834 +      }
  84.835 +   }
  84.836 +#endif
  84.837 +
  84.838 +#if defined(PNG_READ_BACKGROUND_SUPPORTED) && defined(PNG_READ_GAMMA_SUPPORTED)
  84.839 +   png_ptr->background_1 = png_ptr->background;
  84.840 +#endif
  84.841 +#if defined(PNG_READ_GAMMA_SUPPORTED) && defined(PNG_FLOATING_POINT_SUPPORTED)
  84.842 +
  84.843 +   if ((color_type == PNG_COLOR_TYPE_PALETTE && png_ptr->num_trans != 0)
  84.844 +       && (fabs(png_ptr->screen_gamma * png_ptr->gamma - 1.0)
  84.845 +         < PNG_GAMMA_THRESHOLD))
  84.846 +   {
  84.847 +    int i, k;
  84.848 +    k=0;
  84.849 +    for (i=0; i<png_ptr->num_trans; i++)
  84.850 +    {
  84.851 +      if (png_ptr->trans[i] != 0 && png_ptr->trans[i] != 0xff)
  84.852 +        k=1; /* partial transparency is present */
  84.853 +    }
  84.854 +    if (k == 0)
  84.855 +      png_ptr->transformations &= ~PNG_GAMMA;
  84.856 +   }
  84.857 +
  84.858 +   if ((png_ptr->transformations & (PNG_GAMMA | PNG_RGB_TO_GRAY)) &&
  84.859 +        png_ptr->gamma != 0.0)
  84.860 +   {
  84.861 +      png_build_gamma_table(png_ptr);
  84.862 +#if defined(PNG_READ_BACKGROUND_SUPPORTED)
  84.863 +      if (png_ptr->transformations & PNG_BACKGROUND)
  84.864 +      {
  84.865 +         if (color_type == PNG_COLOR_TYPE_PALETTE)
  84.866 +         {
  84.867 +           /* could skip if no transparency and
  84.868 +           */
  84.869 +            png_color back, back_1;
  84.870 +            png_colorp palette = png_ptr->palette;
  84.871 +            int num_palette = png_ptr->num_palette;
  84.872 +            int i;
  84.873 +            if (png_ptr->background_gamma_type == PNG_BACKGROUND_GAMMA_FILE)
  84.874 +            {
  84.875 +               back.red = png_ptr->gamma_table[png_ptr->background.red];
  84.876 +               back.green = png_ptr->gamma_table[png_ptr->background.green];
  84.877 +               back.blue = png_ptr->gamma_table[png_ptr->background.blue];
  84.878 +
  84.879 +               back_1.red = png_ptr->gamma_to_1[png_ptr->background.red];
  84.880 +               back_1.green = png_ptr->gamma_to_1[png_ptr->background.green];
  84.881 +               back_1.blue = png_ptr->gamma_to_1[png_ptr->background.blue];
  84.882 +            }
  84.883 +            else
  84.884 +            {
  84.885 +               double g, gs;
  84.886 +
  84.887 +               switch (png_ptr->background_gamma_type)
  84.888 +               {
  84.889 +                  case PNG_BACKGROUND_GAMMA_SCREEN:
  84.890 +                     g = (png_ptr->screen_gamma);
  84.891 +                     gs = 1.0;
  84.892 +                     break;
  84.893 +                  case PNG_BACKGROUND_GAMMA_FILE:
  84.894 +                     g = 1.0 / (png_ptr->gamma);
  84.895 +                     gs = 1.0 / (png_ptr->gamma * png_ptr->screen_gamma);
  84.896 +                     break;
  84.897 +                  case PNG_BACKGROUND_GAMMA_UNIQUE:
  84.898 +                     g = 1.0 / (png_ptr->background_gamma);
  84.899 +                     gs = 1.0 / (png_ptr->background_gamma *
  84.900 +                                 png_ptr->screen_gamma);
  84.901 +                     break;
  84.902 +                  default:
  84.903 +                     g = 1.0;    /* back_1 */
  84.904 +                     gs = 1.0;   /* back */
  84.905 +               }
  84.906 +
  84.907 +               if ( fabs(gs - 1.0) < PNG_GAMMA_THRESHOLD)
  84.908 +               {
  84.909 +                  back.red   = (png_byte)png_ptr->background.red;
  84.910 +                  back.green = (png_byte)png_ptr->background.green;
  84.911 +                  back.blue  = (png_byte)png_ptr->background.blue;
  84.912 +               }
  84.913 +               else
  84.914 +               {
  84.915 +                  back.red = (png_byte)(pow(
  84.916 +                     (double)png_ptr->background.red/255, gs) * 255.0 + .5);
  84.917 +                  back.green = (png_byte)(pow(
  84.918 +                     (double)png_ptr->background.green/255, gs) * 255.0 + .5);
  84.919 +                  back.blue = (png_byte)(pow(
  84.920 +                     (double)png_ptr->background.blue/255, gs) * 255.0 + .5);
  84.921 +               }
  84.922 +
  84.923 +               back_1.red = (png_byte)(pow(
  84.924 +                  (double)png_ptr->background.red/255, g) * 255.0 + .5);
  84.925 +               back_1.green = (png_byte)(pow(
  84.926 +                  (double)png_ptr->background.green/255, g) * 255.0 + .5);
  84.927 +               back_1.blue = (png_byte)(pow(
  84.928 +                  (double)png_ptr->background.blue/255, g) * 255.0 + .5);
  84.929 +            }
  84.930 +            for (i = 0; i < num_palette; i++)
  84.931 +            {
  84.932 +               if (i < (int)png_ptr->num_trans && png_ptr->trans[i] != 0xff)
  84.933 +               {
  84.934 +                  if (png_ptr->trans[i] == 0)
  84.935 +                  {
  84.936 +                     palette[i] = back;
  84.937 +                  }
  84.938 +                  else /* if (png_ptr->trans[i] != 0xff) */
  84.939 +                  {
  84.940 +                     png_byte v, w;
  84.941 +
  84.942 +                     v = png_ptr->gamma_to_1[palette[i].red];
  84.943 +                     png_composite(w, v, png_ptr->trans[i], back_1.red);
  84.944 +                     palette[i].red = png_ptr->gamma_from_1[w];
  84.945 +
  84.946 +                     v = png_ptr->gamma_to_1[palette[i].green];
  84.947 +                     png_composite(w, v, png_ptr->trans[i], back_1.green);
  84.948 +                     palette[i].green = png_ptr->gamma_from_1[w];
  84.949 +
  84.950 +                     v = png_ptr->gamma_to_1[palette[i].blue];
  84.951 +                     png_composite(w, v, png_ptr->trans[i], back_1.blue);
  84.952 +                     palette[i].blue = png_ptr->gamma_from_1[w];
  84.953 +                  }
  84.954 +               }
  84.955 +               else
  84.956 +               {
  84.957 +                  palette[i].red = png_ptr->gamma_table[palette[i].red];
  84.958 +                  palette[i].green = png_ptr->gamma_table[palette[i].green];
  84.959 +                  palette[i].blue = png_ptr->gamma_table[palette[i].blue];
  84.960 +               }
  84.961 +            }
  84.962 +	    /* Prevent the transformations being done again, and make sure
  84.963 +	     * that the now spurious alpha channel is stripped - the code
  84.964 +	     * has just reduced background composition and gamma correction
  84.965 +	     * to a simple alpha channel strip.
  84.966 +	     */
  84.967 +	    png_ptr->transformations &= ~PNG_BACKGROUND;
  84.968 +	    png_ptr->transformations &= ~PNG_GAMMA;
  84.969 +	    png_ptr->transformations |= PNG_STRIP_ALPHA;
  84.970 +         }
  84.971 +         /* if (png_ptr->background_gamma_type!=PNG_BACKGROUND_GAMMA_UNKNOWN) */
  84.972 +         else
  84.973 +         /* color_type != PNG_COLOR_TYPE_PALETTE */
  84.974 +         {
  84.975 +            double m = (double)(((png_uint_32)1 << png_ptr->bit_depth) - 1);
  84.976 +            double g = 1.0;
  84.977 +            double gs = 1.0;
  84.978 +
  84.979 +            switch (png_ptr->background_gamma_type)
  84.980 +            {
  84.981 +               case PNG_BACKGROUND_GAMMA_SCREEN:
  84.982 +                  g = (png_ptr->screen_gamma);
  84.983 +                  gs = 1.0;
  84.984 +                  break;
  84.985 +               case PNG_BACKGROUND_GAMMA_FILE:
  84.986 +                  g = 1.0 / (png_ptr->gamma);
  84.987 +                  gs = 1.0 / (png_ptr->gamma * png_ptr->screen_gamma);
  84.988 +                  break;
  84.989 +               case PNG_BACKGROUND_GAMMA_UNIQUE:
  84.990 +                  g = 1.0 / (png_ptr->background_gamma);
  84.991 +                  gs = 1.0 / (png_ptr->background_gamma *
  84.992 +                     png_ptr->screen_gamma);
  84.993 +                  break;
  84.994 +            }
  84.995 +
  84.996 +            png_ptr->background_1.gray = (png_uint_16)(pow(
  84.997 +               (double)png_ptr->background.gray / m, g) * m + .5);
  84.998 +            png_ptr->background.gray = (png_uint_16)(pow(
  84.999 +               (double)png_ptr->background.gray / m, gs) * m + .5);
 84.1000 +
 84.1001 +            if ((png_ptr->background.red != png_ptr->background.green) ||
 84.1002 +                (png_ptr->background.red != png_ptr->background.blue) ||
 84.1003 +                (png_ptr->background.red != png_ptr->background.gray))
 84.1004 +            {
 84.1005 +               /* RGB or RGBA with color background */
 84.1006 +               png_ptr->background_1.red = (png_uint_16)(pow(
 84.1007 +                  (double)png_ptr->background.red / m, g) * m + .5);
 84.1008 +               png_ptr->background_1.green = (png_uint_16)(pow(
 84.1009 +                  (double)png_ptr->background.green / m, g) * m + .5);
 84.1010 +               png_ptr->background_1.blue = (png_uint_16)(pow(
 84.1011 +                  (double)png_ptr->background.blue / m, g) * m + .5);
 84.1012 +               png_ptr->background.red = (png_uint_16)(pow(
 84.1013 +                  (double)png_ptr->background.red / m, gs) * m + .5);
 84.1014 +               png_ptr->background.green = (png_uint_16)(pow(
 84.1015 +                  (double)png_ptr->background.green / m, gs) * m + .5);
 84.1016 +               png_ptr->background.blue = (png_uint_16)(pow(
 84.1017 +                  (double)png_ptr->background.blue / m, gs) * m + .5);
 84.1018 +            }
 84.1019 +            else
 84.1020 +            {
 84.1021 +               /* GRAY, GRAY ALPHA, RGB, or RGBA with gray background */
 84.1022 +               png_ptr->background_1.red = png_ptr->background_1.green
 84.1023 +                 = png_ptr->background_1.blue = png_ptr->background_1.gray;
 84.1024 +               png_ptr->background.red = png_ptr->background.green
 84.1025 +                 = png_ptr->background.blue = png_ptr->background.gray;
 84.1026 +            }
 84.1027 +         }
 84.1028 +      }
 84.1029 +      else
 84.1030 +      /* transformation does not include PNG_BACKGROUND */
 84.1031 +#endif /* PNG_READ_BACKGROUND_SUPPORTED */
 84.1032 +      if (color_type == PNG_COLOR_TYPE_PALETTE)
 84.1033 +      {
 84.1034 +         png_colorp palette = png_ptr->palette;
 84.1035 +         int num_palette = png_ptr->num_palette;
 84.1036 +         int i;
 84.1037 +
 84.1038 +         for (i = 0; i < num_palette; i++)
 84.1039 +         {
 84.1040 +            palette[i].red = png_ptr->gamma_table[palette[i].red];
 84.1041 +            palette[i].green = png_ptr->gamma_table[palette[i].green];
 84.1042 +            palette[i].blue = png_ptr->gamma_table[palette[i].blue];
 84.1043 +         }
 84.1044 +
 84.1045 +	 /* Done the gamma correction. */
 84.1046 +	 png_ptr->transformations &= ~PNG_GAMMA;
 84.1047 +      }
 84.1048 +   }
 84.1049 +#if defined(PNG_READ_BACKGROUND_SUPPORTED)
 84.1050 +   else
 84.1051 +#endif
 84.1052 +#endif /* PNG_READ_GAMMA_SUPPORTED && PNG_FLOATING_POINT_SUPPORTED */
 84.1053 +#if defined(PNG_READ_BACKGROUND_SUPPORTED)
 84.1054 +   /* No GAMMA transformation */
 84.1055 +   if ((png_ptr->transformations & PNG_BACKGROUND) &&
 84.1056 +       (color_type == PNG_COLOR_TYPE_PALETTE))
 84.1057 +   {
 84.1058 +      int i;
 84.1059 +      int istop = (int)png_ptr->num_trans;
 84.1060 +      png_color back;
 84.1061 +      png_colorp palette = png_ptr->palette;
 84.1062 +
 84.1063 +      back.red   = (png_byte)png_ptr->background.red;
 84.1064 +      back.green = (png_byte)png_ptr->background.green;
 84.1065 +      back.blue  = (png_byte)png_ptr->background.blue;
 84.1066 +
 84.1067 +      for (i = 0; i < istop; i++)
 84.1068 +      {
 84.1069 +         if (png_ptr->trans[i] == 0)
 84.1070 +         {
 84.1071 +            palette[i] = back;
 84.1072 +         }
 84.1073 +         else if (png_ptr->trans[i] != 0xff)
 84.1074 +         {
 84.1075 +            /* The png_composite() macro is defined in png.h */
 84.1076 +            png_composite(palette[i].red, palette[i].red,
 84.1077 +               png_ptr->trans[i], back.red);
 84.1078 +            png_composite(palette[i].green, palette[i].green,
 84.1079 +               png_ptr->trans[i], back.green);
 84.1080 +            png_composite(palette[i].blue, palette[i].blue,
 84.1081 +               png_ptr->trans[i], back.blue);
 84.1082 +         }
 84.1083 +      }
 84.1084 +
 84.1085 +      /* Handled alpha, still need to strip the channel. */
 84.1086 +      png_ptr->transformations &= ~PNG_BACKGROUND;
 84.1087 +      png_ptr->transformations |= PNG_STRIP_ALPHA;
 84.1088 +   }
 84.1089 +#endif /* PNG_READ_BACKGROUND_SUPPORTED */
 84.1090 +
 84.1091 +#if defined(PNG_READ_SHIFT_SUPPORTED)
 84.1092 +   if ((png_ptr->transformations & PNG_SHIFT) &&
 84.1093 +      (color_type == PNG_COLOR_TYPE_PALETTE))
 84.1094 +   {
 84.1095 +      png_uint_16 i;
 84.1096 +      png_uint_16 istop = png_ptr->num_palette;
 84.1097 +      int sr = 8 - png_ptr->sig_bit.red;
 84.1098 +      int sg = 8 - png_ptr->sig_bit.green;
 84.1099 +      int sb = 8 - png_ptr->sig_bit.blue;
 84.1100 +
 84.1101 +      if (sr < 0 || sr > 8)
 84.1102 +         sr = 0;
 84.1103 +      if (sg < 0 || sg > 8)
 84.1104 +         sg = 0;
 84.1105 +      if (sb < 0 || sb > 8)
 84.1106 +         sb = 0;
 84.1107 +      for (i = 0; i < istop; i++)
 84.1108 +      {
 84.1109 +         png_ptr->palette[i].red >>= sr;
 84.1110 +         png_ptr->palette[i].green >>= sg;
 84.1111 +         png_ptr->palette[i].blue >>= sb;
 84.1112 +      }
 84.1113 +   }
 84.1114 +#endif  /* PNG_READ_SHIFT_SUPPORTED */
 84.1115 + }
 84.1116 +#if !defined(PNG_READ_GAMMA_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED) \
 84.1117 + && !defined(PNG_READ_BACKGROUND_SUPPORTED)
 84.1118 +   if (png_ptr)
 84.1119 +      return;
 84.1120 +#endif
 84.1121 +}
 84.1122 +
 84.1123 +/* Modify the info structure to reflect the transformations.  The
 84.1124 + * info should be updated so a PNG file could be written with it,
 84.1125 + * assuming the transformations result in valid PNG data.
 84.1126 + */
 84.1127 +void /* PRIVATE */
 84.1128 +png_read_transform_info(png_structp png_ptr, png_infop info_ptr)
 84.1129 +{
 84.1130 +   png_debug(1, "in png_read_transform_info\n");
 84.1131 +#if defined(PNG_READ_EXPAND_SUPPORTED)
 84.1132 +   if (png_ptr->transformations & PNG_EXPAND)
 84.1133 +   {
 84.1134 +      if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
 84.1135 +      {
 84.1136 +         if (png_ptr->num_trans &&
 84.1137 +              (png_ptr->transformations & PNG_EXPAND_tRNS))
 84.1138 +            info_ptr->color_type = PNG_COLOR_TYPE_RGB_ALPHA;
 84.1139 +         else
 84.1140 +            info_ptr->color_type = PNG_COLOR_TYPE_RGB;
 84.1141 +         info_ptr->bit_depth = 8;
 84.1142 +         info_ptr->num_trans = 0;
 84.1143 +      }
 84.1144 +      else
 84.1145 +      {
 84.1146 +         if (png_ptr->num_trans)
 84.1147 +         {
 84.1148 +            if (png_ptr->transformations & PNG_EXPAND_tRNS)
 84.1149 +              info_ptr->color_type |= PNG_COLOR_MASK_ALPHA;
 84.1150 +#if 0 /* Removed from libpng-1.2.27 */
 84.1151 +            else
 84.1152 +              info_ptr->color_type |= PNG_COLOR_MASK_COLOR;
 84.1153 +#endif
 84.1154 +         }
 84.1155 +         if (info_ptr->bit_depth < 8)
 84.1156 +            info_ptr->bit_depth = 8;
 84.1157 +         info_ptr->num_trans = 0;
 84.1158 +      }
 84.1159 +   }
 84.1160 +#endif
 84.1161 +
 84.1162 +#if defined(PNG_READ_BACKGROUND_SUPPORTED)
 84.1163 +   if (png_ptr->transformations & PNG_BACKGROUND)
 84.1164 +   {
 84.1165 +      info_ptr->color_type &= ~PNG_COLOR_MASK_ALPHA;
 84.1166 +      info_ptr->num_trans = 0;
 84.1167 +      info_ptr->background = png_ptr->background;
 84.1168 +   }
 84.1169 +#endif
 84.1170 +
 84.1171 +#if defined(PNG_READ_GAMMA_SUPPORTED)
 84.1172 +   if (png_ptr->transformations & PNG_GAMMA)
 84.1173 +   {
 84.1174 +#ifdef PNG_FLOATING_POINT_SUPPORTED
 84.1175 +      info_ptr->gamma = png_ptr->gamma;
 84.1176 +#endif
 84.1177 +#ifdef PNG_FIXED_POINT_SUPPORTED
 84.1178 +      info_ptr->int_gamma = png_ptr->int_gamma;
 84.1179 +#endif
 84.1180 +   }
 84.1181 +#endif
 84.1182 +
 84.1183 +#if defined(PNG_READ_16_TO_8_SUPPORTED)
 84.1184 +   if ((png_ptr->transformations & PNG_16_TO_8) && (info_ptr->bit_depth == 16))
 84.1185 +      info_ptr->bit_depth = 8;
 84.1186 +#endif
 84.1187 +
 84.1188 +#if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED)
 84.1189 +   if (png_ptr->transformations & PNG_GRAY_TO_RGB)
 84.1190 +      info_ptr->color_type |= PNG_COLOR_MASK_COLOR;
 84.1191 +#endif
 84.1192 +
 84.1193 +#if defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
 84.1194 +   if (png_ptr->transformations & PNG_RGB_TO_GRAY)
 84.1195 +      info_ptr->color_type &= ~PNG_COLOR_MASK_COLOR;
 84.1196 +#endif
 84.1197 +
 84.1198 +#if defined(PNG_READ_DITHER_SUPPORTED)
 84.1199 +   if (png_ptr->transformations & PNG_DITHER)
 84.1200 +   {
 84.1201 +      if (((info_ptr->color_type == PNG_COLOR_TYPE_RGB) ||
 84.1202 +         (info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)) &&
 84.1203 +         png_ptr->palette_lookup && info_ptr->bit_depth == 8)
 84.1204 +      {
 84.1205 +         info_ptr->color_type = PNG_COLOR_TYPE_PALETTE;
 84.1206 +      }
 84.1207 +   }
 84.1208 +#endif
 84.1209 +
 84.1210 +#if defined(PNG_READ_PACK_SUPPORTED)
 84.1211 +   if ((png_ptr->transformations & PNG_PACK) && (info_ptr->bit_depth < 8))
 84.1212 +      info_ptr->bit_depth = 8;
 84.1213 +#endif
 84.1214 +
 84.1215 +   if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
 84.1216 +      info_ptr->channels = 1;
 84.1217 +   else if (info_ptr->color_type & PNG_COLOR_MASK_COLOR)
 84.1218 +      info_ptr->channels = 3;
 84.1219 +   else
 84.1220 +      info_ptr->channels = 1;
 84.1221 +
 84.1222 +#if defined(PNG_READ_STRIP_ALPHA_SUPPORTED)
 84.1223 +   if (png_ptr->flags & PNG_FLAG_STRIP_ALPHA)
 84.1224 +      info_ptr->color_type &= ~PNG_COLOR_MASK_ALPHA;
 84.1225 +#endif
 84.1226 +
 84.1227 +   if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA)
 84.1228 +      info_ptr->channels++;
 84.1229 +
 84.1230 +#if defined(PNG_READ_FILLER_SUPPORTED)
 84.1231 +   /* STRIP_ALPHA and FILLER allowed:  MASK_ALPHA bit stripped above */
 84.1232 +   if ((png_ptr->transformations & PNG_FILLER) &&
 84.1233 +       ((info_ptr->color_type == PNG_COLOR_TYPE_RGB) ||
 84.1234 +       (info_ptr->color_type == PNG_COLOR_TYPE_GRAY)))
 84.1235 +   {
 84.1236 +      info_ptr->channels++;
 84.1237 +      /* if adding a true alpha channel not just filler */
 84.1238 +#if !defined(PNG_1_0_X)
 84.1239 +      if (png_ptr->transformations & PNG_ADD_ALPHA)
 84.1240 +        info_ptr->color_type |= PNG_COLOR_MASK_ALPHA;
 84.1241 +#endif
 84.1242 +   }
 84.1243 +#endif
 84.1244 +
 84.1245 +#if defined(PNG_USER_TRANSFORM_PTR_SUPPORTED) && \
 84.1246 +defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
 84.1247 +   if (png_ptr->transformations & PNG_USER_TRANSFORM)
 84.1248 +     {
 84.1249 +       if (info_ptr->bit_depth < png_ptr->user_transform_depth)
 84.1250 +         info_ptr->bit_depth = png_ptr->user_transform_depth;
 84.1251 +       if (info_ptr->channels < png_ptr->user_transform_channels)
 84.1252 +         info_ptr->channels = png_ptr->user_transform_channels;
 84.1253 +     }
 84.1254 +#endif
 84.1255 +
 84.1256 +   info_ptr->pixel_depth = (png_byte)(info_ptr->channels *
 84.1257 +      info_ptr->bit_depth);
 84.1258 +
 84.1259 +   info_ptr->rowbytes = PNG_ROWBYTES(info_ptr->pixel_depth, info_ptr->width);
 84.1260 +
 84.1261 +#if !defined(PNG_READ_EXPAND_SUPPORTED)
 84.1262 +   if (png_ptr)
 84.1263 +      return;
 84.1264 +#endif
 84.1265 +}
 84.1266 +
 84.1267 +/* Transform the row.  The order of transformations is significant,
 84.1268 + * and is very touchy.  If you add a transformation, take care to
 84.1269 + * decide how it fits in with the other transformations here.
 84.1270 + */
 84.1271 +void /* PRIVATE */
 84.1272 +png_do_read_transformations(png_structp png_ptr)
 84.1273 +{
 84.1274 +   png_debug(1, "in png_do_read_transformations\n");
 84.1275 +   if (png_ptr->row_buf == NULL)
 84.1276 +   {
 84.1277 +#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
 84.1278 +      char msg[50];
 84.1279 +
 84.1280 +      png_snprintf2(msg, 50,
 84.1281 +         "NULL row buffer for row %ld, pass %d", (long)png_ptr->row_number,
 84.1282 +         png_ptr->pass);
 84.1283 +      png_error(png_ptr, msg);
 84.1284 +#else
 84.1285 +      png_error(png_ptr, "NULL row buffer");
 84.1286 +#endif
 84.1287 +   }
 84.1288 +#ifdef PNG_WARN_UNINITIALIZED_ROW
 84.1289 +   if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
 84.1290 +      /* Application has failed to call either png_read_start_image()
 84.1291 +       * or png_read_update_info() after setting transforms that expand
 84.1292 +       * pixels.  This check added to libpng-1.2.19 */
 84.1293 +#if (PNG_WARN_UNINITIALIZED_ROW==1)
 84.1294 +      png_error(png_ptr, "Uninitialized row");
 84.1295 +#else
 84.1296 +      png_warning(png_ptr, "Uninitialized row");
 84.1297 +#endif
 84.1298 +#endif
 84.1299 +
 84.1300 +#if defined(PNG_READ_EXPAND_SUPPORTED)
 84.1301 +   if (png_ptr->transformations & PNG_EXPAND)
 84.1302 +   {
 84.1303 +      if (png_ptr->row_info.color_type == PNG_COLOR_TYPE_PALETTE)
 84.1304 +      {
 84.1305 +         png_do_expand_palette(&(png_ptr->row_info), png_ptr->row_buf + 1,
 84.1306 +            png_ptr->palette, png_ptr->trans, png_ptr->num_trans);
 84.1307 +      }
 84.1308 +      else
 84.1309 +      {
 84.1310 +         if (png_ptr->num_trans &&
 84.1311 +             (png_ptr->transformations & PNG_EXPAND_tRNS))
 84.1312 +            png_do_expand(&(png_ptr->row_info), png_ptr->row_buf + 1,
 84.1313 +               &(png_ptr->trans_values));
 84.1314 +         else
 84.1315 +            png_do_expand(&(png_ptr->row_info), png_ptr->row_buf + 1,
 84.1316 +               NULL);
 84.1317 +      }
 84.1318 +   }
 84.1319 +#endif
 84.1320 +
 84.1321 +#if defined(PNG_READ_STRIP_ALPHA_SUPPORTED)
 84.1322 +   if (png_ptr->flags & PNG_FLAG_STRIP_ALPHA)
 84.1323 +      png_do_strip_filler(&(png_ptr->row_info), png_ptr->row_buf + 1,
 84.1324 +         PNG_FLAG_FILLER_AFTER | (png_ptr->flags & PNG_FLAG_STRIP_ALPHA));
 84.1325 +#endif
 84.1326 +
 84.1327 +#if defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
 84.1328 +   if (png_ptr->transformations & PNG_RGB_TO_GRAY)
 84.1329 +   {
 84.1330 +      int rgb_error =
 84.1331 +         png_do_rgb_to_gray(png_ptr, &(png_ptr->row_info), png_ptr->row_buf + 1);
 84.1332 +      if (rgb_error)
 84.1333 +      {
 84.1334 +         png_ptr->rgb_to_gray_status=1;
 84.1335 +         if ((png_ptr->transformations & PNG_RGB_TO_GRAY) == 
 84.1336 +             PNG_RGB_TO_GRAY_WARN)
 84.1337 +            png_warning(png_ptr, "png_do_rgb_to_gray found nongray pixel");
 84.1338 +         if ((png_ptr->transformations & PNG_RGB_TO_GRAY) ==
 84.1339 +             PNG_RGB_TO_GRAY_ERR)
 84.1340 +            png_error(png_ptr, "png_do_rgb_to_gray found nongray pixel");
 84.1341 +      }
 84.1342 +   }
 84.1343 +#endif
 84.1344 +
 84.1345 +/*
 84.1346 +From Andreas Dilger e-mail to png-implement, 26 March 1998:
 84.1347 +
 84.1348 +  In most cases, the "simple transparency" should be done prior to doing
 84.1349 +  gray-to-RGB, or you will have to test 3x as many bytes to check if a
 84.1350 +  pixel is transparent.  You would also need to make sure that the
 84.1351 +  transparency information is upgraded to RGB.
 84.1352 +
 84.1353 +  To summarize, the current flow is:
 84.1354 +  - Gray + simple transparency -> compare 1 or 2 gray bytes and composite
 84.1355 +                                  with background "in place" if transparent,
 84.1356 +                                  convert to RGB if necessary
 84.1357 +  - Gray + alpha -> composite with gray background and remove alpha bytes,
 84.1358 +                                  convert to RGB if necessary
 84.1359 +
 84.1360 +  To support RGB backgrounds for gray images we need:
 84.1361 +  - Gray + simple transparency -> convert to RGB + simple transparency, compare
 84.1362 +                                  3 or 6 bytes and composite with background
 84.1363 +                                  "in place" if transparent (3x compare/pixel
 84.1364 +                                  compared to doing composite with gray bkgrnd)
 84.1365 +  - Gray + alpha -> convert to RGB + alpha, composite with background and
 84.1366 +                                  remove alpha bytes (3x float operations/pixel
 84.1367 +                                  compared with composite on gray background)
 84.1368 +
 84.1369 +  Greg's change will do this.  The reason it wasn't done before is for
 84.1370 +  performance, as this increases the per-pixel operations.  If we would check
 84.1371 +  in advance if the background was gray or RGB, and position the gray-to-RGB
 84.1372 +  transform appropriately, then it would save a lot of work/time.
 84.1373 + */
 84.1374 +
 84.1375 +#if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED)
 84.1376 +   /* if gray -> RGB, do so now only if background is non-gray; else do later
 84.1377 +    * for performance reasons */
 84.1378 +   if ((png_ptr->transformations & PNG_GRAY_TO_RGB) &&
 84.1379 +       !(png_ptr->mode & PNG_BACKGROUND_IS_GRAY))
 84.1380 +      png_do_gray_to_rgb(&(png_ptr->row_info), png_ptr->row_buf + 1);
 84.1381 +#endif
 84.1382 +
 84.1383 +#if defined(PNG_READ_BACKGROUND_SUPPORTED)
 84.1384 +   if ((png_ptr->transformations & PNG_BACKGROUND) &&
 84.1385 +      ((png_ptr->num_trans != 0 ) ||
 84.1386 +      (png_ptr->color_type & PNG_COLOR_MASK_ALPHA)))
 84.1387 +      png_do_background(&(png_ptr->row_info), png_ptr->row_buf + 1,
 84.1388 +         &(png_ptr->trans_values), &(png_ptr->background)
 84.1389 +#if defined(PNG_READ_GAMMA_SUPPORTED)
 84.1390 +         , &(png_ptr->background_1),
 84.1391 +         png_ptr->gamma_table, png_ptr->gamma_from_1,
 84.1392 +         png_ptr->gamma_to_1, png_ptr->gamma_16_table,
 84.1393 +         png_ptr->gamma_16_from_1, png_ptr->gamma_16_to_1,
 84.1394 +         png_ptr->gamma_shift
 84.1395 +#endif
 84.1396 +);
 84.1397 +#endif
 84.1398 +
 84.1399 +#if defined(PNG_READ_GAMMA_SUPPORTED)
 84.1400 +   if ((png_ptr->transformations & PNG_GAMMA) &&
 84.1401 +#if defined(PNG_READ_BACKGROUND_SUPPORTED)
 84.1402 +      !((png_ptr->transformations & PNG_BACKGROUND) &&
 84.1403 +      ((png_ptr->num_trans != 0) ||
 84.1404 +      (png_ptr->color_type & PNG_COLOR_MASK_ALPHA))) &&
 84.1405 +#endif
 84.1406 +      (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE))
 84.1407 +      png_do_gamma(&(png_ptr->row_info), png_ptr->row_buf + 1,
 84.1408 +         png_ptr->gamma_table, png_ptr->gamma_16_table,
 84.1409 +         png_ptr->gamma_shift);
 84.1410 +#endif
 84.1411 +
 84.1412 +#if defined(PNG_READ_16_TO_8_SUPPORTED)
 84.1413 +   if (png_ptr->transformations & PNG_16_TO_8)
 84.1414 +      png_do_chop(&(png_ptr->row_info), png_ptr->row_buf + 1);
 84.1415 +#endif
 84.1416 +
 84.1417 +#if defined(PNG_READ_DITHER_SUPPORTED)
 84.1418 +   if (png_ptr->transformations & PNG_DITHER)
 84.1419 +   {
 84.1420 +      png_do_dither((png_row_infop)&(png_ptr->row_info), png_ptr->row_buf + 1,
 84.1421 +         png_ptr->palette_lookup, png_ptr->dither_index);
 84.1422 +      if (png_ptr->row_info.rowbytes == (png_uint_32)0)
 84.1423 +         png_error(png_ptr, "png_do_dither returned rowbytes=0");
 84.1424 +   }
 84.1425 +#endif
 84.1426 +
 84.1427 +#if defined(PNG_READ_INVERT_SUPPORTED)
 84.1428 +   if (png_ptr->transformations & PNG_INVERT_MONO)
 84.1429 +      png_do_invert(&(png_ptr->row_info), png_ptr->row_buf + 1);
 84.1430 +#endif
 84.1431 +
 84.1432 +#if defined(PNG_READ_SHIFT_SUPPORTED)
 84.1433 +   if (png_ptr->transformations & PNG_SHIFT)
 84.1434 +      png_do_unshift(&(png_ptr->row_info), png_ptr->row_buf + 1,
 84.1435 +         &(png_ptr->shift));
 84.1436 +#endif
 84.1437 +
 84.1438 +#if defined(PNG_READ_PACK_SUPPORTED)
 84.1439 +   if (png_ptr->transformations & PNG_PACK)
 84.1440 +      png_do_unpack(&(png_ptr->row_info), png_ptr->row_buf + 1);
 84.1441 +#endif
 84.1442 +
 84.1443 +#if defined(PNG_READ_BGR_SUPPORTED)
 84.1444 +   if (png_ptr->transformations & PNG_BGR)
 84.1445 +      png_do_bgr(&(png_ptr->row_info), png_ptr->row_buf + 1);
 84.1446 +#endif
 84.1447 +
 84.1448 +#if defined(PNG_READ_PACKSWAP_SUPPORTED)
 84.1449 +   if (png_ptr->transformations & PNG_PACKSWAP)
 84.1450 +      png_do_packswap(&(png_ptr->row_info), png_ptr->row_buf + 1);
 84.1451 +#endif
 84.1452 +
 84.1453 +#if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED)
 84.1454 +   /* if gray -> RGB, do so now only if we did not do so above */
 84.1455 +   if ((png_ptr->transformations & PNG_GRAY_TO_RGB) &&
 84.1456 +       (png_ptr->mode & PNG_BACKGROUND_IS_GRAY))
 84.1457 +      png_do_gray_to_rgb(&(png_ptr->row_info), png_ptr->row_buf + 1);
 84.1458 +#endif
 84.1459 +
 84.1460 +#if defined(PNG_READ_FILLER_SUPPORTED)
 84.1461 +   if (png_ptr->transformations & PNG_FILLER)
 84.1462 +      png_do_read_filler(&(png_ptr->row_info), png_ptr->row_buf + 1,
 84.1463 +         (png_uint_32)png_ptr->filler, png_ptr->flags);
 84.1464 +#endif
 84.1465 +
 84.1466 +#if defined(PNG_READ_INVERT_ALPHA_SUPPORTED)
 84.1467 +   if (png_ptr->transformations & PNG_INVERT_ALPHA)
 84.1468 +      png_do_read_invert_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1);
 84.1469 +#endif
 84.1470 +
 84.1471 +#if defined(PNG_READ_SWAP_ALPHA_SUPPORTED)
 84.1472 +   if (png_ptr->transformations & PNG_SWAP_ALPHA)
 84.1473 +      png_do_read_swap_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1);
 84.1474 +#endif
 84.1475 +
 84.1476 +#if defined(PNG_READ_SWAP_SUPPORTED)
 84.1477 +   if (png_ptr->transformations & PNG_SWAP_BYTES)
 84.1478 +      png_do_swap(&(png_ptr->row_info), png_ptr->row_buf + 1);
 84.1479 +#endif
 84.1480 +
 84.1481 +#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
 84.1482 +   if (png_ptr->transformations & PNG_USER_TRANSFORM)
 84.1483 +    {
 84.1484 +      if (png_ptr->read_user_transform_fn != NULL)
 84.1485 +        (*(png_ptr->read_user_transform_fn)) /* user read transform function */
 84.1486 +          (png_ptr,                    /* png_ptr */
 84.1487 +           &(png_ptr->row_info),       /* row_info:     */
 84.1488 +             /*  png_uint_32 width;          width of row */
 84.1489 +             /*  png_uint_32 rowbytes;       number of bytes in row */
 84.1490 +             /*  png_byte color_type;        color type of pixels */
 84.1491 +             /*  png_byte bit_depth;         bit depth of samples */
 84.1492 +             /*  png_byte channels;          number of channels (1-4) */
 84.1493 +             /*  png_byte pixel_depth;       bits per pixel (depth*channels) */
 84.1494 +           png_ptr->row_buf + 1);      /* start of pixel data for row */
 84.1495 +#if defined(PNG_USER_TRANSFORM_PTR_SUPPORTED)
 84.1496 +      if (png_ptr->user_transform_depth)
 84.1497 +         png_ptr->row_info.bit_depth = png_ptr->user_transform_depth;
 84.1498 +      if (png_ptr->user_transform_channels)
 84.1499 +         png_ptr->row_info.channels = png_ptr->user_transform_channels;
 84.1500 +#endif
 84.1501 +      png_ptr->row_info.pixel_depth = (png_byte)(png_ptr->row_info.bit_depth *
 84.1502 +         png_ptr->row_info.channels);
 84.1503 +      png_ptr->row_info.rowbytes = PNG_ROWBYTES(png_ptr->row_info.pixel_depth,
 84.1504 +         png_ptr->row_info.width);
 84.1505 +   }
 84.1506 +#endif
 84.1507 +
 84.1508 +}
 84.1509 +
 84.1510 +#if defined(PNG_READ_PACK_SUPPORTED)
 84.1511 +/* Unpack pixels of 1, 2, or 4 bits per pixel into 1 byte per pixel,
 84.1512 + * without changing the actual values.  Thus, if you had a row with
 84.1513 + * a bit depth of 1, you would end up with bytes that only contained
 84.1514 + * the numbers 0 or 1.  If you would rather they contain 0 and 255, use
 84.1515 + * png_do_shift() after this.
 84.1516 + */
 84.1517 +void /* PRIVATE */
 84.1518 +png_do_unpack(png_row_infop row_info, png_bytep row)
 84.1519 +{
 84.1520 +   png_debug(1, "in png_do_unpack\n");
 84.1521 +#if defined(PNG_USELESS_TESTS_SUPPORTED)
 84.1522 +   if (row != NULL && row_info != NULL && row_info->bit_depth < 8)
 84.1523 +#else
 84.1524 +   if (row_info->bit_depth < 8)
 84.1525 +#endif
 84.1526 +   {
 84.1527 +      png_uint_32 i;
 84.1528 +      png_uint_32 row_width=row_info->width;
 84.1529 +
 84.1530 +      switch (row_info->bit_depth)
 84.1531 +      {
 84.1532 +         case 1:
 84.1533 +         {
 84.1534 +            png_bytep sp = row + (png_size_t)((row_width - 1) >> 3);
 84.1535 +            png_bytep dp = row + (png_size_t)row_width - 1;
 84.1536 +            png_uint_32 shift = 7 - (int)((row_width + 7) & 0x07);
 84.1537 +            for (i = 0; i < row_width; i++)
 84.1538 +            {
 84.1539 +               *dp = (png_byte)((*sp >> shift) & 0x01);
 84.1540 +               if (shift == 7)
 84.1541 +               {
 84.1542 +                  shift = 0;
 84.1543 +                  sp--;
 84.1544 +               }
 84.1545 +               else
 84.1546 +                  shift++;
 84.1547 +
 84.1548 +               dp--;
 84.1549 +            }
 84.1550 +            break;
 84.1551 +         }
 84.1552 +         case 2:
 84.1553 +         {
 84.1554 +
 84.1555 +            png_bytep sp = row + (png_size_t)((row_width - 1) >> 2);
 84.1556 +            png_bytep dp = row + (png_size_t)row_width - 1;
 84.1557 +            png_uint_32 shift = (int)((3 - ((row_width + 3) & 0x03)) << 1);
 84.1558 +            for (i = 0; i < row_width; i++)
 84.1559 +            {
 84.1560 +               *dp = (png_byte)((*sp >> shift) & 0x03);
 84.1561 +               if (shift == 6)
 84.1562 +               {
 84.1563 +                  shift = 0;
 84.1564 +                  sp--;
 84.1565 +               }
 84.1566 +               else
 84.1567 +                  shift += 2;
 84.1568 +
 84.1569 +               dp--;
 84.1570 +            }
 84.1571 +            break;
 84.1572 +         }
 84.1573 +         case 4:
 84.1574 +         {
 84.1575 +            png_bytep sp = row + (png_size_t)((row_width - 1) >> 1);
 84.1576 +            png_bytep dp = row + (png_size_t)row_width - 1;
 84.1577 +            png_uint_32 shift = (int)((1 - ((row_width + 1) & 0x01)) << 2);
 84.1578 +            for (i = 0; i < row_width; i++)
 84.1579 +            {
 84.1580 +               *dp = (png_byte)((*sp >> shift) & 0x0f);
 84.1581 +               if (shift == 4)
 84.1582 +               {
 84.1583 +                  shift = 0;
 84.1584 +                  sp--;
 84.1585 +               }
 84.1586 +               else
 84.1587 +                  shift = 4;
 84.1588 +
 84.1589 +               dp--;
 84.1590 +            }
 84.1591 +            break;
 84.1592 +         }
 84.1593 +      }
 84.1594 +      row_info->bit_depth = 8;
 84.1595 +      row_info->pixel_depth = (png_byte)(8 * row_info->channels);
 84.1596 +      row_info->rowbytes = row_width * row_info->channels;
 84.1597 +   }
 84.1598 +}
 84.1599 +#endif
 84.1600 +
 84.1601 +#if defined(PNG_READ_SHIFT_SUPPORTED)
 84.1602 +/* Reverse the effects of png_do_shift.  This routine merely shifts the
 84.1603 + * pixels back to their significant bits values.  Thus, if you have
 84.1604 + * a row of bit depth 8, but only 5 are significant, this will shift
 84.1605 + * the values back to 0 through 31.
 84.1606 + */
 84.1607 +void /* PRIVATE */
 84.1608 +png_do_unshift(png_row_infop row_info, png_bytep row, png_color_8p sig_bits)
 84.1609 +{
 84.1610 +   png_debug(1, "in png_do_unshift\n");
 84.1611 +   if (
 84.1612 +#if defined(PNG_USELESS_TESTS_SUPPORTED)
 84.1613 +       row != NULL && row_info != NULL && sig_bits != NULL &&
 84.1614 +#endif
 84.1615 +       row_info->color_type != PNG_COLOR_TYPE_PALETTE)
 84.1616 +   {
 84.1617 +      int shift[4];
 84.1618 +      int channels = 0;
 84.1619 +      int c;
 84.1620 +      png_uint_16 value = 0;
 84.1621 +      png_uint_32 row_width = row_info->width;
 84.1622 +
 84.1623 +      if (row_info->color_type & PNG_COLOR_MASK_COLOR)
 84.1624 +      {
 84.1625 +         shift[channels++] = row_info->bit_depth - sig_bits->red;
 84.1626 +         shift[channels++] = row_info->bit_depth - sig_bits->green;
 84.1627 +         shift[channels++] = row_info->bit_depth - sig_bits->blue;
 84.1628 +      }
 84.1629 +      else
 84.1630 +      {
 84.1631 +         shift[channels++] = row_info->bit_depth - sig_bits->gray;
 84.1632 +      }
 84.1633 +      if (row_info->color_type & PNG_COLOR_MASK_ALPHA)
 84.1634 +      {
 84.1635 +         shift[channels++] = row_info->bit_depth - sig_bits->alpha;
 84.1636 +      }
 84.1637 +
 84.1638 +      for (c = 0; c < channels; c++)
 84.1639 +      {
 84.1640 +         if (shift[c] <= 0)
 84.1641 +            shift[c] = 0;
 84.1642 +         else
 84.1643 +            value = 1;
 84.1644 +      }
 84.1645 +
 84.1646 +      if (!value)
 84.1647 +         return;
 84.1648 +
 84.1649 +      switch (row_info->bit_depth)
 84.1650 +      {
 84.1651 +         case 2:
 84.1652 +         {
 84.1653 +            png_bytep bp;
 84.1654 +            png_uint_32 i;
 84.1655 +            png_uint_32 istop = row_info->rowbytes;
 84.1656 +
 84.1657 +            for (bp = row, i = 0; i < istop; i++)
 84.1658 +            {
 84.1659 +               *bp >>= 1;
 84.1660 +               *bp++ &= 0x55;
 84.1661 +            }
 84.1662 +            break;
 84.1663 +         }
 84.1664 +         case 4:
 84.1665 +         {
 84.1666 +            png_bytep bp = row;
 84.1667 +            png_uint_32 i;
 84.1668 +            png_uint_32 istop = row_info->rowbytes;
 84.1669 +            png_byte mask = (png_byte)((((int)0xf0 >> shift[0]) & (int)0xf0) |
 84.1670 +               (png_byte)((int)0xf >> shift[0]));
 84.1671 +
 84.1672 +            for (i = 0; i < istop; i++)
 84.1673 +            {
 84.1674 +               *bp >>= shift[0];
 84.1675 +               *bp++ &= mask;
 84.1676 +            }
 84.1677 +            break;
 84.1678 +         }
 84.1679 +         case 8:
 84.1680 +         {
 84.1681 +            png_bytep bp = row;
 84.1682 +            png_uint_32 i;
 84.1683 +            png_uint_32 istop = row_width * channels;
 84.1684 +
 84.1685 +            for (i = 0; i < istop; i++)
 84.1686 +            {
 84.1687 +               *bp++ >>= shift[i%channels];
 84.1688 +            }
 84.1689 +            break;
 84.1690 +         }
 84.1691 +         case 16:
 84.1692 +         {
 84.1693 +            png_bytep bp = row;
 84.1694 +            png_uint_32 i;
 84.1695 +            png_uint_32 istop = channels * row_width;
 84.1696 +
 84.1697 +            for (i = 0; i < istop; i++)
 84.1698 +            {
 84.1699 +               value = (png_uint_16)((*bp << 8) + *(bp + 1));
 84.1700 +               value >>= shift[i%channels];
 84.1701 +               *bp++ = (png_byte)(value >> 8);
 84.1702 +               *bp++ = (png_byte)(value & 0xff);
 84.1703 +            }
 84.1704 +            break;
 84.1705 +         }
 84.1706 +      }
 84.1707 +   }
 84.1708 +}
 84.1709 +#endif
 84.1710 +
 84.1711 +#if defined(PNG_READ_16_TO_8_SUPPORTED)
 84.1712 +/* chop rows of bit depth 16 down to 8 */
 84.1713 +void /* PRIVATE */
 84.1714 +png_do_chop(png_row_infop row_info, png_bytep row)
 84.1715 +{
 84.1716 +   png_debug(1, "in png_do_chop\n");
 84.1717 +#if defined(PNG_USELESS_TESTS_SUPPORTED)
 84.1718 +   if (row != NULL && row_info != NULL && row_info->bit_depth == 16)
 84.1719 +#else
 84.1720 +   if (row_info->bit_depth == 16)
 84.1721 +#endif
 84.1722 +   {
 84.1723 +      png_bytep sp = row;
 84.1724 +      png_bytep dp = row;
 84.1725 +      png_uint_32 i;
 84.1726 +      png_uint_32 istop = row_info->width * row_info->channels;
 84.1727 +
 84.1728 +      for (i = 0; i<istop; i++, sp += 2, dp++)
 84.1729 +      {
 84.1730 +#if defined(PNG_READ_16_TO_8_ACCURATE_SCALE_SUPPORTED)
 84.1731 +      /* This does a more accurate scaling of the 16-bit color
 84.1732 +       * value, rather than a simple low-byte truncation.
 84.1733 +       *
 84.1734 +       * What the ideal calculation should be:
 84.1735 +       *   *dp = (((((png_uint_32)(*sp) << 8) |
 84.1736 +       *          (png_uint_32)(*(sp + 1))) * 255 + 127) / (png_uint_32)65535L;
 84.1737 +       *
 84.1738 +       * GRR: no, I think this is what it really should be:
 84.1739 +       *   *dp = (((((png_uint_32)(*sp) << 8) |
 84.1740 +       *           (png_uint_32)(*(sp + 1))) + 128L) / (png_uint_32)257L;
 84.1741 +       *
 84.1742 +       * GRR: here's the exact calculation with shifts:
 84.1743 +       *   temp = (((png_uint_32)(*sp) << 8) | (png_uint_32)(*(sp + 1))) + 128L;
 84.1744 +       *   *dp = (temp - (temp >> 8)) >> 8;
 84.1745 +       *
 84.1746 +       * Approximate calculation with shift/add instead of multiply/divide:
 84.1747 +       *   *dp = ((((png_uint_32)(*sp) << 8) |
 84.1748 +       *          (png_uint_32)((int)(*(sp + 1)) - *sp)) + 128) >> 8;
 84.1749 +       *
 84.1750 +       * What we actually do to avoid extra shifting and conversion:
 84.1751 +       */
 84.1752 +
 84.1753 +         *dp = *sp + ((((int)(*(sp + 1)) - *sp) > 128) ? 1 : 0);
 84.1754 +#else
 84.1755 +       /* Simply discard the low order byte */
 84.1756 +         *dp = *sp;
 84.1757 +#endif
 84.1758 +      }
 84.1759 +      row_info->bit_depth = 8;
 84.1760 +      row_info->pixel_depth = (png_byte)(8 * row_info->channels);
 84.1761 +      row_info->rowbytes = row_info->width * row_info->channels;
 84.1762 +   }
 84.1763 +}
 84.1764 +#endif
 84.1765 +
 84.1766 +#if defined(PNG_READ_SWAP_ALPHA_SUPPORTED)
 84.1767 +void /* PRIVATE */
 84.1768 +png_do_read_swap_alpha(png_row_infop row_info, png_bytep row)
 84.1769 +{
 84.1770 +   png_debug(1, "in png_do_read_swap_alpha\n");
 84.1771 +#if defined(PNG_USELESS_TESTS_SUPPORTED)
 84.1772 +   if (row != NULL && row_info != NULL)
 84.1773 +#endif
 84.1774 +   {
 84.1775 +      png_uint_32 row_width = row_info->width;
 84.1776 +      if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
 84.1777 +      {
 84.1778 +         /* This converts from RGBA to ARGB */
 84.1779 +         if (row_info->bit_depth == 8)
 84.1780 +         {
 84.1781 +            png_bytep sp = row + row_info->rowbytes;
 84.1782 +            png_bytep dp = sp;
 84.1783 +            png_byte save;
 84.1784 +            png_uint_32 i;
 84.1785 +
 84.1786 +            for (i = 0; i < row_width; i++)
 84.1787 +            {
 84.1788 +               save = *(--sp);
 84.1789 +               *(--dp) = *(--sp);
 84.1790 +               *(--dp) = *(--sp);
 84.1791 +               *(--dp) = *(--sp);
 84.1792 +               *(--dp) = save;
 84.1793 +            }
 84.1794 +         }
 84.1795 +         /* This converts from RRGGBBAA to AARRGGBB */
 84.1796 +         else
 84.1797 +         {
 84.1798 +            png_bytep sp = row + row_info->rowbytes;
 84.1799 +            png_bytep dp = sp;
 84.1800 +            png_byte save[2];
 84.1801 +            png_uint_32 i;
 84.1802 +
 84.1803 +            for (i = 0; i < row_width; i++)
 84.1804 +            {
 84.1805 +               save[0] = *(--sp);
 84.1806 +               save[1] = *(--sp);
 84.1807 +               *(--dp) = *(--sp);
 84.1808 +               *(--dp) = *(--sp);
 84.1809 +               *(--dp) = *(--sp);
 84.1810 +               *(--dp) = *(--sp);
 84.1811 +               *(--dp) = *(--sp);
 84.1812 +               *(--dp) = *(--sp);
 84.1813 +               *(--dp) = save[0];
 84.1814 +               *(--dp) = save[1];
 84.1815 +            }
 84.1816 +         }
 84.1817 +      }
 84.1818 +      else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
 84.1819 +      {
 84.1820 +         /* This converts from GA to AG */
 84.1821 +         if (row_info->bit_depth == 8)
 84.1822 +         {
 84.1823 +            png_bytep sp = row + row_info->rowbytes;
 84.1824 +            png_bytep dp = sp;
 84.1825 +            png_byte save;
 84.1826 +            png_uint_32 i;
 84.1827 +
 84.1828 +            for (i = 0; i < row_width; i++)
 84.1829 +            {
 84.1830 +               save = *(--sp);
 84.1831 +               *(--dp) = *(--sp);
 84.1832 +               *(--dp) = save;
 84.1833 +            }
 84.1834 +         }
 84.1835 +         /* This converts from GGAA to AAGG */
 84.1836 +         else
 84.1837 +         {
 84.1838 +            png_bytep sp = row + row_info->rowbytes;
 84.1839 +            png_bytep dp = sp;
 84.1840 +            png_byte save[2];
 84.1841 +            png_uint_32 i;
 84.1842 +
 84.1843 +            for (i = 0; i < row_width; i++)
 84.1844 +            {
 84.1845 +               save[0] = *(--sp);
 84.1846 +               save[1] = *(--sp);
 84.1847 +               *(--dp) = *(--sp);
 84.1848 +               *(--dp) = *(--sp);
 84.1849 +               *(--dp) = save[0];
 84.1850 +               *(--dp) = save[1];
 84.1851 +            }
 84.1852 +         }
 84.1853 +      }
 84.1854 +   }
 84.1855 +}
 84.1856 +#endif
 84.1857 +
 84.1858 +#if defined(PNG_READ_INVERT_ALPHA_SUPPORTED)
 84.1859 +void /* PRIVATE */
 84.1860 +png_do_read_invert_alpha(png_row_infop row_info, png_bytep row)
 84.1861 +{
 84.1862 +   png_debug(1, "in png_do_read_invert_alpha\n");
 84.1863 +#if defined(PNG_USELESS_TESTS_SUPPORTED)
 84.1864 +   if (row != NULL && row_info != NULL)
 84.1865 +#endif
 84.1866 +   {
 84.1867 +      png_uint_32 row_width = row_info->width;
 84.1868 +      if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
 84.1869 +      {
 84.1870 +         /* This inverts the alpha channel in RGBA */
 84.1871 +         if (row_info->bit_depth == 8)
 84.1872 +         {
 84.1873 +            png_bytep sp = row + row_info->rowbytes;
 84.1874 +            png_bytep dp = sp;
 84.1875 +            png_uint_32 i;
 84.1876 +
 84.1877 +            for (i = 0; i < row_width; i++)
 84.1878 +            {
 84.1879 +               *(--dp) = (png_byte)(255 - *(--sp));
 84.1880 +
 84.1881 +/*             This does nothing:
 84.1882 +               *(--dp) = *(--sp);
 84.1883 +               *(--dp) = *(--sp);
 84.1884 +               *(--dp) = *(--sp);
 84.1885 +               We can replace it with:
 84.1886 +*/
 84.1887 +               sp-=3;
 84.1888 +               dp=sp;
 84.1889 +            }
 84.1890 +         }
 84.1891 +         /* This inverts the alpha channel in RRGGBBAA */
 84.1892 +         else
 84.1893 +         {
 84.1894 +            png_bytep sp = row + row_info->rowbytes;
 84.1895 +            png_bytep dp = sp;
 84.1896 +            png_uint_32 i;
 84.1897 +
 84.1898 +            for (i = 0; i < row_width; i++)
 84.1899 +            {
 84.1900 +               *(--dp) = (png_byte)(255 - *(--sp));
 84.1901 +               *(--dp) = (png_byte)(255 - *(--sp));
 84.1902 +
 84.1903 +/*             This does nothing:
 84.1904 +               *(--dp) = *(--sp);
 84.1905 +               *(--dp) = *(--sp);
 84.1906 +               *(--dp) = *(--sp);
 84.1907 +               *(--dp) = *(--sp);
 84.1908 +               *(--dp) = *(--sp);
 84.1909 +               *(--dp) = *(--sp);
 84.1910 +               We can replace it with:
 84.1911 +*/
 84.1912 +               sp-=6;
 84.1913 +               dp=sp;
 84.1914 +            }
 84.1915 +         }
 84.1916 +      }
 84.1917 +      else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
 84.1918 +      {
 84.1919 +         /* This inverts the alpha channel in GA */
 84.1920 +         if (row_info->bit_depth == 8)
 84.1921 +         {
 84.1922 +            png_bytep sp = row + row_info->rowbytes;
 84.1923 +            png_bytep dp = sp;
 84.1924 +            png_uint_32 i;
 84.1925 +
 84.1926 +            for (i = 0; i < row_width; i++)
 84.1927 +            {
 84.1928 +               *(--dp) = (png_byte)(255 - *(--sp));
 84.1929 +               *(--dp) = *(--sp);
 84.1930 +            }
 84.1931 +         }
 84.1932 +         /* This inverts the alpha channel in GGAA */
 84.1933 +         else
 84.1934 +         {
 84.1935 +            png_bytep sp  = row + row_info->rowbytes;
 84.1936 +            png_bytep dp = sp;
 84.1937 +            png_uint_32 i;
 84.1938 +
 84.1939 +            for (i = 0; i < row_width; i++)
 84.1940 +            {
 84.1941 +               *(--dp) = (png_byte)(255 - *(--sp));
 84.1942 +               *(--dp) = (png_byte)(255 - *(--sp));
 84.1943 +/*
 84.1944 +               *(--dp) = *(--sp);
 84.1945 +               *(--dp) = *(--sp);
 84.1946 +*/
 84.1947 +               sp-=2;
 84.1948 +               dp=sp;
 84.1949 +            }
 84.1950 +         }
 84.1951 +      }
 84.1952 +   }
 84.1953 +}
 84.1954 +#endif
 84.1955 +
 84.1956 +#if defined(PNG_READ_FILLER_SUPPORTED)
 84.1957 +/* Add filler channel if we have RGB color */
 84.1958 +void /* PRIVATE */
 84.1959 +png_do_read_filler(png_row_infop row_info, png_bytep row,
 84.1960 +   png_uint_32 filler, png_uint_32 flags)
 84.1961 +{
 84.1962 +   png_uint_32 i;
 84.1963 +   png_uint_32 row_width = row_info->width;
 84.1964 +
 84.1965 +   png_byte hi_filler = (png_byte)((filler>>8) & 0xff);
 84.1966 +   png_byte lo_filler = (png_byte)(filler & 0xff);
 84.1967 +
 84.1968 +   png_debug(1, "in png_do_read_filler\n");
 84.1969 +   if (
 84.1970 +#if defined(PNG_USELESS_TESTS_SUPPORTED)
 84.1971 +       row != NULL  && row_info != NULL &&
 84.1972 +#endif
 84.1973 +       row_info->color_type == PNG_COLOR_TYPE_GRAY)
 84.1974 +   {
 84.1975 +      if (row_info->bit_depth == 8)
 84.1976 +      {
 84.1977 +         /* This changes the data from G to GX */
 84.1978 +         if (flags & PNG_FLAG_FILLER_AFTER)
 84.1979 +         {
 84.1980 +            png_bytep sp = row + (png_size_t)row_width;
 84.1981 +            png_bytep dp =  sp + (png_size_t)row_width;
 84.1982 +            for (i = 1; i < row_width; i++)
 84.1983 +            {
 84.1984 +               *(--dp) = lo_filler;
 84.1985 +               *(--dp) = *(--sp);
 84.1986 +            }
 84.1987 +            *(--dp) = lo_filler;
 84.1988 +            row_info->channels = 2;
 84.1989 +            row_info->pixel_depth = 16;
 84.1990 +            row_info->rowbytes = row_width * 2;
 84.1991 +         }
 84.1992 +      /* This changes the data from G to XG */
 84.1993 +         else
 84.1994 +         {
 84.1995 +            png_bytep sp = row + (png_size_t)row_width;
 84.1996 +            png_bytep dp = sp  + (png_size_t)row_width;
 84.1997 +            for (i = 0; i < row_width; i++)
 84.1998 +            {
 84.1999 +               *(--dp) = *(--sp);
 84.2000 +               *(--dp) = lo_filler;
 84.2001 +            }
 84.2002 +            row_info->channels = 2;
 84.2003 +            row_info->pixel_depth = 16;
 84.2004 +            row_info->rowbytes = row_width * 2;
 84.2005 +         }
 84.2006 +      }
 84.2007 +      else if (row_info->bit_depth == 16)
 84.2008 +      {
 84.2009 +         /* This changes the data from GG to GGXX */
 84.2010 +         if (flags & PNG_FLAG_FILLER_AFTER)
 84.2011 +         {
 84.2012 +            png_bytep sp = row + (png_size_t)row_width * 2;
 84.2013 +            png_bytep dp = sp  + (png_size_t)row_width * 2;
 84.2014 +            for (i = 1; i < row_width; i++)
 84.2015 +            {
 84.2016 +               *(--dp) = hi_filler;
 84.2017 +               *(--dp) = lo_filler;
 84.2018 +               *(--dp) = *(--sp);
 84.2019 +               *(--dp) = *(--sp);
 84.2020 +            }
 84.2021 +            *(--dp) = hi_filler;
 84.2022 +            *(--dp) = lo_filler;
 84.2023 +            row_info->channels = 2;
 84.2024 +            row_info->pixel_depth = 32;
 84.2025 +            row_info->rowbytes = row_width * 4;
 84.2026 +         }
 84.2027 +         /* This changes the data from GG to XXGG */
 84.2028 +         else
 84.2029 +         {
 84.2030 +            png_bytep sp = row + (png_size_t)row_width * 2;
 84.2031 +            png_bytep dp = sp  + (png_size_t)row_width * 2;
 84.2032 +            for (i = 0; i < row_width; i++)
 84.2033 +            {
 84.2034 +               *(--dp) = *(--sp);
 84.2035 +               *(--dp) = *(--sp);
 84.2036 +               *(--dp) = hi_filler;
 84.2037 +               *(--dp) = lo_filler;
 84.2038 +            }
 84.2039 +            row_info->channels = 2;
 84.2040 +            row_info->pixel_depth = 32;
 84.2041 +            row_info->rowbytes = row_width * 4;
 84.2042 +         }
 84.2043 +      }
 84.2044 +   } /* COLOR_TYPE == GRAY */
 84.2045 +   else if (row_info->color_type == PNG_COLOR_TYPE_RGB)
 84.2046 +   {
 84.2047 +      if (row_info->bit_depth == 8)
 84.2048 +      {
 84.2049 +         /* This changes the data from RGB to RGBX */
 84.2050 +         if (flags & PNG_FLAG_FILLER_AFTER)
 84.2051 +         {
 84.2052 +            png_bytep sp = row + (png_size_t)row_width * 3;
 84.2053 +            png_bytep dp = sp  + (png_size_t)row_width;
 84.2054 +            for (i = 1; i < row_width; i++)
 84.2055 +            {
 84.2056 +               *(--dp) = lo_filler;
 84.2057 +               *(--dp) = *(--sp);
 84.2058 +               *(--dp) = *(--sp);
 84.2059 +               *(--dp) = *(--sp);
 84.2060 +            }
 84.2061 +            *(--dp) = lo_filler;
 84.2062 +            row_info->channels = 4;
 84.2063 +            row_info->pixel_depth = 32;
 84.2064 +            row_info->rowbytes = row_width * 4;
 84.2065 +         }
 84.2066 +      /* This changes the data from RGB to XRGB */
 84.2067 +         else
 84.2068 +         {
 84.2069 +            png_bytep sp = row + (png_size_t)row_width * 3;
 84.2070 +            png_bytep dp = sp + (png_size_t)row_width;
 84.2071 +            for (i = 0; i < row_width; i++)
 84.2072 +            {
 84.2073 +               *(--dp) = *(--sp);
 84.2074 +               *(--dp) = *(--sp);
 84.2075 +               *(--dp) = *(--sp);
 84.2076 +               *(--dp) = lo_filler;
 84.2077 +            }
 84.2078 +            row_info->channels = 4;
 84.2079 +            row_info->pixel_depth = 32;
 84.2080 +            row_info->rowbytes = row_width * 4;
 84.2081 +         }
 84.2082 +      }
 84.2083 +      else if (row_info->bit_depth == 16)
 84.2084 +      {
 84.2085 +         /* This changes the data from RRGGBB to RRGGBBXX */
 84.2086 +         if (flags & PNG_FLAG_FILLER_AFTER)
 84.2087 +         {
 84.2088 +            png_bytep sp = row + (png_size_t)row_width * 6;
 84.2089 +            png_bytep dp = sp  + (png_size_t)row_width * 2;
 84.2090 +            for (i = 1; i < row_width; i++)
 84.2091 +            {
 84.2092 +               *(--dp) = hi_filler;
 84.2093 +               *(--dp) = lo_filler;
 84.2094 +               *(--dp) = *(--sp);
 84.2095 +               *(--dp) = *(--sp);
 84.2096 +               *(--dp) = *(--sp);
 84.2097 +               *(--dp) = *(--sp);
 84.2098 +               *(--dp) = *(--sp);
 84.2099 +               *(--dp) = *(--sp);
 84.2100 +            }
 84.2101 +            *(--dp) = hi_filler;
 84.2102 +            *(--dp) = lo_filler;
 84.2103 +            row_info->channels = 4;
 84.2104 +            row_info->pixel_depth = 64;
 84.2105 +            row_info->rowbytes = row_width * 8;
 84.2106 +         }
 84.2107 +         /* This changes the data from RRGGBB to XXRRGGBB */
 84.2108 +         else
 84.2109 +         {
 84.2110 +            png_bytep sp = row + (png_size_t)row_width * 6;
 84.2111 +            png_bytep dp = sp  + (png_size_t)row_width * 2;
 84.2112 +            for (i = 0; i < row_width; i++)
 84.2113 +            {
 84.2114 +               *(--dp) = *(--sp);
 84.2115 +               *(--dp) = *(--sp);
 84.2116 +               *(--dp) = *(--sp);
 84.2117 +               *(--dp) = *(--sp);
 84.2118 +               *(--dp) = *(--sp);
 84.2119 +               *(--dp) = *(--sp);
 84.2120 +               *(--dp) = hi_filler;
 84.2121 +               *(--dp) = lo_filler;
 84.2122 +            }
 84.2123 +            row_info->channels = 4;
 84.2124 +            row_info->pixel_depth = 64;
 84.2125 +            row_info->rowbytes = row_width * 8;
 84.2126 +         }
 84.2127 +      }
 84.2128 +   } /* COLOR_TYPE == RGB */
 84.2129 +}
 84.2130 +#endif
 84.2131 +
 84.2132 +#if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED)
 84.2133 +/* expand grayscale files to RGB, with or without alpha */
 84.2134 +void /* PRIVATE */
 84.2135 +png_do_gray_to_rgb(png_row_infop row_info, png_bytep row)
 84.2136 +{
 84.2137 +   png_uint_32 i;
 84.2138 +   png_uint_32 row_width = row_info->width;
 84.2139 +
 84.2140 +   png_debug(1, "in png_do_gray_to_rgb\n");
 84.2141 +   if (row_info->bit_depth >= 8 &&
 84.2142 +#if defined(PNG_USELESS_TESTS_SUPPORTED)
 84.2143 +       row != NULL && row_info != NULL &&
 84.2144 +#endif
 84.2145 +      !(row_info->color_type & PNG_COLOR_MASK_COLOR))
 84.2146 +   {
 84.2147 +      if (row_info->color_type == PNG_COLOR_TYPE_GRAY)
 84.2148 +      {
 84.2149 +         if (row_info->bit_depth == 8)
 84.2150 +         {
 84.2151 +            png_bytep sp = row + (png_size_t)row_width - 1;
 84.2152 +            png_bytep dp = sp  + (png_size_t)row_width * 2;
 84.2153 +            for (i = 0; i < row_width; i++)
 84.2154 +            {
 84.2155 +               *(dp--) = *sp;
 84.2156 +               *(dp--) = *sp;
 84.2157 +               *(dp--) = *(sp--);
 84.2158 +            }
 84.2159 +         }
 84.2160 +         else
 84.2161 +         {
 84.2162 +            png_bytep sp = row + (png_size_t)row_width * 2 - 1;
 84.2163 +            png_bytep dp = sp  + (png_size_t)row_width * 4;
 84.2164 +            for (i = 0; i < row_width; i++)
 84.2165 +            {
 84.2166 +               *(dp--) = *sp;
 84.2167 +               *(dp--) = *(sp - 1);
 84.2168 +               *(dp--) = *sp;
 84.2169 +               *(dp--) = *(sp - 1);
 84.2170 +               *(dp--) = *(sp--);
 84.2171 +               *(dp--) = *(sp--);
 84.2172 +            }
 84.2173 +         }
 84.2174 +      }
 84.2175 +      else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
 84.2176 +      {
 84.2177 +         if (row_info->bit_depth == 8)
 84.2178 +         {
 84.2179 +            png_bytep sp = row + (png_size_t)row_width * 2 - 1;
 84.2180 +            png_bytep dp = sp  + (png_size_t)row_width * 2;
 84.2181 +            for (i = 0; i < row_width; i++)
 84.2182 +            {
 84.2183 +               *(dp--) = *(sp--);
 84.2184 +               *(dp--) = *sp;
 84.2185 +               *(dp--) = *sp;
 84.2186 +               *(dp--) = *(sp--);
 84.2187 +            }
 84.2188 +         }
 84.2189 +         else
 84.2190 +         {
 84.2191 +            png_bytep sp = row + (png_size_t)row_width * 4 - 1;
 84.2192 +            png_bytep dp = sp  + (png_size_t)row_width * 4;
 84.2193 +            for (i = 0; i < row_width; i++)
 84.2194 +            {
 84.2195 +               *(dp--) = *(sp--);
 84.2196 +               *(dp--) = *(sp--);
 84.2197 +               *(dp--) = *sp;
 84.2198 +               *(dp--) = *(sp - 1);
 84.2199 +               *(dp--) = *sp;
 84.2200 +               *(dp--) = *(sp - 1);
 84.2201 +               *(dp--) = *(sp--);
 84.2202 +               *(dp--) = *(sp--);
 84.2203 +            }
 84.2204 +         }
 84.2205 +      }
 84.2206 +      row_info->channels += (png_byte)2;
 84.2207 +      row_info->color_type |= PNG_COLOR_MASK_COLOR;
 84.2208 +      row_info->pixel_depth = (png_byte)(row_info->channels *
 84.2209 +         row_info->bit_depth);
 84.2210 +      row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
 84.2211 +   }
 84.2212 +}
 84.2213 +#endif
 84.2214 +
 84.2215 +#if defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
 84.2216 +/* reduce RGB files to grayscale, with or without alpha
 84.2217 + * using the equation given in Poynton's ColorFAQ at
 84.2218 + * <http://www.inforamp.net/~poynton/>  (THIS LINK IS DEAD June 2008)
 84.2219 + * New link:
 84.2220 + * <http://www.poynton.com/notes/colour_and_gamma/>
 84.2221 + * Charles Poynton poynton at poynton.com
 84.2222 + *
 84.2223 + *     Y = 0.212671 * R + 0.715160 * G + 0.072169 * B
 84.2224 + *
 84.2225 + *  We approximate this with
 84.2226 + *
 84.2227 + *     Y = 0.21268 * R    + 0.7151 * G    + 0.07217 * B
 84.2228 + *
 84.2229 + *  which can be expressed with integers as
 84.2230 + *
 84.2231 + *     Y = (6969 * R + 23434 * G + 2365 * B)/32768
 84.2232 + *
 84.2233 + *  The calculation is to be done in a linear colorspace.
 84.2234 + *
 84.2235 + *  Other integer coefficents can be used via png_set_rgb_to_gray().
 84.2236 + */
 84.2237 +int /* PRIVATE */
 84.2238 +png_do_rgb_to_gray(png_structp png_ptr, png_row_infop row_info, png_bytep row)
 84.2239 +
 84.2240 +{
 84.2241 +   png_uint_32 i;
 84.2242 +
 84.2243 +   png_uint_32 row_width = row_info->width;
 84.2244 +   int rgb_error = 0;
 84.2245 +
 84.2246 +   png_debug(1, "in png_do_rgb_to_gray\n");
 84.2247 +   if (
 84.2248 +#if defined(PNG_USELESS_TESTS_SUPPORTED)
 84.2249 +       row != NULL && row_info != NULL &&
 84.2250 +#endif
 84.2251 +      (row_info->color_type & PNG_COLOR_MASK_COLOR))
 84.2252 +   {
 84.2253 +      png_uint_32 rc = png_ptr->rgb_to_gray_red_coeff;
 84.2254 +      png_uint_32 gc = png_ptr->rgb_to_gray_green_coeff;
 84.2255 +      png_uint_32 bc = png_ptr->rgb_to_gray_blue_coeff;
 84.2256 +
 84.2257 +      if (row_info->color_type == PNG_COLOR_TYPE_RGB)
 84.2258 +      {
 84.2259 +         if (row_info->bit_depth == 8)
 84.2260 +         {
 84.2261 +#if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
 84.2262 +            if (png_ptr->gamma_from_1 != NULL && png_ptr->gamma_to_1 != NULL)
 84.2263 +            {
 84.2264 +               png_bytep sp = row;
 84.2265 +               png_bytep dp = row;
 84.2266 +
 84.2267 +               for (i = 0; i < row_width; i++)
 84.2268 +               {
 84.2269 +                  png_byte red   = png_ptr->gamma_to_1[*(sp++)];
 84.2270 +                  png_byte green = png_ptr->gamma_to_1[*(sp++)];
 84.2271 +                  png_byte blue  = png_ptr->gamma_to_1[*(sp++)];
 84.2272 +                  if (red != green || red != blue)
 84.2273 +                  {
 84.2274 +                     rgb_error |= 1;
 84.2275 +                     *(dp++) = png_ptr->gamma_from_1[
 84.2276 +                       (rc*red + gc*green + bc*blue)>>15];
 84.2277 +                  }
 84.2278 +                  else
 84.2279 +                     *(dp++) = *(sp - 1);
 84.2280 +               }
 84.2281 +            }
 84.2282 +            else
 84.2283 +#endif
 84.2284 +            {
 84.2285 +               png_bytep sp = row;
 84.2286 +               png_bytep dp = row;
 84.2287 +               for (i = 0; i < row_width; i++)
 84.2288 +               {
 84.2289 +                  png_byte red   = *(sp++);
 84.2290 +                  png_byte green = *(sp++);
 84.2291 +                  png_byte blue  = *(sp++);
 84.2292 +                  if (red != green || red != blue)
 84.2293 +                  {
 84.2294 +                     rgb_error |= 1;
 84.2295 +                     *(dp++) = (png_byte)((rc*red + gc*green + bc*blue)>>15);
 84.2296 +                  }
 84.2297 +                  else
 84.2298 +                     *(dp++) = *(sp - 1);
 84.2299 +               }
 84.2300 +            }
 84.2301 +         }
 84.2302 +
 84.2303 +         else /* RGB bit_depth == 16 */
 84.2304 +         {
 84.2305 +#if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
 84.2306 +            if (png_ptr->gamma_16_to_1 != NULL &&
 84.2307 +                png_ptr->gamma_16_from_1 != NULL)
 84.2308 +            {
 84.2309 +               png_bytep sp = row;
 84.2310 +               png_bytep dp = row;
 84.2311 +               for (i = 0; i < row_width; i++)
 84.2312 +               {
 84.2313 +                  png_uint_16 red, green, blue, w;
 84.2314 +
 84.2315 +                  red   = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2;
 84.2316 +                  green = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2;
 84.2317 +                  blue  = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2;
 84.2318 +
 84.2319 +                  if (red == green && red == blue)
 84.2320 +                     w = red;
 84.2321 +                  else
 84.2322 +                  {
 84.2323 +                     png_uint_16 red_1   = png_ptr->gamma_16_to_1[(red&0xff) >>
 84.2324 +                                  png_ptr->gamma_shift][red>>8];
 84.2325 +                     png_uint_16 green_1 = png_ptr->gamma_16_to_1[(green&0xff) >>
 84.2326 +                                  png_ptr->gamma_shift][green>>8];
 84.2327 +                     png_uint_16 blue_1  = png_ptr->gamma_16_to_1[(blue&0xff) >>
 84.2328 +                                  png_ptr->gamma_shift][blue>>8];
 84.2329 +                     png_uint_16 gray16  = (png_uint_16)((rc*red_1 + gc*green_1
 84.2330 +                                  + bc*blue_1)>>15);
 84.2331 +                     w = png_ptr->gamma_16_from_1[(gray16&0xff) >>
 84.2332 +                         png_ptr->gamma_shift][gray16 >> 8];
 84.2333 +                     rgb_error |= 1;
 84.2334 +                  }
 84.2335 +
 84.2336 +                  *(dp++) = (png_byte)((w>>8) & 0xff);
 84.2337 +                  *(dp++) = (png_byte)(w & 0xff);
 84.2338 +               }
 84.2339 +            }
 84.2340 +            else
 84.2341 +#endif
 84.2342 +            {
 84.2343 +               png_bytep sp = row;
 84.2344 +               png_bytep dp = row;
 84.2345 +               for (i = 0; i < row_width; i++)
 84.2346 +               {
 84.2347 +                  png_uint_16 red, green, blue, gray16;
 84.2348 +
 84.2349 +                  red   = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2;
 84.2350 +                  green = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2;
 84.2351 +                  blue  = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2;
 84.2352 +
 84.2353 +                  if (red != green || red != blue)
 84.2354 +                     rgb_error |= 1;
 84.2355 +                  gray16  = (png_uint_16)((rc*red + gc*green + bc*blue)>>15);
 84.2356 +                  *(dp++) = (png_byte)((gray16>>8) & 0xff);
 84.2357 +                  *(dp++) = (png_byte)(gray16 & 0xff);
 84.2358 +               }
 84.2359 +            }
 84.2360 +         }
 84.2361 +      }
 84.2362 +      if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
 84.2363 +      {
 84.2364 +         if (row_info->bit_depth == 8)
 84.2365 +         {
 84.2366 +#if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
 84.2367 +            if (png_ptr->gamma_from_1 != NULL && png_ptr->gamma_to_1 != NULL)
 84.2368 +            {
 84.2369 +               png_bytep sp = row;
 84.2370 +               png_bytep dp = row;
 84.2371 +               for (i = 0; i < row_width; i++)
 84.2372 +               {
 84.2373 +                  png_byte red   = png_ptr->gamma_to_1[*(sp++)];
 84.2374 +                  png_byte green = png_ptr->gamma_to_1[*(sp++)];
 84.2375 +                  png_byte blue  = png_ptr->gamma_to_1[*(sp++)];
 84.2376 +                  if (red != green || red != blue)
 84.2377 +                     rgb_error |= 1;
 84.2378 +                  *(dp++) =  png_ptr->gamma_from_1
 84.2379 +                             [(rc*red + gc*green + bc*blue)>>15];
 84.2380 +                  *(dp++) = *(sp++);  /* alpha */
 84.2381 +               }
 84.2382 +            }
 84.2383 +            else
 84.2384 +#endif
 84.2385 +            {
 84.2386 +               png_bytep sp = row;
 84.2387 +               png_bytep dp = row;
 84.2388 +               for (i = 0; i < row_width; i++)
 84.2389 +               {
 84.2390 +                  png_byte red   = *(sp++);
 84.2391 +                  png_byte green = *(sp++);
 84.2392 +                  png_byte blue  = *(sp++);
 84.2393 +                  if (red != green || red != blue)
 84.2394 +                     rgb_error |= 1;
 84.2395 +                  *(dp++) =  (png_byte)((rc*red + gc*green + bc*blue)>>15);
 84.2396 +                  *(dp++) = *(sp++);  /* alpha */
 84.2397 +               }
 84.2398 +            }
 84.2399 +         }
 84.2400 +         else /* RGBA bit_depth == 16 */
 84.2401 +         {
 84.2402 +#if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
 84.2403 +            if (png_ptr->gamma_16_to_1 != NULL &&
 84.2404 +                png_ptr->gamma_16_from_1 != NULL)
 84.2405 +            {
 84.2406 +               png_bytep sp = row;
 84.2407 +               png_bytep dp = row;
 84.2408 +               for (i = 0; i < row_width; i++)
 84.2409 +               {
 84.2410 +                  png_uint_16 red, green, blue, w;
 84.2411 +
 84.2412 +                  red   = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2;
 84.2413 +                  green = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2;
 84.2414 +                  blue  = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2;
 84.2415 +
 84.2416 +                  if (red == green && red == blue)
 84.2417 +                     w = red;
 84.2418 +                  else
 84.2419 +                  {
 84.2420 +                     png_uint_16 red_1   = png_ptr->gamma_16_to_1[(red&0xff) >>
 84.2421 +                                  png_ptr->gamma_shift][red>>8];
 84.2422 +                     png_uint_16 green_1 = png_ptr->gamma_16_to_1[(green&0xff) >>
 84.2423 +                                  png_ptr->gamma_shift][green>>8];
 84.2424 +                     png_uint_16 blue_1  = png_ptr->gamma_16_to_1[(blue&0xff) >>
 84.2425 +                                  png_ptr->gamma_shift][blue>>8];
 84.2426 +                     png_uint_16 gray16  = (png_uint_16)((rc * red_1
 84.2427 +                                  + gc * green_1 + bc * blue_1)>>15);
 84.2428 +                     w = png_ptr->gamma_16_from_1[(gray16&0xff) >>
 84.2429 +                         png_ptr->gamma_shift][gray16 >> 8];
 84.2430 +                     rgb_error |= 1;
 84.2431 +                  }
 84.2432 +
 84.2433 +                  *(dp++) = (png_byte)((w>>8) & 0xff);
 84.2434 +                  *(dp++) = (png_byte)(w & 0xff);
 84.2435 +                  *(dp++) = *(sp++);  /* alpha */
 84.2436 +                  *(dp++) = *(sp++);
 84.2437 +               }
 84.2438 +            }
 84.2439 +            else
 84.2440 +#endif
 84.2441 +            {
 84.2442 +               png_bytep sp = row;
 84.2443 +               png_bytep dp = row;
 84.2444 +               for (i = 0; i < row_width; i++)
 84.2445 +               {
 84.2446 +                  png_uint_16 red, green, blue, gray16;
 84.2447 +                  red   = (png_uint_16)((*(sp)<<8) | *(sp+1)); sp+=2;
 84.2448 +                  green = (png_uint_16)((*(sp)<<8) | *(sp+1)); sp+=2;
 84.2449 +                  blue  = (png_uint_16)((*(sp)<<8) | *(sp+1)); sp+=2;
 84.2450 +                  if (red != green || red != blue)
 84.2451 +                     rgb_error |= 1;
 84.2452 +                  gray16  = (png_uint_16)((rc*red + gc*green + bc*blue)>>15);
 84.2453 +                  *(dp++) = (png_byte)((gray16>>8) & 0xff);
 84.2454 +                  *(dp++) = (png_byte)(gray16 & 0xff);
 84.2455 +                  *(dp++) = *(sp++);  /* alpha */
 84.2456 +                  *(dp++) = *(sp++);
 84.2457 +               }
 84.2458 +            }
 84.2459 +         }
 84.2460 +      }
 84.2461 +   row_info->channels -= (png_byte)2;
 84.2462 +      row_info->color_type &= ~PNG_COLOR_MASK_COLOR;
 84.2463 +      row_info->pixel_depth = (png_byte)(row_info->channels *
 84.2464 +         row_info->bit_depth);
 84.2465 +      row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
 84.2466 +   }
 84.2467 +   return rgb_error;
 84.2468 +}
 84.2469 +#endif
 84.2470 +
 84.2471 +/* Build a grayscale palette.  Palette is assumed to be 1 << bit_depth
 84.2472 + * large of png_color.  This lets grayscale images be treated as
 84.2473 + * paletted.  Most useful for gamma correction and simplification
 84.2474 + * of code.
 84.2475 + */
 84.2476 +void PNGAPI
 84.2477 +png_build_grayscale_palette(int bit_depth, png_colorp palette)
 84.2478 +{
 84.2479 +   int num_palette;
 84.2480 +   int color_inc;
 84.2481 +   int i;
 84.2482 +   int v;
 84.2483 +
 84.2484 +   png_debug(1, "in png_do_build_grayscale_palette\n");
 84.2485 +   if (palette == NULL)
 84.2486 +      return;
 84.2487 +
 84.2488 +   switch (bit_depth)
 84.2489 +   {
 84.2490 +      case 1:
 84.2491 +         num_palette = 2;
 84.2492 +         color_inc = 0xff;
 84.2493 +         break;
 84.2494 +      case 2:
 84.2495 +         num_palette = 4;
 84.2496 +         color_inc = 0x55;
 84.2497 +         break;
 84.2498 +      case 4:
 84.2499 +         num_palette = 16;
 84.2500 +         color_inc = 0x11;
 84.2501 +         break;
 84.2502 +      case 8:
 84.2503 +         num_palette = 256;
 84.2504 +         color_inc = 1;
 84.2505 +         break;
 84.2506 +      default:
 84.2507 +         num_palette = 0;
 84.2508 +         color_inc = 0;
 84.2509 +         break;
 84.2510 +   }
 84.2511 +
 84.2512 +   for (i = 0, v = 0; i < num_palette; i++, v += color_inc)
 84.2513 +   {
 84.2514 +      palette[i].red = (png_byte)v;
 84.2515 +      palette[i].green = (png_byte)v;
 84.2516 +      palette[i].blue = (png_byte)v;
 84.2517 +   }
 84.2518 +}
 84.2519 +
 84.2520 +/* This function is currently unused.  Do we really need it? */
 84.2521 +#if defined(PNG_READ_DITHER_SUPPORTED) && defined(PNG_CORRECT_PALETTE_SUPPORTED)
 84.2522 +void /* PRIVATE */
 84.2523 +png_correct_palette(png_structp png_ptr, png_colorp palette,
 84.2524 +   int num_palette)
 84.2525 +{
 84.2526 +   png_debug(1, "in png_correct_palette\n");
 84.2527 +#if defined(PNG_READ_BACKGROUND_SUPPORTED) && \
 84.2528 +    defined(PNG_READ_GAMMA_SUPPORTED) && defined(PNG_FLOATING_POINT_SUPPORTED)
 84.2529 +   if (png_ptr->transformations & (PNG_GAMMA | PNG_BACKGROUND))
 84.2530 +   {
 84.2531 +      png_color back, back_1;
 84.2532 +
 84.2533 +      if (png_ptr->background_gamma_type == PNG_BACKGROUND_GAMMA_FILE)
 84.2534 +      {
 84.2535 +         back.red = png_ptr->gamma_table[png_ptr->background.red];
 84.2536 +         back.green = png_ptr->gamma_table[png_ptr->background.green];
 84.2537 +         back.blue = png_ptr->gamma_table[png_ptr->background.blue];
 84.2538 +
 84.2539 +         back_1.red = png_ptr->gamma_to_1[png_ptr->background.red];
 84.2540 +         back_1.green = png_ptr->gamma_to_1[png_ptr->background.green];
 84.2541 +         back_1.blue = png_ptr->gamma_to_1[png_ptr->background.blue];
 84.2542 +      }
 84.2543 +      else
 84.2544 +      {
 84.2545 +         double g;
 84.2546 +
 84.2547 +         g = 1.0 / (png_ptr->background_gamma * png_ptr->screen_gamma);
 84.2548 +
 84.2549 +         if (png_ptr->background_gamma_type == PNG_BACKGROUND_GAMMA_SCREEN ||
 84.2550 +             fabs(g - 1.0) < PNG_GAMMA_THRESHOLD)
 84.2551 +         {
 84.2552 +            back.red = png_ptr->background.red;
 84.2553 +            back.green = png_ptr->background.green;
 84.2554 +            back.blue = png_ptr->background.blue;
 84.2555 +         }
 84.2556 +         else
 84.2557 +         {
 84.2558 +            back.red =
 84.2559 +               (png_byte)(pow((double)png_ptr->background.red/255, g) *
 84.2560 +                255.0 + 0.5);
 84.2561 +            back.green =
 84.2562 +               (png_byte)(pow((double)png_ptr->background.green/255, g) *
 84.2563 +                255.0 + 0.5);
 84.2564 +            back.blue =
 84.2565 +               (png_byte)(pow((double)png_ptr->background.blue/255, g) *
 84.2566 +                255.0 + 0.5);
 84.2567 +         }
 84.2568 +
 84.2569 +         g = 1.0 / png_ptr->background_gamma;
 84.2570 +
 84.2571 +         back_1.red =
 84.2572 +            (png_byte)(pow((double)png_ptr->background.red/255, g) *
 84.2573 +             255.0 + 0.5);
 84.2574 +         back_1.green =
 84.2575 +            (png_byte)(pow((double)png_ptr->background.green/255, g) *
 84.2576 +             255.0 + 0.5);
 84.2577 +         back_1.blue =
 84.2578 +            (png_byte)(pow((double)png_ptr->background.blue/255, g) *
 84.2579 +             255.0 + 0.5);
 84.2580 +      }
 84.2581 +
 84.2582 +      if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
 84.2583 +      {
 84.2584 +         png_uint_32 i;
 84.2585 +
 84.2586 +         for (i = 0; i < (png_uint_32)num_palette; i++)
 84.2587 +         {
 84.2588 +            if (i < png_ptr->num_trans && png_ptr->trans[i] == 0)
 84.2589 +            {
 84.2590 +               palette[i] = back;
 84.2591 +            }
 84.2592 +            else if (i < png_ptr->num_trans && png_ptr->trans[i] != 0xff)
 84.2593 +            {
 84.2594 +               png_byte v, w;
 84.2595 +
 84.2596 +               v = png_ptr->gamma_to_1[png_ptr->palette[i].red];
 84.2597 +               png_composite(w, v, png_ptr->trans[i], back_1.red);
 84.2598 +               palette[i].red = png_ptr->gamma_from_1[w];
 84.2599 +
 84.2600 +               v = png_ptr->gamma_to_1[png_ptr->palette[i].green];
 84.2601 +               png_composite(w, v, png_ptr->trans[i], back_1.green);
 84.2602 +               palette[i].green = png_ptr->gamma_from_1[w];
 84.2603 +
 84.2604 +               v = png_ptr->gamma_to_1[png_ptr->palette[i].blue];
 84.2605 +               png_composite(w, v, png_ptr->trans[i], back_1.blue);
 84.2606 +               palette[i].blue = png_ptr->gamma_from_1[w];
 84.2607 +            }
 84.2608 +            else
 84.2609 +            {
 84.2610 +               palette[i].red = png_ptr->gamma_table[palette[i].red];
 84.2611 +               palette[i].green = png_ptr->gamma_table[palette[i].green];
 84.2612 +               palette[i].blue = png_ptr->gamma_table[palette[i].blue];
 84.2613 +            }
 84.2614 +         }
 84.2615 +      }
 84.2616 +      else
 84.2617 +      {
 84.2618 +         int i;
 84.2619 +
 84.2620 +         for (i = 0; i < num_palette; i++)
 84.2621 +         {
 84.2622 +            if (palette[i].red == (png_byte)png_ptr->trans_values.gray)
 84.2623 +            {
 84.2624 +               palette[i] = back;
 84.2625 +            }
 84.2626 +            else
 84.2627 +            {
 84.2628 +               palette[i].red = png_ptr->gamma_table[palette[i].red];
 84.2629 +               palette[i].green = png_ptr->gamma_table[palette[i].green];
 84.2630 +               palette[i].blue = png_ptr->gamma_table[palette[i].blue];
 84.2631 +            }
 84.2632 +         }
 84.2633 +      }
 84.2634 +   }
 84.2635 +   else
 84.2636 +#endif
 84.2637 +#if defined(PNG_READ_GAMMA_SUPPORTED)
 84.2638 +   if (png_ptr->transformations & PNG_GAMMA)
 84.2639 +   {
 84.2640 +      int i;
 84.2641 +
 84.2642 +      for (i = 0; i < num_palette; i++)
 84.2643 +      {
 84.2644 +         palette[i].red = png_ptr->gamma_table[palette[i].red];
 84.2645 +         palette[i].green = png_ptr->gamma_table[palette[i].green];
 84.2646 +         palette[i].blue = png_ptr->gamma_table[palette[i].blue];
 84.2647 +      }
 84.2648 +   }
 84.2649 +#if defined(PNG_READ_BACKGROUND_SUPPORTED)
 84.2650 +   else
 84.2651 +#endif
 84.2652 +#endif
 84.2653 +#if defined(PNG_READ_BACKGROUND_SUPPORTED)
 84.2654 +   if (png_ptr->transformations & PNG_BACKGROUND)
 84.2655 +   {
 84.2656 +      if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
 84.2657 +      {
 84.2658 +         png_color back;
 84.2659 +
 84.2660 +         back.red   = (png_byte)png_ptr->background.red;
 84.2661 +         back.green = (png_byte)png_ptr->background.green;
 84.2662 +         back.blue  = (png_byte)png_ptr->background.blue;
 84.2663 +
 84.2664 +         for (i = 0; i < (int)png_ptr->num_trans; i++)
 84.2665 +         {
 84.2666 +            if (png_ptr->trans[i] == 0)
 84.2667 +            {
 84.2668 +               palette[i].red = back.red;
 84.2669 +               palette[i].green = back.green;
 84.2670 +               palette[i].blue = back.blue;
 84.2671 +            }
 84.2672 +            else if (png_ptr->trans[i] != 0xff)
 84.2673 +            {
 84.2674 +               png_composite(palette[i].red, png_ptr->palette[i].red,
 84.2675 +                  png_ptr->trans[i], back.red);
 84.2676 +               png_composite(palette[i].green, png_ptr->palette[i].green,
 84.2677 +                  png_ptr->trans[i], back.green);
 84.2678 +               png_composite(palette[i].blue, png_ptr->palette[i].blue,
 84.2679 +                  png_ptr->trans[i], back.blue);
 84.2680 +            }
 84.2681 +         }
 84.2682 +      }
 84.2683 +      else /* assume grayscale palette (what else could it be?) */
 84.2684 +      {
 84.2685 +         int i;
 84.2686 +
 84.2687 +         for (i = 0; i < num_palette; i++)
 84.2688 +         {
 84.2689 +            if (i == (png_byte)png_ptr->trans_values.gray)
 84.2690 +            {
 84.2691 +               palette[i].red = (png_byte)png_ptr->background.red;
 84.2692 +               palette[i].green = (png_byte)png_ptr->background.green;
 84.2693 +               palette[i].blue = (png_byte)png_ptr->background.blue;
 84.2694 +            }
 84.2695 +         }
 84.2696 +      }
 84.2697 +   }
 84.2698 +#endif
 84.2699 +}
 84.2700 +#endif
 84.2701 +
 84.2702 +#if defined(PNG_READ_BACKGROUND_SUPPORTED)
 84.2703 +/* Replace any alpha or transparency with the supplied background color.
 84.2704 + * "background" is already in the screen gamma, while "background_1" is
 84.2705 + * at a gamma of 1.0.  Paletted files have already been taken care of.
 84.2706 + */
 84.2707 +void /* PRIVATE */
 84.2708 +png_do_background(png_row_infop row_info, png_bytep row,
 84.2709 +   png_color_16p trans_values, png_color_16p background
 84.2710 +#if defined(PNG_READ_GAMMA_SUPPORTED)
 84.2711 +   , png_color_16p background_1,
 84.2712 +   png_bytep gamma_table, png_bytep gamma_from_1, png_bytep gamma_to_1,
 84.2713 +   png_uint_16pp gamma_16, png_uint_16pp gamma_16_from_1,
 84.2714 +   png_uint_16pp gamma_16_to_1, int gamma_shift
 84.2715 +#endif
 84.2716 +   )
 84.2717 +{
 84.2718 +   png_bytep sp, dp;
 84.2719 +   png_uint_32 i;
 84.2720 +   png_uint_32 row_width=row_info->width;
 84.2721 +   int shift;
 84.2722 +
 84.2723 +   png_debug(1, "in png_do_background\n");
 84.2724 +   if (background != NULL &&
 84.2725 +#if defined(PNG_USELESS_TESTS_SUPPORTED)
 84.2726 +       row != NULL && row_info != NULL &&
 84.2727 +#endif
 84.2728 +      (!(row_info->color_type & PNG_COLOR_MASK_ALPHA) ||
 84.2729 +      (row_info->color_type != PNG_COLOR_TYPE_PALETTE && trans_values)))
 84.2730 +   {
 84.2731 +      switch (row_info->color_type)
 84.2732 +      {
 84.2733 +         case PNG_COLOR_TYPE_GRAY:
 84.2734 +         {
 84.2735 +            switch (row_info->bit_depth)
 84.2736 +            {
 84.2737 +               case 1:
 84.2738 +               {
 84.2739 +                  sp = row;
 84.2740 +                  shift = 7;
 84.2741 +                  for (i = 0; i < row_width; i++)
 84.2742 +                  {
 84.2743 +                     if ((png_uint_16)((*sp >> shift) & 0x01)
 84.2744 +                        == trans_values->gray)
 84.2745 +                     {
 84.2746 +                        *sp &= (png_byte)((0x7f7f >> (7 - shift)) & 0xff);
 84.2747 +                        *sp |= (png_byte)(background->gray << shift);
 84.2748 +                     }
 84.2749 +                     if (!shift)
 84.2750 +                     {
 84.2751 +                        shift = 7;
 84.2752 +                        sp++;
 84.2753 +                     }
 84.2754 +                     else
 84.2755 +                        shift--;
 84.2756 +                  }
 84.2757 +                  break;
 84.2758 +               }
 84.2759 +               case 2:
 84.2760 +               {
 84.2761 +#if defined(PNG_READ_GAMMA_SUPPORTED)
 84.2762 +                  if (gamma_table != NULL)
 84.2763 +                  {
 84.2764 +                     sp = row;
 84.2765 +                     shift = 6;
 84.2766 +                     for (i = 0; i < row_width; i++)
 84.2767 +                     {
 84.2768 +                        if ((png_uint_16)((*sp >> shift) & 0x03)
 84.2769 +                            == trans_values->gray)
 84.2770 +                        {
 84.2771 +                           *sp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff);
 84.2772 +                           *sp |= (png_byte)(background->gray << shift);
 84.2773 +                        }
 84.2774 +                        else
 84.2775 +                        {
 84.2776 +                           png_byte p = (png_byte)((*sp >> shift) & 0x03);
 84.2777 +                           png_byte g = (png_byte)((gamma_table [p | (p << 2) |
 84.2778 +                               (p << 4) | (p << 6)] >> 6) & 0x03);
 84.2779 +                           *sp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff);
 84.2780 +                           *sp |= (png_byte)(g << shift);
 84.2781 +                        }
 84.2782 +                        if (!shift)
 84.2783 +                        {
 84.2784 +                           shift = 6;
 84.2785 +                           sp++;
 84.2786 +                        }
 84.2787 +                        else
 84.2788 +                           shift -= 2;
 84.2789 +                     }
 84.2790 +                  }
 84.2791 +                  else
 84.2792 +#endif
 84.2793 +                  {
 84.2794 +                     sp = row;
 84.2795 +                     shift = 6;
 84.2796 +                     for (i = 0; i < row_width; i++)
 84.2797 +                     {
 84.2798 +                        if ((png_uint_16)((*sp >> shift) & 0x03)
 84.2799 +                            == trans_values->gray)
 84.2800 +                        {
 84.2801 +                           *sp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff);
 84.2802 +                           *sp |= (png_byte)(background->gray << shift);
 84.2803 +                        }
 84.2804 +                        if (!shift)
 84.2805 +                        {
 84.2806 +                           shift = 6;
 84.2807 +                           sp++;
 84.2808 +                        }
 84.2809 +                        else
 84.2810 +                           shift -= 2;
 84.2811 +                     }
 84.2812 +                  }
 84.2813 +                  break;
 84.2814 +               }
 84.2815 +               case 4:
 84.2816 +               {
 84.2817 +#if defined(PNG_READ_GAMMA_SUPPORTED)
 84.2818 +                  if (gamma_table != NULL)
 84.2819 +                  {
 84.2820 +                     sp = row;
 84.2821 +                     shift = 4;
 84.2822 +                     for (i = 0; i < row_width; i++)
 84.2823 +                     {
 84.2824 +                        if ((png_uint_16)((*sp >> shift) & 0x0f)
 84.2825 +                            == trans_values->gray)
 84.2826 +                        {
 84.2827 +                           *sp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff);
 84.2828 +                           *sp |= (png_byte)(background->gray << shift);
 84.2829 +                        }
 84.2830 +                        else
 84.2831 +                        {
 84.2832 +                           png_byte p = (png_byte)((*sp >> shift) & 0x0f);
 84.2833 +                           png_byte g = (png_byte)((gamma_table[p |
 84.2834 +                             (p << 4)] >> 4) & 0x0f);
 84.2835 +                           *sp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff);
 84.2836 +                           *sp |= (png_byte)(g << shift);
 84.2837 +                        }
 84.2838 +                        if (!shift)
 84.2839 +                        {
 84.2840 +                           shift = 4;
 84.2841 +                           sp++;
 84.2842 +                        }
 84.2843 +                        else
 84.2844 +                           shift -= 4;
 84.2845 +                     }
 84.2846 +                  }
 84.2847 +                  else
 84.2848 +#endif
 84.2849 +                  {
 84.2850 +                     sp = row;
 84.2851 +                     shift = 4;
 84.2852 +                     for (i = 0; i < row_width; i++)
 84.2853 +                     {
 84.2854 +                        if ((png_uint_16)((*sp >> shift) & 0x0f)
 84.2855 +                            == trans_values->gray)
 84.2856 +                        {
 84.2857 +                           *sp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff);
 84.2858 +                           *sp |= (png_byte)(background->gray << shift);
 84.2859 +                        }
 84.2860 +                        if (!shift)
 84.2861 +                        {
 84.2862 +                           shift = 4;
 84.2863 +                           sp++;
 84.2864 +                        }
 84.2865 +                        else
 84.2866 +                           shift -= 4;
 84.2867 +                     }
 84.2868 +                  }
 84.2869 +                  break;
 84.2870 +               }
 84.2871 +               case 8:
 84.2872 +               {
 84.2873 +#if defined(PNG_READ_GAMMA_SUPPORTED)
 84.2874 +                  if (gamma_table != NULL)
 84.2875 +                  {
 84.2876 +                     sp = row;
 84.2877 +                     for (i = 0; i < row_width; i++, sp++)
 84.2878 +                     {
 84.2879 +                        if (*sp == trans_values->gray)
 84.2880 +                        {
 84.2881 +                           *sp = (png_byte)background->gray;
 84.2882 +                        }
 84.2883 +                        else
 84.2884 +                        {
 84.2885 +                           *sp = gamma_table[*sp];
 84.2886 +                        }
 84.2887 +                     }
 84.2888 +                  }
 84.2889 +                  else
 84.2890 +#endif
 84.2891 +                  {
 84.2892 +                     sp = row;
 84.2893 +                     for (i = 0; i < row_width; i++, sp++)
 84.2894 +                     {
 84.2895 +                        if (*sp == trans_values->gray)
 84.2896 +                        {
 84.2897 +                           *sp = (png_byte)background->gray;
 84.2898 +                        }
 84.2899 +                     }
 84.2900 +                  }
 84.2901 +                  break;
 84.2902 +               }
 84.2903 +               case 16:
 84.2904 +               {
 84.2905 +#if defined(PNG_READ_GAMMA_SUPPORTED)
 84.2906 +                  if (gamma_16 != NULL)
 84.2907 +                  {
 84.2908 +                     sp = row;
 84.2909 +                     for (i = 0; i < row_width; i++, sp += 2)
 84.2910 +                     {
 84.2911 +                        png_uint_16 v;
 84.2912 +
 84.2913 +                        v = (png_uint_16)(((*sp) << 8) + *(sp + 1));
 84.2914 +                        if (v == trans_values->gray)
 84.2915 +                        {
 84.2916 +                           /* background is already in screen gamma */
 84.2917 +                           *sp = (png_byte)((background->gray >> 8) & 0xff);
 84.2918 +                           *(sp + 1) = (png_byte)(background->gray & 0xff);
 84.2919 +                        }
 84.2920 +                        else
 84.2921 +                        {
 84.2922 +                           v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
 84.2923 +                           *sp = (png_byte)((v >> 8) & 0xff);
 84.2924 +                           *(sp + 1) = (png_byte)(v & 0xff);
 84.2925 +                        }
 84.2926 +                     }
 84.2927 +                  }
 84.2928 +                  else
 84.2929 +#endif
 84.2930 +                  {
 84.2931 +                     sp = row;
 84.2932 +                     for (i = 0; i < row_width; i++, sp += 2)
 84.2933 +                     {
 84.2934 +                        png_uint_16 v;
 84.2935 +
 84.2936 +                        v = (png_uint_16)(((*sp) << 8) + *(sp + 1));
 84.2937 +                        if (v == trans_values->gray)
 84.2938 +                        {
 84.2939 +                           *sp = (png_byte)((background->gray >> 8) & 0xff);
 84.2940 +                           *(sp + 1) = (png_byte)(background->gray & 0xff);
 84.2941 +                        }
 84.2942 +                     }
 84.2943 +                  }
 84.2944 +                  break;
 84.2945 +               }
 84.2946 +            }
 84.2947 +            break;
 84.2948 +         }
 84.2949 +         case PNG_COLOR_TYPE_RGB:
 84.2950 +         {
 84.2951 +            if (row_info->bit_depth == 8)
 84.2952 +            {
 84.2953 +#if defined(PNG_READ_GAMMA_SUPPORTED)
 84.2954 +               if (gamma_table != NULL)
 84.2955 +               {
 84.2956 +                  sp = row;
 84.2957 +                  for (i = 0; i < row_width; i++, sp += 3)
 84.2958 +                  {
 84.2959 +                     if (*sp == trans_values->red &&
 84.2960 +                        *(sp + 1) == trans_values->green &&
 84.2961 +                        *(sp + 2) == trans_values->blue)
 84.2962 +                     {
 84.2963 +                        *sp = (png_byte)background->red;
 84.2964 +                        *(sp + 1) = (png_byte)background->green;
 84.2965 +                        *(sp + 2) = (png_byte)background->blue;
 84.2966 +                     }
 84.2967 +                     else
 84.2968 +                     {
 84.2969 +                        *sp = gamma_table[*sp];
 84.2970 +                        *(sp + 1) = gamma_table[*(sp + 1)];
 84.2971 +                        *(sp + 2) = gamma_table[*(sp + 2)];
 84.2972 +                     }
 84.2973 +                  }
 84.2974 +               }
 84.2975 +               else
 84.2976 +#endif
 84.2977 +               {
 84.2978 +                  sp = row;
 84.2979 +                  for (i = 0; i < row_width; i++, sp += 3)
 84.2980 +                  {
 84.2981 +                     if (*sp == trans_values->red &&
 84.2982 +                        *(sp + 1) == trans_values->green &&
 84.2983 +                        *(sp + 2) == trans_values->blue)
 84.2984 +                     {
 84.2985 +                        *sp = (png_byte)background->red;
 84.2986 +                        *(sp + 1) = (png_byte)background->green;
 84.2987 +                        *(sp + 2) = (png_byte)background->blue;
 84.2988 +                     }
 84.2989 +                  }
 84.2990 +               }
 84.2991 +            }
 84.2992 +            else /* if (row_info->bit_depth == 16) */
 84.2993 +            {
 84.2994 +#if defined(PNG_READ_GAMMA_SUPPORTED)
 84.2995 +               if (gamma_16 != NULL)
 84.2996 +               {
 84.2997 +                  sp = row;
 84.2998 +                  for (i = 0; i < row_width; i++, sp += 6)
 84.2999 +                  {
 84.3000 +                     png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp + 1));
 84.3001 +                     png_uint_16 g = (png_uint_16)(((*(sp+2)) << 8) + *(sp+3));
 84.3002 +                     png_uint_16 b = (png_uint_16)(((*(sp+4)) << 8) + *(sp+5));
 84.3003 +                     if (r == trans_values->red && g == trans_values->green &&
 84.3004 +                        b == trans_values->blue)
 84.3005 +                     {
 84.3006 +                        /* background is already in screen gamma */
 84.3007 +                        *sp = (png_byte)((background->red >> 8) & 0xff);
 84.3008 +                        *(sp + 1) = (png_byte)(background->red & 0xff);
 84.3009 +                        *(sp + 2) = (png_byte)((background->green >> 8) & 0xff);
 84.3010 +                        *(sp + 3) = (png_byte)(background->green & 0xff);
 84.3011 +                        *(sp + 4) = (png_byte)((background->blue >> 8) & 0xff);
 84.3012 +                        *(sp + 5) = (png_byte)(background->blue & 0xff);
 84.3013 +                     }
 84.3014 +                     else
 84.3015 +                     {
 84.3016 +                        png_uint_16 v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
 84.3017 +                        *sp = (png_byte)((v >> 8) & 0xff);
 84.3018 +                        *(sp + 1) = (png_byte)(v & 0xff);
 84.3019 +                        v = gamma_16[*(sp + 3) >> gamma_shift][*(sp + 2)];
 84.3020 +                        *(sp + 2) = (png_byte)((v >> 8) & 0xff);
 84.3021 +                        *(sp + 3) = (png_byte)(v & 0xff);
 84.3022 +                        v = gamma_16[*(sp + 5) >> gamma_shift][*(sp + 4)];
 84.3023 +                        *(sp + 4) = (png_byte)((v >> 8) & 0xff);
 84.3024 +                        *(sp + 5) = (png_byte)(v & 0xff);
 84.3025 +                     }
 84.3026 +                  }
 84.3027 +               }
 84.3028 +               else
 84.3029 +#endif
 84.3030 +               {
 84.3031 +                  sp = row;
 84.3032 +                  for (i = 0; i < row_width; i++, sp += 6)
 84.3033 +                  {
 84.3034 +                     png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp+1));
 84.3035 +                     png_uint_16 g = (png_uint_16)(((*(sp+2)) << 8) + *(sp+3));
 84.3036 +                     png_uint_16 b = (png_uint_16)(((*(sp+4)) << 8) + *(sp+5));
 84.3037 +
 84.3038 +                     if (r == trans_values->red && g == trans_values->green &&
 84.3039 +                        b == trans_values->blue)
 84.3040 +                     {
 84.3041 +                        *sp = (png_byte)((background->red >> 8) & 0xff);
 84.3042 +                        *(sp + 1) = (png_byte)(background->red & 0xff);
 84.3043 +                        *(sp + 2) = (png_byte)((background->green >> 8) & 0xff);
 84.3044 +                        *(sp + 3) = (png_byte)(background->green & 0xff);
 84.3045 +                        *(sp + 4) = (png_byte)((background->blue >> 8) & 0xff);
 84.3046 +                        *(sp + 5) = (png_byte)(background->blue & 0xff);
 84.3047 +                     }
 84.3048 +                  }
 84.3049 +               }
 84.3050 +            }
 84.3051 +            break;
 84.3052 +         }
 84.3053 +         case PNG_COLOR_TYPE_GRAY_ALPHA:
 84.3054 +         {
 84.3055 +            if (row_info->bit_depth == 8)
 84.3056 +            {
 84.3057 +#if defined(PNG_READ_GAMMA_SUPPORTED)
 84.3058 +               if (gamma_to_1 != NULL && gamma_from_1 != NULL &&
 84.3059 +                   gamma_table != NULL)
 84.3060 +               {
 84.3061 +                  sp = row;
 84.3062 +                  dp = row;
 84.3063 +                  for (i = 0; i < row_width; i++, sp += 2, dp++)
 84.3064 +                  {
 84.3065 +                     png_uint_16 a = *(sp + 1);
 84.3066 +
 84.3067 +                     if (a == 0xff)
 84.3068 +                     {
 84.3069 +                        *dp = gamma_table[*sp];
 84.3070 +                     }
 84.3071 +                     else if (a == 0)
 84.3072 +                     {
 84.3073 +                        /* background is already in screen gamma */
 84.3074 +                        *dp = (png_byte)background->gray;
 84.3075 +                     }
 84.3076 +                     else
 84.3077 +                     {
 84.3078 +                        png_byte v, w;
 84.3079 +
 84.3080 +                        v = gamma_to_1[*sp];
 84.3081 +                        png_composite(w, v, a, background_1->gray);
 84.3082 +                        *dp = gamma_from_1[w];
 84.3083 +                     }
 84.3084 +                  }
 84.3085 +               }
 84.3086 +               else
 84.3087 +#endif
 84.3088 +               {
 84.3089 +                  sp = row;
 84.3090 +                  dp = row;
 84.3091 +                  for (i = 0; i < row_width; i++, sp += 2, dp++)
 84.3092 +                  {
 84.3093 +                     png_byte a = *(sp + 1);
 84.3094 +
 84.3095 +                     if (a == 0xff)
 84.3096 +                     {
 84.3097 +                        *dp = *sp;
 84.3098 +                     }
 84.3099 +#if defined(PNG_READ_GAMMA_SUPPORTED)
 84.3100 +                     else if (a == 0)
 84.3101 +                     {
 84.3102 +                        *dp = (png_byte)background->gray;
 84.3103 +                     }
 84.3104 +                     else
 84.3105 +                     {
 84.3106 +                        png_composite(*dp, *sp, a, background_1->gray);
 84.3107 +                     }
 84.3108 +#else
 84.3109 +                     *dp = (png_byte)background->gray;
 84.3110 +#endif
 84.3111 +                  }
 84.3112 +               }
 84.3113 +            }
 84.3114 +            else /* if (png_ptr->bit_depth == 16) */
 84.3115 +            {
 84.3116 +#if defined(PNG_READ_GAMMA_SUPPORTED)
 84.3117 +               if (gamma_16 != NULL && gamma_16_from_1 != NULL &&
 84.3118 +                   gamma_16_to_1 != NULL)
 84.3119 +               {
 84.3120 +                  sp = row;
 84.3121 +                  dp = row;
 84.3122 +                  for (i = 0; i < row_width; i++, sp += 4, dp += 2)
 84.3123 +                  {
 84.3124 +                     png_uint_16 a = (png_uint_16)(((*(sp+2)) << 8) + *(sp+3));
 84.3125 +
 84.3126 +                     if (a == (png_uint_16)0xffff)
 84.3127 +                     {
 84.3128 +                        png_uint_16 v;
 84.3129 +
 84.3130 +                        v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
 84.3131 +                        *dp = (png_byte)((v >> 8) & 0xff);
 84.3132 +                        *(dp + 1) = (png_byte)(v & 0xff);
 84.3133 +                     }
 84.3134 +#if defined(PNG_READ_GAMMA_SUPPORTED)
 84.3135 +                     else if (a == 0)
 84.3136 +#else
 84.3137 +                     else
 84.3138 +#endif
 84.3139 +                     {
 84.3140 +                        /* background is already in screen gamma */
 84.3141 +                        *dp = (png_byte)((background->gray >> 8) & 0xff);
 84.3142 +                        *(dp + 1) = (png_byte)(background->gray & 0xff);
 84.3143 +                     }
 84.3144 +#if defined(PNG_READ_GAMMA_SUPPORTED)
 84.3145 +                     else
 84.3146 +                     {
 84.3147 +                        png_uint_16 g, v, w;
 84.3148 +
 84.3149 +                        g = gamma_16_to_1[*(sp + 1) >> gamma_shift][*sp];
 84.3150 +                        png_composite_16(v, g, a, background_1->gray);
 84.3151 +                        w = gamma_16_from_1[(v&0xff) >> gamma_shift][v >> 8];
 84.3152 +                        *dp = (png_byte)((w >> 8) & 0xff);
 84.3153 +                        *(dp + 1) = (png_byte)(w & 0xff);
 84.3154 +                     }
 84.3155 +#endif
 84.3156 +                  }
 84.3157 +               }
 84.3158 +               else
 84.3159 +#endif
 84.3160 +               {
 84.3161 +                  sp = row;
 84.3162 +                  dp = row;
 84.3163 +                  for (i = 0; i < row_width; i++, sp += 4, dp += 2)
 84.3164 +                  {
 84.3165 +                     png_uint_16 a = (png_uint_16)(((*(sp+2)) << 8) + *(sp+3));
 84.3166 +                     if (a == (png_uint_16)0xffff)
 84.3167 +                     {
 84.3168 +                        png_memcpy(dp, sp, 2);
 84.3169 +                     }
 84.3170 +#if defined(PNG_READ_GAMMA_SUPPORTED)
 84.3171 +                     else if (a == 0)
 84.3172 +#else
 84.3173 +                     else
 84.3174 +#endif
 84.3175 +                     {
 84.3176 +                        *dp = (png_byte)((background->gray >> 8) & 0xff);
 84.3177 +                        *(dp + 1) = (png_byte)(background->gray & 0xff);
 84.3178 +                     }
 84.3179 +#if defined(PNG_READ_GAMMA_SUPPORTED)
 84.3180 +                     else
 84.3181 +                     {
 84.3182 +                        png_uint_16 g, v;
 84.3183 +
 84.3184 +                        g = (png_uint_16)(((*sp) << 8) + *(sp + 1));
 84.3185 +                        png_composite_16(v, g, a, background_1->gray);
 84.3186 +                        *dp = (png_byte)((v >> 8) & 0xff);
 84.3187 +                        *(dp + 1) = (png_byte)(v & 0xff);
 84.3188 +                     }
 84.3189 +#endif
 84.3190 +                  }
 84.3191 +               }
 84.3192 +            }
 84.3193 +            break;
 84.3194 +         }
 84.3195 +         case PNG_COLOR_TYPE_RGB_ALPHA:
 84.3196 +         {
 84.3197 +            if (row_info->bit_depth == 8)
 84.3198 +            {
 84.3199 +#if defined(PNG_READ_GAMMA_SUPPORTED)
 84.3200 +               if (gamma_to_1 != NULL && gamma_from_1 != NULL &&
 84.3201 +                   gamma_table != NULL)
 84.3202 +               {
 84.3203 +                  sp = row;
 84.3204 +                  dp = row;
 84.3205 +                  for (i = 0; i < row_width; i++, sp += 4, dp += 3)
 84.3206 +                  {
 84.3207 +                     png_byte a = *(sp + 3);
 84.3208 +
 84.3209 +                     if (a == 0xff)
 84.3210 +                     {
 84.3211 +                        *dp = gamma_table[*sp];
 84.3212 +                        *(dp + 1) = gamma_table[*(sp + 1)];
 84.3213 +                        *(dp + 2) = gamma_table[*(sp + 2)];
 84.3214 +                     }
 84.3215 +                     else if (a == 0)
 84.3216 +                     {
 84.3217 +                        /* background is already in screen gamma */
 84.3218 +                        *dp = (png_byte)background->red;
 84.3219 +                        *(dp + 1) = (png_byte)background->green;
 84.3220 +                        *(dp + 2) = (png_byte)background->blue;
 84.3221 +                     }
 84.3222 +                     else
 84.3223 +                     {
 84.3224 +                        png_byte v, w;
 84.3225 +
 84.3226 +                        v = gamma_to_1[*sp];
 84.3227 +                        png_composite(w, v, a, background_1->red);
 84.3228 +                        *dp = gamma_from_1[w];
 84.3229 +                        v = gamma_to_1[*(sp + 1)];
 84.3230 +                        png_composite(w, v, a, background_1->green);
 84.3231 +                        *(dp + 1) = gamma_from_1[w];
 84.3232 +                        v = gamma_to_1[*(sp + 2)];
 84.3233 +                        png_composite(w, v, a, background_1->blue);
 84.3234 +                        *(dp + 2) = gamma_from_1[w];
 84.3235 +                     }
 84.3236 +                  }
 84.3237 +               }
 84.3238 +               else
 84.3239 +#endif
 84.3240 +               {
 84.3241 +                  sp = row;
 84.3242 +                  dp = row;
 84.3243 +                  for (i = 0; i < row_width; i++, sp += 4, dp += 3)
 84.3244 +                  {
 84.3245 +                     png_byte a = *(sp + 3);
 84.3246 +
 84.3247 +                     if (a == 0xff)
 84.3248 +                     {
 84.3249 +                        *dp = *sp;
 84.3250 +                        *(dp + 1) = *(sp + 1);
 84.3251 +                        *(dp + 2) = *(sp + 2);
 84.3252 +                     }
 84.3253 +                     else if (a == 0)
 84.3254 +                     {
 84.3255 +                        *dp = (png_byte)background->red;
 84.3256 +                        *(dp + 1) = (png_byte)background->green;
 84.3257 +                        *(dp + 2) = (png_byte)background->blue;
 84.3258 +                     }
 84.3259 +                     else
 84.3260 +                     {
 84.3261 +                        png_composite(*dp, *sp, a, background->red);
 84.3262 +                        png_composite(*(dp + 1), *(sp + 1), a,
 84.3263 +                           background->green);
 84.3264 +                        png_composite(*(dp + 2), *(sp + 2), a,
 84.3265 +                           background->blue);
 84.3266 +                     }
 84.3267 +                  }
 84.3268 +               }
 84.3269 +            }
 84.3270 +            else /* if (row_info->bit_depth == 16) */
 84.3271 +            {
 84.3272 +#if defined(PNG_READ_GAMMA_SUPPORTED)
 84.3273 +               if (gamma_16 != NULL && gamma_16_from_1 != NULL &&
 84.3274 +                   gamma_16_to_1 != NULL)
 84.3275 +               {
 84.3276 +                  sp = row;
 84.3277 +                  dp = row;
 84.3278 +                  for (i = 0; i < row_width; i++, sp += 8, dp += 6)
 84.3279 +                  {
 84.3280 +                     png_uint_16 a = (png_uint_16)(((png_uint_16)(*(sp + 6))
 84.3281 +                         << 8) + (png_uint_16)(*(sp + 7)));
 84.3282 +                     if (a == (png_uint_16)0xffff)
 84.3283 +                     {
 84.3284 +                        png_uint_16 v;
 84.3285 +
 84.3286 +                        v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
 84.3287 +                        *dp = (png_byte)((v >> 8) & 0xff);
 84.3288 +                        *(dp + 1) = (png_byte)(v & 0xff);
 84.3289 +                        v = gamma_16[*(sp + 3) >> gamma_shift][*(sp + 2)];
 84.3290 +                        *(dp + 2) = (png_byte)((v >> 8) & 0xff);
 84.3291 +                        *(dp + 3) = (png_byte)(v & 0xff);
 84.3292 +                        v = gamma_16[*(sp + 5) >> gamma_shift][*(sp + 4)];
 84.3293 +                        *(dp + 4) = (png_byte)((v >> 8) & 0xff);
 84.3294 +                        *(dp + 5) = (png_byte)(v & 0xff);
 84.3295 +                     }
 84.3296 +                     else if (a == 0)
 84.3297 +                     {
 84.3298 +                        /* background is already in screen gamma */
 84.3299 +                        *dp = (png_byte)((background->red >> 8) & 0xff);
 84.3300 +                        *(dp + 1) = (png_byte)(background->red & 0xff);
 84.3301 +                        *(dp + 2) = (png_byte)((background->green >> 8) & 0xff);
 84.3302 +                        *(dp + 3) = (png_byte)(background->green & 0xff);
 84.3303 +                        *(dp + 4) = (png_byte)((background->blue >> 8) & 0xff);
 84.3304 +                        *(dp + 5) = (png_byte)(background->blue & 0xff);
 84.3305 +                     }
 84.3306 +                     else
 84.3307 +                     {
 84.3308 +                        png_uint_16 v, w, x;
 84.3309 +
 84.3310 +                        v = gamma_16_to_1[*(sp + 1) >> gamma_shift][*sp];
 84.3311 +                        png_composite_16(w, v, a, background_1->red);
 84.3312 +                        x = gamma_16_from_1[((w&0xff) >> gamma_shift)][w >> 8];
 84.3313 +                        *dp = (png_byte)((x >> 8) & 0xff);
 84.3314 +                        *(dp + 1) = (png_byte)(x & 0xff);
 84.3315 +                        v = gamma_16_to_1[*(sp + 3) >> gamma_shift][*(sp + 2)];
 84.3316 +                        png_composite_16(w, v, a, background_1->green);
 84.3317 +                        x = gamma_16_from_1[((w&0xff) >> gamma_shift)][w >> 8];
 84.3318 +                        *(dp + 2) = (png_byte)((x >> 8) & 0xff);
 84.3319 +                        *(dp + 3) = (png_byte)(x & 0xff);
 84.3320 +                        v = gamma_16_to_1[*(sp + 5) >> gamma_shift][*(sp + 4)];
 84.3321 +                        png_composite_16(w, v, a, background_1->blue);
 84.3322 +                        x = gamma_16_from_1[(w & 0xff) >> gamma_shift][w >> 8];
 84.3323 +                        *(dp + 4) = (png_byte)((x >> 8) & 0xff);
 84.3324 +                        *(dp + 5) = (png_byte)(x & 0xff);
 84.3325 +                     }
 84.3326 +                  }
 84.3327 +               }
 84.3328 +               else
 84.3329 +#endif
 84.3330 +               {
 84.3331 +                  sp = row;
 84.3332 +                  dp = row;
 84.3333 +                  for (i = 0; i < row_width; i++, sp += 8, dp += 6)
 84.3334 +                  {
 84.3335 +                     png_uint_16 a = (png_uint_16)(((png_uint_16)(*(sp + 6))
 84.3336 +                        << 8) + (png_uint_16)(*(sp + 7)));
 84.3337 +                     if (a == (png_uint_16)0xffff)
 84.3338 +                     {
 84.3339 +                        png_memcpy(dp, sp, 6);
 84.3340 +                     }
 84.3341 +                     else if (a == 0)
 84.3342 +                     {
 84.3343 +                        *dp = (png_byte)((background->red >> 8) & 0xff);
 84.3344 +                        *(dp + 1) = (png_byte)(background->red & 0xff);
 84.3345 +                        *(dp + 2) = (png_byte)((background->green >> 8) & 0xff);
 84.3346 +                        *(dp + 3) = (png_byte)(background->green & 0xff);
 84.3347 +                        *(dp + 4) = (png_byte)((background->blue >> 8) & 0xff);
 84.3348 +                        *(dp + 5) = (png_byte)(background->blue & 0xff);
 84.3349 +                     }
 84.3350 +                     else
 84.3351 +                     {
 84.3352 +                        png_uint_16 v;
 84.3353 +
 84.3354 +                        png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp + 1));
 84.3355 +                        png_uint_16 g = (png_uint_16)(((*(sp + 2)) << 8)
 84.3356 +                            + *(sp + 3));
 84.3357 +                        png_uint_16 b = (png_uint_16)(((*(sp + 4)) << 8)
 84.3358 +                            + *(sp + 5));
 84.3359 +
 84.3360 +                        png_composite_16(v, r, a, background->red);
 84.3361 +                        *dp = (png_byte)((v >> 8) & 0xff);
 84.3362 +                        *(dp + 1) = (png_byte)(v & 0xff);
 84.3363 +                        png_composite_16(v, g, a, background->green);
 84.3364 +                        *(dp + 2) = (png_byte)((v >> 8) & 0xff);
 84.3365 +                        *(dp + 3) = (png_byte)(v & 0xff);
 84.3366 +                        png_composite_16(v, b, a, background->blue);
 84.3367 +                        *(dp + 4) = (png_byte)((v >> 8) & 0xff);
 84.3368 +                        *(dp + 5) = (png_byte)(v & 0xff);
 84.3369 +                     }
 84.3370 +                  }
 84.3371 +               }
 84.3372 +            }
 84.3373 +            break;
 84.3374 +         }
 84.3375 +      }
 84.3376 +
 84.3377 +      if (row_info->color_type & PNG_COLOR_MASK_ALPHA)
 84.3378 +      {
 84.3379 +         row_info->color_type &= ~PNG_COLOR_MASK_ALPHA;
 84.3380 +         row_info->channels--;
 84.3381 +         row_info->pixel_depth = (png_byte)(row_info->channels *
 84.3382 +            row_info->bit_depth);
 84.3383 +         row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
 84.3384 +      }
 84.3385 +   }
 84.3386 +}
 84.3387 +#endif
 84.3388 +
 84.3389 +#if defined(PNG_READ_GAMMA_SUPPORTED)
 84.3390 +/* Gamma correct the image, avoiding the alpha channel.  Make sure
 84.3391 + * you do this after you deal with the transparency issue on grayscale
 84.3392 + * or RGB images. If your bit depth is 8, use gamma_table, if it
 84.3393 + * is 16, use gamma_16_table and gamma_shift.  Build these with
 84.3394 + * build_gamma_table().
 84.3395 + */
 84.3396 +void /* PRIVATE */
 84.3397 +png_do_gamma(png_row_infop row_info, png_bytep row,
 84.3398 +   png_bytep gamma_table, png_uint_16pp gamma_16_table,
 84.3399 +   int gamma_shift)
 84.3400 +{
 84.3401 +   png_bytep sp;
 84.3402 +   png_uint_32 i;
 84.3403 +   png_uint_32 row_width=row_info->width;
 84.3404 +
 84.3405 +   png_debug(1, "in png_do_gamma\n");
 84.3406 +   if (
 84.3407 +#if defined(PNG_USELESS_TESTS_SUPPORTED)
 84.3408 +       row != NULL && row_info != NULL &&
 84.3409 +#endif
 84.3410 +       ((row_info->bit_depth <= 8 && gamma_table != NULL) ||
 84.3411 +        (row_info->bit_depth == 16 && gamma_16_table != NULL)))
 84.3412 +   {
 84.3413 +      switch (row_info->color_type)
 84.3414 +      {
 84.3415 +         case PNG_COLOR_TYPE_RGB:
 84.3416 +         {
 84.3417 +            if (row_info->bit_depth == 8)
 84.3418 +            {
 84.3419 +               sp = row;
 84.3420 +               for (i = 0; i < row_width; i++)
 84.3421 +               {
 84.3422 +                  *sp = gamma_table[*sp];
 84.3423 +                  sp++;
 84.3424 +                  *sp = gamma_table[*sp];
 84.3425 +                  sp++;
 84.3426 +                  *sp = gamma_table[*sp];
 84.3427 +                  sp++;
 84.3428 +               }
 84.3429 +            }
 84.3430 +            else /* if (row_info->bit_depth == 16) */
 84.3431 +            {
 84.3432 +               sp = row;
 84.3433 +               for (i = 0; i < row_width; i++)
 84.3434 +               {
 84.3435 +                  png_uint_16 v;
 84.3436 +
 84.3437 +                  v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
 84.3438 +                  *sp = (png_byte)((v >> 8) & 0xff);
 84.3439 +                  *(sp + 1) = (png_byte)(v & 0xff);
 84.3440 +                  sp += 2;
 84.3441 +                  v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
 84.3442 +                  *sp = (png_byte)((v >> 8) & 0xff);
 84.3443 +                  *(sp + 1) = (png_byte)(v & 0xff);
 84.3444 +                  sp += 2;
 84.3445 +                  v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
 84.3446 +                  *sp = (png_byte)((v >> 8) & 0xff);
 84.3447 +                  *(sp + 1) = (png_byte)(v & 0xff);
 84.3448 +                  sp += 2;
 84.3449 +               }
 84.3450 +            }
 84.3451 +            break;
 84.3452 +         }
 84.3453 +         case PNG_COLOR_TYPE_RGB_ALPHA:
 84.3454 +         {
 84.3455 +            if (row_info->bit_depth == 8)
 84.3456 +            {
 84.3457 +               sp = row;
 84.3458 +               for (i = 0; i < row_width; i++)
 84.3459 +               {
 84.3460 +                  *sp = gamma_table[*sp];
 84.3461 +                  sp++;
 84.3462 +                  *sp = gamma_table[*sp];
 84.3463 +                  sp++;
 84.3464 +                  *sp = gamma_table[*sp];
 84.3465 +                  sp++;
 84.3466 +                  sp++;
 84.3467 +               }
 84.3468 +            }
 84.3469 +            else /* if (row_info->bit_depth == 16) */
 84.3470 +            {
 84.3471 +               sp = row;
 84.3472 +               for (i = 0; i < row_width; i++)
 84.3473 +               {
 84.3474 +                  png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
 84.3475 +                  *sp = (png_byte)((v >> 8) & 0xff);
 84.3476 +                  *(sp + 1) = (png_byte)(v & 0xff);
 84.3477 +                  sp += 2;
 84.3478 +                  v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
 84.3479 +                  *sp = (png_byte)((v >> 8) & 0xff);
 84.3480 +                  *(sp + 1) = (png_byte)(v & 0xff);
 84.3481 +                  sp += 2;
 84.3482 +                  v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
 84.3483 +                  *sp = (png_byte)((v >> 8) & 0xff);
 84.3484 +                  *(sp + 1) = (png_byte)(v & 0xff);
 84.3485 +                  sp += 4;
 84.3486 +               }
 84.3487 +            }
 84.3488 +            break;
 84.3489 +         }
 84.3490 +         case PNG_COLOR_TYPE_GRAY_ALPHA:
 84.3491 +         {
 84.3492 +            if (row_info->bit_depth == 8)
 84.3493 +            {
 84.3494 +               sp = row;
 84.3495 +               for (i = 0; i < row_width; i++)
 84.3496 +               {
 84.3497 +                  *sp = gamma_table[*sp];
 84.3498 +                  sp += 2;
 84.3499 +               }
 84.3500 +            }
 84.3501 +            else /* if (row_info->bit_depth == 16) */
 84.3502 +            {
 84.3503 +               sp = row;
 84.3504 +               for (i = 0; i < row_width; i++)
 84.3505 +               {
 84.3506 +                  png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
 84.3507 +                  *sp = (png_byte)((v >> 8) & 0xff);
 84.3508 +                  *(sp + 1) = (png_byte)(v & 0xff);
 84.3509 +                  sp += 4;
 84.3510 +               }
 84.3511 +            }
 84.3512 +            break;
 84.3513 +         }
 84.3514 +         case PNG_COLOR_TYPE_GRAY:
 84.3515 +         {
 84.3516 +            if (row_info->bit_depth == 2)
 84.3517 +            {
 84.3518 +               sp = row;
 84.3519 +               for (i = 0; i < row_width; i += 4)
 84.3520 +               {
 84.3521 +                  int a = *sp & 0xc0;
 84.3522 +                  int b = *sp & 0x30;
 84.3523 +                  int c = *sp & 0x0c;
 84.3524 +                  int d = *sp & 0x03;
 84.3525 +
 84.3526 +                  *sp = (png_byte)(
 84.3527 +                        ((((int)gamma_table[a|(a>>2)|(a>>4)|(a>>6)])   ) & 0xc0)|
 84.3528 +                        ((((int)gamma_table[(b<<2)|b|(b>>2)|(b>>4)])>>2) & 0x30)|
 84.3529 +                        ((((int)gamma_table[(c<<4)|(c<<2)|c|(c>>2)])>>4) & 0x0c)|
 84.3530 +                        ((((int)gamma_table[(d<<6)|(d<<4)|(d<<2)|d])>>6) ));
 84.3531 +                  sp++;
 84.3532 +               }
 84.3533 +            }
 84.3534 +            if (row_info->bit_depth == 4)
 84.3535 +            {
 84.3536 +               sp = row;
 84.3537 +               for (i = 0; i < row_width; i += 2)
 84.3538 +               {
 84.3539 +                  int msb = *sp & 0xf0;
 84.3540 +                  int lsb = *sp & 0x0f;
 84.3541 +
 84.3542 +                  *sp = (png_byte)((((int)gamma_table[msb | (msb >> 4)]) & 0xf0)
 84.3543 +                          | (((int)gamma_table[(lsb << 4) | lsb]) >> 4));
 84.3544 +                  sp++;
 84.3545 +               }
 84.3546 +            }
 84.3547 +            else if (row_info->bit_depth == 8)
 84.3548 +            {
 84.3549 +               sp = row;
 84.3550 +               for (i = 0; i < row_width; i++)
 84.3551 +               {
 84.3552 +                  *sp = gamma_table[*sp];
 84.3553 +                  sp++;
 84.3554 +               }
 84.3555 +            }
 84.3556 +            else if (row_info->bit_depth == 16)
 84.3557 +            {
 84.3558 +               sp = row;
 84.3559 +               for (i = 0; i < row_width; i++)
 84.3560 +               {
 84.3561 +                  png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
 84.3562 +                  *sp = (png_byte)((v >> 8) & 0xff);
 84.3563 +                  *(sp + 1) = (png_byte)(v & 0xff);
 84.3564 +                  sp += 2;
 84.3565 +               }
 84.3566 +            }
 84.3567 +            break;
 84.3568 +         }
 84.3569 +      }
 84.3570 +   }
 84.3571 +}
 84.3572 +#endif
 84.3573 +
 84.3574 +#if defined(PNG_READ_EXPAND_SUPPORTED)
 84.3575 +/* Expands a palette row to an RGB or RGBA row depending
 84.3576 + * upon whether you supply trans and num_trans.
 84.3577 + */
 84.3578 +void /* PRIVATE */
 84.3579 +png_do_expand_palette(png_row_infop row_info, png_bytep row,
 84.3580 +   png_colorp palette, png_bytep trans, int num_trans)
 84.3581 +{
 84.3582 +   int shift, value;
 84.3583 +   png_bytep sp, dp;
 84.3584 +   png_uint_32 i;
 84.3585 +   png_uint_32 row_width=row_info->width;
 84.3586 +
 84.3587 +   png_debug(1, "in png_do_expand_palette\n");
 84.3588 +   if (
 84.3589 +#if defined(PNG_USELESS_TESTS_SUPPORTED)
 84.3590 +       row != NULL && row_info != NULL &&
 84.3591 +#endif
 84.3592 +       row_info->color_type == PNG_COLOR_TYPE_PALETTE)
 84.3593 +   {
 84.3594 +      if (row_info->bit_depth < 8)
 84.3595 +      {
 84.3596 +         switch (row_info->bit_depth)
 84.3597 +         {
 84.3598 +            case 1:
 84.3599 +            {
 84.3600 +               sp = row + (png_size_t)((row_width - 1) >> 3);
 84.3601 +               dp = row + (png_size_t)row_width - 1;
 84.3602 +               shift = 7 - (int)((row_width + 7) & 0x07);
 84.3603 +               for (i = 0; i < row_width; i++)
 84.3604 +               {
 84.3605 +                  if ((*sp >> shift) & 0x01)
 84.3606 +                     *dp = 1;
 84.3607 +                  else
 84.3608 +                     *dp = 0;
 84.3609 +                  if (shift == 7)
 84.3610 +                  {
 84.3611 +                     shift = 0;
 84.3612 +                     sp--;
 84.3613 +                  }
 84.3614 +                  else
 84.3615 +                     shift++;
 84.3616 +
 84.3617 +                  dp--;
 84.3618 +               }
 84.3619 +               break;
 84.3620 +            }
 84.3621 +            case 2:
 84.3622 +            {
 84.3623 +               sp = row + (png_size_t)((row_width - 1) >> 2);
 84.3624 +               dp = row + (png_size_t)row_width - 1;
 84.3625 +               shift = (int)((3 - ((row_width + 3) & 0x03)) << 1);
 84.3626 +               for (i = 0; i < row_width; i++)
 84.3627 +               {
 84.3628 +                  value = (*sp >> shift) & 0x03;
 84.3629 +                  *dp = (png_byte)value;
 84.3630 +                  if (shift == 6)
 84.3631 +                  {
 84.3632 +                     shift = 0;
 84.3633 +                     sp--;
 84.3634 +                  }
 84.3635 +                  else
 84.3636 +                     shift += 2;
 84.3637 +
 84.3638 +                  dp--;
 84.3639 +               }
 84.3640 +               break;
 84.3641 +            }
 84.3642 +            case 4:
 84.3643 +            {
 84.3644 +               sp = row + (png_size_t)((row_width - 1) >> 1);
 84.3645 +               dp = row + (png_size_t)row_width - 1;
 84.3646 +               shift = (int)((row_width & 0x01) << 2);
 84.3647 +               for (i = 0; i < row_width; i++)
 84.3648 +               {
 84.3649 +                  value = (*sp >> shift) & 0x0f;
 84.3650 +                  *dp = (png_byte)value;
 84.3651 +                  if (shift == 4)
 84.3652 +                  {
 84.3653 +                     shift = 0;
 84.3654 +                     sp--;
 84.3655 +                  }
 84.3656 +                  else
 84.3657 +                     shift += 4;
 84.3658 +
 84.3659 +                  dp--;
 84.3660 +               }
 84.3661 +               break;
 84.3662 +            }
 84.3663 +         }
 84.3664 +         row_info->bit_depth = 8;
 84.3665 +         row_info->pixel_depth = 8;
 84.3666 +         row_info->rowbytes = row_width;
 84.3667 +      }
 84.3668 +      switch (row_info->bit_depth)
 84.3669 +      {
 84.3670 +         case 8:
 84.3671 +         {
 84.3672 +            if (trans != NULL)
 84.3673 +            {
 84.3674 +               sp = row + (png_size_t)row_width - 1;
 84.3675 +               dp = row + (png_size_t)(row_width << 2) - 1;
 84.3676 +
 84.3677 +               for (i = 0; i < row_width; i++)
 84.3678 +               {
 84.3679 +                  if ((int)(*sp) >= num_trans)
 84.3680 +                     *dp-- = 0xff;
 84.3681 +                  else
 84.3682 +                     *dp-- = trans[*sp];
 84.3683 +                  *dp-- = palette[*sp].blue;
 84.3684 +                  *dp-- = palette[*sp].green;
 84.3685 +                  *dp-- = palette[*sp].red;
 84.3686 +                  sp--;
 84.3687 +               }
 84.3688 +               row_info->bit_depth = 8;
 84.3689 +               row_info->pixel_depth = 32;
 84.3690 +               row_info->rowbytes = row_width * 4;
 84.3691 +               row_info->color_type = 6;
 84.3692 +               row_info->channels = 4;
 84.3693 +            }
 84.3694 +            else
 84.3695 +            {
 84.3696 +               sp = row + (png_size_t)row_width - 1;
 84.3697 +               dp = row + (png_size_t)(row_width * 3) - 1;
 84.3698 +
 84.3699 +               for (i = 0; i < row_width; i++)
 84.3700 +               {
 84.3701 +                  *dp-- = palette[*sp].blue;
 84.3702 +                  *dp-- = palette[*sp].green;
 84.3703 +                  *dp-- = palette[*sp].red;
 84.3704 +                  sp--;
 84.3705 +               }
 84.3706 +               row_info->bit_depth = 8;
 84.3707 +               row_info->pixel_depth = 24;
 84.3708 +               row_info->rowbytes = row_width * 3;
 84.3709 +               row_info->color_type = 2;
 84.3710 +               row_info->channels = 3;
 84.3711 +            }
 84.3712 +            break;
 84.3713 +         }
 84.3714 +      }
 84.3715 +   }
 84.3716 +}
 84.3717 +
 84.3718 +/* If the bit depth < 8, it is expanded to 8.  Also, if the already
 84.3719 + * expanded transparency value is supplied, an alpha channel is built.
 84.3720 + */
 84.3721 +void /* PRIVATE */
 84.3722 +png_do_expand(png_row_infop row_info, png_bytep row,
 84.3723 +   png_color_16p trans_value)
 84.3724 +{
 84.3725 +   int shift, value;
 84.3726 +   png_bytep sp, dp;
 84.3727 +   png_uint_32 i;
 84.3728 +   png_uint_32 row_width=row_info->width;
 84.3729 +
 84.3730 +   png_debug(1, "in png_do_expand\n");
 84.3731 +#if defined(PNG_USELESS_TESTS_SUPPORTED)
 84.3732 +   if (row != NULL && row_info != NULL)
 84.3733 +#endif
 84.3734 +   {
 84.3735 +      if (row_info->color_type == PNG_COLOR_TYPE_GRAY)
 84.3736 +      {
 84.3737 +         png_uint_16 gray = (png_uint_16)(trans_value ? trans_value->gray : 0);
 84.3738 +
 84.3739 +         if (row_info->bit_depth < 8)
 84.3740 +         {
 84.3741 +            switch (row_info->bit_depth)
 84.3742 +            {
 84.3743 +               case 1:
 84.3744 +               {
 84.3745 +                  gray = (png_uint_16)((gray&0x01)*0xff);
 84.3746 +                  sp = row + (png_size_t)((row_width - 1) >> 3);
 84.3747 +                  dp = row + (png_size_t)row_width - 1;
 84.3748 +                  shift = 7 - (int)((row_width + 7) & 0x07);
 84.3749 +                  for (i = 0; i < row_width; i++)
 84.3750 +                  {
 84.3751 +                     if ((*sp >> shift) & 0x01)
 84.3752 +                        *dp = 0xff;
 84.3753 +                     else
 84.3754 +                        *dp = 0;
 84.3755 +                     if (shift == 7)
 84.3756 +                     {
 84.3757 +                        shift = 0;
 84.3758 +                        sp--;
 84.3759 +                     }
 84.3760 +                     else
 84.3761 +                        shift++;
 84.3762 +
 84.3763 +                     dp--;
 84.3764 +                  }
 84.3765 +                  break;
 84.3766 +               }
 84.3767 +               case 2:
 84.3768 +               {
 84.3769 +                  gray = (png_uint_16)((gray&0x03)*0x55);
 84.3770 +                  sp = row + (png_size_t)((row_width - 1) >> 2);
 84.3771 +                  dp = row + (png_size_t)row_width - 1;
 84.3772 +                  shift = (int)((3 - ((row_width + 3) & 0x03)) << 1);
 84.3773 +                  for (i = 0; i < row_width; i++)
 84.3774 +                  {
 84.3775 +                     value = (*sp >> shift) & 0x03;
 84.3776 +                     *dp = (png_byte)(value | (value << 2) | (value << 4) |
 84.3777 +                        (value << 6));
 84.3778 +                     if (shift == 6)
 84.3779 +                     {
 84.3780 +                        shift = 0;
 84.3781 +                        sp--;
 84.3782 +                     }
 84.3783 +                     else
 84.3784 +                        shift += 2;
 84.3785 +
 84.3786 +                     dp--;
 84.3787 +                  }
 84.3788 +                  break;
 84.3789 +               }
 84.3790 +               case 4:
 84.3791 +               {
 84.3792 +                  gray = (png_uint_16)((gray&0x0f)*0x11);
 84.3793 +                  sp = row + (png_size_t)((row_width - 1) >> 1);
 84.3794 +                  dp = row + (png_size_t)row_width - 1;
 84.3795 +                  shift = (int)((1 - ((row_width + 1) & 0x01)) << 2);
 84.3796 +                  for (i = 0; i < row_width; i++)
 84.3797 +                  {
 84.3798 +                     value = (*sp >> shift) & 0x0f;
 84.3799 +                     *dp = (png_byte)(value | (value << 4));
 84.3800 +                     if (shift == 4)
 84.3801 +                     {
 84.3802 +                        shift = 0;
 84.3803 +                        sp--;
 84.3804 +                     }
 84.3805 +                     else
 84.3806 +                        shift = 4;
 84.3807 +
 84.3808 +                     dp--;
 84.3809 +                  }
 84.3810 +                  break;
 84.3811 +               }
 84.3812 +            }
 84.3813 +            row_info->bit_depth = 8;
 84.3814 +            row_info->pixel_depth = 8;
 84.3815 +            row_info->rowbytes = row_width;
 84.3816 +         }
 84.3817 +
 84.3818 +         if (trans_value != NULL)
 84.3819 +         {
 84.3820 +            if (row_info->bit_depth == 8)
 84.3821 +            {
 84.3822 +               gray = gray & 0xff;
 84.3823 +               sp = row + (png_size_t)row_width - 1;
 84.3824 +               dp = row + (png_size_t)(row_width << 1) - 1;
 84.3825 +               for (i = 0; i < row_width; i++)
 84.3826 +               {
 84.3827 +                  if (*sp == gray)
 84.3828 +                     *dp-- = 0;
 84.3829 +                  else
 84.3830 +                     *dp-- = 0xff;
 84.3831 +                  *dp-- = *sp--;
 84.3832 +               }
 84.3833 +            }
 84.3834 +            else if (row_info->bit_depth == 16)
 84.3835 +            {
 84.3836 +               png_byte gray_high = (gray >> 8) & 0xff;
 84.3837 +               png_byte gray_low = gray & 0xff;
 84.3838 +               sp = row + row_info->rowbytes - 1;
 84.3839 +               dp = row + (row_info->rowbytes << 1) - 1;
 84.3840 +               for (i = 0; i < row_width; i++)
 84.3841 +               {
 84.3842 +                  if (*(sp - 1) == gray_high && *(sp) == gray_low) 
 84.3843 +                  {
 84.3844 +                     *dp-- = 0;
 84.3845 +                     *dp-- = 0;
 84.3846 +                  }
 84.3847 +                  else
 84.3848 +                  {
 84.3849 +                     *dp-- = 0xff;
 84.3850 +                     *dp-- = 0xff;
 84.3851 +                  }
 84.3852 +                  *dp-- = *sp--;
 84.3853 +                  *dp-- = *sp--;
 84.3854 +               }
 84.3855 +            }
 84.3856 +            row_info->color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
 84.3857 +            row_info->channels = 2;
 84.3858 +            row_info->pixel_depth = (png_byte)(row_info->bit_depth << 1);
 84.3859 +            row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,
 84.3860 +               row_width);
 84.3861 +         }
 84.3862 +      }
 84.3863 +      else if (row_info->color_type == PNG_COLOR_TYPE_RGB && trans_value)
 84.3864 +      {
 84.3865 +         if (row_info->bit_depth == 8)
 84.3866 +         {
 84.3867 +            png_byte red = trans_value->red & 0xff;
 84.3868 +            png_byte green = trans_value->green & 0xff;
 84.3869 +            png_byte blue = trans_value->blue & 0xff;
 84.3870 +            sp = row + (png_size_t)row_info->rowbytes - 1;
 84.3871 +            dp = row + (png_size_t)(row_width << 2) - 1;
 84.3872 +            for (i = 0; i < row_width; i++)
 84.3873 +            {
 84.3874 +               if (*(sp - 2) == red && *(sp - 1) == green && *(sp) == blue)
 84.3875 +                  *dp-- = 0;
 84.3876 +               else
 84.3877 +                  *dp-- = 0xff;
 84.3878 +               *dp-- = *sp--;
 84.3879 +               *dp-- = *sp--;
 84.3880 +               *dp-- = *sp--;
 84.3881 +            }
 84.3882 +         }
 84.3883 +         else if (row_info->bit_depth == 16)
 84.3884 +         {
 84.3885 +            png_byte red_high = (trans_value->red >> 8) & 0xff;
 84.3886 +            png_byte green_high = (trans_value->green >> 8) & 0xff;
 84.3887 +            png_byte blue_high = (trans_value->blue >> 8) & 0xff;
 84.3888 +            png_byte red_low = trans_value->red & 0xff;
 84.3889 +            png_byte green_low = trans_value->green & 0xff;
 84.3890 +            png_byte blue_low = trans_value->blue & 0xff;
 84.3891 +            sp = row + row_info->rowbytes - 1;
 84.3892 +            dp = row + (png_size_t)(row_width << 3) - 1;
 84.3893 +            for (i = 0; i < row_width; i++)
 84.3894 +            {
 84.3895 +               if (*(sp - 5) == red_high &&
 84.3896 +                  *(sp - 4) == red_low &&
 84.3897 +                  *(sp - 3) == green_high &&
 84.3898 +                  *(sp - 2) == green_low &&
 84.3899 +                  *(sp - 1) == blue_high &&
 84.3900 +                  *(sp    ) == blue_low)
 84.3901 +               {
 84.3902 +                  *dp-- = 0;
 84.3903 +                  *dp-- = 0;
 84.3904 +               }
 84.3905 +               else
 84.3906 +               {
 84.3907 +                  *dp-- = 0xff;
 84.3908 +                  *dp-- = 0xff;
 84.3909 +               }
 84.3910 +               *dp-- = *sp--;
 84.3911 +               *dp-- = *sp--;
 84.3912 +               *dp-- = *sp--;
 84.3913 +               *dp-- = *sp--;
 84.3914 +               *dp-- = *sp--;
 84.3915 +               *dp-- = *sp--;
 84.3916 +            }
 84.3917 +         }
 84.3918 +         row_info->color_type = PNG_COLOR_TYPE_RGB_ALPHA;
 84.3919 +         row_info->channels = 4;
 84.3920 +         row_info->pixel_depth = (png_byte)(row_info->bit_depth << 2);
 84.3921 +         row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
 84.3922 +      }
 84.3923 +   }
 84.3924 +}
 84.3925 +#endif
 84.3926 +
 84.3927 +#if defined(PNG_READ_DITHER_SUPPORTED)
 84.3928 +void /* PRIVATE */
 84.3929 +png_do_dither(png_row_infop row_info, png_bytep row,
 84.3930 +    png_bytep palette_lookup, png_bytep dither_lookup)
 84.3931 +{
 84.3932 +   png_bytep sp, dp;
 84.3933 +   png_uint_32 i;
 84.3934 +   png_uint_32 row_width=row_info->width;
 84.3935 +
 84.3936 +   png_debug(1, "in png_do_dither\n");
 84.3937 +#if defined(PNG_USELESS_TESTS_SUPPORTED)
 84.3938 +   if (row != NULL && row_info != NULL)
 84.3939 +#endif
 84.3940 +   {
 84.3941 +      if (row_info->color_type == PNG_COLOR_TYPE_RGB &&
 84.3942 +         palette_lookup && row_info->bit_depth == 8)
 84.3943 +      {
 84.3944 +         int r, g, b, p;
 84.3945 +         sp = row;
 84.3946 +         dp = row;
 84.3947 +         for (i = 0; i < row_width; i++)
 84.3948 +         {
 84.3949 +            r = *sp++;
 84.3950 +            g = *sp++;
 84.3951 +            b = *sp++;
 84.3952 +
 84.3953 +            /* this looks real messy, but the compiler will reduce
 84.3954 +               it down to a reasonable formula.  For example, with
 84.3955 +               5 bits per color, we get:
 84.3956 +               p = (((r >> 3) & 0x1f) << 10) |
 84.3957 +                  (((g >> 3) & 0x1f) << 5) |
 84.3958 +                  ((b >> 3) & 0x1f);
 84.3959 +               */
 84.3960 +            p = (((r >> (8 - PNG_DITHER_RED_BITS)) &
 84.3961 +               ((1 << PNG_DITHER_RED_BITS) - 1)) <<
 84.3962 +               (PNG_DITHER_GREEN_BITS + PNG_DITHER_BLUE_BITS)) |
 84.3963 +               (((g >> (8 - PNG_DITHER_GREEN_BITS)) &
 84.3964 +               ((1 << PNG_DITHER_GREEN_BITS) - 1)) <<
 84.3965 +               (PNG_DITHER_BLUE_BITS)) |
 84.3966 +               ((b >> (8 - PNG_DITHER_BLUE_BITS)) &
 84.3967 +               ((1 << PNG_DITHER_BLUE_BITS) - 1));
 84.3968 +
 84.3969 +            *dp++ = palette_lookup[p];
 84.3970 +         }
 84.3971 +         row_info->color_type = PNG_COLOR_TYPE_PALETTE;
 84.3972 +         row_info->channels = 1;
 84.3973 +         row_info->pixel_depth = row_info->bit_depth;
 84.3974 +         row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
 84.3975 +      }
 84.3976 +      else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA &&
 84.3977 +         palette_lookup != NULL && row_info->bit_depth == 8)
 84.3978 +      {
 84.3979 +         int r, g, b, p;
 84.3980 +         sp = row;
 84.3981 +         dp = row;
 84.3982 +         for (i = 0; i < row_width; i++)
 84.3983 +         {
 84.3984 +            r = *sp++;
 84.3985 +            g = *sp++;
 84.3986 +            b = *sp++;
 84.3987 +            sp++;
 84.3988 +
 84.3989 +            p = (((r >> (8 - PNG_DITHER_RED_BITS)) &
 84.3990 +               ((1 << PNG_DITHER_RED_BITS) - 1)) <<
 84.3991 +               (PNG_DITHER_GREEN_BITS + PNG_DITHER_BLUE_BITS)) |
 84.3992 +               (((g >> (8 - PNG_DITHER_GREEN_BITS)) &
 84.3993 +               ((1 << PNG_DITHER_GREEN_BITS) - 1)) <<
 84.3994 +               (PNG_DITHER_BLUE_BITS)) |
 84.3995 +               ((b >> (8 - PNG_DITHER_BLUE_BITS)) &
 84.3996 +               ((1 << PNG_DITHER_BLUE_BITS) - 1));
 84.3997 +
 84.3998 +            *dp++ = palette_lookup[p];
 84.3999 +         }
 84.4000 +         row_info->color_type = PNG_COLOR_TYPE_PALETTE;
 84.4001 +         row_info->channels = 1;
 84.4002 +         row_info->pixel_depth = row_info->bit_depth;
 84.4003 +         row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
 84.4004 +      }
 84.4005 +      else if (row_info->color_type == PNG_COLOR_TYPE_PALETTE &&
 84.4006 +         dither_lookup && row_info->bit_depth == 8)
 84.4007 +      {
 84.4008 +         sp = row;
 84.4009 +         for (i = 0; i < row_width; i++, sp++)
 84.4010 +         {
 84.4011 +            *sp = dither_lookup[*sp];
 84.4012 +         }
 84.4013 +      }
 84.4014 +   }
 84.4015 +}
 84.4016 +#endif
 84.4017 +
 84.4018 +#ifdef PNG_FLOATING_POINT_SUPPORTED
 84.4019 +#if defined(PNG_READ_GAMMA_SUPPORTED)
 84.4020 +static PNG_CONST int png_gamma_shift[] =
 84.4021 +   {0x10, 0x21, 0x42, 0x84, 0x110, 0x248, 0x550, 0xff0, 0x00};
 84.4022 +
 84.4023 +/* We build the 8- or 16-bit gamma tables here.  Note that for 16-bit
 84.4024 + * tables, we don't make a full table if we are reducing to 8-bit in
 84.4025 + * the future.  Note also how the gamma_16 tables are segmented so that
 84.4026 + * we don't need to allocate > 64K chunks for a full 16-bit table.
 84.4027 + */
 84.4028 +void /* PRIVATE */
 84.4029 +png_build_gamma_table(png_structp png_ptr)
 84.4030 +{
 84.4031 +  png_debug(1, "in png_build_gamma_table\n");
 84.4032 +
 84.4033 +  if (png_ptr->bit_depth <= 8)
 84.4034 +  {
 84.4035 +     int i;
 84.4036 +     double g;
 84.4037 +
 84.4038 +     if (png_ptr->screen_gamma > .000001)
 84.4039 +        g = 1.0 / (png_ptr->gamma * png_ptr->screen_gamma);
 84.4040 +     else
 84.4041 +        g = 1.0;
 84.4042 +
 84.4043 +     png_ptr->gamma_table = (png_bytep)png_malloc(png_ptr,
 84.4044 +        (png_uint_32)256);
 84.4045 +
 84.4046 +     for (i = 0; i < 256; i++)
 84.4047 +     {
 84.4048 +        png_ptr->gamma_table[i] = (png_byte)(pow((double)i / 255.0,
 84.4049 +           g) * 255.0 + .5);
 84.4050 +     }
 84.4051 +
 84.4052 +#if defined(PNG_READ_BACKGROUND_SUPPORTED) || \
 84.4053 +   defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
 84.4054 +     if (png_ptr->transformations & ((PNG_BACKGROUND) | PNG_RGB_TO_GRAY))
 84.4055 +     {
 84.4056 +
 84.4057 +        g = 1.0 / (png_ptr->gamma);
 84.4058 +
 84.4059 +        png_ptr->gamma_to_1 = (png_bytep)png_malloc(png_ptr,
 84.4060 +           (png_uint_32)256);
 84.4061 +
 84.4062 +        for (i = 0; i < 256; i++)
 84.4063 +        {
 84.4064 +           png_ptr->gamma_to_1[i] = (png_byte)(pow((double)i / 255.0,
 84.4065 +              g) * 255.0 + .5);
 84.4066 +        }
 84.4067 +
 84.4068 +
 84.4069 +        png_ptr->gamma_from_1 = (png_bytep)png_malloc(png_ptr,
 84.4070 +           (png_uint_32)256);
 84.4071 +
 84.4072 +        if (png_ptr->screen_gamma > 0.000001)
 84.4073 +           g = 1.0 / png_ptr->screen_gamma;
 84.4074 +        else
 84.4075 +           g = png_ptr->gamma;   /* probably doing rgb_to_gray */
 84.4076 +
 84.4077 +        for (i = 0; i < 256; i++)
 84.4078 +        {
 84.4079 +           png_ptr->gamma_from_1[i] = (png_byte)(pow((double)i / 255.0,
 84.4080 +              g) * 255.0 + .5);
 84.4081 +
 84.4082 +        }
 84.4083 +     }
 84.4084 +#endif /* PNG_READ_BACKGROUND_SUPPORTED || PNG_RGB_TO_GRAY_SUPPORTED */
 84.4085 +  }
 84.4086 +  else
 84.4087 +  {
 84.4088 +     double g;
 84.4089 +     int i, j, shift, num;
 84.4090 +     int sig_bit;
 84.4091 +     png_uint_32 ig;
 84.4092 +
 84.4093 +     if (png_ptr->color_type & PNG_COLOR_MASK_COLOR)
 84.4094 +     {
 84.4095 +        sig_bit = (int)png_ptr->sig_bit.red;
 84.4096 +        if ((int)png_ptr->sig_bit.green > sig_bit)
 84.4097 +           sig_bit = png_ptr->sig_bit.green;
 84.4098 +        if ((int)png_ptr->sig_bit.blue > sig_bit)
 84.4099 +           sig_bit = png_ptr->sig_bit.blue;
 84.4100 +     }
 84.4101 +     else
 84.4102 +     {
 84.4103 +        sig_bit = (int)png_ptr->sig_bit.gray;
 84.4104 +     }
 84.4105 +
 84.4106 +     if (sig_bit > 0)
 84.4107 +        shift = 16 - sig_bit;
 84.4108 +     else
 84.4109 +        shift = 0;
 84.4110 +
 84.4111 +     if (png_ptr->transformations & PNG_16_TO_8)
 84.4112 +     {
 84.4113 +        if (shift < (16 - PNG_MAX_GAMMA_8))
 84.4114 +           shift = (16 - PNG_MAX_GAMMA_8);
 84.4115 +     }
 84.4116 +
 84.4117 +     if (shift > 8)
 84.4118 +        shift = 8;
 84.4119 +     if (shift < 0)
 84.4120 +        shift = 0;
 84.4121 +
 84.4122 +     png_ptr->gamma_shift = (png_byte)shift;
 84.4123 +
 84.4124 +     num = (1 << (8 - shift));
 84.4125 +
 84.4126 +     if (png_ptr->screen_gamma > .000001)
 84.4127 +        g = 1.0 / (png_ptr->gamma * png_ptr->screen_gamma);
 84.4128 +     else
 84.4129 +        g = 1.0;
 84.4130 +
 84.4131 +     png_ptr->gamma_16_table = (png_uint_16pp)png_malloc(png_ptr,
 84.4132 +        (png_uint_32)(num * png_sizeof(png_uint_16p)));
 84.4133 +
 84.4134 +     if (png_ptr->transformations & (PNG_16_TO_8 | PNG_BACKGROUND))
 84.4135 +     {
 84.4136 +        double fin, fout;
 84.4137 +        png_uint_32 last, max;
 84.4138 +
 84.4139 +        for (i = 0; i < num; i++)
 84.4140 +        {
 84.4141 +           png_ptr->gamma_16_table[i] = (png_uint_16p)png_malloc(png_ptr,
 84.4142 +              (png_uint_32)(256 * png_sizeof(png_uint_16)));
 84.4143 +        }
 84.4144 +
 84.4145 +        g = 1.0 / g;
 84.4146 +        last = 0;
 84.4147 +        for (i = 0; i < 256; i++)
 84.4148 +        {
 84.4149 +           fout = ((double)i + 0.5) / 256.0;
 84.4150 +           fin = pow(fout, g);
 84.4151 +           max = (png_uint_32)(fin * (double)((png_uint_32)num << 8));
 84.4152 +           while (last <= max)
 84.4153 +           {
 84.4154 +              png_ptr->gamma_16_table[(int)(last & (0xff >> shift))]
 84.4155 +                 [(int)(last >> (8 - shift))] = (png_uint_16)(
 84.4156 +                 (png_uint_16)i | ((png_uint_16)i << 8));
 84.4157 +              last++;
 84.4158 +           }
 84.4159 +        }
 84.4160 +        while (last < ((png_uint_32)num << 8))
 84.4161 +        {
 84.4162 +           png_ptr->gamma_16_table[(int)(last & (0xff >> shift))]
 84.4163 +              [(int)(last >> (8 - shift))] = (png_uint_16)65535L;
 84.4164 +           last++;
 84.4165 +        }
 84.4166 +     }
 84.4167 +     else
 84.4168 +     {
 84.4169 +        for (i = 0; i < num; i++)
 84.4170 +        {
 84.4171 +           png_ptr->gamma_16_table[i] = (png_uint_16p)png_malloc(png_ptr,
 84.4172 +              (png_uint_32)(256 * png_sizeof(png_uint_16)));
 84.4173 +
 84.4174 +           ig = (((png_uint_32)i * (png_uint_32)png_gamma_shift[shift]) >> 4);
 84.4175 +           for (j = 0; j < 256; j++)
 84.4176 +           {
 84.4177 +              png_ptr->gamma_16_table[i][j] =
 84.4178 +                 (png_uint_16)(pow((double)(ig + ((png_uint_32)j << 8)) /
 84.4179 +                    65535.0, g) * 65535.0 + .5);
 84.4180 +           }
 84.4181 +        }
 84.4182 +     }
 84.4183 +
 84.4184 +#if defined(PNG_READ_BACKGROUND_SUPPORTED) || \
 84.4185 +   defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
 84.4186 +     if (png_ptr->transformations & (PNG_BACKGROUND | PNG_RGB_TO_GRAY))
 84.4187 +     {
 84.4188 +
 84.4189 +        g = 1.0 / (png_ptr->gamma);
 84.4190 +
 84.4191 +        png_ptr->gamma_16_to_1 = (png_uint_16pp)png_malloc(png_ptr,
 84.4192 +           (png_uint_32)(num * png_sizeof(png_uint_16p )));
 84.4193 +
 84.4194 +        for (i = 0; i < num; i++)
 84.4195 +        {
 84.4196 +           png_ptr->gamma_16_to_1[i] = (png_uint_16p)png_malloc(png_ptr,
 84.4197 +              (png_uint_32)(256 * png_sizeof(png_uint_16)));
 84.4198 +
 84.4199 +           ig = (((png_uint_32)i *
 84.4200 +              (png_uint_32)png_gamma_shift[shift]) >> 4);
 84.4201 +           for (j = 0; j < 256; j++)
 84.4202 +           {
 84.4203 +              png_ptr->gamma_16_to_1[i][j] =
 84.4204 +                 (png_uint_16)(pow((double)(ig + ((png_uint_32)j << 8)) /
 84.4205 +                    65535.0, g) * 65535.0 + .5);
 84.4206 +           }
 84.4207 +        }
 84.4208 +
 84.4209 +        if (png_ptr->screen_gamma > 0.000001)
 84.4210 +           g = 1.0 / png_ptr->screen_gamma;
 84.4211 +        else
 84.4212 +           g = png_ptr->gamma;   /* probably doing rgb_to_gray */
 84.4213 +
 84.4214 +        png_ptr->gamma_16_from_1 = (png_uint_16pp)png_malloc(png_ptr,
 84.4215 +           (png_uint_32)(num * png_sizeof(png_uint_16p)));
 84.4216 +
 84.4217 +        for (i = 0; i < num; i++)
 84.4218 +        {
 84.4219 +           png_ptr->gamma_16_from_1[i] = (png_uint_16p)png_malloc(png_ptr,
 84.4220 +              (png_uint_32)(256 * png_sizeof(png_uint_16)));
 84.4221 +
 84.4222 +           ig = (((png_uint_32)i *
 84.4223 +              (png_uint_32)png_gamma_shift[shift]) >> 4);
 84.4224 +           for (j = 0; j < 256; j++)
 84.4225 +           {
 84.4226 +              png_ptr->gamma_16_from_1[i][j] =
 84.4227 +                 (png_uint_16)(pow((double)(ig + ((png_uint_32)j << 8)) /
 84.4228 +                    65535.0, g) * 65535.0 + .5);
 84.4229 +           }
 84.4230 +        }
 84.4231 +     }
 84.4232 +#endif /* PNG_READ_BACKGROUND_SUPPORTED || PNG_RGB_TO_GRAY_SUPPORTED */
 84.4233 +  }
 84.4234 +}
 84.4235 +#endif
 84.4236 +/* To do: install integer version of png_build_gamma_table here */
 84.4237 +#endif
 84.4238 +
 84.4239 +#if defined(PNG_MNG_FEATURES_SUPPORTED)
 84.4240 +/* undoes intrapixel differencing  */
 84.4241 +void /* PRIVATE */
 84.4242 +png_do_read_intrapixel(png_row_infop row_info, png_bytep row)
 84.4243 +{
 84.4244 +   png_debug(1, "in png_do_read_intrapixel\n");
 84.4245 +   if (
 84.4246 +#if defined(PNG_USELESS_TESTS_SUPPORTED)
 84.4247 +       row != NULL && row_info != NULL &&
 84.4248 +#endif
 84.4249 +       (row_info->color_type & PNG_COLOR_MASK_COLOR))
 84.4250 +   {
 84.4251 +      int bytes_per_pixel;
 84.4252 +      png_uint_32 row_width = row_info->width;
 84.4253 +      if (row_info->bit_depth == 8)
 84.4254 +      {
 84.4255 +         png_bytep rp;
 84.4256 +         png_uint_32 i;
 84.4257 +
 84.4258 +         if (row_info->color_type == PNG_COLOR_TYPE_RGB)
 84.4259 +            bytes_per_pixel = 3;
 84.4260 +         else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
 84.4261 +            bytes_per_pixel = 4;
 84.4262 +         else
 84.4263 +            return;
 84.4264 +
 84.4265 +         for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
 84.4266 +         {
 84.4267 +            *(rp) = (png_byte)((256 + *rp + *(rp+1))&0xff);
 84.4268 +            *(rp+2) = (png_byte)((256 + *(rp+2) + *(rp+1))&0xff);
 84.4269 +         }
 84.4270 +      }
 84.4271 +      else if (row_info->bit_depth == 16)
 84.4272 +      {
 84.4273 +         png_bytep rp;
 84.4274 +         png_uint_32 i;
 84.4275 +
 84.4276 +         if (row_info->color_type == PNG_COLOR_TYPE_RGB)
 84.4277 +            bytes_per_pixel = 6;
 84.4278 +         else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
 84.4279 +            bytes_per_pixel = 8;
 84.4280 +         else
 84.4281 +            return;
 84.4282 +
 84.4283 +         for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
 84.4284 +         {
 84.4285 +            png_uint_32 s0   = (*(rp    ) << 8) | *(rp + 1);
 84.4286 +            png_uint_32 s1   = (*(rp + 2) << 8) | *(rp + 3);
 84.4287 +            png_uint_32 s2   = (*(rp + 4) << 8) | *(rp + 5);
 84.4288 +            png_uint_32 red  = (png_uint_32)((s0 + s1 + 65536L) & 0xffffL);
 84.4289 +            png_uint_32 blue = (png_uint_32)((s2 + s1 + 65536L) & 0xffffL);
 84.4290 +            *(rp  ) = (png_byte)((red >> 8) & 0xff);
 84.4291 +            *(rp+1) = (png_byte)(red & 0xff);
 84.4292 +            *(rp+4) = (png_byte)((blue >> 8) & 0xff);
 84.4293 +            *(rp+5) = (png_byte)(blue & 0xff);
 84.4294 +         }
 84.4295 +      }
 84.4296 +   }
 84.4297 +}
 84.4298 +#endif /* PNG_MNG_FEATURES_SUPPORTED */
 84.4299 +#endif /* PNG_READ_SUPPORTED */
    85.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    85.2 +++ b/libs/libpng/pngrutil.c	Sat Sep 19 05:51:51 2015 +0300
    85.3 @@ -0,0 +1,3234 @@
    85.4 +
    85.5 +/* pngrutil.c - utilities to read a PNG file
    85.6 + *
    85.7 + * Last changed in libpng 1.2.33 [October 31, 2008]
    85.8 + * For conditions of distribution and use, see copyright notice in png.h
    85.9 + * Copyright (c) 1998-2008 Glenn Randers-Pehrson
   85.10 + * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
   85.11 + * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
   85.12 + *
   85.13 + * This file contains routines that are only called from within
   85.14 + * libpng itself during the course of reading an image.
   85.15 + */
   85.16 +
   85.17 +#define PNG_INTERNAL
   85.18 +#include "png.h"
   85.19 +#if defined(PNG_READ_SUPPORTED)
   85.20 +
   85.21 +#if defined(_WIN32_WCE) && (_WIN32_WCE<0x500)
   85.22 +#  define WIN32_WCE_OLD
   85.23 +#endif
   85.24 +
   85.25 +#ifdef PNG_FLOATING_POINT_SUPPORTED
   85.26 +#  if defined(WIN32_WCE_OLD)
   85.27 +/* strtod() function is not supported on WindowsCE */
   85.28 +__inline double png_strtod(png_structp png_ptr, PNG_CONST char *nptr, char **endptr)
   85.29 +{
   85.30 +   double result = 0;
   85.31 +   int len;
   85.32 +   wchar_t *str, *end;
   85.33 +
   85.34 +   len = MultiByteToWideChar(CP_ACP, 0, nptr, -1, NULL, 0);
   85.35 +   str = (wchar_t *)png_malloc(png_ptr, len * png_sizeof(wchar_t));
   85.36 +   if ( NULL != str )
   85.37 +   {
   85.38 +      MultiByteToWideChar(CP_ACP, 0, nptr, -1, str, len);
   85.39 +      result = wcstod(str, &end);
   85.40 +      len = WideCharToMultiByte(CP_ACP, 0, end, -1, NULL, 0, NULL, NULL);
   85.41 +      *endptr = (char *)nptr + (png_strlen(nptr) - len + 1);
   85.42 +      png_free(png_ptr, str);
   85.43 +   }
   85.44 +   return result;
   85.45 +}
   85.46 +#  else
   85.47 +#    define png_strtod(p,a,b) strtod(a,b)
   85.48 +#  endif
   85.49 +#endif
   85.50 +
   85.51 +png_uint_32 PNGAPI
   85.52 +png_get_uint_31(png_structp png_ptr, png_bytep buf)
   85.53 +{
   85.54 +#ifdef PNG_READ_BIG_ENDIAN_SUPPORTED
   85.55 +   png_uint_32 i = png_get_uint_32(buf);
   85.56 +#else
   85.57 +   /* Avoid an extra function call by inlining the result. */
   85.58 +   png_uint_32 i = ((png_uint_32)(*buf) << 24) +
   85.59 +      ((png_uint_32)(*(buf + 1)) << 16) +
   85.60 +      ((png_uint_32)(*(buf + 2)) << 8) +
   85.61 +      (png_uint_32)(*(buf + 3));
   85.62 +#endif
   85.63 +   if (i > PNG_UINT_31_MAX)
   85.64 +     png_error(png_ptr, "PNG unsigned integer out of range.");
   85.65 +   return (i);
   85.66 +}
   85.67 +#ifndef PNG_READ_BIG_ENDIAN_SUPPORTED
   85.68 +/* Grab an unsigned 32-bit integer from a buffer in big-endian format. */
   85.69 +png_uint_32 PNGAPI
   85.70 +png_get_uint_32(png_bytep buf)
   85.71 +{
   85.72 +   png_uint_32 i = ((png_uint_32)(*buf) << 24) +
   85.73 +      ((png_uint_32)(*(buf + 1)) << 16) +
   85.74 +      ((png_uint_32)(*(buf + 2)) << 8) +
   85.75 +      (png_uint_32)(*(buf + 3));
   85.76 +
   85.77 +   return (i);
   85.78 +}
   85.79 +
   85.80 +/* Grab a signed 32-bit integer from a buffer in big-endian format.  The
   85.81 + * data is stored in the PNG file in two's complement format, and it is
   85.82 + * assumed that the machine format for signed integers is the same. */
   85.83 +png_int_32 PNGAPI
   85.84 +png_get_int_32(png_bytep buf)
   85.85 +{
   85.86 +   png_int_32 i = ((png_int_32)(*buf) << 24) +
   85.87 +      ((png_int_32)(*(buf + 1)) << 16) +
   85.88 +      ((png_int_32)(*(buf + 2)) << 8) +
   85.89 +      (png_int_32)(*(buf + 3));
   85.90 +
   85.91 +   return (i);
   85.92 +}
   85.93 +
   85.94 +/* Grab an unsigned 16-bit integer from a buffer in big-endian format. */
   85.95 +png_uint_16 PNGAPI
   85.96 +png_get_uint_16(png_bytep buf)
   85.97 +{
   85.98 +   png_uint_16 i = (png_uint_16)(((png_uint_16)(*buf) << 8) +
   85.99 +      (png_uint_16)(*(buf + 1)));
  85.100 +
  85.101 +   return (i);
  85.102 +}
  85.103 +#endif /* PNG_READ_BIG_ENDIAN_SUPPORTED */
  85.104 +
  85.105 +/* Read the chunk header (length + type name).
  85.106 + * Put the type name into png_ptr->chunk_name, and return the length.
  85.107 + */
  85.108 +png_uint_32 /* PRIVATE */
  85.109 +png_read_chunk_header(png_structp png_ptr)
  85.110 +{
  85.111 +   png_byte buf[8];
  85.112 +   png_uint_32 length;
  85.113 +
  85.114 +   /* read the length and the chunk name */
  85.115 +   png_read_data(png_ptr, buf, 8);
  85.116 +   length = png_get_uint_31(png_ptr, buf);
  85.117 +
  85.118 +   /* put the chunk name into png_ptr->chunk_name */
  85.119 +   png_memcpy(png_ptr->chunk_name, buf + 4, 4);
  85.120 +
  85.121 +   png_debug2(0, "Reading %s chunk, length = %lu\n",
  85.122 +      png_ptr->chunk_name, length);
  85.123 +
  85.124 +   /* reset the crc and run it over the chunk name */
  85.125 +   png_reset_crc(png_ptr);
  85.126 +   png_calculate_crc(png_ptr, png_ptr->chunk_name, 4);
  85.127 +
  85.128 +   /* check to see if chunk name is valid */
  85.129 +   png_check_chunk_name(png_ptr, png_ptr->chunk_name);
  85.130 +
  85.131 +   return length;
  85.132 +}
  85.133 +
  85.134 +/* Read data, and (optionally) run it through the CRC. */
  85.135 +void /* PRIVATE */
  85.136 +png_crc_read(png_structp png_ptr, png_bytep buf, png_size_t length)
  85.137 +{
  85.138 +   if (png_ptr == NULL) return;
  85.139 +   png_read_data(png_ptr, buf, length);
  85.140 +   png_calculate_crc(png_ptr, buf, length);
  85.141 +}
  85.142 +
  85.143 +/* Optionally skip data and then check the CRC.  Depending on whether we
  85.144 +   are reading a ancillary or critical chunk, and how the program has set
  85.145 +   things up, we may calculate the CRC on the data and print a message.
  85.146 +   Returns '1' if there was a CRC error, '0' otherwise. */
  85.147 +int /* PRIVATE */
  85.148 +png_crc_finish(png_structp png_ptr, png_uint_32 skip)
  85.149 +{
  85.150 +   png_size_t i;
  85.151 +   png_size_t istop = png_ptr->zbuf_size;
  85.152 +
  85.153 +   for (i = (png_size_t)skip; i > istop; i -= istop)
  85.154 +   {
  85.155 +      png_crc_read(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
  85.156 +   }
  85.157 +   if (i)
  85.158 +   {
  85.159 +      png_crc_read(png_ptr, png_ptr->zbuf, i);
  85.160 +   }
  85.161 +
  85.162 +   if (png_crc_error(png_ptr))
  85.163 +   {
  85.164 +      if (((png_ptr->chunk_name[0] & 0x20) &&                /* Ancillary */
  85.165 +           !(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN)) ||
  85.166 +          (!(png_ptr->chunk_name[0] & 0x20) &&             /* Critical  */
  85.167 +          (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_USE)))
  85.168 +      {
  85.169 +         png_chunk_warning(png_ptr, "CRC error");
  85.170 +      }
  85.171 +      else
  85.172 +      {
  85.173 +         png_chunk_error(png_ptr, "CRC error");
  85.174 +      }
  85.175 +      return (1);
  85.176 +   }
  85.177 +
  85.178 +   return (0);
  85.179 +}
  85.180 +
  85.181 +/* Compare the CRC stored in the PNG file with that calculated by libpng from
  85.182 +   the data it has read thus far. */
  85.183 +int /* PRIVATE */
  85.184 +png_crc_error(png_structp png_ptr)
  85.185 +{
  85.186 +   png_byte crc_bytes[4];
  85.187 +   png_uint_32 crc;
  85.188 +   int need_crc = 1;
  85.189 +
  85.190 +   if (png_ptr->chunk_name[0] & 0x20)                     /* ancillary */
  85.191 +   {
  85.192 +      if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_MASK) ==
  85.193 +          (PNG_FLAG_CRC_ANCILLARY_USE | PNG_FLAG_CRC_ANCILLARY_NOWARN))
  85.194 +         need_crc = 0;
  85.195 +   }
  85.196 +   else                                                    /* critical */
  85.197 +   {
  85.198 +      if (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_IGNORE)
  85.199 +         need_crc = 0;
  85.200 +   }
  85.201 +
  85.202 +   png_read_data(png_ptr, crc_bytes, 4);
  85.203 +
  85.204 +   if (need_crc)
  85.205 +   {
  85.206 +      crc = png_get_uint_32(crc_bytes);
  85.207 +      return ((int)(crc != png_ptr->crc));
  85.208 +   }
  85.209 +   else
  85.210 +      return (0);
  85.211 +}
  85.212 +
  85.213 +#if defined(PNG_READ_zTXt_SUPPORTED) || defined(PNG_READ_iTXt_SUPPORTED) || \
  85.214 +    defined(PNG_READ_iCCP_SUPPORTED)
  85.215 +/*
  85.216 + * Decompress trailing data in a chunk.  The assumption is that chunkdata
  85.217 + * points at an allocated area holding the contents of a chunk with a
  85.218 + * trailing compressed part.  What we get back is an allocated area
  85.219 + * holding the original prefix part and an uncompressed version of the
  85.220 + * trailing part (the malloc area passed in is freed).
  85.221 + */
  85.222 +void /* PRIVATE */
  85.223 +png_decompress_chunk(png_structp png_ptr, int comp_type,
  85.224 +                              png_size_t chunklength,
  85.225 +                              png_size_t prefix_size, png_size_t *newlength)
  85.226 +{
  85.227 +   static PNG_CONST char msg[] = "Error decoding compressed text";
  85.228 +   png_charp text;
  85.229 +   png_size_t text_size;
  85.230 +
  85.231 +   if (comp_type == PNG_COMPRESSION_TYPE_BASE)
  85.232 +   {
  85.233 +      int ret = Z_OK;
  85.234 +      png_ptr->zstream.next_in = (png_bytep)(png_ptr->chunkdata + prefix_size);
  85.235 +      png_ptr->zstream.avail_in = (uInt)(chunklength - prefix_size);
  85.236 +      png_ptr->zstream.next_out = png_ptr->zbuf;
  85.237 +      png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
  85.238 +
  85.239 +      text_size = 0;
  85.240 +      text = NULL;
  85.241 +
  85.242 +      while (png_ptr->zstream.avail_in)
  85.243 +      {
  85.244 +         ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);
  85.245 +         if (ret != Z_OK && ret != Z_STREAM_END)
  85.246 +         {
  85.247 +            if (png_ptr->zstream.msg != NULL)
  85.248 +               png_warning(png_ptr, png_ptr->zstream.msg);
  85.249 +            else
  85.250 +               png_warning(png_ptr, msg);
  85.251 +            inflateReset(&png_ptr->zstream);
  85.252 +            png_ptr->zstream.avail_in = 0;
  85.253 +
  85.254 +            if (text ==  NULL)
  85.255 +            {
  85.256 +               text_size = prefix_size + png_sizeof(msg) + 1;
  85.257 +               text = (png_charp)png_malloc_warn(png_ptr, text_size);
  85.258 +               if (text ==  NULL)
  85.259 +                 {
  85.260 +                    png_free(png_ptr, png_ptr->chunkdata);
  85.261 +                    png_ptr->chunkdata = NULL;
  85.262 +                    png_error(png_ptr, "Not enough memory to decompress chunk");
  85.263 +                 }
  85.264 +               png_memcpy(text, png_ptr->chunkdata, prefix_size);
  85.265 +            }
  85.266 +
  85.267 +            text[text_size - 1] = 0x00;
  85.268 +
  85.269 +            /* Copy what we can of the error message into the text chunk */
  85.270 +            text_size = (png_size_t)(chunklength -
  85.271 +              (text - png_ptr->chunkdata) - 1);
  85.272 +            if (text_size > png_sizeof(msg))
  85.273 +               text_size = png_sizeof(msg);
  85.274 +            png_memcpy(text + prefix_size, msg, text_size);
  85.275 +            break;
  85.276 +         }
  85.277 +         if (!png_ptr->zstream.avail_out || ret == Z_STREAM_END)
  85.278 +         {
  85.279 +            if (text == NULL)
  85.280 +            {
  85.281 +               text_size = prefix_size +
  85.282 +                   png_ptr->zbuf_size - png_ptr->zstream.avail_out;
  85.283 +               text = (png_charp)png_malloc_warn(png_ptr, text_size + 1);
  85.284 +               if (text ==  NULL)
  85.285 +               {
  85.286 +                  png_free(png_ptr, png_ptr->chunkdata);
  85.287 +                  png_ptr->chunkdata = NULL;
  85.288 +                  png_error(png_ptr,
  85.289 +                    "Not enough memory to decompress chunk.");
  85.290 +               }
  85.291 +               png_memcpy(text + prefix_size, png_ptr->zbuf,
  85.292 +                    text_size - prefix_size);
  85.293 +               png_memcpy(text, png_ptr->chunkdata, prefix_size);
  85.294 +               *(text + text_size) = 0x00;
  85.295 +            }
  85.296 +            else
  85.297 +            {
  85.298 +               png_charp tmp;
  85.299 +
  85.300 +               tmp = text;
  85.301 +               text = (png_charp)png_malloc_warn(png_ptr,
  85.302 +                  (png_uint_32)(text_size +
  85.303 +                  png_ptr->zbuf_size - png_ptr->zstream.avail_out + 1));
  85.304 +               if (text == NULL)
  85.305 +               {
  85.306 +                  png_free(png_ptr, tmp);
  85.307 +                  png_free(png_ptr, png_ptr->chunkdata);
  85.308 +                  png_ptr->chunkdata = NULL;
  85.309 +                  png_error(png_ptr,
  85.310 +                    "Not enough memory to decompress chunk..");
  85.311 +               }
  85.312 +               png_memcpy(text, tmp, text_size);
  85.313 +               png_free(png_ptr, tmp);
  85.314 +               png_memcpy(text + text_size, png_ptr->zbuf,
  85.315 +                  (png_ptr->zbuf_size - png_ptr->zstream.avail_out));
  85.316 +               text_size += png_ptr->zbuf_size - png_ptr->zstream.avail_out;
  85.317 +               *(text + text_size) = 0x00;
  85.318 +            }
  85.319 +            if (ret == Z_STREAM_END)
  85.320 +               break;
  85.321 +            else
  85.322 +            {
  85.323 +               png_ptr->zstream.next_out = png_ptr->zbuf;
  85.324 +               png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
  85.325 +            }
  85.326 +         }
  85.327 +      }
  85.328 +      if (ret != Z_STREAM_END)
  85.329 +      {
  85.330 +#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
  85.331 +         char umsg[52];
  85.332 +
  85.333 +         if (ret == Z_BUF_ERROR)
  85.334 +            png_snprintf(umsg, 52,
  85.335 +                "Buffer error in compressed datastream in %s chunk",
  85.336 +                png_ptr->chunk_name);
  85.337 +         else if (ret == Z_DATA_ERROR)
  85.338 +            png_snprintf(umsg, 52,
  85.339 +                "Data error in compressed datastream in %s chunk",
  85.340 +                png_ptr->chunk_name);
  85.341 +         else
  85.342 +            png_snprintf(umsg, 52,
  85.343 +                "Incomplete compressed datastream in %s chunk",
  85.344 +                png_ptr->chunk_name);
  85.345 +         png_warning(png_ptr, umsg);
  85.346 +#else
  85.347 +         png_warning(png_ptr,
  85.348 +            "Incomplete compressed datastream in chunk other than IDAT");
  85.349 +#endif
  85.350 +         text_size = prefix_size;
  85.351 +         if (text ==  NULL)
  85.352 +         {
  85.353 +            text = (png_charp)png_malloc_warn(png_ptr, text_size+1);
  85.354 +            if (text == NULL)
  85.355 +              {
  85.356 +                png_free(png_ptr, png_ptr->chunkdata);
  85.357 +                png_ptr->chunkdata = NULL;
  85.358 +                png_error(png_ptr, "Not enough memory for text.");
  85.359 +              }
  85.360 +            png_memcpy(text, png_ptr->chunkdata, prefix_size);
  85.361 +         }
  85.362 +         *(text + text_size) = 0x00;
  85.363 +      }
  85.364 +
  85.365 +      inflateReset(&png_ptr->zstream);
  85.366 +      png_ptr->zstream.avail_in = 0;
  85.367 +
  85.368 +      png_free(png_ptr, png_ptr->chunkdata);
  85.369 +      png_ptr->chunkdata = text;
  85.370 +      *newlength=text_size;
  85.371 +   }
  85.372 +   else /* if (comp_type != PNG_COMPRESSION_TYPE_BASE) */
  85.373 +   {
  85.374 +#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
  85.375 +      char umsg[50];
  85.376 +
  85.377 +      png_snprintf(umsg, 50, "Unknown zTXt compression type %d", comp_type);
  85.378 +      png_warning(png_ptr, umsg);
  85.379 +#else
  85.380 +      png_warning(png_ptr, "Unknown zTXt compression type");
  85.381 +#endif
  85.382 +
  85.383 +      *(png_ptr->chunkdata + prefix_size) = 0x00;
  85.384 +      *newlength = prefix_size;
  85.385 +   }
  85.386 +}
  85.387 +#endif
  85.388 +
  85.389 +/* read and check the IDHR chunk */
  85.390 +void /* PRIVATE */
  85.391 +png_handle_IHDR(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  85.392 +{
  85.393 +   png_byte buf[13];
  85.394 +   png_uint_32 width, height;
  85.395 +   int bit_depth, color_type, compression_type, filter_type;
  85.396 +   int interlace_type;
  85.397 +
  85.398 +   png_debug(1, "in png_handle_IHDR\n");
  85.399 +
  85.400 +   if (png_ptr->mode & PNG_HAVE_IHDR)
  85.401 +      png_error(png_ptr, "Out of place IHDR");
  85.402 +
  85.403 +   /* check the length */
  85.404 +   if (length != 13)
  85.405 +      png_error(png_ptr, "Invalid IHDR chunk");
  85.406 +
  85.407 +   png_ptr->mode |= PNG_HAVE_IHDR;
  85.408 +
  85.409 +   png_crc_read(png_ptr, buf, 13);
  85.410 +   png_crc_finish(png_ptr, 0);
  85.411 +
  85.412 +   width = png_get_uint_31(png_ptr, buf);
  85.413 +   height = png_get_uint_31(png_ptr, buf + 4);
  85.414 +   bit_depth = buf[8];
  85.415 +   color_type = buf[9];
  85.416 +   compression_type = buf[10];
  85.417 +   filter_type = buf[11];
  85.418 +   interlace_type = buf[12];
  85.419 +
  85.420 +   /* set internal variables */
  85.421 +   png_ptr->width = width;
  85.422 +   png_ptr->height = height;
  85.423 +   png_ptr->bit_depth = (png_byte)bit_depth;
  85.424 +   png_ptr->interlaced = (png_byte)interlace_type;
  85.425 +   png_ptr->color_type = (png_byte)color_type;
  85.426 +#if defined(PNG_MNG_FEATURES_SUPPORTED)
  85.427 +   png_ptr->filter_type = (png_byte)filter_type;
  85.428 +#endif
  85.429 +   png_ptr->compression_type = (png_byte)compression_type;
  85.430 +
  85.431 +   /* find number of channels */
  85.432 +   switch (png_ptr->color_type)
  85.433 +   {
  85.434 +      case PNG_COLOR_TYPE_GRAY:
  85.435 +      case PNG_COLOR_TYPE_PALETTE:
  85.436 +         png_ptr->channels = 1;
  85.437 +         break;
  85.438 +      case PNG_COLOR_TYPE_RGB:
  85.439 +         png_ptr->channels = 3;
  85.440 +         break;
  85.441 +      case PNG_COLOR_TYPE_GRAY_ALPHA:
  85.442 +         png_ptr->channels = 2;
  85.443 +         break;
  85.444 +      case PNG_COLOR_TYPE_RGB_ALPHA:
  85.445 +         png_ptr->channels = 4;
  85.446 +         break;
  85.447 +   }
  85.448 +
  85.449 +   /* set up other useful info */
  85.450 +   png_ptr->pixel_depth = (png_byte)(png_ptr->bit_depth *
  85.451 +   png_ptr->channels);
  85.452 +   png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->width);
  85.453 +   png_debug1(3, "bit_depth = %d\n", png_ptr->bit_depth);
  85.454 +   png_debug1(3, "channels = %d\n", png_ptr->channels);
  85.455 +   png_debug1(3, "rowbytes = %lu\n", png_ptr->rowbytes);
  85.456 +   png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth,
  85.457 +      color_type, interlace_type, compression_type, filter_type);
  85.458 +}
  85.459 +
  85.460 +/* read and check the palette */
  85.461 +void /* PRIVATE */
  85.462 +png_handle_PLTE(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  85.463 +{
  85.464 +   png_color palette[PNG_MAX_PALETTE_LENGTH];
  85.465 +   int num, i;
  85.466 +#ifndef PNG_NO_POINTER_INDEXING
  85.467 +   png_colorp pal_ptr;
  85.468 +#endif
  85.469 +
  85.470 +   png_debug(1, "in png_handle_PLTE\n");
  85.471 +
  85.472 +   if (!(png_ptr->mode & PNG_HAVE_IHDR))
  85.473 +      png_error(png_ptr, "Missing IHDR before PLTE");
  85.474 +   else if (png_ptr->mode & PNG_HAVE_IDAT)
  85.475 +   {
  85.476 +      png_warning(png_ptr, "Invalid PLTE after IDAT");
  85.477 +      png_crc_finish(png_ptr, length);
  85.478 +      return;
  85.479 +   }
  85.480 +   else if (png_ptr->mode & PNG_HAVE_PLTE)
  85.481 +      png_error(png_ptr, "Duplicate PLTE chunk");
  85.482 +
  85.483 +   png_ptr->mode |= PNG_HAVE_PLTE;
  85.484 +
  85.485 +   if (!(png_ptr->color_type&PNG_COLOR_MASK_COLOR))
  85.486 +   {
  85.487 +      png_warning(png_ptr,
  85.488 +        "Ignoring PLTE chunk in grayscale PNG");
  85.489 +      png_crc_finish(png_ptr, length);
  85.490 +      return;
  85.491 +   }
  85.492 +#if !defined(PNG_READ_OPT_PLTE_SUPPORTED)
  85.493 +   if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE)
  85.494 +   {
  85.495 +      png_crc_finish(png_ptr, length);
  85.496 +      return;
  85.497 +   }
  85.498 +#endif
  85.499 +
  85.500 +   if (length > 3*PNG_MAX_PALETTE_LENGTH || length % 3)
  85.501 +   {
  85.502 +      if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE)
  85.503 +      {
  85.504 +         png_warning(png_ptr, "Invalid palette chunk");
  85.505 +         png_crc_finish(png_ptr, length);
  85.506 +         return;
  85.507 +      }
  85.508 +      else
  85.509 +      {
  85.510 +         png_error(png_ptr, "Invalid palette chunk");
  85.511 +      }
  85.512 +   }
  85.513 +
  85.514 +   num = (int)length / 3;
  85.515 +
  85.516 +#ifndef PNG_NO_POINTER_INDEXING
  85.517 +   for (i = 0, pal_ptr = palette; i < num; i++, pal_ptr++)
  85.518 +   {
  85.519 +      png_byte buf[3];
  85.520 +
  85.521 +      png_crc_read(png_ptr, buf, 3);
  85.522 +      pal_ptr->red = buf[0];
  85.523 +      pal_ptr->green = buf[1];
  85.524 +      pal_ptr->blue = buf[2];
  85.525 +   }
  85.526 +#else
  85.527 +   for (i = 0; i < num; i++)
  85.528 +   {
  85.529 +      png_byte buf[3];
  85.530 +
  85.531 +      png_crc_read(png_ptr, buf, 3);
  85.532 +      /* don't depend upon png_color being any order */
  85.533 +      palette[i].red = buf[0];
  85.534 +      palette[i].green = buf[1];
  85.535 +      palette[i].blue = buf[2];
  85.536 +   }
  85.537 +#endif
  85.538 +
  85.539 +   /* If we actually NEED the PLTE chunk (ie for a paletted image), we do
  85.540 +      whatever the normal CRC configuration tells us.  However, if we
  85.541 +      have an RGB image, the PLTE can be considered ancillary, so
  85.542 +      we will act as though it is. */
  85.543 +#if !defined(PNG_READ_OPT_PLTE_SUPPORTED)
  85.544 +   if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  85.545 +#endif
  85.546 +   {
  85.547 +      png_crc_finish(png_ptr, 0);
  85.548 +   }
  85.549 +#if !defined(PNG_READ_OPT_PLTE_SUPPORTED)
  85.550 +   else if (png_crc_error(png_ptr))  /* Only if we have a CRC error */
  85.551 +   {
  85.552 +      /* If we don't want to use the data from an ancillary chunk,
  85.553 +         we have two options: an error abort, or a warning and we
  85.554 +         ignore the data in this chunk (which should be OK, since
  85.555 +         it's considered ancillary for a RGB or RGBA image). */
  85.556 +      if (!(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_USE))
  85.557 +      {
  85.558 +         if (png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN)
  85.559 +         {
  85.560 +            png_chunk_error(png_ptr, "CRC error");
  85.561 +         }
  85.562 +         else
  85.563 +         {
  85.564 +            png_chunk_warning(png_ptr, "CRC error");
  85.565 +            return;
  85.566 +         }
  85.567 +      }
  85.568 +      /* Otherwise, we (optionally) emit a warning and use the chunk. */
  85.569 +      else if (!(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN))
  85.570 +      {
  85.571 +         png_chunk_warning(png_ptr, "CRC error");
  85.572 +      }
  85.573 +   }
  85.574 +#endif
  85.575 +
  85.576 +   png_set_PLTE(png_ptr, info_ptr, palette, num);
  85.577 +
  85.578 +#if defined(PNG_READ_tRNS_SUPPORTED)
  85.579 +   if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  85.580 +   {
  85.581 +      if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS))
  85.582 +      {
  85.583 +         if (png_ptr->num_trans > (png_uint_16)num)
  85.584 +         {
  85.585 +            png_warning(png_ptr, "Truncating incorrect tRNS chunk length");
  85.586 +            png_ptr->num_trans = (png_uint_16)num;
  85.587 +         }
  85.588 +         if (info_ptr->num_trans > (png_uint_16)num)
  85.589 +         {
  85.590 +            png_warning(png_ptr, "Truncating incorrect info tRNS chunk length");
  85.591 +            info_ptr->num_trans = (png_uint_16)num;
  85.592 +         }
  85.593 +      }
  85.594 +   }
  85.595 +#endif
  85.596 +
  85.597 +}
  85.598 +
  85.599 +void /* PRIVATE */
  85.600 +png_handle_IEND(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  85.601 +{
  85.602 +   png_debug(1, "in png_handle_IEND\n");
  85.603 +
  85.604 +   if (!(png_ptr->mode & PNG_HAVE_IHDR) || !(png_ptr->mode & PNG_HAVE_IDAT))
  85.605 +   {
  85.606 +      png_error(png_ptr, "No image in file");
  85.607 +   }
  85.608 +
  85.609 +   png_ptr->mode |= (PNG_AFTER_IDAT | PNG_HAVE_IEND);
  85.610 +
  85.611 +   if (length != 0)
  85.612 +   {
  85.613 +      png_warning(png_ptr, "Incorrect IEND chunk length");
  85.614 +   }
  85.615 +   png_crc_finish(png_ptr, length);
  85.616 +
  85.617 +   info_ptr = info_ptr; /* quiet compiler warnings about unused info_ptr */
  85.618 +}
  85.619 +
  85.620 +#if defined(PNG_READ_gAMA_SUPPORTED)
  85.621 +void /* PRIVATE */
  85.622 +png_handle_gAMA(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  85.623 +{
  85.624 +   png_fixed_point igamma;
  85.625 +#ifdef PNG_FLOATING_POINT_SUPPORTED
  85.626 +   float file_gamma;
  85.627 +#endif
  85.628 +   png_byte buf[4];
  85.629 +
  85.630 +   png_debug(1, "in png_handle_gAMA\n");
  85.631 +
  85.632 +   if (!(png_ptr->mode & PNG_HAVE_IHDR))
  85.633 +      png_error(png_ptr, "Missing IHDR before gAMA");
  85.634 +   else if (png_ptr->mode & PNG_HAVE_IDAT)
  85.635 +   {
  85.636 +      png_warning(png_ptr, "Invalid gAMA after IDAT");
  85.637 +      png_crc_finish(png_ptr, length);
  85.638 +      return;
  85.639 +   }
  85.640 +   else if (png_ptr->mode & PNG_HAVE_PLTE)
  85.641 +      /* Should be an error, but we can cope with it */
  85.642 +      png_warning(png_ptr, "Out of place gAMA chunk");
  85.643 +
  85.644 +   if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_gAMA)
  85.645 +#if defined(PNG_READ_sRGB_SUPPORTED)
  85.646 +      && !(info_ptr->valid & PNG_INFO_sRGB)
  85.647 +#endif
  85.648 +      )
  85.649 +   {
  85.650 +      png_warning(png_ptr, "Duplicate gAMA chunk");
  85.651 +      png_crc_finish(png_ptr, length);
  85.652 +      return;
  85.653 +   }
  85.654 +
  85.655 +   if (length != 4)
  85.656 +   {
  85.657 +      png_warning(png_ptr, "Incorrect gAMA chunk length");
  85.658 +      png_crc_finish(png_ptr, length);
  85.659 +      return;
  85.660 +   }
  85.661 +
  85.662 +   png_crc_read(png_ptr, buf, 4);
  85.663 +   if (png_crc_finish(png_ptr, 0))
  85.664 +      return;
  85.665 +
  85.666 +   igamma = (png_fixed_point)png_get_uint_32(buf);
  85.667 +   /* check for zero gamma */
  85.668 +   if (igamma == 0)
  85.669 +      {
  85.670 +         png_warning(png_ptr,
  85.671 +           "Ignoring gAMA chunk with gamma=0");
  85.672 +         return;
  85.673 +      }
  85.674 +
  85.675 +#if defined(PNG_READ_sRGB_SUPPORTED)
  85.676 +   if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sRGB))
  85.677 +      if (PNG_OUT_OF_RANGE(igamma, 45500L, 500))
  85.678 +      {
  85.679 +         png_warning(png_ptr,
  85.680 +           "Ignoring incorrect gAMA value when sRGB is also present");
  85.681 +#ifndef PNG_NO_CONSOLE_IO
  85.682 +         fprintf(stderr, "gamma = (%d/100000)\n", (int)igamma);
  85.683 +#endif
  85.684 +         return;
  85.685 +      }
  85.686 +#endif /* PNG_READ_sRGB_SUPPORTED */
  85.687 +
  85.688 +#ifdef PNG_FLOATING_POINT_SUPPORTED
  85.689 +   file_gamma = (float)igamma / (float)100000.0;
  85.690 +#  ifdef PNG_READ_GAMMA_SUPPORTED
  85.691 +     png_ptr->gamma = file_gamma;
  85.692 +#  endif
  85.693 +     png_set_gAMA(png_ptr, info_ptr, file_gamma);
  85.694 +#endif
  85.695 +#ifdef PNG_FIXED_POINT_SUPPORTED
  85.696 +   png_set_gAMA_fixed(png_ptr, info_ptr, igamma);
  85.697 +#endif
  85.698 +}
  85.699 +#endif
  85.700 +
  85.701 +#if defined(PNG_READ_sBIT_SUPPORTED)
  85.702 +void /* PRIVATE */
  85.703 +png_handle_sBIT(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  85.704 +{
  85.705 +   png_size_t truelen;
  85.706 +   png_byte buf[4];
  85.707 +
  85.708 +   png_debug(1, "in png_handle_sBIT\n");
  85.709 +
  85.710 +   buf[0] = buf[1] = buf[2] = buf[3] = 0;
  85.711 +
  85.712 +   if (!(png_ptr->mode & PNG_HAVE_IHDR))
  85.713 +      png_error(png_ptr, "Missing IHDR before sBIT");
  85.714 +   else if (png_ptr->mode & PNG_HAVE_IDAT)
  85.715 +   {
  85.716 +      png_warning(png_ptr, "Invalid sBIT after IDAT");
  85.717 +      png_crc_finish(png_ptr, length);
  85.718 +      return;
  85.719 +   }
  85.720 +   else if (png_ptr->mode & PNG_HAVE_PLTE)
  85.721 +   {
  85.722 +      /* Should be an error, but we can cope with it */
  85.723 +      png_warning(png_ptr, "Out of place sBIT chunk");
  85.724 +   }
  85.725 +   if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sBIT))
  85.726 +   {
  85.727 +      png_warning(png_ptr, "Duplicate sBIT chunk");
  85.728 +      png_crc_finish(png_ptr, length);
  85.729 +      return;
  85.730 +   }
  85.731 +
  85.732 +   if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  85.733 +      truelen = 3;
  85.734 +   else
  85.735 +      truelen = (png_size_t)png_ptr->channels;
  85.736 +
  85.737 +   if (length != truelen || length > 4)
  85.738 +   {
  85.739 +      png_warning(png_ptr, "Incorrect sBIT chunk length");
  85.740 +      png_crc_finish(png_ptr, length);
  85.741 +      return;
  85.742 +   }
  85.743 +
  85.744 +   png_crc_read(png_ptr, buf, truelen);
  85.745 +   if (png_crc_finish(png_ptr, 0))
  85.746 +      return;
  85.747 +
  85.748 +   if (png_ptr->color_type & PNG_COLOR_MASK_COLOR)
  85.749 +   {
  85.750 +      png_ptr->sig_bit.red = buf[0];
  85.751 +      png_ptr->sig_bit.green = buf[1];
  85.752 +      png_ptr->sig_bit.blue = buf[2];
  85.753 +      png_ptr->sig_bit.alpha = buf[3];
  85.754 +   }
  85.755 +   else
  85.756 +   {
  85.757 +      png_ptr->sig_bit.gray = buf[0];
  85.758 +      png_ptr->sig_bit.red = buf[0];
  85.759 +      png_ptr->sig_bit.green = buf[0];
  85.760 +      png_ptr->sig_bit.blue = buf[0];
  85.761 +      png_ptr->sig_bit.alpha = buf[1];
  85.762 +   }
  85.763 +   png_set_sBIT(png_ptr, info_ptr, &(png_ptr->sig_bit));
  85.764 +}
  85.765 +#endif
  85.766 +
  85.767 +#if defined(PNG_READ_cHRM_SUPPORTED)
  85.768 +void /* PRIVATE */
  85.769 +png_handle_cHRM(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  85.770 +{
  85.771 +   png_byte buf[32];
  85.772 +#ifdef PNG_FLOATING_POINT_SUPPORTED
  85.773 +   float white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y;
  85.774 +#endif
  85.775 +   png_fixed_point int_x_white, int_y_white, int_x_red, int_y_red, int_x_green,
  85.776 +      int_y_green, int_x_blue, int_y_blue;
  85.777 +
  85.778 +   png_uint_32 uint_x, uint_y;
  85.779 +
  85.780 +   png_debug(1, "in png_handle_cHRM\n");
  85.781 +
  85.782 +   if (!(png_ptr->mode & PNG_HAVE_IHDR))
  85.783 +      png_error(png_ptr, "Missing IHDR before cHRM");
  85.784 +   else if (png_ptr->mode & PNG_HAVE_IDAT)
  85.785 +   {
  85.786 +      png_warning(png_ptr, "Invalid cHRM after IDAT");
  85.787 +      png_crc_finish(png_ptr, length);
  85.788 +      return;
  85.789 +   }
  85.790 +   else if (png_ptr->mode & PNG_HAVE_PLTE)
  85.791 +      /* Should be an error, but we can cope with it */
  85.792 +      png_warning(png_ptr, "Missing PLTE before cHRM");
  85.793 +
  85.794 +   if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_cHRM)
  85.795 +#if defined(PNG_READ_sRGB_SUPPORTED)
  85.796 +      && !(info_ptr->valid & PNG_INFO_sRGB)
  85.797 +#endif
  85.798 +      )
  85.799 +   {
  85.800 +      png_warning(png_ptr, "Duplicate cHRM chunk");
  85.801 +      png_crc_finish(png_ptr, length);
  85.802 +      return;
  85.803 +   }
  85.804 +
  85.805 +   if (length != 32)
  85.806 +   {
  85.807 +      png_warning(png_ptr, "Incorrect cHRM chunk length");
  85.808 +      png_crc_finish(png_ptr, length);
  85.809 +      return;
  85.810 +   }
  85.811 +
  85.812 +   png_crc_read(png_ptr, buf, 32);
  85.813 +   if (png_crc_finish(png_ptr, 0))
  85.814 +      return;
  85.815 +
  85.816 +   uint_x = png_get_uint_32(buf);
  85.817 +   uint_y = png_get_uint_32(buf + 4);
  85.818 +   if (uint_x > 80000L || uint_y > 80000L ||
  85.819 +      uint_x + uint_y > 100000L)
  85.820 +   {
  85.821 +      png_warning(png_ptr, "Invalid cHRM white point");
  85.822 +      return;
  85.823 +   }
  85.824 +   int_x_white = (png_fixed_point)uint_x;
  85.825 +   int_y_white = (png_fixed_point)uint_y;
  85.826 +
  85.827 +   uint_x = png_get_uint_32(buf + 8);
  85.828 +   uint_y = png_get_uint_32(buf + 12);
  85.829 +   if (uint_x + uint_y > 100000L)
  85.830 +   {
  85.831 +      png_warning(png_ptr, "Invalid cHRM red point");
  85.832 +      return;
  85.833 +   }
  85.834 +   int_x_red = (png_fixed_point)uint_x;
  85.835 +   int_y_red = (png_fixed_point)uint_y;
  85.836 +
  85.837 +   uint_x = png_get_uint_32(buf + 16);
  85.838 +   uint_y = png_get_uint_32(buf + 20);
  85.839 +   if (uint_x + uint_y > 100000L)
  85.840 +   {
  85.841 +      png_warning(png_ptr, "Invalid cHRM green point");
  85.842 +      return;
  85.843 +   }
  85.844 +   int_x_green = (png_fixed_point)uint_x;
  85.845 +   int_y_green = (png_fixed_point)uint_y;
  85.846 +
  85.847 +   uint_x = png_get_uint_32(buf + 24);
  85.848 +   uint_y = png_get_uint_32(buf + 28);
  85.849 +   if (uint_x + uint_y > 100000L)
  85.850 +   {
  85.851 +      png_warning(png_ptr, "Invalid cHRM blue point");
  85.852 +      return;
  85.853 +   }
  85.854 +   int_x_blue = (png_fixed_point)uint_x;
  85.855 +   int_y_blue = (png_fixed_point)uint_y;
  85.856 +
  85.857 +#ifdef PNG_FLOATING_POINT_SUPPORTED
  85.858 +   white_x = (float)int_x_white / (float)100000.0;
  85.859 +   white_y = (float)int_y_white / (float)100000.0;
  85.860 +   red_x   = (float)int_x_red   / (float)100000.0;
  85.861 +   red_y   = (float)int_y_red   / (float)100000.0;
  85.862 +   green_x = (float)int_x_green / (float)100000.0;
  85.863 +   green_y = (float)int_y_green / (float)100000.0;
  85.864 +   blue_x  = (float)int_x_blue  / (float)100000.0;
  85.865 +   blue_y  = (float)int_y_blue  / (float)100000.0;
  85.866 +#endif
  85.867 +
  85.868 +#if defined(PNG_READ_sRGB_SUPPORTED)
  85.869 +   if ((info_ptr != NULL) && (info_ptr->valid & PNG_INFO_sRGB))
  85.870 +      {
  85.871 +      if (PNG_OUT_OF_RANGE(int_x_white, 31270,  1000) ||
  85.872 +          PNG_OUT_OF_RANGE(int_y_white, 32900,  1000) ||
  85.873 +          PNG_OUT_OF_RANGE(int_x_red,   64000L, 1000) ||
  85.874 +          PNG_OUT_OF_RANGE(int_y_red,   33000,  1000) ||
  85.875 +          PNG_OUT_OF_RANGE(int_x_green, 30000,  1000) ||
  85.876 +          PNG_OUT_OF_RANGE(int_y_green, 60000L, 1000) ||
  85.877 +          PNG_OUT_OF_RANGE(int_x_blue,  15000,  1000) ||
  85.878 +          PNG_OUT_OF_RANGE(int_y_blue,   6000,  1000))
  85.879 +         {
  85.880 +            png_warning(png_ptr,
  85.881 +              "Ignoring incorrect cHRM value when sRGB is also present");
  85.882 +#ifndef PNG_NO_CONSOLE_IO
  85.883 +#ifdef PNG_FLOATING_POINT_SUPPORTED
  85.884 +            fprintf(stderr, "wx=%f, wy=%f, rx=%f, ry=%f\n",
  85.885 +               white_x, white_y, red_x, red_y);
  85.886 +            fprintf(stderr, "gx=%f, gy=%f, bx=%f, by=%f\n",
  85.887 +               green_x, green_y, blue_x, blue_y);
  85.888 +#else
  85.889 +            fprintf(stderr, "wx=%ld, wy=%ld, rx=%ld, ry=%ld\n",
  85.890 +               int_x_white, int_y_white, int_x_red, int_y_red);
  85.891 +            fprintf(stderr, "gx=%ld, gy=%ld, bx=%ld, by=%ld\n",
  85.892 +               int_x_green, int_y_green, int_x_blue, int_y_blue);
  85.893 +#endif
  85.894 +#endif /* PNG_NO_CONSOLE_IO */
  85.895 +         }
  85.896 +         return;
  85.897 +      }
  85.898 +#endif /* PNG_READ_sRGB_SUPPORTED */
  85.899 +
  85.900 +#ifdef PNG_FLOATING_POINT_SUPPORTED
  85.901 +   png_set_cHRM(png_ptr, info_ptr,
  85.902 +      white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y);
  85.903 +#endif
  85.904 +#ifdef PNG_FIXED_POINT_SUPPORTED
  85.905 +   png_set_cHRM_fixed(png_ptr, info_ptr,
  85.906 +      int_x_white, int_y_white, int_x_red, int_y_red, int_x_green,
  85.907 +      int_y_green, int_x_blue, int_y_blue);
  85.908 +#endif
  85.909 +}
  85.910 +#endif
  85.911 +
  85.912 +#if defined(PNG_READ_sRGB_SUPPORTED)
  85.913 +void /* PRIVATE */
  85.914 +png_handle_sRGB(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  85.915 +{
  85.916 +   int intent;
  85.917 +   png_byte buf[1];
  85.918 +
  85.919 +   png_debug(1, "in png_handle_sRGB\n");
  85.920 +
  85.921 +   if (!(png_ptr->mode & PNG_HAVE_IHDR))
  85.922 +      png_error(png_ptr, "Missing IHDR before sRGB");
  85.923 +   else if (png_ptr->mode & PNG_HAVE_IDAT)
  85.924 +   {
  85.925 +      png_warning(png_ptr, "Invalid sRGB after IDAT");
  85.926 +      png_crc_finish(png_ptr, length);
  85.927 +      return;
  85.928 +   }
  85.929 +   else if (png_ptr->mode & PNG_HAVE_PLTE)
  85.930 +      /* Should be an error, but we can cope with it */
  85.931 +      png_warning(png_ptr, "Out of place sRGB chunk");
  85.932 +
  85.933 +   if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sRGB))
  85.934 +   {
  85.935 +      png_warning(png_ptr, "Duplicate sRGB chunk");
  85.936 +      png_crc_finish(png_ptr, length);
  85.937 +      return;
  85.938 +   }
  85.939 +
  85.940 +   if (length != 1)
  85.941 +   {
  85.942 +      png_warning(png_ptr, "Incorrect sRGB chunk length");
  85.943 +      png_crc_finish(png_ptr, length);
  85.944 +      return;
  85.945 +   }
  85.946 +
  85.947 +   png_crc_read(png_ptr, buf, 1);
  85.948 +   if (png_crc_finish(png_ptr, 0))
  85.949 +      return;
  85.950 +
  85.951 +   intent = buf[0];
  85.952 +   /* check for bad intent */
  85.953 +   if (intent >= PNG_sRGB_INTENT_LAST)
  85.954 +   {
  85.955 +      png_warning(png_ptr, "Unknown sRGB intent");
  85.956 +      return;
  85.957 +   }
  85.958 +
  85.959 +#if defined(PNG_READ_gAMA_SUPPORTED) && defined(PNG_READ_GAMMA_SUPPORTED)
  85.960 +   if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_gAMA))
  85.961 +   {
  85.962 +   png_fixed_point igamma;
  85.963 +#ifdef PNG_FIXED_POINT_SUPPORTED
  85.964 +      igamma=info_ptr->int_gamma;
  85.965 +#else
  85.966 +#  ifdef PNG_FLOATING_POINT_SUPPORTED
  85.967 +      igamma=(png_fixed_point)(info_ptr->gamma * 100000.);
  85.968 +#  endif
  85.969 +#endif
  85.970 +      if (PNG_OUT_OF_RANGE(igamma, 45500L, 500))
  85.971 +      {
  85.972 +         png_warning(png_ptr,
  85.973 +           "Ignoring incorrect gAMA value when sRGB is also present");
  85.974 +#ifndef PNG_NO_CONSOLE_IO
  85.975 +#  ifdef PNG_FIXED_POINT_SUPPORTED
  85.976 +         fprintf(stderr, "incorrect gamma=(%d/100000)\n",
  85.977 +            (int)png_ptr->int_gamma);
  85.978 +#  else
  85.979 +#    ifdef PNG_FLOATING_POINT_SUPPORTED
  85.980 +         fprintf(stderr, "incorrect gamma=%f\n", png_ptr->gamma);
  85.981 +#    endif
  85.982 +#  endif
  85.983 +#endif
  85.984 +      }
  85.985 +   }
  85.986 +#endif /* PNG_READ_gAMA_SUPPORTED */
  85.987 +
  85.988 +#ifdef PNG_READ_cHRM_SUPPORTED
  85.989 +#ifdef PNG_FIXED_POINT_SUPPORTED
  85.990 +   if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_cHRM))
  85.991 +      if (PNG_OUT_OF_RANGE(info_ptr->int_x_white, 31270,  1000) ||
  85.992 +          PNG_OUT_OF_RANGE(info_ptr->int_y_white, 32900,  1000) ||
  85.993 +          PNG_OUT_OF_RANGE(info_ptr->int_x_red,   64000L, 1000) ||
  85.994 +          PNG_OUT_OF_RANGE(info_ptr->int_y_red,   33000,  1000) ||
  85.995 +          PNG_OUT_OF_RANGE(info_ptr->int_x_green, 30000,  1000) ||
  85.996 +          PNG_OUT_OF_RANGE(info_ptr->int_y_green, 60000L, 1000) ||
  85.997 +          PNG_OUT_OF_RANGE(info_ptr->int_x_blue,  15000,  1000) ||
  85.998 +          PNG_OUT_OF_RANGE(info_ptr->int_y_blue,   6000,  1000))
  85.999 +         {
 85.1000 +            png_warning(png_ptr,
 85.1001 +              "Ignoring incorrect cHRM value when sRGB is also present");
 85.1002 +         }
 85.1003 +#endif /* PNG_FIXED_POINT_SUPPORTED */
 85.1004 +#endif /* PNG_READ_cHRM_SUPPORTED */
 85.1005 +
 85.1006 +   png_set_sRGB_gAMA_and_cHRM(png_ptr, info_ptr, intent);
 85.1007 +}
 85.1008 +#endif /* PNG_READ_sRGB_SUPPORTED */
 85.1009 +
 85.1010 +#if defined(PNG_READ_iCCP_SUPPORTED)
 85.1011 +void /* PRIVATE */
 85.1012 +png_handle_iCCP(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
 85.1013 +/* Note: this does not properly handle chunks that are > 64K under DOS */
 85.1014 +{
 85.1015 +   png_byte compression_type;
 85.1016 +   png_bytep pC;
 85.1017 +   png_charp profile;
 85.1018 +   png_uint_32 skip = 0;
 85.1019 +   png_uint_32 profile_size, profile_length;
 85.1020 +   png_size_t slength, prefix_length, data_length;
 85.1021 +
 85.1022 +   png_debug(1, "in png_handle_iCCP\n");
 85.1023 +
 85.1024 +   if (!(png_ptr->mode & PNG_HAVE_IHDR))
 85.1025 +      png_error(png_ptr, "Missing IHDR before iCCP");
 85.1026 +   else if (png_ptr->mode & PNG_HAVE_IDAT)
 85.1027 +   {
 85.1028 +      png_warning(png_ptr, "Invalid iCCP after IDAT");
 85.1029 +      png_crc_finish(png_ptr, length);
 85.1030 +      return;
 85.1031 +   }
 85.1032 +   else if (png_ptr->mode & PNG_HAVE_PLTE)
 85.1033 +      /* Should be an error, but we can cope with it */
 85.1034 +      png_warning(png_ptr, "Out of place iCCP chunk");
 85.1035 +
 85.1036 +   if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_iCCP))
 85.1037 +   {
 85.1038 +      png_warning(png_ptr, "Duplicate iCCP chunk");
 85.1039 +      png_crc_finish(png_ptr, length);
 85.1040 +      return;
 85.1041 +   }
 85.1042 +
 85.1043 +#ifdef PNG_MAX_MALLOC_64K
 85.1044 +   if (length > (png_uint_32)65535L)
 85.1045 +   {
 85.1046 +      png_warning(png_ptr, "iCCP chunk too large to fit in memory");
 85.1047 +      skip = length - (png_uint_32)65535L;
 85.1048 +      length = (png_uint_32)65535L;
 85.1049 +   }
 85.1050 +#endif
 85.1051 +
 85.1052 +   png_free(png_ptr, png_ptr->chunkdata);
 85.1053 +   png_ptr->chunkdata = (png_charp)png_malloc(png_ptr, length + 1);
 85.1054 +   slength = (png_size_t)length;
 85.1055 +   png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);
 85.1056 +
 85.1057 +   if (png_crc_finish(png_ptr, skip))
 85.1058 +   {
 85.1059 +      png_free(png_ptr, png_ptr->chunkdata);
 85.1060 +      png_ptr->chunkdata = NULL;
 85.1061 +      return;
 85.1062 +   }
 85.1063 +
 85.1064 +   png_ptr->chunkdata[slength] = 0x00;
 85.1065 +
 85.1066 +   for (profile = png_ptr->chunkdata; *profile; profile++)
 85.1067 +      /* empty loop to find end of name */ ;
 85.1068 +
 85.1069 +   ++profile;
 85.1070 +
 85.1071 +   /* there should be at least one zero (the compression type byte)
 85.1072 +      following the separator, and we should be on it  */
 85.1073 +   if ( profile >= png_ptr->chunkdata + slength - 1)
 85.1074 +   {
 85.1075 +      png_free(png_ptr, png_ptr->chunkdata);
 85.1076 +      png_ptr->chunkdata = NULL;
 85.1077 +      png_warning(png_ptr, "Malformed iCCP chunk");
 85.1078 +      return;
 85.1079 +   }
 85.1080 +
 85.1081 +   /* compression_type should always be zero */
 85.1082 +   compression_type = *profile++;
 85.1083 +   if (compression_type)
 85.1084 +   {
 85.1085 +      png_warning(png_ptr, "Ignoring nonzero compression type in iCCP chunk");
 85.1086 +      compression_type = 0x00;  /* Reset it to zero (libpng-1.0.6 through 1.0.8
 85.1087 +                                 wrote nonzero) */
 85.1088 +   }
 85.1089 +
 85.1090 +   prefix_length = profile - png_ptr->chunkdata;
 85.1091 +   png_decompress_chunk(png_ptr, compression_type,
 85.1092 +     slength, prefix_length, &data_length);
 85.1093 +
 85.1094 +   profile_length = data_length - prefix_length;
 85.1095 +
 85.1096 +   if ( prefix_length > data_length || profile_length < 4)
 85.1097 +   {
 85.1098 +      png_free(png_ptr, png_ptr->chunkdata);
 85.1099 +      png_ptr->chunkdata = NULL;
 85.1100 +      png_warning(png_ptr, "Profile size field missing from iCCP chunk");
 85.1101 +      return;
 85.1102 +   }
 85.1103 +
 85.1104 +   /* Check the profile_size recorded in the first 32 bits of the ICC profile */
 85.1105 +   pC = (png_bytep)(png_ptr->chunkdata + prefix_length);
 85.1106 +   profile_size = ((*(pC    ))<<24) |
 85.1107 +                  ((*(pC + 1))<<16) |
 85.1108 +                  ((*(pC + 2))<< 8) |
 85.1109 +                  ((*(pC + 3))    );
 85.1110 +
 85.1111 +   if (profile_size < profile_length)
 85.1112 +      profile_length = profile_size;
 85.1113 +
 85.1114 +   if (profile_size > profile_length)
 85.1115 +   {
 85.1116 +      png_free(png_ptr, png_ptr->chunkdata);
 85.1117 +      png_ptr->chunkdata = NULL;
 85.1118 +      png_warning(png_ptr, "Ignoring truncated iCCP profile.");
 85.1119 +      return;
 85.1120 +   }
 85.1121 +
 85.1122 +   png_set_iCCP(png_ptr, info_ptr, png_ptr->chunkdata,
 85.1123 +     compression_type, png_ptr->chunkdata + prefix_length, profile_length);
 85.1124 +   png_free(png_ptr, png_ptr->chunkdata);
 85.1125 +   png_ptr->chunkdata = NULL;
 85.1126 +}
 85.1127 +#endif /* PNG_READ_iCCP_SUPPORTED */
 85.1128 +
 85.1129 +#if defined(PNG_READ_sPLT_SUPPORTED)
 85.1130 +void /* PRIVATE */
 85.1131 +png_handle_sPLT(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
 85.1132 +/* Note: this does not properly handle chunks that are > 64K under DOS */
 85.1133 +{
 85.1134 +   png_bytep entry_start;
 85.1135 +   png_sPLT_t new_palette;
 85.1136 +#ifdef PNG_NO_POINTER_INDEXING
 85.1137 +   png_sPLT_entryp pp;
 85.1138 +#endif
 85.1139 +   int data_length, entry_size, i;
 85.1140 +   png_uint_32 skip = 0;
 85.1141 +   png_size_t slength;
 85.1142 +
 85.1143 +   png_debug(1, "in png_handle_sPLT\n");
 85.1144 +
 85.1145 +   if (!(png_ptr->mode & PNG_HAVE_IHDR))
 85.1146 +      png_error(png_ptr, "Missing IHDR before sPLT");
 85.1147 +   else if (png_ptr->mode & PNG_HAVE_IDAT)
 85.1148 +   {
 85.1149 +      png_warning(png_ptr, "Invalid sPLT after IDAT");
 85.1150 +      png_crc_finish(png_ptr, length);
 85.1151 +      return;
 85.1152 +   }
 85.1153 +
 85.1154 +#ifdef PNG_MAX_MALLOC_64K
 85.1155 +   if (length > (png_uint_32)65535L)
 85.1156 +   {
 85.1157 +      png_warning(png_ptr, "sPLT chunk too large to fit in memory");
 85.1158 +      skip = length - (png_uint_32)65535L;
 85.1159 +      length = (png_uint_32)65535L;
 85.1160 +   }
 85.1161 +#endif
 85.1162 +
 85.1163 +   png_free(png_ptr, png_ptr->chunkdata);
 85.1164 +   png_ptr->chunkdata = (png_charp)png_malloc(png_ptr, length + 1);
 85.1165 +   slength = (png_size_t)length;
 85.1166 +   png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);
 85.1167 +
 85.1168 +   if (png_crc_finish(png_ptr, skip))
 85.1169 +   {
 85.1170 +      png_free(png_ptr, png_ptr->chunkdata);
 85.1171 +      png_ptr->chunkdata = NULL;
 85.1172 +      return;
 85.1173 +   }
 85.1174 +
 85.1175 +   png_ptr->chunkdata[slength] = 0x00;
 85.1176 +
 85.1177 +   for (entry_start = (png_bytep)png_ptr->chunkdata; *entry_start; entry_start++)
 85.1178 +      /* empty loop to find end of name */ ;
 85.1179 +   ++entry_start;
 85.1180 +
 85.1181 +   /* a sample depth should follow the separator, and we should be on it  */
 85.1182 +   if (entry_start > (png_bytep)png_ptr->chunkdata + slength - 2)
 85.1183 +   {
 85.1184 +      png_free(png_ptr, png_ptr->chunkdata);
 85.1185 +      png_ptr->chunkdata = NULL;
 85.1186 +      png_warning(png_ptr, "malformed sPLT chunk");
 85.1187 +      return;
 85.1188 +   }
 85.1189 +
 85.1190 +   new_palette.depth = *entry_start++;
 85.1191 +   entry_size = (new_palette.depth == 8 ? 6 : 10);
 85.1192 +   data_length = (slength - (entry_start - (png_bytep)png_ptr->chunkdata));
 85.1193 +
 85.1194 +   /* integrity-check the data length */
 85.1195 +   if (data_length % entry_size)
 85.1196 +   {
 85.1197 +      png_free(png_ptr, png_ptr->chunkdata);
 85.1198 +      png_ptr->chunkdata = NULL;
 85.1199 +      png_warning(png_ptr, "sPLT chunk has bad length");
 85.1200 +      return;
 85.1201 +   }
 85.1202 +
 85.1203 +   new_palette.nentries = (png_int_32) ( data_length / entry_size);
 85.1204 +   if ((png_uint_32) new_palette.nentries >
 85.1205 +       (png_uint_32) (PNG_SIZE_MAX / png_sizeof(png_sPLT_entry)))
 85.1206 +   {
 85.1207 +       png_warning(png_ptr, "sPLT chunk too long");
 85.1208 +       return;
 85.1209 +   }
 85.1210 +   new_palette.entries = (png_sPLT_entryp)png_malloc_warn(
 85.1211 +       png_ptr, new_palette.nentries * png_sizeof(png_sPLT_entry));
 85.1212 +   if (new_palette.entries == NULL)
 85.1213 +   {
 85.1214 +       png_warning(png_ptr, "sPLT chunk requires too much memory");
 85.1215 +       return;
 85.1216 +   }
 85.1217 +
 85.1218 +#ifndef PNG_NO_POINTER_INDEXING
 85.1219 +   for (i = 0; i < new_palette.nentries; i++)
 85.1220 +   {
 85.1221 +      png_sPLT_entryp pp = new_palette.entries + i;
 85.1222 +
 85.1223 +      if (new_palette.depth == 8)
 85.1224 +      {
 85.1225 +          pp->red = *entry_start++;
 85.1226 +          pp->green = *entry_start++;
 85.1227 +          pp->blue = *entry_start++;
 85.1228 +          pp->alpha = *entry_start++;
 85.1229 +      }
 85.1230 +      else
 85.1231 +      {
 85.1232 +          pp->red   = png_get_uint_16(entry_start); entry_start += 2;
 85.1233 +          pp->green = png_get_uint_16(entry_start); entry_start += 2;
 85.1234 +          pp->blue  = png_get_uint_16(entry_start); entry_start += 2;
 85.1235 +          pp->alpha = png_get_uint_16(entry_start); entry_start += 2;
 85.1236 +      }
 85.1237 +      pp->frequency = png_get_uint_16(entry_start); entry_start += 2;
 85.1238 +   }
 85.1239 +#else
 85.1240 +   pp = new_palette.entries;
 85.1241 +   for (i = 0; i < new_palette.nentries; i++)
 85.1242 +   {
 85.1243 +
 85.1244 +      if (new_palette.depth == 8)
 85.1245 +      {
 85.1246 +          pp[i].red   = *entry_start++;
 85.1247 +          pp[i].green = *entry_start++;
 85.1248 +          pp[i].blue  = *entry_start++;
 85.1249 +          pp[i].alpha = *entry_start++;
 85.1250 +      }
 85.1251 +      else
 85.1252 +      {
 85.1253 +          pp[i].red   = png_get_uint_16(entry_start); entry_start += 2;
 85.1254 +          pp[i].green = png_get_uint_16(entry_start); entry_start += 2;
 85.1255 +          pp[i].blue  = png_get_uint_16(entry_start); entry_start += 2;
 85.1256 +          pp[i].alpha = png_get_uint_16(entry_start); entry_start += 2;
 85.1257 +      }
 85.1258 +      pp->frequency = png_get_uint_16(entry_start); entry_start += 2;
 85.1259 +   }
 85.1260 +#endif
 85.1261 +
 85.1262 +   /* discard all chunk data except the name and stash that */
 85.1263 +   new_palette.name = png_ptr->chunkdata;
 85.1264 +
 85.1265 +   png_set_sPLT(png_ptr, info_ptr, &new_palette, 1);
 85.1266 +
 85.1267 +   png_free(png_ptr, png_ptr->chunkdata);
 85.1268 +   png_ptr->chunkdata = NULL;
 85.1269 +   png_free(png_ptr, new_palette.entries);
 85.1270 +}
 85.1271 +#endif /* PNG_READ_sPLT_SUPPORTED */
 85.1272 +
 85.1273 +#if defined(PNG_READ_tRNS_SUPPORTED)
 85.1274 +void /* PRIVATE */
 85.1275 +png_handle_tRNS(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
 85.1276 +{
 85.1277 +   png_byte readbuf[PNG_MAX_PALETTE_LENGTH];
 85.1278 +
 85.1279 +   png_debug(1, "in png_handle_tRNS\n");
 85.1280 +
 85.1281 +   if (!(png_ptr->mode & PNG_HAVE_IHDR))
 85.1282 +      png_error(png_ptr, "Missing IHDR before tRNS");
 85.1283 +   else if (png_ptr->mode & PNG_HAVE_IDAT)
 85.1284 +   {
 85.1285 +      png_warning(png_ptr, "Invalid tRNS after IDAT");
 85.1286 +      png_crc_finish(png_ptr, length);
 85.1287 +      return;
 85.1288 +   }
 85.1289 +   else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS))
 85.1290 +   {
 85.1291 +      png_warning(png_ptr, "Duplicate tRNS chunk");
 85.1292 +      png_crc_finish(png_ptr, length);
 85.1293 +      return;
 85.1294 +   }
 85.1295 +
 85.1296 +   if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
 85.1297 +   {
 85.1298 +      png_byte buf[2];
 85.1299 +
 85.1300 +      if (length != 2)
 85.1301 +      {
 85.1302 +         png_warning(png_ptr, "Incorrect tRNS chunk length");
 85.1303 +         png_crc_finish(png_ptr, length);
 85.1304 +         return;
 85.1305 +      }
 85.1306 +
 85.1307 +      png_crc_read(png_ptr, buf, 2);
 85.1308 +      png_ptr->num_trans = 1;
 85.1309 +      png_ptr->trans_values.gray = png_get_uint_16(buf);
 85.1310 +   }
 85.1311 +   else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
 85.1312 +   {
 85.1313 +      png_byte buf[6];
 85.1314 +
 85.1315 +      if (length != 6)
 85.1316 +      {
 85.1317 +         png_warning(png_ptr, "Incorrect tRNS chunk length");
 85.1318 +         png_crc_finish(png_ptr, length);
 85.1319 +         return;
 85.1320 +      }
 85.1321 +      png_crc_read(png_ptr, buf, (png_size_t)length);
 85.1322 +      png_ptr->num_trans = 1;
 85.1323 +      png_ptr->trans_values.red = png_get_uint_16(buf);
 85.1324 +      png_ptr->trans_values.green = png_get_uint_16(buf + 2);
 85.1325 +      png_ptr->trans_values.blue = png_get_uint_16(buf + 4);
 85.1326 +   }
 85.1327 +   else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
 85.1328 +   {
 85.1329 +      if (!(png_ptr->mode & PNG_HAVE_PLTE))
 85.1330 +      {
 85.1331 +         /* Should be an error, but we can cope with it. */
 85.1332 +         png_warning(png_ptr, "Missing PLTE before tRNS");
 85.1333 +      }
 85.1334 +      if (length > (png_uint_32)png_ptr->num_palette ||
 85.1335 +          length > PNG_MAX_PALETTE_LENGTH)
 85.1336 +      {
 85.1337 +         png_warning(png_ptr, "Incorrect tRNS chunk length");
 85.1338 +         png_crc_finish(png_ptr, length);
 85.1339 +         return;
 85.1340 +      }
 85.1341 +      if (length == 0)
 85.1342 +      {
 85.1343 +         png_warning(png_ptr, "Zero length tRNS chunk");
 85.1344 +         png_crc_finish(png_ptr, length);
 85.1345 +         return;
 85.1346 +      }
 85.1347 +      png_crc_read(png_ptr, readbuf, (png_size_t)length);
 85.1348 +      png_ptr->num_trans = (png_uint_16)length;
 85.1349 +   }
 85.1350 +   else
 85.1351 +   {
 85.1352 +      png_warning(png_ptr, "tRNS chunk not allowed with alpha channel");
 85.1353 +      png_crc_finish(png_ptr, length);
 85.1354 +      return;
 85.1355 +   }
 85.1356 +
 85.1357 +   if (png_crc_finish(png_ptr, 0))
 85.1358 +   {
 85.1359 +      png_ptr->num_trans = 0;
 85.1360 +      return;
 85.1361 +   }
 85.1362 +
 85.1363 +   png_set_tRNS(png_ptr, info_ptr, readbuf, png_ptr->num_trans,
 85.1364 +      &(png_ptr->trans_values));
 85.1365 +}
 85.1366 +#endif
 85.1367 +
 85.1368 +#if defined(PNG_READ_bKGD_SUPPORTED)
 85.1369 +void /* PRIVATE */
 85.1370 +png_handle_bKGD(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
 85.1371 +{
 85.1372 +   png_size_t truelen;
 85.1373 +   png_byte buf[6];
 85.1374 +
 85.1375 +   png_debug(1, "in png_handle_bKGD\n");
 85.1376 +
 85.1377 +   if (!(png_ptr->mode & PNG_HAVE_IHDR))
 85.1378 +      png_error(png_ptr, "Missing IHDR before bKGD");
 85.1379 +   else if (png_ptr->mode & PNG_HAVE_IDAT)
 85.1380 +   {
 85.1381 +      png_warning(png_ptr, "Invalid bKGD after IDAT");
 85.1382 +      png_crc_finish(png_ptr, length);
 85.1383 +      return;
 85.1384 +   }
 85.1385 +   else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
 85.1386 +            !(png_ptr->mode & PNG_HAVE_PLTE))
 85.1387 +   {
 85.1388 +      png_warning(png_ptr, "Missing PLTE before bKGD");
 85.1389 +      png_crc_finish(png_ptr, length);
 85.1390 +      return;
 85.1391 +   }
 85.1392 +   else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_bKGD))
 85.1393 +   {
 85.1394 +      png_warning(png_ptr, "Duplicate bKGD chunk");
 85.1395 +      png_crc_finish(png_ptr, length);
 85.1396 +      return;
 85.1397 +   }
 85.1398 +
 85.1399 +   if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
 85.1400 +      truelen = 1;
 85.1401 +   else if (png_ptr->color_type & PNG_COLOR_MASK_COLOR)
 85.1402 +      truelen = 6;
 85.1403 +   else
 85.1404 +      truelen = 2;
 85.1405 +
 85.1406 +   if (length != truelen)
 85.1407 +   {
 85.1408 +      png_warning(png_ptr, "Incorrect bKGD chunk length");
 85.1409 +      png_crc_finish(png_ptr, length);
 85.1410 +      return;
 85.1411 +   }
 85.1412 +
 85.1413 +   png_crc_read(png_ptr, buf, truelen);
 85.1414 +   if (png_crc_finish(png_ptr, 0))
 85.1415 +      return;
 85.1416 +
 85.1417 +   /* We convert the index value into RGB components so that we can allow
 85.1418 +    * arbitrary RGB values for background when we have transparency, and
 85.1419 +    * so it is easy to determine the RGB values of the background color
 85.1420 +    * from the info_ptr struct. */
 85.1421 +   if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
 85.1422 +   {
 85.1423 +      png_ptr->background.index = buf[0];
 85.1424 +      if (info_ptr && info_ptr->num_palette)
 85.1425 +      {
 85.1426 +          if (buf[0] > info_ptr->num_palette)
 85.1427 +          {
 85.1428 +             png_warning(png_ptr, "Incorrect bKGD chunk index value");
 85.1429 +             return;
 85.1430 +          }
 85.1431 +          png_ptr->background.red =
 85.1432 +             (png_uint_16)png_ptr->palette[buf[0]].red;
 85.1433 +          png_ptr->background.green =
 85.1434 +             (png_uint_16)png_ptr->palette[buf[0]].green;
 85.1435 +          png_ptr->background.blue =
 85.1436 +             (png_uint_16)png_ptr->palette[buf[0]].blue;
 85.1437 +      }
 85.1438 +   }
 85.1439 +   else if (!(png_ptr->color_type & PNG_COLOR_MASK_COLOR)) /* GRAY */
 85.1440 +   {
 85.1441 +      png_ptr->background.red =
 85.1442 +      png_ptr->background.green =
 85.1443 +      png_ptr->background.blue =
 85.1444 +      png_ptr->background.gray = png_get_uint_16(buf);
 85.1445 +   }
 85.1446 +   else
 85.1447 +   {
 85.1448 +      png_ptr->background.red = png_get_uint_16(buf);
 85.1449 +      png_ptr->background.green = png_get_uint_16(buf + 2);
 85.1450 +      png_ptr->background.blue = png_get_uint_16(buf + 4);
 85.1451 +   }
 85.1452 +
 85.1453 +   png_set_bKGD(png_ptr, info_ptr, &(png_ptr->background));
 85.1454 +}
 85.1455 +#endif
 85.1456 +
 85.1457 +#if defined(PNG_READ_hIST_SUPPORTED)
 85.1458 +void /* PRIVATE */
 85.1459 +png_handle_hIST(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
 85.1460 +{
 85.1461 +   unsigned int num, i;
 85.1462 +   png_uint_16 readbuf[PNG_MAX_PALETTE_LENGTH];
 85.1463 +
 85.1464 +   png_debug(1, "in png_handle_hIST\n");
 85.1465 +
 85.1466 +   if (!(png_ptr->mode & PNG_HAVE_IHDR))
 85.1467 +      png_error(png_ptr, "Missing IHDR before hIST");
 85.1468 +   else if (png_ptr->mode & PNG_HAVE_IDAT)
 85.1469 +   {
 85.1470 +      png_warning(png_ptr, "Invalid hIST after IDAT");
 85.1471 +      png_crc_finish(png_ptr, length);
 85.1472 +      return;
 85.1473 +   }
 85.1474 +   else if (!(png_ptr->mode & PNG_HAVE_PLTE))
 85.1475 +   {
 85.1476 +      png_warning(png_ptr, "Missing PLTE before hIST");
 85.1477 +      png_crc_finish(png_ptr, length);
 85.1478 +      return;
 85.1479 +   }
 85.1480 +   else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_hIST))
 85.1481 +   {
 85.1482 +      png_warning(png_ptr, "Duplicate hIST chunk");
 85.1483 +      png_crc_finish(png_ptr, length);
 85.1484 +      return;
 85.1485 +   }
 85.1486 +
 85.1487 +   num = length / 2 ;
 85.1488 +   if (num != (unsigned int) png_ptr->num_palette || num >
 85.1489 +      (unsigned int) PNG_MAX_PALETTE_LENGTH)
 85.1490 +   {
 85.1491 +      png_warning(png_ptr, "Incorrect hIST chunk length");
 85.1492 +      png_crc_finish(png_ptr, length);
 85.1493 +      return;
 85.1494 +   }
 85.1495 +
 85.1496 +   for (i = 0; i < num; i++)
 85.1497 +   {
 85.1498 +      png_byte buf[2];
 85.1499 +
 85.1500 +      png_crc_read(png_ptr, buf, 2);
 85.1501 +      readbuf[i] = png_get_uint_16(buf);
 85.1502 +   }
 85.1503 +
 85.1504 +   if (png_crc_finish(png_ptr, 0))
 85.1505 +      return;
 85.1506 +
 85.1507 +   png_set_hIST(png_ptr, info_ptr, readbuf);
 85.1508 +}
 85.1509 +#endif
 85.1510 +
 85.1511 +#if defined(PNG_READ_pHYs_SUPPORTED)
 85.1512 +void /* PRIVATE */
 85.1513 +png_handle_pHYs(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
 85.1514 +{
 85.1515 +   png_byte buf[9];
 85.1516 +   png_uint_32 res_x, res_y;
 85.1517 +   int unit_type;
 85.1518 +
 85.1519 +   png_debug(1, "in png_handle_pHYs\n");
 85.1520 +
 85.1521 +   if (!(png_ptr->mode & PNG_HAVE_IHDR))
 85.1522 +      png_error(png_ptr, "Missing IHDR before pHYs");
 85.1523 +   else if (png_ptr->mode & PNG_HAVE_IDAT)
 85.1524 +   {
 85.1525 +      png_warning(png_ptr, "Invalid pHYs after IDAT");
 85.1526 +      png_crc_finish(png_ptr, length);
 85.1527 +      return;
 85.1528 +   }
 85.1529 +   else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pHYs))
 85.1530 +   {
 85.1531 +      png_warning(png_ptr, "Duplicate pHYs chunk");
 85.1532 +      png_crc_finish(png_ptr, length);
 85.1533 +      return;
 85.1534 +   }
 85.1535 +
 85.1536 +   if (length != 9)
 85.1537 +   {
 85.1538 +      png_warning(png_ptr, "Incorrect pHYs chunk length");
 85.1539 +      png_crc_finish(png_ptr, length);
 85.1540 +      return;
 85.1541 +   }
 85.1542 +
 85.1543 +   png_crc_read(png_ptr, buf, 9);
 85.1544 +   if (png_crc_finish(png_ptr, 0))
 85.1545 +      return;
 85.1546 +
 85.1547 +   res_x = png_get_uint_32(buf);
 85.1548 +   res_y = png_get_uint_32(buf + 4);
 85.1549 +   unit_type = buf[8];
 85.1550 +   png_set_pHYs(png_ptr, info_ptr, res_x, res_y, unit_type);
 85.1551 +}
 85.1552 +#endif
 85.1553 +
 85.1554 +#if defined(PNG_READ_oFFs_SUPPORTED)
 85.1555 +void /* PRIVATE */
 85.1556 +png_handle_oFFs(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
 85.1557 +{
 85.1558 +   png_byte buf[9];
 85.1559 +   png_int_32 offset_x, offset_y;
 85.1560 +   int unit_type;
 85.1561 +
 85.1562 +   png_debug(1, "in png_handle_oFFs\n");
 85.1563 +
 85.1564 +   if (!(png_ptr->mode & PNG_HAVE_IHDR))
 85.1565 +      png_error(png_ptr, "Missing IHDR before oFFs");
 85.1566 +   else if (png_ptr->mode & PNG_HAVE_IDAT)
 85.1567 +   {
 85.1568 +      png_warning(png_ptr, "Invalid oFFs after IDAT");
 85.1569 +      png_crc_finish(png_ptr, length);
 85.1570 +      return;
 85.1571 +   }
 85.1572 +   else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_oFFs))
 85.1573 +   {
 85.1574 +      png_warning(png_ptr, "Duplicate oFFs chunk");
 85.1575 +      png_crc_finish(png_ptr, length);
 85.1576 +      return;
 85.1577 +   }
 85.1578 +
 85.1579 +   if (length != 9)
 85.1580 +   {
 85.1581 +      png_warning(png_ptr, "Incorrect oFFs chunk length");
 85.1582 +      png_crc_finish(png_ptr, length);
 85.1583 +      return;
 85.1584 +   }
 85.1585 +
 85.1586 +   png_crc_read(png_ptr, buf, 9);
 85.1587 +   if (png_crc_finish(png_ptr, 0))
 85.1588 +      return;
 85.1589 +
 85.1590 +   offset_x = png_get_int_32(buf);
 85.1591 +   offset_y = png_get_int_32(buf + 4);
 85.1592 +   unit_type = buf[8];
 85.1593 +   png_set_oFFs(png_ptr, info_ptr, offset_x, offset_y, unit_type);
 85.1594 +}
 85.1595 +#endif
 85.1596 +
 85.1597 +#if defined(PNG_READ_pCAL_SUPPORTED)
 85.1598 +/* read the pCAL chunk (described in the PNG Extensions document) */
 85.1599 +void /* PRIVATE */
 85.1600 +png_handle_pCAL(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
 85.1601 +{
 85.1602 +   png_int_32 X0, X1;
 85.1603 +   png_byte type, nparams;
 85.1604 +   png_charp buf, units, endptr;
 85.1605 +   png_charpp params;
 85.1606 +   png_size_t slength;
 85.1607 +   int i;
 85.1608 +
 85.1609 +   png_debug(1, "in png_handle_pCAL\n");
 85.1610 +
 85.1611 +   if (!(png_ptr->mode & PNG_HAVE_IHDR))
 85.1612 +      png_error(png_ptr, "Missing IHDR before pCAL");
 85.1613 +   else if (png_ptr->mode & PNG_HAVE_IDAT)
 85.1614 +   {
 85.1615 +      png_warning(png_ptr, "Invalid pCAL after IDAT");
 85.1616 +      png_crc_finish(png_ptr, length);
 85.1617 +      return;
 85.1618 +   }
 85.1619 +   else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pCAL))
 85.1620 +   {
 85.1621 +      png_warning(png_ptr, "Duplicate pCAL chunk");
 85.1622 +      png_crc_finish(png_ptr, length);
 85.1623 +      return;
 85.1624 +   }
 85.1625 +
 85.1626 +   png_debug1(2, "Allocating and reading pCAL chunk data (%lu bytes)\n",
 85.1627 +      length + 1);
 85.1628 +   png_free(png_ptr, png_ptr->chunkdata);
 85.1629 +   png_ptr->chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1);
 85.1630 +   if (png_ptr->chunkdata == NULL)
 85.1631 +     {
 85.1632 +       png_warning(png_ptr, "No memory for pCAL purpose.");
 85.1633 +       return;
 85.1634 +     }
 85.1635 +   slength = (png_size_t)length;
 85.1636 +   png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);
 85.1637 +
 85.1638 +   if (png_crc_finish(png_ptr, 0))
 85.1639 +   {
 85.1640 +      png_free(png_ptr, png_ptr->chunkdata);
 85.1641 +      png_ptr->chunkdata = NULL;
 85.1642 +      return;
 85.1643 +   }
 85.1644 +
 85.1645 +   png_ptr->chunkdata[slength] = 0x00; /* null terminate the last string */
 85.1646 +
 85.1647 +   png_debug(3, "Finding end of pCAL purpose string\n");
 85.1648 +   for (buf = png_ptr->chunkdata; *buf; buf++)
 85.1649 +      /* empty loop */ ;
 85.1650 +
 85.1651 +   endptr = png_ptr->chunkdata + slength;
 85.1652 +
 85.1653 +   /* We need to have at least 12 bytes after the purpose string
 85.1654 +      in order to get the parameter information. */
 85.1655 +   if (endptr <= buf + 12)
 85.1656 +   {
 85.1657 +      png_warning(png_ptr, "Invalid pCAL data");
 85.1658 +      png_free(png_ptr, png_ptr->chunkdata);
 85.1659 +      png_ptr->chunkdata = NULL;
 85.1660 +      return;
 85.1661 +   }
 85.1662 +
 85.1663 +   png_debug(3, "Reading pCAL X0, X1, type, nparams, and units\n");
 85.1664 +   X0 = png_get_int_32((png_bytep)buf+1);
 85.1665 +   X1 = png_get_int_32((png_bytep)buf+5);
 85.1666 +   type = buf[9];
 85.1667 +   nparams = buf[10];
 85.1668 +   units = buf + 11;
 85.1669 +
 85.1670 +   png_debug(3, "Checking pCAL equation type and number of parameters\n");
 85.1671 +   /* Check that we have the right number of parameters for known
 85.1672 +      equation types. */
 85.1673 +   if ((type == PNG_EQUATION_LINEAR && nparams != 2) ||
 85.1674 +       (type == PNG_EQUATION_BASE_E && nparams != 3) ||
 85.1675 +       (type == PNG_EQUATION_ARBITRARY && nparams != 3) ||
 85.1676 +       (type == PNG_EQUATION_HYPERBOLIC && nparams != 4))
 85.1677 +   {
 85.1678 +      png_warning(png_ptr, "Invalid pCAL parameters for equation type");
 85.1679 +      png_free(png_ptr, png_ptr->chunkdata);
 85.1680 +      png_ptr->chunkdata = NULL;
 85.1681 +      return;
 85.1682 +   }
 85.1683 +   else if (type >= PNG_EQUATION_LAST)
 85.1684 +   {
 85.1685 +      png_warning(png_ptr, "Unrecognized equation type for pCAL chunk");
 85.1686 +   }
 85.1687 +
 85.1688 +   for (buf = units; *buf; buf++)
 85.1689 +      /* Empty loop to move past the units string. */ ;
 85.1690 +
 85.1691 +   png_debug(3, "Allocating pCAL parameters array\n");
 85.1692 +   params = (png_charpp)png_malloc_warn(png_ptr,
 85.1693 +      (png_uint_32)(nparams * png_sizeof(png_charp))) ;
 85.1694 +   if (params == NULL)
 85.1695 +     {
 85.1696 +       png_free(png_ptr, png_ptr->chunkdata);
 85.1697 +       png_ptr->chunkdata = NULL;
 85.1698 +       png_warning(png_ptr, "No memory for pCAL params.");
 85.1699 +       return;
 85.1700 +     }
 85.1701 +
 85.1702 +   /* Get pointers to the start of each parameter string. */
 85.1703 +   for (i = 0; i < (int)nparams; i++)
 85.1704 +   {
 85.1705 +      buf++; /* Skip the null string terminator from previous parameter. */
 85.1706 +
 85.1707 +      png_debug1(3, "Reading pCAL parameter %d\n", i);
 85.1708 +      for (params[i] = buf; buf <= endptr && *buf != 0x00; buf++)
 85.1709 +         /* Empty loop to move past each parameter string */ ;
 85.1710 +
 85.1711 +      /* Make sure we haven't run out of data yet */
 85.1712 +      if (buf > endptr)
 85.1713 +      {
 85.1714 +         png_warning(png_ptr, "Invalid pCAL data");
 85.1715 +         png_free(png_ptr, png_ptr->chunkdata);
 85.1716 +         png_ptr->chunkdata = NULL;
 85.1717 +         png_free(png_ptr, params);
 85.1718 +         return;
 85.1719 +      }
 85.1720 +   }
 85.1721 +
 85.1722 +   png_set_pCAL(png_ptr, info_ptr, png_ptr->chunkdata, X0, X1, type, nparams,
 85.1723 +      units, params);
 85.1724 +
 85.1725 +   png_free(png_ptr, png_ptr->chunkdata);
 85.1726 +   png_ptr->chunkdata = NULL;
 85.1727 +   png_free(png_ptr, params);
 85.1728 +}
 85.1729 +#endif
 85.1730 +
 85.1731 +#if defined(PNG_READ_sCAL_SUPPORTED)
 85.1732 +/* read the sCAL chunk */
 85.1733 +void /* PRIVATE */
 85.1734 +png_handle_sCAL(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
 85.1735 +{
 85.1736 +   png_charp ep;
 85.1737 +#ifdef PNG_FLOATING_POINT_SUPPORTED
 85.1738 +   double width, height;
 85.1739 +   png_charp vp;
 85.1740 +#else
 85.1741 +#ifdef PNG_FIXED_POINT_SUPPORTED
 85.1742 +   png_charp swidth, sheight;
 85.1743 +#endif
 85.1744 +#endif
 85.1745 +   png_size_t slength;
 85.1746 +
 85.1747 +   png_debug(1, "in png_handle_sCAL\n");
 85.1748 +
 85.1749 +   if (!(png_ptr->mode & PNG_HAVE_IHDR))
 85.1750 +      png_error(png_ptr, "Missing IHDR before sCAL");
 85.1751 +   else if (png_ptr->mode & PNG_HAVE_IDAT)
 85.1752 +   {
 85.1753 +      png_warning(png_ptr, "Invalid sCAL after IDAT");
 85.1754 +      png_crc_finish(png_ptr, length);
 85.1755 +      return;
 85.1756 +   }
 85.1757 +   else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sCAL))
 85.1758 +   {
 85.1759 +      png_warning(png_ptr, "Duplicate sCAL chunk");
 85.1760 +      png_crc_finish(png_ptr, length);
 85.1761 +      return;
 85.1762 +   }
 85.1763 +
 85.1764 +   png_debug1(2, "Allocating and reading sCAL chunk data (%lu bytes)\n",
 85.1765 +      length + 1);
 85.1766 +   png_ptr->chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1);
 85.1767 +   if (png_ptr->chunkdata == NULL)
 85.1768 +   {
 85.1769 +      png_warning(png_ptr, "Out of memory while processing sCAL chunk");
 85.1770 +      return;
 85.1771 +   }
 85.1772 +   slength = (png_size_t)length;
 85.1773 +   png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);
 85.1774 +
 85.1775 +   if (png_crc_finish(png_ptr, 0))
 85.1776 +   {
 85.1777 +      png_free(png_ptr, png_ptr->chunkdata);
 85.1778 +      png_ptr->chunkdata = NULL;
 85.1779 +      return;
 85.1780 +   }
 85.1781 +
 85.1782 +   png_ptr->chunkdata[slength] = 0x00; /* null terminate the last string */
 85.1783 +
 85.1784 +   ep = png_ptr->chunkdata + 1;        /* skip unit byte */
 85.1785 +
 85.1786 +#ifdef PNG_FLOATING_POINT_SUPPORTED
 85.1787 +   width = png_strtod(png_ptr, ep, &vp);
 85.1788 +   if (*vp)
 85.1789 +   {
 85.1790 +      png_warning(png_ptr, "malformed width string in sCAL chunk");
 85.1791 +      return;
 85.1792 +   }
 85.1793 +#else
 85.1794 +#ifdef PNG_FIXED_POINT_SUPPORTED
 85.1795 +   swidth = (png_charp)png_malloc_warn(png_ptr, png_strlen(ep) + 1);
 85.1796 +   if (swidth == NULL)
 85.1797 +   {
 85.1798 +      png_warning(png_ptr, "Out of memory while processing sCAL chunk width");
 85.1799 +      return;
 85.1800 +   }
 85.1801 +   png_memcpy(swidth, ep, (png_size_t)png_strlen(ep));
 85.1802 +#endif
 85.1803 +#endif
 85.1804 +
 85.1805 +   for (ep = png_ptr->chunkdata; *ep; ep++)
 85.1806 +      /* empty loop */ ;
 85.1807 +   ep++;
 85.1808 +
 85.1809 +   if (png_ptr->chunkdata + slength < ep)
 85.1810 +   {
 85.1811 +      png_warning(png_ptr, "Truncated sCAL chunk");
 85.1812 +#if defined(PNG_FIXED_POINT_SUPPORTED) && \
 85.1813 +    !defined(PNG_FLOATING_POINT_SUPPORTED)
 85.1814 +      png_free(png_ptr, swidth);
 85.1815 +#endif
 85.1816 +      png_free(png_ptr, png_ptr->chunkdata);
 85.1817 +      png_ptr->chunkdata = NULL;
 85.1818 +      return;
 85.1819 +   }
 85.1820 +
 85.1821 +#ifdef PNG_FLOATING_POINT_SUPPORTED
 85.1822 +   height = png_strtod(png_ptr, ep, &vp);
 85.1823 +   if (*vp)
 85.1824 +   {
 85.1825 +      png_warning(png_ptr, "malformed height string in sCAL chunk");
 85.1826 +      return;
 85.1827 +   }
 85.1828 +#else
 85.1829 +#ifdef PNG_FIXED_POINT_SUPPORTED
 85.1830 +   sheight = (png_charp)png_malloc_warn(png_ptr, png_strlen(ep) + 1);
 85.1831 +   if (sheight == NULL)
 85.1832 +   {
 85.1833 +      png_warning(png_ptr, "Out of memory while processing sCAL chunk height");
 85.1834 +      return;
 85.1835 +   }
 85.1836 +   png_memcpy(sheight, ep, (png_size_t)png_strlen(ep));
 85.1837 +#endif
 85.1838 +#endif
 85.1839 +
 85.1840 +   if (png_ptr->chunkdata + slength < ep
 85.1841 +#ifdef PNG_FLOATING_POINT_SUPPORTED
 85.1842 +      || width <= 0. || height <= 0.
 85.1843 +#endif
 85.1844 +      )
 85.1845 +   {
 85.1846 +      png_warning(png_ptr, "Invalid sCAL data");
 85.1847 +      png_free(png_ptr, png_ptr->chunkdata);
 85.1848 +      png_ptr->chunkdata = NULL;
 85.1849 +#if defined(PNG_FIXED_POINT_SUPPORTED) && !defined(PNG_FLOATING_POINT_SUPPORTED)
 85.1850 +      png_free(png_ptr, swidth);
 85.1851 +      png_free(png_ptr, sheight);
 85.1852 +#endif
 85.1853 +      return;
 85.1854 +   }
 85.1855 +
 85.1856 +
 85.1857 +#ifdef PNG_FLOATING_POINT_SUPPORTED
 85.1858 +   png_set_sCAL(png_ptr, info_ptr, png_ptr->chunkdata[0], width, height);
 85.1859 +#else
 85.1860 +#ifdef PNG_FIXED_POINT_SUPPORTED
 85.1861 +   png_set_sCAL_s(png_ptr, info_ptr, png_ptr->chunkdata[0], swidth, sheight);
 85.1862 +#endif
 85.1863 +#endif
 85.1864 +
 85.1865 +   png_free(png_ptr, png_ptr->chunkdata);
 85.1866 +   png_ptr->chunkdata = NULL;
 85.1867 +#if defined(PNG_FIXED_POINT_SUPPORTED) && !defined(PNG_FLOATING_POINT_SUPPORTED)
 85.1868 +   png_free(png_ptr, swidth);
 85.1869 +   png_free(png_ptr, sheight);
 85.1870 +#endif
 85.1871 +}
 85.1872 +#endif
 85.1873 +
 85.1874 +#if defined(PNG_READ_tIME_SUPPORTED)
 85.1875 +void /* PRIVATE */
 85.1876 +png_handle_tIME(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
 85.1877 +{
 85.1878 +   png_byte buf[7];
 85.1879 +   png_time mod_time;
 85.1880 +
 85.1881 +   png_debug(1, "in png_handle_tIME\n");
 85.1882 +
 85.1883 +   if (!(png_ptr->mode & PNG_HAVE_IHDR))
 85.1884 +      png_error(png_ptr, "Out of place tIME chunk");
 85.1885 +   else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tIME))
 85.1886 +   {
 85.1887 +      png_warning(png_ptr, "Duplicate tIME chunk");
 85.1888 +      png_crc_finish(png_ptr, length);
 85.1889 +      return;
 85.1890 +   }
 85.1891 +
 85.1892 +   if (png_ptr->mode & PNG_HAVE_IDAT)
 85.1893 +      png_ptr->mode |= PNG_AFTER_IDAT;
 85.1894 +
 85.1895 +   if (length != 7)
 85.1896 +   {
 85.1897 +      png_warning(png_ptr, "Incorrect tIME chunk length");
 85.1898 +      png_crc_finish(png_ptr, length);
 85.1899 +      return;
 85.1900 +   }
 85.1901 +
 85.1902 +   png_crc_read(png_ptr, buf, 7);
 85.1903 +   if (png_crc_finish(png_ptr, 0))
 85.1904 +      return;
 85.1905 +
 85.1906 +   mod_time.second = buf[6];
 85.1907 +   mod_time.minute = buf[5];
 85.1908 +   mod_time.hour = buf[4];
 85.1909 +   mod_time.day = buf[3];
 85.1910 +   mod_time.month = buf[2];
 85.1911 +   mod_time.year = png_get_uint_16(buf);
 85.1912 +
 85.1913 +   png_set_tIME(png_ptr, info_ptr, &mod_time);
 85.1914 +}
 85.1915 +#endif
 85.1916 +
 85.1917 +#if defined(PNG_READ_tEXt_SUPPORTED)
 85.1918 +/* Note: this does not properly handle chunks that are > 64K under DOS */
 85.1919 +void /* PRIVATE */
 85.1920 +png_handle_tEXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
 85.1921 +{
 85.1922 +   png_textp text_ptr;
 85.1923 +   png_charp key;
 85.1924 +   png_charp text;
 85.1925 +   png_uint_32 skip = 0;
 85.1926 +   png_size_t slength;
 85.1927 +   int ret;
 85.1928 +
 85.1929 +   png_debug(1, "in png_handle_tEXt\n");
 85.1930 +
 85.1931 +   if (!(png_ptr->mode & PNG_HAVE_IHDR))
 85.1932 +      png_error(png_ptr, "Missing IHDR before tEXt");
 85.1933 +
 85.1934 +   if (png_ptr->mode & PNG_HAVE_IDAT)
 85.1935 +      png_ptr->mode |= PNG_AFTER_IDAT;
 85.1936 +
 85.1937 +#ifdef PNG_MAX_MALLOC_64K
 85.1938 +   if (length > (png_uint_32)65535L)
 85.1939 +   {
 85.1940 +      png_warning(png_ptr, "tEXt chunk too large to fit in memory");
 85.1941 +      skip = length - (png_uint_32)65535L;
 85.1942 +      length = (png_uint_32)65535L;
 85.1943 +   }
 85.1944 +#endif
 85.1945 +
 85.1946 +   png_free(png_ptr, png_ptr->chunkdata);
 85.1947 +   png_ptr->chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1);
 85.1948 +   if (png_ptr->chunkdata == NULL)
 85.1949 +   {
 85.1950 +     png_warning(png_ptr, "No memory to process text chunk.");
 85.1951 +     return;
 85.1952 +   }
 85.1953 +   slength = (png_size_t)length;
 85.1954 +   png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);
 85.1955 +
 85.1956 +   if (png_crc_finish(png_ptr, skip))
 85.1957 +   {
 85.1958 +      png_free(png_ptr, png_ptr->chunkdata);
 85.1959 +      png_ptr->chunkdata = NULL;
 85.1960 +      return;
 85.1961 +   }
 85.1962 +
 85.1963 +   key = png_ptr->chunkdata;
 85.1964 +   key[slength] = 0x00;
 85.1965 +
 85.1966 +   for (text = key; *text; text++)
 85.1967 +      /* empty loop to find end of key */ ;
 85.1968 +
 85.1969 +   if (text != key + slength)
 85.1970 +      text++;
 85.1971 +
 85.1972 +   text_ptr = (png_textp)png_malloc_warn(png_ptr,
 85.1973 +      (png_uint_32)png_sizeof(png_text));
 85.1974 +   if (text_ptr == NULL)
 85.1975 +   {
 85.1976 +     png_warning(png_ptr, "Not enough memory to process text chunk.");
 85.1977 +     png_free(png_ptr, png_ptr->chunkdata);
 85.1978 +     png_ptr->chunkdata = NULL;
 85.1979 +     return;
 85.1980 +   }
 85.1981 +   text_ptr->compression = PNG_TEXT_COMPRESSION_NONE;
 85.1982 +   text_ptr->key = key;
 85.1983 +#ifdef PNG_iTXt_SUPPORTED
 85.1984 +   text_ptr->lang = NULL;
 85.1985 +   text_ptr->lang_key = NULL;
 85.1986 +   text_ptr->itxt_length = 0;
 85.1987 +#endif
 85.1988 +   text_ptr->text = text;
 85.1989 +   text_ptr->text_length = png_strlen(text);
 85.1990 +
 85.1991 +   ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1);
 85.1992 +
 85.1993 +   png_free(png_ptr, png_ptr->chunkdata);
 85.1994 +   png_ptr->chunkdata = NULL;
 85.1995 +   png_free(png_ptr, text_ptr);
 85.1996 +   if (ret)
 85.1997 +     png_warning(png_ptr, "Insufficient memory to process text chunk.");
 85.1998 +}
 85.1999 +#endif
 85.2000 +
 85.2001 +#if defined(PNG_READ_zTXt_SUPPORTED)
 85.2002 +/* note: this does not correctly handle chunks that are > 64K under DOS */
 85.2003 +void /* PRIVATE */
 85.2004 +png_handle_zTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
 85.2005 +{
 85.2006 +   png_textp text_ptr;
 85.2007 +   png_charp text;
 85.2008 +   int comp_type;
 85.2009 +   int ret;
 85.2010 +   png_size_t slength, prefix_len, data_len;
 85.2011 +
 85.2012 +   png_debug(1, "in png_handle_zTXt\n");
 85.2013 +   if (!(png_ptr->mode & PNG_HAVE_IHDR))
 85.2014 +      png_error(png_ptr, "Missing IHDR before zTXt");
 85.2015 +
 85.2016 +   if (png_ptr->mode & PNG_HAVE_IDAT)
 85.2017 +      png_ptr->mode |= PNG_AFTER_IDAT;
 85.2018 +
 85.2019 +#ifdef PNG_MAX_MALLOC_64K
 85.2020 +   /* We will no doubt have problems with chunks even half this size, but
 85.2021 +      there is no hard and fast rule to tell us where to stop. */
 85.2022 +   if (length > (png_uint_32)65535L)
 85.2023 +   {
 85.2024 +     png_warning(png_ptr, "zTXt chunk too large to fit in memory");
 85.2025 +     png_crc_finish(png_ptr, length);
 85.2026 +     return;
 85.2027 +   }
 85.2028 +#endif
 85.2029 +
 85.2030 +   png_free(png_ptr, png_ptr->chunkdata);
 85.2031 +   png_ptr->chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1);
 85.2032 +   if (png_ptr->chunkdata == NULL)
 85.2033 +   {
 85.2034 +     png_warning(png_ptr, "Out of memory processing zTXt chunk.");
 85.2035 +     return;
 85.2036 +   }
 85.2037 +   slength = (png_size_t)length;
 85.2038 +   png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);
 85.2039 +   if (png_crc_finish(png_ptr, 0))
 85.2040 +   {
 85.2041 +      png_free(png_ptr, png_ptr->chunkdata);
 85.2042 +      png_ptr->chunkdata = NULL;
 85.2043 +      return;
 85.2044 +   }
 85.2045 +
 85.2046 +   png_ptr->chunkdata[slength] = 0x00;
 85.2047 +
 85.2048 +   for (text = png_ptr->chunkdata; *text; text++)
 85.2049 +      /* empty loop */ ;
 85.2050 +
 85.2051 +   /* zTXt must have some text after the chunkdataword */
 85.2052 +   if (text >= png_ptr->chunkdata + slength - 2)
 85.2053 +   {
 85.2054 +      png_warning(png_ptr, "Truncated zTXt chunk");
 85.2055 +      png_free(png_ptr, png_ptr->chunkdata);
 85.2056 +      png_ptr->chunkdata = NULL;
 85.2057 +      return;
 85.2058 +   }
 85.2059 +   else
 85.2060 +   {
 85.2061 +       comp_type = *(++text);
 85.2062 +       if (comp_type != PNG_TEXT_COMPRESSION_zTXt)
 85.2063 +       {
 85.2064 +          png_warning(png_ptr, "Unknown compression type in zTXt chunk");
 85.2065 +          comp_type = PNG_TEXT_COMPRESSION_zTXt;
 85.2066 +       }
 85.2067 +       text++;        /* skip the compression_method byte */
 85.2068 +   }
 85.2069 +   prefix_len = text - png_ptr->chunkdata;
 85.2070 +
 85.2071 +   png_decompress_chunk(png_ptr, comp_type,
 85.2072 +     (png_size_t)length, prefix_len, &data_len);
 85.2073 +
 85.2074 +   text_ptr = (png_textp)png_malloc_warn(png_ptr,
 85.2075 +      (png_uint_32)png_sizeof(png_text));
 85.2076 +   if (text_ptr == NULL)
 85.2077 +   {
 85.2078 +     png_warning(png_ptr, "Not enough memory to process zTXt chunk.");
 85.2079 +     png_free(png_ptr, png_ptr->chunkdata);
 85.2080 +     png_ptr->chunkdata = NULL;
 85.2081 +     return;
 85.2082 +   }
 85.2083 +   text_ptr->compression = comp_type;
 85.2084 +   text_ptr->key = png_ptr->chunkdata;
 85.2085 +#ifdef PNG_iTXt_SUPPORTED
 85.2086 +   text_ptr->lang = NULL;
 85.2087 +   text_ptr->lang_key = NULL;
 85.2088 +   text_ptr->itxt_length = 0;
 85.2089 +#endif
 85.2090 +   text_ptr->text = png_ptr->chunkdata + prefix_len;
 85.2091 +   text_ptr->text_length = data_len;
 85.2092 +
 85.2093 +   ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1);
 85.2094 +
 85.2095 +   png_free(png_ptr, text_ptr);
 85.2096 +   png_free(png_ptr, png_ptr->chunkdata);
 85.2097 +   png_ptr->chunkdata = NULL;
 85.2098 +   if (ret)
 85.2099 +     png_error(png_ptr, "Insufficient memory to store zTXt chunk.");
 85.2100 +}
 85.2101 +#endif
 85.2102 +
 85.2103 +#if defined(PNG_READ_iTXt_SUPPORTED)
 85.2104 +/* note: this does not correctly handle chunks that are > 64K under DOS */
 85.2105 +void /* PRIVATE */
 85.2106 +png_handle_iTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
 85.2107 +{
 85.2108 +   png_textp text_ptr;
 85.2109 +   png_charp key, lang, text, lang_key;
 85.2110 +   int comp_flag;
 85.2111 +   int comp_type = 0;
 85.2112 +   int ret;
 85.2113 +   png_size_t slength, prefix_len, data_len;
 85.2114 +
 85.2115 +   png_debug(1, "in png_handle_iTXt\n");
 85.2116 +
 85.2117 +   if (!(png_ptr->mode & PNG_HAVE_IHDR))
 85.2118 +      png_error(png_ptr, "Missing IHDR before iTXt");
 85.2119 +
 85.2120 +   if (png_ptr->mode & PNG_HAVE_IDAT)
 85.2121 +      png_ptr->mode |= PNG_AFTER_IDAT;
 85.2122 +
 85.2123 +#ifdef PNG_MAX_MALLOC_64K
 85.2124 +   /* We will no doubt have problems with chunks even half this size, but
 85.2125 +      there is no hard and fast rule to tell us where to stop. */
 85.2126 +   if (length > (png_uint_32)65535L)
 85.2127 +   {
 85.2128 +     png_warning(png_ptr, "iTXt chunk too large to fit in memory");
 85.2129 +     png_crc_finish(png_ptr, length);
 85.2130 +     return;
 85.2131 +   }
 85.2132 +#endif
 85.2133 +
 85.2134 +   png_free(png_ptr, png_ptr->chunkdata);
 85.2135 +   png_ptr->chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1);
 85.2136 +   if (png_ptr->chunkdata == NULL)
 85.2137 +   {
 85.2138 +     png_warning(png_ptr, "No memory to process iTXt chunk.");
 85.2139 +     return;
 85.2140 +   }
 85.2141 +   slength = (png_size_t)length;
 85.2142 +   png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);
 85.2143 +   if (png_crc_finish(png_ptr, 0))
 85.2144 +   {
 85.2145 +      png_free(png_ptr, png_ptr->chunkdata);
 85.2146 +      png_ptr->chunkdata = NULL;
 85.2147 +      return;
 85.2148 +   }
 85.2149 +
 85.2150 +   png_ptr->chunkdata[slength] = 0x00;
 85.2151 +
 85.2152 +   for (lang = png_ptr->chunkdata; *lang; lang++)
 85.2153 +      /* empty loop */ ;
 85.2154 +   lang++;        /* skip NUL separator */
 85.2155 +
 85.2156 +   /* iTXt must have a language tag (possibly empty), two compression bytes,
 85.2157 +      translated keyword (possibly empty), and possibly some text after the
 85.2158 +      keyword */
 85.2159 +
 85.2160 +   if (lang >= png_ptr->chunkdata + slength - 3)
 85.2161 +   {
 85.2162 +      png_warning(png_ptr, "Truncated iTXt chunk");
 85.2163 +      png_free(png_ptr, png_ptr->chunkdata);
 85.2164 +      png_ptr->chunkdata = NULL;
 85.2165 +      return;
 85.2166 +   }
 85.2167 +   else
 85.2168 +   {
 85.2169 +       comp_flag = *lang++;
 85.2170 +       comp_type = *lang++;
 85.2171 +   }
 85.2172 +
 85.2173 +   for (lang_key = lang; *lang_key; lang_key++)
 85.2174 +      /* empty loop */ ;
 85.2175 +   lang_key++;        /* skip NUL separator */
 85.2176 +
 85.2177 +   if (lang_key >= png_ptr->chunkdata + slength)
 85.2178 +   {
 85.2179 +      png_warning(png_ptr, "Truncated iTXt chunk");
 85.2180 +      png_free(png_ptr, png_ptr->chunkdata);
 85.2181 +      png_ptr->chunkdata = NULL;
 85.2182 +      return;
 85.2183 +   }
 85.2184 +
 85.2185 +   for (text = lang_key; *text; text++)
 85.2186 +      /* empty loop */ ;
 85.2187 +   text++;        /* skip NUL separator */
 85.2188 +   if (text >= png_ptr->chunkdata + slength)
 85.2189 +   {
 85.2190 +      png_warning(png_ptr, "Malformed iTXt chunk");
 85.2191 +      png_free(png_ptr, png_ptr->chunkdata);
 85.2192 +      png_ptr->chunkdata = NULL;
 85.2193 +      return;
 85.2194 +   }
 85.2195 +
 85.2196 +   prefix_len = text - png_ptr->chunkdata;
 85.2197 +
 85.2198 +   key=png_ptr->chunkdata;
 85.2199 +   if (comp_flag)
 85.2200 +       png_decompress_chunk(png_ptr, comp_type,
 85.2201 +         (size_t)length, prefix_len, &data_len);
 85.2202 +   else
 85.2203 +       data_len = png_strlen(png_ptr->chunkdata + prefix_len);
 85.2204 +   text_ptr = (png_textp)png_malloc_warn(png_ptr,
 85.2205 +      (png_uint_32)png_sizeof(png_text));
 85.2206 +   if (text_ptr == NULL)
 85.2207 +   {
 85.2208 +     png_warning(png_ptr, "Not enough memory to process iTXt chunk.");
 85.2209 +     png_free(png_ptr, png_ptr->chunkdata);
 85.2210 +     png_ptr->chunkdata = NULL;
 85.2211 +     return;
 85.2212 +   }
 85.2213 +   text_ptr->compression = (int)comp_flag + 1;
 85.2214 +   text_ptr->lang_key = png_ptr->chunkdata + (lang_key - key);
 85.2215 +   text_ptr->lang = png_ptr->chunkdata + (lang - key);
 85.2216 +   text_ptr->itxt_length = data_len;
 85.2217 +   text_ptr->text_length = 0;
 85.2218 +   text_ptr->key = png_ptr->chunkdata;
 85.2219 +   text_ptr->text = png_ptr->chunkdata + prefix_len;
 85.2220 +
 85.2221 +   ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1);
 85.2222 +
 85.2223 +   png_free(png_ptr, text_ptr);
 85.2224 +   png_free(png_ptr, png_ptr->chunkdata);
 85.2225 +   png_ptr->chunkdata = NULL;
 85.2226 +   if (ret)
 85.2227 +     png_error(png_ptr, "Insufficient memory to store iTXt chunk.");
 85.2228 +}
 85.2229 +#endif
 85.2230 +
 85.2231 +/* This function is called when we haven't found a handler for a
 85.2232 +   chunk.  If there isn't a problem with the chunk itself (ie bad
 85.2233 +   chunk name, CRC, or a critical chunk), the chunk is silently ignored
 85.2234 +   -- unless the PNG_FLAG_UNKNOWN_CHUNKS_SUPPORTED flag is on in which
 85.2235 +   case it will be saved away to be written out later. */
 85.2236 +void /* PRIVATE */
 85.2237 +png_handle_unknown(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
 85.2238 +{
 85.2239 +   png_uint_32 skip = 0;
 85.2240 +
 85.2241 +   png_debug(1, "in png_handle_unknown\n");
 85.2242 +
 85.2243 +   if (png_ptr->mode & PNG_HAVE_IDAT)
 85.2244 +   {
 85.2245 +#ifdef PNG_USE_LOCAL_ARRAYS
 85.2246 +      PNG_CONST PNG_IDAT;
 85.2247 +#endif
 85.2248 +      if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4))  /* not an IDAT */
 85.2249 +         png_ptr->mode |= PNG_AFTER_IDAT;
 85.2250 +   }
 85.2251 +
 85.2252 +   if (!(png_ptr->chunk_name[0] & 0x20))
 85.2253 +   {
 85.2254 +#if defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
 85.2255 +      if (png_handle_as_unknown(png_ptr, png_ptr->chunk_name) !=
 85.2256 +           PNG_HANDLE_CHUNK_ALWAYS
 85.2257 +#if defined(PNG_READ_USER_CHUNKS_SUPPORTED)
 85.2258 +           && png_ptr->read_user_chunk_fn == NULL
 85.2259 +#endif
 85.2260 +        )
 85.2261 +#endif
 85.2262 +          png_chunk_error(png_ptr, "unknown critical chunk");
 85.2263 +   }
 85.2264 +
 85.2265 +#if defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
 85.2266 +   if ((png_ptr->flags & PNG_FLAG_KEEP_UNKNOWN_CHUNKS) ||
 85.2267 +       (png_ptr->read_user_chunk_fn != NULL))
 85.2268 +   {
 85.2269 +#ifdef PNG_MAX_MALLOC_64K
 85.2270 +       if (length > (png_uint_32)65535L)
 85.2271 +       {
 85.2272 +           png_warning(png_ptr, "unknown chunk too large to fit in memory");
 85.2273 +           skip = length - (png_uint_32)65535L;
 85.2274 +           length = (png_uint_32)65535L;
 85.2275 +       }
 85.2276 +#endif
 85.2277 +       png_memcpy((png_charp)png_ptr->unknown_chunk.name,
 85.2278 +                  (png_charp)png_ptr->chunk_name, 
 85.2279 +                  png_sizeof(png_ptr->unknown_chunk.name));
 85.2280 +       png_ptr->unknown_chunk.name[png_sizeof(png_ptr->unknown_chunk.name)-1] = '\0';
 85.2281 +       png_ptr->unknown_chunk.size = (png_size_t)length;
 85.2282 +       if (length == 0)
 85.2283 +         png_ptr->unknown_chunk.data = NULL;
 85.2284 +       else
 85.2285 +       {
 85.2286 +         png_ptr->unknown_chunk.data = (png_bytep)png_malloc(png_ptr, length);
 85.2287 +         png_crc_read(png_ptr, (png_bytep)png_ptr->unknown_chunk.data, length);
 85.2288 +       }
 85.2289 +#if defined(PNG_READ_USER_CHUNKS_SUPPORTED)
 85.2290 +       if (png_ptr->read_user_chunk_fn != NULL)
 85.2291 +       {
 85.2292 +          /* callback to user unknown chunk handler */
 85.2293 +          int ret;
 85.2294 +          ret = (*(png_ptr->read_user_chunk_fn))
 85.2295 +            (png_ptr, &png_ptr->unknown_chunk);
 85.2296 +          if (ret < 0)
 85.2297 +             png_chunk_error(png_ptr, "error in user chunk");
 85.2298 +          if (ret == 0)
 85.2299 +          {
 85.2300 +             if (!(png_ptr->chunk_name[0] & 0x20))
 85.2301 +                if (png_handle_as_unknown(png_ptr, png_ptr->chunk_name) !=
 85.2302 +                     PNG_HANDLE_CHUNK_ALWAYS)
 85.2303 +                   png_chunk_error(png_ptr, "unknown critical chunk");
 85.2304 +             png_set_unknown_chunks(png_ptr, info_ptr,
 85.2305 +               &png_ptr->unknown_chunk, 1);
 85.2306 +          }
 85.2307 +       }
 85.2308 +       else
 85.2309 +#endif
 85.2310 +       png_set_unknown_chunks(png_ptr, info_ptr, &png_ptr->unknown_chunk, 1);
 85.2311 +       png_free(png_ptr, png_ptr->unknown_chunk.data);
 85.2312 +       png_ptr->unknown_chunk.data = NULL;
 85.2313 +   }
 85.2314 +   else
 85.2315 +#endif
 85.2316 +      skip = length;
 85.2317 +
 85.2318 +   png_crc_finish(png_ptr, skip);
 85.2319 +
 85.2320 +#if !defined(PNG_READ_USER_CHUNKS_SUPPORTED)
 85.2321 +   info_ptr = info_ptr; /* quiet compiler warnings about unused info_ptr */
 85.2322 +#endif
 85.2323 +}
 85.2324 +
 85.2325 +/* This function is called to verify that a chunk name is valid.
 85.2326 +   This function can't have the "critical chunk check" incorporated
 85.2327 +   into it, since in the future we will need to be able to call user
 85.2328 +   functions to handle unknown critical chunks after we check that
 85.2329 +   the chunk name itself is valid. */
 85.2330 +
 85.2331 +#define isnonalpha(c) ((c) < 65 || (c) > 122 || ((c) > 90 && (c) < 97))
 85.2332 +
 85.2333 +void /* PRIVATE */
 85.2334 +png_check_chunk_name(png_structp png_ptr, png_bytep chunk_name)
 85.2335 +{
 85.2336 +   png_debug(1, "in png_check_chunk_name\n");
 85.2337 +   if (isnonalpha(chunk_name[0]) || isnonalpha(chunk_name[1]) ||
 85.2338 +       isnonalpha(chunk_name[2]) || isnonalpha(chunk_name[3]))
 85.2339 +   {
 85.2340 +      png_chunk_error(png_ptr, "invalid chunk type");
 85.2341 +   }
 85.2342 +}
 85.2343 +
 85.2344 +/* Combines the row recently read in with the existing pixels in the
 85.2345 +   row.  This routine takes care of alpha and transparency if requested.
 85.2346 +   This routine also handles the two methods of progressive display
 85.2347 +   of interlaced images, depending on the mask value.
 85.2348 +   The mask value describes which pixels are to be combined with
 85.2349 +   the row.  The pattern always repeats every 8 pixels, so just 8
 85.2350 +   bits are needed.  A one indicates the pixel is to be combined,
 85.2351 +   a zero indicates the pixel is to be skipped.  This is in addition
 85.2352 +   to any alpha or transparency value associated with the pixel.  If
 85.2353 +   you want all pixels to be combined, pass 0xff (255) in mask.  */
 85.2354 +
 85.2355 +void /* PRIVATE */
 85.2356 +png_combine_row(png_structp png_ptr, png_bytep row, int mask)
 85.2357 +{
 85.2358 +   png_debug(1, "in png_combine_row\n");
 85.2359 +   if (mask == 0xff)
 85.2360 +   {
 85.2361 +      png_memcpy(row, png_ptr->row_buf + 1,
 85.2362 +         PNG_ROWBYTES(png_ptr->row_info.pixel_depth, png_ptr->width));
 85.2363 +   }
 85.2364 +   else
 85.2365 +   {
 85.2366 +      switch (png_ptr->row_info.pixel_depth)
 85.2367 +      {
 85.2368 +         case 1:
 85.2369 +         {
 85.2370 +            png_bytep sp = png_ptr->row_buf + 1;
 85.2371 +            png_bytep dp = row;
 85.2372 +            int s_inc, s_start, s_end;
 85.2373 +            int m = 0x80;
 85.2374 +            int shift;
 85.2375 +            png_uint_32 i;
 85.2376 +            png_uint_32 row_width = png_ptr->width;
 85.2377 +
 85.2378 +#if defined(PNG_READ_PACKSWAP_SUPPORTED)
 85.2379 +            if (png_ptr->transformations & PNG_PACKSWAP)
 85.2380 +            {
 85.2381 +                s_start = 0;
 85.2382 +                s_end = 7;
 85.2383 +                s_inc = 1;
 85.2384 +            }
 85.2385 +            else
 85.2386 +#endif
 85.2387 +            {
 85.2388 +                s_start = 7;
 85.2389 +                s_end = 0;
 85.2390 +                s_inc = -1;
 85.2391 +            }
 85.2392 +
 85.2393 +            shift = s_start;
 85.2394 +
 85.2395 +            for (i = 0; i < row_width; i++)
 85.2396 +            {
 85.2397 +               if (m & mask)
 85.2398 +               {
 85.2399 +                  int value;
 85.2400 +
 85.2401 +                  value = (*sp >> shift) & 0x01;
 85.2402 +                  *dp &= (png_byte)((0x7f7f >> (7 - shift)) & 0xff);
 85.2403 +                  *dp |= (png_byte)(value << shift);
 85.2404 +               }
 85.2405 +
 85.2406 +               if (shift == s_end)
 85.2407 +               {
 85.2408 +                  shift = s_start;
 85.2409 +                  sp++;
 85.2410 +                  dp++;
 85.2411 +               }
 85.2412 +               else
 85.2413 +                  shift += s_inc;
 85.2414 +
 85.2415 +               if (m == 1)
 85.2416 +                  m = 0x80;
 85.2417 +               else
 85.2418 +                  m >>= 1;
 85.2419 +            }
 85.2420 +            break;
 85.2421 +         }
 85.2422 +         case 2:
 85.2423 +         {
 85.2424 +            png_bytep sp = png_ptr->row_buf + 1;
 85.2425 +            png_bytep dp = row;
 85.2426 +            int s_start, s_end, s_inc;
 85.2427 +            int m = 0x80;
 85.2428 +            int shift;
 85.2429 +            png_uint_32 i;
 85.2430 +            png_uint_32 row_width = png_ptr->width;
 85.2431 +            int value;
 85.2432 +
 85.2433 +#if defined(PNG_READ_PACKSWAP_SUPPORTED)
 85.2434 +            if (png_ptr->transformations & PNG_PACKSWAP)
 85.2435 +            {
 85.2436 +               s_start = 0;
 85.2437 +               s_end = 6;
 85.2438 +               s_inc = 2;
 85.2439 +            }
 85.2440 +            else
 85.2441 +#endif
 85.2442 +            {
 85.2443 +               s_start = 6;
 85.2444 +               s_end = 0;
 85.2445 +               s_inc = -2;
 85.2446 +            }
 85.2447 +
 85.2448 +            shift = s_start;
 85.2449 +
 85.2450 +            for (i = 0; i < row_width; i++)
 85.2451 +            {
 85.2452 +               if (m & mask)
 85.2453 +               {
 85.2454 +                  value = (*sp >> shift) & 0x03;
 85.2455 +                  *dp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff);
 85.2456 +                  *dp |= (png_byte)(value << shift);
 85.2457 +               }
 85.2458 +
 85.2459 +               if (shift == s_end)
 85.2460 +               {
 85.2461 +                  shift = s_start;
 85.2462 +                  sp++;
 85.2463 +                  dp++;
 85.2464 +               }
 85.2465 +               else
 85.2466 +                  shift += s_inc;
 85.2467 +               if (m == 1)
 85.2468 +                  m = 0x80;
 85.2469 +               else
 85.2470 +                  m >>= 1;
 85.2471 +            }
 85.2472 +            break;
 85.2473 +         }
 85.2474 +         case 4:
 85.2475 +         {
 85.2476 +            png_bytep sp = png_ptr->row_buf + 1;
 85.2477 +            png_bytep dp = row;
 85.2478 +            int s_start, s_end, s_inc;
 85.2479 +            int m = 0x80;
 85.2480 +            int shift;
 85.2481 +            png_uint_32 i;
 85.2482 +            png_uint_32 row_width = png_ptr->width;
 85.2483 +            int value;
 85.2484 +
 85.2485 +#if defined(PNG_READ_PACKSWAP_SUPPORTED)
 85.2486 +            if (png_ptr->transformations & PNG_PACKSWAP)
 85.2487 +            {
 85.2488 +               s_start = 0;
 85.2489 +               s_end = 4;
 85.2490 +               s_inc = 4;
 85.2491 +            }
 85.2492 +            else
 85.2493 +#endif
 85.2494 +            {
 85.2495 +               s_start = 4;
 85.2496 +               s_end = 0;
 85.2497 +               s_inc = -4;
 85.2498 +            }
 85.2499 +            shift = s_start;
 85.2500 +
 85.2501 +            for (i = 0; i < row_width; i++)
 85.2502 +            {
 85.2503 +               if (m & mask)
 85.2504 +               {
 85.2505 +                  value = (*sp >> shift) & 0xf;
 85.2506 +                  *dp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff);
 85.2507 +                  *dp |= (png_byte)(value << shift);
 85.2508 +               }
 85.2509 +
 85.2510 +               if (shift == s_end)
 85.2511 +               {
 85.2512 +                  shift = s_start;
 85.2513 +                  sp++;
 85.2514 +                  dp++;
 85.2515 +               }
 85.2516 +               else
 85.2517 +                  shift += s_inc;
 85.2518 +               if (m == 1)
 85.2519 +                  m = 0x80;
 85.2520 +               else
 85.2521 +                  m >>= 1;
 85.2522 +            }
 85.2523 +            break;
 85.2524 +         }
 85.2525 +         default:
 85.2526 +         {
 85.2527 +            png_bytep sp = png_ptr->row_buf + 1;
 85.2528 +            png_bytep dp = row;
 85.2529 +            png_size_t pixel_bytes = (png_ptr->row_info.pixel_depth >> 3);
 85.2530 +            png_uint_32 i;
 85.2531 +            png_uint_32 row_width = png_ptr->width;
 85.2532 +            png_byte m = 0x80;
 85.2533 +
 85.2534 +
 85.2535 +            for (i = 0; i < row_width; i++)
 85.2536 +            {
 85.2537 +               if (m & mask)
 85.2538 +               {
 85.2539 +                  png_memcpy(dp, sp, pixel_bytes);
 85.2540 +               }
 85.2541 +
 85.2542 +               sp += pixel_bytes;
 85.2543 +               dp += pixel_bytes;
 85.2544 +
 85.2545 +               if (m == 1)
 85.2546 +                  m = 0x80;
 85.2547 +               else
 85.2548 +                  m >>= 1;
 85.2549 +            }
 85.2550 +            break;
 85.2551 +         }
 85.2552 +      }
 85.2553 +   }
 85.2554 +}
 85.2555 +
 85.2556 +#ifdef PNG_READ_INTERLACING_SUPPORTED
 85.2557 +/* OLD pre-1.0.9 interface:
 85.2558 +void png_do_read_interlace(png_row_infop row_info, png_bytep row, int pass,
 85.2559 +   png_uint_32 transformations)
 85.2560 + */
 85.2561 +void /* PRIVATE */
 85.2562 +png_do_read_interlace(png_structp png_ptr)
 85.2563 +{
 85.2564 +   png_row_infop row_info = &(png_ptr->row_info);
 85.2565 +   png_bytep row = png_ptr->row_buf + 1;
 85.2566 +   int pass = png_ptr->pass;
 85.2567 +   png_uint_32 transformations = png_ptr->transformations;
 85.2568 +#ifdef PNG_USE_LOCAL_ARRAYS
 85.2569 +   /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
 85.2570 +   /* offset to next interlace block */
 85.2571 +   PNG_CONST int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
 85.2572 +#endif
 85.2573 +
 85.2574 +   png_debug(1, "in png_do_read_interlace\n");
 85.2575 +   if (row != NULL && row_info != NULL)
 85.2576 +   {
 85.2577 +      png_uint_32 final_width;
 85.2578 +
 85.2579 +      final_width = row_info->width * png_pass_inc[pass];
 85.2580 +
 85.2581 +      switch (row_info->pixel_depth)
 85.2582 +      {
 85.2583 +         case 1:
 85.2584 +         {
 85.2585 +            png_bytep sp = row + (png_size_t)((row_info->width - 1) >> 3);
 85.2586 +            png_bytep dp = row + (png_size_t)((final_width - 1) >> 3);
 85.2587 +            int sshift, dshift;
 85.2588 +            int s_start, s_end, s_inc;
 85.2589 +            int jstop = png_pass_inc[pass];
 85.2590 +            png_byte v;
 85.2591 +            png_uint_32 i;
 85.2592 +            int j;
 85.2593 +
 85.2594 +#if defined(PNG_READ_PACKSWAP_SUPPORTED)
 85.2595 +            if (transformations & PNG_PACKSWAP)
 85.2596 +            {
 85.2597 +                sshift = (int)((row_info->width + 7) & 0x07);
 85.2598 +                dshift = (int)((final_width + 7) & 0x07);
 85.2599 +                s_start = 7;
 85.2600 +                s_end = 0;
 85.2601 +                s_inc = -1;
 85.2602 +            }
 85.2603 +            else
 85.2604 +#endif
 85.2605 +            {
 85.2606 +                sshift = 7 - (int)((row_info->width + 7) & 0x07);
 85.2607 +                dshift = 7 - (int)((final_width + 7) & 0x07);
 85.2608 +                s_start = 0;
 85.2609 +                s_end = 7;
 85.2610 +                s_inc = 1;
 85.2611 +            }
 85.2612 +
 85.2613 +            for (i = 0; i < row_info->width; i++)
 85.2614 +            {
 85.2615 +               v = (png_byte)((*sp >> sshift) & 0x01);
 85.2616 +               for (j = 0; j < jstop; j++)
 85.2617 +               {
 85.2618 +                  *dp &= (png_byte)((0x7f7f >> (7 - dshift)) & 0xff);
 85.2619 +                  *dp |= (png_byte)(v << dshift);
 85.2620 +                  if (dshift == s_end)
 85.2621 +                  {
 85.2622 +                     dshift = s_start;
 85.2623 +                     dp--;
 85.2624 +                  }
 85.2625 +                  else
 85.2626 +                     dshift += s_inc;
 85.2627 +               }
 85.2628 +               if (sshift == s_end)
 85.2629 +               {
 85.2630 +                  sshift = s_start;
 85.2631 +                  sp--;
 85.2632 +               }
 85.2633 +               else
 85.2634 +                  sshift += s_inc;
 85.2635 +            }
 85.2636 +            break;
 85.2637 +         }
 85.2638 +         case 2:
 85.2639 +         {
 85.2640 +            png_bytep sp = row + (png_uint_32)((row_info->width - 1) >> 2);
 85.2641 +            png_bytep dp = row + (png_uint_32)((final_width - 1) >> 2);
 85.2642 +            int sshift, dshift;
 85.2643 +            int s_start, s_end, s_inc;
 85.2644 +            int jstop = png_pass_inc[pass];
 85.2645 +            png_uint_32 i;
 85.2646 +
 85.2647 +#if defined(PNG_READ_PACKSWAP_SUPPORTED)
 85.2648 +            if (transformations & PNG_PACKSWAP)
 85.2649 +            {
 85.2650 +               sshift = (int)(((row_info->width + 3) & 0x03) << 1);
 85.2651 +               dshift = (int)(((final_width + 3) & 0x03) << 1);
 85.2652 +               s_start = 6;
 85.2653 +               s_end = 0;
 85.2654 +               s_inc = -2;
 85.2655 +            }
 85.2656 +            else
 85.2657 +#endif
 85.2658 +            {
 85.2659 +               sshift = (int)((3 - ((row_info->width + 3) & 0x03)) << 1);
 85.2660 +               dshift = (int)((3 - ((final_width + 3) & 0x03)) << 1);
 85.2661 +               s_start = 0;
 85.2662 +               s_end = 6;
 85.2663 +               s_inc = 2;
 85.2664 +            }
 85.2665 +
 85.2666 +            for (i = 0; i < row_info->width; i++)
 85.2667 +            {
 85.2668 +               png_byte v;
 85.2669 +               int j;
 85.2670 +
 85.2671 +               v = (png_byte)((*sp >> sshift) & 0x03);
 85.2672 +               for (j = 0; j < jstop; j++)
 85.2673 +               {
 85.2674 +                  *dp &= (png_byte)((0x3f3f >> (6 - dshift)) & 0xff);
 85.2675 +                  *dp |= (png_byte)(v << dshift);
 85.2676 +                  if (dshift == s_end)
 85.2677 +                  {
 85.2678 +                     dshift = s_start;
 85.2679 +                     dp--;
 85.2680 +                  }
 85.2681 +                  else
 85.2682 +                     dshift += s_inc;
 85.2683 +               }
 85.2684 +               if (sshift == s_end)
 85.2685 +               {
 85.2686 +                  sshift = s_start;
 85.2687 +                  sp--;
 85.2688 +               }
 85.2689 +               else
 85.2690 +                  sshift += s_inc;
 85.2691 +            }
 85.2692 +            break;
 85.2693 +         }
 85.2694 +         case 4:
 85.2695 +         {
 85.2696 +            png_bytep sp = row + (png_size_t)((row_info->width - 1) >> 1);
 85.2697 +            png_bytep dp = row + (png_size_t)((final_width - 1) >> 1);
 85.2698 +            int sshift, dshift;
 85.2699 +            int s_start, s_end, s_inc;
 85.2700 +            png_uint_32 i;
 85.2701 +            int jstop = png_pass_inc[pass];
 85.2702 +
 85.2703 +#if defined(PNG_READ_PACKSWAP_SUPPORTED)
 85.2704 +            if (transformations & PNG_PACKSWAP)
 85.2705 +            {
 85.2706 +               sshift = (int)(((row_info->width + 1) & 0x01) << 2);
 85.2707 +               dshift = (int)(((final_width + 1) & 0x01) << 2);
 85.2708 +               s_start = 4;
 85.2709 +               s_end = 0;
 85.2710 +               s_inc = -4;
 85.2711 +            }
 85.2712 +            else
 85.2713 +#endif
 85.2714 +            {
 85.2715 +               sshift = (int)((1 - ((row_info->width + 1) & 0x01)) << 2);
 85.2716 +               dshift = (int)((1 - ((final_width + 1) & 0x01)) << 2);
 85.2717 +               s_start = 0;
 85.2718 +               s_end = 4;
 85.2719 +               s_inc = 4;
 85.2720 +            }
 85.2721 +
 85.2722 +            for (i = 0; i < row_info->width; i++)
 85.2723 +            {
 85.2724 +               png_byte v = (png_byte)((*sp >> sshift) & 0xf);
 85.2725 +               int j;
 85.2726 +
 85.2727 +               for (j = 0; j < jstop; j++)
 85.2728 +               {
 85.2729 +                  *dp &= (png_byte)((0xf0f >> (4 - dshift)) & 0xff);
 85.2730 +                  *dp |= (png_byte)(v << dshift);
 85.2731 +                  if (dshift == s_end)
 85.2732 +                  {
 85.2733 +                     dshift = s_start;
 85.2734 +                     dp--;
 85.2735 +                  }
 85.2736 +                  else
 85.2737 +                     dshift += s_inc;
 85.2738 +               }
 85.2739 +               if (sshift == s_end)
 85.2740 +               {
 85.2741 +                  sshift = s_start;
 85.2742 +                  sp--;
 85.2743 +               }
 85.2744 +               else
 85.2745 +                  sshift += s_inc;
 85.2746 +            }
 85.2747 +            break;
 85.2748 +         }
 85.2749 +         default:
 85.2750 +         {
 85.2751 +            png_size_t pixel_bytes = (row_info->pixel_depth >> 3);
 85.2752 +            png_bytep sp = row + (png_size_t)(row_info->width - 1) * pixel_bytes;
 85.2753 +            png_bytep dp = row + (png_size_t)(final_width - 1) * pixel_bytes;
 85.2754 +
 85.2755 +            int jstop = png_pass_inc[pass];
 85.2756 +            png_uint_32 i;
 85.2757 +
 85.2758 +            for (i = 0; i < row_info->width; i++)
 85.2759 +            {
 85.2760 +               png_byte v[8];
 85.2761 +               int j;
 85.2762 +
 85.2763 +               png_memcpy(v, sp, pixel_bytes);
 85.2764 +               for (j = 0; j < jstop; j++)
 85.2765 +               {
 85.2766 +                  png_memcpy(dp, v, pixel_bytes);
 85.2767 +                  dp -= pixel_bytes;
 85.2768 +               }
 85.2769 +               sp -= pixel_bytes;
 85.2770 +            }
 85.2771 +            break;
 85.2772 +         }
 85.2773 +      }
 85.2774 +      row_info->width = final_width;
 85.2775 +      row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, final_width);
 85.2776 +   }
 85.2777 +#if !defined(PNG_READ_PACKSWAP_SUPPORTED)
 85.2778 +   transformations = transformations; /* silence compiler warning */
 85.2779 +#endif
 85.2780 +}
 85.2781 +#endif /* PNG_READ_INTERLACING_SUPPORTED */
 85.2782 +
 85.2783 +void /* PRIVATE */
 85.2784 +png_read_filter_row(png_structp png_ptr, png_row_infop row_info, png_bytep row,
 85.2785 +   png_bytep prev_row, int filter)
 85.2786 +{
 85.2787 +   png_debug(1, "in png_read_filter_row\n");
 85.2788 +   png_debug2(2, "row = %lu, filter = %d\n", png_ptr->row_number, filter);
 85.2789 +   switch (filter)
 85.2790 +   {
 85.2791 +      case PNG_FILTER_VALUE_NONE:
 85.2792 +         break;
 85.2793 +      case PNG_FILTER_VALUE_SUB:
 85.2794 +      {
 85.2795 +         png_uint_32 i;
 85.2796 +         png_uint_32 istop = row_info->rowbytes;
 85.2797 +         png_uint_32 bpp = (row_info->pixel_depth + 7) >> 3;
 85.2798 +         png_bytep rp = row + bpp;
 85.2799 +         png_bytep lp = row;
 85.2800 +
 85.2801 +         for (i = bpp; i < istop; i++)
 85.2802 +         {
 85.2803 +            *rp = (png_byte)(((int)(*rp) + (int)(*lp++)) & 0xff);
 85.2804 +            rp++;
 85.2805 +         }
 85.2806 +         break;
 85.2807 +      }
 85.2808 +      case PNG_FILTER_VALUE_UP:
 85.2809 +      {
 85.2810 +         png_uint_32 i;
 85.2811 +         png_uint_32 istop = row_info->rowbytes;
 85.2812 +         png_bytep rp = row;
 85.2813 +         png_bytep pp = prev_row;
 85.2814 +
 85.2815 +         for (i = 0; i < istop; i++)
 85.2816 +         {
 85.2817 +            *rp = (png_byte)(((int)(*rp) + (int)(*pp++)) & 0xff);
 85.2818 +            rp++;
 85.2819 +         }
 85.2820 +         break;
 85.2821 +      }
 85.2822 +      case PNG_FILTER_VALUE_AVG:
 85.2823 +      {
 85.2824 +         png_uint_32 i;
 85.2825 +         png_bytep rp = row;
 85.2826 +         png_bytep pp = prev_row;
 85.2827 +         png_bytep lp = row;
 85.2828 +         png_uint_32 bpp = (row_info->pixel_depth + 7) >> 3;
 85.2829 +         png_uint_32 istop = row_info->rowbytes - bpp;
 85.2830 +
 85.2831 +         for (i = 0; i < bpp; i++)
 85.2832 +         {
 85.2833 +            *rp = (png_byte)(((int)(*rp) +
 85.2834 +               ((int)(*pp++) / 2 )) & 0xff);
 85.2835 +            rp++;
 85.2836 +         }
 85.2837 +
 85.2838 +         for (i = 0; i < istop; i++)
 85.2839 +         {
 85.2840 +            *rp = (png_byte)(((int)(*rp) +
 85.2841 +               (int)(*pp++ + *lp++) / 2 ) & 0xff);
 85.2842 +            rp++;
 85.2843 +         }
 85.2844 +         break;
 85.2845 +      }
 85.2846 +      case PNG_FILTER_VALUE_PAETH:
 85.2847 +      {
 85.2848 +         png_uint_32 i;
 85.2849 +         png_bytep rp = row;
 85.2850 +         png_bytep pp = prev_row;
 85.2851 +         png_bytep lp = row;
 85.2852 +         png_bytep cp = prev_row;
 85.2853 +         png_uint_32 bpp = (row_info->pixel_depth + 7) >> 3;
 85.2854 +         png_uint_32 istop=row_info->rowbytes - bpp;
 85.2855 +
 85.2856 +         for (i = 0; i < bpp; i++)
 85.2857 +         {
 85.2858 +            *rp = (png_byte)(((int)(*rp) + (int)(*pp++)) & 0xff);
 85.2859 +            rp++;
 85.2860 +         }
 85.2861 +
 85.2862 +         for (i = 0; i < istop; i++)   /* use leftover rp,pp */
 85.2863 +         {
 85.2864 +            int a, b, c, pa, pb, pc, p;
 85.2865 +
 85.2866 +            a = *lp++;
 85.2867 +            b = *pp++;
 85.2868 +            c = *cp++;
 85.2869 +
 85.2870 +            p = b - c;
 85.2871 +            pc = a - c;
 85.2872 +
 85.2873 +#ifdef PNG_USE_ABS
 85.2874 +            pa = abs(p);
 85.2875 +            pb = abs(pc);
 85.2876 +            pc = abs(p + pc);
 85.2877 +#else
 85.2878 +            pa = p < 0 ? -p : p;
 85.2879 +            pb = pc < 0 ? -pc : pc;
 85.2880 +            pc = (p + pc) < 0 ? -(p + pc) : p + pc;
 85.2881 +#endif
 85.2882 +
 85.2883 +            /*
 85.2884 +               if (pa <= pb && pa <= pc)
 85.2885 +                  p = a;
 85.2886 +               else if (pb <= pc)
 85.2887 +                  p = b;
 85.2888 +               else
 85.2889 +                  p = c;
 85.2890 +             */
 85.2891 +
 85.2892 +            p = (pa <= pb && pa <= pc) ? a : (pb <= pc) ? b : c;
 85.2893 +
 85.2894 +            *rp = (png_byte)(((int)(*rp) + p) & 0xff);
 85.2895 +            rp++;
 85.2896 +         }
 85.2897 +         break;
 85.2898 +      }
 85.2899 +      default:
 85.2900 +         png_warning(png_ptr, "Ignoring bad adaptive filter type");
 85.2901 +         *row = 0;
 85.2902 +         break;
 85.2903 +   }
 85.2904 +}
 85.2905 +
 85.2906 +void /* PRIVATE */
 85.2907 +png_read_finish_row(png_structp png_ptr)
 85.2908 +{
 85.2909 +#ifdef PNG_USE_LOCAL_ARRAYS
 85.2910 +#ifdef PNG_READ_INTERLACING_SUPPORTED
 85.2911 +   /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
 85.2912 +
 85.2913 +   /* start of interlace block */
 85.2914 +   PNG_CONST int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
 85.2915 +
 85.2916 +   /* offset to next interlace block */
 85.2917 +   PNG_CONST int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
 85.2918 +
 85.2919 +   /* start of interlace block in the y direction */
 85.2920 +   PNG_CONST int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
 85.2921 +
 85.2922 +   /* offset to next interlace block in the y direction */
 85.2923 +   PNG_CONST int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
 85.2924 +#endif /* PNG_READ_INTERLACING_SUPPORTED */
 85.2925 +#endif
 85.2926 +
 85.2927 +   png_debug(1, "in png_read_finish_row\n");
 85.2928 +   png_ptr->row_number++;
 85.2929 +   if (png_ptr->row_number < png_ptr->num_rows)
 85.2930 +      return;
 85.2931 +
 85.2932 +#ifdef PNG_READ_INTERLACING_SUPPORTED
 85.2933 +   if (png_ptr->interlaced)
 85.2934 +   {
 85.2935 +      png_ptr->row_number = 0;
 85.2936 +      png_memset_check(png_ptr, png_ptr->prev_row, 0,
 85.2937 +         png_ptr->rowbytes + 1);
 85.2938 +      do
 85.2939 +      {
 85.2940 +         png_ptr->pass++;
 85.2941 +         if (png_ptr->pass >= 7)
 85.2942 +            break;
 85.2943 +         png_ptr->iwidth = (png_ptr->width +
 85.2944 +            png_pass_inc[png_ptr->pass] - 1 -
 85.2945 +            png_pass_start[png_ptr->pass]) /
 85.2946 +            png_pass_inc[png_ptr->pass];
 85.2947 +
 85.2948 +         png_ptr->irowbytes = PNG_ROWBYTES(png_ptr->pixel_depth,
 85.2949 +            png_ptr->iwidth) + 1;
 85.2950 +
 85.2951 +         if (!(png_ptr->transformations & PNG_INTERLACE))
 85.2952 +         {
 85.2953 +            png_ptr->num_rows = (png_ptr->height +
 85.2954 +               png_pass_yinc[png_ptr->pass] - 1 -
 85.2955 +               png_pass_ystart[png_ptr->pass]) /
 85.2956 +               png_pass_yinc[png_ptr->pass];
 85.2957 +            if (!(png_ptr->num_rows))
 85.2958 +               continue;
 85.2959 +         }
 85.2960 +         else  /* if (png_ptr->transformations & PNG_INTERLACE) */
 85.2961 +            break;
 85.2962 +      } while (png_ptr->iwidth == 0);
 85.2963 +
 85.2964 +      if (png_ptr->pass < 7)
 85.2965 +         return;
 85.2966 +   }
 85.2967 +#endif /* PNG_READ_INTERLACING_SUPPORTED */
 85.2968 +
 85.2969 +   if (!(png_ptr->flags & PNG_FLAG_ZLIB_FINISHED))
 85.2970 +   {
 85.2971 +#ifdef PNG_USE_LOCAL_ARRAYS
 85.2972 +      PNG_CONST PNG_IDAT;
 85.2973 +#endif
 85.2974 +      char extra;
 85.2975 +      int ret;
 85.2976 +
 85.2977 +      png_ptr->zstream.next_out = (Byte *)&extra;
 85.2978 +      png_ptr->zstream.avail_out = (uInt)1;
 85.2979 +      for (;;)
 85.2980 +      {
 85.2981 +         if (!(png_ptr->zstream.avail_in))
 85.2982 +         {
 85.2983 +            while (!png_ptr->idat_size)
 85.2984 +            {
 85.2985 +               png_byte chunk_length[4];
 85.2986 +
 85.2987 +               png_crc_finish(png_ptr, 0);
 85.2988 +
 85.2989 +               png_read_data(png_ptr, chunk_length, 4);
 85.2990 +               png_ptr->idat_size = png_get_uint_31(png_ptr, chunk_length);
 85.2991 +               png_reset_crc(png_ptr);
 85.2992 +               png_crc_read(png_ptr, png_ptr->chunk_name, 4);
 85.2993 +               if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
 85.2994 +                  png_error(png_ptr, "Not enough image data");
 85.2995 +
 85.2996 +            }
 85.2997 +            png_ptr->zstream.avail_in = (uInt)png_ptr->zbuf_size;
 85.2998 +            png_ptr->zstream.next_in = png_ptr->zbuf;
 85.2999 +            if (png_ptr->zbuf_size > png_ptr->idat_size)
 85.3000 +               png_ptr->zstream.avail_in = (uInt)png_ptr->idat_size;
 85.3001 +            png_crc_read(png_ptr, png_ptr->zbuf, png_ptr->zstream.avail_in);
 85.3002 +            png_ptr->idat_size -= png_ptr->zstream.avail_in;
 85.3003 +         }
 85.3004 +         ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);
 85.3005 +         if (ret == Z_STREAM_END)
 85.3006 +         {
 85.3007 +            if (!(png_ptr->zstream.avail_out) || png_ptr->zstream.avail_in ||
 85.3008 +               png_ptr->idat_size)
 85.3009 +               png_warning(png_ptr, "Extra compressed data");
 85.3010 +            png_ptr->mode |= PNG_AFTER_IDAT;
 85.3011 +            png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
 85.3012 +            break;
 85.3013 +         }
 85.3014 +         if (ret != Z_OK)
 85.3015 +            png_error(png_ptr, png_ptr->zstream.msg ? png_ptr->zstream.msg :
 85.3016 +                      "Decompression Error");
 85.3017 +
 85.3018 +         if (!(png_ptr->zstream.avail_out))
 85.3019 +         {
 85.3020 +            png_warning(png_ptr, "Extra compressed data.");
 85.3021 +            png_ptr->mode |= PNG_AFTER_IDAT;
 85.3022 +            png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
 85.3023 +            break;
 85.3024 +         }
 85.3025 +
 85.3026 +      }
 85.3027 +      png_ptr->zstream.avail_out = 0;
 85.3028 +   }
 85.3029 +
 85.3030 +   if (png_ptr->idat_size || png_ptr->zstream.avail_in)
 85.3031 +      png_warning(png_ptr, "Extra compression data");
 85.3032 +
 85.3033 +   inflateReset(&png_ptr->zstream);
 85.3034 +
 85.3035 +   png_ptr->mode |= PNG_AFTER_IDAT;
 85.3036 +}
 85.3037 +
 85.3038 +void /* PRIVATE */
 85.3039 +png_read_start_row(png_structp png_ptr)
 85.3040 +{
 85.3041 +#ifdef PNG_USE_LOCAL_ARRAYS
 85.3042 +#ifdef PNG_READ_INTERLACING_SUPPORTED
 85.3043 +   /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
 85.3044 +
 85.3045 +   /* start of interlace block */
 85.3046 +   PNG_CONST int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
 85.3047 +
 85.3048 +   /* offset to next interlace block */
 85.3049 +   PNG_CONST int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
 85.3050 +
 85.3051 +   /* start of interlace block in the y direction */
 85.3052 +   PNG_CONST int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
 85.3053 +
 85.3054 +   /* offset to next interlace block in the y direction */
 85.3055 +   PNG_CONST int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
 85.3056 +#endif
 85.3057 +#endif
 85.3058 +
 85.3059 +   int max_pixel_depth;
 85.3060 +   png_size_t row_bytes;
 85.3061 +
 85.3062 +   png_debug(1, "in png_read_start_row\n");
 85.3063 +   png_ptr->zstream.avail_in = 0;
 85.3064 +   png_init_read_transformations(png_ptr);
 85.3065 +#ifdef PNG_READ_INTERLACING_SUPPORTED
 85.3066 +   if (png_ptr->interlaced)
 85.3067 +   {
 85.3068 +      if (!(png_ptr->transformations & PNG_INTERLACE))
 85.3069 +         png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
 85.3070 +            png_pass_ystart[0]) / png_pass_yinc[0];
 85.3071 +      else
 85.3072 +         png_ptr->num_rows = png_ptr->height;
 85.3073 +
 85.3074 +      png_ptr->iwidth = (png_ptr->width +
 85.3075 +         png_pass_inc[png_ptr->pass] - 1 -
 85.3076 +         png_pass_start[png_ptr->pass]) /
 85.3077 +         png_pass_inc[png_ptr->pass];
 85.3078 +
 85.3079 +         png_ptr->irowbytes =
 85.3080 +            PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->iwidth) + 1;
 85.3081 +   }
 85.3082 +   else
 85.3083 +#endif /* PNG_READ_INTERLACING_SUPPORTED */
 85.3084 +   {
 85.3085 +      png_ptr->num_rows = png_ptr->height;
 85.3086 +      png_ptr->iwidth = png_ptr->width;
 85.3087 +      png_ptr->irowbytes = png_ptr->rowbytes + 1;
 85.3088 +   }
 85.3089 +   max_pixel_depth = png_ptr->pixel_depth;
 85.3090 +
 85.3091 +#if defined(PNG_READ_PACK_SUPPORTED)
 85.3092 +   if ((png_ptr->transformations & PNG_PACK) && png_ptr->bit_depth < 8)
 85.3093 +      max_pixel_depth = 8;
 85.3094 +#endif
 85.3095 +
 85.3096 +#if defined(PNG_READ_EXPAND_SUPPORTED)
 85.3097 +   if (png_ptr->transformations & PNG_EXPAND)
 85.3098 +   {
 85.3099 +      if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
 85.3100 +      {
 85.3101 +         if (png_ptr->num_trans)
 85.3102 +            max_pixel_depth = 32;
 85.3103 +         else
 85.3104 +            max_pixel_depth = 24;
 85.3105 +      }
 85.3106 +      else if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
 85.3107 +      {
 85.3108 +         if (max_pixel_depth < 8)
 85.3109 +            max_pixel_depth = 8;
 85.3110 +         if (png_ptr->num_trans)
 85.3111 +            max_pixel_depth *= 2;
 85.3112 +      }
 85.3113 +      else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
 85.3114 +      {
 85.3115 +         if (png_ptr->num_trans)
 85.3116 +         {
 85.3117 +            max_pixel_depth *= 4;
 85.3118 +            max_pixel_depth /= 3;
 85.3119 +         }
 85.3120 +      }
 85.3121 +   }
 85.3122 +#endif
 85.3123 +
 85.3124 +#if defined(PNG_READ_FILLER_SUPPORTED)
 85.3125 +   if (png_ptr->transformations & (PNG_FILLER))
 85.3126 +   {
 85.3127 +      if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
 85.3128 +         max_pixel_depth = 32;
 85.3129 +      else if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
 85.3130 +      {
 85.3131 +         if (max_pixel_depth <= 8)
 85.3132 +            max_pixel_depth = 16;
 85.3133 +         else
 85.3134 +            max_pixel_depth = 32;
 85.3135 +      }
 85.3136 +      else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
 85.3137 +      {
 85.3138 +         if (max_pixel_depth <= 32)
 85.3139 +            max_pixel_depth = 32;
 85.3140 +         else
 85.3141 +            max_pixel_depth = 64;
 85.3142 +      }
 85.3143 +   }
 85.3144 +#endif
 85.3145 +
 85.3146 +#if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED)
 85.3147 +   if (png_ptr->transformations & PNG_GRAY_TO_RGB)
 85.3148 +   {
 85.3149 +      if (
 85.3150 +#if defined(PNG_READ_EXPAND_SUPPORTED)
 85.3151 +        (png_ptr->num_trans && (png_ptr->transformations & PNG_EXPAND)) ||
 85.3152 +#endif
 85.3153 +#if defined(PNG_READ_FILLER_SUPPORTED)
 85.3154 +        (png_ptr->transformations & (PNG_FILLER)) ||
 85.3155 +#endif
 85.3156 +        png_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
 85.3157 +      {
 85.3158 +         if (max_pixel_depth <= 16)
 85.3159 +            max_pixel_depth = 32;
 85.3160 +         else
 85.3161 +            max_pixel_depth = 64;
 85.3162 +      }
 85.3163 +      else
 85.3164 +      {
 85.3165 +         if (max_pixel_depth <= 8)
 85.3166 +           {
 85.3167 +             if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
 85.3168 +               max_pixel_depth = 32;
 85.3169 +             else
 85.3170 +               max_pixel_depth = 24;
 85.3171 +           }
 85.3172 +         else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
 85.3173 +            max_pixel_depth = 64;
 85.3174 +         else
 85.3175 +            max_pixel_depth = 48;
 85.3176 +      }
 85.3177 +   }
 85.3178 +#endif
 85.3179 +
 85.3180 +#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) && \
 85.3181 +defined(PNG_USER_TRANSFORM_PTR_SUPPORTED)
 85.3182 +   if (png_ptr->transformations & PNG_USER_TRANSFORM)
 85.3183 +     {
 85.3184 +       int user_pixel_depth = png_ptr->user_transform_depth*
 85.3185 +         png_ptr->user_transform_channels;
 85.3186 +       if (user_pixel_depth > max_pixel_depth)
 85.3187 +         max_pixel_depth=user_pixel_depth;
 85.3188 +     }
 85.3189 +#endif
 85.3190 +
 85.3191 +   /* align the width on the next larger 8 pixels.  Mainly used
 85.3192 +      for interlacing */
 85.3193 +   row_bytes = ((png_ptr->width + 7) & ~((png_uint_32)7));
 85.3194 +   /* calculate the maximum bytes needed, adding a byte and a pixel
 85.3195 +      for safety's sake */
 85.3196 +   row_bytes = PNG_ROWBYTES(max_pixel_depth, row_bytes) +
 85.3197 +      1 + ((max_pixel_depth + 7) >> 3);
 85.3198 +#ifdef PNG_MAX_MALLOC_64K
 85.3199 +   if (row_bytes > (png_uint_32)65536L)
 85.3200 +      png_error(png_ptr, "This image requires a row greater than 64KB");
 85.3201 +#endif
 85.3202 +
 85.3203 +   if (row_bytes + 64 > png_ptr->old_big_row_buf_size)
 85.3204 +   {
 85.3205 +     png_free(png_ptr, png_ptr->big_row_buf);
 85.3206 +     png_ptr->big_row_buf = (png_bytep)png_malloc(png_ptr, row_bytes+64);
 85.3207 +     png_ptr->row_buf = png_ptr->big_row_buf+32;
 85.3208 +     png_ptr->old_big_row_buf_size = row_bytes+64;
 85.3209 +   }
 85.3210 +
 85.3211 +#ifdef PNG_MAX_MALLOC_64K
 85.3212 +   if ((png_uint_32)png_ptr->rowbytes + 1 > (png_uint_32)65536L)
 85.3213 +      png_error(png_ptr, "This image requires a row greater than 64KB");
 85.3214 +#endif
 85.3215 +   if ((png_uint_32)png_ptr->rowbytes > (png_uint_32)(PNG_SIZE_MAX - 1))
 85.3216 +      png_error(png_ptr, "Row has too many bytes to allocate in memory.");
 85.3217 +
 85.3218 +   if (png_ptr->rowbytes+1 > png_ptr->old_prev_row_size)
 85.3219 +   {
 85.3220 +     png_free(png_ptr, png_ptr->prev_row);
 85.3221 +     png_ptr->prev_row = (png_bytep)png_malloc(png_ptr, (png_uint_32)(
 85.3222 +        png_ptr->rowbytes + 1));
 85.3223 +     png_ptr->old_prev_row_size = png_ptr->rowbytes+1;
 85.3224 +   }
 85.3225 +
 85.3226 +   png_memset_check(png_ptr, png_ptr->prev_row, 0, png_ptr->rowbytes + 1);
 85.3227 +
 85.3228 +   png_debug1(3, "width = %lu,\n", png_ptr->width);
 85.3229 +   png_debug1(3, "height = %lu,\n", png_ptr->height);
 85.3230 +   png_debug1(3, "iwidth = %lu,\n", png_ptr->iwidth);
 85.3231 +   png_debug1(3, "num_rows = %lu\n", png_ptr->num_rows);
 85.3232 +   png_debug1(3, "rowbytes = %lu,\n", png_ptr->rowbytes);
 85.3233 +   png_debug1(3, "irowbytes = %lu,\n", png_ptr->irowbytes);
 85.3234 +
 85.3235 +   png_ptr->flags |= PNG_FLAG_ROW_INIT;
 85.3236 +}
 85.3237 +#endif /* PNG_READ_SUPPORTED */
    86.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    86.2 +++ b/libs/libpng/pngset.c	Sat Sep 19 05:51:51 2015 +0300
    86.3 @@ -0,0 +1,1293 @@
    86.4 +
    86.5 +/* pngset.c - storage of image information into info struct
    86.6 + *
    86.7 + * Last changed in libpng 1.2.30 [August 15, 2008]
    86.8 + * For conditions of distribution and use, see copyright notice in png.h
    86.9 + * Copyright (c) 1998-2008 Glenn Randers-Pehrson
   86.10 + * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
   86.11 + * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
   86.12 + *
   86.13 + * The functions here are used during reads to store data from the file
   86.14 + * into the info struct, and during writes to store application data
   86.15 + * into the info struct for writing into the file.  This abstracts the
   86.16 + * info struct and allows us to change the structure in the future.
   86.17 + */
   86.18 +
   86.19 +#define PNG_INTERNAL
   86.20 +#include "png.h"
   86.21 +#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
   86.22 +
   86.23 +#if defined(PNG_bKGD_SUPPORTED)
   86.24 +void PNGAPI
   86.25 +png_set_bKGD(png_structp png_ptr, png_infop info_ptr, png_color_16p background)
   86.26 +{
   86.27 +   png_debug1(1, "in %s storage function\n", "bKGD");
   86.28 +   if (png_ptr == NULL || info_ptr == NULL)
   86.29 +      return;
   86.30 +
   86.31 +   png_memcpy(&(info_ptr->background), background, png_sizeof(png_color_16));
   86.32 +   info_ptr->valid |= PNG_INFO_bKGD;
   86.33 +}
   86.34 +#endif
   86.35 +
   86.36 +#if defined(PNG_cHRM_SUPPORTED)
   86.37 +#ifdef PNG_FLOATING_POINT_SUPPORTED
   86.38 +void PNGAPI
   86.39 +png_set_cHRM(png_structp png_ptr, png_infop info_ptr,
   86.40 +   double white_x, double white_y, double red_x, double red_y,
   86.41 +   double green_x, double green_y, double blue_x, double blue_y)
   86.42 +{
   86.43 +   png_debug1(1, "in %s storage function\n", "cHRM");
   86.44 +   if (png_ptr == NULL || info_ptr == NULL)
   86.45 +      return;
   86.46 +   if (!(white_x || white_y || red_x || red_y || green_x || green_y ||
   86.47 +       blue_x || blue_y))
   86.48 +   {
   86.49 +      png_warning(png_ptr,
   86.50 +        "Ignoring attempt to set all-zero chromaticity values");
   86.51 +      return;
   86.52 +   }
   86.53 +   if (white_x < 0.0 || white_y < 0.0 ||
   86.54 +         red_x < 0.0 ||   red_y < 0.0 ||
   86.55 +       green_x < 0.0 || green_y < 0.0 ||
   86.56 +        blue_x < 0.0 ||  blue_y < 0.0)
   86.57 +   {
   86.58 +      png_warning(png_ptr,
   86.59 +        "Ignoring attempt to set negative chromaticity value");
   86.60 +      return;
   86.61 +   }
   86.62 +   if (white_x > 21474.83 || white_y > 21474.83 ||
   86.63 +         red_x > 21474.83 ||   red_y > 21474.83 ||
   86.64 +       green_x > 21474.83 || green_y > 21474.83 ||
   86.65 +        blue_x > 21474.83 ||  blue_y > 21474.83)
   86.66 +   {
   86.67 +      png_warning(png_ptr,
   86.68 +        "Ignoring attempt to set chromaticity value exceeding 21474.83");
   86.69 +      return;
   86.70 +   }
   86.71 +
   86.72 +   info_ptr->x_white = (float)white_x;
   86.73 +   info_ptr->y_white = (float)white_y;
   86.74 +   info_ptr->x_red   = (float)red_x;
   86.75 +   info_ptr->y_red   = (float)red_y;
   86.76 +   info_ptr->x_green = (float)green_x;
   86.77 +   info_ptr->y_green = (float)green_y;
   86.78 +   info_ptr->x_blue  = (float)blue_x;
   86.79 +   info_ptr->y_blue  = (float)blue_y;
   86.80 +#ifdef PNG_FIXED_POINT_SUPPORTED
   86.81 +   info_ptr->int_x_white = (png_fixed_point)(white_x*100000.+0.5);
   86.82 +   info_ptr->int_y_white = (png_fixed_point)(white_y*100000.+0.5);
   86.83 +   info_ptr->int_x_red   = (png_fixed_point)(  red_x*100000.+0.5);
   86.84 +   info_ptr->int_y_red   = (png_fixed_point)(  red_y*100000.+0.5);
   86.85 +   info_ptr->int_x_green = (png_fixed_point)(green_x*100000.+0.5);
   86.86 +   info_ptr->int_y_green = (png_fixed_point)(green_y*100000.+0.5);
   86.87 +   info_ptr->int_x_blue  = (png_fixed_point)( blue_x*100000.+0.5);
   86.88 +   info_ptr->int_y_blue  = (png_fixed_point)( blue_y*100000.+0.5);
   86.89 +#endif
   86.90 +   info_ptr->valid |= PNG_INFO_cHRM;
   86.91 +}
   86.92 +#endif
   86.93 +#ifdef PNG_FIXED_POINT_SUPPORTED
   86.94 +void PNGAPI
   86.95 +png_set_cHRM_fixed(png_structp png_ptr, png_infop info_ptr,
   86.96 +   png_fixed_point white_x, png_fixed_point white_y, png_fixed_point red_x,
   86.97 +   png_fixed_point red_y, png_fixed_point green_x, png_fixed_point green_y,
   86.98 +   png_fixed_point blue_x, png_fixed_point blue_y)
   86.99 +{
  86.100 +   png_debug1(1, "in %s storage function\n", "cHRM");
  86.101 +   if (png_ptr == NULL || info_ptr == NULL)
  86.102 +      return;
  86.103 +
  86.104 +   if (!(white_x || white_y || red_x || red_y || green_x || green_y ||
  86.105 +       blue_x || blue_y))
  86.106 +   {
  86.107 +      png_warning(png_ptr,
  86.108 +        "Ignoring attempt to set all-zero chromaticity values");
  86.109 +      return;
  86.110 +   }
  86.111 +   if (white_x < 0 || white_y < 0 ||
  86.112 +         red_x < 0 ||   red_y < 0 ||
  86.113 +       green_x < 0 || green_y < 0 ||
  86.114 +        blue_x < 0 ||  blue_y < 0)
  86.115 +   {
  86.116 +      png_warning(png_ptr,
  86.117 +        "Ignoring attempt to set negative chromaticity value");
  86.118 +      return;
  86.119 +   }
  86.120 +   if (white_x > (png_fixed_point) PNG_UINT_31_MAX ||
  86.121 +       white_y > (png_fixed_point) PNG_UINT_31_MAX ||
  86.122 +         red_x > (png_fixed_point) PNG_UINT_31_MAX ||
  86.123 +         red_y > (png_fixed_point) PNG_UINT_31_MAX ||
  86.124 +       green_x > (png_fixed_point) PNG_UINT_31_MAX ||
  86.125 +       green_y > (png_fixed_point) PNG_UINT_31_MAX ||
  86.126 +        blue_x > (png_fixed_point) PNG_UINT_31_MAX ||
  86.127 +        blue_y > (png_fixed_point) PNG_UINT_31_MAX )
  86.128 +   {
  86.129 +      png_warning(png_ptr,
  86.130 +        "Ignoring attempt to set chromaticity value exceeding 21474.83");
  86.131 +      return;
  86.132 +   }
  86.133 +   info_ptr->int_x_white = white_x;
  86.134 +   info_ptr->int_y_white = white_y;
  86.135 +   info_ptr->int_x_red   = red_x;
  86.136 +   info_ptr->int_y_red   = red_y;
  86.137 +   info_ptr->int_x_green = green_x;
  86.138 +   info_ptr->int_y_green = green_y;
  86.139 +   info_ptr->int_x_blue  = blue_x;
  86.140 +   info_ptr->int_y_blue  = blue_y;
  86.141 +#ifdef PNG_FLOATING_POINT_SUPPORTED
  86.142 +   info_ptr->x_white = (float)(white_x/100000.);
  86.143 +   info_ptr->y_white = (float)(white_y/100000.);
  86.144 +   info_ptr->x_red   = (float)(  red_x/100000.);
  86.145 +   info_ptr->y_red   = (float)(  red_y/100000.);
  86.146 +   info_ptr->x_green = (float)(green_x/100000.);
  86.147 +   info_ptr->y_green = (float)(green_y/100000.);
  86.148 +   info_ptr->x_blue  = (float)( blue_x/100000.);
  86.149 +   info_ptr->y_blue  = (float)( blue_y/100000.);
  86.150 +#endif
  86.151 +   info_ptr->valid |= PNG_INFO_cHRM;
  86.152 +}
  86.153 +#endif
  86.154 +#endif
  86.155 +
  86.156 +#if defined(PNG_gAMA_SUPPORTED)
  86.157 +#ifdef PNG_FLOATING_POINT_SUPPORTED
  86.158 +void PNGAPI
  86.159 +png_set_gAMA(png_structp png_ptr, png_infop info_ptr, double file_gamma)
  86.160 +{
  86.161 +   double gamma;
  86.162 +   png_debug1(1, "in %s storage function\n", "gAMA");
  86.163 +   if (png_ptr == NULL || info_ptr == NULL)
  86.164 +      return;
  86.165 +
  86.166 +   /* Check for overflow */
  86.167 +   if (file_gamma > 21474.83)
  86.168 +   {
  86.169 +      png_warning(png_ptr, "Limiting gamma to 21474.83");
  86.170 +      gamma=21474.83;
  86.171 +   }
  86.172 +   else
  86.173 +      gamma = file_gamma;
  86.174 +   info_ptr->gamma = (float)gamma;
  86.175 +#ifdef PNG_FIXED_POINT_SUPPORTED
  86.176 +   info_ptr->int_gamma = (int)(gamma*100000.+.5);
  86.177 +#endif
  86.178 +   info_ptr->valid |= PNG_INFO_gAMA;
  86.179 +   if (gamma == 0.0)
  86.180 +      png_warning(png_ptr, "Setting gamma=0");
  86.181 +}
  86.182 +#endif
  86.183 +void PNGAPI
  86.184 +png_set_gAMA_fixed(png_structp png_ptr, png_infop info_ptr, png_fixed_point
  86.185 +   int_gamma)
  86.186 +{
  86.187 +   png_fixed_point gamma;
  86.188 +
  86.189 +   png_debug1(1, "in %s storage function\n", "gAMA");
  86.190 +   if (png_ptr == NULL || info_ptr == NULL)
  86.191 +      return;
  86.192 +
  86.193 +   if (int_gamma > (png_fixed_point) PNG_UINT_31_MAX)
  86.194 +   {
  86.195 +     png_warning(png_ptr, "Limiting gamma to 21474.83");
  86.196 +     gamma=PNG_UINT_31_MAX;
  86.197 +   }
  86.198 +   else
  86.199 +   {
  86.200 +     if (int_gamma < 0)
  86.201 +     {
  86.202 +       png_warning(png_ptr, "Setting negative gamma to zero");
  86.203 +       gamma = 0;
  86.204 +     }
  86.205 +     else
  86.206 +       gamma = int_gamma;
  86.207 +   }
  86.208 +#ifdef PNG_FLOATING_POINT_SUPPORTED
  86.209 +   info_ptr->gamma = (float)(gamma/100000.);
  86.210 +#endif
  86.211 +#ifdef PNG_FIXED_POINT_SUPPORTED
  86.212 +   info_ptr->int_gamma = gamma;
  86.213 +#endif
  86.214 +   info_ptr->valid |= PNG_INFO_gAMA;
  86.215 +   if (gamma == 0)
  86.216 +      png_warning(png_ptr, "Setting gamma=0");
  86.217 +}
  86.218 +#endif
  86.219 +
  86.220 +#if defined(PNG_hIST_SUPPORTED)
  86.221 +void PNGAPI
  86.222 +png_set_hIST(png_structp png_ptr, png_infop info_ptr, png_uint_16p hist)
  86.223 +{
  86.224 +   int i;
  86.225 +
  86.226 +   png_debug1(1, "in %s storage function\n", "hIST");
  86.227 +   if (png_ptr == NULL || info_ptr == NULL)
  86.228 +      return;
  86.229 +   if (info_ptr->num_palette == 0 || info_ptr->num_palette
  86.230 +       > PNG_MAX_PALETTE_LENGTH)
  86.231 +   {
  86.232 +       png_warning(png_ptr,
  86.233 +          "Invalid palette size, hIST allocation skipped.");
  86.234 +       return;
  86.235 +   }
  86.236 +
  86.237 +#ifdef PNG_FREE_ME_SUPPORTED
  86.238 +   png_free_data(png_ptr, info_ptr, PNG_FREE_HIST, 0);
  86.239 +#endif
  86.240 +   /* Changed from info->num_palette to PNG_MAX_PALETTE_LENGTH in version
  86.241 +      1.2.1 */
  86.242 +   png_ptr->hist = (png_uint_16p)png_malloc_warn(png_ptr,
  86.243 +      (png_uint_32)(PNG_MAX_PALETTE_LENGTH * png_sizeof(png_uint_16)));
  86.244 +   if (png_ptr->hist == NULL)
  86.245 +     {
  86.246 +       png_warning(png_ptr, "Insufficient memory for hIST chunk data.");
  86.247 +       return;
  86.248 +     }
  86.249 +
  86.250 +   for (i = 0; i < info_ptr->num_palette; i++)
  86.251 +       png_ptr->hist[i] = hist[i];
  86.252 +   info_ptr->hist = png_ptr->hist;
  86.253 +   info_ptr->valid |= PNG_INFO_hIST;
  86.254 +
  86.255 +#ifdef PNG_FREE_ME_SUPPORTED
  86.256 +   info_ptr->free_me |= PNG_FREE_HIST;
  86.257 +#else
  86.258 +   png_ptr->flags |= PNG_FLAG_FREE_HIST;
  86.259 +#endif
  86.260 +}
  86.261 +#endif
  86.262 +
  86.263 +void PNGAPI
  86.264 +png_set_IHDR(png_structp png_ptr, png_infop info_ptr,
  86.265 +   png_uint_32 width, png_uint_32 height, int bit_depth,
  86.266 +   int color_type, int interlace_type, int compression_type,
  86.267 +   int filter_type)
  86.268 +{
  86.269 +   png_debug1(1, "in %s storage function\n", "IHDR");
  86.270 +   if (png_ptr == NULL || info_ptr == NULL)
  86.271 +      return;
  86.272 +
  86.273 +   /* check for width and height valid values */
  86.274 +   if (width == 0 || height == 0)
  86.275 +      png_error(png_ptr, "Image width or height is zero in IHDR");
  86.276 +#ifdef PNG_SET_USER_LIMITS_SUPPORTED
  86.277 +   if (width > png_ptr->user_width_max || height > png_ptr->user_height_max)
  86.278 +      png_error(png_ptr, "image size exceeds user limits in IHDR");
  86.279 +#else
  86.280 +   if (width > PNG_USER_WIDTH_MAX || height > PNG_USER_HEIGHT_MAX)
  86.281 +      png_error(png_ptr, "image size exceeds user limits in IHDR");
  86.282 +#endif
  86.283 +   if (width > PNG_UINT_31_MAX || height > PNG_UINT_31_MAX)
  86.284 +      png_error(png_ptr, "Invalid image size in IHDR");
  86.285 +   if ( width > (PNG_UINT_32_MAX
  86.286 +                 >> 3)      /* 8-byte RGBA pixels */
  86.287 +                 - 64       /* bigrowbuf hack */
  86.288 +                 - 1        /* filter byte */
  86.289 +                 - 7*8      /* rounding of width to multiple of 8 pixels */
  86.290 +                 - 8)       /* extra max_pixel_depth pad */
  86.291 +      png_warning(png_ptr, "Width is too large for libpng to process pixels");
  86.292 +
  86.293 +   /* check other values */
  86.294 +   if (bit_depth != 1 && bit_depth != 2 && bit_depth != 4 &&
  86.295 +      bit_depth != 8 && bit_depth != 16)
  86.296 +      png_error(png_ptr, "Invalid bit depth in IHDR");
  86.297 +
  86.298 +   if (color_type < 0 || color_type == 1 ||
  86.299 +      color_type == 5 || color_type > 6)
  86.300 +      png_error(png_ptr, "Invalid color type in IHDR");
  86.301 +
  86.302 +   if (((color_type == PNG_COLOR_TYPE_PALETTE) && bit_depth > 8) ||
  86.303 +       ((color_type == PNG_COLOR_TYPE_RGB ||
  86.304 +         color_type == PNG_COLOR_TYPE_GRAY_ALPHA ||
  86.305 +         color_type == PNG_COLOR_TYPE_RGB_ALPHA) && bit_depth < 8))
  86.306 +      png_error(png_ptr, "Invalid color type/bit depth combination in IHDR");
  86.307 +
  86.308 +   if (interlace_type >= PNG_INTERLACE_LAST)
  86.309 +      png_error(png_ptr, "Unknown interlace method in IHDR");
  86.310 +
  86.311 +   if (compression_type != PNG_COMPRESSION_TYPE_BASE)
  86.312 +      png_error(png_ptr, "Unknown compression method in IHDR");
  86.313 +
  86.314 +#if defined(PNG_MNG_FEATURES_SUPPORTED)
  86.315 +   /* Accept filter_method 64 (intrapixel differencing) only if
  86.316 +    * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
  86.317 +    * 2. Libpng did not read a PNG signature (this filter_method is only
  86.318 +    *    used in PNG datastreams that are embedded in MNG datastreams) and
  86.319 +    * 3. The application called png_permit_mng_features with a mask that
  86.320 +    *    included PNG_FLAG_MNG_FILTER_64 and
  86.321 +    * 4. The filter_method is 64 and
  86.322 +    * 5. The color_type is RGB or RGBA
  86.323 +    */
  86.324 +   if ((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE)&&png_ptr->mng_features_permitted)
  86.325 +      png_warning(png_ptr, "MNG features are not allowed in a PNG datastream");
  86.326 +   if (filter_type != PNG_FILTER_TYPE_BASE)
  86.327 +   {
  86.328 +     if (!((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
  86.329 +        (filter_type == PNG_INTRAPIXEL_DIFFERENCING) &&
  86.330 +        ((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE) == 0) &&
  86.331 +        (color_type == PNG_COLOR_TYPE_RGB ||
  86.332 +         color_type == PNG_COLOR_TYPE_RGB_ALPHA)))
  86.333 +        png_error(png_ptr, "Unknown filter method in IHDR");
  86.334 +     if (png_ptr->mode&PNG_HAVE_PNG_SIGNATURE)
  86.335 +        png_warning(png_ptr, "Invalid filter method in IHDR");
  86.336 +   }
  86.337 +#else
  86.338 +   if (filter_type != PNG_FILTER_TYPE_BASE)
  86.339 +      png_error(png_ptr, "Unknown filter method in IHDR");
  86.340 +#endif
  86.341 +
  86.342 +   info_ptr->width = width;
  86.343 +   info_ptr->height = height;
  86.344 +   info_ptr->bit_depth = (png_byte)bit_depth;
  86.345 +   info_ptr->color_type =(png_byte) color_type;
  86.346 +   info_ptr->compression_type = (png_byte)compression_type;
  86.347 +   info_ptr->filter_type = (png_byte)filter_type;
  86.348 +   info_ptr->interlace_type = (png_byte)interlace_type;
  86.349 +   if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  86.350 +      info_ptr->channels = 1;
  86.351 +   else if (info_ptr->color_type & PNG_COLOR_MASK_COLOR)
  86.352 +      info_ptr->channels = 3;
  86.353 +   else
  86.354 +      info_ptr->channels = 1;
  86.355 +   if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA)
  86.356 +      info_ptr->channels++;
  86.357 +   info_ptr->pixel_depth = (png_byte)(info_ptr->channels * info_ptr->bit_depth);
  86.358 +
  86.359 +   /* check for potential overflow */
  86.360 +   if (width > (PNG_UINT_32_MAX
  86.361 +                 >> 3)      /* 8-byte RGBA pixels */
  86.362 +                 - 64       /* bigrowbuf hack */
  86.363 +                 - 1        /* filter byte */
  86.364 +                 - 7*8      /* rounding of width to multiple of 8 pixels */
  86.365 +                 - 8)       /* extra max_pixel_depth pad */
  86.366 +      info_ptr->rowbytes = (png_size_t)0;
  86.367 +   else
  86.368 +      info_ptr->rowbytes = PNG_ROWBYTES(info_ptr->pixel_depth, width);
  86.369 +}
  86.370 +
  86.371 +#if defined(PNG_oFFs_SUPPORTED)
  86.372 +void PNGAPI
  86.373 +png_set_oFFs(png_structp png_ptr, png_infop info_ptr,
  86.374 +   png_int_32 offset_x, png_int_32 offset_y, int unit_type)
  86.375 +{
  86.376 +   png_debug1(1, "in %s storage function\n", "oFFs");
  86.377 +   if (png_ptr == NULL || info_ptr == NULL)
  86.378 +      return;
  86.379 +
  86.380 +   info_ptr->x_offset = offset_x;
  86.381 +   info_ptr->y_offset = offset_y;
  86.382 +   info_ptr->offset_unit_type = (png_byte)unit_type;
  86.383 +   info_ptr->valid |= PNG_INFO_oFFs;
  86.384 +}
  86.385 +#endif
  86.386 +
  86.387 +#if defined(PNG_pCAL_SUPPORTED)
  86.388 +void PNGAPI
  86.389 +png_set_pCAL(png_structp png_ptr, png_infop info_ptr,
  86.390 +   png_charp purpose, png_int_32 X0, png_int_32 X1, int type, int nparams,
  86.391 +   png_charp units, png_charpp params)
  86.392 +{
  86.393 +   png_uint_32 length;
  86.394 +   int i;
  86.395 +
  86.396 +   png_debug1(1, "in %s storage function\n", "pCAL");
  86.397 +   if (png_ptr == NULL || info_ptr == NULL)
  86.398 +      return;
  86.399 +
  86.400 +   length = png_strlen(purpose) + 1;
  86.401 +   png_debug1(3, "allocating purpose for info (%lu bytes)\n",
  86.402 +     (unsigned long)length);
  86.403 +   info_ptr->pcal_purpose = (png_charp)png_malloc_warn(png_ptr, length);
  86.404 +   if (info_ptr->pcal_purpose == NULL)
  86.405 +   {
  86.406 +       png_warning(png_ptr, "Insufficient memory for pCAL purpose.");
  86.407 +      return;
  86.408 +   }
  86.409 +   png_memcpy(info_ptr->pcal_purpose, purpose, (png_size_t)length);
  86.410 +
  86.411 +   png_debug(3, "storing X0, X1, type, and nparams in info\n");
  86.412 +   info_ptr->pcal_X0 = X0;
  86.413 +   info_ptr->pcal_X1 = X1;
  86.414 +   info_ptr->pcal_type = (png_byte)type;
  86.415 +   info_ptr->pcal_nparams = (png_byte)nparams;
  86.416 +
  86.417 +   length = png_strlen(units) + 1;
  86.418 +   png_debug1(3, "allocating units for info (%lu bytes)\n",
  86.419 +     (unsigned long)length);
  86.420 +   info_ptr->pcal_units = (png_charp)png_malloc_warn(png_ptr, length);
  86.421 +   if (info_ptr->pcal_units == NULL)
  86.422 +   {
  86.423 +       png_warning(png_ptr, "Insufficient memory for pCAL units.");
  86.424 +      return;
  86.425 +   }
  86.426 +   png_memcpy(info_ptr->pcal_units, units, (png_size_t)length);
  86.427 +
  86.428 +   info_ptr->pcal_params = (png_charpp)png_malloc_warn(png_ptr,
  86.429 +      (png_uint_32)((nparams + 1) * png_sizeof(png_charp)));
  86.430 +   if (info_ptr->pcal_params == NULL)
  86.431 +   {
  86.432 +       png_warning(png_ptr, "Insufficient memory for pCAL params.");
  86.433 +      return;
  86.434 +   }
  86.435 +
  86.436 +   info_ptr->pcal_params[nparams] = NULL;
  86.437 +
  86.438 +   for (i = 0; i < nparams; i++)
  86.439 +   {
  86.440 +      length = png_strlen(params[i]) + 1;
  86.441 +      png_debug2(3, "allocating parameter %d for info (%lu bytes)\n", i,
  86.442 +        (unsigned long)length);
  86.443 +      info_ptr->pcal_params[i] = (png_charp)png_malloc_warn(png_ptr, length);
  86.444 +      if (info_ptr->pcal_params[i] == NULL)
  86.445 +      {
  86.446 +          png_warning(png_ptr, "Insufficient memory for pCAL parameter.");
  86.447 +          return;
  86.448 +      }
  86.449 +      png_memcpy(info_ptr->pcal_params[i], params[i], (png_size_t)length);
  86.450 +   }
  86.451 +
  86.452 +   info_ptr->valid |= PNG_INFO_pCAL;
  86.453 +#ifdef PNG_FREE_ME_SUPPORTED
  86.454 +   info_ptr->free_me |= PNG_FREE_PCAL;
  86.455 +#endif
  86.456 +}
  86.457 +#endif
  86.458 +
  86.459 +#if defined(PNG_READ_sCAL_SUPPORTED) || defined(PNG_WRITE_sCAL_SUPPORTED)
  86.460 +#ifdef PNG_FLOATING_POINT_SUPPORTED
  86.461 +void PNGAPI
  86.462 +png_set_sCAL(png_structp png_ptr, png_infop info_ptr,
  86.463 +             int unit, double width, double height)
  86.464 +{
  86.465 +   png_debug1(1, "in %s storage function\n", "sCAL");
  86.466 +   if (png_ptr == NULL || info_ptr == NULL)
  86.467 +      return;
  86.468 +
  86.469 +   info_ptr->scal_unit = (png_byte)unit;
  86.470 +   info_ptr->scal_pixel_width = width;
  86.471 +   info_ptr->scal_pixel_height = height;
  86.472 +
  86.473 +   info_ptr->valid |= PNG_INFO_sCAL;
  86.474 +}
  86.475 +#else
  86.476 +#ifdef PNG_FIXED_POINT_SUPPORTED
  86.477 +void PNGAPI
  86.478 +png_set_sCAL_s(png_structp png_ptr, png_infop info_ptr,
  86.479 +             int unit, png_charp swidth, png_charp sheight)
  86.480 +{
  86.481 +   png_uint_32 length;
  86.482 +
  86.483 +   png_debug1(1, "in %s storage function\n", "sCAL");
  86.484 +   if (png_ptr == NULL || info_ptr == NULL)
  86.485 +      return;
  86.486 +
  86.487 +   info_ptr->scal_unit = (png_byte)unit;
  86.488 +
  86.489 +   length = png_strlen(swidth) + 1;
  86.490 +   png_debug1(3, "allocating unit for info (%u bytes)\n",
  86.491 +      (unsigned int)length);
  86.492 +   info_ptr->scal_s_width = (png_charp)png_malloc_warn(png_ptr, length);
  86.493 +   if (info_ptr->scal_s_width == NULL)
  86.494 +   {
  86.495 +      png_warning(png_ptr,
  86.496 +       "Memory allocation failed while processing sCAL.");
  86.497 +      return;
  86.498 +   }
  86.499 +   png_memcpy(info_ptr->scal_s_width, swidth, (png_size_t)length);
  86.500 +
  86.501 +   length = png_strlen(sheight) + 1;
  86.502 +   png_debug1(3, "allocating unit for info (%u bytes)\n",
  86.503 +      (unsigned int)length);
  86.504 +   info_ptr->scal_s_height = (png_charp)png_malloc_warn(png_ptr, length);
  86.505 +   if (info_ptr->scal_s_height == NULL)
  86.506 +   {
  86.507 +      png_free (png_ptr, info_ptr->scal_s_width);
  86.508 +      info_ptr->scal_s_width = NULL;
  86.509 +      png_warning(png_ptr,
  86.510 +       "Memory allocation failed while processing sCAL.");
  86.511 +      return;
  86.512 +   }
  86.513 +   png_memcpy(info_ptr->scal_s_height, sheight, (png_size_t)length);
  86.514 +   info_ptr->valid |= PNG_INFO_sCAL;
  86.515 +#ifdef PNG_FREE_ME_SUPPORTED
  86.516 +   info_ptr->free_me |= PNG_FREE_SCAL;
  86.517 +#endif
  86.518 +}
  86.519 +#endif
  86.520 +#endif
  86.521 +#endif
  86.522 +
  86.523 +#if defined(PNG_pHYs_SUPPORTED)
  86.524 +void PNGAPI
  86.525 +png_set_pHYs(png_structp png_ptr, png_infop info_ptr,
  86.526 +   png_uint_32 res_x, png_uint_32 res_y, int unit_type)
  86.527 +{
  86.528 +   png_debug1(1, "in %s storage function\n", "pHYs");
  86.529 +   if (png_ptr == NULL || info_ptr == NULL)
  86.530 +      return;
  86.531 +
  86.532 +   info_ptr->x_pixels_per_unit = res_x;
  86.533 +   info_ptr->y_pixels_per_unit = res_y;
  86.534 +   info_ptr->phys_unit_type = (png_byte)unit_type;
  86.535 +   info_ptr->valid |= PNG_INFO_pHYs;
  86.536 +}
  86.537 +#endif
  86.538 +
  86.539 +void PNGAPI
  86.540 +png_set_PLTE(png_structp png_ptr, png_infop info_ptr,
  86.541 +   png_colorp palette, int num_palette)
  86.542 +{
  86.543 +
  86.544 +   png_debug1(1, "in %s storage function\n", "PLTE");
  86.545 +   if (png_ptr == NULL || info_ptr == NULL)
  86.546 +      return;
  86.547 +
  86.548 +   if (num_palette < 0 || num_palette > PNG_MAX_PALETTE_LENGTH)
  86.549 +     {
  86.550 +       if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  86.551 +         png_error(png_ptr, "Invalid palette length");
  86.552 +       else
  86.553 +       {
  86.554 +         png_warning(png_ptr, "Invalid palette length");
  86.555 +         return;
  86.556 +       }
  86.557 +     }
  86.558 +
  86.559 +   /*
  86.560 +    * It may not actually be necessary to set png_ptr->palette here;
  86.561 +    * we do it for backward compatibility with the way the png_handle_tRNS
  86.562 +    * function used to do the allocation.
  86.563 +    */
  86.564 +#ifdef PNG_FREE_ME_SUPPORTED
  86.565 +   png_free_data(png_ptr, info_ptr, PNG_FREE_PLTE, 0);
  86.566 +#endif
  86.567 +
  86.568 +   /* Changed in libpng-1.2.1 to allocate PNG_MAX_PALETTE_LENGTH instead
  86.569 +      of num_palette entries,
  86.570 +      in case of an invalid PNG file that has too-large sample values. */
  86.571 +   png_ptr->palette = (png_colorp)png_malloc(png_ptr,
  86.572 +      PNG_MAX_PALETTE_LENGTH * png_sizeof(png_color));
  86.573 +   png_memset(png_ptr->palette, 0, PNG_MAX_PALETTE_LENGTH *
  86.574 +      png_sizeof(png_color));
  86.575 +   png_memcpy(png_ptr->palette, palette, num_palette * png_sizeof(png_color));
  86.576 +   info_ptr->palette = png_ptr->palette;
  86.577 +   info_ptr->num_palette = png_ptr->num_palette = (png_uint_16)num_palette;
  86.578 +
  86.579 +#ifdef PNG_FREE_ME_SUPPORTED
  86.580 +   info_ptr->free_me |= PNG_FREE_PLTE;
  86.581 +#else
  86.582 +   png_ptr->flags |= PNG_FLAG_FREE_PLTE;
  86.583 +#endif
  86.584 +
  86.585 +   info_ptr->valid |= PNG_INFO_PLTE;
  86.586 +}
  86.587 +
  86.588 +#if defined(PNG_sBIT_SUPPORTED)
  86.589 +void PNGAPI
  86.590 +png_set_sBIT(png_structp png_ptr, png_infop info_ptr,
  86.591 +   png_color_8p sig_bit)
  86.592 +{
  86.593 +   png_debug1(1, "in %s storage function\n", "sBIT");
  86.594 +   if (png_ptr == NULL || info_ptr == NULL)
  86.595 +      return;
  86.596 +
  86.597 +   png_memcpy(&(info_ptr->sig_bit), sig_bit, png_sizeof(png_color_8));
  86.598 +   info_ptr->valid |= PNG_INFO_sBIT;
  86.599 +}
  86.600 +#endif
  86.601 +
  86.602 +#if defined(PNG_sRGB_SUPPORTED)
  86.603 +void PNGAPI
  86.604 +png_set_sRGB(png_structp png_ptr, png_infop info_ptr, int intent)
  86.605 +{
  86.606 +   png_debug1(1, "in %s storage function\n", "sRGB");
  86.607 +   if (png_ptr == NULL || info_ptr == NULL)
  86.608 +      return;
  86.609 +
  86.610 +   info_ptr->srgb_intent = (png_byte)intent;
  86.611 +   info_ptr->valid |= PNG_INFO_sRGB;
  86.612 +}
  86.613 +
  86.614 +void PNGAPI
  86.615 +png_set_sRGB_gAMA_and_cHRM(png_structp png_ptr, png_infop info_ptr,
  86.616 +   int intent)
  86.617 +{
  86.618 +#if defined(PNG_gAMA_SUPPORTED)
  86.619 +#ifdef PNG_FLOATING_POINT_SUPPORTED
  86.620 +   float file_gamma;
  86.621 +#endif
  86.622 +#ifdef PNG_FIXED_POINT_SUPPORTED
  86.623 +   png_fixed_point int_file_gamma;
  86.624 +#endif
  86.625 +#endif
  86.626 +#if defined(PNG_cHRM_SUPPORTED)
  86.627 +#ifdef PNG_FLOATING_POINT_SUPPORTED
  86.628 +   float white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y;
  86.629 +#endif
  86.630 +#ifdef PNG_FIXED_POINT_SUPPORTED
  86.631 +   png_fixed_point int_white_x, int_white_y, int_red_x, int_red_y, int_green_x,
  86.632 +      int_green_y, int_blue_x, int_blue_y;
  86.633 +#endif
  86.634 +#endif
  86.635 +   png_debug1(1, "in %s storage function\n", "sRGB_gAMA_and_cHRM");
  86.636 +   if (png_ptr == NULL || info_ptr == NULL)
  86.637 +      return;
  86.638 +
  86.639 +   png_set_sRGB(png_ptr, info_ptr, intent);
  86.640 +
  86.641 +#if defined(PNG_gAMA_SUPPORTED)
  86.642 +#ifdef PNG_FLOATING_POINT_SUPPORTED
  86.643 +   file_gamma = (float).45455;
  86.644 +   png_set_gAMA(png_ptr, info_ptr, file_gamma);
  86.645 +#endif
  86.646 +#ifdef PNG_FIXED_POINT_SUPPORTED
  86.647 +   int_file_gamma = 45455L;
  86.648 +   png_set_gAMA_fixed(png_ptr, info_ptr, int_file_gamma);
  86.649 +#endif
  86.650 +#endif
  86.651 +
  86.652 +#if defined(PNG_cHRM_SUPPORTED)
  86.653 +#ifdef PNG_FIXED_POINT_SUPPORTED
  86.654 +   int_white_x = 31270L;
  86.655 +   int_white_y = 32900L;
  86.656 +   int_red_x   = 64000L;
  86.657 +   int_red_y   = 33000L;
  86.658 +   int_green_x = 30000L;
  86.659 +   int_green_y = 60000L;
  86.660 +   int_blue_x  = 15000L;
  86.661 +   int_blue_y  =  6000L;
  86.662 +
  86.663 +   png_set_cHRM_fixed(png_ptr, info_ptr,
  86.664 +      int_white_x, int_white_y, int_red_x, int_red_y, int_green_x, int_green_y,
  86.665 +      int_blue_x, int_blue_y);
  86.666 +#endif
  86.667 +#ifdef PNG_FLOATING_POINT_SUPPORTED
  86.668 +   white_x = (float).3127;
  86.669 +   white_y = (float).3290;
  86.670 +   red_x   = (float).64;
  86.671 +   red_y   = (float).33;
  86.672 +   green_x = (float).30;
  86.673 +   green_y = (float).60;
  86.674 +   blue_x  = (float).15;
  86.675 +   blue_y  = (float).06;
  86.676 +
  86.677 +   png_set_cHRM(png_ptr, info_ptr,
  86.678 +      white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y);
  86.679 +#endif
  86.680 +#endif
  86.681 +}
  86.682 +#endif
  86.683 +
  86.684 +
  86.685 +#if defined(PNG_iCCP_SUPPORTED)
  86.686 +void PNGAPI
  86.687 +png_set_iCCP(png_structp png_ptr, png_infop info_ptr,
  86.688 +             png_charp name, int compression_type,
  86.689 +             png_charp profile, png_uint_32 proflen)
  86.690 +{
  86.691 +   png_charp new_iccp_name;
  86.692 +   png_charp new_iccp_profile;
  86.693 +   png_uint_32 length;
  86.694 +
  86.695 +   png_debug1(1, "in %s storage function\n", "iCCP");
  86.696 +   if (png_ptr == NULL || info_ptr == NULL || name == NULL || profile == NULL)
  86.697 +      return;
  86.698 +
  86.699 +   length = png_strlen(name)+1;
  86.700 +   new_iccp_name = (png_charp)png_malloc_warn(png_ptr, length);
  86.701 +   if (new_iccp_name == NULL)
  86.702 +   {
  86.703 +      png_warning(png_ptr, "Insufficient memory to process iCCP chunk.");
  86.704 +      return;
  86.705 +   }
  86.706 +   png_memcpy(new_iccp_name, name, length);
  86.707 +   new_iccp_profile = (png_charp)png_malloc_warn(png_ptr, proflen);
  86.708 +   if (new_iccp_profile == NULL)
  86.709 +   {
  86.710 +      png_free (png_ptr, new_iccp_name);
  86.711 +      png_warning(png_ptr,
  86.712 +      "Insufficient memory to process iCCP profile.");
  86.713 +      return;
  86.714 +   }
  86.715 +   png_memcpy(new_iccp_profile, profile, (png_size_t)proflen);
  86.716 +
  86.717 +   png_free_data(png_ptr, info_ptr, PNG_FREE_ICCP, 0);
  86.718 +
  86.719 +   info_ptr->iccp_proflen = proflen;
  86.720 +   info_ptr->iccp_name = new_iccp_name;
  86.721 +   info_ptr->iccp_profile = new_iccp_profile;
  86.722 +   /* Compression is always zero but is here so the API and info structure
  86.723 +    * does not have to change if we introduce multiple compression types */
  86.724 +   info_ptr->iccp_compression = (png_byte)compression_type;
  86.725 +#ifdef PNG_FREE_ME_SUPPORTED
  86.726 +   info_ptr->free_me |= PNG_FREE_ICCP;
  86.727 +#endif
  86.728 +   info_ptr->valid |= PNG_INFO_iCCP;
  86.729 +}
  86.730 +#endif
  86.731 +
  86.732 +#if defined(PNG_TEXT_SUPPORTED)
  86.733 +void PNGAPI
  86.734 +png_set_text(png_structp png_ptr, png_infop info_ptr, png_textp text_ptr,
  86.735 +   int num_text)
  86.736 +{
  86.737 +   int ret;
  86.738 +   ret = png_set_text_2(png_ptr, info_ptr, text_ptr, num_text);
  86.739 +   if (ret)
  86.740 +     png_error(png_ptr, "Insufficient memory to store text");
  86.741 +}
  86.742 +
  86.743 +int /* PRIVATE */
  86.744 +png_set_text_2(png_structp png_ptr, png_infop info_ptr, png_textp text_ptr,
  86.745 +   int num_text)
  86.746 +{
  86.747 +   int i;
  86.748 +
  86.749 +   png_debug1(1, "in %s storage function\n", (png_ptr->chunk_name[0] == '\0' ?
  86.750 +      "text" : (png_const_charp)png_ptr->chunk_name));
  86.751 +
  86.752 +   if (png_ptr == NULL || info_ptr == NULL || num_text == 0)
  86.753 +      return(0);
  86.754 +
  86.755 +   /* Make sure we have enough space in the "text" array in info_struct
  86.756 +    * to hold all of the incoming text_ptr objects.
  86.757 +    */
  86.758 +   if (info_ptr->num_text + num_text > info_ptr->max_text)
  86.759 +   {
  86.760 +      if (info_ptr->text != NULL)
  86.761 +      {
  86.762 +         png_textp old_text;
  86.763 +         int old_max;
  86.764 +
  86.765 +         old_max = info_ptr->max_text;
  86.766 +         info_ptr->max_text = info_ptr->num_text + num_text + 8;
  86.767 +         old_text = info_ptr->text;
  86.768 +         info_ptr->text = (png_textp)png_malloc_warn(png_ptr,
  86.769 +            (png_uint_32)(info_ptr->max_text * png_sizeof(png_text)));
  86.770 +         if (info_ptr->text == NULL)
  86.771 +           {
  86.772 +             png_free(png_ptr, old_text);
  86.773 +             return(1);
  86.774 +           }
  86.775 +         png_memcpy(info_ptr->text, old_text, (png_size_t)(old_max *
  86.776 +            png_sizeof(png_text)));
  86.777 +         png_free(png_ptr, old_text);
  86.778 +      }
  86.779 +      else
  86.780 +      {
  86.781 +         info_ptr->max_text = num_text + 8;
  86.782 +         info_ptr->num_text = 0;
  86.783 +         info_ptr->text = (png_textp)png_malloc_warn(png_ptr,
  86.784 +            (png_uint_32)(info_ptr->max_text * png_sizeof(png_text)));
  86.785 +         if (info_ptr->text == NULL)
  86.786 +           return(1);
  86.787 +#ifdef PNG_FREE_ME_SUPPORTED
  86.788 +         info_ptr->free_me |= PNG_FREE_TEXT;
  86.789 +#endif
  86.790 +      }
  86.791 +      png_debug1(3, "allocated %d entries for info_ptr->text\n",
  86.792 +         info_ptr->max_text);
  86.793 +   }
  86.794 +   for (i = 0; i < num_text; i++)
  86.795 +   {
  86.796 +      png_size_t text_length, key_len;
  86.797 +      png_size_t lang_len, lang_key_len;
  86.798 +      png_textp textp = &(info_ptr->text[info_ptr->num_text]);
  86.799 +
  86.800 +      if (text_ptr[i].key == NULL)
  86.801 +          continue;
  86.802 +
  86.803 +      key_len = png_strlen(text_ptr[i].key);
  86.804 +
  86.805 +      if (text_ptr[i].compression <= 0)
  86.806 +      {
  86.807 +        lang_len = 0;
  86.808 +        lang_key_len = 0;
  86.809 +      }
  86.810 +      else
  86.811 +#ifdef PNG_iTXt_SUPPORTED
  86.812 +      {
  86.813 +        /* set iTXt data */
  86.814 +        if (text_ptr[i].lang != NULL)
  86.815 +          lang_len = png_strlen(text_ptr[i].lang);
  86.816 +        else
  86.817 +          lang_len = 0;
  86.818 +        if (text_ptr[i].lang_key != NULL)
  86.819 +          lang_key_len = png_strlen(text_ptr[i].lang_key);
  86.820 +        else
  86.821 +          lang_key_len = 0;
  86.822 +      }
  86.823 +#else
  86.824 +      {
  86.825 +        png_warning(png_ptr, "iTXt chunk not supported.");
  86.826 +        continue;
  86.827 +      }
  86.828 +#endif
  86.829 +
  86.830 +      if (text_ptr[i].text == NULL || text_ptr[i].text[0] == '\0')
  86.831 +      {
  86.832 +         text_length = 0;
  86.833 +#ifdef PNG_iTXt_SUPPORTED
  86.834 +         if (text_ptr[i].compression > 0)
  86.835 +            textp->compression = PNG_ITXT_COMPRESSION_NONE;
  86.836 +         else
  86.837 +#endif
  86.838 +            textp->compression = PNG_TEXT_COMPRESSION_NONE;
  86.839 +      }
  86.840 +      else
  86.841 +      {
  86.842 +         text_length = png_strlen(text_ptr[i].text);
  86.843 +         textp->compression = text_ptr[i].compression;
  86.844 +      }
  86.845 +
  86.846 +      textp->key = (png_charp)png_malloc_warn(png_ptr,
  86.847 +         (png_uint_32)
  86.848 +         (key_len + text_length + lang_len + lang_key_len + 4));
  86.849 +      if (textp->key == NULL)
  86.850 +        return(1);
  86.851 +      png_debug2(2, "Allocated %lu bytes at %x in png_set_text\n",
  86.852 +         (png_uint_32)
  86.853 +         (key_len + lang_len + lang_key_len + text_length + 4),
  86.854 +         (int)textp->key);
  86.855 +
  86.856 +      png_memcpy(textp->key, text_ptr[i].key,
  86.857 +         (png_size_t)(key_len));
  86.858 +      *(textp->key + key_len) = '\0';
  86.859 +#ifdef PNG_iTXt_SUPPORTED
  86.860 +      if (text_ptr[i].compression > 0)
  86.861 +      {
  86.862 +         textp->lang = textp->key + key_len + 1;
  86.863 +         png_memcpy(textp->lang, text_ptr[i].lang, lang_len);
  86.864 +         *(textp->lang + lang_len) = '\0';
  86.865 +         textp->lang_key = textp->lang + lang_len + 1;
  86.866 +         png_memcpy(textp->lang_key, text_ptr[i].lang_key, lang_key_len);
  86.867 +         *(textp->lang_key + lang_key_len) = '\0';
  86.868 +         textp->text = textp->lang_key + lang_key_len + 1;
  86.869 +      }
  86.870 +      else
  86.871 +#endif
  86.872 +      {
  86.873 +#ifdef PNG_iTXt_SUPPORTED
  86.874 +         textp->lang=NULL;
  86.875 +         textp->lang_key=NULL;
  86.876 +#endif
  86.877 +         textp->text = textp->key + key_len + 1;
  86.878 +      }
  86.879 +      if (text_length)
  86.880 +         png_memcpy(textp->text, text_ptr[i].text,
  86.881 +            (png_size_t)(text_length));
  86.882 +      *(textp->text + text_length) = '\0';
  86.883 +
  86.884 +#ifdef PNG_iTXt_SUPPORTED
  86.885 +      if (textp->compression > 0)
  86.886 +      {
  86.887 +         textp->text_length = 0;
  86.888 +         textp->itxt_length = text_length;
  86.889 +      }
  86.890 +      else
  86.891 +#endif
  86.892 +      {
  86.893 +         textp->text_length = text_length;
  86.894 +#ifdef PNG_iTXt_SUPPORTED
  86.895 +         textp->itxt_length = 0;
  86.896 +#endif
  86.897 +      }
  86.898 +      info_ptr->num_text++;
  86.899 +      png_debug1(3, "transferred text chunk %d\n", info_ptr->num_text);
  86.900 +   }
  86.901 +   return(0);
  86.902 +}
  86.903 +#endif
  86.904 +
  86.905 +#if defined(PNG_tIME_SUPPORTED)
  86.906 +void PNGAPI
  86.907 +png_set_tIME(png_structp png_ptr, png_infop info_ptr, png_timep mod_time)
  86.908 +{
  86.909 +   png_debug1(1, "in %s storage function\n", "tIME");
  86.910 +   if (png_ptr == NULL || info_ptr == NULL ||
  86.911 +       (png_ptr->mode & PNG_WROTE_tIME))
  86.912 +      return;
  86.913 +
  86.914 +   png_memcpy(&(info_ptr->mod_time), mod_time, png_sizeof(png_time));
  86.915 +   info_ptr->valid |= PNG_INFO_tIME;
  86.916 +}
  86.917 +#endif
  86.918 +
  86.919 +#if defined(PNG_tRNS_SUPPORTED)
  86.920 +void PNGAPI
  86.921 +png_set_tRNS(png_structp png_ptr, png_infop info_ptr,
  86.922 +   png_bytep trans, int num_trans, png_color_16p trans_values)
  86.923 +{
  86.924 +   png_debug1(1, "in %s storage function\n", "tRNS");
  86.925 +   if (png_ptr == NULL || info_ptr == NULL)
  86.926 +      return;
  86.927 +
  86.928 +   if (trans != NULL)
  86.929 +   {
  86.930 +       /*
  86.931 +        * It may not actually be necessary to set png_ptr->trans here;
  86.932 +        * we do it for backward compatibility with the way the png_handle_tRNS
  86.933 +        * function used to do the allocation.
  86.934 +        */
  86.935 +
  86.936 +#ifdef PNG_FREE_ME_SUPPORTED
  86.937 +       png_free_data(png_ptr, info_ptr, PNG_FREE_TRNS, 0);
  86.938 +#endif
  86.939 +
  86.940 +       /* Changed from num_trans to PNG_MAX_PALETTE_LENGTH in version 1.2.1 */
  86.941 +       png_ptr->trans = info_ptr->trans = (png_bytep)png_malloc(png_ptr,
  86.942 +           (png_uint_32)PNG_MAX_PALETTE_LENGTH);
  86.943 +       if (num_trans > 0 && num_trans <= PNG_MAX_PALETTE_LENGTH)
  86.944 +         png_memcpy(info_ptr->trans, trans, (png_size_t)num_trans);
  86.945 +   }
  86.946 +
  86.947 +   if (trans_values != NULL)
  86.948 +   {
  86.949 +      int sample_max = (1 << info_ptr->bit_depth);
  86.950 +      if ((info_ptr->color_type == PNG_COLOR_TYPE_GRAY &&
  86.951 +          (int)trans_values->gray > sample_max) ||
  86.952 +          (info_ptr->color_type == PNG_COLOR_TYPE_RGB &&
  86.953 +          ((int)trans_values->red > sample_max ||
  86.954 +          (int)trans_values->green > sample_max ||
  86.955 +          (int)trans_values->blue > sample_max)))
  86.956 +        png_warning(png_ptr,
  86.957 +           "tRNS chunk has out-of-range samples for bit_depth");
  86.958 +      png_memcpy(&(info_ptr->trans_values), trans_values,
  86.959 +         png_sizeof(png_color_16));
  86.960 +      if (num_trans == 0)
  86.961 +        num_trans = 1;
  86.962 +   }
  86.963 +
  86.964 +   info_ptr->num_trans = (png_uint_16)num_trans;
  86.965 +   if (num_trans != 0)
  86.966 +   {
  86.967 +      info_ptr->valid |= PNG_INFO_tRNS;
  86.968 +#ifdef PNG_FREE_ME_SUPPORTED
  86.969 +      info_ptr->free_me |= PNG_FREE_TRNS;
  86.970 +#else
  86.971 +      png_ptr->flags |= PNG_FLAG_FREE_TRNS;
  86.972 +#endif
  86.973 +   }
  86.974 +}
  86.975 +#endif
  86.976 +
  86.977 +#if defined(PNG_sPLT_SUPPORTED)
  86.978 +void PNGAPI
  86.979 +png_set_sPLT(png_structp png_ptr,
  86.980 +             png_infop info_ptr, png_sPLT_tp entries, int nentries)
  86.981 +/*
  86.982 + *  entries        - array of png_sPLT_t structures
  86.983 + *                   to be added to the list of palettes
  86.984 + *                   in the info structure.
  86.985 + *  nentries       - number of palette structures to be
  86.986 + *                   added.
  86.987 + */
  86.988 +{
  86.989 +    png_sPLT_tp np;
  86.990 +    int i;
  86.991 +
  86.992 +    if (png_ptr == NULL || info_ptr == NULL)
  86.993 +       return;
  86.994 +
  86.995 +    np = (png_sPLT_tp)png_malloc_warn(png_ptr,
  86.996 +        (info_ptr->splt_palettes_num + nentries) *
  86.997 +        (png_uint_32)png_sizeof(png_sPLT_t));
  86.998 +    if (np == NULL)
  86.999 +    {
 86.1000 +      png_warning(png_ptr, "No memory for sPLT palettes.");
 86.1001 +      return;
 86.1002 +    }
 86.1003 +
 86.1004 +    png_memcpy(np, info_ptr->splt_palettes,
 86.1005 +           info_ptr->splt_palettes_num * png_sizeof(png_sPLT_t));
 86.1006 +    png_free(png_ptr, info_ptr->splt_palettes);
 86.1007 +    info_ptr->splt_palettes=NULL;
 86.1008 +
 86.1009 +    for (i = 0; i < nentries; i++)
 86.1010 +    {
 86.1011 +        png_sPLT_tp to = np + info_ptr->splt_palettes_num + i;
 86.1012 +        png_sPLT_tp from = entries + i;
 86.1013 +        png_uint_32 length;
 86.1014 +
 86.1015 +        length = png_strlen(from->name) + 1;
 86.1016 +        to->name = (png_charp)png_malloc_warn(png_ptr, length);
 86.1017 +        if (to->name == NULL)
 86.1018 +        {
 86.1019 +           png_warning(png_ptr,
 86.1020 +             "Out of memory while processing sPLT chunk");
 86.1021 +           continue;
 86.1022 +        }
 86.1023 +        png_memcpy(to->name, from->name, length);
 86.1024 +        to->entries = (png_sPLT_entryp)png_malloc_warn(png_ptr,
 86.1025 +            (png_uint_32)(from->nentries * png_sizeof(png_sPLT_entry)));
 86.1026 +        if (to->entries == NULL)
 86.1027 +        {
 86.1028 +           png_warning(png_ptr,
 86.1029 +             "Out of memory while processing sPLT chunk");
 86.1030 +           png_free(png_ptr, to->name);
 86.1031 +           to->name = NULL;
 86.1032 +           continue;
 86.1033 +        }
 86.1034 +        png_memcpy(to->entries, from->entries,
 86.1035 +            from->nentries * png_sizeof(png_sPLT_entry));
 86.1036 +        to->nentries = from->nentries;
 86.1037 +        to->depth = from->depth;
 86.1038 +    }
 86.1039 +
 86.1040 +    info_ptr->splt_palettes = np;
 86.1041 +    info_ptr->splt_palettes_num += nentries;
 86.1042 +    info_ptr->valid |= PNG_INFO_sPLT;
 86.1043 +#ifdef PNG_FREE_ME_SUPPORTED
 86.1044 +    info_ptr->free_me |= PNG_FREE_SPLT;
 86.1045 +#endif
 86.1046 +}
 86.1047 +#endif /* PNG_sPLT_SUPPORTED */
 86.1048 +
 86.1049 +#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
 86.1050 +void PNGAPI
 86.1051 +png_set_unknown_chunks(png_structp png_ptr,
 86.1052 +   png_infop info_ptr, png_unknown_chunkp unknowns, int num_unknowns)
 86.1053 +{
 86.1054 +    png_unknown_chunkp np;
 86.1055 +    int i;
 86.1056 +
 86.1057 +    if (png_ptr == NULL || info_ptr == NULL || num_unknowns == 0)
 86.1058 +        return;
 86.1059 +
 86.1060 +    np = (png_unknown_chunkp)png_malloc_warn(png_ptr,
 86.1061 +        (png_uint_32)((info_ptr->unknown_chunks_num + num_unknowns) *
 86.1062 +        png_sizeof(png_unknown_chunk)));
 86.1063 +    if (np == NULL)
 86.1064 +    {
 86.1065 +       png_warning(png_ptr,
 86.1066 +          "Out of memory while processing unknown chunk.");
 86.1067 +       return;
 86.1068 +    }
 86.1069 +
 86.1070 +    png_memcpy(np, info_ptr->unknown_chunks,
 86.1071 +           info_ptr->unknown_chunks_num * png_sizeof(png_unknown_chunk));
 86.1072 +    png_free(png_ptr, info_ptr->unknown_chunks);
 86.1073 +    info_ptr->unknown_chunks=NULL;
 86.1074 +
 86.1075 +    for (i = 0; i < num_unknowns; i++)
 86.1076 +    {
 86.1077 +       png_unknown_chunkp to = np + info_ptr->unknown_chunks_num + i;
 86.1078 +       png_unknown_chunkp from = unknowns + i;
 86.1079 +
 86.1080 +       png_memcpy((png_charp)to->name, 
 86.1081 +                  (png_charp)from->name, 
 86.1082 +                  png_sizeof(from->name));
 86.1083 +       to->name[png_sizeof(to->name)-1] = '\0';
 86.1084 +       to->size = from->size;
 86.1085 +       /* note our location in the read or write sequence */
 86.1086 +       to->location = (png_byte)(png_ptr->mode & 0xff);
 86.1087 +
 86.1088 +       if (from->size == 0)
 86.1089 +          to->data=NULL;
 86.1090 +       else
 86.1091 +       {
 86.1092 +          to->data = (png_bytep)png_malloc_warn(png_ptr,
 86.1093 +            (png_uint_32)from->size);
 86.1094 +          if (to->data == NULL)
 86.1095 +          {
 86.1096 +             png_warning(png_ptr,
 86.1097 +              "Out of memory while processing unknown chunk.");
 86.1098 +             to->size = 0;
 86.1099 +          }
 86.1100 +          else
 86.1101 +             png_memcpy(to->data, from->data, from->size);
 86.1102 +       }
 86.1103 +    }
 86.1104 +
 86.1105 +    info_ptr->unknown_chunks = np;
 86.1106 +    info_ptr->unknown_chunks_num += num_unknowns;
 86.1107 +#ifdef PNG_FREE_ME_SUPPORTED
 86.1108 +    info_ptr->free_me |= PNG_FREE_UNKN;
 86.1109 +#endif
 86.1110 +}
 86.1111 +void PNGAPI
 86.1112 +png_set_unknown_chunk_location(png_structp png_ptr, png_infop info_ptr,
 86.1113 +   int chunk, int location)
 86.1114 +{
 86.1115 +   if (png_ptr != NULL && info_ptr != NULL && chunk >= 0 && chunk <
 86.1116 +         (int)info_ptr->unknown_chunks_num)
 86.1117 +      info_ptr->unknown_chunks[chunk].location = (png_byte)location;
 86.1118 +}
 86.1119 +#endif
 86.1120 +
 86.1121 +#if defined(PNG_1_0_X) || defined(PNG_1_2_X)
 86.1122 +#if defined(PNG_READ_EMPTY_PLTE_SUPPORTED) || \
 86.1123 +    defined(PNG_WRITE_EMPTY_PLTE_SUPPORTED)
 86.1124 +void PNGAPI
 86.1125 +png_permit_empty_plte (png_structp png_ptr, int empty_plte_permitted)
 86.1126 +{
 86.1127 +   /* This function is deprecated in favor of png_permit_mng_features()
 86.1128 +      and will be removed from libpng-1.3.0 */
 86.1129 +   png_debug(1, "in png_permit_empty_plte, DEPRECATED.\n");
 86.1130 +   if (png_ptr == NULL)
 86.1131 +      return;
 86.1132 +   png_ptr->mng_features_permitted = (png_byte)
 86.1133 +     ((png_ptr->mng_features_permitted & (~PNG_FLAG_MNG_EMPTY_PLTE)) |
 86.1134 +     ((empty_plte_permitted & PNG_FLAG_MNG_EMPTY_PLTE)));
 86.1135 +}
 86.1136 +#endif
 86.1137 +#endif
 86.1138 +
 86.1139 +#if defined(PNG_MNG_FEATURES_SUPPORTED)
 86.1140 +png_uint_32 PNGAPI
 86.1141 +png_permit_mng_features (png_structp png_ptr, png_uint_32 mng_features)
 86.1142 +{
 86.1143 +   png_debug(1, "in png_permit_mng_features\n");
 86.1144 +   if (png_ptr == NULL)
 86.1145 +      return (png_uint_32)0;
 86.1146 +   png_ptr->mng_features_permitted =
 86.1147 +     (png_byte)(mng_features & PNG_ALL_MNG_FEATURES);
 86.1148 +   return (png_uint_32)png_ptr->mng_features_permitted;
 86.1149 +}
 86.1150 +#endif
 86.1151 +
 86.1152 +#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
 86.1153 +void PNGAPI
 86.1154 +png_set_keep_unknown_chunks(png_structp png_ptr, int keep, png_bytep
 86.1155 +   chunk_list, int num_chunks)
 86.1156 +{
 86.1157 +    png_bytep new_list, p;
 86.1158 +    int i, old_num_chunks;
 86.1159 +    if (png_ptr == NULL)
 86.1160 +       return;
 86.1161 +    if (num_chunks == 0)
 86.1162 +    {
 86.1163 +      if (keep == PNG_HANDLE_CHUNK_ALWAYS || keep == PNG_HANDLE_CHUNK_IF_SAFE)
 86.1164 +        png_ptr->flags |= PNG_FLAG_KEEP_UNKNOWN_CHUNKS;
 86.1165 +      else
 86.1166 +        png_ptr->flags &= ~PNG_FLAG_KEEP_UNKNOWN_CHUNKS;
 86.1167 +
 86.1168 +      if (keep == PNG_HANDLE_CHUNK_ALWAYS)
 86.1169 +        png_ptr->flags |= PNG_FLAG_KEEP_UNSAFE_CHUNKS;
 86.1170 +      else
 86.1171 +        png_ptr->flags &= ~PNG_FLAG_KEEP_UNSAFE_CHUNKS;
 86.1172 +      return;
 86.1173 +    }
 86.1174 +    if (chunk_list == NULL)
 86.1175 +      return;
 86.1176 +    old_num_chunks = png_ptr->num_chunk_list;
 86.1177 +    new_list=(png_bytep)png_malloc(png_ptr,
 86.1178 +       (png_uint_32)
 86.1179 +       (5*(num_chunks + old_num_chunks)));
 86.1180 +    if (png_ptr->chunk_list != NULL)
 86.1181 +    {
 86.1182 +       png_memcpy(new_list, png_ptr->chunk_list,
 86.1183 +          (png_size_t)(5*old_num_chunks));
 86.1184 +       png_free(png_ptr, png_ptr->chunk_list);
 86.1185 +       png_ptr->chunk_list=NULL;
 86.1186 +    }
 86.1187 +    png_memcpy(new_list + 5*old_num_chunks, chunk_list,
 86.1188 +       (png_size_t)(5*num_chunks));
 86.1189 +    for (p = new_list + 5*old_num_chunks + 4, i = 0; i<num_chunks; i++, p += 5)
 86.1190 +       *p=(png_byte)keep;
 86.1191 +    png_ptr->num_chunk_list = old_num_chunks + num_chunks;
 86.1192 +    png_ptr->chunk_list = new_list;
 86.1193 +#ifdef PNG_FREE_ME_SUPPORTED
 86.1194 +    png_ptr->free_me |= PNG_FREE_LIST;
 86.1195 +#endif
 86.1196 +}
 86.1197 +#endif
 86.1198 +
 86.1199 +#if defined(PNG_READ_USER_CHUNKS_SUPPORTED)
 86.1200 +void PNGAPI
 86.1201 +png_set_read_user_chunk_fn(png_structp png_ptr, png_voidp user_chunk_ptr,
 86.1202 +   png_user_chunk_ptr read_user_chunk_fn)
 86.1203 +{
 86.1204 +   png_debug(1, "in png_set_read_user_chunk_fn\n");
 86.1205 +   if (png_ptr == NULL)
 86.1206 +      return;
 86.1207 +   png_ptr->read_user_chunk_fn = read_user_chunk_fn;
 86.1208 +   png_ptr->user_chunk_ptr = user_chunk_ptr;
 86.1209 +}
 86.1210 +#endif
 86.1211 +
 86.1212 +#if defined(PNG_INFO_IMAGE_SUPPORTED)
 86.1213 +void PNGAPI
 86.1214 +png_set_rows(png_structp png_ptr, png_infop info_ptr, png_bytepp row_pointers)
 86.1215 +{
 86.1216 +   png_debug1(1, "in %s storage function\n", "rows");
 86.1217 +
 86.1218 +   if (png_ptr == NULL || info_ptr == NULL)
 86.1219 +      return;
 86.1220 +
 86.1221 +   if (info_ptr->row_pointers && (info_ptr->row_pointers != row_pointers))
 86.1222 +      png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0);
 86.1223 +   info_ptr->row_pointers = row_pointers;
 86.1224 +   if (row_pointers)
 86.1225 +      info_ptr->valid |= PNG_INFO_IDAT;
 86.1226 +}
 86.1227 +#endif
 86.1228 +
 86.1229 +#ifdef PNG_WRITE_SUPPORTED
 86.1230 +void PNGAPI
 86.1231 +png_set_compression_buffer_size(png_structp png_ptr,
 86.1232 +    png_uint_32 size)
 86.1233 +{
 86.1234 +    if (png_ptr == NULL)
 86.1235 +       return;
 86.1236 +    png_free(png_ptr, png_ptr->zbuf);
 86.1237 +    png_ptr->zbuf_size = (png_size_t)size;
 86.1238 +    png_ptr->zbuf = (png_bytep)png_malloc(png_ptr, size);
 86.1239 +    png_ptr->zstream.next_out = png_ptr->zbuf;
 86.1240 +    png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
 86.1241 +}
 86.1242 +#endif
 86.1243 +
 86.1244 +void PNGAPI
 86.1245 +png_set_invalid(png_structp png_ptr, png_infop info_ptr, int mask)
 86.1246 +{
 86.1247 +   if (png_ptr && info_ptr)
 86.1248 +      info_ptr->valid &= ~mask;
 86.1249 +}
 86.1250 +
 86.1251 +
 86.1252 +#ifndef PNG_1_0_X
 86.1253 +#ifdef PNG_ASSEMBLER_CODE_SUPPORTED
 86.1254 +/* function was added to libpng 1.2.0 and should always exist by default */
 86.1255 +void PNGAPI
 86.1256 +png_set_asm_flags (png_structp png_ptr, png_uint_32 asm_flags)
 86.1257 +{
 86.1258 +/* Obsolete as of libpng-1.2.20 and will be removed from libpng-1.4.0 */
 86.1259 +    if (png_ptr != NULL)
 86.1260 +    png_ptr->asm_flags = 0;
 86.1261 +    asm_flags = asm_flags; /* Quiet the compiler */
 86.1262 +}
 86.1263 +
 86.1264 +/* this function was added to libpng 1.2.0 */
 86.1265 +void PNGAPI
 86.1266 +png_set_mmx_thresholds (png_structp png_ptr,
 86.1267 +                        png_byte mmx_bitdepth_threshold,
 86.1268 +                        png_uint_32 mmx_rowbytes_threshold)
 86.1269 +{
 86.1270 +/* Obsolete as of libpng-1.2.20 and will be removed from libpng-1.4.0 */
 86.1271 +    if (png_ptr == NULL)
 86.1272 +       return;
 86.1273 +    /* Quiet the compiler */
 86.1274 +    mmx_bitdepth_threshold = mmx_bitdepth_threshold;
 86.1275 +    mmx_rowbytes_threshold = mmx_rowbytes_threshold;
 86.1276 +}
 86.1277 +#endif /* ?PNG_ASSEMBLER_CODE_SUPPORTED */
 86.1278 +
 86.1279 +#ifdef PNG_SET_USER_LIMITS_SUPPORTED
 86.1280 +/* this function was added to libpng 1.2.6 */
 86.1281 +void PNGAPI
 86.1282 +png_set_user_limits (png_structp png_ptr, png_uint_32 user_width_max,
 86.1283 +    png_uint_32 user_height_max)
 86.1284 +{
 86.1285 +    /* Images with dimensions larger than these limits will be
 86.1286 +     * rejected by png_set_IHDR().  To accept any PNG datastream
 86.1287 +     * regardless of dimensions, set both limits to 0x7ffffffL.
 86.1288 +     */
 86.1289 +    if (png_ptr == NULL) return;
 86.1290 +    png_ptr->user_width_max = user_width_max;
 86.1291 +    png_ptr->user_height_max = user_height_max;
 86.1292 +}
 86.1293 +#endif /* ?PNG_SET_USER_LIMITS_SUPPORTED */
 86.1294 +
 86.1295 +#endif /* ?PNG_1_0_X */
 86.1296 +#endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */
    87.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    87.2 +++ b/libs/libpng/pngtrans.c	Sat Sep 19 05:51:51 2015 +0300
    87.3 @@ -0,0 +1,662 @@
    87.4 +
    87.5 +/* pngtrans.c - transforms the data in a row (used by both readers and writers)
    87.6 + *
    87.7 + * Last changed in libpng 1.2.30 [August 15, 2008]
    87.8 + * For conditions of distribution and use, see copyright notice in png.h
    87.9 + * Copyright (c) 1998-2008 Glenn Randers-Pehrson
   87.10 + * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
   87.11 + * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
   87.12 + */
   87.13 +
   87.14 +#define PNG_INTERNAL
   87.15 +#include "png.h"
   87.16 +#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
   87.17 +
   87.18 +#if defined(PNG_READ_BGR_SUPPORTED) || defined(PNG_WRITE_BGR_SUPPORTED)
   87.19 +/* turn on BGR-to-RGB mapping */
   87.20 +void PNGAPI
   87.21 +png_set_bgr(png_structp png_ptr)
   87.22 +{
   87.23 +   png_debug(1, "in png_set_bgr\n");
   87.24 +   if (png_ptr == NULL) return;
   87.25 +   png_ptr->transformations |= PNG_BGR;
   87.26 +}
   87.27 +#endif
   87.28 +
   87.29 +#if defined(PNG_READ_SWAP_SUPPORTED) || defined(PNG_WRITE_SWAP_SUPPORTED)
   87.30 +/* turn on 16 bit byte swapping */
   87.31 +void PNGAPI
   87.32 +png_set_swap(png_structp png_ptr)
   87.33 +{
   87.34 +   png_debug(1, "in png_set_swap\n");
   87.35 +   if (png_ptr == NULL) return;
   87.36 +   if (png_ptr->bit_depth == 16)
   87.37 +      png_ptr->transformations |= PNG_SWAP_BYTES;
   87.38 +}
   87.39 +#endif
   87.40 +
   87.41 +#if defined(PNG_READ_PACK_SUPPORTED) || defined(PNG_WRITE_PACK_SUPPORTED)
   87.42 +/* turn on pixel packing */
   87.43 +void PNGAPI
   87.44 +png_set_packing(png_structp png_ptr)
   87.45 +{
   87.46 +   png_debug(1, "in png_set_packing\n");
   87.47 +   if (png_ptr == NULL) return;
   87.48 +   if (png_ptr->bit_depth < 8)
   87.49 +   {
   87.50 +      png_ptr->transformations |= PNG_PACK;
   87.51 +      png_ptr->usr_bit_depth = 8;
   87.52 +   }
   87.53 +}
   87.54 +#endif
   87.55 +
   87.56 +#if defined(PNG_READ_PACKSWAP_SUPPORTED)||defined(PNG_WRITE_PACKSWAP_SUPPORTED)
   87.57 +/* turn on packed pixel swapping */
   87.58 +void PNGAPI
   87.59 +png_set_packswap(png_structp png_ptr)
   87.60 +{
   87.61 +   png_debug(1, "in png_set_packswap\n");
   87.62 +   if (png_ptr == NULL) return;
   87.63 +   if (png_ptr->bit_depth < 8)
   87.64 +      png_ptr->transformations |= PNG_PACKSWAP;
   87.65 +}
   87.66 +#endif
   87.67 +
   87.68 +#if defined(PNG_READ_SHIFT_SUPPORTED) || defined(PNG_WRITE_SHIFT_SUPPORTED)
   87.69 +void PNGAPI
   87.70 +png_set_shift(png_structp png_ptr, png_color_8p true_bits)
   87.71 +{
   87.72 +   png_debug(1, "in png_set_shift\n");
   87.73 +   if (png_ptr == NULL) return;
   87.74 +   png_ptr->transformations |= PNG_SHIFT;
   87.75 +   png_ptr->shift = *true_bits;
   87.76 +}
   87.77 +#endif
   87.78 +
   87.79 +#if defined(PNG_READ_INTERLACING_SUPPORTED) || \
   87.80 +    defined(PNG_WRITE_INTERLACING_SUPPORTED)
   87.81 +int PNGAPI
   87.82 +png_set_interlace_handling(png_structp png_ptr)
   87.83 +{
   87.84 +   png_debug(1, "in png_set_interlace handling\n");
   87.85 +   if (png_ptr && png_ptr->interlaced)
   87.86 +   {
   87.87 +      png_ptr->transformations |= PNG_INTERLACE;
   87.88 +      return (7);
   87.89 +   }
   87.90 +
   87.91 +   return (1);
   87.92 +}
   87.93 +#endif
   87.94 +
   87.95 +#if defined(PNG_READ_FILLER_SUPPORTED) || defined(PNG_WRITE_FILLER_SUPPORTED)
   87.96 +/* Add a filler byte on read, or remove a filler or alpha byte on write.
   87.97 + * The filler type has changed in v0.95 to allow future 2-byte fillers
   87.98 + * for 48-bit input data, as well as to avoid problems with some compilers
   87.99 + * that don't like bytes as parameters.
  87.100 + */
  87.101 +void PNGAPI
  87.102 +png_set_filler(png_structp png_ptr, png_uint_32 filler, int filler_loc)
  87.103 +{
  87.104 +   png_debug(1, "in png_set_filler\n");
  87.105 +   if (png_ptr == NULL) return;
  87.106 +   png_ptr->transformations |= PNG_FILLER;
  87.107 +   png_ptr->filler = (png_byte)filler;
  87.108 +   if (filler_loc == PNG_FILLER_AFTER)
  87.109 +      png_ptr->flags |= PNG_FLAG_FILLER_AFTER;
  87.110 +   else
  87.111 +      png_ptr->flags &= ~PNG_FLAG_FILLER_AFTER;
  87.112 +
  87.113 +   /* This should probably go in the "do_read_filler" routine.
  87.114 +    * I attempted to do that in libpng-1.0.1a but that caused problems
  87.115 +    * so I restored it in libpng-1.0.2a
  87.116 +   */
  87.117 +
  87.118 +   if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
  87.119 +   {
  87.120 +      png_ptr->usr_channels = 4;
  87.121 +   }
  87.122 +
  87.123 +   /* Also I added this in libpng-1.0.2a (what happens when we expand
  87.124 +    * a less-than-8-bit grayscale to GA? */
  87.125 +
  87.126 +   if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY && png_ptr->bit_depth >= 8)
  87.127 +   {
  87.128 +      png_ptr->usr_channels = 2;
  87.129 +   }
  87.130 +}
  87.131 +
  87.132 +#if !defined(PNG_1_0_X)
  87.133 +/* Added to libpng-1.2.7 */
  87.134 +void PNGAPI
  87.135 +png_set_add_alpha(png_structp png_ptr, png_uint_32 filler, int filler_loc)
  87.136 +{
  87.137 +   png_debug(1, "in png_set_add_alpha\n");
  87.138 +   if (png_ptr == NULL) return;
  87.139 +   png_set_filler(png_ptr, filler, filler_loc);
  87.140 +   png_ptr->transformations |= PNG_ADD_ALPHA;
  87.141 +}
  87.142 +#endif
  87.143 +
  87.144 +#endif
  87.145 +
  87.146 +#if defined(PNG_READ_SWAP_ALPHA_SUPPORTED) || \
  87.147 +    defined(PNG_WRITE_SWAP_ALPHA_SUPPORTED)
  87.148 +void PNGAPI
  87.149 +png_set_swap_alpha(png_structp png_ptr)
  87.150 +{
  87.151 +   png_debug(1, "in png_set_swap_alpha\n");
  87.152 +   if (png_ptr == NULL) return;
  87.153 +   png_ptr->transformations |= PNG_SWAP_ALPHA;
  87.154 +}
  87.155 +#endif
  87.156 +
  87.157 +#if defined(PNG_READ_INVERT_ALPHA_SUPPORTED) || \
  87.158 +    defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED)
  87.159 +void PNGAPI
  87.160 +png_set_invert_alpha(png_structp png_ptr)
  87.161 +{
  87.162 +   png_debug(1, "in png_set_invert_alpha\n");
  87.163 +   if (png_ptr == NULL) return;
  87.164 +   png_ptr->transformations |= PNG_INVERT_ALPHA;
  87.165 +}
  87.166 +#endif
  87.167 +
  87.168 +#if defined(PNG_READ_INVERT_SUPPORTED) || defined(PNG_WRITE_INVERT_SUPPORTED)
  87.169 +void PNGAPI
  87.170 +png_set_invert_mono(png_structp png_ptr)
  87.171 +{
  87.172 +   png_debug(1, "in png_set_invert_mono\n");
  87.173 +   if (png_ptr == NULL) return;
  87.174 +   png_ptr->transformations |= PNG_INVERT_MONO;
  87.175 +}
  87.176 +
  87.177 +/* invert monochrome grayscale data */
  87.178 +void /* PRIVATE */
  87.179 +png_do_invert(png_row_infop row_info, png_bytep row)
  87.180 +{
  87.181 +   png_debug(1, "in png_do_invert\n");
  87.182 +  /* This test removed from libpng version 1.0.13 and 1.2.0:
  87.183 +   *   if (row_info->bit_depth == 1 &&
  87.184 +   */
  87.185 +#if defined(PNG_USELESS_TESTS_SUPPORTED)
  87.186 +   if (row == NULL || row_info == NULL)
  87.187 +     return;
  87.188 +#endif
  87.189 +   if (row_info->color_type == PNG_COLOR_TYPE_GRAY)
  87.190 +   {
  87.191 +      png_bytep rp = row;
  87.192 +      png_uint_32 i;
  87.193 +      png_uint_32 istop = row_info->rowbytes;
  87.194 +
  87.195 +      for (i = 0; i < istop; i++)
  87.196 +      {
  87.197 +         *rp = (png_byte)(~(*rp));
  87.198 +         rp++;
  87.199 +      }
  87.200 +   }
  87.201 +   else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA &&
  87.202 +      row_info->bit_depth == 8)
  87.203 +   {
  87.204 +      png_bytep rp = row;
  87.205 +      png_uint_32 i;
  87.206 +      png_uint_32 istop = row_info->rowbytes;
  87.207 +
  87.208 +      for (i = 0; i < istop; i+=2)
  87.209 +      {
  87.210 +         *rp = (png_byte)(~(*rp));
  87.211 +         rp+=2;
  87.212 +      }
  87.213 +   }
  87.214 +   else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA &&
  87.215 +      row_info->bit_depth == 16)
  87.216 +   {
  87.217 +      png_bytep rp = row;
  87.218 +      png_uint_32 i;
  87.219 +      png_uint_32 istop = row_info->rowbytes;
  87.220 +
  87.221 +      for (i = 0; i < istop; i+=4)
  87.222 +      {
  87.223 +         *rp = (png_byte)(~(*rp));
  87.224 +         *(rp+1) = (png_byte)(~(*(rp+1)));
  87.225 +         rp+=4;
  87.226 +      }
  87.227 +   }
  87.228 +}
  87.229 +#endif
  87.230 +
  87.231 +#if defined(PNG_READ_SWAP_SUPPORTED) || defined(PNG_WRITE_SWAP_SUPPORTED)
  87.232 +/* swaps byte order on 16 bit depth images */
  87.233 +void /* PRIVATE */
  87.234 +png_do_swap(png_row_infop row_info, png_bytep row)
  87.235 +{
  87.236 +   png_debug(1, "in png_do_swap\n");
  87.237 +   if (
  87.238 +#if defined(PNG_USELESS_TESTS_SUPPORTED)
  87.239 +       row != NULL && row_info != NULL &&
  87.240 +#endif
  87.241 +       row_info->bit_depth == 16)
  87.242 +   {
  87.243 +      png_bytep rp = row;
  87.244 +      png_uint_32 i;
  87.245 +      png_uint_32 istop= row_info->width * row_info->channels;
  87.246 +
  87.247 +      for (i = 0; i < istop; i++, rp += 2)
  87.248 +      {
  87.249 +         png_byte t = *rp;
  87.250 +         *rp = *(rp + 1);
  87.251 +         *(rp + 1) = t;
  87.252 +      }
  87.253 +   }
  87.254 +}
  87.255 +#endif
  87.256 +
  87.257 +#if defined(PNG_READ_PACKSWAP_SUPPORTED)||defined(PNG_WRITE_PACKSWAP_SUPPORTED)
  87.258 +static PNG_CONST png_byte onebppswaptable[256] = {
  87.259 +   0x00, 0x80, 0x40, 0xC0, 0x20, 0xA0, 0x60, 0xE0,
  87.260 +   0x10, 0x90, 0x50, 0xD0, 0x30, 0xB0, 0x70, 0xF0,
  87.261 +   0x08, 0x88, 0x48, 0xC8, 0x28, 0xA8, 0x68, 0xE8,
  87.262 +   0x18, 0x98, 0x58, 0xD8, 0x38, 0xB8, 0x78, 0xF8,
  87.263 +   0x04, 0x84, 0x44, 0xC4, 0x24, 0xA4, 0x64, 0xE4,
  87.264 +   0x14, 0x94, 0x54, 0xD4, 0x34, 0xB4, 0x74, 0xF4,
  87.265 +   0x0C, 0x8C, 0x4C, 0xCC, 0x2C, 0xAC, 0x6C, 0xEC,
  87.266 +   0x1C, 0x9C, 0x5C, 0xDC, 0x3C, 0xBC, 0x7C, 0xFC,
  87.267 +   0x02, 0x82, 0x42, 0xC2, 0x22, 0xA2, 0x62, 0xE2,
  87.268 +   0x12, 0x92, 0x52, 0xD2, 0x32, 0xB2, 0x72, 0xF2,
  87.269 +   0x0A, 0x8A, 0x4A, 0xCA, 0x2A, 0xAA, 0x6A, 0xEA,
  87.270 +   0x1A, 0x9A, 0x5A, 0xDA, 0x3A, 0xBA, 0x7A, 0xFA,
  87.271 +   0x06, 0x86, 0x46, 0xC6, 0x26, 0xA6, 0x66, 0xE6,
  87.272 +   0x16, 0x96, 0x56, 0xD6, 0x36, 0xB6, 0x76, 0xF6,
  87.273 +   0x0E, 0x8E, 0x4E, 0xCE, 0x2E, 0xAE, 0x6E, 0xEE,
  87.274 +   0x1E, 0x9E, 0x5E, 0xDE, 0x3E, 0xBE, 0x7E, 0xFE,
  87.275 +   0x01, 0x81, 0x41, 0xC1, 0x21, 0xA1, 0x61, 0xE1,
  87.276 +   0x11, 0x91, 0x51, 0xD1, 0x31, 0xB1, 0x71, 0xF1,
  87.277 +   0x09, 0x89, 0x49, 0xC9, 0x29, 0xA9, 0x69, 0xE9,
  87.278 +   0x19, 0x99, 0x59, 0xD9, 0x39, 0xB9, 0x79, 0xF9,
  87.279 +   0x05, 0x85, 0x45, 0xC5, 0x25, 0xA5, 0x65, 0xE5,
  87.280 +   0x15, 0x95, 0x55, 0xD5, 0x35, 0xB5, 0x75, 0xF5,
  87.281 +   0x0D, 0x8D, 0x4D, 0xCD, 0x2D, 0xAD, 0x6D, 0xED,
  87.282 +   0x1D, 0x9D, 0x5D, 0xDD, 0x3D, 0xBD, 0x7D, 0xFD,
  87.283 +   0x03, 0x83, 0x43, 0xC3, 0x23, 0xA3, 0x63, 0xE3,
  87.284 +   0x13, 0x93, 0x53, 0xD3, 0x33, 0xB3, 0x73, 0xF3,
  87.285 +   0x0B, 0x8B, 0x4B, 0xCB, 0x2B, 0xAB, 0x6B, 0xEB,
  87.286 +   0x1B, 0x9B, 0x5B, 0xDB, 0x3B, 0xBB, 0x7B, 0xFB,
  87.287 +   0x07, 0x87, 0x47, 0xC7, 0x27, 0xA7, 0x67, 0xE7,
  87.288 +   0x17, 0x97, 0x57, 0xD7, 0x37, 0xB7, 0x77, 0xF7,
  87.289 +   0x0F, 0x8F, 0x4F, 0xCF, 0x2F, 0xAF, 0x6F, 0xEF,
  87.290 +   0x1F, 0x9F, 0x5F, 0xDF, 0x3F, 0xBF, 0x7F, 0xFF
  87.291 +};
  87.292 +
  87.293 +static PNG_CONST png_byte twobppswaptable[256] = {
  87.294 +   0x00, 0x40, 0x80, 0xC0, 0x10, 0x50, 0x90, 0xD0,
  87.295 +   0x20, 0x60, 0xA0, 0xE0, 0x30, 0x70, 0xB0, 0xF0,
  87.296 +   0x04, 0x44, 0x84, 0xC4, 0x14, 0x54, 0x94, 0xD4,
  87.297 +   0x24, 0x64, 0xA4, 0xE4, 0x34, 0x74, 0xB4, 0xF4,
  87.298 +   0x08, 0x48, 0x88, 0xC8, 0x18, 0x58, 0x98, 0xD8,
  87.299 +   0x28, 0x68, 0xA8, 0xE8, 0x38, 0x78, 0xB8, 0xF8,
  87.300 +   0x0C, 0x4C, 0x8C, 0xCC, 0x1C, 0x5C, 0x9C, 0xDC,
  87.301 +   0x2C, 0x6C, 0xAC, 0xEC, 0x3C, 0x7C, 0xBC, 0xFC,
  87.302 +   0x01, 0x41, 0x81, 0xC1, 0x11, 0x51, 0x91, 0xD1,
  87.303 +   0x21, 0x61, 0xA1, 0xE1, 0x31, 0x71, 0xB1, 0xF1,
  87.304 +   0x05, 0x45, 0x85, 0xC5, 0x15, 0x55, 0x95, 0xD5,
  87.305 +   0x25, 0x65, 0xA5, 0xE5, 0x35, 0x75, 0xB5, 0xF5,
  87.306 +   0x09, 0x49, 0x89, 0xC9, 0x19, 0x59, 0x99, 0xD9,
  87.307 +   0x29, 0x69, 0xA9, 0xE9, 0x39, 0x79, 0xB9, 0xF9,
  87.308 +   0x0D, 0x4D, 0x8D, 0xCD, 0x1D, 0x5D, 0x9D, 0xDD,
  87.309 +   0x2D, 0x6D, 0xAD, 0xED, 0x3D, 0x7D, 0xBD, 0xFD,
  87.310 +   0x02, 0x42, 0x82, 0xC2, 0x12, 0x52, 0x92, 0xD2,
  87.311 +   0x22, 0x62, 0xA2, 0xE2, 0x32, 0x72, 0xB2, 0xF2,
  87.312 +   0x06, 0x46, 0x86, 0xC6, 0x16, 0x56, 0x96, 0xD6,
  87.313 +   0x26, 0x66, 0xA6, 0xE6, 0x36, 0x76, 0xB6, 0xF6,
  87.314 +   0x0A, 0x4A, 0x8A, 0xCA, 0x1A, 0x5A, 0x9A, 0xDA,
  87.315 +   0x2A, 0x6A, 0xAA, 0xEA, 0x3A, 0x7A, 0xBA, 0xFA,
  87.316 +   0x0E, 0x4E, 0x8E, 0xCE, 0x1E, 0x5E, 0x9E, 0xDE,
  87.317 +   0x2E, 0x6E, 0xAE, 0xEE, 0x3E, 0x7E, 0xBE, 0xFE,
  87.318 +   0x03, 0x43, 0x83, 0xC3, 0x13, 0x53, 0x93, 0xD3,
  87.319 +   0x23, 0x63, 0xA3, 0xE3, 0x33, 0x73, 0xB3, 0xF3,
  87.320 +   0x07, 0x47, 0x87, 0xC7, 0x17, 0x57, 0x97, 0xD7,
  87.321 +   0x27, 0x67, 0xA7, 0xE7, 0x37, 0x77, 0xB7, 0xF7,
  87.322 +   0x0B, 0x4B, 0x8B, 0xCB, 0x1B, 0x5B, 0x9B, 0xDB,
  87.323 +   0x2B, 0x6B, 0xAB, 0xEB, 0x3B, 0x7B, 0xBB, 0xFB,
  87.324 +   0x0F, 0x4F, 0x8F, 0xCF, 0x1F, 0x5F, 0x9F, 0xDF,
  87.325 +   0x2F, 0x6F, 0xAF, 0xEF, 0x3F, 0x7F, 0xBF, 0xFF
  87.326 +};
  87.327 +
  87.328 +static PNG_CONST png_byte fourbppswaptable[256] = {
  87.329 +   0x00, 0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70,
  87.330 +   0x80, 0x90, 0xA0, 0xB0, 0xC0, 0xD0, 0xE0, 0xF0,
  87.331 +   0x01, 0x11, 0x21, 0x31, 0x41, 0x51, 0x61, 0x71,
  87.332 +   0x81, 0x91, 0xA1, 0xB1, 0xC1, 0xD1, 0xE1, 0xF1,
  87.333 +   0x02, 0x12, 0x22, 0x32, 0x42, 0x52, 0x62, 0x72,
  87.334 +   0x82, 0x92, 0xA2, 0xB2, 0xC2, 0xD2, 0xE2, 0xF2,
  87.335 +   0x03, 0x13, 0x23, 0x33, 0x43, 0x53, 0x63, 0x73,
  87.336 +   0x83, 0x93, 0xA3, 0xB3, 0xC3, 0xD3, 0xE3, 0xF3,
  87.337 +   0x04, 0x14, 0x24, 0x34, 0x44, 0x54, 0x64, 0x74,
  87.338 +   0x84, 0x94, 0xA4, 0xB4, 0xC4, 0xD4, 0xE4, 0xF4,
  87.339 +   0x05, 0x15, 0x25, 0x35, 0x45, 0x55, 0x65, 0x75,
  87.340 +   0x85, 0x95, 0xA5, 0xB5, 0xC5, 0xD5, 0xE5, 0xF5,
  87.341 +   0x06, 0x16, 0x26, 0x36, 0x46, 0x56, 0x66, 0x76,
  87.342 +   0x86, 0x96, 0xA6, 0xB6, 0xC6, 0xD6, 0xE6, 0xF6,
  87.343 +   0x07, 0x17, 0x27, 0x37, 0x47, 0x57, 0x67, 0x77,
  87.344 +   0x87, 0x97, 0xA7, 0xB7, 0xC7, 0xD7, 0xE7, 0xF7,
  87.345 +   0x08, 0x18, 0x28, 0x38, 0x48, 0x58, 0x68, 0x78,
  87.346 +   0x88, 0x98, 0xA8, 0xB8, 0xC8, 0xD8, 0xE8, 0xF8,
  87.347 +   0x09, 0x19, 0x29, 0x39, 0x49, 0x59, 0x69, 0x79,
  87.348 +   0x89, 0x99, 0xA9, 0xB9, 0xC9, 0xD9, 0xE9, 0xF9,
  87.349 +   0x0A, 0x1A, 0x2A, 0x3A, 0x4A, 0x5A, 0x6A, 0x7A,
  87.350 +   0x8A, 0x9A, 0xAA, 0xBA, 0xCA, 0xDA, 0xEA, 0xFA,
  87.351 +   0x0B, 0x1B, 0x2B, 0x3B, 0x4B, 0x5B, 0x6B, 0x7B,
  87.352 +   0x8B, 0x9B, 0xAB, 0xBB, 0xCB, 0xDB, 0xEB, 0xFB,
  87.353 +   0x0C, 0x1C, 0x2C, 0x3C, 0x4C, 0x5C, 0x6C, 0x7C,
  87.354 +   0x8C, 0x9C, 0xAC, 0xBC, 0xCC, 0xDC, 0xEC, 0xFC,
  87.355 +   0x0D, 0x1D, 0x2D, 0x3D, 0x4D, 0x5D, 0x6D, 0x7D,
  87.356 +   0x8D, 0x9D, 0xAD, 0xBD, 0xCD, 0xDD, 0xED, 0xFD,
  87.357 +   0x0E, 0x1E, 0x2E, 0x3E, 0x4E, 0x5E, 0x6E, 0x7E,
  87.358 +   0x8E, 0x9E, 0xAE, 0xBE, 0xCE, 0xDE, 0xEE, 0xFE,
  87.359 +   0x0F, 0x1F, 0x2F, 0x3F, 0x4F, 0x5F, 0x6F, 0x7F,
  87.360 +   0x8F, 0x9F, 0xAF, 0xBF, 0xCF, 0xDF, 0xEF, 0xFF
  87.361 +};
  87.362 +
  87.363 +/* swaps pixel packing order within bytes */
  87.364 +void /* PRIVATE */
  87.365 +png_do_packswap(png_row_infop row_info, png_bytep row)
  87.366 +{
  87.367 +   png_debug(1, "in png_do_packswap\n");
  87.368 +   if (
  87.369 +#if defined(PNG_USELESS_TESTS_SUPPORTED)
  87.370 +       row != NULL && row_info != NULL &&
  87.371 +#endif
  87.372 +       row_info->bit_depth < 8)
  87.373 +   {
  87.374 +      png_bytep rp, end, table;
  87.375 +
  87.376 +      end = row + row_info->rowbytes;
  87.377 +
  87.378 +      if (row_info->bit_depth == 1)
  87.379 +         table = (png_bytep)onebppswaptable;
  87.380 +      else if (row_info->bit_depth == 2)
  87.381 +         table = (png_bytep)twobppswaptable;
  87.382 +      else if (row_info->bit_depth == 4)
  87.383 +         table = (png_bytep)fourbppswaptable;
  87.384 +      else
  87.385 +         return;
  87.386 +
  87.387 +      for (rp = row; rp < end; rp++)
  87.388 +         *rp = table[*rp];
  87.389 +   }
  87.390 +}
  87.391 +#endif /* PNG_READ_PACKSWAP_SUPPORTED or PNG_WRITE_PACKSWAP_SUPPORTED */
  87.392 +
  87.393 +#if defined(PNG_WRITE_FILLER_SUPPORTED) || \
  87.394 +    defined(PNG_READ_STRIP_ALPHA_SUPPORTED)
  87.395 +/* remove filler or alpha byte(s) */
  87.396 +void /* PRIVATE */
  87.397 +png_do_strip_filler(png_row_infop row_info, png_bytep row, png_uint_32 flags)
  87.398 +{
  87.399 +   png_debug(1, "in png_do_strip_filler\n");
  87.400 +#if defined(PNG_USELESS_TESTS_SUPPORTED)
  87.401 +   if (row != NULL && row_info != NULL)
  87.402 +#endif
  87.403 +   {
  87.404 +      png_bytep sp=row;
  87.405 +      png_bytep dp=row;
  87.406 +      png_uint_32 row_width=row_info->width;
  87.407 +      png_uint_32 i;
  87.408 +
  87.409 +      if ((row_info->color_type == PNG_COLOR_TYPE_RGB ||
  87.410 +         (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA &&
  87.411 +         (flags & PNG_FLAG_STRIP_ALPHA))) &&
  87.412 +         row_info->channels == 4)
  87.413 +      {
  87.414 +         if (row_info->bit_depth == 8)
  87.415 +         {
  87.416 +            /* This converts from RGBX or RGBA to RGB */
  87.417 +            if (flags & PNG_FLAG_FILLER_AFTER)
  87.418 +            {
  87.419 +               dp+=3; sp+=4;
  87.420 +               for (i = 1; i < row_width; i++)
  87.421 +               {
  87.422 +                  *dp++ = *sp++;
  87.423 +                  *dp++ = *sp++;
  87.424 +                  *dp++ = *sp++;
  87.425 +                  sp++;
  87.426 +               }
  87.427 +            }
  87.428 +            /* This converts from XRGB or ARGB to RGB */
  87.429 +            else
  87.430 +            {
  87.431 +               for (i = 0; i < row_width; i++)
  87.432 +               {
  87.433 +                  sp++;
  87.434 +                  *dp++ = *sp++;
  87.435 +                  *dp++ = *sp++;
  87.436 +                  *dp++ = *sp++;
  87.437 +               }
  87.438 +            }
  87.439 +            row_info->pixel_depth = 24;
  87.440 +            row_info->rowbytes = row_width * 3;
  87.441 +         }
  87.442 +         else /* if (row_info->bit_depth == 16) */
  87.443 +         {
  87.444 +            if (flags & PNG_FLAG_FILLER_AFTER)
  87.445 +            {
  87.446 +               /* This converts from RRGGBBXX or RRGGBBAA to RRGGBB */
  87.447 +               sp += 8; dp += 6;
  87.448 +               for (i = 1; i < row_width; i++)
  87.449 +               {
  87.450 +                  /* This could be (although png_memcpy is probably slower):
  87.451 +                  png_memcpy(dp, sp, 6);
  87.452 +                  sp += 8;
  87.453 +                  dp += 6;
  87.454 +                  */
  87.455 +
  87.456 +                  *dp++ = *sp++;
  87.457 +                  *dp++ = *sp++;
  87.458 +                  *dp++ = *sp++;
  87.459 +                  *dp++ = *sp++;
  87.460 +                  *dp++ = *sp++;
  87.461 +                  *dp++ = *sp++;
  87.462 +                  sp += 2;
  87.463 +               }
  87.464 +            }
  87.465 +            else
  87.466 +            {
  87.467 +               /* This converts from XXRRGGBB or AARRGGBB to RRGGBB */
  87.468 +               for (i = 0; i < row_width; i++)
  87.469 +               {
  87.470 +                  /* This could be (although png_memcpy is probably slower):
  87.471 +                  png_memcpy(dp, sp, 6);
  87.472 +                  sp += 8;
  87.473 +                  dp += 6;
  87.474 +                  */
  87.475 +
  87.476 +                  sp+=2;
  87.477 +                  *dp++ = *sp++;
  87.478 +                  *dp++ = *sp++;
  87.479 +                  *dp++ = *sp++;
  87.480 +                  *dp++ = *sp++;
  87.481 +                  *dp++ = *sp++;
  87.482 +                  *dp++ = *sp++;
  87.483 +               }
  87.484 +            }
  87.485 +            row_info->pixel_depth = 48;
  87.486 +            row_info->rowbytes = row_width * 6;
  87.487 +         }
  87.488 +         row_info->channels = 3;
  87.489 +      }
  87.490 +      else if ((row_info->color_type == PNG_COLOR_TYPE_GRAY ||
  87.491 +         (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA &&
  87.492 +         (flags & PNG_FLAG_STRIP_ALPHA))) &&
  87.493 +          row_info->channels == 2)
  87.494 +      {
  87.495 +         if (row_info->bit_depth == 8)
  87.496 +         {
  87.497 +            /* This converts from GX or GA to G */
  87.498 +            if (flags & PNG_FLAG_FILLER_AFTER)
  87.499 +            {
  87.500 +               for (i = 0; i < row_width; i++)
  87.501 +               {
  87.502 +                  *dp++ = *sp++;
  87.503 +                  sp++;
  87.504 +               }
  87.505 +            }
  87.506 +            /* This converts from XG or AG to G */
  87.507 +            else
  87.508 +            {
  87.509 +               for (i = 0; i < row_width; i++)
  87.510 +               {
  87.511 +                  sp++;
  87.512 +                  *dp++ = *sp++;
  87.513 +               }
  87.514 +            }
  87.515 +            row_info->pixel_depth = 8;
  87.516 +            row_info->rowbytes = row_width;
  87.517 +         }
  87.518 +         else /* if (row_info->bit_depth == 16) */
  87.519 +         {
  87.520 +            if (flags & PNG_FLAG_FILLER_AFTER)
  87.521 +            {
  87.522 +               /* This converts from GGXX or GGAA to GG */
  87.523 +               sp += 4; dp += 2;
  87.524 +               for (i = 1; i < row_width; i++)
  87.525 +               {
  87.526 +                  *dp++ = *sp++;
  87.527 +                  *dp++ = *sp++;
  87.528 +                  sp += 2;
  87.529 +               }
  87.530 +            }
  87.531 +            else
  87.532 +            {
  87.533 +               /* This converts from XXGG or AAGG to GG */
  87.534 +               for (i = 0; i < row_width; i++)
  87.535 +               {
  87.536 +                  sp += 2;
  87.537 +                  *dp++ = *sp++;
  87.538 +                  *dp++ = *sp++;
  87.539 +               }
  87.540 +            }
  87.541 +            row_info->pixel_depth = 16;
  87.542 +            row_info->rowbytes = row_width * 2;
  87.543 +         }
  87.544 +         row_info->channels = 1;
  87.545 +      }
  87.546 +      if (flags & PNG_FLAG_STRIP_ALPHA)
  87.547 +        row_info->color_type &= ~PNG_COLOR_MASK_ALPHA;
  87.548 +   }
  87.549 +}
  87.550 +#endif
  87.551 +
  87.552 +#if defined(PNG_READ_BGR_SUPPORTED) || defined(PNG_WRITE_BGR_SUPPORTED)
  87.553 +/* swaps red and blue bytes within a pixel */
  87.554 +void /* PRIVATE */
  87.555 +png_do_bgr(png_row_infop row_info, png_bytep row)
  87.556 +{
  87.557 +   png_debug(1, "in png_do_bgr\n");
  87.558 +   if (
  87.559 +#if defined(PNG_USELESS_TESTS_SUPPORTED)
  87.560 +       row != NULL && row_info != NULL &&
  87.561 +#endif
  87.562 +       (row_info->color_type & PNG_COLOR_MASK_COLOR))
  87.563 +   {
  87.564 +      png_uint_32 row_width = row_info->width;
  87.565 +      if (row_info->bit_depth == 8)
  87.566 +      {
  87.567 +         if (row_info->color_type == PNG_COLOR_TYPE_RGB)
  87.568 +         {
  87.569 +            png_bytep rp;
  87.570 +            png_uint_32 i;
  87.571 +
  87.572 +            for (i = 0, rp = row; i < row_width; i++, rp += 3)
  87.573 +            {
  87.574 +               png_byte save = *rp;
  87.575 +               *rp = *(rp + 2);
  87.576 +               *(rp + 2) = save;
  87.577 +            }
  87.578 +         }
  87.579 +         else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
  87.580 +         {
  87.581 +            png_bytep rp;
  87.582 +            png_uint_32 i;
  87.583 +
  87.584 +            for (i = 0, rp = row; i < row_width; i++, rp += 4)
  87.585 +            {
  87.586 +               png_byte save = *rp;
  87.587 +               *rp = *(rp + 2);
  87.588 +               *(rp + 2) = save;
  87.589 +            }
  87.590 +         }
  87.591 +      }
  87.592 +      else if (row_info->bit_depth == 16)
  87.593 +      {
  87.594 +         if (row_info->color_type == PNG_COLOR_TYPE_RGB)
  87.595 +         {
  87.596 +            png_bytep rp;
  87.597 +            png_uint_32 i;
  87.598 +
  87.599 +            for (i = 0, rp = row; i < row_width; i++, rp += 6)
  87.600 +            {
  87.601 +               png_byte save = *rp;
  87.602 +               *rp = *(rp + 4);
  87.603 +               *(rp + 4) = save;
  87.604 +               save = *(rp + 1);
  87.605 +               *(rp + 1) = *(rp + 5);
  87.606 +               *(rp + 5) = save;
  87.607 +            }
  87.608 +         }
  87.609 +         else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
  87.610 +         {
  87.611 +            png_bytep rp;
  87.612 +            png_uint_32 i;
  87.613 +
  87.614 +            for (i = 0, rp = row; i < row_width; i++, rp += 8)
  87.615 +            {
  87.616 +               png_byte save = *rp;
  87.617 +               *rp = *(rp + 4);
  87.618 +               *(rp + 4) = save;
  87.619 +               save = *(rp + 1);
  87.620 +               *(rp + 1) = *(rp + 5);
  87.621 +               *(rp + 5) = save;
  87.622 +            }
  87.623 +         }
  87.624 +      }
  87.625 +   }
  87.626 +}
  87.627 +#endif /* PNG_READ_BGR_SUPPORTED or PNG_WRITE_BGR_SUPPORTED */
  87.628 +
  87.629 +#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \
  87.630 +    defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED) || \
  87.631 +    defined(PNG_LEGACY_SUPPORTED)
  87.632 +void PNGAPI
  87.633 +png_set_user_transform_info(png_structp png_ptr, png_voidp
  87.634 +   user_transform_ptr, int user_transform_depth, int user_transform_channels)
  87.635 +{
  87.636 +   png_debug(1, "in png_set_user_transform_info\n");
  87.637 +   if (png_ptr == NULL) return;
  87.638 +#if defined(PNG_USER_TRANSFORM_PTR_SUPPORTED)
  87.639 +   png_ptr->user_transform_ptr = user_transform_ptr;
  87.640 +   png_ptr->user_transform_depth = (png_byte)user_transform_depth;
  87.641 +   png_ptr->user_transform_channels = (png_byte)user_transform_channels;
  87.642 +#else
  87.643 +   if (user_transform_ptr || user_transform_depth || user_transform_channels)
  87.644 +      png_warning(png_ptr,
  87.645 +        "This version of libpng does not support user transform info");
  87.646 +#endif
  87.647 +}
  87.648 +#endif
  87.649 +
  87.650 +/* This function returns a pointer to the user_transform_ptr associated with
  87.651 + * the user transform functions.  The application should free any memory
  87.652 + * associated with this pointer before png_write_destroy and png_read_destroy
  87.653 + * are called.
  87.654 + */
  87.655 +png_voidp PNGAPI
  87.656 +png_get_user_transform_ptr(png_structp png_ptr)
  87.657 +{
  87.658 +   if (png_ptr == NULL) return (NULL);
  87.659 +#if defined(PNG_USER_TRANSFORM_PTR_SUPPORTED)
  87.660 +   return ((png_voidp)png_ptr->user_transform_ptr);
  87.661 +#else
  87.662 +   return (NULL);
  87.663 +#endif
  87.664 +}
  87.665 +#endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */
    88.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    88.2 +++ b/libs/libpng/pngvcrd.c	Sat Sep 19 05:51:51 2015 +0300
    88.3 @@ -0,0 +1,1 @@
    88.4 +/* pnggvrd.c was removed from libpng-1.2.20. */
    89.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    89.2 +++ b/libs/libpng/pngwio.c	Sat Sep 19 05:51:51 2015 +0300
    89.3 @@ -0,0 +1,234 @@
    89.4 +
    89.5 +/* pngwio.c - functions for data output
    89.6 + *
    89.7 + * Last changed in libpng 1.2.30 [August 15, 2008]
    89.8 + * For conditions of distribution and use, see copyright notice in png.h
    89.9 + * Copyright (c) 1998-2008 Glenn Randers-Pehrson
   89.10 + * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
   89.11 + * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
   89.12 + *
   89.13 + * This file provides a location for all output.  Users who need
   89.14 + * special handling are expected to write functions that have the same
   89.15 + * arguments as these and perform similar functions, but that possibly
   89.16 + * use different output methods.  Note that you shouldn't change these
   89.17 + * functions, but rather write replacement functions and then change
   89.18 + * them at run time with png_set_write_fn(...).
   89.19 + */
   89.20 +
   89.21 +#define PNG_INTERNAL
   89.22 +#include "png.h"
   89.23 +#ifdef PNG_WRITE_SUPPORTED
   89.24 +
   89.25 +/* Write the data to whatever output you are using.  The default routine
   89.26 +   writes to a file pointer.  Note that this routine sometimes gets called
   89.27 +   with very small lengths, so you should implement some kind of simple
   89.28 +   buffering if you are using unbuffered writes.  This should never be asked
   89.29 +   to write more than 64K on a 16 bit machine.  */
   89.30 +
   89.31 +void /* PRIVATE */
   89.32 +png_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
   89.33 +{
   89.34 +   if (png_ptr->write_data_fn != NULL )
   89.35 +      (*(png_ptr->write_data_fn))(png_ptr, data, length);
   89.36 +   else
   89.37 +      png_error(png_ptr, "Call to NULL write function");
   89.38 +}
   89.39 +
   89.40 +#if !defined(PNG_NO_STDIO)
   89.41 +/* This is the function that does the actual writing of data.  If you are
   89.42 +   not writing to a standard C stream, you should create a replacement
   89.43 +   write_data function and use it at run time with png_set_write_fn(), rather
   89.44 +   than changing the library. */
   89.45 +#ifndef USE_FAR_KEYWORD
   89.46 +void PNGAPI
   89.47 +png_default_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
   89.48 +{
   89.49 +   png_uint_32 check;
   89.50 +
   89.51 +   if (png_ptr == NULL) return;
   89.52 +#if defined(_WIN32_WCE)
   89.53 +   if ( !WriteFile((HANDLE)(png_ptr->io_ptr), data, length, &check, NULL) )
   89.54 +      check = 0;
   89.55 +#else
   89.56 +   check = fwrite(data, 1, length, (png_FILE_p)(png_ptr->io_ptr));
   89.57 +#endif
   89.58 +   if (check != length)
   89.59 +      png_error(png_ptr, "Write Error");
   89.60 +}
   89.61 +#else
   89.62 +/* this is the model-independent version. Since the standard I/O library
   89.63 +   can't handle far buffers in the medium and small models, we have to copy
   89.64 +   the data.
   89.65 +*/
   89.66 +
   89.67 +#define NEAR_BUF_SIZE 1024
   89.68 +#define MIN(a,b) (a <= b ? a : b)
   89.69 +
   89.70 +void PNGAPI
   89.71 +png_default_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
   89.72 +{
   89.73 +   png_uint_32 check;
   89.74 +   png_byte *near_data;  /* Needs to be "png_byte *" instead of "png_bytep" */
   89.75 +   png_FILE_p io_ptr;
   89.76 +
   89.77 +   if (png_ptr == NULL) return;
   89.78 +   /* Check if data really is near. If so, use usual code. */
   89.79 +   near_data = (png_byte *)CVT_PTR_NOCHECK(data);
   89.80 +   io_ptr = (png_FILE_p)CVT_PTR(png_ptr->io_ptr);
   89.81 +   if ((png_bytep)near_data == data)
   89.82 +   {
   89.83 +#if defined(_WIN32_WCE)
   89.84 +      if ( !WriteFile(io_ptr, near_data, length, &check, NULL) )
   89.85 +         check = 0;
   89.86 +#else
   89.87 +      check = fwrite(near_data, 1, length, io_ptr);
   89.88 +#endif
   89.89 +   }
   89.90 +   else
   89.91 +   {
   89.92 +      png_byte buf[NEAR_BUF_SIZE];
   89.93 +      png_size_t written, remaining, err;
   89.94 +      check = 0;
   89.95 +      remaining = length;
   89.96 +      do
   89.97 +      {
   89.98 +         written = MIN(NEAR_BUF_SIZE, remaining);
   89.99 +         png_memcpy(buf, data, written); /* copy far buffer to near buffer */
  89.100 +#if defined(_WIN32_WCE)
  89.101 +         if ( !WriteFile(io_ptr, buf, written, &err, NULL) )
  89.102 +            err = 0;
  89.103 +#else
  89.104 +         err = fwrite(buf, 1, written, io_ptr);
  89.105 +#endif
  89.106 +         if (err != written)
  89.107 +            break;
  89.108 +         else
  89.109 +            check += err;
  89.110 +         data += written;
  89.111 +         remaining -= written;
  89.112 +      }
  89.113 +      while (remaining != 0);
  89.114 +   }
  89.115 +   if (check != length)
  89.116 +      png_error(png_ptr, "Write Error");
  89.117 +}
  89.118 +
  89.119 +#endif
  89.120 +#endif
  89.121 +
  89.122 +/* This function is called to output any data pending writing (normally
  89.123 +   to disk).  After png_flush is called, there should be no data pending
  89.124 +   writing in any buffers. */
  89.125 +#if defined(PNG_WRITE_FLUSH_SUPPORTED)
  89.126 +void /* PRIVATE */
  89.127 +png_flush(png_structp png_ptr)
  89.128 +{
  89.129 +   if (png_ptr->output_flush_fn != NULL)
  89.130 +      (*(png_ptr->output_flush_fn))(png_ptr);
  89.131 +}
  89.132 +
  89.133 +#if !defined(PNG_NO_STDIO)
  89.134 +void PNGAPI
  89.135 +png_default_flush(png_structp png_ptr)
  89.136 +{
  89.137 +#if !defined(_WIN32_WCE)
  89.138 +   png_FILE_p io_ptr;
  89.139 +#endif
  89.140 +   if (png_ptr == NULL) return;
  89.141 +#if !defined(_WIN32_WCE)
  89.142 +   io_ptr = (png_FILE_p)CVT_PTR((png_ptr->io_ptr));
  89.143 +   if (io_ptr != NULL)
  89.144 +      fflush(io_ptr);
  89.145 +#endif
  89.146 +}
  89.147 +#endif
  89.148 +#endif
  89.149 +
  89.150 +/* This function allows the application to supply new output functions for
  89.151 +   libpng if standard C streams aren't being used.
  89.152 +
  89.153 +   This function takes as its arguments:
  89.154 +   png_ptr       - pointer to a png output data structure
  89.155 +   io_ptr        - pointer to user supplied structure containing info about
  89.156 +                   the output functions.  May be NULL.
  89.157 +   write_data_fn - pointer to a new output function that takes as its
  89.158 +                   arguments a pointer to a png_struct, a pointer to
  89.159 +                   data to be written, and a 32-bit unsigned int that is
  89.160 +                   the number of bytes to be written.  The new write
  89.161 +                   function should call png_error(png_ptr, "Error msg")
  89.162 +                   to exit and output any fatal error messages.
  89.163 +   flush_data_fn - pointer to a new flush function that takes as its
  89.164 +                   arguments a pointer to a png_struct.  After a call to
  89.165 +                   the flush function, there should be no data in any buffers
  89.166 +                   or pending transmission.  If the output method doesn't do
  89.167 +                   any buffering of ouput, a function prototype must still be
  89.168 +                   supplied although it doesn't have to do anything.  If
  89.169 +                   PNG_WRITE_FLUSH_SUPPORTED is not defined at libpng compile
  89.170 +                   time, output_flush_fn will be ignored, although it must be
  89.171 +                   supplied for compatibility. */
  89.172 +void PNGAPI
  89.173 +png_set_write_fn(png_structp png_ptr, png_voidp io_ptr,
  89.174 +   png_rw_ptr write_data_fn, png_flush_ptr output_flush_fn)
  89.175 +{
  89.176 +   if (png_ptr == NULL) return;
  89.177 +   png_ptr->io_ptr = io_ptr;
  89.178 +
  89.179 +#if !defined(PNG_NO_STDIO)
  89.180 +   if (write_data_fn != NULL)
  89.181 +      png_ptr->write_data_fn = write_data_fn;
  89.182 +   else
  89.183 +      png_ptr->write_data_fn = png_default_write_data;
  89.184 +#else
  89.185 +   png_ptr->write_data_fn = write_data_fn;
  89.186 +#endif
  89.187 +
  89.188 +#if defined(PNG_WRITE_FLUSH_SUPPORTED)
  89.189 +#if !defined(PNG_NO_STDIO)
  89.190 +   if (output_flush_fn != NULL)
  89.191 +      png_ptr->output_flush_fn = output_flush_fn;
  89.192 +   else
  89.193 +      png_ptr->output_flush_fn = png_default_flush;
  89.194 +#else
  89.195 +   png_ptr->output_flush_fn = output_flush_fn;
  89.196 +#endif
  89.197 +#endif /* PNG_WRITE_FLUSH_SUPPORTED */
  89.198 +
  89.199 +   /* It is an error to read while writing a png file */
  89.200 +   if (png_ptr->read_data_fn != NULL)
  89.201 +   {
  89.202 +      png_ptr->read_data_fn = NULL;
  89.203 +      png_warning(png_ptr,
  89.204 +         "Attempted to set both read_data_fn and write_data_fn in");
  89.205 +      png_warning(png_ptr,
  89.206 +         "the same structure.  Resetting read_data_fn to NULL.");
  89.207 +   }
  89.208 +}
  89.209 +
  89.210 +#if defined(USE_FAR_KEYWORD)
  89.211 +#if defined(_MSC_VER)
  89.212 +void *png_far_to_near(png_structp png_ptr, png_voidp ptr, int check)
  89.213 +{
  89.214 +   void *near_ptr;
  89.215 +   void FAR *far_ptr;
  89.216 +   FP_OFF(near_ptr) = FP_OFF(ptr);
  89.217 +   far_ptr = (void FAR *)near_ptr;
  89.218 +   if (check != 0)
  89.219 +      if (FP_SEG(ptr) != FP_SEG(far_ptr))
  89.220 +         png_error(png_ptr, "segment lost in conversion");
  89.221 +   return(near_ptr);
  89.222 +}
  89.223 +#  else
  89.224 +void *png_far_to_near(png_structp png_ptr, png_voidp ptr, int check)
  89.225 +{
  89.226 +   void *near_ptr;
  89.227 +   void FAR *far_ptr;
  89.228 +   near_ptr = (void FAR *)ptr;
  89.229 +   far_ptr = (void FAR *)near_ptr;
  89.230 +   if (check != 0)
  89.231 +      if (far_ptr != ptr)
  89.232 +         png_error(png_ptr, "segment lost in conversion");
  89.233 +   return(near_ptr);
  89.234 +}
  89.235 +#   endif
  89.236 +#   endif
  89.237 +#endif /* PNG_WRITE_SUPPORTED */
    90.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    90.2 +++ b/libs/libpng/pngwrite.c	Sat Sep 19 05:51:51 2015 +0300
    90.3 @@ -0,0 +1,1547 @@
    90.4 +
    90.5 +/* pngwrite.c - general routines to write a PNG file
    90.6 + *
    90.7 + * Last changed in libpng 1.2.31 [August 19, 2008]
    90.8 + * For conditions of distribution and use, see copyright notice in png.h
    90.9 + * Copyright (c) 1998-2008 Glenn Randers-Pehrson
   90.10 + * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
   90.11 + * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
   90.12 + */
   90.13 +
   90.14 +/* get internal access to png.h */
   90.15 +#define PNG_INTERNAL
   90.16 +#include "png.h"
   90.17 +#ifdef PNG_WRITE_SUPPORTED
   90.18 +
   90.19 +/* Writes all the PNG information.  This is the suggested way to use the
   90.20 + * library.  If you have a new chunk to add, make a function to write it,
   90.21 + * and put it in the correct location here.  If you want the chunk written
   90.22 + * after the image data, put it in png_write_end().  I strongly encourage
   90.23 + * you to supply a PNG_INFO_ flag, and check info_ptr->valid before writing
   90.24 + * the chunk, as that will keep the code from breaking if you want to just
   90.25 + * write a plain PNG file.  If you have long comments, I suggest writing
   90.26 + * them in png_write_end(), and compressing them.
   90.27 + */
   90.28 +void PNGAPI
   90.29 +png_write_info_before_PLTE(png_structp png_ptr, png_infop info_ptr)
   90.30 +{
   90.31 +   png_debug(1, "in png_write_info_before_PLTE\n");
   90.32 +   if (png_ptr == NULL || info_ptr == NULL)
   90.33 +      return;
   90.34 +   if (!(png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE))
   90.35 +   {
   90.36 +   png_write_sig(png_ptr); /* write PNG signature */
   90.37 +#if defined(PNG_MNG_FEATURES_SUPPORTED)
   90.38 +   if ((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE)&&(png_ptr->mng_features_permitted))
   90.39 +   {
   90.40 +      png_warning(png_ptr, "MNG features are not allowed in a PNG datastream");
   90.41 +      png_ptr->mng_features_permitted=0;
   90.42 +   }
   90.43 +#endif
   90.44 +   /* write IHDR information. */
   90.45 +   png_write_IHDR(png_ptr, info_ptr->width, info_ptr->height,
   90.46 +      info_ptr->bit_depth, info_ptr->color_type, info_ptr->compression_type,
   90.47 +      info_ptr->filter_type,
   90.48 +#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
   90.49 +      info_ptr->interlace_type);
   90.50 +#else
   90.51 +      0);
   90.52 +#endif
   90.53 +   /* the rest of these check to see if the valid field has the appropriate
   90.54 +      flag set, and if it does, writes the chunk. */
   90.55 +#if defined(PNG_WRITE_gAMA_SUPPORTED)
   90.56 +   if (info_ptr->valid & PNG_INFO_gAMA)
   90.57 +   {
   90.58 +#  ifdef PNG_FLOATING_POINT_SUPPORTED
   90.59 +      png_write_gAMA(png_ptr, info_ptr->gamma);
   90.60 +#else
   90.61 +#ifdef PNG_FIXED_POINT_SUPPORTED
   90.62 +      png_write_gAMA_fixed(png_ptr, info_ptr->int_gamma);
   90.63 +#  endif
   90.64 +#endif
   90.65 +   }
   90.66 +#endif
   90.67 +#if defined(PNG_WRITE_sRGB_SUPPORTED)
   90.68 +   if (info_ptr->valid & PNG_INFO_sRGB)
   90.69 +      png_write_sRGB(png_ptr, (int)info_ptr->srgb_intent);
   90.70 +#endif
   90.71 +#if defined(PNG_WRITE_iCCP_SUPPORTED)
   90.72 +   if (info_ptr->valid & PNG_INFO_iCCP)
   90.73 +      png_write_iCCP(png_ptr, info_ptr->iccp_name, PNG_COMPRESSION_TYPE_BASE,
   90.74 +                     info_ptr->iccp_profile, (int)info_ptr->iccp_proflen);
   90.75 +#endif
   90.76 +#if defined(PNG_WRITE_sBIT_SUPPORTED)
   90.77 +   if (info_ptr->valid & PNG_INFO_sBIT)
   90.78 +      png_write_sBIT(png_ptr, &(info_ptr->sig_bit), info_ptr->color_type);
   90.79 +#endif
   90.80 +#if defined(PNG_WRITE_cHRM_SUPPORTED)
   90.81 +   if (info_ptr->valid & PNG_INFO_cHRM)
   90.82 +   {
   90.83 +#ifdef PNG_FLOATING_POINT_SUPPORTED
   90.84 +      png_write_cHRM(png_ptr,
   90.85 +         info_ptr->x_white, info_ptr->y_white,
   90.86 +         info_ptr->x_red, info_ptr->y_red,
   90.87 +         info_ptr->x_green, info_ptr->y_green,
   90.88 +         info_ptr->x_blue, info_ptr->y_blue);
   90.89 +#else
   90.90 +#  ifdef PNG_FIXED_POINT_SUPPORTED
   90.91 +      png_write_cHRM_fixed(png_ptr,
   90.92 +         info_ptr->int_x_white, info_ptr->int_y_white,
   90.93 +         info_ptr->int_x_red, info_ptr->int_y_red,
   90.94 +         info_ptr->int_x_green, info_ptr->int_y_green,
   90.95 +         info_ptr->int_x_blue, info_ptr->int_y_blue);
   90.96 +#  endif
   90.97 +#endif
   90.98 +   }
   90.99 +#endif
  90.100 +#if defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED)
  90.101 +   if (info_ptr->unknown_chunks_num)
  90.102 +   {
  90.103 +       png_unknown_chunk *up;
  90.104 +
  90.105 +       png_debug(5, "writing extra chunks\n");
  90.106 +
  90.107 +       for (up = info_ptr->unknown_chunks;
  90.108 +            up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
  90.109 +            up++)
  90.110 +       {
  90.111 +         int keep=png_handle_as_unknown(png_ptr, up->name);
  90.112 +         if (keep != PNG_HANDLE_CHUNK_NEVER &&
  90.113 +            up->location && !(up->location & PNG_HAVE_PLTE) &&
  90.114 +            !(up->location & PNG_HAVE_IDAT) &&
  90.115 +            ((up->name[3] & 0x20) || keep == PNG_HANDLE_CHUNK_ALWAYS ||
  90.116 +            (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS)))
  90.117 +         {
  90.118 +            if (up->size == 0)
  90.119 +               png_warning(png_ptr, "Writing zero-length unknown chunk");
  90.120 +            png_write_chunk(png_ptr, up->name, up->data, up->size);
  90.121 +         }
  90.122 +       }
  90.123 +   }
  90.124 +#endif
  90.125 +      png_ptr->mode |= PNG_WROTE_INFO_BEFORE_PLTE;
  90.126 +   }
  90.127 +}
  90.128 +
  90.129 +void PNGAPI
  90.130 +png_write_info(png_structp png_ptr, png_infop info_ptr)
  90.131 +{
  90.132 +#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
  90.133 +   int i;
  90.134 +#endif
  90.135 +
  90.136 +   png_debug(1, "in png_write_info\n");
  90.137 +
  90.138 +   if (png_ptr == NULL || info_ptr == NULL)
  90.139 +      return;
  90.140 +
  90.141 +   png_write_info_before_PLTE(png_ptr, info_ptr);
  90.142 +
  90.143 +   if (info_ptr->valid & PNG_INFO_PLTE)
  90.144 +      png_write_PLTE(png_ptr, info_ptr->palette,
  90.145 +         (png_uint_32)info_ptr->num_palette);
  90.146 +   else if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  90.147 +      png_error(png_ptr, "Valid palette required for paletted images");
  90.148 +
  90.149 +#if defined(PNG_WRITE_tRNS_SUPPORTED)
  90.150 +   if (info_ptr->valid & PNG_INFO_tRNS)
  90.151 +      {
  90.152 +#if defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED)
  90.153 +         /* invert the alpha channel (in tRNS) */
  90.154 +         if ((png_ptr->transformations & PNG_INVERT_ALPHA) &&
  90.155 +            info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  90.156 +         {
  90.157 +            int j;
  90.158 +            for (j=0; j<(int)info_ptr->num_trans; j++)
  90.159 +               info_ptr->trans[j] = (png_byte)(255 - info_ptr->trans[j]);
  90.160 +         }
  90.161 +#endif
  90.162 +      png_write_tRNS(png_ptr, info_ptr->trans, &(info_ptr->trans_values),
  90.163 +         info_ptr->num_trans, info_ptr->color_type);
  90.164 +      }
  90.165 +#endif
  90.166 +#if defined(PNG_WRITE_bKGD_SUPPORTED)
  90.167 +   if (info_ptr->valid & PNG_INFO_bKGD)
  90.168 +      png_write_bKGD(png_ptr, &(info_ptr->background), info_ptr->color_type);
  90.169 +#endif
  90.170 +#if defined(PNG_WRITE_hIST_SUPPORTED)
  90.171 +   if (info_ptr->valid & PNG_INFO_hIST)
  90.172 +      png_write_hIST(png_ptr, info_ptr->hist, info_ptr->num_palette);
  90.173 +#endif
  90.174 +#if defined(PNG_WRITE_oFFs_SUPPORTED)
  90.175 +   if (info_ptr->valid & PNG_INFO_oFFs)
  90.176 +      png_write_oFFs(png_ptr, info_ptr->x_offset, info_ptr->y_offset,
  90.177 +         info_ptr->offset_unit_type);
  90.178 +#endif
  90.179 +#if defined(PNG_WRITE_pCAL_SUPPORTED)
  90.180 +   if (info_ptr->valid & PNG_INFO_pCAL)
  90.181 +      png_write_pCAL(png_ptr, info_ptr->pcal_purpose, info_ptr->pcal_X0,
  90.182 +         info_ptr->pcal_X1, info_ptr->pcal_type, info_ptr->pcal_nparams,
  90.183 +         info_ptr->pcal_units, info_ptr->pcal_params);
  90.184 +#endif
  90.185 +#if defined(PNG_WRITE_sCAL_SUPPORTED)
  90.186 +   if (info_ptr->valid & PNG_INFO_sCAL)
  90.187 +#if defined(PNG_FLOATING_POINT_SUPPORTED) && !defined(PNG_NO_STDIO)
  90.188 +      png_write_sCAL(png_ptr, (int)info_ptr->scal_unit,
  90.189 +          info_ptr->scal_pixel_width, info_ptr->scal_pixel_height);
  90.190 +#else
  90.191 +#ifdef PNG_FIXED_POINT_SUPPORTED
  90.192 +      png_write_sCAL_s(png_ptr, (int)info_ptr->scal_unit,
  90.193 +          info_ptr->scal_s_width, info_ptr->scal_s_height);
  90.194 +#else
  90.195 +      png_warning(png_ptr,
  90.196 +          "png_write_sCAL not supported; sCAL chunk not written.");
  90.197 +#endif
  90.198 +#endif
  90.199 +#endif
  90.200 +#if defined(PNG_WRITE_pHYs_SUPPORTED)
  90.201 +   if (info_ptr->valid & PNG_INFO_pHYs)
  90.202 +      png_write_pHYs(png_ptr, info_ptr->x_pixels_per_unit,
  90.203 +         info_ptr->y_pixels_per_unit, info_ptr->phys_unit_type);
  90.204 +#endif
  90.205 +#if defined(PNG_WRITE_tIME_SUPPORTED)
  90.206 +   if (info_ptr->valid & PNG_INFO_tIME)
  90.207 +   {
  90.208 +      png_write_tIME(png_ptr, &(info_ptr->mod_time));
  90.209 +      png_ptr->mode |= PNG_WROTE_tIME;
  90.210 +   }
  90.211 +#endif
  90.212 +#if defined(PNG_WRITE_sPLT_SUPPORTED)
  90.213 +   if (info_ptr->valid & PNG_INFO_sPLT)
  90.214 +     for (i = 0; i < (int)info_ptr->splt_palettes_num; i++)
  90.215 +       png_write_sPLT(png_ptr, info_ptr->splt_palettes + i);
  90.216 +#endif
  90.217 +#if defined(PNG_WRITE_TEXT_SUPPORTED)
  90.218 +   /* Check to see if we need to write text chunks */
  90.219 +   for (i = 0; i < info_ptr->num_text; i++)
  90.220 +   {
  90.221 +      png_debug2(2, "Writing header text chunk %d, type %d\n", i,
  90.222 +         info_ptr->text[i].compression);
  90.223 +      /* an internationalized chunk? */
  90.224 +      if (info_ptr->text[i].compression > 0)
  90.225 +      {
  90.226 +#if defined(PNG_WRITE_iTXt_SUPPORTED)
  90.227 +          /* write international chunk */
  90.228 +          png_write_iTXt(png_ptr,
  90.229 +                         info_ptr->text[i].compression,
  90.230 +                         info_ptr->text[i].key,
  90.231 +                         info_ptr->text[i].lang,
  90.232 +                         info_ptr->text[i].lang_key,
  90.233 +                         info_ptr->text[i].text);
  90.234 +#else
  90.235 +          png_warning(png_ptr, "Unable to write international text");
  90.236 +#endif
  90.237 +          /* Mark this chunk as written */
  90.238 +          info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
  90.239 +      }
  90.240 +      /* If we want a compressed text chunk */
  90.241 +      else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_zTXt)
  90.242 +      {
  90.243 +#if defined(PNG_WRITE_zTXt_SUPPORTED)
  90.244 +         /* write compressed chunk */
  90.245 +         png_write_zTXt(png_ptr, info_ptr->text[i].key,
  90.246 +            info_ptr->text[i].text, 0,
  90.247 +            info_ptr->text[i].compression);
  90.248 +#else
  90.249 +         png_warning(png_ptr, "Unable to write compressed text");
  90.250 +#endif
  90.251 +         /* Mark this chunk as written */
  90.252 +         info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
  90.253 +      }
  90.254 +      else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
  90.255 +      {
  90.256 +#if defined(PNG_WRITE_tEXt_SUPPORTED)
  90.257 +         /* write uncompressed chunk */
  90.258 +         png_write_tEXt(png_ptr, info_ptr->text[i].key,
  90.259 +                         info_ptr->text[i].text,
  90.260 +                         0);
  90.261 +#else
  90.262 +         png_warning(png_ptr, "Unable to write uncompressed text");
  90.263 +#endif
  90.264 +         /* Mark this chunk as written */
  90.265 +         info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
  90.266 +      }
  90.267 +   }
  90.268 +#endif
  90.269 +#if defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED)
  90.270 +   if (info_ptr->unknown_chunks_num)
  90.271 +   {
  90.272 +       png_unknown_chunk *up;
  90.273 +
  90.274 +       png_debug(5, "writing extra chunks\n");
  90.275 +
  90.276 +       for (up = info_ptr->unknown_chunks;
  90.277 +            up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
  90.278 +            up++)
  90.279 +       {
  90.280 +         int keep=png_handle_as_unknown(png_ptr, up->name);
  90.281 +         if (keep != PNG_HANDLE_CHUNK_NEVER &&
  90.282 +            up->location && (up->location & PNG_HAVE_PLTE) &&
  90.283 +            !(up->location & PNG_HAVE_IDAT) &&
  90.284 +            ((up->name[3] & 0x20) || keep == PNG_HANDLE_CHUNK_ALWAYS ||
  90.285 +            (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS)))
  90.286 +         {
  90.287 +            png_write_chunk(png_ptr, up->name, up->data, up->size);
  90.288 +         }
  90.289 +       }
  90.290 +   }
  90.291 +#endif
  90.292 +}
  90.293 +
  90.294 +/* Writes the end of the PNG file.  If you don't want to write comments or
  90.295 + * time information, you can pass NULL for info.  If you already wrote these
  90.296 + * in png_write_info(), do not write them again here.  If you have long
  90.297 + * comments, I suggest writing them here, and compressing them.
  90.298 + */
  90.299 +void PNGAPI
  90.300 +png_write_end(png_structp png_ptr, png_infop info_ptr)
  90.301 +{
  90.302 +   png_debug(1, "in png_write_end\n");
  90.303 +   if (png_ptr == NULL)
  90.304 +      return;
  90.305 +   if (!(png_ptr->mode & PNG_HAVE_IDAT))
  90.306 +      png_error(png_ptr, "No IDATs written into file");
  90.307 +
  90.308 +   /* see if user wants us to write information chunks */
  90.309 +   if (info_ptr != NULL)
  90.310 +   {
  90.311 +#if defined(PNG_WRITE_TEXT_SUPPORTED)
  90.312 +      int i; /* local index variable */
  90.313 +#endif
  90.314 +#if defined(PNG_WRITE_tIME_SUPPORTED)
  90.315 +      /* check to see if user has supplied a time chunk */
  90.316 +      if ((info_ptr->valid & PNG_INFO_tIME) &&
  90.317 +         !(png_ptr->mode & PNG_WROTE_tIME))
  90.318 +         png_write_tIME(png_ptr, &(info_ptr->mod_time));
  90.319 +#endif
  90.320 +#if defined(PNG_WRITE_TEXT_SUPPORTED)
  90.321 +      /* loop through comment chunks */
  90.322 +      for (i = 0; i < info_ptr->num_text; i++)
  90.323 +      {
  90.324 +         png_debug2(2, "Writing trailer text chunk %d, type %d\n", i,
  90.325 +            info_ptr->text[i].compression);
  90.326 +         /* an internationalized chunk? */
  90.327 +         if (info_ptr->text[i].compression > 0)
  90.328 +         {
  90.329 +#if defined(PNG_WRITE_iTXt_SUPPORTED)
  90.330 +             /* write international chunk */
  90.331 +             png_write_iTXt(png_ptr,
  90.332 +                         info_ptr->text[i].compression,
  90.333 +                         info_ptr->text[i].key,
  90.334 +                         info_ptr->text[i].lang,
  90.335 +                         info_ptr->text[i].lang_key,
  90.336 +                         info_ptr->text[i].text);
  90.337 +#else
  90.338 +             png_warning(png_ptr, "Unable to write international text");
  90.339 +#endif
  90.340 +             /* Mark this chunk as written */
  90.341 +             info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
  90.342 +         }
  90.343 +         else if (info_ptr->text[i].compression >= PNG_TEXT_COMPRESSION_zTXt)
  90.344 +         {
  90.345 +#if defined(PNG_WRITE_zTXt_SUPPORTED)
  90.346 +            /* write compressed chunk */
  90.347 +            png_write_zTXt(png_ptr, info_ptr->text[i].key,
  90.348 +               info_ptr->text[i].text, 0,
  90.349 +               info_ptr->text[i].compression);
  90.350 +#else
  90.351 +            png_warning(png_ptr, "Unable to write compressed text");
  90.352 +#endif
  90.353 +            /* Mark this chunk as written */
  90.354 +            info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
  90.355 +         }
  90.356 +         else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
  90.357 +         {
  90.358 +#if defined(PNG_WRITE_tEXt_SUPPORTED)
  90.359 +            /* write uncompressed chunk */
  90.360 +            png_write_tEXt(png_ptr, info_ptr->text[i].key,
  90.361 +               info_ptr->text[i].text, 0);
  90.362 +#else
  90.363 +            png_warning(png_ptr, "Unable to write uncompressed text");
  90.364 +#endif
  90.365 +
  90.366 +            /* Mark this chunk as written */
  90.367 +            info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
  90.368 +         }
  90.369 +      }
  90.370 +#endif
  90.371 +#if defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED)
  90.372 +   if (info_ptr->unknown_chunks_num)
  90.373 +   {
  90.374 +       png_unknown_chunk *up;
  90.375 +
  90.376 +       png_debug(5, "writing extra chunks\n");
  90.377 +
  90.378 +       for (up = info_ptr->unknown_chunks;
  90.379 +            up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
  90.380 +            up++)
  90.381 +       {
  90.382 +         int keep=png_handle_as_unknown(png_ptr, up->name);
  90.383 +         if (keep != PNG_HANDLE_CHUNK_NEVER &&
  90.384 +            up->location && (up->location & PNG_AFTER_IDAT) &&
  90.385 +            ((up->name[3] & 0x20) || keep == PNG_HANDLE_CHUNK_ALWAYS ||
  90.386 +            (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS)))
  90.387 +         {
  90.388 +            png_write_chunk(png_ptr, up->name, up->data, up->size);
  90.389 +         }
  90.390 +       }
  90.391 +   }
  90.392 +#endif
  90.393 +   }
  90.394 +
  90.395 +   png_ptr->mode |= PNG_AFTER_IDAT;
  90.396 +
  90.397 +   /* write end of PNG file */
  90.398 +   png_write_IEND(png_ptr);
  90.399 +   /* This flush, added in libpng-1.0.8, removed from libpng-1.0.9beta03,
  90.400 +    * and restored again in libpng-1.2.30, may cause some applications that
  90.401 +    * do not set png_ptr->output_flush_fn to crash.  If your application
  90.402 +    * experiences a problem, please try building libpng with
  90.403 +    * PNG_WRITE_FLUSH_AFTER_IEND_SUPPORTED defined, and report the event to
  90.404 +    * png-mng-implement at lists.sf.net .  This kludge will be removed
  90.405 +    * from libpng-1.4.0.
  90.406 +    */
  90.407 +#if defined(PNG_WRITE_FLUSH_SUPPORTED) && \
  90.408 +    defined(PNG_WRITE_FLUSH_AFTER_IEND_SUPPORTED)
  90.409 +   png_flush(png_ptr);
  90.410 +#endif
  90.411 +}
  90.412 +
  90.413 +#if defined(PNG_WRITE_tIME_SUPPORTED)
  90.414 +#if !defined(_WIN32_WCE)
  90.415 +/* "time.h" functions are not supported on WindowsCE */
  90.416 +void PNGAPI
  90.417 +png_convert_from_struct_tm(png_timep ptime, struct tm FAR * ttime)
  90.418 +{
  90.419 +   png_debug(1, "in png_convert_from_struct_tm\n");
  90.420 +   ptime->year = (png_uint_16)(1900 + ttime->tm_year);
  90.421 +   ptime->month = (png_byte)(ttime->tm_mon + 1);
  90.422 +   ptime->day = (png_byte)ttime->tm_mday;
  90.423 +   ptime->hour = (png_byte)ttime->tm_hour;
  90.424 +   ptime->minute = (png_byte)ttime->tm_min;
  90.425 +   ptime->second = (png_byte)ttime->tm_sec;
  90.426 +}
  90.427 +
  90.428 +void PNGAPI
  90.429 +png_convert_from_time_t(png_timep ptime, time_t ttime)
  90.430 +{
  90.431 +   struct tm *tbuf;
  90.432 +
  90.433 +   png_debug(1, "in png_convert_from_time_t\n");
  90.434 +   tbuf = gmtime(&ttime);
  90.435 +   png_convert_from_struct_tm(ptime, tbuf);
  90.436 +}
  90.437 +#endif
  90.438 +#endif
  90.439 +
  90.440 +/* Initialize png_ptr structure, and allocate any memory needed */
  90.441 +png_structp PNGAPI
  90.442 +png_create_write_struct(png_const_charp user_png_ver, png_voidp error_ptr,
  90.443 +   png_error_ptr error_fn, png_error_ptr warn_fn)
  90.444 +{
  90.445 +#ifdef PNG_USER_MEM_SUPPORTED
  90.446 +   return (png_create_write_struct_2(user_png_ver, error_ptr, error_fn,
  90.447 +      warn_fn, png_voidp_NULL, png_malloc_ptr_NULL, png_free_ptr_NULL));
  90.448 +}
  90.449 +
  90.450 +/* Alternate initialize png_ptr structure, and allocate any memory needed */
  90.451 +png_structp PNGAPI
  90.452 +png_create_write_struct_2(png_const_charp user_png_ver, png_voidp error_ptr,
  90.453 +   png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
  90.454 +   png_malloc_ptr malloc_fn, png_free_ptr free_fn)
  90.455 +{
  90.456 +#endif /* PNG_USER_MEM_SUPPORTED */
  90.457 +#ifdef PNG_SETJMP_SUPPORTED
  90.458 +    volatile
  90.459 +#endif
  90.460 +    png_structp png_ptr;
  90.461 +#ifdef PNG_SETJMP_SUPPORTED
  90.462 +#ifdef USE_FAR_KEYWORD
  90.463 +   jmp_buf jmpbuf;
  90.464 +#endif
  90.465 +#endif
  90.466 +   int i;
  90.467 +   png_debug(1, "in png_create_write_struct\n");
  90.468 +#ifdef PNG_USER_MEM_SUPPORTED
  90.469 +   png_ptr = (png_structp)png_create_struct_2(PNG_STRUCT_PNG,
  90.470 +      (png_malloc_ptr)malloc_fn, (png_voidp)mem_ptr);
  90.471 +#else
  90.472 +   png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG);
  90.473 +#endif /* PNG_USER_MEM_SUPPORTED */
  90.474 +   if (png_ptr == NULL)
  90.475 +      return (NULL);
  90.476 +
  90.477 +   /* added at libpng-1.2.6 */
  90.478 +#ifdef PNG_SET_USER_LIMITS_SUPPORTED
  90.479 +   png_ptr->user_width_max=PNG_USER_WIDTH_MAX;
  90.480 +   png_ptr->user_height_max=PNG_USER_HEIGHT_MAX;
  90.481 +#endif
  90.482 +
  90.483 +#ifdef PNG_SETJMP_SUPPORTED
  90.484 +#ifdef USE_FAR_KEYWORD
  90.485 +   if (setjmp(jmpbuf))
  90.486 +#else
  90.487 +   if (setjmp(png_ptr->jmpbuf))
  90.488 +#endif
  90.489 +   {
  90.490 +      png_free(png_ptr, png_ptr->zbuf);
  90.491 +       png_ptr->zbuf=NULL;
  90.492 +      png_destroy_struct(png_ptr);
  90.493 +      return (NULL);
  90.494 +   }
  90.495 +#ifdef USE_FAR_KEYWORD
  90.496 +   png_memcpy(png_ptr->jmpbuf, jmpbuf, png_sizeof(jmp_buf));
  90.497 +#endif
  90.498 +#endif
  90.499 +
  90.500 +#ifdef PNG_USER_MEM_SUPPORTED
  90.501 +   png_set_mem_fn(png_ptr, mem_ptr, malloc_fn, free_fn);
  90.502 +#endif /* PNG_USER_MEM_SUPPORTED */
  90.503 +   png_set_error_fn(png_ptr, error_ptr, error_fn, warn_fn);
  90.504 +
  90.505 +   if (user_png_ver)
  90.506 +   {
  90.507 +     i=0;
  90.508 +     do
  90.509 +     {
  90.510 +       if (user_png_ver[i] != png_libpng_ver[i])
  90.511 +          png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
  90.512 +     } while (png_libpng_ver[i++]);
  90.513 +   }
  90.514 +
  90.515 +   if (png_ptr->flags & PNG_FLAG_LIBRARY_MISMATCH)
  90.516 +   {
  90.517 +     /* Libpng 0.90 and later are binary incompatible with libpng 0.89, so
  90.518 +      * we must recompile any applications that use any older library version.
  90.519 +      * For versions after libpng 1.0, we will be compatible, so we need
  90.520 +      * only check the first digit.
  90.521 +      */
  90.522 +     if (user_png_ver == NULL || user_png_ver[0] != png_libpng_ver[0] ||
  90.523 +         (user_png_ver[0] == '1' && user_png_ver[2] != png_libpng_ver[2]) ||
  90.524 +         (user_png_ver[0] == '0' && user_png_ver[2] < '9'))
  90.525 +     {
  90.526 +#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
  90.527 +        char msg[80];
  90.528 +        if (user_png_ver)
  90.529 +        {
  90.530 +          png_snprintf(msg, 80,
  90.531 +             "Application was compiled with png.h from libpng-%.20s",
  90.532 +             user_png_ver);
  90.533 +          png_warning(png_ptr, msg);
  90.534 +        }
  90.535 +        png_snprintf(msg, 80,
  90.536 +           "Application  is  running with png.c from libpng-%.20s",
  90.537 +           png_libpng_ver);
  90.538 +        png_warning(png_ptr, msg);
  90.539 +#endif
  90.540 +#ifdef PNG_ERROR_NUMBERS_SUPPORTED
  90.541 +        png_ptr->flags=0;
  90.542 +#endif
  90.543 +        png_error(png_ptr,
  90.544 +           "Incompatible libpng version in application and library");
  90.545 +     }
  90.546 +   }
  90.547 +
  90.548 +   /* initialize zbuf - compression buffer */
  90.549 +   png_ptr->zbuf_size = PNG_ZBUF_SIZE;
  90.550 +   png_ptr->zbuf = (png_bytep)png_malloc(png_ptr,
  90.551 +      (png_uint_32)png_ptr->zbuf_size);
  90.552 +
  90.553 +   png_set_write_fn(png_ptr, png_voidp_NULL, png_rw_ptr_NULL,
  90.554 +      png_flush_ptr_NULL);
  90.555 +
  90.556 +#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
  90.557 +   png_set_filter_heuristics(png_ptr, PNG_FILTER_HEURISTIC_DEFAULT,
  90.558 +      1, png_doublep_NULL, png_doublep_NULL);
  90.559 +#endif
  90.560 +
  90.561 +#ifdef PNG_SETJMP_SUPPORTED
  90.562 +/* Applications that neglect to set up their own setjmp() and then encounter
  90.563 +   a png_error() will longjmp here.  Since the jmpbuf is then meaningless we
  90.564 +   abort instead of returning. */
  90.565 +#ifdef USE_FAR_KEYWORD
  90.566 +   if (setjmp(jmpbuf))
  90.567 +      PNG_ABORT();
  90.568 +   png_memcpy(png_ptr->jmpbuf, jmpbuf, png_sizeof(jmp_buf));
  90.569 +#else
  90.570 +   if (setjmp(png_ptr->jmpbuf))
  90.571 +      PNG_ABORT();
  90.572 +#endif
  90.573 +#endif
  90.574 +   return (png_ptr);
  90.575 +}
  90.576 +
  90.577 +/* Initialize png_ptr structure, and allocate any memory needed */
  90.578 +#if defined(PNG_1_0_X) || defined(PNG_1_2_X)
  90.579 +/* Deprecated. */
  90.580 +#undef png_write_init
  90.581 +void PNGAPI
  90.582 +png_write_init(png_structp png_ptr)
  90.583 +{
  90.584 +   /* We only come here via pre-1.0.7-compiled applications */
  90.585 +   png_write_init_2(png_ptr, "1.0.6 or earlier", 0, 0);
  90.586 +}
  90.587 +
  90.588 +void PNGAPI
  90.589 +png_write_init_2(png_structp png_ptr, png_const_charp user_png_ver,
  90.590 +   png_size_t png_struct_size, png_size_t png_info_size)
  90.591 +{
  90.592 +   /* We only come here via pre-1.0.12-compiled applications */
  90.593 +   if (png_ptr == NULL) return;
  90.594 +#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
  90.595 +   if (png_sizeof(png_struct) > png_struct_size ||
  90.596 +      png_sizeof(png_info) > png_info_size)
  90.597 +   {
  90.598 +      char msg[80];
  90.599 +      png_ptr->warning_fn=NULL;
  90.600 +      if (user_png_ver)
  90.601 +      {
  90.602 +        png_snprintf(msg, 80,
  90.603 +           "Application was compiled with png.h from libpng-%.20s",
  90.604 +           user_png_ver);
  90.605 +        png_warning(png_ptr, msg);
  90.606 +      }
  90.607 +      png_snprintf(msg, 80,
  90.608 +         "Application  is  running with png.c from libpng-%.20s",
  90.609 +         png_libpng_ver);
  90.610 +      png_warning(png_ptr, msg);
  90.611 +   }
  90.612 +#endif
  90.613 +   if (png_sizeof(png_struct) > png_struct_size)
  90.614 +     {
  90.615 +       png_ptr->error_fn=NULL;
  90.616 +#ifdef PNG_ERROR_NUMBERS_SUPPORTED
  90.617 +       png_ptr->flags=0;
  90.618 +#endif
  90.619 +       png_error(png_ptr,
  90.620 +       "The png struct allocated by the application for writing is too small.");
  90.621 +     }
  90.622 +   if (png_sizeof(png_info) > png_info_size)
  90.623 +     {
  90.624 +       png_ptr->error_fn=NULL;
  90.625 +#ifdef PNG_ERROR_NUMBERS_SUPPORTED
  90.626 +       png_ptr->flags=0;
  90.627 +#endif
  90.628 +       png_error(png_ptr,
  90.629 +       "The info struct allocated by the application for writing is too small.");
  90.630 +     }
  90.631 +   png_write_init_3(&png_ptr, user_png_ver, png_struct_size);
  90.632 +}
  90.633 +#endif /* PNG_1_0_X || PNG_1_2_X */
  90.634 +
  90.635 +
  90.636 +void PNGAPI
  90.637 +png_write_init_3(png_structpp ptr_ptr, png_const_charp user_png_ver,
  90.638 +   png_size_t png_struct_size)
  90.639 +{
  90.640 +   png_structp png_ptr=*ptr_ptr;
  90.641 +#ifdef PNG_SETJMP_SUPPORTED
  90.642 +   jmp_buf tmp_jmp; /* to save current jump buffer */
  90.643 +#endif
  90.644 +
  90.645 +   int i = 0;
  90.646 +
  90.647 +   if (png_ptr == NULL)
  90.648 +      return;
  90.649 +
  90.650 +   do
  90.651 +   {
  90.652 +     if (user_png_ver[i] != png_libpng_ver[i])
  90.653 +     {
  90.654 +#ifdef PNG_LEGACY_SUPPORTED
  90.655 +       png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
  90.656 +#else
  90.657 +       png_ptr->warning_fn=NULL;
  90.658 +       png_warning(png_ptr,
  90.659 + "Application uses deprecated png_write_init() and should be recompiled.");
  90.660 +       break;
  90.661 +#endif
  90.662 +     }
  90.663 +   } while (png_libpng_ver[i++]);
  90.664 +
  90.665 +   png_debug(1, "in png_write_init_3\n");
  90.666 +
  90.667 +#ifdef PNG_SETJMP_SUPPORTED
  90.668 +   /* save jump buffer and error functions */
  90.669 +   png_memcpy(tmp_jmp, png_ptr->jmpbuf, png_sizeof(jmp_buf));
  90.670 +#endif
  90.671 +
  90.672 +   if (png_sizeof(png_struct) > png_struct_size)
  90.673 +     {
  90.674 +       png_destroy_struct(png_ptr);
  90.675 +       png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG);
  90.676 +       *ptr_ptr = png_ptr;
  90.677 +     }
  90.678 +
  90.679 +   /* reset all variables to 0 */
  90.680 +   png_memset(png_ptr, 0, png_sizeof(png_struct));
  90.681 +
  90.682 +   /* added at libpng-1.2.6 */
  90.683 +#ifdef PNG_SET_USER_LIMITS_SUPPORTED
  90.684 +   png_ptr->user_width_max=PNG_USER_WIDTH_MAX;
  90.685 +   png_ptr->user_height_max=PNG_USER_HEIGHT_MAX;
  90.686 +#endif
  90.687 +
  90.688 +#ifdef PNG_SETJMP_SUPPORTED
  90.689 +   /* restore jump buffer */
  90.690 +   png_memcpy(png_ptr->jmpbuf, tmp_jmp, png_sizeof(jmp_buf));
  90.691 +#endif
  90.692 +
  90.693 +   png_set_write_fn(png_ptr, png_voidp_NULL, png_rw_ptr_NULL,
  90.694 +      png_flush_ptr_NULL);
  90.695 +
  90.696 +   /* initialize zbuf - compression buffer */
  90.697 +   png_ptr->zbuf_size = PNG_ZBUF_SIZE;
  90.698 +   png_ptr->zbuf = (png_bytep)png_malloc(png_ptr,
  90.699 +      (png_uint_32)png_ptr->zbuf_size);
  90.700 +
  90.701 +#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
  90.702 +   png_set_filter_heuristics(png_ptr, PNG_FILTER_HEURISTIC_DEFAULT,
  90.703 +      1, png_doublep_NULL, png_doublep_NULL);
  90.704 +#endif
  90.705 +}
  90.706 +
  90.707 +/* Write a few rows of image data.  If the image is interlaced,
  90.708 + * either you will have to write the 7 sub images, or, if you
  90.709 + * have called png_set_interlace_handling(), you will have to
  90.710 + * "write" the image seven times.
  90.711 + */
  90.712 +void PNGAPI
  90.713 +png_write_rows(png_structp png_ptr, png_bytepp row,
  90.714 +   png_uint_32 num_rows)
  90.715 +{
  90.716 +   png_uint_32 i; /* row counter */
  90.717 +   png_bytepp rp; /* row pointer */
  90.718 +
  90.719 +   png_debug(1, "in png_write_rows\n");
  90.720 +
  90.721 +   if (png_ptr == NULL)
  90.722 +      return;
  90.723 +
  90.724 +   /* loop through the rows */
  90.725 +   for (i = 0, rp = row; i < num_rows; i++, rp++)
  90.726 +   {
  90.727 +      png_write_row(png_ptr, *rp);
  90.728 +   }
  90.729 +}
  90.730 +
  90.731 +/* Write the image.  You only need to call this function once, even
  90.732 + * if you are writing an interlaced image.
  90.733 + */
  90.734 +void PNGAPI
  90.735 +png_write_image(png_structp png_ptr, png_bytepp image)
  90.736 +{
  90.737 +   png_uint_32 i; /* row index */
  90.738 +   int pass, num_pass; /* pass variables */
  90.739 +   png_bytepp rp; /* points to current row */
  90.740 +
  90.741 +   if (png_ptr == NULL)
  90.742 +      return;
  90.743 +
  90.744 +   png_debug(1, "in png_write_image\n");
  90.745 +#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
  90.746 +   /* intialize interlace handling.  If image is not interlaced,
  90.747 +      this will set pass to 1 */
  90.748 +   num_pass = png_set_interlace_handling(png_ptr);
  90.749 +#else
  90.750 +   num_pass = 1;
  90.751 +#endif
  90.752 +   /* loop through passes */
  90.753 +   for (pass = 0; pass < num_pass; pass++)
  90.754 +   {
  90.755 +      /* loop through image */
  90.756 +      for (i = 0, rp = image; i < png_ptr->height; i++, rp++)
  90.757 +      {
  90.758 +         png_write_row(png_ptr, *rp);
  90.759 +      }
  90.760 +   }
  90.761 +}
  90.762 +
  90.763 +/* called by user to write a row of image data */
  90.764 +void PNGAPI
  90.765 +png_write_row(png_structp png_ptr, png_bytep row)
  90.766 +{
  90.767 +   if (png_ptr == NULL)
  90.768 +      return;
  90.769 +   png_debug2(1, "in png_write_row (row %ld, pass %d)\n",
  90.770 +      png_ptr->row_number, png_ptr->pass);
  90.771 +
  90.772 +   /* initialize transformations and other stuff if first time */
  90.773 +   if (png_ptr->row_number == 0 && png_ptr->pass == 0)
  90.774 +   {
  90.775 +   /* make sure we wrote the header info */
  90.776 +   if (!(png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE))
  90.777 +      png_error(png_ptr,
  90.778 +         "png_write_info was never called before png_write_row.");
  90.779 +
  90.780 +   /* check for transforms that have been set but were defined out */
  90.781 +#if !defined(PNG_WRITE_INVERT_SUPPORTED) && defined(PNG_READ_INVERT_SUPPORTED)
  90.782 +   if (png_ptr->transformations & PNG_INVERT_MONO)
  90.783 +      png_warning(png_ptr, "PNG_WRITE_INVERT_SUPPORTED is not defined.");
  90.784 +#endif
  90.785 +#if !defined(PNG_WRITE_FILLER_SUPPORTED) && defined(PNG_READ_FILLER_SUPPORTED)
  90.786 +   if (png_ptr->transformations & PNG_FILLER)
  90.787 +      png_warning(png_ptr, "PNG_WRITE_FILLER_SUPPORTED is not defined.");
  90.788 +#endif
  90.789 +#if !defined(PNG_WRITE_PACKSWAP_SUPPORTED) && defined(PNG_READ_PACKSWAP_SUPPORTED)
  90.790 +   if (png_ptr->transformations & PNG_PACKSWAP)
  90.791 +      png_warning(png_ptr, "PNG_WRITE_PACKSWAP_SUPPORTED is not defined.");
  90.792 +#endif
  90.793 +#if !defined(PNG_WRITE_PACK_SUPPORTED) && defined(PNG_READ_PACK_SUPPORTED)
  90.794 +   if (png_ptr->transformations & PNG_PACK)
  90.795 +      png_warning(png_ptr, "PNG_WRITE_PACK_SUPPORTED is not defined.");
  90.796 +#endif
  90.797 +#if !defined(PNG_WRITE_SHIFT_SUPPORTED) && defined(PNG_READ_SHIFT_SUPPORTED)
  90.798 +   if (png_ptr->transformations & PNG_SHIFT)
  90.799 +      png_warning(png_ptr, "PNG_WRITE_SHIFT_SUPPORTED is not defined.");
  90.800 +#endif
  90.801 +#if !defined(PNG_WRITE_BGR_SUPPORTED) && defined(PNG_READ_BGR_SUPPORTED)
  90.802 +   if (png_ptr->transformations & PNG_BGR)
  90.803 +      png_warning(png_ptr, "PNG_WRITE_BGR_SUPPORTED is not defined.");
  90.804 +#endif
  90.805 +#if !defined(PNG_WRITE_SWAP_SUPPORTED) && defined(PNG_READ_SWAP_SUPPORTED)
  90.806 +   if (png_ptr->transformations & PNG_SWAP_BYTES)
  90.807 +      png_warning(png_ptr, "PNG_WRITE_SWAP_SUPPORTED is not defined.");
  90.808 +#endif
  90.809 +
  90.810 +      png_write_start_row(png_ptr);
  90.811 +   }
  90.812 +
  90.813 +#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
  90.814 +   /* if interlaced and not interested in row, return */
  90.815 +   if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
  90.816 +   {
  90.817 +      switch (png_ptr->pass)
  90.818 +      {
  90.819 +         case 0:
  90.820 +            if (png_ptr->row_number & 0x07)
  90.821 +            {
  90.822 +               png_write_finish_row(png_ptr);
  90.823 +               return;
  90.824 +            }
  90.825 +            break;
  90.826 +         case 1:
  90.827 +            if ((png_ptr->row_number & 0x07) || png_ptr->width < 5)
  90.828 +            {
  90.829 +               png_write_finish_row(png_ptr);
  90.830 +               return;
  90.831 +            }
  90.832 +            break;
  90.833 +         case 2:
  90.834 +            if ((png_ptr->row_number & 0x07) != 4)
  90.835 +            {
  90.836 +               png_write_finish_row(png_ptr);
  90.837 +               return;
  90.838 +            }
  90.839 +            break;
  90.840 +         case 3:
  90.841 +            if ((png_ptr->row_number & 0x03) || png_ptr->width < 3)
  90.842 +            {
  90.843 +               png_write_finish_row(png_ptr);
  90.844 +               return;
  90.845 +            }
  90.846 +            break;
  90.847 +         case 4:
  90.848 +            if ((png_ptr->row_number & 0x03) != 2)
  90.849 +            {
  90.850 +               png_write_finish_row(png_ptr);
  90.851 +               return;
  90.852 +            }
  90.853 +            break;
  90.854 +         case 5:
  90.855 +            if ((png_ptr->row_number & 0x01) || png_ptr->width < 2)
  90.856 +            {
  90.857 +               png_write_finish_row(png_ptr);
  90.858 +               return;
  90.859 +            }
  90.860 +            break;
  90.861 +         case 6:
  90.862 +            if (!(png_ptr->row_number & 0x01))
  90.863 +            {
  90.864 +               png_write_finish_row(png_ptr);
  90.865 +               return;
  90.866 +            }
  90.867 +            break;
  90.868 +      }
  90.869 +   }
  90.870 +#endif
  90.871 +
  90.872 +   /* set up row info for transformations */
  90.873 +   png_ptr->row_info.color_type = png_ptr->color_type;
  90.874 +   png_ptr->row_info.width = png_ptr->usr_width;
  90.875 +   png_ptr->row_info.channels = png_ptr->usr_channels;
  90.876 +   png_ptr->row_info.bit_depth = png_ptr->usr_bit_depth;
  90.877 +   png_ptr->row_info.pixel_depth = (png_byte)(png_ptr->row_info.bit_depth *
  90.878 +      png_ptr->row_info.channels);
  90.879 +
  90.880 +   png_ptr->row_info.rowbytes = PNG_ROWBYTES(png_ptr->row_info.pixel_depth,
  90.881 +      png_ptr->row_info.width);
  90.882 +
  90.883 +   png_debug1(3, "row_info->color_type = %d\n", png_ptr->row_info.color_type);
  90.884 +   png_debug1(3, "row_info->width = %lu\n", png_ptr->row_info.width);
  90.885 +   png_debug1(3, "row_info->channels = %d\n", png_ptr->row_info.channels);
  90.886 +   png_debug1(3, "row_info->bit_depth = %d\n", png_ptr->row_info.bit_depth);
  90.887 +   png_debug1(3, "row_info->pixel_depth = %d\n", png_ptr->row_info.pixel_depth);
  90.888 +   png_debug1(3, "row_info->rowbytes = %lu\n", png_ptr->row_info.rowbytes);
  90.889 +
  90.890 +   /* Copy user's row into buffer, leaving room for filter byte. */
  90.891 +   png_memcpy_check(png_ptr, png_ptr->row_buf + 1, row,
  90.892 +      png_ptr->row_info.rowbytes);
  90.893 +
  90.894 +#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
  90.895 +   /* handle interlacing */
  90.896 +   if (png_ptr->interlaced && png_ptr->pass < 6 &&
  90.897 +      (png_ptr->transformations & PNG_INTERLACE))
  90.898 +   {
  90.899 +      png_do_write_interlace(&(png_ptr->row_info),
  90.900 +         png_ptr->row_buf + 1, png_ptr->pass);
  90.901 +      /* this should always get caught above, but still ... */
  90.902 +      if (!(png_ptr->row_info.width))
  90.903 +      {
  90.904 +         png_write_finish_row(png_ptr);
  90.905 +         return;
  90.906 +      }
  90.907 +   }
  90.908 +#endif
  90.909 +
  90.910 +   /* handle other transformations */
  90.911 +   if (png_ptr->transformations)
  90.912 +      png_do_write_transformations(png_ptr);
  90.913 +
  90.914 +#if defined(PNG_MNG_FEATURES_SUPPORTED)
  90.915 +   /* Write filter_method 64 (intrapixel differencing) only if
  90.916 +    * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
  90.917 +    * 2. Libpng did not write a PNG signature (this filter_method is only
  90.918 +    *    used in PNG datastreams that are embedded in MNG datastreams) and
  90.919 +    * 3. The application called png_permit_mng_features with a mask that
  90.920 +    *    included PNG_FLAG_MNG_FILTER_64 and
  90.921 +    * 4. The filter_method is 64 and
  90.922 +    * 5. The color_type is RGB or RGBA
  90.923 +    */
  90.924 +   if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
  90.925 +      (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING))
  90.926 +   {
  90.927 +      /* Intrapixel differencing */
  90.928 +      png_do_write_intrapixel(&(png_ptr->row_info), png_ptr->row_buf + 1);
  90.929 +   }
  90.930 +#endif
  90.931 +
  90.932 +   /* Find a filter if necessary, filter the row and write it out. */
  90.933 +   png_write_find_filter(png_ptr, &(png_ptr->row_info));
  90.934 +
  90.935 +   if (png_ptr->write_row_fn != NULL)
  90.936 +      (*(png_ptr->write_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
  90.937 +}
  90.938 +
  90.939 +#if defined(PNG_WRITE_FLUSH_SUPPORTED)
  90.940 +/* Set the automatic flush interval or 0 to turn flushing off */
  90.941 +void PNGAPI
  90.942 +png_set_flush(png_structp png_ptr, int nrows)
  90.943 +{
  90.944 +   png_debug(1, "in png_set_flush\n");
  90.945 +   if (png_ptr == NULL)
  90.946 +      return;
  90.947 +   png_ptr->flush_dist = (nrows < 0 ? 0 : nrows);
  90.948 +}
  90.949 +
  90.950 +/* flush the current output buffers now */
  90.951 +void PNGAPI
  90.952 +png_write_flush(png_structp png_ptr)
  90.953 +{
  90.954 +   int wrote_IDAT;
  90.955 +
  90.956 +   png_debug(1, "in png_write_flush\n");
  90.957 +   if (png_ptr == NULL)
  90.958 +      return;
  90.959 +   /* We have already written out all of the data */
  90.960 +   if (png_ptr->row_number >= png_ptr->num_rows)
  90.961 +     return;
  90.962 +
  90.963 +   do
  90.964 +   {
  90.965 +      int ret;
  90.966 +
  90.967 +      /* compress the data */
  90.968 +      ret = deflate(&png_ptr->zstream, Z_SYNC_FLUSH);
  90.969 +      wrote_IDAT = 0;
  90.970 +
  90.971 +      /* check for compression errors */
  90.972 +      if (ret != Z_OK)
  90.973 +      {
  90.974 +         if (png_ptr->zstream.msg != NULL)
  90.975 +            png_error(png_ptr, png_ptr->zstream.msg);
  90.976 +         else
  90.977 +            png_error(png_ptr, "zlib error");
  90.978 +      }
  90.979 +
  90.980 +      if (!(png_ptr->zstream.avail_out))
  90.981 +      {
  90.982 +         /* write the IDAT and reset the zlib output buffer */
  90.983 +         png_write_IDAT(png_ptr, png_ptr->zbuf,
  90.984 +                        png_ptr->zbuf_size);
  90.985 +         png_ptr->zstream.next_out = png_ptr->zbuf;
  90.986 +         png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
  90.987 +         wrote_IDAT = 1;
  90.988 +      }
  90.989 +   } while(wrote_IDAT == 1);
  90.990 +
  90.991 +   /* If there is any data left to be output, write it into a new IDAT */
  90.992 +   if (png_ptr->zbuf_size != png_ptr->zstream.avail_out)
  90.993 +   {
  90.994 +      /* write the IDAT and reset the zlib output buffer */
  90.995 +      png_write_IDAT(png_ptr, png_ptr->zbuf,
  90.996 +                     png_ptr->zbuf_size - png_ptr->zstream.avail_out);
  90.997 +      png_ptr->zstream.next_out = png_ptr->zbuf;
  90.998 +      png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
  90.999 +   }
 90.1000 +   png_ptr->flush_rows = 0;
 90.1001 +   png_flush(png_ptr);
 90.1002 +}
 90.1003 +#endif /* PNG_WRITE_FLUSH_SUPPORTED */
 90.1004 +
 90.1005 +/* free all memory used by the write */
 90.1006 +void PNGAPI
 90.1007 +png_destroy_write_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr)
 90.1008 +{
 90.1009 +   png_structp png_ptr = NULL;
 90.1010 +   png_infop info_ptr = NULL;
 90.1011 +#ifdef PNG_USER_MEM_SUPPORTED
 90.1012 +   png_free_ptr free_fn = NULL;
 90.1013 +   png_voidp mem_ptr = NULL;
 90.1014 +#endif
 90.1015 +
 90.1016 +   png_debug(1, "in png_destroy_write_struct\n");
 90.1017 +   if (png_ptr_ptr != NULL)
 90.1018 +   {
 90.1019 +      png_ptr = *png_ptr_ptr;
 90.1020 +#ifdef PNG_USER_MEM_SUPPORTED
 90.1021 +      free_fn = png_ptr->free_fn;
 90.1022 +      mem_ptr = png_ptr->mem_ptr;
 90.1023 +#endif
 90.1024 +   }
 90.1025 +
 90.1026 +#ifdef PNG_USER_MEM_SUPPORTED
 90.1027 +   if (png_ptr != NULL)
 90.1028 +   {
 90.1029 +      free_fn = png_ptr->free_fn;
 90.1030 +      mem_ptr = png_ptr->mem_ptr;
 90.1031 +   }
 90.1032 +#endif
 90.1033 +
 90.1034 +   if (info_ptr_ptr != NULL)
 90.1035 +      info_ptr = *info_ptr_ptr;
 90.1036 +
 90.1037 +   if (info_ptr != NULL)
 90.1038 +   {
 90.1039 +      if (png_ptr != NULL)
 90.1040 +      {
 90.1041 +        png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
 90.1042 +
 90.1043 +#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
 90.1044 +        if (png_ptr->num_chunk_list)
 90.1045 +        {
 90.1046 +           png_free(png_ptr, png_ptr->chunk_list);
 90.1047 +           png_ptr->chunk_list=NULL;
 90.1048 +           png_ptr->num_chunk_list = 0;
 90.1049 +        }
 90.1050 +#endif
 90.1051 +      }
 90.1052 +
 90.1053 +#ifdef PNG_USER_MEM_SUPPORTED
 90.1054 +      png_destroy_struct_2((png_voidp)info_ptr, (png_free_ptr)free_fn,
 90.1055 +         (png_voidp)mem_ptr);
 90.1056 +#else
 90.1057 +      png_destroy_struct((png_voidp)info_ptr);
 90.1058 +#endif
 90.1059 +      *info_ptr_ptr = NULL;
 90.1060 +   }
 90.1061 +
 90.1062 +   if (png_ptr != NULL)
 90.1063 +   {
 90.1064 +      png_write_destroy(png_ptr);
 90.1065 +#ifdef PNG_USER_MEM_SUPPORTED
 90.1066 +      png_destroy_struct_2((png_voidp)png_ptr, (png_free_ptr)free_fn,
 90.1067 +         (png_voidp)mem_ptr);
 90.1068 +#else
 90.1069 +      png_destroy_struct((png_voidp)png_ptr);
 90.1070 +#endif
 90.1071 +      *png_ptr_ptr = NULL;
 90.1072 +   }
 90.1073 +}
 90.1074 +
 90.1075 +
 90.1076 +/* Free any memory used in png_ptr struct (old method) */
 90.1077 +void /* PRIVATE */
 90.1078 +png_write_destroy(png_structp png_ptr)
 90.1079 +{
 90.1080 +#ifdef PNG_SETJMP_SUPPORTED
 90.1081 +   jmp_buf tmp_jmp; /* save jump buffer */
 90.1082 +#endif
 90.1083 +   png_error_ptr error_fn;
 90.1084 +   png_error_ptr warning_fn;
 90.1085 +   png_voidp error_ptr;
 90.1086 +#ifdef PNG_USER_MEM_SUPPORTED
 90.1087 +   png_free_ptr free_fn;
 90.1088 +#endif
 90.1089 +
 90.1090 +   png_debug(1, "in png_write_destroy\n");
 90.1091 +   /* free any memory zlib uses */
 90.1092 +   deflateEnd(&png_ptr->zstream);
 90.1093 +
 90.1094 +   /* free our memory.  png_free checks NULL for us. */
 90.1095 +   png_free(png_ptr, png_ptr->zbuf);
 90.1096 +   png_free(png_ptr, png_ptr->row_buf);
 90.1097 +#ifndef PNG_NO_WRITE_FILTER
 90.1098 +   png_free(png_ptr, png_ptr->prev_row);
 90.1099 +   png_free(png_ptr, png_ptr->sub_row);
 90.1100 +   png_free(png_ptr, png_ptr->up_row);
 90.1101 +   png_free(png_ptr, png_ptr->avg_row);
 90.1102 +   png_free(png_ptr, png_ptr->paeth_row);
 90.1103 +#endif
 90.1104 +
 90.1105 +#if defined(PNG_TIME_RFC1123_SUPPORTED)
 90.1106 +   png_free(png_ptr, png_ptr->time_buffer);
 90.1107 +#endif
 90.1108 +
 90.1109 +#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
 90.1110 +   png_free(png_ptr, png_ptr->prev_filters);
 90.1111 +   png_free(png_ptr, png_ptr->filter_weights);
 90.1112 +   png_free(png_ptr, png_ptr->inv_filter_weights);
 90.1113 +   png_free(png_ptr, png_ptr->filter_costs);
 90.1114 +   png_free(png_ptr, png_ptr->inv_filter_costs);
 90.1115 +#endif
 90.1116 +
 90.1117 +#ifdef PNG_SETJMP_SUPPORTED
 90.1118 +   /* reset structure */
 90.1119 +   png_memcpy(tmp_jmp, png_ptr->jmpbuf, png_sizeof(jmp_buf));
 90.1120 +#endif
 90.1121 +
 90.1122 +   error_fn = png_ptr->error_fn;
 90.1123 +   warning_fn = png_ptr->warning_fn;
 90.1124 +   error_ptr = png_ptr->error_ptr;
 90.1125 +#ifdef PNG_USER_MEM_SUPPORTED
 90.1126 +   free_fn = png_ptr->free_fn;
 90.1127 +#endif
 90.1128 +
 90.1129 +   png_memset(png_ptr, 0, png_sizeof(png_struct));
 90.1130 +
 90.1131 +   png_ptr->error_fn = error_fn;
 90.1132 +   png_ptr->warning_fn = warning_fn;
 90.1133 +   png_ptr->error_ptr = error_ptr;
 90.1134 +#ifdef PNG_USER_MEM_SUPPORTED
 90.1135 +   png_ptr->free_fn = free_fn;
 90.1136 +#endif
 90.1137 +
 90.1138 +#ifdef PNG_SETJMP_SUPPORTED
 90.1139 +   png_memcpy(png_ptr->jmpbuf, tmp_jmp, png_sizeof(jmp_buf));
 90.1140 +#endif
 90.1141 +}
 90.1142 +
 90.1143 +/* Allow the application to select one or more row filters to use. */
 90.1144 +void PNGAPI
 90.1145 +png_set_filter(png_structp png_ptr, int method, int filters)
 90.1146 +{
 90.1147 +   png_debug(1, "in png_set_filter\n");
 90.1148 +   if (png_ptr == NULL)
 90.1149 +      return;
 90.1150 +#if defined(PNG_MNG_FEATURES_SUPPORTED)
 90.1151 +   if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
 90.1152 +      (method == PNG_INTRAPIXEL_DIFFERENCING))
 90.1153 +         method = PNG_FILTER_TYPE_BASE;
 90.1154 +#endif
 90.1155 +   if (method == PNG_FILTER_TYPE_BASE)
 90.1156 +   {
 90.1157 +      switch (filters & (PNG_ALL_FILTERS | 0x07))
 90.1158 +      {
 90.1159 +#ifndef PNG_NO_WRITE_FILTER
 90.1160 +         case 5:
 90.1161 +         case 6:
 90.1162 +         case 7: png_warning(png_ptr, "Unknown row filter for method 0");
 90.1163 +#endif /* PNG_NO_WRITE_FILTER */
 90.1164 +         case PNG_FILTER_VALUE_NONE:
 90.1165 +              png_ptr->do_filter=PNG_FILTER_NONE; break;
 90.1166 +#ifndef PNG_NO_WRITE_FILTER
 90.1167 +         case PNG_FILTER_VALUE_SUB:
 90.1168 +              png_ptr->do_filter=PNG_FILTER_SUB; break;
 90.1169 +         case PNG_FILTER_VALUE_UP:
 90.1170 +              png_ptr->do_filter=PNG_FILTER_UP; break;
 90.1171 +         case PNG_FILTER_VALUE_AVG:
 90.1172 +              png_ptr->do_filter=PNG_FILTER_AVG; break;
 90.1173 +         case PNG_FILTER_VALUE_PAETH:
 90.1174 +              png_ptr->do_filter=PNG_FILTER_PAETH; break;
 90.1175 +         default: png_ptr->do_filter = (png_byte)filters; break;
 90.1176 +#else
 90.1177 +         default: png_warning(png_ptr, "Unknown row filter for method 0");
 90.1178 +#endif /* PNG_NO_WRITE_FILTER */
 90.1179 +      }
 90.1180 +
 90.1181 +      /* If we have allocated the row_buf, this means we have already started
 90.1182 +       * with the image and we should have allocated all of the filter buffers
 90.1183 +       * that have been selected.  If prev_row isn't already allocated, then
 90.1184 +       * it is too late to start using the filters that need it, since we
 90.1185 +       * will be missing the data in the previous row.  If an application
 90.1186 +       * wants to start and stop using particular filters during compression,
 90.1187 +       * it should start out with all of the filters, and then add and
 90.1188 +       * remove them after the start of compression.
 90.1189 +       */
 90.1190 +      if (png_ptr->row_buf != NULL)
 90.1191 +      {
 90.1192 +#ifndef PNG_NO_WRITE_FILTER
 90.1193 +         if ((png_ptr->do_filter & PNG_FILTER_SUB) && png_ptr->sub_row == NULL)
 90.1194 +         {
 90.1195 +            png_ptr->sub_row = (png_bytep)png_malloc(png_ptr,
 90.1196 +              (png_ptr->rowbytes + 1));
 90.1197 +            png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
 90.1198 +         }
 90.1199 +
 90.1200 +         if ((png_ptr->do_filter & PNG_FILTER_UP) && png_ptr->up_row == NULL)
 90.1201 +         {
 90.1202 +            if (png_ptr->prev_row == NULL)
 90.1203 +            {
 90.1204 +               png_warning(png_ptr, "Can't add Up filter after starting");
 90.1205 +               png_ptr->do_filter &= ~PNG_FILTER_UP;
 90.1206 +            }
 90.1207 +            else
 90.1208 +            {
 90.1209 +               png_ptr->up_row = (png_bytep)png_malloc(png_ptr,
 90.1210 +                  (png_ptr->rowbytes + 1));
 90.1211 +               png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
 90.1212 +            }
 90.1213 +         }
 90.1214 +
 90.1215 +         if ((png_ptr->do_filter & PNG_FILTER_AVG) && png_ptr->avg_row == NULL)
 90.1216 +         {
 90.1217 +            if (png_ptr->prev_row == NULL)
 90.1218 +            {
 90.1219 +               png_warning(png_ptr, "Can't add Average filter after starting");
 90.1220 +               png_ptr->do_filter &= ~PNG_FILTER_AVG;
 90.1221 +            }
 90.1222 +            else
 90.1223 +            {
 90.1224 +               png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
 90.1225 +                  (png_ptr->rowbytes + 1));
 90.1226 +               png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
 90.1227 +            }
 90.1228 +         }
 90.1229 +
 90.1230 +         if ((png_ptr->do_filter & PNG_FILTER_PAETH) &&
 90.1231 +             png_ptr->paeth_row == NULL)
 90.1232 +         {
 90.1233 +            if (png_ptr->prev_row == NULL)
 90.1234 +            {
 90.1235 +               png_warning(png_ptr, "Can't add Paeth filter after starting");
 90.1236 +               png_ptr->do_filter &= (png_byte)(~PNG_FILTER_PAETH);
 90.1237 +            }
 90.1238 +            else
 90.1239 +            {
 90.1240 +               png_ptr->paeth_row = (png_bytep)png_malloc(png_ptr,
 90.1241 +                  (png_ptr->rowbytes + 1));
 90.1242 +               png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
 90.1243 +            }
 90.1244 +         }
 90.1245 +
 90.1246 +         if (png_ptr->do_filter == PNG_NO_FILTERS)
 90.1247 +#endif /* PNG_NO_WRITE_FILTER */
 90.1248 +            png_ptr->do_filter = PNG_FILTER_NONE;
 90.1249 +      }
 90.1250 +   }
 90.1251 +   else
 90.1252 +      png_error(png_ptr, "Unknown custom filter method");
 90.1253 +}
 90.1254 +
 90.1255 +/* This allows us to influence the way in which libpng chooses the "best"
 90.1256 + * filter for the current scanline.  While the "minimum-sum-of-absolute-
 90.1257 + * differences metric is relatively fast and effective, there is some
 90.1258 + * question as to whether it can be improved upon by trying to keep the
 90.1259 + * filtered data going to zlib more consistent, hopefully resulting in
 90.1260 + * better compression.
 90.1261 + */
 90.1262 +#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)      /* GRR 970116 */
 90.1263 +void PNGAPI
 90.1264 +png_set_filter_heuristics(png_structp png_ptr, int heuristic_method,
 90.1265 +   int num_weights, png_doublep filter_weights,
 90.1266 +   png_doublep filter_costs)
 90.1267 +{
 90.1268 +   int i;
 90.1269 +
 90.1270 +   png_debug(1, "in png_set_filter_heuristics\n");
 90.1271 +   if (png_ptr == NULL)
 90.1272 +      return;
 90.1273 +   if (heuristic_method >= PNG_FILTER_HEURISTIC_LAST)
 90.1274 +   {
 90.1275 +      png_warning(png_ptr, "Unknown filter heuristic method");
 90.1276 +      return;
 90.1277 +   }
 90.1278 +
 90.1279 +   if (heuristic_method == PNG_FILTER_HEURISTIC_DEFAULT)
 90.1280 +   {
 90.1281 +      heuristic_method = PNG_FILTER_HEURISTIC_UNWEIGHTED;
 90.1282 +   }
 90.1283 +
 90.1284 +   if (num_weights < 0 || filter_weights == NULL ||
 90.1285 +      heuristic_method == PNG_FILTER_HEURISTIC_UNWEIGHTED)
 90.1286 +   {
 90.1287 +      num_weights = 0;
 90.1288 +   }
 90.1289 +
 90.1290 +   png_ptr->num_prev_filters = (png_byte)num_weights;
 90.1291 +   png_ptr->heuristic_method = (png_byte)heuristic_method;
 90.1292 +
 90.1293 +   if (num_weights > 0)
 90.1294 +   {
 90.1295 +      if (png_ptr->prev_filters == NULL)
 90.1296 +      {
 90.1297 +         png_ptr->prev_filters = (png_bytep)png_malloc(png_ptr,
 90.1298 +            (png_uint_32)(png_sizeof(png_byte) * num_weights));
 90.1299 +
 90.1300 +         /* To make sure that the weighting starts out fairly */
 90.1301 +         for (i = 0; i < num_weights; i++)
 90.1302 +         {
 90.1303 +            png_ptr->prev_filters[i] = 255;
 90.1304 +         }
 90.1305 +      }
 90.1306 +
 90.1307 +      if (png_ptr->filter_weights == NULL)
 90.1308 +      {
 90.1309 +         png_ptr->filter_weights = (png_uint_16p)png_malloc(png_ptr,
 90.1310 +            (png_uint_32)(png_sizeof(png_uint_16) * num_weights));
 90.1311 +
 90.1312 +         png_ptr->inv_filter_weights = (png_uint_16p)png_malloc(png_ptr,
 90.1313 +            (png_uint_32)(png_sizeof(png_uint_16) * num_weights));
 90.1314 +         for (i = 0; i < num_weights; i++)
 90.1315 +         {
 90.1316 +            png_ptr->inv_filter_weights[i] =
 90.1317 +            png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
 90.1318 +         }
 90.1319 +      }
 90.1320 +
 90.1321 +      for (i = 0; i < num_weights; i++)
 90.1322 +      {
 90.1323 +         if (filter_weights[i] < 0.0)
 90.1324 +         {
 90.1325 +            png_ptr->inv_filter_weights[i] =
 90.1326 +            png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
 90.1327 +         }
 90.1328 +         else
 90.1329 +         {
 90.1330 +            png_ptr->inv_filter_weights[i] =
 90.1331 +               (png_uint_16)((double)PNG_WEIGHT_FACTOR*filter_weights[i]+0.5);
 90.1332 +            png_ptr->filter_weights[i] =
 90.1333 +               (png_uint_16)((double)PNG_WEIGHT_FACTOR/filter_weights[i]+0.5);
 90.1334 +         }
 90.1335 +      }
 90.1336 +   }
 90.1337 +
 90.1338 +   /* If, in the future, there are other filter methods, this would
 90.1339 +    * need to be based on png_ptr->filter.
 90.1340 +    */
 90.1341 +   if (png_ptr->filter_costs == NULL)
 90.1342 +   {
 90.1343 +      png_ptr->filter_costs = (png_uint_16p)png_malloc(png_ptr,
 90.1344 +         (png_uint_32)(png_sizeof(png_uint_16) * PNG_FILTER_VALUE_LAST));
 90.1345 +
 90.1346 +      png_ptr->inv_filter_costs = (png_uint_16p)png_malloc(png_ptr,
 90.1347 +         (png_uint_32)(png_sizeof(png_uint_16) * PNG_FILTER_VALUE_LAST));
 90.1348 +
 90.1349 +      for (i = 0; i < PNG_FILTER_VALUE_LAST; i++)
 90.1350 +      {
 90.1351 +         png_ptr->inv_filter_costs[i] =
 90.1352 +         png_ptr->filter_costs[i] = PNG_COST_FACTOR;
 90.1353 +      }
 90.1354 +   }
 90.1355 +
 90.1356 +   /* Here is where we set the relative costs of the different filters.  We
 90.1357 +    * should take the desired compression level into account when setting
 90.1358 +    * the costs, so that Paeth, for instance, has a high relative cost at low
 90.1359 +    * compression levels, while it has a lower relative cost at higher
 90.1360 +    * compression settings.  The filter types are in order of increasing
 90.1361 +    * relative cost, so it would be possible to do this with an algorithm.
 90.1362 +    */
 90.1363 +   for (i = 0; i < PNG_FILTER_VALUE_LAST; i++)
 90.1364 +   {
 90.1365 +      if (filter_costs == NULL || filter_costs[i] < 0.0)
 90.1366 +      {
 90.1367 +         png_ptr->inv_filter_costs[i] =
 90.1368 +         png_ptr->filter_costs[i] = PNG_COST_FACTOR;
 90.1369 +      }
 90.1370 +      else if (filter_costs[i] >= 1.0)
 90.1371 +      {
 90.1372 +         png_ptr->inv_filter_costs[i] =
 90.1373 +            (png_uint_16)((double)PNG_COST_FACTOR / filter_costs[i] + 0.5);
 90.1374 +         png_ptr->filter_costs[i] =
 90.1375 +            (png_uint_16)((double)PNG_COST_FACTOR * filter_costs[i] + 0.5);
 90.1376 +      }
 90.1377 +   }
 90.1378 +}
 90.1379 +#endif /* PNG_WRITE_WEIGHTED_FILTER_SUPPORTED */
 90.1380 +
 90.1381 +void PNGAPI
 90.1382 +png_set_compression_level(png_structp png_ptr, int level)
 90.1383 +{
 90.1384 +   png_debug(1, "in png_set_compression_level\n");
 90.1385 +   if (png_ptr == NULL)
 90.1386 +      return;
 90.1387 +   png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_LEVEL;
 90.1388 +   png_ptr->zlib_level = level;
 90.1389 +}
 90.1390 +
 90.1391 +void PNGAPI
 90.1392 +png_set_compression_mem_level(png_structp png_ptr, int mem_level)
 90.1393 +{
 90.1394 +   png_debug(1, "in png_set_compression_mem_level\n");
 90.1395 +   if (png_ptr == NULL)
 90.1396 +      return;
 90.1397 +   png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL;
 90.1398 +   png_ptr->zlib_mem_level = mem_level;
 90.1399 +}
 90.1400 +
 90.1401 +void PNGAPI
 90.1402 +png_set_compression_strategy(png_structp png_ptr, int strategy)
 90.1403 +{
 90.1404 +   png_debug(1, "in png_set_compression_strategy\n");
 90.1405 +   if (png_ptr == NULL)
 90.1406 +      return;
 90.1407 +   png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_STRATEGY;
 90.1408 +   png_ptr->zlib_strategy = strategy;
 90.1409 +}
 90.1410 +
 90.1411 +void PNGAPI
 90.1412 +png_set_compression_window_bits(png_structp png_ptr, int window_bits)
 90.1413 +{
 90.1414 +   if (png_ptr == NULL)
 90.1415 +      return;
 90.1416 +   if (window_bits > 15)
 90.1417 +      png_warning(png_ptr, "Only compression windows <= 32k supported by PNG");
 90.1418 +   else if (window_bits < 8)
 90.1419 +      png_warning(png_ptr, "Only compression windows >= 256 supported by PNG");
 90.1420 +#ifndef WBITS_8_OK
 90.1421 +   /* avoid libpng bug with 256-byte windows */
 90.1422 +   if (window_bits == 8)
 90.1423 +     {
 90.1424 +       png_warning(png_ptr, "Compression window is being reset to 512");
 90.1425 +       window_bits=9;
 90.1426 +     }
 90.1427 +#endif
 90.1428 +   png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS;
 90.1429 +   png_ptr->zlib_window_bits = window_bits;
 90.1430 +}
 90.1431 +
 90.1432 +void PNGAPI
 90.1433 +png_set_compression_method(png_structp png_ptr, int method)
 90.1434 +{
 90.1435 +   png_debug(1, "in png_set_compression_method\n");
 90.1436 +   if (png_ptr == NULL)
 90.1437 +      return;
 90.1438 +   if (method != 8)
 90.1439 +      png_warning(png_ptr, "Only compression method 8 is supported by PNG");
 90.1440 +   png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_METHOD;
 90.1441 +   png_ptr->zlib_method = method;
 90.1442 +}
 90.1443 +
 90.1444 +void PNGAPI
 90.1445 +png_set_write_status_fn(png_structp png_ptr, png_write_status_ptr write_row_fn)
 90.1446 +{
 90.1447 +   if (png_ptr == NULL)
 90.1448 +      return;
 90.1449 +   png_ptr->write_row_fn = write_row_fn;
 90.1450 +}
 90.1451 +
 90.1452 +#if defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
 90.1453 +void PNGAPI
 90.1454 +png_set_write_user_transform_fn(png_structp png_ptr, png_user_transform_ptr
 90.1455 +   write_user_transform_fn)
 90.1456 +{
 90.1457 +   png_debug(1, "in png_set_write_user_transform_fn\n");
 90.1458 +   if (png_ptr == NULL)
 90.1459 +      return;
 90.1460 +   png_ptr->transformations |= PNG_USER_TRANSFORM;
 90.1461 +   png_ptr->write_user_transform_fn = write_user_transform_fn;
 90.1462 +}
 90.1463 +#endif
 90.1464 +
 90.1465 +
 90.1466 +#if defined(PNG_INFO_IMAGE_SUPPORTED)
 90.1467 +void PNGAPI
 90.1468 +png_write_png(png_structp png_ptr, png_infop info_ptr,
 90.1469 +              int transforms, voidp params)
 90.1470 +{
 90.1471 +   if (png_ptr == NULL || info_ptr == NULL)
 90.1472 +      return;
 90.1473 +#if defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED)
 90.1474 +   /* invert the alpha channel from opacity to transparency */
 90.1475 +   if (transforms & PNG_TRANSFORM_INVERT_ALPHA)
 90.1476 +       png_set_invert_alpha(png_ptr);
 90.1477 +#endif
 90.1478 +
 90.1479 +   /* Write the file header information. */
 90.1480 +   png_write_info(png_ptr, info_ptr);
 90.1481 +
 90.1482 +   /* ------ these transformations don't touch the info structure ------- */
 90.1483 +
 90.1484 +#if defined(PNG_WRITE_INVERT_SUPPORTED)
 90.1485 +   /* invert monochrome pixels */
 90.1486 +   if (transforms & PNG_TRANSFORM_INVERT_MONO)
 90.1487 +       png_set_invert_mono(png_ptr);
 90.1488 +#endif
 90.1489 +
 90.1490 +#if defined(PNG_WRITE_SHIFT_SUPPORTED)
 90.1491 +   /* Shift the pixels up to a legal bit depth and fill in
 90.1492 +    * as appropriate to correctly scale the image.
 90.1493 +    */
 90.1494 +   if ((transforms & PNG_TRANSFORM_SHIFT)
 90.1495 +               && (info_ptr->valid & PNG_INFO_sBIT))
 90.1496 +       png_set_shift(png_ptr, &info_ptr->sig_bit);
 90.1497 +#endif
 90.1498 +
 90.1499 +#if defined(PNG_WRITE_PACK_SUPPORTED)
 90.1500 +   /* pack pixels into bytes */
 90.1501 +   if (transforms & PNG_TRANSFORM_PACKING)
 90.1502 +       png_set_packing(png_ptr);
 90.1503 +#endif
 90.1504 +
 90.1505 +#if defined(PNG_WRITE_SWAP_ALPHA_SUPPORTED)
 90.1506 +   /* swap location of alpha bytes from ARGB to RGBA */
 90.1507 +   if (transforms & PNG_TRANSFORM_SWAP_ALPHA)
 90.1508 +       png_set_swap_alpha(png_ptr);
 90.1509 +#endif
 90.1510 +
 90.1511 +#if defined(PNG_WRITE_FILLER_SUPPORTED)
 90.1512 +   /* Get rid of filler (OR ALPHA) bytes, pack XRGB/RGBX/ARGB/RGBA into
 90.1513 +    * RGB (4 channels -> 3 channels). The second parameter is not used.
 90.1514 +    */
 90.1515 +   if (transforms & PNG_TRANSFORM_STRIP_FILLER)
 90.1516 +       png_set_filler(png_ptr, 0, PNG_FILLER_BEFORE);
 90.1517 +#endif
 90.1518 +
 90.1519 +#if defined(PNG_WRITE_BGR_SUPPORTED)
 90.1520 +   /* flip BGR pixels to RGB */
 90.1521 +   if (transforms & PNG_TRANSFORM_BGR)
 90.1522 +       png_set_bgr(png_ptr);
 90.1523 +#endif
 90.1524 +
 90.1525 +#if defined(PNG_WRITE_SWAP_SUPPORTED)
 90.1526 +   /* swap bytes of 16-bit files to most significant byte first */
 90.1527 +   if (transforms & PNG_TRANSFORM_SWAP_ENDIAN)
 90.1528 +       png_set_swap(png_ptr);
 90.1529 +#endif
 90.1530 +
 90.1531 +#if defined(PNG_WRITE_PACKSWAP_SUPPORTED)
 90.1532 +   /* swap bits of 1, 2, 4 bit packed pixel formats */
 90.1533 +   if (transforms & PNG_TRANSFORM_PACKSWAP)
 90.1534 +       png_set_packswap(png_ptr);
 90.1535 +#endif
 90.1536 +
 90.1537 +   /* ----------------------- end of transformations ------------------- */
 90.1538 +
 90.1539 +   /* write the bits */
 90.1540 +   if (info_ptr->valid & PNG_INFO_IDAT)
 90.1541 +       png_write_image(png_ptr, info_ptr->row_pointers);
 90.1542 +
 90.1543 +   /* It is REQUIRED to call this to finish writing the rest of the file */
 90.1544 +   png_write_end(png_ptr, info_ptr);
 90.1545 +
 90.1546 +   transforms = transforms; /* quiet compiler warnings */
 90.1547 +   params = params;
 90.1548 +}
 90.1549 +#endif
 90.1550 +#endif /* PNG_WRITE_SUPPORTED */
    91.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    91.2 +++ b/libs/libpng/pngwtran.c	Sat Sep 19 05:51:51 2015 +0300
    91.3 @@ -0,0 +1,572 @@
    91.4 +
    91.5 +/* pngwtran.c - transforms the data in a row for PNG writers
    91.6 + *
    91.7 + * Last changed in libpng 1.2.9 April 14, 2006
    91.8 + * For conditions of distribution and use, see copyright notice in png.h
    91.9 + * Copyright (c) 1998-2006 Glenn Randers-Pehrson
   91.10 + * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
   91.11 + * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
   91.12 + */
   91.13 +
   91.14 +#define PNG_INTERNAL
   91.15 +#include "png.h"
   91.16 +#ifdef PNG_WRITE_SUPPORTED
   91.17 +
   91.18 +/* Transform the data according to the user's wishes.  The order of
   91.19 + * transformations is significant.
   91.20 + */
   91.21 +void /* PRIVATE */
   91.22 +png_do_write_transformations(png_structp png_ptr)
   91.23 +{
   91.24 +   png_debug(1, "in png_do_write_transformations\n");
   91.25 +
   91.26 +   if (png_ptr == NULL)
   91.27 +      return;
   91.28 +
   91.29 +#if defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
   91.30 +   if (png_ptr->transformations & PNG_USER_TRANSFORM)
   91.31 +      if (png_ptr->write_user_transform_fn != NULL)
   91.32 +        (*(png_ptr->write_user_transform_fn)) /* user write transform function */
   91.33 +          (png_ptr,                    /* png_ptr */
   91.34 +           &(png_ptr->row_info),       /* row_info:     */
   91.35 +             /*  png_uint_32 width;          width of row */
   91.36 +             /*  png_uint_32 rowbytes;       number of bytes in row */
   91.37 +             /*  png_byte color_type;        color type of pixels */
   91.38 +             /*  png_byte bit_depth;         bit depth of samples */
   91.39 +             /*  png_byte channels;          number of channels (1-4) */
   91.40 +             /*  png_byte pixel_depth;       bits per pixel (depth*channels) */
   91.41 +           png_ptr->row_buf + 1);      /* start of pixel data for row */
   91.42 +#endif
   91.43 +#if defined(PNG_WRITE_FILLER_SUPPORTED)
   91.44 +   if (png_ptr->transformations & PNG_FILLER)
   91.45 +      png_do_strip_filler(&(png_ptr->row_info), png_ptr->row_buf + 1,
   91.46 +         png_ptr->flags);
   91.47 +#endif
   91.48 +#if defined(PNG_WRITE_PACKSWAP_SUPPORTED)
   91.49 +   if (png_ptr->transformations & PNG_PACKSWAP)
   91.50 +      png_do_packswap(&(png_ptr->row_info), png_ptr->row_buf + 1);
   91.51 +#endif
   91.52 +#if defined(PNG_WRITE_PACK_SUPPORTED)
   91.53 +   if (png_ptr->transformations & PNG_PACK)
   91.54 +      png_do_pack(&(png_ptr->row_info), png_ptr->row_buf + 1,
   91.55 +         (png_uint_32)png_ptr->bit_depth);
   91.56 +#endif
   91.57 +#if defined(PNG_WRITE_SWAP_SUPPORTED)
   91.58 +   if (png_ptr->transformations & PNG_SWAP_BYTES)
   91.59 +      png_do_swap(&(png_ptr->row_info), png_ptr->row_buf + 1);
   91.60 +#endif
   91.61 +#if defined(PNG_WRITE_SHIFT_SUPPORTED)
   91.62 +   if (png_ptr->transformations & PNG_SHIFT)
   91.63 +      png_do_shift(&(png_ptr->row_info), png_ptr->row_buf + 1,
   91.64 +         &(png_ptr->shift));
   91.65 +#endif
   91.66 +#if defined(PNG_WRITE_SWAP_ALPHA_SUPPORTED)
   91.67 +   if (png_ptr->transformations & PNG_SWAP_ALPHA)
   91.68 +      png_do_write_swap_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1);
   91.69 +#endif
   91.70 +#if defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED)
   91.71 +   if (png_ptr->transformations & PNG_INVERT_ALPHA)
   91.72 +      png_do_write_invert_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1);
   91.73 +#endif
   91.74 +#if defined(PNG_WRITE_BGR_SUPPORTED)
   91.75 +   if (png_ptr->transformations & PNG_BGR)
   91.76 +      png_do_bgr(&(png_ptr->row_info), png_ptr->row_buf + 1);
   91.77 +#endif
   91.78 +#if defined(PNG_WRITE_INVERT_SUPPORTED)
   91.79 +   if (png_ptr->transformations & PNG_INVERT_MONO)
   91.80 +      png_do_invert(&(png_ptr->row_info), png_ptr->row_buf + 1);
   91.81 +#endif
   91.82 +}
   91.83 +
   91.84 +#if defined(PNG_WRITE_PACK_SUPPORTED)
   91.85 +/* Pack pixels into bytes.  Pass the true bit depth in bit_depth.  The
   91.86 + * row_info bit depth should be 8 (one pixel per byte).  The channels
   91.87 + * should be 1 (this only happens on grayscale and paletted images).
   91.88 + */
   91.89 +void /* PRIVATE */
   91.90 +png_do_pack(png_row_infop row_info, png_bytep row, png_uint_32 bit_depth)
   91.91 +{
   91.92 +   png_debug(1, "in png_do_pack\n");
   91.93 +   if (row_info->bit_depth == 8 &&
   91.94 +#if defined(PNG_USELESS_TESTS_SUPPORTED)
   91.95 +       row != NULL && row_info != NULL &&
   91.96 +#endif
   91.97 +      row_info->channels == 1)
   91.98 +   {
   91.99 +      switch ((int)bit_depth)
  91.100 +      {
  91.101 +         case 1:
  91.102 +         {
  91.103 +            png_bytep sp, dp;
  91.104 +            int mask, v;
  91.105 +            png_uint_32 i;
  91.106 +            png_uint_32 row_width = row_info->width;
  91.107 +
  91.108 +            sp = row;
  91.109 +            dp = row;
  91.110 +            mask = 0x80;
  91.111 +            v = 0;
  91.112 +
  91.113 +            for (i = 0; i < row_width; i++)
  91.114 +            {
  91.115 +               if (*sp != 0)
  91.116 +                  v |= mask;
  91.117 +               sp++;
  91.118 +               if (mask > 1)
  91.119 +                  mask >>= 1;
  91.120 +               else
  91.121 +               {
  91.122 +                  mask = 0x80;
  91.123 +                  *dp = (png_byte)v;
  91.124 +                  dp++;
  91.125 +                  v = 0;
  91.126 +               }
  91.127 +            }
  91.128 +            if (mask != 0x80)
  91.129 +               *dp = (png_byte)v;
  91.130 +            break;
  91.131 +         }
  91.132 +         case 2:
  91.133 +         {
  91.134 +            png_bytep sp, dp;
  91.135 +            int shift, v;
  91.136 +            png_uint_32 i;
  91.137 +            png_uint_32 row_width = row_info->width;
  91.138 +
  91.139 +            sp = row;
  91.140 +            dp = row;
  91.141 +            shift = 6;
  91.142 +            v = 0;
  91.143 +            for (i = 0; i < row_width; i++)
  91.144 +            {
  91.145 +               png_byte value;
  91.146 +
  91.147 +               value = (png_byte)(*sp & 0x03);
  91.148 +               v |= (value << shift);
  91.149 +               if (shift == 0)
  91.150 +               {
  91.151 +                  shift = 6;
  91.152 +                  *dp = (png_byte)v;
  91.153 +                  dp++;
  91.154 +                  v = 0;
  91.155 +               }
  91.156 +               else
  91.157 +                  shift -= 2;
  91.158 +               sp++;
  91.159 +            }
  91.160 +            if (shift != 6)
  91.161 +               *dp = (png_byte)v;
  91.162 +            break;
  91.163 +         }
  91.164 +         case 4:
  91.165 +         {
  91.166 +            png_bytep sp, dp;
  91.167 +            int shift, v;
  91.168 +            png_uint_32 i;
  91.169 +            png_uint_32 row_width = row_info->width;
  91.170 +
  91.171 +            sp = row;
  91.172 +            dp = row;
  91.173 +            shift = 4;
  91.174 +            v = 0;
  91.175 +            for (i = 0; i < row_width; i++)
  91.176 +            {
  91.177 +               png_byte value;
  91.178 +
  91.179 +               value = (png_byte)(*sp & 0x0f);
  91.180 +               v |= (value << shift);
  91.181 +
  91.182 +               if (shift == 0)
  91.183 +               {
  91.184 +                  shift = 4;
  91.185 +                  *dp = (png_byte)v;
  91.186 +                  dp++;
  91.187 +                  v = 0;
  91.188 +               }
  91.189 +               else
  91.190 +                  shift -= 4;
  91.191 +
  91.192 +               sp++;
  91.193 +            }
  91.194 +            if (shift != 4)
  91.195 +               *dp = (png_byte)v;
  91.196 +            break;
  91.197 +         }
  91.198 +      }
  91.199 +      row_info->bit_depth = (png_byte)bit_depth;
  91.200 +      row_info->pixel_depth = (png_byte)(bit_depth * row_info->channels);
  91.201 +      row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,
  91.202 +         row_info->width);
  91.203 +   }
  91.204 +}
  91.205 +#endif
  91.206 +
  91.207 +#if defined(PNG_WRITE_SHIFT_SUPPORTED)
  91.208 +/* Shift pixel values to take advantage of whole range.  Pass the
  91.209 + * true number of bits in bit_depth.  The row should be packed
  91.210 + * according to row_info->bit_depth.  Thus, if you had a row of
  91.211 + * bit depth 4, but the pixels only had values from 0 to 7, you
  91.212 + * would pass 3 as bit_depth, and this routine would translate the
  91.213 + * data to 0 to 15.
  91.214 + */
  91.215 +void /* PRIVATE */
  91.216 +png_do_shift(png_row_infop row_info, png_bytep row, png_color_8p bit_depth)
  91.217 +{
  91.218 +   png_debug(1, "in png_do_shift\n");
  91.219 +#if defined(PNG_USELESS_TESTS_SUPPORTED)
  91.220 +   if (row != NULL && row_info != NULL &&
  91.221 +#else
  91.222 +   if (
  91.223 +#endif
  91.224 +      row_info->color_type != PNG_COLOR_TYPE_PALETTE)
  91.225 +   {
  91.226 +      int shift_start[4], shift_dec[4];
  91.227 +      int channels = 0;
  91.228 +
  91.229 +      if (row_info->color_type & PNG_COLOR_MASK_COLOR)
  91.230 +      {
  91.231 +         shift_start[channels] = row_info->bit_depth - bit_depth->red;
  91.232 +         shift_dec[channels] = bit_depth->red;
  91.233 +         channels++;
  91.234 +         shift_start[channels] = row_info->bit_depth - bit_depth->green;
  91.235 +         shift_dec[channels] = bit_depth->green;
  91.236 +         channels++;
  91.237 +         shift_start[channels] = row_info->bit_depth - bit_depth->blue;
  91.238 +         shift_dec[channels] = bit_depth->blue;
  91.239 +         channels++;
  91.240 +      }
  91.241 +      else
  91.242 +      {
  91.243 +         shift_start[channels] = row_info->bit_depth - bit_depth->gray;
  91.244 +         shift_dec[channels] = bit_depth->gray;
  91.245 +         channels++;
  91.246 +      }
  91.247 +      if (row_info->color_type & PNG_COLOR_MASK_ALPHA)
  91.248 +      {
  91.249 +         shift_start[channels] = row_info->bit_depth - bit_depth->alpha;
  91.250 +         shift_dec[channels] = bit_depth->alpha;
  91.251 +         channels++;
  91.252 +      }
  91.253 +
  91.254 +      /* with low row depths, could only be grayscale, so one channel */
  91.255 +      if (row_info->bit_depth < 8)
  91.256 +      {
  91.257 +         png_bytep bp = row;
  91.258 +         png_uint_32 i;
  91.259 +         png_byte mask;
  91.260 +         png_uint_32 row_bytes = row_info->rowbytes;
  91.261 +
  91.262 +         if (bit_depth->gray == 1 && row_info->bit_depth == 2)
  91.263 +            mask = 0x55;
  91.264 +         else if (row_info->bit_depth == 4 && bit_depth->gray == 3)
  91.265 +            mask = 0x11;
  91.266 +         else
  91.267 +            mask = 0xff;
  91.268 +
  91.269 +         for (i = 0; i < row_bytes; i++, bp++)
  91.270 +         {
  91.271 +            png_uint_16 v;
  91.272 +            int j;
  91.273 +
  91.274 +            v = *bp;
  91.275 +            *bp = 0;
  91.276 +            for (j = shift_start[0]; j > -shift_dec[0]; j -= shift_dec[0])
  91.277 +            {
  91.278 +               if (j > 0)
  91.279 +                  *bp |= (png_byte)((v << j) & 0xff);
  91.280 +               else
  91.281 +                  *bp |= (png_byte)((v >> (-j)) & mask);
  91.282 +            }
  91.283 +         }
  91.284 +      }
  91.285 +      else if (row_info->bit_depth == 8)
  91.286 +      {
  91.287 +         png_bytep bp = row;
  91.288 +         png_uint_32 i;
  91.289 +         png_uint_32 istop = channels * row_info->width;
  91.290 +
  91.291 +         for (i = 0; i < istop; i++, bp++)
  91.292 +         {
  91.293 +
  91.294 +            png_uint_16 v;
  91.295 +            int j;
  91.296 +            int c = (int)(i%channels);
  91.297 +
  91.298 +            v = *bp;
  91.299 +            *bp = 0;
  91.300 +            for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c])
  91.301 +            {
  91.302 +               if (j > 0)
  91.303 +                  *bp |= (png_byte)((v << j) & 0xff);
  91.304 +               else
  91.305 +                  *bp |= (png_byte)((v >> (-j)) & 0xff);
  91.306 +            }
  91.307 +         }
  91.308 +      }
  91.309 +      else
  91.310 +      {
  91.311 +         png_bytep bp;
  91.312 +         png_uint_32 i;
  91.313 +         png_uint_32 istop = channels * row_info->width;
  91.314 +
  91.315 +         for (bp = row, i = 0; i < istop; i++)
  91.316 +         {
  91.317 +            int c = (int)(i%channels);
  91.318 +            png_uint_16 value, v;
  91.319 +            int j;
  91.320 +
  91.321 +            v = (png_uint_16)(((png_uint_16)(*bp) << 8) + *(bp + 1));
  91.322 +            value = 0;
  91.323 +            for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c])
  91.324 +            {
  91.325 +               if (j > 0)
  91.326 +                  value |= (png_uint_16)((v << j) & (png_uint_16)0xffff);
  91.327 +               else
  91.328 +                  value |= (png_uint_16)((v >> (-j)) & (png_uint_16)0xffff);
  91.329 +            }
  91.330 +            *bp++ = (png_byte)(value >> 8);
  91.331 +            *bp++ = (png_byte)(value & 0xff);
  91.332 +         }
  91.333 +      }
  91.334 +   }
  91.335 +}
  91.336 +#endif
  91.337 +
  91.338 +#if defined(PNG_WRITE_SWAP_ALPHA_SUPPORTED)
  91.339 +void /* PRIVATE */
  91.340 +png_do_write_swap_alpha(png_row_infop row_info, png_bytep row)
  91.341 +{
  91.342 +   png_debug(1, "in png_do_write_swap_alpha\n");
  91.343 +#if defined(PNG_USELESS_TESTS_SUPPORTED)
  91.344 +   if (row != NULL && row_info != NULL)
  91.345 +#endif
  91.346 +   {
  91.347 +      if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
  91.348 +      {
  91.349 +         /* This converts from ARGB to RGBA */
  91.350 +         if (row_info->bit_depth == 8)
  91.351 +         {
  91.352 +            png_bytep sp, dp;
  91.353 +            png_uint_32 i;
  91.354 +            png_uint_32 row_width = row_info->width;
  91.355 +            for (i = 0, sp = dp = row; i < row_width; i++)
  91.356 +            {
  91.357 +               png_byte save = *(sp++);
  91.358 +               *(dp++) = *(sp++);
  91.359 +               *(dp++) = *(sp++);
  91.360 +               *(dp++) = *(sp++);
  91.361 +               *(dp++) = save;
  91.362 +            }
  91.363 +         }
  91.364 +         /* This converts from AARRGGBB to RRGGBBAA */
  91.365 +         else
  91.366 +         {
  91.367 +            png_bytep sp, dp;
  91.368 +            png_uint_32 i;
  91.369 +            png_uint_32 row_width = row_info->width;
  91.370 +
  91.371 +            for (i = 0, sp = dp = row; i < row_width; i++)
  91.372 +            {
  91.373 +               png_byte save[2];
  91.374 +               save[0] = *(sp++);
  91.375 +               save[1] = *(sp++);
  91.376 +               *(dp++) = *(sp++);
  91.377 +               *(dp++) = *(sp++);
  91.378 +               *(dp++) = *(sp++);
  91.379 +               *(dp++) = *(sp++);
  91.380 +               *(dp++) = *(sp++);
  91.381 +               *(dp++) = *(sp++);
  91.382 +               *(dp++) = save[0];
  91.383 +               *(dp++) = save[1];
  91.384 +            }
  91.385 +         }
  91.386 +      }
  91.387 +      else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
  91.388 +      {
  91.389 +         /* This converts from AG to GA */
  91.390 +         if (row_info->bit_depth == 8)
  91.391 +         {
  91.392 +            png_bytep sp, dp;
  91.393 +            png_uint_32 i;
  91.394 +            png_uint_32 row_width = row_info->width;
  91.395 +
  91.396 +            for (i = 0, sp = dp = row; i < row_width; i++)
  91.397 +            {
  91.398 +               png_byte save = *(sp++);
  91.399 +               *(dp++) = *(sp++);
  91.400 +               *(dp++) = save;
  91.401 +            }
  91.402 +         }
  91.403 +         /* This converts from AAGG to GGAA */
  91.404 +         else
  91.405 +         {
  91.406 +            png_bytep sp, dp;
  91.407 +            png_uint_32 i;
  91.408 +            png_uint_32 row_width = row_info->width;
  91.409 +
  91.410 +            for (i = 0, sp = dp = row; i < row_width; i++)
  91.411 +            {
  91.412 +               png_byte save[2];
  91.413 +               save[0] = *(sp++);
  91.414 +               save[1] = *(sp++);
  91.415 +               *(dp++) = *(sp++);
  91.416 +               *(dp++) = *(sp++);
  91.417 +               *(dp++) = save[0];
  91.418 +               *(dp++) = save[1];
  91.419 +            }
  91.420 +         }
  91.421 +      }
  91.422 +   }
  91.423 +}
  91.424 +#endif
  91.425 +
  91.426 +#if defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED)
  91.427 +void /* PRIVATE */
  91.428 +png_do_write_invert_alpha(png_row_infop row_info, png_bytep row)
  91.429 +{
  91.430 +   png_debug(1, "in png_do_write_invert_alpha\n");
  91.431 +#if defined(PNG_USELESS_TESTS_SUPPORTED)
  91.432 +   if (row != NULL && row_info != NULL)
  91.433 +#endif
  91.434 +   {
  91.435 +      if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
  91.436 +      {
  91.437 +         /* This inverts the alpha channel in RGBA */
  91.438 +         if (row_info->bit_depth == 8)
  91.439 +         {
  91.440 +            png_bytep sp, dp;
  91.441 +            png_uint_32 i;
  91.442 +            png_uint_32 row_width = row_info->width;
  91.443 +            for (i = 0, sp = dp = row; i < row_width; i++)
  91.444 +            {
  91.445 +               /* does nothing
  91.446 +               *(dp++) = *(sp++);
  91.447 +               *(dp++) = *(sp++);
  91.448 +               *(dp++) = *(sp++);
  91.449 +               */
  91.450 +               sp+=3; dp = sp;
  91.451 +               *(dp++) = (png_byte)(255 - *(sp++));
  91.452 +            }
  91.453 +         }
  91.454 +         /* This inverts the alpha channel in RRGGBBAA */
  91.455 +         else
  91.456 +         {
  91.457 +            png_bytep sp, dp;
  91.458 +            png_uint_32 i;
  91.459 +            png_uint_32 row_width = row_info->width;
  91.460 +
  91.461 +            for (i = 0, sp = dp = row; i < row_width; i++)
  91.462 +            {
  91.463 +               /* does nothing
  91.464 +               *(dp++) = *(sp++);
  91.465 +               *(dp++) = *(sp++);
  91.466 +               *(dp++) = *(sp++);
  91.467 +               *(dp++) = *(sp++);
  91.468 +               *(dp++) = *(sp++);
  91.469 +               *(dp++) = *(sp++);
  91.470 +               */
  91.471 +               sp+=6; dp = sp;
  91.472 +               *(dp++) = (png_byte)(255 - *(sp++));
  91.473 +               *(dp++) = (png_byte)(255 - *(sp++));
  91.474 +            }
  91.475 +         }
  91.476 +      }
  91.477 +      else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
  91.478 +      {
  91.479 +         /* This inverts the alpha channel in GA */
  91.480 +         if (row_info->bit_depth == 8)
  91.481 +         {
  91.482 +            png_bytep sp, dp;
  91.483 +            png_uint_32 i;
  91.484 +            png_uint_32 row_width = row_info->width;
  91.485 +
  91.486 +            for (i = 0, sp = dp = row; i < row_width; i++)
  91.487 +            {
  91.488 +               *(dp++) = *(sp++);
  91.489 +               *(dp++) = (png_byte)(255 - *(sp++));
  91.490 +            }
  91.491 +         }
  91.492 +         /* This inverts the alpha channel in GGAA */
  91.493 +         else
  91.494 +         {
  91.495 +            png_bytep sp, dp;
  91.496 +            png_uint_32 i;
  91.497 +            png_uint_32 row_width = row_info->width;
  91.498 +
  91.499 +            for (i = 0, sp = dp = row; i < row_width; i++)
  91.500 +            {
  91.501 +               /* does nothing
  91.502 +               *(dp++) = *(sp++);
  91.503 +               *(dp++) = *(sp++);
  91.504 +               */
  91.505 +               sp+=2; dp = sp;
  91.506 +               *(dp++) = (png_byte)(255 - *(sp++));
  91.507 +               *(dp++) = (png_byte)(255 - *(sp++));
  91.508 +            }
  91.509 +         }
  91.510 +      }
  91.511 +   }
  91.512 +}
  91.513 +#endif
  91.514 +
  91.515 +#if defined(PNG_MNG_FEATURES_SUPPORTED)
  91.516 +/* undoes intrapixel differencing  */
  91.517 +void /* PRIVATE */
  91.518 +png_do_write_intrapixel(png_row_infop row_info, png_bytep row)
  91.519 +{
  91.520 +   png_debug(1, "in png_do_write_intrapixel\n");
  91.521 +   if (
  91.522 +#if defined(PNG_USELESS_TESTS_SUPPORTED)
  91.523 +       row != NULL && row_info != NULL &&
  91.524 +#endif
  91.525 +       (row_info->color_type & PNG_COLOR_MASK_COLOR))
  91.526 +   {
  91.527 +      int bytes_per_pixel;
  91.528 +      png_uint_32 row_width = row_info->width;
  91.529 +      if (row_info->bit_depth == 8)
  91.530 +      {
  91.531 +         png_bytep rp;
  91.532 +         png_uint_32 i;
  91.533 +
  91.534 +         if (row_info->color_type == PNG_COLOR_TYPE_RGB)
  91.535 +            bytes_per_pixel = 3;
  91.536 +         else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
  91.537 +            bytes_per_pixel = 4;
  91.538 +         else
  91.539 +            return;
  91.540 +
  91.541 +         for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
  91.542 +         {
  91.543 +            *(rp)   = (png_byte)((*rp     - *(rp+1))&0xff);
  91.544 +            *(rp+2) = (png_byte)((*(rp+2) - *(rp+1))&0xff);
  91.545 +         }
  91.546 +      }
  91.547 +      else if (row_info->bit_depth == 16)
  91.548 +      {
  91.549 +         png_bytep rp;
  91.550 +         png_uint_32 i;
  91.551 +
  91.552 +         if (row_info->color_type == PNG_COLOR_TYPE_RGB)
  91.553 +            bytes_per_pixel = 6;
  91.554 +         else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
  91.555 +            bytes_per_pixel = 8;
  91.556 +         else
  91.557 +            return;
  91.558 +
  91.559 +         for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
  91.560 +         {
  91.561 +            png_uint_32 s0   = (*(rp  ) << 8) | *(rp+1);
  91.562 +            png_uint_32 s1   = (*(rp+2) << 8) | *(rp+3);
  91.563 +            png_uint_32 s2   = (*(rp+4) << 8) | *(rp+5);
  91.564 +            png_uint_32 red  = (png_uint_32)((s0 - s1) & 0xffffL);
  91.565 +            png_uint_32 blue = (png_uint_32)((s2 - s1) & 0xffffL);
  91.566 +            *(rp  ) = (png_byte)((red >> 8) & 0xff);
  91.567 +            *(rp+1) = (png_byte)(red & 0xff);
  91.568 +            *(rp+4) = (png_byte)((blue >> 8) & 0xff);
  91.569 +            *(rp+5) = (png_byte)(blue & 0xff);
  91.570 +         }
  91.571 +      }
  91.572 +   }
  91.573 +}
  91.574 +#endif /* PNG_MNG_FEATURES_SUPPORTED */
  91.575 +#endif /* PNG_WRITE_SUPPORTED */
    92.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    92.2 +++ b/libs/libpng/pngwutil.c	Sat Sep 19 05:51:51 2015 +0300
    92.3 @@ -0,0 +1,2827 @@
    92.4 +
    92.5 +/* pngwutil.c - utilities to write a PNG file
    92.6 + *
    92.7 + * Last changed in libpng 1.2.30 [August 15, 2008]
    92.8 + * For conditions of distribution and use, see copyright notice in png.h
    92.9 + * Copyright (c) 1998-2008 Glenn Randers-Pehrson
   92.10 + * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
   92.11 + * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
   92.12 + */
   92.13 +
   92.14 +#define PNG_INTERNAL
   92.15 +#include "png.h"
   92.16 +#ifdef PNG_WRITE_SUPPORTED
   92.17 +
   92.18 +/* Place a 32-bit number into a buffer in PNG byte order.  We work
   92.19 + * with unsigned numbers for convenience, although one supported
   92.20 + * ancillary chunk uses signed (two's complement) numbers.
   92.21 + */
   92.22 +void PNGAPI
   92.23 +png_save_uint_32(png_bytep buf, png_uint_32 i)
   92.24 +{
   92.25 +   buf[0] = (png_byte)((i >> 24) & 0xff);
   92.26 +   buf[1] = (png_byte)((i >> 16) & 0xff);
   92.27 +   buf[2] = (png_byte)((i >> 8) & 0xff);
   92.28 +   buf[3] = (png_byte)(i & 0xff);
   92.29 +}
   92.30 +
   92.31 +/* The png_save_int_32 function assumes integers are stored in two's
   92.32 + * complement format.  If this isn't the case, then this routine needs to
   92.33 + * be modified to write data in two's complement format.
   92.34 + */
   92.35 +void PNGAPI
   92.36 +png_save_int_32(png_bytep buf, png_int_32 i)
   92.37 +{
   92.38 +   buf[0] = (png_byte)((i >> 24) & 0xff);
   92.39 +   buf[1] = (png_byte)((i >> 16) & 0xff);
   92.40 +   buf[2] = (png_byte)((i >> 8) & 0xff);
   92.41 +   buf[3] = (png_byte)(i & 0xff);
   92.42 +}
   92.43 +
   92.44 +/* Place a 16-bit number into a buffer in PNG byte order.
   92.45 + * The parameter is declared unsigned int, not png_uint_16,
   92.46 + * just to avoid potential problems on pre-ANSI C compilers.
   92.47 + */
   92.48 +void PNGAPI
   92.49 +png_save_uint_16(png_bytep buf, unsigned int i)
   92.50 +{
   92.51 +   buf[0] = (png_byte)((i >> 8) & 0xff);
   92.52 +   buf[1] = (png_byte)(i & 0xff);
   92.53 +}
   92.54 +
   92.55 +/* Simple function to write the signature.  If we have already written
   92.56 + * the magic bytes of the signature, or more likely, the PNG stream is
   92.57 + * being embedded into another stream and doesn't need its own signature,
   92.58 + * we should call png_set_sig_bytes() to tell libpng how many of the
   92.59 + * bytes have already been written.
   92.60 + */
   92.61 +void /* PRIVATE */
   92.62 +png_write_sig(png_structp png_ptr)
   92.63 +{
   92.64 +   png_byte png_signature[8] = {137, 80, 78, 71, 13, 10, 26, 10};
   92.65 +
   92.66 +   /* write the rest of the 8 byte signature */
   92.67 +   png_write_data(png_ptr, &png_signature[png_ptr->sig_bytes],
   92.68 +      (png_size_t)(8 - png_ptr->sig_bytes));
   92.69 +   if (png_ptr->sig_bytes < 3)
   92.70 +      png_ptr->mode |= PNG_HAVE_PNG_SIGNATURE;
   92.71 +}
   92.72 +
   92.73 +/* Write a PNG chunk all at once.  The type is an array of ASCII characters
   92.74 + * representing the chunk name.  The array must be at least 4 bytes in
   92.75 + * length, and does not need to be null terminated.  To be safe, pass the
   92.76 + * pre-defined chunk names here, and if you need a new one, define it
   92.77 + * where the others are defined.  The length is the length of the data.
   92.78 + * All the data must be present.  If that is not possible, use the
   92.79 + * png_write_chunk_start(), png_write_chunk_data(), and png_write_chunk_end()
   92.80 + * functions instead.
   92.81 + */
   92.82 +void PNGAPI
   92.83 +png_write_chunk(png_structp png_ptr, png_bytep chunk_name,
   92.84 +   png_bytep data, png_size_t length)
   92.85 +{
   92.86 +   if (png_ptr == NULL) return;
   92.87 +   png_write_chunk_start(png_ptr, chunk_name, (png_uint_32)length);
   92.88 +   png_write_chunk_data(png_ptr, data, (png_size_t)length);
   92.89 +   png_write_chunk_end(png_ptr);
   92.90 +}
   92.91 +
   92.92 +/* Write the start of a PNG chunk.  The type is the chunk type.
   92.93 + * The total_length is the sum of the lengths of all the data you will be
   92.94 + * passing in png_write_chunk_data().
   92.95 + */
   92.96 +void PNGAPI
   92.97 +png_write_chunk_start(png_structp png_ptr, png_bytep chunk_name,
   92.98 +   png_uint_32 length)
   92.99 +{
  92.100 +   png_byte buf[8];
  92.101 +
  92.102 +   png_debug2(0, "Writing %s chunk, length = %lu\n", chunk_name,
  92.103 +      (unsigned long)length);
  92.104 +   if (png_ptr == NULL) return;
  92.105 +
  92.106 +   /* write the length and the chunk name */
  92.107 +   png_save_uint_32(buf, length);
  92.108 +   png_memcpy(buf + 4, chunk_name, 4);
  92.109 +   png_write_data(png_ptr, buf, (png_size_t)8);
  92.110 +   /* put the chunk name into png_ptr->chunk_name */
  92.111 +   png_memcpy(png_ptr->chunk_name, chunk_name, 4);
  92.112 +   /* reset the crc and run it over the chunk name */
  92.113 +   png_reset_crc(png_ptr);
  92.114 +   png_calculate_crc(png_ptr, chunk_name, (png_size_t)4);
  92.115 +}
  92.116 +
  92.117 +/* Write the data of a PNG chunk started with png_write_chunk_start().
  92.118 + * Note that multiple calls to this function are allowed, and that the
  92.119 + * sum of the lengths from these calls *must* add up to the total_length
  92.120 + * given to png_write_chunk_start().
  92.121 + */
  92.122 +void PNGAPI
  92.123 +png_write_chunk_data(png_structp png_ptr, png_bytep data, png_size_t length)
  92.124 +{
  92.125 +   /* write the data, and run the CRC over it */
  92.126 +   if (png_ptr == NULL) return;
  92.127 +   if (data != NULL && length > 0)
  92.128 +   {
  92.129 +      png_write_data(png_ptr, data, length);
  92.130 +      /* update the CRC after writing the data,
  92.131 +       * in case that the user I/O routine alters it.
  92.132 +       */
  92.133 +      png_calculate_crc(png_ptr, data, length);
  92.134 +   }
  92.135 +}
  92.136 +
  92.137 +/* Finish a chunk started with png_write_chunk_start(). */
  92.138 +void PNGAPI
  92.139 +png_write_chunk_end(png_structp png_ptr)
  92.140 +{
  92.141 +   png_byte buf[4];
  92.142 +
  92.143 +   if (png_ptr == NULL) return;
  92.144 +
  92.145 +   /* write the crc in a single operation */
  92.146 +   png_save_uint_32(buf, png_ptr->crc);
  92.147 +
  92.148 +   png_write_data(png_ptr, buf, (png_size_t)4);
  92.149 +}
  92.150 +
  92.151 +#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_iCCP_SUPPORTED)
  92.152 +/*
  92.153 + * This pair of functions encapsulates the operation of (a) compressing a
  92.154 + * text string, and (b) issuing it later as a series of chunk data writes.
  92.155 + * The compression_state structure is shared context for these functions
  92.156 + * set up by the caller in order to make the whole mess thread-safe.
  92.157 + */
  92.158 +
  92.159 +typedef struct
  92.160 +{
  92.161 +    char *input;   /* the uncompressed input data */
  92.162 +    int input_len;   /* its length */
  92.163 +    int num_output_ptr; /* number of output pointers used */
  92.164 +    int max_output_ptr; /* size of output_ptr */
  92.165 +    png_charpp output_ptr; /* array of pointers to output */
  92.166 +} compression_state;
  92.167 +
  92.168 +/* compress given text into storage in the png_ptr structure */
  92.169 +static int /* PRIVATE */
  92.170 +png_text_compress(png_structp png_ptr,
  92.171 +        png_charp text, png_size_t text_len, int compression,
  92.172 +        compression_state *comp)
  92.173 +{
  92.174 +   int ret;
  92.175 +
  92.176 +   comp->num_output_ptr = 0;
  92.177 +   comp->max_output_ptr = 0;
  92.178 +   comp->output_ptr = NULL;
  92.179 +   comp->input = NULL;
  92.180 +   comp->input_len = 0;
  92.181 +
  92.182 +   /* we may just want to pass the text right through */
  92.183 +   if (compression == PNG_TEXT_COMPRESSION_NONE)
  92.184 +   {
  92.185 +       comp->input = text;
  92.186 +       comp->input_len = text_len;
  92.187 +       return((int)text_len);
  92.188 +   }
  92.189 +
  92.190 +   if (compression >= PNG_TEXT_COMPRESSION_LAST)
  92.191 +   {
  92.192 +#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
  92.193 +      char msg[50];
  92.194 +      png_snprintf(msg, 50, "Unknown compression type %d", compression);
  92.195 +      png_warning(png_ptr, msg);
  92.196 +#else
  92.197 +      png_warning(png_ptr, "Unknown compression type");
  92.198 +#endif
  92.199 +   }
  92.200 +
  92.201 +   /* We can't write the chunk until we find out how much data we have,
  92.202 +    * which means we need to run the compressor first and save the
  92.203 +    * output.  This shouldn't be a problem, as the vast majority of
  92.204 +    * comments should be reasonable, but we will set up an array of
  92.205 +    * malloc'd pointers to be sure.
  92.206 +    *
  92.207 +    * If we knew the application was well behaved, we could simplify this
  92.208 +    * greatly by assuming we can always malloc an output buffer large
  92.209 +    * enough to hold the compressed text ((1001 * text_len / 1000) + 12)
  92.210 +    * and malloc this directly.  The only time this would be a bad idea is
  92.211 +    * if we can't malloc more than 64K and we have 64K of random input
  92.212 +    * data, or if the input string is incredibly large (although this
  92.213 +    * wouldn't cause a failure, just a slowdown due to swapping).
  92.214 +    */
  92.215 +
  92.216 +   /* set up the compression buffers */
  92.217 +   png_ptr->zstream.avail_in = (uInt)text_len;
  92.218 +   png_ptr->zstream.next_in = (Bytef *)text;
  92.219 +   png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
  92.220 +   png_ptr->zstream.next_out = (Bytef *)png_ptr->zbuf;
  92.221 +
  92.222 +   /* this is the same compression loop as in png_write_row() */
  92.223 +   do
  92.224 +   {
  92.225 +      /* compress the data */
  92.226 +      ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);
  92.227 +      if (ret != Z_OK)
  92.228 +      {
  92.229 +         /* error */
  92.230 +         if (png_ptr->zstream.msg != NULL)
  92.231 +            png_error(png_ptr, png_ptr->zstream.msg);
  92.232 +         else
  92.233 +            png_error(png_ptr, "zlib error");
  92.234 +      }
  92.235 +      /* check to see if we need more room */
  92.236 +      if (!(png_ptr->zstream.avail_out))
  92.237 +      {
  92.238 +         /* make sure the output array has room */
  92.239 +         if (comp->num_output_ptr >= comp->max_output_ptr)
  92.240 +         {
  92.241 +            int old_max;
  92.242 +
  92.243 +            old_max = comp->max_output_ptr;
  92.244 +            comp->max_output_ptr = comp->num_output_ptr + 4;
  92.245 +            if (comp->output_ptr != NULL)
  92.246 +            {
  92.247 +               png_charpp old_ptr;
  92.248 +
  92.249 +               old_ptr = comp->output_ptr;
  92.250 +               comp->output_ptr = (png_charpp)png_malloc(png_ptr,
  92.251 +                  (png_uint_32)
  92.252 +                  (comp->max_output_ptr * png_sizeof(png_charpp)));
  92.253 +               png_memcpy(comp->output_ptr, old_ptr, old_max
  92.254 +                  * png_sizeof(png_charp));
  92.255 +               png_free(png_ptr, old_ptr);
  92.256 +            }
  92.257 +            else
  92.258 +               comp->output_ptr = (png_charpp)png_malloc(png_ptr,
  92.259 +                  (png_uint_32)
  92.260 +                  (comp->max_output_ptr * png_sizeof(png_charp)));
  92.261 +         }
  92.262 +
  92.263 +         /* save the data */
  92.264 +         comp->output_ptr[comp->num_output_ptr] =
  92.265 +            (png_charp)png_malloc(png_ptr,
  92.266 +            (png_uint_32)png_ptr->zbuf_size);
  92.267 +         png_memcpy(comp->output_ptr[comp->num_output_ptr], png_ptr->zbuf,
  92.268 +            png_ptr->zbuf_size);
  92.269 +         comp->num_output_ptr++;
  92.270 +
  92.271 +         /* and reset the buffer */
  92.272 +         png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
  92.273 +         png_ptr->zstream.next_out = png_ptr->zbuf;
  92.274 +      }
  92.275 +   /* continue until we don't have any more to compress */
  92.276 +   } while (png_ptr->zstream.avail_in);
  92.277 +
  92.278 +   /* finish the compression */
  92.279 +   do
  92.280 +   {
  92.281 +      /* tell zlib we are finished */
  92.282 +      ret = deflate(&png_ptr->zstream, Z_FINISH);
  92.283 +
  92.284 +      if (ret == Z_OK)
  92.285 +      {
  92.286 +         /* check to see if we need more room */
  92.287 +         if (!(png_ptr->zstream.avail_out))
  92.288 +         {
  92.289 +            /* check to make sure our output array has room */
  92.290 +            if (comp->num_output_ptr >= comp->max_output_ptr)
  92.291 +            {
  92.292 +               int old_max;
  92.293 +
  92.294 +               old_max = comp->max_output_ptr;
  92.295 +               comp->max_output_ptr = comp->num_output_ptr + 4;
  92.296 +               if (comp->output_ptr != NULL)
  92.297 +               {
  92.298 +                  png_charpp old_ptr;
  92.299 +
  92.300 +                  old_ptr = comp->output_ptr;
  92.301 +                  /* This could be optimized to realloc() */
  92.302 +                  comp->output_ptr = (png_charpp)png_malloc(png_ptr,
  92.303 +                     (png_uint_32)(comp->max_output_ptr *
  92.304 +                     png_sizeof(png_charp)));
  92.305 +                  png_memcpy(comp->output_ptr, old_ptr,
  92.306 +                     old_max * png_sizeof(png_charp));
  92.307 +                  png_free(png_ptr, old_ptr);
  92.308 +               }
  92.309 +               else
  92.310 +                  comp->output_ptr = (png_charpp)png_malloc(png_ptr,
  92.311 +                     (png_uint_32)(comp->max_output_ptr *
  92.312 +                     png_sizeof(png_charp)));
  92.313 +            }
  92.314 +
  92.315 +            /* save off the data */
  92.316 +            comp->output_ptr[comp->num_output_ptr] =
  92.317 +               (png_charp)png_malloc(png_ptr,
  92.318 +               (png_uint_32)png_ptr->zbuf_size);
  92.319 +            png_memcpy(comp->output_ptr[comp->num_output_ptr], png_ptr->zbuf,
  92.320 +               png_ptr->zbuf_size);
  92.321 +            comp->num_output_ptr++;
  92.322 +
  92.323 +            /* and reset the buffer pointers */
  92.324 +            png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
  92.325 +            png_ptr->zstream.next_out = png_ptr->zbuf;
  92.326 +         }
  92.327 +      }
  92.328 +      else if (ret != Z_STREAM_END)
  92.329 +      {
  92.330 +         /* we got an error */
  92.331 +         if (png_ptr->zstream.msg != NULL)
  92.332 +            png_error(png_ptr, png_ptr->zstream.msg);
  92.333 +         else
  92.334 +            png_error(png_ptr, "zlib error");
  92.335 +      }
  92.336 +   } while (ret != Z_STREAM_END);
  92.337 +
  92.338 +   /* text length is number of buffers plus last buffer */
  92.339 +   text_len = png_ptr->zbuf_size * comp->num_output_ptr;
  92.340 +   if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
  92.341 +      text_len += png_ptr->zbuf_size - (png_size_t)png_ptr->zstream.avail_out;
  92.342 +
  92.343 +   return((int)text_len);
  92.344 +}
  92.345 +
  92.346 +/* ship the compressed text out via chunk writes */
  92.347 +static void /* PRIVATE */
  92.348 +png_write_compressed_data_out(png_structp png_ptr, compression_state *comp)
  92.349 +{
  92.350 +   int i;
  92.351 +
  92.352 +   /* handle the no-compression case */
  92.353 +   if (comp->input)
  92.354 +   {
  92.355 +       png_write_chunk_data(png_ptr, (png_bytep)comp->input,
  92.356 +                            (png_size_t)comp->input_len);
  92.357 +       return;
  92.358 +   }
  92.359 +
  92.360 +   /* write saved output buffers, if any */
  92.361 +   for (i = 0; i < comp->num_output_ptr; i++)
  92.362 +   {
  92.363 +      png_write_chunk_data(png_ptr, (png_bytep)comp->output_ptr[i],
  92.364 +         (png_size_t)png_ptr->zbuf_size);
  92.365 +      png_free(png_ptr, comp->output_ptr[i]);
  92.366 +       comp->output_ptr[i]=NULL;
  92.367 +   }
  92.368 +   if (comp->max_output_ptr != 0)
  92.369 +      png_free(png_ptr, comp->output_ptr);
  92.370 +       comp->output_ptr=NULL;
  92.371 +   /* write anything left in zbuf */
  92.372 +   if (png_ptr->zstream.avail_out < (png_uint_32)png_ptr->zbuf_size)
  92.373 +      png_write_chunk_data(png_ptr, png_ptr->zbuf,
  92.374 +         (png_size_t)(png_ptr->zbuf_size - png_ptr->zstream.avail_out));
  92.375 +
  92.376 +   /* reset zlib for another zTXt/iTXt or image data */
  92.377 +   deflateReset(&png_ptr->zstream);
  92.378 +   png_ptr->zstream.data_type = Z_BINARY;
  92.379 +}
  92.380 +#endif
  92.381 +
  92.382 +/* Write the IHDR chunk, and update the png_struct with the necessary
  92.383 + * information.  Note that the rest of this code depends upon this
  92.384 + * information being correct.
  92.385 + */
  92.386 +void /* PRIVATE */
  92.387 +png_write_IHDR(png_structp png_ptr, png_uint_32 width, png_uint_32 height,
  92.388 +   int bit_depth, int color_type, int compression_type, int filter_type,
  92.389 +   int interlace_type)
  92.390 +{
  92.391 +#ifdef PNG_USE_LOCAL_ARRAYS
  92.392 +   PNG_IHDR;
  92.393 +#endif
  92.394 +   int ret;
  92.395 +
  92.396 +   png_byte buf[13]; /* buffer to store the IHDR info */
  92.397 +
  92.398 +   png_debug(1, "in png_write_IHDR\n");
  92.399 +   /* Check that we have valid input data from the application info */
  92.400 +   switch (color_type)
  92.401 +   {
  92.402 +      case PNG_COLOR_TYPE_GRAY:
  92.403 +         switch (bit_depth)
  92.404 +         {
  92.405 +            case 1:
  92.406 +            case 2:
  92.407 +            case 4:
  92.408 +            case 8:
  92.409 +            case 16: png_ptr->channels = 1; break;
  92.410 +            default: png_error(png_ptr, "Invalid bit depth for grayscale image");
  92.411 +         }
  92.412 +         break;
  92.413 +      case PNG_COLOR_TYPE_RGB:
  92.414 +         if (bit_depth != 8 && bit_depth != 16)
  92.415 +            png_error(png_ptr, "Invalid bit depth for RGB image");
  92.416 +         png_ptr->channels = 3;
  92.417 +         break;
  92.418 +      case PNG_COLOR_TYPE_PALETTE:
  92.419 +         switch (bit_depth)
  92.420 +         {
  92.421 +            case 1:
  92.422 +            case 2:
  92.423 +            case 4:
  92.424 +            case 8: png_ptr->channels = 1; break;
  92.425 +            default: png_error(png_ptr, "Invalid bit depth for paletted image");
  92.426 +         }
  92.427 +         break;
  92.428 +      case PNG_COLOR_TYPE_GRAY_ALPHA:
  92.429 +         if (bit_depth != 8 && bit_depth != 16)
  92.430 +            png_error(png_ptr, "Invalid bit depth for grayscale+alpha image");
  92.431 +         png_ptr->channels = 2;
  92.432 +         break;
  92.433 +      case PNG_COLOR_TYPE_RGB_ALPHA:
  92.434 +         if (bit_depth != 8 && bit_depth != 16)
  92.435 +            png_error(png_ptr, "Invalid bit depth for RGBA image");
  92.436 +         png_ptr->channels = 4;
  92.437 +         break;
  92.438 +      default:
  92.439 +         png_error(png_ptr, "Invalid image color type specified");
  92.440 +   }
  92.441 +
  92.442 +   if (compression_type != PNG_COMPRESSION_TYPE_BASE)
  92.443 +   {
  92.444 +      png_warning(png_ptr, "Invalid compression type specified");
  92.445 +      compression_type = PNG_COMPRESSION_TYPE_BASE;
  92.446 +   }
  92.447 +
  92.448 +   /* Write filter_method 64 (intrapixel differencing) only if
  92.449 +    * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
  92.450 +    * 2. Libpng did not write a PNG signature (this filter_method is only
  92.451 +    *    used in PNG datastreams that are embedded in MNG datastreams) and
  92.452 +    * 3. The application called png_permit_mng_features with a mask that
  92.453 +    *    included PNG_FLAG_MNG_FILTER_64 and
  92.454 +    * 4. The filter_method is 64 and
  92.455 +    * 5. The color_type is RGB or RGBA
  92.456 +    */
  92.457 +   if (
  92.458 +#if defined(PNG_MNG_FEATURES_SUPPORTED)
  92.459 +      !((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
  92.460 +      ((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE) == 0) &&
  92.461 +      (color_type == PNG_COLOR_TYPE_RGB ||
  92.462 +       color_type == PNG_COLOR_TYPE_RGB_ALPHA) &&
  92.463 +      (filter_type == PNG_INTRAPIXEL_DIFFERENCING)) &&
  92.464 +#endif
  92.465 +      filter_type != PNG_FILTER_TYPE_BASE)
  92.466 +   {
  92.467 +      png_warning(png_ptr, "Invalid filter type specified");
  92.468 +      filter_type = PNG_FILTER_TYPE_BASE;
  92.469 +   }
  92.470 +
  92.471 +#ifdef PNG_WRITE_INTERLACING_SUPPORTED
  92.472 +   if (interlace_type != PNG_INTERLACE_NONE &&
  92.473 +      interlace_type != PNG_INTERLACE_ADAM7)
  92.474 +   {
  92.475 +      png_warning(png_ptr, "Invalid interlace type specified");
  92.476 +      interlace_type = PNG_INTERLACE_ADAM7;
  92.477 +   }
  92.478 +#else
  92.479 +   interlace_type=PNG_INTERLACE_NONE;
  92.480 +#endif
  92.481 +
  92.482 +   /* save off the relevent information */
  92.483 +   png_ptr->bit_depth = (png_byte)bit_depth;
  92.484 +   png_ptr->color_type = (png_byte)color_type;
  92.485 +   png_ptr->interlaced = (png_byte)interlace_type;
  92.486 +#if defined(PNG_MNG_FEATURES_SUPPORTED)
  92.487 +   png_ptr->filter_type = (png_byte)filter_type;
  92.488 +#endif
  92.489 +   png_ptr->compression_type = (png_byte)compression_type;
  92.490 +   png_ptr->width = width;
  92.491 +   png_ptr->height = height;
  92.492 +
  92.493 +   png_ptr->pixel_depth = (png_byte)(bit_depth * png_ptr->channels);
  92.494 +   png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, width);
  92.495 +   /* set the usr info, so any transformations can modify it */
  92.496 +   png_ptr->usr_width = png_ptr->width;
  92.497 +   png_ptr->usr_bit_depth = png_ptr->bit_depth;
  92.498 +   png_ptr->usr_channels = png_ptr->channels;
  92.499 +
  92.500 +   /* pack the header information into the buffer */
  92.501 +   png_save_uint_32(buf, width);
  92.502 +   png_save_uint_32(buf + 4, height);
  92.503 +   buf[8] = (png_byte)bit_depth;
  92.504 +   buf[9] = (png_byte)color_type;
  92.505 +   buf[10] = (png_byte)compression_type;
  92.506 +   buf[11] = (png_byte)filter_type;
  92.507 +   buf[12] = (png_byte)interlace_type;
  92.508 +
  92.509 +   /* write the chunk */
  92.510 +   png_write_chunk(png_ptr, (png_bytep)png_IHDR, buf, (png_size_t)13);
  92.511 +
  92.512 +   /* initialize zlib with PNG info */
  92.513 +   png_ptr->zstream.zalloc = png_zalloc;
  92.514 +   png_ptr->zstream.zfree = png_zfree;
  92.515 +   png_ptr->zstream.opaque = (voidpf)png_ptr;
  92.516 +   if (!(png_ptr->do_filter))
  92.517 +   {
  92.518 +      if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE ||
  92.519 +         png_ptr->bit_depth < 8)
  92.520 +         png_ptr->do_filter = PNG_FILTER_NONE;
  92.521 +      else
  92.522 +         png_ptr->do_filter = PNG_ALL_FILTERS;
  92.523 +   }
  92.524 +   if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_STRATEGY))
  92.525 +   {
  92.526 +      if (png_ptr->do_filter != PNG_FILTER_NONE)
  92.527 +         png_ptr->zlib_strategy = Z_FILTERED;
  92.528 +      else
  92.529 +         png_ptr->zlib_strategy = Z_DEFAULT_STRATEGY;
  92.530 +   }
  92.531 +   if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_LEVEL))
  92.532 +      png_ptr->zlib_level = Z_DEFAULT_COMPRESSION;
  92.533 +   if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL))
  92.534 +      png_ptr->zlib_mem_level = 8;
  92.535 +   if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS))
  92.536 +      png_ptr->zlib_window_bits = 15;
  92.537 +   if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_METHOD))
  92.538 +      png_ptr->zlib_method = 8;
  92.539 +   ret = deflateInit2(&png_ptr->zstream, png_ptr->zlib_level,
  92.540 +         png_ptr->zlib_method, png_ptr->zlib_window_bits,
  92.541 +         png_ptr->zlib_mem_level, png_ptr->zlib_strategy);
  92.542 +   if (ret != Z_OK)
  92.543 +   {
  92.544 +      if (ret == Z_VERSION_ERROR) png_error(png_ptr,
  92.545 +          "zlib failed to initialize compressor -- version error");
  92.546 +      if (ret == Z_STREAM_ERROR) png_error(png_ptr,
  92.547 +           "zlib failed to initialize compressor -- stream error");
  92.548 +      if (ret == Z_MEM_ERROR) png_error(png_ptr,
  92.549 +           "zlib failed to initialize compressor -- mem error");
  92.550 +      png_error(png_ptr, "zlib failed to initialize compressor");
  92.551 +   }
  92.552 +   png_ptr->zstream.next_out = png_ptr->zbuf;
  92.553 +   png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
  92.554 +   /* libpng is not interested in zstream.data_type */
  92.555 +   /* set it to a predefined value, to avoid its evaluation inside zlib */
  92.556 +   png_ptr->zstream.data_type = Z_BINARY;
  92.557 +
  92.558 +   png_ptr->mode = PNG_HAVE_IHDR;
  92.559 +}
  92.560 +
  92.561 +/* write the palette.  We are careful not to trust png_color to be in the
  92.562 + * correct order for PNG, so people can redefine it to any convenient
  92.563 + * structure.
  92.564 + */
  92.565 +void /* PRIVATE */
  92.566 +png_write_PLTE(png_structp png_ptr, png_colorp palette, png_uint_32 num_pal)
  92.567 +{
  92.568 +#ifdef PNG_USE_LOCAL_ARRAYS
  92.569 +   PNG_PLTE;
  92.570 +#endif
  92.571 +   png_uint_32 i;
  92.572 +   png_colorp pal_ptr;
  92.573 +   png_byte buf[3];
  92.574 +
  92.575 +   png_debug(1, "in png_write_PLTE\n");
  92.576 +   if ((
  92.577 +#if defined(PNG_MNG_FEATURES_SUPPORTED)
  92.578 +        !(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE) &&
  92.579 +#endif
  92.580 +        num_pal == 0) || num_pal > 256)
  92.581 +   {
  92.582 +     if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  92.583 +     {
  92.584 +        png_error(png_ptr, "Invalid number of colors in palette");
  92.585 +     }
  92.586 +     else
  92.587 +     {
  92.588 +        png_warning(png_ptr, "Invalid number of colors in palette");
  92.589 +        return;
  92.590 +     }
  92.591 +   }
  92.592 +
  92.593 +   if (!(png_ptr->color_type&PNG_COLOR_MASK_COLOR))
  92.594 +   {
  92.595 +      png_warning(png_ptr,
  92.596 +        "Ignoring request to write a PLTE chunk in grayscale PNG");
  92.597 +      return;
  92.598 +   }
  92.599 +
  92.600 +   png_ptr->num_palette = (png_uint_16)num_pal;
  92.601 +   png_debug1(3, "num_palette = %d\n", png_ptr->num_palette);
  92.602 +
  92.603 +   png_write_chunk_start(png_ptr, (png_bytep)png_PLTE,
  92.604 +     (png_uint_32)(num_pal * 3));
  92.605 +#ifndef PNG_NO_POINTER_INDEXING
  92.606 +   for (i = 0, pal_ptr = palette; i < num_pal; i++, pal_ptr++)
  92.607 +   {
  92.608 +      buf[0] = pal_ptr->red;
  92.609 +      buf[1] = pal_ptr->green;
  92.610 +      buf[2] = pal_ptr->blue;
  92.611 +      png_write_chunk_data(png_ptr, buf, (png_size_t)3);
  92.612 +   }
  92.613 +#else
  92.614 +   /* This is a little slower but some buggy compilers need to do this instead */
  92.615 +   pal_ptr=palette;
  92.616 +   for (i = 0; i < num_pal; i++)
  92.617 +   {
  92.618 +      buf[0] = pal_ptr[i].red;
  92.619 +      buf[1] = pal_ptr[i].green;
  92.620 +      buf[2] = pal_ptr[i].blue;
  92.621 +      png_write_chunk_data(png_ptr, buf, (png_size_t)3);
  92.622 +   }
  92.623 +#endif
  92.624 +   png_write_chunk_end(png_ptr);
  92.625 +   png_ptr->mode |= PNG_HAVE_PLTE;
  92.626 +}
  92.627 +
  92.628 +/* write an IDAT chunk */
  92.629 +void /* PRIVATE */
  92.630 +png_write_IDAT(png_structp png_ptr, png_bytep data, png_size_t length)
  92.631 +{
  92.632 +#ifdef PNG_USE_LOCAL_ARRAYS
  92.633 +   PNG_IDAT;
  92.634 +#endif
  92.635 +   png_debug(1, "in png_write_IDAT\n");
  92.636 +
  92.637 +   /* Optimize the CMF field in the zlib stream. */
  92.638 +   /* This hack of the zlib stream is compliant to the stream specification. */
  92.639 +   if (!(png_ptr->mode & PNG_HAVE_IDAT) &&
  92.640 +       png_ptr->compression_type == PNG_COMPRESSION_TYPE_BASE)
  92.641 +   {
  92.642 +      unsigned int z_cmf = data[0];  /* zlib compression method and flags */
  92.643 +      if ((z_cmf & 0x0f) == 8 && (z_cmf & 0xf0) <= 0x70)
  92.644 +      {
  92.645 +         /* Avoid memory underflows and multiplication overflows. */
  92.646 +         /* The conditions below are practically always satisfied;
  92.647 +            however, they still must be checked. */
  92.648 +         if (length >= 2 &&
  92.649 +             png_ptr->height < 16384 && png_ptr->width < 16384)
  92.650 +         {
  92.651 +            png_uint_32 uncompressed_idat_size = png_ptr->height *
  92.652 +               ((png_ptr->width *
  92.653 +               png_ptr->channels * png_ptr->bit_depth + 15) >> 3);
  92.654 +            unsigned int z_cinfo = z_cmf >> 4;
  92.655 +            unsigned int half_z_window_size = 1 << (z_cinfo + 7);
  92.656 +            while (uncompressed_idat_size <= half_z_window_size &&
  92.657 +                   half_z_window_size >= 256)
  92.658 +            {
  92.659 +               z_cinfo--;
  92.660 +               half_z_window_size >>= 1;
  92.661 +            }
  92.662 +            z_cmf = (z_cmf & 0x0f) | (z_cinfo << 4);
  92.663 +            if (data[0] != (png_byte)z_cmf)
  92.664 +            {
  92.665 +               data[0] = (png_byte)z_cmf;
  92.666 +               data[1] &= 0xe0;
  92.667 +               data[1] += (png_byte)(0x1f - ((z_cmf << 8) + data[1]) % 0x1f);
  92.668 +            }
  92.669 +         }
  92.670 +      }
  92.671 +      else
  92.672 +         png_error(png_ptr,
  92.673 +            "Invalid zlib compression method or flags in IDAT");
  92.674 +   }
  92.675 +
  92.676 +   png_write_chunk(png_ptr, (png_bytep)png_IDAT, data, length);
  92.677 +   png_ptr->mode |= PNG_HAVE_IDAT;
  92.678 +}
  92.679 +
  92.680 +/* write an IEND chunk */
  92.681 +void /* PRIVATE */
  92.682 +png_write_IEND(png_structp png_ptr)
  92.683 +{
  92.684 +#ifdef PNG_USE_LOCAL_ARRAYS
  92.685 +   PNG_IEND;
  92.686 +#endif
  92.687 +   png_debug(1, "in png_write_IEND\n");
  92.688 +   png_write_chunk(png_ptr, (png_bytep)png_IEND, png_bytep_NULL,
  92.689 +     (png_size_t)0);
  92.690 +   png_ptr->mode |= PNG_HAVE_IEND;
  92.691 +}
  92.692 +
  92.693 +#if defined(PNG_WRITE_gAMA_SUPPORTED)
  92.694 +/* write a gAMA chunk */
  92.695 +#ifdef PNG_FLOATING_POINT_SUPPORTED
  92.696 +void /* PRIVATE */
  92.697 +png_write_gAMA(png_structp png_ptr, double file_gamma)
  92.698 +{
  92.699 +#ifdef PNG_USE_LOCAL_ARRAYS
  92.700 +   PNG_gAMA;
  92.701 +#endif
  92.702 +   png_uint_32 igamma;
  92.703 +   png_byte buf[4];
  92.704 +
  92.705 +   png_debug(1, "in png_write_gAMA\n");
  92.706 +   /* file_gamma is saved in 1/100,000ths */
  92.707 +   igamma = (png_uint_32)(file_gamma * 100000.0 + 0.5);
  92.708 +   png_save_uint_32(buf, igamma);
  92.709 +   png_write_chunk(png_ptr, (png_bytep)png_gAMA, buf, (png_size_t)4);
  92.710 +}
  92.711 +#endif
  92.712 +#ifdef PNG_FIXED_POINT_SUPPORTED
  92.713 +void /* PRIVATE */
  92.714 +png_write_gAMA_fixed(png_structp png_ptr, png_fixed_point file_gamma)
  92.715 +{
  92.716 +#ifdef PNG_USE_LOCAL_ARRAYS
  92.717 +   PNG_gAMA;
  92.718 +#endif
  92.719 +   png_byte buf[4];
  92.720 +
  92.721 +   png_debug(1, "in png_write_gAMA\n");
  92.722 +   /* file_gamma is saved in 1/100,000ths */
  92.723 +   png_save_uint_32(buf, (png_uint_32)file_gamma);
  92.724 +   png_write_chunk(png_ptr, (png_bytep)png_gAMA, buf, (png_size_t)4);
  92.725 +}
  92.726 +#endif
  92.727 +#endif
  92.728 +
  92.729 +#if defined(PNG_WRITE_sRGB_SUPPORTED)
  92.730 +/* write a sRGB chunk */
  92.731 +void /* PRIVATE */
  92.732 +png_write_sRGB(png_structp png_ptr, int srgb_intent)
  92.733 +{
  92.734 +#ifdef PNG_USE_LOCAL_ARRAYS
  92.735 +   PNG_sRGB;
  92.736 +#endif
  92.737 +   png_byte buf[1];
  92.738 +
  92.739 +   png_debug(1, "in png_write_sRGB\n");
  92.740 +   if (srgb_intent >= PNG_sRGB_INTENT_LAST)
  92.741 +         png_warning(png_ptr,
  92.742 +            "Invalid sRGB rendering intent specified");
  92.743 +   buf[0]=(png_byte)srgb_intent;
  92.744 +   png_write_chunk(png_ptr, (png_bytep)png_sRGB, buf, (png_size_t)1);
  92.745 +}
  92.746 +#endif
  92.747 +
  92.748 +#if defined(PNG_WRITE_iCCP_SUPPORTED)
  92.749 +/* write an iCCP chunk */
  92.750 +void /* PRIVATE */
  92.751 +png_write_iCCP(png_structp png_ptr, png_charp name, int compression_type,
  92.752 +   png_charp profile, int profile_len)
  92.753 +{
  92.754 +#ifdef PNG_USE_LOCAL_ARRAYS
  92.755 +   PNG_iCCP;
  92.756 +#endif
  92.757 +   png_size_t name_len;
  92.758 +   png_charp new_name;
  92.759 +   compression_state comp;
  92.760 +   int embedded_profile_len = 0;
  92.761 +
  92.762 +   png_debug(1, "in png_write_iCCP\n");
  92.763 +
  92.764 +   comp.num_output_ptr = 0;
  92.765 +   comp.max_output_ptr = 0;
  92.766 +   comp.output_ptr = NULL;
  92.767 +   comp.input = NULL;
  92.768 +   comp.input_len = 0;
  92.769 +
  92.770 +   if (name == NULL || (name_len = png_check_keyword(png_ptr, name,
  92.771 +      &new_name)) == 0)
  92.772 +   {
  92.773 +      png_warning(png_ptr, "Empty keyword in iCCP chunk");
  92.774 +      return;
  92.775 +   }
  92.776 +
  92.777 +   if (compression_type != PNG_COMPRESSION_TYPE_BASE)
  92.778 +      png_warning(png_ptr, "Unknown compression type in iCCP chunk");
  92.779 +
  92.780 +   if (profile == NULL)
  92.781 +      profile_len = 0;
  92.782 +
  92.783 +   if (profile_len > 3)
  92.784 +      embedded_profile_len =
  92.785 +          ((*( (png_bytep)profile    ))<<24) |
  92.786 +          ((*( (png_bytep)profile + 1))<<16) |
  92.787 +          ((*( (png_bytep)profile + 2))<< 8) |
  92.788 +          ((*( (png_bytep)profile + 3))    );
  92.789 +
  92.790 +   if (profile_len < embedded_profile_len)
  92.791 +   {
  92.792 +      png_warning(png_ptr,
  92.793 +        "Embedded profile length too large in iCCP chunk");
  92.794 +      return;
  92.795 +   }
  92.796 +
  92.797 +   if (profile_len > embedded_profile_len)
  92.798 +   {
  92.799 +      png_warning(png_ptr,
  92.800 +        "Truncating profile to actual length in iCCP chunk");
  92.801 +      profile_len = embedded_profile_len;
  92.802 +   }
  92.803 +
  92.804 +   if (profile_len)
  92.805 +      profile_len = png_text_compress(png_ptr, profile,
  92.806 +        (png_size_t)profile_len, PNG_COMPRESSION_TYPE_BASE, &comp);
  92.807 +
  92.808 +   /* make sure we include the NULL after the name and the compression type */
  92.809 +   png_write_chunk_start(png_ptr, (png_bytep)png_iCCP,
  92.810 +          (png_uint_32)(name_len + profile_len + 2));
  92.811 +   new_name[name_len + 1] = 0x00;
  92.812 +   png_write_chunk_data(png_ptr, (png_bytep)new_name,
  92.813 +     (png_size_t)(name_len + 2));
  92.814 +
  92.815 +   if (profile_len)
  92.816 +      png_write_compressed_data_out(png_ptr, &comp);
  92.817 +
  92.818 +   png_write_chunk_end(png_ptr);
  92.819 +   png_free(png_ptr, new_name);
  92.820 +}
  92.821 +#endif
  92.822 +
  92.823 +#if defined(PNG_WRITE_sPLT_SUPPORTED)
  92.824 +/* write a sPLT chunk */
  92.825 +void /* PRIVATE */
  92.826 +png_write_sPLT(png_structp png_ptr, png_sPLT_tp spalette)
  92.827 +{
  92.828 +#ifdef PNG_USE_LOCAL_ARRAYS
  92.829 +   PNG_sPLT;
  92.830 +#endif
  92.831 +   png_size_t name_len;
  92.832 +   png_charp new_name;
  92.833 +   png_byte entrybuf[10];
  92.834 +   int entry_size = (spalette->depth == 8 ? 6 : 10);
  92.835 +   int palette_size = entry_size * spalette->nentries;
  92.836 +   png_sPLT_entryp ep;
  92.837 +#ifdef PNG_NO_POINTER_INDEXING
  92.838 +   int i;
  92.839 +#endif
  92.840 +
  92.841 +   png_debug(1, "in png_write_sPLT\n");
  92.842 +   if (spalette->name == NULL || (name_len = png_check_keyword(png_ptr,
  92.843 +      spalette->name, &new_name))==0)
  92.844 +   {
  92.845 +      png_warning(png_ptr, "Empty keyword in sPLT chunk");
  92.846 +      return;
  92.847 +   }
  92.848 +
  92.849 +   /* make sure we include the NULL after the name */
  92.850 +   png_write_chunk_start(png_ptr, (png_bytep)png_sPLT,
  92.851 +     (png_uint_32)(name_len + 2 + palette_size));
  92.852 +   png_write_chunk_data(png_ptr, (png_bytep)new_name,
  92.853 +     (png_size_t)(name_len + 1));
  92.854 +   png_write_chunk_data(png_ptr, (png_bytep)&spalette->depth, (png_size_t)1);
  92.855 +
  92.856 +   /* loop through each palette entry, writing appropriately */
  92.857 +#ifndef PNG_NO_POINTER_INDEXING
  92.858 +   for (ep = spalette->entries; ep<spalette->entries + spalette->nentries; ep++)
  92.859 +   {
  92.860 +      if (spalette->depth == 8)
  92.861 +      {
  92.862 +          entrybuf[0] = (png_byte)ep->red;
  92.863 +          entrybuf[1] = (png_byte)ep->green;
  92.864 +          entrybuf[2] = (png_byte)ep->blue;
  92.865 +          entrybuf[3] = (png_byte)ep->alpha;
  92.866 +          png_save_uint_16(entrybuf + 4, ep->frequency);
  92.867 +      }
  92.868 +      else
  92.869 +      {
  92.870 +          png_save_uint_16(entrybuf + 0, ep->red);
  92.871 +          png_save_uint_16(entrybuf + 2, ep->green);
  92.872 +          png_save_uint_16(entrybuf + 4, ep->blue);
  92.873 +          png_save_uint_16(entrybuf + 6, ep->alpha);
  92.874 +          png_save_uint_16(entrybuf + 8, ep->frequency);
  92.875 +      }
  92.876 +      png_write_chunk_data(png_ptr, entrybuf, (png_size_t)entry_size);
  92.877 +   }
  92.878 +#else
  92.879 +   ep=spalette->entries;
  92.880 +   for (i=0; i>spalette->nentries; i++)
  92.881 +   {
  92.882 +      if (spalette->depth == 8)
  92.883 +      {
  92.884 +          entrybuf[0] = (png_byte)ep[i].red;
  92.885 +          entrybuf[1] = (png_byte)ep[i].green;
  92.886 +          entrybuf[2] = (png_byte)ep[i].blue;
  92.887 +          entrybuf[3] = (png_byte)ep[i].alpha;
  92.888 +          png_save_uint_16(entrybuf + 4, ep[i].frequency);
  92.889 +      }
  92.890 +      else
  92.891 +      {
  92.892 +          png_save_uint_16(entrybuf + 0, ep[i].red);
  92.893 +          png_save_uint_16(entrybuf + 2, ep[i].green);
  92.894 +          png_save_uint_16(entrybuf + 4, ep[i].blue);
  92.895 +          png_save_uint_16(entrybuf + 6, ep[i].alpha);
  92.896 +          png_save_uint_16(entrybuf + 8, ep[i].frequency);
  92.897 +      }
  92.898 +      png_write_chunk_data(png_ptr, entrybuf, (png_size_t)entry_size);
  92.899 +   }
  92.900 +#endif
  92.901 +
  92.902 +   png_write_chunk_end(png_ptr);
  92.903 +   png_free(png_ptr, new_name);
  92.904 +}
  92.905 +#endif
  92.906 +
  92.907 +#if defined(PNG_WRITE_sBIT_SUPPORTED)
  92.908 +/* write the sBIT chunk */
  92.909 +void /* PRIVATE */
  92.910 +png_write_sBIT(png_structp png_ptr, png_color_8p sbit, int color_type)
  92.911 +{
  92.912 +#ifdef PNG_USE_LOCAL_ARRAYS
  92.913 +   PNG_sBIT;
  92.914 +#endif
  92.915 +   png_byte buf[4];
  92.916 +   png_size_t size;
  92.917 +
  92.918 +   png_debug(1, "in png_write_sBIT\n");
  92.919 +   /* make sure we don't depend upon the order of PNG_COLOR_8 */
  92.920 +   if (color_type & PNG_COLOR_MASK_COLOR)
  92.921 +   {
  92.922 +      png_byte maxbits;
  92.923 +
  92.924 +      maxbits = (png_byte)(color_type==PNG_COLOR_TYPE_PALETTE ? 8 :
  92.925 +                png_ptr->usr_bit_depth);
  92.926 +      if (sbit->red == 0 || sbit->red > maxbits ||
  92.927 +          sbit->green == 0 || sbit->green > maxbits ||
  92.928 +          sbit->blue == 0 || sbit->blue > maxbits)
  92.929 +      {
  92.930 +         png_warning(png_ptr, "Invalid sBIT depth specified");
  92.931 +         return;
  92.932 +      }
  92.933 +      buf[0] = sbit->red;
  92.934 +      buf[1] = sbit->green;
  92.935 +      buf[2] = sbit->blue;
  92.936 +      size = 3;
  92.937 +   }
  92.938 +   else
  92.939 +   {
  92.940 +      if (sbit->gray == 0 || sbit->gray > png_ptr->usr_bit_depth)
  92.941 +      {
  92.942 +         png_warning(png_ptr, "Invalid sBIT depth specified");
  92.943 +         return;
  92.944 +      }
  92.945 +      buf[0] = sbit->gray;
  92.946 +      size = 1;
  92.947 +   }
  92.948 +
  92.949 +   if (color_type & PNG_COLOR_MASK_ALPHA)
  92.950 +   {
  92.951 +      if (sbit->alpha == 0 || sbit->alpha > png_ptr->usr_bit_depth)
  92.952 +      {
  92.953 +         png_warning(png_ptr, "Invalid sBIT depth specified");
  92.954 +         return;
  92.955 +      }
  92.956 +      buf[size++] = sbit->alpha;
  92.957 +   }
  92.958 +
  92.959 +   png_write_chunk(png_ptr, (png_bytep)png_sBIT, buf, size);
  92.960 +}
  92.961 +#endif
  92.962 +
  92.963 +#if defined(PNG_WRITE_cHRM_SUPPORTED)
  92.964 +/* write the cHRM chunk */
  92.965 +#ifdef PNG_FLOATING_POINT_SUPPORTED
  92.966 +void /* PRIVATE */
  92.967 +png_write_cHRM(png_structp png_ptr, double white_x, double white_y,
  92.968 +   double red_x, double red_y, double green_x, double green_y,
  92.969 +   double blue_x, double blue_y)
  92.970 +{
  92.971 +#ifdef PNG_USE_LOCAL_ARRAYS
  92.972 +   PNG_cHRM;
  92.973 +#endif
  92.974 +   png_byte buf[32];
  92.975 +   png_uint_32 itemp;
  92.976 +
  92.977 +   png_debug(1, "in png_write_cHRM\n");
  92.978 +   /* each value is saved in 1/100,000ths */
  92.979 +   if (white_x < 0 || white_x > 0.8 || white_y < 0 || white_y > 0.8 ||
  92.980 +       white_x + white_y > 1.0)
  92.981 +   {
  92.982 +      png_warning(png_ptr, "Invalid cHRM white point specified");
  92.983 +#if !defined(PNG_NO_CONSOLE_IO)
  92.984 +      fprintf(stderr, "white_x=%f, white_y=%f\n", white_x, white_y);
  92.985 +#endif
  92.986 +      return;
  92.987 +   }
  92.988 +   itemp = (png_uint_32)(white_x * 100000.0 + 0.5);
  92.989 +   png_save_uint_32(buf, itemp);
  92.990 +   itemp = (png_uint_32)(white_y * 100000.0 + 0.5);
  92.991 +   png_save_uint_32(buf + 4, itemp);
  92.992 +
  92.993 +   if (red_x < 0 ||  red_y < 0 || red_x + red_y > 1.0)
  92.994 +   {
  92.995 +      png_warning(png_ptr, "Invalid cHRM red point specified");
  92.996 +      return;
  92.997 +   }
  92.998 +   itemp = (png_uint_32)(red_x * 100000.0 + 0.5);
  92.999 +   png_save_uint_32(buf + 8, itemp);
 92.1000 +   itemp = (png_uint_32)(red_y * 100000.0 + 0.5);
 92.1001 +   png_save_uint_32(buf + 12, itemp);
 92.1002 +
 92.1003 +   if (green_x < 0 || green_y < 0 || green_x + green_y > 1.0)
 92.1004 +   {
 92.1005 +      png_warning(png_ptr, "Invalid cHRM green point specified");
 92.1006 +      return;
 92.1007 +   }
 92.1008 +   itemp = (png_uint_32)(green_x * 100000.0 + 0.5);
 92.1009 +   png_save_uint_32(buf + 16, itemp);
 92.1010 +   itemp = (png_uint_32)(green_y * 100000.0 + 0.5);
 92.1011 +   png_save_uint_32(buf + 20, itemp);
 92.1012 +
 92.1013 +   if (blue_x < 0 || blue_y < 0 || blue_x + blue_y > 1.0)
 92.1014 +   {
 92.1015 +      png_warning(png_ptr, "Invalid cHRM blue point specified");
 92.1016 +      return;
 92.1017 +   }
 92.1018 +   itemp = (png_uint_32)(blue_x * 100000.0 + 0.5);
 92.1019 +   png_save_uint_32(buf + 24, itemp);
 92.1020 +   itemp = (png_uint_32)(blue_y * 100000.0 + 0.5);
 92.1021 +   png_save_uint_32(buf + 28, itemp);
 92.1022 +
 92.1023 +   png_write_chunk(png_ptr, (png_bytep)png_cHRM, buf, (png_size_t)32);
 92.1024 +}
 92.1025 +#endif
 92.1026 +#ifdef PNG_FIXED_POINT_SUPPORTED
 92.1027 +void /* PRIVATE */
 92.1028 +png_write_cHRM_fixed(png_structp png_ptr, png_fixed_point white_x,
 92.1029 +   png_fixed_point white_y, png_fixed_point red_x, png_fixed_point red_y,
 92.1030 +   png_fixed_point green_x, png_fixed_point green_y, png_fixed_point blue_x,
 92.1031 +   png_fixed_point blue_y)
 92.1032 +{
 92.1033 +#ifdef PNG_USE_LOCAL_ARRAYS
 92.1034 +   PNG_cHRM;
 92.1035 +#endif
 92.1036 +   png_byte buf[32];
 92.1037 +
 92.1038 +   png_debug(1, "in png_write_cHRM\n");
 92.1039 +   /* each value is saved in 1/100,000ths */
 92.1040 +   if (white_x > 80000L || white_y > 80000L || white_x + white_y > 100000L)
 92.1041 +   {
 92.1042 +      png_warning(png_ptr, "Invalid fixed cHRM white point specified");
 92.1043 +#if !defined(PNG_NO_CONSOLE_IO)
 92.1044 +      fprintf(stderr, "white_x=%ld, white_y=%ld\n", (unsigned long)white_x,
 92.1045 +        (unsigned long)white_y);
 92.1046 +#endif
 92.1047 +      return;
 92.1048 +   }
 92.1049 +   png_save_uint_32(buf, (png_uint_32)white_x);
 92.1050 +   png_save_uint_32(buf + 4, (png_uint_32)white_y);
 92.1051 +
 92.1052 +   if (red_x + red_y > 100000L)
 92.1053 +   {
 92.1054 +      png_warning(png_ptr, "Invalid cHRM fixed red point specified");
 92.1055 +      return;
 92.1056 +   }
 92.1057 +   png_save_uint_32(buf + 8, (png_uint_32)red_x);
 92.1058 +   png_save_uint_32(buf + 12, (png_uint_32)red_y);
 92.1059 +
 92.1060 +   if (green_x + green_y > 100000L)
 92.1061 +   {
 92.1062 +      png_warning(png_ptr, "Invalid fixed cHRM green point specified");
 92.1063 +      return;
 92.1064 +   }
 92.1065 +   png_save_uint_32(buf + 16, (png_uint_32)green_x);
 92.1066 +   png_save_uint_32(buf + 20, (png_uint_32)green_y);
 92.1067 +
 92.1068 +   if (blue_x + blue_y > 100000L)
 92.1069 +   {
 92.1070 +      png_warning(png_ptr, "Invalid fixed cHRM blue point specified");
 92.1071 +      return;
 92.1072 +   }
 92.1073 +   png_save_uint_32(buf + 24, (png_uint_32)blue_x);
 92.1074 +   png_save_uint_32(buf + 28, (png_uint_32)blue_y);
 92.1075 +
 92.1076 +   png_write_chunk(png_ptr, (png_bytep)png_cHRM, buf, (png_size_t)32);
 92.1077 +}
 92.1078 +#endif
 92.1079 +#endif
 92.1080 +
 92.1081 +#if defined(PNG_WRITE_tRNS_SUPPORTED)
 92.1082 +/* write the tRNS chunk */
 92.1083 +void /* PRIVATE */
 92.1084 +png_write_tRNS(png_structp png_ptr, png_bytep trans, png_color_16p tran,
 92.1085 +   int num_trans, int color_type)
 92.1086 +{
 92.1087 +#ifdef PNG_USE_LOCAL_ARRAYS
 92.1088 +   PNG_tRNS;
 92.1089 +#endif
 92.1090 +   png_byte buf[6];
 92.1091 +
 92.1092 +   png_debug(1, "in png_write_tRNS\n");
 92.1093 +   if (color_type == PNG_COLOR_TYPE_PALETTE)
 92.1094 +   {
 92.1095 +      if (num_trans <= 0 || num_trans > (int)png_ptr->num_palette)
 92.1096 +      {
 92.1097 +         png_warning(png_ptr, "Invalid number of transparent colors specified");
 92.1098 +         return;
 92.1099 +      }
 92.1100 +      /* write the chunk out as it is */
 92.1101 +      png_write_chunk(png_ptr, (png_bytep)png_tRNS, trans,
 92.1102 +        (png_size_t)num_trans);
 92.1103 +   }
 92.1104 +   else if (color_type == PNG_COLOR_TYPE_GRAY)
 92.1105 +   {
 92.1106 +      /* one 16 bit value */
 92.1107 +      if (tran->gray >= (1 << png_ptr->bit_depth))
 92.1108 +      {
 92.1109 +         png_warning(png_ptr,
 92.1110 +           "Ignoring attempt to write tRNS chunk out-of-range for bit_depth");
 92.1111 +         return;
 92.1112 +      }
 92.1113 +      png_save_uint_16(buf, tran->gray);
 92.1114 +      png_write_chunk(png_ptr, (png_bytep)png_tRNS, buf, (png_size_t)2);
 92.1115 +   }
 92.1116 +   else if (color_type == PNG_COLOR_TYPE_RGB)
 92.1117 +   {
 92.1118 +      /* three 16 bit values */
 92.1119 +      png_save_uint_16(buf, tran->red);
 92.1120 +      png_save_uint_16(buf + 2, tran->green);
 92.1121 +      png_save_uint_16(buf + 4, tran->blue);
 92.1122 +      if (png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4]))
 92.1123 +      {
 92.1124 +         png_warning(png_ptr,
 92.1125 +           "Ignoring attempt to write 16-bit tRNS chunk when bit_depth is 8");
 92.1126 +         return;
 92.1127 +      }
 92.1128 +      png_write_chunk(png_ptr, (png_bytep)png_tRNS, buf, (png_size_t)6);
 92.1129 +   }
 92.1130 +   else
 92.1131 +   {
 92.1132 +      png_warning(png_ptr, "Can't write tRNS with an alpha channel");
 92.1133 +   }
 92.1134 +}
 92.1135 +#endif
 92.1136 +
 92.1137 +#if defined(PNG_WRITE_bKGD_SUPPORTED)
 92.1138 +/* write the background chunk */
 92.1139 +void /* PRIVATE */
 92.1140 +png_write_bKGD(png_structp png_ptr, png_color_16p back, int color_type)
 92.1141 +{
 92.1142 +#ifdef PNG_USE_LOCAL_ARRAYS
 92.1143 +   PNG_bKGD;
 92.1144 +#endif
 92.1145 +   png_byte buf[6];
 92.1146 +
 92.1147 +   png_debug(1, "in png_write_bKGD\n");
 92.1148 +   if (color_type == PNG_COLOR_TYPE_PALETTE)
 92.1149 +   {
 92.1150 +      if (
 92.1151 +#if defined(PNG_MNG_FEATURES_SUPPORTED)
 92.1152 +          (png_ptr->num_palette ||
 92.1153 +          (!(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE))) &&
 92.1154 +#endif
 92.1155 +         back->index > png_ptr->num_palette)
 92.1156 +      {
 92.1157 +         png_warning(png_ptr, "Invalid background palette index");
 92.1158 +         return;
 92.1159 +      }
 92.1160 +      buf[0] = back->index;
 92.1161 +      png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)1);
 92.1162 +   }
 92.1163 +   else if (color_type & PNG_COLOR_MASK_COLOR)
 92.1164 +   {
 92.1165 +      png_save_uint_16(buf, back->red);
 92.1166 +      png_save_uint_16(buf + 2, back->green);
 92.1167 +      png_save_uint_16(buf + 4, back->blue);
 92.1168 +      if (png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4]))
 92.1169 +      {
 92.1170 +         png_warning(png_ptr,
 92.1171 +           "Ignoring attempt to write 16-bit bKGD chunk when bit_depth is 8");
 92.1172 +         return;
 92.1173 +      }
 92.1174 +      png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)6);
 92.1175 +   }
 92.1176 +   else
 92.1177 +   {
 92.1178 +      if (back->gray >= (1 << png_ptr->bit_depth))
 92.1179 +      {
 92.1180 +         png_warning(png_ptr,
 92.1181 +           "Ignoring attempt to write bKGD chunk out-of-range for bit_depth");
 92.1182 +         return;
 92.1183 +      }
 92.1184 +      png_save_uint_16(buf, back->gray);
 92.1185 +      png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)2);
 92.1186 +   }
 92.1187 +}
 92.1188 +#endif
 92.1189 +
 92.1190 +#if defined(PNG_WRITE_hIST_SUPPORTED)
 92.1191 +/* write the histogram */
 92.1192 +void /* PRIVATE */
 92.1193 +png_write_hIST(png_structp png_ptr, png_uint_16p hist, int num_hist)
 92.1194 +{
 92.1195 +#ifdef PNG_USE_LOCAL_ARRAYS
 92.1196 +   PNG_hIST;
 92.1197 +#endif
 92.1198 +   int i;
 92.1199 +   png_byte buf[3];
 92.1200 +
 92.1201 +   png_debug(1, "in png_write_hIST\n");
 92.1202 +   if (num_hist > (int)png_ptr->num_palette)
 92.1203 +   {
 92.1204 +      png_debug2(3, "num_hist = %d, num_palette = %d\n", num_hist,
 92.1205 +         png_ptr->num_palette);
 92.1206 +      png_warning(png_ptr, "Invalid number of histogram entries specified");
 92.1207 +      return;
 92.1208 +   }
 92.1209 +
 92.1210 +   png_write_chunk_start(png_ptr, (png_bytep)png_hIST,
 92.1211 +     (png_uint_32)(num_hist * 2));
 92.1212 +   for (i = 0; i < num_hist; i++)
 92.1213 +   {
 92.1214 +      png_save_uint_16(buf, hist[i]);
 92.1215 +      png_write_chunk_data(png_ptr, buf, (png_size_t)2);
 92.1216 +   }
 92.1217 +   png_write_chunk_end(png_ptr);
 92.1218 +}
 92.1219 +#endif
 92.1220 +
 92.1221 +#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_pCAL_SUPPORTED) || \
 92.1222 +    defined(PNG_WRITE_iCCP_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
 92.1223 +/* Check that the tEXt or zTXt keyword is valid per PNG 1.0 specification,
 92.1224 + * and if invalid, correct the keyword rather than discarding the entire
 92.1225 + * chunk.  The PNG 1.0 specification requires keywords 1-79 characters in
 92.1226 + * length, forbids leading or trailing whitespace, multiple internal spaces,
 92.1227 + * and the non-break space (0x80) from ISO 8859-1.  Returns keyword length.
 92.1228 + *
 92.1229 + * The new_key is allocated to hold the corrected keyword and must be freed
 92.1230 + * by the calling routine.  This avoids problems with trying to write to
 92.1231 + * static keywords without having to have duplicate copies of the strings.
 92.1232 + */
 92.1233 +png_size_t /* PRIVATE */
 92.1234 +png_check_keyword(png_structp png_ptr, png_charp key, png_charpp new_key)
 92.1235 +{
 92.1236 +   png_size_t key_len;
 92.1237 +   png_charp kp, dp;
 92.1238 +   int kflag;
 92.1239 +   int kwarn=0;
 92.1240 +
 92.1241 +   png_debug(1, "in png_check_keyword\n");
 92.1242 +   *new_key = NULL;
 92.1243 +
 92.1244 +   if (key == NULL || (key_len = png_strlen(key)) == 0)
 92.1245 +   {
 92.1246 +      png_warning(png_ptr, "zero length keyword");
 92.1247 +      return ((png_size_t)0);
 92.1248 +   }
 92.1249 +
 92.1250 +   png_debug1(2, "Keyword to be checked is '%s'\n", key);
 92.1251 +
 92.1252 +   *new_key = (png_charp)png_malloc_warn(png_ptr, (png_uint_32)(key_len + 2));
 92.1253 +   if (*new_key == NULL)
 92.1254 +   {
 92.1255 +      png_warning(png_ptr, "Out of memory while procesing keyword");
 92.1256 +      return ((png_size_t)0);
 92.1257 +   }
 92.1258 +
 92.1259 +   /* Replace non-printing characters with a blank and print a warning */
 92.1260 +   for (kp = key, dp = *new_key; *kp != '\0'; kp++, dp++)
 92.1261 +   {
 92.1262 +      if ((png_byte)*kp < 0x20 ||
 92.1263 +         ((png_byte)*kp > 0x7E && (png_byte)*kp < 0xA1))
 92.1264 +      {
 92.1265 +#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
 92.1266 +         char msg[40];
 92.1267 +
 92.1268 +         png_snprintf(msg, 40,
 92.1269 +           "invalid keyword character 0x%02X", (png_byte)*kp);
 92.1270 +         png_warning(png_ptr, msg);
 92.1271 +#else
 92.1272 +         png_warning(png_ptr, "invalid character in keyword");
 92.1273 +#endif
 92.1274 +         *dp = ' ';
 92.1275 +      }
 92.1276 +      else
 92.1277 +      {
 92.1278 +         *dp = *kp;
 92.1279 +      }
 92.1280 +   }
 92.1281 +   *dp = '\0';
 92.1282 +
 92.1283 +   /* Remove any trailing white space. */
 92.1284 +   kp = *new_key + key_len - 1;
 92.1285 +   if (*kp == ' ')
 92.1286 +   {
 92.1287 +      png_warning(png_ptr, "trailing spaces removed from keyword");
 92.1288 +
 92.1289 +      while (*kp == ' ')
 92.1290 +      {
 92.1291 +        *(kp--) = '\0';
 92.1292 +        key_len--;
 92.1293 +      }
 92.1294 +   }
 92.1295 +
 92.1296 +   /* Remove any leading white space. */
 92.1297 +   kp = *new_key;
 92.1298 +   if (*kp == ' ')
 92.1299 +   {
 92.1300 +      png_warning(png_ptr, "leading spaces removed from keyword");
 92.1301 +
 92.1302 +      while (*kp == ' ')
 92.1303 +      {
 92.1304 +        kp++;
 92.1305 +        key_len--;
 92.1306 +      }
 92.1307 +   }
 92.1308 +
 92.1309 +   png_debug1(2, "Checking for multiple internal spaces in '%s'\n", kp);
 92.1310 +
 92.1311 +   /* Remove multiple internal spaces. */
 92.1312 +   for (kflag = 0, dp = *new_key; *kp != '\0'; kp++)
 92.1313 +   {
 92.1314 +      if (*kp == ' ' && kflag == 0)
 92.1315 +      {
 92.1316 +         *(dp++) = *kp;
 92.1317 +         kflag = 1;
 92.1318 +      }
 92.1319 +      else if (*kp == ' ')
 92.1320 +      {
 92.1321 +         key_len--;
 92.1322 +         kwarn=1;
 92.1323 +      }
 92.1324 +      else
 92.1325 +      {
 92.1326 +         *(dp++) = *kp;
 92.1327 +         kflag = 0;
 92.1328 +      }
 92.1329 +   }
 92.1330 +   *dp = '\0';
 92.1331 +   if (kwarn)
 92.1332 +      png_warning(png_ptr, "extra interior spaces removed from keyword");
 92.1333 +
 92.1334 +   if (key_len == 0)
 92.1335 +   {
 92.1336 +      png_free(png_ptr, *new_key);
 92.1337 +       *new_key=NULL;
 92.1338 +      png_warning(png_ptr, "Zero length keyword");
 92.1339 +   }
 92.1340 +
 92.1341 +   if (key_len > 79)
 92.1342 +   {
 92.1343 +      png_warning(png_ptr, "keyword length must be 1 - 79 characters");
 92.1344 +      new_key[79] = '\0';
 92.1345 +      key_len = 79;
 92.1346 +   }
 92.1347 +
 92.1348 +   return (key_len);
 92.1349 +}
 92.1350 +#endif
 92.1351 +
 92.1352 +#if defined(PNG_WRITE_tEXt_SUPPORTED)
 92.1353 +/* write a tEXt chunk */
 92.1354 +void /* PRIVATE */
 92.1355 +png_write_tEXt(png_structp png_ptr, png_charp key, png_charp text,
 92.1356 +   png_size_t text_len)
 92.1357 +{
 92.1358 +#ifdef PNG_USE_LOCAL_ARRAYS
 92.1359 +   PNG_tEXt;
 92.1360 +#endif
 92.1361 +   png_size_t key_len;
 92.1362 +   png_charp new_key;
 92.1363 +
 92.1364 +   png_debug(1, "in png_write_tEXt\n");
 92.1365 +   if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
 92.1366 +   {
 92.1367 +      png_warning(png_ptr, "Empty keyword in tEXt chunk");
 92.1368 +      return;
 92.1369 +   }
 92.1370 +
 92.1371 +   if (text == NULL || *text == '\0')
 92.1372 +      text_len = 0;
 92.1373 +   else
 92.1374 +      text_len = png_strlen(text);
 92.1375 +
 92.1376 +   /* make sure we include the 0 after the key */
 92.1377 +   png_write_chunk_start(png_ptr, (png_bytep)png_tEXt,
 92.1378 +      (png_uint_32)(key_len + text_len + 1));
 92.1379 +   /*
 92.1380 +    * We leave it to the application to meet PNG-1.0 requirements on the
 92.1381 +    * contents of the text.  PNG-1.0 through PNG-1.2 discourage the use of
 92.1382 +    * any non-Latin-1 characters except for NEWLINE.  ISO PNG will forbid them.
 92.1383 +    * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
 92.1384 +    */
 92.1385 +   png_write_chunk_data(png_ptr, (png_bytep)new_key,
 92.1386 +     (png_size_t)(key_len + 1));
 92.1387 +   if (text_len)
 92.1388 +      png_write_chunk_data(png_ptr, (png_bytep)text, (png_size_t)text_len);
 92.1389 +
 92.1390 +   png_write_chunk_end(png_ptr);
 92.1391 +   png_free(png_ptr, new_key);
 92.1392 +}
 92.1393 +#endif
 92.1394 +
 92.1395 +#if defined(PNG_WRITE_zTXt_SUPPORTED)
 92.1396 +/* write a compressed text chunk */
 92.1397 +void /* PRIVATE */
 92.1398 +png_write_zTXt(png_structp png_ptr, png_charp key, png_charp text,
 92.1399 +   png_size_t text_len, int compression)
 92.1400 +{
 92.1401 +#ifdef PNG_USE_LOCAL_ARRAYS
 92.1402 +   PNG_zTXt;
 92.1403 +#endif
 92.1404 +   png_size_t key_len;
 92.1405 +   char buf[1];
 92.1406 +   png_charp new_key;
 92.1407 +   compression_state comp;
 92.1408 +
 92.1409 +   png_debug(1, "in png_write_zTXt\n");
 92.1410 +
 92.1411 +   comp.num_output_ptr = 0;
 92.1412 +   comp.max_output_ptr = 0;
 92.1413 +   comp.output_ptr = NULL;
 92.1414 +   comp.input = NULL;
 92.1415 +   comp.input_len = 0;
 92.1416 +
 92.1417 +   if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
 92.1418 +   {
 92.1419 +      png_warning(png_ptr, "Empty keyword in zTXt chunk");
 92.1420 +      png_free(png_ptr, new_key);
 92.1421 +      return;
 92.1422 +   }
 92.1423 +
 92.1424 +   if (text == NULL || *text == '\0' || compression==PNG_TEXT_COMPRESSION_NONE)
 92.1425 +   {
 92.1426 +      png_write_tEXt(png_ptr, new_key, text, (png_size_t)0);
 92.1427 +      png_free(png_ptr, new_key);
 92.1428 +      return;
 92.1429 +   }
 92.1430 +
 92.1431 +   text_len = png_strlen(text);
 92.1432 +
 92.1433 +   /* compute the compressed data; do it now for the length */
 92.1434 +   text_len = png_text_compress(png_ptr, text, text_len, compression,
 92.1435 +       &comp);
 92.1436 +
 92.1437 +   /* write start of chunk */
 92.1438 +   png_write_chunk_start(png_ptr, (png_bytep)png_zTXt,
 92.1439 +     (png_uint_32)(key_len+text_len + 2));
 92.1440 +   /* write key */
 92.1441 +   png_write_chunk_data(png_ptr, (png_bytep)new_key,
 92.1442 +     (png_size_t)(key_len + 1));
 92.1443 +   png_free(png_ptr, new_key);
 92.1444 +
 92.1445 +   buf[0] = (png_byte)compression;
 92.1446 +   /* write compression */
 92.1447 +   png_write_chunk_data(png_ptr, (png_bytep)buf, (png_size_t)1);
 92.1448 +   /* write the compressed data */
 92.1449 +   png_write_compressed_data_out(png_ptr, &comp);
 92.1450 +
 92.1451 +   /* close the chunk */
 92.1452 +   png_write_chunk_end(png_ptr);
 92.1453 +}
 92.1454 +#endif
 92.1455 +
 92.1456 +#if defined(PNG_WRITE_iTXt_SUPPORTED)
 92.1457 +/* write an iTXt chunk */
 92.1458 +void /* PRIVATE */
 92.1459 +png_write_iTXt(png_structp png_ptr, int compression, png_charp key,
 92.1460 +    png_charp lang, png_charp lang_key, png_charp text)
 92.1461 +{
 92.1462 +#ifdef PNG_USE_LOCAL_ARRAYS
 92.1463 +   PNG_iTXt;
 92.1464 +#endif
 92.1465 +   png_size_t lang_len, key_len, lang_key_len, text_len;
 92.1466 +   png_charp new_lang, new_key;
 92.1467 +   png_byte cbuf[2];
 92.1468 +   compression_state comp;
 92.1469 +
 92.1470 +   png_debug(1, "in png_write_iTXt\n");
 92.1471 +
 92.1472 +   comp.num_output_ptr = 0;
 92.1473 +   comp.max_output_ptr = 0;
 92.1474 +   comp.output_ptr = NULL;
 92.1475 +   comp.input = NULL;
 92.1476 +
 92.1477 +   if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
 92.1478 +   {
 92.1479 +      png_warning(png_ptr, "Empty keyword in iTXt chunk");
 92.1480 +      return;
 92.1481 +   }
 92.1482 +   if (lang == NULL || (lang_len = png_check_keyword(png_ptr, lang, &new_lang))==0)
 92.1483 +   {
 92.1484 +      png_warning(png_ptr, "Empty language field in iTXt chunk");
 92.1485 +      new_lang = NULL;
 92.1486 +      lang_len = 0;
 92.1487 +   }
 92.1488 +
 92.1489 +   if (lang_key == NULL)
 92.1490 +     lang_key_len = 0;
 92.1491 +   else
 92.1492 +     lang_key_len = png_strlen(lang_key);
 92.1493 +
 92.1494 +   if (text == NULL)
 92.1495 +      text_len = 0;
 92.1496 +   else
 92.1497 +     text_len = png_strlen(text);
 92.1498 +
 92.1499 +   /* compute the compressed data; do it now for the length */
 92.1500 +   text_len = png_text_compress(png_ptr, text, text_len, compression-2,
 92.1501 +      &comp);
 92.1502 +
 92.1503 +
 92.1504 +   /* make sure we include the compression flag, the compression byte,
 92.1505 +    * and the NULs after the key, lang, and lang_key parts */
 92.1506 +
 92.1507 +   png_write_chunk_start(png_ptr, (png_bytep)png_iTXt,
 92.1508 +          (png_uint_32)(
 92.1509 +        5 /* comp byte, comp flag, terminators for key, lang and lang_key */
 92.1510 +        + key_len
 92.1511 +        + lang_len
 92.1512 +        + lang_key_len
 92.1513 +        + text_len));
 92.1514 +
 92.1515 +   /*
 92.1516 +    * We leave it to the application to meet PNG-1.0 requirements on the
 92.1517 +    * contents of the text.  PNG-1.0 through PNG-1.2 discourage the use of
 92.1518 +    * any non-Latin-1 characters except for NEWLINE.  ISO PNG will forbid them.
 92.1519 +    * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
 92.1520 +    */
 92.1521 +   png_write_chunk_data(png_ptr, (png_bytep)new_key,
 92.1522 +     (png_size_t)(key_len + 1));
 92.1523 +
 92.1524 +   /* set the compression flag */
 92.1525 +   if (compression == PNG_ITXT_COMPRESSION_NONE || \
 92.1526 +       compression == PNG_TEXT_COMPRESSION_NONE)
 92.1527 +       cbuf[0] = 0;
 92.1528 +   else /* compression == PNG_ITXT_COMPRESSION_zTXt */
 92.1529 +       cbuf[0] = 1;
 92.1530 +   /* set the compression method */
 92.1531 +   cbuf[1] = 0;
 92.1532 +   png_write_chunk_data(png_ptr, cbuf, (png_size_t)2);
 92.1533 +
 92.1534 +   cbuf[0] = 0;
 92.1535 +   png_write_chunk_data(png_ptr, (new_lang ? (png_bytep)new_lang : cbuf),
 92.1536 +     (png_size_t)(lang_len + 1));
 92.1537 +   png_write_chunk_data(png_ptr, (lang_key ? (png_bytep)lang_key : cbuf),
 92.1538 +     (png_size_t)(lang_key_len + 1));
 92.1539 +   png_write_compressed_data_out(png_ptr, &comp);
 92.1540 +
 92.1541 +   png_write_chunk_end(png_ptr);
 92.1542 +   png_free(png_ptr, new_key);
 92.1543 +   png_free(png_ptr, new_lang);
 92.1544 +}
 92.1545 +#endif
 92.1546 +
 92.1547 +#if defined(PNG_WRITE_oFFs_SUPPORTED)
 92.1548 +/* write the oFFs chunk */
 92.1549 +void /* PRIVATE */
 92.1550 +png_write_oFFs(png_structp png_ptr, png_int_32 x_offset, png_int_32 y_offset,
 92.1551 +   int unit_type)
 92.1552 +{
 92.1553 +#ifdef PNG_USE_LOCAL_ARRAYS
 92.1554 +   PNG_oFFs;
 92.1555 +#endif
 92.1556 +   png_byte buf[9];
 92.1557 +
 92.1558 +   png_debug(1, "in png_write_oFFs\n");
 92.1559 +   if (unit_type >= PNG_OFFSET_LAST)
 92.1560 +      png_warning(png_ptr, "Unrecognized unit type for oFFs chunk");
 92.1561 +
 92.1562 +   png_save_int_32(buf, x_offset);
 92.1563 +   png_save_int_32(buf + 4, y_offset);
 92.1564 +   buf[8] = (png_byte)unit_type;
 92.1565 +
 92.1566 +   png_write_chunk(png_ptr, (png_bytep)png_oFFs, buf, (png_size_t)9);
 92.1567 +}
 92.1568 +#endif
 92.1569 +#if defined(PNG_WRITE_pCAL_SUPPORTED)
 92.1570 +/* write the pCAL chunk (described in the PNG extensions document) */
 92.1571 +void /* PRIVATE */
 92.1572 +png_write_pCAL(png_structp png_ptr, png_charp purpose, png_int_32 X0,
 92.1573 +   png_int_32 X1, int type, int nparams, png_charp units, png_charpp params)
 92.1574 +{
 92.1575 +#ifdef PNG_USE_LOCAL_ARRAYS
 92.1576 +   PNG_pCAL;
 92.1577 +#endif
 92.1578 +   png_size_t purpose_len, units_len, total_len;
 92.1579 +   png_uint_32p params_len;
 92.1580 +   png_byte buf[10];
 92.1581 +   png_charp new_purpose;
 92.1582 +   int i;
 92.1583 +
 92.1584 +   png_debug1(1, "in png_write_pCAL (%d parameters)\n", nparams);
 92.1585 +   if (type >= PNG_EQUATION_LAST)
 92.1586 +      png_warning(png_ptr, "Unrecognized equation type for pCAL chunk");
 92.1587 +
 92.1588 +   purpose_len = png_check_keyword(png_ptr, purpose, &new_purpose) + 1;
 92.1589 +   png_debug1(3, "pCAL purpose length = %d\n", (int)purpose_len);
 92.1590 +   units_len = png_strlen(units) + (nparams == 0 ? 0 : 1);
 92.1591 +   png_debug1(3, "pCAL units length = %d\n", (int)units_len);
 92.1592 +   total_len = purpose_len + units_len + 10;
 92.1593 +
 92.1594 +   params_len = (png_uint_32p)png_malloc(png_ptr,
 92.1595 +      (png_uint_32)(nparams * png_sizeof(png_uint_32)));
 92.1596 +
 92.1597 +   /* Find the length of each parameter, making sure we don't count the
 92.1598 +      null terminator for the last parameter. */
 92.1599 +   for (i = 0; i < nparams; i++)
 92.1600 +   {
 92.1601 +      params_len[i] = png_strlen(params[i]) + (i == nparams - 1 ? 0 : 1);
 92.1602 +      png_debug2(3, "pCAL parameter %d length = %lu\n", i,
 92.1603 +        (unsigned long) params_len[i]);
 92.1604 +      total_len += (png_size_t)params_len[i];
 92.1605 +   }
 92.1606 +
 92.1607 +   png_debug1(3, "pCAL total length = %d\n", (int)total_len);
 92.1608 +   png_write_chunk_start(png_ptr, (png_bytep)png_pCAL, (png_uint_32)total_len);
 92.1609 +   png_write_chunk_data(png_ptr, (png_bytep)new_purpose,
 92.1610 +     (png_size_t)purpose_len);
 92.1611 +   png_save_int_32(buf, X0);
 92.1612 +   png_save_int_32(buf + 4, X1);
 92.1613 +   buf[8] = (png_byte)type;
 92.1614 +   buf[9] = (png_byte)nparams;
 92.1615 +   png_write_chunk_data(png_ptr, buf, (png_size_t)10);
 92.1616 +   png_write_chunk_data(png_ptr, (png_bytep)units, (png_size_t)units_len);
 92.1617 +
 92.1618 +   png_free(png_ptr, new_purpose);
 92.1619 +
 92.1620 +   for (i = 0; i < nparams; i++)
 92.1621 +   {
 92.1622 +      png_write_chunk_data(png_ptr, (png_bytep)params[i],
 92.1623 +         (png_size_t)params_len[i]);
 92.1624 +   }
 92.1625 +
 92.1626 +   png_free(png_ptr, params_len);
 92.1627 +   png_write_chunk_end(png_ptr);
 92.1628 +}
 92.1629 +#endif
 92.1630 +
 92.1631 +#if defined(PNG_WRITE_sCAL_SUPPORTED)
 92.1632 +/* write the sCAL chunk */
 92.1633 +#if defined(PNG_FLOATING_POINT_SUPPORTED) && !defined(PNG_NO_STDIO)
 92.1634 +void /* PRIVATE */
 92.1635 +png_write_sCAL(png_structp png_ptr, int unit, double width, double height)
 92.1636 +{
 92.1637 +#ifdef PNG_USE_LOCAL_ARRAYS
 92.1638 +   PNG_sCAL;
 92.1639 +#endif
 92.1640 +   char buf[64];
 92.1641 +   png_size_t total_len;
 92.1642 +
 92.1643 +   png_debug(1, "in png_write_sCAL\n");
 92.1644 +
 92.1645 +   buf[0] = (char)unit;
 92.1646 +#if defined(_WIN32_WCE)
 92.1647 +/* sprintf() function is not supported on WindowsCE */
 92.1648 +   {
 92.1649 +      wchar_t wc_buf[32];
 92.1650 +      size_t wc_len;
 92.1651 +      swprintf(wc_buf, TEXT("%12.12e"), width);
 92.1652 +      wc_len = wcslen(wc_buf);
 92.1653 +      WideCharToMultiByte(CP_ACP, 0, wc_buf, -1, buf + 1, wc_len, NULL, NULL);
 92.1654 +      total_len = wc_len + 2;
 92.1655 +      swprintf(wc_buf, TEXT("%12.12e"), height);
 92.1656 +      wc_len = wcslen(wc_buf);
 92.1657 +      WideCharToMultiByte(CP_ACP, 0, wc_buf, -1, buf + total_len, wc_len,
 92.1658 +         NULL, NULL);
 92.1659 +      total_len += wc_len;
 92.1660 +   }
 92.1661 +#else
 92.1662 +   png_snprintf(buf + 1, 63, "%12.12e", width);
 92.1663 +   total_len = 1 + png_strlen(buf + 1) + 1;
 92.1664 +   png_snprintf(buf + total_len, 64-total_len, "%12.12e", height);
 92.1665 +   total_len += png_strlen(buf + total_len);
 92.1666 +#endif
 92.1667 +
 92.1668 +   png_debug1(3, "sCAL total length = %u\n", (unsigned int)total_len);
 92.1669 +   png_write_chunk(png_ptr, (png_bytep)png_sCAL, (png_bytep)buf, total_len);
 92.1670 +}
 92.1671 +#else
 92.1672 +#ifdef PNG_FIXED_POINT_SUPPORTED
 92.1673 +void /* PRIVATE */
 92.1674 +png_write_sCAL_s(png_structp png_ptr, int unit, png_charp width,
 92.1675 +   png_charp height)
 92.1676 +{
 92.1677 +#ifdef PNG_USE_LOCAL_ARRAYS
 92.1678 +   PNG_sCAL;
 92.1679 +#endif
 92.1680 +   png_byte buf[64];
 92.1681 +   png_size_t wlen, hlen, total_len;
 92.1682 +
 92.1683 +   png_debug(1, "in png_write_sCAL_s\n");
 92.1684 +
 92.1685 +   wlen = png_strlen(width);
 92.1686 +   hlen = png_strlen(height);
 92.1687 +   total_len = wlen + hlen + 2;
 92.1688 +   if (total_len > 64)
 92.1689 +   {
 92.1690 +      png_warning(png_ptr, "Can't write sCAL (buffer too small)");
 92.1691 +      return;
 92.1692 +   }
 92.1693 +
 92.1694 +   buf[0] = (png_byte)unit;
 92.1695 +   png_memcpy(buf + 1, width, wlen + 1);      /* append the '\0' here */
 92.1696 +   png_memcpy(buf + wlen + 2, height, hlen);  /* do NOT append the '\0' here */
 92.1697 +
 92.1698 +   png_debug1(3, "sCAL total length = %u\n", (unsigned int)total_len);
 92.1699 +   png_write_chunk(png_ptr, (png_bytep)png_sCAL, buf, total_len);
 92.1700 +}
 92.1701 +#endif
 92.1702 +#endif
 92.1703 +#endif
 92.1704 +
 92.1705 +#if defined(PNG_WRITE_pHYs_SUPPORTED)
 92.1706 +/* write the pHYs chunk */
 92.1707 +void /* PRIVATE */
 92.1708 +png_write_pHYs(png_structp png_ptr, png_uint_32 x_pixels_per_unit,
 92.1709 +   png_uint_32 y_pixels_per_unit,
 92.1710 +   int unit_type)
 92.1711 +{
 92.1712 +#ifdef PNG_USE_LOCAL_ARRAYS
 92.1713 +   PNG_pHYs;
 92.1714 +#endif
 92.1715 +   png_byte buf[9];
 92.1716 +
 92.1717 +   png_debug(1, "in png_write_pHYs\n");
 92.1718 +   if (unit_type >= PNG_RESOLUTION_LAST)
 92.1719 +      png_warning(png_ptr, "Unrecognized unit type for pHYs chunk");
 92.1720 +
 92.1721 +   png_save_uint_32(buf, x_pixels_per_unit);
 92.1722 +   png_save_uint_32(buf + 4, y_pixels_per_unit);
 92.1723 +   buf[8] = (png_byte)unit_type;
 92.1724 +
 92.1725 +   png_write_chunk(png_ptr, (png_bytep)png_pHYs, buf, (png_size_t)9);
 92.1726 +}
 92.1727 +#endif
 92.1728 +
 92.1729 +#if defined(PNG_WRITE_tIME_SUPPORTED)
 92.1730 +/* Write the tIME chunk.  Use either png_convert_from_struct_tm()
 92.1731 + * or png_convert_from_time_t(), or fill in the structure yourself.
 92.1732 + */
 92.1733 +void /* PRIVATE */
 92.1734 +png_write_tIME(png_structp png_ptr, png_timep mod_time)
 92.1735 +{
 92.1736 +#ifdef PNG_USE_LOCAL_ARRAYS
 92.1737 +   PNG_tIME;
 92.1738 +#endif
 92.1739 +   png_byte buf[7];
 92.1740 +
 92.1741 +   png_debug(1, "in png_write_tIME\n");
 92.1742 +   if (mod_time->month  > 12 || mod_time->month  < 1 ||
 92.1743 +       mod_time->day    > 31 || mod_time->day    < 1 ||
 92.1744 +       mod_time->hour   > 23 || mod_time->second > 60)
 92.1745 +   {
 92.1746 +      png_warning(png_ptr, "Invalid time specified for tIME chunk");
 92.1747 +      return;
 92.1748 +   }
 92.1749 +
 92.1750 +   png_save_uint_16(buf, mod_time->year);
 92.1751 +   buf[2] = mod_time->month;
 92.1752 +   buf[3] = mod_time->day;
 92.1753 +   buf[4] = mod_time->hour;
 92.1754 +   buf[5] = mod_time->minute;
 92.1755 +   buf[6] = mod_time->second;
 92.1756 +
 92.1757 +   png_write_chunk(png_ptr, (png_bytep)png_tIME, buf, (png_size_t)7);
 92.1758 +}
 92.1759 +#endif
 92.1760 +
 92.1761 +/* initializes the row writing capability of libpng */
 92.1762 +void /* PRIVATE */
 92.1763 +png_write_start_row(png_structp png_ptr)
 92.1764 +{
 92.1765 +#ifdef PNG_WRITE_INTERLACING_SUPPORTED
 92.1766 +#ifdef PNG_USE_LOCAL_ARRAYS
 92.1767 +   /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
 92.1768 +
 92.1769 +   /* start of interlace block */
 92.1770 +   int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
 92.1771 +
 92.1772 +   /* offset to next interlace block */
 92.1773 +   int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
 92.1774 +
 92.1775 +   /* start of interlace block in the y direction */
 92.1776 +   int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
 92.1777 +
 92.1778 +   /* offset to next interlace block in the y direction */
 92.1779 +   int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
 92.1780 +#endif
 92.1781 +#endif
 92.1782 +
 92.1783 +   png_size_t buf_size;
 92.1784 +
 92.1785 +   png_debug(1, "in png_write_start_row\n");
 92.1786 +   buf_size = (png_size_t)(PNG_ROWBYTES(
 92.1787 +      png_ptr->usr_channels*png_ptr->usr_bit_depth, png_ptr->width) + 1);
 92.1788 +
 92.1789 +   /* set up row buffer */
 92.1790 +   png_ptr->row_buf = (png_bytep)png_malloc(png_ptr,
 92.1791 +     (png_uint_32)buf_size);
 92.1792 +   png_ptr->row_buf[0] = PNG_FILTER_VALUE_NONE;
 92.1793 +
 92.1794 +#ifndef PNG_NO_WRITE_FILTER
 92.1795 +   /* set up filtering buffer, if using this filter */
 92.1796 +   if (png_ptr->do_filter & PNG_FILTER_SUB)
 92.1797 +   {
 92.1798 +      png_ptr->sub_row = (png_bytep)png_malloc(png_ptr,
 92.1799 +         (png_uint_32)(png_ptr->rowbytes + 1));
 92.1800 +      png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
 92.1801 +   }
 92.1802 +
 92.1803 +   /* We only need to keep the previous row if we are using one of these. */
 92.1804 +   if (png_ptr->do_filter & (PNG_FILTER_AVG | PNG_FILTER_UP | PNG_FILTER_PAETH))
 92.1805 +   {
 92.1806 +     /* set up previous row buffer */
 92.1807 +      png_ptr->prev_row = (png_bytep)png_malloc(png_ptr,
 92.1808 +        (png_uint_32)buf_size);
 92.1809 +      png_memset(png_ptr->prev_row, 0, buf_size);
 92.1810 +
 92.1811 +      if (png_ptr->do_filter & PNG_FILTER_UP)
 92.1812 +      {
 92.1813 +         png_ptr->up_row = (png_bytep)png_malloc(png_ptr,
 92.1814 +           (png_uint_32)(png_ptr->rowbytes + 1));
 92.1815 +         png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
 92.1816 +      }
 92.1817 +
 92.1818 +      if (png_ptr->do_filter & PNG_FILTER_AVG)
 92.1819 +      {
 92.1820 +         png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
 92.1821 +           (png_uint_32)(png_ptr->rowbytes + 1));
 92.1822 +         png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
 92.1823 +      }
 92.1824 +
 92.1825 +      if (png_ptr->do_filter & PNG_FILTER_PAETH)
 92.1826 +      {
 92.1827 +         png_ptr->paeth_row = (png_bytep)png_malloc(png_ptr,
 92.1828 +           (png_uint_32)(png_ptr->rowbytes + 1));
 92.1829 +         png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
 92.1830 +      }
 92.1831 +   }
 92.1832 +#endif /* PNG_NO_WRITE_FILTER */
 92.1833 +
 92.1834 +#ifdef PNG_WRITE_INTERLACING_SUPPORTED
 92.1835 +   /* if interlaced, we need to set up width and height of pass */
 92.1836 +   if (png_ptr->interlaced)
 92.1837 +   {
 92.1838 +      if (!(png_ptr->transformations & PNG_INTERLACE))
 92.1839 +      {
 92.1840 +         png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
 92.1841 +            png_pass_ystart[0]) / png_pass_yinc[0];
 92.1842 +         png_ptr->usr_width = (png_ptr->width + png_pass_inc[0] - 1 -
 92.1843 +            png_pass_start[0]) / png_pass_inc[0];
 92.1844 +      }
 92.1845 +      else
 92.1846 +      {
 92.1847 +         png_ptr->num_rows = png_ptr->height;
 92.1848 +         png_ptr->usr_width = png_ptr->width;
 92.1849 +      }
 92.1850 +   }
 92.1851 +   else
 92.1852 +#endif
 92.1853 +   {
 92.1854 +      png_ptr->num_rows = png_ptr->height;
 92.1855 +      png_ptr->usr_width = png_ptr->width;
 92.1856 +   }
 92.1857 +   png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
 92.1858 +   png_ptr->zstream.next_out = png_ptr->zbuf;
 92.1859 +}
 92.1860 +
 92.1861 +/* Internal use only.  Called when finished processing a row of data. */
 92.1862 +void /* PRIVATE */
 92.1863 +png_write_finish_row(png_structp png_ptr)
 92.1864 +{
 92.1865 +#ifdef PNG_WRITE_INTERLACING_SUPPORTED
 92.1866 +#ifdef PNG_USE_LOCAL_ARRAYS
 92.1867 +   /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
 92.1868 +
 92.1869 +   /* start of interlace block */
 92.1870 +   int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
 92.1871 +
 92.1872 +   /* offset to next interlace block */
 92.1873 +   int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
 92.1874 +
 92.1875 +   /* start of interlace block in the y direction */
 92.1876 +   int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
 92.1877 +
 92.1878 +   /* offset to next interlace block in the y direction */
 92.1879 +   int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
 92.1880 +#endif
 92.1881 +#endif
 92.1882 +
 92.1883 +   int ret;
 92.1884 +
 92.1885 +   png_debug(1, "in png_write_finish_row\n");
 92.1886 +   /* next row */
 92.1887 +   png_ptr->row_number++;
 92.1888 +
 92.1889 +   /* see if we are done */
 92.1890 +   if (png_ptr->row_number < png_ptr->num_rows)
 92.1891 +      return;
 92.1892 +
 92.1893 +#ifdef PNG_WRITE_INTERLACING_SUPPORTED
 92.1894 +   /* if interlaced, go to next pass */
 92.1895 +   if (png_ptr->interlaced)
 92.1896 +   {
 92.1897 +      png_ptr->row_number = 0;
 92.1898 +      if (png_ptr->transformations & PNG_INTERLACE)
 92.1899 +      {
 92.1900 +         png_ptr->pass++;
 92.1901 +      }
 92.1902 +      else
 92.1903 +      {
 92.1904 +         /* loop until we find a non-zero width or height pass */
 92.1905 +         do
 92.1906 +         {
 92.1907 +            png_ptr->pass++;
 92.1908 +            if (png_ptr->pass >= 7)
 92.1909 +               break;
 92.1910 +            png_ptr->usr_width = (png_ptr->width +
 92.1911 +               png_pass_inc[png_ptr->pass] - 1 -
 92.1912 +               png_pass_start[png_ptr->pass]) /
 92.1913 +               png_pass_inc[png_ptr->pass];
 92.1914 +            png_ptr->num_rows = (png_ptr->height +
 92.1915 +               png_pass_yinc[png_ptr->pass] - 1 -
 92.1916 +               png_pass_ystart[png_ptr->pass]) /
 92.1917 +               png_pass_yinc[png_ptr->pass];
 92.1918 +            if (png_ptr->transformations & PNG_INTERLACE)
 92.1919 +               break;
 92.1920 +         } while (png_ptr->usr_width == 0 || png_ptr->num_rows == 0);
 92.1921 +
 92.1922 +      }
 92.1923 +
 92.1924 +      /* reset the row above the image for the next pass */
 92.1925 +      if (png_ptr->pass < 7)
 92.1926 +      {
 92.1927 +         if (png_ptr->prev_row != NULL)
 92.1928 +            png_memset(png_ptr->prev_row, 0,
 92.1929 +               (png_size_t)(PNG_ROWBYTES(png_ptr->usr_channels*
 92.1930 +               png_ptr->usr_bit_depth, png_ptr->width)) + 1);
 92.1931 +         return;
 92.1932 +      }
 92.1933 +   }
 92.1934 +#endif
 92.1935 +
 92.1936 +   /* if we get here, we've just written the last row, so we need
 92.1937 +      to flush the compressor */
 92.1938 +   do
 92.1939 +   {
 92.1940 +      /* tell the compressor we are done */
 92.1941 +      ret = deflate(&png_ptr->zstream, Z_FINISH);
 92.1942 +      /* check for an error */
 92.1943 +      if (ret == Z_OK)
 92.1944 +      {
 92.1945 +         /* check to see if we need more room */
 92.1946 +         if (!(png_ptr->zstream.avail_out))
 92.1947 +         {
 92.1948 +            png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
 92.1949 +            png_ptr->zstream.next_out = png_ptr->zbuf;
 92.1950 +            png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
 92.1951 +         }
 92.1952 +      }
 92.1953 +      else if (ret != Z_STREAM_END)
 92.1954 +      {
 92.1955 +         if (png_ptr->zstream.msg != NULL)
 92.1956 +            png_error(png_ptr, png_ptr->zstream.msg);
 92.1957 +         else
 92.1958 +            png_error(png_ptr, "zlib error");
 92.1959 +      }
 92.1960 +   } while (ret != Z_STREAM_END);
 92.1961 +
 92.1962 +   /* write any extra space */
 92.1963 +   if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
 92.1964 +   {
 92.1965 +      png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size -
 92.1966 +         png_ptr->zstream.avail_out);
 92.1967 +   }
 92.1968 +
 92.1969 +   deflateReset(&png_ptr->zstream);
 92.1970 +   png_ptr->zstream.data_type = Z_BINARY;
 92.1971 +}
 92.1972 +
 92.1973 +#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
 92.1974 +/* Pick out the correct pixels for the interlace pass.
 92.1975 + * The basic idea here is to go through the row with a source
 92.1976 + * pointer and a destination pointer (sp and dp), and copy the
 92.1977 + * correct pixels for the pass.  As the row gets compacted,
 92.1978 + * sp will always be >= dp, so we should never overwrite anything.
 92.1979 + * See the default: case for the easiest code to understand.
 92.1980 + */
 92.1981 +void /* PRIVATE */
 92.1982 +png_do_write_interlace(png_row_infop row_info, png_bytep row, int pass)
 92.1983 +{
 92.1984 +#ifdef PNG_USE_LOCAL_ARRAYS
 92.1985 +   /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
 92.1986 +
 92.1987 +   /* start of interlace block */
 92.1988 +   int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
 92.1989 +
 92.1990 +   /* offset to next interlace block */
 92.1991 +   int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
 92.1992 +#endif
 92.1993 +
 92.1994 +   png_debug(1, "in png_do_write_interlace\n");
 92.1995 +   /* we don't have to do anything on the last pass (6) */
 92.1996 +#if defined(PNG_USELESS_TESTS_SUPPORTED)
 92.1997 +   if (row != NULL && row_info != NULL && pass < 6)
 92.1998 +#else
 92.1999 +   if (pass < 6)
 92.2000 +#endif
 92.2001 +   {
 92.2002 +      /* each pixel depth is handled separately */
 92.2003 +      switch (row_info->pixel_depth)
 92.2004 +      {
 92.2005 +         case 1:
 92.2006 +         {
 92.2007 +            png_bytep sp;
 92.2008 +            png_bytep dp;
 92.2009 +            int shift;
 92.2010 +            int d;
 92.2011 +            int value;
 92.2012 +            png_uint_32 i;
 92.2013 +            png_uint_32 row_width = row_info->width;
 92.2014 +
 92.2015 +            dp = row;
 92.2016 +            d = 0;
 92.2017 +            shift = 7;
 92.2018 +            for (i = png_pass_start[pass]; i < row_width;
 92.2019 +               i += png_pass_inc[pass])
 92.2020 +            {
 92.2021 +               sp = row + (png_size_t)(i >> 3);
 92.2022 +               value = (int)(*sp >> (7 - (int)(i & 0x07))) & 0x01;
 92.2023 +               d |= (value << shift);
 92.2024 +
 92.2025 +               if (shift == 0)
 92.2026 +               {
 92.2027 +                  shift = 7;
 92.2028 +                  *dp++ = (png_byte)d;
 92.2029 +                  d = 0;
 92.2030 +               }
 92.2031 +               else
 92.2032 +                  shift--;
 92.2033 +
 92.2034 +            }
 92.2035 +            if (shift != 7)
 92.2036 +               *dp = (png_byte)d;
 92.2037 +            break;
 92.2038 +         }
 92.2039 +         case 2:
 92.2040 +         {
 92.2041 +            png_bytep sp;
 92.2042 +            png_bytep dp;
 92.2043 +            int shift;
 92.2044 +            int d;
 92.2045 +            int value;
 92.2046 +            png_uint_32 i;
 92.2047 +            png_uint_32 row_width = row_info->width;
 92.2048 +
 92.2049 +            dp = row;
 92.2050 +            shift = 6;
 92.2051 +            d = 0;
 92.2052 +            for (i = png_pass_start[pass]; i < row_width;
 92.2053 +               i += png_pass_inc[pass])
 92.2054 +            {
 92.2055 +               sp = row + (png_size_t)(i >> 2);
 92.2056 +               value = (*sp >> ((3 - (int)(i & 0x03)) << 1)) & 0x03;
 92.2057 +               d |= (value << shift);
 92.2058 +
 92.2059 +               if (shift == 0)
 92.2060 +               {
 92.2061 +                  shift = 6;
 92.2062 +                  *dp++ = (png_byte)d;
 92.2063 +                  d = 0;
 92.2064 +               }
 92.2065 +               else
 92.2066 +                  shift -= 2;
 92.2067 +            }
 92.2068 +            if (shift != 6)
 92.2069 +                   *dp = (png_byte)d;
 92.2070 +            break;
 92.2071 +         }
 92.2072 +         case 4:
 92.2073 +         {
 92.2074 +            png_bytep sp;
 92.2075 +            png_bytep dp;
 92.2076 +            int shift;
 92.2077 +            int d;
 92.2078 +            int value;
 92.2079 +            png_uint_32 i;
 92.2080 +            png_uint_32 row_width = row_info->width;
 92.2081 +
 92.2082 +            dp = row;
 92.2083 +            shift = 4;
 92.2084 +            d = 0;
 92.2085 +            for (i = png_pass_start[pass]; i < row_width;
 92.2086 +               i += png_pass_inc[pass])
 92.2087 +            {
 92.2088 +               sp = row + (png_size_t)(i >> 1);
 92.2089 +               value = (*sp >> ((1 - (int)(i & 0x01)) << 2)) & 0x0f;
 92.2090 +               d |= (value << shift);
 92.2091 +
 92.2092 +               if (shift == 0)
 92.2093 +               {
 92.2094 +                  shift = 4;
 92.2095 +                  *dp++ = (png_byte)d;
 92.2096 +                  d = 0;
 92.2097 +               }
 92.2098 +               else
 92.2099 +                  shift -= 4;
 92.2100 +            }
 92.2101 +            if (shift != 4)
 92.2102 +               *dp = (png_byte)d;
 92.2103 +            break;
 92.2104 +         }
 92.2105 +         default:
 92.2106 +         {
 92.2107 +            png_bytep sp;
 92.2108 +            png_bytep dp;
 92.2109 +            png_uint_32 i;
 92.2110 +            png_uint_32 row_width = row_info->width;
 92.2111 +            png_size_t pixel_bytes;
 92.2112 +
 92.2113 +            /* start at the beginning */
 92.2114 +            dp = row;
 92.2115 +            /* find out how many bytes each pixel takes up */
 92.2116 +            pixel_bytes = (row_info->pixel_depth >> 3);
 92.2117 +            /* loop through the row, only looking at the pixels that
 92.2118 +               matter */
 92.2119 +            for (i = png_pass_start[pass]; i < row_width;
 92.2120 +               i += png_pass_inc[pass])
 92.2121 +            {
 92.2122 +               /* find out where the original pixel is */
 92.2123 +               sp = row + (png_size_t)i * pixel_bytes;
 92.2124 +               /* move the pixel */
 92.2125 +               if (dp != sp)
 92.2126 +                  png_memcpy(dp, sp, pixel_bytes);
 92.2127 +               /* next pixel */
 92.2128 +               dp += pixel_bytes;
 92.2129 +            }
 92.2130 +            break;
 92.2131 +         }
 92.2132 +      }
 92.2133 +      /* set new row width */
 92.2134 +      row_info->width = (row_info->width +
 92.2135 +         png_pass_inc[pass] - 1 -
 92.2136 +         png_pass_start[pass]) /
 92.2137 +         png_pass_inc[pass];
 92.2138 +         row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,
 92.2139 +            row_info->width);
 92.2140 +   }
 92.2141 +}
 92.2142 +#endif
 92.2143 +
 92.2144 +/* This filters the row, chooses which filter to use, if it has not already
 92.2145 + * been specified by the application, and then writes the row out with the
 92.2146 + * chosen filter.
 92.2147 + */
 92.2148 +#define PNG_MAXSUM (((png_uint_32)(-1)) >> 1)
 92.2149 +#define PNG_HISHIFT 10
 92.2150 +#define PNG_LOMASK ((png_uint_32)0xffffL)
 92.2151 +#define PNG_HIMASK ((png_uint_32)(~PNG_LOMASK >> PNG_HISHIFT))
 92.2152 +void /* PRIVATE */
 92.2153 +png_write_find_filter(png_structp png_ptr, png_row_infop row_info)
 92.2154 +{
 92.2155 +   png_bytep best_row;
 92.2156 +#ifndef PNG_NO_WRITE_FILTER
 92.2157 +   png_bytep prev_row, row_buf;
 92.2158 +   png_uint_32 mins, bpp;
 92.2159 +   png_byte filter_to_do = png_ptr->do_filter;
 92.2160 +   png_uint_32 row_bytes = row_info->rowbytes;
 92.2161 +#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
 92.2162 +   int num_p_filters = (int)png_ptr->num_prev_filters;
 92.2163 +#endif
 92.2164 +
 92.2165 +   png_debug(1, "in png_write_find_filter\n");
 92.2166 +   /* find out how many bytes offset each pixel is */
 92.2167 +   bpp = (row_info->pixel_depth + 7) >> 3;
 92.2168 +
 92.2169 +   prev_row = png_ptr->prev_row;
 92.2170 +#endif
 92.2171 +   best_row = png_ptr->row_buf;
 92.2172 +#ifndef PNG_NO_WRITE_FILTER
 92.2173 +   row_buf = best_row;
 92.2174 +   mins = PNG_MAXSUM;
 92.2175 +
 92.2176 +   /* The prediction method we use is to find which method provides the
 92.2177 +    * smallest value when summing the absolute values of the distances
 92.2178 +    * from zero, using anything >= 128 as negative numbers.  This is known
 92.2179 +    * as the "minimum sum of absolute differences" heuristic.  Other
 92.2180 +    * heuristics are the "weighted minimum sum of absolute differences"
 92.2181 +    * (experimental and can in theory improve compression), and the "zlib
 92.2182 +    * predictive" method (not implemented yet), which does test compressions
 92.2183 +    * of lines using different filter methods, and then chooses the
 92.2184 +    * (series of) filter(s) that give minimum compressed data size (VERY
 92.2185 +    * computationally expensive).
 92.2186 +    *
 92.2187 +    * GRR 980525:  consider also
 92.2188 +    *   (1) minimum sum of absolute differences from running average (i.e.,
 92.2189 +    *       keep running sum of non-absolute differences & count of bytes)
 92.2190 +    *       [track dispersion, too?  restart average if dispersion too large?]
 92.2191 +    *  (1b) minimum sum of absolute differences from sliding average, probably
 92.2192 +    *       with window size <= deflate window (usually 32K)
 92.2193 +    *   (2) minimum sum of squared differences from zero or running average
 92.2194 +    *       (i.e., ~ root-mean-square approach)
 92.2195 +    */
 92.2196 +
 92.2197 +
 92.2198 +   /* We don't need to test the 'no filter' case if this is the only filter
 92.2199 +    * that has been chosen, as it doesn't actually do anything to the data.
 92.2200 +    */
 92.2201 +   if ((filter_to_do & PNG_FILTER_NONE) &&
 92.2202 +       filter_to_do != PNG_FILTER_NONE)
 92.2203 +   {
 92.2204 +      png_bytep rp;
 92.2205 +      png_uint_32 sum = 0;
 92.2206 +      png_uint_32 i;
 92.2207 +      int v;
 92.2208 +
 92.2209 +      for (i = 0, rp = row_buf + 1; i < row_bytes; i++, rp++)
 92.2210 +      {
 92.2211 +         v = *rp;
 92.2212 +         sum += (v < 128) ? v : 256 - v;
 92.2213 +      }
 92.2214 +
 92.2215 +#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
 92.2216 +      if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
 92.2217 +      {
 92.2218 +         png_uint_32 sumhi, sumlo;
 92.2219 +         int j;
 92.2220 +         sumlo = sum & PNG_LOMASK;
 92.2221 +         sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; /* Gives us some footroom */
 92.2222 +
 92.2223 +         /* Reduce the sum if we match any of the previous rows */
 92.2224 +         for (j = 0; j < num_p_filters; j++)
 92.2225 +         {
 92.2226 +            if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
 92.2227 +            {
 92.2228 +               sumlo = (sumlo * png_ptr->filter_weights[j]) >>
 92.2229 +                  PNG_WEIGHT_SHIFT;
 92.2230 +               sumhi = (sumhi * png_ptr->filter_weights[j]) >>
 92.2231 +                  PNG_WEIGHT_SHIFT;
 92.2232 +            }
 92.2233 +         }
 92.2234 +
 92.2235 +         /* Factor in the cost of this filter (this is here for completeness,
 92.2236 +          * but it makes no sense to have a "cost" for the NONE filter, as
 92.2237 +          * it has the minimum possible computational cost - none).
 92.2238 +          */
 92.2239 +         sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
 92.2240 +            PNG_COST_SHIFT;
 92.2241 +         sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
 92.2242 +            PNG_COST_SHIFT;
 92.2243 +
 92.2244 +         if (sumhi > PNG_HIMASK)
 92.2245 +            sum = PNG_MAXSUM;
 92.2246 +         else
 92.2247 +            sum = (sumhi << PNG_HISHIFT) + sumlo;
 92.2248 +      }
 92.2249 +#endif
 92.2250 +      mins = sum;
 92.2251 +   }
 92.2252 +
 92.2253 +   /* sub filter */
 92.2254 +   if (filter_to_do == PNG_FILTER_SUB)
 92.2255 +   /* it's the only filter so no testing is needed */
 92.2256 +   {
 92.2257 +      png_bytep rp, lp, dp;
 92.2258 +      png_uint_32 i;
 92.2259 +      for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
 92.2260 +           i++, rp++, dp++)
 92.2261 +      {
 92.2262 +         *dp = *rp;
 92.2263 +      }
 92.2264 +      for (lp = row_buf + 1; i < row_bytes;
 92.2265 +         i++, rp++, lp++, dp++)
 92.2266 +      {
 92.2267 +         *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
 92.2268 +      }
 92.2269 +      best_row = png_ptr->sub_row;
 92.2270 +   }
 92.2271 +
 92.2272 +   else if (filter_to_do & PNG_FILTER_SUB)
 92.2273 +   {
 92.2274 +      png_bytep rp, dp, lp;
 92.2275 +      png_uint_32 sum = 0, lmins = mins;
 92.2276 +      png_uint_32 i;
 92.2277 +      int v;
 92.2278 +
 92.2279 +#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
 92.2280 +      /* We temporarily increase the "minimum sum" by the factor we
 92.2281 +       * would reduce the sum of this filter, so that we can do the
 92.2282 +       * early exit comparison without scaling the sum each time.
 92.2283 +       */
 92.2284 +      if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
 92.2285 +      {
 92.2286 +         int j;
 92.2287 +         png_uint_32 lmhi, lmlo;
 92.2288 +         lmlo = lmins & PNG_LOMASK;
 92.2289 +         lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
 92.2290 +
 92.2291 +         for (j = 0; j < num_p_filters; j++)
 92.2292 +         {
 92.2293 +            if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
 92.2294 +            {
 92.2295 +               lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
 92.2296 +                  PNG_WEIGHT_SHIFT;
 92.2297 +               lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
 92.2298 +                  PNG_WEIGHT_SHIFT;
 92.2299 +            }
 92.2300 +         }
 92.2301 +
 92.2302 +         lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
 92.2303 +            PNG_COST_SHIFT;
 92.2304 +         lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
 92.2305 +            PNG_COST_SHIFT;
 92.2306 +
 92.2307 +         if (lmhi > PNG_HIMASK)
 92.2308 +            lmins = PNG_MAXSUM;
 92.2309 +         else
 92.2310 +            lmins = (lmhi << PNG_HISHIFT) + lmlo;
 92.2311 +      }
 92.2312 +#endif
 92.2313 +
 92.2314 +      for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
 92.2315 +           i++, rp++, dp++)
 92.2316 +      {
 92.2317 +         v = *dp = *rp;
 92.2318 +
 92.2319 +         sum += (v < 128) ? v : 256 - v;
 92.2320 +      }
 92.2321 +      for (lp = row_buf + 1; i < row_bytes;
 92.2322 +         i++, rp++, lp++, dp++)
 92.2323 +      {
 92.2324 +         v = *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
 92.2325 +
 92.2326 +         sum += (v < 128) ? v : 256 - v;
 92.2327 +
 92.2328 +         if (sum > lmins)  /* We are already worse, don't continue. */
 92.2329 +            break;
 92.2330 +      }
 92.2331 +
 92.2332 +#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
 92.2333 +      if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
 92.2334 +      {
 92.2335 +         int j;
 92.2336 +         png_uint_32 sumhi, sumlo;
 92.2337 +         sumlo = sum & PNG_LOMASK;
 92.2338 +         sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
 92.2339 +
 92.2340 +         for (j = 0; j < num_p_filters; j++)
 92.2341 +         {
 92.2342 +            if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
 92.2343 +            {
 92.2344 +               sumlo = (sumlo * png_ptr->inv_filter_weights[j]) >>
 92.2345 +                  PNG_WEIGHT_SHIFT;
 92.2346 +               sumhi = (sumhi * png_ptr->inv_filter_weights[j]) >>
 92.2347 +                  PNG_WEIGHT_SHIFT;
 92.2348 +            }
 92.2349 +         }
 92.2350 +
 92.2351 +         sumlo = (sumlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
 92.2352 +            PNG_COST_SHIFT;
 92.2353 +         sumhi = (sumhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
 92.2354 +            PNG_COST_SHIFT;
 92.2355 +
 92.2356 +         if (sumhi > PNG_HIMASK)
 92.2357 +            sum = PNG_MAXSUM;
 92.2358 +         else
 92.2359 +            sum = (sumhi << PNG_HISHIFT) + sumlo;
 92.2360 +      }
 92.2361 +#endif
 92.2362 +
 92.2363 +      if (sum < mins)
 92.2364 +      {
 92.2365 +         mins = sum;
 92.2366 +         best_row = png_ptr->sub_row;
 92.2367 +      }
 92.2368 +   }
 92.2369 +
 92.2370 +   /* up filter */
 92.2371 +   if (filter_to_do == PNG_FILTER_UP)
 92.2372 +   {
 92.2373 +      png_bytep rp, dp, pp;
 92.2374 +      png_uint_32 i;
 92.2375 +
 92.2376 +      for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
 92.2377 +           pp = prev_row + 1; i < row_bytes;
 92.2378 +           i++, rp++, pp++, dp++)
 92.2379 +      {
 92.2380 +         *dp = (png_byte)(((int)*rp - (int)*pp) & 0xff);
 92.2381 +      }
 92.2382 +      best_row = png_ptr->up_row;
 92.2383 +   }
 92.2384 +
 92.2385 +   else if (filter_to_do & PNG_FILTER_UP)
 92.2386 +   {
 92.2387 +      png_bytep rp, dp, pp;
 92.2388 +      png_uint_32 sum = 0, lmins = mins;
 92.2389 +      png_uint_32 i;
 92.2390 +      int v;
 92.2391 +
 92.2392 +
 92.2393 +#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
 92.2394 +      if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
 92.2395 +      {
 92.2396 +         int j;
 92.2397 +         png_uint_32 lmhi, lmlo;
 92.2398 +         lmlo = lmins & PNG_LOMASK;
 92.2399 +         lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
 92.2400 +
 92.2401 +         for (j = 0; j < num_p_filters; j++)
 92.2402 +         {
 92.2403 +            if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
 92.2404 +            {
 92.2405 +               lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
 92.2406 +                  PNG_WEIGHT_SHIFT;
 92.2407 +               lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
 92.2408 +                  PNG_WEIGHT_SHIFT;
 92.2409 +            }
 92.2410 +         }
 92.2411 +
 92.2412 +         lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
 92.2413 +            PNG_COST_SHIFT;
 92.2414 +         lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
 92.2415 +            PNG_COST_SHIFT;
 92.2416 +
 92.2417 +         if (lmhi > PNG_HIMASK)
 92.2418 +            lmins = PNG_MAXSUM;
 92.2419 +         else
 92.2420 +            lmins = (lmhi << PNG_HISHIFT) + lmlo;
 92.2421 +      }
 92.2422 +#endif
 92.2423 +
 92.2424 +      for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
 92.2425 +           pp = prev_row + 1; i < row_bytes; i++)
 92.2426 +      {
 92.2427 +         v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
 92.2428 +
 92.2429 +         sum += (v < 128) ? v : 256 - v;
 92.2430 +
 92.2431 +         if (sum > lmins)  /* We are already worse, don't continue. */
 92.2432 +            break;
 92.2433 +      }
 92.2434 +
 92.2435 +#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
 92.2436 +      if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
 92.2437 +      {
 92.2438 +         int j;
 92.2439 +         png_uint_32 sumhi, sumlo;
 92.2440 +         sumlo = sum & PNG_LOMASK;
 92.2441 +         sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
 92.2442 +
 92.2443 +         for (j = 0; j < num_p_filters; j++)
 92.2444 +         {
 92.2445 +            if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
 92.2446 +            {
 92.2447 +               sumlo = (sumlo * png_ptr->filter_weights[j]) >>
 92.2448 +                  PNG_WEIGHT_SHIFT;
 92.2449 +               sumhi = (sumhi * png_ptr->filter_weights[j]) >>
 92.2450 +                  PNG_WEIGHT_SHIFT;
 92.2451 +            }
 92.2452 +         }
 92.2453 +
 92.2454 +         sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
 92.2455 +            PNG_COST_SHIFT;
 92.2456 +         sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
 92.2457 +            PNG_COST_SHIFT;
 92.2458 +
 92.2459 +         if (sumhi > PNG_HIMASK)
 92.2460 +            sum = PNG_MAXSUM;
 92.2461 +         else
 92.2462 +            sum = (sumhi << PNG_HISHIFT) + sumlo;
 92.2463 +      }
 92.2464 +#endif
 92.2465 +
 92.2466 +      if (sum < mins)
 92.2467 +      {
 92.2468 +         mins = sum;
 92.2469 +         best_row = png_ptr->up_row;
 92.2470 +      }
 92.2471 +   }
 92.2472 +
 92.2473 +   /* avg filter */
 92.2474 +   if (filter_to_do == PNG_FILTER_AVG)
 92.2475 +   {
 92.2476 +      png_bytep rp, dp, pp, lp;
 92.2477 +      png_uint_32 i;
 92.2478 +      for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
 92.2479 +           pp = prev_row + 1; i < bpp; i++)
 92.2480 +      {
 92.2481 +         *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
 92.2482 +      }
 92.2483 +      for (lp = row_buf + 1; i < row_bytes; i++)
 92.2484 +      {
 92.2485 +         *dp++ = (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2))
 92.2486 +                 & 0xff);
 92.2487 +      }
 92.2488 +      best_row = png_ptr->avg_row;
 92.2489 +   }
 92.2490 +
 92.2491 +   else if (filter_to_do & PNG_FILTER_AVG)
 92.2492 +   {
 92.2493 +      png_bytep rp, dp, pp, lp;
 92.2494 +      png_uint_32 sum = 0, lmins = mins;
 92.2495 +      png_uint_32 i;
 92.2496 +      int v;
 92.2497 +
 92.2498 +#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
 92.2499 +      if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
 92.2500 +      {
 92.2501 +         int j;
 92.2502 +         png_uint_32 lmhi, lmlo;
 92.2503 +         lmlo = lmins & PNG_LOMASK;
 92.2504 +         lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
 92.2505 +
 92.2506 +         for (j = 0; j < num_p_filters; j++)
 92.2507 +         {
 92.2508 +            if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_AVG)
 92.2509 +            {
 92.2510 +               lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
 92.2511 +                  PNG_WEIGHT_SHIFT;
 92.2512 +               lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
 92.2513 +                  PNG_WEIGHT_SHIFT;
 92.2514 +            }
 92.2515 +         }
 92.2516 +
 92.2517 +         lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
 92.2518 +            PNG_COST_SHIFT;
 92.2519 +         lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
 92.2520 +            PNG_COST_SHIFT;
 92.2521 +
 92.2522 +         if (lmhi > PNG_HIMASK)
 92.2523 +            lmins = PNG_MAXSUM;
 92.2524 +         else
 92.2525 +            lmins = (lmhi << PNG_HISHIFT) + lmlo;
 92.2526 +      }
 92.2527 +#endif
 92.2528 +
 92.2529 +      for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
 92.2530 +           pp = prev_row + 1; i < bpp; i++)
 92.2531 +      {
 92.2532 +         v = *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
 92.2533 +
 92.2534 +         sum += (v < 128) ? v : 256 - v;
 92.2535 +      }
 92.2536 +      for (lp = row_buf + 1; i < row_bytes; i++)
 92.2537 +      {
 92.2538 +         v = *dp++ =
 92.2539 +          (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2)) & 0xff);
 92.2540 +
 92.2541 +         sum += (v < 128) ? v : 256 - v;
 92.2542 +
 92.2543 +         if (sum > lmins)  /* We are already worse, don't continue. */
 92.2544 +            break;
 92.2545 +      }
 92.2546 +
 92.2547 +#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
 92.2548 +      if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
 92.2549 +      {
 92.2550 +         int j;
 92.2551 +         png_uint_32 sumhi, sumlo;
 92.2552 +         sumlo = sum & PNG_LOMASK;
 92.2553 +         sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
 92.2554 +
 92.2555 +         for (j = 0; j < num_p_filters; j++)
 92.2556 +         {
 92.2557 +            if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
 92.2558 +            {
 92.2559 +               sumlo = (sumlo * png_ptr->filter_weights[j]) >>
 92.2560 +                  PNG_WEIGHT_SHIFT;
 92.2561 +               sumhi = (sumhi * png_ptr->filter_weights[j]) >>
 92.2562 +                  PNG_WEIGHT_SHIFT;
 92.2563 +            }
 92.2564 +         }
 92.2565 +
 92.2566 +         sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
 92.2567 +            PNG_COST_SHIFT;
 92.2568 +         sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
 92.2569 +            PNG_COST_SHIFT;
 92.2570 +
 92.2571 +         if (sumhi > PNG_HIMASK)
 92.2572 +            sum = PNG_MAXSUM;
 92.2573 +         else
 92.2574 +            sum = (sumhi << PNG_HISHIFT) + sumlo;
 92.2575 +      }
 92.2576 +#endif
 92.2577 +
 92.2578 +      if (sum < mins)
 92.2579 +      {
 92.2580 +         mins = sum;
 92.2581 +         best_row = png_ptr->avg_row;
 92.2582 +      }
 92.2583 +   }
 92.2584 +
 92.2585 +   /* Paeth filter */
 92.2586 +   if (filter_to_do == PNG_FILTER_PAETH)
 92.2587 +   {
 92.2588 +      png_bytep rp, dp, pp, cp, lp;
 92.2589 +      png_uint_32 i;
 92.2590 +      for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
 92.2591 +           pp = prev_row + 1; i < bpp; i++)
 92.2592 +      {
 92.2593 +         *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
 92.2594 +      }
 92.2595 +
 92.2596 +      for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
 92.2597 +      {
 92.2598 +         int a, b, c, pa, pb, pc, p;
 92.2599 +
 92.2600 +         b = *pp++;
 92.2601 +         c = *cp++;
 92.2602 +         a = *lp++;
 92.2603 +
 92.2604 +         p = b - c;
 92.2605 +         pc = a - c;
 92.2606 +
 92.2607 +#ifdef PNG_USE_ABS
 92.2608 +         pa = abs(p);
 92.2609 +         pb = abs(pc);
 92.2610 +         pc = abs(p + pc);
 92.2611 +#else
 92.2612 +         pa = p < 0 ? -p : p;
 92.2613 +         pb = pc < 0 ? -pc : pc;
 92.2614 +         pc = (p + pc) < 0 ? -(p + pc) : p + pc;
 92.2615 +#endif
 92.2616 +
 92.2617 +         p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
 92.2618 +
 92.2619 +         *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
 92.2620 +      }
 92.2621 +      best_row = png_ptr->paeth_row;
 92.2622 +   }
 92.2623 +
 92.2624 +   else if (filter_to_do & PNG_FILTER_PAETH)
 92.2625 +   {
 92.2626 +      png_bytep rp, dp, pp, cp, lp;
 92.2627 +      png_uint_32 sum = 0, lmins = mins;
 92.2628 +      png_uint_32 i;
 92.2629 +      int v;
 92.2630 +
 92.2631 +#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
 92.2632 +      if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
 92.2633 +      {
 92.2634 +         int j;
 92.2635 +         png_uint_32 lmhi, lmlo;
 92.2636 +         lmlo = lmins & PNG_LOMASK;
 92.2637 +         lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
 92.2638 +
 92.2639 +         for (j = 0; j < num_p_filters; j++)
 92.2640 +         {
 92.2641 +            if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
 92.2642 +            {
 92.2643 +               lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
 92.2644 +                  PNG_WEIGHT_SHIFT;
 92.2645 +               lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
 92.2646 +                  PNG_WEIGHT_SHIFT;
 92.2647 +            }
 92.2648 +         }
 92.2649 +
 92.2650 +         lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
 92.2651 +            PNG_COST_SHIFT;
 92.2652 +         lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
 92.2653 +            PNG_COST_SHIFT;
 92.2654 +
 92.2655 +         if (lmhi > PNG_HIMASK)
 92.2656 +            lmins = PNG_MAXSUM;
 92.2657 +         else
 92.2658 +            lmins = (lmhi << PNG_HISHIFT) + lmlo;
 92.2659 +      }
 92.2660 +#endif
 92.2661 +
 92.2662 +      for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
 92.2663 +           pp = prev_row + 1; i < bpp; i++)
 92.2664 +      {
 92.2665 +         v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
 92.2666 +
 92.2667 +         sum += (v < 128) ? v : 256 - v;
 92.2668 +      }
 92.2669 +
 92.2670 +      for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
 92.2671 +      {
 92.2672 +         int a, b, c, pa, pb, pc, p;
 92.2673 +
 92.2674 +         b = *pp++;
 92.2675 +         c = *cp++;
 92.2676 +         a = *lp++;
 92.2677 +
 92.2678 +#ifndef PNG_SLOW_PAETH
 92.2679 +         p = b - c;
 92.2680 +         pc = a - c;
 92.2681 +#ifdef PNG_USE_ABS
 92.2682 +         pa = abs(p);
 92.2683 +         pb = abs(pc);
 92.2684 +         pc = abs(p + pc);
 92.2685 +#else
 92.2686 +         pa = p < 0 ? -p : p;
 92.2687 +         pb = pc < 0 ? -pc : pc;
 92.2688 +         pc = (p + pc) < 0 ? -(p + pc) : p + pc;
 92.2689 +#endif
 92.2690 +         p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
 92.2691 +#else /* PNG_SLOW_PAETH */
 92.2692 +         p = a + b - c;
 92.2693 +         pa = abs(p - a);
 92.2694 +         pb = abs(p - b);
 92.2695 +         pc = abs(p - c);
 92.2696 +         if (pa <= pb && pa <= pc)
 92.2697 +            p = a;
 92.2698 +         else if (pb <= pc)
 92.2699 +            p = b;
 92.2700 +         else
 92.2701 +            p = c;
 92.2702 +#endif /* PNG_SLOW_PAETH */
 92.2703 +
 92.2704 +         v = *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
 92.2705 +
 92.2706 +         sum += (v < 128) ? v : 256 - v;
 92.2707 +
 92.2708 +         if (sum > lmins)  /* We are already worse, don't continue. */
 92.2709 +            break;
 92.2710 +      }
 92.2711 +
 92.2712 +#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
 92.2713 +      if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
 92.2714 +      {
 92.2715 +         int j;
 92.2716 +         png_uint_32 sumhi, sumlo;
 92.2717 +         sumlo = sum & PNG_LOMASK;
 92.2718 +         sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
 92.2719 +
 92.2720 +         for (j = 0; j < num_p_filters; j++)
 92.2721 +         {
 92.2722 +            if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
 92.2723 +            {
 92.2724 +               sumlo = (sumlo * png_ptr->filter_weights[j]) >>
 92.2725 +                  PNG_WEIGHT_SHIFT;
 92.2726 +               sumhi = (sumhi * png_ptr->filter_weights[j]) >>
 92.2727 +                  PNG_WEIGHT_SHIFT;
 92.2728 +            }
 92.2729 +         }
 92.2730 +
 92.2731 +         sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
 92.2732 +            PNG_COST_SHIFT;
 92.2733 +         sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
 92.2734 +            PNG_COST_SHIFT;
 92.2735 +
 92.2736 +         if (sumhi > PNG_HIMASK)
 92.2737 +            sum = PNG_MAXSUM;
 92.2738 +         else
 92.2739 +            sum = (sumhi << PNG_HISHIFT) + sumlo;
 92.2740 +      }
 92.2741 +#endif
 92.2742 +
 92.2743 +      if (sum < mins)
 92.2744 +      {
 92.2745 +         best_row = png_ptr->paeth_row;
 92.2746 +      }
 92.2747 +   }
 92.2748 +#endif /* PNG_NO_WRITE_FILTER */
 92.2749 +   /* Do the actual writing of the filtered row data from the chosen filter. */
 92.2750 +
 92.2751 +   png_write_filtered_row(png_ptr, best_row);
 92.2752 +
 92.2753 +#ifndef PNG_NO_WRITE_FILTER
 92.2754 +#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
 92.2755 +   /* Save the type of filter we picked this time for future calculations */
 92.2756 +   if (png_ptr->num_prev_filters > 0)
 92.2757 +   {
 92.2758 +      int j;
 92.2759 +      for (j = 1; j < num_p_filters; j++)
 92.2760 +      {
 92.2761 +         png_ptr->prev_filters[j] = png_ptr->prev_filters[j - 1];
 92.2762 +      }
 92.2763 +      png_ptr->prev_filters[j] = best_row[0];
 92.2764 +   }
 92.2765 +#endif
 92.2766 +#endif /* PNG_NO_WRITE_FILTER */
 92.2767 +}
 92.2768 +
 92.2769 +
 92.2770 +/* Do the actual writing of a previously filtered row. */
 92.2771 +void /* PRIVATE */
 92.2772 +png_write_filtered_row(png_structp png_ptr, png_bytep filtered_row)
 92.2773 +{
 92.2774 +   png_debug(1, "in png_write_filtered_row\n");
 92.2775 +   png_debug1(2, "filter = %d\n", filtered_row[0]);
 92.2776 +   /* set up the zlib input buffer */
 92.2777 +
 92.2778 +   png_ptr->zstream.next_in = filtered_row;
 92.2779 +   png_ptr->zstream.avail_in = (uInt)png_ptr->row_info.rowbytes + 1;
 92.2780 +   /* repeat until we have compressed all the data */
 92.2781 +   do
 92.2782 +   {
 92.2783 +      int ret; /* return of zlib */
 92.2784 +
 92.2785 +      /* compress the data */
 92.2786 +      ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);
 92.2787 +      /* check for compression errors */
 92.2788 +      if (ret != Z_OK)
 92.2789 +      {
 92.2790 +         if (png_ptr->zstream.msg != NULL)
 92.2791 +            png_error(png_ptr, png_ptr->zstream.msg);
 92.2792 +         else
 92.2793 +            png_error(png_ptr, "zlib error");
 92.2794 +      }
 92.2795 +
 92.2796 +      /* see if it is time to write another IDAT */
 92.2797 +      if (!(png_ptr->zstream.avail_out))
 92.2798 +      {
 92.2799 +         /* write the IDAT and reset the zlib output buffer */
 92.2800 +         png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
 92.2801 +         png_ptr->zstream.next_out = png_ptr->zbuf;
 92.2802 +         png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
 92.2803 +      }
 92.2804 +   /* repeat until all data has been compressed */
 92.2805 +   } while (png_ptr->zstream.avail_in);
 92.2806 +
 92.2807 +   /* swap the current and previous rows */
 92.2808 +   if (png_ptr->prev_row != NULL)
 92.2809 +   {
 92.2810 +      png_bytep tptr;
 92.2811 +
 92.2812 +      tptr = png_ptr->prev_row;
 92.2813 +      png_ptr->prev_row = png_ptr->row_buf;
 92.2814 +      png_ptr->row_buf = tptr;
 92.2815 +   }
 92.2816 +
 92.2817 +   /* finish row - updates counters and flushes zlib if last row */
 92.2818 +   png_write_finish_row(png_ptr);
 92.2819 +
 92.2820 +#if defined(PNG_WRITE_FLUSH_SUPPORTED)
 92.2821 +   png_ptr->flush_rows++;
 92.2822 +
 92.2823 +   if (png_ptr->flush_dist > 0 &&
 92.2824 +       png_ptr->flush_rows >= png_ptr->flush_dist)
 92.2825 +   {
 92.2826 +      png_write_flush(png_ptr);
 92.2827 +   }
 92.2828 +#endif
 92.2829 +}
 92.2830 +#endif /* PNG_WRITE_SUPPORTED */
    93.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    93.2 +++ b/libs/vmath/COPYING	Sat Sep 19 05:51:51 2015 +0300
    93.3 @@ -0,0 +1,674 @@
    93.4 +                    GNU GENERAL PUBLIC LICENSE
    93.5 +                       Version 3, 29 June 2007
    93.6 +
    93.7 + Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
    93.8 + Everyone is permitted to copy and distribute verbatim copies
    93.9 + of this license document, but changing it is not allowed.
   93.10 +
   93.11 +                            Preamble
   93.12 +
   93.13 +  The GNU General Public License is a free, copyleft license for
   93.14 +software and other kinds of works.
   93.15 +
   93.16 +  The licenses for most software and other practical works are designed
   93.17 +to take away your freedom to share and change the works.  By contrast,
   93.18 +the GNU General Public License is intended to guarantee your freedom to
   93.19 +share and change all versions of a program--to make sure it remains free
   93.20 +software for all its users.  We, the Free Software Foundation, use the
   93.21 +GNU General Public License for most of our software; it applies also to
   93.22 +any other work released this way by its authors.  You can apply it to
   93.23 +your programs, too.
   93.24 +
   93.25 +  When we speak of free software, we are referring to freedom, not
   93.26 +price.  Our General Public Licenses are designed to make sure that you
   93.27 +have the freedom to distribute copies of free software (and charge for
   93.28 +them if you wish), that you receive source code or can get it if you
   93.29 +want it, that you can change the software or use pieces of it in new
   93.30 +free programs, and that you know you can do these things.
   93.31 +
   93.32 +  To protect your rights, we need to prevent others from denying you
   93.33 +these rights or asking you to surrender the rights.  Therefore, you have
   93.34 +certain responsibilities if you distribute copies of the software, or if
   93.35 +you modify it: responsibilities to respect the freedom of others.
   93.36 +
   93.37 +  For example, if you distribute copies of such a program, whether
   93.38 +gratis or for a fee, you must pass on to the recipients the same
   93.39 +freedoms that you received.  You must make sure that they, too, receive
   93.40 +or can get the source code.  And you must show them these terms so they
   93.41 +know their rights.
   93.42 +
   93.43 +  Developers that use the GNU GPL protect your rights with two steps:
   93.44 +(1) assert copyright on the software, and (2) offer you this License
   93.45 +giving you legal permission to copy, distribute and/or modify it.
   93.46 +
   93.47 +  For the developers' and authors' protection, the GPL clearly explains
   93.48 +that there is no warranty for this free software.  For both users' and
   93.49 +authors' sake, the GPL requires that modified versions be marked as
   93.50 +changed, so that their problems will not be attributed erroneously to
   93.51 +authors of previous versions.
   93.52 +
   93.53 +  Some devices are designed to deny users access to install or run
   93.54 +modified versions of the software inside them, although the manufacturer
   93.55 +can do so.  This is fundamentally incompatible with the aim of
   93.56 +protecting users' freedom to change the software.  The systematic
   93.57 +pattern of such abuse occurs in the area of products for individuals to
   93.58 +use, which is precisely where it is most unacceptable.  Therefore, we
   93.59 +have designed this version of the GPL to prohibit the practice for those
   93.60 +products.  If such problems arise substantially in other domains, we
   93.61 +stand ready to extend this provision to those domains in future versions
   93.62 +of the GPL, as needed to protect the freedom of users.
   93.63 +
   93.64 +  Finally, every program is threatened constantly by software patents.
   93.65 +States should not allow patents to restrict development and use of
   93.66 +software on general-purpose computers, but in those that do, we wish to
   93.67 +avoid the special danger that patents applied to a free program could
   93.68 +make it effectively proprietary.  To prevent this, the GPL assures that
   93.69 +patents cannot be used to render the program non-free.
   93.70 +
   93.71 +  The precise terms and conditions for copying, distribution and
   93.72 +modification follow.
   93.73 +
   93.74 +                       TERMS AND CONDITIONS
   93.75 +
   93.76 +  0. Definitions.
   93.77 +
   93.78 +  "This License" refers to version 3 of the GNU General Public License.
   93.79 +
   93.80 +  "Copyright" also means copyright-like laws that apply to other kinds of
   93.81 +works, such as semiconductor masks.
   93.82 +
   93.83 +  "The Program" refers to any copyrightable work licensed under this
   93.84 +License.  Each licensee is addressed as "you".  "Licensees" and
   93.85 +"recipients" may be individuals or organizations.
   93.86 +
   93.87 +  To "modify" a work means to copy from or adapt all or part of the work
   93.88 +in a fashion requiring copyright permission, other than the making of an
   93.89 +exact copy.  The resulting work is called a "modified version" of the
   93.90 +earlier work or a work "based on" the earlier work.
   93.91 +
   93.92 +  A "covered work" means either the unmodified Program or a work based
   93.93 +on the Program.
   93.94 +
   93.95 +  To "propagate" a work means to do anything with it that, without
   93.96 +permission, would make you directly or secondarily liable for
   93.97 +infringement under applicable copyright law, except executing it on a
   93.98 +computer or modifying a private copy.  Propagation includes copying,
   93.99 +distribution (with or without modification), making available to the
  93.100 +public, and in some countries other activities as well.
  93.101 +
  93.102 +  To "convey" a work means any kind of propagation that enables other
  93.103 +parties to make or receive copies.  Mere interaction with a user through
  93.104 +a computer network, with no transfer of a copy, is not conveying.
  93.105 +
  93.106 +  An interactive user interface displays "Appropriate Legal Notices"
  93.107 +to the extent that it includes a convenient and prominently visible
  93.108 +feature that (1) displays an appropriate copyright notice, and (2)
  93.109 +tells the user that there is no warranty for the work (except to the
  93.110 +extent that warranties are provided), that licensees may convey the
  93.111 +work under this License, and how to view a copy of this License.  If
  93.112 +the interface presents a list of user commands or options, such as a
  93.113 +menu, a prominent item in the list meets this criterion.
  93.114 +
  93.115 +  1. Source Code.
  93.116 +
  93.117 +  The "source code" for a work means the preferred form of the work
  93.118 +for making modifications to it.  "Object code" means any non-source
  93.119 +form of a work.
  93.120 +
  93.121 +  A "Standard Interface" means an interface that either is an official
  93.122 +standard defined by a recognized standards body, or, in the case of
  93.123 +interfaces specified for a particular programming language, one that
  93.124 +is widely used among developers working in that language.
  93.125 +
  93.126 +  The "System Libraries" of an executable work include anything, other
  93.127 +than the work as a whole, that (a) is included in the normal form of
  93.128 +packaging a Major Component, but which is not part of that Major
  93.129 +Component, and (b) serves only to enable use of the work with that
  93.130 +Major Component, or to implement a Standard Interface for which an
  93.131 +implementation is available to the public in source code form.  A
  93.132 +"Major Component", in this context, means a major essential component
  93.133 +(kernel, window system, and so on) of the specific operating system
  93.134 +(if any) on which the executable work runs, or a compiler used to
  93.135 +produce the work, or an object code interpreter used to run it.
  93.136 +
  93.137 +  The "Corresponding Source" for a work in object code form means all
  93.138 +the source code needed to generate, install, and (for an executable
  93.139 +work) run the object code and to modify the work, including scripts to
  93.140 +control those activities.  However, it does not include the work's
  93.141 +System Libraries, or general-purpose tools or generally available free
  93.142 +programs which are used unmodified in performing those activities but
  93.143 +which are not part of the work.  For example, Corresponding Source
  93.144 +includes interface definition files associated with source files for
  93.145 +the work, and the source code for shared libraries and dynamically
  93.146 +linked subprograms that the work is specifically designed to require,
  93.147 +such as by intimate data communication or control flow between those
  93.148 +subprograms and other parts of the work.
  93.149 +
  93.150 +  The Corresponding Source need not include anything that users
  93.151 +can regenerate automatically from other parts of the Corresponding
  93.152 +Source.
  93.153 +
  93.154 +  The Corresponding Source for a work in source code form is that
  93.155 +same work.
  93.156 +
  93.157 +  2. Basic Permissions.
  93.158 +
  93.159 +  All rights granted under this License are granted for the term of
  93.160 +copyright on the Program, and are irrevocable provided the stated
  93.161 +conditions are met.  This License explicitly affirms your unlimited
  93.162 +permission to run the unmodified Program.  The output from running a
  93.163 +covered work is covered by this License only if the output, given its
  93.164 +content, constitutes a covered work.  This License acknowledges your
  93.165 +rights of fair use or other equivalent, as provided by copyright law.
  93.166 +
  93.167 +  You may make, run and propagate covered works that you do not
  93.168 +convey, without conditions so long as your license otherwise remains
  93.169 +in force.  You may convey covered works to others for the sole purpose
  93.170 +of having them make modifications exclusively for you, or provide you
  93.171 +with facilities for running those works, provided that you comply with
  93.172 +the terms of this License in conveying all material for which you do
  93.173 +not control copyright.  Those thus making or running the covered works
  93.174 +for you must do so exclusively on your behalf, under your direction
  93.175 +and control, on terms that prohibit them from making any copies of
  93.176 +your copyrighted material outside their relationship with you.
  93.177 +
  93.178 +  Conveying under any other circumstances is permitted solely under
  93.179 +the conditions stated below.  Sublicensing is not allowed; section 10
  93.180 +makes it unnecessary.
  93.181 +
  93.182 +  3. Protecting Users' Legal Rights From Anti-Circumvention Law.
  93.183 +
  93.184 +  No covered work shall be deemed part of an effective technological
  93.185 +measure under any applicable law fulfilling obligations under article
  93.186 +11 of the WIPO copyright treaty adopted on 20 December 1996, or
  93.187 +similar laws prohibiting or restricting circumvention of such
  93.188 +measures.
  93.189 +
  93.190 +  When you convey a covered work, you waive any legal power to forbid
  93.191 +circumvention of technological measures to the extent such circumvention
  93.192 +is effected by exercising rights under this License with respect to
  93.193 +the covered work, and you disclaim any intention to limit operation or
  93.194 +modification of the work as a means of enforcing, against the work's
  93.195 +users, your or third parties' legal rights to forbid circumvention of
  93.196 +technological measures.
  93.197 +
  93.198 +  4. Conveying Verbatim Copies.
  93.199 +
  93.200 +  You may convey verbatim copies of the Program's source code as you
  93.201 +receive it, in any medium, provided that you conspicuously and
  93.202 +appropriately publish on each copy an appropriate copyright notice;
  93.203 +keep intact all notices stating that this License and any
  93.204 +non-permissive terms added in accord with section 7 apply to the code;
  93.205 +keep intact all notices of the absence of any warranty; and give all
  93.206 +recipients a copy of this License along with the Program.
  93.207 +
  93.208 +  You may charge any price or no price for each copy that you convey,
  93.209 +and you may offer support or warranty protection for a fee.
  93.210 +
  93.211 +  5. Conveying Modified Source Versions.
  93.212 +
  93.213 +  You may convey a work based on the Program, or the modifications to
  93.214 +produce it from the Program, in the form of source code under the
  93.215 +terms of section 4, provided that you also meet all of these conditions:
  93.216 +
  93.217 +    a) The work must carry prominent notices stating that you modified
  93.218 +    it, and giving a relevant date.
  93.219 +
  93.220 +    b) The work must carry prominent notices stating that it is
  93.221 +    released under this License and any conditions added under section
  93.222 +    7.  This requirement modifies the requirement in section 4 to
  93.223 +    "keep intact all notices".
  93.224 +
  93.225 +    c) You must license the entire work, as a whole, under this
  93.226 +    License to anyone who comes into possession of a copy.  This
  93.227 +    License will therefore apply, along with any applicable section 7
  93.228 +    additional terms, to the whole of the work, and all its parts,
  93.229 +    regardless of how they are packaged.  This License gives no
  93.230 +    permission to license the work in any other way, but it does not
  93.231 +    invalidate such permission if you have separately received it.
  93.232 +
  93.233 +    d) If the work has interactive user interfaces, each must display
  93.234 +    Appropriate Legal Notices; however, if the Program has interactive
  93.235 +    interfaces that do not display Appropriate Legal Notices, your
  93.236 +    work need not make them do so.
  93.237 +
  93.238 +  A compilation of a covered work with other separate and independent
  93.239 +works, which are not by their nature extensions of the covered work,
  93.240 +and which are not combined with it such as to form a larger program,
  93.241 +in or on a volume of a storage or distribution medium, is called an
  93.242 +"aggregate" if the compilation and its resulting copyright are not
  93.243 +used to limit the access or legal rights of the compilation's users
  93.244 +beyond what the individual works permit.  Inclusion of a covered work
  93.245 +in an aggregate does not cause this License to apply to the other
  93.246 +parts of the aggregate.
  93.247 +
  93.248 +  6. Conveying Non-Source Forms.
  93.249 +
  93.250 +  You may convey a covered work in object code form under the terms
  93.251 +of sections 4 and 5, provided that you also convey the
  93.252 +machine-readable Corresponding Source under the terms of this License,
  93.253 +in one of these ways:
  93.254 +
  93.255 +    a) Convey the object code in, or embodied in, a physical product
  93.256 +    (including a physical distribution medium), accompanied by the
  93.257 +    Corresponding Source fixed on a durable physical medium
  93.258 +    customarily used for software interchange.
  93.259 +
  93.260 +    b) Convey the object code in, or embodied in, a physical product
  93.261 +    (including a physical distribution medium), accompanied by a
  93.262 +    written offer, valid for at least three years and valid for as
  93.263 +    long as you offer spare parts or customer support for that product
  93.264 +    model, to give anyone who possesses the object code either (1) a
  93.265 +    copy of the Corresponding Source for all the software in the
  93.266 +    product that is covered by this License, on a durable physical
  93.267 +    medium customarily used for software interchange, for a price no
  93.268 +    more than your reasonable cost of physically performing this
  93.269 +    conveying of source, or (2) access to copy the
  93.270 +    Corresponding Source from a network server at no charge.
  93.271 +
  93.272 +    c) Convey individual copies of the object code with a copy of the
  93.273 +    written offer to provide the Corresponding Source.  This
  93.274 +    alternative is allowed only occasionally and noncommercially, and
  93.275 +    only if you received the object code with such an offer, in accord
  93.276 +    with subsection 6b.
  93.277 +
  93.278 +    d) Convey the object code by offering access from a designated
  93.279 +    place (gratis or for a charge), and offer equivalent access to the
  93.280 +    Corresponding Source in the same way through the same place at no
  93.281 +    further charge.  You need not require recipients to copy the
  93.282 +    Corresponding Source along with the object code.  If the place to
  93.283 +    copy the object code is a network server, the Corresponding Source
  93.284 +    may be on a different server (operated by you or a third party)
  93.285 +    that supports equivalent copying facilities, provided you maintain
  93.286 +    clear directions next to the object code saying where to find the
  93.287 +    Corresponding Source.  Regardless of what server hosts the
  93.288 +    Corresponding Source, you remain obligated to ensure that it is
  93.289 +    available for as long as needed to satisfy these requirements.
  93.290 +
  93.291 +    e) Convey the object code using peer-to-peer transmission, provided
  93.292 +    you inform other peers where the object code and Corresponding
  93.293 +    Source of the work are being offered to the general public at no
  93.294 +    charge under subsection 6d.
  93.295 +
  93.296 +  A separable portion of the object code, whose source code is excluded
  93.297 +from the Corresponding Source as a System Library, need not be
  93.298 +included in conveying the object code work.
  93.299 +
  93.300 +  A "User Product" is either (1) a "consumer product", which means any
  93.301 +tangible personal property which is normally used for personal, family,
  93.302 +or household purposes, or (2) anything designed or sold for incorporation
  93.303 +into a dwelling.  In determining whether a product is a consumer product,
  93.304 +doubtful cases shall be resolved in favor of coverage.  For a particular
  93.305 +product received by a particular user, "normally used" refers to a
  93.306 +typical or common use of that class of product, regardless of the status
  93.307 +of the particular user or of the way in which the particular user
  93.308 +actually uses, or expects or is expected to use, the product.  A product
  93.309 +is a consumer product regardless of whether the product has substantial
  93.310 +commercial, industrial or non-consumer uses, unless such uses represent
  93.311 +the only significant mode of use of the product.
  93.312 +
  93.313 +  "Installation Information" for a User Product means any methods,
  93.314 +procedures, authorization keys, or other information required to install
  93.315 +and execute modified versions of a covered work in that User Product from
  93.316 +a modified version of its Corresponding Source.  The information must
  93.317 +suffice to ensure that the continued functioning of the modified object
  93.318 +code is in no case prevented or interfered with solely because
  93.319 +modification has been made.
  93.320 +
  93.321 +  If you convey an object code work under this section in, or with, or
  93.322 +specifically for use in, a User Product, and the conveying occurs as
  93.323 +part of a transaction in which the right of possession and use of the
  93.324 +User Product is transferred to the recipient in perpetuity or for a
  93.325 +fixed term (regardless of how the transaction is characterized), the
  93.326 +Corresponding Source conveyed under this section must be accompanied
  93.327 +by the Installation Information.  But this requirement does not apply
  93.328 +if neither you nor any third party retains the ability to install
  93.329 +modified object code on the User Product (for example, the work has
  93.330 +been installed in ROM).
  93.331 +
  93.332 +  The requirement to provide Installation Information does not include a
  93.333 +requirement to continue to provide support service, warranty, or updates
  93.334 +for a work that has been modified or installed by the recipient, or for
  93.335 +the User Product in which it has been modified or installed.  Access to a
  93.336 +network may be denied when the modification itself materially and
  93.337 +adversely affects the operation of the network or violates the rules and
  93.338 +protocols for communication across the network.
  93.339 +
  93.340 +  Corresponding Source conveyed, and Installation Information provided,
  93.341 +in accord with this section must be in a format that is publicly
  93.342 +documented (and with an implementation available to the public in
  93.343 +source code form), and must require no special password or key for
  93.344 +unpacking, reading or copying.
  93.345 +
  93.346 +  7. Additional Terms.
  93.347 +
  93.348 +  "Additional permissions" are terms that supplement the terms of this
  93.349 +License by making exceptions from one or more of its conditions.
  93.350 +Additional permissions that are applicable to the entire Program shall
  93.351 +be treated as though they were included in this License, to the extent
  93.352 +that they are valid under applicable law.  If additional permissions
  93.353 +apply only to part of the Program, that part may be used separately
  93.354 +under those permissions, but the entire Program remains governed by
  93.355 +this License without regard to the additional permissions.
  93.356 +
  93.357 +  When you convey a copy of a covered work, you may at your option
  93.358 +remove any additional permissions from that copy, or from any part of
  93.359 +it.  (Additional permissions may be written to require their own
  93.360 +removal in certain cases when you modify the work.)  You may place
  93.361 +additional permissions on material, added by you to a covered work,
  93.362 +for which you have or can give appropriate copyright permission.
  93.363 +
  93.364 +  Notwithstanding any other provision of this License, for material you
  93.365 +add to a covered work, you may (if authorized by the copyright holders of
  93.366 +that material) supplement the terms of this License with terms:
  93.367 +
  93.368 +    a) Disclaiming warranty or limiting liability differently from the
  93.369 +    terms of sections 15 and 16 of this License; or
  93.370 +
  93.371 +    b) Requiring preservation of specified reasonable legal notices or
  93.372 +    author attributions in that material or in the Appropriate Legal
  93.373 +    Notices displayed by works containing it; or
  93.374 +
  93.375 +    c) Prohibiting misrepresentation of the origin of that material, or
  93.376 +    requiring that modified versions of such material be marked in
  93.377 +    reasonable ways as different from the original version; or
  93.378 +
  93.379 +    d) Limiting the use for publicity purposes of names of licensors or
  93.380 +    authors of the material; or
  93.381 +
  93.382 +    e) Declining to grant rights under trademark law for use of some
  93.383 +    trade names, trademarks, or service marks; or
  93.384 +
  93.385 +    f) Requiring indemnification of licensors and authors of that
  93.386 +    material by anyone who conveys the material (or modified versions of
  93.387 +    it) with contractual assumptions of liability to the recipient, for
  93.388 +    any liability that these contractual assumptions directly impose on
  93.389 +    those licensors and authors.
  93.390 +
  93.391 +  All other non-permissive additional terms are considered "further
  93.392 +restrictions" within the meaning of section 10.  If the Program as you
  93.393 +received it, or any part of it, contains a notice stating that it is
  93.394 +governed by this License along with a term that is a further
  93.395 +restriction, you may remove that term.  If a license document contains
  93.396 +a further restriction but permits relicensing or conveying under this
  93.397 +License, you may add to a covered work material governed by the terms
  93.398 +of that license document, provided that the further restriction does
  93.399 +not survive such relicensing or conveying.
  93.400 +
  93.401 +  If you add terms to a covered work in accord with this section, you
  93.402 +must place, in the relevant source files, a statement of the
  93.403 +additional terms that apply to those files, or a notice indicating
  93.404 +where to find the applicable terms.
  93.405 +
  93.406 +  Additional terms, permissive or non-permissive, may be stated in the
  93.407 +form of a separately written license, or stated as exceptions;
  93.408 +the above requirements apply either way.
  93.409 +
  93.410 +  8. Termination.
  93.411 +
  93.412 +  You may not propagate or modify a covered work except as expressly
  93.413 +provided under this License.  Any attempt otherwise to propagate or
  93.414 +modify it is void, and will automatically terminate your rights under
  93.415 +this License (including any patent licenses granted under the third
  93.416 +paragraph of section 11).
  93.417 +
  93.418 +  However, if you cease all violation of this License, then your
  93.419 +license from a particular copyright holder is reinstated (a)
  93.420 +provisionally, unless and until the copyright holder explicitly and
  93.421 +finally terminates your license, and (b) permanently, if the copyright
  93.422 +holder fails to notify you of the violation by some reasonable means
  93.423 +prior to 60 days after the cessation.
  93.424 +
  93.425 +  Moreover, your license from a particular copyright holder is
  93.426 +reinstated permanently if the copyright holder notifies you of the
  93.427 +violation by some reasonable means, this is the first time you have
  93.428 +received notice of violation of this License (for any work) from that
  93.429 +copyright holder, and you cure the violation prior to 30 days after
  93.430 +your receipt of the notice.
  93.431 +
  93.432 +  Termination of your rights under this section does not terminate the
  93.433 +licenses of parties who have received copies or rights from you under
  93.434 +this License.  If your rights have been terminated and not permanently
  93.435 +reinstated, you do not qualify to receive new licenses for the same
  93.436 +material under section 10.
  93.437 +
  93.438 +  9. Acceptance Not Required for Having Copies.
  93.439 +
  93.440 +  You are not required to accept this License in order to receive or
  93.441 +run a copy of the Program.  Ancillary propagation of a covered work
  93.442 +occurring solely as a consequence of using peer-to-peer transmission
  93.443 +to receive a copy likewise does not require acceptance.  However,
  93.444 +nothing other than this License grants you permission to propagate or
  93.445 +modify any covered work.  These actions infringe copyright if you do
  93.446 +not accept this License.  Therefore, by modifying or propagating a
  93.447 +covered work, you indicate your acceptance of this License to do so.
  93.448 +
  93.449 +  10. Automatic Licensing of Downstream Recipients.
  93.450 +
  93.451 +  Each time you convey a covered work, the recipient automatically
  93.452 +receives a license from the original licensors, to run, modify and
  93.453 +propagate that work, subject to this License.  You are not responsible
  93.454 +for enforcing compliance by third parties with this License.
  93.455 +
  93.456 +  An "entity transaction" is a transaction transferring control of an
  93.457 +organization, or substantially all assets of one, or subdividing an
  93.458 +organization, or merging organizations.  If propagation of a covered
  93.459 +work results from an entity transaction, each party to that
  93.460 +transaction who receives a copy of the work also receives whatever
  93.461 +licenses to the work the party's predecessor in interest had or could
  93.462 +give under the previous paragraph, plus a right to possession of the
  93.463 +Corresponding Source of the work from the predecessor in interest, if
  93.464 +the predecessor has it or can get it with reasonable efforts.
  93.465 +
  93.466 +  You may not impose any further restrictions on the exercise of the
  93.467 +rights granted or affirmed under this License.  For example, you may
  93.468 +not impose a license fee, royalty, or other charge for exercise of
  93.469 +rights granted under this License, and you may not initiate litigation
  93.470 +(including a cross-claim or counterclaim in a lawsuit) alleging that
  93.471 +any patent claim is infringed by making, using, selling, offering for
  93.472 +sale, or importing the Program or any portion of it.
  93.473 +
  93.474 +  11. Patents.
  93.475 +
  93.476 +  A "contributor" is a copyright holder who authorizes use under this
  93.477 +License of the Program or a work on which the Program is based.  The
  93.478 +work thus licensed is called the contributor's "contributor version".
  93.479 +
  93.480 +  A contributor's "essential patent claims" are all patent claims
  93.481 +owned or controlled by the contributor, whether already acquired or
  93.482 +hereafter acquired, that would be infringed by some manner, permitted
  93.483 +by this License, of making, using, or selling its contributor version,
  93.484 +but do not include claims that would be infringed only as a
  93.485 +consequence of further modification of the contributor version.  For
  93.486 +purposes of this definition, "control" includes the right to grant
  93.487 +patent sublicenses in a manner consistent with the requirements of
  93.488 +this License.
  93.489 +
  93.490 +  Each contributor grants you a non-exclusive, worldwide, royalty-free
  93.491 +patent license under the contributor's essential patent claims, to
  93.492 +make, use, sell, offer for sale, import and otherwise run, modify and
  93.493 +propagate the contents of its contributor version.
  93.494 +
  93.495 +  In the following three paragraphs, a "patent license" is any express
  93.496 +agreement or commitment, however denominated, not to enforce a patent
  93.497 +(such as an express permission to practice a patent or covenant not to
  93.498 +sue for patent infringement).  To "grant" such a patent license to a
  93.499 +party means to make such an agreement or commitment not to enforce a
  93.500 +patent against the party.
  93.501 +
  93.502 +  If you convey a covered work, knowingly relying on a patent license,
  93.503 +and the Corresponding Source of the work is not available for anyone
  93.504 +to copy, free of charge and under the terms of this License, through a
  93.505 +publicly available network server or other readily accessible means,
  93.506 +then you must either (1) cause the Corresponding Source to be so
  93.507 +available, or (2) arrange to deprive yourself of the benefit of the
  93.508 +patent license for this particular work, or (3) arrange, in a manner
  93.509 +consistent with the requirements of this License, to extend the patent
  93.510 +license to downstream recipients.  "Knowingly relying" means you have
  93.511 +actual knowledge that, but for the patent license, your conveying the
  93.512 +covered work in a country, or your recipient's use of the covered work
  93.513 +in a country, would infringe one or more identifiable patents in that
  93.514 +country that you have reason to believe are valid.
  93.515 +
  93.516 +  If, pursuant to or in connection with a single transaction or
  93.517 +arrangement, you convey, or propagate by procuring conveyance of, a
  93.518 +covered work, and grant a patent license to some of the parties
  93.519 +receiving the covered work authorizing them to use, propagate, modify
  93.520 +or convey a specific copy of the covered work, then the patent license
  93.521 +you grant is automatically extended to all recipients of the covered
  93.522 +work and works based on it.
  93.523 +
  93.524 +  A patent license is "discriminatory" if it does not include within
  93.525 +the scope of its coverage, prohibits the exercise of, or is
  93.526 +conditioned on the non-exercise of one or more of the rights that are
  93.527 +specifically granted under this License.  You may not convey a covered
  93.528 +work if you are a party to an arrangement with a third party that is
  93.529 +in the business of distributing software, under which you make payment
  93.530 +to the third party based on the extent of your activity of conveying
  93.531 +the work, and under which the third party grants, to any of the
  93.532 +parties who would receive the covered work from you, a discriminatory
  93.533 +patent license (a) in connection with copies of the covered work
  93.534 +conveyed by you (or copies made from those copies), or (b) primarily
  93.535 +for and in connection with specific products or compilations that
  93.536 +contain the covered work, unless you entered into that arrangement,
  93.537 +or that patent license was granted, prior to 28 March 2007.
  93.538 +
  93.539 +  Nothing in this License shall be construed as excluding or limiting
  93.540 +any implied license or other defenses to infringement that may
  93.541 +otherwise be available to you under applicable patent law.
  93.542 +
  93.543 +  12. No Surrender of Others' Freedom.
  93.544 +
  93.545 +  If conditions are imposed on you (whether by court order, agreement or
  93.546 +otherwise) that contradict the conditions of this License, they do not
  93.547 +excuse you from the conditions of this License.  If you cannot convey a
  93.548 +covered work so as to satisfy simultaneously your obligations under this
  93.549 +License and any other pertinent obligations, then as a consequence you may
  93.550 +not convey it at all.  For example, if you agree to terms that obligate you
  93.551 +to collect a royalty for further conveying from those to whom you convey
  93.552 +the Program, the only way you could satisfy both those terms and this
  93.553 +License would be to refrain entirely from conveying the Program.
  93.554 +
  93.555 +  13. Use with the GNU Affero General Public License.
  93.556 +
  93.557 +  Notwithstanding any other provision of this License, you have
  93.558 +permission to link or combine any covered work with a work licensed
  93.559 +under version 3 of the GNU Affero General Public License into a single
  93.560 +combined work, and to convey the resulting work.  The terms of this
  93.561 +License will continue to apply to the part which is the covered work,
  93.562 +but the special requirements of the GNU Affero General Public License,
  93.563 +section 13, concerning interaction through a network will apply to the
  93.564 +combination as such.
  93.565 +
  93.566 +  14. Revised Versions of this License.
  93.567 +
  93.568 +  The Free Software Foundation may publish revised and/or new versions of
  93.569 +the GNU General Public License from time to time.  Such new versions will
  93.570 +be similar in spirit to the present version, but may differ in detail to
  93.571 +address new problems or concerns.
  93.572 +
  93.573 +  Each version is given a distinguishing version number.  If the
  93.574 +Program specifies that a certain numbered version of the GNU General
  93.575 +Public License "or any later version" applies to it, you have the
  93.576 +option of following the terms and conditions either of that numbered
  93.577 +version or of any later version published by the Free Software
  93.578 +Foundation.  If the Program does not specify a version number of the
  93.579 +GNU General Public License, you may choose any version ever published
  93.580 +by the Free Software Foundation.
  93.581 +
  93.582 +  If the Program specifies that a proxy can decide which future
  93.583 +versions of the GNU General Public License can be used, that proxy's
  93.584 +public statement of acceptance of a version permanently authorizes you
  93.585 +to choose that version for the Program.
  93.586 +
  93.587 +  Later license versions may give you additional or different
  93.588 +permissions.  However, no additional obligations are imposed on any
  93.589 +author or copyright holder as a result of your choosing to follow a
  93.590 +later version.
  93.591 +
  93.592 +  15. Disclaimer of Warranty.
  93.593 +
  93.594 +  THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
  93.595 +APPLICABLE LAW.  EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
  93.596 +HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
  93.597 +OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
  93.598 +THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  93.599 +PURPOSE.  THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
  93.600 +IS WITH YOU.  SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
  93.601 +ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
  93.602 +
  93.603 +  16. Limitation of Liability.
  93.604 +
  93.605 +  IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
  93.606 +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
  93.607 +THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
  93.608 +GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
  93.609 +USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
  93.610 +DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
  93.611 +PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
  93.612 +EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
  93.613 +SUCH DAMAGES.
  93.614 +
  93.615 +  17. Interpretation of Sections 15 and 16.
  93.616 +
  93.617 +  If the disclaimer of warranty and limitation of liability provided
  93.618 +above cannot be given local legal effect according to their terms,
  93.619 +reviewing courts shall apply local law that most closely approximates
  93.620 +an absolute waiver of all civil liability in connection with the
  93.621 +Program, unless a warranty or assumption of liability accompanies a
  93.622 +copy of the Program in return for a fee.
  93.623 +
  93.624 +                     END OF TERMS AND CONDITIONS
  93.625 +
  93.626 +            How to Apply These Terms to Your New Programs
  93.627 +
  93.628 +  If you develop a new program, and you want it to be of the greatest
  93.629 +possible use to the public, the best way to achieve this is to make it
  93.630 +free software which everyone can redistribute and change under these terms.
  93.631 +
  93.632 +  To do so, attach the following notices to the program.  It is safest
  93.633 +to attach them to the start of each source file to most effectively
  93.634 +state the exclusion of warranty; and each file should have at least
  93.635 +the "copyright" line and a pointer to where the full notice is found.
  93.636 +
  93.637 +    <one line to give the program's name and a brief idea of what it does.>
  93.638 +    Copyright (C) <year>  <name of author>
  93.639 +
  93.640 +    This program is free software: you can redistribute it and/or modify
  93.641 +    it under the terms of the GNU General Public License as published by
  93.642 +    the Free Software Foundation, either version 3 of the License, or
  93.643 +    (at your option) any later version.
  93.644 +
  93.645 +    This program is distributed in the hope that it will be useful,
  93.646 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
  93.647 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  93.648 +    GNU General Public License for more details.
  93.649 +
  93.650 +    You should have received a copy of the GNU General Public License
  93.651 +    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  93.652 +
  93.653 +Also add information on how to contact you by electronic and paper mail.
  93.654 +
  93.655 +  If the program does terminal interaction, make it output a short
  93.656 +notice like this when it starts in an interactive mode:
  93.657 +
  93.658 +    <program>  Copyright (C) <year>  <name of author>
  93.659 +    This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
  93.660 +    This is free software, and you are welcome to redistribute it
  93.661 +    under certain conditions; type `show c' for details.
  93.662 +
  93.663 +The hypothetical commands `show w' and `show c' should show the appropriate
  93.664 +parts of the General Public License.  Of course, your program's commands
  93.665 +might be different; for a GUI interface, you would use an "about box".
  93.666 +
  93.667 +  You should also get your employer (if you work as a programmer) or school,
  93.668 +if any, to sign a "copyright disclaimer" for the program, if necessary.
  93.669 +For more information on this, and how to apply and follow the GNU GPL, see
  93.670 +<http://www.gnu.org/licenses/>.
  93.671 +
  93.672 +  The GNU General Public License does not permit incorporating your program
  93.673 +into proprietary programs.  If your program is a subroutine library, you
  93.674 +may consider it more useful to permit linking proprietary applications with
  93.675 +the library.  If this is what you want to do, use the GNU Lesser General
  93.676 +Public License instead of this License.  But first, please read
  93.677 +<http://www.gnu.org/philosophy/why-not-lgpl.html>.
    94.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    94.2 +++ b/libs/vmath/COPYING.LESSER	Sat Sep 19 05:51:51 2015 +0300
    94.3 @@ -0,0 +1,165 @@
    94.4 +                   GNU LESSER GENERAL PUBLIC LICENSE
    94.5 +                       Version 3, 29 June 2007
    94.6 +
    94.7 + Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
    94.8 + Everyone is permitted to copy and distribute verbatim copies
    94.9 + of this license document, but changing it is not allowed.
   94.10 +
   94.11 +
   94.12 +  This version of the GNU Lesser General Public License incorporates
   94.13 +the terms and conditions of version 3 of the GNU General Public
   94.14 +License, supplemented by the additional permissions listed below.
   94.15 +
   94.16 +  0. Additional Definitions.
   94.17 +
   94.18 +  As used herein, "this License" refers to version 3 of the GNU Lesser
   94.19 +General Public License, and the "GNU GPL" refers to version 3 of the GNU
   94.20 +General Public License.
   94.21 +
   94.22 +  "The Library" refers to a covered work governed by this License,
   94.23 +other than an Application or a Combined Work as defined below.
   94.24 +
   94.25 +  An "Application" is any work that makes use of an interface provided
   94.26 +by the Library, but which is not otherwise based on the Library.
   94.27 +Defining a subclass of a class defined by the Library is deemed a mode
   94.28 +of using an interface provided by the Library.
   94.29 +
   94.30 +  A "Combined Work" is a work produced by combining or linking an
   94.31 +Application with the Library.  The particular version of the Library
   94.32 +with which the Combined Work was made is also called the "Linked
   94.33 +Version".
   94.34 +
   94.35 +  The "Minimal Corresponding Source" for a Combined Work means the
   94.36 +Corresponding Source for the Combined Work, excluding any source code
   94.37 +for portions of the Combined Work that, considered in isolation, are
   94.38 +based on the Application, and not on the Linked Version.
   94.39 +
   94.40 +  The "Corresponding Application Code" for a Combined Work means the
   94.41 +object code and/or source code for the Application, including any data
   94.42 +and utility programs needed for reproducing the Combined Work from the
   94.43 +Application, but excluding the System Libraries of the Combined Work.
   94.44 +
   94.45 +  1. Exception to Section 3 of the GNU GPL.
   94.46 +
   94.47 +  You may convey a covered work under sections 3 and 4 of this License
   94.48 +without being bound by section 3 of the GNU GPL.
   94.49 +
   94.50 +  2. Conveying Modified Versions.
   94.51 +
   94.52 +  If you modify a copy of the Library, and, in your modifications, a
   94.53 +facility refers to a function or data to be supplied by an Application
   94.54 +that uses the facility (other than as an argument passed when the
   94.55 +facility is invoked), then you may convey a copy of the modified
   94.56 +version:
   94.57 +
   94.58 +   a) under this License, provided that you make a good faith effort to
   94.59 +   ensure that, in the event an Application does not supply the
   94.60 +   function or data, the facility still operates, and performs
   94.61 +   whatever part of its purpose remains meaningful, or
   94.62 +
   94.63 +   b) under the GNU GPL, with none of the additional permissions of
   94.64 +   this License applicable to that copy.
   94.65 +
   94.66 +  3. Object Code Incorporating Material from Library Header Files.
   94.67 +
   94.68 +  The object code form of an Application may incorporate material from
   94.69 +a header file that is part of the Library.  You may convey such object
   94.70 +code under terms of your choice, provided that, if the incorporated
   94.71 +material is not limited to numerical parameters, data structure
   94.72 +layouts and accessors, or small macros, inline functions and templates
   94.73 +(ten or fewer lines in length), you do both of the following:
   94.74 +
   94.75 +   a) Give prominent notice with each copy of the object code that the
   94.76 +   Library is used in it and that the Library and its use are
   94.77 +   covered by this License.
   94.78 +
   94.79 +   b) Accompany the object code with a copy of the GNU GPL and this license
   94.80 +   document.
   94.81 +
   94.82 +  4. Combined Works.
   94.83 +
   94.84 +  You may convey a Combined Work under terms of your choice that,
   94.85 +taken together, effectively do not restrict modification of the
   94.86 +portions of the Library contained in the Combined Work and reverse
   94.87 +engineering for debugging such modifications, if you also do each of
   94.88 +the following:
   94.89 +
   94.90 +   a) Give prominent notice with each copy of the Combined Work that
   94.91 +   the Library is used in it and that the Library and its use are
   94.92 +   covered by this License.
   94.93 +
   94.94 +   b) Accompany the Combined Work with a copy of the GNU GPL and this license
   94.95 +   document.
   94.96 +
   94.97 +   c) For a Combined Work that displays copyright notices during
   94.98 +   execution, include the copyright notice for the Library among
   94.99 +   these notices, as well as a reference directing the user to the
  94.100 +   copies of the GNU GPL and this license document.
  94.101 +
  94.102 +   d) Do one of the following:
  94.103 +
  94.104 +       0) Convey the Minimal Corresponding Source under the terms of this
  94.105 +       License, and the Corresponding Application Code in a form
  94.106 +       suitable for, and under terms that permit, the user to
  94.107 +       recombine or relink the Application with a modified version of
  94.108 +       the Linked Version to produce a modified Combined Work, in the
  94.109 +       manner specified by section 6 of the GNU GPL for conveying
  94.110 +       Corresponding Source.
  94.111 +
  94.112 +       1) Use a suitable shared library mechanism for linking with the
  94.113 +       Library.  A suitable mechanism is one that (a) uses at run time
  94.114 +       a copy of the Library already present on the user's computer
  94.115 +       system, and (b) will operate properly with a modified version
  94.116 +       of the Library that is interface-compatible with the Linked
  94.117 +       Version.
  94.118 +
  94.119 +   e) Provide Installation Information, but only if you would otherwise
  94.120 +   be required to provide such information under section 6 of the
  94.121 +   GNU GPL, and only to the extent that such information is
  94.122 +   necessary to install and execute a modified version of the
  94.123 +   Combined Work produced by recombining or relinking the
  94.124 +   Application with a modified version of the Linked Version. (If
  94.125 +   you use option 4d0, the Installation Information must accompany
  94.126 +   the Minimal Corresponding Source and Corresponding Application
  94.127 +   Code. If you use option 4d1, you must provide the Installation
  94.128 +   Information in the manner specified by section 6 of the GNU GPL
  94.129 +   for conveying Corresponding Source.)
  94.130 +
  94.131 +  5. Combined Libraries.
  94.132 +
  94.133 +  You may place library facilities that are a work based on the
  94.134 +Library side by side in a single library together with other library
  94.135 +facilities that are not Applications and are not covered by this
  94.136 +License, and convey such a combined library under terms of your
  94.137 +choice, if you do both of the following:
  94.138 +
  94.139 +   a) Accompany the combined library with a copy of the same work based
  94.140 +   on the Library, uncombined with any other library facilities,
  94.141 +   conveyed under the terms of this License.
  94.142 +
  94.143 +   b) Give prominent notice with the combined library that part of it
  94.144 +   is a work based on the Library, and explaining where to find the
  94.145 +   accompanying uncombined form of the same work.
  94.146 +
  94.147 +  6. Revised Versions of the GNU Lesser General Public License.
  94.148 +
  94.149 +  The Free Software Foundation may publish revised and/or new versions
  94.150 +of the GNU Lesser General Public License from time to time. Such new
  94.151 +versions will be similar in spirit to the present version, but may
  94.152 +differ in detail to address new problems or concerns.
  94.153 +
  94.154 +  Each version is given a distinguishing version number. If the
  94.155 +Library as you received it specifies that a certain numbered version
  94.156 +of the GNU Lesser General Public License "or any later version"
  94.157 +applies to it, you have the option of following the terms and
  94.158 +conditions either of that published version or of any later version
  94.159 +published by the Free Software Foundation. If the Library as you
  94.160 +received it does not specify a version number of the GNU Lesser
  94.161 +General Public License, you may choose any version of the GNU Lesser
  94.162 +General Public License ever published by the Free Software Foundation.
  94.163 +
  94.164 +  If the Library as you received it specifies that a proxy can decide
  94.165 +whether future versions of the GNU Lesser General Public License shall
  94.166 +apply, that proxy's public statement of acceptance of any version is
  94.167 +permanent authorization for you to choose that version for the
  94.168 +Library.
    95.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    95.2 +++ b/libs/vmath/basis.h	Sat Sep 19 05:51:51 2015 +0300
    95.3 @@ -0,0 +1,57 @@
    95.4 +/*
    95.5 +libvmath - a vector math library
    95.6 +Copyright (C) 2004-2011 John Tsiombikas <nuclear@member.fsf.org>
    95.7 +
    95.8 +This program is free software: you can redistribute it and/or modify
    95.9 +it under the terms of the GNU Lesser General Public License as published
   95.10 +by the Free Software Foundation, either version 3 of the License, or
   95.11 +(at your option) any later version.
   95.12 +
   95.13 +This program is distributed in the hope that it will be useful,
   95.14 +but WITHOUT ANY WARRANTY; without even the implied warranty of
   95.15 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   95.16 +GNU Lesser General Public License for more details.
   95.17 +
   95.18 +You should have received a copy of the GNU Lesser General Public License
   95.19 +along with this program.  If not, see <http://www.gnu.org/licenses/>.
   95.20 +*/
   95.21 +
   95.22 +#ifndef VMATH_BASIS_H_
   95.23 +#define VMATH_BASIS_H_
   95.24 +
   95.25 +#include "vector.h"
   95.26 +#include "matrix.h"
   95.27 +
   95.28 +enum {
   95.29 +	LEFT_HANDED,
   95.30 +	RIGHT_HANDED
   95.31 +};
   95.32 +
   95.33 +#ifdef __cplusplus
   95.34 +extern "C" {
   95.35 +#endif	/* __cplusplus */
   95.36 +
   95.37 +void basis_matrix(mat4_t res, vec3_t i, vec3_t j, vec3_t k);
   95.38 +void basis_matrix_dir(mat4_t res, vec3_t dir);
   95.39 +
   95.40 +#ifdef __cplusplus
   95.41 +}	/* extern "C" */
   95.42 +
   95.43 +class Basis {
   95.44 +public:
   95.45 +	Vector3 i, j, k;
   95.46 +
   95.47 +	Basis();
   95.48 +	Basis(const Vector3 &i, const Vector3 &j, const Vector3 &k);
   95.49 +	Basis(const Vector3 &dir, bool left_handed = true);
   95.50 +
   95.51 +	void rotate(scalar_t x, scalar_t y, scalar_t z);
   95.52 +	void rotate(const Vector3 &axis, scalar_t angle);
   95.53 +	void rotate(const Matrix4x4 &mat);
   95.54 +	void rotate(const Quaternion &quat);
   95.55 +
   95.56 +	Matrix3x3 create_rotation_matrix() const;
   95.57 +};
   95.58 +#endif	/* __cplusplus */
   95.59 +
   95.60 +#endif	/* VMATH_BASIS_H_ */
    96.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    96.2 +++ b/libs/vmath/basis_c.c	Sat Sep 19 05:51:51 2015 +0300
    96.3 @@ -0,0 +1,37 @@
    96.4 +/*
    96.5 +libvmath - a vector math library
    96.6 +Copyright (C) 2004-2011 John Tsiombikas <nuclear@member.fsf.org>
    96.7 +
    96.8 +This program is free software: you can redistribute it and/or modify
    96.9 +it under the terms of the GNU Lesser General Public License as published
   96.10 +by the Free Software Foundation, either version 3 of the License, or
   96.11 +(at your option) any later version.
   96.12 +
   96.13 +This program is distributed in the hope that it will be useful,
   96.14 +but WITHOUT ANY WARRANTY; without even the implied warranty of
   96.15 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   96.16 +GNU Lesser General Public License for more details.
   96.17 +
   96.18 +You should have received a copy of the GNU Lesser General Public License
   96.19 +along with this program.  If not, see <http://www.gnu.org/licenses/>.
   96.20 +*/
   96.21 +
   96.22 +#include "basis.h"
   96.23 +#include "matrix.h"
   96.24 +
   96.25 +void basis_matrix(mat4_t res, vec3_t i, vec3_t j, vec3_t k)
   96.26 +{
   96.27 +	m4_identity(res);
   96.28 +	m4_set_column(res, v4_cons(i.x, i.y, i.z, 1.0), 0);
   96.29 +	m4_set_column(res, v4_cons(j.x, j.y, j.z, 1.0), 1);
   96.30 +	m4_set_column(res, v4_cons(k.x, k.y, k.z, 1.0), 2);
   96.31 +}
   96.32 +
   96.33 +void basis_matrix_dir(mat4_t res, vec3_t dir)
   96.34 +{
   96.35 +	vec3_t k = v3_normalize(dir);
   96.36 +	vec3_t j = {0, 1, 0};
   96.37 +	vec3_t i = v3_cross(j, k);
   96.38 +	j = v3_cross(k, i);
   96.39 +	basis_matrix(res, i, j, k);
   96.40 +}
    97.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    97.2 +++ b/libs/vmath/geom.c	Sat Sep 19 05:51:51 2015 +0300
    97.3 @@ -0,0 +1,150 @@
    97.4 +/*
    97.5 +libvmath - a vector math library
    97.6 +Copyright (C) 2004-2011 John Tsiombikas <nuclear@member.fsf.org>
    97.7 +
    97.8 +This program is free software: you can redistribute it and/or modify
    97.9 +it under the terms of the GNU Lesser General Public License as published
   97.10 +by the Free Software Foundation, either version 3 of the License, or
   97.11 +(at your option) any later version.
   97.12 +
   97.13 +This program is distributed in the hope that it will be useful,
   97.14 +but WITHOUT ANY WARRANTY; without even the implied warranty of
   97.15 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   97.16 +GNU Lesser General Public License for more details.
   97.17 +
   97.18 +You should have received a copy of the GNU Lesser General Public License
   97.19 +along with this program.  If not, see <http://www.gnu.org/licenses/>.
   97.20 +*/
   97.21 +
   97.22 +
   97.23 +#include <math.h>
   97.24 +#include "geom.h"
   97.25 +#include "vector.h"
   97.26 +
   97.27 +plane_t plane_cons(scalar_t nx, scalar_t ny, scalar_t nz, scalar_t d)
   97.28 +{
   97.29 +	plane_t p;
   97.30 +	p.norm.x = nx;
   97.31 +	p.norm.y = ny;
   97.32 +	p.norm.z = nz;
   97.33 +	p.d = d;
   97.34 +	return p;
   97.35 +}
   97.36 +
   97.37 +plane_t plane_poly(vec3_t v0, vec3_t v1, vec3_t v2)
   97.38 +{
   97.39 +	vec3_t a, b, norm;
   97.40 +
   97.41 +	a = v3_sub(v1, v0);
   97.42 +	b = v3_sub(v2, v0);
   97.43 +	norm = v3_cross(a, b);
   97.44 +	norm = v3_normalize(norm);
   97.45 +
   97.46 +	return plane_ptnorm(v0, norm);
   97.47 +}
   97.48 +
   97.49 +plane_t plane_ptnorm(vec3_t pt, vec3_t normal)
   97.50 +{
   97.51 +	plane_t plane;
   97.52 +
   97.53 +	plane.norm = normal;
   97.54 +	plane.d = v3_dot(pt, normal);
   97.55 +
   97.56 +	return plane;
   97.57 +}
   97.58 +
   97.59 +plane_t plane_invert(plane_t p)
   97.60 +{
   97.61 +	p.norm = v3_neg(p.norm);
   97.62 +	p.d = -p.d;
   97.63 +	return p;
   97.64 +}
   97.65 +
   97.66 +scalar_t plane_signed_dist(plane_t plane, vec3_t pt)
   97.67 +{
   97.68 +	vec3_t pp = plane_point(plane);
   97.69 +	vec3_t pptopt = v3_sub(pt, pp);
   97.70 +	return v3_dot(pptopt, plane.norm);
   97.71 +}
   97.72 +
   97.73 +scalar_t plane_dist(plane_t plane, vec3_t pt)
   97.74 +{
   97.75 +	return fabs(plane_signed_dist(plane, pt));
   97.76 +}
   97.77 +
   97.78 +vec3_t plane_point(plane_t plane)
   97.79 +{
   97.80 +	return v3_scale(plane.norm, plane.d);
   97.81 +}
   97.82 +
   97.83 +int plane_ray_intersect(ray_t ray, plane_t plane, scalar_t *pos)
   97.84 +{
   97.85 +	vec3_t pt, orig_to_pt;
   97.86 +	scalar_t ndotdir;
   97.87 +
   97.88 +	pt = plane_point(plane);
   97.89 +	ndotdir = v3_dot(plane.norm, ray.dir);
   97.90 +
   97.91 +	if(fabs(ndotdir) < 1e-7) {
   97.92 +		return 0;
   97.93 +	}
   97.94 +
   97.95 +	if(pos) {
   97.96 +		orig_to_pt = v3_sub(pt, ray.origin);
   97.97 +		*pos = v3_dot(plane.norm, orig_to_pt) / ndotdir;
   97.98 +	}
   97.99 +	return 1;
  97.100 +}
  97.101 +
  97.102 +sphere_t sphere_cons(scalar_t x, scalar_t y, scalar_t z, scalar_t rad)
  97.103 +{
  97.104 +	sphere_t sph;
  97.105 +	sph.pos.x = x;
  97.106 +	sph.pos.y = y;
  97.107 +	sph.pos.z = z;
  97.108 +	sph.rad = rad;
  97.109 +	return sph;
  97.110 +}
  97.111 +
  97.112 +int sphere_ray_intersect(ray_t ray, sphere_t sph, scalar_t *pos)
  97.113 +{
  97.114 +	scalar_t a, b, c, d, sqrt_d, t1, t2, t;
  97.115 +
  97.116 +	a = v3_dot(ray.dir, ray.dir);
  97.117 +	b = 2.0 * ray.dir.x * (ray.origin.x - sph.pos.x) +
  97.118 +		2.0 * ray.dir.y * (ray.origin.y - sph.pos.y) +
  97.119 +		2.0 * ray.dir.z * (ray.origin.z - sph.pos.z);
  97.120 +	c = v3_dot(sph.pos, sph.pos) + v3_dot(ray.origin, ray.origin) +
  97.121 +		2.0 * v3_dot(v3_neg(sph.pos), ray.origin) - sph.rad * sph.rad;
  97.122 +
  97.123 +	d = b * b - 4.0 * a * c;
  97.124 +	if(d < 0.0) {
  97.125 +		return 0;
  97.126 +	}
  97.127 +
  97.128 +	sqrt_d = sqrt(d);
  97.129 +	t1 = (-b + sqrt_d) / (2.0 * a);
  97.130 +	t2 = (-b - sqrt_d) / (2.0 * a);
  97.131 +
  97.132 +	if(t1 < 1e-7 || t1 > 1.0) {
  97.133 +		t1 = t2;
  97.134 +	}
  97.135 +	if(t2 < 1e-7 || t2 > 1.0) {
  97.136 +		t2 = t1;
  97.137 +	}
  97.138 +	t = t1 < t2 ? t1 : t2;
  97.139 +
  97.140 +	if(t < 1e-7 || t > 1.0) {
  97.141 +		return 0;
  97.142 +	}
  97.143 +
  97.144 +	if(pos) {
  97.145 +		*pos = t;
  97.146 +	}
  97.147 +	return 1;
  97.148 +}
  97.149 +
  97.150 +int sphere_sphere_intersect(sphere_t sph1, sphere_t sph2, scalar_t *pos, scalar_t *rad)
  97.151 +{
  97.152 +	return -1;
  97.153 +}
    98.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    98.2 +++ b/libs/vmath/geom.h	Sat Sep 19 05:51:51 2015 +0300
    98.3 @@ -0,0 +1,73 @@
    98.4 +/*
    98.5 +libvmath - a vector math library
    98.6 +Copyright (C) 2004-2011 John Tsiombikas <nuclear@member.fsf.org>
    98.7 +
    98.8 +This program is free software: you can redistribute it and/or modify
    98.9 +it under the terms of the GNU Lesser General Public License as published
   98.10 +by the Free Software Foundation, either version 3 of the License, or
   98.11 +(at your option) any later version.
   98.12 +
   98.13 +This program is distributed in the hope that it will be useful,
   98.14 +but WITHOUT ANY WARRANTY; without even the implied warranty of
   98.15 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   98.16 +GNU Lesser General Public License for more details.
   98.17 +
   98.18 +You should have received a copy of the GNU Lesser General Public License
   98.19 +along with this program.  If not, see <http://www.gnu.org/licenses/>.
   98.20 +*/
   98.21 +
   98.22 +#ifndef GEOM_H_
   98.23 +#define GEOM_H_
   98.24 +
   98.25 +#include "vector.h"
   98.26 +#include "ray.h"
   98.27 +
   98.28 +typedef struct {
   98.29 +	vec3_t norm;
   98.30 +	scalar_t d;
   98.31 +} plane_t;
   98.32 +
   98.33 +typedef struct {
   98.34 +	vec3_t pos;
   98.35 +	scalar_t rad;
   98.36 +} sphere_t;
   98.37 +
   98.38 +typedef struct {
   98.39 +	vec3_t min, max;
   98.40 +} aabox_t;
   98.41 +
   98.42 +#ifdef __cplusplus
   98.43 +extern "C" {
   98.44 +#endif
   98.45 +
   98.46 +/* planes are good... you need planes, yes you do */
   98.47 +plane_t plane_cons(scalar_t nx, scalar_t ny, scalar_t nz, scalar_t d);
   98.48 +plane_t plane_poly(vec3_t v0, vec3_t v1, vec3_t v2);
   98.49 +plane_t plane_ptnorm(vec3_t pt, vec3_t normal);
   98.50 +
   98.51 +plane_t plane_invert(plane_t p);
   98.52 +
   98.53 +scalar_t plane_signed_dist(plane_t plane, vec3_t pt);
   98.54 +scalar_t plane_dist(plane_t plane, vec3_t pt);
   98.55 +vec3_t plane_point(plane_t plane);
   98.56 +
   98.57 +int plane_ray_intersect(ray_t ray, plane_t plane, scalar_t *pos);
   98.58 +
   98.59 +/* spheres always come in handy */
   98.60 +sphere_t sphere_cons(scalar_t x, scalar_t y, scalar_t z, scalar_t rad);
   98.61 +
   98.62 +int sphere_ray_intersect(ray_t ray, sphere_t sph, scalar_t *pos);
   98.63 +int sphere_sphere_intersect(sphere_t sph1, sphere_t sph2, scalar_t *pos, scalar_t *rad);
   98.64 +
   98.65 +#ifdef __cplusplus
   98.66 +}
   98.67 +
   98.68 +/* TODO
   98.69 +class Plane : public plane_t {
   98.70 +public:
   98.71 +};
   98.72 +*/
   98.73 +
   98.74 +#endif
   98.75 +
   98.76 +#endif	/* GEOM_H_ */
    99.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    99.2 +++ b/libs/vmath/matrix.h	Sat Sep 19 05:51:51 2015 +0300
    99.3 @@ -0,0 +1,205 @@
    99.4 +/*
    99.5 +libvmath - a vector math library
    99.6 +Copyright (C) 2004-2011 John Tsiombikas <nuclear@member.fsf.org>
    99.7 +
    99.8 +This program is free software: you can redistribute it and/or modify
    99.9 +it under the terms of the GNU Lesser General Public License as published
   99.10 +by the Free Software Foundation, either version 3 of the License, or
   99.11 +(at your option) any later version.
   99.12 +
   99.13 +This program is distributed in the hope that it will be useful,
   99.14 +but WITHOUT ANY WARRANTY; without even the implied warranty of
   99.15 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   99.16 +GNU Lesser General Public License for more details.
   99.17 +
   99.18 +You should have received a copy of the GNU Lesser General Public License
   99.19 +along with this program.  If not, see <http://www.gnu.org/licenses/>.
   99.20 +*/
   99.21 +
   99.22 +#ifndef VMATH_MATRIX_H_
   99.23 +#define VMATH_MATRIX_H_
   99.24 +
   99.25 +#include <stdio.h>
   99.26 +#include "vmath_types.h"
   99.27 +
   99.28 +#ifdef __cplusplus
   99.29 +extern "C" {
   99.30 +#endif	/* __cplusplus */
   99.31 +
   99.32 +/* C matrix 3x3 functions */
   99.33 +static inline void m3_identity(mat3_t m);
   99.34 +static inline void m3_cons(mat3_t m,
   99.35 +		scalar_t m11, scalar_t m12, scalar_t m13,
   99.36 +		scalar_t m21, scalar_t m22, scalar_t m23,
   99.37 +		scalar_t m31, scalar_t m32, scalar_t m33);
   99.38 +static inline void m3_copy(mat3_t dest, mat3_t src);
   99.39 +void m3_to_m4(mat4_t dest, mat3_t src);
   99.40 +
   99.41 +void m3_print(FILE *fp, mat3_t m);
   99.42 +
   99.43 +/* C matrix 4x4 functions */
   99.44 +static inline void m4_identity(mat4_t m);
   99.45 +static inline void m4_cons(mat4_t m,
   99.46 +		scalar_t m11, scalar_t m12, scalar_t m13, scalar_t m14,
   99.47 +		scalar_t m21, scalar_t m22, scalar_t m23, scalar_t m24,
   99.48 +		scalar_t m31, scalar_t m32, scalar_t m33, scalar_t m34,
   99.49 +		scalar_t m41, scalar_t m42, scalar_t m43, scalar_t m44);
   99.50 +static inline void m4_copy(mat4_t dest, mat4_t src);
   99.51 +void m4_to_m3(mat3_t dest, mat4_t src);
   99.52 +
   99.53 +static inline void m4_mult(mat4_t res, mat4_t m1, mat4_t m2);
   99.54 +
   99.55 +void m4_translate(mat4_t m, scalar_t x, scalar_t y, scalar_t z);
   99.56 +void m4_rotate(mat4_t m, scalar_t x, scalar_t y, scalar_t z);
   99.57 +void m4_rotate_x(mat4_t m, scalar_t angle);
   99.58 +void m4_rotate_y(mat4_t m, scalar_t angle);
   99.59 +void m4_rotate_z(mat4_t m, scalar_t angle);
   99.60 +void m4_rotate_axis(mat4_t m, scalar_t angle, scalar_t x, scalar_t y, scalar_t z);
   99.61 +void m4_rotate_quat(mat4_t m, quat_t q);
   99.62 +void m4_scale(mat4_t m, scalar_t x, scalar_t y, scalar_t z);
   99.63 +static inline void m4_set_column(mat4_t m, vec4_t v, int idx);
   99.64 +static inline void m4_set_row(mat4_t m, vec4_t v, int idx);
   99.65 +
   99.66 +void m4_transpose(mat4_t res, mat4_t m);
   99.67 +scalar_t m4_determinant(mat4_t m);
   99.68 +void m4_adjoint(mat4_t res, mat4_t m);
   99.69 +void m4_inverse(mat4_t res, mat4_t m);
   99.70 +
   99.71 +void m4_print(FILE *fp, mat4_t m);
   99.72 +
   99.73 +#ifdef __cplusplus
   99.74 +}
   99.75 +
   99.76 +/* when included from C++ source files, also define the matrix classes */
   99.77 +#include <iostream>
   99.78 +
   99.79 +/** 3x3 matrix */
   99.80 +class Matrix3x3 {
   99.81 +private:
   99.82 +	scalar_t m[3][3];
   99.83 +
   99.84 +public:
   99.85 +
   99.86 +	static Matrix3x3 identity;
   99.87 +
   99.88 +	Matrix3x3();
   99.89 +	Matrix3x3(	scalar_t m11, scalar_t m12, scalar_t m13,
   99.90 +				scalar_t m21, scalar_t m22, scalar_t m23,
   99.91 +				scalar_t m31, scalar_t m32, scalar_t m33);
   99.92 +	Matrix3x3(const mat3_t cmat);
   99.93 +
   99.94 +	Matrix3x3(const Matrix4x4 &mat4x4);
   99.95 +
   99.96 +	/* binary operations matrix (op) matrix */
   99.97 +	friend Matrix3x3 operator +(const Matrix3x3 &m1, const Matrix3x3 &m2);
   99.98 +	friend Matrix3x3 operator -(const Matrix3x3 &m1, const Matrix3x3 &m2);
   99.99 +	friend Matrix3x3 operator *(const Matrix3x3 &m1, const Matrix3x3 &m2);
  99.100 +
  99.101 +	friend void operator +=(Matrix3x3 &m1, const Matrix3x3 &m2);
  99.102 +	friend void operator -=(Matrix3x3 &m1, const Matrix3x3 &m2);
  99.103 +	friend void operator *=(Matrix3x3 &m1, const Matrix3x3 &m2);
  99.104 +
  99.105 +	/* binary operations matrix (op) scalar and scalar (op) matrix */
  99.106 +	friend Matrix3x3 operator *(const Matrix3x3 &mat, scalar_t scalar);
  99.107 +	friend Matrix3x3 operator *(scalar_t scalar, const Matrix3x3 &mat);
  99.108 +
  99.109 +	friend void operator *=(Matrix3x3 &mat, scalar_t scalar);
  99.110 +
  99.111 +	inline scalar_t *operator [](int index);
  99.112 +	inline const scalar_t *operator [](int index) const;
  99.113 +
  99.114 +	inline void reset_identity();
  99.115 +
  99.116 +	void translate(const Vector2 &trans);
  99.117 +	void set_translation(const Vector2 &trans);
  99.118 +
  99.119 +	void rotate(scalar_t angle);						/* 2d rotation */
  99.120 +	void rotate(const Vector3 &euler_angles);			/* 3d rotation with euler angles */
  99.121 +	void rotate(const Vector3 &axis, scalar_t angle);	/* 3d axis/angle rotation */
  99.122 +	void set_rotation(scalar_t angle);
  99.123 +	void set_rotation(const Vector3 &euler_angles);
  99.124 +	void set_rotation(const Vector3 &axis, scalar_t angle);
  99.125 +
  99.126 +	void scale(const Vector3 &scale_vec);
  99.127 +	void set_scaling(const Vector3 &scale_vec);
  99.128 +
  99.129 +	void set_column_vector(const Vector3 &vec, unsigned int col_index);
  99.130 +	void set_row_vector(const Vector3 &vec, unsigned int row_index);
  99.131 +	Vector3 get_column_vector(unsigned int col_index) const;
  99.132 +	Vector3 get_row_vector(unsigned int row_index) const;
  99.133 +
  99.134 +	void transpose();
  99.135 +	Matrix3x3 transposed() const;
  99.136 +	scalar_t determinant() const;
  99.137 +	Matrix3x3 inverse() const;
  99.138 +
  99.139 +	friend std::ostream &operator <<(std::ostream &out, const Matrix3x3 &mat);
  99.140 +};
  99.141 +
  99.142 +/** 4x4 matrix */
  99.143 +class Matrix4x4 {
  99.144 +private:
  99.145 +	scalar_t m[4][4];
  99.146 +
  99.147 +public:
  99.148 +
  99.149 +	static Matrix4x4 identity;
  99.150 +
  99.151 +	Matrix4x4();
  99.152 +	Matrix4x4(	scalar_t m11, scalar_t m12, scalar_t m13, scalar_t m14,
  99.153 +				scalar_t m21, scalar_t m22, scalar_t m23, scalar_t m24,
  99.154 +				scalar_t m31, scalar_t m32, scalar_t m33, scalar_t m34,
  99.155 +				scalar_t m41, scalar_t m42, scalar_t m43, scalar_t m44);
  99.156 +	Matrix4x4(const mat4_t cmat);
  99.157 +
  99.158 +	Matrix4x4(const Matrix3x3 &mat3x3);
  99.159 +
  99.160 +	/* binary operations matrix (op) matrix */
  99.161 +	friend Matrix4x4 operator +(const Matrix4x4 &m1, const Matrix4x4 &m2);
  99.162 +	friend Matrix4x4 operator -(const Matrix4x4 &m1, const Matrix4x4 &m2);
  99.163 +	friend Matrix4x4 operator *(const Matrix4x4 &m1, const Matrix4x4 &m2);
  99.164 +
  99.165 +	friend void operator +=(Matrix4x4 &m1, const Matrix4x4 &m2);
  99.166 +	friend void operator -=(Matrix4x4 &m1, const Matrix4x4 &m2);
  99.167 +	friend inline void operator *=(Matrix4x4 &m1, const Matrix4x4 &m2);
  99.168 +
  99.169 +	/* binary operations matrix (op) scalar and scalar (op) matrix */
  99.170 +	friend Matrix4x4 operator *(const Matrix4x4 &mat, scalar_t scalar);
  99.171 +	friend Matrix4x4 operator *(scalar_t scalar, const Matrix4x4 &mat);
  99.172 +
  99.173 +	friend void operator *=(Matrix4x4 &mat, scalar_t scalar);
  99.174 +
  99.175 +	inline scalar_t *operator [](int index);
  99.176 +	inline const scalar_t *operator [](int index) const;
  99.177 +
  99.178 +	inline void reset_identity();
  99.179 +
  99.180 +	void translate(const Vector3 &trans);
  99.181 +	void set_translation(const Vector3 &trans);
  99.182 +
  99.183 +	void rotate(const Vector3 &euler_angles);			/* 3d rotation with euler angles */
  99.184 +	void rotate(const Vector3 &axis, scalar_t angle);	/* 3d axis/angle rotation */
  99.185 +	void set_rotation(const Vector3 &euler_angles);
  99.186 +	void set_rotation(const Vector3 &axis, scalar_t angle);
  99.187 +
  99.188 +	void scale(const Vector4 &scale_vec);
  99.189 +	void set_scaling(const Vector4 &scale_vec);
  99.190 +
  99.191 +	void set_column_vector(const Vector4 &vec, unsigned int col_index);
  99.192 +	void set_row_vector(const Vector4 &vec, unsigned int row_index);
  99.193 +	Vector4 get_column_vector(unsigned int col_index) const;
  99.194 +	Vector4 get_row_vector(unsigned int row_index) const;
  99.195 +
  99.196 +	void transpose();
  99.197 +	Matrix4x4 transposed() const;
  99.198 +	scalar_t determinant() const;
  99.199 +	Matrix4x4 adjoint() const;
  99.200 +	Matrix4x4 inverse() const;
  99.201 +
  99.202 +	friend std::ostream &operator <<(std::ostream &out, const Matrix4x4 &mat);
  99.203 +};
  99.204 +#endif	/* __cplusplus */
  99.205 +
  99.206 +#include "matrix.inl"
  99.207 +
  99.208 +#endif	/* VMATH_MATRIX_H_ */
   100.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   100.2 +++ b/libs/vmath/matrix.inl	Sat Sep 19 05:51:51 2015 +0300
   100.3 @@ -0,0 +1,165 @@
   100.4 +/*
   100.5 +libvmath - a vector math library
   100.6 +Copyright (C) 2004-2011 John Tsiombikas <nuclear@member.fsf.org>
   100.7 +
   100.8 +This program is free software: you can redistribute it and/or modify
   100.9 +it under the terms of the GNU Lesser General Public License as published
  100.10 +by the Free Software Foundation, either version 3 of the License, or
  100.11 +(at your option) any later version.
  100.12 +
  100.13 +This program is distributed in the hope that it will be useful,
  100.14 +but WITHOUT ANY WARRANTY; without even the implied warranty of
  100.15 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  100.16 +GNU Lesser General Public License for more details.
  100.17 +
  100.18 +You should have received a copy of the GNU Lesser General Public License
  100.19 +along with this program.  If not, see <http://www.gnu.org/licenses/>.
  100.20 +*/
  100.21 +
  100.22 +#include <string.h>
  100.23 +
  100.24 +#ifdef __cplusplus
  100.25 +extern "C" {
  100.26 +#endif	/* __cplusplus */
  100.27 +
  100.28 +/* C matrix 3x3 functions */
  100.29 +static inline void m3_identity(mat3_t m)
  100.30 +{
  100.31 +	static const mat3_t id = {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}};
  100.32 +	memcpy(m, id, sizeof id);
  100.33 +}
  100.34 +
  100.35 +static inline void m3_cons(mat3_t m,
  100.36 +		scalar_t m11, scalar_t m12, scalar_t m13,
  100.37 +		scalar_t m21, scalar_t m22, scalar_t m23,
  100.38 +		scalar_t m31, scalar_t m32, scalar_t m33)
  100.39 +{
  100.40 +	m[0][0] = m11; m[0][1] = m12; m[0][2] = m13;
  100.41 +	m[1][0] = m21; m[1][1] = m22; m[1][2] = m23;
  100.42 +	m[2][0] = m31; m[2][1] = m32; m[2][2] = m33;
  100.43 +}
  100.44 +
  100.45 +static inline void m3_copy(mat3_t dest, mat3_t src)
  100.46 +{
  100.47 +	memcpy(dest, src, sizeof(mat3_t));
  100.48 +}
  100.49 +
  100.50 +
  100.51 +/* C matrix 4x4 functions */
  100.52 +static inline void m4_identity(mat4_t m)
  100.53 +{
  100.54 +	static const mat4_t id = {{1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 1, 0}, {0, 0, 0, 1}};
  100.55 +	memcpy(m, id, sizeof id);
  100.56 +}
  100.57 +
  100.58 +static inline void m4_cons(mat4_t m,
  100.59 +		scalar_t m11, scalar_t m12, scalar_t m13, scalar_t m14,
  100.60 +		scalar_t m21, scalar_t m22, scalar_t m23, scalar_t m24,
  100.61 +		scalar_t m31, scalar_t m32, scalar_t m33, scalar_t m34,
  100.62 +		scalar_t m41, scalar_t m42, scalar_t m43, scalar_t m44)
  100.63 +{
  100.64 +	m[0][0] = m11; m[0][1] = m12; m[0][2] = m13; m[0][3] = m14;
  100.65 +	m[1][0] = m21; m[1][1] = m22; m[1][2] = m23; m[1][3] = m24;
  100.66 +	m[2][0] = m31; m[2][1] = m32; m[2][2] = m33; m[2][3] = m34;
  100.67 +	m[3][0] = m41; m[3][1] = m42; m[3][2] = m43; m[3][3] = m44;
  100.68 +}
  100.69 +
  100.70 +static inline void m4_copy(mat4_t dest, mat4_t src)
  100.71 +{
  100.72 +	memcpy(dest, src, sizeof(mat4_t));
  100.73 +}
  100.74 +
  100.75 +static inline void m4_mult(mat4_t res, mat4_t m1, mat4_t m2)
  100.76 +{
  100.77 +	res[0][0] = m1[0][0] * m2[0][0] + m1[0][1] * m2[1][0] + m1[0][2] * m2[2][0] + m1[0][3] * m2[3][0];
  100.78 +	res[0][1] = m1[0][0] * m2[0][1] + m1[0][1] * m2[1][1] + m1[0][2] * m2[2][1] + m1[0][3] * m2[3][1];
  100.79 +	res[0][2] = m1[0][0] * m2[0][2] + m1[0][1] * m2[1][2] + m1[0][2] * m2[2][2] + m1[0][3] * m2[3][2];
  100.80 +	res[0][3] = m1[0][0] * m2[0][3] + m1[0][1] * m2[1][3] + m1[0][2] * m2[2][3] + m1[0][3] * m2[3][3];
  100.81 +
  100.82 +	res[1][0] = m1[1][0] * m2[0][0] + m1[1][1] * m2[1][0] + m1[1][2] * m2[2][0] + m1[1][3] * m2[3][0];
  100.83 +	res[1][1] = m1[1][0] * m2[0][1] + m1[1][1] * m2[1][1] + m1[1][2] * m2[2][1] + m1[1][3] * m2[3][1];
  100.84 +	res[1][2] = m1[1][0] * m2[0][2] + m1[1][1] * m2[1][2] + m1[1][2] * m2[2][2] + m1[1][3] * m2[3][2];
  100.85 +	res[1][3] = m1[1][0] * m2[0][3] + m1[1][1] * m2[1][3] + m1[1][2] * m2[2][3] + m1[1][3] * m2[3][3];
  100.86 +
  100.87 +	res[2][0] = m1[2][0] * m2[0][0] + m1[2][1] * m2[1][0] + m1[2][2] * m2[2][0] + m1[2][3] * m2[3][0];
  100.88 +	res[2][1] = m1[2][0] * m2[0][1] + m1[2][1] * m2[1][1] + m1[2][2] * m2[2][1] + m1[2][3] * m2[3][1];
  100.89 +	res[2][2] = m1[2][0] * m2[0][2] + m1[2][1] * m2[1][2] + m1[2][2] * m2[2][2] + m1[2][3] * m2[3][2];
  100.90 +	res[2][3] = m1[2][0] * m2[0][3] + m1[2][1] * m2[1][3] + m1[2][2] * m2[2][3] + m1[2][3] * m2[3][3];
  100.91 +
  100.92 +	res[3][0] = m1[3][0] * m2[0][0] + m1[3][1] * m2[1][0] + m1[3][2] * m2[2][0] + m1[3][3] * m2[3][0];
  100.93 +	res[3][1] = m1[3][0] * m2[0][1] + m1[3][1] * m2[1][1] + m1[3][2] * m2[2][1] + m1[3][3] * m2[3][1];
  100.94 +	res[3][2] = m1[3][0] * m2[0][2] + m1[3][1] * m2[1][2] + m1[3][2] * m2[2][2] + m1[3][3] * m2[3][2];
  100.95 +	res[3][3] = m1[3][0] * m2[0][3] + m1[3][1] * m2[1][3] + m1[3][2] * m2[2][3] + m1[3][3] * m2[3][3];
  100.96 +}
  100.97 +
  100.98 +static inline void m4_set_column(mat4_t m, vec4_t v, int idx)
  100.99 +{
 100.100 +	m[0][idx] = v.x;
 100.101 +	m[1][idx] = v.y;
 100.102 +	m[2][idx] = v.z;
 100.103 +	m[3][idx] = v.w;
 100.104 +}
 100.105 +
 100.106 +static inline void m4_set_row(mat4_t m, vec4_t v, int idx)
 100.107 +{
 100.108 +	m[idx][0] = v.x;
 100.109 +	m[idx][1] = v.y;
 100.110 +	m[idx][2] = v.z;
 100.111 +	m[idx][3] = v.w;
 100.112 +}
 100.113 +
 100.114 +#ifdef __cplusplus
 100.115 +}	/* extern "C" */
 100.116 +
 100.117 +
 100.118 +/* unrolled to hell and inline */
 100.119 +inline Matrix4x4 operator *(const Matrix4x4 &m1, const Matrix4x4 &m2) {
 100.120 +	Matrix4x4 res;
 100.121 +
 100.122 +	res.m[0][0] = m1.m[0][0] * m2.m[0][0] + m1.m[0][1] * m2.m[1][0] + m1.m[0][2] * m2.m[2][0] + m1.m[0][3] * m2.m[3][0];
 100.123 +	res.m[0][1] = m1.m[0][0] * m2.m[0][1] + m1.m[0][1] * m2.m[1][1] + m1.m[0][2] * m2.m[2][1] + m1.m[0][3] * m2.m[3][1];
 100.124 +	res.m[0][2] = m1.m[0][0] * m2.m[0][2] + m1.m[0][1] * m2.m[1][2] + m1.m[0][2] * m2.m[2][2] + m1.m[0][3] * m2.m[3][2];
 100.125 +	res.m[0][3] = m1.m[0][0] * m2.m[0][3] + m1.m[0][1] * m2.m[1][3] + m1.m[0][2] * m2.m[2][3] + m1.m[0][3] * m2.m[3][3];
 100.126 +
 100.127 +	res.m[1][0] = m1.m[1][0] * m2.m[0][0] + m1.m[1][1] * m2.m[1][0] + m1.m[1][2] * m2.m[2][0] + m1.m[1][3] * m2.m[3][0];
 100.128 +	res.m[1][1] = m1.m[1][0] * m2.m[0][1] + m1.m[1][1] * m2.m[1][1] + m1.m[1][2] * m2.m[2][1] + m1.m[1][3] * m2.m[3][1];
 100.129 +	res.m[1][2] = m1.m[1][0] * m2.m[0][2] + m1.m[1][1] * m2.m[1][2] + m1.m[1][2] * m2.m[2][2] + m1.m[1][3] * m2.m[3][2];
 100.130 +	res.m[1][3] = m1.m[1][0] * m2.m[0][3] + m1.m[1][1] * m2.m[1][3] + m1.m[1][2] * m2.m[2][3] + m1.m[1][3] * m2.m[3][3];
 100.131 +
 100.132 +	res.m[2][0] = m1.m[2][0] * m2.m[0][0] + m1.m[2][1] * m2.m[1][0] + m1.m[2][2] * m2.m[2][0] + m1.m[2][3] * m2.m[3][0];
 100.133 +	res.m[2][1] = m1.m[2][0] * m2.m[0][1] + m1.m[2][1] * m2.m[1][1] + m1.m[2][2] * m2.m[2][1] + m1.m[2][3] * m2.m[3][1];
 100.134 +	res.m[2][2] = m1.m[2][0] * m2.m[0][2] + m1.m[2][1] * m2.m[1][2] + m1.m[2][2] * m2.m[2][2] + m1.m[2][3] * m2.m[3][2];
 100.135 +	res.m[2][3] = m1.m[2][0] * m2.m[0][3] + m1.m[2][1] * m2.m[1][3] + m1.m[2][2] * m2.m[2][3] + m1.m[2][3] * m2.m[3][3];
 100.136 +
 100.137 +	res.m[3][0] = m1.m[3][0] * m2.m[0][0] + m1.m[3][1] * m2.m[1][0] + m1.m[3][2] * m2.m[2][0] + m1.m[3][3] * m2.m[3][0];
 100.138 +	res.m[3][1] = m1.m[3][0] * m2.m[0][1] + m1.m[3][1] * m2.m[1][1] + m1.m[3][2] * m2.m[2][1] + m1.m[3][3] * m2.m[3][1];
 100.139 +	res.m[3][2] = m1.m[3][0] * m2.m[0][2] + m1.m[3][1] * m2.m[1][2] + m1.m[3][2] * m2.m[2][2] + m1.m[3][3] * m2.m[3][2];
 100.140 +	res.m[3][3] = m1.m[3][0] * m2.m[0][3] + m1.m[3][1] * m2.m[1][3] + m1.m[3][2] * m2.m[2][3] + m1.m[3][3] * m2.m[3][3];
 100.141 +
 100.142 +	return res;
 100.143 +}
 100.144 +
 100.145 +inline scalar_t *Matrix3x3::operator [](int index) {
 100.146 +	return m[index];
 100.147 +}
 100.148 +
 100.149 +inline const scalar_t *Matrix3x3::operator [](int index) const {
 100.150 +	return m[index];
 100.151 +}
 100.152 +
 100.153 +inline void Matrix3x3::reset_identity() {
 100.154 +	memcpy(this->m, identity.m, 9 * sizeof(scalar_t));
 100.155 +}
 100.156 +
 100.157 +inline scalar_t *Matrix4x4::operator [](int index) {
 100.158 +	return m[index];
 100.159 +}
 100.160 +
 100.161 +inline const scalar_t *Matrix4x4::operator [](int index) const {
 100.162 +	return m[index];
 100.163 +}
 100.164 +
 100.165 +inline void Matrix4x4::reset_identity() {
 100.166 +	memcpy(this->m, identity.m, 16 * sizeof(scalar_t));
 100.167 +}
 100.168 +#endif	/* __cplusplus */
   101.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   101.2 +++ b/libs/vmath/matrix_c.c	Sat Sep 19 05:51:51 2015 +0300
   101.3 @@ -0,0 +1,265 @@
   101.4 +/*
   101.5 +libvmath - a vector math library
   101.6 +Copyright (C) 2004-2011 John Tsiombikas <nuclear@member.fsf.org>
   101.7 +
   101.8 +This program is free software: you can redistribute it and/or modify
   101.9 +it under the terms of the GNU Lesser General Public License as published
  101.10 +by the Free Software Foundation, either version 3 of the License, or
  101.11 +(at your option) any later version.
  101.12 +
  101.13 +This program is distributed in the hope that it will be useful,
  101.14 +but WITHOUT ANY WARRANTY; without even the implied warranty of
  101.15 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  101.16 +GNU Lesser General Public License for more details.
  101.17 +
  101.18 +You should have received a copy of the GNU Lesser General Public License
  101.19 +along with this program.  If not, see <http://www.gnu.org/licenses/>.
  101.20 +*/
  101.21 +
  101.22 +
  101.23 +#include <stdio.h>
  101.24 +#include "matrix.h"
  101.25 +#include "vector.h"
  101.26 +#include "quat.h"
  101.27 +
  101.28 +void m3_to_m4(mat4_t dest, mat3_t src)
  101.29 +{
  101.30 +	int i, j;
  101.31 +
  101.32 +	memset(dest, 0, sizeof(mat4_t));
  101.33 +	for(i=0; i<3; i++) {
  101.34 +		for(j=0; j<3; j++) {
  101.35 +			dest[i][j] = src[i][j];
  101.36 +		}
  101.37 +	}
  101.38 +	dest[3][3] = 1.0;
  101.39 +}
  101.40 +
  101.41 +void m3_print(FILE *fp, mat3_t m)
  101.42 +{
  101.43 +	int i;
  101.44 +	for(i=0; i<3; i++) {
  101.45 +		fprintf(fp, "[ %12.5f %12.5f %12.5f ]\n", (float)m[i][0], (float)m[i][1], (float)m[i][2]);
  101.46 +	}
  101.47 +}
  101.48 +
  101.49 +/* C matrix 4x4 functions */
  101.50 +void m4_to_m3(mat3_t dest, mat4_t src)
  101.51 +{
  101.52 +	int i, j;
  101.53 +	for(i=0; i<3; i++) {
  101.54 +		for(j=0; j<3; j++) {
  101.55 +			dest[i][j] = src[i][j];
  101.56 +		}
  101.57 +	}
  101.58 +}
  101.59 +
  101.60 +void m4_translate(mat4_t m, scalar_t x, scalar_t y, scalar_t z)
  101.61 +{
  101.62 +	mat4_t tm;
  101.63 +	m4_identity(tm);
  101.64 +	tm[0][3] = x;
  101.65 +	tm[1][3] = y;
  101.66 +	tm[2][3] = z;
  101.67 +	m4_mult(m, m, tm);
  101.68 +}
  101.69 +
  101.70 +void m4_rotate(mat4_t m, scalar_t x, scalar_t y, scalar_t z)
  101.71 +{
  101.72 +	m4_rotate_x(m, x);
  101.73 +	m4_rotate_y(m, y);
  101.74 +	m4_rotate_z(m, z);
  101.75 +}
  101.76 +
  101.77 +void m4_rotate_x(mat4_t m, scalar_t angle)
  101.78 +{
  101.79 +	mat4_t rm;
  101.80 +	m4_identity(rm);
  101.81 +	rm[1][1] = cos(angle); rm[1][2] = -sin(angle);
  101.82 +	rm[2][1] = sin(angle); rm[2][2] = cos(angle);
  101.83 +	m4_mult(m, m, rm);
  101.84 +}
  101.85 +
  101.86 +void m4_rotate_y(mat4_t m, scalar_t angle)
  101.87 +{
  101.88 +	mat4_t rm;
  101.89 +	m4_identity(rm);
  101.90 +	rm[0][0] = cos(angle); rm[0][2] = sin(angle);
  101.91 +	rm[2][0] = -sin(angle); rm[2][2] = cos(angle);
  101.92 +	m4_mult(m, m, rm);
  101.93 +}
  101.94 +
  101.95 +void m4_rotate_z(mat4_t m, scalar_t angle)
  101.96 +{
  101.97 +	mat4_t rm;
  101.98 +	m4_identity(rm);
  101.99 +	rm[0][0] = cos(angle); rm[0][1] = -sin(angle);
 101.100 +	rm[1][0] = sin(angle); rm[1][1] = cos(angle);
 101.101 +	m4_mult(m, m, rm);
 101.102 +}
 101.103 +
 101.104 +void m4_rotate_axis(mat4_t m, scalar_t angle, scalar_t x, scalar_t y, scalar_t z)
 101.105 +{
 101.106 +	mat4_t xform;
 101.107 +	scalar_t sina = sin(angle);
 101.108 +	scalar_t cosa = cos(angle);
 101.109 +	scalar_t one_minus_cosa = 1.0 - cosa;
 101.110 +	scalar_t nxsq = x * x;
 101.111 +	scalar_t nysq = y * y;
 101.112 +	scalar_t nzsq = z * z;
 101.113 +
 101.114 +	m4_identity(xform);
 101.115 +	xform[0][0] = nxsq + (1.0 - nxsq) * cosa;
 101.116 +	xform[0][1] = x * y * one_minus_cosa - z * sina;
 101.117 +	xform[0][2] = x * z * one_minus_cosa + y * sina;
 101.118 +	xform[1][0] = x * y * one_minus_cosa + z * sina;
 101.119 +	xform[1][1] = nysq + (1.0 - nysq) * cosa;
 101.120 +	xform[1][2] = y * z * one_minus_cosa - x * sina;
 101.121 +	xform[2][0] = x * z * one_minus_cosa - y * sina;
 101.122 +	xform[2][1] = y * z * one_minus_cosa + x * sina;
 101.123 +	xform[2][2] = nzsq + (1.0 - nzsq) * cosa;
 101.124 +
 101.125 +	m4_mult(m, m, xform);
 101.126 +}
 101.127 +
 101.128 +void m4_rotate_quat(mat4_t m, quat_t q)
 101.129 +{
 101.130 +	mat4_t rm;
 101.131 +	quat_to_mat4(rm, q);
 101.132 +	m4_mult(m, m, rm);
 101.133 +}
 101.134 +
 101.135 +void m4_scale(mat4_t m, scalar_t x, scalar_t y, scalar_t z)
 101.136 +{
 101.137 +	mat4_t sm;
 101.138 +	m4_identity(sm);
 101.139 +	sm[0][0] = x;
 101.140 +	sm[1][1] = y;
 101.141 +	sm[2][2] = z;
 101.142 +	m4_mult(m, m, sm);
 101.143 +}
 101.144 +
 101.145 +void m4_transpose(mat4_t res, mat4_t m)
 101.146 +{
 101.147 +	int i, j;
 101.148 +	mat4_t tmp;
 101.149 +	m4_copy(tmp, m);
 101.150 +
 101.151 +	for(i=0; i<4; i++) {
 101.152 +		for(j=0; j<4; j++) {
 101.153 +			res[i][j] = tmp[j][i];
 101.154 +		}
 101.155 +	}
 101.156 +}
 101.157 +
 101.158 +scalar_t m4_determinant(mat4_t m)
 101.159 +{
 101.160 +	scalar_t det11 =	(m[1][1] * (m[2][2] * m[3][3] - m[3][2] * m[2][3])) -
 101.161 +						(m[1][2] * (m[2][1] * m[3][3] - m[3][1] * m[2][3])) +
 101.162 +						(m[1][3] * (m[2][1] * m[3][2] - m[3][1] * m[2][2]));
 101.163 +
 101.164 +	scalar_t det12 =	(m[1][0] * (m[2][2] * m[3][3] - m[3][2] * m[2][3])) -
 101.165 +						(m[1][2] * (m[2][0] * m[3][3] - m[3][0] * m[2][3])) +
 101.166 +						(m[1][3] * (m[2][0] * m[3][2] - m[3][0] * m[2][2]));
 101.167 +
 101.168 +	scalar_t det13 =	(m[1][0] * (m[2][1] * m[3][3] - m[3][1] * m[2][3])) -
 101.169 +						(m[1][1] * (m[2][0] * m[3][3] - m[3][0] * m[2][3])) +
 101.170 +						(m[1][3] * (m[2][0] * m[3][1] - m[3][0] * m[2][1]));
 101.171 +
 101.172 +	scalar_t det14 =	(m[1][0] * (m[2][1] * m[3][2] - m[3][1] * m[2][2])) -
 101.173 +						(m[1][1] * (m[2][0] * m[3][2] - m[3][0] * m[2][2])) +
 101.174 +						(m[1][2] * (m[2][0] * m[3][1] - m[3][0] * m[2][1]));
 101.175 +
 101.176 +	return m[0][0] * det11 - m[0][1] * det12 + m[0][2] * det13 - m[0][3] * det14;
 101.177 +}
 101.178 +
 101.179 +void m4_adjoint(mat4_t res, mat4_t m)
 101.180 +{
 101.181 +	int i, j;
 101.182 +	mat4_t coef;
 101.183 +
 101.184 +	coef[0][0] =	(m[1][1] * (m[2][2] * m[3][3] - m[3][2] * m[2][3])) -
 101.185 +					(m[1][2] * (m[2][1] * m[3][3] - m[3][1] * m[2][3])) +
 101.186 +					(m[1][3] * (m[2][1] * m[3][2] - m[3][1] * m[2][2]));
 101.187 +	coef[0][1] =	(m[1][0] * (m[2][2] * m[3][3] - m[3][2] * m[2][3])) -
 101.188 +					(m[1][2] * (m[2][0] * m[3][3] - m[3][0] * m[2][3])) +
 101.189 +					(m[1][3] * (m[2][0] * m[3][2] - m[3][0] * m[2][2]));
 101.190 +	coef[0][2] =	(m[1][0] * (m[2][1] * m[3][3] - m[3][1] * m[2][3])) -
 101.191 +					(m[1][1] * (m[2][0] * m[3][3] - m[3][0] * m[2][3])) +
 101.192 +					(m[1][3] * (m[2][0] * m[3][1] - m[3][0] * m[2][1]));
 101.193 +	coef[0][3] =	(m[1][0] * (m[2][1] * m[3][2] - m[3][1] * m[2][2])) -
 101.194 +					(m[1][1] * (m[2][0] * m[3][2] - m[3][0] * m[2][2])) +
 101.195 +					(m[1][2] * (m[2][0] * m[3][1] - m[3][0] * m[2][1]));
 101.196 +
 101.197 +	coef[1][0] =	(m[0][1] * (m[2][2] * m[3][3] - m[3][2] * m[2][3])) -
 101.198 +					(m[0][2] * (m[2][1] * m[3][3] - m[3][1] * m[2][3])) +
 101.199 +					(m[0][3] * (m[2][1] * m[3][2] - m[3][1] * m[2][2]));
 101.200 +	coef[1][1] =	(m[0][0] * (m[2][2] * m[3][3] - m[3][2] * m[2][3])) -
 101.201 +					(m[0][2] * (m[2][0] * m[3][3] - m[3][0] * m[2][3])) +
 101.202 +					(m[0][3] * (m[2][0] * m[3][2] - m[3][0] * m[2][2]));
 101.203 +	coef[1][2] =	(m[0][0] * (m[2][1] * m[3][3] - m[3][1] * m[2][3])) -
 101.204 +					(m[0][1] * (m[2][0] * m[3][3] - m[3][0] * m[2][3])) +
 101.205 +					(m[0][3] * (m[2][0] * m[3][1] - m[3][0] * m[2][1]));
 101.206 +	coef[1][3] =	(m[0][0] * (m[2][1] * m[3][2] - m[3][1] * m[2][2])) -
 101.207 +					(m[0][1] * (m[2][0] * m[3][2] - m[3][0] * m[2][2])) +
 101.208 +					(m[0][2] * (m[2][0] * m[3][1] - m[3][0] * m[2][1]));
 101.209 +
 101.210 +	coef[2][0] =	(m[0][1] * (m[1][2] * m[3][3] - m[3][2] * m[1][3])) -
 101.211 +					(m[0][2] * (m[1][1] * m[3][3] - m[3][1] * m[1][3])) +
 101.212 +					(m[0][3] * (m[1][1] * m[3][2] - m[3][1] * m[1][2]));
 101.213 +	coef[2][1] =	(m[0][0] * (m[1][2] * m[3][3] - m[3][2] * m[1][3])) -
 101.214 +					(m[0][2] * (m[1][0] * m[3][3] - m[3][0] * m[1][3])) +
 101.215 +					(m[0][3] * (m[1][0] * m[3][2] - m[3][0] * m[1][2]));
 101.216 +	coef[2][2] =	(m[0][0] * (m[1][1] * m[3][3] - m[3][1] * m[1][3])) -
 101.217 +					(m[0][1] * (m[1][0] * m[3][3] - m[3][0] * m[1][3])) +
 101.218 +					(m[0][3] * (m[1][0] * m[3][1] - m[3][0] * m[1][1]));
 101.219 +	coef[2][3] =	(m[0][0] * (m[1][1] * m[3][2] - m[3][1] * m[1][2])) -
 101.220 +					(m[0][1] * (m[1][0] * m[3][2] - m[3][0] * m[1][2])) +
 101.221 +					(m[0][2] * (m[1][0] * m[3][1] - m[3][0] * m[1][1]));
 101.222 +
 101.223 +	coef[3][0] =	(m[0][1] * (m[1][2] * m[2][3] - m[2][2] * m[1][3])) -
 101.224 +					(m[0][2] * (m[1][1] * m[2][3] - m[2][1] * m[1][3])) +
 101.225 +					(m[0][3] * (m[1][1] * m[2][2] - m[2][1] * m[1][2]));
 101.226 +	coef[3][1] =	(m[0][0] * (m[1][2] * m[2][3] - m[2][2] * m[1][3])) -
 101.227 +					(m[0][2] * (m[1][0] * m[2][3] - m[2][0] * m[1][3])) +
 101.228 +					(m[0][3] * (m[1][0] * m[2][2] - m[2][0] * m[1][2]));
 101.229 +	coef[3][2] =	(m[0][0] * (m[1][1] * m[2][3] - m[2][1] * m[1][3])) -
 101.230 +					(m[0][1] * (m[1][0] * m[2][3] - m[2][0] * m[1][3])) +
 101.231 +					(m[0][3] * (m[1][0] * m[2][1] - m[2][0] * m[1][1]));
 101.232 +	coef[3][3] =	(m[0][0] * (m[1][1] * m[2][2] - m[2][1] * m[1][2])) -
 101.233 +					(m[0][1] * (m[1][0] * m[2][2] - m[2][0] * m[1][2])) +
 101.234 +					(m[0][2] * (m[1][0] * m[2][1] - m[2][0] * m[1][1]));
 101.235 +
 101.236 +	m4_transpose(res, coef);
 101.237 +
 101.238 +	for(i=0; i<4; i++) {
 101.239 +		for(j=0; j<4; j++) {
 101.240 +			res[i][j] = j % 2 ? -res[i][j] : res[i][j];
 101.241 +			if(i % 2) res[i][j] = -res[i][j];
 101.242 +		}
 101.243 +	}
 101.244 +}
 101.245 +
 101.246 +void m4_inverse(mat4_t res, mat4_t m)
 101.247 +{
 101.248 +	int i, j;
 101.249 +	mat4_t adj;
 101.250 +	scalar_t det;
 101.251 +
 101.252 +	m4_adjoint(adj, m);
 101.253 +	det = m4_determinant(m);
 101.254 +	
 101.255 +	for(i=0; i<4; i++) {
 101.256 +		for(j=0; j<4; j++) {
 101.257 +			res[i][j] = adj[i][j] / det;
 101.258 +		}
 101.259 +	}
 101.260 +}
 101.261 +
 101.262 +void m4_print(FILE *fp, mat4_t m)
 101.263 +{
 101.264 +	int i;
 101.265 +	for(i=0; i<4; i++) {
 101.266 +		fprintf(fp, "[ %12.5f %12.5f %12.5f %12.5f ]\n", (float)m[i][0], (float)m[i][1], (float)m[i][2], (float)m[i][3]);
 101.267 +	}
 101.268 +}
   102.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   102.2 +++ b/libs/vmath/quat.h	Sat Sep 19 05:51:51 2015 +0300
   102.3 @@ -0,0 +1,119 @@
   102.4 +/*
   102.5 +libvmath - a vector math library
   102.6 +Copyright (C) 2004-2011 John Tsiombikas <nuclear@member.fsf.org>
   102.7 +
   102.8 +This program is free software: you can redistribute it and/or modify
   102.9 +it under the terms of the GNU Lesser General Public License as published
  102.10 +by the Free Software Foundation, either version 3 of the License, or
  102.11 +(at your option) any later version.
  102.12 +
  102.13 +This program is distributed in the hope that it will be useful,
  102.14 +but WITHOUT ANY WARRANTY; without even the implied warranty of
  102.15 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  102.16 +GNU Lesser General Public License for more details.
  102.17 +
  102.18 +You should have received a copy of the GNU Lesser General Public License
  102.19 +along with this program.  If not, see <http://www.gnu.org/licenses/>.
  102.20 +*/
  102.21 +
  102.22 +#ifndef VMATH_QUATERNION_H_
  102.23 +#define VMATH_QUATERNION_H_
  102.24 +
  102.25 +#include <stdio.h>
  102.26 +#include "vmath_types.h"
  102.27 +#include "vector.h"
  102.28 +
  102.29 +#ifdef __cplusplus
  102.30 +extern "C" {
  102.31 +#endif	/* __cplusplus */
  102.32 +
  102.33 +#define quat_cons(s, x, y, z)	v4_cons(x, y, z, s)
  102.34 +#define quat_vec(q)				v3_cons((q).x, (q).y, (q).z)
  102.35 +#define quat_s(q)				((q).w)
  102.36 +#define quat_identity()			quat_cons(1.0, 0.0, 0.0, 0.0)
  102.37 +void quat_print(FILE *fp, quat_t q);
  102.38 +
  102.39 +#define quat_add		v4_add
  102.40 +#define quat_sub		v4_sub
  102.41 +#define quat_neg		v4_neg
  102.42 +
  102.43 +static inline quat_t quat_mul(quat_t q1, quat_t q2);
  102.44 +
  102.45 +static inline quat_t quat_conjugate(quat_t q);
  102.46 +
  102.47 +#define quat_length		v4_length
  102.48 +#define quat_length_sq	v4_length_sq
  102.49 +
  102.50 +#define quat_normalize	v4_normalize
  102.51 +static inline quat_t quat_inverse(quat_t q);
  102.52 +
  102.53 +quat_t quat_rotate(quat_t q, scalar_t angle, scalar_t x, scalar_t y, scalar_t z);
  102.54 +quat_t quat_rotate_quat(quat_t q, quat_t rotq);
  102.55 +
  102.56 +static inline void quat_to_mat3(mat3_t res, quat_t q);
  102.57 +static inline void quat_to_mat4(mat4_t res, quat_t q);
  102.58 +
  102.59 +#define quat_lerp quat_slerp
  102.60 +quat_t quat_slerp(quat_t q1, quat_t q2, scalar_t t);
  102.61 +
  102.62 +
  102.63 +#ifdef __cplusplus
  102.64 +}	/* extern "C" */
  102.65 +
  102.66 +#include <iostream>
  102.67 +
  102.68 +/* Quaternion */
  102.69 +class Quaternion {
  102.70 +public:
  102.71 +	scalar_t s;
  102.72 +	Vector3 v;
  102.73 +
  102.74 +	Quaternion();
  102.75 +	Quaternion(scalar_t s, const Vector3 &v);
  102.76 +	Quaternion(scalar_t s, scalar_t x, scalar_t y, scalar_t z);
  102.77 +	Quaternion(const Vector3 &axis, scalar_t angle);
  102.78 +	Quaternion(const quat_t &quat);
  102.79 +
  102.80 +	Quaternion operator +(const Quaternion &quat) const;
  102.81 +	Quaternion operator -(const Quaternion &quat) const;
  102.82 +	Quaternion operator -() const;
  102.83 +	Quaternion operator *(const Quaternion &quat) const;
  102.84 +
  102.85 +	void operator +=(const Quaternion &quat);
  102.86 +	void operator -=(const Quaternion &quat);
  102.87 +	void operator *=(const Quaternion &quat);
  102.88 +
  102.89 +	void reset_identity();
  102.90 +
  102.91 +	Quaternion conjugate() const;
  102.92 +
  102.93 +	scalar_t length() const;
  102.94 +	scalar_t length_sq() const;
  102.95 +
  102.96 +	void normalize();
  102.97 +	Quaternion normalized() const;
  102.98 +
  102.99 +	Quaternion inverse() const;
 102.100 +
 102.101 +	void set_rotation(const Vector3 &axis, scalar_t angle);
 102.102 +	void rotate(const Vector3 &axis, scalar_t angle);
 102.103 +	/* note: this is a totally different operation from the above
 102.104 +	 * this treats the quaternion as signifying direction and rotates
 102.105 +	 * it by a rotation quaternion by rot * q * rot'
 102.106 +	 */
 102.107 +	void rotate(const Quaternion &q);
 102.108 +
 102.109 +	Matrix3x3 get_rotation_matrix() const;
 102.110 +
 102.111 +	friend Quaternion slerp(const Quaternion &q1, const Quaternion &q2, scalar_t t);
 102.112 +
 102.113 +	friend std::ostream &operator <<(std::ostream &out, const Quaternion &q);
 102.114 +};
 102.115 +
 102.116 +Quaternion slerp(const Quaternion &q1, const Quaternion &q2, scalar_t t);
 102.117 +inline Quaternion lerp(const Quaternion &q1, const Quaternion &q2, scalar_t t);
 102.118 +#endif	/* __cplusplus */
 102.119 +
 102.120 +#include "quat.inl"
 102.121 +
 102.122 +#endif	/* VMATH_QUATERNION_H_ */
   103.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   103.2 +++ b/libs/vmath/quat.inl	Sat Sep 19 05:51:51 2015 +0300
   103.3 @@ -0,0 +1,81 @@
   103.4 +/*
   103.5 +libvmath - a vector math library
   103.6 +Copyright (C) 2004-2011 John Tsiombikas <nuclear@member.fsf.org>
   103.7 +
   103.8 +This program is free software: you can redistribute it and/or modify
   103.9 +it under the terms of the GNU Lesser General Public License as published
  103.10 +by the Free Software Foundation, either version 3 of the License, or
  103.11 +(at your option) any later version.
  103.12 +
  103.13 +This program is distributed in the hope that it will be useful,
  103.14 +but WITHOUT ANY WARRANTY; without even the implied warranty of
  103.15 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  103.16 +GNU Lesser General Public License for more details.
  103.17 +
  103.18 +You should have received a copy of the GNU Lesser General Public License
  103.19 +along with this program.  If not, see <http://www.gnu.org/licenses/>.
  103.20 +*/
  103.21 +
  103.22 +#include "vector.h"
  103.23 +#include "matrix.h"
  103.24 +
  103.25 +#ifdef __cplusplus
  103.26 +extern "C" {
  103.27 +#endif	/* __cplusplus */
  103.28 +
  103.29 +static inline quat_t quat_mul(quat_t q1, quat_t q2)
  103.30 +{
  103.31 +	quat_t res;
  103.32 +	vec3_t v1 = quat_vec(q1);
  103.33 +	vec3_t v2 = quat_vec(q2);
  103.34 +
  103.35 +	res.w = q1.w * q2.w - v3_dot(v1, v2);
  103.36 +	/* resvec = v2 * q1 + v1 * q2 + cross(v1, v2) */
  103.37 +	res.x = v2.x * q1.w + v1.x * q2.w + (v1.y * v2.z - v1.z * v2.y);
  103.38 +	res.y = v2.y * q1.w + v1.y * q2.w + (v1.z * v2.x - v1.x * v2.z);
  103.39 +	res.z = v2.z * q1.w + v1.z * q2.w + (v1.x * v2.y - v1.y * v2.x);
  103.40 +	return res;
  103.41 +}
  103.42 +
  103.43 +static inline quat_t quat_conjugate(quat_t q)
  103.44 +{
  103.45 +	q.x = -q.x;
  103.46 +	q.y = -q.y;
  103.47 +	q.z = -q.z;
  103.48 +	return q;
  103.49 +}
  103.50 +
  103.51 +static inline quat_t quat_inverse(quat_t q)
  103.52 +{
  103.53 +	scalar_t lensq = quat_length_sq(q);
  103.54 +	q = quat_conjugate(q);
  103.55 +	q.x /= lensq;
  103.56 +	q.y /= lensq;
  103.57 +	q.z /= lensq;
  103.58 +	q.w /= lensq;
  103.59 +	return q;
  103.60 +}
  103.61 +
  103.62 +static inline void quat_to_mat3(mat3_t res, quat_t q)
  103.63 +{
  103.64 +	m3_cons(res, 1.0 - 2.0 * q.y*q.y - 2.0 * q.z*q.z, 2.0 * q.x * q.y + 2.0 * q.w * q.z,   2.0 * q.z * q.x - 2.0 * q.w * q.y,
  103.65 +				 2.0 * q.x * q.y - 2.0 * q.w * q.z,   1.0 - 2.0 * q.x*q.x - 2.0 * q.z*q.z, 2.0 * q.y * q.z + 2.0 * q.w * q.x,
  103.66 +				 2.0 * q.z * q.x + 2.0 * q.w * q.y,   2.0 * q.y * q.z - 2.0 * q.w * q.x,   1.0 - 2.0 * q.x*q.x - 2.0 * q.y*q.y);
  103.67 +}
  103.68 +
  103.69 +static inline void quat_to_mat4(mat4_t res, quat_t q)
  103.70 +{
  103.71 +	m4_cons(res, 1.0 - 2.0 * q.y*q.y - 2.0 * q.z*q.z, 2.0 * q.x * q.y + 2.0 * q.w * q.z,   2.0 * q.z * q.x - 2.0 * q.w * q.y,   0,
  103.72 +				 2.0 * q.x * q.y - 2.0 * q.w * q.z,   1.0 - 2.0 * q.x*q.x - 2.0 * q.z*q.z, 2.0 * q.y * q.z + 2.0 * q.w * q.x,   0,
  103.73 +				 2.0 * q.z * q.x + 2.0 * q.w * q.y,   2.0 * q.y * q.z - 2.0 * q.w * q.x,   1.0 - 2.0 * q.x*q.x - 2.0 * q.y*q.y, 0,
  103.74 +				 0, 0, 0, 1);
  103.75 +}
  103.76 +
  103.77 +#ifdef __cplusplus
  103.78 +}	/* extern "C" */
  103.79 +
  103.80 +inline Quaternion lerp(const Quaternion &a, const Quaternion &b, scalar_t t)
  103.81 +{
  103.82 +	return slerp(a, b, t);
  103.83 +}
  103.84 +#endif	/* __cplusplus */
   104.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   104.2 +++ b/libs/vmath/quat_c.c	Sat Sep 19 05:51:51 2015 +0300
   104.3 @@ -0,0 +1,61 @@
   104.4 +/*
   104.5 +libvmath - a vector math library
   104.6 +Copyright (C) 2004-2011 John Tsiombikas <nuclear@member.fsf.org>
   104.7 +
   104.8 +This program is free software: you can redistribute it and/or modify
   104.9 +it under the terms of the GNU Lesser General Public License as published
  104.10 +by the Free Software Foundation, either version 3 of the License, or
  104.11 +(at your option) any later version.
  104.12 +
  104.13 +This program is distributed in the hope that it will be useful,
  104.14 +but WITHOUT ANY WARRANTY; without even the implied warranty of
  104.15 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  104.16 +GNU Lesser General Public License for more details.
  104.17 +
  104.18 +You should have received a copy of the GNU Lesser General Public License
  104.19 +along with this program.  If not, see <http://www.gnu.org/licenses/>.
  104.20 +*/
  104.21 +
  104.22 +
  104.23 +#include <stdio.h>
  104.24 +#include <math.h>
  104.25 +#include "quat.h"
  104.26 +
  104.27 +void quat_print(FILE *fp, quat_t q)
  104.28 +{
  104.29 +	fprintf(fp, "([ %.4f %.4f %.4f ] %.4f)", q.x, q.y, q.z, q.w);
  104.30 +}
  104.31 +
  104.32 +quat_t quat_rotate(quat_t q, scalar_t angle, scalar_t x, scalar_t y, scalar_t z)
  104.33 +{
  104.34 +	quat_t rq;
  104.35 +	scalar_t half_angle = angle * 0.5;
  104.36 +	scalar_t sin_half = sin(half_angle);
  104.37 +
  104.38 +	rq.w = cos(half_angle);
  104.39 +	rq.x = x * sin_half;
  104.40 +	rq.y = y * sin_half;
  104.41 +	rq.z = z * sin_half;
  104.42 +
  104.43 +	return quat_mul(q, rq);
  104.44 +}
  104.45 +
  104.46 +quat_t quat_rotate_quat(quat_t q, quat_t rotq)
  104.47 +{
  104.48 +	return quat_mul(quat_mul(rotq, q), quat_conjugate(rotq));
  104.49 +}
  104.50 +
  104.51 +quat_t quat_slerp(quat_t q1, quat_t q2, scalar_t t)
  104.52 +{
  104.53 +	quat_t res;
  104.54 +	scalar_t angle = acos(q1.w * q2.w + q1.x * q2.x + q1.y * q2.y + q1.z * q2.z);
  104.55 +	scalar_t a = sin((1.0f - t) * angle);
  104.56 +	scalar_t b = sin(t * angle);
  104.57 +	scalar_t c = sin(angle);
  104.58 +
  104.59 +	res.x = (q1.x * a + q2.x * b) / c;
  104.60 +	res.y = (q1.y * a + q2.y * b) / c;
  104.61 +	res.z = (q1.z * a + q2.z * b) / c;
  104.62 +	res.w = (q1.w * a + q2.w * b) / c;
  104.63 +	return quat_normalize(res);
  104.64 +}
   105.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   105.2 +++ b/libs/vmath/ray.h	Sat Sep 19 05:51:51 2015 +0300
   105.3 @@ -0,0 +1,73 @@
   105.4 +/*
   105.5 +libvmath - a vector math library
   105.6 +Copyright (C) 2004-2011 John Tsiombikas <nuclear@member.fsf.org>
   105.7 +
   105.8 +This program is free software: you can redistribute it and/or modify
   105.9 +it under the terms of the GNU Lesser General Public License as published
  105.10 +by the Free Software Foundation, either version 3 of the License, or
  105.11 +(at your option) any later version.
  105.12 +
  105.13 +This program is distributed in the hope that it will be useful,
  105.14 +but WITHOUT ANY WARRANTY; without even the implied warranty of
  105.15 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  105.16 +GNU Lesser General Public License for more details.
  105.17 +
  105.18 +You should have received a copy of the GNU Lesser General Public License
  105.19 +along with this program.  If not, see <http://www.gnu.org/licenses/>.
  105.20 +*/
  105.21 +
  105.22 +#ifndef VMATH_RAY_H_
  105.23 +#define VMATH_RAY_H_
  105.24 +
  105.25 +#include "matrix.h"
  105.26 +#include "vector.h"
  105.27 +
  105.28 +typedef struct {
  105.29 +	vec3_t origin, dir;
  105.30 +} ray_t;
  105.31 +
  105.32 +#ifdef __cplusplus
  105.33 +extern "C" {
  105.34 +#endif	/* __cplusplus */
  105.35 +
  105.36 +static inline ray_t ray_cons(vec3_t origin, vec3_t dir);
  105.37 +ray_t ray_transform(ray_t r, mat4_t m);
  105.38 +
  105.39 +#ifdef __cplusplus
  105.40 +}	/* __cplusplus */
  105.41 +
  105.42 +#include <stack>
  105.43 +
  105.44 +class Ray {
  105.45 +private:
  105.46 +	std::stack<scalar_t> ior_stack;
  105.47 +
  105.48 +public:
  105.49 +	/* enviornmental index of refraction, normally 1.0 */
  105.50 +	static scalar_t env_ior;
  105.51 +	
  105.52 +	Vector3 origin, dir;
  105.53 +	scalar_t energy;
  105.54 +	int iter;
  105.55 +	scalar_t ior;
  105.56 +	long time;
  105.57 +
  105.58 +	Ray();
  105.59 +	Ray(const Vector3 &origin, const Vector3 &dir);
  105.60 +
  105.61 +	void transform(const Matrix4x4 &xform);
  105.62 +	Ray transformed(const Matrix4x4 &xform) const;
  105.63 +
  105.64 +	void enter(scalar_t new_ior);
  105.65 +	void leave();
  105.66 +
  105.67 +	scalar_t calc_ior(bool entering, scalar_t mat_ior = 1.0) const;
  105.68 +};
  105.69 +
  105.70 +inline Ray reflect_ray(const Ray &inray, const Vector3 &norm);
  105.71 +inline Ray refract_ray(const Ray &inray, const Vector3 &norm, scalar_t ior, bool entering, scalar_t ray_mag = -1.0);
  105.72 +#endif	/* __cplusplus */
  105.73 +
  105.74 +#include "ray.inl"
  105.75 +
  105.76 +#endif	/* VMATH_RAY_H_ */
   106.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   106.2 +++ b/libs/vmath/ray.inl	Sat Sep 19 05:51:51 2015 +0300
   106.3 @@ -0,0 +1,70 @@
   106.4 +/*
   106.5 +libvmath - a vector math library
   106.6 +Copyright (C) 2004-2011 John Tsiombikas <nuclear@member.fsf.org>
   106.7 +
   106.8 +This program is free software: you can redistribute it and/or modify
   106.9 +it under the terms of the GNU Lesser General Public License as published
  106.10 +by the Free Software Foundation, either version 3 of the License, or
  106.11 +(at your option) any later version.
  106.12 +
  106.13 +This program is distributed in the hope that it will be useful,
  106.14 +but WITHOUT ANY WARRANTY; without even the implied warranty of
  106.15 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  106.16 +GNU Lesser General Public License for more details.
  106.17 +
  106.18 +You should have received a copy of the GNU Lesser General Public License
  106.19 +along with this program.  If not, see <http://www.gnu.org/licenses/>.
  106.20 +*/
  106.21 +
  106.22 +#ifdef __cplusplus
  106.23 +extern "C" {
  106.24 +#endif	/* __cplusplus */
  106.25 +
  106.26 +static inline ray_t ray_cons(vec3_t origin, vec3_t dir)
  106.27 +{
  106.28 +	ray_t r;
  106.29 +	r.origin = origin;
  106.30 +	r.dir = dir;
  106.31 +	return r;
  106.32 +}
  106.33 +
  106.34 +#ifdef __cplusplus
  106.35 +}
  106.36 +
  106.37 +inline Ray reflect_ray(const Ray &inray, const Vector3 &norm)
  106.38 +{
  106.39 +	Ray ray = inray;
  106.40 +	ray.iter--;
  106.41 +	ray.dir = ray.dir.reflection(norm);
  106.42 +	return ray;
  106.43 +}
  106.44 +
  106.45 +inline Ray refract_ray(const Ray &inray, const Vector3 &norm, scalar_t mat_ior, bool entering, scalar_t ray_mag)
  106.46 +{
  106.47 +	Ray ray = inray;
  106.48 +	ray.iter--;
  106.49 +
  106.50 +	scalar_t ior = ray.calc_ior(entering, mat_ior);
  106.51 +	
  106.52 +	if(entering) {
  106.53 +		ray.enter(mat_ior);
  106.54 +	} else {
  106.55 +		ray.leave();
  106.56 +	}
  106.57 +
  106.58 +	if(ray_mag < 0.0) {
  106.59 +		ray_mag = ray.dir.length();
  106.60 +	}
  106.61 +	ray.dir = (ray.dir / ray_mag).refraction(norm, ior) * ray_mag;
  106.62 +
  106.63 +	/* check TIR */
  106.64 +	if(dot_product(ray.dir, norm) > 0.0) {
  106.65 +		if(entering) {
  106.66 +			ray.leave();
  106.67 +		} else {
  106.68 +			ray.enter(mat_ior);
  106.69 +		}
  106.70 +	}
  106.71 +	return ray;
  106.72 +}
  106.73 +#endif	/* __cplusplus */
   107.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   107.2 +++ b/libs/vmath/ray_c.c	Sat Sep 19 05:51:51 2015 +0300
   107.3 @@ -0,0 +1,36 @@
   107.4 +/*
   107.5 +libvmath - a vector math library
   107.6 +Copyright (C) 2004-2011 John Tsiombikas <nuclear@member.fsf.org>
   107.7 +
   107.8 +This program is free software: you can redistribute it and/or modify
   107.9 +it under the terms of the GNU Lesser General Public License as published
  107.10 +by the Free Software Foundation, either version 3 of the License, or
  107.11 +(at your option) any later version.
  107.12 +
  107.13 +This program is distributed in the hope that it will be useful,
  107.14 +but WITHOUT ANY WARRANTY; without even the implied warranty of
  107.15 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  107.16 +GNU Lesser General Public License for more details.
  107.17 +
  107.18 +You should have received a copy of the GNU Lesser General Public License
  107.19 +along with this program.  If not, see <http://www.gnu.org/licenses/>.
  107.20 +*/
  107.21 +
  107.22 +#include "ray.h"
  107.23 +#include "vector.h"
  107.24 +
  107.25 +ray_t ray_transform(ray_t r, mat4_t xform)
  107.26 +{
  107.27 +	mat4_t upper;
  107.28 +	vec3_t dir;
  107.29 +
  107.30 +	m4_copy(upper, xform);
  107.31 +	upper[0][3] = upper[1][3] = upper[2][3] = upper[3][0] = upper[3][1] = upper[3][2] = 0.0;
  107.32 +	upper[3][3] = 1.0;
  107.33 +
  107.34 +	dir = v3_sub(r.dir, r.origin);
  107.35 +	dir = v3_transform(dir, upper);
  107.36 +	r.origin = v3_transform(r.origin, xform);
  107.37 +	r.dir = v3_add(dir, r.origin);
  107.38 +	return r;
  107.39 +}
   108.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   108.2 +++ b/libs/vmath/sphvec.h	Sat Sep 19 05:51:51 2015 +0300
   108.3 @@ -0,0 +1,36 @@
   108.4 +/*
   108.5 +libvmath - a vector math library
   108.6 +Copyright (C) 2004-2011 John Tsiombikas <nuclear@member.fsf.org>
   108.7 +
   108.8 +This program is free software: you can redistribute it and/or modify
   108.9 +it under the terms of the GNU Lesser General Public License as published
  108.10 +by the Free Software Foundation, either version 3 of the License, or
  108.11 +(at your option) any later version.
  108.12 +
  108.13 +This program is distributed in the hope that it will be useful,
  108.14 +but WITHOUT ANY WARRANTY; without even the implied warranty of
  108.15 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  108.16 +GNU Lesser General Public License for more details.
  108.17 +
  108.18 +You should have received a copy of the GNU Lesser General Public License
  108.19 +along with this program.  If not, see <http://www.gnu.org/licenses/>.
  108.20 +*/
  108.21 +
  108.22 +#ifndef VMATH_SPHVEC_H_
  108.23 +#define VMATH_SPHVEC_H_
  108.24 +
  108.25 +#include "vmath_types.h"
  108.26 +
  108.27 +#ifdef __cplusplus
  108.28 +/* Vector in spherical coordinates */
  108.29 +class SphVector {
  108.30 +public:
  108.31 +	scalar_t theta, phi, r;
  108.32 +
  108.33 +	SphVector(scalar_t theta = 0.0, scalar_t phi = 0.0, scalar_t r = 1.0);
  108.34 +	SphVector(const Vector3 &cvec);
  108.35 +	SphVector &operator =(const Vector3 &cvec);
  108.36 +};
  108.37 +#endif	/* __cplusplus */
  108.38 +
  108.39 +#endif	/* VMATH_SPHVEC_H_ */
   109.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   109.2 +++ b/libs/vmath/vector.h	Sat Sep 19 05:51:51 2015 +0300
   109.3 @@ -0,0 +1,292 @@
   109.4 +/*
   109.5 +libvmath - a vector math library
   109.6 +Copyright (C) 2004-2011 John Tsiombikas <nuclear@member.fsf.org>
   109.7 +
   109.8 +This program is free software: you can redistribute it and/or modify
   109.9 +it under the terms of the GNU Lesser General Public License as published
  109.10 +by the Free Software Foundation, either version 3 of the License, or
  109.11 +(at your option) any later version.
  109.12 +
  109.13 +This program is distributed in the hope that it will be useful,
  109.14 +but WITHOUT ANY WARRANTY; without even the implied warranty of
  109.15 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  109.16 +GNU Lesser General Public License for more details.
  109.17 +
  109.18 +You should have received a copy of the GNU Lesser General Public License
  109.19 +along with this program.  If not, see <http://www.gnu.org/licenses/>.
  109.20 +*/
  109.21 +
  109.22 +#ifndef VMATH_VECTOR_H_
  109.23 +#define VMATH_VECTOR_H_
  109.24 +
  109.25 +#include <stdio.h>
  109.26 +#include "vmath_types.h"
  109.27 +
  109.28 +#ifdef __cplusplus
  109.29 +extern "C" {
  109.30 +#endif	/* __cplusplus */
  109.31 +
  109.32 +/* C 2D vector functions */
  109.33 +static inline vec2_t v2_cons(scalar_t x, scalar_t y);
  109.34 +static inline void v2_print(FILE *fp, vec2_t v);
  109.35 +
  109.36 +static inline vec2_t v2_add(vec2_t v1, vec2_t v2);
  109.37 +static inline vec2_t v2_sub(vec2_t v1, vec2_t v2);
  109.38 +static inline vec2_t v2_scale(vec2_t v, scalar_t s);
  109.39 +static inline scalar_t v2_dot(vec2_t v1, vec2_t v2);
  109.40 +static inline scalar_t v2_length(vec2_t v);
  109.41 +static inline scalar_t v2_length_sq(vec2_t v);
  109.42 +static inline vec2_t v2_normalize(vec2_t v);
  109.43 +
  109.44 +static inline vec2_t v2_lerp(vec2_t v1, vec2_t v2, scalar_t t);
  109.45 +
  109.46 +/* C 3D vector functions */
  109.47 +static inline vec3_t v3_cons(scalar_t x, scalar_t y, scalar_t z);
  109.48 +static inline void v3_print(FILE *fp, vec3_t v);
  109.49 +
  109.50 +static inline vec3_t v3_add(vec3_t v1, vec3_t v2);
  109.51 +static inline vec3_t v3_sub(vec3_t v1, vec3_t v2);
  109.52 +static inline vec3_t v3_neg(vec3_t v);
  109.53 +static inline vec3_t v3_mul(vec3_t v1, vec3_t v2);
  109.54 +static inline vec3_t v3_scale(vec3_t v1, scalar_t s);
  109.55 +static inline scalar_t v3_dot(vec3_t v1, vec3_t v2);
  109.56 +static inline vec3_t v3_cross(vec3_t v1, vec3_t v2);
  109.57 +static inline scalar_t v3_length(vec3_t v);
  109.58 +static inline scalar_t v3_length_sq(vec3_t v);
  109.59 +static inline vec3_t v3_normalize(vec3_t v);
  109.60 +static inline vec3_t v3_transform(vec3_t v, mat4_t m);
  109.61 +
  109.62 +static inline vec3_t v3_rotate(vec3_t v, scalar_t x, scalar_t y, scalar_t z);
  109.63 +static inline vec3_t v3_rotate_axis(vec3_t v, scalar_t angle, scalar_t x, scalar_t y, scalar_t z);
  109.64 +static inline vec3_t v3_rotate_quat(vec3_t v, quat_t q);
  109.65 +
  109.66 +static inline vec3_t v3_reflect(vec3_t v, vec3_t n);
  109.67 +
  109.68 +static inline vec3_t v3_lerp(vec3_t v1, vec3_t v2, scalar_t t);
  109.69 +
  109.70 +/* C 4D vector functions */
  109.71 +static inline vec4_t v4_cons(scalar_t x, scalar_t y, scalar_t z, scalar_t w);
  109.72 +static inline void v4_print(FILE *fp, vec4_t v);
  109.73 +
  109.74 +static inline vec4_t v4_add(vec4_t v1, vec4_t v2);
  109.75 +static inline vec4_t v4_sub(vec4_t v1, vec4_t v2);
  109.76 +static inline vec4_t v4_neg(vec4_t v);
  109.77 +static inline vec4_t v4_mul(vec4_t v1, vec4_t v2);
  109.78 +static inline vec4_t v4_scale(vec4_t v, scalar_t s);
  109.79 +static inline scalar_t v4_dot(vec4_t v1, vec4_t v2);
  109.80 +static inline scalar_t v4_length(vec4_t v);
  109.81 +static inline scalar_t v4_length_sq(vec4_t v);
  109.82 +static inline vec4_t v4_normalize(vec4_t v);
  109.83 +static inline vec4_t v4_transform(vec4_t v, mat4_t m);
  109.84 +
  109.85 +#ifdef __cplusplus
  109.86 +}	/* extern "C" */
  109.87 +
  109.88 +/* when included from C++ source files, also define the vector classes */
  109.89 +#include <iostream>
  109.90 +
  109.91 +/** 2D Vector */
  109.92 +class Vector2 {
  109.93 +public:
  109.94 +	scalar_t x, y;
  109.95 +
  109.96 +	explicit Vector2(scalar_t x = 0.0, scalar_t y = 0.0);
  109.97 +	Vector2(const vec2_t &vec);
  109.98 +	Vector2(const Vector3 &vec);
  109.99 +	Vector2(const Vector4 &vec);
 109.100 +
 109.101 +	inline scalar_t &operator [](int elem);
 109.102 +	inline const scalar_t &operator [](int elem) const;
 109.103 +
 109.104 +	inline scalar_t length() const;
 109.105 +	inline scalar_t length_sq() const;
 109.106 +	void normalize();
 109.107 +	Vector2 normalized() const;
 109.108 +
 109.109 +	void transform(const Matrix3x3 &mat);
 109.110 +	Vector2 transformed(const Matrix3x3 &mat) const;
 109.111 +
 109.112 +	void rotate(scalar_t angle);
 109.113 +	Vector2 rotated(scalar_t angle) const;
 109.114 +
 109.115 +	Vector2 reflection(const Vector2 &normal) const;
 109.116 +	Vector2 refraction(const Vector2 &normal, scalar_t src_ior, scalar_t dst_ior) const;
 109.117 +};
 109.118 +
 109.119 +/* unary operations */
 109.120 +inline Vector2 operator -(const Vector2 &vec);
 109.121 +
 109.122 +/* binary vector (op) vector operations */
 109.123 +inline scalar_t dot_product(const Vector2 &v1, const Vector2 &v2);
 109.124 +
 109.125 +inline Vector2 operator +(const Vector2 &v1, const Vector2 &v2);
 109.126 +inline Vector2 operator -(const Vector2 &v1, const Vector2 &v2);
 109.127 +inline Vector2 operator *(const Vector2 &v1, const Vector2 &v2);
 109.128 +inline Vector2 operator /(const Vector2 &v1, const Vector2 &v2);
 109.129 +inline bool operator ==(const Vector2 &v1, const Vector2 &v2);
 109.130 +
 109.131 +inline void operator +=(Vector2 &v1, const Vector2 &v2);
 109.132 +inline void operator -=(Vector2 &v1, const Vector2 &v2);
 109.133 +inline void operator *=(Vector2 &v1, const Vector2 &v2);
 109.134 +inline void operator /=(Vector2 &v1, const Vector2 &v2);
 109.135 +
 109.136 +/* binary vector (op) scalar and scalar (op) vector operations */
 109.137 +inline Vector2 operator +(const Vector2 &vec, scalar_t scalar);
 109.138 +inline Vector2 operator +(scalar_t scalar, const Vector2 &vec);
 109.139 +inline Vector2 operator -(const Vector2 &vec, scalar_t scalar);
 109.140 +inline Vector2 operator *(const Vector2 &vec, scalar_t scalar);
 109.141 +inline Vector2 operator *(scalar_t scalar, const Vector2 &vec);
 109.142 +inline Vector2 operator /(const Vector2 &vec, scalar_t scalar);
 109.143 +
 109.144 +inline void operator +=(Vector2 &vec, scalar_t scalar);
 109.145 +inline void operator -=(Vector2 &vec, scalar_t scalar);
 109.146 +inline void operator *=(Vector2 &vec, scalar_t scalar);
 109.147 +inline void operator /=(Vector2 &vec, scalar_t scalar);
 109.148 +
 109.149 +std::ostream &operator <<(std::ostream &out, const Vector2 &vec);
 109.150 +
 109.151 +inline Vector2 lerp(const Vector2 &a, const Vector2 &b, scalar_t t);
 109.152 +inline Vector2 catmull_rom_spline(const Vector2 &v0, const Vector2 &v1,
 109.153 +		const Vector2 &v2, const Vector2 &v3, scalar_t t);
 109.154 +
 109.155 +/* 3D Vector */
 109.156 +class Vector3 {
 109.157 +public:
 109.158 +	scalar_t x, y, z;
 109.159 +
 109.160 +	explicit Vector3(scalar_t x = 0.0, scalar_t y = 0.0, scalar_t z = 0.0);
 109.161 +	Vector3(const vec3_t &vec);
 109.162 +	Vector3(const Vector2 &vec);
 109.163 +	Vector3(const Vector4 &vec);
 109.164 +	Vector3(const SphVector &sph);
 109.165 +
 109.166 +	Vector3 &operator =(const SphVector &sph);
 109.167 +
 109.168 +	inline scalar_t &operator [](int elem);
 109.169 +	inline const scalar_t &operator [](int elem) const;
 109.170 +
 109.171 +	inline scalar_t length() const;
 109.172 +	inline scalar_t length_sq() const;
 109.173 +	void normalize();
 109.174 +	Vector3 normalized() const;
 109.175 +
 109.176 +	void transform(const Matrix3x3 &mat);
 109.177 +	Vector3 transformed(const Matrix3x3 &mat) const;
 109.178 +	void transform(const Matrix4x4 &mat);
 109.179 +	Vector3 transformed(const Matrix4x4 &mat) const;
 109.180 +	void transform(const Quaternion &quat);
 109.181 +	Vector3 transformed(const Quaternion &quat) const;
 109.182 +
 109.183 +	void rotate(const Vector3 &euler);
 109.184 +	Vector3 rotated(const Vector3 &euler) const;
 109.185 +
 109.186 +	Vector3 reflection(const Vector3 &normal) const;
 109.187 +	Vector3 refraction(const Vector3 &normal, scalar_t src_ior, scalar_t dst_ior) const;
 109.188 +	Vector3 refraction(const Vector3 &normal, scalar_t ior) const;
 109.189 +};
 109.190 +
 109.191 +/* unary operations */
 109.192 +inline Vector3 operator -(const Vector3 &vec);
 109.193 +
 109.194 +/* binary vector (op) vector operations */
 109.195 +inline scalar_t dot_product(const Vector3 &v1, const Vector3 &v2);
 109.196 +inline Vector3 cross_product(const Vector3 &v1, const Vector3 &v2);
 109.197 +
 109.198 +inline Vector3 operator +(const Vector3 &v1, const Vector3 &v2);
 109.199 +inline Vector3 operator -(const Vector3 &v1, const Vector3 &v2);
 109.200 +inline Vector3 operator *(const Vector3 &v1, const Vector3 &v2);
 109.201 +inline Vector3 operator /(const Vector3 &v1, const Vector3 &v2);
 109.202 +inline bool operator ==(const Vector3 &v1, const Vector3 &v2);
 109.203 +
 109.204 +inline void operator +=(Vector3 &v1, const Vector3 &v2);
 109.205 +inline void operator -=(Vector3 &v1, const Vector3 &v2);
 109.206 +inline void operator *=(Vector3 &v1, const Vector3 &v2);
 109.207 +inline void operator /=(Vector3 &v1, const Vector3 &v2);
 109.208 +
 109.209 +/* binary vector (op) scalar and scalar (op) vector operations */
 109.210 +inline Vector3 operator +(const Vector3 &vec, scalar_t scalar);
 109.211 +inline Vector3 operator +(scalar_t scalar, const Vector3 &vec);
 109.212 +inline Vector3 operator -(const Vector3 &vec, scalar_t scalar);
 109.213 +inline Vector3 operator *(const Vector3 &vec, scalar_t scalar);
 109.214 +inline Vector3 operator *(scalar_t scalar, const Vector3 &vec);
 109.215 +inline Vector3 operator /(const Vector3 &vec, scalar_t scalar);
 109.216 +
 109.217 +inline void operator +=(Vector3 &vec, scalar_t scalar);
 109.218 +inline void operator -=(Vector3 &vec, scalar_t scalar);
 109.219 +inline void operator *=(Vector3 &vec, scalar_t scalar);
 109.220 +inline void operator /=(Vector3 &vec, scalar_t scalar);
 109.221 +
 109.222 +std::ostream &operator <<(std::ostream &out, const Vector3 &vec);
 109.223 +
 109.224 +inline Vector3 lerp(const Vector3 &a, const Vector3 &b, scalar_t t);
 109.225 +inline Vector3 catmull_rom_spline(const Vector3 &v0, const Vector3 &v1,
 109.226 +		const Vector3 &v2, const Vector3 &v3, scalar_t t);
 109.227 +
 109.228 +/* 4D Vector */
 109.229 +class Vector4 {
 109.230 +public:
 109.231 +	scalar_t x, y, z, w;
 109.232 +
 109.233 +	explicit Vector4(scalar_t x = 0.0, scalar_t y = 0.0, scalar_t z = 0.0, scalar_t w = 0.0);
 109.234 +	Vector4(const vec4_t &vec);
 109.235 +	Vector4(const Vector2 &vec);
 109.236 +	Vector4(const Vector3 &vec);
 109.237 +
 109.238 +	inline scalar_t &operator [](int elem);
 109.239 +	inline const scalar_t &operator [](int elem) const;
 109.240 +
 109.241 +	inline scalar_t length() const;
 109.242 +	inline scalar_t length_sq() const;
 109.243 +	void normalize();
 109.244 +	Vector4 normalized() const;
 109.245 +
 109.246 +	void transform(const Matrix4x4 &mat);
 109.247 +	Vector4 transformed(const Matrix4x4 &mat) const;
 109.248 +
 109.249 +	Vector4 reflection(const Vector4 &normal) const;
 109.250 +	Vector4 refraction(const Vector4 &normal, scalar_t src_ior, scalar_t dst_ior) const;
 109.251 +};
 109.252 +
 109.253 +
 109.254 +/* unary operations */
 109.255 +inline Vector4 operator -(const Vector4 &vec);
 109.256 +
 109.257 +/* binary vector (op) vector operations */
 109.258 +inline scalar_t dot_product(const Vector4 &v1, const Vector4 &v2);
 109.259 +inline Vector4 cross_product(const Vector4 &v1, const Vector4 &v2, const Vector4 &v3);
 109.260 +
 109.261 +inline Vector4 operator +(const Vector4 &v1, const Vector4 &v2);
 109.262 +inline Vector4 operator -(const Vector4 &v1, const Vector4 &v2);
 109.263 +inline Vector4 operator *(const Vector4 &v1, const Vector4 &v2);
 109.264 +inline Vector4 operator /(const Vector4 &v1, const Vector4 &v2);
 109.265 +inline bool operator ==(const Vector4 &v1, const Vector4 &v2);
 109.266 +
 109.267 +inline void operator +=(Vector4 &v1, const Vector4 &v2);
 109.268 +inline void operator -=(Vector4 &v1, const Vector4 &v2);
 109.269 +inline void operator *=(Vector4 &v1, const Vector4 &v2);
 109.270 +inline void operator /=(Vector4 &v1, const Vector4 &v2);
 109.271 +
 109.272 +/* binary vector (op) scalar and scalar (op) vector operations */
 109.273 +inline Vector4 operator +(const Vector4 &vec, scalar_t scalar);
 109.274 +inline Vector4 operator +(scalar_t scalar, const Vector4 &vec);
 109.275 +inline Vector4 operator -(const Vector4 &vec, scalar_t scalar);
 109.276 +inline Vector4 operator *(const Vector4 &vec, scalar_t scalar);
 109.277 +inline Vector4 operator *(scalar_t scalar, const Vector4 &vec);
 109.278 +inline Vector4 operator /(const Vector4 &vec, scalar_t scalar);
 109.279 +
 109.280 +inline void operator +=(Vector4 &vec, scalar_t scalar);
 109.281 +inline void operator -=(Vector4 &vec, scalar_t scalar);
 109.282 +inline void operator *=(Vector4 &vec, scalar_t scalar);
 109.283 +inline void operator /=(Vector4 &vec, scalar_t scalar);
 109.284 +
 109.285 +std::ostream &operator <<(std::ostream &out, const Vector4 &vec);
 109.286 +
 109.287 +inline Vector4 lerp(const Vector4 &v0, const Vector4 &v1, scalar_t t);
 109.288 +inline Vector4 catmull_rom_spline(const Vector4 &v0, const Vector4 &v1,
 109.289 +		const Vector4 &v2, const Vector4 &v3, scalar_t t);
 109.290 +
 109.291 +#endif	/* __cplusplus */
 109.292 +
 109.293 +#include "vector.inl"
 109.294 +
 109.295 +#endif	/* VMATH_VECTOR_H_ */
   110.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   110.2 +++ b/libs/vmath/vector.inl	Sat Sep 19 05:51:51 2015 +0300
   110.3 @@ -0,0 +1,761 @@
   110.4 +/*
   110.5 +libvmath - a vector math library
   110.6 +Copyright (C) 2004-2011 John Tsiombikas <nuclear@member.fsf.org>
   110.7 +
   110.8 +This program is free software: you can redistribute it and/or modify
   110.9 +it under the terms of the GNU Lesser General Public License as published
  110.10 +by the Free Software Foundation, either version 3 of the License, or
  110.11 +(at your option) any later version.
  110.12 +
  110.13 +This program is distributed in the hope that it will be useful,
  110.14 +but WITHOUT ANY WARRANTY; without even the implied warranty of
  110.15 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  110.16 +GNU Lesser General Public License for more details.
  110.17 +
  110.18 +You should have received a copy of the GNU Lesser General Public License
  110.19 +along with this program.  If not, see <http://www.gnu.org/licenses/>.
  110.20 +*/
  110.21 +
  110.22 +#include <math.h>
  110.23 +
  110.24 +#ifdef __cplusplus
  110.25 +extern "C" {
  110.26 +#endif	/* __cplusplus */
  110.27 +
  110.28 +/* C 2D vector functions */
  110.29 +static inline vec2_t v2_cons(scalar_t x, scalar_t y)
  110.30 +{
  110.31 +	vec2_t v;
  110.32 +	v.x = x;
  110.33 +	v.y = y;
  110.34 +	return v;
  110.35 +}
  110.36 +
  110.37 +static inline void v2_print(FILE *fp, vec2_t v)
  110.38 +{
  110.39 +	fprintf(fp, "[ %.4f %.4f ]", v.x, v.y);
  110.40 +}
  110.41 +
  110.42 +static inline vec2_t v2_add(vec2_t v1, vec2_t v2)
  110.43 +{
  110.44 +	vec2_t res;
  110.45 +	res.x = v1.x + v2.x;
  110.46 +	res.y = v1.y + v2.y;
  110.47 +	return res;
  110.48 +}
  110.49 +
  110.50 +static inline vec2_t v2_sub(vec2_t v1, vec2_t v2)
  110.51 +{
  110.52 +	vec2_t res;
  110.53 +	res.x = v1.x - v2.x;
  110.54 +	res.y = v1.y - v2.y;
  110.55 +	return res;
  110.56 +}
  110.57 +
  110.58 +static inline vec2_t v2_scale(vec2_t v, scalar_t s)
  110.59 +{
  110.60 +	vec2_t res;
  110.61 +	res.x = v.x * s;
  110.62 +	res.y = v.y * s;
  110.63 +	return res;
  110.64 +}
  110.65 +
  110.66 +static inline scalar_t v2_dot(vec2_t v1, vec2_t v2)
  110.67 +{
  110.68 +	return v1.x * v2.x + v1.y * v2.y;
  110.69 +}
  110.70 +
  110.71 +static inline scalar_t v2_length(vec2_t v)
  110.72 +{
  110.73 +	return sqrt(v.x * v.x + v.y * v.y);
  110.74 +}
  110.75 +
  110.76 +static inline scalar_t v2_length_sq(vec2_t v)
  110.77 +{
  110.78 +	return v.x * v.x + v.y * v.y;
  110.79 +}
  110.80 +
  110.81 +static inline vec2_t v2_normalize(vec2_t v)
  110.82 +{
  110.83 +	scalar_t len = (scalar_t)sqrt(v.x * v.x + v.y * v.y);
  110.84 +	v.x /= len;
  110.85 +	v.y /= len;
  110.86 +	return v;
  110.87 +}
  110.88 +
  110.89 +static inline vec2_t v2_lerp(vec2_t v1, vec2_t v2, scalar_t t)
  110.90 +{
  110.91 +	vec2_t res;
  110.92 +	res.x = v1.x + (v2.x - v1.x) * t;
  110.93 +	res.y = v1.y + (v2.y - v1.y) * t;
  110.94 +	return res;
  110.95 +}
  110.96 +
  110.97 +
  110.98 +/* C 3D vector functions */
  110.99 +static inline vec3_t v3_cons(scalar_t x, scalar_t y, scalar_t z)
 110.100 +{
 110.101 +	vec3_t v;
 110.102 +	v.x = x;
 110.103 +	v.y = y;
 110.104 +	v.z = z;
 110.105 +	return v;
 110.106 +}
 110.107 +
 110.108 +static inline void v3_print(FILE *fp, vec3_t v)
 110.109 +{
 110.110 +	fprintf(fp, "[ %.4f %.4f %.4f ]", v.x, v.y, v.z);
 110.111 +}
 110.112 +
 110.113 +static inline vec3_t v3_add(vec3_t v1, vec3_t v2)
 110.114 +{
 110.115 +	v1.x += v2.x;
 110.116 +	v1.y += v2.y;
 110.117 +	v1.z += v2.z;
 110.118 +	return v1;
 110.119 +}
 110.120 +
 110.121 +static inline vec3_t v3_sub(vec3_t v1, vec3_t v2)
 110.122 +{
 110.123 +	v1.x -= v2.x;
 110.124 +	v1.y -= v2.y;
 110.125 +	v1.z -= v2.z;
 110.126 +	return v1;
 110.127 +}
 110.128 +
 110.129 +static inline vec3_t v3_neg(vec3_t v)
 110.130 +{
 110.131 +	v.x = -v.x;
 110.132 +	v.y = -v.y;
 110.133 +	v.z = -v.z;
 110.134 +	return v;
 110.135 +}
 110.136 +
 110.137 +static inline vec3_t v3_mul(vec3_t v1, vec3_t v2)
 110.138 +{
 110.139 +	v1.x *= v2.x;
 110.140 +	v1.y *= v2.y;
 110.141 +	v1.z *= v2.z;
 110.142 +	return v1;
 110.143 +}
 110.144 +
 110.145 +static inline vec3_t v3_scale(vec3_t v1, scalar_t s)
 110.146 +{
 110.147 +	v1.x *= s;
 110.148 +	v1.y *= s;
 110.149 +	v1.z *= s;
 110.150 +	return v1;
 110.151 +}
 110.152 +
 110.153 +static inline scalar_t v3_dot(vec3_t v1, vec3_t v2)
 110.154 +{
 110.155 +	return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
 110.156 +}
 110.157 +
 110.158 +static inline vec3_t v3_cross(vec3_t v1, vec3_t v2)
 110.159 +{
 110.160 +	vec3_t v;
 110.161 +	v.x = v1.y * v2.z - v1.z * v2.y;
 110.162 +	v.y = v1.z * v2.x - v1.x * v2.z;
 110.163 +	v.z = v1.x * v2.y - v1.y * v2.x;
 110.164 +	return v;
 110.165 +}
 110.166 +
 110.167 +static inline scalar_t v3_length(vec3_t v)
 110.168 +{
 110.169 +	return sqrt(v.x * v.x + v.y * v.y + v.z * v.z);
 110.170 +}
 110.171 +
 110.172 +static inline scalar_t v3_length_sq(vec3_t v)
 110.173 +{
 110.174 +	return v.x * v.x + v.y * v.y + v.z * v.z;
 110.175 +}
 110.176 +
 110.177 +static inline vec3_t v3_normalize(vec3_t v)
 110.178 +{
 110.179 +	scalar_t len = sqrt(v.x * v.x + v.y * v.y + v.z * v.z);
 110.180 +	v.x /= len;
 110.181 +	v.y /= len;
 110.182 +	v.z /= len;
 110.183 +	return v;
 110.184 +}
 110.185 +
 110.186 +static inline vec3_t v3_transform(vec3_t v, mat4_t m)
 110.187 +{
 110.188 +	vec3_t res;
 110.189 +	res.x = m[0][0] * v.x + m[0][1] * v.y + m[0][2] * v.z + m[0][3];
 110.190 +	res.y = m[1][0] * v.x + m[1][1] * v.y + m[1][2] * v.z + m[1][3];
 110.191 +	res.z = m[2][0] * v.x + m[2][1] * v.y + m[2][2] * v.z + m[2][3];
 110.192 +	return res;
 110.193 +}
 110.194 +
 110.195 +static inline vec3_t v3_rotate(vec3_t v, scalar_t x, scalar_t y, scalar_t z)
 110.196 +{
 110.197 +	void m4_rotate(mat4_t, scalar_t, scalar_t, scalar_t);
 110.198 +
 110.199 +	mat4_t m = {{1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 1, 0}, {0, 0, 0, 1}};
 110.200 +	m4_rotate(m, x, y, z);
 110.201 +	return v3_transform(v, m);
 110.202 +}
 110.203 +
 110.204 +static inline vec3_t v3_rotate_axis(vec3_t v, scalar_t angle, scalar_t x, scalar_t y, scalar_t z)
 110.205 +{
 110.206 +	void m4_rotate_axis(mat4_t, scalar_t, scalar_t, scalar_t, scalar_t);
 110.207 +
 110.208 +	mat4_t m = {{1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 1, 0}, {0, 0, 0, 1}};
 110.209 +	m4_rotate_axis(m, angle, x, y, z);
 110.210 +	return v3_transform(v, m);
 110.211 +}
 110.212 +
 110.213 +static inline vec3_t v3_rotate_quat(vec3_t v, quat_t q)
 110.214 +{
 110.215 +	quat_t quat_rotate_quat(quat_t, quat_t);
 110.216 +
 110.217 +	quat_t vq = v4_cons(v.x, v.y, v.z, 0.0);
 110.218 +	quat_t res = quat_rotate_quat(vq, q);
 110.219 +	return v3_cons(res.x, res.y, res.z);
 110.220 +}
 110.221 +
 110.222 +static inline vec3_t v3_reflect(vec3_t v, vec3_t n)
 110.223 +{
 110.224 +	scalar_t dot = v3_dot(v, n);
 110.225 +	return v3_sub(v3_scale(n, dot * 2.0), v);
 110.226 +}
 110.227 +
 110.228 +static inline vec3_t v3_lerp(vec3_t v1, vec3_t v2, scalar_t t)
 110.229 +{
 110.230 +	v1.x += (v2.x - v1.x) * t;
 110.231 +	v1.y += (v2.y - v1.y) * t;
 110.232 +	v1.z += (v2.z - v1.z) * t;
 110.233 +	return v1;
 110.234 +}
 110.235 +
 110.236 +/* C 4D vector functions */
 110.237 +static inline vec4_t v4_cons(scalar_t x, scalar_t y, scalar_t z, scalar_t w)
 110.238 +{
 110.239 +	vec4_t v;
 110.240 +	v.x = x;
 110.241 +	v.y = y;
 110.242 +	v.z = z;
 110.243 +	v.w = w;
 110.244 +	return v;
 110.245 +}
 110.246 +
 110.247 +static inline void v4_print(FILE *fp, vec4_t v)
 110.248 +{
 110.249 +	fprintf(fp, "[ %.4f %.4f %.4f %.4f ]", v.x, v.y, v.z, v.w);
 110.250 +}
 110.251 +
 110.252 +static inline vec4_t v4_add(vec4_t v1, vec4_t v2)
 110.253 +{
 110.254 +	v1.x += v2.x;
 110.255 +	v1.y += v2.y;
 110.256 +	v1.z += v2.z;
 110.257 +	v1.w += v2.w;
 110.258 +	return v1;
 110.259 +}
 110.260 +
 110.261 +static inline vec4_t v4_sub(vec4_t v1, vec4_t v2)
 110.262 +{
 110.263 +	v1.x -= v2.x;
 110.264 +	v1.y -= v2.y;
 110.265 +	v1.z -= v2.z;
 110.266 +	v1.w -= v2.w;
 110.267 +	return v1;
 110.268 +}
 110.269 +
 110.270 +static inline vec4_t v4_neg(vec4_t v)
 110.271 +{
 110.272 +	v.x = -v.x;
 110.273 +	v.y = -v.y;
 110.274 +	v.z = -v.z;
 110.275 +	v.w = -v.w;
 110.276 +	return v;
 110.277 +}
 110.278 +
 110.279 +static inline vec4_t v4_mul(vec4_t v1, vec4_t v2)
 110.280 +{
 110.281 +	v1.x *= v2.x;
 110.282 +	v1.y *= v2.y;
 110.283 +	v1.z *= v2.z;
 110.284 +	v1.w *= v2.w;
 110.285 +	return v1;
 110.286 +}
 110.287 +
 110.288 +static inline vec4_t v4_scale(vec4_t v, scalar_t s)
 110.289 +{
 110.290 +	v.x *= s;
 110.291 +	v.y *= s;
 110.292 +	v.z *= s;
 110.293 +	v.w *= s;
 110.294 +	return v;
 110.295 +}
 110.296 +
 110.297 +static inline scalar_t v4_dot(vec4_t v1, vec4_t v2)
 110.298 +{
 110.299 +	return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z + v1.w * v2.w;
 110.300 +}
 110.301 +
 110.302 +static inline scalar_t v4_length(vec4_t v)
 110.303 +{
 110.304 +	return sqrt(v.x * v.x + v.y * v.y + v.z * v.z + v.w * v.w);
 110.305 +}
 110.306 +
 110.307 +static inline scalar_t v4_length_sq(vec4_t v)
 110.308 +{
 110.309 +	return v.x * v.x + v.y * v.y + v.z * v.z + v.w * v.w;
 110.310 +}
 110.311 +
 110.312 +static inline vec4_t v4_normalize(vec4_t v)
 110.313 +{
 110.314 +	scalar_t len = sqrt(v.x * v.x + v.y * v.y + v.z * v.z + v.w * v.w);
 110.315 +	v.x /= len;
 110.316 +	v.y /= len;
 110.317 +	v.z /= len;
 110.318 +	v.w /= len;
 110.319 +	return v;
 110.320 +}
 110.321 +
 110.322 +static inline vec4_t v4_transform(vec4_t v, mat4_t m)
 110.323 +{
 110.324 +	vec4_t res;
 110.325 +	res.x = m[0][0] * v.x + m[0][1] * v.y + m[0][2] * v.z + m[0][3] * v.w;
 110.326 +	res.y = m[1][0] * v.x + m[1][1] * v.y + m[1][2] * v.z + m[1][3] * v.w;
 110.327 +	res.z = m[2][0] * v.x + m[2][1] * v.y + m[2][2] * v.z + m[2][3] * v.w;
 110.328 +	res.w = m[3][0] * v.x + m[3][1] * v.y + m[3][2] * v.z + m[3][3] * v.w;
 110.329 +	return res;
 110.330 +}
 110.331 +
 110.332 +#ifdef __cplusplus
 110.333 +}	/* extern "C" */
 110.334 +
 110.335 +
 110.336 +/* --------------- C++ part -------------- */
 110.337 +
 110.338 +inline scalar_t &Vector2::operator [](int elem) {
 110.339 +	return elem ? y : x;
 110.340 +}
 110.341 +
 110.342 +inline const scalar_t &Vector2::operator [](int elem) const {
 110.343 +	return elem ? y : x;
 110.344 +}
 110.345 +
 110.346 +inline Vector2 operator -(const Vector2 &vec) {
 110.347 +	return Vector2(-vec.x, -vec.y);
 110.348 +}
 110.349 +
 110.350 +inline scalar_t dot_product(const Vector2 &v1, const Vector2 &v2) {
 110.351 +	return v1.x * v2.x + v1.y * v2.y;
 110.352 +}
 110.353 +
 110.354 +inline Vector2 operator +(const Vector2 &v1, const Vector2 &v2) {
 110.355 +	return Vector2(v1.x + v2.x, v1.y + v2.y);
 110.356 +}
 110.357 +
 110.358 +inline Vector2 operator -(const Vector2 &v1, const Vector2 &v2) {
 110.359 +	return Vector2(v1.x - v2.x, v1.y - v2.y);
 110.360 +}
 110.361 +
 110.362 +inline Vector2 operator *(const Vector2 &v1, const Vector2 &v2) {
 110.363 +	return Vector2(v1.x * v2.x, v1.y * v2.y);
 110.364 +}
 110.365 +
 110.366 +inline Vector2 operator /(const Vector2 &v1, const Vector2 &v2) {
 110.367 +	return Vector2(v1.x / v2.x, v1.y / v2.y);
 110.368 +}
 110.369 +
 110.370 +inline bool operator ==(const Vector2 &v1, const Vector2 &v2) {
 110.371 +	return (fabs(v1.x - v2.x) < XSMALL_NUMBER) && (fabs(v1.y - v2.x) < XSMALL_NUMBER);
 110.372 +}
 110.373 +
 110.374 +inline void operator +=(Vector2 &v1, const Vector2 &v2) {
 110.375 +	v1.x += v2.x;
 110.376 +	v1.y += v2.y;
 110.377 +}
 110.378 +
 110.379 +inline void operator -=(Vector2 &v1, const Vector2 &v2) {
 110.380 +	v1.x -= v2.x;
 110.381 +	v1.y -= v2.y;
 110.382 +}
 110.383 +
 110.384 +inline void operator *=(Vector2 &v1, const Vector2 &v2) {
 110.385 +	v1.x *= v2.x;
 110.386 +	v1.y *= v2.y;
 110.387 +}
 110.388 +
 110.389 +inline void operator /=(Vector2 &v1, const Vector2 &v2) {
 110.390 +	v1.x /= v2.x;
 110.391 +	v1.y /= v2.y;
 110.392 +}
 110.393 +
 110.394 +inline Vector2 operator +(const Vector2 &vec, scalar_t scalar) {
 110.395 +	return Vector2(vec.x + scalar, vec.y + scalar);
 110.396 +}
 110.397 +
 110.398 +inline Vector2 operator +(scalar_t scalar, const Vector2 &vec) {
 110.399 +	return Vector2(vec.x + scalar, vec.y + scalar);
 110.400 +}
 110.401 +
 110.402 +inline Vector2 operator -(scalar_t scalar, const Vector2 &vec) {
 110.403 +	return Vector2(vec.x - scalar, vec.y - scalar);
 110.404 +}
 110.405 +
 110.406 +inline Vector2 operator *(const Vector2 &vec, scalar_t scalar) {
 110.407 +	return Vector2(vec.x * scalar, vec.y * scalar);
 110.408 +}
 110.409 +
 110.410 +inline Vector2 operator *(scalar_t scalar, const Vector2 &vec) {
 110.411 +	return Vector2(vec.x * scalar, vec.y * scalar);
 110.412 +}
 110.413 +
 110.414 +inline Vector2 operator /(const Vector2 &vec, scalar_t scalar) {
 110.415 +	return Vector2(vec.x / scalar, vec.y / scalar);
 110.416 +}
 110.417 +
 110.418 +inline void operator +=(Vector2 &vec, scalar_t scalar) {
 110.419 +	vec.x += scalar;
 110.420 +	vec.y += scalar;
 110.421 +}
 110.422 +
 110.423 +inline void operator -=(Vector2 &vec, scalar_t scalar) {
 110.424 +	vec.x -= scalar;
 110.425 +	vec.y -= scalar;
 110.426 +}
 110.427 +
 110.428 +inline void operator *=(Vector2 &vec, scalar_t scalar) {
 110.429 +	vec.x *= scalar;
 110.430 +	vec.y *= scalar;
 110.431 +}
 110.432 +
 110.433 +inline void operator /=(Vector2 &vec, scalar_t scalar) {
 110.434 +	vec.x /= scalar;
 110.435 +	vec.y /= scalar;
 110.436 +}
 110.437 +
 110.438 +inline scalar_t Vector2::length() const {
 110.439 +	return sqrt(x*x + y*y);
 110.440 +}
 110.441 +
 110.442 +inline scalar_t Vector2::length_sq() const {
 110.443 +	return x*x + y*y;
 110.444 +}
 110.445 +
 110.446 +inline Vector2 lerp(const Vector2 &a, const Vector2 &b, scalar_t t)
 110.447 +{
 110.448 +	return a + (b - a) * t;
 110.449 +}
 110.450 +
 110.451 +inline Vector2 catmull_rom_spline(const Vector2 &v0, const Vector2 &v1,
 110.452 +		const Vector2 &v2, const Vector2 &v3, scalar_t t)
 110.453 +{
 110.454 +	scalar_t spline(scalar_t, scalar_t, scalar_t, scalar_t, scalar_t);
 110.455 +	scalar_t x = spline(v0.x, v1.x, v2.x, v3.x, t);
 110.456 +	scalar_t y = spline(v0.y, v1.y, v2.y, v3.y, t);
 110.457 +	return Vector2(x, y);
 110.458 +}
 110.459 +
 110.460 +
 110.461 +/* ------------- Vector3 -------------- */
 110.462 +
 110.463 +inline scalar_t &Vector3::operator [](int elem) {
 110.464 +	return elem ? (elem == 1 ? y : z) : x;
 110.465 +}
 110.466 +
 110.467 +inline const scalar_t &Vector3::operator [](int elem) const {
 110.468 +	return elem ? (elem == 1 ? y : z) : x;
 110.469 +}
 110.470 +
 110.471 +/* unary operations */
 110.472 +inline Vector3 operator -(const Vector3 &vec) {
 110.473 +	return Vector3(-vec.x, -vec.y, -vec.z);
 110.474 +}
 110.475 +
 110.476 +/* binary vector (op) vector operations */
 110.477 +inline scalar_t dot_product(const Vector3 &v1, const Vector3 &v2) {
 110.478 +	return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
 110.479 +}
 110.480 +
 110.481 +inline Vector3 cross_product(const Vector3 &v1, const Vector3 &v2) {
 110.482 +	return Vector3(v1.y * v2.z - v1.z * v2.y,  v1.z * v2.x - v1.x * v2.z,  v1.x * v2.y - v1.y * v2.x);
 110.483 +}
 110.484 +
 110.485 +
 110.486 +inline Vector3 operator +(const Vector3 &v1, const Vector3 &v2) {
 110.487 +	return Vector3(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z);
 110.488 +}
 110.489 +
 110.490 +inline Vector3 operator -(const Vector3 &v1, const Vector3 &v2) {
 110.491 +	return Vector3(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z);
 110.492 +}
 110.493 +
 110.494 +inline Vector3 operator *(const Vector3 &v1, const Vector3 &v2) {
 110.495 +	return Vector3(v1.x * v2.x, v1.y * v2.y, v1.z * v2.z);
 110.496 +}
 110.497 +
 110.498 +inline Vector3 operator /(const Vector3 &v1, const Vector3 &v2) {
 110.499 +	return Vector3(v1.x / v2.x, v1.y / v2.y, v1.z / v2.z);
 110.500 +}
 110.501 +
 110.502 +inline bool operator ==(const Vector3 &v1, const Vector3 &v2) {
 110.503 +	return (fabs(v1.x - v2.x) < XSMALL_NUMBER) && (fabs(v1.y - v2.y) < XSMALL_NUMBER) && (fabs(v1.z - v2.z) < XSMALL_NUMBER);
 110.504 +}
 110.505 +
 110.506 +inline void operator +=(Vector3 &v1, const Vector3 &v2) {
 110.507 +	v1.x += v2.x;
 110.508 +	v1.y += v2.y;
 110.509 +	v1.z += v2.z;
 110.510 +}
 110.511 +
 110.512 +inline void operator -=(Vector3 &v1, const Vector3 &v2) {
 110.513 +	v1.x -= v2.x;
 110.514 +	v1.y -= v2.y;
 110.515 +	v1.z -= v2.z;
 110.516 +}
 110.517 +
 110.518 +inline void operator *=(Vector3 &v1, const Vector3 &v2) {
 110.519 +	v1.x *= v2.x;
 110.520 +	v1.y *= v2.y;
 110.521 +	v1.z *= v2.z;
 110.522 +}
 110.523 +
 110.524 +inline void operator /=(Vector3 &v1, const Vector3 &v2) {
 110.525 +	v1.x /= v2.x;
 110.526 +	v1.y /= v2.y;
 110.527 +	v1.z /= v2.z;
 110.528 +}
 110.529 +/* binary vector (op) scalar and scalar (op) vector operations */
 110.530 +inline Vector3 operator +(const Vector3 &vec, scalar_t scalar) {
 110.531 +	return Vector3(vec.x + scalar, vec.y + scalar, vec.z + scalar);
 110.532 +}
 110.533 +
 110.534 +inline Vector3 operator +(scalar_t scalar, const Vector3 &vec) {
 110.535 +	return Vector3(vec.x + scalar, vec.y + scalar, vec.z + scalar);
 110.536 +}
 110.537 +
 110.538 +inline Vector3 operator -(const Vector3 &vec, scalar_t scalar) {
 110.539 +	return Vector3(vec.x - scalar, vec.y - scalar, vec.z - scalar);
 110.540 +}
 110.541 +
 110.542 +inline Vector3 operator *(const Vector3 &vec, scalar_t scalar) {
 110.543 +	return Vector3(vec.x * scalar, vec.y * scalar, vec.z * scalar);
 110.544 +}
 110.545 +
 110.546 +inline Vector3 operator *(scalar_t scalar, const Vector3 &vec) {
 110.547 +	return Vector3(vec.x * scalar, vec.y * scalar, vec.z * scalar);
 110.548 +}
 110.549 +
 110.550 +inline Vector3 operator /(const Vector3 &vec, scalar_t scalar) {
 110.551 +	return Vector3(vec.x / scalar, vec.y / scalar, vec.z / scalar);
 110.552 +}
 110.553 +
 110.554 +inline void operator +=(Vector3 &vec, scalar_t scalar) {
 110.555 +	vec.x += scalar;
 110.556 +	vec.y += scalar;
 110.557 +	vec.z += scalar;
 110.558 +}
 110.559 +
 110.560 +inline void operator -=(Vector3 &vec, scalar_t scalar) {
 110.561 +	vec.x -= scalar;
 110.562 +	vec.y -= scalar;
 110.563 +	vec.z -= scalar;
 110.564 +}
 110.565 +
 110.566 +inline void operator *=(Vector3 &vec, scalar_t scalar) {
 110.567 +	vec.x *= scalar;
 110.568 +	vec.y *= scalar;
 110.569 +	vec.z *= scalar;
 110.570 +}
 110.571 +
 110.572 +inline void operator /=(Vector3 &vec, scalar_t scalar) {
 110.573 +	vec.x /= scalar;
 110.574 +	vec.y /= scalar;
 110.575 +	vec.z /= scalar;
 110.576 +}
 110.577 +
 110.578 +inline scalar_t Vector3::length() const {
 110.579 +	return sqrt(x*x + y*y + z*z);
 110.580 +}
 110.581 +inline scalar_t Vector3::length_sq() const {
 110.582 +	return x*x + y*y + z*z;
 110.583 +}
 110.584 +
 110.585 +inline Vector3 lerp(const Vector3 &a, const Vector3 &b, scalar_t t) {
 110.586 +	return a + (b - a) * t;
 110.587 +}
 110.588 +
 110.589 +inline Vector3 catmull_rom_spline(const Vector3 &v0, const Vector3 &v1,
 110.590 +		const Vector3 &v2, const Vector3 &v3, scalar_t t)
 110.591 +{
 110.592 +	scalar_t spline(scalar_t, scalar_t, scalar_t, scalar_t, scalar_t);
 110.593 +	scalar_t x = spline(v0.x, v1.x, v2.x, v3.x, t);
 110.594 +	scalar_t y = spline(v0.y, v1.y, v2.y, v3.y, t);
 110.595 +	scalar_t z = spline(v0.z, v1.z, v2.z, v3.z, t);
 110.596 +	return Vector3(x, y, z);
 110.597 +}
 110.598 +
 110.599 +/* ----------- Vector4 ----------------- */
 110.600 +
 110.601 +inline scalar_t &Vector4::operator [](int elem) {
 110.602 +	return elem ? (elem == 1 ? y : (elem == 2 ? z : w)) : x;
 110.603 +}
 110.604 +
 110.605 +inline const scalar_t &Vector4::operator [](int elem) const {
 110.606 +	return elem ? (elem == 1 ? y : (elem == 2 ? z : w)) : x;
 110.607 +}
 110.608 +
 110.609 +inline Vector4 operator -(const Vector4 &vec) {
 110.610 +	return Vector4(-vec.x, -vec.y, -vec.z, -vec.w);
 110.611 +}
 110.612 +
 110.613 +inline scalar_t dot_product(const Vector4 &v1, const Vector4 &v2) {
 110.614 +	return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z + v1.w * v2.w;
 110.615 +}
 110.616 +
 110.617 +inline Vector4 cross_product(const Vector4 &v1, const Vector4 &v2, const Vector4 &v3) {
 110.618 +	scalar_t a, b, c, d, e, f;       /* Intermediate Values */
 110.619 +    Vector4 result;
 110.620 +
 110.621 +    /* Calculate intermediate values. */
 110.622 +    a = (v2.x * v3.y) - (v2.y * v3.x);
 110.623 +    b = (v2.x * v3.z) - (v2.z * v3.x);
 110.624 +    c = (v2.x * v3.w) - (v2.w * v3.x);
 110.625 +    d = (v2.y * v3.z) - (v2.z * v3.y);
 110.626 +    e = (v2.y * v3.w) - (v2.w * v3.y);
 110.627 +    f = (v2.z * v3.w) - (v2.w * v3.z);
 110.628 +
 110.629 +    /* Calculate the result-vector components. */
 110.630 +    result.x =   (v1.y * f) - (v1.z * e) + (v1.w * d);
 110.631 +    result.y = - (v1.x * f) + (v1.z * c) - (v1.w * b);
 110.632 +    result.z =   (v1.x * e) - (v1.y * c) + (v1.w * a);
 110.633 +    result.w = - (v1.x * d) + (v1.y * b) - (v1.z * a);
 110.634 +    return result;
 110.635 +}
 110.636 +
 110.637 +inline Vector4 operator +(const Vector4 &v1, const Vector4 &v2) {
 110.638 +	return Vector4(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z, v1.w + v2.w);
 110.639 +}
 110.640 +
 110.641 +inline Vector4 operator -(const Vector4 &v1, const Vector4 &v2) {
 110.642 +	return Vector4(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z, v1.w - v2.w);
 110.643 +}
 110.644 +
 110.645 +inline Vector4 operator *(const Vector4 &v1, const Vector4 &v2) {
 110.646 +	return Vector4(v1.x * v2.x, v1.y * v2.y, v1.z * v2.z, v1.w * v2.w);
 110.647 +}
 110.648 +
 110.649 +inline Vector4 operator /(const Vector4 &v1, const Vector4 &v2) {
 110.650 +	return Vector4(v1.x / v2.x, v1.y / v2.y, v1.z / v2.z, v1.w / v2.w);
 110.651 +}
 110.652 +
 110.653 +inline bool operator ==(const Vector4 &v1, const Vector4 &v2) {
 110.654 +	return	(fabs(v1.x - v2.x) < XSMALL_NUMBER) &&
 110.655 +			(fabs(v1.y - v2.y) < XSMALL_NUMBER) &&
 110.656 +			(fabs(v1.z - v2.z) < XSMALL_NUMBER) &&
 110.657 +			(fabs(v1.w - v2.w) < XSMALL_NUMBER);
 110.658 +}
 110.659 +
 110.660 +inline void operator +=(Vector4 &v1, const Vector4 &v2) {
 110.661 +	v1.x += v2.x;
 110.662 +	v1.y += v2.y;
 110.663 +	v1.z += v2.z;
 110.664 +	v1.w += v2.w;
 110.665 +}
 110.666 +
 110.667 +inline void operator -=(Vector4 &v1, const Vector4 &v2) {
 110.668 +	v1.x -= v2.x;
 110.669 +	v1.y -= v2.y;
 110.670 +	v1.z -= v2.z;
 110.671 +	v1.w -= v2.w;
 110.672 +}
 110.673 +
 110.674 +inline void operator *=(Vector4 &v1, const Vector4 &v2) {
 110.675 +	v1.x *= v2.x;
 110.676 +	v1.y *= v2.y;
 110.677 +	v1.z *= v2.z;
 110.678 +	v1.w *= v2.w;
 110.679 +}
 110.680 +
 110.681 +inline void operator /=(Vector4 &v1, const Vector4 &v2) {
 110.682 +	v1.x /= v2.x;
 110.683 +	v1.y /= v2.y;
 110.684 +	v1.z /= v2.z;
 110.685 +	v1.w /= v2.w;
 110.686 +}
 110.687 +
 110.688 +/* binary vector (op) scalar and scalar (op) vector operations */
 110.689 +inline Vector4 operator +(const Vector4 &vec, scalar_t scalar) {
 110.690 +	return Vector4(vec.x + scalar, vec.y + scalar, vec.z + scalar, vec.w + scalar);
 110.691 +}
 110.692 +
 110.693 +inline Vector4 operator +(scalar_t scalar, const Vector4 &vec) {
 110.694 +	return Vector4(vec.x + scalar, vec.y + scalar, vec.z + scalar, vec.w + scalar);
 110.695 +}
 110.696 +
 110.697 +inline Vector4 operator -(const Vector4 &vec, scalar_t scalar) {
 110.698 +	return Vector4(vec.x - scalar, vec.y - scalar, vec.z - scalar, vec.w - scalar);
 110.699 +}
 110.700 +
 110.701 +inline Vector4 operator *(const Vector4 &vec, scalar_t scalar) {
 110.702 +	return Vector4(vec.x * scalar, vec.y * scalar, vec.z * scalar, vec.w * scalar);
 110.703 +}
 110.704 +
 110.705 +inline Vector4 operator *(scalar_t scalar, const Vector4 &vec) {
 110.706 +	return Vector4(vec.x * scalar, vec.y * scalar, vec.z * scalar, vec.w * scalar);
 110.707 +}
 110.708 +
 110.709 +inline Vector4 operator /(const Vector4 &vec, scalar_t scalar) {
 110.710 +	return Vector4(vec.x / scalar, vec.y / scalar, vec.z / scalar, vec.w / scalar);
 110.711 +}
 110.712 +
 110.713 +inline void operator +=(Vector4 &vec, scalar_t scalar) {
 110.714 +	vec.x += scalar;
 110.715 +	vec.y += scalar;
 110.716 +	vec.z += scalar;
 110.717 +	vec.w += scalar;
 110.718 +}
 110.719 +
 110.720 +inline void operator -=(Vector4 &vec, scalar_t scalar) {
 110.721 +	vec.x -= scalar;
 110.722 +	vec.y -= scalar;
 110.723 +	vec.z -= scalar;
 110.724 +	vec.w -= scalar;
 110.725 +}
 110.726 +
 110.727 +inline void operator *=(Vector4 &vec, scalar_t scalar) {
 110.728 +	vec.x *= scalar;
 110.729 +	vec.y *= scalar;
 110.730 +	vec.z *= scalar;
 110.731 +	vec.w *= scalar;
 110.732 +}
 110.733 +
 110.734 +inline void operator /=(Vector4 &vec, scalar_t scalar) {
 110.735 +	vec.x /= scalar;
 110.736 +	vec.y /= scalar;
 110.737 +	vec.z /= scalar;
 110.738 +	vec.w /= scalar;
 110.739 +}
 110.740 +
 110.741 +inline scalar_t Vector4::length() const {
 110.742 +	return sqrt(x*x + y*y + z*z + w*w);
 110.743 +}
 110.744 +inline scalar_t Vector4::length_sq() const {
 110.745 +	return x*x + y*y + z*z + w*w;
 110.746 +}
 110.747 +
 110.748 +inline Vector4 lerp(const Vector4 &v0, const Vector4 &v1, scalar_t t)
 110.749 +{
 110.750 +	return v0 + (v1 - v0) * t;
 110.751 +}
 110.752 +
 110.753 +inline Vector4 catmull_rom_spline(const Vector4 &v0, const Vector4 &v1,
 110.754 +		const Vector4 &v2, const Vector4 &v3, scalar_t t)
 110.755 +{
 110.756 +	scalar_t spline(scalar_t, scalar_t, scalar_t, scalar_t, scalar_t);
 110.757 +	scalar_t x = spline(v0.x, v1.x, v2.x, v3.x, t);
 110.758 +	scalar_t y = spline(v0.y, v1.y, v2.y, v3.y, t);
 110.759 +	scalar_t z = spline(v0.z, v1.z, v2.z, v3.z, t);
 110.760 +	scalar_t w = spline(v0.w, v1.w, v2.w, v3.w, t);
 110.761 +	return Vector4(x, y, z, w);
 110.762 +}
 110.763 +
 110.764 +#endif	/* __cplusplus */
   111.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   111.2 +++ b/libs/vmath/vmath.c	Sat Sep 19 05:51:51 2015 +0300
   111.3 @@ -0,0 +1,335 @@
   111.4 +/*
   111.5 +libvmath - a vector math library
   111.6 +Copyright (C) 2004-2011 John Tsiombikas <nuclear@member.fsf.org>
   111.7 +
   111.8 +This program is free software: you can redistribute it and/or modify
   111.9 +it under the terms of the GNU Lesser General Public License as published
  111.10 +by the Free Software Foundation, either version 3 of the License, or
  111.11 +(at your option) any later version.
  111.12 +
  111.13 +This program is distributed in the hope that it will be useful,
  111.14 +but WITHOUT ANY WARRANTY; without even the implied warranty of
  111.15 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  111.16 +GNU Lesser General Public License for more details.
  111.17 +
  111.18 +You should have received a copy of the GNU Lesser General Public License
  111.19 +along with this program.  If not, see <http://www.gnu.org/licenses/>.
  111.20 +*/
  111.21 +
  111.22 +#include <stdlib.h>
  111.23 +#include <math.h>
  111.24 +#include "vmath.h"
  111.25 +
  111.26 +/** Numerical calculation of integrals using simpson's rule */
  111.27 +scalar_t integral(scalar_t (*f)(scalar_t), scalar_t low, scalar_t high, int samples)
  111.28 +{
  111.29 +	int i;
  111.30 +	scalar_t h = (high - low) / (scalar_t)samples;
  111.31 +	scalar_t sum = 0.0;
  111.32 +
  111.33 +	for(i=0; i<samples+1; i++) {
  111.34 +		scalar_t y = f((scalar_t)i * h + low);
  111.35 +		sum += ((!i || i == samples) ? y : ((i % 2) ? 4.0 * y : 2.0 * y)) * (h / 3.0);
  111.36 +	}
  111.37 +	return sum;
  111.38 +}
  111.39 +
  111.40 +/** Gaussuan function */
  111.41 +scalar_t gaussian(scalar_t x, scalar_t mean, scalar_t sdev)
  111.42 +{
  111.43 +	scalar_t exponent = -SQ(x - mean) / (2.0 * SQ(sdev));
  111.44 +	return 1.0 - -pow(M_E, exponent) / (sdev * sqrt(TWO_PI));
  111.45 +}
  111.46 +
  111.47 +
  111.48 +/** b-spline approximation */
  111.49 +scalar_t bspline(scalar_t a, scalar_t b, scalar_t c, scalar_t d, scalar_t t)
  111.50 +{
  111.51 +	vec4_t tmp;
  111.52 +	scalar_t tsq = t * t;
  111.53 +
  111.54 +	static mat4_t bspline_mat = {
  111.55 +		{-1,  3, -3,  1},
  111.56 +		{3, -6,  3,  0},
  111.57 +		{-3,  0,  3,  0},
  111.58 +		{1,  4,  1,  0}
  111.59 +	};
  111.60 +
  111.61 +	tmp = v4_scale(v4_transform(v4_cons(a, b, c, d), bspline_mat), 1.0 / 6.0);
  111.62 +	return v4_dot(v4_cons(tsq * t, tsq, t, 1.0), tmp);
  111.63 +}
  111.64 +
  111.65 +/** Catmull-rom spline interpolation */
  111.66 +scalar_t spline(scalar_t a, scalar_t b, scalar_t c, scalar_t d, scalar_t t) {
  111.67 +	vec4_t tmp;
  111.68 +	scalar_t tsq = t * t;
  111.69 +
  111.70 +	static mat4_t crspline_mat = {
  111.71 +		{-1,  3, -3,  1},
  111.72 +		{2, -5,  4, -1},
  111.73 +		{-1,  0,  1,  0},
  111.74 +		{0,  2,  0,  0}
  111.75 +	};
  111.76 +
  111.77 +	tmp = v4_scale(v4_transform(v4_cons(a, b, c, d), crspline_mat), 0.5);
  111.78 +	return v4_dot(v4_cons(tsq * t, tsq, t, 1.0), tmp);
  111.79 +}
  111.80 +
  111.81 +/** Bezier interpolation */
  111.82 +scalar_t bezier(scalar_t a, scalar_t b, scalar_t c, scalar_t d, scalar_t t)
  111.83 +{
  111.84 +	scalar_t omt, omt3, t3, f;
  111.85 +	t3 = t * t * t;
  111.86 +	omt = 1.0f - t;
  111.87 +	omt3 = omt * omt * omt;
  111.88 +	f = 3 * t * omt;
  111.89 +
  111.90 +	return (a * omt3) + (b * f * omt) + (c * f * t) + (d * t3);
  111.91 +}
  111.92 +
  111.93 +/* ---- Ken Perlin's implementation of noise ---- */
  111.94 +
  111.95 +#define B	0x100
  111.96 +#define BM	0xff
  111.97 +#define N	0x1000
  111.98 +#define NP	12   /* 2^N */
  111.99 +#define NM	0xfff
 111.100 +
 111.101 +#define s_curve(t) (t * t * (3.0f - 2.0f * t))
 111.102 +
 111.103 +#define setup(elem, b0, b1, r0, r1) \
 111.104 +	do {							\
 111.105 +		scalar_t t = elem + N;		\
 111.106 +		b0 = ((int)t) & BM;			\
 111.107 +		b1 = (b0 + 1) & BM;			\
 111.108 +		r0 = t - (int)t;			\
 111.109 +		r1 = r0 - 1.0f;				\
 111.110 +	} while(0)
 111.111 +
 111.112 +
 111.113 +static int perm[B + B + 2];			/* permuted index from g_n onto themselves */
 111.114 +static vec3_t grad3[B + B + 2];		/* 3D random gradients */
 111.115 +static vec2_t grad2[B + B + 2];		/* 2D random gradients */
 111.116 +static scalar_t grad1[B + B + 2];	/* 1D random ... slopes */
 111.117 +static int tables_valid;
 111.118 +
 111.119 +static void init_noise()
 111.120 +{
 111.121 +	int i;
 111.122 +
 111.123 +	/* calculate random gradients */
 111.124 +	for(i=0; i<B; i++) {
 111.125 +		perm[i] = i;	/* .. and initialize permutation mapping to identity */
 111.126 +
 111.127 +		grad1[i] = (scalar_t)((rand() % (B + B)) - B) / B;
 111.128 +
 111.129 +		grad2[i].x = (scalar_t)((rand() % (B + B)) - B) / B;
 111.130 +		grad2[i].y = (scalar_t)((rand() % (B + B)) - B) / B;
 111.131 +		grad2[i] = v2_normalize(grad2[i]);
 111.132 +
 111.133 +		grad3[i].x = (scalar_t)((rand() % (B + B)) - B) / B;
 111.134 +		grad3[i].y = (scalar_t)((rand() % (B + B)) - B) / B;
 111.135 +		grad3[i].z = (scalar_t)((rand() % (B + B)) - B) / B;
 111.136 +		grad3[i] = v3_normalize(grad3[i]);
 111.137 +	}
 111.138 +
 111.139 +	/* permute indices by swapping them randomly */
 111.140 +	for(i=0; i<B; i++) {
 111.141 +		int rand_idx = rand() % B;
 111.142 +
 111.143 +		int tmp = perm[i];
 111.144 +		perm[i] = perm[rand_idx];
 111.145 +		perm[rand_idx] = tmp;
 111.146 +	}
 111.147 +
 111.148 +	/* fill up the rest of the arrays by duplicating the existing gradients */
 111.149 +	/* and permutations */
 111.150 +	for(i=0; i<B+2; i++) {
 111.151 +		perm[B + i] = perm[i];
 111.152 +		grad1[B + i] = grad1[i];
 111.153 +		grad2[B + i] = grad2[i];
 111.154 +		grad3[B + i] = grad3[i];
 111.155 +	}
 111.156 +}
 111.157 +
 111.158 +scalar_t noise1(scalar_t x)
 111.159 +{
 111.160 +	int bx0, bx1;
 111.161 +	scalar_t rx0, rx1, sx, u, v;
 111.162 +
 111.163 +	if(!tables_valid) {
 111.164 +		init_noise();
 111.165 +		tables_valid = 1;
 111.166 +	}
 111.167 +
 111.168 +	setup(x, bx0, bx1, rx0, rx1);
 111.169 +	sx = s_curve(rx0);
 111.170 +	u = rx0 * grad1[perm[bx0]];
 111.171 +	v = rx1 * grad1[perm[bx1]];
 111.172 +
 111.173 +	return lerp(u, v, sx);
 111.174 +}
 111.175 +
 111.176 +scalar_t noise2(scalar_t x, scalar_t y)
 111.177 +{
 111.178 +	int i, j, b00, b10, b01, b11;
 111.179 +	int bx0, bx1, by0, by1;
 111.180 +	scalar_t rx0, rx1, ry0, ry1;
 111.181 +	scalar_t sx, sy, u, v, a, b;
 111.182 +
 111.183 +	if(!tables_valid) {
 111.184 +		init_noise();
 111.185 +		tables_valid = 1;
 111.186 +	}
 111.187 +
 111.188 +	setup(x, bx0, bx1, rx0, rx1);
 111.189 +	setup(y, by0, by1, ry0, ry1);
 111.190 +
 111.191 +	i = perm[bx0];
 111.192 +	j = perm[bx1];
 111.193 +
 111.194 +	b00 = perm[i + by0];
 111.195 +	b10 = perm[j + by0];
 111.196 +	b01 = perm[i + by1];
 111.197 +	b11 = perm[j + by1];
 111.198 +
 111.199 +	/* calculate hermite inteprolating factors */
 111.200 +	sx = s_curve(rx0);
 111.201 +	sy = s_curve(ry0);
 111.202 +
 111.203 +	/* interpolate along the left edge */
 111.204 +	u = v2_dot(grad2[b00], v2_cons(rx0, ry0));
 111.205 +	v = v2_dot(grad2[b10], v2_cons(rx1, ry0));
 111.206 +	a = lerp(u, v, sx);
 111.207 +
 111.208 +	/* interpolate along the right edge */
 111.209 +	u = v2_dot(grad2[b01], v2_cons(rx0, ry1));
 111.210 +	v = v2_dot(grad2[b11], v2_cons(rx1, ry1));
 111.211 +	b = lerp(u, v, sx);
 111.212 +
 111.213 +	/* interpolate between them */
 111.214 +	return lerp(a, b, sy);
 111.215 +}
 111.216 +
 111.217 +scalar_t noise3(scalar_t x, scalar_t y, scalar_t z)
 111.218 +{
 111.219 +	int i, j;
 111.220 +	int bx0, bx1, by0, by1, bz0, bz1;
 111.221 +	int b00, b10, b01, b11;
 111.222 +	scalar_t rx0, rx1, ry0, ry1, rz0, rz1;
 111.223 +	scalar_t sx, sy, sz;
 111.224 +	scalar_t u, v, a, b, c, d;
 111.225 +
 111.226 +	if(!tables_valid) {
 111.227 +		init_noise();
 111.228 +		tables_valid = 1;
 111.229 +	}
 111.230 +
 111.231 +	setup(x, bx0, bx1, rx0, rx1);
 111.232 +	setup(y, by0, by1, ry0, ry1);
 111.233 +	setup(z, bz0, bz1, rz0, rz1);
 111.234 +
 111.235 +	i = perm[bx0];
 111.236 +	j = perm[bx1];
 111.237 +
 111.238 +	b00 = perm[i + by0];
 111.239 +	b10 = perm[j + by0];
 111.240 +	b01 = perm[i + by1];
 111.241 +	b11 = perm[j + by1];
 111.242 +
 111.243 +	/* calculate hermite interpolating factors */
 111.244 +	sx = s_curve(rx0);
 111.245 +	sy = s_curve(ry0);
 111.246 +	sz = s_curve(rz0);
 111.247 +
 111.248 +	/* interpolate along the top slice of the cell */
 111.249 +	u = v3_dot(grad3[b00 + bz0], v3_cons(rx0, ry0, rz0));
 111.250 +	v = v3_dot(grad3[b10 + bz0], v3_cons(rx1, ry0, rz0));
 111.251 +	a = lerp(u, v, sx);
 111.252 +
 111.253 +	u = v3_dot(grad3[b01 + bz0], v3_cons(rx0, ry1, rz0));
 111.254 +	v = v3_dot(grad3[b11 + bz0], v3_cons(rx1, ry1, rz0));
 111.255 +	b = lerp(u, v, sx);
 111.256 +
 111.257 +	c = lerp(a, b, sy);
 111.258 +
 111.259 +	/* interpolate along the bottom slice of the cell */
 111.260 +	u = v3_dot(grad3[b00 + bz0], v3_cons(rx0, ry0, rz1));
 111.261 +	v = v3_dot(grad3[b10 + bz0], v3_cons(rx1, ry0, rz1));
 111.262 +	a = lerp(u, v, sx);
 111.263 +
 111.264 +	u = v3_dot(grad3[b01 + bz0], v3_cons(rx0, ry1, rz1));
 111.265 +	v = v3_dot(grad3[b11 + bz0], v3_cons(rx1, ry1, rz1));
 111.266 +	b = lerp(u, v, sx);
 111.267 +
 111.268 +	d = lerp(a, b, sy);
 111.269 +
 111.270 +	/* interpolate between slices */
 111.271 +	return lerp(c, d, sz);
 111.272 +}
 111.273 +
 111.274 +scalar_t fbm1(scalar_t x, int octaves)
 111.275 +{
 111.276 +	int i;
 111.277 +	scalar_t res = 0.0f, freq = 1.0f;
 111.278 +	for(i=0; i<octaves; i++) {
 111.279 +		res += noise1(x * freq) / freq;
 111.280 +		freq *= 2.0f;
 111.281 +	}
 111.282 +	return res;
 111.283 +}
 111.284 +
 111.285 +scalar_t fbm2(scalar_t x, scalar_t y, int octaves)
 111.286 +{
 111.287 +	int i;
 111.288 +	scalar_t res = 0.0f, freq = 1.0f;
 111.289 +	for(i=0; i<octaves; i++) {
 111.290 +		res += noise2(x * freq, y * freq) / freq;
 111.291 +		freq *= 2.0f;
 111.292 +	}
 111.293 +	return res;
 111.294 +}
 111.295 +
 111.296 +scalar_t fbm3(scalar_t x, scalar_t y, scalar_t z, int octaves)
 111.297 +{
 111.298 +	int i;
 111.299 +	scalar_t res = 0.0f, freq = 1.0f;
 111.300 +	for(i=0; i<octaves; i++) {
 111.301 +		res += noise3(x * freq, y * freq, z * freq) / freq;
 111.302 +		freq *= 2.0f;
 111.303 +	}
 111.304 +	return res;
 111.305 +}
 111.306 +
 111.307 +scalar_t turbulence1(scalar_t x, int octaves)
 111.308 +{
 111.309 +	int i;
 111.310 +	scalar_t res = 0.0f, freq = 1.0f;
 111.311 +	for(i=0; i<octaves; i++) {
 111.312 +		res += fabs(noise1(x * freq) / freq);
 111.313 +		freq *= 2.0f;
 111.314 +	}
 111.315 +	return res;
 111.316 +}
 111.317 +
 111.318 +scalar_t turbulence2(scalar_t x, scalar_t y, int octaves)
 111.319 +{
 111.320 +	int i;
 111.321 +	scalar_t res = 0.0f, freq = 1.0f;
 111.322 +	for(i=0; i<octaves; i++) {
 111.323 +		res += fabs(noise2(x * freq, y * freq) / freq);
 111.324 +		freq *= 2.0f;
 111.325 +	}
 111.326 +	return res;
 111.327 +}
 111.328 +
 111.329 +scalar_t turbulence3(scalar_t x, scalar_t y, scalar_t z, int octaves)
 111.330 +{
 111.331 +	int i;
 111.332 +	scalar_t res = 0.0f, freq = 1.0f;
 111.333 +	for(i=0; i<octaves; i++) {
 111.334 +		res += fabs(noise3(x * freq, y * freq, z * freq) / freq);
 111.335 +		freq *= 2.0f;
 111.336 +	}
 111.337 +	return res;
 111.338 +}
   112.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   112.2 +++ b/libs/vmath/vmath.h	Sat Sep 19 05:51:51 2015 +0300
   112.3 @@ -0,0 +1,92 @@
   112.4 +/*
   112.5 +libvmath - a vector math library
   112.6 +Copyright (C) 2004-2011 John Tsiombikas <nuclear@member.fsf.org>
   112.7 +
   112.8 +This program is free software: you can redistribute it and/or modify
   112.9 +it under the terms of the GNU Lesser General Public License as published
  112.10 +by the Free Software Foundation, either version 3 of the License, or
  112.11 +(at your option) any later version.
  112.12 +
  112.13 +This program is distributed in the hope that it will be useful,
  112.14 +but WITHOUT ANY WARRANTY; without even the implied warranty of
  112.15 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  112.16 +GNU Lesser General Public License for more details.
  112.17 +
  112.18 +You should have received a copy of the GNU Lesser General Public License
  112.19 +along with this program.  If not, see <http://www.gnu.org/licenses/>.
  112.20 +*/
  112.21 +
  112.22 +#ifndef VMATH_H_
  112.23 +#define VMATH_H_
  112.24 +
  112.25 +#include <math.h>
  112.26 +#include "vmath_types.h"
  112.27 +
  112.28 +#ifndef M_PI
  112.29 +#define M_PI	PI
  112.30 +#endif
  112.31 +
  112.32 +#ifndef M_E
  112.33 +#define M_E				2.718281828459045
  112.34 +#endif
  112.35 +
  112.36 +#define PI				3.141592653589793
  112.37 +#define HALF_PI			1.570796326794897
  112.38 +#define QUARTER_PI		0.785398163397448
  112.39 +#define TWO_PI			6.283185307179586
  112.40 +
  112.41 +
  112.42 +#define RAD_TO_DEG(a) ((((scalar_t)a) * 360.0) / TWO_PI)
  112.43 +#define DEG_TO_RAD(a) (((scalar_t)a) * (PI / 180.0))
  112.44 +
  112.45 +#define SQ(x) ((x) * (x))
  112.46 +
  112.47 +#define MIN(a, b)	((a) < (b) ? (a) : (b))
  112.48 +#define MAX(a, b)	((a) > (b) ? (a) : (b))
  112.49 +
  112.50 +#ifndef __GNUC__
  112.51 +#define round(x)	((x) >= 0 ? (x) + 0.5 : (x) - 0.5)
  112.52 +#endif
  112.53 +
  112.54 +#ifdef __cplusplus
  112.55 +extern "C" {
  112.56 +#endif	/* __cplusplus */
  112.57 +
  112.58 +static inline scalar_t frand(scalar_t range);
  112.59 +static inline vec3_t sphrand(scalar_t rad);
  112.60 +
  112.61 +scalar_t integral(scalar_t (*f)(scalar_t), scalar_t low, scalar_t high, int samples);
  112.62 +scalar_t gaussian(scalar_t x, scalar_t mean, scalar_t sdev);
  112.63 +
  112.64 +static inline scalar_t lerp(scalar_t a, scalar_t b, scalar_t t);
  112.65 +
  112.66 +scalar_t bspline(scalar_t a, scalar_t b, scalar_t c, scalar_t d, scalar_t t);
  112.67 +scalar_t spline(scalar_t a, scalar_t b, scalar_t c, scalar_t d, scalar_t t);
  112.68 +scalar_t bezier(scalar_t a, scalar_t b, scalar_t c, scalar_t d, scalar_t t);
  112.69 +
  112.70 +scalar_t noise1(scalar_t x);
  112.71 +scalar_t noise2(scalar_t x, scalar_t y);
  112.72 +scalar_t noise3(scalar_t x, scalar_t y, scalar_t z);
  112.73 +
  112.74 +scalar_t fbm1(scalar_t x, int octaves);
  112.75 +scalar_t fbm2(scalar_t x, scalar_t y, int octaves);
  112.76 +scalar_t fbm3(scalar_t x, scalar_t y, scalar_t z, int octaves);
  112.77 +
  112.78 +scalar_t turbulence1(scalar_t x, int octaves);
  112.79 +scalar_t turbulence2(scalar_t x, scalar_t y, int octaves);
  112.80 +scalar_t turbulence3(scalar_t x, scalar_t y, scalar_t z, int octaves);
  112.81 +
  112.82 +#ifdef __cplusplus
  112.83 +}
  112.84 +#endif	/* __cplusplus */
  112.85 +
  112.86 +#include "vmath.inl"
  112.87 +
  112.88 +#include "vector.h"
  112.89 +#include "matrix.h"
  112.90 +#include "quat.h"
  112.91 +#include "sphvec.h"
  112.92 +#include "ray.h"
  112.93 +#include "geom.h"
  112.94 +
  112.95 +#endif	/* VMATH_H_ */
   113.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   113.2 +++ b/libs/vmath/vmath.inl	Sat Sep 19 05:51:51 2015 +0300
   113.3 @@ -0,0 +1,47 @@
   113.4 +/*
   113.5 +libvmath - a vector math library
   113.6 +Copyright (C) 2004-2011 John Tsiombikas <nuclear@member.fsf.org>
   113.7 +
   113.8 +This program is free software: you can redistribute it and/or modify
   113.9 +it under the terms of the GNU Lesser General Public License as published
  113.10 +by the Free Software Foundation, either version 3 of the License, or
  113.11 +(at your option) any later version.
  113.12 +
  113.13 +This program is distributed in the hope that it will be useful,
  113.14 +but WITHOUT ANY WARRANTY; without even the implied warranty of
  113.15 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  113.16 +GNU Lesser General Public License for more details.
  113.17 +
  113.18 +You should have received a copy of the GNU Lesser General Public License
  113.19 +along with this program.  If not, see <http://www.gnu.org/licenses/>.
  113.20 +*/
  113.21 +
  113.22 +#include <stdlib.h>
  113.23 +
  113.24 +/** Generates a random number in [0, range) */
  113.25 +static inline scalar_t frand(scalar_t range)
  113.26 +{
  113.27 +	return range * (scalar_t)rand() / (scalar_t)RAND_MAX;
  113.28 +}
  113.29 +
  113.30 +/** Generates a random vector on the surface of a sphere */
  113.31 +static inline vec3_t sphrand(scalar_t rad)
  113.32 +{
  113.33 +	scalar_t u = (scalar_t)rand() / RAND_MAX;
  113.34 +	scalar_t v = (scalar_t)rand() / RAND_MAX;
  113.35 +
  113.36 +	scalar_t theta = 2.0 * M_PI * u;
  113.37 +	scalar_t phi = acos(2.0 * v - 1.0);
  113.38 +
  113.39 +	vec3_t res;
  113.40 +	res.x = rad * cos(theta) * sin(phi);
  113.41 +	res.y = rad * sin(theta) * sin(phi);
  113.42 +	res.z = rad * cos(phi);
  113.43 +	return res;
  113.44 +}
  113.45 +
  113.46 +/** linear interpolation */
  113.47 +static inline scalar_t lerp(scalar_t a, scalar_t b, scalar_t t)
  113.48 +{
  113.49 +	return a + (b - a) * t;
  113.50 +}
   114.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   114.2 +++ b/libs/vmath/vmath_config.h	Sat Sep 19 05:51:51 2015 +0300
   114.3 @@ -0,0 +1,19 @@
   114.4 +#ifndef VMATH_CONFIG_H_
   114.5 +#define VMATH_CONFIG_H_
   114.6 +
   114.7 +#if (__STDC_VERSION__ < 199999)
   114.8 +#if defined(__GNUC__) || defined(_MSC_VER)
   114.9 +#define inline __inline
  114.10 +#else
  114.11 +#define inline
  114.12 +
  114.13 +#ifdef VECTOR_H_
  114.14 +#warning "compiling vector operations without inline, performance might suffer"
  114.15 +#endif	/* VECTOR_H_ */
  114.16 +
  114.17 +#endif	/* gcc/msvc */
  114.18 +#endif	/* not C99 */
  114.19 +
  114.20 +#define SINGLE_PRECISION_MATH
  114.21 +
  114.22 +#endif	/* VMATH_CONFIG_H_ */
   115.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   115.2 +++ b/libs/vmath/vmath_types.h	Sat Sep 19 05:51:51 2015 +0300
   115.3 @@ -0,0 +1,58 @@
   115.4 +/*
   115.5 +libvmath - a vector math library
   115.6 +Copyright (C) 2004-2011 John Tsiombikas <nuclear@member.fsf.org>
   115.7 +
   115.8 +This program is free software: you can redistribute it and/or modify
   115.9 +it under the terms of the GNU Lesser General Public License as published
  115.10 +by the Free Software Foundation, either version 3 of the License, or
  115.11 +(at your option) any later version.
  115.12 +
  115.13 +This program is distributed in the hope that it will be useful,
  115.14 +but WITHOUT ANY WARRANTY; without even the implied warranty of
  115.15 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  115.16 +GNU Lesser General Public License for more details.
  115.17 +
  115.18 +You should have received a copy of the GNU Lesser General Public License
  115.19 +along with this program.  If not, see <http://www.gnu.org/licenses/>.
  115.20 +*/
  115.21 +
  115.22 +#ifndef VMATH_TYPES_H_
  115.23 +#define VMATH_TYPES_H_
  115.24 +
  115.25 +#include "vmath_config.h"
  115.26 +
  115.27 +#define SMALL_NUMBER	1.e-4
  115.28 +#define XSMALL_NUMBER	1.e-8
  115.29 +#define ERROR_MARGIN	1.e-6
  115.30 +
  115.31 +
  115.32 +#ifdef SINGLE_PRECISION_MATH
  115.33 +typedef float scalar_t;
  115.34 +#else
  115.35 +typedef double scalar_t;
  115.36 +#endif	/* floating point precision */
  115.37 +
  115.38 +/* vectors */
  115.39 +typedef struct { scalar_t x, y; } vec2_t;
  115.40 +typedef struct { scalar_t x, y, z; } vec3_t;
  115.41 +typedef struct { scalar_t x, y, z, w; } vec4_t;
  115.42 +
  115.43 +/* quaternions */
  115.44 +typedef vec4_t quat_t;
  115.45 +
  115.46 +/* matrices */
  115.47 +typedef scalar_t mat3_t[3][3];
  115.48 +typedef scalar_t mat4_t[4][4];
  115.49 +
  115.50 +
  115.51 +#ifdef __cplusplus
  115.52 +class Vector2;
  115.53 +class Vector3;
  115.54 +class Vector4;
  115.55 +class Quaternion;
  115.56 +class Matrix3x3;
  115.57 +class Matrix4x4;
  115.58 +class SphVector;
  115.59 +#endif	/* __cplusplus */
  115.60 +
  115.61 +#endif	/* VMATH_TYPES_H_ */
   116.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   116.2 +++ b/libs/zlib/LICENSE	Sat Sep 19 05:51:51 2015 +0300
   116.3 @@ -0,0 +1,20 @@
   116.4 + (C) 1995-2010 Jean-loup Gailly and Mark Adler
   116.5 +
   116.6 +  This software is provided 'as-is', without any express or implied
   116.7 +  warranty.  In no event will the authors be held liable for any damages
   116.8 +  arising from the use of this software.
   116.9 +
  116.10 +  Permission is granted to anyone to use this software for any purpose,
  116.11 +  including commercial applications, and to alter it and redistribute it
  116.12 +  freely, subject to the following restrictions:
  116.13 +
  116.14 +  1. The origin of this software must not be misrepresented; you must not
  116.15 +     claim that you wrote the original software. If you use this software
  116.16 +     in a product, an acknowledgment in the product documentation would be
  116.17 +     appreciated but is not required.
  116.18 +  2. Altered source versions must be plainly marked as such, and must not be
  116.19 +     misrepresented as being the original software.
  116.20 +  3. This notice may not be removed or altered from any source distribution.
  116.21 +
  116.22 +  Jean-loup Gailly        Mark Adler
  116.23 +  jloup@gzip.org          madler@alumni.caltech.edu
   117.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   117.2 +++ b/libs/zlib/adler32.c	Sat Sep 19 05:51:51 2015 +0300
   117.3 @@ -0,0 +1,149 @@
   117.4 +/* adler32.c -- compute the Adler-32 checksum of a data stream
   117.5 + * Copyright (C) 1995-2004 Mark Adler
   117.6 + * For conditions of distribution and use, see copyright notice in zlib.h
   117.7 + */
   117.8 +
   117.9 +/* @(#) $Id$ */
  117.10 +
  117.11 +#define ZLIB_INTERNAL
  117.12 +#include "zlib.h"
  117.13 +
  117.14 +#define BASE 65521UL    /* largest prime smaller than 65536 */
  117.15 +#define NMAX 5552
  117.16 +/* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
  117.17 +
  117.18 +#define DO1(buf,i)  {adler += (buf)[i]; sum2 += adler;}
  117.19 +#define DO2(buf,i)  DO1(buf,i); DO1(buf,i+1);
  117.20 +#define DO4(buf,i)  DO2(buf,i); DO2(buf,i+2);
  117.21 +#define DO8(buf,i)  DO4(buf,i); DO4(buf,i+4);
  117.22 +#define DO16(buf)   DO8(buf,0); DO8(buf,8);
  117.23 +
  117.24 +/* use NO_DIVIDE if your processor does not do division in hardware */
  117.25 +#ifdef NO_DIVIDE
  117.26 +#  define MOD(a) \
  117.27 +    do { \
  117.28 +        if (a >= (BASE << 16)) a -= (BASE << 16); \
  117.29 +        if (a >= (BASE << 15)) a -= (BASE << 15); \
  117.30 +        if (a >= (BASE << 14)) a -= (BASE << 14); \
  117.31 +        if (a >= (BASE << 13)) a -= (BASE << 13); \
  117.32 +        if (a >= (BASE << 12)) a -= (BASE << 12); \
  117.33 +        if (a >= (BASE << 11)) a -= (BASE << 11); \
  117.34 +        if (a >= (BASE << 10)) a -= (BASE << 10); \
  117.35 +        if (a >= (BASE << 9)) a -= (BASE << 9); \
  117.36 +        if (a >= (BASE << 8)) a -= (BASE << 8); \
  117.37 +        if (a >= (BASE << 7)) a -= (BASE << 7); \
  117.38 +        if (a >= (BASE << 6)) a -= (BASE << 6); \
  117.39 +        if (a >= (BASE << 5)) a -= (BASE << 5); \
  117.40 +        if (a >= (BASE << 4)) a -= (BASE << 4); \
  117.41 +        if (a >= (BASE << 3)) a -= (BASE << 3); \
  117.42 +        if (a >= (BASE << 2)) a -= (BASE << 2); \
  117.43 +        if (a >= (BASE << 1)) a -= (BASE << 1); \
  117.44 +        if (a >= BASE) a -= BASE; \
  117.45 +    } while (0)
  117.46 +#  define MOD4(a) \
  117.47 +    do { \
  117.48 +        if (a >= (BASE << 4)) a -= (BASE << 4); \
  117.49 +        if (a >= (BASE << 3)) a -= (BASE << 3); \
  117.50 +        if (a >= (BASE << 2)) a -= (BASE << 2); \
  117.51 +        if (a >= (BASE << 1)) a -= (BASE << 1); \
  117.52 +        if (a >= BASE) a -= BASE; \
  117.53 +    } while (0)
  117.54 +#else
  117.55 +#  define MOD(a) a %= BASE
  117.56 +#  define MOD4(a) a %= BASE
  117.57 +#endif
  117.58 +
  117.59 +/* ========================================================================= */
  117.60 +uLong ZEXPORT adler32(adler, buf, len)
  117.61 +    uLong adler;
  117.62 +    const Bytef *buf;
  117.63 +    uInt len;
  117.64 +{
  117.65 +    unsigned long sum2;
  117.66 +    unsigned n;
  117.67 +
  117.68 +    /* split Adler-32 into component sums */
  117.69 +    sum2 = (adler >> 16) & 0xffff;
  117.70 +    adler &= 0xffff;
  117.71 +
  117.72 +    /* in case user likes doing a byte at a time, keep it fast */
  117.73 +    if (len == 1) {
  117.74 +        adler += buf[0];
  117.75 +        if (adler >= BASE)
  117.76 +            adler -= BASE;
  117.77 +        sum2 += adler;
  117.78 +        if (sum2 >= BASE)
  117.79 +            sum2 -= BASE;
  117.80 +        return adler | (sum2 << 16);
  117.81 +    }
  117.82 +
  117.83 +    /* initial Adler-32 value (deferred check for len == 1 speed) */
  117.84 +    if (buf == Z_NULL)
  117.85 +        return 1L;
  117.86 +
  117.87 +    /* in case short lengths are provided, keep it somewhat fast */
  117.88 +    if (len < 16) {
  117.89 +        while (len--) {
  117.90 +            adler += *buf++;
  117.91 +            sum2 += adler;
  117.92 +        }
  117.93 +        if (adler >= BASE)
  117.94 +            adler -= BASE;
  117.95 +        MOD4(sum2);             /* only added so many BASE's */
  117.96 +        return adler | (sum2 << 16);
  117.97 +    }
  117.98 +
  117.99 +    /* do length NMAX blocks -- requires just one modulo operation */
 117.100 +    while (len >= NMAX) {
 117.101 +        len -= NMAX;
 117.102 +        n = NMAX / 16;          /* NMAX is divisible by 16 */
 117.103 +        do {
 117.104 +            DO16(buf);          /* 16 sums unrolled */
 117.105 +            buf += 16;
 117.106 +        } while (--n);
 117.107 +        MOD(adler);
 117.108 +        MOD(sum2);
 117.109 +    }
 117.110 +
 117.111 +    /* do remaining bytes (less than NMAX, still just one modulo) */
 117.112 +    if (len) {                  /* avoid modulos if none remaining */
 117.113 +        while (len >= 16) {
 117.114 +            len -= 16;
 117.115 +            DO16(buf);
 117.116 +            buf += 16;
 117.117 +        }
 117.118 +        while (len--) {
 117.119 +            adler += *buf++;
 117.120 +            sum2 += adler;
 117.121 +        }
 117.122 +        MOD(adler);
 117.123 +        MOD(sum2);
 117.124 +    }
 117.125 +
 117.126 +    /* return recombined sums */
 117.127 +    return adler | (sum2 << 16);
 117.128 +}
 117.129 +
 117.130 +/* ========================================================================= */
 117.131 +uLong ZEXPORT adler32_combine(adler1, adler2, len2)
 117.132 +    uLong adler1;
 117.133 +    uLong adler2;
 117.134 +    z_off_t len2;
 117.135 +{
 117.136 +    unsigned long sum1;
 117.137 +    unsigned long sum2;
 117.138 +    unsigned rem;
 117.139 +
 117.140 +    /* the derivation of this formula is left as an exercise for the reader */
 117.141 +    rem = (unsigned)(len2 % BASE);
 117.142 +    sum1 = adler1 & 0xffff;
 117.143 +    sum2 = rem * sum1;
 117.144 +    MOD(sum2);
 117.145 +    sum1 += (adler2 & 0xffff) + BASE - 1;
 117.146 +    sum2 += ((adler1 >> 16) & 0xffff) + ((adler2 >> 16) & 0xffff) + BASE - rem;
 117.147 +    if (sum1 > BASE) sum1 -= BASE;
 117.148 +    if (sum1 > BASE) sum1 -= BASE;
 117.149 +    if (sum2 > (BASE << 1)) sum2 -= (BASE << 1);
 117.150 +    if (sum2 > BASE) sum2 -= BASE;
 117.151 +    return sum1 | (sum2 << 16);
 117.152 +}
   118.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   118.2 +++ b/libs/zlib/compress.c	Sat Sep 19 05:51:51 2015 +0300
   118.3 @@ -0,0 +1,79 @@
   118.4 +/* compress.c -- compress a memory buffer
   118.5 + * Copyright (C) 1995-2003 Jean-loup Gailly.
   118.6 + * For conditions of distribution and use, see copyright notice in zlib.h
   118.7 + */
   118.8 +
   118.9 +/* @(#) $Id$ */
  118.10 +
  118.11 +#define ZLIB_INTERNAL
  118.12 +#include "zlib.h"
  118.13 +
  118.14 +/* ===========================================================================
  118.15 +     Compresses the source buffer into the destination buffer. The level
  118.16 +   parameter has the same meaning as in deflateInit.  sourceLen is the byte
  118.17 +   length of the source buffer. Upon entry, destLen is the total size of the
  118.18 +   destination buffer, which must be at least 0.1% larger than sourceLen plus
  118.19 +   12 bytes. Upon exit, destLen is the actual size of the compressed buffer.
  118.20 +
  118.21 +     compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
  118.22 +   memory, Z_BUF_ERROR if there was not enough room in the output buffer,
  118.23 +   Z_STREAM_ERROR if the level parameter is invalid.
  118.24 +*/
  118.25 +int ZEXPORT compress2 (dest, destLen, source, sourceLen, level)
  118.26 +    Bytef *dest;
  118.27 +    uLongf *destLen;
  118.28 +    const Bytef *source;
  118.29 +    uLong sourceLen;
  118.30 +    int level;
  118.31 +{
  118.32 +    z_stream stream;
  118.33 +    int err;
  118.34 +
  118.35 +    stream.next_in = (Bytef*)source;
  118.36 +    stream.avail_in = (uInt)sourceLen;
  118.37 +#ifdef MAXSEG_64K
  118.38 +    /* Check for source > 64K on 16-bit machine: */
  118.39 +    if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR;
  118.40 +#endif
  118.41 +    stream.next_out = dest;
  118.42 +    stream.avail_out = (uInt)*destLen;
  118.43 +    if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;
  118.44 +
  118.45 +    stream.zalloc = (alloc_func)0;
  118.46 +    stream.zfree = (free_func)0;
  118.47 +    stream.opaque = (voidpf)0;
  118.48 +
  118.49 +    err = deflateInit(&stream, level);
  118.50 +    if (err != Z_OK) return err;
  118.51 +
  118.52 +    err = deflate(&stream, Z_FINISH);
  118.53 +    if (err != Z_STREAM_END) {
  118.54 +        deflateEnd(&stream);
  118.55 +        return err == Z_OK ? Z_BUF_ERROR : err;
  118.56 +    }
  118.57 +    *destLen = stream.total_out;
  118.58 +
  118.59 +    err = deflateEnd(&stream);
  118.60 +    return err;
  118.61 +}
  118.62 +
  118.63 +/* ===========================================================================
  118.64 + */
  118.65 +int ZEXPORT compress (dest, destLen, source, sourceLen)
  118.66 +    Bytef *dest;
  118.67 +    uLongf *destLen;
  118.68 +    const Bytef *source;
  118.69 +    uLong sourceLen;
  118.70 +{
  118.71 +    return compress2(dest, destLen, source, sourceLen, Z_DEFAULT_COMPRESSION);
  118.72 +}
  118.73 +
  118.74 +/* ===========================================================================
  118.75 +     If the default memLevel or windowBits for deflateInit() is changed, then
  118.76 +   this function needs to be updated.
  118.77 + */
  118.78 +uLong ZEXPORT compressBound (sourceLen)
  118.79 +    uLong sourceLen;
  118.80 +{
  118.81 +    return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) + 11;
  118.82 +}
   119.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   119.2 +++ b/libs/zlib/crc32.c	Sat Sep 19 05:51:51 2015 +0300
   119.3 @@ -0,0 +1,423 @@
   119.4 +/* crc32.c -- compute the CRC-32 of a data stream
   119.5 + * Copyright (C) 1995-2005 Mark Adler
   119.6 + * For conditions of distribution and use, see copyright notice in zlib.h
   119.7 + *
   119.8 + * Thanks to Rodney Brown <rbrown64@csc.com.au> for his contribution of faster
   119.9 + * CRC methods: exclusive-oring 32 bits of data at a time, and pre-computing
  119.10 + * tables for updating the shift register in one step with three exclusive-ors
  119.11 + * instead of four steps with four exclusive-ors.  This results in about a
  119.12 + * factor of two increase in speed on a Power PC G4 (PPC7455) using gcc -O3.
  119.13 + */
  119.14 +
  119.15 +/* @(#) $Id$ */
  119.16 +
  119.17 +/*
  119.18 +  Note on the use of DYNAMIC_CRC_TABLE: there is no mutex or semaphore
  119.19 +  protection on the static variables used to control the first-use generation
  119.20 +  of the crc tables.  Therefore, if you #define DYNAMIC_CRC_TABLE, you should
  119.21 +  first call get_crc_table() to initialize the tables before allowing more than
  119.22 +  one thread to use crc32().
  119.23 + */
  119.24 +
  119.25 +#ifdef MAKECRCH
  119.26 +#  include <stdio.h>
  119.27 +#  ifndef DYNAMIC_CRC_TABLE
  119.28 +#    define DYNAMIC_CRC_TABLE
  119.29 +#  endif /* !DYNAMIC_CRC_TABLE */
  119.30 +#endif /* MAKECRCH */
  119.31 +
  119.32 +#include "zutil.h"      /* for STDC and FAR definitions */
  119.33 +
  119.34 +#define local static
  119.35 +
  119.36 +/* Find a four-byte integer type for crc32_little() and crc32_big(). */
  119.37 +#ifndef NOBYFOUR
  119.38 +#  ifdef STDC           /* need ANSI C limits.h to determine sizes */
  119.39 +#    include <limits.h>
  119.40 +#    define BYFOUR
  119.41 +#    if (UINT_MAX == 0xffffffffUL)
  119.42 +       typedef unsigned int u4;
  119.43 +#    else
  119.44 +#      if (ULONG_MAX == 0xffffffffUL)
  119.45 +         typedef unsigned long u4;
  119.46 +#      else
  119.47 +#        if (USHRT_MAX == 0xffffffffUL)
  119.48 +           typedef unsigned short u4;
  119.49 +#        else
  119.50 +#          undef BYFOUR     /* can't find a four-byte integer type! */
  119.51 +#        endif
  119.52 +#      endif
  119.53 +#    endif
  119.54 +#  endif /* STDC */
  119.55 +#endif /* !NOBYFOUR */
  119.56 +
  119.57 +/* Definitions for doing the crc four data bytes at a time. */
  119.58 +#ifdef BYFOUR
  119.59 +#  define REV(w) (((w)>>24)+(((w)>>8)&0xff00)+ \
  119.60 +                (((w)&0xff00)<<8)+(((w)&0xff)<<24))
  119.61 +   local unsigned long crc32_little OF((unsigned long,
  119.62 +                        const unsigned char FAR *, unsigned));
  119.63 +   local unsigned long crc32_big OF((unsigned long,
  119.64 +                        const unsigned char FAR *, unsigned));
  119.65 +#  define TBLS 8
  119.66 +#else
  119.67 +#  define TBLS 1
  119.68 +#endif /* BYFOUR */
  119.69 +
  119.70 +/* Local functions for crc concatenation */
  119.71 +local unsigned long gf2_matrix_times OF((unsigned long *mat,
  119.72 +                                         unsigned long vec));
  119.73 +local void gf2_matrix_square OF((unsigned long *square, unsigned long *mat));
  119.74 +
  119.75 +#ifdef DYNAMIC_CRC_TABLE
  119.76 +
  119.77 +local volatile int crc_table_empty = 1;
  119.78 +local unsigned long FAR crc_table[TBLS][256];
  119.79 +local void make_crc_table OF((void));
  119.80 +#ifdef MAKECRCH
  119.81 +   local void write_table OF((FILE *, const unsigned long FAR *));
  119.82 +#endif /* MAKECRCH */
  119.83 +/*
  119.84 +  Generate tables for a byte-wise 32-bit CRC calculation on the polynomial:
  119.85 +  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.
  119.86 +
  119.87 +  Polynomials over GF(2) are represented in binary, one bit per coefficient,
  119.88 +  with the lowest powers in the most significant bit.  Then adding polynomials
  119.89 +  is just exclusive-or, and multiplying a polynomial by x is a right shift by
  119.90 +  one.  If we call the above polynomial p, and represent a byte as the
  119.91 +  polynomial q, also with the lowest power in the most significant bit (so the
  119.92 +  byte 0xb1 is the polynomial x^7+x^3+x+1), then the CRC is (q*x^32) mod p,
  119.93 +  where a mod b means the remainder after dividing a by b.
  119.94 +
  119.95 +  This calculation is done using the shift-register method of multiplying and
  119.96 +  taking the remainder.  The register is initialized to zero, and for each
  119.97 +  incoming bit, x^32 is added mod p to the register if the bit is a one (where
  119.98 +  x^32 mod p is p+x^32 = x^26+...+1), and the register is multiplied mod p by
  119.99 +  x (which is shifting right by one and adding x^32 mod p if the bit shifted
 119.100 +  out is a one).  We start with the highest power (least significant bit) of
 119.101 +  q and repeat for all eight bits of q.
 119.102 +
 119.103 +  The first table is simply the CRC of all possible eight bit values.  This is
 119.104 +  all the information needed to generate CRCs on data a byte at a time for all
 119.105 +  combinations of CRC register values and incoming bytes.  The remaining tables
 119.106 +  allow for word-at-a-time CRC calculation for both big-endian and little-
 119.107 +  endian machines, where a word is four bytes.
 119.108 +*/
 119.109 +local void make_crc_table()
 119.110 +{
 119.111 +    unsigned long c;
 119.112 +    int n, k;
 119.113 +    unsigned long poly;                 /* polynomial exclusive-or pattern */
 119.114 +    /* terms of polynomial defining this crc (except x^32): */
 119.115 +    static volatile int first = 1;      /* flag to limit concurrent making */
 119.116 +    static const unsigned char p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26};
 119.117 +
 119.118 +    /* See if another task is already doing this (not thread-safe, but better
 119.119 +       than nothing -- significantly reduces duration of vulnerability in
 119.120 +       case the advice about DYNAMIC_CRC_TABLE is ignored) */
 119.121 +    if (first) {
 119.122 +        first = 0;
 119.123 +
 119.124 +        /* make exclusive-or pattern from polynomial (0xedb88320UL) */
 119.125 +        poly = 0UL;
 119.126 +        for (n = 0; n < sizeof(p)/sizeof(unsigned char); n++)
 119.127 +            poly |= 1UL << (31 - p[n]);
 119.128 +
 119.129 +        /* generate a crc for every 8-bit value */
 119.130 +        for (n = 0; n < 256; n++) {
 119.131 +            c = (unsigned long)n;
 119.132 +            for (k = 0; k < 8; k++)
 119.133 +                c = c & 1 ? poly ^ (c >> 1) : c >> 1;
 119.134 +            crc_table[0][n] = c;
 119.135 +        }
 119.136 +
 119.137 +#ifdef BYFOUR
 119.138 +        /* generate crc for each value followed by one, two, and three zeros,
 119.139 +           and then the byte reversal of those as well as the first table */
 119.140 +        for (n = 0; n < 256; n++) {
 119.141 +            c = crc_table[0][n];
 119.142 +            crc_table[4][n] = REV(c);
 119.143 +            for (k = 1; k < 4; k++) {
 119.144 +                c = crc_table[0][c & 0xff] ^ (c >> 8);
 119.145 +                crc_table[k][n] = c;
 119.146 +                crc_table[k + 4][n] = REV(c);
 119.147 +            }
 119.148 +        }
 119.149 +#endif /* BYFOUR */
 119.150 +
 119.151 +        crc_table_empty = 0;
 119.152 +    }
 119.153 +    else {      /* not first */
 119.154 +        /* wait for the other guy to finish (not efficient, but rare) */
 119.155 +        while (crc_table_empty)
 119.156 +            ;
 119.157 +    }
 119.158 +
 119.159 +#ifdef MAKECRCH
 119.160 +    /* write out CRC tables to crc32.h */
 119.161 +    {
 119.162 +        FILE *out;
 119.163 +
 119.164 +        out = fopen("crc32.h", "w");
 119.165 +        if (out == NULL) return;
 119.166 +        fprintf(out, "/* crc32.h -- tables for rapid CRC calculation\n");
 119.167 +        fprintf(out, " * Generated automatically by crc32.c\n */\n\n");
 119.168 +        fprintf(out, "local const unsigned long FAR ");
 119.169 +        fprintf(out, "crc_table[TBLS][256] =\n{\n  {\n");
 119.170 +        write_table(out, crc_table[0]);
 119.171 +#  ifdef BYFOUR
 119.172 +        fprintf(out, "#ifdef BYFOUR\n");
 119.173 +        for (k = 1; k < 8; k++) {
 119.174 +            fprintf(out, "  },\n  {\n");
 119.175 +            write_table(out, crc_table[k]);
 119.176 +        }
 119.177 +        fprintf(out, "#endif\n");
 119.178 +#  endif /* BYFOUR */
 119.179 +        fprintf(out, "  }\n};\n");
 119.180 +        fclose(out);
 119.181 +    }
 119.182 +#endif /* MAKECRCH */
 119.183 +}
 119.184 +
 119.185 +#ifdef MAKECRCH
 119.186 +local void write_table(out, table)
 119.187 +    FILE *out;
 119.188 +    const unsigned long FAR *table;
 119.189 +{
 119.190 +    int n;
 119.191 +
 119.192 +    for (n = 0; n < 256; n++)
 119.193 +        fprintf(out, "%s0x%08lxUL%s", n % 5 ? "" : "    ", table[n],
 119.194 +                n == 255 ? "\n" : (n % 5 == 4 ? ",\n" : ", "));
 119.195 +}
 119.196 +#endif /* MAKECRCH */
 119.197 +
 119.198 +#else /* !DYNAMIC_CRC_TABLE */
 119.199 +/* ========================================================================
 119.200 + * Tables of CRC-32s of all single-byte values, made by make_crc_table().
 119.201 + */
 119.202 +#include "crc32.h"
 119.203 +#endif /* DYNAMIC_CRC_TABLE */
 119.204 +
 119.205 +/* =========================================================================
 119.206 + * This function can be used by asm versions of crc32()
 119.207 + */
 119.208 +const unsigned long FAR * ZEXPORT get_crc_table()
 119.209 +{
 119.210 +#ifdef DYNAMIC_CRC_TABLE
 119.211 +    if (crc_table_empty)
 119.212 +        make_crc_table();
 119.213 +#endif /* DYNAMIC_CRC_TABLE */
 119.214 +    return (const unsigned long FAR *)crc_table;
 119.215 +}
 119.216 +
 119.217 +/* ========================================================================= */
 119.218 +#define DO1 crc = crc_table[0][((int)crc ^ (*buf++)) & 0xff] ^ (crc >> 8)
 119.219 +#define DO8 DO1; DO1; DO1; DO1; DO1; DO1; DO1; DO1
 119.220 +
 119.221 +/* ========================================================================= */
 119.222 +unsigned long ZEXPORT crc32(crc, buf, len)
 119.223 +    unsigned long crc;
 119.224 +    const unsigned char FAR *buf;
 119.225 +    unsigned len;
 119.226 +{
 119.227 +    if (buf == Z_NULL) return 0UL;
 119.228 +
 119.229 +#ifdef DYNAMIC_CRC_TABLE
 119.230 +    if (crc_table_empty)
 119.231 +        make_crc_table();
 119.232 +#endif /* DYNAMIC_CRC_TABLE */
 119.233 +
 119.234 +#ifdef BYFOUR
 119.235 +    if (sizeof(void *) == sizeof(ptrdiff_t)) {
 119.236 +        u4 endian;
 119.237 +
 119.238 +        endian = 1;
 119.239 +        if (*((unsigned char *)(&endian)))
 119.240 +            return crc32_little(crc, buf, len);
 119.241 +        else
 119.242 +            return crc32_big(crc, buf, len);
 119.243 +    }
 119.244 +#endif /* BYFOUR */
 119.245 +    crc = crc ^ 0xffffffffUL;
 119.246 +    while (len >= 8) {
 119.247 +        DO8;
 119.248 +        len -= 8;
 119.249 +    }
 119.250 +    if (len) do {
 119.251 +        DO1;
 119.252 +    } while (--len);
 119.253 +    return crc ^ 0xffffffffUL;
 119.254 +}
 119.255 +
 119.256 +#ifdef BYFOUR
 119.257 +
 119.258 +/* ========================================================================= */
 119.259 +#define DOLIT4 c ^= *buf4++; \
 119.260 +        c = crc_table[3][c & 0xff] ^ crc_table[2][(c >> 8) & 0xff] ^ \
 119.261 +            crc_table[1][(c >> 16) & 0xff] ^ crc_table[0][c >> 24]
 119.262 +#define DOLIT32 DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4
 119.263 +
 119.264 +/* ========================================================================= */
 119.265 +local unsigned long crc32_little(crc, buf, len)
 119.266 +    unsigned long crc;
 119.267 +    const unsigned char FAR *buf;
 119.268 +    unsigned len;
 119.269 +{
 119.270 +    register u4 c;
 119.271 +    register const u4 FAR *buf4;
 119.272 +
 119.273 +    c = (u4)crc;
 119.274 +    c = ~c;
 119.275 +    while (len && ((ptrdiff_t)buf & 3)) {
 119.276 +        c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8);
 119.277 +        len--;
 119.278 +    }
 119.279 +
 119.280 +    buf4 = (const u4 FAR *)(const void FAR *)buf;
 119.281 +    while (len >= 32) {
 119.282 +        DOLIT32;
 119.283 +        len -= 32;
 119.284 +    }
 119.285 +    while (len >= 4) {
 119.286 +        DOLIT4;
 119.287 +        len -= 4;
 119.288 +    }
 119.289 +    buf = (const unsigned char FAR *)buf4;
 119.290 +
 119.291 +    if (len) do {
 119.292 +        c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8);
 119.293 +    } while (--len);
 119.294 +    c = ~c;
 119.295 +    return (unsigned long)c;
 119.296 +}
 119.297 +
 119.298 +/* ========================================================================= */
 119.299 +#define DOBIG4 c ^= *++buf4; \
 119.300 +        c = crc_table[4][c & 0xff] ^ crc_table[5][(c >> 8) & 0xff] ^ \
 119.301 +            crc_table[6][(c >> 16) & 0xff] ^ crc_table[7][c >> 24]
 119.302 +#define DOBIG32 DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4
 119.303 +
 119.304 +/* ========================================================================= */
 119.305 +local unsigned long crc32_big(crc, buf, len)
 119.306 +    unsigned long crc;
 119.307 +    const unsigned char FAR *buf;
 119.308 +    unsigned len;
 119.309 +{
 119.310 +    register u4 c;
 119.311 +    register const u4 FAR *buf4;
 119.312 +
 119.313 +    c = REV((u4)crc);
 119.314 +    c = ~c;
 119.315 +    while (len && ((ptrdiff_t)buf & 3)) {
 119.316 +        c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8);
 119.317 +        len--;
 119.318 +    }
 119.319 +
 119.320 +    buf4 = (const u4 FAR *)(const void FAR *)buf;
 119.321 +    buf4--;
 119.322 +    while (len >= 32) {
 119.323 +        DOBIG32;
 119.324 +        len -= 32;
 119.325 +    }
 119.326 +    while (len >= 4) {
 119.327 +        DOBIG4;
 119.328 +        len -= 4;
 119.329 +    }
 119.330 +    buf4++;
 119.331 +    buf = (const unsigned char FAR *)buf4;
 119.332 +
 119.333 +    if (len) do {
 119.334 +        c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8);
 119.335 +    } while (--len);
 119.336 +    c = ~c;
 119.337 +    return (unsigned long)(REV(c));
 119.338 +}
 119.339 +
 119.340 +#endif /* BYFOUR */
 119.341 +
 119.342 +#define GF2_DIM 32      /* dimension of GF(2) vectors (length of CRC) */
 119.343 +
 119.344 +/* ========================================================================= */
 119.345 +local unsigned long gf2_matrix_times(mat, vec)
 119.346 +    unsigned long *mat;
 119.347 +    unsigned long vec;
 119.348 +{
 119.349 +    unsigned long sum;
 119.350 +
 119.351 +    sum = 0;
 119.352 +    while (vec) {
 119.353 +        if (vec & 1)
 119.354 +            sum ^= *mat;
 119.355 +        vec >>= 1;
 119.356 +        mat++;
 119.357 +    }
 119.358 +    return sum;
 119.359 +}
 119.360 +
 119.361 +/* ========================================================================= */
 119.362 +local void gf2_matrix_square(square, mat)
 119.363 +    unsigned long *square;
 119.364 +    unsigned long *mat;
 119.365 +{
 119.366 +    int n;
 119.367 +
 119.368 +    for (n = 0; n < GF2_DIM; n++)
 119.369 +        square[n] = gf2_matrix_times(mat, mat[n]);
 119.370 +}
 119.371 +
 119.372 +/* ========================================================================= */
 119.373 +uLong ZEXPORT crc32_combine(crc1, crc2, len2)
 119.374 +    uLong crc1;
 119.375 +    uLong crc2;
 119.376 +    z_off_t len2;
 119.377 +{
 119.378 +    int n;
 119.379 +    unsigned long row;
 119.380 +    unsigned long even[GF2_DIM];    /* even-power-of-two zeros operator */
 119.381 +    unsigned long odd[GF2_DIM];     /* odd-power-of-two zeros operator */
 119.382 +
 119.383 +    /* degenerate case */
 119.384 +    if (len2 == 0)
 119.385 +        return crc1;
 119.386 +
 119.387 +    /* put operator for one zero bit in odd */
 119.388 +    odd[0] = 0xedb88320L;           /* CRC-32 polynomial */
 119.389 +    row = 1;
 119.390 +    for (n = 1; n < GF2_DIM; n++) {
 119.391 +        odd[n] = row;
 119.392 +        row <<= 1;
 119.393 +    }
 119.394 +
 119.395 +    /* put operator for two zero bits in even */
 119.396 +    gf2_matrix_square(even, odd);
 119.397 +
 119.398 +    /* put operator for four zero bits in odd */
 119.399 +    gf2_matrix_square(odd, even);
 119.400 +
 119.401 +    /* apply len2 zeros to crc1 (first square will put the operator for one
 119.402 +       zero byte, eight zero bits, in even) */
 119.403 +    do {
 119.404 +        /* apply zeros operator for this bit of len2 */
 119.405 +        gf2_matrix_square(even, odd);
 119.406 +        if (len2 & 1)
 119.407 +            crc1 = gf2_matrix_times(even, crc1);
 119.408 +        len2 >>= 1;
 119.409 +
 119.410 +        /* if no more bits set, then done */
 119.411 +        if (len2 == 0)
 119.412 +            break;
 119.413 +
 119.414 +        /* another iteration of the loop with odd and even swapped */
 119.415 +        gf2_matrix_square(odd, even);
 119.416 +        if (len2 & 1)
 119.417 +            crc1 = gf2_matrix_times(odd, crc1);
 119.418 +        len2 >>= 1;
 119.419 +
 119.420 +        /* if no more bits set, then done */
 119.421 +    } while (len2 != 0);
 119.422 +
 119.423 +    /* return combined crc */
 119.424 +    crc1 ^= crc2;
 119.425 +    return crc1;
 119.426 +}
   120.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   120.2 +++ b/libs/zlib/crc32.h	Sat Sep 19 05:51:51 2015 +0300
   120.3 @@ -0,0 +1,441 @@
   120.4 +/* crc32.h -- tables for rapid CRC calculation
   120.5 + * Generated automatically by crc32.c
   120.6 + */
   120.7 +
   120.8 +local const unsigned long FAR crc_table[TBLS][256] =
   120.9 +{
  120.10 +  {
  120.11 +    0x00000000UL, 0x77073096UL, 0xee0e612cUL, 0x990951baUL, 0x076dc419UL,
  120.12 +    0x706af48fUL, 0xe963a535UL, 0x9e6495a3UL, 0x0edb8832UL, 0x79dcb8a4UL,
  120.13 +    0xe0d5e91eUL, 0x97d2d988UL, 0x09b64c2bUL, 0x7eb17cbdUL, 0xe7b82d07UL,
  120.14 +    0x90bf1d91UL, 0x1db71064UL, 0x6ab020f2UL, 0xf3b97148UL, 0x84be41deUL,
  120.15 +    0x1adad47dUL, 0x6ddde4ebUL, 0xf4d4b551UL, 0x83d385c7UL, 0x136c9856UL,
  120.16 +    0x646ba8c0UL, 0xfd62f97aUL, 0x8a65c9ecUL, 0x14015c4fUL, 0x63066cd9UL,
  120.17 +    0xfa0f3d63UL, 0x8d080df5UL, 0x3b6e20c8UL, 0x4c69105eUL, 0xd56041e4UL,
  120.18 +    0xa2677172UL, 0x3c03e4d1UL, 0x4b04d447UL, 0xd20d85fdUL, 0xa50ab56bUL,
  120.19 +    0x35b5a8faUL, 0x42b2986cUL, 0xdbbbc9d6UL, 0xacbcf940UL, 0x32d86ce3UL,
  120.20 +    0x45df5c75UL, 0xdcd60dcfUL, 0xabd13d59UL, 0x26d930acUL, 0x51de003aUL,
  120.21 +    0xc8d75180UL, 0xbfd06116UL, 0x21b4f4b5UL, 0x56b3c423UL, 0xcfba9599UL,
  120.22 +    0xb8bda50fUL, 0x2802b89eUL, 0x5f058808UL, 0xc60cd9b2UL, 0xb10be924UL,
  120.23 +    0x2f6f7c87UL, 0x58684c11UL, 0xc1611dabUL, 0xb6662d3dUL, 0x76dc4190UL,
  120.24 +    0x01db7106UL, 0x98d220bcUL, 0xefd5102aUL, 0x71b18589UL, 0x06b6b51fUL,
  120.25 +    0x9fbfe4a5UL, 0xe8b8d433UL, 0x7807c9a2UL, 0x0f00f934UL, 0x9609a88eUL,
  120.26 +    0xe10e9818UL, 0x7f6a0dbbUL, 0x086d3d2dUL, 0x91646c97UL, 0xe6635c01UL,
  120.27 +    0x6b6b51f4UL, 0x1c6c6162UL, 0x856530d8UL, 0xf262004eUL, 0x6c0695edUL,
  120.28 +    0x1b01a57bUL, 0x8208f4c1UL, 0xf50fc457UL, 0x65b0d9c6UL, 0x12b7e950UL,
  120.29 +    0x8bbeb8eaUL, 0xfcb9887cUL, 0x62dd1ddfUL, 0x15da2d49UL, 0x8cd37cf3UL,
  120.30 +    0xfbd44c65UL, 0x4db26158UL, 0x3ab551ceUL, 0xa3bc0074UL, 0xd4bb30e2UL,
  120.31 +    0x4adfa541UL, 0x3dd895d7UL, 0xa4d1c46dUL, 0xd3d6f4fbUL, 0x4369e96aUL,
  120.32 +    0x346ed9fcUL, 0xad678846UL, 0xda60b8d0UL, 0x44042d73UL, 0x33031de5UL,
  120.33 +    0xaa0a4c5fUL, 0xdd0d7cc9UL, 0x5005713cUL, 0x270241aaUL, 0xbe0b1010UL,
  120.34 +    0xc90c2086UL, 0x5768b525UL, 0x206f85b3UL, 0xb966d409UL, 0xce61e49fUL,
  120.35 +    0x5edef90eUL, 0x29d9c998UL, 0xb0d09822UL, 0xc7d7a8b4UL, 0x59b33d17UL,
  120.36 +    0x2eb40d81UL, 0xb7bd5c3bUL, 0xc0ba6cadUL, 0xedb88320UL, 0x9abfb3b6UL,
  120.37 +    0x03b6e20cUL, 0x74b1d29aUL, 0xead54739UL, 0x9dd277afUL, 0x04db2615UL,
  120.38 +    0x73dc1683UL, 0xe3630b12UL, 0x94643b84UL, 0x0d6d6a3eUL, 0x7a6a5aa8UL,
  120.39 +    0xe40ecf0bUL, 0x9309ff9dUL, 0x0a00ae27UL, 0x7d079eb1UL, 0xf00f9344UL,
  120.40 +    0x8708a3d2UL, 0x1e01f268UL, 0x6906c2feUL, 0xf762575dUL, 0x806567cbUL,
  120.41 +    0x196c3671UL, 0x6e6b06e7UL, 0xfed41b76UL, 0x89d32be0UL, 0x10da7a5aUL,
  120.42 +    0x67dd4accUL, 0xf9b9df6fUL, 0x8ebeeff9UL, 0x17b7be43UL, 0x60b08ed5UL,
  120.43 +    0xd6d6a3e8UL, 0xa1d1937eUL, 0x38d8c2c4UL, 0x4fdff252UL, 0xd1bb67f1UL,
  120.44 +    0xa6bc5767UL, 0x3fb506ddUL, 0x48b2364bUL, 0xd80d2bdaUL, 0xaf0a1b4cUL,
  120.45 +    0x36034af6UL, 0x41047a60UL, 0xdf60efc3UL, 0xa867df55UL, 0x316e8eefUL,
  120.46 +    0x4669be79UL, 0xcb61b38cUL, 0xbc66831aUL, 0x256fd2a0UL, 0x5268e236UL,
  120.47 +    0xcc0c7795UL, 0xbb0b4703UL, 0x220216b9UL, 0x5505262fUL, 0xc5ba3bbeUL,
  120.48 +    0xb2bd0b28UL, 0x2bb45a92UL, 0x5cb36a04UL, 0xc2d7ffa7UL, 0xb5d0cf31UL,
  120.49 +    0x2cd99e8bUL, 0x5bdeae1dUL, 0x9b64c2b0UL, 0xec63f226UL, 0x756aa39cUL,
  120.50 +    0x026d930aUL, 0x9c0906a9UL, 0xeb0e363fUL, 0x72076785UL, 0x05005713UL,
  120.51 +    0x95bf4a82UL, 0xe2b87a14UL, 0x7bb12baeUL, 0x0cb61b38UL, 0x92d28e9bUL,
  120.52 +    0xe5d5be0dUL, 0x7cdcefb7UL, 0x0bdbdf21UL, 0x86d3d2d4UL, 0xf1d4e242UL,
  120.53 +    0x68ddb3f8UL, 0x1fda836eUL, 0x81be16cdUL, 0xf6b9265bUL, 0x6fb077e1UL,
  120.54 +    0x18b74777UL, 0x88085ae6UL, 0xff0f6a70UL, 0x66063bcaUL, 0x11010b5cUL,
  120.55 +    0x8f659effUL, 0xf862ae69UL, 0x616bffd3UL, 0x166ccf45UL, 0xa00ae278UL,
  120.56 +    0xd70dd2eeUL, 0x4e048354UL, 0x3903b3c2UL, 0xa7672661UL, 0xd06016f7UL,
  120.57 +    0x4969474dUL, 0x3e6e77dbUL, 0xaed16a4aUL, 0xd9d65adcUL, 0x40df0b66UL,
  120.58 +    0x37d83bf0UL, 0xa9bcae53UL, 0xdebb9ec5UL, 0x47b2cf7fUL, 0x30b5ffe9UL,
  120.59 +    0xbdbdf21cUL, 0xcabac28aUL, 0x53b39330UL, 0x24b4a3a6UL, 0xbad03605UL,
  120.60 +    0xcdd70693UL, 0x54de5729UL, 0x23d967bfUL, 0xb3667a2eUL, 0xc4614ab8UL,
  120.61 +    0x5d681b02UL, 0x2a6f2b94UL, 0xb40bbe37UL, 0xc30c8ea1UL, 0x5a05df1bUL,
  120.62 +    0x2d02ef8dUL
  120.63 +#ifdef BYFOUR
  120.64 +  },
  120.65 +  {
  120.66 +    0x00000000UL, 0x191b3141UL, 0x32366282UL, 0x2b2d53c3UL, 0x646cc504UL,
  120.67 +    0x7d77f445UL, 0x565aa786UL, 0x4f4196c7UL, 0xc8d98a08UL, 0xd1c2bb49UL,
  120.68 +    0xfaefe88aUL, 0xe3f4d9cbUL, 0xacb54f0cUL, 0xb5ae7e4dUL, 0x9e832d8eUL,
  120.69 +    0x87981ccfUL, 0x4ac21251UL, 0x53d92310UL, 0x78f470d3UL, 0x61ef4192UL,
  120.70 +    0x2eaed755UL, 0x37b5e614UL, 0x1c98b5d7UL, 0x05838496UL, 0x821b9859UL,
  120.71 +    0x9b00a918UL, 0xb02dfadbUL, 0xa936cb9aUL, 0xe6775d5dUL, 0xff6c6c1cUL,
  120.72 +    0xd4413fdfUL, 0xcd5a0e9eUL, 0x958424a2UL, 0x8c9f15e3UL, 0xa7b24620UL,
  120.73 +    0xbea97761UL, 0xf1e8e1a6UL, 0xe8f3d0e7UL, 0xc3de8324UL, 0xdac5b265UL,
  120.74 +    0x5d5daeaaUL, 0x44469febUL, 0x6f6bcc28UL, 0x7670fd69UL, 0x39316baeUL,
  120.75 +    0x202a5aefUL, 0x0b07092cUL, 0x121c386dUL, 0xdf4636f3UL, 0xc65d07b2UL,
  120.76 +    0xed705471UL, 0xf46b6530UL, 0xbb2af3f7UL, 0xa231c2b6UL, 0x891c9175UL,
  120.77 +    0x9007a034UL, 0x179fbcfbUL, 0x0e848dbaUL, 0x25a9de79UL, 0x3cb2ef38UL,
  120.78 +    0x73f379ffUL, 0x6ae848beUL, 0x41c51b7dUL, 0x58de2a3cUL, 0xf0794f05UL,
  120.79 +    0xe9627e44UL, 0xc24f2d87UL, 0xdb541cc6UL, 0x94158a01UL, 0x8d0ebb40UL,
  120.80 +    0xa623e883UL, 0xbf38d9c2UL, 0x38a0c50dUL, 0x21bbf44cUL, 0x0a96a78fUL,
  120.81 +    0x138d96ceUL, 0x5ccc0009UL, 0x45d73148UL, 0x6efa628bUL, 0x77e153caUL,
  120.82 +    0xbabb5d54UL, 0xa3a06c15UL, 0x888d3fd6UL, 0x91960e97UL, 0xded79850UL,
  120.83 +    0xc7cca911UL, 0xece1fad2UL, 0xf5facb93UL, 0x7262d75cUL, 0x6b79e61dUL,
  120.84 +    0x4054b5deUL, 0x594f849fUL, 0x160e1258UL, 0x0f152319UL, 0x243870daUL,
  120.85 +    0x3d23419bUL, 0x65fd6ba7UL, 0x7ce65ae6UL, 0x57cb0925UL, 0x4ed03864UL,
  120.86 +    0x0191aea3UL, 0x188a9fe2UL, 0x33a7cc21UL, 0x2abcfd60UL, 0xad24e1afUL,
  120.87 +    0xb43fd0eeUL, 0x9f12832dUL, 0x8609b26cUL, 0xc94824abUL, 0xd05315eaUL,
  120.88 +    0xfb7e4629UL, 0xe2657768UL, 0x2f3f79f6UL, 0x362448b7UL, 0x1d091b74UL,
  120.89 +    0x04122a35UL, 0x4b53bcf2UL, 0x52488db3UL, 0x7965de70UL, 0x607eef31UL,
  120.90 +    0xe7e6f3feUL, 0xfefdc2bfUL, 0xd5d0917cUL, 0xcccba03dUL, 0x838a36faUL,
  120.91 +    0x9a9107bbUL, 0xb1bc5478UL, 0xa8a76539UL, 0x3b83984bUL, 0x2298a90aUL,
  120.92 +    0x09b5fac9UL, 0x10aecb88UL, 0x5fef5d4fUL, 0x46f46c0eUL, 0x6dd93fcdUL,
  120.93 +    0x74c20e8cUL, 0xf35a1243UL, 0xea412302UL, 0xc16c70c1UL, 0xd8774180UL,
  120.94 +    0x9736d747UL, 0x8e2de606UL, 0xa500b5c5UL, 0xbc1b8484UL, 0x71418a1aUL,
  120.95 +    0x685abb5bUL, 0x4377e898UL, 0x5a6cd9d9UL, 0x152d4f1eUL, 0x0c367e5fUL,
  120.96 +    0x271b2d9cUL, 0x3e001cddUL, 0xb9980012UL, 0xa0833153UL, 0x8bae6290UL,
  120.97 +    0x92b553d1UL, 0xddf4c516UL, 0xc4eff457UL, 0xefc2a794UL, 0xf6d996d5UL,
  120.98 +    0xae07bce9UL, 0xb71c8da8UL, 0x9c31de6bUL, 0x852aef2aUL, 0xca6b79edUL,
  120.99 +    0xd37048acUL, 0xf85d1b6fUL, 0xe1462a2eUL, 0x66de36e1UL, 0x7fc507a0UL,
 120.100 +    0x54e85463UL, 0x4df36522UL, 0x02b2f3e5UL, 0x1ba9c2a4UL, 0x30849167UL,
 120.101 +    0x299fa026UL, 0xe4c5aeb8UL, 0xfdde9ff9UL, 0xd6f3cc3aUL, 0xcfe8fd7bUL,
 120.102 +    0x80a96bbcUL, 0x99b25afdUL, 0xb29f093eUL, 0xab84387fUL, 0x2c1c24b0UL,
 120.103 +    0x350715f1UL, 0x1e2a4632UL, 0x07317773UL, 0x4870e1b4UL, 0x516bd0f5UL,
 120.104 +    0x7a468336UL, 0x635db277UL, 0xcbfad74eUL, 0xd2e1e60fUL, 0xf9ccb5ccUL,
 120.105 +    0xe0d7848dUL, 0xaf96124aUL, 0xb68d230bUL, 0x9da070c8UL, 0x84bb4189UL,
 120.106 +    0x03235d46UL, 0x1a386c07UL, 0x31153fc4UL, 0x280e0e85UL, 0x674f9842UL,
 120.107 +    0x7e54a903UL, 0x5579fac0UL, 0x4c62cb81UL, 0x8138c51fUL, 0x9823f45eUL,
 120.108 +    0xb30ea79dUL, 0xaa1596dcUL, 0xe554001bUL, 0xfc4f315aUL, 0xd7626299UL,
 120.109 +    0xce7953d8UL, 0x49e14f17UL, 0x50fa7e56UL, 0x7bd72d95UL, 0x62cc1cd4UL,
 120.110 +    0x2d8d8a13UL, 0x3496bb52UL, 0x1fbbe891UL, 0x06a0d9d0UL, 0x5e7ef3ecUL,
 120.111 +    0x4765c2adUL, 0x6c48916eUL, 0x7553a02fUL, 0x3a1236e8UL, 0x230907a9UL,
 120.112 +    0x0824546aUL, 0x113f652bUL, 0x96a779e4UL, 0x8fbc48a5UL, 0xa4911b66UL,
 120.113 +    0xbd8a2a27UL, 0xf2cbbce0UL, 0xebd08da1UL, 0xc0fdde62UL, 0xd9e6ef23UL,
 120.114 +    0x14bce1bdUL, 0x0da7d0fcUL, 0x268a833fUL, 0x3f91b27eUL, 0x70d024b9UL,
 120.115 +    0x69cb15f8UL, 0x42e6463bUL, 0x5bfd777aUL, 0xdc656bb5UL, 0xc57e5af4UL,
 120.116 +    0xee530937UL, 0xf7483876UL, 0xb809aeb1UL, 0xa1129ff0UL, 0x8a3fcc33UL,
 120.117 +    0x9324fd72UL
 120.118 +  },
 120.119 +  {
 120.120 +    0x00000000UL, 0x01c26a37UL, 0x0384d46eUL, 0x0246be59UL, 0x0709a8dcUL,
 120.121 +    0x06cbc2ebUL, 0x048d7cb2UL, 0x054f1685UL, 0x0e1351b8UL, 0x0fd13b8fUL,
 120.122 +    0x0d9785d6UL, 0x0c55efe1UL, 0x091af964UL, 0x08d89353UL, 0x0a9e2d0aUL,
 120.123 +    0x0b5c473dUL, 0x1c26a370UL, 0x1de4c947UL, 0x1fa2771eUL, 0x1e601d29UL,
 120.124 +    0x1b2f0bacUL, 0x1aed619bUL, 0x18abdfc2UL, 0x1969b5f5UL, 0x1235f2c8UL,
 120.125 +    0x13f798ffUL, 0x11b126a6UL, 0x10734c91UL, 0x153c5a14UL, 0x14fe3023UL,
 120.126 +    0x16b88e7aUL, 0x177ae44dUL, 0x384d46e0UL, 0x398f2cd7UL, 0x3bc9928eUL,
 120.127 +    0x3a0bf8b9UL, 0x3f44ee3cUL, 0x3e86840bUL, 0x3cc03a52UL, 0x3d025065UL,
 120.128 +    0x365e1758UL, 0x379c7d6fUL, 0x35dac336UL, 0x3418a901UL, 0x3157bf84UL,
 120.129 +    0x3095d5b3UL, 0x32d36beaUL, 0x331101ddUL, 0x246be590UL, 0x25a98fa7UL,
 120.130 +    0x27ef31feUL, 0x262d5bc9UL, 0x23624d4cUL, 0x22a0277bUL, 0x20e69922UL,
 120.131 +    0x2124f315UL, 0x2a78b428UL, 0x2bbade1fUL, 0x29fc6046UL, 0x283e0a71UL,
 120.132 +    0x2d711cf4UL, 0x2cb376c3UL, 0x2ef5c89aUL, 0x2f37a2adUL, 0x709a8dc0UL,
 120.133 +    0x7158e7f7UL, 0x731e59aeUL, 0x72dc3399UL, 0x7793251cUL, 0x76514f2bUL,
 120.134 +    0x7417f172UL, 0x75d59b45UL, 0x7e89dc78UL, 0x7f4bb64fUL, 0x7d0d0816UL,
 120.135 +    0x7ccf6221UL, 0x798074a4UL, 0x78421e93UL, 0x7a04a0caUL, 0x7bc6cafdUL,
 120.136 +    0x6cbc2eb0UL, 0x6d7e4487UL, 0x6f38fadeUL, 0x6efa90e9UL, 0x6bb5866cUL,
 120.137 +    0x6a77ec5bUL, 0x68315202UL, 0x69f33835UL, 0x62af7f08UL, 0x636d153fUL,
 120.138 +    0x612bab66UL, 0x60e9c151UL, 0x65a6d7d4UL, 0x6464bde3UL, 0x662203baUL,
 120.139 +    0x67e0698dUL, 0x48d7cb20UL, 0x4915a117UL, 0x4b531f4eUL, 0x4a917579UL,
 120.140 +    0x4fde63fcUL, 0x4e1c09cbUL, 0x4c5ab792UL, 0x4d98dda5UL, 0x46c49a98UL,
 120.141 +    0x4706f0afUL, 0x45404ef6UL, 0x448224c1UL, 0x41cd3244UL, 0x400f5873UL,
 120.142 +    0x4249e62aUL, 0x438b8c1dUL, 0x54f16850UL, 0x55330267UL, 0x5775bc3eUL,
 120.143 +    0x56b7d609UL, 0x53f8c08cUL, 0x523aaabbUL, 0x507c14e2UL, 0x51be7ed5UL,
 120.144 +    0x5ae239e8UL, 0x5b2053dfUL, 0x5966ed86UL, 0x58a487b1UL, 0x5deb9134UL,
 120.145 +    0x5c29fb03UL, 0x5e6f455aUL, 0x5fad2f6dUL, 0xe1351b80UL, 0xe0f771b7UL,
 120.146 +    0xe2b1cfeeUL, 0xe373a5d9UL, 0xe63cb35cUL, 0xe7fed96bUL, 0xe5b86732UL,
 120.147 +    0xe47a0d05UL, 0xef264a38UL, 0xeee4200fUL, 0xeca29e56UL, 0xed60f461UL,
 120.148 +    0xe82fe2e4UL, 0xe9ed88d3UL, 0xebab368aUL, 0xea695cbdUL, 0xfd13b8f0UL,
 120.149 +    0xfcd1d2c7UL, 0xfe976c9eUL, 0xff5506a9UL, 0xfa1a102cUL, 0xfbd87a1bUL,
 120.150 +    0xf99ec442UL, 0xf85cae75UL, 0xf300e948UL, 0xf2c2837fUL, 0xf0843d26UL,
 120.151 +    0xf1465711UL, 0xf4094194UL, 0xf5cb2ba3UL, 0xf78d95faUL, 0xf64fffcdUL,
 120.152 +    0xd9785d60UL, 0xd8ba3757UL, 0xdafc890eUL, 0xdb3ee339UL, 0xde71f5bcUL,
 120.153 +    0xdfb39f8bUL, 0xddf521d2UL, 0xdc374be5UL, 0xd76b0cd8UL, 0xd6a966efUL,
 120.154 +    0xd4efd8b6UL, 0xd52db281UL, 0xd062a404UL, 0xd1a0ce33UL, 0xd3e6706aUL,
 120.155 +    0xd2241a5dUL, 0xc55efe10UL, 0xc49c9427UL, 0xc6da2a7eUL, 0xc7184049UL,
 120.156 +    0xc25756ccUL, 0xc3953cfbUL, 0xc1d382a2UL, 0xc011e895UL, 0xcb4dafa8UL,
 120.157 +    0xca8fc59fUL, 0xc8c97bc6UL, 0xc90b11f1UL, 0xcc440774UL, 0xcd866d43UL,
 120.158 +    0xcfc0d31aUL, 0xce02b92dUL, 0x91af9640UL, 0x906dfc77UL, 0x922b422eUL,
 120.159 +    0x93e92819UL, 0x96a63e9cUL, 0x976454abUL, 0x9522eaf2UL, 0x94e080c5UL,
 120.160 +    0x9fbcc7f8UL, 0x9e7eadcfUL, 0x9c381396UL, 0x9dfa79a1UL, 0x98b56f24UL,
 120.161 +    0x99770513UL, 0x9b31bb4aUL, 0x9af3d17dUL, 0x8d893530UL, 0x8c4b5f07UL,
 120.162 +    0x8e0de15eUL, 0x8fcf8b69UL, 0x8a809decUL, 0x8b42f7dbUL, 0x89044982UL,
 120.163 +    0x88c623b5UL, 0x839a6488UL, 0x82580ebfUL, 0x801eb0e6UL, 0x81dcdad1UL,
 120.164 +    0x8493cc54UL, 0x8551a663UL, 0x8717183aUL, 0x86d5720dUL, 0xa9e2d0a0UL,
 120.165 +    0xa820ba97UL, 0xaa6604ceUL, 0xaba46ef9UL, 0xaeeb787cUL, 0xaf29124bUL,
 120.166 +    0xad6fac12UL, 0xacadc625UL, 0xa7f18118UL, 0xa633eb2fUL, 0xa4755576UL,
 120.167 +    0xa5b73f41UL, 0xa0f829c4UL, 0xa13a43f3UL, 0xa37cfdaaUL, 0xa2be979dUL,
 120.168 +    0xb5c473d0UL, 0xb40619e7UL, 0xb640a7beUL, 0xb782cd89UL, 0xb2cddb0cUL,
 120.169 +    0xb30fb13bUL, 0xb1490f62UL, 0xb08b6555UL, 0xbbd72268UL, 0xba15485fUL,
 120.170 +    0xb853f606UL, 0xb9919c31UL, 0xbcde8ab4UL, 0xbd1ce083UL, 0xbf5a5edaUL,
 120.171 +    0xbe9834edUL
 120.172 +  },
 120.173 +  {
 120.174 +    0x00000000UL, 0xb8bc6765UL, 0xaa09c88bUL, 0x12b5afeeUL, 0x8f629757UL,
 120.175 +    0x37def032UL, 0x256b5fdcUL, 0x9dd738b9UL, 0xc5b428efUL, 0x7d084f8aUL,
 120.176 +    0x6fbde064UL, 0xd7018701UL, 0x4ad6bfb8UL, 0xf26ad8ddUL, 0xe0df7733UL,
 120.177 +    0x58631056UL, 0x5019579fUL, 0xe8a530faUL, 0xfa109f14UL, 0x42acf871UL,
 120.178 +    0xdf7bc0c8UL, 0x67c7a7adUL, 0x75720843UL, 0xcdce6f26UL, 0x95ad7f70UL,
 120.179 +    0x2d111815UL, 0x3fa4b7fbUL, 0x8718d09eUL, 0x1acfe827UL, 0xa2738f42UL,
 120.180 +    0xb0c620acUL, 0x087a47c9UL, 0xa032af3eUL, 0x188ec85bUL, 0x0a3b67b5UL,
 120.181 +    0xb28700d0UL, 0x2f503869UL, 0x97ec5f0cUL, 0x8559f0e2UL, 0x3de59787UL,
 120.182 +    0x658687d1UL, 0xdd3ae0b4UL, 0xcf8f4f5aUL, 0x7733283fUL, 0xeae41086UL,
 120.183 +    0x525877e3UL, 0x40edd80dUL, 0xf851bf68UL, 0xf02bf8a1UL, 0x48979fc4UL,
 120.184 +    0x5a22302aUL, 0xe29e574fUL, 0x7f496ff6UL, 0xc7f50893UL, 0xd540a77dUL,
 120.185 +    0x6dfcc018UL, 0x359fd04eUL, 0x8d23b72bUL, 0x9f9618c5UL, 0x272a7fa0UL,
 120.186 +    0xbafd4719UL, 0x0241207cUL, 0x10f48f92UL, 0xa848e8f7UL, 0x9b14583dUL,
 120.187 +    0x23a83f58UL, 0x311d90b6UL, 0x89a1f7d3UL, 0x1476cf6aUL, 0xaccaa80fUL,
 120.188 +    0xbe7f07e1UL, 0x06c36084UL, 0x5ea070d2UL, 0xe61c17b7UL, 0xf4a9b859UL,
 120.189 +    0x4c15df3cUL, 0xd1c2e785UL, 0x697e80e0UL, 0x7bcb2f0eUL, 0xc377486bUL,
 120.190 +    0xcb0d0fa2UL, 0x73b168c7UL, 0x6104c729UL, 0xd9b8a04cUL, 0x446f98f5UL,
 120.191 +    0xfcd3ff90UL, 0xee66507eUL, 0x56da371bUL, 0x0eb9274dUL, 0xb6054028UL,
 120.192 +    0xa4b0efc6UL, 0x1c0c88a3UL, 0x81dbb01aUL, 0x3967d77fUL, 0x2bd27891UL,
 120.193 +    0x936e1ff4UL, 0x3b26f703UL, 0x839a9066UL, 0x912f3f88UL, 0x299358edUL,
 120.194 +    0xb4446054UL, 0x0cf80731UL, 0x1e4da8dfUL, 0xa6f1cfbaUL, 0xfe92dfecUL,
 120.195 +    0x462eb889UL, 0x549b1767UL, 0xec277002UL, 0x71f048bbUL, 0xc94c2fdeUL,
 120.196 +    0xdbf98030UL, 0x6345e755UL, 0x6b3fa09cUL, 0xd383c7f9UL, 0xc1366817UL,
 120.197 +    0x798a0f72UL, 0xe45d37cbUL, 0x5ce150aeUL, 0x4e54ff40UL, 0xf6e89825UL,
 120.198 +    0xae8b8873UL, 0x1637ef16UL, 0x048240f8UL, 0xbc3e279dUL, 0x21e91f24UL,
 120.199 +    0x99557841UL, 0x8be0d7afUL, 0x335cb0caUL, 0xed59b63bUL, 0x55e5d15eUL,
 120.200 +    0x47507eb0UL, 0xffec19d5UL, 0x623b216cUL, 0xda874609UL, 0xc832e9e7UL,
 120.201 +    0x708e8e82UL, 0x28ed9ed4UL, 0x9051f9b1UL, 0x82e4565fUL, 0x3a58313aUL,
 120.202 +    0xa78f0983UL, 0x1f336ee6UL, 0x0d86c108UL, 0xb53aa66dUL, 0xbd40e1a4UL,
 120.203 +    0x05fc86c1UL, 0x1749292fUL, 0xaff54e4aUL, 0x322276f3UL, 0x8a9e1196UL,
 120.204 +    0x982bbe78UL, 0x2097d91dUL, 0x78f4c94bUL, 0xc048ae2eUL, 0xd2fd01c0UL,
 120.205 +    0x6a4166a5UL, 0xf7965e1cUL, 0x4f2a3979UL, 0x5d9f9697UL, 0xe523f1f2UL,
 120.206 +    0x4d6b1905UL, 0xf5d77e60UL, 0xe762d18eUL, 0x5fdeb6ebUL, 0xc2098e52UL,
 120.207 +    0x7ab5e937UL, 0x680046d9UL, 0xd0bc21bcUL, 0x88df31eaUL, 0x3063568fUL,
 120.208 +    0x22d6f961UL, 0x9a6a9e04UL, 0x07bda6bdUL, 0xbf01c1d8UL, 0xadb46e36UL,
 120.209 +    0x15080953UL, 0x1d724e9aUL, 0xa5ce29ffUL, 0xb77b8611UL, 0x0fc7e174UL,
 120.210 +    0x9210d9cdUL, 0x2aacbea8UL, 0x38191146UL, 0x80a57623UL, 0xd8c66675UL,
 120.211 +    0x607a0110UL, 0x72cfaefeUL, 0xca73c99bUL, 0x57a4f122UL, 0xef189647UL,
 120.212 +    0xfdad39a9UL, 0x45115eccUL, 0x764dee06UL, 0xcef18963UL, 0xdc44268dUL,
 120.213 +    0x64f841e8UL, 0xf92f7951UL, 0x41931e34UL, 0x5326b1daUL, 0xeb9ad6bfUL,
 120.214 +    0xb3f9c6e9UL, 0x0b45a18cUL, 0x19f00e62UL, 0xa14c6907UL, 0x3c9b51beUL,
 120.215 +    0x842736dbUL, 0x96929935UL, 0x2e2efe50UL, 0x2654b999UL, 0x9ee8defcUL,
 120.216 +    0x8c5d7112UL, 0x34e11677UL, 0xa9362eceUL, 0x118a49abUL, 0x033fe645UL,
 120.217 +    0xbb838120UL, 0xe3e09176UL, 0x5b5cf613UL, 0x49e959fdUL, 0xf1553e98UL,
 120.218 +    0x6c820621UL, 0xd43e6144UL, 0xc68bceaaUL, 0x7e37a9cfUL, 0xd67f4138UL,
 120.219 +    0x6ec3265dUL, 0x7c7689b3UL, 0xc4caeed6UL, 0x591dd66fUL, 0xe1a1b10aUL,
 120.220 +    0xf3141ee4UL, 0x4ba87981UL, 0x13cb69d7UL, 0xab770eb2UL, 0xb9c2a15cUL,
 120.221 +    0x017ec639UL, 0x9ca9fe80UL, 0x241599e5UL, 0x36a0360bUL, 0x8e1c516eUL,
 120.222 +    0x866616a7UL, 0x3eda71c2UL, 0x2c6fde2cUL, 0x94d3b949UL, 0x090481f0UL,
 120.223 +    0xb1b8e695UL, 0xa30d497bUL, 0x1bb12e1eUL, 0x43d23e48UL, 0xfb6e592dUL,
 120.224 +    0xe9dbf6c3UL, 0x516791a6UL, 0xccb0a91fUL, 0x740cce7aUL, 0x66b96194UL,
 120.225 +    0xde0506f1UL
 120.226 +  },
 120.227 +  {
 120.228 +    0x00000000UL, 0x96300777UL, 0x2c610eeeUL, 0xba510999UL, 0x19c46d07UL,
 120.229 +    0x8ff46a70UL, 0x35a563e9UL, 0xa395649eUL, 0x3288db0eUL, 0xa4b8dc79UL,
 120.230 +    0x1ee9d5e0UL, 0x88d9d297UL, 0x2b4cb609UL, 0xbd7cb17eUL, 0x072db8e7UL,
 120.231 +    0x911dbf90UL, 0x6410b71dUL, 0xf220b06aUL, 0x4871b9f3UL, 0xde41be84UL,
 120.232 +    0x7dd4da1aUL, 0xebe4dd6dUL, 0x51b5d4f4UL, 0xc785d383UL, 0x56986c13UL,
 120.233 +    0xc0a86b64UL, 0x7af962fdUL, 0xecc9658aUL, 0x4f5c0114UL, 0xd96c0663UL,
 120.234 +    0x633d0ffaUL, 0xf50d088dUL, 0xc8206e3bUL, 0x5e10694cUL, 0xe44160d5UL,
 120.235 +    0x727167a2UL, 0xd1e4033cUL, 0x47d4044bUL, 0xfd850dd2UL, 0x6bb50aa5UL,
 120.236 +    0xfaa8b535UL, 0x6c98b242UL, 0xd6c9bbdbUL, 0x40f9bcacUL, 0xe36cd832UL,
 120.237 +    0x755cdf45UL, 0xcf0dd6dcUL, 0x593dd1abUL, 0xac30d926UL, 0x3a00de51UL,
 120.238 +    0x8051d7c8UL, 0x1661d0bfUL, 0xb5f4b421UL, 0x23c4b356UL, 0x9995bacfUL,
 120.239 +    0x0fa5bdb8UL, 0x9eb80228UL, 0x0888055fUL, 0xb2d90cc6UL, 0x24e90bb1UL,
 120.240 +    0x877c6f2fUL, 0x114c6858UL, 0xab1d61c1UL, 0x3d2d66b6UL, 0x9041dc76UL,
 120.241 +    0x0671db01UL, 0xbc20d298UL, 0x2a10d5efUL, 0x8985b171UL, 0x1fb5b606UL,
 120.242 +    0xa5e4bf9fUL, 0x33d4b8e8UL, 0xa2c90778UL, 0x34f9000fUL, 0x8ea80996UL,
 120.243 +    0x18980ee1UL, 0xbb0d6a7fUL, 0x2d3d6d08UL, 0x976c6491UL, 0x015c63e6UL,
 120.244 +    0xf4516b6bUL, 0x62616c1cUL, 0xd8306585UL, 0x4e0062f2UL, 0xed95066cUL,
 120.245 +    0x7ba5011bUL, 0xc1f40882UL, 0x57c40ff5UL, 0xc6d9b065UL, 0x50e9b712UL,
 120.246 +    0xeab8be8bUL, 0x7c88b9fcUL, 0xdf1ddd62UL, 0x492dda15UL, 0xf37cd38cUL,
 120.247 +    0x654cd4fbUL, 0x5861b24dUL, 0xce51b53aUL, 0x7400bca3UL, 0xe230bbd4UL,
 120.248 +    0x41a5df4aUL, 0xd795d83dUL, 0x6dc4d1a4UL, 0xfbf4d6d3UL, 0x6ae96943UL,
 120.249 +    0xfcd96e34UL, 0x468867adUL, 0xd0b860daUL, 0x732d0444UL, 0xe51d0333UL,
 120.250 +    0x5f4c0aaaUL, 0xc97c0dddUL, 0x3c710550UL, 0xaa410227UL, 0x10100bbeUL,
 120.251 +    0x86200cc9UL, 0x25b56857UL, 0xb3856f20UL, 0x09d466b9UL, 0x9fe461ceUL,
 120.252 +    0x0ef9de5eUL, 0x98c9d929UL, 0x2298d0b0UL, 0xb4a8d7c7UL, 0x173db359UL,
 120.253 +    0x810db42eUL, 0x3b5cbdb7UL, 0xad6cbac0UL, 0x2083b8edUL, 0xb6b3bf9aUL,
 120.254 +    0x0ce2b603UL, 0x9ad2b174UL, 0x3947d5eaUL, 0xaf77d29dUL, 0x1526db04UL,
 120.255 +    0x8316dc73UL, 0x120b63e3UL, 0x843b6494UL, 0x3e6a6d0dUL, 0xa85a6a7aUL,
 120.256 +    0x0bcf0ee4UL, 0x9dff0993UL, 0x27ae000aUL, 0xb19e077dUL, 0x44930ff0UL,
 120.257 +    0xd2a30887UL, 0x68f2011eUL, 0xfec20669UL, 0x5d5762f7UL, 0xcb676580UL,
 120.258 +    0x71366c19UL, 0xe7066b6eUL, 0x761bd4feUL, 0xe02bd389UL, 0x5a7ada10UL,
 120.259 +    0xcc4add67UL, 0x6fdfb9f9UL, 0xf9efbe8eUL, 0x43beb717UL, 0xd58eb060UL,
 120.260 +    0xe8a3d6d6UL, 0x7e93d1a1UL, 0xc4c2d838UL, 0x52f2df4fUL, 0xf167bbd1UL,
 120.261 +    0x6757bca6UL, 0xdd06b53fUL, 0x4b36b248UL, 0xda2b0dd8UL, 0x4c1b0aafUL,
 120.262 +    0xf64a0336UL, 0x607a0441UL, 0xc3ef60dfUL, 0x55df67a8UL, 0xef8e6e31UL,
 120.263 +    0x79be6946UL, 0x8cb361cbUL, 0x1a8366bcUL, 0xa0d26f25UL, 0x36e26852UL,
 120.264 +    0x95770cccUL, 0x03470bbbUL, 0xb9160222UL, 0x2f260555UL, 0xbe3bbac5UL,
 120.265 +    0x280bbdb2UL, 0x925ab42bUL, 0x046ab35cUL, 0xa7ffd7c2UL, 0x31cfd0b5UL,
 120.266 +    0x8b9ed92cUL, 0x1daede5bUL, 0xb0c2649bUL, 0x26f263ecUL, 0x9ca36a75UL,
 120.267 +    0x0a936d02UL, 0xa906099cUL, 0x3f360eebUL, 0x85670772UL, 0x13570005UL,
 120.268 +    0x824abf95UL, 0x147ab8e2UL, 0xae2bb17bUL, 0x381bb60cUL, 0x9b8ed292UL,
 120.269 +    0x0dbed5e5UL, 0xb7efdc7cUL, 0x21dfdb0bUL, 0xd4d2d386UL, 0x42e2d4f1UL,
 120.270 +    0xf8b3dd68UL, 0x6e83da1fUL, 0xcd16be81UL, 0x5b26b9f6UL, 0xe177b06fUL,
 120.271 +    0x7747b718UL, 0xe65a0888UL, 0x706a0fffUL, 0xca3b0666UL, 0x5c0b0111UL,
 120.272 +    0xff9e658fUL, 0x69ae62f8UL, 0xd3ff6b61UL, 0x45cf6c16UL, 0x78e20aa0UL,
 120.273 +    0xeed20dd7UL, 0x5483044eUL, 0xc2b30339UL, 0x612667a7UL, 0xf71660d0UL,
 120.274 +    0x4d476949UL, 0xdb776e3eUL, 0x4a6ad1aeUL, 0xdc5ad6d9UL, 0x660bdf40UL,
 120.275 +    0xf03bd837UL, 0x53aebca9UL, 0xc59ebbdeUL, 0x7fcfb247UL, 0xe9ffb530UL,
 120.276 +    0x1cf2bdbdUL, 0x8ac2bacaUL, 0x3093b353UL, 0xa6a3b424UL, 0x0536d0baUL,
 120.277 +    0x9306d7cdUL, 0x2957de54UL, 0xbf67d923UL, 0x2e7a66b3UL, 0xb84a61c4UL,
 120.278 +    0x021b685dUL, 0x942b6f2aUL, 0x37be0bb4UL, 0xa18e0cc3UL, 0x1bdf055aUL,
 120.279 +    0x8def022dUL
 120.280 +  },
 120.281 +  {
 120.282 +    0x00000000UL, 0x41311b19UL, 0x82623632UL, 0xc3532d2bUL, 0x04c56c64UL,
 120.283 +    0x45f4777dUL, 0x86a75a56UL, 0xc796414fUL, 0x088ad9c8UL, 0x49bbc2d1UL,
 120.284 +    0x8ae8effaUL, 0xcbd9f4e3UL, 0x0c4fb5acUL, 0x4d7eaeb5UL, 0x8e2d839eUL,
 120.285 +    0xcf1c9887UL, 0x5112c24aUL, 0x1023d953UL, 0xd370f478UL, 0x9241ef61UL,
 120.286 +    0x55d7ae2eUL, 0x14e6b537UL, 0xd7b5981cUL, 0x96848305UL, 0x59981b82UL,
 120.287 +    0x18a9009bUL, 0xdbfa2db0UL, 0x9acb36a9UL, 0x5d5d77e6UL, 0x1c6c6cffUL,
 120.288 +    0xdf3f41d4UL, 0x9e0e5acdUL, 0xa2248495UL, 0xe3159f8cUL, 0x2046b2a7UL,
 120.289 +    0x6177a9beUL, 0xa6e1e8f1UL, 0xe7d0f3e8UL, 0x2483dec3UL, 0x65b2c5daUL,
 120.290 +    0xaaae5d5dUL, 0xeb9f4644UL, 0x28cc6b6fUL, 0x69fd7076UL, 0xae6b3139UL,
 120.291 +    0xef5a2a20UL, 0x2c09070bUL, 0x6d381c12UL, 0xf33646dfUL, 0xb2075dc6UL,
 120.292 +    0x715470edUL, 0x30656bf4UL, 0xf7f32abbUL, 0xb6c231a2UL, 0x75911c89UL,
 120.293 +    0x34a00790UL, 0xfbbc9f17UL, 0xba8d840eUL, 0x79dea925UL, 0x38efb23cUL,
 120.294 +    0xff79f373UL, 0xbe48e86aUL, 0x7d1bc541UL, 0x3c2ade58UL, 0x054f79f0UL,
 120.295 +    0x447e62e9UL, 0x872d4fc2UL, 0xc61c54dbUL, 0x018a1594UL, 0x40bb0e8dUL,
 120.296 +    0x83e823a6UL, 0xc2d938bfUL, 0x0dc5a038UL, 0x4cf4bb21UL, 0x8fa7960aUL,
 120.297 +    0xce968d13UL, 0x0900cc5cUL, 0x4831d745UL, 0x8b62fa6eUL, 0xca53e177UL,
 120.298 +    0x545dbbbaUL, 0x156ca0a3UL, 0xd63f8d88UL, 0x970e9691UL, 0x5098d7deUL,
 120.299 +    0x11a9ccc7UL, 0xd2fae1ecUL, 0x93cbfaf5UL, 0x5cd76272UL, 0x1de6796bUL,
 120.300 +    0xdeb55440UL, 0x9f844f59UL, 0x58120e16UL, 0x1923150fUL, 0xda703824UL,
 120.301 +    0x9b41233dUL, 0xa76bfd65UL, 0xe65ae67cUL, 0x2509cb57UL, 0x6438d04eUL,
 120.302 +    0xa3ae9101UL, 0xe29f8a18UL, 0x21cca733UL, 0x60fdbc2aUL, 0xafe124adUL,
 120.303 +    0xeed03fb4UL, 0x2d83129fUL, 0x6cb20986UL, 0xab2448c9UL, 0xea1553d0UL,
 120.304 +    0x29467efbUL, 0x687765e2UL, 0xf6793f2fUL, 0xb7482436UL, 0x741b091dUL,
 120.305 +    0x352a1204UL, 0xf2bc534bUL, 0xb38d4852UL, 0x70de6579UL, 0x31ef7e60UL,
 120.306 +    0xfef3e6e7UL, 0xbfc2fdfeUL, 0x7c91d0d5UL, 0x3da0cbccUL, 0xfa368a83UL,
 120.307 +    0xbb07919aUL, 0x7854bcb1UL, 0x3965a7a8UL, 0x4b98833bUL, 0x0aa99822UL,
 120.308 +    0xc9fab509UL, 0x88cbae10UL, 0x4f5def5fUL, 0x0e6cf446UL, 0xcd3fd96dUL,
 120.309 +    0x8c0ec274UL, 0x43125af3UL, 0x022341eaUL, 0xc1706cc1UL, 0x804177d8UL,
 120.310 +    0x47d73697UL, 0x06e62d8eUL, 0xc5b500a5UL, 0x84841bbcUL, 0x1a8a4171UL,
 120.311 +    0x5bbb5a68UL, 0x98e87743UL, 0xd9d96c5aUL, 0x1e4f2d15UL, 0x5f7e360cUL,
 120.312 +    0x9c2d1b27UL, 0xdd1c003eUL, 0x120098b9UL, 0x533183a0UL, 0x9062ae8bUL,
 120.313 +    0xd153b592UL, 0x16c5f4ddUL, 0x57f4efc4UL, 0x94a7c2efUL, 0xd596d9f6UL,
 120.314 +    0xe9bc07aeUL, 0xa88d1cb7UL, 0x6bde319cUL, 0x2aef2a85UL, 0xed796bcaUL,
 120.315 +    0xac4870d3UL, 0x6f1b5df8UL, 0x2e2a46e1UL, 0xe136de66UL, 0xa007c57fUL,
 120.316 +    0x6354e854UL, 0x2265f34dUL, 0xe5f3b202UL, 0xa4c2a91bUL, 0x67918430UL,
 120.317 +    0x26a09f29UL, 0xb8aec5e4UL, 0xf99fdefdUL, 0x3accf3d6UL, 0x7bfde8cfUL,
 120.318 +    0xbc6ba980UL, 0xfd5ab299UL, 0x3e099fb2UL, 0x7f3884abUL, 0xb0241c2cUL,
 120.319 +    0xf1150735UL, 0x32462a1eUL, 0x73773107UL, 0xb4e17048UL, 0xf5d06b51UL,
 120.320 +    0x3683467aUL, 0x77b25d63UL, 0x4ed7facbUL, 0x0fe6e1d2UL, 0xccb5ccf9UL,
 120.321 +    0x8d84d7e0UL, 0x4a1296afUL, 0x0b238db6UL, 0xc870a09dUL, 0x8941bb84UL,
 120.322 +    0x465d2303UL, 0x076c381aUL, 0xc43f1531UL, 0x850e0e28UL, 0x42984f67UL,
 120.323 +    0x03a9547eUL, 0xc0fa7955UL, 0x81cb624cUL, 0x1fc53881UL, 0x5ef42398UL,
 120.324 +    0x9da70eb3UL, 0xdc9615aaUL, 0x1b0054e5UL, 0x5a314ffcUL, 0x996262d7UL,
 120.325 +    0xd85379ceUL, 0x174fe149UL, 0x567efa50UL, 0x952dd77bUL, 0xd41ccc62UL,
 120.326 +    0x138a8d2dUL, 0x52bb9634UL, 0x91e8bb1fUL, 0xd0d9a006UL, 0xecf37e5eUL,
 120.327 +    0xadc26547UL, 0x6e91486cUL, 0x2fa05375UL, 0xe836123aUL, 0xa9070923UL,
 120.328 +    0x6a542408UL, 0x2b653f11UL, 0xe479a796UL, 0xa548bc8fUL, 0x661b91a4UL,
 120.329 +    0x272a8abdUL, 0xe0bccbf2UL, 0xa18dd0ebUL, 0x62defdc0UL, 0x23efe6d9UL,
 120.330 +    0xbde1bc14UL, 0xfcd0a70dUL, 0x3f838a26UL, 0x7eb2913fUL, 0xb924d070UL,
 120.331 +    0xf815cb69UL, 0x3b46e642UL, 0x7a77fd5bUL, 0xb56b65dcUL, 0xf45a7ec5UL,
 120.332 +    0x370953eeUL, 0x763848f7UL, 0xb1ae09b8UL, 0xf09f12a1UL, 0x33cc3f8aUL,
 120.333 +    0x72fd2493UL
 120.334 +  },
 120.335 +  {
 120.336 +    0x00000000UL, 0x376ac201UL, 0x6ed48403UL, 0x59be4602UL, 0xdca80907UL,
 120.337 +    0xebc2cb06UL, 0xb27c8d04UL, 0x85164f05UL, 0xb851130eUL, 0x8f3bd10fUL,
 120.338 +    0xd685970dUL, 0xe1ef550cUL, 0x64f91a09UL, 0x5393d808UL, 0x0a2d9e0aUL,
 120.339 +    0x3d475c0bUL, 0x70a3261cUL, 0x47c9e41dUL, 0x1e77a21fUL, 0x291d601eUL,
 120.340 +    0xac0b2f1bUL, 0x9b61ed1aUL, 0xc2dfab18UL, 0xf5b56919UL, 0xc8f23512UL,
 120.341 +    0xff98f713UL, 0xa626b111UL, 0x914c7310UL, 0x145a3c15UL, 0x2330fe14UL,
 120.342 +    0x7a8eb816UL, 0x4de47a17UL, 0xe0464d38UL, 0xd72c8f39UL, 0x8e92c93bUL,
 120.343 +    0xb9f80b3aUL, 0x3cee443fUL, 0x0b84863eUL, 0x523ac03cUL, 0x6550023dUL,
 120.344 +    0x58175e36UL, 0x6f7d9c37UL, 0x36c3da35UL, 0x01a91834UL, 0x84bf5731UL,
 120.345 +    0xb3d59530UL, 0xea6bd332UL, 0xdd011133UL, 0x90e56b24UL, 0xa78fa925UL,
 120.346 +    0xfe31ef27UL, 0xc95b2d26UL, 0x4c4d6223UL, 0x7b27a022UL, 0x2299e620UL,
 120.347 +    0x15f32421UL, 0x28b4782aUL, 0x1fdeba2bUL, 0x4660fc29UL, 0x710a3e28UL,
 120.348 +    0xf41c712dUL, 0xc376b32cUL, 0x9ac8f52eUL, 0xada2372fUL, 0xc08d9a70UL,
 120.349 +    0xf7e75871UL, 0xae591e73UL, 0x9933dc72UL, 0x1c259377UL, 0x2b4f5176UL,
 120.350 +    0x72f11774UL, 0x459bd575UL, 0x78dc897eUL, 0x4fb64b7fUL, 0x16080d7dUL,
 120.351 +    0x2162cf7cUL, 0xa4748079UL, 0x931e4278UL, 0xcaa0047aUL, 0xfdcac67bUL,
 120.352 +    0xb02ebc6cUL, 0x87447e6dUL, 0xdefa386fUL, 0xe990fa6eUL, 0x6c86b56bUL,
 120.353 +    0x5bec776aUL, 0x02523168UL, 0x3538f369UL, 0x087faf62UL, 0x3f156d63UL,
 120.354 +    0x66ab2b61UL, 0x51c1e960UL, 0xd4d7a665UL, 0xe3bd6464UL, 0xba032266UL,
 120.355 +    0x8d69e067UL, 0x20cbd748UL, 0x17a11549UL, 0x4e1f534bUL, 0x7975914aUL,
 120.356 +    0xfc63de4fUL, 0xcb091c4eUL, 0x92b75a4cUL, 0xa5dd984dUL, 0x989ac446UL,
 120.357 +    0xaff00647UL, 0xf64e4045UL, 0xc1248244UL, 0x4432cd41UL, 0x73580f40UL,
 120.358 +    0x2ae64942UL, 0x1d8c8b43UL, 0x5068f154UL, 0x67023355UL, 0x3ebc7557UL,
 120.359 +    0x09d6b756UL, 0x8cc0f853UL, 0xbbaa3a52UL, 0xe2147c50UL, 0xd57ebe51UL,
 120.360 +    0xe839e25aUL, 0xdf53205bUL, 0x86ed6659UL, 0xb187a458UL, 0x3491eb5dUL,
 120.361 +    0x03fb295cUL, 0x5a456f5eUL, 0x6d2fad5fUL, 0x801b35e1UL, 0xb771f7e0UL,
 120.362 +    0xeecfb1e2UL, 0xd9a573e3UL, 0x5cb33ce6UL, 0x6bd9fee7UL, 0x3267b8e5UL,
 120.363 +    0x050d7ae4UL, 0x384a26efUL, 0x0f20e4eeUL, 0x569ea2ecUL, 0x61f460edUL,
 120.364 +    0xe4e22fe8UL, 0xd388ede9UL, 0x8a36abebUL, 0xbd5c69eaUL, 0xf0b813fdUL,
 120.365 +    0xc7d2d1fcUL, 0x9e6c97feUL, 0xa90655ffUL, 0x2c101afaUL, 0x1b7ad8fbUL,
 120.366 +    0x42c49ef9UL, 0x75ae5cf8UL, 0x48e900f3UL, 0x7f83c2f2UL, 0x263d84f0UL,
 120.367 +    0x115746f1UL, 0x944109f4UL, 0xa32bcbf5UL, 0xfa958df7UL, 0xcdff4ff6UL,
 120.368 +    0x605d78d9UL, 0x5737bad8UL, 0x0e89fcdaUL, 0x39e33edbUL, 0xbcf571deUL,
 120.369 +    0x8b9fb3dfUL, 0xd221f5ddUL, 0xe54b37dcUL, 0xd80c6bd7UL, 0xef66a9d6UL,
 120.370 +    0xb6d8efd4UL, 0x81b22dd5UL, 0x04a462d0UL, 0x33cea0d1UL, 0x6a70e6d3UL,
 120.371 +    0x5d1a24d2UL, 0x10fe5ec5UL, 0x27949cc4UL, 0x7e2adac6UL, 0x494018c7UL,
 120.372 +    0xcc5657c2UL, 0xfb3c95c3UL, 0xa282d3c1UL, 0x95e811c0UL, 0xa8af4dcbUL,
 120.373 +    0x9fc58fcaUL, 0xc67bc9c8UL, 0xf1110bc9UL, 0x740744ccUL, 0x436d86cdUL,
 120.374 +    0x1ad3c0cfUL, 0x2db902ceUL, 0x4096af91UL, 0x77fc6d90UL, 0x2e422b92UL,
 120.375 +    0x1928e993UL, 0x9c3ea696UL, 0xab546497UL, 0xf2ea2295UL, 0xc580e094UL,
 120.376 +    0xf8c7bc9fUL, 0xcfad7e9eUL, 0x9613389cUL, 0xa179fa9dUL, 0x246fb598UL,
 120.377 +    0x13057799UL, 0x4abb319bUL, 0x7dd1f39aUL, 0x3035898dUL, 0x075f4b8cUL,
 120.378 +    0x5ee10d8eUL, 0x698bcf8fUL, 0xec9d808aUL, 0xdbf7428bUL, 0x82490489UL,
 120.379 +    0xb523c688UL, 0x88649a83UL, 0xbf0e5882UL, 0xe6b01e80UL, 0xd1dadc81UL,
 120.380 +    0x54cc9384UL, 0x63a65185UL, 0x3a181787UL, 0x0d72d586UL, 0xa0d0e2a9UL,
 120.381 +    0x97ba20a8UL, 0xce0466aaUL, 0xf96ea4abUL, 0x7c78ebaeUL, 0x4b1229afUL,
 120.382 +    0x12ac6fadUL, 0x25c6adacUL, 0x1881f1a7UL, 0x2feb33a6UL, 0x765575a4UL,
 120.383 +    0x413fb7a5UL, 0xc429f8a0UL, 0xf3433aa1UL, 0xaafd7ca3UL, 0x9d97bea2UL,
 120.384 +    0xd073c4b5UL, 0xe71906b4UL, 0xbea740b6UL, 0x89cd82b7UL, 0x0cdbcdb2UL,
 120.385 +    0x3bb10fb3UL, 0x620f49b1UL, 0x55658bb0UL, 0x6822d7bbUL, 0x5f4815baUL,
 120.386 +    0x06f653b8UL, 0x319c91b9UL, 0xb48adebcUL, 0x83e01cbdUL, 0xda5e5abfUL,
 120.387 +    0xed3498beUL
 120.388 +  },
 120.389 +  {
 120.390 +    0x00000000UL, 0x6567bcb8UL, 0x8bc809aaUL, 0xeeafb512UL, 0x5797628fUL,
 120.391 +    0x32f0de37UL, 0xdc5f6b25UL, 0xb938d79dUL, 0xef28b4c5UL, 0x8a4f087dUL,
 120.392 +    0x64e0bd6fUL, 0x018701d7UL, 0xb8bfd64aUL, 0xddd86af2UL, 0x3377dfe0UL,
 120.393 +    0x56106358UL, 0x9f571950UL, 0xfa30a5e8UL, 0x149f10faUL, 0x71f8ac42UL,
 120.394 +    0xc8c07bdfUL, 0xada7c767UL, 0x43087275UL, 0x266fcecdUL, 0x707fad95UL,
 120.395 +    0x1518112dUL, 0xfbb7a43fUL, 0x9ed01887UL, 0x27e8cf1aUL, 0x428f73a2UL,
 120.396 +    0xac20c6b0UL, 0xc9477a08UL, 0x3eaf32a0UL, 0x5bc88e18UL, 0xb5673b0aUL,
 120.397 +    0xd00087b2UL, 0x6938502fUL, 0x0c5fec97UL, 0xe2f05985UL, 0x8797e53dUL,
 120.398 +    0xd1878665UL, 0xb4e03addUL, 0x5a4f8fcfUL, 0x3f283377UL, 0x8610e4eaUL,
 120.399 +    0xe3775852UL, 0x0dd8ed40UL, 0x68bf51f8UL, 0xa1f82bf0UL, 0xc49f9748UL,
 120.400 +    0x2a30225aUL, 0x4f579ee2UL, 0xf66f497fUL, 0x9308f5c7UL, 0x7da740d5UL,
 120.401 +    0x18c0fc6dUL, 0x4ed09f35UL, 0x2bb7238dUL, 0xc518969fUL, 0xa07f2a27UL,
 120.402 +    0x1947fdbaUL, 0x7c204102UL, 0x928ff410UL, 0xf7e848a8UL, 0x3d58149bUL,
 120.403 +    0x583fa823UL, 0xb6901d31UL, 0xd3f7a189UL, 0x6acf7614UL, 0x0fa8caacUL,
 120.404 +    0xe1077fbeUL, 0x8460c306UL, 0xd270a05eUL, 0xb7171ce6UL, 0x59b8a9f4UL,
 120.405 +    0x3cdf154cUL, 0x85e7c2d1UL, 0xe0807e69UL, 0x0e2fcb7bUL, 0x6b4877c3UL,
 120.406 +    0xa20f0dcbUL, 0xc768b173UL, 0x29c70461UL, 0x4ca0b8d9UL, 0xf5986f44UL,
 120.407 +    0x90ffd3fcUL, 0x7e5066eeUL, 0x1b37da56UL, 0x4d27b90eUL, 0x284005b6UL,
 120.408 +    0xc6efb0a4UL, 0xa3880c1cUL, 0x1ab0db81UL, 0x7fd76739UL, 0x9178d22bUL,
 120.409 +    0xf41f6e93UL, 0x03f7263bUL, 0x66909a83UL, 0x883f2f91UL, 0xed589329UL,
 120.410 +    0x546044b4UL, 0x3107f80cUL, 0xdfa84d1eUL, 0xbacff1a6UL, 0xecdf92feUL,
 120.411 +    0x89b82e46UL, 0x67179b54UL, 0x027027ecUL, 0xbb48f071UL, 0xde2f4cc9UL,
 120.412 +    0x3080f9dbUL, 0x55e74563UL, 0x9ca03f6bUL, 0xf9c783d3UL, 0x176836c1UL,
 120.413 +    0x720f8a79UL, 0xcb375de4UL, 0xae50e15cUL, 0x40ff544eUL, 0x2598e8f6UL,
 120.414 +    0x73888baeUL, 0x16ef3716UL, 0xf8408204UL, 0x9d273ebcUL, 0x241fe921UL,
 120.415 +    0x41785599UL, 0xafd7e08bUL, 0xcab05c33UL, 0x3bb659edUL, 0x5ed1e555UL,
 120.416 +    0xb07e5047UL, 0xd519ecffUL, 0x6c213b62UL, 0x094687daUL, 0xe7e932c8UL,
 120.417 +    0x828e8e70UL, 0xd49eed28UL, 0xb1f95190UL, 0x5f56e482UL, 0x3a31583aUL,
 120.418 +    0x83098fa7UL, 0xe66e331fUL, 0x08c1860dUL, 0x6da63ab5UL, 0xa4e140bdUL,
 120.419 +    0xc186fc05UL, 0x2f294917UL, 0x4a4ef5afUL, 0xf3762232UL, 0x96119e8aUL,
 120.420 +    0x78be2b98UL, 0x1dd99720UL, 0x4bc9f478UL, 0x2eae48c0UL, 0xc001fdd2UL,
 120.421 +    0xa566416aUL, 0x1c5e96f7UL, 0x79392a4fUL, 0x97969f5dUL, 0xf2f123e5UL,
 120.422 +    0x05196b4dUL, 0x607ed7f5UL, 0x8ed162e7UL, 0xebb6de5fUL, 0x528e09c2UL,
 120.423 +    0x37e9b57aUL, 0xd9460068UL, 0xbc21bcd0UL, 0xea31df88UL, 0x8f566330UL,
 120.424 +    0x61f9d622UL, 0x049e6a9aUL, 0xbda6bd07UL, 0xd8c101bfUL, 0x366eb4adUL,
 120.425 +    0x53090815UL, 0x9a4e721dUL, 0xff29cea5UL, 0x11867bb7UL, 0x74e1c70fUL,
 120.426 +    0xcdd91092UL, 0xa8beac2aUL, 0x46111938UL, 0x2376a580UL, 0x7566c6d8UL,
 120.427 +    0x10017a60UL, 0xfeaecf72UL, 0x9bc973caUL, 0x22f1a457UL, 0x479618efUL,
 120.428 +    0xa939adfdUL, 0xcc5e1145UL, 0x06ee4d76UL, 0x6389f1ceUL, 0x8d2644dcUL,
 120.429 +    0xe841f864UL, 0x51792ff9UL, 0x341e9341UL, 0xdab12653UL, 0xbfd69aebUL,
 120.430 +    0xe9c6f9b3UL, 0x8ca1450bUL, 0x620ef019UL, 0x07694ca1UL, 0xbe519b3cUL,
 120.431 +    0xdb362784UL, 0x35999296UL, 0x50fe2e2eUL, 0x99b95426UL, 0xfcdee89eUL,
 120.432 +    0x12715d8cUL, 0x7716e134UL, 0xce2e36a9UL, 0xab498a11UL, 0x45e63f03UL,
 120.433 +    0x208183bbUL, 0x7691e0e3UL, 0x13f65c5bUL, 0xfd59e949UL, 0x983e55f1UL,
 120.434 +    0x2106826cUL, 0x44613ed4UL, 0xaace8bc6UL, 0xcfa9377eUL, 0x38417fd6UL,
 120.435 +    0x5d26c36eUL, 0xb389767cUL, 0xd6eecac4UL, 0x6fd61d59UL, 0x0ab1a1e1UL,
 120.436 +    0xe41e14f3UL, 0x8179a84bUL, 0xd769cb13UL, 0xb20e77abUL, 0x5ca1c2b9UL,
 120.437 +    0x39c67e01UL, 0x80fea99cUL, 0xe5991524UL, 0x0b36a036UL, 0x6e511c8eUL,
 120.438 +    0xa7166686UL, 0xc271da3eUL, 0x2cde6f2cUL, 0x49b9d394UL, 0xf0810409UL,
 120.439 +    0x95e6b8b1UL, 0x7b490da3UL, 0x1e2eb11bUL, 0x483ed243UL, 0x2d596efbUL,
 120.440 +    0xc3f6dbe9UL, 0xa6916751UL, 0x1fa9b0ccUL, 0x7ace0c74UL, 0x9461b966UL,
 120.441 +    0xf10605deUL
 120.442 +#endif
 120.443 +  }
 120.444 +};
   121.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   121.2 +++ b/libs/zlib/deflate.c	Sat Sep 19 05:51:51 2015 +0300
   121.3 @@ -0,0 +1,1736 @@
   121.4 +/* deflate.c -- compress data using the deflation algorithm
   121.5 + * Copyright (C) 1995-2005 Jean-loup Gailly.
   121.6 + * For conditions of distribution and use, see copyright notice in zlib.h
   121.7 + */
   121.8 +
   121.9 +/*
  121.10 + *  ALGORITHM
  121.11 + *
  121.12 + *      The "deflation" process depends on being able to identify portions
  121.13 + *      of the input text which are identical to earlier input (within a
  121.14 + *      sliding window trailing behind the input currently being processed).
  121.15 + *
  121.16 + *      The most straightforward technique turns out to be the fastest for
  121.17 + *      most input files: try all possible matches and select the longest.
  121.18 + *      The key feature of this algorithm is that insertions into the string
  121.19 + *      dictionary are very simple and thus fast, and deletions are avoided
  121.20 + *      completely. Insertions are performed at each input character, whereas
  121.21 + *      string matches are performed only when the previous match ends. So it
  121.22 + *      is preferable to spend more time in matches to allow very fast string
  121.23 + *      insertions and avoid deletions. The matching algorithm for small
  121.24 + *      strings is inspired from that of Rabin & Karp. A brute force approach
  121.25 + *      is used to find longer strings when a small match has been found.
  121.26 + *      A similar algorithm is used in comic (by Jan-Mark Wams) and freeze
  121.27 + *      (by Leonid Broukhis).
  121.28 + *         A previous version of this file used a more sophisticated algorithm
  121.29 + *      (by Fiala and Greene) which is guaranteed to run in linear amortized
  121.30 + *      time, but has a larger average cost, uses more memory and is patented.
  121.31 + *      However the F&G algorithm may be faster for some highly redundant
  121.32 + *      files if the parameter max_chain_length (described below) is too large.
  121.33 + *
  121.34 + *  ACKNOWLEDGEMENTS
  121.35 + *
  121.36 + *      The idea of lazy evaluation of matches is due to Jan-Mark Wams, and
  121.37 + *      I found it in 'freeze' written by Leonid Broukhis.
  121.38 + *      Thanks to many people for bug reports and testing.
  121.39 + *
  121.40 + *  REFERENCES
  121.41 + *
  121.42 + *      Deutsch, L.P.,"DEFLATE Compressed Data Format Specification".
  121.43 + *      Available in http://www.ietf.org/rfc/rfc1951.txt
  121.44 + *
  121.45 + *      A description of the Rabin and Karp algorithm is given in the book
  121.46 + *         "Algorithms" by R. Sedgewick, Addison-Wesley, p252.
  121.47 + *
  121.48 + *      Fiala,E.R., and Greene,D.H.
  121.49 + *         Data Compression with Finite Windows, Comm.ACM, 32,4 (1989) 490-595
  121.50 + *
  121.51 + */
  121.52 +
  121.53 +/* @(#) $Id$ */
  121.54 +
  121.55 +#include "deflate.h"
  121.56 +
  121.57 +const char deflate_copyright[] =
  121.58 +   " deflate 1.2.3 Copyright 1995-2005 Jean-loup Gailly ";
  121.59 +/*
  121.60 +  If you use the zlib library in a product, an acknowledgment is welcome
  121.61 +  in the documentation of your product. If for some reason you cannot
  121.62 +  include such an acknowledgment, I would appreciate that you keep this
  121.63 +  copyright string in the executable of your product.
  121.64 + */
  121.65 +
  121.66 +/* ===========================================================================
  121.67 + *  Function prototypes.
  121.68 + */
  121.69 +typedef enum {
  121.70 +    need_more,      /* block not completed, need more input or more output */
  121.71 +    block_done,     /* block flush performed */
  121.72 +    finish_started, /* finish started, need only more output at next deflate */
  121.73 +    finish_done     /* finish done, accept no more input or output */
  121.74 +} block_state;
  121.75 +
  121.76 +typedef block_state (*compress_func) OF((deflate_state *s, int flush));
  121.77 +/* Compression function. Returns the block state after the call. */
  121.78 +
  121.79 +local void fill_window    OF((deflate_state *s));
  121.80 +local block_state deflate_stored OF((deflate_state *s, int flush));
  121.81 +local block_state deflate_fast   OF((deflate_state *s, int flush));
  121.82 +#ifndef FASTEST
  121.83 +local block_state deflate_slow   OF((deflate_state *s, int flush));
  121.84 +#endif
  121.85 +local void lm_init        OF((deflate_state *s));
  121.86 +local void putShortMSB    OF((deflate_state *s, uInt b));
  121.87 +local void flush_pending  OF((z_streamp strm));
  121.88 +local int read_buf        OF((z_streamp strm, Bytef *buf, unsigned size));
  121.89 +#ifndef FASTEST
  121.90 +#ifdef ASMV
  121.91 +      void match_init OF((void)); /* asm code initialization */
  121.92 +      uInt longest_match  OF((deflate_state *s, IPos cur_match));
  121.93 +#else
  121.94 +local uInt longest_match  OF((deflate_state *s, IPos cur_match));
  121.95 +#endif
  121.96 +#endif
  121.97 +local uInt longest_match_fast OF((deflate_state *s, IPos cur_match));
  121.98 +
  121.99 +#ifdef DEBUG
 121.100 +local  void check_match OF((deflate_state *s, IPos start, IPos match,
 121.101 +                            int length));
 121.102 +#endif
 121.103 +
 121.104 +/* ===========================================================================
 121.105 + * Local data
 121.106 + */
 121.107 +
 121.108 +#define NIL 0
 121.109 +/* Tail of hash chains */
 121.110 +
 121.111 +#ifndef TOO_FAR
 121.112 +#  define TOO_FAR 4096
 121.113 +#endif
 121.114 +/* Matches of length 3 are discarded if their distance exceeds TOO_FAR */
 121.115 +
 121.116 +#define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1)
 121.117 +/* Minimum amount of lookahead, except at the end of the input file.
 121.118 + * See deflate.c for comments about the MIN_MATCH+1.
 121.119 + */
 121.120 +
 121.121 +/* Values for max_lazy_match, good_match and max_chain_length, depending on
 121.122 + * the desired pack level (0..9). The values given below have been tuned to
 121.123 + * exclude worst case performance for pathological files. Better values may be
 121.124 + * found for specific files.
 121.125 + */
 121.126 +typedef struct config_s {
 121.127 +   ush good_length; /* reduce lazy search above this match length */
 121.128 +   ush max_lazy;    /* do not perform lazy search above this match length */
 121.129 +   ush nice_length; /* quit search above this match length */
 121.130 +   ush max_chain;
 121.131 +   compress_func func;
 121.132 +} config;
 121.133 +
 121.134 +#ifdef FASTEST
 121.135 +local const config configuration_table[2] = {
 121.136 +/*      good lazy nice chain */
 121.137 +/* 0 */ {0,    0,  0,    0, deflate_stored},  /* store only */
 121.138 +/* 1 */ {4,    4,  8,    4, deflate_fast}}; /* max speed, no lazy matches */
 121.139 +#else
 121.140 +local const config configuration_table[10] = {
 121.141 +/*      good lazy nice chain */
 121.142 +/* 0 */ {0,    0,  0,    0, deflate_stored},  /* store only */
 121.143 +/* 1 */ {4,    4,  8,    4, deflate_fast}, /* max speed, no lazy matches */
 121.144 +/* 2 */ {4,    5, 16,    8, deflate_fast},
 121.145 +/* 3 */ {4,    6, 32,   32, deflate_fast},
 121.146 +
 121.147 +/* 4 */ {4,    4, 16,   16, deflate_slow},  /* lazy matches */
 121.148 +/* 5 */ {8,   16, 32,   32, deflate_slow},
 121.149 +/* 6 */ {8,   16, 128, 128, deflate_slow},
 121.150 +/* 7 */ {8,   32, 128, 256, deflate_slow},
 121.151 +/* 8 */ {32, 128, 258, 1024, deflate_slow},
 121.152 +/* 9 */ {32, 258, 258, 4096, deflate_slow}}; /* max compression */
 121.153 +#endif
 121.154 +
 121.155 +/* Note: the deflate() code requires max_lazy >= MIN_MATCH and max_chain >= 4
 121.156 + * For deflate_fast() (levels <= 3) good is ignored and lazy has a different
 121.157 + * meaning.
 121.158 + */
 121.159 +
 121.160 +#define EQUAL 0
 121.161 +/* result of memcmp for equal strings */
 121.162 +
 121.163 +#ifndef NO_DUMMY_DECL
 121.164 +struct static_tree_desc_s {int dummy;}; /* for buggy compilers */
 121.165 +#endif
 121.166 +
 121.167 +/* ===========================================================================
 121.168 + * Update a hash value with the given input byte
 121.169 + * IN  assertion: all calls to to UPDATE_HASH are made with consecutive
 121.170 + *    input characters, so that a running hash key can be computed from the
 121.171 + *    previous key instead of complete recalculation each time.
 121.172 + */
 121.173 +#define UPDATE_HASH(s,h,c) (h = (((h)<<s->hash_shift) ^ (c)) & s->hash_mask)
 121.174 +
 121.175 +
 121.176 +/* ===========================================================================
 121.177 + * Insert string str in the dictionary and set match_head to the previous head
 121.178 + * of the hash chain (the most recent string with same hash key). Return
 121.179 + * the previous length of the hash chain.
 121.180 + * If this file is compiled with -DFASTEST, the compression level is forced
 121.181 + * to 1, and no hash chains are maintained.
 121.182 + * IN  assertion: all calls to to INSERT_STRING are made with consecutive
 121.183 + *    input characters and the first MIN_MATCH bytes of str are valid
 121.184 + *    (except for the last MIN_MATCH-1 bytes of the input file).
 121.185 + */
 121.186 +#ifdef FASTEST
 121.187 +#define INSERT_STRING(s, str, match_head) \
 121.188 +   (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \
 121.189 +    match_head = s->head[s->ins_h], \
 121.190 +    s->head[s->ins_h] = (Pos)(str))
 121.191 +#else
 121.192 +#define INSERT_STRING(s, str, match_head) \
 121.193 +   (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \
 121.194 +    match_head = s->prev[(str) & s->w_mask] = s->head[s->ins_h], \
 121.195 +    s->head[s->ins_h] = (Pos)(str))
 121.196 +#endif
 121.197 +
 121.198 +/* ===========================================================================
 121.199 + * Initialize the hash table (avoiding 64K overflow for 16 bit systems).
 121.200 + * prev[] will be initialized on the fly.
 121.201 + */
 121.202 +#define CLEAR_HASH(s) \
 121.203 +    s->head[s->hash_size-1] = NIL; \
 121.204 +    zmemzero((Bytef *)s->head, (unsigned)(s->hash_size-1)*sizeof(*s->head));
 121.205 +
 121.206 +/* ========================================================================= */
 121.207 +int ZEXPORT deflateInit_(strm, level, version, stream_size)
 121.208 +    z_streamp strm;
 121.209 +    int level;
 121.210 +    const char *version;
 121.211 +    int stream_size;
 121.212 +{
 121.213 +    return deflateInit2_(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL,
 121.214 +                         Z_DEFAULT_STRATEGY, version, stream_size);
 121.215 +    /* To do: ignore strm->next_in if we use it as window */
 121.216 +}
 121.217 +
 121.218 +/* ========================================================================= */
 121.219 +int ZEXPORT deflateInit2_(strm, level, method, windowBits, memLevel, strategy,
 121.220 +                  version, stream_size)
 121.221 +    z_streamp strm;
 121.222 +    int  level;
 121.223 +    int  method;
 121.224 +    int  windowBits;
 121.225 +    int  memLevel;
 121.226 +    int  strategy;
 121.227 +    const char *version;
 121.228 +    int stream_size;
 121.229 +{
 121.230 +    deflate_state *s;
 121.231 +    int wrap = 1;
 121.232 +    static const char my_version[] = ZLIB_VERSION;
 121.233 +
 121.234 +    ushf *overlay;
 121.235 +    /* We overlay pending_buf and d_buf+l_buf. This works since the average
 121.236 +     * output size for (length,distance) codes is <= 24 bits.
 121.237 +     */
 121.238 +
 121.239 +    if (version == Z_NULL || version[0] != my_version[0] ||
 121.240 +        stream_size != sizeof(z_stream)) {
 121.241 +        return Z_VERSION_ERROR;
 121.242 +    }
 121.243 +    if (strm == Z_NULL) return Z_STREAM_ERROR;
 121.244 +
 121.245 +    strm->msg = Z_NULL;
 121.246 +    if (strm->zalloc == (alloc_func)0) {
 121.247 +        strm->zalloc = zcalloc;
 121.248 +        strm->opaque = (voidpf)0;
 121.249 +    }
 121.250 +    if (strm->zfree == (free_func)0) strm->zfree = zcfree;
 121.251 +
 121.252 +#ifdef FASTEST
 121.253 +    if (level != 0) level = 1;
 121.254 +#else
 121.255 +    if (level == Z_DEFAULT_COMPRESSION) level = 6;
 121.256 +#endif
 121.257 +
 121.258 +    if (windowBits < 0) { /* suppress zlib wrapper */
 121.259 +        wrap = 0;
 121.260 +        windowBits = -windowBits;
 121.261 +    }
 121.262 +#ifdef GZIP
 121.263 +    else if (windowBits > 15) {
 121.264 +        wrap = 2;       /* write gzip wrapper instead */
 121.265 +        windowBits -= 16;
 121.266 +    }
 121.267 +#endif
 121.268 +    if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != Z_DEFLATED ||
 121.269 +        windowBits < 8 || windowBits > 15 || level < 0 || level > 9 ||
 121.270 +        strategy < 0 || strategy > Z_FIXED) {
 121.271 +        return Z_STREAM_ERROR;
 121.272 +    }
 121.273 +    if (windowBits == 8) windowBits = 9;  /* until 256-byte window bug fixed */
 121.274 +    s = (deflate_state *) ZALLOC(strm, 1, sizeof(deflate_state));
 121.275 +    if (s == Z_NULL) return Z_MEM_ERROR;
 121.276 +    strm->state = (struct internal_state FAR *)s;
 121.277 +    s->strm = strm;
 121.278 +
 121.279 +    s->wrap = wrap;
 121.280 +    s->gzhead = Z_NULL;
 121.281 +    s->w_bits = windowBits;
 121.282 +    s->w_size = 1 << s->w_bits;
 121.283 +    s->w_mask = s->w_size - 1;
 121.284 +
 121.285 +    s->hash_bits = memLevel + 7;
 121.286 +    s->hash_size = 1 << s->hash_bits;
 121.287 +    s->hash_mask = s->hash_size - 1;
 121.288 +    s->hash_shift =  ((s->hash_bits+MIN_MATCH-1)/MIN_MATCH);
 121.289 +
 121.290 +    s->window = (Bytef *) ZALLOC(strm, s->w_size, 2*sizeof(Byte));
 121.291 +    s->prev   = (Posf *)  ZALLOC(strm, s->w_size, sizeof(Pos));
 121.292 +    s->head   = (Posf *)  ZALLOC(strm, s->hash_size, sizeof(Pos));
 121.293 +
 121.294 +    s->lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */
 121.295 +
 121.296 +    overlay = (ushf *) ZALLOC(strm, s->lit_bufsize, sizeof(ush)+2);
 121.297 +    s->pending_buf = (uchf *) overlay;
 121.298 +    s->pending_buf_size = (ulg)s->lit_bufsize * (sizeof(ush)+2L);
 121.299 +
 121.300 +    if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL ||
 121.301 +        s->pending_buf == Z_NULL) {
 121.302 +        s->status = FINISH_STATE;
 121.303 +        strm->msg = (char*)ERR_MSG(Z_MEM_ERROR);
 121.304 +        deflateEnd (strm);
 121.305 +        return Z_MEM_ERROR;
 121.306 +    }
 121.307 +    s->d_buf = overlay + s->lit_bufsize/sizeof(ush);
 121.308 +    s->l_buf = s->pending_buf + (1+sizeof(ush))*s->lit_bufsize;
 121.309 +
 121.310 +    s->level = level;
 121.311 +    s->strategy = strategy;
 121.312 +    s->method = (Byte)method;
 121.313 +
 121.314 +    return deflateReset(strm);
 121.315 +}
 121.316 +
 121.317 +/* ========================================================================= */
 121.318 +int ZEXPORT deflateSetDictionary (strm, dictionary, dictLength)
 121.319 +    z_streamp strm;
 121.320 +    const Bytef *dictionary;
 121.321 +    uInt  dictLength;
 121.322 +{
 121.323 +    deflate_state *s;
 121.324 +    uInt length = dictLength;
 121.325 +    uInt n;
 121.326 +    IPos hash_head = 0;
 121.327 +
 121.328 +    if (strm == Z_NULL || strm->state == Z_NULL || dictionary == Z_NULL ||
 121.329 +        strm->state->wrap == 2 ||
 121.330 +        (strm->state->wrap == 1 && strm->state->status != INIT_STATE))
 121.331 +        return Z_STREAM_ERROR;
 121.332 +
 121.333 +    s = strm->state;
 121.334 +    if (s->wrap)
 121.335 +        strm->adler = adler32(strm->adler, dictionary, dictLength);
 121.336 +
 121.337 +    if (length < MIN_MATCH) return Z_OK;
 121.338 +    if (length > MAX_DIST(s)) {
 121.339 +        length = MAX_DIST(s);
 121.340 +        dictionary += dictLength - length; /* use the tail of the dictionary */
 121.341 +    }
 121.342 +    zmemcpy(s->window, dictionary, length);
 121.343 +    s->strstart = length;
 121.344 +    s->block_start = (long)length;
 121.345 +
 121.346 +    /* Insert all strings in the hash table (except for the last two bytes).
 121.347 +     * s->lookahead stays null, so s->ins_h will be recomputed at the next
 121.348 +     * call of fill_window.
 121.349 +     */
 121.350 +    s->ins_h = s->window[0];
 121.351 +    UPDATE_HASH(s, s->ins_h, s->window[1]);
 121.352 +    for (n = 0; n <= length - MIN_MATCH; n++) {
 121.353 +        INSERT_STRING(s, n, hash_head);
 121.354 +    }
 121.355 +    if (hash_head) hash_head = 0;  /* to make compiler happy */
 121.356 +    return Z_OK;
 121.357 +}
 121.358 +
 121.359 +/* ========================================================================= */
 121.360 +int ZEXPORT deflateReset (strm)
 121.361 +    z_streamp strm;
 121.362 +{
 121.363 +    deflate_state *s;
 121.364 +
 121.365 +    if (strm == Z_NULL || strm->state == Z_NULL ||
 121.366 +        strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0) {
 121.367 +        return Z_STREAM_ERROR;
 121.368 +    }
 121.369 +
 121.370 +    strm->total_in = strm->total_out = 0;
 121.371 +    strm->msg = Z_NULL; /* use zfree if we ever allocate msg dynamically */
 121.372 +    strm->data_type = Z_UNKNOWN;
 121.373 +
 121.374 +    s = (deflate_state *)strm->state;
 121.375 +    s->pending = 0;
 121.376 +    s->pending_out = s->pending_buf;
 121.377 +
 121.378 +    if (s->wrap < 0) {
 121.379 +        s->wrap = -s->wrap; /* was made negative by deflate(..., Z_FINISH); */
 121.380 +    }
 121.381 +    s->status = s->wrap ? INIT_STATE : BUSY_STATE;
 121.382 +    strm->adler =
 121.383 +#ifdef GZIP
 121.384 +        s->wrap == 2 ? crc32(0L, Z_NULL, 0) :
 121.385 +#endif
 121.386 +        adler32(0L, Z_NULL, 0);
 121.387 +    s->last_flush = Z_NO_FLUSH;
 121.388 +
 121.389 +    _tr_init(s);
 121.390 +    lm_init(s);
 121.391 +
 121.392 +    return Z_OK;
 121.393 +}
 121.394 +
 121.395 +/* ========================================================================= */
 121.396 +int ZEXPORT deflateSetHeader (strm, head)
 121.397 +    z_streamp strm;
 121.398 +    gz_headerp head;
 121.399 +{
 121.400 +    if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
 121.401 +    if (strm->state->wrap != 2) return Z_STREAM_ERROR;
 121.402 +    strm->state->gzhead = head;
 121.403 +    return Z_OK;
 121.404 +}
 121.405 +
 121.406 +/* ========================================================================= */
 121.407 +int ZEXPORT deflatePrime (strm, bits, value)
 121.408 +    z_streamp strm;
 121.409 +    int bits;
 121.410 +    int value;
 121.411 +{
 121.412 +    if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
 121.413 +    strm->state->bi_valid = bits;
 121.414 +    strm->state->bi_buf = (ush)(value & ((1 << bits) - 1));
 121.415 +    return Z_OK;
 121.416 +}
 121.417 +
 121.418 +/* ========================================================================= */
 121.419 +int ZEXPORT deflateParams(strm, level, strategy)
 121.420 +    z_streamp strm;
 121.421 +    int level;
 121.422 +    int strategy;
 121.423 +{
 121.424 +    deflate_state *s;
 121.425 +    compress_func func;
 121.426 +    int err = Z_OK;
 121.427 +
 121.428 +    if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
 121.429 +    s = strm->state;
 121.430 +
 121.431 +#ifdef FASTEST
 121.432 +    if (level != 0) level = 1;
 121.433 +#else
 121.434 +    if (level == Z_DEFAULT_COMPRESSION) level = 6;
 121.435 +#endif
 121.436 +    if (level < 0 || level > 9 || strategy < 0 || strategy > Z_FIXED) {
 121.437 +        return Z_STREAM_ERROR;
 121.438 +    }
 121.439 +    func = configuration_table[s->level].func;
 121.440 +
 121.441 +    if (func != configuration_table[level].func && strm->total_in != 0) {
 121.442 +        /* Flush the last buffer: */
 121.443 +        err = deflate(strm, Z_PARTIAL_FLUSH);
 121.444 +    }
 121.445 +    if (s->level != level) {
 121.446 +        s->level = level;
 121.447 +        s->max_lazy_match   = configuration_table[level].max_lazy;
 121.448 +        s->good_match       = configuration_table[level].good_length;
 121.449 +        s->nice_match       = configuration_table[level].nice_length;
 121.450 +        s->max_chain_length = configuration_table[level].max_chain;
 121.451 +    }
 121.452 +    s->strategy = strategy;
 121.453 +    return err;
 121.454 +}
 121.455 +
 121.456 +/* ========================================================================= */
 121.457 +int ZEXPORT deflateTune(strm, good_length, max_lazy, nice_length, max_chain)
 121.458 +    z_streamp strm;
 121.459 +    int good_length;
 121.460 +    int max_lazy;
 121.461 +    int nice_length;
 121.462 +    int max_chain;
 121.463 +{
 121.464 +    deflate_state *s;
 121.465 +
 121.466 +    if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
 121.467 +    s = strm->state;
 121.468 +    s->good_match = good_length;
 121.469 +    s->max_lazy_match = max_lazy;
 121.470 +    s->nice_match = nice_length;
 121.471 +    s->max_chain_length = max_chain;
 121.472 +    return Z_OK;
 121.473 +}
 121.474 +
 121.475 +/* =========================================================================
 121.476 + * For the default windowBits of 15 and memLevel of 8, this function returns
 121.477 + * a close to exact, as well as small, upper bound on the compressed size.
 121.478 + * They are coded as constants here for a reason--if the #define's are
 121.479 + * changed, then this function needs to be changed as well.  The return
 121.480 + * value for 15 and 8 only works for those exact settings.
 121.481 + *
 121.482 + * For any setting other than those defaults for windowBits and memLevel,
 121.483 + * the value returned is a conservative worst case for the maximum expansion
 121.484 + * resulting from using fixed blocks instead of stored blocks, which deflate
 121.485 + * can emit on compressed data for some combinations of the parameters.
 121.486 + *
 121.487 + * This function could be more sophisticated to provide closer upper bounds
 121.488 + * for every combination of windowBits and memLevel, as well as wrap.
 121.489 + * But even the conservative upper bound of about 14% expansion does not
 121.490 + * seem onerous for output buffer allocation.
 121.491 + */
 121.492 +uLong ZEXPORT deflateBound(strm, sourceLen)
 121.493 +    z_streamp strm;
 121.494 +    uLong sourceLen;
 121.495 +{
 121.496 +    deflate_state *s;
 121.497 +    uLong destLen;
 121.498 +
 121.499 +    /* conservative upper bound */
 121.500 +    destLen = sourceLen +
 121.501 +              ((sourceLen + 7) >> 3) + ((sourceLen + 63) >> 6) + 11;
 121.502 +
 121.503 +    /* if can't get parameters, return conservative bound */
 121.504 +    if (strm == Z_NULL || strm->state == Z_NULL)
 121.505 +        return destLen;
 121.506 +
 121.507 +    /* if not default parameters, return conservative bound */
 121.508 +    s = strm->state;
 121.509 +    if (s->w_bits != 15 || s->hash_bits != 8 + 7)
 121.510 +        return destLen;
 121.511 +
 121.512 +    /* default settings: return tight bound for that case */
 121.513 +    return compressBound(sourceLen);
 121.514 +}
 121.515 +
 121.516 +/* =========================================================================
 121.517 + * Put a short in the pending buffer. The 16-bit value is put in MSB order.
 121.518 + * IN assertion: the stream state is correct and there is enough room in
 121.519 + * pending_buf.
 121.520 + */
 121.521 +local void putShortMSB (s, b)
 121.522 +    deflate_state *s;
 121.523 +    uInt b;
 121.524 +{
 121.525 +    put_byte(s, (Byte)(b >> 8));
 121.526 +    put_byte(s, (Byte)(b & 0xff));
 121.527 +}
 121.528 +
 121.529 +/* =========================================================================
 121.530 + * Flush as much pending output as possible. All deflate() output goes
 121.531 + * through this function so some applications may wish to modify it
 121.532 + * to avoid allocating a large strm->next_out buffer and copying into it.
 121.533 + * (See also read_buf()).
 121.534 + */
 121.535 +local void flush_pending(strm)
 121.536 +    z_streamp strm;
 121.537 +{
 121.538 +    unsigned len = strm->state->pending;
 121.539 +
 121.540 +    if (len > strm->avail_out) len = strm->avail_out;
 121.541 +    if (len == 0) return;
 121.542 +
 121.543 +    zmemcpy(strm->next_out, strm->state->pending_out, len);
 121.544 +    strm->next_out  += len;
 121.545 +    strm->state->pending_out  += len;
 121.546 +    strm->total_out += len;
 121.547 +    strm->avail_out  -= len;
 121.548 +    strm->state->pending -= len;
 121.549 +    if (strm->state->pending == 0) {
 121.550 +        strm->state->pending_out = strm->state->pending_buf;
 121.551 +    }
 121.552 +}
 121.553 +
 121.554 +/* ========================================================================= */
 121.555 +int ZEXPORT deflate (strm, flush)
 121.556 +    z_streamp strm;
 121.557 +    int flush;
 121.558 +{
 121.559 +    int old_flush; /* value of flush param for previous deflate call */
 121.560 +    deflate_state *s;
 121.561 +
 121.562 +    if (strm == Z_NULL || strm->state == Z_NULL ||
 121.563 +        flush > Z_FINISH || flush < 0) {
 121.564 +        return Z_STREAM_ERROR;
 121.565 +    }
 121.566 +    s = strm->state;
 121.567 +
 121.568 +    if (strm->next_out == Z_NULL ||
 121.569 +        (strm->next_in == Z_NULL && strm->avail_in != 0) ||
 121.570 +        (s->status == FINISH_STATE && flush != Z_FINISH)) {
 121.571 +        ERR_RETURN(strm, Z_STREAM_ERROR);
 121.572 +    }
 121.573 +    if (strm->avail_out == 0) ERR_RETURN(strm, Z_BUF_ERROR);
 121.574 +
 121.575 +    s->strm = strm; /* just in case */
 121.576 +    old_flush = s->last_flush;
 121.577 +    s->last_flush = flush;
 121.578 +
 121.579 +    /* Write the header */
 121.580 +    if (s->status == INIT_STATE) {
 121.581 +#ifdef GZIP
 121.582 +        if (s->wrap == 2) {
 121.583 +            strm->adler = crc32(0L, Z_NULL, 0);
 121.584 +            put_byte(s, 31);
 121.585 +            put_byte(s, 139);
 121.586 +            put_byte(s, 8);
 121.587 +            if (s->gzhead == NULL) {
 121.588 +                put_byte(s, 0);
 121.589 +                put_byte(s, 0);
 121.590 +                put_byte(s, 0);
 121.591 +                put_byte(s, 0);
 121.592 +                put_byte(s, 0);
 121.593 +                put_byte(s, s->level == 9 ? 2 :
 121.594 +                            (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ?
 121.595 +                             4 : 0));
 121.596 +                put_byte(s, OS_CODE);
 121.597 +                s->status = BUSY_STATE;
 121.598 +            }
 121.599 +            else {
 121.600 +                put_byte(s, (s->gzhead->text ? 1 : 0) +
 121.601 +                            (s->gzhead->hcrc ? 2 : 0) +
 121.602 +                            (s->gzhead->extra == Z_NULL ? 0 : 4) +
 121.603 +                            (s->gzhead->name == Z_NULL ? 0 : 8) +
 121.604 +                            (s->gzhead->comment == Z_NULL ? 0 : 16)
 121.605 +                        );
 121.606 +                put_byte(s, (Byte)(s->gzhead->time & 0xff));
 121.607 +                put_byte(s, (Byte)((s->gzhead->time >> 8) & 0xff));
 121.608 +                put_byte(s, (Byte)((s->gzhead->time >> 16) & 0xff));
 121.609 +                put_byte(s, (Byte)((s->gzhead->time >> 24) & 0xff));
 121.610 +                put_byte(s, s->level == 9 ? 2 :
 121.611 +                            (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ?
 121.612 +                             4 : 0));
 121.613 +                put_byte(s, s->gzhead->os & 0xff);
 121.614 +                if (s->gzhead->extra != NULL) {
 121.615 +                    put_byte(s, s->gzhead->extra_len & 0xff);
 121.616 +                    put_byte(s, (s->gzhead->extra_len >> 8) & 0xff);
 121.617 +                }
 121.618 +                if (s->gzhead->hcrc)
 121.619 +                    strm->adler = crc32(strm->adler, s->pending_buf,
 121.620 +                                        s->pending);
 121.621 +                s->gzindex = 0;
 121.622 +                s->status = EXTRA_STATE;
 121.623 +            }
 121.624 +        }
 121.625 +        else
 121.626 +#endif
 121.627 +        {
 121.628 +            uInt header = (Z_DEFLATED + ((s->w_bits-8)<<4)) << 8;
 121.629 +            uInt level_flags;
 121.630 +
 121.631 +            if (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2)
 121.632 +                level_flags = 0;
 121.633 +            else if (s->level < 6)
 121.634 +                level_flags = 1;
 121.635 +            else if (s->level == 6)
 121.636 +                level_flags = 2;
 121.637 +            else
 121.638 +                level_flags = 3;
 121.639 +            header |= (level_flags << 6);
 121.640 +            if (s->strstart != 0) header |= PRESET_DICT;
 121.641 +            header += 31 - (header % 31);
 121.642 +
 121.643 +            s->status = BUSY_STATE;
 121.644 +            putShortMSB(s, header);
 121.645 +
 121.646 +            /* Save the adler32 of the preset dictionary: */
 121.647 +            if (s->strstart != 0) {
 121.648 +                putShortMSB(s, (uInt)(strm->adler >> 16));
 121.649 +                putShortMSB(s, (uInt)(strm->adler & 0xffff));
 121.650 +            }
 121.651 +            strm->adler = adler32(0L, Z_NULL, 0);
 121.652 +        }
 121.653 +    }
 121.654 +#ifdef GZIP
 121.655 +    if (s->status == EXTRA_STATE) {
 121.656 +        if (s->gzhead->extra != NULL) {
 121.657 +            uInt beg = s->pending;  /* start of bytes to update crc */
 121.658 +
 121.659 +            while (s->gzindex < (s->gzhead->extra_len & 0xffff)) {
 121.660 +                if (s->pending == s->pending_buf_size) {
 121.661 +                    if (s->gzhead->hcrc && s->pending > beg)
 121.662 +                        strm->adler = crc32(strm->adler, s->pending_buf + beg,
 121.663 +                                            s->pending - beg);
 121.664 +                    flush_pending(strm);
 121.665 +                    beg = s->pending;
 121.666 +                    if (s->pending == s->pending_buf_size)
 121.667 +                        break;
 121.668 +                }
 121.669 +                put_byte(s, s->gzhead->extra[s->gzindex]);
 121.670 +                s->gzindex++;
 121.671 +            }
 121.672 +            if (s->gzhead->hcrc && s->pending > beg)
 121.673 +                strm->adler = crc32(strm->adler, s->pending_buf + beg,
 121.674 +                                    s->pending - beg);
 121.675 +            if (s->gzindex == s->gzhead->extra_len) {
 121.676 +                s->gzindex = 0;
 121.677 +                s->status = NAME_STATE;
 121.678 +            }
 121.679 +        }
 121.680 +        else
 121.681 +            s->status = NAME_STATE;
 121.682 +    }
 121.683 +    if (s->status == NAME_STATE) {
 121.684 +        if (s->gzhead->name != NULL) {
 121.685 +            uInt beg = s->pending;  /* start of bytes to update crc */
 121.686 +            int val;
 121.687 +
 121.688 +            do {
 121.689 +                if (s->pending == s->pending_buf_size) {
 121.690 +                    if (s->gzhead->hcrc && s->pending > beg)
 121.691 +                        strm->adler = crc32(strm->adler, s->pending_buf + beg,
 121.692 +                                            s->pending - beg);
 121.693 +                    flush_pending(strm);
 121.694 +                    beg = s->pending;
 121.695 +                    if (s->pending == s->pending_buf_size) {
 121.696 +                        val = 1;
 121.697 +                        break;
 121.698 +                    }
 121.699 +                }
 121.700 +                val = s->gzhead->name[s->gzindex++];
 121.701 +                put_byte(s, val);
 121.702 +            } while (val != 0);
 121.703 +            if (s->gzhead->hcrc && s->pending > beg)
 121.704 +                strm->adler = crc32(strm->adler, s->pending_buf + beg,
 121.705 +                                    s->pending - beg);
 121.706 +            if (val == 0) {
 121.707 +                s->gzindex = 0;
 121.708 +                s->status = COMMENT_STATE;
 121.709 +            }
 121.710 +        }
 121.711 +        else
 121.712 +            s->status = COMMENT_STATE;
 121.713 +    }
 121.714 +    if (s->status == COMMENT_STATE) {
 121.715 +        if (s->gzhead->comment != NULL) {
 121.716 +            uInt beg = s->pending;  /* start of bytes to update crc */
 121.717 +            int val;
 121.718 +
 121.719 +            do {
 121.720 +                if (s->pending == s->pending_buf_size) {
 121.721 +                    if (s->gzhead->hcrc && s->pending > beg)
 121.722 +                        strm->adler = crc32(strm->adler, s->pending_buf + beg,
 121.723 +                                            s->pending - beg);
 121.724 +                    flush_pending(strm);
 121.725 +                    beg = s->pending;
 121.726 +                    if (s->pending == s->pending_buf_size) {
 121.727 +                        val = 1;
 121.728 +                        break;
 121.729 +                    }
 121.730 +                }
 121.731 +                val = s->gzhead->comment[s->gzindex++];
 121.732 +                put_byte(s, val);
 121.733 +            } while (val != 0);
 121.734 +            if (s->gzhead->hcrc && s->pending > beg)
 121.735 +                strm->adler = crc32(strm->adler, s->pending_buf + beg,
 121.736 +                                    s->pending - beg);
 121.737 +            if (val == 0)
 121.738 +                s->status = HCRC_STATE;
 121.739 +        }
 121.740 +        else
 121.741 +            s->status = HCRC_STATE;
 121.742 +    }
 121.743 +    if (s->status == HCRC_STATE) {
 121.744 +        if (s->gzhead->hcrc) {
 121.745 +            if (s->pending + 2 > s->pending_buf_size)
 121.746 +                flush_pending(strm);
 121.747 +            if (s->pending + 2 <= s->pending_buf_size) {
 121.748 +                put_byte(s, (Byte)(strm->adler & 0xff));
 121.749 +                put_byte(s, (Byte)((strm->adler >> 8) & 0xff));
 121.750 +                strm->adler = crc32(0L, Z_NULL, 0);
 121.751 +                s->status = BUSY_STATE;
 121.752 +            }
 121.753 +        }
 121.754 +        else
 121.755 +            s->status = BUSY_STATE;
 121.756 +    }
 121.757 +#endif
 121.758 +
 121.759 +    /* Flush as much pending output as possible */
 121.760 +    if (s->pending != 0) {
 121.761 +        flush_pending(strm);
 121.762 +        if (strm->avail_out == 0) {
 121.763 +            /* Since avail_out is 0, deflate will be called again with
 121.764 +             * more output space, but possibly with both pending and
 121.765 +             * avail_in equal to zero. There won't be anything to do,
 121.766 +             * but this is not an error situation so make sure we
 121.767 +             * return OK instead of BUF_ERROR at next call of deflate:
 121.768 +             */
 121.769 +            s->last_flush = -1;
 121.770 +            return Z_OK;
 121.771 +        }
 121.772 +
 121.773 +    /* Make sure there is something to do and avoid duplicate consecutive
 121.774 +     * flushes. For repeated and useless calls with Z_FINISH, we keep
 121.775 +     * returning Z_STREAM_END instead of Z_BUF_ERROR.
 121.776 +     */
 121.777 +    } else if (strm->avail_in == 0 && flush <= old_flush &&
 121.778 +               flush != Z_FINISH) {
 121.779 +        ERR_RETURN(strm, Z_BUF_ERROR);
 121.780 +    }
 121.781 +
 121.782 +    /* User must not provide more input after the first FINISH: */
 121.783 +    if (s->status == FINISH_STATE && strm->avail_in != 0) {
 121.784 +        ERR_RETURN(strm, Z_BUF_ERROR);
 121.785 +    }
 121.786 +
 121.787 +    /* Start a new block or continue the current one.
 121.788 +     */
 121.789 +    if (strm->avail_in != 0 || s->lookahead != 0 ||
 121.790 +        (flush != Z_NO_FLUSH && s->status != FINISH_STATE)) {
 121.791 +        block_state bstate;
 121.792 +
 121.793 +        bstate = (*(configuration_table[s->level].func))(s, flush);
 121.794 +
 121.795 +        if (bstate == finish_started || bstate == finish_done) {
 121.796 +            s->status = FINISH_STATE;
 121.797 +        }
 121.798 +        if (bstate == need_more || bstate == finish_started) {
 121.799 +            if (strm->avail_out == 0) {
 121.800 +                s->last_flush = -1; /* avoid BUF_ERROR next call, see above */
 121.801 +            }
 121.802 +            return Z_OK;
 121.803 +            /* If flush != Z_NO_FLUSH && avail_out == 0, the next call
 121.804 +             * of deflate should use the same flush parameter to make sure
 121.805 +             * that the flush is complete. So we don't have to output an
 121.806 +             * empty block here, this will be done at next call. This also
 121.807 +             * ensures that for a very small output buffer, we emit at most
 121.808 +             * one empty block.
 121.809 +             */
 121.810 +        }
 121.811 +        if (bstate == block_done) {
 121.812 +            if (flush == Z_PARTIAL_FLUSH) {
 121.813 +                _tr_align(s);
 121.814 +            } else { /* FULL_FLUSH or SYNC_FLUSH */
 121.815 +                _tr_stored_block(s, (char*)0, 0L, 0);
 121.816 +                /* For a full flush, this empty block will be recognized
 121.817 +                 * as a special marker by inflate_sync().
 121.818 +                 */
 121.819 +                if (flush == Z_FULL_FLUSH) {
 121.820 +                    CLEAR_HASH(s);             /* forget history */
 121.821 +                }
 121.822 +            }
 121.823 +            flush_pending(strm);
 121.824 +            if (strm->avail_out == 0) {
 121.825 +              s->last_flush = -1; /* avoid BUF_ERROR at next call, see above */
 121.826 +              return Z_OK;
 121.827 +            }
 121.828 +        }
 121.829 +    }
 121.830 +    Assert(strm->avail_out > 0, "bug2");
 121.831 +
 121.832 +    if (flush != Z_FINISH) return Z_OK;
 121.833 +    if (s->wrap <= 0) return Z_STREAM_END;
 121.834 +
 121.835 +    /* Write the trailer */
 121.836 +#ifdef GZIP
 121.837 +    if (s->wrap == 2) {
 121.838 +        put_byte(s, (Byte)(strm->adler & 0xff));
 121.839 +        put_byte(s, (Byte)((strm->adler >> 8) & 0xff));
 121.840 +        put_byte(s, (Byte)((strm->adler >> 16) & 0xff));
 121.841 +        put_byte(s, (Byte)((strm->adler >> 24) & 0xff));
 121.842 +        put_byte(s, (Byte)(strm->total_in & 0xff));
 121.843 +        put_byte(s, (Byte)((strm->total_in >> 8) & 0xff));
 121.844 +        put_byte(s, (Byte)((strm->total_in >> 16) & 0xff));
 121.845 +        put_byte(s, (Byte)((strm->total_in >> 24) & 0xff));
 121.846 +    }
 121.847 +    else
 121.848 +#endif
 121.849 +    {
 121.850 +        putShortMSB(s, (uInt)(strm->adler >> 16));
 121.851 +        putShortMSB(s, (uInt)(strm->adler & 0xffff));
 121.852 +    }
 121.853 +    flush_pending(strm);
 121.854 +    /* If avail_out is zero, the application will call deflate again
 121.855 +     * to flush the rest.
 121.856 +     */
 121.857 +    if (s->wrap > 0) s->wrap = -s->wrap; /* write the trailer only once! */
 121.858 +    return s->pending != 0 ? Z_OK : Z_STREAM_END;
 121.859 +}
 121.860 +
 121.861 +/* ========================================================================= */
 121.862 +int ZEXPORT deflateEnd (strm)
 121.863 +    z_streamp strm;
 121.864 +{
 121.865 +    int status;
 121.866 +
 121.867 +    if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
 121.868 +
 121.869 +    status = strm->state->status;
 121.870 +    if (status != INIT_STATE &&
 121.871 +        status != EXTRA_STATE &&
 121.872 +        status != NAME_STATE &&
 121.873 +        status != COMMENT_STATE &&
 121.874 +        status != HCRC_STATE &&
 121.875 +        status != BUSY_STATE &&
 121.876 +        status != FINISH_STATE) {
 121.877 +      return Z_STREAM_ERROR;
 121.878 +    }
 121.879 +
 121.880 +    /* Deallocate in reverse order of allocations: */
 121.881 +    TRY_FREE(strm, strm->state->pending_buf);
 121.882 +    TRY_FREE(strm, strm->state->head);
 121.883 +    TRY_FREE(strm, strm->state->prev);
 121.884 +    TRY_FREE(strm, strm->state->window);
 121.885 +
 121.886 +    ZFREE(strm, strm->state);
 121.887 +    strm->state = Z_NULL;
 121.888 +
 121.889 +    return status == BUSY_STATE ? Z_DATA_ERROR : Z_OK;
 121.890 +}
 121.891 +
 121.892 +/* =========================================================================
 121.893 + * Copy the source state to the destination state.
 121.894 + * To simplify the source, this is not supported for 16-bit MSDOS (which
 121.895 + * doesn't have enough memory anyway to duplicate compression states).
 121.896 + */
 121.897 +int ZEXPORT deflateCopy (dest, source)
 121.898 +    z_streamp dest;
 121.899 +    z_streamp source;
 121.900 +{
 121.901 +#ifdef MAXSEG_64K
 121.902 +    return Z_STREAM_ERROR;
 121.903 +#else
 121.904 +    deflate_state *ds;
 121.905 +    deflate_state *ss;
 121.906 +    ushf *overlay;
 121.907 +
 121.908 +
 121.909 +    if (source == Z_NULL || dest == Z_NULL || source->state == Z_NULL) {
 121.910 +        return Z_STREAM_ERROR;
 121.911 +    }
 121.912 +
 121.913 +    ss = source->state;
 121.914 +
 121.915 +    zmemcpy(dest, source, sizeof(z_stream));
 121.916 +
 121.917 +    ds = (deflate_state *) ZALLOC(dest, 1, sizeof(deflate_state));
 121.918 +    if (ds == Z_NULL) return Z_MEM_ERROR;
 121.919 +    dest->state = (struct internal_state FAR *) ds;
 121.920 +    zmemcpy(ds, ss, sizeof(deflate_state));
 121.921 +    ds->strm = dest;
 121.922 +
 121.923 +    ds->window = (Bytef *) ZALLOC(dest, ds->w_size, 2*sizeof(Byte));
 121.924 +    ds->prev   = (Posf *)  ZALLOC(dest, ds->w_size, sizeof(Pos));
 121.925 +    ds->head   = (Posf *)  ZALLOC(dest, ds->hash_size, sizeof(Pos));
 121.926 +    overlay = (ushf *) ZALLOC(dest, ds->lit_bufsize, sizeof(ush)+2);
 121.927 +    ds->pending_buf = (uchf *) overlay;
 121.928 +
 121.929 +    if (ds->window == Z_NULL || ds->prev == Z_NULL || ds->head == Z_NULL ||
 121.930 +        ds->pending_buf == Z_NULL) {
 121.931 +        deflateEnd (dest);
 121.932 +        return Z_MEM_ERROR;
 121.933 +    }
 121.934 +    /* following zmemcpy do not work for 16-bit MSDOS */
 121.935 +    zmemcpy(ds->window, ss->window, ds->w_size * 2 * sizeof(Byte));
 121.936 +    zmemcpy(ds->prev, ss->prev, ds->w_size * sizeof(Pos));
 121.937 +    zmemcpy(ds->head, ss->head, ds->hash_size * sizeof(Pos));
 121.938 +    zmemcpy(ds->pending_buf, ss->pending_buf, (uInt)ds->pending_buf_size);
 121.939 +
 121.940 +    ds->pending_out = ds->pending_buf + (ss->pending_out - ss->pending_buf);
 121.941 +    ds->d_buf = overlay + ds->lit_bufsize/sizeof(ush);
 121.942 +    ds->l_buf = ds->pending_buf + (1+sizeof(ush))*ds->lit_bufsize;
 121.943 +
 121.944 +    ds->l_desc.dyn_tree = ds->dyn_ltree;
 121.945 +    ds->d_desc.dyn_tree = ds->dyn_dtree;
 121.946 +    ds->bl_desc.dyn_tree = ds->bl_tree;
 121.947 +
 121.948 +    return Z_OK;
 121.949 +#endif /* MAXSEG_64K */
 121.950 +}
 121.951 +
 121.952 +/* ===========================================================================
 121.953 + * Read a new buffer from the current input stream, update the adler32
 121.954 + * and total number of bytes read.  All deflate() input goes through
 121.955 + * this function so some applications may wish to modify it to avoid
 121.956 + * allocating a large strm->next_in buffer and copying from it.
 121.957 + * (See also flush_pending()).
 121.958 + */
 121.959 +local int read_buf(strm, buf, size)
 121.960 +    z_streamp strm;
 121.961 +    Bytef *buf;
 121.962 +    unsigned size;
 121.963 +{
 121.964 +    unsigned len = strm->avail_in;
 121.965 +
 121.966 +    if (len > size) len = size;
 121.967 +    if (len == 0) return 0;
 121.968 +
 121.969 +    strm->avail_in  -= len;
 121.970 +
 121.971 +    if (strm->state->wrap == 1) {
 121.972 +        strm->adler = adler32(strm->adler, strm->next_in, len);
 121.973 +    }
 121.974 +#ifdef GZIP
 121.975 +    else if (strm->state->wrap == 2) {
 121.976 +        strm->adler = crc32(strm->adler, strm->next_in, len);
 121.977 +    }
 121.978 +#endif
 121.979 +    zmemcpy(buf, strm->next_in, len);
 121.980 +    strm->next_in  += len;
 121.981 +    strm->total_in += len;
 121.982 +
 121.983 +    return (int)len;
 121.984 +}
 121.985 +
 121.986 +/* ===========================================================================
 121.987 + * Initialize the "longest match" routines for a new zlib stream
 121.988 + */
 121.989 +local void lm_init (s)
 121.990 +    deflate_state *s;
 121.991 +{
 121.992 +    s->window_size = (ulg)2L*s->w_size;
 121.993 +
 121.994 +    CLEAR_HASH(s);
 121.995 +
 121.996 +    /* Set the default configuration parameters:
 121.997 +     */
 121.998 +    s->max_lazy_match   = configuration_table[s->level].max_lazy;
 121.999 +    s->good_match       = configuration_table[s->level].good_length;
121.1000 +    s->nice_match       = configuration_table[s->level].nice_length;
121.1001 +    s->max_chain_length = configuration_table[s->level].max_chain;
121.1002 +
121.1003 +    s->strstart = 0;
121.1004 +    s->block_start = 0L;
121.1005 +    s->lookahead = 0;
121.1006 +    s->match_length = s->prev_length = MIN_MATCH-1;
121.1007 +    s->match_available = 0;
121.1008 +    s->ins_h = 0;
121.1009 +#ifndef FASTEST
121.1010 +#ifdef ASMV
121.1011 +    match_init(); /* initialize the asm code */
121.1012 +#endif
121.1013 +#endif
121.1014 +}
121.1015 +
121.1016 +#ifndef FASTEST
121.1017 +/* ===========================================================================
121.1018 + * Set match_start to the longest match starting at the given string and
121.1019 + * return its length. Matches shorter or equal to prev_length are discarded,
121.1020 + * in which case the result is equal to prev_length and match_start is
121.1021 + * garbage.
121.1022 + * IN assertions: cur_match is the head of the hash chain for the current
121.1023 + *   string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1
121.1024 + * OUT assertion: the match length is not greater than s->lookahead.
121.1025 + */
121.1026 +#ifndef ASMV
121.1027 +/* For 80x86 and 680x0, an optimized version will be provided in match.asm or
121.1028 + * match.S. The code will be functionally equivalent.
121.1029 + */
121.1030 +local uInt longest_match(s, cur_match)
121.1031 +    deflate_state *s;
121.1032 +    IPos cur_match;                             /* current match */
121.1033 +{
121.1034 +    unsigned chain_length = s->max_chain_length;/* max hash chain length */
121.1035 +    register Bytef *scan = s->window + s->strstart; /* current string */
121.1036 +    register Bytef *match;                       /* matched string */
121.1037 +    register int len;                           /* length of current match */
121.1038 +    int best_len = s->prev_length;              /* best match length so far */
121.1039 +    int nice_match = s->nice_match;             /* stop if match long enough */
121.1040 +    IPos limit = s->strstart > (IPos)MAX_DIST(s) ?
121.1041 +        s->strstart - (IPos)MAX_DIST(s) : NIL;
121.1042 +    /* Stop when cur_match becomes <= limit. To simplify the code,
121.1043 +     * we prevent matches with the string of window index 0.
121.1044 +     */
121.1045 +    Posf *prev = s->prev;
121.1046 +    uInt wmask = s->w_mask;
121.1047 +
121.1048 +#ifdef UNALIGNED_OK
121.1049 +    /* Compare two bytes at a time. Note: this is not always beneficial.
121.1050 +     * Try with and without -DUNALIGNED_OK to check.
121.1051 +     */
121.1052 +    register Bytef *strend = s->window + s->strstart + MAX_MATCH - 1;
121.1053 +    register ush scan_start = *(ushf*)scan;
121.1054 +    register ush scan_end   = *(ushf*)(scan+best_len-1);
121.1055 +#else
121.1056 +    register Bytef *strend = s->window + s->strstart + MAX_MATCH;
121.1057 +    register Byte scan_end1  = scan[best_len-1];
121.1058 +    register Byte scan_end   = scan[best_len];
121.1059 +#endif
121.1060 +
121.1061 +    /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
121.1062 +     * It is easy to get rid of this optimization if necessary.
121.1063 +     */
121.1064 +    Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
121.1065 +
121.1066 +    /* Do not waste too much time if we already have a good match: */
121.1067 +    if (s->prev_length >= s->good_match) {
121.1068 +        chain_length >>= 2;
121.1069 +    }
121.1070 +    /* Do not look for matches beyond the end of the input. This is necessary
121.1071 +     * to make deflate deterministic.
121.1072 +     */
121.1073 +    if ((uInt)nice_match > s->lookahead) nice_match = s->lookahead;
121.1074 +
121.1075 +    Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead");
121.1076 +
121.1077 +    do {
121.1078 +        Assert(cur_match < s->strstart, "no future");
121.1079 +        match = s->window + cur_match;
121.1080 +
121.1081 +        /* Skip to next match if the match length cannot increase
121.1082 +         * or if the match length is less than 2.  Note that the checks below
121.1083 +         * for insufficient lookahead only occur occasionally for performance
121.1084 +         * reasons.  Therefore uninitialized memory will be accessed, and
121.1085 +         * conditional jumps will be made that depend on those values.
121.1086 +         * However the length of the match is limited to the lookahead, so
121.1087 +         * the output of deflate is not affected by the uninitialized values.
121.1088 +         */
121.1089 +#if (defined(UNALIGNED_OK) && MAX_MATCH == 258)
121.1090 +        /* This code assumes sizeof(unsigned short) == 2. Do not use
121.1091 +         * UNALIGNED_OK if your compiler uses a different size.
121.1092 +         */
121.1093 +        if (*(ushf*)(match+best_len-1) != scan_end ||
121.1094 +            *(ushf*)match != scan_start) continue;
121.1095 +
121.1096 +        /* It is not necessary to compare scan[2] and match[2] since they are
121.1097 +         * always equal when the other bytes match, given that the hash keys
121.1098 +         * are equal and that HASH_BITS >= 8. Compare 2 bytes at a time at
121.1099 +         * strstart+3, +5, ... up to strstart+257. We check for insufficient
121.1100 +         * lookahead only every 4th comparison; the 128th check will be made
121.1101 +         * at strstart+257. If MAX_MATCH-2 is not a multiple of 8, it is
121.1102 +         * necessary to put more guard bytes at the end of the window, or
121.1103 +         * to check more often for insufficient lookahead.
121.1104 +         */
121.1105 +        Assert(scan[2] == match[2], "scan[2]?");
121.1106 +        scan++, match++;
121.1107 +        do {
121.1108 +        } while (*(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
121.1109 +                 *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
121.1110 +                 *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
121.1111 +                 *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
121.1112 +                 scan < strend);
121.1113 +        /* The funny "do {}" generates better code on most compilers */
121.1114 +
121.1115 +        /* Here, scan <= window+strstart+257 */
121.1116 +        Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
121.1117 +        if (*scan == *match) scan++;
121.1118 +
121.1119 +        len = (MAX_MATCH - 1) - (int)(strend-scan);
121.1120 +        scan = strend - (MAX_MATCH-1);
121.1121 +
121.1122 +#else /* UNALIGNED_OK */
121.1123 +
121.1124 +        if (match[best_len]   != scan_end  ||
121.1125 +            match[best_len-1] != scan_end1 ||
121.1126 +            *match            != *scan     ||
121.1127 +            *++match          != scan[1])      continue;
121.1128 +
121.1129 +        /* The check at best_len-1 can be removed because it will be made
121.1130 +         * again later. (This heuristic is not always a win.)
121.1131 +         * It is not necessary to compare scan[2] and match[2] since they
121.1132 +         * are always equal when the other bytes match, given that
121.1133 +         * the hash keys are equal and that HASH_BITS >= 8.
121.1134 +         */
121.1135 +        scan += 2, match++;
121.1136 +        Assert(*scan == *match, "match[2]?");
121.1137 +
121.1138 +        /* We check for insufficient lookahead only every 8th comparison;
121.1139 +         * the 256th check will be made at strstart+258.
121.1140 +         */
121.1141 +        do {
121.1142 +        } while (*++scan == *++match && *++scan == *++match &&
121.1143 +                 *++scan == *++match && *++scan == *++match &&
121.1144 +                 *++scan == *++match && *++scan == *++match &&
121.1145 +                 *++scan == *++match && *++scan == *++match &&
121.1146 +                 scan < strend);
121.1147 +
121.1148 +        Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
121.1149 +
121.1150 +        len = MAX_MATCH - (int)(strend - scan);
121.1151 +        scan = strend - MAX_MATCH;
121.1152 +
121.1153 +#endif /* UNALIGNED_OK */
121.1154 +
121.1155 +        if (len > best_len) {
121.1156 +            s->match_start = cur_match;
121.1157 +            best_len = len;
121.1158 +            if (len >= nice_match) break;
121.1159 +#ifdef UNALIGNED_OK
121.1160 +            scan_end = *(ushf*)(scan+best_len-1);
121.1161 +#else
121.1162 +            scan_end1  = scan[best_len-1];
121.1163 +            scan_end   = scan[best_len];
121.1164 +#endif
121.1165 +        }
121.1166 +    } while ((cur_match = prev[cur_match & wmask]) > limit
121.1167 +             && --chain_length != 0);
121.1168 +
121.1169 +    if ((uInt)best_len <= s->lookahead) return (uInt)best_len;
121.1170 +    return s->lookahead;
121.1171 +}
121.1172 +#endif /* ASMV */
121.1173 +#endif /* FASTEST */
121.1174 +
121.1175 +/* ---------------------------------------------------------------------------
121.1176 + * Optimized version for level == 1 or strategy == Z_RLE only
121.1177 + */
121.1178 +local uInt longest_match_fast(s, cur_match)
121.1179 +    deflate_state *s;
121.1180 +    IPos cur_match;                             /* current match */
121.1181 +{
121.1182 +    register Bytef *scan = s->window + s->strstart; /* current string */
121.1183 +    register Bytef *match;                       /* matched string */
121.1184 +    register int len;                           /* length of current match */
121.1185 +    register Bytef *strend = s->window + s->strstart + MAX_MATCH;
121.1186 +
121.1187 +    /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
121.1188 +     * It is easy to get rid of this optimization if necessary.
121.1189 +     */
121.1190 +    Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
121.1191 +
121.1192 +    Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead");
121.1193 +
121.1194 +    Assert(cur_match < s->strstart, "no future");
121.1195 +
121.1196 +    match = s->window + cur_match;
121.1197 +
121.1198 +    /* Return failure if the match length is less than 2:
121.1199 +     */
121.1200 +    if (match[0] != scan[0] || match[1] != scan[1]) return MIN_MATCH-1;
121.1201 +
121.1202 +    /* The check at best_len-1 can be removed because it will be made
121.1203 +     * again later. (This heuristic is not always a win.)
121.1204 +     * It is not necessary to compare scan[2] and match[2] since they
121.1205 +     * are always equal when the other bytes match, given that
121.1206 +     * the hash keys are equal and that HASH_BITS >= 8.
121.1207 +     */
121.1208 +    scan += 2, match += 2;
121.1209 +    Assert(*scan == *match, "match[2]?");
121.1210 +
121.1211 +    /* We check for insufficient lookahead only every 8th comparison;
121.1212 +     * the 256th check will be made at strstart+258.
121.1213 +     */
121.1214 +    do {
121.1215 +    } while (*++scan == *++match && *++scan == *++match &&
121.1216 +             *++scan == *++match && *++scan == *++match &&
121.1217 +             *++scan == *++match && *++scan == *++match &&
121.1218 +             *++scan == *++match && *++scan == *++match &&
121.1219 +             scan < strend);
121.1220 +
121.1221 +    Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
121.1222 +
121.1223 +    len = MAX_MATCH - (int)(strend - scan);
121.1224 +
121.1225 +    if (len < MIN_MATCH) return MIN_MATCH - 1;
121.1226 +
121.1227 +    s->match_start = cur_match;
121.1228 +    return (uInt)len <= s->lookahead ? (uInt)len : s->lookahead;
121.1229 +}
121.1230 +
121.1231 +#ifdef DEBUG
121.1232 +/* ===========================================================================
121.1233 + * Check that the match at match_start is indeed a match.
121.1234 + */
121.1235 +local void check_match(s, start, match, length)
121.1236 +    deflate_state *s;
121.1237 +    IPos start, match;
121.1238 +    int length;
121.1239 +{
121.1240 +    /* check that the match is indeed a match */
121.1241 +    if (zmemcmp(s->window + match,
121.1242 +                s->window + start, length) != EQUAL) {
121.1243 +        fprintf(stderr, " start %u, match %u, length %d\n",
121.1244 +                start, match, length);
121.1245 +        do {
121.1246 +            fprintf(stderr, "%c%c", s->window[match++], s->window[start++]);
121.1247 +        } while (--length != 0);
121.1248 +        z_error("invalid match");
121.1249 +    }
121.1250 +    if (z_verbose > 1) {
121.1251 +        fprintf(stderr,"\\[%d,%d]", start-match, length);
121.1252 +        do { putc(s->window[start++], stderr); } while (--length != 0);
121.1253 +    }
121.1254 +}
121.1255 +#else
121.1256 +#  define check_match(s, start, match, length)
121.1257 +#endif /* DEBUG */
121.1258 +
121.1259 +/* ===========================================================================
121.1260 + * Fill the window when the lookahead becomes insufficient.
121.1261 + * Updates strstart and lookahead.
121.1262 + *
121.1263 + * IN assertion: lookahead < MIN_LOOKAHEAD
121.1264 + * OUT assertions: strstart <= window_size-MIN_LOOKAHEAD
121.1265 + *    At least one byte has been read, or avail_in == 0; reads are
121.1266 + *    performed for at least two bytes (required for the zip translate_eol
121.1267 + *    option -- not supported here).
121.1268 + */
121.1269 +local void fill_window(s)
121.1270 +    deflate_state *s;
121.1271 +{
121.1272 +    register unsigned n, m;
121.1273 +    register Posf *p;
121.1274 +    unsigned more;    /* Amount of free space at the end of the window. */
121.1275 +    uInt wsize = s->w_size;
121.1276 +
121.1277 +    do {
121.1278 +        more = (unsigned)(s->window_size -(ulg)s->lookahead -(ulg)s->strstart);
121.1279 +
121.1280 +        /* Deal with !@#$% 64K limit: */
121.1281 +        if (sizeof(int) <= 2) {
121.1282 +            if (more == 0 && s->strstart == 0 && s->lookahead == 0) {
121.1283 +                more = wsize;
121.1284 +
121.1285 +            } else if (more == (unsigned)(-1)) {
121.1286 +                /* Very unlikely, but possible on 16 bit machine if
121.1287 +                 * strstart == 0 && lookahead == 1 (input done a byte at time)
121.1288 +                 */
121.1289 +                more--;
121.1290 +            }
121.1291 +        }
121.1292 +
121.1293 +        /* If the window is almost full and there is insufficient lookahead,
121.1294 +         * move the upper half to the lower one to make room in the upper half.
121.1295 +         */
121.1296 +        if (s->strstart >= wsize+MAX_DIST(s)) {
121.1297 +
121.1298 +            zmemcpy(s->window, s->window+wsize, (unsigned)wsize);
121.1299 +            s->match_start -= wsize;
121.1300 +            s->strstart    -= wsize; /* we now have strstart >= MAX_DIST */
121.1301 +            s->block_start -= (long) wsize;
121.1302 +
121.1303 +            /* Slide the hash table (could be avoided with 32 bit values
121.1304 +               at the expense of memory usage). We slide even when level == 0
121.1305 +               to keep the hash table consistent if we switch back to level > 0
121.1306 +               later. (Using level 0 permanently is not an optimal usage of
121.1307 +               zlib, so we don't care about this pathological case.)
121.1308 +             */
121.1309 +            /* %%% avoid this when Z_RLE */
121.1310 +            n = s->hash_size;
121.1311 +            p = &s->head[n];
121.1312 +            do {
121.1313 +                m = *--p;
121.1314 +                *p = (Pos)(m >= wsize ? m-wsize : NIL);
121.1315 +            } while (--n);
121.1316 +
121.1317 +            n = wsize;
121.1318 +#ifndef FASTEST
121.1319 +            p = &s->prev[n];
121.1320 +            do {
121.1321 +                m = *--p;
121.1322 +                *p = (Pos)(m >= wsize ? m-wsize : NIL);
121.1323 +                /* If n is not on any hash chain, prev[n] is garbage but
121.1324 +                 * its value will never be used.
121.1325 +                 */
121.1326 +            } while (--n);
121.1327 +#endif
121.1328 +            more += wsize;
121.1329 +        }
121.1330 +        if (s->strm->avail_in == 0) return;
121.1331 +
121.1332 +        /* If there was no sliding:
121.1333 +         *    strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 &&
121.1334 +         *    more == window_size - lookahead - strstart
121.1335 +         * => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1)
121.1336 +         * => more >= window_size - 2*WSIZE + 2
121.1337 +         * In the BIG_MEM or MMAP case (not yet supported),
121.1338 +         *   window_size == input_size + MIN_LOOKAHEAD  &&
121.1339 +         *   strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD.
121.1340 +         * Otherwise, window_size == 2*WSIZE so more >= 2.
121.1341 +         * If there was sliding, more >= WSIZE. So in all cases, more >= 2.
121.1342 +         */
121.1343 +        Assert(more >= 2, "more < 2");
121.1344 +
121.1345 +        n = read_buf(s->strm, s->window + s->strstart + s->lookahead, more);
121.1346 +        s->lookahead += n;
121.1347 +
121.1348 +        /* Initialize the hash value now that we have some input: */
121.1349 +        if (s->lookahead >= MIN_MATCH) {
121.1350 +            s->ins_h = s->window[s->strstart];
121.1351 +            UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]);
121.1352 +#if MIN_MATCH != 3
121.1353 +            Call UPDATE_HASH() MIN_MATCH-3 more times
121.1354 +#endif
121.1355 +        }
121.1356 +        /* If the whole input has less than MIN_MATCH bytes, ins_h is garbage,
121.1357 +         * but this is not important since only literal bytes will be emitted.
121.1358 +         */
121.1359 +
121.1360 +    } while (s->lookahead < MIN_LOOKAHEAD && s->strm->avail_in != 0);
121.1361 +}
121.1362 +
121.1363 +/* ===========================================================================
121.1364 + * Flush the current block, with given end-of-file flag.
121.1365 + * IN assertion: strstart is set to the end of the current match.
121.1366 + */
121.1367 +#define FLUSH_BLOCK_ONLY(s, eof) { \
121.1368 +   _tr_flush_block(s, (s->block_start >= 0L ? \
121.1369 +                   (charf *)&s->window[(unsigned)s->block_start] : \
121.1370 +                   (charf *)Z_NULL), \
121.1371 +                (ulg)((long)s->strstart - s->block_start), \
121.1372 +                (eof)); \
121.1373 +   s->block_start = s->strstart; \
121.1374 +   flush_pending(s->strm); \
121.1375 +   Tracev((stderr,"[FLUSH]")); \
121.1376 +}
121.1377 +
121.1378 +/* Same but force premature exit if necessary. */
121.1379 +#define FLUSH_BLOCK(s, eof) { \
121.1380 +   FLUSH_BLOCK_ONLY(s, eof); \
121.1381 +   if (s->strm->avail_out == 0) return (eof) ? finish_started : need_more; \
121.1382 +}
121.1383 +
121.1384 +/* ===========================================================================
121.1385 + * Copy without compression as much as possible from the input stream, return
121.1386 + * the current block state.
121.1387 + * This function does not insert new strings in the dictionary since
121.1388 + * uncompressible data is probably not useful. This function is used
121.1389 + * only for the level=0 compression option.
121.1390 + * NOTE: this function should be optimized to avoid extra copying from
121.1391 + * window to pending_buf.
121.1392 + */
121.1393 +local block_state deflate_stored(s, flush)
121.1394 +    deflate_state *s;
121.1395 +    int flush;
121.1396 +{
121.1397 +    /* Stored blocks are limited to 0xffff bytes, pending_buf is limited
121.1398 +     * to pending_buf_size, and each stored block has a 5 byte header:
121.1399 +     */
121.1400 +    ulg max_block_size = 0xffff;
121.1401 +    ulg max_start;
121.1402 +
121.1403 +    if (max_block_size > s->pending_buf_size - 5) {
121.1404 +        max_block_size = s->pending_buf_size - 5;
121.1405 +    }
121.1406 +
121.1407 +    /* Copy as much as possible from input to output: */
121.1408 +    for (;;) {
121.1409 +        /* Fill the window as much as possible: */
121.1410 +        if (s->lookahead <= 1) {
121.1411 +
121.1412 +            Assert(s->strstart < s->w_size+MAX_DIST(s) ||
121.1413 +                   s->block_start >= (long)s->w_size, "slide too late");
121.1414 +
121.1415 +            fill_window(s);
121.1416 +            if (s->lookahead == 0 && flush == Z_NO_FLUSH) return need_more;
121.1417 +
121.1418 +            if (s->lookahead == 0) break; /* flush the current block */
121.1419 +        }
121.1420 +        Assert(s->block_start >= 0L, "block gone");
121.1421 +
121.1422 +        s->strstart += s->lookahead;
121.1423 +        s->lookahead = 0;
121.1424 +
121.1425 +        /* Emit a stored block if pending_buf will be full: */
121.1426 +        max_start = s->block_start + max_block_size;
121.1427 +        if (s->strstart == 0 || (ulg)s->strstart >= max_start) {
121.1428 +            /* strstart == 0 is possible when wraparound on 16-bit machine */
121.1429 +            s->lookahead = (uInt)(s->strstart - max_start);
121.1430 +            s->strstart = (uInt)max_start;
121.1431 +            FLUSH_BLOCK(s, 0);
121.1432 +        }
121.1433 +        /* Flush if we may have to slide, otherwise block_start may become
121.1434 +         * negative and the data will be gone:
121.1435 +         */
121.1436 +        if (s->strstart - (uInt)s->block_start >= MAX_DIST(s)) {
121.1437 +            FLUSH_BLOCK(s, 0);
121.1438 +        }
121.1439 +    }
121.1440 +    FLUSH_BLOCK(s, flush == Z_FINISH);
121.1441 +    return flush == Z_FINISH ? finish_done : block_done;
121.1442 +}
121.1443 +
121.1444 +/* ===========================================================================
121.1445 + * Compress as much as possible from the input stream, return the current
121.1446 + * block state.
121.1447 + * This function does not perform lazy evaluation of matches and inserts
121.1448 + * new strings in the dictionary only for unmatched strings or for short
121.1449 + * matches. It is used only for the fast compression options.
121.1450 + */
121.1451 +local block_state deflate_fast(s, flush)
121.1452 +    deflate_state *s;
121.1453 +    int flush;
121.1454 +{
121.1455 +    IPos hash_head = NIL; /* head of the hash chain */
121.1456 +    int bflush;           /* set if current block must be flushed */
121.1457 +
121.1458 +    for (;;) {
121.1459 +        /* Make sure that we always have enough lookahead, except
121.1460 +         * at the end of the input file. We need MAX_MATCH bytes
121.1461 +         * for the next match, plus MIN_MATCH bytes to insert the
121.1462 +         * string following the next match.
121.1463 +         */
121.1464 +        if (s->lookahead < MIN_LOOKAHEAD) {
121.1465 +            fill_window(s);
121.1466 +            if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
121.1467 +                return need_more;
121.1468 +            }
121.1469 +            if (s->lookahead == 0) break; /* flush the current block */
121.1470 +        }
121.1471 +
121.1472 +        /* Insert the string window[strstart .. strstart+2] in the
121.1473 +         * dictionary, and set hash_head to the head of the hash chain:
121.1474 +         */
121.1475 +        if (s->lookahead >= MIN_MATCH) {
121.1476 +            INSERT_STRING(s, s->strstart, hash_head);
121.1477 +        }
121.1478 +
121.1479 +        /* Find the longest match, discarding those <= prev_length.
121.1480 +         * At this point we have always match_length < MIN_MATCH
121.1481 +         */
121.1482 +        if (hash_head != NIL && s->strstart - hash_head <= MAX_DIST(s)) {
121.1483 +            /* To simplify the code, we prevent matches with the string
121.1484 +             * of window index 0 (in particular we have to avoid a match
121.1485 +             * of the string with itself at the start of the input file).
121.1486 +             */
121.1487 +#ifdef FASTEST
121.1488 +            if ((s->strategy != Z_HUFFMAN_ONLY && s->strategy != Z_RLE) ||
121.1489 +                (s->strategy == Z_RLE && s->strstart - hash_head == 1)) {
121.1490 +                s->match_length = longest_match_fast (s, hash_head);
121.1491 +            }
121.1492 +#else
121.1493 +            if (s->strategy != Z_HUFFMAN_ONLY && s->strategy != Z_RLE) {
121.1494 +                s->match_length = longest_match (s, hash_head);
121.1495 +            } else if (s->strategy == Z_RLE && s->strstart - hash_head == 1) {
121.1496 +                s->match_length = longest_match_fast (s, hash_head);
121.1497 +            }
121.1498 +#endif
121.1499 +            /* longest_match() or longest_match_fast() sets match_start */
121.1500 +        }
121.1501 +        if (s->match_length >= MIN_MATCH) {
121.1502 +            check_match(s, s->strstart, s->match_start, s->match_length);
121.1503 +
121.1504 +            _tr_tally_dist(s, s->strstart - s->match_start,
121.1505 +                           s->match_length - MIN_MATCH, bflush);
121.1506 +
121.1507 +            s->lookahead -= s->match_length;
121.1508 +
121.1509 +            /* Insert new strings in the hash table only if the match length
121.1510 +             * is not too large. This saves time but degrades compression.
121.1511 +             */
121.1512 +#ifndef FASTEST
121.1513 +            if (s->match_length <= s->max_insert_length &&
121.1514 +                s->lookahead >= MIN_MATCH) {
121.1515 +                s->match_length--; /* string at strstart already in table */
121.1516 +                do {
121.1517 +                    s->strstart++;
121.1518 +                    INSERT_STRING(s, s->strstart, hash_head);
121.1519 +                    /* strstart never exceeds WSIZE-MAX_MATCH, so there are
121.1520 +                     * always MIN_MATCH bytes ahead.
121.1521 +                     */
121.1522 +                } while (--s->match_length != 0);
121.1523 +                s->strstart++;
121.1524 +            } else
121.1525 +#endif
121.1526 +            {
121.1527 +                s->strstart += s->match_length;
121.1528 +                s->match_length = 0;
121.1529 +                s->ins_h = s->window[s->strstart];
121.1530 +                UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]);
121.1531 +#if MIN_MATCH != 3
121.1532 +                Call UPDATE_HASH() MIN_MATCH-3 more times
121.1533 +#endif
121.1534 +                /* If lookahead < MIN_MATCH, ins_h is garbage, but it does not
121.1535 +                 * matter since it will be recomputed at next deflate call.
121.1536 +                 */
121.1537 +            }
121.1538 +        } else {
121.1539 +            /* No match, output a literal byte */
121.1540 +            Tracevv((stderr,"%c", s->window[s->strstart]));
121.1541 +            _tr_tally_lit (s, s->window[s->strstart], bflush);
121.1542 +            s->lookahead--;
121.1543 +            s->strstart++;
121.1544 +        }
121.1545 +        if (bflush) FLUSH_BLOCK(s, 0);
121.1546 +    }
121.1547 +    FLUSH_BLOCK(s, flush == Z_FINISH);
121.1548 +    return flush == Z_FINISH ? finish_done : block_done;
121.1549 +}
121.1550 +
121.1551 +#ifndef FASTEST
121.1552 +/* ===========================================================================
121.1553 + * Same as above, but achieves better compression. We use a lazy
121.1554 + * evaluation for matches: a match is finally adopted only if there is
121.1555 + * no better match at the next window position.
121.1556 + */
121.1557 +local block_state deflate_slow(s, flush)
121.1558 +    deflate_state *s;
121.1559 +    int flush;
121.1560 +{
121.1561 +    IPos hash_head = NIL;    /* head of hash chain */
121.1562 +    int bflush;              /* set if current block must be flushed */
121.1563 +
121.1564 +    /* Process the input block. */
121.1565 +    for (;;) {
121.1566 +        /* Make sure that we always have enough lookahead, except
121.1567 +         * at the end of the input file. We need MAX_MATCH bytes
121.1568 +         * for the next match, plus MIN_MATCH bytes to insert the
121.1569 +         * string following the next match.
121.1570 +         */
121.1571 +        if (s->lookahead < MIN_LOOKAHEAD) {
121.1572 +            fill_window(s);
121.1573 +            if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
121.1574 +                return need_more;
121.1575 +            }
121.1576 +            if (s->lookahead == 0) break; /* flush the current block */
121.1577 +        }
121.1578 +
121.1579 +        /* Insert the string window[strstart .. strstart+2] in the
121.1580 +         * dictionary, and set hash_head to the head of the hash chain:
121.1581 +         */
121.1582 +        if (s->lookahead >= MIN_MATCH) {
121.1583 +            INSERT_STRING(s, s->strstart, hash_head);
121.1584 +        }
121.1585 +
121.1586 +        /* Find the longest match, discarding those <= prev_length.
121.1587 +         */
121.1588 +        s->prev_length = s->match_length, s->prev_match = s->match_start;
121.1589 +        s->match_length = MIN_MATCH-1;
121.1590 +
121.1591 +        if (hash_head != NIL && s->prev_length < s->max_lazy_match &&
121.1592 +            s->strstart - hash_head <= MAX_DIST(s)) {
121.1593 +            /* To simplify the code, we prevent matches with the string
121.1594 +             * of window index 0 (in particular we have to avoid a match
121.1595 +             * of the string with itself at the start of the input file).
121.1596 +             */
121.1597 +            if (s->strategy != Z_HUFFMAN_ONLY && s->strategy != Z_RLE) {
121.1598 +                s->match_length = longest_match (s, hash_head);
121.1599 +            } else if (s->strategy == Z_RLE && s->strstart - hash_head == 1) {
121.1600 +                s->match_length = longest_match_fast (s, hash_head);
121.1601 +            }
121.1602 +            /* longest_match() or longest_match_fast() sets match_start */
121.1603 +
121.1604 +            if (s->match_length <= 5 && (s->strategy == Z_FILTERED
121.1605 +#if TOO_FAR <= 32767
121.1606 +                || (s->match_length == MIN_MATCH &&
121.1607 +                    s->strstart - s->match_start > TOO_FAR)
121.1608 +#endif
121.1609 +                )) {
121.1610 +
121.1611 +                /* If prev_match is also MIN_MATCH, match_start is garbage
121.1612 +                 * but we will ignore the current match anyway.
121.1613 +                 */
121.1614 +                s->match_length = MIN_MATCH-1;
121.1615 +            }
121.1616 +        }
121.1617 +        /* If there was a match at the previous step and the current
121.1618 +         * match is not better, output the previous match:
121.1619 +         */
121.1620 +        if (s->prev_length >= MIN_MATCH && s->match_length <= s->prev_length) {
121.1621 +            uInt max_insert = s->strstart + s->lookahead - MIN_MATCH;
121.1622 +            /* Do not insert strings in hash table beyond this. */
121.1623 +
121.1624 +            check_match(s, s->strstart-1, s->prev_match, s->prev_length);
121.1625 +
121.1626 +            _tr_tally_dist(s, s->strstart -1 - s->prev_match,
121.1627 +                           s->prev_length - MIN_MATCH, bflush);
121.1628 +
121.1629 +            /* Insert in hash table all strings up to the end of the match.
121.1630 +             * strstart-1 and strstart are already inserted. If there is not
121.1631 +             * enough lookahead, the last two strings are not inserted in
121.1632 +             * the hash table.
121.1633 +             */
121.1634 +            s->lookahead -= s->prev_length-1;
121.1635 +            s->prev_length -= 2;
121.1636 +            do {
121.1637 +                if (++s->strstart <= max_insert) {
121.1638 +                    INSERT_STRING(s, s->strstart, hash_head);
121.1639 +                }
121.1640 +            } while (--s->prev_length != 0);
121.1641 +            s->match_available = 0;
121.1642 +            s->match_length = MIN_MATCH-1;
121.1643 +            s->strstart++;
121.1644 +
121.1645 +            if (bflush) FLUSH_BLOCK(s, 0);
121.1646 +
121.1647 +        } else if (s->match_available) {
121.1648 +            /* If there was no match at the previous position, output a
121.1649 +             * single literal. If there was a match but the current match
121.1650 +             * is longer, truncate the previous match to a single literal.
121.1651 +             */
121.1652 +            Tracevv((stderr,"%c", s->window[s->strstart-1]));
121.1653 +            _tr_tally_lit(s, s->window[s->strstart-1], bflush);
121.1654 +            if (bflush) {
121.1655 +                FLUSH_BLOCK_ONLY(s, 0);
121.1656 +            }
121.1657 +            s->strstart++;
121.1658 +            s->lookahead--;
121.1659 +            if (s->strm->avail_out == 0) return need_more;
121.1660 +        } else {
121.1661 +            /* There is no previous match to compare with, wait for
121.1662 +             * the next step to decide.
121.1663 +             */
121.1664 +            s->match_available = 1;
121.1665 +            s->strstart++;
121.1666 +            s->lookahead--;
121.1667 +        }
121.1668 +    }
121.1669 +    Assert (flush != Z_NO_FLUSH, "no flush?");
121.1670 +    if (s->match_available) {
121.1671 +        Tracevv((stderr,"%c", s->window[s->strstart-1]));
121.1672 +        _tr_tally_lit(s, s->window[s->strstart-1], bflush);
121.1673 +        s->match_available = 0;
121.1674 +    }
121.1675 +    FLUSH_BLOCK(s, flush == Z_FINISH);
121.1676 +    return flush == Z_FINISH ? finish_done : block_done;
121.1677 +}
121.1678 +#endif /* FASTEST */
121.1679 +
121.1680 +#if 0
121.1681 +/* ===========================================================================
121.1682 + * For Z_RLE, simply look for runs of bytes, generate matches only of distance
121.1683 + * one.  Do not maintain a hash table.  (It will be regenerated if this run of
121.1684 + * deflate switches away from Z_RLE.)
121.1685 + */
121.1686 +local block_state deflate_rle(s, flush)
121.1687 +    deflate_state *s;
121.1688 +    int flush;
121.1689 +{
121.1690 +    int bflush;         /* set if current block must be flushed */
121.1691 +    uInt run;           /* length of run */
121.1692 +    uInt max;           /* maximum length of run */
121.1693 +    uInt prev;          /* byte at distance one to match */
121.1694 +    Bytef *scan;        /* scan for end of run */
121.1695 +
121.1696 +    for (;;) {
121.1697 +        /* Make sure that we always have enough lookahead, except
121.1698 +         * at the end of the input file. We need MAX_MATCH bytes
121.1699 +         * for the longest encodable run.
121.1700 +         */
121.1701 +        if (s->lookahead < MAX_MATCH) {
121.1702 +            fill_window(s);
121.1703 +            if (s->lookahead < MAX_MATCH && flush == Z_NO_FLUSH) {
121.1704 +                return need_more;
121.1705 +            }
121.1706 +            if (s->lookahead == 0) break; /* flush the current block */
121.1707 +        }
121.1708 +
121.1709 +        /* See how many times the previous byte repeats */
121.1710 +        run = 0;
121.1711 +        if (s->strstart > 0) {      /* if there is a previous byte, that is */
121.1712 +            max = s->lookahead < MAX_MATCH ? s->lookahead : MAX_MATCH;
121.1713 +            scan = s->window + s->strstart - 1;
121.1714 +            prev = *scan++;
121.1715 +            do {
121.1716 +                if (*scan++ != prev)
121.1717 +                    break;
121.1718 +            } while (++run < max);
121.1719 +        }
121.1720 +
121.1721 +        /* Emit match if have run of MIN_MATCH or longer, else emit literal */
121.1722 +        if (run >= MIN_MATCH) {
121.1723 +            check_match(s, s->strstart, s->strstart - 1, run);
121.1724 +            _tr_tally_dist(s, 1, run - MIN_MATCH, bflush);
121.1725 +            s->lookahead -= run;
121.1726 +            s->strstart += run;
121.1727 +        } else {
121.1728 +            /* No match, output a literal byte */
121.1729 +            Tracevv((stderr,"%c", s->window[s->strstart]));
121.1730 +            _tr_tally_lit (s, s->window[s->strstart], bflush);
121.1731 +            s->lookahead--;
121.1732 +            s->strstart++;
121.1733 +        }
121.1734 +        if (bflush) FLUSH_BLOCK(s, 0);
121.1735 +    }
121.1736 +    FLUSH_BLOCK(s, flush == Z_FINISH);
121.1737 +    return flush == Z_FINISH ? finish_done : block_done;
121.1738 +}
121.1739 +#endif
   122.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   122.2 +++ b/libs/zlib/deflate.h	Sat Sep 19 05:51:51 2015 +0300
   122.3 @@ -0,0 +1,331 @@
   122.4 +/* deflate.h -- internal compression state
   122.5 + * Copyright (C) 1995-2004 Jean-loup Gailly
   122.6 + * For conditions of distribution and use, see copyright notice in zlib.h
   122.7 + */
   122.8 +
   122.9 +/* WARNING: this file should *not* be used by applications. It is
  122.10 +   part of the implementation of the compression library and is
  122.11 +   subject to change. Applications should only use zlib.h.
  122.12 + */
  122.13 +
  122.14 +/* @(#) $Id$ */
  122.15 +
  122.16 +#ifndef DEFLATE_H
  122.17 +#define DEFLATE_H
  122.18 +
  122.19 +#include "zutil.h"
  122.20 +
  122.21 +/* define NO_GZIP when compiling if you want to disable gzip header and
  122.22 +   trailer creation by deflate().  NO_GZIP would be used to avoid linking in
  122.23 +   the crc code when it is not needed.  For shared libraries, gzip encoding
  122.24 +   should be left enabled. */
  122.25 +#ifndef NO_GZIP
  122.26 +#  define GZIP
  122.27 +#endif
  122.28 +
  122.29 +/* ===========================================================================
  122.30 + * Internal compression state.
  122.31 + */
  122.32 +
  122.33 +#define LENGTH_CODES 29
  122.34 +/* number of length codes, not counting the special END_BLOCK code */
  122.35 +
  122.36 +#define LITERALS  256
  122.37 +/* number of literal bytes 0..255 */
  122.38 +
  122.39 +#define L_CODES (LITERALS+1+LENGTH_CODES)
  122.40 +/* number of Literal or Length codes, including the END_BLOCK code */
  122.41 +
  122.42 +#define D_CODES   30
  122.43 +/* number of distance codes */
  122.44 +
  122.45 +#define BL_CODES  19
  122.46 +/* number of codes used to transfer the bit lengths */
  122.47 +
  122.48 +#define HEAP_SIZE (2*L_CODES+1)
  122.49 +/* maximum heap size */
  122.50 +
  122.51 +#define MAX_BITS 15
  122.52 +/* All codes must not exceed MAX_BITS bits */
  122.53 +
  122.54 +#define INIT_STATE    42
  122.55 +#define EXTRA_STATE   69
  122.56 +#define NAME_STATE    73
  122.57 +#define COMMENT_STATE 91
  122.58 +#define HCRC_STATE   103
  122.59 +#define BUSY_STATE   113
  122.60 +#define FINISH_STATE 666
  122.61 +/* Stream status */
  122.62 +
  122.63 +
  122.64 +/* Data structure describing a single value and its code string. */
  122.65 +typedef struct ct_data_s {
  122.66 +    union {
  122.67 +        ush  freq;       /* frequency count */
  122.68 +        ush  code;       /* bit string */
  122.69 +    } fc;
  122.70 +    union {
  122.71 +        ush  dad;        /* father node in Huffman tree */
  122.72 +        ush  len;        /* length of bit string */
  122.73 +    } dl;
  122.74 +} FAR ct_data;
  122.75 +
  122.76 +#define Freq fc.freq
  122.77 +#define Code fc.code
  122.78 +#define Dad  dl.dad
  122.79 +#define Len  dl.len
  122.80 +
  122.81 +typedef struct static_tree_desc_s  static_tree_desc;
  122.82 +
  122.83 +typedef struct tree_desc_s {
  122.84 +    ct_data *dyn_tree;           /* the dynamic tree */
  122.85 +    int     max_code;            /* largest code with non zero frequency */
  122.86 +    static_tree_desc *stat_desc; /* the corresponding static tree */
  122.87 +} FAR tree_desc;
  122.88 +
  122.89 +typedef ush Pos;
  122.90 +typedef Pos FAR Posf;
  122.91 +typedef unsigned IPos;
  122.92 +
  122.93 +/* A Pos is an index in the character window. We use short instead of int to
  122.94 + * save space in the various tables. IPos is used only for parameter passing.
  122.95 + */
  122.96 +
  122.97 +typedef struct internal_state {
  122.98 +    z_streamp strm;      /* pointer back to this zlib stream */
  122.99 +    int   status;        /* as the name implies */
 122.100 +    Bytef *pending_buf;  /* output still pending */
 122.101 +    ulg   pending_buf_size; /* size of pending_buf */
 122.102 +    Bytef *pending_out;  /* next pending byte to output to the stream */
 122.103 +    uInt   pending;      /* nb of bytes in the pending buffer */
 122.104 +    int   wrap;          /* bit 0 true for zlib, bit 1 true for gzip */
 122.105 +    gz_headerp  gzhead;  /* gzip header information to write */
 122.106 +    uInt   gzindex;      /* where in extra, name, or comment */
 122.107 +    Byte  method;        /* STORED (for zip only) or DEFLATED */
 122.108 +    int   last_flush;    /* value of flush param for previous deflate call */
 122.109 +
 122.110 +                /* used by deflate.c: */
 122.111 +
 122.112 +    uInt  w_size;        /* LZ77 window size (32K by default) */
 122.113 +    uInt  w_bits;        /* log2(w_size)  (8..16) */
 122.114 +    uInt  w_mask;        /* w_size - 1 */
 122.115 +
 122.116 +    Bytef *window;
 122.117 +    /* Sliding window. Input bytes are read into the second half of the window,
 122.118 +     * and move to the first half later to keep a dictionary of at least wSize
 122.119 +     * bytes. With this organization, matches are limited to a distance of
 122.120 +     * wSize-MAX_MATCH bytes, but this ensures that IO is always
 122.121 +     * performed with a length multiple of the block size. Also, it limits
 122.122 +     * the window size to 64K, which is quite useful on MSDOS.
 122.123 +     * To do: use the user input buffer as sliding window.
 122.124 +     */
 122.125 +
 122.126 +    ulg window_size;
 122.127 +    /* Actual size of window: 2*wSize, except when the user input buffer
 122.128 +     * is directly used as sliding window.
 122.129 +     */
 122.130 +
 122.131 +    Posf *prev;
 122.132 +    /* Link to older string with same hash index. To limit the size of this
 122.133 +     * array to 64K, this link is maintained only for the last 32K strings.
 122.134 +     * An index in this array is thus a window index modulo 32K.
 122.135 +     */
 122.136 +
 122.137 +    Posf *head; /* Heads of the hash chains or NIL. */
 122.138 +
 122.139 +    uInt  ins_h;          /* hash index of string to be inserted */
 122.140 +    uInt  hash_size;      /* number of elements in hash table */
 122.141 +    uInt  hash_bits;      /* log2(hash_size) */
 122.142 +    uInt  hash_mask;      /* hash_size-1 */
 122.143 +
 122.144 +    uInt  hash_shift;
 122.145 +    /* Number of bits by which ins_h must be shifted at each input
 122.146 +     * step. It must be such that after MIN_MATCH steps, the oldest
 122.147 +     * byte no longer takes part in the hash key, that is:
 122.148 +     *   hash_shift * MIN_MATCH >= hash_bits
 122.149 +     */
 122.150 +
 122.151 +    long block_start;
 122.152 +    /* Window position at the beginning of the current output block. Gets
 122.153 +     * negative when the window is moved backwards.
 122.154 +     */
 122.155 +
 122.156 +    uInt match_length;           /* length of best match */
 122.157 +    IPos prev_match;             /* previous match */
 122.158 +    int match_available;         /* set if previous match exists */
 122.159 +    uInt strstart;               /* start of string to insert */
 122.160 +    uInt match_start;            /* start of matching string */
 122.161 +    uInt lookahead;              /* number of valid bytes ahead in window */
 122.162 +
 122.163 +    uInt prev_length;
 122.164 +    /* Length of the best match at previous step. Matches not greater than this
 122.165 +     * are discarded. This is used in the lazy match evaluation.
 122.166 +     */
 122.167 +
 122.168 +    uInt max_chain_length;
 122.169 +    /* To speed up deflation, hash chains are never searched beyond this
 122.170 +     * length.  A higher limit improves compression ratio but degrades the
 122.171 +     * speed.
 122.172 +     */
 122.173 +
 122.174 +    uInt max_lazy_match;
 122.175 +    /* Attempt to find a better match only when the current match is strictly
 122.176 +     * smaller than this value. This mechanism is used only for compression
 122.177 +     * levels >= 4.
 122.178 +     */
 122.179 +#   define max_insert_length  max_lazy_match
 122.180 +    /* Insert new strings in the hash table only if the match length is not
 122.181 +     * greater than this length. This saves time but degrades compression.
 122.182 +     * max_insert_length is used only for compression levels <= 3.
 122.183 +     */
 122.184 +
 122.185 +    int level;    /* compression level (1..9) */
 122.186 +    int strategy; /* favor or force Huffman coding*/
 122.187 +
 122.188 +    uInt good_match;
 122.189 +    /* Use a faster search when the previous match is longer than this */
 122.190 +
 122.191 +    int nice_match; /* Stop searching when current match exceeds this */
 122.192 +
 122.193 +                /* used by trees.c: */
 122.194 +    /* Didn't use ct_data typedef below to supress compiler warning */
 122.195 +    struct ct_data_s dyn_ltree[HEAP_SIZE];   /* literal and length tree */
 122.196 +    struct ct_data_s dyn_dtree[2*D_CODES+1]; /* distance tree */
 122.197 +    struct ct_data_s bl_tree[2*BL_CODES+1];  /* Huffman tree for bit lengths */
 122.198 +
 122.199 +    struct tree_desc_s l_desc;               /* desc. for literal tree */
 122.200 +    struct tree_desc_s d_desc;               /* desc. for distance tree */
 122.201 +    struct tree_desc_s bl_desc;              /* desc. for bit length tree */
 122.202 +
 122.203 +    ush bl_count[MAX_BITS+1];
 122.204 +    /* number of codes at each bit length for an optimal tree */
 122.205 +
 122.206 +    int heap[2*L_CODES+1];      /* heap used to build the Huffman trees */
 122.207 +    int heap_len;               /* number of elements in the heap */
 122.208 +    int heap_max;               /* element of largest frequency */
 122.209 +    /* The sons of heap[n] are heap[2*n] and heap[2*n+1]. heap[0] is not used.
 122.210 +     * The same heap array is used to build all trees.
 122.211 +     */
 122.212 +
 122.213 +    uch depth[2*L_CODES+1];
 122.214 +    /* Depth of each subtree used as tie breaker for trees of equal frequency
 122.215 +     */
 122.216 +
 122.217 +    uchf *l_buf;          /* buffer for literals or lengths */
 122.218 +
 122.219 +    uInt  lit_bufsize;
 122.220 +    /* Size of match buffer for literals/lengths.  There are 4 reasons for
 122.221 +     * limiting lit_bufsize to 64K:
 122.222 +     *   - frequencies can be kept in 16 bit counters
 122.223 +     *   - if compression is not successful for the first block, all input
 122.224 +     *     data is still in the window so we can still emit a stored block even
 122.225 +     *     when input comes from standard input.  (This can also be done for
 122.226 +     *     all blocks if lit_bufsize is not greater than 32K.)
 122.227 +     *   - if compression is not successful for a file smaller than 64K, we can
 122.228 +     *     even emit a stored file instead of a stored block (saving 5 bytes).
 122.229 +     *     This is applicable only for zip (not gzip or zlib).
 122.230 +     *   - creating new Huffman trees less frequently may not provide fast
 122.231 +     *     adaptation to changes in the input data statistics. (Take for
 122.232 +     *     example a binary file with poorly compressible code followed by
 122.233 +     *     a highly compressible string table.) Smaller buffer sizes give
 122.234 +     *     fast adaptation but have of course the overhead of transmitting
 122.235 +     *     trees more frequently.
 122.236 +     *   - I can't count above 4
 122.237 +     */
 122.238 +
 122.239 +    uInt last_lit;      /* running index in l_buf */
 122.240 +
 122.241 +    ushf *d_buf;
 122.242 +    /* Buffer for distances. To simplify the code, d_buf and l_buf have
 122.243 +     * the same number of elements. To use different lengths, an extra flag
 122.244 +     * array would be necessary.
 122.245 +     */
 122.246 +
 122.247 +    ulg opt_len;        /* bit length of current block with optimal trees */
 122.248 +    ulg static_len;     /* bit length of current block with static trees */
 122.249 +    uInt matches;       /* number of string matches in current block */
 122.250 +    int last_eob_len;   /* bit length of EOB code for last block */
 122.251 +
 122.252 +#ifdef DEBUG
 122.253 +    ulg compressed_len; /* total bit length of compressed file mod 2^32 */
 122.254 +    ulg bits_sent;      /* bit length of compressed data sent mod 2^32 */
 122.255 +#endif
 122.256 +
 122.257 +    ush bi_buf;
 122.258 +    /* Output buffer. bits are inserted starting at the bottom (least
 122.259 +     * significant bits).
 122.260 +     */
 122.261 +    int bi_valid;
 122.262 +    /* Number of valid bits in bi_buf.  All bits above the last valid bit
 122.263 +     * are always zero.
 122.264 +     */
 122.265 +
 122.266 +} FAR deflate_state;
 122.267 +
 122.268 +/* Output a byte on the stream.
 122.269 + * IN assertion: there is enough room in pending_buf.
 122.270 + */
 122.271 +#define put_byte(s, c) {s->pending_buf[s->pending++] = (c);}
 122.272 +
 122.273 +
 122.274 +#define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1)
 122.275 +/* Minimum amount of lookahead, except at the end of the input file.
 122.276 + * See deflate.c for comments about the MIN_MATCH+1.
 122.277 + */
 122.278 +
 122.279 +#define MAX_DIST(s)  ((s)->w_size-MIN_LOOKAHEAD)
 122.280 +/* In order to simplify the code, particularly on 16 bit machines, match
 122.281 + * distances are limited to MAX_DIST instead of WSIZE.
 122.282 + */
 122.283 +
 122.284 +        /* in trees.c */
 122.285 +void _tr_init         OF((deflate_state *s));
 122.286 +int  _tr_tally        OF((deflate_state *s, unsigned dist, unsigned lc));
 122.287 +void _tr_flush_block  OF((deflate_state *s, charf *buf, ulg stored_len,
 122.288 +                          int eof));
 122.289 +void _tr_align        OF((deflate_state *s));
 122.290 +void _tr_stored_block OF((deflate_state *s, charf *buf, ulg stored_len,
 122.291 +                          int eof));
 122.292 +
 122.293 +#define d_code(dist) \
 122.294 +   ((dist) < 256 ? _dist_code[dist] : _dist_code[256+((dist)>>7)])
 122.295 +/* Mapping from a distance to a distance code. dist is the distance - 1 and
 122.296 + * must not have side effects. _dist_code[256] and _dist_code[257] are never
 122.297 + * used.
 122.298 + */
 122.299 +
 122.300 +#ifndef DEBUG
 122.301 +/* Inline versions of _tr_tally for speed: */
 122.302 +
 122.303 +#if defined(GEN_TREES_H) || !defined(STDC)
 122.304 +  extern uch _length_code[];
 122.305 +  extern uch _dist_code[];
 122.306 +#else
 122.307 +  extern const uch _length_code[];
 122.308 +  extern const uch _dist_code[];
 122.309 +#endif
 122.310 +
 122.311 +# define _tr_tally_lit(s, c, flush) \
 122.312 +  { uch cc = (c); \
 122.313 +    s->d_buf[s->last_lit] = 0; \
 122.314 +    s->l_buf[s->last_lit++] = cc; \
 122.315 +    s->dyn_ltree[cc].Freq++; \
 122.316 +    flush = (s->last_lit == s->lit_bufsize-1); \
 122.317 +   }
 122.318 +# define _tr_tally_dist(s, distance, length, flush) \
 122.319 +  { uch len = (length); \
 122.320 +    ush dist = (distance); \
 122.321 +    s->d_buf[s->last_lit] = dist; \
 122.322 +    s->l_buf[s->last_lit++] = len; \
 122.323 +    dist--; \
 122.324 +    s->dyn_ltree[_length_code[len]+LITERALS+1].Freq++; \
 122.325 +    s->dyn_dtree[d_code(dist)].Freq++; \
 122.326 +    flush = (s->last_lit == s->lit_bufsize-1); \
 122.327 +  }
 122.328 +#else
 122.329 +# define _tr_tally_lit(s, c, flush) flush = _tr_tally(s, 0, c)
 122.330 +# define _tr_tally_dist(s, distance, length, flush) \
 122.331 +              flush = _tr_tally(s, distance, length)
 122.332 +#endif
 122.333 +
 122.334 +#endif /* DEFLATE_H */
   123.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   123.2 +++ b/libs/zlib/gzio.c	Sat Sep 19 05:51:51 2015 +0300
   123.3 @@ -0,0 +1,1026 @@
   123.4 +/* gzio.c -- IO on .gz files
   123.5 + * Copyright (C) 1995-2005 Jean-loup Gailly.
   123.6 + * For conditions of distribution and use, see copyright notice in zlib.h
   123.7 + *
   123.8 + * Compile this file with -DNO_GZCOMPRESS to avoid the compression code.
   123.9 + */
  123.10 +
  123.11 +/* @(#) $Id$ */
  123.12 +
  123.13 +#include <stdio.h>
  123.14 +
  123.15 +#include "zutil.h"
  123.16 +
  123.17 +#ifdef NO_DEFLATE       /* for compatibility with old definition */
  123.18 +#  define NO_GZCOMPRESS
  123.19 +#endif
  123.20 +
  123.21 +#ifndef NO_DUMMY_DECL
  123.22 +struct internal_state {int dummy;}; /* for buggy compilers */
  123.23 +#endif
  123.24 +
  123.25 +#ifndef Z_BUFSIZE
  123.26 +#  ifdef MAXSEG_64K
  123.27 +#    define Z_BUFSIZE 4096 /* minimize memory usage for 16-bit DOS */
  123.28 +#  else
  123.29 +#    define Z_BUFSIZE 16384
  123.30 +#  endif
  123.31 +#endif
  123.32 +#ifndef Z_PRINTF_BUFSIZE
  123.33 +#  define Z_PRINTF_BUFSIZE 4096
  123.34 +#endif
  123.35 +
  123.36 +#ifdef __MVS__
  123.37 +#  pragma map (fdopen , "\174\174FDOPEN")
  123.38 +   FILE *fdopen(int, const char *);
  123.39 +#endif
  123.40 +
  123.41 +#ifndef STDC
  123.42 +extern voidp  malloc OF((uInt size));
  123.43 +extern void   free   OF((voidpf ptr));
  123.44 +#endif
  123.45 +
  123.46 +#define ALLOC(size) malloc(size)
  123.47 +#define TRYFREE(p) {if (p) free(p);}
  123.48 +
  123.49 +static int const gz_magic[2] = {0x1f, 0x8b}; /* gzip magic header */
  123.50 +
  123.51 +/* gzip flag byte */
  123.52 +#define ASCII_FLAG   0x01 /* bit 0 set: file probably ascii text */
  123.53 +#define HEAD_CRC     0x02 /* bit 1 set: header CRC present */
  123.54 +#define EXTRA_FIELD  0x04 /* bit 2 set: extra field present */
  123.55 +#define ORIG_NAME    0x08 /* bit 3 set: original file name present */
  123.56 +#define COMMENT      0x10 /* bit 4 set: file comment present */
  123.57 +#define RESERVED     0xE0 /* bits 5..7: reserved */
  123.58 +
  123.59 +typedef struct gz_stream {
  123.60 +    z_stream stream;
  123.61 +    int      z_err;   /* error code for last stream operation */
  123.62 +    int      z_eof;   /* set if end of input file */
  123.63 +    FILE     *file;   /* .gz file */
  123.64 +    Byte     *inbuf;  /* input buffer */
  123.65 +    Byte     *outbuf; /* output buffer */
  123.66 +    uLong    crc;     /* crc32 of uncompressed data */
  123.67 +    char     *msg;    /* error message */
  123.68 +    char     *path;   /* path name for debugging only */
  123.69 +    int      transparent; /* 1 if input file is not a .gz file */
  123.70 +    char     mode;    /* 'w' or 'r' */
  123.71 +    z_off_t  start;   /* start of compressed data in file (header skipped) */
  123.72 +    z_off_t  in;      /* bytes into deflate or inflate */
  123.73 +    z_off_t  out;     /* bytes out of deflate or inflate */
  123.74 +    int      back;    /* one character push-back */
  123.75 +    int      last;    /* true if push-back is last character */
  123.76 +} gz_stream;
  123.77 +
  123.78 +
  123.79 +local gzFile gz_open      OF((const char *path, const char *mode, int  fd));
  123.80 +local int do_flush        OF((gzFile file, int flush));
  123.81 +local int    get_byte     OF((gz_stream *s));
  123.82 +local void   check_header OF((gz_stream *s));
  123.83 +local int    destroy      OF((gz_stream *s));
  123.84 +local void   putLong      OF((FILE *file, uLong x));
  123.85 +local uLong  getLong      OF((gz_stream *s));
  123.86 +
  123.87 +/* ===========================================================================
  123.88 +     Opens a gzip (.gz) file for reading or writing. The mode parameter
  123.89 +   is as in fopen ("rb" or "wb"). The file is given either by file descriptor
  123.90 +   or path name (if fd == -1).
  123.91 +     gz_open returns NULL if the file could not be opened or if there was
  123.92 +   insufficient memory to allocate the (de)compression state; errno
  123.93 +   can be checked to distinguish the two cases (if errno is zero, the
  123.94 +   zlib error is Z_MEM_ERROR).
  123.95 +*/
  123.96 +local gzFile gz_open (path, mode, fd)
  123.97 +    const char *path;
  123.98 +    const char *mode;
  123.99 +    int  fd;
 123.100 +{
 123.101 +    int err;
 123.102 +    int level = Z_DEFAULT_COMPRESSION; /* compression level */
 123.103 +    int strategy = Z_DEFAULT_STRATEGY; /* compression strategy */
 123.104 +    char *p = (char*)mode;
 123.105 +    gz_stream *s;
 123.106 +    char fmode[80]; /* copy of mode, without the compression level */
 123.107 +    char *m = fmode;
 123.108 +
 123.109 +    if (!path || !mode) return Z_NULL;
 123.110 +
 123.111 +    s = (gz_stream *)ALLOC(sizeof(gz_stream));
 123.112 +    if (!s) return Z_NULL;
 123.113 +
 123.114 +    s->stream.zalloc = (alloc_func)0;
 123.115 +    s->stream.zfree = (free_func)0;
 123.116 +    s->stream.opaque = (voidpf)0;
 123.117 +    s->stream.next_in = s->inbuf = Z_NULL;
 123.118 +    s->stream.next_out = s->outbuf = Z_NULL;
 123.119 +    s->stream.avail_in = s->stream.avail_out = 0;
 123.120 +    s->file = NULL;
 123.121 +    s->z_err = Z_OK;
 123.122 +    s->z_eof = 0;
 123.123 +    s->in = 0;
 123.124 +    s->out = 0;
 123.125 +    s->back = EOF;
 123.126 +    s->crc = crc32(0L, Z_NULL, 0);
 123.127 +    s->msg = NULL;
 123.128 +    s->transparent = 0;
 123.129 +
 123.130 +    s->path = (char*)ALLOC(strlen(path)+1);
 123.131 +    if (s->path == NULL) {
 123.132 +        return destroy(s), (gzFile)Z_NULL;
 123.133 +    }
 123.134 +    strcpy(s->path, path); /* do this early for debugging */
 123.135 +
 123.136 +    s->mode = '\0';
 123.137 +    do {
 123.138 +        if (*p == 'r') s->mode = 'r';
 123.139 +        if (*p == 'w' || *p == 'a') s->mode = 'w';
 123.140 +        if (*p >= '0' && *p <= '9') {
 123.141 +            level = *p - '0';
 123.142 +        } else if (*p == 'f') {
 123.143 +          strategy = Z_FILTERED;
 123.144 +        } else if (*p == 'h') {
 123.145 +          strategy = Z_HUFFMAN_ONLY;
 123.146 +        } else if (*p == 'R') {
 123.147 +          strategy = Z_RLE;
 123.148 +        } else {
 123.149 +            *m++ = *p; /* copy the mode */
 123.150 +        }
 123.151 +    } while (*p++ && m != fmode + sizeof(fmode));
 123.152 +    if (s->mode == '\0') return destroy(s), (gzFile)Z_NULL;
 123.153 +
 123.154 +    if (s->mode == 'w') {
 123.155 +#ifdef NO_GZCOMPRESS
 123.156 +        err = Z_STREAM_ERROR;
 123.157 +#else
 123.158 +        err = deflateInit2(&(s->stream), level,
 123.159 +                           Z_DEFLATED, -MAX_WBITS, DEF_MEM_LEVEL, strategy);
 123.160 +        /* windowBits is passed < 0 to suppress zlib header */
 123.161 +
 123.162 +        s->stream.next_out = s->outbuf = (Byte*)ALLOC(Z_BUFSIZE);
 123.163 +#endif
 123.164 +        if (err != Z_OK || s->outbuf == Z_NULL) {
 123.165 +            return destroy(s), (gzFile)Z_NULL;
 123.166 +        }
 123.167 +    } else {
 123.168 +        s->stream.next_in  = s->inbuf = (Byte*)ALLOC(Z_BUFSIZE);
 123.169 +
 123.170 +        err = inflateInit2(&(s->stream), -MAX_WBITS);
 123.171 +        /* windowBits is passed < 0 to tell that there is no zlib header.
 123.172 +         * Note that in this case inflate *requires* an extra "dummy" byte
 123.173 +         * after the compressed stream in order to complete decompression and
 123.174 +         * return Z_STREAM_END. Here the gzip CRC32 ensures that 4 bytes are
 123.175 +         * present after the compressed stream.
 123.176 +         */
 123.177 +        if (err != Z_OK || s->inbuf == Z_NULL) {
 123.178 +            return destroy(s), (gzFile)Z_NULL;
 123.179 +        }
 123.180 +    }
 123.181 +    s->stream.avail_out = Z_BUFSIZE;
 123.182 +
 123.183 +    errno = 0;
 123.184 +    s->file = fd < 0 ? F_OPEN(path, fmode) : (FILE*)fdopen(fd, fmode);
 123.185 +
 123.186 +    if (s->file == NULL) {
 123.187 +        return destroy(s), (gzFile)Z_NULL;
 123.188 +    }
 123.189 +    if (s->mode == 'w') {
 123.190 +        /* Write a very simple .gz header:
 123.191 +         */
 123.192 +        fprintf(s->file, "%c%c%c%c%c%c%c%c%c%c", gz_magic[0], gz_magic[1],
 123.193 +             Z_DEFLATED, 0 /*flags*/, 0,0,0,0 /*time*/, 0 /*xflags*/, OS_CODE);
 123.194 +        s->start = 10L;
 123.195 +        /* We use 10L instead of ftell(s->file) to because ftell causes an
 123.196 +         * fflush on some systems. This version of the library doesn't use
 123.197 +         * start anyway in write mode, so this initialization is not
 123.198 +         * necessary.
 123.199 +         */
 123.200 +    } else {
 123.201 +        check_header(s); /* skip the .gz header */
 123.202 +        s->start = ftell(s->file) - s->stream.avail_in;
 123.203 +    }
 123.204 +
 123.205 +    return (gzFile)s;
 123.206 +}
 123.207 +
 123.208 +/* ===========================================================================
 123.209 +     Opens a gzip (.gz) file for reading or writing.
 123.210 +*/
 123.211 +gzFile ZEXPORT gzopen (path, mode)
 123.212 +    const char *path;
 123.213 +    const char *mode;
 123.214 +{
 123.215 +    return gz_open (path, mode, -1);
 123.216 +}
 123.217 +
 123.218 +/* ===========================================================================
 123.219 +     Associate a gzFile with the file descriptor fd. fd is not dup'ed here
 123.220 +   to mimic the behavio(u)r of fdopen.
 123.221 +*/
 123.222 +gzFile ZEXPORT gzdopen (fd, mode)
 123.223 +    int fd;
 123.224 +    const char *mode;
 123.225 +{
 123.226 +    char name[46];      /* allow for up to 128-bit integers */
 123.227 +
 123.228 +    if (fd < 0) return (gzFile)Z_NULL;
 123.229 +    sprintf(name, "<fd:%d>", fd); /* for debugging */
 123.230 +
 123.231 +    return gz_open (name, mode, fd);
 123.232 +}
 123.233 +
 123.234 +/* ===========================================================================
 123.235 + * Update the compression level and strategy
 123.236 + */
 123.237 +int ZEXPORT gzsetparams (file, level, strategy)
 123.238 +    gzFile file;
 123.239 +    int level;
 123.240 +    int strategy;
 123.241 +{
 123.242 +    gz_stream *s = (gz_stream*)file;
 123.243 +
 123.244 +    if (s == NULL || s->mode != 'w') return Z_STREAM_ERROR;
 123.245 +
 123.246 +    /* Make room to allow flushing */
 123.247 +    if (s->stream.avail_out == 0) {
 123.248 +
 123.249 +        s->stream.next_out = s->outbuf;
 123.250 +        if (fwrite(s->outbuf, 1, Z_BUFSIZE, s->file) != Z_BUFSIZE) {
 123.251 +            s->z_err = Z_ERRNO;
 123.252 +        }
 123.253 +        s->stream.avail_out = Z_BUFSIZE;
 123.254 +    }
 123.255 +
 123.256 +    return deflateParams (&(s->stream), level, strategy);
 123.257 +}
 123.258 +
 123.259 +/* ===========================================================================
 123.260 +     Read a byte from a gz_stream; update next_in and avail_in. Return EOF
 123.261 +   for end of file.
 123.262 +   IN assertion: the stream s has been sucessfully opened for reading.
 123.263 +*/
 123.264 +local int get_byte(s)
 123.265 +    gz_stream *s;
 123.266 +{
 123.267 +    if (s->z_eof) return EOF;
 123.268 +    if (s->stream.avail_in == 0) {
 123.269 +        errno = 0;
 123.270 +        s->stream.avail_in = (uInt)fread(s->inbuf, 1, Z_BUFSIZE, s->file);
 123.271 +        if (s->stream.avail_in == 0) {
 123.272 +            s->z_eof = 1;
 123.273 +            if (ferror(s->file)) s->z_err = Z_ERRNO;
 123.274 +            return EOF;
 123.275 +        }
 123.276 +        s->stream.next_in = s->inbuf;
 123.277 +    }
 123.278 +    s->stream.avail_in--;
 123.279 +    return *(s->stream.next_in)++;
 123.280 +}
 123.281 +
 123.282 +/* ===========================================================================
 123.283 +      Check the gzip header of a gz_stream opened for reading. Set the stream
 123.284 +    mode to transparent if the gzip magic header is not present; set s->err
 123.285 +    to Z_DATA_ERROR if the magic header is present but the rest of the header
 123.286 +    is incorrect.
 123.287 +    IN assertion: the stream s has already been created sucessfully;
 123.288 +       s->stream.avail_in is zero for the first time, but may be non-zero
 123.289 +       for concatenated .gz files.
 123.290 +*/
 123.291 +local void check_header(s)
 123.292 +    gz_stream *s;
 123.293 +{
 123.294 +    int method; /* method byte */
 123.295 +    int flags;  /* flags byte */
 123.296 +    uInt len;
 123.297 +    int c;
 123.298 +
 123.299 +    /* Assure two bytes in the buffer so we can peek ahead -- handle case
 123.300 +       where first byte of header is at the end of the buffer after the last
 123.301 +       gzip segment */
 123.302 +    len = s->stream.avail_in;
 123.303 +    if (len < 2) {
 123.304 +        if (len) s->inbuf[0] = s->stream.next_in[0];
 123.305 +        errno = 0;
 123.306 +        len = (uInt)fread(s->inbuf + len, 1, Z_BUFSIZE >> len, s->file);
 123.307 +        if (len == 0 && ferror(s->file)) s->z_err = Z_ERRNO;
 123.308 +        s->stream.avail_in += len;
 123.309 +        s->stream.next_in = s->inbuf;
 123.310 +        if (s->stream.avail_in < 2) {
 123.311 +            s->transparent = s->stream.avail_in;
 123.312 +            return;
 123.313 +        }
 123.314 +    }
 123.315 +
 123.316 +    /* Peek ahead to check the gzip magic header */
 123.317 +    if (s->stream.next_in[0] != gz_magic[0] ||
 123.318 +        s->stream.next_in[1] != gz_magic[1]) {
 123.319 +        s->transparent = 1;
 123.320 +        return;
 123.321 +    }
 123.322 +    s->stream.avail_in -= 2;
 123.323 +    s->stream.next_in += 2;
 123.324 +
 123.325 +    /* Check the rest of the gzip header */
 123.326 +    method = get_byte(s);
 123.327 +    flags = get_byte(s);
 123.328 +    if (method != Z_DEFLATED || (flags & RESERVED) != 0) {
 123.329 +        s->z_err = Z_DATA_ERROR;
 123.330 +        return;
 123.331 +    }
 123.332 +
 123.333 +    /* Discard time, xflags and OS code: */
 123.334 +    for (len = 0; len < 6; len++) (void)get_byte(s);
 123.335 +
 123.336 +    if ((flags & EXTRA_FIELD) != 0) { /* skip the extra field */
 123.337 +        len  =  (uInt)get_byte(s);
 123.338 +        len += ((uInt)get_byte(s))<<8;
 123.339 +        /* len is garbage if EOF but the loop below will quit anyway */
 123.340 +        while (len-- != 0 && get_byte(s) != EOF) ;
 123.341 +    }
 123.342 +    if ((flags & ORIG_NAME) != 0) { /* skip the original file name */
 123.343 +        while ((c = get_byte(s)) != 0 && c != EOF) ;
 123.344 +    }
 123.345 +    if ((flags & COMMENT) != 0) {   /* skip the .gz file comment */
 123.346 +        while ((c = get_byte(s)) != 0 && c != EOF) ;
 123.347 +    }
 123.348 +    if ((flags & HEAD_CRC) != 0) {  /* skip the header crc */
 123.349 +        for (len = 0; len < 2; len++) (void)get_byte(s);
 123.350 +    }
 123.351 +    s->z_err = s->z_eof ? Z_DATA_ERROR : Z_OK;
 123.352 +}
 123.353 +
 123.354 + /* ===========================================================================
 123.355 + * Cleanup then free the given gz_stream. Return a zlib error code.
 123.356 +   Try freeing in the reverse order of allocations.
 123.357 + */
 123.358 +local int destroy (s)
 123.359 +    gz_stream *s;
 123.360 +{
 123.361 +    int err = Z_OK;
 123.362 +
 123.363 +    if (!s) return Z_STREAM_ERROR;
 123.364 +
 123.365 +    TRYFREE(s->msg);
 123.366 +
 123.367 +    if (s->stream.state != NULL) {
 123.368 +        if (s->mode == 'w') {
 123.369 +#ifdef NO_GZCOMPRESS
 123.370 +            err = Z_STREAM_ERROR;
 123.371 +#else
 123.372 +            err = deflateEnd(&(s->stream));
 123.373 +#endif
 123.374 +        } else if (s->mode == 'r') {
 123.375 +            err = inflateEnd(&(s->stream));
 123.376 +        }
 123.377 +    }
 123.378 +    if (s->file != NULL && fclose(s->file)) {
 123.379 +#ifdef ESPIPE
 123.380 +        if (errno != ESPIPE) /* fclose is broken for pipes in HP/UX */
 123.381 +#endif
 123.382 +            err = Z_ERRNO;
 123.383 +    }
 123.384 +    if (s->z_err < 0) err = s->z_err;
 123.385 +
 123.386 +    TRYFREE(s->inbuf);
 123.387 +    TRYFREE(s->outbuf);
 123.388 +    TRYFREE(s->path);
 123.389 +    TRYFREE(s);
 123.390 +    return err;
 123.391 +}
 123.392 +
 123.393 +/* ===========================================================================
 123.394 +     Reads the given number of uncompressed bytes from the compressed file.
 123.395 +   gzread returns the number of bytes actually read (0 for end of file).
 123.396 +*/
 123.397 +int ZEXPORT gzread (file, buf, len)
 123.398 +    gzFile file;
 123.399 +    voidp buf;
 123.400 +    unsigned len;
 123.401 +{
 123.402 +    gz_stream *s = (gz_stream*)file;
 123.403 +    Bytef *start = (Bytef*)buf; /* starting point for crc computation */
 123.404 +    Byte  *next_out; /* == stream.next_out but not forced far (for MSDOS) */
 123.405 +
 123.406 +    if (s == NULL || s->mode != 'r') return Z_STREAM_ERROR;
 123.407 +
 123.408 +    if (s->z_err == Z_DATA_ERROR || s->z_err == Z_ERRNO) return -1;
 123.409 +    if (s->z_err == Z_STREAM_END) return 0;  /* EOF */
 123.410 +
 123.411 +    next_out = (Byte*)buf;
 123.412 +    s->stream.next_out = (Bytef*)buf;
 123.413 +    s->stream.avail_out = len;
 123.414 +
 123.415 +    if (s->stream.avail_out && s->back != EOF) {
 123.416 +        *next_out++ = s->back;
 123.417 +        s->stream.next_out++;
 123.418 +        s->stream.avail_out--;
 123.419 +        s->back = EOF;
 123.420 +        s->out++;
 123.421 +        start++;
 123.422 +        if (s->last) {
 123.423 +            s->z_err = Z_STREAM_END;
 123.424 +            return 1;
 123.425 +        }
 123.426 +    }
 123.427 +
 123.428 +    while (s->stream.avail_out != 0) {
 123.429 +
 123.430 +        if (s->transparent) {
 123.431 +            /* Copy first the lookahead bytes: */
 123.432 +            uInt n = s->stream.avail_in;
 123.433 +            if (n > s->stream.avail_out) n = s->stream.avail_out;
 123.434 +            if (n > 0) {
 123.435 +                zmemcpy(s->stream.next_out, s->stream.next_in, n);
 123.436 +                next_out += n;
 123.437 +                s->stream.next_out = next_out;
 123.438 +                s->stream.next_in   += n;
 123.439 +                s->stream.avail_out -= n;
 123.440 +                s->stream.avail_in  -= n;
 123.441 +            }
 123.442 +            if (s->stream.avail_out > 0) {
 123.443 +                s->stream.avail_out -=
 123.444 +                    (uInt)fread(next_out, 1, s->stream.avail_out, s->file);
 123.445 +            }
 123.446 +            len -= s->stream.avail_out;
 123.447 +            s->in  += len;
 123.448 +            s->out += len;
 123.449 +            if (len == 0) s->z_eof = 1;
 123.450 +            return (int)len;
 123.451 +        }
 123.452 +        if (s->stream.avail_in == 0 && !s->z_eof) {
 123.453 +
 123.454 +            errno = 0;
 123.455 +            s->stream.avail_in = (uInt)fread(s->inbuf, 1, Z_BUFSIZE, s->file);
 123.456 +            if (s->stream.avail_in == 0) {
 123.457 +                s->z_eof = 1;
 123.458 +                if (ferror(s->file)) {
 123.459 +                    s->z_err = Z_ERRNO;
 123.460 +                    break;
 123.461 +                }
 123.462 +            }
 123.463 +            s->stream.next_in = s->inbuf;
 123.464 +        }
 123.465 +        s->in += s->stream.avail_in;
 123.466 +        s->out += s->stream.avail_out;
 123.467 +        s->z_err = inflate(&(s->stream), Z_NO_FLUSH);
 123.468 +        s->in -= s->stream.avail_in;
 123.469 +        s->out -= s->stream.avail_out;
 123.470 +
 123.471 +        if (s->z_err == Z_STREAM_END) {
 123.472 +            /* Check CRC and original size */
 123.473 +            s->crc = crc32(s->crc, start, (uInt)(s->stream.next_out - start));
 123.474 +            start = s->stream.next_out;
 123.475 +
 123.476 +            if (getLong(s) != s->crc) {
 123.477 +                s->z_err = Z_DATA_ERROR;
 123.478 +            } else {
 123.479 +                (void)getLong(s);
 123.480 +                /* The uncompressed length returned by above getlong() may be
 123.481 +                 * different from s->out in case of concatenated .gz files.
 123.482 +                 * Check for such files:
 123.483 +                 */
 123.484 +                check_header(s);
 123.485 +                if (s->z_err == Z_OK) {
 123.486 +                    inflateReset(&(s->stream));
 123.487 +                    s->crc = crc32(0L, Z_NULL, 0);
 123.488 +                }
 123.489 +            }
 123.490 +        }
 123.491 +        if (s->z_err != Z_OK || s->z_eof) break;
 123.492 +    }
 123.493 +    s->crc = crc32(s->crc, start, (uInt)(s->stream.next_out - start));
 123.494 +
 123.495 +    if (len == s->stream.avail_out &&
 123.496 +        (s->z_err == Z_DATA_ERROR || s->z_err == Z_ERRNO))
 123.497 +        return -1;
 123.498 +    return (int)(len - s->stream.avail_out);
 123.499 +}
 123.500 +
 123.501 +
 123.502 +/* ===========================================================================
 123.503 +      Reads one byte from the compressed file. gzgetc returns this byte
 123.504 +   or -1 in case of end of file or error.
 123.505 +*/
 123.506 +int ZEXPORT gzgetc(file)
 123.507 +    gzFile file;
 123.508 +{
 123.509 +    unsigned char c;
 123.510 +
 123.511 +    return gzread(file, &c, 1) == 1 ? c : -1;
 123.512 +}
 123.513 +
 123.514 +
 123.515 +/* ===========================================================================
 123.516 +      Push one byte back onto the stream.
 123.517 +*/
 123.518 +int ZEXPORT gzungetc(c, file)
 123.519 +    int c;
 123.520 +    gzFile file;
 123.521 +{
 123.522 +    gz_stream *s = (gz_stream*)file;
 123.523 +
 123.524 +    if (s == NULL || s->mode != 'r' || c == EOF || s->back != EOF) return EOF;
 123.525 +    s->back = c;
 123.526 +    s->out--;
 123.527 +    s->last = (s->z_err == Z_STREAM_END);
 123.528 +    if (s->last) s->z_err = Z_OK;
 123.529 +    s->z_eof = 0;
 123.530 +    return c;
 123.531 +}
 123.532 +
 123.533 +
 123.534 +/* ===========================================================================
 123.535 +      Reads bytes from the compressed file until len-1 characters are
 123.536 +   read, or a newline character is read and transferred to buf, or an
 123.537 +   end-of-file condition is encountered.  The string is then terminated
 123.538 +   with a null character.
 123.539 +      gzgets returns buf, or Z_NULL in case of error.
 123.540 +
 123.541 +      The current implementation is not optimized at all.
 123.542 +*/
 123.543 +char * ZEXPORT gzgets(file, buf, len)
 123.544 +    gzFile file;
 123.545 +    char *buf;
 123.546 +    int len;
 123.547 +{
 123.548 +    char *b = buf;
 123.549 +    if (buf == Z_NULL || len <= 0) return Z_NULL;
 123.550 +
 123.551 +    while (--len > 0 && gzread(file, buf, 1) == 1 && *buf++ != '\n') ;
 123.552 +    *buf = '\0';
 123.553 +    return b == buf && len > 0 ? Z_NULL : b;
 123.554 +}
 123.555 +
 123.556 +
 123.557 +#ifndef NO_GZCOMPRESS
 123.558 +/* ===========================================================================
 123.559 +     Writes the given number of uncompressed bytes into the compressed file.
 123.560 +   gzwrite returns the number of bytes actually written (0 in case of error).
 123.561 +*/
 123.562 +int ZEXPORT gzwrite (file, buf, len)
 123.563 +    gzFile file;
 123.564 +    voidpc buf;
 123.565 +    unsigned len;
 123.566 +{
 123.567 +    gz_stream *s = (gz_stream*)file;
 123.568 +
 123.569 +    if (s == NULL || s->mode != 'w') return Z_STREAM_ERROR;
 123.570 +
 123.571 +    s->stream.next_in = (Bytef*)buf;
 123.572 +    s->stream.avail_in = len;
 123.573 +
 123.574 +    while (s->stream.avail_in != 0) {
 123.575 +
 123.576 +        if (s->stream.avail_out == 0) {
 123.577 +
 123.578 +            s->stream.next_out = s->outbuf;
 123.579 +            if (fwrite(s->outbuf, 1, Z_BUFSIZE, s->file) != Z_BUFSIZE) {
 123.580 +                s->z_err = Z_ERRNO;
 123.581 +                break;
 123.582 +            }
 123.583 +            s->stream.avail_out = Z_BUFSIZE;
 123.584 +        }
 123.585 +        s->in += s->stream.avail_in;
 123.586 +        s->out += s->stream.avail_out;
 123.587 +        s->z_err = deflate(&(s->stream), Z_NO_FLUSH);
 123.588 +        s->in -= s->stream.avail_in;
 123.589 +        s->out -= s->stream.avail_out;
 123.590 +        if (s->z_err != Z_OK) break;
 123.591 +    }
 123.592 +    s->crc = crc32(s->crc, (const Bytef *)buf, len);
 123.593 +
 123.594 +    return (int)(len - s->stream.avail_in);
 123.595 +}
 123.596 +
 123.597 +
 123.598 +/* ===========================================================================
 123.599 +     Converts, formats, and writes the args to the compressed file under
 123.600 +   control of the format string, as in fprintf. gzprintf returns the number of
 123.601 +   uncompressed bytes actually written (0 in case of error).
 123.602 +*/
 123.603 +#ifdef STDC
 123.604 +#include <stdarg.h>
 123.605 +
 123.606 +int ZEXPORTVA gzprintf (gzFile file, const char *format, /* args */ ...)
 123.607 +{
 123.608 +    char buf[Z_PRINTF_BUFSIZE];
 123.609 +    va_list va;
 123.610 +    int len;
 123.611 +
 123.612 +    buf[sizeof(buf) - 1] = 0;
 123.613 +    va_start(va, format);
 123.614 +#ifdef NO_vsnprintf
 123.615 +#  ifdef HAS_vsprintf_void
 123.616 +    (void)vsprintf(buf, format, va);
 123.617 +    va_end(va);
 123.618 +    for (len = 0; len < sizeof(buf); len++)
 123.619 +        if (buf[len] == 0) break;
 123.620 +#  else
 123.621 +    len = vsprintf(buf, format, va);
 123.622 +    va_end(va);
 123.623 +#  endif
 123.624 +#else
 123.625 +#  ifdef HAS_vsnprintf_void
 123.626 +    (void)vsnprintf(buf, sizeof(buf), format, va);
 123.627 +    va_end(va);
 123.628 +    len = strlen(buf);
 123.629 +#  else
 123.630 +    len = vsnprintf(buf, sizeof(buf), format, va);
 123.631 +    va_end(va);
 123.632 +#  endif
 123.633 +#endif
 123.634 +    if (len <= 0 || len >= (int)sizeof(buf) || buf[sizeof(buf) - 1] != 0)
 123.635 +        return 0;
 123.636 +    return gzwrite(file, buf, (unsigned)len);
 123.637 +}
 123.638 +#else /* not ANSI C */
 123.639 +
 123.640 +int ZEXPORTVA gzprintf (file, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10,
 123.641 +                       a11, a12, a13, a14, a15, a16, a17, a18, a19, a20)
 123.642 +    gzFile file;
 123.643 +    const char *format;
 123.644 +    int a1, a2, a3, a4, a5, a6, a7, a8, a9, a10,
 123.645 +        a11, a12, a13, a14, a15, a16, a17, a18, a19, a20;
 123.646 +{
 123.647 +    char buf[Z_PRINTF_BUFSIZE];
 123.648 +    int len;
 123.649 +
 123.650 +    buf[sizeof(buf) - 1] = 0;
 123.651 +#ifdef NO_snprintf
 123.652 +#  ifdef HAS_sprintf_void
 123.653 +    sprintf(buf, format, a1, a2, a3, a4, a5, a6, a7, a8,
 123.654 +            a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
 123.655 +    for (len = 0; len < sizeof(buf); len++)
 123.656 +        if (buf[len] == 0) break;
 123.657 +#  else
 123.658 +    len = sprintf(buf, format, a1, a2, a3, a4, a5, a6, a7, a8,
 123.659 +                a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
 123.660 +#  endif
 123.661 +#else
 123.662 +#  ifdef HAS_snprintf_void
 123.663 +    snprintf(buf, sizeof(buf), format, a1, a2, a3, a4, a5, a6, a7, a8,
 123.664 +             a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
 123.665 +    len = strlen(buf);
 123.666 +#  else
 123.667 +    len = snprintf(buf, sizeof(buf), format, a1, a2, a3, a4, a5, a6, a7, a8,
 123.668 +                 a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
 123.669 +#  endif
 123.670 +#endif
 123.671 +    if (len <= 0 || len >= sizeof(buf) || buf[sizeof(buf) - 1] != 0)
 123.672 +        return 0;
 123.673 +    return gzwrite(file, buf, len);
 123.674 +}
 123.675 +#endif
 123.676 +
 123.677 +/* ===========================================================================
 123.678 +      Writes c, converted to an unsigned char, into the compressed file.
 123.679 +   gzputc returns the value that was written, or -1 in case of error.
 123.680 +*/
 123.681 +int ZEXPORT gzputc(file, c)
 123.682 +    gzFile file;
 123.683 +    int c;
 123.684 +{
 123.685 +    unsigned char cc = (unsigned char) c; /* required for big endian systems */
 123.686 +
 123.687 +    return gzwrite(file, &cc, 1) == 1 ? (int)cc : -1;
 123.688 +}
 123.689 +
 123.690 +
 123.691 +/* ===========================================================================
 123.692 +      Writes the given null-terminated string to the compressed file, excluding
 123.693 +   the terminating null character.
 123.694 +      gzputs returns the number of characters written, or -1 in case of error.
 123.695 +*/
 123.696 +int ZEXPORT gzputs(file, s)
 123.697 +    gzFile file;
 123.698 +    const char *s;
 123.699 +{
 123.700 +    return gzwrite(file, (char*)s, (unsigned)strlen(s));
 123.701 +}
 123.702 +
 123.703 +
 123.704 +/* ===========================================================================
 123.705 +     Flushes all pending output into the compressed file. The parameter
 123.706 +   flush is as in the deflate() function.
 123.707 +*/
 123.708 +local int do_flush (file, flush)
 123.709 +    gzFile file;
 123.710 +    int flush;
 123.711 +{
 123.712 +    uInt len;
 123.713 +    int done = 0;
 123.714 +    gz_stream *s = (gz_stream*)file;
 123.715 +
 123.716 +    if (s == NULL || s->mode != 'w') return Z_STREAM_ERROR;
 123.717 +
 123.718 +    s->stream.avail_in = 0; /* should be zero already anyway */
 123.719 +
 123.720 +    for (;;) {
 123.721 +        len = Z_BUFSIZE - s->stream.avail_out;
 123.722 +
 123.723 +        if (len != 0) {
 123.724 +            if ((uInt)fwrite(s->outbuf, 1, len, s->file) != len) {
 123.725 +                s->z_err = Z_ERRNO;
 123.726 +                return Z_ERRNO;
 123.727 +            }
 123.728 +            s->stream.next_out = s->outbuf;
 123.729 +            s->stream.avail_out = Z_BUFSIZE;
 123.730 +        }
 123.731 +        if (done) break;
 123.732 +        s->out += s->stream.avail_out;
 123.733 +        s->z_err = deflate(&(s->stream), flush);
 123.734 +        s->out -= s->stream.avail_out;
 123.735 +
 123.736 +        /* Ignore the second of two consecutive flushes: */
 123.737 +        if (len == 0 && s->z_err == Z_BUF_ERROR) s->z_err = Z_OK;
 123.738 +
 123.739 +        /* deflate has finished flushing only when it hasn't used up
 123.740 +         * all the available space in the output buffer:
 123.741 +         */
 123.742 +        done = (s->stream.avail_out != 0 || s->z_err == Z_STREAM_END);
 123.743 +
 123.744 +        if (s->z_err != Z_OK && s->z_err != Z_STREAM_END) break;
 123.745 +    }
 123.746 +    return  s->z_err == Z_STREAM_END ? Z_OK : s->z_err;
 123.747 +}
 123.748 +
 123.749 +int ZEXPORT gzflush (file, flush)
 123.750 +     gzFile file;
 123.751 +     int flush;
 123.752 +{
 123.753 +    gz_stream *s = (gz_stream*)file;
 123.754 +    int err = do_flush (file, flush);
 123.755 +
 123.756 +    if (err) return err;
 123.757 +    fflush(s->file);
 123.758 +    return  s->z_err == Z_STREAM_END ? Z_OK : s->z_err;
 123.759 +}
 123.760 +#endif /* NO_GZCOMPRESS */
 123.761 +
 123.762 +/* ===========================================================================
 123.763 +      Sets the starting position for the next gzread or gzwrite on the given
 123.764 +   compressed file. The offset represents a number of bytes in the
 123.765 +      gzseek returns the resulting offset location as measured in bytes from
 123.766 +   the beginning of the uncompressed stream, or -1 in case of error.
 123.767 +      SEEK_END is not implemented, returns error.
 123.768 +      In this version of the library, gzseek can be extremely slow.
 123.769 +*/
 123.770 +z_off_t ZEXPORT gzseek (file, offset, whence)
 123.771 +    gzFile file;
 123.772 +    z_off_t offset;
 123.773 +    int whence;
 123.774 +{
 123.775 +    gz_stream *s = (gz_stream*)file;
 123.776 +
 123.777 +    if (s == NULL || whence == SEEK_END ||
 123.778 +        s->z_err == Z_ERRNO || s->z_err == Z_DATA_ERROR) {
 123.779 +        return -1L;
 123.780 +    }
 123.781 +
 123.782 +    if (s->mode == 'w') {
 123.783 +#ifdef NO_GZCOMPRESS
 123.784 +        return -1L;
 123.785 +#else
 123.786 +        if (whence == SEEK_SET) {
 123.787 +            offset -= s->in;
 123.788 +        }
 123.789 +        if (offset < 0) return -1L;
 123.790 +
 123.791 +        /* At this point, offset is the number of zero bytes to write. */
 123.792 +        if (s->inbuf == Z_NULL) {
 123.793 +            s->inbuf = (Byte*)ALLOC(Z_BUFSIZE); /* for seeking */
 123.794 +            if (s->inbuf == Z_NULL) return -1L;
 123.795 +            zmemzero(s->inbuf, Z_BUFSIZE);
 123.796 +        }
 123.797 +        while (offset > 0)  {
 123.798 +            uInt size = Z_BUFSIZE;
 123.799 +            if (offset < Z_BUFSIZE) size = (uInt)offset;
 123.800 +
 123.801 +            size = gzwrite(file, s->inbuf, size);
 123.802 +            if (size == 0) return -1L;
 123.803 +
 123.804 +            offset -= size;
 123.805 +        }
 123.806 +        return s->in;
 123.807 +#endif
 123.808 +    }
 123.809 +    /* Rest of function is for reading only */
 123.810 +
 123.811 +    /* compute absolute position */
 123.812 +    if (whence == SEEK_CUR) {
 123.813 +        offset += s->out;
 123.814 +    }
 123.815 +    if (offset < 0) return -1L;
 123.816 +
 123.817 +    if (s->transparent) {
 123.818 +        /* map to fseek */
 123.819 +        s->back = EOF;
 123.820 +        s->stream.avail_in = 0;
 123.821 +        s->stream.next_in = s->inbuf;
 123.822 +        if (fseek(s->file, offset, SEEK_SET) < 0) return -1L;
 123.823 +
 123.824 +        s->in = s->out = offset;
 123.825 +        return offset;
 123.826 +    }
 123.827 +
 123.828 +    /* For a negative seek, rewind and use positive seek */
 123.829 +    if (offset >= s->out) {
 123.830 +        offset -= s->out;
 123.831 +    } else if (gzrewind(file) < 0) {
 123.832 +        return -1L;
 123.833 +    }
 123.834 +    /* offset is now the number of bytes to skip. */
 123.835 +
 123.836 +    if (offset != 0 && s->outbuf == Z_NULL) {
 123.837 +        s->outbuf = (Byte*)ALLOC(Z_BUFSIZE);
 123.838 +        if (s->outbuf == Z_NULL) return -1L;
 123.839 +    }
 123.840 +    if (offset && s->back != EOF) {
 123.841 +        s->back = EOF;
 123.842 +        s->out++;
 123.843 +        offset--;
 123.844 +        if (s->last) s->z_err = Z_STREAM_END;
 123.845 +    }
 123.846 +    while (offset > 0)  {
 123.847 +        int size = Z_BUFSIZE;
 123.848 +        if (offset < Z_BUFSIZE) size = (int)offset;
 123.849 +
 123.850 +        size = gzread(file, s->outbuf, (uInt)size);
 123.851 +        if (size <= 0) return -1L;
 123.852 +        offset -= size;
 123.853 +    }
 123.854 +    return s->out;
 123.855 +}
 123.856 +
 123.857 +/* ===========================================================================
 123.858 +     Rewinds input file.
 123.859 +*/
 123.860 +int ZEXPORT gzrewind (file)
 123.861 +    gzFile file;
 123.862 +{
 123.863 +    gz_stream *s = (gz_stream*)file;
 123.864 +
 123.865 +    if (s == NULL || s->mode != 'r') return -1;
 123.866 +
 123.867 +    s->z_err = Z_OK;
 123.868 +    s->z_eof = 0;
 123.869 +    s->back = EOF;
 123.870 +    s->stream.avail_in = 0;
 123.871 +    s->stream.next_in = s->inbuf;
 123.872 +    s->crc = crc32(0L, Z_NULL, 0);
 123.873 +    if (!s->transparent) (void)inflateReset(&s->stream);
 123.874 +    s->in = 0;
 123.875 +    s->out = 0;
 123.876 +    return fseek(s->file, s->start, SEEK_SET);
 123.877 +}
 123.878 +
 123.879 +/* ===========================================================================
 123.880 +     Returns the starting position for the next gzread or gzwrite on the
 123.881 +   given compressed file. This position represents a number of bytes in the
 123.882 +   uncompressed data stream.
 123.883 +*/
 123.884 +z_off_t ZEXPORT gztell (file)
 123.885 +    gzFile file;
 123.886 +{
 123.887 +    return gzseek(file, 0L, SEEK_CUR);
 123.888 +}
 123.889 +
 123.890 +/* ===========================================================================
 123.891 +     Returns 1 when EOF has previously been detected reading the given
 123.892 +   input stream, otherwise zero.
 123.893 +*/
 123.894 +int ZEXPORT gzeof (file)
 123.895 +    gzFile file;
 123.896 +{
 123.897 +    gz_stream *s = (gz_stream*)file;
 123.898 +
 123.899 +    /* With concatenated compressed files that can have embedded
 123.900 +     * crc trailers, z_eof is no longer the only/best indicator of EOF
 123.901 +     * on a gz_stream. Handle end-of-stream error explicitly here.
 123.902 +     */
 123.903 +    if (s == NULL || s->mode != 'r') return 0;
 123.904 +    if (s->z_eof) return 1;
 123.905 +    return s->z_err == Z_STREAM_END;
 123.906 +}
 123.907 +
 123.908 +/* ===========================================================================
 123.909 +     Returns 1 if reading and doing so transparently, otherwise zero.
 123.910 +*/
 123.911 +int ZEXPORT gzdirect (file)
 123.912 +    gzFile file;
 123.913 +{
 123.914 +    gz_stream *s = (gz_stream*)file;
 123.915 +
 123.916 +    if (s == NULL || s->mode != 'r') return 0;
 123.917 +    return s->transparent;
 123.918 +}
 123.919 +
 123.920 +/* ===========================================================================
 123.921 +   Outputs a long in LSB order to the given file
 123.922 +*/
 123.923 +local void putLong (file, x)
 123.924 +    FILE *file;
 123.925 +    uLong x;
 123.926 +{
 123.927 +    int n;
 123.928 +    for (n = 0; n < 4; n++) {
 123.929 +        fputc((int)(x & 0xff), file);
 123.930 +        x >>= 8;
 123.931 +    }
 123.932 +}
 123.933 +
 123.934 +/* ===========================================================================
 123.935 +   Reads a long in LSB order from the given gz_stream. Sets z_err in case
 123.936 +   of error.
 123.937 +*/
 123.938 +local uLong getLong (s)
 123.939 +    gz_stream *s;
 123.940 +{
 123.941 +    uLong x = (uLong)get_byte(s);
 123.942 +    int c;
 123.943 +
 123.944 +    x += ((uLong)get_byte(s))<<8;
 123.945 +    x += ((uLong)get_byte(s))<<16;
 123.946 +    c = get_byte(s);
 123.947 +    if (c == EOF) s->z_err = Z_DATA_ERROR;
 123.948 +    x += ((uLong)c)<<24;
 123.949 +    return x;
 123.950 +}
 123.951 +
 123.952 +/* ===========================================================================
 123.953 +     Flushes all pending output if necessary, closes the compressed file
 123.954 +   and deallocates all the (de)compression state.
 123.955 +*/
 123.956 +int ZEXPORT gzclose (file)
 123.957 +    gzFile file;
 123.958 +{
 123.959 +    gz_stream *s = (gz_stream*)file;
 123.960 +
 123.961 +    if (s == NULL) return Z_STREAM_ERROR;
 123.962 +
 123.963 +    if (s->mode == 'w') {
 123.964 +#ifdef NO_GZCOMPRESS
 123.965 +        return Z_STREAM_ERROR;
 123.966 +#else
 123.967 +        if (do_flush (file, Z_FINISH) != Z_OK)
 123.968 +            return destroy((gz_stream*)file);
 123.969 +
 123.970 +        putLong (s->file, s->crc);
 123.971 +        putLong (s->file, (uLong)(s->in & 0xffffffff));
 123.972 +#endif
 123.973 +    }
 123.974 +    return destroy((gz_stream*)file);
 123.975 +}
 123.976 +
 123.977 +#ifdef STDC
 123.978 +#  define zstrerror(errnum) strerror(errnum)
 123.979 +#else
 123.980 +#  define zstrerror(errnum) ""
 123.981 +#endif
 123.982 +
 123.983 +/* ===========================================================================
 123.984 +     Returns the error message for the last error which occurred on the
 123.985 +   given compressed file. errnum is set to zlib error number. If an
 123.986 +   error occurred in the file system and not in the compression library,
 123.987 +   errnum is set to Z_ERRNO and the application may consult errno
 123.988 +   to get the exact error code.
 123.989 +*/
 123.990 +const char * ZEXPORT gzerror (file, errnum)
 123.991 +    gzFile file;
 123.992 +    int *errnum;
 123.993 +{
 123.994 +    char *m;
 123.995 +    gz_stream *s = (gz_stream*)file;
 123.996 +
 123.997 +    if (s == NULL) {
 123.998 +        *errnum = Z_STREAM_ERROR;
 123.999 +        return (const char*)ERR_MSG(Z_STREAM_ERROR);
123.1000 +    }
123.1001 +    *errnum = s->z_err;
123.1002 +    if (*errnum == Z_OK) return (const char*)"";
123.1003 +
123.1004 +    m = (char*)(*errnum == Z_ERRNO ? zstrerror(errno) : s->stream.msg);
123.1005 +
123.1006 +    if (m == NULL || *m == '\0') m = (char*)ERR_MSG(s->z_err);
123.1007 +
123.1008 +    TRYFREE(s->msg);
123.1009 +    s->msg = (char*)ALLOC(strlen(s->path) + strlen(m) + 3);
123.1010 +    if (s->msg == Z_NULL) return (const char*)ERR_MSG(Z_MEM_ERROR);
123.1011 +    strcpy(s->msg, s->path);
123.1012 +    strcat(s->msg, ": ");
123.1013 +    strcat(s->msg, m);
123.1014 +    return (const char*)s->msg;
123.1015 +}
123.1016 +
123.1017 +/* ===========================================================================
123.1018 +     Clear the error and end-of-file flags, and do the same for the real file.
123.1019 +*/
123.1020 +void ZEXPORT gzclearerr (file)
123.1021 +    gzFile file;
123.1022 +{
123.1023 +    gz_stream *s = (gz_stream*)file;
123.1024 +
123.1025 +    if (s == NULL) return;
123.1026 +    if (s->z_err != Z_STREAM_END) s->z_err = Z_OK;
123.1027 +    s->z_eof = 0;
123.1028 +    clearerr(s->file);
123.1029 +}
   124.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   124.2 +++ b/libs/zlib/infback.c	Sat Sep 19 05:51:51 2015 +0300
   124.3 @@ -0,0 +1,623 @@
   124.4 +/* infback.c -- inflate using a call-back interface
   124.5 + * Copyright (C) 1995-2005 Mark Adler
   124.6 + * For conditions of distribution and use, see copyright notice in zlib.h
   124.7 + */
   124.8 +
   124.9 +/*
  124.10 +   This code is largely copied from inflate.c.  Normally either infback.o or
  124.11 +   inflate.o would be linked into an application--not both.  The interface
  124.12 +   with inffast.c is retained so that optimized assembler-coded versions of
  124.13 +   inflate_fast() can be used with either inflate.c or infback.c.
  124.14 + */
  124.15 +
  124.16 +#include "zutil.h"
  124.17 +#include "inftrees.h"
  124.18 +#include "inflate.h"
  124.19 +#include "inffast.h"
  124.20 +
  124.21 +/* function prototypes */
  124.22 +local void fixedtables OF((struct inflate_state FAR *state));
  124.23 +
  124.24 +/*
  124.25 +   strm provides memory allocation functions in zalloc and zfree, or
  124.26 +   Z_NULL to use the library memory allocation functions.
  124.27 +
  124.28 +   windowBits is in the range 8..15, and window is a user-supplied
  124.29 +   window and output buffer that is 2**windowBits bytes.
  124.30 + */
  124.31 +int ZEXPORT inflateBackInit_(strm, windowBits, window, version, stream_size)
  124.32 +z_streamp strm;
  124.33 +int windowBits;
  124.34 +unsigned char FAR *window;
  124.35 +const char *version;
  124.36 +int stream_size;
  124.37 +{
  124.38 +    struct inflate_state FAR *state;
  124.39 +
  124.40 +    if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
  124.41 +        stream_size != (int)(sizeof(z_stream)))
  124.42 +        return Z_VERSION_ERROR;
  124.43 +    if (strm == Z_NULL || window == Z_NULL ||
  124.44 +        windowBits < 8 || windowBits > 15)
  124.45 +        return Z_STREAM_ERROR;
  124.46 +    strm->msg = Z_NULL;                 /* in case we return an error */
  124.47 +    if (strm->zalloc == (alloc_func)0) {
  124.48 +        strm->zalloc = zcalloc;
  124.49 +        strm->opaque = (voidpf)0;
  124.50 +    }
  124.51 +    if (strm->zfree == (free_func)0) strm->zfree = zcfree;
  124.52 +    state = (struct inflate_state FAR *)ZALLOC(strm, 1,
  124.53 +                                               sizeof(struct inflate_state));
  124.54 +    if (state == Z_NULL) return Z_MEM_ERROR;
  124.55 +    Tracev((stderr, "inflate: allocated\n"));
  124.56 +    strm->state = (struct internal_state FAR *)state;
  124.57 +    state->dmax = 32768U;
  124.58 +    state->wbits = windowBits;
  124.59 +    state->wsize = 1U << windowBits;
  124.60 +    state->window = window;
  124.61 +    state->write = 0;
  124.62 +    state->whave = 0;
  124.63 +    return Z_OK;
  124.64 +}
  124.65 +
  124.66 +/*
  124.67 +   Return state with length and distance decoding tables and index sizes set to
  124.68 +   fixed code decoding.  Normally this returns fixed tables from inffixed.h.
  124.69 +   If BUILDFIXED is defined, then instead this routine builds the tables the
  124.70 +   first time it's called, and returns those tables the first time and
  124.71 +   thereafter.  This reduces the size of the code by about 2K bytes, in
  124.72 +   exchange for a little execution time.  However, BUILDFIXED should not be
  124.73 +   used for threaded applications, since the rewriting of the tables and virgin
  124.74 +   may not be thread-safe.
  124.75 + */
  124.76 +local void fixedtables(state)
  124.77 +struct inflate_state FAR *state;
  124.78 +{
  124.79 +#ifdef BUILDFIXED
  124.80 +    static int virgin = 1;
  124.81 +    static code *lenfix, *distfix;
  124.82 +    static code fixed[544];
  124.83 +
  124.84 +    /* build fixed huffman tables if first call (may not be thread safe) */
  124.85 +    if (virgin) {
  124.86 +        unsigned sym, bits;
  124.87 +        static code *next;
  124.88 +
  124.89 +        /* literal/length table */
  124.90 +        sym = 0;
  124.91 +        while (sym < 144) state->lens[sym++] = 8;
  124.92 +        while (sym < 256) state->lens[sym++] = 9;
  124.93 +        while (sym < 280) state->lens[sym++] = 7;
  124.94 +        while (sym < 288) state->lens[sym++] = 8;
  124.95 +        next = fixed;
  124.96 +        lenfix = next;
  124.97 +        bits = 9;
  124.98 +        inflate_table(LENS, state->lens, 288, &(next), &(bits), state->work);
  124.99 +
 124.100 +        /* distance table */
 124.101 +        sym = 0;
 124.102 +        while (sym < 32) state->lens[sym++] = 5;
 124.103 +        distfix = next;
 124.104 +        bits = 5;
 124.105 +        inflate_table(DISTS, state->lens, 32, &(next), &(bits), state->work);
 124.106 +
 124.107 +        /* do this just once */
 124.108 +        virgin = 0;
 124.109 +    }
 124.110 +#else /* !BUILDFIXED */
 124.111 +#   include "inffixed.h"
 124.112 +#endif /* BUILDFIXED */
 124.113 +    state->lencode = lenfix;
 124.114 +    state->lenbits = 9;
 124.115 +    state->distcode = distfix;
 124.116 +    state->distbits = 5;
 124.117 +}
 124.118 +
 124.119 +/* Macros for inflateBack(): */
 124.120 +
 124.121 +/* Load returned state from inflate_fast() */
 124.122 +#define LOAD() \
 124.123 +    do { \
 124.124 +        put = strm->next_out; \
 124.125 +        left = strm->avail_out; \
 124.126 +        next = strm->next_in; \
 124.127 +        have = strm->avail_in; \
 124.128 +        hold = state->hold; \
 124.129 +        bits = state->bits; \
 124.130 +    } while (0)
 124.131 +
 124.132 +/* Set state from registers for inflate_fast() */
 124.133 +#define RESTORE() \
 124.134 +    do { \
 124.135 +        strm->next_out = put; \
 124.136 +        strm->avail_out = left; \
 124.137 +        strm->next_in = next; \
 124.138 +        strm->avail_in = have; \
 124.139 +        state->hold = hold; \
 124.140 +        state->bits = bits; \
 124.141 +    } while (0)
 124.142 +
 124.143 +/* Clear the input bit accumulator */
 124.144 +#define INITBITS() \
 124.145 +    do { \
 124.146 +        hold = 0; \
 124.147 +        bits = 0; \
 124.148 +    } while (0)
 124.149 +
 124.150 +/* Assure that some input is available.  If input is requested, but denied,
 124.151 +   then return a Z_BUF_ERROR from inflateBack(). */
 124.152 +#define PULL() \
 124.153 +    do { \
 124.154 +        if (have == 0) { \
 124.155 +            have = in(in_desc, &next); \
 124.156 +            if (have == 0) { \
 124.157 +                next = Z_NULL; \
 124.158 +                ret = Z_BUF_ERROR; \
 124.159 +                goto inf_leave; \
 124.160 +            } \
 124.161 +        } \
 124.162 +    } while (0)
 124.163 +
 124.164 +/* Get a byte of input into the bit accumulator, or return from inflateBack()
 124.165 +   with an error if there is no input available. */
 124.166 +#define PULLBYTE() \
 124.167 +    do { \
 124.168 +        PULL(); \
 124.169 +        have--; \
 124.170 +        hold += (unsigned long)(*next++) << bits; \
 124.171 +        bits += 8; \
 124.172 +    } while (0)
 124.173 +
 124.174 +/* Assure that there are at least n bits in the bit accumulator.  If there is
 124.175 +   not enough available input to do that, then return from inflateBack() with
 124.176 +   an error. */
 124.177 +#define NEEDBITS(n) \
 124.178 +    do { \
 124.179 +        while (bits < (unsigned)(n)) \
 124.180 +            PULLBYTE(); \
 124.181 +    } while (0)
 124.182 +
 124.183 +/* Return the low n bits of the bit accumulator (n < 16) */
 124.184 +#define BITS(n) \
 124.185 +    ((unsigned)hold & ((1U << (n)) - 1))
 124.186 +
 124.187 +/* Remove n bits from the bit accumulator */
 124.188 +#define DROPBITS(n) \
 124.189 +    do { \
 124.190 +        hold >>= (n); \
 124.191 +        bits -= (unsigned)(n); \
 124.192 +    } while (0)
 124.193 +
 124.194 +/* Remove zero to seven bits as needed to go to a byte boundary */
 124.195 +#define BYTEBITS() \
 124.196 +    do { \
 124.197 +        hold >>= bits & 7; \
 124.198 +        bits -= bits & 7; \
 124.199 +    } while (0)
 124.200 +
 124.201 +/* Assure that some output space is available, by writing out the window
 124.202 +   if it's full.  If the write fails, return from inflateBack() with a
 124.203 +   Z_BUF_ERROR. */
 124.204 +#define ROOM() \
 124.205 +    do { \
 124.206 +        if (left == 0) { \
 124.207 +            put = state->window; \
 124.208 +            left = state->wsize; \
 124.209 +            state->whave = left; \
 124.210 +            if (out(out_desc, put, left)) { \
 124.211 +                ret = Z_BUF_ERROR; \
 124.212 +                goto inf_leave; \
 124.213 +            } \
 124.214 +        } \
 124.215 +    } while (0)
 124.216 +
 124.217 +/*
 124.218 +   strm provides the memory allocation functions and window buffer on input,
 124.219 +   and provides information on the unused input on return.  For Z_DATA_ERROR
 124.220 +   returns, strm will also provide an error message.
 124.221 +
 124.222 +   in() and out() are the call-back input and output functions.  When
 124.223 +   inflateBack() needs more input, it calls in().  When inflateBack() has
 124.224 +   filled the window with output, or when it completes with data in the
 124.225 +   window, it calls out() to write out the data.  The application must not
 124.226 +   change the provided input until in() is called again or inflateBack()
 124.227 +   returns.  The application must not change the window/output buffer until
 124.228 +   inflateBack() returns.
 124.229 +
 124.230 +   in() and out() are called with a descriptor parameter provided in the
 124.231 +   inflateBack() call.  This parameter can be a structure that provides the
 124.232 +   information required to do the read or write, as well as accumulated
 124.233 +   information on the input and output such as totals and check values.
 124.234 +
 124.235 +   in() should return zero on failure.  out() should return non-zero on
 124.236 +   failure.  If either in() or out() fails, than inflateBack() returns a
 124.237 +   Z_BUF_ERROR.  strm->next_in can be checked for Z_NULL to see whether it
 124.238 +   was in() or out() that caused in the error.  Otherwise,  inflateBack()
 124.239 +   returns Z_STREAM_END on success, Z_DATA_ERROR for an deflate format
 124.240 +   error, or Z_MEM_ERROR if it could not allocate memory for the state.
 124.241 +   inflateBack() can also return Z_STREAM_ERROR if the input parameters
 124.242 +   are not correct, i.e. strm is Z_NULL or the state was not initialized.
 124.243 + */
 124.244 +int ZEXPORT inflateBack(strm, in, in_desc, out, out_desc)
 124.245 +z_streamp strm;
 124.246 +in_func in;
 124.247 +void FAR *in_desc;
 124.248 +out_func out;
 124.249 +void FAR *out_desc;
 124.250 +{
 124.251 +    struct inflate_state FAR *state;
 124.252 +    unsigned char FAR *next;    /* next input */
 124.253 +    unsigned char FAR *put;     /* next output */
 124.254 +    unsigned have, left;        /* available input and output */
 124.255 +    unsigned long hold;         /* bit buffer */
 124.256 +    unsigned bits;              /* bits in bit buffer */
 124.257 +    unsigned copy;              /* number of stored or match bytes to copy */
 124.258 +    unsigned char FAR *from;    /* where to copy match bytes from */
 124.259 +    code this;                  /* current decoding table entry */
 124.260 +    code last;                  /* parent table entry */
 124.261 +    unsigned len;               /* length to copy for repeats, bits to drop */
 124.262 +    int ret;                    /* return code */
 124.263 +    static const unsigned short order[19] = /* permutation of code lengths */
 124.264 +        {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
 124.265 +
 124.266 +    /* Check that the strm exists and that the state was initialized */
 124.267 +    if (strm == Z_NULL || strm->state == Z_NULL)
 124.268 +        return Z_STREAM_ERROR;
 124.269 +    state = (struct inflate_state FAR *)strm->state;
 124.270 +
 124.271 +    /* Reset the state */
 124.272 +    strm->msg = Z_NULL;
 124.273 +    state->mode = TYPE;
 124.274 +    state->last = 0;
 124.275 +    state->whave = 0;
 124.276 +    next = strm->next_in;
 124.277 +    have = next != Z_NULL ? strm->avail_in : 0;
 124.278 +    hold = 0;
 124.279 +    bits = 0;
 124.280 +    put = state->window;
 124.281 +    left = state->wsize;
 124.282 +
 124.283 +    /* Inflate until end of block marked as last */
 124.284 +    for (;;)
 124.285 +        switch (state->mode) {
 124.286 +        case TYPE:
 124.287 +            /* determine and dispatch block type */
 124.288 +            if (state->last) {
 124.289 +                BYTEBITS();
 124.290 +                state->mode = DONE;
 124.291 +                break;
 124.292 +            }
 124.293 +            NEEDBITS(3);
 124.294 +            state->last = BITS(1);
 124.295 +            DROPBITS(1);
 124.296 +            switch (BITS(2)) {
 124.297 +            case 0:                             /* stored block */
 124.298 +                Tracev((stderr, "inflate:     stored block%s\n",
 124.299 +                        state->last ? " (last)" : ""));
 124.300 +                state->mode = STORED;
 124.301 +                break;
 124.302 +            case 1:                             /* fixed block */
 124.303 +                fixedtables(state);
 124.304 +                Tracev((stderr, "inflate:     fixed codes block%s\n",
 124.305 +                        state->last ? " (last)" : ""));
 124.306 +                state->mode = LEN;              /* decode codes */
 124.307 +                break;
 124.308 +            case 2:                             /* dynamic block */
 124.309 +                Tracev((stderr, "inflate:     dynamic codes block%s\n",
 124.310 +                        state->last ? " (last)" : ""));
 124.311 +                state->mode = TABLE;
 124.312 +                break;
 124.313 +            case 3:
 124.314 +                strm->msg = (char *)"invalid block type";
 124.315 +                state->mode = BAD;
 124.316 +            }
 124.317 +            DROPBITS(2);
 124.318 +            break;
 124.319 +
 124.320 +        case STORED:
 124.321 +            /* get and verify stored block length */
 124.322 +            BYTEBITS();                         /* go to byte boundary */
 124.323 +            NEEDBITS(32);
 124.324 +            if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) {
 124.325 +                strm->msg = (char *)"invalid stored block lengths";
 124.326 +                state->mode = BAD;
 124.327 +                break;
 124.328 +            }
 124.329 +            state->length = (unsigned)hold & 0xffff;
 124.330 +            Tracev((stderr, "inflate:       stored length %u\n",
 124.331 +                    state->length));
 124.332 +            INITBITS();
 124.333 +
 124.334 +            /* copy stored block from input to output */
 124.335 +            while (state->length != 0) {
 124.336 +                copy = state->length;
 124.337 +                PULL();
 124.338 +                ROOM();
 124.339 +                if (copy > have) copy = have;
 124.340 +                if (copy > left) copy = left;
 124.341 +                zmemcpy(put, next, copy);
 124.342 +                have -= copy;
 124.343 +                next += copy;
 124.344 +                left -= copy;
 124.345 +                put += copy;
 124.346 +                state->length -= copy;
 124.347 +            }
 124.348 +            Tracev((stderr, "inflate:       stored end\n"));
 124.349 +            state->mode = TYPE;
 124.350 +            break;
 124.351 +
 124.352 +        case TABLE:
 124.353 +            /* get dynamic table entries descriptor */
 124.354 +            NEEDBITS(14);
 124.355 +            state->nlen = BITS(5) + 257;
 124.356 +            DROPBITS(5);
 124.357 +            state->ndist = BITS(5) + 1;
 124.358 +            DROPBITS(5);
 124.359 +            state->ncode = BITS(4) + 4;
 124.360 +            DROPBITS(4);
 124.361 +#ifndef PKZIP_BUG_WORKAROUND
 124.362 +            if (state->nlen > 286 || state->ndist > 30) {
 124.363 +                strm->msg = (char *)"too many length or distance symbols";
 124.364 +                state->mode = BAD;
 124.365 +                break;
 124.366 +            }
 124.367 +#endif
 124.368 +            Tracev((stderr, "inflate:       table sizes ok\n"));
 124.369 +
 124.370 +            /* get code length code lengths (not a typo) */
 124.371 +            state->have = 0;
 124.372 +            while (state->have < state->ncode) {
 124.373 +                NEEDBITS(3);
 124.374 +                state->lens[order[state->have++]] = (unsigned short)BITS(3);
 124.375 +                DROPBITS(3);
 124.376 +            }
 124.377 +            while (state->have < 19)
 124.378 +                state->lens[order[state->have++]] = 0;
 124.379 +            state->next = state->codes;
 124.380 +            state->lencode = (code const FAR *)(state->next);
 124.381 +            state->lenbits = 7;
 124.382 +            ret = inflate_table(CODES, state->lens, 19, &(state->next),
 124.383 +                                &(state->lenbits), state->work);
 124.384 +            if (ret) {
 124.385 +                strm->msg = (char *)"invalid code lengths set";
 124.386 +                state->mode = BAD;
 124.387 +                break;
 124.388 +            }
 124.389 +            Tracev((stderr, "inflate:       code lengths ok\n"));
 124.390 +
 124.391 +            /* get length and distance code code lengths */
 124.392 +            state->have = 0;
 124.393 +            while (state->have < state->nlen + state->ndist) {
 124.394 +                for (;;) {
 124.395 +                    this = state->lencode[BITS(state->lenbits)];
 124.396 +                    if ((unsigned)(this.bits) <= bits) break;
 124.397 +                    PULLBYTE();
 124.398 +                }
 124.399 +                if (this.val < 16) {
 124.400 +                    NEEDBITS(this.bits);
 124.401 +                    DROPBITS(this.bits);
 124.402 +                    state->lens[state->have++] = this.val;
 124.403 +                }
 124.404 +                else {
 124.405 +                    if (this.val == 16) {
 124.406 +                        NEEDBITS(this.bits + 2);
 124.407 +                        DROPBITS(this.bits);
 124.408 +                        if (state->have == 0) {
 124.409 +                            strm->msg = (char *)"invalid bit length repeat";
 124.410 +                            state->mode = BAD;
 124.411 +                            break;
 124.412 +                        }
 124.413 +                        len = (unsigned)(state->lens[state->have - 1]);
 124.414 +                        copy = 3 + BITS(2);
 124.415 +                        DROPBITS(2);
 124.416 +                    }
 124.417 +                    else if (this.val == 17) {
 124.418 +                        NEEDBITS(this.bits + 3);
 124.419 +                        DROPBITS(this.bits);
 124.420 +                        len = 0;
 124.421 +                        copy = 3 + BITS(3);
 124.422 +                        DROPBITS(3);
 124.423 +                    }
 124.424 +                    else {
 124.425 +                        NEEDBITS(this.bits + 7);
 124.426 +                        DROPBITS(this.bits);
 124.427 +                        len = 0;
 124.428 +                        copy = 11 + BITS(7);
 124.429 +                        DROPBITS(7);
 124.430 +                    }
 124.431 +                    if (state->have + copy > state->nlen + state->ndist) {
 124.432 +                        strm->msg = (char *)"invalid bit length repeat";
 124.433 +                        state->mode = BAD;
 124.434 +                        break;
 124.435 +                    }
 124.436 +                    while (copy--)
 124.437 +                        state->lens[state->have++] = (unsigned short)len;
 124.438 +                }
 124.439 +            }
 124.440 +
 124.441 +            /* handle error breaks in while */
 124.442 +            if (state->mode == BAD) break;
 124.443 +
 124.444 +            /* build code tables */
 124.445 +            state->next = state->codes;
 124.446 +            state->lencode = (code const FAR *)(state->next);
 124.447 +            state->lenbits = 9;
 124.448 +            ret = inflate_table(LENS, state->lens, state->nlen, &(state->next),
 124.449 +                                &(state->lenbits), state->work);
 124.450 +            if (ret) {
 124.451 +                strm->msg = (char *)"invalid literal/lengths set";
 124.452 +                state->mode = BAD;
 124.453 +                break;
 124.454 +            }
 124.455 +            state->distcode = (code const FAR *)(state->next);
 124.456 +            state->distbits = 6;
 124.457 +            ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist,
 124.458 +                            &(state->next), &(state->distbits), state->work);
 124.459 +            if (ret) {
 124.460 +                strm->msg = (char *)"invalid distances set";
 124.461 +                state->mode = BAD;
 124.462 +                break;
 124.463 +            }
 124.464 +            Tracev((stderr, "inflate:       codes ok\n"));
 124.465 +            state->mode = LEN;
 124.466 +
 124.467 +        case LEN:
 124.468 +            /* use inflate_fast() if we have enough input and output */
 124.469 +            if (have >= 6 && left >= 258) {
 124.470 +                RESTORE();
 124.471 +                if (state->whave < state->wsize)
 124.472 +                    state->whave = state->wsize - left;
 124.473 +                inflate_fast(strm, state->wsize);
 124.474 +                LOAD();
 124.475 +                break;
 124.476 +            }
 124.477 +
 124.478 +            /* get a literal, length, or end-of-block code */
 124.479 +            for (;;) {
 124.480 +                this = state->lencode[BITS(state->lenbits)];
 124.481 +                if ((unsigned)(this.bits) <= bits) break;
 124.482 +                PULLBYTE();
 124.483 +            }
 124.484 +            if (this.op && (this.op & 0xf0) == 0) {
 124.485 +                last = this;
 124.486 +                for (;;) {
 124.487 +                    this = state->lencode[last.val +
 124.488 +                            (BITS(last.bits + last.op) >> last.bits)];
 124.489 +                    if ((unsigned)(last.bits + this.bits) <= bits) break;
 124.490 +                    PULLBYTE();
 124.491 +                }
 124.492 +                DROPBITS(last.bits);
 124.493 +            }
 124.494 +            DROPBITS(this.bits);
 124.495 +            state->length = (unsigned)this.val;
 124.496 +
 124.497 +            /* process literal */
 124.498 +            if (this.op == 0) {
 124.499 +                Tracevv((stderr, this.val >= 0x20 && this.val < 0x7f ?
 124.500 +                        "inflate:         literal '%c'\n" :
 124.501 +                        "inflate:         literal 0x%02x\n", this.val));
 124.502 +                ROOM();
 124.503 +                *put++ = (unsigned char)(state->length);
 124.504 +                left--;
 124.505 +                state->mode = LEN;
 124.506 +                break;
 124.507 +            }
 124.508 +
 124.509 +            /* process end of block */
 124.510 +            if (this.op & 32) {
 124.511 +                Tracevv((stderr, "inflate:         end of block\n"));
 124.512 +                state->mode = TYPE;
 124.513 +                break;
 124.514 +            }
 124.515 +
 124.516 +            /* invalid code */
 124.517 +            if (this.op & 64) {
 124.518 +                strm->msg = (char *)"invalid literal/length code";
 124.519 +                state->mode = BAD;
 124.520 +                break;
 124.521 +            }
 124.522 +
 124.523 +            /* length code -- get extra bits, if any */
 124.524 +            state->extra = (unsigned)(this.op) & 15;
 124.525 +            if (state->extra != 0) {
 124.526 +                NEEDBITS(state->extra);
 124.527 +                state->length += BITS(state->extra);
 124.528 +                DROPBITS(state->extra);
 124.529 +            }
 124.530 +            Tracevv((stderr, "inflate:         length %u\n", state->length));
 124.531 +
 124.532 +            /* get distance code */
 124.533 +            for (;;) {
 124.534 +                this = state->distcode[BITS(state->distbits)];
 124.535 +                if ((unsigned)(this.bits) <= bits) break;
 124.536 +                PULLBYTE();
 124.537 +            }
 124.538 +            if ((this.op & 0xf0) == 0) {
 124.539 +                last = this;
 124.540 +                for (;;) {
 124.541 +                    this = state->distcode[last.val +
 124.542 +                            (BITS(last.bits + last.op) >> last.bits)];
 124.543 +                    if ((unsigned)(last.bits + this.bits) <= bits) break;
 124.544 +                    PULLBYTE();
 124.545 +                }
 124.546 +                DROPBITS(last.bits);
 124.547 +            }
 124.548 +            DROPBITS(this.bits);
 124.549 +            if (this.op & 64) {
 124.550 +                strm->msg = (char *)"invalid distance code";
 124.551 +                state->mode = BAD;
 124.552 +                break;
 124.553 +            }
 124.554 +            state->offset = (unsigned)this.val;
 124.555 +
 124.556 +            /* get distance extra bits, if any */
 124.557 +            state->extra = (unsigned)(this.op) & 15;
 124.558 +            if (state->extra != 0) {
 124.559 +                NEEDBITS(state->extra);
 124.560 +                state->offset += BITS(state->extra);
 124.561 +                DROPBITS(state->extra);
 124.562 +            }
 124.563 +            if (state->offset > state->wsize - (state->whave < state->wsize ?
 124.564 +                                                left : 0)) {
 124.565 +                strm->msg = (char *)"invalid distance too far back";
 124.566 +                state->mode = BAD;
 124.567 +                break;
 124.568 +            }
 124.569 +            Tracevv((stderr, "inflate:         distance %u\n", state->offset));
 124.570 +
 124.571 +            /* copy match from window to output */
 124.572 +            do {
 124.573 +                ROOM();
 124.574 +                copy = state->wsize - state->offset;
 124.575 +                if (copy < left) {
 124.576 +                    from = put + copy;
 124.577 +                    copy = left - copy;
 124.578 +                }
 124.579 +                else {
 124.580 +                    from = put - state->offset;
 124.581 +                    copy = left;
 124.582 +                }
 124.583 +                if (copy > state->length) copy = state->length;
 124.584 +                state->length -= copy;
 124.585 +                left -= copy;
 124.586 +                do {
 124.587 +                    *put++ = *from++;
 124.588 +                } while (--copy);
 124.589 +            } while (state->length != 0);
 124.590 +            break;
 124.591 +
 124.592 +        case DONE:
 124.593 +            /* inflate stream terminated properly -- write leftover output */
 124.594 +            ret = Z_STREAM_END;
 124.595 +            if (left < state->wsize) {
 124.596 +                if (out(out_desc, state->window, state->wsize - left))
 124.597 +                    ret = Z_BUF_ERROR;
 124.598 +            }
 124.599 +            goto inf_leave;
 124.600 +
 124.601 +        case BAD:
 124.602 +            ret = Z_DATA_ERROR;
 124.603 +            goto inf_leave;
 124.604 +
 124.605 +        default:                /* can't happen, but makes compilers happy */
 124.606 +            ret = Z_STREAM_ERROR;
 124.607 +            goto inf_leave;
 124.608 +        }
 124.609 +
 124.610 +    /* Return unused input */
 124.611 +  inf_leave:
 124.612 +    strm->next_in = next;
 124.613 +    strm->avail_in = have;
 124.614 +    return ret;
 124.615 +}
 124.616 +
 124.617 +int ZEXPORT inflateBackEnd(strm)
 124.618 +z_streamp strm;
 124.619 +{
 124.620 +    if (strm == Z_NULL || strm->state == Z_NULL || strm->zfree == (free_func)0)
 124.621 +        return Z_STREAM_ERROR;
 124.622 +    ZFREE(strm, strm->state);
 124.623 +    strm->state = Z_NULL;
 124.624 +    Tracev((stderr, "inflate: end\n"));
 124.625 +    return Z_OK;
 124.626 +}
   125.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   125.2 +++ b/libs/zlib/inffast.c	Sat Sep 19 05:51:51 2015 +0300
   125.3 @@ -0,0 +1,318 @@
   125.4 +/* inffast.c -- fast decoding
   125.5 + * Copyright (C) 1995-2004 Mark Adler
   125.6 + * For conditions of distribution and use, see copyright notice in zlib.h
   125.7 + */
   125.8 +
   125.9 +#include "zutil.h"
  125.10 +#include "inftrees.h"
  125.11 +#include "inflate.h"
  125.12 +#include "inffast.h"
  125.13 +
  125.14 +#ifndef ASMINF
  125.15 +
  125.16 +/* Allow machine dependent optimization for post-increment or pre-increment.
  125.17 +   Based on testing to date,
  125.18 +   Pre-increment preferred for:
  125.19 +   - PowerPC G3 (Adler)
  125.20 +   - MIPS R5000 (Randers-Pehrson)
  125.21 +   Post-increment preferred for:
  125.22 +   - none
  125.23 +   No measurable difference:
  125.24 +   - Pentium III (Anderson)
  125.25 +   - M68060 (Nikl)
  125.26 + */
  125.27 +#ifdef POSTINC
  125.28 +#  define OFF 0
  125.29 +#  define PUP(a) *(a)++
  125.30 +#else
  125.31 +#  define OFF 1
  125.32 +#  define PUP(a) *++(a)
  125.33 +#endif
  125.34 +
  125.35 +/*
  125.36 +   Decode literal, length, and distance codes and write out the resulting
  125.37 +   literal and match bytes until either not enough input or output is
  125.38 +   available, an end-of-block is encountered, or a data error is encountered.
  125.39 +   When large enough input and output buffers are supplied to inflate(), for
  125.40 +   example, a 16K input buffer and a 64K output buffer, more than 95% of the
  125.41 +   inflate execution time is spent in this routine.
  125.42 +
  125.43 +   Entry assumptions:
  125.44 +
  125.45 +        state->mode == LEN
  125.46 +        strm->avail_in >= 6
  125.47 +        strm->avail_out >= 258
  125.48 +        start >= strm->avail_out
  125.49 +        state->bits < 8
  125.50 +
  125.51 +   On return, state->mode is one of:
  125.52 +
  125.53 +        LEN -- ran out of enough output space or enough available input
  125.54 +        TYPE -- reached end of block code, inflate() to interpret next block
  125.55 +        BAD -- error in block data
  125.56 +
  125.57 +   Notes:
  125.58 +
  125.59 +    - The maximum input bits used by a length/distance pair is 15 bits for the
  125.60 +      length code, 5 bits for the length extra, 15 bits for the distance code,
  125.61 +      and 13 bits for the distance extra.  This totals 48 bits, or six bytes.
  125.62 +      Therefore if strm->avail_in >= 6, then there is enough input to avoid
  125.63 +      checking for available input while decoding.
  125.64 +
  125.65 +    - The maximum bytes that a single length/distance pair can output is 258
  125.66 +      bytes, which is the maximum length that can be coded.  inflate_fast()
  125.67 +      requires strm->avail_out >= 258 for each loop to avoid checking for
  125.68 +      output space.
  125.69 + */
  125.70 +void inflate_fast(strm, start)
  125.71 +z_streamp strm;
  125.72 +unsigned start;         /* inflate()'s starting value for strm->avail_out */
  125.73 +{
  125.74 +    struct inflate_state FAR *state;
  125.75 +    unsigned char FAR *in;      /* local strm->next_in */
  125.76 +    unsigned char FAR *last;    /* while in < last, enough input available */
  125.77 +    unsigned char FAR *out;     /* local strm->next_out */
  125.78 +    unsigned char FAR *beg;     /* inflate()'s initial strm->next_out */
  125.79 +    unsigned char FAR *end;     /* while out < end, enough space available */
  125.80 +#ifdef INFLATE_STRICT
  125.81 +    unsigned dmax;              /* maximum distance from zlib header */
  125.82 +#endif
  125.83 +    unsigned wsize;             /* window size or zero if not using window */
  125.84 +    unsigned whave;             /* valid bytes in the window */
  125.85 +    unsigned write;             /* window write index */
  125.86 +    unsigned char FAR *window;  /* allocated sliding window, if wsize != 0 */
  125.87 +    unsigned long hold;         /* local strm->hold */
  125.88 +    unsigned bits;              /* local strm->bits */
  125.89 +    code const FAR *lcode;      /* local strm->lencode */
  125.90 +    code const FAR *dcode;      /* local strm->distcode */
  125.91 +    unsigned lmask;             /* mask for first level of length codes */
  125.92 +    unsigned dmask;             /* mask for first level of distance codes */
  125.93 +    code this;                  /* retrieved table entry */
  125.94 +    unsigned op;                /* code bits, operation, extra bits, or */
  125.95 +                                /*  window position, window bytes to copy */
  125.96 +    unsigned len;               /* match length, unused bytes */
  125.97 +    unsigned dist;              /* match distance */
  125.98 +    unsigned char FAR *from;    /* where to copy match from */
  125.99 +
 125.100 +    /* copy state to local variables */
 125.101 +    state = (struct inflate_state FAR *)strm->state;
 125.102 +    in = strm->next_in - OFF;
 125.103 +    last = in + (strm->avail_in - 5);
 125.104 +    out = strm->next_out - OFF;
 125.105 +    beg = out - (start - strm->avail_out);
 125.106 +    end = out + (strm->avail_out - 257);
 125.107 +#ifdef INFLATE_STRICT
 125.108 +    dmax = state->dmax;
 125.109 +#endif
 125.110 +    wsize = state->wsize;
 125.111 +    whave = state->whave;
 125.112 +    write = state->write;
 125.113 +    window = state->window;
 125.114 +    hold = state->hold;
 125.115 +    bits = state->bits;
 125.116 +    lcode = state->lencode;
 125.117 +    dcode = state->distcode;
 125.118 +    lmask = (1U << state->lenbits) - 1;
 125.119 +    dmask = (1U << state->distbits) - 1;
 125.120 +
 125.121 +    /* decode literals and length/distances until end-of-block or not enough
 125.122 +       input data or output space */
 125.123 +    do {
 125.124 +        if (bits < 15) {
 125.125 +            hold += (unsigned long)(PUP(in)) << bits;
 125.126 +            bits += 8;
 125.127 +            hold += (unsigned long)(PUP(in)) << bits;
 125.128 +            bits += 8;
 125.129 +        }
 125.130 +        this = lcode[hold & lmask];
 125.131 +      dolen:
 125.132 +        op = (unsigned)(this.bits);
 125.133 +        hold >>= op;
 125.134 +        bits -= op;
 125.135 +        op = (unsigned)(this.op);
 125.136 +        if (op == 0) {                          /* literal */
 125.137 +            Tracevv((stderr, this.val >= 0x20 && this.val < 0x7f ?
 125.138 +                    "inflate:         literal '%c'\n" :
 125.139 +                    "inflate:         literal 0x%02x\n", this.val));
 125.140 +            PUP(out) = (unsigned char)(this.val);
 125.141 +        }
 125.142 +        else if (op & 16) {                     /* length base */
 125.143 +            len = (unsigned)(this.val);
 125.144 +            op &= 15;                           /* number of extra bits */
 125.145 +            if (op) {
 125.146 +                if (bits < op) {
 125.147 +                    hold += (unsigned long)(PUP(in)) << bits;
 125.148 +                    bits += 8;
 125.149 +                }
 125.150 +                len += (unsigned)hold & ((1U << op) - 1);
 125.151 +                hold >>= op;
 125.152 +                bits -= op;
 125.153 +            }
 125.154 +            Tracevv((stderr, "inflate:         length %u\n", len));
 125.155 +            if (bits < 15) {
 125.156 +                hold += (unsigned long)(PUP(in)) << bits;
 125.157 +                bits += 8;
 125.158 +                hold += (unsigned long)(PUP(in)) << bits;
 125.159 +                bits += 8;
 125.160 +            }
 125.161 +            this = dcode[hold & dmask];
 125.162 +          dodist:
 125.163 +            op = (unsigned)(this.bits);
 125.164 +            hold >>= op;
 125.165 +            bits -= op;
 125.166 +            op = (unsigned)(this.op);
 125.167 +            if (op & 16) {                      /* distance base */
 125.168 +                dist = (unsigned)(this.val);
 125.169 +                op &= 15;                       /* number of extra bits */
 125.170 +                if (bits < op) {
 125.171 +                    hold += (unsigned long)(PUP(in)) << bits;
 125.172 +                    bits += 8;
 125.173 +                    if (bits < op) {
 125.174 +                        hold += (unsigned long)(PUP(in)) << bits;
 125.175 +                        bits += 8;
 125.176 +                    }
 125.177 +                }
 125.178 +                dist += (unsigned)hold & ((1U << op) - 1);
 125.179 +#ifdef INFLATE_STRICT
 125.180 +                if (dist > dmax) {
 125.181 +                    strm->msg = (char *)"invalid distance too far back";
 125.182 +                    state->mode = BAD;
 125.183 +                    break;
 125.184 +                }
 125.185 +#endif
 125.186 +                hold >>= op;
 125.187 +                bits -= op;
 125.188 +                Tracevv((stderr, "inflate:         distance %u\n", dist));
 125.189 +                op = (unsigned)(out - beg);     /* max distance in output */
 125.190 +                if (dist > op) {                /* see if copy from window */
 125.191 +                    op = dist - op;             /* distance back in window */
 125.192 +                    if (op > whave) {
 125.193 +                        strm->msg = (char *)"invalid distance too far back";
 125.194 +                        state->mode = BAD;
 125.195 +                        break;
 125.196 +                    }
 125.197 +                    from = window - OFF;
 125.198 +                    if (write == 0) {           /* very common case */
 125.199 +                        from += wsize - op;
 125.200 +                        if (op < len) {         /* some from window */
 125.201 +                            len -= op;
 125.202 +                            do {
 125.203 +                                PUP(out) = PUP(from);
 125.204 +                            } while (--op);
 125.205 +                            from = out - dist;  /* rest from output */
 125.206 +                        }
 125.207 +                    }
 125.208 +                    else if (write < op) {      /* wrap around window */
 125.209 +                        from += wsize + write - op;
 125.210 +                        op -= write;
 125.211 +                        if (op < len) {         /* some from end of window */
 125.212 +                            len -= op;
 125.213 +                            do {
 125.214 +                                PUP(out) = PUP(from);
 125.215 +                            } while (--op);
 125.216 +                            from = window - OFF;
 125.217 +                            if (write < len) {  /* some from start of window */
 125.218 +                                op = write;
 125.219 +                                len -= op;
 125.220 +                                do {
 125.221 +                                    PUP(out) = PUP(from);
 125.222 +                                } while (--op);
 125.223 +                                from = out - dist;      /* rest from output */
 125.224 +                            }
 125.225 +                        }
 125.226 +                    }
 125.227 +                    else {                      /* contiguous in window */
 125.228 +                        from += write - op;
 125.229 +                        if (op < len) {         /* some from window */
 125.230 +                            len -= op;
 125.231 +                            do {
 125.232 +                                PUP(out) = PUP(from);
 125.233 +                            } while (--op);
 125.234 +                            from = out - dist;  /* rest from output */
 125.235 +                        }
 125.236 +                    }
 125.237 +                    while (len > 2) {
 125.238 +                        PUP(out) = PUP(from);
 125.239 +                        PUP(out) = PUP(from);
 125.240 +                        PUP(out) = PUP(from);
 125.241 +                        len -= 3;
 125.242 +                    }
 125.243 +                    if (len) {
 125.244 +                        PUP(out) = PUP(from);
 125.245 +                        if (len > 1)
 125.246 +                            PUP(out) = PUP(from);
 125.247 +                    }
 125.248 +                }
 125.249 +                else {
 125.250 +                    from = out - dist;          /* copy direct from output */
 125.251 +                    do {                        /* minimum length is three */
 125.252 +                        PUP(out) = PUP(from);
 125.253 +                        PUP(out) = PUP(from);
 125.254 +                        PUP(out) = PUP(from);
 125.255 +                        len -= 3;
 125.256 +                    } while (len > 2);
 125.257 +                    if (len) {
 125.258 +                        PUP(out) = PUP(from);
 125.259 +                        if (len > 1)
 125.260 +                            PUP(out) = PUP(from);
 125.261 +                    }
 125.262 +                }
 125.263 +            }
 125.264 +            else if ((op & 64) == 0) {          /* 2nd level distance code */
 125.265 +                this = dcode[this.val + (hold & ((1U << op) - 1))];
 125.266 +                goto dodist;
 125.267 +            }
 125.268 +            else {
 125.269 +                strm->msg = (char *)"invalid distance code";
 125.270 +                state->mode = BAD;
 125.271 +                break;
 125.272 +            }
 125.273 +        }
 125.274 +        else if ((op & 64) == 0) {              /* 2nd level length code */
 125.275 +            this = lcode[this.val + (hold & ((1U << op) - 1))];
 125.276 +            goto dolen;
 125.277 +        }
 125.278 +        else if (op & 32) {                     /* end-of-block */
 125.279 +            Tracevv((stderr, "inflate:         end of block\n"));
 125.280 +            state->mode = TYPE;
 125.281 +            break;
 125.282 +        }
 125.283 +        else {
 125.284 +            strm->msg = (char *)"invalid literal/length code";
 125.285 +            state->mode = BAD;
 125.286 +            break;
 125.287 +        }
 125.288 +    } while (in < last && out < end);
 125.289 +
 125.290 +    /* return unused bytes (on entry, bits < 8, so in won't go too far back) */
 125.291 +    len = bits >> 3;
 125.292 +    in -= len;
 125.293 +    bits -= len << 3;
 125.294 +    hold &= (1U << bits) - 1;
 125.295 +
 125.296 +    /* update state and return */
 125.297 +    strm->next_in = in + OFF;
 125.298 +    strm->next_out = out + OFF;
 125.299 +    strm->avail_in = (unsigned)(in < last ? 5 + (last - in) : 5 - (in - last));
 125.300 +    strm->avail_out = (unsigned)(out < end ?
 125.301 +                                 257 + (end - out) : 257 - (out - end));
 125.302 +    state->hold = hold;
 125.303 +    state->bits = bits;
 125.304 +    return;
 125.305 +}
 125.306 +
 125.307 +/*
 125.308 +   inflate_fast() speedups that turned out slower (on a PowerPC G3 750CXe):
 125.309 +   - Using bit fields for code structure
 125.310 +   - Different op definition to avoid & for extra bits (do & for table bits)
 125.311 +   - Three separate decoding do-loops for direct, window, and write == 0
 125.312 +   - Special case for distance > 1 copies to do overlapped load and store copy
 125.313 +   - Explicit branch predictions (based on measured branch probabilities)
 125.314 +   - Deferring match copy and interspersed it with decoding subsequent codes
 125.315 +   - Swapping literal/length else
 125.316 +   - Swapping window/direct else
 125.317 +   - Larger unrolled copy loops (three is about right)
 125.318 +   - Moving len -= 3 statement into middle of loop
 125.319 + */
 125.320 +
 125.321 +#endif /* !ASMINF */
   126.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   126.2 +++ b/libs/zlib/inffast.h	Sat Sep 19 05:51:51 2015 +0300
   126.3 @@ -0,0 +1,11 @@
   126.4 +/* inffast.h -- header to use inffast.c
   126.5 + * Copyright (C) 1995-2003 Mark Adler
   126.6 + * For conditions of distribution and use, see copyright notice in zlib.h
   126.7 + */
   126.8 +
   126.9 +/* WARNING: this file should *not* be used by applications. It is
  126.10 +   part of the implementation of the compression library and is
  126.11 +   subject to change. Applications should only use zlib.h.
  126.12 + */
  126.13 +
  126.14 +void inflate_fast OF((z_streamp strm, unsigned start));
   127.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   127.2 +++ b/libs/zlib/inffixed.h	Sat Sep 19 05:51:51 2015 +0300
   127.3 @@ -0,0 +1,94 @@
   127.4 +    /* inffixed.h -- table for decoding fixed codes
   127.5 +     * Generated automatically by makefixed().
   127.6 +     */
   127.7 +
   127.8 +    /* WARNING: this file should *not* be used by applications. It
   127.9 +       is part of the implementation of the compression library and
  127.10 +       is subject to change. Applications should only use zlib.h.
  127.11 +     */
  127.12 +
  127.13 +    static const code lenfix[512] = {
  127.14 +        {96,7,0},{0,8,80},{0,8,16},{20,8,115},{18,7,31},{0,8,112},{0,8,48},
  127.15 +        {0,9,192},{16,7,10},{0,8,96},{0,8,32},{0,9,160},{0,8,0},{0,8,128},
  127.16 +        {0,8,64},{0,9,224},{16,7,6},{0,8,88},{0,8,24},{0,9,144},{19,7,59},
  127.17 +        {0,8,120},{0,8,56},{0,9,208},{17,7,17},{0,8,104},{0,8,40},{0,9,176},
  127.18 +        {0,8,8},{0,8,136},{0,8,72},{0,9,240},{16,7,4},{0,8,84},{0,8,20},
  127.19 +        {21,8,227},{19,7,43},{0,8,116},{0,8,52},{0,9,200},{17,7,13},{0,8,100},
  127.20 +        {0,8,36},{0,9,168},{0,8,4},{0,8,132},{0,8,68},{0,9,232},{16,7,8},
  127.21 +        {0,8,92},{0,8,28},{0,9,152},{20,7,83},{0,8,124},{0,8,60},{0,9,216},
  127.22 +        {18,7,23},{0,8,108},{0,8,44},{0,9,184},{0,8,12},{0,8,140},{0,8,76},
  127.23 +        {0,9,248},{16,7,3},{0,8,82},{0,8,18},{21,8,163},{19,7,35},{0,8,114},
  127.24 +        {0,8,50},{0,9,196},{17,7,11},{0,8,98},{0,8,34},{0,9,164},{0,8,2},
  127.25 +        {0,8,130},{0,8,66},{0,9,228},{16,7,7},{0,8,90},{0,8,26},{0,9,148},
  127.26 +        {20,7,67},{0,8,122},{0,8,58},{0,9,212},{18,7,19},{0,8,106},{0,8,42},
  127.27 +        {0,9,180},{0,8,10},{0,8,138},{0,8,74},{0,9,244},{16,7,5},{0,8,86},
  127.28 +        {0,8,22},{64,8,0},{19,7,51},{0,8,118},{0,8,54},{0,9,204},{17,7,15},
  127.29 +        {0,8,102},{0,8,38},{0,9,172},{0,8,6},{0,8,134},{0,8,70},{0,9,236},
  127.30 +        {16,7,9},{0,8,94},{0,8,30},{0,9,156},{20,7,99},{0,8,126},{0,8,62},
  127.31 +        {0,9,220},{18,7,27},{0,8,110},{0,8,46},{0,9,188},{0,8,14},{0,8,142},
  127.32 +        {0,8,78},{0,9,252},{96,7,0},{0,8,81},{0,8,17},{21,8,131},{18,7,31},
  127.33 +        {0,8,113},{0,8,49},{0,9,194},{16,7,10},{0,8,97},{0,8,33},{0,9,162},
  127.34 +        {0,8,1},{0,8,129},{0,8,65},{0,9,226},{16,7,6},{0,8,89},{0,8,25},
  127.35 +        {0,9,146},{19,7,59},{0,8,121},{0,8,57},{0,9,210},{17,7,17},{0,8,105},
  127.36 +        {0,8,41},{0,9,178},{0,8,9},{0,8,137},{0,8,73},{0,9,242},{16,7,4},
  127.37 +        {0,8,85},{0,8,21},{16,8,258},{19,7,43},{0,8,117},{0,8,53},{0,9,202},
  127.38 +        {17,7,13},{0,8,101},{0,8,37},{0,9,170},{0,8,5},{0,8,133},{0,8,69},
  127.39 +        {0,9,234},{16,7,8},{0,8,93},{0,8,29},{0,9,154},{20,7,83},{0,8,125},
  127.40 +        {0,8,61},{0,9,218},{18,7,23},{0,8,109},{0,8,45},{0,9,186},{0,8,13},
  127.41 +        {0,8,141},{0,8,77},{0,9,250},{16,7,3},{0,8,83},{0,8,19},{21,8,195},
  127.42 +        {19,7,35},{0,8,115},{0,8,51},{0,9,198},{17,7,11},{0,8,99},{0,8,35},
  127.43 +        {0,9,166},{0,8,3},{0,8,131},{0,8,67},{0,9,230},{16,7,7},{0,8,91},
  127.44 +        {0,8,27},{0,9,150},{20,7,67},{0,8,123},{0,8,59},{0,9,214},{18,7,19},
  127.45 +        {0,8,107},{0,8,43},{0,9,182},{0,8,11},{0,8,139},{0,8,75},{0,9,246},
  127.46 +        {16,7,5},{0,8,87},{0,8,23},{64,8,0},{19,7,51},{0,8,119},{0,8,55},
  127.47 +        {0,9,206},{17,7,15},{0,8,103},{0,8,39},{0,9,174},{0,8,7},{0,8,135},
  127.48 +        {0,8,71},{0,9,238},{16,7,9},{0,8,95},{0,8,31},{0,9,158},{20,7,99},
  127.49 +        {0,8,127},{0,8,63},{0,9,222},{18,7,27},{0,8,111},{0,8,47},{0,9,190},
  127.50 +        {0,8,15},{0,8,143},{0,8,79},{0,9,254},{96,7,0},{0,8,80},{0,8,16},
  127.51 +        {20,8,115},{18,7,31},{0,8,112},{0,8,48},{0,9,193},{16,7,10},{0,8,96},
  127.52 +        {0,8,32},{0,9,161},{0,8,0},{0,8,128},{0,8,64},{0,9,225},{16,7,6},
  127.53 +        {0,8,88},{0,8,24},{0,9,145},{19,7,59},{0,8,120},{0,8,56},{0,9,209},
  127.54 +        {17,7,17},{0,8,104},{0,8,40},{0,9,177},{0,8,8},{0,8,136},{0,8,72},
  127.55 +        {0,9,241},{16,7,4},{0,8,84},{0,8,20},{21,8,227},{19,7,43},{0,8,116},
  127.56 +        {0,8,52},{0,9,201},{17,7,13},{0,8,100},{0,8,36},{0,9,169},{0,8,4},
  127.57 +        {0,8,132},{0,8,68},{0,9,233},{16,7,8},{0,8,92},{0,8,28},{0,9,153},
  127.58 +        {20,7,83},{0,8,124},{0,8,60},{0,9,217},{18,7,23},{0,8,108},{0,8,44},
  127.59 +        {0,9,185},{0,8,12},{0,8,140},{0,8,76},{0,9,249},{16,7,3},{0,8,82},
  127.60 +        {0,8,18},{21,8,163},{19,7,35},{0,8,114},{0,8,50},{0,9,197},{17,7,11},
  127.61 +        {0,8,98},{0,8,34},{0,9,165},{0,8,2},{0,8,130},{0,8,66},{0,9,229},
  127.62 +        {16,7,7},{0,8,90},{0,8,26},{0,9,149},{20,7,67},{0,8,122},{0,8,58},
  127.63 +        {0,9,213},{18,7,19},{0,8,106},{0,8,42},{0,9,181},{0,8,10},{0,8,138},
  127.64 +        {0,8,74},{0,9,245},{16,7,5},{0,8,86},{0,8,22},{64,8,0},{19,7,51},
  127.65 +        {0,8,118},{0,8,54},{0,9,205},{17,7,15},{0,8,102},{0,8,38},{0,9,173},
  127.66 +        {0,8,6},{0,8,134},{0,8,70},{0,9,237},{16,7,9},{0,8,94},{0,8,30},
  127.67 +        {0,9,157},{20,7,99},{0,8,126},{0,8,62},{0,9,221},{18,7,27},{0,8,110},
  127.68 +        {0,8,46},{0,9,189},{0,8,14},{0,8,142},{0,8,78},{0,9,253},{96,7,0},
  127.69 +        {0,8,81},{0,8,17},{21,8,131},{18,7,31},{0,8,113},{0,8,49},{0,9,195},
  127.70 +        {16,7,10},{0,8,97},{0,8,33},{0,9,163},{0,8,1},{0,8,129},{0,8,65},
  127.71 +        {0,9,227},{16,7,6},{0,8,89},{0,8,25},{0,9,147},{19,7,59},{0,8,121},
  127.72 +        {0,8,57},{0,9,211},{17,7,17},{0,8,105},{0,8,41},{0,9,179},{0,8,9},
  127.73 +        {0,8,137},{0,8,73},{0,9,243},{16,7,4},{0,8,85},{0,8,21},{16,8,258},
  127.74 +        {19,7,43},{0,8,117},{0,8,53},{0,9,203},{17,7,13},{0,8,101},{0,8,37},
  127.75 +        {0,9,171},{0,8,5},{0,8,133},{0,8,69},{0,9,235},{16,7,8},{0,8,93},
  127.76 +        {0,8,29},{0,9,155},{20,7,83},{0,8,125},{0,8,61},{0,9,219},{18,7,23},
  127.77 +        {0,8,109},{0,8,45},{0,9,187},{0,8,13},{0,8,141},{0,8,77},{0,9,251},
  127.78 +        {16,7,3},{0,8,83},{0,8,19},{21,8,195},{19,7,35},{0,8,115},{0,8,51},
  127.79 +        {0,9,199},{17,7,11},{0,8,99},{0,8,35},{0,9,167},{0,8,3},{0,8,131},
  127.80 +        {0,8,67},{0,9,231},{16,7,7},{0,8,91},{0,8,27},{0,9,151},{20,7,67},
  127.81 +        {0,8,123},{0,8,59},{0,9,215},{18,7,19},{0,8,107},{0,8,43},{0,9,183},
  127.82 +        {0,8,11},{0,8,139},{0,8,75},{0,9,247},{16,7,5},{0,8,87},{0,8,23},
  127.83 +        {64,8,0},{19,7,51},{0,8,119},{0,8,55},{0,9,207},{17,7,15},{0,8,103},
  127.84 +        {0,8,39},{0,9,175},{0,8,7},{0,8,135},{0,8,71},{0,9,239},{16,7,9},
  127.85 +        {0,8,95},{0,8,31},{0,9,159},{20,7,99},{0,8,127},{0,8,63},{0,9,223},
  127.86 +        {18,7,27},{0,8,111},{0,8,47},{0,9,191},{0,8,15},{0,8,143},{0,8,79},
  127.87 +        {0,9,255}
  127.88 +    };
  127.89 +
  127.90 +    static const code distfix[32] = {
  127.91 +        {16,5,1},{23,5,257},{19,5,17},{27,5,4097},{17,5,5},{25,5,1025},
  127.92 +        {21,5,65},{29,5,16385},{16,5,3},{24,5,513},{20,5,33},{28,5,8193},
  127.93 +        {18,5,9},{26,5,2049},{22,5,129},{64,5,0},{16,5,2},{23,5,385},
  127.94 +        {19,5,25},{27,5,6145},{17,5,7},{25,5,1537},{21,5,97},{29,5,24577},
  127.95 +        {16,5,4},{24,5,769},{20,5,49},{28,5,12289},{18,5,13},{26,5,3073},
  127.96 +        {22,5,193},{64,5,0}
  127.97 +    };
   128.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   128.2 +++ b/libs/zlib/inflate.c	Sat Sep 19 05:51:51 2015 +0300
   128.3 @@ -0,0 +1,1368 @@
   128.4 +/* inflate.c -- zlib decompression
   128.5 + * Copyright (C) 1995-2005 Mark Adler
   128.6 + * For conditions of distribution and use, see copyright notice in zlib.h
   128.7 + */
   128.8 +
   128.9 +/*
  128.10 + * Change history:
  128.11 + *
  128.12 + * 1.2.beta0    24 Nov 2002
  128.13 + * - First version -- complete rewrite of inflate to simplify code, avoid
  128.14 + *   creation of window when not needed, minimize use of window when it is
  128.15 + *   needed, make inffast.c even faster, implement gzip decoding, and to
  128.16 + *   improve code readability and style over the previous zlib inflate code
  128.17 + *
  128.18 + * 1.2.beta1    25 Nov 2002
  128.19 + * - Use pointers for available input and output checking in inffast.c
  128.20 + * - Remove input and output counters in inffast.c
  128.21 + * - Change inffast.c entry and loop from avail_in >= 7 to >= 6
  128.22 + * - Remove unnecessary second byte pull from length extra in inffast.c
  128.23 + * - Unroll direct copy to three copies per loop in inffast.c
  128.24 + *
  128.25 + * 1.2.beta2    4 Dec 2002
  128.26 + * - Change external routine names to reduce potential conflicts
  128.27 + * - Correct filename to inffixed.h for fixed tables in inflate.c
  128.28 + * - Make hbuf[] unsigned char to match parameter type in inflate.c
  128.29 + * - Change strm->next_out[-state->offset] to *(strm->next_out - state->offset)
  128.30 + *   to avoid negation problem on Alphas (64 bit) in inflate.c
  128.31 + *
  128.32 + * 1.2.beta3    22 Dec 2002
  128.33 + * - Add comments on state->bits assertion in inffast.c
  128.34 + * - Add comments on op field in inftrees.h
  128.35 + * - Fix bug in reuse of allocated window after inflateReset()
  128.36 + * - Remove bit fields--back to byte structure for speed
  128.37 + * - Remove distance extra == 0 check in inflate_fast()--only helps for lengths
  128.38 + * - Change post-increments to pre-increments in inflate_fast(), PPC biased?
  128.39 + * - Add compile time option, POSTINC, to use post-increments instead (Intel?)
  128.40 + * - Make MATCH copy in inflate() much faster for when inflate_fast() not used
  128.41 + * - Use local copies of stream next and avail values, as well as local bit
  128.42 + *   buffer and bit count in inflate()--for speed when inflate_fast() not used
  128.43 + *
  128.44 + * 1.2.beta4    1 Jan 2003
  128.45 + * - Split ptr - 257 statements in inflate_table() to avoid compiler warnings
  128.46 + * - Move a comment on output buffer sizes from inffast.c to inflate.c
  128.47 + * - Add comments in inffast.c to introduce the inflate_fast() routine
  128.48 + * - Rearrange window copies in inflate_fast() for speed and simplification
  128.49 + * - Unroll last copy for window match in inflate_fast()
  128.50 + * - Use local copies of window variables in inflate_fast() for speed
  128.51 + * - Pull out common write == 0 case for speed in inflate_fast()
  128.52 + * - Make op and len in inflate_fast() unsigned for consistency
  128.53 + * - Add FAR to lcode and dcode declarations in inflate_fast()
  128.54 + * - Simplified bad distance check in inflate_fast()
  128.55 + * - Added inflateBackInit(), inflateBack(), and inflateBackEnd() in new
  128.56 + *   source file infback.c to provide a call-back interface to inflate for
  128.57 + *   programs like gzip and unzip -- uses window as output buffer to avoid
  128.58 + *   window copying
  128.59 + *
  128.60 + * 1.2.beta5    1 Jan 2003
  128.61 + * - Improved inflateBack() interface to allow the caller to provide initial
  128.62 + *   input in strm.
  128.63 + * - Fixed stored blocks bug in inflateBack()
  128.64 + *
  128.65 + * 1.2.beta6    4 Jan 2003
  128.66 + * - Added comments in inffast.c on effectiveness of POSTINC
  128.67 + * - Typecasting all around to reduce compiler warnings
  128.68 + * - Changed loops from while (1) or do {} while (1) to for (;;), again to
  128.69 + *   make compilers happy
  128.70 + * - Changed type of window in inflateBackInit() to unsigned char *
  128.71 + *
  128.72 + * 1.2.beta7    27 Jan 2003
  128.73 + * - Changed many types to unsigned or unsigned short to avoid warnings
  128.74 + * - Added inflateCopy() function
  128.75 + *
  128.76 + * 1.2.0        9 Mar 2003
  128.77 + * - Changed inflateBack() interface to provide separate opaque descriptors
  128.78 + *   for the in() and out() functions
  128.79 + * - Changed inflateBack() argument and in_func typedef to swap the length
  128.80 + *   and buffer address return values for the input function
  128.81 + * - Check next_in and next_out for Z_NULL on entry to inflate()
  128.82 + *
  128.83 + * The history for versions after 1.2.0 are in ChangeLog in zlib distribution.
  128.84 + */
  128.85 +
  128.86 +#include "zutil.h"
  128.87 +#include "inftrees.h"
  128.88 +#include "inflate.h"
  128.89 +#include "inffast.h"
  128.90 +
  128.91 +#ifdef MAKEFIXED
  128.92 +#  ifndef BUILDFIXED
  128.93 +#    define BUILDFIXED
  128.94 +#  endif
  128.95 +#endif
  128.96 +
  128.97 +/* function prototypes */
  128.98 +local void fixedtables OF((struct inflate_state FAR *state));
  128.99 +local int updatewindow OF((z_streamp strm, unsigned out));
 128.100 +#ifdef BUILDFIXED
 128.101 +   void makefixed OF((void));
 128.102 +#endif
 128.103 +local unsigned syncsearch OF((unsigned FAR *have, unsigned char FAR *buf,
 128.104 +                              unsigned len));
 128.105 +
 128.106 +int ZEXPORT inflateReset(strm)
 128.107 +z_streamp strm;
 128.108 +{
 128.109 +    struct inflate_state FAR *state;
 128.110 +
 128.111 +    if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
 128.112 +    state = (struct inflate_state FAR *)strm->state;
 128.113 +    strm->total_in = strm->total_out = state->total = 0;
 128.114 +    strm->msg = Z_NULL;
 128.115 +    strm->adler = 1;        /* to support ill-conceived Java test suite */
 128.116 +    state->mode = HEAD;
 128.117 +    state->last = 0;
 128.118 +    state->havedict = 0;
 128.119 +    state->dmax = 32768U;
 128.120 +    state->head = Z_NULL;
 128.121 +    state->wsize = 0;
 128.122 +    state->whave = 0;
 128.123 +    state->write = 0;
 128.124 +    state->hold = 0;
 128.125 +    state->bits = 0;
 128.126 +    state->lencode = state->distcode = state->next = state->codes;
 128.127 +    Tracev((stderr, "inflate: reset\n"));
 128.128 +    return Z_OK;
 128.129 +}
 128.130 +
 128.131 +int ZEXPORT inflatePrime(strm, bits, value)
 128.132 +z_streamp strm;
 128.133 +int bits;
 128.134 +int value;
 128.135 +{
 128.136 +    struct inflate_state FAR *state;
 128.137 +
 128.138 +    if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
 128.139 +    state = (struct inflate_state FAR *)strm->state;
 128.140 +    if (bits > 16 || state->bits + bits > 32) return Z_STREAM_ERROR;
 128.141 +    value &= (1L << bits) - 1;
 128.142 +    state->hold += value << state->bits;
 128.143 +    state->bits += bits;
 128.144 +    return Z_OK;
 128.145 +}
 128.146 +
 128.147 +int ZEXPORT inflateInit2_(strm, windowBits, version, stream_size)
 128.148 +z_streamp strm;
 128.149 +int windowBits;
 128.150 +const char *version;
 128.151 +int stream_size;
 128.152 +{
 128.153 +    struct inflate_state FAR *state;
 128.154 +
 128.155 +    if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
 128.156 +        stream_size != (int)(sizeof(z_stream)))
 128.157 +        return Z_VERSION_ERROR;
 128.158 +    if (strm == Z_NULL) return Z_STREAM_ERROR;
 128.159 +    strm->msg = Z_NULL;                 /* in case we return an error */
 128.160 +    if (strm->zalloc == (alloc_func)0) {
 128.161 +        strm->zalloc = zcalloc;
 128.162 +        strm->opaque = (voidpf)0;
 128.163 +    }
 128.164 +    if (strm->zfree == (free_func)0) strm->zfree = zcfree;
 128.165 +    state = (struct inflate_state FAR *)
 128.166 +            ZALLOC(strm, 1, sizeof(struct inflate_state));
 128.167 +    if (state == Z_NULL) return Z_MEM_ERROR;
 128.168 +    Tracev((stderr, "inflate: allocated\n"));
 128.169 +    strm->state = (struct internal_state FAR *)state;
 128.170 +    if (windowBits < 0) {
 128.171 +        state->wrap = 0;
 128.172 +        windowBits = -windowBits;
 128.173 +    }
 128.174 +    else {
 128.175 +        state->wrap = (windowBits >> 4) + 1;
 128.176 +#ifdef GUNZIP
 128.177 +        if (windowBits < 48) windowBits &= 15;
 128.178 +#endif
 128.179 +    }
 128.180 +    if (windowBits < 8 || windowBits > 15) {
 128.181 +        ZFREE(strm, state);
 128.182 +        strm->state = Z_NULL;
 128.183 +        return Z_STREAM_ERROR;
 128.184 +    }
 128.185 +    state->wbits = (unsigned)windowBits;
 128.186 +    state->window = Z_NULL;
 128.187 +    return inflateReset(strm);
 128.188 +}
 128.189 +
 128.190 +int ZEXPORT inflateInit_(strm, version, stream_size)
 128.191 +z_streamp strm;
 128.192 +const char *version;
 128.193 +int stream_size;
 128.194 +{
 128.195 +    return inflateInit2_(strm, DEF_WBITS, version, stream_size);
 128.196 +}
 128.197 +
 128.198 +/*
 128.199 +   Return state with length and distance decoding tables and index sizes set to
 128.200 +   fixed code decoding.  Normally this returns fixed tables from inffixed.h.
 128.201 +   If BUILDFIXED is defined, then instead this routine builds the tables the
 128.202 +   first time it's called, and returns those tables the first time and
 128.203 +   thereafter.  This reduces the size of the code by about 2K bytes, in
 128.204 +   exchange for a little execution time.  However, BUILDFIXED should not be
 128.205 +   used for threaded applications, since the rewriting of the tables and virgin
 128.206 +   may not be thread-safe.
 128.207 + */
 128.208 +local void fixedtables(state)
 128.209 +struct inflate_state FAR *state;
 128.210 +{
 128.211 +#ifdef BUILDFIXED
 128.212 +    static int virgin = 1;
 128.213 +    static code *lenfix, *distfix;
 128.214 +    static code fixed[544];
 128.215 +
 128.216 +    /* build fixed huffman tables if first call (may not be thread safe) */
 128.217 +    if (virgin) {
 128.218 +        unsigned sym, bits;
 128.219 +        static code *next;
 128.220 +
 128.221 +        /* literal/length table */
 128.222 +        sym = 0;
 128.223 +        while (sym < 144) state->lens[sym++] = 8;
 128.224 +        while (sym < 256) state->lens[sym++] = 9;
 128.225 +        while (sym < 280) state->lens[sym++] = 7;
 128.226 +        while (sym < 288) state->lens[sym++] = 8;
 128.227 +        next = fixed;
 128.228 +        lenfix = next;
 128.229 +        bits = 9;
 128.230 +        inflate_table(LENS, state->lens, 288, &(next), &(bits), state->work);
 128.231 +
 128.232 +        /* distance table */
 128.233 +        sym = 0;
 128.234 +        while (sym < 32) state->lens[sym++] = 5;
 128.235 +        distfix = next;
 128.236 +        bits = 5;
 128.237 +        inflate_table(DISTS, state->lens, 32, &(next), &(bits), state->work);
 128.238 +
 128.239 +        /* do this just once */
 128.240 +        virgin = 0;
 128.241 +    }
 128.242 +#else /* !BUILDFIXED */
 128.243 +#   include "inffixed.h"
 128.244 +#endif /* BUILDFIXED */
 128.245 +    state->lencode = lenfix;
 128.246 +    state->lenbits = 9;
 128.247 +    state->distcode = distfix;
 128.248 +    state->distbits = 5;
 128.249 +}
 128.250 +
 128.251 +#ifdef MAKEFIXED
 128.252 +#include <stdio.h>
 128.253 +
 128.254 +/*
 128.255 +   Write out the inffixed.h that is #include'd above.  Defining MAKEFIXED also
 128.256 +   defines BUILDFIXED, so the tables are built on the fly.  makefixed() writes
 128.257 +   those tables to stdout, which would be piped to inffixed.h.  A small program
 128.258 +   can simply call makefixed to do this:
 128.259 +
 128.260 +    void makefixed(void);
 128.261 +
 128.262 +    int main(void)
 128.263 +    {
 128.264 +        makefixed();
 128.265 +        return 0;
 128.266 +    }
 128.267 +
 128.268 +   Then that can be linked with zlib built with MAKEFIXED defined and run:
 128.269 +
 128.270 +    a.out > inffixed.h
 128.271 + */
 128.272 +void makefixed()
 128.273 +{
 128.274 +    unsigned low, size;
 128.275 +    struct inflate_state state;
 128.276 +
 128.277 +    fixedtables(&state);
 128.278 +    puts("    /* inffixed.h -- table for decoding fixed codes");
 128.279 +    puts("     * Generated automatically by makefixed().");
 128.280 +    puts("     */");
 128.281 +    puts("");
 128.282 +    puts("    /* WARNING: this file should *not* be used by applications.");
 128.283 +    puts("       It is part of the implementation of this library and is");
 128.284 +    puts("       subject to change. Applications should only use zlib.h.");
 128.285 +    puts("     */");
 128.286 +    puts("");
 128.287 +    size = 1U << 9;
 128.288 +    printf("    static const code lenfix[%u] = {", size);
 128.289 +    low = 0;
 128.290 +    for (;;) {
 128.291 +        if ((low % 7) == 0) printf("\n        ");
 128.292 +        printf("{%u,%u,%d}", state.lencode[low].op, state.lencode[low].bits,
 128.293 +               state.lencode[low].val);
 128.294 +        if (++low == size) break;
 128.295 +        putchar(',');
 128.296 +    }
 128.297 +    puts("\n    };");
 128.298 +    size = 1U << 5;
 128.299 +    printf("\n    static const code distfix[%u] = {", size);
 128.300 +    low = 0;
 128.301 +    for (;;) {
 128.302 +        if ((low % 6) == 0) printf("\n        ");
 128.303 +        printf("{%u,%u,%d}", state.distcode[low].op, state.distcode[low].bits,
 128.304 +               state.distcode[low].val);
 128.305 +        if (++low == size) break;
 128.306 +        putchar(',');
 128.307 +    }
 128.308 +    puts("\n    };");
 128.309 +}
 128.310 +#endif /* MAKEFIXED */
 128.311 +
 128.312 +/*
 128.313 +   Update the window with the last wsize (normally 32K) bytes written before
 128.314 +   returning.  If window does not exist yet, create it.  This is only called
 128.315 +   when a window is already in use, or when output has been written during this
 128.316 +   inflate call, but the end of the deflate stream has not been reached yet.
 128.317 +   It is also called to create a window for dictionary data when a dictionary
 128.318 +   is loaded.
 128.319 +
 128.320 +   Providing output buffers larger than 32K to inflate() should provide a speed
 128.321 +   advantage, since only the last 32K of output is copied to the sliding window
 128.322 +   upon return from inflate(), and since all distances after the first 32K of
 128.323 +   output will fall in the output data, making match copies simpler and faster.
 128.324 +   The advantage may be dependent on the size of the processor's data caches.
 128.325 + */
 128.326 +local int updatewindow(strm, out)
 128.327 +z_streamp strm;
 128.328 +unsigned out;
 128.329 +{
 128.330 +    struct inflate_state FAR *state;
 128.331 +    unsigned copy, dist;
 128.332 +
 128.333 +    state = (struct inflate_state FAR *)strm->state;
 128.334 +
 128.335 +    /* if it hasn't been done already, allocate space for the window */
 128.336 +    if (state->window == Z_NULL) {
 128.337 +        state->window = (unsigned char FAR *)
 128.338 +                        ZALLOC(strm, 1U << state->wbits,
 128.339 +                               sizeof(unsigned char));
 128.340 +        if (state->window == Z_NULL) return 1;
 128.341 +    }
 128.342 +
 128.343 +    /* if window not in use yet, initialize */
 128.344 +    if (state->wsize == 0) {
 128.345 +        state->wsize = 1U << state->wbits;
 128.346 +        state->write = 0;
 128.347 +        state->whave = 0;
 128.348 +    }
 128.349 +
 128.350 +    /* copy state->wsize or less output bytes into the circular window */
 128.351 +    copy = out - strm->avail_out;
 128.352 +    if (copy >= state->wsize) {
 128.353 +        zmemcpy(state->window, strm->next_out - state->wsize, state->wsize);
 128.354 +        state->write = 0;
 128.355 +        state->whave = state->wsize;
 128.356 +    }
 128.357 +    else {
 128.358 +        dist = state->wsize - state->write;
 128.359 +        if (dist > copy) dist = copy;
 128.360 +        zmemcpy(state->window + state->write, strm->next_out - copy, dist);
 128.361 +        copy -= dist;
 128.362 +        if (copy) {
 128.363 +            zmemcpy(state->window, strm->next_out - copy, copy);
 128.364 +            state->write = copy;
 128.365 +            state->whave = state->wsize;
 128.366 +        }
 128.367 +        else {
 128.368 +            state->write += dist;
 128.369 +            if (state->write == state->wsize) state->write = 0;
 128.370 +            if (state->whave < state->wsize) state->whave += dist;
 128.371 +        }
 128.372 +    }
 128.373 +    return 0;
 128.374 +}
 128.375 +
 128.376 +/* Macros for inflate(): */
 128.377 +
 128.378 +/* check function to use adler32() for zlib or crc32() for gzip */
 128.379 +#ifdef GUNZIP
 128.380 +#  define UPDATE(check, buf, len) \
 128.381 +    (state->flags ? crc32(check, buf, len) : adler32(check, buf, len))
 128.382 +#else
 128.383 +#  define UPDATE(check, buf, len) adler32(check, buf, len)
 128.384 +#endif
 128.385 +
 128.386 +/* check macros for header crc */
 128.387 +#ifdef GUNZIP
 128.388 +#  define CRC2(check, word) \
 128.389 +    do { \
 128.390 +        hbuf[0] = (unsigned char)(word); \
 128.391 +        hbuf[1] = (unsigned char)((word) >> 8); \
 128.392 +        check = crc32(check, hbuf, 2); \
 128.393 +    } while (0)
 128.394 +
 128.395 +#  define CRC4(check, word) \
 128.396 +    do { \
 128.397 +        hbuf[0] = (unsigned char)(word); \
 128.398 +        hbuf[1] = (unsigned char)((word) >> 8); \
 128.399 +        hbuf[2] = (unsigned char)((word) >> 16); \
 128.400 +        hbuf[3] = (unsigned char)((word) >> 24); \
 128.401 +        check = crc32(check, hbuf, 4); \
 128.402 +    } while (0)
 128.403 +#endif
 128.404 +
 128.405 +/* Load registers with state in inflate() for speed */
 128.406 +#define LOAD() \
 128.407 +    do { \
 128.408 +        put = strm->next_out; \
 128.409 +        left = strm->avail_out; \
 128.410 +        next = strm->next_in; \
 128.411 +        have = strm->avail_in; \
 128.412 +        hold = state->hold; \
 128.413 +        bits = state->bits; \
 128.414 +    } while (0)
 128.415 +
 128.416 +/* Restore state from registers in inflate() */
 128.417 +#define RESTORE() \
 128.418 +    do { \
 128.419 +        strm->next_out = put; \
 128.420 +        strm->avail_out = left; \
 128.421 +        strm->next_in = next; \
 128.422 +        strm->avail_in = have; \
 128.423 +        state->hold = hold; \
 128.424 +        state->bits = bits; \
 128.425 +    } while (0)
 128.426 +
 128.427 +/* Clear the input bit accumulator */
 128.428 +#define INITBITS() \
 128.429 +    do { \
 128.430 +        hold = 0; \
 128.431 +        bits = 0; \
 128.432 +    } while (0)
 128.433 +
 128.434 +/* Get a byte of input into the bit accumulator, or return from inflate()
 128.435 +   if there is no input available. */
 128.436 +#define PULLBYTE() \
 128.437 +    do { \
 128.438 +        if (have == 0) goto inf_leave; \
 128.439 +        have--; \
 128.440 +        hold += (unsigned long)(*next++) << bits; \
 128.441 +        bits += 8; \
 128.442 +    } while (0)
 128.443 +
 128.444 +/* Assure that there are at least n bits in the bit accumulator.  If there is
 128.445 +   not enough available input to do that, then return from inflate(). */
 128.446 +#define NEEDBITS(n) \
 128.447 +    do { \
 128.448 +        while (bits < (unsigned)(n)) \
 128.449 +            PULLBYTE(); \
 128.450 +    } while (0)
 128.451 +
 128.452 +/* Return the low n bits of the bit accumulator (n < 16) */
 128.453 +#define BITS(n) \
 128.454 +    ((unsigned)hold & ((1U << (n)) - 1))
 128.455 +
 128.456 +/* Remove n bits from the bit accumulator */
 128.457 +#define DROPBITS(n) \
 128.458 +    do { \
 128.459 +        hold >>= (n); \
 128.460 +        bits -= (unsigned)(n); \
 128.461 +    } while (0)
 128.462 +
 128.463 +/* Remove zero to seven bits as needed to go to a byte boundary */
 128.464 +#define BYTEBITS() \
 128.465 +    do { \
 128.466 +        hold >>= bits & 7; \
 128.467 +        bits -= bits & 7; \
 128.468 +    } while (0)
 128.469 +
 128.470 +/* Reverse the bytes in a 32-bit value */
 128.471 +#define REVERSE(q) \
 128.472 +    ((((q) >> 24) & 0xff) + (((q) >> 8) & 0xff00) + \
 128.473 +     (((q) & 0xff00) << 8) + (((q) & 0xff) << 24))
 128.474 +
 128.475 +/*
 128.476 +   inflate() uses a state machine to process as much input data and generate as
 128.477 +   much output data as possible before returning.  The state machine is
 128.478 +   structured roughly as follows:
 128.479 +
 128.480 +    for (;;) switch (state) {
 128.481 +    ...
 128.482 +    case STATEn:
 128.483 +        if (not enough input data or output space to make progress)
 128.484 +            return;
 128.485 +        ... make progress ...
 128.486 +        state = STATEm;
 128.487 +        break;
 128.488 +    ...
 128.489 +    }
 128.490 +
 128.491 +   so when inflate() is called again, the same case is attempted again, and
 128.492 +   if the appropriate resources are provided, the machine proceeds to the
 128.493 +   next state.  The NEEDBITS() macro is usually the way the state evaluates
 128.494 +   whether it can proceed or should return.  NEEDBITS() does the return if
 128.495 +   the requested bits are not available.  The typical use of the BITS macros
 128.496 +   is:
 128.497 +
 128.498 +        NEEDBITS(n);
 128.499 +        ... do something with BITS(n) ...
 128.500 +        DROPBITS(n);
 128.501 +
 128.502 +   where NEEDBITS(n) either returns from inflate() if there isn't enough
 128.503 +   input left to load n bits into the accumulator, or it continues.  BITS(n)
 128.504 +   gives the low n bits in the accumulator.  When done, DROPBITS(n) drops
 128.505 +   the low n bits off the accumulator.  INITBITS() clears the accumulator
 128.506 +   and sets the number of available bits to zero.  BYTEBITS() discards just
 128.507 +   enough bits to put the accumulator on a byte boundary.  After BYTEBITS()
 128.508 +   and a NEEDBITS(8), then BITS(8) would return the next byte in the stream.
 128.509 +
 128.510 +   NEEDBITS(n) uses PULLBYTE() to get an available byte of input, or to return
 128.511 +   if there is no input available.  The decoding of variable length codes uses
 128.512 +   PULLBYTE() directly in order to pull just enough bytes to decode the next
 128.513 +   code, and no more.
 128.514 +
 128.515 +   Some states loop until they get enough input, making sure that enough
 128.516 +   state information is maintained to continue the loop where it left off
 128.517 +   if NEEDBITS() returns in the loop.  For example, want, need, and keep
 128.518 +   would all have to actually be part of the saved state in case NEEDBITS()
 128.519 +   returns:
 128.520 +
 128.521 +    case STATEw:
 128.522 +        while (want < need) {
 128.523 +            NEEDBITS(n);
 128.524 +            keep[want++] = BITS(n);
 128.525 +            DROPBITS(n);
 128.526 +        }
 128.527 +        state = STATEx;
 128.528 +    case STATEx:
 128.529 +
 128.530 +   As shown above, if the next state is also the next case, then the break
 128.531 +   is omitted.
 128.532 +
 128.533 +   A state may also return if there is not enough output space available to
 128.534 +   complete that state.  Those states are copying stored data, writing a
 128.535 +   literal byte, and copying a matching string.
 128.536 +
 128.537 +   When returning, a "goto inf_leave" is used to update the total counters,
 128.538 +   update the check value, and determine whether any progress has been made
 128.539 +   during that inflate() call in order to return the proper return code.
 128.540 +   Progress is defined as a change in either strm->avail_in or strm->avail_out.
 128.541 +   When there is a window, goto inf_leave will update the window with the last
 128.542 +   output written.  If a goto inf_leave occurs in the middle of decompression
 128.543 +   and there is no window currently, goto inf_leave will create one and copy
 128.544 +   output to the window for the next call of inflate().
 128.545 +
 128.546 +   In this implementation, the flush parameter of inflate() only affects the
 128.547 +   return code (per zlib.h).  inflate() always writes as much as possible to
 128.548 +   strm->next_out, given the space available and the provided input--the effect
 128.549 +   documented in zlib.h of Z_SYNC_FLUSH.  Furthermore, inflate() always defers
 128.550 +   the allocation of and copying into a sliding window until necessary, which
 128.551 +   provides the effect documented in zlib.h for Z_FINISH when the entire input
 128.552 +   stream available.  So the only thing the flush parameter actually does is:
 128.553 +   when flush is set to Z_FINISH, inflate() cannot return Z_OK.  Instead it
 128.554 +   will return Z_BUF_ERROR if it has not reached the end of the stream.
 128.555 + */
 128.556 +
 128.557 +int ZEXPORT inflate(strm, flush)
 128.558 +z_streamp strm;
 128.559 +int flush;
 128.560 +{
 128.561 +    struct inflate_state FAR *state;
 128.562 +    unsigned char FAR *next;    /* next input */
 128.563 +    unsigned char FAR *put;     /* next output */
 128.564 +    unsigned have, left;        /* available input and output */
 128.565 +    unsigned long hold;         /* bit buffer */
 128.566 +    unsigned bits;              /* bits in bit buffer */
 128.567 +    unsigned in, out;           /* save starting available input and output */
 128.568 +    unsigned copy;              /* number of stored or match bytes to copy */
 128.569 +    unsigned char FAR *from;    /* where to copy match bytes from */
 128.570 +    code this;                  /* current decoding table entry */
 128.571 +    code last;                  /* parent table entry */
 128.572 +    unsigned len;               /* length to copy for repeats, bits to drop */
 128.573 +    int ret;                    /* return code */
 128.574 +#ifdef GUNZIP
 128.575 +    unsigned char hbuf[4];      /* buffer for gzip header crc calculation */
 128.576 +#endif
 128.577 +    static const unsigned short order[19] = /* permutation of code lengths */
 128.578 +        {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
 128.579 +
 128.580 +    if (strm == Z_NULL || strm->state == Z_NULL || strm->next_out == Z_NULL ||
 128.581 +        (strm->next_in == Z_NULL && strm->avail_in != 0))
 128.582 +        return Z_STREAM_ERROR;
 128.583 +
 128.584 +    state = (struct inflate_state FAR *)strm->state;
 128.585 +    if (state->mode == TYPE) state->mode = TYPEDO;      /* skip check */
 128.586 +    LOAD();
 128.587 +    in = have;
 128.588 +    out = left;
 128.589 +    ret = Z_OK;
 128.590 +    for (;;)
 128.591 +        switch (state->mode) {
 128.592 +        case HEAD:
 128.593 +            if (state->wrap == 0) {
 128.594 +                state->mode = TYPEDO;
 128.595 +                break;
 128.596 +            }
 128.597 +            NEEDBITS(16);
 128.598 +#ifdef GUNZIP
 128.599 +            if ((state->wrap & 2) && hold == 0x8b1f) {  /* gzip header */
 128.600 +                state->check = crc32(0L, Z_NULL, 0);
 128.601 +                CRC2(state->check, hold);
 128.602 +                INITBITS();
 128.603 +                state->mode = FLAGS;
 128.604 +                break;
 128.605 +            }
 128.606 +            state->flags = 0;           /* expect zlib header */
 128.607 +            if (state->head != Z_NULL)
 128.608 +                state->head->done = -1;
 128.609 +            if (!(state->wrap & 1) ||   /* check if zlib header allowed */
 128.610 +#else
 128.611 +            if (
 128.612 +#endif
 128.613 +                ((BITS(8) << 8) + (hold >> 8)) % 31) {
 128.614 +                strm->msg = (char *)"incorrect header check";
 128.615 +                state->mode = BAD;
 128.616 +                break;
 128.617 +            }
 128.618 +            if (BITS(4) != Z_DEFLATED) {
 128.619 +                strm->msg = (char *)"unknown compression method";
 128.620 +                state->mode = BAD;
 128.621 +                break;
 128.622 +            }
 128.623 +            DROPBITS(4);
 128.624 +            len = BITS(4) + 8;
 128.625 +            if (len > state->wbits) {
 128.626 +                strm->msg = (char *)"invalid window size";
 128.627 +                state->mode = BAD;
 128.628 +                break;
 128.629 +            }
 128.630 +            state->dmax = 1U << len;
 128.631 +            Tracev((stderr, "inflate:   zlib header ok\n"));
 128.632 +            strm->adler = state->check = adler32(0L, Z_NULL, 0);
 128.633 +            state->mode = hold & 0x200 ? DICTID : TYPE;
 128.634 +            INITBITS();
 128.635 +            break;
 128.636 +#ifdef GUNZIP
 128.637 +        case FLAGS:
 128.638 +            NEEDBITS(16);
 128.639 +            state->flags = (int)(hold);
 128.640 +            if ((state->flags & 0xff) != Z_DEFLATED) {
 128.641 +                strm->msg = (char *)"unknown compression method";
 128.642 +                state->mode = BAD;
 128.643 +                break;
 128.644 +            }
 128.645 +            if (state->flags & 0xe000) {
 128.646 +                strm->msg = (char *)"unknown header flags set";
 128.647 +                state->mode = BAD;
 128.648 +                break;
 128.649 +            }
 128.650 +            if (state->head != Z_NULL)
 128.651 +                state->head->text = (int)((hold >> 8) & 1);
 128.652 +            if (state->flags & 0x0200) CRC2(state->check, hold);
 128.653 +            INITBITS();
 128.654 +            state->mode = TIME;
 128.655 +        case TIME:
 128.656 +            NEEDBITS(32);
 128.657 +            if (state->head != Z_NULL)
 128.658 +                state->head->time = hold;
 128.659 +            if (state->flags & 0x0200) CRC4(state->check, hold);
 128.660 +            INITBITS();
 128.661 +            state->mode = OS;
 128.662 +        case OS:
 128.663 +            NEEDBITS(16);
 128.664 +            if (state->head != Z_NULL) {
 128.665 +                state->head->xflags = (int)(hold & 0xff);
 128.666 +                state->head->os = (int)(hold >> 8);
 128.667 +            }
 128.668 +            if (state->flags & 0x0200) CRC2(state->check, hold);
 128.669 +            INITBITS();
 128.670 +            state->mode = EXLEN;
 128.671 +        case EXLEN:
 128.672 +            if (state->flags & 0x0400) {
 128.673 +                NEEDBITS(16);
 128.674 +                state->length = (unsigned)(hold);
 128.675 +                if (state->head != Z_NULL)
 128.676 +                    state->head->extra_len = (unsigned)hold;
 128.677 +                if (state->flags & 0x0200) CRC2(state->check, hold);
 128.678 +                INITBITS();
 128.679 +            }
 128.680 +            else if (state->head != Z_NULL)
 128.681 +                state->head->extra = Z_NULL;
 128.682 +            state->mode = EXTRA;
 128.683 +        case EXTRA:
 128.684 +            if (state->flags & 0x0400) {
 128.685 +                copy = state->length;
 128.686 +                if (copy > have) copy = have;
 128.687 +                if (copy) {
 128.688 +                    if (state->head != Z_NULL &&
 128.689 +                        state->head->extra != Z_NULL) {
 128.690 +                        len = state->head->extra_len - state->length;
 128.691 +                        zmemcpy(state->head->extra + len, next,
 128.692 +                                len + copy > state->head->extra_max ?
 128.693 +                                state->head->extra_max - len : copy);
 128.694 +                    }
 128.695 +                    if (state->flags & 0x0200)
 128.696 +                        state->check = crc32(state->check, next, copy);
 128.697 +                    have -= copy;
 128.698 +                    next += copy;
 128.699 +                    state->length -= copy;
 128.700 +                }
 128.701 +                if (state->length) goto inf_leave;
 128.702 +            }
 128.703 +            state->length = 0;
 128.704 +            state->mode = NAME;
 128.705 +        case NAME:
 128.706 +            if (state->flags & 0x0800) {
 128.707 +                if (have == 0) goto inf_leave;
 128.708 +                copy = 0;
 128.709 +                do {
 128.710 +                    len = (unsigned)(next[copy++]);
 128.711 +                    if (state->head != Z_NULL &&
 128.712 +                            state->head->name != Z_NULL &&
 128.713 +                            state->length < state->head->name_max)
 128.714 +                        state->head->name[state->length++] = len;
 128.715 +                } while (len && copy < have);
 128.716 +                if (state->flags & 0x0200)
 128.717 +                    state->check = crc32(state->check, next, copy);
 128.718 +                have -= copy;
 128.719 +                next += copy;
 128.720 +                if (len) goto inf_leave;
 128.721 +            }
 128.722 +            else if (state->head != Z_NULL)
 128.723 +                state->head->name = Z_NULL;
 128.724 +            state->length = 0;
 128.725 +            state->mode = COMMENT;
 128.726 +        case COMMENT:
 128.727 +            if (state->flags & 0x1000) {
 128.728 +                if (have == 0) goto inf_leave;
 128.729 +                copy = 0;
 128.730 +                do {
 128.731 +                    len = (unsigned)(next[copy++]);
 128.732 +                    if (state->head != Z_NULL &&
 128.733 +                            state->head->comment != Z_NULL &&
 128.734 +                            state->length < state->head->comm_max)
 128.735 +                        state->head->comment[state->length++] = len;
 128.736 +                } while (len && copy < have);
 128.737 +                if (state->flags & 0x0200)
 128.738 +                    state->check = crc32(state->check, next, copy);
 128.739 +                have -= copy;
 128.740 +                next += copy;
 128.741 +                if (len) goto inf_leave;
 128.742 +            }
 128.743 +            else if (state->head != Z_NULL)
 128.744 +                state->head->comment = Z_NULL;
 128.745 +            state->mode = HCRC;
 128.746 +        case HCRC:
 128.747 +            if (state->flags & 0x0200) {
 128.748 +                NEEDBITS(16);
 128.749 +                if (hold != (state->check & 0xffff)) {
 128.750 +                    strm->msg = (char *)"header crc mismatch";
 128.751 +                    state->mode = BAD;
 128.752 +                    break;
 128.753 +                }
 128.754 +                INITBITS();
 128.755 +            }
 128.756 +            if (state->head != Z_NULL) {
 128.757 +                state->head->hcrc = (int)((state->flags >> 9) & 1);
 128.758 +                state->head->done = 1;
 128.759 +            }
 128.760 +            strm->adler = state->check = crc32(0L, Z_NULL, 0);
 128.761 +            state->mode = TYPE;
 128.762 +            break;
 128.763 +#endif
 128.764 +        case DICTID:
 128.765 +            NEEDBITS(32);
 128.766 +            strm->adler = state->check = REVERSE(hold);
 128.767 +            INITBITS();
 128.768 +            state->mode = DICT;
 128.769 +        case DICT:
 128.770 +            if (state->havedict == 0) {
 128.771 +                RESTORE();
 128.772 +                return Z_NEED_DICT;
 128.773 +            }
 128.774 +            strm->adler = state->check = adler32(0L, Z_NULL, 0);
 128.775 +            state->mode = TYPE;
 128.776 +        case TYPE:
 128.777 +            if (flush == Z_BLOCK) goto inf_leave;
 128.778 +        case TYPEDO:
 128.779 +            if (state->last) {
 128.780 +                BYTEBITS();
 128.781 +                state->mode = CHECK;
 128.782 +                break;
 128.783 +            }
 128.784 +            NEEDBITS(3);
 128.785 +            state->last = BITS(1);
 128.786 +            DROPBITS(1);
 128.787 +            switch (BITS(2)) {
 128.788 +            case 0:                             /* stored block */
 128.789 +                Tracev((stderr, "inflate:     stored block%s\n",
 128.790 +                        state->last ? " (last)" : ""));
 128.791 +                state->mode = STORED;
 128.792 +                break;
 128.793 +            case 1:                             /* fixed block */
 128.794 +                fixedtables(state);
 128.795 +                Tracev((stderr, "inflate:     fixed codes block%s\n",
 128.796 +                        state->last ? " (last)" : ""));
 128.797 +                state->mode = LEN;              /* decode codes */
 128.798 +                break;
 128.799 +            case 2:                             /* dynamic block */
 128.800 +                Tracev((stderr, "inflate:     dynamic codes block%s\n",
 128.801 +                        state->last ? " (last)" : ""));
 128.802 +                state->mode = TABLE;
 128.803 +                break;
 128.804 +            case 3:
 128.805 +                strm->msg = (char *)"invalid block type";
 128.806 +                state->mode = BAD;
 128.807 +            }
 128.808 +            DROPBITS(2);
 128.809 +            break;
 128.810 +        case STORED:
 128.811 +            BYTEBITS();                         /* go to byte boundary */
 128.812 +            NEEDBITS(32);
 128.813 +            if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) {
 128.814 +                strm->msg = (char *)"invalid stored block lengths";
 128.815 +                state->mode = BAD;
 128.816 +                break;
 128.817 +            }
 128.818 +            state->length = (unsigned)hold & 0xffff;
 128.819 +            Tracev((stderr, "inflate:       stored length %u\n",
 128.820 +                    state->length));
 128.821 +            INITBITS();
 128.822 +            state->mode = COPY;
 128.823 +        case COPY:
 128.824 +            copy = state->length;
 128.825 +            if (copy) {
 128.826 +                if (copy > have) copy = have;
 128.827 +                if (copy > left) copy = left;
 128.828 +                if (copy == 0) goto inf_leave;
 128.829 +                zmemcpy(put, next, copy);
 128.830 +                have -= copy;
 128.831 +                next += copy;
 128.832 +                left -= copy;
 128.833 +                put += copy;
 128.834 +                state->length -= copy;
 128.835 +                break;
 128.836 +            }
 128.837 +            Tracev((stderr, "inflate:       stored end\n"));
 128.838 +            state->mode = TYPE;
 128.839 +            break;
 128.840 +        case TABLE:
 128.841 +            NEEDBITS(14);
 128.842 +            state->nlen = BITS(5) + 257;
 128.843 +            DROPBITS(5);
 128.844 +            state->ndist = BITS(5) + 1;
 128.845 +            DROPBITS(5);
 128.846 +            state->ncode = BITS(4) + 4;
 128.847 +            DROPBITS(4);
 128.848 +#ifndef PKZIP_BUG_WORKAROUND
 128.849 +            if (state->nlen > 286 || state->ndist > 30) {
 128.850 +                strm->msg = (char *)"too many length or distance symbols";
 128.851 +                state->mode = BAD;
 128.852 +                break;
 128.853 +            }
 128.854 +#endif
 128.855 +            Tracev((stderr, "inflate:       table sizes ok\n"));
 128.856 +            state->have = 0;
 128.857 +            state->mode = LENLENS;
 128.858 +        case LENLENS:
 128.859 +            while (state->have < state->ncode) {
 128.860 +                NEEDBITS(3);
 128.861 +                state->lens[order[state->have++]] = (unsigned short)BITS(3);
 128.862 +                DROPBITS(3);
 128.863 +            }
 128.864 +            while (state->have < 19)
 128.865 +                state->lens[order[state->have++]] = 0;
 128.866 +            state->next = state->codes;
 128.867 +            state->lencode = (code const FAR *)(state->next);
 128.868 +            state->lenbits = 7;
 128.869 +            ret = inflate_table(CODES, state->lens, 19, &(state->next),
 128.870 +                                &(state->lenbits), state->work);
 128.871 +            if (ret) {
 128.872 +                strm->msg = (char *)"invalid code lengths set";
 128.873 +                state->mode = BAD;
 128.874 +                break;
 128.875 +            }
 128.876 +            Tracev((stderr, "inflate:       code lengths ok\n"));
 128.877 +            state->have = 0;
 128.878 +            state->mode = CODELENS;
 128.879 +        case CODELENS:
 128.880 +            while (state->have < state->nlen + state->ndist) {
 128.881 +                for (;;) {
 128.882 +                    this = state->lencode[BITS(state->lenbits)];
 128.883 +                    if ((unsigned)(this.bits) <= bits) break;
 128.884 +                    PULLBYTE();
 128.885 +                }
 128.886 +                if (this.val < 16) {
 128.887 +                    NEEDBITS(this.bits);
 128.888 +                    DROPBITS(this.bits);
 128.889 +                    state->lens[state->have++] = this.val;
 128.890 +                }
 128.891 +                else {
 128.892 +                    if (this.val == 16) {
 128.893 +                        NEEDBITS(this.bits + 2);
 128.894 +                        DROPBITS(this.bits);
 128.895 +                        if (state->have == 0) {
 128.896 +                            strm->msg = (char *)"invalid bit length repeat";
 128.897 +                            state->mode = BAD;
 128.898 +                            break;
 128.899 +                        }
 128.900 +                        len = state->lens[state->have - 1];
 128.901 +                        copy = 3 + BITS(2);
 128.902 +                        DROPBITS(2);
 128.903 +                    }
 128.904 +                    else if (this.val == 17) {
 128.905 +                        NEEDBITS(this.bits + 3);
 128.906 +                        DROPBITS(this.bits);
 128.907 +                        len = 0;
 128.908 +                        copy = 3 + BITS(3);
 128.909 +                        DROPBITS(3);
 128.910 +                    }
 128.911 +                    else {
 128.912 +                        NEEDBITS(this.bits + 7);
 128.913 +                        DROPBITS(this.bits);
 128.914 +                        len = 0;
 128.915 +                        copy = 11 + BITS(7);
 128.916 +                        DROPBITS(7);
 128.917 +                    }
 128.918 +                    if (state->have + copy > state->nlen + state->ndist) {
 128.919 +                        strm->msg = (char *)"invalid bit length repeat";
 128.920 +                        state->mode = BAD;
 128.921 +                        break;
 128.922 +                    }
 128.923 +                    while (copy--)
 128.924 +                        state->lens[state->have++] = (unsigned short)len;
 128.925 +                }
 128.926 +            }
 128.927 +
 128.928 +            /* handle error breaks in while */
 128.929 +            if (state->mode == BAD) break;
 128.930 +
 128.931 +            /* build code tables */
 128.932 +            state->next = state->codes;
 128.933 +            state->lencode = (code const FAR *)(state->next);
 128.934 +            state->lenbits = 9;
 128.935 +            ret = inflate_table(LENS, state->lens, state->nlen, &(state->next),
 128.936 +                                &(state->lenbits), state->work);
 128.937 +            if (ret) {
 128.938 +                strm->msg = (char *)"invalid literal/lengths set";
 128.939 +                state->mode = BAD;
 128.940 +                break;
 128.941 +            }
 128.942 +            state->distcode = (code const FAR *)(state->next);
 128.943 +            state->distbits = 6;
 128.944 +            ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist,
 128.945 +                            &(state->next), &(state->distbits), state->work);
 128.946 +            if (ret) {
 128.947 +                strm->msg = (char *)"invalid distances set";
 128.948 +                state->mode = BAD;
 128.949 +                break;
 128.950 +            }
 128.951 +            Tracev((stderr, "inflate:       codes ok\n"));
 128.952 +            state->mode = LEN;
 128.953 +        case LEN:
 128.954 +            if (have >= 6 && left >= 258) {
 128.955 +                RESTORE();
 128.956 +                inflate_fast(strm, out);
 128.957 +                LOAD();
 128.958 +                break;
 128.959 +            }
 128.960 +            for (;;) {
 128.961 +                this = state->lencode[BITS(state->lenbits)];
 128.962 +                if ((unsigned)(this.bits) <= bits) break;
 128.963 +                PULLBYTE();
 128.964 +            }
 128.965 +            if (this.op && (this.op & 0xf0) == 0) {
 128.966 +                last = this;
 128.967 +                for (;;) {
 128.968 +                    this = state->lencode[last.val +
 128.969 +                            (BITS(last.bits + last.op) >> last.bits)];
 128.970 +                    if ((unsigned)(last.bits + this.bits) <= bits) break;
 128.971 +                    PULLBYTE();
 128.972 +                }
 128.973 +                DROPBITS(last.bits);
 128.974 +            }
 128.975 +            DROPBITS(this.bits);
 128.976 +            state->length = (unsigned)this.val;
 128.977 +            if ((int)(this.op) == 0) {
 128.978 +                Tracevv((stderr, this.val >= 0x20 && this.val < 0x7f ?
 128.979 +                        "inflate:         literal '%c'\n" :
 128.980 +                        "inflate:         literal 0x%02x\n", this.val));
 128.981 +                state->mode = LIT;
 128.982 +                break;
 128.983 +            }
 128.984 +            if (this.op & 32) {
 128.985 +                Tracevv((stderr, "inflate:         end of block\n"));
 128.986 +                state->mode = TYPE;
 128.987 +                break;
 128.988 +            }
 128.989 +            if (this.op & 64) {
 128.990 +                strm->msg = (char *)"invalid literal/length code";
 128.991 +                state->mode = BAD;
 128.992 +                break;
 128.993 +            }
 128.994 +            state->extra = (unsigned)(this.op) & 15;
 128.995 +            state->mode = LENEXT;
 128.996 +        case LENEXT:
 128.997 +            if (state->extra) {
 128.998 +                NEEDBITS(state->extra);
 128.999 +                state->length += BITS(state->extra);
128.1000 +                DROPBITS(state->extra);
128.1001 +            }
128.1002 +            Tracevv((stderr, "inflate:         length %u\n", state->length));
128.1003 +            state->mode = DIST;
128.1004 +        case DIST:
128.1005 +            for (;;) {
128.1006 +                this = state->distcode[BITS(state->distbits)];
128.1007 +                if ((unsigned)(this.bits) <= bits) break;
128.1008 +                PULLBYTE();
128.1009 +            }
128.1010 +            if ((this.op & 0xf0) == 0) {
128.1011 +                last = this;
128.1012 +                for (;;) {
128.1013 +                    this = state->distcode[last.val +
128.1014 +                            (BITS(last.bits + last.op) >> last.bits)];
128.1015 +                    if ((unsigned)(last.bits + this.bits) <= bits) break;
128.1016 +                    PULLBYTE();
128.1017 +                }
128.1018 +                DROPBITS(last.bits);
128.1019 +            }
128.1020 +            DROPBITS(this.bits);
128.1021 +            if (this.op & 64) {
128.1022 +                strm->msg = (char *)"invalid distance code";
128.1023 +                state->mode = BAD;
128.1024 +                break;
128.1025 +            }
128.1026 +            state->offset = (unsigned)this.val;
128.1027 +            state->extra = (unsigned)(this.op) & 15;
128.1028 +            state->mode = DISTEXT;
128.1029 +        case DISTEXT:
128.1030 +            if (state->extra) {
128.1031 +                NEEDBITS(state->extra);
128.1032 +                state->offset += BITS(state->extra);
128.1033 +                DROPBITS(state->extra);
128.1034 +            }
128.1035 +#ifdef INFLATE_STRICT
128.1036 +            if (state->offset > state->dmax) {
128.1037 +                strm->msg = (char *)"invalid distance too far back";
128.1038 +                state->mode = BAD;
128.1039 +                break;
128.1040 +            }
128.1041 +#endif
128.1042 +            if (state->offset > state->whave + out - left) {
128.1043 +                strm->msg = (char *)"invalid distance too far back";
128.1044 +                state->mode = BAD;
128.1045 +                break;
128.1046 +            }
128.1047 +            Tracevv((stderr, "inflate:         distance %u\n", state->offset));
128.1048 +            state->mode = MATCH;
128.1049 +        case MATCH:
128.1050 +            if (left == 0) goto inf_leave;
128.1051 +            copy = out - left;
128.1052 +            if (state->offset > copy) {         /* copy from window */
128.1053 +                copy = state->offset - copy;
128.1054 +                if (copy > state->write) {
128.1055 +                    copy -= state->write;
128.1056 +                    from = state->window + (state->wsize - copy);
128.1057 +                }
128.1058 +                else
128.1059 +                    from = state->window + (state->write - copy);
128.1060 +                if (copy > state->length) copy = state->length;
128.1061 +            }
128.1062 +            else {                              /* copy from output */
128.1063 +                from = put - state->offset;
128.1064 +                copy = state->length;
128.1065 +            }
128.1066 +            if (copy > left) copy = left;
128.1067 +            left -= copy;
128.1068 +            state->length -= copy;
128.1069 +            do {
128.1070 +                *put++ = *from++;
128.1071 +            } while (--copy);
128.1072 +            if (state->length == 0) state->mode = LEN;
128.1073 +            break;
128.1074 +        case LIT:
128.1075 +            if (left == 0) goto inf_leave;
128.1076 +            *put++ = (unsigned char)(state->length);
128.1077 +            left--;
128.1078 +            state->mode = LEN;
128.1079 +            break;
128.1080 +        case CHECK:
128.1081 +            if (state->wrap) {
128.1082 +                NEEDBITS(32);
128.1083 +                out -= left;
128.1084 +                strm->total_out += out;
128.1085 +                state->total += out;
128.1086 +                if (out)
128.1087 +                    strm->adler = state->check =
128.1088 +                        UPDATE(state->check, put - out, out);
128.1089 +                out = left;
128.1090 +                if ((
128.1091 +#ifdef GUNZIP
128.1092 +                     state->flags ? hold :
128.1093 +#endif
128.1094 +                     REVERSE(hold)) != state->check) {
128.1095 +                    strm->msg = (char *)"incorrect data check";
128.1096 +                    state->mode = BAD;
128.1097 +                    break;
128.1098 +                }
128.1099 +                INITBITS();
128.1100 +                Tracev((stderr, "inflate:   check matches trailer\n"));
128.1101 +            }
128.1102 +#ifdef GUNZIP
128.1103 +            state->mode = LENGTH;
128.1104 +        case LENGTH:
128.1105 +            if (state->wrap && state->flags) {
128.1106 +                NEEDBITS(32);
128.1107 +                if (hold != (state->total & 0xffffffffUL)) {
128.1108 +                    strm->msg = (char *)"incorrect length check";
128.1109 +                    state->mode = BAD;
128.1110 +                    break;
128.1111 +                }
128.1112 +                INITBITS();
128.1113 +                Tracev((stderr, "inflate:   length matches trailer\n"));
128.1114 +            }
128.1115 +#endif
128.1116 +            state->mode = DONE;
128.1117 +        case DONE:
128.1118 +            ret = Z_STREAM_END;
128.1119 +            goto inf_leave;
128.1120 +        case BAD:
128.1121 +            ret = Z_DATA_ERROR;
128.1122 +            goto inf_leave;
128.1123 +        case MEM:
128.1124 +            return Z_MEM_ERROR;
128.1125 +        case SYNC:
128.1126 +        default:
128.1127 +            return Z_STREAM_ERROR;
128.1128 +        }
128.1129 +
128.1130 +    /*
128.1131 +       Return from inflate(), updating the total counts and the check value.
128.1132 +       If there was no progress during the inflate() call, return a buffer
128.1133 +       error.  Call updatewindow() to create and/or update the window state.
128.1134 +       Note: a memory error from inflate() is non-recoverable.
128.1135 +     */
128.1136 +  inf_leave:
128.1137 +    RESTORE();
128.1138 +    if (state->wsize || (state->mode < CHECK && out != strm->avail_out))
128.1139 +        if (updatewindow(strm, out)) {
128.1140 +            state->mode = MEM;
128.1141 +            return Z_MEM_ERROR;
128.1142 +        }
128.1143 +    in -= strm->avail_in;
128.1144 +    out -= strm->avail_out;
128.1145 +    strm->total_in += in;
128.1146 +    strm->total_out += out;
128.1147 +    state->total += out;
128.1148 +    if (state->wrap && out)
128.1149 +        strm->adler = state->check =
128.1150 +            UPDATE(state->check, strm->next_out - out, out);
128.1151 +    strm->data_type = state->bits + (state->last ? 64 : 0) +
128.1152 +                      (state->mode == TYPE ? 128 : 0);
128.1153 +    if (((in == 0 && out == 0) || flush == Z_FINISH) && ret == Z_OK)
128.1154 +        ret = Z_BUF_ERROR;
128.1155 +    return ret;
128.1156 +}
128.1157 +
128.1158 +int ZEXPORT inflateEnd(strm)
128.1159 +z_streamp strm;
128.1160 +{
128.1161 +    struct inflate_state FAR *state;
128.1162 +    if (strm == Z_NULL || strm->state == Z_NULL || strm->zfree == (free_func)0)
128.1163 +        return Z_STREAM_ERROR;
128.1164 +    state = (struct inflate_state FAR *)strm->state;
128.1165 +    if (state->window != Z_NULL) ZFREE(strm, state->window);
128.1166 +    ZFREE(strm, strm->state);
128.1167 +    strm->state = Z_NULL;
128.1168 +    Tracev((stderr, "inflate: end\n"));
128.1169 +    return Z_OK;
128.1170 +}
128.1171 +
128.1172 +int ZEXPORT inflateSetDictionary(strm, dictionary, dictLength)
128.1173 +z_streamp strm;
128.1174 +const Bytef *dictionary;
128.1175 +uInt dictLength;
128.1176 +{
128.1177 +    struct inflate_state FAR *state;
128.1178 +    unsigned long id;
128.1179 +
128.1180 +    /* check state */
128.1181 +    if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
128.1182 +    state = (struct inflate_state FAR *)strm->state;
128.1183 +    if (state->wrap != 0 && state->mode != DICT)
128.1184 +        return Z_STREAM_ERROR;
128.1185 +
128.1186 +    /* check for correct dictionary id */
128.1187 +    if (state->mode == DICT) {
128.1188 +        id = adler32(0L, Z_NULL, 0);
128.1189 +        id = adler32(id, dictionary, dictLength);
128.1190 +        if (id != state->check)
128.1191 +            return Z_DATA_ERROR;
128.1192 +    }
128.1193 +
128.1194 +    /* copy dictionary to window */
128.1195 +    if (updatewindow(strm, strm->avail_out)) {
128.1196 +        state->mode = MEM;
128.1197 +        return Z_MEM_ERROR;
128.1198 +    }
128.1199 +    if (dictLength > state->wsize) {
128.1200 +        zmemcpy(state->window, dictionary + dictLength - state->wsize,
128.1201 +                state->wsize);
128.1202 +        state->whave = state->wsize;
128.1203 +    }
128.1204 +    else {
128.1205 +        zmemcpy(state->window + state->wsize - dictLength, dictionary,
128.1206 +                dictLength);
128.1207 +        state->whave = dictLength;
128.1208 +    }
128.1209 +    state->havedict = 1;
128.1210 +    Tracev((stderr, "inflate:   dictionary set\n"));
128.1211 +    return Z_OK;
128.1212 +}
128.1213 +
128.1214 +int ZEXPORT inflateGetHeader(strm, head)
128.1215 +z_streamp strm;
128.1216 +gz_headerp head;
128.1217 +{
128.1218 +    struct inflate_state FAR *state;
128.1219 +
128.1220 +    /* check state */
128.1221 +    if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
128.1222 +    state = (struct inflate_state FAR *)strm->state;
128.1223 +    if ((state->wrap & 2) == 0) return Z_STREAM_ERROR;
128.1224 +
128.1225 +    /* save header structure */
128.1226 +    state->head = head;
128.1227 +    head->done = 0;
128.1228 +    return Z_OK;
128.1229 +}
128.1230 +
128.1231 +/*
128.1232 +   Search buf[0..len-1] for the pattern: 0, 0, 0xff, 0xff.  Return when found
128.1233 +   or when out of input.  When called, *have is the number of pattern bytes
128.1234 +   found in order so far, in 0..3.  On return *have is updated to the new
128.1235 +   state.  If on return *have equals four, then the pattern was found and the
128.1236 +   return value is how many bytes were read including the last byte of the
128.1237 +   pattern.  If *have is less than four, then the pattern has not been found
128.1238 +   yet and the return value is len.  In the latter case, syncsearch() can be
128.1239 +   called again with more data and the *have state.  *have is initialized to
128.1240 +   zero for the first call.
128.1241 + */
128.1242 +local unsigned syncsearch(have, buf, len)
128.1243 +unsigned FAR *have;
128.1244 +unsigned char FAR *buf;
128.1245 +unsigned len;
128.1246 +{
128.1247 +    unsigned got;
128.1248 +    unsigned next;
128.1249 +
128.1250 +    got = *have;
128.1251 +    next = 0;
128.1252 +    while (next < len && got < 4) {
128.1253 +        if ((int)(buf[next]) == (got < 2 ? 0 : 0xff))
128.1254 +            got++;
128.1255 +        else if (buf[next])
128.1256 +            got = 0;
128.1257 +        else
128.1258 +            got = 4 - got;
128.1259 +        next++;
128.1260 +    }
128.1261 +    *have = got;
128.1262 +    return next;
128.1263 +}
128.1264 +
128.1265 +int ZEXPORT inflateSync(strm)
128.1266 +z_streamp strm;
128.1267 +{
128.1268 +    unsigned len;               /* number of bytes to look at or looked at */
128.1269 +    unsigned long in, out;      /* temporary to save total_in and total_out */
128.1270 +    unsigned char buf[4];       /* to restore bit buffer to byte string */
128.1271 +    struct inflate_state FAR *state;
128.1272 +
128.1273 +    /* check parameters */
128.1274 +    if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
128.1275 +    state = (struct inflate_state FAR *)strm->state;
128.1276 +    if (strm->avail_in == 0 && state->bits < 8) return Z_BUF_ERROR;
128.1277 +
128.1278 +    /* if first time, start search in bit buffer */
128.1279 +    if (state->mode != SYNC) {
128.1280 +        state->mode = SYNC;
128.1281 +        state->hold <<= state->bits & 7;
128.1282 +        state->bits -= state->bits & 7;
128.1283 +        len = 0;
128.1284 +        while (state->bits >= 8) {
128.1285 +            buf[len++] = (unsigned char)(state->hold);
128.1286 +            state->hold >>= 8;
128.1287 +            state->bits -= 8;
128.1288 +        }
128.1289 +        state->have = 0;
128.1290 +        syncsearch(&(state->have), buf, len);
128.1291 +    }
128.1292 +
128.1293 +    /* search available input */
128.1294 +    len = syncsearch(&(state->have), strm->next_in, strm->avail_in);
128.1295 +    strm->avail_in -= len;
128.1296 +    strm->next_in += len;
128.1297 +    strm->total_in += len;
128.1298 +
128.1299 +    /* return no joy or set up to restart inflate() on a new block */
128.1300 +    if (state->have != 4) return Z_DATA_ERROR;
128.1301 +    in = strm->total_in;  out = strm->total_out;
128.1302 +    inflateReset(strm);
128.1303 +    strm->total_in = in;  strm->total_out = out;
128.1304 +    state->mode = TYPE;
128.1305 +    return Z_OK;
128.1306 +}
128.1307 +
128.1308 +/*
128.1309 +   Returns true if inflate is currently at the end of a block generated by
128.1310 +   Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP
128.1311 +   implementation to provide an additional safety check. PPP uses
128.1312 +   Z_SYNC_FLUSH but removes the length bytes of the resulting empty stored
128.1313 +   block. When decompressing, PPP checks that at the end of input packet,
128.1314 +   inflate is waiting for these length bytes.
128.1315 + */
128.1316 +int ZEXPORT inflateSyncPoint(strm)
128.1317 +z_streamp strm;
128.1318 +{
128.1319 +    struct inflate_state FAR *state;
128.1320 +
128.1321 +    if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
128.1322 +    state = (struct inflate_state FAR *)strm->state;
128.1323 +    return state->mode == STORED && state->bits == 0;
128.1324 +}
128.1325 +
128.1326 +int ZEXPORT inflateCopy(dest, source)
128.1327 +z_streamp dest;
128.1328 +z_streamp source;
128.1329 +{
128.1330 +    struct inflate_state FAR *state;
128.1331 +    struct inflate_state FAR *copy;
128.1332 +    unsigned char FAR *window;
128.1333 +    unsigned wsize;
128.1334 +
128.1335 +    /* check input */
128.1336 +    if (dest == Z_NULL || source == Z_NULL || source->state == Z_NULL ||
128.1337 +        source->zalloc == (alloc_func)0 || source->zfree == (free_func)0)
128.1338 +        return Z_STREAM_ERROR;
128.1339 +    state = (struct inflate_state FAR *)source->state;
128.1340 +
128.1341 +    /* allocate space */
128.1342 +    copy = (struct inflate_state FAR *)
128.1343 +           ZALLOC(source, 1, sizeof(struct inflate_state));
128.1344 +    if (copy == Z_NULL) return Z_MEM_ERROR;
128.1345 +    window = Z_NULL;
128.1346 +    if (state->window != Z_NULL) {
128.1347 +        window = (unsigned char FAR *)
128.1348 +                 ZALLOC(source, 1U << state->wbits, sizeof(unsigned char));
128.1349 +        if (window == Z_NULL) {
128.1350 +            ZFREE(source, copy);
128.1351 +            return Z_MEM_ERROR;
128.1352 +        }
128.1353 +    }
128.1354 +
128.1355 +    /* copy state */
128.1356 +    zmemcpy(dest, source, sizeof(z_stream));
128.1357 +    zmemcpy(copy, state, sizeof(struct inflate_state));
128.1358 +    if (state->lencode >= state->codes &&
128.1359 +        state->lencode <= state->codes + ENOUGH - 1) {
128.1360 +        copy->lencode = copy->codes + (state->lencode - state->codes);
128.1361 +        copy->distcode = copy->codes + (state->distcode - state->codes);
128.1362 +    }
128.1363 +    copy->next = copy->codes + (state->next - state->codes);
128.1364 +    if (window != Z_NULL) {
128.1365 +        wsize = 1U << state->wbits;
128.1366 +        zmemcpy(window, state->window, wsize);
128.1367 +    }
128.1368 +    copy->window = window;
128.1369 +    dest->state = (struct internal_state FAR *)copy;
128.1370 +    return Z_OK;
128.1371 +}
   129.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   129.2 +++ b/libs/zlib/inflate.h	Sat Sep 19 05:51:51 2015 +0300
   129.3 @@ -0,0 +1,115 @@
   129.4 +/* inflate.h -- internal inflate state definition
   129.5 + * Copyright (C) 1995-2004 Mark Adler
   129.6 + * For conditions of distribution and use, see copyright notice in zlib.h
   129.7 + */
   129.8 +
   129.9 +/* WARNING: this file should *not* be used by applications. It is
  129.10 +   part of the implementation of the compression library and is
  129.11 +   subject to change. Applications should only use zlib.h.
  129.12 + */
  129.13 +
  129.14 +/* define NO_GZIP when compiling if you want to disable gzip header and
  129.15 +   trailer decoding by inflate().  NO_GZIP would be used to avoid linking in
  129.16 +   the crc code when it is not needed.  For shared libraries, gzip decoding
  129.17 +   should be left enabled. */
  129.18 +#ifndef NO_GZIP
  129.19 +#  define GUNZIP
  129.20 +#endif
  129.21 +
  129.22 +/* Possible inflate modes between inflate() calls */
  129.23 +typedef enum {
  129.24 +    HEAD,       /* i: waiting for magic header */
  129.25 +    FLAGS,      /* i: waiting for method and flags (gzip) */
  129.26 +    TIME,       /* i: waiting for modification time (gzip) */
  129.27 +    OS,         /* i: waiting for extra flags and operating system (gzip) */
  129.28 +    EXLEN,      /* i: waiting for extra length (gzip) */
  129.29 +    EXTRA,      /* i: waiting for extra bytes (gzip) */
  129.30 +    NAME,       /* i: waiting for end of file name (gzip) */
  129.31 +    COMMENT,    /* i: waiting for end of comment (gzip) */
  129.32 +    HCRC,       /* i: waiting for header crc (gzip) */
  129.33 +    DICTID,     /* i: waiting for dictionary check value */
  129.34 +    DICT,       /* waiting for inflateSetDictionary() call */
  129.35 +        TYPE,       /* i: waiting for type bits, including last-flag bit */
  129.36 +        TYPEDO,     /* i: same, but skip check to exit inflate on new block */
  129.37 +        STORED,     /* i: waiting for stored size (length and complement) */
  129.38 +        COPY,       /* i/o: waiting for input or output to copy stored block */
  129.39 +        TABLE,      /* i: waiting for dynamic block table lengths */
  129.40 +        LENLENS,    /* i: waiting for code length code lengths */
  129.41 +        CODELENS,   /* i: waiting for length/lit and distance code lengths */
  129.42 +            LEN,        /* i: waiting for length/lit code */
  129.43 +            LENEXT,     /* i: waiting for length extra bits */
  129.44 +            DIST,       /* i: waiting for distance code */
  129.45 +            DISTEXT,    /* i: waiting for distance extra bits */
  129.46 +            MATCH,      /* o: waiting for output space to copy string */
  129.47 +            LIT,        /* o: waiting for output space to write literal */
  129.48 +    CHECK,      /* i: waiting for 32-bit check value */
  129.49 +    LENGTH,     /* i: waiting for 32-bit length (gzip) */
  129.50 +    DONE,       /* finished check, done -- remain here until reset */
  129.51 +    BAD,        /* got a data error -- remain here until reset */
  129.52 +    MEM,        /* got an inflate() memory error -- remain here until reset */
  129.53 +    SYNC        /* looking for synchronization bytes to restart inflate() */
  129.54 +} inflate_mode;
  129.55 +
  129.56 +/*
  129.57 +    State transitions between above modes -
  129.58 +
  129.59 +    (most modes can go to the BAD or MEM mode -- not shown for clarity)
  129.60 +
  129.61 +    Process header:
  129.62 +        HEAD -> (gzip) or (zlib)
  129.63 +        (gzip) -> FLAGS -> TIME -> OS -> EXLEN -> EXTRA -> NAME
  129.64 +        NAME -> COMMENT -> HCRC -> TYPE
  129.65 +        (zlib) -> DICTID or TYPE
  129.66 +        DICTID -> DICT -> TYPE
  129.67 +    Read deflate blocks:
  129.68 +            TYPE -> STORED or TABLE or LEN or CHECK
  129.69 +            STORED -> COPY -> TYPE
  129.70 +            TABLE -> LENLENS -> CODELENS -> LEN
  129.71 +    Read deflate codes:
  129.72 +                LEN -> LENEXT or LIT or TYPE
  129.73 +                LENEXT -> DIST -> DISTEXT -> MATCH -> LEN
  129.74 +                LIT -> LEN
  129.75 +    Process trailer:
  129.76 +        CHECK -> LENGTH -> DONE
  129.77 + */
  129.78 +
  129.79 +/* state maintained between inflate() calls.  Approximately 7K bytes. */
  129.80 +struct inflate_state {
  129.81 +    inflate_mode mode;          /* current inflate mode */
  129.82 +    int last;                   /* true if processing last block */
  129.83 +    int wrap;                   /* bit 0 true for zlib, bit 1 true for gzip */
  129.84 +    int havedict;               /* true if dictionary provided */
  129.85 +    int flags;                  /* gzip header method and flags (0 if zlib) */
  129.86 +    unsigned dmax;              /* zlib header max distance (INFLATE_STRICT) */
  129.87 +    unsigned long check;        /* protected copy of check value */
  129.88 +    unsigned long total;        /* protected copy of output count */
  129.89 +    gz_headerp head;            /* where to save gzip header information */
  129.90 +        /* sliding window */
  129.91 +    unsigned wbits;             /* log base 2 of requested window size */
  129.92 +    unsigned wsize;             /* window size or zero if not using window */
  129.93 +    unsigned whave;             /* valid bytes in the window */
  129.94 +    unsigned write;             /* window write index */
  129.95 +    unsigned char FAR *window;  /* allocated sliding window, if needed */
  129.96 +        /* bit accumulator */
  129.97 +    unsigned long hold;         /* input bit accumulator */
  129.98 +    unsigned bits;              /* number of bits in "in" */
  129.99 +        /* for string and stored block copying */
 129.100 +    unsigned length;            /* literal or length of data to copy */
 129.101 +    unsigned offset;            /* distance back to copy string from */
 129.102 +        /* for table and code decoding */
 129.103 +    unsigned extra;             /* extra bits needed */
 129.104 +        /* fixed and dynamic code tables */
 129.105 +    code const FAR *lencode;    /* starting table for length/literal codes */
 129.106 +    code const FAR *distcode;   /* starting table for distance codes */
 129.107 +    unsigned lenbits;           /* index bits for lencode */
 129.108 +    unsigned distbits;          /* index bits for distcode */
 129.109 +        /* dynamic table building */
 129.110 +    unsigned ncode;             /* number of code length code lengths */
 129.111 +    unsigned nlen;              /* number of length code lengths */
 129.112 +    unsigned ndist;             /* number of distance code lengths */
 129.113 +    unsigned have;              /* number of code lengths in lens[] */
 129.114 +    code FAR *next;             /* next available space in codes[] */
 129.115 +    unsigned short lens[320];   /* temporary storage for code lengths */
 129.116 +    unsigned short work[288];   /* work area for code table building */
 129.117 +    code codes[ENOUGH];         /* space for code tables */
 129.118 +};
   130.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   130.2 +++ b/libs/zlib/inftrees.c	Sat Sep 19 05:51:51 2015 +0300
   130.3 @@ -0,0 +1,329 @@
   130.4 +/* inftrees.c -- generate Huffman trees for efficient decoding
   130.5 + * Copyright (C) 1995-2005 Mark Adler
   130.6 + * For conditions of distribution and use, see copyright notice in zlib.h
   130.7 + */
   130.8 +
   130.9 +#include "zutil.h"
  130.10 +#include "inftrees.h"
  130.11 +
  130.12 +#define MAXBITS 15
  130.13 +
  130.14 +const char inflate_copyright[] =
  130.15 +   " inflate 1.2.3 Copyright 1995-2005 Mark Adler ";
  130.16 +/*
  130.17 +  If you use the zlib library in a product, an acknowledgment is welcome
  130.18 +  in the documentation of your product. If for some reason you cannot
  130.19 +  include such an acknowledgment, I would appreciate that you keep this
  130.20 +  copyright string in the executable of your product.
  130.21 + */
  130.22 +
  130.23 +/*
  130.24 +   Build a set of tables to decode the provided canonical Huffman code.
  130.25 +   The code lengths are lens[0..codes-1].  The result starts at *table,
  130.26 +   whose indices are 0..2^bits-1.  work is a writable array of at least
  130.27 +   lens shorts, which is used as a work area.  type is the type of code
  130.28 +   to be generated, CODES, LENS, or DISTS.  On return, zero is success,
  130.29 +   -1 is an invalid code, and +1 means that ENOUGH isn't enough.  table
  130.30 +   on return points to the next available entry's address.  bits is the
  130.31 +   requested root table index bits, and on return it is the actual root
  130.32 +   table index bits.  It will differ if the request is greater than the
  130.33 +   longest code or if it is less than the shortest code.
  130.34 + */
  130.35 +int inflate_table(type, lens, codes, table, bits, work)
  130.36 +codetype type;
  130.37 +unsigned short FAR *lens;
  130.38 +unsigned codes;
  130.39 +code FAR * FAR *table;
  130.40 +unsigned FAR *bits;
  130.41 +unsigned short FAR *work;
  130.42 +{
  130.43 +    unsigned len;               /* a code's length in bits */
  130.44 +    unsigned sym;               /* index of code symbols */
  130.45 +    unsigned min, max;          /* minimum and maximum code lengths */
  130.46 +    unsigned root;              /* number of index bits for root table */
  130.47 +    unsigned curr;              /* number of index bits for current table */
  130.48 +    unsigned drop;              /* code bits to drop for sub-table */
  130.49 +    int left;                   /* number of prefix codes available */
  130.50 +    unsigned used;              /* code entries in table used */
  130.51 +    unsigned huff;              /* Huffman code */
  130.52 +    unsigned incr;              /* for incrementing code, index */
  130.53 +    unsigned fill;              /* index for replicating entries */
  130.54 +    unsigned low;               /* low bits for current root entry */
  130.55 +    unsigned mask;              /* mask for low root bits */
  130.56 +    code this;                  /* table entry for duplication */
  130.57 +    code FAR *next;             /* next available space in table */
  130.58 +    const unsigned short FAR *base;     /* base value table to use */
  130.59 +    const unsigned short FAR *extra;    /* extra bits table to use */
  130.60 +    int end;                    /* use base and extra for symbol > end */
  130.61 +    unsigned short count[MAXBITS+1];    /* number of codes of each length */
  130.62 +    unsigned short offs[MAXBITS+1];     /* offsets in table for each length */
  130.63 +    static const unsigned short lbase[31] = { /* Length codes 257..285 base */
  130.64 +        3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
  130.65 +        35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
  130.66 +    static const unsigned short lext[31] = { /* Length codes 257..285 extra */
  130.67 +        16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18,
  130.68 +        19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 16, 201, 196};
  130.69 +    static const unsigned short dbase[32] = { /* Distance codes 0..29 base */
  130.70 +        1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
  130.71 +        257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
  130.72 +        8193, 12289, 16385, 24577, 0, 0};
  130.73 +    static const unsigned short dext[32] = { /* Distance codes 0..29 extra */
  130.74 +        16, 16, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22,
  130.75 +        23, 23, 24, 24, 25, 25, 26, 26, 27, 27,
  130.76 +        28, 28, 29, 29, 64, 64};
  130.77 +
  130.78 +    /*
  130.79 +       Process a set of code lengths to create a canonical Huffman code.  The
  130.80 +       code lengths are lens[0..codes-1].  Each length corresponds to the
  130.81 +       symbols 0..codes-1.  The Huffman code is generated by first sorting the
  130.82 +       symbols by length from short to long, and retaining the symbol order
  130.83 +       for codes with equal lengths.  Then the code starts with all zero bits
  130.84 +       for the first code of the shortest length, and the codes are integer
  130.85 +       increments for the same length, and zeros are appended as the length
  130.86 +       increases.  For the deflate format, these bits are stored backwards
  130.87 +       from their more natural integer increment ordering, and so when the
  130.88 +       decoding tables are built in the large loop below, the integer codes
  130.89 +       are incremented backwards.
  130.90 +
  130.91 +       This routine assumes, but does not check, that all of the entries in
  130.92 +       lens[] are in the range 0..MAXBITS.  The caller must assure this.
  130.93 +       1..MAXBITS is interpreted as that code length.  zero means that that
  130.94 +       symbol does not occur in this code.
  130.95 +
  130.96 +       The codes are sorted by computing a count of codes for each length,
  130.97 +       creating from that a table of starting indices for each length in the
  130.98 +       sorted table, and then entering the symbols in order in the sorted
  130.99 +       table.  The sorted table is work[], with that space being provided by
 130.100 +       the caller.
 130.101 +
 130.102 +       The length counts are used for other purposes as well, i.e. finding
 130.103 +       the minimum and maximum length codes, determining if there are any
 130.104 +       codes at all, checking for a valid set of lengths, and looking ahead
 130.105 +       at length counts to determine sub-table sizes when building the
 130.106 +       decoding tables.
 130.107 +     */
 130.108 +
 130.109 +    /* accumulate lengths for codes (assumes lens[] all in 0..MAXBITS) */
 130.110 +    for (len = 0; len <= MAXBITS; len++)
 130.111 +        count[len] = 0;
 130.112 +    for (sym = 0; sym < codes; sym++)
 130.113 +        count[lens[sym]]++;
 130.114 +
 130.115 +    /* bound code lengths, force root to be within code lengths */
 130.116 +    root = *bits;
 130.117 +    for (max = MAXBITS; max >= 1; max--)
 130.118 +        if (count[max] != 0) break;
 130.119 +    if (root > max) root = max;
 130.120 +    if (max == 0) {                     /* no symbols to code at all */
 130.121 +        this.op = (unsigned char)64;    /* invalid code marker */
 130.122 +        this.bits = (unsigned char)1;
 130.123 +        this.val = (unsigned short)0;
 130.124 +        *(*table)++ = this;             /* make a table to force an error */
 130.125 +        *(*table)++ = this;
 130.126 +        *bits = 1;
 130.127 +        return 0;     /* no symbols, but wait for decoding to report error */
 130.128 +    }
 130.129 +    for (min = 1; min <= MAXBITS; min++)
 130.130 +        if (count[min] != 0) break;
 130.131 +    if (root < min) root = min;
 130.132 +
 130.133 +    /* check for an over-subscribed or incomplete set of lengths */
 130.134 +    left = 1;
 130.135 +    for (len = 1; len <= MAXBITS; len++) {
 130.136 +        left <<= 1;
 130.137 +        left -= count[len];
 130.138 +        if (left < 0) return -1;        /* over-subscribed */
 130.139 +    }
 130.140 +    if (left > 0 && (type == CODES || max != 1))
 130.141 +        return -1;                      /* incomplete set */
 130.142 +
 130.143 +    /* generate offsets into symbol table for each length for sorting */
 130.144 +    offs[1] = 0;
 130.145 +    for (len = 1; len < MAXBITS; len++)
 130.146 +        offs[len + 1] = offs[len] + count[len];
 130.147 +
 130.148 +    /* sort symbols by length, by symbol order within each length */
 130.149 +    for (sym = 0; sym < codes; sym++)
 130.150 +        if (lens[sym] != 0) work[offs[lens[sym]]++] = (unsigned short)sym;
 130.151 +
 130.152 +    /*
 130.153 +       Create and fill in decoding tables.  In this loop, the table being
 130.154 +       filled is at next and has curr index bits.  The code being used is huff
 130.155 +       with length len.  That code is converted to an index by dropping drop
 130.156 +       bits off of the bottom.  For codes where len is less than drop + curr,
 130.157 +       those top drop + curr - len bits are incremented through all values to
 130.158 +       fill the table with replicated entries.
 130.159 +
 130.160 +       root is the number of index bits for the root table.  When len exceeds
 130.161 +       root, sub-tables are created pointed to by the root entry with an index
 130.162 +       of the low root bits of huff.  This is saved in low to check for when a
 130.163 +       new sub-table should be started.  drop is zero when the root table is
 130.164 +       being filled, and drop is root when sub-tables are being filled.
 130.165 +
 130.166 +       When a new sub-table is needed, it is necessary to look ahead in the
 130.167 +       code lengths to determine what size sub-table is needed.  The length
 130.168 +       counts are used for this, and so count[] is decremented as codes are
 130.169 +       entered in the tables.
 130.170 +
 130.171 +       used keeps track of how many table entries have been allocated from the
 130.172 +       provided *table space.  It is checked when a LENS table is being made
 130.173 +       against the space in *table, ENOUGH, minus the maximum space needed by
 130.174 +       the worst case distance code, MAXD.  This should never happen, but the
 130.175 +       sufficiency of ENOUGH has not been proven exhaustively, hence the check.
 130.176 +       This assumes that when type == LENS, bits == 9.
 130.177 +
 130.178 +       sym increments through all symbols, and the loop terminates when
 130.179 +       all codes of length max, i.e. all codes, have been processed.  This
 130.180 +       routine permits incomplete codes, so another loop after this one fills
 130.181 +       in the rest of the decoding tables with invalid code markers.
 130.182 +     */
 130.183 +
 130.184 +    /* set up for code type */
 130.185 +    switch (type) {
 130.186 +    case CODES:
 130.187 +        base = extra = work;    /* dummy value--not used */
 130.188 +        end = 19;
 130.189 +        break;
 130.190 +    case LENS:
 130.191 +        base = lbase;
 130.192 +        base -= 257;
 130.193 +        extra = lext;
 130.194 +        extra -= 257;
 130.195 +        end = 256;
 130.196 +        break;
 130.197 +    default:            /* DISTS */
 130.198 +        base = dbase;
 130.199 +        extra = dext;
 130.200 +        end = -1;
 130.201 +    }
 130.202 +
 130.203 +    /* initialize state for loop */
 130.204 +    huff = 0;                   /* starting code */
 130.205 +    sym = 0;                    /* starting code symbol */
 130.206 +    len = min;                  /* starting code length */
 130.207 +    next = *table;              /* current table to fill in */
 130.208 +    curr = root;                /* current table index bits */
 130.209 +    drop = 0;                   /* current bits to drop from code for index */
 130.210 +    low = (unsigned)(-1);       /* trigger new sub-table when len > root */
 130.211 +    used = 1U << root;          /* use root table entries */
 130.212 +    mask = used - 1;            /* mask for comparing low */
 130.213 +
 130.214 +    /* check available table space */
 130.215 +    if (type == LENS && used >= ENOUGH - MAXD)
 130.216 +        return 1;
 130.217 +
 130.218 +    /* process all codes and make table entries */
 130.219 +    for (;;) {
 130.220 +        /* create table entry */
 130.221 +        this.bits = (unsigned char)(len - drop);
 130.222 +        if ((int)(work[sym]) < end) {
 130.223 +            this.op = (unsigned char)0;
 130.224 +            this.val = work[sym];
 130.225 +        }
 130.226 +        else if ((int)(work[sym]) > end) {
 130.227 +            this.op = (unsigned char)(extra[work[sym]]);
 130.228 +            this.val = base[work[sym]];
 130.229 +        }
 130.230 +        else {
 130.231 +            this.op = (unsigned char)(32 + 64);         /* end of block */
 130.232 +            this.val = 0;
 130.233 +        }
 130.234 +
 130.235 +        /* replicate for those indices with low len bits equal to huff */
 130.236 +        incr = 1U << (len - drop);
 130.237 +        fill = 1U << curr;
 130.238 +        min = fill;                 /* save offset to next table */
 130.239 +        do {
 130.240 +            fill -= incr;
 130.241 +            next[(huff >> drop) + fill] = this;
 130.242 +        } while (fill != 0);
 130.243 +
 130.244 +        /* backwards increment the len-bit code huff */
 130.245 +        incr = 1U << (len - 1);
 130.246 +        while (huff & incr)
 130.247 +            incr >>= 1;
 130.248 +        if (incr != 0) {
 130.249 +            huff &= incr - 1;
 130.250 +            huff += incr;
 130.251 +        }
 130.252 +        else
 130.253 +            huff = 0;
 130.254 +
 130.255 +        /* go to next symbol, update count, len */
 130.256 +        sym++;
 130.257 +        if (--(count[len]) == 0) {
 130.258 +            if (len == max) break;
 130.259 +            len = lens[work[sym]];
 130.260 +        }
 130.261 +
 130.262 +        /* create new sub-table if needed */
 130.263 +        if (len > root && (huff & mask) != low) {
 130.264 +            /* if first time, transition to sub-tables */
 130.265 +            if (drop == 0)
 130.266 +                drop = root;
 130.267 +
 130.268 +            /* increment past last table */
 130.269 +            next += min;            /* here min is 1 << curr */
 130.270 +
 130.271 +            /* determine length of next table */
 130.272 +            curr = len - drop;
 130.273 +            left = (int)(1 << curr);
 130.274 +            while (curr + drop < max) {
 130.275 +                left -= count[curr + drop];
 130.276 +                if (left <= 0) break;
 130.277 +                curr++;
 130.278 +                left <<= 1;
 130.279 +            }
 130.280 +
 130.281 +            /* check for enough space */
 130.282 +            used += 1U << curr;
 130.283 +            if (type == LENS && used >= ENOUGH - MAXD)
 130.284 +                return 1;
 130.285 +
 130.286 +            /* point entry in root table to sub-table */
 130.287 +            low = huff & mask;
 130.288 +            (*table)[low].op = (unsigned char)curr;
 130.289 +            (*table)[low].bits = (unsigned char)root;
 130.290 +            (*table)[low].val = (unsigned short)(next - *table);
 130.291 +        }
 130.292 +    }
 130.293 +
 130.294 +    /*
 130.295 +       Fill in rest of table for incomplete codes.  This loop is similar to the
 130.296 +       loop above in incrementing huff for table indices.  It is assumed that
 130.297 +       len is equal to curr + drop, so there is no loop needed to increment
 130.298 +       through high index bits.  When the current sub-table is filled, the loop
 130.299 +       drops back to the root table to fill in any remaining entries there.
 130.300 +     */
 130.301 +    this.op = (unsigned char)64;                /* invalid code marker */
 130.302 +    this.bits = (unsigned char)(len - drop);
 130.303 +    this.val = (unsigned short)0;
 130.304 +    while (huff != 0) {
 130.305 +        /* when done with sub-table, drop back to root table */
 130.306 +        if (drop != 0 && (huff & mask) != low) {
 130.307 +            drop = 0;
 130.308 +            len = root;
 130.309 +            next = *table;
 130.310 +            this.bits = (unsigned char)len;
 130.311 +        }
 130.312 +
 130.313 +        /* put invalid code marker in table */
 130.314 +        next[huff >> drop] = this;
 130.315 +
 130.316 +        /* backwards increment the len-bit code huff */
 130.317 +        incr = 1U << (len - 1);
 130.318 +        while (huff & incr)
 130.319 +            incr >>= 1;
 130.320 +        if (incr != 0) {
 130.321 +            huff &= incr - 1;
 130.322 +            huff += incr;
 130.323 +        }
 130.324 +        else
 130.325 +            huff = 0;
 130.326 +    }
 130.327 +
 130.328 +    /* set return parameters */
 130.329 +    *table += used;
 130.330 +    *bits = root;
 130.331 +    return 0;
 130.332 +}
   131.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   131.2 +++ b/libs/zlib/inftrees.h	Sat Sep 19 05:51:51 2015 +0300
   131.3 @@ -0,0 +1,55 @@
   131.4 +/* inftrees.h -- header to use inftrees.c
   131.5 + * Copyright (C) 1995-2005 Mark Adler
   131.6 + * For conditions of distribution and use, see copyright notice in zlib.h
   131.7 + */
   131.8 +
   131.9 +/* WARNING: this file should *not* be used by applications. It is
  131.10 +   part of the implementation of the compression library and is
  131.11 +   subject to change. Applications should only use zlib.h.
  131.12 + */
  131.13 +
  131.14 +/* Structure for decoding tables.  Each entry provides either the
  131.15 +   information needed to do the operation requested by the code that
  131.16 +   indexed that table entry, or it provides a pointer to another
  131.17 +   table that indexes more bits of the code.  op indicates whether
  131.18 +   the entry is a pointer to another table, a literal, a length or
  131.19 +   distance, an end-of-block, or an invalid code.  For a table
  131.20 +   pointer, the low four bits of op is the number of index bits of
  131.21 +   that table.  For a length or distance, the low four bits of op
  131.22 +   is the number of extra bits to get after the code.  bits is
  131.23 +   the number of bits in this code or part of the code to drop off
  131.24 +   of the bit buffer.  val is the actual byte to output in the case
  131.25 +   of a literal, the base length or distance, or the offset from
  131.26 +   the current table to the next table.  Each entry is four bytes. */
  131.27 +typedef struct {
  131.28 +    unsigned char op;           /* operation, extra bits, table bits */
  131.29 +    unsigned char bits;         /* bits in this part of the code */
  131.30 +    unsigned short val;         /* offset in table or code value */
  131.31 +} code;
  131.32 +
  131.33 +/* op values as set by inflate_table():
  131.34 +    00000000 - literal
  131.35 +    0000tttt - table link, tttt != 0 is the number of table index bits
  131.36 +    0001eeee - length or distance, eeee is the number of extra bits
  131.37 +    01100000 - end of block
  131.38 +    01000000 - invalid code
  131.39 + */
  131.40 +
  131.41 +/* Maximum size of dynamic tree.  The maximum found in a long but non-
  131.42 +   exhaustive search was 1444 code structures (852 for length/literals
  131.43 +   and 592 for distances, the latter actually the result of an
  131.44 +   exhaustive search).  The true maximum is not known, but the value
  131.45 +   below is more than safe. */
  131.46 +#define ENOUGH 2048
  131.47 +#define MAXD 592
  131.48 +
  131.49 +/* Type of code to build for inftable() */
  131.50 +typedef enum {
  131.51 +    CODES,
  131.52 +    LENS,
  131.53 +    DISTS
  131.54 +} codetype;
  131.55 +
  131.56 +extern int inflate_table OF((codetype type, unsigned short FAR *lens,
  131.57 +                             unsigned codes, code FAR * FAR *table,
  131.58 +                             unsigned FAR *bits, unsigned short FAR *work));
   132.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   132.2 +++ b/libs/zlib/trees.c	Sat Sep 19 05:51:51 2015 +0300
   132.3 @@ -0,0 +1,1219 @@
   132.4 +/* trees.c -- output deflated data using Huffman coding
   132.5 + * Copyright (C) 1995-2005 Jean-loup Gailly
   132.6 + * For conditions of distribution and use, see copyright notice in zlib.h
   132.7 + */
   132.8 +
   132.9 +/*
  132.10 + *  ALGORITHM
  132.11 + *
  132.12 + *      The "deflation" process uses several Huffman trees. The more
  132.13 + *      common source values are represented by shorter bit sequences.
  132.14 + *
  132.15 + *      Each code tree is stored in a compressed form which is itself
  132.16 + * a Huffman encoding of the lengths of all the code strings (in
  132.17 + * ascending order by source values).  The actual code strings are
  132.18 + * reconstructed from the lengths in the inflate process, as described
  132.19 + * in the deflate specification.
  132.20 + *
  132.21 + *  REFERENCES
  132.22 + *
  132.23 + *      Deutsch, L.P.,"'Deflate' Compressed Data Format Specification".
  132.24 + *      Available in ftp.uu.net:/pub/archiving/zip/doc/deflate-1.1.doc
  132.25 + *
  132.26 + *      Storer, James A.
  132.27 + *          Data Compression:  Methods and Theory, pp. 49-50.
  132.28 + *          Computer Science Press, 1988.  ISBN 0-7167-8156-5.
  132.29 + *
  132.30 + *      Sedgewick, R.
  132.31 + *          Algorithms, p290.
  132.32 + *          Addison-Wesley, 1983. ISBN 0-201-06672-6.
  132.33 + */
  132.34 +
  132.35 +/* @(#) $Id$ */
  132.36 +
  132.37 +/* #define GEN_TREES_H */
  132.38 +
  132.39 +#include "deflate.h"
  132.40 +
  132.41 +#ifdef DEBUG
  132.42 +#  include <ctype.h>
  132.43 +#endif
  132.44 +
  132.45 +/* ===========================================================================
  132.46 + * Constants
  132.47 + */
  132.48 +
  132.49 +#define MAX_BL_BITS 7
  132.50 +/* Bit length codes must not exceed MAX_BL_BITS bits */
  132.51 +
  132.52 +#define END_BLOCK 256
  132.53 +/* end of block literal code */
  132.54 +
  132.55 +#define REP_3_6      16
  132.56 +/* repeat previous bit length 3-6 times (2 bits of repeat count) */
  132.57 +
  132.58 +#define REPZ_3_10    17
  132.59 +/* repeat a zero length 3-10 times  (3 bits of repeat count) */
  132.60 +
  132.61 +#define REPZ_11_138  18
  132.62 +/* repeat a zero length 11-138 times  (7 bits of repeat count) */
  132.63 +
  132.64 +local const int extra_lbits[LENGTH_CODES] /* extra bits for each length code */
  132.65 +   = {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};
  132.66 +
  132.67 +local const int extra_dbits[D_CODES] /* extra bits for each distance code */
  132.68 +   = {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};
  132.69 +
  132.70 +local const int extra_blbits[BL_CODES]/* extra bits for each bit length code */
  132.71 +   = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,3,7};
  132.72 +
  132.73 +local const uch bl_order[BL_CODES]
  132.74 +   = {16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15};
  132.75 +/* The lengths of the bit length codes are sent in order of decreasing
  132.76 + * probability, to avoid transmitting the lengths for unused bit length codes.
  132.77 + */
  132.78 +
  132.79 +#define Buf_size (8 * 2*sizeof(char))
  132.80 +/* Number of bits used within bi_buf. (bi_buf might be implemented on
  132.81 + * more than 16 bits on some systems.)
  132.82 + */
  132.83 +
  132.84 +/* ===========================================================================
  132.85 + * Local data. These are initialized only once.
  132.86 + */
  132.87 +
  132.88 +#define DIST_CODE_LEN  512 /* see definition of array dist_code below */
  132.89 +
  132.90 +#if defined(GEN_TREES_H) || !defined(STDC)
  132.91 +/* non ANSI compilers may not accept trees.h */
  132.92 +
  132.93 +local ct_data static_ltree[L_CODES+2];
  132.94 +/* The static literal tree. Since the bit lengths are imposed, there is no
  132.95 + * need for the L_CODES extra codes used during heap construction. However
  132.96 + * The codes 286 and 287 are needed to build a canonical tree (see _tr_init
  132.97 + * below).
  132.98 + */
  132.99 +
 132.100 +local ct_data static_dtree[D_CODES];
 132.101 +/* The static distance tree. (Actually a trivial tree since all codes use
 132.102 + * 5 bits.)
 132.103 + */
 132.104 +
 132.105 +uch _dist_code[DIST_CODE_LEN];
 132.106 +/* Distance codes. The first 256 values correspond to the distances
 132.107 + * 3 .. 258, the last 256 values correspond to the top 8 bits of
 132.108 + * the 15 bit distances.
 132.109 + */
 132.110 +
 132.111 +uch _length_code[MAX_MATCH-MIN_MATCH+1];
 132.112 +/* length code for each normalized match length (0 == MIN_MATCH) */
 132.113 +
 132.114 +local int base_length[LENGTH_CODES];
 132.115 +/* First normalized length for each code (0 = MIN_MATCH) */
 132.116 +
 132.117 +local int base_dist[D_CODES];
 132.118 +/* First normalized distance for each code (0 = distance of 1) */
 132.119 +
 132.120 +#else
 132.121 +#  include "trees.h"
 132.122 +#endif /* GEN_TREES_H */
 132.123 +
 132.124 +struct static_tree_desc_s {
 132.125 +    const ct_data *static_tree;  /* static tree or NULL */
 132.126 +    const intf *extra_bits;      /* extra bits for each code or NULL */
 132.127 +    int     extra_base;          /* base index for extra_bits */
 132.128 +    int     elems;               /* max number of elements in the tree */
 132.129 +    int     max_length;          /* max bit length for the codes */
 132.130 +};
 132.131 +
 132.132 +local static_tree_desc  static_l_desc =
 132.133 +{static_ltree, extra_lbits, LITERALS+1, L_CODES, MAX_BITS};
 132.134 +
 132.135 +local static_tree_desc  static_d_desc =
 132.136 +{static_dtree, extra_dbits, 0,          D_CODES, MAX_BITS};
 132.137 +
 132.138 +local static_tree_desc  static_bl_desc =
 132.139 +{(const ct_data *)0, extra_blbits, 0,   BL_CODES, MAX_BL_BITS};
 132.140 +
 132.141 +/* ===========================================================================
 132.142 + * Local (static) routines in this file.
 132.143 + */
 132.144 +
 132.145 +local void tr_static_init OF((void));
 132.146 +local void init_block     OF((deflate_state *s));
 132.147 +local void pqdownheap     OF((deflate_state *s, ct_data *tree, int k));
 132.148 +local void gen_bitlen     OF((deflate_state *s, tree_desc *desc));
 132.149 +local void gen_codes      OF((ct_data *tree, int max_code, ushf *bl_count));
 132.150 +local void build_tree     OF((deflate_state *s, tree_desc *desc));
 132.151 +local void scan_tree      OF((deflate_state *s, ct_data *tree, int max_code));
 132.152 +local void send_tree      OF((deflate_state *s, ct_data *tree, int max_code));
 132.153 +local int  build_bl_tree  OF((deflate_state *s));
 132.154 +local void send_all_trees OF((deflate_state *s, int lcodes, int dcodes,
 132.155 +                              int blcodes));
 132.156 +local void compress_block OF((deflate_state *s, ct_data *ltree,
 132.157 +                              ct_data *dtree));
 132.158 +local void set_data_type  OF((deflate_state *s));
 132.159 +local unsigned bi_reverse OF((unsigned value, int length));
 132.160 +local void bi_windup      OF((deflate_state *s));
 132.161 +local void bi_flush       OF((deflate_state *s));
 132.162 +local void copy_block     OF((deflate_state *s, charf *buf, unsigned len,
 132.163 +                              int header));
 132.164 +
 132.165 +#ifdef GEN_TREES_H
 132.166 +local void gen_trees_header OF((void));
 132.167 +#endif
 132.168 +
 132.169 +#ifndef DEBUG
 132.170 +#  define send_code(s, c, tree) send_bits(s, tree[c].Code, tree[c].Len)
 132.171 +   /* Send a code of the given tree. c and tree must not have side effects */
 132.172 +
 132.173 +#else /* DEBUG */
 132.174 +#  define send_code(s, c, tree) \
 132.175 +     { if (z_verbose>2) fprintf(stderr,"\ncd %3d ",(c)); \
 132.176 +       send_bits(s, tree[c].Code, tree[c].Len); }
 132.177 +#endif
 132.178 +
 132.179 +/* ===========================================================================
 132.180 + * Output a short LSB first on the stream.
 132.181 + * IN assertion: there is enough room in pendingBuf.
 132.182 + */
 132.183 +#define put_short(s, w) { \
 132.184 +    put_byte(s, (uch)((w) & 0xff)); \
 132.185 +    put_byte(s, (uch)((ush)(w) >> 8)); \
 132.186 +}
 132.187 +
 132.188 +/* ===========================================================================
 132.189 + * Send a value on a given number of bits.
 132.190 + * IN assertion: length <= 16 and value fits in length bits.
 132.191 + */
 132.192 +#ifdef DEBUG
 132.193 +local void send_bits      OF((deflate_state *s, int value, int length));
 132.194 +
 132.195 +local void send_bits(s, value, length)
 132.196 +    deflate_state *s;
 132.197 +    int value;  /* value to send */
 132.198 +    int length; /* number of bits */
 132.199 +{
 132.200 +    Tracevv((stderr," l %2d v %4x ", length, value));
 132.201 +    Assert(length > 0 && length <= 15, "invalid length");
 132.202 +    s->bits_sent += (ulg)length;
 132.203 +
 132.204 +    /* If not enough room in bi_buf, use (valid) bits from bi_buf and
 132.205 +     * (16 - bi_valid) bits from value, leaving (width - (16-bi_valid))
 132.206 +     * unused bits in value.
 132.207 +     */
 132.208 +    if (s->bi_valid > (int)Buf_size - length) {
 132.209 +        s->bi_buf |= (value << s->bi_valid);
 132.210 +        put_short(s, s->bi_buf);
 132.211 +        s->bi_buf = (ush)value >> (Buf_size - s->bi_valid);
 132.212 +        s->bi_valid += length - Buf_size;
 132.213 +    } else {
 132.214 +        s->bi_buf |= value << s->bi_valid;
 132.215 +        s->bi_valid += length;
 132.216 +    }
 132.217 +}
 132.218 +#else /* !DEBUG */
 132.219 +
 132.220 +#define send_bits(s, value, length) \
 132.221 +{ int len = length;\
 132.222 +  if (s->bi_valid > (int)Buf_size - len) {\
 132.223 +    int val = value;\
 132.224 +    s->bi_buf |= (val << s->bi_valid);\
 132.225 +    put_short(s, s->bi_buf);\
 132.226 +    s->bi_buf = (ush)val >> (Buf_size - s->bi_valid);\
 132.227 +    s->bi_valid += len - Buf_size;\
 132.228 +  } else {\
 132.229 +    s->bi_buf |= (value) << s->bi_valid;\
 132.230 +    s->bi_valid += len;\
 132.231 +  }\
 132.232 +}
 132.233 +#endif /* DEBUG */
 132.234 +
 132.235 +
 132.236 +/* the arguments must not have side effects */
 132.237 +
 132.238 +/* ===========================================================================
 132.239 + * Initialize the various 'constant' tables.
 132.240 + */
 132.241 +local void tr_static_init()
 132.242 +{
 132.243 +#if defined(GEN_TREES_H) || !defined(STDC)
 132.244 +    static int static_init_done = 0;
 132.245 +    int n;        /* iterates over tree elements */
 132.246 +    int bits;     /* bit counter */
 132.247 +    int length;   /* length value */
 132.248 +    int code;     /* code value */
 132.249 +    int dist;     /* distance index */
 132.250 +    ush bl_count[MAX_BITS+1];
 132.251 +    /* number of codes at each bit length for an optimal tree */
 132.252 +
 132.253 +    if (static_init_done) return;
 132.254 +
 132.255 +    /* For some embedded targets, global variables are not initialized: */
 132.256 +    static_l_desc.static_tree = static_ltree;
 132.257 +    static_l_desc.extra_bits = extra_lbits;
 132.258 +    static_d_desc.static_tree = static_dtree;
 132.259 +    static_d_desc.extra_bits = extra_dbits;
 132.260 +    static_bl_desc.extra_bits = extra_blbits;
 132.261 +
 132.262 +    /* Initialize the mapping length (0..255) -> length code (0..28) */
 132.263 +    length = 0;
 132.264 +    for (code = 0; code < LENGTH_CODES-1; code++) {
 132.265 +        base_length[code] = length;
 132.266 +        for (n = 0; n < (1<<extra_lbits[code]); n++) {
 132.267 +            _length_code[length++] = (uch)code;
 132.268 +        }
 132.269 +    }
 132.270 +    Assert (length == 256, "tr_static_init: length != 256");
 132.271 +    /* Note that the length 255 (match length 258) can be represented
 132.272 +     * in two different ways: code 284 + 5 bits or code 285, so we
 132.273 +     * overwrite length_code[255] to use the best encoding:
 132.274 +     */
 132.275 +    _length_code[length-1] = (uch)code;
 132.276 +
 132.277 +    /* Initialize the mapping dist (0..32K) -> dist code (0..29) */
 132.278 +    dist = 0;
 132.279 +    for (code = 0 ; code < 16; code++) {
 132.280 +        base_dist[code] = dist;
 132.281 +        for (n = 0; n < (1<<extra_dbits[code]); n++) {
 132.282 +            _dist_code[dist++] = (uch)code;
 132.283 +        }
 132.284 +    }
 132.285 +    Assert (dist == 256, "tr_static_init: dist != 256");
 132.286 +    dist >>= 7; /* from now on, all distances are divided by 128 */
 132.287 +    for ( ; code < D_CODES; code++) {
 132.288 +        base_dist[code] = dist << 7;
 132.289 +        for (n = 0; n < (1<<(extra_dbits[code]-7)); n++) {
 132.290 +            _dist_code[256 + dist++] = (uch)code;
 132.291 +        }
 132.292 +    }
 132.293 +    Assert (dist == 256, "tr_static_init: 256+dist != 512");
 132.294 +
 132.295 +    /* Construct the codes of the static literal tree */
 132.296 +    for (bits = 0; bits <= MAX_BITS; bits++) bl_count[bits] = 0;
 132.297 +    n = 0;
 132.298 +    while (n <= 143) static_ltree[n++].Len = 8, bl_count[8]++;
 132.299 +    while (n <= 255) static_ltree[n++].Len = 9, bl_count[9]++;
 132.300 +    while (n <= 279) static_ltree[n++].Len = 7, bl_count[7]++;
 132.301 +    while (n <= 287) static_ltree[n++].Len = 8, bl_count[8]++;
 132.302 +    /* Codes 286 and 287 do not exist, but we must include them in the
 132.303 +     * tree construction to get a canonical Huffman tree (longest code
 132.304 +     * all ones)
 132.305 +     */
 132.306 +    gen_codes((ct_data *)static_ltree, L_CODES+1, bl_count);
 132.307 +
 132.308 +    /* The static distance tree is trivial: */
 132.309 +    for (n = 0; n < D_CODES; n++) {
 132.310 +        static_dtree[n].Len = 5;
 132.311 +        static_dtree[n].Code = bi_reverse((unsigned)n, 5);
 132.312 +    }
 132.313 +    static_init_done = 1;
 132.314 +
 132.315 +#  ifdef GEN_TREES_H
 132.316 +    gen_trees_header();
 132.317 +#  endif
 132.318 +#endif /* defined(GEN_TREES_H) || !defined(STDC) */
 132.319 +}
 132.320 +
 132.321 +/* ===========================================================================
 132.322 + * Genererate the file trees.h describing the static trees.
 132.323 + */
 132.324 +#ifdef GEN_TREES_H
 132.325 +#  ifndef DEBUG
 132.326 +#    include <stdio.h>
 132.327 +#  endif
 132.328 +
 132.329 +#  define SEPARATOR(i, last, width) \
 132.330 +      ((i) == (last)? "\n};\n\n" :    \
 132.331 +       ((i) % (width) == (width)-1 ? ",\n" : ", "))
 132.332 +
 132.333 +void gen_trees_header()
 132.334 +{
 132.335 +    FILE *header = fopen("trees.h", "w");
 132.336 +    int i;
 132.337 +
 132.338 +    Assert (header != NULL, "Can't open trees.h");
 132.339 +    fprintf(header,
 132.340 +            "/* header created automatically with -DGEN_TREES_H */\n\n");
 132.341 +
 132.342 +    fprintf(header, "local const ct_data static_ltree[L_CODES+2] = {\n");
 132.343 +    for (i = 0; i < L_CODES+2; i++) {
 132.344 +        fprintf(header, "{{%3u},{%3u}}%s", static_ltree[i].Code,
 132.345 +                static_ltree[i].Len, SEPARATOR(i, L_CODES+1, 5));
 132.346 +    }
 132.347 +
 132.348 +    fprintf(header, "local const ct_data static_dtree[D_CODES] = {\n");
 132.349 +    for (i = 0; i < D_CODES; i++) {
 132.350 +        fprintf(header, "{{%2u},{%2u}}%s", static_dtree[i].Code,
 132.351 +                static_dtree[i].Len, SEPARATOR(i, D_CODES-1, 5));
 132.352 +    }
 132.353 +
 132.354 +    fprintf(header, "const uch _dist_code[DIST_CODE_LEN] = {\n");
 132.355 +    for (i = 0; i < DIST_CODE_LEN; i++) {
 132.356 +        fprintf(header, "%2u%s", _dist_code[i],
 132.357 +                SEPARATOR(i, DIST_CODE_LEN-1, 20));
 132.358 +    }
 132.359 +
 132.360 +    fprintf(header, "const uch _length_code[MAX_MATCH-MIN_MATCH+1]= {\n");
 132.361 +    for (i = 0; i < MAX_MATCH-MIN_MATCH+1; i++) {
 132.362 +        fprintf(header, "%2u%s", _length_code[i],
 132.363 +                SEPARATOR(i, MAX_MATCH-MIN_MATCH, 20));
 132.364 +    }
 132.365 +
 132.366 +    fprintf(header, "local const int base_length[LENGTH_CODES] = {\n");
 132.367 +    for (i = 0; i < LENGTH_CODES; i++) {
 132.368 +        fprintf(header, "%1u%s", base_length[i],
 132.369 +                SEPARATOR(i, LENGTH_CODES-1, 20));
 132.370 +    }
 132.371 +
 132.372 +    fprintf(header, "local const int base_dist[D_CODES] = {\n");
 132.373 +    for (i = 0; i < D_CODES; i++) {
 132.374 +        fprintf(header, "%5u%s", base_dist[i],
 132.375 +                SEPARATOR(i, D_CODES-1, 10));
 132.376 +    }
 132.377 +
 132.378 +    fclose(header);
 132.379 +}
 132.380 +#endif /* GEN_TREES_H */
 132.381 +
 132.382 +/* ===========================================================================
 132.383 + * Initialize the tree data structures for a new zlib stream.
 132.384 + */
 132.385 +void _tr_init(s)
 132.386 +    deflate_state *s;
 132.387 +{
 132.388 +    tr_static_init();
 132.389 +
 132.390 +    s->l_desc.dyn_tree = s->dyn_ltree;
 132.391 +    s->l_desc.stat_desc = &static_l_desc;
 132.392 +
 132.393 +    s->d_desc.dyn_tree = s->dyn_dtree;
 132.394 +    s->d_desc.stat_desc = &static_d_desc;
 132.395 +
 132.396 +    s->bl_desc.dyn_tree = s->bl_tree;
 132.397 +    s->bl_desc.stat_desc = &static_bl_desc;
 132.398 +
 132.399 +    s->bi_buf = 0;
 132.400 +    s->bi_valid = 0;
 132.401 +    s->last_eob_len = 8; /* enough lookahead for inflate */
 132.402 +#ifdef DEBUG
 132.403 +    s->compressed_len = 0L;
 132.404 +    s->bits_sent = 0L;
 132.405 +#endif
 132.406 +
 132.407 +    /* Initialize the first block of the first file: */
 132.408 +    init_block(s);
 132.409 +}
 132.410 +
 132.411 +/* ===========================================================================
 132.412 + * Initialize a new block.
 132.413 + */
 132.414 +local void init_block(s)
 132.415 +    deflate_state *s;
 132.416 +{
 132.417 +    int n; /* iterates over tree elements */
 132.418 +
 132.419 +    /* Initialize the trees. */
 132.420 +    for (n = 0; n < L_CODES;  n++) s->dyn_ltree[n].Freq = 0;
 132.421 +    for (n = 0; n < D_CODES;  n++) s->dyn_dtree[n].Freq = 0;
 132.422 +    for (n = 0; n < BL_CODES; n++) s->bl_tree[n].Freq = 0;
 132.423 +
 132.424 +    s->dyn_ltree[END_BLOCK].Freq = 1;
 132.425 +    s->opt_len = s->static_len = 0L;
 132.426 +    s->last_lit = s->matches = 0;
 132.427 +}
 132.428 +
 132.429 +#define SMALLEST 1
 132.430 +/* Index within the heap array of least frequent node in the Huffman tree */
 132.431 +
 132.432 +
 132.433 +/* ===========================================================================
 132.434 + * Remove the smallest element from the heap and recreate the heap with
 132.435 + * one less element. Updates heap and heap_len.
 132.436 + */
 132.437 +#define pqremove(s, tree, top) \
 132.438 +{\
 132.439 +    top = s->heap[SMALLEST]; \
 132.440 +    s->heap[SMALLEST] = s->heap[s->heap_len--]; \
 132.441 +    pqdownheap(s, tree, SMALLEST); \
 132.442 +}
 132.443 +
 132.444 +/* ===========================================================================
 132.445 + * Compares to subtrees, using the tree depth as tie breaker when
 132.446 + * the subtrees have equal frequency. This minimizes the worst case length.
 132.447 + */
 132.448 +#define smaller(tree, n, m, depth) \
 132.449 +   (tree[n].Freq < tree[m].Freq || \
 132.450 +   (tree[n].Freq == tree[m].Freq && depth[n] <= depth[m]))
 132.451 +
 132.452 +/* ===========================================================================
 132.453 + * Restore the heap property by moving down the tree starting at node k,
 132.454 + * exchanging a node with the smallest of its two sons if necessary, stopping
 132.455 + * when the heap property is re-established (each father smaller than its
 132.456 + * two sons).
 132.457 + */
 132.458 +local void pqdownheap(s, tree, k)
 132.459 +    deflate_state *s;
 132.460 +    ct_data *tree;  /* the tree to restore */
 132.461 +    int k;               /* node to move down */
 132.462 +{
 132.463 +    int v = s->heap[k];
 132.464 +    int j = k << 1;  /* left son of k */
 132.465 +    while (j <= s->heap_len) {
 132.466 +        /* Set j to the smallest of the two sons: */
 132.467 +        if (j < s->heap_len &&
 132.468 +            smaller(tree, s->heap[j+1], s->heap[j], s->depth)) {
 132.469 +            j++;
 132.470 +        }
 132.471 +        /* Exit if v is smaller than both sons */
 132.472 +        if (smaller(tree, v, s->heap[j], s->depth)) break;
 132.473 +
 132.474 +        /* Exchange v with the smallest son */
 132.475 +        s->heap[k] = s->heap[j];  k = j;
 132.476 +
 132.477 +        /* And continue down the tree, setting j to the left son of k */
 132.478 +        j <<= 1;
 132.479 +    }
 132.480 +    s->heap[k] = v;
 132.481 +}
 132.482 +
 132.483 +/* ===========================================================================
 132.484 + * Compute the optimal bit lengths for a tree and update the total bit length
 132.485 + * for the current block.
 132.486 + * IN assertion: the fields freq and dad are set, heap[heap_max] and
 132.487 + *    above are the tree nodes sorted by increasing frequency.
 132.488 + * OUT assertions: the field len is set to the optimal bit length, the
 132.489 + *     array bl_count contains the frequencies for each bit length.
 132.490 + *     The length opt_len is updated; static_len is also updated if stree is
 132.491 + *     not null.
 132.492 + */
 132.493 +local void gen_bitlen(s, desc)
 132.494 +    deflate_state *s;
 132.495 +    tree_desc *desc;    /* the tree descriptor */
 132.496 +{
 132.497 +    ct_data *tree        = desc->dyn_tree;
 132.498 +    int max_code         = desc->max_code;
 132.499 +    const ct_data *stree = desc->stat_desc->static_tree;
 132.500 +    const intf *extra    = desc->stat_desc->extra_bits;
 132.501 +    int base             = desc->stat_desc->extra_base;
 132.502 +    int max_length       = desc->stat_desc->max_length;
 132.503 +    int h;              /* heap index */
 132.504 +    int n, m;           /* iterate over the tree elements */
 132.505 +    int bits;           /* bit length */
 132.506 +    int xbits;          /* extra bits */
 132.507 +    ush f;              /* frequency */
 132.508 +    int overflow = 0;   /* number of elements with bit length too large */
 132.509 +
 132.510 +    for (bits = 0; bits <= MAX_BITS; bits++) s->bl_count[bits] = 0;
 132.511 +
 132.512 +    /* In a first pass, compute the optimal bit lengths (which may
 132.513 +     * overflow in the case of the bit length tree).
 132.514 +     */
 132.515 +    tree[s->heap[s->heap_max]].Len = 0; /* root of the heap */
 132.516 +
 132.517 +    for (h = s->heap_max+1; h < HEAP_SIZE; h++) {
 132.518 +        n = s->heap[h];
 132.519 +        bits = tree[tree[n].Dad].Len + 1;
 132.520 +        if (bits > max_length) bits = max_length, overflow++;
 132.521 +        tree[n].Len = (ush)bits;
 132.522 +        /* We overwrite tree[n].Dad which is no longer needed */
 132.523 +
 132.524 +        if (n > max_code) continue; /* not a leaf node */
 132.525 +
 132.526 +        s->bl_count[bits]++;
 132.527 +        xbits = 0;
 132.528 +        if (n >= base) xbits = extra[n-base];
 132.529 +        f = tree[n].Freq;
 132.530 +        s->opt_len += (ulg)f * (bits + xbits);
 132.531 +        if (stree) s->static_len += (ulg)f * (stree[n].Len + xbits);
 132.532 +    }
 132.533 +    if (overflow == 0) return;
 132.534 +
 132.535 +    Trace((stderr,"\nbit length overflow\n"));
 132.536 +    /* This happens for example on obj2 and pic of the Calgary corpus */
 132.537 +
 132.538 +    /* Find the first bit length which could increase: */
 132.539 +    do {
 132.540 +        bits = max_length-1;
 132.541 +        while (s->bl_count[bits] == 0) bits--;
 132.542 +        s->bl_count[bits]--;      /* move one leaf down the tree */
 132.543 +        s->bl_count[bits+1] += 2; /* move one overflow item as its brother */
 132.544 +        s->bl_count[max_length]--;
 132.545 +        /* The brother of the overflow item also moves one step up,
 132.546 +         * but this does not affect bl_count[max_length]
 132.547 +         */
 132.548 +        overflow -= 2;
 132.549 +    } while (overflow > 0);
 132.550 +
 132.551 +    /* Now recompute all bit lengths, scanning in increasing frequency.
 132.552 +     * h is still equal to HEAP_SIZE. (It is simpler to reconstruct all
 132.553 +     * lengths instead of fixing only the wrong ones. This idea is taken
 132.554 +     * from 'ar' written by Haruhiko Okumura.)
 132.555 +     */
 132.556 +    for (bits = max_length; bits != 0; bits--) {
 132.557 +        n = s->bl_count[bits];
 132.558 +        while (n != 0) {
 132.559 +            m = s->heap[--h];
 132.560 +            if (m > max_code) continue;
 132.561 +            if ((unsigned) tree[m].Len != (unsigned) bits) {
 132.562 +                Trace((stderr,"code %d bits %d->%d\n", m, tree[m].Len, bits));
 132.563 +                s->opt_len += ((long)bits - (long)tree[m].Len)
 132.564 +                              *(long)tree[m].Freq;
 132.565 +                tree[m].Len = (ush)bits;
 132.566 +            }
 132.567 +            n--;
 132.568 +        }
 132.569 +    }
 132.570 +}
 132.571 +
 132.572 +/* ===========================================================================
 132.573 + * Generate the codes for a given tree and bit counts (which need not be
 132.574 + * optimal).
 132.575 + * IN assertion: the array bl_count contains the bit length statistics for
 132.576 + * the given tree and the field len is set for all tree elements.
 132.577 + * OUT assertion: the field code is set for all tree elements of non
 132.578 + *     zero code length.
 132.579 + */
 132.580 +local void gen_codes (tree, max_code, bl_count)
 132.581 +    ct_data *tree;             /* the tree to decorate */
 132.582 +    int max_code;              /* largest code with non zero frequency */
 132.583 +    ushf *bl_count;            /* number of codes at each bit length */
 132.584 +{
 132.585 +    ush next_code[MAX_BITS+1]; /* next code value for each bit length */
 132.586 +    ush code = 0;              /* running code value */
 132.587 +    int bits;                  /* bit index */
 132.588 +    int n;                     /* code index */
 132.589 +
 132.590 +    /* The distribution counts are first used to generate the code values
 132.591 +     * without bit reversal.
 132.592 +     */
 132.593 +    for (bits = 1; bits <= MAX_BITS; bits++) {
 132.594 +        next_code[bits] = code = (code + bl_count[bits-1]) << 1;
 132.595 +    }
 132.596 +    /* Check that the bit counts in bl_count are consistent. The last code
 132.597 +     * must be all ones.
 132.598 +     */
 132.599 +    Assert (code + bl_count[MAX_BITS]-1 == (1<<MAX_BITS)-1,
 132.600 +            "inconsistent bit counts");
 132.601 +    Tracev((stderr,"\ngen_codes: max_code %d ", max_code));
 132.602 +
 132.603 +    for (n = 0;  n <= max_code; n++) {
 132.604 +        int len = tree[n].Len;
 132.605 +        if (len == 0) continue;
 132.606 +        /* Now reverse the bits */
 132.607 +        tree[n].Code = bi_reverse(next_code[len]++, len);
 132.608 +
 132.609 +        Tracecv(tree != static_ltree, (stderr,"\nn %3d %c l %2d c %4x (%x) ",
 132.610 +             n, (isgraph(n) ? n : ' '), len, tree[n].Code, next_code[len]-1));
 132.611 +    }
 132.612 +}
 132.613 +
 132.614 +/* ===========================================================================
 132.615 + * Construct one Huffman tree and assigns the code bit strings and lengths.
 132.616 + * Update the total bit length for the current block.
 132.617 + * IN assertion: the field freq is set for all tree elements.
 132.618 + * OUT assertions: the fields len and code are set to the optimal bit length
 132.619 + *     and corresponding code. The length opt_len is updated; static_len is
 132.620 + *     also updated if stree is not null. The field max_code is set.
 132.621 + */
 132.622 +local void build_tree(s, desc)
 132.623 +    deflate_state *s;
 132.624 +    tree_desc *desc; /* the tree descriptor */
 132.625 +{
 132.626 +    ct_data *tree         = desc->dyn_tree;
 132.627 +    const ct_data *stree  = desc->stat_desc->static_tree;
 132.628 +    int elems             = desc->stat_desc->elems;
 132.629 +    int n, m;          /* iterate over heap elements */
 132.630 +    int max_code = -1; /* largest code with non zero frequency */
 132.631 +    int node;          /* new node being created */
 132.632 +
 132.633 +    /* Construct the initial heap, with least frequent element in
 132.634 +     * heap[SMALLEST]. The sons of heap[n] are heap[2*n] and heap[2*n+1].
 132.635 +     * heap[0] is not used.
 132.636 +     */
 132.637 +    s->heap_len = 0, s->heap_max = HEAP_SIZE;
 132.638 +
 132.639 +    for (n = 0; n < elems; n++) {
 132.640 +        if (tree[n].Freq != 0) {
 132.641 +            s->heap[++(s->heap_len)] = max_code = n;
 132.642 +            s->depth[n] = 0;
 132.643 +        } else {
 132.644 +            tree[n].Len = 0;
 132.645 +        }
 132.646 +    }
 132.647 +
 132.648 +    /* The pkzip format requires that at least one distance code exists,
 132.649 +     * and that at least one bit should be sent even if there is only one
 132.650 +     * possible code. So to avoid special checks later on we force at least
 132.651 +     * two codes of non zero frequency.
 132.652 +     */
 132.653 +    while (s->heap_len < 2) {
 132.654 +        node = s->heap[++(s->heap_len)] = (max_code < 2 ? ++max_code : 0);
 132.655 +        tree[node].Freq = 1;
 132.656 +        s->depth[node] = 0;
 132.657 +        s->opt_len--; if (stree) s->static_len -= stree[node].Len;
 132.658 +        /* node is 0 or 1 so it does not have extra bits */
 132.659 +    }
 132.660 +    desc->max_code = max_code;
 132.661 +
 132.662 +    /* The elements heap[heap_len/2+1 .. heap_len] are leaves of the tree,
 132.663 +     * establish sub-heaps of increasing lengths:
 132.664 +     */
 132.665 +    for (n = s->heap_len/2; n >= 1; n--) pqdownheap(s, tree, n);
 132.666 +
 132.667 +    /* Construct the Huffman tree by repeatedly combining the least two
 132.668 +     * frequent nodes.
 132.669 +     */
 132.670 +    node = elems;              /* next internal node of the tree */
 132.671 +    do {
 132.672 +        pqremove(s, tree, n);  /* n = node of least frequency */
 132.673 +        m = s->heap[SMALLEST]; /* m = node of next least frequency */
 132.674 +
 132.675 +        s->heap[--(s->heap_max)] = n; /* keep the nodes sorted by frequency */
 132.676 +        s->heap[--(s->heap_max)] = m;
 132.677 +
 132.678 +        /* Create a new node father of n and m */
 132.679 +        tree[node].Freq = tree[n].Freq + tree[m].Freq;
 132.680 +        s->depth[node] = (uch)((s->depth[n] >= s->depth[m] ?
 132.681 +                                s->depth[n] : s->depth[m]) + 1);
 132.682 +        tree[n].Dad = tree[m].Dad = (ush)node;
 132.683 +#ifdef DUMP_BL_TREE
 132.684 +        if (tree == s->bl_tree) {
 132.685 +            fprintf(stderr,"\nnode %d(%d), sons %d(%d) %d(%d)",
 132.686 +                    node, tree[node].Freq, n, tree[n].Freq, m, tree[m].Freq);
 132.687 +        }
 132.688 +#endif
 132.689 +        /* and insert the new node in the heap */
 132.690 +        s->heap[SMALLEST] = node++;
 132.691 +        pqdownheap(s, tree, SMALLEST);
 132.692 +
 132.693 +    } while (s->heap_len >= 2);
 132.694 +
 132.695 +    s->heap[--(s->heap_max)] = s->heap[SMALLEST];
 132.696 +
 132.697 +    /* At this point, the fields freq and dad are set. We can now
 132.698 +     * generate the bit lengths.
 132.699 +     */
 132.700 +    gen_bitlen(s, (tree_desc *)desc);
 132.701 +
 132.702 +    /* The field len is now set, we can generate the bit codes */
 132.703 +    gen_codes ((ct_data *)tree, max_code, s->bl_count);
 132.704 +}
 132.705 +
 132.706 +/* ===========================================================================
 132.707 + * Scan a literal or distance tree to determine the frequencies of the codes
 132.708 + * in the bit length tree.
 132.709 + */
 132.710 +local void scan_tree (s, tree, max_code)
 132.711 +    deflate_state *s;
 132.712 +    ct_data *tree;   /* the tree to be scanned */
 132.713 +    int max_code;    /* and its largest code of non zero frequency */
 132.714 +{
 132.715 +    int n;                     /* iterates over all tree elements */
 132.716 +    int prevlen = -1;          /* last emitted length */
 132.717 +    int curlen;                /* length of current code */
 132.718 +    int nextlen = tree[0].Len; /* length of next code */
 132.719 +    int count = 0;             /* repeat count of the current code */
 132.720 +    int max_count = 7;         /* max repeat count */
 132.721 +    int min_count = 4;         /* min repeat count */
 132.722 +
 132.723 +    if (nextlen == 0) max_count = 138, min_count = 3;
 132.724 +    tree[max_code+1].Len = (ush)0xffff; /* guard */
 132.725 +
 132.726 +    for (n = 0; n <= max_code; n++) {
 132.727 +        curlen = nextlen; nextlen = tree[n+1].Len;
 132.728 +        if (++count < max_count && curlen == nextlen) {
 132.729 +            continue;
 132.730 +        } else if (count < min_count) {
 132.731 +            s->bl_tree[curlen].Freq += count;
 132.732 +        } else if (curlen != 0) {
 132.733 +            if (curlen != prevlen) s->bl_tree[curlen].Freq++;
 132.734 +            s->bl_tree[REP_3_6].Freq++;
 132.735 +        } else if (count <= 10) {
 132.736 +            s->bl_tree[REPZ_3_10].Freq++;
 132.737 +        } else {
 132.738 +            s->bl_tree[REPZ_11_138].Freq++;
 132.739 +        }
 132.740 +        count = 0; prevlen = curlen;
 132.741 +        if (nextlen == 0) {
 132.742 +            max_count = 138, min_count = 3;
 132.743 +        } else if (curlen == nextlen) {
 132.744 +            max_count = 6, min_count = 3;
 132.745 +        } else {
 132.746 +            max_count = 7, min_count = 4;
 132.747 +        }
 132.748 +    }
 132.749 +}
 132.750 +
 132.751 +/* ===========================================================================
 132.752 + * Send a literal or distance tree in compressed form, using the codes in
 132.753 + * bl_tree.
 132.754 + */
 132.755 +local void send_tree (s, tree, max_code)
 132.756 +    deflate_state *s;
 132.757 +    ct_data *tree; /* the tree to be scanned */
 132.758 +    int max_code;       /* and its largest code of non zero frequency */
 132.759 +{
 132.760 +    int n;                     /* iterates over all tree elements */
 132.761 +    int prevlen = -1;          /* last emitted length */
 132.762 +    int curlen;                /* length of current code */
 132.763 +    int nextlen = tree[0].Len; /* length of next code */
 132.764 +    int count = 0;             /* repeat count of the current code */
 132.765 +    int max_count = 7;         /* max repeat count */
 132.766 +    int min_count = 4;         /* min repeat count */
 132.767 +
 132.768 +    /* tree[max_code+1].Len = -1; */  /* guard already set */
 132.769 +    if (nextlen == 0) max_count = 138, min_count = 3;
 132.770 +
 132.771 +    for (n = 0; n <= max_code; n++) {
 132.772 +        curlen = nextlen; nextlen = tree[n+1].Len;
 132.773 +        if (++count < max_count && curlen == nextlen) {
 132.774 +            continue;
 132.775 +        } else if (count < min_count) {
 132.776 +            do { send_code(s, curlen, s->bl_tree); } while (--count != 0);
 132.777 +
 132.778 +        } else if (curlen != 0) {
 132.779 +            if (curlen != prevlen) {
 132.780 +                send_code(s, curlen, s->bl_tree); count--;
 132.781 +            }
 132.782 +            Assert(count >= 3 && count <= 6, " 3_6?");
 132.783 +            send_code(s, REP_3_6, s->bl_tree); send_bits(s, count-3, 2);
 132.784 +
 132.785 +        } else if (count <= 10) {
 132.786 +            send_code(s, REPZ_3_10, s->bl_tree); send_bits(s, count-3, 3);
 132.787 +
 132.788 +        } else {
 132.789 +            send_code(s, REPZ_11_138, s->bl_tree); send_bits(s, count-11, 7);
 132.790 +        }
 132.791 +        count = 0; prevlen = curlen;
 132.792 +        if (nextlen == 0) {
 132.793 +            max_count = 138, min_count = 3;
 132.794 +        } else if (curlen == nextlen) {
 132.795 +            max_count = 6, min_count = 3;
 132.796 +        } else {
 132.797 +            max_count = 7, min_count = 4;
 132.798 +        }
 132.799 +    }
 132.800 +}
 132.801 +
 132.802 +/* ===========================================================================
 132.803 + * Construct the Huffman tree for the bit lengths and return the index in
 132.804 + * bl_order of the last bit length code to send.
 132.805 + */
 132.806 +local int build_bl_tree(s)
 132.807 +    deflate_state *s;
 132.808 +{
 132.809 +    int max_blindex;  /* index of last bit length code of non zero freq */
 132.810 +
 132.811 +    /* Determine the bit length frequencies for literal and distance trees */
 132.812 +    scan_tree(s, (ct_data *)s->dyn_ltree, s->l_desc.max_code);
 132.813 +    scan_tree(s, (ct_data *)s->dyn_dtree, s->d_desc.max_code);
 132.814 +
 132.815 +    /* Build the bit length tree: */
 132.816 +    build_tree(s, (tree_desc *)(&(s->bl_desc)));
 132.817 +    /* opt_len now includes the length of the tree representations, except
 132.818 +     * the lengths of the bit lengths codes and the 5+5+4 bits for the counts.
 132.819 +     */
 132.820 +
 132.821 +    /* Determine the number of bit length codes to send. The pkzip format
 132.822 +     * requires that at least 4 bit length codes be sent. (appnote.txt says
 132.823 +     * 3 but the actual value used is 4.)
 132.824 +     */
 132.825 +    for (max_blindex = BL_CODES-1; max_blindex >= 3; max_blindex--) {
 132.826 +        if (s->bl_tree[bl_order[max_blindex]].Len != 0) break;
 132.827 +    }
 132.828 +    /* Update opt_len to include the bit length tree and counts */
 132.829 +    s->opt_len += 3*(max_blindex+1) + 5+5+4;
 132.830 +    Tracev((stderr, "\ndyn trees: dyn %ld, stat %ld",
 132.831 +            s->opt_len, s->static_len));
 132.832 +
 132.833 +    return max_blindex;
 132.834 +}
 132.835 +
 132.836 +/* ===========================================================================
 132.837 + * Send the header for a block using dynamic Huffman trees: the counts, the
 132.838 + * lengths of the bit length codes, the literal tree and the distance tree.
 132.839 + * IN assertion: lcodes >= 257, dcodes >= 1, blcodes >= 4.
 132.840 + */
 132.841 +local void send_all_trees(s, lcodes, dcodes, blcodes)
 132.842 +    deflate_state *s;
 132.843 +    int lcodes, dcodes, blcodes; /* number of codes for each tree */
 132.844 +{
 132.845 +    int rank;                    /* index in bl_order */
 132.846 +
 132.847 +    Assert (lcodes >= 257 && dcodes >= 1 && blcodes >= 4, "not enough codes");
 132.848 +    Assert (lcodes <= L_CODES && dcodes <= D_CODES && blcodes <= BL_CODES,
 132.849 +            "too many codes");
 132.850 +    Tracev((stderr, "\nbl counts: "));
 132.851 +    send_bits(s, lcodes-257, 5); /* not +255 as stated in appnote.txt */
 132.852 +    send_bits(s, dcodes-1,   5);
 132.853 +    send_bits(s, blcodes-4,  4); /* not -3 as stated in appnote.txt */
 132.854 +    for (rank = 0; rank < blcodes; rank++) {
 132.855 +        Tracev((stderr, "\nbl code %2d ", bl_order[rank]));
 132.856 +        send_bits(s, s->bl_tree[bl_order[rank]].Len, 3);
 132.857 +    }
 132.858 +    Tracev((stderr, "\nbl tree: sent %ld", s->bits_sent));
 132.859 +
 132.860 +    send_tree(s, (ct_data *)s->dyn_ltree, lcodes-1); /* literal tree */
 132.861 +    Tracev((stderr, "\nlit tree: sent %ld", s->bits_sent));
 132.862 +
 132.863 +    send_tree(s, (ct_data *)s->dyn_dtree, dcodes-1); /* distance tree */
 132.864 +    Tracev((stderr, "\ndist tree: sent %ld", s->bits_sent));
 132.865 +}
 132.866 +
 132.867 +/* ===========================================================================
 132.868 + * Send a stored block
 132.869 + */
 132.870 +void _tr_stored_block(s, buf, stored_len, eof)
 132.871 +    deflate_state *s;
 132.872 +    charf *buf;       /* input block */
 132.873 +    ulg stored_len;   /* length of input block */
 132.874 +    int eof;          /* true if this is the last block for a file */
 132.875 +{
 132.876 +    send_bits(s, (STORED_BLOCK<<1)+eof, 3);  /* send block type */
 132.877 +#ifdef DEBUG
 132.878 +    s->compressed_len = (s->compressed_len + 3 + 7) & (ulg)~7L;
 132.879 +    s->compressed_len += (stored_len + 4) << 3;
 132.880 +#endif
 132.881 +    copy_block(s, buf, (unsigned)stored_len, 1); /* with header */
 132.882 +}
 132.883 +
 132.884 +/* ===========================================================================
 132.885 + * Send one empty static block to give enough lookahead for inflate.
 132.886 + * This takes 10 bits, of which 7 may remain in the bit buffer.
 132.887 + * The current inflate code requires 9 bits of lookahead. If the
 132.888 + * last two codes for the previous block (real code plus EOB) were coded
 132.889 + * on 5 bits or less, inflate may have only 5+3 bits of lookahead to decode
 132.890 + * the last real code. In this case we send two empty static blocks instead
 132.891 + * of one. (There are no problems if the previous block is stored or fixed.)
 132.892 + * To simplify the code, we assume the worst case of last real code encoded
 132.893 + * on one bit only.
 132.894 + */
 132.895 +void _tr_align(s)
 132.896 +    deflate_state *s;
 132.897 +{
 132.898 +    send_bits(s, STATIC_TREES<<1, 3);
 132.899 +    send_code(s, END_BLOCK, static_ltree);
 132.900 +#ifdef DEBUG
 132.901 +    s->compressed_len += 10L; /* 3 for block type, 7 for EOB */
 132.902 +#endif
 132.903 +    bi_flush(s);
 132.904 +    /* Of the 10 bits for the empty block, we have already sent
 132.905 +     * (10 - bi_valid) bits. The lookahead for the last real code (before
 132.906 +     * the EOB of the previous block) was thus at least one plus the length
 132.907 +     * of the EOB plus what we have just sent of the empty static block.
 132.908 +     */
 132.909 +    if (1 + s->last_eob_len + 10 - s->bi_valid < 9) {
 132.910 +        send_bits(s, STATIC_TREES<<1, 3);
 132.911 +        send_code(s, END_BLOCK, static_ltree);
 132.912 +#ifdef DEBUG
 132.913 +        s->compressed_len += 10L;
 132.914 +#endif
 132.915 +        bi_flush(s);
 132.916 +    }
 132.917 +    s->last_eob_len = 7;
 132.918 +}
 132.919 +
 132.920 +/* ===========================================================================
 132.921 + * Determine the best encoding for the current block: dynamic trees, static
 132.922 + * trees or store, and output the encoded block to the zip file.
 132.923 + */
 132.924 +void _tr_flush_block(s, buf, stored_len, eof)
 132.925 +    deflate_state *s;
 132.926 +    charf *buf;       /* input block, or NULL if too old */
 132.927 +    ulg stored_len;   /* length of input block */
 132.928 +    int eof;          /* true if this is the last block for a file */
 132.929 +{
 132.930 +    ulg opt_lenb, static_lenb; /* opt_len and static_len in bytes */
 132.931 +    int max_blindex = 0;  /* index of last bit length code of non zero freq */
 132.932 +
 132.933 +    /* Build the Huffman trees unless a stored block is forced */
 132.934 +    if (s->level > 0) {
 132.935 +
 132.936 +        /* Check if the file is binary or text */
 132.937 +        if (stored_len > 0 && s->strm->data_type == Z_UNKNOWN)
 132.938 +            set_data_type(s);
 132.939 +
 132.940 +        /* Construct the literal and distance trees */
 132.941 +        build_tree(s, (tree_desc *)(&(s->l_desc)));
 132.942 +        Tracev((stderr, "\nlit data: dyn %ld, stat %ld", s->opt_len,
 132.943 +                s->static_len));
 132.944 +
 132.945 +        build_tree(s, (tree_desc *)(&(s->d_desc)));
 132.946 +        Tracev((stderr, "\ndist data: dyn %ld, stat %ld", s->opt_len,
 132.947 +                s->static_len));
 132.948 +        /* At this point, opt_len and static_len are the total bit lengths of
 132.949 +         * the compressed block data, excluding the tree representations.
 132.950 +         */
 132.951 +
 132.952 +        /* Build the bit length tree for the above two trees, and get the index
 132.953 +         * in bl_order of the last bit length code to send.
 132.954 +         */
 132.955 +        max_blindex = build_bl_tree(s);
 132.956 +
 132.957 +        /* Determine the best encoding. Compute the block lengths in bytes. */
 132.958 +        opt_lenb = (s->opt_len+3+7)>>3;
 132.959 +        static_lenb = (s->static_len+3+7)>>3;
 132.960 +
 132.961 +        Tracev((stderr, "\nopt %lu(%lu) stat %lu(%lu) stored %lu lit %u ",
 132.962 +                opt_lenb, s->opt_len, static_lenb, s->static_len, stored_len,
 132.963 +                s->last_lit));
 132.964 +
 132.965 +        if (static_lenb <= opt_lenb) opt_lenb = static_lenb;
 132.966 +
 132.967 +    } else {
 132.968 +        Assert(buf != (char*)0, "lost buf");
 132.969 +        opt_lenb = static_lenb = stored_len + 5; /* force a stored block */
 132.970 +    }
 132.971 +
 132.972 +#ifdef FORCE_STORED
 132.973 +    if (buf != (char*)0) { /* force stored block */
 132.974 +#else
 132.975 +    if (stored_len+4 <= opt_lenb && buf != (char*)0) {
 132.976 +                       /* 4: two words for the lengths */
 132.977 +#endif
 132.978 +        /* The test buf != NULL is only necessary if LIT_BUFSIZE > WSIZE.
 132.979 +         * Otherwise we can't have processed more than WSIZE input bytes since
 132.980 +         * the last block flush, because compression would have been
 132.981 +         * successful. If LIT_BUFSIZE <= WSIZE, it is never too late to
 132.982 +         * transform a block into a stored block.
 132.983 +         */
 132.984 +        _tr_stored_block(s, buf, stored_len, eof);
 132.985 +
 132.986 +#ifdef FORCE_STATIC
 132.987 +    } else if (static_lenb >= 0) { /* force static trees */
 132.988 +#else
 132.989 +    } else if (s->strategy == Z_FIXED || static_lenb == opt_lenb) {
 132.990 +#endif
 132.991 +        send_bits(s, (STATIC_TREES<<1)+eof, 3);
 132.992 +        compress_block(s, (ct_data *)static_ltree, (ct_data *)static_dtree);
 132.993 +#ifdef DEBUG
 132.994 +        s->compressed_len += 3 + s->static_len;
 132.995 +#endif
 132.996 +    } else {
 132.997 +        send_bits(s, (DYN_TREES<<1)+eof, 3);
 132.998 +        send_all_trees(s, s->l_desc.max_code+1, s->d_desc.max_code+1,
 132.999 +                       max_blindex+1);
132.1000 +        compress_block(s, (ct_data *)s->dyn_ltree, (ct_data *)s->dyn_dtree);
132.1001 +#ifdef DEBUG
132.1002 +        s->compressed_len += 3 + s->opt_len;
132.1003 +#endif
132.1004 +    }
132.1005 +    Assert (s->compressed_len == s->bits_sent, "bad compressed size");
132.1006 +    /* The above check is made mod 2^32, for files larger than 512 MB
132.1007 +     * and uLong implemented on 32 bits.
132.1008 +     */
132.1009 +    init_block(s);
132.1010 +
132.1011 +    if (eof) {
132.1012 +        bi_windup(s);
132.1013 +#ifdef DEBUG
132.1014 +        s->compressed_len += 7;  /* align on byte boundary */
132.1015 +#endif
132.1016 +    }
132.1017 +    Tracev((stderr,"\ncomprlen %lu(%lu) ", s->compressed_len>>3,
132.1018 +           s->compressed_len-7*eof));
132.1019 +}
132.1020 +
132.1021 +/* ===========================================================================
132.1022 + * Save the match info and tally the frequency counts. Return true if
132.1023 + * the current block must be flushed.
132.1024 + */
132.1025 +int _tr_tally (s, dist, lc)
132.1026 +    deflate_state *s;
132.1027 +    unsigned dist;  /* distance of matched string */
132.1028 +    unsigned lc;    /* match length-MIN_MATCH or unmatched char (if dist==0) */
132.1029 +{
132.1030 +    s->d_buf[s->last_lit] = (ush)dist;
132.1031 +    s->l_buf[s->last_lit++] = (uch)lc;
132.1032 +    if (dist == 0) {
132.1033 +        /* lc is the unmatched char */
132.1034 +        s->dyn_ltree[lc].Freq++;
132.1035 +    } else {
132.1036 +        s->matches++;
132.1037 +        /* Here, lc is the match length - MIN_MATCH */
132.1038 +        dist--;             /* dist = match distance - 1 */
132.1039 +        Assert((ush)dist < (ush)MAX_DIST(s) &&
132.1040 +               (ush)lc <= (ush)(MAX_MATCH-MIN_MATCH) &&
132.1041 +               (ush)d_code(dist) < (ush)D_CODES,  "_tr_tally: bad match");
132.1042 +
132.1043 +        s->dyn_ltree[_length_code[lc]+LITERALS+1].Freq++;
132.1044 +        s->dyn_dtree[d_code(dist)].Freq++;
132.1045 +    }
132.1046 +
132.1047 +#ifdef TRUNCATE_BLOCK
132.1048 +    /* Try to guess if it is profitable to stop the current block here */
132.1049 +    if ((s->last_lit & 0x1fff) == 0 && s->level > 2) {
132.1050 +        /* Compute an upper bound for the compressed length */
132.1051 +        ulg out_length = (ulg)s->last_lit*8L;
132.1052 +        ulg in_length = (ulg)((long)s->strstart - s->block_start);
132.1053 +        int dcode;
132.1054 +        for (dcode = 0; dcode < D_CODES; dcode++) {
132.1055 +            out_length += (ulg)s->dyn_dtree[dcode].Freq *
132.1056 +                (5L+extra_dbits[dcode]);
132.1057 +        }
132.1058 +        out_length >>= 3;
132.1059 +        Tracev((stderr,"\nlast_lit %u, in %ld, out ~%ld(%ld%%) ",
132.1060 +               s->last_lit, in_length, out_length,
132.1061 +               100L - out_length*100L/in_length));
132.1062 +        if (s->matches < s->last_lit/2 && out_length < in_length/2) return 1;
132.1063 +    }
132.1064 +#endif
132.1065 +    return (s->last_lit == s->lit_bufsize-1);
132.1066 +    /* We avoid equality with lit_bufsize because of wraparound at 64K
132.1067 +     * on 16 bit machines and because stored blocks are restricted to
132.1068 +     * 64K-1 bytes.
132.1069 +     */
132.1070 +}
132.1071 +
132.1072 +/* ===========================================================================
132.1073 + * Send the block data compressed using the given Huffman trees
132.1074 + */
132.1075 +local void compress_block(s, ltree, dtree)
132.1076 +    deflate_state *s;
132.1077 +    ct_data *ltree; /* literal tree */
132.1078 +    ct_data *dtree; /* distance tree */
132.1079 +{
132.1080 +    unsigned dist;      /* distance of matched string */
132.1081 +    int lc;             /* match length or unmatched char (if dist == 0) */
132.1082 +    unsigned lx = 0;    /* running index in l_buf */
132.1083 +    unsigned code;      /* the code to send */
132.1084 +    int extra;          /* number of extra bits to send */
132.1085 +
132.1086 +    if (s->last_lit != 0) do {
132.1087 +        dist = s->d_buf[lx];
132.1088 +        lc = s->l_buf[lx++];
132.1089 +        if (dist == 0) {
132.1090 +            send_code(s, lc, ltree); /* send a literal byte */
132.1091 +            Tracecv(isgraph(lc), (stderr," '%c' ", lc));
132.1092 +        } else {
132.1093 +            /* Here, lc is the match length - MIN_MATCH */
132.1094 +            code = _length_code[lc];
132.1095 +            send_code(s, code+LITERALS+1, ltree); /* send the length code */
132.1096 +            extra = extra_lbits[code];
132.1097 +            if (extra != 0) {
132.1098 +                lc -= base_length[code];
132.1099 +                send_bits(s, lc, extra);       /* send the extra length bits */
132.1100 +            }
132.1101 +            dist--; /* dist is now the match distance - 1 */
132.1102 +            code = d_code(dist);
132.1103 +            Assert (code < D_CODES, "bad d_code");
132.1104 +
132.1105 +            send_code(s, code, dtree);       /* send the distance code */
132.1106 +            extra = extra_dbits[code];
132.1107 +            if (extra != 0) {
132.1108 +                dist -= base_dist[code];
132.1109 +                send_bits(s, dist, extra);   /* send the extra distance bits */
132.1110 +            }
132.1111 +        } /* literal or match pair ? */
132.1112 +
132.1113 +        /* Check that the overlay between pending_buf and d_buf+l_buf is ok: */
132.1114 +        Assert((uInt)(s->pending) < s->lit_bufsize + 2*lx,
132.1115 +               "pendingBuf overflow");
132.1116 +
132.1117 +    } while (lx < s->last_lit);
132.1118 +
132.1119 +    send_code(s, END_BLOCK, ltree);
132.1120 +    s->last_eob_len = ltree[END_BLOCK].Len;
132.1121 +}
132.1122 +
132.1123 +/* ===========================================================================
132.1124 + * Set the data type to BINARY or TEXT, using a crude approximation:
132.1125 + * set it to Z_TEXT if all symbols are either printable characters (33 to 255)
132.1126 + * or white spaces (9 to 13, or 32); or set it to Z_BINARY otherwise.
132.1127 + * IN assertion: the fields Freq of dyn_ltree are set.
132.1128 + */
132.1129 +local void set_data_type(s)
132.1130 +    deflate_state *s;
132.1131 +{
132.1132 +    int n;
132.1133 +
132.1134 +    for (n = 0; n < 9; n++)
132.1135 +        if (s->dyn_ltree[n].Freq != 0)
132.1136 +            break;
132.1137 +    if (n == 9)
132.1138 +        for (n = 14; n < 32; n++)
132.1139 +            if (s->dyn_ltree[n].Freq != 0)
132.1140 +                break;
132.1141 +    s->strm->data_type = (n == 32) ? Z_TEXT : Z_BINARY;
132.1142 +}
132.1143 +
132.1144 +/* ===========================================================================
132.1145 + * Reverse the first len bits of a code, using straightforward code (a faster
132.1146 + * method would use a table)
132.1147 + * IN assertion: 1 <= len <= 15
132.1148 + */
132.1149 +local unsigned bi_reverse(code, len)
132.1150 +    unsigned code; /* the value to invert */
132.1151 +    int len;       /* its bit length */
132.1152 +{
132.1153 +    register unsigned res = 0;
132.1154 +    do {
132.1155 +        res |= code & 1;
132.1156 +        code >>= 1, res <<= 1;
132.1157 +    } while (--len > 0);
132.1158 +    return res >> 1;
132.1159 +}
132.1160 +
132.1161 +/* ===========================================================================
132.1162 + * Flush the bit buffer, keeping at most 7 bits in it.
132.1163 + */
132.1164 +local void bi_flush(s)
132.1165 +    deflate_state *s;
132.1166 +{
132.1167 +    if (s->bi_valid == 16) {
132.1168 +        put_short(s, s->bi_buf);
132.1169 +        s->bi_buf = 0;
132.1170 +        s->bi_valid = 0;
132.1171 +    } else if (s->bi_valid >= 8) {
132.1172 +        put_byte(s, (Byte)s->bi_buf);
132.1173 +        s->bi_buf >>= 8;
132.1174 +        s->bi_valid -= 8;
132.1175 +    }
132.1176 +}
132.1177 +
132.1178 +/* ===========================================================================
132.1179 + * Flush the bit buffer and align the output on a byte boundary
132.1180 + */
132.1181 +local void bi_windup(s)
132.1182 +    deflate_state *s;
132.1183 +{
132.1184 +    if (s->bi_valid > 8) {
132.1185 +        put_short(s, s->bi_buf);
132.1186 +    } else if (s->bi_valid > 0) {
132.1187 +        put_byte(s, (Byte)s->bi_buf);
132.1188 +    }
132.1189 +    s->bi_buf = 0;
132.1190 +    s->bi_valid = 0;
132.1191 +#ifdef DEBUG
132.1192 +    s->bits_sent = (s->bits_sent+7) & ~7;
132.1193 +#endif
132.1194 +}
132.1195 +
132.1196 +/* ===========================================================================
132.1197 + * Copy a stored block, storing first the length and its
132.1198 + * one's complement if requested.
132.1199 + */
132.1200 +local void copy_block(s, buf, len, header)
132.1201 +    deflate_state *s;
132.1202 +    charf    *buf;    /* the input data */
132.1203 +    unsigned len;     /* its length */
132.1204 +    int      header;  /* true if block header must be written */
132.1205 +{
132.1206 +    bi_windup(s);        /* align on byte boundary */
132.1207 +    s->last_eob_len = 8; /* enough lookahead for inflate */
132.1208 +
132.1209 +    if (header) {
132.1210 +        put_short(s, (ush)len);
132.1211 +        put_short(s, (ush)~len);
132.1212 +#ifdef DEBUG
132.1213 +        s->bits_sent += 2*16;
132.1214 +#endif
132.1215 +    }
132.1216 +#ifdef DEBUG
132.1217 +    s->bits_sent += (ulg)len<<3;
132.1218 +#endif
132.1219 +    while (len--) {
132.1220 +        put_byte(s, *buf++);
132.1221 +    }
132.1222 +}
   133.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   133.2 +++ b/libs/zlib/trees.h	Sat Sep 19 05:51:51 2015 +0300
   133.3 @@ -0,0 +1,128 @@
   133.4 +/* header created automatically with -DGEN_TREES_H */
   133.5 +
   133.6 +local const ct_data static_ltree[L_CODES+2] = {
   133.7 +{{ 12},{  8}}, {{140},{  8}}, {{ 76},{  8}}, {{204},{  8}}, {{ 44},{  8}},
   133.8 +{{172},{  8}}, {{108},{  8}}, {{236},{  8}}, {{ 28},{  8}}, {{156},{  8}},
   133.9 +{{ 92},{  8}}, {{220},{  8}}, {{ 60},{  8}}, {{188},{  8}}, {{124},{  8}},
  133.10 +{{252},{  8}}, {{  2},{  8}}, {{130},{  8}}, {{ 66},{  8}}, {{194},{  8}},
  133.11 +{{ 34},{  8}}, {{162},{  8}}, {{ 98},{  8}}, {{226},{  8}}, {{ 18},{  8}},
  133.12 +{{146},{  8}}, {{ 82},{  8}}, {{210},{  8}}, {{ 50},{  8}}, {{178},{  8}},
  133.13 +{{114},{  8}}, {{242},{  8}}, {{ 10},{  8}}, {{138},{  8}}, {{ 74},{  8}},
  133.14 +{{202},{  8}}, {{ 42},{  8}}, {{170},{  8}}, {{106},{  8}}, {{234},{  8}},
  133.15 +{{ 26},{  8}}, {{154},{  8}}, {{ 90},{  8}}, {{218},{  8}}, {{ 58},{  8}},
  133.16 +{{186},{  8}}, {{122},{  8}}, {{250},{  8}}, {{  6},{  8}}, {{134},{  8}},
  133.17 +{{ 70},{  8}}, {{198},{  8}}, {{ 38},{  8}}, {{166},{  8}}, {{102},{  8}},
  133.18 +{{230},{  8}}, {{ 22},{  8}}, {{150},{  8}}, {{ 86},{  8}}, {{214},{  8}},
  133.19 +{{ 54},{  8}}, {{182},{  8}}, {{118},{  8}}, {{246},{  8}}, {{ 14},{  8}},
  133.20 +{{142},{  8}}, {{ 78},{  8}}, {{206},{  8}}, {{ 46},{  8}}, {{174},{  8}},
  133.21 +{{110},{  8}}, {{238},{  8}}, {{ 30},{  8}}, {{158},{  8}}, {{ 94},{  8}},
  133.22 +{{222},{  8}}, {{ 62},{  8}}, {{190},{  8}}, {{126},{  8}}, {{254},{  8}},
  133.23 +{{  1},{  8}}, {{129},{  8}}, {{ 65},{  8}}, {{193},{  8}}, {{ 33},{  8}},
  133.24 +{{161},{  8}}, {{ 97},{  8}}, {{225},{  8}}, {{ 17},{  8}}, {{145},{  8}},
  133.25 +{{ 81},{  8}}, {{209},{  8}}, {{ 49},{  8}}, {{177},{  8}}, {{113},{  8}},
  133.26 +{{241},{  8}}, {{  9},{  8}}, {{137},{  8}}, {{ 73},{  8}}, {{201},{  8}},
  133.27 +{{ 41},{  8}}, {{169},{  8}}, {{105},{  8}}, {{233},{  8}}, {{ 25},{  8}},
  133.28 +{{153},{  8}}, {{ 89},{  8}}, {{217},{  8}}, {{ 57},{  8}}, {{185},{  8}},
  133.29 +{{121},{  8}}, {{249},{  8}}, {{  5},{  8}}, {{133},{  8}}, {{ 69},{  8}},
  133.30 +{{197},{  8}}, {{ 37},{  8}}, {{165},{  8}}, {{101},{  8}}, {{229},{  8}},
  133.31 +{{ 21},{  8}}, {{149},{  8}}, {{ 85},{  8}}, {{213},{  8}}, {{ 53},{  8}},
  133.32 +{{181},{  8}}, {{117},{  8}}, {{245},{  8}}, {{ 13},{  8}}, {{141},{  8}},
  133.33 +{{ 77},{  8}}, {{205},{  8}}, {{ 45},{  8}}, {{173},{  8}}, {{109},{  8}},
  133.34 +{{237},{  8}}, {{ 29},{  8}}, {{157},{  8}}, {{ 93},{  8}}, {{221},{  8}},
  133.35 +{{ 61},{  8}}, {{189},{  8}}, {{125},{  8}}, {{253},{  8}}, {{ 19},{  9}},
  133.36 +{{275},{  9}}, {{147},{  9}}, {{403},{  9}}, {{ 83},{  9}}, {{339},{  9}},
  133.37 +{{211},{  9}}, {{467},{  9}}, {{ 51},{  9}}, {{307},{  9}}, {{179},{  9}},
  133.38 +{{435},{  9}}, {{115},{  9}}, {{371},{  9}}, {{243},{  9}}, {{499},{  9}},
  133.39 +{{ 11},{  9}}, {{267},{  9}}, {{139},{  9}}, {{395},{  9}}, {{ 75},{  9}},
  133.40 +{{331},{  9}}, {{203},{  9}}, {{459},{  9}}, {{ 43},{  9}}, {{299},{  9}},
  133.41 +{{171},{  9}}, {{427},{  9}}, {{107},{  9}}, {{363},{  9}}, {{235},{  9}},
  133.42 +{{491},{  9}}, {{ 27},{  9}}, {{283},{  9}}, {{155},{  9}}, {{411},{  9}},
  133.43 +{{ 91},{  9}}, {{347},{  9}}, {{219},{  9}}, {{475},{  9}}, {{ 59},{  9}},
  133.44 +{{315},{  9}}, {{187},{  9}}, {{443},{  9}}, {{123},{  9}}, {{379},{  9}},
  133.45 +{{251},{  9}}, {{507},{  9}}, {{  7},{  9}}, {{263},{  9}}, {{135},{  9}},
  133.46 +{{391},{  9}}, {{ 71},{  9}}, {{327},{  9}}, {{199},{  9}}, {{455},{  9}},
  133.47 +{{ 39},{  9}}, {{295},{  9}}, {{167},{  9}}, {{423},{  9}}, {{103},{  9}},
  133.48 +{{359},{  9}}, {{231},{  9}}, {{487},{  9}}, {{ 23},{  9}}, {{279},{  9}},
  133.49 +{{151},{  9}}, {{407},{  9}}, {{ 87},{  9}}, {{343},{  9}}, {{215},{  9}},
  133.50 +{{471},{  9}}, {{ 55},{  9}}, {{311},{  9}}, {{183},{  9}}, {{439},{  9}},
  133.51 +{{119},{  9}}, {{375},{  9}}, {{247},{  9}}, {{503},{  9}}, {{ 15},{  9}},
  133.52 +{{271},{  9}}, {{143},{  9}}, {{399},{  9}}, {{ 79},{  9}}, {{335},{  9}},
  133.53 +{{207},{  9}}, {{463},{  9}}, {{ 47},{  9}}, {{303},{  9}}, {{175},{  9}},
  133.54 +{{431},{  9}}, {{111},{  9}}, {{367},{  9}}, {{239},{  9}}, {{495},{  9}},
  133.55 +{{ 31},{  9}}, {{287},{  9}}, {{159},{  9}}, {{415},{  9}}, {{ 95},{  9}},
  133.56 +{{351},{  9}}, {{223},{  9}}, {{479},{  9}}, {{ 63},{  9}}, {{319},{  9}},
  133.57 +{{191},{  9}}, {{447},{  9}}, {{127},{  9}}, {{383},{  9}}, {{255},{  9}},
  133.58 +{{511},{  9}}, {{  0},{  7}}, {{ 64},{  7}}, {{ 32},{  7}}, {{ 96},{  7}},
  133.59 +{{ 16},{  7}}, {{ 80},{  7}}, {{ 48},{  7}}, {{112},{  7}}, {{  8},{  7}},
  133.60 +{{ 72},{  7}}, {{ 40},{  7}}, {{104},{  7}}, {{ 24},{  7}}, {{ 88},{  7}},
  133.61 +{{ 56},{  7}}, {{120},{  7}}, {{  4},{  7}}, {{ 68},{  7}}, {{ 36},{  7}},
  133.62 +{{100},{  7}}, {{ 20},{  7}}, {{ 84},{  7}}, {{ 52},{  7}}, {{116},{  7}},
  133.63 +{{  3},{  8}}, {{131},{  8}}, {{ 67},{  8}}, {{195},{  8}}, {{ 35},{  8}},
  133.64 +{{163},{  8}}, {{ 99},{  8}}, {{227},{  8}}
  133.65 +};
  133.66 +
  133.67 +local const ct_data static_dtree[D_CODES] = {
  133.68 +{{ 0},{ 5}}, {{16},{ 5}}, {{ 8},{ 5}}, {{24},{ 5}}, {{ 4},{ 5}},
  133.69 +{{20},{ 5}}, {{12},{ 5}}, {{28},{ 5}}, {{ 2},{ 5}}, {{18},{ 5}},
  133.70 +{{10},{ 5}}, {{26},{ 5}}, {{ 6},{ 5}}, {{22},{ 5}}, {{14},{ 5}},
  133.71 +{{30},{ 5}}, {{ 1},{ 5}}, {{17},{ 5}}, {{ 9},{ 5}}, {{25},{ 5}},
  133.72 +{{ 5},{ 5}}, {{21},{ 5}}, {{13},{ 5}}, {{29},{ 5}}, {{ 3},{ 5}},
  133.73 +{{19},{ 5}}, {{11},{ 5}}, {{27},{ 5}}, {{ 7},{ 5}}, {{23},{ 5}}
  133.74 +};
  133.75 +
  133.76 +const uch _dist_code[DIST_CODE_LEN] = {
  133.77 + 0,  1,  2,  3,  4,  4,  5,  5,  6,  6,  6,  6,  7,  7,  7,  7,  8,  8,  8,  8,
  133.78 + 8,  8,  8,  8,  9,  9,  9,  9,  9,  9,  9,  9, 10, 10, 10, 10, 10, 10, 10, 10,
  133.79 +10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
  133.80 +11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
  133.81 +12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13,
  133.82 +13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,
  133.83 +13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
  133.84 +14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
  133.85 +14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
  133.86 +14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 15,
  133.87 +15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
  133.88 +15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
  133.89 +15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,  0,  0, 16, 17,
  133.90 +18, 18, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 22, 22, 22, 22, 22, 22, 22, 22,
  133.91 +23, 23, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
  133.92 +24, 24, 24, 24, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
  133.93 +26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
  133.94 +26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27,
  133.95 +27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
  133.96 +27, 27, 27, 27, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
  133.97 +28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
  133.98 +28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
  133.99 +28, 28, 28, 28, 28, 28, 28, 28, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
 133.100 +29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
 133.101 +29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
 133.102 +29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29
 133.103 +};
 133.104 +
 133.105 +const uch _length_code[MAX_MATCH-MIN_MATCH+1]= {
 133.106 + 0,  1,  2,  3,  4,  5,  6,  7,  8,  8,  9,  9, 10, 10, 11, 11, 12, 12, 12, 12,
 133.107 +13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16, 16, 16, 16, 16,
 133.108 +17, 17, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 18, 18, 18, 19, 19, 19, 19,
 133.109 +19, 19, 19, 19, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
 133.110 +21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 22, 22, 22, 22,
 133.111 +22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 23, 23, 23, 23, 23, 23, 23, 23,
 133.112 +23, 23, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
 133.113 +24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
 133.114 +25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
 133.115 +25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 26, 26, 26, 26, 26, 26, 26, 26,
 133.116 +26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
 133.117 +26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
 133.118 +27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 28
 133.119 +};
 133.120 +
 133.121 +local const int base_length[LENGTH_CODES] = {
 133.122 +0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 20, 24, 28, 32, 40, 48, 56,
 133.123 +64, 80, 96, 112, 128, 160, 192, 224, 0
 133.124 +};
 133.125 +
 133.126 +local const int base_dist[D_CODES] = {
 133.127 +    0,     1,     2,     3,     4,     6,     8,    12,    16,    24,
 133.128 +   32,    48,    64,    96,   128,   192,   256,   384,   512,   768,
 133.129 + 1024,  1536,  2048,  3072,  4096,  6144,  8192, 12288, 16384, 24576
 133.130 +};
 133.131 +
   134.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   134.2 +++ b/libs/zlib/uncompr.c	Sat Sep 19 05:51:51 2015 +0300
   134.3 @@ -0,0 +1,61 @@
   134.4 +/* uncompr.c -- decompress a memory buffer
   134.5 + * Copyright (C) 1995-2003 Jean-loup Gailly.
   134.6 + * For conditions of distribution and use, see copyright notice in zlib.h
   134.7 + */
   134.8 +
   134.9 +/* @(#) $Id$ */
  134.10 +
  134.11 +#define ZLIB_INTERNAL
  134.12 +#include "zlib.h"
  134.13 +
  134.14 +/* ===========================================================================
  134.15 +     Decompresses the source buffer into the destination buffer.  sourceLen is
  134.16 +   the byte length of the source buffer. Upon entry, destLen is the total
  134.17 +   size of the destination buffer, which must be large enough to hold the
  134.18 +   entire uncompressed data. (The size of the uncompressed data must have
  134.19 +   been saved previously by the compressor and transmitted to the decompressor
  134.20 +   by some mechanism outside the scope of this compression library.)
  134.21 +   Upon exit, destLen is the actual size of the compressed buffer.
  134.22 +     This function can be used to decompress a whole file at once if the
  134.23 +   input file is mmap'ed.
  134.24 +
  134.25 +     uncompress returns Z_OK if success, Z_MEM_ERROR if there was not
  134.26 +   enough memory, Z_BUF_ERROR if there was not enough room in the output
  134.27 +   buffer, or Z_DATA_ERROR if the input data was corrupted.
  134.28 +*/
  134.29 +int ZEXPORT uncompress (dest, destLen, source, sourceLen)
  134.30 +    Bytef *dest;
  134.31 +    uLongf *destLen;
  134.32 +    const Bytef *source;
  134.33 +    uLong sourceLen;
  134.34 +{
  134.35 +    z_stream stream;
  134.36 +    int err;
  134.37 +
  134.38 +    stream.next_in = (Bytef*)source;
  134.39 +    stream.avail_in = (uInt)sourceLen;
  134.40 +    /* Check for source > 64K on 16-bit machine: */
  134.41 +    if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR;
  134.42 +
  134.43 +    stream.next_out = dest;
  134.44 +    stream.avail_out = (uInt)*destLen;
  134.45 +    if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;
  134.46 +
  134.47 +    stream.zalloc = (alloc_func)0;
  134.48 +    stream.zfree = (free_func)0;
  134.49 +
  134.50 +    err = inflateInit(&stream);
  134.51 +    if (err != Z_OK) return err;
  134.52 +
  134.53 +    err = inflate(&stream, Z_FINISH);
  134.54 +    if (err != Z_STREAM_END) {
  134.55 +        inflateEnd(&stream);
  134.56 +        if (err == Z_NEED_DICT || (err == Z_BUF_ERROR && stream.avail_in == 0))
  134.57 +            return Z_DATA_ERROR;
  134.58 +        return err;
  134.59 +    }
  134.60 +    *destLen = stream.total_out;
  134.61 +
  134.62 +    err = inflateEnd(&stream);
  134.63 +    return err;
  134.64 +}
   135.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   135.2 +++ b/libs/zlib/zconf.h	Sat Sep 19 05:51:51 2015 +0300
   135.3 @@ -0,0 +1,332 @@
   135.4 +/* zconf.h -- configuration of the zlib compression library
   135.5 + * Copyright (C) 1995-2005 Jean-loup Gailly.
   135.6 + * For conditions of distribution and use, see copyright notice in zlib.h
   135.7 + */
   135.8 +
   135.9 +/* @(#) $Id$ */
  135.10 +
  135.11 +#ifndef ZCONF_H
  135.12 +#define ZCONF_H
  135.13 +
  135.14 +/*
  135.15 + * If you *really* need a unique prefix for all types and library functions,
  135.16 + * compile with -DZ_PREFIX. The "standard" zlib should be compiled without it.
  135.17 + */
  135.18 +#ifdef Z_PREFIX
  135.19 +#  define deflateInit_          z_deflateInit_
  135.20 +#  define deflate               z_deflate
  135.21 +#  define deflateEnd            z_deflateEnd
  135.22 +#  define inflateInit_          z_inflateInit_
  135.23 +#  define inflate               z_inflate
  135.24 +#  define inflateEnd            z_inflateEnd
  135.25 +#  define deflateInit2_         z_deflateInit2_
  135.26 +#  define deflateSetDictionary  z_deflateSetDictionary
  135.27 +#  define deflateCopy           z_deflateCopy
  135.28 +#  define deflateReset          z_deflateReset
  135.29 +#  define deflateParams         z_deflateParams
  135.30 +#  define deflateBound          z_deflateBound
  135.31 +#  define deflatePrime          z_deflatePrime
  135.32 +#  define inflateInit2_         z_inflateInit2_
  135.33 +#  define inflateSetDictionary  z_inflateSetDictionary
  135.34 +#  define inflateSync           z_inflateSync
  135.35 +#  define inflateSyncPoint      z_inflateSyncPoint
  135.36 +#  define inflateCopy           z_inflateCopy
  135.37 +#  define inflateReset          z_inflateReset
  135.38 +#  define inflateBack           z_inflateBack
  135.39 +#  define inflateBackEnd        z_inflateBackEnd
  135.40 +#  define compress              z_compress
  135.41 +#  define compress2             z_compress2
  135.42 +#  define compressBound         z_compressBound
  135.43 +#  define uncompress            z_uncompress
  135.44 +#  define adler32               z_adler32
  135.45 +#  define crc32                 z_crc32
  135.46 +#  define get_crc_table         z_get_crc_table
  135.47 +#  define zError                z_zError
  135.48 +
  135.49 +#  define alloc_func            z_alloc_func
  135.50 +#  define free_func             z_free_func
  135.51 +#  define in_func               z_in_func
  135.52 +#  define out_func              z_out_func
  135.53 +#  define Byte                  z_Byte
  135.54 +#  define uInt                  z_uInt
  135.55 +#  define uLong                 z_uLong
  135.56 +#  define Bytef                 z_Bytef
  135.57 +#  define charf                 z_charf
  135.58 +#  define intf                  z_intf
  135.59 +#  define uIntf                 z_uIntf
  135.60 +#  define uLongf                z_uLongf
  135.61 +#  define voidpf                z_voidpf
  135.62 +#  define voidp                 z_voidp
  135.63 +#endif
  135.64 +
  135.65 +#if defined(__MSDOS__) && !defined(MSDOS)
  135.66 +#  define MSDOS
  135.67 +#endif
  135.68 +#if (defined(OS_2) || defined(__OS2__)) && !defined(OS2)
  135.69 +#  define OS2
  135.70 +#endif
  135.71 +#if defined(_WINDOWS) && !defined(WINDOWS)
  135.72 +#  define WINDOWS
  135.73 +#endif
  135.74 +#if defined(_WIN32) || defined(_WIN32_WCE) || defined(__WIN32__)
  135.75 +#  ifndef WIN32
  135.76 +#    define WIN32
  135.77 +#  endif
  135.78 +#endif
  135.79 +#if (defined(MSDOS) || defined(OS2) || defined(WINDOWS)) && !defined(WIN32)
  135.80 +#  if !defined(__GNUC__) && !defined(__FLAT__) && !defined(__386__)
  135.81 +#    ifndef SYS16BIT
  135.82 +#      define SYS16BIT
  135.83 +#    endif
  135.84 +#  endif
  135.85 +#endif
  135.86 +
  135.87 +/*
  135.88 + * Compile with -DMAXSEG_64K if the alloc function cannot allocate more
  135.89 + * than 64k bytes at a time (needed on systems with 16-bit int).
  135.90 + */
  135.91 +#ifdef SYS16BIT
  135.92 +#  define MAXSEG_64K
  135.93 +#endif
  135.94 +#ifdef MSDOS
  135.95 +#  define UNALIGNED_OK
  135.96 +#endif
  135.97 +
  135.98 +#ifdef __STDC_VERSION__
  135.99 +#  ifndef STDC
 135.100 +#    define STDC
 135.101 +#  endif
 135.102 +#  if __STDC_VERSION__ >= 199901L
 135.103 +#    ifndef STDC99
 135.104 +#      define STDC99
 135.105 +#    endif
 135.106 +#  endif
 135.107 +#endif
 135.108 +#if !defined(STDC) && (defined(__STDC__) || defined(__cplusplus))
 135.109 +#  define STDC
 135.110 +#endif
 135.111 +#if !defined(STDC) && (defined(__GNUC__) || defined(__BORLANDC__))
 135.112 +#  define STDC
 135.113 +#endif
 135.114 +#if !defined(STDC) && (defined(MSDOS) || defined(WINDOWS) || defined(WIN32))
 135.115 +#  define STDC
 135.116 +#endif
 135.117 +#if !defined(STDC) && (defined(OS2) || defined(__HOS_AIX__))
 135.118 +#  define STDC
 135.119 +#endif
 135.120 +
 135.121 +#if defined(__OS400__) && !defined(STDC)    /* iSeries (formerly AS/400). */
 135.122 +#  define STDC
 135.123 +#endif
 135.124 +
 135.125 +#ifndef STDC
 135.126 +#  ifndef const /* cannot use !defined(STDC) && !defined(const) on Mac */
 135.127 +#    define const       /* note: need a more gentle solution here */
 135.128 +#  endif
 135.129 +#endif
 135.130 +
 135.131 +/* Some Mac compilers merge all .h files incorrectly: */
 135.132 +#if defined(__MWERKS__)||defined(applec)||defined(THINK_C)||defined(__SC__)
 135.133 +#  define NO_DUMMY_DECL
 135.134 +#endif
 135.135 +
 135.136 +/* Maximum value for memLevel in deflateInit2 */
 135.137 +#ifndef MAX_MEM_LEVEL
 135.138 +#  ifdef MAXSEG_64K
 135.139 +#    define MAX_MEM_LEVEL 8
 135.140 +#  else
 135.141 +#    define MAX_MEM_LEVEL 9
 135.142 +#  endif
 135.143 +#endif
 135.144 +
 135.145 +/* Maximum value for windowBits in deflateInit2 and inflateInit2.
 135.146 + * WARNING: reducing MAX_WBITS makes minigzip unable to extract .gz files
 135.147 + * created by gzip. (Files created by minigzip can still be extracted by
 135.148 + * gzip.)
 135.149 + */
 135.150 +#ifndef MAX_WBITS
 135.151 +#  define MAX_WBITS   15 /* 32K LZ77 window */
 135.152 +#endif
 135.153 +
 135.154 +/* The memory requirements for deflate are (in bytes):
 135.155 +            (1 << (windowBits+2)) +  (1 << (memLevel+9))
 135.156 + that is: 128K for windowBits=15  +  128K for memLevel = 8  (default values)
 135.157 + plus a few kilobytes for small objects. For example, if you want to reduce
 135.158 + the default memory requirements from 256K to 128K, compile with
 135.159 +     make CFLAGS="-O -DMAX_WBITS=14 -DMAX_MEM_LEVEL=7"
 135.160 + Of course this will generally degrade compression (there's no free lunch).
 135.161 +
 135.162 +   The memory requirements for inflate are (in bytes) 1 << windowBits
 135.163 + that is, 32K for windowBits=15 (default value) plus a few kilobytes
 135.164 + for small objects.
 135.165 +*/
 135.166 +
 135.167 +                        /* Type declarations */
 135.168 +
 135.169 +#ifndef OF /* function prototypes */
 135.170 +#  ifdef STDC
 135.171 +#    define OF(args)  args
 135.172 +#  else
 135.173 +#    define OF(args)  ()
 135.174 +#  endif
 135.175 +#endif
 135.176 +
 135.177 +/* The following definitions for FAR are needed only for MSDOS mixed
 135.178 + * model programming (small or medium model with some far allocations).
 135.179 + * This was tested only with MSC; for other MSDOS compilers you may have
 135.180 + * to define NO_MEMCPY in zutil.h.  If you don't need the mixed model,
 135.181 + * just define FAR to be empty.
 135.182 + */
 135.183 +#ifdef SYS16BIT
 135.184 +#  if defined(M_I86SM) || defined(M_I86MM)
 135.185 +     /* MSC small or medium model */
 135.186 +#    define SMALL_MEDIUM
 135.187 +#    ifdef _MSC_VER
 135.188 +#      define FAR _far
 135.189 +#    else
 135.190 +#      define FAR far
 135.191 +#    endif
 135.192 +#  endif
 135.193 +#  if (defined(__SMALL__) || defined(__MEDIUM__))
 135.194 +     /* Turbo C small or medium model */
 135.195 +#    define SMALL_MEDIUM
 135.196 +#    ifdef __BORLANDC__
 135.197 +#      define FAR _far
 135.198 +#    else
 135.199 +#      define FAR far
 135.200 +#    endif
 135.201 +#  endif
 135.202 +#endif
 135.203 +
 135.204 +#if defined(WINDOWS) || defined(WIN32)
 135.205 +   /* If building or using zlib as a DLL, define ZLIB_DLL.
 135.206 +    * This is not mandatory, but it offers a little performance increase.
 135.207 +    */
 135.208 +#  ifdef ZLIB_DLL
 135.209 +#    if defined(WIN32) && (!defined(__BORLANDC__) || (__BORLANDC__ >= 0x500))
 135.210 +#      ifdef ZLIB_INTERNAL
 135.211 +#        define ZEXTERN extern __declspec(dllexport)
 135.212 +#      else
 135.213 +#        define ZEXTERN extern __declspec(dllimport)
 135.214 +#      endif
 135.215 +#    endif
 135.216 +#  endif  /* ZLIB_DLL */
 135.217 +   /* If building or using zlib with the WINAPI/WINAPIV calling convention,
 135.218 +    * define ZLIB_WINAPI.
 135.219 +    * Caution: the standard ZLIB1.DLL is NOT compiled using ZLIB_WINAPI.
 135.220 +    */
 135.221 +#  ifdef ZLIB_WINAPI
 135.222 +#    ifdef FAR
 135.223 +#      undef FAR
 135.224 +#    endif
 135.225 +#    include <windows.h>
 135.226 +     /* No need for _export, use ZLIB.DEF instead. */
 135.227 +     /* For complete Windows compatibility, use WINAPI, not __stdcall. */
 135.228 +#    define ZEXPORT WINAPI
 135.229 +#    ifdef WIN32
 135.230 +#      define ZEXPORTVA WINAPIV
 135.231 +#    else
 135.232 +#      define ZEXPORTVA FAR CDECL
 135.233 +#    endif
 135.234 +#  endif
 135.235 +#endif
 135.236 +
 135.237 +#if defined (__BEOS__)
 135.238 +#  ifdef ZLIB_DLL
 135.239 +#    ifdef ZLIB_INTERNAL
 135.240 +#      define ZEXPORT   __declspec(dllexport)
 135.241 +#      define ZEXPORTVA __declspec(dllexport)
 135.242 +#    else
 135.243 +#      define ZEXPORT   __declspec(dllimport)
 135.244 +#      define ZEXPORTVA __declspec(dllimport)
 135.245 +#    endif
 135.246 +#  endif
 135.247 +#endif
 135.248 +
 135.249 +#ifndef ZEXTERN
 135.250 +#  define ZEXTERN extern
 135.251 +#endif
 135.252 +#ifndef ZEXPORT
 135.253 +#  define ZEXPORT
 135.254 +#endif
 135.255 +#ifndef ZEXPORTVA
 135.256 +#  define ZEXPORTVA
 135.257 +#endif
 135.258 +
 135.259 +#ifndef FAR
 135.260 +#  define FAR
 135.261 +#endif
 135.262 +
 135.263 +#if !defined(__MACTYPES__)
 135.264 +typedef unsigned char  Byte;  /* 8 bits */
 135.265 +#endif
 135.266 +typedef unsigned int   uInt;  /* 16 bits or more */
 135.267 +typedef unsigned long  uLong; /* 32 bits or more */
 135.268 +
 135.269 +#ifdef SMALL_MEDIUM
 135.270 +   /* Borland C/C++ and some old MSC versions ignore FAR inside typedef */
 135.271 +#  define Bytef Byte FAR
 135.272 +#else
 135.273 +   typedef Byte  FAR Bytef;
 135.274 +#endif
 135.275 +typedef char  FAR charf;
 135.276 +typedef int   FAR intf;
 135.277 +typedef uInt  FAR uIntf;
 135.278 +typedef uLong FAR uLongf;
 135.279 +
 135.280 +#ifdef STDC
 135.281 +   typedef void const *voidpc;
 135.282 +   typedef void FAR   *voidpf;
 135.283 +   typedef void       *voidp;
 135.284 +#else
 135.285 +   typedef Byte const *voidpc;
 135.286 +   typedef Byte FAR   *voidpf;
 135.287 +   typedef Byte       *voidp;
 135.288 +#endif
 135.289 +
 135.290 +#if 0           /* HAVE_UNISTD_H -- this line is updated by ./configure */
 135.291 +#  include <sys/types.h> /* for off_t */
 135.292 +#  include <unistd.h>    /* for SEEK_* and off_t */
 135.293 +#  ifdef VMS
 135.294 +#    include <unixio.h>   /* for off_t */
 135.295 +#  endif
 135.296 +#  define z_off_t off_t
 135.297 +#endif
 135.298 +#ifndef SEEK_SET
 135.299 +#  define SEEK_SET        0       /* Seek from beginning of file.  */
 135.300 +#  define SEEK_CUR        1       /* Seek from current position.  */
 135.301 +#  define SEEK_END        2       /* Set file pointer to EOF plus "offset" */
 135.302 +#endif
 135.303 +#ifndef z_off_t
 135.304 +#  define z_off_t long
 135.305 +#endif
 135.306 +
 135.307 +#if defined(__OS400__)
 135.308 +#  define NO_vsnprintf
 135.309 +#endif
 135.310 +
 135.311 +#if defined(__MVS__)
 135.312 +#  define NO_vsnprintf
 135.313 +#  ifdef FAR
 135.314 +#    undef FAR
 135.315 +#  endif
 135.316 +#endif
 135.317 +
 135.318 +/* MVS linker does not support external names larger than 8 bytes */
 135.319 +#if defined(__MVS__)
 135.320 +#   pragma map(deflateInit_,"DEIN")
 135.321 +#   pragma map(deflateInit2_,"DEIN2")
 135.322 +#   pragma map(deflateEnd,"DEEND")
 135.323 +#   pragma map(deflateBound,"DEBND")
 135.324 +#   pragma map(inflateInit_,"ININ")
 135.325 +#   pragma map(inflateInit2_,"ININ2")
 135.326 +#   pragma map(inflateEnd,"INEND")
 135.327 +#   pragma map(inflateSync,"INSY")
 135.328 +#   pragma map(inflateSetDictionary,"INSEDI")
 135.329 +#   pragma map(compressBound,"CMBND")
 135.330 +#   pragma map(inflate_table,"INTABL")
 135.331 +#   pragma map(inflate_fast,"INFA")
 135.332 +#   pragma map(inflate_copyright,"INCOPY")
 135.333 +#endif
 135.334 +
 135.335 +#endif /* ZCONF_H */
   136.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   136.2 +++ b/libs/zlib/zlib.h	Sat Sep 19 05:51:51 2015 +0300
   136.3 @@ -0,0 +1,1357 @@
   136.4 +/* zlib.h -- interface of the 'zlib' general purpose compression library
   136.5 +  version 1.2.3, July 18th, 2005
   136.6 +
   136.7 +  Copyright (C) 1995-2005 Jean-loup Gailly and Mark Adler
   136.8 +
   136.9 +  This software is provided 'as-is', without any express or implied
  136.10 +  warranty.  In no event will the authors be held liable for any damages
  136.11 +  arising from the use of this software.
  136.12 +
  136.13 +  Permission is granted to anyone to use this software for any purpose,
  136.14 +  including commercial applications, and to alter it and redistribute it
  136.15 +  freely, subject to the following restrictions:
  136.16 +
  136.17 +  1. The origin of this software must not be misrepresented; you must not
  136.18 +     claim that you wrote the original software. If you use this software
  136.19 +     in a product, an acknowledgment in the product documentation would be
  136.20 +     appreciated but is not required.
  136.21 +  2. Altered source versions must be plainly marked as such, and must not be
  136.22 +     misrepresented as being the original software.
  136.23 +  3. This notice may not be removed or altered from any source distribution.
  136.24 +
  136.25 +  Jean-loup Gailly        Mark Adler
  136.26 +  jloup@gzip.org          madler@alumni.caltech.edu
  136.27 +
  136.28 +
  136.29 +  The data format used by the zlib library is described by RFCs (Request for
  136.30 +  Comments) 1950 to 1952 in the files http://www.ietf.org/rfc/rfc1950.txt
  136.31 +  (zlib format), rfc1951.txt (deflate format) and rfc1952.txt (gzip format).
  136.32 +*/
  136.33 +
  136.34 +#ifndef ZLIB_H
  136.35 +#define ZLIB_H
  136.36 +
  136.37 +#include "zconf.h"
  136.38 +
  136.39 +#ifdef __cplusplus
  136.40 +extern "C" {
  136.41 +#endif
  136.42 +
  136.43 +#define ZLIB_VERSION "1.2.3"
  136.44 +#define ZLIB_VERNUM 0x1230
  136.45 +
  136.46 +/*
  136.47 +     The 'zlib' compression library provides in-memory compression and
  136.48 +  decompression functions, including integrity checks of the uncompressed
  136.49 +  data.  This version of the library supports only one compression method
  136.50 +  (deflation) but other algorithms will be added later and will have the same
  136.51 +  stream interface.
  136.52 +
  136.53 +     Compression can be done in a single step if the buffers are large
  136.54 +  enough (for example if an input file is mmap'ed), or can be done by
  136.55 +  repeated calls of the compression function.  In the latter case, the
  136.56 +  application must provide more input and/or consume the output
  136.57 +  (providing more output space) before each call.
  136.58 +
  136.59 +     The compressed data format used by default by the in-memory functions is
  136.60 +  the zlib format, which is a zlib wrapper documented in RFC 1950, wrapped
  136.61 +  around a deflate stream, which is itself documented in RFC 1951.
  136.62 +
  136.63 +     The library also supports reading and writing files in gzip (.gz) format
  136.64 +  with an interface similar to that of stdio using the functions that start
  136.65 +  with "gz".  The gzip format is different from the zlib format.  gzip is a
  136.66 +  gzip wrapper, documented in RFC 1952, wrapped around a deflate stream.
  136.67 +
  136.68 +     This library can optionally read and write gzip streams in memory as well.
  136.69 +
  136.70 +     The zlib format was designed to be compact and fast for use in memory
  136.71 +  and on communications channels.  The gzip format was designed for single-
  136.72 +  file compression on file systems, has a larger header than zlib to maintain
  136.73 +  directory information, and uses a different, slower check method than zlib.
  136.74 +
  136.75 +     The library does not install any signal handler. The decoder checks
  136.76 +  the consistency of the compressed data, so the library should never
  136.77 +  crash even in case of corrupted input.
  136.78 +*/
  136.79 +
  136.80 +typedef voidpf (*alloc_func) OF((voidpf opaque, uInt items, uInt size));
  136.81 +typedef void   (*free_func)  OF((voidpf opaque, voidpf address));
  136.82 +
  136.83 +struct internal_state;
  136.84 +
  136.85 +typedef struct z_stream_s {
  136.86 +    Bytef    *next_in;  /* next input byte */
  136.87 +    uInt     avail_in;  /* number of bytes available at next_in */
  136.88 +    uLong    total_in;  /* total nb of input bytes read so far */
  136.89 +
  136.90 +    Bytef    *next_out; /* next output byte should be put there */
  136.91 +    uInt     avail_out; /* remaining free space at next_out */
  136.92 +    uLong    total_out; /* total nb of bytes output so far */
  136.93 +
  136.94 +    char     *msg;      /* last error message, NULL if no error */
  136.95 +    struct internal_state FAR *state; /* not visible by applications */
  136.96 +
  136.97 +    alloc_func zalloc;  /* used to allocate the internal state */
  136.98 +    free_func  zfree;   /* used to free the internal state */
  136.99 +    voidpf     opaque;  /* private data object passed to zalloc and zfree */
 136.100 +
 136.101 +    int     data_type;  /* best guess about the data type: binary or text */
 136.102 +    uLong   adler;      /* adler32 value of the uncompressed data */
 136.103 +    uLong   reserved;   /* reserved for future use */
 136.104 +} z_stream;
 136.105 +
 136.106 +typedef z_stream FAR *z_streamp;
 136.107 +
 136.108 +/*
 136.109 +     gzip header information passed to and from zlib routines.  See RFC 1952
 136.110 +  for more details on the meanings of these fields.
 136.111 +*/
 136.112 +typedef struct gz_header_s {
 136.113 +    int     text;       /* true if compressed data believed to be text */
 136.114 +    uLong   time;       /* modification time */
 136.115 +    int     xflags;     /* extra flags (not used when writing a gzip file) */
 136.116 +    int     os;         /* operating system */
 136.117 +    Bytef   *extra;     /* pointer to extra field or Z_NULL if none */
 136.118 +    uInt    extra_len;  /* extra field length (valid if extra != Z_NULL) */
 136.119 +    uInt    extra_max;  /* space at extra (only when reading header) */
 136.120 +    Bytef   *name;      /* pointer to zero-terminated file name or Z_NULL */
 136.121 +    uInt    name_max;   /* space at name (only when reading header) */
 136.122 +    Bytef   *comment;   /* pointer to zero-terminated comment or Z_NULL */
 136.123 +    uInt    comm_max;   /* space at comment (only when reading header) */
 136.124 +    int     hcrc;       /* true if there was or will be a header crc */
 136.125 +    int     done;       /* true when done reading gzip header (not used
 136.126 +                           when writing a gzip file) */
 136.127 +} gz_header;
 136.128 +
 136.129 +typedef gz_header FAR *gz_headerp;
 136.130 +
 136.131 +/*
 136.132 +   The application must update next_in and avail_in when avail_in has
 136.133 +   dropped to zero. It must update next_out and avail_out when avail_out
 136.134 +   has dropped to zero. The application must initialize zalloc, zfree and
 136.135 +   opaque before calling the init function. All other fields are set by the
 136.136 +   compression library and must not be updated by the application.
 136.137 +
 136.138 +   The opaque value provided by the application will be passed as the first
 136.139 +   parameter for calls of zalloc and zfree. This can be useful for custom
 136.140 +   memory management. The compression library attaches no meaning to the
 136.141 +   opaque value.
 136.142 +
 136.143 +   zalloc must return Z_NULL if there is not enough memory for the object.
 136.144 +   If zlib is used in a multi-threaded application, zalloc and zfree must be
 136.145 +   thread safe.
 136.146 +
 136.147 +   On 16-bit systems, the functions zalloc and zfree must be able to allocate
 136.148 +   exactly 65536 bytes, but will not be required to allocate more than this
 136.149 +   if the symbol MAXSEG_64K is defined (see zconf.h). WARNING: On MSDOS,
 136.150 +   pointers returned by zalloc for objects of exactly 65536 bytes *must*
 136.151 +   have their offset normalized to zero. The default allocation function
 136.152 +   provided by this library ensures this (see zutil.c). To reduce memory
 136.153 +   requirements and avoid any allocation of 64K objects, at the expense of
 136.154 +   compression ratio, compile the library with -DMAX_WBITS=14 (see zconf.h).
 136.155 +
 136.156 +   The fields total_in and total_out can be used for statistics or
 136.157 +   progress reports. After compression, total_in holds the total size of
 136.158 +   the uncompressed data and may be saved for use in the decompressor
 136.159 +   (particularly if the decompressor wants to decompress everything in
 136.160 +   a single step).
 136.161 +*/
 136.162 +
 136.163 +                        /* constants */
 136.164 +
 136.165 +#define Z_NO_FLUSH      0
 136.166 +#define Z_PARTIAL_FLUSH 1 /* will be removed, use Z_SYNC_FLUSH instead */
 136.167 +#define Z_SYNC_FLUSH    2
 136.168 +#define Z_FULL_FLUSH    3
 136.169 +#define Z_FINISH        4
 136.170 +#define Z_BLOCK         5
 136.171 +/* Allowed flush values; see deflate() and inflate() below for details */
 136.172 +
 136.173 +#define Z_OK            0
 136.174 +#define Z_STREAM_END    1
 136.175 +#define Z_NEED_DICT     2
 136.176 +#define Z_ERRNO        (-1)
 136.177 +#define Z_STREAM_ERROR (-2)
 136.178 +#define Z_DATA_ERROR   (-3)
 136.179 +#define Z_MEM_ERROR    (-4)
 136.180 +#define Z_BUF_ERROR    (-5)
 136.181 +#define Z_VERSION_ERROR (-6)
 136.182 +/* Return codes for the compression/decompression functions. Negative
 136.183 + * values are errors, positive values are used for special but normal events.
 136.184 + */
 136.185 +
 136.186 +#define Z_NO_COMPRESSION         0
 136.187 +#define Z_BEST_SPEED             1
 136.188 +#define Z_BEST_COMPRESSION       9
 136.189 +#define Z_DEFAULT_COMPRESSION  (-1)
 136.190 +/* compression levels */
 136.191 +
 136.192 +#define Z_FILTERED            1
 136.193 +#define Z_HUFFMAN_ONLY        2
 136.194 +#define Z_RLE                 3
 136.195 +#define Z_FIXED               4
 136.196 +#define Z_DEFAULT_STRATEGY    0
 136.197 +/* compression strategy; see deflateInit2() below for details */
 136.198 +
 136.199 +#define Z_BINARY   0
 136.200 +#define Z_TEXT     1
 136.201 +#define Z_ASCII    Z_TEXT   /* for compatibility with 1.2.2 and earlier */
 136.202 +#define Z_UNKNOWN  2
 136.203 +/* Possible values of the data_type field (though see inflate()) */
 136.204 +
 136.205 +#define Z_DEFLATED   8
 136.206 +/* The deflate compression method (the only one supported in this version) */
 136.207 +
 136.208 +#define Z_NULL  0  /* for initializing zalloc, zfree, opaque */
 136.209 +
 136.210 +#define zlib_version zlibVersion()
 136.211 +/* for compatibility with versions < 1.0.2 */
 136.212 +
 136.213 +                        /* basic functions */
 136.214 +
 136.215 +ZEXTERN const char * ZEXPORT zlibVersion OF((void));
 136.216 +/* The application can compare zlibVersion and ZLIB_VERSION for consistency.
 136.217 +   If the first character differs, the library code actually used is
 136.218 +   not compatible with the zlib.h header file used by the application.
 136.219 +   This check is automatically made by deflateInit and inflateInit.
 136.220 + */
 136.221 +
 136.222 +/*
 136.223 +ZEXTERN int ZEXPORT deflateInit OF((z_streamp strm, int level));
 136.224 +
 136.225 +     Initializes the internal stream state for compression. The fields
 136.226 +   zalloc, zfree and opaque must be initialized before by the caller.
 136.227 +   If zalloc and zfree are set to Z_NULL, deflateInit updates them to
 136.228 +   use default allocation functions.
 136.229 +
 136.230 +     The compression level must be Z_DEFAULT_COMPRESSION, or between 0 and 9:
 136.231 +   1 gives best speed, 9 gives best compression, 0 gives no compression at
 136.232 +   all (the input data is simply copied a block at a time).
 136.233 +   Z_DEFAULT_COMPRESSION requests a default compromise between speed and
 136.234 +   compression (currently equivalent to level 6).
 136.235 +
 136.236 +     deflateInit returns Z_OK if success, Z_MEM_ERROR if there was not
 136.237 +   enough memory, Z_STREAM_ERROR if level is not a valid compression level,
 136.238 +   Z_VERSION_ERROR if the zlib library version (zlib_version) is incompatible
 136.239 +   with the version assumed by the caller (ZLIB_VERSION).
 136.240 +   msg is set to null if there is no error message.  deflateInit does not
 136.241 +   perform any compression: this will be done by deflate().
 136.242 +*/
 136.243 +
 136.244 +
 136.245 +ZEXTERN int ZEXPORT deflate OF((z_streamp strm, int flush));
 136.246 +/*
 136.247 +    deflate compresses as much data as possible, and stops when the input
 136.248 +  buffer becomes empty or the output buffer becomes full. It may introduce some
 136.249 +  output latency (reading input without producing any output) except when
 136.250 +  forced to flush.
 136.251 +
 136.252 +    The detailed semantics are as follows. deflate performs one or both of the
 136.253 +  following actions:
 136.254 +
 136.255 +  - Compress more input starting at next_in and update next_in and avail_in
 136.256 +    accordingly. If not all input can be processed (because there is not
 136.257 +    enough room in the output buffer), next_in and avail_in are updated and
 136.258 +    processing will resume at this point for the next call of deflate().
 136.259 +
 136.260 +  - Provide more output starting at next_out and update next_out and avail_out
 136.261 +    accordingly. This action is forced if the parameter flush is non zero.
 136.262 +    Forcing flush frequently degrades the compression ratio, so this parameter
 136.263 +    should be set only when necessary (in interactive applications).
 136.264 +    Some output may be provided even if flush is not set.
 136.265 +
 136.266 +  Before the call of deflate(), the application should ensure that at least
 136.267 +  one of the actions is possible, by providing more input and/or consuming
 136.268 +  more output, and updating avail_in or avail_out accordingly; avail_out
 136.269 +  should never be zero before the call. The application can consume the
 136.270 +  compressed output when it wants, for example when the output buffer is full
 136.271 +  (avail_out == 0), or after each call of deflate(). If deflate returns Z_OK
 136.272 +  and with zero avail_out, it must be called again after making room in the
 136.273 +  output buffer because there might be more output pending.
 136.274 +
 136.275 +    Normally the parameter flush is set to Z_NO_FLUSH, which allows deflate to
 136.276 +  decide how much data to accumualte before producing output, in order to
 136.277 +  maximize compression.
 136.278 +
 136.279 +    If the parameter flush is set to Z_SYNC_FLUSH, all pending output is
 136.280 +  flushed to the output buffer and the output is aligned on a byte boundary, so
 136.281 +  that the decompressor can get all input data available so far. (In particular
 136.282 +  avail_in is zero after the call if enough output space has been provided
 136.283 +  before the call.)  Flushing may degrade compression for some compression
 136.284 +  algorithms and so it should be used only when necessary.
 136.285 +
 136.286 +    If flush is set to Z_FULL_FLUSH, all output is flushed as with
 136.287 +  Z_SYNC_FLUSH, and the compression state is reset so that decompression can
 136.288 +  restart from this point if previous compressed data has been damaged or if
 136.289 +  random access is desired. Using Z_FULL_FLUSH too often can seriously degrade
 136.290 +  compression.
 136.291 +
 136.292 +    If deflate returns with avail_out == 0, this function must be called again
 136.293 +  with the same value of the flush parameter and more output space (updated
 136.294 +  avail_out), until the flush is complete (deflate returns with non-zero
 136.295 +  avail_out). In the case of a Z_FULL_FLUSH or Z_SYNC_FLUSH, make sure that
 136.296 +  avail_out is greater than six to avoid repeated flush markers due to
 136.297 +  avail_out == 0 on return.
 136.298 +
 136.299 +    If the parameter flush is set to Z_FINISH, pending input is processed,
 136.300 +  pending output is flushed and deflate returns with Z_STREAM_END if there
 136.301 +  was enough output space; if deflate returns with Z_OK, this function must be
 136.302 +  called again with Z_FINISH and more output space (updated avail_out) but no
 136.303 +  more input data, until it returns with Z_STREAM_END or an error. After
 136.304 +  deflate has returned Z_STREAM_END, the only possible operations on the
 136.305 +  stream are deflateReset or deflateEnd.
 136.306 +
 136.307 +    Z_FINISH can be used immediately after deflateInit if all the compression
 136.308 +  is to be done in a single step. In this case, avail_out must be at least
 136.309 +  the value returned by deflateBound (see below). If deflate does not return
 136.310 +  Z_STREAM_END, then it must be called again as described above.
 136.311 +
 136.312 +    deflate() sets strm->adler to the adler32 checksum of all input read
 136.313 +  so far (that is, total_in bytes).
 136.314 +
 136.315 +    deflate() may update strm->data_type if it can make a good guess about
 136.316 +  the input data type (Z_BINARY or Z_TEXT). In doubt, the data is considered
 136.317 +  binary. This field is only for information purposes and does not affect
 136.318 +  the compression algorithm in any manner.
 136.319 +
 136.320 +    deflate() returns Z_OK if some progress has been made (more input
 136.321 +  processed or more output produced), Z_STREAM_END if all input has been
 136.322 +  consumed and all output has been produced (only when flush is set to
 136.323 +  Z_FINISH), Z_STREAM_ERROR if the stream state was inconsistent (for example
 136.324 +  if next_in or next_out was NULL), Z_BUF_ERROR if no progress is possible
 136.325 +  (for example avail_in or avail_out was zero). Note that Z_BUF_ERROR is not
 136.326 +  fatal, and deflate() can be called again with more input and more output
 136.327 +  space to continue compressing.
 136.328 +*/
 136.329 +
 136.330 +
 136.331 +ZEXTERN int ZEXPORT deflateEnd OF((z_streamp strm));
 136.332 +/*
 136.333 +     All dynamically allocated data structures for this stream are freed.
 136.334 +   This function discards any unprocessed input and does not flush any
 136.335 +   pending output.
 136.336 +
 136.337 +     deflateEnd returns Z_OK if success, Z_STREAM_ERROR if the
 136.338 +   stream state was inconsistent, Z_DATA_ERROR if the stream was freed
 136.339 +   prematurely (some input or output was discarded). In the error case,
 136.340 +   msg may be set but then points to a static string (which must not be
 136.341 +   deallocated).
 136.342 +*/
 136.343 +
 136.344 +
 136.345 +/*
 136.346 +ZEXTERN int ZEXPORT inflateInit OF((z_streamp strm));
 136.347 +
 136.348 +     Initializes the internal stream state for decompression. The fields
 136.349 +   next_in, avail_in, zalloc, zfree and opaque must be initialized before by
 136.350 +   the caller. If next_in is not Z_NULL and avail_in is large enough (the exact
 136.351 +   value depends on the compression method), inflateInit determines the
 136.352 +   compression method from the zlib header and allocates all data structures
 136.353 +   accordingly; otherwise the allocation will be deferred to the first call of
 136.354 +   inflate.  If zalloc and zfree are set to Z_NULL, inflateInit updates them to
 136.355 +   use default allocation functions.
 136.356 +
 136.357 +     inflateInit returns Z_OK if success, Z_MEM_ERROR if there was not enough
 136.358 +   memory, Z_VERSION_ERROR if the zlib library version is incompatible with the
 136.359 +   version assumed by the caller.  msg is set to null if there is no error
 136.360 +   message. inflateInit does not perform any decompression apart from reading
 136.361 +   the zlib header if present: this will be done by inflate().  (So next_in and
 136.362 +   avail_in may be modified, but next_out and avail_out are unchanged.)
 136.363 +*/
 136.364 +
 136.365 +
 136.366 +ZEXTERN int ZEXPORT inflate OF((z_streamp strm, int flush));
 136.367 +/*
 136.368 +    inflate decompresses as much data as possible, and stops when the input
 136.369 +  buffer becomes empty or the output buffer becomes full. It may introduce
 136.370 +  some output latency (reading input without producing any output) except when
 136.371 +  forced to flush.
 136.372 +
 136.373 +  The detailed semantics are as follows. inflate performs one or both of the
 136.374 +  following actions:
 136.375 +
 136.376 +  - Decompress more input starting at next_in and update next_in and avail_in
 136.377 +    accordingly. If not all input can be processed (because there is not
 136.378 +    enough room in the output buffer), next_in is updated and processing
 136.379 +    will resume at this point for the next call of inflate().
 136.380 +
 136.381 +  - Provide more output starting at next_out and update next_out and avail_out
 136.382 +    accordingly.  inflate() provides as much output as possible, until there
 136.383 +    is no more input data or no more space in the output buffer (see below
 136.384 +    about the flush parameter).
 136.385 +
 136.386 +  Before the call of inflate(), the application should ensure that at least
 136.387 +  one of the actions is possible, by providing more input and/or consuming
 136.388 +  more output, and updating the next_* and avail_* values accordingly.
 136.389 +  The application can consume the uncompressed output when it wants, for
 136.390 +  example when the output buffer is full (avail_out == 0), or after each
 136.391 +  call of inflate(). If inflate returns Z_OK and with zero avail_out, it
 136.392 +  must be called again after making room in the output buffer because there
 136.393 +  might be more output pending.
 136.394 +
 136.395 +    The flush parameter of inflate() can be Z_NO_FLUSH, Z_SYNC_FLUSH,
 136.396 +  Z_FINISH, or Z_BLOCK. Z_SYNC_FLUSH requests that inflate() flush as much
 136.397 +  output as possible to the output buffer. Z_BLOCK requests that inflate() stop
 136.398 +  if and when it gets to the next deflate block boundary. When decoding the
 136.399 +  zlib or gzip format, this will cause inflate() to return immediately after
 136.400 +  the header and before the first block. When doing a raw inflate, inflate()
 136.401 +  will go ahead and process the first block, and will return when it gets to
 136.402 +  the end of that block, or when it runs out of data.
 136.403 +
 136.404 +    The Z_BLOCK option assists in appending to or combining deflate streams.
 136.405 +  Also to assist in this, on return inflate() will set strm->data_type to the
 136.406 +  number of unused bits in the last byte taken from strm->next_in, plus 64
 136.407 +  if inflate() is currently decoding the last block in the deflate stream,
 136.408 +  plus 128 if inflate() returned immediately after decoding an end-of-block
 136.409 +  code or decoding the complete header up to just before the first byte of the
 136.410 +  deflate stream. The end-of-block will not be indicated until all of the
 136.411 +  uncompressed data from that block has been written to strm->next_out.  The
 136.412 +  number of unused bits may in general be greater than seven, except when
 136.413 +  bit 7 of data_type is set, in which case the number of unused bits will be
 136.414 +  less than eight.
 136.415 +
 136.416 +    inflate() should normally be called until it returns Z_STREAM_END or an
 136.417 +  error. However if all decompression is to be performed in a single step
 136.418 +  (a single call of inflate), the parameter flush should be set to
 136.419 +  Z_FINISH. In this case all pending input is processed and all pending
 136.420 +  output is flushed; avail_out must be large enough to hold all the
 136.421 +  uncompressed data. (The size of the uncompressed data may have been saved
 136.422 +  by the compressor for this purpose.) The next operation on this stream must
 136.423 +  be inflateEnd to deallocate the decompression state. The use of Z_FINISH
 136.424 +  is never required, but can be used to inform inflate that a faster approach
 136.425 +  may be used for the single inflate() call.
 136.426 +
 136.427 +     In this implementation, inflate() always flushes as much output as
 136.428 +  possible to the output buffer, and always uses the faster approach on the
 136.429 +  first call. So the only effect of the flush parameter in this implementation
 136.430 +  is on the return value of inflate(), as noted below, or when it returns early
 136.431 +  because Z_BLOCK is used.
 136.432 +
 136.433 +     If a preset dictionary is needed after this call (see inflateSetDictionary
 136.434 +  below), inflate sets strm->adler to the adler32 checksum of the dictionary
 136.435 +  chosen by the compressor and returns Z_NEED_DICT; otherwise it sets
 136.436 +  strm->adler to the adler32 checksum of all output produced so far (that is,
 136.437 +  total_out bytes) and returns Z_OK, Z_STREAM_END or an error code as described
 136.438 +  below. At the end of the stream, inflate() checks that its computed adler32
 136.439 +  checksum is equal to that saved by the compressor and returns Z_STREAM_END
 136.440 +  only if the checksum is correct.
 136.441 +
 136.442 +    inflate() will decompress and check either zlib-wrapped or gzip-wrapped
 136.443 +  deflate data.  The header type is detected automatically.  Any information
 136.444 +  contained in the gzip header is not retained, so applications that need that
 136.445 +  information should instead use raw inflate, see inflateInit2() below, or
 136.446 +  inflateBack() and perform their own processing of the gzip header and
 136.447 +  trailer.
 136.448 +
 136.449 +    inflate() returns Z_OK if some progress has been made (more input processed
 136.450 +  or more output produced), Z_STREAM_END if the end of the compressed data has
 136.451 +  been reached and all uncompressed output has been produced, Z_NEED_DICT if a
 136.452 +  preset dictionary is needed at this point, Z_DATA_ERROR if the input data was
 136.453 +  corrupted (input stream not conforming to the zlib format or incorrect check
 136.454 +  value), Z_STREAM_ERROR if the stream structure was inconsistent (for example
 136.455 +  if next_in or next_out was NULL), Z_MEM_ERROR if there was not enough memory,
 136.456 +  Z_BUF_ERROR if no progress is possible or if there was not enough room in the
 136.457 +  output buffer when Z_FINISH is used. Note that Z_BUF_ERROR is not fatal, and
 136.458 +  inflate() can be called again with more input and more output space to
 136.459 +  continue decompressing. If Z_DATA_ERROR is returned, the application may then
 136.460 +  call inflateSync() to look for a good compression block if a partial recovery
 136.461 +  of the data is desired.
 136.462 +*/
 136.463 +
 136.464 +
 136.465 +ZEXTERN int ZEXPORT inflateEnd OF((z_streamp strm));
 136.466 +/*
 136.467 +     All dynamically allocated data structures for this stream are freed.
 136.468 +   This function discards any unprocessed input and does not flush any
 136.469 +   pending output.
 136.470 +
 136.471 +     inflateEnd returns Z_OK if success, Z_STREAM_ERROR if the stream state
 136.472 +   was inconsistent. In the error case, msg may be set but then points to a
 136.473 +   static string (which must not be deallocated).
 136.474 +*/
 136.475 +
 136.476 +                        /* Advanced functions */
 136.477 +
 136.478 +/*
 136.479 +    The following functions are needed only in some special applications.
 136.480 +*/
 136.481 +
 136.482 +/*
 136.483 +ZEXTERN int ZEXPORT deflateInit2 OF((z_streamp strm,
 136.484 +                                     int  level,
 136.485 +                                     int  method,
 136.486 +                                     int  windowBits,
 136.487 +                                     int  memLevel,
 136.488 +                                     int  strategy));
 136.489 +
 136.490 +     This is another version of deflateInit with more compression options. The
 136.491 +   fields next_in, zalloc, zfree and opaque must be initialized before by
 136.492 +   the caller.
 136.493 +
 136.494 +     The method parameter is the compression method. It must be Z_DEFLATED in
 136.495 +   this version of the library.
 136.496 +
 136.497 +     The windowBits parameter is the base two logarithm of the window size
 136.498 +   (the size of the history buffer). It should be in the range 8..15 for this
 136.499 +   version of the library. Larger values of this parameter result in better
 136.500 +   compression at the expense of memory usage. The default value is 15 if
 136.501 +   deflateInit is used instead.
 136.502 +
 136.503 +     windowBits can also be -8..-15 for raw deflate. In this case, -windowBits
 136.504 +   determines the window size. deflate() will then generate raw deflate data
 136.505 +   with no zlib header or trailer, and will not compute an adler32 check value.
 136.506 +
 136.507 +     windowBits can also be greater than 15 for optional gzip encoding. Add
 136.508 +   16 to windowBits to write a simple gzip header and trailer around the
 136.509 +   compressed data instead of a zlib wrapper. The gzip header will have no
 136.510 +   file name, no extra data, no comment, no modification time (set to zero),
 136.511 +   no header crc, and the operating system will be set to 255 (unknown).  If a
 136.512 +   gzip stream is being written, strm->adler is a crc32 instead of an adler32.
 136.513 +
 136.514 +     The memLevel parameter specifies how much memory should be allocated
 136.515 +   for the internal compression state. memLevel=1 uses minimum memory but
 136.516 +   is slow and reduces compression ratio; memLevel=9 uses maximum memory
 136.517 +   for optimal speed. The default value is 8. See zconf.h for total memory
 136.518 +   usage as a function of windowBits and memLevel.
 136.519 +
 136.520 +     The strategy parameter is used to tune the compression algorithm. Use the
 136.521 +   value Z_DEFAULT_STRATEGY for normal data, Z_FILTERED for data produced by a
 136.522 +   filter (or predictor), Z_HUFFMAN_ONLY to force Huffman encoding only (no
 136.523 +   string match), or Z_RLE to limit match distances to one (run-length
 136.524 +   encoding). Filtered data consists mostly of small values with a somewhat
 136.525 +   random distribution. In this case, the compression algorithm is tuned to
 136.526 +   compress them better. The effect of Z_FILTERED is to force more Huffman
 136.527 +   coding and less string matching; it is somewhat intermediate between
 136.528 +   Z_DEFAULT and Z_HUFFMAN_ONLY. Z_RLE is designed to be almost as fast as
 136.529 +   Z_HUFFMAN_ONLY, but give better compression for PNG image data. The strategy
 136.530 +   parameter only affects the compression ratio but not the correctness of the
 136.531 +   compressed output even if it is not set appropriately.  Z_FIXED prevents the
 136.532 +   use of dynamic Huffman codes, allowing for a simpler decoder for special
 136.533 +   applications.
 136.534 +
 136.535 +      deflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
 136.536 +   memory, Z_STREAM_ERROR if a parameter is invalid (such as an invalid
 136.537 +   method). msg is set to null if there is no error message.  deflateInit2 does
 136.538 +   not perform any compression: this will be done by deflate().
 136.539 +*/
 136.540 +
 136.541 +ZEXTERN int ZEXPORT deflateSetDictionary OF((z_streamp strm,
 136.542 +                                             const Bytef *dictionary,
 136.543 +                                             uInt  dictLength));
 136.544 +/*
 136.545 +     Initializes the compression dictionary from the given byte sequence
 136.546 +   without producing any compressed output. This function must be called
 136.547 +   immediately after deflateInit, deflateInit2 or deflateReset, before any
 136.548 +   call of deflate. The compressor and decompressor must use exactly the same
 136.549 +   dictionary (see inflateSetDictionary).
 136.550 +
 136.551 +     The dictionary should consist of strings (byte sequences) that are likely
 136.552 +   to be encountered later in the data to be compressed, with the most commonly
 136.553 +   used strings preferably put towards the end of the dictionary. Using a
 136.554 +   dictionary is most useful when the data to be compressed is short and can be
 136.555 +   predicted with good accuracy; the data can then be compressed better than
 136.556 +   with the default empty dictionary.
 136.557 +
 136.558 +     Depending on the size of the compression data structures selected by
 136.559 +   deflateInit or deflateInit2, a part of the dictionary may in effect be
 136.560 +   discarded, for example if the dictionary is larger than the window size in
 136.561 +   deflate or deflate2. Thus the strings most likely to be useful should be
 136.562 +   put at the end of the dictionary, not at the front. In addition, the
 136.563 +   current implementation of deflate will use at most the window size minus
 136.564 +   262 bytes of the provided dictionary.
 136.565 +
 136.566 +     Upon return of this function, strm->adler is set to the adler32 value
 136.567 +   of the dictionary; the decompressor may later use this value to determine
 136.568 +   which dictionary has been used by the compressor. (The adler32 value
 136.569 +   applies to the whole dictionary even if only a subset of the dictionary is
 136.570 +   actually used by the compressor.) If a raw deflate was requested, then the
 136.571 +   adler32 value is not computed and strm->adler is not set.
 136.572 +
 136.573 +     deflateSetDictionary returns Z_OK if success, or Z_STREAM_ERROR if a
 136.574 +   parameter is invalid (such as NULL dictionary) or the stream state is
 136.575 +   inconsistent (for example if deflate has already been called for this stream
 136.576 +   or if the compression method is bsort). deflateSetDictionary does not
 136.577 +   perform any compression: this will be done by deflate().
 136.578 +*/
 136.579 +
 136.580 +ZEXTERN int ZEXPORT deflateCopy OF((z_streamp dest,
 136.581 +                                    z_streamp source));
 136.582 +/*
 136.583 +     Sets the destination stream as a complete copy of the source stream.
 136.584 +
 136.585 +     This function can be useful when several compression strategies will be
 136.586 +   tried, for example when there are several ways of pre-processing the input
 136.587 +   data with a filter. The streams that will be discarded should then be freed
 136.588 +   by calling deflateEnd.  Note that deflateCopy duplicates the internal
 136.589 +   compression state which can be quite large, so this strategy is slow and
 136.590 +   can consume lots of memory.
 136.591 +
 136.592 +     deflateCopy returns Z_OK if success, Z_MEM_ERROR if there was not
 136.593 +   enough memory, Z_STREAM_ERROR if the source stream state was inconsistent
 136.594 +   (such as zalloc being NULL). msg is left unchanged in both source and
 136.595 +   destination.
 136.596 +*/
 136.597 +
 136.598 +ZEXTERN int ZEXPORT deflateReset OF((z_streamp strm));
 136.599 +/*
 136.600 +     This function is equivalent to deflateEnd followed by deflateInit,
 136.601 +   but does not free and reallocate all the internal compression state.
 136.602 +   The stream will keep the same compression level and any other attributes
 136.603 +   that may have been set by deflateInit2.
 136.604 +
 136.605 +      deflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source
 136.606 +   stream state was inconsistent (such as zalloc or state being NULL).
 136.607 +*/
 136.608 +
 136.609 +ZEXTERN int ZEXPORT deflateParams OF((z_streamp strm,
 136.610 +                                      int level,
 136.611 +                                      int strategy));
 136.612 +/*
 136.613 +     Dynamically update the compression level and compression strategy.  The
 136.614 +   interpretation of level and strategy is as in deflateInit2.  This can be
 136.615 +   used to switch between compression and straight copy of the input data, or
 136.616 +   to switch to a different kind of input data requiring a different
 136.617 +   strategy. If the compression level is changed, the input available so far
 136.618 +   is compressed with the old level (and may be flushed); the new level will
 136.619 +   take effect only at the next call of deflate().
 136.620 +
 136.621 +     Before the call of deflateParams, the stream state must be set as for
 136.622 +   a call of deflate(), since the currently available input may have to
 136.623 +   be compressed and flushed. In particular, strm->avail_out must be non-zero.
 136.624 +
 136.625 +     deflateParams returns Z_OK if success, Z_STREAM_ERROR if the source
 136.626 +   stream state was inconsistent or if a parameter was invalid, Z_BUF_ERROR
 136.627 +   if strm->avail_out was zero.
 136.628 +*/
 136.629 +
 136.630 +ZEXTERN int ZEXPORT deflateTune OF((z_streamp strm,
 136.631 +                                    int good_length,
 136.632 +                                    int max_lazy,
 136.633 +                                    int nice_length,
 136.634 +                                    int max_chain));
 136.635 +/*
 136.636 +     Fine tune deflate's internal compression parameters.  This should only be
 136.637 +   used by someone who understands the algorithm used by zlib's deflate for
 136.638 +   searching for the best matching string, and even then only by the most
 136.639 +   fanatic optimizer trying to squeeze out the last compressed bit for their
 136.640 +   specific input data.  Read the deflate.c source code for the meaning of the
 136.641 +   max_lazy, good_length, nice_length, and max_chain parameters.
 136.642 +
 136.643 +     deflateTune() can be called after deflateInit() or deflateInit2(), and
 136.644 +   returns Z_OK on success, or Z_STREAM_ERROR for an invalid deflate stream.
 136.645 + */
 136.646 +
 136.647 +ZEXTERN uLong ZEXPORT deflateBound OF((z_streamp strm,
 136.648 +                                       uLong sourceLen));
 136.649 +/*
 136.650 +     deflateBound() returns an upper bound on the compressed size after
 136.651 +   deflation of sourceLen bytes.  It must be called after deflateInit()
 136.652 +   or deflateInit2().  This would be used to allocate an output buffer
 136.653 +   for deflation in a single pass, and so would be called before deflate().
 136.654 +*/
 136.655 +
 136.656 +ZEXTERN int ZEXPORT deflatePrime OF((z_streamp strm,
 136.657 +                                     int bits,
 136.658 +                                     int value));
 136.659 +/*
 136.660 +     deflatePrime() inserts bits in the deflate output stream.  The intent
 136.661 +  is that this function is used to start off the deflate output with the
 136.662 +  bits leftover from a previous deflate stream when appending to it.  As such,
 136.663 +  this function can only be used for raw deflate, and must be used before the
 136.664 +  first deflate() call after a deflateInit2() or deflateReset().  bits must be
 136.665 +  less than or equal to 16, and that many of the least significant bits of
 136.666 +  value will be inserted in the output.
 136.667 +
 136.668 +      deflatePrime returns Z_OK if success, or Z_STREAM_ERROR if the source
 136.669 +   stream state was inconsistent.
 136.670 +*/
 136.671 +
 136.672 +ZEXTERN int ZEXPORT deflateSetHeader OF((z_streamp strm,
 136.673 +                                         gz_headerp head));
 136.674 +/*
 136.675 +      deflateSetHeader() provides gzip header information for when a gzip
 136.676 +   stream is requested by deflateInit2().  deflateSetHeader() may be called
 136.677 +   after deflateInit2() or deflateReset() and before the first call of
 136.678 +   deflate().  The text, time, os, extra field, name, and comment information
 136.679 +   in the provided gz_header structure are written to the gzip header (xflag is
 136.680 +   ignored -- the extra flags are set according to the compression level).  The
 136.681 +   caller must assure that, if not Z_NULL, name and comment are terminated with
 136.682 +   a zero byte, and that if extra is not Z_NULL, that extra_len bytes are
 136.683 +   available there.  If hcrc is true, a gzip header crc is included.  Note that
 136.684 +   the current versions of the command-line version of gzip (up through version
 136.685 +   1.3.x) do not support header crc's, and will report that it is a "multi-part
 136.686 +   gzip file" and give up.
 136.687 +
 136.688 +      If deflateSetHeader is not used, the default gzip header has text false,
 136.689 +   the time set to zero, and os set to 255, with no extra, name, or comment
 136.690 +   fields.  The gzip header is returned to the default state by deflateReset().
 136.691 +
 136.692 +      deflateSetHeader returns Z_OK if success, or Z_STREAM_ERROR if the source
 136.693 +   stream state was inconsistent.
 136.694 +*/
 136.695 +
 136.696 +/*
 136.697 +ZEXTERN int ZEXPORT inflateInit2 OF((z_streamp strm,
 136.698 +                                     int  windowBits));
 136.699 +
 136.700 +     This is another version of inflateInit with an extra parameter. The
 136.701 +   fields next_in, avail_in, zalloc, zfree and opaque must be initialized
 136.702 +   before by the caller.
 136.703 +
 136.704 +     The windowBits parameter is the base two logarithm of the maximum window
 136.705 +   size (the size of the history buffer).  It should be in the range 8..15 for
 136.706 +   this version of the library. The default value is 15 if inflateInit is used
 136.707 +   instead. windowBits must be greater than or equal to the windowBits value
 136.708 +   provided to deflateInit2() while compressing, or it must be equal to 15 if
 136.709 +   deflateInit2() was not used. If a compressed stream with a larger window
 136.710 +   size is given as input, inflate() will return with the error code
 136.711 +   Z_DATA_ERROR instead of trying to allocate a larger window.
 136.712 +
 136.713 +     windowBits can also be -8..-15 for raw inflate. In this case, -windowBits
 136.714 +   determines the window size. inflate() will then process raw deflate data,
 136.715 +   not looking for a zlib or gzip header, not generating a check value, and not
 136.716 +   looking for any check values for comparison at the end of the stream. This
 136.717 +   is for use with other formats that use the deflate compressed data format
 136.718 +   such as zip.  Those formats provide their own check values. If a custom
 136.719 +   format is developed using the raw deflate format for compressed data, it is
 136.720 +   recommended that a check value such as an adler32 or a crc32 be applied to
 136.721 +   the uncompressed data as is done in the zlib, gzip, and zip formats.  For
 136.722 +   most applications, the zlib format should be used as is. Note that comments
 136.723 +   above on the use in deflateInit2() applies to the magnitude of windowBits.
 136.724 +
 136.725 +     windowBits can also be greater than 15 for optional gzip decoding. Add
 136.726 +   32 to windowBits to enable zlib and gzip decoding with automatic header
 136.727 +   detection, or add 16 to decode only the gzip format (the zlib format will
 136.728 +   return a Z_DATA_ERROR).  If a gzip stream is being decoded, strm->adler is
 136.729 +   a crc32 instead of an adler32.
 136.730 +
 136.731 +     inflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
 136.732 +   memory, Z_STREAM_ERROR if a parameter is invalid (such as a null strm). msg
 136.733 +   is set to null if there is no error message.  inflateInit2 does not perform
 136.734 +   any decompression apart from reading the zlib header if present: this will
 136.735 +   be done by inflate(). (So next_in and avail_in may be modified, but next_out
 136.736 +   and avail_out are unchanged.)
 136.737 +*/
 136.738 +
 136.739 +ZEXTERN int ZEXPORT inflateSetDictionary OF((z_streamp strm,
 136.740 +                                             const Bytef *dictionary,
 136.741 +                                             uInt  dictLength));
 136.742 +/*
 136.743 +     Initializes the decompression dictionary from the given uncompressed byte
 136.744 +   sequence. This function must be called immediately after a call of inflate,
 136.745 +   if that call returned Z_NEED_DICT. The dictionary chosen by the compressor
 136.746 +   can be determined from the adler32 value returned by that call of inflate.
 136.747 +   The compressor and decompressor must use exactly the same dictionary (see
 136.748 +   deflateSetDictionary).  For raw inflate, this function can be called
 136.749 +   immediately after inflateInit2() or inflateReset() and before any call of
 136.750 +   inflate() to set the dictionary.  The application must insure that the
 136.751 +   dictionary that was used for compression is provided.
 136.752 +
 136.753 +     inflateSetDictionary returns Z_OK if success, Z_STREAM_ERROR if a
 136.754 +   parameter is invalid (such as NULL dictionary) or the stream state is
 136.755 +   inconsistent, Z_DATA_ERROR if the given dictionary doesn't match the
 136.756 +   expected one (incorrect adler32 value). inflateSetDictionary does not
 136.757 +   perform any decompression: this will be done by subsequent calls of
 136.758 +   inflate().
 136.759 +*/
 136.760 +
 136.761 +ZEXTERN int ZEXPORT inflateSync OF((z_streamp strm));
 136.762 +/*
 136.763 +    Skips invalid compressed data until a full flush point (see above the
 136.764 +  description of deflate with Z_FULL_FLUSH) can be found, or until all
 136.765 +  available input is skipped. No output is provided.
 136.766 +
 136.767 +    inflateSync returns Z_OK if a full flush point has been found, Z_BUF_ERROR
 136.768 +  if no more input was provided, Z_DATA_ERROR if no flush point has been found,
 136.769 +  or Z_STREAM_ERROR if the stream structure was inconsistent. In the success
 136.770 +  case, the application may save the current current value of total_in which
 136.771 +  indicates where valid compressed data was found. In the error case, the
 136.772 +  application may repeatedly call inflateSync, providing more input each time,
 136.773 +  until success or end of the input data.
 136.774 +*/
 136.775 +
 136.776 +ZEXTERN int ZEXPORT inflateCopy OF((z_streamp dest,
 136.777 +                                    z_streamp source));
 136.778 +/*
 136.779 +     Sets the destination stream as a complete copy of the source stream.
 136.780 +
 136.781 +     This function can be useful when randomly accessing a large stream.  The
 136.782 +   first pass through the stream can periodically record the inflate state,
 136.783 +   allowing restarting inflate at those points when randomly accessing the
 136.784 +   stream.
 136.785 +
 136.786 +     inflateCopy returns Z_OK if success, Z_MEM_ERROR if there was not
 136.787 +   enough memory, Z_STREAM_ERROR if the source stream state was inconsistent
 136.788 +   (such as zalloc being NULL). msg is left unchanged in both source and
 136.789 +   destination.
 136.790 +*/
 136.791 +
 136.792 +ZEXTERN int ZEXPORT inflateReset OF((z_streamp strm));
 136.793 +/*
 136.794 +     This function is equivalent to inflateEnd followed by inflateInit,
 136.795 +   but does not free and reallocate all the internal decompression state.
 136.796 +   The stream will keep attributes that may have been set by inflateInit2.
 136.797 +
 136.798 +      inflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source
 136.799 +   stream state was inconsistent (such as zalloc or state being NULL).
 136.800 +*/
 136.801 +
 136.802 +ZEXTERN int ZEXPORT inflatePrime OF((z_streamp strm,
 136.803 +                                     int bits,
 136.804 +                                     int value));
 136.805 +/*
 136.806 +     This function inserts bits in the inflate input stream.  The intent is
 136.807 +  that this function is used to start inflating at a bit position in the
 136.808 +  middle of a byte.  The provided bits will be used before any bytes are used
 136.809 +  from next_in.  This function should only be used with raw inflate, and
 136.810 +  should be used before the first inflate() call after inflateInit2() or
 136.811 +  inflateReset().  bits must be less than or equal to 16, and that many of the
 136.812 +  least significant bits of value will be inserted in the input.
 136.813 +
 136.814 +      inflatePrime returns Z_OK if success, or Z_STREAM_ERROR if the source
 136.815 +   stream state was inconsistent.
 136.816 +*/
 136.817 +
 136.818 +ZEXTERN int ZEXPORT inflateGetHeader OF((z_streamp strm,
 136.819 +                                         gz_headerp head));
 136.820 +/*
 136.821 +      inflateGetHeader() requests that gzip header information be stored in the
 136.822 +   provided gz_header structure.  inflateGetHeader() may be called after
 136.823 +   inflateInit2() or inflateReset(), and before the first call of inflate().
 136.824 +   As inflate() processes the gzip stream, head->done is zero until the header
 136.825 +   is completed, at which time head->done is set to one.  If a zlib stream is
 136.826 +   being decoded, then head->done is set to -1 to indicate that there will be
 136.827 +   no gzip header information forthcoming.  Note that Z_BLOCK can be used to
 136.828 +   force inflate() to return immediately after header processing is complete
 136.829 +   and before any actual data is decompressed.
 136.830 +
 136.831 +      The text, time, xflags, and os fields are filled in with the gzip header
 136.832 +   contents.  hcrc is set to true if there is a header CRC.  (The header CRC
 136.833 +   was valid if done is set to one.)  If extra is not Z_NULL, then extra_max
 136.834 +   contains the maximum number of bytes to write to extra.  Once done is true,
 136.835 +   extra_len contains the actual extra field length, and extra contains the
 136.836 +   extra field, or that field truncated if extra_max is less than extra_len.
 136.837 +   If name is not Z_NULL, then up to name_max characters are written there,
 136.838 +   terminated with a zero unless the length is greater than name_max.  If
 136.839 +   comment is not Z_NULL, then up to comm_max characters are written there,
 136.840 +   terminated with a zero unless the length is greater than comm_max.  When
 136.841 +   any of extra, name, or comment are not Z_NULL and the respective field is
 136.842 +   not present in the header, then that field is set to Z_NULL to signal its
 136.843 +   absence.  This allows the use of deflateSetHeader() with the returned
 136.844 +   structure to duplicate the header.  However if those fields are set to
 136.845 +   allocated memory, then the application will need to save those pointers
 136.846 +   elsewhere so that they can be eventually freed.
 136.847 +
 136.848 +      If inflateGetHeader is not used, then the header information is simply
 136.849 +   discarded.  The header is always checked for validity, including the header
 136.850 +   CRC if present.  inflateReset() will reset the process to discard the header
 136.851 +   information.  The application would need to call inflateGetHeader() again to
 136.852 +   retrieve the header from the next gzip stream.
 136.853 +
 136.854 +      inflateGetHeader returns Z_OK if success, or Z_STREAM_ERROR if the source
 136.855 +   stream state was inconsistent.
 136.856 +*/
 136.857 +
 136.858 +/*
 136.859 +ZEXTERN int ZEXPORT inflateBackInit OF((z_streamp strm, int windowBits,
 136.860 +                                        unsigned char FAR *window));
 136.861 +
 136.862 +     Initialize the internal stream state for decompression using inflateBack()
 136.863 +   calls.  The fields zalloc, zfree and opaque in strm must be initialized
 136.864 +   before the call.  If zalloc and zfree are Z_NULL, then the default library-
 136.865 +   derived memory allocation routines are used.  windowBits is the base two
 136.866 +   logarithm of the window size, in the range 8..15.  window is a caller
 136.867 +   supplied buffer of that size.  Except for special applications where it is
 136.868 +   assured that deflate was used with small window sizes, windowBits must be 15
 136.869 +   and a 32K byte window must be supplied to be able to decompress general
 136.870 +   deflate streams.
 136.871 +
 136.872 +     See inflateBack() for the usage of these routines.
 136.873 +
 136.874 +     inflateBackInit will return Z_OK on success, Z_STREAM_ERROR if any of
 136.875 +   the paramaters are invalid, Z_MEM_ERROR if the internal state could not
 136.876 +   be allocated, or Z_VERSION_ERROR if the version of the library does not
 136.877 +   match the version of the header file.
 136.878 +*/
 136.879 +
 136.880 +typedef unsigned (*in_func) OF((void FAR *, unsigned char FAR * FAR *));
 136.881 +typedef int (*out_func) OF((void FAR *, unsigned char FAR *, unsigned));
 136.882 +
 136.883 +ZEXTERN int ZEXPORT inflateBack OF((z_streamp strm,
 136.884 +                                    in_func in, void FAR *in_desc,
 136.885 +                                    out_func out, void FAR *out_desc));
 136.886 +/*
 136.887 +     inflateBack() does a raw inflate with a single call using a call-back
 136.888 +   interface for input and output.  This is more efficient than inflate() for
 136.889 +   file i/o applications in that it avoids copying between the output and the
 136.890 +   sliding window by simply making the window itself the output buffer.  This
 136.891 +   function trusts the application to not change the output buffer passed by
 136.892 +   the output function, at least until inflateBack() returns.
 136.893 +
 136.894 +     inflateBackInit() must be called first to allocate the internal state
 136.895 +   and to initialize the state with the user-provided window buffer.
 136.896 +   inflateBack() may then be used multiple times to inflate a complete, raw
 136.897 +   deflate stream with each call.  inflateBackEnd() is then called to free
 136.898 +   the allocated state.
 136.899 +
 136.900 +     A raw deflate stream is one with no zlib or gzip header or trailer.
 136.901 +   This routine would normally be used in a utility that reads zip or gzip
 136.902 +   files and writes out uncompressed files.  The utility would decode the
 136.903 +   header and process the trailer on its own, hence this routine expects
 136.904 +   only the raw deflate stream to decompress.  This is different from the
 136.905 +   normal behavior of inflate(), which expects either a zlib or gzip header and
 136.906 +   trailer around the deflate stream.
 136.907 +
 136.908 +     inflateBack() uses two subroutines supplied by the caller that are then
 136.909 +   called by inflateBack() for input and output.  inflateBack() calls those
 136.910 +   routines until it reads a complete deflate stream and writes out all of the
 136.911 +   uncompressed data, or until it encounters an error.  The function's
 136.912 +   parameters and return types are defined above in the in_func and out_func
 136.913 +   typedefs.  inflateBack() will call in(in_desc, &buf) which should return the
 136.914 +   number of bytes of provided input, and a pointer to that input in buf.  If
 136.915 +   there is no input available, in() must return zero--buf is ignored in that
 136.916 +   case--and inflateBack() will return a buffer error.  inflateBack() will call
 136.917 +   out(out_desc, buf, len) to write the uncompressed data buf[0..len-1].  out()
 136.918 +   should return zero on success, or non-zero on failure.  If out() returns
 136.919 +   non-zero, inflateBack() will return with an error.  Neither in() nor out()
 136.920 +   are permitted to change the contents of the window provided to
 136.921 +   inflateBackInit(), which is also the buffer that out() uses to write from.
 136.922 +   The length written by out() will be at most the window size.  Any non-zero
 136.923 +   amount of input may be provided by in().
 136.924 +
 136.925 +     For convenience, inflateBack() can be provided input on the first call by
 136.926 +   setting strm->next_in and strm->avail_in.  If that input is exhausted, then
 136.927 +   in() will be called.  Therefore strm->next_in must be initialized before
 136.928 +   calling inflateBack().  If strm->next_in is Z_NULL, then in() will be called
 136.929 +   immediately for input.  If strm->next_in is not Z_NULL, then strm->avail_in
 136.930 +   must also be initialized, and then if strm->avail_in is not zero, input will
 136.931 +   initially be taken from strm->next_in[0 .. strm->avail_in - 1].
 136.932 +
 136.933 +     The in_desc and out_desc parameters of inflateBack() is passed as the
 136.934 +   first parameter of in() and out() respectively when they are called.  These
 136.935 +   descriptors can be optionally used to pass any information that the caller-
 136.936 +   supplied in() and out() functions need to do their job.
 136.937 +
 136.938 +     On return, inflateBack() will set strm->next_in and strm->avail_in to
 136.939 +   pass back any unused input that was provided by the last in() call.  The
 136.940 +   return values of inflateBack() can be Z_STREAM_END on success, Z_BUF_ERROR
 136.941 +   if in() or out() returned an error, Z_DATA_ERROR if there was a format
 136.942 +   error in the deflate stream (in which case strm->msg is set to indicate the
 136.943 +   nature of the error), or Z_STREAM_ERROR if the stream was not properly
 136.944 +   initialized.  In the case of Z_BUF_ERROR, an input or output error can be
 136.945 +   distinguished using strm->next_in which will be Z_NULL only if in() returned
 136.946 +   an error.  If strm->next is not Z_NULL, then the Z_BUF_ERROR was due to
 136.947 +   out() returning non-zero.  (in() will always be called before out(), so
 136.948 +   strm->next_in is assured to be defined if out() returns non-zero.)  Note
 136.949 +   that inflateBack() cannot return Z_OK.
 136.950 +*/
 136.951 +
 136.952 +ZEXTERN int ZEXPORT inflateBackEnd OF((z_streamp strm));
 136.953 +/*
 136.954 +     All memory allocated by inflateBackInit() is freed.
 136.955 +
 136.956 +     inflateBackEnd() returns Z_OK on success, or Z_STREAM_ERROR if the stream
 136.957 +   state was inconsistent.
 136.958 +*/
 136.959 +
 136.960 +ZEXTERN uLong ZEXPORT zlibCompileFlags OF((void));
 136.961 +/* Return flags indicating compile-time options.
 136.962 +
 136.963 +    Type sizes, two bits each, 00 = 16 bits, 01 = 32, 10 = 64, 11 = other:
 136.964 +     1.0: size of uInt
 136.965 +     3.2: size of uLong
 136.966 +     5.4: size of voidpf (pointer)
 136.967 +     7.6: size of z_off_t
 136.968 +
 136.969 +    Compiler, assembler, and debug options:
 136.970 +     8: DEBUG
 136.971 +     9: ASMV or ASMINF -- use ASM code
 136.972 +     10: ZLIB_WINAPI -- exported functions use the WINAPI calling convention
 136.973 +     11: 0 (reserved)
 136.974 +
 136.975 +    One-time table building (smaller code, but not thread-safe if true):
 136.976 +     12: BUILDFIXED -- build static block decoding tables when needed
 136.977 +     13: DYNAMIC_CRC_TABLE -- build CRC calculation tables when needed
 136.978 +     14,15: 0 (reserved)
 136.979 +
 136.980 +    Library content (indicates missing functionality):
 136.981 +     16: NO_GZCOMPRESS -- gz* functions cannot compress (to avoid linking
 136.982 +                          deflate code when not needed)
 136.983 +     17: NO_GZIP -- deflate can't write gzip streams, and inflate can't detect
 136.984 +                    and decode gzip streams (to avoid linking crc code)
 136.985 +     18-19: 0 (reserved)
 136.986 +
 136.987 +    Operation variations (changes in library functionality):
 136.988 +     20: PKZIP_BUG_WORKAROUND -- slightly more permissive inflate
 136.989 +     21: FASTEST -- deflate algorithm with only one, lowest compression level
 136.990 +     22,23: 0 (reserved)
 136.991 +
 136.992 +    The sprintf variant used by gzprintf (zero is best):
 136.993 +     24: 0 = vs*, 1 = s* -- 1 means limited to 20 arguments after the format
 136.994 +     25: 0 = *nprintf, 1 = *printf -- 1 means gzprintf() not secure!
 136.995 +     26: 0 = returns value, 1 = void -- 1 means inferred string length returned
 136.996 +
 136.997 +    Remainder:
 136.998 +     27-31: 0 (reserved)
 136.999 + */
136.1000 +
136.1001 +
136.1002 +                        /* utility functions */
136.1003 +
136.1004 +/*
136.1005 +     The following utility functions are implemented on top of the
136.1006 +   basic stream-oriented functions. To simplify the interface, some
136.1007 +   default options are assumed (compression level and memory usage,
136.1008 +   standard memory allocation functions). The source code of these
136.1009 +   utility functions can easily be modified if you need special options.
136.1010 +*/
136.1011 +
136.1012 +ZEXTERN int ZEXPORT compress OF((Bytef *dest,   uLongf *destLen,
136.1013 +                                 const Bytef *source, uLong sourceLen));
136.1014 +/*
136.1015 +     Compresses the source buffer into the destination buffer.  sourceLen is
136.1016 +   the byte length of the source buffer. Upon entry, destLen is the total
136.1017 +   size of the destination buffer, which must be at least the value returned
136.1018 +   by compressBound(sourceLen). Upon exit, destLen is the actual size of the
136.1019 +   compressed buffer.
136.1020 +     This function can be used to compress a whole file at once if the
136.1021 +   input file is mmap'ed.
136.1022 +     compress returns Z_OK if success, Z_MEM_ERROR if there was not
136.1023 +   enough memory, Z_BUF_ERROR if there was not enough room in the output
136.1024 +   buffer.
136.1025 +*/
136.1026 +
136.1027 +ZEXTERN int ZEXPORT compress2 OF((Bytef *dest,   uLongf *destLen,
136.1028 +                                  const Bytef *source, uLong sourceLen,
136.1029 +                                  int level));
136.1030 +/*
136.1031 +     Compresses the source buffer into the destination buffer. The level
136.1032 +   parameter has the same meaning as in deflateInit.  sourceLen is the byte
136.1033 +   length of the source buffer. Upon entry, destLen is the total size of the
136.1034 +   destination buffer, which must be at least the value returned by
136.1035 +   compressBound(sourceLen). Upon exit, destLen is the actual size of the
136.1036 +   compressed buffer.
136.1037 +
136.1038 +     compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
136.1039 +   memory, Z_BUF_ERROR if there was not enough room in the output buffer,
136.1040 +   Z_STREAM_ERROR if the level parameter is invalid.
136.1041 +*/
136.1042 +
136.1043 +ZEXTERN uLong ZEXPORT compressBound OF((uLong sourceLen));
136.1044 +/*
136.1045 +     compressBound() returns an upper bound on the compressed size after
136.1046 +   compress() or compress2() on sourceLen bytes.  It would be used before
136.1047 +   a compress() or compress2() call to allocate the destination buffer.
136.1048 +*/
136.1049 +
136.1050 +ZEXTERN int ZEXPORT uncompress OF((Bytef *dest,   uLongf *destLen,
136.1051 +                                   const Bytef *source, uLong sourceLen));
136.1052 +/*
136.1053 +     Decompresses the source buffer into the destination buffer.  sourceLen is
136.1054 +   the byte length of the source buffer. Upon entry, destLen is the total
136.1055 +   size of the destination buffer, which must be large enough to hold the
136.1056 +   entire uncompressed data. (The size of the uncompressed data must have
136.1057 +   been saved previously by the compressor and transmitted to the decompressor
136.1058 +   by some mechanism outside the scope of this compression library.)
136.1059 +   Upon exit, destLen is the actual size of the compressed buffer.
136.1060 +     This function can be used to decompress a whole file at once if the
136.1061 +   input file is mmap'ed.
136.1062 +
136.1063 +     uncompress returns Z_OK if success, Z_MEM_ERROR if there was not
136.1064 +   enough memory, Z_BUF_ERROR if there was not enough room in the output
136.1065 +   buffer, or Z_DATA_ERROR if the input data was corrupted or incomplete.
136.1066 +*/
136.1067 +
136.1068 +
136.1069 +typedef voidp gzFile;
136.1070 +
136.1071 +ZEXTERN gzFile ZEXPORT gzopen  OF((const char *path, const char *mode));
136.1072 +/*
136.1073 +     Opens a gzip (.gz) file for reading or writing. The mode parameter
136.1074 +   is as in fopen ("rb" or "wb") but can also include a compression level
136.1075 +   ("wb9") or a strategy: 'f' for filtered data as in "wb6f", 'h' for
136.1076 +   Huffman only compression as in "wb1h", or 'R' for run-length encoding
136.1077 +   as in "wb1R". (See the description of deflateInit2 for more information
136.1078 +   about the strategy parameter.)
136.1079 +
136.1080 +     gzopen can be used to read a file which is not in gzip format; in this
136.1081 +   case gzread will directly read from the file without decompression.
136.1082 +
136.1083 +     gzopen returns NULL if the file could not be opened or if there was
136.1084 +   insufficient memory to allocate the (de)compression state; errno
136.1085 +   can be checked to distinguish the two cases (if errno is zero, the
136.1086 +   zlib error is Z_MEM_ERROR).  */
136.1087 +
136.1088 +ZEXTERN gzFile ZEXPORT gzdopen  OF((int fd, const char *mode));
136.1089 +/*
136.1090 +     gzdopen() associates a gzFile with the file descriptor fd.  File
136.1091 +   descriptors are obtained from calls like open, dup, creat, pipe or
136.1092 +   fileno (in the file has been previously opened with fopen).
136.1093 +   The mode parameter is as in gzopen.
136.1094 +     The next call of gzclose on the returned gzFile will also close the
136.1095 +   file descriptor fd, just like fclose(fdopen(fd), mode) closes the file
136.1096 +   descriptor fd. If you want to keep fd open, use gzdopen(dup(fd), mode).
136.1097 +     gzdopen returns NULL if there was insufficient memory to allocate
136.1098 +   the (de)compression state.
136.1099 +*/
136.1100 +
136.1101 +ZEXTERN int ZEXPORT gzsetparams OF((gzFile file, int level, int strategy));
136.1102 +/*
136.1103 +     Dynamically update the compression level or strategy. See the description
136.1104 +   of deflateInit2 for the meaning of these parameters.
136.1105 +     gzsetparams returns Z_OK if success, or Z_STREAM_ERROR if the file was not
136.1106 +   opened for writing.
136.1107 +*/
136.1108 +
136.1109 +ZEXTERN int ZEXPORT    gzread  OF((gzFile file, voidp buf, unsigned len));
136.1110 +/*
136.1111 +     Reads the given number of uncompressed bytes from the compressed file.
136.1112 +   If the input file was not in gzip format, gzread copies the given number
136.1113 +   of bytes into the buffer.
136.1114 +     gzread returns the number of uncompressed bytes actually read (0 for
136.1115 +   end of file, -1 for error). */
136.1116 +
136.1117 +ZEXTERN int ZEXPORT    gzwrite OF((gzFile file,
136.1118 +                                   voidpc buf, unsigned len));
136.1119 +/*
136.1120 +     Writes the given number of uncompressed bytes into the compressed file.
136.1121 +   gzwrite returns the number of uncompressed bytes actually written
136.1122 +   (0 in case of error).
136.1123 +*/
136.1124 +
136.1125 +ZEXTERN int ZEXPORTVA   gzprintf OF((gzFile file, const char *format, ...));
136.1126 +/*
136.1127 +     Converts, formats, and writes the args to the compressed file under
136.1128 +   control of the format string, as in fprintf. gzprintf returns the number of
136.1129 +   uncompressed bytes actually written (0 in case of error).  The number of
136.1130 +   uncompressed bytes written is limited to 4095. The caller should assure that
136.1131 +   this limit is not exceeded. If it is exceeded, then gzprintf() will return
136.1132 +   return an error (0) with nothing written. In this case, there may also be a
136.1133 +   buffer overflow with unpredictable consequences, which is possible only if
136.1134 +   zlib was compiled with the insecure functions sprintf() or vsprintf()
136.1135 +   because the secure snprintf() or vsnprintf() functions were not available.
136.1136 +*/
136.1137 +
136.1138 +ZEXTERN int ZEXPORT gzputs OF((gzFile file, const char *s));
136.1139 +/*
136.1140 +      Writes the given null-terminated string to the compressed file, excluding
136.1141 +   the terminating null character.
136.1142 +      gzputs returns the number of characters written, or -1 in case of error.
136.1143 +*/
136.1144 +
136.1145 +ZEXTERN char * ZEXPORT gzgets OF((gzFile file, char *buf, int len));
136.1146 +/*
136.1147 +      Reads bytes from the compressed file until len-1 characters are read, or
136.1148 +   a newline character is read and transferred to buf, or an end-of-file
136.1149 +   condition is encountered.  The string is then terminated with a null
136.1150 +   character.
136.1151 +      gzgets returns buf, or Z_NULL in case of error.
136.1152 +*/
136.1153 +
136.1154 +ZEXTERN int ZEXPORT    gzputc OF((gzFile file, int c));
136.1155 +/*
136.1156 +      Writes c, converted to an unsigned char, into the compressed file.
136.1157 +   gzputc returns the value that was written, or -1 in case of error.
136.1158 +*/
136.1159 +
136.1160 +ZEXTERN int ZEXPORT    gzgetc OF((gzFile file));
136.1161 +/*
136.1162 +      Reads one byte from the compressed file. gzgetc returns this byte
136.1163 +   or -1 in case of end of file or error.
136.1164 +*/
136.1165 +
136.1166 +ZEXTERN int ZEXPORT    gzungetc OF((int c, gzFile file));
136.1167 +/*
136.1168 +      Push one character back onto the stream to be read again later.
136.1169 +   Only one character of push-back is allowed.  gzungetc() returns the
136.1170 +   character pushed, or -1 on failure.  gzungetc() will fail if a
136.1171 +   character has been pushed but not read yet, or if c is -1. The pushed
136.1172 +   character will be discarded if the stream is repositioned with gzseek()
136.1173 +   or gzrewind().
136.1174 +*/
136.1175 +
136.1176 +ZEXTERN int ZEXPORT    gzflush OF((gzFile file, int flush));
136.1177 +/*
136.1178 +     Flushes all pending output into the compressed file. The parameter
136.1179 +   flush is as in the deflate() function. The return value is the zlib
136.1180 +   error number (see function gzerror below). gzflush returns Z_OK if
136.1181 +   the flush parameter is Z_FINISH and all output could be flushed.
136.1182 +     gzflush should be called only when strictly necessary because it can
136.1183 +   degrade compression.
136.1184 +*/
136.1185 +
136.1186 +ZEXTERN z_off_t ZEXPORT    gzseek OF((gzFile file,
136.1187 +                                      z_off_t offset, int whence));
136.1188 +/*
136.1189 +      Sets the starting position for the next gzread or gzwrite on the
136.1190 +   given compressed file. The offset represents a number of bytes in the
136.1191 +   uncompressed data stream. The whence parameter is defined as in lseek(2);
136.1192 +   the value SEEK_END is not supported.
136.1193 +     If the file is opened for reading, this function is emulated but can be
136.1194 +   extremely slow. If the file is opened for writing, only forward seeks are
136.1195 +   supported; gzseek then compresses a sequence of zeroes up to the new
136.1196 +   starting position.
136.1197 +
136.1198 +      gzseek returns the resulting offset location as measured in bytes from
136.1199 +   the beginning of the uncompressed stream, or -1 in case of error, in
136.1200 +   particular if the file is opened for writing and the new starting position
136.1201 +   would be before the current position.
136.1202 +*/
136.1203 +
136.1204 +ZEXTERN int ZEXPORT    gzrewind OF((gzFile file));
136.1205 +/*
136.1206 +     Rewinds the given file. This function is supported only for reading.
136.1207 +
136.1208 +   gzrewind(file) is equivalent to (int)gzseek(file, 0L, SEEK_SET)
136.1209 +*/
136.1210 +
136.1211 +ZEXTERN z_off_t ZEXPORT    gztell OF((gzFile file));
136.1212 +/*
136.1213 +     Returns the starting position for the next gzread or gzwrite on the
136.1214 +   given compressed file. This position represents a number of bytes in the
136.1215 +   uncompressed data stream.
136.1216 +
136.1217 +   gztell(file) is equivalent to gzseek(file, 0L, SEEK_CUR)
136.1218 +*/
136.1219 +
136.1220 +ZEXTERN int ZEXPORT gzeof OF((gzFile file));
136.1221 +/*
136.1222 +     Returns 1 when EOF has previously been detected reading the given
136.1223 +   input stream, otherwise zero.
136.1224 +*/
136.1225 +
136.1226 +ZEXTERN int ZEXPORT gzdirect OF((gzFile file));
136.1227 +/*
136.1228 +     Returns 1 if file is being read directly without decompression, otherwise
136.1229 +   zero.
136.1230 +*/
136.1231 +
136.1232 +ZEXTERN int ZEXPORT    gzclose OF((gzFile file));
136.1233 +/*
136.1234 +     Flushes all pending output if necessary, closes the compressed file
136.1235 +   and deallocates all the (de)compression state. The return value is the zlib
136.1236 +   error number (see function gzerror below).
136.1237 +*/
136.1238 +
136.1239 +ZEXTERN const char * ZEXPORT gzerror OF((gzFile file, int *errnum));
136.1240 +/*
136.1241 +     Returns the error message for the last error which occurred on the
136.1242 +   given compressed file. errnum is set to zlib error number. If an
136.1243 +   error occurred in the file system and not in the compression library,
136.1244 +   errnum is set to Z_ERRNO and the application may consult errno
136.1245 +   to get the exact error code.
136.1246 +*/
136.1247 +
136.1248 +ZEXTERN void ZEXPORT gzclearerr OF((gzFile file));
136.1249 +/*
136.1250 +     Clears the error and end-of-file flags for file. This is analogous to the
136.1251 +   clearerr() function in stdio. This is useful for continuing to read a gzip
136.1252 +   file that is being written concurrently.
136.1253 +*/
136.1254 +
136.1255 +                        /* checksum functions */
136.1256 +
136.1257 +/*
136.1258 +     These functions are not related to compression but are exported
136.1259 +   anyway because they might be useful in applications using the
136.1260 +   compression library.
136.1261 +*/
136.1262 +
136.1263 +ZEXTERN uLong ZEXPORT adler32 OF((uLong adler, const Bytef *buf, uInt len));
136.1264 +/*
136.1265 +     Update a running Adler-32 checksum with the bytes buf[0..len-1] and
136.1266 +   return the updated checksum. If buf is NULL, this function returns
136.1267 +   the required initial value for the checksum.
136.1268 +   An Adler-32 checksum is almost as reliable as a CRC32 but can be computed
136.1269 +   much faster. Usage example:
136.1270 +
136.1271 +     uLong adler = adler32(0L, Z_NULL, 0);
136.1272 +
136.1273 +     while (read_buffer(buffer, length) != EOF) {
136.1274 +       adler = adler32(adler, buffer, length);
136.1275 +     }
136.1276 +     if (adler != original_adler) error();
136.1277 +*/
136.1278 +
136.1279 +ZEXTERN uLong ZEXPORT adler32_combine OF((uLong adler1, uLong adler2,
136.1280 +                                          z_off_t len2));
136.1281 +/*
136.1282 +     Combine two Adler-32 checksums into one.  For two sequences of bytes, seq1
136.1283 +   and seq2 with lengths len1 and len2, Adler-32 checksums were calculated for
136.1284 +   each, adler1 and adler2.  adler32_combine() returns the Adler-32 checksum of
136.1285 +   seq1 and seq2 concatenated, requiring only adler1, adler2, and len2.
136.1286 +*/
136.1287 +
136.1288 +ZEXTERN uLong ZEXPORT crc32   OF((uLong crc, const Bytef *buf, uInt len));
136.1289 +/*
136.1290 +     Update a running CRC-32 with the bytes buf[0..len-1] and return the
136.1291 +   updated CRC-32. If buf is NULL, this function returns the required initial
136.1292 +   value for the for the crc. Pre- and post-conditioning (one's complement) is
136.1293 +   performed within this function so it shouldn't be done by the application.
136.1294 +   Usage example:
136.1295 +
136.1296 +     uLong crc = crc32(0L, Z_NULL, 0);
136.1297 +
136.1298 +     while (read_buffer(buffer, length) != EOF) {
136.1299 +       crc = crc32(crc, buffer, length);
136.1300 +     }
136.1301 +     if (crc != original_crc) error();
136.1302 +*/
136.1303 +
136.1304 +ZEXTERN uLong ZEXPORT crc32_combine OF((uLong crc1, uLong crc2, z_off_t len2));
136.1305 +
136.1306 +/*
136.1307 +     Combine two CRC-32 check values into one.  For two sequences of bytes,
136.1308 +   seq1 and seq2 with lengths len1 and len2, CRC-32 check values were
136.1309 +   calculated for each, crc1 and crc2.  crc32_combine() returns the CRC-32
136.1310 +   check value of seq1 and seq2 concatenated, requiring only crc1, crc2, and
136.1311 +   len2.
136.1312 +*/
136.1313 +
136.1314 +
136.1315 +                        /* various hacks, don't look :) */
136.1316 +
136.1317 +/* deflateInit and inflateInit are macros to allow checking the zlib version
136.1318 + * and the compiler's view of z_stream:
136.1319 + */
136.1320 +ZEXTERN int ZEXPORT deflateInit_ OF((z_streamp strm, int level,
136.1321 +                                     const char *version, int stream_size));
136.1322 +ZEXTERN int ZEXPORT inflateInit_ OF((z_streamp strm,
136.1323 +                                     const char *version, int stream_size));
136.1324 +ZEXTERN int ZEXPORT deflateInit2_ OF((z_streamp strm, int  level, int  method,
136.1325 +                                      int windowBits, int memLevel,
136.1326 +                                      int strategy, const char *version,
136.1327 +                                      int stream_size));
136.1328 +ZEXTERN int ZEXPORT inflateInit2_ OF((z_streamp strm, int  windowBits,
136.1329 +                                      const char *version, int stream_size));
136.1330 +ZEXTERN int ZEXPORT inflateBackInit_ OF((z_streamp strm, int windowBits,
136.1331 +                                         unsigned char FAR *window,
136.1332 +                                         const char *version,
136.1333 +                                         int stream_size));
136.1334 +#define deflateInit(strm, level) \
136.1335 +        deflateInit_((strm), (level),       ZLIB_VERSION, sizeof(z_stream))
136.1336 +#define inflateInit(strm) \
136.1337 +        inflateInit_((strm),                ZLIB_VERSION, sizeof(z_stream))
136.1338 +#define deflateInit2(strm, level, method, windowBits, memLevel, strategy) \
136.1339 +        deflateInit2_((strm),(level),(method),(windowBits),(memLevel),\
136.1340 +                      (strategy),           ZLIB_VERSION, sizeof(z_stream))
136.1341 +#define inflateInit2(strm, windowBits) \
136.1342 +        inflateInit2_((strm), (windowBits), ZLIB_VERSION, sizeof(z_stream))
136.1343 +#define inflateBackInit(strm, windowBits, window) \
136.1344 +        inflateBackInit_((strm), (windowBits), (window), \
136.1345 +        ZLIB_VERSION, sizeof(z_stream))
136.1346 +
136.1347 +
136.1348 +#if !defined(ZUTIL_H) && !defined(NO_DUMMY_DECL)
136.1349 +    struct internal_state {int dummy;}; /* hack for buggy compilers */
136.1350 +#endif
136.1351 +
136.1352 +ZEXTERN const char   * ZEXPORT zError           OF((int));
136.1353 +ZEXTERN int            ZEXPORT inflateSyncPoint OF((z_streamp z));
136.1354 +ZEXTERN const uLongf * ZEXPORT get_crc_table    OF((void));
136.1355 +
136.1356 +#ifdef __cplusplus
136.1357 +}
136.1358 +#endif
136.1359 +
136.1360 +#endif /* ZLIB_H */
   137.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   137.2 +++ b/libs/zlib/zutil.c	Sat Sep 19 05:51:51 2015 +0300
   137.3 @@ -0,0 +1,318 @@
   137.4 +/* zutil.c -- target dependent utility functions for the compression library
   137.5 + * Copyright (C) 1995-2005 Jean-loup Gailly.
   137.6 + * For conditions of distribution and use, see copyright notice in zlib.h
   137.7 + */
   137.8 +
   137.9 +/* @(#) $Id$ */
  137.10 +
  137.11 +#include "zutil.h"
  137.12 +
  137.13 +#ifndef NO_DUMMY_DECL
  137.14 +struct internal_state      {int dummy;}; /* for buggy compilers */
  137.15 +#endif
  137.16 +
  137.17 +const char * const z_errmsg[10] = {
  137.18 +"need dictionary",     /* Z_NEED_DICT       2  */
  137.19 +"stream end",          /* Z_STREAM_END      1  */
  137.20 +"",                    /* Z_OK              0  */
  137.21 +"file error",          /* Z_ERRNO         (-1) */
  137.22 +"stream error",        /* Z_STREAM_ERROR  (-2) */
  137.23 +"data error",          /* Z_DATA_ERROR    (-3) */
  137.24 +"insufficient memory", /* Z_MEM_ERROR     (-4) */
  137.25 +"buffer error",        /* Z_BUF_ERROR     (-5) */
  137.26 +"incompatible version",/* Z_VERSION_ERROR (-6) */
  137.27 +""};
  137.28 +
  137.29 +
  137.30 +const char * ZEXPORT zlibVersion()
  137.31 +{
  137.32 +    return ZLIB_VERSION;
  137.33 +}
  137.34 +
  137.35 +uLong ZEXPORT zlibCompileFlags()
  137.36 +{
  137.37 +    uLong flags;
  137.38 +
  137.39 +    flags = 0;
  137.40 +    switch (sizeof(uInt)) {
  137.41 +    case 2:     break;
  137.42 +    case 4:     flags += 1;     break;
  137.43 +    case 8:     flags += 2;     break;
  137.44 +    default:    flags += 3;
  137.45 +    }
  137.46 +    switch (sizeof(uLong)) {
  137.47 +    case 2:     break;
  137.48 +    case 4:     flags += 1 << 2;        break;
  137.49 +    case 8:     flags += 2 << 2;        break;
  137.50 +    default:    flags += 3 << 2;
  137.51 +    }
  137.52 +    switch (sizeof(voidpf)) {
  137.53 +    case 2:     break;
  137.54 +    case 4:     flags += 1 << 4;        break;
  137.55 +    case 8:     flags += 2 << 4;        break;
  137.56 +    default:    flags += 3 << 4;
  137.57 +    }
  137.58 +    switch (sizeof(z_off_t)) {
  137.59 +    case 2:     break;
  137.60 +    case 4:     flags += 1 << 6;        break;
  137.61 +    case 8:     flags += 2 << 6;        break;
  137.62 +    default:    flags += 3 << 6;
  137.63 +    }
  137.64 +#ifdef DEBUG
  137.65 +    flags += 1 << 8;
  137.66 +#endif
  137.67 +#if defined(ASMV) || defined(ASMINF)
  137.68 +    flags += 1 << 9;
  137.69 +#endif
  137.70 +#ifdef ZLIB_WINAPI
  137.71 +    flags += 1 << 10;
  137.72 +#endif
  137.73 +#ifdef BUILDFIXED
  137.74 +    flags += 1 << 12;
  137.75 +#endif
  137.76 +#ifdef DYNAMIC_CRC_TABLE
  137.77 +    flags += 1 << 13;
  137.78 +#endif
  137.79 +#ifdef NO_GZCOMPRESS
  137.80 +    flags += 1L << 16;
  137.81 +#endif
  137.82 +#ifdef NO_GZIP
  137.83 +    flags += 1L << 17;
  137.84 +#endif
  137.85 +#ifdef PKZIP_BUG_WORKAROUND
  137.86 +    flags += 1L << 20;
  137.87 +#endif
  137.88 +#ifdef FASTEST
  137.89 +    flags += 1L << 21;
  137.90 +#endif
  137.91 +#ifdef STDC
  137.92 +#  ifdef NO_vsnprintf
  137.93 +        flags += 1L << 25;
  137.94 +#    ifdef HAS_vsprintf_void
  137.95 +        flags += 1L << 26;
  137.96 +#    endif
  137.97 +#  else
  137.98 +#    ifdef HAS_vsnprintf_void
  137.99 +        flags += 1L << 26;
 137.100 +#    endif
 137.101 +#  endif
 137.102 +#else
 137.103 +        flags += 1L << 24;
 137.104 +#  ifdef NO_snprintf
 137.105 +        flags += 1L << 25;
 137.106 +#    ifdef HAS_sprintf_void
 137.107 +        flags += 1L << 26;
 137.108 +#    endif
 137.109 +#  else
 137.110 +#    ifdef HAS_snprintf_void
 137.111 +        flags += 1L << 26;
 137.112 +#    endif
 137.113 +#  endif
 137.114 +#endif
 137.115 +    return flags;
 137.116 +}
 137.117 +
 137.118 +#ifdef DEBUG
 137.119 +
 137.120 +#  ifndef verbose
 137.121 +#    define verbose 0
 137.122 +#  endif
 137.123 +int z_verbose = verbose;
 137.124 +
 137.125 +void z_error (m)
 137.126 +    char *m;
 137.127 +{
 137.128 +    fprintf(stderr, "%s\n", m);
 137.129 +    exit(1);
 137.130 +}
 137.131 +#endif
 137.132 +
 137.133 +/* exported to allow conversion of error code to string for compress() and
 137.134 + * uncompress()
 137.135 + */
 137.136 +const char * ZEXPORT zError(err)
 137.137 +    int err;
 137.138 +{
 137.139 +    return ERR_MSG(err);
 137.140 +}
 137.141 +
 137.142 +#if defined(_WIN32_WCE)
 137.143 +    /* The Microsoft C Run-Time Library for Windows CE doesn't have
 137.144 +     * errno.  We define it as a global variable to simplify porting.
 137.145 +     * Its value is always 0 and should not be used.
 137.146 +     */
 137.147 +    int errno = 0;
 137.148 +#endif
 137.149 +
 137.150 +#ifndef HAVE_MEMCPY
 137.151 +
 137.152 +void zmemcpy(dest, source, len)
 137.153 +    Bytef* dest;
 137.154 +    const Bytef* source;
 137.155 +    uInt  len;
 137.156 +{
 137.157 +    if (len == 0) return;
 137.158 +    do {
 137.159 +        *dest++ = *source++; /* ??? to be unrolled */
 137.160 +    } while (--len != 0);
 137.161 +}
 137.162 +
 137.163 +int zmemcmp(s1, s2, len)
 137.164 +    const Bytef* s1;
 137.165 +    const Bytef* s2;
 137.166 +    uInt  len;
 137.167 +{
 137.168 +    uInt j;
 137.169 +
 137.170 +    for (j = 0; j < len; j++) {
 137.171 +        if (s1[j] != s2[j]) return 2*(s1[j] > s2[j])-1;
 137.172 +    }
 137.173 +    return 0;
 137.174 +}
 137.175 +
 137.176 +void zmemzero(dest, len)
 137.177 +    Bytef* dest;
 137.178 +    uInt  len;
 137.179 +{
 137.180 +    if (len == 0) return;
 137.181 +    do {
 137.182 +        *dest++ = 0;  /* ??? to be unrolled */
 137.183 +    } while (--len != 0);
 137.184 +}
 137.185 +#endif
 137.186 +
 137.187 +
 137.188 +#ifdef SYS16BIT
 137.189 +
 137.190 +#ifdef __TURBOC__
 137.191 +/* Turbo C in 16-bit mode */
 137.192 +
 137.193 +#  define MY_ZCALLOC
 137.194 +
 137.195 +/* Turbo C malloc() does not allow dynamic allocation of 64K bytes
 137.196 + * and farmalloc(64K) returns a pointer with an offset of 8, so we
 137.197 + * must fix the pointer. Warning: the pointer must be put back to its
 137.198 + * original form in order to free it, use zcfree().
 137.199 + */
 137.200 +
 137.201 +#define MAX_PTR 10
 137.202 +/* 10*64K = 640K */
 137.203 +
 137.204 +local int next_ptr = 0;
 137.205 +
 137.206 +typedef struct ptr_table_s {
 137.207 +    voidpf org_ptr;
 137.208 +    voidpf new_ptr;
 137.209 +} ptr_table;
 137.210 +
 137.211 +local ptr_table table[MAX_PTR];
 137.212 +/* This table is used to remember the original form of pointers
 137.213 + * to large buffers (64K). Such pointers are normalized with a zero offset.
 137.214 + * Since MSDOS is not a preemptive multitasking OS, this table is not
 137.215 + * protected from concurrent access. This hack doesn't work anyway on
 137.216 + * a protected system like OS/2. Use Microsoft C instead.
 137.217 + */
 137.218 +
 137.219 +voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
 137.220 +{
 137.221 +    voidpf buf = opaque; /* just to make some compilers happy */
 137.222 +    ulg bsize = (ulg)items*size;
 137.223 +
 137.224 +    /* If we allocate less than 65520 bytes, we assume that farmalloc
 137.225 +     * will return a usable pointer which doesn't have to be normalized.
 137.226 +     */
 137.227 +    if (bsize < 65520L) {
 137.228 +        buf = farmalloc(bsize);
 137.229 +        if (*(ush*)&buf != 0) return buf;
 137.230 +    } else {
 137.231 +        buf = farmalloc(bsize + 16L);
 137.232 +    }
 137.233 +    if (buf == NULL || next_ptr >= MAX_PTR) return NULL;
 137.234 +    table[next_ptr].org_ptr = buf;
 137.235 +
 137.236 +    /* Normalize the pointer to seg:0 */
 137.237 +    *((ush*)&buf+1) += ((ush)((uch*)buf-0) + 15) >> 4;
 137.238 +    *(ush*)&buf = 0;
 137.239 +    table[next_ptr++].new_ptr = buf;
 137.240 +    return buf;
 137.241 +}
 137.242 +
 137.243 +void  zcfree (voidpf opaque, voidpf ptr)
 137.244 +{
 137.245 +    int n;
 137.246 +    if (*(ush*)&ptr != 0) { /* object < 64K */
 137.247 +        farfree(ptr);
 137.248 +        return;
 137.249 +    }
 137.250 +    /* Find the original pointer */
 137.251 +    for (n = 0; n < next_ptr; n++) {
 137.252 +        if (ptr != table[n].new_ptr) continue;
 137.253 +
 137.254 +        farfree(table[n].org_ptr);
 137.255 +        while (++n < next_ptr) {
 137.256 +            table[n-1] = table[n];
 137.257 +        }
 137.258 +        next_ptr--;
 137.259 +        return;
 137.260 +    }
 137.261 +    ptr = opaque; /* just to make some compilers happy */
 137.262 +    Assert(0, "zcfree: ptr not found");
 137.263 +}
 137.264 +
 137.265 +#endif /* __TURBOC__ */
 137.266 +
 137.267 +
 137.268 +#ifdef M_I86
 137.269 +/* Microsoft C in 16-bit mode */
 137.270 +
 137.271 +#  define MY_ZCALLOC
 137.272 +
 137.273 +#if (!defined(_MSC_VER) || (_MSC_VER <= 600))
 137.274 +#  define _halloc  halloc
 137.275 +#  define _hfree   hfree
 137.276 +#endif
 137.277 +
 137.278 +voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
 137.279 +{
 137.280 +    if (opaque) opaque = 0; /* to make compiler happy */
 137.281 +    return _halloc((long)items, size);
 137.282 +}
 137.283 +
 137.284 +void  zcfree (voidpf opaque, voidpf ptr)
 137.285 +{
 137.286 +    if (opaque) opaque = 0; /* to make compiler happy */
 137.287 +    _hfree(ptr);
 137.288 +}
 137.289 +
 137.290 +#endif /* M_I86 */
 137.291 +
 137.292 +#endif /* SYS16BIT */
 137.293 +
 137.294 +
 137.295 +#ifndef MY_ZCALLOC /* Any system without a special alloc function */
 137.296 +
 137.297 +#ifndef STDC
 137.298 +extern voidp  malloc OF((uInt size));
 137.299 +extern voidp  calloc OF((uInt items, uInt size));
 137.300 +extern void   free   OF((voidpf ptr));
 137.301 +#endif
 137.302 +
 137.303 +voidpf zcalloc (opaque, items, size)
 137.304 +    voidpf opaque;
 137.305 +    unsigned items;
 137.306 +    unsigned size;
 137.307 +{
 137.308 +    if (opaque) items += size - size; /* make compiler happy */
 137.309 +    return sizeof(uInt) > 2 ? (voidpf)malloc(items * size) :
 137.310 +                              (voidpf)calloc(items, size);
 137.311 +}
 137.312 +
 137.313 +void  zcfree (opaque, ptr)
 137.314 +    voidpf opaque;
 137.315 +    voidpf ptr;
 137.316 +{
 137.317 +    free(ptr);
 137.318 +    if (opaque) return; /* make compiler happy */
 137.319 +}
 137.320 +
 137.321 +#endif /* MY_ZCALLOC */
   138.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   138.2 +++ b/libs/zlib/zutil.h	Sat Sep 19 05:51:51 2015 +0300
   138.3 @@ -0,0 +1,269 @@
   138.4 +/* zutil.h -- internal interface and configuration of the compression library
   138.5 + * Copyright (C) 1995-2005 Jean-loup Gailly.
   138.6 + * For conditions of distribution and use, see copyright notice in zlib.h
   138.7 + */
   138.8 +
   138.9 +/* WARNING: this file should *not* be used by applications. It is
  138.10 +   part of the implementation of the compression library and is
  138.11 +   subject to change. Applications should only use zlib.h.
  138.12 + */
  138.13 +
  138.14 +/* @(#) $Id$ */
  138.15 +
  138.16 +#ifndef ZUTIL_H
  138.17 +#define ZUTIL_H
  138.18 +
  138.19 +#define ZLIB_INTERNAL
  138.20 +#include "zlib.h"
  138.21 +
  138.22 +#ifdef STDC
  138.23 +#  ifndef _WIN32_WCE
  138.24 +#    include <stddef.h>
  138.25 +#  endif
  138.26 +#  include <string.h>
  138.27 +#  include <stdlib.h>
  138.28 +#endif
  138.29 +#ifdef NO_ERRNO_H
  138.30 +#   ifdef _WIN32_WCE
  138.31 +      /* The Microsoft C Run-Time Library for Windows CE doesn't have
  138.32 +       * errno.  We define it as a global variable to simplify porting.
  138.33 +       * Its value is always 0 and should not be used.  We rename it to
  138.34 +       * avoid conflict with other libraries that use the same workaround.
  138.35 +       */
  138.36 +#     define errno z_errno
  138.37 +#   endif
  138.38 +    extern int errno;
  138.39 +#else
  138.40 +#  ifndef _WIN32_WCE
  138.41 +#    include <errno.h>
  138.42 +#  endif
  138.43 +#endif
  138.44 +
  138.45 +#ifndef local
  138.46 +#  define local static
  138.47 +#endif
  138.48 +/* compile with -Dlocal if your debugger can't find static symbols */
  138.49 +
  138.50 +typedef unsigned char  uch;
  138.51 +typedef uch FAR uchf;
  138.52 +typedef unsigned short ush;
  138.53 +typedef ush FAR ushf;
  138.54 +typedef unsigned long  ulg;
  138.55 +
  138.56 +extern const char * const z_errmsg[10]; /* indexed by 2-zlib_error */
  138.57 +/* (size given to avoid silly warnings with Visual C++) */
  138.58 +
  138.59 +#define ERR_MSG(err) z_errmsg[Z_NEED_DICT-(err)]
  138.60 +
  138.61 +#define ERR_RETURN(strm,err) \
  138.62 +  return (strm->msg = (char*)ERR_MSG(err), (err))
  138.63 +/* To be used only when the state is known to be valid */
  138.64 +
  138.65 +        /* common constants */
  138.66 +
  138.67 +#ifndef DEF_WBITS
  138.68 +#  define DEF_WBITS MAX_WBITS
  138.69 +#endif
  138.70 +/* default windowBits for decompression. MAX_WBITS is for compression only */
  138.71 +
  138.72 +#if MAX_MEM_LEVEL >= 8
  138.73 +#  define DEF_MEM_LEVEL 8
  138.74 +#else
  138.75 +#  define DEF_MEM_LEVEL  MAX_MEM_LEVEL
  138.76 +#endif
  138.77 +/* default memLevel */
  138.78 +
  138.79 +#define STORED_BLOCK 0
  138.80 +#define STATIC_TREES 1
  138.81 +#define DYN_TREES    2
  138.82 +/* The three kinds of block type */
  138.83 +
  138.84 +#define MIN_MATCH  3
  138.85 +#define MAX_MATCH  258
  138.86 +/* The minimum and maximum match lengths */
  138.87 +
  138.88 +#define PRESET_DICT 0x20 /* preset dictionary flag in zlib header */
  138.89 +
  138.90 +        /* target dependencies */
  138.91 +
  138.92 +#if defined(MSDOS) || (defined(WINDOWS) && !defined(WIN32))
  138.93 +#  define OS_CODE  0x00
  138.94 +#  if defined(__TURBOC__) || defined(__BORLANDC__)
  138.95 +#    if(__STDC__ == 1) && (defined(__LARGE__) || defined(__COMPACT__))
  138.96 +       /* Allow compilation with ANSI keywords only enabled */
  138.97 +       void _Cdecl farfree( void *block );
  138.98 +       void *_Cdecl farmalloc( unsigned long nbytes );
  138.99 +#    else
 138.100 +#      include <alloc.h>
 138.101 +#    endif
 138.102 +#  else /* MSC or DJGPP */
 138.103 +#    include <malloc.h>
 138.104 +#  endif
 138.105 +#endif
 138.106 +
 138.107 +#ifdef AMIGA
 138.108 +#  define OS_CODE  0x01
 138.109 +#endif
 138.110 +
 138.111 +#if defined(VAXC) || defined(VMS)
 138.112 +#  define OS_CODE  0x02
 138.113 +#  define F_OPEN(name, mode) \
 138.114 +     fopen((name), (mode), "mbc=60", "ctx=stm", "rfm=fix", "mrs=512")
 138.115 +#endif
 138.116 +
 138.117 +#if defined(ATARI) || defined(atarist)
 138.118 +#  define OS_CODE  0x05
 138.119 +#endif
 138.120 +
 138.121 +#ifdef OS2
 138.122 +#  define OS_CODE  0x06
 138.123 +#  ifdef M_I86
 138.124 +     #include <malloc.h>
 138.125 +#  endif
 138.126 +#endif
 138.127 +
 138.128 +#if defined(MACOS) || defined(TARGET_OS_MAC)
 138.129 +#  define OS_CODE  0x07
 138.130 +#  if defined(__MWERKS__) && __dest_os != __be_os && __dest_os != __win32_os
 138.131 +#    include <unix.h> /* for fdopen */
 138.132 +#  else
 138.133 +#    ifndef fdopen
 138.134 +#      define fdopen(fd,mode) NULL /* No fdopen() */
 138.135 +#    endif
 138.136 +#  endif
 138.137 +#endif
 138.138 +
 138.139 +#ifdef TOPS20
 138.140 +#  define OS_CODE  0x0a
 138.141 +#endif
 138.142 +
 138.143 +#ifdef WIN32
 138.144 +#  ifndef __CYGWIN__  /* Cygwin is Unix, not Win32 */
 138.145 +#    define OS_CODE  0x0b
 138.146 +#  endif
 138.147 +#endif
 138.148 +
 138.149 +#ifdef __50SERIES /* Prime/PRIMOS */
 138.150 +#  define OS_CODE  0x0f
 138.151 +#endif
 138.152 +
 138.153 +#if defined(_BEOS_) || defined(RISCOS)
 138.154 +#  define fdopen(fd,mode) NULL /* No fdopen() */
 138.155 +#endif
 138.156 +
 138.157 +#if (defined(_MSC_VER) && (_MSC_VER > 600))
 138.158 +#  if defined(_WIN32_WCE)
 138.159 +#    define fdopen(fd,mode) NULL /* No fdopen() */
 138.160 +#    ifndef _PTRDIFF_T_DEFINED
 138.161 +       typedef int ptrdiff_t;
 138.162 +#      define _PTRDIFF_T_DEFINED
 138.163 +#    endif
 138.164 +#  else
 138.165 +#    define fdopen(fd,type)  _fdopen(fd,type)
 138.166 +#  endif
 138.167 +#endif
 138.168 +
 138.169 +        /* common defaults */
 138.170 +
 138.171 +#ifndef OS_CODE
 138.172 +#  define OS_CODE  0x03  /* assume Unix */
 138.173 +#endif
 138.174 +
 138.175 +#ifndef F_OPEN
 138.176 +#  define F_OPEN(name, mode) fopen((name), (mode))
 138.177 +#endif
 138.178 +
 138.179 +         /* functions */
 138.180 +
 138.181 +#if defined(STDC99) || (defined(__TURBOC__) && __TURBOC__ >= 0x550)
 138.182 +#  ifndef HAVE_VSNPRINTF
 138.183 +#    define HAVE_VSNPRINTF
 138.184 +#  endif
 138.185 +#endif
 138.186 +#if defined(__CYGWIN__)
 138.187 +#  ifndef HAVE_VSNPRINTF
 138.188 +#    define HAVE_VSNPRINTF
 138.189 +#  endif
 138.190 +#endif
 138.191 +#ifndef HAVE_VSNPRINTF
 138.192 +#  ifdef MSDOS
 138.193 +     /* vsnprintf may exist on some MS-DOS compilers (DJGPP?),
 138.194 +        but for now we just assume it doesn't. */
 138.195 +#    define NO_vsnprintf
 138.196 +#  endif
 138.197 +#  ifdef __TURBOC__
 138.198 +#    define NO_vsnprintf
 138.199 +#  endif
 138.200 +#  ifdef WIN32
 138.201 +     /* In Win32, vsnprintf is available as the "non-ANSI" _vsnprintf. */
 138.202 +#    if !defined(vsnprintf) && !defined(NO_vsnprintf)
 138.203 +#      define vsnprintf _vsnprintf
 138.204 +#    endif
 138.205 +#  endif
 138.206 +#  ifdef __SASC
 138.207 +#    define NO_vsnprintf
 138.208 +#  endif
 138.209 +#endif
 138.210 +#ifdef VMS
 138.211 +#  define NO_vsnprintf
 138.212 +#endif
 138.213 +
 138.214 +#if defined(pyr)
 138.215 +#  define NO_MEMCPY
 138.216 +#endif
 138.217 +#if defined(SMALL_MEDIUM) && !defined(_MSC_VER) && !defined(__SC__)
 138.218 + /* Use our own functions for small and medium model with MSC <= 5.0.
 138.219 +  * You may have to use the same strategy for Borland C (untested).
 138.220 +  * The __SC__ check is for Symantec.
 138.221 +  */
 138.222 +#  define NO_MEMCPY
 138.223 +#endif
 138.224 +#if defined(STDC) && !defined(HAVE_MEMCPY) && !defined(NO_MEMCPY)
 138.225 +#  define HAVE_MEMCPY
 138.226 +#endif
 138.227 +#ifdef HAVE_MEMCPY
 138.228 +#  ifdef SMALL_MEDIUM /* MSDOS small or medium model */
 138.229 +#    define zmemcpy _fmemcpy
 138.230 +#    define zmemcmp _fmemcmp
 138.231 +#    define zmemzero(dest, len) _fmemset(dest, 0, len)
 138.232 +#  else
 138.233 +#    define zmemcpy memcpy
 138.234 +#    define zmemcmp memcmp
 138.235 +#    define zmemzero(dest, len) memset(dest, 0, len)
 138.236 +#  endif
 138.237 +#else
 138.238 +   extern void zmemcpy  OF((Bytef* dest, const Bytef* source, uInt len));
 138.239 +   extern int  zmemcmp  OF((const Bytef* s1, const Bytef* s2, uInt len));
 138.240 +   extern void zmemzero OF((Bytef* dest, uInt len));
 138.241 +#endif
 138.242 +
 138.243 +/* Diagnostic functions */
 138.244 +#ifdef DEBUG
 138.245 +#  include <stdio.h>
 138.246 +   extern int z_verbose;
 138.247 +   extern void z_error    OF((char *m));
 138.248 +#  define Assert(cond,msg) {if(!(cond)) z_error(msg);}
 138.249 +#  define Trace(x) {if (z_verbose>=0) fprintf x ;}
 138.250 +#  define Tracev(x) {if (z_verbose>0) fprintf x ;}
 138.251 +#  define Tracevv(x) {if (z_verbose>1) fprintf x ;}
 138.252 +#  define Tracec(c,x) {if (z_verbose>0 && (c)) fprintf x ;}
 138.253 +#  define Tracecv(c,x) {if (z_verbose>1 && (c)) fprintf x ;}
 138.254 +#else
 138.255 +#  define Assert(cond,msg)
 138.256 +#  define Trace(x)
 138.257 +#  define Tracev(x)
 138.258 +#  define Tracevv(x)
 138.259 +#  define Tracec(c,x)
 138.260 +#  define Tracecv(c,x)
 138.261 +#endif
 138.262 +
 138.263 +
 138.264 +voidpf zcalloc OF((voidpf opaque, unsigned items, unsigned size));
 138.265 +void   zcfree  OF((voidpf opaque, voidpf ptr));
 138.266 +
 138.267 +#define ZALLOC(strm, items, size) \
 138.268 +           (*((strm)->zalloc))((strm)->opaque, (items), (size))
 138.269 +#define ZFREE(strm, addr)  (*((strm)->zfree))((strm)->opaque, (voidpf)(addr))
 138.270 +#define TRY_FREE(s, p) {if (p) ZFREE(s, p);}
 138.271 +
 138.272 +#endif /* ZUTIL_H */
   139.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   139.2 +++ b/sdr/test.p.glsl	Sat Sep 19 05:51:51 2015 +0300
   139.3 @@ -0,0 +1,22 @@
   139.4 +#ifdef GL_ES
   139.5 +precision mediump float;
   139.6 +#endif
   139.7 +
   139.8 +uniform sampler2D tex;
   139.9 +
  139.10 +varying vec3 vpos;
  139.11 +varying vec4 var_color;
  139.12 +varying vec2 var_texcoord;
  139.13 +
  139.14 +void main()
  139.15 +{
  139.16 +	vec4 fog_color = vec4(0.6, 0.6, 0.6, 1.0);
  139.17 +
  139.18 +	float fog = exp(-(0.2 * -vpos.z));
  139.19 +
  139.20 +	vec4 texel = texture2D(tex, var_texcoord);
  139.21 +	texel.w = 1.0;
  139.22 +
  139.23 +	vec4 col = var_color * texel;
  139.24 +	gl_FragColor = mix(fog_color, col, fog);
  139.25 +}
   140.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   140.2 +++ b/sdr/test.v.glsl	Sat Sep 19 05:51:51 2015 +0300
   140.3 @@ -0,0 +1,17 @@
   140.4 +uniform mat4 matrix_modelview, matrix_projection, matrix_texture;
   140.5 +
   140.6 +attribute vec4 attr_vertex, attr_color;
   140.7 +attribute vec2 attr_texcoord;
   140.8 +
   140.9 +varying vec3 vpos;
  140.10 +varying vec4 var_color;
  140.11 +varying vec2 var_texcoord;
  140.12 +
  140.13 +void main()
  140.14 +{
  140.15 +	mat4 mvp = matrix_projection * matrix_modelview;
  140.16 +	gl_Position = mvp * attr_vertex;
  140.17 +	vpos = (matrix_modelview * attr_vertex).xyz;
  140.18 +	var_color = attr_color;
  140.19 +	var_texcoord = (matrix_texture * vec4(attr_texcoord, 0.0, 1.0)).xy;
  140.20 +}
   141.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   141.2 +++ b/sdr/text.p.glsl	Sat Sep 19 05:51:51 2015 +0300
   141.3 @@ -0,0 +1,22 @@
   141.4 +#ifdef GL_ES
   141.5 +precision mediump float;
   141.6 +#endif
   141.7 +
   141.8 +uniform sampler2D tex;
   141.9 +
  141.10 +varying vec4 color;
  141.11 +varying vec3 vpos;
  141.12 +varying vec2 tc;
  141.13 +
  141.14 +void main()
  141.15 +{
  141.16 +	vec4 tcol = texture2D(tex, tc);
  141.17 +
  141.18 +	float fog = exp(-(0.2 * -vpos.z));
  141.19 +
  141.20 +	float alpha = fog * color.a;
  141.21 +
  141.22 +	vec4 color = vec4(1.0, 0.3, 0.2, tcol.x * alpha);
  141.23 +
  141.24 +	gl_FragColor = color;
  141.25 +}
   142.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   142.2 +++ b/sdr/text.v.glsl	Sat Sep 19 05:51:51 2015 +0300
   142.3 @@ -0,0 +1,21 @@
   142.4 +attribute vec4 attr_vertex, attr_texcoord, attr_color;
   142.5 +
   142.6 +uniform mat4 matrix_modelview, matrix_projection;
   142.7 +uniform float idx;
   142.8 +
   142.9 +varying vec4 color;
  142.10 +varying vec3 vpos;
  142.11 +varying vec2 tc;
  142.12 +
  142.13 +void main()
  142.14 +{
  142.15 +	mat4 mvp = matrix_projection * matrix_modelview;
  142.16 +	gl_Position = mvp * attr_vertex;
  142.17 +
  142.18 +	vpos = (matrix_modelview * attr_vertex).xyz;
  142.19 +
  142.20 +	float sz = 1.0 / 17.0;
  142.21 +	tc = vec2(attr_texcoord.x, (attr_texcoord.y + idx) * sz);
  142.22 +
  142.23 +	color = attr_color;
  142.24 +}
   143.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   143.2 +++ b/sdr/tunnel.p.glsl	Sat Sep 19 05:51:51 2015 +0300
   143.3 @@ -0,0 +1,51 @@
   143.4 +#ifdef GL_ES
   143.5 +precision mediump float;
   143.6 +#endif
   143.7 +
   143.8 +uniform sampler2D tex, tex_norm;
   143.9 +
  143.10 +varying vec3 vpos, normal, tangent;
  143.11 +varying vec4 color, tc;
  143.12 +varying vec3 lpos;
  143.13 +
  143.14 +void main()
  143.15 +{
  143.16 +	vec4 fog_color = vec4(0.0, 0.0, 0.0, 1.0);
  143.17 +	vec3 tcol = texture2D(tex, tc.xy * vec2(1.0, -1.0)).xyz;
  143.18 +	vec3 tnorm = texture2D(tex_norm, tc.xy * vec2(1.0, -1.0)).xyz;
  143.19 +
  143.20 +	float fog = exp(-(0.2 * -vpos.z));
  143.21 +	
  143.22 +	vec3 ldir = lpos - vpos;
  143.23 +	float ldist = length(ldir);
  143.24 +
  143.25 +	/* bring the light direction to tangent space */
  143.26 +	vec3 norm = normalize(normal);
  143.27 +	vec3 tang = normalize(tangent);
  143.28 +	vec3 bitan = cross(norm, tang);
  143.29 +
  143.30 +	mat3 tbn_xform = mat3(tang.x, bitan.x, norm.x,
  143.31 +			tang.y, bitan.y, norm.y,
  143.32 +			tang.z, bitan.z, norm.z);
  143.33 +
  143.34 +	vec3 l = normalize(tbn_xform * ldir);
  143.35 +
  143.36 +	/* grab normal from the normalmap */
  143.37 +	vec3 n = normalize(tnorm * 2.0 - 1.0);
  143.38 +
  143.39 +	float diffuse = max(dot(n, l), 0.0);
  143.40 +
  143.41 +	/* blinn-phong specular */
  143.42 +	vec3 v = normalize(-vpos);
  143.43 +	vec3 h = normalize(v + l);
  143.44 +	float specular = pow(max(dot(n, h), 0.0), 60.0);
  143.45 +
  143.46 +	const vec3 amb = vec3(0.02, 0.02, 0.02);
  143.47 +
  143.48 +	float att = clamp(1.0 / (0.5 * (ldist * ldist)), 0.0, 1.0);
  143.49 +
  143.50 +	vec3 dif = tcol * diffuse * att;
  143.51 +	vec3 spec = vec3(0.6, 0.6, 0.6) * specular * att;
  143.52 +
  143.53 +	gl_FragColor = vec4(fog * (amb + dif + spec), 1.0);
  143.54 +}
   144.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   144.2 +++ b/sdr/tunnel.v.glsl	Sat Sep 19 05:51:51 2015 +0300
   144.3 @@ -0,0 +1,28 @@
   144.4 +attribute vec4 attr_vertex, attr_color;
   144.5 +attribute vec2 attr_texcoord;
   144.6 +attribute vec4 attr_tangent;
   144.7 +attribute vec3 attr_normal;
   144.8 +
   144.9 +uniform mat4 matrix_modelview, matrix_projection, matrix_texture;
  144.10 +uniform mat3 matrix_normal;
  144.11 +uniform float t;
  144.12 +uniform vec4 light_pos;
  144.13 +
  144.14 +varying vec3 vpos, normal, tangent;
  144.15 +varying vec4 tc;
  144.16 +varying vec3 lpos;
  144.17 +
  144.18 +void main()
  144.19 +{
  144.20 +	mat4 mvp = matrix_projection * matrix_modelview;
  144.21 +	gl_Position = mvp * attr_vertex;
  144.22 +
  144.23 +	vpos = (matrix_modelview * attr_vertex).xyz;
  144.24 +
  144.25 +	normal = matrix_normal * attr_normal;
  144.26 +	tangent = matrix_normal * attr_tangent.xyz;
  144.27 +
  144.28 +	lpos = (matrix_modelview * light_pos).xyz;
  144.29 +
  144.30 +	tc = matrix_texture * vec4(attr_texcoord, 0.0, 1.0);
  144.31 +}
   145.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   145.2 +++ b/src/cam.c	Sat Sep 19 05:51:51 2015 +0300
   145.3 @@ -0,0 +1,188 @@
   145.4 +/*
   145.5 +Stereoscopic tunnel for iOS.
   145.6 +Copyright (C) 2011  John Tsiombikas <nuclear@member.fsf.org>
   145.7 +
   145.8 +This program is free software: you can redistribute it and/or modify
   145.9 +it under the terms of the GNU General Public License as published by
  145.10 +the Free Software Foundation, either version 3 of the License, or
  145.11 +(at your option) any later version.
  145.12 +
  145.13 +This program is distributed in the hope that it will be useful,
  145.14 +but WITHOUT ANY WARRANTY; without even the implied warranty of
  145.15 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  145.16 +GNU General Public License for more details.
  145.17 +
  145.18 +You should have received a copy of the GNU General Public License
  145.19 +along with this program.  If not, see <http://www.gnu.org/licenses/>.
  145.20 +*/
  145.21 +
  145.22 +#include <math.h>
  145.23 +#include "opengl.h"
  145.24 +#include "cam.h"
  145.25 +#include "sanegl.h"
  145.26 +
  145.27 +#define DEG_TO_RAD(x)	(M_PI * (x) / 180.0)
  145.28 +
  145.29 +typedef struct vec3 {
  145.30 +	float x, y, z;
  145.31 +} vec3_t;
  145.32 +
  145.33 +/* viewing parameters */
  145.34 +#define DEF_THETA	0
  145.35 +#define DEF_PHI		0
  145.36 +#define DEF_DIST	0
  145.37 +#define DEF_X		0
  145.38 +#define DEF_Y		0
  145.39 +#define DEF_Z		0
  145.40 +
  145.41 +static float cam_theta = DEF_THETA, cam_phi = DEF_PHI;
  145.42 +static float cam_dist = DEF_DIST;
  145.43 +static vec3_t cam_pos = {DEF_X, DEF_Y, DEF_Z};
  145.44 +static float cam_speed = 1.0;
  145.45 +
  145.46 +/* projection parameters */
  145.47 +#define DEF_VFOV	45.0
  145.48 +#define DEF_ASPECT	1.3333333
  145.49 +#define DEF_NEAR	1.0
  145.50 +#define DEF_FAR		1000.0
  145.51 +
  145.52 +static float vfov = DEF_VFOV;
  145.53 +static float aspect = DEF_ASPECT;
  145.54 +static float nearclip = DEF_NEAR, farclip = DEF_FAR;
  145.55 +
  145.56 +/* stereo parameters */
  145.57 +#define DEF_EYE_SEP		0.1
  145.58 +#define DEF_FOCUS_DIST	1.0
  145.59 +
  145.60 +static float eye_sep = DEF_EYE_SEP;
  145.61 +static float focus_dist = DEF_FOCUS_DIST;
  145.62 +
  145.63 +
  145.64 +static float rot_speed = 0.5;
  145.65 +static float zoom_speed = 0.1;
  145.66 +
  145.67 +void cam_reset(void)
  145.68 +{
  145.69 +	cam_reset_view();
  145.70 +	cam_reset_proj();
  145.71 +	cam_reset_stereo();
  145.72 +}
  145.73 +
  145.74 +void cam_reset_view(void)
  145.75 +{
  145.76 +	cam_theta = DEF_THETA;
  145.77 +	cam_phi = DEF_PHI;
  145.78 +	cam_dist = DEF_DIST;
  145.79 +	cam_pos.x = DEF_X;
  145.80 +	cam_pos.y = DEF_Y;
  145.81 +	cam_pos.z = DEF_Z;
  145.82 +}
  145.83 +
  145.84 +void cam_reset_proj(void)
  145.85 +{
  145.86 +	vfov = DEF_VFOV;
  145.87 +	aspect = DEF_ASPECT;
  145.88 +	nearclip = DEF_NEAR;
  145.89 +	farclip = DEF_FAR;
  145.90 +}
  145.91 +
  145.92 +void cam_reset_stereo(void)
  145.93 +{
  145.94 +	eye_sep = DEF_EYE_SEP;
  145.95 +	focus_dist = DEF_FOCUS_DIST;
  145.96 +}
  145.97 +
  145.98 +void cam_pan(int dx, int dy)
  145.99 +{
 145.100 +	float dxf = dx * cam_speed;
 145.101 +	float dyf = dy * cam_speed;
 145.102 +	float angle = -DEG_TO_RAD(cam_theta);
 145.103 +
 145.104 +	cam_pos.x += cos(angle) * dxf + sin(angle) * dyf;
 145.105 +	cam_pos.z += -sin(angle) * dxf + cos(angle) * dyf;
 145.106 +}
 145.107 +
 145.108 +void cam_height(int dh)
 145.109 +{
 145.110 +	cam_pos.y += dh * cam_speed;
 145.111 +}
 145.112 +
 145.113 +void cam_rotate(int dx, int dy)
 145.114 +{
 145.115 +	cam_theta += dx * rot_speed;
 145.116 +	cam_phi += dy * rot_speed;
 145.117 +
 145.118 +	if(cam_phi < -90) cam_phi = -90;
 145.119 +	if(cam_phi > 90) cam_phi = 90;
 145.120 +}
 145.121 +
 145.122 +void cam_zoom(int dz)
 145.123 +{
 145.124 +	cam_dist += dz * zoom_speed;
 145.125 +	if(cam_dist < 0.001) {
 145.126 +		cam_dist = 0.001;
 145.127 +	}
 145.128 +}
 145.129 +
 145.130 +void cam_clip(float n, float f)
 145.131 +{
 145.132 +	nearclip = n;
 145.133 +	farclip = f;
 145.134 +}
 145.135 +
 145.136 +void cam_fov(float f)
 145.137 +{
 145.138 +	vfov = f;
 145.139 +}
 145.140 +
 145.141 +void cam_aspect(float a)
 145.142 +{
 145.143 +	aspect = a;
 145.144 +}
 145.145 +
 145.146 +void cam_separation(float s)
 145.147 +{
 145.148 +	eye_sep = s;
 145.149 +}
 145.150 +
 145.151 +void cam_focus_dist(float d)
 145.152 +{
 145.153 +	focus_dist = d;
 145.154 +
 145.155 +	cam_separation(d / 30.0);
 145.156 +}
 145.157 +
 145.158 +void cam_view_matrix(void)
 145.159 +{
 145.160 +	cam_stereo_view_matrix(CAM_CENTER);
 145.161 +}
 145.162 +
 145.163 +void cam_stereo_view_matrix(int eye)
 145.164 +{
 145.165 +	static const float offs_sign[] = {0.0f, 0.5f, -0.5f};	/* center, left, right */
 145.166 +	float offs = eye_sep * offs_sign[eye];
 145.167 +
 145.168 +	gl_translatef(offs, 0, 0);
 145.169 +
 145.170 +	gl_translatef(0, 0, -cam_dist);
 145.171 +	gl_rotatef(cam_phi, 1, 0, 0);
 145.172 +	gl_rotatef(cam_theta, 0, 1, 0);
 145.173 +	gl_translatef(-cam_pos.x, -cam_pos.y, -cam_pos.z);
 145.174 +}
 145.175 +
 145.176 +void cam_proj_matrix(void)
 145.177 +{
 145.178 +	cam_stereo_proj_matrix(CAM_CENTER);
 145.179 +}
 145.180 +
 145.181 +void cam_stereo_proj_matrix(int eye)
 145.182 +{
 145.183 +	float vfov_rad = M_PI * vfov / 180.0;
 145.184 +	float top = nearclip * tan(vfov_rad * 0.5);
 145.185 +	float right = top * aspect;
 145.186 +
 145.187 +	static const float offs_sign[] = {0.0f, 1.0, -1.0};	/* center, left, right */
 145.188 +	float frust_shift = offs_sign[eye] * (eye_sep * 0.5 * nearclip / focus_dist);
 145.189 +
 145.190 +	gl_frustum(-right + frust_shift, right + frust_shift, -top, top, nearclip, farclip);
 145.191 +}
   146.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   146.2 +++ b/src/cam.h	Sat Sep 19 05:51:51 2015 +0300
   146.3 @@ -0,0 +1,61 @@
   146.4 +/*
   146.5 +Stereoscopic tunnel for iOS.
   146.6 +Copyright (C) 2011  John Tsiombikas <nuclear@member.fsf.org>
   146.7 +
   146.8 +This program is free software: you can redistribute it and/or modify
   146.9 +it under the terms of the GNU General Public License as published by
  146.10 +the Free Software Foundation, either version 3 of the License, or
  146.11 +(at your option) any later version.
  146.12 +
  146.13 +This program is distributed in the hope that it will be useful,
  146.14 +but WITHOUT ANY WARRANTY; without even the implied warranty of
  146.15 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  146.16 +GNU General Public License for more details.
  146.17 +
  146.18 +You should have received a copy of the GNU General Public License
  146.19 +along with this program.  If not, see <http://www.gnu.org/licenses/>.
  146.20 +*/
  146.21 +
  146.22 +#ifndef CAM_H_
  146.23 +#define CAM_H_
  146.24 +
  146.25 +enum {
  146.26 +	CAM_CENTER,
  146.27 +	CAM_LEFT,
  146.28 +	CAM_RIGHT
  146.29 +};
  146.30 +
  146.31 +/* reset to the initial state */
  146.32 +void cam_reset(void);			/* all */
  146.33 +void cam_reset_view(void);		/* view parameters */
  146.34 +void cam_reset_proj(void);		/* projection parameters */
  146.35 +void cam_reset_stereo(void);	/* stereo parameters */
  146.36 +
  146.37 +/* camera viewing parameters */
  146.38 +void cam_pan(int dx, int dy);		/* pan across X/Z plane */
  146.39 +void cam_height(int dh);			/* move verticaly */
  146.40 +void cam_rotate(int dx, int dy);	/* rotate around local Y and X axis */
  146.41 +void cam_zoom(int dz);				/* dolly the camera fwd/back */
  146.42 +
  146.43 +/* camera projection parameters */
  146.44 +void cam_clip(float n, float f);	/* set clipping planes */
  146.45 +void cam_fov(float f);				/* vertical field of view in degrees */
  146.46 +void cam_aspect(float a);			/* aspect ratio (width / height) */
  146.47 +
  146.48 +/* stereo parameters */
  146.49 +void cam_separation(float s);
  146.50 +void cam_focus_dist(float d);
  146.51 +
  146.52 +/* multiply the camera view matrix on top of the current matrix stack
  146.53 + * (which should be GL_MODELVIEW)
  146.54 + */
  146.55 +void cam_view_matrix(void);
  146.56 +void cam_stereo_view_matrix(int eye);
  146.57 +
  146.58 +/* multiply the camera projection matrix on top of the current matrix stack
  146.59 + * (which should be GL_PROJECTION)
  146.60 + */
  146.61 +void cam_proj_matrix(void);
  146.62 +void cam_stereo_proj_matrix(int eye);
  146.63 +
  146.64 +#endif	/* CAM_H_ */
   147.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   147.2 +++ b/src/config.h	Sat Sep 19 05:51:51 2015 +0300
   147.3 @@ -0,0 +1,10 @@
   147.4 +#ifndef CONFIG_H_
   147.5 +#define CONFIG_H_
   147.6 +
   147.7 +#if defined(__IPHONE_3_0) || defined(__IPHONE_3_2) || defined(__IPHONE_4_0)
   147.8 +#define IPHONE
   147.9 +
  147.10 +#define glClearDepth	glClearDepthf
  147.11 +#endif
  147.12 +
  147.13 +#endif	/* CONFIG_H_ */
   148.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   148.2 +++ b/src/glutmain.c	Sat Sep 19 05:51:51 2015 +0300
   148.3 @@ -0,0 +1,89 @@
   148.4 +/*
   148.5 +Stereoscopic tunnel for iOS.
   148.6 +Copyright (C) 2011  John Tsiombikas <nuclear@member.fsf.org>
   148.7 +
   148.8 +This program is free software: you can redistribute it and/or modify
   148.9 +it under the terms of the GNU General Public License as published by
  148.10 +the Free Software Foundation, either version 3 of the License, or
  148.11 +(at your option) any later version.
  148.12 +
  148.13 +This program is distributed in the hope that it will be useful,
  148.14 +but WITHOUT ANY WARRANTY; without even the implied warranty of
  148.15 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  148.16 +GNU General Public License for more details.
  148.17 +
  148.18 +You should have received a copy of the GNU General Public License
  148.19 +along with this program.  If not, see <http://www.gnu.org/licenses/>.
  148.20 +*/
  148.21 +
  148.22 +
  148.23 +#include <stdio.h>
  148.24 +#include <stdlib.h>
  148.25 +#include <GL/glew.h>
  148.26 +#include <GL/glut.h>
  148.27 +#include "sanegl.h"
  148.28 +#include "istereo.h"
  148.29 +#include "sdr.h"
  148.30 +
  148.31 +void disp(void);
  148.32 +void keyb(unsigned char key, int x, int y);
  148.33 +
  148.34 +int main(int argc, char **argv)
  148.35 +{
  148.36 +	glutInit(&argc, argv);
  148.37 +	glutInitWindowSize(640, 920);
  148.38 +	glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
  148.39 +	glutCreateWindow("test");
  148.40 +
  148.41 +	glutDisplayFunc(disp);
  148.42 +	glutIdleFunc(glutPostRedisplay);
  148.43 +	glutReshapeFunc(reshape);
  148.44 +	glutKeyboardFunc(keyb);
  148.45 +
  148.46 +	glewInit();
  148.47 +
  148.48 +	if(init() == -1) {
  148.49 +		return 1;
  148.50 +	}
  148.51 +
  148.52 +	glutMainLoop();
  148.53 +	return 0;
  148.54 +}
  148.55 +
  148.56 +void disp(void)
  148.57 +{
  148.58 +	redraw();
  148.59 +
  148.60 +	glutSwapBuffers();
  148.61 +}
  148.62 +
  148.63 +extern int stereo;
  148.64 +extern int use_bump;
  148.65 +
  148.66 +void keyb(unsigned char key, int x, int y)
  148.67 +{
  148.68 +	switch(key) {
  148.69 +	case 27:
  148.70 +		exit(0);
  148.71 +
  148.72 +	case 's':
  148.73 +		stereo = !stereo;
  148.74 +		break;
  148.75 +
  148.76 +	case 'b':
  148.77 +		use_bump = !use_bump;
  148.78 +		break;
  148.79 +
  148.80 +	case '`':
  148.81 +		{
  148.82 +			int xsz = glutGet(GLUT_WINDOW_WIDTH);
  148.83 +			int ysz = glutGet(GLUT_WINDOW_HEIGHT);
  148.84 +
  148.85 +			glutReshapeWindow(ysz, xsz);
  148.86 +		}
  148.87 +		break;
  148.88 +
  148.89 +	default:
  148.90 +		break;
  148.91 +	}
  148.92 +}
   149.1 --- a/src/ios/viewctl.m	Fri Sep 18 23:02:50 2015 +0300
   149.2 +++ b/src/ios/viewctl.m	Sat Sep 19 05:51:51 2015 +0300
   149.3 @@ -1,5 +1,7 @@
   149.4 +#include <stdlib.h>
   149.5 +#import <OpenGLES/ES2/glext.h>
   149.6  #import "viewctl.h"
   149.7 -#import <OpenGLES/ES2/glext.h>
   149.8 +#include "istereo.h"
   149.9  
  149.10  @interface ViewController () {
  149.11  	EAGLContext *ctx;
  149.12 @@ -10,6 +12,8 @@
  149.13  
  149.14  
  149.15  - (void)create_ad;
  149.16 +- (void)show_ad;
  149.17 +- (void)hide_ad;
  149.18  
  149.19  - (void)init_gl;
  149.20  - (void)destroy_gl;
  149.21 @@ -32,7 +36,6 @@
  149.22  
  149.23  	[self create_ad];
  149.24  
  149.25 -
  149.26  	[self init_gl];
  149.27  }
  149.28  
  149.29 @@ -70,35 +73,15 @@
  149.30  
  149.31  - (void)create_ad
  149.32  {
  149.33 -	ad_visible = NO;
  149.34  	ad = [[ADBannerView alloc] initWithAdType: ADAdTypeBanner];
  149.35  	[ad setAutoresizingMask: UIViewAutoresizingFlexibleWidth];
  149.36 -	ad.frame = CGRectOffset(ad.frame, 0, -ad.frame.size.height);
  149.37 +	ad_visible = YES;
  149.38 +	[self hide_ad];
  149.39  	[self.view addSubview: ad];
  149.40  	ad.delegate = self;
  149.41  }
  149.42  
  149.43 -- (void)init_gl
  149.44 -{
  149.45 -    [EAGLContext setCurrentContext: self->ctx];
  149.46 -
  149.47 -	glClearColor(1.0, 0.0, 0.0, 1.0);
  149.48 -}
  149.49 -
  149.50 -- (void)destroy_gl
  149.51 -{
  149.52 -    [EAGLContext setCurrentContext: self->ctx];
  149.53 -}
  149.54 -
  149.55 -
  149.56 -- (void)glkView: (GLKView*)view drawInRect: (CGRect)rect
  149.57 -{
  149.58 -    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  149.59 -}
  149.60 -
  149.61 -// ADBannerDelegate functions
  149.62 -
  149.63 -- (void)bannerViewDidLoadAd: (ADBannerView*)banner
  149.64 +- (void)show_ad
  149.65  {
  149.66  	if(!ad_visible) {
  149.67  		CGRect rect = ad.frame;
  149.68 @@ -108,12 +91,52 @@
  149.69  	}
  149.70  }
  149.71  
  149.72 +- (void)hide_ad
  149.73 +{
  149.74 +	if(ad_visible) {
  149.75 +		ad.frame = CGRectOffset(ad.frame, 0, -ad.frame.size.height);
  149.76 +		ad_visible = NO;
  149.77 +	}
  149.78 +}
  149.79 +
  149.80 +- (void)init_gl
  149.81 +{
  149.82 +    [EAGLContext setCurrentContext: self->ctx];
  149.83 +
  149.84 +	if(init() == -1) {
  149.85 +		NSLog(@"app initialization failed");
  149.86 +		exit(0);
  149.87 +	}
  149.88 +}
  149.89 +
  149.90 +- (void)destroy_gl
  149.91 +{
  149.92 +	cleanup();
  149.93 +    [EAGLContext setCurrentContext: self->ctx];
  149.94 +}
  149.95 +
  149.96 +
  149.97 +- (void)glkView: (GLKView*)view drawInRect: (CGRect)rect
  149.98 +{
  149.99 +	redraw();
 149.100 +}
 149.101 +
 149.102 +- (void)viewDidLayoutSubviews
 149.103 +{
 149.104 +	CGRect rect = self.view.frame;
 149.105 +	reshape(rect.size.width, rect.size.height);
 149.106 +}
 149.107 +
 149.108 +// ADBannerDelegate functions
 149.109 +
 149.110 +- (void)bannerViewDidLoadAd: (ADBannerView*)banner
 149.111 +{
 149.112 +	[self show_ad];
 149.113 +}
 149.114 +
 149.115  - (void)bannerView: (ADBannerView*)banner didFailToReceiveAdWithError: (NSError*)error
 149.116  {
 149.117 -	if(ad_visible) {
 149.118 -		ad_visible = NO;
 149.119 -		ad.frame = CGRectOffset(ad.frame, 0, -ad.frame.size.height);
 149.120 -	}
 149.121 +	[self hide_ad];
 149.122  
 149.123  	NSLog(@"Failed to retrieve ad");
 149.124  }
   150.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   150.2 +++ b/src/istereo.c	Sat Sep 19 05:51:51 2015 +0300
   150.3 @@ -0,0 +1,399 @@
   150.4 +/*
   150.5 +Stereoscopic tunnel for iOS.
   150.6 +Copyright (C) 2011  John Tsiombikas <nuclear@member.fsf.org>
   150.7 +
   150.8 +This program is free software: you can redistribute it and/or modify
   150.9 +it under the terms of the GNU General Public License as published by
  150.10 +the Free Software Foundation, either version 3 of the License, or
  150.11 +(at your option) any later version.
  150.12 +
  150.13 +This program is distributed in the hope that it will be useful,
  150.14 +but WITHOUT ANY WARRANTY; without even the implied warranty of
  150.15 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  150.16 +GNU General Public License for more details.
  150.17 +
  150.18 +You should have received a copy of the GNU General Public License
  150.19 +along with this program.  If not, see <http://www.gnu.org/licenses/>.
  150.20 +*/
  150.21 +
  150.22 +
  150.23 +#include <stdio.h>
  150.24 +#include <math.h>
  150.25 +#include <assert.h>
  150.26 +#include <unistd.h>
  150.27 +#include "opengl.h"
  150.28 +#include "istereo.h"
  150.29 +#include "sanegl.h"
  150.30 +#include "sdr.h"
  150.31 +#include "respath.h"
  150.32 +#include "tex.h"
  150.33 +#include "cam.h"
  150.34 +#include "vmath.h"
  150.35 +#include "config.h"
  150.36 +
  150.37 +static void render(float t);
  150.38 +static void draw_tunnel(float t);
  150.39 +static void tunnel_vertex(float u, float v, float du, float dv, int tang_loc, float t);
  150.40 +static vec3_t calc_text_pos(float sec);
  150.41 +static void draw_text(float idx, vec3_t tpos, float alpha);
  150.42 +static void worm(float t, float z, float *tx, float *ty);
  150.43 +static unsigned int get_shader_program(const char *vfile, const char *pfile);
  150.44 +static float get_sec(void);
  150.45 +
  150.46 +unsigned int prog, prog_simple, prog_tunnel, prog_text;
  150.47 +unsigned int tex, tex_stones, tex_normal, tex_text;
  150.48 +
  150.49 +int view_xsz, view_ysz;
  150.50 +
  150.51 +int stereo = 0;
  150.52 +int use_bump = 0;
  150.53 +
  150.54 +/* construction parameters */
  150.55 +int sides = 24;
  150.56 +int segm = 20;
  150.57 +float tunnel_speed = 0.75;
  150.58 +float ring_height = 0.5;
  150.59 +float text_period = 13.0;
  150.60 +float text_speed = 2.2;
  150.61 +
  150.62 +float split = 0.5275;
  150.63 +
  150.64 +int init(void)
  150.65 +{
  150.66 +	add_resource_path("sdr");
  150.67 +	add_resource_path("data");
  150.68 +
  150.69 +	if(!(prog_simple = get_shader_program("test.v.glsl", "test.p.glsl"))) {
  150.70 +		return -1;
  150.71 +	}
  150.72 +	if(!(prog_tunnel = get_shader_program("tunnel.v.glsl", "tunnel.p.glsl"))) {
  150.73 +		return -1;
  150.74 +	}
  150.75 +	if(!(prog_text = get_shader_program("text.v.glsl", "text.p.glsl"))) {
  150.76 +		return -1;
  150.77 +	}
  150.78 +
  150.79 +	if(!(tex = load_texture(find_resource("tiles.jpg", 0, 0)))) {
  150.80 +		return -1;
  150.81 +	}
  150.82 +	if(!(tex_stones = load_texture(find_resource("stonewall.jpg", 0, 0)))) {
  150.83 +		return -1;
  150.84 +	}
  150.85 +	if(!(tex_normal = load_texture(find_resource("stonewall_normal.jpg", 0, 0)))) {
  150.86 +		return -1;
  150.87 +	}
  150.88 +	if(!(tex_text = load_texture(find_resource("text.png", 0, 0)))) {
  150.89 +		return -1;
  150.90 +	}
  150.91 +
  150.92 +	glEnable(GL_DEPTH_TEST);
  150.93 +	glEnable(GL_CULL_FACE);
  150.94 +
  150.95 +	cam_fov(42.5);
  150.96 +	cam_clip(0.5, 250.0);
  150.97 +
  150.98 +	return 0;
  150.99 +}
 150.100 +
 150.101 +void cleanup(void)
 150.102 +{
 150.103 +	free_program(prog);
 150.104 +}
 150.105 +
 150.106 +void redraw(void)
 150.107 +{
 150.108 +	float pan_x, pan_y, z;
 150.109 +	float tsec = get_sec();
 150.110 +
 150.111 +	z = ring_height * segm;
 150.112 +	worm(tsec, z, &pan_x, &pan_y);
 150.113 +
 150.114 +	if(use_bump) {
 150.115 +		glClearColor(0.01, 0.01, 0.01, 1.0);
 150.116 +		tunnel_speed = 0.5;
 150.117 +	} else {
 150.118 +		glClearColor(0.6, 0.6, 0.6, 1.0);
 150.119 +		tunnel_speed = 0.75;
 150.120 +	}
 150.121 +	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 150.122 +
 150.123 +	if(stereo) {
 150.124 +		int split_pt = (int)((float)view_ysz * split);
 150.125 +
 150.126 +		/* right eye */
 150.127 +		glViewport(0, 0, view_xsz, split_pt);
 150.128 +		cam_aspect((float)split_pt / (float)view_xsz);
 150.129 +
 150.130 +		gl_matrix_mode(GL_PROJECTION);
 150.131 +		gl_load_identity();
 150.132 +		cam_stereo_proj_matrix(CAM_RIGHT);
 150.133 +		gl_rotatef(-90, 0, 0, 1);
 150.134 +
 150.135 +		gl_matrix_mode(GL_MODELVIEW);
 150.136 +		gl_load_identity();
 150.137 +		cam_stereo_view_matrix(CAM_RIGHT);
 150.138 +		gl_translatef(-pan_x, -pan_y, -1.1 * ring_height * segm);
 150.139 +
 150.140 +		render(tsec);
 150.141 +
 150.142 +		/* left eye */
 150.143 +		glViewport(0, split_pt, view_xsz, view_ysz - split_pt);
 150.144 +		cam_aspect((float)(view_ysz - split_pt) / (float)view_xsz);
 150.145 +
 150.146 +		gl_matrix_mode(GL_PROJECTION);
 150.147 +		gl_load_identity();
 150.148 +		cam_stereo_proj_matrix(CAM_LEFT);
 150.149 +		gl_rotatef(-90, 0, 0, 1);
 150.150 +
 150.151 +		gl_matrix_mode(GL_MODELVIEW);
 150.152 +		gl_load_identity();
 150.153 +		cam_stereo_view_matrix(CAM_LEFT);
 150.154 +		gl_translatef(-pan_x, -pan_y, -1.1 * ring_height * segm);
 150.155 +
 150.156 +		render(tsec);
 150.157 +	} else {
 150.158 +		glViewport(0, 0, view_xsz, view_ysz);
 150.159 +		cam_aspect((float)view_xsz / (float)view_ysz);
 150.160 +
 150.161 +		gl_matrix_mode(GL_PROJECTION);
 150.162 +		gl_load_identity();
 150.163 +		gl_rotatef(-90, 0, 0, 1);
 150.164 +		cam_proj_matrix();
 150.165 +
 150.166 +		gl_matrix_mode(GL_MODELVIEW);
 150.167 +		gl_load_identity();
 150.168 +		cam_view_matrix();
 150.169 +		gl_translatef(-pan_x, -pan_y, -1.1 * ring_height * segm);
 150.170 +
 150.171 +		render(tsec);
 150.172 +	}
 150.173 +
 150.174 +	assert(glGetError() == GL_NO_ERROR);
 150.175 +}
 150.176 +
 150.177 +static void render(float t)
 150.178 +{
 150.179 +	int i;
 150.180 +	float text_line;
 150.181 +
 150.182 +	draw_tunnel(t);
 150.183 +
 150.184 +	if(use_bump) {
 150.185 +		glDepthMask(0);
 150.186 +		text_line = floor((text_speed * t) / text_period);
 150.187 +		for(i=0; i<8; i++) {
 150.188 +			vec3_t tpos = calc_text_pos(t - (float)i * 0.011);
 150.189 +			draw_text(text_line, tpos, 1.5 / (float)i);
 150.190 +		}
 150.191 +		glDepthMask(1);
 150.192 +	}
 150.193 +}
 150.194 +
 150.195 +static void draw_tunnel(float t)
 150.196 +{
 150.197 +	static const float uoffs[] = {0.0, 0.0, 1.0, 1.0};
 150.198 +	static const float voffs[] = {0.0, 1.0, 1.0, 0.0};
 150.199 +	int i, j, k, tang_loc = -1;
 150.200 +	float du, dv;
 150.201 +
 150.202 +	prog = use_bump ? prog_tunnel : prog_simple;
 150.203 +
 150.204 +	bind_program(prog);
 150.205 +	set_uniform_float(prog, "t", t);
 150.206 +
 150.207 +	if(use_bump) {
 150.208 +		vec3_t ltpos = calc_text_pos(t);
 150.209 +
 150.210 +		bind_texture(tex_normal, 1);
 150.211 +		set_uniform_int(prog, "tex_norm", 1);
 150.212 +		bind_texture(tex_stones, 0);
 150.213 +		set_uniform_int(prog, "tex", 0);
 150.214 +
 150.215 +		set_uniform_float4(prog, "light_pos", ltpos.x, ltpos.y, ltpos.z, 1.0);
 150.216 +		tang_loc = get_attrib_loc(prog, "attr_tangent");
 150.217 +	} else {
 150.218 +		bind_texture(tex, 0);
 150.219 +		set_uniform_int(prog, "tex", 0);
 150.220 +	}
 150.221 +
 150.222 +	gl_matrix_mode(GL_TEXTURE);
 150.223 +	gl_load_identity();
 150.224 +	gl_translatef(0, -fmod(t * tunnel_speed, 1.0), 0);
 150.225 +
 150.226 +	gl_begin(GL_QUADS);
 150.227 +	gl_color3f(1.0, 1.0, 1.0);
 150.228 +
 150.229 +	du = 1.0 / sides;
 150.230 +	dv = 1.0 / segm;
 150.231 +
 150.232 +	for(i=0; i<segm; i++) {
 150.233 +		float trans_zp[2], trans_z0[2], trans_z1[2];
 150.234 +
 150.235 +		float zp = ring_height * (i - 1);
 150.236 +		float z0 = ring_height * i;
 150.237 +		float z1 = ring_height * (i + 1);
 150.238 +
 150.239 +		worm(t, zp, trans_zp, trans_zp + 1);
 150.240 +		worm(t, z0, trans_z0, trans_z0 + 1);
 150.241 +		worm(t, z1, trans_z1, trans_z1 + 1);
 150.242 +
 150.243 +		for(j=0; j<sides; j++) {
 150.244 +			for(k=0; k<4; k++) {
 150.245 +				float u = (j + uoffs[k]) * du;
 150.246 +				float v = (i + voffs[k]) * dv;
 150.247 +
 150.248 +				tunnel_vertex(u, v, du, dv, tang_loc, t);
 150.249 +			}
 150.250 +		}
 150.251 +	}
 150.252 +	gl_end();
 150.253 +
 150.254 +	bind_texture(0, 1);
 150.255 +	bind_texture(0, 0);
 150.256 +}
 150.257 +
 150.258 +static void tunnel_vertex(float u, float v, float du, float dv, int tang_loc, float t)
 150.259 +{
 150.260 +	vec3_t pos, norm;
 150.261 +	vec3_t dfdu, dfdv, pos_du, pos_dv;
 150.262 +
 150.263 +	float theta = 2.0 * M_PI * u;
 150.264 +	float theta1 = 2.0 * M_PI * (u + du);
 150.265 +
 150.266 +	float x = cos(theta);
 150.267 +	float y = sin(theta);
 150.268 +	float x1 = cos(theta1);
 150.269 +	float y1 = sin(theta1);
 150.270 +	float z = v / dv * ring_height;
 150.271 +	float z1 = (v + dv) / dv * ring_height;
 150.272 +
 150.273 +	float trans_z[2], trans_z1[2];
 150.274 +
 150.275 +	worm(t, z, trans_z, trans_z + 1);
 150.276 +	worm(t, z1, trans_z1, trans_z1 + 1);
 150.277 +
 150.278 +	pos = v3_cons(x + trans_z[0], y + trans_z[1], z);
 150.279 +	pos_du = v3_cons(x1 + trans_z[0], y1 + trans_z[1], z);
 150.280 +	pos_dv = v3_cons(x + trans_z1[0], y + trans_z1[1], z1);
 150.281 +
 150.282 +	dfdu = v3_sub(pos_du, pos);
 150.283 +	dfdv = v3_sub(pos_dv, pos);
 150.284 +	norm = v3_cross(dfdv, dfdu);
 150.285 +
 150.286 +	gl_vertex_attrib3f(tang_loc, dfdu.x, dfdu.y, dfdu.z);
 150.287 +	gl_normal3f(norm.x, norm.y, norm.z);
 150.288 +	gl_texcoord2f(u * 2.0, v * 4.0);
 150.289 +	gl_vertex3f(pos.x, pos.y, pos.z);
 150.290 +}
 150.291 +
 150.292 +static vec3_t calc_text_pos(float sec)
 150.293 +{
 150.294 +	float t = text_speed * sec;
 150.295 +	float z = fmod(t, text_period);
 150.296 +	float pan[2];
 150.297 +
 150.298 +	worm(sec, z, pan, pan + 1);
 150.299 +	return v3_cons(pan[0], pan[1], z + ring_height);
 150.300 +}
 150.301 +
 150.302 +static void draw_text(float idx, vec3_t tpos, float alpha)
 150.303 +{
 150.304 +	gl_matrix_mode(GL_MODELVIEW);
 150.305 +	gl_push_matrix();
 150.306 +	gl_translatef(tpos.x, tpos.y, tpos.z);
 150.307 +
 150.308 +	glEnable(GL_BLEND);
 150.309 +	glBlendFunc(GL_SRC_ALPHA, GL_ONE);
 150.310 +
 150.311 +	bind_program(prog_text);
 150.312 +	set_uniform_float(prog_text, "idx", idx);
 150.313 +
 150.314 +	bind_texture(tex_text, 0);
 150.315 +
 150.316 +	gl_begin(GL_QUADS);
 150.317 +	gl_color4f(1.0, 1.0, 1.0, alpha > 1.0 ? 1.0 : alpha);
 150.318 +
 150.319 +	gl_texcoord2f(0, 1);
 150.320 +	gl_vertex3f(-1, -0.2, 0);
 150.321 +
 150.322 +	gl_texcoord2f(1, 1);
 150.323 +	gl_vertex3f(1, -0.2, 0);
 150.324 +
 150.325 +	gl_texcoord2f(1, 0);
 150.326 +	gl_vertex3f(1, 0.2, 0);
 150.327 +
 150.328 +	gl_texcoord2f(0, 0);
 150.329 +	gl_vertex3f(-1, 0.2, 0);
 150.330 +	gl_end();
 150.331 +
 150.332 +	bind_texture(0, 0);
 150.333 +	glDisable(GL_BLEND);
 150.334 +
 150.335 +	gl_pop_matrix();
 150.336 +}
 150.337 +
 150.338 +
 150.339 +static void worm(float t, float z, float *tx, float *ty)
 150.340 +{
 150.341 +	float x, y;
 150.342 +	x = sin(t) + cos(t + z) + sin(t * 2.0 + z) / 2.0;
 150.343 +	y = cos(t) + sin(t + z) + cos(t * 2.0 + z) / 2.0;
 150.344 +
 150.345 +	*tx = x * 0.5;
 150.346 +	*ty = y * 0.5;
 150.347 +}
 150.348 +
 150.349 +
 150.350 +void reshape(int x, int y)
 150.351 +{
 150.352 +	glViewport(0, 0, x, y);
 150.353 +
 150.354 +	gl_matrix_mode(GL_PROJECTION);
 150.355 +	gl_load_identity();
 150.356 +	glu_perspective(40.0, (float)x / (float)y, 0.5, 500.0);
 150.357 +
 150.358 +	view_xsz = x;
 150.359 +	view_ysz = y;
 150.360 +}
 150.361 +
 150.362 +static unsigned int get_shader_program(const char *vfile, const char *pfile)
 150.363 +{
 150.364 +	unsigned int prog, vs, ps;
 150.365 +
 150.366 +	if(!(vs = get_vertex_shader(find_resource(vfile, 0, 0)))) {
 150.367 +		return 0;
 150.368 +	}
 150.369 +	if(!(ps = get_pixel_shader(find_resource(pfile, 0, 0)))) {
 150.370 +		return 0;
 150.371 +	}
 150.372 +
 150.373 +	if(!(prog = create_program_link(vs, ps))) {
 150.374 +		return 0;
 150.375 +	}
 150.376 +	return prog;
 150.377 +}
 150.378 +
 150.379 +
 150.380 +#ifdef IPHONE
 150.381 +#include <QuartzCore/QuartzCore.h>
 150.382 +
 150.383 +static float get_sec(void)
 150.384 +{
 150.385 +	static float first;
 150.386 +	static int init;
 150.387 +
 150.388 +	if(!init) {
 150.389 +		init = 1;
 150.390 +		first = CACurrentMediaTime();
 150.391 +		return 0.0f;
 150.392 +	}
 150.393 +	return CACurrentMediaTime() - first;
 150.394 +}
 150.395 +
 150.396 +#else
 150.397 +
 150.398 +static float get_sec(void)
 150.399 +{
 150.400 +	return (float)glutGet(GLUT_ELAPSED_TIME) / 1000.0f;
 150.401 +}
 150.402 +#endif
   151.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   151.2 +++ b/src/istereo.h	Sat Sep 19 05:51:51 2015 +0300
   151.3 @@ -0,0 +1,28 @@
   151.4 +/*
   151.5 +Stereoscopic tunnel for iOS.
   151.6 +Copyright (C) 2011  John Tsiombikas <nuclear@member.fsf.org>
   151.7 +
   151.8 +This program is free software: you can redistribute it and/or modify
   151.9 +it under the terms of the GNU General Public License as published by
  151.10 +the Free Software Foundation, either version 3 of the License, or
  151.11 +(at your option) any later version.
  151.12 +
  151.13 +This program is distributed in the hope that it will be useful,
  151.14 +but WITHOUT ANY WARRANTY; without even the implied warranty of
  151.15 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  151.16 +GNU General Public License for more details.
  151.17 +
  151.18 +You should have received a copy of the GNU General Public License
  151.19 +along with this program.  If not, see <http://www.gnu.org/licenses/>.
  151.20 +*/
  151.21 +
  151.22 +
  151.23 +#ifndef ISTEREO_H_
  151.24 +#define ISTEREO_H_
  151.25 +
  151.26 +int init(void);
  151.27 +void cleanup(void);
  151.28 +void redraw(void);
  151.29 +void reshape(int x, int y);
  151.30 +
  151.31 +#endif	/* ISTEREO_H_ */
   152.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   152.2 +++ b/src/opengl.h	Sat Sep 19 05:51:51 2015 +0300
   152.3 @@ -0,0 +1,39 @@
   152.4 +/*
   152.5 +Stereoscopic tunnel for iOS.
   152.6 +Copyright (C) 2011  John Tsiombikas <nuclear@member.fsf.org>
   152.7 +
   152.8 +This program is free software: you can redistribute it and/or modify
   152.9 +it under the terms of the GNU General Public License as published by
  152.10 +the Free Software Foundation, either version 3 of the License, or
  152.11 +(at your option) any later version.
  152.12 +
  152.13 +This program is distributed in the hope that it will be useful,
  152.14 +but WITHOUT ANY WARRANTY; without even the implied warranty of
  152.15 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  152.16 +GNU General Public License for more details.
  152.17 +
  152.18 +You should have received a copy of the GNU General Public License
  152.19 +along with this program.  If not, see <http://www.gnu.org/licenses/>.
  152.20 +*/
  152.21 +
  152.22 +
  152.23 +#ifndef OPENGL_H_
  152.24 +#define OPENGL_H_
  152.25 +
  152.26 +#include "config.h"
  152.27 +
  152.28 +#ifdef IPHONE
  152.29 +#include <OpenGLES/ES2/gl.h>
  152.30 +#else
  152.31 +
  152.32 +#include <GL/glew.h>
  152.33 +
  152.34 +#ifdef __APPLE__
  152.35 +#include <GLUT/glut.h>
  152.36 +#else
  152.37 +#include <GL/glut.h>
  152.38 +#endif	/* __APPLE__ */
  152.39 +
  152.40 +#endif	/* IPHONE */
  152.41 +
  152.42 +#endif	/* OPENGL_H_ */
   153.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   153.2 +++ b/src/respath.c	Sat Sep 19 05:51:51 2015 +0300
   153.3 @@ -0,0 +1,109 @@
   153.4 +/*
   153.5 +Stereoscopic tunnel for iOS.
   153.6 +Copyright (C) 2011  John Tsiombikas <nuclear@member.fsf.org>
   153.7 +
   153.8 +This program is free software: you can redistribute it and/or modify
   153.9 +it under the terms of the GNU General Public License as published by
  153.10 +the Free Software Foundation, either version 3 of the License, or
  153.11 +(at your option) any later version.
  153.12 +
  153.13 +This program is distributed in the hope that it will be useful,
  153.14 +but WITHOUT ANY WARRANTY; without even the implied warranty of
  153.15 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  153.16 +GNU General Public License for more details.
  153.17 +
  153.18 +You should have received a copy of the GNU General Public License
  153.19 +along with this program.  If not, see <http://www.gnu.org/licenses/>.
  153.20 +*/
  153.21 +
  153.22 +
  153.23 +#include <stdio.h>
  153.24 +#include <stdlib.h>
  153.25 +#include <string.h>
  153.26 +#include <errno.h>
  153.27 +#include <unistd.h>
  153.28 +#include "respath.h"
  153.29 +#include "config.h"
  153.30 +
  153.31 +#ifdef IPHONE
  153.32 +#include <CoreFoundation/CoreFoundation.h>
  153.33 +#endif
  153.34 +
  153.35 +
  153.36 +#ifndef IPHONE
  153.37 +struct path_node {
  153.38 +	char *path;
  153.39 +	struct path_node *next;
  153.40 +};
  153.41 +
  153.42 +static struct path_node *pathlist;
  153.43 +
  153.44 +void add_resource_path(const char *path)
  153.45 +{
  153.46 +	struct path_node *node = 0;
  153.47 +
  153.48 +	if(!(node = malloc(sizeof *node)) || !(node->path = malloc(strlen(path) + 1))) {
  153.49 +		free(node);
  153.50 +		fprintf(stderr, "failed to add path: %s: %s\n", path, strerror(errno));
  153.51 +		return;
  153.52 +	}
  153.53 +	strcpy(node->path, path);
  153.54 +	node->next = pathlist;
  153.55 +	pathlist = node;
  153.56 +}
  153.57 +
  153.58 +char *find_resource(const char *fname, char *path, size_t sz)
  153.59 +{
  153.60 +	static char buffer[1024];
  153.61 +	struct path_node *node;
  153.62 +
  153.63 +	if(!path) {
  153.64 +		path = buffer;
  153.65 +		sz = sizeof buffer;
  153.66 +	}
  153.67 +
  153.68 +	node = pathlist;
  153.69 +	while(node) {
  153.70 +		snprintf(path, sz, "%s/%s", node->path, fname);
  153.71 +		if(access(path, F_OK) != -1) {
  153.72 +			return path;
  153.73 +		}
  153.74 +		node = node->next;
  153.75 +	}
  153.76 +
  153.77 +	fprintf(stderr, "can't find resource: %s\n", fname);
  153.78 +	return 0;
  153.79 +}
  153.80 +
  153.81 +#else	/* IPHONE */
  153.82 +
  153.83 +void add_resource_path(const char *path)
  153.84 +{
  153.85 +}
  153.86 +
  153.87 +
  153.88 +char *find_resource(const char *fname, char *path, size_t sz)
  153.89 +{
  153.90 +	static char buffer[1024];
  153.91 +	CFBundleRef bundle;
  153.92 +	CFURLRef url;
  153.93 +	CFStringRef cfname;
  153.94 +
  153.95 +	cfname = CFStringCreateWithCString(0, fname, kCFStringEncodingASCII);
  153.96 +
  153.97 +	bundle = CFBundleGetMainBundle();
  153.98 +	if(!(url = CFBundleCopyResourceURL(bundle, cfname, 0, 0))) {
  153.99 +		return 0;
 153.100 +	}
 153.101 +
 153.102 +	if(!path) {
 153.103 +		path = buffer;
 153.104 +		sz = sizeof buffer;
 153.105 +	}
 153.106 +
 153.107 +	if(!CFURLGetFileSystemRepresentation(url, 1, (unsigned char*)path, sz)) {
 153.108 +		return 0;
 153.109 +	}
 153.110 +	return path;
 153.111 +}
 153.112 +#endif
   154.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   154.2 +++ b/src/respath.h	Sat Sep 19 05:51:51 2015 +0300
   154.3 @@ -0,0 +1,26 @@
   154.4 +/*
   154.5 +Stereoscopic tunnel for iOS.
   154.6 +Copyright (C) 2011  John Tsiombikas <nuclear@member.fsf.org>
   154.7 +
   154.8 +This program is free software: you can redistribute it and/or modify
   154.9 +it under the terms of the GNU General Public License as published by
  154.10 +the Free Software Foundation, either version 3 of the License, or
  154.11 +(at your option) any later version.
  154.12 +
  154.13 +This program is distributed in the hope that it will be useful,
  154.14 +but WITHOUT ANY WARRANTY; without even the implied warranty of
  154.15 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  154.16 +GNU General Public License for more details.
  154.17 +
  154.18 +You should have received a copy of the GNU General Public License
  154.19 +along with this program.  If not, see <http://www.gnu.org/licenses/>.
  154.20 +*/
  154.21 +
  154.22 +
  154.23 +#ifndef RESPATH_H_
  154.24 +#define RESPATH_H_
  154.25 +
  154.26 +void add_resource_path(const char *path);
  154.27 +char *find_resource(const char *fname, char *path, size_t sz);
  154.28 +
  154.29 +#endif	/* RESPATH_H_ */
   155.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   155.2 +++ b/src/sanegl.c	Sat Sep 19 05:51:51 2015 +0300
   155.3 @@ -0,0 +1,490 @@
   155.4 +/*
   155.5 +SaneGL - a small library to bring back sanity to OpenGL ES 2.x
   155.6 +Copyright (C) 2011  John Tsiombikas <nuclear@member.fsf.org>
   155.7 +
   155.8 +This program is free software: you can redistribute it and/or modify
   155.9 +it under the terms of the GNU General Public License as published by
  155.10 +the Free Software Foundation, either version 3 of the License, or
  155.11 +(at your option) any later version.
  155.12 +
  155.13 +This program is distributed in the hope that it will be useful,
  155.14 +but WITHOUT ANY WARRANTY; without even the implied warranty of
  155.15 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  155.16 +GNU General Public License for more details.
  155.17 +
  155.18 +You should have received a copy of the GNU General Public License
  155.19 +along with this program.  If not, see <http://www.gnu.org/licenses/>.
  155.20 +*/
  155.21 +
  155.22 +#include <stdio.h>
  155.23 +#include <stdlib.h>
  155.24 +#include <string.h>
  155.25 +#include <math.h>
  155.26 +#include <assert.h>
  155.27 +#include "sanegl.h"
  155.28 +
  155.29 +#define MMODE_IDX(x)	((x) - GL_MODELVIEW)
  155.30 +#define MAT_STACK_SIZE	32
  155.31 +#define MAT_IDENT	{1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}
  155.32 +
  155.33 +#define MAX_VERTS	512
  155.34 +
  155.35 +static void gl_draw_immediate(void);
  155.36 +
  155.37 +typedef struct { float x, y; } vec2_t;
  155.38 +typedef struct { float x, y, z; } vec3_t;
  155.39 +typedef struct { float x, y, z, w; } vec4_t;
  155.40 +
  155.41 +static int mm_idx = 0;
  155.42 +static float mat_stack[3][MAT_STACK_SIZE][16] = {{MAT_IDENT}, {MAT_IDENT}, {MAT_IDENT}};
  155.43 +static int stack_top[3];
  155.44 +static float mat_mvp[16];
  155.45 +static int mvp_valid;
  155.46 +static int prim = -1;
  155.47 +
  155.48 +static vec3_t cur_normal;
  155.49 +static vec4_t cur_color, cur_attrib;
  155.50 +static vec2_t cur_texcoord;
  155.51 +
  155.52 +static vec4_t *vert_arr, *col_arr, *attr_arr;
  155.53 +static vec3_t *norm_arr;
  155.54 +static vec2_t *texc_arr;
  155.55 +/*static unsigned int vbuf, cbuf, nbuf, tbuf, abuf;*/
  155.56 +static int vloc, nloc, cloc, tloc, aloc = -1;
  155.57 +
  155.58 +static int num_verts, vert_calls;
  155.59 +static int cur_prog;
  155.60 +
  155.61 +
  155.62 +void gl_matrix_mode(int mm)
  155.63 +{
  155.64 +	mm_idx = MMODE_IDX(mm);
  155.65 +}
  155.66 +
  155.67 +void gl_push_matrix(void)
  155.68 +{
  155.69 +	int top = stack_top[mm_idx];
  155.70 +
  155.71 +	memcpy(mat_stack[mm_idx][top + 1], mat_stack[mm_idx][top], 16 * sizeof(float));
  155.72 +	stack_top[mm_idx]++;
  155.73 +	mvp_valid = 0;
  155.74 +}
  155.75 +
  155.76 +void gl_pop_matrix(void)
  155.77 +{
  155.78 +	stack_top[mm_idx]--;
  155.79 +	mvp_valid = 0;
  155.80 +}
  155.81 +
  155.82 +void gl_load_identity(void)
  155.83 +{
  155.84 +	static const float idmat[] = MAT_IDENT;
  155.85 +	int top = stack_top[mm_idx];
  155.86 +	float *mat = mat_stack[mm_idx][top];
  155.87 +
  155.88 +	memcpy(mat, idmat, sizeof idmat);
  155.89 +	mvp_valid = 0;
  155.90 +}
  155.91 +
  155.92 +void gl_load_matrixf(const float *m)
  155.93 +{
  155.94 +	int top = stack_top[mm_idx];
  155.95 +	float *mat = mat_stack[mm_idx][top];
  155.96 +
  155.97 +	memcpy(mat, m, 16 * sizeof *mat);
  155.98 +	mvp_valid = 0;
  155.99 +}
 155.100 +
 155.101 +#define M(i, j)	((i << 2) + j)
 155.102 +
 155.103 +void gl_mult_matrixf(const float *m2)
 155.104 +{
 155.105 +	int i, j;
 155.106 +	int top = stack_top[mm_idx];
 155.107 +	float *m1 = mat_stack[mm_idx][top];
 155.108 +	float res[16];
 155.109 +
 155.110 +	for(i=0; i<4; i++) {
 155.111 +		for(j=0; j<4; j++) {
 155.112 +			res[M(i,j)] = m1[M(i,0)] * m2[M(0,j)] +
 155.113 +						m1[M(i,1)] * m2[M(1,j)] +
 155.114 +						m1[M(i,2)] * m2[M(2,j)] +
 155.115 +						m1[M(i,3)] * m2[M(3,j)];
 155.116 +		}
 155.117 +	}
 155.118 +
 155.119 +	memcpy(m1, res, sizeof res);
 155.120 +	mvp_valid = 0;
 155.121 +}
 155.122 +
 155.123 +void gl_translatef(float x, float y, float z)
 155.124 +{
 155.125 +	float mat[] = MAT_IDENT;
 155.126 +
 155.127 +	mat[12] = x;
 155.128 +	mat[13] = y;
 155.129 +	mat[14] = z;
 155.130 +
 155.131 +	gl_mult_matrixf(mat);
 155.132 +}
 155.133 +
 155.134 +void gl_rotatef(float angle, float x, float y, float z)
 155.135 +{
 155.136 +	float mat[] = MAT_IDENT;
 155.137 +
 155.138 +	float angle_rad = M_PI * angle / 180.0;
 155.139 +	float sina = sin(angle_rad);
 155.140 +	float cosa = cos(angle_rad);
 155.141 +	float one_minus_cosa = 1.0 - cosa;
 155.142 +	float nxsq = x * x;
 155.143 +	float nysq = y * y;
 155.144 +	float nzsq = z * z;
 155.145 +
 155.146 +	mat[0] = nxsq + (1.0 - nxsq) * cosa;
 155.147 +	mat[4] = x * y * one_minus_cosa - z * sina;
 155.148 +	mat[8] = x * z * one_minus_cosa + y * sina;
 155.149 +	mat[1] = x * y * one_minus_cosa + z * sina;
 155.150 +	mat[5] = nysq + (1.0 - nysq) * cosa;
 155.151 +	mat[9] = y * z * one_minus_cosa - x * sina;
 155.152 +	mat[2] = x * z * one_minus_cosa - y * sina;
 155.153 +	mat[6] = y * z * one_minus_cosa + x * sina;
 155.154 +	mat[10] = nzsq + (1.0 - nzsq) * cosa;
 155.155 +
 155.156 +	gl_mult_matrixf(mat);
 155.157 +}
 155.158 +
 155.159 +void gl_scalef(float x, float y, float z)
 155.160 +{
 155.161 +	float mat[] = MAT_IDENT;
 155.162 +
 155.163 +	mat[0] = x;
 155.164 +	mat[5] = y;
 155.165 +	mat[10] = z;
 155.166 +
 155.167 +	gl_mult_matrixf(mat);
 155.168 +}
 155.169 +
 155.170 +void gl_ortho(float left, float right, float bottom, float top, float near, float far)
 155.171 +{
 155.172 +	float mat[] = MAT_IDENT;
 155.173 +
 155.174 +	float dx = right - left;
 155.175 +	float dy = top - bottom;
 155.176 +	float dz = far - near;
 155.177 +
 155.178 +	float tx = -(right + left) / dx;
 155.179 +	float ty = -(top + bottom) / dy;
 155.180 +	float tz = -(far + near) / dz;
 155.181 +
 155.182 +	float sx = 2.0 / dx;
 155.183 +	float sy = 2.0 / dy;
 155.184 +	float sz = -2.0 / dz;
 155.185 +
 155.186 +	mat[0] = sx;
 155.187 +	mat[5] = sy;
 155.188 +	mat[10] = sz;
 155.189 +	mat[12] = tx;
 155.190 +	mat[13] = ty;
 155.191 +	mat[14] = tz;
 155.192 +
 155.193 +	gl_mult_matrixf(mat);
 155.194 +}
 155.195 +
 155.196 +void gl_frustum(float left, float right, float bottom, float top, float near, float far)
 155.197 +{
 155.198 +	float mat[] = MAT_IDENT;
 155.199 +
 155.200 +	float dx = right - left;
 155.201 +	float dy = top - bottom;
 155.202 +	float dz = far - near;
 155.203 +
 155.204 +	float a = (right + left) / dx;
 155.205 +	float b = (top + bottom) / dy;
 155.206 +	float c = -(far + near) / dz;
 155.207 +	float d = -2.0 * far * near / dz;
 155.208 +
 155.209 +	mat[0] = 2.0 * near / dx;
 155.210 +	mat[5] = 2.0 * near / dy;
 155.211 +	mat[8] = a;
 155.212 +	mat[9] = b;
 155.213 +	mat[10] = c;
 155.214 +	mat[11] = -1.0;
 155.215 +	mat[14] = d;
 155.216 +
 155.217 +	gl_mult_matrixf(mat);
 155.218 +}
 155.219 +
 155.220 +void glu_perspective(float vfov, float aspect, float near, float far)
 155.221 +{
 155.222 +	float vfov_rad = M_PI * vfov / 180.0;
 155.223 +	float x = near * tan(vfov_rad / 2.0);
 155.224 +	gl_frustum(-aspect * x, aspect * x, -x, x, near, far);
 155.225 +}
 155.226 +
 155.227 +void gl_apply_xform(unsigned int prog)
 155.228 +{
 155.229 +	int loc, mvidx, pidx, tidx, mvtop, ptop, ttop;
 155.230 +
 155.231 +	mvidx = MMODE_IDX(GL_MODELVIEW);
 155.232 +	pidx = MMODE_IDX(GL_PROJECTION);
 155.233 +	tidx = MMODE_IDX(GL_TEXTURE);
 155.234 +
 155.235 +	mvtop = stack_top[mvidx];
 155.236 +	ptop = stack_top[pidx];
 155.237 +	ttop = stack_top[tidx];
 155.238 +
 155.239 +	assert(prog);
 155.240 +
 155.241 +	if((loc = glGetUniformLocation(prog, "matrix_modelview")) != -1) {
 155.242 +		glUniformMatrix4fv(loc, 1, 0, mat_stack[mvidx][mvtop]);
 155.243 +	}
 155.244 +
 155.245 +	if((loc = glGetUniformLocation(prog, "matrix_projection")) != -1) {
 155.246 +		glUniformMatrix4fv(loc, 1, 0, mat_stack[pidx][ptop]);
 155.247 +	}
 155.248 +
 155.249 +	if((loc = glGetUniformLocation(prog, "matrix_texture")) != -1) {
 155.250 +		glUniformMatrix4fv(loc, 1, 0, mat_stack[tidx][ttop]);
 155.251 +	}
 155.252 +
 155.253 +	if((loc = glGetUniformLocation(prog, "matrix_normal")) != -1) {
 155.254 +		float nmat[9];
 155.255 +
 155.256 +		nmat[0] = mat_stack[mvidx][mvtop][0];
 155.257 +		nmat[1] = mat_stack[mvidx][mvtop][1];
 155.258 +		nmat[2] = mat_stack[mvidx][mvtop][2];
 155.259 +		nmat[3] = mat_stack[mvidx][mvtop][4];
 155.260 +		nmat[4] = mat_stack[mvidx][mvtop][5];
 155.261 +		nmat[5] = mat_stack[mvidx][mvtop][6];
 155.262 +		nmat[6] = mat_stack[mvidx][mvtop][8];
 155.263 +		nmat[7] = mat_stack[mvidx][mvtop][9];
 155.264 +		nmat[8] = mat_stack[mvidx][mvtop][10];
 155.265 +		glUniformMatrix3fv(loc, 1, 0, nmat);
 155.266 +	}
 155.267 +
 155.268 +	if((loc = glGetUniformLocation(prog, "matrix_modelview_projection")) != -1) {
 155.269 +		if(!mvp_valid) {
 155.270 +			/* TODO calc mvp */
 155.271 +		}
 155.272 +		glUniformMatrix4fv(loc, 1, 0, mat_mvp);
 155.273 +	}
 155.274 +}
 155.275 +
 155.276 +
 155.277 +/* immediate mode rendering */
 155.278 +void gl_begin(int p)
 155.279 +{
 155.280 +	if(!vert_arr) {
 155.281 +		vert_arr = malloc(MAX_VERTS * sizeof *vert_arr);
 155.282 +		norm_arr = malloc(MAX_VERTS * sizeof *norm_arr);
 155.283 +		texc_arr = malloc(MAX_VERTS * sizeof *texc_arr);
 155.284 +		col_arr = malloc(MAX_VERTS * sizeof *col_arr);
 155.285 +		attr_arr = malloc(MAX_VERTS * sizeof *attr_arr);
 155.286 +		assert(vert_arr && norm_arr && texc_arr && col_arr && attr_arr);
 155.287 +	}
 155.288 +
 155.289 +	prim = p;
 155.290 +	num_verts = vert_calls = 0;
 155.291 +
 155.292 +	glGetIntegerv(GL_CURRENT_PROGRAM, &cur_prog);
 155.293 +	assert(cur_prog);
 155.294 +
 155.295 +	gl_apply_xform(cur_prog);
 155.296 +
 155.297 +	vloc = glGetAttribLocation(cur_prog, "attr_vertex");
 155.298 +	nloc = glGetAttribLocation(cur_prog, "attr_normal");
 155.299 +	cloc = glGetAttribLocation(cur_prog, "attr_color");
 155.300 +	tloc = glGetAttribLocation(cur_prog, "attr_texcoord");
 155.301 +}
 155.302 +
 155.303 +void gl_end(void)
 155.304 +{
 155.305 +	if(num_verts > 0) {
 155.306 +		gl_draw_immediate();
 155.307 +	}
 155.308 +	aloc = -1;
 155.309 +}
 155.310 +
 155.311 +static void gl_draw_immediate(void)
 155.312 +{
 155.313 +	int glprim;
 155.314 +
 155.315 +	if(vloc == -1) {
 155.316 +		fprintf(stderr, "gl_draw_immediate call with vloc == -1\n");
 155.317 +		return;
 155.318 +	}
 155.319 +
 155.320 +	glprim = prim == GL_QUADS ? GL_TRIANGLES : prim;
 155.321 +
 155.322 +	glVertexAttribPointer(vloc, 4, GL_FLOAT, 0, 0, vert_arr);
 155.323 +	glEnableVertexAttribArray(vloc);
 155.324 +
 155.325 +	if(nloc != -1) {
 155.326 +		glVertexAttribPointer(nloc, 3, GL_FLOAT, 0, 0, norm_arr);
 155.327 +		glEnableVertexAttribArray(nloc);
 155.328 +	}
 155.329 +
 155.330 +	if(cloc != -1) {
 155.331 +		glVertexAttribPointer(cloc, 4, GL_FLOAT, 1, 0, col_arr);
 155.332 +		glEnableVertexAttribArray(cloc);
 155.333 +	}
 155.334 +
 155.335 +	if(tloc != -1) {
 155.336 +		glVertexAttribPointer(tloc, 2, GL_FLOAT, 0, 0, texc_arr);
 155.337 +		glEnableVertexAttribArray(tloc);
 155.338 +	}
 155.339 +
 155.340 +	if(aloc != -1) {
 155.341 +		glVertexAttribPointer(aloc, 4, GL_FLOAT, 0, 0, attr_arr);
 155.342 +		glEnableVertexAttribArray(aloc);
 155.343 +	}
 155.344 +
 155.345 +	glDrawArrays(glprim, 0, num_verts);
 155.346 +
 155.347 +	glDisableVertexAttribArray(vloc);
 155.348 +	if(nloc != -1) {
 155.349 +		glDisableVertexAttribArray(nloc);
 155.350 +	}
 155.351 +	if(cloc != -1) {
 155.352 +		glDisableVertexAttribArray(cloc);
 155.353 +	}
 155.354 +	if(tloc != -1) {
 155.355 +		glDisableVertexAttribArray(tloc);
 155.356 +	}
 155.357 +	if(aloc != -1) {
 155.358 +		glDisableVertexAttribArray(aloc);
 155.359 +	}
 155.360 +}
 155.361 +
 155.362 +
 155.363 +void gl_vertex2f(float x, float y)
 155.364 +{
 155.365 +	gl_vertex4f(x, y, 0.0f, 1.0f);
 155.366 +}
 155.367 +
 155.368 +void gl_vertex3f(float x, float y, float z)
 155.369 +{
 155.370 +	gl_vertex4f(x, y, z, 1.0f);
 155.371 +}
 155.372 +
 155.373 +void gl_vertex4f(float x, float y, float z, float w)
 155.374 +{
 155.375 +	int i, buffer_full;
 155.376 +
 155.377 +	if(prim == GL_QUADS && vert_calls % 4 == 3) {
 155.378 +		for(i=0; i<2; i++) {
 155.379 +			if(aloc != -1) {
 155.380 +				attr_arr[num_verts] = attr_arr[num_verts - 3 + i];
 155.381 +			}
 155.382 +			if(cloc != -1) {
 155.383 +				col_arr[num_verts] = col_arr[num_verts - 3 + i];
 155.384 +			}
 155.385 +			if(tloc != -1) {
 155.386 +				texc_arr[num_verts] = texc_arr[num_verts - 3 + i];
 155.387 +			}
 155.388 +			if(nloc != -1) {
 155.389 +				norm_arr[num_verts] = norm_arr[num_verts - 3 + i];
 155.390 +			}
 155.391 +			vert_arr[num_verts] = vert_arr[num_verts - 3 + i];
 155.392 +			num_verts++;
 155.393 +		}
 155.394 +	}
 155.395 +
 155.396 +	vert_arr[num_verts].x = x;
 155.397 +	vert_arr[num_verts].y = y;
 155.398 +	vert_arr[num_verts].z = z;
 155.399 +	vert_arr[num_verts].w = w;
 155.400 +
 155.401 +	if(cloc != -1) {
 155.402 +		col_arr[num_verts] = cur_color;
 155.403 +	}
 155.404 +	if(nloc != -1) {
 155.405 +		norm_arr[num_verts] = cur_normal;
 155.406 +	}
 155.407 +	if(tloc != -1) {
 155.408 +		texc_arr[num_verts] = cur_texcoord;
 155.409 +	}
 155.410 +	if(aloc != -1) {
 155.411 +		attr_arr[num_verts] = cur_attrib;
 155.412 +	}
 155.413 +
 155.414 +	vert_calls++;
 155.415 +	num_verts++;
 155.416 +
 155.417 +	if(prim == GL_QUADS) {
 155.418 +		/* leave space for 6 more worst-case and don't allow flushes mid-quad */
 155.419 +		buffer_full = num_verts >= MAX_VERTS - 6 && vert_calls % 4 == 0;
 155.420 +	} else {
 155.421 +		buffer_full = num_verts >= MAX_VERTS - prim;
 155.422 +	}
 155.423 +
 155.424 +	if(buffer_full) {
 155.425 +		gl_draw_immediate();
 155.426 +		gl_begin(prim);	/* reset everything */
 155.427 +	}
 155.428 +}
 155.429 +
 155.430 +
 155.431 +void gl_normal3f(float x, float y, float z)
 155.432 +{
 155.433 +	cur_normal.x = x;
 155.434 +	cur_normal.y = y;
 155.435 +	cur_normal.z = z;
 155.436 +}
 155.437 +
 155.438 +
 155.439 +void gl_color3f(float r, float g, float b)
 155.440 +{
 155.441 +	cur_color.x = r;
 155.442 +	cur_color.y = g;
 155.443 +	cur_color.z = b;
 155.444 +	cur_color.w = 1.0f;
 155.445 +}
 155.446 +
 155.447 +void gl_color4f(float r, float g, float b, float a)
 155.448 +{
 155.449 +	cur_color.x = r;
 155.450 +	cur_color.y = g;
 155.451 +	cur_color.z = b;
 155.452 +	cur_color.w = a;
 155.453 +}
 155.454 +
 155.455 +
 155.456 +void gl_texcoord1f(float s)
 155.457 +{
 155.458 +	cur_texcoord.x = s;
 155.459 +	cur_texcoord.y = 0.0f;
 155.460 +}
 155.461 +
 155.462 +void gl_texcoord2f(float s, float t)
 155.463 +{
 155.464 +	cur_texcoord.x = s;
 155.465 +	cur_texcoord.y = t;
 155.466 +}
 155.467 +
 155.468 +void gl_vertex_attrib2f(int loc, float x, float y)
 155.469 +{
 155.470 +	aloc = loc;
 155.471 +	cur_attrib.x = x;
 155.472 +	cur_attrib.y = y;
 155.473 +	cur_attrib.z = 0.0f;
 155.474 +	cur_attrib.w = 1.0f;
 155.475 +}
 155.476 +
 155.477 +void gl_vertex_attrib3f(int loc, float x, float y, float z)
 155.478 +{
 155.479 +	aloc = loc;
 155.480 +	cur_attrib.x = x;
 155.481 +	cur_attrib.y = y;
 155.482 +	cur_attrib.z = z;
 155.483 +	cur_attrib.w = 1.0f;
 155.484 +}
 155.485 +
 155.486 +void gl_vertex_attrib4f(int loc, float x, float y, float z, float w)
 155.487 +{
 155.488 +	aloc = loc;
 155.489 +	cur_attrib.x = x;
 155.490 +	cur_attrib.y = y;
 155.491 +	cur_attrib.z = z;
 155.492 +	cur_attrib.w = w;
 155.493 +}
   156.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   156.2 +++ b/src/sanegl.h	Sat Sep 19 05:51:51 2015 +0300
   156.3 @@ -0,0 +1,114 @@
   156.4 +/*
   156.5 +SaneGL - a small library to bring back sanity to OpenGL ES 2.x
   156.6 +Copyright (C) 2011  John Tsiombikas <nuclear@member.fsf.org>
   156.7 +
   156.8 +This program is free software: you can redistribute it and/or modify
   156.9 +it under the terms of the GNU General Public License as published by
  156.10 +the Free Software Foundation, either version 3 of the License, or
  156.11 +(at your option) any later version.
  156.12 +
  156.13 +This program is distributed in the hope that it will be useful,
  156.14 +but WITHOUT ANY WARRANTY; without even the implied warranty of
  156.15 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  156.16 +GNU General Public License for more details.
  156.17 +
  156.18 +You should have received a copy of the GNU General Public License
  156.19 +along with this program.  If not, see <http://www.gnu.org/licenses/>.
  156.20 +*/
  156.21 +
  156.22 +#ifndef SANEGL_H_
  156.23 +#define SANEGL_H_
  156.24 +
  156.25 +#include "opengl.h"
  156.26 +
  156.27 +#ifndef GL_MODELVIEW
  156.28 +#define GL_MODELVIEW		0x1700
  156.29 +#endif
  156.30 +#ifndef GL_PROJECTION
  156.31 +#define GL_PROJECTION		0x1701
  156.32 +#endif
  156.33 +#ifndef GL_TEXTURE
  156.34 +#define GL_TEXTURE			0x1702
  156.35 +#endif
  156.36 +
  156.37 +#ifndef GL_POINTS
  156.38 +#define GL_POINTS			0
  156.39 +#endif
  156.40 +#ifndef GL_LINES
  156.41 +#define GL_LINES			1
  156.42 +#endif
  156.43 +#ifndef GL_TRIANGLES
  156.44 +#define GL_TRIANGLES		4
  156.45 +#endif
  156.46 +#ifndef GL_QUADS
  156.47 +#define GL_QUADS			7
  156.48 +#endif
  156.49 +
  156.50 +#ifdef GLDEF
  156.51 +
  156.52 +#define glMatrixMode		gl_matrix_mode
  156.53 +#define glPushMatrix		gl_push_matrix
  156.54 +#define glPopMatrix			gl_pop_matrix
  156.55 +#define glLoadIdentity		gl_load_identity
  156.56 +#define glLoadMatrixf		gl_load_matrixf
  156.57 +#define glMultMatrixf		gl_mult_matrixf
  156.58 +#define glTranslatef		gl_translatef
  156.59 +#define glRotatef			gl_rotatef
  156.60 +#define glScalef			gl_scalef
  156.61 +#define glOrtho				gl_ortho
  156.62 +#define glFrustum			gl_frustum
  156.63 +#define gluPerspective		glu_perspective
  156.64 +
  156.65 +#define glBegin				gl_begin
  156.66 +#define glEnd				gl_end
  156.67 +#define glVertex2f			gl_vertex2f
  156.68 +#define glVertex3f			gl_vertex3f
  156.69 +#define glVertex4f			gl_vertex4f
  156.70 +#define glNormal3f			gl_normal3f
  156.71 +#define glColor3f			gl_color3f
  156.72 +#define glColor4f			gl_color4f
  156.73 +#define glTexCoord1f		gl_texcoord1f
  156.74 +#define glTexCoord2f		gl_texcoord2f
  156.75 +#define glVertexAttrib2f	gl_vertex_attrib2f
  156.76 +#define glVertexAttrib3f	gl_vertex_attrib3f
  156.77 +#define glVertexAttrib4f	gl_vertex_attrib4f
  156.78 +#endif
  156.79 +
  156.80 +/* matrix stuff */
  156.81 +void gl_matrix_mode(int mmode);
  156.82 +void gl_push_matrix(void);
  156.83 +void gl_pop_matrix(void);
  156.84 +void gl_load_identity(void);
  156.85 +void gl_load_matrixf(const float *mat);
  156.86 +void gl_mult_matrixf(const float *mat);
  156.87 +void gl_translatef(float x, float y, float z);
  156.88 +void gl_rotatef(float angle, float x, float y, float z);
  156.89 +void gl_scalef(float x, float y, float z);
  156.90 +void gl_ortho(float left, float right, float bottom, float top, float near, float far);
  156.91 +void gl_frustum(float left, float right, float bottom, float top, float near, float far);
  156.92 +void glu_perspective(float vfov, float aspect, float near, float far);
  156.93 +
  156.94 +void gl_apply_xform(unsigned int prog);
  156.95 +
  156.96 +
  156.97 +/* immediate mode rendering */
  156.98 +void gl_begin(int prim);
  156.99 +void gl_end(void);
 156.100 +
 156.101 +void gl_vertex2f(float x, float y);
 156.102 +void gl_vertex3f(float x, float y, float z);
 156.103 +void gl_vertex4f(float x, float y, float z, float w);
 156.104 +
 156.105 +void gl_normal3f(float x, float y, float z);
 156.106 +
 156.107 +void gl_color3f(float r, float g, float b);
 156.108 +void gl_color4f(float r, float g, float b, float a);
 156.109 +
 156.110 +void gl_texcoord1f(float s);
 156.111 +void gl_texcoord2f(float s, float t);
 156.112 +
 156.113 +void gl_vertex_attrib2f(int loc, float x, float y);
 156.114 +void gl_vertex_attrib3f(int loc, float x, float y, float z);
 156.115 +void gl_vertex_attrib4f(int loc, float x, float y, float z, float w);
 156.116 +
 156.117 +#endif	/* SANEGL_H_ */
   157.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   157.2 +++ b/src/sdr.c	Sat Sep 19 05:51:51 2015 +0300
   157.3 @@ -0,0 +1,341 @@
   157.4 +/*
   157.5 +Stereoscopic tunnel for iOS.
   157.6 +Copyright (C) 2011  John Tsiombikas <nuclear@member.fsf.org>
   157.7 +
   157.8 +This program is free software: you can redistribute it and/or modify
   157.9 +it under the terms of the GNU General Public License as published by
  157.10 +the Free Software Foundation, either version 3 of the License, or
  157.11 +(at your option) any later version.
  157.12 +
  157.13 +This program is distributed in the hope that it will be useful,
  157.14 +but WITHOUT ANY WARRANTY; without even the implied warranty of
  157.15 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  157.16 +GNU General Public License for more details.
  157.17 +
  157.18 +You should have received a copy of the GNU General Public License
  157.19 +along with this program.  If not, see <http://www.gnu.org/licenses/>.
  157.20 +*/
  157.21 +
  157.22 +#include <stdio.h>
  157.23 +#include <stdlib.h>
  157.24 +#include <string.h>
  157.25 +#include <errno.h>
  157.26 +#include <assert.h>
  157.27 +#include "opengl.h"
  157.28 +
  157.29 +#if defined(unix) || defined(__unix__)
  157.30 +#include <unistd.h>
  157.31 +#include <sys/stat.h>
  157.32 +#endif	/* unix */
  157.33 +
  157.34 +#include "sdr.h"
  157.35 +
  157.36 +
  157.37 +unsigned int create_vertex_shader(const char *src)
  157.38 +{
  157.39 +	return create_shader(src, GL_VERTEX_SHADER);
  157.40 +}
  157.41 +
  157.42 +unsigned int create_pixel_shader(const char *src)
  157.43 +{
  157.44 +	return create_shader(src, GL_FRAGMENT_SHADER);
  157.45 +}
  157.46 +
  157.47 +unsigned int create_shader(const char *src, unsigned int sdr_type)
  157.48 +{
  157.49 +	unsigned int sdr;
  157.50 +	int success, info_len;
  157.51 +	char *info_str = 0;
  157.52 +	GLenum err;
  157.53 +
  157.54 +	sdr = glCreateShader(sdr_type);
  157.55 +	assert(glGetError() == GL_NO_ERROR);
  157.56 +	glShaderSource(sdr, 1, &src, 0);
  157.57 +	err = glGetError();
  157.58 +	assert(err == GL_NO_ERROR);
  157.59 +	glCompileShader(sdr);
  157.60 +	assert(glGetError() == GL_NO_ERROR);
  157.61 +
  157.62 +	glGetShaderiv(sdr, GL_COMPILE_STATUS, &success);
  157.63 +	assert(glGetError() == GL_NO_ERROR);
  157.64 +	glGetShaderiv(sdr, GL_INFO_LOG_LENGTH, &info_len);
  157.65 +	assert(glGetError() == GL_NO_ERROR);
  157.66 +
  157.67 +	if(info_len) {
  157.68 +		if((info_str = malloc(info_len + 1))) {
  157.69 +			glGetShaderInfoLog(sdr, info_len, 0, info_str);
  157.70 +			assert(glGetError() == GL_NO_ERROR);
  157.71 +		}
  157.72 +	}
  157.73 +
  157.74 +	if(success) {
  157.75 +		fprintf(stderr, info_str ? "done: %s\n" : "done\n", info_str);
  157.76 +	} else {
  157.77 +		fprintf(stderr, info_str ? "failed: %s\n" : "failed\n", info_str);
  157.78 +		glDeleteShader(sdr);
  157.79 +		sdr = 0;
  157.80 +	}
  157.81 +
  157.82 +	free(info_str);
  157.83 +	return sdr;
  157.84 +}
  157.85 +
  157.86 +void free_shader(unsigned int sdr)
  157.87 +{
  157.88 +	glDeleteShader(sdr);
  157.89 +}
  157.90 +
  157.91 +unsigned int load_vertex_shader(const char *fname)
  157.92 +{
  157.93 +	return load_shader(fname, GL_VERTEX_SHADER);
  157.94 +}
  157.95 +
  157.96 +unsigned int load_pixel_shader(const char *fname)
  157.97 +{
  157.98 +	return load_shader(fname, GL_FRAGMENT_SHADER);
  157.99 +}
 157.100 +
 157.101 +unsigned int load_shader(const char *fname, unsigned int sdr_type)
 157.102 +{
 157.103 +#if defined(unix) || defined(__unix__)
 157.104 +	struct stat st;
 157.105 +#endif
 157.106 +	unsigned int sdr;
 157.107 +	size_t filesize;
 157.108 +	FILE *fp;
 157.109 +	char *src;
 157.110 +
 157.111 +	if(!(fp = fopen(fname, "r"))) {
 157.112 +		fprintf(stderr, "failed to open shader %s: %s\n", fname, strerror(errno));
 157.113 +		return 0;
 157.114 +	}
 157.115 +
 157.116 +#if defined(unix) || defined(__unix__)
 157.117 +	fstat(fileno(fp), &st);
 157.118 +	filesize = st.st_size;
 157.119 +#else
 157.120 +	fseek(fp, 0, SEEK_END);
 157.121 +	filesize = ftell(fp);
 157.122 +	fseek(fp, 0, SEEK_SET);
 157.123 +#endif	/* unix */
 157.124 +
 157.125 +	if(!(src = malloc(filesize + 1))) {
 157.126 +		fclose(fp);
 157.127 +		return 0;
 157.128 +	}
 157.129 +	fread(src, 1, filesize, fp);
 157.130 +	src[filesize] = 0;
 157.131 +	fclose(fp);
 157.132 +
 157.133 +	fprintf(stderr, "compiling %s shader: %s... ", (sdr_type == GL_VERTEX_SHADER ? "vertex" : "pixel"), fname);
 157.134 +	sdr = create_shader(src, sdr_type);
 157.135 +
 157.136 +	free(src);
 157.137 +	return sdr;
 157.138 +}
 157.139 +
 157.140 +
 157.141 +unsigned int get_vertex_shader(const char *fname)
 157.142 +{
 157.143 +	return get_shader(fname, GL_VERTEX_SHADER);
 157.144 +}
 157.145 +
 157.146 +unsigned int get_pixel_shader(const char *fname)
 157.147 +{
 157.148 +	return get_shader(fname, GL_FRAGMENT_SHADER);
 157.149 +}
 157.150 +
 157.151 +unsigned int get_shader(const char *fname, unsigned int sdr_type)
 157.152 +{
 157.153 +	unsigned int sdr;
 157.154 +
 157.155 +	if(!fname || !(sdr = load_shader(fname, sdr_type))) {
 157.156 +		return 0;
 157.157 +	}
 157.158 +	return sdr;
 157.159 +}
 157.160 +
 157.161 +
 157.162 +/* ---- gpu programs ---- */
 157.163 +
 157.164 +unsigned int create_program(void)
 157.165 +{
 157.166 +	unsigned int prog = glCreateProgram();
 157.167 +	assert(glGetError() == GL_NO_ERROR);
 157.168 +	return prog;
 157.169 +}
 157.170 +
 157.171 +unsigned int create_program_link(unsigned int vs, unsigned int ps)
 157.172 +{
 157.173 +	unsigned int prog;
 157.174 +
 157.175 +	if(!(prog = create_program())) {
 157.176 +		return 0;
 157.177 +	}
 157.178 +
 157.179 +	attach_shader(prog, vs);
 157.180 +	assert(glGetError() == GL_NO_ERROR);
 157.181 +	attach_shader(prog, ps);
 157.182 +	assert(glGetError() == GL_NO_ERROR);
 157.183 +
 157.184 +	if(link_program(prog) == -1) {
 157.185 +		free_program(prog);
 157.186 +		return 0;
 157.187 +	}
 157.188 +	return prog;
 157.189 +}
 157.190 +
 157.191 +unsigned int create_program_load(const char *vfile, const char *pfile)
 157.192 +{
 157.193 +	unsigned int vs, ps;
 157.194 +
 157.195 +	if(!(vs = get_vertex_shader(vfile)) || !(ps = get_pixel_shader(pfile))) {
 157.196 +		return 0;
 157.197 +	}
 157.198 +	return create_program_link(vs, ps);
 157.199 +}
 157.200 +
 157.201 +void free_program(unsigned int sdr)
 157.202 +{
 157.203 +	glDeleteProgram(sdr);
 157.204 +}
 157.205 +
 157.206 +void attach_shader(unsigned int prog, unsigned int sdr)
 157.207 +{
 157.208 +	glAttachShader(prog, sdr);
 157.209 +	assert(glGetError() == GL_NO_ERROR);
 157.210 +}
 157.211 +
 157.212 +int link_program(unsigned int prog)
 157.213 +{
 157.214 +	int linked, info_len, retval = 0;
 157.215 +	char *info_str = 0;
 157.216 +
 157.217 +	glLinkProgram(prog);
 157.218 +	assert(glGetError() == GL_NO_ERROR);
 157.219 +	glGetProgramiv(prog, GL_LINK_STATUS, &linked);
 157.220 +	assert(glGetError() == GL_NO_ERROR);
 157.221 +	glGetProgramiv(prog, GL_INFO_LOG_LENGTH, &info_len);
 157.222 +	assert(glGetError() == GL_NO_ERROR);
 157.223 +
 157.224 +	if(info_len) {
 157.225 +		if((info_str = malloc(info_len + 1))) {
 157.226 +			glGetProgramInfoLog(prog, info_len, 0, info_str);
 157.227 +			assert(glGetError() == GL_NO_ERROR);
 157.228 +		}
 157.229 +	}
 157.230 +
 157.231 +	if(linked) {
 157.232 +		fprintf(stderr, info_str ? "linking done: %s\n" : "linking done\n", info_str);
 157.233 +	} else {
 157.234 +		fprintf(stderr, info_str ? "linking failed: %s\n" : "linking failed\n", info_str);
 157.235 +		retval = -1;
 157.236 +	}
 157.237 +
 157.238 +	free(info_str);
 157.239 +	return retval;
 157.240 +}
 157.241 +
 157.242 +int bind_program(unsigned int prog)
 157.243 +{
 157.244 +	GLenum err;
 157.245 +
 157.246 +	glUseProgram(prog);
 157.247 +	if(prog && (err = glGetError()) != GL_NO_ERROR) {
 157.248 +		/* maybe the program is not linked, try linking first */
 157.249 +		if(err == GL_INVALID_OPERATION) {
 157.250 +			if(link_program(prog) == -1) {
 157.251 +				return -1;
 157.252 +			}
 157.253 +			glUseProgram(prog);
 157.254 +			return glGetError() == GL_NO_ERROR ? 0 : -1;
 157.255 +		}
 157.256 +		return -1;
 157.257 +	}
 157.258 +	return 0;
 157.259 +}
 157.260 +
 157.261 +/* ugly but I'm not going to write the same bloody code over and over */
 157.262 +#define BEGIN_UNIFORM_CODE \
 157.263 +	int loc, curr_prog; \
 157.264 +	glGetIntegerv(GL_CURRENT_PROGRAM, &curr_prog); \
 157.265 +	if(curr_prog != prog && bind_program(prog) == -1) { \
 157.266 +		return -1; \
 157.267 +	} \
 157.268 +	if((loc = glGetUniformLocation(prog, name)) != -1)
 157.269 +
 157.270 +#define END_UNIFORM_CODE \
 157.271 +	if(curr_prog != prog) { \
 157.272 +		bind_program(curr_prog); \
 157.273 +	} \
 157.274 +	return loc == -1 ? -1 : 0
 157.275 +
 157.276 +int set_uniform_int(unsigned int prog, const char *name, int val)
 157.277 +{
 157.278 +	BEGIN_UNIFORM_CODE {
 157.279 +		glUniform1i(loc, val);
 157.280 +	}
 157.281 +	END_UNIFORM_CODE;
 157.282 +}
 157.283 +
 157.284 +int set_uniform_float(unsigned int prog, const char *name, float val)
 157.285 +{
 157.286 +	BEGIN_UNIFORM_CODE {
 157.287 +		glUniform1f(loc, val);
 157.288 +	}
 157.289 +	END_UNIFORM_CODE;
 157.290 +}
 157.291 +
 157.292 +int set_uniform_float3(unsigned int prog, const char *name, float x, float y, float z)
 157.293 +{
 157.294 +	BEGIN_UNIFORM_CODE {
 157.295 +		glUniform3f(loc, x, y, z);
 157.296 +	}
 157.297 +	END_UNIFORM_CODE;
 157.298 +}
 157.299 +
 157.300 +int set_uniform_float4(unsigned int prog, const char *name, float x, float y, float z, float w)
 157.301 +{
 157.302 +	BEGIN_UNIFORM_CODE {
 157.303 +		glUniform4f(loc, x, y, z, w);
 157.304 +	}
 157.305 +	END_UNIFORM_CODE;
 157.306 +}
 157.307 +
 157.308 +int set_uniform_matrix4(unsigned int prog, const char *name, float *mat)
 157.309 +{
 157.310 +	BEGIN_UNIFORM_CODE {
 157.311 +		glUniformMatrix4fv(loc, 1, GL_FALSE, mat);
 157.312 +	}
 157.313 +	END_UNIFORM_CODE;
 157.314 +}
 157.315 +
 157.316 +int set_uniform_matrix4_transposed(unsigned int prog, const char *name, float *mat)
 157.317 +{
 157.318 +	BEGIN_UNIFORM_CODE {
 157.319 +		glUniformMatrix4fv(loc, 1, GL_TRUE, mat);
 157.320 +	}
 157.321 +	END_UNIFORM_CODE;
 157.322 +}
 157.323 +
 157.324 +int get_attrib_loc(unsigned int prog, const char *name)
 157.325 +{
 157.326 +	int loc, curr_prog;
 157.327 +
 157.328 +	glGetIntegerv(GL_CURRENT_PROGRAM, &curr_prog);
 157.329 +	if(curr_prog != prog && bind_program(prog) == -1) {
 157.330 +		return -1;
 157.331 +	}
 157.332 +
 157.333 +	loc = glGetAttribLocation(prog, (char*)name);
 157.334 +
 157.335 +	if(curr_prog != prog) {
 157.336 +		bind_program(curr_prog);
 157.337 +	}
 157.338 +	return loc;
 157.339 +}
 157.340 +
 157.341 +void set_attrib_float3(int attr_loc, float x, float y, float z)
 157.342 +{
 157.343 +	glVertexAttrib3f(attr_loc, x, y, z);
 157.344 +}
   158.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   158.2 +++ b/src/sdr.h	Sat Sep 19 05:51:51 2015 +0300
   158.3 @@ -0,0 +1,67 @@
   158.4 +/*
   158.5 +Stereoscopic tunnel for iOS.
   158.6 +Copyright (C) 2011  John Tsiombikas <nuclear@member.fsf.org>
   158.7 +
   158.8 +This program is free software: you can redistribute it and/or modify
   158.9 +it under the terms of the GNU General Public License as published by
  158.10 +the Free Software Foundation, either version 3 of the License, or
  158.11 +(at your option) any later version.
  158.12 +
  158.13 +This program is distributed in the hope that it will be useful,
  158.14 +but WITHOUT ANY WARRANTY; without even the implied warranty of
  158.15 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  158.16 +GNU General Public License for more details.
  158.17 +
  158.18 +You should have received a copy of the GNU General Public License
  158.19 +along with this program.  If not, see <http://www.gnu.org/licenses/>.
  158.20 +*/
  158.21 +
  158.22 +#ifndef SDR_H_
  158.23 +#define SDR_H_
  158.24 +
  158.25 +#ifdef __cplusplus
  158.26 +extern "C" {
  158.27 +#endif	/* __cplusplus */
  158.28 +
  158.29 +/* ---- shaders ---- */
  158.30 +unsigned int create_vertex_shader(const char *src);
  158.31 +unsigned int create_pixel_shader(const char *src);
  158.32 +unsigned int create_shader(const char *src, unsigned int sdr_type);
  158.33 +void free_shader(unsigned int sdr);
  158.34 +
  158.35 +unsigned int load_vertex_shader(const char *fname);
  158.36 +unsigned int load_pixel_shader(const char *fname);
  158.37 +unsigned int load_shader(const char *src, unsigned int sdr_type);
  158.38 +
  158.39 +unsigned int get_vertex_shader(const char *fname);
  158.40 +unsigned int get_pixel_shader(const char *fname);
  158.41 +unsigned int get_shader(const char *fname, unsigned int sdr_type);
  158.42 +
  158.43 +int add_shader(const char *fname, unsigned int sdr);
  158.44 +int remove_shader(const char *fname);
  158.45 +
  158.46 +/* ---- gpu programs ---- */
  158.47 +unsigned int create_program(void);
  158.48 +unsigned int create_program_link(unsigned int vs, unsigned int ps);
  158.49 +unsigned int create_program_load(const char *vfile, const char *pfile);
  158.50 +void free_program(unsigned int sdr);
  158.51 +
  158.52 +void attach_shader(unsigned int prog, unsigned int sdr);
  158.53 +int link_program(unsigned int prog);
  158.54 +int bind_program(unsigned int prog);
  158.55 +
  158.56 +int set_uniform_int(unsigned int prog, const char *name, int val);
  158.57 +int set_uniform_float(unsigned int prog, const char *name, float val);
  158.58 +int set_uniform_float3(unsigned int prog, const char *name, float x, float y, float z);
  158.59 +int set_uniform_float4(unsigned int prog, const char *name, float x, float y, float z, float w);
  158.60 +int set_uniform_matrix4(unsigned int prog, const char *name, float *mat);
  158.61 +int set_uniform_matrix4_transposed(unsigned int prog, const char *name, float *mat);
  158.62 +
  158.63 +int get_attrib_loc(unsigned int prog, const char *name);
  158.64 +void set_attrib_float3(int attr_loc, float x, float y, float z);
  158.65 +
  158.66 +#ifdef __cplusplus
  158.67 +}
  158.68 +#endif	/* __cplusplus */
  158.69 +
  158.70 +#endif	/* SDR_H_ */
   159.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   159.2 +++ b/src/tex.c	Sat Sep 19 05:51:51 2015 +0300
   159.3 @@ -0,0 +1,67 @@
   159.4 +/*
   159.5 +Stereoscopic tunnel for iOS.
   159.6 +Copyright (C) 2011  John Tsiombikas <nuclear@member.fsf.org>
   159.7 +
   159.8 +This program is free software: you can redistribute it and/or modify
   159.9 +it under the terms of the GNU General Public License as published by
  159.10 +the Free Software Foundation, either version 3 of the License, or
  159.11 +(at your option) any later version.
  159.12 +
  159.13 +This program is distributed in the hope that it will be useful,
  159.14 +but WITHOUT ANY WARRANTY; without even the implied warranty of
  159.15 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  159.16 +GNU General Public License for more details.
  159.17 +
  159.18 +You should have received a copy of the GNU General Public License
  159.19 +along with this program.  If not, see <http://www.gnu.org/licenses/>.
  159.20 +*/
  159.21 +
  159.22 +#include <stdio.h>
  159.23 +#include <stdlib.h>
  159.24 +#include <string.h>
  159.25 +#include <errno.h>
  159.26 +#include "opengl.h"
  159.27 +#include "tex.h"
  159.28 +#include "config.h"
  159.29 +#include "imago2.h"
  159.30 +
  159.31 +unsigned int load_texture(const char *fname)
  159.32 +{
  159.33 +	int xsz, ysz;
  159.34 +	unsigned int tex;
  159.35 +	void *pixels;
  159.36 +
  159.37 +	if(!fname) {
  159.38 +		return 0;
  159.39 +	}
  159.40 +	if(!(pixels = img_load_pixels(fname, &xsz, &ysz, IMG_FMT_RGBA32))) {
  159.41 +		fprintf(stderr, "failed to load image: %s\n", fname);
  159.42 +		return 0;
  159.43 +	}
  159.44 +
  159.45 +	glGenTextures(1, &tex);
  159.46 +	glBindTexture(GL_TEXTURE_2D, tex);
  159.47 +	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  159.48 +	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  159.49 +	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  159.50 +	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  159.51 +	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, xsz, ysz, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
  159.52 +	img_free_pixels(pixels);
  159.53 +
  159.54 +	return tex;
  159.55 +}
  159.56 +
  159.57 +void bind_texture(unsigned int tex, int unit)
  159.58 +{
  159.59 +	glActiveTexture(GL_TEXTURE0 + unit);
  159.60 +
  159.61 +#ifndef IPHONE
  159.62 +	if(tex) {
  159.63 +		glEnable(GL_TEXTURE_2D);
  159.64 +	} else {
  159.65 +		glDisable(GL_TEXTURE_2D);
  159.66 +	}
  159.67 +#endif
  159.68 +
  159.69 +	glBindTexture(GL_TEXTURE_2D, tex);
  159.70 +}
   160.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   160.2 +++ b/src/tex.h	Sat Sep 19 05:51:51 2015 +0300
   160.3 @@ -0,0 +1,26 @@
   160.4 +/*
   160.5 +Stereoscopic tunnel for iOS.
   160.6 +Copyright (C) 2011  John Tsiombikas <nuclear@member.fsf.org>
   160.7 +
   160.8 +This program is free software: you can redistribute it and/or modify
   160.9 +it under the terms of the GNU General Public License as published by
  160.10 +the Free Software Foundation, either version 3 of the License, or
  160.11 +(at your option) any later version.
  160.12 +
  160.13 +This program is distributed in the hope that it will be useful,
  160.14 +but WITHOUT ANY WARRANTY; without even the implied warranty of
  160.15 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  160.16 +GNU General Public License for more details.
  160.17 +
  160.18 +You should have received a copy of the GNU General Public License
  160.19 +along with this program.  If not, see <http://www.gnu.org/licenses/>.
  160.20 +*/
  160.21 +
  160.22 +
  160.23 +#ifndef TEX_H_
  160.24 +#define TEX_H_
  160.25 +
  160.26 +unsigned int load_texture(const char *fname);
  160.27 +void bind_texture(unsigned int tex, int unit);
  160.28 +
  160.29 +#endif	/* TEX_H_ */
   161.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   161.2 +++ b/src/ui.h	Sat Sep 19 05:51:51 2015 +0300
   161.3 @@ -0,0 +1,41 @@
   161.4 +/*
   161.5 +Stereoscopic tunnel for iOS.
   161.6 +Copyright (C) 2011  John Tsiombikas <nuclear@member.fsf.org>
   161.7 +
   161.8 +This program is free software: you can redistribute it and/or modify
   161.9 +it under the terms of the GNU General Public License as published by
  161.10 +the Free Software Foundation, either version 3 of the License, or
  161.11 +(at your option) any later version.
  161.12 +
  161.13 +This program is distributed in the hope that it will be useful,
  161.14 +but WITHOUT ANY WARRANTY; without even the implied warranty of
  161.15 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  161.16 +GNU General Public License for more details.
  161.17 +
  161.18 +You should have received a copy of the GNU General Public License
  161.19 +along with this program.  If not, see <http://www.gnu.org/licenses/>.
  161.20 +*/
  161.21 +
  161.22 +
  161.23 +/* UI and shit */
  161.24 +
  161.25 +#import <UIKit/UIKit.h>
  161.26 +
  161.27 +@interface UI : UIViewController {
  161.28 +	IBOutlet UISwitch *sw_stereo;
  161.29 +	IBOutlet UISlider *slider_split;
  161.30 +	IBOutlet UISegmentedControl *grp_mode;
  161.31 +	IBOutlet UIButton *bn_done;
  161.32 +}
  161.33 +
  161.34 +@property (nonatomic, retain) IBOutlet UISwitch *sw_stereo;
  161.35 +@property (nonatomic, retain) IBOutlet UISlider *slider_split;
  161.36 +@property (nonatomic, retain) IBOutlet UISegmentedControl *grp_mode;
  161.37 +@property (nonatomic, retain) IBOutlet UIButton *bn_done;
  161.38 +
  161.39 +-(IBAction) done_clicked: (id) sender;
  161.40 +-(IBAction) split_changed: (id) sender;
  161.41 +-(IBAction) stereo_changed: (id) sender;
  161.42 +-(IBAction) mode_changed: (id) sender;
  161.43 +
  161.44 +@end
   162.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   162.2 +++ b/src/ui.xib	Sat Sep 19 05:51:51 2015 +0300
   162.3 @@ -0,0 +1,778 @@
   162.4 +<?xml version="1.0" encoding="UTF-8"?>
   162.5 +<archive type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="7.10">
   162.6 +	<data>
   162.7 +		<int key="IBDocument.SystemTarget">1056</int>
   162.8 +		<string key="IBDocument.SystemVersion">10K549</string>
   162.9 +		<string key="IBDocument.InterfaceBuilderVersion">851</string>
  162.10 +		<string key="IBDocument.AppKitVersion">1038.36</string>
  162.11 +		<string key="IBDocument.HIToolboxVersion">461.00</string>
  162.12 +		<object class="NSMutableDictionary" key="IBDocument.PluginVersions">
  162.13 +			<string key="NS.key.0">com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
  162.14 +			<string key="NS.object.0">141</string>
  162.15 +		</object>
  162.16 +		<object class="NSMutableArray" key="IBDocument.EditedObjectIDs">
  162.17 +			<bool key="EncodedWithXMLCoder">YES</bool>
  162.18 +			<integer value="1"/>
  162.19 +		</object>
  162.20 +		<object class="NSArray" key="IBDocument.PluginDependencies">
  162.21 +			<bool key="EncodedWithXMLCoder">YES</bool>
  162.22 +			<string>com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
  162.23 +		</object>
  162.24 +		<object class="NSMutableDictionary" key="IBDocument.Metadata">
  162.25 +			<bool key="EncodedWithXMLCoder">YES</bool>
  162.26 +			<object class="NSArray" key="dict.sortedKeys" id="0">
  162.27 +				<bool key="EncodedWithXMLCoder">YES</bool>
  162.28 +			</object>
  162.29 +			<object class="NSMutableArray" key="dict.values">
  162.30 +				<bool key="EncodedWithXMLCoder">YES</bool>
  162.31 +			</object>
  162.32 +		</object>
  162.33 +		<object class="NSMutableArray" key="IBDocument.RootObjects" id="1000">
  162.34 +			<bool key="EncodedWithXMLCoder">YES</bool>
  162.35 +			<object class="IBProxyObject" id="372490531">
  162.36 +				<string key="IBProxiedObjectIdentifier">IBFilesOwner</string>
  162.37 +				<string key="targetRuntimeIdentifier">IBCocoaTouchFramework</string>
  162.38 +			</object>
  162.39 +			<object class="IBProxyObject" id="975951072">
  162.40 +				<string key="IBProxiedObjectIdentifier">IBFirstResponder</string>
  162.41 +				<string key="targetRuntimeIdentifier">IBCocoaTouchFramework</string>
  162.42 +			</object>
  162.43 +			<object class="IBUIView" id="191373211">
  162.44 +				<reference key="NSNextResponder"/>
  162.45 +				<int key="NSvFlags">278</int>
  162.46 +				<object class="NSMutableArray" key="NSSubviews">
  162.47 +					<bool key="EncodedWithXMLCoder">YES</bool>
  162.48 +					<object class="IBUISwitch" id="5150575">
  162.49 +						<reference key="NSNextResponder" ref="191373211"/>
  162.50 +						<int key="NSvFlags">292</int>
  162.51 +						<string key="NSFrame">{{366, 20}, {94, 27}}</string>
  162.52 +						<reference key="NSSuperview" ref="191373211"/>
  162.53 +						<bool key="IBUIOpaque">NO</bool>
  162.54 +						<string key="targetRuntimeIdentifier">IBCocoaTouchFramework</string>
  162.55 +						<int key="IBUIContentHorizontalAlignment">0</int>
  162.56 +						<int key="IBUIContentVerticalAlignment">0</int>
  162.57 +						<bool key="IBUIOn">YES</bool>
  162.58 +					</object>
  162.59 +					<object class="IBUISegmentedControl" id="694841059">
  162.60 +						<reference key="NSNextResponder" ref="191373211"/>
  162.61 +						<int key="NSvFlags">292</int>
  162.62 +						<string key="NSFrame">{{172, 55}, {288, 44}}</string>
  162.63 +						<reference key="NSSuperview" ref="191373211"/>
  162.64 +						<bool key="IBUIOpaque">NO</bool>
  162.65 +						<string key="targetRuntimeIdentifier">IBCocoaTouchFramework</string>
  162.66 +						<int key="IBNumberOfSegments">2</int>
  162.67 +						<int key="IBSelectedSegmentIndex">0</int>
  162.68 +						<object class="NSArray" key="IBSegmentTitles">
  162.69 +							<bool key="EncodedWithXMLCoder">YES</bool>
  162.70 +							<string>Simple</string>
  162.71 +							<string>Bump-mapped</string>
  162.72 +						</object>
  162.73 +						<object class="NSMutableArray" key="IBSegmentWidths">
  162.74 +							<bool key="EncodedWithXMLCoder">YES</bool>
  162.75 +							<real value="0.0"/>
  162.76 +							<real value="0.0"/>
  162.77 +						</object>
  162.78 +						<object class="NSMutableArray" key="IBSegmentEnabledStates">
  162.79 +							<bool key="EncodedWithXMLCoder">YES</bool>
  162.80 +							<boolean value="YES"/>
  162.81 +							<boolean value="YES"/>
  162.82 +						</object>
  162.83 +						<object class="NSMutableArray" key="IBSegmentContentOffsets">
  162.84 +							<bool key="EncodedWithXMLCoder">YES</bool>
  162.85 +							<string>{0, 0}</string>
  162.86 +							<string>{0, 0}</string>
  162.87 +						</object>
  162.88 +						<object class="NSMutableArray" key="IBSegmentImages">
  162.89 +							<bool key="EncodedWithXMLCoder">YES</bool>
  162.90 +							<object class="NSNull" id="4"/>
  162.91 +							<reference ref="4"/>
  162.92 +						</object>
  162.93 +					</object>
  162.94 +					<object class="IBUILabel" id="799767528">
  162.95 +						<reference key="NSNextResponder" ref="191373211"/>
  162.96 +						<int key="NSvFlags">292</int>
  162.97 +						<string key="NSFrame">{{182, 23}, {176, 21}}</string>
  162.98 +						<reference key="NSSuperview" ref="191373211"/>
  162.99 +						<bool key="IBUIOpaque">NO</bool>
 162.100 +						<bool key="IBUIClipsSubviews">YES</bool>
 162.101 +						<int key="IBUIContentMode">7</int>
 162.102 +						<bool key="IBUIUserInteractionEnabled">NO</bool>
 162.103 +						<string key="targetRuntimeIdentifier">IBCocoaTouchFramework</string>
 162.104 +						<string key="IBUIText">Stereoscopic rendering</string>
 162.105 +						<object class="NSColor" key="IBUITextColor" id="621632613">
 162.106 +							<int key="NSColorSpace">3</int>
 162.107 +							<bytes key="NSWhite">MQA</bytes>
 162.108 +						</object>
 162.109 +						<reference key="IBUIHighlightedColor" ref="621632613"/>
 162.110 +						<int key="IBUIBaselineAdjustment">1</int>
 162.111 +						<float key="IBUIMinimumFontSize">10</float>
 162.112 +					</object>
 162.113 +					<object class="IBUILabel" id="337771867">
 162.114 +						<reference key="NSNextResponder" ref="191373211"/>
 162.115 +						<int key="NSvFlags">292</int>
 162.116 +						<string key="NSFrame">{{64, 66}, {100, 21}}</string>
 162.117 +						<reference key="NSSuperview" ref="191373211"/>
 162.118 +						<bool key="IBUIOpaque">NO</bool>
 162.119 +						<bool key="IBUIClipsSubviews">YES</bool>
 162.120 +						<int key="IBUIContentMode">7</int>
 162.121 +						<bool key="IBUIUserInteractionEnabled">NO</bool>
 162.122 +						<string key="targetRuntimeIdentifier">IBCocoaTouchFramework</string>
 162.123 +						<string key="IBUIText">Tunnel mode</string>
 162.124 +						<reference key="IBUITextColor" ref="621632613"/>
 162.125 +						<reference key="IBUIHighlightedColor" ref="621632613"/>
 162.126 +						<int key="IBUIBaselineAdjustment">1</int>
 162.127 +						<float key="IBUIMinimumFontSize">10</float>
 162.128 +					</object>
 162.129 +					<object class="IBUISlider" id="548530308">
 162.130 +						<reference key="NSNextResponder" ref="191373211"/>
 162.131 +						<int key="NSvFlags">292</int>
 162.132 +						<string key="NSFrame">{{200, 106}, {262, 23}}</string>
 162.133 +						<reference key="NSSuperview" ref="191373211"/>
 162.134 +						<bool key="IBUIOpaque">NO</bool>
 162.135 +						<string key="targetRuntimeIdentifier">IBCocoaTouchFramework</string>
 162.136 +						<int key="IBUIContentHorizontalAlignment">0</int>
 162.137 +						<int key="IBUIContentVerticalAlignment">0</int>
 162.138 +						<float key="IBUIValue">0.5</float>
 162.139 +					</object>
 162.140 +					<object class="IBUILabel" id="108081265">
 162.141 +						<reference key="NSNextResponder" ref="191373211"/>
 162.142 +						<int key="NSvFlags">292</int>
 162.143 +						<string key="NSFrame">{{20, 106}, {174, 21}}</string>
 162.144 +						<reference key="NSSuperview" ref="191373211"/>
 162.145 +						<bool key="IBUIOpaque">NO</bool>
 162.146 +						<bool key="IBUIClipsSubviews">YES</bool>
 162.147 +						<int key="IBUIContentMode">7</int>
 162.148 +						<bool key="IBUIUserInteractionEnabled">NO</bool>
 162.149 +						<string key="targetRuntimeIdentifier">IBCocoaTouchFramework</string>
 162.150 +						<string key="IBUIText">Screen divider position</string>
 162.151 +						<reference key="IBUITextColor" ref="621632613"/>
 162.152 +						<reference key="IBUIHighlightedColor" ref="621632613"/>
 162.153 +						<int key="IBUIBaselineAdjustment">1</int>
 162.154 +						<float key="IBUIMinimumFontSize">10</float>
 162.155 +					</object>
 162.156 +					<object class="IBUIButton" id="509948995">
 162.157 +						<reference key="NSNextResponder" ref="191373211"/>
 162.158 +						<int key="NSvFlags">292</int>
 162.159 +						<string key="NSFrame">{{350, 263}, {110, 37}}</string>
 162.160 +						<reference key="NSSuperview" ref="191373211"/>
 162.161 +						<bool key="IBUIOpaque">NO</bool>
 162.162 +						<string key="targetRuntimeIdentifier">IBCocoaTouchFramework</string>
 162.163 +						<int key="IBUIContentHorizontalAlignment">0</int>
 162.164 +						<int key="IBUIContentVerticalAlignment">0</int>
 162.165 +						<object class="NSFont" key="IBUIFont">
 162.166 +							<string key="NSName">Helvetica-Bold</string>
 162.167 +							<double key="NSSize">15</double>
 162.168 +							<int key="NSfFlags">16</int>
 162.169 +						</object>
 162.170 +						<int key="IBUIButtonType">1</int>
 162.171 +						<string key="IBUINormalTitle">Done</string>
 162.172 +						<reference key="IBUIHighlightedTitleColor" ref="621632613"/>
 162.173 +						<object class="NSColor" key="IBUINormalTitleColor">
 162.174 +							<int key="NSColorSpace">1</int>
 162.175 +							<bytes key="NSRGB">MC4xOTYwNzg0MzQ2IDAuMzA5ODAzOTMyOSAwLjUyMTU2ODY1NgA</bytes>
 162.176 +						</object>
 162.177 +						<object class="NSColor" key="IBUINormalTitleShadowColor">
 162.178 +							<int key="NSColorSpace">3</int>
 162.179 +							<bytes key="NSWhite">MC41AA</bytes>
 162.180 +						</object>
 162.181 +					</object>
 162.182 +				</object>
 162.183 +				<string key="NSFrameSize">{480, 320}</string>
 162.184 +				<reference key="NSSuperview"/>
 162.185 +				<object class="NSColor" key="IBUIBackgroundColor">
 162.186 +					<int key="NSColorSpace">1</int>
 162.187 +					<bytes key="NSRGB">MCAwIDAgMC41AA</bytes>
 162.188 +				</object>
 162.189 +				<bool key="IBUIOpaque">NO</bool>
 162.190 +				<int key="IBUIContentMode">6</int>
 162.191 +				<object class="IBUISimulatedOrientationMetrics" key="IBUISimulatedOrientationMetrics">
 162.192 +					<int key="interfaceOrientation">3</int>
 162.193 +				</object>
 162.194 +				<string key="targetRuntimeIdentifier">IBCocoaTouchFramework</string>
 162.195 +			</object>
 162.196 +		</object>
 162.197 +		<object class="IBObjectContainer" key="IBDocument.Objects">
 162.198 +			<object class="NSMutableArray" key="connectionRecords">
 162.199 +				<bool key="EncodedWithXMLCoder">YES</bool>
 162.200 +				<object class="IBConnectionRecord">
 162.201 +					<object class="IBCocoaTouchOutletConnection" key="connection">
 162.202 +						<string key="label">view</string>
 162.203 +						<reference key="source" ref="372490531"/>
 162.204 +						<reference key="destination" ref="191373211"/>
 162.205 +					</object>
 162.206 +					<int key="connectionID">3</int>
 162.207 +				</object>
 162.208 +				<object class="IBConnectionRecord">
 162.209 +					<object class="IBCocoaTouchOutletConnection" key="connection">
 162.210 +						<string key="label">sw_stereo</string>
 162.211 +						<reference key="source" ref="372490531"/>
 162.212 +						<reference key="destination" ref="5150575"/>
 162.213 +					</object>
 162.214 +					<int key="connectionID">10</int>
 162.215 +				</object>
 162.216 +				<object class="IBConnectionRecord">
 162.217 +					<object class="IBCocoaTouchOutletConnection" key="connection">
 162.218 +						<string key="label">grp_mode</string>
 162.219 +						<reference key="source" ref="372490531"/>
 162.220 +						<reference key="destination" ref="694841059"/>
 162.221 +					</object>
 162.222 +					<int key="connectionID">11</int>
 162.223 +				</object>
 162.224 +				<object class="IBConnectionRecord">
 162.225 +					<object class="IBCocoaTouchOutletConnection" key="connection">
 162.226 +						<string key="label">slider_split</string>
 162.227 +						<reference key="source" ref="372490531"/>
 162.228 +						<reference key="destination" ref="548530308"/>
 162.229 +					</object>
 162.230 +					<int key="connectionID">12</int>
 162.231 +				</object>
 162.232 +				<object class="IBConnectionRecord">
 162.233 +					<object class="IBCocoaTouchOutletConnection" key="connection">
 162.234 +						<string key="label">bn_done</string>
 162.235 +						<reference key="source" ref="372490531"/>
 162.236 +						<reference key="destination" ref="509948995"/>
 162.237 +					</object>
 162.238 +					<int key="connectionID">15</int>
 162.239 +				</object>
 162.240 +				<object class="IBConnectionRecord">
 162.241 +					<object class="IBCocoaTouchEventConnection" key="connection">
 162.242 +						<string key="label">done_clicked:</string>
 162.243 +						<reference key="source" ref="509948995"/>
 162.244 +						<reference key="destination" ref="372490531"/>
 162.245 +						<int key="IBEventType">7</int>
 162.246 +					</object>
 162.247 +					<int key="connectionID">16</int>
 162.248 +				</object>
 162.249 +				<object class="IBConnectionRecord">
 162.250 +					<object class="IBCocoaTouchEventConnection" key="connection">
 162.251 +						<string key="label">split_changed:</string>
 162.252 +						<reference key="source" ref="548530308"/>
 162.253 +						<reference key="destination" ref="372490531"/>
 162.254 +						<int key="IBEventType">13</int>
 162.255 +					</object>
 162.256 +					<int key="connectionID">17</int>
 162.257 +				</object>
 162.258 +				<object class="IBConnectionRecord">
 162.259 +					<object class="IBCocoaTouchEventConnection" key="connection">
 162.260 +						<string key="label">mode_changed:</string>
 162.261 +						<reference key="source" ref="694841059"/>
 162.262 +						<reference key="destination" ref="372490531"/>
 162.263 +						<int key="IBEventType">13</int>
 162.264 +					</object>
 162.265 +					<int key="connectionID">18</int>
 162.266 +				</object>
 162.267 +				<object class="IBConnectionRecord">
 162.268 +					<object class="IBCocoaTouchEventConnection" key="connection">
 162.269 +						<string key="label">stereo_changed:</string>
 162.270 +						<reference key="source" ref="5150575"/>
 162.271 +						<reference key="destination" ref="372490531"/>
 162.272 +						<int key="IBEventType">13</int>
 162.273 +					</object>
 162.274 +					<int key="connectionID">19</int>
 162.275 +				</object>
 162.276 +			</object>
 162.277 +			<object class="IBMutableOrderedSet" key="objectRecords">
 162.278 +				<object class="NSArray" key="orderedObjects">
 162.279 +					<bool key="EncodedWithXMLCoder">YES</bool>
 162.280 +					<object class="IBObjectRecord">
 162.281 +						<int key="objectID">0</int>
 162.282 +						<reference key="object" ref="0"/>
 162.283 +						<reference key="children" ref="1000"/>
 162.284 +						<nil key="parent"/>
 162.285 +					</object>
 162.286 +					<object class="IBObjectRecord">
 162.287 +						<int key="objectID">1</int>
 162.288 +						<reference key="object" ref="191373211"/>
 162.289 +						<object class="NSMutableArray" key="children">
 162.290 +							<bool key="EncodedWithXMLCoder">YES</bool>
 162.291 +							<reference ref="5150575"/>
 162.292 +							<reference ref="694841059"/>
 162.293 +							<reference ref="337771867"/>
 162.294 +							<reference ref="548530308"/>
 162.295 +							<reference ref="108081265"/>
 162.296 +							<reference ref="799767528"/>
 162.297 +							<reference ref="509948995"/>
 162.298 +						</object>
 162.299 +						<reference key="parent" ref="0"/>
 162.300 +					</object>
 162.301 +					<object class="IBObjectRecord">
 162.302 +						<int key="objectID">-1</int>
 162.303 +						<reference key="object" ref="372490531"/>
 162.304 +						<reference key="parent" ref="0"/>
 162.305 +						<string key="objectName">File's Owner</string>
 162.306 +					</object>
 162.307 +					<object class="IBObjectRecord">
 162.308 +						<int key="objectID">-2</int>
 162.309 +						<reference key="object" ref="975951072"/>
 162.310 +						<reference key="parent" ref="0"/>
 162.311 +					</object>
 162.312 +					<object class="IBObjectRecord">
 162.313 +						<int key="objectID">4</int>
 162.314 +						<reference key="object" ref="5150575"/>
 162.315 +						<reference key="parent" ref="191373211"/>
 162.316 +					</object>
 162.317 +					<object class="IBObjectRecord">
 162.318 +						<int key="objectID">5</int>
 162.319 +						<reference key="object" ref="694841059"/>
 162.320 +						<reference key="parent" ref="191373211"/>
 162.321 +					</object>
 162.322 +					<object class="IBObjectRecord">
 162.323 +						<int key="objectID">6</int>
 162.324 +						<reference key="object" ref="799767528"/>
 162.325 +						<reference key="parent" ref="191373211"/>
 162.326 +					</object>
 162.327 +					<object class="IBObjectRecord">
 162.328 +						<int key="objectID">7</int>
 162.329 +						<reference key="object" ref="337771867"/>
 162.330 +						<reference key="parent" ref="191373211"/>
 162.331 +					</object>
 162.332 +					<object class="IBObjectRecord">
 162.333 +						<int key="objectID">8</int>
 162.334 +						<reference key="object" ref="548530308"/>
 162.335 +						<reference key="parent" ref="191373211"/>
 162.336 +					</object>
 162.337 +					<object class="IBObjectRecord">
 162.338 +						<int key="objectID">9</int>
 162.339 +						<reference key="object" ref="108081265"/>
 162.340 +						<reference key="parent" ref="191373211"/>
 162.341 +					</object>
 162.342 +					<object class="IBObjectRecord">
 162.343 +						<int key="objectID">13</int>
 162.344 +						<reference key="object" ref="509948995"/>
 162.345 +						<reference key="parent" ref="191373211"/>
 162.346 +					</object>
 162.347 +				</object>
 162.348 +			</object>
 162.349 +			<object class="NSMutableDictionary" key="flattenedProperties">
 162.350 +				<bool key="EncodedWithXMLCoder">YES</bool>
 162.351 +				<object class="NSArray" key="dict.sortedKeys">
 162.352 +					<bool key="EncodedWithXMLCoder">YES</bool>
 162.353 +					<string>-1.CustomClassName</string>
 162.354 +					<string>-2.CustomClassName</string>
 162.355 +					<string>1.IBEditorWindowLastContentRect</string>
 162.356 +					<string>1.IBPluginDependency</string>
 162.357 +					<string>13.IBPluginDependency</string>
 162.358 +					<string>13.IBViewBoundsToFrameTransform</string>
 162.359 +					<string>4.IBPluginDependency</string>
 162.360 +					<string>4.IBViewBoundsToFrameTransform</string>
 162.361 +					<string>5.IBPluginDependency</string>
 162.362 +					<string>5.IBViewBoundsToFrameTransform</string>
 162.363 +					<string>6.IBPluginDependency</string>
 162.364 +					<string>6.IBViewBoundsToFrameTransform</string>
 162.365 +					<string>7.IBPluginDependency</string>
 162.366 +					<string>7.IBViewBoundsToFrameTransform</string>
 162.367 +					<string>8.IBPluginDependency</string>
 162.368 +					<string>8.IBViewBoundsToFrameTransform</string>
 162.369 +					<string>9.IBPluginDependency</string>
 162.370 +					<string>9.IBViewBoundsToFrameTransform</string>
 162.371 +				</object>
 162.372 +				<object class="NSMutableArray" key="dict.values">
 162.373 +					<bool key="EncodedWithXMLCoder">YES</bool>
 162.374 +					<string>UI</string>
 162.375 +					<string>UIResponder</string>
 162.376 +					<string>{{403, 352}, {480, 320}}</string>
 162.377 +					<string>com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
 162.378 +					<string>com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
 162.379 +					<object class="NSAffineTransform">
 162.380 +						<bytes key="NSTransformStruct">P4AAAL+AAABDrwAAwysAAA</bytes>
 162.381 +					</object>
 162.382 +					<string>com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
 162.383 +					<object class="NSAffineTransform">
 162.384 +						<bytes key="NSTransformStruct">P4AAAL+AAABDtwAAwxgAAA</bytes>
 162.385 +					</object>
 162.386 +					<string>com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
 162.387 +					<object class="NSAffineTransform">
 162.388 +						<bytes key="NSTransformStruct">P4AAAL+AAABDLAAAw0wAAA</bytes>
 162.389 +					</object>
 162.390 +					<string>com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
 162.391 +					<object class="NSAffineTransform">
 162.392 +						<bytes key="NSTransformStruct">P4AAAL+AAABDNgAAwigAAA</bytes>
 162.393 +					</object>
 162.394 +					<string>com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
 162.395 +					<object class="NSAffineTransform">
 162.396 +						<bytes key="NSTransformStruct">P4AAAL+AAABCZAAAw0AAAA</bytes>
 162.397 +					</object>
 162.398 +					<string>com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
 162.399 +					<object class="NSAffineTransform">
 162.400 +						<bytes key="NSTransformStruct">P4AAAL+AAABDSAAAw2oAAA</bytes>
 162.401 +					</object>
 162.402 +					<string>com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
 162.403 +					<object class="NSAffineTransform">
 162.404 +						<bytes key="NSTransformStruct">P4AAAL+AAABBoAAAw2gAAA</bytes>
 162.405 +					</object>
 162.406 +				</object>
 162.407 +			</object>
 162.408 +			<object class="NSMutableDictionary" key="unlocalizedProperties">
 162.409 +				<bool key="EncodedWithXMLCoder">YES</bool>
 162.410 +				<reference key="dict.sortedKeys" ref="0"/>
 162.411 +				<object class="NSMutableArray" key="dict.values">
 162.412 +					<bool key="EncodedWithXMLCoder">YES</bool>
 162.413 +				</object>
 162.414 +			</object>
 162.415 +			<nil key="activeLocalization"/>
 162.416 +			<object class="NSMutableDictionary" key="localizations">
 162.417 +				<bool key="EncodedWithXMLCoder">YES</bool>
 162.418 +				<reference key="dict.sortedKeys" ref="0"/>
 162.419 +				<object class="NSMutableArray" key="dict.values">
 162.420 +					<bool key="EncodedWithXMLCoder">YES</bool>
 162.421 +				</object>
 162.422 +			</object>
 162.423 +			<nil key="sourceID"/>
 162.424 +			<int key="maxID">19</int>
 162.425 +		</object>
 162.426 +		<object class="IBClassDescriber" key="IBDocument.Classes">
 162.427 +			<object class="NSMutableArray" key="referencedPartialClassDescriptions">
 162.428 +				<bool key="EncodedWithXMLCoder">YES</bool>
 162.429 +				<object class="IBPartialClassDescription">
 162.430 +					<string key="className">UI</string>
 162.431 +					<string key="superclassName">UIViewController</string>
 162.432 +					<object class="NSMutableDictionary" key="actions">
 162.433 +						<bool key="EncodedWithXMLCoder">YES</bool>
 162.434 +						<object class="NSArray" key="dict.sortedKeys">
 162.435 +							<bool key="EncodedWithXMLCoder">YES</bool>
 162.436 +							<string>done_clicked:</string>
 162.437 +							<string>mode_changed:</string>
 162.438 +							<string>split_changed:</string>
 162.439 +							<string>stereo_changed:</string>
 162.440 +						</object>
 162.441 +						<object class="NSMutableArray" key="dict.values">
 162.442 +							<bool key="EncodedWithXMLCoder">YES</bool>
 162.443 +							<string>id</string>
 162.444 +							<string>id</string>
 162.445 +							<string>id</string>
 162.446 +							<string>id</string>
 162.447 +						</object>
 162.448 +					</object>
 162.449 +					<object class="NSMutableDictionary" key="actionInfosByName">
 162.450 +						<bool key="EncodedWithXMLCoder">YES</bool>
 162.451 +						<object class="NSArray" key="dict.sortedKeys">
 162.452 +							<bool key="EncodedWithXMLCoder">YES</bool>
 162.453 +							<string>done_clicked:</string>
 162.454 +							<string>mode_changed:</string>
 162.455 +							<string>split_changed:</string>
 162.456 +							<string>stereo_changed:</string>
 162.457 +						</object>
 162.458 +						<object class="NSMutableArray" key="dict.values">
 162.459 +							<bool key="EncodedWithXMLCoder">YES</bool>
 162.460 +							<object class="IBActionInfo">
 162.461 +								<string key="name">done_clicked:</string>
 162.462 +								<string key="candidateClassName">id</string>
 162.463 +							</object>
 162.464 +							<object class="IBActionInfo">
 162.465 +								<string key="name">mode_changed:</string>
 162.466 +								<string key="candidateClassName">id</string>
 162.467 +							</object>
 162.468 +							<object class="IBActionInfo">
 162.469 +								<string key="name">split_changed:</string>
 162.470 +								<string key="candidateClassName">id</string>
 162.471 +							</object>
 162.472 +							<object class="IBActionInfo">
 162.473 +								<string key="name">stereo_changed:</string>
 162.474 +								<string key="candidateClassName">id</string>
 162.475 +							</object>
 162.476 +						</object>
 162.477 +					</object>
 162.478 +					<object class="NSMutableDictionary" key="outlets">
 162.479 +						<bool key="EncodedWithXMLCoder">YES</bool>
 162.480 +						<object class="NSArray" key="dict.sortedKeys">
 162.481 +							<bool key="EncodedWithXMLCoder">YES</bool>
 162.482 +							<string>bn_done</string>
 162.483 +							<string>grp_mode</string>
 162.484 +							<string>slider_split</string>
 162.485 +							<string>sw_stereo</string>
 162.486 +						</object>
 162.487 +						<object class="NSMutableArray" key="dict.values">
 162.488 +							<bool key="EncodedWithXMLCoder">YES</bool>
 162.489 +							<string>UIButton</string>
 162.490 +							<string>UISegmentedControl</string>
 162.491 +							<string>UISlider</string>
 162.492 +							<string>UISwitch</string>
 162.493 +						</object>
 162.494 +					</object>
 162.495 +					<object class="NSMutableDictionary" key="toOneOutletInfosByName">
 162.496 +						<bool key="EncodedWithXMLCoder">YES</bool>
 162.497 +						<object class="NSArray" key="dict.sortedKeys">
 162.498 +							<bool key="EncodedWithXMLCoder">YES</bool>
 162.499 +							<string>bn_done</string>
 162.500 +							<string>grp_mode</string>
 162.501 +							<string>slider_split</string>
 162.502 +							<string>sw_stereo</string>
 162.503 +						</object>
 162.504 +						<object class="NSMutableArray" key="dict.values">
 162.505 +							<bool key="EncodedWithXMLCoder">YES</bool>
 162.506 +							<object class="IBToOneOutletInfo">
 162.507 +								<string key="name">bn_done</string>
 162.508 +								<string key="candidateClassName">UIButton</string>
 162.509 +							</object>
 162.510 +							<object class="IBToOneOutletInfo">
 162.511 +								<string key="name">grp_mode</string>
 162.512 +								<string key="candidateClassName">UISegmentedControl</string>
 162.513 +							</object>
 162.514 +							<object class="IBToOneOutletInfo">
 162.515 +								<string key="name">slider_split</string>
 162.516 +								<string key="candidateClassName">UISlider</string>
 162.517 +							</object>
 162.518 +							<object class="IBToOneOutletInfo">
 162.519 +								<string key="name">sw_stereo</string>
 162.520 +								<string key="candidateClassName">UISwitch</string>
 162.521 +							</object>
 162.522 +						</object>
 162.523 +					</object>
 162.524 +					<object class="IBClassDescriptionSource" key="sourceIdentifier">
 162.525 +						<string key="majorKey">IBProjectSource</string>
 162.526 +						<string key="minorKey">src/ui.h</string>
 162.527 +					</object>
 162.528 +				</object>
 162.529 +			</object>
 162.530 +			<object class="NSMutableArray" key="referencedPartialClassDescriptionsV3.2+">
 162.531 +				<bool key="EncodedWithXMLCoder">YES</bool>
 162.532 +				<object class="IBPartialClassDescription">
 162.533 +					<string key="className">NSObject</string>
 162.534 +					<object class="IBClassDescriptionSource" key="sourceIdentifier">
 162.535 +						<string key="majorKey">IBFrameworkSource</string>
 162.536 +						<string key="minorKey">Foundation.framework/Headers/NSError.h</string>
 162.537 +					</object>
 162.538 +				</object>
 162.539 +				<object class="IBPartialClassDescription">
 162.540 +					<string key="className">NSObject</string>
 162.541 +					<object class="IBClassDescriptionSource" key="sourceIdentifier">
 162.542 +						<string key="majorKey">IBFrameworkSource</string>
 162.543 +						<string key="minorKey">Foundation.framework/Headers/NSFileManager.h</string>
 162.544 +					</object>
 162.545 +				</object>
 162.546 +				<object class="IBPartialClassDescription">
 162.547 +					<string key="className">NSObject</string>
 162.548 +					<object class="IBClassDescriptionSource" key="sourceIdentifier">
 162.549 +						<string key="majorKey">IBFrameworkSource</string>
 162.550 +						<string key="minorKey">Foundation.framework/Headers/NSKeyValueCoding.h</string>
 162.551 +					</object>
 162.552 +				</object>
 162.553 +				<object class="IBPartialClassDescription">
 162.554 +					<string key="className">NSObject</string>
 162.555 +					<object class="IBClassDescriptionSource" key="sourceIdentifier">
 162.556 +						<string key="majorKey">IBFrameworkSource</string>
 162.557 +						<string key="minorKey">Foundation.framework/Headers/NSKeyValueObserving.h</string>
 162.558 +					</object>
 162.559 +				</object>
 162.560 +				<object class="IBPartialClassDescription">
 162.561 +					<string key="className">NSObject</string>
 162.562 +					<object class="IBClassDescriptionSource" key="sourceIdentifier">
 162.563 +						<string key="majorKey">IBFrameworkSource</string>
 162.564 +						<string key="minorKey">Foundation.framework/Headers/NSKeyedArchiver.h</string>
 162.565 +					</object>
 162.566 +				</object>
 162.567 +				<object class="IBPartialClassDescription">
 162.568 +					<string key="className">NSObject</string>
 162.569 +					<object class="IBClassDescriptionSource" key="sourceIdentifier">
 162.570 +						<string key="majorKey">IBFrameworkSource</string>
 162.571 +						<string key="minorKey">Foundation.framework/Headers/NSObject.h</string>
 162.572 +					</object>
 162.573 +				</object>
 162.574 +				<object class="IBPartialClassDescription">
 162.575 +					<string key="className">NSObject</string>
 162.576 +					<object class="IBClassDescriptionSource" key="sourceIdentifier">
 162.577 +						<string key="majorKey">IBFrameworkSource</string>
 162.578 +						<string key="minorKey">Foundation.framework/Headers/NSRunLoop.h</string>
 162.579 +					</object>
 162.580 +				</object>
 162.581 +				<object class="IBPartialClassDescription">
 162.582 +					<string key="className">NSObject</string>
 162.583 +					<object class="IBClassDescriptionSource" key="sourceIdentifier">
 162.584 +						<string key="majorKey">IBFrameworkSource</string>
 162.585 +						<string key="minorKey">Foundation.framework/Headers/NSThread.h</string>
 162.586 +					</object>
 162.587 +				</object>
 162.588 +				<object class="IBPartialClassDescription">
 162.589 +					<string key="className">NSObject</string>
 162.590 +					<object class="IBClassDescriptionSource" key="sourceIdentifier">
 162.591 +						<string key="majorKey">IBFrameworkSource</string>
 162.592 +						<string key="minorKey">Foundation.framework/Headers/NSURL.h</string>
 162.593 +					</object>
 162.594 +				</object>
 162.595 +				<object class="IBPartialClassDescription">
 162.596 +					<string key="className">NSObject</string>
 162.597 +					<object class="IBClassDescriptionSource" key="sourceIdentifier">
 162.598 +						<string key="majorKey">IBFrameworkSource</string>
 162.599 +						<string key="minorKey">Foundation.framework/Headers/NSURLConnection.h</string>
 162.600 +					</object>
 162.601 +				</object>
 162.602 +				<object class="IBPartialClassDescription">
 162.603 +					<string key="className">NSObject</string>
 162.604 +					<object class="IBClassDescriptionSource" key="sourceIdentifier">
 162.605 +						<string key="majorKey">IBFrameworkSource</string>
 162.606 +						<string key="minorKey">QuartzCore.framework/Headers/CAAnimation.h</string>
 162.607 +					</object>
 162.608 +				</object>
 162.609 +				<object class="IBPartialClassDescription">
 162.610 +					<string key="className">NSObject</string>
 162.611 +					<object class="IBClassDescriptionSource" key="sourceIdentifier">
 162.612 +						<string key="majorKey">IBFrameworkSource</string>
 162.613 +						<string key="minorKey">QuartzCore.framework/Headers/CALayer.h</string>
 162.614 +					</object>
 162.615 +				</object>
 162.616 +				<object class="IBPartialClassDescription">
 162.617 +					<string key="className">NSObject</string>
 162.618 +					<object class="IBClassDescriptionSource" key="sourceIdentifier">
 162.619 +						<string key="majorKey">IBFrameworkSource</string>
 162.620 +						<string key="minorKey">UIKit.framework/Headers/UIAccessibility.h</string>
 162.621 +					</object>
 162.622 +				</object>
 162.623 +				<object class="IBPartialClassDescription">
 162.624 +					<string key="className">NSObject</string>
 162.625 +					<object class="IBClassDescriptionSource" key="sourceIdentifier">
 162.626 +						<string key="majorKey">IBFrameworkSource</string>
 162.627 +						<string key="minorKey">UIKit.framework/Headers/UINibLoading.h</string>
 162.628 +					</object>
 162.629 +				</object>
 162.630 +				<object class="IBPartialClassDescription">
 162.631 +					<string key="className">NSObject</string>
 162.632 +					<object class="IBClassDescriptionSource" key="sourceIdentifier" id="342843117">
 162.633 +						<string key="majorKey">IBFrameworkSource</string>
 162.634 +						<string key="minorKey">UIKit.framework/Headers/UIResponder.h</string>
 162.635 +					</object>
 162.636 +				</object>
 162.637 +				<object class="IBPartialClassDescription">
 162.638 +					<string key="className">UIButton</string>
 162.639 +					<string key="superclassName">UIControl</string>
 162.640 +					<object class="IBClassDescriptionSource" key="sourceIdentifier">
 162.641 +						<string key="majorKey">IBFrameworkSource</string>
 162.642 +						<string key="minorKey">UIKit.framework/Headers/UIButton.h</string>
 162.643 +					</object>
 162.644 +				</object>
 162.645 +				<object class="IBPartialClassDescription">
 162.646 +					<string key="className">UIControl</string>
 162.647 +					<string key="superclassName">UIView</string>
 162.648 +					<object class="IBClassDescriptionSource" key="sourceIdentifier">
 162.649 +						<string key="majorKey">IBFrameworkSource</string>
 162.650 +						<string key="minorKey">UIKit.framework/Headers/UIControl.h</string>
 162.651 +					</object>
 162.652 +				</object>
 162.653 +				<object class="IBPartialClassDescription">
 162.654 +					<string key="className">UILabel</string>
 162.655 +					<string key="superclassName">UIView</string>
 162.656 +					<object class="IBClassDescriptionSource" key="sourceIdentifier">
 162.657 +						<string key="majorKey">IBFrameworkSource</string>
 162.658 +						<string key="minorKey">UIKit.framework/Headers/UILabel.h</string>
 162.659 +					</object>
 162.660 +				</object>
 162.661 +				<object class="IBPartialClassDescription">
 162.662 +					<string key="className">UIResponder</string>
 162.663 +					<string key="superclassName">NSObject</string>
 162.664 +					<reference key="sourceIdentifier" ref="342843117"/>
 162.665 +				</object>
 162.666 +				<object class="IBPartialClassDescription">
 162.667 +					<string key="className">UISearchBar</string>
 162.668 +					<string key="superclassName">UIView</string>
 162.669 +					<object class="IBClassDescriptionSource" key="sourceIdentifier">
 162.670 +						<string key="majorKey">IBFrameworkSource</string>
 162.671 +						<string key="minorKey">UIKit.framework/Headers/UISearchBar.h</string>
 162.672 +					</object>
 162.673 +				</object>
 162.674 +				<object class="IBPartialClassDescription">
 162.675 +					<string key="className">UISearchDisplayController</string>
 162.676 +					<string key="superclassName">NSObject</string>
 162.677 +					<object class="IBClassDescriptionSource" key="sourceIdentifier">
 162.678 +						<string key="majorKey">IBFrameworkSource</string>
 162.679 +						<string key="minorKey">UIKit.framework/Headers/UISearchDisplayController.h</string>
 162.680 +					</object>
 162.681 +				</object>
 162.682 +				<object class="IBPartialClassDescription">
 162.683 +					<string key="className">UISegmentedControl</string>
 162.684 +					<string key="superclassName">UIControl</string>
 162.685 +					<object class="IBClassDescriptionSource" key="sourceIdentifier">
 162.686 +						<string key="majorKey">IBFrameworkSource</string>
 162.687 +						<string key="minorKey">UIKit.framework/Headers/UISegmentedControl.h</string>
 162.688 +					</object>
 162.689 +				</object>
 162.690 +				<object class="IBPartialClassDescription">
 162.691 +					<string key="className">UISlider</string>
 162.692 +					<string key="superclassName">UIControl</string>
 162.693 +					<object class="IBClassDescriptionSource" key="sourceIdentifier">
 162.694 +						<string key="majorKey">IBFrameworkSource</string>
 162.695 +						<string key="minorKey">UIKit.framework/Headers/UISlider.h</string>
 162.696 +					</object>
 162.697 +				</object>
 162.698 +				<object class="IBPartialClassDescription">
 162.699 +					<string key="className">UISwitch</string>
 162.700 +					<string key="superclassName">UIControl</string>
 162.701 +					<object class="IBClassDescriptionSource" key="sourceIdentifier">
 162.702 +						<string key="majorKey">IBFrameworkSource</string>
 162.703 +						<string key="minorKey">UIKit.framework/Headers/UISwitch.h</string>
 162.704 +					</object>
 162.705 +				</object>
 162.706 +				<object class="IBPartialClassDescription">
 162.707 +					<string key="className">UIView</string>
 162.708 +					<object class="IBClassDescriptionSource" key="sourceIdentifier">
 162.709 +						<string key="majorKey">IBFrameworkSource</string>
 162.710 +						<string key="minorKey">UIKit.framework/Headers/UIPrintFormatter.h</string>
 162.711 +					</object>
 162.712 +				</object>
 162.713 +				<object class="IBPartialClassDescription">
 162.714 +					<string key="className">UIView</string>
 162.715 +					<object class="IBClassDescriptionSource" key="sourceIdentifier">
 162.716 +						<string key="majorKey">IBFrameworkSource</string>
 162.717 +						<string key="minorKey">UIKit.framework/Headers/UITextField.h</string>
 162.718 +					</object>
 162.719 +				</object>
 162.720 +				<object class="IBPartialClassDescription">
 162.721 +					<string key="className">UIView</string>
 162.722 +					<string key="superclassName">UIResponder</string>
 162.723 +					<object class="IBClassDescriptionSource" key="sourceIdentifier">
 162.724 +						<string key="majorKey">IBFrameworkSource</string>
 162.725 +						<string key="minorKey">UIKit.framework/Headers/UIView.h</string>
 162.726 +					</object>
 162.727 +				</object>
 162.728 +				<object class="IBPartialClassDescription">
 162.729 +					<string key="className">UIViewController</string>
 162.730 +					<object class="IBClassDescriptionSource" key="sourceIdentifier">
 162.731 +						<string key="majorKey">IBFrameworkSource</string>
 162.732 +						<string key="minorKey">UIKit.framework/Headers/UINavigationController.h</string>
 162.733 +					</object>
 162.734 +				</object>
 162.735 +				<object class="IBPartialClassDescription">
 162.736 +					<string key="className">UIViewController</string>
 162.737 +					<object class="IBClassDescriptionSource" key="sourceIdentifier">
 162.738 +						<string key="majorKey">IBFrameworkSource</string>
 162.739 +						<string key="minorKey">UIKit.framework/Headers/UIPopoverController.h</string>
 162.740 +					</object>
 162.741 +				</object>
 162.742 +				<object class="IBPartialClassDescription">
 162.743 +					<string key="className">UIViewController</string>
 162.744 +					<object class="IBClassDescriptionSource" key="sourceIdentifier">
 162.745 +						<string key="majorKey">IBFrameworkSource</string>
 162.746 +						<string key="minorKey">UIKit.framework/Headers/UISplitViewController.h</string>
 162.747 +					</object>
 162.748 +				</object>
 162.749 +				<object class="IBPartialClassDescription">
 162.750 +					<string key="className">UIViewController</string>
 162.751 +					<object class="IBClassDescriptionSource" key="sourceIdentifier">
 162.752 +						<string key="majorKey">IBFrameworkSource</string>
 162.753 +						<string key="minorKey">UIKit.framework/Headers/UITabBarController.h</string>
 162.754 +					</object>
 162.755 +				</object>
 162.756 +				<object class="IBPartialClassDescription">
 162.757 +					<string key="className">UIViewController</string>
 162.758 +					<string key="superclassName">UIResponder</string>
 162.759 +					<object class="IBClassDescriptionSource" key="sourceIdentifier">
 162.760 +						<string key="majorKey">IBFrameworkSource</string>
 162.761 +						<string key="minorKey">UIKit.framework/Headers/UIViewController.h</string>
 162.762 +					</object>
 162.763 +				</object>
 162.764 +			</object>
 162.765 +		</object>
 162.766 +		<int key="IBDocument.localizationMode">0</int>
 162.767 +		<string key="IBDocument.TargetRuntimeIdentifier">IBCocoaTouchFramework</string>
 162.768 +		<object class="NSMutableDictionary" key="IBDocument.PluginDeclaredDependencyDefaults">
 162.769 +			<string key="NS.key.0">com.apple.InterfaceBuilder.CocoaTouchPlugin.iPhoneOS</string>
 162.770 +			<integer value="1056" key="NS.object.0"/>
 162.771 +		</object>
 162.772 +		<object class="NSMutableDictionary" key="IBDocument.PluginDeclaredDevelopmentDependencies">
 162.773 +			<string key="NS.key.0">com.apple.InterfaceBuilder.CocoaTouchPlugin.InterfaceBuilder3</string>
 162.774 +			<integer value="3000" key="NS.object.0"/>
 162.775 +		</object>
 162.776 +		<bool key="IBDocument.PluginDeclaredDependenciesTrackSystemTargetVersion">YES</bool>
 162.777 +		<string key="IBDocument.LastKnownRelativeProjectPath">../istereo.xcodeproj</string>
 162.778 +		<int key="IBDocument.defaultPropertyAccessControl">3</int>
 162.779 +		<string key="IBCocoaTouchPluginVersion">141</string>
 162.780 +	</data>
 162.781 +</archive>