updated 3rd party libs: CLapack 3.1.1.1 => 3.2.1, zlib 1.2.3 => 1.2.5, libpng 1.2...
authorVadim Pisarevsky <no@email>
Fri, 16 Jul 2010 12:54:53 +0000 (12:54 +0000)
committerVadim Pisarevsky <no@email>
Fri, 16 Jul 2010 12:54:53 +0000 (12:54 +0000)
465 files changed:
3rdparty/flann/util/allocator.h
3rdparty/flann/util/result_set.h
3rdparty/flann/util/saving.cpp
3rdparty/flann/util/saving.h
3rdparty/include/clapack.h
3rdparty/include/png.h
3rdparty/include/pngconf.h
3rdparty/include/tiff.h
3rdparty/include/tiffconf.h [moved from 3rdparty/libtiff/tiffconf.h with 69% similarity]
3rdparty/include/tiffio.h
3rdparty/include/tiffvers.h
3rdparty/include/zconf.h
3rdparty/include/zlib.h
3rdparty/lapack/dasum.c
3rdparty/lapack/daxpy.c
3rdparty/lapack/dbdsdc.c
3rdparty/lapack/dbdsqr.c
3rdparty/lapack/dcopy.c
3rdparty/lapack/ddot.c
3rdparty/lapack/dgebd2.c
3rdparty/lapack/dgebrd.c
3rdparty/lapack/dgelq2.c
3rdparty/lapack/dgelqf.c
3rdparty/lapack/dgels.c
3rdparty/lapack/dgelsd.c
3rdparty/lapack/dgemm.c
3rdparty/lapack/dgemv.c
3rdparty/lapack/dgeqr2.c
3rdparty/lapack/dgeqrf.c
3rdparty/lapack/dger.c
3rdparty/lapack/dgesdd.c
3rdparty/lapack/dgesv.c
3rdparty/lapack/dgetf2.c
3rdparty/lapack/dgetrf.c
3rdparty/lapack/dgetri.c
3rdparty/lapack/dgetrs.c
3rdparty/lapack/dlabad.c
3rdparty/lapack/dlabrd.c
3rdparty/lapack/dlacpy.c
3rdparty/lapack/dlae2.c
3rdparty/lapack/dlaebz.c
3rdparty/lapack/dlaed0.c
3rdparty/lapack/dlaed1.c
3rdparty/lapack/dlaed2.c
3rdparty/lapack/dlaed3.c
3rdparty/lapack/dlaed4.c
3rdparty/lapack/dlaed5.c
3rdparty/lapack/dlaed6.c
3rdparty/lapack/dlaed7.c
3rdparty/lapack/dlaed8.c
3rdparty/lapack/dlaed9.c
3rdparty/lapack/dlaeda.c
3rdparty/lapack/dlaev2.c
3rdparty/lapack/dlagtf.c
3rdparty/lapack/dlagts.c
3rdparty/lapack/dlaisnan.c
3rdparty/lapack/dlals0.c
3rdparty/lapack/dlalsa.c
3rdparty/lapack/dlalsd.c
3rdparty/lapack/dlamrg.c
3rdparty/lapack/dlaneg.c
3rdparty/lapack/dlange.c
3rdparty/lapack/dlanst.c
3rdparty/lapack/dlansy.c
3rdparty/lapack/dlapy2.c
3rdparty/lapack/dlar1v.c
3rdparty/lapack/dlarf.c
3rdparty/lapack/dlarfb.c
3rdparty/lapack/dlarfg.c
3rdparty/lapack/dlarfp.c [new file with mode: 0644]
3rdparty/lapack/dlarft.c
3rdparty/lapack/dlarnv.c
3rdparty/lapack/dlarra.c
3rdparty/lapack/dlarrb.c
3rdparty/lapack/dlarrc.c
3rdparty/lapack/dlarrd.c
3rdparty/lapack/dlarre.c
3rdparty/lapack/dlarrf.c
3rdparty/lapack/dlarrj.c
3rdparty/lapack/dlarrk.c
3rdparty/lapack/dlarrr.c
3rdparty/lapack/dlarrv.c
3rdparty/lapack/dlartg.c
3rdparty/lapack/dlaruv.c
3rdparty/lapack/dlas2.c
3rdparty/lapack/dlascl.c
3rdparty/lapack/dlasd0.c
3rdparty/lapack/dlasd1.c
3rdparty/lapack/dlasd2.c
3rdparty/lapack/dlasd3.c
3rdparty/lapack/dlasd4.c
3rdparty/lapack/dlasd5.c
3rdparty/lapack/dlasd6.c
3rdparty/lapack/dlasd7.c
3rdparty/lapack/dlasd8.c
3rdparty/lapack/dlasda.c
3rdparty/lapack/dlasdq.c
3rdparty/lapack/dlasdt.c
3rdparty/lapack/dlaset.c
3rdparty/lapack/dlasq1.c
3rdparty/lapack/dlasq2.c
3rdparty/lapack/dlasq3.c
3rdparty/lapack/dlasq4.c
3rdparty/lapack/dlasq5.c
3rdparty/lapack/dlasq6.c
3rdparty/lapack/dlasr.c
3rdparty/lapack/dlasrt.c
3rdparty/lapack/dlassq.c
3rdparty/lapack/dlasv2.c
3rdparty/lapack/dlaswp.c
3rdparty/lapack/dlasyf.c
3rdparty/lapack/dlatrd.c
3rdparty/lapack/dlauu2.c
3rdparty/lapack/dlauum.c
3rdparty/lapack/dlazq3.c [deleted file]
3rdparty/lapack/dlazq4.c [deleted file]
3rdparty/lapack/dnrm2.c
3rdparty/lapack/dorg2r.c
3rdparty/lapack/dorgbr.c
3rdparty/lapack/dorgl2.c
3rdparty/lapack/dorglq.c
3rdparty/lapack/dorgqr.c
3rdparty/lapack/dorm2l.c
3rdparty/lapack/dorm2r.c
3rdparty/lapack/dormbr.c
3rdparty/lapack/dorml2.c
3rdparty/lapack/dormlq.c
3rdparty/lapack/dormql.c
3rdparty/lapack/dormqr.c
3rdparty/lapack/dormtr.c
3rdparty/lapack/dpotf2.c
3rdparty/lapack/dpotrf.c
3rdparty/lapack/dpotri.c
3rdparty/lapack/dpotrs.c
3rdparty/lapack/drot.c
3rdparty/lapack/dscal.c
3rdparty/lapack/dstebz.c
3rdparty/lapack/dstein.c
3rdparty/lapack/dstemr.c
3rdparty/lapack/dsteqr.c
3rdparty/lapack/dsterf.c
3rdparty/lapack/dswap.c
3rdparty/lapack/dsyevr.c
3rdparty/lapack/dsymv.c
3rdparty/lapack/dsyr.c
3rdparty/lapack/dsyr2.c
3rdparty/lapack/dsyr2k.c
3rdparty/lapack/dsyrk.c
3rdparty/lapack/dsytd2.c
3rdparty/lapack/dsytf2.c
3rdparty/lapack/dsytrd.c
3rdparty/lapack/dsytrf.c
3rdparty/lapack/dsytri.c
3rdparty/lapack/dsytrs.c
3rdparty/lapack/dtrmm.c
3rdparty/lapack/dtrmv.c
3rdparty/lapack/dtrsm.c
3rdparty/lapack/dtrti2.c
3rdparty/lapack/dtrtri.c
3rdparty/lapack/dtrtrs.c
3rdparty/lapack/idamax.c
3rdparty/lapack/ieeeck.c
3rdparty/lapack/iladlc.c [new file with mode: 0644]
3rdparty/lapack/iladlr.c [new file with mode: 0644]
3rdparty/lapack/ilaenv.c
3rdparty/lapack/ilaslc.c [new file with mode: 0644]
3rdparty/lapack/ilaslr.c [new file with mode: 0644]
3rdparty/lapack/iparmq.c
3rdparty/lapack/isamax.c
3rdparty/lapack/sasum.c
3rdparty/lapack/saxpy.c
3rdparty/lapack/sbdsdc.c
3rdparty/lapack/sbdsqr.c
3rdparty/lapack/scopy.c
3rdparty/lapack/sdot.c
3rdparty/lapack/sgebd2.c
3rdparty/lapack/sgebrd.c
3rdparty/lapack/sgelq2.c
3rdparty/lapack/sgelqf.c
3rdparty/lapack/sgels.c
3rdparty/lapack/sgelsd.c
3rdparty/lapack/sgemm.c
3rdparty/lapack/sgemv.c
3rdparty/lapack/sgeqr2.c
3rdparty/lapack/sgeqrf.c
3rdparty/lapack/sger.c
3rdparty/lapack/sgesdd.c
3rdparty/lapack/sgesv.c
3rdparty/lapack/sgetf2.c
3rdparty/lapack/sgetrf.c
3rdparty/lapack/sgetri.c
3rdparty/lapack/sgetrs.c
3rdparty/lapack/slabad.c
3rdparty/lapack/slabrd.c
3rdparty/lapack/slacpy.c
3rdparty/lapack/slae2.c
3rdparty/lapack/slaebz.c
3rdparty/lapack/slaed0.c
3rdparty/lapack/slaed1.c
3rdparty/lapack/slaed2.c
3rdparty/lapack/slaed3.c
3rdparty/lapack/slaed4.c
3rdparty/lapack/slaed5.c
3rdparty/lapack/slaed6.c
3rdparty/lapack/slaed7.c
3rdparty/lapack/slaed8.c
3rdparty/lapack/slaed9.c
3rdparty/lapack/slaeda.c
3rdparty/lapack/slaev2.c
3rdparty/lapack/slagtf.c
3rdparty/lapack/slagts.c
3rdparty/lapack/slaisnan.c
3rdparty/lapack/slals0.c
3rdparty/lapack/slalsa.c
3rdparty/lapack/slalsd.c
3rdparty/lapack/slamrg.c
3rdparty/lapack/slaneg.c
3rdparty/lapack/slange.c
3rdparty/lapack/slanst.c
3rdparty/lapack/slansy.c
3rdparty/lapack/slapy2.c
3rdparty/lapack/slar1v.c
3rdparty/lapack/slarf.c
3rdparty/lapack/slarfb.c
3rdparty/lapack/slarfg.c
3rdparty/lapack/slarfp.c [new file with mode: 0644]
3rdparty/lapack/slarft.c
3rdparty/lapack/slarnv.c
3rdparty/lapack/slarra.c
3rdparty/lapack/slarrb.c
3rdparty/lapack/slarrc.c
3rdparty/lapack/slarrd.c
3rdparty/lapack/slarre.c
3rdparty/lapack/slarrf.c
3rdparty/lapack/slarrj.c
3rdparty/lapack/slarrk.c
3rdparty/lapack/slarrr.c
3rdparty/lapack/slarrv.c
3rdparty/lapack/slartg.c
3rdparty/lapack/slaruv.c
3rdparty/lapack/slas2.c
3rdparty/lapack/slascl.c
3rdparty/lapack/slasd0.c
3rdparty/lapack/slasd1.c
3rdparty/lapack/slasd2.c
3rdparty/lapack/slasd3.c
3rdparty/lapack/slasd4.c
3rdparty/lapack/slasd5.c
3rdparty/lapack/slasd6.c
3rdparty/lapack/slasd7.c
3rdparty/lapack/slasd8.c
3rdparty/lapack/slasda.c
3rdparty/lapack/slasdq.c
3rdparty/lapack/slasdt.c
3rdparty/lapack/slaset.c
3rdparty/lapack/slasq1.c
3rdparty/lapack/slasq2.c
3rdparty/lapack/slasq3.c
3rdparty/lapack/slasq4.c
3rdparty/lapack/slasq5.c
3rdparty/lapack/slasq6.c
3rdparty/lapack/slasr.c
3rdparty/lapack/slasrt.c
3rdparty/lapack/slassq.c
3rdparty/lapack/slasv2.c
3rdparty/lapack/slaswp.c
3rdparty/lapack/slatrd.c
3rdparty/lapack/slauu2.c
3rdparty/lapack/slauum.c
3rdparty/lapack/slazq3.c [deleted file]
3rdparty/lapack/slazq4.c [deleted file]
3rdparty/lapack/snrm2.c
3rdparty/lapack/sorg2r.c
3rdparty/lapack/sorgbr.c
3rdparty/lapack/sorgl2.c
3rdparty/lapack/sorglq.c
3rdparty/lapack/sorgqr.c
3rdparty/lapack/sorm2l.c
3rdparty/lapack/sorm2r.c
3rdparty/lapack/sormbr.c
3rdparty/lapack/sorml2.c
3rdparty/lapack/sormlq.c
3rdparty/lapack/sormql.c
3rdparty/lapack/sormqr.c
3rdparty/lapack/sormtr.c
3rdparty/lapack/spotf2.c
3rdparty/lapack/spotrf.c
3rdparty/lapack/spotri.c
3rdparty/lapack/spotrs.c
3rdparty/lapack/srot.c
3rdparty/lapack/sscal.c
3rdparty/lapack/sstebz.c
3rdparty/lapack/sstein.c
3rdparty/lapack/sstemr.c
3rdparty/lapack/ssteqr.c
3rdparty/lapack/ssterf.c
3rdparty/lapack/sswap.c
3rdparty/lapack/ssyevr.c
3rdparty/lapack/ssymv.c
3rdparty/lapack/ssyr2.c
3rdparty/lapack/ssyr2k.c
3rdparty/lapack/ssyrk.c
3rdparty/lapack/ssytd2.c
3rdparty/lapack/ssytrd.c
3rdparty/lapack/strmm.c
3rdparty/lapack/strmv.c
3rdparty/lapack/strsm.c
3rdparty/lapack/strti2.c
3rdparty/lapack/strtri.c
3rdparty/lapack/strtrs.c
3rdparty/lapack/xerbla.c
3rdparty/libjasper/CMakeLists.txt
3rdparty/libjpeg/jmemmgr.c
3rdparty/libpng/README
3rdparty/libpng/png.c
3rdparty/libpng/png.h [new file with mode: 0644]
3rdparty/libpng/pngerror.c
3rdparty/libpng/pnggccrd.c [deleted file]
3rdparty/libpng/pngget.c
3rdparty/libpng/pngmem.c
3rdparty/libpng/pngpread.c
3rdparty/libpng/pngpriv.h [new file with mode: 0644]
3rdparty/libpng/pngread.c
3rdparty/libpng/pngrio.c
3rdparty/libpng/pngrtran.c
3rdparty/libpng/pngrutil.c
3rdparty/libpng/pngset.c
3rdparty/libpng/pngtest.c
3rdparty/libpng/pngtest.png [new file with mode: 0644]
3rdparty/libpng/pngtrans.c
3rdparty/libpng/pngvcrd.c [deleted file]
3rdparty/libpng/pngwio.c
3rdparty/libpng/pngwrite.c
3rdparty/libpng/pngwtran.c
3rdparty/libpng/pngwutil.c
3rdparty/libtiff/CMakeLists.txt
3rdparty/libtiff/Makefile.vc [deleted file]
3rdparty/libtiff/libtiff.def [deleted file]
3rdparty/libtiff/t4.h
3rdparty/libtiff/tif_apple.c
3rdparty/libtiff/tif_aux.c
3rdparty/libtiff/tif_close.c
3rdparty/libtiff/tif_codec.c
3rdparty/libtiff/tif_color.c
3rdparty/libtiff/tif_compress.c
3rdparty/libtiff/tif_config.h
3rdparty/libtiff/tif_dir.c
3rdparty/libtiff/tif_dir.h
3rdparty/libtiff/tif_dirinfo.c
3rdparty/libtiff/tif_dirread.c
3rdparty/libtiff/tif_dirwrite.c
3rdparty/libtiff/tif_dumpmode.c
3rdparty/libtiff/tif_error.c
3rdparty/libtiff/tif_extension.c
3rdparty/libtiff/tif_fax3.c
3rdparty/libtiff/tif_fax3.h
3rdparty/libtiff/tif_fax3sm.c
3rdparty/libtiff/tif_flush.c
3rdparty/libtiff/tif_getimage.c
3rdparty/libtiff/tif_jbig.c [new file with mode: 0644]
3rdparty/libtiff/tif_jpeg.c
3rdparty/libtiff/tif_luv.c
3rdparty/libtiff/tif_lzw.c
3rdparty/libtiff/tif_next.c
3rdparty/libtiff/tif_ojpeg.c
3rdparty/libtiff/tif_open.c
3rdparty/libtiff/tif_packbits.c
3rdparty/libtiff/tif_pixarlog.c
3rdparty/libtiff/tif_predict.c
3rdparty/libtiff/tif_predict.h
3rdparty/libtiff/tif_print.c
3rdparty/libtiff/tif_read.c
3rdparty/libtiff/tif_stream.cxx
3rdparty/libtiff/tif_strip.c
3rdparty/libtiff/tif_swab.c
3rdparty/libtiff/tif_thunder.c
3rdparty/libtiff/tif_tile.c
3rdparty/libtiff/tif_unix.c
3rdparty/libtiff/tif_version.c
3rdparty/libtiff/tif_warning.c
3rdparty/libtiff/tif_win32.c
3rdparty/libtiff/tif_write.c
3rdparty/libtiff/tif_zip.c
3rdparty/libtiff/tiffio.hxx
3rdparty/libtiff/tiffiop.h
3rdparty/libtiff/uvcode.h
3rdparty/readme.txt
3rdparty/zlib/README
3rdparty/zlib/adler32.c
3rdparty/zlib/compress.c
3rdparty/zlib/configure [deleted file]
3rdparty/zlib/crc32.c
3rdparty/zlib/deflate.c
3rdparty/zlib/deflate.h
3rdparty/zlib/example.c [deleted file]
3rdparty/zlib/gzclose.c [new file with mode: 0644]
3rdparty/zlib/gzguts.h [new file with mode: 0644]
3rdparty/zlib/gzio.c [deleted file]
3rdparty/zlib/gzlib.c [new file with mode: 0644]
3rdparty/zlib/gzread.c [new file with mode: 0644]
3rdparty/zlib/gzwrite.c [new file with mode: 0644]
3rdparty/zlib/infback.c
3rdparty/zlib/inffast.c
3rdparty/zlib/inffast.h
3rdparty/zlib/inflate.c
3rdparty/zlib/inflate.h
3rdparty/zlib/inftrees.c
3rdparty/zlib/inftrees.h
3rdparty/zlib/trees.c
3rdparty/zlib/trees.h
3rdparty/zlib/uncompr.c
3rdparty/zlib/zutil.c
3rdparty/zlib/zutil.h
modules/calib3d/src/modelest.cpp
modules/calib3d/src/stereobm.cpp
modules/calib3d/src/stereosgbm.cpp
modules/contrib/include/opencv2/contrib/contrib.hpp
modules/contrib/src/adaptiveskindetector.cpp
modules/contrib/src/ba.cpp
modules/contrib/src/quadsubpix.cpp
modules/contrib/src/selfsimilarity.cpp
modules/contrib/src/spinimages.cpp
modules/core/include/opencv2/core/core.hpp
modules/core/include/opencv2/core/operations.hpp
modules/core/src/convert.cpp
modules/core/src/datastructs.cpp
modules/core/src/flann.cpp
modules/core/src/lapack.cpp
modules/core/src/matrix.cpp
modules/features2d/include/opencv2/features2d/features2d.hpp
modules/features2d/src/calonder.cpp
modules/features2d/src/descriptors.cpp
modules/features2d/src/detectors.cpp
modules/features2d/src/fast.cpp
modules/features2d/src/mser.cpp
modules/features2d/src/oneway.cpp
modules/features2d/src/planardetect.cpp
modules/features2d/src/sift.cpp
modules/highgui/src/grfmt_bmp.cpp
modules/highgui/src/grfmt_jpeg.cpp
modules/highgui/src/grfmt_jpeg2000.cpp
modules/highgui/src/grfmt_png.cpp
modules/highgui/src/grfmt_pxm.cpp
modules/highgui/src/grfmt_sunras.cpp
modules/highgui/src/grfmt_tiff.cpp
modules/highgui/src/window_w32.cpp
modules/imgproc/src/imgwarp.cpp
modules/imgproc/src/rotcalipers.cpp
modules/imgproc/src/segmentation.cpp
modules/legacy/src/vecfacetracking.cpp
modules/objdetect/src/haar.cpp
modules/traincascade/boost.cpp
tests/cv/src/abundleadjustment.cpp
tests/cv/src/acameracalibration.cpp
tests/cv/src/acameracalibration_artificial.cpp
tests/cv/src/achesscorners.cpp
tests/cv/src/acomposeRT.cpp
tests/cv/src/acornerssubpix.cpp
tests/cv/src/areprojectImageTo3D.cpp
tests/cv/src/astereomatching.cpp
tests/cv/src/bchesscorners.cpp
tests/cv/src/fast.cpp
tests/cv/src/watershed.cpp
tests/cxcore/src/aarray.cpp
tests/cxcore/src/aio.cpp

index b6bdf37..5b30a28 100644 (file)
@@ -177,7 +177,7 @@ public:
     template <typename T>
        T* allocate(size_t count = 1)
        {
-               T* mem = (T*) this->malloc(sizeof(T)*count);
+               T* mem = (T*) this->malloc((int)(sizeof(T)*count));
                return mem;
        }
 
index 2985d69..b481a1f 100644 (file)
@@ -281,7 +281,7 @@ public:
 
     int size() const
     {
-       return items.size();
+       return (int)items.size();
     }
 
        bool full() const
index bd51803..a599788 100644 (file)
@@ -56,7 +56,7 @@ void save_header(FILE* stream, const NNIndex& index)
 IndexHeader load_header(FILE* stream)
 {
        IndexHeader header;
-       int read_size = fread(&header,sizeof(header),1,stream);
+       int read_size = (int)fread(&header,sizeof(header),1,stream);
 
        if (read_size!=1) {
                throw FLANNException("Invalid index file, cannot read");
index f10162b..9373641 100644 (file)
@@ -77,7 +77,7 @@ void save_value(FILE* stream, const T& value, int count = 1)
 template<typename T>
 void load_value(FILE* stream, T& value, int count = 1)
 {
-       int read_cnt = fread(&value, sizeof(value),count, stream);
+       int read_cnt = (int)fread(&value, sizeof(value),count, stream);
        if (read_cnt!=count) {
                throw FLANNException("Cannot read from file");
        }
index 192d48f..1be8351 100644 (file)
@@ -1,3 +1,5 @@
+/* header file for clapack 3.2.1 */
+
 #ifndef __CLAPACK_H
 #define __CLAPACK_H
 
 extern "C" {
 #endif
 
+doublereal dasum_(integer *n, doublereal *dx, integer *incx);
+
+/* Subroutine */ int daxpy_(integer *n, doublereal *da, doublereal *dx, 
+       integer *incx, doublereal *dy, integer *incy);
+
+doublereal dcabs1_(doublecomplex *z__);
+
+/* Subroutine */ int dcopy_(integer *n, doublereal *dx, integer *incx, 
+       doublereal *dy, integer *incy);
+
+doublereal ddot_(integer *n, doublereal *dx, integer *incx, doublereal *dy, 
+       integer *incy);
+
+/* Subroutine */ int dgbmv_(char *trans, integer *m, integer *n, integer *kl, 
+       integer *ku, doublereal *alpha, doublereal *a, integer *lda, 
+       doublereal *x, integer *incx, doublereal *beta, doublereal *y, 
+       integer *incy);
+
+/* Subroutine */ int dgemm_(char *transa, char *transb, integer *m, integer *
+       n, integer *k, doublereal *alpha, doublereal *a, integer *lda, 
+       doublereal *b, integer *ldb, doublereal *beta, doublereal *c__, 
+       integer *ldc);
+
+/* Subroutine */ int dgemv_(char *trans, integer *m, integer *n, doublereal *
+       alpha, doublereal *a, integer *lda, doublereal *x, integer *incx, 
+       doublereal *beta, doublereal *y, integer *incy);
+
+/* Subroutine */ int dger_(integer *m, integer *n, doublereal *alpha, 
+       doublereal *x, integer *incx, doublereal *y, integer *incy, 
+       doublereal *a, integer *lda);
+
+doublereal dnrm2_(integer *n, doublereal *x, integer *incx);
+
+/* Subroutine */ int drot_(integer *n, doublereal *dx, integer *incx, 
+       doublereal *dy, integer *incy, doublereal *c__, doublereal *s);
+
+/* Subroutine */ int drotg_(doublereal *da, doublereal *db, doublereal *c__, 
+       doublereal *s);
+
+/* Subroutine */ int drotm_(integer *n, doublereal *dx, integer *incx, 
+       doublereal *dy, integer *incy, doublereal *dparam);
+
+/* Subroutine */ int drotmg_(doublereal *dd1, doublereal *dd2, doublereal *
+       dx1, doublereal *dy1, doublereal *dparam);
+
+/* Subroutine */ int dsbmv_(char *uplo, integer *n, integer *k, doublereal *
+       alpha, doublereal *a, integer *lda, doublereal *x, integer *incx, 
+       doublereal *beta, doublereal *y, integer *incy);
+
+/* Subroutine */ int dscal_(integer *n, doublereal *da, doublereal *dx, 
+       integer *incx);
+
+doublereal dsdot_(integer *n, real *sx, integer *incx, real *sy, integer *
+       incy);
+
+/* Subroutine */ int dspmv_(char *uplo, integer *n, doublereal *alpha, 
+       doublereal *ap, doublereal *x, integer *incx, doublereal *beta, 
+       doublereal *y, integer *incy);
+
+/* Subroutine */ int dspr_(char *uplo, integer *n, doublereal *alpha, 
+       doublereal *x, integer *incx, doublereal *ap);
+
+/* Subroutine */ int dspr2_(char *uplo, integer *n, doublereal *alpha, 
+       doublereal *x, integer *incx, doublereal *y, integer *incy, 
+       doublereal *ap);
+
+/* Subroutine */ int dswap_(integer *n, doublereal *dx, integer *incx, 
+       doublereal *dy, integer *incy);
+
+/* Subroutine */ int dsymm_(char *side, char *uplo, integer *m, integer *n, 
+       doublereal *alpha, doublereal *a, integer *lda, doublereal *b, 
+       integer *ldb, doublereal *beta, doublereal *c__, integer *ldc);
+
+/* Subroutine */ int dsymv_(char *uplo, integer *n, doublereal *alpha, 
+       doublereal *a, integer *lda, doublereal *x, integer *incx, doublereal 
+       *beta, doublereal *y, integer *incy);
+
+/* Subroutine */ int dsyr_(char *uplo, integer *n, doublereal *alpha, 
+       doublereal *x, integer *incx, doublereal *a, integer *lda);
+
+/* Subroutine */ int dsyr2_(char *uplo, integer *n, doublereal *alpha, 
+       doublereal *x, integer *incx, doublereal *y, integer *incy, 
+       doublereal *a, integer *lda);
+
+/* Subroutine */ int dsyr2k_(char *uplo, char *trans, integer *n, integer *k, 
+       doublereal *alpha, doublereal *a, integer *lda, doublereal *b, 
+       integer *ldb, doublereal *beta, doublereal *c__, integer *ldc);
+
+/* Subroutine */ int dsyrk_(char *uplo, char *trans, integer *n, integer *k, 
+       doublereal *alpha, doublereal *a, integer *lda, doublereal *beta, 
+       doublereal *c__, integer *ldc);
+
+/* Subroutine */ int dtbmv_(char *uplo, char *trans, char *diag, integer *n, 
+       integer *k, doublereal *a, integer *lda, doublereal *x, integer *incx);
+
+/* Subroutine */ int dtbsv_(char *uplo, char *trans, char *diag, integer *n, 
+       integer *k, doublereal *a, integer *lda, doublereal *x, integer *incx);
+
+/* Subroutine */ int dtpmv_(char *uplo, char *trans, char *diag, integer *n, 
+       doublereal *ap, doublereal *x, integer *incx);
+
+/* Subroutine */ int dtpsv_(char *uplo, char *trans, char *diag, integer *n, 
+       doublereal *ap, doublereal *x, integer *incx);
+
+/* Subroutine */ int dtrmm_(char *side, char *uplo, char *transa, char *diag, 
+       integer *m, integer *n, doublereal *alpha, doublereal *a, integer *
+       lda, doublereal *b, integer *ldb);
+
+/* Subroutine */ int dtrmv_(char *uplo, char *trans, char *diag, integer *n, 
+       doublereal *a, integer *lda, doublereal *x, integer *incx);
+
+/* Subroutine */ int dtrsm_(char *side, char *uplo, char *transa, char *diag, 
+       integer *m, integer *n, doublereal *alpha, doublereal *a, integer *
+       lda, doublereal *b, integer *ldb);
+
+/* Subroutine */ int dtrsv_(char *uplo, char *trans, char *diag, integer *n, 
+       doublereal *a, integer *lda, doublereal *x, integer *incx);
+
+integer idamax_(integer *n, doublereal *dx, integer *incx);
+
+integer isamax_(integer *n, real *sx, integer *incx);
+
+doublereal sasum_(integer *n, real *sx, integer *incx);
+
+/* Subroutine */ int saxpy_(integer *n, real *sa, real *sx, integer *incx, 
+       real *sy, integer *incy);
+
+/* Subroutine */ int scopy_(integer *n, real *sx, integer *incx, real *sy, 
+       integer *incy);
+
+doublereal sdot_(integer *n, real *sx, integer *incx, real *sy, integer *incy);
+
+doublereal sdsdot_(integer *n, real *sb, real *sx, integer *incx, real *sy, 
+       integer *incy);
+
+/* Subroutine */ int sgbmv_(char *trans, integer *m, integer *n, integer *kl, 
+       integer *ku, real *alpha, real *a, integer *lda, real *x, integer *
+       incx, real *beta, real *y, integer *incy);
+
+/* Subroutine */ int sgemm_(char *transa, char *transb, integer *m, integer *
+       n, integer *k, real *alpha, real *a, integer *lda, real *b, integer *
+       ldb, real *beta, real *c__, integer *ldc);
+
+/* Subroutine */ int sgemv_(char *trans, integer *m, integer *n, real *alpha, 
+       real *a, integer *lda, real *x, integer *incx, real *beta, real *y, 
+       integer *incy);
+
+/* Subroutine */ int sger_(integer *m, integer *n, real *alpha, real *x, 
+       integer *incx, real *y, integer *incy, real *a, integer *lda);
+
+doublereal snrm2_(integer *n, real *x, integer *incx);
+
+/* Subroutine */ int srot_(integer *n, real *sx, integer *incx, real *sy, 
+       integer *incy, real *c__, real *s);
+
+/* Subroutine */ int srotg_(real *sa, real *sb, real *c__, real *s);
+
+/* Subroutine */ int srotm_(integer *n, real *sx, integer *incx, real *sy, 
+       integer *incy, real *sparam);
+
+/* Subroutine */ int srotmg_(real *sd1, real *sd2, real *sx1, real *sy1, real 
+       *sparam);
+
+/* Subroutine */ int ssbmv_(char *uplo, integer *n, integer *k, real *alpha, 
+       real *a, integer *lda, real *x, integer *incx, real *beta, real *y, 
+       integer *incy);
+
+/* Subroutine */ int sscal_(integer *n, real *sa, real *sx, integer *incx);
+
+/* Subroutine */ int sspmv_(char *uplo, integer *n, real *alpha, real *ap, 
+       real *x, integer *incx, real *beta, real *y, integer *incy);
+
+/* Subroutine */ int sspr_(char *uplo, integer *n, real *alpha, real *x, 
+       integer *incx, real *ap);
+
+/* Subroutine */ int sspr2_(char *uplo, integer *n, real *alpha, real *x, 
+       integer *incx, real *y, integer *incy, real *ap);
+
+/* Subroutine */ int sswap_(integer *n, real *sx, integer *incx, real *sy, 
+       integer *incy);
+
+/* Subroutine */ int ssymm_(char *side, char *uplo, integer *m, integer *n, 
+       real *alpha, real *a, integer *lda, real *b, integer *ldb, real *beta, 
+        real *c__, integer *ldc);
+
+/* Subroutine */ int ssymv_(char *uplo, integer *n, real *alpha, real *a, 
+       integer *lda, real *x, integer *incx, real *beta, real *y, integer *
+       incy);
+
+/* Subroutine */ int ssyr_(char *uplo, integer *n, real *alpha, real *x, 
+       integer *incx, real *a, integer *lda);
+
+/* Subroutine */ int ssyr2_(char *uplo, integer *n, real *alpha, real *x, 
+       integer *incx, real *y, integer *incy, real *a, integer *lda);
+
+/* Subroutine */ int ssyr2k_(char *uplo, char *trans, integer *n, integer *k, 
+       real *alpha, real *a, integer *lda, real *b, integer *ldb, real *beta, 
+        real *c__, integer *ldc);
+
+/* Subroutine */ int ssyrk_(char *uplo, char *trans, integer *n, integer *k, 
+       real *alpha, real *a, integer *lda, real *beta, real *c__, integer *
+       ldc);
+
+/* Subroutine */ int stbmv_(char *uplo, char *trans, char *diag, integer *n, 
+       integer *k, real *a, integer *lda, real *x, integer *incx);
+
+/* Subroutine */ int stbsv_(char *uplo, char *trans, char *diag, integer *n, 
+       integer *k, real *a, integer *lda, real *x, integer *incx);
+
+/* Subroutine */ int stpmv_(char *uplo, char *trans, char *diag, integer *n, 
+       real *ap, real *x, integer *incx);
+
+/* Subroutine */ int stpsv_(char *uplo, char *trans, char *diag, integer *n, 
+       real *ap, real *x, integer *incx);
+
+/* Subroutine */ int strmm_(char *side, char *uplo, char *transa, char *diag, 
+       integer *m, integer *n, real *alpha, real *a, integer *lda, real *b, 
+       integer *ldb);
+
+/* Subroutine */ int strmv_(char *uplo, char *trans, char *diag, integer *n, 
+       real *a, integer *lda, real *x, integer *incx);
+
+/* Subroutine */ int strsm_(char *side, char *uplo, char *transa, char *diag, 
+       integer *m, integer *n, real *alpha, real *a, integer *lda, real *b, 
+       integer *ldb);
+
+/* Subroutine */ int strsv_(char *uplo, char *trans, char *diag, integer *n, 
+       real *a, integer *lda, real *x, integer *incx);
+
+/* Subroutine */ int xerbla_(char *srname, integer *info);
+
+/* Subroutine */ int xerbla_array__(char *srname_array__, integer *
+       srname_len__, integer *info, ftnlen srname_array_len);
+
 /* Subroutine */ int dbdsdc_(char *uplo, char *compq, integer *n, doublereal *
        d__, doublereal *e, doublereal *u, integer *ldu, doublereal *vt, 
        integer *ldvt, doublereal *q, integer *iq, doublereal *work, integer *
@@ -21,42 +257,65 @@ extern "C" {
 /* Subroutine */ int ddisna_(char *job, integer *m, integer *n, doublereal *
        d__, doublereal *sep, integer *info);
 
-/* Subroutine */ int dgbbrd_(char *vect, integer *m, integer *n, integer *ncc,
+/* Subroutine */ int dgbbrd_(char *vect, integer *m, integer *n, integer *ncc, 
         integer *kl, integer *ku, doublereal *ab, integer *ldab, doublereal *
        d__, doublereal *e, doublereal *q, integer *ldq, doublereal *pt, 
        integer *ldpt, doublereal *c__, integer *ldc, doublereal *work, 
        integer *info);
 
-/* Subroutine */ int dgbcon_(char *norm, integer *n, integer *kl, integer *ku,
+/* Subroutine */ int dgbcon_(char *norm, integer *n, integer *kl, integer *ku, 
         doublereal *ab, integer *ldab, integer *ipiv, doublereal *anorm, 
        doublereal *rcond, doublereal *work, integer *iwork, integer *info);
 
-/* Subroutine */ int dgbequ_(integer *m, integer *n, integer *kl, integer *ku,
+/* Subroutine */ int dgbequ_(integer *m, integer *n, integer *kl, integer *ku, 
         doublereal *ab, integer *ldab, doublereal *r__, doublereal *c__, 
        doublereal *rowcnd, doublereal *colcnd, doublereal *amax, integer *
        info);
 
+/* Subroutine */ int dgbequb_(integer *m, integer *n, integer *kl, integer *
+       ku, doublereal *ab, integer *ldab, doublereal *r__, doublereal *c__, 
+       doublereal *rowcnd, doublereal *colcnd, doublereal *amax, integer *
+       info);
+
 /* Subroutine */ int dgbrfs_(char *trans, integer *n, integer *kl, integer *
        ku, integer *nrhs, doublereal *ab, integer *ldab, doublereal *afb, 
        integer *ldafb, integer *ipiv, doublereal *b, integer *ldb, 
        doublereal *x, integer *ldx, doublereal *ferr, doublereal *berr, 
        doublereal *work, integer *iwork, integer *info);
 
+/* Subroutine */ int dgbrfsx_(char *trans, char *equed, integer *n, integer *
+       kl, integer *ku, integer *nrhs, doublereal *ab, integer *ldab, 
+       doublereal *afb, integer *ldafb, integer *ipiv, doublereal *r__, 
+       doublereal *c__, doublereal *b, integer *ldb, doublereal *x, integer *
+       ldx, doublereal *rcond, doublereal *berr, integer *n_err_bnds__, 
+       doublereal *err_bnds_norm__, doublereal *err_bnds_comp__, integer *
+       nparams, doublereal *params, doublereal *work, integer *iwork, 
+       integer *info);
+
 /* Subroutine */ int dgbsv_(integer *n, integer *kl, integer *ku, integer *
        nrhs, doublereal *ab, integer *ldab, integer *ipiv, doublereal *b, 
        integer *ldb, integer *info);
 
-/* Subroutine */ int dgbsvx_(char *fact, char *trans, integer *n, integer *kl,
+/* Subroutine */ int dgbsvx_(char *fact, char *trans, integer *n, integer *kl, 
         integer *ku, integer *nrhs, doublereal *ab, integer *ldab, 
        doublereal *afb, integer *ldafb, integer *ipiv, char *equed, 
        doublereal *r__, doublereal *c__, doublereal *b, integer *ldb, 
        doublereal *x, integer *ldx, doublereal *rcond, doublereal *ferr, 
        doublereal *berr, doublereal *work, integer *iwork, integer *info);
 
-/* Subroutine */ int dgbtf2_(integer *m, integer *n, integer *kl, integer *ku,
+/* Subroutine */ int dgbsvxx_(char *fact, char *trans, integer *n, integer *
+       kl, integer *ku, integer *nrhs, doublereal *ab, integer *ldab, 
+       doublereal *afb, integer *ldafb, integer *ipiv, char *equed, 
+       doublereal *r__, doublereal *c__, doublereal *b, integer *ldb, 
+       doublereal *x, integer *ldx, doublereal *rcond, doublereal *rpvgrw, 
+       doublereal *berr, integer *n_err_bnds__, doublereal *err_bnds_norm__, 
+       doublereal *err_bnds_comp__, integer *nparams, doublereal *params, 
+       doublereal *work, integer *iwork, integer *info);
+
+/* Subroutine */ int dgbtf2_(integer *m, integer *n, integer *kl, integer *ku, 
         doublereal *ab, integer *ldab, integer *ipiv, integer *info);
 
-/* Subroutine */ int dgbtrf_(integer *m, integer *n, integer *kl, integer *ku,
+/* Subroutine */ int dgbtrf_(integer *m, integer *n, integer *kl, integer *ku, 
         doublereal *ab, integer *ldab, integer *ipiv, integer *info);
 
 /* Subroutine */ int dgbtrs_(char *trans, integer *n, integer *kl, integer *
@@ -86,6 +345,10 @@ extern "C" {
        lda, doublereal *r__, doublereal *c__, doublereal *rowcnd, doublereal 
        *colcnd, doublereal *amax, integer *info);
 
+/* Subroutine */ int dgeequb_(integer *m, integer *n, doublereal *a, integer *
+       lda, doublereal *r__, doublereal *c__, doublereal *rowcnd, doublereal 
+       *colcnd, doublereal *amax, integer *info);
+
 /* Subroutine */ int dgees_(char *jobvs, char *sort, L_fp select, integer *n, 
        doublereal *a, integer *lda, integer *sdim, doublereal *wr, 
        doublereal *wi, doublereal *vs, integer *ldvs, doublereal *work, 
@@ -106,7 +369,7 @@ extern "C" {
        sense, integer *n, doublereal *a, integer *lda, doublereal *wr, 
        doublereal *wi, doublereal *vl, integer *ldvl, doublereal *vr, 
        integer *ldvr, integer *ilo, integer *ihi, doublereal *scale, 
-       doublereal *abnrm, doublereal *rconde, doublereal *rcondv, doublereal   
+       doublereal *abnrm, doublereal *rconde, doublereal *rcondv, doublereal 
        *work, integer *lwork, integer *iwork, integer *info);
 
 /* Subroutine */ int dgegs_(char *jobvsl, char *jobvsr, integer *n, 
@@ -129,6 +392,12 @@ extern "C" {
        doublereal *a, integer *lda, doublereal *tau, doublereal *work, 
        integer *lwork, integer *info);
 
+/* Subroutine */ int dgejsv_(char *joba, char *jobu, char *jobv, char *jobr, 
+       char *jobt, char *jobp, integer *m, integer *n, doublereal *a, 
+       integer *lda, doublereal *sva, doublereal *u, integer *ldu, 
+       doublereal *v, integer *ldv, doublereal *work, integer *lwork, 
+       integer *iwork, integer *info);
+
 /* Subroutine */ int dgelq2_(integer *m, integer *n, doublereal *a, integer *
        lda, doublereal *tau, doublereal *work, integer *info);
 
@@ -141,12 +410,12 @@ extern "C" {
 
 /* Subroutine */ int dgelsd_(integer *m, integer *n, integer *nrhs, 
        doublereal *a, integer *lda, doublereal *b, integer *ldb, doublereal *
-       s, doublereal *rcond, integer *rank, doublereal *work, integer *lwork,
+       s, doublereal *rcond, integer *rank, doublereal *work, integer *lwork, 
         integer *iwork, integer *info);
 
 /* Subroutine */ int dgelss_(integer *m, integer *n, integer *nrhs, 
        doublereal *a, integer *lda, doublereal *b, integer *ldb, doublereal *
-       s, doublereal *rcond, integer *rank, doublereal *work, integer *lwork,
+       s, doublereal *rcond, integer *rank, doublereal *work, integer *lwork, 
         integer *info);
 
 /* Subroutine */ int dgelsx_(integer *m, integer *n, integer *nrhs, 
@@ -166,7 +435,7 @@ extern "C" {
        lda, doublereal *tau, doublereal *work, integer *lwork, integer *info);
 
 /* Subroutine */ int dgeqp3_(integer *m, integer *n, doublereal *a, integer *
-       lda, integer *jpvt, doublereal *tau, doublereal *work, integer *lwork,
+       lda, integer *jpvt, doublereal *tau, doublereal *work, integer *lwork, 
         integer *info);
 
 /* Subroutine */ int dgeqpf_(integer *m, integer *n, doublereal *a, integer *
@@ -184,6 +453,14 @@ extern "C" {
        doublereal *ferr, doublereal *berr, doublereal *work, integer *iwork, 
        integer *info);
 
+/* Subroutine */ int dgerfsx_(char *trans, char *equed, integer *n, integer *
+       nrhs, doublereal *a, integer *lda, doublereal *af, integer *ldaf, 
+       integer *ipiv, doublereal *r__, doublereal *c__, doublereal *b, 
+       integer *ldb, doublereal *x, integer *ldx, doublereal *rcond, 
+       doublereal *berr, integer *n_err_bnds__, doublereal *err_bnds_norm__, 
+       doublereal *err_bnds_comp__, integer *nparams, doublereal *params, 
+       doublereal *work, integer *iwork, integer *info);
+
 /* Subroutine */ int dgerq2_(integer *m, integer *n, doublereal *a, integer *
        lda, doublereal *tau, doublereal *work, integer *info);
 
@@ -206,6 +483,11 @@ extern "C" {
        ldu, doublereal *vt, integer *ldvt, doublereal *work, integer *lwork, 
        integer *info);
 
+/* Subroutine */ int dgesvj_(char *joba, char *jobu, char *jobv, integer *m, 
+       integer *n, doublereal *a, integer *lda, doublereal *sva, integer *mv, 
+        doublereal *v, integer *ldv, doublereal *work, integer *lwork, 
+       integer *info);
+
 /* Subroutine */ int dgesvx_(char *fact, char *trans, integer *n, integer *
        nrhs, doublereal *a, integer *lda, doublereal *af, integer *ldaf, 
        integer *ipiv, char *equed, doublereal *r__, doublereal *c__, 
@@ -213,6 +495,15 @@ extern "C" {
        rcond, doublereal *ferr, doublereal *berr, doublereal *work, integer *
        iwork, integer *info);
 
+/* Subroutine */ int dgesvxx_(char *fact, char *trans, integer *n, integer *
+       nrhs, doublereal *a, integer *lda, doublereal *af, integer *ldaf, 
+       integer *ipiv, char *equed, doublereal *r__, doublereal *c__, 
+       doublereal *b, integer *ldb, doublereal *x, integer *ldx, doublereal *
+       rcond, doublereal *rpvgrw, doublereal *berr, integer *n_err_bnds__, 
+       doublereal *err_bnds_norm__, doublereal *err_bnds_comp__, integer *
+       nparams, doublereal *params, doublereal *work, integer *iwork, 
+       integer *info);
+
 /* Subroutine */ int dgetc2_(integer *n, doublereal *a, integer *lda, integer 
        *ipiv, integer *jpiv, integer *info);
 
@@ -248,9 +539,9 @@ extern "C" {
 /* Subroutine */ int dggesx_(char *jobvsl, char *jobvsr, char *sort, L_fp 
        selctg, char *sense, integer *n, doublereal *a, integer *lda, 
        doublereal *b, integer *ldb, integer *sdim, doublereal *alphar, 
-       doublereal *alphai, doublereal *beta, doublereal *vsl, integer *ldvsl,
+       doublereal *alphai, doublereal *beta, doublereal *vsl, integer *ldvsl, 
         doublereal *vsr, integer *ldvsr, doublereal *rconde, doublereal *
-       rcondv, doublereal *work, integer *lwork, integer *iwork, integer *     
+       rcondv, doublereal *work, integer *lwork, integer *iwork, integer *
        liwork, logical *bwork, integer *info);
 
 /* Subroutine */ int dggev_(char *jobvl, char *jobvr, integer *n, doublereal *
@@ -265,7 +556,7 @@ extern "C" {
        beta, doublereal *vl, integer *ldvl, doublereal *vr, integer *ldvr, 
        integer *ilo, integer *ihi, doublereal *lscale, doublereal *rscale, 
        doublereal *abnrm, doublereal *bbnrm, doublereal *rconde, doublereal *
-       rcondv, doublereal *work, integer *lwork, integer *iwork, logical *     
+       rcondv, doublereal *work, integer *lwork, integer *iwork, logical *
        bwork, integer *info);
 
 /* Subroutine */ int dggglm_(integer *n, integer *m, integer *p, doublereal *
@@ -305,6 +596,18 @@ extern "C" {
        doublereal *q, integer *ldq, integer *iwork, doublereal *tau, 
        doublereal *work, integer *info);
 
+/* Subroutine */ int dgsvj0_(char *jobv, integer *m, integer *n, doublereal *
+       a, integer *lda, doublereal *d__, doublereal *sva, integer *mv, 
+       doublereal *v, integer *ldv, doublereal *eps, doublereal *sfmin, 
+       doublereal *tol, integer *nsweep, doublereal *work, integer *lwork, 
+       integer *info);
+
+/* Subroutine */ int dgsvj1_(char *jobv, integer *m, integer *n, integer *n1, 
+       doublereal *a, integer *lda, doublereal *d__, doublereal *sva, 
+       integer *mv, doublereal *v, integer *ldv, doublereal *eps, doublereal 
+       *sfmin, doublereal *tol, integer *nsweep, doublereal *work, integer *
+       lwork, integer *info);
+
 /* Subroutine */ int dgtcon_(char *norm, integer *n, doublereal *dl, 
        doublereal *d__, doublereal *du, doublereal *du2, integer *ipiv, 
        doublereal *anorm, doublereal *rcond, doublereal *work, integer *
@@ -332,7 +635,7 @@ extern "C" {
        doublereal *du, doublereal *du2, integer *ipiv, integer *info);
 
 /* Subroutine */ int dgttrs_(char *trans, integer *n, integer *nrhs, 
-       doublereal *dl, doublereal *d__, doublereal *du, doublereal *du2,       
+       doublereal *dl, doublereal *d__, doublereal *du, doublereal *du2, 
        integer *ipiv, doublereal *b, integer *ldb, integer *info);
 
 /* Subroutine */ int dgtts2_(integer *itrans, integer *n, integer *nrhs, 
@@ -351,11 +654,104 @@ extern "C" {
        integer *ldvr, integer *mm, integer *m, doublereal *work, integer *
        ifaill, integer *ifailr, integer *info);
 
-/* Subroutine */ int dhseqr_(char *job, char *compz, integer *n, integer *ilo,
+/* Subroutine */ int dhseqr_(char *job, char *compz, integer *n, integer *ilo, 
         integer *ihi, doublereal *h__, integer *ldh, doublereal *wr, 
        doublereal *wi, doublereal *z__, integer *ldz, doublereal *work, 
        integer *lwork, integer *info);
 
+logical disnan_(doublereal *din);
+
+/* Subroutine */ int dla_gbamv__(integer *trans, integer *m, integer *n, 
+       integer *kl, integer *ku, doublereal *alpha, doublereal *ab, integer *
+       ldab, doublereal *x, integer *incx, doublereal *beta, doublereal *y, 
+       integer *incy);
+
+doublereal dla_gbrcond__(char *trans, integer *n, integer *kl, integer *ku, 
+       doublereal *ab, integer *ldab, doublereal *afb, integer *ldafb, 
+       integer *ipiv, integer *cmode, doublereal *c__, integer *info, 
+       doublereal *work, integer *iwork, ftnlen trans_len);
+
+/* Subroutine */ int dla_gbrfsx_extended__(integer *prec_type__, integer *
+       trans_type__, integer *n, integer *kl, integer *ku, integer *nrhs, 
+       doublereal *ab, integer *ldab, doublereal *afb, integer *ldafb, 
+       integer *ipiv, logical *colequ, doublereal *c__, doublereal *b, 
+       integer *ldb, doublereal *y, integer *ldy, doublereal *berr_out__, 
+       integer *n_norms__, doublereal *errs_n__, doublereal *errs_c__, 
+       doublereal *res, doublereal *ayb, doublereal *dy, doublereal *
+       y_tail__, doublereal *rcond, integer *ithresh, doublereal *rthresh, 
+       doublereal *dz_ub__, logical *ignore_cwise__, integer *info);
+
+doublereal dla_gbrpvgrw__(integer *n, integer *kl, integer *ku, integer *
+       ncols, doublereal *ab, integer *ldab, doublereal *afb, integer *ldafb);
+
+/* Subroutine */ int dla_geamv__(integer *trans, integer *m, integer *n, 
+       doublereal *alpha, doublereal *a, integer *lda, doublereal *x, 
+       integer *incx, doublereal *beta, doublereal *y, integer *incy);
+
+doublereal dla_gercond__(char *trans, integer *n, doublereal *a, integer *lda,
+        doublereal *af, integer *ldaf, integer *ipiv, integer *cmode, 
+       doublereal *c__, integer *info, doublereal *work, integer *iwork, 
+       ftnlen trans_len);
+
+/* Subroutine */ int dla_gerfsx_extended__(integer *prec_type__, integer *
+       trans_type__, integer *n, integer *nrhs, doublereal *a, integer *lda, 
+       doublereal *af, integer *ldaf, integer *ipiv, logical *colequ, 
+       doublereal *c__, doublereal *b, integer *ldb, doublereal *y, integer *
+       ldy, doublereal *berr_out__, integer *n_norms__, doublereal *errs_n__,
+        doublereal *errs_c__, doublereal *res, doublereal *ayb, doublereal *
+       dy, doublereal *y_tail__, doublereal *rcond, integer *ithresh, 
+       doublereal *rthresh, doublereal *dz_ub__, logical *ignore_cwise__, 
+       integer *info);
+
+/* Subroutine */ int dla_lin_berr__(integer *n, integer *nz, integer *nrhs, 
+       doublereal *res, doublereal *ayb, doublereal *berr);
+
+doublereal dla_porcond__(char *uplo, integer *n, doublereal *a, integer *lda, 
+       doublereal *af, integer *ldaf, integer *cmode, doublereal *c__, 
+       integer *info, doublereal *work, integer *iwork, ftnlen uplo_len);
+
+/* Subroutine */ int dla_porfsx_extended__(integer *prec_type__, char *uplo, 
+       integer *n, integer *nrhs, doublereal *a, integer *lda, doublereal *
+       af, integer *ldaf, logical *colequ, doublereal *c__, doublereal *b, 
+       integer *ldb, doublereal *y, integer *ldy, doublereal *berr_out__, 
+       integer *n_norms__, doublereal *errs_n__, doublereal *errs_c__, 
+       doublereal *res, doublereal *ayb, doublereal *dy, doublereal *
+       y_tail__, doublereal *rcond, integer *ithresh, doublereal *rthresh, 
+       doublereal *dz_ub__, logical *ignore_cwise__, integer *info, ftnlen 
+       uplo_len);
+
+doublereal dla_porpvgrw__(char *uplo, integer *ncols, doublereal *a, integer *
+       lda, doublereal *af, integer *ldaf, doublereal *work, ftnlen uplo_len);
+
+doublereal dla_rpvgrw__(integer *n, integer *ncols, doublereal *a, integer *
+       lda, doublereal *af, integer *ldaf);
+
+/* Subroutine */ int dla_syamv__(integer *uplo, integer *n, doublereal *alpha,
+        doublereal *a, integer *lda, doublereal *x, integer *incx, 
+       doublereal *beta, doublereal *y, integer *incy);
+
+doublereal dla_syrcond__(char *uplo, integer *n, doublereal *a, integer *lda, 
+       doublereal *af, integer *ldaf, integer *ipiv, integer *cmode, 
+       doublereal *c__, integer *info, doublereal *work, integer *iwork, 
+       ftnlen uplo_len);
+
+/* Subroutine */ int dla_syrfsx_extended__(integer *prec_type__, char *uplo, 
+       integer *n, integer *nrhs, doublereal *a, integer *lda, doublereal *
+       af, integer *ldaf, integer *ipiv, logical *colequ, doublereal *c__, 
+       doublereal *b, integer *ldb, doublereal *y, integer *ldy, doublereal *
+       berr_out__, integer *n_norms__, doublereal *errs_n__, doublereal *
+       errs_c__, doublereal *res, doublereal *ayb, doublereal *dy, 
+       doublereal *y_tail__, doublereal *rcond, integer *ithresh, doublereal 
+       *rthresh, doublereal *dz_ub__, logical *ignore_cwise__, integer *info,
+        ftnlen uplo_len);
+
+doublereal dla_syrpvgrw__(char *uplo, integer *n, integer *info, doublereal *
+       a, integer *lda, doublereal *af, integer *ldaf, integer *ipiv, 
+       doublereal *work, ftnlen uplo_len);
+
+/* Subroutine */ int dla_wwaddw__(integer *n, doublereal *x, doublereal *y, 
+       doublereal *w);
+
 /* Subroutine */ int dlabad_(doublereal *small, doublereal *large);
 
 /* Subroutine */ int dlabrd_(integer *m, integer *n, integer *nb, doublereal *
@@ -401,12 +797,12 @@ extern "C" {
        integer *info);
 
 /* Subroutine */ int dlaed3_(integer *k, integer *n, integer *n1, doublereal *
-       d__, doublereal *q, integer *ldq, doublereal *rho, doublereal *dlamda,
+       d__, doublereal *q, integer *ldq, doublereal *rho, doublereal *dlamda, 
         doublereal *q2, integer *indx, integer *ctot, doublereal *w, 
        doublereal *s, integer *info);
 
 /* Subroutine */ int dlaed4_(integer *n, integer *i__, doublereal *d__, 
-       doublereal *z__, doublereal *delta, doublereal *rho, doublereal *dlam,
+       doublereal *z__, doublereal *delta, doublereal *rho, doublereal *dlam, 
         integer *info);
 
 /* Subroutine */ int dlaed5_(integer *i__, doublereal *d__, doublereal *z__, 
@@ -425,7 +821,7 @@ extern "C" {
 
 /* Subroutine */ int dlaed8_(integer *icompq, integer *k, integer *n, integer 
        *qsiz, doublereal *d__, doublereal *q, integer *ldq, integer *indxq, 
-       doublereal *rho, integer *cutpnt, doublereal *z__, doublereal *dlamda,
+       doublereal *rho, integer *cutpnt, doublereal *z__, doublereal *dlamda, 
         doublereal *q2, integer *ldq2, doublereal *w, integer *perm, integer 
        *givptr, integer *givcol, doublereal *givnum, integer *indxp, integer 
        *indx, integer *info);
@@ -500,6 +896,8 @@ extern "C" {
        doublereal *sest, doublereal *w, doublereal *gamma, doublereal *
        sestpr, doublereal *s, doublereal *c__);
 
+logical dlaisnan_(doublereal *din1, doublereal *din2);
+
 /* Subroutine */ int dlaln2_(logical *ltrans, integer *na, integer *nw, 
        doublereal *smin, doublereal *ca, doublereal *a, integer *lda, 
        doublereal *d1, doublereal *d2, doublereal *b, integer *ldb, 
@@ -529,8 +927,46 @@ extern "C" {
 /* Subroutine */ int dlamrg_(integer *n1, integer *n2, doublereal *a, integer 
        *dtrd1, integer *dtrd2, integer *index);
 
+integer dlaneg_(integer *n, doublereal *d__, doublereal *lld, doublereal *
+       sigma, doublereal *pivmin, integer *r__);
+
+doublereal dlangb_(char *norm, integer *n, integer *kl, integer *ku, 
+       doublereal *ab, integer *ldab, doublereal *work);
+
+doublereal dlange_(char *norm, integer *m, integer *n, doublereal *a, integer 
+       *lda, doublereal *work);
+
+doublereal dlangt_(char *norm, integer *n, doublereal *dl, doublereal *d__, 
+       doublereal *du);
+
+doublereal dlanhs_(char *norm, integer *n, doublereal *a, integer *lda, 
+       doublereal *work);
+
+doublereal dlansb_(char *norm, char *uplo, integer *n, integer *k, doublereal 
+       *ab, integer *ldab, doublereal *work);
+
+doublereal dlansf_(char *norm, char *transr, char *uplo, integer *n, 
+       doublereal *a, doublereal *work);
+
+doublereal dlansp_(char *norm, char *uplo, integer *n, doublereal *ap, 
+       doublereal *work);
+
+doublereal dlanst_(char *norm, integer *n, doublereal *d__, doublereal *e);
+
+doublereal dlansy_(char *norm, char *uplo, integer *n, doublereal *a, integer 
+       *lda, doublereal *work);
+
+doublereal dlantb_(char *norm, char *uplo, char *diag, integer *n, integer *k, 
+        doublereal *ab, integer *ldab, doublereal *work);
+
+doublereal dlantp_(char *norm, char *uplo, char *diag, integer *n, doublereal 
+       *ap, doublereal *work);
+
+doublereal dlantr_(char *norm, char *uplo, char *diag, integer *m, integer *n, 
+        doublereal *a, integer *lda, doublereal *work);
+
 /* Subroutine */ int dlanv2_(doublereal *a, doublereal *b, doublereal *c__, 
-       doublereal *d__, doublereal *rt1r, doublereal *rt1i, doublereal *rt2r,
+       doublereal *d__, doublereal *rt1r, doublereal *rt1i, doublereal *rt2r, 
         doublereal *rt2i, doublereal *cs, doublereal *sn);
 
 /* Subroutine */ int dlapll_(integer *n, doublereal *x, integer *incx, 
@@ -539,7 +975,11 @@ extern "C" {
 /* Subroutine */ int dlapmt_(logical *forwrd, integer *m, integer *n, 
        doublereal *x, integer *ldx, integer *k);
 
-/* Subroutine */ int dlaqgb_(integer *m, integer *n, integer *kl, integer *ku,
+doublereal dlapy2_(doublereal *x, doublereal *y);
+
+doublereal dlapy3_(doublereal *x, doublereal *y, doublereal *z__);
+
+/* Subroutine */ int dlaqgb_(integer *m, integer *n, integer *kl, integer *ku, 
         doublereal *ab, integer *ldab, doublereal *r__, doublereal *c__, 
        doublereal *rowcnd, doublereal *colcnd, doublereal *amax, char *equed);
 
@@ -592,7 +1032,7 @@ extern "C" {
        integer *ldwv, integer *nh, doublereal *wh, integer *ldwh);
 
 /* Subroutine */ int dlaqsb_(char *uplo, integer *n, integer *kd, doublereal *
-       ab, integer *ldab, doublereal *s, doublereal *scond, doublereal *amax,
+       ab, integer *ldab, doublereal *s, doublereal *scond, doublereal *amax, 
         char *equed);
 
 /* Subroutine */ int dlaqsp_(char *uplo, integer *n, doublereal *ap, 
@@ -616,7 +1056,7 @@ extern "C" {
        doublereal *z__, integer *incx, doublereal *c__, doublereal *s, 
        integer *incc);
 
-/* Subroutine */ int dlarf_(char *side, integer *m, integer *n, doublereal *v,
+/* Subroutine */ int dlarf_(char *side, integer *m, integer *n, doublereal *v, 
         integer *incv, doublereal *tau, doublereal *c__, integer *ldc, 
        doublereal *work);
 
@@ -628,6 +1068,9 @@ extern "C" {
 /* Subroutine */ int dlarfg_(integer *n, doublereal *alpha, doublereal *x, 
        integer *incx, doublereal *tau);
 
+/* Subroutine */ int dlarfp_(integer *n, doublereal *alpha, doublereal *x, 
+       integer *incx, doublereal *tau);
+
 /* Subroutine */ int dlarft_(char *direct, char *storev, integer *n, integer *
        k, doublereal *v, integer *ldv, doublereal *tau, doublereal *t, 
        integer *ldt);
@@ -642,11 +1085,11 @@ extern "C" {
        doublereal *x);
 
 /* Subroutine */ int dlarra_(integer *n, doublereal *d__, doublereal *e, 
-       doublereal *e2, doublereal *spltol, doublereal *tnrm, integer *nsplit,
+       doublereal *e2, doublereal *spltol, doublereal *tnrm, integer *nsplit, 
         integer *isplit, integer *info);
 
 /* Subroutine */ int dlarrb_(integer *n, doublereal *d__, doublereal *lld, 
-       integer *ifirst, integer *ilast, doublereal *rtol1, doublereal *rtol2,
+       integer *ifirst, integer *ilast, doublereal *rtol1, doublereal *rtol2, 
         integer *offset, doublereal *w, doublereal *wgap, doublereal *werr, 
        doublereal *work, integer *iwork, doublereal *pivmin, doublereal *
        spdiam, integer *twist, integer *info);
@@ -692,11 +1135,14 @@ extern "C" {
 /* Subroutine */ int dlarrv_(integer *n, doublereal *vl, doublereal *vu, 
        doublereal *d__, doublereal *l, doublereal *pivmin, integer *isplit, 
        integer *m, integer *dol, integer *dou, doublereal *minrgp, 
-       doublereal *rtol1, doublereal *rtol2, doublereal *w, doublereal *werr,
-        doublereal *wgap, integer *iblock, integer *indexw, doublereal *gers,
+       doublereal *rtol1, doublereal *rtol2, doublereal *w, doublereal *werr, 
+        doublereal *wgap, integer *iblock, integer *indexw, doublereal *gers, 
         doublereal *z__, integer *ldz, integer *isuppz, doublereal *work, 
        integer *iwork, integer *info);
 
+/* Subroutine */ int dlarscl2_(integer *m, integer *n, doublereal *d__, 
+       doublereal *x, integer *ldx);
+
 /* Subroutine */ int dlartg_(doublereal *f, doublereal *g, doublereal *cs, 
        doublereal *sn, doublereal *r__);
 
@@ -711,9 +1157,9 @@ extern "C" {
        integer *ldc, doublereal *work);
 
 /* Subroutine */ int dlarzb_(char *side, char *trans, char *direct, char *
-       storev, integer *m, integer *n, integer *k, integer *l, doublereal *v,
+       storev, integer *m, integer *n, integer *k, integer *l, doublereal *v, 
         integer *ldv, doublereal *t, integer *ldt, doublereal *c__, integer *
-       ldc, doublereal *work, integer *ldwork          );
+       ldc, doublereal *work, integer *ldwork);
 
 /* Subroutine */ int dlarzt_(char *direct, char *storev, integer *n, integer *
        k, doublereal *v, integer *ldv, doublereal *tau, doublereal *t, 
@@ -726,6 +1172,9 @@ extern "C" {
        doublereal *cfrom, doublereal *cto, integer *m, integer *n, 
        doublereal *a, integer *lda, integer *info);
 
+/* Subroutine */ int dlascl2_(integer *m, integer *n, doublereal *d__, 
+       doublereal *x, integer *ldx);
+
 /* Subroutine */ int dlasd0_(integer *n, integer *sqre, doublereal *d__, 
        doublereal *e, doublereal *u, integer *ldu, doublereal *vt, integer *
        ldvt, integer *smlsiz, integer *iwork, doublereal *work, integer *
@@ -760,7 +1209,7 @@ extern "C" {
 /* Subroutine */ int dlasd6_(integer *icompq, integer *nl, integer *nr, 
        integer *sqre, doublereal *d__, doublereal *vf, doublereal *vl, 
        doublereal *alpha, doublereal *beta, integer *idxq, integer *perm, 
-       integer *givptr, integer *givcol, integer *ldgcol, doublereal *givnum,
+       integer *givptr, integer *givcol, integer *ldgcol, doublereal *givnum, 
         integer *ldgnum, doublereal *poles, doublereal *difl, doublereal *
        difr, doublereal *z__, integer *k, doublereal *c__, doublereal *s, 
        doublereal *work, integer *iwork, integer *info);
@@ -770,7 +1219,7 @@ extern "C" {
        doublereal *zw, doublereal *vf, doublereal *vfw, doublereal *vl, 
        doublereal *vlw, doublereal *alpha, doublereal *beta, doublereal *
        dsigma, integer *idx, integer *idxp, integer *idxq, integer *perm, 
-       integer *givptr, integer *givcol, integer *ldgcol, doublereal *givnum,
+       integer *givptr, integer *givcol, integer *ldgcol, doublereal *givnum, 
         integer *ldgnum, doublereal *c__, doublereal *s, integer *info);
 
 /* Subroutine */ int dlasd8_(integer *icompq, integer *k, doublereal *d__, 
@@ -802,25 +1251,27 @@ extern "C" {
 /* Subroutine */ int dlasq2_(integer *n, doublereal *z__, integer *info);
 
 /* Subroutine */ int dlasq3_(integer *i0, integer *n0, doublereal *z__, 
-       integer *pp, doublereal *dmin__, doublereal *sigma, doublereal *desig,
+       integer *pp, doublereal *dmin__, doublereal *sigma, doublereal *desig, 
         doublereal *qmax, integer *nfail, integer *iter, integer *ndiv, 
-       logical *ieee);
+       logical *ieee, integer *ttype, doublereal *dmin1, doublereal *dmin2, 
+       doublereal *dn, doublereal *dn1, doublereal *dn2, doublereal *g, 
+       doublereal *tau);
 
 /* Subroutine */ int dlasq4_(integer *i0, integer *n0, doublereal *z__, 
        integer *pp, integer *n0in, doublereal *dmin__, doublereal *dmin1, 
        doublereal *dmin2, doublereal *dn, doublereal *dn1, doublereal *dn2, 
-       doublereal *tau, integer *ttype);
+       doublereal *tau, integer *ttype, doublereal *g);
 
 /* Subroutine */ int dlasq5_(integer *i0, integer *n0, doublereal *z__, 
        integer *pp, doublereal *tau, doublereal *dmin__, doublereal *dmin1, 
-       doublereal *dmin2, doublereal *dn, doublereal *dnm1, doublereal *dnm2,
+       doublereal *dmin2, doublereal *dn, doublereal *dnm1, doublereal *dnm2, 
         logical *ieee);
 
 /* Subroutine */ int dlasq6_(integer *i0, integer *n0, doublereal *z__, 
-       integer *pp, doublereal *dmin__, doublereal *dmin1, doublereal *dmin2,
+       integer *pp, doublereal *dmin__, doublereal *dmin1, doublereal *dmin2, 
         doublereal *dn, doublereal *dnm1, doublereal *dnm2);
 
-/* Subroutine */ int dlasr_(char *side, char *pivot, char *direct, integer *m,
+/* Subroutine */ int dlasr_(char *side, char *pivot, char *direct, integer *m, 
         integer *n, doublereal *c__, doublereal *s, doublereal *a, integer *
        lda);
 
@@ -842,10 +1293,13 @@ extern "C" {
        tr, integer *ldtr, doublereal *b, integer *ldb, doublereal *scale, 
        doublereal *x, integer *ldx, doublereal *xnorm, integer *info);
 
-/* Subroutine */ int dlasyf_(char *uplo, integer *n, integer *nb, integer *kb,
+/* Subroutine */ int dlasyf_(char *uplo, integer *n, integer *nb, integer *kb, 
         doublereal *a, integer *lda, integer *ipiv, doublereal *w, integer *
        ldw, integer *info);
 
+/* Subroutine */ int dlat2s_(char *uplo, integer *n, doublereal *a, integer *
+       lda, real *sa, integer *ldsa, integer *info);
+
 /* Subroutine */ int dlatbs_(char *uplo, char *trans, char *diag, char *
        normin, integer *n, integer *kd, doublereal *ab, integer *ldab, 
        doublereal *x, doublereal *scale, doublereal *cnorm, integer *info);
@@ -879,17 +1333,6 @@ extern "C" {
 /* Subroutine */ int dlauum_(char *uplo, integer *n, doublereal *a, integer *
        lda, integer *info);
 
-/* Subroutine */ int dlazq3_(integer *i0, integer *n0, doublereal *z__, 
-       integer *pp, doublereal *dmin__, doublereal *sigma, doublereal *desig,
-        doublereal *qmax, integer *nfail, integer *iter, integer *ndiv, 
-       logical *ieee, integer *ttype, doublereal *dmin1, doublereal *dmin2, 
-       doublereal *dn, doublereal *dn1, doublereal *dn2, doublereal *tau);
-
-/* Subroutine */ int dlazq4_(integer *i0, integer *n0, doublereal *z__, 
-       integer *pp, integer *n0in, doublereal *dmin__, doublereal *dmin1, 
-       doublereal *dmin2, doublereal *dn, doublereal *dn1, doublereal *dn2, 
-       doublereal *tau, integer *ttype, doublereal *g);
-
 /* Subroutine */ int dopgtr_(char *uplo, integer *n, doublereal *ap, 
        doublereal *tau, doublereal *q, integer *ldq, doublereal *work, 
        integer *info);
@@ -997,7 +1440,7 @@ extern "C" {
        work, integer *iwork, integer *info);
 
 /* Subroutine */ int dpbequ_(char *uplo, integer *n, integer *kd, doublereal *
-       ab, integer *ldab, doublereal *s, doublereal *scond, doublereal *amax,
+       ab, integer *ldab, doublereal *s, doublereal *scond, doublereal *amax, 
         integer *info);
 
 /* Subroutine */ int dpbrfs_(char *uplo, integer *n, integer *kd, integer *
@@ -1016,7 +1459,7 @@ extern "C" {
 /* Subroutine */ int dpbsvx_(char *fact, char *uplo, integer *n, integer *kd, 
        integer *nrhs, doublereal *ab, integer *ldab, doublereal *afb, 
        integer *ldafb, char *equed, doublereal *s, doublereal *b, integer *
-       ldb, doublereal *x, integer *ldx, doublereal *rcond, doublereal *ferr,
+       ldb, doublereal *x, integer *ldx, doublereal *rcond, doublereal *ferr, 
         doublereal *berr, doublereal *work, integer *iwork, integer *info);
 
 /* Subroutine */ int dpbtf2_(char *uplo, integer *n, integer *kd, doublereal *
@@ -1029,6 +1472,15 @@ extern "C" {
        nrhs, doublereal *ab, integer *ldab, doublereal *b, integer *ldb, 
        integer *info);
 
+/* Subroutine */ int dpftrf_(char *transr, char *uplo, integer *n, doublereal 
+       *a, integer *info);
+
+/* Subroutine */ int dpftri_(char *transr, char *uplo, integer *n, doublereal 
+       *a, integer *info);
+
+/* Subroutine */ int dpftrs_(char *transr, char *uplo, integer *n, integer *
+       nrhs, doublereal *a, doublereal *b, integer *ldb, integer *info);
+
 /* Subroutine */ int dpocon_(char *uplo, integer *n, doublereal *a, integer *
        lda, doublereal *anorm, doublereal *rcond, doublereal *work, integer *
        iwork, integer *info);
@@ -1036,12 +1488,23 @@ extern "C" {
 /* Subroutine */ int dpoequ_(integer *n, doublereal *a, integer *lda, 
        doublereal *s, doublereal *scond, doublereal *amax, integer *info);
 
+/* Subroutine */ int dpoequb_(integer *n, doublereal *a, integer *lda, 
+       doublereal *s, doublereal *scond, doublereal *amax, integer *info);
+
 /* Subroutine */ int dporfs_(char *uplo, integer *n, integer *nrhs, 
        doublereal *a, integer *lda, doublereal *af, integer *ldaf, 
        doublereal *b, integer *ldb, doublereal *x, integer *ldx, doublereal *
        ferr, doublereal *berr, doublereal *work, integer *iwork, integer *
        info);
 
+/* Subroutine */ int dporfsx_(char *uplo, char *equed, integer *n, integer *
+       nrhs, doublereal *a, integer *lda, doublereal *af, integer *ldaf, 
+       doublereal *s, doublereal *b, integer *ldb, doublereal *x, integer *
+       ldx, doublereal *rcond, doublereal *berr, integer *n_err_bnds__, 
+       doublereal *err_bnds_norm__, doublereal *err_bnds_comp__, integer *
+       nparams, doublereal *params, doublereal *work, integer *iwork, 
+       integer *info);
+
 /* Subroutine */ int dposv_(char *uplo, integer *n, integer *nrhs, doublereal 
        *a, integer *lda, doublereal *b, integer *ldb, integer *info);
 
@@ -1051,6 +1514,14 @@ extern "C" {
        x, integer *ldx, doublereal *rcond, doublereal *ferr, doublereal *
        berr, doublereal *work, integer *iwork, integer *info);
 
+/* Subroutine */ int dposvxx_(char *fact, char *uplo, integer *n, integer *
+       nrhs, doublereal *a, integer *lda, doublereal *af, integer *ldaf, 
+       char *equed, doublereal *s, doublereal *b, integer *ldb, doublereal *
+       x, integer *ldx, doublereal *rcond, doublereal *rpvgrw, doublereal *
+       berr, integer *n_err_bnds__, doublereal *err_bnds_norm__, doublereal *
+       err_bnds_comp__, integer *nparams, doublereal *params, doublereal *
+       work, integer *iwork, integer *info);
+
 /* Subroutine */ int dpotf2_(char *uplo, integer *n, doublereal *a, integer *
        lda, integer *info);
 
@@ -1094,6 +1565,14 @@ extern "C" {
 /* Subroutine */ int dpptrs_(char *uplo, integer *n, integer *nrhs, 
        doublereal *ap, doublereal *b, integer *ldb, integer *info);
 
+/* Subroutine */ int dpstf2_(char *uplo, integer *n, doublereal *a, integer *
+       lda, integer *piv, integer *rank, doublereal *tol, doublereal *work, 
+       integer *info);
+
+/* Subroutine */ int dpstrf_(char *uplo, integer *n, doublereal *a, integer *
+       lda, integer *piv, integer *rank, doublereal *tol, doublereal *work, 
+       integer *info);
+
 /* Subroutine */ int dptcon_(integer *n, doublereal *d__, doublereal *e, 
        doublereal *anorm, doublereal *rcond, doublereal *work, integer *info);
 
@@ -1103,7 +1582,7 @@ extern "C" {
 
 /* Subroutine */ int dptrfs_(integer *n, integer *nrhs, doublereal *d__, 
        doublereal *e, doublereal *df, doublereal *ef, doublereal *b, integer 
-       *ldb, doublereal *x, integer *ldx, doublereal *ferr, doublereal *berr,
+       *ldb, doublereal *x, integer *ldx, doublereal *ferr, doublereal *berr, 
         doublereal *work, integer *info);
 
 /* Subroutine */ int dptsv_(integer *n, integer *nrhs, doublereal *d__, 
@@ -1168,6 +1647,10 @@ extern "C" {
        doublereal *ab, integer *ldab, doublereal *d__, doublereal *e, 
        doublereal *q, integer *ldq, doublereal *work, integer *info);
 
+/* Subroutine */ int dsfrk_(char *transr, char *uplo, char *trans, integer *n, 
+        integer *k, doublereal *alpha, doublereal *a, integer *lda, 
+       doublereal *beta, doublereal *c__);
+
 /* Subroutine */ int dsgesv_(integer *n, integer *nrhs, doublereal *a, 
        integer *lda, integer *ipiv, doublereal *b, integer *ldb, doublereal *
        x, integer *ldx, doublereal *work, real *swork, integer *iter, 
@@ -1209,6 +1692,11 @@ extern "C" {
        *m, doublereal *w, doublereal *z__, integer *ldz, doublereal *work, 
        integer *iwork, integer *ifail, integer *info);
 
+/* Subroutine */ int dsposv_(char *uplo, integer *n, integer *nrhs, 
+       doublereal *a, integer *lda, doublereal *b, integer *ldb, doublereal *
+       x, integer *ldx, doublereal *work, real *swork, integer *iter, 
+       integer *info);
+
 /* Subroutine */ int dsprfs_(char *uplo, integer *n, integer *nrhs, 
        doublereal *ap, doublereal *afp, integer *ipiv, doublereal *b, 
        integer *ldb, doublereal *x, integer *ldx, doublereal *ferr, 
@@ -1259,7 +1747,7 @@ extern "C" {
 
 /* Subroutine */ int dstemr_(char *jobz, char *range, integer *n, doublereal *
        d__, doublereal *e, doublereal *vl, doublereal *vu, integer *il, 
-       integer *iu, integer *m, doublereal *w, doublereal *z__, integer *ldz,
+       integer *iu, integer *m, doublereal *w, doublereal *z__, integer *ldz, 
         integer *nzc, integer *isuppz, logical *tryrac, doublereal *work, 
        integer *lwork, integer *iwork, integer *liwork, integer *info);
 
@@ -1294,7 +1782,11 @@ extern "C" {
        lda, integer *ipiv, doublereal *anorm, doublereal *rcond, doublereal *
        work, integer *iwork, integer *info);
 
-/* Subroutine */ int dsyev_(char *jobz, char *uplo, integer *n, doublereal *a,
+/* Subroutine */ int dsyequb_(char *uplo, integer *n, doublereal *a, integer *
+       lda, doublereal *s, doublereal *scond, doublereal *amax, doublereal *
+       work, integer *info);
+
+/* Subroutine */ int dsyev_(char *jobz, char *uplo, integer *n, doublereal *a, 
         integer *lda, doublereal *w, doublereal *work, integer *lwork, 
        integer *info);
 
@@ -1344,6 +1836,14 @@ extern "C" {
        doublereal *ferr, doublereal *berr, doublereal *work, integer *iwork, 
        integer *info);
 
+/* Subroutine */ int dsyrfsx_(char *uplo, char *equed, integer *n, integer *
+       nrhs, doublereal *a, integer *lda, doublereal *af, integer *ldaf, 
+       integer *ipiv, doublereal *s, doublereal *b, integer *ldb, doublereal 
+       *x, integer *ldx, doublereal *rcond, doublereal *berr, integer *
+       n_err_bnds__, doublereal *err_bnds_norm__, doublereal *
+       err_bnds_comp__, integer *nparams, doublereal *params, doublereal *
+       work, integer *iwork, integer *info);
+
 /* Subroutine */ int dsysv_(char *uplo, integer *n, integer *nrhs, doublereal 
        *a, integer *lda, integer *ipiv, doublereal *b, integer *ldb, 
        doublereal *work, integer *lwork, integer *info);
@@ -1354,6 +1854,14 @@ extern "C" {
        ldx, doublereal *rcond, doublereal *ferr, doublereal *berr, 
        doublereal *work, integer *lwork, integer *iwork, integer *info);
 
+/* Subroutine */ int dsysvxx_(char *fact, char *uplo, integer *n, integer *
+       nrhs, doublereal *a, integer *lda, doublereal *af, integer *ldaf, 
+       integer *ipiv, char *equed, doublereal *s, doublereal *b, integer *
+       ldb, doublereal *x, integer *ldx, doublereal *rcond, doublereal *
+       rpvgrw, doublereal *berr, integer *n_err_bnds__, doublereal *
+       err_bnds_norm__, doublereal *err_bnds_comp__, integer *nparams, 
+       doublereal *params, doublereal *work, integer *iwork, integer *info);
+
 /* Subroutine */ int dsytd2_(char *uplo, integer *n, doublereal *a, integer *
        lda, doublereal *d__, doublereal *e, doublereal *tau, integer *info);
 
@@ -1387,6 +1895,19 @@ extern "C" {
        integer *kd, integer *nrhs, doublereal *ab, integer *ldab, doublereal 
        *b, integer *ldb, integer *info);
 
+/* Subroutine */ int dtfsm_(char *transr, char *side, char *uplo, char *trans, 
+        char *diag, integer *m, integer *n, doublereal *alpha, doublereal *a, 
+        doublereal *b, integer *ldb);
+
+/* Subroutine */ int dtftri_(char *transr, char *uplo, char *diag, integer *n, 
+        doublereal *a, integer *info);
+
+/* Subroutine */ int dtfttp_(char *transr, char *uplo, integer *n, doublereal 
+       *arf, doublereal *ap, integer *info);
+
+/* Subroutine */ int dtfttr_(char *transr, char *uplo, integer *n, doublereal 
+       *arf, doublereal *a, integer *lda, integer *info);
+
 /* Subroutine */ int dtgevc_(char *side, char *howmny, logical *select, 
        integer *n, doublereal *s, integer *lds, doublereal *p, integer *ldp, 
        doublereal *vl, integer *ldvl, doublereal *vr, integer *ldvr, integer 
@@ -1453,6 +1974,12 @@ extern "C" {
        integer *nrhs, doublereal *ap, doublereal *b, integer *ldb, integer *
        info);
 
+/* Subroutine */ int dtpttf_(char *transr, char *uplo, integer *n, doublereal 
+       *ap, doublereal *arf, integer *info);
+
+/* Subroutine */ int dtpttr_(char *uplo, integer *n, doublereal *ap, 
+       doublereal *a, integer *lda, integer *info);
+
 /* Subroutine */ int dtrcon_(char *norm, char *uplo, char *diag, integer *n, 
        doublereal *a, integer *lda, doublereal *rcond, doublereal *work, 
        integer *iwork, integer *info);
@@ -1497,61 +2024,127 @@ extern "C" {
        integer *nrhs, doublereal *a, integer *lda, doublereal *b, integer *
        ldb, integer *info);
 
+/* Subroutine */ int dtrttf_(char *transr, char *uplo, integer *n, doublereal 
+       *a, integer *lda, doublereal *arf, integer *info);
+
+/* Subroutine */ int dtrttp_(char *uplo, integer *n, doublereal *a, integer *
+       lda, doublereal *ap, integer *info);
+
 /* Subroutine */ int dtzrqf_(integer *m, integer *n, doublereal *a, integer *
        lda, doublereal *tau, integer *info);
 
 /* Subroutine */ int dtzrzf_(integer *m, integer *n, doublereal *a, integer *
        lda, doublereal *tau, doublereal *work, integer *lwork, integer *info);
 
+doublereal dzsum1_(integer *n, doublecomplex *cx, integer *incx);
+
+integer icmax1_(integer *n, complex *cx, integer *incx);
+
+integer ieeeck_(integer *ispec, real *zero, real *one);
+
+integer ilaclc_(integer *m, integer *n, complex *a, integer *lda);
+
+integer ilaclr_(integer *m, integer *n, complex *a, integer *lda);
+
+integer iladiag_(char *diag);
+
+integer iladlc_(integer *m, integer *n, doublereal *a, integer *lda);
+
+integer iladlr_(integer *m, integer *n, doublereal *a, integer *lda);
+
+integer ilaenv_(integer *ispec, char *name__, char *opts, integer *n1, 
+       integer *n2, integer *n3, integer *n4);
+
+integer ilaprec_(char *prec);
+
+integer ilaslc_(integer *m, integer *n, real *a, integer *lda);
+
+integer ilaslr_(integer *m, integer *n, real *a, integer *lda);
+
+integer ilatrans_(char *trans);
+
+integer ilauplo_(char *uplo);
+
 /* Subroutine */ int ilaver_(integer *vers_major__, integer *vers_minor__, 
        integer *vers_patch__);
 
-/* Subroutine */ int dgesv_(integer *n, integer *nrhs, doublereal *a, integer 
-       *lda, integer *ipiv, doublereal *b, integer *ldb, integer *info);
+integer ilazlc_(integer *m, integer *n, doublecomplex *a, integer *lda);
+
+integer ilazlr_(integer *m, integer *n, doublecomplex *a, integer *lda);
+
+integer iparmq_(integer *ispec, char *name__, char *opts, integer *n, integer 
+       *ilo, integer *ihi, integer *lwork);
+
+integer izmax1_(integer *n, doublecomplex *cx, integer *incx);
+
+logical lsamen_(integer *n, char *ca, char *cb);
+
+integer smaxloc_(real *a, integer *dimm);
 
 /* Subroutine */ int sbdsdc_(char *uplo, char *compq, integer *n, real *d__, 
-       real *e, real *u, integer *ldu, real *vt, integer *ldvt, real *q,       
+       real *e, real *u, integer *ldu, real *vt, integer *ldvt, real *q, 
        integer *iq, real *work, integer *iwork, integer *info);
 
 /* Subroutine */ int sbdsqr_(char *uplo, integer *n, integer *ncvt, integer *
        nru, integer *ncc, real *d__, real *e, real *vt, integer *ldvt, real *
        u, integer *ldu, real *c__, integer *ldc, real *work, integer *info);
 
+doublereal scsum1_(integer *n, complex *cx, integer *incx);
+
 /* Subroutine */ int sdisna_(char *job, integer *m, integer *n, real *d__, 
        real *sep, integer *info);
 
-/* Subroutine */ int sgbbrd_(char *vect, integer *m, integer *n, integer *ncc,
+/* Subroutine */ int sgbbrd_(char *vect, integer *m, integer *n, integer *ncc, 
         integer *kl, integer *ku, real *ab, integer *ldab, real *d__, real *
        e, real *q, integer *ldq, real *pt, integer *ldpt, real *c__, integer 
        *ldc, real *work, integer *info);
 
-/* Subroutine */ int sgbcon_(char *norm, integer *n, integer *kl, integer *ku,
+/* Subroutine */ int sgbcon_(char *norm, integer *n, integer *kl, integer *ku, 
         real *ab, integer *ldab, integer *ipiv, real *anorm, real *rcond, 
        real *work, integer *iwork, integer *info);
 
-/* Subroutine */ int sgbequ_(integer *m, integer *n, integer *kl, integer *ku,
+/* Subroutine */ int sgbequ_(integer *m, integer *n, integer *kl, integer *ku, 
         real *ab, integer *ldab, real *r__, real *c__, real *rowcnd, real *
        colcnd, real *amax, integer *info);
 
+/* Subroutine */ int sgbequb_(integer *m, integer *n, integer *kl, integer *
+       ku, real *ab, integer *ldab, real *r__, real *c__, real *rowcnd, real 
+       *colcnd, real *amax, integer *info);
+
 /* Subroutine */ int sgbrfs_(char *trans, integer *n, integer *kl, integer *
-       ku, integer *nrhs, real *ab, integer *ldab, real *afb, integer *ldafb,
+       ku, integer *nrhs, real *ab, integer *ldab, real *afb, integer *ldafb, 
         integer *ipiv, real *b, integer *ldb, real *x, integer *ldx, real *
        ferr, real *berr, real *work, integer *iwork, integer *info);
 
+/* Subroutine */ int sgbrfsx_(char *trans, char *equed, integer *n, integer *
+       kl, integer *ku, integer *nrhs, real *ab, integer *ldab, real *afb, 
+       integer *ldafb, integer *ipiv, real *r__, real *c__, real *b, integer 
+       *ldb, real *x, integer *ldx, real *rcond, real *berr, integer *
+       n_err_bnds__, real *err_bnds_norm__, real *err_bnds_comp__, integer *
+       nparams, real *params, real *work, integer *iwork, integer *info);
+
 /* Subroutine */ int sgbsv_(integer *n, integer *kl, integer *ku, integer *
        nrhs, real *ab, integer *ldab, integer *ipiv, real *b, integer *ldb, 
        integer *info);
 
-/* Subroutine */ int sgbsvx_(char *fact, char *trans, integer *n, integer *kl,
+/* Subroutine */ int sgbsvx_(char *fact, char *trans, integer *n, integer *kl, 
         integer *ku, integer *nrhs, real *ab, integer *ldab, real *afb, 
        integer *ldafb, integer *ipiv, char *equed, real *r__, real *c__, 
-       real *b, integer *ldb, real *x, integer *ldx, real *rcond, real *ferr,
+       real *b, integer *ldb, real *x, integer *ldx, real *rcond, real *ferr, 
         real *berr, real *work, integer *iwork, integer *info);
 
-/* Subroutine */ int sgbtf2_(integer *m, integer *n, integer *kl, integer *ku,
+/* Subroutine */ int sgbsvxx_(char *fact, char *trans, integer *n, integer *
+       kl, integer *ku, integer *nrhs, real *ab, integer *ldab, real *afb, 
+       integer *ldafb, integer *ipiv, char *equed, real *r__, real *c__, 
+       real *b, integer *ldb, real *x, integer *ldx, real *rcond, real *
+       rpvgrw, real *berr, integer *n_err_bnds__, real *err_bnds_norm__, 
+       real *err_bnds_comp__, integer *nparams, real *params, real *work, 
+       integer *iwork, integer *info);
+
+/* Subroutine */ int sgbtf2_(integer *m, integer *n, integer *kl, integer *ku, 
         real *ab, integer *ldab, integer *ipiv, integer *info);
 
-/* Subroutine */ int sgbtrf_(integer *m, integer *n, integer *kl, integer *ku,
+/* Subroutine */ int sgbtrf_(integer *m, integer *n, integer *kl, integer *ku, 
         real *ab, integer *ldab, integer *ipiv, integer *info);
 
 /* Subroutine */ int sgbtrs_(char *trans, integer *n, integer *kl, integer *
@@ -1579,6 +2172,10 @@ extern "C" {
        real *r__, real *c__, real *rowcnd, real *colcnd, real *amax, integer 
        *info);
 
+/* Subroutine */ int sgeequb_(integer *m, integer *n, real *a, integer *lda, 
+       real *r__, real *c__, real *rowcnd, real *colcnd, real *amax, integer 
+       *info);
+
 /* Subroutine */ int sgees_(char *jobvs, char *sort, L_fp select, integer *n, 
        real *a, integer *lda, integer *sdim, real *wr, real *wi, real *vs, 
        integer *ldvs, real *work, integer *lwork, logical *bwork, integer *
@@ -1587,22 +2184,22 @@ extern "C" {
 /* Subroutine */ int sgeesx_(char *jobvs, char *sort, L_fp select, char *
        sense, integer *n, real *a, integer *lda, integer *sdim, real *wr, 
        real *wi, real *vs, integer *ldvs, real *rconde, real *rcondv, real *
-       work, integer *lwork, integer *iwork, integer *liwork, logical *bwork,
+       work, integer *lwork, integer *iwork, integer *liwork, logical *bwork, 
         integer *info);
 
 /* Subroutine */ int sgeev_(char *jobvl, char *jobvr, integer *n, real *a, 
-       integer *lda, real *wr, real *wi, real *vl, integer *ldvl, real *vr,    
+       integer *lda, real *wr, real *wi, real *vl, integer *ldvl, real *vr, 
        integer *ldvr, real *work, integer *lwork, integer *info);
 
 /* Subroutine */ int sgeevx_(char *balanc, char *jobvl, char *jobvr, char *
        sense, integer *n, real *a, integer *lda, real *wr, real *wi, real *
        vl, integer *ldvl, real *vr, integer *ldvr, integer *ilo, integer *
-       ihi, real *scale, real *abnrm, real *rconde, real *rcondv, real *work,
+       ihi, real *scale, real *abnrm, real *rconde, real *rcondv, real *work, 
         integer *lwork, integer *iwork, integer *info);
 
 /* Subroutine */ int sgegs_(char *jobvsl, char *jobvsr, integer *n, real *a, 
        integer *lda, real *b, integer *ldb, real *alphar, real *alphai, real 
-       *beta, real *vsl, integer *ldvsl, real *vsr, integer *ldvsr, real *     
+       *beta, real *vsl, integer *ldvsl, real *vsr, integer *ldvsr, real *
        work, integer *lwork, integer *info);
 
 /* Subroutine */ int sgegv_(char *jobvl, char *jobvr, integer *n, real *a, 
@@ -1616,6 +2213,11 @@ extern "C" {
 /* Subroutine */ int sgehrd_(integer *n, integer *ilo, integer *ihi, real *a, 
        integer *lda, real *tau, real *work, integer *lwork, integer *info);
 
+/* Subroutine */ int sgejsv_(char *joba, char *jobu, char *jobv, char *jobr, 
+       char *jobt, char *jobp, integer *m, integer *n, real *a, integer *lda, 
+        real *sva, real *u, integer *ldu, real *v, integer *ldv, real *work, 
+       integer *lwork, integer *iwork, integer *info);
+
 /* Subroutine */ int sgelq2_(integer *m, integer *n, real *a, integer *lda, 
        real *tau, real *work, integer *info);
 
@@ -1665,6 +2267,13 @@ extern "C" {
        integer *ldb, real *x, integer *ldx, real *ferr, real *berr, real *
        work, integer *iwork, integer *info);
 
+/* Subroutine */ int sgerfsx_(char *trans, char *equed, integer *n, integer *
+       nrhs, real *a, integer *lda, real *af, integer *ldaf, integer *ipiv, 
+       real *r__, real *c__, real *b, integer *ldb, real *x, integer *ldx, 
+       real *rcond, real *berr, integer *n_err_bnds__, real *err_bnds_norm__, 
+        real *err_bnds_comp__, integer *nparams, real *params, real *work, 
+       integer *iwork, integer *info);
+
 /* Subroutine */ int sgerq2_(integer *m, integer *n, real *a, integer *lda, 
        real *tau, real *work, integer *info);
 
@@ -1675,23 +2284,34 @@ extern "C" {
        integer *ipiv, integer *jpiv, real *scale);
 
 /* Subroutine */ int sgesdd_(char *jobz, integer *m, integer *n, real *a, 
-       integer *lda, real *s, real *u, integer *ldu, real *vt, integer *ldvt,   
-       real *work, integer *lwork, integer *iwork, integer *info);
+       integer *lda, real *s, real *u, integer *ldu, real *vt, integer *ldvt, 
+        real *work, integer *lwork, integer *iwork, integer *info);
 
 /* Subroutine */ int sgesv_(integer *n, integer *nrhs, real *a, integer *lda, 
        integer *ipiv, real *b, integer *ldb, integer *info);
 
 /* Subroutine */ int sgesvd_(char *jobu, char *jobvt, integer *m, integer *n, 
-       real *a, integer *lda, real *s, real *u, integer *ldu, real *vt,        
+       real *a, integer *lda, real *s, real *u, integer *ldu, real *vt, 
        integer *ldvt, real *work, integer *lwork, integer *info);
 
+/* Subroutine */ int sgesvj_(char *joba, char *jobu, char *jobv, integer *m, 
+       integer *n, real *a, integer *lda, real *sva, integer *mv, real *v, 
+       integer *ldv, real *work, integer *lwork, integer *info);
+
 /* Subroutine */ int sgesvx_(char *fact, char *trans, integer *n, integer *
        nrhs, real *a, integer *lda, real *af, integer *ldaf, integer *ipiv, 
        char *equed, real *r__, real *c__, real *b, integer *ldb, real *x, 
        integer *ldx, real *rcond, real *ferr, real *berr, real *work, 
        integer *iwork, integer *info);
 
-/* Subroutine */ int sgetc2_(integer *n, real *a, integer *lda, integer *ipiv,
+/* Subroutine */ int sgesvxx_(char *fact, char *trans, integer *n, integer *
+       nrhs, real *a, integer *lda, real *af, integer *ldaf, integer *ipiv, 
+       char *equed, real *r__, real *c__, real *b, integer *ldb, real *x, 
+       integer *ldx, real *rcond, real *rpvgrw, real *berr, integer *
+       n_err_bnds__, real *err_bnds_norm__, real *err_bnds_comp__, integer *
+       nparams, real *params, real *work, integer *iwork, integer *info);
+
+/* Subroutine */ int sgetc2_(integer *n, real *a, integer *lda, integer *ipiv, 
         integer *jpiv, integer *info);
 
 /* Subroutine */ int sgetf2_(integer *m, integer *n, real *a, integer *lda, 
@@ -1700,7 +2320,7 @@ extern "C" {
 /* Subroutine */ int sgetrf_(integer *m, integer *n, real *a, integer *lda, 
        integer *ipiv, integer *info);
 
-/* Subroutine */ int sgetri_(integer *n, real *a, integer *lda, integer *ipiv,
+/* Subroutine */ int sgetri_(integer *n, real *a, integer *lda, integer *ipiv, 
         real *work, integer *lwork, integer *info);
 
 /* Subroutine */ int sgetrs_(char *trans, integer *n, integer *nrhs, real *a, 
@@ -1717,14 +2337,14 @@ extern "C" {
 /* Subroutine */ int sgges_(char *jobvsl, char *jobvsr, char *sort, L_fp 
        selctg, integer *n, real *a, integer *lda, real *b, integer *ldb, 
        integer *sdim, real *alphar, real *alphai, real *beta, real *vsl, 
-       integer *ldvsl, real *vsr, integer *ldvsr, real *work, integer *lwork,
+       integer *ldvsl, real *vsr, integer *ldvsr, real *work, integer *lwork, 
         logical *bwork, integer *info);
 
 /* Subroutine */ int sggesx_(char *jobvsl, char *jobvsr, char *sort, L_fp 
        selctg, char *sense, integer *n, real *a, integer *lda, real *b, 
        integer *ldb, integer *sdim, real *alphar, real *alphai, real *beta, 
        real *vsl, integer *ldvsl, real *vsr, integer *ldvsr, real *rconde, 
-       real *rcondv, real *work, integer *lwork, integer *iwork, integer *     
+       real *rcondv, real *work, integer *lwork, integer *iwork, integer *
        liwork, logical *bwork, integer *info);
 
 /* Subroutine */ int sggev_(char *jobvl, char *jobvr, integer *n, real *a, 
@@ -1735,8 +2355,8 @@ extern "C" {
 /* Subroutine */ int sggevx_(char *balanc, char *jobvl, char *jobvr, char *
        sense, integer *n, real *a, integer *lda, real *b, integer *ldb, real 
        *alphar, real *alphai, real *beta, real *vl, integer *ldvl, real *vr, 
-       integer *ldvr, integer *ilo, integer *ihi, real *lscale, real *rscale,
-        real *abnrm, real *bbnrm, real *rconde, real *rcondv, real *work,      
+       integer *ldvr, integer *ilo, integer *ihi, real *lscale, real *rscale, 
+        real *abnrm, real *bbnrm, real *rconde, real *rcondv, real *work, 
        integer *lwork, integer *iwork, logical *bwork, integer *info);
 
 /* Subroutine */ int sggglm_(integer *n, integer *m, integer *p, real *a, 
@@ -1760,22 +2380,32 @@ extern "C" {
        work, integer *lwork, integer *info);
 
 /* Subroutine */ int sggsvd_(char *jobu, char *jobv, char *jobq, integer *m, 
-       integer *n, integer *p, integer *k, integer *l, real *a, integer *lda,
+       integer *n, integer *p, integer *k, integer *l, real *a, integer *lda, 
         real *b, integer *ldb, real *alpha, real *beta, real *u, integer *
        ldu, real *v, integer *ldv, real *q, integer *ldq, real *work, 
        integer *iwork, integer *info);
 
 /* Subroutine */ int sggsvp_(char *jobu, char *jobv, char *jobq, integer *m, 
        integer *p, integer *n, real *a, integer *lda, real *b, integer *ldb, 
-       real *tola, real *tolb, integer *k, integer *l, real *u, integer *ldu,
+       real *tola, real *tolb, integer *k, integer *l, real *u, integer *ldu, 
         real *v, integer *ldv, real *q, integer *ldq, integer *iwork, real *
        tau, real *work, integer *info);
 
+/* Subroutine */ int sgsvj0_(char *jobv, integer *m, integer *n, real *a, 
+       integer *lda, real *d__, real *sva, integer *mv, real *v, integer *
+       ldv, real *eps, real *sfmin, real *tol, integer *nsweep, real *work, 
+       integer *lwork, integer *info);
+
+/* Subroutine */ int sgsvj1_(char *jobv, integer *m, integer *n, integer *n1, 
+       real *a, integer *lda, real *d__, real *sva, integer *mv, real *v, 
+       integer *ldv, real *eps, real *sfmin, real *tol, integer *nsweep, 
+       real *work, integer *lwork, integer *info);
+
 /* Subroutine */ int sgtcon_(char *norm, integer *n, real *dl, real *d__, 
        real *du, real *du2, integer *ipiv, real *anorm, real *rcond, real *
        work, integer *iwork, integer *info);
 
-/* Subroutine */ int sgtrfs_(char *trans, integer *n, integer *nrhs, real *dl,
+/* Subroutine */ int sgtrfs_(char *trans, integer *n, integer *nrhs, real *dl, 
         real *d__, real *du, real *dlf, real *df, real *duf, real *du2, 
        integer *ipiv, real *b, integer *ldb, real *x, integer *ldx, real *
        ferr, real *berr, real *work, integer *iwork, integer *info);
@@ -1792,8 +2422,8 @@ extern "C" {
 /* Subroutine */ int sgttrf_(integer *n, real *dl, real *d__, real *du, real *
        du2, integer *ipiv, integer *info);
 
-/* Subroutine */ int sgttrs_(char *trans, integer *n, integer *nrhs, real *dl,
-        real *d__, real *du, real *du2, integer *ipiv, real *b, integer *ldb,
+/* Subroutine */ int sgttrs_(char *trans, integer *n, integer *nrhs, real *dl, 
+        real *d__, real *du, real *du2, integer *ipiv, real *b, integer *ldb, 
         integer *info);
 
 /* Subroutine */ int sgtts2_(integer *itrans, integer *n, integer *nrhs, real 
@@ -1810,9 +2440,91 @@ extern "C" {
        *vl, integer *ldvl, real *vr, integer *ldvr, integer *mm, integer *m, 
        real *work, integer *ifaill, integer *ifailr, integer *info);
 
-/* Subroutine */ int shseqr_(char *job, char *compz, integer *n, integer *ilo,
-        integer *ihi, real *h__, integer *ldh, real *wr, real *wi, real *z__,   
-       integer *ldz, real *work, integer *lwork, integer *info);
+/* Subroutine */ int shseqr_(char *job, char *compz, integer *n, integer *ilo, 
+        integer *ihi, real *h__, integer *ldh, real *wr, real *wi, real *z__, 
+        integer *ldz, real *work, integer *lwork, integer *info);
+
+logical sisnan_(real *sin__);
+
+/* Subroutine */ int sla_gbamv__(integer *trans, integer *m, integer *n, 
+       integer *kl, integer *ku, real *alpha, real *ab, integer *ldab, real *
+       x, integer *incx, real *beta, real *y, integer *incy);
+
+doublereal sla_gbrcond__(char *trans, integer *n, integer *kl, integer *ku, 
+       real *ab, integer *ldab, real *afb, integer *ldafb, integer *ipiv, 
+       integer *cmode, real *c__, integer *info, real *work, integer *iwork, 
+       ftnlen trans_len);
+
+/* Subroutine */ int sla_gbrfsx_extended__(integer *prec_type__, integer *
+       trans_type__, integer *n, integer *kl, integer *ku, integer *nrhs, 
+       real *ab, integer *ldab, real *afb, integer *ldafb, integer *ipiv, 
+       logical *colequ, real *c__, real *b, integer *ldb, real *y, integer *
+       ldy, real *berr_out__, integer *n_norms__, real *errs_n__, real *
+       errs_c__, real *res, real *ayb, real *dy, real *y_tail__, real *rcond,
+        integer *ithresh, real *rthresh, real *dz_ub__, logical *
+       ignore_cwise__, integer *info);
+
+doublereal sla_gbrpvgrw__(integer *n, integer *kl, integer *ku, integer *
+       ncols, real *ab, integer *ldab, real *afb, integer *ldafb);
+
+/* Subroutine */ int sla_geamv__(integer *trans, integer *m, integer *n, real 
+       *alpha, real *a, integer *lda, real *x, integer *incx, real *beta, 
+       real *y, integer *incy);
+
+doublereal sla_gercond__(char *trans, integer *n, real *a, integer *lda, real 
+       *af, integer *ldaf, integer *ipiv, integer *cmode, real *c__, integer 
+       *info, real *work, integer *iwork, ftnlen trans_len);
+
+/* Subroutine */ int sla_gerfsx_extended__(integer *prec_type__, integer *
+       trans_type__, integer *n, integer *nrhs, real *a, integer *lda, real *
+       af, integer *ldaf, integer *ipiv, logical *colequ, real *c__, real *b,
+        integer *ldb, real *y, integer *ldy, real *berr_out__, integer *
+       n_norms__, real *errs_n__, real *errs_c__, real *res, real *ayb, real 
+       *dy, real *y_tail__, real *rcond, integer *ithresh, real *rthresh, 
+       real *dz_ub__, logical *ignore_cwise__, integer *info);
+
+/* Subroutine */ int sla_lin_berr__(integer *n, integer *nz, integer *nrhs, 
+       real *res, real *ayb, real *berr);
+
+doublereal sla_porcond__(char *uplo, integer *n, real *a, integer *lda, real *
+       af, integer *ldaf, integer *cmode, real *c__, integer *info, real *
+       work, integer *iwork, ftnlen uplo_len);
+
+/* Subroutine */ int sla_porfsx_extended__(integer *prec_type__, char *uplo, 
+       integer *n, integer *nrhs, real *a, integer *lda, real *af, integer *
+       ldaf, logical *colequ, real *c__, real *b, integer *ldb, real *y, 
+       integer *ldy, real *berr_out__, integer *n_norms__, real *errs_n__, 
+       real *errs_c__, real *res, real *ayb, real *dy, real *y_tail__, real *
+       rcond, integer *ithresh, real *rthresh, real *dz_ub__, logical *
+       ignore_cwise__, integer *info, ftnlen uplo_len);
+
+doublereal sla_porpvgrw__(char *uplo, integer *ncols, real *a, integer *lda, 
+       real *af, integer *ldaf, real *work, ftnlen uplo_len);
+
+doublereal sla_rpvgrw__(integer *n, integer *ncols, real *a, integer *lda, 
+       real *af, integer *ldaf);
+
+/* Subroutine */ int sla_syamv__(integer *uplo, integer *n, real *alpha, real 
+       *a, integer *lda, real *x, integer *incx, real *beta, real *y, 
+       integer *incy);
+
+doublereal sla_syrcond__(char *uplo, integer *n, real *a, integer *lda, real *
+       af, integer *ldaf, integer *ipiv, integer *cmode, real *c__, integer *
+       info, real *work, integer *iwork, ftnlen uplo_len);
+
+/* Subroutine */ int sla_syrfsx_extended__(integer *prec_type__, char *uplo, 
+       integer *n, integer *nrhs, real *a, integer *lda, real *af, integer *
+       ldaf, integer *ipiv, logical *colequ, real *c__, real *b, integer *
+       ldb, real *y, integer *ldy, real *berr_out__, integer *n_norms__, 
+       real *errs_n__, real *errs_c__, real *res, real *ayb, real *dy, real *
+       y_tail__, real *rcond, integer *ithresh, real *rthresh, real *dz_ub__,
+        logical *ignore_cwise__, integer *info, ftnlen uplo_len);
+
+doublereal sla_syrpvgrw__(char *uplo, integer *n, integer *info, real *a, 
+       integer *lda, real *af, integer *ldaf, integer *ipiv, real *work, 
+       ftnlen uplo_len);
+
+/* Subroutine */ int sla_wwaddw__(integer *n, real *x, real *y, real *w);
 
 /* Subroutine */ int slabad_(real *small, real *large);
 
@@ -1880,7 +2592,7 @@ extern "C" {
        givnum, integer *indxp, integer *indx, integer *info);
 
 /* Subroutine */ int slaed9_(integer *k, integer *kstart, integer *kstop, 
-       integer *n, real *d__, real *q, integer *ldq, real *rho, real *dlamda,
+       integer *n, real *d__, real *q, integer *ldq, real *rho, real *dlamda, 
         real *w, real *s, integer *lds, integer *info);
 
 /* Subroutine */ int slaeda_(integer *n, integer *tlvls, integer *curlvl, 
@@ -1939,6 +2651,8 @@ extern "C" {
 /* Subroutine */ int slaic1_(integer *job, integer *j, real *x, real *sest, 
        real *w, real *gamma, real *sestpr, real *s, real *c__);
 
+logical slaisnan_(real *sin1, real *sin2);
+
 /* Subroutine */ int slaln2_(logical *ltrans, integer *na, integer *nw, real *
        smin, real *ca, real *a, integer *lda, real *d1, real *d2, real *b, 
        integer *ldb, real *wr, real *wi, real *x, integer *ldx, real *scale, 
@@ -1965,16 +2679,55 @@ extern "C" {
 /* Subroutine */ int slamrg_(integer *n1, integer *n2, real *a, integer *
        strd1, integer *strd2, integer *index);
 
+integer slaneg_(integer *n, real *d__, real *lld, real *sigma, real *pivmin, 
+       integer *r__);
+
+doublereal slangb_(char *norm, integer *n, integer *kl, integer *ku, real *ab, 
+        integer *ldab, real *work);
+
+doublereal slange_(char *norm, integer *m, integer *n, real *a, integer *lda, 
+       real *work);
+
+doublereal slangt_(char *norm, integer *n, real *dl, real *d__, real *du);
+
+doublereal slanhs_(char *norm, integer *n, real *a, integer *lda, real *work);
+
+doublereal slansb_(char *norm, char *uplo, integer *n, integer *k, real *ab, 
+       integer *ldab, real *work);
+
+doublereal slansf_(char *norm, char *transr, char *uplo, integer *n, real *a, 
+       real *work);
+
+doublereal slansp_(char *norm, char *uplo, integer *n, real *ap, real *work);
+
+doublereal slanst_(char *norm, integer *n, real *d__, real *e);
+
+doublereal slansy_(char *norm, char *uplo, integer *n, real *a, integer *lda, 
+       real *work);
+
+doublereal slantb_(char *norm, char *uplo, char *diag, integer *n, integer *k, 
+        real *ab, integer *ldab, real *work);
+
+doublereal slantp_(char *norm, char *uplo, char *diag, integer *n, real *ap, 
+       real *work);
+
+doublereal slantr_(char *norm, char *uplo, char *diag, integer *m, integer *n, 
+        real *a, integer *lda, real *work);
+
 /* Subroutine */ int slanv2_(real *a, real *b, real *c__, real *d__, real *
        rt1r, real *rt1i, real *rt2r, real *rt2i, real *cs, real *sn);
 
 /* Subroutine */ int slapll_(integer *n, real *x, integer *incx, real *y, 
        integer *incy, real *ssmin);
 
-/* Subroutine */ int slapmt_(logical *forwrd, integer *m, integer *n, real *x,
+/* Subroutine */ int slapmt_(logical *forwrd, integer *m, integer *n, real *x, 
         integer *ldx, integer *k);
 
-/* Subroutine */ int slaqgb_(integer *m, integer *n, integer *kl, integer *ku,
+doublereal slapy2_(real *x, real *y);
+
+doublereal slapy3_(real *x, real *y, real *z__);
+
+/* Subroutine */ int slaqgb_(integer *m, integer *n, integer *kl, integer *ku, 
         real *ab, integer *ldab, real *r__, real *c__, real *rowcnd, real *
        colcnd, real *amax, char *equed);
 
@@ -1982,7 +2735,7 @@ extern "C" {
        real *r__, real *c__, real *rowcnd, real *colcnd, real *amax, char *
        equed);
 
-/* Subroutine */ int slaqp2_(integer *m, integer *n, integer *offset, real *a,
+/* Subroutine */ int slaqp2_(integer *m, integer *n, integer *offset, real *a, 
         integer *lda, integer *jpvt, real *tau, real *vn1, real *vn2, real *
        work);
 
@@ -1992,7 +2745,7 @@ extern "C" {
 
 /* Subroutine */ int slaqr0_(logical *wantt, logical *wantz, integer *n, 
        integer *ilo, integer *ihi, real *h__, integer *ldh, real *wr, real *
-       wi, integer *iloz, integer *ihiz, real *z__, integer *ldz, real *work,
+       wi, integer *iloz, integer *ihiz, real *z__, integer *ldz, real *work, 
         integer *lwork, integer *info);
 
 /* Subroutine */ int slaqr1_(integer *n, real *h__, integer *ldh, real *sr1, 
@@ -2014,7 +2767,7 @@ extern "C" {
 
 /* Subroutine */ int slaqr4_(logical *wantt, logical *wantz, integer *n, 
        integer *ilo, integer *ihi, real *h__, integer *ldh, real *wr, real *
-       wi, integer *iloz, integer *ihiz, real *z__, integer *ldz, real *work,
+       wi, integer *iloz, integer *ihiz, real *z__, integer *ldz, real *work, 
         integer *lwork, integer *info);
 
 /* Subroutine */ int slaqr5_(logical *wantt, logical *wantz, integer *kacc22, 
@@ -2025,7 +2778,7 @@ extern "C" {
        ldwh);
 
 /* Subroutine */ int slaqsb_(char *uplo, integer *n, integer *kd, real *ab, 
-       integer *ldab, real *s, real *scond, real *amax, char *equed    );
+       integer *ldab, real *s, real *scond, real *amax, char *equed);
 
 /* Subroutine */ int slaqsp_(char *uplo, integer *n, real *ap, real *s, real *
        scond, real *amax, char *equed);
@@ -2047,18 +2800,21 @@ extern "C" {
        *incx, real *c__, real *s, integer *incc);
 
 /* Subroutine */ int slarf_(char *side, integer *m, integer *n, real *v, 
-       integer *incv, real *tau, real *c__, integer *ldc, real *work   );
+       integer *incv, real *tau, real *c__, integer *ldc, real *work);
 
 /* Subroutine */ int slarfb_(char *side, char *trans, char *direct, char *
        storev, integer *m, integer *n, integer *k, real *v, integer *ldv, 
        real *t, integer *ldt, real *c__, integer *ldc, real *work, integer *
-       ldwork          );
+       ldwork);
 
 /* Subroutine */ int slarfg_(integer *n, real *alpha, real *x, integer *incx, 
        real *tau);
 
+/* Subroutine */ int slarfp_(integer *n, real *alpha, real *x, integer *incx, 
+       real *tau);
+
 /* Subroutine */ int slarft_(char *direct, char *storev, integer *n, integer *
-       k, real *v, integer *ldv, real *tau, real *t, integer *ldt      );
+       k, real *v, integer *ldv, real *tau, real *t, integer *ldt);
 
 /* Subroutine */ int slarfx_(char *side, integer *m, integer *n, real *v, 
        real *tau, real *c__, integer *ldc, real *work);
@@ -2098,7 +2854,7 @@ extern "C" {
        real *spdiam, real *clgapl, real *clgapr, real *pivmin, real *sigma, 
        real *dplus, real *lplus, real *work, integer *info);
 
-/* Subroutine */ int slarrj_(integer *n, real *d__, real *e2, integer *ifirst,
+/* Subroutine */ int slarrj_(integer *n, real *d__, real *e2, integer *ifirst, 
         integer *ilast, real *rtol, integer *offset, real *w, real *werr, 
        real *work, integer *iwork, real *pivmin, real *spdiam, integer *info);
 
@@ -2115,6 +2871,9 @@ extern "C" {
        integer *ldz, integer *isuppz, real *work, integer *iwork, integer *
        info);
 
+/* Subroutine */ int slarscl2_(integer *m, integer *n, real *d__, real *x, 
+       integer *ldx);
+
 /* Subroutine */ int slartg_(real *f, real *g, real *cs, real *sn, real *r__);
 
 /* Subroutine */ int slartv_(integer *n, real *x, integer *incx, real *y, 
@@ -2129,10 +2888,10 @@ extern "C" {
 /* Subroutine */ int slarzb_(char *side, char *trans, char *direct, char *
        storev, integer *m, integer *n, integer *k, integer *l, real *v, 
        integer *ldv, real *t, integer *ldt, real *c__, integer *ldc, real *
-       work, integer *ldwork   );
+       work, integer *ldwork);
 
 /* Subroutine */ int slarzt_(char *direct, char *storev, integer *n, integer *
-       k, real *v, integer *ldv, real *tau, real *t, integer *ldt      );
+       k, real *v, integer *ldv, real *tau, real *t, integer *ldt);
 
 /* Subroutine */ int slas2_(real *f, real *g, real *h__, real *ssmin, real *
        ssmax);
@@ -2141,6 +2900,9 @@ extern "C" {
        cfrom, real *cto, integer *m, integer *n, real *a, integer *lda, 
        integer *info);
 
+/* Subroutine */ int slascl2_(integer *m, integer *n, real *d__, real *x, 
+       integer *ldx);
+
 /* Subroutine */ int slasd0_(integer *n, integer *sqre, real *d__, real *e, 
        real *u, integer *ldu, real *vt, integer *ldvt, integer *smlsiz, 
        integer *iwork, real *work, integer *info);
@@ -2153,7 +2915,7 @@ extern "C" {
 /* Subroutine */ int slasd2_(integer *nl, integer *nr, integer *sqre, integer 
        *k, real *d__, real *z__, real *alpha, real *beta, real *u, integer *
        ldu, real *vt, integer *ldvt, real *dsigma, real *u2, integer *ldu2, 
-       real *vt2, integer *ldvt2, integer *idxp, integer *idx, integer *idxc,
+       real *vt2, integer *ldvt2, integer *idxp, integer *idx, integer *idxc, 
         integer *idxq, integer *coltyp, integer *info);
 
 /* Subroutine */ int slasd3_(integer *nl, integer *nr, integer *sqre, integer 
@@ -2169,7 +2931,7 @@ extern "C" {
        real *rho, real *dsigma, real *work);
 
 /* Subroutine */ int slasd6_(integer *icompq, integer *nl, integer *nr, 
-       integer *sqre, real *d__, real *vf, real *vl, real *alpha, real *beta,
+       integer *sqre, real *d__, real *vf, real *vl, real *alpha, real *beta, 
         integer *idxq, integer *perm, integer *givptr, integer *givcol, 
        integer *ldgcol, real *givnum, integer *ldgnum, real *poles, real *
        difl, real *difr, real *z__, integer *k, real *c__, real *s, real *
@@ -2177,7 +2939,7 @@ extern "C" {
 
 /* Subroutine */ int slasd7_(integer *icompq, integer *nl, integer *nr, 
        integer *sqre, integer *k, real *d__, real *z__, real *zw, real *vf, 
-       real *vfw, real *vl, real *vlw, real *alpha, real *beta, real *dsigma,
+       real *vfw, real *vl, real *vlw, real *alpha, real *beta, real *dsigma, 
         integer *idx, integer *idxp, integer *idxq, integer *perm, integer *
        givptr, integer *givcol, integer *ldgcol, real *givnum, integer *
        ldgnum, real *c__, real *s, integer *info);
@@ -2186,14 +2948,10 @@ extern "C" {
        z__, real *vf, real *vl, real *difl, real *difr, integer *lddifr, 
        real *dsigma, real *work, integer *info);
 
-/* Subroutine */ int slasd9_(integer *icompq, integer *ldu, integer *k, real *
-       d__, real *z__, real *vf, real *vl, real *difl, real *difr, real *
-       dsigma, real *work, integer *info);
-
 /* Subroutine */ int slasda_(integer *icompq, integer *smlsiz, integer *n, 
        integer *sqre, real *d__, real *e, real *u, integer *ldu, real *vt, 
        integer *k, real *difl, real *difr, real *z__, real *poles, integer *
-       givptr, integer *givcol, integer *ldgcol, integer *perm, real *givnum,
+       givptr, integer *givcol, integer *ldgcol, integer *perm, real *givnum, 
         real *c__, real *s, real *work, integer *iwork, integer *info);
 
 /* Subroutine */ int slasdq_(char *uplo, integer *sqre, integer *n, integer *
@@ -2212,24 +2970,26 @@ extern "C" {
 
 /* Subroutine */ int slasq2_(integer *n, real *z__, integer *info);
 
-/* Subroutine */ int slasq3_(integer *i0, integer *n0, real *z__, integer *pp,
+/* Subroutine */ int slasq3_(integer *i0, integer *n0, real *z__, integer *pp, 
         real *dmin__, real *sigma, real *desig, real *qmax, integer *nfail, 
-       integer *iter, integer *ndiv, logical *ieee);
+       integer *iter, integer *ndiv, logical *ieee, integer *ttype, real *
+       dmin1, real *dmin2, real *dn, real *dn1, real *dn2, real *g, real *
+       tau);
 
-/* Subroutine */ int slasq4_(integer *i0, integer *n0, real *z__, integer *pp,
+/* Subroutine */ int slasq4_(integer *i0, integer *n0, real *z__, integer *pp, 
         integer *n0in, real *dmin__, real *dmin1, real *dmin2, real *dn, 
-       real *dn1, real *dn2, real *tau, integer *ttype);
+       real *dn1, real *dn2, real *tau, integer *ttype, real *g);
 
-/* Subroutine */ int slasq5_(integer *i0, integer *n0, real *z__, integer *pp,
+/* Subroutine */ int slasq5_(integer *i0, integer *n0, real *z__, integer *pp, 
         real *tau, real *dmin__, real *dmin1, real *dmin2, real *dn, real *
        dnm1, real *dnm2, logical *ieee);
 
-/* Subroutine */ int slasq6_(integer *i0, integer *n0, real *z__, integer *pp,
+/* Subroutine */ int slasq6_(integer *i0, integer *n0, real *z__, integer *pp, 
         real *dmin__, real *dmin1, real *dmin2, real *dn, real *dnm1, real *
        dnm2);
 
-/* Subroutine */ int slasr_(char *side, char *pivot, char *direct, integer *m,
-        integer *n, real *c__, real *s, real *a, integer *lda          );
+/* Subroutine */ int slasr_(char *side, char *pivot, char *direct, integer *m, 
+        integer *n, real *c__, real *s, real *a, integer *lda);
 
 /* Subroutine */ int slasrt_(char *id, integer *n, real *d__, integer *info);
 
@@ -2247,7 +3007,7 @@ extern "C" {
        ldtr, real *b, integer *ldb, real *scale, real *x, integer *ldx, real 
        *xnorm, integer *info);
 
-/* Subroutine */ int slasyf_(char *uplo, integer *n, integer *nb, integer *kb,
+/* Subroutine */ int slasyf_(char *uplo, integer *n, integer *nb, integer *kb, 
         real *a, integer *lda, integer *ipiv, real *w, integer *ldw, integer 
        *info);
 
@@ -2264,7 +3024,7 @@ extern "C" {
        integer *info);
 
 /* Subroutine */ int slatrd_(char *uplo, integer *n, integer *nb, real *a, 
-       integer *lda, real *e, real *tau, real *w, integer *ldw         );
+       integer *lda, real *e, real *tau, real *w, integer *ldw);
 
 /* Subroutine */ int slatrs_(char *uplo, char *trans, char *diag, char *
        normin, integer *n, real *a, integer *lda, real *x, real *scale, real 
@@ -2283,15 +3043,6 @@ extern "C" {
 /* Subroutine */ int slauum_(char *uplo, integer *n, real *a, integer *lda, 
        integer *info);
 
-/* Subroutine */ int slazq3_(integer *i0, integer *n0, real *z__, integer *pp,
-        real *dmin__, real *sigma, real *desig, real *qmax, integer *nfail, 
-       integer *iter, integer *ndiv, logical *ieee, integer *ttype, real *
-       dmin1, real *dmin2, real *dn, real *dn1, real *dn2, real *tau);
-
-/* Subroutine */ int slazq4_(integer *i0, integer *n0, real *z__, integer *pp,
-        integer *n0in, real *dmin__, real *dmin1, real *dmin2, real *dn, 
-       real *dn1, real *dn2, real *tau, integer *ttype, real *g);
-
 /* Subroutine */ int sopgtr_(char *uplo, integer *n, real *ap, real *tau, 
        real *q, integer *ldq, real *work, integer *info);
 
@@ -2334,39 +3085,39 @@ extern "C" {
        real *tau, real *work, integer *lwork, integer *info);
 
 /* Subroutine */ int sorm2l_(char *side, char *trans, integer *m, integer *n, 
-       integer *k, real *a, integer *lda, real *tau, real *c__, integer *ldc,
+       integer *k, real *a, integer *lda, real *tau, real *c__, integer *ldc, 
         real *work, integer *info);
 
 /* Subroutine */ int sorm2r_(char *side, char *trans, integer *m, integer *n, 
-       integer *k, real *a, integer *lda, real *tau, real *c__, integer *ldc,
+       integer *k, real *a, integer *lda, real *tau, real *c__, integer *ldc, 
         real *work, integer *info);
 
 /* Subroutine */ int sormbr_(char *vect, char *side, char *trans, integer *m, 
-       integer *n, integer *k, real *a, integer *lda, real *tau, real *c__,    
+       integer *n, integer *k, real *a, integer *lda, real *tau, real *c__, 
        integer *ldc, real *work, integer *lwork, integer *info);
 
 /* Subroutine */ int sormhr_(char *side, char *trans, integer *m, integer *n, 
-       integer *ilo, integer *ihi, real *a, integer *lda, real *tau, real *    
+       integer *ilo, integer *ihi, real *a, integer *lda, real *tau, real *
        c__, integer *ldc, real *work, integer *lwork, integer *info);
 
 /* Subroutine */ int sorml2_(char *side, char *trans, integer *m, integer *n, 
-       integer *k, real *a, integer *lda, real *tau, real *c__, integer *ldc,
+       integer *k, real *a, integer *lda, real *tau, real *c__, integer *ldc, 
         real *work, integer *info);
 
 /* Subroutine */ int sormlq_(char *side, char *trans, integer *m, integer *n, 
-       integer *k, real *a, integer *lda, real *tau, real *c__, integer *ldc,   
-       real *work, integer *lwork, integer *info);
+       integer *k, real *a, integer *lda, real *tau, real *c__, integer *ldc, 
+        real *work, integer *lwork, integer *info);
 
 /* Subroutine */ int sormql_(char *side, char *trans, integer *m, integer *n, 
-       integer *k, real *a, integer *lda, real *tau, real *c__, integer *ldc,   
-       real *work, integer *lwork, integer *info);
+       integer *k, real *a, integer *lda, real *tau, real *c__, integer *ldc, 
+        real *work, integer *lwork, integer *info);
 
 /* Subroutine */ int sormqr_(char *side, char *trans, integer *m, integer *n, 
-       integer *k, real *a, integer *lda, real *tau, real *c__, integer *ldc,   
-       real *work, integer *lwork, integer *info);
+       integer *k, real *a, integer *lda, real *tau, real *c__, integer *ldc, 
+        real *work, integer *lwork, integer *info);
 
 /* Subroutine */ int sormr2_(char *side, char *trans, integer *m, integer *n, 
-       integer *k, real *a, integer *lda, real *tau, real *c__, integer *ldc,
+       integer *k, real *a, integer *lda, real *tau, real *c__, integer *ldc, 
         real *work, integer *info);
 
 /* Subroutine */ int sormr3_(char *side, char *trans, integer *m, integer *n, 
@@ -2374,16 +3125,16 @@ extern "C" {
        integer *ldc, real *work, integer *info);
 
 /* Subroutine */ int sormrq_(char *side, char *trans, integer *m, integer *n, 
-       integer *k, real *a, integer *lda, real *tau, real *c__, integer *ldc,   
-       real *work, integer *lwork, integer *info);
+       integer *k, real *a, integer *lda, real *tau, real *c__, integer *ldc, 
+        real *work, integer *lwork, integer *info);
 
 /* Subroutine */ int sormrz_(char *side, char *trans, integer *m, integer *n, 
-       integer *k, integer *l, real *a, integer *lda, real *tau, real *c__,    
+       integer *k, integer *l, real *a, integer *lda, real *tau, real *c__, 
        integer *ldc, real *work, integer *lwork, integer *info);
 
 /* Subroutine */ int sormtr_(char *side, char *uplo, char *trans, integer *m, 
-       integer *n, real *a, integer *lda, real *tau, real *c__, integer *ldc,   
-       real *work, integer *lwork, integer *info);
+       integer *n, real *a, integer *lda, real *tau, real *c__, integer *ldc, 
+        real *work, integer *lwork, integer *info);
 
 /* Subroutine */ int spbcon_(char *uplo, integer *n, integer *kd, real *ab, 
        integer *ldab, real *anorm, real *rcond, real *work, integer *iwork, 
@@ -2418,17 +3169,36 @@ extern "C" {
 /* Subroutine */ int spbtrs_(char *uplo, integer *n, integer *kd, integer *
        nrhs, real *ab, integer *ldab, real *b, integer *ldb, integer *info);
 
+/* Subroutine */ int spftrf_(char *transr, char *uplo, integer *n, real *a, 
+       integer *info);
+
+/* Subroutine */ int spftri_(char *transr, char *uplo, integer *n, real *a, 
+       integer *info);
+
+/* Subroutine */ int spftrs_(char *transr, char *uplo, integer *n, integer *
+       nrhs, real *a, real *b, integer *ldb, integer *info);
+
 /* Subroutine */ int spocon_(char *uplo, integer *n, real *a, integer *lda, 
        real *anorm, real *rcond, real *work, integer *iwork, integer *info);
 
 /* Subroutine */ int spoequ_(integer *n, real *a, integer *lda, real *s, real 
        *scond, real *amax, integer *info);
 
+/* Subroutine */ int spoequb_(integer *n, real *a, integer *lda, real *s, 
+       real *scond, real *amax, integer *info);
+
 /* Subroutine */ int sporfs_(char *uplo, integer *n, integer *nrhs, real *a, 
-       integer *lda, real *af, integer *ldaf, real *b, integer *ldb, real *x,
+       integer *lda, real *af, integer *ldaf, real *b, integer *ldb, real *x, 
         integer *ldx, real *ferr, real *berr, real *work, integer *iwork, 
        integer *info);
 
+/* Subroutine */ int sporfsx_(char *uplo, char *equed, integer *n, integer *
+       nrhs, real *a, integer *lda, real *af, integer *ldaf, real *s, real *
+       b, integer *ldb, real *x, integer *ldx, real *rcond, real *berr, 
+       integer *n_err_bnds__, real *err_bnds_norm__, real *err_bnds_comp__, 
+       integer *nparams, real *params, real *work, integer *iwork, integer *
+       info);
+
 /* Subroutine */ int sposv_(char *uplo, integer *n, integer *nrhs, real *a, 
        integer *lda, real *b, integer *ldb, integer *info);
 
@@ -2437,6 +3207,13 @@ extern "C" {
        real *s, real *b, integer *ldb, real *x, integer *ldx, real *rcond, 
        real *ferr, real *berr, real *work, integer *iwork, integer *info);
 
+/* Subroutine */ int sposvxx_(char *fact, char *uplo, integer *n, integer *
+       nrhs, real *a, integer *lda, real *af, integer *ldaf, char *equed, 
+       real *s, real *b, integer *ldb, real *x, integer *ldx, real *rcond, 
+       real *rpvgrw, real *berr, integer *n_err_bnds__, real *
+       err_bnds_norm__, real *err_bnds_comp__, integer *nparams, real *
+       params, real *work, integer *iwork, integer *info);
+
 /* Subroutine */ int spotf2_(char *uplo, integer *n, real *a, integer *lda, 
        integer *info);
 
@@ -2474,6 +3251,12 @@ extern "C" {
 /* Subroutine */ int spptrs_(char *uplo, integer *n, integer *nrhs, real *ap, 
        real *b, integer *ldb, integer *info);
 
+/* Subroutine */ int spstf2_(char *uplo, integer *n, real *a, integer *lda, 
+       integer *piv, integer *rank, real *tol, real *work, integer *info);
+
+/* Subroutine */ int spstrf_(char *uplo, integer *n, real *a, integer *lda, 
+       integer *piv, integer *rank, real *tol, real *work, integer *info);
+
 /* Subroutine */ int sptcon_(integer *n, real *d__, real *e, real *anorm, 
        real *rcond, real *work, integer *info);
 
@@ -2487,7 +3270,7 @@ extern "C" {
 /* Subroutine */ int sptsv_(integer *n, integer *nrhs, real *d__, real *e, 
        real *b, integer *ldb, integer *info);
 
-/* Subroutine */ int sptsvx_(char *fact, integer *n, integer *nrhs, real *d__,
+/* Subroutine */ int sptsvx_(char *fact, integer *n, integer *nrhs, real *d__, 
         real *e, real *df, real *ef, real *b, integer *ldb, real *x, integer 
        *ldx, real *rcond, real *ferr, real *berr, real *work, integer *info);
 
@@ -2502,15 +3285,15 @@ extern "C" {
 /* Subroutine */ int srscl_(integer *n, real *sa, real *sx, integer *incx);
 
 /* Subroutine */ int ssbev_(char *jobz, char *uplo, integer *n, integer *kd, 
-       real *ab, integer *ldab, real *w, real *z__, integer *ldz, real *work,
+       real *ab, integer *ldab, real *w, real *z__, integer *ldz, real *work, 
         integer *info);
 
 /* Subroutine */ int ssbevd_(char *jobz, char *uplo, integer *n, integer *kd, 
-       real *ab, integer *ldab, real *w, real *z__, integer *ldz, real *work,
+       real *ab, integer *ldab, real *w, real *z__, integer *ldz, real *work, 
         integer *lwork, integer *iwork, integer *liwork, integer *info);
 
 /* Subroutine */ int ssbevx_(char *jobz, char *range, char *uplo, integer *n, 
-       integer *kd, real *ab, integer *ldab, real *q, integer *ldq, real *vl,
+       integer *kd, real *ab, integer *ldab, real *q, integer *ldq, real *vl, 
         real *vu, integer *il, integer *iu, real *abstol, integer *m, real *
        w, real *z__, integer *ldz, real *work, integer *iwork, integer *
        ifail, integer *info);
@@ -2538,6 +3321,10 @@ extern "C" {
        real *ab, integer *ldab, real *d__, real *e, real *q, integer *ldq, 
        real *work, integer *info);
 
+/* Subroutine */ int ssfrk_(char *transr, char *uplo, char *trans, integer *n, 
+        integer *k, real *alpha, real *a, integer *lda, real *beta, real *
+       c__);
+
 /* Subroutine */ int sspcon_(char *uplo, integer *n, real *ap, integer *ipiv, 
        real *anorm, real *rcond, real *work, integer *iwork, integer *info);
 
@@ -2553,7 +3340,7 @@ extern "C" {
        integer *m, real *w, real *z__, integer *ldz, real *work, integer *
        iwork, integer *ifail, integer *info);
 
-/* Subroutine */ int sspgst_(integer *itype, char *uplo, integer *n, real *ap,
+/* Subroutine */ int sspgst_(integer *itype, char *uplo, integer *n, real *ap, 
         real *bp, integer *info);
 
 /* Subroutine */ int sspgv_(integer *itype, char *jobz, char *uplo, integer *
@@ -2565,7 +3352,7 @@ extern "C" {
        integer *lwork, integer *iwork, integer *liwork, integer *info);
 
 /* Subroutine */ int sspgvx_(integer *itype, char *jobz, char *range, char *
-       uplo, integer *n, real *ap, real *bp, real *vl, real *vu, integer *il,
+       uplo, integer *n, real *ap, real *bp, real *vl, real *vu, integer *il, 
         integer *iu, real *abstol, integer *m, real *w, real *z__, integer *
        ldz, real *work, integer *iwork, integer *ifail, integer *info);
 
@@ -2644,6 +3431,9 @@ extern "C" {
        integer *ipiv, real *anorm, real *rcond, real *work, integer *iwork, 
        integer *info);
 
+/* Subroutine */ int ssyequb_(char *uplo, integer *n, real *a, integer *lda, 
+       real *s, real *scond, real *amax, real *work, integer *info);
+
 /* Subroutine */ int ssyev_(char *jobz, char *uplo, integer *n, real *a, 
        integer *lda, real *w, real *work, integer *lwork, integer *info);
 
@@ -2679,7 +3469,7 @@ extern "C" {
 /* Subroutine */ int ssygvx_(integer *itype, char *jobz, char *range, char *
        uplo, integer *n, real *a, integer *lda, real *b, integer *ldb, real *
        vl, real *vu, integer *il, integer *iu, real *abstol, integer *m, 
-       real *w, real *z__, integer *ldz, real *work, integer *lwork, integer   
+       real *w, real *z__, integer *ldz, real *work, integer *lwork, integer 
        *iwork, integer *ifail, integer *info);
 
 /* Subroutine */ int ssyrfs_(char *uplo, integer *n, integer *nrhs, real *a, 
@@ -2687,16 +3477,30 @@ extern "C" {
        integer *ldb, real *x, integer *ldx, real *ferr, real *berr, real *
        work, integer *iwork, integer *info);
 
+/* Subroutine */ int ssyrfsx_(char *uplo, char *equed, integer *n, integer *
+       nrhs, real *a, integer *lda, real *af, integer *ldaf, integer *ipiv, 
+       real *s, real *b, integer *ldb, real *x, integer *ldx, real *rcond, 
+       real *berr, integer *n_err_bnds__, real *err_bnds_norm__, real *
+       err_bnds_comp__, integer *nparams, real *params, real *work, integer *
+       iwork, integer *info);
+
 /* Subroutine */ int ssysv_(char *uplo, integer *n, integer *nrhs, real *a, 
        integer *lda, integer *ipiv, real *b, integer *ldb, real *work, 
        integer *lwork, integer *info);
 
 /* Subroutine */ int ssysvx_(char *fact, char *uplo, integer *n, integer *
        nrhs, real *a, integer *lda, real *af, integer *ldaf, integer *ipiv, 
-       real *b, integer *ldb, real *x, integer *ldx, real *rcond, real *ferr,
+       real *b, integer *ldb, real *x, integer *ldx, real *rcond, real *ferr, 
         real *berr, real *work, integer *lwork, integer *iwork, integer *
        info);
 
+/* Subroutine */ int ssysvxx_(char *fact, char *uplo, integer *n, integer *
+       nrhs, real *a, integer *lda, real *af, integer *ldaf, integer *ipiv, 
+       char *equed, real *s, real *b, integer *ldb, real *x, integer *ldx, 
+       real *rcond, real *rpvgrw, real *berr, integer *n_err_bnds__, real *
+       err_bnds_norm__, real *err_bnds_comp__, integer *nparams, real *
+       params, real *work, integer *iwork, integer *info);
+
 /* Subroutine */ int ssytd2_(char *uplo, integer *n, real *a, integer *lda, 
        real *d__, real *e, real *tau, integer *info);
 
@@ -2707,8 +3511,8 @@ extern "C" {
        real *d__, real *e, real *tau, real *work, integer *lwork, integer *
        info);
 
-/* Subroutine */ int ssytrf_(char *uplo, integer *n, real *a, integer *lda,    
-    integer *ipiv, real *work, integer *lwork, integer *info);
+/* Subroutine */ int ssytrf_(char *uplo, integer *n, real *a, integer *lda, 
+       integer *ipiv, real *work, integer *lwork, integer *info);
 
 /* Subroutine */ int ssytri_(char *uplo, integer *n, real *a, integer *lda, 
        integer *ipiv, real *work, integer *info);
@@ -2729,6 +3533,19 @@ extern "C" {
        integer *kd, integer *nrhs, real *ab, integer *ldab, real *b, integer 
        *ldb, integer *info);
 
+/* Subroutine */ int stfsm_(char *transr, char *side, char *uplo, char *trans, 
+        char *diag, integer *m, integer *n, real *alpha, real *a, real *b, 
+       integer *ldb);
+
+/* Subroutine */ int stftri_(char *transr, char *uplo, char *diag, integer *n, 
+        real *a, integer *info);
+
+/* Subroutine */ int stfttp_(char *transr, char *uplo, integer *n, real *arf, 
+       real *ap, integer *info);
+
+/* Subroutine */ int stfttr_(char *transr, char *uplo, integer *n, real *arf, 
+       real *a, integer *lda, integer *info);
+
 /* Subroutine */ int stgevc_(char *side, char *howmny, logical *select, 
        integer *n, real *s, integer *lds, real *p, integer *ldp, real *vl, 
        integer *ldvl, real *vr, integer *ldvr, integer *mm, integer *m, real 
@@ -2752,7 +3569,7 @@ extern "C" {
        info);
 
 /* Subroutine */ int stgsja_(char *jobu, char *jobv, char *jobq, integer *m, 
-       integer *p, integer *n, integer *k, integer *l, real *a, integer *lda,
+       integer *p, integer *n, integer *k, integer *l, real *a, integer *lda, 
         real *b, integer *ldb, real *tola, real *tolb, real *alpha, real *
        beta, real *u, integer *ldu, real *v, integer *ldv, real *q, integer *
        ldq, real *work, integer *ncycle, integer *info);
@@ -2779,7 +3596,7 @@ extern "C" {
        real *ap, real *rcond, real *work, integer *iwork, integer *info);
 
 /* Subroutine */ int stprfs_(char *uplo, char *trans, char *diag, integer *n, 
-       integer *nrhs, real *ap, real *b, integer *ldb, real *x, integer *ldx,
+       integer *nrhs, real *ap, real *b, integer *ldb, real *x, integer *ldx, 
         real *ferr, real *berr, real *work, integer *iwork, integer *info);
 
 /* Subroutine */ int stptri_(char *uplo, char *diag, integer *n, real *ap, 
@@ -2788,6 +3605,12 @@ extern "C" {
 /* Subroutine */ int stptrs_(char *uplo, char *trans, char *diag, integer *n, 
        integer *nrhs, real *ap, real *b, integer *ldb, integer *info);
 
+/* Subroutine */ int stpttf_(char *transr, char *uplo, integer *n, real *ap, 
+       real *arf, integer *info);
+
+/* Subroutine */ int stpttr_(char *uplo, integer *n, real *ap, real *a, 
+       integer *lda, integer *info);
+
 /* Subroutine */ int strcon_(char *norm, char *uplo, char *diag, integer *n, 
        real *a, integer *lda, real *rcond, real *work, integer *iwork, 
        integer *info);
@@ -2829,6 +3652,12 @@ extern "C" {
        integer *nrhs, real *a, integer *lda, real *b, integer *ldb, integer *
        info);
 
+/* Subroutine */ int strttf_(char *transr, char *uplo, integer *n, real *a, 
+       integer *lda, real *arf, integer *info);
+
+/* Subroutine */ int strttp_(char *uplo, integer *n, real *a, integer *lda, 
+       real *ap, integer *info);
+
 /* Subroutine */ int stzrqf_(integer *m, integer *n, real *a, integer *lda, 
        real *tau, integer *info);
 
@@ -2837,9 +3666,57 @@ extern "C" {
 
 /* Subroutine */ int xerbla_(char *srname, integer *info);
 
+/* Subroutine */ int xerbla_array__(char *srname_array__, integer *
+       srname_len__, integer *info, ftnlen srname_array_len);
+
+
+/* Subroutine */ int dlamc1_(integer *beta, integer *t, logical *rnd, logical 
+       *ieee1);
+
+doublereal dsecnd_();
+
+/* Subroutine */ int ilaver_(integer *vers_major__, integer *vers_minor__, 
+       integer *vers_patch__);
+
+doublereal second_();
+
+doublereal slamch_(char *cmach);
+
+/* Subroutine */ int slamc1_(integer *beta, integer *t, logical *rnd, logical 
+       *ieee1);
+
+/* Subroutine */ int slamc2_(integer *beta, integer *t, logical *rnd, real *
+                   eps, integer *emin, real *rmin, integer *emax, real *rmax);
+
+doublereal slamc3_(real *a, real *b);
+
+/* Subroutine */ int slamc4_(integer *emin, real *start, integer *base);
+
+/* Subroutine */ int slamc5_(integer *beta, integer *p, integer *emin,
+                   logical *ieee, integer *emax, real *rmax);
+
+
+doublereal dlamch_(char *cmach);
+
+/* Subroutine */ int dlamc1_(integer *beta, integer *t, logical *rnd, logical
+                   *ieee1);
+
+/* Subroutine */ int dlamc2_(integer *beta, integer *t, logical *rnd,
+                   doublereal *eps, integer *emin, doublereal *rmin, integer *emax,
+                           doublereal *rmax);
+
+doublereal dlamc3_(doublereal *a, doublereal *b);
+
+/* Subroutine */ int dlamc4_(integer *emin, doublereal *start, integer *base);
+
+/* Subroutine */ int dlamc5_(integer *beta, integer *p, integer *emin,
+                   logical *ieee, integer *emax, doublereal *rmax);
+
+integer ilaenv_(integer *ispec, char *name__, char *opts, integer *n1, 
+       integer *n2, integer *n3, integer *n4);
+
 #ifdef __cplusplus
 }
 #endif
 
 #endif /* __CLAPACK_H */
-
index 2e194b7..ef6dc8e 100644 (file)
-/* png.h - header file for PNG reference library
- *
- * libpng version 1.2.29 - May 8, 2008
- * Copyright (c) 1998-2008 Glenn Randers-Pehrson
- * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
- * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
- *
- * Authors and maintainers:
- *  libpng versions 0.71, May 1995, through 0.88, January 1996: Guy Schalnat
- *  libpng versions 0.89c, June 1996, through 0.96, May 1997: Andreas Dilger
- *  libpng versions 0.97, January 1998, through 1.2.29 - May 8, 2008: Glenn
- *  See also "Contributing Authors", below.
- *
- * Note about libpng version numbers:
- *
- *    Due to various miscommunications, unforeseen code incompatibilities
- *    and occasional factors outside the authors' control, version numbering
- *    on the library has not always been consistent and straightforward.
- *    The following table summarizes matters since version 0.89c, which was
- *    the first widely used release:
- *
- *    source                 png.h  png.h  shared-lib
- *    version                string   int  version
- *    -------                ------ -----  ----------
- *    0.89c "1.0 beta 3"     0.89      89  1.0.89
- *    0.90  "1.0 beta 4"     0.90      90  0.90  [should have been 2.0.90]
- *    0.95  "1.0 beta 5"     0.95      95  0.95  [should have been 2.0.95]
- *    0.96  "1.0 beta 6"     0.96      96  0.96  [should have been 2.0.96]
- *    0.97b "1.00.97 beta 7" 1.00.97   97  1.0.1 [should have been 2.0.97]
- *    0.97c                  0.97      97  2.0.97
- *    0.98                   0.98      98  2.0.98
- *    0.99                   0.99      98  2.0.99
- *    0.99a-m                0.99      99  2.0.99
- *    1.00                   1.00     100  2.1.0 [100 should be 10000]
- *    1.0.0      (from here on, the   100  2.1.0 [100 should be 10000]
- *    1.0.1       png.h string is   10001  2.1.0
- *    1.0.1a-e    identical to the  10002  from here on, the shared library
- *    1.0.2       source version)   10002  is 2.V where V is the source code
- *    1.0.2a-b                      10003  version, except as noted.
- *    1.0.3                         10003
- *    1.0.3a-d                      10004
- *    1.0.4                         10004
- *    1.0.4a-f                      10005
- *    1.0.5 (+ 2 patches)           10005
- *    1.0.5a-d                      10006
- *    1.0.5e-r                      10100 (not source compatible)
- *    1.0.5s-v                      10006 (not binary compatible)
- *    1.0.6 (+ 3 patches)           10006 (still binary incompatible)
- *    1.0.6d-f                      10007 (still binary incompatible)
- *    1.0.6g                        10007
- *    1.0.6h                        10007  10.6h (testing xy.z so-numbering)
- *    1.0.6i                        10007  10.6i
- *    1.0.6j                        10007  2.1.0.6j (incompatible with 1.0.0)
- *    1.0.7beta11-14        DLLNUM  10007  2.1.0.7beta11-14 (binary compatible)
- *    1.0.7beta15-18           1    10007  2.1.0.7beta15-18 (binary compatible)
- *    1.0.7rc1-2               1    10007  2.1.0.7rc1-2 (binary compatible)
- *    1.0.7                    1    10007  (still compatible)
- *    1.0.8beta1-4             1    10008  2.1.0.8beta1-4
- *    1.0.8rc1                 1    10008  2.1.0.8rc1
- *    1.0.8                    1    10008  2.1.0.8
- *    1.0.9beta1-6             1    10009  2.1.0.9beta1-6
- *    1.0.9rc1                 1    10009  2.1.0.9rc1
- *    1.0.9beta7-10            1    10009  2.1.0.9beta7-10
- *    1.0.9rc2                 1    10009  2.1.0.9rc2
- *    1.0.9                    1    10009  2.1.0.9
- *    1.0.10beta1              1    10010  2.1.0.10beta1
- *    1.0.10rc1                1    10010  2.1.0.10rc1
- *    1.0.10                   1    10010  2.1.0.10
- *    1.0.11beta1-3            1    10011  2.1.0.11beta1-3
- *    1.0.11rc1                1    10011  2.1.0.11rc1
- *    1.0.11                   1    10011  2.1.0.11
- *    1.0.12beta1-2            2    10012  2.1.0.12beta1-2
- *    1.0.12rc1                2    10012  2.1.0.12rc1
- *    1.0.12                   2    10012  2.1.0.12
- *    1.1.0a-f                 -    10100  2.1.1.0a-f (branch abandoned)
- *    1.2.0beta1-2             2    10200  2.1.2.0beta1-2
- *    1.2.0beta3-5             3    10200  3.1.2.0beta3-5
- *    1.2.0rc1                 3    10200  3.1.2.0rc1
- *    1.2.0                    3    10200  3.1.2.0
- *    1.2.1beta1-4             3    10201  3.1.2.1beta1-4
- *    1.2.1rc1-2               3    10201  3.1.2.1rc1-2
- *    1.2.1                    3    10201  3.1.2.1
- *    1.2.2beta1-6            12    10202  12.so.0.1.2.2beta1-6
- *    1.0.13beta1             10    10013  10.so.0.1.0.13beta1
- *    1.0.13rc1               10    10013  10.so.0.1.0.13rc1
- *    1.2.2rc1                12    10202  12.so.0.1.2.2rc1
- *    1.0.13                  10    10013  10.so.0.1.0.13
- *    1.2.2                   12    10202  12.so.0.1.2.2
- *    1.2.3rc1-6              12    10203  12.so.0.1.2.3rc1-6
- *    1.2.3                   12    10203  12.so.0.1.2.3
- *    1.2.4beta1-3            13    10204  12.so.0.1.2.4beta1-3
- *    1.0.14rc1               13    10014  10.so.0.1.0.14rc1
- *    1.2.4rc1                13    10204  12.so.0.1.2.4rc1
- *    1.0.14                  10    10014  10.so.0.1.0.14
- *    1.2.4                   13    10204  12.so.0.1.2.4
- *    1.2.5beta1-2            13    10205  12.so.0.1.2.5beta1-2
- *    1.0.15rc1-3             10    10015  10.so.0.1.0.15rc1-3
- *    1.2.5rc1-3              13    10205  12.so.0.1.2.5rc1-3
- *    1.0.15                  10    10015  10.so.0.1.0.15
- *    1.2.5                   13    10205  12.so.0.1.2.5
- *    1.2.6beta1-4            13    10206  12.so.0.1.2.6beta1-4
- *    1.0.16                  10    10016  10.so.0.1.0.16
- *    1.2.6                   13    10206  12.so.0.1.2.6
- *    1.2.7beta1-2            13    10207  12.so.0.1.2.7beta1-2
- *    1.0.17rc1               10    10017  10.so.0.1.0.17rc1
- *    1.2.7rc1                13    10207  12.so.0.1.2.7rc1
- *    1.0.17                  10    10017  10.so.0.1.0.17
- *    1.2.7                   13    10207  12.so.0.1.2.7
- *    1.2.8beta1-5            13    10208  12.so.0.1.2.8beta1-5
- *    1.0.18rc1-5             10    10018  10.so.0.1.0.18rc1-5
- *    1.2.8rc1-5              13    10208  12.so.0.1.2.8rc1-5
- *    1.0.18                  10    10018  10.so.0.1.0.18
- *    1.2.8                   13    10208  12.so.0.1.2.8
- *    1.2.9beta1-3            13    10209  12.so.0.1.2.9beta1-3
- *    1.2.9beta4-11           13    10209  12.so.0.9[.0]
- *    1.2.9rc1                13    10209  12.so.0.9[.0]
- *    1.2.9                   13    10209  12.so.0.9[.0]
- *    1.2.10beta1-8           13    10210  12.so.0.10[.0]
- *    1.2.10rc1-3             13    10210  12.so.0.10[.0]
- *    1.2.10                  13    10210  12.so.0.10[.0]
- *    1.2.11beta1-4           13    10211  12.so.0.11[.0]
- *    1.0.19rc1-5             10    10019  10.so.0.19[.0]
- *    1.2.11rc1-5             13    10211  12.so.0.11[.0]
- *    1.0.19                  10    10019  10.so.0.19[.0]
- *    1.2.11                  13    10211  12.so.0.11[.0]
- *    1.0.20                  10    10020  10.so.0.20[.0]
- *    1.2.12                  13    10212  12.so.0.12[.0]
- *    1.2.13beta1             13    10213  12.so.0.13[.0]
- *    1.0.21                  10    10021  10.so.0.21[.0]
- *    1.2.13                  13    10213  12.so.0.13[.0]
- *    1.2.14beta1-2           13    10214  12.so.0.14[.0]
- *    1.0.22rc1               10    10022  10.so.0.22[.0]
- *    1.2.14rc1               13    10214  12.so.0.14[.0]
- *    1.0.22                  10    10022  10.so.0.22[.0]
- *    1.2.14                  13    10214  12.so.0.14[.0]
- *    1.2.15beta1-6           13    10215  12.so.0.15[.0]
- *    1.0.23rc1-5             10    10023  10.so.0.23[.0]
- *    1.2.15rc1-5             13    10215  12.so.0.15[.0]
- *    1.0.23                  10    10023  10.so.0.23[.0]
- *    1.2.15                  13    10215  12.so.0.15[.0]
- *    1.2.16beta1-2           13    10216  12.so.0.16[.0]
- *    1.2.16rc1               13    10216  12.so.0.16[.0]
- *    1.0.24                  10    10024  10.so.0.24[.0]
- *    1.2.16                  13    10216  12.so.0.16[.0]
- *    1.2.17beta1-2           13    10217  12.so.0.17[.0]
- *    1.0.25rc1               10    10025  10.so.0.25[.0]
- *    1.2.17rc1-3             13    10217  12.so.0.17[.0]
- *    1.0.25                  10    10025  10.so.0.25[.0]
- *    1.2.17                  13    10217  12.so.0.17[.0]
- *    1.0.26                  10    10026  10.so.0.26[.0]
- *    1.2.18                  13    10218  12.so.0.18[.0]
- *    1.2.19beta1-31          13    10219  12.so.0.19[.0]
- *    1.0.27rc1-6             10    10027  10.so.0.27[.0]
- *    1.2.19rc1-6             13    10219  12.so.0.19[.0]
- *    1.0.27                  10    10027  10.so.0.27[.0]
- *    1.2.19                  13    10219  12.so.0.19[.0]
- *    1.2.20beta01-04         13    10220  12.so.0.20[.0]
- *    1.0.28rc1-6             10    10028  10.so.0.28[.0]
- *    1.2.20rc1-6             13    10220  12.so.0.20[.0]
- *    1.0.28                  10    10028  10.so.0.28[.0]
- *    1.2.20                  13    10220  12.so.0.20[.0]
- *    1.2.21beta1-2           13    10221  12.so.0.21[.0]
- *    1.2.21rc1-3             13    10221  12.so.0.21[.0]
- *    1.0.29                  10    10029  10.so.0.29[.0]
- *    1.2.21                  13    10221  12.so.0.21[.0]
- *    1.2.22beta1-4           13    10222  12.so.0.22[.0]
- *    1.0.30rc1               10    10030  10.so.0.30[.0]
- *    1.2.22rc1               13    10222  12.so.0.22[.0]
- *    1.0.30                  10    10030  10.so.0.30[.0]
- *    1.2.22                  13    10222  12.so.0.22[.0]
- *    1.2.23beta01-05         13    10223  12.so.0.23[.0]
- *    1.2.23rc01              13    10223  12.so.0.23[.0]
- *    1.2.23                  13    10223  12.so.0.23[.0]
- *    1.2.24beta01-02         13    10224  12.so.0.24[.0]
- *    1.2.24rc01              13    10224  12.so.0.24[.0]
- *    1.2.24                  13    10224  12.so.0.24[.0]
- *    1.2.25beta01-06         13    10225  12.so.0.25[.0]
- *    1.2.25rc01-02           13    10225  12.so.0.25[.0]
- *    1.0.31                  10    10031  10.so.0.31[.0]
- *    1.2.25                  13    10225  12.so.0.25[.0]
- *    1.2.26beta01-06         13    10226  12.so.0.26[.0]
- *    1.2.26rc01              13    10226  12.so.0.26[.0]
- *    1.2.26                  13    10226  12.so.0.26[.0]
- *    1.0.32                  10    10032  10.so.0.32[.0]
- *    1.2.27beta01-06         13    10227  12.so.0.27[.0]
- *    1.2.27rc01              13    10227  12.so.0.27[.0]
- *    1.0.33                  10    10033  10.so.0.33[.0]
- *    1.2.27                  13    10227  12.so.0.27[.0]
- *    1.0.34                  10    10034  10.so.0.34[.0]
- *    1.2.28                  13    10228  12.so.0.28[.0]
- *    1.2.29beta01-03         13    10229  12.so.0.29[.0]
- *    1.2.29rc01              13    10229  12.so.0.29[.0]
- *    1.0.35                  10    10035  10.so.0.35[.0]
- *    1.2.29                  13    10229  12.so.0.29[.0]
- *
- *    Henceforth the source version will match the shared-library major
- *    and minor numbers; the shared-library major version number will be
- *    used for changes in backward compatibility, as it is intended.  The
- *    PNG_LIBPNG_VER macro, which is not used within libpng but is available
- *    for applications, is an unsigned integer of the form xyyzz corresponding
- *    to the source version x.y.z (leading zeros in y and z).  Beta versions
- *    were given the previous public release number plus a letter, until
- *    version 1.0.6j; from then on they were given the upcoming public
- *    release number plus "betaNN" or "rcNN".
- *
- *    Binary incompatibility exists only when applications make direct access
- *    to the info_ptr or png_ptr members through png.h, and the compiled
- *    application is loaded with a different version of the library.
- *
- *    DLLNUM will change each time there are forward or backward changes
- *    in binary compatibility (e.g., when a new feature is added).
- *
- * See libpng.txt or libpng.3 for more information.  The PNG specification
- * is available as a W3C Recommendation and as an ISO Specification,
- * <http://www.w3.org/TR/2003/REC-PNG-20031110/
- */
-
-/*
- * COPYRIGHT NOTICE, DISCLAIMER, and LICENSE:
- *
- * If you modify libpng you may insert additional notices immediately following
- * this sentence.
- *
- * libpng versions 1.2.6, August 15, 2004, through 1.2.29, May 8, 2008, are
- * Copyright (c) 2004, 2006-2008 Glenn Randers-Pehrson, and are
- * distributed according to the same disclaimer and license as libpng-1.2.5
- * with the following individual added to the list of Contributing Authors:
- *
- *    Cosmin Truta
- *
- * libpng versions 1.0.7, July 1, 2000, through 1.2.5, October 3, 2002, are
- * Copyright (c) 2000-2002 Glenn Randers-Pehrson, and are
- * distributed according to the same disclaimer and license as libpng-1.0.6
- * with the following individuals added to the list of Contributing Authors:
- *
- *    Simon-Pierre Cadieux
- *    Eric S. Raymond
- *    Gilles Vollant
- *
- * and with the following additions to the disclaimer:
- *
- *    There is no warranty against interference with your enjoyment of the
- *    library or against infringement.  There is no warranty that our
- *    efforts or the library will fulfill any of your particular purposes
- *    or needs.  This library is provided with all faults, and the entire
- *    risk of satisfactory quality, performance, accuracy, and effort is with
- *    the user.
- *
- * libpng versions 0.97, January 1998, through 1.0.6, March 20, 2000, are
- * Copyright (c) 1998, 1999, 2000 Glenn Randers-Pehrson, and are
- * distributed according to the same disclaimer and license as libpng-0.96,
- * with the following individuals added to the list of Contributing Authors:
- *
- *    Tom Lane
- *    Glenn Randers-Pehrson
- *    Willem van Schaik
- *
- * libpng versions 0.89, June 1996, through 0.96, May 1997, are
- * Copyright (c) 1996, 1997 Andreas Dilger
- * Distributed according to the same disclaimer and license as libpng-0.88,
- * with the following individuals added to the list of Contributing Authors:
- *
- *    John Bowler
- *    Kevin Bracey
- *    Sam Bushell
- *    Magnus Holmgren
- *    Greg Roelofs
- *    Tom Tanner
- *
- * libpng versions 0.5, May 1995, through 0.88, January 1996, are
- * Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
- *
- * For the purposes of this copyright and license, "Contributing Authors"
- * is defined as the following set of individuals:
- *
- *    Andreas Dilger
- *    Dave Martindale
- *    Guy Eric Schalnat
- *    Paul Schmidt
- *    Tim Wegner
- *
- * The PNG Reference Library is supplied "AS IS".  The Contributing Authors
- * and Group 42, Inc. disclaim all warranties, expressed or implied,
- * including, without limitation, the warranties of merchantability and of
- * fitness for any purpose.  The Contributing Authors and Group 42, Inc.
- * assume no liability for direct, indirect, incidental, special, exemplary,
- * or consequential damages, which may result from the use of the PNG
- * Reference Library, even if advised of the possibility of such damage.
- *
- * Permission is hereby granted to use, copy, modify, and distribute this
- * source code, or portions hereof, for any purpose, without fee, subject
- * to the following restrictions:
- *
- * 1. The origin of this source code must not be misrepresented.
- *
- * 2. Altered versions must be plainly marked as such and
- * must not be misrepresented as being the original source.
- *
- * 3. This Copyright notice may not be removed or altered from
- *    any source or altered source distribution.
- *
- * The Contributing Authors and Group 42, Inc. specifically permit, without
- * fee, and encourage the use of this source code as a component to
- * supporting the PNG file format in commercial products.  If you use this
- * source code in a product, acknowledgment is not required but would be
- * appreciated.
- */
-
-/*
- * A "png_get_copyright" function is available, for convenient use in "about"
- * boxes and the like:
- *
- * printf("%s",png_get_copyright(NULL));
- *
- * Also, the PNG logo (in PNG format, of course) is supplied in the
- * files "pngbar.png" and "pngbar.jpg (88x31) and "pngnow.png" (98x31).
- */
-
-/*
- * Libpng is OSI Certified Open Source Software.  OSI Certified is a
- * certification mark of the Open Source Initiative.
- */
-
-/*
- * The contributing authors would like to thank all those who helped
- * with testing, bug fixes, and patience.  This wouldn't have been
- * possible without all of you.
- *
- * Thanks to Frank J. T. Wojcik for helping with the documentation.
- */
-
-/*
- * Y2K compliance in libpng:
- * =========================
- *
- *    May 8, 2008
- *
- *    Since the PNG Development group is an ad-hoc body, we can't make
- *    an official declaration.
- *
- *    This is your unofficial assurance that libpng from version 0.71 and
- *    upward through 1.2.29 are Y2K compliant.  It is my belief that earlier
- *    versions were also Y2K compliant.
- *
- *    Libpng only has three year fields.  One is a 2-byte unsigned integer
- *    that will hold years up to 65535.  The other two hold the date in text
- *    format, and will hold years up to 9999.
- *
- *    The integer is
- *        "png_uint_16 year" in png_time_struct.
- *
- *    The strings are
- *        "png_charp time_buffer" in png_struct and
- *        "near_time_buffer", which is a local character string in png.c.
- *
- *    There are seven time-related functions:
- *        png.c: png_convert_to_rfc_1123() in png.c
- *          (formerly png_convert_to_rfc_1152() in error)
- *        png_convert_from_struct_tm() in pngwrite.c, called in pngwrite.c
- *        png_convert_from_time_t() in pngwrite.c
- *        png_get_tIME() in pngget.c
- *        png_handle_tIME() in pngrutil.c, called in pngread.c
- *        png_set_tIME() in pngset.c
- *        png_write_tIME() in pngwutil.c, called in pngwrite.c
- *
- *    All handle dates properly in a Y2K environment.  The
- *    png_convert_from_time_t() function calls gmtime() to convert from system
- *    clock time, which returns (year - 1900), which we properly convert to
- *    the full 4-digit year.  There is a possibility that applications using
- *    libpng are not passing 4-digit years into the png_convert_to_rfc_1123()
- *    function, or that they are incorrectly passing only a 2-digit year
- *    instead of "year - 1900" into the png_convert_from_struct_tm() function,
- *    but this is not under our control.  The libpng documentation has always
- *    stated that it works with 4-digit years, and the APIs have been
- *    documented as such.
- *
- *    The tIME chunk itself is also Y2K compliant.  It uses a 2-byte unsigned
- *    integer to hold the year, and can hold years as large as 65535.
- *
- *    zlib, upon which libpng depends, is also Y2K compliant.  It contains
- *    no date-related code.
- *
- *       Glenn Randers-Pehrson
- *       libpng maintainer
- *       PNG Development Group
- */
-
-#ifndef PNG_H
-#define PNG_H
-
-/* This is not the place to learn how to use libpng.  The file libpng.txt
- * describes how to use libpng, and the file example.c summarizes it
- * with some code on which to build.  This file is useful for looking
- * at the actual function definitions and structure components.
- */
-
-/* Version information for png.h - this should match the version in png.c */
-#define PNG_LIBPNG_VER_STRING "1.2.29"
-#define PNG_HEADER_VERSION_STRING \
-   " libpng version 1.2.29 - May 8, 2008\n"
-
-#define PNG_LIBPNG_VER_SONUM   0
-#define PNG_LIBPNG_VER_DLLNUM  13
-
-/* These should match the first 3 components of PNG_LIBPNG_VER_STRING: */
-#define PNG_LIBPNG_VER_MAJOR   1
-#define PNG_LIBPNG_VER_MINOR   2
-#define PNG_LIBPNG_VER_RELEASE 29
-/* This should match the numeric part of the final component of
- * PNG_LIBPNG_VER_STRING, omitting any leading zero: */
-
-#define PNG_LIBPNG_VER_BUILD  0
-
-/* Release Status */
-#define PNG_LIBPNG_BUILD_ALPHA    1
-#define PNG_LIBPNG_BUILD_BETA     2
-#define PNG_LIBPNG_BUILD_RC       3
-#define PNG_LIBPNG_BUILD_STABLE   4
-#define PNG_LIBPNG_BUILD_RELEASE_STATUS_MASK 7
-  
-/* Release-Specific Flags */
-#define PNG_LIBPNG_BUILD_PATCH    8 /* Can be OR'ed with
-                                       PNG_LIBPNG_BUILD_STABLE only */
-#define PNG_LIBPNG_BUILD_PRIVATE 16 /* Cannot be OR'ed with
-                                       PNG_LIBPNG_BUILD_SPECIAL */
-#define PNG_LIBPNG_BUILD_SPECIAL 32 /* Cannot be OR'ed with
-                                       PNG_LIBPNG_BUILD_PRIVATE */
-
-#define PNG_LIBPNG_BUILD_BASE_TYPE PNG_LIBPNG_BUILD_STABLE
-
-/* Careful here.  At one time, Guy wanted to use 082, but that would be octal.
- * We must not include leading zeros.
- * Versions 0.7 through 1.0.0 were in the range 0 to 100 here (only
- * version 1.0.0 was mis-numbered 100 instead of 10000).  From
- * version 1.0.1 it's    xxyyzz, where x=major, y=minor, z=release */
-#define PNG_LIBPNG_VER 10229 /* 1.2.29 */
-
-#ifndef PNG_VERSION_INFO_ONLY
-/* include the compression library's header */
-#include "zlib.h"
-#endif
-
-/* include all user configurable info, including optional assembler routines */
-#include "pngconf.h"
-
-/*
- * Added at libpng-1.2.8 */
-/* Ref MSDN: Private as priority over Special
- * VS_FF_PRIVATEBUILD File *was not* built using standard release
- * procedures. If this value is given, the StringFileInfo block must
- * contain a PrivateBuild string. 
- *
- * VS_FF_SPECIALBUILD File *was* built by the original company using
- * standard release procedures but is a variation of the standard
- * file of the same version number. If this value is given, the
- * StringFileInfo block must contain a SpecialBuild string. 
- */
-
-#if defined(PNG_USER_PRIVATEBUILD)
-#  define PNG_LIBPNG_BUILD_TYPE \
-          (PNG_LIBPNG_BUILD_BASE_TYPE | PNG_LIBPNG_BUILD_PRIVATE)
-#else
-#  if defined(PNG_LIBPNG_SPECIALBUILD)
-#    define PNG_LIBPNG_BUILD_TYPE \
-            (PNG_LIBPNG_BUILD_BASE_TYPE | PNG_LIBPNG_BUILD_SPECIAL)
-#  else
-#    define PNG_LIBPNG_BUILD_TYPE (PNG_LIBPNG_BUILD_BASE_TYPE)
-#  endif
-#endif
-
-#ifndef PNG_VERSION_INFO_ONLY
-
-/* Inhibit C++ name-mangling for libpng functions but not for system calls. */
-#ifdef __cplusplus
-extern "C" {
-#endif /* __cplusplus */
-
-/* This file is arranged in several sections.  The first section contains
- * structure and type definitions.  The second section contains the external
- * library functions, while the third has the internal library functions,
- * which applications aren't expected to use directly.
- */
-
-#ifndef PNG_NO_TYPECAST_NULL
-#define int_p_NULL                (int *)NULL
-#define png_bytep_NULL            (png_bytep)NULL
-#define png_bytepp_NULL           (png_bytepp)NULL
-#define png_doublep_NULL          (png_doublep)NULL
-#define png_error_ptr_NULL        (png_error_ptr)NULL
-#define png_flush_ptr_NULL        (png_flush_ptr)NULL
-#define png_free_ptr_NULL         (png_free_ptr)NULL
-#define png_infopp_NULL           (png_infopp)NULL
-#define png_malloc_ptr_NULL       (png_malloc_ptr)NULL
-#define png_read_status_ptr_NULL  (png_read_status_ptr)NULL
-#define png_rw_ptr_NULL           (png_rw_ptr)NULL
-#define png_structp_NULL          (png_structp)NULL
-#define png_uint_16p_NULL         (png_uint_16p)NULL
-#define png_voidp_NULL            (png_voidp)NULL
-#define png_write_status_ptr_NULL (png_write_status_ptr)NULL
-#else
-#define int_p_NULL                NULL
-#define png_bytep_NULL            NULL
-#define png_bytepp_NULL           NULL
-#define png_doublep_NULL          NULL
-#define png_error_ptr_NULL        NULL
-#define png_flush_ptr_NULL        NULL
-#define png_free_ptr_NULL         NULL
-#define png_infopp_NULL           NULL
-#define png_malloc_ptr_NULL       NULL
-#define png_read_status_ptr_NULL  NULL
-#define png_rw_ptr_NULL           NULL
-#define png_structp_NULL          NULL
-#define png_uint_16p_NULL         NULL
-#define png_voidp_NULL            NULL
-#define png_write_status_ptr_NULL NULL
-#endif
-
-/* variables declared in png.c - only it needs to define PNG_NO_EXTERN */
-#if !defined(PNG_NO_EXTERN) || defined(PNG_ALWAYS_EXTERN)
-/* Version information for C files, stored in png.c.  This had better match
- * the version above.
- */
-#ifdef PNG_USE_GLOBAL_ARRAYS
-PNG_EXPORT_VAR (PNG_CONST char) png_libpng_ver[18];
-  /* need room for 99.99.99beta99z */
-#else
-#define png_libpng_ver png_get_header_ver(NULL)
-#endif
-
-#ifdef PNG_USE_GLOBAL_ARRAYS
-/* This was removed in version 1.0.5c */
-/* Structures to facilitate easy interlacing.  See png.c for more details */
-PNG_EXPORT_VAR (PNG_CONST int FARDATA) png_pass_start[7];
-PNG_EXPORT_VAR (PNG_CONST int FARDATA) png_pass_inc[7];
-PNG_EXPORT_VAR (PNG_CONST int FARDATA) png_pass_ystart[7];
-PNG_EXPORT_VAR (PNG_CONST int FARDATA) png_pass_yinc[7];
-PNG_EXPORT_VAR (PNG_CONST int FARDATA) png_pass_mask[7];
-PNG_EXPORT_VAR (PNG_CONST int FARDATA) png_pass_dsp_mask[7];
-/* This isn't currently used.  If you need it, see png.c for more details.
-PNG_EXPORT_VAR (PNG_CONST int FARDATA) png_pass_height[7];
-*/
-#endif
-
-#endif /* PNG_NO_EXTERN */
-
-/* Three color definitions.  The order of the red, green, and blue, (and the
- * exact size) is not important, although the size of the fields need to
- * be png_byte or png_uint_16 (as defined below).
- */
-typedef struct png_color_struct
-{
-   png_byte red;
-   png_byte green;
-   png_byte blue;
-} png_color;
-typedef png_color FAR * png_colorp;
-typedef png_color FAR * FAR * png_colorpp;
-
-typedef struct png_color_16_struct
-{
-   png_byte index;    /* used for palette files */
-   png_uint_16 red;   /* for use in red green blue files */
-   png_uint_16 green;
-   png_uint_16 blue;
-   png_uint_16 gray;  /* for use in grayscale files */
-} png_color_16;
-typedef png_color_16 FAR * png_color_16p;
-typedef png_color_16 FAR * FAR * png_color_16pp;
-
-typedef struct png_color_8_struct
-{
-   png_byte red;   /* for use in red green blue files */
-   png_byte green;
-   png_byte blue;
-   png_byte gray;  /* for use in grayscale files */
-   png_byte alpha; /* for alpha channel files */
-} png_color_8;
-typedef png_color_8 FAR * png_color_8p;
-typedef png_color_8 FAR * FAR * png_color_8pp;
-
-/*
- * The following two structures are used for the in-core representation
- * of sPLT chunks.
- */
-typedef struct png_sPLT_entry_struct
-{
-   png_uint_16 red;
-   png_uint_16 green;
-   png_uint_16 blue;
-   png_uint_16 alpha;
-   png_uint_16 frequency;
-} png_sPLT_entry;
-typedef png_sPLT_entry FAR * png_sPLT_entryp;
-typedef png_sPLT_entry FAR * FAR * png_sPLT_entrypp;
-
-/*  When the depth of the sPLT palette is 8 bits, the color and alpha samples
- *  occupy the LSB of their respective members, and the MSB of each member
- *  is zero-filled.  The frequency member always occupies the full 16 bits.
- */
-
-typedef struct png_sPLT_struct
-{
-   png_charp name;           /* palette name */
-   png_byte depth;           /* depth of palette samples */
-   png_sPLT_entryp entries;  /* palette entries */
-   png_int_32 nentries;      /* number of palette entries */
-} png_sPLT_t;
-typedef png_sPLT_t FAR * png_sPLT_tp;
-typedef png_sPLT_t FAR * FAR * png_sPLT_tpp;
-
-#ifdef PNG_TEXT_SUPPORTED
-/* png_text holds the contents of a text/ztxt/itxt chunk in a PNG file,
- * and whether that contents is compressed or not.  The "key" field
- * points to a regular zero-terminated C string.  The "text", "lang", and
- * "lang_key" fields can be regular C strings, empty strings, or NULL pointers.
- * However, the * structure returned by png_get_text() will always contain
- * regular zero-terminated C strings (possibly empty), never NULL pointers,
- * so they can be safely used in printf() and other string-handling functions.
- */
-typedef struct png_text_struct
-{
-   int  compression;       /* compression value:
-                             -1: tEXt, none
-                              0: zTXt, deflate
-                              1: iTXt, none
-                              2: iTXt, deflate  */
-   png_charp key;          /* keyword, 1-79 character description of "text" */
-   png_charp text;         /* comment, may be an empty string (ie "")
-                              or a NULL pointer */
-   png_size_t text_length; /* length of the text string */
-#ifdef PNG_iTXt_SUPPORTED
-   png_size_t itxt_length; /* length of the itxt string */
-   png_charp lang;         /* language code, 0-79 characters
-                              or a NULL pointer */
-   png_charp lang_key;     /* keyword translated UTF-8 string, 0 or more
-                              chars or a NULL pointer */
-#endif
-} png_text;
-typedef png_text FAR * png_textp;
-typedef png_text FAR * FAR * png_textpp;
-#endif
-
-/* Supported compression types for text in PNG files (tEXt, and zTXt).
- * The values of the PNG_TEXT_COMPRESSION_ defines should NOT be changed. */
-#define PNG_TEXT_COMPRESSION_NONE_WR -3
-#define PNG_TEXT_COMPRESSION_zTXt_WR -2
-#define PNG_TEXT_COMPRESSION_NONE    -1
-#define PNG_TEXT_COMPRESSION_zTXt     0
-#define PNG_ITXT_COMPRESSION_NONE     1
-#define PNG_ITXT_COMPRESSION_zTXt     2
-#define PNG_TEXT_COMPRESSION_LAST     3  /* Not a valid value */
-
-/* png_time is a way to hold the time in an machine independent way.
- * Two conversions are provided, both from time_t and struct tm.  There
- * is no portable way to convert to either of these structures, as far
- * as I know.  If you know of a portable way, send it to me.  As a side
- * note - PNG has always been Year 2000 compliant!
- */
-typedef struct png_time_struct
-{
-   png_uint_16 year; /* full year, as in, 1995 */
-   png_byte month;   /* month of year, 1 - 12 */
-   png_byte day;     /* day of month, 1 - 31 */
-   png_byte hour;    /* hour of day, 0 - 23 */
-   png_byte minute;  /* minute of hour, 0 - 59 */
-   png_byte second;  /* second of minute, 0 - 60 (for leap seconds) */
-} png_time;
-typedef png_time FAR * png_timep;
-typedef png_time FAR * FAR * png_timepp;
-
-#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
-/* png_unknown_chunk is a structure to hold queued chunks for which there is
- * no specific support.  The idea is that we can use this to queue
- * up private chunks for output even though the library doesn't actually
- * know about their semantics.
- */
-#define PNG_CHUNK_NAME_LENGTH 5
-typedef struct png_unknown_chunk_t
-{
-    png_byte name[PNG_CHUNK_NAME_LENGTH];
-    png_byte *data;
-    png_size_t size;
-
-    /* libpng-using applications should NOT directly modify this byte. */
-    png_byte location; /* mode of operation at read time */
-}
-png_unknown_chunk;
-typedef png_unknown_chunk FAR * png_unknown_chunkp;
-typedef png_unknown_chunk FAR * FAR * png_unknown_chunkpp;
-#endif
-
-/* png_info is a structure that holds the information in a PNG file so
- * that the application can find out the characteristics of the image.
- * If you are reading the file, this structure will tell you what is
- * in the PNG file.  If you are writing the file, fill in the information
- * you want to put into the PNG file, then call png_write_info().
- * The names chosen should be very close to the PNG specification, so
- * consult that document for information about the meaning of each field.
- *
- * With libpng < 0.95, it was only possible to directly set and read the
- * the values in the png_info_struct, which meant that the contents and
- * order of the values had to remain fixed.  With libpng 0.95 and later,
- * however, there are now functions that abstract the contents of
- * png_info_struct from the application, so this makes it easier to use
- * libpng with dynamic libraries, and even makes it possible to use
- * libraries that don't have all of the libpng ancillary chunk-handing
- * functionality.
- *
- * In any case, the order of the parameters in png_info_struct should NOT
- * be changed for as long as possible to keep compatibility with applications
- * that use the old direct-access method with png_info_struct.
- *
- * The following members may have allocated storage attached that should be
- * cleaned up before the structure is discarded: palette, trans, text,
- * pcal_purpose, pcal_units, pcal_params, hist, iccp_name, iccp_profile,
- * splt_palettes, scal_unit, row_pointers, and unknowns.   By default, these
- * are automatically freed when the info structure is deallocated, if they were
- * allocated internally by libpng.  This behavior can be changed by means
- * of the png_data_freer() function.
- *
- * More allocation details: all the chunk-reading functions that
- * change these members go through the corresponding png_set_*
- * functions.  A function to clear these members is available: see
- * png_free_data().  The png_set_* functions do not depend on being
- * able to point info structure members to any of the storage they are
- * passed (they make their own copies), EXCEPT that the png_set_text
- * functions use the same storage passed to them in the text_ptr or
- * itxt_ptr structure argument, and the png_set_rows and png_set_unknowns
- * functions do not make their own copies.
- */
-typedef struct png_info_struct
-{
-   /* the following are necessary for every PNG file */
-   png_uint_32 width;       /* width of image in pixels (from IHDR) */
-   png_uint_32 height;      /* height of image in pixels (from IHDR) */
-   png_uint_32 valid;       /* valid chunk data (see PNG_INFO_ below) */
-   png_uint_32 rowbytes;    /* bytes needed to hold an untransformed row */
-   png_colorp palette;      /* array of color values (valid & PNG_INFO_PLTE) */
-   png_uint_16 num_palette; /* number of color entries in "palette" (PLTE) */
-   png_uint_16 num_trans;   /* number of transparent palette color (tRNS) */
-   png_byte bit_depth;      /* 1, 2, 4, 8, or 16 bits/channel (from IHDR) */
-   png_byte color_type;     /* see PNG_COLOR_TYPE_ below (from IHDR) */
-   /* The following three should have been named *_method not *_type */
-   png_byte compression_type; /* must be PNG_COMPRESSION_TYPE_BASE (IHDR) */
-   png_byte filter_type;    /* must be PNG_FILTER_TYPE_BASE (from IHDR) */
-   png_byte interlace_type; /* One of PNG_INTERLACE_NONE, PNG_INTERLACE_ADAM7 */
-
-   /* The following is informational only on read, and not used on writes. */
-   png_byte channels;       /* number of data channels per pixel (1, 2, 3, 4) */
-   png_byte pixel_depth;    /* number of bits per pixel */
-   png_byte spare_byte;     /* to align the data, and for future use */
-   png_byte signature[8];   /* magic bytes read by libpng from start of file */
-
-   /* The rest of the data is optional.  If you are reading, check the
-    * valid field to see if the information in these are valid.  If you
-    * are writing, set the valid field to those chunks you want written,
-    * and initialize the appropriate fields below.
-    */
-
-#if defined(PNG_gAMA_SUPPORTED) && defined(PNG_FLOATING_POINT_SUPPORTED)
-   /* The gAMA chunk describes the gamma characteristics of the system
-    * on which the image was created, normally in the range [1.0, 2.5].
-    * Data is valid if (valid & PNG_INFO_gAMA) is non-zero.
-    */
-   float gamma; /* gamma value of image, if (valid & PNG_INFO_gAMA) */
-#endif
-
-#if defined(PNG_sRGB_SUPPORTED)
-    /* GR-P, 0.96a */
-    /* Data valid if (valid & PNG_INFO_sRGB) non-zero. */
-   png_byte srgb_intent; /* sRGB rendering intent [0, 1, 2, or 3] */
-#endif
-
-#if defined(PNG_TEXT_SUPPORTED)
-   /* The tEXt, and zTXt chunks contain human-readable textual data in
-    * uncompressed, compressed, and optionally compressed forms, respectively.
-    * The data in "text" is an array of pointers to uncompressed,
-    * null-terminated C strings. Each chunk has a keyword that describes the
-    * textual data contained in that chunk.  Keywords are not required to be
-    * unique, and the text string may be empty.  Any number of text chunks may
-    * be in an image.
-    */
-   int num_text; /* number of comments read/to write */
-   int max_text; /* current size of text array */
-   png_textp text; /* array of comments read/to write */
-#endif /* PNG_TEXT_SUPPORTED */
-
-#if defined(PNG_tIME_SUPPORTED)
-   /* The tIME chunk holds the last time the displayed image data was
-    * modified.  See the png_time struct for the contents of this struct.
-    */
-   png_time mod_time;
-#endif
-
-#if defined(PNG_sBIT_SUPPORTED)
-   /* The sBIT chunk specifies the number of significant high-order bits
-    * in the pixel data.  Values are in the range [1, bit_depth], and are
-    * only specified for the channels in the pixel data.  The contents of
-    * the low-order bits is not specified.  Data is valid if
-    * (valid & PNG_INFO_sBIT) is non-zero.
-    */
-   png_color_8 sig_bit; /* significant bits in color channels */
-#endif
-
-#if defined(PNG_tRNS_SUPPORTED) || defined(PNG_READ_EXPAND_SUPPORTED) || \
-defined(PNG_READ_BACKGROUND_SUPPORTED)
-   /* The tRNS chunk supplies transparency data for paletted images and
-    * other image types that don't need a full alpha channel.  There are
-    * "num_trans" transparency values for a paletted image, stored in the
-    * same order as the palette colors, starting from index 0.  Values
-    * for the data are in the range [0, 255], ranging from fully transparent
-    * to fully opaque, respectively.  For non-paletted images, there is a
-    * single color specified that should be treated as fully transparent.
-    * Data is valid if (valid & PNG_INFO_tRNS) is non-zero.
-    */
-   png_bytep trans; /* transparent values for paletted image */
-   png_color_16 trans_values; /* transparent color for non-palette image */
-#endif
-
-#if defined(PNG_bKGD_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
-   /* The bKGD chunk gives the suggested image background color if the
-    * display program does not have its own background color and the image
-    * is needs to composited onto a background before display.  The colors
-    * in "background" are normally in the same color space/depth as the
-    * pixel data.  Data is valid if (valid & PNG_INFO_bKGD) is non-zero.
-    */
-   png_color_16 background;
-#endif
-
-#if defined(PNG_oFFs_SUPPORTED)
-   /* The oFFs chunk gives the offset in "offset_unit_type" units rightwards
-    * and downwards from the top-left corner of the display, page, or other
-    * application-specific co-ordinate space.  See the PNG_OFFSET_ defines
-    * below for the unit types.  Valid if (valid & PNG_INFO_oFFs) non-zero.
-    */
-   png_int_32 x_offset; /* x offset on page */
-   png_int_32 y_offset; /* y offset on page */
-   png_byte offset_unit_type; /* offset units type */
-#endif
-
-#if defined(PNG_pHYs_SUPPORTED)
-   /* The pHYs chunk gives the physical pixel density of the image for
-    * display or printing in "phys_unit_type" units (see PNG_RESOLUTION_
-    * defines below).  Data is valid if (valid & PNG_INFO_pHYs) is non-zero.
-    */
-   png_uint_32 x_pixels_per_unit; /* horizontal pixel density */
-   png_uint_32 y_pixels_per_unit; /* vertical pixel density */
-   png_byte phys_unit_type; /* resolution type (see PNG_RESOLUTION_ below) */
-#endif
-
-#if defined(PNG_hIST_SUPPORTED)
-   /* The hIST chunk contains the relative frequency or importance of the
-    * various palette entries, so that a viewer can intelligently select a
-    * reduced-color palette, if required.  Data is an array of "num_palette"
-    * values in the range [0,65535]. Data valid if (valid & PNG_INFO_hIST)
-    * is non-zero.
-    */
-   png_uint_16p hist;
-#endif
-
-#ifdef PNG_cHRM_SUPPORTED
-   /* The cHRM chunk describes the CIE color characteristics of the monitor
-    * on which the PNG was created.  This data allows the viewer to do gamut
-    * mapping of the input image to ensure that the viewer sees the same
-    * colors in the image as the creator.  Values are in the range
-    * [0.0, 0.8].  Data valid if (valid & PNG_INFO_cHRM) non-zero.
-    */
-#ifdef PNG_FLOATING_POINT_SUPPORTED
-   float x_white;
-   float y_white;
-   float x_red;
-   float y_red;
-   float x_green;
-   float y_green;
-   float x_blue;
-   float y_blue;
-#endif
-#endif
-
-#if defined(PNG_pCAL_SUPPORTED)
-   /* The pCAL chunk describes a transformation between the stored pixel
-    * values and original physical data values used to create the image.
-    * The integer range [0, 2^bit_depth - 1] maps to the floating-point
-    * range given by [pcal_X0, pcal_X1], and are further transformed by a
-    * (possibly non-linear) transformation function given by "pcal_type"
-    * and "pcal_params" into "pcal_units".  Please see the PNG_EQUATION_
-    * defines below, and the PNG-Group's PNG extensions document for a
-    * complete description of the transformations and how they should be
-    * implemented, and for a description of the ASCII parameter strings.
-    * Data values are valid if (valid & PNG_INFO_pCAL) non-zero.
-    */
-   png_charp pcal_purpose;  /* pCAL chunk description string */
-   png_int_32 pcal_X0;      /* minimum value */
-   png_int_32 pcal_X1;      /* maximum value */
-   png_charp pcal_units;    /* Latin-1 string giving physical units */
-   png_charpp pcal_params;  /* ASCII strings containing parameter values */
-   png_byte pcal_type;      /* equation type (see PNG_EQUATION_ below) */
-   png_byte pcal_nparams;   /* number of parameters given in pcal_params */
-#endif
-
-/* New members added in libpng-1.0.6 */
-#ifdef PNG_FREE_ME_SUPPORTED
-   png_uint_32 free_me;     /* flags items libpng is responsible for freeing */
-#endif
-
-#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
-   /* storage for unknown chunks that the library doesn't recognize. */
-   png_unknown_chunkp unknown_chunks;
-   png_size_t unknown_chunks_num;
-#endif
-
-#if defined(PNG_iCCP_SUPPORTED)
-   /* iCCP chunk data. */
-   png_charp iccp_name;     /* profile name */
-   png_charp iccp_profile;  /* International Color Consortium profile data */
-                            /* Note to maintainer: should be png_bytep */
-   png_uint_32 iccp_proflen;  /* ICC profile data length */
-   png_byte iccp_compression; /* Always zero */
-#endif
-
-#if defined(PNG_sPLT_SUPPORTED)
-   /* data on sPLT chunks (there may be more than one). */
-   png_sPLT_tp splt_palettes;
-   png_uint_32 splt_palettes_num;
-#endif
-
-#if defined(PNG_sCAL_SUPPORTED)
-   /* The sCAL chunk describes the actual physical dimensions of the
-    * subject matter of the graphic.  The chunk contains a unit specification
-    * a byte value, and two ASCII strings representing floating-point
-    * values.  The values are width and height corresponsing to one pixel
-    * in the image.  This external representation is converted to double
-    * here.  Data values are valid if (valid & PNG_INFO_sCAL) is non-zero.
-    */
-   png_byte scal_unit;         /* unit of physical scale */
-#ifdef PNG_FLOATING_POINT_SUPPORTED
-   double scal_pixel_width;    /* width of one pixel */
-   double scal_pixel_height;   /* height of one pixel */
-#endif
-#ifdef PNG_FIXED_POINT_SUPPORTED
-   png_charp scal_s_width;     /* string containing height */
-   png_charp scal_s_height;    /* string containing width */
-#endif
-#endif
-
-#if defined(PNG_INFO_IMAGE_SUPPORTED)
-   /* Memory has been allocated if (valid & PNG_ALLOCATED_INFO_ROWS) non-zero */
-   /* Data valid if (valid & PNG_INFO_IDAT) non-zero */
-   png_bytepp row_pointers;        /* the image bits */
-#endif
-
-#if defined(PNG_FIXED_POINT_SUPPORTED) && defined(PNG_gAMA_SUPPORTED)
-   png_fixed_point int_gamma; /* gamma of image, if (valid & PNG_INFO_gAMA) */
-#endif
-
-#if defined(PNG_cHRM_SUPPORTED) && defined(PNG_FIXED_POINT_SUPPORTED)
-   png_fixed_point int_x_white;
-   png_fixed_point int_y_white;
-   png_fixed_point int_x_red;
-   png_fixed_point int_y_red;
-   png_fixed_point int_x_green;
-   png_fixed_point int_y_green;
-   png_fixed_point int_x_blue;
-   png_fixed_point int_y_blue;
-#endif
-
-} png_info;
-
-typedef png_info FAR * png_infop;
-typedef png_info FAR * FAR * png_infopp;
-
-/* Maximum positive integer used in PNG is (2^31)-1 */
-#define PNG_UINT_31_MAX ((png_uint_32)0x7fffffffL)
-#define PNG_UINT_32_MAX ((png_uint_32)(-1))
-#define PNG_SIZE_MAX ((png_size_t)(-1))
-#if defined(PNG_1_0_X) || defined (PNG_1_2_X)
-/* PNG_MAX_UINT is deprecated; use PNG_UINT_31_MAX instead. */
-#define PNG_MAX_UINT PNG_UINT_31_MAX
-#endif
-
-/* These describe the color_type field in png_info. */
-/* color type masks */
-#define PNG_COLOR_MASK_PALETTE    1
-#define PNG_COLOR_MASK_COLOR      2
-#define PNG_COLOR_MASK_ALPHA      4
-
-/* color types.  Note that not all combinations are legal */
-#define PNG_COLOR_TYPE_GRAY 0
-#define PNG_COLOR_TYPE_PALETTE  (PNG_COLOR_MASK_COLOR | PNG_COLOR_MASK_PALETTE)
-#define PNG_COLOR_TYPE_RGB        (PNG_COLOR_MASK_COLOR)
-#define PNG_COLOR_TYPE_RGB_ALPHA  (PNG_COLOR_MASK_COLOR | PNG_COLOR_MASK_ALPHA)
-#define PNG_COLOR_TYPE_GRAY_ALPHA (PNG_COLOR_MASK_ALPHA)
-/* aliases */
-#define PNG_COLOR_TYPE_RGBA  PNG_COLOR_TYPE_RGB_ALPHA
-#define PNG_COLOR_TYPE_GA  PNG_COLOR_TYPE_GRAY_ALPHA
-
-/* This is for compression type. PNG 1.0-1.2 only define the single type. */
-#define PNG_COMPRESSION_TYPE_BASE 0 /* Deflate method 8, 32K window */
-#define PNG_COMPRESSION_TYPE_DEFAULT PNG_COMPRESSION_TYPE_BASE
-
-/* This is for filter type. PNG 1.0-1.2 only define the single type. */
-#define PNG_FILTER_TYPE_BASE      0 /* Single row per-byte filtering */
-#define PNG_INTRAPIXEL_DIFFERENCING 64 /* Used only in MNG datastreams */
-#define PNG_FILTER_TYPE_DEFAULT   PNG_FILTER_TYPE_BASE
-
-/* These are for the interlacing type.  These values should NOT be changed. */
-#define PNG_INTERLACE_NONE        0 /* Non-interlaced image */
-#define PNG_INTERLACE_ADAM7       1 /* Adam7 interlacing */
-#define PNG_INTERLACE_LAST        2 /* Not a valid value */
-
-/* These are for the oFFs chunk.  These values should NOT be changed. */
-#define PNG_OFFSET_PIXEL          0 /* Offset in pixels */
-#define PNG_OFFSET_MICROMETER     1 /* Offset in micrometers (1/10^6 meter) */
-#define PNG_OFFSET_LAST           2 /* Not a valid value */
-
-/* These are for the pCAL chunk.  These values should NOT be changed. */
-#define PNG_EQUATION_LINEAR       0 /* Linear transformation */
-#define PNG_EQUATION_BASE_E       1 /* Exponential base e transform */
-#define PNG_EQUATION_ARBITRARY    2 /* Arbitrary base exponential transform */
-#define PNG_EQUATION_HYPERBOLIC   3 /* Hyperbolic sine transformation */
-#define PNG_EQUATION_LAST         4 /* Not a valid value */
-
-/* These are for the sCAL chunk.  These values should NOT be changed. */
-#define PNG_SCALE_UNKNOWN         0 /* unknown unit (image scale) */
-#define PNG_SCALE_METER           1 /* meters per pixel */
-#define PNG_SCALE_RADIAN          2 /* radians per pixel */
-#define PNG_SCALE_LAST            3 /* Not a valid value */
-
-/* These are for the pHYs chunk.  These values should NOT be changed. */
-#define PNG_RESOLUTION_UNKNOWN    0 /* pixels/unknown unit (aspect ratio) */
-#define PNG_RESOLUTION_METER      1 /* pixels/meter */
-#define PNG_RESOLUTION_LAST       2 /* Not a valid value */
-
-/* These are for the sRGB chunk.  These values should NOT be changed. */
-#define PNG_sRGB_INTENT_PERCEPTUAL 0
-#define PNG_sRGB_INTENT_RELATIVE   1
-#define PNG_sRGB_INTENT_SATURATION 2
-#define PNG_sRGB_INTENT_ABSOLUTE   3
-#define PNG_sRGB_INTENT_LAST       4 /* Not a valid value */
-
-/* This is for text chunks */
-#define PNG_KEYWORD_MAX_LENGTH     79
-
-/* Maximum number of entries in PLTE/sPLT/tRNS arrays */
-#define PNG_MAX_PALETTE_LENGTH    256
-
-/* These determine if an ancillary chunk's data has been successfully read
- * from the PNG header, or if the application has filled in the corresponding
- * data in the info_struct to be written into the output file.  The values
- * of the PNG_INFO_<chunk> defines should NOT be changed.
- */
-#define PNG_INFO_gAMA 0x0001
-#define PNG_INFO_sBIT 0x0002
-#define PNG_INFO_cHRM 0x0004
-#define PNG_INFO_PLTE 0x0008
-#define PNG_INFO_tRNS 0x0010
-#define PNG_INFO_bKGD 0x0020
-#define PNG_INFO_hIST 0x0040
-#define PNG_INFO_pHYs 0x0080
-#define PNG_INFO_oFFs 0x0100
-#define PNG_INFO_tIME 0x0200
-#define PNG_INFO_pCAL 0x0400
-#define PNG_INFO_sRGB 0x0800   /* GR-P, 0.96a */
-#define PNG_INFO_iCCP 0x1000   /* ESR, 1.0.6 */
-#define PNG_INFO_sPLT 0x2000   /* ESR, 1.0.6 */
-#define PNG_INFO_sCAL 0x4000   /* ESR, 1.0.6 */
-#define PNG_INFO_IDAT 0x8000L  /* ESR, 1.0.6 */
-
-/* This is used for the transformation routines, as some of them
- * change these values for the row.  It also should enable using
- * the routines for other purposes.
- */
-typedef struct png_row_info_struct
-{
-   png_uint_32 width; /* width of row */
-   png_uint_32 rowbytes; /* number of bytes in row */
-   png_byte color_type; /* color type of row */
-   png_byte bit_depth; /* bit depth of row */
-   png_byte channels; /* number of channels (1, 2, 3, or 4) */
-   png_byte pixel_depth; /* bits per pixel (depth * channels) */
-} png_row_info;
-
-typedef png_row_info FAR * png_row_infop;
-typedef png_row_info FAR * FAR * png_row_infopp;
-
-/* These are the function types for the I/O functions and for the functions
- * that allow the user to override the default I/O functions with his or her
- * own.  The png_error_ptr type should match that of user-supplied warning
- * and error functions, while the png_rw_ptr type should match that of the
- * user read/write data functions.
- */
-typedef struct png_struct_def png_struct;
-typedef png_struct FAR * png_structp;
-
-typedef void (PNGAPI *png_error_ptr) PNGARG((png_structp, png_const_charp));
-typedef void (PNGAPI *png_rw_ptr) PNGARG((png_structp, png_bytep, png_size_t));
-typedef void (PNGAPI *png_flush_ptr) PNGARG((png_structp));
-typedef void (PNGAPI *png_read_status_ptr) PNGARG((png_structp, png_uint_32,
-   int));
-typedef void (PNGAPI *png_write_status_ptr) PNGARG((png_structp, png_uint_32,
-   int));
-
-#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
-typedef void (PNGAPI *png_progressive_info_ptr) PNGARG((png_structp, png_infop));
-typedef void (PNGAPI *png_progressive_end_ptr) PNGARG((png_structp, png_infop));
-typedef void (PNGAPI *png_progressive_row_ptr) PNGARG((png_structp, png_bytep,
-   png_uint_32, int));
-#endif
-
-#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \
-    defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED) || \
-    defined(PNG_LEGACY_SUPPORTED)
-typedef void (PNGAPI *png_user_transform_ptr) PNGARG((png_structp,
-    png_row_infop, png_bytep));
-#endif
-
-#if defined(PNG_USER_CHUNKS_SUPPORTED)
-typedef int (PNGAPI *png_user_chunk_ptr) PNGARG((png_structp, png_unknown_chunkp));
-#endif
-#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
-typedef void (PNGAPI *png_unknown_chunk_ptr) PNGARG((png_structp));
-#endif
-
-/* Transform masks for the high-level interface */
-#define PNG_TRANSFORM_IDENTITY       0x0000    /* read and write */
-#define PNG_TRANSFORM_STRIP_16       0x0001    /* read only */
-#define PNG_TRANSFORM_STRIP_ALPHA    0x0002    /* read only */
-#define PNG_TRANSFORM_PACKING        0x0004    /* read and write */
-#define PNG_TRANSFORM_PACKSWAP       0x0008    /* read and write */
-#define PNG_TRANSFORM_EXPAND         0x0010    /* read only */
-#define PNG_TRANSFORM_INVERT_MONO    0x0020    /* read and write */
-#define PNG_TRANSFORM_SHIFT          0x0040    /* read and write */
-#define PNG_TRANSFORM_BGR            0x0080    /* read and write */
-#define PNG_TRANSFORM_SWAP_ALPHA     0x0100    /* read and write */
-#define PNG_TRANSFORM_SWAP_ENDIAN    0x0200    /* read and write */
-#define PNG_TRANSFORM_INVERT_ALPHA   0x0400    /* read and write */
-#define PNG_TRANSFORM_STRIP_FILLER   0x0800    /* WRITE only */
-
-/* Flags for MNG supported features */
-#define PNG_FLAG_MNG_EMPTY_PLTE     0x01
-#define PNG_FLAG_MNG_FILTER_64      0x04
-#define PNG_ALL_MNG_FEATURES        0x05
-
-typedef png_voidp (*png_malloc_ptr) PNGARG((png_structp, png_size_t));
-typedef void (*png_free_ptr) PNGARG((png_structp, png_voidp));
-
-/* The structure that holds the information to read and write PNG files.
- * The only people who need to care about what is inside of this are the
- * people who will be modifying the library for their own special needs.
- * It should NOT be accessed directly by an application, except to store
- * the jmp_buf.
- */
-
-struct png_struct_def
-{
-#ifdef PNG_SETJMP_SUPPORTED
-   jmp_buf jmpbuf;            /* used in png_error */
-#endif
-   png_error_ptr error_fn;    /* function for printing errors and aborting */
-   png_error_ptr warning_fn;  /* function for printing warnings */
-   png_voidp error_ptr;       /* user supplied struct for error functions */
-   png_rw_ptr write_data_fn;  /* function for writing output data */
-   png_rw_ptr read_data_fn;   /* function for reading input data */
-   png_voidp io_ptr;          /* ptr to application struct for I/O functions */
-
-#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
-   png_user_transform_ptr read_user_transform_fn; /* user read transform */
-#endif
-
-#if defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
-   png_user_transform_ptr write_user_transform_fn; /* user write transform */
-#endif
-
-/* These were added in libpng-1.0.2 */
-#if defined(PNG_USER_TRANSFORM_PTR_SUPPORTED)
-#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \
-    defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
-   png_voidp user_transform_ptr; /* user supplied struct for user transform */
-   png_byte user_transform_depth;    /* bit depth of user transformed pixels */
-   png_byte user_transform_channels; /* channels in user transformed pixels */
-#endif
-#endif
-
-   png_uint_32 mode;          /* tells us where we are in the PNG file */
-   png_uint_32 flags;         /* flags indicating various things to libpng */
-   png_uint_32 transformations; /* which transformations to perform */
-
-   z_stream zstream;          /* pointer to decompression structure (below) */
-   png_bytep zbuf;            /* buffer for zlib */
-   png_size_t zbuf_size;      /* size of zbuf */
-   int zlib_level;            /* holds zlib compression level */
-   int zlib_method;           /* holds zlib compression method */
-   int zlib_window_bits;      /* holds zlib compression window bits */
-   int zlib_mem_level;        /* holds zlib compression memory level */
-   int zlib_strategy;         /* holds zlib compression strategy */
-
-   png_uint_32 width;         /* width of image in pixels */
-   png_uint_32 height;        /* height of image in pixels */
-   png_uint_32 num_rows;      /* number of rows in current pass */
-   png_uint_32 usr_width;     /* width of row at start of write */
-   png_uint_32 rowbytes;      /* size of row in bytes */
-   png_uint_32 irowbytes;     /* size of current interlaced row in bytes */
-   png_uint_32 iwidth;        /* width of current interlaced row in pixels */
-   png_uint_32 row_number;    /* current row in interlace pass */
-   png_bytep prev_row;        /* buffer to save previous (unfiltered) row */
-   png_bytep row_buf;         /* buffer to save current (unfiltered) row */
-#ifndef PNG_NO_WRITE_FILTERING
-   png_bytep sub_row;         /* buffer to save "sub" row when filtering */
-   png_bytep up_row;          /* buffer to save "up" row when filtering */
-   png_bytep avg_row;         /* buffer to save "avg" row when filtering */
-   png_bytep paeth_row;       /* buffer to save "Paeth" row when filtering */
-#endif
-   png_row_info row_info;     /* used for transformation routines */
-
-   png_uint_32 idat_size;     /* current IDAT size for read */
-   png_uint_32 crc;           /* current chunk CRC value */
-   png_colorp palette;        /* palette from the input file */
-   png_uint_16 num_palette;   /* number of color entries in palette */
-   png_uint_16 num_trans;     /* number of transparency values */
-   png_byte chunk_name[5];    /* null-terminated name of current chunk */
-   png_byte compression;      /* file compression type (always 0) */
-   png_byte filter;           /* file filter type (always 0) */
-   png_byte interlaced;       /* PNG_INTERLACE_NONE, PNG_INTERLACE_ADAM7 */
-   png_byte pass;             /* current interlace pass (0 - 6) */
-   png_byte do_filter;        /* row filter flags (see PNG_FILTER_ below ) */
-   png_byte color_type;       /* color type of file */
-   png_byte bit_depth;        /* bit depth of file */
-   png_byte usr_bit_depth;    /* bit depth of users row */
-   png_byte pixel_depth;      /* number of bits per pixel */
-   png_byte channels;         /* number of channels in file */
-   png_byte usr_channels;     /* channels at start of write */
-   png_byte sig_bytes;        /* magic bytes read/written from start of file */
-
-#if defined(PNG_READ_FILLER_SUPPORTED) || defined(PNG_WRITE_FILLER_SUPPORTED)
-#ifdef PNG_LEGACY_SUPPORTED
-   png_byte filler;           /* filler byte for pixel expansion */
-#else
-   png_uint_16 filler;           /* filler bytes for pixel expansion */
-#endif
-#endif
-
-#if defined(PNG_bKGD_SUPPORTED)
-   png_byte background_gamma_type;
-#  ifdef PNG_FLOATING_POINT_SUPPORTED
-   float background_gamma;
-#  endif
-   png_color_16 background;   /* background color in screen gamma space */
-#if defined(PNG_READ_GAMMA_SUPPORTED)
-   png_color_16 background_1; /* background normalized to gamma 1.0 */
-#endif
-#endif /* PNG_bKGD_SUPPORTED */
-
-#if defined(PNG_WRITE_FLUSH_SUPPORTED)
-   png_flush_ptr output_flush_fn;/* Function for flushing output */
-   png_uint_32 flush_dist;    /* how many rows apart to flush, 0 - no flush */
-   png_uint_32 flush_rows;    /* number of rows written since last flush */
-#endif
-
-#if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
-   int gamma_shift;      /* number of "insignificant" bits 16-bit gamma */
-#ifdef PNG_FLOATING_POINT_SUPPORTED
-   float gamma;          /* file gamma value */
-   float screen_gamma;   /* screen gamma value (display_exponent) */
-#endif
-#endif
-
-#if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
-   png_bytep gamma_table;     /* gamma table for 8-bit depth files */
-   png_bytep gamma_from_1;    /* converts from 1.0 to screen */
-   png_bytep gamma_to_1;      /* converts from file to 1.0 */
-   png_uint_16pp gamma_16_table; /* gamma table for 16-bit depth files */
-   png_uint_16pp gamma_16_from_1; /* converts from 1.0 to screen */
-   png_uint_16pp gamma_16_to_1; /* converts from file to 1.0 */
-#endif
-
-#if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_sBIT_SUPPORTED)
-   png_color_8 sig_bit;       /* significant bits in each available channel */
-#endif
-
-#if defined(PNG_READ_SHIFT_SUPPORTED) || defined(PNG_WRITE_SHIFT_SUPPORTED)
-   png_color_8 shift;         /* shift for significant bit tranformation */
-#endif
-
-#if defined(PNG_tRNS_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED) \
- || defined(PNG_READ_EXPAND_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
-   png_bytep trans;           /* transparency values for paletted files */
-   png_color_16 trans_values; /* transparency values for non-paletted files */
-#endif
-
-   png_read_status_ptr read_row_fn;   /* called after each row is decoded */
-   png_write_status_ptr write_row_fn; /* called after each row is encoded */
-#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
-   png_progressive_info_ptr info_fn; /* called after header data fully read */
-   png_progressive_row_ptr row_fn;   /* called after each prog. row is decoded */
-   png_progressive_end_ptr end_fn;   /* called after image is complete */
-   png_bytep save_buffer_ptr;        /* current location in save_buffer */
-   png_bytep save_buffer;            /* buffer for previously read data */
-   png_bytep current_buffer_ptr;     /* current location in current_buffer */
-   png_bytep current_buffer;         /* buffer for recently used data */
-   png_uint_32 push_length;          /* size of current input chunk */
-   png_uint_32 skip_length;          /* bytes to skip in input data */
-   png_size_t save_buffer_size;      /* amount of data now in save_buffer */
-   png_size_t save_buffer_max;       /* total size of save_buffer */
-   png_size_t buffer_size;           /* total amount of available input data */
-   png_size_t current_buffer_size;   /* amount of data now in current_buffer */
-   int process_mode;                 /* what push library is currently doing */
-   int cur_palette;                  /* current push library palette index */
-
-#  if defined(PNG_TEXT_SUPPORTED)
-     png_size_t current_text_size;   /* current size of text input data */
-     png_size_t current_text_left;   /* how much text left to read in input */
-     png_charp current_text;         /* current text chunk buffer */
-     png_charp current_text_ptr;     /* current location in current_text */
-#  endif /* PNG_TEXT_SUPPORTED */
-#endif /* PNG_PROGRESSIVE_READ_SUPPORTED */
-
-#if defined(__TURBOC__) && !defined(_Windows) && !defined(__FLAT__)
-/* for the Borland special 64K segment handler */
-   png_bytepp offset_table_ptr;
-   png_bytep offset_table;
-   png_uint_16 offset_table_number;
-   png_uint_16 offset_table_count;
-   png_uint_16 offset_table_count_free;
-#endif
-
-#if defined(PNG_READ_DITHER_SUPPORTED)
-   png_bytep palette_lookup;         /* lookup table for dithering */
-   png_bytep dither_index;           /* index translation for palette files */
-#endif
-
-#if defined(PNG_READ_DITHER_SUPPORTED) || defined(PNG_hIST_SUPPORTED)
-   png_uint_16p hist;                /* histogram */
-#endif
-
-#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
-   png_byte heuristic_method;        /* heuristic for row filter selection */
-   png_byte num_prev_filters;        /* number of weights for previous rows */
-   png_bytep prev_filters;           /* filter type(s) of previous row(s) */
-   png_uint_16p filter_weights;      /* weight(s) for previous line(s) */
-   png_uint_16p inv_filter_weights;  /* 1/weight(s) for previous line(s) */
-   png_uint_16p filter_costs;        /* relative filter calculation cost */
-   png_uint_16p inv_filter_costs;    /* 1/relative filter calculation cost */
-#endif
-
-#if defined(PNG_TIME_RFC1123_SUPPORTED)
-   png_charp time_buffer;            /* String to hold RFC 1123 time text */
-#endif
-
-/* New members added in libpng-1.0.6 */
-
-#ifdef PNG_FREE_ME_SUPPORTED
-   png_uint_32 free_me;       /* flags items libpng is responsible for freeing */
-#endif
-
-#if defined(PNG_USER_CHUNKS_SUPPORTED)
-   png_voidp user_chunk_ptr;
-   png_user_chunk_ptr read_user_chunk_fn; /* user read chunk handler */
-#endif
-
-#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
-   int num_chunk_list;
-   png_bytep chunk_list;
-#endif
-
-/* New members added in libpng-1.0.3 */
-#if defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
-   png_byte rgb_to_gray_status;
-   /* These were changed from png_byte in libpng-1.0.6 */
-   png_uint_16 rgb_to_gray_red_coeff;
-   png_uint_16 rgb_to_gray_green_coeff;
-   png_uint_16 rgb_to_gray_blue_coeff;
-#endif
-
-/* New member added in libpng-1.0.4 (renamed in 1.0.9) */
-#if defined(PNG_MNG_FEATURES_SUPPORTED) || \
-    defined(PNG_READ_EMPTY_PLTE_SUPPORTED) || \
-    defined(PNG_WRITE_EMPTY_PLTE_SUPPORTED)
-/* changed from png_byte to png_uint_32 at version 1.2.0 */
-#ifdef PNG_1_0_X
-   png_byte mng_features_permitted;
-#else
-   png_uint_32 mng_features_permitted;
-#endif /* PNG_1_0_X */
-#endif
-
-/* New member added in libpng-1.0.7 */
-#if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
-   png_fixed_point int_gamma;
-#endif
-
-/* New member added in libpng-1.0.9, ifdef'ed out in 1.0.12, enabled in 1.2.0 */
-#if defined(PNG_MNG_FEATURES_SUPPORTED)
-   png_byte filter_type;
-#endif
-
-#if defined(PNG_1_0_X)
-/* New member added in libpng-1.0.10, ifdef'ed out in 1.2.0 */
-   png_uint_32 row_buf_size;
-#endif
-
-/* New members added in libpng-1.2.0 */
-#if defined(PNG_ASSEMBLER_CODE_SUPPORTED)
-#  if !defined(PNG_1_0_X)
-#    if defined(PNG_MMX_CODE_SUPPORTED)
-   png_byte     mmx_bitdepth_threshold;
-   png_uint_32  mmx_rowbytes_threshold;
-#    endif
-   png_uint_32  asm_flags;
-#  endif
-#endif
-
-/* New members added in libpng-1.0.2 but first enabled by default in 1.2.0 */
-#ifdef PNG_USER_MEM_SUPPORTED
-   png_voidp mem_ptr;                /* user supplied struct for mem functions */
-   png_malloc_ptr malloc_fn;         /* function for allocating memory */
-   png_free_ptr free_fn;             /* function for freeing memory */
-#endif
-
-/* New member added in libpng-1.0.13 and 1.2.0 */
-   png_bytep big_row_buf;         /* buffer to save current (unfiltered) row */
-
-#if defined(PNG_READ_DITHER_SUPPORTED)
-/* The following three members were added at version 1.0.14 and 1.2.4 */
-   png_bytep dither_sort;            /* working sort array */
-   png_bytep index_to_palette;       /* where the original index currently is */
-                                     /* in the palette */
-   png_bytep palette_to_index;       /* which original index points to this */
-                                     /* palette color */
-#endif
-
-/* New members added in libpng-1.0.16 and 1.2.6 */
-   png_byte compression_type;
-
-#ifdef PNG_SET_USER_LIMITS_SUPPORTED
-   png_uint_32 user_width_max;
-   png_uint_32 user_height_max;
-#endif
-
-/* New member added in libpng-1.0.25 and 1.2.17 */
-#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
-   /* storage for unknown chunk that the library doesn't recognize. */
-   png_unknown_chunk unknown_chunk;
-#endif
-
-/* New members added in libpng-1.2.26 */
-  png_uint_32 old_big_row_buf_size, old_prev_row_size;
-};
-
-
-/* This triggers a compiler error in png.c, if png.c and png.h
- * do not agree upon the version number.
- */
-typedef png_structp version_1_2_29;
-
-typedef png_struct FAR * FAR * png_structpp;
-
-/* Here are the function definitions most commonly used.  This is not
- * the place to find out how to use libpng.  See libpng.txt for the
- * full explanation, see example.c for the summary.  This just provides
- * a simple one line description of the use of each function.
- */
-
-/* Returns the version number of the library */
-extern PNG_EXPORT(png_uint_32,png_access_version_number) PNGARG((void));
-
-/* Tell lib we have already handled the first <num_bytes> magic bytes.
- * Handling more than 8 bytes from the beginning of the file is an error.
- */
-extern PNG_EXPORT(void,png_set_sig_bytes) PNGARG((png_structp png_ptr,
-   int num_bytes));
-
-/* Check sig[start] through sig[start + num_to_check - 1] to see if it's a
- * PNG file.  Returns zero if the supplied bytes match the 8-byte PNG
- * signature, and non-zero otherwise.  Having num_to_check == 0 or
- * start > 7 will always fail (ie return non-zero).
- */
-extern PNG_EXPORT(int,png_sig_cmp) PNGARG((png_bytep sig, png_size_t start,
-   png_size_t num_to_check));
-
-/* Simple signature checking function.  This is the same as calling
- * png_check_sig(sig, n) := !png_sig_cmp(sig, 0, n).
- */
-extern PNG_EXPORT(int,png_check_sig) PNGARG((png_bytep sig, int num));
-
-/* Allocate and initialize png_ptr struct for reading, and any other memory. */
-extern PNG_EXPORT(png_structp,png_create_read_struct)
-   PNGARG((png_const_charp user_png_ver, png_voidp error_ptr,
-   png_error_ptr error_fn, png_error_ptr warn_fn));
-
-/* Allocate and initialize png_ptr struct for writing, and any other memory */
-extern PNG_EXPORT(png_structp,png_create_write_struct)
-   PNGARG((png_const_charp user_png_ver, png_voidp error_ptr,
-   png_error_ptr error_fn, png_error_ptr warn_fn));
-
-#ifdef PNG_WRITE_SUPPORTED
-extern PNG_EXPORT(png_uint_32,png_get_compression_buffer_size)
-   PNGARG((png_structp png_ptr));
-#endif
-
-#ifdef PNG_WRITE_SUPPORTED
-extern PNG_EXPORT(void,png_set_compression_buffer_size)
-   PNGARG((png_structp png_ptr, png_uint_32 size));
-#endif
-
-/* Reset the compression stream */
-extern PNG_EXPORT(int,png_reset_zstream) PNGARG((png_structp png_ptr));
-
-/* New functions added in libpng-1.0.2 (not enabled by default until 1.2.0) */
-#ifdef PNG_USER_MEM_SUPPORTED
-extern PNG_EXPORT(png_structp,png_create_read_struct_2)
-   PNGARG((png_const_charp user_png_ver, png_voidp error_ptr,
-   png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
-   png_malloc_ptr malloc_fn, png_free_ptr free_fn));
-extern PNG_EXPORT(png_structp,png_create_write_struct_2)
-   PNGARG((png_const_charp user_png_ver, png_voidp error_ptr,
-   png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
-   png_malloc_ptr malloc_fn, png_free_ptr free_fn));
-#endif
-
-/* Write a PNG chunk - size, type, (optional) data, CRC. */
-extern PNG_EXPORT(void,png_write_chunk) PNGARG((png_structp png_ptr,
-   png_bytep chunk_name, png_bytep data, png_size_t length));
-
-/* Write the start of a PNG chunk - length and chunk name. */
-extern PNG_EXPORT(void,png_write_chunk_start) PNGARG((png_structp png_ptr,
-   png_bytep chunk_name, png_uint_32 length));
-
-/* Write the data of a PNG chunk started with png_write_chunk_start(). */
-extern PNG_EXPORT(void,png_write_chunk_data) PNGARG((png_structp png_ptr,
-   png_bytep data, png_size_t length));
-
-/* Finish a chunk started with png_write_chunk_start() (includes CRC). */
-extern PNG_EXPORT(void,png_write_chunk_end) PNGARG((png_structp png_ptr));
-
-/* Allocate and initialize the info structure */
-extern PNG_EXPORT(png_infop,png_create_info_struct)
-   PNGARG((png_structp png_ptr));
-
-#if defined(PNG_1_0_X) || defined (PNG_1_2_X)
-/* Initialize the info structure (old interface - DEPRECATED) */
-extern PNG_EXPORT(void,png_info_init) PNGARG((png_infop info_ptr));
-#undef png_info_init
-#define png_info_init(info_ptr) png_info_init_3(&info_ptr,\
-    png_sizeof(png_info));
-#endif
-
-extern PNG_EXPORT(void,png_info_init_3) PNGARG((png_infopp info_ptr,
-    png_size_t png_info_struct_size));
-
-/* Writes all the PNG information before the image. */
-extern PNG_EXPORT(void,png_write_info_before_PLTE) PNGARG((png_structp png_ptr,
-   png_infop info_ptr));
-extern PNG_EXPORT(void,png_write_info) PNGARG((png_structp png_ptr,
-   png_infop info_ptr));
-
-#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
-/* read the information before the actual image data. */
-extern PNG_EXPORT(void,png_read_info) PNGARG((png_structp png_ptr,
-   png_infop info_ptr));
-#endif
-
-#if defined(PNG_TIME_RFC1123_SUPPORTED)
-extern PNG_EXPORT(png_charp,png_convert_to_rfc1123)
-   PNGARG((png_structp png_ptr, png_timep ptime));
-#endif
-
-#if !defined(_WIN32_WCE)
-/* "time.h" functions are not supported on WindowsCE */
-#if defined(PNG_WRITE_tIME_SUPPORTED)
-/* convert from a struct tm to png_time */
-extern PNG_EXPORT(void,png_convert_from_struct_tm) PNGARG((png_timep ptime,
-   struct tm FAR * ttime));
-
-/* convert from time_t to png_time.  Uses gmtime() */
-extern PNG_EXPORT(void,png_convert_from_time_t) PNGARG((png_timep ptime,
-   time_t ttime));
-#endif /* PNG_WRITE_tIME_SUPPORTED */
-#endif /* _WIN32_WCE */
-
-#if defined(PNG_READ_EXPAND_SUPPORTED)
-/* Expand data to 24-bit RGB, or 8-bit grayscale, with alpha if available. */
-extern PNG_EXPORT(void,png_set_expand) PNGARG((png_structp png_ptr));
-#if !defined(PNG_1_0_X)
-extern PNG_EXPORT(void,png_set_expand_gray_1_2_4_to_8) PNGARG((png_structp
-  png_ptr));
-#endif
-extern PNG_EXPORT(void,png_set_palette_to_rgb) PNGARG((png_structp png_ptr));
-extern PNG_EXPORT(void,png_set_tRNS_to_alpha) PNGARG((png_structp png_ptr));
-#if defined(PNG_1_0_X) || defined (PNG_1_2_X)
-/* Deprecated */
-extern PNG_EXPORT(void,png_set_gray_1_2_4_to_8) PNGARG((png_structp png_ptr));
-#endif
-#endif
-
-#if defined(PNG_READ_BGR_SUPPORTED) || defined(PNG_WRITE_BGR_SUPPORTED)
-/* Use blue, green, red order for pixels. */
-extern PNG_EXPORT(void,png_set_bgr) PNGARG((png_structp png_ptr));
-#endif
-
-#if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED)
-/* Expand the grayscale to 24-bit RGB if necessary. */
-extern PNG_EXPORT(void,png_set_gray_to_rgb) PNGARG((png_structp png_ptr));
-#endif
-
-#if defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
-/* Reduce RGB to grayscale. */
-#ifdef PNG_FLOATING_POINT_SUPPORTED
-extern PNG_EXPORT(void,png_set_rgb_to_gray) PNGARG((png_structp png_ptr,
-   int error_action, double red, double green ));
-#endif
-extern PNG_EXPORT(void,png_set_rgb_to_gray_fixed) PNGARG((png_structp png_ptr,
-   int error_action, png_fixed_point red, png_fixed_point green ));
-extern PNG_EXPORT(png_byte,png_get_rgb_to_gray_status) PNGARG((png_structp
-   png_ptr));
-#endif
-
-extern PNG_EXPORT(void,png_build_grayscale_palette) PNGARG((int bit_depth,
-   png_colorp palette));
-
-#if defined(PNG_READ_STRIP_ALPHA_SUPPORTED)
-extern PNG_EXPORT(void,png_set_strip_alpha) PNGARG((png_structp png_ptr));
-#endif
-
-#if defined(PNG_READ_SWAP_ALPHA_SUPPORTED) || \
-    defined(PNG_WRITE_SWAP_ALPHA_SUPPORTED)
-extern PNG_EXPORT(void,png_set_swap_alpha) PNGARG((png_structp png_ptr));
-#endif
-
-#if defined(PNG_READ_INVERT_ALPHA_SUPPORTED) || \
-    defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED)
-extern PNG_EXPORT(void,png_set_invert_alpha) PNGARG((png_structp png_ptr));
-#endif
-
-#if defined(PNG_READ_FILLER_SUPPORTED) || defined(PNG_WRITE_FILLER_SUPPORTED)
-/* Add a filler byte to 8-bit Gray or 24-bit RGB images. */
-extern PNG_EXPORT(void,png_set_filler) PNGARG((png_structp png_ptr,
-   png_uint_32 filler, int flags));
-/* The values of the PNG_FILLER_ defines should NOT be changed */
-#define PNG_FILLER_BEFORE 0
-#define PNG_FILLER_AFTER 1
-/* Add an alpha byte to 8-bit Gray or 24-bit RGB images. */
-#if !defined(PNG_1_0_X)
-extern PNG_EXPORT(void,png_set_add_alpha) PNGARG((png_structp png_ptr,
-   png_uint_32 filler, int flags));
-#endif
-#endif /* PNG_READ_FILLER_SUPPORTED || PNG_WRITE_FILLER_SUPPORTED */
-
-#if defined(PNG_READ_SWAP_SUPPORTED) || defined(PNG_WRITE_SWAP_SUPPORTED)
-/* Swap bytes in 16-bit depth files. */
-extern PNG_EXPORT(void,png_set_swap) PNGARG((png_structp png_ptr));
-#endif
-
-#if defined(PNG_READ_PACK_SUPPORTED) || defined(PNG_WRITE_PACK_SUPPORTED)
-/* Use 1 byte per pixel in 1, 2, or 4-bit depth files. */
-extern PNG_EXPORT(void,png_set_packing) PNGARG((png_structp png_ptr));
-#endif
-
-#if defined(PNG_READ_PACKSWAP_SUPPORTED) || defined(PNG_WRITE_PACKSWAP_SUPPORTED)
-/* Swap packing order of pixels in bytes. */
-extern PNG_EXPORT(void,png_set_packswap) PNGARG((png_structp png_ptr));
-#endif
-
-#if defined(PNG_READ_SHIFT_SUPPORTED) || defined(PNG_WRITE_SHIFT_SUPPORTED)
-/* Converts files to legal bit depths. */
-extern PNG_EXPORT(void,png_set_shift) PNGARG((png_structp png_ptr,
-   png_color_8p true_bits));
-#endif
-
-#if defined(PNG_READ_INTERLACING_SUPPORTED) || \
-    defined(PNG_WRITE_INTERLACING_SUPPORTED)
-/* Have the code handle the interlacing.  Returns the number of passes. */
-extern PNG_EXPORT(int,png_set_interlace_handling) PNGARG((png_structp png_ptr));
-#endif
-
-#if defined(PNG_READ_INVERT_SUPPORTED) || defined(PNG_WRITE_INVERT_SUPPORTED)
-/* Invert monochrome files */
-extern PNG_EXPORT(void,png_set_invert_mono) PNGARG((png_structp png_ptr));
-#endif
-
-#if defined(PNG_READ_BACKGROUND_SUPPORTED)
-/* Handle alpha and tRNS by replacing with a background color. */
-#ifdef PNG_FLOATING_POINT_SUPPORTED
-extern PNG_EXPORT(void,png_set_background) PNGARG((png_structp png_ptr,
-   png_color_16p background_color, int background_gamma_code,
-   int need_expand, double background_gamma));
-#endif
-#define PNG_BACKGROUND_GAMMA_UNKNOWN 0
-#define PNG_BACKGROUND_GAMMA_SCREEN  1
-#define PNG_BACKGROUND_GAMMA_FILE    2
-#define PNG_BACKGROUND_GAMMA_UNIQUE  3
-#endif
-
-#if defined(PNG_READ_16_TO_8_SUPPORTED)
-/* strip the second byte of information from a 16-bit depth file. */
-extern PNG_EXPORT(void,png_set_strip_16) PNGARG((png_structp png_ptr));
-#endif
-
-#if defined(PNG_READ_DITHER_SUPPORTED)
-/* Turn on dithering, and reduce the palette to the number of colors available. */
-extern PNG_EXPORT(void,png_set_dither) PNGARG((png_structp png_ptr,
-   png_colorp palette, int num_palette, int maximum_colors,
-   png_uint_16p histogram, int full_dither));
-#endif
-
-#if defined(PNG_READ_GAMMA_SUPPORTED)
-/* Handle gamma correction. Screen_gamma=(display_exponent) */
-#ifdef PNG_FLOATING_POINT_SUPPORTED
-extern PNG_EXPORT(void,png_set_gamma) PNGARG((png_structp png_ptr,
-   double screen_gamma, double default_file_gamma));
-#endif
-#endif
-
-#if defined(PNG_1_0_X) || defined (PNG_1_2_X)
-#if defined(PNG_READ_EMPTY_PLTE_SUPPORTED) || \
-    defined(PNG_WRITE_EMPTY_PLTE_SUPPORTED)
-/* Permit or disallow empty PLTE (0: not permitted, 1: permitted) */
-/* Deprecated and will be removed.  Use png_permit_mng_features() instead. */
-extern PNG_EXPORT(void,png_permit_empty_plte) PNGARG((png_structp png_ptr,
-   int empty_plte_permitted));
-#endif
-#endif
-
-#if defined(PNG_WRITE_FLUSH_SUPPORTED)
-/* Set how many lines between output flushes - 0 for no flushing */
-extern PNG_EXPORT(void,png_set_flush) PNGARG((png_structp png_ptr, int nrows));
-/* Flush the current PNG output buffer */
-extern PNG_EXPORT(void,png_write_flush) PNGARG((png_structp png_ptr));
-#endif
-
-/* optional update palette with requested transformations */
-extern PNG_EXPORT(void,png_start_read_image) PNGARG((png_structp png_ptr));
-
-/* optional call to update the users info structure */
-extern PNG_EXPORT(void,png_read_update_info) PNGARG((png_structp png_ptr,
-   png_infop info_ptr));
-
-#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
-/* read one or more rows of image data. */
-extern PNG_EXPORT(void,png_read_rows) PNGARG((png_structp png_ptr,
-   png_bytepp row, png_bytepp display_row, png_uint_32 num_rows));
-#endif
-
-#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
-/* read a row of data. */
-extern PNG_EXPORT(void,png_read_row) PNGARG((png_structp png_ptr,
-   png_bytep row,
-   png_bytep display_row));
-#endif
-
-#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
-/* read the whole image into memory at once. */
-extern PNG_EXPORT(void,png_read_image) PNGARG((png_structp png_ptr,
-   png_bytepp image));
-#endif
-
-/* write a row of image data */
-extern PNG_EXPORT(void,png_write_row) PNGARG((png_structp png_ptr,
-   png_bytep row));
-
-/* write a few rows of image data */
-extern PNG_EXPORT(void,png_write_rows) PNGARG((png_structp png_ptr,
-   png_bytepp row, png_uint_32 num_rows));
-
-/* write the image data */
-extern PNG_EXPORT(void,png_write_image) PNGARG((png_structp png_ptr,
-   png_bytepp image));
-
-/* writes the end of the PNG file. */
-extern PNG_EXPORT(void,png_write_end) PNGARG((png_structp png_ptr,
-   png_infop info_ptr));
-
-#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
-/* read the end of the PNG file. */
-extern PNG_EXPORT(void,png_read_end) PNGARG((png_structp png_ptr,
-   png_infop info_ptr));
-#endif
-
-/* free any memory associated with the png_info_struct */
-extern PNG_EXPORT(void,png_destroy_info_struct) PNGARG((png_structp png_ptr,
-   png_infopp info_ptr_ptr));
-
-/* free any memory associated with the png_struct and the png_info_structs */
-extern PNG_EXPORT(void,png_destroy_read_struct) PNGARG((png_structpp
-   png_ptr_ptr, png_infopp info_ptr_ptr, png_infopp end_info_ptr_ptr));
-
-/* free all memory used by the read (old method - NOT DLL EXPORTED) */
-extern void png_read_destroy PNGARG((png_structp png_ptr, png_infop info_ptr,
-   png_infop end_info_ptr));
-
-/* free any memory associated with the png_struct and the png_info_structs */
-extern PNG_EXPORT(void,png_destroy_write_struct)
-   PNGARG((png_structpp png_ptr_ptr, png_infopp info_ptr_ptr));
-
-/* free any memory used in png_ptr struct (old method - NOT DLL EXPORTED) */
-extern void png_write_destroy PNGARG((png_structp png_ptr));
-
-/* set the libpng method of handling chunk CRC errors */
-extern PNG_EXPORT(void,png_set_crc_action) PNGARG((png_structp png_ptr,
-   int crit_action, int ancil_action));
-
-/* Values for png_set_crc_action() to say how to handle CRC errors in
- * ancillary and critical chunks, and whether to use the data contained
- * therein.  Note that it is impossible to "discard" data in a critical
- * chunk.  For versions prior to 0.90, the action was always error/quit,
- * whereas in version 0.90 and later, the action for CRC errors in ancillary
- * chunks is warn/discard.  These values should NOT be changed.
- *
- *      value                       action:critical     action:ancillary
- */
-#define PNG_CRC_DEFAULT       0  /* error/quit          warn/discard data */
-#define PNG_CRC_ERROR_QUIT    1  /* error/quit          error/quit        */
-#define PNG_CRC_WARN_DISCARD  2  /* (INVALID)           warn/discard data */
-#define PNG_CRC_WARN_USE      3  /* warn/use data       warn/use data     */
-#define PNG_CRC_QUIET_USE     4  /* quiet/use data      quiet/use data    */
-#define PNG_CRC_NO_CHANGE     5  /* use current value   use current value */
-
-/* These functions give the user control over the scan-line filtering in
- * libpng and the compression methods used by zlib.  These functions are
- * mainly useful for testing, as the defaults should work with most users.
- * Those users who are tight on memory or want faster performance at the
- * expense of compression can modify them.  See the compression library
- * header file (zlib.h) for an explination of the compression functions.
- */
-
-/* set the filtering method(s) used by libpng.  Currently, the only valid
- * value for "method" is 0.
- */
-extern PNG_EXPORT(void,png_set_filter) PNGARG((png_structp png_ptr, int method,
-   int filters));
-
-/* Flags for png_set_filter() to say which filters to use.  The flags
- * are chosen so that they don't conflict with real filter types
- * below, in case they are supplied instead of the #defined constants.
- * These values should NOT be changed.
- */
-#define PNG_NO_FILTERS     0x00
-#define PNG_FILTER_NONE    0x08
-#define PNG_FILTER_SUB     0x10
-#define PNG_FILTER_UP      0x20
-#define PNG_FILTER_AVG     0x40
-#define PNG_FILTER_PAETH   0x80
-#define PNG_ALL_FILTERS (PNG_FILTER_NONE | PNG_FILTER_SUB | PNG_FILTER_UP | \
-                         PNG_FILTER_AVG | PNG_FILTER_PAETH)
-
-/* Filter values (not flags) - used in pngwrite.c, pngwutil.c for now.
- * These defines should NOT be changed.
- */
-#define PNG_FILTER_VALUE_NONE  0
-#define PNG_FILTER_VALUE_SUB   1
-#define PNG_FILTER_VALUE_UP    2
-#define PNG_FILTER_VALUE_AVG   3
-#define PNG_FILTER_VALUE_PAETH 4
-#define PNG_FILTER_VALUE_LAST  5
-
-#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED) /* EXPERIMENTAL */
-/* The "heuristic_method" is given by one of the PNG_FILTER_HEURISTIC_
- * defines, either the default (minimum-sum-of-absolute-differences), or
- * the experimental method (weighted-minimum-sum-of-absolute-differences).
- *
- * Weights are factors >= 1.0, indicating how important it is to keep the
- * filter type consistent between rows.  Larger numbers mean the current
- * filter is that many times as likely to be the same as the "num_weights"
- * previous filters.  This is cumulative for each previous row with a weight.
- * There needs to be "num_weights" values in "filter_weights", or it can be
- * NULL if the weights aren't being specified.  Weights have no influence on
- * the selection of the first row filter.  Well chosen weights can (in theory)
- * improve the compression for a given image.
- *
- * Costs are factors >= 1.0 indicating the relative decoding costs of a
- * filter type.  Higher costs indicate more decoding expense, and are
- * therefore less likely to be selected over a filter with lower computational
- * costs.  There needs to be a value in "filter_costs" for each valid filter
- * type (given by PNG_FILTER_VALUE_LAST), or it can be NULL if you aren't
- * setting the costs.  Costs try to improve the speed of decompression without
- * unduly increasing the compressed image size.
- *
- * A negative weight or cost indicates the default value is to be used, and
- * values in the range [0.0, 1.0) indicate the value is to remain unchanged.
- * The default values for both weights and costs are currently 1.0, but may
- * change if good general weighting/cost heuristics can be found.  If both
- * the weights and costs are set to 1.0, this degenerates the WEIGHTED method
- * to the UNWEIGHTED method, but with added encoding time/computation.
- */
-#ifdef PNG_FLOATING_POINT_SUPPORTED
-extern PNG_EXPORT(void,png_set_filter_heuristics) PNGARG((png_structp png_ptr,
-   int heuristic_method, int num_weights, png_doublep filter_weights,
-   png_doublep filter_costs));
-#endif
-#endif /*  PNG_WRITE_WEIGHTED_FILTER_SUPPORTED */
-
-/* Heuristic used for row filter selection.  These defines should NOT be
- * changed.
- */
-#define PNG_FILTER_HEURISTIC_DEFAULT    0  /* Currently "UNWEIGHTED" */
-#define PNG_FILTER_HEURISTIC_UNWEIGHTED 1  /* Used by libpng < 0.95 */
-#define PNG_FILTER_HEURISTIC_WEIGHTED   2  /* Experimental feature */
-#define PNG_FILTER_HEURISTIC_LAST       3  /* Not a valid value */
-
-/* Set the library compression level.  Currently, valid values range from
- * 0 - 9, corresponding directly to the zlib compression levels 0 - 9
- * (0 - no compression, 9 - "maximal" compression).  Note that tests have
- * shown that zlib compression levels 3-6 usually perform as well as level 9
- * for PNG images, and do considerably fewer caclulations.  In the future,
- * these values may not correspond directly to the zlib compression levels.
- */
-extern PNG_EXPORT(void,png_set_compression_level) PNGARG((png_structp png_ptr,
-   int level));
-
-extern PNG_EXPORT(void,png_set_compression_mem_level)
-   PNGARG((png_structp png_ptr, int mem_level));
-
-extern PNG_EXPORT(void,png_set_compression_strategy)
-   PNGARG((png_structp png_ptr, int strategy));
-
-extern PNG_EXPORT(void,png_set_compression_window_bits)
-   PNGARG((png_structp png_ptr, int window_bits));
-
-extern PNG_EXPORT(void,png_set_compression_method) PNGARG((png_structp png_ptr,
-   int method));
-
-/* These next functions are called for input/output, memory, and error
- * handling.  They are in the file pngrio.c, pngwio.c, and pngerror.c,
- * and call standard C I/O routines such as fread(), fwrite(), and
- * fprintf().  These functions can be made to use other I/O routines
- * at run time for those applications that need to handle I/O in a
- * different manner by calling png_set_???_fn().  See libpng.txt for
- * more information.
- */
-
-#if !defined(PNG_NO_STDIO)
-/* Initialize the input/output for the PNG file to the default functions. */
-extern PNG_EXPORT(void,png_init_io) PNGARG((png_structp png_ptr, png_FILE_p fp));
-#endif
-
-/* Replace the (error and abort), and warning functions with user
- * supplied functions.  If no messages are to be printed you must still
- * write and use replacement functions. The replacement error_fn should
- * still do a longjmp to the last setjmp location if you are using this
- * method of error handling.  If error_fn or warning_fn is NULL, the
- * default function will be used.
- */
-
-extern PNG_EXPORT(void,png_set_error_fn) PNGARG((png_structp png_ptr,
-   png_voidp error_ptr, png_error_ptr error_fn, png_error_ptr warning_fn));
-
-/* Return the user pointer associated with the error functions */
-extern PNG_EXPORT(png_voidp,png_get_error_ptr) PNGARG((png_structp png_ptr));
-
-/* Replace the default data output functions with a user supplied one(s).
- * If buffered output is not used, then output_flush_fn can be set to NULL.
- * If PNG_WRITE_FLUSH_SUPPORTED is not defined at libpng compile time
- * output_flush_fn will be ignored (and thus can be NULL).
- */
-extern PNG_EXPORT(void,png_set_write_fn) PNGARG((png_structp png_ptr,
-   png_voidp io_ptr, png_rw_ptr write_data_fn, png_flush_ptr output_flush_fn));
-
-/* Replace the default data input function with a user supplied one. */
-extern PNG_EXPORT(void,png_set_read_fn) PNGARG((png_structp png_ptr,
-   png_voidp io_ptr, png_rw_ptr read_data_fn));
-
-/* Return the user pointer associated with the I/O functions */
-extern PNG_EXPORT(png_voidp,png_get_io_ptr) PNGARG((png_structp png_ptr));
-
-extern PNG_EXPORT(void,png_set_read_status_fn) PNGARG((png_structp png_ptr,
-   png_read_status_ptr read_row_fn));
-
-extern PNG_EXPORT(void,png_set_write_status_fn) PNGARG((png_structp png_ptr,
-   png_write_status_ptr write_row_fn));
-
-#ifdef PNG_USER_MEM_SUPPORTED
-/* Replace the default memory allocation functions with user supplied one(s). */
-extern PNG_EXPORT(void,png_set_mem_fn) PNGARG((png_structp png_ptr,
-   png_voidp mem_ptr, png_malloc_ptr malloc_fn, png_free_ptr free_fn));
-/* Return the user pointer associated with the memory functions */
-extern PNG_EXPORT(png_voidp,png_get_mem_ptr) PNGARG((png_structp png_ptr));
-#endif
-
-#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \
-    defined(PNG_LEGACY_SUPPORTED)
-extern PNG_EXPORT(void,png_set_read_user_transform_fn) PNGARG((png_structp
-   png_ptr, png_user_transform_ptr read_user_transform_fn));
-#endif
-
-#if defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED) || \
-    defined(PNG_LEGACY_SUPPORTED)
-extern PNG_EXPORT(void,png_set_write_user_transform_fn) PNGARG((png_structp
-   png_ptr, png_user_transform_ptr write_user_transform_fn));
-#endif
-
-#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \
-    defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED) || \
-    defined(PNG_LEGACY_SUPPORTED)
-extern PNG_EXPORT(void,png_set_user_transform_info) PNGARG((png_structp
-   png_ptr, png_voidp user_transform_ptr, int user_transform_depth,
-   int user_transform_channels));
-/* Return the user pointer associated with the user transform functions */
-extern PNG_EXPORT(png_voidp,png_get_user_transform_ptr)
-   PNGARG((png_structp png_ptr));
-#endif
-
-#ifdef PNG_USER_CHUNKS_SUPPORTED
-extern PNG_EXPORT(void,png_set_read_user_chunk_fn) PNGARG((png_structp png_ptr,
-   png_voidp user_chunk_ptr, png_user_chunk_ptr read_user_chunk_fn));
-extern PNG_EXPORT(png_voidp,png_get_user_chunk_ptr) PNGARG((png_structp
-   png_ptr));
-#endif
-
-#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
-/* Sets the function callbacks for the push reader, and a pointer to a
- * user-defined structure available to the callback functions.
- */
-extern PNG_EXPORT(void,png_set_progressive_read_fn) PNGARG((png_structp png_ptr,
-   png_voidp progressive_ptr,
-   png_progressive_info_ptr info_fn, png_progressive_row_ptr row_fn,
-   png_progressive_end_ptr end_fn));
-
-/* returns the user pointer associated with the push read functions */
-extern PNG_EXPORT(png_voidp,png_get_progressive_ptr)
-   PNGARG((png_structp png_ptr));
-
-/* function to be called when data becomes available */
-extern PNG_EXPORT(void,png_process_data) PNGARG((png_structp png_ptr,
-   png_infop info_ptr, png_bytep buffer, png_size_t buffer_size));
-
-/* function that combines rows.  Not very much different than the
- * png_combine_row() call.  Is this even used?????
- */
-extern PNG_EXPORT(void,png_progressive_combine_row) PNGARG((png_structp png_ptr,
-   png_bytep old_row, png_bytep new_row));
-#endif /* PNG_PROGRESSIVE_READ_SUPPORTED */
-
-extern PNG_EXPORT(png_voidp,png_malloc) PNGARG((png_structp png_ptr,
-   png_uint_32 size));
-
-#if defined(PNG_1_0_X)
-#  define png_malloc_warn png_malloc
-#else
-/* Added at libpng version 1.2.4 */
-extern PNG_EXPORT(png_voidp,png_malloc_warn) PNGARG((png_structp png_ptr,
-   png_uint_32 size));
-#endif
-
-/* frees a pointer allocated by png_malloc() */
-extern PNG_EXPORT(void,png_free) PNGARG((png_structp png_ptr, png_voidp ptr));
-
-#if defined(PNG_1_0_X)
-/* Function to allocate memory for zlib. */
-extern PNG_EXPORT(voidpf,png_zalloc) PNGARG((voidpf png_ptr, uInt items,
-   uInt size));
-
-/* Function to free memory for zlib */
-extern PNG_EXPORT(void,png_zfree) PNGARG((voidpf png_ptr, voidpf ptr));
-#endif
-
-/* Free data that was allocated internally */
-extern PNG_EXPORT(void,png_free_data) PNGARG((png_structp png_ptr,
-   png_infop info_ptr, png_uint_32 free_me, int num));
-#ifdef PNG_FREE_ME_SUPPORTED
-/* Reassign responsibility for freeing existing data, whether allocated
- * by libpng or by the application */
-extern PNG_EXPORT(void,png_data_freer) PNGARG((png_structp png_ptr,
-   png_infop info_ptr, int freer, png_uint_32 mask));
-#endif
-/* assignments for png_data_freer */
-#define PNG_DESTROY_WILL_FREE_DATA 1
-#define PNG_SET_WILL_FREE_DATA 1
-#define PNG_USER_WILL_FREE_DATA 2
-/* Flags for png_ptr->free_me and info_ptr->free_me */
-#define PNG_FREE_HIST 0x0008
-#define PNG_FREE_ICCP 0x0010
-#define PNG_FREE_SPLT 0x0020
-#define PNG_FREE_ROWS 0x0040
-#define PNG_FREE_PCAL 0x0080
-#define PNG_FREE_SCAL 0x0100
-#define PNG_FREE_UNKN 0x0200
-#define PNG_FREE_LIST 0x0400
-#define PNG_FREE_PLTE 0x1000
-#define PNG_FREE_TRNS 0x2000
-#define PNG_FREE_TEXT 0x4000
-#define PNG_FREE_ALL  0x7fff
-#define PNG_FREE_MUL  0x4220 /* PNG_FREE_SPLT|PNG_FREE_TEXT|PNG_FREE_UNKN */
-
-#ifdef PNG_USER_MEM_SUPPORTED
-extern PNG_EXPORT(png_voidp,png_malloc_default) PNGARG((png_structp png_ptr,
-   png_uint_32 size));
-extern PNG_EXPORT(void,png_free_default) PNGARG((png_structp png_ptr,
-   png_voidp ptr));
-#endif
-
-extern PNG_EXPORT(png_voidp,png_memcpy_check) PNGARG((png_structp png_ptr,
-   png_voidp s1, png_voidp s2, png_uint_32 size));
-
-extern PNG_EXPORT(png_voidp,png_memset_check) PNGARG((png_structp png_ptr,
-   png_voidp s1, int value, png_uint_32 size));
-
-#if defined(USE_FAR_KEYWORD)  /* memory model conversion function */
-extern void *png_far_to_near PNGARG((png_structp png_ptr,png_voidp ptr,
-   int check));
-#endif /* USE_FAR_KEYWORD */
-
-#ifndef PNG_NO_ERROR_TEXT
-/* Fatal error in PNG image of libpng - can't continue */
-extern PNG_EXPORT(void,png_error) PNGARG((png_structp png_ptr,
-   png_const_charp error_message));
-
-/* The same, but the chunk name is prepended to the error string. */
-extern PNG_EXPORT(void,png_chunk_error) PNGARG((png_structp png_ptr,
-   png_const_charp error_message));
-#else
-/* Fatal error in PNG image of libpng - can't continue */
-extern PNG_EXPORT(void,png_err) PNGARG((png_structp png_ptr));
-#endif
-
-#ifndef PNG_NO_WARNINGS
-/* Non-fatal error in libpng.  Can continue, but may have a problem. */
-extern PNG_EXPORT(void,png_warning) PNGARG((png_structp png_ptr,
-   png_const_charp warning_message));
-
-#ifdef PNG_READ_SUPPORTED
-/* Non-fatal error in libpng, chunk name is prepended to message. */
-extern PNG_EXPORT(void,png_chunk_warning) PNGARG((png_structp png_ptr,
-   png_const_charp warning_message));
-#endif /* PNG_READ_SUPPORTED */
-#endif /* PNG_NO_WARNINGS */
-
-/* The png_set_<chunk> functions are for storing values in the png_info_struct.
- * Similarly, the png_get_<chunk> calls are used to read values from the
- * png_info_struct, either storing the parameters in the passed variables, or
- * setting pointers into the png_info_struct where the data is stored.  The
- * png_get_<chunk> functions return a non-zero value if the data was available
- * in info_ptr, or return zero and do not change any of the parameters if the
- * data was not available.
- *
- * These functions should be used instead of directly accessing png_info
- * to avoid problems with future changes in the size and internal layout of
- * png_info_struct.
- */
-/* Returns "flag" if chunk data is valid in info_ptr. */
-extern PNG_EXPORT(png_uint_32,png_get_valid) PNGARG((png_structp png_ptr,
-png_infop info_ptr, png_uint_32 flag));
-
-/* Returns number of bytes needed to hold a transformed row. */
-extern PNG_EXPORT(png_uint_32,png_get_rowbytes) PNGARG((png_structp png_ptr,
-png_infop info_ptr));
-
-#if defined(PNG_INFO_IMAGE_SUPPORTED)
-/* Returns row_pointers, which is an array of pointers to scanlines that was
-returned from png_read_png(). */
-extern PNG_EXPORT(png_bytepp,png_get_rows) PNGARG((png_structp png_ptr,
-png_infop info_ptr));
-/* Set row_pointers, which is an array of pointers to scanlines for use
-by png_write_png(). */
-extern PNG_EXPORT(void,png_set_rows) PNGARG((png_structp png_ptr,
-   png_infop info_ptr, png_bytepp row_pointers));
-#endif
-
-/* Returns number of color channels in image. */
-extern PNG_EXPORT(png_byte,png_get_channels) PNGARG((png_structp png_ptr,
-png_infop info_ptr));
-
-#ifdef PNG_EASY_ACCESS_SUPPORTED
-/* Returns image width in pixels. */
-extern PNG_EXPORT(png_uint_32, png_get_image_width) PNGARG((png_structp
-png_ptr, png_infop info_ptr));
-
-/* Returns image height in pixels. */
-extern PNG_EXPORT(png_uint_32, png_get_image_height) PNGARG((png_structp
-png_ptr, png_infop info_ptr));
-
-/* Returns image bit_depth. */
-extern PNG_EXPORT(png_byte, png_get_bit_depth) PNGARG((png_structp
-png_ptr, png_infop info_ptr));
-
-/* Returns image color_type. */
-extern PNG_EXPORT(png_byte, png_get_color_type) PNGARG((png_structp
-png_ptr, png_infop info_ptr));
-
-/* Returns image filter_type. */
-extern PNG_EXPORT(png_byte, png_get_filter_type) PNGARG((png_structp
-png_ptr, png_infop info_ptr));
-
-/* Returns image interlace_type. */
-extern PNG_EXPORT(png_byte, png_get_interlace_type) PNGARG((png_structp
-png_ptr, png_infop info_ptr));
-
-/* Returns image compression_type. */
-extern PNG_EXPORT(png_byte, png_get_compression_type) PNGARG((png_structp
-png_ptr, png_infop info_ptr));
-
-/* Returns image resolution in pixels per meter, from pHYs chunk data. */
-extern PNG_EXPORT(png_uint_32, png_get_pixels_per_meter) PNGARG((png_structp
-png_ptr, png_infop info_ptr));
-extern PNG_EXPORT(png_uint_32, png_get_x_pixels_per_meter) PNGARG((png_structp
-png_ptr, png_infop info_ptr));
-extern PNG_EXPORT(png_uint_32, png_get_y_pixels_per_meter) PNGARG((png_structp
-png_ptr, png_infop info_ptr));
-
-/* Returns pixel aspect ratio, computed from pHYs chunk data.  */
-#ifdef PNG_FLOATING_POINT_SUPPORTED
-extern PNG_EXPORT(float, png_get_pixel_aspect_ratio) PNGARG((png_structp
-png_ptr, png_infop info_ptr));
-#endif
-
-/* Returns image x, y offset in pixels or microns, from oFFs chunk data. */
-extern PNG_EXPORT(png_int_32, png_get_x_offset_pixels) PNGARG((png_structp
-png_ptr, png_infop info_ptr));
-extern PNG_EXPORT(png_int_32, png_get_y_offset_pixels) PNGARG((png_structp
-png_ptr, png_infop info_ptr));
-extern PNG_EXPORT(png_int_32, png_get_x_offset_microns) PNGARG((png_structp
-png_ptr, png_infop info_ptr));
-extern PNG_EXPORT(png_int_32, png_get_y_offset_microns) PNGARG((png_structp
-png_ptr, png_infop info_ptr));
-
-#endif /* PNG_EASY_ACCESS_SUPPORTED */
-
-/* Returns pointer to signature string read from PNG header */
-extern PNG_EXPORT(png_bytep,png_get_signature) PNGARG((png_structp png_ptr,
-png_infop info_ptr));
-
-#if defined(PNG_bKGD_SUPPORTED)
-extern PNG_EXPORT(png_uint_32,png_get_bKGD) PNGARG((png_structp png_ptr,
-   png_infop info_ptr, png_color_16p *background));
-#endif
-
-#if defined(PNG_bKGD_SUPPORTED)
-extern PNG_EXPORT(void,png_set_bKGD) PNGARG((png_structp png_ptr,
-   png_infop info_ptr, png_color_16p background));
-#endif
-
-#if defined(PNG_cHRM_SUPPORTED)
-#ifdef PNG_FLOATING_POINT_SUPPORTED
-extern PNG_EXPORT(png_uint_32,png_get_cHRM) PNGARG((png_structp png_ptr,
-   png_infop info_ptr, double *white_x, double *white_y, double *red_x,
-   double *red_y, double *green_x, double *green_y, double *blue_x,
-   double *blue_y));
-#endif
-#ifdef PNG_FIXED_POINT_SUPPORTED
-extern PNG_EXPORT(png_uint_32,png_get_cHRM_fixed) PNGARG((png_structp png_ptr,
-   png_infop info_ptr, png_fixed_point *int_white_x, png_fixed_point
-   *int_white_y, png_fixed_point *int_red_x, png_fixed_point *int_red_y,
-   png_fixed_point *int_green_x, png_fixed_point *int_green_y, png_fixed_point
-   *int_blue_x, png_fixed_point *int_blue_y));
-#endif
-#endif
-
-#if defined(PNG_cHRM_SUPPORTED)
-#ifdef PNG_FLOATING_POINT_SUPPORTED
-extern PNG_EXPORT(void,png_set_cHRM) PNGARG((png_structp png_ptr,
-   png_infop info_ptr, double white_x, double white_y, double red_x,
-   double red_y, double green_x, double green_y, double blue_x, double blue_y));
-#endif
-#ifdef PNG_FIXED_POINT_SUPPORTED
-extern PNG_EXPORT(void,png_set_cHRM_fixed) PNGARG((png_structp png_ptr,
-   png_infop info_ptr, png_fixed_point int_white_x, png_fixed_point int_white_y,
-   png_fixed_point int_red_x, png_fixed_point int_red_y, png_fixed_point
-   int_green_x, png_fixed_point int_green_y, png_fixed_point int_blue_x,
-   png_fixed_point int_blue_y));
-#endif
-#endif
-
-#if defined(PNG_gAMA_SUPPORTED)
-#ifdef PNG_FLOATING_POINT_SUPPORTED
-extern PNG_EXPORT(png_uint_32,png_get_gAMA) PNGARG((png_structp png_ptr,
-   png_infop info_ptr, double *file_gamma));
-#endif
-extern PNG_EXPORT(png_uint_32,png_get_gAMA_fixed) PNGARG((png_structp png_ptr,
-   png_infop info_ptr, png_fixed_point *int_file_gamma));
-#endif
-
-#if defined(PNG_gAMA_SUPPORTED)
-#ifdef PNG_FLOATING_POINT_SUPPORTED
-extern PNG_EXPORT(void,png_set_gAMA) PNGARG((png_structp png_ptr,
-   png_infop info_ptr, double file_gamma));
-#endif
-extern PNG_EXPORT(void,png_set_gAMA_fixed) PNGARG((png_structp png_ptr,
-   png_infop info_ptr, png_fixed_point int_file_gamma));
-#endif
-
-#if defined(PNG_hIST_SUPPORTED)
-extern PNG_EXPORT(png_uint_32,png_get_hIST) PNGARG((png_structp png_ptr,
-   png_infop info_ptr, png_uint_16p *hist));
-#endif
-
-#if defined(PNG_hIST_SUPPORTED)
-extern PNG_EXPORT(void,png_set_hIST) PNGARG((png_structp png_ptr,
-   png_infop info_ptr, png_uint_16p hist));
-#endif
-
-extern PNG_EXPORT(png_uint_32,png_get_IHDR) PNGARG((png_structp png_ptr,
-   png_infop info_ptr, png_uint_32 *width, png_uint_32 *height,
-   int *bit_depth, int *color_type, int *interlace_method,
-   int *compression_method, int *filter_method));
-
-extern PNG_EXPORT(void,png_set_IHDR) PNGARG((png_structp png_ptr,
-   png_infop info_ptr, png_uint_32 width, png_uint_32 height, int bit_depth,
-   int color_type, int interlace_method, int compression_method,
-   int filter_method));
-
-#if defined(PNG_oFFs_SUPPORTED)
-extern PNG_EXPORT(png_uint_32,png_get_oFFs) PNGARG((png_structp png_ptr,
-   png_infop info_ptr, png_int_32 *offset_x, png_int_32 *offset_y,
-   int *unit_type));
-#endif
-
-#if defined(PNG_oFFs_SUPPORTED)
-extern PNG_EXPORT(void,png_set_oFFs) PNGARG((png_structp png_ptr,
-   png_infop info_ptr, png_int_32 offset_x, png_int_32 offset_y,
-   int unit_type));
-#endif
-
-#if defined(PNG_pCAL_SUPPORTED)
-extern PNG_EXPORT(png_uint_32,png_get_pCAL) PNGARG((png_structp png_ptr,
-   png_infop info_ptr, png_charp *purpose, png_int_32 *X0, png_int_32 *X1,
-   int *type, int *nparams, png_charp *units, png_charpp *params));
-#endif
-
-#if defined(PNG_pCAL_SUPPORTED)
-extern PNG_EXPORT(void,png_set_pCAL) PNGARG((png_structp png_ptr,
-   png_infop info_ptr, png_charp purpose, png_int_32 X0, png_int_32 X1,
-   int type, int nparams, png_charp units, png_charpp params));
-#endif
-
-#if defined(PNG_pHYs_SUPPORTED)
-extern PNG_EXPORT(png_uint_32,png_get_pHYs) PNGARG((png_structp png_ptr,
-   png_infop info_ptr, png_uint_32 *res_x, png_uint_32 *res_y, int *unit_type));
-#endif
-
-#if defined(PNG_pHYs_SUPPORTED)
-extern PNG_EXPORT(void,png_set_pHYs) PNGARG((png_structp png_ptr,
-   png_infop info_ptr, png_uint_32 res_x, png_uint_32 res_y, int unit_type));
-#endif
-
-extern PNG_EXPORT(png_uint_32,png_get_PLTE) PNGARG((png_structp png_ptr,
-   png_infop info_ptr, png_colorp *palette, int *num_palette));
-
-extern PNG_EXPORT(void,png_set_PLTE) PNGARG((png_structp png_ptr,
-   png_infop info_ptr, png_colorp palette, int num_palette));
-
-#if defined(PNG_sBIT_SUPPORTED)
-extern PNG_EXPORT(png_uint_32,png_get_sBIT) PNGARG((png_structp png_ptr,
-   png_infop info_ptr, png_color_8p *sig_bit));
-#endif
-
-#if defined(PNG_sBIT_SUPPORTED)
-extern PNG_EXPORT(void,png_set_sBIT) PNGARG((png_structp png_ptr,
-   png_infop info_ptr, png_color_8p sig_bit));
-#endif
-
-#if defined(PNG_sRGB_SUPPORTED)
-extern PNG_EXPORT(png_uint_32,png_get_sRGB) PNGARG((png_structp png_ptr,
-   png_infop info_ptr, int *intent));
-#endif
-
-#if defined(PNG_sRGB_SUPPORTED)
-extern PNG_EXPORT(void,png_set_sRGB) PNGARG((png_structp png_ptr,
-   png_infop info_ptr, int intent));
-extern PNG_EXPORT(void,png_set_sRGB_gAMA_and_cHRM) PNGARG((png_structp png_ptr,
-   png_infop info_ptr, int intent));
-#endif
-
-#if defined(PNG_iCCP_SUPPORTED)
-extern PNG_EXPORT(png_uint_32,png_get_iCCP) PNGARG((png_structp png_ptr,
-   png_infop info_ptr, png_charpp name, int *compression_type,
-   png_charpp profile, png_uint_32 *proflen));
-   /* Note to maintainer: profile should be png_bytepp */
-#endif
-
-#if defined(PNG_iCCP_SUPPORTED)
-extern PNG_EXPORT(void,png_set_iCCP) PNGARG((png_structp png_ptr,
-   png_infop info_ptr, png_charp name, int compression_type,
-   png_charp profile, png_uint_32 proflen));
-   /* Note to maintainer: profile should be png_bytep */
-#endif
-
-#if defined(PNG_sPLT_SUPPORTED)
-extern PNG_EXPORT(png_uint_32,png_get_sPLT) PNGARG((png_structp png_ptr,
-   png_infop info_ptr, png_sPLT_tpp entries));
-#endif
-
-#if defined(PNG_sPLT_SUPPORTED)
-extern PNG_EXPORT(void,png_set_sPLT) PNGARG((png_structp png_ptr,
-   png_infop info_ptr, png_sPLT_tp entries, int nentries));
-#endif
-
-#if defined(PNG_TEXT_SUPPORTED)
-/* png_get_text also returns the number of text chunks in *num_text */
-extern PNG_EXPORT(png_uint_32,png_get_text) PNGARG((png_structp png_ptr,
-   png_infop info_ptr, png_textp *text_ptr, int *num_text));
-#endif
-
-/*
- *  Note while png_set_text() will accept a structure whose text,
- *  language, and  translated keywords are NULL pointers, the structure
- *  returned by png_get_text will always contain regular
- *  zero-terminated C strings.  They might be empty strings but
- *  they will never be NULL pointers.
- */
-
-#if defined(PNG_TEXT_SUPPORTED)
-extern PNG_EXPORT(void,png_set_text) PNGARG((png_structp png_ptr,
-   png_infop info_ptr, png_textp text_ptr, int num_text));
-#endif
-
-#if defined(PNG_tIME_SUPPORTED)
-extern PNG_EXPORT(png_uint_32,png_get_tIME) PNGARG((png_structp png_ptr,
-   png_infop info_ptr, png_timep *mod_time));
-#endif
-
-#if defined(PNG_tIME_SUPPORTED)
-extern PNG_EXPORT(void,png_set_tIME) PNGARG((png_structp png_ptr,
-   png_infop info_ptr, png_timep mod_time));
-#endif
-
-#if defined(PNG_tRNS_SUPPORTED)
-extern PNG_EXPORT(png_uint_32,png_get_tRNS) PNGARG((png_structp png_ptr,
-   png_infop info_ptr, png_bytep *trans, int *num_trans,
-   png_color_16p *trans_values));
-#endif
-
-#if defined(PNG_tRNS_SUPPORTED)
-extern PNG_EXPORT(void,png_set_tRNS) PNGARG((png_structp png_ptr,
-   png_infop info_ptr, png_bytep trans, int num_trans,
-   png_color_16p trans_values));
-#endif
-
-#if defined(PNG_tRNS_SUPPORTED)
-#endif
-
-#if defined(PNG_sCAL_SUPPORTED)
-#ifdef PNG_FLOATING_POINT_SUPPORTED
-extern PNG_EXPORT(png_uint_32,png_get_sCAL) PNGARG((png_structp png_ptr,
-   png_infop info_ptr, int *unit, double *width, double *height));
-#else
-#ifdef PNG_FIXED_POINT_SUPPORTED
-extern PNG_EXPORT(png_uint_32,png_get_sCAL_s) PNGARG((png_structp png_ptr,
-   png_infop info_ptr, int *unit, png_charpp swidth, png_charpp sheight));
-#endif
-#endif
-#endif /* PNG_sCAL_SUPPORTED */
-
-#if defined(PNG_sCAL_SUPPORTED)
-#ifdef PNG_FLOATING_POINT_SUPPORTED
-extern PNG_EXPORT(void,png_set_sCAL) PNGARG((png_structp png_ptr,
-   png_infop info_ptr, int unit, double width, double height));
-#else
-#ifdef PNG_FIXED_POINT_SUPPORTED
-extern PNG_EXPORT(void,png_set_sCAL_s) PNGARG((png_structp png_ptr,
-   png_infop info_ptr, int unit, png_charp swidth, png_charp sheight));
-#endif
-#endif
-#endif /* PNG_sCAL_SUPPORTED || PNG_WRITE_sCAL_SUPPORTED */
-
-#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
-/* provide a list of chunks and how they are to be handled, if the built-in
-   handling or default unknown chunk handling is not desired.  Any chunks not
-   listed will be handled in the default manner.  The IHDR and IEND chunks
-   must not be listed.
-      keep = 0: follow default behaviour
-           = 1: do not keep
-           = 2: keep only if safe-to-copy
-           = 3: keep even if unsafe-to-copy
-*/
-extern PNG_EXPORT(void, png_set_keep_unknown_chunks) PNGARG((png_structp
-   png_ptr, int keep, png_bytep chunk_list, int num_chunks));
-extern PNG_EXPORT(void, png_set_unknown_chunks) PNGARG((png_structp png_ptr,
-   png_infop info_ptr, png_unknown_chunkp unknowns, int num_unknowns));
-extern PNG_EXPORT(void, png_set_unknown_chunk_location)
-   PNGARG((png_structp png_ptr, png_infop info_ptr, int chunk, int location));
-extern PNG_EXPORT(png_uint_32,png_get_unknown_chunks) PNGARG((png_structp
-   png_ptr, png_infop info_ptr, png_unknown_chunkpp entries));
-#endif
-#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
-PNG_EXPORT(int,png_handle_as_unknown) PNGARG((png_structp png_ptr, png_bytep
-   chunk_name));
-#endif
-
-/* Png_free_data() will turn off the "valid" flag for anything it frees.
-   If you need to turn it off for a chunk that your application has freed,
-   you can use png_set_invalid(png_ptr, info_ptr, PNG_INFO_CHNK); */
-extern PNG_EXPORT(void, png_set_invalid) PNGARG((png_structp png_ptr,
-   png_infop info_ptr, int mask));
-
-#if defined(PNG_INFO_IMAGE_SUPPORTED)
-/* The "params" pointer is currently not used and is for future expansion. */
-extern PNG_EXPORT(void, png_read_png) PNGARG((png_structp png_ptr,
-                        png_infop info_ptr,
-                        int transforms,
-                        png_voidp params));
-extern PNG_EXPORT(void, png_write_png) PNGARG((png_structp png_ptr,
-                        png_infop info_ptr,
-                        int transforms,
-                        png_voidp params));
-#endif
-
-/* Define PNG_DEBUG at compile time for debugging information.  Higher
- * numbers for PNG_DEBUG mean more debugging information.  This has
- * only been added since version 0.95 so it is not implemented throughout
- * libpng yet, but more support will be added as needed.
- */
-#ifdef PNG_DEBUG
-#if (PNG_DEBUG > 0)
-#if !defined(PNG_DEBUG_FILE) && defined(_MSC_VER)
-#include <crtdbg.h>
-#if (PNG_DEBUG > 1)
-#define png_debug(l,m)  _RPT0(_CRT_WARN,m)
-#define png_debug1(l,m,p1)  _RPT1(_CRT_WARN,m,p1)
-#define png_debug2(l,m,p1,p2) _RPT2(_CRT_WARN,m,p1,p2)
-#endif
-#else /* PNG_DEBUG_FILE || !_MSC_VER */
-#ifndef PNG_DEBUG_FILE
-#define PNG_DEBUG_FILE stderr
-#endif /* PNG_DEBUG_FILE */
-#if (PNG_DEBUG > 1)
-#define png_debug(l,m) \
-{ \
-     int num_tabs=l; \
-     fprintf(PNG_DEBUG_FILE,"%s"m,(num_tabs==1 ? "\t" : \
-       (num_tabs==2 ? "\t\t":(num_tabs>2 ? "\t\t\t":"")))); \
-}
-#define png_debug1(l,m,p1) \
-{ \
-     int num_tabs=l; \
-     fprintf(PNG_DEBUG_FILE,"%s"m,(num_tabs==1 ? "\t" : \
-       (num_tabs==2 ? "\t\t":(num_tabs>2 ? "\t\t\t":""))),p1); \
-}
-#define png_debug2(l,m,p1,p2) \
-{ \
-     int num_tabs=l; \
-     fprintf(PNG_DEBUG_FILE,"%s"m,(num_tabs==1 ? "\t" : \
-       (num_tabs==2 ? "\t\t":(num_tabs>2 ? "\t\t\t":""))),p1,p2); \
-}
-#endif /* (PNG_DEBUG > 1) */
-#endif /* _MSC_VER */
-#endif /* (PNG_DEBUG > 0) */
-#endif /* PNG_DEBUG */
-#ifndef png_debug
-#define png_debug(l, m)
-#endif
-#ifndef png_debug1
-#define png_debug1(l, m, p1)
-#endif
-#ifndef png_debug2
-#define png_debug2(l, m, p1, p2)
-#endif
-
-extern PNG_EXPORT(png_charp,png_get_copyright) PNGARG((png_structp png_ptr));
-extern PNG_EXPORT(png_charp,png_get_header_ver) PNGARG((png_structp png_ptr));
-extern PNG_EXPORT(png_charp,png_get_header_version) PNGARG((png_structp png_ptr));
-extern PNG_EXPORT(png_charp,png_get_libpng_ver) PNGARG((png_structp png_ptr));
-
-#ifdef PNG_MNG_FEATURES_SUPPORTED
-extern PNG_EXPORT(png_uint_32,png_permit_mng_features) PNGARG((png_structp
-   png_ptr, png_uint_32 mng_features_permitted));
-#endif
-
-/* For use in png_set_keep_unknown, added to version 1.2.6 */
-#define PNG_HANDLE_CHUNK_AS_DEFAULT   0
-#define PNG_HANDLE_CHUNK_NEVER        1
-#define PNG_HANDLE_CHUNK_IF_SAFE      2
-#define PNG_HANDLE_CHUNK_ALWAYS       3
-
-/* Added to version 1.2.0 */
-#if defined(PNG_ASSEMBLER_CODE_SUPPORTED)
-#if defined(PNG_MMX_CODE_SUPPORTED)
-#define PNG_ASM_FLAG_MMX_SUPPORT_COMPILED  0x01  /* not user-settable */
-#define PNG_ASM_FLAG_MMX_SUPPORT_IN_CPU    0x02  /* not user-settable */
-#define PNG_ASM_FLAG_MMX_READ_COMBINE_ROW  0x04
-#define PNG_ASM_FLAG_MMX_READ_INTERLACE    0x08
-#define PNG_ASM_FLAG_MMX_READ_FILTER_SUB   0x10
-#define PNG_ASM_FLAG_MMX_READ_FILTER_UP    0x20
-#define PNG_ASM_FLAG_MMX_READ_FILTER_AVG   0x40
-#define PNG_ASM_FLAG_MMX_READ_FILTER_PAETH 0x80
-#define PNG_ASM_FLAGS_INITIALIZED          0x80000000  /* not user-settable */
-
-#define PNG_MMX_READ_FLAGS ( PNG_ASM_FLAG_MMX_READ_COMBINE_ROW  \
-                           | PNG_ASM_FLAG_MMX_READ_INTERLACE    \
-                           | PNG_ASM_FLAG_MMX_READ_FILTER_SUB   \
-                           | PNG_ASM_FLAG_MMX_READ_FILTER_UP    \
-                           | PNG_ASM_FLAG_MMX_READ_FILTER_AVG   \
-                           | PNG_ASM_FLAG_MMX_READ_FILTER_PAETH )
-#define PNG_MMX_WRITE_FLAGS ( 0 )
-
-#define PNG_MMX_FLAGS ( PNG_ASM_FLAG_MMX_SUPPORT_COMPILED \
-                      | PNG_ASM_FLAG_MMX_SUPPORT_IN_CPU   \
-                      | PNG_MMX_READ_FLAGS                \
-                      | PNG_MMX_WRITE_FLAGS )
-
-#define PNG_SELECT_READ   1
-#define PNG_SELECT_WRITE  2
-#endif /* PNG_MMX_CODE_SUPPORTED */
-
-#if !defined(PNG_1_0_X)
-/* pngget.c */
-extern PNG_EXPORT(png_uint_32,png_get_mmx_flagmask)
-   PNGARG((int flag_select, int *compilerID));
-
-/* pngget.c */
-extern PNG_EXPORT(png_uint_32,png_get_asm_flagmask)
-   PNGARG((int flag_select));
-
-/* pngget.c */
-extern PNG_EXPORT(png_uint_32,png_get_asm_flags)
-   PNGARG((png_structp png_ptr));
-
-/* pngget.c */
-extern PNG_EXPORT(png_byte,png_get_mmx_bitdepth_threshold)
-   PNGARG((png_structp png_ptr));
-
-/* pngget.c */
-extern PNG_EXPORT(png_uint_32,png_get_mmx_rowbytes_threshold)
-   PNGARG((png_structp png_ptr));
-
-/* pngset.c */
-extern PNG_EXPORT(void,png_set_asm_flags)
-   PNGARG((png_structp png_ptr, png_uint_32 asm_flags));
-
-/* pngset.c */
-extern PNG_EXPORT(void,png_set_mmx_thresholds)
-   PNGARG((png_structp png_ptr, png_byte mmx_bitdepth_threshold,
-   png_uint_32 mmx_rowbytes_threshold));
-
-#endif /* PNG_1_0_X */
-
-#if !defined(PNG_1_0_X)
-/* png.c, pnggccrd.c, or pngvcrd.c */
-extern PNG_EXPORT(int,png_mmx_support) PNGARG((void));
-#endif /* PNG_ASSEMBLER_CODE_SUPPORTED */
-
-/* Strip the prepended error numbers ("#nnn ") from error and warning
- * messages before passing them to the error or warning handler. */
-#ifdef PNG_ERROR_NUMBERS_SUPPORTED
-extern PNG_EXPORT(void,png_set_strip_error_numbers) PNGARG((png_structp
-   png_ptr, png_uint_32 strip_mode));
-#endif
-
-#endif /* PNG_1_0_X */
-
-/* Added at libpng-1.2.6 */
-#ifdef PNG_SET_USER_LIMITS_SUPPORTED
-extern PNG_EXPORT(void,png_set_user_limits) PNGARG((png_structp
-   png_ptr, png_uint_32 user_width_max, png_uint_32 user_height_max));
-extern PNG_EXPORT(png_uint_32,png_get_user_width_max) PNGARG((png_structp
-   png_ptr));
-extern PNG_EXPORT(png_uint_32,png_get_user_height_max) PNGARG((png_structp
-   png_ptr));
-#endif
-
-/* Maintainer: Put new public prototypes here ^, in libpng.3, and project defs */
-
-#ifdef PNG_READ_COMPOSITE_NODIV_SUPPORTED
-/* With these routines we avoid an integer divide, which will be slower on
- * most machines.  However, it does take more operations than the corresponding
- * divide method, so it may be slower on a few RISC systems.  There are two
- * shifts (by 8 or 16 bits) and an addition, versus a single integer divide.
- *
- * Note that the rounding factors are NOT supposed to be the same!  128 and
- * 32768 are correct for the NODIV code; 127 and 32767 are correct for the
- * standard method.
- *
- * [Optimized code by Greg Roelofs and Mark Adler...blame us for bugs. :-) ]
- */
-
- /* fg and bg should be in `gamma 1.0' space; alpha is the opacity          */
-
-#  define png_composite(composite, fg, alpha, bg)                            \
-     { png_uint_16 temp = (png_uint_16)((png_uint_16)(fg) * (png_uint_16)(alpha) \
-                        +        (png_uint_16)(bg)*(png_uint_16)(255 -       \
-                        (png_uint_16)(alpha)) + (png_uint_16)128);           \
-       (composite) = (png_byte)((temp + (temp >> 8)) >> 8); }
-
-#  define png_composite_16(composite, fg, alpha, bg)                         \
-     { png_uint_32 temp = (png_uint_32)((png_uint_32)(fg) * (png_uint_32)(alpha) \
-                        + (png_uint_32)(bg)*(png_uint_32)(65535L -           \
-                        (png_uint_32)(alpha)) + (png_uint_32)32768L);        \
-       (composite) = (png_uint_16)((temp + (temp >> 16)) >> 16); }
-
-#else  /* standard method using integer division */
-
-#  define png_composite(composite, fg, alpha, bg)                            \
-     (composite) = (png_byte)(((png_uint_16)(fg) * (png_uint_16)(alpha) +    \
-       (png_uint_16)(bg) * (png_uint_16)(255 - (png_uint_16)(alpha)) +       \
-       (png_uint_16)127) / 255)
-
-#  define png_composite_16(composite, fg, alpha, bg)                         \
-     (composite) = (png_uint_16)(((png_uint_32)(fg) * (png_uint_32)(alpha) + \
-       (png_uint_32)(bg)*(png_uint_32)(65535L - (png_uint_32)(alpha)) +      \
-       (png_uint_32)32767) / (png_uint_32)65535L)
-
-#endif /* PNG_READ_COMPOSITE_NODIV_SUPPORTED */
-
-/* Inline macros to do direct reads of bytes from the input buffer.  These
- * require that you are using an architecture that uses PNG byte ordering
- * (MSB first) and supports unaligned data storage.  I think that PowerPC
- * in big-endian mode and 680x0 are the only ones that will support this.
- * The x86 line of processors definitely do not.  The png_get_int_32()
- * routine also assumes we are using two's complement format for negative
- * values, which is almost certainly true.
- */
-#if defined(PNG_READ_BIG_ENDIAN_SUPPORTED)
-#  define png_get_uint_32(buf) ( *((png_uint_32p) (buf)))
-#  define png_get_uint_16(buf) ( *((png_uint_16p) (buf)))
-#  define png_get_int_32(buf)  ( *((png_int_32p)  (buf)))
-#else
-extern PNG_EXPORT(png_uint_32,png_get_uint_32) PNGARG((png_bytep buf));
-extern PNG_EXPORT(png_uint_16,png_get_uint_16) PNGARG((png_bytep buf));
-extern PNG_EXPORT(png_int_32,png_get_int_32) PNGARG((png_bytep buf));
-#endif /* !PNG_READ_BIG_ENDIAN_SUPPORTED */
-extern PNG_EXPORT(png_uint_32,png_get_uint_31)
-  PNGARG((png_structp png_ptr, png_bytep buf));
-/* No png_get_int_16 -- may be added if there's a real need for it. */
-
-/* Place a 32-bit number into a buffer in PNG byte order (big-endian).
- */
-extern PNG_EXPORT(void,png_save_uint_32)
-   PNGARG((png_bytep buf, png_uint_32 i));
-extern PNG_EXPORT(void,png_save_int_32)
-   PNGARG((png_bytep buf, png_int_32 i));
-
-/* Place a 16-bit number into a buffer in PNG byte order.
- * The parameter is declared unsigned int, not png_uint_16,
- * just to avoid potential problems on pre-ANSI C compilers.
- */
-extern PNG_EXPORT(void,png_save_uint_16)
-   PNGARG((png_bytep buf, unsigned int i));
-/* No png_save_int_16 -- may be added if there's a real need for it. */
-
-/* ************************************************************************* */
-
-/* These next functions are used internally in the code.  They generally
- * shouldn't be used unless you are writing code to add or replace some
- * functionality in libpng.  More information about most functions can
- * be found in the files where the functions are located.
- */
-
-
-/* Various modes of operation, that are visible to applications because
- * they are used for unknown chunk location.
- */
-#define PNG_HAVE_IHDR               0x01
-#define PNG_HAVE_PLTE               0x02
-#define PNG_HAVE_IDAT               0x04
-#define PNG_AFTER_IDAT              0x08 /* Have complete zlib datastream */
-#define PNG_HAVE_IEND               0x10
-
-#if defined(PNG_INTERNAL)
-
-/* More modes of operation.  Note that after an init, mode is set to
- * zero automatically when the structure is created.
- */
-#define PNG_HAVE_gAMA               0x20
-#define PNG_HAVE_cHRM               0x40
-#define PNG_HAVE_sRGB               0x80
-#define PNG_HAVE_CHUNK_HEADER      0x100
-#define PNG_WROTE_tIME             0x200
-#define PNG_WROTE_INFO_BEFORE_PLTE 0x400
-#define PNG_BACKGROUND_IS_GRAY     0x800
-#define PNG_HAVE_PNG_SIGNATURE    0x1000
-#define PNG_HAVE_CHUNK_AFTER_IDAT 0x2000 /* Have another chunk after IDAT */
-
-/* flags for the transformations the PNG library does on the image data */
-#define PNG_BGR                0x0001
-#define PNG_INTERLACE          0x0002
-#define PNG_PACK               0x0004
-#define PNG_SHIFT              0x0008
-#define PNG_SWAP_BYTES         0x0010
-#define PNG_INVERT_MONO        0x0020
-#define PNG_DITHER             0x0040
-#define PNG_BACKGROUND         0x0080
-#define PNG_BACKGROUND_EXPAND  0x0100
-                          /*   0x0200 unused */
-#define PNG_16_TO_8            0x0400
-#define PNG_RGBA               0x0800
-#define PNG_EXPAND             0x1000
-#define PNG_GAMMA              0x2000
-#define PNG_GRAY_TO_RGB        0x4000
-#define PNG_FILLER             0x8000L
-#define PNG_PACKSWAP          0x10000L
-#define PNG_SWAP_ALPHA        0x20000L
-#define PNG_STRIP_ALPHA       0x40000L
-#define PNG_INVERT_ALPHA      0x80000L
-#define PNG_USER_TRANSFORM   0x100000L
-#define PNG_RGB_TO_GRAY_ERR  0x200000L
-#define PNG_RGB_TO_GRAY_WARN 0x400000L
-#define PNG_RGB_TO_GRAY      0x600000L  /* two bits, RGB_TO_GRAY_ERR|WARN */
-                       /*    0x800000L     Unused */
-#define PNG_ADD_ALPHA       0x1000000L  /* Added to libpng-1.2.7 */
-#define PNG_EXPAND_tRNS     0x2000000L  /* Added to libpng-1.2.9 */
-                       /*   0x4000000L  unused */
-                       /*   0x8000000L  unused */
-                       /*  0x10000000L  unused */
-                       /*  0x20000000L  unused */
-                       /*  0x40000000L  unused */
-
-/* flags for png_create_struct */
-#define PNG_STRUCT_PNG   0x0001
-#define PNG_STRUCT_INFO  0x0002
-
-/* Scaling factor for filter heuristic weighting calculations */
-#define PNG_WEIGHT_SHIFT 8
-#define PNG_WEIGHT_FACTOR (1<<(PNG_WEIGHT_SHIFT))
-#define PNG_COST_SHIFT 3
-#define PNG_COST_FACTOR (1<<(PNG_COST_SHIFT))
-
-/* flags for the png_ptr->flags rather than declaring a byte for each one */
-#define PNG_FLAG_ZLIB_CUSTOM_STRATEGY     0x0001
-#define PNG_FLAG_ZLIB_CUSTOM_LEVEL        0x0002
-#define PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL    0x0004
-#define PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS  0x0008
-#define PNG_FLAG_ZLIB_CUSTOM_METHOD       0x0010
-#define PNG_FLAG_ZLIB_FINISHED            0x0020
-#define PNG_FLAG_ROW_INIT                 0x0040
-#define PNG_FLAG_FILLER_AFTER             0x0080
-#define PNG_FLAG_CRC_ANCILLARY_USE        0x0100
-#define PNG_FLAG_CRC_ANCILLARY_NOWARN     0x0200
-#define PNG_FLAG_CRC_CRITICAL_USE         0x0400
-#define PNG_FLAG_CRC_CRITICAL_IGNORE      0x0800
-#define PNG_FLAG_FREE_PLTE                0x1000
-#define PNG_FLAG_FREE_TRNS                0x2000
-#define PNG_FLAG_FREE_HIST                0x4000
-#define PNG_FLAG_KEEP_UNKNOWN_CHUNKS      0x8000L
-#define PNG_FLAG_KEEP_UNSAFE_CHUNKS       0x10000L
-#define PNG_FLAG_LIBRARY_MISMATCH         0x20000L
-#define PNG_FLAG_STRIP_ERROR_NUMBERS      0x40000L
-#define PNG_FLAG_STRIP_ERROR_TEXT         0x80000L
-#define PNG_FLAG_MALLOC_NULL_MEM_OK       0x100000L
-#define PNG_FLAG_ADD_ALPHA                0x200000L  /* Added to libpng-1.2.8 */
-#define PNG_FLAG_STRIP_ALPHA              0x400000L  /* Added to libpng-1.2.8 */
-                                  /*      0x800000L  unused */
-                                  /*     0x1000000L  unused */
-                                  /*     0x2000000L  unused */
-                                  /*     0x4000000L  unused */
-                                  /*     0x8000000L  unused */
-                                  /*    0x10000000L  unused */
-                                  /*    0x20000000L  unused */
-                                  /*    0x40000000L  unused */
-
-#define PNG_FLAG_CRC_ANCILLARY_MASK (PNG_FLAG_CRC_ANCILLARY_USE | \
-                                     PNG_FLAG_CRC_ANCILLARY_NOWARN)
-
-#define PNG_FLAG_CRC_CRITICAL_MASK  (PNG_FLAG_CRC_CRITICAL_USE | \
-                                     PNG_FLAG_CRC_CRITICAL_IGNORE)
-
-#define PNG_FLAG_CRC_MASK           (PNG_FLAG_CRC_ANCILLARY_MASK | \
-                                     PNG_FLAG_CRC_CRITICAL_MASK)
-
-/* save typing and make code easier to understand */
-
-#define PNG_COLOR_DIST(c1, c2) (abs((int)((c1).red) - (int)((c2).red)) + \
-   abs((int)((c1).green) - (int)((c2).green)) + \
-   abs((int)((c1).blue) - (int)((c2).blue)))
-
-/* Added to libpng-1.2.6 JB */
-#define PNG_ROWBYTES(pixel_bits, width) \
-    ((pixel_bits) >= 8 ? \
-    ((width) * (((png_uint_32)(pixel_bits)) >> 3)) : \
-    (( ((width) * ((png_uint_32)(pixel_bits))) + 7) >> 3) )
-
-/* PNG_OUT_OF_RANGE returns true if value is outside the range
-   ideal-delta..ideal+delta.  Each argument is evaluated twice.
-   "ideal" and "delta" should be constants, normally simple
-   integers, "value" a variable. Added to libpng-1.2.6 JB */
-#define PNG_OUT_OF_RANGE(value, ideal, delta) \
-        ( (value) < (ideal)-(delta) || (value) > (ideal)+(delta) )
-
-/* variables declared in png.c - only it needs to define PNG_NO_EXTERN */
-#if !defined(PNG_NO_EXTERN) || defined(PNG_ALWAYS_EXTERN)
-/* place to hold the signature string for a PNG file. */
-#ifdef PNG_USE_GLOBAL_ARRAYS
-   PNG_EXPORT_VAR (PNG_CONST png_byte FARDATA) png_sig[8];
-#else
-#endif
-#endif /* PNG_NO_EXTERN */
-
-/* Constant strings for known chunk types.  If you need to add a chunk,
- * define the name here, and add an invocation of the macro in png.c and
- * wherever it's needed.
- */
-#define PNG_IHDR png_byte png_IHDR[5] = { 73,  72,  68,  82, '\0'}
-#define PNG_IDAT png_byte png_IDAT[5] = { 73,  68,  65,  84, '\0'}
-#define PNG_IEND png_byte png_IEND[5] = { 73,  69,  78,  68, '\0'}
-#define PNG_PLTE png_byte png_PLTE[5] = { 80,  76,  84,  69, '\0'}
-#define PNG_bKGD png_byte png_bKGD[5] = { 98,  75,  71,  68, '\0'}
-#define PNG_cHRM png_byte png_cHRM[5] = { 99,  72,  82,  77, '\0'}
-#define PNG_gAMA png_byte png_gAMA[5] = {103,  65,  77,  65, '\0'}
-#define PNG_hIST png_byte png_hIST[5] = {104,  73,  83,  84, '\0'}
-#define PNG_iCCP png_byte png_iCCP[5] = {105,  67,  67,  80, '\0'}
-#define PNG_iTXt png_byte png_iTXt[5] = {105,  84,  88, 116, '\0'}
-#define PNG_oFFs png_byte png_oFFs[5] = {111,  70,  70, 115, '\0'}
-#define PNG_pCAL png_byte png_pCAL[5] = {112,  67,  65,  76, '\0'}
-#define PNG_sCAL png_byte png_sCAL[5] = {115,  67,  65,  76, '\0'}
-#define PNG_pHYs png_byte png_pHYs[5] = {112,  72,  89, 115, '\0'}
-#define PNG_sBIT png_byte png_sBIT[5] = {115,  66,  73,  84, '\0'}
-#define PNG_sPLT png_byte png_sPLT[5] = {115,  80,  76,  84, '\0'}
-#define PNG_sRGB png_byte png_sRGB[5] = {115,  82,  71,  66, '\0'}
-#define PNG_tEXt png_byte png_tEXt[5] = {116,  69,  88, 116, '\0'}
-#define PNG_tIME png_byte png_tIME[5] = {116,  73,  77,  69, '\0'}
-#define PNG_tRNS png_byte png_tRNS[5] = {116,  82,  78,  83, '\0'}
-#define PNG_zTXt png_byte png_zTXt[5] = {122,  84,  88, 116, '\0'}
-
-#ifdef PNG_USE_GLOBAL_ARRAYS
-PNG_EXPORT_VAR (png_byte FARDATA) png_IHDR[5];
-PNG_EXPORT_VAR (png_byte FARDATA) png_IDAT[5];
-PNG_EXPORT_VAR (png_byte FARDATA) png_IEND[5];
-PNG_EXPORT_VAR (png_byte FARDATA) png_PLTE[5];
-PNG_EXPORT_VAR (png_byte FARDATA) png_bKGD[5];
-PNG_EXPORT_VAR (png_byte FARDATA) png_cHRM[5];
-PNG_EXPORT_VAR (png_byte FARDATA) png_gAMA[5];
-PNG_EXPORT_VAR (png_byte FARDATA) png_hIST[5];
-PNG_EXPORT_VAR (png_byte FARDATA) png_iCCP[5];
-PNG_EXPORT_VAR (png_byte FARDATA) png_iTXt[5];
-PNG_EXPORT_VAR (png_byte FARDATA) png_oFFs[5];
-PNG_EXPORT_VAR (png_byte FARDATA) png_pCAL[5];
-PNG_EXPORT_VAR (png_byte FARDATA) png_sCAL[5];
-PNG_EXPORT_VAR (png_byte FARDATA) png_pHYs[5];
-PNG_EXPORT_VAR (png_byte FARDATA) png_sBIT[5];
-PNG_EXPORT_VAR (png_byte FARDATA) png_sPLT[5];
-PNG_EXPORT_VAR (png_byte FARDATA) png_sRGB[5];
-PNG_EXPORT_VAR (png_byte FARDATA) png_tEXt[5];
-PNG_EXPORT_VAR (png_byte FARDATA) png_tIME[5];
-PNG_EXPORT_VAR (png_byte FARDATA) png_tRNS[5];
-PNG_EXPORT_VAR (png_byte FARDATA) png_zTXt[5];
-#endif /* PNG_USE_GLOBAL_ARRAYS */
-
-#if defined(PNG_1_0_X) || defined (PNG_1_2_X)
-/* Initialize png_ptr struct for reading, and allocate any other memory.
- * (old interface - DEPRECATED - use png_create_read_struct instead).
- */
-extern PNG_EXPORT(void,png_read_init) PNGARG((png_structp png_ptr));
-#undef png_read_init
-#define png_read_init(png_ptr) png_read_init_3(&png_ptr, \
-    PNG_LIBPNG_VER_STRING,  png_sizeof(png_struct));
-#endif
-
-extern PNG_EXPORT(void,png_read_init_3) PNGARG((png_structpp ptr_ptr,
-    png_const_charp user_png_ver, png_size_t png_struct_size));
-#if defined(PNG_1_0_X) || defined (PNG_1_2_X)
-extern PNG_EXPORT(void,png_read_init_2) PNGARG((png_structp png_ptr,
-    png_const_charp user_png_ver, png_size_t png_struct_size, png_size_t
-    png_info_size));
-#endif
-
-#if defined(PNG_1_0_X) || defined (PNG_1_2_X)
-/* Initialize png_ptr struct for writing, and allocate any other memory.
- * (old interface - DEPRECATED - use png_create_write_struct instead).
- */
-extern PNG_EXPORT(void,png_write_init) PNGARG((png_structp png_ptr));
-#undef png_write_init
-#define png_write_init(png_ptr) png_write_init_3(&png_ptr, \
-    PNG_LIBPNG_VER_STRING, png_sizeof(png_struct));
-#endif
-
-extern PNG_EXPORT(void,png_write_init_3) PNGARG((png_structpp ptr_ptr,
-    png_const_charp user_png_ver, png_size_t png_struct_size));
-extern PNG_EXPORT(void,png_write_init_2) PNGARG((png_structp png_ptr,
-    png_const_charp user_png_ver, png_size_t png_struct_size, png_size_t
-    png_info_size));
-
-/* Allocate memory for an internal libpng struct */
-PNG_EXTERN png_voidp png_create_struct PNGARG((int type));
-
-/* Free memory from internal libpng struct */
-PNG_EXTERN void png_destroy_struct PNGARG((png_voidp struct_ptr));
-
-PNG_EXTERN png_voidp png_create_struct_2 PNGARG((int type, png_malloc_ptr
-  malloc_fn, png_voidp mem_ptr));
-PNG_EXTERN void png_destroy_struct_2 PNGARG((png_voidp struct_ptr,
-   png_free_ptr free_fn, png_voidp mem_ptr));
-
-/* Free any memory that info_ptr points to and reset struct. */
-PNG_EXTERN void png_info_destroy PNGARG((png_structp png_ptr,
-   png_infop info_ptr));
-
-#ifndef PNG_1_0_X
-/* Function to allocate memory for zlib. */
-PNG_EXTERN voidpf png_zalloc PNGARG((voidpf png_ptr, uInt items, uInt size));
-
-/* Function to free memory for zlib */
-PNG_EXTERN void png_zfree PNGARG((voidpf png_ptr, voidpf ptr));
-
-#ifdef PNG_SIZE_T
-/* Function to convert a sizeof an item to png_sizeof item */
-   PNG_EXTERN png_size_t PNGAPI png_convert_size PNGARG((size_t size));
-#endif
-
-/* Next four functions are used internally as callbacks.  PNGAPI is required
- * but not PNG_EXPORT.  PNGAPI added at libpng version 1.2.3. */
-
-PNG_EXTERN void PNGAPI png_default_read_data PNGARG((png_structp png_ptr,
-   png_bytep data, png_size_t length));
-
-#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
-PNG_EXTERN void PNGAPI png_push_fill_buffer PNGARG((png_structp png_ptr,
-   png_bytep buffer, png_size_t length));
-#endif
-
-PNG_EXTERN void PNGAPI png_default_write_data PNGARG((png_structp png_ptr,
-   png_bytep data, png_size_t length));
-
-#if defined(PNG_WRITE_FLUSH_SUPPORTED)
-#if !defined(PNG_NO_STDIO)
-PNG_EXTERN void PNGAPI png_default_flush PNGARG((png_structp png_ptr));
-#endif
-#endif
-#else /* PNG_1_0_X */
-#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
-PNG_EXTERN void png_push_fill_buffer PNGARG((png_structp png_ptr,
-   png_bytep buffer, png_size_t length));
-#endif
-#endif /* PNG_1_0_X */
-
-/* Reset the CRC variable */
-PNG_EXTERN void png_reset_crc PNGARG((png_structp png_ptr));
-
-/* Write the "data" buffer to whatever output you are using. */
-PNG_EXTERN void png_write_data PNGARG((png_structp png_ptr, png_bytep data,
-   png_size_t length));
-
-/* Read data from whatever input you are using into the "data" buffer */
-PNG_EXTERN void png_read_data PNGARG((png_structp png_ptr, png_bytep data,
-   png_size_t length));
-
-/* Read bytes into buf, and update png_ptr->crc */
-PNG_EXTERN void png_crc_read PNGARG((png_structp png_ptr, png_bytep buf,
-   png_size_t length));
-
-/* Decompress data in a chunk that uses compression */
-#if defined(PNG_zTXt_SUPPORTED) || defined(PNG_iTXt_SUPPORTED) || \
-    defined(PNG_iCCP_SUPPORTED) || defined(PNG_sPLT_SUPPORTED)
-PNG_EXTERN png_charp png_decompress_chunk PNGARG((png_structp png_ptr,
-   int comp_type, png_charp chunkdata, png_size_t chunklength,
-   png_size_t prefix_length, png_size_t *data_length));
-#endif
-
-/* Read "skip" bytes, read the file crc, and (optionally) verify png_ptr->crc */
-PNG_EXTERN int png_crc_finish PNGARG((png_structp png_ptr, png_uint_32 skip));
-
-/* Read the CRC from the file and compare it to the libpng calculated CRC */
-PNG_EXTERN int png_crc_error PNGARG((png_structp png_ptr));
-
-/* Calculate the CRC over a section of data.  Note that we are only
- * passing a maximum of 64K on systems that have this as a memory limit,
- * since this is the maximum buffer size we can specify.
- */
-PNG_EXTERN void png_calculate_crc PNGARG((png_structp png_ptr, png_bytep ptr,
-   png_size_t length));
-
-#if defined(PNG_WRITE_FLUSH_SUPPORTED)
-PNG_EXTERN void png_flush PNGARG((png_structp png_ptr));
-#endif
-
-/* simple function to write the signature */
-PNG_EXTERN void png_write_sig PNGARG((png_structp png_ptr));
-
-/* write various chunks */
-
-/* Write the IHDR chunk, and update the png_struct with the necessary
- * information.
- */
-PNG_EXTERN void png_write_IHDR PNGARG((png_structp png_ptr, png_uint_32 width,
-   png_uint_32 height,
-   int bit_depth, int color_type, int compression_method, int filter_method,
-   int interlace_method));
-
-PNG_EXTERN void png_write_PLTE PNGARG((png_structp png_ptr, png_colorp palette,
-   png_uint_32 num_pal));
-
-PNG_EXTERN void png_write_IDAT PNGARG((png_structp png_ptr, png_bytep data,
-   png_size_t length));
-
-PNG_EXTERN void png_write_IEND PNGARG((png_structp png_ptr));
-
-#if defined(PNG_WRITE_gAMA_SUPPORTED)
-#ifdef PNG_FLOATING_POINT_SUPPORTED
-PNG_EXTERN void png_write_gAMA PNGARG((png_structp png_ptr, double file_gamma));
-#endif
-#ifdef PNG_FIXED_POINT_SUPPORTED
-PNG_EXTERN void png_write_gAMA_fixed PNGARG((png_structp png_ptr, png_fixed_point
-    file_gamma));
-#endif
-#endif
-
-#if defined(PNG_WRITE_sBIT_SUPPORTED)
-PNG_EXTERN void png_write_sBIT PNGARG((png_structp png_ptr, png_color_8p sbit,
-   int color_type));
-#endif
-
-#if defined(PNG_WRITE_cHRM_SUPPORTED)
-#ifdef PNG_FLOATING_POINT_SUPPORTED
-PNG_EXTERN void png_write_cHRM PNGARG((png_structp png_ptr,
-   double white_x, double white_y,
-   double red_x, double red_y, double green_x, double green_y,
-   double blue_x, double blue_y));
-#endif
-#ifdef PNG_FIXED_POINT_SUPPORTED
-PNG_EXTERN void png_write_cHRM_fixed PNGARG((png_structp png_ptr,
-   png_fixed_point int_white_x, png_fixed_point int_white_y,
-   png_fixed_point int_red_x, png_fixed_point int_red_y, png_fixed_point
-   int_green_x, png_fixed_point int_green_y, png_fixed_point int_blue_x,
-   png_fixed_point int_blue_y));
-#endif
-#endif
-
-#if defined(PNG_WRITE_sRGB_SUPPORTED)
-PNG_EXTERN void png_write_sRGB PNGARG((png_structp png_ptr,
-   int intent));
-#endif
-
-#if defined(PNG_WRITE_iCCP_SUPPORTED)
-PNG_EXTERN void png_write_iCCP PNGARG((png_structp png_ptr,
-   png_charp name, int compression_type,
-   png_charp profile, int proflen));
-   /* Note to maintainer: profile should be png_bytep */
-#endif
-
-#if defined(PNG_WRITE_sPLT_SUPPORTED)
-PNG_EXTERN void png_write_sPLT PNGARG((png_structp png_ptr,
-   png_sPLT_tp palette));
-#endif
-
-#if defined(PNG_WRITE_tRNS_SUPPORTED)
-PNG_EXTERN void png_write_tRNS PNGARG((png_structp png_ptr, png_bytep trans,
-   png_color_16p values, int number, int color_type));
-#endif
-
-#if defined(PNG_WRITE_bKGD_SUPPORTED)
-PNG_EXTERN void png_write_bKGD PNGARG((png_structp png_ptr,
-   png_color_16p values, int color_type));
-#endif
-
-#if defined(PNG_WRITE_hIST_SUPPORTED)
-PNG_EXTERN void png_write_hIST PNGARG((png_structp png_ptr, png_uint_16p hist,
-   int num_hist));
-#endif
-
-#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_pCAL_SUPPORTED) || \
-    defined(PNG_WRITE_iCCP_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
-PNG_EXTERN png_size_t png_check_keyword PNGARG((png_structp png_ptr,
-   png_charp key, png_charpp new_key));
-#endif
-
-#if defined(PNG_WRITE_tEXt_SUPPORTED)
-PNG_EXTERN void png_write_tEXt PNGARG((png_structp png_ptr, png_charp key,
-   png_charp text, png_size_t text_len));
-#endif
-
-#if defined(PNG_WRITE_zTXt_SUPPORTED)
-PNG_EXTERN void png_write_zTXt PNGARG((png_structp png_ptr, png_charp key,
-   png_charp text, png_size_t text_len, int compression));
-#endif
-
-#if defined(PNG_WRITE_iTXt_SUPPORTED)
-PNG_EXTERN void png_write_iTXt PNGARG((png_structp png_ptr,
-   int compression, png_charp key, png_charp lang, png_charp lang_key,
-   png_charp text));
-#endif
-
-#if defined(PNG_TEXT_SUPPORTED)  /* Added at version 1.0.14 and 1.2.4 */
-PNG_EXTERN int png_set_text_2 PNGARG((png_structp png_ptr,
-   png_infop info_ptr, png_textp text_ptr, int num_text));
-#endif
-
-#if defined(PNG_WRITE_oFFs_SUPPORTED)
-PNG_EXTERN void png_write_oFFs PNGARG((png_structp png_ptr,
-   png_int_32 x_offset, png_int_32 y_offset, int unit_type));
-#endif
-
-#if defined(PNG_WRITE_pCAL_SUPPORTED)
-PNG_EXTERN void png_write_pCAL PNGARG((png_structp png_ptr, png_charp purpose,
-   png_int_32 X0, png_int_32 X1, int type, int nparams,
-   png_charp units, png_charpp params));
-#endif
-
-#if defined(PNG_WRITE_pHYs_SUPPORTED)
-PNG_EXTERN void png_write_pHYs PNGARG((png_structp png_ptr,
-   png_uint_32 x_pixels_per_unit, png_uint_32 y_pixels_per_unit,
-   int unit_type));
-#endif
-
-#if defined(PNG_WRITE_tIME_SUPPORTED)
-PNG_EXTERN void png_write_tIME PNGARG((png_structp png_ptr,
-   png_timep mod_time));
-#endif
-
-#if defined(PNG_WRITE_sCAL_SUPPORTED)
-#if defined(PNG_FLOATING_POINT_SUPPORTED) && !defined(PNG_NO_STDIO)
-PNG_EXTERN void png_write_sCAL PNGARG((png_structp png_ptr,
-   int unit, double width, double height));
-#else
-#ifdef PNG_FIXED_POINT_SUPPORTED
-PNG_EXTERN void png_write_sCAL_s PNGARG((png_structp png_ptr,
-   int unit, png_charp width, png_charp height));
-#endif
-#endif
-#endif
-
-/* Called when finished processing a row of data */
-PNG_EXTERN void png_write_finish_row PNGARG((png_structp png_ptr));
-
-/* Internal use only.   Called before first row of data */
-PNG_EXTERN void png_write_start_row PNGARG((png_structp png_ptr));
-
-#if defined(PNG_READ_GAMMA_SUPPORTED)
-PNG_EXTERN void png_build_gamma_table PNGARG((png_structp png_ptr));
-#endif
-
-/* combine a row of data, dealing with alpha, etc. if requested */
-PNG_EXTERN void png_combine_row PNGARG((png_structp png_ptr, png_bytep row,
-   int mask));
-
-#if defined(PNG_READ_INTERLACING_SUPPORTED)
-/* expand an interlaced row */
-/* OLD pre-1.0.9 interface:
-PNG_EXTERN void png_do_read_interlace PNGARG((png_row_infop row_info,
-   png_bytep row, int pass, png_uint_32 transformations));
- */
-PNG_EXTERN void png_do_read_interlace PNGARG((png_structp png_ptr));
-#endif
-
-/* GRR TO DO (2.0 or whenever):  simplify other internal calling interfaces */
-
-#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
-/* grab pixels out of a row for an interlaced pass */
-PNG_EXTERN void png_do_write_interlace PNGARG((png_row_infop row_info,
-   png_bytep row, int pass));
-#endif
-
-/* unfilter a row */
-PNG_EXTERN void png_read_filter_row PNGARG((png_structp png_ptr,
-   png_row_infop row_info, png_bytep row, png_bytep prev_row, int filter));
-
-/* Choose the best filter to use and filter the row data */
-PNG_EXTERN void png_write_find_filter PNGARG((png_structp png_ptr,
-   png_row_infop row_info));
-
-/* Write out the filtered row. */
-PNG_EXTERN void png_write_filtered_row PNGARG((png_structp png_ptr,
-   png_bytep filtered_row));
-/* finish a row while reading, dealing with interlacing passes, etc. */
-PNG_EXTERN void png_read_finish_row PNGARG((png_structp png_ptr));
-
-/* initialize the row buffers, etc. */
-PNG_EXTERN void png_read_start_row PNGARG((png_structp png_ptr));
-/* optional call to update the users info structure */
-PNG_EXTERN void png_read_transform_info PNGARG((png_structp png_ptr,
-   png_infop info_ptr));
-
-/* these are the functions that do the transformations */
-#if defined(PNG_READ_FILLER_SUPPORTED)
-PNG_EXTERN void png_do_read_filler PNGARG((png_row_infop row_info,
-   png_bytep row, png_uint_32 filler, png_uint_32 flags));
-#endif
-
-#if defined(PNG_READ_SWAP_ALPHA_SUPPORTED)
-PNG_EXTERN void png_do_read_swap_alpha PNGARG((png_row_infop row_info,
-   png_bytep row));
-#endif
-
-#if defined(PNG_WRITE_SWAP_ALPHA_SUPPORTED)
-PNG_EXTERN void png_do_write_swap_alpha PNGARG((png_row_infop row_info,
-   png_bytep row));
-#endif
-
-#if defined(PNG_READ_INVERT_ALPHA_SUPPORTED)
-PNG_EXTERN void png_do_read_invert_alpha PNGARG((png_row_infop row_info,
-   png_bytep row));
-#endif
-
-#if defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED)
-PNG_EXTERN void png_do_write_invert_alpha PNGARG((png_row_infop row_info,
-   png_bytep row));
-#endif
-
-#if defined(PNG_WRITE_FILLER_SUPPORTED) || \
-    defined(PNG_READ_STRIP_ALPHA_SUPPORTED)
-PNG_EXTERN void png_do_strip_filler PNGARG((png_row_infop row_info,
-   png_bytep row, png_uint_32 flags));
-#endif
-
-#if defined(PNG_READ_SWAP_SUPPORTED) || defined(PNG_WRITE_SWAP_SUPPORTED)
-PNG_EXTERN void png_do_swap PNGARG((png_row_infop row_info, png_bytep row));
-#endif
-
-#if defined(PNG_READ_PACKSWAP_SUPPORTED) || defined(PNG_WRITE_PACKSWAP_SUPPORTED)
-PNG_EXTERN void png_do_packswap PNGARG((png_row_infop row_info, png_bytep row));
-#endif
-
-#if defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
-PNG_EXTERN int png_do_rgb_to_gray PNGARG((png_structp png_ptr, png_row_infop
-   row_info, png_bytep row));
-#endif
-
-#if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED)
-PNG_EXTERN void png_do_gray_to_rgb PNGARG((png_row_infop row_info,
-   png_bytep row));
-#endif
-
-#if defined(PNG_READ_PACK_SUPPORTED)
-PNG_EXTERN void png_do_unpack PNGARG((png_row_infop row_info, png_bytep row));
-#endif
-
-#if defined(PNG_READ_SHIFT_SUPPORTED)
-PNG_EXTERN void png_do_unshift PNGARG((png_row_infop row_info, png_bytep row,
-   png_color_8p sig_bits));
-#endif
-
-#if defined(PNG_READ_INVERT_SUPPORTED) || defined(PNG_WRITE_INVERT_SUPPORTED)
-PNG_EXTERN void png_do_invert PNGARG((png_row_infop row_info, png_bytep row));
-#endif
-
-#if defined(PNG_READ_16_TO_8_SUPPORTED)
-PNG_EXTERN void png_do_chop PNGARG((png_row_infop row_info, png_bytep row));
-#endif
-
-#if defined(PNG_READ_DITHER_SUPPORTED)
-PNG_EXTERN void png_do_dither PNGARG((png_row_infop row_info,
-   png_bytep row, png_bytep palette_lookup, png_bytep dither_lookup));
-
-#  if defined(PNG_CORRECT_PALETTE_SUPPORTED)
-PNG_EXTERN void png_correct_palette PNGARG((png_structp png_ptr,
-   png_colorp palette, int num_palette));
-#  endif
-#endif
-
-#if defined(PNG_READ_BGR_SUPPORTED) || defined(PNG_WRITE_BGR_SUPPORTED)
-PNG_EXTERN void png_do_bgr PNGARG((png_row_infop row_info, png_bytep row));
-#endif
-
-#if defined(PNG_WRITE_PACK_SUPPORTED)
-PNG_EXTERN void png_do_pack PNGARG((png_row_infop row_info,
-   png_bytep row, png_uint_32 bit_depth));
-#endif
-
-#if defined(PNG_WRITE_SHIFT_SUPPORTED)
-PNG_EXTERN void png_do_shift PNGARG((png_row_infop row_info, png_bytep row,
-   png_color_8p bit_depth));
-#endif
-
-#if defined(PNG_READ_BACKGROUND_SUPPORTED)
-#if defined(PNG_READ_GAMMA_SUPPORTED)
-PNG_EXTERN void png_do_background PNGARG((png_row_infop row_info, png_bytep row,
-   png_color_16p trans_values, png_color_16p background,
-   png_color_16p background_1,
-   png_bytep gamma_table, png_bytep gamma_from_1, png_bytep gamma_to_1,
-   png_uint_16pp gamma_16, png_uint_16pp gamma_16_from_1,
-   png_uint_16pp gamma_16_to_1, int gamma_shift));
-#else
-PNG_EXTERN void png_do_background PNGARG((png_row_infop row_info, png_bytep row,
-   png_color_16p trans_values, png_color_16p background));
-#endif
-#endif
-
-#if defined(PNG_READ_GAMMA_SUPPORTED)
-PNG_EXTERN void png_do_gamma PNGARG((png_row_infop row_info, png_bytep row,
-   png_bytep gamma_table, png_uint_16pp gamma_16_table,
-   int gamma_shift));
-#endif
-
-#if defined(PNG_READ_EXPAND_SUPPORTED)
-PNG_EXTERN void png_do_expand_palette PNGARG((png_row_infop row_info,
-   png_bytep row, png_colorp palette, png_bytep trans, int num_trans));
-PNG_EXTERN void png_do_expand PNGARG((png_row_infop row_info,
-   png_bytep row, png_color_16p trans_value));
-#endif
-
-/* The following decodes the appropriate chunks, and does error correction,
- * then calls the appropriate callback for the chunk if it is valid.
- */
-
-/* decode the IHDR chunk */
-PNG_EXTERN void png_handle_IHDR PNGARG((png_structp png_ptr, png_infop info_ptr,
-   png_uint_32 length));
-PNG_EXTERN void png_handle_PLTE PNGARG((png_structp png_ptr, png_infop info_ptr,
-   png_uint_32 length));
-PNG_EXTERN void png_handle_IEND PNGARG((png_structp png_ptr, png_infop info_ptr,
-   png_uint_32 length));
-
-#if defined(PNG_READ_bKGD_SUPPORTED)
-PNG_EXTERN void png_handle_bKGD PNGARG((png_structp png_ptr, png_infop info_ptr,
-   png_uint_32 length));
-#endif
-
-#if defined(PNG_READ_cHRM_SUPPORTED)
-PNG_EXTERN void png_handle_cHRM PNGARG((png_structp png_ptr, png_infop info_ptr,
-   png_uint_32 length));
-#endif
-
-#if defined(PNG_READ_gAMA_SUPPORTED)
-PNG_EXTERN void png_handle_gAMA PNGARG((png_structp png_ptr, png_infop info_ptr,
-   png_uint_32 length));
-#endif
-
-#if defined(PNG_READ_hIST_SUPPORTED)
-PNG_EXTERN void png_handle_hIST PNGARG((png_structp png_ptr, png_infop info_ptr,
-   png_uint_32 length));
-#endif
-
-#if defined(PNG_READ_iCCP_SUPPORTED)
-extern void png_handle_iCCP PNGARG((png_structp png_ptr, png_infop info_ptr,
-   png_uint_32 length));
-#endif /* PNG_READ_iCCP_SUPPORTED */
-
-#if defined(PNG_READ_iTXt_SUPPORTED)
-PNG_EXTERN void png_handle_iTXt PNGARG((png_structp png_ptr, png_infop info_ptr,
-   png_uint_32 length));
-#endif
-
-#if defined(PNG_READ_oFFs_SUPPORTED)
-PNG_EXTERN void png_handle_oFFs PNGARG((png_structp png_ptr, png_infop info_ptr,
-   png_uint_32 length));
-#endif
-
-#if defined(PNG_READ_pCAL_SUPPORTED)
-PNG_EXTERN void png_handle_pCAL PNGARG((png_structp png_ptr, png_infop info_ptr,
-   png_uint_32 length));
-#endif
-
-#if defined(PNG_READ_pHYs_SUPPORTED)
-PNG_EXTERN void png_handle_pHYs PNGARG((png_structp png_ptr, png_infop info_ptr,
-   png_uint_32 length));
-#endif
-
-#if defined(PNG_READ_sBIT_SUPPORTED)
-PNG_EXTERN void png_handle_sBIT PNGARG((png_structp png_ptr, png_infop info_ptr,
-   png_uint_32 length));
-#endif
-
-#if defined(PNG_READ_sCAL_SUPPORTED)
-PNG_EXTERN void png_handle_sCAL PNGARG((png_structp png_ptr, png_infop info_ptr,
-   png_uint_32 length));
-#endif
-
-#if defined(PNG_READ_sPLT_SUPPORTED)
-extern void png_handle_sPLT PNGARG((png_structp png_ptr, png_infop info_ptr,
-   png_uint_32 length));
-#endif /* PNG_READ_sPLT_SUPPORTED */
-
-#if defined(PNG_READ_sRGB_SUPPORTED)
-PNG_EXTERN void png_handle_sRGB PNGARG((png_structp png_ptr, png_infop info_ptr,
-   png_uint_32 length));
-#endif
-
-#if defined(PNG_READ_tEXt_SUPPORTED)
-PNG_EXTERN void png_handle_tEXt PNGARG((png_structp png_ptr, png_infop info_ptr,
-   png_uint_32 length));
-#endif
-
-#if defined(PNG_READ_tIME_SUPPORTED)
-PNG_EXTERN void png_handle_tIME PNGARG((png_structp png_ptr, png_infop info_ptr,
-   png_uint_32 length));
-#endif
-
-#if defined(PNG_READ_tRNS_SUPPORTED)
-PNG_EXTERN void png_handle_tRNS PNGARG((png_structp png_ptr, png_infop info_ptr,
-   png_uint_32 length));
-#endif
-
-#if defined(PNG_READ_zTXt_SUPPORTED)
-PNG_EXTERN void png_handle_zTXt PNGARG((png_structp png_ptr, png_infop info_ptr,
-   png_uint_32 length));
-#endif
-
-PNG_EXTERN void png_handle_unknown PNGARG((png_structp png_ptr,
-   png_infop info_ptr, png_uint_32 length));
-
-PNG_EXTERN void png_check_chunk_name PNGARG((png_structp png_ptr,
-   png_bytep chunk_name));
-
-/* handle the transformations for reading and writing */
-PNG_EXTERN void png_do_read_transformations PNGARG((png_structp png_ptr));
-PNG_EXTERN void png_do_write_transformations PNGARG((png_structp png_ptr));
-
-PNG_EXTERN void png_init_read_transformations PNGARG((png_structp png_ptr));
-
-#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
-PNG_EXTERN void png_push_read_chunk PNGARG((png_structp png_ptr,
-   png_infop info_ptr));
-PNG_EXTERN void png_push_read_sig PNGARG((png_structp png_ptr,
-   png_infop info_ptr));
-PNG_EXTERN void png_push_check_crc PNGARG((png_structp png_ptr));
-PNG_EXTERN void png_push_crc_skip PNGARG((png_structp png_ptr,
-   png_uint_32 length));
-PNG_EXTERN void png_push_crc_finish PNGARG((png_structp png_ptr));
-PNG_EXTERN void png_push_save_buffer PNGARG((png_structp png_ptr));
-PNG_EXTERN void png_push_restore_buffer PNGARG((png_structp png_ptr,
-   png_bytep buffer, png_size_t buffer_length));
-PNG_EXTERN void png_push_read_IDAT PNGARG((png_structp png_ptr));
-PNG_EXTERN void png_process_IDAT_data PNGARG((png_structp png_ptr,
-   png_bytep buffer, png_size_t buffer_length));
-PNG_EXTERN void png_push_process_row PNGARG((png_structp png_ptr));
-PNG_EXTERN void png_push_handle_unknown PNGARG((png_structp png_ptr,
-   png_infop info_ptr, png_uint_32 length));
-PNG_EXTERN void png_push_have_info PNGARG((png_structp png_ptr,
-   png_infop info_ptr));
-PNG_EXTERN void png_push_have_end PNGARG((png_structp png_ptr,
-   png_infop info_ptr));
-PNG_EXTERN void png_push_have_row PNGARG((png_structp png_ptr, png_bytep row));
-PNG_EXTERN void png_push_read_end PNGARG((png_structp png_ptr,
-   png_infop info_ptr));
-PNG_EXTERN void png_process_some_data PNGARG((png_structp png_ptr,
-   png_infop info_ptr));
-PNG_EXTERN void png_read_push_finish_row PNGARG((png_structp png_ptr));
-#if defined(PNG_READ_tEXt_SUPPORTED)
-PNG_EXTERN void png_push_handle_tEXt PNGARG((png_structp png_ptr,
-   png_infop info_ptr, png_uint_32 length));
-PNG_EXTERN void png_push_read_tEXt PNGARG((png_structp png_ptr,
-   png_infop info_ptr));
-#endif
-#if defined(PNG_READ_zTXt_SUPPORTED)
-PNG_EXTERN void png_push_handle_zTXt PNGARG((png_structp png_ptr,
-   png_infop info_ptr, png_uint_32 length));
-PNG_EXTERN void png_push_read_zTXt PNGARG((png_structp png_ptr,
-   png_infop info_ptr));
-#endif
-#if defined(PNG_READ_iTXt_SUPPORTED)
-PNG_EXTERN void png_push_handle_iTXt PNGARG((png_structp png_ptr,
-   png_infop info_ptr, png_uint_32 length));
-PNG_EXTERN void png_push_read_iTXt PNGARG((png_structp png_ptr,
-   png_infop info_ptr));
-#endif
-
-#endif /* PNG_PROGRESSIVE_READ_SUPPORTED */
-
-#ifdef PNG_MNG_FEATURES_SUPPORTED
-PNG_EXTERN void png_do_read_intrapixel PNGARG((png_row_infop row_info,
-   png_bytep row));
-PNG_EXTERN void png_do_write_intrapixel PNGARG((png_row_infop row_info,
-   png_bytep row));
-#endif
-
-#if defined(PNG_ASSEMBLER_CODE_SUPPORTED)
-#if defined(PNG_MMX_CODE_SUPPORTED)
-/* png.c */ /* PRIVATE */
-PNG_EXTERN void png_init_mmx_flags PNGARG((png_structp png_ptr));
-#endif
-#endif
-
-#if defined(PNG_INCH_CONVERSIONS) && defined(PNG_FLOATING_POINT_SUPPORTED)
-PNG_EXTERN png_uint_32 png_get_pixels_per_inch PNGARG((png_structp png_ptr,
-png_infop info_ptr));
-
-PNG_EXTERN png_uint_32 png_get_x_pixels_per_inch PNGARG((png_structp png_ptr,
-png_infop info_ptr));
-
-PNG_EXTERN png_uint_32 png_get_y_pixels_per_inch PNGARG((png_structp png_ptr,
-png_infop info_ptr));
-
-PNG_EXTERN float png_get_x_offset_inches PNGARG((png_structp png_ptr,
-png_infop info_ptr));
-
-PNG_EXTERN float png_get_y_offset_inches PNGARG((png_structp png_ptr,
-png_infop info_ptr));
-
-#if defined(PNG_pHYs_SUPPORTED)
-PNG_EXTERN png_uint_32 png_get_pHYs_dpi PNGARG((png_structp png_ptr,
-png_infop info_ptr, png_uint_32 *res_x, png_uint_32 *res_y, int *unit_type));
-#endif /* PNG_pHYs_SUPPORTED */
-#endif  /* PNG_INCH_CONVERSIONS && PNG_FLOATING_POINT_SUPPORTED */
-
-/* Maintainer: Put new private prototypes here ^ and in libpngpf.3 */
-
-#endif /* PNG_INTERNAL */
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif /* PNG_VERSION_INFO_ONLY */
-/* do not put anything past this line */
-#endif /* PNG_H */
+\r
+/* png.h - header file for PNG reference library\r
+ *\r
+ * libpng version 1.4.3 - June 26, 2010\r
+ * Copyright (c) 1998-2010 Glenn Randers-Pehrson\r
+ * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)\r
+ * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)\r
+ *\r
+ * This code is released under the libpng license (See LICENSE, below)\r
+ *\r
+ * Authors and maintainers:\r
+ *  libpng versions 0.71, May 1995, through 0.88, January 1996: Guy Schalnat\r
+ *  libpng versions 0.89c, June 1996, through 0.96, May 1997: Andreas Dilger\r
+ *  libpng versions 0.97, January 1998, through 1.4.3 - June 26, 2010: Glenn\r
+ *  See also "Contributing Authors", below.\r
+ *\r
+ * Note about libpng version numbers:\r
+ *\r
+ *    Due to various miscommunications, unforeseen code incompatibilities\r
+ *    and occasional factors outside the authors' control, version numbering\r
+ *    on the library has not always been consistent and straightforward.\r
+ *    The following table summarizes matters since version 0.89c, which was\r
+ *    the first widely used release:\r
+ *\r
+ *    source                 png.h  png.h  shared-lib\r
+ *    version                string   int  version\r
+ *    -------                ------ -----  ----------\r
+ *    0.89c "1.0 beta 3"     0.89      89  1.0.89\r
+ *    0.90  "1.0 beta 4"     0.90      90  0.90  [should have been 2.0.90]\r
+ *    0.95  "1.0 beta 5"     0.95      95  0.95  [should have been 2.0.95]\r
+ *    0.96  "1.0 beta 6"     0.96      96  0.96  [should have been 2.0.96]\r
+ *    0.97b "1.00.97 beta 7" 1.00.97   97  1.0.1 [should have been 2.0.97]\r
+ *    0.97c                  0.97      97  2.0.97\r
+ *    0.98                   0.98      98  2.0.98\r
+ *    0.99                   0.99      98  2.0.99\r
+ *    0.99a-m                0.99      99  2.0.99\r
+ *    1.00                   1.00     100  2.1.0 [100 should be 10000]\r
+ *    1.0.0      (from here on, the   100  2.1.0 [100 should be 10000]\r
+ *    1.0.1       png.h string is   10001  2.1.0\r
+ *    1.0.1a-e    identical to the  10002  from here on, the shared library\r
+ *    1.0.2       source version)   10002  is 2.V where V is the source code\r
+ *    1.0.2a-b                      10003  version, except as noted.\r
+ *    1.0.3                         10003\r
+ *    1.0.3a-d                      10004\r
+ *    1.0.4                         10004\r
+ *    1.0.4a-f                      10005\r
+ *    1.0.5 (+ 2 patches)           10005\r
+ *    1.0.5a-d                      10006\r
+ *    1.0.5e-r                      10100 (not source compatible)\r
+ *    1.0.5s-v                      10006 (not binary compatible)\r
+ *    1.0.6 (+ 3 patches)           10006 (still binary incompatible)\r
+ *    1.0.6d-f                      10007 (still binary incompatible)\r
+ *    1.0.6g                        10007\r
+ *    1.0.6h                        10007  10.6h (testing xy.z so-numbering)\r
+ *    1.0.6i                        10007  10.6i\r
+ *    1.0.6j                        10007  2.1.0.6j (incompatible with 1.0.0)\r
+ *    1.0.7beta11-14        DLLNUM  10007  2.1.0.7beta11-14 (binary compatible)\r
+ *    1.0.7beta15-18           1    10007  2.1.0.7beta15-18 (binary compatible)\r
+ *    1.0.7rc1-2               1    10007  2.1.0.7rc1-2 (binary compatible)\r
+ *    1.0.7                    1    10007  (still compatible)\r
+ *    1.0.8beta1-4             1    10008  2.1.0.8beta1-4\r
+ *    1.0.8rc1                 1    10008  2.1.0.8rc1\r
+ *    1.0.8                    1    10008  2.1.0.8\r
+ *    1.0.9beta1-6             1    10009  2.1.0.9beta1-6\r
+ *    1.0.9rc1                 1    10009  2.1.0.9rc1\r
+ *    1.0.9beta7-10            1    10009  2.1.0.9beta7-10\r
+ *    1.0.9rc2                 1    10009  2.1.0.9rc2\r
+ *    1.0.9                    1    10009  2.1.0.9\r
+ *    1.0.10beta1              1    10010  2.1.0.10beta1\r
+ *    1.0.10rc1                1    10010  2.1.0.10rc1\r
+ *    1.0.10                   1    10010  2.1.0.10\r
+ *    1.0.11beta1-3            1    10011  2.1.0.11beta1-3\r
+ *    1.0.11rc1                1    10011  2.1.0.11rc1\r
+ *    1.0.11                   1    10011  2.1.0.11\r
+ *    1.0.12beta1-2            2    10012  2.1.0.12beta1-2\r
+ *    1.0.12rc1                2    10012  2.1.0.12rc1\r
+ *    1.0.12                   2    10012  2.1.0.12\r
+ *    1.1.0a-f                 -    10100  2.1.1.0a-f (branch abandoned)\r
+ *    1.2.0beta1-2             2    10200  2.1.2.0beta1-2\r
+ *    1.2.0beta3-5             3    10200  3.1.2.0beta3-5\r
+ *    1.2.0rc1                 3    10200  3.1.2.0rc1\r
+ *    1.2.0                    3    10200  3.1.2.0\r
+ *    1.2.1beta1-4             3    10201  3.1.2.1beta1-4\r
+ *    1.2.1rc1-2               3    10201  3.1.2.1rc1-2\r
+ *    1.2.1                    3    10201  3.1.2.1\r
+ *    1.2.2beta1-6            12    10202  12.so.0.1.2.2beta1-6\r
+ *    1.0.13beta1             10    10013  10.so.0.1.0.13beta1\r
+ *    1.0.13rc1               10    10013  10.so.0.1.0.13rc1\r
+ *    1.2.2rc1                12    10202  12.so.0.1.2.2rc1\r
+ *    1.0.13                  10    10013  10.so.0.1.0.13\r
+ *    1.2.2                   12    10202  12.so.0.1.2.2\r
+ *    1.2.3rc1-6              12    10203  12.so.0.1.2.3rc1-6\r
+ *    1.2.3                   12    10203  12.so.0.1.2.3\r
+ *    1.2.4beta1-3            13    10204  12.so.0.1.2.4beta1-3\r
+ *    1.0.14rc1               13    10014  10.so.0.1.0.14rc1\r
+ *    1.2.4rc1                13    10204  12.so.0.1.2.4rc1\r
+ *    1.0.14                  10    10014  10.so.0.1.0.14\r
+ *    1.2.4                   13    10204  12.so.0.1.2.4\r
+ *    1.2.5beta1-2            13    10205  12.so.0.1.2.5beta1-2\r
+ *    1.0.15rc1-3             10    10015  10.so.0.1.0.15rc1-3\r
+ *    1.2.5rc1-3              13    10205  12.so.0.1.2.5rc1-3\r
+ *    1.0.15                  10    10015  10.so.0.1.0.15\r
+ *    1.2.5                   13    10205  12.so.0.1.2.5\r
+ *    1.2.6beta1-4            13    10206  12.so.0.1.2.6beta1-4\r
+ *    1.0.16                  10    10016  10.so.0.1.0.16\r
+ *    1.2.6                   13    10206  12.so.0.1.2.6\r
+ *    1.2.7beta1-2            13    10207  12.so.0.1.2.7beta1-2\r
+ *    1.0.17rc1               10    10017  12.so.0.1.0.17rc1\r
+ *    1.2.7rc1                13    10207  12.so.0.1.2.7rc1\r
+ *    1.0.17                  10    10017  12.so.0.1.0.17\r
+ *    1.2.7                   13    10207  12.so.0.1.2.7\r
+ *    1.2.8beta1-5            13    10208  12.so.0.1.2.8beta1-5\r
+ *    1.0.18rc1-5             10    10018  12.so.0.1.0.18rc1-5\r
+ *    1.2.8rc1-5              13    10208  12.so.0.1.2.8rc1-5\r
+ *    1.0.18                  10    10018  12.so.0.1.0.18\r
+ *    1.2.8                   13    10208  12.so.0.1.2.8\r
+ *    1.2.9beta1-3            13    10209  12.so.0.1.2.9beta1-3\r
+ *    1.2.9beta4-11           13    10209  12.so.0.9[.0]\r
+ *    1.2.9rc1                13    10209  12.so.0.9[.0]\r
+ *    1.2.9                   13    10209  12.so.0.9[.0]\r
+ *    1.2.10beta1-7           13    10210  12.so.0.10[.0]\r
+ *    1.2.10rc1-2             13    10210  12.so.0.10[.0]\r
+ *    1.2.10                  13    10210  12.so.0.10[.0]\r
+ *    1.4.0beta1-5            14    10400  14.so.0.0[.0]\r
+ *    1.2.11beta1-4           13    10211  12.so.0.11[.0]\r
+ *    1.4.0beta7-8            14    10400  14.so.0.0[.0]\r
+ *    1.2.11                  13    10211  12.so.0.11[.0]\r
+ *    1.2.12                  13    10212  12.so.0.12[.0]\r
+ *    1.4.0beta9-14           14    10400  14.so.0.0[.0]\r
+ *    1.2.13                  13    10213  12.so.0.13[.0]\r
+ *    1.4.0beta15-36          14    10400  14.so.0.0[.0]\r
+ *    1.4.0beta37-87          14    10400  14.so.14.0[.0]\r
+ *    1.4.0rc01               14    10400  14.so.14.0[.0]\r
+ *    1.4.0beta88-109         14    10400  14.so.14.0[.0]\r
+ *    1.4.0rc02-08            14    10400  14.so.14.0[.0]\r
+ *    1.4.0                   14    10400  14.so.14.0[.0]\r
+ *    1.4.1beta01-03          14    10401  14.so.14.1[.0]\r
+ *    1.4.1rc01               14    10401  14.so.14.1[.0]\r
+ *    1.4.1beta04-12          14    10401  14.so.14.1[.0]\r
+ *    1.4.1rc02-04            14    10401  14.so.14.1[.0]\r
+ *    1.4.1                   14    10401  14.so.14.1[.0]\r
+ *    1.4.2beta01             14    10402  14.so.14.2[.0]\r
+ *    1.4.2rc02-06            14    10402  14.so.14.2[.0]\r
+ *    1.4.2                   14    10402  14.so.14.2[.0]\r
+ *    1.4.3beta01-05          14    10403  14.so.14.3[.0]\r
+ *    1.4.3rc01-03            14    10403  14.so.14.3[.0]\r
+ *    1.4.3                   14    10403  14.so.14.3[.0]\r
+ *\r
+ *    Henceforth the source version will match the shared-library major\r
+ *    and minor numbers; the shared-library major version number will be\r
+ *    used for changes in backward compatibility, as it is intended.  The\r
+ *    PNG_LIBPNG_VER macro, which is not used within libpng but is available\r
+ *    for applications, is an unsigned integer of the form xyyzz corresponding\r
+ *    to the source version x.y.z (leading zeros in y and z).  Beta versions\r
+ *    were given the previous public release number plus a letter, until\r
+ *    version 1.0.6j; from then on they were given the upcoming public\r
+ *    release number plus "betaNN" or "rcN".\r
+ *\r
+ *    Binary incompatibility exists only when applications make direct access\r
+ *    to the info_ptr or png_ptr members through png.h, and the compiled\r
+ *    application is loaded with a different version of the library.\r
+ *\r
+ *    DLLNUM will change each time there are forward or backward changes\r
+ *    in binary compatibility (e.g., when a new feature is added).\r
+ *\r
+ * See libpng.txt or libpng.3 for more information.  The PNG specification\r
+ * is available as a W3C Recommendation and as an ISO Specification,\r
+ * <http://www.w3.org/TR/2003/REC-PNG-20031110/\r
+ */\r
+\r
+/*\r
+ * COPYRIGHT NOTICE, DISCLAIMER, and LICENSE:\r
+ *\r
+ * If you modify libpng you may insert additional notices immediately following\r
+ * this sentence.\r
+ *\r
+ * This code is released under the libpng license.\r
+ *\r
+ * libpng versions 1.2.6, August 15, 2004, through 1.4.3, June 26, 2010, are\r
+ * Copyright (c) 2004, 2006-2010 Glenn Randers-Pehrson, and are\r
+ * distributed according to the same disclaimer and license as libpng-1.2.5\r
+ * with the following individual added to the list of Contributing Authors:\r
+ *\r
+ *    Cosmin Truta\r
+ *\r
+ * libpng versions 1.0.7, July 1, 2000, through 1.2.5, October 3, 2002, are\r
+ * Copyright (c) 2000-2002 Glenn Randers-Pehrson, and are\r
+ * distributed according to the same disclaimer and license as libpng-1.0.6\r
+ * with the following individuals added to the list of Contributing Authors:\r
+ *\r
+ *    Simon-Pierre Cadieux\r
+ *    Eric S. Raymond\r
+ *    Gilles Vollant\r
+ *\r
+ * and with the following additions to the disclaimer:\r
+ *\r
+ *    There is no warranty against interference with your enjoyment of the\r
+ *    library or against infringement.  There is no warranty that our\r
+ *    efforts or the library will fulfill any of your particular purposes\r
+ *    or needs.  This library is provided with all faults, and the entire\r
+ *    risk of satisfactory quality, performance, accuracy, and effort is with\r
+ *    the user.\r
+ *\r
+ * libpng versions 0.97, January 1998, through 1.0.6, March 20, 2000, are\r
+ * Copyright (c) 1998, 1999, 2000 Glenn Randers-Pehrson, and are\r
+ * distributed according to the same disclaimer and license as libpng-0.96,\r
+ * with the following individuals added to the list of Contributing Authors:\r
+ *\r
+ *    Tom Lane\r
+ *    Glenn Randers-Pehrson\r
+ *    Willem van Schaik\r
+ *\r
+ * libpng versions 0.89, June 1996, through 0.96, May 1997, are\r
+ * Copyright (c) 1996, 1997 Andreas Dilger\r
+ * Distributed according to the same disclaimer and license as libpng-0.88,\r
+ * with the following individuals added to the list of Contributing Authors:\r
+ *\r
+ *    John Bowler\r
+ *    Kevin Bracey\r
+ *    Sam Bushell\r
+ *    Magnus Holmgren\r
+ *    Greg Roelofs\r
+ *    Tom Tanner\r
+ *\r
+ * libpng versions 0.5, May 1995, through 0.88, January 1996, are\r
+ * Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.\r
+ *\r
+ * For the purposes of this copyright and license, "Contributing Authors"\r
+ * is defined as the following set of individuals:\r
+ *\r
+ *    Andreas Dilger\r
+ *    Dave Martindale\r
+ *    Guy Eric Schalnat\r
+ *    Paul Schmidt\r
+ *    Tim Wegner\r
+ *\r
+ * The PNG Reference Library is supplied "AS IS".  The Contributing Authors\r
+ * and Group 42, Inc. disclaim all warranties, expressed or implied,\r
+ * including, without limitation, the warranties of merchantability and of\r
+ * fitness for any purpose.  The Contributing Authors and Group 42, Inc.\r
+ * assume no liability for direct, indirect, incidental, special, exemplary,\r
+ * or consequential damages, which may result from the use of the PNG\r
+ * Reference Library, even if advised of the possibility of such damage.\r
+ *\r
+ * Permission is hereby granted to use, copy, modify, and distribute this\r
+ * source code, or portions hereof, for any purpose, without fee, subject\r
+ * to the following restrictions:\r
+ *\r
+ * 1. The origin of this source code must not be misrepresented.\r
+ *\r
+ * 2. Altered versions must be plainly marked as such and\r
+ * must not be misrepresented as being the original source.\r
+ *\r
+ * 3. This Copyright notice may not be removed or altered from\r
+ *    any source or altered source distribution.\r
+ *\r
+ * The Contributing Authors and Group 42, Inc. specifically permit, without\r
+ * fee, and encourage the use of this source code as a component to\r
+ * supporting the PNG file format in commercial products.  If you use this\r
+ * source code in a product, acknowledgment is not required but would be\r
+ * appreciated.\r
+ */\r
+\r
+/*\r
+ * A "png_get_copyright" function is available, for convenient use in "about"\r
+ * boxes and the like:\r
+ *\r
+ *     printf("%s",png_get_copyright(NULL));\r
+ *\r
+ * Also, the PNG logo (in PNG format, of course) is supplied in the\r
+ * files "pngbar.png" and "pngbar.jpg (88x31) and "pngnow.png" (98x31).\r
+ */\r
+\r
+/*\r
+ * Libpng is OSI Certified Open Source Software.  OSI Certified is a\r
+ * certification mark of the Open Source Initiative.\r
+ */\r
+\r
+/*\r
+ * The contributing authors would like to thank all those who helped\r
+ * with testing, bug fixes, and patience.  This wouldn't have been\r
+ * possible without all of you.\r
+ *\r
+ * Thanks to Frank J. T. Wojcik for helping with the documentation.\r
+ */\r
+\r
+/*\r
+ * Y2K compliance in libpng:\r
+ * =========================\r
+ *\r
+ *    June 26, 2010\r
+ *\r
+ *    Since the PNG Development group is an ad-hoc body, we can't make\r
+ *    an official declaration.\r
+ *\r
+ *    This is your unofficial assurance that libpng from version 0.71 and\r
+ *    upward through 1.4.3 are Y2K compliant.  It is my belief that earlier\r
+ *    versions were also Y2K compliant.\r
+ *\r
+ *    Libpng only has three year fields.  One is a 2-byte unsigned integer\r
+ *    that will hold years up to 65535.  The other two hold the date in text\r
+ *    format, and will hold years up to 9999.\r
+ *\r
+ *    The integer is\r
+ *        "png_uint_16 year" in png_time_struct.\r
+ *\r
+ *    The strings are\r
+ *        "png_charp time_buffer" in png_struct and\r
+ *        "near_time_buffer", which is a local character string in png.c.\r
+ *\r
+ *    There are seven time-related functions:\r
+ *        png.c: png_convert_to_rfc_1123() in png.c\r
+ *          (formerly png_convert_to_rfc_1152() in error)\r
+ *        png_convert_from_struct_tm() in pngwrite.c, called in pngwrite.c\r
+ *        png_convert_from_time_t() in pngwrite.c\r
+ *        png_get_tIME() in pngget.c\r
+ *        png_handle_tIME() in pngrutil.c, called in pngread.c\r
+ *        png_set_tIME() in pngset.c\r
+ *        png_write_tIME() in pngwutil.c, called in pngwrite.c\r
+ *\r
+ *    All handle dates properly in a Y2K environment.  The\r
+ *    png_convert_from_time_t() function calls gmtime() to convert from system\r
+ *    clock time, which returns (year - 1900), which we properly convert to\r
+ *    the full 4-digit year.  There is a possibility that applications using\r
+ *    libpng are not passing 4-digit years into the png_convert_to_rfc_1123()\r
+ *    function, or that they are incorrectly passing only a 2-digit year\r
+ *    instead of "year - 1900" into the png_convert_from_struct_tm() function,\r
+ *    but this is not under our control.  The libpng documentation has always\r
+ *    stated that it works with 4-digit years, and the APIs have been\r
+ *    documented as such.\r
+ *\r
+ *    The tIME chunk itself is also Y2K compliant.  It uses a 2-byte unsigned\r
+ *    integer to hold the year, and can hold years as large as 65535.\r
+ *\r
+ *    zlib, upon which libpng depends, is also Y2K compliant.  It contains\r
+ *    no date-related code.\r
+ *\r
+ *       Glenn Randers-Pehrson\r
+ *       libpng maintainer\r
+ *       PNG Development Group\r
+ */\r
+\r
+#ifndef PNG_H\r
+#define PNG_H\r
+\r
+/* This is not the place to learn how to use libpng.  The file libpng.txt\r
+ * describes how to use libpng, and the file example.c summarizes it\r
+ * with some code on which to build.  This file is useful for looking\r
+ * at the actual function definitions and structure components.\r
+ */\r
+\r
+/* Version information for png.h - this should match the version in png.c */\r
+#define PNG_LIBPNG_VER_STRING "1.4.3"\r
+#define PNG_HEADER_VERSION_STRING \\r
+   " libpng version 1.4.3 - June 26, 2010\n"\r
+\r
+#define PNG_LIBPNG_VER_SONUM   14\r
+#define PNG_LIBPNG_VER_DLLNUM  14\r
+\r
+/* These should match the first 3 components of PNG_LIBPNG_VER_STRING: */\r
+#define PNG_LIBPNG_VER_MAJOR   1\r
+#define PNG_LIBPNG_VER_MINOR   4\r
+#define PNG_LIBPNG_VER_RELEASE 3\r
+/* This should match the numeric part of the final component of\r
+ * PNG_LIBPNG_VER_STRING, omitting any leading zero:\r
+ */\r
+\r
+#define PNG_LIBPNG_VER_BUILD  0\r
+\r
+/* Release Status */\r
+#define PNG_LIBPNG_BUILD_ALPHA    1\r
+#define PNG_LIBPNG_BUILD_BETA     2\r
+#define PNG_LIBPNG_BUILD_RC       3\r
+#define PNG_LIBPNG_BUILD_STABLE   4\r
+#define PNG_LIBPNG_BUILD_RELEASE_STATUS_MASK 7\r
+\r
+/* Release-Specific Flags */\r
+#define PNG_LIBPNG_BUILD_PATCH    8 /* Can be OR'ed with\r
+                                       PNG_LIBPNG_BUILD_STABLE only */\r
+#define PNG_LIBPNG_BUILD_PRIVATE 16 /* Cannot be OR'ed with\r
+                                       PNG_LIBPNG_BUILD_SPECIAL */\r
+#define PNG_LIBPNG_BUILD_SPECIAL 32 /* Cannot be OR'ed with\r
+                                       PNG_LIBPNG_BUILD_PRIVATE */\r
+\r
+#define PNG_LIBPNG_BUILD_BASE_TYPE PNG_LIBPNG_BUILD_BETA\r
+\r
+/* Careful here.  At one time, Guy wanted to use 082, but that would be octal.\r
+ * We must not include leading zeros.\r
+ * Versions 0.7 through 1.0.0 were in the range 0 to 100 here (only\r
+ * version 1.0.0 was mis-numbered 100 instead of 10000).  From\r
+ * version 1.0.1 it's    xxyyzz, where x=major, y=minor, z=release\r
+ */\r
+#define PNG_LIBPNG_VER 10403 /* 1.4.3 */\r
+\r
+#ifndef PNG_VERSION_INFO_ONLY\r
+/* Include the compression library's header */\r
+#include "zlib.h"\r
+#endif\r
+\r
+/* Include all user configurable info, including optional assembler routines */\r
+#include "pngconf.h"\r
+\r
+/*\r
+ * Added at libpng-1.2.8\r
+ *\r
+ * Ref MSDN: Private as priority over Special\r
+ * VS_FF_PRIVATEBUILD File *was not* built using standard release\r
+ * procedures. If this value is given, the StringFileInfo block must\r
+ * contain a PrivateBuild string.\r
+ *\r
+ * VS_FF_SPECIALBUILD File *was* built by the original company using\r
+ * standard release procedures but is a variation of the standard\r
+ * file of the same version number. If this value is given, the\r
+ * StringFileInfo block must contain a SpecialBuild string.\r
+ */\r
+\r
+#ifdef PNG_USER_PRIVATEBUILD\r
+#  define PNG_LIBPNG_BUILD_TYPE \\r
+          (PNG_LIBPNG_BUILD_BASE_TYPE | PNG_LIBPNG_BUILD_PRIVATE)\r
+#else\r
+#  ifdef PNG_LIBPNG_SPECIALBUILD\r
+#    define PNG_LIBPNG_BUILD_TYPE \\r
+            (PNG_LIBPNG_BUILD_BASE_TYPE | PNG_LIBPNG_BUILD_SPECIAL)\r
+#  else\r
+#    define PNG_LIBPNG_BUILD_TYPE (PNG_LIBPNG_BUILD_BASE_TYPE)\r
+#  endif\r
+#endif\r
+\r
+#ifndef PNG_VERSION_INFO_ONLY\r
+\r
+/* Inhibit C++ name-mangling for libpng functions but not for system calls. */\r
+#ifdef __cplusplus\r
+extern "C" {\r
+#endif /* __cplusplus */\r
+\r
+/* This file is arranged in several sections.  The first section contains\r
+ * structure and type definitions.  The second section contains the external\r
+ * library functions, while the third has the internal library functions,\r
+ * which applications aren't expected to use directly.\r
+ */\r
+\r
+/* Variables declared in png.c - only it needs to define PNG_NO_EXTERN */\r
+#if !defined(PNG_NO_EXTERN) || defined(PNG_ALWAYS_EXTERN)\r
+/* Version information for C files, stored in png.c.  This had better match\r
+ * the version above.\r
+ */\r
+#define png_libpng_ver png_get_header_ver(NULL)\r
+\r
+#endif /* PNG_NO_EXTERN */\r
+\r
+/* Three color definitions.  The order of the red, green, and blue, (and the\r
+ * exact size) is not important, although the size of the fields need to\r
+ * be png_byte or png_uint_16 (as defined below).\r
+ */\r
+typedef struct png_color_struct\r
+{\r
+   png_byte red;\r
+   png_byte green;\r
+   png_byte blue;\r
+} png_color;\r
+typedef png_color FAR * png_colorp;\r
+typedef png_color FAR * FAR * png_colorpp;\r
+\r
+typedef struct png_color_16_struct\r
+{\r
+   png_byte index;    /* used for palette files */\r
+   png_uint_16 red;   /* for use in red green blue files */\r
+   png_uint_16 green;\r
+   png_uint_16 blue;\r
+   png_uint_16 gray;  /* for use in grayscale files */\r
+} png_color_16;\r
+typedef png_color_16 FAR * png_color_16p;\r
+typedef png_color_16 FAR * FAR * png_color_16pp;\r
+\r
+typedef struct png_color_8_struct\r
+{\r
+   png_byte red;   /* for use in red green blue files */\r
+   png_byte green;\r
+   png_byte blue;\r
+   png_byte gray;  /* for use in grayscale files */\r
+   png_byte alpha; /* for alpha channel files */\r
+} png_color_8;\r
+typedef png_color_8 FAR * png_color_8p;\r
+typedef png_color_8 FAR * FAR * png_color_8pp;\r
+\r
+/*\r
+ * The following two structures are used for the in-core representation\r
+ * of sPLT chunks.\r
+ */\r
+typedef struct png_sPLT_entry_struct\r
+{\r
+   png_uint_16 red;\r
+   png_uint_16 green;\r
+   png_uint_16 blue;\r
+   png_uint_16 alpha;\r
+   png_uint_16 frequency;\r
+} png_sPLT_entry;\r
+typedef png_sPLT_entry FAR * png_sPLT_entryp;\r
+typedef png_sPLT_entry FAR * FAR * png_sPLT_entrypp;\r
+\r
+/*  When the depth of the sPLT palette is 8 bits, the color and alpha samples\r
+ *  occupy the LSB of their respective members, and the MSB of each member\r
+ *  is zero-filled.  The frequency member always occupies the full 16 bits.\r
+ */\r
+\r
+typedef struct png_sPLT_struct\r
+{\r
+   png_charp name;           /* palette name */\r
+   png_byte depth;           /* depth of palette samples */\r
+   png_sPLT_entryp entries;  /* palette entries */\r
+   png_int_32 nentries;      /* number of palette entries */\r
+} png_sPLT_t;\r
+typedef png_sPLT_t FAR * png_sPLT_tp;\r
+typedef png_sPLT_t FAR * FAR * png_sPLT_tpp;\r
+\r
+#ifdef PNG_TEXT_SUPPORTED\r
+/* png_text holds the contents of a text/ztxt/itxt chunk in a PNG file,\r
+ * and whether that contents is compressed or not.  The "key" field\r
+ * points to a regular zero-terminated C string.  The "text", "lang", and\r
+ * "lang_key" fields can be regular C strings, empty strings, or NULL pointers.\r
+ * However, the * structure returned by png_get_text() will always contain\r
+ * regular zero-terminated C strings (possibly empty), never NULL pointers,\r
+ * so they can be safely used in printf() and other string-handling functions.\r
+ */\r
+typedef struct png_text_struct\r
+{\r
+   int  compression;       /* compression value:\r
+                             -1: tEXt, none\r
+                              0: zTXt, deflate\r
+                              1: iTXt, none\r
+                              2: iTXt, deflate  */\r
+   png_charp key;          /* keyword, 1-79 character description of "text" */\r
+   png_charp text;         /* comment, may be an empty string (ie "")\r
+                              or a NULL pointer */\r
+   png_size_t text_length; /* length of the text string */\r
+#ifdef PNG_iTXt_SUPPORTED\r
+   png_size_t itxt_length; /* length of the itxt string */\r
+   png_charp lang;         /* language code, 0-79 characters\r
+                              or a NULL pointer */\r
+   png_charp lang_key;     /* keyword translated UTF-8 string, 0 or more\r
+                              chars or a NULL pointer */\r
+#endif\r
+} png_text;\r
+typedef png_text FAR * png_textp;\r
+typedef png_text FAR * FAR * png_textpp;\r
+#endif\r
+\r
+/* Supported compression types for text in PNG files (tEXt, and zTXt).\r
+ * The values of the PNG_TEXT_COMPRESSION_ defines should NOT be changed. */\r
+#define PNG_TEXT_COMPRESSION_NONE_WR -3\r
+#define PNG_TEXT_COMPRESSION_zTXt_WR -2\r
+#define PNG_TEXT_COMPRESSION_NONE    -1\r
+#define PNG_TEXT_COMPRESSION_zTXt     0\r
+#define PNG_ITXT_COMPRESSION_NONE     1\r
+#define PNG_ITXT_COMPRESSION_zTXt     2\r
+#define PNG_TEXT_COMPRESSION_LAST     3  /* Not a valid value */\r
+\r
+/* png_time is a way to hold the time in an machine independent way.\r
+ * Two conversions are provided, both from time_t and struct tm.  There\r
+ * is no portable way to convert to either of these structures, as far\r
+ * as I know.  If you know of a portable way, send it to me.  As a side\r
+ * note - PNG has always been Year 2000 compliant!\r
+ */\r
+typedef struct png_time_struct\r
+{\r
+   png_uint_16 year; /* full year, as in, 1995 */\r
+   png_byte month;   /* month of year, 1 - 12 */\r
+   png_byte day;     /* day of month, 1 - 31 */\r
+   png_byte hour;    /* hour of day, 0 - 23 */\r
+   png_byte minute;  /* minute of hour, 0 - 59 */\r
+   png_byte second;  /* second of minute, 0 - 60 (for leap seconds) */\r
+} png_time;\r
+typedef png_time FAR * png_timep;\r
+typedef png_time FAR * FAR * png_timepp;\r
+\r
+#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED) || \\r
+ defined(PNG_HANDLE_AS_UNKNOWN_SUPPORTED)\r
+/* png_unknown_chunk is a structure to hold queued chunks for which there is\r
+ * no specific support.  The idea is that we can use this to queue\r
+ * up private chunks for output even though the library doesn't actually\r
+ * know about their semantics.\r
+ */\r
+typedef struct png_unknown_chunk_t\r
+{\r
+    png_byte name[5];\r
+    png_byte *data;\r
+    png_size_t size;\r
+\r
+    /* libpng-using applications should NOT directly modify this byte. */\r
+    png_byte location; /* mode of operation at read time */\r
+}\r
+png_unknown_chunk;\r
+typedef png_unknown_chunk FAR * png_unknown_chunkp;\r
+typedef png_unknown_chunk FAR * FAR * png_unknown_chunkpp;\r
+#endif\r
+\r
+/* png_info is a structure that holds the information in a PNG file so\r
+ * that the application can find out the characteristics of the image.\r
+ * If you are reading the file, this structure will tell you what is\r
+ * in the PNG file.  If you are writing the file, fill in the information\r
+ * you want to put into the PNG file, then call png_write_info().\r
+ * The names chosen should be very close to the PNG specification, so\r
+ * consult that document for information about the meaning of each field.\r
+ *\r
+ * With libpng < 0.95, it was only possible to directly set and read the\r
+ * the values in the png_info_struct, which meant that the contents and\r
+ * order of the values had to remain fixed.  With libpng 0.95 and later,\r
+ * however, there are now functions that abstract the contents of\r
+ * png_info_struct from the application, so this makes it easier to use\r
+ * libpng with dynamic libraries, and even makes it possible to use\r
+ * libraries that don't have all of the libpng ancillary chunk-handing\r
+ * functionality.\r
+ *\r
+ * In any case, the order of the parameters in png_info_struct should NOT\r
+ * be changed for as long as possible to keep compatibility with applications\r
+ * that use the old direct-access method with png_info_struct.\r
+ *\r
+ * The following members may have allocated storage attached that should be\r
+ * cleaned up before the structure is discarded: palette, trans, text,\r
+ * pcal_purpose, pcal_units, pcal_params, hist, iccp_name, iccp_profile,\r
+ * splt_palettes, scal_unit, row_pointers, and unknowns.   By default, these\r
+ * are automatically freed when the info structure is deallocated, if they were\r
+ * allocated internally by libpng.  This behavior can be changed by means\r
+ * of the png_data_freer() function.\r
+ *\r
+ * More allocation details: all the chunk-reading functions that\r
+ * change these members go through the corresponding png_set_*\r
+ * functions.  A function to clear these members is available: see\r
+ * png_free_data().  The png_set_* functions do not depend on being\r
+ * able to point info structure members to any of the storage they are\r
+ * passed (they make their own copies), EXCEPT that the png_set_text\r
+ * functions use the same storage passed to them in the text_ptr or\r
+ * itxt_ptr structure argument, and the png_set_rows and png_set_unknowns\r
+ * functions do not make their own copies.\r
+ */\r
+typedef struct png_info_struct\r
+{\r
+   /* the following are necessary for every PNG file */\r
+   png_uint_32 width PNG_DEPSTRUCT;  /* width of image in pixels (from IHDR) */\r
+   png_uint_32 height PNG_DEPSTRUCT; /* height of image in pixels (from IHDR) */\r
+   png_uint_32 valid PNG_DEPSTRUCT;  /* valid chunk data (see PNG_INFO_\r
+                                        below) */\r
+   png_size_t rowbytes PNG_DEPSTRUCT; /* bytes needed to hold an untransformed\r
+                                         row */\r
+   png_colorp palette PNG_DEPSTRUCT;      /* array of color values\r
+                                             (valid & PNG_INFO_PLTE) */\r
+   png_uint_16 num_palette PNG_DEPSTRUCT; /* number of color entries in\r
+                                             "palette" (PLTE) */\r
+   png_uint_16 num_trans PNG_DEPSTRUCT;   /* number of transparent palette\r
+                                             color (tRNS) */\r
+   png_byte bit_depth PNG_DEPSTRUCT;      /* 1, 2, 4, 8, or 16 bits/channel\r
+                                             (from IHDR) */\r
+   png_byte color_type PNG_DEPSTRUCT;     /* see PNG_COLOR_TYPE_ below\r
+                                             (from IHDR) */\r
+   /* The following three should have been named *_method not *_type */\r
+   png_byte compression_type PNG_DEPSTRUCT; /* must be\r
+                                             PNG_COMPRESSION_TYPE_BASE (IHDR) */\r
+   png_byte filter_type PNG_DEPSTRUCT;    /* must be PNG_FILTER_TYPE_BASE\r
+                                             (from IHDR) */\r
+   png_byte interlace_type PNG_DEPSTRUCT; /* One of PNG_INTERLACE_NONE,\r
+                                             PNG_INTERLACE_ADAM7 */\r
+\r
+   /* The following is informational only on read, and not used on writes. */\r
+   png_byte channels PNG_DEPSTRUCT;       /* number of data channels per\r
+                                             pixel (1, 2, 3, 4) */\r
+   png_byte pixel_depth PNG_DEPSTRUCT;    /* number of bits per pixel */\r
+   png_byte spare_byte PNG_DEPSTRUCT;     /* to align the data, and for\r
+                                             future use */\r
+   png_byte signature[8] PNG_DEPSTRUCT;   /* magic bytes read by libpng\r
+                                             from start of file */\r
+\r
+   /* The rest of the data is optional.  If you are reading, check the\r
+    * valid field to see if the information in these are valid.  If you\r
+    * are writing, set the valid field to those chunks you want written,\r
+    * and initialize the appropriate fields below.\r
+    */\r
+\r
+#if defined(PNG_gAMA_SUPPORTED) && defined(PNG_FLOATING_POINT_SUPPORTED)\r
+   /* The gAMA chunk describes the gamma characteristics of the system\r
+    * on which the image was created, normally in the range [1.0, 2.5].\r
+    * Data is valid if (valid & PNG_INFO_gAMA) is non-zero.\r
+    */\r
+   float gamma PNG_DEPSTRUCT; /* gamma value of image,\r
+                                 if (valid & PNG_INFO_gAMA) */\r
+#endif\r
+\r
+#ifdef PNG_sRGB_SUPPORTED\r
+    /* GR-P, 0.96a */\r
+    /* Data valid if (valid & PNG_INFO_sRGB) non-zero. */\r
+   png_byte srgb_intent PNG_DEPSTRUCT; /* sRGB rendering intent\r
+                                          [0, 1, 2, or 3] */\r
+#endif\r
+\r
+#ifdef PNG_TEXT_SUPPORTED\r
+   /* The tEXt, and zTXt chunks contain human-readable textual data in\r
+    * uncompressed, compressed, and optionally compressed forms, respectively.\r
+    * The data in "text" is an array of pointers to uncompressed,\r
+    * null-terminated C strings. Each chunk has a keyword that describes the\r
+    * textual data contained in that chunk.  Keywords are not required to be\r
+    * unique, and the text string may be empty.  Any number of text chunks may\r
+    * be in an image.\r
+    */\r
+   int num_text PNG_DEPSTRUCT; /* number of comments read/to write */\r
+   int max_text PNG_DEPSTRUCT; /* current size of text array */\r
+   png_textp text PNG_DEPSTRUCT; /* array of comments read/to write */\r
+#endif /* PNG_TEXT_SUPPORTED */\r
+\r
+#ifdef PNG_tIME_SUPPORTED\r
+   /* The tIME chunk holds the last time the displayed image data was\r
+    * modified.  See the png_time struct for the contents of this struct.\r
+    */\r
+   png_time mod_time PNG_DEPSTRUCT;\r
+#endif\r
+\r
+#ifdef PNG_sBIT_SUPPORTED\r
+   /* The sBIT chunk specifies the number of significant high-order bits\r
+    * in the pixel data.  Values are in the range [1, bit_depth], and are\r
+    * only specified for the channels in the pixel data.  The contents of\r
+    * the low-order bits is not specified.  Data is valid if\r
+    * (valid & PNG_INFO_sBIT) is non-zero.\r
+    */\r
+   png_color_8 sig_bit PNG_DEPSTRUCT; /* significant bits in color channels */\r
+#endif\r
+\r
+#if defined(PNG_tRNS_SUPPORTED) || defined(PNG_READ_EXPAND_SUPPORTED) || \\r
+defined(PNG_READ_BACKGROUND_SUPPORTED)\r
+   /* The tRNS chunk supplies transparency data for paletted images and\r
+    * other image types that don't need a full alpha channel.  There are\r
+    * "num_trans" transparency values for a paletted image, stored in the\r
+    * same order as the palette colors, starting from index 0.  Values\r
+    * for the data are in the range [0, 255], ranging from fully transparent\r
+    * to fully opaque, respectively.  For non-paletted images, there is a\r
+    * single color specified that should be treated as fully transparent.\r
+    * Data is valid if (valid & PNG_INFO_tRNS) is non-zero.\r
+    */\r
+   png_bytep trans_alpha PNG_DEPSTRUCT;    /* alpha values for paletted\r
+                                              image */\r
+   png_color_16 trans_color PNG_DEPSTRUCT; /* transparent color for\r
+                                              non-palette image */\r
+#endif\r
+\r
+#if defined(PNG_bKGD_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)\r
+   /* The bKGD chunk gives the suggested image background color if the\r
+    * display program does not have its own background color and the image\r
+    * is needs to composited onto a background before display.  The colors\r
+    * in "background" are normally in the same color space/depth as the\r
+    * pixel data.  Data is valid if (valid & PNG_INFO_bKGD) is non-zero.\r
+    */\r
+   png_color_16 background PNG_DEPSTRUCT;\r
+#endif\r
+\r
+#ifdef PNG_oFFs_SUPPORTED\r
+   /* The oFFs chunk gives the offset in "offset_unit_type" units rightwards\r
+    * and downwards from the top-left corner of the display, page, or other\r
+    * application-specific co-ordinate space.  See the PNG_OFFSET_ defines\r
+    * below for the unit types.  Valid if (valid & PNG_INFO_oFFs) non-zero.\r
+    */\r
+   png_int_32 x_offset PNG_DEPSTRUCT; /* x offset on page */\r
+   png_int_32 y_offset PNG_DEPSTRUCT; /* y offset on page */\r
+   png_byte offset_unit_type PNG_DEPSTRUCT; /* offset units type */\r
+#endif\r
+\r
+#ifdef PNG_pHYs_SUPPORTED\r
+   /* The pHYs chunk gives the physical pixel density of the image for\r
+    * display or printing in "phys_unit_type" units (see PNG_RESOLUTION_\r
+    * defines below).  Data is valid if (valid & PNG_INFO_pHYs) is non-zero.\r
+    */\r
+   png_uint_32 x_pixels_per_unit PNG_DEPSTRUCT; /* horizontal pixel density */\r
+   png_uint_32 y_pixels_per_unit PNG_DEPSTRUCT; /* vertical pixel density */\r
+   png_byte phys_unit_type PNG_DEPSTRUCT; /* resolution type (see\r
+                                             PNG_RESOLUTION_ below) */\r
+#endif\r
+\r
+#ifdef PNG_hIST_SUPPORTED\r
+   /* The hIST chunk contains the relative frequency or importance of the\r
+    * various palette entries, so that a viewer can intelligently select a\r
+    * reduced-color palette, if required.  Data is an array of "num_palette"\r
+    * values in the range [0,65535]. Data valid if (valid & PNG_INFO_hIST)\r
+    * is non-zero.\r
+    */\r
+   png_uint_16p hist PNG_DEPSTRUCT;\r
+#endif\r
+\r
+#ifdef PNG_cHRM_SUPPORTED\r
+   /* The cHRM chunk describes the CIE color characteristics of the monitor\r
+    * on which the PNG was created.  This data allows the viewer to do gamut\r
+    * mapping of the input image to ensure that the viewer sees the same\r
+    * colors in the image as the creator.  Values are in the range\r
+    * [0.0, 0.8].  Data valid if (valid & PNG_INFO_cHRM) non-zero.\r
+    */\r
+#ifdef PNG_FLOATING_POINT_SUPPORTED\r
+   float x_white PNG_DEPSTRUCT;\r
+   float y_white PNG_DEPSTRUCT;\r
+   float x_red PNG_DEPSTRUCT;\r
+   float y_red PNG_DEPSTRUCT;\r
+   float x_green PNG_DEPSTRUCT;\r
+   float y_green PNG_DEPSTRUCT;\r
+   float x_blue PNG_DEPSTRUCT;\r
+   float y_blue PNG_DEPSTRUCT;\r
+#endif\r
+#endif\r
+\r
+#ifdef PNG_pCAL_SUPPORTED\r
+   /* The pCAL chunk describes a transformation between the stored pixel\r
+    * values and original physical data values used to create the image.\r
+    * The integer range [0, 2^bit_depth - 1] maps to the floating-point\r
+    * range given by [pcal_X0, pcal_X1], and are further transformed by a\r
+    * (possibly non-linear) transformation function given by "pcal_type"\r
+    * and "pcal_params" into "pcal_units".  Please see the PNG_EQUATION_\r
+    * defines below, and the PNG-Group's PNG extensions document for a\r
+    * complete description of the transformations and how they should be\r
+    * implemented, and for a description of the ASCII parameter strings.\r
+    * Data values are valid if (valid & PNG_INFO_pCAL) non-zero.\r
+    */\r
+   png_charp pcal_purpose PNG_DEPSTRUCT;  /* pCAL chunk description string */\r
+   png_int_32 pcal_X0 PNG_DEPSTRUCT;      /* minimum value */\r
+   png_int_32 pcal_X1 PNG_DEPSTRUCT;      /* maximum value */\r
+   png_charp pcal_units PNG_DEPSTRUCT;    /* Latin-1 string giving physical\r
+                                             units */\r
+   png_charpp pcal_params PNG_DEPSTRUCT;  /* ASCII strings containing\r
+                                             parameter values */\r
+   png_byte pcal_type PNG_DEPSTRUCT;      /* equation type\r
+                                             (see PNG_EQUATION_ below) */\r
+   png_byte pcal_nparams PNG_DEPSTRUCT;   /* number of parameters given\r
+                                             in pcal_params */\r
+#endif\r
+\r
+/* New members added in libpng-1.0.6 */\r
+   png_uint_32 free_me PNG_DEPSTRUCT;     /* flags items libpng is\r
+                                             responsible for freeing */\r
+\r
+#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED) || \\r
+ defined(PNG_HANDLE_AS_UNKNOWN_SUPPORTED)\r
+   /* Storage for unknown chunks that the library doesn't recognize. */\r
+   png_unknown_chunkp unknown_chunks PNG_DEPSTRUCT;\r
+   png_size_t unknown_chunks_num PNG_DEPSTRUCT;\r
+#endif\r
+\r
+#ifdef PNG_iCCP_SUPPORTED\r
+   /* iCCP chunk data. */\r
+   png_charp iccp_name PNG_DEPSTRUCT;     /* profile name */\r
+   png_charp iccp_profile PNG_DEPSTRUCT;  /* International Color Consortium\r
+                                             profile data */\r
+                            /* Note to maintainer: should be png_bytep */\r
+   png_uint_32 iccp_proflen PNG_DEPSTRUCT;  /* ICC profile data length */\r
+   png_byte iccp_compression PNG_DEPSTRUCT; /* Always zero */\r
+#endif\r
+\r
+#ifdef PNG_sPLT_SUPPORTED\r
+   /* Data on sPLT chunks (there may be more than one). */\r
+   png_sPLT_tp splt_palettes PNG_DEPSTRUCT;\r
+   png_uint_32 splt_palettes_num PNG_DEPSTRUCT;\r
+#endif\r
+\r
+#ifdef PNG_sCAL_SUPPORTED\r
+   /* The sCAL chunk describes the actual physical dimensions of the\r
+    * subject matter of the graphic.  The chunk contains a unit specification\r
+    * a byte value, and two ASCII strings representing floating-point\r
+    * values.  The values are width and height corresponsing to one pixel\r
+    * in the image.  This external representation is converted to double\r
+    * here.  Data values are valid if (valid & PNG_INFO_sCAL) is non-zero.\r
+    */\r
+   png_byte scal_unit PNG_DEPSTRUCT;         /* unit of physical scale */\r
+#ifdef PNG_FLOATING_POINT_SUPPORTED\r
+   double scal_pixel_width PNG_DEPSTRUCT;    /* width of one pixel */\r
+   double scal_pixel_height PNG_DEPSTRUCT;   /* height of one pixel */\r
+#endif\r
+#ifdef PNG_FIXED_POINT_SUPPORTED\r
+   png_charp scal_s_width PNG_DEPSTRUCT;     /* string containing height */\r
+   png_charp scal_s_height PNG_DEPSTRUCT;    /* string containing width */\r
+#endif\r
+#endif\r
+\r
+#ifdef PNG_INFO_IMAGE_SUPPORTED\r
+   /* Memory has been allocated if (valid & PNG_ALLOCATED_INFO_ROWS)\r
+      non-zero */\r
+   /* Data valid if (valid & PNG_INFO_IDAT) non-zero */\r
+   png_bytepp row_pointers PNG_DEPSTRUCT;        /* the image bits */\r
+#endif\r
+\r
+#if defined(PNG_FIXED_POINT_SUPPORTED) && defined(PNG_gAMA_SUPPORTED)\r
+   png_fixed_point int_gamma PNG_DEPSTRUCT; /* gamma of image,\r
+                                               if (valid & PNG_INFO_gAMA) */\r
+#endif\r
+\r
+#if defined(PNG_cHRM_SUPPORTED) && defined(PNG_FIXED_POINT_SUPPORTED)\r
+   png_fixed_point int_x_white PNG_DEPSTRUCT;\r
+   png_fixed_point int_y_white PNG_DEPSTRUCT;\r
+   png_fixed_point int_x_red PNG_DEPSTRUCT;\r
+   png_fixed_point int_y_red PNG_DEPSTRUCT;\r
+   png_fixed_point int_x_green PNG_DEPSTRUCT;\r
+   png_fixed_point int_y_green PNG_DEPSTRUCT;\r
+   png_fixed_point int_x_blue PNG_DEPSTRUCT;\r
+   png_fixed_point int_y_blue PNG_DEPSTRUCT;\r
+#endif\r
+\r
+} png_info;\r
+\r
+typedef png_info FAR * png_infop;\r
+typedef png_info FAR * FAR * png_infopp;\r
+\r
+/* Maximum positive integer used in PNG is (2^31)-1 */\r
+#define PNG_UINT_31_MAX ((png_uint_32)0x7fffffffL)\r
+#define PNG_UINT_32_MAX ((png_uint_32)(-1))\r
+#define PNG_SIZE_MAX ((png_size_t)(-1))\r
+\r
+/* These describe the color_type field in png_info. */\r
+/* color type masks */\r
+#define PNG_COLOR_MASK_PALETTE    1\r
+#define PNG_COLOR_MASK_COLOR      2\r
+#define PNG_COLOR_MASK_ALPHA      4\r
+\r
+/* color types.  Note that not all combinations are legal */\r
+#define PNG_COLOR_TYPE_GRAY 0\r
+#define PNG_COLOR_TYPE_PALETTE  (PNG_COLOR_MASK_COLOR | PNG_COLOR_MASK_PALETTE)\r
+#define PNG_COLOR_TYPE_RGB        (PNG_COLOR_MASK_COLOR)\r
+#define PNG_COLOR_TYPE_RGB_ALPHA  (PNG_COLOR_MASK_COLOR | PNG_COLOR_MASK_ALPHA)\r
+#define PNG_COLOR_TYPE_GRAY_ALPHA (PNG_COLOR_MASK_ALPHA)\r
+/* aliases */\r
+#define PNG_COLOR_TYPE_RGBA  PNG_COLOR_TYPE_RGB_ALPHA\r
+#define PNG_COLOR_TYPE_GA  PNG_COLOR_TYPE_GRAY_ALPHA\r
+\r
+/* This is for compression type. PNG 1.0-1.2 only define the single type. */\r
+#define PNG_COMPRESSION_TYPE_BASE 0 /* Deflate method 8, 32K window */\r
+#define PNG_COMPRESSION_TYPE_DEFAULT PNG_COMPRESSION_TYPE_BASE\r
+\r
+/* This is for filter type. PNG 1.0-1.2 only define the single type. */\r
+#define PNG_FILTER_TYPE_BASE      0 /* Single row per-byte filtering */\r
+#define PNG_INTRAPIXEL_DIFFERENCING 64 /* Used only in MNG datastreams */\r
+#define PNG_FILTER_TYPE_DEFAULT   PNG_FILTER_TYPE_BASE\r
+\r
+/* These are for the interlacing type.  These values should NOT be changed. */\r
+#define PNG_INTERLACE_NONE        0 /* Non-interlaced image */\r
+#define PNG_INTERLACE_ADAM7       1 /* Adam7 interlacing */\r
+#define PNG_INTERLACE_LAST        2 /* Not a valid value */\r
+\r
+/* These are for the oFFs chunk.  These values should NOT be changed. */\r
+#define PNG_OFFSET_PIXEL          0 /* Offset in pixels */\r
+#define PNG_OFFSET_MICROMETER     1 /* Offset in micrometers (1/10^6 meter) */\r
+#define PNG_OFFSET_LAST           2 /* Not a valid value */\r
+\r
+/* These are for the pCAL chunk.  These values should NOT be changed. */\r
+#define PNG_EQUATION_LINEAR       0 /* Linear transformation */\r
+#define PNG_EQUATION_BASE_E       1 /* Exponential base e transform */\r
+#define PNG_EQUATION_ARBITRARY    2 /* Arbitrary base exponential transform */\r
+#define PNG_EQUATION_HYPERBOLIC   3 /* Hyperbolic sine transformation */\r
+#define PNG_EQUATION_LAST         4 /* Not a valid value */\r
+\r
+/* These are for the sCAL chunk.  These values should NOT be changed. */\r
+#define PNG_SCALE_UNKNOWN         0 /* unknown unit (image scale) */\r
+#define PNG_SCALE_METER           1 /* meters per pixel */\r
+#define PNG_SCALE_RADIAN          2 /* radians per pixel */\r
+#define PNG_SCALE_LAST            3 /* Not a valid value */\r
+\r
+/* These are for the pHYs chunk.  These values should NOT be changed. */\r
+#define PNG_RESOLUTION_UNKNOWN    0 /* pixels/unknown unit (aspect ratio) */\r
+#define PNG_RESOLUTION_METER      1 /* pixels/meter */\r
+#define PNG_RESOLUTION_LAST       2 /* Not a valid value */\r
+\r
+/* These are for the sRGB chunk.  These values should NOT be changed. */\r
+#define PNG_sRGB_INTENT_PERCEPTUAL 0\r
+#define PNG_sRGB_INTENT_RELATIVE   1\r
+#define PNG_sRGB_INTENT_SATURATION 2\r
+#define PNG_sRGB_INTENT_ABSOLUTE   3\r
+#define PNG_sRGB_INTENT_LAST       4 /* Not a valid value */\r
+\r
+/* This is for text chunks */\r
+#define PNG_KEYWORD_MAX_LENGTH     79\r
+\r
+/* Maximum number of entries in PLTE/sPLT/tRNS arrays */\r
+#define PNG_MAX_PALETTE_LENGTH    256\r
+\r
+/* These determine if an ancillary chunk's data has been successfully read\r
+ * from the PNG header, or if the application has filled in the corresponding\r
+ * data in the info_struct to be written into the output file.  The values\r
+ * of the PNG_INFO_<chunk> defines should NOT be changed.\r
+ */\r
+#define PNG_INFO_gAMA 0x0001\r
+#define PNG_INFO_sBIT 0x0002\r
+#define PNG_INFO_cHRM 0x0004\r
+#define PNG_INFO_PLTE 0x0008\r
+#define PNG_INFO_tRNS 0x0010\r
+#define PNG_INFO_bKGD 0x0020\r
+#define PNG_INFO_hIST 0x0040\r
+#define PNG_INFO_pHYs 0x0080\r
+#define PNG_INFO_oFFs 0x0100\r
+#define PNG_INFO_tIME 0x0200\r
+#define PNG_INFO_pCAL 0x0400\r
+#define PNG_INFO_sRGB 0x0800   /* GR-P, 0.96a */\r
+#define PNG_INFO_iCCP 0x1000   /* ESR, 1.0.6 */\r
+#define PNG_INFO_sPLT 0x2000   /* ESR, 1.0.6 */\r
+#define PNG_INFO_sCAL 0x4000   /* ESR, 1.0.6 */\r
+#define PNG_INFO_IDAT 0x8000L  /* ESR, 1.0.6 */\r
+\r
+/* This is used for the transformation routines, as some of them\r
+ * change these values for the row.  It also should enable using\r
+ * the routines for other purposes.\r
+ */\r
+typedef struct png_row_info_struct\r
+{\r
+   png_uint_32 width; /* width of row */\r
+   png_size_t rowbytes; /* number of bytes in row */\r
+   png_byte color_type; /* color type of row */\r
+   png_byte bit_depth; /* bit depth of row */\r
+   png_byte channels; /* number of channels (1, 2, 3, or 4) */\r
+   png_byte pixel_depth; /* bits per pixel (depth * channels) */\r
+} png_row_info;\r
+\r
+typedef png_row_info FAR * png_row_infop;\r
+typedef png_row_info FAR * FAR * png_row_infopp;\r
+\r
+/* These are the function types for the I/O functions and for the functions\r
+ * that allow the user to override the default I/O functions with his or her\r
+ * own.  The png_error_ptr type should match that of user-supplied warning\r
+ * and error functions, while the png_rw_ptr type should match that of the\r
+ * user read/write data functions.\r
+ */\r
+typedef struct png_struct_def png_struct;\r
+typedef png_struct FAR * png_structp;\r
+\r
+typedef void (PNGAPI *png_error_ptr) PNGARG((png_structp, png_const_charp));\r
+typedef void (PNGAPI *png_rw_ptr) PNGARG((png_structp, png_bytep, png_size_t));\r
+typedef void (PNGAPI *png_flush_ptr) PNGARG((png_structp));\r
+typedef void (PNGAPI *png_read_status_ptr) PNGARG((png_structp, png_uint_32,\r
+   int));\r
+typedef void (PNGAPI *png_write_status_ptr) PNGARG((png_structp, png_uint_32,\r
+   int));\r
+\r
+#ifdef PNG_PROGRESSIVE_READ_SUPPORTED\r
+typedef void (PNGAPI *png_progressive_info_ptr) PNGARG((png_structp,\r
+   png_infop));\r
+typedef void (PNGAPI *png_progressive_end_ptr) PNGARG((png_structp, png_infop));\r
+typedef void (PNGAPI *png_progressive_row_ptr) PNGARG((png_structp, png_bytep,\r
+   png_uint_32, int));\r
+#endif\r
+\r
+#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \\r
+    defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)\r
+typedef void (PNGAPI *png_user_transform_ptr) PNGARG((png_structp,\r
+    png_row_infop, png_bytep));\r
+#endif\r
+\r
+#ifdef PNG_USER_CHUNKS_SUPPORTED\r
+typedef int (PNGAPI *png_user_chunk_ptr) PNGARG((png_structp,\r
+   png_unknown_chunkp));\r
+#endif\r
+#ifdef PNG_UNKNOWN_CHUNKS_SUPPORTED\r
+typedef void (PNGAPI *png_unknown_chunk_ptr) PNGARG((png_structp));\r
+#endif\r
+#ifdef PNG_SETJMP_SUPPORTED\r
+/* This must match the function definition in <setjmp.h>, and the\r
+ * application must include this before png.h to obtain the definition\r
+ * of jmp_buf.\r
+ */\r
+typedef void (PNGAPI *png_longjmp_ptr) PNGARG((jmp_buf, int));\r
+#endif\r
+\r
+/* Transform masks for the high-level interface */\r
+#define PNG_TRANSFORM_IDENTITY       0x0000    /* read and write */\r
+#define PNG_TRANSFORM_STRIP_16       0x0001    /* read only */\r
+#define PNG_TRANSFORM_STRIP_ALPHA    0x0002    /* read only */\r
+#define PNG_TRANSFORM_PACKING        0x0004    /* read and write */\r
+#define PNG_TRANSFORM_PACKSWAP       0x0008    /* read and write */\r
+#define PNG_TRANSFORM_EXPAND         0x0010    /* read only */\r
+#define PNG_TRANSFORM_INVERT_MONO    0x0020    /* read and write */\r
+#define PNG_TRANSFORM_SHIFT          0x0040    /* read and write */\r
+#define PNG_TRANSFORM_BGR            0x0080    /* read and write */\r
+#define PNG_TRANSFORM_SWAP_ALPHA     0x0100    /* read and write */\r
+#define PNG_TRANSFORM_SWAP_ENDIAN    0x0200    /* read and write */\r
+#define PNG_TRANSFORM_INVERT_ALPHA   0x0400    /* read and write */\r
+#define PNG_TRANSFORM_STRIP_FILLER   0x0800    /* write only */\r
+/* Added to libpng-1.2.34 */\r
+#define PNG_TRANSFORM_STRIP_FILLER_BEFORE PNG_TRANSFORM_STRIP_FILLER\r
+#define PNG_TRANSFORM_STRIP_FILLER_AFTER 0x1000 /* write only */\r
+/* Added to libpng-1.4.0 */\r
+#define PNG_TRANSFORM_GRAY_TO_RGB   0x2000      /* read only */\r
+\r
+/* Flags for MNG supported features */\r
+#define PNG_FLAG_MNG_EMPTY_PLTE     0x01\r
+#define PNG_FLAG_MNG_FILTER_64      0x04\r
+#define PNG_ALL_MNG_FEATURES        0x05\r
+\r
+typedef png_voidp (*png_malloc_ptr) PNGARG((png_structp, png_alloc_size_t));\r
+typedef void (*png_free_ptr) PNGARG((png_structp, png_voidp));\r
+\r
+/* The structure that holds the information to read and write PNG files.\r
+ * The only people who need to care about what is inside of this are the\r
+ * people who will be modifying the library for their own special needs.\r
+ * It should NOT be accessed directly by an application, except to store\r
+ * the jmp_buf.\r
+ */\r
+\r
+struct png_struct_def\r
+{\r
+#ifdef PNG_SETJMP_SUPPORTED\r
+   jmp_buf jmpbuf PNG_DEPSTRUCT;            /* used in png_error */\r
+   png_longjmp_ptr longjmp_fn PNG_DEPSTRUCT;/* setjmp non-local goto\r
+                                               function. */\r
+#endif\r
+   png_error_ptr error_fn PNG_DEPSTRUCT;    /* function for printing\r
+                                               errors and aborting */\r
+   png_error_ptr warning_fn PNG_DEPSTRUCT;  /* function for printing\r
+                                               warnings */\r
+   png_voidp error_ptr PNG_DEPSTRUCT;       /* user supplied struct for\r
+                                               error functions */\r
+   png_rw_ptr write_data_fn PNG_DEPSTRUCT;  /* function for writing\r
+                                               output data */\r
+   png_rw_ptr read_data_fn PNG_DEPSTRUCT;   /* function for reading\r
+                                               input data */\r
+   png_voidp io_ptr PNG_DEPSTRUCT;          /* ptr to application struct\r
+                                               for I/O functions */\r
+\r
+#ifdef PNG_READ_USER_TRANSFORM_SUPPORTED\r
+   png_user_transform_ptr read_user_transform_fn PNG_DEPSTRUCT; /* user read\r
+                                                                 transform */\r
+#endif\r
+\r
+#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED\r
+   png_user_transform_ptr write_user_transform_fn PNG_DEPSTRUCT; /* user write\r
+                                                                  transform */\r
+#endif\r
+\r
+/* These were added in libpng-1.0.2 */\r
+#ifdef PNG_USER_TRANSFORM_PTR_SUPPORTED\r
+#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \\r
+    defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)\r
+   png_voidp user_transform_ptr PNG_DEPSTRUCT; /* user supplied struct\r
+                                                  for user transform */\r
+   png_byte user_transform_depth PNG_DEPSTRUCT;    /* bit depth of user\r
+                                                      transformed pixels */\r
+   png_byte user_transform_channels PNG_DEPSTRUCT; /* channels in user\r
+                                                      transformed pixels */\r
+#endif\r
+#endif\r
+\r
+   png_uint_32 mode PNG_DEPSTRUCT;          /* tells us where we are in\r
+                                               the PNG file */\r
+   png_uint_32 flags PNG_DEPSTRUCT;         /* flags indicating various\r
+                                               things to libpng */\r
+   png_uint_32 transformations PNG_DEPSTRUCT; /* which transformations\r
+                                                 to perform */\r
+\r
+   z_stream zstream PNG_DEPSTRUCT;          /* pointer to decompression\r
+                                               structure (below) */\r
+   png_bytep zbuf PNG_DEPSTRUCT;            /* buffer for zlib */\r
+   png_size_t zbuf_size PNG_DEPSTRUCT;      /* size of zbuf */\r
+   int zlib_level PNG_DEPSTRUCT;            /* holds zlib compression level */\r
+   int zlib_method PNG_DEPSTRUCT;           /* holds zlib compression method */\r
+   int zlib_window_bits PNG_DEPSTRUCT;      /* holds zlib compression window\r
+                                               bits */\r
+   int zlib_mem_level PNG_DEPSTRUCT;        /* holds zlib compression memory\r
+                                               level */\r
+   int zlib_strategy PNG_DEPSTRUCT;         /* holds zlib compression\r
+                                               strategy */\r
+\r
+   png_uint_32 width PNG_DEPSTRUCT;         /* width of image in pixels */\r
+   png_uint_32 height PNG_DEPSTRUCT;        /* height of image in pixels */\r
+   png_uint_32 num_rows PNG_DEPSTRUCT;      /* number of rows in current pass */\r
+   png_uint_32 usr_width PNG_DEPSTRUCT;     /* width of row at start of write */\r
+   png_size_t rowbytes PNG_DEPSTRUCT;       /* size of row in bytes */\r
+#if 0 /* Replaced with the following in libpng-1.4.1 */\r
+   png_size_t irowbytes PNG_DEPSTRUCT;\r
+#endif\r
+/* Added in libpng-1.4.1 */\r
+#ifdef PNG_USER_LIMITS_SUPPORTED\r
+   /* Total memory that a zTXt, sPLT, iTXt, iCCP, or unknown chunk\r
+    * can occupy when decompressed.  0 means unlimited.\r
+    * We will change the typedef from png_size_t to png_alloc_size_t\r
+    * in libpng-1.6.0\r
+    */\r
+   png_alloc_size_t user_chunk_malloc_max PNG_DEPSTRUCT;\r
+#endif\r
+   png_uint_32 iwidth PNG_DEPSTRUCT;        /* width of current interlaced\r
+                                               row in pixels */\r
+   png_uint_32 row_number PNG_DEPSTRUCT;    /* current row in interlace pass */\r
+   png_bytep prev_row PNG_DEPSTRUCT;        /* buffer to save previous\r
+                                               (unfiltered) row */\r
+   png_bytep row_buf PNG_DEPSTRUCT;         /* buffer to save current\r
+                                               (unfiltered) row */\r
+   png_bytep sub_row PNG_DEPSTRUCT;         /* buffer to save "sub" row\r
+                                               when filtering */\r
+   png_bytep up_row PNG_DEPSTRUCT;          /* buffer to save "up" row\r
+                                               when filtering */\r
+   png_bytep avg_row PNG_DEPSTRUCT;         /* buffer to save "avg" row\r
+                                               when filtering */\r
+   png_bytep paeth_row PNG_DEPSTRUCT;       /* buffer to save "Paeth" row\r
+                                               when filtering */\r
+   png_row_info row_info PNG_DEPSTRUCT;     /* used for transformation\r
+                                               routines */\r
+\r
+   png_uint_32 idat_size PNG_DEPSTRUCT;     /* current IDAT size for read */\r
+   png_uint_32 crc PNG_DEPSTRUCT;           /* current chunk CRC value */\r
+   png_colorp palette PNG_DEPSTRUCT;        /* palette from the input file */\r
+   png_uint_16 num_palette PNG_DEPSTRUCT;   /* number of color entries in\r
+                                               palette */\r
+   png_uint_16 num_trans PNG_DEPSTRUCT;     /* number of transparency values */\r
+   png_byte chunk_name[5] PNG_DEPSTRUCT;    /* null-terminated name of current\r
+                                               chunk */\r
+   png_byte compression PNG_DEPSTRUCT;      /* file compression type\r
+                                               (always 0) */\r
+   png_byte filter PNG_DEPSTRUCT;           /* file filter type (always 0) */\r
+   png_byte interlaced PNG_DEPSTRUCT;       /* PNG_INTERLACE_NONE,\r
+                                               PNG_INTERLACE_ADAM7 */\r
+   png_byte pass PNG_DEPSTRUCT;             /* current interlace pass (0 - 6) */\r
+   png_byte do_filter PNG_DEPSTRUCT;        /* row filter flags (see\r
+                                               PNG_FILTER_ below ) */\r
+   png_byte color_type PNG_DEPSTRUCT;       /* color type of file */\r
+   png_byte bit_depth PNG_DEPSTRUCT;        /* bit depth of file */\r
+   png_byte usr_bit_depth PNG_DEPSTRUCT;    /* bit depth of users row */\r
+   png_byte pixel_depth PNG_DEPSTRUCT;      /* number of bits per pixel */\r
+   png_byte channels PNG_DEPSTRUCT;         /* number of channels in file */\r
+   png_byte usr_channels PNG_DEPSTRUCT;     /* channels at start of write */\r
+   png_byte sig_bytes PNG_DEPSTRUCT;        /* magic bytes read/written from\r
+                                               start of file */\r
+\r
+#if defined(PNG_READ_FILLER_SUPPORTED) || defined(PNG_WRITE_FILLER_SUPPORTED)\r
+   png_uint_16 filler PNG_DEPSTRUCT;           /* filler bytes for pixel\r
+                                                  expansion */\r
+#endif\r
+\r
+#ifdef PNG_bKGD_SUPPORTED\r
+   png_byte background_gamma_type PNG_DEPSTRUCT;\r
+#  ifdef PNG_FLOATING_POINT_SUPPORTED\r
+   float background_gamma PNG_DEPSTRUCT;\r
+#  endif\r
+   png_color_16 background PNG_DEPSTRUCT;   /* background color in\r
+                                               screen gamma space */\r
+#ifdef PNG_READ_GAMMA_SUPPORTED\r
+   png_color_16 background_1 PNG_DEPSTRUCT; /* background normalized\r
+                                               to gamma 1.0 */\r
+#endif\r
+#endif /* PNG_bKGD_SUPPORTED */\r
+\r
+#ifdef PNG_WRITE_FLUSH_SUPPORTED\r
+   png_flush_ptr output_flush_fn PNG_DEPSTRUCT; /* Function for flushing\r
+                                               output */\r
+   png_uint_32 flush_dist PNG_DEPSTRUCT;    /* how many rows apart to flush,\r
+                                               0 - no flush */\r
+   png_uint_32 flush_rows PNG_DEPSTRUCT;    /* number of rows written since\r
+                                               last flush */\r
+#endif\r
+\r
+#if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)\r
+   int gamma_shift PNG_DEPSTRUCT;      /* number of "insignificant" bits\r
+                                          16-bit gamma */\r
+#ifdef PNG_FLOATING_POINT_SUPPORTED\r
+   float gamma PNG_DEPSTRUCT;          /* file gamma value */\r
+   float screen_gamma PNG_DEPSTRUCT;   /* screen gamma value\r
+                                          (display_exponent) */\r
+#endif\r
+#endif\r
+\r
+#if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)\r
+   png_bytep gamma_table PNG_DEPSTRUCT;     /* gamma table for 8-bit\r
+                                               depth files */\r
+   png_bytep gamma_from_1 PNG_DEPSTRUCT;    /* converts from 1.0 to screen */\r
+   png_bytep gamma_to_1 PNG_DEPSTRUCT;      /* converts from file to 1.0 */\r
+   png_uint_16pp gamma_16_table PNG_DEPSTRUCT; /* gamma table for 16-bit\r
+                                                  depth files */\r
+   png_uint_16pp gamma_16_from_1 PNG_DEPSTRUCT; /* converts from 1.0 to\r
+                                                   screen */\r
+   png_uint_16pp gamma_16_to_1 PNG_DEPSTRUCT; /* converts from file to 1.0 */\r
+#endif\r
+\r
+#if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_sBIT_SUPPORTED)\r
+   png_color_8 sig_bit PNG_DEPSTRUCT;       /* significant bits in each\r
+                                               available channel */\r
+#endif\r
+\r
+#if defined(PNG_READ_SHIFT_SUPPORTED) || defined(PNG_WRITE_SHIFT_SUPPORTED)\r
+   png_color_8 shift PNG_DEPSTRUCT;         /* shift for significant bit\r
+                                               tranformation */\r
+#endif\r
+\r
+#if defined(PNG_tRNS_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED) \\r
+ || defined(PNG_READ_EXPAND_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)\r
+   png_bytep trans_alpha PNG_DEPSTRUCT;           /* alpha values for\r
+                                                     paletted files */\r
+   png_color_16 trans_color PNG_DEPSTRUCT;  /* transparent color for\r
+                                               non-paletted files */\r
+#endif\r
+\r
+   png_read_status_ptr read_row_fn PNG_DEPSTRUCT;   /* called after each\r
+                                                       row is decoded */\r
+   png_write_status_ptr write_row_fn PNG_DEPSTRUCT; /* called after each\r
+                                                       row is encoded */\r
+#ifdef PNG_PROGRESSIVE_READ_SUPPORTED\r
+   png_progressive_info_ptr info_fn PNG_DEPSTRUCT; /* called after header\r
+                                                      data fully read */\r
+   png_progressive_row_ptr row_fn PNG_DEPSTRUCT;   /* called after each\r
+                                                      prog. row is decoded */\r
+   png_progressive_end_ptr end_fn PNG_DEPSTRUCT;   /* called after image\r
+                                                      is complete */\r
+   png_bytep save_buffer_ptr PNG_DEPSTRUCT;        /* current location in\r
+                                                      save_buffer */\r
+   png_bytep save_buffer PNG_DEPSTRUCT;            /* buffer for previously\r
+                                                      read data */\r
+   png_bytep current_buffer_ptr PNG_DEPSTRUCT;     /* current location in\r
+                                                      current_buffer */\r
+   png_bytep current_buffer PNG_DEPSTRUCT;         /* buffer for recently\r
+                                                      used data */\r
+   png_uint_32 push_length PNG_DEPSTRUCT;          /* size of current input\r
+                                                      chunk */\r
+   png_uint_32 skip_length PNG_DEPSTRUCT;          /* bytes to skip in\r
+                                                      input data */\r
+   png_size_t save_buffer_size PNG_DEPSTRUCT;      /* amount of data now\r
+                                                      in save_buffer */\r
+   png_size_t save_buffer_max PNG_DEPSTRUCT;       /* total size of\r
+                                                      save_buffer */\r
+   png_size_t buffer_size PNG_DEPSTRUCT;           /* total amount of\r
+                                                      available input data */\r
+   png_size_t current_buffer_size PNG_DEPSTRUCT;   /* amount of data now\r
+                                                      in current_buffer */\r
+   int process_mode PNG_DEPSTRUCT;                 /* what push library\r
+                                                      is currently doing */\r
+   int cur_palette PNG_DEPSTRUCT;                  /* current push library\r
+                                                      palette index */\r
+\r
+#  ifdef PNG_TEXT_SUPPORTED\r
+     png_size_t current_text_size PNG_DEPSTRUCT;   /* current size of\r
+                                                      text input data */\r
+     png_size_t current_text_left PNG_DEPSTRUCT;   /* how much text left\r
+                                                      to read in input */\r
+     png_charp current_text PNG_DEPSTRUCT;         /* current text chunk\r
+                                                      buffer */\r
+     png_charp current_text_ptr PNG_DEPSTRUCT;     /* current location\r
+                                                      in current_text */\r
+#  endif /* PNG_PROGRESSIVE_READ_SUPPORTED && PNG_TEXT_SUPPORTED */\r
+\r
+#endif /* PNG_PROGRESSIVE_READ_SUPPORTED */\r
+\r
+#if defined(__TURBOC__) && !defined(_Windows) && !defined(__FLAT__)\r
+/* For the Borland special 64K segment handler */\r
+   png_bytepp offset_table_ptr PNG_DEPSTRUCT;\r
+   png_bytep offset_table PNG_DEPSTRUCT;\r
+   png_uint_16 offset_table_number PNG_DEPSTRUCT;\r
+   png_uint_16 offset_table_count PNG_DEPSTRUCT;\r
+   png_uint_16 offset_table_count_free PNG_DEPSTRUCT;\r
+#endif\r
+\r
+#ifdef PNG_READ_QUANTIZE_SUPPORTED\r
+   png_bytep palette_lookup PNG_DEPSTRUCT; /* lookup table for quantizing */\r
+   png_bytep quantize_index PNG_DEPSTRUCT; /* index translation for palette\r
+                                              files */\r
+#endif\r
+\r
+#if defined(PNG_READ_QUANTIZE_SUPPORTED) || defined(PNG_hIST_SUPPORTED)\r
+   png_uint_16p hist PNG_DEPSTRUCT;                /* histogram */\r
+#endif\r
+\r
+#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED\r
+   png_byte heuristic_method PNG_DEPSTRUCT;        /* heuristic for row\r
+                                                      filter selection */\r
+   png_byte num_prev_filters PNG_DEPSTRUCT;        /* number of weights\r
+                                                      for previous rows */\r
+   png_bytep prev_filters PNG_DEPSTRUCT;           /* filter type(s) of\r
+                                                      previous row(s) */\r
+   png_uint_16p filter_weights PNG_DEPSTRUCT;      /* weight(s) for previous\r
+                                                      line(s) */\r
+   png_uint_16p inv_filter_weights PNG_DEPSTRUCT;  /* 1/weight(s) for\r
+                                                      previous line(s) */\r
+   png_uint_16p filter_costs PNG_DEPSTRUCT;        /* relative filter\r
+                                                      calculation cost */\r
+   png_uint_16p inv_filter_costs PNG_DEPSTRUCT;    /* 1/relative filter\r
+                                                      calculation cost */\r
+#endif\r
+\r
+#ifdef PNG_TIME_RFC1123_SUPPORTED\r
+   png_charp time_buffer PNG_DEPSTRUCT; /* String to hold RFC 1123 time text */\r
+#endif\r
+\r
+/* New members added in libpng-1.0.6 */\r
+\r
+   png_uint_32 free_me PNG_DEPSTRUCT;    /* flags items libpng is\r
+                                            responsible for freeing */\r
+\r
+#ifdef PNG_USER_CHUNKS_SUPPORTED\r
+   png_voidp user_chunk_ptr PNG_DEPSTRUCT;\r
+   png_user_chunk_ptr read_user_chunk_fn PNG_DEPSTRUCT; /* user read\r
+                                                           chunk handler */\r
+#endif\r
+\r
+#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED\r
+   int num_chunk_list PNG_DEPSTRUCT;\r
+   png_bytep chunk_list PNG_DEPSTRUCT;\r
+#endif\r
+\r
+/* New members added in libpng-1.0.3 */\r
+#ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED\r
+   png_byte rgb_to_gray_status PNG_DEPSTRUCT;\r
+   /* These were changed from png_byte in libpng-1.0.6 */\r
+   png_uint_16 rgb_to_gray_red_coeff PNG_DEPSTRUCT;\r
+   png_uint_16 rgb_to_gray_green_coeff PNG_DEPSTRUCT;\r
+   png_uint_16 rgb_to_gray_blue_coeff PNG_DEPSTRUCT;\r
+#endif\r
+\r
+/* New member added in libpng-1.0.4 (renamed in 1.0.9) */\r
+#if defined(PNG_MNG_FEATURES_SUPPORTED) || \\r
+    defined(PNG_READ_EMPTY_PLTE_SUPPORTED) || \\r
+    defined(PNG_WRITE_EMPTY_PLTE_SUPPORTED)\r
+/* Changed from png_byte to png_uint_32 at version 1.2.0 */\r
+   png_uint_32 mng_features_permitted PNG_DEPSTRUCT;\r
+#endif\r
+\r
+/* New member added in libpng-1.0.7 */\r
+#if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)\r
+   png_fixed_point int_gamma PNG_DEPSTRUCT;\r
+#endif\r
+\r
+/* New member added in libpng-1.0.9, ifdef'ed out in 1.0.12, enabled in 1.2.0 */\r
+#ifdef PNG_MNG_FEATURES_SUPPORTED\r
+   png_byte filter_type PNG_DEPSTRUCT;\r
+#endif\r
+\r
+/* New members added in libpng-1.2.0 */\r
+\r
+/* New members added in libpng-1.0.2 but first enabled by default in 1.2.0 */\r
+#ifdef PNG_USER_MEM_SUPPORTED\r
+   png_voidp mem_ptr PNG_DEPSTRUCT;             /* user supplied struct for\r
+                                                   mem functions */\r
+   png_malloc_ptr malloc_fn PNG_DEPSTRUCT;      /* function for\r
+                                                   allocating memory */\r
+   png_free_ptr free_fn PNG_DEPSTRUCT;          /* function for\r
+                                                   freeing memory */\r
+#endif\r
+\r
+/* New member added in libpng-1.0.13 and 1.2.0 */\r
+   png_bytep big_row_buf PNG_DEPSTRUCT;         /* buffer to save current\r
+                                                   (unfiltered) row */\r
+\r
+#ifdef PNG_READ_QUANTIZE_SUPPORTED\r
+/* The following three members were added at version 1.0.14 and 1.2.4 */\r
+   png_bytep quantize_sort PNG_DEPSTRUCT;          /* working sort array */\r
+   png_bytep index_to_palette PNG_DEPSTRUCT;       /* where the original\r
+                                                     index currently is\r
+                                                     in the palette */\r
+   png_bytep palette_to_index PNG_DEPSTRUCT;       /* which original index\r
+                                                      points to this\r
+                                                      palette color */\r
+#endif\r
+\r
+/* New members added in libpng-1.0.16 and 1.2.6 */\r
+   png_byte compression_type PNG_DEPSTRUCT;\r
+\r
+#ifdef PNG_USER_LIMITS_SUPPORTED\r
+   png_uint_32 user_width_max PNG_DEPSTRUCT;\r
+   png_uint_32 user_height_max PNG_DEPSTRUCT;\r
+   /* Added in libpng-1.4.0: Total number of sPLT, text, and unknown\r
+    * chunks that can be stored (0 means unlimited).\r
+    */\r
+   png_uint_32 user_chunk_cache_max PNG_DEPSTRUCT;\r
+#endif\r
+\r
+/* New member added in libpng-1.0.25 and 1.2.17 */\r
+#ifdef PNG_UNKNOWN_CHUNKS_SUPPORTED\r
+   /* Storage for unknown chunk that the library doesn't recognize. */\r
+   png_unknown_chunk unknown_chunk PNG_DEPSTRUCT;\r
+#endif\r
+\r
+/* New members added in libpng-1.2.26 */\r
+  png_uint_32 old_big_row_buf_size PNG_DEPSTRUCT;\r
+  png_uint_32 old_prev_row_size PNG_DEPSTRUCT;\r
+\r
+/* New member added in libpng-1.2.30 */\r
+  png_charp chunkdata PNG_DEPSTRUCT;  /* buffer for reading chunk data */\r
+\r
+#ifdef PNG_IO_STATE_SUPPORTED\r
+/* New member added in libpng-1.4.0 */\r
+   png_uint_32 io_state PNG_DEPSTRUCT;\r
+#endif\r
+};\r
+\r
+\r
+/* This triggers a compiler error in png.c, if png.c and png.h\r
+ * do not agree upon the version number.\r
+ */\r
+typedef png_structp version_1_4_3;\r
+\r
+typedef png_struct FAR * FAR * png_structpp;\r
+\r
+/* Here are the function definitions most commonly used.  This is not\r
+ * the place to find out how to use libpng.  See libpng.txt for the\r
+ * full explanation, see example.c for the summary.  This just provides\r
+ * a simple one line description of the use of each function.\r
+ */\r
+\r
+/* Returns the version number of the library */\r
+extern PNG_EXPORT(png_uint_32,png_access_version_number) PNGARG((void));\r
+\r
+/* Tell lib we have already handled the first <num_bytes> magic bytes.\r
+ * Handling more than 8 bytes from the beginning of the file is an error.\r
+ */\r
+extern PNG_EXPORT(void,png_set_sig_bytes) PNGARG((png_structp png_ptr,\r
+   int num_bytes));\r
+\r
+/* Check sig[start] through sig[start + num_to_check - 1] to see if it's a\r
+ * PNG file.  Returns zero if the supplied bytes match the 8-byte PNG\r
+ * signature, and non-zero otherwise.  Having num_to_check == 0 or\r
+ * start > 7 will always fail (ie return non-zero).\r
+ */\r
+extern PNG_EXPORT(int,png_sig_cmp) PNGARG((png_bytep sig, png_size_t start,\r
+   png_size_t num_to_check));\r
+\r
+/* Simple signature checking function.  This is the same as calling\r
+ * png_check_sig(sig, n) := !png_sig_cmp(sig, 0, n).\r
+ */\r
+#define png_check_sig(sig,n) !png_sig_cmp((sig), 0, (n))\r
+\r
+/* Allocate and initialize png_ptr struct for reading, and any other memory. */\r
+extern PNG_EXPORT(png_structp,png_create_read_struct)\r
+   PNGARG((png_const_charp user_png_ver, png_voidp error_ptr,\r
+   png_error_ptr error_fn, png_error_ptr warn_fn)) PNG_ALLOCATED;\r
+\r
+/* Allocate and initialize png_ptr struct for writing, and any other memory */\r
+extern PNG_EXPORT(png_structp,png_create_write_struct)\r
+   PNGARG((png_const_charp user_png_ver, png_voidp error_ptr,\r
+   png_error_ptr error_fn, png_error_ptr warn_fn)) PNG_ALLOCATED;\r
+\r
+extern PNG_EXPORT(png_size_t,png_get_compression_buffer_size)\r
+   PNGARG((png_structp png_ptr));\r
+\r
+extern PNG_EXPORT(void,png_set_compression_buffer_size)\r
+   PNGARG((png_structp png_ptr, png_size_t size));\r
+\r
+/* Moved from pngconf.h in 1.4.0 and modified to ensure setjmp/longjmp\r
+ * match up.\r
+ */\r
+#ifdef PNG_SETJMP_SUPPORTED\r
+/* This function returns the jmp_buf built in to *png_ptr.  It must be\r
+ * supplied with an appropriate 'longjmp' function to use on that jmp_buf\r
+ * unless the default error function is overridden in which case NULL is\r
+ * acceptable.  The size of the jmp_buf is checked against the actual size\r
+ * allocated by the library - the call will return NULL on a mismatch\r
+ * indicating an ABI mismatch.\r
+ */\r
+extern PNG_EXPORT(jmp_buf*, png_set_longjmp_fn)\r
+   PNGARG((png_structp png_ptr, png_longjmp_ptr longjmp_fn, size_t\r
+       jmp_buf_size));\r
+#  define png_jmpbuf(png_ptr) \\r
+   (*png_set_longjmp_fn((png_ptr), longjmp, sizeof (jmp_buf)))\r
+#else\r
+#  define png_jmpbuf(png_ptr) \\r
+   (LIBPNG_WAS_COMPILED_WITH__PNG_NO_SETJMP)\r
+#endif\r
+\r
+#ifdef PNG_READ_SUPPORTED\r
+/* Reset the compression stream */\r
+extern PNG_EXPORT(int,png_reset_zstream) PNGARG((png_structp png_ptr));\r
+#endif\r
+\r
+/* New functions added in libpng-1.0.2 (not enabled by default until 1.2.0) */\r
+#ifdef PNG_USER_MEM_SUPPORTED\r
+extern PNG_EXPORT(png_structp,png_create_read_struct_2)\r
+   PNGARG((png_const_charp user_png_ver, png_voidp error_ptr,\r
+   png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,\r
+   png_malloc_ptr malloc_fn, png_free_ptr free_fn)) PNG_ALLOCATED;\r
+extern PNG_EXPORT(png_structp,png_create_write_struct_2)\r
+   PNGARG((png_const_charp user_png_ver, png_voidp error_ptr,\r
+   png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,\r
+   png_malloc_ptr malloc_fn, png_free_ptr free_fn)) PNG_ALLOCATED;\r
+#endif\r
+\r
+/* Write the PNG file signature. */\r
+extern PNG_EXPORT(void,png_write_sig) PNGARG((png_structp png_ptr));\r
+\r
+/* Write a PNG chunk - size, type, (optional) data, CRC. */\r
+extern PNG_EXPORT(void,png_write_chunk) PNGARG((png_structp png_ptr,\r
+   png_bytep chunk_name, png_bytep data, png_size_t length));\r
+\r
+/* Write the start of a PNG chunk - length and chunk name. */\r
+extern PNG_EXPORT(void,png_write_chunk_start) PNGARG((png_structp png_ptr,\r
+   png_bytep chunk_name, png_uint_32 length));\r
+\r
+/* Write the data of a PNG chunk started with png_write_chunk_start(). */\r
+extern PNG_EXPORT(void,png_write_chunk_data) PNGARG((png_structp png_ptr,\r
+   png_bytep data, png_size_t length));\r
+\r
+/* Finish a chunk started with png_write_chunk_start() (includes CRC). */\r
+extern PNG_EXPORT(void,png_write_chunk_end) PNGARG((png_structp png_ptr));\r
+\r
+/* Allocate and initialize the info structure */\r
+extern PNG_EXPORT(png_infop,png_create_info_struct)\r
+   PNGARG((png_structp png_ptr)) PNG_ALLOCATED;\r
+\r
+extern PNG_EXPORT(void,png_info_init_3) PNGARG((png_infopp info_ptr,\r
+    png_size_t png_info_struct_size));\r
+\r
+/* Writes all the PNG information before the image. */\r
+extern PNG_EXPORT(void,png_write_info_before_PLTE) PNGARG((png_structp png_ptr,\r
+   png_infop info_ptr));\r
+extern PNG_EXPORT(void,png_write_info) PNGARG((png_structp png_ptr,\r
+   png_infop info_ptr));\r
+\r
+#ifdef PNG_SEQUENTIAL_READ_SUPPORTED\r
+/* Read the information before the actual image data. */\r
+extern PNG_EXPORT(void,png_read_info) PNGARG((png_structp png_ptr,\r
+   png_infop info_ptr));\r
+#endif\r
+\r
+#ifdef PNG_TIME_RFC1123_SUPPORTED\r
+extern PNG_EXPORT(png_charp,png_convert_to_rfc1123)\r
+   PNGARG((png_structp png_ptr, png_timep ptime));\r
+#endif\r
+\r
+#ifdef PNG_CONVERT_tIME_SUPPORTED\r
+/* Convert from a struct tm to png_time */\r
+extern PNG_EXPORT(void,png_convert_from_struct_tm) PNGARG((png_timep ptime,\r
+   struct tm FAR * ttime));\r
+\r
+/* Convert from time_t to png_time.  Uses gmtime() */\r
+extern PNG_EXPORT(void,png_convert_from_time_t) PNGARG((png_timep ptime,\r
+   time_t ttime));\r
+#endif /* PNG_CONVERT_tIME_SUPPORTED */\r
+\r
+#ifdef PNG_READ_EXPAND_SUPPORTED\r
+/* Expand data to 24-bit RGB, or 8-bit grayscale, with alpha if available. */\r
+extern PNG_EXPORT(void,png_set_expand) PNGARG((png_structp png_ptr));\r
+extern PNG_EXPORT(void,png_set_expand_gray_1_2_4_to_8) PNGARG((png_structp\r
+  png_ptr));\r
+extern PNG_EXPORT(void,png_set_palette_to_rgb) PNGARG((png_structp png_ptr));\r
+extern PNG_EXPORT(void,png_set_tRNS_to_alpha) PNGARG((png_structp png_ptr));\r
+#endif\r
+\r
+#if defined(PNG_READ_BGR_SUPPORTED) || defined(PNG_WRITE_BGR_SUPPORTED)\r
+/* Use blue, green, red order for pixels. */\r
+extern PNG_EXPORT(void,png_set_bgr) PNGARG((png_structp png_ptr));\r
+#endif\r
+\r
+#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED\r
+/* Expand the grayscale to 24-bit RGB if necessary. */\r
+extern PNG_EXPORT(void,png_set_gray_to_rgb) PNGARG((png_structp png_ptr));\r
+#endif\r
+\r
+#ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED\r
+/* Reduce RGB to grayscale. */\r
+#ifdef PNG_FLOATING_POINT_SUPPORTED\r
+extern PNG_EXPORT(void,png_set_rgb_to_gray) PNGARG((png_structp png_ptr,\r
+   int error_action, double red, double green ));\r
+#endif\r
+extern PNG_EXPORT(void,png_set_rgb_to_gray_fixed) PNGARG((png_structp png_ptr,\r
+   int error_action, png_fixed_point red, png_fixed_point green ));\r
+extern PNG_EXPORT(png_byte,png_get_rgb_to_gray_status) PNGARG((png_structp\r
+   png_ptr));\r
+#endif\r
+\r
+extern PNG_EXPORT(void,png_build_grayscale_palette) PNGARG((int bit_depth,\r
+   png_colorp palette));\r
+\r
+#ifdef PNG_READ_STRIP_ALPHA_SUPPORTED\r
+extern PNG_EXPORT(void,png_set_strip_alpha) PNGARG((png_structp png_ptr));\r
+#endif\r
+\r
+#if defined(PNG_READ_SWAP_ALPHA_SUPPORTED) || \\r
+    defined(PNG_WRITE_SWAP_ALPHA_SUPPORTED)\r
+extern PNG_EXPORT(void,png_set_swap_alpha) PNGARG((png_structp png_ptr));\r
+#endif\r
+\r
+#if defined(PNG_READ_INVERT_ALPHA_SUPPORTED) || \\r
+    defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED)\r
+extern PNG_EXPORT(void,png_set_invert_alpha) PNGARG((png_structp png_ptr));\r
+#endif\r
+\r
+#if defined(PNG_READ_FILLER_SUPPORTED) || defined(PNG_WRITE_FILLER_SUPPORTED)\r
+/* Add a filler byte to 8-bit Gray or 24-bit RGB images. */\r
+extern PNG_EXPORT(void,png_set_filler) PNGARG((png_structp png_ptr,\r
+   png_uint_32 filler, int flags));\r
+/* The values of the PNG_FILLER_ defines should NOT be changed */\r
+#define PNG_FILLER_BEFORE 0\r
+#define PNG_FILLER_AFTER 1\r
+/* Add an alpha byte to 8-bit Gray or 24-bit RGB images. */\r
+extern PNG_EXPORT(void,png_set_add_alpha) PNGARG((png_structp png_ptr,\r
+   png_uint_32 filler, int flags));\r
+#endif /* PNG_READ_FILLER_SUPPORTED || PNG_WRITE_FILLER_SUPPORTED */\r
+\r
+#if defined(PNG_READ_SWAP_SUPPORTED) || defined(PNG_WRITE_SWAP_SUPPORTED)\r
+/* Swap bytes in 16-bit depth files. */\r
+extern PNG_EXPORT(void,png_set_swap) PNGARG((png_structp png_ptr));\r
+#endif\r
+\r
+#if defined(PNG_READ_PACK_SUPPORTED) || defined(PNG_WRITE_PACK_SUPPORTED)\r
+/* Use 1 byte per pixel in 1, 2, or 4-bit depth files. */\r
+extern PNG_EXPORT(void,png_set_packing) PNGARG((png_structp png_ptr));\r
+#endif\r
+\r
+#if defined(PNG_READ_PACKSWAP_SUPPORTED) || \\r
+    defined(PNG_WRITE_PACKSWAP_SUPPORTED)\r
+/* Swap packing order of pixels in bytes. */\r
+extern PNG_EXPORT(void,png_set_packswap) PNGARG((png_structp png_ptr));\r
+#endif\r
+\r
+#if defined(PNG_READ_SHIFT_SUPPORTED) || defined(PNG_WRITE_SHIFT_SUPPORTED)\r
+/* Converts files to legal bit depths. */\r
+extern PNG_EXPORT(void,png_set_shift) PNGARG((png_structp png_ptr,\r
+   png_color_8p true_bits));\r
+#endif\r
+\r
+#if defined(PNG_READ_INTERLACING_SUPPORTED) || \\r
+    defined(PNG_WRITE_INTERLACING_SUPPORTED)\r
+/* Have the code handle the interlacing.  Returns the number of passes. */\r
+extern PNG_EXPORT(int,png_set_interlace_handling) PNGARG((png_structp png_ptr));\r
+#endif\r
+\r
+#if defined(PNG_READ_INVERT_SUPPORTED) || defined(PNG_WRITE_INVERT_SUPPORTED)\r
+/* Invert monochrome files */\r
+extern PNG_EXPORT(void,png_set_invert_mono) PNGARG((png_structp png_ptr));\r
+#endif\r
+\r
+#ifdef PNG_READ_BACKGROUND_SUPPORTED\r
+/* Handle alpha and tRNS by replacing with a background color. */\r
+#ifdef PNG_FLOATING_POINT_SUPPORTED\r
+extern PNG_EXPORT(void,png_set_background) PNGARG((png_structp png_ptr,\r
+   png_color_16p background_color, int background_gamma_code,\r
+   int need_expand, double background_gamma));\r
+#endif\r
+#define PNG_BACKGROUND_GAMMA_UNKNOWN 0\r
+#define PNG_BACKGROUND_GAMMA_SCREEN  1\r
+#define PNG_BACKGROUND_GAMMA_FILE    2\r
+#define PNG_BACKGROUND_GAMMA_UNIQUE  3\r
+#endif\r
+\r
+#ifdef PNG_READ_16_TO_8_SUPPORTED\r
+/* Strip the second byte of information from a 16-bit depth file. */\r
+extern PNG_EXPORT(void,png_set_strip_16) PNGARG((png_structp png_ptr));\r
+#endif\r
+\r
+#ifdef PNG_READ_QUANTIZE_SUPPORTED\r
+/* Turn on quantizing, and reduce the palette to the number of colors\r
+ * available.  Prior to libpng-1.4.2, this was png_set_dither().\r
+ */\r
+extern PNG_EXPORT(void,png_set_quantize) PNGARG((png_structp png_ptr,\r
+   png_colorp palette, int num_palette, int maximum_colors,\r
+   png_uint_16p histogram, int full_quantize));\r
+#endif\r
+/* This migration aid will be removed from libpng-1.5.0 */\r
+#define png_set_dither png_set_quantize\r
+\r
+#ifdef PNG_READ_GAMMA_SUPPORTED\r
+/* Handle gamma correction. Screen_gamma=(display_exponent) */\r
+#ifdef PNG_FLOATING_POINT_SUPPORTED\r
+extern PNG_EXPORT(void,png_set_gamma) PNGARG((png_structp png_ptr,\r
+   double screen_gamma, double default_file_gamma));\r
+#endif\r
+#endif\r
+\r
+\r
+#ifdef PNG_WRITE_FLUSH_SUPPORTED\r
+/* Set how many lines between output flushes - 0 for no flushing */\r
+extern PNG_EXPORT(void,png_set_flush) PNGARG((png_structp png_ptr, int nrows));\r
+/* Flush the current PNG output buffer */\r
+extern PNG_EXPORT(void,png_write_flush) PNGARG((png_structp png_ptr));\r
+#endif\r
+\r
+/* Optional update palette with requested transformations */\r
+extern PNG_EXPORT(void,png_start_read_image) PNGARG((png_structp png_ptr));\r
+\r
+/* Optional call to update the users info structure */\r
+extern PNG_EXPORT(void,png_read_update_info) PNGARG((png_structp png_ptr,\r
+   png_infop info_ptr));\r
+\r
+#ifdef PNG_SEQUENTIAL_READ_SUPPORTED\r
+/* Read one or more rows of image data. */\r
+extern PNG_EXPORT(void,png_read_rows) PNGARG((png_structp png_ptr,\r
+   png_bytepp row, png_bytepp display_row, png_uint_32 num_rows));\r
+#endif\r
+\r
+#ifdef PNG_SEQUENTIAL_READ_SUPPORTED\r
+/* Read a row of data. */\r
+extern PNG_EXPORT(void,png_read_row) PNGARG((png_structp png_ptr,\r
+   png_bytep row,\r
+   png_bytep display_row));\r
+#endif\r
+\r
+#ifdef PNG_SEQUENTIAL_READ_SUPPORTED\r
+/* Read the whole image into memory at once. */\r
+extern PNG_EXPORT(void,png_read_image) PNGARG((png_structp png_ptr,\r
+   png_bytepp image));\r
+#endif\r
+\r
+/* Write a row of image data */\r
+extern PNG_EXPORT(void,png_write_row) PNGARG((png_structp png_ptr,\r
+   png_bytep row));\r
+\r
+/* Write a few rows of image data */\r
+extern PNG_EXPORT(void,png_write_rows) PNGARG((png_structp png_ptr,\r
+   png_bytepp row, png_uint_32 num_rows));\r
+\r
+/* Write the image data */\r
+extern PNG_EXPORT(void,png_write_image) PNGARG((png_structp png_ptr,\r
+   png_bytepp image));\r
+\r
+/* Write the end of the PNG file. */\r
+extern PNG_EXPORT(void,png_write_end) PNGARG((png_structp png_ptr,\r
+   png_infop info_ptr));\r
+\r
+#ifdef PNG_SEQUENTIAL_READ_SUPPORTED\r
+/* Read the end of the PNG file. */\r
+extern PNG_EXPORT(void,png_read_end) PNGARG((png_structp png_ptr,\r
+   png_infop info_ptr));\r
+#endif\r
+\r
+/* Free any memory associated with the png_info_struct */\r
+extern PNG_EXPORT(void,png_destroy_info_struct) PNGARG((png_structp png_ptr,\r
+   png_infopp info_ptr_ptr));\r
+\r
+/* Free any memory associated with the png_struct and the png_info_structs */\r
+extern PNG_EXPORT(void,png_destroy_read_struct) PNGARG((png_structpp\r
+   png_ptr_ptr, png_infopp info_ptr_ptr, png_infopp end_info_ptr_ptr));\r
+\r
+/* Free any memory associated with the png_struct and the png_info_structs */\r
+extern PNG_EXPORT(void,png_destroy_write_struct)\r
+   PNGARG((png_structpp png_ptr_ptr, png_infopp info_ptr_ptr));\r
+\r
+/* Set the libpng method of handling chunk CRC errors */\r
+extern PNG_EXPORT(void,png_set_crc_action) PNGARG((png_structp png_ptr,\r
+   int crit_action, int ancil_action));\r
+\r
+/* Values for png_set_crc_action() to say how to handle CRC errors in\r
+ * ancillary and critical chunks, and whether to use the data contained\r
+ * therein.  Note that it is impossible to "discard" data in a critical\r
+ * chunk.  For versions prior to 0.90, the action was always error/quit,\r
+ * whereas in version 0.90 and later, the action for CRC errors in ancillary\r
+ * chunks is warn/discard.  These values should NOT be changed.\r
+ *\r
+ *      value                       action:critical     action:ancillary\r
+ */\r
+#define PNG_CRC_DEFAULT       0  /* error/quit          warn/discard data */\r
+#define PNG_CRC_ERROR_QUIT    1  /* error/quit          error/quit        */\r
+#define PNG_CRC_WARN_DISCARD  2  /* (INVALID)           warn/discard data */\r
+#define PNG_CRC_WARN_USE      3  /* warn/use data       warn/use data     */\r
+#define PNG_CRC_QUIET_USE     4  /* quiet/use data      quiet/use data    */\r
+#define PNG_CRC_NO_CHANGE     5  /* use current value   use current value */\r
+\r
+/* These functions give the user control over the scan-line filtering in\r
+ * libpng and the compression methods used by zlib.  These functions are\r
+ * mainly useful for testing, as the defaults should work with most users.\r
+ * Those users who are tight on memory or want faster performance at the\r
+ * expense of compression can modify them.  See the compression library\r
+ * header file (zlib.h) for an explination of the compression functions.\r
+ */\r
+\r
+/* Set the filtering method(s) used by libpng.  Currently, the only valid\r
+ * value for "method" is 0.\r
+ */\r
+extern PNG_EXPORT(void,png_set_filter) PNGARG((png_structp png_ptr, int method,\r
+   int filters));\r
+\r
+/* Flags for png_set_filter() to say which filters to use.  The flags\r
+ * are chosen so that they don't conflict with real filter types\r
+ * below, in case they are supplied instead of the #defined constants.\r
+ * These values should NOT be changed.\r
+ */\r
+#define PNG_NO_FILTERS     0x00\r
+#define PNG_FILTER_NONE    0x08\r
+#define PNG_FILTER_SUB     0x10\r
+#define PNG_FILTER_UP      0x20\r
+#define PNG_FILTER_AVG     0x40\r
+#define PNG_FILTER_PAETH   0x80\r
+#define PNG_ALL_FILTERS (PNG_FILTER_NONE | PNG_FILTER_SUB | PNG_FILTER_UP | \\r
+                         PNG_FILTER_AVG | PNG_FILTER_PAETH)\r
+\r
+/* Filter values (not flags) - used in pngwrite.c, pngwutil.c for now.\r
+ * These defines should NOT be changed.\r
+ */\r
+#define PNG_FILTER_VALUE_NONE  0\r
+#define PNG_FILTER_VALUE_SUB   1\r
+#define PNG_FILTER_VALUE_UP    2\r
+#define PNG_FILTER_VALUE_AVG   3\r
+#define PNG_FILTER_VALUE_PAETH 4\r
+#define PNG_FILTER_VALUE_LAST  5\r
+\r
+#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED /* EXPERIMENTAL */\r
+/* The "heuristic_method" is given by one of the PNG_FILTER_HEURISTIC_\r
+ * defines, either the default (minimum-sum-of-absolute-differences), or\r
+ * the experimental method (weighted-minimum-sum-of-absolute-differences).\r
+ *\r
+ * Weights are factors >= 1.0, indicating how important it is to keep the\r
+ * filter type consistent between rows.  Larger numbers mean the current\r
+ * filter is that many times as likely to be the same as the "num_weights"\r
+ * previous filters.  This is cumulative for each previous row with a weight.\r
+ * There needs to be "num_weights" values in "filter_weights", or it can be\r
+ * NULL if the weights aren't being specified.  Weights have no influence on\r
+ * the selection of the first row filter.  Well chosen weights can (in theory)\r
+ * improve the compression for a given image.\r
+ *\r
+ * Costs are factors >= 1.0 indicating the relative decoding costs of a\r
+ * filter type.  Higher costs indicate more decoding expense, and are\r
+ * therefore less likely to be selected over a filter with lower computational\r
+ * costs.  There needs to be a value in "filter_costs" for each valid filter\r
+ * type (given by PNG_FILTER_VALUE_LAST), or it can be NULL if you aren't\r
+ * setting the costs.  Costs try to improve the speed of decompression without\r
+ * unduly increasing the compressed image size.\r
+ *\r
+ * A negative weight or cost indicates the default value is to be used, and\r
+ * values in the range [0.0, 1.0) indicate the value is to remain unchanged.\r
+ * The default values for both weights and costs are currently 1.0, but may\r
+ * change if good general weighting/cost heuristics can be found.  If both\r
+ * the weights and costs are set to 1.0, this degenerates the WEIGHTED method\r
+ * to the UNWEIGHTED method, but with added encoding time/computation.\r
+ */\r
+#ifdef PNG_FLOATING_POINT_SUPPORTED\r
+extern PNG_EXPORT(void,png_set_filter_heuristics) PNGARG((png_structp png_ptr,\r
+   int heuristic_method, int num_weights, png_doublep filter_weights,\r
+   png_doublep filter_costs));\r
+#endif\r
+#endif /*  PNG_WRITE_WEIGHTED_FILTER_SUPPORTED */\r
+\r
+/* Heuristic used for row filter selection.  These defines should NOT be\r
+ * changed.\r
+ */\r
+#define PNG_FILTER_HEURISTIC_DEFAULT    0  /* Currently "UNWEIGHTED" */\r
+#define PNG_FILTER_HEURISTIC_UNWEIGHTED 1  /* Used by libpng < 0.95 */\r
+#define PNG_FILTER_HEURISTIC_WEIGHTED   2  /* Experimental feature */\r
+#define PNG_FILTER_HEURISTIC_LAST       3  /* Not a valid value */\r
+\r
+/* Set the library compression level.  Currently, valid values range from\r
+ * 0 - 9, corresponding directly to the zlib compression levels 0 - 9\r
+ * (0 - no compression, 9 - "maximal" compression).  Note that tests have\r
+ * shown that zlib compression levels 3-6 usually perform as well as level 9\r
+ * for PNG images, and do considerably fewer caclulations.  In the future,\r
+ * these values may not correspond directly to the zlib compression levels.\r
+ */\r
+extern PNG_EXPORT(void,png_set_compression_level) PNGARG((png_structp png_ptr,\r
+   int level));\r
+\r
+extern PNG_EXPORT(void,png_set_compression_mem_level)\r
+   PNGARG((png_structp png_ptr, int mem_level));\r
+\r
+extern PNG_EXPORT(void,png_set_compression_strategy)\r
+   PNGARG((png_structp png_ptr, int strategy));\r
+\r
+extern PNG_EXPORT(void,png_set_compression_window_bits)\r
+   PNGARG((png_structp png_ptr, int window_bits));\r
+\r
+extern PNG_EXPORT(void,png_set_compression_method) PNGARG((png_structp png_ptr,\r
+   int method));\r
+\r
+/* These next functions are called for input/output, memory, and error\r
+ * handling.  They are in the file pngrio.c, pngwio.c, and pngerror.c,\r
+ * and call standard C I/O routines such as fread(), fwrite(), and\r
+ * fprintf().  These functions can be made to use other I/O routines\r
+ * at run time for those applications that need to handle I/O in a\r
+ * different manner by calling png_set_???_fn().  See libpng.txt for\r
+ * more information.\r
+ */\r
+\r
+#ifdef PNG_STDIO_SUPPORTED\r
+/* Initialize the input/output for the PNG file to the default functions. */\r
+extern PNG_EXPORT(void,png_init_io) PNGARG((png_structp png_ptr,\r
+    png_FILE_p fp));\r
+#endif\r
+\r
+/* Replace the (error and abort), and warning functions with user\r
+ * supplied functions.  If no messages are to be printed you must still\r
+ * write and use replacement functions. The replacement error_fn should\r
+ * still do a longjmp to the last setjmp location if you are using this\r
+ * method of error handling.  If error_fn or warning_fn is NULL, the\r
+ * default function will be used.\r
+ */\r
+\r
+extern PNG_EXPORT(void,png_set_error_fn) PNGARG((png_structp png_ptr,\r
+   png_voidp error_ptr, png_error_ptr error_fn, png_error_ptr warning_fn));\r
+\r
+/* Return the user pointer associated with the error functions */\r
+extern PNG_EXPORT(png_voidp,png_get_error_ptr) PNGARG((png_structp png_ptr));\r
+\r
+/* Replace the default data output functions with a user supplied one(s).\r
+ * If buffered output is not used, then output_flush_fn can be set to NULL.\r
+ * If PNG_WRITE_FLUSH_SUPPORTED is not defined at libpng compile time\r
+ * output_flush_fn will be ignored (and thus can be NULL).\r
+ * It is probably a mistake to use NULL for output_flush_fn if\r
+ * write_data_fn is not also NULL unless you have built libpng with\r
+ * PNG_WRITE_FLUSH_SUPPORTED undefined, because in this case libpng's\r
+ * default flush function, which uses the standard *FILE structure, will\r
+ * be used.\r
+ */\r
+extern PNG_EXPORT(void,png_set_write_fn) PNGARG((png_structp png_ptr,\r
+   png_voidp io_ptr, png_rw_ptr write_data_fn, png_flush_ptr output_flush_fn));\r
+\r
+/* Replace the default data input function with a user supplied one. */\r
+extern PNG_EXPORT(void,png_set_read_fn) PNGARG((png_structp png_ptr,\r
+   png_voidp io_ptr, png_rw_ptr read_data_fn));\r
+\r
+/* Return the user pointer associated with the I/O functions */\r
+extern PNG_EXPORT(png_voidp,png_get_io_ptr) PNGARG((png_structp png_ptr));\r
+\r
+extern PNG_EXPORT(void,png_set_read_status_fn) PNGARG((png_structp png_ptr,\r
+   png_read_status_ptr read_row_fn));\r
+\r
+extern PNG_EXPORT(void,png_set_write_status_fn) PNGARG((png_structp png_ptr,\r
+   png_write_status_ptr write_row_fn));\r
+\r
+#ifdef PNG_USER_MEM_SUPPORTED\r
+/* Replace the default memory allocation functions with user supplied one(s). */\r
+extern PNG_EXPORT(void,png_set_mem_fn) PNGARG((png_structp png_ptr,\r
+   png_voidp mem_ptr, png_malloc_ptr malloc_fn, png_free_ptr free_fn));\r
+/* Return the user pointer associated with the memory functions */\r
+extern PNG_EXPORT(png_voidp,png_get_mem_ptr) PNGARG((png_structp png_ptr));\r
+#endif\r
+\r
+#ifdef PNG_READ_USER_TRANSFORM_SUPPORTED\r
+extern PNG_EXPORT(void,png_set_read_user_transform_fn) PNGARG((png_structp\r
+   png_ptr, png_user_transform_ptr read_user_transform_fn));\r
+#endif\r
+\r
+#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED\r
+extern PNG_EXPORT(void,png_set_write_user_transform_fn) PNGARG((png_structp\r
+   png_ptr, png_user_transform_ptr write_user_transform_fn));\r
+#endif\r
+\r
+#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \\r
+    defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)\r
+extern PNG_EXPORT(void,png_set_user_transform_info) PNGARG((png_structp\r
+   png_ptr, png_voidp user_transform_ptr, int user_transform_depth,\r
+   int user_transform_channels));\r
+/* Return the user pointer associated with the user transform functions */\r
+extern PNG_EXPORT(png_voidp,png_get_user_transform_ptr)\r
+   PNGARG((png_structp png_ptr));\r
+#endif\r
+\r
+#ifdef PNG_USER_CHUNKS_SUPPORTED\r
+extern PNG_EXPORT(void,png_set_read_user_chunk_fn) PNGARG((png_structp png_ptr,\r
+   png_voidp user_chunk_ptr, png_user_chunk_ptr read_user_chunk_fn));\r
+extern PNG_EXPORT(png_voidp,png_get_user_chunk_ptr) PNGARG((png_structp\r
+   png_ptr));\r
+#endif\r
+\r
+#ifdef PNG_PROGRESSIVE_READ_SUPPORTED\r
+/* Sets the function callbacks for the push reader, and a pointer to a\r
+ * user-defined structure available to the callback functions.\r
+ */\r
+extern PNG_EXPORT(void,png_set_progressive_read_fn) PNGARG((png_structp png_ptr,\r
+   png_voidp progressive_ptr,\r
+   png_progressive_info_ptr info_fn, png_progressive_row_ptr row_fn,\r
+   png_progressive_end_ptr end_fn));\r
+\r
+/* Returns the user pointer associated with the push read functions */\r
+extern PNG_EXPORT(png_voidp,png_get_progressive_ptr)\r
+   PNGARG((png_structp png_ptr));\r
+\r
+/* Function to be called when data becomes available */\r
+extern PNG_EXPORT(void,png_process_data) PNGARG((png_structp png_ptr,\r
+   png_infop info_ptr, png_bytep buffer, png_size_t buffer_size));\r
+\r
+/* Function that combines rows.  Not very much different than the\r
+ * png_combine_row() call.  Is this even used?????\r
+ */\r
+extern PNG_EXPORT(void,png_progressive_combine_row) PNGARG((png_structp png_ptr,\r
+   png_bytep old_row, png_bytep new_row));\r
+#endif /* PNG_PROGRESSIVE_READ_SUPPORTED */\r
+\r
+extern PNG_EXPORT(png_voidp,png_malloc) PNGARG((png_structp png_ptr,\r
+   png_alloc_size_t size)) PNG_ALLOCATED;\r
+/* Added at libpng version 1.4.0 */\r
+extern PNG_EXPORT(png_voidp,png_calloc) PNGARG((png_structp png_ptr,\r
+   png_alloc_size_t size)) PNG_ALLOCATED;\r
+\r
+/* Added at libpng version 1.2.4 */\r
+extern PNG_EXPORT(png_voidp,png_malloc_warn) PNGARG((png_structp png_ptr,\r
+   png_alloc_size_t size)) PNG_ALLOCATED;\r
+\r
+/* Frees a pointer allocated by png_malloc() */\r
+extern PNG_EXPORT(void,png_free) PNGARG((png_structp png_ptr, png_voidp ptr));\r
+\r
+/* Free data that was allocated internally */\r
+extern PNG_EXPORT(void,png_free_data) PNGARG((png_structp png_ptr,\r
+   png_infop info_ptr, png_uint_32 free_me, int num));\r
+/* Reassign responsibility for freeing existing data, whether allocated\r
+ * by libpng or by the application */\r
+extern PNG_EXPORT(void,png_data_freer) PNGARG((png_structp png_ptr,\r
+   png_infop info_ptr, int freer, png_uint_32 mask));\r
+/* Assignments for png_data_freer */\r
+#define PNG_DESTROY_WILL_FREE_DATA 1\r
+#define PNG_SET_WILL_FREE_DATA 1\r
+#define PNG_USER_WILL_FREE_DATA 2\r
+/* Flags for png_ptr->free_me and info_ptr->free_me */\r
+#define PNG_FREE_HIST 0x0008\r
+#define PNG_FREE_ICCP 0x0010\r
+#define PNG_FREE_SPLT 0x0020\r
+#define PNG_FREE_ROWS 0x0040\r
+#define PNG_FREE_PCAL 0x0080\r
+#define PNG_FREE_SCAL 0x0100\r
+#define PNG_FREE_UNKN 0x0200\r
+#define PNG_FREE_LIST 0x0400\r
+#define PNG_FREE_PLTE 0x1000\r
+#define PNG_FREE_TRNS 0x2000\r
+#define PNG_FREE_TEXT 0x4000\r
+#define PNG_FREE_ALL  0x7fff\r
+#define PNG_FREE_MUL  0x4220 /* PNG_FREE_SPLT|PNG_FREE_TEXT|PNG_FREE_UNKN */\r
+\r
+#ifdef PNG_USER_MEM_SUPPORTED\r
+extern PNG_EXPORT(png_voidp,png_malloc_default) PNGARG((png_structp png_ptr,\r
+   png_alloc_size_t size)) PNG_ALLOCATED;\r
+extern PNG_EXPORT(void,png_free_default) PNGARG((png_structp png_ptr,\r
+   png_voidp ptr));\r
+#endif\r
+\r
+#ifndef PNG_NO_ERROR_TEXT\r
+/* Fatal error in PNG image of libpng - can't continue */\r
+extern PNG_EXPORT(void,png_error) PNGARG((png_structp png_ptr,\r
+   png_const_charp error_message)) PNG_NORETURN;\r
+\r
+/* The same, but the chunk name is prepended to the error string. */\r
+extern PNG_EXPORT(void,png_chunk_error) PNGARG((png_structp png_ptr,\r
+   png_const_charp error_message)) PNG_NORETURN;\r
+\r
+#else\r
+/* Fatal error in PNG image of libpng - can't continue */\r
+extern PNG_EXPORT(void,png_err) PNGARG((png_structp png_ptr)) PNG_NORETURN;\r
+#endif\r
+\r
+/* Non-fatal error in libpng.  Can continue, but may have a problem. */\r
+extern PNG_EXPORT(void,png_warning) PNGARG((png_structp png_ptr,\r
+   png_const_charp warning_message));\r
+\r
+/* Non-fatal error in libpng, chunk name is prepended to message. */\r
+extern PNG_EXPORT(void,png_chunk_warning) PNGARG((png_structp png_ptr,\r
+   png_const_charp warning_message));\r
+\r
+#ifdef PNG_BENIGN_ERRORS_SUPPORTED\r
+/* Benign error in libpng.  Can continue, but may have a problem.\r
+ * User can choose whether to handle as a fatal error or as a warning. */\r
+extern PNG_EXPORT(void,png_benign_error) PNGARG((png_structp png_ptr,\r
+   png_const_charp warning_message));\r
+\r
+/* Same, chunk name is prepended to message. */\r
+extern PNG_EXPORT(void,png_chunk_benign_error) PNGARG((png_structp png_ptr,\r
+   png_const_charp warning_message));\r
+\r
+extern PNG_EXPORT(void,png_set_benign_errors) PNGARG((png_structp\r
+   png_ptr, int allowed));\r
+#endif\r
+\r
+/* The png_set_<chunk> functions are for storing values in the png_info_struct.\r
+ * Similarly, the png_get_<chunk> calls are used to read values from the\r
+ * png_info_struct, either storing the parameters in the passed variables, or\r
+ * setting pointers into the png_info_struct where the data is stored.  The\r
+ * png_get_<chunk> functions return a non-zero value if the data was available\r
+ * in info_ptr, or return zero and do not change any of the parameters if the\r
+ * data was not available.\r
+ *\r
+ * These functions should be used instead of directly accessing png_info\r
+ * to avoid problems with future changes in the size and internal layout of\r
+ * png_info_struct.\r
+ */\r
+/* Returns "flag" if chunk data is valid in info_ptr. */\r
+extern PNG_EXPORT(png_uint_32,png_get_valid) PNGARG((png_structp png_ptr,\r
+png_infop info_ptr, png_uint_32 flag));\r
+\r
+/* Returns number of bytes needed to hold a transformed row. */\r
+extern PNG_EXPORT(png_size_t,png_get_rowbytes) PNGARG((png_structp png_ptr,\r
+png_infop info_ptr));\r
+\r
+#ifdef PNG_INFO_IMAGE_SUPPORTED\r
+/* Returns row_pointers, which is an array of pointers to scanlines that was\r
+ * returned from png_read_png().\r
+ */\r
+extern PNG_EXPORT(png_bytepp,png_get_rows) PNGARG((png_structp png_ptr,\r
+png_infop info_ptr));\r
+/* Set row_pointers, which is an array of pointers to scanlines for use\r
+ * by png_write_png().\r
+ */\r
+extern PNG_EXPORT(void,png_set_rows) PNGARG((png_structp png_ptr,\r
+   png_infop info_ptr, png_bytepp row_pointers));\r
+#endif\r
+\r
+/* Returns number of color channels in image. */\r
+extern PNG_EXPORT(png_byte,png_get_channels) PNGARG((png_structp png_ptr,\r
+png_infop info_ptr));\r
+\r
+#ifdef PNG_EASY_ACCESS_SUPPORTED\r
+/* Returns image width in pixels. */\r
+extern PNG_EXPORT(png_uint_32, png_get_image_width) PNGARG((png_structp\r
+png_ptr, png_infop info_ptr));\r
+\r
+/* Returns image height in pixels. */\r
+extern PNG_EXPORT(png_uint_32, png_get_image_height) PNGARG((png_structp\r
+png_ptr, png_infop info_ptr));\r
+\r
+/* Returns image bit_depth. */\r
+extern PNG_EXPORT(png_byte, png_get_bit_depth) PNGARG((png_structp\r
+png_ptr, png_infop info_ptr));\r
+\r
+/* Returns image color_type. */\r
+extern PNG_EXPORT(png_byte, png_get_color_type) PNGARG((png_structp\r
+png_ptr, png_infop info_ptr));\r
+\r
+/* Returns image filter_type. */\r
+extern PNG_EXPORT(png_byte, png_get_filter_type) PNGARG((png_structp\r
+png_ptr, png_infop info_ptr));\r
+\r
+/* Returns image interlace_type. */\r
+extern PNG_EXPORT(png_byte, png_get_interlace_type) PNGARG((png_structp\r
+png_ptr, png_infop info_ptr));\r
+\r
+/* Returns image compression_type. */\r
+extern PNG_EXPORT(png_byte, png_get_compression_type) PNGARG((png_structp\r
+png_ptr, png_infop info_ptr));\r
+\r
+/* Returns image resolution in pixels per meter, from pHYs chunk data. */\r
+extern PNG_EXPORT(png_uint_32, png_get_pixels_per_meter) PNGARG((png_structp\r
+png_ptr, png_infop info_ptr));\r
+extern PNG_EXPORT(png_uint_32, png_get_x_pixels_per_meter) PNGARG((png_structp\r
+png_ptr, png_infop info_ptr));\r
+extern PNG_EXPORT(png_uint_32, png_get_y_pixels_per_meter) PNGARG((png_structp\r
+png_ptr, png_infop info_ptr));\r
+\r
+/* Returns pixel aspect ratio, computed from pHYs chunk data.  */\r
+#ifdef PNG_FLOATING_POINT_SUPPORTED\r
+extern PNG_EXPORT(float, png_get_pixel_aspect_ratio) PNGARG((png_structp\r
+png_ptr, png_infop info_ptr));\r
+#endif\r
+\r
+/* Returns image x, y offset in pixels or microns, from oFFs chunk data. */\r
+extern PNG_EXPORT(png_int_32, png_get_x_offset_pixels) PNGARG((png_structp\r
+png_ptr, png_infop info_ptr));\r
+extern PNG_EXPORT(png_int_32, png_get_y_offset_pixels) PNGARG((png_structp\r
+png_ptr, png_infop info_ptr));\r
+extern PNG_EXPORT(png_int_32, png_get_x_offset_microns) PNGARG((png_structp\r
+png_ptr, png_infop info_ptr));\r
+extern PNG_EXPORT(png_int_32, png_get_y_offset_microns) PNGARG((png_structp\r
+png_ptr, png_infop info_ptr));\r
+\r
+#endif /* PNG_EASY_ACCESS_SUPPORTED */\r
+\r
+/* Returns pointer to signature string read from PNG header */\r
+extern PNG_EXPORT(png_bytep,png_get_signature) PNGARG((png_structp png_ptr,\r
+png_infop info_ptr));\r
+\r
+#ifdef PNG_bKGD_SUPPORTED\r
+extern PNG_EXPORT(png_uint_32,png_get_bKGD) PNGARG((png_structp png_ptr,\r
+   png_infop info_ptr, png_color_16p *background));\r
+#endif\r
+\r
+#ifdef PNG_bKGD_SUPPORTED\r
+extern PNG_EXPORT(void,png_set_bKGD) PNGARG((png_structp png_ptr,\r
+   png_infop info_ptr, png_color_16p background));\r
+#endif\r
+\r
+#ifdef PNG_cHRM_SUPPORTED\r
+#ifdef PNG_FLOATING_POINT_SUPPORTED\r
+extern PNG_EXPORT(png_uint_32,png_get_cHRM) PNGARG((png_structp png_ptr,\r
+   png_infop info_ptr, double *white_x, double *white_y, double *red_x,\r
+   double *red_y, double *green_x, double *green_y, double *blue_x,\r
+   double *blue_y));\r
+#endif\r
+#ifdef PNG_FIXED_POINT_SUPPORTED\r
+extern PNG_EXPORT(png_uint_32,png_get_cHRM_fixed) PNGARG((png_structp png_ptr,\r
+   png_infop info_ptr, png_fixed_point *int_white_x, png_fixed_point\r
+   *int_white_y, png_fixed_point *int_red_x, png_fixed_point *int_red_y,\r
+   png_fixed_point *int_green_x, png_fixed_point *int_green_y, png_fixed_point\r
+   *int_blue_x, png_fixed_point *int_blue_y));\r
+#endif\r
+#endif\r
+\r
+#ifdef PNG_cHRM_SUPPORTED\r
+#ifdef PNG_FLOATING_POINT_SUPPORTED\r
+extern PNG_EXPORT(void,png_set_cHRM) PNGARG((png_structp png_ptr,\r
+   png_infop info_ptr, double white_x, double white_y, double red_x,\r
+   double red_y, double green_x, double green_y, double blue_x, double blue_y));\r
+#endif\r
+#ifdef PNG_FIXED_POINT_SUPPORTED\r
+extern PNG_EXPORT(void,png_set_cHRM_fixed) PNGARG((png_structp png_ptr,\r
+   png_infop info_ptr, png_fixed_point int_white_x, png_fixed_point int_white_y,\r
+   png_fixed_point int_red_x, png_fixed_point int_red_y, png_fixed_point\r
+   int_green_x, png_fixed_point int_green_y, png_fixed_point int_blue_x,\r
+   png_fixed_point int_blue_y));\r
+#endif\r
+#endif\r
+\r
+#ifdef PNG_gAMA_SUPPORTED\r
+#ifdef PNG_FLOATING_POINT_SUPPORTED\r
+extern PNG_EXPORT(png_uint_32,png_get_gAMA) PNGARG((png_structp png_ptr,\r
+   png_infop info_ptr, double *file_gamma));\r
+#endif\r
+extern PNG_EXPORT(png_uint_32,png_get_gAMA_fixed) PNGARG((png_structp png_ptr,\r
+   png_infop info_ptr, png_fixed_point *int_file_gamma));\r
+#endif\r
+\r
+#ifdef PNG_gAMA_SUPPORTED\r
+#ifdef PNG_FLOATING_POINT_SUPPORTED\r
+extern PNG_EXPORT(void,png_set_gAMA) PNGARG((png_structp png_ptr,\r
+   png_infop info_ptr, double file_gamma));\r
+#endif\r
+extern PNG_EXPORT(void,png_set_gAMA_fixed) PNGARG((png_structp png_ptr,\r
+   png_infop info_ptr, png_fixed_point int_file_gamma));\r
+#endif\r
+\r
+#ifdef PNG_hIST_SUPPORTED\r
+extern PNG_EXPORT(png_uint_32,png_get_hIST) PNGARG((png_structp png_ptr,\r
+   png_infop info_ptr, png_uint_16p *hist));\r
+#endif\r
+\r
+#ifdef PNG_hIST_SUPPORTED\r
+extern PNG_EXPORT(void,png_set_hIST) PNGARG((png_structp png_ptr,\r
+   png_infop info_ptr, png_uint_16p hist));\r
+#endif\r
+\r
+extern PNG_EXPORT(png_uint_32,png_get_IHDR) PNGARG((png_structp png_ptr,\r
+   png_infop info_ptr, png_uint_32 *width, png_uint_32 *height,\r
+   int *bit_depth, int *color_type, int *interlace_method,\r
+   int *compression_method, int *filter_method));\r
+\r
+extern PNG_EXPORT(void,png_set_IHDR) PNGARG((png_structp png_ptr,\r
+   png_infop info_ptr, png_uint_32 width, png_uint_32 height, int bit_depth,\r
+   int color_type, int interlace_method, int compression_method,\r
+   int filter_method));\r
+\r
+#ifdef PNG_oFFs_SUPPORTED\r
+extern PNG_EXPORT(png_uint_32,png_get_oFFs) PNGARG((png_structp png_ptr,\r
+   png_infop info_ptr, png_int_32 *offset_x, png_int_32 *offset_y,\r
+   int *unit_type));\r
+#endif\r
+\r
+#ifdef PNG_oFFs_SUPPORTED\r
+extern PNG_EXPORT(void,png_set_oFFs) PNGARG((png_structp png_ptr,\r
+   png_infop info_ptr, png_int_32 offset_x, png_int_32 offset_y,\r
+   int unit_type));\r
+#endif\r
+\r
+#ifdef PNG_pCAL_SUPPORTED\r
+extern PNG_EXPORT(png_uint_32,png_get_pCAL) PNGARG((png_structp png_ptr,\r
+   png_infop info_ptr, png_charp *purpose, png_int_32 *X0, png_int_32 *X1,\r
+   int *type, int *nparams, png_charp *units, png_charpp *params));\r
+#endif\r
+\r
+#ifdef PNG_pCAL_SUPPORTED\r
+extern PNG_EXPORT(void,png_set_pCAL) PNGARG((png_structp png_ptr,\r
+   png_infop info_ptr, png_charp purpose, png_int_32 X0, png_int_32 X1,\r
+   int type, int nparams, png_charp units, png_charpp params));\r
+#endif\r
+\r
+#ifdef PNG_pHYs_SUPPORTED\r
+extern PNG_EXPORT(png_uint_32,png_get_pHYs) PNGARG((png_structp png_ptr,\r
+   png_infop info_ptr, png_uint_32 *res_x, png_uint_32 *res_y, int *unit_type));\r
+#endif\r
+\r
+#ifdef PNG_pHYs_SUPPORTED\r
+extern PNG_EXPORT(void,png_set_pHYs) PNGARG((png_structp png_ptr,\r
+   png_infop info_ptr, png_uint_32 res_x, png_uint_32 res_y, int unit_type));\r
+#endif\r
+\r
+extern PNG_EXPORT(png_uint_32,png_get_PLTE) PNGARG((png_structp png_ptr,\r
+   png_infop info_ptr, png_colorp *palette, int *num_palette));\r
+\r
+extern PNG_EXPORT(void,png_set_PLTE) PNGARG((png_structp png_ptr,\r
+   png_infop info_ptr, png_colorp palette, int num_palette));\r
+\r
+#ifdef PNG_sBIT_SUPPORTED\r
+extern PNG_EXPORT(png_uint_32,png_get_sBIT) PNGARG((png_structp png_ptr,\r
+   png_infop info_ptr, png_color_8p *sig_bit));\r
+#endif\r
+\r
+#ifdef PNG_sBIT_SUPPORTED\r
+extern PNG_EXPORT(void,png_set_sBIT) PNGARG((png_structp png_ptr,\r
+   png_infop info_ptr, png_color_8p sig_bit));\r
+#endif\r
+\r
+#ifdef PNG_sRGB_SUPPORTED\r
+extern PNG_EXPORT(png_uint_32,png_get_sRGB) PNGARG((png_structp png_ptr,\r
+   png_infop info_ptr, int *intent));\r
+#endif\r
+\r
+#ifdef PNG_sRGB_SUPPORTED\r
+extern PNG_EXPORT(void,png_set_sRGB) PNGARG((png_structp png_ptr,\r
+   png_infop info_ptr, int intent));\r
+extern PNG_EXPORT(void,png_set_sRGB_gAMA_and_cHRM) PNGARG((png_structp png_ptr,\r
+   png_infop info_ptr, int intent));\r
+#endif\r
+\r
+#ifdef PNG_iCCP_SUPPORTED\r
+extern PNG_EXPORT(png_uint_32,png_get_iCCP) PNGARG((png_structp png_ptr,\r
+   png_infop info_ptr, png_charpp name, int *compression_type,\r
+   png_charpp profile, png_uint_32 *proflen));\r
+   /* Note to maintainer: profile should be png_bytepp */\r
+#endif\r
+\r
+#ifdef PNG_iCCP_SUPPORTED\r
+extern PNG_EXPORT(void,png_set_iCCP) PNGARG((png_structp png_ptr,\r
+   png_infop info_ptr, png_charp name, int compression_type,\r
+   png_charp profile, png_uint_32 proflen));\r
+   /* Note to maintainer: profile should be png_bytep */\r
+#endif\r
+\r
+#ifdef PNG_sPLT_SUPPORTED\r
+extern PNG_EXPORT(png_uint_32,png_get_sPLT) PNGARG((png_structp png_ptr,\r
+   png_infop info_ptr, png_sPLT_tpp entries));\r
+#endif\r
+\r
+#ifdef PNG_sPLT_SUPPORTED\r
+extern PNG_EXPORT(void,png_set_sPLT) PNGARG((png_structp png_ptr,\r
+   png_infop info_ptr, png_sPLT_tp entries, int nentries));\r
+#endif\r
+\r
+#ifdef PNG_TEXT_SUPPORTED\r
+/* png_get_text also returns the number of text chunks in *num_text */\r
+extern PNG_EXPORT(png_uint_32,png_get_text) PNGARG((png_structp png_ptr,\r
+   png_infop info_ptr, png_textp *text_ptr, int *num_text));\r
+#endif\r
+\r
+/* Note while png_set_text() will accept a structure whose text,\r
+ * language, and  translated keywords are NULL pointers, the structure\r
+ * returned by png_get_text will always contain regular\r
+ * zero-terminated C strings.  They might be empty strings but\r
+ * they will never be NULL pointers.\r
+ */\r
+\r
+#ifdef PNG_TEXT_SUPPORTED\r
+extern PNG_EXPORT(void,png_set_text) PNGARG((png_structp png_ptr,\r
+   png_infop info_ptr, png_textp text_ptr, int num_text));\r
+#endif\r
+\r
+#ifdef PNG_tIME_SUPPORTED\r
+extern PNG_EXPORT(png_uint_32,png_get_tIME) PNGARG((png_structp png_ptr,\r
+   png_infop info_ptr, png_timep *mod_time));\r
+#endif\r
+\r
+#ifdef PNG_tIME_SUPPORTED\r
+extern PNG_EXPORT(void,png_set_tIME) PNGARG((png_structp png_ptr,\r
+   png_infop info_ptr, png_timep mod_time));\r
+#endif\r
+\r
+#ifdef PNG_tRNS_SUPPORTED\r
+extern PNG_EXPORT(png_uint_32,png_get_tRNS) PNGARG((png_structp png_ptr,\r
+   png_infop info_ptr, png_bytep *trans_alpha, int *num_trans,\r
+   png_color_16p *trans_color));\r
+#endif\r
+\r
+#ifdef PNG_tRNS_SUPPORTED\r
+extern PNG_EXPORT(void,png_set_tRNS) PNGARG((png_structp png_ptr,\r
+   png_infop info_ptr, png_bytep trans_alpha, int num_trans,\r
+   png_color_16p trans_color));\r
+#endif\r
+\r
+#ifdef PNG_tRNS_SUPPORTED\r
+#endif\r
+\r
+#ifdef PNG_sCAL_SUPPORTED\r
+#ifdef PNG_FLOATING_POINT_SUPPORTED\r
+extern PNG_EXPORT(png_uint_32,png_get_sCAL) PNGARG((png_structp png_ptr,\r
+   png_infop info_ptr, int *unit, double *width, double *height));\r
+#else\r
+#ifdef PNG_FIXED_POINT_SUPPORTED\r
+extern PNG_EXPORT(png_uint_32,png_get_sCAL_s) PNGARG((png_structp png_ptr,\r
+   png_infop info_ptr, int *unit, png_charpp swidth, png_charpp sheight));\r
+#endif\r
+#endif\r
+#endif /* PNG_sCAL_SUPPORTED */\r
+\r
+#ifdef PNG_sCAL_SUPPORTED\r
+#ifdef PNG_FLOATING_POINT_SUPPORTED\r
+extern PNG_EXPORT(void,png_set_sCAL) PNGARG((png_structp png_ptr,\r
+   png_infop info_ptr, int unit, double width, double height));\r
+#else\r
+#ifdef PNG_FIXED_POINT_SUPPORTED\r
+extern PNG_EXPORT(void,png_set_sCAL_s) PNGARG((png_structp png_ptr,\r
+   png_infop info_ptr, int unit, png_charp swidth, png_charp sheight));\r
+#endif\r
+#endif\r
+#endif /* PNG_sCAL_SUPPORTED || PNG_WRITE_sCAL_SUPPORTED */\r
+\r
+#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED\r
+/* Provide a list of chunks and how they are to be handled, if the built-in\r
+   handling or default unknown chunk handling is not desired.  Any chunks not\r
+   listed will be handled in the default manner.  The IHDR and IEND chunks\r
+   must not be listed.\r
+      keep = 0: follow default behaviour\r
+           = 1: do not keep\r
+           = 2: keep only if safe-to-copy\r
+           = 3: keep even if unsafe-to-copy\r
+*/\r
+extern PNG_EXPORT(void, png_set_keep_unknown_chunks) PNGARG((png_structp\r
+   png_ptr, int keep, png_bytep chunk_list, int num_chunks));\r
+PNG_EXPORT(int,png_handle_as_unknown) PNGARG((png_structp png_ptr, png_bytep\r
+   chunk_name));\r
+#endif\r
+#ifdef PNG_UNKNOWN_CHUNKS_SUPPORTED\r
+extern PNG_EXPORT(void, png_set_unknown_chunks) PNGARG((png_structp png_ptr,\r
+   png_infop info_ptr, png_unknown_chunkp unknowns, int num_unknowns));\r
+extern PNG_EXPORT(void, png_set_unknown_chunk_location)\r
+   PNGARG((png_structp png_ptr, png_infop info_ptr, int chunk, int location));\r
+extern PNG_EXPORT(png_uint_32,png_get_unknown_chunks) PNGARG((png_structp\r
+   png_ptr, png_infop info_ptr, png_unknown_chunkpp entries));\r
+#endif\r
+\r
+/* Png_free_data() will turn off the "valid" flag for anything it frees.\r
+ * If you need to turn it off for a chunk that your application has freed,\r
+ * you can use png_set_invalid(png_ptr, info_ptr, PNG_INFO_CHNK);\r
+ */\r
+extern PNG_EXPORT(void, png_set_invalid) PNGARG((png_structp png_ptr,\r
+   png_infop info_ptr, int mask));\r
+\r
+#ifdef PNG_INFO_IMAGE_SUPPORTED\r
+/* The "params" pointer is currently not used and is for future expansion. */\r
+extern PNG_EXPORT(void, png_read_png) PNGARG((png_structp png_ptr,\r
+                        png_infop info_ptr,\r
+                        int transforms,\r
+                        png_voidp params));\r
+extern PNG_EXPORT(void, png_write_png) PNGARG((png_structp png_ptr,\r
+                        png_infop info_ptr,\r
+                        int transforms,\r
+                        png_voidp params));\r
+#endif\r
+\r
+extern PNG_EXPORT(png_charp,png_get_copyright) PNGARG((png_structp png_ptr));\r
+extern PNG_EXPORT(png_charp,png_get_header_ver) PNGARG((png_structp png_ptr));\r
+extern PNG_EXPORT(png_charp,png_get_header_version) PNGARG((png_structp\r
+    png_ptr));\r
+extern PNG_EXPORT(png_charp,png_get_libpng_ver) PNGARG((png_structp png_ptr));\r
+\r
+#ifdef PNG_MNG_FEATURES_SUPPORTED\r
+extern PNG_EXPORT(png_uint_32,png_permit_mng_features) PNGARG((png_structp\r
+   png_ptr, png_uint_32 mng_features_permitted));\r
+#endif\r
+\r
+/* For use in png_set_keep_unknown, added to version 1.2.6 */\r
+#define PNG_HANDLE_CHUNK_AS_DEFAULT   0\r
+#define PNG_HANDLE_CHUNK_NEVER        1\r
+#define PNG_HANDLE_CHUNK_IF_SAFE      2\r
+#define PNG_HANDLE_CHUNK_ALWAYS       3\r
+\r
+/* Strip the prepended error numbers ("#nnn ") from error and warning\r
+ * messages before passing them to the error or warning handler.\r
+ */\r
+#ifdef PNG_ERROR_NUMBERS_SUPPORTED\r
+extern PNG_EXPORT(void,png_set_strip_error_numbers) PNGARG((png_structp\r
+   png_ptr, png_uint_32 strip_mode));\r
+#endif\r
+\r
+/* Added in libpng-1.2.6 */\r
+#ifdef PNG_SET_USER_LIMITS_SUPPORTED\r
+extern PNG_EXPORT(void,png_set_user_limits) PNGARG((png_structp\r
+   png_ptr, png_uint_32 user_width_max, png_uint_32 user_height_max));\r
+extern PNG_EXPORT(png_uint_32,png_get_user_width_max) PNGARG((png_structp\r
+   png_ptr));\r
+extern PNG_EXPORT(png_uint_32,png_get_user_height_max) PNGARG((png_structp\r
+   png_ptr));\r
+/* Added in libpng-1.4.0 */\r
+extern PNG_EXPORT(void,png_set_chunk_cache_max) PNGARG((png_structp\r
+   png_ptr, png_uint_32 user_chunk_cache_max));\r
+extern PNG_EXPORT(png_uint_32,png_get_chunk_cache_max)\r
+   PNGARG((png_structp png_ptr));\r
+/* Added in libpng-1.4.1 */\r
+extern PNG_EXPORT(void,png_set_chunk_malloc_max) PNGARG((png_structp\r
+   png_ptr, png_alloc_size_t user_chunk_cache_max));\r
+extern PNG_EXPORT(png_alloc_size_t,png_get_chunk_malloc_max)\r
+   PNGARG((png_structp png_ptr));\r
+#endif\r
+\r
+#if defined(PNG_INCH_CONVERSIONS) && defined(PNG_FLOATING_POINT_SUPPORTED)\r
+PNG_EXPORT(png_uint_32,png_get_pixels_per_inch) PNGARG((png_structp png_ptr,\r
+png_infop info_ptr));\r
+\r
+PNG_EXPORT(png_uint_32,png_get_x_pixels_per_inch) PNGARG((png_structp png_ptr,\r
+png_infop info_ptr));\r
+\r
+PNG_EXPORT(png_uint_32,png_get_y_pixels_per_inch) PNGARG((png_structp png_ptr,\r
+png_infop info_ptr));\r
+\r
+PNG_EXPORT(float,png_get_x_offset_inches) PNGARG((png_structp png_ptr,\r
+png_infop info_ptr));\r
+\r
+PNG_EXPORT(float,png_get_y_offset_inches) PNGARG((png_structp png_ptr,\r
+png_infop info_ptr));\r
+\r
+#ifdef PNG_pHYs_SUPPORTED\r
+PNG_EXPORT(png_uint_32,png_get_pHYs_dpi) PNGARG((png_structp png_ptr,\r
+png_infop info_ptr, png_uint_32 *res_x, png_uint_32 *res_y, int *unit_type));\r
+#endif /* PNG_pHYs_SUPPORTED */\r
+#endif  /* PNG_INCH_CONVERSIONS && PNG_FLOATING_POINT_SUPPORTED */\r
+\r
+/* Added in libpng-1.4.0 */\r
+#ifdef PNG_IO_STATE_SUPPORTED\r
+extern PNG_EXPORT(png_uint_32,png_get_io_state) PNGARG((png_structp png_ptr));\r
+\r
+extern PNG_EXPORT(png_bytep,png_get_io_chunk_name)\r
+   PNGARG((png_structp png_ptr));\r
+\r
+/* The flags returned by png_get_io_state() are the following: */\r
+#define PNG_IO_NONE        0x0000   /* no I/O at this moment */\r
+#define PNG_IO_READING     0x0001   /* currently reading */\r
+#define PNG_IO_WRITING     0x0002   /* currently writing */\r
+#define PNG_IO_SIGNATURE   0x0010   /* currently at the file signature */\r
+#define PNG_IO_CHUNK_HDR   0x0020   /* currently at the chunk header */\r
+#define PNG_IO_CHUNK_DATA  0x0040   /* currently at the chunk data */\r
+#define PNG_IO_CHUNK_CRC   0x0080   /* currently at the chunk crc */\r
+#define PNG_IO_MASK_OP     0x000f   /* current operation: reading/writing */\r
+#define PNG_IO_MASK_LOC    0x00f0   /* current location: sig/hdr/data/crc */\r
+#endif /* ?PNG_IO_STATE_SUPPORTED */\r
+\r
+/* Maintainer: Put new public prototypes here ^, in libpng.3, and project\r
+ * defs\r
+ */\r
+\r
+#ifdef PNG_READ_COMPOSITE_NODIV_SUPPORTED\r
+/* With these routines we avoid an integer divide, which will be slower on\r
+ * most machines.  However, it does take more operations than the corresponding\r
+ * divide method, so it may be slower on a few RISC systems.  There are two\r
+ * shifts (by 8 or 16 bits) and an addition, versus a single integer divide.\r
+ *\r
+ * Note that the rounding factors are NOT supposed to be the same!  128 and\r
+ * 32768 are correct for the NODIV code; 127 and 32767 are correct for the\r
+ * standard method.\r
+ *\r
+ * [Optimized code by Greg Roelofs and Mark Adler...blame us for bugs. :-) ]\r
+ */\r
+\r
+ /* fg and bg should be in `gamma 1.0' space; alpha is the opacity          */\r
+\r
+#  define png_composite(composite, fg, alpha, bg)         \\r
+     { png_uint_16 temp = (png_uint_16)((png_uint_16)(fg) \\r
+           * (png_uint_16)(alpha)                         \\r
+           + (png_uint_16)(bg)*(png_uint_16)(255          \\r
+           - (png_uint_16)(alpha)) + (png_uint_16)128);   \\r
+       (composite) = (png_byte)((temp + (temp >> 8)) >> 8); }\r
+\r
+#  define png_composite_16(composite, fg, alpha, bg)       \\r
+     { png_uint_32 temp = (png_uint_32)((png_uint_32)(fg)  \\r
+           * (png_uint_32)(alpha)                          \\r
+           + (png_uint_32)(bg)*(png_uint_32)(65535L        \\r
+           - (png_uint_32)(alpha)) + (png_uint_32)32768L); \\r
+       (composite) = (png_uint_16)((temp + (temp >> 16)) >> 16); }\r
+\r
+#else  /* Standard method using integer division */\r
+\r
+#  define png_composite(composite, fg, alpha, bg)                            \\r
+     (composite) = (png_byte)(((png_uint_16)(fg) * (png_uint_16)(alpha) +    \\r
+       (png_uint_16)(bg) * (png_uint_16)(255 - (png_uint_16)(alpha)) +       \\r
+       (png_uint_16)127) / 255)\r
+\r
+#  define png_composite_16(composite, fg, alpha, bg)                         \\r
+     (composite) = (png_uint_16)(((png_uint_32)(fg) * (png_uint_32)(alpha) + \\r
+       (png_uint_32)(bg)*(png_uint_32)(65535L - (png_uint_32)(alpha)) +      \\r
+       (png_uint_32)32767) / (png_uint_32)65535L)\r
+#endif /* PNG_READ_COMPOSITE_NODIV_SUPPORTED */\r
+\r
+#ifdef PNG_USE_READ_MACROS\r
+/* Inline macros to do direct reads of bytes from the input buffer.\r
+ * The png_get_int_32() routine assumes we are using two's complement\r
+ * format for negative values, which is almost certainly true.\r
+ */\r
+/* We could make special-case BIG_ENDIAN macros that do direct reads here */\r
+#  define png_get_uint_32(buf) \\r
+     (((png_uint_32)(*(buf)) << 24) + \\r
+      ((png_uint_32)(*((buf) + 1)) << 16) + \\r
+      ((png_uint_32)(*((buf) + 2)) << 8) + \\r
+      ((png_uint_32)(*((buf) + 3))))\r
+#  define png_get_uint_16(buf) \\r
+     (((png_uint_32)(*(buf)) << 8) + \\r
+      ((png_uint_32)(*((buf) + 1))))\r
+#ifdef PNG_GET_INT_32_SUPPORTED\r
+#  define png_get_int_32(buf) \\r
+     (((png_int_32)(*(buf)) << 24) + \\r
+      ((png_int_32)(*((buf) + 1)) << 16) + \\r
+      ((png_int_32)(*((buf) + 2)) << 8) + \\r
+      ((png_int_32)(*((buf) + 3))))\r
+#endif\r
+#else\r
+extern PNG_EXPORT(png_uint_32,png_get_uint_32) PNGARG((png_bytep buf));\r
+extern PNG_EXPORT(png_uint_16,png_get_uint_16) PNGARG((png_bytep buf));\r
+#ifdef PNG_GET_INT_32_SUPPORTED\r
+extern PNG_EXPORT(png_int_32,png_get_int_32) PNGARG((png_bytep buf));\r
+#endif\r
+#endif\r
+extern PNG_EXPORT(png_uint_32,png_get_uint_31)\r
+  PNGARG((png_structp png_ptr, png_bytep buf));\r
+/* No png_get_int_16 -- may be added if there's a real need for it. */\r
+\r
+/* Place a 32-bit number into a buffer in PNG byte order (big-endian). */\r
+extern PNG_EXPORT(void,png_save_uint_32)\r
+   PNGARG((png_bytep buf, png_uint_32 i));\r
+extern PNG_EXPORT(void,png_save_int_32)\r
+   PNGARG((png_bytep buf, png_int_32 i));\r
+\r
+/* Place a 16-bit number into a buffer in PNG byte order.\r
+ * The parameter is declared unsigned int, not png_uint_16,\r
+ * just to avoid potential problems on pre-ANSI C compilers.\r
+ */\r
+extern PNG_EXPORT(void,png_save_uint_16)\r
+   PNGARG((png_bytep buf, unsigned int i));\r
+/* No png_save_int_16 -- may be added if there's a real need for it. */\r
+\r
+/* ************************************************************************* */\r
+\r
+/* Various modes of operation.  Note that after an init, mode is set to\r
+ * zero automatically when the structure is created.\r
+ */\r
+#define PNG_HAVE_IHDR               0x01\r
+#define PNG_HAVE_PLTE               0x02\r
+#define PNG_HAVE_IDAT               0x04\r
+#define PNG_AFTER_IDAT              0x08 /* Have complete zlib datastream */\r
+#define PNG_HAVE_IEND               0x10\r
+#define PNG_HAVE_gAMA               0x20\r
+#define PNG_HAVE_cHRM               0x40\r
+\r
+#ifdef __cplusplus\r
+}\r
+#endif\r
+\r
+#endif /* PNG_VERSION_INFO_ONLY */\r
+/* Do not put anything past this line */\r
+#endif /* PNG_H */\r
index d1e2995..2ebe72d 100644 (file)
-
-/* pngconf.h - machine configurable file for libpng
- *
- * libpng version 1.2.29 - May 8, 2008
- * For conditions of distribution and use, see copyright notice in png.h
- * Copyright (c) 1998-2008 Glenn Randers-Pehrson
- * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
- * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
- */
-
-/* Any machine specific code is near the front of this file, so if you
- * are configuring libpng for a machine, you may want to read the section
- * starting here down to where it starts to typedef png_color, png_text,
- * and png_info.
- */
-
-#ifndef PNGCONF_H
-#define PNGCONF_H
-
-#define PNG_1_2_X
-
-/* 
- * PNG_USER_CONFIG has to be defined on the compiler command line. This
- * includes the resource compiler for Windows DLL configurations.
- */
-#ifdef PNG_USER_CONFIG
-#  ifndef PNG_USER_PRIVATEBUILD
-#    define PNG_USER_PRIVATEBUILD
-#  endif
-#include "pngusr.h"
-#endif
-
-/* PNG_CONFIGURE_LIBPNG is set by the "configure" script. */
-#ifdef PNG_CONFIGURE_LIBPNG
-#ifdef HAVE_CONFIG_H
-#include "config.h"
-#endif
-#endif
-
-/*
- * Added at libpng-1.2.8
- *  
- * If you create a private DLL you need to define in "pngusr.h" the followings:
- * #define PNG_USER_PRIVATEBUILD <Describes by whom and why this version of
- *        the DLL was built>
- *  e.g. #define PNG_USER_PRIVATEBUILD "Build by MyCompany for xyz reasons."
- * #define PNG_USER_DLLFNAME_POSTFIX <two-letter postfix that serve to
- *        distinguish your DLL from those of the official release. These
- *        correspond to the trailing letters that come after the version
- *        number and must match your private DLL name>
- *  e.g. // private DLL "libpng13gx.dll"
- *       #define PNG_USER_DLLFNAME_POSTFIX "gx"
- * 
- * The following macros are also at your disposal if you want to complete the 
- * DLL VERSIONINFO structure.
- * - PNG_USER_VERSIONINFO_COMMENTS
- * - PNG_USER_VERSIONINFO_COMPANYNAME
- * - PNG_USER_VERSIONINFO_LEGALTRADEMARKS
- */
-
-#ifdef __STDC__
-#ifdef SPECIALBUILD
-#  pragma message("PNG_LIBPNG_SPECIALBUILD (and deprecated SPECIALBUILD)\
- are now LIBPNG reserved macros. Use PNG_USER_PRIVATEBUILD instead.")
-#endif
-
-#ifdef PRIVATEBUILD
-# pragma message("PRIVATEBUILD is deprecated.\
- Use PNG_USER_PRIVATEBUILD instead.")
-# define PNG_USER_PRIVATEBUILD PRIVATEBUILD
-#endif
-#endif /* __STDC__ */
-
-#ifndef PNG_VERSION_INFO_ONLY
-
-/* End of material added to libpng-1.2.8 */
-
-/* Added at libpng-1.2.19, removed at libpng-1.2.20 because it caused trouble
-   Restored at libpng-1.2.21 */
-#if !defined(PNG_NO_WARN_UNINITIALIZED_ROW) && \
-    !defined(PNG_WARN_UNINITIALIZED_ROW)
-#  define PNG_WARN_UNINITIALIZED_ROW 1
-#endif
-/* End of material added at libpng-1.2.19/1.2.21 */
-
-/* This is the size of the compression buffer, and thus the size of
- * an IDAT chunk.  Make this whatever size you feel is best for your
- * machine.  One of these will be allocated per png_struct.  When this
- * is full, it writes the data to the disk, and does some other
- * calculations.  Making this an extremely small size will slow
- * the library down, but you may want to experiment to determine
- * where it becomes significant, if you are concerned with memory
- * usage.  Note that zlib allocates at least 32Kb also.  For readers,
- * this describes the size of the buffer available to read the data in.
- * Unless this gets smaller than the size of a row (compressed),
- * it should not make much difference how big this is.
- */
-
-#ifndef PNG_ZBUF_SIZE
-#  define PNG_ZBUF_SIZE 8192
-#endif
-
-/* Enable if you want a write-only libpng */
-
-#ifndef PNG_NO_READ_SUPPORTED
-#  define PNG_READ_SUPPORTED
-#endif
-
-/* Enable if you want a read-only libpng */
-
-#ifndef PNG_NO_WRITE_SUPPORTED
-#  define PNG_WRITE_SUPPORTED
-#endif
-
-/* Enabled by default in 1.2.0.  You can disable this if you don't need to
-   support PNGs that are embedded in MNG datastreams */
-#if !defined(PNG_1_0_X) && !defined(PNG_NO_MNG_FEATURES)
-#  ifndef PNG_MNG_FEATURES_SUPPORTED
-#    define PNG_MNG_FEATURES_SUPPORTED
-#  endif
-#endif
-
-#ifndef PNG_NO_FLOATING_POINT_SUPPORTED
-#  ifndef PNG_FLOATING_POINT_SUPPORTED
-#    define PNG_FLOATING_POINT_SUPPORTED
-#  endif
-#endif
-
-/* If you are running on a machine where you cannot allocate more
- * than 64K of memory at once, uncomment this.  While libpng will not
- * normally need that much memory in a chunk (unless you load up a very
- * large file), zlib needs to know how big of a chunk it can use, and
- * libpng thus makes sure to check any memory allocation to verify it
- * will fit into memory.
-#define PNG_MAX_MALLOC_64K
- */
-#if defined(MAXSEG_64K) && !defined(PNG_MAX_MALLOC_64K)
-#  define PNG_MAX_MALLOC_64K
-#endif
-
-/* Special munging to support doing things the 'cygwin' way:
- * 'Normal' png-on-win32 defines/defaults:
- *   PNG_BUILD_DLL -- building dll
- *   PNG_USE_DLL   -- building an application, linking to dll
- *   (no define)   -- building static library, or building an
- *                    application and linking to the static lib
- * 'Cygwin' defines/defaults:
- *   PNG_BUILD_DLL -- (ignored) building the dll
- *   (no define)   -- (ignored) building an application, linking to the dll
- *   PNG_STATIC    -- (ignored) building the static lib, or building an 
- *                    application that links to the static lib.
- *   ALL_STATIC    -- (ignored) building various static libs, or building an 
- *                    application that links to the static libs.
- * Thus,
- * a cygwin user should define either PNG_BUILD_DLL or PNG_STATIC, and
- * this bit of #ifdefs will define the 'correct' config variables based on
- * that. If a cygwin user *wants* to define 'PNG_USE_DLL' that's okay, but
- * unnecessary.
- *
- * Also, the precedence order is:
- *   ALL_STATIC (since we can't #undef something outside our namespace)
- *   PNG_BUILD_DLL
- *   PNG_STATIC
- *   (nothing) == PNG_USE_DLL
- * 
- * CYGWIN (2002-01-20): The preceding is now obsolete. With the advent
- *   of auto-import in binutils, we no longer need to worry about 
- *   __declspec(dllexport) / __declspec(dllimport) and friends.  Therefore,
- *   we don't need to worry about PNG_STATIC or ALL_STATIC when it comes
- *   to __declspec() stuff.  However, we DO need to worry about 
- *   PNG_BUILD_DLL and PNG_STATIC because those change some defaults
- *   such as CONSOLE_IO and whether GLOBAL_ARRAYS are allowed.
- */
-#if defined(__CYGWIN__)
-#  if defined(ALL_STATIC)
-#    if defined(PNG_BUILD_DLL)
-#      undef PNG_BUILD_DLL
-#    endif
-#    if defined(PNG_USE_DLL)
-#      undef PNG_USE_DLL
-#    endif
-#    if defined(PNG_DLL)
-#      undef PNG_DLL
-#    endif
-#    if !defined(PNG_STATIC)
-#      define PNG_STATIC
-#    endif
-#  else
-#    if defined (PNG_BUILD_DLL)
-#      if defined(PNG_STATIC)
-#        undef PNG_STATIC
-#      endif
-#      if defined(PNG_USE_DLL)
-#        undef PNG_USE_DLL
-#      endif
-#      if !defined(PNG_DLL)
-#        define PNG_DLL
-#      endif
-#    else
-#      if defined(PNG_STATIC)
-#        if defined(PNG_USE_DLL)
-#          undef PNG_USE_DLL
-#        endif
-#        if defined(PNG_DLL)
-#          undef PNG_DLL
-#        endif
-#      else
-#        if !defined(PNG_USE_DLL)
-#          define PNG_USE_DLL
-#        endif
-#        if !defined(PNG_DLL)
-#          define PNG_DLL
-#        endif
-#      endif  
-#    endif  
-#  endif
-#endif
-
-/* This protects us against compilers that run on a windowing system
- * and thus don't have or would rather us not use the stdio types:
- * stdin, stdout, and stderr.  The only one currently used is stderr
- * in png_error() and png_warning().  #defining PNG_NO_CONSOLE_IO will
- * prevent these from being compiled and used. #defining PNG_NO_STDIO
- * will also prevent these, plus will prevent the entire set of stdio
- * macros and functions (FILE *, printf, etc.) from being compiled and used,
- * unless (PNG_DEBUG > 0) has been #defined.
- *
- * #define PNG_NO_CONSOLE_IO
- * #define PNG_NO_STDIO
- */
-
-#if defined(_WIN32_WCE)
-#  include <windows.h>
-   /* Console I/O functions are not supported on WindowsCE */
-#  define PNG_NO_CONSOLE_IO
-#  ifdef PNG_DEBUG
-#    undef PNG_DEBUG
-#  endif
-#endif
-
-#ifdef PNG_BUILD_DLL
-#  ifndef PNG_CONSOLE_IO_SUPPORTED
-#    ifndef PNG_NO_CONSOLE_IO
-#      define PNG_NO_CONSOLE_IO
-#    endif
-#  endif
-#endif
-
-#  ifdef PNG_NO_STDIO
-#    ifndef PNG_NO_CONSOLE_IO
-#      define PNG_NO_CONSOLE_IO
-#    endif
-#    ifdef PNG_DEBUG
-#      if (PNG_DEBUG > 0)
-#        include <stdio.h>
-#      endif
-#    endif
-#  else
-#    if !defined(_WIN32_WCE)
-/* "stdio.h" functions are not supported on WindowsCE */
-#      include <stdio.h>
-#    endif
-#  endif
-
-/* This macro protects us against machines that don't have function
- * prototypes (ie K&R style headers).  If your compiler does not handle
- * function prototypes, define this macro and use the included ansi2knr.
- * I've always been able to use _NO_PROTO as the indicator, but you may
- * need to drag the empty declaration out in front of here, or change the
- * ifdef to suit your own needs.
- */
-#ifndef PNGARG
-
-#ifdef OF /* zlib prototype munger */
-#  define PNGARG(arglist) OF(arglist)
-#else
-
-#ifdef _NO_PROTO
-#  define PNGARG(arglist) ()
-#  ifndef PNG_TYPECAST_NULL
-#     define PNG_TYPECAST_NULL
-#  endif
-#else
-#  define PNGARG(arglist) arglist
-#endif /* _NO_PROTO */
-
-
-#endif /* OF */
-
-#endif /* PNGARG */
-
-/* Try to determine if we are compiling on a Mac.  Note that testing for
- * just __MWERKS__ is not good enough, because the Codewarrior is now used
- * on non-Mac platforms.
- */
-#ifndef MACOS
-#  if (defined(__MWERKS__) && defined(macintosh)) || defined(applec) || \
-      defined(THINK_C) || defined(__SC__) || defined(TARGET_OS_MAC)
-#    define MACOS
-#  endif
-#endif
-
-/* enough people need this for various reasons to include it here */
-#if !defined(MACOS) && !defined(RISCOS) && !defined(_WIN32_WCE)
-#  include <sys/types.h>
-#endif
-
-#if !defined(PNG_SETJMP_NOT_SUPPORTED) && !defined(PNG_NO_SETJMP_SUPPORTED)
-#  define PNG_SETJMP_SUPPORTED
-#endif
-
-#ifdef PNG_SETJMP_SUPPORTED
-/* This is an attempt to force a single setjmp behaviour on Linux.  If
- * the X config stuff didn't define _BSD_SOURCE we wouldn't need this.
- */
-
-#  ifdef __linux__
-#    ifdef _BSD_SOURCE
-#      define PNG_SAVE_BSD_SOURCE
-#      undef _BSD_SOURCE
-#    endif
-#    ifdef _SETJMP_H
-     /* If you encounter a compiler error here, see the explanation
-      * near the end of INSTALL.
-      */
-         __pngconf.h__ already includes setjmp.h;
-         __dont__ include it again.;
-#    endif
-#  endif /* __linux__ */
-
-   /* include setjmp.h for error handling */
-#  include <setjmp.h>
-
-#  ifdef __linux__
-#    ifdef PNG_SAVE_BSD_SOURCE
-#      ifndef _BSD_SOURCE
-#        define _BSD_SOURCE
-#      endif
-#      undef PNG_SAVE_BSD_SOURCE
-#    endif
-#  endif /* __linux__ */
-#endif /* PNG_SETJMP_SUPPORTED */
-
-#ifdef BSD
-#  include <strings.h>
-#else
-#  include <string.h>
-#endif
-
-/* Other defines for things like memory and the like can go here.  */
-#ifdef PNG_INTERNAL
-
-#include <stdlib.h>
-
-/* The functions exported by PNG_EXTERN are PNG_INTERNAL functions, which
- * aren't usually used outside the library (as far as I know), so it is
- * debatable if they should be exported at all.  In the future, when it is
- * possible to have run-time registry of chunk-handling functions, some of
- * these will be made available again.
-#define PNG_EXTERN extern
- */
-#define PNG_EXTERN
-
-/* Other defines specific to compilers can go here.  Try to keep
- * them inside an appropriate ifdef/endif pair for portability.
- */
-
-#if defined(PNG_FLOATING_POINT_SUPPORTED)
-#  if defined(MACOS)
-     /* We need to check that <math.h> hasn't already been included earlier
-      * as it seems it doesn't agree with <fp.h>, yet we should really use
-      * <fp.h> if possible.
-      */
-#    if !defined(__MATH_H__) && !defined(__MATH_H) && !defined(__cmath__)
-#      include <fp.h>
-#    endif
-#  else
-#    include <math.h>
-#  endif
-#  if defined(_AMIGA) && defined(__SASC) && defined(_M68881)
-     /* Amiga SAS/C: We must include builtin FPU functions when compiling using
-      * MATH=68881
-      */
-#    include <m68881.h>
-#  endif
-#endif
-
-/* Codewarrior on NT has linking problems without this. */
-#if (defined(__MWERKS__) && defined(WIN32)) || defined(__STDC__)
-#  define PNG_ALWAYS_EXTERN
-#endif
-
-/* This provides the non-ANSI (far) memory allocation routines. */
-#if defined(__TURBOC__) && defined(__MSDOS__)
-#  include <mem.h>
-#  include <alloc.h>
-#endif
-
-/* I have no idea why is this necessary... */
-#if defined(_MSC_VER) && (defined(WIN32) || defined(_Windows) || \
-    defined(_WINDOWS) || defined(_WIN32) || defined(__WIN32__))
-#  include <malloc.h>
-#endif
-
-/* This controls how fine the dithering gets.  As this allocates
- * a largish chunk of memory (32K), those who are not as concerned
- * with dithering quality can decrease some or all of these.
- */
-#ifndef PNG_DITHER_RED_BITS
-#  define PNG_DITHER_RED_BITS 5
-#endif
-#ifndef PNG_DITHER_GREEN_BITS
-#  define PNG_DITHER_GREEN_BITS 5
-#endif
-#ifndef PNG_DITHER_BLUE_BITS
-#  define PNG_DITHER_BLUE_BITS 5
-#endif
-
-/* This controls how fine the gamma correction becomes when you
- * are only interested in 8 bits anyway.  Increasing this value
- * results in more memory being used, and more pow() functions
- * being called to fill in the gamma tables.  Don't set this value
- * less then 8, and even that may not work (I haven't tested it).
- */
-
-#ifndef PNG_MAX_GAMMA_8
-#  define PNG_MAX_GAMMA_8 11
-#endif
-
-/* This controls how much a difference in gamma we can tolerate before
- * we actually start doing gamma conversion.
- */
-#ifndef PNG_GAMMA_THRESHOLD
-#  define PNG_GAMMA_THRESHOLD 0.05
-#endif
-
-#endif /* PNG_INTERNAL */
-
-/* The following uses const char * instead of char * for error
- * and warning message functions, so some compilers won't complain.
- * If you do not want to use const, define PNG_NO_CONST here.
- */
-
-#ifndef PNG_NO_CONST
-#  define PNG_CONST const
-#else
-#  define PNG_CONST
-#endif
-
-/* The following defines give you the ability to remove code from the
- * library that you will not be using.  I wish I could figure out how to
- * automate this, but I can't do that without making it seriously hard
- * on the users.  So if you are not using an ability, change the #define
- * to and #undef, and that part of the library will not be compiled.  If
- * your linker can't find a function, you may want to make sure the
- * ability is defined here.  Some of these depend upon some others being
- * defined.  I haven't figured out all the interactions here, so you may
- * have to experiment awhile to get everything to compile.  If you are
- * creating or using a shared library, you probably shouldn't touch this,
- * as it will affect the size of the structures, and this will cause bad
- * things to happen if the library and/or application ever change.
- */
-
-/* Any features you will not be using can be undef'ed here */
-
-/* GR-P, 0.96a: Set "*TRANSFORMS_SUPPORTED as default but allow user
- * to turn it off with "*TRANSFORMS_NOT_SUPPORTED" or *PNG_NO_*_TRANSFORMS
- * on the compile line, then pick and choose which ones to define without
- * having to edit this file. It is safe to use the *TRANSFORMS_NOT_SUPPORTED
- * if you only want to have a png-compliant reader/writer but don't need
- * any of the extra transformations.  This saves about 80 kbytes in a
- * typical installation of the library. (PNG_NO_* form added in version
- * 1.0.1c, for consistency)
- */
-
-/* The size of the png_text structure changed in libpng-1.0.6 when
- * iTXt support was added.  iTXt support was turned off by default through
- * libpng-1.2.x, to support old apps that malloc the png_text structure
- * instead of calling png_set_text() and letting libpng malloc it.  It
- * was turned on by default in libpng-1.3.0.
- */
-
-#if defined(PNG_1_0_X) || defined (PNG_1_2_X)
-#  ifndef PNG_NO_iTXt_SUPPORTED
-#    define PNG_NO_iTXt_SUPPORTED
-#  endif
-#  ifndef PNG_NO_READ_iTXt
-#    define PNG_NO_READ_iTXt
-#  endif
-#  ifndef PNG_NO_WRITE_iTXt
-#    define PNG_NO_WRITE_iTXt
-#  endif
-#endif
-
-#if !defined(PNG_NO_iTXt_SUPPORTED)
-#  if !defined(PNG_READ_iTXt_SUPPORTED) && !defined(PNG_NO_READ_iTXt)
-#    define PNG_READ_iTXt
-#  endif
-#  if !defined(PNG_WRITE_iTXt_SUPPORTED) && !defined(PNG_NO_WRITE_iTXt)
-#    define PNG_WRITE_iTXt
-#  endif
-#endif
-
-/* The following support, added after version 1.0.0, can be turned off here en
- * masse by defining PNG_LEGACY_SUPPORTED in case you need binary compatibility
- * with old applications that require the length of png_struct and png_info
- * to remain unchanged.
- */
-
-#ifdef PNG_LEGACY_SUPPORTED
-#  define PNG_NO_FREE_ME
-#  define PNG_NO_READ_UNKNOWN_CHUNKS
-#  define PNG_NO_WRITE_UNKNOWN_CHUNKS
-#  define PNG_NO_READ_USER_CHUNKS
-#  define PNG_NO_READ_iCCP
-#  define PNG_NO_WRITE_iCCP
-#  define PNG_NO_READ_iTXt
-#  define PNG_NO_WRITE_iTXt
-#  define PNG_NO_READ_sCAL
-#  define PNG_NO_WRITE_sCAL
-#  define PNG_NO_READ_sPLT
-#  define PNG_NO_WRITE_sPLT
-#  define PNG_NO_INFO_IMAGE
-#  define PNG_NO_READ_RGB_TO_GRAY
-#  define PNG_NO_READ_USER_TRANSFORM
-#  define PNG_NO_WRITE_USER_TRANSFORM
-#  define PNG_NO_USER_MEM
-#  define PNG_NO_READ_EMPTY_PLTE
-#  define PNG_NO_MNG_FEATURES
-#  define PNG_NO_FIXED_POINT_SUPPORTED
-#endif
-
-/* Ignore attempt to turn off both floating and fixed point support */
-#if !defined(PNG_FLOATING_POINT_SUPPORTED) || \
-    !defined(PNG_NO_FIXED_POINT_SUPPORTED)
-#  define PNG_FIXED_POINT_SUPPORTED
-#endif
-
-#ifndef PNG_NO_FREE_ME
-#  define PNG_FREE_ME_SUPPORTED
-#endif
-
-#if defined(PNG_READ_SUPPORTED)
-
-#if !defined(PNG_READ_TRANSFORMS_NOT_SUPPORTED) && \
-      !defined(PNG_NO_READ_TRANSFORMS)
-#  define PNG_READ_TRANSFORMS_SUPPORTED
-#endif
-
-#ifdef PNG_READ_TRANSFORMS_SUPPORTED
-#  ifndef PNG_NO_READ_EXPAND
-#    define PNG_READ_EXPAND_SUPPORTED
-#  endif
-#  ifndef PNG_NO_READ_SHIFT
-#    define PNG_READ_SHIFT_SUPPORTED
-#  endif
-#  ifndef PNG_NO_READ_PACK
-#    define PNG_READ_PACK_SUPPORTED
-#  endif
-#  ifndef PNG_NO_READ_BGR
-#    define PNG_READ_BGR_SUPPORTED
-#  endif
-#  ifndef PNG_NO_READ_SWAP
-#    define PNG_READ_SWAP_SUPPORTED
-#  endif
-#  ifndef PNG_NO_READ_PACKSWAP
-#    define PNG_READ_PACKSWAP_SUPPORTED
-#  endif
-#  ifndef PNG_NO_READ_INVERT
-#    define PNG_READ_INVERT_SUPPORTED
-#  endif
-#  ifndef PNG_NO_READ_DITHER
-#    define PNG_READ_DITHER_SUPPORTED
-#  endif
-#  ifndef PNG_NO_READ_BACKGROUND
-#    define PNG_READ_BACKGROUND_SUPPORTED
-#  endif
-#  ifndef PNG_NO_READ_16_TO_8
-#    define PNG_READ_16_TO_8_SUPPORTED
-#  endif
-#  ifndef PNG_NO_READ_FILLER
-#    define PNG_READ_FILLER_SUPPORTED
-#  endif
-#  ifndef PNG_NO_READ_GAMMA
-#    define PNG_READ_GAMMA_SUPPORTED
-#  endif
-#  ifndef PNG_NO_READ_GRAY_TO_RGB
-#    define PNG_READ_GRAY_TO_RGB_SUPPORTED
-#  endif
-#  ifndef PNG_NO_READ_SWAP_ALPHA
-#    define PNG_READ_SWAP_ALPHA_SUPPORTED
-#  endif
-#  ifndef PNG_NO_READ_INVERT_ALPHA
-#    define PNG_READ_INVERT_ALPHA_SUPPORTED
-#  endif
-#  ifndef PNG_NO_READ_STRIP_ALPHA
-#    define PNG_READ_STRIP_ALPHA_SUPPORTED
-#  endif
-#  ifndef PNG_NO_READ_USER_TRANSFORM
-#    define PNG_READ_USER_TRANSFORM_SUPPORTED
-#  endif
-#  ifndef PNG_NO_READ_RGB_TO_GRAY
-#    define PNG_READ_RGB_TO_GRAY_SUPPORTED
-#  endif
-#endif /* PNG_READ_TRANSFORMS_SUPPORTED */
-
-#if !defined(PNG_NO_PROGRESSIVE_READ) && \
- !defined(PNG_PROGRESSIVE_READ_SUPPORTED) /* if you don't do progressive   */
-#  define PNG_PROGRESSIVE_READ_SUPPORTED  /* reading.  This is not talking */
-#endif                            /* about interlacing capability!  You'll */
-           /* still have interlacing unless you change the following line: */
-
-#define PNG_READ_INTERLACING_SUPPORTED /* required in PNG-compliant decoders */
-
-#ifndef PNG_NO_READ_COMPOSITE_NODIV
-#  ifndef PNG_NO_READ_COMPOSITED_NODIV  /* libpng-1.0.x misspelling */
-#    define PNG_READ_COMPOSITE_NODIV_SUPPORTED  /* well tested on Intel, SGI */
-#  endif
-#endif
-
-#if defined(PNG_1_0_X) || defined (PNG_1_2_X)
-/* Deprecated, will be removed from version 2.0.0.
-   Use PNG_MNG_FEATURES_SUPPORTED instead. */
-#ifndef PNG_NO_READ_EMPTY_PLTE
-#  define PNG_READ_EMPTY_PLTE_SUPPORTED
-#endif
-#endif
-
-#endif /* PNG_READ_SUPPORTED */
-
-#if defined(PNG_WRITE_SUPPORTED)
-
-# if !defined(PNG_WRITE_TRANSFORMS_NOT_SUPPORTED) && \
-    !defined(PNG_NO_WRITE_TRANSFORMS)
-#  define PNG_WRITE_TRANSFORMS_SUPPORTED
-#endif
-
-#ifdef PNG_WRITE_TRANSFORMS_SUPPORTED
-#  ifndef PNG_NO_WRITE_SHIFT
-#    define PNG_WRITE_SHIFT_SUPPORTED
-#  endif
-#  ifndef PNG_NO_WRITE_PACK
-#    define PNG_WRITE_PACK_SUPPORTED
-#  endif
-#  ifndef PNG_NO_WRITE_BGR
-#    define PNG_WRITE_BGR_SUPPORTED
-#  endif
-#  ifndef PNG_NO_WRITE_SWAP
-#    define PNG_WRITE_SWAP_SUPPORTED
-#  endif
-#  ifndef PNG_NO_WRITE_PACKSWAP
-#    define PNG_WRITE_PACKSWAP_SUPPORTED
-#  endif
-#  ifndef PNG_NO_WRITE_INVERT
-#    define PNG_WRITE_INVERT_SUPPORTED
-#  endif
-#  ifndef PNG_NO_WRITE_FILLER
-#    define PNG_WRITE_FILLER_SUPPORTED   /* same as WRITE_STRIP_ALPHA */
-#  endif
-#  ifndef PNG_NO_WRITE_SWAP_ALPHA
-#    define PNG_WRITE_SWAP_ALPHA_SUPPORTED
-#  endif
-#  ifndef PNG_NO_WRITE_INVERT_ALPHA
-#    define PNG_WRITE_INVERT_ALPHA_SUPPORTED
-#  endif
-#  ifndef PNG_NO_WRITE_USER_TRANSFORM
-#    define PNG_WRITE_USER_TRANSFORM_SUPPORTED
-#  endif
-#endif /* PNG_WRITE_TRANSFORMS_SUPPORTED */
-
-#if !defined(PNG_NO_WRITE_INTERLACING_SUPPORTED) && \
-    !defined(PNG_WRITE_INTERLACING_SUPPORTED)
-#define PNG_WRITE_INTERLACING_SUPPORTED  /* not required for PNG-compliant
-                                            encoders, but can cause trouble
-                                            if left undefined */
-#endif
-
-#if !defined(PNG_NO_WRITE_WEIGHTED_FILTER) && \
-    !defined(PNG_WRITE_WEIGHTED_FILTER) && \
-     defined(PNG_FLOATING_POINT_SUPPORTED)
-#  define PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
-#endif
-
-#ifndef PNG_NO_WRITE_FLUSH
-#  define PNG_WRITE_FLUSH_SUPPORTED
-#endif
-
-#if defined(PNG_1_0_X) || defined (PNG_1_2_X)
-/* Deprecated, see PNG_MNG_FEATURES_SUPPORTED, above */
-#ifndef PNG_NO_WRITE_EMPTY_PLTE
-#  define PNG_WRITE_EMPTY_PLTE_SUPPORTED
-#endif
-#endif
-
-#endif /* PNG_WRITE_SUPPORTED */
-
-#ifndef PNG_1_0_X
-#  ifndef PNG_NO_ERROR_NUMBERS
-#    define PNG_ERROR_NUMBERS_SUPPORTED
-#  endif
-#endif /* PNG_1_0_X */
-
-#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \
-    defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
-#  ifndef PNG_NO_USER_TRANSFORM_PTR
-#    define PNG_USER_TRANSFORM_PTR_SUPPORTED
-#  endif
-#endif
-
-#ifndef PNG_NO_STDIO
-#  define PNG_TIME_RFC1123_SUPPORTED
-#endif
-
-/* This adds extra functions in pngget.c for accessing data from the
- * info pointer (added in version 0.99)
- * png_get_image_width()
- * png_get_image_height()
- * png_get_bit_depth()
- * png_get_color_type()
- * png_get_compression_type()
- * png_get_filter_type()
- * png_get_interlace_type()
- * png_get_pixel_aspect_ratio()
- * png_get_pixels_per_meter()
- * png_get_x_offset_pixels()
- * png_get_y_offset_pixels()
- * png_get_x_offset_microns()
- * png_get_y_offset_microns()
- */
-#if !defined(PNG_NO_EASY_ACCESS) && !defined(PNG_EASY_ACCESS_SUPPORTED)
-#  define PNG_EASY_ACCESS_SUPPORTED
-#endif
-
-/* PNG_ASSEMBLER_CODE was enabled by default in version 1.2.0 
- * and removed from version 1.2.20.  The following will be removed
- * from libpng-1.4.0
-*/
-
-#if defined(PNG_READ_SUPPORTED) && !defined(PNG_NO_OPTIMIZED_CODE)
-#  ifndef PNG_OPTIMIZED_CODE_SUPPORTED
-#    define PNG_OPTIMIZED_CODE_SUPPORTED
-#  endif
-#endif
-
-#if defined(PNG_READ_SUPPORTED) && !defined(PNG_NO_ASSEMBLER_CODE)
-#  ifndef PNG_ASSEMBLER_CODE_SUPPORTED
-#    define PNG_ASSEMBLER_CODE_SUPPORTED
-#  endif
-
-#  if defined(__GNUC__) && defined(__x86_64__) && (__GNUC__ < 4)
-     /* work around 64-bit gcc compiler bugs in gcc-3.x */
-#    if !defined(PNG_MMX_CODE_SUPPORTED) && !defined(PNG_NO_MMX_CODE)
-#      define PNG_NO_MMX_CODE
-#    endif
-#  endif
-
-#  if defined(__APPLE__)
-#    if !defined(PNG_MMX_CODE_SUPPORTED) && !defined(PNG_NO_MMX_CODE)
-#      define PNG_NO_MMX_CODE
-#    endif
-#  endif
-
-#  if (defined(__MWERKS__) && ((__MWERKS__ < 0x0900) || macintosh))
-#    if !defined(PNG_MMX_CODE_SUPPORTED) && !defined(PNG_NO_MMX_CODE)
-#      define PNG_NO_MMX_CODE
-#    endif
-#  endif
-
-#  if !defined(PNG_MMX_CODE_SUPPORTED) && !defined(PNG_NO_MMX_CODE)
-#    define PNG_MMX_CODE_SUPPORTED
-#  endif
-
-#endif
-/* end of obsolete code to be removed from libpng-1.4.0 */
-
-#if !defined(PNG_1_0_X)
-#if !defined(PNG_NO_USER_MEM) && !defined(PNG_USER_MEM_SUPPORTED)
-#  define PNG_USER_MEM_SUPPORTED
-#endif
-#endif /* PNG_1_0_X */
-
-/* Added at libpng-1.2.6 */
-#if !defined(PNG_1_0_X)
-#ifndef PNG_SET_USER_LIMITS_SUPPORTED
-#if !defined(PNG_NO_SET_USER_LIMITS) && !defined(PNG_SET_USER_LIMITS_SUPPORTED)
-#  define PNG_SET_USER_LIMITS_SUPPORTED
-#endif
-#endif
-#endif /* PNG_1_0_X */
-
-/* Added at libpng-1.0.16 and 1.2.6.  To accept all valid PNGS no matter
- * how large, set these limits to 0x7fffffffL
- */
-#ifndef PNG_USER_WIDTH_MAX
-#  define PNG_USER_WIDTH_MAX 1000000L
-#endif
-#ifndef PNG_USER_HEIGHT_MAX
-#  define PNG_USER_HEIGHT_MAX 1000000L
-#endif
-
-/* These are currently experimental features, define them if you want */
-
-/* very little testing */
-/*
-#ifdef PNG_READ_SUPPORTED
-#  ifndef PNG_READ_16_TO_8_ACCURATE_SCALE_SUPPORTED
-#    define PNG_READ_16_TO_8_ACCURATE_SCALE_SUPPORTED
-#  endif
-#endif
-*/
-
-/* This is only for PowerPC big-endian and 680x0 systems */
-/* some testing */
-/*
-#ifndef PNG_READ_BIG_ENDIAN_SUPPORTED
-#  define PNG_READ_BIG_ENDIAN_SUPPORTED
-#endif
-*/
-
-/* Buggy compilers (e.g., gcc 2.7.2.2) need this */
-/*
-#define PNG_NO_POINTER_INDEXING
-*/
-
-/* These functions are turned off by default, as they will be phased out. */
-/*
-#define  PNG_USELESS_TESTS_SUPPORTED
-#define  PNG_CORRECT_PALETTE_SUPPORTED
-*/
-
-/* Any chunks you are not interested in, you can undef here.  The
- * ones that allocate memory may be expecially important (hIST,
- * tEXt, zTXt, tRNS, pCAL).  Others will just save time and make png_info
- * a bit smaller.
- */
-
-#if defined(PNG_READ_SUPPORTED) && \
-    !defined(PNG_READ_ANCILLARY_CHUNKS_NOT_SUPPORTED) && \
-    !defined(PNG_NO_READ_ANCILLARY_CHUNKS)
-#  define PNG_READ_ANCILLARY_CHUNKS_SUPPORTED
-#endif
-
-#if defined(PNG_WRITE_SUPPORTED) && \
-    !defined(PNG_WRITE_ANCILLARY_CHUNKS_NOT_SUPPORTED) && \
-    !defined(PNG_NO_WRITE_ANCILLARY_CHUNKS)
-#  define PNG_WRITE_ANCILLARY_CHUNKS_SUPPORTED
-#endif
-
-#ifdef PNG_READ_ANCILLARY_CHUNKS_SUPPORTED
-
-#ifdef PNG_NO_READ_TEXT
-#  define PNG_NO_READ_iTXt
-#  define PNG_NO_READ_tEXt
-#  define PNG_NO_READ_zTXt
-#endif
-#ifndef PNG_NO_READ_bKGD
-#  define PNG_READ_bKGD_SUPPORTED
-#  define PNG_bKGD_SUPPORTED
-#endif
-#ifndef PNG_NO_READ_cHRM
-#  define PNG_READ_cHRM_SUPPORTED
-#  define PNG_cHRM_SUPPORTED
-#endif
-#ifndef PNG_NO_READ_gAMA
-#  define PNG_READ_gAMA_SUPPORTED
-#  define PNG_gAMA_SUPPORTED
-#endif
-#ifndef PNG_NO_READ_hIST
-#  define PNG_READ_hIST_SUPPORTED
-#  define PNG_hIST_SUPPORTED
-#endif
-#ifndef PNG_NO_READ_iCCP
-#  define PNG_READ_iCCP_SUPPORTED
-#  define PNG_iCCP_SUPPORTED
-#endif
-#ifndef PNG_NO_READ_iTXt
-#  ifndef PNG_READ_iTXt_SUPPORTED
-#    define PNG_READ_iTXt_SUPPORTED
-#  endif
-#  ifndef PNG_iTXt_SUPPORTED
-#    define PNG_iTXt_SUPPORTED
-#  endif
-#endif
-#ifndef PNG_NO_READ_oFFs
-#  define PNG_READ_oFFs_SUPPORTED
-#  define PNG_oFFs_SUPPORTED
-#endif
-#ifndef PNG_NO_READ_pCAL
-#  define PNG_READ_pCAL_SUPPORTED
-#  define PNG_pCAL_SUPPORTED
-#endif
-#ifndef PNG_NO_READ_sCAL
-#  define PNG_READ_sCAL_SUPPORTED
-#  define PNG_sCAL_SUPPORTED
-#endif
-#ifndef PNG_NO_READ_pHYs
-#  define PNG_READ_pHYs_SUPPORTED
-#  define PNG_pHYs_SUPPORTED
-#endif
-#ifndef PNG_NO_READ_sBIT
-#  define PNG_READ_sBIT_SUPPORTED
-#  define PNG_sBIT_SUPPORTED
-#endif
-#ifndef PNG_NO_READ_sPLT
-#  define PNG_READ_sPLT_SUPPORTED
-#  define PNG_sPLT_SUPPORTED
-#endif
-#ifndef PNG_NO_READ_sRGB
-#  define PNG_READ_sRGB_SUPPORTED
-#  define PNG_sRGB_SUPPORTED
-#endif
-#ifndef PNG_NO_READ_tEXt
-#  define PNG_READ_tEXt_SUPPORTED
-#  define PNG_tEXt_SUPPORTED
-#endif
-#ifndef PNG_NO_READ_tIME
-#  define PNG_READ_tIME_SUPPORTED
-#  define PNG_tIME_SUPPORTED
-#endif
-#ifndef PNG_NO_READ_tRNS
-#  define PNG_READ_tRNS_SUPPORTED
-#  define PNG_tRNS_SUPPORTED
-#endif
-#ifndef PNG_NO_READ_zTXt
-#  define PNG_READ_zTXt_SUPPORTED
-#  define PNG_zTXt_SUPPORTED
-#endif
-#ifndef PNG_NO_READ_UNKNOWN_CHUNKS
-#  define PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
-#  ifndef PNG_UNKNOWN_CHUNKS_SUPPORTED
-#    define PNG_UNKNOWN_CHUNKS_SUPPORTED
-#  endif
-#  ifndef PNG_NO_HANDLE_AS_UNKNOWN
-#    define PNG_HANDLE_AS_UNKNOWN_SUPPORTED
-#  endif
-#endif
-#if !defined(PNG_NO_READ_USER_CHUNKS) && \
-     defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
-#  define PNG_READ_USER_CHUNKS_SUPPORTED
-#  define PNG_USER_CHUNKS_SUPPORTED
-#  ifdef PNG_NO_READ_UNKNOWN_CHUNKS
-#    undef PNG_NO_READ_UNKNOWN_CHUNKS
-#  endif
-#  ifdef PNG_NO_HANDLE_AS_UNKNOWN
-#    undef PNG_NO_HANDLE_AS_UNKNOWN
-#  endif
-#endif
-#ifndef PNG_NO_READ_OPT_PLTE
-#  define PNG_READ_OPT_PLTE_SUPPORTED /* only affects support of the */
-#endif                      /* optional PLTE chunk in RGB and RGBA images */
-#if defined(PNG_READ_iTXt_SUPPORTED) || defined(PNG_READ_tEXt_SUPPORTED) || \
-    defined(PNG_READ_zTXt_SUPPORTED)
-#  define PNG_READ_TEXT_SUPPORTED
-#  define PNG_TEXT_SUPPORTED
-#endif
-
-#endif /* PNG_READ_ANCILLARY_CHUNKS_SUPPORTED */
-
-#ifdef PNG_WRITE_ANCILLARY_CHUNKS_SUPPORTED
-
-#ifdef PNG_NO_WRITE_TEXT
-#  define PNG_NO_WRITE_iTXt
-#  define PNG_NO_WRITE_tEXt
-#  define PNG_NO_WRITE_zTXt
-#endif
-#ifndef PNG_NO_WRITE_bKGD
-#  define PNG_WRITE_bKGD_SUPPORTED
-#  ifndef PNG_bKGD_SUPPORTED
-#    define PNG_bKGD_SUPPORTED
-#  endif
-#endif
-#ifndef PNG_NO_WRITE_cHRM
-#  define PNG_WRITE_cHRM_SUPPORTED
-#  ifndef PNG_cHRM_SUPPORTED
-#    define PNG_cHRM_SUPPORTED
-#  endif
-#endif
-#ifndef PNG_NO_WRITE_gAMA
-#  define PNG_WRITE_gAMA_SUPPORTED
-#  ifndef PNG_gAMA_SUPPORTED
-#    define PNG_gAMA_SUPPORTED
-#  endif
-#endif
-#ifndef PNG_NO_WRITE_hIST
-#  define PNG_WRITE_hIST_SUPPORTED
-#  ifndef PNG_hIST_SUPPORTED
-#    define PNG_hIST_SUPPORTED
-#  endif
-#endif
-#ifndef PNG_NO_WRITE_iCCP
-#  define PNG_WRITE_iCCP_SUPPORTED
-#  ifndef PNG_iCCP_SUPPORTED
-#    define PNG_iCCP_SUPPORTED
-#  endif
-#endif
-#ifndef PNG_NO_WRITE_iTXt
-#  ifndef PNG_WRITE_iTXt_SUPPORTED
-#    define PNG_WRITE_iTXt_SUPPORTED
-#  endif
-#  ifndef PNG_iTXt_SUPPORTED
-#    define PNG_iTXt_SUPPORTED
-#  endif
-#endif
-#ifndef PNG_NO_WRITE_oFFs
-#  define PNG_WRITE_oFFs_SUPPORTED
-#  ifndef PNG_oFFs_SUPPORTED
-#    define PNG_oFFs_SUPPORTED
-#  endif
-#endif
-#ifndef PNG_NO_WRITE_pCAL
-#  define PNG_WRITE_pCAL_SUPPORTED
-#  ifndef PNG_pCAL_SUPPORTED
-#    define PNG_pCAL_SUPPORTED
-#  endif
-#endif
-#ifndef PNG_NO_WRITE_sCAL
-#  define PNG_WRITE_sCAL_SUPPORTED
-#  ifndef PNG_sCAL_SUPPORTED
-#    define PNG_sCAL_SUPPORTED
-#  endif
-#endif
-#ifndef PNG_NO_WRITE_pHYs
-#  define PNG_WRITE_pHYs_SUPPORTED
-#  ifndef PNG_pHYs_SUPPORTED
-#    define PNG_pHYs_SUPPORTED
-#  endif
-#endif
-#ifndef PNG_NO_WRITE_sBIT
-#  define PNG_WRITE_sBIT_SUPPORTED
-#  ifndef PNG_sBIT_SUPPORTED
-#    define PNG_sBIT_SUPPORTED
-#  endif
-#endif
-#ifndef PNG_NO_WRITE_sPLT
-#  define PNG_WRITE_sPLT_SUPPORTED
-#  ifndef PNG_sPLT_SUPPORTED
-#    define PNG_sPLT_SUPPORTED
-#  endif
-#endif
-#ifndef PNG_NO_WRITE_sRGB
-#  define PNG_WRITE_sRGB_SUPPORTED
-#  ifndef PNG_sRGB_SUPPORTED
-#    define PNG_sRGB_SUPPORTED
-#  endif
-#endif
-#ifndef PNG_NO_WRITE_tEXt
-#  define PNG_WRITE_tEXt_SUPPORTED
-#  ifndef PNG_tEXt_SUPPORTED
-#    define PNG_tEXt_SUPPORTED
-#  endif
-#endif
-#ifndef PNG_NO_WRITE_tIME
-#  define PNG_WRITE_tIME_SUPPORTED
-#  ifndef PNG_tIME_SUPPORTED
-#    define PNG_tIME_SUPPORTED
-#  endif
-#endif
-#ifndef PNG_NO_WRITE_tRNS
-#  define PNG_WRITE_tRNS_SUPPORTED
-#  ifndef PNG_tRNS_SUPPORTED
-#    define PNG_tRNS_SUPPORTED
-#  endif
-#endif
-#ifndef PNG_NO_WRITE_zTXt
-#  define PNG_WRITE_zTXt_SUPPORTED
-#  ifndef PNG_zTXt_SUPPORTED
-#    define PNG_zTXt_SUPPORTED
-#  endif
-#endif
-#ifndef PNG_NO_WRITE_UNKNOWN_CHUNKS
-#  define PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
-#  ifndef PNG_UNKNOWN_CHUNKS_SUPPORTED
-#    define PNG_UNKNOWN_CHUNKS_SUPPORTED
-#  endif
-#  ifndef PNG_NO_HANDLE_AS_UNKNOWN
-#     ifndef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
-#       define PNG_HANDLE_AS_UNKNOWN_SUPPORTED
-#     endif
-#  endif
-#endif
-#if defined(PNG_WRITE_iTXt_SUPPORTED) || defined(PNG_WRITE_tEXt_SUPPORTED) || \
-    defined(PNG_WRITE_zTXt_SUPPORTED)
-#  define PNG_WRITE_TEXT_SUPPORTED
-#  ifndef PNG_TEXT_SUPPORTED
-#    define PNG_TEXT_SUPPORTED
-#  endif
-#endif
-
-#endif /* PNG_WRITE_ANCILLARY_CHUNKS_SUPPORTED */
-
-/* Turn this off to disable png_read_png() and
- * png_write_png() and leave the row_pointers member
- * out of the info structure.
- */
-#ifndef PNG_NO_INFO_IMAGE
-#  define PNG_INFO_IMAGE_SUPPORTED
-#endif
-
-/* need the time information for reading tIME chunks */
-#if defined(PNG_tIME_SUPPORTED)
-#  if !defined(_WIN32_WCE)
-     /* "time.h" functions are not supported on WindowsCE */
-#    include <time.h>
-#  endif
-#endif
-
-/* Some typedefs to get us started.  These should be safe on most of the
- * common platforms.  The typedefs should be at least as large as the
- * numbers suggest (a png_uint_32 must be at least 32 bits long), but they
- * don't have to be exactly that size.  Some compilers dislike passing
- * unsigned shorts as function parameters, so you may be better off using
- * unsigned int for png_uint_16.  Likewise, for 64-bit systems, you may
- * want to have unsigned int for png_uint_32 instead of unsigned long.
- */
-
-typedef unsigned long png_uint_32;
-typedef long png_int_32;
-typedef unsigned short png_uint_16;
-typedef short png_int_16;
-typedef unsigned char png_byte;
-
-/* This is usually size_t.  It is typedef'ed just in case you need it to
-   change (I'm not sure if you will or not, so I thought I'd be safe) */
-#ifdef PNG_SIZE_T
-   typedef PNG_SIZE_T png_size_t;
-#  define png_sizeof(x) png_convert_size(sizeof (x))
-#else
-   typedef size_t png_size_t;
-#  define png_sizeof(x) sizeof (x)
-#endif
-
-/* The following is needed for medium model support.  It cannot be in the
- * PNG_INTERNAL section.  Needs modification for other compilers besides
- * MSC.  Model independent support declares all arrays and pointers to be
- * large using the far keyword.  The zlib version used must also support
- * model independent data.  As of version zlib 1.0.4, the necessary changes
- * have been made in zlib.  The USE_FAR_KEYWORD define triggers other
- * changes that are needed. (Tim Wegner)
- */
-
-/* Separate compiler dependencies (problem here is that zlib.h always
-   defines FAR. (SJT) */
-#ifdef __BORLANDC__
-#  if defined(__LARGE__) || defined(__HUGE__) || defined(__COMPACT__)
-#    define LDATA 1
-#  else
-#    define LDATA 0
-#  endif
-   /* GRR:  why is Cygwin in here?  Cygwin is not Borland C... */
-#  if !defined(__WIN32__) && !defined(__FLAT__) && !defined(__CYGWIN__)
-#    define PNG_MAX_MALLOC_64K
-#    if (LDATA != 1)
-#      ifndef FAR
-#        define FAR __far
-#      endif
-#      define USE_FAR_KEYWORD
-#    endif   /* LDATA != 1 */
-     /* Possibly useful for moving data out of default segment.
-      * Uncomment it if you want. Could also define FARDATA as
-      * const if your compiler supports it. (SJT)
-#    define FARDATA FAR
-      */
-#  endif  /* __WIN32__, __FLAT__, __CYGWIN__ */
-#endif   /* __BORLANDC__ */
-
-
-/* Suggest testing for specific compiler first before testing for
- * FAR.  The Watcom compiler defines both __MEDIUM__ and M_I86MM,
- * making reliance oncertain keywords suspect. (SJT)
- */
-
-/* MSC Medium model */
-#if defined(FAR)
-#  if defined(M_I86MM)
-#    define USE_FAR_KEYWORD
-#    define FARDATA FAR
-#    include <dos.h>
-#  endif
-#endif
-
-/* SJT: default case */
-#ifndef FAR
-#  define FAR
-#endif
-
-/* At this point FAR is always defined */
-#ifndef FARDATA
-#  define FARDATA
-#endif
-
-/* Typedef for floating-point numbers that are converted
-   to fixed-point with a multiple of 100,000, e.g., int_gamma */
-typedef png_int_32 png_fixed_point;
-
-/* Add typedefs for pointers */
-typedef void            FAR * png_voidp;
-typedef png_byte        FAR * png_bytep;
-typedef png_uint_32     FAR * png_uint_32p;
-typedef png_int_32      FAR * png_int_32p;
-typedef png_uint_16     FAR * png_uint_16p;
-typedef png_int_16      FAR * png_int_16p;
-typedef PNG_CONST char  FAR * png_const_charp;
-typedef char            FAR * png_charp;
-typedef png_fixed_point FAR * png_fixed_point_p;
-
-#ifndef PNG_NO_STDIO
-#if defined(_WIN32_WCE)
-typedef HANDLE                png_FILE_p;
-#else
-typedef FILE                * png_FILE_p;
-#endif
-#endif
-
-#ifdef PNG_FLOATING_POINT_SUPPORTED
-typedef double          FAR * png_doublep;
-#endif
-
-/* Pointers to pointers; i.e. arrays */
-typedef png_byte        FAR * FAR * png_bytepp;
-typedef png_uint_32     FAR * FAR * png_uint_32pp;
-typedef png_int_32      FAR * FAR * png_int_32pp;
-typedef png_uint_16     FAR * FAR * png_uint_16pp;
-typedef png_int_16      FAR * FAR * png_int_16pp;
-typedef PNG_CONST char  FAR * FAR * png_const_charpp;
-typedef char            FAR * FAR * png_charpp;
-typedef png_fixed_point FAR * FAR * png_fixed_point_pp;
-#ifdef PNG_FLOATING_POINT_SUPPORTED
-typedef double          FAR * FAR * png_doublepp;
-#endif
-
-/* Pointers to pointers to pointers; i.e., pointer to array */
-typedef char            FAR * FAR * FAR * png_charppp;
-
-#if defined(PNG_1_0_X) || defined(PNG_1_2_X)
-/* SPC -  Is this stuff deprecated? */
-/* It'll be removed as of libpng-1.3.0 - GR-P */
-/* libpng typedefs for types in zlib. If zlib changes
- * or another compression library is used, then change these.
- * Eliminates need to change all the source files.
- */
-typedef charf *         png_zcharp;
-typedef charf * FAR *   png_zcharpp;
-typedef z_stream FAR *  png_zstreamp;
-#endif /* (PNG_1_0_X) || defined(PNG_1_2_X) */
-
-/*
- * Define PNG_BUILD_DLL if the module being built is a Windows
- * LIBPNG DLL.
- *
- * Define PNG_USE_DLL if you want to *link* to the Windows LIBPNG DLL.
- * It is equivalent to Microsoft predefined macro _DLL that is
- * automatically defined when you compile using the share
- * version of the CRT (C Run-Time library)
- *
- * The cygwin mods make this behavior a little different:
- * Define PNG_BUILD_DLL if you are building a dll for use with cygwin
- * Define PNG_STATIC if you are building a static library for use with cygwin,
- *   -or- if you are building an application that you want to link to the
- *   static library.
- * PNG_USE_DLL is defined by default (no user action needed) unless one of
- *   the other flags is defined.
- */
-
-#if !defined(PNG_DLL) && (defined(PNG_BUILD_DLL) || defined(PNG_USE_DLL))
-#  define PNG_DLL
-#endif
-/* If CYGWIN, then disallow GLOBAL ARRAYS unless building a static lib.
- * When building a static lib, default to no GLOBAL ARRAYS, but allow
- * command-line override
- */
-#if defined(__CYGWIN__)
-#  if !defined(PNG_STATIC)
-#    if defined(PNG_USE_GLOBAL_ARRAYS)
-#      undef PNG_USE_GLOBAL_ARRAYS
-#    endif
-#    if !defined(PNG_USE_LOCAL_ARRAYS)
-#      define PNG_USE_LOCAL_ARRAYS
-#    endif
-#  else
-#    if defined(PNG_USE_LOCAL_ARRAYS) || defined(PNG_NO_GLOBAL_ARRAYS)
-#      if defined(PNG_USE_GLOBAL_ARRAYS)
-#        undef PNG_USE_GLOBAL_ARRAYS
-#      endif
-#    endif
-#  endif
-#  if !defined(PNG_USE_LOCAL_ARRAYS) && !defined(PNG_USE_GLOBAL_ARRAYS)
-#    define PNG_USE_LOCAL_ARRAYS
-#  endif
-#endif
-
-/* Do not use global arrays (helps with building DLL's)
- * They are no longer used in libpng itself, since version 1.0.5c,
- * but might be required for some pre-1.0.5c applications.
- */
-#if !defined(PNG_USE_LOCAL_ARRAYS) && !defined(PNG_USE_GLOBAL_ARRAYS)
-#  if defined(PNG_NO_GLOBAL_ARRAYS) || \
-      (defined(__GNUC__) && defined(PNG_DLL)) || defined(_MSC_VER)
-#    define PNG_USE_LOCAL_ARRAYS
-#  else
-#    define PNG_USE_GLOBAL_ARRAYS
-#  endif
-#endif
-
-#if defined(__CYGWIN__)
-#  undef PNGAPI
-#  define PNGAPI __cdecl
-#  undef PNG_IMPEXP
-#  define PNG_IMPEXP
-#endif  
-
-/* If you define PNGAPI, e.g., with compiler option "-DPNGAPI=__stdcall",
- * you may get warnings regarding the linkage of png_zalloc and png_zfree.
- * Don't ignore those warnings; you must also reset the default calling
- * convention in your compiler to match your PNGAPI, and you must build
- * zlib and your applications the same way you build libpng.
- */
-
-#if defined(__MINGW32__) && !defined(PNG_MODULEDEF)
-#  ifndef PNG_NO_MODULEDEF
-#    define PNG_NO_MODULEDEF
-#  endif
-#endif
-
-#if !defined(PNG_IMPEXP) && defined(PNG_BUILD_DLL) && !defined(PNG_NO_MODULEDEF)
-#  define PNG_IMPEXP
-#endif
-
-#if defined(PNG_DLL) || defined(_DLL) || defined(__DLL__ ) || \
-    (( defined(_Windows) || defined(_WINDOWS) || \
-       defined(WIN32) || defined(_WIN32) || defined(__WIN32__) ))
-
-#  ifndef PNGAPI
-#     if defined(__GNUC__) || (defined (_MSC_VER) && (_MSC_VER >= 800))
-#        define PNGAPI __cdecl
-#     else
-#        define PNGAPI _cdecl
-#     endif
-#  endif
-
-#  if !defined(PNG_IMPEXP) && (!defined(PNG_DLL) || \
-       0 /* WINCOMPILER_WITH_NO_SUPPORT_FOR_DECLIMPEXP */)
-#     define PNG_IMPEXP
-#  endif
-
-#  if !defined(PNG_IMPEXP)
-
-#     define PNG_EXPORT_TYPE1(type,symbol)  PNG_IMPEXP type PNGAPI symbol
-#     define PNG_EXPORT_TYPE2(type,symbol)  type PNG_IMPEXP PNGAPI symbol
-
-      /* Borland/Microsoft */
-#     if defined(_MSC_VER) || defined(__BORLANDC__)
-#        if (_MSC_VER >= 800) || (__BORLANDC__ >= 0x500)
-#           define PNG_EXPORT PNG_EXPORT_TYPE1
-#        else
-#           define PNG_EXPORT PNG_EXPORT_TYPE2
-#           if defined(PNG_BUILD_DLL)
-#              define PNG_IMPEXP __export
-#           else
-#              define PNG_IMPEXP /*__import */ /* doesn't exist AFAIK in
-                                                 VC++ */
-#           endif                             /* Exists in Borland C++ for
-                                                 C++ classes (== huge) */
-#        endif
-#     endif
-
-#     if !defined(PNG_IMPEXP)
-#        if defined(PNG_BUILD_DLL)
-#           define PNG_IMPEXP __declspec(dllexport)
-#        else
-#           define PNG_IMPEXP __declspec(dllimport)
-#        endif
-#     endif
-#  endif  /* PNG_IMPEXP */
-#else /* !(DLL || non-cygwin WINDOWS) */
-#   if (defined(__IBMC__) || defined(__IBMCPP__)) && defined(__OS2__)
-#      ifndef PNGAPI
-#         define PNGAPI _System
-#      endif
-#   else
-#      if 0 /* ... other platforms, with other meanings */
-#      endif
-#   endif
-#endif
-
-#ifndef PNGAPI
-#  define PNGAPI
-#endif
-#ifndef PNG_IMPEXP
-#  define PNG_IMPEXP
-#endif
-
-#ifdef PNG_BUILDSYMS
-#  ifndef PNG_EXPORT
-#    define PNG_EXPORT(type,symbol) PNG_FUNCTION_EXPORT symbol END
-#  endif
-#  ifdef PNG_USE_GLOBAL_ARRAYS
-#    ifndef PNG_EXPORT_VAR
-#      define PNG_EXPORT_VAR(type) PNG_DATA_EXPORT
-#    endif
-#  endif
-#endif
-
-#ifndef PNG_EXPORT
-#  define PNG_EXPORT(type,symbol) PNG_IMPEXP type PNGAPI symbol
-#endif
-
-#ifdef PNG_USE_GLOBAL_ARRAYS
-#  ifndef PNG_EXPORT_VAR
-#    define PNG_EXPORT_VAR(type) extern PNG_IMPEXP type
-#  endif
-#endif
-
-/* User may want to use these so they are not in PNG_INTERNAL. Any library
- * functions that are passed far data must be model independent.
- */
-
-#ifndef PNG_ABORT
-#  define PNG_ABORT() abort()
-#endif
-
-#ifdef PNG_SETJMP_SUPPORTED
-#  define png_jmpbuf(png_ptr) ((png_ptr)->jmpbuf)
-#else
-#  define png_jmpbuf(png_ptr) \
-   (LIBPNG_WAS_COMPILED_WITH__PNG_SETJMP_NOT_SUPPORTED)
-#endif
-
-#if defined(USE_FAR_KEYWORD)  /* memory model independent fns */
-/* use this to make far-to-near assignments */
-#  define CHECK   1
-#  define NOCHECK 0
-#  define CVT_PTR(ptr) (png_far_to_near(png_ptr,ptr,CHECK))
-#  define CVT_PTR_NOCHECK(ptr) (png_far_to_near(png_ptr,ptr,NOCHECK))
-#  define png_snprintf _fsnprintf   /* Added to v 1.2.19 */
-#  define png_strlen  _fstrlen
-#  define png_memcmp  _fmemcmp    /* SJT: added */
-#  define png_memcpy  _fmemcpy
-#  define png_memset  _fmemset
-#else /* use the usual functions */
-#  define CVT_PTR(ptr)         (ptr)
-#  define CVT_PTR_NOCHECK(ptr) (ptr)
-#  ifndef PNG_NO_SNPRINTF
-#    ifdef _MSC_VER
-#      define png_snprintf _snprintf   /* Added to v 1.2.19 */
-#      define png_snprintf2 _snprintf
-#      define png_snprintf6 _snprintf
-#    else
-#      define png_snprintf snprintf   /* Added to v 1.2.19 */
-#      define png_snprintf2 snprintf
-#      define png_snprintf6 snprintf
-#    endif
-#  else
-     /* You don't have or don't want to use snprintf().  Caution: Using
-      * sprintf instead of snprintf exposes your application to accidental
-      * or malevolent buffer overflows.  If you don't have snprintf()
-      * as a general rule you should provide one (you can get one from
-      * Portable OpenSSH). */
-#    define png_snprintf(s1,n,fmt,x1) sprintf(s1,fmt,x1)
-#    define png_snprintf2(s1,n,fmt,x1,x2) sprintf(s1,fmt,x1,x2)
-#    define png_snprintf6(s1,n,fmt,x1,x2,x3,x4,x5,x6) \
-        sprintf(s1,fmt,x1,x2,x3,x4,x5,x6)
-#  endif
-#  define png_strlen  strlen
-#  define png_memcmp  memcmp      /* SJT: added */
-#  define png_memcpy  memcpy
-#  define png_memset  memset
-#endif
-/* End of memory model independent support */
-
-/* Just a little check that someone hasn't tried to define something
- * contradictory.
- */
-#if (PNG_ZBUF_SIZE > 65536L) && defined(PNG_MAX_MALLOC_64K)
-#  undef PNG_ZBUF_SIZE
-#  define PNG_ZBUF_SIZE 65536L
-#endif
-
-/* Added at libpng-1.2.8 */
-#endif /* PNG_VERSION_INFO_ONLY */
-
-#endif /* PNGCONF_H */
+\r
+/* pngconf.h - machine configurable file for libpng\r
+ *\r
+ * libpng version 1.4.3 - June 26, 2010\r
+ * For conditions of distribution and use, see copyright notice in png.h\r
+ * Copyright (c) 1998-2010 Glenn Randers-Pehrson\r
+ * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)\r
+ * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)\r
+ *\r
+ * This code is released under the libpng license.\r
+ * For conditions of distribution and use, see the disclaimer\r
+ * and license in png.h\r
+ *\r
+ */\r
+\r
+/* Any machine specific code is near the front of this file, so if you\r
+ * are configuring libpng for a machine, you may want to read the section\r
+ * starting here down to where it starts to typedef png_color, png_text,\r
+ * and png_info.\r
+ */\r
+\r
+#ifndef PNGCONF_H\r
+#define PNGCONF_H\r
+\r
+#ifndef PNG_NO_LIMITS_H\r
+#  include <limits.h>\r
+#endif\r
+\r
+/* Added at libpng-1.2.9 */\r
+\r
+/* config.h is created by and PNG_CONFIGURE_LIBPNG is set by the "configure"\r
+ * script.\r
+ */\r
+#ifdef PNG_CONFIGURE_LIBPNG\r
+#  ifdef HAVE_CONFIG_H\r
+#    include "config.h"\r
+#  endif\r
+#endif\r
+\r
+/*\r
+ * Added at libpng-1.2.8\r
+ *\r
+ * PNG_USER_CONFIG has to be defined on the compiler command line. This\r
+ * includes the resource compiler for Windows DLL configurations.\r
+ */\r
+#ifdef PNG_USER_CONFIG\r
+#  ifndef PNG_USER_PRIVATEBUILD\r
+#    define PNG_USER_PRIVATEBUILD\r
+#  endif\r
+#  include "pngusr.h"\r
+#endif\r
+\r
+/*\r
+ * If you create a private DLL you need to define in "pngusr.h" the followings:\r
+ * #define PNG_USER_PRIVATEBUILD <Describes by whom and why this version of\r
+ *        the DLL was built>\r
+ *  e.g. #define PNG_USER_PRIVATEBUILD "Build by MyCompany for xyz reasons."\r
+ * #define PNG_USER_DLLFNAME_POSTFIX <two-letter postfix that serve to\r
+ *        distinguish your DLL from those of the official release. These\r
+ *        correspond to the trailing letters that come after the version\r
+ *        number and must match your private DLL name>\r
+ *  e.g. // private DLL "libpng13gx.dll"\r
+ *       #define PNG_USER_DLLFNAME_POSTFIX "gx"\r
+ *\r
+ * The following macros are also at your disposal if you want to complete the\r
+ * DLL VERSIONINFO structure.\r
+ * - PNG_USER_VERSIONINFO_COMMENTS\r
+ * - PNG_USER_VERSIONINFO_COMPANYNAME\r
+ * - PNG_USER_VERSIONINFO_LEGALTRADEMARKS\r
+ */\r
+\r
+#ifdef __STDC__\r
+#  ifdef SPECIALBUILD\r
+#    pragma message("PNG_LIBPNG_SPECIALBUILD (and deprecated SPECIALBUILD)\\r
+     are now LIBPNG reserved macros. Use PNG_USER_PRIVATEBUILD instead.")\r
+#  endif\r
+\r
+#  ifdef PRIVATEBUILD\r
+#    pragma message("PRIVATEBUILD is deprecated.\\r
+     Use PNG_USER_PRIVATEBUILD instead.")\r
+#    define PNG_USER_PRIVATEBUILD PRIVATEBUILD\r
+#  endif\r
+#endif /* __STDC__ */\r
+\r
+/* End of material added to libpng-1.2.8 */\r
+\r
+#ifndef PNG_VERSION_INFO_ONLY\r
+\r
+/* This is the size of the compression buffer, and thus the size of\r
+ * an IDAT chunk.  Make this whatever size you feel is best for your\r
+ * machine.  One of these will be allocated per png_struct.  When this\r
+ * is full, it writes the data to the disk, and does some other\r
+ * calculations.  Making this an extremely small size will slow\r
+ * the library down, but you may want to experiment to determine\r
+ * where it becomes significant, if you are concerned with memory\r
+ * usage.  Note that zlib allocates at least 32Kb also.  For readers,\r
+ * this describes the size of the buffer available to read the data in.\r
+ * Unless this gets smaller than the size of a row (compressed),\r
+ * it should not make much difference how big this is.\r
+ */\r
+\r
+#ifndef PNG_ZBUF_SIZE\r
+#  define PNG_ZBUF_SIZE 8192\r
+#endif\r
+\r
+/* Enable if you want a write-only libpng */\r
+\r
+#ifndef PNG_NO_READ_SUPPORTED\r
+#  define PNG_READ_SUPPORTED\r
+#endif\r
+\r
+/* Enable if you want a read-only libpng */\r
+\r
+#ifndef PNG_NO_WRITE_SUPPORTED\r
+#  define PNG_WRITE_SUPPORTED\r
+#endif\r
+\r
+/* Enabled in 1.4.0. */\r
+#ifdef PNG_ALLOW_BENIGN_ERRORS\r
+#  define png_benign_error png_warning\r
+#  define png_chunk_benign_error png_chunk_warning\r
+#else\r
+#  ifndef PNG_BENIGN_ERRORS_SUPPORTED\r
+#    define png_benign_error png_error\r
+#    define png_chunk_benign_error png_chunk_error\r
+#  endif\r
+#endif\r
+\r
+/* Added at libpng version 1.4.0 */\r
+#if !defined(PNG_NO_WARNINGS) && !defined(PNG_WARNINGS_SUPPORTED)\r
+#  define PNG_WARNINGS_SUPPORTED\r
+#endif\r
+\r
+/* Added at libpng version 1.4.0 */\r
+#if !defined(PNG_NO_ERROR_TEXT) && !defined(PNG_ERROR_TEXT_SUPPORTED)\r
+#  define PNG_ERROR_TEXT_SUPPORTED\r
+#endif\r
+\r
+/* Added at libpng version 1.4.0 */\r
+#if !defined(PNG_NO_CHECK_cHRM) && !defined(PNG_CHECK_cHRM_SUPPORTED)\r
+#  define PNG_CHECK_cHRM_SUPPORTED\r
+#endif\r
+\r
+/* Added at libpng version 1.4.0 */\r
+#if !defined(PNG_NO_ALIGNED_MEMORY) && !defined(PNG_ALIGNED_MEMORY_SUPPORTED)\r
+#  define PNG_ALIGNED_MEMORY_SUPPORTED\r
+#endif\r
+\r
+/* Enabled by default in 1.2.0.  You can disable this if you don't need to\r
+   support PNGs that are embedded in MNG datastreams */\r
+#ifndef PNG_NO_MNG_FEATURES\r
+#  ifndef PNG_MNG_FEATURES_SUPPORTED\r
+#    define PNG_MNG_FEATURES_SUPPORTED\r
+#  endif\r
+#endif\r
+\r
+/* Added at libpng version 1.4.0 */\r
+#ifndef PNG_NO_FLOATING_POINT_SUPPORTED\r
+#  ifndef PNG_FLOATING_POINT_SUPPORTED\r
+#    define PNG_FLOATING_POINT_SUPPORTED\r
+#  endif\r
+#endif\r
+\r
+/* Added at libpng-1.4.0beta49 for testing (this test is no longer used\r
+   in libpng and png_calloc() is always present)\r
+ */\r
+#define PNG_CALLOC_SUPPORTED\r
+\r
+/* If you are running on a machine where you cannot allocate more\r
+ * than 64K of memory at once, uncomment this.  While libpng will not\r
+ * normally need that much memory in a chunk (unless you load up a very\r
+ * large file), zlib needs to know how big of a chunk it can use, and\r
+ * libpng thus makes sure to check any memory allocation to verify it\r
+ * will fit into memory.\r
+#define PNG_MAX_MALLOC_64K\r
+ */\r
+#if defined(MAXSEG_64K) && !defined(PNG_MAX_MALLOC_64K)\r
+#  define PNG_MAX_MALLOC_64K\r
+#endif\r
+\r
+/* Special munging to support doing things the 'cygwin' way:\r
+ * 'Normal' png-on-win32 defines/defaults:\r
+ *   PNG_BUILD_DLL -- building dll\r
+ *   PNG_USE_DLL   -- building an application, linking to dll\r
+ *   (no define)   -- building static library, or building an\r
+ *                    application and linking to the static lib\r
+ * 'Cygwin' defines/defaults:\r
+ *   PNG_BUILD_DLL -- (ignored) building the dll\r
+ *   (no define)   -- (ignored) building an application, linking to the dll\r
+ *   PNG_STATIC    -- (ignored) building the static lib, or building an\r
+ *                    application that links to the static lib.\r
+ *   ALL_STATIC    -- (ignored) building various static libs, or building an\r
+ *                    application that links to the static libs.\r
+ * Thus,\r
+ * a cygwin user should define either PNG_BUILD_DLL or PNG_STATIC, and\r
+ * this bit of #ifdefs will define the 'correct' config variables based on\r
+ * that. If a cygwin user *wants* to define 'PNG_USE_DLL' that's okay, but\r
+ * unnecessary.\r
+ *\r
+ * Also, the precedence order is:\r
+ *   ALL_STATIC (since we can't #undef something outside our namespace)\r
+ *   PNG_BUILD_DLL\r
+ *   PNG_STATIC\r
+ *   (nothing) == PNG_USE_DLL\r
+ *\r
+ * CYGWIN (2002-01-20): The preceding is now obsolete. With the advent\r
+ *   of auto-import in binutils, we no longer need to worry about\r
+ *   __declspec(dllexport) / __declspec(dllimport) and friends.  Therefore,\r
+ *   we don't need to worry about PNG_STATIC or ALL_STATIC when it comes\r
+ *   to __declspec() stuff.  However, we DO need to worry about\r
+ *   PNG_BUILD_DLL and PNG_STATIC because those change some defaults\r
+ *   such as CONSOLE_IO.\r
+ */\r
+#ifdef __CYGWIN__\r
+#  ifdef ALL_STATIC\r
+#    ifdef PNG_BUILD_DLL\r
+#      undef PNG_BUILD_DLL\r
+#    endif\r
+#    ifdef PNG_USE_DLL\r
+#      undef PNG_USE_DLL\r
+#    endif\r
+#    ifdef PNG_DLL\r
+#      undef PNG_DLL\r
+#    endif\r
+#    ifndef PNG_STATIC\r
+#      define PNG_STATIC\r
+#    endif\r
+#  else\r
+#    ifdef PNG_BUILD_DLL\r
+#      ifdef PNG_STATIC\r
+#        undef PNG_STATIC\r
+#      endif\r
+#      ifdef PNG_USE_DLL\r
+#        undef PNG_USE_DLL\r
+#      endif\r
+#      ifndef PNG_DLL\r
+#        define PNG_DLL\r
+#      endif\r
+#    else\r
+#      ifdef PNG_STATIC\r
+#        ifdef PNG_USE_DLL\r
+#          undef PNG_USE_DLL\r
+#        endif\r
+#        ifdef PNG_DLL\r
+#          undef PNG_DLL\r
+#        endif\r
+#      else\r
+#        ifndef PNG_USE_DLL\r
+#          define PNG_USE_DLL\r
+#        endif\r
+#        ifndef PNG_DLL\r
+#          define PNG_DLL\r
+#        endif\r
+#      endif\r
+#    endif\r
+#  endif\r
+#endif\r
+\r
+/* This protects us against compilers that run on a windowing system\r
+ * and thus don't have or would rather us not use the stdio types:\r
+ * stdin, stdout, and stderr.  The only one currently used is stderr\r
+ * in png_error() and png_warning().  #defining PNG_NO_CONSOLE_IO will\r
+ * prevent these from being compiled and used. #defining PNG_NO_STDIO\r
+ * will also prevent these, plus will prevent the entire set of stdio\r
+ * macros and functions (FILE *, printf, etc.) from being compiled and used,\r
+ * unless (PNG_DEBUG > 0) has been #defined.\r
+ *\r
+ * #define PNG_NO_CONSOLE_IO\r
+ * #define PNG_NO_STDIO\r
+ */\r
+\r
+#if !defined(PNG_NO_STDIO) && !defined(PNG_STDIO_SUPPORTED)\r
+#  define PNG_STDIO_SUPPORTED\r
+#endif\r
+\r
+\r
+#ifdef PNG_BUILD_DLL\r
+#  if !defined(PNG_CONSOLE_IO_SUPPORTED) && !defined(PNG_NO_CONSOLE_IO)\r
+#    define PNG_NO_CONSOLE_IO\r
+#  endif\r
+#endif\r
+\r
+#  ifdef PNG_NO_STDIO\r
+#    ifndef PNG_NO_CONSOLE_IO\r
+#      define PNG_NO_CONSOLE_IO\r
+#    endif\r
+#    ifdef PNG_DEBUG\r
+#      if (PNG_DEBUG > 0)\r
+#        include <stdio.h>\r
+#      endif\r
+#    endif\r
+#  else\r
+#    include <stdio.h>\r
+#  endif\r
+\r
+#if !(defined PNG_NO_CONSOLE_IO) && !defined(PNG_CONSOLE_IO_SUPPORTED)\r
+#  define PNG_CONSOLE_IO_SUPPORTED\r
+#endif\r
+\r
+/* This macro protects us against machines that don't have function\r
+ * prototypes (ie K&R style headers).  If your compiler does not handle\r
+ * function prototypes, define this macro and use the included ansi2knr.\r
+ * I've always been able to use _NO_PROTO as the indicator, but you may\r
+ * need to drag the empty declaration out in front of here, or change the\r
+ * ifdef to suit your own needs.\r
+ */\r
+#ifndef PNGARG\r
+\r
+#ifdef OF /* zlib prototype munger */\r
+#  define PNGARG(arglist) OF(arglist)\r
+#else\r
+\r
+#ifdef _NO_PROTO\r
+#  define PNGARG(arglist) ()\r
+#else\r
+#  define PNGARG(arglist) arglist\r
+#endif /* _NO_PROTO */\r
+\r
+#endif /* OF */\r
+\r
+#endif /* PNGARG */\r
+\r
+/* Try to determine if we are compiling on a Mac.  Note that testing for\r
+ * just __MWERKS__ is not good enough, because the Codewarrior is now used\r
+ * on non-Mac platforms.\r
+ */\r
+#ifndef MACOS\r
+#  if (defined(__MWERKS__) && defined(macintosh)) || defined(applec) || \\r
+      defined(THINK_C) || defined(__SC__) || defined(TARGET_OS_MAC)\r
+#    define MACOS\r
+#  endif\r
+#endif\r
+\r
+/* Enough people need this for various reasons to include it here */\r
+#if !defined(MACOS) && !defined(RISCOS)\r
+#  include <sys/types.h>\r
+#endif\r
+\r
+/* PNG_SETJMP_NOT_SUPPORTED and PNG_NO_SETJMP_SUPPORTED are deprecated. */\r
+#if !defined(PNG_NO_SETJMP) && \\r
+    !defined(PNG_SETJMP_NOT_SUPPORTED) && !defined(PNG_NO_SETJMP_SUPPORTED)\r
+#  define PNG_SETJMP_SUPPORTED\r
+#endif\r
+\r
+#ifdef PNG_SETJMP_SUPPORTED\r
+/* This is an attempt to force a single setjmp behaviour on Linux.  If\r
+ * the X config stuff didn't define _BSD_SOURCE we wouldn't need this.\r
+ *\r
+ * You can bypass this test if you know that your application uses exactly\r
+ * the same setjmp.h that was included when libpng was built.  Only define\r
+ * PNG_SKIP_SETJMP_CHECK while building your application, prior to the\r
+ * application's '#include "png.h"'. Don't define PNG_SKIP_SETJMP_CHECK\r
+ * while building a separate libpng library for general use.\r
+ */\r
+\r
+#  ifndef PNG_SKIP_SETJMP_CHECK\r
+#    ifdef __linux__\r
+#      ifdef _BSD_SOURCE\r
+#        define PNG_SAVE_BSD_SOURCE\r
+#        undef _BSD_SOURCE\r
+#      endif\r
+#      ifdef _SETJMP_H\r
+       /* If you encounter a compiler error here, see the explanation\r
+        * near the end of INSTALL.\r
+        */\r
+           __pngconf.h__ in libpng already includes setjmp.h;\r
+           __dont__ include it again.;\r
+#      endif\r
+#    endif /* __linux__ */\r
+#  endif /* PNG_SKIP_SETJMP_CHECK */\r
+\r
+   /* Include setjmp.h for error handling */\r
+#  include <setjmp.h>\r
+\r
+#  ifdef __linux__\r
+#    ifdef PNG_SAVE_BSD_SOURCE\r
+#      ifdef _BSD_SOURCE\r
+#        undef _BSD_SOURCE\r
+#      endif\r
+#      define _BSD_SOURCE\r
+#      undef PNG_SAVE_BSD_SOURCE\r
+#    endif\r
+#  endif /* __linux__ */\r
+#endif /* PNG_SETJMP_SUPPORTED */\r
+\r
+#ifdef BSD\r
+#  include <strings.h>\r
+#else\r
+#  include <string.h>\r
+#endif\r
+\r
+/* Other defines for things like memory and the like can go here.  */\r
+\r
+/* This controls how fine the quantizing gets.  As this allocates\r
+ * a largish chunk of memory (32K), those who are not as concerned\r
+ * with quantizing quality can decrease some or all of these.\r
+ */\r
+\r
+/* Prior to libpng-1.4.2, these were PNG_DITHER_*_BITS\r
+ * These migration aids will be removed from libpng-1.5.0.\r
+ */\r
+#ifdef PNG_DITHER_RED_BITS\r
+#  define PNG_QUANTIZE_RED_BITS PNG_DITHER_RED_BITS\r
+#endif\r
+#ifdef PNG_DITHER_GREEN_BITS\r
+#  define PNG_QUANTIZE_GREEN_BITS PNG_DITHER_GREEN_BITS\r
+#endif\r
+#ifdef PNG_DITHER_BLUE_BITS\r
+#  define PNG_QUANTIZE_BLUE_BITS PNG_DITHER_BLUE_BITS\r
+#endif\r
+\r
+#ifndef PNG_QUANTIZE_RED_BITS\r
+#  define PNG_QUANTIZE_RED_BITS 5\r
+#endif\r
+#ifndef PNG_QUANTIZE_GREEN_BITS\r
+#  define PNG_QUANTIZE_GREEN_BITS 5\r
+#endif\r
+#ifndef PNG_QUANTIZE_BLUE_BITS\r
+#  define PNG_QUANTIZE_BLUE_BITS 5\r
+#endif\r
+\r
+/* This controls how fine the gamma correction becomes when you\r
+ * are only interested in 8 bits anyway.  Increasing this value\r
+ * results in more memory being used, and more pow() functions\r
+ * being called to fill in the gamma tables.  Don't set this value\r
+ * less then 8, and even that may not work (I haven't tested it).\r
+ */\r
+\r
+#ifndef PNG_MAX_GAMMA_8\r
+#  define PNG_MAX_GAMMA_8 11\r
+#endif\r
+\r
+/* This controls how much a difference in gamma we can tolerate before\r
+ * we actually start doing gamma conversion.\r
+ */\r
+#ifndef PNG_GAMMA_THRESHOLD\r
+#  define PNG_GAMMA_THRESHOLD 0.05\r
+#endif\r
+\r
+/* The following uses const char * instead of char * for error\r
+ * and warning message functions, so some compilers won't complain.\r
+ * If you do not want to use const, define PNG_NO_CONST here.\r
+ */\r
+\r
+#ifndef PNG_CONST\r
+#  ifndef PNG_NO_CONST\r
+#    define PNG_CONST const\r
+#  else\r
+#    define PNG_CONST\r
+#  endif\r
+#endif\r
+\r
+/* The following defines give you the ability to remove code from the\r
+ * library that you will not be using.  I wish I could figure out how to\r
+ * automate this, but I can't do that without making it seriously hard\r
+ * on the users.  So if you are not using an ability, change the #define\r
+ * to and #undef, and that part of the library will not be compiled.  If\r
+ * your linker can't find a function, you may want to make sure the\r
+ * ability is defined here.  Some of these depend upon some others being\r
+ * defined.  I haven't figured out all the interactions here, so you may\r
+ * have to experiment awhile to get everything to compile.  If you are\r
+ * creating or using a shared library, you probably shouldn't touch this,\r
+ * as it will affect the size of the structures, and this will cause bad\r
+ * things to happen if the library and/or application ever change.\r
+ */\r
+\r
+/* Any features you will not be using can be undef'ed here */\r
+\r
+/* GR-P, 0.96a: Set "*TRANSFORMS_SUPPORTED as default but allow user\r
+ * to turn it off with PNG_NO_READ|WRITE_TRANSFORMS on the compile line,\r
+ * then pick and choose which ones to define without having to edit this\r
+ * file. It is safe to use the PNG_NO_READ|WRITE_TRANSFORMS\r
+ * if you only want to have a png-compliant reader/writer but don't need\r
+ * any of the extra transformations.  This saves about 80 kbytes in a\r
+ * typical installation of the library. (PNG_NO_* form added in version\r
+ * 1.0.1c, for consistency; PNG_*_TRANSFORMS_NOT_SUPPORTED deprecated in\r
+ * 1.4.0)\r
+ */\r
+\r
+/* Ignore attempt to turn off both floating and fixed point support */\r
+#if !defined(PNG_FLOATING_POINT_SUPPORTED) || \\r
+    !defined(PNG_NO_FIXED_POINT_SUPPORTED)\r
+#  define PNG_FIXED_POINT_SUPPORTED\r
+#endif\r
+\r
+#ifdef PNG_READ_SUPPORTED\r
+\r
+/* PNG_READ_TRANSFORMS_NOT_SUPPORTED is deprecated. */\r
+#if !defined(PNG_READ_TRANSFORMS_NOT_SUPPORTED) && \\r
+      !defined(PNG_NO_READ_TRANSFORMS)\r
+#  define PNG_READ_TRANSFORMS_SUPPORTED\r
+#endif\r
+\r
+#ifdef PNG_READ_TRANSFORMS_SUPPORTED\r
+#  ifndef PNG_NO_READ_EXPAND\r
+#    define PNG_READ_EXPAND_SUPPORTED\r
+#  endif\r
+#  ifndef PNG_NO_READ_SHIFT\r
+#    define PNG_READ_SHIFT_SUPPORTED\r
+#  endif\r
+#  ifndef PNG_NO_READ_PACK\r
+#    define PNG_READ_PACK_SUPPORTED\r
+#  endif\r
+#  ifndef PNG_NO_READ_BGR\r
+#    define PNG_READ_BGR_SUPPORTED\r
+#  endif\r
+#  ifndef PNG_NO_READ_SWAP\r
+#    define PNG_READ_SWAP_SUPPORTED\r
+#  endif\r
+#  ifndef PNG_NO_READ_PACKSWAP\r
+#    define PNG_READ_PACKSWAP_SUPPORTED\r
+#  endif\r
+#  ifndef PNG_NO_READ_INVERT\r
+#    define PNG_READ_INVERT_SUPPORTED\r
+#  endif\r
+#  ifndef PNG_NO_READ_QUANTIZE\r
+     /* Prior to libpng-1.4.0 this was PNG_READ_DITHER_SUPPORTED */\r
+#    ifndef PNG_NO_READ_DITHER  /* This migration aid will be removed */\r
+#      define PNG_READ_QUANTIZE_SUPPORTED\r
+#    endif\r
+#  endif\r
+#  ifndef PNG_NO_READ_BACKGROUND\r
+#    define PNG_READ_BACKGROUND_SUPPORTED\r
+#  endif\r
+#  ifndef PNG_NO_READ_16_TO_8\r
+#    define PNG_READ_16_TO_8_SUPPORTED\r
+#  endif\r
+#  ifndef PNG_NO_READ_FILLER\r
+#    define PNG_READ_FILLER_SUPPORTED\r
+#  endif\r
+#  ifndef PNG_NO_READ_GAMMA\r
+#    define PNG_READ_GAMMA_SUPPORTED\r
+#  endif\r
+#  ifndef PNG_NO_READ_GRAY_TO_RGB\r
+#    define PNG_READ_GRAY_TO_RGB_SUPPORTED\r
+#  endif\r
+#  ifndef PNG_NO_READ_SWAP_ALPHA\r
+#    define PNG_READ_SWAP_ALPHA_SUPPORTED\r
+#  endif\r
+#  ifndef PNG_NO_READ_INVERT_ALPHA\r
+#    define PNG_READ_INVERT_ALPHA_SUPPORTED\r
+#  endif\r
+#  ifndef PNG_NO_READ_STRIP_ALPHA\r
+#    define PNG_READ_STRIP_ALPHA_SUPPORTED\r
+#  endif\r
+#  ifndef PNG_NO_READ_USER_TRANSFORM\r
+#    define PNG_READ_USER_TRANSFORM_SUPPORTED\r
+#  endif\r
+#  ifndef PNG_NO_READ_RGB_TO_GRAY\r
+#    define PNG_READ_RGB_TO_GRAY_SUPPORTED\r
+#  endif\r
+#endif /* PNG_READ_TRANSFORMS_SUPPORTED */\r
+\r
+/* PNG_PROGRESSIVE_READ_NOT_SUPPORTED is deprecated. */\r
+#if !defined(PNG_NO_PROGRESSIVE_READ) && \\r
+ !defined(PNG_PROGRESSIVE_READ_NOT_SUPPORTED)  /* if you don't do progressive */\r
+#  define PNG_PROGRESSIVE_READ_SUPPORTED     /* reading.  This is not talking */\r
+#endif                               /* about interlacing capability!  You'll */\r
+            /* still have interlacing unless you change the following define: */\r
+\r
+#define PNG_READ_INTERLACING_SUPPORTED /* required for PNG-compliant decoders */\r
+\r
+/* PNG_NO_SEQUENTIAL_READ_SUPPORTED is deprecated. */\r
+#if !defined(PNG_NO_SEQUENTIAL_READ) && \\r
+    !defined(PNG_SEQUENTIAL_READ_SUPPORTED) && \\r
+    !defined(PNG_NO_SEQUENTIAL_READ_SUPPORTED)\r
+#  define PNG_SEQUENTIAL_READ_SUPPORTED\r
+#endif\r
+\r
+#ifndef PNG_NO_READ_COMPOSITE_NODIV\r
+#  ifndef PNG_NO_READ_COMPOSITED_NODIV  /* libpng-1.0.x misspelling */\r
+#    define PNG_READ_COMPOSITE_NODIV_SUPPORTED   /* well tested on Intel, SGI */\r
+#  endif\r
+#endif\r
+\r
+#if !defined(PNG_NO_GET_INT_32) || defined(PNG_READ_oFFS_SUPPORTED) || \\r
+    defined(PNG_READ_pCAL_SUPPORTED)\r
+#  ifndef PNG_GET_INT_32_SUPPORTED\r
+#    define PNG_GET_INT_32_SUPPORTED\r
+#  endif\r
+#endif\r
+\r
+#endif /* PNG_READ_SUPPORTED */\r
+\r
+#ifdef PNG_WRITE_SUPPORTED\r
+\r
+/* PNG_WRITE_TRANSFORMS_NOT_SUPPORTED is deprecated. */\r
+#if !defined(PNG_WRITE_TRANSFORMS_NOT_SUPPORTED) && \\r
+    !defined(PNG_NO_WRITE_TRANSFORMS)\r
+#  define PNG_WRITE_TRANSFORMS_SUPPORTED\r
+#endif\r
+\r
+#ifdef PNG_WRITE_TRANSFORMS_SUPPORTED\r
+#  ifndef PNG_NO_WRITE_SHIFT\r
+#    define PNG_WRITE_SHIFT_SUPPORTED\r
+#  endif\r
+#  ifndef PNG_NO_WRITE_PACK\r
+#    define PNG_WRITE_PACK_SUPPORTED\r
+#  endif\r
+#  ifndef PNG_NO_WRITE_BGR\r
+#    define PNG_WRITE_BGR_SUPPORTED\r
+#  endif\r
+#  ifndef PNG_NO_WRITE_SWAP\r
+#    define PNG_WRITE_SWAP_SUPPORTED\r
+#  endif\r
+#  ifndef PNG_NO_WRITE_PACKSWAP\r
+#    define PNG_WRITE_PACKSWAP_SUPPORTED\r
+#  endif\r
+#  ifndef PNG_NO_WRITE_INVERT\r
+#    define PNG_WRITE_INVERT_SUPPORTED\r
+#  endif\r
+#  ifndef PNG_NO_WRITE_FILLER\r
+#    define PNG_WRITE_FILLER_SUPPORTED   /* same as WRITE_STRIP_ALPHA */\r
+#  endif\r
+#  ifndef PNG_NO_WRITE_SWAP_ALPHA\r
+#    define PNG_WRITE_SWAP_ALPHA_SUPPORTED\r
+#  endif\r
+#  ifndef PNG_NO_WRITE_INVERT_ALPHA\r
+#    define PNG_WRITE_INVERT_ALPHA_SUPPORTED\r
+#  endif\r
+#  ifndef PNG_NO_WRITE_USER_TRANSFORM\r
+#    define PNG_WRITE_USER_TRANSFORM_SUPPORTED\r
+#  endif\r
+#endif /* PNG_WRITE_TRANSFORMS_SUPPORTED */\r
+\r
+#if !defined(PNG_NO_WRITE_INTERLACING_SUPPORTED) && \\r
+    !defined(PNG_WRITE_INTERLACING_SUPPORTED)\r
+    /* This is not required for PNG-compliant encoders, but can cause\r
+     * trouble if left undefined\r
+    */\r
+#  define PNG_WRITE_INTERLACING_SUPPORTED\r
+#endif\r
+\r
+#if !defined(PNG_NO_WRITE_WEIGHTED_FILTER) && \\r
+    !defined(PNG_WRITE_WEIGHTED_FILTER) && \\r
+     defined(PNG_FLOATING_POINT_SUPPORTED)\r
+#  define PNG_WRITE_WEIGHTED_FILTER_SUPPORTED\r
+#endif\r
+\r
+#ifndef PNG_NO_WRITE_FLUSH\r
+#  define PNG_WRITE_FLUSH_SUPPORTED\r
+#endif\r
+\r
+#if !defined(PNG_NO_SAVE_INT_32) || defined(PNG_WRITE_oFFS_SUPPORTED) || \\r
+    defined(PNG_WRITE_pCAL_SUPPORTED)\r
+#  ifndef PNG_SAVE_INT_32_SUPPORTED\r
+#    define PNG_SAVE_INT_32_SUPPORTED\r
+#  endif\r
+#endif\r
+\r
+#endif /* PNG_WRITE_SUPPORTED */\r
+\r
+#define PNG_NO_ERROR_NUMBERS\r
+\r
+#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \\r
+    defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)\r
+#  ifndef PNG_NO_USER_TRANSFORM_PTR\r
+#    define PNG_USER_TRANSFORM_PTR_SUPPORTED\r
+#  endif\r
+#endif\r
+\r
+#if defined(PNG_STDIO_SUPPORTED) && !defined(PNG_TIME_RFC1123_SUPPORTED)\r
+#  define PNG_TIME_RFC1123_SUPPORTED\r
+#endif\r
+\r
+/* This adds extra functions in pngget.c for accessing data from the\r
+ * info pointer (added in version 0.99)\r
+ * png_get_image_width()\r
+ * png_get_image_height()\r
+ * png_get_bit_depth()\r
+ * png_get_color_type()\r
+ * png_get_compression_type()\r
+ * png_get_filter_type()\r
+ * png_get_interlace_type()\r
+ * png_get_pixel_aspect_ratio()\r
+ * png_get_pixels_per_meter()\r
+ * png_get_x_offset_pixels()\r
+ * png_get_y_offset_pixels()\r
+ * png_get_x_offset_microns()\r
+ * png_get_y_offset_microns()\r
+ */\r
+#if !defined(PNG_NO_EASY_ACCESS) && !defined(PNG_EASY_ACCESS_SUPPORTED)\r
+#  define PNG_EASY_ACCESS_SUPPORTED\r
+#endif\r
+\r
+/* Added at libpng-1.2.0 */\r
+#if !defined(PNG_NO_USER_MEM) && !defined(PNG_USER_MEM_SUPPORTED)\r
+#  define PNG_USER_MEM_SUPPORTED\r
+#endif\r
+\r
+/* Added at libpng-1.2.6 */\r
+#ifndef PNG_NO_SET_USER_LIMITS\r
+#  ifndef PNG_SET_USER_LIMITS_SUPPORTED\r
+#    define PNG_SET_USER_LIMITS_SUPPORTED\r
+#  endif\r
+  /* Feature added at libpng-1.4.0, this flag added at 1.4.1 */\r
+#  ifndef PNG_SET_CHUNK_CACHE_LIMIT_SUPPORTED\r
+#    define PNG_SET_CHUNK_CACHE_LIMIT_SUPPORTED\r
+#  endif\r
+  /* Feature added at libpng-1.4.1, this flag added at 1.4.1 */\r
+#  ifndef PNG_SET_CHUNK_MALLOC_LIMIT_SUPPORTED\r
+#    define PNG_SET_CHUNK_MALLOC_LIMIT_SUPPORTED\r
+#  endif\r
+#endif\r
+\r
+/* Added at libpng-1.2.43 */\r
+#ifndef PNG_USER_LIMITS_SUPPORTED\r
+#  ifndef PNG_NO_USER_LIMITS\r
+#    define PNG_USER_LIMITS_SUPPORTED\r
+#  endif\r
+#endif\r
+\r
+/* Added at libpng-1.0.16 and 1.2.6.  To accept all valid PNGs no matter\r
+ * how large, set these two limits to 0x7fffffffL\r
+ */\r
+#ifndef PNG_USER_WIDTH_MAX\r
+#  define PNG_USER_WIDTH_MAX 1000000L\r
+#endif\r
+#ifndef PNG_USER_HEIGHT_MAX\r
+#  define PNG_USER_HEIGHT_MAX 1000000L\r
+#endif\r
+\r
+/* Added at libpng-1.2.43.  To accept all valid PNGs no matter\r
+ * how large, set these two limits to 0.\r
+ */\r
+#ifndef PNG_USER_CHUNK_CACHE_MAX\r
+#  define PNG_USER_CHUNK_CACHE_MAX 0\r
+#endif\r
+\r
+/* Added at libpng-1.2.43 */\r
+#ifndef PNG_USER_CHUNK_MALLOC_MAX\r
+#  define PNG_USER_CHUNK_MALLOC_MAX 0\r
+#endif\r
+\r
+/* Added at libpng-1.4.0 */\r
+#if !defined(PNG_NO_IO_STATE) && !defined(PNG_IO_STATE_SUPPORTED)\r
+#  define PNG_IO_STATE_SUPPORTED\r
+#endif\r
+\r
+#ifndef PNG_LITERAL_SHARP\r
+#  define PNG_LITERAL_SHARP 0x23\r
+#endif\r
+#ifndef PNG_LITERAL_LEFT_SQUARE_BRACKET\r
+#  define PNG_LITERAL_LEFT_SQUARE_BRACKET 0x5b\r
+#endif\r
+#ifndef PNG_LITERAL_RIGHT_SQUARE_BRACKET\r
+#  define PNG_LITERAL_RIGHT_SQUARE_BRACKET 0x5d\r
+#endif\r
+#ifndef PNG_STRING_NEWLINE\r
+#define PNG_STRING_NEWLINE "\n"\r
+#endif\r
+\r
+/* These are currently experimental features, define them if you want */\r
+\r
+/* Very little testing */\r
+/*\r
+#ifdef PNG_READ_SUPPORTED\r
+#  ifndef PNG_READ_16_TO_8_ACCURATE_SCALE_SUPPORTED\r
+#    define PNG_READ_16_TO_8_ACCURATE_SCALE_SUPPORTED\r
+#  endif\r
+#endif\r
+*/\r
+\r
+/* This is only for PowerPC big-endian and 680x0 systems */\r
+/* some testing */\r
+/*\r
+#ifndef PNG_READ_BIG_ENDIAN_SUPPORTED\r
+#  define PNG_READ_BIG_ENDIAN_SUPPORTED\r
+#endif\r
+*/\r
+\r
+#if !defined(PNG_NO_USE_READ_MACROS) && !defined(PNG_USE_READ_MACROS)\r
+#  define PNG_USE_READ_MACROS\r
+#endif\r
+\r
+/* Buggy compilers (e.g., gcc 2.7.2.2) need PNG_NO_POINTER_INDEXING */\r
+\r
+#if !defined(PNG_NO_POINTER_INDEXING) && \\r
+    !defined(PNG_POINTER_INDEXING_SUPPORTED)\r
+#  define PNG_POINTER_INDEXING_SUPPORTED\r
+#endif\r
+\r
+\r
+/* Any chunks you are not interested in, you can undef here.  The\r
+ * ones that allocate memory may be expecially important (hIST,\r
+ * tEXt, zTXt, tRNS, pCAL).  Others will just save time and make png_info\r
+ * a bit smaller.\r
+ */\r
+\r
+/* The size of the png_text structure changed in libpng-1.0.6 when\r
+ * iTXt support was added.  iTXt support was turned off by default through\r
+ * libpng-1.2.x, to support old apps that malloc the png_text structure\r
+ * instead of calling png_set_text() and letting libpng malloc it.  It\r
+ * was turned on by default in libpng-1.4.0.\r
+ */\r
+\r
+/* PNG_READ_ANCILLARY_CHUNKS_NOT_SUPPORTED is deprecated. */\r
+#if defined(PNG_READ_SUPPORTED) && \\r
+    !defined(PNG_READ_ANCILLARY_CHUNKS_NOT_SUPPORTED) && \\r
+    !defined(PNG_NO_READ_ANCILLARY_CHUNKS)\r
+#  define PNG_READ_ANCILLARY_CHUNKS_SUPPORTED\r
+#endif\r
+\r
+/* PNG_WRITE_ANCILLARY_CHUNKS_NOT_SUPPORTED is deprecated. */\r
+#if defined(PNG_WRITE_SUPPORTED) && \\r
+    !defined(PNG_WRITE_ANCILLARY_CHUNKS_NOT_SUPPORTED) && \\r
+    !defined(PNG_NO_WRITE_ANCILLARY_CHUNKS)\r
+#  define PNG_WRITE_ANCILLARY_CHUNKS_SUPPORTED\r
+#endif\r
+\r
+#ifdef PNG_READ_ANCILLARY_CHUNKS_SUPPORTED\r
+\r
+#ifdef PNG_NO_READ_TEXT\r
+#  define PNG_NO_READ_iTXt\r
+#  define PNG_NO_READ_tEXt\r
+#  define PNG_NO_READ_zTXt\r
+#endif\r
+\r
+#ifndef PNG_NO_READ_bKGD\r
+#  define PNG_READ_bKGD_SUPPORTED\r
+#  define PNG_bKGD_SUPPORTED\r
+#endif\r
+#ifndef PNG_NO_READ_cHRM\r
+#  define PNG_READ_cHRM_SUPPORTED\r
+#  define PNG_cHRM_SUPPORTED\r
+#endif\r
+#ifndef PNG_NO_READ_gAMA\r
+#  define PNG_READ_gAMA_SUPPORTED\r
+#  define PNG_gAMA_SUPPORTED\r
+#endif\r
+#ifndef PNG_NO_READ_hIST\r
+#  define PNG_READ_hIST_SUPPORTED\r
+#  define PNG_hIST_SUPPORTED\r
+#endif\r
+#ifndef PNG_NO_READ_iCCP\r
+#  define PNG_READ_iCCP_SUPPORTED\r
+#  define PNG_iCCP_SUPPORTED\r
+#endif\r
+#ifndef PNG_NO_READ_iTXt\r
+#  ifndef PNG_READ_iTXt_SUPPORTED\r
+#    define PNG_READ_iTXt_SUPPORTED\r
+#  endif\r
+#  ifndef PNG_iTXt_SUPPORTED\r
+#    define PNG_iTXt_SUPPORTED\r
+#  endif\r
+#endif\r
+#ifndef PNG_NO_READ_oFFs\r
+#  define PNG_READ_oFFs_SUPPORTED\r
+#  define PNG_oFFs_SUPPORTED\r
+#endif\r
+#ifndef PNG_NO_READ_pCAL\r
+#  define PNG_READ_pCAL_SUPPORTED\r
+#  define PNG_pCAL_SUPPORTED\r
+#endif\r
+#ifndef PNG_NO_READ_sCAL\r
+#  define PNG_READ_sCAL_SUPPORTED\r
+#  define PNG_sCAL_SUPPORTED\r
+#endif\r
+#ifndef PNG_NO_READ_pHYs\r
+#  define PNG_READ_pHYs_SUPPORTED\r
+#  define PNG_pHYs_SUPPORTED\r
+#endif\r
+#ifndef PNG_NO_READ_sBIT\r
+#  define PNG_READ_sBIT_SUPPORTED\r
+#  define PNG_sBIT_SUPPORTED\r
+#endif\r
+#ifndef PNG_NO_READ_sPLT\r
+#  define PNG_READ_sPLT_SUPPORTED\r
+#  define PNG_sPLT_SUPPORTED\r
+#endif\r
+#ifndef PNG_NO_READ_sRGB\r
+#  define PNG_READ_sRGB_SUPPORTED\r
+#  define PNG_sRGB_SUPPORTED\r
+#endif\r
+#ifndef PNG_NO_READ_tEXt\r
+#  define PNG_READ_tEXt_SUPPORTED\r
+#  define PNG_tEXt_SUPPORTED\r
+#endif\r
+#ifndef PNG_NO_READ_tIME\r
+#  define PNG_READ_tIME_SUPPORTED\r
+#  define PNG_tIME_SUPPORTED\r
+#endif\r
+#ifndef PNG_NO_READ_tRNS\r
+#  define PNG_READ_tRNS_SUPPORTED\r
+#  define PNG_tRNS_SUPPORTED\r
+#endif\r
+#ifndef PNG_NO_READ_zTXt\r
+#  define PNG_READ_zTXt_SUPPORTED\r
+#  define PNG_zTXt_SUPPORTED\r
+#endif\r
+#ifndef PNG_NO_READ_OPT_PLTE\r
+#  define PNG_READ_OPT_PLTE_SUPPORTED /* only affects support of the */\r
+#endif                      /* optional PLTE chunk in RGB and RGBA images */\r
+#if defined(PNG_READ_iTXt_SUPPORTED) || defined(PNG_READ_tEXt_SUPPORTED) || \\r
+    defined(PNG_READ_zTXt_SUPPORTED)\r
+#  define PNG_READ_TEXT_SUPPORTED\r
+#  define PNG_TEXT_SUPPORTED\r
+#endif\r
+\r
+#endif /* PNG_READ_ANCILLARY_CHUNKS_SUPPORTED */\r
+\r
+#ifndef PNG_NO_READ_UNKNOWN_CHUNKS\r
+#  ifndef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED\r
+#    define PNG_READ_UNKNOWN_CHUNKS_SUPPORTED\r
+#  endif\r
+#  ifndef PNG_UNKNOWN_CHUNKS_SUPPORTED\r
+#    define PNG_UNKNOWN_CHUNKS_SUPPORTED\r
+#  endif\r
+#  ifndef PNG_READ_USER_CHUNKS_SUPPORTED\r
+#    define PNG_READ_USER_CHUNKS_SUPPORTED\r
+#  endif\r
+#endif\r
+#ifndef PNG_NO_READ_USER_CHUNKS\r
+#  ifndef PNG_READ_USER_CHUNKS_SUPPORTED\r
+#    define PNG_READ_USER_CHUNKS_SUPPORTED\r
+#  endif\r
+#  ifndef PNG_USER_CHUNKS_SUPPORTED\r
+#    define PNG_USER_CHUNKS_SUPPORTED\r
+#  endif\r
+#endif\r
+#ifndef PNG_NO_HANDLE_AS_UNKNOWN\r
+#  ifndef PNG_HANDLE_AS_UNKNOWN_SUPPORTED\r
+#    define PNG_HANDLE_AS_UNKNOWN_SUPPORTED\r
+#  endif\r
+#endif\r
+\r
+#ifdef PNG_WRITE_SUPPORTED\r
+#ifdef PNG_WRITE_ANCILLARY_CHUNKS_SUPPORTED\r
+\r
+#ifdef PNG_NO_WRITE_TEXT\r
+#  define PNG_NO_WRITE_iTXt\r
+#  define PNG_NO_WRITE_tEXt\r
+#  define PNG_NO_WRITE_zTXt\r
+#endif\r
+#ifndef PNG_NO_WRITE_bKGD\r
+#  define PNG_WRITE_bKGD_SUPPORTED\r
+#  ifndef PNG_bKGD_SUPPORTED\r
+#    define PNG_bKGD_SUPPORTED\r
+#  endif\r
+#endif\r
+#ifndef PNG_NO_WRITE_cHRM\r
+#  define PNG_WRITE_cHRM_SUPPORTED\r
+#  ifndef PNG_cHRM_SUPPORTED\r
+#    define PNG_cHRM_SUPPORTED\r
+#  endif\r
+#endif\r
+#ifndef PNG_NO_WRITE_gAMA\r
+#  define PNG_WRITE_gAMA_SUPPORTED\r
+#  ifndef PNG_gAMA_SUPPORTED\r
+#    define PNG_gAMA_SUPPORTED\r
+#  endif\r
+#endif\r
+#ifndef PNG_NO_WRITE_hIST\r
+#  define PNG_WRITE_hIST_SUPPORTED\r
+#  ifndef PNG_hIST_SUPPORTED\r
+#    define PNG_hIST_SUPPORTED\r
+#  endif\r
+#endif\r
+#ifndef PNG_NO_WRITE_iCCP\r
+#  define PNG_WRITE_iCCP_SUPPORTED\r
+#  ifndef PNG_iCCP_SUPPORTED\r
+#    define PNG_iCCP_SUPPORTED\r
+#  endif\r
+#endif\r
+#ifndef PNG_NO_WRITE_iTXt\r
+#  ifndef PNG_WRITE_iTXt_SUPPORTED\r
+#    define PNG_WRITE_iTXt_SUPPORTED\r
+#  endif\r
+#  ifndef PNG_iTXt_SUPPORTED\r
+#    define PNG_iTXt_SUPPORTED\r
+#  endif\r
+#endif\r
+#ifndef PNG_NO_WRITE_oFFs\r
+#  define PNG_WRITE_oFFs_SUPPORTED\r
+#  ifndef PNG_oFFs_SUPPORTED\r
+#    define PNG_oFFs_SUPPORTED\r
+#  endif\r
+#endif\r
+#ifndef PNG_NO_WRITE_pCAL\r
+#  define PNG_WRITE_pCAL_SUPPORTED\r
+#  ifndef PNG_pCAL_SUPPORTED\r
+#    define PNG_pCAL_SUPPORTED\r
+#  endif\r
+#endif\r
+#ifndef PNG_NO_WRITE_sCAL\r
+#  define PNG_WRITE_sCAL_SUPPORTED\r
+#  ifndef PNG_sCAL_SUPPORTED\r
+#    define PNG_sCAL_SUPPORTED\r
+#  endif\r
+#endif\r
+#ifndef PNG_NO_WRITE_pHYs\r
+#  define PNG_WRITE_pHYs_SUPPORTED\r
+#  ifndef PNG_pHYs_SUPPORTED\r
+#    define PNG_pHYs_SUPPORTED\r
+#  endif\r
+#endif\r
+#ifndef PNG_NO_WRITE_sBIT\r
+#  define PNG_WRITE_sBIT_SUPPORTED\r
+#  ifndef PNG_sBIT_SUPPORTED\r
+#    define PNG_sBIT_SUPPORTED\r
+#  endif\r
+#endif\r
+#ifndef PNG_NO_WRITE_sPLT\r
+#  define PNG_WRITE_sPLT_SUPPORTED\r
+#  ifndef PNG_sPLT_SUPPORTED\r
+#    define PNG_sPLT_SUPPORTED\r
+#  endif\r
+#endif\r
+#ifndef PNG_NO_WRITE_sRGB\r
+#  define PNG_WRITE_sRGB_SUPPORTED\r
+#  ifndef PNG_sRGB_SUPPORTED\r
+#    define PNG_sRGB_SUPPORTED\r
+#  endif\r
+#endif\r
+#ifndef PNG_NO_WRITE_tEXt\r
+#  define PNG_WRITE_tEXt_SUPPORTED\r
+#  ifndef PNG_tEXt_SUPPORTED\r
+#    define PNG_tEXt_SUPPORTED\r
+#  endif\r
+#endif\r
+#ifndef PNG_NO_WRITE_tIME\r
+#  define PNG_WRITE_tIME_SUPPORTED\r
+#  ifndef PNG_tIME_SUPPORTED\r
+#    define PNG_tIME_SUPPORTED\r
+#  endif\r
+#endif\r
+#ifndef PNG_NO_WRITE_tRNS\r
+#  define PNG_WRITE_tRNS_SUPPORTED\r
+#  ifndef PNG_tRNS_SUPPORTED\r
+#    define PNG_tRNS_SUPPORTED\r
+#  endif\r
+#endif\r
+#ifndef PNG_NO_WRITE_zTXt\r
+#  define PNG_WRITE_zTXt_SUPPORTED\r
+#  ifndef PNG_zTXt_SUPPORTED\r
+#    define PNG_zTXt_SUPPORTED\r
+#  endif\r
+#endif\r
+#if defined(PNG_WRITE_iTXt_SUPPORTED) || defined(PNG_WRITE_tEXt_SUPPORTED) || \\r
+    defined(PNG_WRITE_zTXt_SUPPORTED)\r
+#  define PNG_WRITE_TEXT_SUPPORTED\r
+#  ifndef PNG_TEXT_SUPPORTED\r
+#    define PNG_TEXT_SUPPORTED\r
+#  endif\r
+#endif\r
+\r
+#ifdef PNG_WRITE_tIME_SUPPORTED\r
+#  ifndef PNG_NO_CONVERT_tIME\r
+#    ifndef _WIN32_WCE\r
+/*   The "tm" structure is not supported on WindowsCE */\r
+#      ifndef PNG_CONVERT_tIME_SUPPORTED\r
+#        define PNG_CONVERT_tIME_SUPPORTED\r
+#      endif\r
+#   endif\r
+#  endif\r
+#endif\r
+\r
+#endif /* PNG_WRITE_ANCILLARY_CHUNKS_SUPPORTED */\r
+\r
+#ifndef PNG_NO_WRITE_FILTER\r
+#  ifndef PNG_WRITE_FILTER_SUPPORTED\r
+#    define PNG_WRITE_FILTER_SUPPORTED\r
+#  endif\r
+#endif\r
+\r
+#ifndef PNG_NO_WRITE_UNKNOWN_CHUNKS\r
+#  define PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED\r
+#  ifndef PNG_UNKNOWN_CHUNKS_SUPPORTED\r
+#    define PNG_UNKNOWN_CHUNKS_SUPPORTED\r
+#  endif\r
+#endif\r
+#ifndef PNG_NO_HANDLE_AS_UNKNOWN\r
+#  ifndef PNG_HANDLE_AS_UNKNOWN_SUPPORTED\r
+#    define PNG_HANDLE_AS_UNKNOWN_SUPPORTED\r
+#  endif\r
+#endif\r
+#endif /* PNG_WRITE_SUPPORTED */\r
+\r
+/* Turn this off to disable png_read_png() and\r
+ * png_write_png() and leave the row_pointers member\r
+ * out of the info structure.\r
+ */\r
+#ifndef PNG_NO_INFO_IMAGE\r
+#  define PNG_INFO_IMAGE_SUPPORTED\r
+#endif\r
+\r
+/* Need the time information for converting tIME chunks */\r
+#ifdef PNG_CONVERT_tIME_SUPPORTED\r
+     /* "time.h" functions are not supported on WindowsCE */\r
+#    include <time.h>\r
+#endif\r
+\r
+/* Some typedefs to get us started.  These should be safe on most of the\r
+ * common platforms.  The typedefs should be at least as large as the\r
+ * numbers suggest (a png_uint_32 must be at least 32 bits long), but they\r
+ * don't have to be exactly that size.  Some compilers dislike passing\r
+ * unsigned shorts as function parameters, so you may be better off using\r
+ * unsigned int for png_uint_16.\r
+ */\r
+\r
+#if defined(INT_MAX) && (INT_MAX > 0x7ffffffeL)\r
+typedef unsigned int png_uint_32;\r
+typedef int png_int_32;\r
+#else\r
+typedef unsigned long png_uint_32;\r
+typedef long png_int_32;\r
+#endif\r
+typedef unsigned short png_uint_16;\r
+typedef short png_int_16;\r
+typedef unsigned char png_byte;\r
+\r
+#ifdef PNG_NO_SIZE_T\r
+   typedef unsigned int png_size_t;\r
+#else\r
+   typedef size_t png_size_t;\r
+#endif\r
+#define png_sizeof(x) sizeof(x)\r
+\r
+/* The following is needed for medium model support.  It cannot be in the\r
+ * pngpriv.h header.  Needs modification for other compilers besides\r
+ * MSC.  Model independent support declares all arrays and pointers to be\r
+ * large using the far keyword.  The zlib version used must also support\r
+ * model independent data.  As of version zlib 1.0.4, the necessary changes\r
+ * have been made in zlib.  The USE_FAR_KEYWORD define triggers other\r
+ * changes that are needed. (Tim Wegner)\r
+ */\r
+\r
+/* Separate compiler dependencies (problem here is that zlib.h always\r
+ * defines FAR. (SJT)\r
+ */\r
+#ifdef __BORLANDC__\r
+#  if defined(__LARGE__) || defined(__HUGE__) || defined(__COMPACT__)\r
+#    define LDATA 1\r
+#  else\r
+#    define LDATA 0\r
+#  endif\r
+   /* GRR:  why is Cygwin in here?  Cygwin is not Borland C... */\r
+#  if !defined(__WIN32__) && !defined(__FLAT__) && !defined(__CYGWIN__)\r
+#    define PNG_MAX_MALLOC_64K\r
+#    if (LDATA != 1)\r
+#      ifndef FAR\r
+#        define FAR __far\r
+#      endif\r
+#      define USE_FAR_KEYWORD\r
+#    endif   /* LDATA != 1 */\r
+     /* Possibly useful for moving data out of default segment.\r
+      * Uncomment it if you want. Could also define FARDATA as\r
+      * const if your compiler supports it. (SJT)\r
+#    define FARDATA FAR\r
+      */\r
+#  endif  /* __WIN32__, __FLAT__, __CYGWIN__ */\r
+#endif   /* __BORLANDC__ */\r
+\r
+\r
+/* Suggest testing for specific compiler first before testing for\r
+ * FAR.  The Watcom compiler defines both __MEDIUM__ and M_I86MM,\r
+ * making reliance oncertain keywords suspect. (SJT)\r
+ */\r
+\r
+/* MSC Medium model */\r
+#ifdef FAR\r
+#  ifdef M_I86MM\r
+#    define USE_FAR_KEYWORD\r
+#    define FARDATA FAR\r
+#    include <dos.h>\r
+#  endif\r
+#endif\r
+\r
+/* SJT: default case */\r
+#ifndef FAR\r
+#  define FAR\r
+#endif\r
+\r
+/* At this point FAR is always defined */\r
+#ifndef FARDATA\r
+#  define FARDATA\r
+#endif\r
+\r
+/* Typedef for floating-point numbers that are converted\r
+   to fixed-point with a multiple of 100,000, e.g., int_gamma */\r
+typedef png_int_32 png_fixed_point;\r
+\r
+/* Add typedefs for pointers */\r
+typedef void            FAR * png_voidp;\r
+typedef png_byte        FAR * png_bytep;\r
+typedef png_uint_32     FAR * png_uint_32p;\r
+typedef png_int_32      FAR * png_int_32p;\r
+typedef png_uint_16     FAR * png_uint_16p;\r
+typedef png_int_16      FAR * png_int_16p;\r
+typedef PNG_CONST char  FAR * png_const_charp;\r
+typedef char            FAR * png_charp;\r
+typedef png_fixed_point FAR * png_fixed_point_p;\r
+\r
+#ifndef PNG_NO_STDIO\r
+typedef FILE                * png_FILE_p;\r
+#endif\r
+\r
+#ifdef PNG_FLOATING_POINT_SUPPORTED\r
+typedef double          FAR * png_doublep;\r
+#endif\r
+\r
+/* Pointers to pointers; i.e. arrays */\r
+typedef png_byte        FAR * FAR * png_bytepp;\r
+typedef png_uint_32     FAR * FAR * png_uint_32pp;\r
+typedef png_int_32      FAR * FAR * png_int_32pp;\r
+typedef png_uint_16     FAR * FAR * png_uint_16pp;\r
+typedef png_int_16      FAR * FAR * png_int_16pp;\r
+typedef PNG_CONST char  FAR * FAR * png_const_charpp;\r
+typedef char            FAR * FAR * png_charpp;\r
+typedef png_fixed_point FAR * FAR * png_fixed_point_pp;\r
+#ifdef PNG_FLOATING_POINT_SUPPORTED\r
+typedef double          FAR * FAR * png_doublepp;\r
+#endif\r
+\r
+/* Pointers to pointers to pointers; i.e., pointer to array */\r
+typedef char            FAR * FAR * FAR * png_charppp;\r
+\r
+/* Define PNG_BUILD_DLL if the module being built is a Windows\r
+ * LIBPNG DLL.\r
+ *\r
+ * Define PNG_USE_DLL if you want to *link* to the Windows LIBPNG DLL.\r
+ * It is equivalent to Microsoft predefined macro _DLL that is\r
+ * automatically defined when you compile using the share\r
+ * version of the CRT (C Run-Time library)\r
+ *\r
+ * The cygwin mods make this behavior a little different:\r
+ * Define PNG_BUILD_DLL if you are building a dll for use with cygwin\r
+ * Define PNG_STATIC if you are building a static library for use with cygwin,\r
+ *   -or- if you are building an application that you want to link to the\r
+ *   static library.\r
+ * PNG_USE_DLL is defined by default (no user action needed) unless one of\r
+ *   the other flags is defined.\r
+ */\r
+\r
+#if !defined(PNG_DLL) && (defined(PNG_BUILD_DLL) || defined(PNG_USE_DLL))\r
+#  define PNG_DLL\r
+#endif\r
+\r
+#ifdef __CYGWIN__\r
+#  undef PNGAPI\r
+#  define PNGAPI __cdecl\r
+#  undef PNG_IMPEXP\r
+#  define PNG_IMPEXP\r
+#endif\r
+\r
+#define PNG_USE_LOCAL_ARRAYS /* Not used in libpng, defined for legacy apps */\r
+\r
+/* If you define PNGAPI, e.g., with compiler option "-DPNGAPI=__stdcall",\r
+ * you may get warnings regarding the linkage of png_zalloc and png_zfree.\r
+ * Don't ignore those warnings; you must also reset the default calling\r
+ * convention in your compiler to match your PNGAPI, and you must build\r
+ * zlib and your applications the same way you build libpng.\r
+ */\r
+\r
+#if defined(__MINGW32__) && !defined(PNG_MODULEDEF)\r
+#  ifndef PNG_NO_MODULEDEF\r
+#    define PNG_NO_MODULEDEF\r
+#  endif\r
+#endif\r
+\r
+#if !defined(PNG_IMPEXP) && defined(PNG_BUILD_DLL) && !defined(PNG_NO_MODULEDEF)\r
+#  define PNG_IMPEXP\r
+#endif\r
+\r
+#if defined(PNG_DLL) || defined(_DLL) || defined(__DLL__ ) || \\r
+    (( defined(_Windows) || defined(_WINDOWS) || \\r
+       defined(WIN32) || defined(_WIN32) || defined(__WIN32__) ))\r
+\r
+#  ifndef PNGAPI\r
+#     if defined(__GNUC__) || (defined (_MSC_VER) && (_MSC_VER >= 800))\r
+#        define PNGAPI __cdecl\r
+#     else\r
+#        define PNGAPI _cdecl\r
+#     endif\r
+#  endif\r
+\r
+#  if !defined(PNG_IMPEXP) && (!defined(PNG_DLL) || \\r
+       0 /* WINCOMPILER_WITH_NO_SUPPORT_FOR_DECLIMPEXP */)\r
+#     define PNG_IMPEXP\r
+#  endif\r
+\r
+#  ifndef PNG_IMPEXP\r
+\r
+#    define PNG_EXPORT_TYPE1(type,symbol)  PNG_IMPEXP type PNGAPI symbol\r
+#    define PNG_EXPORT_TYPE2(type,symbol)  type PNG_IMPEXP PNGAPI symbol\r
+\r
+     /* Borland/Microsoft */\r
+#    if defined(_MSC_VER) || defined(__BORLANDC__)\r
+#      if (_MSC_VER >= 800) || (__BORLANDC__ >= 0x500)\r
+#         define PNG_EXPORT PNG_EXPORT_TYPE1\r
+#      else\r
+#         define PNG_EXPORT PNG_EXPORT_TYPE2\r
+#         ifdef PNG_BUILD_DLL\r
+#            define PNG_IMPEXP __export\r
+#         else\r
+#            define PNG_IMPEXP /*__import */ /* doesn't exist AFAIK in VC++ */\r
+#         endif                              /* Exists in Borland C++ for\r
+                                                C++ classes (== huge) */\r
+#      endif\r
+#    endif\r
+\r
+#    ifndef PNG_IMPEXP\r
+#      ifdef PNG_BUILD_DLL\r
+#        define PNG_IMPEXP __declspec(dllexport)\r
+#      else\r
+#        define PNG_IMPEXP __declspec(dllimport)\r
+#      endif\r
+#    endif\r
+#  endif  /* PNG_IMPEXP */\r
+#else /* !(DLL || non-cygwin WINDOWS) */\r
+#   if (defined(__IBMC__) || defined(__IBMCPP__)) && defined(__OS2__)\r
+#     ifndef PNGAPI\r
+#       define PNGAPI _System\r
+#     endif\r
+#   else\r
+#     if 0 /* ... other platforms, with other meanings */\r
+#     endif\r
+#   endif\r
+#endif\r
+\r
+#ifndef PNGAPI\r
+#  define PNGAPI\r
+#endif\r
+#ifndef PNG_IMPEXP\r
+#  define PNG_IMPEXP\r
+#endif\r
+\r
+#ifdef PNG_BUILDSYMS\r
+#  ifndef PNG_EXPORT\r
+#    define PNG_EXPORT(type,symbol) PNG_FUNCTION_EXPORT symbol END\r
+#  endif\r
+#endif\r
+\r
+#ifndef PNG_EXPORT\r
+#  define PNG_EXPORT(type,symbol) PNG_IMPEXP type PNGAPI symbol\r
+#endif\r
+\r
+/* Support for compiler specific function attributes.  These are used\r
+ * so that where compiler support is available incorrect use of API\r
+ * functions in png.h will generate compiler warnings.\r
+ *\r
+ * Added at libpng-1.2.41.\r
+ */\r
+\r
+#ifndef PNG_NO_PEDANTIC_WARNINGS\r
+#  ifndef PNG_PEDANTIC_WARNINGS_SUPPORTED\r
+#    define PNG_PEDANTIC_WARNINGS_SUPPORTED\r
+#  endif\r
+#endif\r
+\r
+#ifdef PNG_PEDANTIC_WARNINGS_SUPPORTED\r
+/* Support for compiler specific function attributes.  These are used\r
+ * so that where compiler support is available incorrect use of API\r
+ * functions in png.h will generate compiler warnings.  Added at libpng\r
+ * version 1.2.41.\r
+ */\r
+#  ifdef __GNUC__\r
+#    ifndef PNG_USE_RESULT\r
+#      define PNG_USE_RESULT __attribute__((__warn_unused_result__))\r
+#    endif\r
+#    ifndef PNG_NORETURN\r
+#      define PNG_NORETURN   __attribute__((__noreturn__))\r
+#    endif\r
+#    ifndef PNG_ALLOCATED\r
+#      define PNG_ALLOCATED  __attribute__((__malloc__))\r
+#    endif\r
+\r
+    /* This specifically protects structure members that should only be\r
+     * accessed from within the library, therefore should be empty during\r
+     * a library build.\r
+     */\r
+#    ifndef PNG_DEPRECATED\r
+#      define PNG_DEPRECATED __attribute__((__deprecated__))\r
+#    endif\r
+#    ifndef PNG_DEPSTRUCT\r
+#      define PNG_DEPSTRUCT  __attribute__((__deprecated__))\r
+#    endif\r
+#    ifndef PNG_PRIVATE\r
+#      if 0 /* Doesn't work so we use deprecated instead*/\r
+#        define PNG_PRIVATE \\r
+          __attribute__((warning("This function is not exported by libpng.")))\r
+#      else\r
+#        define PNG_PRIVATE \\r
+          __attribute__((__deprecated__))\r
+#      endif\r
+#    endif /* PNG_PRIVATE */\r
+#  endif /* __GNUC__ */\r
+#endif /* PNG_PEDANTIC_WARNINGS */\r
+\r
+#ifndef PNG_DEPRECATED\r
+#  define PNG_DEPRECATED  /* Use of this function is deprecated */\r
+#endif\r
+#ifndef PNG_USE_RESULT\r
+#  define PNG_USE_RESULT  /* The result of this function must be checked */\r
+#endif\r
+#ifndef PNG_NORETURN\r
+#  define PNG_NORETURN    /* This function does not return */\r
+#endif\r
+#ifndef PNG_ALLOCATED\r
+#  define PNG_ALLOCATED   /* The result of the function is new memory */\r
+#endif\r
+#ifndef PNG_DEPSTRUCT\r
+#  define PNG_DEPSTRUCT   /* Access to this struct member is deprecated */\r
+#endif\r
+#ifndef PNG_PRIVATE\r
+#  define PNG_PRIVATE     /* This is a private libpng function */\r
+#endif\r
+\r
+/* Users may want to use these so they are not private.  Any library\r
+ * functions that are passed far data must be model-independent.\r
+ */\r
+\r
+/* memory model/platform independent fns */\r
+#ifndef PNG_ABORT\r
+#  ifdef _WINDOWS_\r
+#     define PNG_ABORT() ExitProcess(0)\r
+#  else\r
+#     define PNG_ABORT() abort()\r
+#  endif\r
+#endif\r
+\r
+#ifdef USE_FAR_KEYWORD\r
+/* Use this to make far-to-near assignments */\r
+#  define CHECK   1\r
+#  define NOCHECK 0\r
+#  define CVT_PTR(ptr) (png_far_to_near(png_ptr,ptr,CHECK))\r
+#  define CVT_PTR_NOCHECK(ptr) (png_far_to_near(png_ptr,ptr,NOCHECK))\r
+#  define png_strcpy  _fstrcpy\r
+#  define png_strncpy _fstrncpy   /* Added to v 1.2.6 */\r
+#  define png_strlen  _fstrlen\r
+#  define png_memcmp  _fmemcmp    /* SJT: added */\r
+#  define png_memcpy  _fmemcpy\r
+#  define png_memset  _fmemset\r
+#  define png_sprintf sprintf\r
+#else\r
+#  ifdef _WINDOWS_  /* Favor Windows over C runtime fns */\r
+#    define CVT_PTR(ptr)         (ptr)\r
+#    define CVT_PTR_NOCHECK(ptr) (ptr)\r
+#    define png_strcpy  lstrcpyA\r
+#    define png_strncpy lstrcpynA\r
+#    define png_strlen  lstrlenA\r
+#    define png_memcmp  memcmp\r
+#    define png_memcpy  CopyMemory\r
+#    define png_memset  memset\r
+#    define png_sprintf wsprintfA\r
+#  else\r
+#    define CVT_PTR(ptr)         (ptr)\r
+#    define CVT_PTR_NOCHECK(ptr) (ptr)\r
+#    define png_strcpy  strcpy\r
+#    define png_strncpy strncpy     /* Added to v 1.2.6 */\r
+#    define png_strlen  strlen\r
+#    define png_memcmp  memcmp      /* SJT: added */\r
+#    define png_memcpy  memcpy\r
+#    define png_memset  memset\r
+#    define png_sprintf sprintf\r
+#    ifndef PNG_NO_SNPRINTF\r
+#      ifdef _MSC_VER\r
+#        define png_snprintf _snprintf   /* Added to v 1.2.19 */\r
+#        define png_snprintf2 _snprintf\r
+#        define png_snprintf6 _snprintf\r
+#      else\r
+#        define png_snprintf snprintf   /* Added to v 1.2.19 */\r
+#        define png_snprintf2 snprintf\r
+#        define png_snprintf6 snprintf\r
+#      endif\r
+#    else\r
+       /* You don't have or don't want to use snprintf().  Caution: Using\r
+        * sprintf instead of snprintf exposes your application to accidental\r
+        * or malevolent buffer overflows.  If you don't have snprintf()\r
+        * as a general rule you should provide one (you can get one from\r
+        * Portable OpenSSH).\r
+        */\r
+#      define png_snprintf(s1,n,fmt,x1) sprintf(s1,fmt,x1)\r
+#      define png_snprintf2(s1,n,fmt,x1,x2) sprintf(s1,fmt,x1,x2)\r
+#      define png_snprintf6(s1,n,fmt,x1,x2,x3,x4,x5,x6) \\r
+          sprintf(s1,fmt,x1,x2,x3,x4,x5,x6)\r
+#    endif\r
+#  endif\r
+#endif\r
+\r
+/* png_alloc_size_t is guaranteed to be no smaller than png_size_t,\r
+ * and no smaller than png_uint_32.  Casts from png_size_t or png_uint_32\r
+ * to png_alloc_size_t are not necessary; in fact, it is recommended\r
+ * not to use them at all so that the compiler can complain when something\r
+ * turns out to be problematic.\r
+ * Casts in the other direction (from png_alloc_size_t to png_size_t or\r
+ * png_uint_32) should be explicitly applied; however, we do not expect\r
+ * to encounter practical situations that require such conversions.\r
+ */\r
+#if defined(__TURBOC__) && !defined(__FLAT__)\r
+#  define  png_mem_alloc farmalloc\r
+#  define  png_mem_free  farfree\r
+   typedef unsigned long png_alloc_size_t;\r
+#else\r
+#  if defined(_MSC_VER) && defined(MAXSEG_64K)\r
+#    define  png_mem_alloc(s) halloc(s, 1)\r
+#    define  png_mem_free     hfree\r
+     typedef unsigned long    png_alloc_size_t;\r
+#  else\r
+#    if defined(_WINDOWS_) && (!defined(INT_MAX) || INT_MAX <= 0x7ffffffeL)\r
+#      define  png_mem_alloc(s) HeapAlloc(GetProcessHeap(), 0, s)\r
+#      define  png_mem_free(p)  HeapFree(GetProcessHeap(), 0, p)\r
+       typedef DWORD            png_alloc_size_t;\r
+#    else\r
+#      define  png_mem_alloc malloc\r
+#      define  png_mem_free  free\r
+       typedef png_size_t    png_alloc_size_t;\r
+#    endif\r
+#  endif\r
+#endif\r
+/* End of memory model/platform independent support */\r
+\r
+/* Just a little check that someone hasn't tried to define something\r
+ * contradictory.\r
+ */\r
+#if (PNG_ZBUF_SIZE > 65536L) && defined(PNG_MAX_MALLOC_64K)\r
+#  undef PNG_ZBUF_SIZE\r
+#  define PNG_ZBUF_SIZE 65536L\r
+#endif\r
+\r
+\r
+/* Added at libpng-1.2.8 */\r
+#endif /* PNG_VERSION_INFO_ONLY */\r
+\r
+#endif /* PNGCONF_H */\r
index c7c3a92..0d4ab9f 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: tiff.h,v 1.3 2005-06-17 13:57:03 vp153 Exp $ */
+/* $Id: tiff.h,v 1.43.2.1 2010-06-08 18:50:43 bfriesen Exp $ */
 
 /*
  * Copyright (c) 1988-1997 Sam Leffler
@@ -26,6 +26,9 @@
 
 #ifndef _TIFF_
 #define        _TIFF_
+
+#include "tiffconf.h"
+
 /*
  * Tag Image File Format (TIFF)
  *
  *    (http://partners.adobe.com/asn/developer/PDFS/TN/TIFF6.pdf)
  *
  * For Big TIFF design notes see the following link
- *    http://gdal.maptools.org/twiki/bin/view/libtiff/BigTIFFDesign
+ *    http://www.remotesensing.org/libtiff/bigtiffdesign.html
  */
 #define        TIFF_VERSION            42
 #define TIFF_BIGTIFF_VERSION    43
 
 #define        TIFF_BIGENDIAN          0x4d4d
 #define        TIFF_LITTLEENDIAN       0x4949
-
-/*
- * The so called TIFF types conflict with definitions from inttypes.h 
- * included from sys/types.h on AIX (at least using VisualAge compiler). 
- * We try to work around this by detecting this case.  Defining 
- * _TIFF_DATA_TYPEDEFS_ short circuits the later definitions in tiff.h, and
- * we will in the holes not provided for by inttypes.h. 
- *
- * See http://bugzilla.remotesensing.org/show_bug.cgi?id=39
- */
-#if defined(_H_INTTYPES) && defined(_ALL_SOURCE) && defined(USING_VISUALAGE)
-
-#define _TIFF_DATA_TYPEDEFS_
-typedef unsigned char uint8;
-typedef unsigned short uint16;
-typedef unsigned int uint32;
-
-#endif
-
+#define        MDI_LITTLEENDIAN        0x5045
+#define        MDI_BIGENDIAN           0x4550
 /*
  * Intrinsic data types required by the file format:
  *
@@ -74,23 +60,27 @@ typedef unsigned int uint32;
  * 32-bit quantities   int32/uint32
  * strings             unsigned char*
  */
-#ifndef _TIFF_DATA_TYPEDEFS_
-#define _TIFF_DATA_TYPEDEFS_
 
+#ifndef HAVE_INT8
 typedef        signed char int8;       /* NB: non-ANSI compilers may not grok */
+#endif
 typedef        unsigned char uint8;
+#ifndef HAVE_INT16
 typedef        short int16;
+#endif
 typedef        unsigned short uint16;  /* sizeof (uint16) must == 2 */
-#if defined(__alpha) || (defined(_MIPS_SZLONG) && _MIPS_SZLONG == 64) || defined(__LP64__) || defined(__arch64__)
+#if SIZEOF_INT == 4
+#ifndef HAVE_INT32
 typedef        int int32;
+#endif
 typedef        unsigned int uint32;    /* sizeof (uint32) must == 4 */
-#else
+#elif SIZEOF_LONG == 4
+#ifndef HAVE_INT32
 typedef        long int32;
+#endif
 typedef        unsigned long uint32;   /* sizeof (uint32) must == 4 */
 #endif
 
-#endif /* _TIFF_DATA_TYPEDEFS_ */
-
 /* For TIFFReassignTagToIgnore */
 enum TIFFIgnoreSense /* IGNORE tag table */
 {
@@ -289,6 +279,9 @@ typedef     enum {
 #define        TIFFTAG_ARTIST                  315     /* creator of image */
 #define        TIFFTAG_HOSTCOMPUTER            316     /* machine where created */
 #define        TIFFTAG_PREDICTOR               317     /* prediction scheme w/ LZW */
+#define     PREDICTOR_NONE             1       /* no prediction scheme used */
+#define     PREDICTOR_HORIZONTAL       2       /* horizontal differencing */
+#define     PREDICTOR_FLOATINGPOINT    3       /* floating point predictor */
 #define        TIFFTAG_WHITEPOINT              318     /* image white point */
 #define        TIFFTAG_PRIMARYCHROMATICITIES   319     /* !primary chromaticities */
 #define        TIFFTAG_COLORMAP                320     /* RGB map for pallette image */
@@ -415,12 +408,15 @@ typedef   enum {
 #define TIFFTAG_IT8CMYKEQUIVALENT      34032   /* CMYK color equivalents */
 /* tags 34232-34236 are private tags registered to Texas Instruments */
 #define TIFFTAG_FRAMECOUNT              34232   /* Sequence Frame Count */
-/* tag 34750 is a private tag registered to Adobe? */
-#define TIFFTAG_ICCPROFILE             34675   /* ICC profile data */
 /* tag 34377 is private tag registered to Adobe for PhotoShop */
 #define TIFFTAG_PHOTOSHOP              34377 
+/* tags 34665, 34853 and 40965 are documented in EXIF specification */
+#define TIFFTAG_EXIFIFD                        34665   /* Pointer to EXIF private directory */
+/* tag 34750 is a private tag registered to Adobe? */
+#define TIFFTAG_ICCPROFILE             34675   /* ICC profile data */
 /* tag 34750 is a private tag registered to Pixel Magic */
 #define        TIFFTAG_JBIGOPTIONS             34750   /* JBIG options */
+#define TIFFTAG_GPSIFD                 34853   /* Pointer to GPS private directory */
 /* tags 34908-34914 are private tags registered to SGI */
 #define        TIFFTAG_FAXRECVPARAMS           34908   /* encoded Class 2 ses. parms */
 #define        TIFFTAG_FAXSUBADDRESS           34909   /* received SubAddr string */
@@ -430,9 +426,8 @@ typedef     enum {
 #define TIFFTAG_STONITS                        37439   /* Sample value to Nits */
 /* tag 34929 is a private tag registered to FedEx */
 #define        TIFFTAG_FEDEX_EDR               34929   /* unknown use */
-/* tag 65535 is an undefined tag used by Eastman Kodak */
-#define TIFFTAG_DCSHUESHIFTVALUES       65535   /* hue shift correction data */
-/* Adobe Digital Negative format tags */
+#define TIFFTAG_INTEROPERABILITYIFD    40965   /* Pointer to Interoperability private directory */
+/* Adobe Digital Negative (DNG) format tags */
 #define TIFFTAG_DNGVERSION             50706   /* &DNG version number */
 #define TIFFTAG_DNGBACKWARDVERSION     50707   /* &DNG compatibility version */
 #define TIFFTAG_UNIQUECAMERAMODEL      50708   /* &name for the camera model */
@@ -485,9 +480,11 @@ typedef    enum {
                                                   in the red/green rows */
 #define TIFFTAG_LINEARRESPONSELIMIT    50734   /* &non-linear encoding range */
 #define TIFFTAG_CAMERASERIALNUMBER     50735   /* &camera's serial number */
+#define TIFFTAG_LENSINFO               50736   /* info about the lens */
 #define TIFFTAG_CHROMABLURRADIUS       50737   /* &chroma blur radius */
 #define TIFFTAG_ANTIALIASSTRENGTH      50738   /* &relative strength of the
                                                   camera's anti-alias filter */
+#define TIFFTAG_SHADOWSCALE            50739   /* &used by Adobe Camera Raw */
 #define TIFFTAG_DNGPRIVATEDATA         50740   /* &manufacturer's private data */
 #define TIFFTAG_MAKERNOTESAFETY                50741   /* &whether the EXIF MakerNote
                                                   tag is safe to preserve
@@ -496,6 +493,23 @@ typedef    enum {
 #define        TIFFTAG_CALIBRATIONILLUMINANT1  50778   /* &illuminant 1 */
 #define TIFFTAG_CALIBRATIONILLUMINANT2 50779   /* &illuminant 2 */
 #define TIFFTAG_BESTQUALITYSCALE       50780   /* &best quality multiplier */
+#define TIFFTAG_RAWDATAUNIQUEID                50781   /* &unique identifier for
+                                                  the raw image data */
+#define TIFFTAG_ORIGINALRAWFILENAME    50827   /* &file name of the original
+                                                  raw file */
+#define TIFFTAG_ORIGINALRAWFILEDATA    50828   /* &contents of the original
+                                                  raw file */
+#define TIFFTAG_ACTIVEAREA             50829   /* &active (non-masked) pixels
+                                                  of the sensor */
+#define TIFFTAG_MASKEDAREAS            50830   /* &list of coordinates
+                                                  of fully masked pixels */
+#define TIFFTAG_ASSHOTICCPROFILE       50831   /* &these two tags used to */
+#define TIFFTAG_ASSHOTPREPROFILEMATRIX 50832   /* map cameras's color space
+                                                  into ICC profile space */
+#define TIFFTAG_CURRENTICCPROFILE      50833   /* & */
+#define TIFFTAG_CURRENTPREPROFILEMATRIX        50834   /* & */
+/* tag 65535 is an undefined tag used by Eastman Kodak */
+#define TIFFTAG_DCSHUESHIFTVALUES       65535   /* hue shift correction data */
 
 /*
  * The following are ``pseudo tags'' that can be used to control
@@ -562,6 +576,79 @@ typedef    enum {
 #define TIFFTAG_SGILOGENCODE           65561 /* SGILog data encoding control*/
 #define     SGILOGENCODE_NODITHER      0     /* do not dither encoded values*/
 #define     SGILOGENCODE_RANDITHER     1     /* randomly dither encd values */
+
+/*
+ * EXIF tags
+ */
+#define EXIFTAG_EXPOSURETIME           33434   /* Exposure time */
+#define EXIFTAG_FNUMBER                        33437   /* F number */
+#define EXIFTAG_EXPOSUREPROGRAM                34850   /* Exposure program */
+#define EXIFTAG_SPECTRALSENSITIVITY    34852   /* Spectral sensitivity */
+#define EXIFTAG_ISOSPEEDRATINGS                34855   /* ISO speed rating */
+#define EXIFTAG_OECF                   34856   /* Optoelectric conversion
+                                                  factor */
+#define EXIFTAG_EXIFVERSION            36864   /* Exif version */
+#define EXIFTAG_DATETIMEORIGINAL       36867   /* Date and time of original
+                                                  data generation */
+#define EXIFTAG_DATETIMEDIGITIZED      36868   /* Date and time of digital
+                                                  data generation */
+#define EXIFTAG_COMPONENTSCONFIGURATION        37121   /* Meaning of each component */
+#define EXIFTAG_COMPRESSEDBITSPERPIXEL 37122   /* Image compression mode */
+#define EXIFTAG_SHUTTERSPEEDVALUE      37377   /* Shutter speed */
+#define EXIFTAG_APERTUREVALUE          37378   /* Aperture */
+#define EXIFTAG_BRIGHTNESSVALUE                37379   /* Brightness */
+#define EXIFTAG_EXPOSUREBIASVALUE      37380   /* Exposure bias */
+#define EXIFTAG_MAXAPERTUREVALUE       37381   /* Maximum lens aperture */
+#define EXIFTAG_SUBJECTDISTANCE                37382   /* Subject distance */
+#define EXIFTAG_METERINGMODE           37383   /* Metering mode */
+#define EXIFTAG_LIGHTSOURCE            37384   /* Light source */
+#define EXIFTAG_FLASH                  37385   /* Flash */
+#define EXIFTAG_FOCALLENGTH            37386   /* Lens focal length */
+#define EXIFTAG_SUBJECTAREA            37396   /* Subject area */
+#define EXIFTAG_MAKERNOTE              37500   /* Manufacturer notes */
+#define EXIFTAG_USERCOMMENT            37510   /* User comments */
+#define EXIFTAG_SUBSECTIME             37520   /* DateTime subseconds */
+#define EXIFTAG_SUBSECTIMEORIGINAL     37521   /* DateTimeOriginal subseconds */
+#define EXIFTAG_SUBSECTIMEDIGITIZED    37522   /* DateTimeDigitized subseconds */
+#define EXIFTAG_FLASHPIXVERSION                40960   /* Supported Flashpix version */
+#define EXIFTAG_COLORSPACE             40961   /* Color space information */
+#define EXIFTAG_PIXELXDIMENSION                40962   /* Valid image width */
+#define EXIFTAG_PIXELYDIMENSION                40963   /* Valid image height */
+#define EXIFTAG_RELATEDSOUNDFILE       40964   /* Related audio file */
+#define EXIFTAG_FLASHENERGY            41483   /* Flash energy */
+#define EXIFTAG_SPATIALFREQUENCYRESPONSE 41484 /* Spatial frequency response */
+#define EXIFTAG_FOCALPLANEXRESOLUTION  41486   /* Focal plane X resolution */
+#define EXIFTAG_FOCALPLANEYRESOLUTION  41487   /* Focal plane Y resolution */
+#define EXIFTAG_FOCALPLANERESOLUTIONUNIT 41488 /* Focal plane resolution unit */
+#define EXIFTAG_SUBJECTLOCATION                41492   /* Subject location */
+#define EXIFTAG_EXPOSUREINDEX          41493   /* Exposure index */
+#define EXIFTAG_SENSINGMETHOD          41495   /* Sensing method */
+#define EXIFTAG_FILESOURCE             41728   /* File source */
+#define EXIFTAG_SCENETYPE              41729   /* Scene type */
+#define EXIFTAG_CFAPATTERN             41730   /* CFA pattern */
+#define EXIFTAG_CUSTOMRENDERED         41985   /* Custom image processing */
+#define EXIFTAG_EXPOSUREMODE           41986   /* Exposure mode */
+#define EXIFTAG_WHITEBALANCE           41987   /* White balance */
+#define EXIFTAG_DIGITALZOOMRATIO       41988   /* Digital zoom ratio */
+#define EXIFTAG_FOCALLENGTHIN35MMFILM  41989   /* Focal length in 35 mm film */
+#define EXIFTAG_SCENECAPTURETYPE       41990   /* Scene capture type */
+#define EXIFTAG_GAINCONTROL            41991   /* Gain control */
+#define EXIFTAG_CONTRAST               41992   /* Contrast */
+#define EXIFTAG_SATURATION             41993   /* Saturation */
+#define EXIFTAG_SHARPNESS              41994   /* Sharpness */
+#define EXIFTAG_DEVICESETTINGDESCRIPTION 41995 /* Device settings description */
+#define EXIFTAG_SUBJECTDISTANCERANGE   41996   /* Subject distance range */
+#define EXIFTAG_GAINCONTROL            41991   /* Gain control */
+#define EXIFTAG_GAINCONTROL            41991   /* Gain control */
+#define EXIFTAG_IMAGEUNIQUEID          42016   /* Unique image ID */
+
 #endif /* _TIFF_ */
 
 /* vim: set ts=8 sts=8 sw=8 noet: */
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
similarity index 69%
rename from 3rdparty/libtiff/tiffconf.h
rename to 3rdparty/include/tiffconf.h
index 563a2da..3d14847 100644 (file)
@@ -1,12 +1,41 @@
-/* libtiff/tiffconf.h.  Generated by configure.  */
 /*
   Configuration defines for installed libtiff.
-  Editing this file no longer has any effect on the libtiff build!
+  This file maintained for backward compatibility. Do not use definitions
+  from this file in your programs.
 */
 
 #ifndef _TIFFCONF_
 #define _TIFFCONF_
 
+/* Define to 1 if the system has the type `int16'. */
+/* #undef HAVE_INT16 */
+
+/* Define to 1 if the system has the type `int32'. */
+/* #undef HAVE_INT32 */
+
+/* Define to 1 if the system has the type `int8'. */
+/* #undef HAVE_INT8 */
+
+/* The size of a `int', as computed by sizeof. */
+#define SIZEOF_INT 4
+
+/* The size of a `long', as computed by sizeof. */
+#define SIZEOF_LONG 4
+
+/* Signed 64-bit type formatter */
+#define TIFF_INT64_FORMAT "%I64d"
+
+/* Signed 64-bit type */
+#define TIFF_INT64_T signed __int64
+
+/* Unsigned 64-bit type formatter */
+#define TIFF_UINT64_FORMAT "%I64u"
+
+/* Unsigned 64-bit type */
+#define TIFF_UINT64_T unsigned __int64
+
+/* Compatibility stuff. */
+
 /* Define as 0 or 1 according to the floating point format suported by the
    machine */
 #define HAVE_IEEEFP 1
@@ -22,7 +51,7 @@
 #define CCITT_SUPPORT 1
 
 /* Support JPEG compression (requires IJG JPEG library) */
-#define JPEG_SUPPORT 1
+/* #undef JPEG_SUPPORT */
 
 /* Support LogLuv high dynamic range encoding */
 #define LOGLUV_SUPPORT 1
 #define PACKBITS_SUPPORT 1
 
 /* Support Pixar log-format algorithm (requires Zlib) */
-#define PIXARLOG_SUPPORT 1
+/* #undef PIXARLOG_SUPPORT */
 
 /* Support ThunderScan 4-bit RLE algorithm */
 #define THUNDER_SUPPORT 1
 
 /* Support Deflate compression */
-#define ZIP_SUPPORT 1
+/* #undef ZIP_SUPPORT */
 
 /* Support strip chopping (whether or not to convert single-strip uncompressed
    images to mutiple strips of ~8Kb to reduce memory usage) */
 #define IPTC_SUPPORT
 
 #endif /* _TIFFCONF_ */
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
index 4c87071..06ec25c 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: tiffio.h,v 1.3 2005-06-17 13:57:03 vp153 Exp $ */
+/* $Id: tiffio.h,v 1.56.2.4 2010-06-08 18:50:43 bfriesen Exp $ */
 
 /*
  * Copyright (c) 1988-1997 Sam Leffler
@@ -19,8 +19,8 @@
  * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
- * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
- * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
+ * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
+ * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  * OF THIS SOFTWARE.
  */
 
@@ -58,14 +58,15 @@ typedef     struct tiff TIFF;
  *     32-bit file offsets being the most important, and to ensure
  *     that it is unsigned, rather than signed.
  */
-typedef        uint32 ttag_t;          /* directory tag */
-typedef        uint16 tdir_t;          /* directory index */
-typedef        uint16 tsample_t;       /* sample number */
-typedef        uint32 tstrip_t;        /* strip number */
-typedef uint32 ttile_t;                /* tile number */
-typedef        int32 tsize_t;          /* i/o size in bytes */
-typedef        void* tdata_t;          /* image data ref */
-typedef        uint32 toff_t;          /* file offset */
+typedef uint32 ttag_t;          /* directory tag */
+typedef uint16 tdir_t;          /* directory index */
+typedef uint16 tsample_t;       /* sample number */
+typedef uint32 tstrile_t;       /* strip or tile number */
+typedef tstrile_t tstrip_t;     /* strip number */
+typedef tstrile_t ttile_t;      /* tile number */
+typedef int32 tsize_t;          /* i/o size in bytes */
+typedef void* tdata_t;          /* image data ref */
+typedef uint32 toff_t;          /* file offset */
 
 #if !defined(__WIN32__) && (defined(_WIN32) || defined(WIN32))
 #define __WIN32__
@@ -75,17 +76,17 @@ typedef     uint32 toff_t;          /* file offset */
  * On windows you should define USE_WIN32_FILEIO if you are using tif_win32.c
  * or AVOID_WIN32_FILEIO if you are using something else (like tif_unix.c).
  *
- * By default tif_win32.c is assumed on windows if not using the cygwin
- * environment.
+ * By default tif_unix.c is assumed.
  */
 
 #if defined(_WINDOWS) || defined(__WIN32__) || defined(_Windows)
-#  if !defined(__CYGWIN) && !defined(AVOID_WIN32_FILEIO) && !defined(USE_WIN32_FILIO)
-#    define USE_WIN32_FILEIO
+#  if !defined(__CYGWIN) && !defined(AVOID_WIN32_FILEIO) && !defined(USE_WIN32_FILEIO)
+#    define AVOID_WIN32_FILEIO
 #  endif
 #endif
 
 #if defined(USE_WIN32_FILEIO)
+# define VC_EXTRALEAN
 # include <windows.h>
 # ifdef __WIN32__
 DECLARE_HANDLE(thandle_t);     /* Win32 file handle */
@@ -96,10 +97,6 @@ typedef      HFILE thandle_t;        /* client data handle */
 typedef        void* thandle_t;        /* client data handle */
 #endif /* USE_WIN32_FILEIO */
 
-#ifndef NULL
-# define NULL  (void *)0
-#endif
-
 /*
  * Flags to pass to TIFFPrintDirectory to control
  * printing of data structures that are potentially
@@ -191,35 +188,36 @@ typedef void (*tileSeparateRoutine)
  * RGBA-reader state.
  */
 struct _TIFFRGBAImage {
-       TIFF*   tif;                            /* image handle */
-       int     stoponerr;                      /* stop on read error */
-       int     isContig;                       /* data is packed/separate */
-       int     alpha;                          /* type of alpha data present */
-       uint32  width;                          /* image width */
-       uint32  height;                         /* image height */
-       uint16  bitspersample;                  /* image bits/sample */
-       uint16  samplesperpixel;                /* image samples/pixel */
-       uint16  orientation;                    /* image orientation */
-       uint16  req_orientation;                /* requested orientation */
-       uint16  photometric;                    /* image photometric interp */
-       uint16* redcmap;                        /* colormap pallete */
-       uint16* greencmap;
-       uint16* bluecmap;
-                                               /* get image data routine */
-       int     (*get)(TIFFRGBAImage*, uint32*, uint32, uint32);
+       TIFF* tif;                              /* image handle */
+       int stoponerr;                          /* stop on read error */
+       int isContig;                           /* data is packed/separate */
+       int alpha;                              /* type of alpha data present */
+       uint32 width;                           /* image width */
+       uint32 height;                          /* image height */
+       uint16 bitspersample;                   /* image bits/sample */
+       uint16 samplesperpixel;                 /* image samples/pixel */
+       uint16 orientation;                     /* image orientation */
+       uint16 req_orientation;                 /* requested orientation */
+       uint16 photometric;                     /* image photometric interp */
+       uint16* redcmap;                        /* colormap pallete */
+       uint16* greencmap;
+       uint16* bluecmap;
+       /* get image data routine */
+       int (*get)(TIFFRGBAImage*, uint32*, uint32, uint32);
+       /* put decoded strip/tile */
        union {
            void (*any)(TIFFRGBAImage*);
-           tileContigRoutine   contig;
-           tileSeparateRoutine separate;
-       } put;                                  /* put decoded strip/tile */
-       TIFFRGBValue* Map;                      /* sample mapping array */
-       uint32** BWmap;                         /* black&white map */
-       uint32** PALmap;                        /* palette image map */
-       TIFFYCbCrToRGB* ycbcr;                  /* YCbCr conversion state */
-        TIFFCIELabToRGB* cielab;               /* CIE L*a*b conversion state */
-
-        int    row_offset;
-        int     col_offset;
+           tileContigRoutine contig;
+           tileSeparateRoutine separate;
+       } put;
+       TIFFRGBValue* Map;                      /* sample mapping array */
+       uint32** BWmap;                         /* black&white map */
+       uint32** PALmap;                        /* palette image map */
+       TIFFYCbCrToRGB* ycbcr;                  /* YCbCr conversion state */
+       TIFFCIELabToRGB* cielab;                /* CIE L*a*b conversion state */
+
+       int row_offset;
+       int col_offset;
 };
 
 /*
@@ -250,13 +248,18 @@ typedef struct {
 
 /* share internal LogLuv conversion routines? */
 #ifndef LOGLUV_PUBLIC
-#define LOGLUV_PUBLIC          1       
+#define LOGLUV_PUBLIC          1
+#endif
+
+#if !defined(__GNUC__) && !defined(__attribute__)
+#  define __attribute__(x) /*nothing*/
 #endif
 
 #if defined(c_plusplus) || defined(__cplusplus)
 extern "C" {
 #endif
 typedef        void (*TIFFErrorHandler)(const char*, const char*, va_list);
+typedef        void (*TIFFErrorHandlerExt)(thandle_t, const char*, const char*, va_list);
 typedef        tsize_t (*TIFFReadWriteProc)(thandle_t, tdata_t, tsize_t);
 typedef        toff_t (*TIFFSeekProc)(thandle_t, toff_t, int);
 typedef        int (*TIFFCloseProc)(thandle_t);
@@ -271,7 +274,11 @@ extern     const TIFFCodec* TIFFFindCODEC(uint16);
 extern TIFFCodec* TIFFRegisterCODEC(uint16, const char*, TIFFInitMethod);
 extern void TIFFUnRegisterCODEC(TIFFCodec*);
 extern  int TIFFIsCODECConfigured(uint16);
-extern TIFFCodec* TIFFGetConfiguredCODECs();
+extern TIFFCodec* TIFFGetConfiguredCODECs(void);
+
+/*
+ * Auxiliary functions.
+ */
 
 extern tdata_t _TIFFmalloc(tsize_t);
 extern tdata_t _TIFFrealloc(tdata_t, tsize_t);
@@ -280,6 +287,57 @@ extern     void _TIFFmemcpy(tdata_t, const tdata_t, tsize_t);
 extern int _TIFFmemcmp(const tdata_t, const tdata_t, tsize_t);
 extern void _TIFFfree(tdata_t);
 
+/*
+** Stuff, related to tag handling and creating custom tags.
+*/
+extern  int  TIFFGetTagListCount( TIFF * );
+extern  ttag_t TIFFGetTagListEntry( TIFF *, int tag_index );
+    
+#define        TIFF_ANY        TIFF_NOTYPE     /* for field descriptor searching */
+#define        TIFF_VARIABLE   -1              /* marker for variable length tags */
+#define        TIFF_SPP        -2              /* marker for SamplesPerPixel tags */
+#define        TIFF_VARIABLE2  -3              /* marker for uint32 var-length tags */
+
+#define FIELD_CUSTOM    65    
+
+typedef        struct {
+       ttag_t  field_tag;              /* field's tag */
+       short   field_readcount;        /* read count/TIFF_VARIABLE/TIFF_SPP */
+       short   field_writecount;       /* write count/TIFF_VARIABLE */
+       TIFFDataType field_type;        /* type of associated data */
+        unsigned short field_bit;      /* bit in fieldsset bit vector */
+       unsigned char field_oktochange; /* if true, can change while writing */
+       unsigned char field_passcount;  /* if true, pass dir count on set */
+       char    *field_name;            /* ASCII name */
+} TIFFFieldInfo;
+
+typedef struct _TIFFTagValue {
+    const TIFFFieldInfo  *info;
+    int             count;
+    void           *value;
+} TIFFTagValue;
+
+extern void TIFFMergeFieldInfo(TIFF*, const TIFFFieldInfo[], int);
+extern const TIFFFieldInfo* TIFFFindFieldInfo(TIFF*, ttag_t, TIFFDataType);
+extern  const TIFFFieldInfo* TIFFFindFieldInfoByName(TIFF* , const char *,
+                                                    TIFFDataType);
+extern const TIFFFieldInfo* TIFFFieldWithTag(TIFF*, ttag_t);
+extern const TIFFFieldInfo* TIFFFieldWithName(TIFF*, const char *);
+
+typedef        int (*TIFFVSetMethod)(TIFF*, ttag_t, va_list);
+typedef        int (*TIFFVGetMethod)(TIFF*, ttag_t, va_list);
+typedef        void (*TIFFPrintMethod)(TIFF*, FILE*, long);
+    
+typedef struct {
+    TIFFVSetMethod     vsetfield;      /* tag set routine */
+    TIFFVGetMethod     vgetfield;      /* tag get routine */
+    TIFFPrintMethod    printdir;       /* directory print routine */
+} TIFFTagMethods;
+        
+extern  TIFFTagMethods *TIFFAccessTagMethods( TIFF * );
+extern  void *TIFFGetClientInfo( TIFF *, const char * );
+extern  void TIFFSetClientInfo( TIFF *, void *, const char * );
+
 extern void TIFFCleanup(TIFF*);
 extern void TIFFClose(TIFF*);
 extern int TIFFFlush(TIFF*);
@@ -289,7 +347,12 @@ extern     int TIFFVGetField(TIFF*, ttag_t, va_list);
 extern int TIFFGetFieldDefaulted(TIFF*, ttag_t, ...);
 extern int TIFFVGetFieldDefaulted(TIFF*, ttag_t, va_list);
 extern int TIFFReadDirectory(TIFF*);
+extern int TIFFReadCustomDirectory(TIFF*, toff_t, const TIFFFieldInfo[],
+                                   size_t);
+extern int TIFFReadEXIFDirectory(TIFF*, toff_t);
 extern tsize_t TIFFScanlineSize(TIFF*);
+extern tsize_t TIFFOldScanlineSize(TIFF*);
+extern tsize_t TIFFNewScanlineSize(TIFF*);
 extern tsize_t TIFFRasterScanlineSize(TIFF*);
 extern tsize_t TIFFStripSize(TIFF*);
 extern tsize_t TIFFRawStripSize(TIFF*, tstrip_t);
@@ -327,6 +390,7 @@ extern      int TIFFReadBufferSetup(TIFF*, tdata_t, tsize_t);
 extern int TIFFWriteBufferSetup(TIFF*, tdata_t, tsize_t);
 extern int TIFFSetupStrips(TIFF *);
 extern  int TIFFWriteCheck(TIFF*, int, const char *);
+extern void TIFFFreeDirectory(TIFF*);
 extern  int TIFFCreateDirectory(TIFF*);
 extern int TIFFLastDirectory(TIFF*);
 extern int TIFFSetDirectory(TIFF*, tdir_t);
@@ -372,11 +436,15 @@ extern    TIFF* TIFFClientOpen(const char*, const char*,
            TIFFSizeProc,
            TIFFMapFileProc, TIFFUnmapFileProc);
 extern const char* TIFFFileName(TIFF*);
-extern const char* TIFFSetFileName(TIFF*, const char *);
-extern void TIFFError(const char*, const char*, ...);
-extern void TIFFWarning(const char*, const char*, ...);
+extern const char* TIFFSetFileName(TIFF*, const char *);
+extern void TIFFError(const char*, const char*, ...) __attribute__((format (printf,2,3)));
+extern void TIFFErrorExt(thandle_t, const char*, const char*, ...) __attribute__((format (printf,3,4)));
+extern void TIFFWarning(const char*, const char*, ...) __attribute__((format (printf,2,3)));
+extern void TIFFWarningExt(thandle_t, const char*, const char*, ...) __attribute__((format (printf,3,4)));
 extern TIFFErrorHandler TIFFSetErrorHandler(TIFFErrorHandler);
+extern TIFFErrorHandlerExt TIFFSetErrorHandlerExt(TIFFErrorHandlerExt);
 extern TIFFErrorHandler TIFFSetWarningHandler(TIFFErrorHandler);
+extern TIFFErrorHandlerExt TIFFSetWarningHandlerExt(TIFFErrorHandlerExt);
 extern TIFFExtendProc TIFFSetTagExtender(TIFFExtendProc);
 extern ttile_t TIFFComputeTile(TIFF*, uint32, uint32, uint32, tsample_t);
 extern int TIFFCheckTile(TIFF*, uint32, uint32, uint32, tsample_t);
@@ -401,6 +469,7 @@ extern      void TIFFSwabShort(uint16*);
 extern void TIFFSwabLong(uint32*);
 extern void TIFFSwabDouble(double*);
 extern void TIFFSwabArrayOfShort(uint16*, unsigned long);
+extern void TIFFSwabArrayOfTriples(uint8*, unsigned long);
 extern void TIFFSwabArrayOfLong(uint32*, unsigned long);
 extern void TIFFSwabArrayOfDouble(double*, unsigned long);
 extern void TIFFReverseBits(unsigned char *, unsigned long);
@@ -430,57 +499,6 @@ extern     uint32 LogLuv24fromXYZ(float*, int);
 extern uint32 LogLuv32fromXYZ(float*, int);
 #endif
 #endif /* LOGLUV_PUBLIC */
-
-/*
-** Stuff, related to tag handling and creating custom tags.
-*/
-extern  int  TIFFGetTagListCount( TIFF * );
-extern  ttag_t TIFFGetTagListEntry( TIFF *, int tag_index );
-    
-#define        TIFF_ANY        TIFF_NOTYPE     /* for field descriptor searching */
-#define        TIFF_VARIABLE   -1              /* marker for variable length tags */
-#define        TIFF_SPP        -2              /* marker for SamplesPerPixel tags */
-#define        TIFF_VARIABLE2  -3              /* marker for uint32 var-length tags */
-
-#define FIELD_CUSTOM    65    
-
-typedef        struct {
-       ttag_t  field_tag;              /* field's tag */
-       short   field_readcount;        /* read count/TIFF_VARIABLE/TIFF_SPP */
-       short   field_writecount;       /* write count/TIFF_VARIABLE */
-       TIFFDataType field_type;        /* type of associated data */
-        unsigned short field_bit;      /* bit in fieldsset bit vector */
-       unsigned char field_oktochange; /* if true, can change while writing */
-       unsigned char field_passcount;  /* if true, pass dir count on set */
-       char    *field_name;            /* ASCII name */
-} TIFFFieldInfo;
-
-typedef struct _TIFFTagValue {
-    const TIFFFieldInfo  *info;
-    int             count;
-    void           *value;
-} TIFFTagValue;
-
-extern void TIFFMergeFieldInfo(TIFF*, const TIFFFieldInfo[], int);
-extern const TIFFFieldInfo* TIFFFindFieldInfo(TIFF*, ttag_t, TIFFDataType);
-extern  const TIFFFieldInfo* TIFFFindFieldInfoByName(TIFF* , const char *,
-                                                    TIFFDataType);
-extern const TIFFFieldInfo* TIFFFieldWithTag(TIFF*, ttag_t);
-extern const TIFFFieldInfo* TIFFFieldWithName(TIFF*, const char *);
-
-typedef        int (*TIFFVSetMethod)(TIFF*, ttag_t, va_list);
-typedef        int (*TIFFVGetMethod)(TIFF*, ttag_t, va_list);
-typedef        void (*TIFFPrintMethod)(TIFF*, FILE*, long);
-    
-typedef struct {
-    TIFFVSetMethod     vsetfield;      /* tag set routine */
-    TIFFVGetMethod     vgetfield;      /* tag get routine */
-    TIFFPrintMethod    printdir;       /* directory print routine */
-} TIFFTagMethods;
-        
-extern  TIFFTagMethods *TIFFAccessTagMethods( TIFF * );
-extern  void *TIFFGetClientInfo( TIFF *, const char * );
-extern  void TIFFSetClientInfo( TIFF *, void *, const char * );
     
 extern int TIFFCIELabToRGBInit(TIFFCIELabToRGB*, TIFFDisplay *, float*);
 extern void TIFFCIELabToXYZ(TIFFCIELabToRGB *, uint32, int32, int32,
@@ -499,3 +517,10 @@ extern void TIFFYCbCrtoRGB(TIFFYCbCrToRGB *, uint32, int32, int32,
 #endif /* _TIFFIO_ */
 
 /* vim: set ts=8 sts=8 sw=8 noet: */
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
index 9492587..314a22a 100644 (file)
@@ -1,4 +1,4 @@
-#define TIFFLIB_VERSION_STR "LIBTIFF, Version 3.7.2\nCopyright (c) 1988-1996 Sam Leffler\nCopyright (c) 1991-1996 Silicon Graphics, Inc."
+#define TIFFLIB_VERSION_STR "LIBTIFF, Version 3.9.4\nCopyright (c) 1988-1996 Sam Leffler\nCopyright (c) 1991-1996 Silicon Graphics, Inc."
 /*
  * This define can be used in code that requires
  * compilation-related definitions specific to a
@@ -6,4 +6,4 @@
  * version checking should be done based on the
  * string returned by TIFFGetVersion.
  */
-#define TIFFLIB_VERSION 20050315
+#define TIFFLIB_VERSION 20100615
index 8909f46..02ce56c 100644 (file)
@@ -1,9 +1,9 @@
 /* zconf.h -- configuration of the zlib compression library
- * Copyright (C) 1995-2005 Jean-loup Gailly.
+ * Copyright (C) 1995-2010 Jean-loup Gailly.
  * For conditions of distribution and use, see copyright notice in zlib.h
  */
 
-/* @(#) $Id: zconf.h,v 1.3 2008-05-26 19:08:11 vp153 Exp $ */
+/* @(#) $Id$ */
 
 #ifndef ZCONF_H
 #define ZCONF_H
 /*
  * If you *really* need a unique prefix for all types and library functions,
  * compile with -DZ_PREFIX. The "standard" zlib should be compiled without it.
+ * Even better than compiling with -DZ_PREFIX would be to use configure to set
+ * this permanently in zconf.h using "./configure --zprefix".
  */
-#ifdef Z_PREFIX
-#  define deflateInit_          z_deflateInit_
+#ifdef Z_PREFIX     /* may be set to #if 1 by ./configure */
+
+/* all linked symbols */
+#  define _dist_code            z__dist_code
+#  define _length_code          z__length_code
+#  define _tr_align             z__tr_align
+#  define _tr_flush_block       z__tr_flush_block
+#  define _tr_init              z__tr_init
+#  define _tr_stored_block      z__tr_stored_block
+#  define _tr_tally             z__tr_tally
+#  define adler32               z_adler32
+#  define adler32_combine       z_adler32_combine
+#  define adler32_combine64     z_adler32_combine64
+#  define compress              z_compress
+#  define compress2             z_compress2
+#  define compressBound         z_compressBound
+#  define crc32                 z_crc32
+#  define crc32_combine         z_crc32_combine
+#  define crc32_combine64       z_crc32_combine64
 #  define deflate               z_deflate
+#  define deflateBound          z_deflateBound
+#  define deflateCopy           z_deflateCopy
 #  define deflateEnd            z_deflateEnd
-#  define inflateInit_          z_inflateInit_
-#  define inflate               z_inflate
-#  define inflateEnd            z_inflateEnd
 #  define deflateInit2_         z_deflateInit2_
-#  define deflateSetDictionary  z_deflateSetDictionary
-#  define deflateCopy           z_deflateCopy
-#  define deflateReset          z_deflateReset
+#  define deflateInit_          z_deflateInit_
 #  define deflateParams         z_deflateParams
-#  define deflateBound          z_deflateBound
 #  define deflatePrime          z_deflatePrime
+#  define deflateReset          z_deflateReset
+#  define deflateSetDictionary  z_deflateSetDictionary
+#  define deflateSetHeader      z_deflateSetHeader
+#  define deflateTune           z_deflateTune
+#  define deflate_copyright     z_deflate_copyright
+#  define get_crc_table         z_get_crc_table
+#  define gz_error              z_gz_error
+#  define gz_intmax             z_gz_intmax
+#  define gz_strwinerror        z_gz_strwinerror
+#  define gzbuffer              z_gzbuffer
+#  define gzclearerr            z_gzclearerr
+#  define gzclose               z_gzclose
+#  define gzclose_r             z_gzclose_r
+#  define gzclose_w             z_gzclose_w
+#  define gzdirect              z_gzdirect
+#  define gzdopen               z_gzdopen
+#  define gzeof                 z_gzeof
+#  define gzerror               z_gzerror
+#  define gzflush               z_gzflush
+#  define gzgetc                z_gzgetc
+#  define gzgets                z_gzgets
+#  define gzoffset              z_gzoffset
+#  define gzoffset64            z_gzoffset64
+#  define gzopen                z_gzopen
+#  define gzopen64              z_gzopen64
+#  define gzprintf              z_gzprintf
+#  define gzputc                z_gzputc
+#  define gzputs                z_gzputs
+#  define gzread                z_gzread
+#  define gzrewind              z_gzrewind
+#  define gzseek                z_gzseek
+#  define gzseek64              z_gzseek64
+#  define gzsetparams           z_gzsetparams
+#  define gztell                z_gztell
+#  define gztell64              z_gztell64
+#  define gzungetc              z_gzungetc
+#  define gzwrite               z_gzwrite
+#  define inflate               z_inflate
+#  define inflateBack           z_inflateBack
+#  define inflateBackEnd        z_inflateBackEnd
+#  define inflateBackInit_      z_inflateBackInit_
+#  define inflateCopy           z_inflateCopy
+#  define inflateEnd            z_inflateEnd
+#  define inflateGetHeader      z_inflateGetHeader
 #  define inflateInit2_         z_inflateInit2_
+#  define inflateInit_          z_inflateInit_
+#  define inflateMark           z_inflateMark
+#  define inflatePrime          z_inflatePrime
+#  define inflateReset          z_inflateReset
+#  define inflateReset2         z_inflateReset2
 #  define inflateSetDictionary  z_inflateSetDictionary
 #  define inflateSync           z_inflateSync
 #  define inflateSyncPoint      z_inflateSyncPoint
-#  define inflateCopy           z_inflateCopy
-#  define inflateReset          z_inflateReset
-#  define inflateBack           z_inflateBack
-#  define inflateBackEnd        z_inflateBackEnd
-#  define compress              z_compress
-#  define compress2             z_compress2
-#  define compressBound         z_compressBound
+#  define inflateUndermine      z_inflateUndermine
+#  define inflate_copyright     z_inflate_copyright
+#  define inflate_fast          z_inflate_fast
+#  define inflate_table         z_inflate_table
 #  define uncompress            z_uncompress
-#  define adler32               z_adler32
-#  define crc32                 z_crc32
-#  define get_crc_table         z_get_crc_table
 #  define zError                z_zError
+#  define zcalloc               z_zcalloc
+#  define zcfree                z_zcfree
+#  define zlibCompileFlags      z_zlibCompileFlags
+#  define zlibVersion           z_zlibVersion
 
+/* all zlib typedefs in zlib.h and zconf.h */
+#  define Byte                  z_Byte
+#  define Bytef                 z_Bytef
 #  define alloc_func            z_alloc_func
+#  define charf                 z_charf
 #  define free_func             z_free_func
+#  define gzFile                z_gzFile
+#  define gz_header             z_gz_header
+#  define gz_headerp            z_gz_headerp
 #  define in_func               z_in_func
+#  define intf                  z_intf
 #  define out_func              z_out_func
-#  define Byte                  z_Byte
 #  define uInt                  z_uInt
-#  define uLong                 z_uLong
-#  define Bytef                 z_Bytef
-#  define charf                 z_charf
-#  define intf                  z_intf
 #  define uIntf                 z_uIntf
+#  define uLong                 z_uLong
 #  define uLongf                z_uLongf
-#  define voidpf                z_voidpf
 #  define voidp                 z_voidp
+#  define voidpc                z_voidpc
+#  define voidpf                z_voidpf
+
+/* all zlib structs in zlib.h and zconf.h */
+#  define gz_header_s           z_gz_header_s
+#  define internal_state        z_internal_state
+
 #endif
 
 #if defined(__MSDOS__) && !defined(MSDOS)
@@ -284,49 +356,73 @@ typedef uLong FAR uLongf;
    typedef Byte       *voidp;
 #endif
 
-#if 0           /* HAVE_UNISTD_H -- this line is updated by ./configure */
-#  include <sys/types.h> /* for off_t */
-#  include <unistd.h>    /* for SEEK_* and off_t */
+#ifdef HAVE_UNISTD_H    /* may be set to #if 1 by ./configure */
+#  define Z_HAVE_UNISTD_H
+#endif
+
+#ifdef STDC
+#  include <sys/types.h>    /* for off_t */
+#endif
+
+/* a little trick to accommodate both "#define _LARGEFILE64_SOURCE" and
+ * "#define _LARGEFILE64_SOURCE 1" as requesting 64-bit operations, (even
+ * though the former does not conform to the LFS document), but considering
+ * both "#undef _LARGEFILE64_SOURCE" and "#define _LARGEFILE64_SOURCE 0" as
+ * equivalently requesting no 64-bit operations
+ */
+#if -_LARGEFILE64_SOURCE - -1 == 1
+#  undef _LARGEFILE64_SOURCE
+#endif
+
+#if defined(Z_HAVE_UNISTD_H) || defined(_LARGEFILE64_SOURCE)
+#  include <unistd.h>       /* for SEEK_* and off_t */
 #  ifdef VMS
-#    include <unixio.h>   /* for off_t */
+#    include <unixio.h>     /* for off_t */
+#  endif
+#  ifndef z_off_t
+#    define z_off_t off_t
 #  endif
-#  define z_off_t off_t
 #endif
+
 #ifndef SEEK_SET
 #  define SEEK_SET        0       /* Seek from beginning of file.  */
 #  define SEEK_CUR        1       /* Seek from current position.  */
 #  define SEEK_END        2       /* Set file pointer to EOF plus "offset" */
 #endif
+
 #ifndef z_off_t
 #  define z_off_t long
 #endif
 
+#if defined(_LARGEFILE64_SOURCE) && _LFS64_LARGEFILE-0
+#  define z_off64_t off64_t
+#else
+#  define z_off64_t z_off_t
+#endif
+
 #if defined(__OS400__)
 #  define NO_vsnprintf
 #endif
 
 #if defined(__MVS__)
 #  define NO_vsnprintf
-#  ifdef FAR
-#    undef FAR
-#  endif
 #endif
 
 /* MVS linker does not support external names larger than 8 bytes */
 #if defined(__MVS__)
-#   pragma map(deflateInit_,"DEIN")
-#   pragma map(deflateInit2_,"DEIN2")
-#   pragma map(deflateEnd,"DEEND")
-#   pragma map(deflateBound,"DEBND")
-#   pragma map(inflateInit_,"ININ")
-#   pragma map(inflateInit2_,"ININ2")
-#   pragma map(inflateEnd,"INEND")
-#   pragma map(inflateSync,"INSY")
-#   pragma map(inflateSetDictionary,"INSEDI")
-#   pragma map(compressBound,"CMBND")
-#   pragma map(inflate_table,"INTABL")
-#   pragma map(inflate_fast,"INFA")
-#   pragma map(inflate_copyright,"INCOPY")
+  #pragma map(deflateInit_,"DEIN")
+  #pragma map(deflateInit2_,"DEIN2")
+  #pragma map(deflateEnd,"DEEND")
+  #pragma map(deflateBound,"DEBND")
+  #pragma map(inflateInit_,"ININ")
+  #pragma map(inflateInit2_,"ININ2")
+  #pragma map(inflateEnd,"INEND")
+  #pragma map(inflateSync,"INSY")
+  #pragma map(inflateSetDictionary,"INSEDI")
+  #pragma map(compressBound,"CMBND")
+  #pragma map(inflate_table,"INTABL")
+  #pragma map(inflate_fast,"INFA")
+  #pragma map(inflate_copyright,"INCOPY")
 #endif
 
 #endif /* ZCONF_H */
index 0228179..bfbba83 100644 (file)
@@ -1,7 +1,7 @@
 /* zlib.h -- interface of the 'zlib' general purpose compression library
-  version 1.2.3, July 18th, 2005
+  version 1.2.5, April 19th, 2010
 
-  Copyright (C) 1995-2005 Jean-loup Gailly and Mark Adler
+  Copyright (C) 1995-2010 Jean-loup Gailly and Mark Adler
 
   This software is provided 'as-is', without any express or implied
   warranty.  In no event will the authors be held liable for any damages
 extern "C" {
 #endif
 
-#define ZLIB_VERSION "1.2.3"
-#define ZLIB_VERNUM 0x1230
+#define ZLIB_VERSION "1.2.5"
+#define ZLIB_VERNUM 0x1250
+#define ZLIB_VER_MAJOR 1
+#define ZLIB_VER_MINOR 2
+#define ZLIB_VER_REVISION 5
+#define ZLIB_VER_SUBREVISION 0
 
 /*
-     The 'zlib' compression library provides in-memory compression and
-  decompression functions, including integrity checks of the uncompressed
-  data.  This version of the library supports only one compression method
-  (deflation) but other algorithms will be added later and will have the same
-  stream interface.
-
-     Compression can be done in a single step if the buffers are large
-  enough (for example if an input file is mmap'ed), or can be done by
-  repeated calls of the compression function.  In the latter case, the
-  application must provide more input and/or consume the output
+    The 'zlib' compression library provides in-memory compression and
+  decompression functions, including integrity checks of the uncompressed data.
+  This version of the library supports only one compression method (deflation)
+  but other algorithms will be added later and will have the same stream
+  interface.
+
+    Compression can be done in a single step if the buffers are large enough,
+  or can be done by repeated calls of the compression function.  In the latter
+  case, the application must provide more input and/or consume the output
   (providing more output space) before each call.
 
-     The compressed data format used by default by the in-memory functions is
+    The compressed data format used by default by the in-memory functions is
   the zlib format, which is a zlib wrapper documented in RFC 1950, wrapped
   around a deflate stream, which is itself documented in RFC 1951.
 
-     The library also supports reading and writing files in gzip (.gz) format
+    The library also supports reading and writing files in gzip (.gz) format
   with an interface similar to that of stdio using the functions that start
   with "gz".  The gzip format is different from the zlib format.  gzip is a
   gzip wrapper, documented in RFC 1952, wrapped around a deflate stream.
 
-     This library can optionally read and write gzip streams in memory as well.
+    This library can optionally read and write gzip streams in memory as well.
 
-     The zlib format was designed to be compact and fast for use in memory
+    The zlib format was designed to be compact and fast for use in memory
   and on communications channels.  The gzip format was designed for single-
   file compression on file systems, has a larger header than zlib to maintain
   directory information, and uses a different, slower check method than zlib.
 
-     The library does not install any signal handler. The decoder checks
-  the consistency of the compressed data, so the library should never
-  crash even in case of corrupted input.
+    The library does not install any signal handler.  The decoder checks
+  the consistency of the compressed data, so the library should never crash
+  even in case of corrupted input.
 */
 
 typedef voidpf (*alloc_func) OF((voidpf opaque, uInt items, uInt size));
@@ -126,45 +129,45 @@ typedef struct gz_header_s {
 typedef gz_header FAR *gz_headerp;
 
 /*
-   The application must update next_in and avail_in when avail_in has
-   dropped to zero. It must update next_out and avail_out when avail_out
-   has dropped to zero. The application must initialize zalloc, zfree and
-   opaque before calling the init function. All other fields are set by the
-   compression library and must not be updated by the application.
-
-   The opaque value provided by the application will be passed as the first
-   parameter for calls of zalloc and zfree. This can be useful for custom
-   memory management. The compression library attaches no meaning to the
+     The application must update next_in and avail_in when avail_in has dropped
+   to zero.  It must update next_out and avail_out when avail_out has dropped
+   to zero.  The application must initialize zalloc, zfree and opaque before
+   calling the init function.  All other fields are set by the compression
+   library and must not be updated by the application.
+
+     The opaque value provided by the application will be passed as the first
+   parameter for calls of zalloc and zfree.  This can be useful for custom
+   memory management.  The compression library attaches no meaning to the
    opaque value.
 
-   zalloc must return Z_NULL if there is not enough memory for the object.
+     zalloc must return Z_NULL if there is not enough memory for the object.
    If zlib is used in a multi-threaded application, zalloc and zfree must be
    thread safe.
 
-   On 16-bit systems, the functions zalloc and zfree must be able to allocate
-   exactly 65536 bytes, but will not be required to allocate more than this
-   if the symbol MAXSEG_64K is defined (see zconf.h). WARNING: On MSDOS,
-   pointers returned by zalloc for objects of exactly 65536 bytes *must*
-   have their offset normalized to zero. The default allocation function
-   provided by this library ensures this (see zutil.c). To reduce memory
-   requirements and avoid any allocation of 64K objects, at the expense of
-   compression ratio, compile the library with -DMAX_WBITS=14 (see zconf.h).
-
-   The fields total_in and total_out can be used for statistics or
-   progress reports. After compression, total_in holds the total size of
-   the uncompressed data and may be saved for use in the decompressor
-   (particularly if the decompressor wants to decompress everything in
-   a single step).
+     On 16-bit systems, the functions zalloc and zfree must be able to allocate
+   exactly 65536 bytes, but will not be required to allocate more than this if
+   the symbol MAXSEG_64K is defined (see zconf.h).  WARNING: On MSDOS, pointers
+   returned by zalloc for objects of exactly 65536 bytes *must* have their
+   offset normalized to zero.  The default allocation function provided by this
+   library ensures this (see zutil.c).  To reduce memory requirements and avoid
+   any allocation of 64K objects, at the expense of compression ratio, compile
+   the library with -DMAX_WBITS=14 (see zconf.h).
+
+     The fields total_in and total_out can be used for statistics or progress
+   reports.  After compression, total_in holds the total size of the
+   uncompressed data and may be saved for use in the decompressor (particularly
+   if the decompressor wants to decompress everything in a single step).
 */
 
                         /* constants */
 
 #define Z_NO_FLUSH      0
-#define Z_PARTIAL_FLUSH 1 /* will be removed, use Z_SYNC_FLUSH instead */
+#define Z_PARTIAL_FLUSH 1
 #define Z_SYNC_FLUSH    2
 #define Z_FULL_FLUSH    3
 #define Z_FINISH        4
 #define Z_BLOCK         5
+#define Z_TREES         6
 /* Allowed flush values; see deflate() and inflate() below for details */
 
 #define Z_OK            0
@@ -176,8 +179,8 @@ typedef gz_header FAR *gz_headerp;
 #define Z_MEM_ERROR    (-4)
 #define Z_BUF_ERROR    (-5)
 #define Z_VERSION_ERROR (-6)
-/* Return codes for the compression/decompression functions. Negative
- * values are errors, positive values are used for special but normal events.
+/* Return codes for the compression/decompression functions. Negative values
+ * are errors, positive values are used for special but normal events.
  */
 
 #define Z_NO_COMPRESSION         0
@@ -207,119 +210,140 @@ typedef gz_header FAR *gz_headerp;
 #define zlib_version zlibVersion()
 /* for compatibility with versions < 1.0.2 */
 
+
                         /* basic functions */
 
 ZEXTERN const char * ZEXPORT zlibVersion OF((void));
 /* The application can compare zlibVersion and ZLIB_VERSION for consistency.
-   If the first character differs, the library code actually used is
-   not compatible with the zlib.h header file used by the application.
-   This check is automatically made by deflateInit and inflateInit.
+   If the first character differs, the library code actually used is not
+   compatible with the zlib.h header file used by the application.  This check
+   is automatically made by deflateInit and inflateInit.
  */
 
 /*
 ZEXTERN int ZEXPORT deflateInit OF((z_streamp strm, int level));
 
-     Initializes the internal stream state for compression. The fields
-   zalloc, zfree and opaque must be initialized before by the caller.
-   If zalloc and zfree are set to Z_NULL, deflateInit updates them to
-   use default allocation functions.
+     Initializes the internal stream state for compression.  The fields
+   zalloc, zfree and opaque must be initialized before by the caller.  If
+   zalloc and zfree are set to Z_NULL, deflateInit updates them to use default
+   allocation functions.
 
      The compression level must be Z_DEFAULT_COMPRESSION, or between 0 and 9:
-   1 gives best speed, 9 gives best compression, 0 gives no compression at
-   all (the input data is simply copied a block at a time).
-   Z_DEFAULT_COMPRESSION requests a default compromise between speed and
-   compression (currently equivalent to level 6).
+   1 gives best speed, 9 gives best compression, 0 gives no compression at all
+   (the input data is simply copied a block at a time).  Z_DEFAULT_COMPRESSION
+   requests a default compromise between speed and compression (currently
+   equivalent to level 6).
 
-     deflateInit returns Z_OK if success, Z_MEM_ERROR if there was not
-   enough memory, Z_STREAM_ERROR if level is not a valid compression level,
+     deflateInit returns Z_OK if success, Z_MEM_ERROR if there was not enough
+   memory, Z_STREAM_ERROR if level is not a valid compression level, or
    Z_VERSION_ERROR if the zlib library version (zlib_version) is incompatible
-   with the version assumed by the caller (ZLIB_VERSION).
-   msg is set to null if there is no error message.  deflateInit does not
-   perform any compression: this will be done by deflate().
+   with the version assumed by the caller (ZLIB_VERSION).  msg is set to null
+   if there is no error message.  deflateInit does not perform any compression:
+   this will be done by deflate().
 */
 
 
 ZEXTERN int ZEXPORT deflate OF((z_streamp strm, int flush));
 /*
     deflate compresses as much data as possible, and stops when the input
-  buffer becomes empty or the output buffer becomes full. It may introduce some
-  output latency (reading input without producing any output) except when
+  buffer becomes empty or the output buffer becomes full.  It may introduce
+  some output latency (reading input without producing any output) except when
   forced to flush.
 
-    The detailed semantics are as follows. deflate performs one or both of the
+    The detailed semantics are as follows.  deflate performs one or both of the
   following actions:
 
   - Compress more input starting at next_in and update next_in and avail_in
-    accordingly. If not all input can be processed (because there is not
+    accordingly.  If not all input can be processed (because there is not
     enough room in the output buffer), next_in and avail_in are updated and
     processing will resume at this point for the next call of deflate().
 
   - Provide more output starting at next_out and update next_out and avail_out
-    accordingly. This action is forced if the parameter flush is non zero.
+    accordingly.  This action is forced if the parameter flush is non zero.
     Forcing flush frequently degrades the compression ratio, so this parameter
-    should be set only when necessary (in interactive applications).
-    Some output may be provided even if flush is not set.
-
-  Before the call of deflate(), the application should ensure that at least
-  one of the actions is possible, by providing more input and/or consuming
-  more output, and updating avail_in or avail_out accordingly; avail_out
-  should never be zero before the call. The application can consume the
-  compressed output when it wants, for example when the output buffer is full
-  (avail_out == 0), or after each call of deflate(). If deflate returns Z_OK
-  and with zero avail_out, it must be called again after making room in the
-  output buffer because there might be more output pending.
+    should be set only when necessary (in interactive applications).  Some
+    output may be provided even if flush is not set.
+
+    Before the call of deflate(), the application should ensure that at least
+  one of the actions is possible, by providing more input and/or consuming more
+  output, and updating avail_in or avail_out accordingly; avail_out should
+  never be zero before the call.  The application can consume the compressed
+  output when it wants, for example when the output buffer is full (avail_out
+  == 0), or after each call of deflate().  If deflate returns Z_OK and with
+  zero avail_out, it must be called again after making room in the output
+  buffer because there might be more output pending.
 
     Normally the parameter flush is set to Z_NO_FLUSH, which allows deflate to
-  decide how much data to accumualte before producing output, in order to
+  decide how much data to accumulate before producing output, in order to
   maximize compression.
 
     If the parameter flush is set to Z_SYNC_FLUSH, all pending output is
   flushed to the output buffer and the output is aligned on a byte boundary, so
-  that the decompressor can get all input data available so far. (In particular
-  avail_in is zero after the call if enough output space has been provided
-  before the call.)  Flushing may degrade compression for some compression
-  algorithms and so it should be used only when necessary.
+  that the decompressor can get all input data available so far.  (In
+  particular avail_in is zero after the call if enough output space has been
+  provided before the call.) Flushing may degrade compression for some
+  compression algorithms and so it should be used only when necessary.  This
+  completes the current deflate block and follows it with an empty stored block
+  that is three bits plus filler bits to the next byte, followed by four bytes
+  (00 00 ff ff).
+
+    If flush is set to Z_PARTIAL_FLUSH, all pending output is flushed to the
+  output buffer, but the output is not aligned to a byte boundary.  All of the
+  input data so far will be available to the decompressor, as for Z_SYNC_FLUSH.
+  This completes the current deflate block and follows it with an empty fixed
+  codes block that is 10 bits long.  This assures that enough bytes are output
+  in order for the decompressor to finish the block before the empty fixed code
+  block.
+
+    If flush is set to Z_BLOCK, a deflate block is completed and emitted, as
+  for Z_SYNC_FLUSH, but the output is not aligned on a byte boundary, and up to
+  seven bits of the current block are held to be written as the next byte after
+  the next deflate block is completed.  In this case, the decompressor may not
+  be provided enough bits at this point in order to complete decompression of
+  the data provided so far to the compressor.  It may need to wait for the next
+  block to be emitted.  This is for advanced applications that need to control
+  the emission of deflate blocks.
 
     If flush is set to Z_FULL_FLUSH, all output is flushed as with
   Z_SYNC_FLUSH, and the compression state is reset so that decompression can
   restart from this point if previous compressed data has been damaged or if
-  random access is desired. Using Z_FULL_FLUSH too often can seriously degrade
+  random access is desired.  Using Z_FULL_FLUSH too often can seriously degrade
   compression.
 
     If deflate returns with avail_out == 0, this function must be called again
   with the same value of the flush parameter and more output space (updated
   avail_out), until the flush is complete (deflate returns with non-zero
-  avail_out). In the case of a Z_FULL_FLUSH or Z_SYNC_FLUSH, make sure that
+  avail_out).  In the case of a Z_FULL_FLUSH or Z_SYNC_FLUSH, make sure that
   avail_out is greater than six to avoid repeated flush markers due to
   avail_out == 0 on return.
 
     If the parameter flush is set to Z_FINISH, pending input is processed,
-  pending output is flushed and deflate returns with Z_STREAM_END if there
-  was enough output space; if deflate returns with Z_OK, this function must be
+  pending output is flushed and deflate returns with Z_STREAM_END if there was
+  enough output space; if deflate returns with Z_OK, this function must be
   called again with Z_FINISH and more output space (updated avail_out) but no
-  more input data, until it returns with Z_STREAM_END or an error. After
-  deflate has returned Z_STREAM_END, the only possible operations on the
-  stream are deflateReset or deflateEnd.
+  more input data, until it returns with Z_STREAM_END or an error.  After
+  deflate has returned Z_STREAM_END, the only possible operations on the stream
+  are deflateReset or deflateEnd.
 
     Z_FINISH can be used immediately after deflateInit if all the compression
-  is to be done in a single step. In this case, avail_out must be at least
-  the value returned by deflateBound (see below). If deflate does not return
+  is to be done in a single step.  In this case, avail_out must be at least the
+  value returned by deflateBound (see below).  If deflate does not return
   Z_STREAM_END, then it must be called again as described above.
 
     deflate() sets strm->adler to the adler32 checksum of all input read
   so far (that is, total_in bytes).
 
     deflate() may update strm->data_type if it can make a good guess about
-  the input data type (Z_BINARY or Z_TEXT). In doubt, the data is considered
-  binary. This field is only for information purposes and does not affect
-  the compression algorithm in any manner.
+  the input data type (Z_BINARY or Z_TEXT).  In doubt, the data is considered
+  binary.  This field is only for information purposes and does not affect the
+  compression algorithm in any manner.
 
     deflate() returns Z_OK if some progress has been made (more input
   processed or more output produced), Z_STREAM_END if all input has been
   consumed and all output has been produced (only when flush is set to
   Z_FINISH), Z_STREAM_ERROR if the stream state was inconsistent (for example
-  if next_in or next_out was NULL), Z_BUF_ERROR if no progress is possible
-  (for example avail_in or avail_out was zero). Note that Z_BUF_ERROR is not
+  if next_in or next_out was Z_NULL), Z_BUF_ERROR if no progress is possible
+  (for example avail_in or avail_out was zero).  Note that Z_BUF_ERROR is not
   fatal, and deflate() can be called again with more input and more output
   space to continue compressing.
 */
@@ -328,13 +352,13 @@ ZEXTERN int ZEXPORT deflate OF((z_streamp strm, int flush));
 ZEXTERN int ZEXPORT deflateEnd OF((z_streamp strm));
 /*
      All dynamically allocated data structures for this stream are freed.
-   This function discards any unprocessed input and does not flush any
-   pending output.
+   This function discards any unprocessed input and does not flush any pending
+   output.
 
      deflateEnd returns Z_OK if success, Z_STREAM_ERROR if the
    stream state was inconsistent, Z_DATA_ERROR if the stream was freed
-   prematurely (some input or output was discarded). In the error case,
-   msg may be set but then points to a static string (which must not be
+   prematurely (some input or output was discarded).  In the error case, msg
+   may be set but then points to a static string (which must not be
    deallocated).
 */
 
@@ -342,10 +366,10 @@ ZEXTERN int ZEXPORT deflateEnd OF((z_streamp strm));
 /*
 ZEXTERN int ZEXPORT inflateInit OF((z_streamp strm));
 
-     Initializes the internal stream state for decompression. The fields
+     Initializes the internal stream state for decompression.  The fields
    next_in, avail_in, zalloc, zfree and opaque must be initialized before by
-   the caller. If next_in is not Z_NULL and avail_in is large enough (the exact
-   value depends on the compression method), inflateInit determines the
+   the caller.  If next_in is not Z_NULL and avail_in is large enough (the
+   exact value depends on the compression method), inflateInit determines the
    compression method from the zlib header and allocates all data structures
    accordingly; otherwise the allocation will be deferred to the first call of
    inflate.  If zalloc and zfree are set to Z_NULL, inflateInit updates them to
@@ -353,95 +377,108 @@ ZEXTERN int ZEXPORT inflateInit OF((z_streamp strm));
 
      inflateInit returns Z_OK if success, Z_MEM_ERROR if there was not enough
    memory, Z_VERSION_ERROR if the zlib library version is incompatible with the
-   version assumed by the caller.  msg is set to null if there is no error
-   message. inflateInit does not perform any decompression apart from reading
-   the zlib header if present: this will be done by inflate().  (So next_in and
-   avail_in may be modified, but next_out and avail_out are unchanged.)
+   version assumed by the caller, or Z_STREAM_ERROR if the parameters are
+   invalid, such as a null pointer to the structure.  msg is set to null if
+   there is no error message.  inflateInit does not perform any decompression
+   apart from possibly reading the zlib header if present: actual decompression
+   will be done by inflate().  (So next_in and avail_in may be modified, but
+   next_out and avail_out are unused and unchanged.) The current implementation
+   of inflateInit() does not process any header information -- that is deferred
+   until inflate() is called.
 */
 
 
 ZEXTERN int ZEXPORT inflate OF((z_streamp strm, int flush));
 /*
     inflate decompresses as much data as possible, and stops when the input
-  buffer becomes empty or the output buffer becomes full. It may introduce
+  buffer becomes empty or the output buffer becomes full.  It may introduce
   some output latency (reading input without producing any output) except when
   forced to flush.
 
-  The detailed semantics are as follows. inflate performs one or both of the
+  The detailed semantics are as follows.  inflate performs one or both of the
   following actions:
 
   - Decompress more input starting at next_in and update next_in and avail_in
-    accordingly. If not all input can be processed (because there is not
-    enough room in the output buffer), next_in is updated and processing
-    will resume at this point for the next call of inflate().
+    accordingly.  If not all input can be processed (because there is not
+    enough room in the output buffer), next_in is updated and processing will
+    resume at this point for the next call of inflate().
 
   - Provide more output starting at next_out and update next_out and avail_out
-    accordingly.  inflate() provides as much output as possible, until there
-    is no more input data or no more space in the output buffer (see below
-    about the flush parameter).
-
-  Before the call of inflate(), the application should ensure that at least
-  one of the actions is possible, by providing more input and/or consuming
-  more output, and updating the next_* and avail_* values accordingly.
-  The application can consume the uncompressed output when it wants, for
-  example when the output buffer is full (avail_out == 0), or after each
-  call of inflate(). If inflate returns Z_OK and with zero avail_out, it
-  must be called again after making room in the output buffer because there
-  might be more output pending.
-
-    The flush parameter of inflate() can be Z_NO_FLUSH, Z_SYNC_FLUSH,
-  Z_FINISH, or Z_BLOCK. Z_SYNC_FLUSH requests that inflate() flush as much
-  output as possible to the output buffer. Z_BLOCK requests that inflate() stop
-  if and when it gets to the next deflate block boundary. When decoding the
-  zlib or gzip format, this will cause inflate() to return immediately after
-  the header and before the first block. When doing a raw inflate, inflate()
-  will go ahead and process the first block, and will return when it gets to
-  the end of that block, or when it runs out of data.
+    accordingly.  inflate() provides as much output as possible, until there is
+    no more input data or no more space in the output buffer (see below about
+    the flush parameter).
+
+    Before the call of inflate(), the application should ensure that at least
+  one of the actions is possible, by providing more input and/or consuming more
+  output, and updating the next_* and avail_* values accordingly.  The
+  application can consume the uncompressed output when it wants, for example
+  when the output buffer is full (avail_out == 0), or after each call of
+  inflate().  If inflate returns Z_OK and with zero avail_out, it must be
+  called again after making room in the output buffer because there might be
+  more output pending.
+
+    The flush parameter of inflate() can be Z_NO_FLUSH, Z_SYNC_FLUSH, Z_FINISH,
+  Z_BLOCK, or Z_TREES.  Z_SYNC_FLUSH requests that inflate() flush as much
+  output as possible to the output buffer.  Z_BLOCK requests that inflate()
+  stop if and when it gets to the next deflate block boundary.  When decoding
+  the zlib or gzip format, this will cause inflate() to return immediately
+  after the header and before the first block.  When doing a raw inflate,
+  inflate() will go ahead and process the first block, and will return when it
+  gets to the end of that block, or when it runs out of data.
 
     The Z_BLOCK option assists in appending to or combining deflate streams.
   Also to assist in this, on return inflate() will set strm->data_type to the
-  number of unused bits in the last byte taken from strm->next_in, plus 64
-  if inflate() is currently decoding the last block in the deflate stream,
-  plus 128 if inflate() returned immediately after decoding an end-of-block
-  code or decoding the complete header up to just before the first byte of the
-  deflate stream. The end-of-block will not be indicated until all of the
-  uncompressed data from that block has been written to strm->next_out.  The
-  number of unused bits may in general be greater than seven, except when
-  bit 7 of data_type is set, in which case the number of unused bits will be
-  less than eight.
+  number of unused bits in the last byte taken from strm->next_in, plus 64 if
+  inflate() is currently decoding the last block in the deflate stream, plus
+  128 if inflate() returned immediately after decoding an end-of-block code or
+  decoding the complete header up to just before the first byte of the deflate
+  stream.  The end-of-block will not be indicated until all of the uncompressed
+  data from that block has been written to strm->next_out.  The number of
+  unused bits may in general be greater than seven, except when bit 7 of
+  data_type is set, in which case the number of unused bits will be less than
+  eight.  data_type is set as noted here every time inflate() returns for all
+  flush options, and so can be used to determine the amount of currently
+  consumed input in bits.
+
+    The Z_TREES option behaves as Z_BLOCK does, but it also returns when the
+  end of each deflate block header is reached, before any actual data in that
+  block is decoded.  This allows the caller to determine the length of the
+  deflate block header for later use in random access within a deflate block.
+  256 is added to the value of strm->data_type when inflate() returns
+  immediately after reaching the end of the deflate block header.
 
     inflate() should normally be called until it returns Z_STREAM_END or an
-  error. However if all decompression is to be performed in a single step
-  (a single call of inflate), the parameter flush should be set to
-  Z_FINISH. In this case all pending input is processed and all pending
-  output is flushed; avail_out must be large enough to hold all the
-  uncompressed data. (The size of the uncompressed data may have been saved
-  by the compressor for this purpose.) The next operation on this stream must
-  be inflateEnd to deallocate the decompression state. The use of Z_FINISH
-  is never required, but can be used to inform inflate that a faster approach
-  may be used for the single inflate() call.
+  error.  However if all decompression is to be performed in a single step (a
+  single call of inflate), the parameter flush should be set to Z_FINISH.  In
+  this case all pending input is processed and all pending output is flushed;
+  avail_out must be large enough to hold all the uncompressed data.  (The size
+  of the uncompressed data may have been saved by the compressor for this
+  purpose.) The next operation on this stream must be inflateEnd to deallocate
+  the decompression state.  The use of Z_FINISH is never required, but can be
+  used to inform inflate that a faster approach may be used for the single
+  inflate() call.
 
      In this implementation, inflate() always flushes as much output as
   possible to the output buffer, and always uses the faster approach on the
-  first call. So the only effect of the flush parameter in this implementation
+  first call.  So the only effect of the flush parameter in this implementation
   is on the return value of inflate(), as noted below, or when it returns early
-  because Z_BLOCK is used.
+  because Z_BLOCK or Z_TREES is used.
 
      If a preset dictionary is needed after this call (see inflateSetDictionary
   below), inflate sets strm->adler to the adler32 checksum of the dictionary
   chosen by the compressor and returns Z_NEED_DICT; otherwise it sets
   strm->adler to the adler32 checksum of all output produced so far (that is,
   total_out bytes) and returns Z_OK, Z_STREAM_END or an error code as described
-  below. At the end of the stream, inflate() checks that its computed adler32
+  below.  At the end of the stream, inflate() checks that its computed adler32
   checksum is equal to that saved by the compressor and returns Z_STREAM_END
   only if the checksum is correct.
 
-    inflate() will decompress and check either zlib-wrapped or gzip-wrapped
-  deflate data.  The header type is detected automatically.  Any information
-  contained in the gzip header is not retained, so applications that need that
-  information should instead use raw inflate, see inflateInit2() below, or
-  inflateBack() and perform their own processing of the gzip header and
-  trailer.
+    inflate() can decompress and check either zlib-wrapped or gzip-wrapped
+  deflate data.  The header type is detected automatically, if requested when
+  initializing with inflateInit2().  Any information contained in the gzip
+  header is not retained, so applications that need that information should
+  instead use raw inflate, see inflateInit2() below, or inflateBack() and
+  perform their own processing of the gzip header and trailer.
 
     inflate() returns Z_OK if some progress has been made (more input processed
   or more output produced), Z_STREAM_END if the end of the compressed data has
@@ -449,27 +486,28 @@ ZEXTERN int ZEXPORT inflate OF((z_streamp strm, int flush));
   preset dictionary is needed at this point, Z_DATA_ERROR if the input data was
   corrupted (input stream not conforming to the zlib format or incorrect check
   value), Z_STREAM_ERROR if the stream structure was inconsistent (for example
-  if next_in or next_out was NULL), Z_MEM_ERROR if there was not enough memory,
+  next_in or next_out was Z_NULL), Z_MEM_ERROR if there was not enough memory,
   Z_BUF_ERROR if no progress is possible or if there was not enough room in the
-  output buffer when Z_FINISH is used. Note that Z_BUF_ERROR is not fatal, and
+  output buffer when Z_FINISH is used.  Note that Z_BUF_ERROR is not fatal, and
   inflate() can be called again with more input and more output space to
-  continue decompressing. If Z_DATA_ERROR is returned, the application may then
-  call inflateSync() to look for a good compression block if a partial recovery
-  of the data is desired.
+  continue decompressing.  If Z_DATA_ERROR is returned, the application may
+  then call inflateSync() to look for a good compression block if a partial
+  recovery of the data is desired.
 */
 
 
 ZEXTERN int ZEXPORT inflateEnd OF((z_streamp strm));
 /*
      All dynamically allocated data structures for this stream are freed.
-   This function discards any unprocessed input and does not flush any
-   pending output.
+   This function discards any unprocessed input and does not flush any pending
+   output.
 
      inflateEnd returns Z_OK if success, Z_STREAM_ERROR if the stream state
-   was inconsistent. In the error case, msg may be set but then points to a
+   was inconsistent.  In the error case, msg may be set but then points to a
    static string (which must not be deallocated).
 */
 
+
                         /* Advanced functions */
 
 /*
@@ -484,55 +522,57 @@ ZEXTERN int ZEXPORT deflateInit2 OF((z_streamp strm,
                                      int  memLevel,
                                      int  strategy));
 
-     This is another version of deflateInit with more compression options. The
-   fields next_in, zalloc, zfree and opaque must be initialized before by
-   the caller.
+     This is another version of deflateInit with more compression options.  The
+   fields next_in, zalloc, zfree and opaque must be initialized before by the
+   caller.
 
-     The method parameter is the compression method. It must be Z_DEFLATED in
+     The method parameter is the compression method.  It must be Z_DEFLATED in
    this version of the library.
 
      The windowBits parameter is the base two logarithm of the window size
-   (the size of the history buffer). It should be in the range 8..15 for this
-   version of the library. Larger values of this parameter result in better
-   compression at the expense of memory usage. The default value is 15 if
+   (the size of the history buffer).  It should be in the range 8..15 for this
+   version of the library.  Larger values of this parameter result in better
+   compression at the expense of memory usage.  The default value is 15 if
    deflateInit is used instead.
 
-     windowBits can also be -8..-15 for raw deflate. In this case, -windowBits
-   determines the window size. deflate() will then generate raw deflate data
+     windowBits can also be -8..-15 for raw deflate.  In this case, -windowBits
+   determines the window size.  deflate() will then generate raw deflate data
    with no zlib header or trailer, and will not compute an adler32 check value.
 
-     windowBits can also be greater than 15 for optional gzip encoding. Add
+     windowBits can also be greater than 15 for optional gzip encoding.  Add
    16 to windowBits to write a simple gzip header and trailer around the
-   compressed data instead of a zlib wrapper. The gzip header will have no
-   file name, no extra data, no comment, no modification time (set to zero),
-   no header crc, and the operating system will be set to 255 (unknown).  If a
+   compressed data instead of a zlib wrapper.  The gzip header will have no
+   file name, no extra data, no comment, no modification time (set to zero), no
+   header crc, and the operating system will be set to 255 (unknown).  If a
    gzip stream is being written, strm->adler is a crc32 instead of an adler32.
 
      The memLevel parameter specifies how much memory should be allocated
-   for the internal compression state. memLevel=1 uses minimum memory but
-   is slow and reduces compression ratio; memLevel=9 uses maximum memory
-   for optimal speed. The default value is 8. See zconf.h for total memory
-   usage as a function of windowBits and memLevel.
+   for the internal compression state.  memLevel=1 uses minimum memory but is
+   slow and reduces compression ratio; memLevel=9 uses maximum memory for
+   optimal speed.  The default value is 8.  See zconf.h for total memory usage
+   as a function of windowBits and memLevel.
 
-     The strategy parameter is used to tune the compression algorithm. Use the
+     The strategy parameter is used to tune the compression algorithm.  Use the
    value Z_DEFAULT_STRATEGY for normal data, Z_FILTERED for data produced by a
    filter (or predictor), Z_HUFFMAN_ONLY to force Huffman encoding only (no
    string match), or Z_RLE to limit match distances to one (run-length
-   encoding). Filtered data consists mostly of small values with a somewhat
-   random distribution. In this case, the compression algorithm is tuned to
-   compress them better. The effect of Z_FILTERED is to force more Huffman
+   encoding).  Filtered data consists mostly of small values with a somewhat
+   random distribution.  In this case, the compression algorithm is tuned to
+   compress them better.  The effect of Z_FILTERED is to force more Huffman
    coding and less string matching; it is somewhat intermediate between
-   Z_DEFAULT and Z_HUFFMAN_ONLY. Z_RLE is designed to be almost as fast as
-   Z_HUFFMAN_ONLY, but give better compression for PNG image data. The strategy
-   parameter only affects the compression ratio but not the correctness of the
-   compressed output even if it is not set appropriately.  Z_FIXED prevents the
-   use of dynamic Huffman codes, allowing for a simpler decoder for special
-   applications.
-
-      deflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
-   memory, Z_STREAM_ERROR if a parameter is invalid (such as an invalid
-   method). msg is set to null if there is no error message.  deflateInit2 does
-   not perform any compression: this will be done by deflate().
+   Z_DEFAULT_STRATEGY and Z_HUFFMAN_ONLY.  Z_RLE is designed to be almost as
+   fast as Z_HUFFMAN_ONLY, but give better compression for PNG image data.  The
+   strategy parameter only affects the compression ratio but not the
+   correctness of the compressed output even if it is not set appropriately.
+   Z_FIXED prevents the use of dynamic Huffman codes, allowing for a simpler
+   decoder for special applications.
+
+     deflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
+   memory, Z_STREAM_ERROR if any parameter is invalid (such as an invalid
+   method), or Z_VERSION_ERROR if the zlib library version (zlib_version) is
+   incompatible with the version assumed by the caller (ZLIB_VERSION).  msg is
+   set to null if there is no error message.  deflateInit2 does not perform any
+   compression: this will be done by deflate().
 */
 
 ZEXTERN int ZEXPORT deflateSetDictionary OF((z_streamp strm,
@@ -540,37 +580,37 @@ ZEXTERN int ZEXPORT deflateSetDictionary OF((z_streamp strm,
                                              uInt  dictLength));
 /*
      Initializes the compression dictionary from the given byte sequence
-   without producing any compressed output. This function must be called
-   immediately after deflateInit, deflateInit2 or deflateReset, before any
-   call of deflate. The compressor and decompressor must use exactly the same
+   without producing any compressed output.  This function must be called
+   immediately after deflateInit, deflateInit2 or deflateReset, before any call
+   of deflate.  The compressor and decompressor must use exactly the same
    dictionary (see inflateSetDictionary).
 
      The dictionary should consist of strings (byte sequences) that are likely
    to be encountered later in the data to be compressed, with the most commonly
-   used strings preferably put towards the end of the dictionary. Using a
+   used strings preferably put towards the end of the dictionary.  Using a
    dictionary is most useful when the data to be compressed is short and can be
    predicted with good accuracy; the data can then be compressed better than
    with the default empty dictionary.
 
      Depending on the size of the compression data structures selected by
    deflateInit or deflateInit2, a part of the dictionary may in effect be
-   discarded, for example if the dictionary is larger than the window size in
-   deflate or deflate2. Thus the strings most likely to be useful should be
-   put at the end of the dictionary, not at the front. In addition, the
-   current implementation of deflate will use at most the window size minus
-   262 bytes of the provided dictionary.
+   discarded, for example if the dictionary is larger than the window size
+   provided in deflateInit or deflateInit2.  Thus the strings most likely to be
+   useful should be put at the end of the dictionary, not at the front.  In
+   addition, the current implementation of deflate will use at most the window
+   size minus 262 bytes of the provided dictionary.
 
      Upon return of this function, strm->adler is set to the adler32 value
    of the dictionary; the decompressor may later use this value to determine
-   which dictionary has been used by the compressor. (The adler32 value
+   which dictionary has been used by the compressor.  (The adler32 value
    applies to the whole dictionary even if only a subset of the dictionary is
    actually used by the compressor.) If a raw deflate was requested, then the
    adler32 value is not computed and strm->adler is not set.
 
      deflateSetDictionary returns Z_OK if success, or Z_STREAM_ERROR if a
-   parameter is invalid (such as NULL dictionary) or the stream state is
+   parameter is invalid (e.g.  dictionary being Z_NULL) or the stream state is
    inconsistent (for example if deflate has already been called for this stream
-   or if the compression method is bsort). deflateSetDictionary does not
+   or if the compression method is bsort).  deflateSetDictionary does not
    perform any compression: this will be done by deflate().
 */
 
@@ -581,26 +621,26 @@ ZEXTERN int ZEXPORT deflateCopy OF((z_streamp dest,
 
      This function can be useful when several compression strategies will be
    tried, for example when there are several ways of pre-processing the input
-   data with a filter. The streams that will be discarded should then be freed
+   data with a filter.  The streams that will be discarded should then be freed
    by calling deflateEnd.  Note that deflateCopy duplicates the internal
-   compression state which can be quite large, so this strategy is slow and
-   can consume lots of memory.
+   compression state which can be quite large, so this strategy is slow and can
+   consume lots of memory.
 
      deflateCopy returns Z_OK if success, Z_MEM_ERROR if there was not
    enough memory, Z_STREAM_ERROR if the source stream state was inconsistent
-   (such as zalloc being NULL). msg is left unchanged in both source and
+   (such as zalloc being Z_NULL).  msg is left unchanged in both source and
    destination.
 */
 
 ZEXTERN int ZEXPORT deflateReset OF((z_streamp strm));
 /*
      This function is equivalent to deflateEnd followed by deflateInit,
-   but does not free and reallocate all the internal compression state.
-   The stream will keep the same compression level and any other attributes
-   that may have been set by deflateInit2.
+   but does not free and reallocate all the internal compression state.  The
+   stream will keep the same compression level and any other attributes that
+   may have been set by deflateInit2.
 
-      deflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source
-   stream state was inconsistent (such as zalloc or state being NULL).
+     deflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source
+   stream state was inconsistent (such as zalloc or state being Z_NULL).
 */
 
 ZEXTERN int ZEXPORT deflateParams OF((z_streamp strm,
@@ -610,18 +650,18 @@ ZEXTERN int ZEXPORT deflateParams OF((z_streamp strm,
      Dynamically update the compression level and compression strategy.  The
    interpretation of level and strategy is as in deflateInit2.  This can be
    used to switch between compression and straight copy of the input data, or
-   to switch to a different kind of input data requiring a different
-   strategy. If the compression level is changed, the input available so far
-   is compressed with the old level (and may be flushed); the new level will
-   take effect only at the next call of deflate().
+   to switch to a different kind of input data requiring a different strategy.
+   If the compression level is changed, the input available so far is
+   compressed with the old level (and may be flushed); the new level will take
+   effect only at the next call of deflate().
 
      Before the call of deflateParams, the stream state must be set as for
-   a call of deflate(), since the currently available input may have to
-   be compressed and flushed. In particular, strm->avail_out must be non-zero.
+   a call of deflate(), since the currently available input may have to be
+   compressed and flushed.  In particular, strm->avail_out must be non-zero.
 
      deflateParams returns Z_OK if success, Z_STREAM_ERROR if the source
-   stream state was inconsistent or if a parameter was invalid, Z_BUF_ERROR
-   if strm->avail_out was zero.
+   stream state was inconsistent or if a parameter was invalid, Z_BUF_ERROR if
+   strm->avail_out was zero.
 */
 
 ZEXTERN int ZEXPORT deflateTune OF((z_streamp strm,
@@ -645,9 +685,10 @@ ZEXTERN uLong ZEXPORT deflateBound OF((z_streamp strm,
                                        uLong sourceLen));
 /*
      deflateBound() returns an upper bound on the compressed size after
-   deflation of sourceLen bytes.  It must be called after deflateInit()
-   or deflateInit2().  This would be used to allocate an output buffer
-   for deflation in a single pass, and so would be called before deflate().
+   deflation of sourceLen bytes.  It must be called after deflateInit() or
+   deflateInit2(), and after deflateSetHeader(), if used.  This would be used
+   to allocate an output buffer for deflation in a single pass, and so would be
+   called before deflate().
 */
 
 ZEXTERN int ZEXPORT deflatePrime OF((z_streamp strm,
@@ -655,21 +696,21 @@ ZEXTERN int ZEXPORT deflatePrime OF((z_streamp strm,
                                      int value));
 /*
      deflatePrime() inserts bits in the deflate output stream.  The intent
-  is that this function is used to start off the deflate output with the
-  bits leftover from a previous deflate stream when appending to it.  As such,
-  this function can only be used for raw deflate, and must be used before the
-  first deflate() call after a deflateInit2() or deflateReset().  bits must be
-  less than or equal to 16, and that many of the least significant bits of
-  value will be inserted in the output.
-
-      deflatePrime returns Z_OK if success, or Z_STREAM_ERROR if the source
+   is that this function is used to start off the deflate output with the bits
+   leftover from a previous deflate stream when appending to it.  As such, this
+   function can only be used for raw deflate, and must be used before the first
+   deflate() call after a deflateInit2() or deflateReset().  bits must be less
+   than or equal to 16, and that many of the least significant bits of value
+   will be inserted in the output.
+
+     deflatePrime returns Z_OK if success, or Z_STREAM_ERROR if the source
    stream state was inconsistent.
 */
 
 ZEXTERN int ZEXPORT deflateSetHeader OF((z_streamp strm,
                                          gz_headerp head));
 /*
-      deflateSetHeader() provides gzip header information for when a gzip
+     deflateSetHeader() provides gzip header information for when a gzip
    stream is requested by deflateInit2().  deflateSetHeader() may be called
    after deflateInit2() or deflateReset() and before the first call of
    deflate().  The text, time, os, extra field, name, and comment information
@@ -682,11 +723,11 @@ ZEXTERN int ZEXPORT deflateSetHeader OF((z_streamp strm,
    1.3.x) do not support header crc's, and will report that it is a "multi-part
    gzip file" and give up.
 
-      If deflateSetHeader is not used, the default gzip header has text false,
+     If deflateSetHeader is not used, the default gzip header has text false,
    the time set to zero, and os set to 255, with no extra, name, or comment
    fields.  The gzip header is returned to the default state by deflateReset().
 
-      deflateSetHeader returns Z_OK if success, or Z_STREAM_ERROR if the source
+     deflateSetHeader returns Z_OK if success, or Z_STREAM_ERROR if the source
    stream state was inconsistent.
 */
 
@@ -694,43 +735,50 @@ ZEXTERN int ZEXPORT deflateSetHeader OF((z_streamp strm,
 ZEXTERN int ZEXPORT inflateInit2 OF((z_streamp strm,
                                      int  windowBits));
 
-     This is another version of inflateInit with an extra parameter. The
+     This is another version of inflateInit with an extra parameter.  The
    fields next_in, avail_in, zalloc, zfree and opaque must be initialized
    before by the caller.
 
      The windowBits parameter is the base two logarithm of the maximum window
    size (the size of the history buffer).  It should be in the range 8..15 for
-   this version of the library. The default value is 15 if inflateInit is used
-   instead. windowBits must be greater than or equal to the windowBits value
+   this version of the library.  The default value is 15 if inflateInit is used
+   instead.  windowBits must be greater than or equal to the windowBits value
    provided to deflateInit2() while compressing, or it must be equal to 15 if
-   deflateInit2() was not used. If a compressed stream with a larger window
+   deflateInit2() was not used.  If a compressed stream with a larger window
    size is given as input, inflate() will return with the error code
    Z_DATA_ERROR instead of trying to allocate a larger window.
 
-     windowBits can also be -8..-15 for raw inflate. In this case, -windowBits
-   determines the window size. inflate() will then process raw deflate data,
+     windowBits can also be zero to request that inflate use the window size in
+   the zlib header of the compressed stream.
+
+     windowBits can also be -8..-15 for raw inflate.  In this case, -windowBits
+   determines the window size.  inflate() will then process raw deflate data,
    not looking for a zlib or gzip header, not generating a check value, and not
-   looking for any check values for comparison at the end of the stream. This
+   looking for any check values for comparison at the end of the stream.  This
    is for use with other formats that use the deflate compressed data format
-   such as zip.  Those formats provide their own check values. If a custom
+   such as zip.  Those formats provide their own check values.  If a custom
    format is developed using the raw deflate format for compressed data, it is
    recommended that a check value such as an adler32 or a crc32 be applied to
    the uncompressed data as is done in the zlib, gzip, and zip formats.  For
-   most applications, the zlib format should be used as is. Note that comments
+   most applications, the zlib format should be used as is.  Note that comments
    above on the use in deflateInit2() applies to the magnitude of windowBits.
 
-     windowBits can also be greater than 15 for optional gzip decoding. Add
+     windowBits can also be greater than 15 for optional gzip decoding.  Add
    32 to windowBits to enable zlib and gzip decoding with automatic header
    detection, or add 16 to decode only the gzip format (the zlib format will
-   return a Z_DATA_ERROR).  If a gzip stream is being decoded, strm->adler is
-   crc32 instead of an adler32.
+   return a Z_DATA_ERROR).  If a gzip stream is being decoded, strm->adler is a
+   crc32 instead of an adler32.
 
      inflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
-   memory, Z_STREAM_ERROR if a parameter is invalid (such as a null strm). msg
-   is set to null if there is no error message.  inflateInit2 does not perform
-   any decompression apart from reading the zlib header if present: this will
-   be done by inflate(). (So next_in and avail_in may be modified, but next_out
-   and avail_out are unchanged.)
+   memory, Z_VERSION_ERROR if the zlib library version is incompatible with the
+   version assumed by the caller, or Z_STREAM_ERROR if the parameters are
+   invalid, such as a null pointer to the structure.  msg is set to null if
+   there is no error message.  inflateInit2 does not perform any decompression
+   apart from possibly reading the zlib header if present: actual decompression
+   will be done by inflate().  (So next_in and avail_in may be modified, but
+   next_out and avail_out are unused and unchanged.) The current implementation
+   of inflateInit2() does not process any header information -- that is
+   deferred until inflate() is called.
 */
 
 ZEXTERN int ZEXPORT inflateSetDictionary OF((z_streamp strm,
@@ -738,8 +786,8 @@ ZEXTERN int ZEXPORT inflateSetDictionary OF((z_streamp strm,
                                              uInt  dictLength));
 /*
      Initializes the decompression dictionary from the given uncompressed byte
-   sequence. This function must be called immediately after a call of inflate,
-   if that call returned Z_NEED_DICT. The dictionary chosen by the compressor
+   sequence.  This function must be called immediately after a call of inflate,
+   if that call returned Z_NEED_DICT.  The dictionary chosen by the compressor
    can be determined from the adler32 value returned by that call of inflate.
    The compressor and decompressor must use exactly the same dictionary (see
    deflateSetDictionary).  For raw inflate, this function can be called
@@ -748,26 +796,26 @@ ZEXTERN int ZEXPORT inflateSetDictionary OF((z_streamp strm,
    dictionary that was used for compression is provided.
 
      inflateSetDictionary returns Z_OK if success, Z_STREAM_ERROR if a
-   parameter is invalid (such as NULL dictionary) or the stream state is
+   parameter is invalid (e.g.  dictionary being Z_NULL) or the stream state is
    inconsistent, Z_DATA_ERROR if the given dictionary doesn't match the
-   expected one (incorrect adler32 value). inflateSetDictionary does not
+   expected one (incorrect adler32 value).  inflateSetDictionary does not
    perform any decompression: this will be done by subsequent calls of
    inflate().
 */
 
 ZEXTERN int ZEXPORT inflateSync OF((z_streamp strm));
 /*
-    Skips invalid compressed data until a full flush point (see above the
-  description of deflate with Z_FULL_FLUSH) can be found, or until all
-  available input is skipped. No output is provided.
-
-    inflateSync returns Z_OK if a full flush point has been found, Z_BUF_ERROR
-  if no more input was provided, Z_DATA_ERROR if no flush point has been found,
-  or Z_STREAM_ERROR if the stream structure was inconsistent. In the success
-  case, the application may save the current current value of total_in which
-  indicates where valid compressed data was found. In the error case, the
-  application may repeatedly call inflateSync, providing more input each time,
-  until success or end of the input data.
+     Skips invalid compressed data until a full flush point (see above the
+   description of deflate with Z_FULL_FLUSH) can be found, or until all
+   available input is skipped.  No output is provided.
+
+     inflateSync returns Z_OK if a full flush point has been found, Z_BUF_ERROR
+   if no more input was provided, Z_DATA_ERROR if no flush point has been
+   found, or Z_STREAM_ERROR if the stream structure was inconsistent.  In the
+   success case, the application may save the current current value of total_in
+   which indicates where valid compressed data was found.  In the error case,
+   the application may repeatedly call inflateSync, providing more input each
+   time, until success or end of the input data.
 */
 
 ZEXTERN int ZEXPORT inflateCopy OF((z_streamp dest,
@@ -782,18 +830,30 @@ ZEXTERN int ZEXPORT inflateCopy OF((z_streamp dest,
 
      inflateCopy returns Z_OK if success, Z_MEM_ERROR if there was not
    enough memory, Z_STREAM_ERROR if the source stream state was inconsistent
-   (such as zalloc being NULL). msg is left unchanged in both source and
+   (such as zalloc being Z_NULL).  msg is left unchanged in both source and
    destination.
 */
 
 ZEXTERN int ZEXPORT inflateReset OF((z_streamp strm));
 /*
      This function is equivalent to inflateEnd followed by inflateInit,
-   but does not free and reallocate all the internal decompression state.
-   The stream will keep attributes that may have been set by inflateInit2.
+   but does not free and reallocate all the internal decompression state.  The
+   stream will keep attributes that may have been set by inflateInit2.
 
-      inflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source
-   stream state was inconsistent (such as zalloc or state being NULL).
+     inflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source
+   stream state was inconsistent (such as zalloc or state being Z_NULL).
+*/
+
+ZEXTERN int ZEXPORT inflateReset2 OF((z_streamp strm,
+                                      int windowBits));
+/*
+     This function is the same as inflateReset, but it also permits changing
+   the wrap and window size requests.  The windowBits parameter is interpreted
+   the same as it is for inflateInit2.
+
+     inflateReset2 returns Z_OK if success, or Z_STREAM_ERROR if the source
+   stream state was inconsistent (such as zalloc or state being Z_NULL), or if
+   the windowBits parameter is invalid.
 */
 
 ZEXTERN int ZEXPORT inflatePrime OF((z_streamp strm,
@@ -801,54 +861,87 @@ ZEXTERN int ZEXPORT inflatePrime OF((z_streamp strm,
                                      int value));
 /*
      This function inserts bits in the inflate input stream.  The intent is
-  that this function is used to start inflating at a bit position in the
-  middle of a byte.  The provided bits will be used before any bytes are used
-  from next_in.  This function should only be used with raw inflate, and
-  should be used before the first inflate() call after inflateInit2() or
-  inflateReset().  bits must be less than or equal to 16, and that many of the
-  least significant bits of value will be inserted in the input.
-
-      inflatePrime returns Z_OK if success, or Z_STREAM_ERROR if the source
+   that this function is used to start inflating at a bit position in the
+   middle of a byte.  The provided bits will be used before any bytes are used
+   from next_in.  This function should only be used with raw inflate, and
+   should be used before the first inflate() call after inflateInit2() or
+   inflateReset().  bits must be less than or equal to 16, and that many of the
+   least significant bits of value will be inserted in the input.
+
+     If bits is negative, then the input stream bit buffer is emptied.  Then
+   inflatePrime() can be called again to put bits in the buffer.  This is used
+   to clear out bits leftover after feeding inflate a block description prior
+   to feeding inflate codes.
+
+     inflatePrime returns Z_OK if success, or Z_STREAM_ERROR if the source
    stream state was inconsistent.
 */
 
+ZEXTERN long ZEXPORT inflateMark OF((z_streamp strm));
+/*
+     This function returns two values, one in the lower 16 bits of the return
+   value, and the other in the remaining upper bits, obtained by shifting the
+   return value down 16 bits.  If the upper value is -1 and the lower value is
+   zero, then inflate() is currently decoding information outside of a block.
+   If the upper value is -1 and the lower value is non-zero, then inflate is in
+   the middle of a stored block, with the lower value equaling the number of
+   bytes from the input remaining to copy.  If the upper value is not -1, then
+   it is the number of bits back from the current bit position in the input of
+   the code (literal or length/distance pair) currently being processed.  In
+   that case the lower value is the number of bytes already emitted for that
+   code.
+
+     A code is being processed if inflate is waiting for more input to complete
+   decoding of the code, or if it has completed decoding but is waiting for
+   more output space to write the literal or match data.
+
+     inflateMark() is used to mark locations in the input data for random
+   access, which may be at bit positions, and to note those cases where the
+   output of a code may span boundaries of random access blocks.  The current
+   location in the input stream can be determined from avail_in and data_type
+   as noted in the description for the Z_BLOCK flush parameter for inflate.
+
+     inflateMark returns the value noted above or -1 << 16 if the provided
+   source stream state was inconsistent.
+*/
+
 ZEXTERN int ZEXPORT inflateGetHeader OF((z_streamp strm,
                                          gz_headerp head));
 /*
-      inflateGetHeader() requests that gzip header information be stored in the
+     inflateGetHeader() requests that gzip header information be stored in the
    provided gz_header structure.  inflateGetHeader() may be called after
    inflateInit2() or inflateReset(), and before the first call of inflate().
    As inflate() processes the gzip stream, head->done is zero until the header
    is completed, at which time head->done is set to one.  If a zlib stream is
    being decoded, then head->done is set to -1 to indicate that there will be
-   no gzip header information forthcoming.  Note that Z_BLOCK can be used to
-   force inflate() to return immediately after header processing is complete
-   and before any actual data is decompressed.
+   no gzip header information forthcoming.  Note that Z_BLOCK or Z_TREES can be
+   used to force inflate() to return immediately after header processing is
+   complete and before any actual data is decompressed.
 
-      The text, time, xflags, and os fields are filled in with the gzip header
+     The text, time, xflags, and os fields are filled in with the gzip header
    contents.  hcrc is set to true if there is a header CRC.  (The header CRC
-   was valid if done is set to one.)  If extra is not Z_NULL, then extra_max
+   was valid if done is set to one.) If extra is not Z_NULL, then extra_max
    contains the maximum number of bytes to write to extra.  Once done is true,
    extra_len contains the actual extra field length, and extra contains the
    extra field, or that field truncated if extra_max is less than extra_len.
    If name is not Z_NULL, then up to name_max characters are written there,
    terminated with a zero unless the length is greater than name_max.  If
    comment is not Z_NULL, then up to comm_max characters are written there,
-   terminated with a zero unless the length is greater than comm_max.  When
-   any of extra, name, or comment are not Z_NULL and the respective field is
-   not present in the header, then that field is set to Z_NULL to signal its
+   terminated with a zero unless the length is greater than comm_max.  When any
+   of extra, name, or comment are not Z_NULL and the respective field is not
+   present in the header, then that field is set to Z_NULL to signal its
    absence.  This allows the use of deflateSetHeader() with the returned
    structure to duplicate the header.  However if those fields are set to
    allocated memory, then the application will need to save those pointers
    elsewhere so that they can be eventually freed.
 
-      If inflateGetHeader is not used, then the header information is simply
+     If inflateGetHeader is not used, then the header information is simply
    discarded.  The header is always checked for validity, including the header
    CRC if present.  inflateReset() will reset the process to discard the header
    information.  The application would need to call inflateGetHeader() again to
    retrieve the header from the next gzip stream.
 
-      inflateGetHeader returns Z_OK if success, or Z_STREAM_ERROR if the source
+     inflateGetHeader returns Z_OK if success, or Z_STREAM_ERROR if the source
    stream state was inconsistent.
 */
 
@@ -869,9 +962,9 @@ ZEXTERN int ZEXPORT inflateBackInit OF((z_streamp strm, int windowBits,
      See inflateBack() for the usage of these routines.
 
      inflateBackInit will return Z_OK on success, Z_STREAM_ERROR if any of
-   the paramaters are invalid, Z_MEM_ERROR if the internal state could not
-   be allocated, or Z_VERSION_ERROR if the version of the library does not
-   match the version of the header file.
+   the paramaters are invalid, Z_MEM_ERROR if the internal state could not be
+   allocated, or Z_VERSION_ERROR if the version of the library does not match
+   the version of the header file.
 */
 
 typedef unsigned (*in_func) OF((void FAR *, unsigned char FAR * FAR *));
@@ -891,15 +984,15 @@ ZEXTERN int ZEXPORT inflateBack OF((z_streamp strm,
      inflateBackInit() must be called first to allocate the internal state
    and to initialize the state with the user-provided window buffer.
    inflateBack() may then be used multiple times to inflate a complete, raw
-   deflate stream with each call.  inflateBackEnd() is then called to free
-   the allocated state.
+   deflate stream with each call.  inflateBackEnd() is then called to free the
+   allocated state.
 
      A raw deflate stream is one with no zlib or gzip header or trailer.
    This routine would normally be used in a utility that reads zip or gzip
    files and writes out uncompressed files.  The utility would decode the
-   header and process the trailer on its own, hence this routine expects
-   only the raw deflate stream to decompress.  This is different from the
-   normal behavior of inflate(), which expects either a zlib or gzip header and
+   header and process the trailer on its own, hence this routine expects only
+   the raw deflate stream to decompress.  This is different from the normal
+   behavior of inflate(), which expects either a zlib or gzip header and
    trailer around the deflate stream.
 
      inflateBack() uses two subroutines supplied by the caller that are then
@@ -925,7 +1018,7 @@ ZEXTERN int ZEXPORT inflateBack OF((z_streamp strm,
    calling inflateBack().  If strm->next_in is Z_NULL, then in() will be called
    immediately for input.  If strm->next_in is not Z_NULL, then strm->avail_in
    must also be initialized, and then if strm->avail_in is not zero, input will
-   initially be taken from strm->next_in[0 .. strm->avail_in - 1].
+   initially be taken from strm->next_in[0 ..  strm->avail_in - 1].
 
      The in_desc and out_desc parameters of inflateBack() is passed as the
    first parameter of in() and out() respectively when they are called.  These
@@ -935,15 +1028,15 @@ ZEXTERN int ZEXPORT inflateBack OF((z_streamp strm,
      On return, inflateBack() will set strm->next_in and strm->avail_in to
    pass back any unused input that was provided by the last in() call.  The
    return values of inflateBack() can be Z_STREAM_END on success, Z_BUF_ERROR
-   if in() or out() returned an error, Z_DATA_ERROR if there was a format
-   error in the deflate stream (in which case strm->msg is set to indicate the
-   nature of the error), or Z_STREAM_ERROR if the stream was not properly
-   initialized.  In the case of Z_BUF_ERROR, an input or output error can be
-   distinguished using strm->next_in which will be Z_NULL only if in() returned
-   an error.  If strm->next is not Z_NULL, then the Z_BUF_ERROR was due to
-   out() returning non-zero.  (in() will always be called before out(), so
-   strm->next_in is assured to be defined if out() returns non-zero.)  Note
-   that inflateBack() cannot return Z_OK.
+   if in() or out() returned an error, Z_DATA_ERROR if there was a format error
+   in the deflate stream (in which case strm->msg is set to indicate the nature
+   of the error), or Z_STREAM_ERROR if the stream was not properly initialized.
+   In the case of Z_BUF_ERROR, an input or output error can be distinguished
+   using strm->next_in which will be Z_NULL only if in() returned an error.  If
+   strm->next_in is not Z_NULL, then the Z_BUF_ERROR was due to out() returning
+   non-zero.  (in() will always be called before out(), so strm->next_in is
+   assured to be defined if out() returns non-zero.) Note that inflateBack()
+   cannot return Z_OK.
 */
 
 ZEXTERN int ZEXPORT inflateBackEnd OF((z_streamp strm));
@@ -999,23 +1092,22 @@ ZEXTERN uLong ZEXPORT zlibCompileFlags OF((void));
                         /* utility functions */
 
 /*
-     The following utility functions are implemented on top of the
-   basic stream-oriented functions. To simplify the interface, some
-   default options are assumed (compression level and memory usage,
-   standard memory allocation functions). The source code of these
-   utility functions can easily be modified if you need special options.
+     The following utility functions are implemented on top of the basic
+   stream-oriented functions.  To simplify the interface, some default options
+   are assumed (compression level and memory usage, standard memory allocation
+   functions).  The source code of these utility functions can be modified if
+   you need special options.
 */
 
 ZEXTERN int ZEXPORT compress OF((Bytef *dest,   uLongf *destLen,
                                  const Bytef *source, uLong sourceLen));
 /*
      Compresses the source buffer into the destination buffer.  sourceLen is
-   the byte length of the source buffer. Upon entry, destLen is the total
-   size of the destination buffer, which must be at least the value returned
-   by compressBound(sourceLen). Upon exit, destLen is the actual size of the
+   the byte length of the source buffer.  Upon entry, destLen is the total size
+   of the destination buffer, which must be at least the value returned by
+   compressBound(sourceLen).  Upon exit, destLen is the actual size of the
    compressed buffer.
-     This function can be used to compress a whole file at once if the
-   input file is mmap'ed.
+
      compress returns Z_OK if success, Z_MEM_ERROR if there was not
    enough memory, Z_BUF_ERROR if there was not enough room in the output
    buffer.
@@ -1025,11 +1117,11 @@ ZEXTERN int ZEXPORT compress2 OF((Bytef *dest,   uLongf *destLen,
                                   const Bytef *source, uLong sourceLen,
                                   int level));
 /*
-     Compresses the source buffer into the destination buffer. The level
+     Compresses the source buffer into the destination buffer.  The level
    parameter has the same meaning as in deflateInit.  sourceLen is the byte
-   length of the source buffer. Upon entry, destLen is the total size of the
+   length of the source buffer.  Upon entry, destLen is the total size of the
    destination buffer, which must be at least the value returned by
-   compressBound(sourceLen). Upon exit, destLen is the actual size of the
+   compressBound(sourceLen).  Upon exit, destLen is the actual size of the
    compressed buffer.
 
      compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
@@ -1040,22 +1132,20 @@ ZEXTERN int ZEXPORT compress2 OF((Bytef *dest,   uLongf *destLen,
 ZEXTERN uLong ZEXPORT compressBound OF((uLong sourceLen));
 /*
      compressBound() returns an upper bound on the compressed size after
-   compress() or compress2() on sourceLen bytes.  It would be used before
-   compress() or compress2() call to allocate the destination buffer.
+   compress() or compress2() on sourceLen bytes.  It would be used before a
+   compress() or compress2() call to allocate the destination buffer.
 */
 
 ZEXTERN int ZEXPORT uncompress OF((Bytef *dest,   uLongf *destLen,
                                    const Bytef *source, uLong sourceLen));
 /*
      Decompresses the source buffer into the destination buffer.  sourceLen is
-   the byte length of the source buffer. Upon entry, destLen is the total
-   size of the destination buffer, which must be large enough to hold the
-   entire uncompressed data. (The size of the uncompressed data must have
-   been saved previously by the compressor and transmitted to the decompressor
-   by some mechanism outside the scope of this compression library.)
-   Upon exit, destLen is the actual size of the compressed buffer.
-     This function can be used to decompress a whole file at once if the
-   input file is mmap'ed.
+   the byte length of the source buffer.  Upon entry, destLen is the total size
+   of the destination buffer, which must be large enough to hold the entire
+   uncompressed data.  (The size of the uncompressed data must have been saved
+   previously by the compressor and transmitted to the decompressor by some
+   mechanism outside the scope of this compression library.) Upon exit, destLen
+   is the actual size of the uncompressed buffer.
 
      uncompress returns Z_OK if success, Z_MEM_ERROR if there was not
    enough memory, Z_BUF_ERROR if there was not enough room in the output
@@ -1063,136 +1153,199 @@ ZEXTERN int ZEXPORT uncompress OF((Bytef *dest,   uLongf *destLen,
 */
 
 
-typedef voidp gzFile;
+                        /* gzip file access functions */
+
+/*
+     This library supports reading and writing files in gzip (.gz) format with
+   an interface similar to that of stdio, using the functions that start with
+   "gz".  The gzip format is different from the zlib format.  gzip is a gzip
+   wrapper, documented in RFC 1952, wrapped around a deflate stream.
+*/
+
+typedef voidp gzFile;       /* opaque gzip file descriptor */
 
-ZEXTERN gzFile ZEXPORT gzopen  OF((const char *path, const char *mode));
 /*
-     Opens a gzip (.gz) file for reading or writing. The mode parameter
-   is as in fopen ("rb" or "wb") but can also include a compression level
-   ("wb9") or a strategy: 'f' for filtered data as in "wb6f", 'h' for
-   Huffman only compression as in "wb1h", or 'R' for run-length encoding
-   as in "wb1R". (See the description of deflateInit2 for more information
-   about the strategy parameter.)
+ZEXTERN gzFile ZEXPORT gzopen OF((const char *path, const char *mode));
+
+     Opens a gzip (.gz) file for reading or writing.  The mode parameter is as
+   in fopen ("rb" or "wb") but can also include a compression level ("wb9") or
+   a strategy: 'f' for filtered data as in "wb6f", 'h' for Huffman-only
+   compression as in "wb1h", 'R' for run-length encoding as in "wb1R", or 'F'
+   for fixed code compression as in "wb9F".  (See the description of
+   deflateInit2 for more information about the strategy parameter.) Also "a"
+   can be used instead of "w" to request that the gzip stream that will be
+   written be appended to the file.  "+" will result in an error, since reading
+   and writing to the same gzip file is not supported.
 
      gzopen can be used to read a file which is not in gzip format; in this
    case gzread will directly read from the file without decompression.
 
-     gzopen returns NULL if the file could not be opened or if there was
-   insufficient memory to allocate the (de)compression state; errno
-   can be checked to distinguish the two cases (if errno is zero, the
-   zlib error is Z_MEM_ERROR).  */
+     gzopen returns NULL if the file could not be opened, if there was
+   insufficient memory to allocate the gzFile state, or if an invalid mode was
+   specified (an 'r', 'w', or 'a' was not provided, or '+' was provided).
+   errno can be checked to determine if the reason gzopen failed was that the
+   file could not be opened.
+*/
 
-ZEXTERN gzFile ZEXPORT gzdopen  OF((int fd, const char *mode));
+ZEXTERN gzFile ZEXPORT gzdopen OF((int fd, const char *mode));
 /*
-     gzdopen() associates a gzFile with the file descriptor fd.  File
-   descriptors are obtained from calls like open, dup, creat, pipe or
-   fileno (in the file has been previously opened with fopen).
-   The mode parameter is as in gzopen.
-     The next call of gzclose on the returned gzFile will also close the
-   file descriptor fd, just like fclose(fdopen(fd), mode) closes the file
-   descriptor fd. If you want to keep fd open, use gzdopen(dup(fd), mode).
-     gzdopen returns NULL if there was insufficient memory to allocate
-   the (de)compression state.
+     gzdopen associates a gzFile with the file descriptor fd.  File descriptors
+   are obtained from calls like open, dup, creat, pipe or fileno (if the file
+   has been previously opened with fopen).  The mode parameter is as in gzopen.
+
+     The next call of gzclose on the returned gzFile will also close the file
+   descriptor fd, just like fclose(fdopen(fd, mode)) closes the file descriptor
+   fd.  If you want to keep fd open, use fd = dup(fd_keep); gz = gzdopen(fd,
+   mode);.  The duplicated descriptor should be saved to avoid a leak, since
+   gzdopen does not close fd if it fails.
+
+     gzdopen returns NULL if there was insufficient memory to allocate the
+   gzFile state, if an invalid mode was specified (an 'r', 'w', or 'a' was not
+   provided, or '+' was provided), or if fd is -1.  The file descriptor is not
+   used until the next gz* read, write, seek, or close operation, so gzdopen
+   will not detect if fd is invalid (unless fd is -1).
+*/
+
+ZEXTERN int ZEXPORT gzbuffer OF((gzFile file, unsigned size));
+/*
+     Set the internal buffer size used by this library's functions.  The
+   default buffer size is 8192 bytes.  This function must be called after
+   gzopen() or gzdopen(), and before any other calls that read or write the
+   file.  The buffer memory allocation is always deferred to the first read or
+   write.  Two buffers are allocated, either both of the specified size when
+   writing, or one of the specified size and the other twice that size when
+   reading.  A larger buffer size of, for example, 64K or 128K bytes will
+   noticeably increase the speed of decompression (reading).
+
+     The new buffer size also affects the maximum length for gzprintf().
+
+     gzbuffer() returns 0 on success, or -1 on failure, such as being called
+   too late.
 */
 
 ZEXTERN int ZEXPORT gzsetparams OF((gzFile file, int level, int strategy));
 /*
-     Dynamically update the compression level or strategy. See the description
+     Dynamically update the compression level or strategy.  See the description
    of deflateInit2 for the meaning of these parameters.
+
      gzsetparams returns Z_OK if success, or Z_STREAM_ERROR if the file was not
    opened for writing.
 */
 
-ZEXTERN int ZEXPORT    gzread  OF((gzFile file, voidp buf, unsigned len));
+ZEXTERN int ZEXPORT gzread OF((gzFile file, voidp buf, unsigned len));
 /*
-     Reads the given number of uncompressed bytes from the compressed file.
-   If the input file was not in gzip format, gzread copies the given number
-   of bytes into the buffer.
-     gzread returns the number of uncompressed bytes actually read (0 for
-   end of file, -1 for error). */
+     Reads the given number of uncompressed bytes from the compressed file.  If
+   the input file was not in gzip format, gzread copies the given number of
+   bytes into the buffer.
+
+     After reaching the end of a gzip stream in the input, gzread will continue
+   to read, looking for another gzip stream, or failing that, reading the rest
+   of the input file directly without decompression.  The entire input file
+   will be read if gzread is called until it returns less than the requested
+   len.
+
+     gzread returns the number of uncompressed bytes actually read, less than
+   len for end of file, or -1 for error.
+*/
 
-ZEXTERN int ZEXPORT    gzwrite OF((gzFile file,
-                                   voidpc buf, unsigned len));
+ZEXTERN int ZEXPORT gzwrite OF((gzFile file,
+                                voidpc buf, unsigned len));
 /*
      Writes the given number of uncompressed bytes into the compressed file.
-   gzwrite returns the number of uncompressed bytes actually written
-   (0 in case of error).
+   gzwrite returns the number of uncompressed bytes written or 0 in case of
+   error.
 */
 
-ZEXTERN int ZEXPORTVA   gzprintf OF((gzFile file, const char *format, ...));
+ZEXTERN int ZEXPORTVA gzprintf OF((gzFile file, const char *format, ...));
 /*
-     Converts, formats, and writes the args to the compressed file under
-   control of the format string, as in fprintf. gzprintf returns the number of
-   uncompressed bytes actually written (0 in case of error).  The number of
-   uncompressed bytes written is limited to 4095. The caller should assure that
-   this limit is not exceeded. If it is exceeded, then gzprintf() will return
-   return an error (0) with nothing written. In this case, there may also be a
-   buffer overflow with unpredictable consequences, which is possible only if
-   zlib was compiled with the insecure functions sprintf() or vsprintf()
-   because the secure snprintf() or vsnprintf() functions were not available.
+     Converts, formats, and writes the arguments to the compressed file under
+   control of the format string, as in fprintf.  gzprintf returns the number of
+   uncompressed bytes actually written, or 0 in case of error.  The number of
+   uncompressed bytes written is limited to 8191, or one less than the buffer
+   size given to gzbuffer().  The caller should assure that this limit is not
+   exceeded.  If it is exceeded, then gzprintf() will return an error (0) with
+   nothing written.  In this case, there may also be a buffer overflow with
+   unpredictable consequences, which is possible only if zlib was compiled with
+   the insecure functions sprintf() or vsprintf() because the secure snprintf()
+   or vsnprintf() functions were not available.  This can be determined using
+   zlibCompileFlags().
 */
 
 ZEXTERN int ZEXPORT gzputs OF((gzFile file, const char *s));
 /*
-      Writes the given null-terminated string to the compressed file, excluding
+     Writes the given null-terminated string to the compressed file, excluding
    the terminating null character.
-      gzputs returns the number of characters written, or -1 in case of error.
+
+     gzputs returns the number of characters written, or -1 in case of error.
 */
 
 ZEXTERN char * ZEXPORT gzgets OF((gzFile file, char *buf, int len));
 /*
-      Reads bytes from the compressed file until len-1 characters are read, or
-   a newline character is read and transferred to buf, or an end-of-file
-   condition is encountered.  The string is then terminated with a null
-   character.
-      gzgets returns buf, or Z_NULL in case of error.
+     Reads bytes from the compressed file until len-1 characters are read, or a
+   newline character is read and transferred to buf, or an end-of-file
+   condition is encountered.  If any characters are read or if len == 1, the
+   string is terminated with a null character.  If no characters are read due
+   to an end-of-file or len < 1, then the buffer is left untouched.
+
+     gzgets returns buf which is a null-terminated string, or it returns NULL
+   for end-of-file or in case of error.  If there was an error, the contents at
+   buf are indeterminate.
 */
 
-ZEXTERN int ZEXPORT    gzputc OF((gzFile file, int c));
+ZEXTERN int ZEXPORT gzputc OF((gzFile file, int c));
 /*
-      Writes c, converted to an unsigned char, into the compressed file.
-   gzputc returns the value that was written, or -1 in case of error.
+     Writes c, converted to an unsigned char, into the compressed file.  gzputc
+   returns the value that was written, or -1 in case of error.
 */
 
-ZEXTERN int ZEXPORT    gzgetc OF((gzFile file));
+ZEXTERN int ZEXPORT gzgetc OF((gzFile file));
 /*
-      Reads one byte from the compressed file. gzgetc returns this byte
-   or -1 in case of end of file or error.
+     Reads one byte from the compressed file.  gzgetc returns this byte or -1
+   in case of end of file or error.
 */
 
-ZEXTERN int ZEXPORT    gzungetc OF((int c, gzFile file));
+ZEXTERN int ZEXPORT gzungetc OF((int c, gzFile file));
 /*
-      Push one character back onto the stream to be read again later.
-   Only one character of push-back is allowed.  gzungetc() returns the
-   character pushed, or -1 on failure.  gzungetc() will fail if a
-   character has been pushed but not read yet, or if c is -1. The pushed
-   character will be discarded if the stream is repositioned with gzseek()
-   or gzrewind().
+     Push one character back onto the stream to be read as the first character
+   on the next read.  At least one character of push-back is allowed.
+   gzungetc() returns the character pushed, or -1 on failure.  gzungetc() will
+   fail if c is -1, and may fail if a character has been pushed but not read
+   yet.  If gzungetc is used immediately after gzopen or gzdopen, at least the
+   output buffer size of pushed characters is allowed.  (See gzbuffer above.)
+   The pushed character will be discarded if the stream is repositioned with
+   gzseek() or gzrewind().
 */
 
-ZEXTERN int ZEXPORT    gzflush OF((gzFile file, int flush));
+ZEXTERN int ZEXPORT gzflush OF((gzFile file, int flush));
 /*
-     Flushes all pending output into the compressed file. The parameter
-   flush is as in the deflate() function. The return value is the zlib
-   error number (see function gzerror below). gzflush returns Z_OK if
-   the flush parameter is Z_FINISH and all output could be flushed.
-     gzflush should be called only when strictly necessary because it can
-   degrade compression.
+     Flushes all pending output into the compressed file.  The parameter flush
+   is as in the deflate() function.  The return value is the zlib error number
+   (see function gzerror below).  gzflush is only permitted when writing.
+
+     If the flush parameter is Z_FINISH, the remaining data is written and the
+   gzip stream is completed in the output.  If gzwrite() is called again, a new
+   gzip stream will be started in the output.  gzread() is able to read such
+   concatented gzip streams.
+
+     gzflush should be called only when strictly necessary because it will
+   degrade compression if called too often.
 */
 
-ZEXTERN z_off_t ZEXPORT    gzseek OF((gzFile file,
-                                      z_off_t offset, int whence));
 /*
-      Sets the starting position for the next gzread or gzwrite on the
-   given compressed file. The offset represents a number of bytes in the
-   uncompressed data stream. The whence parameter is defined as in lseek(2);
+ZEXTERN z_off_t ZEXPORT gzseek OF((gzFile file,
+                                   z_off_t offset, int whence));
+
+     Sets the starting position for the next gzread or gzwrite on the given
+   compressed file.  The offset represents a number of bytes in the
+   uncompressed data stream.  The whence parameter is defined as in lseek(2);
    the value SEEK_END is not supported.
+
      If the file is opened for reading, this function is emulated but can be
-   extremely slow. If the file is opened for writing, only forward seeks are
+   extremely slow.  If the file is opened for writing, only forward seeks are
    supported; gzseek then compresses a sequence of zeroes up to the new
    starting position.
 
-      gzseek returns the resulting offset location as measured in bytes from
+     gzseek returns the resulting offset location as measured in bytes from
    the beginning of the uncompressed stream, or -1 in case of error, in
    particular if the file is opened for writing and the new starting position
    would be before the current position.
@@ -1202,68 +1355,127 @@ ZEXTERN int ZEXPORT    gzrewind OF((gzFile file));
 /*
      Rewinds the given file. This function is supported only for reading.
 
-   gzrewind(file) is equivalent to (int)gzseek(file, 0L, SEEK_SET)
+     gzrewind(file) is equivalent to (int)gzseek(file, 0L, SEEK_SET)
 */
 
+/*
 ZEXTERN z_off_t ZEXPORT    gztell OF((gzFile file));
+
+     Returns the starting position for the next gzread or gzwrite on the given
+   compressed file.  This position represents a number of bytes in the
+   uncompressed data stream, and is zero when starting, even if appending or
+   reading a gzip stream from the middle of a file using gzdopen().
+
+     gztell(file) is equivalent to gzseek(file, 0L, SEEK_CUR)
+*/
+
 /*
-     Returns the starting position for the next gzread or gzwrite on the
-   given compressed file. This position represents a number of bytes in the
-   uncompressed data stream.
+ZEXTERN z_off_t ZEXPORT gzoffset OF((gzFile file));
 
-   gztell(file) is equivalent to gzseek(file, 0L, SEEK_CUR)
+     Returns the current offset in the file being read or written.  This offset
+   includes the count of bytes that precede the gzip stream, for example when
+   appending or when using gzdopen() for reading.  When reading, the offset
+   does not include as yet unused buffered input.  This information can be used
+   for a progress indicator.  On error, gzoffset() returns -1.
 */
 
 ZEXTERN int ZEXPORT gzeof OF((gzFile file));
 /*
-     Returns 1 when EOF has previously been detected reading the given
-   input stream, otherwise zero.
+     Returns true (1) if the end-of-file indicator has been set while reading,
+   false (0) otherwise.  Note that the end-of-file indicator is set only if the
+   read tried to go past the end of the input, but came up short.  Therefore,
+   just like feof(), gzeof() may return false even if there is no more data to
+   read, in the event that the last read request was for the exact number of
+   bytes remaining in the input file.  This will happen if the input file size
+   is an exact multiple of the buffer size.
+
+     If gzeof() returns true, then the read functions will return no more data,
+   unless the end-of-file indicator is reset by gzclearerr() and the input file
+   has grown since the previous end of file was detected.
 */
 
 ZEXTERN int ZEXPORT gzdirect OF((gzFile file));
 /*
-     Returns 1 if file is being read directly without decompression, otherwise
-   zero.
+     Returns true (1) if file is being copied directly while reading, or false
+   (0) if file is a gzip stream being decompressed.  This state can change from
+   false to true while reading the input file if the end of a gzip stream is
+   reached, but is followed by data that is not another gzip stream.
+
+     If the input file is empty, gzdirect() will return true, since the input
+   does not contain a gzip stream.
+
+     If gzdirect() is used immediately after gzopen() or gzdopen() it will
+   cause buffers to be allocated to allow reading the file to determine if it
+   is a gzip file.  Therefore if gzbuffer() is used, it should be called before
+   gzdirect().
 */
 
 ZEXTERN int ZEXPORT    gzclose OF((gzFile file));
 /*
-     Flushes all pending output if necessary, closes the compressed file
-   and deallocates all the (de)compression state. The return value is the zlib
-   error number (see function gzerror below).
+     Flushes all pending output if necessary, closes the compressed file and
+   deallocates the (de)compression state.  Note that once file is closed, you
+   cannot call gzerror with file, since its structures have been deallocated.
+   gzclose must not be called more than once on the same file, just as free
+   must not be called more than once on the same allocation.
+
+     gzclose will return Z_STREAM_ERROR if file is not valid, Z_ERRNO on a
+   file operation error, or Z_OK on success.
+*/
+
+ZEXTERN int ZEXPORT gzclose_r OF((gzFile file));
+ZEXTERN int ZEXPORT gzclose_w OF((gzFile file));
+/*
+     Same as gzclose(), but gzclose_r() is only for use when reading, and
+   gzclose_w() is only for use when writing or appending.  The advantage to
+   using these instead of gzclose() is that they avoid linking in zlib
+   compression or decompression code that is not used when only reading or only
+   writing respectively.  If gzclose() is used, then both compression and
+   decompression code will be included the application when linking to a static
+   zlib library.
 */
 
 ZEXTERN const char * ZEXPORT gzerror OF((gzFile file, int *errnum));
 /*
-     Returns the error message for the last error which occurred on the
-   given compressed file. errnum is set to zlib error number. If an
-   error occurred in the file system and not in the compression library,
-   errnum is set to Z_ERRNO and the application may consult errno
-   to get the exact error code.
+     Returns the error message for the last error which occurred on the given
+   compressed file.  errnum is set to zlib error number.  If an error occurred
+   in the file system and not in the compression library, errnum is set to
+   Z_ERRNO and the application may consult errno to get the exact error code.
+
+     The application must not modify the returned string.  Future calls to
+   this function may invalidate the previously returned string.  If file is
+   closed, then the string previously returned by gzerror will no longer be
+   available.
+
+     gzerror() should be used to distinguish errors from end-of-file for those
+   functions above that do not distinguish those cases in their return values.
 */
 
 ZEXTERN void ZEXPORT gzclearerr OF((gzFile file));
 /*
-     Clears the error and end-of-file flags for file. This is analogous to the
-   clearerr() function in stdio. This is useful for continuing to read a gzip
+     Clears the error and end-of-file flags for file.  This is analogous to the
+   clearerr() function in stdio.  This is useful for continuing to read a gzip
    file that is being written concurrently.
 */
 
+
                         /* checksum functions */
 
 /*
      These functions are not related to compression but are exported
-   anyway because they might be useful in applications using the
-   compression library.
+   anyway because they might be useful in applications using the compression
+   library.
 */
 
 ZEXTERN uLong ZEXPORT adler32 OF((uLong adler, const Bytef *buf, uInt len));
 /*
      Update a running Adler-32 checksum with the bytes buf[0..len-1] and
-   return the updated checksum. If buf is NULL, this function returns
-   the required initial value for the checksum.
-   An Adler-32 checksum is almost as reliable as a CRC32 but can be computed
-   much faster. Usage example:
+   return the updated checksum.  If buf is Z_NULL, this function returns the
+   required initial value for the checksum.
+
+     An Adler-32 checksum is almost as reliable as a CRC32 but can be computed
+   much faster.
+
+   Usage example:
 
      uLong adler = adler32(0L, Z_NULL, 0);
 
@@ -1273,9 +1485,10 @@ ZEXTERN uLong ZEXPORT adler32 OF((uLong adler, const Bytef *buf, uInt len));
      if (adler != original_adler) error();
 */
 
+/*
 ZEXTERN uLong ZEXPORT adler32_combine OF((uLong adler1, uLong adler2,
                                           z_off_t len2));
-/*
+
      Combine two Adler-32 checksums into one.  For two sequences of bytes, seq1
    and seq2 with lengths len1 and len2, Adler-32 checksums were calculated for
    each, adler1 and adler2.  adler32_combine() returns the Adler-32 checksum of
@@ -1285,9 +1498,11 @@ ZEXTERN uLong ZEXPORT adler32_combine OF((uLong adler1, uLong adler2,
 ZEXTERN uLong ZEXPORT crc32   OF((uLong crc, const Bytef *buf, uInt len));
 /*
      Update a running CRC-32 with the bytes buf[0..len-1] and return the
-   updated CRC-32. If buf is NULL, this function returns the required initial
-   value for the for the crc. Pre- and post-conditioning (one's complement) is
-   performed within this function so it shouldn't be done by the application.
+   updated CRC-32.  If buf is Z_NULL, this function returns the required
+   initial value for the for the crc.  Pre- and post-conditioning (one's
+   complement) is performed within this function so it shouldn't be done by the
+   application.
+
    Usage example:
 
      uLong crc = crc32(0L, Z_NULL, 0);
@@ -1298,9 +1513,9 @@ ZEXTERN uLong ZEXPORT crc32   OF((uLong crc, const Bytef *buf, uInt len));
      if (crc != original_crc) error();
 */
 
+/*
 ZEXTERN uLong ZEXPORT crc32_combine OF((uLong crc1, uLong crc2, z_off_t len2));
 
-/*
      Combine two CRC-32 check values into one.  For two sequences of bytes,
    seq1 and seq2 with lengths len1 and len2, CRC-32 check values were
    calculated for each, crc1 and crc2.  crc32_combine() returns the CRC-32
@@ -1339,16 +1554,57 @@ ZEXTERN int ZEXPORT inflateBackInit_ OF((z_streamp strm, int windowBits,
         inflateInit2_((strm), (windowBits), ZLIB_VERSION, sizeof(z_stream))
 #define inflateBackInit(strm, windowBits, window) \
         inflateBackInit_((strm), (windowBits), (window), \
-        ZLIB_VERSION, sizeof(z_stream))
+                                            ZLIB_VERSION, sizeof(z_stream))
 
+/* provide 64-bit offset functions if _LARGEFILE64_SOURCE defined, and/or
+ * change the regular functions to 64 bits if _FILE_OFFSET_BITS is 64 (if
+ * both are true, the application gets the *64 functions, and the regular
+ * functions are changed to 64 bits) -- in case these are set on systems
+ * without large file support, _LFS64_LARGEFILE must also be true
+ */
+#if defined(_LARGEFILE64_SOURCE) && _LFS64_LARGEFILE-0
+   ZEXTERN gzFile ZEXPORT gzopen64 OF((const char *, const char *));
+   ZEXTERN z_off64_t ZEXPORT gzseek64 OF((gzFile, z_off64_t, int));
+   ZEXTERN z_off64_t ZEXPORT gztell64 OF((gzFile));
+   ZEXTERN z_off64_t ZEXPORT gzoffset64 OF((gzFile));
+   ZEXTERN uLong ZEXPORT adler32_combine64 OF((uLong, uLong, z_off64_t));
+   ZEXTERN uLong ZEXPORT crc32_combine64 OF((uLong, uLong, z_off64_t));
+#endif
+
+#if !defined(ZLIB_INTERNAL) && _FILE_OFFSET_BITS-0 == 64 && _LFS64_LARGEFILE-0
+#  define gzopen gzopen64
+#  define gzseek gzseek64
+#  define gztell gztell64
+#  define gzoffset gzoffset64
+#  define adler32_combine adler32_combine64
+#  define crc32_combine crc32_combine64
+#  ifdef _LARGEFILE64_SOURCE
+     ZEXTERN gzFile ZEXPORT gzopen64 OF((const char *, const char *));
+     ZEXTERN z_off_t ZEXPORT gzseek64 OF((gzFile, z_off_t, int));
+     ZEXTERN z_off_t ZEXPORT gztell64 OF((gzFile));
+     ZEXTERN z_off_t ZEXPORT gzoffset64 OF((gzFile));
+     ZEXTERN uLong ZEXPORT adler32_combine64 OF((uLong, uLong, z_off_t));
+     ZEXTERN uLong ZEXPORT crc32_combine64 OF((uLong, uLong, z_off_t));
+#  endif
+#else
+   ZEXTERN gzFile ZEXPORT gzopen OF((const char *, const char *));
+   ZEXTERN z_off_t ZEXPORT gzseek OF((gzFile, z_off_t, int));
+   ZEXTERN z_off_t ZEXPORT gztell OF((gzFile));
+   ZEXTERN z_off_t ZEXPORT gzoffset OF((gzFile));
+   ZEXTERN uLong ZEXPORT adler32_combine OF((uLong, uLong, z_off_t));
+   ZEXTERN uLong ZEXPORT crc32_combine OF((uLong, uLong, z_off_t));
+#endif
 
+/* hack for buggy compilers */
 #if !defined(ZUTIL_H) && !defined(NO_DUMMY_DECL)
-    struct internal_state {int dummy;}; /* hack for buggy compilers */
+    struct internal_state {int dummy;};
 #endif
 
+/* undocumented functions */
 ZEXTERN const char   * ZEXPORT zError           OF((int));
-ZEXTERN int            ZEXPORT inflateSyncPoint OF((z_streamp z));
+ZEXTERN int            ZEXPORT inflateSyncPoint OF((z_streamp));
 ZEXTERN const uLongf * ZEXPORT get_crc_table    OF((void));
+ZEXTERN int            ZEXPORT inflateUndermine OF((z_streamp, int));
 
 #ifdef __cplusplus
 }
index 1edf221..e0ecaf5 100644 (file)
@@ -1,5 +1,18 @@
+/* dasum.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 doublereal dasum_(integer *n, doublereal *dx, integer *incx)
 {
     /* System generated locals */
index 9ad6465..a9e2ea4 100644 (file)
@@ -1,5 +1,18 @@
+/* daxpy.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int daxpy_(integer *n, doublereal *da, doublereal *dx, 
        integer *incx, doublereal *dy, integer *incy)
 {
index e55dad7..83ebb0a 100644 (file)
@@ -1,5 +1,18 @@
+/* dbdsdc.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__9 = 9;
@@ -63,7 +76,7 @@ static doublereal c_b29 = 0.;
     integer givnum, givptr, qstart, smlsiz, wstart, smlszp;
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 96b54ff..9aa6bf8 100644 (file)
@@ -1,5 +1,18 @@
+/* dbdsqr.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static doublereal c_b15 = -.125;
@@ -64,7 +77,7 @@ static doublereal c_b72 = -1.;
     doublereal tolmul;
 
 
-/*  -- LAPACK routine (version 3.1.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     January 2007 */
 
@@ -164,16 +177,23 @@ static doublereal c_b72 = -1.;
 /*          The leading dimension of the array C. */
 /*          LDC >= max(1,N) if NCC > 0; LDC >=1 if NCC = 0. */
 
-/*  WORK    (workspace) DOUBLE PRECISION array, dimension (2*N) */
-/*          if NCVT = NRU = NCC = 0, (max(1, 4*N)) otherwise */
+/*  WORK    (workspace) DOUBLE PRECISION array, dimension (4*N) */
 
 /*  INFO    (output) INTEGER */
 /*          = 0:  successful exit */
 /*          < 0:  If INFO = -i, the i-th argument had an illegal value */
-/*          > 0:  the algorithm did not converge; D and E contain the */
-/*                elements of a bidiagonal matrix which is orthogonally */
-/*                similar to the input matrix B;  if INFO = i, i */
-/*                elements of E have not converged to zero. */
+/*          > 0: */
+/*             if NCVT = NRU = NCC = 0, */
+/*                = 1, a split was marked by a positive value in E */
+/*                = 2, current block of Z not diagonalized after 30*N */
+/*                     iterations (in inner while loop) */
+/*                = 3, termination criterion of outer while loop not met */
+/*                     (program created more than N unreduced blocks) */
+/*             else NCVT = NRU = NCC = 0, */
+/*                   the algorithm did not converge; D and E contain the */
+/*                   elements of a bidiagonal matrix which is orthogonally */
+/*                   similar to the input matrix B;  if INFO = i, i */
+/*                   elements of E have not converged to zero. */
 
 /*  Internal Parameters */
 /*  =================== */
index b291910..4b7637c 100644 (file)
@@ -1,5 +1,18 @@
+/* dcopy.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int dcopy_(integer *n, doublereal *dx, integer *incx, 
        doublereal *dy, integer *incy)
 {
index 8d21642..18eb516 100644 (file)
@@ -1,5 +1,18 @@
+/* ddot.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 doublereal ddot_(integer *n, doublereal *dx, integer *incx, doublereal *dy, 
        integer *incy)
 {
index 12277dd..c79274f 100644 (file)
@@ -1,5 +1,18 @@
+/* dgebd2.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -19,7 +32,7 @@ static integer c__1 = 1;
            doublereal *, integer *, doublereal *), xerbla_(char *, integer *);
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index c0beb83..d280627 100644 (file)
@@ -1,5 +1,18 @@
+/* dgebrd.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -35,7 +48,7 @@ static doublereal c_b22 = 1.;
     logical lquery;
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index aeabfb4..399a9fc 100644 (file)
@@ -1,5 +1,18 @@
+/* dgelq2.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int dgelq2_(integer *m, integer *n, doublereal *a, integer *
        lda, doublereal *tau, doublereal *work, integer *info)
 {
     doublereal aii;
     extern /* Subroutine */ int dlarf_(char *, integer *, integer *, 
            doublereal *, integer *, doublereal *, doublereal *, integer *, 
-           doublereal *), dlarfg_(integer *, doublereal *, 
+           doublereal *), dlarfp_(integer *, doublereal *, 
            doublereal *, integer *, doublereal *), xerbla_(char *, integer *);
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
        i__2 = *n - i__ + 1;
 /* Computing MIN */
        i__3 = i__ + 1;
-       dlarfg_(&i__2, &a[i__ + i__ * a_dim1], &a[i__ + min(i__3, *n)* a_dim1]
+       dlarfp_(&i__2, &a[i__ + i__ * a_dim1], &a[i__ + min(i__3, *n)* a_dim1]
 , lda, &tau[i__]);
        if (i__ < *m) {
 
index 3284986..fc62cac 100644 (file)
@@ -1,5 +1,18 @@
+/* dgelqf.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -27,7 +40,7 @@ static integer c__2 = 2;
     logical lquery;
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 2018399..bbfee70 100644 (file)
@@ -1,5 +1,18 @@
+/* dgels.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -51,7 +64,7 @@ static integer c__0 = 0;
            integer *);
 
 
-/*  -- LAPACK driver routine (version 3.1) -- */
+/*  -- LAPACK driver routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 4644c50..1738a91 100644 (file)
@@ -1,5 +1,18 @@
+/* dgelsd.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__6 = 6;
@@ -64,7 +77,7 @@ static doublereal c_b82 = 0.;
     integer smlsiz;
 
 
-/*  -- LAPACK driver routine (version 3.1) -- */
+/*  -- LAPACK driver routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
@@ -336,6 +349,14 @@ static doublereal c_b82 = 0.;
 /* Computing MAX */
                i__1 = maxwrk, i__2 = *m * *m + (*m << 2) + wlalsd;
                maxwrk = max(i__1,i__2);
+/*     XXX: Ensure the Path 2a case below is triggered.  The workspace */
+/*     calculation should use queries for all routines eventually. */
+/* Computing MAX */
+/* Computing MAX */
+               i__3 = *m, i__4 = (*m << 1) - 4, i__3 = max(i__3,i__4), i__3 =
+                        max(i__3,*nrhs), i__4 = *n - *m * 3;
+               i__1 = maxwrk, i__2 = (*m << 2) + *m * *m + max(i__3,i__4);
+               maxwrk = max(i__1,i__2);
            } else {
 
 /*              Path 2 - remaining underdetermined cases. */
index f550ef0..3d78f04 100644 (file)
@@ -1,5 +1,18 @@
+/* dgemm.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int dgemm_(char *transa, char *transb, integer *m, integer *
        n, integer *k, doublereal *alpha, doublereal *a, integer *lda, 
        doublereal *b, integer *ldb, doublereal *beta, doublereal *c__, 
index 67dbeef..696fd01 100644 (file)
@@ -1,5 +1,18 @@
+/* dgemv.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int dgemv_(char *trans, integer *m, integer *n, doublereal *
        alpha, doublereal *a, integer *lda, doublereal *x, integer *incx, 
        doublereal *beta, doublereal *y, integer *incy)
index ad6ef6e..d85f10d 100644 (file)
@@ -1,5 +1,18 @@
+/* dgeqr2.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -15,11 +28,11 @@ static integer c__1 = 1;
     doublereal aii;
     extern /* Subroutine */ int dlarf_(char *, integer *, integer *, 
            doublereal *, integer *, doublereal *, doublereal *, integer *, 
-           doublereal *), dlarfg_(integer *, doublereal *, 
+           doublereal *), dlarfp_(integer *, doublereal *, 
            doublereal *, integer *, doublereal *), xerbla_(char *, integer *);
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
@@ -125,7 +138,7 @@ static integer c__1 = 1;
        i__2 = *m - i__ + 1;
 /* Computing MIN */
        i__3 = i__ + 1;
-       dlarfg_(&i__2, &a[i__ + i__ * a_dim1], &a[min(i__3, *m)+ i__ * a_dim1]
+       dlarfp_(&i__2, &a[i__ + i__ * a_dim1], &a[min(i__3, *m)+ i__ * a_dim1]
 , &c__1, &tau[i__]);
        if (i__ < *n) {
 
index 57a6801..0415f14 100644 (file)
@@ -1,5 +1,18 @@
+/* dgeqrf.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -27,7 +40,7 @@ static integer c__2 = 2;
     logical lquery;
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index dc7e7d1..e581c8a 100644 (file)
@@ -1,5 +1,18 @@
+/* dger.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int dger_(integer *m, integer *n, doublereal *alpha, 
        doublereal *x, integer *incx, doublereal *y, integer *incy, 
        doublereal *a, integer *lda)
index 28c1bdd..2d9e594 100644 (file)
@@ -1,5 +1,18 @@
+/* dgesdd.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -68,9 +81,10 @@ static doublereal c_b248 = 1.;
     logical wntqas, lquery;
 
 
-/*  -- LAPACK driver routine (version 3.1) -- */
-/*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
-/*     November 2006 */
+/*  -- LAPACK driver routine (version 3.2.1)                                  -- */
+/*  -- LAPACK is a software package provided by Univ. of Tennessee,    -- */
+/*  -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..-- */
+/*     March 2009 */
 
 /*     .. Scalar Arguments .. */
 /*     .. */
@@ -179,10 +193,10 @@ static doublereal c_b248 = 1.;
 /*          If JOBZ = 'N', */
 /*            LWORK >= 3*min(M,N) + max(max(M,N),7*min(M,N)). */
 /*          If JOBZ = 'O', */
-/*            LWORK >= 3*min(M,N)*min(M,N) + */
+/*            LWORK >= 3*min(M,N) + */
 /*                     max(max(M,N),5*min(M,N)*min(M,N)+4*min(M,N)). */
 /*          If JOBZ = 'S' or 'A' */
-/*            LWORK >= 3*min(M,N)*min(M,N) + */
+/*            LWORK >= 3*min(M,N) + */
 /*                     max(max(M,N),4*min(M,N)*min(M,N)+4*min(M,N)). */
 /*          For good performance, LWORK should generally be larger. */
 /*          If LWORK = -1 but other input arguments are legal, WORK(1) */
index 62ac831..53a3257 100644 (file)
@@ -1,5 +1,18 @@
+/* dgesv.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int dgesv_(integer *n, integer *nrhs, doublereal *a, integer 
        *lda, integer *ipiv, doublereal *b, integer *ldb, integer *info)
 {
@@ -12,7 +25,7 @@
            integer *, integer *, doublereal *, integer *, integer *);
 
 
-/*  -- LAPACK driver routine (version 3.1) -- */
+/*  -- LAPACK driver routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 0eaae52..aea4657 100644 (file)
@@ -1,5 +1,18 @@
+/* dgetf2.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -26,7 +39,7 @@ static doublereal c_b8 = -1.;
     extern /* Subroutine */ int xerbla_(char *, integer *);
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index c3513b7..de76a5b 100644 (file)
@@ -1,5 +1,18 @@
+/* dgetrf.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -30,7 +43,7 @@ static doublereal c_b19 = -1.;
            integer *, integer *, integer *, integer *);
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 8ff1455..9c0467a 100644 (file)
@@ -1,5 +1,18 @@
+/* dgetri.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -37,7 +50,7 @@ static doublereal c_b22 = 1.;
     logical lquery;
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index c8931b4..767dafe 100644 (file)
@@ -1,5 +1,18 @@
+/* dgetrs.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -23,7 +36,7 @@ static integer c_n1 = -1;
     logical notran;
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 045b902..d63599b 100644 (file)
@@ -1,12 +1,25 @@
+/* dlabad.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int dlabad_(doublereal *small, doublereal *large)
 {
     /* Builtin functions */
     double d_lg10(doublereal *), sqrt(doublereal);
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 4cc3514..9f794fa 100644 (file)
@@ -1,5 +1,18 @@
+/* dlabrd.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static doublereal c_b4 = -1.;
@@ -25,7 +38,7 @@ static doublereal c_b16 = 0.;
             doublereal *, integer *, doublereal *);
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 12db3d0..327ce8a 100644 (file)
@@ -1,5 +1,18 @@
+/* dlacpy.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int dlacpy_(char *uplo, integer *m, integer *n, doublereal *
        a, integer *lda, doublereal *b, integer *ldb)
 {
@@ -11,7 +24,7 @@
     extern logical lsame_(char *, char *);
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 0b7b5f7..5d05abe 100644 (file)
@@ -1,5 +1,18 @@
+/* dlae2.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int dlae2_(doublereal *a, doublereal *b, doublereal *c__, 
        doublereal *rt1, doublereal *rt2)
 {
@@ -13,7 +26,7 @@
     doublereal ab, df, tb, sm, rt, adf, acmn, acmx;
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index a76f635..d5ebcc1 100644 (file)
@@ -1,5 +1,18 @@
+/* dlaebz.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int dlaebz_(integer *ijob, integer *nitmax, integer *n, 
        integer *mmax, integer *minp, integer *nbmin, doublereal *abstol, 
        doublereal *reltol, doublereal *pivmin, doublereal *d__, doublereal *
@@ -18,7 +31,7 @@
     integer itmp1, itmp2, kfnew, klnew;
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 41283d4..a8fdc5a 100644 (file)
@@ -1,5 +1,18 @@
+/* dlaed0.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__9 = 9;
@@ -55,7 +68,7 @@ static integer c__1 = 1;
     integer curlvl, matsiz, iprmpt, smlsiz;
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 13ed8c5..bf07869 100644 (file)
@@ -1,5 +1,18 @@
+/* dlaed1.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -30,7 +43,7 @@ static integer c_n1 = -1;
     integer coltyp;
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index e34ad20..7a61241 100644 (file)
@@ -1,5 +1,18 @@
+/* dlaed2.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static doublereal c_b3 = -1.;
@@ -38,7 +51,7 @@ static integer c__1 = 1;
            integer *, doublereal *, integer *, doublereal *, integer *), xerbla_(char *, integer *);
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 914c464..ea84c47 100644 (file)
@@ -1,5 +1,18 @@
+/* dlaed3.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -35,7 +48,7 @@ static doublereal c_b23 = 0.;
            doublereal *, integer *), xerbla_(char *, integer *);
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 1a3686f..7054ad6 100644 (file)
@@ -1,5 +1,18 @@
+/* dlaed4.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int dlaed4_(integer *n, integer *i__, doublereal *d__, 
        doublereal *z__, doublereal *delta, doublereal *rho, doublereal *dlam, 
         integer *info)
@@ -35,7 +48,7 @@
     doublereal erretm, rhoinv;
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index a4be3ea..a733acb 100644 (file)
@@ -1,5 +1,18 @@
+/* dlaed5.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int dlaed5_(integer *i__, doublereal *d__, doublereal *z__, 
        doublereal *delta, doublereal *rho, doublereal *dlam)
 {
@@ -13,7 +26,7 @@
     doublereal b, c__, w, del, tau, temp;
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index f84613d..5cd51d9 100644 (file)
@@ -1,5 +1,18 @@
+/* dlaed6.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int dlaed6_(integer *kniter, logical *orgati, doublereal *
        rho, doublereal *d__, doublereal *z__, doublereal *finit, doublereal *
        tau, integer *info)
@@ -24,7 +37,7 @@
     doublereal dscale[3], sclfac, zscale[3], erretm, sclinv;
 
 
-/*  -- LAPACK routine (version 3.1.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     February 2007 */
 
index 71f3e64..5982de0 100644 (file)
@@ -1,5 +1,18 @@
+/* dlaed7.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__2 = 2;
@@ -44,7 +57,7 @@ static integer c_n1 = -1;
     integer coltyp;
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 44e6c7c..03ea90b 100644 (file)
@@ -1,5 +1,18 @@
+/* dlaed8.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static doublereal c_b3 = -1.;
@@ -37,7 +50,7 @@ static integer c__1 = 1;
            integer *, doublereal *, integer *, doublereal *, integer *), xerbla_(char *, integer *);
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 6c02809..48a1942 100644 (file)
@@ -1,5 +1,18 @@
+/* dlaed9.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -28,7 +41,7 @@ static integer c__1 = 1;
     extern /* Subroutine */ int xerbla_(char *, integer *);
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 3984d5a..8d94236 100644 (file)
@@ -1,5 +1,18 @@
+/* dlaeda.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__2 = 2;
@@ -31,7 +44,7 @@ static doublereal c_b26 = 0.;
             integer *);
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index f0d4f0f..61ab4ee 100644 (file)
@@ -1,5 +1,18 @@
+/* dlaev2.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int dlaev2_(doublereal *a, doublereal *b, doublereal *c__, 
        doublereal *rt1, doublereal *rt2, doublereal *cs1, doublereal *sn1)
 {
@@ -15,7 +28,7 @@
     doublereal acmn, acmx;
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 68fb88d..a278cef 100644 (file)
@@ -1,5 +1,18 @@
+/* dlagtf.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int dlagtf_(integer *n, doublereal *a, doublereal *lambda, 
        doublereal *b, doublereal *c__, doublereal *tol, doublereal *d__, 
        integer *in, integer *info)
@@ -15,7 +28,7 @@
     extern /* Subroutine */ int xerbla_(char *, integer *);
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index c6f2ab9..b7618b3 100644 (file)
@@ -1,5 +1,18 @@
+/* dlagts.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int dlagts_(integer *job, integer *n, doublereal *a, 
        doublereal *b, doublereal *c__, doublereal *d__, integer *in, 
        doublereal *y, doublereal *tol, integer *info)
@@ -19,7 +32,7 @@
     doublereal bignum;
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index d4f1a14..7ee1333 100644 (file)
@@ -1,12 +1,25 @@
+/* dlaisnan.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 logical dlaisnan_(doublereal *din1, doublereal *din2)
 {
     /* System generated locals */
     logical ret_val;
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
@@ -24,8 +37,7 @@ logical dlaisnan_(doublereal *din1, doublereal *din2)
 /*  returns .TRUE.  To check for NaNs, pass the same variable as both */
 /*  arguments. */
 
-/*  Strictly speaking, Fortran does not allow aliasing of function */
-/*  arguments. So a compiler must assume that the two arguments are */
+/*  A compiler must assume that the two arguments are */
 /*  not the same variable, and the test will not be optimized away. */
 /*  Interprocedural or whole-program optimization may delete this */
 /*  test.  The ISNAN functions will be replaced by the correct */
index 077a1b1..4a8fd17 100644 (file)
@@ -1,5 +1,18 @@
+/* dlals0.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static doublereal c_b5 = -1.;
@@ -45,7 +58,7 @@ static integer c__0 = 0;
     doublereal dsigjp;
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index d84c3ac..7dceaea 100644 (file)
@@ -1,5 +1,18 @@
+/* dlalsa.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static doublereal c_b7 = 1.;
@@ -41,7 +54,7 @@ static integer c__2 = 2;
            integer *, integer *, integer *), xerbla_(char *, integer *);
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 474ac66..43a746c 100644 (file)
@@ -1,5 +1,18 @@
+/* dlalsd.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -73,7 +86,7 @@ static doublereal c_b11 = 1.;
     integer givnum, givptr, smlszp;
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 75c1fe7..f5e6760 100644 (file)
@@ -1,5 +1,18 @@
+/* dlamrg.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int dlamrg_(integer *n1, integer *n2, doublereal *a, integer 
        *dtrd1, integer *dtrd2, integer *index)
 {
@@ -10,7 +23,7 @@
     integer i__, ind1, ind2, n1sv, n2sv;
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 1a90072..a12d8fd 100644 (file)
@@ -1,5 +1,18 @@
+/* dlaneg.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 integer dlaneg_(integer *n, doublereal *d__, doublereal *lld, doublereal *
        sigma, doublereal *pivmin, integer *r__)
 {
@@ -19,7 +32,7 @@ integer dlaneg_(integer *n, doublereal *d__, doublereal *lld, doublereal *
     doublereal dminus;
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 6a03c41..830a6c2 100644 (file)
@@ -1,5 +1,18 @@
+/* dlange.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -23,7 +36,7 @@ doublereal dlange_(char *norm, integer *m, integer *n, doublereal *a, integer
            doublereal *, doublereal *);
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 6f81526..38be203 100644 (file)
@@ -1,5 +1,18 @@
+/* dlanst.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -22,7 +35,7 @@ doublereal dlanst_(char *norm, integer *n, doublereal *d__, doublereal *e)
            doublereal *, doublereal *);
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index bc75a31..6b8780f 100644 (file)
@@ -1,5 +1,18 @@
+/* dlansy.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -23,7 +36,7 @@ doublereal dlansy_(char *norm, char *uplo, integer *n, doublereal *a, integer
            doublereal *, doublereal *);
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 82b91d3..b05b474 100644 (file)
@@ -1,5 +1,18 @@
+/* dlapy2.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 doublereal dlapy2_(doublereal *x, doublereal *y)
 {
     /* System generated locals */
@@ -12,7 +25,7 @@ doublereal dlapy2_(doublereal *x, doublereal *y)
     doublereal w, z__, xabs, yabs;
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index b46286b..0801489 100644 (file)
@@ -1,5 +1,18 @@
+/* dlar1v.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int dlar1v_(integer *n, integer *b1, integer *bn, doublereal 
        *lambda, doublereal *d__, doublereal *l, doublereal *ld, doublereal *
        lld, doublereal *pivmin, doublereal *gaptol, doublereal *z__, logical 
@@ -28,7 +41,7 @@
     logical sawnan1, sawnan2;
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 428471b..d62cc42 100644 (file)
@@ -1,5 +1,18 @@
+/* dlarf.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static doublereal c_b4 = 1.;
@@ -15,6 +28,8 @@ static integer c__1 = 1;
     doublereal d__1;
 
     /* Local variables */
+    integer i__;
+    logical applyleft;
     extern /* Subroutine */ int dger_(integer *, integer *, doublereal *, 
            doublereal *, integer *, doublereal *, integer *, doublereal *, 
            integer *);
@@ -22,9 +37,12 @@ static integer c__1 = 1;
     extern /* Subroutine */ int dgemv_(char *, integer *, integer *, 
            doublereal *, doublereal *, integer *, doublereal *, integer *, 
            doublereal *, doublereal *, integer *);
+    integer lastc, lastv;
+    extern integer iladlc_(integer *, integer *, doublereal *, integer *), 
+           iladlr_(integer *, integer *, doublereal *, integer *);
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
@@ -86,6 +104,8 @@ static integer c__1 = 1;
 
 /*     .. Parameters .. */
 /*     .. */
+/*     .. Local Scalars .. */
+/*     .. */
 /*     .. External Subroutines .. */
 /*     .. */
 /*     .. External Functions .. */
@@ -100,39 +120,70 @@ static integer c__1 = 1;
     --work;
 
     /* Function Body */
-    if (lsame_(side, "L")) {
+    applyleft = lsame_(side, "L");
+    lastv = 0;
+    lastc = 0;
+    if (*tau != 0.) {
+/*     Set up variables for scanning V.  LASTV begins pointing to the end */
+/*     of V. */
+       if (applyleft) {
+           lastv = *m;
+       } else {
+           lastv = *n;
+       }
+       if (*incv > 0) {
+           i__ = (lastv - 1) * *incv + 1;
+       } else {
+           i__ = 1;
+       }
+/*     Look for the last non-zero row in V. */
+       while(lastv > 0 && v[i__] == 0.) {
+           --lastv;
+           i__ -= *incv;
+       }
+       if (applyleft) {
+/*     Scan for the last non-zero column in C(1:lastv,:). */
+           lastc = iladlc_(&lastv, n, &c__[c_offset], ldc);
+       } else {
+/*     Scan for the last non-zero row in C(:,1:lastv). */
+           lastc = iladlr_(m, &lastv, &c__[c_offset], ldc);
+       }
+    }
+/*     Note that lastc.eq.0 renders the BLAS operations null; no special */
+/*     case is needed at this level. */
+    if (applyleft) {
 
 /*        Form  H * C */
 
-       if (*tau != 0.) {
+       if (lastv > 0) {
 
-/*           w := C' * v */
+/*           w(1:lastc,1) := C(1:lastv,1:lastc)' * v(1:lastv,1) */
 
-           dgemv_("Transpose", m, n, &c_b4, &c__[c_offset], ldc, &v[1], incv, 
-                    &c_b5, &work[1], &c__1);
+           dgemv_("Transpose", &lastv, &lastc, &c_b4, &c__[c_offset], ldc, &
+                   v[1], incv, &c_b5, &work[1], &c__1);
 
-/*           C := C - v * w' */
+/*           C(1:lastv,1:lastc) := C(...) - v(1:lastv,1) * w(1:lastc,1)' */
 
            d__1 = -(*tau);
-           dger_(m, n, &d__1, &v[1], incv, &work[1], &c__1, &c__[c_offset], 
-                   ldc);
+           dger_(&lastv, &lastc, &d__1, &v[1], incv, &work[1], &c__1, &c__[
+                   c_offset], ldc);
        }
     } else {
 
 /*        Form  C * H */
 
-       if (*tau != 0.) {
+       if (lastv > 0) {
 
-/*           w := C * v */
+/*           w(1:lastc,1) := C(1:lastc,1:lastv) * v(1:lastv,1) */
 
-           dgemv_("No transpose", m, n, &c_b4, &c__[c_offset], ldc, &v[1]
-                   incv, &c_b5, &work[1], &c__1);
+           dgemv_("No transpose", &lastc, &lastv, &c_b4, &c__[c_offset], ldc
+                    &v[1], incv, &c_b5, &work[1], &c__1);
 
-/*           C := C - w * v' */
+/*           C(1:lastc,1:lastv) := C(...) - w(1:lastc,1) * v(1:lastv,1)' */
 
            d__1 = -(*tau);
-           dger_(m, n, &d__1, &work[1], &c__1, &v[1], incv, &c__[c_offset], 
-                   ldc);
+           dger_(&lastc, &lastv, &d__1, &work[1], &c__1, &v[1], incv, &c__[
+                   c_offset], ldc);
        }
     }
     return 0;
index 3d7135a..fa58ac3 100644 (file)
@@ -1,5 +1,18 @@
+/* dlarfb.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -21,14 +34,18 @@ static doublereal c_b25 = -1.;
            integer *, doublereal *, doublereal *, integer *, doublereal *, 
            integer *, doublereal *, doublereal *, integer *);
     extern logical lsame_(char *, char *);
+    integer lastc;
     extern /* Subroutine */ int dcopy_(integer *, doublereal *, integer *, 
            doublereal *, integer *), dtrmm_(char *, char *, char *, char *, 
            integer *, integer *, doublereal *, doublereal *, integer *, 
            doublereal *, integer *);
+    integer lastv;
+    extern integer iladlc_(integer *, integer *, doublereal *, integer *), 
+           iladlr_(integer *, integer *, doublereal *, integer *);
     char transt[1];
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
@@ -161,58 +178,64 @@ static doublereal c_b25 = -1.;
 /*              Form  H * C  or  H' * C  where  C = ( C1 ) */
 /*                                                  ( C2 ) */
 
+/* Computing MAX */
+               i__1 = *k, i__2 = iladlr_(m, k, &v[v_offset], ldv);
+               lastv = max(i__1,i__2);
+               lastc = iladlc_(&lastv, n, &c__[c_offset], ldc);
+
 /*              W := C' * V  =  (C1'*V1 + C2'*V2)  (stored in WORK) */
 
 /*              W := C1' */
 
                i__1 = *k;
                for (j = 1; j <= i__1; ++j) {
-                   dcopy_(n, &c__[j + c_dim1], ldc, &work[j * work_dim1 + 1], 
-                            &c__1);
+                   dcopy_(&lastc, &c__[j + c_dim1], ldc, &work[j * work_dim1 
+                           + 1], &c__1);
 /* L10: */
                }
 
 /*              W := W * V1 */
 
-               dtrmm_("Right", "Lower", "No transpose", "Unit", n, k, &c_b14, 
-                        &v[v_offset], ldv, &work[work_offset], ldwork);
-               if (*m > *k) {
+               dtrmm_("Right", "Lower", "No transpose", "Unit", &lastc, k, &
+                       c_b14, &v[v_offset], ldv, &work[work_offset], ldwork);
+               if (lastv > *k) {
 
 /*                 W := W + C2'*V2 */
 
-                   i__1 = *m - *k;
-                   dgemm_("Transpose", "No transpose", n, k, &i__1, &c_b14, &
-                           c__[*k + 1 + c_dim1], ldc, &v[*k + 1 + v_dim1], 
-                           ldv, &c_b14, &work[work_offset], ldwork);
+                   i__1 = lastv - *k;
+                   dgemm_("Transpose", "No transpose", &lastc, k, &i__1, &
+                           c_b14, &c__[*k + 1 + c_dim1], ldc, &v[*k + 1 + 
+                           v_dim1], ldv, &c_b14, &work[work_offset], ldwork);
                }
 
 /*              W := W * T'  or  W * T */
 
-               dtrmm_("Right", "Upper", transt, "Non-unit", n, k, &c_b14, &t[
-                       t_offset], ldt, &work[work_offset], ldwork);
+               dtrmm_("Right", "Upper", transt, "Non-unit", &lastc, k, &
+                       c_b14, &t[t_offset], ldt, &work[work_offset], ldwork);
 
 /*              C := C - V * W' */
 
-               if (*m > *k) {
+               if (lastv > *k) {
 
 /*                 C2 := C2 - V2 * W' */
 
-                   i__1 = *m - *k;
-                   dgemm_("No transpose", "Transpose", &i__1, n, k, &c_b25, &
-                           v[*k + 1 + v_dim1], ldv, &work[work_offset], 
-                           ldwork, &c_b14, &c__[*k + 1 + c_dim1], ldc);
+                   i__1 = lastv - *k;
+                   dgemm_("No transpose", "Transpose", &i__1, &lastc, k, &
+                           c_b25, &v[*k + 1 + v_dim1], ldv, &work[
+                           work_offset], ldwork, &c_b14, &c__[*k + 1 + 
+                           c_dim1], ldc);
                }
 
 /*              W := W * V1' */
 
-               dtrmm_("Right", "Lower", "Transpose", "Unit", n, k, &c_b14, &
-                       v[v_offset], ldv, &work[work_offset], ldwork);
+               dtrmm_("Right", "Lower", "Transpose", "Unit", &lastc, k, &
+                       c_b14, &v[v_offset], ldv, &work[work_offset], ldwork);
 
 /*              C1 := C1 - W' */
 
                i__1 = *k;
                for (j = 1; j <= i__1; ++j) {
-                   i__2 = *n;
+                   i__2 = lastc;
                    for (i__ = 1; i__ <= i__2; ++i__) {
                        c__[j + i__ * c_dim1] -= work[i__ + j * work_dim1];
 /* L20: */
@@ -224,27 +247,32 @@ static doublereal c_b25 = -1.;
 
 /*              Form  C * H  or  C * H'  where  C = ( C1  C2 ) */
 
+/* Computing MAX */
+               i__1 = *k, i__2 = iladlr_(n, k, &v[v_offset], ldv);
+               lastv = max(i__1,i__2);
+               lastc = iladlr_(m, &lastv, &c__[c_offset], ldc);
+
 /*              W := C * V  =  (C1*V1 + C2*V2)  (stored in WORK) */
 
 /*              W := C1 */
 
                i__1 = *k;
                for (j = 1; j <= i__1; ++j) {
-                   dcopy_(m, &c__[j * c_dim1 + 1], &c__1, &work[j * 
+                   dcopy_(&lastc, &c__[j * c_dim1 + 1], &c__1, &work[j * 
                            work_dim1 + 1], &c__1);
 /* L40: */
                }
 
 /*              W := W * V1 */
 
-               dtrmm_("Right", "Lower", "No transpose", "Unit", m, k, &c_b14, 
-                        &v[v_offset], ldv, &work[work_offset], ldwork);
-               if (*n > *k) {
+               dtrmm_("Right", "Lower", "No transpose", "Unit", &lastc, k, &
+                       c_b14, &v[v_offset], ldv, &work[work_offset], ldwork);
+               if (lastv > *k) {
 
 /*                 W := W + C2 * V2 */
 
-                   i__1 = *n - *k;
-                   dgemm_("No transpose", "No transpose", m, k, &i__1, &
+                   i__1 = lastv - *k;
+                   dgemm_("No transpose", "No transpose", &lastc, k, &i__1, &
                            c_b14, &c__[(*k + 1) * c_dim1 + 1], ldc, &v[*k + 
                            1 + v_dim1], ldv, &c_b14, &work[work_offset], 
                            ldwork);
@@ -252,31 +280,32 @@ static doublereal c_b25 = -1.;
 
 /*              W := W * T  or  W * T' */
 
-               dtrmm_("Right", "Upper", trans, "Non-unit", m, k, &c_b14, &t[
-                       t_offset], ldt, &work[work_offset], ldwork);
+               dtrmm_("Right", "Upper", trans, "Non-unit", &lastc, k, &c_b14, 
+                        &t[t_offset], ldt, &work[work_offset], ldwork);
 
 /*              C := C - W * V' */
 
-               if (*n > *k) {
+               if (lastv > *k) {
 
 /*                 C2 := C2 - W * V2' */
 
-                   i__1 = *n - *k;
-                   dgemm_("No transpose", "Transpose", m, &i__1, k, &c_b25, &
-                           work[work_offset], ldwork, &v[*k + 1 + v_dim1], 
-                           ldv, &c_b14, &c__[(*k + 1) * c_dim1 + 1], ldc);
+                   i__1 = lastv - *k;
+                   dgemm_("No transpose", "Transpose", &lastc, &i__1, k, &
+                           c_b25, &work[work_offset], ldwork, &v[*k + 1 + 
+                           v_dim1], ldv, &c_b14, &c__[(*k + 1) * c_dim1 + 1], 
+                            ldc);
                }
 
 /*              W := W * V1' */
 
-               dtrmm_("Right", "Lower", "Transpose", "Unit", m, k, &c_b14, &
-                       v[v_offset], ldv, &work[work_offset], ldwork);
+               dtrmm_("Right", "Lower", "Transpose", "Unit", &lastc, k, &
+                       c_b14, &v[v_offset], ldv, &work[work_offset], ldwork);
 
 /*              C1 := C1 - W */
 
                i__1 = *k;
                for (j = 1; j <= i__1; ++j) {
-                   i__2 = *m;
+                   i__2 = lastc;
                    for (i__ = 1; i__ <= i__2; ++i__) {
                        c__[i__ + j * c_dim1] -= work[i__ + j * work_dim1];
 /* L50: */
@@ -296,63 +325,67 @@ static doublereal c_b25 = -1.;
 /*              Form  H * C  or  H' * C  where  C = ( C1 ) */
 /*                                                  ( C2 ) */
 
+/* Computing MAX */
+               i__1 = *k, i__2 = iladlr_(m, k, &v[v_offset], ldv);
+               lastv = max(i__1,i__2);
+               lastc = iladlc_(&lastv, n, &c__[c_offset], ldc);
+
 /*              W := C' * V  =  (C1'*V1 + C2'*V2)  (stored in WORK) */
 
 /*              W := C2' */
 
                i__1 = *k;
                for (j = 1; j <= i__1; ++j) {
-                   dcopy_(n, &c__[*m - *k + j + c_dim1], ldc, &work[j * 
-                           work_dim1 + 1], &c__1);
+                   dcopy_(&lastc, &c__[lastv - *k + j + c_dim1], ldc, &work[
+                           j * work_dim1 + 1], &c__1);
 /* L70: */
                }
 
 /*              W := W * V2 */
 
-               dtrmm_("Right", "Upper", "No transpose", "Unit", n, k, &c_b14, 
-                        &v[*m - *k + 1 + v_dim1], ldv, &work[work_offset], 
-                       ldwork);
-               if (*m > *k) {
+               dtrmm_("Right", "Upper", "No transpose", "Unit", &lastc, k, &
+                       c_b14, &v[lastv - *k + 1 + v_dim1], ldv, &work[
+                       work_offset], ldwork);
+               if (lastv > *k) {
 
 /*                 W := W + C1'*V1 */
 
-                   i__1 = *m - *k;
-                   dgemm_("Transpose", "No transpose", n, k, &i__1, &c_b14, &
-                           c__[c_offset], ldc, &v[v_offset], ldv, &c_b14, &
-                           work[work_offset], ldwork);
+                   i__1 = lastv - *k;
+                   dgemm_("Transpose", "No transpose", &lastc, k, &i__1, &
+                           c_b14, &c__[c_offset], ldc, &v[v_offset], ldv, &
+                           c_b14, &work[work_offset], ldwork);
                }
 
 /*              W := W * T'  or  W * T */
 
-               dtrmm_("Right", "Lower", transt, "Non-unit", n, k, &c_b14, &t[
-                       t_offset], ldt, &work[work_offset], ldwork);
+               dtrmm_("Right", "Lower", transt, "Non-unit", &lastc, k, &
+                       c_b14, &t[t_offset], ldt, &work[work_offset], ldwork);
 
 /*              C := C - V * W' */
 
-               if (*m > *k) {
+               if (lastv > *k) {
 
 /*                 C1 := C1 - V1 * W' */
 
-                   i__1 = *m - *k;
-                   dgemm_("No transpose", "Transpose", &i__1, n, k, &c_b25, &
-                           v[v_offset], ldv, &work[work_offset], ldwork, &
-                           c_b14, &c__[c_offset], ldc)
-                           ;
+                   i__1 = lastv - *k;
+                   dgemm_("No transpose", "Transpose", &i__1, &lastc, k, &
+                           c_b25, &v[v_offset], ldv, &work[work_offset], 
+                           ldwork, &c_b14, &c__[c_offset], ldc);
                }
 
 /*              W := W * V2' */
 
-               dtrmm_("Right", "Upper", "Transpose", "Unit", n, k, &c_b14, &
-                       v[*m - *k + 1 + v_dim1], ldv, &work[work_offset], 
-                       ldwork);
+               dtrmm_("Right", "Upper", "Transpose", "Unit", &lastc, k, &
+                       c_b14, &v[lastv - *k + 1 + v_dim1], ldv, &work[
+                       work_offset], ldwork);
 
 /*              C2 := C2 - W' */
 
                i__1 = *k;
                for (j = 1; j <= i__1; ++j) {
-                   i__2 = *n;
+                   i__2 = lastc;
                    for (i__ = 1; i__ <= i__2; ++i__) {
-                       c__[*m - *k + j + i__ * c_dim1] -= work[i__ + j * 
+                       c__[lastv - *k + j + i__ * c_dim1] -= work[i__ + j * 
                                work_dim1];
 /* L80: */
                    }
@@ -363,64 +396,68 @@ static doublereal c_b25 = -1.;
 
 /*              Form  C * H  or  C * H'  where  C = ( C1  C2 ) */
 
+/* Computing MAX */
+               i__1 = *k, i__2 = iladlr_(n, k, &v[v_offset], ldv);
+               lastv = max(i__1,i__2);
+               lastc = iladlr_(m, &lastv, &c__[c_offset], ldc);
+
 /*              W := C * V  =  (C1*V1 + C2*V2)  (stored in WORK) */
 
 /*              W := C2 */
 
                i__1 = *k;
                for (j = 1; j <= i__1; ++j) {
-                   dcopy_(m, &c__[(*n - *k + j) * c_dim1 + 1], &c__1, &work[
-                           j * work_dim1 + 1], &c__1);
+                   dcopy_(&lastc, &c__[(*n - *k + j) * c_dim1 + 1], &c__1, &
+                           work[j * work_dim1 + 1], &c__1);
 /* L100: */
                }
 
 /*              W := W * V2 */
 
-               dtrmm_("Right", "Upper", "No transpose", "Unit", m, k, &c_b14, 
-                        &v[*n - *k + 1 + v_dim1], ldv, &work[work_offset], 
-                       ldwork);
-               if (*n > *k) {
+               dtrmm_("Right", "Upper", "No transpose", "Unit", &lastc, k, &
+                       c_b14, &v[lastv - *k + 1 + v_dim1], ldv, &work[
+                       work_offset], ldwork);
+               if (lastv > *k) {
 
 /*                 W := W + C1 * V1 */
 
-                   i__1 = *n - *k;
-                   dgemm_("No transpose", "No transpose", m, k, &i__1, &
+                   i__1 = lastv - *k;
+                   dgemm_("No transpose", "No transpose", &lastc, k, &i__1, &
                            c_b14, &c__[c_offset], ldc, &v[v_offset], ldv, &
                            c_b14, &work[work_offset], ldwork);
                }
 
 /*              W := W * T  or  W * T' */
 
-               dtrmm_("Right", "Lower", trans, "Non-unit", m, k, &c_b14, &t[
-                       t_offset], ldt, &work[work_offset], ldwork);
+               dtrmm_("Right", "Lower", trans, "Non-unit", &lastc, k, &c_b14, 
+                        &t[t_offset], ldt, &work[work_offset], ldwork);
 
 /*              C := C - W * V' */
 
-               if (*n > *k) {
+               if (lastv > *k) {
 
 /*                 C1 := C1 - W * V1' */
 
-                   i__1 = *n - *k;
-                   dgemm_("No transpose", "Transpose", m, &i__1, k, &c_b25, &
-                           work[work_offset], ldwork, &v[v_offset], ldv, &
-                           c_b14, &c__[c_offset], ldc)
-                           ;
+                   i__1 = lastv - *k;
+                   dgemm_("No transpose", "Transpose", &lastc, &i__1, k, &
+                           c_b25, &work[work_offset], ldwork, &v[v_offset], 
+                           ldv, &c_b14, &c__[c_offset], ldc);
                }
 
 /*              W := W * V2' */
 
-               dtrmm_("Right", "Upper", "Transpose", "Unit", m, k, &c_b14, &
-                       v[*n - *k + 1 + v_dim1], ldv, &work[work_offset], 
-                       ldwork);
+               dtrmm_("Right", "Upper", "Transpose", "Unit", &lastc, k, &
+                       c_b14, &v[lastv - *k + 1 + v_dim1], ldv, &work[
+                       work_offset], ldwork);
 
 /*              C2 := C2 - W */
 
                i__1 = *k;
                for (j = 1; j <= i__1; ++j) {
-                   i__2 = *m;
+                   i__2 = lastc;
                    for (i__ = 1; i__ <= i__2; ++i__) {
-                       c__[i__ + (*n - *k + j) * c_dim1] -= work[i__ + j * 
-                               work_dim1];
+                       c__[i__ + (lastv - *k + j) * c_dim1] -= work[i__ + j *
+                                work_dim1];
 /* L110: */
                    }
 /* L120: */
@@ -440,58 +477,64 @@ static doublereal c_b25 = -1.;
 /*              Form  H * C  or  H' * C  where  C = ( C1 ) */
 /*                                                  ( C2 ) */
 
+/* Computing MAX */
+               i__1 = *k, i__2 = iladlc_(k, m, &v[v_offset], ldv);
+               lastv = max(i__1,i__2);
+               lastc = iladlc_(&lastv, n, &c__[c_offset], ldc);
+
 /*              W := C' * V'  =  (C1'*V1' + C2'*V2') (stored in WORK) */
 
 /*              W := C1' */
 
                i__1 = *k;
                for (j = 1; j <= i__1; ++j) {
-                   dcopy_(n, &c__[j + c_dim1], ldc, &work[j * work_dim1 + 1], 
-                            &c__1);
+                   dcopy_(&lastc, &c__[j + c_dim1], ldc, &work[j * work_dim1 
+                           + 1], &c__1);
 /* L130: */
                }
 
 /*              W := W * V1' */
 
-               dtrmm_("Right", "Upper", "Transpose", "Unit", n, k, &c_b14, &
-                       v[v_offset], ldv, &work[work_offset], ldwork);
-               if (*m > *k) {
+               dtrmm_("Right", "Upper", "Transpose", "Unit", &lastc, k, &
+                       c_b14, &v[v_offset], ldv, &work[work_offset], ldwork);
+               if (lastv > *k) {
 
 /*                 W := W + C2'*V2' */
 
-                   i__1 = *m - *k;
-                   dgemm_("Transpose", "Transpose", n, k, &i__1, &c_b14, &
-                           c__[*k + 1 + c_dim1], ldc, &v[(*k + 1) * v_dim1 + 
-                           1], ldv, &c_b14, &work[work_offset], ldwork);
+                   i__1 = lastv - *k;
+                   dgemm_("Transpose", "Transpose", &lastc, k, &i__1, &c_b14, 
+                            &c__[*k + 1 + c_dim1], ldc, &v[(*k + 1) * v_dim1 
+                           1], ldv, &c_b14, &work[work_offset], ldwork);
                }
 
 /*              W := W * T'  or  W * T */
 
-               dtrmm_("Right", "Upper", transt, "Non-unit", n, k, &c_b14, &t[
-                       t_offset], ldt, &work[work_offset], ldwork);
+               dtrmm_("Right", "Upper", transt, "Non-unit", &lastc, k, &
+                       c_b14, &t[t_offset], ldt, &work[work_offset], ldwork);
 
 /*              C := C - V' * W' */
 
-               if (*m > *k) {
+               if (lastv > *k) {
 
 /*                 C2 := C2 - V2' * W' */
 
-                   i__1 = *m - *k;
-                   dgemm_("Transpose", "Transpose", &i__1, n, k, &c_b25, &v[(
-                           *k + 1) * v_dim1 + 1], ldv, &work[work_offset], 
-                           ldwork, &c_b14, &c__[*k + 1 + c_dim1], ldc);
+                   i__1 = lastv - *k;
+                   dgemm_("Transpose", "Transpose", &i__1, &lastc, k, &c_b25, 
+                            &v[(*k + 1) * v_dim1 + 1], ldv, &work[
+                           work_offset], ldwork, &c_b14, &c__[*k + 1 + 
+                           c_dim1], ldc);
                }
 
 /*              W := W * V1 */
 
-               dtrmm_("Right", "Upper", "No transpose", "Unit", n, k, &c_b14, 
-                        &v[v_offset], ldv, &work[work_offset], ldwork);
+               dtrmm_("Right", "Upper", "No transpose", "Unit", &lastc, k, &
+                       c_b14, &v[v_offset], ldv, &work[work_offset], ldwork);
 
 /*              C1 := C1 - W' */
 
                i__1 = *k;
                for (j = 1; j <= i__1; ++j) {
-                   i__2 = *n;
+                   i__2 = lastc;
                    for (i__ = 1; i__ <= i__2; ++i__) {
                        c__[j + i__ * c_dim1] -= work[i__ + j * work_dim1];
 /* L140: */
@@ -503,45 +546,50 @@ static doublereal c_b25 = -1.;
 
 /*              Form  C * H  or  C * H'  where  C = ( C1  C2 ) */
 
+/* Computing MAX */
+               i__1 = *k, i__2 = iladlc_(k, n, &v[v_offset], ldv);
+               lastv = max(i__1,i__2);
+               lastc = iladlr_(m, &lastv, &c__[c_offset], ldc);
+
 /*              W := C * V'  =  (C1*V1' + C2*V2')  (stored in WORK) */
 
 /*              W := C1 */
 
                i__1 = *k;
                for (j = 1; j <= i__1; ++j) {
-                   dcopy_(m, &c__[j * c_dim1 + 1], &c__1, &work[j * 
+                   dcopy_(&lastc, &c__[j * c_dim1 + 1], &c__1, &work[j * 
                            work_dim1 + 1], &c__1);
 /* L160: */
                }
 
 /*              W := W * V1' */
 
-               dtrmm_("Right", "Upper", "Transpose", "Unit", m, k, &c_b14, &
-                       v[v_offset], ldv, &work[work_offset], ldwork);
-               if (*n > *k) {
+               dtrmm_("Right", "Upper", "Transpose", "Unit", &lastc, k, &
+                       c_b14, &v[v_offset], ldv, &work[work_offset], ldwork);
+               if (lastv > *k) {
 
 /*                 W := W + C2 * V2' */
 
-                   i__1 = *n - *k;
-                   dgemm_("No transpose", "Transpose", m, k, &i__1, &c_b14, &
-                           c__[(*k + 1) * c_dim1 + 1], ldc, &v[(*k + 1) * 
-                           v_dim1 + 1], ldv, &c_b14, &work[work_offset], 
-                           ldwork);
+                   i__1 = lastv - *k;
+                   dgemm_("No transpose", "Transpose", &lastc, k, &i__1, &
+                           c_b14, &c__[(*k + 1) * c_dim1 + 1], ldc, &v[(*k + 
+                           1) * v_dim1 + 1], ldv, &c_b14, &work[work_offset], 
+                            ldwork);
                }
 
 /*              W := W * T  or  W * T' */
 
-               dtrmm_("Right", "Upper", trans, "Non-unit", m, k, &c_b14, &t[
-                       t_offset], ldt, &work[work_offset], ldwork);
+               dtrmm_("Right", "Upper", trans, "Non-unit", &lastc, k, &c_b14, 
+                        &t[t_offset], ldt, &work[work_offset], ldwork);
 
 /*              C := C - W * V */
 
-               if (*n > *k) {
+               if (lastv > *k) {
 
 /*                 C2 := C2 - W * V2 */
 
-                   i__1 = *n - *k;
-                   dgemm_("No transpose", "No transpose", m, &i__1, k, &
+                   i__1 = lastv - *k;
+                   dgemm_("No transpose", "No transpose", &lastc, &i__1, k, &
                            c_b25, &work[work_offset], ldwork, &v[(*k + 1) * 
                            v_dim1 + 1], ldv, &c_b14, &c__[(*k + 1) * c_dim1 
                            + 1], ldc);
@@ -549,14 +597,14 @@ static doublereal c_b25 = -1.;
 
 /*              W := W * V1 */
 
-               dtrmm_("Right", "Upper", "No transpose", "Unit", m, k, &c_b14, 
-                        &v[v_offset], ldv, &work[work_offset], ldwork);
+               dtrmm_("Right", "Upper", "No transpose", "Unit", &lastc, k, &
+                       c_b14, &v[v_offset], ldv, &work[work_offset], ldwork);
 
 /*              C1 := C1 - W */
 
                i__1 = *k;
                for (j = 1; j <= i__1; ++j) {
-                   i__2 = *m;
+                   i__2 = lastc;
                    for (i__ = 1; i__ <= i__2; ++i__) {
                        c__[i__ + j * c_dim1] -= work[i__ + j * work_dim1];
 /* L170: */
@@ -576,62 +624,67 @@ static doublereal c_b25 = -1.;
 /*              Form  H * C  or  H' * C  where  C = ( C1 ) */
 /*                                                  ( C2 ) */
 
+/* Computing MAX */
+               i__1 = *k, i__2 = iladlc_(k, m, &v[v_offset], ldv);
+               lastv = max(i__1,i__2);
+               lastc = iladlc_(&lastv, n, &c__[c_offset], ldc);
+
 /*              W := C' * V'  =  (C1'*V1' + C2'*V2') (stored in WORK) */
 
 /*              W := C2' */
 
                i__1 = *k;
                for (j = 1; j <= i__1; ++j) {
-                   dcopy_(n, &c__[*m - *k + j + c_dim1], ldc, &work[j * 
-                           work_dim1 + 1], &c__1);
+                   dcopy_(&lastc, &c__[lastv - *k + j + c_dim1], ldc, &work[
+                           j * work_dim1 + 1], &c__1);
 /* L190: */
                }
 
 /*              W := W * V2' */
 
-               dtrmm_("Right", "Lower", "Transpose", "Unit", n, k, &c_b14, &
-                       v[(*m - *k + 1) * v_dim1 + 1], ldv, &work[work_offset]
-, ldwork);
-               if (*m > *k) {
+               dtrmm_("Right", "Lower", "Transpose", "Unit", &lastc, k, &
+                       c_b14, &v[(lastv - *k + 1) * v_dim1 + 1], ldv, &work[
+                       work_offset], ldwork);
+               if (lastv > *k) {
 
 /*                 W := W + C1'*V1' */
 
-                   i__1 = *m - *k;
-                   dgemm_("Transpose", "Transpose", n, k, &i__1, &c_b14, &
-                           c__[c_offset], ldc, &v[v_offset], ldv, &c_b14, &
+                   i__1 = lastv - *k;
+                   dgemm_("Transpose", "Transpose", &lastc, k, &i__1, &c_b14, 
+                            &c__[c_offset], ldc, &v[v_offset], ldv, &c_b14, &
                            work[work_offset], ldwork);
                }
 
 /*              W := W * T'  or  W * T */
 
-               dtrmm_("Right", "Lower", transt, "Non-unit", n, k, &c_b14, &t[
-                       t_offset], ldt, &work[work_offset], ldwork);
+               dtrmm_("Right", "Lower", transt, "Non-unit", &lastc, k, &
+                       c_b14, &t[t_offset], ldt, &work[work_offset], ldwork);
 
 /*              C := C - V' * W' */
 
-               if (*m > *k) {
+               if (lastv > *k) {
 
 /*                 C1 := C1 - V1' * W' */
 
-                   i__1 = *m - *k;
-                   dgemm_("Transpose", "Transpose", &i__1, n, k, &c_b25, &v[
-                           v_offset], ldv, &work[work_offset], ldwork, &
+                   i__1 = lastv - *k;
+                   dgemm_("Transpose", "Transpose", &i__1, &lastc, k, &c_b25, 
+                            &v[v_offset], ldv, &work[work_offset], ldwork, &
                            c_b14, &c__[c_offset], ldc);
                }
 
 /*              W := W * V2 */
 
-               dtrmm_("Right", "Lower", "No transpose", "Unit", n, k, &c_b14, 
-                        &v[(*m - *k + 1) * v_dim1 + 1], ldv, &work[
+               dtrmm_("Right", "Lower", "No transpose", "Unit", &lastc, k, &
+                       c_b14, &v[(lastv - *k + 1) * v_dim1 + 1], ldv, &work[
                        work_offset], ldwork);
 
 /*              C2 := C2 - W' */
 
                i__1 = *k;
                for (j = 1; j <= i__1; ++j) {
-                   i__2 = *n;
+                   i__2 = lastc;
                    for (i__ = 1; i__ <= i__2; ++i__) {
-                       c__[*m - *k + j + i__ * c_dim1] -= work[i__ + j * 
+                       c__[lastv - *k + j + i__ * c_dim1] -= work[i__ + j * 
                                work_dim1];
 /* L200: */
                    }
@@ -642,63 +695,68 @@ static doublereal c_b25 = -1.;
 
 /*              Form  C * H  or  C * H'  where  C = ( C1  C2 ) */
 
+/* Computing MAX */
+               i__1 = *k, i__2 = iladlc_(k, n, &v[v_offset], ldv);
+               lastv = max(i__1,i__2);
+               lastc = iladlr_(m, &lastv, &c__[c_offset], ldc);
+
 /*              W := C * V'  =  (C1*V1' + C2*V2')  (stored in WORK) */
 
 /*              W := C2 */
 
                i__1 = *k;
                for (j = 1; j <= i__1; ++j) {
-                   dcopy_(m, &c__[(*n - *k + j) * c_dim1 + 1], &c__1, &work[
-                           j * work_dim1 + 1], &c__1);
+                   dcopy_(&lastc, &c__[(lastv - *k + j) * c_dim1 + 1], &c__1, 
+                            &work[j * work_dim1 + 1], &c__1);
 /* L220: */
                }
 
 /*              W := W * V2' */
 
-               dtrmm_("Right", "Lower", "Transpose", "Unit", m, k, &c_b14, &
-                       v[(*n - *k + 1) * v_dim1 + 1], ldv, &work[work_offset]
-, ldwork);
-               if (*n > *k) {
+               dtrmm_("Right", "Lower", "Transpose", "Unit", &lastc, k, &
+                       c_b14, &v[(lastv - *k + 1) * v_dim1 + 1], ldv, &work[
+                       work_offset], ldwork);
+               if (lastv > *k) {
 
 /*                 W := W + C1 * V1' */
 
-                   i__1 = *n - *k;
-                   dgemm_("No transpose", "Transpose", m, k, &i__1, &c_b14, &
-                           c__[c_offset], ldc, &v[v_offset], ldv, &c_b14, &
-                           work[work_offset], ldwork);
+                   i__1 = lastv - *k;
+                   dgemm_("No transpose", "Transpose", &lastc, k, &i__1, &
+                           c_b14, &c__[c_offset], ldc, &v[v_offset], ldv, &
+                           c_b14, &work[work_offset], ldwork);
                }
 
 /*              W := W * T  or  W * T' */
 
-               dtrmm_("Right", "Lower", trans, "Non-unit", m, k, &c_b14, &t[
-                       t_offset], ldt, &work[work_offset], ldwork);
+               dtrmm_("Right", "Lower", trans, "Non-unit", &lastc, k, &c_b14, 
+                        &t[t_offset], ldt, &work[work_offset], ldwork);
 
 /*              C := C - W * V */
 
-               if (*n > *k) {
+               if (lastv > *k) {
 
 /*                 C1 := C1 - W * V1 */
 
-                   i__1 = *n - *k;
-                   dgemm_("No transpose", "No transpose", m, &i__1, k, &
+                   i__1 = lastv - *k;
+                   dgemm_("No transpose", "No transpose", &lastc, &i__1, k, &
                            c_b25, &work[work_offset], ldwork, &v[v_offset], 
                            ldv, &c_b14, &c__[c_offset], ldc);
                }
 
 /*              W := W * V2 */
 
-               dtrmm_("Right", "Lower", "No transpose", "Unit", m, k, &c_b14, 
-                        &v[(*n - *k + 1) * v_dim1 + 1], ldv, &work[
+               dtrmm_("Right", "Lower", "No transpose", "Unit", &lastc, k, &
+                       c_b14, &v[(lastv - *k + 1) * v_dim1 + 1], ldv, &work[
                        work_offset], ldwork);
 
 /*              C1 := C1 - W */
 
                i__1 = *k;
                for (j = 1; j <= i__1; ++j) {
-                   i__2 = *m;
+                   i__2 = lastc;
                    for (i__ = 1; i__ <= i__2; ++i__) {
-                       c__[i__ + (*n - *k + j) * c_dim1] -= work[i__ + j * 
-                               work_dim1];
+                       c__[i__ + (lastv - *k + j) * c_dim1] -= work[i__ + j *
+                                work_dim1];
 /* L230: */
                    }
 /* L240: */
index 920eab7..361b6f1 100644 (file)
@@ -1,5 +1,18 @@
+/* dlarfg.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int dlarfg_(integer *n, doublereal *alpha, doublereal *x, 
        integer *incx, doublereal *tau)
 {
@@ -21,7 +34,7 @@
     doublereal safmin, rsafmn;
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
        d__1 = dlapy2_(alpha, &xnorm);
        beta = -d_sign(&d__1, alpha);
        safmin = dlamch_("S") / dlamch_("E");
+       knt = 0;
        if (abs(beta) < safmin) {
 
 /*           XNORM, BETA may be inaccurate; scale X and recompute them */
 
            rsafmn = 1. / safmin;
-           knt = 0;
 L10:
            ++knt;
            i__1 = *n - 1;
@@ -134,26 +147,20 @@ L10:
            xnorm = dnrm2_(&i__1, &x[1], incx);
            d__1 = dlapy2_(alpha, &xnorm);
            beta = -d_sign(&d__1, alpha);
-           *tau = (beta - *alpha) / beta;
-           i__1 = *n - 1;
-           d__1 = 1. / (*alpha - beta);
-           dscal_(&i__1, &d__1, &x[1], incx);
+       }
+       *tau = (beta - *alpha) / beta;
+       i__1 = *n - 1;
+       d__1 = 1. / (*alpha - beta);
+       dscal_(&i__1, &d__1, &x[1], incx);
 
-/*           If ALPHA is subnormal, it may lose relative accuracy */
+/*        If ALPHA is subnormal, it may lose relative accuracy */
 
-           *alpha = beta;
-           i__1 = knt;
-           for (j = 1; j <= i__1; ++j) {
-               *alpha *= safmin;
+       i__1 = knt;
+       for (j = 1; j <= i__1; ++j) {
+           beta *= safmin;
 /* L20: */
-           }
-       } else {
-           *tau = (beta - *alpha) / beta;
-           i__1 = *n - 1;
-           d__1 = 1. / (*alpha - beta);
-           dscal_(&i__1, &d__1, &x[1], incx);
-           *alpha = beta;
        }
+       *alpha = beta;
     }
 
     return 0;
diff --git a/3rdparty/lapack/dlarfp.c b/3rdparty/lapack/dlarfp.c
new file mode 100644 (file)
index 0000000..1fdd7ef
--- /dev/null
@@ -0,0 +1,192 @@
+/* dlarfp.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
+#include "clapack.h"
+
+
+/* Subroutine */ int dlarfp_(integer *n, doublereal *alpha, doublereal *x, 
+       integer *incx, doublereal *tau)
+{
+    /* System generated locals */
+    integer i__1;
+    doublereal d__1;
+
+    /* Builtin functions */
+    double d_sign(doublereal *, doublereal *);
+
+    /* Local variables */
+    integer j, knt;
+    doublereal beta;
+    extern doublereal dnrm2_(integer *, doublereal *, integer *);
+    extern /* Subroutine */ int dscal_(integer *, doublereal *, doublereal *, 
+           integer *);
+    doublereal xnorm;
+    extern doublereal dlapy2_(doublereal *, doublereal *), dlamch_(char *);
+    doublereal safmin, rsafmn;
+
+
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
+/*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
+/*     November 2006 */
+
+/*     .. Scalar Arguments .. */
+/*     .. */
+/*     .. Array Arguments .. */
+/*     .. */
+
+/*  Purpose */
+/*  ======= */
+
+/*  DLARFP generates a real elementary reflector H of order n, such */
+/*  that */
+
+/*        H * ( alpha ) = ( beta ),   H' * H = I. */
+/*            (   x   )   (   0  ) */
+
+/*  where alpha and beta are scalars, beta is non-negative, and x is */
+/*  an (n-1)-element real vector.  H is represented in the form */
+
+/*        H = I - tau * ( 1 ) * ( 1 v' ) , */
+/*                      ( v ) */
+
+/*  where tau is a real scalar and v is a real (n-1)-element */
+/*  vector. */
+
+/*  If the elements of x are all zero, then tau = 0 and H is taken to be */
+/*  the unit matrix. */
+
+/*  Otherwise  1 <= tau <= 2. */
+
+/*  Arguments */
+/*  ========= */
+
+/*  N       (input) INTEGER */
+/*          The order of the elementary reflector. */
+
+/*  ALPHA   (input/output) DOUBLE PRECISION */
+/*          On entry, the value alpha. */
+/*          On exit, it is overwritten with the value beta. */
+
+/*  X       (input/output) DOUBLE PRECISION array, dimension */
+/*                         (1+(N-2)*abs(INCX)) */
+/*          On entry, the vector x. */
+/*          On exit, it is overwritten with the vector v. */
+
+/*  INCX    (input) INTEGER */
+/*          The increment between elements of X. INCX > 0. */
+
+/*  TAU     (output) DOUBLE PRECISION */
+/*          The value tau. */
+
+/*  ===================================================================== */
+
+/*     .. Parameters .. */
+/*     .. */
+/*     .. Local Scalars .. */
+/*     .. */
+/*     .. External Functions .. */
+/*     .. */
+/*     .. Intrinsic Functions .. */
+/*     .. */
+/*     .. External Subroutines .. */
+/*     .. */
+/*     .. Executable Statements .. */
+
+    /* Parameter adjustments */
+    --x;
+
+    /* Function Body */
+    if (*n <= 0) {
+       *tau = 0.;
+       return 0;
+    }
+
+    i__1 = *n - 1;
+    xnorm = dnrm2_(&i__1, &x[1], incx);
+
+    if (xnorm == 0.) {
+
+/*        H  =  [+/-1, 0; I], sign chosen so ALPHA >= 0 */
+
+       if (*alpha >= 0.) {
+/*           When TAU.eq.ZERO, the vector is special-cased to be */
+/*           all zeros in the application routines.  We do not need */
+/*           to clear it. */
+           *tau = 0.;
+       } else {
+/*           However, the application routines rely on explicit */
+/*           zero checks when TAU.ne.ZERO, and we must clear X. */
+           *tau = 2.;
+           i__1 = *n - 1;
+           for (j = 1; j <= i__1; ++j) {
+               x[(j - 1) * *incx + 1] = 0.;
+           }
+           *alpha = -(*alpha);
+       }
+    } else {
+
+/*        general case */
+
+       d__1 = dlapy2_(alpha, &xnorm);
+       beta = d_sign(&d__1, alpha);
+       safmin = dlamch_("S") / dlamch_("E");
+       knt = 0;
+       if (abs(beta) < safmin) {
+
+/*           XNORM, BETA may be inaccurate; scale X and recompute them */
+
+           rsafmn = 1. / safmin;
+L10:
+           ++knt;
+           i__1 = *n - 1;
+           dscal_(&i__1, &rsafmn, &x[1], incx);
+           beta *= rsafmn;
+           *alpha *= rsafmn;
+           if (abs(beta) < safmin) {
+               goto L10;
+           }
+
+/*           New BETA is at most 1, at least SAFMIN */
+
+           i__1 = *n - 1;
+           xnorm = dnrm2_(&i__1, &x[1], incx);
+           d__1 = dlapy2_(alpha, &xnorm);
+           beta = d_sign(&d__1, alpha);
+       }
+       *alpha += beta;
+       if (beta < 0.) {
+           beta = -beta;
+           *tau = -(*alpha) / beta;
+       } else {
+           *alpha = xnorm * (xnorm / *alpha);
+           *tau = *alpha / beta;
+           *alpha = -(*alpha);
+       }
+       i__1 = *n - 1;
+       d__1 = 1. / *alpha;
+       dscal_(&i__1, &d__1, &x[1], incx);
+
+/*        If BETA is subnormal, it may lose relative accuracy */
+
+       i__1 = knt;
+       for (j = 1; j <= i__1; ++j) {
+           beta *= safmin;
+/* L20: */
+       }
+       *alpha = beta;
+    }
+
+    return 0;
+
+/*     End of DLARFP */
+
+} /* dlarfp_ */
index 62f8753..95ace4c 100644 (file)
@@ -1,5 +1,18 @@
+/* dlarft.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -14,17 +27,18 @@ static doublereal c_b8 = 0.;
     doublereal d__1;
 
     /* Local variables */
-    integer i__, j;
+    integer i__, j, prevlastv;
     doublereal vii;
     extern logical lsame_(char *, char *);
     extern /* Subroutine */ int dgemv_(char *, integer *, integer *, 
            doublereal *, doublereal *, integer *, doublereal *, integer *, 
-           doublereal *, doublereal *, integer *), dtrmv_(char *, 
-           char *, char *, integer *, doublereal *, integer *, doublereal *, 
-           integer *);
+           doublereal *, doublereal *, integer *);
+    integer lastv;
+    extern /* Subroutine */ int dtrmv_(char *, char *, char *, integer *, 
+           doublereal *, integer *, doublereal *, integer *);
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
@@ -150,8 +164,10 @@ static doublereal c_b8 = 0.;
     }
 
     if (lsame_(direct, "F")) {
+       prevlastv = *n;
        i__1 = *k;
        for (i__ = 1; i__ <= i__1; ++i__) {
+           prevlastv = max(i__,prevlastv);
            if (tau[i__] == 0.) {
 
 /*              H(i)  =  I */
@@ -168,21 +184,37 @@ static doublereal c_b8 = 0.;
                vii = v[i__ + i__ * v_dim1];
                v[i__ + i__ * v_dim1] = 1.;
                if (lsame_(storev, "C")) {
+/*                 Skip any trailing zeros. */
+                   i__2 = i__ + 1;
+                   for (lastv = *n; lastv >= i__2; --lastv) {
+                       if (v[lastv + i__ * v_dim1] != 0.) {
+                           break;
+                       }
+                   }
+                   j = min(lastv,prevlastv);
 
-/*                 T(1:i-1,i) := - tau(i) * V(i:n,1:i-1)' * V(i:n,i) */
+/*                 T(1:i-1,i) := - tau(i) * V(i:j,1:i-1)' * V(i:j,i) */
 
-                   i__2 = *n - i__ + 1;
+                   i__2 = j - i__ + 1;
                    i__3 = i__ - 1;
                    d__1 = -tau[i__];
                    dgemv_("Transpose", &i__2, &i__3, &d__1, &v[i__ + v_dim1], 
                             ldv, &v[i__ + i__ * v_dim1], &c__1, &c_b8, &t[
                            i__ * t_dim1 + 1], &c__1);
                } else {
+/*                 Skip any trailing zeros. */
+                   i__2 = i__ + 1;
+                   for (lastv = *n; lastv >= i__2; --lastv) {
+                       if (v[i__ + lastv * v_dim1] != 0.) {
+                           break;
+                       }
+                   }
+                   j = min(lastv,prevlastv);
 
-/*                 T(1:i-1,i) := - tau(i) * V(1:i-1,i:n) * V(i,i:n)' */
+/*                 T(1:i-1,i) := - tau(i) * V(1:i-1,i:j) * V(i,i:j)' */
 
                    i__2 = i__ - 1;
-                   i__3 = *n - i__ + 1;
+                   i__3 = j - i__ + 1;
                    d__1 = -tau[i__];
                    dgemv_("No transpose", &i__2, &i__3, &d__1, &v[i__ * 
                            v_dim1 + 1], ldv, &v[i__ + i__ * v_dim1], ldv, &
@@ -196,10 +228,16 @@ static doublereal c_b8 = 0.;
                dtrmv_("Upper", "No transpose", "Non-unit", &i__2, &t[
                        t_offset], ldt, &t[i__ * t_dim1 + 1], &c__1);
                t[i__ + i__ * t_dim1] = tau[i__];
+               if (i__ > 1) {
+                   prevlastv = max(prevlastv,lastv);
+               } else {
+                   prevlastv = lastv;
+               }
            }
 /* L20: */
        }
     } else {
+       prevlastv = 1;
        for (i__ = *k; i__ >= 1; --i__) {
            if (tau[i__] == 0.) {
 
@@ -218,31 +256,47 @@ static doublereal c_b8 = 0.;
                    if (lsame_(storev, "C")) {
                        vii = v[*n - *k + i__ + i__ * v_dim1];
                        v[*n - *k + i__ + i__ * v_dim1] = 1.;
+/*                    Skip any leading zeros. */
+                       i__1 = i__ - 1;
+                       for (lastv = 1; lastv <= i__1; ++lastv) {
+                           if (v[lastv + i__ * v_dim1] != 0.) {
+                               break;
+                           }
+                       }
+                       j = max(lastv,prevlastv);
 
 /*                    T(i+1:k,i) := */
-/*                            - tau(i) * V(1:n-k+i,i+1:k)' * V(1:n-k+i,i) */
+/*                            - tau(i) * V(j:n-k+i,i+1:k)' * V(j:n-k+i,i) */
 
-                       i__1 = *n - *k + i__;
+                       i__1 = *n - *k + i__ - j + 1;
                        i__2 = *k - i__;
                        d__1 = -tau[i__];
-                       dgemv_("Transpose", &i__1, &i__2, &d__1, &v[(i__ + 1) 
-                               * v_dim1 + 1], ldv, &v[i__ * v_dim1 + 1], &
+                       dgemv_("Transpose", &i__1, &i__2, &d__1, &v[j + (i__ 
+                               + 1) * v_dim1], ldv, &v[j + i__ * v_dim1], &
                                c__1, &c_b8, &t[i__ + 1 + i__ * t_dim1], &
                                c__1);
                        v[*n - *k + i__ + i__ * v_dim1] = vii;
                    } else {
                        vii = v[i__ + (*n - *k + i__) * v_dim1];
                        v[i__ + (*n - *k + i__) * v_dim1] = 1.;
+/*                    Skip any leading zeros. */
+                       i__1 = i__ - 1;
+                       for (lastv = 1; lastv <= i__1; ++lastv) {
+                           if (v[i__ + lastv * v_dim1] != 0.) {
+                               break;
+                           }
+                       }
+                       j = max(lastv,prevlastv);
 
 /*                    T(i+1:k,i) := */
-/*                            - tau(i) * V(i+1:k,1:n-k+i) * V(i,1:n-k+i)' */
+/*                            - tau(i) * V(i+1:k,j:n-k+i) * V(i,j:n-k+i)' */
 
                        i__1 = *k - i__;
-                       i__2 = *n - *k + i__;
+                       i__2 = *n - *k + i__ - j + 1;
                        d__1 = -tau[i__];
                        dgemv_("No transpose", &i__1, &i__2, &d__1, &v[i__ + 
-                               1 + v_dim1], ldv, &v[i__ + v_dim1], ldv, &
-                               c_b8, &t[i__ + 1 + i__ * t_dim1], &c__1);
+                               1 + j * v_dim1], ldv, &v[i__ + j * v_dim1], 
+                               ldv, &c_b8, &t[i__ + 1 + i__ * t_dim1], &c__1);
                        v[i__ + (*n - *k + i__) * v_dim1] = vii;
                    }
 
@@ -253,6 +307,11 @@ static doublereal c_b8 = 0.;
                            + 1 + (i__ + 1) * t_dim1], ldt, &t[i__ + 1 + i__ *
                             t_dim1], &c__1)
                            ;
+                   if (i__ > 1) {
+                       prevlastv = min(prevlastv,lastv);
+                   } else {
+                       prevlastv = lastv;
+                   }
                }
                t[i__ + i__ * t_dim1] = tau[i__];
            }
index d486910..0d8bc66 100644 (file)
@@ -1,5 +1,18 @@
+/* dlarnv.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int dlarnv_(integer *idist, integer *iseed, integer *n, 
        doublereal *x)
 {
@@ -16,7 +29,7 @@
     extern /* Subroutine */ int dlaruv_(integer *, integer *, doublereal *);
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 8033867..eebc990 100644 (file)
@@ -1,5 +1,18 @@
+/* dlarra.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int dlarra_(integer *n, doublereal *d__, doublereal *e, 
        doublereal *e2, doublereal *spltol, doublereal *tnrm, integer *nsplit, 
         integer *isplit, integer *info)
@@ -16,7 +29,7 @@
     doublereal tmp1, eabs;
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 13f6a42..a2851c7 100644 (file)
@@ -1,5 +1,18 @@
+/* dlarrb.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int dlarrb_(integer *n, doublereal *d__, doublereal *lld, 
        integer *ifirst, integer *ilast, doublereal *rtol1, doublereal *rtol2, 
         integer *offset, doublereal *w, doublereal *wgap, doublereal *werr, 
@@ -25,7 +38,7 @@
     integer olnint, maxitr;
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index b929738..abab4bc 100644 (file)
@@ -1,5 +1,18 @@
+/* dlarrc.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int dlarrc_(char *jobt, integer *n, doublereal *vl, 
        doublereal *vu, doublereal *d__, doublereal *e, doublereal *pivmin, 
        integer *eigcnt, integer *lcnt, integer *rcnt, integer *info)
@@ -16,7 +29,7 @@
     doublereal lpivot, rpivot;
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index e7d5cda..841f172 100644 (file)
@@ -1,5 +1,18 @@
+/* dlarrd.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -48,16 +61,16 @@ static integer c__0 = 0;
             doublereal *, doublereal *, integer *, integer *, doublereal *, 
            integer *, integer *);
     integer irange, idiscl, idumma[1];
-    doublereal spdiam;
     extern integer ilaenv_(integer *, char *, char *, integer *, integer *, 
            integer *, integer *);
     integer idiscu;
     logical ncnvrg, toofew;
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
-/*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
-/*     November 2006 */
+/*  -- LAPACK auxiliary routine (version 3.2.1)                        -- */
+/*  -- LAPACK is a software package provided by Univ. of Tennessee,    -- */
+/*  -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..-- */
+/*  -- April 2009                                                      -- */
 
 /*     .. Scalar Arguments .. */
 /*     .. */
@@ -368,7 +381,8 @@ static integer c__0 = 0;
     tnorm = max(d__1,d__2);
     gl = gl - tnorm * 2. * eps * *n - *pivmin * 4.;
     gu = gu + tnorm * 2. * eps * *n + *pivmin * 4.;
-    spdiam = gu - gl;
+/*     [JAN/28/2009] remove the line below since SPDIAM variable not use */
+/*     SPDIAM = GU - GL */
 /*     Input arguments for DLAEBZ: */
 /*     The relative tolerance.  An interval (a,b] lies within */
 /*     "relative tolerance" if  b-a < RELTOL*max(|a|,|b|), */
@@ -532,9 +546,14 @@ static integer c__0 = 0;
                gu = max(d__1,d__2);
 /* L40: */
            }
-           spdiam = gu - gl;
-           gl = gl - spdiam * 2. * eps * in - *pivmin * 2.;
-           gu = gu + spdiam * 2. * eps * in + *pivmin * 2.;
+/*           [JAN/28/2009] */
+/*           change SPDIAM by TNORM in lines 2 and 3 thereafter */
+/*           line 1: remove computation of SPDIAM (not useful anymore) */
+/*           SPDIAM = GU - GL */
+/*           GL = GL - FUDGE*SPDIAM*EPS*IN - FUDGE*PIVMIN */
+/*           GU = GU + FUDGE*SPDIAM*EPS*IN + FUDGE*PIVMIN */
+           gl = gl - tnorm * 2. * eps * in - *pivmin * 2.;
+           gu = gu + tnorm * 2. * eps * in + *pivmin * 2.;
 
            if (irange > 1) {
                if (gu < *wl) {
index 368b11c..25558b6 100644 (file)
@@ -1,5 +1,18 @@
+/* dlarre.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -76,7 +89,7 @@ static integer c__2 = 2;
     doublereal isrght, bsrtol, dpivot;
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
@@ -352,6 +365,9 @@ static integer c__2 = 2;
 /*     Can force use of bisection instead of faster DQDS. */
 /*     Option left in the code for future multisection work. */
     forceb = FALSE_;
+/*     Initialize USEDQD, DQDS should be used for ALLRNG unless someone */
+/*     explicitly wants bisection. */
+    usedqd = irange == 1 && ! forceb;
     if (irange == 1 && ! forceb) {
 /*        Set interval [VL,VU] that contains all eigenvalues */
        *vl = gl;
index ef92cdd..e6b8fc5 100644 (file)
@@ -1,5 +1,18 @@
+/* dlarrf.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -39,7 +52,7 @@ static integer c__1 = 1;
     logical sawnan1, sawnan2, tryrrr1;
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 /* * */
index d50d0de..1220bc6 100644 (file)
@@ -1,5 +1,18 @@
+/* dlarrj.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int dlarrj_(integer *n, doublereal *d__, doublereal *e2, 
        integer *ifirst, integer *ilast, doublereal *rtol, integer *offset, 
        doublereal *w, doublereal *werr, doublereal *work, integer *iwork, 
@@ -24,7 +37,7 @@
     integer olnint, maxitr;
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index c5f0823..355c5f2 100644 (file)
@@ -1,5 +1,18 @@
+/* dlarrk.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int dlarrk_(integer *n, integer *iw, doublereal *gl, 
        doublereal *gu, doublereal *d__, doublereal *e2, doublereal *pivmin, 
        doublereal *reltol, doublereal *w, doublereal *werr, integer *info)
@@ -20,7 +33,7 @@
     integer negcnt;
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 5aee2a1..d136f00 100644 (file)
@@ -1,5 +1,18 @@
+/* dlarrr.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int dlarrr_(integer *n, doublereal *d__, doublereal *e, 
        integer *info)
 {
@@ -19,7 +32,7 @@
     doublereal smlnum, offdig2;
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index ae0ac38..bb0d336 100644 (file)
@@ -1,5 +1,18 @@
+/* dlarrv.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static doublereal c_b5 = 0.;
@@ -94,7 +107,7 @@ static integer c__2 = 2;
     integer isupmx;
 
 
-/*  -- LAPACK auxiliary routine (version 3.1.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 3c8e40d..8ee9593 100644 (file)
@@ -1,5 +1,18 @@
+/* dlartg.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int dlartg_(doublereal *f, doublereal *g, doublereal *cs, 
        doublereal *sn, doublereal *r__)
 {
 
     /* Local variables */
     integer i__;
-    doublereal f1, g1, scale;
+    doublereal f1, g1, eps, scale;
     integer count;
-    static doublereal safmn2, safmx2;
+    doublereal safmn2, safmx2;
     extern doublereal dlamch_(char *);
-    static doublereal safmin, eps;
-    static volatile logical first = TRUE_;
+    doublereal safmin;
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
 /*     .. Executable Statements .. */
 
 /*     IF( FIRST ) THEN */
-    if (first) {
-        safmin = dlamch_("S");
-        eps = dlamch_("E");
-        d__1 = dlamch_("B");
-        i__1 = (integer) (log(safmin / eps) / log(dlamch_("B")) / 2.);
-        safmn2 = pow_di(&d__1, &i__1);
-        safmx2 = 1. / safmn2;
-        first = FALSE_;
-    }
+    safmin = dlamch_("S");
+    eps = dlamch_("E");
+    d__1 = dlamch_("B");
+    i__1 = (integer) (log(safmin / eps) / log(dlamch_("B")) / 2.);
+    safmn2 = pow_di(&d__1, &i__1);
+    safmx2 = 1. / safmn2;
 /*        FIRST = .FALSE. */
 /*     END IF */
     if (*g == 0.) {
index 5ce9439..007a1ad 100644 (file)
@@ -1,5 +1,18 @@
+/* dlaruv.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int dlaruv_(integer *iseed, integer *n, doublereal *x)
 {
     /* Initialized data */
@@ -51,7 +64,7 @@
     integer i__, i1, i2, i3, i4, it1, it2, it3, it4;
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index b8162bc..f2e1528 100644 (file)
@@ -1,5 +1,18 @@
+/* dlas2.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int dlas2_(doublereal *f, doublereal *g, doublereal *h__, 
        doublereal *ssmin, doublereal *ssmax)
 {
@@ -13,7 +26,7 @@
     doublereal c__, fa, ga, ha, as, at, au, fhmn, fhmx;
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 7e62f38..a571cee 100644 (file)
@@ -1,5 +1,18 @@
+/* dlascl.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int dlascl_(char *type__, integer *kl, integer *ku, 
        doublereal *cfrom, doublereal *cto, integer *m, integer *n, 
        doublereal *a, integer *lda, integer *info)
     doublereal cfrom1;
     extern doublereal dlamch_(char *);
     doublereal cfromc;
+    extern logical disnan_(doublereal *);
     extern /* Subroutine */ int xerbla_(char *, integer *);
     doublereal bignum, smlnum;
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
 
     if (itype == -1) {
        *info = -1;
-    } else if (*cfrom == 0.) {
+    } else if (*cfrom == 0. || disnan_(cfrom)) {
        *info = -4;
+    } else if (disnan_(cto)) {
+       *info = -5;
     } else if (*m < 0) {
        *info = -6;
     } else if (*n < 0 || itype == 4 && *n != *m || itype == 5 && *n != *m) {
 
 L10:
     cfrom1 = cfromc * smlnum;
-    cto1 = ctoc / bignum;
-    if (abs(cfrom1) > abs(ctoc) && ctoc != 0.) {
-       mul = smlnum;
-       done = FALSE_;
-       cfromc = cfrom1;
-    } else if (abs(cto1) > abs(cfromc)) {
-       mul = bignum;
-       done = FALSE_;
-       ctoc = cto1;
-    } else {
+    if (cfrom1 == cfromc) {
+/*        CFROMC is an inf.  Multiply by a correctly signed zero for */
+/*        finite CTOC, or a NaN if CTOC is infinite. */
        mul = ctoc / cfromc;
        done = TRUE_;
+       cto1 = ctoc;
+    } else {
+       cto1 = ctoc / bignum;
+       if (cto1 == ctoc) {
+/*           CTOC is either 0 or an inf.  In both cases, CTOC itself */
+/*           serves as the correct multiplication factor. */
+           mul = ctoc;
+           done = TRUE_;
+           cfromc = 1.;
+       } else if (abs(cfrom1) > abs(ctoc) && ctoc != 0.) {
+           mul = smlnum;
+           done = FALSE_;
+           cfromc = cfrom1;
+       } else if (abs(cto1) > abs(cfromc)) {
+           mul = bignum;
+           done = FALSE_;
+           ctoc = cto1;
+       } else {
+           mul = ctoc / cfromc;
+           done = TRUE_;
+       }
     }
 
     if (itype == 0) {
index 7c1a9ae..4d733b3 100644 (file)
@@ -1,5 +1,18 @@
+/* dlasd0.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__0 = 0;
@@ -34,7 +47,7 @@ static integer c__2 = 2;
            char *, integer *);
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 40f868f..9feedcb 100644 (file)
@@ -1,5 +1,18 @@
+/* dlasd1.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__0 = 0;
@@ -38,7 +51,7 @@ static integer c_n1 = -1;
     integer coltyp;
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 58fed21..0b98cf3 100644 (file)
@@ -1,5 +1,18 @@
+/* dlasd2.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -41,7 +54,7 @@ static doublereal c_b30 = 0.;
     doublereal hlftol;
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 290904b..4908be4 100644 (file)
@@ -1,5 +1,18 @@
+/* dlasd3.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -44,7 +57,7 @@ static doublereal c_b26 = 0.;
            xerbla_(char *, integer *);
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index c544ee3..cf17371 100644 (file)
@@ -1,5 +1,18 @@
+/* dlasd4.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int dlasd4_(integer *n, integer *i__, doublereal *d__, 
        doublereal *z__, doublereal *delta, doublereal *rho, doublereal *
        sigma, doublereal *work, integer *info)
@@ -38,7 +51,7 @@
     doublereal erretm, dtipsq, rhoinv;
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 0156345..d5c801d 100644 (file)
@@ -1,5 +1,18 @@
+/* dlasd5.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int dlasd5_(integer *i__, doublereal *d__, doublereal *z__, 
        doublereal *delta, doublereal *rho, doublereal *dsigma, doublereal *
        work)
@@ -14,7 +27,7 @@
     doublereal b, c__, w, del, tau, delsq;
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index d218365..5306f4a 100644 (file)
@@ -1,5 +1,18 @@
+/* dlasd6.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__0 = 0;
@@ -40,7 +53,7 @@ static integer c_n1 = -1;
     doublereal orgnrm;
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 254f2d4..cee3688 100644 (file)
@@ -1,5 +1,18 @@
+/* dlasd7.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -34,7 +47,7 @@ static integer c__1 = 1;
     doublereal hlftol;
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 169bf43..731e334 100644 (file)
@@ -1,5 +1,18 @@
+/* dlasd8.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -41,9 +54,9 @@ static doublereal c_b8 = 1.;
     doublereal dsigjp;
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
-/*     November 2006 */
+/*     October 2006 */
 
 /*     .. Scalar Arguments .. */
 /*     .. */
@@ -78,9 +91,10 @@ static doublereal c_b8 = 1.;
 /*  D       (output) DOUBLE PRECISION array, dimension ( K ) */
 /*          On output, D contains the updated singular values. */
 
-/*  Z       (input) DOUBLE PRECISION array, dimension ( K ) */
-/*          The first K elements of this array contain the components */
-/*          of the deflation-adjusted updating row vector. */
+/*  Z       (input/output) DOUBLE PRECISION array, dimension ( K ) */
+/*          On entry, the first K elements of this array contain the */
+/*          components of the deflation-adjusted updating row vector. */
+/*          On exit, Z is updated. */
 
 /*  VF      (input/output) DOUBLE PRECISION array, dimension ( K ) */
 /*          On entry, VF contains  information passed through DBEDE8. */
@@ -109,10 +123,12 @@ static doublereal c_b8 = 1.;
 /*  LDDIFR  (input) INTEGER */
 /*          The leading dimension of DIFR, must be at least K. */
 
-/*  DSIGMA  (input) DOUBLE PRECISION array, dimension ( K ) */
-/*          The first K elements of this array contain the old roots */
-/*          of the deflated updating problem.  These are the poles */
+/*  DSIGMA  (input/output) DOUBLE PRECISION array, dimension ( K ) */
+/*          On entry, the first K elements of this array contain the old */
+/*          roots of the deflated updating problem.  These are the poles */
 /*          of the secular equation. */
+/*          On exit, the elements of DSIGMA may be very slightly altered */
+/*          in value. */
 
 /*  WORK    (workspace) DOUBLE PRECISION array, dimension at least 3 * K */
 
@@ -198,7 +214,7 @@ static doublereal c_b8 = 1.;
 /*     changes the bottommost bits of DSIGMA(I). It does not account */
 /*     for hexadecimal or decimal machines without guard digits */
 /*     (we know of none). We use a subroutine call to compute */
-/*     2*DSIGMA(I) to prevent optimizing compilers from eliminating */
+/*     2*DLAMBDA(I) to prevent optimizing compilers from eliminating */
 /*     this code. */
 
     i__1 = *k;
index 5cb01de..dc604f8 100644 (file)
@@ -1,5 +1,18 @@
+/* dlasda.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__0 = 0;
@@ -51,7 +64,7 @@ static integer c__2 = 2;
     integer smlszp;
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 9791dc4..a45e994 100644 (file)
@@ -1,5 +1,18 @@
+/* dlasdq.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -33,7 +46,7 @@ static integer c__1 = 1;
     logical rotate;
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index c1cef86..a63bb70 100644 (file)
@@ -1,5 +1,18 @@
+/* dlasdt.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int dlasdt_(integer *n, integer *lvl, integer *nd, integer *
        inode, integer *ndiml, integer *ndimr, integer *msub)
 {
@@ -15,7 +28,7 @@
     integer nlvl, llst, ncrnt;
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 958648a..2547258 100644 (file)
@@ -1,5 +1,18 @@
+/* dlaset.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int dlaset_(char *uplo, integer *m, integer *n, doublereal *
        alpha, doublereal *beta, doublereal *a, integer *lda)
 {
@@ -11,7 +24,7 @@
     extern logical lsame_(char *, char *);
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index a6cfaeb..a14d0fa 100644 (file)
@@ -1,5 +1,18 @@
+/* dlasq1.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -37,9 +50,15 @@ static integer c__0 = 0;
            char *, integer *, doublereal *, integer *);
 
 
-/*  -- LAPACK routine (version 3.1) -- */
-/*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
-/*     November 2006 */
+/*  -- LAPACK routine (version 3.2)                                    -- */
+
+/*  -- Contributed by Osni Marques of the Lawrence Berkeley National   -- */
+/*  -- Laboratory and Beresford Parlett of the Univ. of California at  -- */
+/*  -- Berkeley                                                        -- */
+/*  -- November 2008                                                   -- */
+
+/*  -- LAPACK is a software package provided by Univ. of Tennessee,    -- */
+/*  -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..-- */
 
 /*     .. Scalar Arguments .. */
 /*     .. */
index c898a15..5359cbe 100644 (file)
@@ -1,5 +1,18 @@
+/* dlasq2.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -19,31 +32,32 @@ static integer c__11 = 11;
     double sqrt(doublereal);
 
     /* Local variables */
-    doublereal d__, e;
+    doublereal d__, e, g;
     integer k;
     doublereal s, t;
     integer i0, i4, n0;
     doublereal dn;
     integer pp;
-    doublereal dn1, dn2, eps, tau, tol;
+    doublereal dn1, dn2, dee, eps, tau, tol;
     integer ipn4;
     doublereal tol2;
     logical ieee;
     integer nbig;
     doublereal dmin__, emin, emax;
-    integer ndiv, iter;
+    integer kmin, ndiv, iter;
     doublereal qmin, temp, qmax, zmax;
     integer splt;
     doublereal dmin1, dmin2;
     integer nfail;
     doublereal desig, trace, sigma;
     integer iinfo, ttype;
-    extern /* Subroutine */ int dlazq3_(integer *, integer *, doublereal *, 
+    extern /* Subroutine */ int dlasq3_(integer *, integer *, doublereal *, 
            integer *, doublereal *, doublereal *, doublereal *, doublereal *, 
             integer *, integer *, integer *, logical *, integer *, 
            doublereal *, doublereal *, doublereal *, doublereal *, 
-           doublereal *, doublereal *);
+           doublereal *, doublereal *, doublereal *);
     extern doublereal dlamch_(char *);
+    doublereal deemin;
     integer iwhila, iwhilb;
     doublereal oldemn, safmin;
     extern /* Subroutine */ int xerbla_(char *, integer *);
@@ -53,11 +67,15 @@ static integer c__11 = 11;
            integer *);
 
 
-/*  -- LAPACK routine (version 3.1) -- */
-/*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
-/*     November 2006 */
+/*  -- LAPACK routine (version 3.2)                                    -- */
+
+/*  -- Contributed by Osni Marques of the Lawrence Berkeley National   -- */
+/*  -- Laboratory and Beresford Parlett of the Univ. of California at  -- */
+/*  -- Berkeley                                                        -- */
+/*  -- November 2008                                                   -- */
 
-/*     Modified to call DLAZQ3 in place of DLASQ3, 13 Feb 03, SJH. */
+/*  -- LAPACK is a software package provided by Univ. of Tennessee,    -- */
+/*  -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..-- */
 
 /*     .. Scalar Arguments .. */
 /*     .. */
@@ -81,7 +99,7 @@ static integer c__11 = 11;
 /*  Note : DLASQ2 defines a logical variable, IEEE, which is true */
 /*  on machines which follow ieee-754 floating-point standard in their */
 /*  handling of infinities and NaNs, and false otherwise. This variable */
-/*  is passed to DLAZQ3. */
+/*  is passed to DLASQ3. */
 
 /*  Arguments */
 /*  ========= */
@@ -89,7 +107,7 @@ static integer c__11 = 11;
 /*  N     (input) INTEGER */
 /*        The number of rows and columns in the matrix. N >= 0. */
 
-/*  Z     (workspace) DOUBLE PRECISION array, dimension ( 4*N ) */
+/*  Z     (input/output) DOUBLE PRECISION array, dimension ( 4*N ) */
 /*        On entry Z holds the qd array. On exit, entries 1 to N hold */
 /*        the eigenvalues in decreasing order, Z( 2*N+1 ) holds the */
 /*        trace, and Z( 2*N+2 ) holds the sum of the eigenvalues. If */
@@ -355,7 +373,7 @@ static integer c__11 = 11;
 /* L80: */
     }
 
-/*     Initialise variables to pass to DLAZQ3 */
+/*     Initialise variables to pass to DLASQ3. */
 
     ttype = 0;
     dmin1 = 0.;
@@ -363,6 +381,7 @@ static integer c__11 = 11;
     dn = 0.;
     dn1 = 0.;
     dn2 = 0.;
+    g = 0.;
     tau = 0.;
 
     iter = 2;
@@ -372,7 +391,7 @@ static integer c__11 = 11;
     i__1 = *n + 1;
     for (iwhila = 1; iwhila <= i__1; ++iwhila) {
        if (n0 < 1) {
-           goto L150;
+           goto L170;
        }
 
 /*        While array unfinished do */
@@ -426,10 +445,43 @@ static integer c__11 = 11;
 
 L100:
        i0 = i4 / 4;
+       pp = 0;
 
-/*        Store EMIN for passing to DLAZQ3. */
-
-       z__[(n0 << 2) - 1] = emin;
+       if (n0 - i0 > 1) {
+           dee = z__[(i0 << 2) - 3];
+           deemin = dee;
+           kmin = i0;
+           i__2 = (n0 << 2) - 3;
+           for (i4 = (i0 << 2) + 1; i4 <= i__2; i4 += 4) {
+               dee = z__[i4] * (dee / (dee + z__[i4 - 2]));
+               if (dee <= deemin) {
+                   deemin = dee;
+                   kmin = (i4 + 3) / 4;
+               }
+/* L110: */
+           }
+           if (kmin - i0 << 1 < n0 - kmin && deemin <= z__[(n0 << 2) - 3] * 
+                   .5) {
+               ipn4 = i0 + n0 << 2;
+               pp = 2;
+               i__2 = i0 + n0 - 1 << 1;
+               for (i4 = i0 << 2; i4 <= i__2; i4 += 4) {
+                   temp = z__[i4 - 3];
+                   z__[i4 - 3] = z__[ipn4 - i4 - 3];
+                   z__[ipn4 - i4 - 3] = temp;
+                   temp = z__[i4 - 2];
+                   z__[i4 - 2] = z__[ipn4 - i4 - 2];
+                   z__[ipn4 - i4 - 2] = temp;
+                   temp = z__[i4 - 1];
+                   z__[i4 - 1] = z__[ipn4 - i4 - 5];
+                   z__[ipn4 - i4 - 5] = temp;
+                   temp = z__[i4];
+                   z__[i4] = z__[ipn4 - i4 - 4];
+                   z__[ipn4 - i4 - 4] = temp;
+/* L120: */
+               }
+           }
+       }
 
 /*        Put -(initial shift) into DMIN. */
 
@@ -437,22 +489,24 @@ L100:
        d__1 = 0., d__2 = qmin - sqrt(qmin) * 2. * sqrt(emax);
        dmin__ = -max(d__1,d__2);
 
-/*        Now I0:N0 is unreduced. PP = 0 for ping, PP = 1 for pong. */
-
-       pp = 0;
+/*        Now I0:N0 is unreduced. */
+/*        PP = 0 for ping, PP = 1 for pong. */
+/*        PP = 2 indicates that flipping was applied to the Z array and */
+/*               and that the tests for deflation upon entry in DLASQ3 */
+/*               should not be performed. */
 
        nbig = (n0 - i0 + 1) * 30;
        i__2 = nbig;
        for (iwhilb = 1; iwhilb <= i__2; ++iwhilb) {
            if (i0 > n0) {
-               goto L130;
+               goto L150;
            }
 
 /*           While submatrix unfinished take a good dqds step. */
 
-           dlazq3_(&i0, &n0, &z__[1], &pp, &dmin__, &sigma, &desig, &qmax, &
+           dlasq3_(&i0, &n0, &z__[1], &pp, &dmin__, &sigma, &desig, &qmax, &
                    nfail, &iter, &ndiv, &ieee, &ttype, &dmin1, &dmin2, &dn, &
-                   dn1, &dn2, &tau);
+                   dn1, &dn2, &g, &tau);
 
            pp = 1 - pp;
 
@@ -485,7 +539,7 @@ L100:
                            d__1 = oldemn, d__2 = z__[i4];
                            oldemn = min(d__1,d__2);
                        }
-/* L110: */
+/* L130: */
                    }
                    z__[(n0 << 2) - 1] = emin;
                    z__[n0 * 4] = oldemn;
@@ -493,7 +547,7 @@ L100:
                }
            }
 
-/* L120: */
+/* L140: */
        }
 
        *info = 2;
@@ -501,9 +555,9 @@ L100:
 
 /*        end IWHILB */
 
-L130:
+L150:
 
-/* L140: */
+/* L160: */
        ;
     }
 
@@ -512,14 +566,14 @@ L130:
 
 /*     end IWHILA */
 
-L150:
+L170:
 
 /*     Move q's to the front. */
 
     i__1 = *n;
     for (k = 2; k <= i__1; ++k) {
        z__[k] = z__[(k << 2) - 3];
-/* L160: */
+/* L180: */
     }
 
 /*     Sort and compute sum of eigenvalues. */
@@ -529,7 +583,7 @@ L150:
     e = 0.;
     for (k = *n; k >= 1; --k) {
        e += z__[k];
-/* L170: */
+/* L190: */
     }
 
 /*     Store trace, sum(eigenvalues) and information on performance. */
index fcce6a3..1227c50 100644 (file)
@@ -1,20 +1,25 @@
+/* dlasq3.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int dlasq3_(integer *i0, integer *n0, doublereal *z__, 
        integer *pp, doublereal *dmin__, doublereal *sigma, doublereal *desig, 
         doublereal *qmax, integer *nfail, integer *iter, integer *ndiv, 
-       logical *ieee)
+       logical *ieee, integer *ttype, doublereal *dmin1, doublereal *dmin2, 
+       doublereal *dn, doublereal *dn1, doublereal *dn2, doublereal *g, 
+       doublereal *tau)
 {
-    /* Initialized data */
-
-    static integer ttype = 0;
-    static doublereal dmin1 = 0.;
-    static doublereal dmin2 = 0.;
-    static doublereal dn = 0.;
-    static doublereal dn1 = 0.;
-    static doublereal dn2 = 0.;
-    static doublereal tau = 0.;
-
     /* System generated locals */
     integer i__1;
     doublereal d__1, d__2;
     doublereal tol2, temp;
     extern /* Subroutine */ int dlasq4_(integer *, integer *, doublereal *, 
            integer *, integer *, doublereal *, doublereal *, doublereal *, 
-           doublereal *, doublereal *, doublereal *, doublereal *, integer *)
-           , dlasq5_(integer *, integer *, doublereal *, integer *, 
-           doublereal *, doublereal *, doublereal *, doublereal *, 
-           doublereal *, doublereal *, doublereal *, logical *), dlasq6_(
+           doublereal *, doublereal *, doublereal *, doublereal *, integer *
+            doublereal *), dlasq5_(integer *, integer *, doublereal *, 
+           integer *, doublereal *, doublereal *, doublereal *, doublereal *, 
+            doublereal *, doublereal *, doublereal *, logical *), dlasq6_(
            integer *, integer *, doublereal *, integer *, doublereal *, 
            doublereal *, doublereal *, doublereal *, doublereal *, 
            doublereal *);
     extern doublereal dlamch_(char *);
-    doublereal safmin;
+    extern logical disnan_(doublereal *);
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
-/*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
-/*     November 2006 */
+/*  -- LAPACK routine (version 3.2)                                    -- */
+
+/*  -- Contributed by Osni Marques of the Lawrence Berkeley National   -- */
+/*  -- Laboratory and Beresford Parlett of the Univ. of California at  -- */
+/*  -- Berkeley                                                        -- */
+/*  -- November 2008                                                   -- */
+
+/*  -- LAPACK is a software package provided by Univ. of Tennessee,    -- */
+/*  -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..-- */
 
 /*     .. Scalar Arguments .. */
 /*     .. */
 /*  Z      (input) DOUBLE PRECISION array, dimension ( 4*N ) */
 /*         Z holds the qd array. */
 
-/*  PP     (input) INTEGER */
+/*  PP     (input/output) INTEGER */
 /*         PP=0 for ping, PP=1 for pong. */
+/*         PP=2 indicates that flipping was applied to the Z array */
+/*         and that the initial tests for deflation should not be */
+/*         performed. */
 
 /*  DMIN   (output) DOUBLE PRECISION */
 /*         Minimum value of d. */
 /*  NDIV   (output) INTEGER */
 /*         Number of divisions. */
 
-/*  TTYPE  (output) INTEGER */
-/*         Shift type. */
-
 /*  IEEE   (input) LOGICAL */
 /*         Flag for IEEE or non IEEE arithmetic (passed to DLASQ5). */
 
+/*  TTYPE  (input/output) INTEGER */
+/*         Shift type. */
+
+/*  DMIN1, DMIN2, DN, DN1, DN2, G, TAU (input/output) DOUBLE PRECISION */
+/*         These are passed as arguments in order to save their values */
+/*         between calls to DLASQ3. */
+
 /*  ===================================================================== */
 
 /*     .. Parameters .. */
 /*     .. */
 /*     .. Intrinsic Functions .. */
 /*     .. */
-/*     .. Save statement .. */
-/*     .. */
-/*     .. Data statement .. */
+/*     .. Executable Statements .. */
+
     /* Parameter adjustments */
     --z__;
 
     /* Function Body */
-/*     .. */
-/*     .. Executable Statements .. */
-
     n0in = *n0;
     eps = dlamch_("Precision");
-    safmin = dlamch_("Safe minimum");
     tol = eps * 100.;
 /* Computing 2nd power */
     d__1 = tol;
@@ -191,6 +204,9 @@ L40:
     goto L10;
 
 L50:
+    if (*pp == 2) {
+       *pp = 0;
+    }
 
 /*     Reverse the qd-array, if warranted. */
 
@@ -218,8 +234,8 @@ L50:
                z__[(*n0 << 2) - *pp] = z__[(*i0 << 2) - *pp];
            }
 /* Computing MIN */
-           d__1 = dmin2, d__2 = z__[(*n0 << 2) + *pp - 1];
-           dmin2 = min(d__1,d__2);
+           d__1 = *dmin2, d__2 = z__[(*n0 << 2) + *pp - 1];
+           *dmin2 = min(d__1,d__2);
 /* Computing MIN */
            d__1 = z__[(*n0 << 2) + *pp - 1], d__2 = z__[(*i0 << 2) + *pp - 1]
                    , d__1 = min(d__1,d__2), d__2 = z__[(*i0 << 2) + *pp + 3];
@@ -236,96 +252,94 @@ L50:
        }
     }
 
-/* Computing MIN */
-    d__1 = z__[(*n0 << 2) + *pp - 1], d__2 = z__[(*n0 << 2) + *pp - 9], d__1 =
-            min(d__1,d__2), d__2 = dmin2 + z__[(*n0 << 2) - *pp];
-    if (*dmin__ < 0. || safmin * *qmax < min(d__1,d__2)) {
+/*     Choose a shift. */
 
-/*        Choose a shift. */
+    dlasq4_(i0, n0, &z__[1], pp, &n0in, dmin__, dmin1, dmin2, dn, dn1, dn2, 
+           tau, ttype, g);
 
-       dlasq4_(i0, n0, &z__[1], pp, &n0in, dmin__, &dmin1, &dmin2, &dn, &dn1, 
-                &dn2, &tau, &ttype);
+/*     Call dqds until DMIN > 0. */
 
-/*        Call dqds until DMIN > 0. */
-
-L80:
+L70:
 
-       dlasq5_(i0, n0, &z__[1], pp, &tau, dmin__, &dmin1, &dmin2, &dn, &dn1
-               &dn2, ieee);
+    dlasq5_(i0, n0, &z__[1], pp, tau, dmin__, dmin1, dmin2, dn, dn1, dn2
+           ieee);
 
-       *ndiv += *n0 - *i0 + 2;
-       ++(*iter);
+    *ndiv += *n0 - *i0 + 2;
+    ++(*iter);
 
-/*        Check status. */
+/*     Check status. */
 
-       if (*dmin__ >= 0. && dmin1 > 0.) {
+    if (*dmin__ >= 0. && *dmin1 > 0.) {
 
-/*           Success. */
+/*        Success. */
 
-           goto L100;
+       goto L90;
 
-       } else if (*dmin__ < 0. && dmin1 > 0. && z__[(*n0 - 1 << 2) - *pp] < 
-               tol * (*sigma + dn1) && abs(dn) < tol * *sigma) {
+    } else if (*dmin__ < 0. && *dmin1 > 0. && z__[(*n0 - 1 << 2) - *pp] < tol 
+           * (*sigma + *dn1) && abs(*dn) < tol * *sigma) {
 
-/*           Convergence hidden by negative DN. */
+/*        Convergence hidden by negative DN. */
 
-           z__[(*n0 - 1 << 2) - *pp + 2] = 0.;
-           *dmin__ = 0.;
-           goto L100;
-       } else if (*dmin__ < 0.) {
+       z__[(*n0 - 1 << 2) - *pp + 2] = 0.;
+       *dmin__ = 0.;
+       goto L90;
+    } else if (*dmin__ < 0.) {
 
-/*           TAU too big. Select new TAU and try again. */
+/*        TAU too big. Select new TAU and try again. */
 
-           ++(*nfail);
-           if (ttype < -22) {
+       ++(*nfail);
+       if (*ttype < -22) {
 
-/*              Failed twice. Play it safe. */
+/*           Failed twice. Play it safe. */
 
-               tau = 0.;
-           } else if (dmin1 > 0.) {
+           *tau = 0.;
+       } else if (*dmin1 > 0.) {
 
-/*              Late failure. Gives excellent shift. */
+/*           Late failure. Gives excellent shift. */
 
-               tau = (tau + *dmin__) * (1. - eps * 2.);
-               ttype += -11;
-           } else {
+           *tau = (*tau + *dmin__) * (1. - eps * 2.);
+           *ttype += -11;
+       } else {
 
-/*              Early failure. Divide by 4. */
+/*           Early failure. Divide by 4. */
 
-               tau *= .25;
-               ttype += -12;
-           }
-           goto L80;
-       } else if (*dmin__ != *dmin__) {
+           *tau *= .25;
+           *ttype += -12;
+       }
+       goto L70;
+    } else if (disnan_(dmin__)) {
 
-/*           NaN. */
+/*        NaN. */
 
-           tau = 0.;
+       if (*tau == 0.) {
            goto L80;
        } else {
+           *tau = 0.;
+           goto L70;
+       }
+    } else {
 
-/*           Possible underflow. Play it safe. */
+/*        Possible underflow. Play it safe. */
 
-           goto L90;
-       }
+       goto L80;
     }
 
 /*     Risk of underflow. */
 
-L90:
-    dlasq6_(i0, n0, &z__[1], pp, dmin__, &dmin1, &dmin2, &dn, &dn1, &dn2);
+L80:
+    dlasq6_(i0, n0, &z__[1], pp, dmin__, dmin1, dmin2, dn, dn1, dn2);
     *ndiv += *n0 - *i0 + 2;
     ++(*iter);
-    tau = 0.;
+    *tau = 0.;
 
-L100:
-    if (tau < *sigma) {
-       *desig += tau;
+L90:
+    if (*tau < *sigma) {
+       *desig += *tau;
        t = *sigma + *desig;
        *desig -= t - *sigma;
     } else {
-       t = *sigma + tau;
-       *desig = *sigma - (t - tau) + *desig;
+       t = *sigma + *tau;
+       *desig = *sigma - (t - *tau) + *desig;
     }
     *sigma = t;
 
index ee0e57a..8d9020b 100644 (file)
@@ -1,14 +1,23 @@
+/* dlasq4.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int dlasq4_(integer *i0, integer *n0, doublereal *z__, 
        integer *pp, integer *n0in, doublereal *dmin__, doublereal *dmin1, 
        doublereal *dmin2, doublereal *dn, doublereal *dn1, doublereal *dn2, 
-       doublereal *tau, integer *ttype)
+       doublereal *tau, integer *ttype, doublereal *g)
 {
-    /* Initialized data */
-
-    static doublereal g = 0.;
-
     /* System generated locals */
     integer i__1;
     doublereal d__1, d__2;
     doublereal gam, gap1, gap2;
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
-/*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
-/*     November 2006 */
+/*  -- LAPACK routine (version 3.2)                                    -- */
+
+/*  -- Contributed by Osni Marques of the Lawrence Berkeley National   -- */
+/*  -- Laboratory and Beresford Parlett of the Univ. of California at  -- */
+/*  -- Berkeley                                                        -- */
+/*  -- November 2008                                                   -- */
+
+/*  -- LAPACK is a software package provided by Univ. of Tennessee,    -- */
+/*  -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..-- */
 
 /*     .. Scalar Arguments .. */
 /*     .. */
@@ -49,7 +64,7 @@
 /*  PP    (input) INTEGER */
 /*        PP=0 for ping, PP=1 for pong. */
 
-/*  N0IN  (input) INTEGER */
+/*  NOIN  (input) INTEGER */
 /*        The value of N0 at start of EIGTEST. */
 
 /*  DMIN  (input) DOUBLE PRECISION */
 /*  TTYPE (output) INTEGER */
 /*        Shift type. */
 
+/*  G     (input/output) REAL */
+/*        G is passed as an argument in order to save its value between */
+/*        calls to DLASQ4. */
+
 /*  Further Details */
 /*  =============== */
 /*  CNST1 = 9/16 */
 /*     .. */
 /*     .. Intrinsic Functions .. */
 /*     .. */
-/*     .. Save statement .. */
-/*     .. */
-/*     .. Data statement .. */
-    /* Parameter adjustments */
-    --z__;
-
-    /* Function Body */
-/*     .. */
 /*     .. Executable Statements .. */
 
 /*     A negative DMIN forces the shift to take that absolute value */
 /*     TTYPE records the type of shift. */
 
+    /* Parameter adjustments */
+    --z__;
+
+    /* Function Body */
     if (*dmin__ <= 0.) {
        *tau = -(*dmin__);
        *ttype = -1;
@@ -255,13 +270,13 @@ L40:
 /*           Case 6, no information to guide us. */
 
            if (*ttype == -6) {
-               g += (1. - g) * .333;
+               *g += (1. - *g) * .333;
            } else if (*ttype == -18) {
-               g = .083250000000000005;
+               *g = .083250000000000005;
            } else {
-               g = .25;
+               *g = .25;
            }
-           s = g * *dmin__;
+           s = *g * *dmin__;
            *ttype = -6;
        }
 
index 39135ca..1439891 100644 (file)
@@ -1,5 +1,18 @@
+/* dlasq5.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int dlasq5_(integer *i0, integer *n0, doublereal *z__, 
        integer *pp, doublereal *tau, doublereal *dmin__, doublereal *dmin1, 
        doublereal *dmin2, doublereal *dn, doublereal *dnm1, doublereal *dnm2, 
     doublereal emin, temp;
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
-/*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
-/*     November 2006 */
+/*  -- LAPACK routine (version 3.2)                                    -- */
+
+/*  -- Contributed by Osni Marques of the Lawrence Berkeley National   -- */
+/*  -- Laboratory and Beresford Parlett of the Univ. of California at  -- */
+/*  -- Berkeley                                                        -- */
+/*  -- November 2008                                                   -- */
+
+/*  -- LAPACK is a software package provided by Univ. of Tennessee,    -- */
+/*  -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..-- */
 
 /*     .. Scalar Arguments .. */
 /*     .. */
index 586aa67..d34eb3b 100644 (file)
@@ -1,5 +1,18 @@
+/* dlasq6.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int dlasq6_(integer *i0, integer *n0, doublereal *z__, 
        integer *pp, doublereal *dmin__, doublereal *dmin1, doublereal *dmin2, 
         doublereal *dn, doublereal *dnm1, doublereal *dnm2)
     doublereal safmin;
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
-/*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
-/*     November 2006 */
+/*  -- LAPACK routine (version 3.2)                                    -- */
+
+/*  -- Contributed by Osni Marques of the Lawrence Berkeley National   -- */
+/*  -- Laboratory and Beresford Parlett of the Univ. of California at  -- */
+/*  -- Berkeley                                                        -- */
+/*  -- November 2008                                                   -- */
+
+/*  -- LAPACK is a software package provided by Univ. of Tennessee,    -- */
+/*  -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..-- */
 
 /*     .. Scalar Arguments .. */
 /*     .. */
index 82d47de..92aa27a 100644 (file)
@@ -1,5 +1,18 @@
+/* dlasr.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int dlasr_(char *side, char *pivot, char *direct, integer *m, 
         integer *n, doublereal *c__, doublereal *s, doublereal *a, integer *
        lda)
@@ -15,7 +28,7 @@
     extern /* Subroutine */ int xerbla_(char *, integer *);
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index c129c46..9b41492 100644 (file)
@@ -1,5 +1,18 @@
+/* dlasrt.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int dlasrt_(char *id, integer *n, doublereal *d__, integer *
        info)
 {
@@ -20,7 +33,7 @@
     integer stkpnt;
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 06d8f25..8776435 100644 (file)
@@ -1,5 +1,18 @@
+/* dlassq.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int dlassq_(integer *n, doublereal *x, integer *incx, 
        doublereal *scale, doublereal *sumsq)
 {
@@ -12,7 +25,7 @@
     doublereal absxi;
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 3142a87..97263c5 100644 (file)
@@ -1,5 +1,18 @@
+/* dlasv2.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static doublereal c_b3 = 2.;
@@ -26,7 +39,7 @@ static doublereal c_b4 = 1.;
     logical gasmal;
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 7ebcf6f..43ed0d5 100644 (file)
@@ -1,5 +1,18 @@
+/* dlaswp.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int dlaswp_(integer *n, doublereal *a, integer *lda, integer 
        *k1, integer *k2, integer *ipiv, integer *incx)
 {
@@ -11,7 +24,7 @@
     doublereal temp;
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index bcaf32d..65be9c3 100644 (file)
+/* dlasyf.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
-/* Subroutine */ int dlasyf_(char *uplo, integer *n, integer *nb, integer *kb,
+
+/* Table of constant values */
+
+static integer c__1 = 1;
+static doublereal c_b8 = -1.;
+static doublereal c_b9 = 1.;
+
+/* Subroutine */ int dlasyf_(char *uplo, integer *n, integer *nb, integer *kb, 
         doublereal *a, integer *lda, integer *ipiv, doublereal *w, integer *
        ldw, integer *info)
 {
-/*  -- LAPACK routine (version 3.1) --   
-       Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd..   
-       November 2006   
-
-
-    Purpose   
-    =======   
-
-    DLASYF computes a partial factorization of a real symmetric matrix A   
-    using the Bunch-Kaufman diagonal pivoting method. The partial   
-    factorization has the form:   
-
-    A  =  ( I  U12 ) ( A11  0  ) (  I    0   )  if UPLO = 'U', or:   
-          ( 0  U22 ) (  0   D  ) ( U12' U22' )   
-
-    A  =  ( L11  0 ) (  D   0  ) ( L11' L21' )  if UPLO = 'L'   
-          ( L21  I ) (  0  A22 ) (  0    I   )   
-
-    where the order of D is at most NB. The actual order is returned in   
-    the argument KB, and is either NB or NB-1, or N if N <= NB.   
-
-    DLASYF is an auxiliary routine called by DSYTRF. It uses blocked code   
-    (calling Level 3 BLAS) to update the submatrix A11 (if UPLO = 'U') or   
-    A22 (if UPLO = 'L').   
-
-    Arguments   
-    =========   
-
-    UPLO    (input) CHARACTER*1   
-            Specifies whether the upper or lower triangular part of the   
-            symmetric matrix A is stored:   
-            = 'U':  Upper triangular   
-            = 'L':  Lower triangular   
-
-    N       (input) INTEGER   
-            The order of the matrix A.  N >= 0.   
-
-    NB      (input) INTEGER   
-            The maximum number of columns of the matrix A that should be   
-            factored.  NB should be at least 2 to allow for 2-by-2 pivot   
-            blocks.   
-
-    KB      (output) INTEGER   
-            The number of columns of A that were actually factored.   
-            KB is either NB-1 or NB, or N if N <= NB.   
-
-    A       (input/output) DOUBLE PRECISION array, dimension (LDA,N)   
-            On entry, the symmetric matrix A.  If UPLO = 'U', the leading   
-            n-by-n upper triangular part of A contains the upper   
-            triangular part of the matrix A, and the strictly lower   
-            triangular part of A is not referenced.  If UPLO = 'L', the   
-            leading n-by-n lower triangular part of A contains the lower   
-            triangular part of the matrix A, and the strictly upper   
-            triangular part of A is not referenced.   
-            On exit, A contains details of the partial factorization.   
-
-    LDA     (input) INTEGER   
-            The leading dimension of the array A.  LDA >= max(1,N).   
-
-    IPIV    (output) INTEGER array, dimension (N)   
-            Details of the interchanges and the block structure of D.   
-            If UPLO = 'U', only the last KB elements of IPIV are set;   
-            if UPLO = 'L', only the first KB elements are set.   
-
-            If IPIV(k) > 0, then rows and columns k and IPIV(k) were   
-            interchanged and D(k,k) is a 1-by-1 diagonal block.   
-            If UPLO = 'U' and IPIV(k) = IPIV(k-1) < 0, then rows and   
-            columns k-1 and -IPIV(k) were interchanged and D(k-1:k,k-1:k)   
-            is a 2-by-2 diagonal block.  If UPLO = 'L' and IPIV(k) =   
-            IPIV(k+1) < 0, then rows and columns k+1 and -IPIV(k) were   
-            interchanged and D(k:k+1,k:k+1) is a 2-by-2 diagonal block.   
-
-    W       (workspace) DOUBLE PRECISION array, dimension (LDW,NB)   
-
-    LDW     (input) INTEGER   
-            The leading dimension of the array W.  LDW >= max(1,N).   
-
-    INFO    (output) INTEGER   
-            = 0: successful exit   
-            > 0: if INFO = k, D(k,k) is exactly zero.  The factorization   
-                 has been completed, but the block diagonal matrix D is   
-                 exactly singular.   
-
-    =====================================================================   
-
-
-       Parameter adjustments */
-    /* Table of constant values */
-    static integer c__1 = 1;
-    static doublereal c_b8 = -1.;
-    static doublereal c_b9 = 1.;
-    
     /* System generated locals */
     integer a_dim1, a_offset, w_dim1, w_offset, i__1, i__2, i__3, i__4, i__5;
     doublereal d__1, d__2, d__3;
+
     /* Builtin functions */
     double sqrt(doublereal);
+
     /* Local variables */
-    static integer j, k;
-    static doublereal t, r1, d11, d21, d22;
-    static integer jb, jj, kk, jp, kp, kw, kkw, imax, jmax;
-    static doublereal alpha;
+    integer j, k;
+    doublereal t, r1, d11, d21, d22;
+    integer jb, jj, kk, jp, kp, kw, kkw, imax, jmax;
+    doublereal alpha;
     extern /* Subroutine */ int dscal_(integer *, doublereal *, doublereal *, 
            integer *), dgemm_(char *, char *, integer *, integer *, integer *
 , doublereal *, doublereal *, integer *, doublereal *, integer *, 
            doublereal *, doublereal *, integer *), dcopy_(integer *, 
            doublereal *, integer *, doublereal *, integer *), dswap_(integer 
            *, doublereal *, integer *, doublereal *, integer *);
-    static integer kstep;
-    static doublereal absakk;
+    integer kstep;
+    doublereal absakk;
     extern integer idamax_(integer *, doublereal *, integer *);
-    static doublereal colmax, rowmax;
-
-
+    doublereal colmax, rowmax;
+
+
+/*  -- LAPACK routine (version 3.2) -- */
+/*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
+/*     November 2006 */
+
+/*     .. Scalar Arguments .. */
+/*     .. */
+/*     .. Array Arguments .. */
+/*     .. */
+
+/*  Purpose */
+/*  ======= */
+
+/*  DLASYF computes a partial factorization of a real symmetric matrix A */
+/*  using the Bunch-Kaufman diagonal pivoting method. The partial */
+/*  factorization has the form: */
+
+/*  A  =  ( I  U12 ) ( A11  0  ) (  I    0   )  if UPLO = 'U', or: */
+/*        ( 0  U22 ) (  0   D  ) ( U12' U22' ) */
+
+/*  A  =  ( L11  0 ) (  D   0  ) ( L11' L21' )  if UPLO = 'L' */
+/*        ( L21  I ) (  0  A22 ) (  0    I   ) */
+
+/*  where the order of D is at most NB. The actual order is returned in */
+/*  the argument KB, and is either NB or NB-1, or N if N <= NB. */
+
+/*  DLASYF is an auxiliary routine called by DSYTRF. It uses blocked code */
+/*  (calling Level 3 BLAS) to update the submatrix A11 (if UPLO = 'U') or */
+/*  A22 (if UPLO = 'L'). */
+
+/*  Arguments */
+/*  ========= */
+
+/*  UPLO    (input) CHARACTER*1 */
+/*          Specifies whether the upper or lower triangular part of the */
+/*          symmetric matrix A is stored: */
+/*          = 'U':  Upper triangular */
+/*          = 'L':  Lower triangular */
+
+/*  N       (input) INTEGER */
+/*          The order of the matrix A.  N >= 0. */
+
+/*  NB      (input) INTEGER */
+/*          The maximum number of columns of the matrix A that should be */
+/*          factored.  NB should be at least 2 to allow for 2-by-2 pivot */
+/*          blocks. */
+
+/*  KB      (output) INTEGER */
+/*          The number of columns of A that were actually factored. */
+/*          KB is either NB-1 or NB, or N if N <= NB. */
+
+/*  A       (input/output) DOUBLE PRECISION array, dimension (LDA,N) */
+/*          On entry, the symmetric matrix A.  If UPLO = 'U', the leading */
+/*          n-by-n upper triangular part of A contains the upper */
+/*          triangular part of the matrix A, and the strictly lower */
+/*          triangular part of A is not referenced.  If UPLO = 'L', the */
+/*          leading n-by-n lower triangular part of A contains the lower */
+/*          triangular part of the matrix A, and the strictly upper */
+/*          triangular part of A is not referenced. */
+/*          On exit, A contains details of the partial factorization. */
+
+/*  LDA     (input) INTEGER */
+/*          The leading dimension of the array A.  LDA >= max(1,N). */
+
+/*  IPIV    (output) INTEGER array, dimension (N) */
+/*          Details of the interchanges and the block structure of D. */
+/*          If UPLO = 'U', only the last KB elements of IPIV are set; */
+/*          if UPLO = 'L', only the first KB elements are set. */
+
+/*          If IPIV(k) > 0, then rows and columns k and IPIV(k) were */
+/*          interchanged and D(k,k) is a 1-by-1 diagonal block. */
+/*          If UPLO = 'U' and IPIV(k) = IPIV(k-1) < 0, then rows and */
+/*          columns k-1 and -IPIV(k) were interchanged and D(k-1:k,k-1:k) */
+/*          is a 2-by-2 diagonal block.  If UPLO = 'L' and IPIV(k) = */
+/*          IPIV(k+1) < 0, then rows and columns k+1 and -IPIV(k) were */
+/*          interchanged and D(k:k+1,k:k+1) is a 2-by-2 diagonal block. */
+
+/*  W       (workspace) DOUBLE PRECISION array, dimension (LDW,NB) */
+
+/*  LDW     (input) INTEGER */
+/*          The leading dimension of the array W.  LDW >= max(1,N). */
+
+/*  INFO    (output) INTEGER */
+/*          = 0: successful exit */
+/*          > 0: if INFO = k, D(k,k) is exactly zero.  The factorization */
+/*               has been completed, but the block diagonal matrix D is */
+/*               exactly singular. */
+
+/*  ===================================================================== */
+
+/*     .. Parameters .. */
+/*     .. */
+/*     .. Local Scalars .. */
+/*     .. */
+/*     .. External Functions .. */
+/*     .. */
+/*     .. External Subroutines .. */
+/*     .. */
+/*     .. Intrinsic Functions .. */
+/*     .. */
+/*     .. Executable Statements .. */
+
+    /* Parameter adjustments */
     a_dim1 = *lda;
     a_offset = 1 + a_dim1;
     a -= a_offset;
 
     if (lsame_(uplo, "U")) {
 
-/*        Factorize the trailing columns of A using the upper triangle   
-          of A and working backwards, and compute the matrix W = U12*D   
-          for use in updating A11   
+/*        Factorize the trailing columns of A using the upper triangle */
+/*        of A and working backwards, and compute the matrix W = U12*D */
+/*        for use in updating A11 */
 
-          K is the main loop index, decreasing from N in steps of 1 or 2   
+/*        K is the main loop index, decreasing from N in steps of 1 or 2 */
 
-          KW is the column of W which corresponds to column K of A */
+/*        KW is the column of W which corresponds to column K of A */
 
        k = *n;
 L10:
@@ -169,13 +200,13 @@ L10:
 
        kstep = 1;
 
-/*        Determine rows and columns to be interchanged and whether   
-          a 1-by-1 or 2-by-2 pivot block will be used */
+/*        Determine rows and columns to be interchanged and whether */
+/*        a 1-by-1 or 2-by-2 pivot block will be used */
 
        absakk = (d__1 = w[k + kw * w_dim1], abs(d__1));
 
-/*        IMAX is the row-index of the largest off-diagonal element in   
-          column K, and COLMAX is its absolute value */
+/*        IMAX is the row-index of the largest off-diagonal element in */
+/*        column K, and COLMAX is its absolute value */
 
        if (k > 1) {
            i__1 = k - 1;
@@ -215,8 +246,8 @@ L10:
                            ldw, &c_b9, &w[(kw - 1) * w_dim1 + 1], &c__1);
                }
 
-/*              JMAX is the column-index of the largest off-diagonal   
-                element in row IMAX, and ROWMAX is its absolute value */
+/*              JMAX is the column-index of the largest off-diagonal */
+/*              element in row IMAX, and ROWMAX is its absolute value */
 
                i__1 = k - imax;
                jmax = imax + idamax_(&i__1, &w[imax + 1 + (kw - 1) * w_dim1], 
@@ -239,8 +270,8 @@ L10:
                } else if ((d__1 = w[imax + (kw - 1) * w_dim1], abs(d__1)) >= 
                        alpha * rowmax) {
 
-/*                 interchange rows and columns K and IMAX, use 1-by-1   
-                   pivot block */
+/*                 interchange rows and columns K and IMAX, use 1-by-1 */
+/*                 pivot block */
 
                    kp = imax;
 
@@ -250,8 +281,8 @@ L10:
                            w_dim1 + 1], &c__1);
                } else {
 
-/*                 interchange rows and columns K-1 and IMAX, use 2-by-2   
-                   pivot block */
+/*                 interchange rows and columns K-1 and IMAX, use 2-by-2 */
+/*                 pivot block */
 
                    kp = imax;
                    kstep = 2;
@@ -286,13 +317,13 @@ L10:
 
            if (kstep == 1) {
 
-/*              1-by-1 pivot block D(k): column KW of W now holds   
+/*              1-by-1 pivot block D(k): column KW of W now holds */
 
-                W(k) = U(k)*D(k)   
+/*              W(k) = U(k)*D(k) */
 
-                where U(k) is the k-th column of U   
+/*              where U(k) is the k-th column of U */
 
-                Store U(k) in column k of A */
+/*              Store U(k) in column k of A */
 
                dcopy_(&k, &w[kw * w_dim1 + 1], &c__1, &a[k * a_dim1 + 1], &
                        c__1);
@@ -301,13 +332,13 @@ L10:
                dscal_(&i__1, &r1, &a[k * a_dim1 + 1], &c__1);
            } else {
 
-/*              2-by-2 pivot block D(k): columns KW and KW-1 of W now   
-                hold   
+/*              2-by-2 pivot block D(k): columns KW and KW-1 of W now */
+/*              hold */
 
-                ( W(k-1) W(k) ) = ( U(k-1) U(k) )*D(k)   
+/*              ( W(k-1) W(k) ) = ( U(k-1) U(k) )*D(k) */
 
-                where U(k) and U(k-1) are the k-th and (k-1)-th columns   
-                of U */
+/*              where U(k) and U(k-1) are the k-th and (k-1)-th columns */
+/*              of U */
 
                if (k > 2) {
 
@@ -352,11 +383,11 @@ L10:
 
 L30:
 
-/*        Update the upper triangle of A11 (= A(1:k,1:k)) as   
+/*        Update the upper triangle of A11 (= A(1:k,1:k)) as */
 
-          A11 := A11 - U12*D*U12' = A11 - U12*W'   
+/*        A11 := A11 - U12*D*U12' = A11 - U12*W' */
 
-          computing blocks of NB columns at a time */
+/*        computing blocks of NB columns at a time */
 
        i__1 = -(*nb);
        for (j = (k - 1) / *nb * *nb + 1; i__1 < 0 ? j >= 1 : j <= 1; j += 
@@ -387,8 +418,8 @@ L30:
 /* L50: */
        }
 
-/*        Put U12 in standard form by partially undoing the interchanges   
-          in columns k+1:n */
+/*        Put U12 in standard form by partially undoing the interchanges */
+/*        in columns k+1:n */
 
        j = k + 1;
 L60:
@@ -413,11 +444,11 @@ L60:
 
     } else {
 
-/*        Factorize the leading columns of A using the lower triangle   
-          of A and working forwards, and compute the matrix W = L21*D   
-          for use in updating A22   
+/*        Factorize the leading columns of A using the lower triangle */
+/*        of A and working forwards, and compute the matrix W = L21*D */
+/*        for use in updating A22 */
 
-          K is the main loop index, increasing from 1 in steps of 1 or 2 */
+/*        K is the main loop index, increasing from 1 in steps of 1 or 2 */
 
        k = 1;
 L70:
@@ -439,13 +470,13 @@ L70:
 
        kstep = 1;
 
-/*        Determine rows and columns to be interchanged and whether   
-          a 1-by-1 or 2-by-2 pivot block will be used */
+/*        Determine rows and columns to be interchanged and whether */
+/*        a 1-by-1 or 2-by-2 pivot block will be used */
 
        absakk = (d__1 = w[k + k * w_dim1], abs(d__1));
 
-/*        IMAX is the row-index of the largest off-diagonal element in   
-          column K, and COLMAX is its absolute value */
+/*        IMAX is the row-index of the largest off-diagonal element in */
+/*        column K, and COLMAX is its absolute value */
 
        if (k < *n) {
            i__1 = *n - k;
@@ -485,8 +516,8 @@ L70:
                        lda, &w[imax + w_dim1], ldw, &c_b9, &w[k + (k + 1) * 
                        w_dim1], &c__1);
 
-/*              JMAX is the column-index of the largest off-diagonal   
-                element in row IMAX, and ROWMAX is its absolute value */
+/*              JMAX is the column-index of the largest off-diagonal */
+/*              element in row IMAX, and ROWMAX is its absolute value */
 
                i__1 = imax - k;
                jmax = k - 1 + idamax_(&i__1, &w[k + (k + 1) * w_dim1], &c__1)
@@ -510,8 +541,8 @@ L70:
                } else if ((d__1 = w[imax + (k + 1) * w_dim1], abs(d__1)) >= 
                        alpha * rowmax) {
 
-/*                 interchange rows and columns K and IMAX, use 1-by-1   
-                   pivot block */
+/*                 interchange rows and columns K and IMAX, use 1-by-1 */
+/*                 pivot block */
 
                    kp = imax;
 
@@ -522,8 +553,8 @@ L70:
                            w_dim1], &c__1);
                } else {
 
-/*                 interchange rows and columns K+1 and IMAX, use 2-by-2   
-                   pivot block */
+/*                 interchange rows and columns K+1 and IMAX, use 2-by-2 */
+/*                 pivot block */
 
                    kp = imax;
                    kstep = 2;
@@ -554,13 +585,13 @@ L70:
 
            if (kstep == 1) {
 
-/*              1-by-1 pivot block D(k): column k of W now holds   
+/*              1-by-1 pivot block D(k): column k of W now holds */
 
-                W(k) = L(k)*D(k)   
+/*              W(k) = L(k)*D(k) */
 
-                where L(k) is the k-th column of L   
+/*              where L(k) is the k-th column of L */
 
-                Store L(k) in column k of A */
+/*              Store L(k) in column k of A */
 
                i__1 = *n - k + 1;
                dcopy_(&i__1, &w[k + k * w_dim1], &c__1, &a[k + k * a_dim1], &
@@ -572,12 +603,12 @@ L70:
                }
            } else {
 
-/*              2-by-2 pivot block D(k): columns k and k+1 of W now hold   
+/*              2-by-2 pivot block D(k): columns k and k+1 of W now hold */
 
-                ( W(k) W(k+1) ) = ( L(k) L(k+1) )*D(k)   
+/*              ( W(k) W(k+1) ) = ( L(k) L(k+1) )*D(k) */
 
-                where L(k) and L(k+1) are the k-th and (k+1)-th columns   
-                of L */
+/*              where L(k) and L(k+1) are the k-th and (k+1)-th columns */
+/*              of L */
 
                if (k < *n - 1) {
 
@@ -622,11 +653,11 @@ L70:
 
 L90:
 
-/*        Update the lower triangle of A22 (= A(k:n,k:n)) as   
+/*        Update the lower triangle of A22 (= A(k:n,k:n)) as */
 
-          A22 := A22 - L21*D*L21' = A22 - L21*W'   
+/*        A22 := A22 - L21*D*L21' = A22 - L21*W' */
 
-          computing blocks of NB columns at a time */
+/*        computing blocks of NB columns at a time */
 
        i__1 = *n;
        i__2 = *nb;
@@ -659,8 +690,8 @@ L90:
 /* L110: */
        }
 
-/*        Put L21 in standard form by partially undoing the interchanges   
-          in columns 1:k-1 */
+/*        Put L21 in standard form by partially undoing the interchanges */
+/*        in columns 1:k-1 */
 
        j = k - 1;
 L120:
index 845b9ac..4c80a5b 100644 (file)
@@ -1,5 +1,18 @@
+/* dlatrd.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static doublereal c_b5 = -1.;
@@ -31,7 +44,7 @@ static doublereal c_b16 = 0.;
             doublereal *);
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 8c768b2..f805c64 100644 (file)
@@ -1,5 +1,18 @@
+/* dlauu2.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static doublereal c_b7 = 1.;
@@ -26,7 +39,7 @@ static integer c__1 = 1;
     extern /* Subroutine */ int xerbla_(char *, integer *);
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 1d8156d..f1bdf33 100644 (file)
@@ -1,5 +1,18 @@
+/* dlauum.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -31,7 +44,7 @@ static doublereal c_b15 = 1.;
            integer *, integer *);
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
diff --git a/3rdparty/lapack/dlazq3.c b/3rdparty/lapack/dlazq3.c
deleted file mode 100644 (file)
index f709455..0000000
+++ /dev/null
@@ -1,338 +0,0 @@
-#include "clapack.h"
-
-/* Subroutine */ int dlazq3_(integer *i0, integer *n0, doublereal *z__, 
-       integer *pp, doublereal *dmin__, doublereal *sigma, doublereal *desig, 
-        doublereal *qmax, integer *nfail, integer *iter, integer *ndiv, 
-       logical *ieee, integer *ttype, doublereal *dmin1, doublereal *dmin2, 
-       doublereal *dn, doublereal *dn1, doublereal *dn2, doublereal *tau)
-{
-    /* System generated locals */
-    integer i__1;
-    doublereal d__1, d__2;
-
-    /* Builtin functions */
-    double sqrt(doublereal);
-
-    /* Local variables */
-    doublereal g, s, t;
-    integer j4, nn;
-    doublereal eps, tol;
-    integer n0in, ipn4;
-    doublereal tol2, temp;
-    extern /* Subroutine */ int dlasq5_(integer *, integer *, doublereal *, 
-           integer *, doublereal *, doublereal *, doublereal *, doublereal *, 
-            doublereal *, doublereal *, doublereal *, logical *), dlasq6_(
-           integer *, integer *, doublereal *, integer *, doublereal *, 
-           doublereal *, doublereal *, doublereal *, doublereal *, 
-           doublereal *), dlazq4_(integer *, integer *, doublereal *, 
-           integer *, integer *, doublereal *, doublereal *, doublereal *, 
-           doublereal *, doublereal *, doublereal *, doublereal *, integer *, 
-            doublereal *);
-    extern doublereal dlamch_(char *);
-    doublereal safmin;
-
-
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
-/*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
-/*     November 2006 */
-
-/*     .. Scalar Arguments .. */
-/*     .. */
-/*     .. Array Arguments .. */
-/*     .. */
-
-/*  Purpose */
-/*  ======= */
-
-/*  DLAZQ3 checks for deflation, computes a shift (TAU) and calls dqds. */
-/*  In case of failure it changes shifts, and tries again until output */
-/*  is positive. */
-
-/*  Arguments */
-/*  ========= */
-
-/*  I0     (input) INTEGER */
-/*         First index. */
-
-/*  N0     (input) INTEGER */
-/*         Last index. */
-
-/*  Z      (input) DOUBLE PRECISION array, dimension ( 4*N ) */
-/*         Z holds the qd array. */
-
-/*  PP     (input) INTEGER */
-/*         PP=0 for ping, PP=1 for pong. */
-
-/*  DMIN   (output) DOUBLE PRECISION */
-/*         Minimum value of d. */
-
-/*  SIGMA  (output) DOUBLE PRECISION */
-/*         Sum of shifts used in current segment. */
-
-/*  DESIG  (input/output) DOUBLE PRECISION */
-/*         Lower order part of SIGMA */
-
-/*  QMAX   (input) DOUBLE PRECISION */
-/*         Maximum value of q. */
-
-/*  NFAIL  (output) INTEGER */
-/*         Number of times shift was too big. */
-
-/*  ITER   (output) INTEGER */
-/*         Number of iterations. */
-
-/*  NDIV   (output) INTEGER */
-/*         Number of divisions. */
-
-/*  IEEE   (input) LOGICAL */
-/*         Flag for IEEE or non IEEE arithmetic (passed to DLASQ5). */
-
-/*  TTYPE  (input/output) INTEGER */
-/*         Shift type.  TTYPE is passed as an argument in order to save */
-/*         its value between calls to DLAZQ3 */
-
-/*  DMIN1  (input/output) REAL */
-/*  DMIN2  (input/output) REAL */
-/*  DN     (input/output) REAL */
-/*  DN1    (input/output) REAL */
-/*  DN2    (input/output) REAL */
-/*  TAU    (input/output) REAL */
-/*         These are passed as arguments in order to save their values */
-/*         between calls to DLAZQ3 */
-
-/*  This is a thread safe version of DLASQ3, which passes TTYPE, DMIN1, */
-/*  DMIN2, DN, DN1. DN2 and TAU through the argument list in place of */
-/*  declaring them in a SAVE statment. */
-
-/*  ===================================================================== */
-
-/*     .. Parameters .. */
-/*     .. */
-/*     .. Local Scalars .. */
-/*     .. */
-/*     .. External Subroutines .. */
-/*     .. */
-/*     .. External Function .. */
-/*     .. */
-/*     .. Intrinsic Functions .. */
-/*     .. */
-/*     .. Executable Statements .. */
-
-    /* Parameter adjustments */
-    --z__;
-
-    /* Function Body */
-    n0in = *n0;
-    eps = dlamch_("Precision");
-    safmin = dlamch_("Safe minimum");
-    tol = eps * 100.;
-/* Computing 2nd power */
-    d__1 = tol;
-    tol2 = d__1 * d__1;
-    g = 0.;
-
-/*     Check for deflation. */
-
-L10:
-
-    if (*n0 < *i0) {
-       return 0;
-    }
-    if (*n0 == *i0) {
-       goto L20;
-    }
-    nn = (*n0 << 2) + *pp;
-    if (*n0 == *i0 + 1) {
-       goto L40;
-    }
-
-/*     Check whether E(N0-1) is negligible, 1 eigenvalue. */
-
-    if (z__[nn - 5] > tol2 * (*sigma + z__[nn - 3]) && z__[nn - (*pp << 1) - 
-           4] > tol2 * z__[nn - 7]) {
-       goto L30;
-    }
-
-L20:
-
-    z__[(*n0 << 2) - 3] = z__[(*n0 << 2) + *pp - 3] + *sigma;
-    --(*n0);
-    goto L10;
-
-/*     Check  whether E(N0-2) is negligible, 2 eigenvalues. */
-
-L30:
-
-    if (z__[nn - 9] > tol2 * *sigma && z__[nn - (*pp << 1) - 8] > tol2 * z__[
-           nn - 11]) {
-       goto L50;
-    }
-
-L40:
-
-    if (z__[nn - 3] > z__[nn - 7]) {
-       s = z__[nn - 3];
-       z__[nn - 3] = z__[nn - 7];
-       z__[nn - 7] = s;
-    }
-    if (z__[nn - 5] > z__[nn - 3] * tol2) {
-       t = (z__[nn - 7] - z__[nn - 3] + z__[nn - 5]) * .5;
-       s = z__[nn - 3] * (z__[nn - 5] / t);
-       if (s <= t) {
-           s = z__[nn - 3] * (z__[nn - 5] / (t * (sqrt(s / t + 1.) + 1.)));
-       } else {
-           s = z__[nn - 3] * (z__[nn - 5] / (t + sqrt(t) * sqrt(t + s)));
-       }
-       t = z__[nn - 7] + (s + z__[nn - 5]);
-       z__[nn - 3] *= z__[nn - 7] / t;
-       z__[nn - 7] = t;
-    }
-    z__[(*n0 << 2) - 7] = z__[nn - 7] + *sigma;
-    z__[(*n0 << 2) - 3] = z__[nn - 3] + *sigma;
-    *n0 += -2;
-    goto L10;
-
-L50:
-
-/*     Reverse the qd-array, if warranted. */
-
-    if (*dmin__ <= 0. || *n0 < n0in) {
-       if (z__[(*i0 << 2) + *pp - 3] * 1.5 < z__[(*n0 << 2) + *pp - 3]) {
-           ipn4 = *i0 + *n0 << 2;
-           i__1 = *i0 + *n0 - 1 << 1;
-           for (j4 = *i0 << 2; j4 <= i__1; j4 += 4) {
-               temp = z__[j4 - 3];
-               z__[j4 - 3] = z__[ipn4 - j4 - 3];
-               z__[ipn4 - j4 - 3] = temp;
-               temp = z__[j4 - 2];
-               z__[j4 - 2] = z__[ipn4 - j4 - 2];
-               z__[ipn4 - j4 - 2] = temp;
-               temp = z__[j4 - 1];
-               z__[j4 - 1] = z__[ipn4 - j4 - 5];
-               z__[ipn4 - j4 - 5] = temp;
-               temp = z__[j4];
-               z__[j4] = z__[ipn4 - j4 - 4];
-               z__[ipn4 - j4 - 4] = temp;
-/* L60: */
-           }
-           if (*n0 - *i0 <= 4) {
-               z__[(*n0 << 2) + *pp - 1] = z__[(*i0 << 2) + *pp - 1];
-               z__[(*n0 << 2) - *pp] = z__[(*i0 << 2) - *pp];
-           }
-/* Computing MIN */
-           d__1 = *dmin2, d__2 = z__[(*n0 << 2) + *pp - 1];
-           *dmin2 = min(d__1,d__2);
-/* Computing MIN */
-           d__1 = z__[(*n0 << 2) + *pp - 1], d__2 = z__[(*i0 << 2) + *pp - 1]
-                   , d__1 = min(d__1,d__2), d__2 = z__[(*i0 << 2) + *pp + 3];
-           z__[(*n0 << 2) + *pp - 1] = min(d__1,d__2);
-/* Computing MIN */
-           d__1 = z__[(*n0 << 2) - *pp], d__2 = z__[(*i0 << 2) - *pp], d__1 =
-                    min(d__1,d__2), d__2 = z__[(*i0 << 2) - *pp + 4];
-           z__[(*n0 << 2) - *pp] = min(d__1,d__2);
-/* Computing MAX */
-           d__1 = *qmax, d__2 = z__[(*i0 << 2) + *pp - 3], d__1 = max(d__1,
-                   d__2), d__2 = z__[(*i0 << 2) + *pp + 1];
-           *qmax = max(d__1,d__2);
-           *dmin__ = -0.;
-       }
-    }
-
-/* Computing MIN */
-    d__1 = z__[(*n0 << 2) + *pp - 1], d__2 = z__[(*n0 << 2) + *pp - 9], d__1 =
-            min(d__1,d__2), d__2 = *dmin2 + z__[(*n0 << 2) - *pp];
-    if (*dmin__ < 0. || safmin * *qmax < min(d__1,d__2)) {
-
-/*        Choose a shift. */
-
-       dlazq4_(i0, n0, &z__[1], pp, &n0in, dmin__, dmin1, dmin2, dn, dn1, 
-               dn2, tau, ttype, &g);
-
-/*        Call dqds until DMIN > 0. */
-
-L80:
-
-       dlasq5_(i0, n0, &z__[1], pp, tau, dmin__, dmin1, dmin2, dn, dn1, dn2, 
-               ieee);
-
-       *ndiv += *n0 - *i0 + 2;
-       ++(*iter);
-
-/*        Check status. */
-
-       if (*dmin__ >= 0. && *dmin1 > 0.) {
-
-/*           Success. */
-
-           goto L100;
-
-       } else if (*dmin__ < 0. && *dmin1 > 0. && z__[(*n0 - 1 << 2) - *pp] < 
-               tol * (*sigma + *dn1) && abs(*dn) < tol * *sigma) {
-
-/*           Convergence hidden by negative DN. */
-
-           z__[(*n0 - 1 << 2) - *pp + 2] = 0.;
-           *dmin__ = 0.;
-           goto L100;
-       } else if (*dmin__ < 0.) {
-
-/*           TAU too big. Select new TAU and try again. */
-
-           ++(*nfail);
-           if (*ttype < -22) {
-
-/*              Failed twice. Play it safe. */
-
-               *tau = 0.;
-           } else if (*dmin1 > 0.) {
-
-/*              Late failure. Gives excellent shift. */
-
-               *tau = (*tau + *dmin__) * (1. - eps * 2.);
-               *ttype += -11;
-           } else {
-
-/*              Early failure. Divide by 4. */
-
-               *tau *= .25;
-               *ttype += -12;
-           }
-           goto L80;
-       } else if (*dmin__ != *dmin__) {
-
-/*           NaN. */
-
-           *tau = 0.;
-           goto L80;
-       } else {
-
-/*           Possible underflow. Play it safe. */
-
-           goto L90;
-       }
-    }
-
-/*     Risk of underflow. */
-
-L90:
-    dlasq6_(i0, n0, &z__[1], pp, dmin__, dmin1, dmin2, dn, dn1, dn2);
-    *ndiv += *n0 - *i0 + 2;
-    ++(*iter);
-    *tau = 0.;
-
-L100:
-    if (*tau < *sigma) {
-       *desig += *tau;
-       t = *sigma + *desig;
-       *desig -= t - *sigma;
-    } else {
-       t = *sigma + *tau;
-       *desig = *sigma - (t - *tau) + *desig;
-    }
-    *sigma = t;
-
-    return 0;
-
-/*     End of DLAZQ3 */
-
-} /* dlazq3_ */
diff --git a/3rdparty/lapack/dlazq4.c b/3rdparty/lapack/dlazq4.c
deleted file mode 100644 (file)
index bb0f65a..0000000
+++ /dev/null
@@ -1,387 +0,0 @@
-#include "clapack.h"
-
-/* Subroutine */ int dlazq4_(integer *i0, integer *n0, doublereal *z__, 
-       integer *pp, integer *n0in, doublereal *dmin__, doublereal *dmin1, 
-       doublereal *dmin2, doublereal *dn, doublereal *dn1, doublereal *dn2, 
-       doublereal *tau, integer *ttype, doublereal *g)
-{
-    /* System generated locals */
-    integer i__1;
-    doublereal d__1, d__2;
-
-    /* Builtin functions */
-    double sqrt(doublereal);
-
-    /* Local variables */
-    doublereal s, a2, b1, b2;
-    integer i4, nn, np;
-    doublereal gam, gap1, gap2;
-
-
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
-/*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
-/*     November 2006 */
-
-/*     .. Scalar Arguments .. */
-/*     .. */
-/*     .. Array Arguments .. */
-/*     .. */
-
-/*  Purpose */
-/*  ======= */
-
-/*  DLAZQ4 computes an approximation TAU to the smallest eigenvalue */
-/*  using values of d from the previous transform. */
-
-/*  I0    (input) INTEGER */
-/*        First index. */
-
-/*  N0    (input) INTEGER */
-/*        Last index. */
-
-/*  Z     (input) DOUBLE PRECISION array, dimension ( 4*N ) */
-/*        Z holds the qd array. */
-
-/*  PP    (input) INTEGER */
-/*        PP=0 for ping, PP=1 for pong. */
-
-/*  N0IN  (input) INTEGER */
-/*        The value of N0 at start of EIGTEST. */
-
-/*  DMIN  (input) DOUBLE PRECISION */
-/*        Minimum value of d. */
-
-/*  DMIN1 (input) DOUBLE PRECISION */
-/*        Minimum value of d, excluding D( N0 ). */
-
-/*  DMIN2 (input) DOUBLE PRECISION */
-/*        Minimum value of d, excluding D( N0 ) and D( N0-1 ). */
-
-/*  DN    (input) DOUBLE PRECISION */
-/*        d(N) */
-
-/*  DN1   (input) DOUBLE PRECISION */
-/*        d(N-1) */
-
-/*  DN2   (input) DOUBLE PRECISION */
-/*        d(N-2) */
-
-/*  TAU   (output) DOUBLE PRECISION */
-/*        This is the shift. */
-
-/*  TTYPE (output) INTEGER */
-/*        Shift type. */
-
-/*  G     (input/output) DOUBLE PRECISION */
-/*        G is passed as an argument in order to save its value between */
-/*        calls to DLAZQ4 */
-
-/*  Further Details */
-/*  =============== */
-/*  CNST1 = 9/16 */
-
-/*  This is a thread safe version of DLASQ4, which passes G through the */
-/*  argument list in place of declaring G in a SAVE statment. */
-
-/*  ===================================================================== */
-
-/*     .. Parameters .. */
-/*     .. */
-/*     .. Local Scalars .. */
-/*     .. */
-/*     .. Intrinsic Functions .. */
-/*     .. */
-/*     .. Executable Statements .. */
-
-/*     A negative DMIN forces the shift to take that absolute value */
-/*     TTYPE records the type of shift. */
-
-    /* Parameter adjustments */
-    --z__;
-
-    /* Function Body */
-    if (*dmin__ <= 0.) {
-       *tau = -(*dmin__);
-       *ttype = -1;
-       return 0;
-    }
-
-    nn = (*n0 << 2) + *pp;
-    if (*n0in == *n0) {
-
-/*        No eigenvalues deflated. */
-
-       if (*dmin__ == *dn || *dmin__ == *dn1) {
-
-           b1 = sqrt(z__[nn - 3]) * sqrt(z__[nn - 5]);
-           b2 = sqrt(z__[nn - 7]) * sqrt(z__[nn - 9]);
-           a2 = z__[nn - 7] + z__[nn - 5];
-
-/*           Cases 2 and 3. */
-
-           if (*dmin__ == *dn && *dmin1 == *dn1) {
-               gap2 = *dmin2 - a2 - *dmin2 * .25;
-               if (gap2 > 0. && gap2 > b2) {
-                   gap1 = a2 - *dn - b2 / gap2 * b2;
-               } else {
-                   gap1 = a2 - *dn - (b1 + b2);
-               }
-               if (gap1 > 0. && gap1 > b1) {
-/* Computing MAX */
-                   d__1 = *dn - b1 / gap1 * b1, d__2 = *dmin__ * .5;
-                   s = max(d__1,d__2);
-                   *ttype = -2;
-               } else {
-                   s = 0.;
-                   if (*dn > b1) {
-                       s = *dn - b1;
-                   }
-                   if (a2 > b1 + b2) {
-/* Computing MIN */
-                       d__1 = s, d__2 = a2 - (b1 + b2);
-                       s = min(d__1,d__2);
-                   }
-/* Computing MAX */
-                   d__1 = s, d__2 = *dmin__ * .333;
-                   s = max(d__1,d__2);
-                   *ttype = -3;
-               }
-           } else {
-
-/*              Case 4. */
-
-               *ttype = -4;
-               s = *dmin__ * .25;
-               if (*dmin__ == *dn) {
-                   gam = *dn;
-                   a2 = 0.;
-                   if (z__[nn - 5] > z__[nn - 7]) {
-                       return 0;
-                   }
-                   b2 = z__[nn - 5] / z__[nn - 7];
-                   np = nn - 9;
-               } else {
-                   np = nn - (*pp << 1);
-                   b2 = z__[np - 2];
-                   gam = *dn1;
-                   if (z__[np - 4] > z__[np - 2]) {
-                       return 0;
-                   }
-                   a2 = z__[np - 4] / z__[np - 2];
-                   if (z__[nn - 9] > z__[nn - 11]) {
-                       return 0;
-                   }
-                   b2 = z__[nn - 9] / z__[nn - 11];
-                   np = nn - 13;
-               }
-
-/*              Approximate contribution to norm squared from I < NN-1. */
-
-               a2 += b2;
-               i__1 = (*i0 << 2) - 1 + *pp;
-               for (i4 = np; i4 >= i__1; i4 += -4) {
-                   if (b2 == 0.) {
-                       goto L20;
-                   }
-                   b1 = b2;
-                   if (z__[i4] > z__[i4 - 2]) {
-                       return 0;
-                   }
-                   b2 *= z__[i4] / z__[i4 - 2];
-                   a2 += b2;
-                   if (max(b2,b1) * 100. < a2 || .563 < a2) {
-                       goto L20;
-                   }
-/* L10: */
-               }
-L20:
-               a2 *= 1.05;
-
-/*              Rayleigh quotient residual bound. */
-
-               if (a2 < .563) {
-                   s = gam * (1. - sqrt(a2)) / (a2 + 1.);
-               }
-           }
-       } else if (*dmin__ == *dn2) {
-
-/*           Case 5. */
-
-           *ttype = -5;
-           s = *dmin__ * .25;
-
-/*           Compute contribution to norm squared from I > NN-2. */
-
-           np = nn - (*pp << 1);
-           b1 = z__[np - 2];
-           b2 = z__[np - 6];
-           gam = *dn2;
-           if (z__[np - 8] > b2 || z__[np - 4] > b1) {
-               return 0;
-           }
-           a2 = z__[np - 8] / b2 * (z__[np - 4] / b1 + 1.);
-
-/*           Approximate contribution to norm squared from I < NN-2. */
-
-           if (*n0 - *i0 > 2) {
-               b2 = z__[nn - 13] / z__[nn - 15];
-               a2 += b2;
-               i__1 = (*i0 << 2) - 1 + *pp;
-               for (i4 = nn - 17; i4 >= i__1; i4 += -4) {
-                   if (b2 == 0.) {
-                       goto L40;
-                   }
-                   b1 = b2;
-                   if (z__[i4] > z__[i4 - 2]) {
-                       return 0;
-                   }
-                   b2 *= z__[i4] / z__[i4 - 2];
-                   a2 += b2;
-                   if (max(b2,b1) * 100. < a2 || .563 < a2) {
-                       goto L40;
-                   }
-/* L30: */
-               }
-L40:
-               a2 *= 1.05;
-           }
-
-           if (a2 < .563) {
-               s = gam * (1. - sqrt(a2)) / (a2 + 1.);
-           }
-       } else {
-
-/*           Case 6, no information to guide us. */
-
-           if (*ttype == -6) {
-               *g += (1. - *g) * .333;
-           } else if (*ttype == -18) {
-               *g = .083250000000000005;
-           } else {
-               *g = .25;
-           }
-           s = *g * *dmin__;
-           *ttype = -6;
-       }
-
-    } else if (*n0in == *n0 + 1) {
-
-/*        One eigenvalue just deflated. Use DMIN1, DN1 for DMIN and DN. */
-
-       if (*dmin1 == *dn1 && *dmin2 == *dn2) {
-
-/*           Cases 7 and 8. */
-
-           *ttype = -7;
-           s = *dmin1 * .333;
-           if (z__[nn - 5] > z__[nn - 7]) {
-               return 0;
-           }
-           b1 = z__[nn - 5] / z__[nn - 7];
-           b2 = b1;
-           if (b2 == 0.) {
-               goto L60;
-           }
-           i__1 = (*i0 << 2) - 1 + *pp;
-           for (i4 = (*n0 << 2) - 9 + *pp; i4 >= i__1; i4 += -4) {
-               a2 = b1;
-               if (z__[i4] > z__[i4 - 2]) {
-                   return 0;
-               }
-               b1 *= z__[i4] / z__[i4 - 2];
-               b2 += b1;
-               if (max(b1,a2) * 100. < b2) {
-                   goto L60;
-               }
-/* L50: */
-           }
-L60:
-           b2 = sqrt(b2 * 1.05);
-/* Computing 2nd power */
-           d__1 = b2;
-           a2 = *dmin1 / (d__1 * d__1 + 1.);
-           gap2 = *dmin2 * .5 - a2;
-           if (gap2 > 0. && gap2 > b2 * a2) {
-/* Computing MAX */
-               d__1 = s, d__2 = a2 * (1. - a2 * 1.01 * (b2 / gap2) * b2);
-               s = max(d__1,d__2);
-           } else {
-/* Computing MAX */
-               d__1 = s, d__2 = a2 * (1. - b2 * 1.01);
-               s = max(d__1,d__2);
-               *ttype = -8;
-           }
-       } else {
-
-/*           Case 9. */
-
-           s = *dmin1 * .25;
-           if (*dmin1 == *dn1) {
-               s = *dmin1 * .5;
-           }
-           *ttype = -9;
-       }
-
-    } else if (*n0in == *n0 + 2) {
-
-/*        Two eigenvalues deflated. Use DMIN2, DN2 for DMIN and DN. */
-
-/*        Cases 10 and 11. */
-
-       if (*dmin2 == *dn2 && z__[nn - 5] * 2. < z__[nn - 7]) {
-           *ttype = -10;
-           s = *dmin2 * .333;
-           if (z__[nn - 5] > z__[nn - 7]) {
-               return 0;
-           }
-           b1 = z__[nn - 5] / z__[nn - 7];
-           b2 = b1;
-           if (b2 == 0.) {
-               goto L80;
-           }
-           i__1 = (*i0 << 2) - 1 + *pp;
-           for (i4 = (*n0 << 2) - 9 + *pp; i4 >= i__1; i4 += -4) {
-               if (z__[i4] > z__[i4 - 2]) {
-                   return 0;
-               }
-               b1 *= z__[i4] / z__[i4 - 2];
-               b2 += b1;
-               if (b1 * 100. < b2) {
-                   goto L80;
-               }
-/* L70: */
-           }
-L80:
-           b2 = sqrt(b2 * 1.05);
-/* Computing 2nd power */
-           d__1 = b2;
-           a2 = *dmin2 / (d__1 * d__1 + 1.);
-           gap2 = z__[nn - 7] + z__[nn - 9] - sqrt(z__[nn - 11]) * sqrt(z__[
-                   nn - 9]) - a2;
-           if (gap2 > 0. && gap2 > b2 * a2) {
-/* Computing MAX */
-               d__1 = s, d__2 = a2 * (1. - a2 * 1.01 * (b2 / gap2) * b2);
-               s = max(d__1,d__2);
-           } else {
-/* Computing MAX */
-               d__1 = s, d__2 = a2 * (1. - b2 * 1.01);
-               s = max(d__1,d__2);
-           }
-       } else {
-           s = *dmin2 * .25;
-           *ttype = -11;
-       }
-    } else if (*n0in > *n0 + 2) {
-
-/*        Case 12, more than two eigenvalues deflated. No information. */
-
-       s = 0.;
-       *ttype = -12;
-    }
-
-    *tau = s;
-    return 0;
-
-/*     End of DLAZQ4 */
-
-} /* dlazq4_ */
index 253647a..b1546e8 100644 (file)
@@ -1,5 +1,18 @@
+/* dnrm2.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 doublereal dnrm2_(integer *n, doublereal *x, integer *incx)
 {
     /* System generated locals */
index 774feb0..2889004 100644 (file)
@@ -1,5 +1,18 @@
+/* dorg2r.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -18,7 +31,7 @@ static integer c__1 = 1;
            integer *, doublereal *, doublereal *, integer *, doublereal *), xerbla_(char *, integer *);
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index b55e177..029b09c 100644 (file)
@@ -1,5 +1,18 @@
+/* dorgbr.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -28,7 +41,7 @@ static integer c_n1 = -1;
     logical lquery;
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 7b0d7e6..26b7f85 100644 (file)
@@ -1,5 +1,18 @@
+/* dorgl2.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int dorgl2_(integer *m, integer *n, integer *k, doublereal *
        a, integer *lda, doublereal *tau, doublereal *work, integer *info)
 {
@@ -14,7 +27,7 @@
            integer *, doublereal *, doublereal *, integer *, doublereal *), xerbla_(char *, integer *);
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 6850749..ee11743 100644 (file)
@@ -1,5 +1,18 @@
+/* dorglq.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -28,7 +41,7 @@ static integer c__2 = 2;
     logical lquery;
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index b4407fc..8f34e49 100644 (file)
@@ -1,5 +1,18 @@
+/* dorgqr.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -28,7 +41,7 @@ static integer c__2 = 2;
     logical lquery;
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 6811ae0..f826a2d 100644 (file)
@@ -1,5 +1,18 @@
+/* dorm2l.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -23,7 +36,7 @@ static integer c__1 = 1;
     logical notran;
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 938f5c8..22d3988 100644 (file)
@@ -1,5 +1,18 @@
+/* dorm2r.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -23,7 +36,7 @@ static integer c__1 = 1;
     logical notran;
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index bbef42a..5b96dad 100644 (file)
@@ -1,5 +1,18 @@
+/* dormbr.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -40,7 +53,7 @@ static integer c__2 = 2;
     logical lquery;
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index eeb5d94..58382f0 100644 (file)
@@ -1,5 +1,18 @@
+/* dorml2.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int dorml2_(char *side, char *trans, integer *m, integer *n, 
        integer *k, doublereal *a, integer *lda, doublereal *tau, doublereal *
        c__, integer *ldc, doublereal *work, integer *info)
@@ -19,7 +32,7 @@
     logical notran;
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 1b17173..d70ae7d 100644 (file)
@@ -1,5 +1,18 @@
+/* dormlq.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -43,7 +56,7 @@ static integer c__65 = 65;
     logical lquery;
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 0173971..d0b2a16 100644 (file)
@@ -1,5 +1,18 @@
+/* dormql.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -41,7 +54,7 @@ static integer c__65 = 65;
     logical lquery;
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index a770809..2d5d28e 100644 (file)
@@ -1,5 +1,18 @@
+/* dormqr.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -41,7 +54,7 @@ static integer c__65 = 65;
     logical lquery;
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index ab294d1..3d844c9 100644 (file)
@@ -1,5 +1,18 @@
+/* dormtr.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -37,7 +50,7 @@ static integer c__2 = 2;
     logical lquery;
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 1c3dae0..bc6d77c 100644 (file)
@@ -1,5 +1,18 @@
+/* dpotf2.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -28,10 +41,11 @@ static doublereal c_b12 = 1.;
            doublereal *, doublereal *, integer *, doublereal *, integer *, 
            doublereal *, doublereal *, integer *);
     logical upper;
+    extern logical disnan_(doublereal *);
     extern /* Subroutine */ int xerbla_(char *, integer *);
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
@@ -142,7 +156,7 @@ static doublereal c_b12 = 1.;
            i__2 = j - 1;
            ajj = a[j + j * a_dim1] - ddot_(&i__2, &a[j * a_dim1 + 1], &c__1, 
                    &a[j * a_dim1 + 1], &c__1);
-           if (ajj <= 0.) {
+           if (ajj <= 0. || disnan_(&ajj)) {
                a[j + j * a_dim1] = ajj;
                goto L30;
            }
@@ -175,7 +189,7 @@ static doublereal c_b12 = 1.;
            i__2 = j - 1;
            ajj = a[j + j * a_dim1] - ddot_(&i__2, &a[j + a_dim1], lda, &a[j 
                    + a_dim1], lda);
-           if (ajj <= 0.) {
+           if (ajj <= 0. || disnan_(&ajj)) {
                a[j + j * a_dim1] = ajj;
                goto L30;
            }
index ff4dc54..5983abb 100644 (file)
@@ -1,5 +1,18 @@
+/* dpotrf.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -32,7 +45,7 @@ static doublereal c_b14 = 1.;
            integer *, integer *);
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 70e2296..d69bd45 100644 (file)
@@ -1,5 +1,18 @@
+/* dpotri.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int dpotri_(char *uplo, integer *n, doublereal *a, integer *
        lda, integer *info)
 {
@@ -14,7 +27,7 @@
            integer *);
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index a13c5ad..2f2ca66 100644 (file)
@@ -1,5 +1,18 @@
+/* dpotrs.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static doublereal c_b9 = 1.;
@@ -20,7 +33,7 @@ static doublereal c_b9 = 1.;
     extern /* Subroutine */ int xerbla_(char *, integer *);
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 50b1ab1..8e17f35 100644 (file)
@@ -1,5 +1,18 @@
+/* drot.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int drot_(integer *n, doublereal *dx, integer *incx, 
        doublereal *dy, integer *incy, doublereal *c__, doublereal *s)
 {
index 82e0a6c..f39dfd4 100644 (file)
@@ -1,5 +1,18 @@
+/* dscal.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int dscal_(integer *n, doublereal *da, doublereal *dx, 
        integer *incx)
 {
index 205f530..9748de0 100644 (file)
@@ -1,5 +1,18 @@
+/* dstebz.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -59,7 +72,7 @@ static integer c__0 = 0;
     logical toofew;
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 /*     8-18-00:  Increase FUDGE factor for T3E (eca) */
index c0d12ee..c2f047e 100644 (file)
@@ -1,5 +1,18 @@
+/* dstein.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__2 = 2;
@@ -52,7 +65,7 @@ static integer c_n1 = -1;
     doublereal onenrm, dtpcrt, pertol;
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 0209d70..dc34245 100644 (file)
@@ -1,5 +1,18 @@
+/* dstemr.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -88,7 +101,7 @@ static doublereal c_b18 = .001;
     logical lquery, zquery;
 
 
-/*  -- LAPACK computational routine (version 3.1) -- */
+/*  -- LAPACK computational routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
@@ -325,7 +338,6 @@ static doublereal c_b18 = .001;
 
     lquery = *lwork == -1 || *liwork == -1;
     zquery = *nzc == -1;
-    *tryrac = *info != 0;
 /*     DSTEMR needs WORK of size 6*N, IWORK of size 3*N. */
 /*     In addition, DLARRE needs WORK of size 6*N, IWORK of size 5*N. */
 /*     Furthermore, DLARRV needs WORK of size 12*N, IWORK of size 7*N. */
index 07cc44b..1dc354e 100644 (file)
@@ -1,5 +1,18 @@
+/* dsteqr.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static doublereal c_b9 = 0.;
@@ -59,7 +72,7 @@ static integer c__2 = 2;
     doublereal ssfmax;
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 004c045..ea66b97 100644 (file)
@@ -1,5 +1,18 @@
+/* dsterf.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__0 = 0;
@@ -45,7 +58,7 @@ static doublereal c_b32 = 1.;
     doublereal ssfmax;
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index ebf46c6..2e29260 100644 (file)
@@ -1,5 +1,18 @@
+/* dswap.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int dswap_(integer *n, doublereal *dx, integer *incx, 
        doublereal *dy, integer *incy)
 {
index 416dc00..898eee7 100644 (file)
@@ -1,5 +1,18 @@
+/* dsyevr.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__10 = 10;
@@ -82,7 +95,7 @@ static integer c_n1 = -1;
     logical lquery;
 
 
-/*  -- LAPACK driver routine (version 3.1) -- */
+/*  -- LAPACK driver routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
@@ -526,7 +539,7 @@ static integer c_n1 = -1;
            dcopy_(&i__1, &work[inde], &c__1, &work[indee], &c__1);
            dcopy_(n, &work[indd], &c__1, &work[inddd], &c__1);
 
-           if (*abstol <= *n * 0. * eps) {
+           if (*abstol <= *n * 2. * eps) {
                tryrac = TRUE_;
            } else {
                tryrac = FALSE_;
index 9da4f72..aa190ec 100644 (file)
@@ -1,5 +1,18 @@
+/* dsymv.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int dsymv_(char *uplo, integer *n, doublereal *alpha, 
        doublereal *a, integer *lda, doublereal *x, integer *incx, doublereal 
        *beta, doublereal *y, integer *incy)
index 24877c2..a4616b1 100644 (file)
-
-/*  -- translated by f2c (version 19940927).
-   You must link the resulting object file with the libraries:
-       -lf2c -lm   (in that order)
+/* dsyr.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
 */
 
 #include "clapack.h"
 
+
 /* Subroutine */ int dsyr_(char *uplo, integer *n, doublereal *alpha, 
        doublereal *x, integer *incx, doublereal *a, integer *lda)
 {
-
-
     /* System generated locals */
     integer a_dim1, a_offset, i__1, i__2;
 
     /* Local variables */
-    static integer info;
-    static doublereal temp;
-    static integer i, j;
+    integer i__, j, ix, jx, kx, info;
+    doublereal temp;
     extern logical lsame_(char *, char *);
-    static integer ix, jx, kx;
     extern /* Subroutine */ int xerbla_(char *, integer *);
 
-
-/*  Purpose   
-    =======   
-
-    DSYR   performs the symmetric rank 1 operation   
-
-       A := alpha*x*x' + A,   
-
-    where alpha is a real scalar, x is an n element vector and A is an   
-    n by n symmetric matrix.   
-
-    Parameters   
-    ==========   
-
-    UPLO   - CHARACTER*1.   
-             On entry, UPLO specifies whether the upper or lower   
-             triangular part of the array A is to be referenced as   
-             follows:   
-
-                UPLO = 'U' or 'u'   Only the upper triangular part of A   
-                                    is to be referenced.   
-
-                UPLO = 'L' or 'l'   Only the lower triangular part of A   
-                                    is to be referenced.   
-
-             Unchanged on exit.   
-
-    N      - INTEGER.   
-             On entry, N specifies the order of the matrix A.   
-             N must be at least zero.   
-             Unchanged on exit.   
-
-    ALPHA  - DOUBLE PRECISION.   
-             On entry, ALPHA specifies the scalar alpha.   
-             Unchanged on exit.   
-
-    X      - DOUBLE PRECISION array of dimension at least   
-             ( 1 + ( n - 1 )*abs( INCX ) ).   
-             Before entry, the incremented array X must contain the n   
-             element vector x.   
-             Unchanged on exit.   
-
-    INCX   - INTEGER.   
-             On entry, INCX specifies the increment for the elements of   
-             X. INCX must not be zero.   
-             Unchanged on exit.   
-
-    A      - DOUBLE PRECISION array of DIMENSION ( LDA, n ).   
-             Before entry with  UPLO = 'U' or 'u', the leading n by n   
-             upper triangular part of the array A must contain the upper 
-  
-             triangular part of the symmetric matrix and the strictly   
-             lower triangular part of A is not referenced. On exit, the   
-             upper triangular part of the array A is overwritten by the   
-             upper triangular part of the updated matrix.   
-             Before entry with UPLO = 'L' or 'l', the leading n by n   
-             lower triangular part of the array A must contain the lower 
-  
-             triangular part of the symmetric matrix and the strictly   
-             upper triangular part of A is not referenced. On exit, the   
-             lower triangular part of the array A is overwritten by the   
-             lower triangular part of the updated matrix.   
-
-    LDA    - INTEGER.   
-             On entry, LDA specifies the first dimension of A as declared 
-  
-             in the calling (sub) program. LDA must be at least   
-             max( 1, n ).   
-             Unchanged on exit.   
-
-
-    Level 2 Blas routine.   
-
-    -- Written on 22-October-1986.   
-       Jack Dongarra, Argonne National Lab.   
-       Jeremy Du Croz, Nag Central Office.   
-       Sven Hammarling, Nag Central Office.   
-       Richard Hanson, Sandia National Labs.   
-
-
-
-       Test the input parameters.   
-
-    
-   Parameter adjustments   
-       Function Body */
-#define X(I) x[(I)-1]
-
-#define A(I,J) a[(I)-1 + ((J)-1)* ( *lda)]
-
+/*     .. Scalar Arguments .. */
+/*     .. */
+/*     .. Array Arguments .. */
+/*     .. */
+
+/*  Purpose */
+/*  ======= */
+
+/*  DSYR   performs the symmetric rank 1 operation */
+
+/*     A := alpha*x*x' + A, */
+
+/*  where alpha is a real scalar, x is an n element vector and A is an */
+/*  n by n symmetric matrix. */
+
+/*  Arguments */
+/*  ========== */
+
+/*  UPLO   - CHARACTER*1. */
+/*           On entry, UPLO specifies whether the upper or lower */
+/*           triangular part of the array A is to be referenced as */
+/*           follows: */
+
+/*              UPLO = 'U' or 'u'   Only the upper triangular part of A */
+/*                                  is to be referenced. */
+
+/*              UPLO = 'L' or 'l'   Only the lower triangular part of A */
+/*                                  is to be referenced. */
+
+/*           Unchanged on exit. */
+
+/*  N      - INTEGER. */
+/*           On entry, N specifies the order of the matrix A. */
+/*           N must be at least zero. */
+/*           Unchanged on exit. */
+
+/*  ALPHA  - DOUBLE PRECISION. */
+/*           On entry, ALPHA specifies the scalar alpha. */
+/*           Unchanged on exit. */
+
+/*  X      - DOUBLE PRECISION array of dimension at least */
+/*           ( 1 + ( n - 1 )*abs( INCX ) ). */
+/*           Before entry, the incremented array X must contain the n */
+/*           element vector x. */
+/*           Unchanged on exit. */
+
+/*  INCX   - INTEGER. */
+/*           On entry, INCX specifies the increment for the elements of */
+/*           X. INCX must not be zero. */
+/*           Unchanged on exit. */
+
+/*  A      - DOUBLE PRECISION array of DIMENSION ( LDA, n ). */
+/*           Before entry with  UPLO = 'U' or 'u', the leading n by n */
+/*           upper triangular part of the array A must contain the upper */
+/*           triangular part of the symmetric matrix and the strictly */
+/*           lower triangular part of A is not referenced. On exit, the */
+/*           upper triangular part of the array A is overwritten by the */
+/*           upper triangular part of the updated matrix. */
+/*           Before entry with UPLO = 'L' or 'l', the leading n by n */
+/*           lower triangular part of the array A must contain the lower */
+/*           triangular part of the symmetric matrix and the strictly */
+/*           upper triangular part of A is not referenced. On exit, the */
+/*           lower triangular part of the array A is overwritten by the */
+/*           lower triangular part of the updated matrix. */
+
+/*  LDA    - INTEGER. */
+/*           On entry, LDA specifies the first dimension of A as declared */
+/*           in the calling (sub) program. LDA must be at least */
+/*           max( 1, n ). */
+/*           Unchanged on exit. */
+
+
+/*  Level 2 Blas routine. */
+
+/*  -- Written on 22-October-1986. */
+/*     Jack Dongarra, Argonne National Lab. */
+/*     Jeremy Du Croz, Nag Central Office. */
+/*     Sven Hammarling, Nag Central Office. */
+/*     Richard Hanson, Sandia National Labs. */
+
+
+/*     .. Parameters .. */
+/*     .. */
+/*     .. Local Scalars .. */
+/*     .. */
+/*     .. External Functions .. */
+/*     .. */
+/*     .. External Subroutines .. */
+/*     .. */
+/*     .. Intrinsic Functions .. */
+/*     .. */
+
+/*     Test the input parameters. */
+
+    /* Parameter adjustments */
+    --x;
+    a_dim1 = *lda;
+    a_offset = 1 + a_dim1;
+    a -= a_offset;
+
+    /* Function Body */
     info = 0;
     if (! lsame_(uplo, "U") && ! lsame_(uplo, "L")) {
        info = 1;
        kx = 1;
     }
 
-/*     Start the operations. In this version the elements of A are   
-       accessed sequentially with one pass through the triangular part   
-       of A. */
+/*     Start the operations. In this version the elements of A are */
+/*     accessed sequentially with one pass through the triangular part */
+/*     of A. */
 
     if (lsame_(uplo, "U")) {
 
 
        if (*incx == 1) {
            i__1 = *n;
-           for (j = 1; j <= *n; ++j) {
-               if (X(j) != 0.) {
-                   temp = *alpha * X(j);
+           for (j = 1; j <= i__1; ++j) {
+               if (x[j] != 0.) {
+                   temp = *alpha * x[j];
                    i__2 = j;
-                   for (i = 1; i <= j; ++i) {
-                       A(i,j) += X(i) * temp;
+                   for (i__ = 1; i__ <= i__2; ++i__) {
+                       a[i__ + j * a_dim1] += x[i__] * temp;
 /* L10: */
                    }
                }
        } else {
            jx = kx;
            i__1 = *n;
-           for (j = 1; j <= *n; ++j) {
-               if (X(jx) != 0.) {
-                   temp = *alpha * X(jx);
+           for (j = 1; j <= i__1; ++j) {
+               if (x[jx] != 0.) {
+                   temp = *alpha * x[jx];
                    ix = kx;
                    i__2 = j;
-                   for (i = 1; i <= j; ++i) {
-                       A(i,j) += X(ix) * temp;
+                   for (i__ = 1; i__ <= i__2; ++i__) {
+                       a[i__ + j * a_dim1] += x[ix] * temp;
                        ix += *incx;
 /* L30: */
                    }
 
        if (*incx == 1) {
            i__1 = *n;
-           for (j = 1; j <= *n; ++j) {
-               if (X(j) != 0.) {
-                   temp = *alpha * X(j);
+           for (j = 1; j <= i__1; ++j) {
+               if (x[j] != 0.) {
+                   temp = *alpha * x[j];
                    i__2 = *n;
-                   for (i = j; i <= *n; ++i) {
-                       A(i,j) += X(i) * temp;
+                   for (i__ = j; i__ <= i__2; ++i__) {
+                       a[i__ + j * a_dim1] += x[i__] * temp;
 /* L50: */
                    }
                }
        } else {
            jx = kx;
            i__1 = *n;
-           for (j = 1; j <= *n; ++j) {
-               if (X(jx) != 0.) {
-                   temp = *alpha * X(jx);
+           for (j = 1; j <= i__1; ++j) {
+               if (x[jx] != 0.) {
+                   temp = *alpha * x[jx];
                    ix = jx;
                    i__2 = *n;
-                   for (i = j; i <= *n; ++i) {
-                       A(i,j) += X(ix) * temp;
+                   for (i__ = j; i__ <= i__2; ++i__) {
+                       a[i__ + j * a_dim1] += x[ix] * temp;
                        ix += *incx;
 /* L70: */
                    }
 /*     End of DSYR  . */
 
 } /* dsyr_ */
-
index 65270b7..472d6d7 100644 (file)
@@ -1,5 +1,18 @@
+/* dsyr2.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int dsyr2_(char *uplo, integer *n, doublereal *alpha, 
        doublereal *x, integer *incx, doublereal *y, integer *incy, 
        doublereal *a, integer *lda)
index dde7329..56f4729 100644 (file)
@@ -1,5 +1,18 @@
+/* dsyr2k.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int dsyr2k_(char *uplo, char *trans, integer *n, integer *k, 
        doublereal *alpha, doublereal *a, integer *lda, doublereal *b, 
        integer *ldb, doublereal *beta, doublereal *c__, integer *ldc)
index a240c14..b8cd9b4 100644 (file)
@@ -1,5 +1,18 @@
+/* dsyrk.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int dsyrk_(char *uplo, char *trans, integer *n, integer *k, 
        doublereal *alpha, doublereal *a, integer *lda, doublereal *beta, 
        doublereal *c__, integer *ldc)
index 3009512..31e70ad 100644 (file)
@@ -1,5 +1,18 @@
+/* dsytd2.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -32,7 +45,7 @@ static doublereal c_b14 = -1.;
 );
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 9047e23..fcbf7b3 100644 (file)
+/* dsytf2.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
+/* Table of constant values */
+
+static integer c__1 = 1;
+
 /* Subroutine */ int dsytf2_(char *uplo, integer *n, doublereal *a, integer *
        lda, integer *ipiv, integer *info)
 {
-/*  -- LAPACK routine (version 3.1) --   
-       Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd..   
-       November 2006   
-
-
-    Purpose   
-    =======   
-
-    DSYTF2 computes the factorization of a real symmetric matrix A using   
-    the Bunch-Kaufman diagonal pivoting method:   
-
-       A = U*D*U'  or  A = L*D*L'   
-
-    where U (or L) is a product of permutation and unit upper (lower)   
-    triangular matrices, U' is the transpose of U, and D is symmetric and   
-    block diagonal with 1-by-1 and 2-by-2 diagonal blocks.   
-
-    This is the unblocked version of the algorithm, calling Level 2 BLAS.   
-
-    Arguments   
-    =========   
-
-    UPLO    (input) CHARACTER*1   
-            Specifies whether the upper or lower triangular part of the   
-            symmetric matrix A is stored:   
-            = 'U':  Upper triangular   
-            = 'L':  Lower triangular   
-
-    N       (input) INTEGER   
-            The order of the matrix A.  N >= 0.   
-
-    A       (input/output) DOUBLE PRECISION array, dimension (LDA,N)   
-            On entry, the symmetric matrix A.  If UPLO = 'U', the leading   
-            n-by-n upper triangular part of A contains the upper   
-            triangular part of the matrix A, and the strictly lower   
-            triangular part of A is not referenced.  If UPLO = 'L', the   
-            leading n-by-n lower triangular part of A contains the lower   
-            triangular part of the matrix A, and the strictly upper   
-            triangular part of A is not referenced.   
-
-            On exit, the block diagonal matrix D and the multipliers used   
-            to obtain the factor U or L (see below for further details).   
-
-    LDA     (input) INTEGER   
-            The leading dimension of the array A.  LDA >= max(1,N).   
-
-    IPIV    (output) INTEGER array, dimension (N)   
-            Details of the interchanges and the block structure of D.   
-            If IPIV(k) > 0, then rows and columns k and IPIV(k) were   
-            interchanged and D(k,k) is a 1-by-1 diagonal block.   
-            If UPLO = 'U' and IPIV(k) = IPIV(k-1) < 0, then rows and   
-            columns k-1 and -IPIV(k) were interchanged and D(k-1:k,k-1:k)   
-            is a 2-by-2 diagonal block.  If UPLO = 'L' and IPIV(k) =   
-            IPIV(k+1) < 0, then rows and columns k+1 and -IPIV(k) were   
-            interchanged and D(k:k+1,k:k+1) is a 2-by-2 diagonal block.   
-
-    INFO    (output) INTEGER   
-            = 0: successful exit   
-            < 0: if INFO = -k, the k-th argument had an illegal value   
-            > 0: if INFO = k, D(k,k) is exactly zero.  The factorization   
-                 has been completed, but the block diagonal matrix D is   
-                 exactly singular, and division by zero will occur if it   
-                 is used to solve a system of equations.   
-
-    Further Details   
-    ===============   
-
-    09-29-06 - patch from   
-      Bobby Cheng, MathWorks   
-
-      Replace l.204 and l.372   
-           IF( MAX( ABSAKK, COLMAX ).EQ.ZERO ) THEN   
-      by   
-           IF( (MAX( ABSAKK, COLMAX ).EQ.ZERO) .OR. DISNAN(ABSAKK) ) THEN   
-
-    01-01-96 - Based on modifications by   
-      J. Lewis, Boeing Computer Services Company   
-      A. Petitet, Computer Science Dept., Univ. of Tenn., Knoxville, USA   
-    1-96 - Based on modifications by J. Lewis, Boeing Computer Services   
-           Company   
-
-    If UPLO = 'U', then A = U*D*U', where   
-       U = P(n)*U(n)* ... *P(k)U(k)* ...,   
-    i.e., U is a product of terms P(k)*U(k), where k decreases from n to   
-    1 in steps of 1 or 2, and D is a block diagonal matrix with 1-by-1   
-    and 2-by-2 diagonal blocks D(k).  P(k) is a permutation matrix as   
-    defined by IPIV(k), and U(k) is a unit upper triangular matrix, such   
-    that if the diagonal block D(k) is of order s (s = 1 or 2), then   
-
-               (   I    v    0   )   k-s   
-       U(k) =  (   0    I    0   )   s   
-               (   0    0    I   )   n-k   
-                  k-s   s   n-k   
-
-    If s = 1, D(k) overwrites A(k,k), and v overwrites A(1:k-1,k).   
-    If s = 2, the upper triangle of D(k) overwrites A(k-1,k-1), A(k-1,k),   
-    and A(k,k), and v overwrites A(1:k-2,k-1:k).   
-
-    If UPLO = 'L', then A = L*D*L', where   
-       L = P(1)*L(1)* ... *P(k)*L(k)* ...,   
-    i.e., L is a product of terms P(k)*L(k), where k increases from 1 to   
-    n in steps of 1 or 2, and D is a block diagonal matrix with 1-by-1   
-    and 2-by-2 diagonal blocks D(k).  P(k) is a permutation matrix as   
-    defined by IPIV(k), and L(k) is a unit lower triangular matrix, such   
-    that if the diagonal block D(k) is of order s (s = 1 or 2), then   
-
-               (   I    0     0   )  k-1   
-       L(k) =  (   0    I     0   )  s   
-               (   0    v     I   )  n-k-s+1   
-                  k-1   s  n-k-s+1   
-
-    If s = 1, D(k) overwrites A(k,k), and v overwrites A(k+1:n,k).   
-    If s = 2, the lower triangle of D(k) overwrites A(k,k), A(k+1,k),   
-    and A(k+1,k+1), and v overwrites A(k+2:n,k:k+1).   
-
-    =====================================================================   
-
-
-       Test the input parameters.   
-
-       Parameter adjustments */
-    /* Table of constant values */
-    static integer c__1 = 1;
-    
     /* System generated locals */
     integer a_dim1, a_offset, i__1, i__2;
     doublereal d__1, d__2, d__3;
+
     /* Builtin functions */
     double sqrt(doublereal);
+
     /* Local variables */
-    static integer i__, j, k;
-    static doublereal t, r1, d11, d12, d21, d22;
-    static integer kk, kp;
-    static doublereal wk, wkm1, wkp1;
-    static integer imax, jmax;
+    integer i__, j, k;
+    doublereal t, r1, d11, d12, d21, d22;
+    integer kk, kp;
+    doublereal wk, wkm1, wkp1;
+    integer imax, jmax;
     extern /* Subroutine */ int dsyr_(char *, integer *, doublereal *, 
            doublereal *, integer *, doublereal *, integer *);
-    static doublereal alpha;
+    doublereal alpha;
     extern /* Subroutine */ int dscal_(integer *, doublereal *, doublereal *, 
            integer *);
     extern logical lsame_(char *, char *);
     extern /* Subroutine */ int dswap_(integer *, doublereal *, integer *, 
            doublereal *, integer *);
-    static integer kstep;
-    static logical upper;
-    static doublereal absakk;
+    integer kstep;
+    logical upper;
+    doublereal absakk;
     extern integer idamax_(integer *, doublereal *, integer *);
     extern logical disnan_(doublereal *);
     extern /* Subroutine */ int xerbla_(char *, integer *);
-    static doublereal colmax, rowmax;
-
-
+    doublereal colmax, rowmax;
+
+
+/*  -- LAPACK routine (version 3.2) -- */
+/*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
+/*     November 2006 */
+
+/*     .. Scalar Arguments .. */
+/*     .. */
+/*     .. Array Arguments .. */
+/*     .. */
+
+/*  Purpose */
+/*  ======= */
+
+/*  DSYTF2 computes the factorization of a real symmetric matrix A using */
+/*  the Bunch-Kaufman diagonal pivoting method: */
+
+/*     A = U*D*U'  or  A = L*D*L' */
+
+/*  where U (or L) is a product of permutation and unit upper (lower) */
+/*  triangular matrices, U' is the transpose of U, and D is symmetric and */
+/*  block diagonal with 1-by-1 and 2-by-2 diagonal blocks. */
+
+/*  This is the unblocked version of the algorithm, calling Level 2 BLAS. */
+
+/*  Arguments */
+/*  ========= */
+
+/*  UPLO    (input) CHARACTER*1 */
+/*          Specifies whether the upper or lower triangular part of the */
+/*          symmetric matrix A is stored: */
+/*          = 'U':  Upper triangular */
+/*          = 'L':  Lower triangular */
+
+/*  N       (input) INTEGER */
+/*          The order of the matrix A.  N >= 0. */
+
+/*  A       (input/output) DOUBLE PRECISION array, dimension (LDA,N) */
+/*          On entry, the symmetric matrix A.  If UPLO = 'U', the leading */
+/*          n-by-n upper triangular part of A contains the upper */
+/*          triangular part of the matrix A, and the strictly lower */
+/*          triangular part of A is not referenced.  If UPLO = 'L', the */
+/*          leading n-by-n lower triangular part of A contains the lower */
+/*          triangular part of the matrix A, and the strictly upper */
+/*          triangular part of A is not referenced. */
+
+/*          On exit, the block diagonal matrix D and the multipliers used */
+/*          to obtain the factor U or L (see below for further details). */
+
+/*  LDA     (input) INTEGER */
+/*          The leading dimension of the array A.  LDA >= max(1,N). */
+
+/*  IPIV    (output) INTEGER array, dimension (N) */
+/*          Details of the interchanges and the block structure of D. */
+/*          If IPIV(k) > 0, then rows and columns k and IPIV(k) were */
+/*          interchanged and D(k,k) is a 1-by-1 diagonal block. */
+/*          If UPLO = 'U' and IPIV(k) = IPIV(k-1) < 0, then rows and */
+/*          columns k-1 and -IPIV(k) were interchanged and D(k-1:k,k-1:k) */
+/*          is a 2-by-2 diagonal block.  If UPLO = 'L' and IPIV(k) = */
+/*          IPIV(k+1) < 0, then rows and columns k+1 and -IPIV(k) were */
+/*          interchanged and D(k:k+1,k:k+1) is a 2-by-2 diagonal block. */
+
+/*  INFO    (output) INTEGER */
+/*          = 0: successful exit */
+/*          < 0: if INFO = -k, the k-th argument had an illegal value */
+/*          > 0: if INFO = k, D(k,k) is exactly zero.  The factorization */
+/*               has been completed, but the block diagonal matrix D is */
+/*               exactly singular, and division by zero will occur if it */
+/*               is used to solve a system of equations. */
+
+/*  Further Details */
+/*  =============== */
+
+/*  09-29-06 - patch from */
+/*    Bobby Cheng, MathWorks */
+
+/*    Replace l.204 and l.372 */
+/*         IF( MAX( ABSAKK, COLMAX ).EQ.ZERO ) THEN */
+/*    by */
+/*         IF( (MAX( ABSAKK, COLMAX ).EQ.ZERO) .OR. DISNAN(ABSAKK) ) THEN */
+
+/*  01-01-96 - Based on modifications by */
+/*    J. Lewis, Boeing Computer Services Company */
+/*    A. Petitet, Computer Science Dept., Univ. of Tenn., Knoxville, USA */
+/*  1-96 - Based on modifications by J. Lewis, Boeing Computer Services */
+/*         Company */
+
+/*  If UPLO = 'U', then A = U*D*U', where */
+/*     U = P(n)*U(n)* ... *P(k)U(k)* ..., */
+/*  i.e., U is a product of terms P(k)*U(k), where k decreases from n to */
+/*  1 in steps of 1 or 2, and D is a block diagonal matrix with 1-by-1 */
+/*  and 2-by-2 diagonal blocks D(k).  P(k) is a permutation matrix as */
+/*  defined by IPIV(k), and U(k) is a unit upper triangular matrix, such */
+/*  that if the diagonal block D(k) is of order s (s = 1 or 2), then */
+
+/*             (   I    v    0   )   k-s */
+/*     U(k) =  (   0    I    0   )   s */
+/*             (   0    0    I   )   n-k */
+/*                k-s   s   n-k */
+
+/*  If s = 1, D(k) overwrites A(k,k), and v overwrites A(1:k-1,k). */
+/*  If s = 2, the upper triangle of D(k) overwrites A(k-1,k-1), A(k-1,k), */
+/*  and A(k,k), and v overwrites A(1:k-2,k-1:k). */
+
+/*  If UPLO = 'L', then A = L*D*L', where */
+/*     L = P(1)*L(1)* ... *P(k)*L(k)* ..., */
+/*  i.e., L is a product of terms P(k)*L(k), where k increases from 1 to */
+/*  n in steps of 1 or 2, and D is a block diagonal matrix with 1-by-1 */
+/*  and 2-by-2 diagonal blocks D(k).  P(k) is a permutation matrix as */
+/*  defined by IPIV(k), and L(k) is a unit lower triangular matrix, such */
+/*  that if the diagonal block D(k) is of order s (s = 1 or 2), then */
+
+/*             (   I    0     0   )  k-1 */
+/*     L(k) =  (   0    I     0   )  s */
+/*             (   0    v     I   )  n-k-s+1 */
+/*                k-1   s  n-k-s+1 */
+
+/*  If s = 1, D(k) overwrites A(k,k), and v overwrites A(k+1:n,k). */
+/*  If s = 2, the lower triangle of D(k) overwrites A(k,k), A(k+1,k), */
+/*  and A(k+1,k+1), and v overwrites A(k+2:n,k:k+1). */
+
+/*  ===================================================================== */
+
+/*     .. Parameters .. */
+/*     .. */
+/*     .. Local Scalars .. */
+/*     .. */
+/*     .. External Functions .. */
+/*     .. */
+/*     .. External Subroutines .. */
+/*     .. */
+/*     .. Intrinsic Functions .. */
+/*     .. */
+/*     .. Executable Statements .. */
+
+/*     Test the input parameters. */
+
+    /* Parameter adjustments */
     a_dim1 = *lda;
     a_offset = 1 + a_dim1;
     a -= a_offset;
 
     if (upper) {
 
-/*        Factorize A as U*D*U' using the upper triangle of A   
+/*        Factorize A as U*D*U' using the upper triangle of A */
 
-          K is the main loop index, decreasing from N to 1 in steps of   
-          1 or 2 */
+/*        K is the main loop index, decreasing from N to 1 in steps of */
+/*        1 or 2 */
 
        k = *n;
 L10:
@@ -197,13 +228,13 @@ L10:
        }
        kstep = 1;
 
-/*        Determine rows and columns to be interchanged and whether   
-          a 1-by-1 or 2-by-2 pivot block will be used */
+/*        Determine rows and columns to be interchanged and whether */
+/*        a 1-by-1 or 2-by-2 pivot block will be used */
 
        absakk = (d__1 = a[k + k * a_dim1], abs(d__1));
 
-/*        IMAX is the row-index of the largest off-diagonal element in   
-          column K, and COLMAX is its absolute value */
+/*        IMAX is the row-index of the largest off-diagonal element in */
+/*        column K, and COLMAX is its absolute value */
 
        if (k > 1) {
            i__1 = k - 1;
@@ -229,8 +260,8 @@ L10:
                kp = k;
            } else {
 
-/*              JMAX is the column-index of the largest off-diagonal   
-                element in row IMAX, and ROWMAX is its absolute value */
+/*              JMAX is the column-index of the largest off-diagonal */
+/*              element in row IMAX, and ROWMAX is its absolute value */
 
                i__1 = k - imax;
                jmax = imax + idamax_(&i__1, &a[imax + (imax + 1) * a_dim1], 
@@ -253,14 +284,14 @@ L10:
                } else if ((d__1 = a[imax + imax * a_dim1], abs(d__1)) >= 
                        alpha * rowmax) {
 
-/*                 interchange rows and columns K and IMAX, use 1-by-1   
-                   pivot block */
+/*                 interchange rows and columns K and IMAX, use 1-by-1 */
+/*                 pivot block */
 
                    kp = imax;
                } else {
 
-/*                 interchange rows and columns K-1 and IMAX, use 2-by-2   
-                   pivot block */
+/*                 interchange rows and columns K-1 and IMAX, use 2-by-2 */
+/*                 pivot block */
 
                    kp = imax;
                    kstep = 2;
@@ -270,8 +301,8 @@ L10:
            kk = k - kstep + 1;
            if (kp != kk) {
 
-/*              Interchange rows and columns KK and KP in the leading   
-                submatrix A(1:k,1:k) */
+/*              Interchange rows and columns KK and KP in the leading */
+/*              submatrix A(1:k,1:k) */
 
                i__1 = kp - 1;
                dswap_(&i__1, &a[kk * a_dim1 + 1], &c__1, &a[kp * a_dim1 + 1], 
@@ -293,15 +324,15 @@ L10:
 
            if (kstep == 1) {
 
-/*              1-by-1 pivot block D(k): column k now holds   
+/*              1-by-1 pivot block D(k): column k now holds */
 
-                W(k) = U(k)*D(k)   
+/*              W(k) = U(k)*D(k) */
 
-                where U(k) is the k-th column of U   
+/*              where U(k) is the k-th column of U */
 
-                Perform a rank-1 update of A(1:k-1,1:k-1) as   
+/*              Perform a rank-1 update of A(1:k-1,1:k-1) as */
 
-                A := A - U(k)*D(k)*U(k)' = A - W(k)*1/D(k)*W(k)' */
+/*              A := A - U(k)*D(k)*U(k)' = A - W(k)*1/D(k)*W(k)' */
 
                r1 = 1. / a[k + k * a_dim1];
                i__1 = k - 1;
@@ -315,17 +346,17 @@ L10:
                dscal_(&i__1, &r1, &a[k * a_dim1 + 1], &c__1);
            } else {
 
-/*              2-by-2 pivot block D(k): columns k and k-1 now hold   
+/*              2-by-2 pivot block D(k): columns k and k-1 now hold */
 
-                ( W(k-1) W(k) ) = ( U(k-1) U(k) )*D(k)   
+/*              ( W(k-1) W(k) ) = ( U(k-1) U(k) )*D(k) */
 
-                where U(k) and U(k-1) are the k-th and (k-1)-th columns   
-                of U   
+/*              where U(k) and U(k-1) are the k-th and (k-1)-th columns */
+/*              of U */
 
-                Perform a rank-2 update of A(1:k-2,1:k-2) as   
+/*              Perform a rank-2 update of A(1:k-2,1:k-2) as */
 
-                A := A - ( U(k-1) U(k) )*D(k)*( U(k-1) U(k) )'   
-                   = A - ( W(k-1) W(k) )*inv(D(k))*( W(k-1) W(k) )' */
+/*              A := A - ( U(k-1) U(k) )*D(k)*( U(k-1) U(k) )' */
+/*                 = A - ( W(k-1) W(k) )*inv(D(k))*( W(k-1) W(k) )' */
 
                if (k > 2) {
 
@@ -372,10 +403,10 @@ L10:
 
     } else {
 
-/*        Factorize A as L*D*L' using the lower triangle of A   
+/*        Factorize A as L*D*L' using the lower triangle of A */
 
-          K is the main loop index, increasing from 1 to N in steps of   
-          1 or 2 */
+/*        K is the main loop index, increasing from 1 to N in steps of */
+/*        1 or 2 */
 
        k = 1;
 L40:
@@ -387,13 +418,13 @@ L40:
        }
        kstep = 1;
 
-/*        Determine rows and columns to be interchanged and whether   
-          a 1-by-1 or 2-by-2 pivot block will be used */
+/*        Determine rows and columns to be interchanged and whether */
+/*        a 1-by-1 or 2-by-2 pivot block will be used */
 
        absakk = (d__1 = a[k + k * a_dim1], abs(d__1));
 
-/*        IMAX is the row-index of the largest off-diagonal element in   
-          column K, and COLMAX is its absolute value */
+/*        IMAX is the row-index of the largest off-diagonal element in */
+/*        column K, and COLMAX is its absolute value */
 
        if (k < *n) {
            i__1 = *n - k;
@@ -419,8 +450,8 @@ L40:
                kp = k;
            } else {
 
-/*              JMAX is the column-index of the largest off-diagonal   
-                element in row IMAX, and ROWMAX is its absolute value */
+/*              JMAX is the column-index of the largest off-diagonal */
+/*              element in row IMAX, and ROWMAX is its absolute value */
 
                i__1 = imax - k;
                jmax = k - 1 + idamax_(&i__1, &a[imax + k * a_dim1], lda);
@@ -443,14 +474,14 @@ L40:
                } else if ((d__1 = a[imax + imax * a_dim1], abs(d__1)) >= 
                        alpha * rowmax) {
 
-/*                 interchange rows and columns K and IMAX, use 1-by-1   
-                   pivot block */
+/*                 interchange rows and columns K and IMAX, use 1-by-1 */
+/*                 pivot block */
 
                    kp = imax;
                } else {
 
-/*                 interchange rows and columns K+1 and IMAX, use 2-by-2   
-                   pivot block */
+/*                 interchange rows and columns K+1 and IMAX, use 2-by-2 */
+/*                 pivot block */
 
                    kp = imax;
                    kstep = 2;
@@ -460,8 +491,8 @@ L40:
            kk = k + kstep - 1;
            if (kp != kk) {
 
-/*              Interchange rows and columns KK and KP in the trailing   
-                submatrix A(k:n,k:n) */
+/*              Interchange rows and columns KK and KP in the trailing */
+/*              submatrix A(k:n,k:n) */
 
                if (kp < *n) {
                    i__1 = *n - kp;
@@ -485,17 +516,17 @@ L40:
 
            if (kstep == 1) {
 
-/*              1-by-1 pivot block D(k): column k now holds   
+/*              1-by-1 pivot block D(k): column k now holds */
 
-                W(k) = L(k)*D(k)   
+/*              W(k) = L(k)*D(k) */
 
-                where L(k) is the k-th column of L */
+/*              where L(k) is the k-th column of L */
 
                if (k < *n) {
 
-/*                 Perform a rank-1 update of A(k+1:n,k+1:n) as   
+/*                 Perform a rank-1 update of A(k+1:n,k+1:n) as */
 
-                   A := A - L(k)*D(k)*L(k)' = A - W(k)*(1/D(k))*W(k)' */
+/*                 A := A - L(k)*D(k)*L(k)' = A - W(k)*(1/D(k))*W(k)' */
 
                    d11 = 1. / a[k + k * a_dim1];
                    i__1 = *n - k;
@@ -514,12 +545,12 @@ L40:
 
                if (k < *n - 1) {
 
-/*                 Perform a rank-2 update of A(k+2:n,k+2:n) as   
+/*                 Perform a rank-2 update of A(k+2:n,k+2:n) as */
 
-                   A := A - ( (A(k) A(k+1))*D(k)**(-1) ) * (A(k) A(k+1))'   
+/*                 A := A - ( (A(k) A(k+1))*D(k)**(-1) ) * (A(k) A(k+1))' */
 
-                   where L(k) and L(k+1) are the k-th and (k+1)-th   
-                   columns of L */
+/*                 where L(k) and L(k+1) are the k-th and (k+1)-th */
+/*                 columns of L */
 
                    d21 = a[k + 1 + k * a_dim1];
                    d11 = a[k + 1 + (k + 1) * a_dim1] / d21;
index d9911b8..79c1f7e 100644 (file)
@@ -1,5 +1,18 @@
+/* dsytrd.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -34,7 +47,7 @@ static doublereal c_b23 = 1.;
     logical lquery;
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 1eb7425..9ee6574 100644 (file)
+/* dsytrf.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
+/* Table of constant values */
+
+static integer c__1 = 1;
+static integer c_n1 = -1;
+static integer c__2 = 2;
+
 /* Subroutine */ int dsytrf_(char *uplo, integer *n, doublereal *a, integer *
        lda, integer *ipiv, doublereal *work, integer *lwork, integer *info)
 {
-/*  -- LAPACK routine (version 3.0) --   
-       Univ. of Tennessee, Univ. of California Berkeley, NAG Ltd.,   
-       Courant Institute, Argonne National Lab, and Rice University   
-       June 30, 1999   
-
-
-    Purpose   
-    =======   
-
-    DSYTRF computes the factorization of a real symmetric matrix A using   
-    the Bunch-Kaufman diagonal pivoting method.  The form of the   
-    factorization is   
-
-       A = U*D*U**T  or  A = L*D*L**T   
-
-    where U (or L) is a product of permutation and unit upper (lower)   
-    triangular matrices, and D is symmetric and block diagonal with   
-    1-by-1 and 2-by-2 diagonal blocks.   
-
-    This is the blocked version of the algorithm, calling Level 3 BLAS.   
-
-    Arguments   
-    =========   
-
-    UPLO    (input) CHARACTER*1   
-            = 'U':  Upper triangle of A is stored;   
-            = 'L':  Lower triangle of A is stored.   
-
-    N       (input) INTEGER   
-            The order of the matrix A.  N >= 0.   
-
-    A       (input/output) DOUBLE PRECISION array, dimension (LDA,N)   
-            On entry, the symmetric matrix A.  If UPLO = 'U', the leading   
-            N-by-N upper triangular part of A contains the upper   
-            triangular part of the matrix A, and the strictly lower   
-            triangular part of A is not referenced.  If UPLO = 'L', the   
-            leading N-by-N lower triangular part of A contains the lower   
-            triangular part of the matrix A, and the strictly upper   
-            triangular part of A is not referenced.   
-
-            On exit, the block diagonal matrix D and the multipliers used   
-            to obtain the factor U or L (see below for further details).   
-
-    LDA     (input) INTEGER   
-            The leading dimension of the array A.  LDA >= max(1,N).   
-
-    IPIV    (output) INTEGER array, dimension (N)   
-            Details of the interchanges and the block structure of D.   
-            If IPIV(k) > 0, then rows and columns k and IPIV(k) were   
-            interchanged and D(k,k) is a 1-by-1 diagonal block.   
-            If UPLO = 'U' and IPIV(k) = IPIV(k-1) < 0, then rows and   
-            columns k-1 and -IPIV(k) were interchanged and D(k-1:k,k-1:k)   
-            is a 2-by-2 diagonal block.  If UPLO = 'L' and IPIV(k) =   
-            IPIV(k+1) < 0, then rows and columns k+1 and -IPIV(k) were   
-            interchanged and D(k:k+1,k:k+1) is a 2-by-2 diagonal block.   
-
-    WORK    (workspace/output) DOUBLE PRECISION array, dimension (LWORK)   
-            On exit, if INFO = 0, WORK(1) returns the optimal LWORK.   
-
-    LWORK   (input) INTEGER   
-            The length of WORK.  LWORK >=1.  For best performance   
-            LWORK >= N*NB, where NB is the block size returned by ILAENV.   
-
-            If LWORK = -1, then a workspace query is assumed; the routine   
-            only calculates the optimal size of the WORK array, returns   
-            this value as the first entry of the WORK array, and no error   
-            message related to LWORK is issued by XERBLA.   
-
-    INFO    (output) INTEGER   
-            = 0:  successful exit   
-            < 0:  if INFO = -i, the i-th argument had an illegal value   
-            > 0:  if INFO = i, D(i,i) is exactly zero.  The factorization   
-                  has been completed, but the block diagonal matrix D is   
-                  exactly singular, and division by zero will occur if it   
-                  is used to solve a system of equations.   
-
-    Further Details   
-    ===============   
-
-    If UPLO = 'U', then A = U*D*U', where   
-       U = P(n)*U(n)* ... *P(k)U(k)* ...,   
-    i.e., U is a product of terms P(k)*U(k), where k decreases from n to   
-    1 in steps of 1 or 2, and D is a block diagonal matrix with 1-by-1   
-    and 2-by-2 diagonal blocks D(k).  P(k) is a permutation matrix as   
-    defined by IPIV(k), and U(k) is a unit upper triangular matrix, such   
-    that if the diagonal block D(k) is of order s (s = 1 or 2), then   
-
-               (   I    v    0   )   k-s   
-       U(k) =  (   0    I    0   )   s   
-               (   0    0    I   )   n-k   
-                  k-s   s   n-k   
-
-    If s = 1, D(k) overwrites A(k,k), and v overwrites A(1:k-1,k).   
-    If s = 2, the upper triangle of D(k) overwrites A(k-1,k-1), A(k-1,k),   
-    and A(k,k), and v overwrites A(1:k-2,k-1:k).   
-
-    If UPLO = 'L', then A = L*D*L', where   
-       L = P(1)*L(1)* ... *P(k)*L(k)* ...,   
-    i.e., L is a product of terms P(k)*L(k), where k increases from 1 to   
-    n in steps of 1 or 2, and D is a block diagonal matrix with 1-by-1   
-    and 2-by-2 diagonal blocks D(k).  P(k) is a permutation matrix as   
-    defined by IPIV(k), and L(k) is a unit lower triangular matrix, such   
-    that if the diagonal block D(k) is of order s (s = 1 or 2), then   
-
-               (   I    0     0   )  k-1   
-       L(k) =  (   0    I     0   )  s   
-               (   0    v     I   )  n-k-s+1   
-                  k-1   s  n-k-s+1   
-
-    If s = 1, D(k) overwrites A(k,k), and v overwrites A(k+1:n,k).   
-    If s = 2, the lower triangle of D(k) overwrites A(k,k), A(k+1,k),   
-    and A(k+1,k+1), and v overwrites A(k+2:n,k:k+1).   
-
-    =====================================================================   
-
-
-       Test the input parameters.   
-
-       Parameter adjustments */
-    /* Table of constant values */
-    static integer c__1 = 1;
-    static integer c_n1 = -1;
-    static integer c__2 = 2;
-    
     /* System generated locals */
     integer a_dim1, a_offset, i__1, i__2;
+
     /* Local variables */
-    static integer j, k;
+    integer j, k, kb, nb, iws;
     extern logical lsame_(char *, char *);
-    static integer nbmin, iinfo;
-    static logical upper;
+    integer nbmin, iinfo;
+    logical upper;
     extern /* Subroutine */ int dsytf2_(char *, integer *, doublereal *, 
-           integer *, integer *, integer *);
-    static integer kb, nb;
-    extern /* Subroutine */ int xerbla_(char *, integer *);
+           integer *, integer *, integer *), xerbla_(char *, integer 
+           *);
     extern integer ilaenv_(integer *, char *, char *, integer *, integer *, 
-           integer *, integer *, ftnlen, ftnlen);
+           integer *, integer *);
     extern /* Subroutine */ int dlasyf_(char *, integer *, integer *, integer 
            *, doublereal *, integer *, integer *, doublereal *, integer *, 
            integer *);
-    static integer ldwork, lwkopt;
-    static logical lquery;
-    static integer iws;
-#define a_ref(a_1,a_2) a[(a_2)*a_dim1 + a_1]
-
-
+    integer ldwork, lwkopt;
+    logical lquery;
+
+
+/*  -- LAPACK routine (version 3.2) -- */
+/*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
+/*     November 2006 */
+
+/*     .. Scalar Arguments .. */
+/*     .. */
+/*     .. Array Arguments .. */
+/*     .. */
+
+/*  Purpose */
+/*  ======= */
+
+/*  DSYTRF computes the factorization of a real symmetric matrix A using */
+/*  the Bunch-Kaufman diagonal pivoting method.  The form of the */
+/*  factorization is */
+
+/*     A = U*D*U**T  or  A = L*D*L**T */
+
+/*  where U (or L) is a product of permutation and unit upper (lower) */
+/*  triangular matrices, and D is symmetric and block diagonal with */
+/*  1-by-1 and 2-by-2 diagonal blocks. */
+
+/*  This is the blocked version of the algorithm, calling Level 3 BLAS. */
+
+/*  Arguments */
+/*  ========= */
+
+/*  UPLO    (input) CHARACTER*1 */
+/*          = 'U':  Upper triangle of A is stored; */
+/*          = 'L':  Lower triangle of A is stored. */
+
+/*  N       (input) INTEGER */
+/*          The order of the matrix A.  N >= 0. */
+
+/*  A       (input/output) DOUBLE PRECISION array, dimension (LDA,N) */
+/*          On entry, the symmetric matrix A.  If UPLO = 'U', the leading */
+/*          N-by-N upper triangular part of A contains the upper */
+/*          triangular part of the matrix A, and the strictly lower */
+/*          triangular part of A is not referenced.  If UPLO = 'L', the */
+/*          leading N-by-N lower triangular part of A contains the lower */
+/*          triangular part of the matrix A, and the strictly upper */
+/*          triangular part of A is not referenced. */
+
+/*          On exit, the block diagonal matrix D and the multipliers used */
+/*          to obtain the factor U or L (see below for further details). */
+
+/*  LDA     (input) INTEGER */
+/*          The leading dimension of the array A.  LDA >= max(1,N). */
+
+/*  IPIV    (output) INTEGER array, dimension (N) */
+/*          Details of the interchanges and the block structure of D. */
+/*          If IPIV(k) > 0, then rows and columns k and IPIV(k) were */
+/*          interchanged and D(k,k) is a 1-by-1 diagonal block. */
+/*          If UPLO = 'U' and IPIV(k) = IPIV(k-1) < 0, then rows and */
+/*          columns k-1 and -IPIV(k) were interchanged and D(k-1:k,k-1:k) */
+/*          is a 2-by-2 diagonal block.  If UPLO = 'L' and IPIV(k) = */
+/*          IPIV(k+1) < 0, then rows and columns k+1 and -IPIV(k) were */
+/*          interchanged and D(k:k+1,k:k+1) is a 2-by-2 diagonal block. */
+
+/*  WORK    (workspace/output) DOUBLE PRECISION array, dimension (MAX(1,LWORK)) */
+/*          On exit, if INFO = 0, WORK(1) returns the optimal LWORK. */
+
+/*  LWORK   (input) INTEGER */
+/*          The length of WORK.  LWORK >=1.  For best performance */
+/*          LWORK >= N*NB, where NB is the block size returned by ILAENV. */
+
+/*          If LWORK = -1, then a workspace query is assumed; the routine */
+/*          only calculates the optimal size of the WORK array, returns */
+/*          this value as the first entry of the WORK array, and no error */
+/*          message related to LWORK is issued by XERBLA. */
+
+/*  INFO    (output) INTEGER */
+/*          = 0:  successful exit */
+/*          < 0:  if INFO = -i, the i-th argument had an illegal value */
+/*          > 0:  if INFO = i, D(i,i) is exactly zero.  The factorization */
+/*                has been completed, but the block diagonal matrix D is */
+/*                exactly singular, and division by zero will occur if it */
+/*                is used to solve a system of equations. */
+
+/*  Further Details */
+/*  =============== */
+
+/*  If UPLO = 'U', then A = U*D*U', where */
+/*     U = P(n)*U(n)* ... *P(k)U(k)* ..., */
+/*  i.e., U is a product of terms P(k)*U(k), where k decreases from n to */
+/*  1 in steps of 1 or 2, and D is a block diagonal matrix with 1-by-1 */
+/*  and 2-by-2 diagonal blocks D(k).  P(k) is a permutation matrix as */
+/*  defined by IPIV(k), and U(k) is a unit upper triangular matrix, such */
+/*  that if the diagonal block D(k) is of order s (s = 1 or 2), then */
+
+/*             (   I    v    0   )   k-s */
+/*     U(k) =  (   0    I    0   )   s */
+/*             (   0    0    I   )   n-k */
+/*                k-s   s   n-k */
+
+/*  If s = 1, D(k) overwrites A(k,k), and v overwrites A(1:k-1,k). */
+/*  If s = 2, the upper triangle of D(k) overwrites A(k-1,k-1), A(k-1,k), */
+/*  and A(k,k), and v overwrites A(1:k-2,k-1:k). */
+
+/*  If UPLO = 'L', then A = L*D*L', where */
+/*     L = P(1)*L(1)* ... *P(k)*L(k)* ..., */
+/*  i.e., L is a product of terms P(k)*L(k), where k increases from 1 to */
+/*  n in steps of 1 or 2, and D is a block diagonal matrix with 1-by-1 */
+/*  and 2-by-2 diagonal blocks D(k).  P(k) is a permutation matrix as */
+/*  defined by IPIV(k), and L(k) is a unit lower triangular matrix, such */
+/*  that if the diagonal block D(k) is of order s (s = 1 or 2), then */
+
+/*             (   I    0     0   )  k-1 */
+/*     L(k) =  (   0    I     0   )  s */
+/*             (   0    v     I   )  n-k-s+1 */
+/*                k-1   s  n-k-s+1 */
+
+/*  If s = 1, D(k) overwrites A(k,k), and v overwrites A(k+1:n,k). */
+/*  If s = 2, the lower triangle of D(k) overwrites A(k,k), A(k+1,k), */
+/*  and A(k+1,k+1), and v overwrites A(k+2:n,k:k+1). */
+
+/*  ===================================================================== */
+
+/*     .. Local Scalars .. */
+/*     .. */
+/*     .. External Functions .. */
+/*     .. */
+/*     .. External Subroutines .. */
+/*     .. */
+/*     .. Intrinsic Functions .. */
+/*     .. */
+/*     .. Executable Statements .. */
+
+/*     Test the input parameters. */
+
+    /* Parameter adjustments */
     a_dim1 = *lda;
-    a_offset = 1 + a_dim1 * 1;
+    a_offset = 1 + a_dim1;
     a -= a_offset;
     --ipiv;
     --work;
 
 /*        Determine the block size */
 
-       nb = ilaenv_(&c__1, "DSYTRF", uplo, n, &c_n1, &c_n1, &c_n1, (ftnlen)6,
-                (ftnlen)1);
+       nb = ilaenv_(&c__1, "DSYTRF", uplo, n, &c_n1, &c_n1, &c_n1);
        lwkopt = *n * nb;
        work[1] = (doublereal) lwkopt;
     }
            nb = max(i__1,1);
 /* Computing MAX */
            i__1 = 2, i__2 = ilaenv_(&c__2, "DSYTRF", uplo, n, &c_n1, &c_n1, &
-                   c_n1, (ftnlen)6, (ftnlen)1);
+                   c_n1);
            nbmin = max(i__1,i__2);
        }
     } else {
 
     if (upper) {
 
-/*        Factorize A as U*D*U' using the upper triangle of A   
+/*        Factorize A as U*D*U' using the upper triangle of A */
 
-          K is the main loop index, decreasing from N to 1 in steps of   
-          KB, where KB is the number of columns factorized by DLASYF;   
-          KB is either NB or NB-1, or K for the last block */
+/*        K is the main loop index, decreasing from N to 1 in steps of */
+/*        KB, where KB is the number of columns factorized by DLASYF; */
+/*        KB is either NB or NB-1, or K for the last block */
 
        k = *n;
 L10:
@@ -226,10 +249,10 @@ L10:
 
        if (k > nb) {
 
-/*           Factorize columns k-kb+1:k of A and use blocked code to   
-             update columns 1:k-kb */
+/*           Factorize columns k-kb+1:k of A and use blocked code to */
+/*           update columns 1:k-kb */
 
-           dlasyf_(uplo, &k, &nb, &kb, &a[a_offset], lda, &ipiv[1], &work[1],
+           dlasyf_(uplo, &k, &nb, &kb, &a[a_offset], lda, &ipiv[1], &work[1], 
                     &ldwork, &iinfo);
        } else {
 
@@ -252,11 +275,11 @@ L10:
 
     } else {
 
-/*        Factorize A as L*D*L' using the lower triangle of A   
+/*        Factorize A as L*D*L' using the lower triangle of A */
 
-          K is the main loop index, increasing from 1 to N in steps of   
-          KB, where KB is the number of columns factorized by DLASYF;   
-          KB is either NB or NB-1, or N-K+1 for the last block */
+/*        K is the main loop index, increasing from 1 to N in steps of */
+/*        KB, where KB is the number of columns factorized by DLASYF; */
+/*        KB is either NB or NB-1, or N-K+1 for the last block */
 
        k = 1;
 L20:
@@ -269,18 +292,18 @@ L20:
 
        if (k <= *n - nb) {
 
-/*           Factorize columns k:k+kb-1 of A and use blocked code to   
-             update columns k+kb:n */
+/*           Factorize columns k:k+kb-1 of A and use blocked code to */
+/*           update columns k+kb:n */
 
            i__1 = *n - k + 1;
-           dlasyf_(uplo, &i__1, &nb, &kb, &a_ref(k, k), lda, &ipiv[k], &work[
-                   1], &ldwork, &iinfo);
+           dlasyf_(uplo, &i__1, &nb, &kb, &a[k + k * a_dim1], lda, &ipiv[k], 
+                   &work[1], &ldwork, &iinfo);
        } else {
 
 /*           Use unblocked code to factorize columns k:n of A */
 
            i__1 = *n - k + 1;
-           dsytf2_(uplo, &i__1, &a_ref(k, k), lda, &ipiv[k], &iinfo);
+           dsytf2_(uplo, &i__1, &a[k + k * a_dim1], lda, &ipiv[k], &iinfo);
            kb = *n - k + 1;
        }
 
@@ -316,7 +339,3 @@ L40:
 /*     End of DSYTRF */
 
 } /* dsytrf_ */
-
-#undef a_ref
-
-
index 09c988d..cc88033 100644 (file)
+/* dsytri.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
+/* Table of constant values */
+
+static integer c__1 = 1;
+static doublereal c_b11 = -1.;
+static doublereal c_b13 = 0.;
+
 /* Subroutine */ int dsytri_(char *uplo, integer *n, doublereal *a, integer *
        lda, integer *ipiv, doublereal *work, integer *info)
 {
-/*  -- LAPACK routine (version 3.0) --   
-       Univ. of Tennessee, Univ. of California Berkeley, NAG Ltd.,   
-       Courant Institute, Argonne National Lab, and Rice University   
-       March 31, 1993   
+    /* System generated locals */
+    integer a_dim1, a_offset, i__1;
+    doublereal d__1;
 
+    /* Local variables */
+    doublereal d__;
+    integer k;
+    doublereal t, ak;
+    integer kp;
+    doublereal akp1;
+    extern doublereal ddot_(integer *, doublereal *, integer *, doublereal *, 
+           integer *);
+    doublereal temp, akkp1;
+    extern logical lsame_(char *, char *);
+    extern /* Subroutine */ int dcopy_(integer *, doublereal *, integer *, 
+           doublereal *, integer *), dswap_(integer *, doublereal *, integer 
+           *, doublereal *, integer *);
+    integer kstep;
+    logical upper;
+    extern /* Subroutine */ int dsymv_(char *, integer *, doublereal *, 
+           doublereal *, integer *, doublereal *, integer *, doublereal *, 
+           doublereal *, integer *), xerbla_(char *, integer *);
 
-    Purpose   
-    =======   
 
-    DSYTRI computes the inverse of a real symmetric indefinite matrix   
-    A using the factorization A = U*D*U**T or A = L*D*L**T computed by   
-    DSYTRF.   
+/*  -- LAPACK routine (version 3.2) -- */
+/*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
+/*     November 2006 */
 
-    Arguments   
-    =========   
+/*     .. Scalar Arguments .. */
+/*     .. */
+/*     .. Array Arguments .. */
+/*     .. */
 
-    UPLO    (input) CHARACTER*1   
-            Specifies whether the details of the factorization are stored   
-            as an upper or lower triangular matrix.   
-            = 'U':  Upper triangular, form is A = U*D*U**T;   
-            = 'L':  Lower triangular, form is A = L*D*L**T.   
+/*  Purpose */
+/*  ======= */
 
-    N       (input) INTEGER   
-            The order of the matrix A.  N >= 0.   
+/*  DSYTRI computes the inverse of a real symmetric indefinite matrix */
+/*  A using the factorization A = U*D*U**T or A = L*D*L**T computed by */
+/*  DSYTRF. */
 
-    A       (input/output) DOUBLE PRECISION array, dimension (LDA,N)   
-            On entry, the block diagonal matrix D and the multipliers   
-            used to obtain the factor U or L as computed by DSYTRF.   
+/*  Arguments */
+/*  ========= */
 
-            On exit, if INFO = 0, the (symmetric) inverse of the original   
-            matrix.  If UPLO = 'U', the upper triangular part of the   
-            inverse is formed and the part of A below the diagonal is not   
-            referenced; if UPLO = 'L' the lower triangular part of the   
-            inverse is formed and the part of A above the diagonal is   
-            not referenced.   
+/*  UPLO    (input) CHARACTER*1 */
+/*          Specifies whether the details of the factorization are stored */
+/*          as an upper or lower triangular matrix. */
+/*          = 'U':  Upper triangular, form is A = U*D*U**T; */
+/*          = 'L':  Lower triangular, form is A = L*D*L**T. */
 
-    LDA     (input) INTEGER   
-            The leading dimension of the array A.  LDA >= max(1,N).   
+/*  N       (input) INTEGER */
+/*          The order of the matrix A.  N >= 0. */
 
-    IPIV    (input) INTEGER array, dimension (N)   
-            Details of the interchanges and the block structure of D   
-            as determined by DSYTRF.   
+/*  A       (input/output) DOUBLE PRECISION array, dimension (LDA,N) */
+/*          On entry, the block diagonal matrix D and the multipliers */
+/*          used to obtain the factor U or L as computed by DSYTRF. */
 
-    WORK    (workspace) DOUBLE PRECISION array, dimension (N)   
+/*          On exit, if INFO = 0, the (symmetric) inverse of the original */
+/*          matrix.  If UPLO = 'U', the upper triangular part of the */
+/*          inverse is formed and the part of A below the diagonal is not */
+/*          referenced; if UPLO = 'L' the lower triangular part of the */
+/*          inverse is formed and the part of A above the diagonal is */
+/*          not referenced. */
 
-    INFO    (output) INTEGER   
-            = 0: successful exit   
-            < 0: if INFO = -i, the i-th argument had an illegal value   
-            > 0: if INFO = i, D(i,i) = 0; the matrix is singular and its   
-                 inverse could not be computed.   
+/*  LDA     (input) INTEGER */
+/*          The leading dimension of the array A.  LDA >= max(1,N). */
 
-    =====================================================================   
+/*  IPIV    (input) INTEGER array, dimension (N) */
+/*          Details of the interchanges and the block structure of D */
+/*          as determined by DSYTRF. */
 
+/*  WORK    (workspace) DOUBLE PRECISION array, dimension (N) */
 
-       Test the input parameters.   
+/*  INFO    (output) INTEGER */
+/*          = 0: successful exit */
+/*          < 0: if INFO = -i, the i-th argument had an illegal value */
+/*          > 0: if INFO = i, D(i,i) = 0; the matrix is singular and its */
+/*               inverse could not be computed. */
 
-       Parameter adjustments */
-    /* Table of constant values */
-    static integer c__1 = 1;
-    static doublereal c_b11 = -1.;
-    static doublereal c_b13 = 0.;
-    
-    /* System generated locals */
-    integer a_dim1, a_offset, i__1;
-    doublereal d__1;
-    /* Local variables */
-    extern doublereal ddot_(integer *, doublereal *, integer *, doublereal *, 
-           integer *);
-    static doublereal temp, akkp1, d__;
-    static integer k;
-    static doublereal t;
-    extern logical lsame_(char *, char *);
-    extern /* Subroutine */ int dcopy_(integer *, doublereal *, integer *, 
-           doublereal *, integer *), dswap_(integer *, doublereal *, integer 
-           *, doublereal *, integer *);
-    static integer kstep;
-    static logical upper;
-    extern /* Subroutine */ int dsymv_(char *, integer *, doublereal *, 
-           doublereal *, integer *, doublereal *, integer *, doublereal *, 
-           doublereal *, integer *);
-    static doublereal ak;
-    static integer kp;
-    extern /* Subroutine */ int xerbla_(char *, integer *);
-    static doublereal akp1;
-#define a_ref(a_1,a_2) a[(a_2)*a_dim1 + a_1]
+/*  ===================================================================== */
 
+/*     .. Parameters .. */
+/*     .. */
+/*     .. Local Scalars .. */
+/*     .. */
+/*     .. External Functions .. */
+/*     .. */
+/*     .. External Subroutines .. */
+/*     .. */
+/*     .. Intrinsic Functions .. */
+/*     .. */
+/*     .. Executable Statements .. */
 
+/*     Test the input parameters. */
+
+    /* Parameter adjustments */
     a_dim1 = *lda;
-    a_offset = 1 + a_dim1 * 1;
+    a_offset = 1 + a_dim1;
     a -= a_offset;
     --ipiv;
     --work;
 /*        Upper triangular storage: examine D from bottom to top */
 
        for (*info = *n; *info >= 1; --(*info)) {
-           if (ipiv[*info] > 0 && a_ref(*info, *info) == 0.) {
+           if (ipiv[*info] > 0 && a[*info + *info * a_dim1] == 0.) {
                return 0;
            }
 /* L10: */
 
        i__1 = *n;
        for (*info = 1; *info <= i__1; ++(*info)) {
-           if (ipiv[*info] > 0 && a_ref(*info, *info) == 0.) {
+           if (ipiv[*info] > 0 && a[*info + *info * a_dim1] == 0.) {
                return 0;
            }
 /* L20: */
 
     if (upper) {
 
-/*        Compute inv(A) from the factorization A = U*D*U'.   
+/*        Compute inv(A) from the factorization A = U*D*U'. */
 
-          K is the main loop index, increasing from 1 to N in steps of   
-          1 or 2, depending on the size of the diagonal blocks. */
+/*        K is the main loop index, increasing from 1 to N in steps of */
+/*        1 or 2, depending on the size of the diagonal blocks. */
 
        k = 1;
 L30:
@@ -162,62 +189,63 @@ L30:
 
        if (ipiv[k] > 0) {
 
-/*           1 x 1 diagonal block   
+/*           1 x 1 diagonal block */
 
-             Invert the diagonal block. */
+/*           Invert the diagonal block. */
 
-           a_ref(k, k) = 1. / a_ref(k, k);
+           a[k + k * a_dim1] = 1. / a[k + k * a_dim1];
 
 /*           Compute column K of the inverse. */
 
            if (k > 1) {
                i__1 = k - 1;
-               dcopy_(&i__1, &a_ref(1, k), &c__1, &work[1], &c__1);
+               dcopy_(&i__1, &a[k * a_dim1 + 1], &c__1, &work[1], &c__1);
                i__1 = k - 1;
                dsymv_(uplo, &i__1, &c_b11, &a[a_offset], lda, &work[1], &
-                       c__1, &c_b13, &a_ref(1, k), &c__1);
+                       c__1, &c_b13, &a[k * a_dim1 + 1], &c__1);
                i__1 = k - 1;
-               a_ref(k, k) = a_ref(k, k) - ddot_(&i__1, &work[1], &c__1, &
-                       a_ref(1, k), &c__1);
+               a[k + k * a_dim1] -= ddot_(&i__1, &work[1], &c__1, &a[k * 
+                       a_dim1 + 1], &c__1);
            }
            kstep = 1;
        } else {
 
-/*           2 x 2 diagonal block   
+/*           2 x 2 diagonal block */
 
-             Invert the diagonal block. */
+/*           Invert the diagonal block. */
 
-           t = (d__1 = a_ref(k, k + 1), abs(d__1));
-           ak = a_ref(k, k) / t;
-           akp1 = a_ref(k + 1, k + 1) / t;
-           akkp1 = a_ref(k, k + 1) / t;
+           t = (d__1 = a[k + (k + 1) * a_dim1], abs(d__1));
+           ak = a[k + k * a_dim1] / t;
+           akp1 = a[k + 1 + (k + 1) * a_dim1] / t;
+           akkp1 = a[k + (k + 1) * a_dim1] / t;
            d__ = t * (ak * akp1 - 1.);
-           a_ref(k, k) = akp1 / d__;
-           a_ref(k + 1, k + 1) = ak / d__;
-           a_ref(k, k + 1) = -akkp1 / d__;
+           a[k + k * a_dim1] = akp1 / d__;
+           a[k + 1 + (k + 1) * a_dim1] = ak / d__;
+           a[k + (k + 1) * a_dim1] = -akkp1 / d__;
 
 /*           Compute columns K and K+1 of the inverse. */
 
            if (k > 1) {
                i__1 = k - 1;
-               dcopy_(&i__1, &a_ref(1, k), &c__1, &work[1], &c__1);
+               dcopy_(&i__1, &a[k * a_dim1 + 1], &c__1, &work[1], &c__1);
                i__1 = k - 1;
                dsymv_(uplo, &i__1, &c_b11, &a[a_offset], lda, &work[1], &
-                       c__1, &c_b13, &a_ref(1, k), &c__1);
+                       c__1, &c_b13, &a[k * a_dim1 + 1], &c__1);
                i__1 = k - 1;
-               a_ref(k, k) = a_ref(k, k) - ddot_(&i__1, &work[1], &c__1, &
-                       a_ref(1, k), &c__1);
+               a[k + k * a_dim1] -= ddot_(&i__1, &work[1], &c__1, &a[k * 
+                       a_dim1 + 1], &c__1);
                i__1 = k - 1;
-               a_ref(k, k + 1) = a_ref(k, k + 1) - ddot_(&i__1, &a_ref(1, k),
-                        &c__1, &a_ref(1, k + 1), &c__1);
+               a[k + (k + 1) * a_dim1] -= ddot_(&i__1, &a[k * a_dim1 + 1], &
+                       c__1, &a[(k + 1) * a_dim1 + 1], &c__1);
                i__1 = k - 1;
-               dcopy_(&i__1, &a_ref(1, k + 1), &c__1, &work[1], &c__1);
+               dcopy_(&i__1, &a[(k + 1) * a_dim1 + 1], &c__1, &work[1], &
+                       c__1);
                i__1 = k - 1;
                dsymv_(uplo, &i__1, &c_b11, &a[a_offset], lda, &work[1], &
-                       c__1, &c_b13, &a_ref(1, k + 1), &c__1);
+                       c__1, &c_b13, &a[(k + 1) * a_dim1 + 1], &c__1);
                i__1 = k - 1;
-               a_ref(k + 1, k + 1) = a_ref(k + 1, k + 1) - ddot_(&i__1, &
-                       work[1], &c__1, &a_ref(1, k + 1), &c__1);
+               a[k + 1 + (k + 1) * a_dim1] -= ddot_(&i__1, &work[1], &c__1, &
+                       a[(k + 1) * a_dim1 + 1], &c__1);
            }
            kstep = 2;
        }
@@ -225,20 +253,22 @@ L30:
        kp = (i__1 = ipiv[k], abs(i__1));
        if (kp != k) {
 
-/*           Interchange rows and columns K and KP in the leading   
-             submatrix A(1:k+1,1:k+1) */
+/*           Interchange rows and columns K and KP in the leading */
+/*           submatrix A(1:k+1,1:k+1) */
 
            i__1 = kp - 1;
-           dswap_(&i__1, &a_ref(1, k), &c__1, &a_ref(1, kp), &c__1);
+           dswap_(&i__1, &a[k * a_dim1 + 1], &c__1, &a[kp * a_dim1 + 1], &
+                   c__1);
            i__1 = k - kp - 1;
-           dswap_(&i__1, &a_ref(kp + 1, k), &c__1, &a_ref(kp, kp + 1), lda);
-           temp = a_ref(k, k);
-           a_ref(k, k) = a_ref(kp, kp);
-           a_ref(kp, kp) = temp;
+           dswap_(&i__1, &a[kp + 1 + k * a_dim1], &c__1, &a[kp + (kp + 1) * 
+                   a_dim1], lda);
+           temp = a[k + k * a_dim1];
+           a[k + k * a_dim1] = a[kp + kp * a_dim1];
+           a[kp + kp * a_dim1] = temp;
            if (kstep == 2) {
-               temp = a_ref(k, k + 1);
-               a_ref(k, k + 1) = a_ref(kp, k + 1);
-               a_ref(kp, k + 1) = temp;
+               temp = a[k + (k + 1) * a_dim1];
+               a[k + (k + 1) * a_dim1] = a[kp + (k + 1) * a_dim1];
+               a[kp + (k + 1) * a_dim1] = temp;
            }
        }
 
@@ -249,10 +279,10 @@ L40:
        ;
     } else {
 
-/*        Compute inv(A) from the factorization A = L*D*L'.   
+/*        Compute inv(A) from the factorization A = L*D*L'. */
 
-          K is the main loop index, increasing from 1 to N in steps of   
-          1 or 2, depending on the size of the diagonal blocks. */
+/*        K is the main loop index, increasing from 1 to N in steps of */
+/*        1 or 2, depending on the size of the diagonal blocks. */
 
        k = *n;
 L50:
@@ -265,64 +295,66 @@ L50:
 
        if (ipiv[k] > 0) {
 
-/*           1 x 1 diagonal block   
+/*           1 x 1 diagonal block */
 
-             Invert the diagonal block. */
+/*           Invert the diagonal block. */
 
-           a_ref(k, k) = 1. / a_ref(k, k);
+           a[k + k * a_dim1] = 1. / a[k + k * a_dim1];
 
 /*           Compute column K of the inverse. */
 
            if (k < *n) {
                i__1 = *n - k;
-               dcopy_(&i__1, &a_ref(k + 1, k), &c__1, &work[1], &c__1);
+               dcopy_(&i__1, &a[k + 1 + k * a_dim1], &c__1, &work[1], &c__1);
                i__1 = *n - k;
-               dsymv_(uplo, &i__1, &c_b11, &a_ref(k + 1, k + 1), lda, &work[
-                       1], &c__1, &c_b13, &a_ref(k + 1, k), &c__1)
-                       ;
+               dsymv_(uplo, &i__1, &c_b11, &a[k + 1 + (k + 1) * a_dim1], lda, 
+                        &work[1], &c__1, &c_b13, &a[k + 1 + k * a_dim1], &
+                       c__1);
                i__1 = *n - k;
-               a_ref(k, k) = a_ref(k, k) - ddot_(&i__1, &work[1], &c__1, &
-                       a_ref(k + 1, k), &c__1);
+               a[k + k * a_dim1] -= ddot_(&i__1, &work[1], &c__1, &a[k + 1 + 
+                       k * a_dim1], &c__1);
            }
            kstep = 1;
        } else {
 
-/*           2 x 2 diagonal block   
+/*           2 x 2 diagonal block */
 
-             Invert the diagonal block. */
+/*           Invert the diagonal block. */
 
-           t = (d__1 = a_ref(k, k - 1), abs(d__1));
-           ak = a_ref(k - 1, k - 1) / t;
-           akp1 = a_ref(k, k) / t;
-           akkp1 = a_ref(k, k - 1) / t;
+           t = (d__1 = a[k + (k - 1) * a_dim1], abs(d__1));
+           ak = a[k - 1 + (k - 1) * a_dim1] / t;
+           akp1 = a[k + k * a_dim1] / t;
+           akkp1 = a[k + (k - 1) * a_dim1] / t;
            d__ = t * (ak * akp1 - 1.);
-           a_ref(k - 1, k - 1) = akp1 / d__;
-           a_ref(k, k) = ak / d__;
-           a_ref(k, k - 1) = -akkp1 / d__;
+           a[k - 1 + (k - 1) * a_dim1] = akp1 / d__;
+           a[k + k * a_dim1] = ak / d__;
+           a[k + (k - 1) * a_dim1] = -akkp1 / d__;
 
 /*           Compute columns K-1 and K of the inverse. */
 
            if (k < *n) {
                i__1 = *n - k;
-               dcopy_(&i__1, &a_ref(k + 1, k), &c__1, &work[1], &c__1);
+               dcopy_(&i__1, &a[k + 1 + k * a_dim1], &c__1, &work[1], &c__1);
                i__1 = *n - k;
-               dsymv_(uplo, &i__1, &c_b11, &a_ref(k + 1, k + 1), lda, &work[
-                       1], &c__1, &c_b13, &a_ref(k + 1, k), &c__1)
-                       ;
+               dsymv_(uplo, &i__1, &c_b11, &a[k + 1 + (k + 1) * a_dim1], lda, 
+                        &work[1], &c__1, &c_b13, &a[k + 1 + k * a_dim1], &
+                       c__1);
                i__1 = *n - k;
-               a_ref(k, k) = a_ref(k, k) - ddot_(&i__1, &work[1], &c__1, &
-                       a_ref(k + 1, k), &c__1);
+               a[k + k * a_dim1] -= ddot_(&i__1, &work[1], &c__1, &a[k + 1 + 
+                       k * a_dim1], &c__1);
                i__1 = *n - k;
-               a_ref(k, k - 1) = a_ref(k, k - 1) - ddot_(&i__1, &a_ref(k + 1,
-                        k), &c__1, &a_ref(k + 1, k - 1), &c__1);
+               a[k + (k - 1) * a_dim1] -= ddot_(&i__1, &a[k + 1 + k * a_dim1]
+, &c__1, &a[k + 1 + (k - 1) * a_dim1], &c__1);
                i__1 = *n - k;
-               dcopy_(&i__1, &a_ref(k + 1, k - 1), &c__1, &work[1], &c__1);
+               dcopy_(&i__1, &a[k + 1 + (k - 1) * a_dim1], &c__1, &work[1], &
+                       c__1);
                i__1 = *n - k;
-               dsymv_(uplo, &i__1, &c_b11, &a_ref(k + 1, k + 1), lda, &work[
-                       1], &c__1, &c_b13, &a_ref(k + 1, k - 1), &c__1);
+               dsymv_(uplo, &i__1, &c_b11, &a[k + 1 + (k + 1) * a_dim1], lda, 
+                        &work[1], &c__1, &c_b13, &a[k + 1 + (k - 1) * a_dim1]
+, &c__1);
                i__1 = *n - k;
-               a_ref(k - 1, k - 1) = a_ref(k - 1, k - 1) - ddot_(&i__1, &
-                       work[1], &c__1, &a_ref(k + 1, k - 1), &c__1);
+               a[k - 1 + (k - 1) * a_dim1] -= ddot_(&i__1, &work[1], &c__1, &
+                       a[k + 1 + (k - 1) * a_dim1], &c__1);
            }
            kstep = 2;
        }
@@ -330,23 +362,24 @@ L50:
        kp = (i__1 = ipiv[k], abs(i__1));
        if (kp != k) {
 
-/*           Interchange rows and columns K and KP in the trailing   
-             submatrix A(k-1:n,k-1:n) */
+/*           Interchange rows and columns K and KP in the trailing */
+/*           submatrix A(k-1:n,k-1:n) */
 
            if (kp < *n) {
                i__1 = *n - kp;
-               dswap_(&i__1, &a_ref(kp + 1, k), &c__1, &a_ref(kp + 1, kp), &
-                       c__1);
+               dswap_(&i__1, &a[kp + 1 + k * a_dim1], &c__1, &a[kp + 1 + kp *
+                        a_dim1], &c__1);
            }
            i__1 = kp - k - 1;
-           dswap_(&i__1, &a_ref(k + 1, k), &c__1, &a_ref(kp, k + 1), lda);
-           temp = a_ref(k, k);
-           a_ref(k, k) = a_ref(kp, kp);
-           a_ref(kp, kp) = temp;
+           dswap_(&i__1, &a[k + 1 + k * a_dim1], &c__1, &a[kp + (k + 1) * 
+                   a_dim1], lda);
+           temp = a[k + k * a_dim1];
+           a[k + k * a_dim1] = a[kp + kp * a_dim1];
+           a[kp + kp * a_dim1] = temp;
            if (kstep == 2) {
-               temp = a_ref(k, k - 1);
-               a_ref(k, k - 1) = a_ref(kp, k - 1);
-               a_ref(kp, k - 1) = temp;
+               temp = a[k + (k - 1) * a_dim1];
+               a[k + (k - 1) * a_dim1] = a[kp + (k - 1) * a_dim1];
+               a[kp + (k - 1) * a_dim1] = temp;
            }
        }
 
@@ -361,7 +394,3 @@ L60:
 /*     End of DSYTRI */
 
 } /* dsytri_ */
-
-#undef a_ref
-
-
index f76570e..04f2d40 100644 (file)
+/* dsytrs.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
+/* Table of constant values */
+
+static doublereal c_b7 = -1.;
+static integer c__1 = 1;
+static doublereal c_b19 = 1.;
+
 /* Subroutine */ int dsytrs_(char *uplo, integer *n, integer *nrhs, 
        doublereal *a, integer *lda, integer *ipiv, doublereal *b, integer *
        ldb, integer *info)
 {
-/*  -- LAPACK routine (version 3.0) --   
-       Univ. of Tennessee, Univ. of California Berkeley, NAG Ltd.,   
-       Courant Institute, Argonne National Lab, and Rice University   
-       March 31, 1993   
+    /* System generated locals */
+    integer a_dim1, a_offset, b_dim1, b_offset, i__1;
+    doublereal d__1;
 
+    /* Local variables */
+    integer j, k;
+    doublereal ak, bk;
+    integer kp;
+    doublereal akm1, bkm1;
+    extern /* Subroutine */ int dger_(integer *, integer *, doublereal *, 
+           doublereal *, integer *, doublereal *, integer *, doublereal *, 
+           integer *);
+    doublereal akm1k;
+    extern /* Subroutine */ int dscal_(integer *, doublereal *, doublereal *, 
+           integer *);
+    extern logical lsame_(char *, char *);
+    doublereal denom;
+    extern /* Subroutine */ int dgemv_(char *, integer *, integer *, 
+           doublereal *, doublereal *, integer *, doublereal *, integer *, 
+           doublereal *, doublereal *, integer *), dswap_(integer *, 
+           doublereal *, integer *, doublereal *, integer *);
+    logical upper;
+    extern /* Subroutine */ int xerbla_(char *, integer *);
 
-    Purpose   
-    =======   
 
-    DSYTRS solves a system of linear equations A*X = B with a real   
-    symmetric matrix A using the factorization A = U*D*U**T or   
-    A = L*D*L**T computed by DSYTRF.   
+/*  -- LAPACK routine (version 3.2) -- */
+/*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
+/*     November 2006 */
 
-    Arguments   
-    =========   
+/*     .. Scalar Arguments .. */
+/*     .. */
+/*     .. Array Arguments .. */
+/*     .. */
 
-    UPLO    (input) CHARACTER*1   
-            Specifies whether the details of the factorization are stored   
-            as an upper or lower triangular matrix.   
-            = 'U':  Upper triangular, form is A = U*D*U**T;   
-            = 'L':  Lower triangular, form is A = L*D*L**T.   
+/*  Purpose */
+/*  ======= */
 
-    N       (input) INTEGER   
-            The order of the matrix A.  N >= 0.   
+/*  DSYTRS solves a system of linear equations A*X = B with a real */
+/*  symmetric matrix A using the factorization A = U*D*U**T or */
+/*  A = L*D*L**T computed by DSYTRF. */
 
-    NRHS    (input) INTEGER   
-            The number of right hand sides, i.e., the number of columns   
-            of the matrix B.  NRHS >= 0.   
+/*  Arguments */
+/*  ========= */
 
-    A       (input) DOUBLE PRECISION array, dimension (LDA,N)   
-            The block diagonal matrix D and the multipliers used to   
-            obtain the factor U or L as computed by DSYTRF.   
+/*  UPLO    (input) CHARACTER*1 */
+/*          Specifies whether the details of the factorization are stored */
+/*          as an upper or lower triangular matrix. */
+/*          = 'U':  Upper triangular, form is A = U*D*U**T; */
+/*          = 'L':  Lower triangular, form is A = L*D*L**T. */
 
-    LDA     (input) INTEGER   
-            The leading dimension of the array A.  LDA >= max(1,N).   
+/*  N       (input) INTEGER */
+/*          The order of the matrix A.  N >= 0. */
 
-    IPIV    (input) INTEGER array, dimension (N)   
-            Details of the interchanges and the block structure of D   
-            as determined by DSYTRF.   
+/*  NRHS    (input) INTEGER */
+/*          The number of right hand sides, i.e., the number of columns */
+/*          of the matrix B.  NRHS >= 0. */
 
-    B       (input/output) DOUBLE PRECISION array, dimension (LDB,NRHS)   
-            On entry, the right hand side matrix B.   
-            On exit, the solution matrix X.   
+/*  A       (input) DOUBLE PRECISION array, dimension (LDA,N) */
+/*          The block diagonal matrix D and the multipliers used to */
+/*          obtain the factor U or L as computed by DSYTRF. */
 
-    LDB     (input) INTEGER   
-            The leading dimension of the array B.  LDB >= max(1,N).   
+/*  LDA     (input) INTEGER */
+/*          The leading dimension of the array A.  LDA >= max(1,N). */
 
-    INFO    (output) INTEGER   
-            = 0:  successful exit   
-            < 0:  if INFO = -i, the i-th argument had an illegal value   
+/*  IPIV    (input) INTEGER array, dimension (N) */
+/*          Details of the interchanges and the block structure of D */
+/*          as determined by DSYTRF. */
 
-    =====================================================================   
+/*  B       (input/output) DOUBLE PRECISION array, dimension (LDB,NRHS) */
+/*          On entry, the right hand side matrix B. */
+/*          On exit, the solution matrix X. */
 
+/*  LDB     (input) INTEGER */
+/*          The leading dimension of the array B.  LDB >= max(1,N). */
 
-       Parameter adjustments */
-    /* Table of constant values */
-    static doublereal c_b7 = -1.;
-    static integer c__1 = 1;
-    static doublereal c_b19 = 1.;
-    
-    /* System generated locals */
-    integer a_dim1, a_offset, b_dim1, b_offset, i__1;
-    doublereal d__1;
-    /* Local variables */
-    extern /* Subroutine */ int dger_(integer *, integer *, doublereal *, 
-           doublereal *, integer *, doublereal *, integer *, doublereal *, 
-           integer *);
-    static doublereal akm1k;
-    static integer j, k;
-    extern /* Subroutine */ int dscal_(integer *, doublereal *, doublereal *, 
-           integer *);
-    extern logical lsame_(char *, char *);
-    static doublereal denom;
-    extern /* Subroutine */ int dgemv_(char *, integer *, integer *, 
-           doublereal *, doublereal *, integer *, doublereal *, integer *, 
-           doublereal *, doublereal *, integer *), dswap_(integer *, 
-           doublereal *, integer *, doublereal *, integer *);
-    static logical upper;
-    static doublereal ak, bk;
-    static integer kp;
-    extern /* Subroutine */ int xerbla_(char *, integer *);
-    static doublereal akm1, bkm1;
-#define a_ref(a_1,a_2) a[(a_2)*a_dim1 + a_1]
-#define b_ref(a_1,a_2) b[(a_2)*b_dim1 + a_1]
+/*  INFO    (output) INTEGER */
+/*          = 0:  successful exit */
+/*          < 0:  if INFO = -i, the i-th argument had an illegal value */
+
+/*  ===================================================================== */
 
+/*     .. Parameters .. */
+/*     .. */
+/*     .. Local Scalars .. */
+/*     .. */
+/*     .. External Functions .. */
+/*     .. */
+/*     .. External Subroutines .. */
+/*     .. */
+/*     .. Intrinsic Functions .. */
+/*     .. */
+/*     .. Executable Statements .. */
 
+    /* Parameter adjustments */
     a_dim1 = *lda;
-    a_offset = 1 + a_dim1 * 1;
+    a_offset = 1 + a_dim1;
     a -= a_offset;
     --ipiv;
     b_dim1 = *ldb;
-    b_offset = 1 + b_dim1 * 1;
+    b_offset = 1 + b_dim1;
     b -= b_offset;
 
     /* Function Body */
 
     if (upper) {
 
-/*        Solve A*X = B, where A = U*D*U'.   
+/*        Solve A*X = B, where A = U*D*U'. */
 
-          First solve U*D*X = B, overwriting B with X.   
+/*        First solve U*D*X = B, overwriting B with X. */
 
-          K is the main loop index, decreasing from N to 1 in steps of   
-          1 or 2, depending on the size of the diagonal blocks. */
+/*        K is the main loop index, decreasing from N to 1 in steps of */
+/*        1 or 2, depending on the size of the diagonal blocks. */
 
        k = *n;
 L10:
@@ -144,60 +171,60 @@ L10:
 
        if (ipiv[k] > 0) {
 
-/*           1 x 1 diagonal block   
+/*           1 x 1 diagonal block */
 
-             Interchange rows K and IPIV(K). */
+/*           Interchange rows K and IPIV(K). */
 
            kp = ipiv[k];
            if (kp != k) {
-               dswap_(nrhs, &b_ref(k, 1), ldb, &b_ref(kp, 1), ldb);
+               dswap_(nrhs, &b[k + b_dim1], ldb, &b[kp + b_dim1], ldb);
            }
 
-/*           Multiply by inv(U(K)), where U(K) is the transformation   
-             stored in column K of A. */
+/*           Multiply by inv(U(K)), where U(K) is the transformation */
+/*           stored in column K of A. */
 
            i__1 = k - 1;
-           dger_(&i__1, nrhs, &c_b7, &a_ref(1, k), &c__1, &b_ref(k, 1), ldb, 
-                   &b_ref(1, 1), ldb);
+           dger_(&i__1, nrhs, &c_b7, &a[k * a_dim1 + 1], &c__1, &b[k + 
+                   b_dim1], ldb, &b[b_dim1 + 1], ldb);
 
 /*           Multiply by the inverse of the diagonal block. */
 
-           d__1 = 1. / a_ref(k, k);
-           dscal_(nrhs, &d__1, &b_ref(k, 1), ldb);
+           d__1 = 1. / a[k + k * a_dim1];
+           dscal_(nrhs, &d__1, &b[k + b_dim1], ldb);
            --k;
        } else {
 
-/*           2 x 2 diagonal block   
+/*           2 x 2 diagonal block */
 
-             Interchange rows K-1 and -IPIV(K). */
+/*           Interchange rows K-1 and -IPIV(K). */
 
            kp = -ipiv[k];
            if (kp != k - 1) {
-               dswap_(nrhs, &b_ref(k - 1, 1), ldb, &b_ref(kp, 1), ldb);
+               dswap_(nrhs, &b[k - 1 + b_dim1], ldb, &b[kp + b_dim1], ldb);
            }
 
-/*           Multiply by inv(U(K)), where U(K) is the transformation   
-             stored in columns K-1 and K of A. */
+/*           Multiply by inv(U(K)), where U(K) is the transformation */
+/*           stored in columns K-1 and K of A. */
 
            i__1 = k - 2;
-           dger_(&i__1, nrhs, &c_b7, &a_ref(1, k), &c__1, &b_ref(k, 1), ldb, 
-                   &b_ref(1, 1), ldb);
+           dger_(&i__1, nrhs, &c_b7, &a[k * a_dim1 + 1], &c__1, &b[k + 
+                   b_dim1], ldb, &b[b_dim1 + 1], ldb);
            i__1 = k - 2;
-           dger_(&i__1, nrhs, &c_b7, &a_ref(1, k - 1), &c__1, &b_ref(k - 1, 
-                   1), ldb, &b_ref(1, 1), ldb);
+           dger_(&i__1, nrhs, &c_b7, &a[(k - 1) * a_dim1 + 1], &c__1, &b[k - 
+                   1 + b_dim1], ldb, &b[b_dim1 + 1], ldb);
 
 /*           Multiply by the inverse of the diagonal block. */
 
-           akm1k = a_ref(k - 1, k);
-           akm1 = a_ref(k - 1, k - 1) / akm1k;
-           ak = a_ref(k, k) / akm1k;
+           akm1k = a[k - 1 + k * a_dim1];
+           akm1 = a[k - 1 + (k - 1) * a_dim1] / akm1k;
+           ak = a[k + k * a_dim1] / akm1k;
            denom = akm1 * ak - 1.;
            i__1 = *nrhs;
            for (j = 1; j <= i__1; ++j) {
-               bkm1 = b_ref(k - 1, j) / akm1k;
-               bk = b_ref(k, j) / akm1k;
-               b_ref(k - 1, j) = (ak * bkm1 - bk) / denom;
-               b_ref(k, j) = (akm1 * bk - bkm1) / denom;
+               bkm1 = b[k - 1 + j * b_dim1] / akm1k;
+               bk = b[k + j * b_dim1] / akm1k;
+               b[k - 1 + j * b_dim1] = (ak * bkm1 - bk) / denom;
+               b[k + j * b_dim1] = (akm1 * bk - bkm1) / denom;
 /* L20: */
            }
            k += -2;
@@ -206,10 +233,10 @@ L10:
        goto L10;
 L30:
 
-/*        Next solve U'*X = B, overwriting B with X.   
+/*        Next solve U'*X = B, overwriting B with X. */
 
-          K is the main loop index, increasing from 1 to N in steps of   
-          1 or 2, depending on the size of the diagonal blocks. */
+/*        K is the main loop index, increasing from 1 to N in steps of */
+/*        1 or 2, depending on the size of the diagonal blocks. */
 
        k = 1;
 L40:
@@ -222,41 +249,42 @@ L40:
 
        if (ipiv[k] > 0) {
 
-/*           1 x 1 diagonal block   
+/*           1 x 1 diagonal block */
 
-             Multiply by inv(U'(K)), where U(K) is the transformation   
-             stored in column K of A. */
+/*           Multiply by inv(U'(K)), where U(K) is the transformation */
+/*           stored in column K of A. */
 
            i__1 = k - 1;
-           dgemv_("Transpose", &i__1, nrhs, &c_b7, &b[b_offset], ldb, &a_ref(
-                   1, k), &c__1, &c_b19, &b_ref(k, 1), ldb);
+           dgemv_("Transpose", &i__1, nrhs, &c_b7, &b[b_offset], ldb, &a[k * 
+                   a_dim1 + 1], &c__1, &c_b19, &b[k + b_dim1], ldb);
 
 /*           Interchange rows K and IPIV(K). */
 
            kp = ipiv[k];
            if (kp != k) {
-               dswap_(nrhs, &b_ref(k, 1), ldb, &b_ref(kp, 1), ldb);
+               dswap_(nrhs, &b[k + b_dim1], ldb, &b[kp + b_dim1], ldb);
            }
            ++k;
        } else {
 
-/*           2 x 2 diagonal block   
+/*           2 x 2 diagonal block */
 
-             Multiply by inv(U'(K+1)), where U(K+1) is the transformation   
-             stored in columns K and K+1 of A. */
+/*           Multiply by inv(U'(K+1)), where U(K+1) is the transformation */
+/*           stored in columns K and K+1 of A. */
 
            i__1 = k - 1;
-           dgemv_("Transpose", &i__1, nrhs, &c_b7, &b[b_offset], ldb, &a_ref(
-                   1, k), &c__1, &c_b19, &b_ref(k, 1), ldb);
+           dgemv_("Transpose", &i__1, nrhs, &c_b7, &b[b_offset], ldb, &a[k * 
+                   a_dim1 + 1], &c__1, &c_b19, &b[k + b_dim1], ldb);
            i__1 = k - 1;
-           dgemv_("Transpose", &i__1, nrhs, &c_b7, &b[b_offset], ldb, &a_ref(
-                   1, k + 1), &c__1, &c_b19, &b_ref(k + 1, 1), ldb);
+           dgemv_("Transpose", &i__1, nrhs, &c_b7, &b[b_offset], ldb, &a[(k 
+                   + 1) * a_dim1 + 1], &c__1, &c_b19, &b[k + 1 + b_dim1], 
+                   ldb);
 
 /*           Interchange rows K and -IPIV(K). */
 
            kp = -ipiv[k];
            if (kp != k) {
-               dswap_(nrhs, &b_ref(k, 1), ldb, &b_ref(kp, 1), ldb);
+               dswap_(nrhs, &b[k + b_dim1], ldb, &b[kp + b_dim1], ldb);
            }
            k += 2;
        }
@@ -267,12 +295,12 @@ L50:
        ;
     } else {
 
-/*        Solve A*X = B, where A = L*D*L'.   
+/*        Solve A*X = B, where A = L*D*L'. */
 
-          First solve L*D*X = B, overwriting B with X.   
+/*        First solve L*D*X = B, overwriting B with X. */
 
-          K is the main loop index, increasing from 1 to N in steps of   
-          1 or 2, depending on the size of the diagonal blocks. */
+/*        K is the main loop index, increasing from 1 to N in steps of */
+/*        1 or 2, depending on the size of the diagonal blocks. */
 
        k = 1;
 L60:
@@ -285,64 +313,64 @@ L60:
 
        if (ipiv[k] > 0) {
 
-/*           1 x 1 diagonal block   
+/*           1 x 1 diagonal block */
 
-             Interchange rows K and IPIV(K). */
+/*           Interchange rows K and IPIV(K). */
 
            kp = ipiv[k];
            if (kp != k) {
-               dswap_(nrhs, &b_ref(k, 1), ldb, &b_ref(kp, 1), ldb);
+               dswap_(nrhs, &b[k + b_dim1], ldb, &b[kp + b_dim1], ldb);
            }
 
-/*           Multiply by inv(L(K)), where L(K) is the transformation   
-             stored in column K of A. */
+/*           Multiply by inv(L(K)), where L(K) is the transformation */
+/*           stored in column K of A. */
 
            if (k < *n) {
                i__1 = *n - k;
-               dger_(&i__1, nrhs, &c_b7, &a_ref(k + 1, k), &c__1, &b_ref(k, 
-                       1), ldb, &b_ref(k + 1, 1), ldb);
+               dger_(&i__1, nrhs, &c_b7, &a[k + 1 + k * a_dim1], &c__1, &b[k 
+                       + b_dim1], ldb, &b[k + 1 + b_dim1], ldb);
            }
 
 /*           Multiply by the inverse of the diagonal block. */
 
-           d__1 = 1. / a_ref(k, k);
-           dscal_(nrhs, &d__1, &b_ref(k, 1), ldb);
+           d__1 = 1. / a[k + k * a_dim1];
+           dscal_(nrhs, &d__1, &b[k + b_dim1], ldb);
            ++k;
        } else {
 
-/*           2 x 2 diagonal block   
+/*           2 x 2 diagonal block */
 
-             Interchange rows K+1 and -IPIV(K). */
+/*           Interchange rows K+1 and -IPIV(K). */
 
            kp = -ipiv[k];
            if (kp != k + 1) {
-               dswap_(nrhs, &b_ref(k + 1, 1), ldb, &b_ref(kp, 1), ldb);
+               dswap_(nrhs, &b[k + 1 + b_dim1], ldb, &b[kp + b_dim1], ldb);
            }
 
-/*           Multiply by inv(L(K)), where L(K) is the transformation   
-             stored in columns K and K+1 of A. */
+/*           Multiply by inv(L(K)), where L(K) is the transformation */
+/*           stored in columns K and K+1 of A. */
 
            if (k < *n - 1) {
                i__1 = *n - k - 1;
-               dger_(&i__1, nrhs, &c_b7, &a_ref(k + 2, k), &c__1, &b_ref(k, 
-                       1), ldb, &b_ref(k + 2, 1), ldb);
+               dger_(&i__1, nrhs, &c_b7, &a[k + 2 + k * a_dim1], &c__1, &b[k 
+                       + b_dim1], ldb, &b[k + 2 + b_dim1], ldb);
                i__1 = *n - k - 1;
-               dger_(&i__1, nrhs, &c_b7, &a_ref(k + 2, k + 1), &c__1, &b_ref(
-                       k + 1, 1), ldb, &b_ref(k + 2, 1), ldb);
+               dger_(&i__1, nrhs, &c_b7, &a[k + 2 + (k + 1) * a_dim1], &c__1, 
+                        &b[k + 1 + b_dim1], ldb, &b[k + 2 + b_dim1], ldb);
            }
 
 /*           Multiply by the inverse of the diagonal block. */
 
-           akm1k = a_ref(k + 1, k);
-           akm1 = a_ref(k, k) / akm1k;
-           ak = a_ref(k + 1, k + 1) / akm1k;
+           akm1k = a[k + 1 + k * a_dim1];
+           akm1 = a[k + k * a_dim1] / akm1k;
+           ak = a[k + 1 + (k + 1) * a_dim1] / akm1k;
            denom = akm1 * ak - 1.;
            i__1 = *nrhs;
            for (j = 1; j <= i__1; ++j) {
-               bkm1 = b_ref(k, j) / akm1k;
-               bk = b_ref(k + 1, j) / akm1k;
-               b_ref(k, j) = (ak * bkm1 - bk) / denom;
-               b_ref(k + 1, j) = (akm1 * bk - bkm1) / denom;
+               bkm1 = b[k + j * b_dim1] / akm1k;
+               bk = b[k + 1 + j * b_dim1] / akm1k;
+               b[k + j * b_dim1] = (ak * bkm1 - bk) / denom;
+               b[k + 1 + j * b_dim1] = (akm1 * bk - bkm1) / denom;
 /* L70: */
            }
            k += 2;
@@ -351,10 +379,10 @@ L60:
        goto L60;
 L80:
 
-/*        Next solve L'*X = B, overwriting B with X.   
+/*        Next solve L'*X = B, overwriting B with X. */
 
-          K is the main loop index, decreasing from N to 1 in steps of   
-          1 or 2, depending on the size of the diagonal blocks. */
+/*        K is the main loop index, decreasing from N to 1 in steps of */
+/*        1 or 2, depending on the size of the diagonal blocks. */
 
        k = *n;
 L90:
@@ -367,46 +395,48 @@ L90:
 
        if (ipiv[k] > 0) {
 
-/*           1 x 1 diagonal block   
+/*           1 x 1 diagonal block */
 
-             Multiply by inv(L'(K)), where L(K) is the transformation   
-             stored in column K of A. */
+/*           Multiply by inv(L'(K)), where L(K) is the transformation */
+/*           stored in column K of A. */
 
            if (k < *n) {
                i__1 = *n - k;
-               dgemv_("Transpose", &i__1, nrhs, &c_b7, &b_ref(k + 1, 1), ldb,
-                        &a_ref(k + 1, k), &c__1, &c_b19, &b_ref(k, 1), ldb);
+               dgemv_("Transpose", &i__1, nrhs, &c_b7, &b[k + 1 + b_dim1], 
+                       ldb, &a[k + 1 + k * a_dim1], &c__1, &c_b19, &b[k + 
+                       b_dim1], ldb);
            }
 
 /*           Interchange rows K and IPIV(K). */
 
            kp = ipiv[k];
            if (kp != k) {
-               dswap_(nrhs, &b_ref(k, 1), ldb, &b_ref(kp, 1), ldb);
+               dswap_(nrhs, &b[k + b_dim1], ldb, &b[kp + b_dim1], ldb);
            }
            --k;
        } else {
 
-/*           2 x 2 diagonal block   
+/*           2 x 2 diagonal block */
 
-             Multiply by inv(L'(K-1)), where L(K-1) is the transformation   
-             stored in columns K-1 and K of A. */
+/*           Multiply by inv(L'(K-1)), where L(K-1) is the transformation */
+/*           stored in columns K-1 and K of A. */
 
            if (k < *n) {
                i__1 = *n - k;
-               dgemv_("Transpose", &i__1, nrhs, &c_b7, &b_ref(k + 1, 1), ldb,
-                        &a_ref(k + 1, k), &c__1, &c_b19, &b_ref(k, 1), ldb);
+               dgemv_("Transpose", &i__1, nrhs, &c_b7, &b[k + 1 + b_dim1], 
+                       ldb, &a[k + 1 + k * a_dim1], &c__1, &c_b19, &b[k + 
+                       b_dim1], ldb);
                i__1 = *n - k;
-               dgemv_("Transpose", &i__1, nrhs, &c_b7, &b_ref(k + 1, 1), ldb,
-                        &a_ref(k + 1, k - 1), &c__1, &c_b19, &b_ref(k - 1, 1)
-                       , ldb);
+               dgemv_("Transpose", &i__1, nrhs, &c_b7, &b[k + 1 + b_dim1], 
+                       ldb, &a[k + 1 + (k - 1) * a_dim1], &c__1, &c_b19, &b[
+                       k - 1 + b_dim1], ldb);
            }
 
 /*           Interchange rows K and -IPIV(K). */
 
            kp = -ipiv[k];
            if (kp != k) {
-               dswap_(nrhs, &b_ref(k, 1), ldb, &b_ref(kp, 1), ldb);
+               dswap_(nrhs, &b[k + b_dim1], ldb, &b[kp + b_dim1], ldb);
            }
            k += -2;
        }
@@ -421,8 +451,3 @@ L100:
 /*     End of DSYTRS */
 
 } /* dsytrs_ */
-
-#undef b_ref
-#undef a_ref
-
-
index 0adddee..d83838a 100644 (file)
@@ -1,5 +1,18 @@
+/* dtrmm.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int dtrmm_(char *side, char *uplo, char *transa, char *diag, 
        integer *m, integer *n, doublereal *alpha, doublereal *a, integer *
        lda, doublereal *b, integer *ldb)
 
 /*     Quick return if possible. */
 
-    if (*n == 0) {
+    if (*m == 0 || *n == 0) {
        return 0;
     }
 
index 1c7aae2..c9a0b12 100644 (file)
@@ -1,5 +1,18 @@
+/* dtrmv.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int dtrmv_(char *uplo, char *trans, char *diag, integer *n, 
        doublereal *a, integer *lda, doublereal *x, integer *incx)
 {
index 3b7c584..ba301fa 100644 (file)
@@ -1,5 +1,18 @@
+/* dtrsm.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int dtrsm_(char *side, char *uplo, char *transa, char *diag, 
        integer *m, integer *n, doublereal *alpha, doublereal *a, integer *
        lda, doublereal *b, integer *ldb)
 
 /*     Quick return if possible. */
 
-    if (*n == 0) {
+    if (*m == 0 || *n == 0) {
        return 0;
     }
 
index 9b99417..3702993 100644 (file)
@@ -1,5 +1,18 @@
+/* dtrti2.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -22,7 +35,7 @@ static integer c__1 = 1;
     logical nounit;
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 4979094..94553cc 100644 (file)
@@ -1,5 +1,18 @@
+/* dtrtri.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -36,7 +49,7 @@ static doublereal c_b22 = -1.;
     logical nounit;
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 6f76301..abf72d6 100644 (file)
@@ -1,5 +1,18 @@
+/* dtrtrs.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static doublereal c_b12 = 1.;
@@ -20,7 +33,7 @@ static doublereal c_b12 = 1.;
     logical nounit;
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index b6449f0..104ad50 100644 (file)
@@ -1,5 +1,18 @@
+/* idamax.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 integer idamax_(integer *n, doublereal *dx, integer *incx)
 {
     /* System generated locals */
index 8e4e040..404ebba 100644 (file)
@@ -1,5 +1,18 @@
+/* ieeeck.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 integer ieeeck_(integer *ispec, real *zero, real *one)
 {
     /* System generated locals */
@@ -9,7 +22,7 @@ integer ieeeck_(integer *ispec, real *zero, real *one)
     real nan1, nan2, nan3, nan4, nan5, nan6, neginf, posinf, negzro, newzro;
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
diff --git a/3rdparty/lapack/iladlc.c b/3rdparty/lapack/iladlc.c
new file mode 100644 (file)
index 0000000..34eee96
--- /dev/null
@@ -0,0 +1,88 @@
+/* iladlc.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
+#include "clapack.h"
+
+
+integer iladlc_(integer *m, integer *n, doublereal *a, integer *lda)
+{
+    /* System generated locals */
+    integer a_dim1, a_offset, ret_val, i__1;
+
+    /* Local variables */
+    integer i__;
+
+
+/*  -- LAPACK auxiliary routine (version 3.2.1)                        -- */
+
+/*  -- April 2009                                                      -- */
+
+/*  -- LAPACK is a software package provided by Univ. of Tennessee,    -- */
+/*  -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..-- */
+
+/*     .. Scalar Arguments .. */
+/*     .. */
+/*     .. Array Arguments .. */
+/*     .. */
+
+/*  Purpose */
+/*  ======= */
+
+/*  ILADLC scans A for its last non-zero column. */
+
+/*  Arguments */
+/*  ========= */
+
+/*  M       (input) INTEGER */
+/*          The number of rows of the matrix A. */
+
+/*  N       (input) INTEGER */
+/*          The number of columns of the matrix A. */
+
+/*  A       (input) DOUBLE PRECISION array, dimension (LDA,N) */
+/*          The m by n matrix A. */
+
+/*  LDA     (input) INTEGER */
+/*          The leading dimension of the array A. LDA >= max(1,M). */
+
+/*  ===================================================================== */
+
+/*     .. Parameters .. */
+/*     .. */
+/*     .. Local Scalars .. */
+/*     .. */
+/*     .. Executable Statements .. */
+
+/*     Quick test for the common case where one corner is non-zero. */
+    /* Parameter adjustments */
+    a_dim1 = *lda;
+    a_offset = 1 + a_dim1;
+    a -= a_offset;
+
+    /* Function Body */
+    if (*n == 0) {
+       ret_val = *n;
+    } else if (a[*n * a_dim1 + 1] != 0. || a[*m + *n * a_dim1] != 0.) {
+       ret_val = *n;
+    } else {
+/*     Now scan each column from the end, returning with the first non-zero. */
+       for (ret_val = *n; ret_val >= 1; --ret_val) {
+           i__1 = *m;
+           for (i__ = 1; i__ <= i__1; ++i__) {
+               if (a[i__ + ret_val * a_dim1] != 0.) {
+                   return ret_val;
+               }
+           }
+       }
+    }
+    return ret_val;
+} /* iladlc_ */
diff --git a/3rdparty/lapack/iladlr.c b/3rdparty/lapack/iladlr.c
new file mode 100644 (file)
index 0000000..1e6e00f
--- /dev/null
@@ -0,0 +1,90 @@
+/* iladlr.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
+#include "clapack.h"
+
+
+integer iladlr_(integer *m, integer *n, doublereal *a, integer *lda)
+{
+    /* System generated locals */
+    integer a_dim1, a_offset, ret_val, i__1;
+
+    /* Local variables */
+    integer i__, j;
+
+
+/*  -- LAPACK auxiliary routine (version 3.2.1)                        -- */
+
+/*  -- April 2009                                                      -- */
+
+/*  -- LAPACK is a software package provided by Univ. of Tennessee,    -- */
+/*  -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..-- */
+
+/*     .. Scalar Arguments .. */
+/*     .. */
+/*     .. Array Arguments .. */
+/*     .. */
+
+/*  Purpose */
+/*  ======= */
+
+/*  ILADLR scans A for its last non-zero row. */
+
+/*  Arguments */
+/*  ========= */
+
+/*  M       (input) INTEGER */
+/*          The number of rows of the matrix A. */
+
+/*  N       (input) INTEGER */
+/*          The number of columns of the matrix A. */
+
+/*  A       (input) DOUBLE PRECISION array, dimension (LDA,N) */
+/*          The m by n matrix A. */
+
+/*  LDA     (input) INTEGER */
+/*          The leading dimension of the array A. LDA >= max(1,M). */
+
+/*  ===================================================================== */
+
+/*     .. Parameters .. */
+/*     .. */
+/*     .. Local Scalars .. */
+/*     .. */
+/*     .. Executable Statements .. */
+
+/*     Quick test for the common case where one corner is non-zero. */
+    /* Parameter adjustments */
+    a_dim1 = *lda;
+    a_offset = 1 + a_dim1;
+    a -= a_offset;
+
+    /* Function Body */
+    if (*m == 0) {
+       ret_val = *m;
+    } else if (a[*m + a_dim1] != 0. || a[*m + *n * a_dim1] != 0.) {
+       ret_val = *m;
+    } else {
+/*     Scan up each column tracking the last zero row seen. */
+       ret_val = 0;
+       i__1 = *n;
+       for (j = 1; j <= i__1; ++j) {
+           for (i__ = *m; i__ >= 1; --i__) {
+               if (a[i__ + j * a_dim1] != 0.) {
+                   break;
+               }
+           }
+           ret_val = max(ret_val,i__);
+       }
+    }
+    return ret_val;
+} /* iladlr_ */
index 1b40d6c..d35a8f7 100644 (file)
@@ -1,12 +1,25 @@
+/* ilaenv.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
+
 #include "string.h"
 
 /* Table of constant values */
 
-static integer c__0 = 0;
+static integer c__1 = 1;
 static real c_b163 = 0.f;
 static real c_b164 = 1.f;
-static integer c__1 = 1;
+static integer c__0 = 0;
 
 integer ilaenv_(integer *ispec, char *name__, char *opts, integer *n1, 
        integer *n2, integer *n3, integer *n4)
@@ -19,24 +32,23 @@ integer ilaenv_(integer *ispec, char *name__, char *opts, integer *n1,
     integer s_cmp(char *, char *, ftnlen, ftnlen);
 
     /* Local variables */
-    static integer i__;
-    static char c1[1], c2[2], c3[3], c4[2];
-    static integer ic, nb, iz, nx;
-    static logical cname;
-    static integer nbmin;
-    static logical sname;
+    integer i__;
+    char c1[1], c2[2], c3[3], c4[2];
+    integer ic, nb, iz, nx;
+    logical cname;
+    integer nbmin;
+    logical sname;
     extern integer ieeeck_(integer *, real *, real *);
-    static char subnam[6];
+    char subnam[6];
     extern integer iparmq_(integer *, char *, char *, integer *, integer *, 
            integer *, integer *);
 
     ftnlen name_len, opts_len;
 
-    name_len = strlen (name__);
-    opts_len = strlen (opts);
+    name_len = (ftnlen)strlen (name__);
+    opts_len = (ftnlen)strlen (opts);
 
-
-/*  -- LAPACK auxiliary routine (version 3.1.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     January 2007 */
 
@@ -173,7 +185,7 @@ L10:
 /*     Convert NAME to upper case if the first character is lower case. */
 
     ret_val = 1;
-    s_copy(subnam, name__, (ftnlen)6, name_len);
+    s_copy(subnam, name__, (ftnlen)1, name_len);
     ic = *(unsigned char *)subnam;
     iz = 'Z';
     if (iz == 90 || iz == 122) {
@@ -230,9 +242,9 @@ L10:
     if (! (cname || sname)) {
        return ret_val;
     }
-    s_copy(c2, subnam + 1, (ftnlen)2, (ftnlen)2);
-    s_copy(c3, subnam + 3, (ftnlen)3, (ftnlen)3);
-    s_copy(c4, c3 + 1, (ftnlen)2, (ftnlen)2);
+    s_copy(c2, subnam + 1, (ftnlen)1, (ftnlen)2);
+    s_copy(c3, subnam + 3, (ftnlen)1, (ftnlen)3);
+    s_copy(c4, c3 + 1, (ftnlen)1, (ftnlen)2);
 
     switch (*ispec) {
        case 1:  goto L50;
@@ -250,111 +262,111 @@ L50:
 
     nb = 1;
 
-    if (s_cmp(c2, "GE", (ftnlen)2, (ftnlen)2) == 0) {
-       if (s_cmp(c3, "TRF", (ftnlen)3, (ftnlen)3) == 0) {
+    if (s_cmp(c2, "GE", (ftnlen)1, (ftnlen)2) == 0) {
+       if (s_cmp(c3, "TRF", (ftnlen)1, (ftnlen)3) == 0) {
            if (sname) {
                nb = 64;
            } else {
                nb = 64;
            }
-       } else if (s_cmp(c3, "QRF", (ftnlen)3, (ftnlen)3) == 0 || s_cmp(c3, 
-               "RQF", (ftnlen)3, (ftnlen)3) == 0 || s_cmp(c3, "LQF", (ftnlen)
-               3, (ftnlen)3) == 0 || s_cmp(c3, "QLF", (ftnlen)3, (ftnlen)3) 
+       } else if (s_cmp(c3, "QRF", (ftnlen)1, (ftnlen)3) == 0 || s_cmp(c3, 
+               "RQF", (ftnlen)1, (ftnlen)3) == 0 || s_cmp(c3, "LQF", (ftnlen)
+               1, (ftnlen)3) == 0 || s_cmp(c3, "QLF", (ftnlen)1, (ftnlen)3) 
                == 0) {
            if (sname) {
                nb = 32;
            } else {
                nb = 32;
            }
-       } else if (s_cmp(c3, "HRD", (ftnlen)3, (ftnlen)3) == 0) {
+       } else if (s_cmp(c3, "HRD", (ftnlen)1, (ftnlen)3) == 0) {
            if (sname) {
                nb = 32;
            } else {
                nb = 32;
            }
-       } else if (s_cmp(c3, "BRD", (ftnlen)3, (ftnlen)3) == 0) {
+       } else if (s_cmp(c3, "BRD", (ftnlen)1, (ftnlen)3) == 0) {
            if (sname) {
                nb = 32;
            } else {
                nb = 32;
            }
-       } else if (s_cmp(c3, "TRI", (ftnlen)3, (ftnlen)3) == 0) {
+       } else if (s_cmp(c3, "TRI", (ftnlen)1, (ftnlen)3) == 0) {
            if (sname) {
                nb = 64;
            } else {
                nb = 64;
            }
        }
-    } else if (s_cmp(c2, "PO", (ftnlen)2, (ftnlen)2) == 0) {
-       if (s_cmp(c3, "TRF", (ftnlen)3, (ftnlen)3) == 0) {
+    } else if (s_cmp(c2, "PO", (ftnlen)1, (ftnlen)2) == 0) {
+       if (s_cmp(c3, "TRF", (ftnlen)1, (ftnlen)3) == 0) {
            if (sname) {
                nb = 64;
            } else {
                nb = 64;
            }
        }
-    } else if (s_cmp(c2, "SY", (ftnlen)2, (ftnlen)2) == 0) {
-       if (s_cmp(c3, "TRF", (ftnlen)3, (ftnlen)3) == 0) {
+    } else if (s_cmp(c2, "SY", (ftnlen)1, (ftnlen)2) == 0) {
+       if (s_cmp(c3, "TRF", (ftnlen)1, (ftnlen)3) == 0) {
            if (sname) {
                nb = 64;
            } else {
                nb = 64;
            }
-       } else if (sname && s_cmp(c3, "TRD", (ftnlen)3, (ftnlen)3) == 0) {
+       } else if (sname && s_cmp(c3, "TRD", (ftnlen)1, (ftnlen)3) == 0) {
            nb = 32;
-       } else if (sname && s_cmp(c3, "GST", (ftnlen)3, (ftnlen)3) == 0) {
+       } else if (sname && s_cmp(c3, "GST", (ftnlen)1, (ftnlen)3) == 0) {
            nb = 64;
        }
-    } else if (cname && s_cmp(c2, "HE", (ftnlen)2, (ftnlen)2) == 0) {
-       if (s_cmp(c3, "TRF", (ftnlen)3, (ftnlen)3) == 0) {
+    } else if (cname && s_cmp(c2, "HE", (ftnlen)1, (ftnlen)2) == 0) {
+       if (s_cmp(c3, "TRF", (ftnlen)1, (ftnlen)3) == 0) {
            nb = 64;
-       } else if (s_cmp(c3, "TRD", (ftnlen)3, (ftnlen)3) == 0) {
+       } else if (s_cmp(c3, "TRD", (ftnlen)1, (ftnlen)3) == 0) {
            nb = 32;
-       } else if (s_cmp(c3, "GST", (ftnlen)3, (ftnlen)3) == 0) {
+       } else if (s_cmp(c3, "GST", (ftnlen)1, (ftnlen)3) == 0) {
            nb = 64;
        }
-    } else if (sname && s_cmp(c2, "OR", (ftnlen)2, (ftnlen)2) == 0) {
+    } else if (sname && s_cmp(c2, "OR", (ftnlen)1, (ftnlen)2) == 0) {
        if (*(unsigned char *)c3 == 'G') {
-           if (s_cmp(c4, "QR", (ftnlen)2, (ftnlen)2) == 0 || s_cmp(c4, "RQ", 
-                   (ftnlen)2, (ftnlen)2) == 0 || s_cmp(c4, "LQ", (ftnlen)2, (
-                   ftnlen)2) == 0 || s_cmp(c4, "QL", (ftnlen)2, (ftnlen)2) ==
-                    0 || s_cmp(c4, "HR", (ftnlen)2, (ftnlen)2) == 0 || s_cmp(
-                   c4, "TR", (ftnlen)2, (ftnlen)2) == 0 || s_cmp(c4, "BR", (
-                   ftnlen)2, (ftnlen)2) == 0) {
+           if (s_cmp(c4, "QR", (ftnlen)1, (ftnlen)2) == 0 || s_cmp(c4, "RQ", 
+                   (ftnlen)1, (ftnlen)2) == 0 || s_cmp(c4, "LQ", (ftnlen)1, (
+                   ftnlen)2) == 0 || s_cmp(c4, "QL", (ftnlen)1, (ftnlen)2) ==
+                    0 || s_cmp(c4, "HR", (ftnlen)1, (ftnlen)2) == 0 || s_cmp(
+                   c4, "TR", (ftnlen)1, (ftnlen)2) == 0 || s_cmp(c4, "BR", (
+                   ftnlen)1, (ftnlen)2) == 0) {
                nb = 32;
            }
        } else if (*(unsigned char *)c3 == 'M') {
-           if (s_cmp(c4, "QR", (ftnlen)2, (ftnlen)2) == 0 || s_cmp(c4, "RQ", 
-                   (ftnlen)2, (ftnlen)2) == 0 || s_cmp(c4, "LQ", (ftnlen)2, (
-                   ftnlen)2) == 0 || s_cmp(c4, "QL", (ftnlen)2, (ftnlen)2) ==
-                    0 || s_cmp(c4, "HR", (ftnlen)2, (ftnlen)2) == 0 || s_cmp(
-                   c4, "TR", (ftnlen)2, (ftnlen)2) == 0 || s_cmp(c4, "BR", (
-                   ftnlen)2, (ftnlen)2) == 0) {
+           if (s_cmp(c4, "QR", (ftnlen)1, (ftnlen)2) == 0 || s_cmp(c4, "RQ", 
+                   (ftnlen)1, (ftnlen)2) == 0 || s_cmp(c4, "LQ", (ftnlen)1, (
+                   ftnlen)2) == 0 || s_cmp(c4, "QL", (ftnlen)1, (ftnlen)2) ==
+                    0 || s_cmp(c4, "HR", (ftnlen)1, (ftnlen)2) == 0 || s_cmp(
+                   c4, "TR", (ftnlen)1, (ftnlen)2) == 0 || s_cmp(c4, "BR", (
+                   ftnlen)1, (ftnlen)2) == 0) {
                nb = 32;
            }
        }
-    } else if (cname && s_cmp(c2, "UN", (ftnlen)2, (ftnlen)2) == 0) {
+    } else if (cname && s_cmp(c2, "UN", (ftnlen)1, (ftnlen)2) == 0) {
        if (*(unsigned char *)c3 == 'G') {
-           if (s_cmp(c4, "QR", (ftnlen)2, (ftnlen)2) == 0 || s_cmp(c4, "RQ", 
-                   (ftnlen)2, (ftnlen)2) == 0 || s_cmp(c4, "LQ", (ftnlen)2, (
-                   ftnlen)2) == 0 || s_cmp(c4, "QL", (ftnlen)2, (ftnlen)2) ==
-                    0 || s_cmp(c4, "HR", (ftnlen)2, (ftnlen)2) == 0 || s_cmp(
-                   c4, "TR", (ftnlen)2, (ftnlen)2) == 0 || s_cmp(c4, "BR", (
-                   ftnlen)2, (ftnlen)2) == 0) {
+           if (s_cmp(c4, "QR", (ftnlen)1, (ftnlen)2) == 0 || s_cmp(c4, "RQ", 
+                   (ftnlen)1, (ftnlen)2) == 0 || s_cmp(c4, "LQ", (ftnlen)1, (
+                   ftnlen)2) == 0 || s_cmp(c4, "QL", (ftnlen)1, (ftnlen)2) ==
+                    0 || s_cmp(c4, "HR", (ftnlen)1, (ftnlen)2) == 0 || s_cmp(
+                   c4, "TR", (ftnlen)1, (ftnlen)2) == 0 || s_cmp(c4, "BR", (
+                   ftnlen)1, (ftnlen)2) == 0) {
                nb = 32;
            }
        } else if (*(unsigned char *)c3 == 'M') {
-           if (s_cmp(c4, "QR", (ftnlen)2, (ftnlen)2) == 0 || s_cmp(c4, "RQ", 
-                   (ftnlen)2, (ftnlen)2) == 0 || s_cmp(c4, "LQ", (ftnlen)2, (
-                   ftnlen)2) == 0 || s_cmp(c4, "QL", (ftnlen)2, (ftnlen)2) ==
-                    0 || s_cmp(c4, "HR", (ftnlen)2, (ftnlen)2) == 0 || s_cmp(
-                   c4, "TR", (ftnlen)2, (ftnlen)2) == 0 || s_cmp(c4, "BR", (
-                   ftnlen)2, (ftnlen)2) == 0) {
+           if (s_cmp(c4, "QR", (ftnlen)1, (ftnlen)2) == 0 || s_cmp(c4, "RQ", 
+                   (ftnlen)1, (ftnlen)2) == 0 || s_cmp(c4, "LQ", (ftnlen)1, (
+                   ftnlen)2) == 0 || s_cmp(c4, "QL", (ftnlen)1, (ftnlen)2) ==
+                    0 || s_cmp(c4, "HR", (ftnlen)1, (ftnlen)2) == 0 || s_cmp(
+                   c4, "TR", (ftnlen)1, (ftnlen)2) == 0 || s_cmp(c4, "BR", (
+                   ftnlen)1, (ftnlen)2) == 0) {
                nb = 32;
            }
        }
-    } else if (s_cmp(c2, "GB", (ftnlen)2, (ftnlen)2) == 0) {
-       if (s_cmp(c3, "TRF", (ftnlen)3, (ftnlen)3) == 0) {
+    } else if (s_cmp(c2, "GB", (ftnlen)1, (ftnlen)2) == 0) {
+       if (s_cmp(c3, "TRF", (ftnlen)1, (ftnlen)3) == 0) {
            if (sname) {
                if (*n4 <= 64) {
                    nb = 1;
@@ -369,8 +381,8 @@ L50:
                }
            }
        }
-    } else if (s_cmp(c2, "PB", (ftnlen)2, (ftnlen)2) == 0) {
-       if (s_cmp(c3, "TRF", (ftnlen)3, (ftnlen)3) == 0) {
+    } else if (s_cmp(c2, "PB", (ftnlen)1, (ftnlen)2) == 0) {
+       if (s_cmp(c3, "TRF", (ftnlen)1, (ftnlen)3) == 0) {
            if (sname) {
                if (*n2 <= 64) {
                    nb = 1;
@@ -385,24 +397,24 @@ L50:
                }
            }
        }
-    } else if (s_cmp(c2, "TR", (ftnlen)2, (ftnlen)2) == 0) {
-       if (s_cmp(c3, "TRI", (ftnlen)3, (ftnlen)3) == 0) {
+    } else if (s_cmp(c2, "TR", (ftnlen)1, (ftnlen)2) == 0) {
+       if (s_cmp(c3, "TRI", (ftnlen)1, (ftnlen)3) == 0) {
            if (sname) {
                nb = 64;
            } else {
                nb = 64;
            }
        }
-    } else if (s_cmp(c2, "LA", (ftnlen)2, (ftnlen)2) == 0) {
-       if (s_cmp(c3, "UUM", (ftnlen)3, (ftnlen)3) == 0) {
+    } else if (s_cmp(c2, "LA", (ftnlen)1, (ftnlen)2) == 0) {
+       if (s_cmp(c3, "UUM", (ftnlen)1, (ftnlen)3) == 0) {
            if (sname) {
                nb = 64;
            } else {
                nb = 64;
            }
        }
-    } else if (sname && s_cmp(c2, "ST", (ftnlen)2, (ftnlen)2) == 0) {
-       if (s_cmp(c3, "EBZ", (ftnlen)3, (ftnlen)3) == 0) {
+    } else if (sname && s_cmp(c2, "ST", (ftnlen)1, (ftnlen)2) == 0) {
+       if (s_cmp(c3, "EBZ", (ftnlen)1, (ftnlen)3) == 0) {
            nb = 1;
        }
     }
@@ -414,86 +426,86 @@ L60:
 /*     ISPEC = 2:  minimum block size */
 
     nbmin = 2;
-    if (s_cmp(c2, "GE", (ftnlen)2, (ftnlen)2) == 0) {
-       if (s_cmp(c3, "QRF", (ftnlen)3, (ftnlen)3) == 0 || s_cmp(c3, "RQF", (
-               ftnlen)3, (ftnlen)3) == 0 || s_cmp(c3, "LQF", (ftnlen)3, (
-               ftnlen)3) == 0 || s_cmp(c3, "QLF", (ftnlen)3, (ftnlen)3) == 0)
+    if (s_cmp(c2, "GE", (ftnlen)1, (ftnlen)2) == 0) {
+       if (s_cmp(c3, "QRF", (ftnlen)1, (ftnlen)3) == 0 || s_cmp(c3, "RQF", (
+               ftnlen)1, (ftnlen)3) == 0 || s_cmp(c3, "LQF", (ftnlen)1, (
+               ftnlen)3) == 0 || s_cmp(c3, "QLF", (ftnlen)1, (ftnlen)3) == 0)
                 {
            if (sname) {
                nbmin = 2;
            } else {
                nbmin = 2;
            }
-       } else if (s_cmp(c3, "HRD", (ftnlen)3, (ftnlen)3) == 0) {
+       } else if (s_cmp(c3, "HRD", (ftnlen)1, (ftnlen)3) == 0) {
            if (sname) {
                nbmin = 2;
            } else {
                nbmin = 2;
            }
-       } else if (s_cmp(c3, "BRD", (ftnlen)3, (ftnlen)3) == 0) {
+       } else if (s_cmp(c3, "BRD", (ftnlen)1, (ftnlen)3) == 0) {
            if (sname) {
                nbmin = 2;
            } else {
                nbmin = 2;
            }
-       } else if (s_cmp(c3, "TRI", (ftnlen)3, (ftnlen)3) == 0) {
+       } else if (s_cmp(c3, "TRI", (ftnlen)1, (ftnlen)3) == 0) {
            if (sname) {
                nbmin = 2;
            } else {
                nbmin = 2;
            }
        }
-    } else if (s_cmp(c2, "SY", (ftnlen)2, (ftnlen)2) == 0) {
-       if (s_cmp(c3, "TRF", (ftnlen)3, (ftnlen)3) == 0) {
+    } else if (s_cmp(c2, "SY", (ftnlen)1, (ftnlen)2) == 0) {
+       if (s_cmp(c3, "TRF", (ftnlen)1, (ftnlen)3) == 0) {
            if (sname) {
                nbmin = 8;
            } else {
                nbmin = 8;
            }
-       } else if (sname && s_cmp(c3, "TRD", (ftnlen)3, (ftnlen)3) == 0) {
+       } else if (sname && s_cmp(c3, "TRD", (ftnlen)1, (ftnlen)3) == 0) {
            nbmin = 2;
        }
-    } else if (cname && s_cmp(c2, "HE", (ftnlen)2, (ftnlen)2) == 0) {
-       if (s_cmp(c3, "TRD", (ftnlen)3, (ftnlen)3) == 0) {
+    } else if (cname && s_cmp(c2, "HE", (ftnlen)1, (ftnlen)2) == 0) {
+       if (s_cmp(c3, "TRD", (ftnlen)1, (ftnlen)3) == 0) {
            nbmin = 2;
        }
-    } else if (sname && s_cmp(c2, "OR", (ftnlen)2, (ftnlen)2) == 0) {
+    } else if (sname && s_cmp(c2, "OR", (ftnlen)1, (ftnlen)2) == 0) {
        if (*(unsigned char *)c3 == 'G') {
-           if (s_cmp(c4, "QR", (ftnlen)2, (ftnlen)2) == 0 || s_cmp(c4, "RQ", 
-                   (ftnlen)2, (ftnlen)2) == 0 || s_cmp(c4, "LQ", (ftnlen)2, (
-                   ftnlen)2) == 0 || s_cmp(c4, "QL", (ftnlen)2, (ftnlen)2) ==
-                    0 || s_cmp(c4, "HR", (ftnlen)2, (ftnlen)2) == 0 || s_cmp(
-                   c4, "TR", (ftnlen)2, (ftnlen)2) == 0 || s_cmp(c4, "BR", (
-                   ftnlen)2, (ftnlen)2) == 0) {
+           if (s_cmp(c4, "QR", (ftnlen)1, (ftnlen)2) == 0 || s_cmp(c4, "RQ", 
+                   (ftnlen)1, (ftnlen)2) == 0 || s_cmp(c4, "LQ", (ftnlen)1, (
+                   ftnlen)2) == 0 || s_cmp(c4, "QL", (ftnlen)1, (ftnlen)2) ==
+                    0 || s_cmp(c4, "HR", (ftnlen)1, (ftnlen)2) == 0 || s_cmp(
+                   c4, "TR", (ftnlen)1, (ftnlen)2) == 0 || s_cmp(c4, "BR", (
+                   ftnlen)1, (ftnlen)2) == 0) {
                nbmin = 2;
            }
        } else if (*(unsigned char *)c3 == 'M') {
-           if (s_cmp(c4, "QR", (ftnlen)2, (ftnlen)2) == 0 || s_cmp(c4, "RQ", 
-                   (ftnlen)2, (ftnlen)2) == 0 || s_cmp(c4, "LQ", (ftnlen)2, (
-                   ftnlen)2) == 0 || s_cmp(c4, "QL", (ftnlen)2, (ftnlen)2) ==
-                    0 || s_cmp(c4, "HR", (ftnlen)2, (ftnlen)2) == 0 || s_cmp(
-                   c4, "TR", (ftnlen)2, (ftnlen)2) == 0 || s_cmp(c4, "BR", (
-                   ftnlen)2, (ftnlen)2) == 0) {
+           if (s_cmp(c4, "QR", (ftnlen)1, (ftnlen)2) == 0 || s_cmp(c4, "RQ", 
+                   (ftnlen)1, (ftnlen)2) == 0 || s_cmp(c4, "LQ", (ftnlen)1, (
+                   ftnlen)2) == 0 || s_cmp(c4, "QL", (ftnlen)1, (ftnlen)2) ==
+                    0 || s_cmp(c4, "HR", (ftnlen)1, (ftnlen)2) == 0 || s_cmp(
+                   c4, "TR", (ftnlen)1, (ftnlen)2) == 0 || s_cmp(c4, "BR", (
+                   ftnlen)1, (ftnlen)2) == 0) {
                nbmin = 2;
            }
        }
-    } else if (cname && s_cmp(c2, "UN", (ftnlen)2, (ftnlen)2) == 0) {
+    } else if (cname && s_cmp(c2, "UN", (ftnlen)1, (ftnlen)2) == 0) {
        if (*(unsigned char *)c3 == 'G') {
-           if (s_cmp(c4, "QR", (ftnlen)2, (ftnlen)2) == 0 || s_cmp(c4, "RQ", 
-                   (ftnlen)2, (ftnlen)2) == 0 || s_cmp(c4, "LQ", (ftnlen)2, (
-                   ftnlen)2) == 0 || s_cmp(c4, "QL", (ftnlen)2, (ftnlen)2) ==
-                    0 || s_cmp(c4, "HR", (ftnlen)2, (ftnlen)2) == 0 || s_cmp(
-                   c4, "TR", (ftnlen)2, (ftnlen)2) == 0 || s_cmp(c4, "BR", (
-                   ftnlen)2, (ftnlen)2) == 0) {
+           if (s_cmp(c4, "QR", (ftnlen)1, (ftnlen)2) == 0 || s_cmp(c4, "RQ", 
+                   (ftnlen)1, (ftnlen)2) == 0 || s_cmp(c4, "LQ", (ftnlen)1, (
+                   ftnlen)2) == 0 || s_cmp(c4, "QL", (ftnlen)1, (ftnlen)2) ==
+                    0 || s_cmp(c4, "HR", (ftnlen)1, (ftnlen)2) == 0 || s_cmp(
+                   c4, "TR", (ftnlen)1, (ftnlen)2) == 0 || s_cmp(c4, "BR", (
+                   ftnlen)1, (ftnlen)2) == 0) {
                nbmin = 2;
            }
        } else if (*(unsigned char *)c3 == 'M') {
-           if (s_cmp(c4, "QR", (ftnlen)2, (ftnlen)2) == 0 || s_cmp(c4, "RQ", 
-                   (ftnlen)2, (ftnlen)2) == 0 || s_cmp(c4, "LQ", (ftnlen)2, (
-                   ftnlen)2) == 0 || s_cmp(c4, "QL", (ftnlen)2, (ftnlen)2) ==
-                    0 || s_cmp(c4, "HR", (ftnlen)2, (ftnlen)2) == 0 || s_cmp(
-                   c4, "TR", (ftnlen)2, (ftnlen)2) == 0 || s_cmp(c4, "BR", (
-                   ftnlen)2, (ftnlen)2) == 0) {
+           if (s_cmp(c4, "QR", (ftnlen)1, (ftnlen)2) == 0 || s_cmp(c4, "RQ", 
+                   (ftnlen)1, (ftnlen)2) == 0 || s_cmp(c4, "LQ", (ftnlen)1, (
+                   ftnlen)2) == 0 || s_cmp(c4, "QL", (ftnlen)1, (ftnlen)2) ==
+                    0 || s_cmp(c4, "HR", (ftnlen)1, (ftnlen)2) == 0 || s_cmp(
+                   c4, "TR", (ftnlen)1, (ftnlen)2) == 0 || s_cmp(c4, "BR", (
+                   ftnlen)1, (ftnlen)2) == 0) {
                nbmin = 2;
            }
        }
@@ -506,56 +518,56 @@ L70:
 /*     ISPEC = 3:  crossover point */
 
     nx = 0;
-    if (s_cmp(c2, "GE", (ftnlen)2, (ftnlen)2) == 0) {
-       if (s_cmp(c3, "QRF", (ftnlen)3, (ftnlen)3) == 0 || s_cmp(c3, "RQF", (
-               ftnlen)3, (ftnlen)3) == 0 || s_cmp(c3, "LQF", (ftnlen)3, (
-               ftnlen)3) == 0 || s_cmp(c3, "QLF", (ftnlen)3, (ftnlen)3) == 0)
+    if (s_cmp(c2, "GE", (ftnlen)1, (ftnlen)2) == 0) {
+       if (s_cmp(c3, "QRF", (ftnlen)1, (ftnlen)3) == 0 || s_cmp(c3, "RQF", (
+               ftnlen)1, (ftnlen)3) == 0 || s_cmp(c3, "LQF", (ftnlen)1, (
+               ftnlen)3) == 0 || s_cmp(c3, "QLF", (ftnlen)1, (ftnlen)3) == 0)
                 {
            if (sname) {
                nx = 128;
            } else {
                nx = 128;
            }
-       } else if (s_cmp(c3, "HRD", (ftnlen)3, (ftnlen)3) == 0) {
+       } else if (s_cmp(c3, "HRD", (ftnlen)1, (ftnlen)3) == 0) {
            if (sname) {
                nx = 128;
            } else {
                nx = 128;
            }
-       } else if (s_cmp(c3, "BRD", (ftnlen)3, (ftnlen)3) == 0) {
+       } else if (s_cmp(c3, "BRD", (ftnlen)1, (ftnlen)3) == 0) {
            if (sname) {
                nx = 128;
            } else {
                nx = 128;
            }
        }
-    } else if (s_cmp(c2, "SY", (ftnlen)2, (ftnlen)2) == 0) {
-       if (sname && s_cmp(c3, "TRD", (ftnlen)3, (ftnlen)3) == 0) {
+    } else if (s_cmp(c2, "SY", (ftnlen)1, (ftnlen)2) == 0) {
+       if (sname && s_cmp(c3, "TRD", (ftnlen)1, (ftnlen)3) == 0) {
            nx = 32;
        }
-    } else if (cname && s_cmp(c2, "HE", (ftnlen)2, (ftnlen)2) == 0) {
-       if (s_cmp(c3, "TRD", (ftnlen)3, (ftnlen)3) == 0) {
+    } else if (cname && s_cmp(c2, "HE", (ftnlen)1, (ftnlen)2) == 0) {
+       if (s_cmp(c3, "TRD", (ftnlen)1, (ftnlen)3) == 0) {
            nx = 32;
        }
-    } else if (sname && s_cmp(c2, "OR", (ftnlen)2, (ftnlen)2) == 0) {
+    } else if (sname && s_cmp(c2, "OR", (ftnlen)1, (ftnlen)2) == 0) {
        if (*(unsigned char *)c3 == 'G') {
-           if (s_cmp(c4, "QR", (ftnlen)2, (ftnlen)2) == 0 || s_cmp(c4, "RQ", 
-                   (ftnlen)2, (ftnlen)2) == 0 || s_cmp(c4, "LQ", (ftnlen)2, (
-                   ftnlen)2) == 0 || s_cmp(c4, "QL", (ftnlen)2, (ftnlen)2) ==
-                    0 || s_cmp(c4, "HR", (ftnlen)2, (ftnlen)2) == 0 || s_cmp(
-                   c4, "TR", (ftnlen)2, (ftnlen)2) == 0 || s_cmp(c4, "BR", (
-                   ftnlen)2, (ftnlen)2) == 0) {
+           if (s_cmp(c4, "QR", (ftnlen)1, (ftnlen)2) == 0 || s_cmp(c4, "RQ", 
+                   (ftnlen)1, (ftnlen)2) == 0 || s_cmp(c4, "LQ", (ftnlen)1, (
+                   ftnlen)2) == 0 || s_cmp(c4, "QL", (ftnlen)1, (ftnlen)2) ==
+                    0 || s_cmp(c4, "HR", (ftnlen)1, (ftnlen)2) == 0 || s_cmp(
+                   c4, "TR", (ftnlen)1, (ftnlen)2) == 0 || s_cmp(c4, "BR", (
+                   ftnlen)1, (ftnlen)2) == 0) {
                nx = 128;
            }
        }
-    } else if (cname && s_cmp(c2, "UN", (ftnlen)2, (ftnlen)2) == 0) {
+    } else if (cname && s_cmp(c2, "UN", (ftnlen)1, (ftnlen)2) == 0) {
        if (*(unsigned char *)c3 == 'G') {
-           if (s_cmp(c4, "QR", (ftnlen)2, (ftnlen)2) == 0 || s_cmp(c4, "RQ", 
-                   (ftnlen)2, (ftnlen)2) == 0 || s_cmp(c4, "LQ", (ftnlen)2, (
-                   ftnlen)2) == 0 || s_cmp(c4, "QL", (ftnlen)2, (ftnlen)2) ==
-                    0 || s_cmp(c4, "HR", (ftnlen)2, (ftnlen)2) == 0 || s_cmp(
-                   c4, "TR", (ftnlen)2, (ftnlen)2) == 0 || s_cmp(c4, "BR", (
-                   ftnlen)2, (ftnlen)2) == 0) {
+           if (s_cmp(c4, "QR", (ftnlen)1, (ftnlen)2) == 0 || s_cmp(c4, "RQ", 
+                   (ftnlen)1, (ftnlen)2) == 0 || s_cmp(c4, "LQ", (ftnlen)1, (
+                   ftnlen)2) == 0 || s_cmp(c4, "QL", (ftnlen)1, (ftnlen)2) ==
+                    0 || s_cmp(c4, "HR", (ftnlen)1, (ftnlen)2) == 0 || s_cmp(
+                   c4, "TR", (ftnlen)1, (ftnlen)2) == 0 || s_cmp(c4, "BR", (
+                   ftnlen)1, (ftnlen)2) == 0) {
                nx = 128;
            }
        }
@@ -614,7 +626,7 @@ L140:
 /*     ILAENV = 0 */
     ret_val = 1;
     if (ret_val == 1) {
-       ret_val = ieeeck_(&c__0, &c_b163, &c_b164);
+       ret_val = ieeeck_(&c__1, &c_b163, &c_b164);
     }
     return ret_val;
 
@@ -625,7 +637,7 @@ L150:
 /*     ILAENV = 0 */
     ret_val = 1;
     if (ret_val == 1) {
-       ret_val = ieeeck_(&c__1, &c_b163, &c_b164);
+       ret_val = ieeeck_(&c__0, &c_b163, &c_b164);
     }
     return ret_val;
 
diff --git a/3rdparty/lapack/ilaslc.c b/3rdparty/lapack/ilaslc.c
new file mode 100644 (file)
index 0000000..e057453
--- /dev/null
@@ -0,0 +1,88 @@
+/* ilaslc.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
+#include "clapack.h"
+
+
+integer ilaslc_(integer *m, integer *n, real *a, integer *lda)
+{
+    /* System generated locals */
+    integer a_dim1, a_offset, ret_val, i__1;
+
+    /* Local variables */
+    integer i__;
+
+
+/*  -- LAPACK auxiliary routine (version 3.2.1)                        -- */
+
+/*  -- April 2009                                                      -- */
+
+/*  -- LAPACK is a software package provided by Univ. of Tennessee,    -- */
+/*  -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..-- */
+
+/*     .. Scalar Arguments .. */
+/*     .. */
+/*     .. Array Arguments .. */
+/*     .. */
+
+/*  Purpose */
+/*  ======= */
+
+/*  ILASLC scans A for its last non-zero column. */
+
+/*  Arguments */
+/*  ========= */
+
+/*  M       (input) INTEGER */
+/*          The number of rows of the matrix A. */
+
+/*  N       (input) INTEGER */
+/*          The number of columns of the matrix A. */
+
+/*  A       (input) REAL array, dimension (LDA,N) */
+/*          The m by n matrix A. */
+
+/*  LDA     (input) INTEGER */
+/*          The leading dimension of the array A. LDA >= max(1,M). */
+
+/*  ===================================================================== */
+
+/*     .. Parameters .. */
+/*     .. */
+/*     .. Local Scalars .. */
+/*     .. */
+/*     .. Executable Statements .. */
+
+/*     Quick test for the common case where one corner is non-zero. */
+    /* Parameter adjustments */
+    a_dim1 = *lda;
+    a_offset = 1 + a_dim1;
+    a -= a_offset;
+
+    /* Function Body */
+    if (*n == 0) {
+       ret_val = *n;
+    } else if (a[*n * a_dim1 + 1] != 0.f || a[*m + *n * a_dim1] != 0.f) {
+       ret_val = *n;
+    } else {
+/*     Now scan each column from the end, returning with the first non-zero. */
+       for (ret_val = *n; ret_val >= 1; --ret_val) {
+           i__1 = *m;
+           for (i__ = 1; i__ <= i__1; ++i__) {
+               if (a[i__ + ret_val * a_dim1] != 0.f) {
+                   return ret_val;
+               }
+           }
+       }
+    }
+    return ret_val;
+} /* ilaslc_ */
diff --git a/3rdparty/lapack/ilaslr.c b/3rdparty/lapack/ilaslr.c
new file mode 100644 (file)
index 0000000..b0fa274
--- /dev/null
@@ -0,0 +1,90 @@
+/* ilaslr.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
+#include "clapack.h"
+
+
+integer ilaslr_(integer *m, integer *n, real *a, integer *lda)
+{
+    /* System generated locals */
+    integer a_dim1, a_offset, ret_val, i__1;
+
+    /* Local variables */
+    integer i__, j;
+
+
+/*  -- LAPACK auxiliary routine (version 3.2.1)                        -- */
+
+/*  -- April 2009                                                      -- */
+
+/*  -- LAPACK is a software package provided by Univ. of Tennessee,    -- */
+/*  -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..-- */
+
+/*     .. Scalar Arguments .. */
+/*     .. */
+/*     .. Array Arguments .. */
+/*     .. */
+
+/*  Purpose */
+/*  ======= */
+
+/*  ILASLR scans A for its last non-zero row. */
+
+/*  Arguments */
+/*  ========= */
+
+/*  M       (input) INTEGER */
+/*          The number of rows of the matrix A. */
+
+/*  N       (input) INTEGER */
+/*          The number of columns of the matrix A. */
+
+/*  A       (input) REAL             array, dimension (LDA,N) */
+/*          The m by n matrix A. */
+
+/*  LDA     (input) INTEGER */
+/*          The leading dimension of the array A. LDA >= max(1,M). */
+
+/*  ===================================================================== */
+
+/*     .. Parameters .. */
+/*     .. */
+/*     .. Local Scalars .. */
+/*     .. */
+/*     .. Executable Statements .. */
+
+/*     Quick test for the common case where one corner is non-zero. */
+    /* Parameter adjustments */
+    a_dim1 = *lda;
+    a_offset = 1 + a_dim1;
+    a -= a_offset;
+
+    /* Function Body */
+    if (*m == 0) {
+       ret_val = *m;
+    } else if (a[*m + a_dim1] != 0.f || a[*m + *n * a_dim1] != 0.f) {
+       ret_val = *m;
+    } else {
+/*     Scan up each column tracking the last zero row seen. */
+       ret_val = 0;
+       i__1 = *n;
+       for (j = 1; j <= i__1; ++j) {
+           for (i__ = *m; i__ >= 1; --i__) {
+               if (a[i__ + j * a_dim1] != 0.f) {
+                   break;
+               }
+           }
+           ret_val = max(ret_val,i__);
+       }
+    }
+    return ret_val;
+} /* ilaslr_ */
index d5bbd88..abc4036 100644 (file)
@@ -1,5 +1,18 @@
+/* iparmq.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 integer iparmq_(integer *ispec, char *name__, char *opts, integer *n, integer 
        *ilo, integer *ihi, integer *lwork)
 {
@@ -15,7 +28,7 @@ integer iparmq_(integer *ispec, char *name__, char *opts, integer *n, integer
     integer nh, ns;
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
@@ -189,7 +202,7 @@ integer iparmq_(integer *ispec, char *name__, char *opts, integer *n, integer
        }
        if (nh >= 150) {
 /* Computing MAX */
-           r__1 = (real)(log((doublereal) nh) / log(2.));
+           r__1 = log((real) nh) / log(2.f);
            i__1 = 10, i__2 = nh / i_nint(&r__1);
            ns = max(i__1,i__2);
        }
index c90d7c8..bf17812 100644 (file)
@@ -1,5 +1,18 @@
+/* isamax.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 integer isamax_(integer *n, real *sx, integer *incx)
 {
     /* System generated locals */
index 0cdc2b3..2fd9efa 100644 (file)
@@ -1,5 +1,18 @@
+/* sasum.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 doublereal sasum_(integer *n, real *sx, integer *incx)
 {
     /* System generated locals */
index 070c899..3a48bb8 100644 (file)
@@ -1,5 +1,18 @@
+/* saxpy.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int saxpy_(integer *n, real *sa, real *sx, integer *incx, 
        real *sy, integer *incy)
 {
index 6874cfa..0f383d9 100644 (file)
@@ -1,5 +1,18 @@
+/* sbdsdc.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__9 = 9;
@@ -62,7 +75,7 @@ static real c_b29 = 0.f;
     integer givptr, qstart, smlsiz, wstart, smlszp;
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index ebeb1eb..fa3f04a 100644 (file)
@@ -1,5 +1,18 @@
+/* sbdsqr.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static doublereal c_b15 = -.125;
@@ -63,7 +76,7 @@ static real c_b72 = -1.f;
     real tolmul;
 
 
-/*  -- LAPACK routine (version 3.1.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     January 2007 */
 
@@ -163,16 +176,23 @@ static real c_b72 = -1.f;
 /*          The leading dimension of the array C. */
 /*          LDC >= max(1,N) if NCC > 0; LDC >=1 if NCC = 0. */
 
-/*  WORK    (workspace) REAL array, dimension (2*N) */
-/*          if NCVT = NRU = NCC = 0, (max(1, 4*N)) otherwise */
+/*  WORK    (workspace) REAL array, dimension (4*N) */
 
 /*  INFO    (output) INTEGER */
 /*          = 0:  successful exit */
 /*          < 0:  If INFO = -i, the i-th argument had an illegal value */
-/*          > 0:  the algorithm did not converge; D and E contain the */
-/*                elements of a bidiagonal matrix which is orthogonally */
-/*                similar to the input matrix B;  if INFO = i, i */
-/*                elements of E have not converged to zero. */
+/*          > 0: */
+/*             if NCVT = NRU = NCC = 0, */
+/*                = 1, a split was marked by a positive value in E */
+/*                = 2, current block of Z not diagonalized after 30*N */
+/*                     iterations (in inner while loop) */
+/*                = 3, termination criterion of outer while loop not met */
+/*                     (program created more than N unreduced blocks) */
+/*             else NCVT = NRU = NCC = 0, */
+/*                   the algorithm did not converge; D and E contain the */
+/*                   elements of a bidiagonal matrix which is orthogonally */
+/*                   similar to the input matrix B;  if INFO = i, i */
+/*                   elements of E have not converged to zero. */
 
 /*  Internal Parameters */
 /*  =================== */
index 842d316..92cee80 100644 (file)
@@ -1,5 +1,18 @@
+/* scopy.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int scopy_(integer *n, real *sx, integer *incx, real *sy, 
        integer *incy)
 {
index 34326ed..5caed92 100644 (file)
@@ -1,5 +1,18 @@
+/* sdot.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 doublereal sdot_(integer *n, real *sx, integer *incx, real *sy, integer *incy)
 {
     /* System generated locals */
index d521f80..a7755ae 100644 (file)
@@ -1,5 +1,18 @@
+/* sgebd2.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -18,7 +31,7 @@ static integer c__1 = 1;
            integer *, real *);
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index d75449e..2f2cd68 100644 (file)
@@ -1,5 +1,18 @@
+/* sgebrd.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -35,7 +48,7 @@ static real c_b22 = 1.f;
     logical lquery;
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index fa9896f..6f17b9f 100644 (file)
@@ -1,5 +1,18 @@
+/* sgelq2.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int sgelq2_(integer *m, integer *n, real *a, integer *lda, 
        real *tau, real *work, integer *info)
 {
     real aii;
     extern /* Subroutine */ int slarf_(char *, integer *, integer *, real *, 
            integer *, real *, real *, integer *, real *), xerbla_(
-           char *, integer *), slarfg_(integer *, real *, real *, 
+           char *, integer *), slarfp_(integer *, real *, real *, 
            integer *, real *);
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
        i__2 = *n - i__ + 1;
 /* Computing MIN */
        i__3 = i__ + 1;
-       slarfg_(&i__2, &a[i__ + i__ * a_dim1], &a[i__ + min(i__3, *n)* a_dim1]
+       slarfp_(&i__2, &a[i__ + i__ * a_dim1], &a[i__ + min(i__3, *n)* a_dim1]
 , lda, &tau[i__]);
        if (i__ < *m) {
 
index f2cf99e..c777eba 100644 (file)
@@ -1,5 +1,18 @@
+/* sgelqf.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -27,7 +40,7 @@ static integer c__2 = 2;
     logical lquery;
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 9bf812f..f5017bf 100644 (file)
@@ -1,5 +1,18 @@
+/* sgels.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -49,7 +62,7 @@ static integer c__0 = 0;
 , integer *);
 
 
-/*  -- LAPACK driver routine (version 3.1) -- */
+/*  -- LAPACK driver routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 6651763..1e3d174 100644 (file)
@@ -1,5 +1,18 @@
+/* sgelsd.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__9 = 9;
@@ -62,7 +75,7 @@ static real c_b81 = 0.f;
            integer *, integer *);
 
 
-/*  -- LAPACK driver routine (version 3.1) -- */
+/*  -- LAPACK driver routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
@@ -337,6 +350,15 @@ static real c_b81 = 0.f;
 /* Computing MAX */
                    i__1 = maxwrk, i__2 = *m * *m + (*m << 2) + wlalsd;
                    maxwrk = max(i__1,i__2);
+/*     XXX: Ensure the Path 2a case below is triggered.  The workspace */
+/*     calculation should use queries for all routines eventually. */
+/* Computing MAX */
+/* Computing MAX */
+                   i__3 = *m, i__4 = (*m << 1) - 4, i__3 = max(i__3,i__4), 
+                           i__3 = max(i__3,*nrhs), i__4 = *n - *m * 3;
+                   i__1 = maxwrk, i__2 = (*m << 2) + *m * *m + max(i__3,i__4)
+                           ;
+                   maxwrk = max(i__1,i__2);
                } else {
 
 /*                 Path 2 - remaining underdetermined cases. */
index 95ed35d..8e8a556 100644 (file)
@@ -1,5 +1,18 @@
+/* sgemm.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int sgemm_(char *transa, char *transb, integer *m, integer *
        n, integer *k, real *alpha, real *a, integer *lda, real *b, integer *
        ldb, real *beta, real *c__, integer *ldc)
index efa31d2..857f013 100644 (file)
@@ -1,5 +1,18 @@
+/* sgemv.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int sgemv_(char *trans, integer *m, integer *n, real *alpha, 
        real *a, integer *lda, real *x, integer *incx, real *beta, real *y, 
        integer *incy)
index 8a0d7ca..001b8ef 100644 (file)
@@ -1,5 +1,18 @@
+/* sgeqr2.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -15,11 +28,11 @@ static integer c__1 = 1;
     real aii;
     extern /* Subroutine */ int slarf_(char *, integer *, integer *, real *, 
            integer *, real *, real *, integer *, real *), xerbla_(
-           char *, integer *), slarfg_(integer *, real *, real *, 
+           char *, integer *), slarfp_(integer *, real *, real *, 
            integer *, real *);
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
@@ -125,7 +138,7 @@ static integer c__1 = 1;
        i__2 = *m - i__ + 1;
 /* Computing MIN */
        i__3 = i__ + 1;
-       slarfg_(&i__2, &a[i__ + i__ * a_dim1], &a[min(i__3, *m)+ i__ * a_dim1]
+       slarfp_(&i__2, &a[i__ + i__ * a_dim1], &a[min(i__3, *m)+ i__ * a_dim1]
 , &c__1, &tau[i__]);
        if (i__ < *n) {
 
index 1a75e9a..6a1e557 100644 (file)
@@ -1,5 +1,18 @@
+/* sgeqrf.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -27,7 +40,7 @@ static integer c__2 = 2;
     logical lquery;
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index cbaa99f..7c55942 100644 (file)
@@ -1,5 +1,18 @@
+/* sger.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int sger_(integer *m, integer *n, real *alpha, real *x, 
        integer *incx, real *y, integer *incy, real *a, integer *lda)
 {
index 0610ec3..47eea27 100644 (file)
@@ -1,5 +1,18 @@
+/* sgesdd.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -70,9 +83,10 @@ static real c_b248 = 1.f;
     logical lquery;
 
 
-/*  -- LAPACK driver routine (version 3.1) -- */
-/*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
-/*     November 2006 */
+/*  -- LAPACK driver routine (version 3.2.1)                                  -- */
+/*  -- LAPACK is a software package provided by Univ. of Tennessee,    -- */
+/*  -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..-- */
+/*     March 2009 */
 
 /*     .. Scalar Arguments .. */
 /*     .. */
@@ -181,10 +195,10 @@ static real c_b248 = 1.f;
 /*          If JOBZ = 'N', */
 /*            LWORK >= 3*min(M,N) + max(max(M,N),6*min(M,N)). */
 /*          If JOBZ = 'O', */
-/*            LWORK >= 3*min(M,N)*min(M,N) + */
+/*            LWORK >= 3*min(M,N) + */
 /*                     max(max(M,N),5*min(M,N)*min(M,N)+4*min(M,N)). */
 /*          If JOBZ = 'S' or 'A' */
-/*            LWORK >= 3*min(M,N)*min(M,N) + */
+/*            LWORK >= 3*min(M,N) + */
 /*                     max(max(M,N),4*min(M,N)*min(M,N)+4*min(M,N)). */
 /*          For good performance, LWORK should generally be larger. */
 /*          If LWORK = -1 but other input arguments are legal, WORK(1) */
index c2a9d14..b12beeb 100644 (file)
@@ -1,5 +1,18 @@
+/* sgesv.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int sgesv_(integer *n, integer *nrhs, real *a, integer *lda, 
        integer *ipiv, real *b, integer *ldb, integer *info)
 {
@@ -13,7 +26,7 @@
 , real *, integer *, integer *);
 
 
-/*  -- LAPACK driver routine (version 3.1) -- */
+/*  -- LAPACK driver routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index ed7dd60..4fbe523 100644 (file)
@@ -1,5 +1,18 @@
+/* sgetf2.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -25,7 +38,7 @@ static real c_b8 = -1.f;
     extern integer isamax_(integer *, real *, integer *);
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 9ddfb0b..4f570bf 100644 (file)
@@ -1,5 +1,18 @@
+/* sgetrf.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -28,7 +41,7 @@ static real c_b19 = -1.f;
            *, integer *, integer *, integer *);
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index f5d9770..db435fc 100644 (file)
@@ -1,5 +1,18 @@
+/* sgetri.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -32,7 +45,7 @@ static real c_b22 = 1.f;
            integer *, integer *);
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index f6b8bc9..3bfdb62 100644 (file)
@@ -1,5 +1,18 @@
+/* sgetrs.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -22,7 +35,7 @@ static integer c_n1 = -1;
            *, integer *, integer *, integer *);
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 9e00fc9..e6f6c25 100644 (file)
@@ -1,12 +1,25 @@
+/* slabad.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int slabad_(real *small, real *large)
 {
     /* Builtin functions */
     double r_lg10(real *), sqrt(doublereal);
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 8067641..43b5dd7 100644 (file)
@@ -1,5 +1,18 @@
+/* slabrd.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static real c_b4 = -1.f;
@@ -23,7 +36,7 @@ static real c_b16 = 0.f;
            integer *, real *, real *, integer *, real *);
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 59ed5ea..529168e 100644 (file)
@@ -1,5 +1,18 @@
+/* slacpy.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int slacpy_(char *uplo, integer *m, integer *n, real *a, 
        integer *lda, real *b, integer *ldb)
 {
@@ -11,7 +24,7 @@
     extern logical lsame_(char *, char *);
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index ac73237..021bc3f 100644 (file)
@@ -1,5 +1,18 @@
+/* slae2.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int slae2_(real *a, real *b, real *c__, real *rt1, real *rt2)
 {
     /* System generated locals */
@@ -12,7 +25,7 @@
     real ab, df, tb, sm, rt, adf, acmn, acmx;
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index b0106ce..e169b57 100644 (file)
@@ -1,5 +1,18 @@
+/* slaebz.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int slaebz_(integer *ijob, integer *nitmax, integer *n, 
        integer *mmax, integer *minp, integer *nbmin, real *abstol, real *
        reltol, real *pivmin, real *d__, real *e, real *e2, integer *nval, 
@@ -17,7 +30,7 @@
     integer itmp1, itmp2, kfnew, klnew;
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 37d1b1c..96228e7 100644 (file)
@@ -1,5 +1,18 @@
+/* slaed0.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__9 = 9;
@@ -50,7 +63,7 @@ static integer c__1 = 1;
            real *, integer *, real *, integer *);
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 85acc30..1c307af 100644 (file)
@@ -1,5 +1,18 @@
+/* slaed1.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -27,7 +40,7 @@ static integer c_n1 = -1;
     integer coltyp;
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 0ae5292..9736d4e 100644 (file)
@@ -1,5 +1,18 @@
+/* slaed2.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static real c_b3 = -1.f;
@@ -36,7 +49,7 @@ static integer c__1 = 1;
            real *, integer *, real *, integer *);
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 658c6b8..0603930 100644 (file)
@@ -1,5 +1,18 @@
+/* slaed3.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -33,7 +46,7 @@ static real c_b23 = 0.f;
            real *, integer *);
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 42d15af..355e9de 100644 (file)
@@ -1,5 +1,18 @@
+/* slaed4.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int slaed4_(integer *n, integer *i__, real *d__, real *z__, 
        real *delta, real *rho, real *dlam, integer *info)
 {
@@ -33,7 +46,7 @@
     real erretm, rhoinv;
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 8848e99..aaf1880 100644 (file)
@@ -1,5 +1,18 @@
+/* slaed5.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int slaed5_(integer *i__, real *d__, real *z__, real *delta, 
        real *rho, real *dlam)
 {
@@ -13,7 +26,7 @@
     real b, c__, w, del, tau, temp;
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 8d9fe71..67c21a7 100644 (file)
@@ -1,5 +1,18 @@
+/* slaed6.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int slaed6_(integer *kniter, logical *orgati, real *rho, 
        real *d__, real *z__, real *finit, real *tau, integer *info)
 {
@@ -23,7 +36,7 @@
     real zscale[3], erretm, sclinv;
 
 
-/*  -- LAPACK routine (version 3.1.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     February 2007 */
 
index 9457c22..62539f4 100644 (file)
@@ -1,5 +1,18 @@
+/* slaed7.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__2 = 2;
@@ -42,7 +55,7 @@ static integer c_n1 = -1;
     integer coltyp;
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index eebcbe5..fb6a4cf 100644 (file)
@@ -1,5 +1,18 @@
+/* slaed8.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static real c_b3 = -1.f;
@@ -37,7 +50,7 @@ static integer c__1 = 1;
            real *, integer *, real *, integer *);
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 353b3f4..9152734 100644 (file)
@@ -1,5 +1,18 @@
+/* slaed9.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -26,7 +39,7 @@ static integer c__1 = 1;
     extern /* Subroutine */ int xerbla_(char *, integer *);
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index d03e2d8..24da1a5 100644 (file)
@@ -1,5 +1,18 @@
+/* slaeda.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__2 = 2;
@@ -29,7 +42,7 @@ static real c_b26 = 0.f;
            xerbla_(char *, integer *);
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 19356a8..8b698a7 100644 (file)
@@ -1,5 +1,18 @@
+/* slaev2.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int slaev2_(real *a, real *b, real *c__, real *rt1, real *
        rt2, real *cs1, real *sn1)
 {
@@ -15,7 +28,7 @@
     real acmn, acmx;
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index d24f691..8b78a50 100644 (file)
@@ -1,5 +1,18 @@
+/* slagtf.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int slagtf_(integer *n, real *a, real *lambda, real *b, real 
        *c__, real *tol, real *d__, integer *in, integer *info)
 {
@@ -14,7 +27,7 @@
     extern /* Subroutine */ int xerbla_(char *, integer *);
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index d104b7b..8ff7b64 100644 (file)
@@ -1,5 +1,18 @@
+/* slagts.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int slagts_(integer *job, integer *n, real *a, real *b, real 
        *c__, real *d__, integer *in, real *y, real *tol, integer *info)
 {
@@ -18,7 +31,7 @@
     real bignum;
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 6a3cae9..4c1f57c 100644 (file)
@@ -1,12 +1,25 @@
+/* slaisnan.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 logical slaisnan_(real *sin1, real *sin2)
 {
     /* System generated locals */
     logical ret_val;
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
@@ -24,8 +37,7 @@ logical slaisnan_(real *sin1, real *sin2)
 /*  returns .TRUE.  To check for NaNs, pass the same variable as both */
 /*  arguments. */
 
-/*  Strictly speaking, Fortran does not allow aliasing of function */
-/*  arguments. So a compiler must assume that the two arguments are */
+/*  A compiler must assume that the two arguments are */
 /*  not the same variable, and the test will not be optimized away. */
 /*  Interprocedural or whole-program optimization may delete this */
 /*  test.  The ISNAN functions will be replaced by the correct */
index 8d0ebbf..60378ce 100644 (file)
@@ -1,5 +1,18 @@
+/* slals0.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static real c_b5 = -1.f;
@@ -42,7 +55,7 @@ static integer c__0 = 0;
            real *, integer *);
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 652894d..6d102c5 100644 (file)
@@ -1,5 +1,18 @@
+/* slalsa.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static real c_b7 = 1.f;
@@ -39,7 +52,7 @@ static integer c__2 = 2;
            integer *, integer *, integer *, integer *, integer *);
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 01f1d66..22dc5d7 100644 (file)
@@ -1,5 +1,18 @@
+/* slalsd.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -68,7 +81,7 @@ static real c_b11 = 1.f;
     integer givptr, smlszp;
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 945f171..57601f6 100644 (file)
@@ -1,5 +1,18 @@
+/* slamrg.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int slamrg_(integer *n1, integer *n2, real *a, integer *
        strd1, integer *strd2, integer *index)
 {
@@ -10,7 +23,7 @@
     integer i__, ind1, ind2, n1sv, n2sv;
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index a624a1a..beff686 100644 (file)
@@ -1,5 +1,18 @@
+/* slaneg.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 integer slaneg_(integer *n, real *d__, real *lld, real *sigma, real *pivmin, 
        integer *r__)
 {
@@ -19,7 +32,7 @@ integer slaneg_(integer *n, real *d__, real *lld, real *sigma, real *pivmin,
     real dminus;
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index abb1271..bcce944 100644 (file)
@@ -1,5 +1,18 @@
+/* slange.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -23,7 +36,7 @@ doublereal slange_(char *norm, integer *m, integer *n, real *a, integer *lda,
            real *);
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index e001112..54bef0c 100644 (file)
@@ -1,5 +1,18 @@
+/* slanst.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -22,7 +35,7 @@ doublereal slanst_(char *norm, integer *n, real *d__, real *e)
            real *);
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index f583690..9dd684e 100644 (file)
@@ -1,5 +1,18 @@
+/* slansy.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -23,7 +36,7 @@ doublereal slansy_(char *norm, char *uplo, integer *n, real *a, integer *lda,
            real *);
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index e8054eb..13eb5d9 100644 (file)
@@ -1,5 +1,18 @@
+/* slapy2.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 doublereal slapy2_(real *x, real *y)
 {
     /* System generated locals */
@@ -12,7 +25,7 @@ doublereal slapy2_(real *x, real *y)
     real w, z__, xabs, yabs;
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index fae9beb..b6e8d76 100644 (file)
@@ -1,5 +1,18 @@
+/* slar1v.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int slar1v_(integer *n, integer *b1, integer *bn, real *
        lambda, real *d__, real *l, real *ld, real *lld, real *pivmin, real *
        gaptol, real *z__, logical *wantnc, integer *negcnt, real *ztz, real *
@@ -27,7 +40,7 @@
     logical sawnan1, sawnan2;
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 7ab2dfa..110b33a 100644 (file)
@@ -1,5 +1,18 @@
+/* slarf.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static real c_b4 = 1.f;
@@ -14,14 +27,20 @@ static integer c__1 = 1;
     real r__1;
 
     /* Local variables */
+    integer i__;
+    logical applyleft;
     extern /* Subroutine */ int sger_(integer *, integer *, real *, real *, 
            integer *, real *, integer *, real *, integer *);
     extern logical lsame_(char *, char *);
+    integer lastc;
     extern /* Subroutine */ int sgemv_(char *, integer *, integer *, real *, 
            real *, integer *, real *, integer *, real *, real *, integer *);
+    integer lastv;
+    extern integer ilaslc_(integer *, integer *, real *, integer *), ilaslr_(
+           integer *, integer *, real *, integer *);
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
@@ -83,6 +102,8 @@ static integer c__1 = 1;
 
 /*     .. Parameters .. */
 /*     .. */
+/*     .. Local Scalars .. */
+/*     .. */
 /*     .. External Subroutines .. */
 /*     .. */
 /*     .. External Functions .. */
@@ -97,39 +118,70 @@ static integer c__1 = 1;
     --work;
 
     /* Function Body */
-    if (lsame_(side, "L")) {
+    applyleft = lsame_(side, "L");
+    lastv = 0;
+    lastc = 0;
+    if (*tau != 0.f) {
+/*     Set up variables for scanning V.  LASTV begins pointing to the end */
+/*     of V. */
+       if (applyleft) {
+           lastv = *m;
+       } else {
+           lastv = *n;
+       }
+       if (*incv > 0) {
+           i__ = (lastv - 1) * *incv + 1;
+       } else {
+           i__ = 1;
+       }
+/*     Look for the last non-zero row in V. */
+       while(lastv > 0 && v[i__] == 0.f) {
+           --lastv;
+           i__ -= *incv;
+       }
+       if (applyleft) {
+/*     Scan for the last non-zero column in C(1:lastv,:). */
+           lastc = ilaslc_(&lastv, n, &c__[c_offset], ldc);
+       } else {
+/*     Scan for the last non-zero row in C(:,1:lastv). */
+           lastc = ilaslr_(m, &lastv, &c__[c_offset], ldc);
+       }
+    }
+/*     Note that lastc.eq.0 renders the BLAS operations null; no special */
+/*     case is needed at this level. */
+    if (applyleft) {
 
 /*        Form  H * C */
 
-       if (*tau != 0.f) {
+       if (lastv > 0) {
 
-/*           w := C' * v */
+/*           w(1:lastc,1) := C(1:lastv,1:lastc)' * v(1:lastv,1) */
 
-           sgemv_("Transpose", m, n, &c_b4, &c__[c_offset], ldc, &v[1], incv, 
-                    &c_b5, &work[1], &c__1);
+           sgemv_("Transpose", &lastv, &lastc, &c_b4, &c__[c_offset], ldc, &
+                   v[1], incv, &c_b5, &work[1], &c__1);
 
-/*           C := C - v * w' */
+/*           C(1:lastv,1:lastc) := C(...) - v(1:lastv,1) * w(1:lastc,1)' */
 
            r__1 = -(*tau);
-           sger_(m, n, &r__1, &v[1], incv, &work[1], &c__1, &c__[c_offset], 
-                   ldc);
+           sger_(&lastv, &lastc, &r__1, &v[1], incv, &work[1], &c__1, &c__[
+                   c_offset], ldc);
        }
     } else {
 
 /*        Form  C * H */
 
-       if (*tau != 0.f) {
+       if (lastv > 0) {
 
-/*           w := C * v */
+/*           w(1:lastc,1) := C(1:lastc,1:lastv) * v(1:lastv,1) */
 
-           sgemv_("No transpose", m, n, &c_b4, &c__[c_offset], ldc, &v[1]
-                   incv, &c_b5, &work[1], &c__1);
+           sgemv_("No transpose", &lastc, &lastv, &c_b4, &c__[c_offset], ldc
+                    &v[1], incv, &c_b5, &work[1], &c__1);
 
-/*           C := C - w * v' */
+/*           C(1:lastc,1:lastv) := C(...) - w(1:lastc,1) * v(1:lastv,1)' */
 
            r__1 = -(*tau);
-           sger_(m, n, &r__1, &work[1], &c__1, &v[1], incv, &c__[c_offset], 
-                   ldc);
+           sger_(&lastc, &lastv, &r__1, &work[1], &c__1, &v[1], incv, &c__[
+                   c_offset], ldc);
        }
     }
     return 0;
index cea4387..72d7285 100644 (file)
@@ -1,5 +1,18 @@
+/* slarfb.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -18,16 +31,20 @@ static real c_b25 = -1.f;
     /* Local variables */
     integer i__, j;
     extern logical lsame_(char *, char *);
+    integer lastc;
     extern /* Subroutine */ int sgemm_(char *, char *, integer *, integer *, 
            integer *, real *, real *, integer *, real *, integer *, real *, 
-           real *, integer *), scopy_(integer *, real *, 
-           integer *, real *, integer *), strmm_(char *, char *, char *, 
-           char *, integer *, integer *, real *, real *, integer *, real *, 
-           integer *);
+           real *, integer *);
+    integer lastv;
+    extern /* Subroutine */ int scopy_(integer *, real *, integer *, real *, 
+           integer *), strmm_(char *, char *, char *, char *, integer *, 
+           integer *, real *, real *, integer *, real *, integer *);
+    extern integer ilaslc_(integer *, integer *, real *, integer *), ilaslr_(
+           integer *, integer *, real *, integer *);
     char transt[1];
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
@@ -160,58 +177,64 @@ static real c_b25 = -1.f;
 /*              Form  H * C  or  H' * C  where  C = ( C1 ) */
 /*                                                  ( C2 ) */
 
+/* Computing MAX */
+               i__1 = *k, i__2 = ilaslr_(m, k, &v[v_offset], ldv);
+               lastv = max(i__1,i__2);
+               lastc = ilaslc_(&lastv, n, &c__[c_offset], ldc);
+
 /*              W := C' * V  =  (C1'*V1 + C2'*V2)  (stored in WORK) */
 
 /*              W := C1' */
 
                i__1 = *k;
                for (j = 1; j <= i__1; ++j) {
-                   scopy_(n, &c__[j + c_dim1], ldc, &work[j * work_dim1 + 1], 
-                            &c__1);
+                   scopy_(&lastc, &c__[j + c_dim1], ldc, &work[j * work_dim1 
+                           + 1], &c__1);
 /* L10: */
                }
 
 /*              W := W * V1 */
 
-               strmm_("Right", "Lower", "No transpose", "Unit", n, k, &c_b14, 
-                        &v[v_offset], ldv, &work[work_offset], ldwork);
-               if (*m > *k) {
+               strmm_("Right", "Lower", "No transpose", "Unit", &lastc, k, &
+                       c_b14, &v[v_offset], ldv, &work[work_offset], ldwork);
+               if (lastv > *k) {
 
 /*                 W := W + C2'*V2 */
 
-                   i__1 = *m - *k;
-                   sgemm_("Transpose", "No transpose", n, k, &i__1, &c_b14, &
-                           c__[*k + 1 + c_dim1], ldc, &v[*k + 1 + v_dim1], 
-                           ldv, &c_b14, &work[work_offset], ldwork);
+                   i__1 = lastv - *k;
+                   sgemm_("Transpose", "No transpose", &lastc, k, &i__1, &
+                           c_b14, &c__[*k + 1 + c_dim1], ldc, &v[*k + 1 + 
+                           v_dim1], ldv, &c_b14, &work[work_offset], ldwork);
                }
 
 /*              W := W * T'  or  W * T */
 
-               strmm_("Right", "Upper", transt, "Non-unit", n, k, &c_b14, &t[
-                       t_offset], ldt, &work[work_offset], ldwork);
+               strmm_("Right", "Upper", transt, "Non-unit", &lastc, k, &
+                       c_b14, &t[t_offset], ldt, &work[work_offset], ldwork);
 
 /*              C := C - V * W' */
 
-               if (*m > *k) {
+               if (lastv > *k) {
 
 /*                 C2 := C2 - V2 * W' */
 
-                   i__1 = *m - *k;
-                   sgemm_("No transpose", "Transpose", &i__1, n, k, &c_b25, &
-                           v[*k + 1 + v_dim1], ldv, &work[work_offset], 
-                           ldwork, &c_b14, &c__[*k + 1 + c_dim1], ldc);
+                   i__1 = lastv - *k;
+                   sgemm_("No transpose", "Transpose", &i__1, &lastc, k, &
+                           c_b25, &v[*k + 1 + v_dim1], ldv, &work[
+                           work_offset], ldwork, &c_b14, &c__[*k + 1 + 
+                           c_dim1], ldc);
                }
 
 /*              W := W * V1' */
 
-               strmm_("Right", "Lower", "Transpose", "Unit", n, k, &c_b14, &
-                       v[v_offset], ldv, &work[work_offset], ldwork);
+               strmm_("Right", "Lower", "Transpose", "Unit", &lastc, k, &
+                       c_b14, &v[v_offset], ldv, &work[work_offset], ldwork);
 
 /*              C1 := C1 - W' */
 
                i__1 = *k;
                for (j = 1; j <= i__1; ++j) {
-                   i__2 = *n;
+                   i__2 = lastc;
                    for (i__ = 1; i__ <= i__2; ++i__) {
                        c__[j + i__ * c_dim1] -= work[i__ + j * work_dim1];
 /* L20: */
@@ -223,27 +246,32 @@ static real c_b25 = -1.f;
 
 /*              Form  C * H  or  C * H'  where  C = ( C1  C2 ) */
 
+/* Computing MAX */
+               i__1 = *k, i__2 = ilaslr_(n, k, &v[v_offset], ldv);
+               lastv = max(i__1,i__2);
+               lastc = ilaslr_(m, &lastv, &c__[c_offset], ldc);
+
 /*              W := C * V  =  (C1*V1 + C2*V2)  (stored in WORK) */
 
 /*              W := C1 */
 
                i__1 = *k;
                for (j = 1; j <= i__1; ++j) {
-                   scopy_(m, &c__[j * c_dim1 + 1], &c__1, &work[j * 
+                   scopy_(&lastc, &c__[j * c_dim1 + 1], &c__1, &work[j * 
                            work_dim1 + 1], &c__1);
 /* L40: */
                }
 
 /*              W := W * V1 */
 
-               strmm_("Right", "Lower", "No transpose", "Unit", m, k, &c_b14, 
-                        &v[v_offset], ldv, &work[work_offset], ldwork);
-               if (*n > *k) {
+               strmm_("Right", "Lower", "No transpose", "Unit", &lastc, k, &
+                       c_b14, &v[v_offset], ldv, &work[work_offset], ldwork);
+               if (lastv > *k) {
 
 /*                 W := W + C2 * V2 */
 
-                   i__1 = *n - *k;
-                   sgemm_("No transpose", "No transpose", m, k, &i__1, &
+                   i__1 = lastv - *k;
+                   sgemm_("No transpose", "No transpose", &lastc, k, &i__1, &
                            c_b14, &c__[(*k + 1) * c_dim1 + 1], ldc, &v[*k + 
                            1 + v_dim1], ldv, &c_b14, &work[work_offset], 
                            ldwork);
@@ -251,31 +279,32 @@ static real c_b25 = -1.f;
 
 /*              W := W * T  or  W * T' */
 
-               strmm_("Right", "Upper", trans, "Non-unit", m, k, &c_b14, &t[
-                       t_offset], ldt, &work[work_offset], ldwork);
+               strmm_("Right", "Upper", trans, "Non-unit", &lastc, k, &c_b14, 
+                        &t[t_offset], ldt, &work[work_offset], ldwork);
 
 /*              C := C - W * V' */
 
-               if (*n > *k) {
+               if (lastv > *k) {
 
 /*                 C2 := C2 - W * V2' */
 
-                   i__1 = *n - *k;
-                   sgemm_("No transpose", "Transpose", m, &i__1, k, &c_b25, &
-                           work[work_offset], ldwork, &v[*k + 1 + v_dim1], 
-                           ldv, &c_b14, &c__[(*k + 1) * c_dim1 + 1], ldc);
+                   i__1 = lastv - *k;
+                   sgemm_("No transpose", "Transpose", &lastc, &i__1, k, &
+                           c_b25, &work[work_offset], ldwork, &v[*k + 1 + 
+                           v_dim1], ldv, &c_b14, &c__[(*k + 1) * c_dim1 + 1], 
+                            ldc);
                }
 
 /*              W := W * V1' */
 
-               strmm_("Right", "Lower", "Transpose", "Unit", m, k, &c_b14, &
-                       v[v_offset], ldv, &work[work_offset], ldwork);
+               strmm_("Right", "Lower", "Transpose", "Unit", &lastc, k, &
+                       c_b14, &v[v_offset], ldv, &work[work_offset], ldwork);
 
 /*              C1 := C1 - W */
 
                i__1 = *k;
                for (j = 1; j <= i__1; ++j) {
-                   i__2 = *m;
+                   i__2 = lastc;
                    for (i__ = 1; i__ <= i__2; ++i__) {
                        c__[i__ + j * c_dim1] -= work[i__ + j * work_dim1];
 /* L50: */
@@ -295,63 +324,67 @@ static real c_b25 = -1.f;
 /*              Form  H * C  or  H' * C  where  C = ( C1 ) */
 /*                                                  ( C2 ) */
 
+/* Computing MAX */
+               i__1 = *k, i__2 = ilaslr_(m, k, &v[v_offset], ldv);
+               lastv = max(i__1,i__2);
+               lastc = ilaslc_(&lastv, n, &c__[c_offset], ldc);
+
 /*              W := C' * V  =  (C1'*V1 + C2'*V2)  (stored in WORK) */
 
 /*              W := C2' */
 
                i__1 = *k;
                for (j = 1; j <= i__1; ++j) {
-                   scopy_(n, &c__[*m - *k + j + c_dim1], ldc, &work[j * 
-                           work_dim1 + 1], &c__1);
+                   scopy_(&lastc, &c__[lastv - *k + j + c_dim1], ldc, &work[
+                           j * work_dim1 + 1], &c__1);
 /* L70: */
                }
 
 /*              W := W * V2 */
 
-               strmm_("Right", "Upper", "No transpose", "Unit", n, k, &c_b14, 
-                        &v[*m - *k + 1 + v_dim1], ldv, &work[work_offset], 
-                       ldwork);
-               if (*m > *k) {
+               strmm_("Right", "Upper", "No transpose", "Unit", &lastc, k, &
+                       c_b14, &v[lastv - *k + 1 + v_dim1], ldv, &work[
+                       work_offset], ldwork);
+               if (lastv > *k) {
 
 /*                 W := W + C1'*V1 */
 
-                   i__1 = *m - *k;
-                   sgemm_("Transpose", "No transpose", n, k, &i__1, &c_b14, &
-                           c__[c_offset], ldc, &v[v_offset], ldv, &c_b14, &
-                           work[work_offset], ldwork);
+                   i__1 = lastv - *k;
+                   sgemm_("Transpose", "No transpose", &lastc, k, &i__1, &
+                           c_b14, &c__[c_offset], ldc, &v[v_offset], ldv, &
+                           c_b14, &work[work_offset], ldwork);
                }
 
 /*              W := W * T'  or  W * T */
 
-               strmm_("Right", "Lower", transt, "Non-unit", n, k, &c_b14, &t[
-                       t_offset], ldt, &work[work_offset], ldwork);
+               strmm_("Right", "Lower", transt, "Non-unit", &lastc, k, &
+                       c_b14, &t[t_offset], ldt, &work[work_offset], ldwork);
 
 /*              C := C - V * W' */
 
-               if (*m > *k) {
+               if (lastv > *k) {
 
 /*                 C1 := C1 - V1 * W' */
 
-                   i__1 = *m - *k;
-                   sgemm_("No transpose", "Transpose", &i__1, n, k, &c_b25, &
-                           v[v_offset], ldv, &work[work_offset], ldwork, &
-                           c_b14, &c__[c_offset], ldc)
-                           ;
+                   i__1 = lastv - *k;
+                   sgemm_("No transpose", "Transpose", &i__1, &lastc, k, &
+                           c_b25, &v[v_offset], ldv, &work[work_offset], 
+                           ldwork, &c_b14, &c__[c_offset], ldc);
                }
 
 /*              W := W * V2' */
 
-               strmm_("Right", "Upper", "Transpose", "Unit", n, k, &c_b14, &
-                       v[*m - *k + 1 + v_dim1], ldv, &work[work_offset], 
-                       ldwork);
+               strmm_("Right", "Upper", "Transpose", "Unit", &lastc, k, &
+                       c_b14, &v[lastv - *k + 1 + v_dim1], ldv, &work[
+                       work_offset], ldwork);
 
 /*              C2 := C2 - W' */
 
                i__1 = *k;
                for (j = 1; j <= i__1; ++j) {
-                   i__2 = *n;
+                   i__2 = lastc;
                    for (i__ = 1; i__ <= i__2; ++i__) {
-                       c__[*m - *k + j + i__ * c_dim1] -= work[i__ + j * 
+                       c__[lastv - *k + j + i__ * c_dim1] -= work[i__ + j * 
                                work_dim1];
 /* L80: */
                    }
@@ -362,64 +395,68 @@ static real c_b25 = -1.f;
 
 /*              Form  C * H  or  C * H'  where  C = ( C1  C2 ) */
 
+/* Computing MAX */
+               i__1 = *k, i__2 = ilaslr_(n, k, &v[v_offset], ldv);
+               lastv = max(i__1,i__2);
+               lastc = ilaslr_(m, &lastv, &c__[c_offset], ldc);
+
 /*              W := C * V  =  (C1*V1 + C2*V2)  (stored in WORK) */
 
 /*              W := C2 */
 
                i__1 = *k;
                for (j = 1; j <= i__1; ++j) {
-                   scopy_(m, &c__[(*n - *k + j) * c_dim1 + 1], &c__1, &work[
-                           j * work_dim1 + 1], &c__1);
+                   scopy_(&lastc, &c__[(*n - *k + j) * c_dim1 + 1], &c__1, &
+                           work[j * work_dim1 + 1], &c__1);
 /* L100: */
                }
 
 /*              W := W * V2 */
 
-               strmm_("Right", "Upper", "No transpose", "Unit", m, k, &c_b14, 
-                        &v[*n - *k + 1 + v_dim1], ldv, &work[work_offset], 
-                       ldwork);
-               if (*n > *k) {
+               strmm_("Right", "Upper", "No transpose", "Unit", &lastc, k, &
+                       c_b14, &v[lastv - *k + 1 + v_dim1], ldv, &work[
+                       work_offset], ldwork);
+               if (lastv > *k) {
 
 /*                 W := W + C1 * V1 */
 
-                   i__1 = *n - *k;
-                   sgemm_("No transpose", "No transpose", m, k, &i__1, &
+                   i__1 = lastv - *k;
+                   sgemm_("No transpose", "No transpose", &lastc, k, &i__1, &
                            c_b14, &c__[c_offset], ldc, &v[v_offset], ldv, &
                            c_b14, &work[work_offset], ldwork);
                }
 
 /*              W := W * T  or  W * T' */
 
-               strmm_("Right", "Lower", trans, "Non-unit", m, k, &c_b14, &t[
-                       t_offset], ldt, &work[work_offset], ldwork);
+               strmm_("Right", "Lower", trans, "Non-unit", &lastc, k, &c_b14, 
+                        &t[t_offset], ldt, &work[work_offset], ldwork);
 
 /*              C := C - W * V' */
 
-               if (*n > *k) {
+               if (lastv > *k) {
 
 /*                 C1 := C1 - W * V1' */
 
-                   i__1 = *n - *k;
-                   sgemm_("No transpose", "Transpose", m, &i__1, k, &c_b25, &
-                           work[work_offset], ldwork, &v[v_offset], ldv, &
-                           c_b14, &c__[c_offset], ldc)
-                           ;
+                   i__1 = lastv - *k;
+                   sgemm_("No transpose", "Transpose", &lastc, &i__1, k, &
+                           c_b25, &work[work_offset], ldwork, &v[v_offset], 
+                           ldv, &c_b14, &c__[c_offset], ldc);
                }
 
 /*              W := W * V2' */
 
-               strmm_("Right", "Upper", "Transpose", "Unit", m, k, &c_b14, &
-                       v[*n - *k + 1 + v_dim1], ldv, &work[work_offset], 
-                       ldwork);
+               strmm_("Right", "Upper", "Transpose", "Unit", &lastc, k, &
+                       c_b14, &v[lastv - *k + 1 + v_dim1], ldv, &work[
+                       work_offset], ldwork);
 
 /*              C2 := C2 - W */
 
                i__1 = *k;
                for (j = 1; j <= i__1; ++j) {
-                   i__2 = *m;
+                   i__2 = lastc;
                    for (i__ = 1; i__ <= i__2; ++i__) {
-                       c__[i__ + (*n - *k + j) * c_dim1] -= work[i__ + j * 
-                               work_dim1];
+                       c__[i__ + (lastv - *k + j) * c_dim1] -= work[i__ + j *
+                                work_dim1];
 /* L110: */
                    }
 /* L120: */
@@ -439,58 +476,64 @@ static real c_b25 = -1.f;
 /*              Form  H * C  or  H' * C  where  C = ( C1 ) */
 /*                                                  ( C2 ) */
 
+/* Computing MAX */
+               i__1 = *k, i__2 = ilaslc_(k, m, &v[v_offset], ldv);
+               lastv = max(i__1,i__2);
+               lastc = ilaslc_(&lastv, n, &c__[c_offset], ldc);
+
 /*              W := C' * V'  =  (C1'*V1' + C2'*V2') (stored in WORK) */
 
 /*              W := C1' */
 
                i__1 = *k;
                for (j = 1; j <= i__1; ++j) {
-                   scopy_(n, &c__[j + c_dim1], ldc, &work[j * work_dim1 + 1], 
-                            &c__1);
+                   scopy_(&lastc, &c__[j + c_dim1], ldc, &work[j * work_dim1 
+                           + 1], &c__1);
 /* L130: */
                }
 
 /*              W := W * V1' */
 
-               strmm_("Right", "Upper", "Transpose", "Unit", n, k, &c_b14, &
-                       v[v_offset], ldv, &work[work_offset], ldwork);
-               if (*m > *k) {
+               strmm_("Right", "Upper", "Transpose", "Unit", &lastc, k, &
+                       c_b14, &v[v_offset], ldv, &work[work_offset], ldwork);
+               if (lastv > *k) {
 
 /*                 W := W + C2'*V2' */
 
-                   i__1 = *m - *k;
-                   sgemm_("Transpose", "Transpose", n, k, &i__1, &c_b14, &
-                           c__[*k + 1 + c_dim1], ldc, &v[(*k + 1) * v_dim1 + 
-                           1], ldv, &c_b14, &work[work_offset], ldwork);
+                   i__1 = lastv - *k;
+                   sgemm_("Transpose", "Transpose", &lastc, k, &i__1, &c_b14, 
+                            &c__[*k + 1 + c_dim1], ldc, &v[(*k + 1) * v_dim1 
+                           1], ldv, &c_b14, &work[work_offset], ldwork);
                }
 
 /*              W := W * T'  or  W * T */
 
-               strmm_("Right", "Upper", transt, "Non-unit", n, k, &c_b14, &t[
-                       t_offset], ldt, &work[work_offset], ldwork);
+               strmm_("Right", "Upper", transt, "Non-unit", &lastc, k, &
+                       c_b14, &t[t_offset], ldt, &work[work_offset], ldwork);
 
 /*              C := C - V' * W' */
 
-               if (*m > *k) {
+               if (lastv > *k) {
 
 /*                 C2 := C2 - V2' * W' */
 
-                   i__1 = *m - *k;
-                   sgemm_("Transpose", "Transpose", &i__1, n, k, &c_b25, &v[(
-                           *k + 1) * v_dim1 + 1], ldv, &work[work_offset], 
-                           ldwork, &c_b14, &c__[*k + 1 + c_dim1], ldc);
+                   i__1 = lastv - *k;
+                   sgemm_("Transpose", "Transpose", &i__1, &lastc, k, &c_b25, 
+                            &v[(*k + 1) * v_dim1 + 1], ldv, &work[
+                           work_offset], ldwork, &c_b14, &c__[*k + 1 + 
+                           c_dim1], ldc);
                }
 
 /*              W := W * V1 */
 
-               strmm_("Right", "Upper", "No transpose", "Unit", n, k, &c_b14, 
-                        &v[v_offset], ldv, &work[work_offset], ldwork);
+               strmm_("Right", "Upper", "No transpose", "Unit", &lastc, k, &
+                       c_b14, &v[v_offset], ldv, &work[work_offset], ldwork);
 
 /*              C1 := C1 - W' */
 
                i__1 = *k;
                for (j = 1; j <= i__1; ++j) {
-                   i__2 = *n;
+                   i__2 = lastc;
                    for (i__ = 1; i__ <= i__2; ++i__) {
                        c__[j + i__ * c_dim1] -= work[i__ + j * work_dim1];
 /* L140: */
@@ -502,45 +545,50 @@ static real c_b25 = -1.f;
 
 /*              Form  C * H  or  C * H'  where  C = ( C1  C2 ) */
 
+/* Computing MAX */
+               i__1 = *k, i__2 = ilaslc_(k, n, &v[v_offset], ldv);
+               lastv = max(i__1,i__2);
+               lastc = ilaslr_(m, &lastv, &c__[c_offset], ldc);
+
 /*              W := C * V'  =  (C1*V1' + C2*V2')  (stored in WORK) */
 
 /*              W := C1 */
 
                i__1 = *k;
                for (j = 1; j <= i__1; ++j) {
-                   scopy_(m, &c__[j * c_dim1 + 1], &c__1, &work[j * 
+                   scopy_(&lastc, &c__[j * c_dim1 + 1], &c__1, &work[j * 
                            work_dim1 + 1], &c__1);
 /* L160: */
                }
 
 /*              W := W * V1' */
 
-               strmm_("Right", "Upper", "Transpose", "Unit", m, k, &c_b14, &
-                       v[v_offset], ldv, &work[work_offset], ldwork);
-               if (*n > *k) {
+               strmm_("Right", "Upper", "Transpose", "Unit", &lastc, k, &
+                       c_b14, &v[v_offset], ldv, &work[work_offset], ldwork);
+               if (lastv > *k) {
 
 /*                 W := W + C2 * V2' */
 
-                   i__1 = *n - *k;
-                   sgemm_("No transpose", "Transpose", m, k, &i__1, &c_b14, &
-                           c__[(*k + 1) * c_dim1 + 1], ldc, &v[(*k + 1) * 
-                           v_dim1 + 1], ldv, &c_b14, &work[work_offset], 
-                           ldwork);
+                   i__1 = lastv - *k;
+                   sgemm_("No transpose", "Transpose", &lastc, k, &i__1, &
+                           c_b14, &c__[(*k + 1) * c_dim1 + 1], ldc, &v[(*k + 
+                           1) * v_dim1 + 1], ldv, &c_b14, &work[work_offset], 
+                            ldwork);
                }
 
 /*              W := W * T  or  W * T' */
 
-               strmm_("Right", "Upper", trans, "Non-unit", m, k, &c_b14, &t[
-                       t_offset], ldt, &work[work_offset], ldwork);
+               strmm_("Right", "Upper", trans, "Non-unit", &lastc, k, &c_b14, 
+                        &t[t_offset], ldt, &work[work_offset], ldwork);
 
 /*              C := C - W * V */
 
-               if (*n > *k) {
+               if (lastv > *k) {
 
 /*                 C2 := C2 - W * V2 */
 
-                   i__1 = *n - *k;
-                   sgemm_("No transpose", "No transpose", m, &i__1, k, &
+                   i__1 = lastv - *k;
+                   sgemm_("No transpose", "No transpose", &lastc, &i__1, k, &
                            c_b25, &work[work_offset], ldwork, &v[(*k + 1) * 
                            v_dim1 + 1], ldv, &c_b14, &c__[(*k + 1) * c_dim1 
                            + 1], ldc);
@@ -548,14 +596,14 @@ static real c_b25 = -1.f;
 
 /*              W := W * V1 */
 
-               strmm_("Right", "Upper", "No transpose", "Unit", m, k, &c_b14, 
-                        &v[v_offset], ldv, &work[work_offset], ldwork);
+               strmm_("Right", "Upper", "No transpose", "Unit", &lastc, k, &
+                       c_b14, &v[v_offset], ldv, &work[work_offset], ldwork);
 
 /*              C1 := C1 - W */
 
                i__1 = *k;
                for (j = 1; j <= i__1; ++j) {
-                   i__2 = *m;
+                   i__2 = lastc;
                    for (i__ = 1; i__ <= i__2; ++i__) {
                        c__[i__ + j * c_dim1] -= work[i__ + j * work_dim1];
 /* L170: */
@@ -575,62 +623,67 @@ static real c_b25 = -1.f;
 /*              Form  H * C  or  H' * C  where  C = ( C1 ) */
 /*                                                  ( C2 ) */
 
+/* Computing MAX */
+               i__1 = *k, i__2 = ilaslc_(k, m, &v[v_offset], ldv);
+               lastv = max(i__1,i__2);
+               lastc = ilaslc_(&lastv, n, &c__[c_offset], ldc);
+
 /*              W := C' * V'  =  (C1'*V1' + C2'*V2') (stored in WORK) */
 
 /*              W := C2' */
 
                i__1 = *k;
                for (j = 1; j <= i__1; ++j) {
-                   scopy_(n, &c__[*m - *k + j + c_dim1], ldc, &work[j * 
-                           work_dim1 + 1], &c__1);
+                   scopy_(&lastc, &c__[lastv - *k + j + c_dim1], ldc, &work[
+                           j * work_dim1 + 1], &c__1);
 /* L190: */
                }
 
 /*              W := W * V2' */
 
-               strmm_("Right", "Lower", "Transpose", "Unit", n, k, &c_b14, &
-                       v[(*m - *k + 1) * v_dim1 + 1], ldv, &work[work_offset]
-, ldwork);
-               if (*m > *k) {
+               strmm_("Right", "Lower", "Transpose", "Unit", &lastc, k, &
+                       c_b14, &v[(lastv - *k + 1) * v_dim1 + 1], ldv, &work[
+                       work_offset], ldwork);
+               if (lastv > *k) {
 
 /*                 W := W + C1'*V1' */
 
-                   i__1 = *m - *k;
-                   sgemm_("Transpose", "Transpose", n, k, &i__1, &c_b14, &
-                           c__[c_offset], ldc, &v[v_offset], ldv, &c_b14, &
+                   i__1 = lastv - *k;
+                   sgemm_("Transpose", "Transpose", &lastc, k, &i__1, &c_b14, 
+                            &c__[c_offset], ldc, &v[v_offset], ldv, &c_b14, &
                            work[work_offset], ldwork);
                }
 
 /*              W := W * T'  or  W * T */
 
-               strmm_("Right", "Lower", transt, "Non-unit", n, k, &c_b14, &t[
-                       t_offset], ldt, &work[work_offset], ldwork);
+               strmm_("Right", "Lower", transt, "Non-unit", &lastc, k, &
+                       c_b14, &t[t_offset], ldt, &work[work_offset], ldwork);
 
 /*              C := C - V' * W' */
 
-               if (*m > *k) {
+               if (lastv > *k) {
 
 /*                 C1 := C1 - V1' * W' */
 
-                   i__1 = *m - *k;
-                   sgemm_("Transpose", "Transpose", &i__1, n, k, &c_b25, &v[
-                           v_offset], ldv, &work[work_offset], ldwork, &
+                   i__1 = lastv - *k;
+                   sgemm_("Transpose", "Transpose", &i__1, &lastc, k, &c_b25, 
+                            &v[v_offset], ldv, &work[work_offset], ldwork, &
                            c_b14, &c__[c_offset], ldc);
                }
 
 /*              W := W * V2 */
 
-               strmm_("Right", "Lower", "No transpose", "Unit", n, k, &c_b14, 
-                        &v[(*m - *k + 1) * v_dim1 + 1], ldv, &work[
+               strmm_("Right", "Lower", "No transpose", "Unit", &lastc, k, &
+                       c_b14, &v[(lastv - *k + 1) * v_dim1 + 1], ldv, &work[
                        work_offset], ldwork);
 
 /*              C2 := C2 - W' */
 
                i__1 = *k;
                for (j = 1; j <= i__1; ++j) {
-                   i__2 = *n;
+                   i__2 = lastc;
                    for (i__ = 1; i__ <= i__2; ++i__) {
-                       c__[*m - *k + j + i__ * c_dim1] -= work[i__ + j * 
+                       c__[lastv - *k + j + i__ * c_dim1] -= work[i__ + j * 
                                work_dim1];
 /* L200: */
                    }
@@ -641,63 +694,68 @@ static real c_b25 = -1.f;
 
 /*              Form  C * H  or  C * H'  where  C = ( C1  C2 ) */
 
+/* Computing MAX */
+               i__1 = *k, i__2 = ilaslc_(k, n, &v[v_offset], ldv);
+               lastv = max(i__1,i__2);
+               lastc = ilaslr_(m, &lastv, &c__[c_offset], ldc);
+
 /*              W := C * V'  =  (C1*V1' + C2*V2')  (stored in WORK) */
 
 /*              W := C2 */
 
                i__1 = *k;
                for (j = 1; j <= i__1; ++j) {
-                   scopy_(m, &c__[(*n - *k + j) * c_dim1 + 1], &c__1, &work[
-                           j * work_dim1 + 1], &c__1);
+                   scopy_(&lastc, &c__[(lastv - *k + j) * c_dim1 + 1], &c__1, 
+                            &work[j * work_dim1 + 1], &c__1);
 /* L220: */
                }
 
 /*              W := W * V2' */
 
-               strmm_("Right", "Lower", "Transpose", "Unit", m, k, &c_b14, &
-                       v[(*n - *k + 1) * v_dim1 + 1], ldv, &work[work_offset]
-, ldwork);
-               if (*n > *k) {
+               strmm_("Right", "Lower", "Transpose", "Unit", &lastc, k, &
+                       c_b14, &v[(lastv - *k + 1) * v_dim1 + 1], ldv, &work[
+                       work_offset], ldwork);
+               if (lastv > *k) {
 
 /*                 W := W + C1 * V1' */
 
-                   i__1 = *n - *k;
-                   sgemm_("No transpose", "Transpose", m, k, &i__1, &c_b14, &
-                           c__[c_offset], ldc, &v[v_offset], ldv, &c_b14, &
-                           work[work_offset], ldwork);
+                   i__1 = lastv - *k;
+                   sgemm_("No transpose", "Transpose", &lastc, k, &i__1, &
+                           c_b14, &c__[c_offset], ldc, &v[v_offset], ldv, &
+                           c_b14, &work[work_offset], ldwork);
                }
 
 /*              W := W * T  or  W * T' */
 
-               strmm_("Right", "Lower", trans, "Non-unit", m, k, &c_b14, &t[
-                       t_offset], ldt, &work[work_offset], ldwork);
+               strmm_("Right", "Lower", trans, "Non-unit", &lastc, k, &c_b14, 
+                        &t[t_offset], ldt, &work[work_offset], ldwork);
 
 /*              C := C - W * V */
 
-               if (*n > *k) {
+               if (lastv > *k) {
 
 /*                 C1 := C1 - W * V1 */
 
-                   i__1 = *n - *k;
-                   sgemm_("No transpose", "No transpose", m, &i__1, k, &
+                   i__1 = lastv - *k;
+                   sgemm_("No transpose", "No transpose", &lastc, &i__1, k, &
                            c_b25, &work[work_offset], ldwork, &v[v_offset], 
                            ldv, &c_b14, &c__[c_offset], ldc);
                }
 
 /*              W := W * V2 */
 
-               strmm_("Right", "Lower", "No transpose", "Unit", m, k, &c_b14, 
-                        &v[(*n - *k + 1) * v_dim1 + 1], ldv, &work[
+               strmm_("Right", "Lower", "No transpose", "Unit", &lastc, k, &
+                       c_b14, &v[(lastv - *k + 1) * v_dim1 + 1], ldv, &work[
                        work_offset], ldwork);
 
 /*              C1 := C1 - W */
 
                i__1 = *k;
                for (j = 1; j <= i__1; ++j) {
-                   i__2 = *m;
+                   i__2 = lastc;
                    for (i__ = 1; i__ <= i__2; ++i__) {
-                       c__[i__ + (*n - *k + j) * c_dim1] -= work[i__ + j * 
-                               work_dim1];
+                       c__[i__ + (lastv - *k + j) * c_dim1] -= work[i__ + j *
+                                work_dim1];
 /* L230: */
                    }
 /* L240: */
index bd99e17..7f75262 100644 (file)
@@ -1,5 +1,18 @@
+/* slarfg.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int slarfg_(integer *n, real *alpha, real *x, integer *incx, 
        real *tau)
 {
@@ -20,7 +33,7 @@
     real safmin, rsafmn;
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
        r__1 = slapy2_(alpha, &xnorm);
        beta = -r_sign(&r__1, alpha);
        safmin = slamch_("S") / slamch_("E");
+       knt = 0;
        if (dabs(beta) < safmin) {
 
 /*           XNORM, BETA may be inaccurate; scale X and recompute them */
 
            rsafmn = 1.f / safmin;
-           knt = 0;
 L10:
            ++knt;
            i__1 = *n - 1;
@@ -133,26 +146,20 @@ L10:
            xnorm = snrm2_(&i__1, &x[1], incx);
            r__1 = slapy2_(alpha, &xnorm);
            beta = -r_sign(&r__1, alpha);
-           *tau = (beta - *alpha) / beta;
-           i__1 = *n - 1;
-           r__1 = 1.f / (*alpha - beta);
-           sscal_(&i__1, &r__1, &x[1], incx);
+       }
+       *tau = (beta - *alpha) / beta;
+       i__1 = *n - 1;
+       r__1 = 1.f / (*alpha - beta);
+       sscal_(&i__1, &r__1, &x[1], incx);
 
-/*           If ALPHA is subnormal, it may lose relative accuracy */
+/*        If ALPHA is subnormal, it may lose relative accuracy */
 
-           *alpha = beta;
-           i__1 = knt;
-           for (j = 1; j <= i__1; ++j) {
-               *alpha *= safmin;
+       i__1 = knt;
+       for (j = 1; j <= i__1; ++j) {
+           beta *= safmin;
 /* L20: */
-           }
-       } else {
-           *tau = (beta - *alpha) / beta;
-           i__1 = *n - 1;
-           r__1 = 1.f / (*alpha - beta);
-           sscal_(&i__1, &r__1, &x[1], incx);
-           *alpha = beta;
        }
+       *alpha = beta;
     }
 
     return 0;
diff --git a/3rdparty/lapack/slarfp.c b/3rdparty/lapack/slarfp.c
new file mode 100644 (file)
index 0000000..bbe9d4f
--- /dev/null
@@ -0,0 +1,191 @@
+/* slarfp.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
+#include "clapack.h"
+
+
+/* Subroutine */ int slarfp_(integer *n, real *alpha, real *x, integer *incx, 
+       real *tau)
+{
+    /* System generated locals */
+    integer i__1;
+    real r__1;
+
+    /* Builtin functions */
+    double r_sign(real *, real *);
+
+    /* Local variables */
+    integer j, knt;
+    real beta;
+    extern doublereal snrm2_(integer *, real *, integer *);
+    extern /* Subroutine */ int sscal_(integer *, real *, real *, integer *);
+    real xnorm;
+    extern doublereal slapy2_(real *, real *), slamch_(char *);
+    real safmin, rsafmn;
+
+
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
+/*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
+/*     November 2006 */
+
+/*     .. Scalar Arguments .. */
+/*     .. */
+/*     .. Array Arguments .. */
+/*     .. */
+
+/*  Purpose */
+/*  ======= */
+
+/*  SLARFP generates a real elementary reflector H of order n, such */
+/*  that */
+
+/*        H * ( alpha ) = ( beta ),   H' * H = I. */
+/*            (   x   )   (   0  ) */
+
+/*  where alpha and beta are scalars, beta is non-negative, and x is */
+/*  an (n-1)-element real vector.  H is represented in the form */
+
+/*        H = I - tau * ( 1 ) * ( 1 v' ) , */
+/*                      ( v ) */
+
+/*  where tau is a real scalar and v is a real (n-1)-element */
+/*  vector. */
+
+/*  If the elements of x are all zero, then tau = 0 and H is taken to be */
+/*  the unit matrix. */
+
+/*  Otherwise  1 <= tau <= 2. */
+
+/*  Arguments */
+/*  ========= */
+
+/*  N       (input) INTEGER */
+/*          The order of the elementary reflector. */
+
+/*  ALPHA   (input/output) REAL */
+/*          On entry, the value alpha. */
+/*          On exit, it is overwritten with the value beta. */
+
+/*  X       (input/output) REAL array, dimension */
+/*                         (1+(N-2)*abs(INCX)) */
+/*          On entry, the vector x. */
+/*          On exit, it is overwritten with the vector v. */
+
+/*  INCX    (input) INTEGER */
+/*          The increment between elements of X. INCX > 0. */
+
+/*  TAU     (output) REAL */
+/*          The value tau. */
+
+/*  ===================================================================== */
+
+/*     .. Parameters .. */
+/*     .. */
+/*     .. Local Scalars .. */
+/*     .. */
+/*     .. External Functions .. */
+/*     .. */
+/*     .. Intrinsic Functions .. */
+/*     .. */
+/*     .. External Subroutines .. */
+/*     .. */
+/*     .. Executable Statements .. */
+
+    /* Parameter adjustments */
+    --x;
+
+    /* Function Body */
+    if (*n <= 0) {
+       *tau = 0.f;
+       return 0;
+    }
+
+    i__1 = *n - 1;
+    xnorm = snrm2_(&i__1, &x[1], incx);
+
+    if (xnorm == 0.f) {
+
+/*        H  =  [+/-1, 0; I], sign chosen so ALPHA >= 0. */
+
+       if (*alpha >= 0.f) {
+/*           When TAU.eq.ZERO, the vector is special-cased to be */
+/*           all zeros in the application routines.  We do not need */
+/*           to clear it. */
+           *tau = 0.f;
+       } else {
+/*           However, the application routines rely on explicit */
+/*           zero checks when TAU.ne.ZERO, and we must clear X. */
+           *tau = 2.f;
+           i__1 = *n - 1;
+           for (j = 1; j <= i__1; ++j) {
+               x[(j - 1) * *incx + 1] = 0.f;
+           }
+           *alpha = -(*alpha);
+       }
+    } else {
+
+/*        general case */
+
+       r__1 = slapy2_(alpha, &xnorm);
+       beta = r_sign(&r__1, alpha);
+       safmin = slamch_("S") / slamch_("E");
+       knt = 0;
+       if (dabs(beta) < safmin) {
+
+/*           XNORM, BETA may be inaccurate; scale X and recompute them */
+
+           rsafmn = 1.f / safmin;
+L10:
+           ++knt;
+           i__1 = *n - 1;
+           sscal_(&i__1, &rsafmn, &x[1], incx);
+           beta *= rsafmn;
+           *alpha *= rsafmn;
+           if (dabs(beta) < safmin) {
+               goto L10;
+           }
+
+/*           New BETA is at most 1, at least SAFMIN */
+
+           i__1 = *n - 1;
+           xnorm = snrm2_(&i__1, &x[1], incx);
+           r__1 = slapy2_(alpha, &xnorm);
+           beta = r_sign(&r__1, alpha);
+       }
+       *alpha += beta;
+       if (beta < 0.f) {
+           beta = -beta;
+           *tau = -(*alpha) / beta;
+       } else {
+           *alpha = xnorm * (xnorm / *alpha);
+           *tau = *alpha / beta;
+           *alpha = -(*alpha);
+       }
+       i__1 = *n - 1;
+       r__1 = 1.f / *alpha;
+       sscal_(&i__1, &r__1, &x[1], incx);
+
+/*        If BETA is subnormal, it may lose relative accuracy */
+
+       i__1 = knt;
+       for (j = 1; j <= i__1; ++j) {
+           beta *= safmin;
+/* L20: */
+       }
+       *alpha = beta;
+    }
+
+    return 0;
+
+/*     End of SLARFP */
+
+} /* slarfp_ */
index 5459f63..dcbb6a7 100644 (file)
@@ -1,5 +1,18 @@
+/* slarft.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -13,15 +26,17 @@ static real c_b8 = 0.f;
     real r__1;
 
     /* Local variables */
-    integer i__, j;
+    integer i__, j, prevlastv;
     real vii;
     extern logical lsame_(char *, char *);
     extern /* Subroutine */ int sgemv_(char *, integer *, integer *, real *, 
-           real *, integer *, real *, integer *, real *, real *, integer *), strmv_(char *, char *, char *, integer *, real *, 
-           integer *, real *, integer *);
+           real *, integer *, real *, integer *, real *, real *, integer *);
+    integer lastv;
+    extern /* Subroutine */ int strmv_(char *, char *, char *, integer *, 
+           real *, integer *, real *, integer *);
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
@@ -147,8 +162,10 @@ static real c_b8 = 0.f;
     }
 
     if (lsame_(direct, "F")) {
+       prevlastv = *n;
        i__1 = *k;
        for (i__ = 1; i__ <= i__1; ++i__) {
+           prevlastv = max(i__,prevlastv);
            if (tau[i__] == 0.f) {
 
 /*              H(i)  =  I */
@@ -165,21 +182,37 @@ static real c_b8 = 0.f;
                vii = v[i__ + i__ * v_dim1];
                v[i__ + i__ * v_dim1] = 1.f;
                if (lsame_(storev, "C")) {
+/*                 Skip any trailing zeros. */
+                   i__2 = i__ + 1;
+                   for (lastv = *n; lastv >= i__2; --lastv) {
+                       if (v[lastv + i__ * v_dim1] != 0.f) {
+                           break;
+                       }
+                   }
+                   j = min(lastv,prevlastv);
 
-/*                 T(1:i-1,i) := - tau(i) * V(i:n,1:i-1)' * V(i:n,i) */
+/*                 T(1:i-1,i) := - tau(i) * V(i:j,1:i-1)' * V(i:j,i) */
 
-                   i__2 = *n - i__ + 1;
+                   i__2 = j - i__ + 1;
                    i__3 = i__ - 1;
                    r__1 = -tau[i__];
                    sgemv_("Transpose", &i__2, &i__3, &r__1, &v[i__ + v_dim1], 
                             ldv, &v[i__ + i__ * v_dim1], &c__1, &c_b8, &t[
                            i__ * t_dim1 + 1], &c__1);
                } else {
+/*                 Skip any trailing zeros. */
+                   i__2 = i__ + 1;
+                   for (lastv = *n; lastv >= i__2; --lastv) {
+                       if (v[i__ + lastv * v_dim1] != 0.f) {
+                           break;
+                       }
+                   }
+                   j = min(lastv,prevlastv);
 
-/*                 T(1:i-1,i) := - tau(i) * V(1:i-1,i:n) * V(i,i:n)' */
+/*                 T(1:i-1,i) := - tau(i) * V(1:i-1,i:j) * V(i,i:j)' */
 
                    i__2 = i__ - 1;
-                   i__3 = *n - i__ + 1;
+                   i__3 = j - i__ + 1;
                    r__1 = -tau[i__];
                    sgemv_("No transpose", &i__2, &i__3, &r__1, &v[i__ * 
                            v_dim1 + 1], ldv, &v[i__ + i__ * v_dim1], ldv, &
@@ -193,10 +226,16 @@ static real c_b8 = 0.f;
                strmv_("Upper", "No transpose", "Non-unit", &i__2, &t[
                        t_offset], ldt, &t[i__ * t_dim1 + 1], &c__1);
                t[i__ + i__ * t_dim1] = tau[i__];
+               if (i__ > 1) {
+                   prevlastv = max(prevlastv,lastv);
+               } else {
+                   prevlastv = lastv;
+               }
            }
 /* L20: */
        }
     } else {
+       prevlastv = 1;
        for (i__ = *k; i__ >= 1; --i__) {
            if (tau[i__] == 0.f) {
 
@@ -215,31 +254,47 @@ static real c_b8 = 0.f;
                    if (lsame_(storev, "C")) {
                        vii = v[*n - *k + i__ + i__ * v_dim1];
                        v[*n - *k + i__ + i__ * v_dim1] = 1.f;
+/*                    Skip any leading zeros. */
+                       i__1 = i__ - 1;
+                       for (lastv = 1; lastv <= i__1; ++lastv) {
+                           if (v[lastv + i__ * v_dim1] != 0.f) {
+                               break;
+                           }
+                       }
+                       j = max(lastv,prevlastv);
 
 /*                    T(i+1:k,i) := */
-/*                            - tau(i) * V(1:n-k+i,i+1:k)' * V(1:n-k+i,i) */
+/*                            - tau(i) * V(j:n-k+i,i+1:k)' * V(j:n-k+i,i) */
 
-                       i__1 = *n - *k + i__;
+                       i__1 = *n - *k + i__ - j + 1;
                        i__2 = *k - i__;
                        r__1 = -tau[i__];
-                       sgemv_("Transpose", &i__1, &i__2, &r__1, &v[(i__ + 1) 
-                               * v_dim1 + 1], ldv, &v[i__ * v_dim1 + 1], &
+                       sgemv_("Transpose", &i__1, &i__2, &r__1, &v[j + (i__ 
+                               + 1) * v_dim1], ldv, &v[j + i__ * v_dim1], &
                                c__1, &c_b8, &t[i__ + 1 + i__ * t_dim1], &
                                c__1);
                        v[*n - *k + i__ + i__ * v_dim1] = vii;
                    } else {
                        vii = v[i__ + (*n - *k + i__) * v_dim1];
                        v[i__ + (*n - *k + i__) * v_dim1] = 1.f;
+/*                    Skip any leading zeros. */
+                       i__1 = i__ - 1;
+                       for (lastv = 1; lastv <= i__1; ++lastv) {
+                           if (v[i__ + lastv * v_dim1] != 0.f) {
+                               break;
+                           }
+                       }
+                       j = max(lastv,prevlastv);
 
 /*                    T(i+1:k,i) := */
-/*                            - tau(i) * V(i+1:k,1:n-k+i) * V(i,1:n-k+i)' */
+/*                            - tau(i) * V(i+1:k,j:n-k+i) * V(i,j:n-k+i)' */
 
                        i__1 = *k - i__;
-                       i__2 = *n - *k + i__;
+                       i__2 = *n - *k + i__ - j + 1;
                        r__1 = -tau[i__];
                        sgemv_("No transpose", &i__1, &i__2, &r__1, &v[i__ + 
-                               1 + v_dim1], ldv, &v[i__ + v_dim1], ldv, &
-                               c_b8, &t[i__ + 1 + i__ * t_dim1], &c__1);
+                               1 + j * v_dim1], ldv, &v[i__ + j * v_dim1], 
+                               ldv, &c_b8, &t[i__ + 1 + i__ * t_dim1], &c__1);
                        v[i__ + (*n - *k + i__) * v_dim1] = vii;
                    }
 
@@ -250,6 +305,11 @@ static real c_b8 = 0.f;
                            + 1 + (i__ + 1) * t_dim1], ldt, &t[i__ + 1 + i__ *
                             t_dim1], &c__1)
                            ;
+                   if (i__ > 1) {
+                       prevlastv = min(prevlastv,lastv);
+                   } else {
+                       prevlastv = lastv;
+                   }
                }
                t[i__ + i__ * t_dim1] = tau[i__];
            }
index 95da773..3511089 100644 (file)
@@ -1,5 +1,18 @@
+/* slarnv.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int slarnv_(integer *idist, integer *iseed, integer *n, real 
        *x)
 {
@@ -16,7 +29,7 @@
     extern /* Subroutine */ int slaruv_(integer *, integer *, real *);
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index c04bb73..e96f8aa 100644 (file)
@@ -1,5 +1,18 @@
+/* slarra.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int slarra_(integer *n, real *d__, real *e, real *e2, real *
        spltol, real *tnrm, integer *nsplit, integer *isplit, integer *info)
 {
@@ -15,7 +28,7 @@
     real tmp1, eabs;
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 2694223..e52e011 100644 (file)
@@ -1,5 +1,18 @@
+/* slarrb.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int slarrb_(integer *n, real *d__, real *lld, integer *
        ifirst, integer *ilast, real *rtol1, real *rtol2, integer *offset, 
        real *w, real *wgap, real *werr, real *work, integer *iwork, real *
@@ -24,7 +37,7 @@
     integer olnint, maxitr;
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 44847dd..11b173b 100644 (file)
@@ -1,5 +1,18 @@
+/* slarrc.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int slarrc_(char *jobt, integer *n, real *vl, real *vu, real 
        *d__, real *e, real *pivmin, integer *eigcnt, integer *lcnt, integer *
        rcnt, integer *info)
@@ -16,7 +29,7 @@
     real lpivot, rpivot;
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index fd026d4..16d9421 100644 (file)
@@ -1,5 +1,18 @@
+/* slarrd.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -41,7 +54,6 @@ static integer c__0 = 0;
     integer ibegin, irange, idiscl;
     extern doublereal slamch_(char *);
     integer idumma[1];
-    real spdiam;
     extern integer ilaenv_(integer *, char *, char *, integer *, integer *, 
            integer *, integer *);
     integer idiscu;
@@ -52,9 +64,10 @@ static integer c__0 = 0;
     logical ncnvrg, toofew;
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
-/*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
-/*     November 2006 */
+/*  -- LAPACK auxiliary routine (version 3.2.1)                        -- */
+/*  -- LAPACK is a software package provided by Univ. of Tennessee,    -- */
+/*  -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..-- */
+/*  -- April 2009                                                      -- */
 
 /*     .. Scalar Arguments .. */
 /*     .. */
@@ -365,7 +378,8 @@ static integer c__0 = 0;
     tnorm = dmax(r__1,r__2);
     gl = gl - tnorm * 2.f * eps * *n - *pivmin * 4.f;
     gu = gu + tnorm * 2.f * eps * *n + *pivmin * 4.f;
-    spdiam = gu - gl;
+/*     [JAN/28/2009] remove the line below since SPDIAM variable not use */
+/*     SPDIAM = GU - GL */
 /*     Input arguments for SLAEBZ: */
 /*     The relative tolerance.  An interval (a,b] lies within */
 /*     "relative tolerance" if  b-a < RELTOL*max(|a|,|b|), */
@@ -529,9 +543,14 @@ static integer c__0 = 0;
                gu = dmax(r__1,r__2);
 /* L40: */
            }
-           spdiam = gu - gl;
-           gl = gl - spdiam * 2.f * eps * in - *pivmin * 2.f;
-           gu = gu + spdiam * 2.f * eps * in + *pivmin * 2.f;
+/*           [JAN/28/2009] */
+/*           change SPDIAM by TNORM in lines 2 and 3 thereafter */
+/*           line 1: remove computation of SPDIAM (not useful anymore) */
+/*           SPDIAM = GU - GL */
+/*           GL = GL - FUDGE*SPDIAM*EPS*IN - FUDGE*PIVMIN */
+/*           GU = GU + FUDGE*SPDIAM*EPS*IN + FUDGE*PIVMIN */
+           gl = gl - tnorm * 2.f * eps * in - *pivmin * 2.f;
+           gu = gu + tnorm * 2.f * eps * in + *pivmin * 2.f;
 
            if (irange > 1) {
                if (gu < *wl) {
index 1880c50..becd7a6 100644 (file)
@@ -1,5 +1,18 @@
+/* slarre.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -69,7 +82,7 @@ static integer c__2 = 2;
            *);
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
@@ -349,6 +362,9 @@ static integer c__2 = 2;
 /*     Can force use of bisection instead of faster DQDS. */
 /*     Option left in the code for future multisection work. */
     forceb = FALSE_;
+/*     Initialize USEDQD, DQDS should be used for ALLRNG unless someone */
+/*     explicitly wants bisection. */
+    usedqd = irange == 1 && ! forceb;
     if (irange == 1 && ! forceb) {
 /*        Set interval [VL,VU] that contains all eigenvalues */
        *vl = gl;
index a6541e5..c74559f 100644 (file)
@@ -1,5 +1,18 @@
+/* slarrf.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -38,7 +51,7 @@ static integer c__1 = 1;
     logical sawnan1, sawnan2, tryrrr1;
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 /* * */
index a04d153..69e836e 100644 (file)
@@ -1,5 +1,18 @@
+/* slarrj.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int slarrj_(integer *n, real *d__, real *e2, integer *ifirst, 
         integer *ilast, real *rtol, integer *offset, real *w, real *werr, 
        real *work, integer *iwork, real *pivmin, real *spdiam, integer *info)
@@ -23,7 +36,7 @@
     integer olnint, maxitr;
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index ec4dadb..b2ac4c0 100644 (file)
@@ -1,5 +1,18 @@
+/* slarrk.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int slarrk_(integer *n, integer *iw, real *gl, real *gu, 
        real *d__, real *e2, real *pivmin, real *reltol, real *w, real *werr, 
        integer *info)
@@ -20,7 +33,7 @@
     integer negcnt;
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 4db671d..291c80f 100644 (file)
@@ -1,5 +1,18 @@
+/* slarrr.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int slarrr_(integer *n, real *d__, real *e, integer *info)
 {
     /* System generated locals */
@@ -18,7 +31,7 @@
     real smlnum, offdig2;
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 4922d5c..35ff9c8 100644 (file)
@@ -1,5 +1,18 @@
+/* slarrv.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static real c_b5 = 0.f;
@@ -86,7 +99,7 @@ static integer c__2 = 2;
            real *, real *, integer *);
 
 
-/*  -- LAPACK auxiliary routine (version 3.1.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index c0a76dd..b0b8efa 100644 (file)
@@ -1,5 +1,18 @@
+/* slartg.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int slartg_(real *f, real *g, real *cs, real *sn, real *r__)
 {
     /* System generated locals */
@@ -18,7 +31,7 @@
     real safmin;
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 063adc4..60e2333 100644 (file)
@@ -1,5 +1,18 @@
+/* slaruv.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int slaruv_(integer *iseed, integer *n, real *x)
 {
     /* Initialized data */
@@ -51,7 +64,7 @@
     integer i__, i1, i2, i3, i4, it1, it2, it3, it4;
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 748cd68..a65cfe5 100644 (file)
@@ -1,5 +1,18 @@
+/* slas2.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int slas2_(real *f, real *g, real *h__, real *ssmin, real *
        ssmax)
 {
@@ -13,7 +26,7 @@
     real c__, fa, ga, ha, as, at, au, fhmn, fhmx;
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 0c52668..f8afad7 100644 (file)
@@ -1,5 +1,18 @@
+/* slascl.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int slascl_(char *type__, integer *kl, integer *ku, real *
        cfrom, real *cto, integer *m, integer *n, real *a, integer *lda, 
        integer *info)
     extern doublereal slamch_(char *);
     real cfromc;
     extern /* Subroutine */ int xerbla_(char *, integer *);
-    real bignum, smlnum;
+    real bignum;
+    extern logical sisnan_(real *);
+    real smlnum;
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
 
     if (itype == -1) {
        *info = -1;
-    } else if (*cfrom == 0.f) {
+    } else if (*cfrom == 0.f || sisnan_(cfrom)) {
        *info = -4;
+    } else if (sisnan_(cto)) {
+       *info = -5;
     } else if (*m < 0) {
        *info = -6;
     } else if (*n < 0 || itype == 4 && *n != *m || itype == 5 && *n != *m) {
 
 L10:
     cfrom1 = cfromc * smlnum;
-    cto1 = ctoc / bignum;
-    if (dabs(cfrom1) > dabs(ctoc) && ctoc != 0.f) {
-       mul = smlnum;
-       done = FALSE_;
-       cfromc = cfrom1;
-    } else if (dabs(cto1) > dabs(cfromc)) {
-       mul = bignum;
-       done = FALSE_;
-       ctoc = cto1;
-    } else {
+    if (cfrom1 == cfromc) {
+/*        CFROMC is an inf.  Multiply by a correctly signed zero for */
+/*        finite CTOC, or a NaN if CTOC is infinite. */
        mul = ctoc / cfromc;
        done = TRUE_;
+       cto1 = ctoc;
+    } else {
+       cto1 = ctoc / bignum;
+       if (cto1 == ctoc) {
+/*           CTOC is either 0 or an inf.  In both cases, CTOC itself */
+/*           serves as the correct multiplication factor. */
+           mul = ctoc;
+           done = TRUE_;
+           cfromc = 1.f;
+       } else if (dabs(cfrom1) > dabs(ctoc) && ctoc != 0.f) {
+           mul = smlnum;
+           done = FALSE_;
+           cfromc = cfrom1;
+       } else if (dabs(cto1) > dabs(cfromc)) {
+           mul = bignum;
+           done = FALSE_;
+           ctoc = cto1;
+       } else {
+           mul = ctoc / cfromc;
+           done = TRUE_;
+       }
     }
 
     if (itype == 0) {
index e24ada9..9906433 100644 (file)
@@ -1,5 +1,18 @@
+/* slasd0.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__0 = 0;
@@ -31,7 +44,7 @@ static integer c__2 = 2;
 );
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index ba18f28..3da0399 100644 (file)
@@ -1,5 +1,18 @@
+/* slasd1.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__0 = 0;
@@ -36,7 +49,7 @@ static integer c_n1 = -1;
     integer coltyp;
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index c1bc8fa..0bf7e2a 100644 (file)
@@ -1,5 +1,18 @@
+/* slasd2.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -39,7 +52,7 @@ static real c_b30 = 0.f;
            integer *, real *, real *, real *, integer *);
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 9d0112a..9b5fa7b 100644 (file)
@@ -1,5 +1,18 @@
+/* slasd3.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -42,7 +55,7 @@ static real c_b26 = 0.f;
            real *, integer *);
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index a655b9b..b57b231 100644 (file)
@@ -1,5 +1,18 @@
+/* slasd4.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int slasd4_(integer *n, integer *i__, real *d__, real *z__, 
        real *delta, real *rho, real *sigma, real *work, integer *info)
 {
@@ -38,7 +51,7 @@
     real erretm, dtipsq, rhoinv;
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index a46ec84..b8bdada 100644 (file)
@@ -1,5 +1,18 @@
+/* slasd5.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int slasd5_(integer *i__, real *d__, real *z__, real *delta, 
        real *rho, real *dsigma, real *work)
 {
@@ -13,7 +26,7 @@
     real b, c__, w, del, tau, delsq;
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index a6b9944..4ebc82e 100644 (file)
@@ -1,5 +1,18 @@
+/* slasd6.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__0 = 0;
@@ -37,7 +50,7 @@ static integer c_n1 = -1;
     real orgnrm;
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index d816f32..5533633 100644 (file)
@@ -1,5 +1,18 @@
+/* slasd7.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -32,7 +45,7 @@ static integer c__1 = 1;
     real hlftol;
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index f8b8bca..195498f 100644 (file)
@@ -1,5 +1,18 @@
+/* slasd8.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -38,9 +51,9 @@ static real c_b8 = 1.f;
            real *, integer *);
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
-/*     November 2006 */
+/*     October 2006 */
 
 /*     .. Scalar Arguments .. */
 /*     .. */
@@ -75,9 +88,10 @@ static real c_b8 = 1.f;
 /*  D       (output) REAL array, dimension ( K ) */
 /*          On output, D contains the updated singular values. */
 
-/*  Z       (input) REAL array, dimension ( K ) */
-/*          The first K elements of this array contain the components */
-/*          of the deflation-adjusted updating row vector. */
+/*  Z       (input/output) REAL array, dimension ( K ) */
+/*          On entry, the first K elements of this array contain the */
+/*          components of the deflation-adjusted updating row vector. */
+/*          On exit, Z is updated. */
 
 /*  VF      (input/output) REAL array, dimension ( K ) */
 /*          On entry, VF contains  information passed through DBEDE8. */
@@ -106,10 +120,12 @@ static real c_b8 = 1.f;
 /*  LDDIFR  (input) INTEGER */
 /*          The leading dimension of DIFR, must be at least K. */
 
-/*  DSIGMA  (input) REAL array, dimension ( K ) */
-/*          The first K elements of this array contain the old roots */
-/*          of the deflated updating problem.  These are the poles */
+/*  DSIGMA  (input/output) REAL array, dimension ( K ) */
+/*          On entry, the first K elements of this array contain the old */
+/*          roots of the deflated updating problem.  These are the poles */
 /*          of the secular equation. */
+/*          On exit, the elements of DSIGMA may be very slightly altered */
+/*          in value. */
 
 /*  WORK    (workspace) REAL array, dimension at least 3 * K */
 
@@ -195,7 +211,7 @@ static real c_b8 = 1.f;
 /*     changes the bottommost bits of DSIGMA(I). It does not account */
 /*     for hexadecimal or decimal machines without guard digits */
 /*     (we know of none). We use a subroutine call to compute */
-/*     2*DSIGMA(I) to prevent optimizing compilers from eliminating */
+/*     2*DLAMBDA(I) to prevent optimizing compilers from eliminating */
 /*     this code. */
 
     i__1 = *k;
index d23e4ea..daab1e0 100644 (file)
@@ -1,5 +1,18 @@
+/* slasda.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__0 = 0;
@@ -47,7 +60,7 @@ static integer c__2 = 2;
     integer smlszp;
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index ec79066..515aa96 100644 (file)
@@ -1,5 +1,18 @@
+/* slasdq.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -32,7 +45,7 @@ static integer c__1 = 1;
 , real *, integer *, real *, integer *);
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 8a605a5..2ceb0bb 100644 (file)
@@ -1,5 +1,18 @@
+/* slasdt.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int slasdt_(integer *n, integer *lvl, integer *nd, integer *
        inode, integer *ndiml, integer *ndimr, integer *msub)
 {
@@ -15,7 +28,7 @@
     integer nlvl, llst, ncrnt;
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 6b2563c..55c571c 100644 (file)
@@ -1,5 +1,18 @@
+/* slaset.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int slaset_(char *uplo, integer *m, integer *n, real *alpha, 
        real *beta, real *a, integer *lda)
 {
@@ -11,7 +24,7 @@
     extern logical lsame_(char *, char *);
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 403aa17..6b3ac67 100644 (file)
@@ -1,5 +1,18 @@
+/* slasq1.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -34,9 +47,15 @@ static integer c__0 = 0;
 , real *, integer *);
 
 
-/*  -- LAPACK routine (version 3.1) -- */
-/*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
-/*     November 2006 */
+/*  -- LAPACK routine (version 3.2)                                    -- */
+
+/*  -- Contributed by Osni Marques of the Lawrence Berkeley National   -- */
+/*  -- Laboratory and Beresford Parlett of the Univ. of California at  -- */
+/*  -- Berkeley                                                        -- */
+/*  -- November 2008                                                   -- */
+
+/*  -- LAPACK is a software package provided by Univ. of Tennessee,    -- */
+/*  -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..-- */
 
 /*     .. Scalar Arguments .. */
 /*     .. */
index 464eb64..b6b34b3 100644 (file)
@@ -1,13 +1,22 @@
+/* slasq2.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
 static integer c__2 = 2;
-static integer c__10 = 10;
-static integer c__3 = 3;
-static integer c__4 = 4;
-static integer c__11 = 11;
 
 /* Subroutine */ int slasq2_(integer *n, real *z__, integer *info)
 {
@@ -19,43 +28,46 @@ static integer c__11 = 11;
     double sqrt(doublereal);
 
     /* Local variables */
-    real d__, e;
+    real d__, e, g;
     integer k;
     real s, t;
     integer i0, i4, n0;
     real dn;
     integer pp;
-    real dn1, dn2, eps, tau, tol;
+    real dn1, dn2, dee, eps, tau, tol;
     integer ipn4;
     real tol2;
     logical ieee;
     integer nbig;
     real dmin__, emin, emax;
-    integer ndiv, iter;
+    integer kmin, ndiv, iter;
     real qmin, temp, qmax, zmax;
     integer splt;
     real dmin1, dmin2;
     integer nfail;
     real desig, trace, sigma;
     integer iinfo, ttype;
-    extern /* Subroutine */ int slazq3_(integer *, integer *, real *, integer 
+    extern /* Subroutine */ int slasq3_(integer *, integer *, real *, integer 
            *, real *, real *, real *, real *, integer *, integer *, integer *
 , logical *, integer *, real *, real *, real *, real *, real *, 
-           real *);
+           real *, real *);
+    real deemin;
     extern doublereal slamch_(char *);
     integer iwhila, iwhilb;
     real oldemn, safmin;
-    extern /* Subroutine */ int xerbla_(char *, integer *);
-    extern integer ilaenv_(integer *, char *, char *, integer *, integer *, 
-           integer *, integer *);
-    extern /* Subroutine */ int slasrt_(char *, integer *, real *, integer *);
+    extern /* Subroutine */ int xerbla_(char *, integer *), slasrt_(
+           char *, integer *, real *, integer *);
+
 
+/*  -- LAPACK routine (version 3.2)                                    -- */
 
-/*  -- LAPACK routine (version 3.1) -- */
-/*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
-/*     November 2006 */
+/*  -- Contributed by Osni Marques of the Lawrence Berkeley National   -- */
+/*  -- Laboratory and Beresford Parlett of the Univ. of California at  -- */
+/*  -- Berkeley                                                        -- */
+/*  -- November 2008                                                   -- */
 
-/*     Modified to call SLAZQ3 in place of SLASQ3, 13 Feb 03, SJH. */
+/*  -- LAPACK is a software package provided by Univ. of Tennessee,    -- */
+/*  -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..-- */
 
 /*     .. Scalar Arguments .. */
 /*     .. */
@@ -79,7 +91,7 @@ static integer c__11 = 11;
 /*  Note : SLASQ2 defines a logical variable, IEEE, which is true */
 /*  on machines which follow ieee-754 floating-point standard in their */
 /*  handling of infinities and NaNs, and false otherwise. This variable */
-/*  is passed to SLAZQ3. */
+/*  is passed to SLASQ3. */
 
 /*  Arguments */
 /*  ========= */
@@ -87,7 +99,7 @@ static integer c__11 = 11;
 /*  N     (input) INTEGER */
 /*        The number of rows and columns in the matrix. N >= 0. */
 
-/*  Z     (workspace) REAL array, dimension (4*N) */
+/*  Z     (input/output) REAL array, dimension ( 4*N ) */
 /*        On entry Z holds the qd array. On exit, entries 1 to N hold */
 /*        the eigenvalues in decreasing order, Z( 2*N+1 ) holds the */
 /*        trace, and Z( 2*N+2 ) holds the sum of the eigenvalues. If */
@@ -257,8 +269,13 @@ static integer c__11 = 11;
 
 /*     Check whether the machine is IEEE conformable. */
 
-    ieee = ilaenv_(&c__10, "SLASQ2", "N", &c__1, &c__2, &c__3, &c__4) == 1 && ilaenv_(&c__11, "SLASQ2", "N", &c__1, &c__2, 
-            &c__3, &c__4) == 1;
+/*     IEEE = ILAENV( 10, 'SLASQ2', 'N', 1, 2, 3, 4 ).EQ.1 .AND. */
+/*    $       ILAENV( 11, 'SLASQ2', 'N', 1, 2, 3, 4 ).EQ.1 */
+
+/*     [11/15/2008] The case IEEE=.TRUE. has a problem in single precision with */
+/*     some the test matrices of type 16. The double precision code is fine. */
+
+    ieee = FALSE_;
 
 /*     Rearrange data for locality: Z=(q1,qq1,e1,ee1,q2,qq2,e2,ee2,...). */
 
@@ -353,7 +370,7 @@ static integer c__11 = 11;
 /* L80: */
     }
 
-/*     Initialise variables to pass to SLAZQ3 */
+/*     Initialise variables to pass to SLASQ3. */
 
     ttype = 0;
     dmin1 = 0.f;
@@ -361,6 +378,7 @@ static integer c__11 = 11;
     dn = 0.f;
     dn1 = 0.f;
     dn2 = 0.f;
+    g = 0.f;
     tau = 0.f;
 
     iter = 2;
@@ -370,7 +388,7 @@ static integer c__11 = 11;
     i__1 = *n + 1;
     for (iwhila = 1; iwhila <= i__1; ++iwhila) {
        if (n0 < 1) {
-           goto L150;
+           goto L170;
        }
 
 /*        While array unfinished do */
@@ -424,10 +442,43 @@ static integer c__11 = 11;
 
 L100:
        i0 = i4 / 4;
+       pp = 0;
 
-/*        Store EMIN for passing to SLAZQ3. */
-
-       z__[(n0 << 2) - 1] = emin;
+       if (n0 - i0 > 1) {
+           dee = z__[(i0 << 2) - 3];
+           deemin = dee;
+           kmin = i0;
+           i__2 = (n0 << 2) - 3;
+           for (i4 = (i0 << 2) + 1; i4 <= i__2; i4 += 4) {
+               dee = z__[i4] * (dee / (dee + z__[i4 - 2]));
+               if (dee <= deemin) {
+                   deemin = dee;
+                   kmin = (i4 + 3) / 4;
+               }
+/* L110: */
+           }
+           if (kmin - i0 << 1 < n0 - kmin && deemin <= z__[(n0 << 2) - 3] * 
+                   .5f) {
+               ipn4 = i0 + n0 << 2;
+               pp = 2;
+               i__2 = i0 + n0 - 1 << 1;
+               for (i4 = i0 << 2; i4 <= i__2; i4 += 4) {
+                   temp = z__[i4 - 3];
+                   z__[i4 - 3] = z__[ipn4 - i4 - 3];
+                   z__[ipn4 - i4 - 3] = temp;
+                   temp = z__[i4 - 2];
+                   z__[i4 - 2] = z__[ipn4 - i4 - 2];
+                   z__[ipn4 - i4 - 2] = temp;
+                   temp = z__[i4 - 1];
+                   z__[i4 - 1] = z__[ipn4 - i4 - 5];
+                   z__[ipn4 - i4 - 5] = temp;
+                   temp = z__[i4];
+                   z__[i4] = z__[ipn4 - i4 - 4];
+                   z__[ipn4 - i4 - 4] = temp;
+/* L120: */
+               }
+           }
+       }
 
 /*        Put -(initial shift) into DMIN. */
 
@@ -435,22 +486,24 @@ L100:
        r__1 = 0.f, r__2 = qmin - sqrt(qmin) * 2.f * sqrt(emax);
        dmin__ = -dmax(r__1,r__2);
 
-/*        Now I0:N0 is unreduced. PP = 0 for ping, PP = 1 for pong. */
-
-       pp = 0;
+/*        Now I0:N0 is unreduced. */
+/*        PP = 0 for ping, PP = 1 for pong. */
+/*        PP = 2 indicates that flipping was applied to the Z array and */
+/*               and that the tests for deflation upon entry in SLASQ3 */
+/*               should not be performed. */
 
        nbig = (n0 - i0 + 1) * 30;
        i__2 = nbig;
        for (iwhilb = 1; iwhilb <= i__2; ++iwhilb) {
            if (i0 > n0) {
-               goto L130;
+               goto L150;
            }
 
 /*           While submatrix unfinished take a good dqds step. */
 
-           slazq3_(&i0, &n0, &z__[1], &pp, &dmin__, &sigma, &desig, &qmax, &
+           slasq3_(&i0, &n0, &z__[1], &pp, &dmin__, &sigma, &desig, &qmax, &
                    nfail, &iter, &ndiv, &ieee, &ttype, &dmin1, &dmin2, &dn, &
-                   dn1, &dn2, &tau);
+                   dn1, &dn2, &g, &tau);
 
            pp = 1 - pp;
 
@@ -483,7 +536,7 @@ L100:
                            r__1 = oldemn, r__2 = z__[i4];
                            oldemn = dmin(r__1,r__2);
                        }
-/* L110: */
+/* L130: */
                    }
                    z__[(n0 << 2) - 1] = emin;
                    z__[n0 * 4] = oldemn;
@@ -491,7 +544,7 @@ L100:
                }
            }
 
-/* L120: */
+/* L140: */
        }
 
        *info = 2;
@@ -499,9 +552,9 @@ L100:
 
 /*        end IWHILB */
 
-L130:
+L150:
 
-/* L140: */
+/* L160: */
        ;
     }
 
@@ -510,14 +563,14 @@ L130:
 
 /*     end IWHILA */
 
-L150:
+L170:
 
 /*     Move q's to the front. */
 
     i__1 = *n;
     for (k = 2; k <= i__1; ++k) {
        z__[k] = z__[(k << 2) - 3];
-/* L160: */
+/* L180: */
     }
 
 /*     Sort and compute sum of eigenvalues. */
@@ -527,7 +580,7 @@ L150:
     e = 0.f;
     for (k = *n; k >= 1; --k) {
        e += z__[k];
-/* L170: */
+/* L190: */
     }
 
 /*     Store trace, sum(eigenvalues) and information on performance. */
index 01fcb5f..2c3452a 100644 (file)
@@ -1,19 +1,24 @@
+/* slasq3.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int slasq3_(integer *i0, integer *n0, real *z__, integer *pp, 
         real *dmin__, real *sigma, real *desig, real *qmax, integer *nfail, 
-       integer *iter, integer *ndiv, logical *ieee)
+       integer *iter, integer *ndiv, logical *ieee, integer *ttype, real *
+       dmin1, real *dmin2, real *dn, real *dn1, real *dn2, real *g, real *
+       tau)
 {
-    /* Initialized data */
-
-    static integer ttype = 0;
-    static real dmin1 = 0.f;
-    static real dmin2 = 0.f;
-    static real dn = 0.f;
-    static real dn1 = 0.f;
-    static real dn2 = 0.f;
-    static real tau = 0.f;
-
     /* System generated locals */
     integer i__1;
     real r__1, r__2;
     real tol2, temp;
     extern /* Subroutine */ int slasq4_(integer *, integer *, real *, integer 
            *, integer *, real *, real *, real *, real *, real *, real *, 
-           real *, integer *), slasq5_(integer *, integer *, real *, integer 
-           *, real *, real *, real *, real *, real *, real *, real *, 
-           logical *), slasq6_(integer *, integer *, real *, integer *, real 
-           *, real *, real *, real *, real *, real *);
+           real *, integer *, real *), slasq5_(integer *, integer *, real *, 
+           integer *, real *, real *, real *, real *, real *, real *, real *, 
+            logical *), slasq6_(integer *, integer *, real *, integer *, 
+           real *, real *, real *, real *, real *, real *);
     extern doublereal slamch_(char *);
-    real safmin;
+    extern logical sisnan_(real *);
+
 
+/*  -- LAPACK routine (version 3.2)                                    -- */
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
-/*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
-/*     November 2006 */
+/*  -- Contributed by Osni Marques of the Lawrence Berkeley National   -- */
+/*  -- Laboratory and Beresford Parlett of the Univ. of California at  -- */
+/*  -- Berkeley                                                        -- */
+/*  -- November 2008                                                   -- */
+
+/*  -- LAPACK is a software package provided by Univ. of Tennessee,    -- */
+/*  -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..-- */
 
 /*     .. Scalar Arguments .. */
 /*     .. */
 /*  Z      (input) REAL array, dimension ( 4*N ) */
 /*         Z holds the qd array. */
 
-/*  PP     (input) INTEGER */
+/*  PP     (input/output) INTEGER */
 /*         PP=0 for ping, PP=1 for pong. */
+/*         PP=2 indicates that flipping was applied to the Z array */
+/*         and that the initial tests for deflation should not be */
+/*         performed. */
 
 /*  DMIN   (output) REAL */
 /*         Minimum value of d. */
 /*  NDIV   (output) INTEGER */
 /*         Number of divisions. */
 
-/*  TTYPE  (output) INTEGER */
-/*         Shift type. */
-
 /*  IEEE   (input) LOGICAL */
 /*         Flag for IEEE or non IEEE arithmetic (passed to SLASQ5). */
 
+/*  TTYPE  (input/output) INTEGER */
+/*         Shift type. */
+
+/*  DMIN1, DMIN2, DN, DN1, DN2, G, TAU (input/output) REAL */
+/*         These are passed as arguments in order to save their values */
+/*         between calls to SLASQ3. */
+
 /*  ===================================================================== */
 
 /*     .. Parameters .. */
 /*     .. */
 /*     .. Intrinsic Functions .. */
 /*     .. */
-/*     .. Save statement .. */
-/*     .. */
-/*     .. Data statement .. */
+/*     .. Executable Statements .. */
+
     /* Parameter adjustments */
     --z__;
 
     /* Function Body */
-/*     .. */
-/*     .. Executable Statements .. */
-
     n0in = *n0;
     eps = slamch_("Precision");
-    safmin = slamch_("Safe minimum");
     tol = eps * 100.f;
 /* Computing 2nd power */
     r__1 = tol;
@@ -187,6 +200,9 @@ L40:
     goto L10;
 
 L50:
+    if (*pp == 2) {
+       *pp = 0;
+    }
 
 /*     Reverse the qd-array, if warranted. */
 
@@ -214,8 +230,8 @@ L50:
                z__[(*n0 << 2) - *pp] = z__[(*i0 << 2) - *pp];
            }
 /* Computing MIN */
-           r__1 = dmin2, r__2 = z__[(*n0 << 2) + *pp - 1];
-           dmin2 = dmin(r__1,r__2);
+           r__1 = *dmin2, r__2 = z__[(*n0 << 2) + *pp - 1];
+           *dmin2 = dmin(r__1,r__2);
 /* Computing MIN */
            r__1 = z__[(*n0 << 2) + *pp - 1], r__2 = z__[(*i0 << 2) + *pp - 1]
                    , r__1 = min(r__1,r__2), r__2 = z__[(*i0 << 2) + *pp + 3];
@@ -232,96 +248,94 @@ L50:
        }
     }
 
-/* Computing MIN */
-    r__1 = z__[(*n0 << 2) + *pp - 1], r__2 = z__[(*n0 << 2) + *pp - 9], r__1 =
-            min(r__1,r__2), r__2 = dmin2 + z__[(*n0 << 2) - *pp];
-    if (*dmin__ < 0.f || safmin * *qmax < dmin(r__1,r__2)) {
-
-/*        Choose a shift. */
+/*     Choose a shift. */
 
-       slasq4_(i0, n0, &z__[1], pp, &n0in, dmin__, &dmin1, &dmin2, &dn, &dn1
-                &dn2, &tau, &ttype);
+    slasq4_(i0, n0, &z__[1], pp, &n0in, dmin__, dmin1, dmin2, dn, dn1, dn2
+           tau, ttype, g);
 
-/*        Call dqds until DMIN > 0. */
+/*     Call dqds until DMIN > 0. */
 
-L80:
+L70:
 
-       slasq5_(i0, n0, &z__[1], pp, &tau, dmin__, &dmin1, &dmin2, &dn, &dn1
-               &dn2, ieee);
+    slasq5_(i0, n0, &z__[1], pp, tau, dmin__, dmin1, dmin2, dn, dn1, dn2
+           ieee);
 
-       *ndiv += *n0 - *i0 + 2;
-       ++(*iter);
+    *ndiv += *n0 - *i0 + 2;
+    ++(*iter);
 
-/*        Check status. */
+/*     Check status. */
 
-       if (*dmin__ >= 0.f && dmin1 > 0.f) {
+    if (*dmin__ >= 0.f && *dmin1 > 0.f) {
 
-/*           Success. */
+/*        Success. */
 
-           goto L100;
+       goto L90;
 
-       } else if (*dmin__ < 0.f && dmin1 > 0.f && z__[(*n0 - 1 << 2) - *pp] <
-                tol * (*sigma + dn1) && dabs(dn) < tol * *sigma) {
+    } else if (*dmin__ < 0.f && *dmin1 > 0.f && z__[(*n0 - 1 << 2) - *pp] < 
+           tol * (*sigma + *dn1) && dabs(*dn) < tol * *sigma) {
 
-/*           Convergence hidden by negative DN. */
+/*        Convergence hidden by negative DN. */
 
-           z__[(*n0 - 1 << 2) - *pp + 2] = 0.f;
-           *dmin__ = 0.f;
-           goto L100;
-       } else if (*dmin__ < 0.f) {
+       z__[(*n0 - 1 << 2) - *pp + 2] = 0.f;
+       *dmin__ = 0.f;
+       goto L90;
+    } else if (*dmin__ < 0.f) {
 
-/*           TAU too big. Select new TAU and try again. */
+/*        TAU too big. Select new TAU and try again. */
 
-           ++(*nfail);
-           if (ttype < -22) {
+       ++(*nfail);
+       if (*ttype < -22) {
 
-/*              Failed twice. Play it safe. */
+/*           Failed twice. Play it safe. */
 
-               tau = 0.f;
-           } else if (dmin1 > 0.f) {
+           *tau = 0.f;
+       } else if (*dmin1 > 0.f) {
 
-/*              Late failure. Gives excellent shift. */
+/*           Late failure. Gives excellent shift. */
 
-               tau = (tau + *dmin__) * (1.f - eps * 2.f);
-               ttype += -11;
-           } else {
+           *tau = (*tau + *dmin__) * (1.f - eps * 2.f);
+           *ttype += -11;
+       } else {
 
-/*              Early failure. Divide by 4. */
+/*           Early failure. Divide by 4. */
 
-               tau *= .25f;
-               ttype += -12;
-           }
-           goto L80;
-       } else if (*dmin__ != *dmin__) {
+           *tau *= .25f;
+           *ttype += -12;
+       }
+       goto L70;
+    } else if (sisnan_(dmin__)) {
 
-/*           NaN. */
+/*        NaN. */
 
-           tau = 0.f;
+       if (*tau == 0.f) {
            goto L80;
        } else {
+           *tau = 0.f;
+           goto L70;
+       }
+    } else {
 
-/*           Possible underflow. Play it safe. */
+/*        Possible underflow. Play it safe. */
 
-           goto L90;
-       }
+       goto L80;
     }
 
 /*     Risk of underflow. */
 
-L90:
-    slasq6_(i0, n0, &z__[1], pp, dmin__, &dmin1, &dmin2, &dn, &dn1, &dn2);
+L80:
+    slasq6_(i0, n0, &z__[1], pp, dmin__, dmin1, dmin2, dn, dn1, dn2);
     *ndiv += *n0 - *i0 + 2;
     ++(*iter);
-    tau = 0.f;
+    *tau = 0.f;
 
-L100:
-    if (tau < *sigma) {
-       *desig += tau;
+L90:
+    if (*tau < *sigma) {
+       *desig += *tau;
        t = *sigma + *desig;
        *desig -= t - *sigma;
     } else {
-       t = *sigma + tau;
-       *desig = *sigma - (t - tau) + *desig;
+       t = *sigma + *tau;
+       *desig = *sigma - (t - *tau) + *desig;
     }
     *sigma = t;
 
index 1e8125f..b7ad1b8 100644 (file)
@@ -1,13 +1,22 @@
+/* slasq4.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int slasq4_(integer *i0, integer *n0, real *z__, integer *pp, 
         integer *n0in, real *dmin__, real *dmin1, real *dmin2, real *dn, 
-       real *dn1, real *dn2, real *tau, integer *ttype)
+       real *dn1, real *dn2, real *tau, integer *ttype, real *g)
 {
-    /* Initialized data */
-
-    static real g = 0.f;
-
     /* System generated locals */
     integer i__1;
     real r__1, r__2;
     real gam, gap1, gap2;
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
-/*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
-/*     November 2006 */
+/*  -- LAPACK routine (version 3.2)                                    -- */
+
+/*  -- Contributed by Osni Marques of the Lawrence Berkeley National   -- */
+/*  -- Laboratory and Beresford Parlett of the Univ. of California at  -- */
+/*  -- Berkeley                                                        -- */
+/*  -- November 2008                                                   -- */
+
+/*  -- LAPACK is a software package provided by Univ. of Tennessee,    -- */
+/*  -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..-- */
 
 /*     .. Scalar Arguments .. */
 /*     .. */
@@ -48,7 +63,7 @@
 /*  PP    (input) INTEGER */
 /*        PP=0 for ping, PP=1 for pong. */
 
-/*  N0IN  (input) INTEGER */
+/*  NOIN  (input) INTEGER */
 /*        The value of N0 at start of EIGTEST. */
 
 /*  DMIN  (input) REAL */
 /*  TTYPE (output) INTEGER */
 /*        Shift type. */
 
+/*  G     (input/output) REAL */
+/*        G is passed as an argument in order to save its value between */
+/*        calls to SLASQ4. */
+
 /*  Further Details */
 /*  =============== */
 /*  CNST1 = 9/16 */
 /*     .. */
 /*     .. Intrinsic Functions .. */
 /*     .. */
-/*     .. Save statement .. */
-/*     .. */
-/*     .. Data statement .. */
-    /* Parameter adjustments */
-    --z__;
-
-    /* Function Body */
-/*     .. */
 /*     .. Executable Statements .. */
 
 /*     A negative DMIN forces the shift to take that absolute value */
 /*     TTYPE records the type of shift. */
 
+    /* Parameter adjustments */
+    --z__;
+
+    /* Function Body */
     if (*dmin__ <= 0.f) {
        *tau = -(*dmin__);
        *ttype = -1;
@@ -254,13 +269,13 @@ L40:
 /*           Case 6, no information to guide us. */
 
            if (*ttype == -6) {
-               g += (1.f - g) * .333f;
+               *g += (1.f - *g) * .333f;
            } else if (*ttype == -18) {
-               g = .083250000000000005f;
+               *g = .083250000000000005f;
            } else {
-               g = .25f;
+               *g = .25f;
            }
-           s = g * *dmin__;
+           s = *g * *dmin__;
            *ttype = -6;
        }
 
index cf616ab..72f27d4 100644 (file)
@@ -1,5 +1,18 @@
+/* slasq5.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int slasq5_(integer *i0, integer *n0, real *z__, integer *pp, 
         real *tau, real *dmin__, real *dmin1, real *dmin2, real *dn, real *
        dnm1, real *dnm2, logical *ieee)
     real emin, temp;
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
-/*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
-/*     November 2006 */
+/*  -- LAPACK routine (version 3.2)                                    -- */
+
+/*  -- Contributed by Osni Marques of the Lawrence Berkeley National   -- */
+/*  -- Laboratory and Beresford Parlett of the Univ. of California at  -- */
+/*  -- Berkeley                                                        -- */
+/*  -- November 2008                                                   -- */
+
+/*  -- LAPACK is a software package provided by Univ. of Tennessee,    -- */
+/*  -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..-- */
 
 /*     .. Scalar Arguments .. */
 /*     .. */
index c9fbd35..c3dc46f 100644 (file)
@@ -1,5 +1,18 @@
+/* slasq6.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int slasq6_(integer *i0, integer *n0, real *z__, integer *pp, 
         real *dmin__, real *dmin1, real *dmin2, real *dn, real *dnm1, real *
        dnm2)
     real safmin;
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
-/*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
-/*     November 2006 */
+/*  -- LAPACK routine (version 3.2)                                    -- */
+
+/*  -- Contributed by Osni Marques of the Lawrence Berkeley National   -- */
+/*  -- Laboratory and Beresford Parlett of the Univ. of California at  -- */
+/*  -- Berkeley                                                        -- */
+/*  -- November 2008                                                   -- */
+
+/*  -- LAPACK is a software package provided by Univ. of Tennessee,    -- */
+/*  -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..-- */
 
 /*     .. Scalar Arguments .. */
 /*     .. */
index 8a70979..458ced7 100644 (file)
@@ -1,5 +1,18 @@
+/* slasr.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int slasr_(char *side, char *pivot, char *direct, integer *m, 
         integer *n, real *c__, real *s, real *a, integer *lda)
 {
@@ -14,7 +27,7 @@
     extern /* Subroutine */ int xerbla_(char *, integer *);
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 67124ed..36c6553 100644 (file)
@@ -1,5 +1,18 @@
+/* slasrt.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int slasrt_(char *id, integer *n, real *d__, integer *info)
 {
     /* System generated locals */
@@ -19,7 +32,7 @@
     integer stkpnt;
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index df01973..4a36f2b 100644 (file)
@@ -1,5 +1,18 @@
+/* slassq.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int slassq_(integer *n, real *x, integer *incx, real *scale, 
        real *sumsq)
 {
@@ -12,7 +25,7 @@
     real absxi;
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 3b88738..9cce1d9 100644 (file)
@@ -1,5 +1,18 @@
+/* slasv2.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static real c_b3 = 2.f;
@@ -25,7 +38,7 @@ static real c_b4 = 1.f;
     extern doublereal slamch_(char *);
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 4e48ff2..dbe5a97 100644 (file)
@@ -1,5 +1,18 @@
+/* slaswp.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int slaswp_(integer *n, real *a, integer *lda, integer *k1, 
        integer *k2, integer *ipiv, integer *incx)
 {
@@ -11,7 +24,7 @@
     real temp;
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 000f273..38f6a61 100644 (file)
@@ -1,5 +1,18 @@
+/* slatrd.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static real c_b5 = -1.f;
@@ -27,7 +40,7 @@ static real c_b16 = 0.f;
            real *, integer *, real *);
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index a311f1f..354c973 100644 (file)
@@ -1,5 +1,18 @@
+/* slauu2.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static real c_b7 = 1.f;
@@ -23,7 +36,7 @@ static integer c__1 = 1;
     extern /* Subroutine */ int xerbla_(char *, integer *);
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 1f2cb1f..4f99e6b 100644 (file)
@@ -1,5 +1,18 @@
+/* slauum.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -29,7 +42,7 @@ static real c_b15 = 1.f;
            integer *, integer *);
 
 
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
+/*  -- LAPACK auxiliary routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
diff --git a/3rdparty/lapack/slazq3.c b/3rdparty/lapack/slazq3.c
deleted file mode 100644 (file)
index 7c6336e..0000000
+++ /dev/null
@@ -1,334 +0,0 @@
-#include "clapack.h"
-
-/* Subroutine */ int slazq3_(integer *i0, integer *n0, real *z__, integer *pp, 
-        real *dmin__, real *sigma, real *desig, real *qmax, integer *nfail, 
-       integer *iter, integer *ndiv, logical *ieee, integer *ttype, real *
-       dmin1, real *dmin2, real *dn, real *dn1, real *dn2, real *tau)
-{
-    /* System generated locals */
-    integer i__1;
-    real r__1, r__2;
-
-    /* Builtin functions */
-    double sqrt(doublereal);
-
-    /* Local variables */
-    real g, s, t;
-    integer j4, nn;
-    real eps, tol;
-    integer n0in, ipn4;
-    real tol2, temp;
-    extern /* Subroutine */ int slasq5_(integer *, integer *, real *, integer 
-           *, real *, real *, real *, real *, real *, real *, real *, 
-           logical *), slasq6_(integer *, integer *, real *, integer *, real 
-           *, real *, real *, real *, real *, real *), slazq4_(integer *, 
-           integer *, real *, integer *, integer *, real *, real *, real *, 
-           real *, real *, real *, real *, integer *, real *);
-    extern doublereal slamch_(char *);
-    real safmin;
-
-
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
-/*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
-/*     November 2006 */
-
-/*     .. Scalar Arguments .. */
-/*     .. */
-/*     .. Array Arguments .. */
-/*     .. */
-
-/*  Purpose */
-/*  ======= */
-
-/*  SLAZQ3 checks for deflation, computes a shift (TAU) and calls dqds. */
-/*  In case of failure it changes shifts, and tries again until output */
-/*  is positive. */
-
-/*  Arguments */
-/*  ========= */
-
-/*  I0     (input) INTEGER */
-/*         First index. */
-
-/*  N0     (input) INTEGER */
-/*         Last index. */
-
-/*  Z      (input) REAL array, dimension ( 4*N ) */
-/*         Z holds the qd array. */
-
-/*  PP     (input) INTEGER */
-/*         PP=0 for ping, PP=1 for pong. */
-
-/*  DMIN   (output) REAL */
-/*         Minimum value of d. */
-
-/*  SIGMA  (output) REAL */
-/*         Sum of shifts used in current segment. */
-
-/*  DESIG  (input/output) REAL */
-/*         Lower order part of SIGMA */
-
-/*  QMAX   (input) REAL */
-/*         Maximum value of q. */
-
-/*  NFAIL  (output) INTEGER */
-/*         Number of times shift was too big. */
-
-/*  ITER   (output) INTEGER */
-/*         Number of iterations. */
-
-/*  NDIV   (output) INTEGER */
-/*         Number of divisions. */
-
-/*  IEEE   (input) LOGICAL */
-/*         Flag for IEEE or non IEEE arithmetic (passed to SLASQ5). */
-
-/*  TTYPE  (input/output) INTEGER */
-/*         Shift type.  TTYPE is passed as an argument in order to save */
-/*         its value between calls to SLAZQ3 */
-
-/*  DMIN1  (input/output) REAL */
-/*  DMIN2  (input/output) REAL */
-/*  DN     (input/output) REAL */
-/*  DN1    (input/output) REAL */
-/*  DN2    (input/output) REAL */
-/*  TAU    (input/output) REAL */
-/*         These are passed as arguments in order to save their values */
-/*         between calls to SLAZQ3 */
-
-/*  This is a thread safe version of SLASQ3, which passes TTYPE, DMIN1, */
-/*  DMIN2, DN, DN1. DN2 and TAU through the argument list in place of */
-/*  declaring them in a SAVE statment. */
-
-/*  ===================================================================== */
-
-/*     .. Parameters .. */
-/*     .. */
-/*     .. Local Scalars .. */
-/*     .. */
-/*     .. External Subroutines .. */
-/*     .. */
-/*     .. External Function .. */
-/*     .. */
-/*     .. Intrinsic Functions .. */
-/*     .. */
-/*     .. Executable Statements .. */
-
-    /* Parameter adjustments */
-    --z__;
-
-    /* Function Body */
-    n0in = *n0;
-    eps = slamch_("Precision");
-    safmin = slamch_("Safe minimum");
-    tol = eps * 100.f;
-/* Computing 2nd power */
-    r__1 = tol;
-    tol2 = r__1 * r__1;
-    g = 0.f;
-
-/*     Check for deflation. */
-
-L10:
-
-    if (*n0 < *i0) {
-       return 0;
-    }
-    if (*n0 == *i0) {
-       goto L20;
-    }
-    nn = (*n0 << 2) + *pp;
-    if (*n0 == *i0 + 1) {
-       goto L40;
-    }
-
-/*     Check whether E(N0-1) is negligible, 1 eigenvalue. */
-
-    if (z__[nn - 5] > tol2 * (*sigma + z__[nn - 3]) && z__[nn - (*pp << 1) - 
-           4] > tol2 * z__[nn - 7]) {
-       goto L30;
-    }
-
-L20:
-
-    z__[(*n0 << 2) - 3] = z__[(*n0 << 2) + *pp - 3] + *sigma;
-    --(*n0);
-    goto L10;
-
-/*     Check  whether E(N0-2) is negligible, 2 eigenvalues. */
-
-L30:
-
-    if (z__[nn - 9] > tol2 * *sigma && z__[nn - (*pp << 1) - 8] > tol2 * z__[
-           nn - 11]) {
-       goto L50;
-    }
-
-L40:
-
-    if (z__[nn - 3] > z__[nn - 7]) {
-       s = z__[nn - 3];
-       z__[nn - 3] = z__[nn - 7];
-       z__[nn - 7] = s;
-    }
-    if (z__[nn - 5] > z__[nn - 3] * tol2) {
-       t = (z__[nn - 7] - z__[nn - 3] + z__[nn - 5]) * .5f;
-       s = z__[nn - 3] * (z__[nn - 5] / t);
-       if (s <= t) {
-           s = z__[nn - 3] * (z__[nn - 5] / (t * (sqrt(s / t + 1.f) + 1.f)));
-       } else {
-           s = z__[nn - 3] * (z__[nn - 5] / (t + sqrt(t) * sqrt(t + s)));
-       }
-       t = z__[nn - 7] + (s + z__[nn - 5]);
-       z__[nn - 3] *= z__[nn - 7] / t;
-       z__[nn - 7] = t;
-    }
-    z__[(*n0 << 2) - 7] = z__[nn - 7] + *sigma;
-    z__[(*n0 << 2) - 3] = z__[nn - 3] + *sigma;
-    *n0 += -2;
-    goto L10;
-
-L50:
-
-/*     Reverse the qd-array, if warranted. */
-
-    if (*dmin__ <= 0.f || *n0 < n0in) {
-       if (z__[(*i0 << 2) + *pp - 3] * 1.5f < z__[(*n0 << 2) + *pp - 3]) {
-           ipn4 = *i0 + *n0 << 2;
-           i__1 = *i0 + *n0 - 1 << 1;
-           for (j4 = *i0 << 2; j4 <= i__1; j4 += 4) {
-               temp = z__[j4 - 3];
-               z__[j4 - 3] = z__[ipn4 - j4 - 3];
-               z__[ipn4 - j4 - 3] = temp;
-               temp = z__[j4 - 2];
-               z__[j4 - 2] = z__[ipn4 - j4 - 2];
-               z__[ipn4 - j4 - 2] = temp;
-               temp = z__[j4 - 1];
-               z__[j4 - 1] = z__[ipn4 - j4 - 5];
-               z__[ipn4 - j4 - 5] = temp;
-               temp = z__[j4];
-               z__[j4] = z__[ipn4 - j4 - 4];
-               z__[ipn4 - j4 - 4] = temp;
-/* L60: */
-           }
-           if (*n0 - *i0 <= 4) {
-               z__[(*n0 << 2) + *pp - 1] = z__[(*i0 << 2) + *pp - 1];
-               z__[(*n0 << 2) - *pp] = z__[(*i0 << 2) - *pp];
-           }
-/* Computing MIN */
-           r__1 = *dmin2, r__2 = z__[(*n0 << 2) + *pp - 1];
-           *dmin2 = dmin(r__1,r__2);
-/* Computing MIN */
-           r__1 = z__[(*n0 << 2) + *pp - 1], r__2 = z__[(*i0 << 2) + *pp - 1]
-                   , r__1 = min(r__1,r__2), r__2 = z__[(*i0 << 2) + *pp + 3];
-           z__[(*n0 << 2) + *pp - 1] = dmin(r__1,r__2);
-/* Computing MIN */
-           r__1 = z__[(*n0 << 2) - *pp], r__2 = z__[(*i0 << 2) - *pp], r__1 =
-                    min(r__1,r__2), r__2 = z__[(*i0 << 2) - *pp + 4];
-           z__[(*n0 << 2) - *pp] = dmin(r__1,r__2);
-/* Computing MAX */
-           r__1 = *qmax, r__2 = z__[(*i0 << 2) + *pp - 3], r__1 = max(r__1,
-                   r__2), r__2 = z__[(*i0 << 2) + *pp + 1];
-           *qmax = dmax(r__1,r__2);
-           *dmin__ = -0.f;
-       }
-    }
-
-/* Computing MIN */
-    r__1 = z__[(*n0 << 2) + *pp - 1], r__2 = z__[(*n0 << 2) + *pp - 9], r__1 =
-            min(r__1,r__2), r__2 = *dmin2 + z__[(*n0 << 2) - *pp];
-    if (*dmin__ < 0.f || safmin * *qmax < dmin(r__1,r__2)) {
-
-/*        Choose a shift. */
-
-       slazq4_(i0, n0, &z__[1], pp, &n0in, dmin__, dmin1, dmin2, dn, dn1, 
-               dn2, tau, ttype, &g);
-
-/*        Call dqds until DMIN > 0. */
-
-L80:
-
-       slasq5_(i0, n0, &z__[1], pp, tau, dmin__, dmin1, dmin2, dn, dn1, dn2, 
-               ieee);
-
-       *ndiv += *n0 - *i0 + 2;
-       ++(*iter);
-
-/*        Check status. */
-
-       if (*dmin__ >= 0.f && *dmin1 > 0.f) {
-
-/*           Success. */
-
-           goto L100;
-
-       } else if (*dmin__ < 0.f && *dmin1 > 0.f && z__[(*n0 - 1 << 2) - *pp] 
-               < tol * (*sigma + *dn1) && dabs(*dn) < tol * *sigma) {
-
-/*           Convergence hidden by negative DN. */
-
-           z__[(*n0 - 1 << 2) - *pp + 2] = 0.f;
-           *dmin__ = 0.f;
-           goto L100;
-       } else if (*dmin__ < 0.f) {
-
-/*           TAU too big. Select new TAU and try again. */
-
-           ++(*nfail);
-           if (*ttype < -22) {
-
-/*              Failed twice. Play it safe. */
-
-               *tau = 0.f;
-           } else if (*dmin1 > 0.f) {
-
-/*              Late failure. Gives excellent shift. */
-
-               *tau = (*tau + *dmin__) * (1.f - eps * 2.f);
-               *ttype += -11;
-           } else {
-
-/*              Early failure. Divide by 4. */
-
-               *tau *= .25f;
-               *ttype += -12;
-           }
-           goto L80;
-       } else if (*dmin__ != *dmin__) {
-
-/*           NaN. */
-
-           *tau = 0.f;
-           goto L80;
-       } else {
-
-/*           Possible underflow. Play it safe. */
-
-           goto L90;
-       }
-    }
-
-/*     Risk of underflow. */
-
-L90:
-    slasq6_(i0, n0, &z__[1], pp, dmin__, dmin1, dmin2, dn, dn1, dn2);
-    *ndiv += *n0 - *i0 + 2;
-    ++(*iter);
-    *tau = 0.f;
-
-L100:
-    if (*tau < *sigma) {
-       *desig += *tau;
-       t = *sigma + *desig;
-       *desig -= t - *sigma;
-    } else {
-       t = *sigma + *tau;
-       *desig = *sigma - (t - *tau) + *desig;
-    }
-    *sigma = t;
-
-    return 0;
-
-/*     End of SLAZQ3 */
-
-} /* slazq3_ */
diff --git a/3rdparty/lapack/slazq4.c b/3rdparty/lapack/slazq4.c
deleted file mode 100644 (file)
index 7c93f19..0000000
+++ /dev/null
@@ -1,386 +0,0 @@
-#include "clapack.h"
-
-/* Subroutine */ int slazq4_(integer *i0, integer *n0, real *z__, integer *pp, 
-        integer *n0in, real *dmin__, real *dmin1, real *dmin2, real *dn, 
-       real *dn1, real *dn2, real *tau, integer *ttype, real *g)
-{
-    /* System generated locals */
-    integer i__1;
-    real r__1, r__2;
-
-    /* Builtin functions */
-    double sqrt(doublereal);
-
-    /* Local variables */
-    real s, a2, b1, b2;
-    integer i4, nn, np;
-    real gam, gap1, gap2;
-
-
-/*  -- LAPACK auxiliary routine (version 3.1) -- */
-/*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
-/*     November 2006 */
-
-/*     .. Scalar Arguments .. */
-/*     .. */
-/*     .. Array Arguments .. */
-/*     .. */
-
-/*  Purpose */
-/*  ======= */
-
-/*  SLAZQ4 computes an approximation TAU to the smallest eigenvalue */
-/*  using values of d from the previous transform. */
-
-/*  I0    (input) INTEGER */
-/*        First index. */
-
-/*  N0    (input) INTEGER */
-/*        Last index. */
-
-/*  Z     (input) REAL array, dimension ( 4*N ) */
-/*        Z holds the qd array. */
-
-/*  PP    (input) INTEGER */
-/*        PP=0 for ping, PP=1 for pong. */
-
-/*  N0IN  (input) INTEGER */
-/*        The value of N0 at start of EIGTEST. */
-
-/*  DMIN  (input) REAL */
-/*        Minimum value of d. */
-
-/*  DMIN1 (input) REAL */
-/*        Minimum value of d, excluding D( N0 ). */
-
-/*  DMIN2 (input) REAL */
-/*        Minimum value of d, excluding D( N0 ) and D( N0-1 ). */
-
-/*  DN    (input) REAL */
-/*        d(N) */
-
-/*  DN1   (input) REAL */
-/*        d(N-1) */
-
-/*  DN2   (input) REAL */
-/*        d(N-2) */
-
-/*  TAU   (output) REAL */
-/*        This is the shift. */
-
-/*  TTYPE (output) INTEGER */
-/*        Shift type. */
-
-/*  G     (input/output) REAL */
-/*        G is passed as an argument in order to save its value between */
-/*        calls to SLAZQ4 */
-
-/*  Further Details */
-/*  =============== */
-/*  CNST1 = 9/16 */
-
-/*  This is a thread safe version of SLASQ4, which passes G through the */
-/*  argument list in place of declaring G in a SAVE statment. */
-
-/*  ===================================================================== */
-
-/*     .. Parameters .. */
-/*     .. */
-/*     .. Local Scalars .. */
-/*     .. */
-/*     .. Intrinsic Functions .. */
-/*     .. */
-/*     .. Executable Statements .. */
-
-/*     A negative DMIN forces the shift to take that absolute value */
-/*     TTYPE records the type of shift. */
-
-    /* Parameter adjustments */
-    --z__;
-
-    /* Function Body */
-    if (*dmin__ <= 0.f) {
-       *tau = -(*dmin__);
-       *ttype = -1;
-       return 0;
-    }
-
-    nn = (*n0 << 2) + *pp;
-    if (*n0in == *n0) {
-
-/*        No eigenvalues deflated. */
-
-       if (*dmin__ == *dn || *dmin__ == *dn1) {
-
-           b1 = sqrt(z__[nn - 3]) * sqrt(z__[nn - 5]);
-           b2 = sqrt(z__[nn - 7]) * sqrt(z__[nn - 9]);
-           a2 = z__[nn - 7] + z__[nn - 5];
-
-/*           Cases 2 and 3. */
-
-           if (*dmin__ == *dn && *dmin1 == *dn1) {
-               gap2 = *dmin2 - a2 - *dmin2 * .25f;
-               if (gap2 > 0.f && gap2 > b2) {
-                   gap1 = a2 - *dn - b2 / gap2 * b2;
-               } else {
-                   gap1 = a2 - *dn - (b1 + b2);
-               }
-               if (gap1 > 0.f && gap1 > b1) {
-/* Computing MAX */
-                   r__1 = *dn - b1 / gap1 * b1, r__2 = *dmin__ * .5f;
-                   s = dmax(r__1,r__2);
-                   *ttype = -2;
-               } else {
-                   s = 0.f;
-                   if (*dn > b1) {
-                       s = *dn - b1;
-                   }
-                   if (a2 > b1 + b2) {
-/* Computing MIN */
-                       r__1 = s, r__2 = a2 - (b1 + b2);
-                       s = dmin(r__1,r__2);
-                   }
-/* Computing MAX */
-                   r__1 = s, r__2 = *dmin__ * .333f;
-                   s = dmax(r__1,r__2);
-                   *ttype = -3;
-               }
-           } else {
-
-/*              Case 4. */
-
-               *ttype = -4;
-               s = *dmin__ * .25f;
-               if (*dmin__ == *dn) {
-                   gam = *dn;
-                   a2 = 0.f;
-                   if (z__[nn - 5] > z__[nn - 7]) {
-                       return 0;
-                   }
-                   b2 = z__[nn - 5] / z__[nn - 7];
-                   np = nn - 9;
-               } else {
-                   np = nn - (*pp << 1);
-                   b2 = z__[np - 2];
-                   gam = *dn1;
-                   if (z__[np - 4] > z__[np - 2]) {
-                       return 0;
-                   }
-                   a2 = z__[np - 4] / z__[np - 2];
-                   if (z__[nn - 9] > z__[nn - 11]) {
-                       return 0;
-                   }
-                   b2 = z__[nn - 9] / z__[nn - 11];
-                   np = nn - 13;
-               }
-
-/*              Approximate contribution to norm squared from I < NN-1. */
-
-               a2 += b2;
-               i__1 = (*i0 << 2) - 1 + *pp;
-               for (i4 = np; i4 >= i__1; i4 += -4) {
-                   if (b2 == 0.f) {
-                       goto L20;
-                   }
-                   b1 = b2;
-                   if (z__[i4] > z__[i4 - 2]) {
-                       return 0;
-                   }
-                   b2 *= z__[i4] / z__[i4 - 2];
-                   a2 += b2;
-                   if (dmax(b2,b1) * 100.f < a2 || .563f < a2) {
-                       goto L20;
-                   }
-/* L10: */
-               }
-L20:
-               a2 *= 1.05f;
-
-/*              Rayleigh quotient residual bound. */
-
-               if (a2 < .563f) {
-                   s = gam * (1.f - sqrt(a2)) / (a2 + 1.f);
-               }
-           }
-       } else if (*dmin__ == *dn2) {
-
-/*           Case 5. */
-
-           *ttype = -5;
-           s = *dmin__ * .25f;
-
-/*           Compute contribution to norm squared from I > NN-2. */
-
-           np = nn - (*pp << 1);
-           b1 = z__[np - 2];
-           b2 = z__[np - 6];
-           gam = *dn2;
-           if (z__[np - 8] > b2 || z__[np - 4] > b1) {
-               return 0;
-           }
-           a2 = z__[np - 8] / b2 * (z__[np - 4] / b1 + 1.f);
-
-/*           Approximate contribution to norm squared from I < NN-2. */
-
-           if (*n0 - *i0 > 2) {
-               b2 = z__[nn - 13] / z__[nn - 15];
-               a2 += b2;
-               i__1 = (*i0 << 2) - 1 + *pp;
-               for (i4 = nn - 17; i4 >= i__1; i4 += -4) {
-                   if (b2 == 0.f) {
-                       goto L40;
-                   }
-                   b1 = b2;
-                   if (z__[i4] > z__[i4 - 2]) {
-                       return 0;
-                   }
-                   b2 *= z__[i4] / z__[i4 - 2];
-                   a2 += b2;
-                   if (dmax(b2,b1) * 100.f < a2 || .563f < a2) {
-                       goto L40;
-                   }
-/* L30: */
-               }
-L40:
-               a2 *= 1.05f;
-           }
-
-           if (a2 < .563f) {
-               s = gam * (1.f - sqrt(a2)) / (a2 + 1.f);
-           }
-       } else {
-
-/*           Case 6, no information to guide us. */
-
-           if (*ttype == -6) {
-               *g += (1.f - *g) * .333f;
-           } else if (*ttype == -18) {
-               *g = .083250000000000005f;
-           } else {
-               *g = .25f;
-           }
-           s = *g * *dmin__;
-           *ttype = -6;
-       }
-
-    } else if (*n0in == *n0 + 1) {
-
-/*        One eigenvalue just deflated. Use DMIN1, DN1 for DMIN and DN. */
-
-       if (*dmin1 == *dn1 && *dmin2 == *dn2) {
-
-/*           Cases 7 and 8. */
-
-           *ttype = -7;
-           s = *dmin1 * .333f;
-           if (z__[nn - 5] > z__[nn - 7]) {
-               return 0;
-           }
-           b1 = z__[nn - 5] / z__[nn - 7];
-           b2 = b1;
-           if (b2 == 0.f) {
-               goto L60;
-           }
-           i__1 = (*i0 << 2) - 1 + *pp;
-           for (i4 = (*n0 << 2) - 9 + *pp; i4 >= i__1; i4 += -4) {
-               a2 = b1;
-               if (z__[i4] > z__[i4 - 2]) {
-                   return 0;
-               }
-               b1 *= z__[i4] / z__[i4 - 2];
-               b2 += b1;
-               if (dmax(b1,a2) * 100.f < b2) {
-                   goto L60;
-               }
-/* L50: */
-           }
-L60:
-           b2 = sqrt(b2 * 1.05f);
-/* Computing 2nd power */
-           r__1 = b2;
-           a2 = *dmin1 / (r__1 * r__1 + 1.f);
-           gap2 = *dmin2 * .5f - a2;
-           if (gap2 > 0.f && gap2 > b2 * a2) {
-/* Computing MAX */
-               r__1 = s, r__2 = a2 * (1.f - a2 * 1.01f * (b2 / gap2) * b2);
-               s = dmax(r__1,r__2);
-           } else {
-/* Computing MAX */
-               r__1 = s, r__2 = a2 * (1.f - b2 * 1.01f);
-               s = dmax(r__1,r__2);
-               *ttype = -8;
-           }
-       } else {
-
-/*           Case 9. */
-
-           s = *dmin1 * .25f;
-           if (*dmin1 == *dn1) {
-               s = *dmin1 * .5f;
-           }
-           *ttype = -9;
-       }
-
-    } else if (*n0in == *n0 + 2) {
-
-/*        Two eigenvalues deflated. Use DMIN2, DN2 for DMIN and DN. */
-
-/*        Cases 10 and 11. */
-
-       if (*dmin2 == *dn2 && z__[nn - 5] * 2.f < z__[nn - 7]) {
-           *ttype = -10;
-           s = *dmin2 * .333f;
-           if (z__[nn - 5] > z__[nn - 7]) {
-               return 0;
-           }
-           b1 = z__[nn - 5] / z__[nn - 7];
-           b2 = b1;
-           if (b2 == 0.f) {
-               goto L80;
-           }
-           i__1 = (*i0 << 2) - 1 + *pp;
-           for (i4 = (*n0 << 2) - 9 + *pp; i4 >= i__1; i4 += -4) {
-               if (z__[i4] > z__[i4 - 2]) {
-                   return 0;
-               }
-               b1 *= z__[i4] / z__[i4 - 2];
-               b2 += b1;
-               if (b1 * 100.f < b2) {
-                   goto L80;
-               }
-/* L70: */
-           }
-L80:
-           b2 = sqrt(b2 * 1.05f);
-/* Computing 2nd power */
-           r__1 = b2;
-           a2 = *dmin2 / (r__1 * r__1 + 1.f);
-           gap2 = z__[nn - 7] + z__[nn - 9] - sqrt(z__[nn - 11]) * sqrt(z__[
-                   nn - 9]) - a2;
-           if (gap2 > 0.f && gap2 > b2 * a2) {
-/* Computing MAX */
-               r__1 = s, r__2 = a2 * (1.f - a2 * 1.01f * (b2 / gap2) * b2);
-               s = dmax(r__1,r__2);
-           } else {
-/* Computing MAX */
-               r__1 = s, r__2 = a2 * (1.f - b2 * 1.01f);
-               s = dmax(r__1,r__2);
-           }
-       } else {
-           s = *dmin2 * .25f;
-           *ttype = -11;
-       }
-    } else if (*n0in > *n0 + 2) {
-
-/*        Case 12, more than two eigenvalues deflated. No information. */
-
-       s = 0.f;
-       *ttype = -12;
-    }
-
-    *tau = s;
-    return 0;
-
-/*     End of SLAZQ4 */
-
-} /* slazq4_ */
index 7e9a209..3184861 100644 (file)
@@ -1,5 +1,18 @@
+/* snrm2.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 doublereal snrm2_(integer *n, real *x, integer *incx)
 {
     /* System generated locals */
index 00b4143..041c03c 100644 (file)
@@ -1,5 +1,18 @@
+/* sorg2r.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -18,7 +31,7 @@ static integer c__1 = 1;
            real *, integer *, real *), xerbla_(char *, integer *);
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 97c86be..1b6b59b 100644 (file)
@@ -1,5 +1,18 @@
+/* sorgbr.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -28,7 +41,7 @@ static integer c_n1 = -1;
     logical lquery;
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 5a329f7..f11a840 100644 (file)
@@ -1,5 +1,18 @@
+/* sorgl2.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int sorgl2_(integer *m, integer *n, integer *k, real *a, 
        integer *lda, real *tau, real *work, integer *info)
 {
@@ -14,7 +27,7 @@
            real *, integer *, real *), xerbla_(char *, integer *);
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index ac33ca5..846562f 100644 (file)
@@ -1,5 +1,18 @@
+/* sorglq.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -27,7 +40,7 @@ static integer c__2 = 2;
     logical lquery;
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 93c7a32..bbdb54e 100644 (file)
@@ -1,5 +1,18 @@
+/* sorgqr.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -27,7 +40,7 @@ static integer c__2 = 2;
     logical lquery;
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 8bca56b..0665799 100644 (file)
@@ -1,5 +1,18 @@
+/* sorm2l.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -22,7 +35,7 @@ static integer c__1 = 1;
     logical notran;
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index b2eb677..8b6d727 100644 (file)
@@ -1,5 +1,18 @@
+/* sorm2r.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -22,7 +35,7 @@ static integer c__1 = 1;
     logical notran;
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 65191ca..24023b8 100644 (file)
@@ -1,5 +1,18 @@
+/* sormbr.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -38,7 +51,7 @@ static integer c__2 = 2;
            integer *, integer *);
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index f95585a..63a84c7 100644 (file)
@@ -1,5 +1,18 @@
+/* sorml2.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int sorml2_(char *side, char *trans, integer *m, integer *n, 
        integer *k, real *a, integer *lda, real *tau, real *c__, integer *ldc, 
         real *work, integer *info)
@@ -18,7 +31,7 @@
     logical notran;
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index a112bcb..cb4aefb 100644 (file)
@@ -1,5 +1,18 @@
+/* sormlq.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -43,7 +56,7 @@ static integer c__65 = 65;
     logical lquery;
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 64e47c2..8e4f247 100644 (file)
@@ -1,5 +1,18 @@
+/* sormql.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -41,7 +54,7 @@ static integer c__65 = 65;
     logical lquery;
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index fc3e468..a792f45 100644 (file)
@@ -1,5 +1,18 @@
+/* sormqr.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -41,7 +54,7 @@ static integer c__65 = 65;
     logical lquery;
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 7ce4207..2ef533a 100644 (file)
@@ -1,5 +1,18 @@
+/* sormtr.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -37,7 +50,7 @@ static integer c__2 = 2;
            integer *, integer *);
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index a21af39..ae8d93d 100644 (file)
@@ -1,5 +1,18 @@
+/* spotf2.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -26,9 +39,10 @@ static real c_b12 = 1.f;
            real *, integer *, real *, real *, integer *);
     logical upper;
     extern /* Subroutine */ int xerbla_(char *, integer *);
+    extern logical sisnan_(real *);
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
@@ -139,7 +153,7 @@ static real c_b12 = 1.f;
            i__2 = j - 1;
            ajj = a[j + j * a_dim1] - sdot_(&i__2, &a[j * a_dim1 + 1], &c__1, 
                    &a[j * a_dim1 + 1], &c__1);
-           if (ajj <= 0.f) {
+           if (ajj <= 0.f || sisnan_(&ajj)) {
                a[j + j * a_dim1] = ajj;
                goto L30;
            }
@@ -172,7 +186,7 @@ static real c_b12 = 1.f;
            i__2 = j - 1;
            ajj = a[j + j * a_dim1] - sdot_(&i__2, &a[j + a_dim1], lda, &a[j 
                    + a_dim1], lda);
-           if (ajj <= 0.f) {
+           if (ajj <= 0.f || sisnan_(&ajj)) {
                a[j + j * a_dim1] = ajj;
                goto L30;
            }
index e777744..3029f61 100644 (file)
@@ -1,5 +1,18 @@
+/* spotrf.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -30,7 +43,7 @@ static real c_b14 = 1.f;
            integer *, integer *);
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 04e8908..c098722 100644 (file)
@@ -1,5 +1,18 @@
+/* spotri.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int spotri_(char *uplo, integer *n, real *a, integer *lda, 
        integer *info)
 {
@@ -13,7 +26,7 @@
            char *, char *, integer *, real *, integer *, integer *);
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index e71ce75..21640ed 100644 (file)
@@ -1,5 +1,18 @@
+/* spotrs.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static real c_b9 = 1.f;
@@ -18,7 +31,7 @@ static real c_b9 = 1.f;
 ), xerbla_(char *, integer *);
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 6436243..e57d5ba 100644 (file)
@@ -1,5 +1,18 @@
+/* srot.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int srot_(integer *n, real *sx, integer *incx, real *sy, 
        integer *incy, real *c__, real *s)
 {
index ad2a46c..890967e 100644 (file)
@@ -1,5 +1,18 @@
+/* sscal.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int sscal_(integer *n, real *sa, real *sx, integer *incx)
 {
     /* System generated locals */
index 4dd5ab5..f13dc26 100644 (file)
@@ -1,5 +1,18 @@
+/* sstebz.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -57,7 +70,7 @@ static integer c__0 = 0;
     logical toofew;
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 /*     8-18-00:  Increase FUDGE factor for T3E (eca) */
index fd8e317..365e40f 100644 (file)
@@ -1,5 +1,18 @@
+/* sstein.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__2 = 2;
@@ -49,7 +62,7 @@ static integer c_n1 = -1;
     real stpcrt;
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 4fe99b1..c4936e2 100644 (file)
@@ -1,5 +1,18 @@
+/* sstemr.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -84,7 +97,7 @@ static real c_b18 = .003f;
     logical lquery, zquery;
 
 
-/*  -- LAPACK computational routine (version 3.1) -- */
+/*  -- LAPACK computational routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
@@ -321,7 +334,6 @@ static real c_b18 = .003f;
 
     lquery = *lwork == -1 || *liwork == -1;
     zquery = *nzc == -1;
-    *tryrac = *info != 0;
 /*     SSTEMR needs WORK of size 6*N, IWORK of size 3*N. */
 /*     In addition, SLARRE needs WORK of size 6*N, IWORK of size 5*N. */
 /*     Furthermore, SLARRV needs WORK of size 12*N, IWORK of size 7*N. */
index 6af39d8..9f92562 100644 (file)
@@ -1,5 +1,18 @@
+/* ssteqr.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static real c_b9 = 0.f;
@@ -55,7 +68,7 @@ static integer c__2 = 2;
     extern /* Subroutine */ int slasrt_(char *, integer *, real *, integer *);
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 0c70d82..262a8c5 100644 (file)
@@ -1,5 +1,18 @@
+/* ssterf.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__0 = 0;
@@ -44,7 +57,7 @@ static real c_b32 = 1.f;
     extern /* Subroutine */ int slasrt_(char *, integer *, real *, integer *);
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 80c78c9..cb4ebfe 100644 (file)
@@ -1,5 +1,18 @@
+/* sswap.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int sswap_(integer *n, real *sx, integer *incx, real *sy, 
        integer *incy)
 {
index 93a2e49..5797264 100644 (file)
@@ -1,5 +1,18 @@
+/* ssyevr.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__10 = 10;
@@ -76,7 +89,7 @@ static integer c_n1 = -1;
            integer *, integer *);
 
 
-/*  -- LAPACK driver routine (version 3.1) -- */
+/*  -- LAPACK driver routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index faf18bc..4280729 100644 (file)
@@ -1,5 +1,18 @@
+/* ssymv.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int ssymv_(char *uplo, integer *n, real *alpha, real *a, 
        integer *lda, real *x, integer *incx, real *beta, real *y, integer *
        incy)
index 3911da8..9aea514 100644 (file)
@@ -1,5 +1,18 @@
+/* ssyr2.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int ssyr2_(char *uplo, integer *n, real *alpha, real *x, 
        integer *incx, real *y, integer *incy, real *a, integer *lda)
 {
index 9980205..2abedb3 100644 (file)
@@ -1,5 +1,18 @@
+/* ssyr2k.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int ssyr2k_(char *uplo, char *trans, integer *n, integer *k, 
        real *alpha, real *a, integer *lda, real *b, integer *ldb, real *beta, 
         real *c__, integer *ldc)
index 1796205..528422c 100644 (file)
@@ -1,5 +1,18 @@
+/* ssyrk.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int ssyrk_(char *uplo, char *trans, integer *n, integer *k, 
        real *alpha, real *a, integer *lda, real *beta, real *c__, integer *
        ldc)
index 41ecb17..f7bb3c1 100644 (file)
@@ -1,5 +1,18 @@
+/* ssytd2.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -28,7 +41,7 @@ static real c_b14 = -1.f;
            real *, integer *, real *);
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 0735c88..726c5b3 100644 (file)
@@ -1,5 +1,18 @@
+/* ssytrd.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -34,7 +47,7 @@ static real c_b23 = 1.f;
     logical lquery;
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index e964450..bf83224 100644 (file)
@@ -1,5 +1,18 @@
+/* strmm.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int strmm_(char *side, char *uplo, char *transa, char *diag, 
        integer *m, integer *n, real *alpha, real *a, integer *lda, real *b, 
        integer *ldb)
 
 /*     Quick return if possible. */
 
-    if (*n == 0) {
+    if (*m == 0 || *n == 0) {
        return 0;
     }
 
index 704309f..b3cf7f0 100644 (file)
@@ -1,5 +1,18 @@
+/* strmv.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int strmv_(char *uplo, char *trans, char *diag, integer *n, 
        real *a, integer *lda, real *x, integer *incx)
 {
index ade388f..8a6ff18 100644 (file)
@@ -1,5 +1,18 @@
+/* strsm.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Subroutine */ int strsm_(char *side, char *uplo, char *transa, char *diag, 
        integer *m, integer *n, real *alpha, real *a, integer *lda, real *b, 
        integer *ldb)
 
 /*     Quick return if possible. */
 
-    if (*n == 0) {
+    if (*m == 0 || *n == 0) {
        return 0;
     }
 
index 40b283f..25f0a3e 100644 (file)
@@ -1,5 +1,18 @@
+/* strti2.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -22,7 +35,7 @@ static integer c__1 = 1;
     logical nounit;
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 052fc11..4737472 100644 (file)
@@ -1,5 +1,18 @@
+/* strtri.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static integer c__1 = 1;
@@ -35,7 +48,7 @@ static real c_b22 = -1.f;
     logical nounit;
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 019bb2e..b4660eb 100644 (file)
@@ -1,5 +1,18 @@
+/* strtrs.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
 
+
 /* Table of constant values */
 
 static real c_b12 = 1.f;
@@ -19,7 +32,7 @@ static real c_b12 = 1.f;
     logical nounit;
 
 
-/*  -- LAPACK routine (version 3.1) -- */
+/*  -- LAPACK routine (version 3.2) -- */
 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
 /*     November 2006 */
 
index 29c5a38..01687e1 100644 (file)
@@ -1,43 +1,76 @@
+/* xerbla.f -- translated by f2c (version 20061008).
+   You must link the resulting object file with libf2c:
+       on Microsoft Windows system, link with libf2c.lib;
+       on Linux or Unix systems, link with .../path/to/libf2c.a -lm
+       or, if you install libf2c.a in a standard place, with -lf2c -lm
+       -- in that order, at the end of the command line, as in
+               cc *.o -lf2c -lm
+       Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
+
+               http://www.netlib.org/f2c/libf2c.zip
+*/
+
 #include "clapack.h"
-#include "stdio.h"
+
+
+/* Table of constant values */
+
+static integer c__1 = 1;
 
 /* Subroutine */ int xerbla_(char *srname, integer *info)
 {
-/*  -- LAPACK auxiliary routine (version 2.0) --   
-       Univ. of Tennessee, Univ. of California Berkeley, NAG Ltd.,   
-       Courant Institute, Argonne National Lab, and Rice University   
-       September 30, 1994   
+    /* Format strings */
+    static char fmt_9999[] = "(\002 ** On entry to \002,a,\002 parameter num"
+           "ber \002,i2,\002 had \002,\002an illegal value\002)";
 
+    /* Builtin functions */
+    integer s_wsfe(cilist *), i_len_trim(char *, ftnlen), do_fio(integer *, 
+           char *, ftnlen), e_wsfe(void);
+    /* Subroutine */ int s_stop(char *, ftnlen);
 
-    Purpose   
-    =======   
+    /* Fortran I/O blocks */
+    static cilist io___1 = { 0, 6, 0, fmt_9999, 0 };
 
-    XERBLA  is an error handler for the LAPACK routines.   
-    It is called by an LAPACK routine if an input parameter has an   
-    invalid value.  A message is printed and execution stops.   
 
-    Installers may consider modifying the STOP statement in order to   
-    call system-specific exception-handling facilities.   
 
-    Arguments   
-    =========   
+/*  -- LAPACK auxiliary routine (preliminary version) -- */
+/*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
+/*     November 2006 */
 
-    SRNAME  (input) CHARACTER*6   
-            The name of the routine which called XERBLA.   
+/*     .. Scalar Arguments .. */
+/*     .. */
 
-    INFO    (input) INTEGER   
-            The position of the invalid parameter in the parameter list   
+/*  Purpose */
+/*  ======= */
 
-            of the calling routine.   
+/*  XERBLA  is an error handler for the LAPACK routines. */
+/*  It is called by an LAPACK routine if an input parameter has an */
+/*  invalid value.  A message is printed and execution stops. */
 
-   ===================================================================== 
-*/
+/*  Installers may consider modifying the STOP statement in order to */
+/*  call system-specific exception-handling facilities. */
 
-    printf("** On entry to %6s, parameter number %2i had an illegal value\n",
+/*  Arguments */
+/*  ========= */
+
+/*  SRNAME  (input) CHARACTER*(*) */
+/*          The name of the routine which called XERBLA. */
+
+/*  INFO    (input) INTEGER */
+/*          The position of the invalid parameter in the parameter list */
+/*          of the calling routine. */
+
+/* ===================================================================== */
+
+/*     .. Intrinsic Functions .. */
+/*     .. */
+/*     .. Executable Statements .. */
+
+       printf("** On entry to %6s, parameter number %2i had an illegal value\n",
                srname, *info);
 
+
 /*     End of XERBLA */
 
     return 0;
 } /* xerbla_ */
-
index 30628a1..0564cae 100644 (file)
@@ -26,7 +26,8 @@ set(the_target "libjasper")
 add_library(${the_target} STATIC ${lib_srcs} ${lib_hdrs} ${lib_ext_hdrs})
 
 if(MSVC)
-    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /W3")
+    string(REPLACE "/W3" "/W0" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
+    string(REPLACE "/W4" "/W0" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
     add_definitions(-DJAS_WIN_MSVC_BUILD)
 endif()
 
index d801b32..31050bf 100644 (file)
@@ -36,6 +36,9 @@ extern char * getenv JPP((const char * name));
 #endif
 #endif
 
+#if defined _MSC_VER && _MSC_VER >= 1400
+#pragma warning(disable: 4267)
+#endif
 
 /*
  * Some important notes:
index fd2dbb1..4cf825f 100644 (file)
-README for libpng version 1.2.29 - May 8, 2008 (shared library 12.0)
-See the note about version numbers near the top of png.h
-
-See INSTALL for instructions on how to install libpng.
-
-Libpng comes in several distribution formats.  Get libpng-*.tar.gz,
-libpng-*.tar.lzma, or libpng-*.tar.bz2 if you want UNIX-style line
-endings in the text files, or lpng*.7z or lpng*.zip if you want DOS-style
-line endings.
-
-Version 0.89 was the first official release of libpng.  Don't let the
-fact that it's the first release fool you.  The libpng library has been in
-extensive use and testing since mid-1995.  By late 1997 it had
-finally gotten to the stage where there hadn't been significant
-changes to the API in some time, and people have a bad feeling about
-libraries with versions < 1.0.  Version 1.0.0 was released in
-March 1998.
-
-****
-Note that some of the changes to the png_info structure render this
-version of the library binary incompatible with libpng-0.89 or
-earlier versions if you are using a shared library.  The type of the
-"filler" parameter for png_set_filler() has changed from png_byte to
-png_uint_32, which will affect shared-library applications that use
-this function.
-
-To avoid problems with changes to the internals of png_info_struct,
-new APIs have been made available in 0.95 to avoid direct application
-access to info_ptr.  These functions are the png_set_<chunk> and
-png_get_<chunk> functions.  These functions should be used when
-accessing/storing the info_struct data, rather than manipulating it
-directly, to avoid such problems in the future.
-
-It is important to note that the APIs do not make current programs
-that access the info struct directly incompatible with the new
-library.  However, it is strongly suggested that new programs use
-the new APIs (as shown in example.c and pngtest.c), and older programs
-be converted to the new format, to facilitate upgrades in the future.
-****
-
-Additions since 0.90 include the ability to compile libpng as a
-Windows DLL, and new APIs for accessing data in the info struct.
-Experimental functions include the ability to set weighting and cost
-factors for row filter selection, direct reads of integers from buffers
-on big-endian processors that support misaligned data access, faster
-methods of doing alpha composition, and more accurate 16->8 bit color
-conversion.
-
-The additions since 0.89 include the ability to read from a PNG stream
-which has had some (or all) of the signature bytes read by the calling
-application.  This also allows the reading of embedded PNG streams that
-do not have the PNG file signature.  As well, it is now possible to set
-the library action on the detection of chunk CRC errors.  It is possible
-to set different actions based on whether the CRC error occurred in a
-critical or an ancillary chunk.
-
-The changes made to the library, and bugs fixed are based on discussions
-on the PNG-implement mailing list
-and not on material submitted privately to Guy, Andreas, or Glenn.  They will
-forward any good suggestions to the list.
-
-For a detailed description on using libpng, read libpng.txt.  For
-examples of libpng in a program, see example.c and pngtest.c.  For usage
-information and restrictions (what little they are) on libpng, see
-png.h.  For a description on using zlib (the compression library used by
-libpng) and zlib's restrictions, see zlib.h
-
-I have included a general makefile, as well as several machine and
-compiler specific ones, but you may have to modify one for your own needs.
-
-You should use zlib 1.0.4 or later to run this, but it MAY work with
-versions as old as zlib 0.95.  Even so, there are bugs in older zlib
-versions which can cause the output of invalid compression streams for
-some images.  You will definitely need zlib 1.0.4 or later if you are
-taking advantage of the MS-DOS "far" structure allocation for the small
-and medium memory models.  You should also note that zlib is a
-compression library that is useful for more things than just PNG files.
-You can use zlib as a drop-in replacement for fread() and fwrite() if
-you are so inclined.
-
-zlib should be available at the same place that libpng is, or at
-ftp://ftp.simplesystems.org/pub/png/src/
-
-You may also want a copy of the PNG specification.  It is available
-as an RFC, a W3C Recommendation, and an ISO/IEC Standard.  You can find
-these at http://www.libpng.org/pub/png/pngdocs.html
-
-This code is currently being archived at libpng.sf.net in the
-[DOWNLOAD] area, and on CompuServe, Lib 20 (PNG SUPPORT)
-at GO GRAPHSUP.  If you can't find it in any of those places,
-e-mail me, and I'll help you find it.
-
-If you have any code changes, requests, problems, etc., please e-mail
-them to me.  Also, I'd appreciate any make files or project files,
-and any modifications you needed to make to get libpng to compile,
-along with a #define variable to tell what compiler/system you are on.
-If you needed to add transformations to libpng, or wish libpng would
-provide the image in a different way, drop me a note (and code, if
-possible), so I can consider supporting the transformation.
-Finally, if you get any warning messages when compiling libpng
-(note: not zlib), and they are easy to fix, I'd appreciate the
-fix.  Please mention "libpng" somewhere in the subject line.  Thanks.
-
-This release was created and will be supported by myself (of course
-based in a large way on Guy's and Andreas' earlier work), and the PNG group.
-
-Send comments/corrections/commendations to png-mng-implement at lists.sf.net
-(subscription required; visit 
-https://lists.sourceforge.net/lists/listinfo/png-mng-implement
-to subscribe) or to glennrp at users.sourceforge.net
-
-You can't reach Guy, the original libpng author, at the addresses
-given in previous versions of this document.  He and Andreas will read mail
-addressed to the png-implement list, however.
-
-Please do not send general questions about PNG.  Send them to
-the (png-mng-misc at lists.sourceforge.net, subscription required, visit
-https://lists.sourceforge.net/lists/listinfo/png-mng-implement to subscribe)
-On the other hand,
-please do not send libpng questions to that address, send them to me
-or to the png-implement list.  I'll
-get them in the end anyway.  If you have a question about something
-in the PNG specification that is related to using libpng, send it
-to me.  Send me any questions that start with "I was using libpng,
-and ...".  If in doubt, send questions to me.  I'll bounce them
-to others, if necessary.
-
-Please do not send suggestions on how to change PNG.  We have
-been discussing PNG for twelve years now, and it is official and
-finished.  If you have suggestions for libpng, however, I'll
-gladly listen.  Even if your suggestion is not used immediately,
-it may be used later.
-
-Files in this distribution:
-
-      ANNOUNCE      =>  Announcement of this version, with recent changes
-      CHANGES       =>  Description of changes between libpng versions
-      KNOWNBUG      =>  List of known bugs and deficiencies
-      LICENSE       =>  License to use and redistribute libpng
-      README        =>  This file
-      TODO          =>  Things not implemented in the current library
-      Y2KINFO       =>  Statement of Y2K compliance
-      example.c     =>  Example code for using libpng functions
-      libpng-*-*-diff.txt => Diff from previous release
-      libpng.3      =>  manual page for libpng (includes libpng.txt)
-      libpng.txt    =>  Description of libpng and its functions
-      libpngpf.3    =>  manual page for libpng's private functions
-      png.5         =>  manual page for the PNG format
-      png.c         =>  Basic interface functions common to library
-      png.h         =>  Library function and interface declarations
-      pngconf.h     =>  System specific library configuration
-      pngerror.c    =>  Error/warning message I/O functions
-      pngget.c      =>  Functions for retrieving info from struct
-      pngmem.c      =>  Memory handling functions
-      pngbar.png    =>  PNG logo, 88x31
-      pngnow.png    =>  PNG logo, 98x31
-      pngpread.c    =>  Progressive reading functions
-      pngread.c     =>  Read data/helper high-level functions
-      pngrio.c      =>  Lowest-level data read I/O functions
-      pngrtran.c    =>  Read data transformation functions
-      pngrutil.c    =>  Read data utility functions
-      pngset.c      =>  Functions for storing data into the info_struct
-      pngtest.c     =>  Library test program
-      pngtest.png   =>  Library test sample image
-      pngtrans.c    =>  Common data transformation functions
-      pngwio.c      =>  Lowest-level write I/O functions
-      pngwrite.c    =>  High-level write functions
-      pngwtran.c    =>  Write data transformations
-      pngwutil.c    =>  Write utility functions
-      contrib       =>  Contributions
-       gregbook         =>  source code for PNG reading and writing, from
-                            Greg Roelofs' "PNG: The Definitive Guide",
-                            O'Reilly, 1999
-       msvctest     =>  Builds and runs pngtest using a MSVC workspace
-       pngminus     =>  Simple pnm2png and png2pnm programs
-       pngsuite     =>  Test images
-       visupng      =>  Contains a MSVC workspace for VisualPng
-      projects      =>  Contains project files and workspaces for building DLL
-       beos             =>  Contains a Beos workspace for building libpng
-       c5builder        =>  Contains a Borland workspace for building libpng
-                            and zlib
-       visualc6         =>  Contains a Microsoft Visual C++ (MSVC) workspace
-                            for building libpng and zlib
-       netware.txt      =>  Contains instructions for downloading a set of
-                            project files for building libpng and zlib on
-                            Netware.
-       wince.txt        =>  Contains instructions for downloading a Microsoft
-                            Visual C++ (Windows CD Toolkit) workspace for
-                            building libpng and zlib on WindowsCE
-      scripts       =>  Directory containing scripts for building libpng:
-       descrip.mms      =>  VMS makefile for MMS or MMK
-       makefile.std     =>  Generic UNIX makefile (cc, creates static libpng.a)
-       makefile.elf     =>  Linux/ELF makefile symbol versioning,
-                            gcc, creates libpng12.so.0.1.2.29)
-       makefile.linux   =>  Linux/ELF makefile
-                            (gcc, creates libpng12.so.0.1.2.29)
-       makefile.gcmmx   =>  Linux/ELF makefile
-                            (gcc, creates libpng12.so.0.1.2.29,
-                            uses assembler code tuned for Intel MMX platform)
-       makefile.gcc     =>  Generic makefile (gcc, creates static libpng.a)
-       makefile.knr     =>  Archaic UNIX Makefile that converts files with
-                            ansi2knr (Requires ansi2knr.c from
-                            ftp://ftp.cs.wisc.edu/ghost)
-       makefile.aix     =>  AIX makefile
-       makefile.cygwin  =>  Cygwin/gcc makefile
-       makefile.darwin  =>  Darwin makefile
-       makefile.dec     =>  DEC Alpha UNIX makefile
-       makefile.freebsd =>  FreeBSD makefile
-       makefile.hpgcc   =>  HPUX makefile using gcc
-       makefile.hpux    =>  HPUX (10.20 and 11.00) makefile
-       makefile.hp64    =>  HPUX (10.20 and 11.00) makefile, 64 bit
-       makefile.ibmc    =>  IBM C/C++ version 3.x for Win32 and OS/2 (static)
-       makefile.intel   =>  Intel C/C++ version 4.0 and later
-       libpng.icc       =>  Project file, IBM VisualAge/C++ 4.0 or later
-       makefile.netbsd  =>  NetBSD/cc makefile, PNGGCCRD, makes libpng.so.
-       makefile.ne12bsd  =>  NetBSD/cc makefile, PNGGCCRD, makes libpng12.so
-       makefile.openbsd =>  OpenBSD makefile
-       makefile.sgi     =>  Silicon Graphics IRIX (cc, creates static lib)
-       makefile.sggcc   =>  Silicon Graphics
-                            (gcc, creates libpng12.so.0.1.2.29)
-       makefile.sunos   =>  Sun makefile
-       makefile.solaris =>  Solaris 2.X makefile
-                            (gcc, creates libpng12.so.0.1.2.29)
-       makefile.so9     =>  Solaris 9 makefile
-                            (gcc, creates libpng12.so.0.1.2.29)
-       makefile.32sunu  =>  Sun Ultra 32-bit makefile
-       makefile.64sunu  =>  Sun Ultra 64-bit makefile
-       makefile.sco     =>  For SCO OSr5  ELF and Unixware 7 with Native cc
-       makefile.mips    =>  MIPS makefile
-       makefile.acorn   =>  Acorn makefile
-       makefile.amiga   =>  Amiga makefile
-       smakefile.ppc    =>  AMIGA smakefile for SAS C V6.58/7.00 PPC
-                            compiler (Requires SCOPTIONS, copied from
-                            scripts/SCOPTIONS.ppc)
-       makefile.atari   =>  Atari makefile
-       makefile.beos    =>  BEOS makefile for X86
-       makefile.bor     =>  Borland makefile (uses bcc)
-       makefile.bc32    =>  32-bit Borland C++ (all modules compiled in C mode)
-       makefile.tc3     =>  Turbo C 3.0 makefile
-       makefile.dj2     =>  DJGPP 2 makefile
-       makefile.msc     =>  Microsoft C makefile
-       makefile.vcawin32=>  makefile for Microsoft Visual C++ 5.0 and
-                            later (uses assembler code tuned for Intel MMX
-                            platform)
-       makefile.vcwin32 =>  makefile for Microsoft Visual C++ 4.0 and
-                            later (does not use assembler code)
-       makefile.os2     =>  OS/2 Makefile (gcc and emx, requires pngos2.def)
-       pngos2.def       =>  OS/2 module definition file used by makefile.os2
-       makefile.watcom  =>  Watcom 10a+ Makefile, 32-bit flat memory model
-       makevms.com      =>  VMS build script
-       SCOPTIONS.ppc    =>  Used with smakefile.ppc
-
-Good luck, and happy coding.
-
--Glenn Randers-Pehrson (current maintainer)
- Internet: glennrp at users.sourceforge.net
-
--Andreas Eric Dilger (former maintainer, 1996-1997)
- Internet: adilger at enel.ucalgary.ca
- Web: http://members.shaw.ca/adilger/
-
--Guy Eric Schalnat (original author and former maintainer, 1995-1996)
- (formerly of Group 42, Inc)
- Internet: gschal at infinet.com
+README for libpng version 1.4.3 - June 26, 2010 (shared library 14.0)\r
+See the note about version numbers near the top of png.h\r
+\r
+See INSTALL for instructions on how to install libpng.\r
+\r
+Libpng comes in several distribution formats.  Get libpng-*.tar.gz,\r
+libpng-*.tar.xz or libpng-*.tar.bz2 if you want UNIX-style line endings\r
+in the text files, or lpng*.zip if you want DOS-style line endings.\r
+\r
+Version 0.89 was the first official release of libpng.  Don't let the\r
+fact that it's the first release fool you.  The libpng library has been in\r
+extensive use and testing since mid-1995.  By late 1997 it had\r
+finally gotten to the stage where there hadn't been significant\r
+changes to the API in some time, and people have a bad feeling about\r
+libraries with versions < 1.0.  Version 1.0.0 was released in\r
+March 1998.\r
+\r
+****\r
+Note that some of the changes to the png_info structure render this\r
+version of the library binary incompatible with libpng-0.89 or\r
+earlier versions if you are using a shared library.  The type of the\r
+"filler" parameter for png_set_filler() has changed from png_byte to\r
+png_uint_32, which will affect shared-library applications that use\r
+this function.\r
+\r
+To avoid problems with changes to the internals of png_info_struct,\r
+new APIs have been made available in 0.95 to avoid direct application\r
+access to info_ptr.  These functions are the png_set_<chunk> and\r
+png_get_<chunk> functions.  These functions should be used when\r
+accessing/storing the info_struct data, rather than manipulating it\r
+directly, to avoid such problems in the future.\r
+\r
+It is important to note that the APIs do not make current programs\r
+that access the info struct directly incompatible with the new\r
+library.  However, it is strongly suggested that new programs use\r
+the new APIs (as shown in example.c and pngtest.c), and older programs\r
+be converted to the new format, to facilitate upgrades in the future.\r
+****\r
+\r
+Additions since 0.90 include the ability to compile libpng as a\r
+Windows DLL, and new APIs for accessing data in the info struct.\r
+Experimental functions include the ability to set weighting and cost\r
+factors for row filter selection, direct reads of integers from buffers\r
+on big-endian processors that support misaligned data access, faster\r
+methods of doing alpha composition, and more accurate 16->8 bit color\r
+conversion.\r
+\r
+The additions since 0.89 include the ability to read from a PNG stream\r
+which has had some (or all) of the signature bytes read by the calling\r
+application.  This also allows the reading of embedded PNG streams that\r
+do not have the PNG file signature.  As well, it is now possible to set\r
+the library action on the detection of chunk CRC errors.  It is possible\r
+to set different actions based on whether the CRC error occurred in a\r
+critical or an ancillary chunk.\r
+\r
+The changes made to the library, and bugs fixed are based on discussions\r
+on the PNG-implement mailing list and not on material submitted\r
+privately to Guy, Andreas, or Glenn.  They will forward any good\r
+suggestions to the list.\r
+\r
+For a detailed description on using libpng, read libpng.txt.  For\r
+examples of libpng in a program, see example.c and pngtest.c.  For usage\r
+information and restrictions (what little they are) on libpng, see\r
+png.h.  For a description on using zlib (the compression library used by\r
+libpng) and zlib's restrictions, see zlib.h\r
+\r
+I have included a general makefile, as well as several machine and\r
+compiler specific ones, but you may have to modify one for your own needs.\r
+\r
+You should use zlib 1.0.4 or later to run this, but it MAY work with\r
+versions as old as zlib 0.95.  Even so, there are bugs in older zlib\r
+versions which can cause the output of invalid compression streams for\r
+some images.  You will definitely need zlib 1.0.4 or later if you are\r
+taking advantage of the MS-DOS "far" structure allocation for the small\r
+and medium memory models.  You should also note that zlib is a\r
+compression library that is useful for more things than just PNG files.\r
+You can use zlib as a drop-in replacement for fread() and fwrite() if\r
+you are so inclined.\r
+\r
+zlib should be available at the same place that libpng is, or at.\r
+ftp://ftp.info-zip.org/pub/infozip/zlib\r
+\r
+You may also want a copy of the PNG specification.  It is available\r
+as an RFC, a W3C Recommendation, and an ISO/IEC Standard.  You can find\r
+these at http://www.libpng.org/pub/png/documents/\r
+\r
+This code is currently being archived at libpng.sf.net in the\r
+[DOWNLOAD] area, and on CompuServe, Lib 20 (PNG SUPPORT)\r
+at GO GRAPHSUP.  If you can't find it in any of those places,\r
+e-mail me, and I'll help you find it.\r
+\r
+If you have any code changes, requests, problems, etc., please e-mail\r
+them to me.  Also, I'd appreciate any make files or project files,\r
+and any modifications you needed to make to get libpng to compile,\r
+along with a #define variable to tell what compiler/system you are on.\r
+If you needed to add transformations to libpng, or wish libpng would\r
+provide the image in a different way, drop me a note (and code, if\r
+possible), so I can consider supporting the transformation.\r
+Finally, if you get any warning messages when compiling libpng\r
+(note: not zlib), and they are easy to fix, I'd appreciate the\r
+fix.  Please mention "libpng" somewhere in the subject line.  Thanks.\r
+\r
+This release was created and will be supported by myself (of course\r
+based in a large way on Guy's and Andreas' earlier work), and the PNG\r
+development group.\r
+\r
+Send comments/corrections/commendations to png-mng-implement at\r
+lists.sourceforge.net (subscription required; visit \r
+https://lists.sourceforge.net/lists/listinfo/png-mng-implement\r
+to subscribe) or to glennrp at users.sourceforge.net\r
+\r
+You can't reach Guy, the original libpng author, at the addresses\r
+given in previous versions of this document.  He and Andreas will\r
+read mail addressed to the png-implement list, however.\r
+\r
+Please do not send general questions about PNG.  Send them to\r
+the (png-list at ccrc.wustl.edu, subscription required, write to\r
+majordomo at ccrc.wustl.edu with "subscribe png-list" in your message).\r
+On the other hand,\r
+please do not send libpng questions to that address, send them to me\r
+or to the png-implement list.  I'll\r
+get them in the end anyway.  If you have a question about something\r
+in the PNG specification that is related to using libpng, send it\r
+to me.  Send me any questions that start with "I was using libpng,\r
+and ...".  If in doubt, send questions to me.  I'll bounce them\r
+to others, if necessary.\r
+\r
+Please do not send suggestions on how to change PNG.  We have\r
+been discussing PNG for nine years now, and it is official and\r
+finished.  If you have suggestions for libpng, however, I'll\r
+gladly listen.  Even if your suggestion is not used immediately,\r
+it may be used later.\r
+\r
+Files in this distribution:\r
+\r
+      ANNOUNCE      =>  Announcement of this version, with recent changes\r
+      CHANGES       =>  Description of changes between libpng versions\r
+      KNOWNBUG      =>  List of known bugs and deficiencies\r
+      LICENSE       =>  License to use and redistribute libpng\r
+      README        =>  This file\r
+      TODO          =>  Things not implemented in the current library\r
+      Y2KINFO       =>  Statement of Y2K compliance\r
+      example.c     =>  Example code for using libpng functions\r
+      libpng.3      =>  manual page for libpng (includes libpng.txt)\r
+      libpng.txt    =>  Description of libpng and its functions\r
+      libpngpf.3    =>  manual page for libpng's private functions\r
+      png.5         =>  manual page for the PNG format\r
+      png.c         =>  Basic interface functions common to library\r
+      png.h         =>  Library function and interface declarations\r
+      pngconf.h     =>  System specific library configuration\r
+      pngerror.c    =>  Error/warning message I/O functions\r
+      pngget.c      =>  Functions for retrieving info from struct\r
+      pngmem.c      =>  Memory handling functions\r
+      pngbar.png    =>  PNG logo, 88x31\r
+      pngnow.png    =>  PNG logo, 98x31\r
+      pngpread.c    =>  Progressive reading functions\r
+      pngread.c     =>  Read data/helper high-level functions\r
+      pngrio.c      =>  Lowest-level data read I/O functions\r
+      pngrtran.c    =>  Read data transformation functions\r
+      pngrutil.c    =>  Read data utility functions\r
+      pngset.c      =>  Functions for storing data into the info_struct\r
+      pngtest.c     =>  Library test program\r
+      pngtest.png   =>  Library test sample image\r
+      pngtrans.c    =>  Common data transformation functions\r
+      pngwio.c      =>  Lowest-level write I/O functions\r
+      pngwrite.c    =>  High-level write functions\r
+      pngwtran.c    =>  Write data transformations\r
+      pngwutil.c    =>  Write utility functions\r
+      contrib       =>  Contributions\r
+       gregbook         =>  source code for PNG reading and writing, from\r
+                            Greg Roelofs' "PNG: The Definitive Guide",\r
+                            O'Reilly, 1999\r
+       msvctest     =>  Builds and runs pngtest using a MSVC workspace\r
+       pngminus     =>  Simple pnm2png and png2pnm programs\r
+       pngsuite     =>  Test images\r
+       visupng      =>  Contains a MSVC workspace for VisualPng\r
+      projects      =>  Contains project files and workspaces for\r
+                        building a DLL\r
+       c5builder        =>  Contains a Borland workspace for building\r
+                            libpng and zlib\r
+       visualc6         =>  Contains a Microsoft Visual C++ (MSVC)\r
+                            workspace for building libpng and zlib\r
+      scripts       =>  Directory containing scripts for building libpng:\r
+       descrip.mms      =>  VMS makefile for MMS or MMK\r
+       makefile.std     =>  Generic UNIX makefile (cc, creates static\r
+                            libpng.a)\r
+       makefile.elf     =>  Linux/ELF makefile symbol versioning,\r
+                            gcc, creates libpng14.so.14.1.4.3)\r
+       makefile.linux   =>  Linux/ELF makefile\r
+                            (gcc, creates libpng14.so.14.1.4.3)\r
+       makefile.gcc     =>  Generic makefile (gcc, creates static libpng.a)\r
+       makefile.knr     =>  Archaic UNIX Makefile that converts files with\r
+                            ansi2knr (Requires ansi2knr.c from\r
+                            ftp://ftp.cs.wisc.edu/ghost)\r
+       makefile.aix     =>  AIX makefile\r
+       makefile.cygwin  =>  Cygwin/gcc makefile\r
+       makefile.darwin  =>  Darwin makefile\r
+       makefile.dec     =>  DEC Alpha UNIX makefile\r
+       makefile.freebsd =>  FreeBSD makefile\r
+       makefile.hpgcc   =>  HPUX makefile using gcc\r
+       makefile.hpux    =>  HPUX (10.20 and 11.00) makefile\r
+       makefile.hp64    =>  HPUX (10.20 and 11.00) makefile, 64 bit\r
+       makefile.ibmc    =>  IBM C/C++ version 3.x for Win32 and OS/2 (static)\r
+       makefile.intel   =>  Intel C/C++ version 4.0 and later\r
+       makefile.mingw   =>  Mingw/gcc makefile\r
+       makefile.netbsd  =>  NetBSD/cc makefile, makes libpng.so.\r
+       makefile.ne14bsd  =>  NetBSD/cc makefile, makes\r
+                            libpng14.so\r
+       makefile.openbsd =>  OpenBSD makefile\r
+       makefile.sgi     =>  Silicon Graphics IRIX (cc, creates static lib)\r
+       makefile.sggcc   =>  Silicon Graphics\r
+                            (gcc, creates libpng14.so.14.1.4.3)\r
+       makefile.sunos   =>  Sun makefile\r
+       makefile.solaris =>  Solaris 2.X makefile\r
+                            (gcc, creates libpng14.so.14.1.4.3)\r
+       makefile.so9     =>  Solaris 9 makefile\r
+                            (gcc, creates libpng14.so.14.1.4.3)\r
+       makefile.32sunu  =>  Sun Ultra 32-bit makefile\r
+       makefile.64sunu  =>  Sun Ultra 64-bit makefile\r
+       makefile.sco     =>  For SCO OSr5  ELF and Unixware 7 with Native cc\r
+       makefile.mips    =>  MIPS makefile\r
+       makefile.acorn   =>  Acorn makefile\r
+       makefile.amiga   =>  Amiga makefile\r
+       smakefile.ppc    =>  AMIGA smakefile for SAS C V6.58/7.00 PPC\r
+                            compiler (Requires SCOPTIONS, copied from\r
+                            scripts/SCOPTIONS.ppc)\r
+       makefile.atari   =>  Atari makefile\r
+       makefile.beos    =>  BEOS makefile for X86\r
+       makefile.bor     =>  Borland makefile (uses bcc)\r
+       makefile.bc32    =>  32-bit Borland C++ (all modules compiled in C mode)\r
+       makefile.tc3     =>  Turbo C 3.0 makefile\r
+       makefile.dj2     =>  DJGPP 2 makefile\r
+       makefile.msc     =>  Microsoft C makefile\r
+       makefile.vcwin32 =>  makefile for Microsoft Visual C++ 4.0 and\r
+                            later (does not use assembler code)\r
+       makefile.os2     =>  OS/2 Makefile (gcc and emx, requires pngos2.def)\r
+       png32ce.def      =>  module definition for makefile.cegccg\r
+       pngos2.def       =>  OS/2 module definition file used by\r
+                            makefile.os2\r
+       pngwin.def       =>  module definition file used by\r
+                            makefile.cygwin and makefile.mingw\r
+       makefile.watcom  =>  Watcom 10a+ Makefile, 32-bit flat memory model\r
+       makevms.com      =>  VMS build script\r
+       SCOPTIONS.ppc    =>  Used with smakefile.ppc\r
+\r
+Good luck, and happy coding.\r
+\r
+-Glenn Randers-Pehrson (current maintainer, since 1998)\r
+ Internet: glennrp at users.sourceforge.net\r
+\r
+-Andreas Eric Dilger (former maintainer, 1996-1997)\r
+ Internet: adilger at enel.ucalgary.ca\r
+ Web: http://www-mddsp.enel.ucalgary.ca/People/adilger/\r
+\r
+-Guy Eric Schalnat (original author and former maintainer, 1995-1996)\r
+ (formerly of Group 42, Inc)\r
+ Internet: gschal at infinet.com\r
index 63ae86e..239615d 100644 (file)
-
-/* png.c - location for general purpose libpng functions
- *
- * Last changed in libpng 1.2.21 October 4, 2007
- * For conditions of distribution and use, see copyright notice in png.h
- * Copyright (c) 1998-2007 Glenn Randers-Pehrson
- * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
- * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
- */
-
-#define PNG_INTERNAL
-#define PNG_NO_EXTERN
-#include "png.h"
-
-/* Generate a compiler error if there is an old png.h in the search path. */
-typedef version_1_2_29 Your_png_h_is_not_version_1_2_29;
-
-/* Version information for C files.  This had better match the version
- * string defined in png.h.  */
-
-#ifdef PNG_USE_GLOBAL_ARRAYS
-/* png_libpng_ver was changed to a function in version 1.0.5c */
-PNG_CONST char png_libpng_ver[18] = PNG_LIBPNG_VER_STRING;
-
-#ifdef PNG_READ_SUPPORTED
-
-/* png_sig was changed to a function in version 1.0.5c */
-/* Place to hold the signature string for a PNG file. */
-PNG_CONST png_byte FARDATA png_sig[8] = {137, 80, 78, 71, 13, 10, 26, 10};
-#endif /* PNG_READ_SUPPORTED */
-
-/* Invoke global declarations for constant strings for known chunk types */
-PNG_IHDR;
-PNG_IDAT;
-PNG_IEND;
-PNG_PLTE;
-PNG_bKGD;
-PNG_cHRM;
-PNG_gAMA;
-PNG_hIST;
-PNG_iCCP;
-PNG_iTXt;
-PNG_oFFs;
-PNG_pCAL;
-PNG_sCAL;
-PNG_pHYs;
-PNG_sBIT;
-PNG_sPLT;
-PNG_sRGB;
-PNG_tEXt;
-PNG_tIME;
-PNG_tRNS;
-PNG_zTXt;
-
-#ifdef PNG_READ_SUPPORTED
-/* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
-
-/* start of interlace block */
-PNG_CONST int FARDATA png_pass_start[] = {0, 4, 0, 2, 0, 1, 0};
-
-/* offset to next interlace block */
-PNG_CONST int FARDATA png_pass_inc[] = {8, 8, 4, 4, 2, 2, 1};
-
-/* start of interlace block in the y direction */
-PNG_CONST int FARDATA png_pass_ystart[] = {0, 0, 4, 0, 2, 0, 1};
-
-/* offset to next interlace block in the y direction */
-PNG_CONST int FARDATA png_pass_yinc[] = {8, 8, 8, 4, 4, 2, 2};
-
-/* Height of interlace block.  This is not currently used - if you need
- * it, uncomment it here and in png.h
-PNG_CONST int FARDATA png_pass_height[] = {8, 8, 4, 4, 2, 2, 1};
-*/
-
-/* Mask to determine which pixels are valid in a pass */
-PNG_CONST int FARDATA png_pass_mask[] = {0x80, 0x08, 0x88, 0x22, 0xaa, 0x55, 0xff};
-
-/* Mask to determine which pixels to overwrite while displaying */
-PNG_CONST int FARDATA png_pass_dsp_mask[]
-   = {0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff};
-
-#endif /* PNG_READ_SUPPORTED */
-#endif /* PNG_USE_GLOBAL_ARRAYS */
-
-/* Tells libpng that we have already handled the first "num_bytes" bytes
- * of the PNG file signature.  If the PNG data is embedded into another
- * stream we can set num_bytes = 8 so that libpng will not attempt to read
- * or write any of the magic bytes before it starts on the IHDR.
- */
-
-#ifdef PNG_READ_SUPPORTED
-void PNGAPI
-png_set_sig_bytes(png_structp png_ptr, int num_bytes)
-{
-   if(png_ptr == NULL) return;
-   png_debug(1, "in png_set_sig_bytes\n");
-   if (num_bytes > 8)
-      png_error(png_ptr, "Too many bytes for PNG signature.");
-
-   png_ptr->sig_bytes = (png_byte)(num_bytes < 0 ? 0 : num_bytes);
-}
-
-/* Checks whether the supplied bytes match the PNG signature.  We allow
- * checking less than the full 8-byte signature so that those apps that
- * already read the first few bytes of a file to determine the file type
- * can simply check the remaining bytes for extra assurance.  Returns
- * an integer less than, equal to, or greater than zero if sig is found,
- * respectively, to be less than, to match, or be greater than the correct
- * PNG signature (this is the same behaviour as strcmp, memcmp, etc).
- */
-int PNGAPI
-png_sig_cmp(png_bytep sig, png_size_t start, png_size_t num_to_check)
-{
-   png_byte png_signature[8] = {137, 80, 78, 71, 13, 10, 26, 10};
-   if (num_to_check > 8)
-      num_to_check = 8;
-   else if (num_to_check < 1)
-      return (-1);
-
-   if (start > 7)
-      return (-1);
-
-   if (start + num_to_check > 8)
-      num_to_check = 8 - start;
-
-   return ((int)(png_memcmp(&sig[start], &png_signature[start], num_to_check)));
-}
-
-#if defined(PNG_1_0_X) || defined(PNG_1_2_X)
-/* (Obsolete) function to check signature bytes.  It does not allow one
- * to check a partial signature.  This function might be removed in the
- * future - use png_sig_cmp().  Returns true (nonzero) if the file is PNG.
- */
-int PNGAPI
-png_check_sig(png_bytep sig, int num)
-{
-  return ((int)!png_sig_cmp(sig, (png_size_t)0, (png_size_t)num));
-}
-#endif
-#endif /* PNG_READ_SUPPORTED */
-
-#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
-/* Function to allocate memory for zlib and clear it to 0. */
-#ifdef PNG_1_0_X
-voidpf PNGAPI
-#else
-voidpf /* private */
-#endif
-png_zalloc(voidpf png_ptr, uInt items, uInt size)
-{
-   png_voidp ptr;
-   png_structp p=(png_structp)png_ptr;
-   png_uint_32 save_flags=p->flags;
-   png_uint_32 num_bytes;
-
-   if(png_ptr == NULL) return (NULL);
-   if (items > PNG_UINT_32_MAX/size)
-   {
-     png_warning (p, "Potential overflow in png_zalloc()");
-     return (NULL);
-   }
-   num_bytes = (png_uint_32)items * size;
-
-   p->flags|=PNG_FLAG_MALLOC_NULL_MEM_OK;
-   ptr = (png_voidp)png_malloc((png_structp)png_ptr, num_bytes);
-   p->flags=save_flags;
-
-#if defined(PNG_1_0_X) && !defined(PNG_NO_ZALLOC_ZERO)
-   if (ptr == NULL)
-       return ((voidpf)ptr);
-
-   if (num_bytes > (png_uint_32)0x8000L)
-   {
-      png_memset(ptr, 0, (png_size_t)0x8000L);
-      png_memset((png_bytep)ptr + (png_size_t)0x8000L, 0,
-         (png_size_t)(num_bytes - (png_uint_32)0x8000L));
-   }
-   else
-   {
-      png_memset(ptr, 0, (png_size_t)num_bytes);
-   }
-#endif
-   return ((voidpf)ptr);
-}
-
-/* function to free memory for zlib */
-#ifdef PNG_1_0_X
-void PNGAPI
-#else
-void /* private */
-#endif
-png_zfree(voidpf png_ptr, voidpf ptr)
-{
-   png_free((png_structp)png_ptr, (png_voidp)ptr);
-}
-
-/* Reset the CRC variable to 32 bits of 1's.  Care must be taken
- * in case CRC is > 32 bits to leave the top bits 0.
- */
-void /* PRIVATE */
-png_reset_crc(png_structp png_ptr)
-{
-   png_ptr->crc = crc32(0, Z_NULL, 0);
-}
-
-/* Calculate the CRC over a section of data.  We can only pass as
- * much data to this routine as the largest single buffer size.  We
- * also check that this data will actually be used before going to the
- * trouble of calculating it.
- */
-void /* PRIVATE */
-png_calculate_crc(png_structp png_ptr, png_bytep ptr, png_size_t length)
-{
-   int need_crc = 1;
-
-   if (png_ptr->chunk_name[0] & 0x20)                     /* ancillary */
-   {
-      if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_MASK) ==
-          (PNG_FLAG_CRC_ANCILLARY_USE | PNG_FLAG_CRC_ANCILLARY_NOWARN))
-         need_crc = 0;
-   }
-   else                                                    /* critical */
-   {
-      if (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_IGNORE)
-         need_crc = 0;
-   }
-
-   if (need_crc)
-      png_ptr->crc = crc32(png_ptr->crc, ptr, (uInt)length);
-}
-
-/* Allocate the memory for an info_struct for the application.  We don't
- * really need the png_ptr, but it could potentially be useful in the
- * future.  This should be used in favour of malloc(png_sizeof(png_info))
- * and png_info_init() so that applications that want to use a shared
- * libpng don't have to be recompiled if png_info changes size.
- */
-png_infop PNGAPI
-png_create_info_struct(png_structp png_ptr)
-{
-   png_infop info_ptr;
-
-   png_debug(1, "in png_create_info_struct\n");
-   if(png_ptr == NULL) return (NULL);
-#ifdef PNG_USER_MEM_SUPPORTED
-   info_ptr = (png_infop)png_create_struct_2(PNG_STRUCT_INFO,
-      png_ptr->malloc_fn, png_ptr->mem_ptr);
-#else
-   info_ptr = (png_infop)png_create_struct(PNG_STRUCT_INFO);
-#endif
-   if (info_ptr != NULL)
-      png_info_init_3(&info_ptr, png_sizeof(png_info));
-
-   return (info_ptr);
-}
-
-/* This function frees the memory associated with a single info struct.
- * Normally, one would use either png_destroy_read_struct() or
- * png_destroy_write_struct() to free an info struct, but this may be
- * useful for some applications.
- */
-void PNGAPI
-png_destroy_info_struct(png_structp png_ptr, png_infopp info_ptr_ptr)
-{
-   png_infop info_ptr = NULL;
-   if(png_ptr == NULL) return;
-
-   png_debug(1, "in png_destroy_info_struct\n");
-   if (info_ptr_ptr != NULL)
-      info_ptr = *info_ptr_ptr;
-
-   if (info_ptr != NULL)
-   {
-      png_info_destroy(png_ptr, info_ptr);
-
-#ifdef PNG_USER_MEM_SUPPORTED
-      png_destroy_struct_2((png_voidp)info_ptr, png_ptr->free_fn,
-          png_ptr->mem_ptr);
-#else
-      png_destroy_struct((png_voidp)info_ptr);
-#endif
-      *info_ptr_ptr = NULL;
-   }
-}
-
-/* Initialize the info structure.  This is now an internal function (0.89)
- * and applications using it are urged to use png_create_info_struct()
- * instead.
- */
-#if defined(PNG_1_0_X) || defined(PNG_1_2_X)
-#undef png_info_init
-void PNGAPI
-png_info_init(png_infop info_ptr)
-{
-   /* We only come here via pre-1.0.12-compiled applications */
-   png_info_init_3(&info_ptr, 0);
-}
-#endif
-
-void PNGAPI
-png_info_init_3(png_infopp ptr_ptr, png_size_t png_info_struct_size)
-{
-   png_infop info_ptr = *ptr_ptr;
-
-   if(info_ptr == NULL) return;
-
-   png_debug(1, "in png_info_init_3\n");
-
-   if(png_sizeof(png_info) > png_info_struct_size)
-     {
-       png_destroy_struct(info_ptr);
-       info_ptr = (png_infop)png_create_struct(PNG_STRUCT_INFO);
-       *ptr_ptr = info_ptr;
-     }
-
-   /* set everything to 0 */
-   png_memset(info_ptr, 0, png_sizeof (png_info));
-}
-
-#ifdef PNG_FREE_ME_SUPPORTED
-void PNGAPI
-png_data_freer(png_structp png_ptr, png_infop info_ptr,
-   int freer, png_uint_32 mask)
-{
-   png_debug(1, "in png_data_freer\n");
-   if (png_ptr == NULL || info_ptr == NULL)
-      return;
-   if(freer == PNG_DESTROY_WILL_FREE_DATA)
-      info_ptr->free_me |= mask;
-   else if(freer == PNG_USER_WILL_FREE_DATA)
-      info_ptr->free_me &= ~mask;
-   else
-      png_warning(png_ptr,
-         "Unknown freer parameter in png_data_freer.");
-}
-#endif
-
-void PNGAPI
-png_free_data(png_structp png_ptr, png_infop info_ptr, png_uint_32 mask,
-   int num)
-{
-   png_debug(1, "in png_free_data\n");
-   if (png_ptr == NULL || info_ptr == NULL)
-      return;
-
-#if defined(PNG_TEXT_SUPPORTED)
-/* free text item num or (if num == -1) all text items */
-#ifdef PNG_FREE_ME_SUPPORTED
-if ((mask & PNG_FREE_TEXT) & info_ptr->free_me)
-#else
-if (mask & PNG_FREE_TEXT)
-#endif
-{
-   if (num != -1)
-   {
-     if (info_ptr->text && info_ptr->text[num].key)
-     {
-         png_free(png_ptr, info_ptr->text[num].key);
-         info_ptr->text[num].key = NULL;
-     }
-   }
-   else
-   {
-       int i;
-       for (i = 0; i < info_ptr->num_text; i++)
-           png_free_data(png_ptr, info_ptr, PNG_FREE_TEXT, i);
-       png_free(png_ptr, info_ptr->text);
-       info_ptr->text = NULL;
-       info_ptr->num_text=0;
-   }
-}
-#endif
-
-#if defined(PNG_tRNS_SUPPORTED)
-/* free any tRNS entry */
-#ifdef PNG_FREE_ME_SUPPORTED
-if ((mask & PNG_FREE_TRNS) & info_ptr->free_me)
-#else
-if ((mask & PNG_FREE_TRNS) && (png_ptr->flags & PNG_FLAG_FREE_TRNS))
-#endif
-{
-    png_free(png_ptr, info_ptr->trans);
-    info_ptr->valid &= ~PNG_INFO_tRNS;
-#ifndef PNG_FREE_ME_SUPPORTED
-    png_ptr->flags &= ~PNG_FLAG_FREE_TRNS;
-#endif
-    info_ptr->trans = NULL;
-}
-#endif
-
-#if defined(PNG_sCAL_SUPPORTED)
-/* free any sCAL entry */
-#ifdef PNG_FREE_ME_SUPPORTED
-if ((mask & PNG_FREE_SCAL) & info_ptr->free_me)
-#else
-if (mask & PNG_FREE_SCAL)
-#endif
-{
-#if defined(PNG_FIXED_POINT_SUPPORTED) && !defined(PNG_FLOATING_POINT_SUPPORTED)
-    png_free(png_ptr, info_ptr->scal_s_width);
-    png_free(png_ptr, info_ptr->scal_s_height);
-    info_ptr->scal_s_width = NULL;
-    info_ptr->scal_s_height = NULL;
-#endif
-    info_ptr->valid &= ~PNG_INFO_sCAL;
-}
-#endif
-
-#if defined(PNG_pCAL_SUPPORTED)
-/* free any pCAL entry */
-#ifdef PNG_FREE_ME_SUPPORTED
-if ((mask & PNG_FREE_PCAL) & info_ptr->free_me)
-#else
-if (mask & PNG_FREE_PCAL)
-#endif
-{
-    png_free(png_ptr, info_ptr->pcal_purpose);
-    png_free(png_ptr, info_ptr->pcal_units);
-    info_ptr->pcal_purpose = NULL;
-    info_ptr->pcal_units = NULL;
-    if (info_ptr->pcal_params != NULL)
-    {
-        int i;
-        for (i = 0; i < (int)info_ptr->pcal_nparams; i++)
-        {
-          png_free(png_ptr, info_ptr->pcal_params[i]);
-          info_ptr->pcal_params[i]=NULL;
-        }
-        png_free(png_ptr, info_ptr->pcal_params);
-        info_ptr->pcal_params = NULL;
-    }
-    info_ptr->valid &= ~PNG_INFO_pCAL;
-}
-#endif
-
-#if defined(PNG_iCCP_SUPPORTED)
-/* free any iCCP entry */
-#ifdef PNG_FREE_ME_SUPPORTED
-if ((mask & PNG_FREE_ICCP) & info_ptr->free_me)
-#else
-if (mask & PNG_FREE_ICCP)
-#endif
-{
-    png_free(png_ptr, info_ptr->iccp_name);
-    png_free(png_ptr, info_ptr->iccp_profile);
-    info_ptr->iccp_name = NULL;
-    info_ptr->iccp_profile = NULL;
-    info_ptr->valid &= ~PNG_INFO_iCCP;
-}
-#endif
-
-#if defined(PNG_sPLT_SUPPORTED)
-/* free a given sPLT entry, or (if num == -1) all sPLT entries */
-#ifdef PNG_FREE_ME_SUPPORTED
-if ((mask & PNG_FREE_SPLT) & info_ptr->free_me)
-#else
-if (mask & PNG_FREE_SPLT)
-#endif
-{
-   if (num != -1)
-   {
-      if(info_ptr->splt_palettes)
-      {
-          png_free(png_ptr, info_ptr->splt_palettes[num].name);
-          png_free(png_ptr, info_ptr->splt_palettes[num].entries);
-          info_ptr->splt_palettes[num].name = NULL;
-          info_ptr->splt_palettes[num].entries = NULL;
-      }
-   }
-   else
-   {
-       if(info_ptr->splt_palettes_num)
-       {
-         int i;
-         for (i = 0; i < (int)info_ptr->splt_palettes_num; i++)
-            png_free_data(png_ptr, info_ptr, PNG_FREE_SPLT, i);
-
-         png_free(png_ptr, info_ptr->splt_palettes);
-         info_ptr->splt_palettes = NULL;
-         info_ptr->splt_palettes_num = 0;
-       }
-       info_ptr->valid &= ~PNG_INFO_sPLT;
-   }
-}
-#endif
-
-#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
-  if(png_ptr->unknown_chunk.data)
-  {
-    png_free(png_ptr, png_ptr->unknown_chunk.data);
-    png_ptr->unknown_chunk.data = NULL;
-  }
-#ifdef PNG_FREE_ME_SUPPORTED
-if ((mask & PNG_FREE_UNKN) & info_ptr->free_me)
-#else
-if (mask & PNG_FREE_UNKN)
-#endif
-{
-   if (num != -1)
-   {
-       if(info_ptr->unknown_chunks)
-       {
-          png_free(png_ptr, info_ptr->unknown_chunks[num].data);
-          info_ptr->unknown_chunks[num].data = NULL;
-       }
-   }
-   else
-   {
-       int i;
-
-       if(info_ptr->unknown_chunks_num)
-       {
-         for (i = 0; i < (int)info_ptr->unknown_chunks_num; i++)
-            png_free_data(png_ptr, info_ptr, PNG_FREE_UNKN, i);
-
-         png_free(png_ptr, info_ptr->unknown_chunks);
-         info_ptr->unknown_chunks = NULL;
-         info_ptr->unknown_chunks_num = 0;
-       }
-   }
-}
-#endif
-
-#if defined(PNG_hIST_SUPPORTED)
-/* free any hIST entry */
-#ifdef PNG_FREE_ME_SUPPORTED
-if ((mask & PNG_FREE_HIST)  & info_ptr->free_me)
-#else
-if ((mask & PNG_FREE_HIST) && (png_ptr->flags & PNG_FLAG_FREE_HIST))
-#endif
-{
-    png_free(png_ptr, info_ptr->hist);
-    info_ptr->hist = NULL;
-    info_ptr->valid &= ~PNG_INFO_hIST;
-#ifndef PNG_FREE_ME_SUPPORTED
-    png_ptr->flags &= ~PNG_FLAG_FREE_HIST;
-#endif
-}
-#endif
-
-/* free any PLTE entry that was internally allocated */
-#ifdef PNG_FREE_ME_SUPPORTED
-if ((mask & PNG_FREE_PLTE) & info_ptr->free_me)
-#else
-if ((mask & PNG_FREE_PLTE) && (png_ptr->flags & PNG_FLAG_FREE_PLTE))
-#endif
-{
-    png_zfree(png_ptr, info_ptr->palette);
-    info_ptr->palette = NULL;
-    info_ptr->valid &= ~PNG_INFO_PLTE;
-#ifndef PNG_FREE_ME_SUPPORTED
-    png_ptr->flags &= ~PNG_FLAG_FREE_PLTE;
-#endif
-    info_ptr->num_palette = 0;
-}
-
-#if defined(PNG_INFO_IMAGE_SUPPORTED)
-/* free any image bits attached to the info structure */
-#ifdef PNG_FREE_ME_SUPPORTED
-if ((mask & PNG_FREE_ROWS) & info_ptr->free_me)
-#else
-if (mask & PNG_FREE_ROWS)
-#endif
-{
-    if(info_ptr->row_pointers)
-    {
-       int row;
-       for (row = 0; row < (int)info_ptr->height; row++)
-       {
-          png_free(png_ptr, info_ptr->row_pointers[row]);
-          info_ptr->row_pointers[row]=NULL;
-       }
-       png_free(png_ptr, info_ptr->row_pointers);
-       info_ptr->row_pointers=NULL;
-    }
-    info_ptr->valid &= ~PNG_INFO_IDAT;
-}
-#endif
-
-#ifdef PNG_FREE_ME_SUPPORTED
-   if(num == -1)
-     info_ptr->free_me &= ~mask;
-   else
-     info_ptr->free_me &= ~(mask & ~PNG_FREE_MUL);
-#endif
-}
-
-/* This is an internal routine to free any memory that the info struct is
- * pointing to before re-using it or freeing the struct itself.  Recall
- * that png_free() checks for NULL pointers for us.
- */
-void /* PRIVATE */
-png_info_destroy(png_structp png_ptr, png_infop info_ptr)
-{
-   png_debug(1, "in png_info_destroy\n");
-
-   png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
-
-#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
-   if (png_ptr->num_chunk_list)
-   {
-       png_free(png_ptr, png_ptr->chunk_list);
-       png_ptr->chunk_list=NULL;
-       png_ptr->num_chunk_list=0;
-   }
-#endif
-
-   png_info_init_3(&info_ptr, png_sizeof(png_info));
-}
-#endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */
-
-/* This function returns a pointer to the io_ptr associated with the user
- * functions.  The application should free any memory associated with this
- * pointer before png_write_destroy() or png_read_destroy() are called.
- */
-png_voidp PNGAPI
-png_get_io_ptr(png_structp png_ptr)
-{
-   if(png_ptr == NULL) return (NULL);
-   return (png_ptr->io_ptr);
-}
-
-#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
-#if !defined(PNG_NO_STDIO)
-/* Initialize the default input/output functions for the PNG file.  If you
- * use your own read or write routines, you can call either png_set_read_fn()
- * or png_set_write_fn() instead of png_init_io().  If you have defined
- * PNG_NO_STDIO, you must use a function of your own because "FILE *" isn't
- * necessarily available.
- */
-void PNGAPI
-png_init_io(png_structp png_ptr, png_FILE_p fp)
-{
-   png_debug(1, "in png_init_io\n");
-   if(png_ptr == NULL) return;
-   png_ptr->io_ptr = (png_voidp)fp;
-}
-#endif
-
-#if defined(PNG_TIME_RFC1123_SUPPORTED)
-/* Convert the supplied time into an RFC 1123 string suitable for use in
- * a "Creation Time" or other text-based time string.
- */
-png_charp PNGAPI
-png_convert_to_rfc1123(png_structp png_ptr, png_timep ptime)
-{
-   static PNG_CONST char short_months[12][4] =
-        {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
-         "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
-
-   if(png_ptr == NULL) return (NULL);
-   if (png_ptr->time_buffer == NULL)
-   {
-      png_ptr->time_buffer = (png_charp)png_malloc(png_ptr, (png_uint_32)(29*
-         png_sizeof(char)));
-   }
-
-#if defined(_WIN32_WCE)
-   {
-      wchar_t time_buf[29];
-      wsprintf(time_buf, TEXT("%d %S %d %02d:%02d:%02d +0000"),
-          ptime->day % 32, short_months[(ptime->month - 1) % 12],
-        ptime->year, ptime->hour % 24, ptime->minute % 60,
-          ptime->second % 61);
-      WideCharToMultiByte(CP_ACP, 0, time_buf, -1, png_ptr->time_buffer, 29,
-          NULL, NULL);
-   }
-#else
-#ifdef USE_FAR_KEYWORD
-   {
-      char near_time_buf[29];
-      png_snprintf6(near_time_buf,29,"%d %s %d %02d:%02d:%02d +0000",
-          ptime->day % 32, short_months[(ptime->month - 1) % 12],
-          ptime->year, ptime->hour % 24, ptime->minute % 60,
-          ptime->second % 61);
-      png_memcpy(png_ptr->time_buffer, near_time_buf,
-          29*png_sizeof(char));
-   }
-#else
-   png_snprintf6(png_ptr->time_buffer,29,"%d %s %d %02d:%02d:%02d +0000",
-       ptime->day % 32, short_months[(ptime->month - 1) % 12],
-       ptime->year, ptime->hour % 24, ptime->minute % 60,
-       ptime->second % 61);
-#endif
-#endif /* _WIN32_WCE */
-   return ((png_charp)png_ptr->time_buffer);
-}
-#endif /* PNG_TIME_RFC1123_SUPPORTED */
-
-#endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */
-
-png_charp PNGAPI
-png_get_copyright(png_structp png_ptr)
-{
-   png_ptr = png_ptr;  /* silence compiler warning about unused png_ptr */
-   return ((png_charp) "\n libpng version 1.2.29 - May 8, 2008\n\
-   Copyright (c) 1998-2008 Glenn Randers-Pehrson\n\
-   Copyright (c) 1996-1997 Andreas Dilger\n\
-   Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc.\n");
-}
-
-/* The following return the library version as a short string in the
- * format 1.0.0 through 99.99.99zz.  To get the version of *.h files
- * used with your application, print out PNG_LIBPNG_VER_STRING, which
- * is defined in png.h.
- * Note: now there is no difference between png_get_libpng_ver() and
- * png_get_header_ver().  Due to the version_nn_nn_nn typedef guard,
- * it is guaranteed that png.c uses the correct version of png.h.
- */
-png_charp PNGAPI
-png_get_libpng_ver(png_structp png_ptr)
-{
-   /* Version of *.c files used when building libpng */
-   png_ptr = png_ptr;  /* silence compiler warning about unused png_ptr */
-   return ((png_charp) PNG_LIBPNG_VER_STRING);
-}
-
-png_charp PNGAPI
-png_get_header_ver(png_structp png_ptr)
-{
-   /* Version of *.h files used when building libpng */
-   png_ptr = png_ptr;  /* silence compiler warning about unused png_ptr */
-   return ((png_charp) PNG_LIBPNG_VER_STRING);
-}
-
-png_charp PNGAPI
-png_get_header_version(png_structp png_ptr)
-{
-   /* Returns longer string containing both version and date */
-   png_ptr = png_ptr;  /* silence compiler warning about unused png_ptr */
-   return ((png_charp) PNG_HEADER_VERSION_STRING
-#ifndef PNG_READ_SUPPORTED
-   "     (NO READ SUPPORT)"
-#endif
-   "\n");
-}
-
-#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
-#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
-int PNGAPI
-png_handle_as_unknown(png_structp png_ptr, png_bytep chunk_name)
-{
-   /* check chunk_name and return "keep" value if it's on the list, else 0 */
-   int i;
-   png_bytep p;
-   if(png_ptr == NULL || chunk_name == NULL || png_ptr->num_chunk_list<=0)
-      return 0;
-   p=png_ptr->chunk_list+png_ptr->num_chunk_list*5-5;
-   for (i = png_ptr->num_chunk_list; i; i--, p-=5)
-      if (!png_memcmp(chunk_name, p, 4))
-        return ((int)*(p+4));
-   return 0;
-}
-#endif
-
-/* This function, added to libpng-1.0.6g, is untested. */
-int PNGAPI
-png_reset_zstream(png_structp png_ptr)
-{
-   if (png_ptr == NULL) return Z_STREAM_ERROR;
-   return (inflateReset(&png_ptr->zstream));
-}
-#endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */
-
-/* This function was added to libpng-1.0.7 */
-png_uint_32 PNGAPI
-png_access_version_number(void)
-{
-   /* Version of *.c files used when building libpng */
-   return((png_uint_32) PNG_LIBPNG_VER);
-}
-
-
-#if defined(PNG_READ_SUPPORTED) && defined(PNG_ASSEMBLER_CODE_SUPPORTED)
-#if !defined(PNG_1_0_X)
-/* this function was added to libpng 1.2.0 */
-int PNGAPI
-png_mmx_support(void)
-{
-   /* obsolete, to be removed from libpng-1.4.0 */
-    return -1;
-}
-#endif /* PNG_1_0_X */
-#endif /* PNG_READ_SUPPORTED && PNG_ASSEMBLER_CODE_SUPPORTED */
-
-#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
-#ifdef PNG_SIZE_T
-/* Added at libpng version 1.2.6 */
-   PNG_EXTERN png_size_t PNGAPI png_convert_size PNGARG((size_t size));
-png_size_t PNGAPI
-png_convert_size(size_t size)
-{
-  if (size > (png_size_t)-1)
-     PNG_ABORT();  /* We haven't got access to png_ptr, so no png_error() */
-  return ((png_size_t)size);
-}
-#endif /* PNG_SIZE_T */
-#endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */
+\r
+/* png.c - location for general purpose libpng functions\r
+ *\r
+ * Last changed in libpng 1.4.2 [May 6, 2010]\r
+ * Copyright (c) 1998-2010 Glenn Randers-Pehrson\r
+ * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)\r
+ * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)\r
+ *\r
+ * This code is released under the libpng license.\r
+ * For conditions of distribution and use, see the disclaimer\r
+ * and license in png.h\r
+ */\r
+\r
+#define PNG_NO_EXTERN\r
+#define PNG_NO_PEDANTIC_WARNINGS\r
+#include "png.h"\r
+#include "pngpriv.h"\r
+\r
+/* Generate a compiler error if there is an old png.h in the search path. */\r
+typedef version_1_4_3 Your_png_h_is_not_version_1_4_3;\r
+\r
+/* Version information for C files.  This had better match the version\r
+ * string defined in png.h.\r
+ */\r
+\r
+/* Tells libpng that we have already handled the first "num_bytes" bytes\r
+ * of the PNG file signature.  If the PNG data is embedded into another\r
+ * stream we can set num_bytes = 8 so that libpng will not attempt to read\r
+ * or write any of the magic bytes before it starts on the IHDR.\r
+ */\r
+\r
+#ifdef PNG_READ_SUPPORTED\r
+void PNGAPI\r
+png_set_sig_bytes(png_structp png_ptr, int num_bytes)\r
+{\r
+   png_debug(1, "in png_set_sig_bytes");\r
+\r
+   if (png_ptr == NULL)\r
+      return;\r
+\r
+   if (num_bytes > 8)\r
+      png_error(png_ptr, "Too many bytes for PNG signature");\r
+\r
+   png_ptr->sig_bytes = (png_byte)(num_bytes < 0 ? 0 : num_bytes);\r
+}\r
+\r
+/* Checks whether the supplied bytes match the PNG signature.  We allow\r
+ * checking less than the full 8-byte signature so that those apps that\r
+ * already read the first few bytes of a file to determine the file type\r
+ * can simply check the remaining bytes for extra assurance.  Returns\r
+ * an integer less than, equal to, or greater than zero if sig is found,\r
+ * respectively, to be less than, to match, or be greater than the correct\r
+ * PNG signature (this is the same behaviour as strcmp, memcmp, etc).\r
+ */\r
+int PNGAPI\r
+png_sig_cmp(png_bytep sig, png_size_t start, png_size_t num_to_check)\r
+{\r
+   png_byte png_signature[8] = {137, 80, 78, 71, 13, 10, 26, 10};\r
+   if (num_to_check > 8)\r
+      num_to_check = 8;\r
+   else if (num_to_check < 1)\r
+      return (-1);\r
+\r
+   if (start > 7)\r
+      return (-1);\r
+\r
+   if (start + num_to_check > 8)\r
+      num_to_check = 8 - start;\r
+\r
+   return ((int)(png_memcmp(&sig[start], &png_signature[start], num_to_check)));\r
+}\r
+\r
+#endif /* PNG_READ_SUPPORTED */\r
+\r
+#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)\r
+/* Function to allocate memory for zlib and clear it to 0. */\r
+voidpf /* PRIVATE */\r
+png_zalloc(voidpf png_ptr, uInt items, uInt size)\r
+{\r
+   png_voidp ptr;\r
+   png_structp p=(png_structp)png_ptr;\r
+   png_uint_32 save_flags=p->flags;\r
+   png_alloc_size_t num_bytes;\r
+\r
+   if (png_ptr == NULL)\r
+      return (NULL);\r
+   if (items > PNG_UINT_32_MAX/size)\r
+   {\r
+     png_warning (p, "Potential overflow in png_zalloc()");\r
+     return (NULL);\r
+   }\r
+   num_bytes = (png_alloc_size_t)items * size;\r
+\r
+   p->flags|=PNG_FLAG_MALLOC_NULL_MEM_OK;\r
+   ptr = (png_voidp)png_malloc((png_structp)png_ptr, num_bytes);\r
+   p->flags=save_flags;\r
+\r
+   return ((voidpf)ptr);\r
+}\r
+\r
+/* Function to free memory for zlib */\r
+void /* PRIVATE */\r
+png_zfree(voidpf png_ptr, voidpf ptr)\r
+{\r
+   png_free((png_structp)png_ptr, (png_voidp)ptr);\r
+}\r
+\r
+/* Reset the CRC variable to 32 bits of 1's.  Care must be taken\r
+ * in case CRC is > 32 bits to leave the top bits 0.\r
+ */\r
+void /* PRIVATE */\r
+png_reset_crc(png_structp png_ptr)\r
+{\r
+   png_ptr->crc = crc32(0, Z_NULL, 0);\r
+}\r
+\r
+/* Calculate the CRC over a section of data.  We can only pass as\r
+ * much data to this routine as the largest single buffer size.  We\r
+ * also check that this data will actually be used before going to the\r
+ * trouble of calculating it.\r
+ */\r
+void /* PRIVATE */\r
+png_calculate_crc(png_structp png_ptr, png_bytep ptr, png_size_t length)\r
+{\r
+   int need_crc = 1;\r
+\r
+   if (png_ptr->chunk_name[0] & 0x20)                     /* ancillary */\r
+   {\r
+      if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_MASK) ==\r
+          (PNG_FLAG_CRC_ANCILLARY_USE | PNG_FLAG_CRC_ANCILLARY_NOWARN))\r
+         need_crc = 0;\r
+   }\r
+   else                                                    /* critical */\r
+   {\r
+      if (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_IGNORE)\r
+         need_crc = 0;\r
+   }\r
+\r
+   if (need_crc)\r
+      png_ptr->crc = crc32(png_ptr->crc, ptr, (uInt)length);\r
+}\r
+\r
+/* Allocate the memory for an info_struct for the application.  We don't\r
+ * really need the png_ptr, but it could potentially be useful in the\r
+ * future.  This should be used in favour of malloc(png_sizeof(png_info))\r
+ * and png_info_init() so that applications that want to use a shared\r
+ * libpng don't have to be recompiled if png_info changes size.\r
+ */\r
+png_infop PNGAPI\r
+png_create_info_struct(png_structp png_ptr)\r
+{\r
+   png_infop info_ptr;\r
+\r
+   png_debug(1, "in png_create_info_struct");\r
+\r
+   if (png_ptr == NULL)\r
+      return (NULL);\r
+\r
+#ifdef PNG_USER_MEM_SUPPORTED\r
+   info_ptr = (png_infop)png_create_struct_2(PNG_STRUCT_INFO,\r
+      png_ptr->malloc_fn, png_ptr->mem_ptr);\r
+#else\r
+   info_ptr = (png_infop)png_create_struct(PNG_STRUCT_INFO);\r
+#endif\r
+   if (info_ptr != NULL)\r
+      png_info_init_3(&info_ptr, png_sizeof(png_info));\r
+\r
+   return (info_ptr);\r
+}\r
+\r
+/* This function frees the memory associated with a single info struct.\r
+ * Normally, one would use either png_destroy_read_struct() or\r
+ * png_destroy_write_struct() to free an info struct, but this may be\r
+ * useful for some applications.\r
+ */\r
+void PNGAPI\r
+png_destroy_info_struct(png_structp png_ptr, png_infopp info_ptr_ptr)\r
+{\r
+   png_infop info_ptr = NULL;\r
+\r
+   png_debug(1, "in png_destroy_info_struct");\r
+\r
+   if (png_ptr == NULL)\r
+      return;\r
+\r
+   if (info_ptr_ptr != NULL)\r
+      info_ptr = *info_ptr_ptr;\r
+\r
+   if (info_ptr != NULL)\r
+   {\r
+      png_info_destroy(png_ptr, info_ptr);\r
+\r
+#ifdef PNG_USER_MEM_SUPPORTED\r
+      png_destroy_struct_2((png_voidp)info_ptr, png_ptr->free_fn,\r
+          png_ptr->mem_ptr);\r
+#else\r
+      png_destroy_struct((png_voidp)info_ptr);\r
+#endif\r
+      *info_ptr_ptr = NULL;\r
+   }\r
+}\r
+\r
+/* Initialize the info structure.  This is now an internal function (0.89)\r
+ * and applications using it are urged to use png_create_info_struct()\r
+ * instead.\r
+ */\r
+\r
+void PNGAPI\r
+png_info_init_3(png_infopp ptr_ptr, png_size_t png_info_struct_size)\r
+{\r
+   png_infop info_ptr = *ptr_ptr;\r
+\r
+   png_debug(1, "in png_info_init_3");\r
+\r
+   if (info_ptr == NULL)\r
+      return;\r
+\r
+   if (png_sizeof(png_info) > png_info_struct_size)\r
+   {\r
+      png_destroy_struct(info_ptr);\r
+      info_ptr = (png_infop)png_create_struct(PNG_STRUCT_INFO);\r
+      *ptr_ptr = info_ptr;\r
+   }\r
+\r
+   /* Set everything to 0 */\r
+   png_memset(info_ptr, 0, png_sizeof(png_info));\r
+}\r
+\r
+void PNGAPI\r
+png_data_freer(png_structp png_ptr, png_infop info_ptr,\r
+   int freer, png_uint_32 mask)\r
+{\r
+   png_debug(1, "in png_data_freer");\r
+\r
+   if (png_ptr == NULL || info_ptr == NULL)\r
+      return;\r
+\r
+   if (freer == PNG_DESTROY_WILL_FREE_DATA)\r
+      info_ptr->free_me |= mask;\r
+   else if (freer == PNG_USER_WILL_FREE_DATA)\r
+      info_ptr->free_me &= ~mask;\r
+   else\r
+      png_warning(png_ptr,\r
+         "Unknown freer parameter in png_data_freer");\r
+}\r
+\r
+void PNGAPI\r
+png_free_data(png_structp png_ptr, png_infop info_ptr, png_uint_32 mask,\r
+   int num)\r
+{\r
+   png_debug(1, "in png_free_data");\r
+\r
+   if (png_ptr == NULL || info_ptr == NULL)\r
+      return;\r
+\r
+#ifdef PNG_TEXT_SUPPORTED\r
+   /* Free text item num or (if num == -1) all text items */\r
+   if ((mask & PNG_FREE_TEXT) & info_ptr->free_me)\r
+   {\r
+      if (num != -1)\r
+      {\r
+         if (info_ptr->text && info_ptr->text[num].key)\r
+         {\r
+            png_free(png_ptr, info_ptr->text[num].key);\r
+            info_ptr->text[num].key = NULL;\r
+         }\r
+      }\r
+      else\r
+      {\r
+         int i;\r
+         for (i = 0; i < info_ptr->num_text; i++)\r
+             png_free_data(png_ptr, info_ptr, PNG_FREE_TEXT, i);\r
+         png_free(png_ptr, info_ptr->text);\r
+         info_ptr->text = NULL;\r
+         info_ptr->num_text=0;\r
+      }\r
+   }\r
+#endif\r
+\r
+#ifdef PNG_tRNS_SUPPORTED\r
+   /* Free any tRNS entry */\r
+   if ((mask & PNG_FREE_TRNS) & info_ptr->free_me)\r
+   {\r
+      png_free(png_ptr, info_ptr->trans_alpha);\r
+      info_ptr->trans_alpha = NULL;\r
+      info_ptr->valid &= ~PNG_INFO_tRNS;\r
+   }\r
+#endif\r
+\r
+#ifdef PNG_sCAL_SUPPORTED\r
+   /* Free any sCAL entry */\r
+   if ((mask & PNG_FREE_SCAL) & info_ptr->free_me)\r
+   {\r
+#if defined(PNG_FIXED_POINT_SUPPORTED) && !defined(PNG_FLOATING_POINT_SUPPORTED)\r
+      png_free(png_ptr, info_ptr->scal_s_width);\r
+      png_free(png_ptr, info_ptr->scal_s_height);\r
+      info_ptr->scal_s_width = NULL;\r
+      info_ptr->scal_s_height = NULL;\r
+#endif\r
+      info_ptr->valid &= ~PNG_INFO_sCAL;\r
+   }\r
+#endif\r
+\r
+#ifdef PNG_pCAL_SUPPORTED\r
+   /* Free any pCAL entry */\r
+   if ((mask & PNG_FREE_PCAL) & info_ptr->free_me)\r
+   {\r
+      png_free(png_ptr, info_ptr->pcal_purpose);\r
+      png_free(png_ptr, info_ptr->pcal_units);\r
+      info_ptr->pcal_purpose = NULL;\r
+      info_ptr->pcal_units = NULL;\r
+      if (info_ptr->pcal_params != NULL)\r
+         {\r
+            int i;\r
+            for (i = 0; i < (int)info_ptr->pcal_nparams; i++)\r
+            {\r
+               png_free(png_ptr, info_ptr->pcal_params[i]);\r
+               info_ptr->pcal_params[i] = NULL;\r
+            }\r
+            png_free(png_ptr, info_ptr->pcal_params);\r
+            info_ptr->pcal_params = NULL;\r
+         }\r
+      info_ptr->valid &= ~PNG_INFO_pCAL;\r
+   }\r
+#endif\r
+\r
+#ifdef PNG_iCCP_SUPPORTED\r
+   /* Free any iCCP entry */\r
+   if ((mask & PNG_FREE_ICCP) & info_ptr->free_me)\r
+   {\r
+      png_free(png_ptr, info_ptr->iccp_name);\r
+      png_free(png_ptr, info_ptr->iccp_profile);\r
+      info_ptr->iccp_name = NULL;\r
+      info_ptr->iccp_profile = NULL;\r
+      info_ptr->valid &= ~PNG_INFO_iCCP;\r
+   }\r
+#endif\r
+\r
+#ifdef PNG_sPLT_SUPPORTED\r
+   /* Free a given sPLT entry, or (if num == -1) all sPLT entries */\r
+   if ((mask & PNG_FREE_SPLT) & info_ptr->free_me)\r
+   {\r
+      if (num != -1)\r
+      {\r
+         if (info_ptr->splt_palettes)\r
+         {\r
+            png_free(png_ptr, info_ptr->splt_palettes[num].name);\r
+            png_free(png_ptr, info_ptr->splt_palettes[num].entries);\r
+            info_ptr->splt_palettes[num].name = NULL;\r
+            info_ptr->splt_palettes[num].entries = NULL;\r
+         }\r
+      }\r
+      else\r
+      {\r
+         if (info_ptr->splt_palettes_num)\r
+         {\r
+            int i;\r
+            for (i = 0; i < (int)info_ptr->splt_palettes_num; i++)\r
+               png_free_data(png_ptr, info_ptr, PNG_FREE_SPLT, i);\r
+\r
+            png_free(png_ptr, info_ptr->splt_palettes);\r
+            info_ptr->splt_palettes = NULL;\r
+            info_ptr->splt_palettes_num = 0;\r
+         }\r
+         info_ptr->valid &= ~PNG_INFO_sPLT;\r
+      }\r
+   }\r
+#endif\r
+\r
+#ifdef PNG_UNKNOWN_CHUNKS_SUPPORTED\r
+   if (png_ptr->unknown_chunk.data)\r
+   {\r
+      png_free(png_ptr, png_ptr->unknown_chunk.data);\r
+      png_ptr->unknown_chunk.data = NULL;\r
+   }\r
+\r
+   if ((mask & PNG_FREE_UNKN) & info_ptr->free_me)\r
+   {\r
+      if (num != -1)\r
+      {\r
+          if (info_ptr->unknown_chunks)\r
+          {\r
+             png_free(png_ptr, info_ptr->unknown_chunks[num].data);\r
+             info_ptr->unknown_chunks[num].data = NULL;\r
+          }\r
+      }\r
+      else\r
+      {\r
+         int i;\r
+\r
+         if (info_ptr->unknown_chunks_num)\r
+         {\r
+            for (i = 0; i < (int)info_ptr->unknown_chunks_num; i++)\r
+               png_free_data(png_ptr, info_ptr, PNG_FREE_UNKN, i);\r
+\r
+            png_free(png_ptr, info_ptr->unknown_chunks);\r
+            info_ptr->unknown_chunks = NULL;\r
+            info_ptr->unknown_chunks_num = 0;\r
+         }\r
+      }\r
+   }\r
+#endif\r
+\r
+#ifdef PNG_hIST_SUPPORTED\r
+   /* Free any hIST entry */\r
+   if ((mask & PNG_FREE_HIST)  & info_ptr->free_me)\r
+   {\r
+      png_free(png_ptr, info_ptr->hist);\r
+      info_ptr->hist = NULL;\r
+      info_ptr->valid &= ~PNG_INFO_hIST;\r
+   }\r
+#endif\r
+\r
+   /* Free any PLTE entry that was internally allocated */\r
+   if ((mask & PNG_FREE_PLTE) & info_ptr->free_me)\r
+   {\r
+      png_zfree(png_ptr, info_ptr->palette);\r
+      info_ptr->palette = NULL;\r
+      info_ptr->valid &= ~PNG_INFO_PLTE;\r
+      info_ptr->num_palette = 0;\r
+   }\r
+\r
+#ifdef PNG_INFO_IMAGE_SUPPORTED\r
+   /* Free any image bits attached to the info structure */\r
+   if ((mask & PNG_FREE_ROWS) & info_ptr->free_me)\r
+   {\r
+      if (info_ptr->row_pointers)\r
+      {\r
+         int row;\r
+         for (row = 0; row < (int)info_ptr->height; row++)\r
+         {\r
+            png_free(png_ptr, info_ptr->row_pointers[row]);\r
+            info_ptr->row_pointers[row] = NULL;\r
+         }\r
+         png_free(png_ptr, info_ptr->row_pointers);\r
+         info_ptr->row_pointers = NULL;\r
+      }\r
+      info_ptr->valid &= ~PNG_INFO_IDAT;\r
+   }\r
+#endif\r
+\r
+   if (num == -1)\r
+      info_ptr->free_me &= ~mask;\r
+   else\r
+      info_ptr->free_me &= ~(mask & ~PNG_FREE_MUL);\r
+}\r
+\r
+/* This is an internal routine to free any memory that the info struct is\r
+ * pointing to before re-using it or freeing the struct itself.  Recall\r
+ * that png_free() checks for NULL pointers for us.\r
+ */\r
+void /* PRIVATE */\r
+png_info_destroy(png_structp png_ptr, png_infop info_ptr)\r
+{\r
+   png_debug(1, "in png_info_destroy");\r
+\r
+   png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);\r
+\r
+#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED\r
+   if (png_ptr->num_chunk_list)\r
+   {\r
+      png_free(png_ptr, png_ptr->chunk_list);\r
+      png_ptr->chunk_list = NULL;\r
+      png_ptr->num_chunk_list = 0;\r
+   }\r
+#endif\r
+\r
+   png_info_init_3(&info_ptr, png_sizeof(png_info));\r
+}\r
+#endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */\r
+\r
+/* This function returns a pointer to the io_ptr associated with the user\r
+ * functions.  The application should free any memory associated with this\r
+ * pointer before png_write_destroy() or png_read_destroy() are called.\r
+ */\r
+png_voidp PNGAPI\r
+png_get_io_ptr(png_structp png_ptr)\r
+{\r
+   if (png_ptr == NULL)\r
+      return (NULL);\r
+   return (png_ptr->io_ptr);\r
+}\r
+\r
+#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)\r
+#ifdef PNG_STDIO_SUPPORTED\r
+/* Initialize the default input/output functions for the PNG file.  If you\r
+ * use your own read or write routines, you can call either png_set_read_fn()\r
+ * or png_set_write_fn() instead of png_init_io().  If you have defined\r
+ * PNG_NO_STDIO, you must use a function of your own because "FILE *" isn't\r
+ * necessarily available.\r
+ */\r
+void PNGAPI\r
+png_init_io(png_structp png_ptr, png_FILE_p fp)\r
+{\r
+   png_debug(1, "in png_init_io");\r
+\r
+   if (png_ptr == NULL)\r
+      return;\r
+\r
+   png_ptr->io_ptr = (png_voidp)fp;\r
+}\r
+#endif\r
+\r
+#ifdef PNG_TIME_RFC1123_SUPPORTED\r
+/* Convert the supplied time into an RFC 1123 string suitable for use in\r
+ * a "Creation Time" or other text-based time string.\r
+ */\r
+png_charp PNGAPI\r
+png_convert_to_rfc1123(png_structp png_ptr, png_timep ptime)\r
+{\r
+   static PNG_CONST char short_months[12][4] =\r
+        {"Jan", "Feb", "Mar", "Apr", "May", "Jun",\r
+         "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};\r
+\r
+   if (png_ptr == NULL)\r
+      return (NULL);\r
+   if (png_ptr->time_buffer == NULL)\r
+   {\r
+      png_ptr->time_buffer = (png_charp)png_malloc(png_ptr, (png_uint_32)(29*\r
+         png_sizeof(char)));\r
+   }\r
+\r
+#ifdef USE_FAR_KEYWORD\r
+   {\r
+      char near_time_buf[29];\r
+      png_snprintf6(near_time_buf, 29, "%d %s %d %02d:%02d:%02d +0000",\r
+          ptime->day % 32, short_months[(ptime->month - 1) % 12],\r
+          ptime->year, ptime->hour % 24, ptime->minute % 60,\r
+          ptime->second % 61);\r
+      png_memcpy(png_ptr->time_buffer, near_time_buf,\r
+          29*png_sizeof(char));\r
+   }\r
+#else\r
+   png_snprintf6(png_ptr->time_buffer, 29, "%d %s %d %02d:%02d:%02d +0000",\r
+       ptime->day % 32, short_months[(ptime->month - 1) % 12],\r
+       ptime->year, ptime->hour % 24, ptime->minute % 60,\r
+       ptime->second % 61);\r
+#endif\r
+   return ((png_charp)png_ptr->time_buffer);\r
+}\r
+#endif /* PNG_TIME_RFC1123_SUPPORTED */\r
+\r
+#endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */\r
+\r
+png_charp PNGAPI\r
+png_get_copyright(png_structp png_ptr)\r
+{\r
+   png_ptr = png_ptr;  /* Silence compiler warning about unused png_ptr */\r
+#ifdef PNG_STRING_COPYRIGHT\r
+      return PNG_STRING_COPYRIGHT\r
+#else\r
+#ifdef __STDC__\r
+   return ((png_charp) PNG_STRING_NEWLINE \\r
+     "libpng version 1.4.3 - June 26, 2010" PNG_STRING_NEWLINE \\r
+     "Copyright (c) 1998-2010 Glenn Randers-Pehrson" PNG_STRING_NEWLINE \\r
+     "Copyright (c) 1996-1997 Andreas Dilger" PNG_STRING_NEWLINE \\r
+     "Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc." \\r
+     PNG_STRING_NEWLINE);\r
+#else\r
+      return ((png_charp) "libpng version 1.4.3 - June 26, 2010\\r
+      Copyright (c) 1998-2010 Glenn Randers-Pehrson\\r
+      Copyright (c) 1996-1997 Andreas Dilger\\r
+      Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc.");\r
+#endif\r
+#endif\r
+}\r
+\r
+/* The following return the library version as a short string in the\r
+ * format 1.0.0 through 99.99.99zz.  To get the version of *.h files\r
+ * used with your application, print out PNG_LIBPNG_VER_STRING, which\r
+ * is defined in png.h.\r
+ * Note: now there is no difference between png_get_libpng_ver() and\r
+ * png_get_header_ver().  Due to the version_nn_nn_nn typedef guard,\r
+ * it is guaranteed that png.c uses the correct version of png.h.\r
+ */\r
+png_charp PNGAPI\r
+png_get_libpng_ver(png_structp png_ptr)\r
+{\r
+   /* Version of *.c files used when building libpng */\r
+   png_ptr = png_ptr;  /* Silence compiler warning about unused png_ptr */\r
+   return ((png_charp) PNG_LIBPNG_VER_STRING);\r
+}\r
+\r
+png_charp PNGAPI\r
+png_get_header_ver(png_structp png_ptr)\r
+{\r
+   /* Version of *.h files used when building libpng */\r
+   png_ptr = png_ptr;  /* Silence compiler warning about unused png_ptr */\r
+   return ((png_charp) PNG_LIBPNG_VER_STRING);\r
+}\r
+\r
+png_charp PNGAPI\r
+png_get_header_version(png_structp png_ptr)\r
+{\r
+   /* Returns longer string containing both version and date */\r
+   png_ptr = png_ptr;  /* Silence compiler warning about unused png_ptr */\r
+#ifdef __STDC__\r
+   return ((png_charp) PNG_HEADER_VERSION_STRING\r
+#ifndef PNG_READ_SUPPORTED\r
+   "     (NO READ SUPPORT)"\r
+#endif\r
+   PNG_STRING_NEWLINE);\r
+#else\r
+   return ((png_charp) PNG_HEADER_VERSION_STRING);\r
+#endif\r
+}\r
+\r
+#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)\r
+#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED\r
+int PNGAPI\r
+png_handle_as_unknown(png_structp png_ptr, png_bytep chunk_name)\r
+{\r
+   /* Check chunk_name and return "keep" value if it's on the list, else 0 */\r
+   int i;\r
+   png_bytep p;\r
+   if (png_ptr == NULL || chunk_name == NULL || png_ptr->num_chunk_list<=0)\r
+      return 0;\r
+   p = png_ptr->chunk_list + png_ptr->num_chunk_list*5 - 5;\r
+   for (i = png_ptr->num_chunk_list; i; i--, p -= 5)\r
+      if (!png_memcmp(chunk_name, p, 4))\r
+        return ((int)*(p + 4));\r
+   return 0;\r
+}\r
+#endif\r
+#endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */\r
+\r
+#ifdef PNG_READ_SUPPORTED\r
+/* This function, added to libpng-1.0.6g, is untested. */\r
+int PNGAPI\r
+png_reset_zstream(png_structp png_ptr)\r
+{\r
+   if (png_ptr == NULL)\r
+      return Z_STREAM_ERROR;\r
+   return (inflateReset(&png_ptr->zstream));\r
+}\r
+#endif /* PNG_READ_SUPPORTED */\r
+\r
+/* This function was added to libpng-1.0.7 */\r
+png_uint_32 PNGAPI\r
+png_access_version_number(void)\r
+{\r
+   /* Version of *.c files used when building libpng */\r
+   return((png_uint_32) PNG_LIBPNG_VER);\r
+}\r
+\r
+\r
+\r
+#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)\r
+#ifdef PNG_SIZE_T\r
+/* Added at libpng version 1.2.6 */\r
+   PNG_EXTERN png_size_t PNGAPI png_convert_size PNGARG((size_t size));\r
+png_size_t PNGAPI\r
+png_convert_size(size_t size)\r
+{\r
+   if (size > (png_size_t)-1)\r
+      PNG_ABORT();  /* We haven't got access to png_ptr, so no png_error() */\r
+   return ((png_size_t)size);\r
+}\r
+#endif /* PNG_SIZE_T */\r
+\r
+/* Added at libpng version 1.2.34 and 1.4.0 (moved from pngset.c) */\r
+#ifdef PNG_cHRM_SUPPORTED\r
+#ifdef PNG_CHECK_cHRM_SUPPORTED\r
+\r
+/*\r
+ *    Multiply two 32-bit numbers, V1 and V2, using 32-bit\r
+ *    arithmetic, to produce a 64 bit result in the HI/LO words.\r
+ *\r
+ *                  A B\r
+ *                x C D\r
+ *               ------\r
+ *              AD || BD\r
+ *        AC || CB || 0\r
+ *\r
+ *    where A and B are the high and low 16-bit words of V1,\r
+ *    C and D are the 16-bit words of V2, AD is the product of\r
+ *    A and D, and X || Y is (X << 16) + Y.\r
+*/\r
+\r
+void /* PRIVATE */\r
+png_64bit_product (long v1, long v2, unsigned long *hi_product,\r
+   unsigned long *lo_product)\r
+{\r
+   int a, b, c, d;\r
+   long lo, hi, x, y;\r
+\r
+   a = (v1 >> 16) & 0xffff;\r
+   b = v1 & 0xffff;\r
+   c = (v2 >> 16) & 0xffff;\r
+   d = v2 & 0xffff;\r
+\r
+   lo = b * d;                   /* BD */\r
+   x = a * d + c * b;            /* AD + CB */\r
+   y = ((lo >> 16) & 0xffff) + x;\r
+\r
+   lo = (lo & 0xffff) | ((y & 0xffff) << 16);\r
+   hi = (y >> 16) & 0xffff;\r
+\r
+   hi += a * c;                  /* AC */\r
+\r
+   *hi_product = (unsigned long)hi;\r
+   *lo_product = (unsigned long)lo;\r
+}\r
+\r
+int /* PRIVATE */\r
+png_check_cHRM_fixed(png_structp png_ptr,\r
+   png_fixed_point white_x, png_fixed_point white_y, png_fixed_point red_x,\r
+   png_fixed_point red_y, png_fixed_point green_x, png_fixed_point green_y,\r
+   png_fixed_point blue_x, png_fixed_point blue_y)\r
+{\r
+   int ret = 1;\r
+   unsigned long xy_hi,xy_lo,yx_hi,yx_lo;\r
+\r
+   png_debug(1, "in function png_check_cHRM_fixed");\r
+\r
+   if (png_ptr == NULL)\r
+      return 0;\r
+\r
+   if (white_x < 0 || white_y <= 0 ||\r
+         red_x < 0 ||   red_y <  0 ||\r
+       green_x < 0 || green_y <  0 ||\r
+        blue_x < 0 ||  blue_y <  0)\r
+   {\r
+      png_warning(png_ptr,\r
+        "Ignoring attempt to set negative chromaticity value");\r
+      ret = 0;\r
+   }\r
+   if (white_x > (png_fixed_point) PNG_UINT_31_MAX ||\r
+       white_y > (png_fixed_point) PNG_UINT_31_MAX ||\r
+         red_x > (png_fixed_point) PNG_UINT_31_MAX ||\r
+         red_y > (png_fixed_point) PNG_UINT_31_MAX ||\r
+       green_x > (png_fixed_point) PNG_UINT_31_MAX ||\r
+       green_y > (png_fixed_point) PNG_UINT_31_MAX ||\r
+        blue_x > (png_fixed_point) PNG_UINT_31_MAX ||\r
+        blue_y > (png_fixed_point) PNG_UINT_31_MAX )\r
+   {\r
+      png_warning(png_ptr,\r
+        "Ignoring attempt to set chromaticity value exceeding 21474.83");\r
+      ret = 0;\r
+   }\r
+   if (white_x > 100000L - white_y)\r
+   {\r
+      png_warning(png_ptr, "Invalid cHRM white point");\r
+      ret = 0;\r
+   }\r
+   if (red_x > 100000L - red_y)\r
+   {\r
+      png_warning(png_ptr, "Invalid cHRM red point");\r
+      ret = 0;\r
+   }\r
+   if (green_x > 100000L - green_y)\r
+   {\r
+      png_warning(png_ptr, "Invalid cHRM green point");\r
+      ret = 0;\r
+   }\r
+   if (blue_x > 100000L - blue_y)\r
+   {\r
+      png_warning(png_ptr, "Invalid cHRM blue point");\r
+      ret = 0;\r
+   }\r
+\r
+   png_64bit_product(green_x - red_x, blue_y - red_y, &xy_hi, &xy_lo);\r
+   png_64bit_product(green_y - red_y, blue_x - red_x, &yx_hi, &yx_lo);\r
+\r
+   if (xy_hi == yx_hi && xy_lo == yx_lo)\r
+   {\r
+      png_warning(png_ptr,\r
+         "Ignoring attempt to set cHRM RGB triangle with zero area");\r
+      ret = 0;\r
+   }\r
+\r
+   return ret;\r
+}\r
+#endif /* PNG_CHECK_cHRM_SUPPORTED */\r
+#endif /* PNG_cHRM_SUPPORTED */\r
+\r
+void /* PRIVATE */\r
+png_check_IHDR(png_structp png_ptr,\r
+   png_uint_32 width, png_uint_32 height, int bit_depth,\r
+   int color_type, int interlace_type, int compression_type,\r
+   int filter_type)\r
+{\r
+   int error = 0;\r
+\r
+   /* Check for width and height valid values */\r
+   if (width == 0)\r
+   {\r
+      png_warning(png_ptr, "Image width is zero in IHDR");\r
+      error = 1;\r
+   }\r
+\r
+   if (height == 0)\r
+   {\r
+      png_warning(png_ptr, "Image height is zero in IHDR");\r
+      error = 1;\r
+   }\r
+\r
+#ifdef PNG_SET_USER_LIMITS_SUPPORTED\r
+   if (width > png_ptr->user_width_max || width > PNG_USER_WIDTH_MAX)\r
+#else\r
+   if (width > PNG_USER_WIDTH_MAX)\r
+#endif\r
+   {\r
+      png_warning(png_ptr, "Image width exceeds user limit in IHDR");\r
+      error = 1;\r
+   }\r
+\r
+#ifdef PNG_SET_USER_LIMITS_SUPPORTED\r
+   if (height > png_ptr->user_height_max || height > PNG_USER_HEIGHT_MAX)\r
+#else\r
+   if (height > PNG_USER_HEIGHT_MAX)\r
+#endif\r
+   {\r
+      png_warning(png_ptr, "Image height exceeds user limit in IHDR");\r
+      error = 1;\r
+   }\r
+\r
+   if (width > PNG_UINT_31_MAX)\r
+   {\r
+      png_warning(png_ptr, "Invalid image width in IHDR");\r
+      error = 1;\r
+   }\r
+\r
+   if ( height > PNG_UINT_31_MAX)\r
+   {\r
+      png_warning(png_ptr, "Invalid image height in IHDR");\r
+      error = 1;\r
+   }\r
+\r
+   if ( width > (PNG_UINT_32_MAX\r
+                 >> 3)      /* 8-byte RGBA pixels */\r
+                 - 64       /* bigrowbuf hack */\r
+                 - 1        /* filter byte */\r
+                 - 7*8      /* rounding of width to multiple of 8 pixels */\r
+                 - 8)       /* extra max_pixel_depth pad */\r
+      png_warning(png_ptr, "Width is too large for libpng to process pixels");\r
+\r
+   /* Check other values */\r
+   if (bit_depth != 1 && bit_depth != 2 && bit_depth != 4 &&\r
+       bit_depth != 8 && bit_depth != 16)\r
+   {\r
+      png_warning(png_ptr, "Invalid bit depth in IHDR");\r
+      error = 1;\r
+   }\r
+\r
+   if (color_type < 0 || color_type == 1 ||\r
+       color_type == 5 || color_type > 6)\r
+   {\r
+      png_warning(png_ptr, "Invalid color type in IHDR");\r
+      error = 1;\r
+   }\r
+\r
+   if (((color_type == PNG_COLOR_TYPE_PALETTE) && bit_depth > 8) ||\r
+       ((color_type == PNG_COLOR_TYPE_RGB ||\r
+         color_type == PNG_COLOR_TYPE_GRAY_ALPHA ||\r
+         color_type == PNG_COLOR_TYPE_RGB_ALPHA) && bit_depth < 8))\r
+   {\r
+      png_warning(png_ptr, "Invalid color type/bit depth combination in IHDR");\r
+      error = 1;\r
+   }\r
+\r
+   if (interlace_type >= PNG_INTERLACE_LAST)\r
+   {\r
+      png_warning(png_ptr, "Unknown interlace method in IHDR");\r
+      error = 1;\r
+   }\r
+\r
+   if (compression_type != PNG_COMPRESSION_TYPE_BASE)\r
+   {\r
+      png_warning(png_ptr, "Unknown compression method in IHDR");\r
+      error = 1;\r
+   }\r
+\r
+#ifdef PNG_MNG_FEATURES_SUPPORTED\r
+   /* Accept filter_method 64 (intrapixel differencing) only if\r
+    * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and\r
+    * 2. Libpng did not read a PNG signature (this filter_method is only\r
+    *    used in PNG datastreams that are embedded in MNG datastreams) and\r
+    * 3. The application called png_permit_mng_features with a mask that\r
+    *    included PNG_FLAG_MNG_FILTER_64 and\r
+    * 4. The filter_method is 64 and\r
+    * 5. The color_type is RGB or RGBA\r
+    */\r
+   if ((png_ptr->mode & PNG_HAVE_PNG_SIGNATURE) &&\r
+       png_ptr->mng_features_permitted)\r
+      png_warning(png_ptr, "MNG features are not allowed in a PNG datastream");\r
+\r
+   if (filter_type != PNG_FILTER_TYPE_BASE)\r
+   {\r
+      if (!((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&\r
+         (filter_type == PNG_INTRAPIXEL_DIFFERENCING) &&\r
+         ((png_ptr->mode & PNG_HAVE_PNG_SIGNATURE) == 0) &&\r
+         (color_type == PNG_COLOR_TYPE_RGB ||\r
+         color_type == PNG_COLOR_TYPE_RGB_ALPHA)))\r
+      {\r
+         png_warning(png_ptr, "Unknown filter method in IHDR");\r
+         error = 1;\r
+      }\r
+\r
+      if (png_ptr->mode & PNG_HAVE_PNG_SIGNATURE)\r
+      {\r
+         png_warning(png_ptr, "Invalid filter method in IHDR");\r
+         error = 1;\r
+      }\r
+   }\r
+\r
+#else\r
+   if (filter_type != PNG_FILTER_TYPE_BASE)\r
+   {\r
+      png_warning(png_ptr, "Unknown filter method in IHDR");\r
+      error = 1;\r
+   }\r
+#endif\r
+\r
+   if (error == 1)\r
+      png_error(png_ptr, "Invalid IHDR data");\r
+}\r
+#endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */\r
diff --git a/3rdparty/libpng/png.h b/3rdparty/libpng/png.h
new file mode 100644 (file)
index 0000000..842f3fc
--- /dev/null
@@ -0,0 +1,2701 @@
+
+/* png.h - header file for PNG reference library
+ *
+ * libpng version 1.4.3 - June 26, 2010
+ * Copyright (c) 1998-2010 Glenn Randers-Pehrson
+ * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
+ * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
+ *
+ * This code is released under the libpng license (See LICENSE, below)
+ *
+ * Authors and maintainers:
+ *  libpng versions 0.71, May 1995, through 0.88, January 1996: Guy Schalnat
+ *  libpng versions 0.89c, June 1996, through 0.96, May 1997: Andreas Dilger
+ *  libpng versions 0.97, January 1998, through 1.4.3 - June 26, 2010: Glenn
+ *  See also "Contributing Authors", below.
+ *
+ * Note about libpng version numbers:
+ *
+ *    Due to various miscommunications, unforeseen code incompatibilities
+ *    and occasional factors outside the authors' control, version numbering
+ *    on the library has not always been consistent and straightforward.
+ *    The following table summarizes matters since version 0.89c, which was
+ *    the first widely used release:
+ *
+ *    source                 png.h  png.h  shared-lib
+ *    version                string   int  version
+ *    -------                ------ -----  ----------
+ *    0.89c "1.0 beta 3"     0.89      89  1.0.89
+ *    0.90  "1.0 beta 4"     0.90      90  0.90  [should have been 2.0.90]
+ *    0.95  "1.0 beta 5"     0.95      95  0.95  [should have been 2.0.95]
+ *    0.96  "1.0 beta 6"     0.96      96  0.96  [should have been 2.0.96]
+ *    0.97b "1.00.97 beta 7" 1.00.97   97  1.0.1 [should have been 2.0.97]
+ *    0.97c                  0.97      97  2.0.97
+ *    0.98                   0.98      98  2.0.98
+ *    0.99                   0.99      98  2.0.99
+ *    0.99a-m                0.99      99  2.0.99
+ *    1.00                   1.00     100  2.1.0 [100 should be 10000]
+ *    1.0.0      (from here on, the   100  2.1.0 [100 should be 10000]
+ *    1.0.1       png.h string is   10001  2.1.0
+ *    1.0.1a-e    identical to the  10002  from here on, the shared library
+ *    1.0.2       source version)   10002  is 2.V where V is the source code
+ *    1.0.2a-b                      10003  version, except as noted.
+ *    1.0.3                         10003
+ *    1.0.3a-d                      10004
+ *    1.0.4                         10004
+ *    1.0.4a-f                      10005
+ *    1.0.5 (+ 2 patches)           10005
+ *    1.0.5a-d                      10006
+ *    1.0.5e-r                      10100 (not source compatible)
+ *    1.0.5s-v                      10006 (not binary compatible)
+ *    1.0.6 (+ 3 patches)           10006 (still binary incompatible)
+ *    1.0.6d-f                      10007 (still binary incompatible)
+ *    1.0.6g                        10007
+ *    1.0.6h                        10007  10.6h (testing xy.z so-numbering)
+ *    1.0.6i                        10007  10.6i
+ *    1.0.6j                        10007  2.1.0.6j (incompatible with 1.0.0)
+ *    1.0.7beta11-14        DLLNUM  10007  2.1.0.7beta11-14 (binary compatible)
+ *    1.0.7beta15-18           1    10007  2.1.0.7beta15-18 (binary compatible)
+ *    1.0.7rc1-2               1    10007  2.1.0.7rc1-2 (binary compatible)
+ *    1.0.7                    1    10007  (still compatible)
+ *    1.0.8beta1-4             1    10008  2.1.0.8beta1-4
+ *    1.0.8rc1                 1    10008  2.1.0.8rc1
+ *    1.0.8                    1    10008  2.1.0.8
+ *    1.0.9beta1-6             1    10009  2.1.0.9beta1-6
+ *    1.0.9rc1                 1    10009  2.1.0.9rc1
+ *    1.0.9beta7-10            1    10009  2.1.0.9beta7-10
+ *    1.0.9rc2                 1    10009  2.1.0.9rc2
+ *    1.0.9                    1    10009  2.1.0.9
+ *    1.0.10beta1              1    10010  2.1.0.10beta1
+ *    1.0.10rc1                1    10010  2.1.0.10rc1
+ *    1.0.10                   1    10010  2.1.0.10
+ *    1.0.11beta1-3            1    10011  2.1.0.11beta1-3
+ *    1.0.11rc1                1    10011  2.1.0.11rc1
+ *    1.0.11                   1    10011  2.1.0.11
+ *    1.0.12beta1-2            2    10012  2.1.0.12beta1-2
+ *    1.0.12rc1                2    10012  2.1.0.12rc1
+ *    1.0.12                   2    10012  2.1.0.12
+ *    1.1.0a-f                 -    10100  2.1.1.0a-f (branch abandoned)
+ *    1.2.0beta1-2             2    10200  2.1.2.0beta1-2
+ *    1.2.0beta3-5             3    10200  3.1.2.0beta3-5
+ *    1.2.0rc1                 3    10200  3.1.2.0rc1
+ *    1.2.0                    3    10200  3.1.2.0
+ *    1.2.1beta1-4             3    10201  3.1.2.1beta1-4
+ *    1.2.1rc1-2               3    10201  3.1.2.1rc1-2
+ *    1.2.1                    3    10201  3.1.2.1
+ *    1.2.2beta1-6            12    10202  12.so.0.1.2.2beta1-6
+ *    1.0.13beta1             10    10013  10.so.0.1.0.13beta1
+ *    1.0.13rc1               10    10013  10.so.0.1.0.13rc1
+ *    1.2.2rc1                12    10202  12.so.0.1.2.2rc1
+ *    1.0.13                  10    10013  10.so.0.1.0.13
+ *    1.2.2                   12    10202  12.so.0.1.2.2
+ *    1.2.3rc1-6              12    10203  12.so.0.1.2.3rc1-6
+ *    1.2.3                   12    10203  12.so.0.1.2.3
+ *    1.2.4beta1-3            13    10204  12.so.0.1.2.4beta1-3
+ *    1.0.14rc1               13    10014  10.so.0.1.0.14rc1
+ *    1.2.4rc1                13    10204  12.so.0.1.2.4rc1
+ *    1.0.14                  10    10014  10.so.0.1.0.14
+ *    1.2.4                   13    10204  12.so.0.1.2.4
+ *    1.2.5beta1-2            13    10205  12.so.0.1.2.5beta1-2
+ *    1.0.15rc1-3             10    10015  10.so.0.1.0.15rc1-3
+ *    1.2.5rc1-3              13    10205  12.so.0.1.2.5rc1-3
+ *    1.0.15                  10    10015  10.so.0.1.0.15
+ *    1.2.5                   13    10205  12.so.0.1.2.5
+ *    1.2.6beta1-4            13    10206  12.so.0.1.2.6beta1-4
+ *    1.0.16                  10    10016  10.so.0.1.0.16
+ *    1.2.6                   13    10206  12.so.0.1.2.6
+ *    1.2.7beta1-2            13    10207  12.so.0.1.2.7beta1-2
+ *    1.0.17rc1               10    10017  12.so.0.1.0.17rc1
+ *    1.2.7rc1                13    10207  12.so.0.1.2.7rc1
+ *    1.0.17                  10    10017  12.so.0.1.0.17
+ *    1.2.7                   13    10207  12.so.0.1.2.7
+ *    1.2.8beta1-5            13    10208  12.so.0.1.2.8beta1-5
+ *    1.0.18rc1-5             10    10018  12.so.0.1.0.18rc1-5
+ *    1.2.8rc1-5              13    10208  12.so.0.1.2.8rc1-5
+ *    1.0.18                  10    10018  12.so.0.1.0.18
+ *    1.2.8                   13    10208  12.so.0.1.2.8
+ *    1.2.9beta1-3            13    10209  12.so.0.1.2.9beta1-3
+ *    1.2.9beta4-11           13    10209  12.so.0.9[.0]
+ *    1.2.9rc1                13    10209  12.so.0.9[.0]
+ *    1.2.9                   13    10209  12.so.0.9[.0]
+ *    1.2.10beta1-7           13    10210  12.so.0.10[.0]
+ *    1.2.10rc1-2             13    10210  12.so.0.10[.0]
+ *    1.2.10                  13    10210  12.so.0.10[.0]
+ *    1.4.0beta1-5            14    10400  14.so.0.0[.0]
+ *    1.2.11beta1-4           13    10211  12.so.0.11[.0]
+ *    1.4.0beta7-8            14    10400  14.so.0.0[.0]
+ *    1.2.11                  13    10211  12.so.0.11[.0]
+ *    1.2.12                  13    10212  12.so.0.12[.0]
+ *    1.4.0beta9-14           14    10400  14.so.0.0[.0]
+ *    1.2.13                  13    10213  12.so.0.13[.0]
+ *    1.4.0beta15-36          14    10400  14.so.0.0[.0]
+ *    1.4.0beta37-87          14    10400  14.so.14.0[.0]
+ *    1.4.0rc01               14    10400  14.so.14.0[.0]
+ *    1.4.0beta88-109         14    10400  14.so.14.0[.0]
+ *    1.4.0rc02-08            14    10400  14.so.14.0[.0]
+ *    1.4.0                   14    10400  14.so.14.0[.0]
+ *    1.4.1beta01-03          14    10401  14.so.14.1[.0]
+ *    1.4.1rc01               14    10401  14.so.14.1[.0]
+ *    1.4.1beta04-12          14    10401  14.so.14.1[.0]
+ *    1.4.1rc02-04            14    10401  14.so.14.1[.0]
+ *    1.4.1                   14    10401  14.so.14.1[.0]
+ *    1.4.2beta01             14    10402  14.so.14.2[.0]
+ *    1.4.2rc02-06            14    10402  14.so.14.2[.0]
+ *    1.4.2                   14    10402  14.so.14.2[.0]
+ *    1.4.3beta01-05          14    10403  14.so.14.3[.0]
+ *    1.4.3rc01-03            14    10403  14.so.14.3[.0]
+ *    1.4.3                   14    10403  14.so.14.3[.0]
+ *
+ *    Henceforth the source version will match the shared-library major
+ *    and minor numbers; the shared-library major version number will be
+ *    used for changes in backward compatibility, as it is intended.  The
+ *    PNG_LIBPNG_VER macro, which is not used within libpng but is available
+ *    for applications, is an unsigned integer of the form xyyzz corresponding
+ *    to the source version x.y.z (leading zeros in y and z).  Beta versions
+ *    were given the previous public release number plus a letter, until
+ *    version 1.0.6j; from then on they were given the upcoming public
+ *    release number plus "betaNN" or "rcN".
+ *
+ *    Binary incompatibility exists only when applications make direct access
+ *    to the info_ptr or png_ptr members through png.h, and the compiled
+ *    application is loaded with a different version of the library.
+ *
+ *    DLLNUM will change each time there are forward or backward changes
+ *    in binary compatibility (e.g., when a new feature is added).
+ *
+ * See libpng.txt or libpng.3 for more information.  The PNG specification
+ * is available as a W3C Recommendation and as an ISO Specification,
+ * <http://www.w3.org/TR/2003/REC-PNG-20031110/
+ */
+
+/*
+ * COPYRIGHT NOTICE, DISCLAIMER, and LICENSE:
+ *
+ * If you modify libpng you may insert additional notices immediately following
+ * this sentence.
+ *
+ * This code is released under the libpng license.
+ *
+ * libpng versions 1.2.6, August 15, 2004, through 1.4.3, June 26, 2010, are
+ * Copyright (c) 2004, 2006-2010 Glenn Randers-Pehrson, and are
+ * distributed according to the same disclaimer and license as libpng-1.2.5
+ * with the following individual added to the list of Contributing Authors:
+ *
+ *    Cosmin Truta
+ *
+ * libpng versions 1.0.7, July 1, 2000, through 1.2.5, October 3, 2002, are
+ * Copyright (c) 2000-2002 Glenn Randers-Pehrson, and are
+ * distributed according to the same disclaimer and license as libpng-1.0.6
+ * with the following individuals added to the list of Contributing Authors:
+ *
+ *    Simon-Pierre Cadieux
+ *    Eric S. Raymond
+ *    Gilles Vollant
+ *
+ * and with the following additions to the disclaimer:
+ *
+ *    There is no warranty against interference with your enjoyment of the
+ *    library or against infringement.  There is no warranty that our
+ *    efforts or the library will fulfill any of your particular purposes
+ *    or needs.  This library is provided with all faults, and the entire
+ *    risk of satisfactory quality, performance, accuracy, and effort is with
+ *    the user.
+ *
+ * libpng versions 0.97, January 1998, through 1.0.6, March 20, 2000, are
+ * Copyright (c) 1998, 1999, 2000 Glenn Randers-Pehrson, and are
+ * distributed according to the same disclaimer and license as libpng-0.96,
+ * with the following individuals added to the list of Contributing Authors:
+ *
+ *    Tom Lane
+ *    Glenn Randers-Pehrson
+ *    Willem van Schaik
+ *
+ * libpng versions 0.89, June 1996, through 0.96, May 1997, are
+ * Copyright (c) 1996, 1997 Andreas Dilger
+ * Distributed according to the same disclaimer and license as libpng-0.88,
+ * with the following individuals added to the list of Contributing Authors:
+ *
+ *    John Bowler
+ *    Kevin Bracey
+ *    Sam Bushell
+ *    Magnus Holmgren
+ *    Greg Roelofs
+ *    Tom Tanner
+ *
+ * libpng versions 0.5, May 1995, through 0.88, January 1996, are
+ * Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
+ *
+ * For the purposes of this copyright and license, "Contributing Authors"
+ * is defined as the following set of individuals:
+ *
+ *    Andreas Dilger
+ *    Dave Martindale
+ *    Guy Eric Schalnat
+ *    Paul Schmidt
+ *    Tim Wegner
+ *
+ * The PNG Reference Library is supplied "AS IS".  The Contributing Authors
+ * and Group 42, Inc. disclaim all warranties, expressed or implied,
+ * including, without limitation, the warranties of merchantability and of
+ * fitness for any purpose.  The Contributing Authors and Group 42, Inc.
+ * assume no liability for direct, indirect, incidental, special, exemplary,
+ * or consequential damages, which may result from the use of the PNG
+ * Reference Library, even if advised of the possibility of such damage.
+ *
+ * Permission is hereby granted to use, copy, modify, and distribute this
+ * source code, or portions hereof, for any purpose, without fee, subject
+ * to the following restrictions:
+ *
+ * 1. The origin of this source code must not be misrepresented.
+ *
+ * 2. Altered versions must be plainly marked as such and
+ * must not be misrepresented as being the original source.
+ *
+ * 3. This Copyright notice may not be removed or altered from
+ *    any source or altered source distribution.
+ *
+ * The Contributing Authors and Group 42, Inc. specifically permit, without
+ * fee, and encourage the use of this source code as a component to
+ * supporting the PNG file format in commercial products.  If you use this
+ * source code in a product, acknowledgment is not required but would be
+ * appreciated.
+ */
+
+/*
+ * A "png_get_copyright" function is available, for convenient use in "about"
+ * boxes and the like:
+ *
+ *     printf("%s",png_get_copyright(NULL));
+ *
+ * Also, the PNG logo (in PNG format, of course) is supplied in the
+ * files "pngbar.png" and "pngbar.jpg (88x31) and "pngnow.png" (98x31).
+ */
+
+/*
+ * Libpng is OSI Certified Open Source Software.  OSI Certified is a
+ * certification mark of the Open Source Initiative.
+ */
+
+/*
+ * The contributing authors would like to thank all those who helped
+ * with testing, bug fixes, and patience.  This wouldn't have been
+ * possible without all of you.
+ *
+ * Thanks to Frank J. T. Wojcik for helping with the documentation.
+ */
+
+/*
+ * Y2K compliance in libpng:
+ * =========================
+ *
+ *    June 26, 2010
+ *
+ *    Since the PNG Development group is an ad-hoc body, we can't make
+ *    an official declaration.
+ *
+ *    This is your unofficial assurance that libpng from version 0.71 and
+ *    upward through 1.4.3 are Y2K compliant.  It is my belief that earlier
+ *    versions were also Y2K compliant.
+ *
+ *    Libpng only has three year fields.  One is a 2-byte unsigned integer
+ *    that will hold years up to 65535.  The other two hold the date in text
+ *    format, and will hold years up to 9999.
+ *
+ *    The integer is
+ *        "png_uint_16 year" in png_time_struct.
+ *
+ *    The strings are
+ *        "png_charp time_buffer" in png_struct and
+ *        "near_time_buffer", which is a local character string in png.c.
+ *
+ *    There are seven time-related functions:
+ *        png.c: png_convert_to_rfc_1123() in png.c
+ *          (formerly png_convert_to_rfc_1152() in error)
+ *        png_convert_from_struct_tm() in pngwrite.c, called in pngwrite.c
+ *        png_convert_from_time_t() in pngwrite.c
+ *        png_get_tIME() in pngget.c
+ *        png_handle_tIME() in pngrutil.c, called in pngread.c
+ *        png_set_tIME() in pngset.c
+ *        png_write_tIME() in pngwutil.c, called in pngwrite.c
+ *
+ *    All handle dates properly in a Y2K environment.  The
+ *    png_convert_from_time_t() function calls gmtime() to convert from system
+ *    clock time, which returns (year - 1900), which we properly convert to
+ *    the full 4-digit year.  There is a possibility that applications using
+ *    libpng are not passing 4-digit years into the png_convert_to_rfc_1123()
+ *    function, or that they are incorrectly passing only a 2-digit year
+ *    instead of "year - 1900" into the png_convert_from_struct_tm() function,
+ *    but this is not under our control.  The libpng documentation has always
+ *    stated that it works with 4-digit years, and the APIs have been
+ *    documented as such.
+ *
+ *    The tIME chunk itself is also Y2K compliant.  It uses a 2-byte unsigned
+ *    integer to hold the year, and can hold years as large as 65535.
+ *
+ *    zlib, upon which libpng depends, is also Y2K compliant.  It contains
+ *    no date-related code.
+ *
+ *       Glenn Randers-Pehrson
+ *       libpng maintainer
+ *       PNG Development Group
+ */
+
+#ifndef PNG_H
+#define PNG_H
+
+/* This is not the place to learn how to use libpng.  The file libpng.txt
+ * describes how to use libpng, and the file example.c summarizes it
+ * with some code on which to build.  This file is useful for looking
+ * at the actual function definitions and structure components.
+ */
+
+/* Version information for png.h - this should match the version in png.c */
+#define PNG_LIBPNG_VER_STRING "1.4.3"
+#define PNG_HEADER_VERSION_STRING \
+   " libpng version 1.4.3 - June 26, 2010\n"
+
+#define PNG_LIBPNG_VER_SONUM   14
+#define PNG_LIBPNG_VER_DLLNUM  14
+
+/* These should match the first 3 components of PNG_LIBPNG_VER_STRING: */
+#define PNG_LIBPNG_VER_MAJOR   1
+#define PNG_LIBPNG_VER_MINOR   4
+#define PNG_LIBPNG_VER_RELEASE 3
+/* This should match the numeric part of the final component of
+ * PNG_LIBPNG_VER_STRING, omitting any leading zero:
+ */
+
+#define PNG_LIBPNG_VER_BUILD  0
+
+/* Release Status */
+#define PNG_LIBPNG_BUILD_ALPHA    1
+#define PNG_LIBPNG_BUILD_BETA     2
+#define PNG_LIBPNG_BUILD_RC       3
+#define PNG_LIBPNG_BUILD_STABLE   4
+#define PNG_LIBPNG_BUILD_RELEASE_STATUS_MASK 7
+
+/* Release-Specific Flags */
+#define PNG_LIBPNG_BUILD_PATCH    8 /* Can be OR'ed with
+                                       PNG_LIBPNG_BUILD_STABLE only */
+#define PNG_LIBPNG_BUILD_PRIVATE 16 /* Cannot be OR'ed with
+                                       PNG_LIBPNG_BUILD_SPECIAL */
+#define PNG_LIBPNG_BUILD_SPECIAL 32 /* Cannot be OR'ed with
+                                       PNG_LIBPNG_BUILD_PRIVATE */
+
+#define PNG_LIBPNG_BUILD_BASE_TYPE PNG_LIBPNG_BUILD_BETA
+
+/* Careful here.  At one time, Guy wanted to use 082, but that would be octal.
+ * We must not include leading zeros.
+ * Versions 0.7 through 1.0.0 were in the range 0 to 100 here (only
+ * version 1.0.0 was mis-numbered 100 instead of 10000).  From
+ * version 1.0.1 it's    xxyyzz, where x=major, y=minor, z=release
+ */
+#define PNG_LIBPNG_VER 10403 /* 1.4.3 */
+
+#ifndef PNG_VERSION_INFO_ONLY
+/* Include the compression library's header */
+#include "zlib.h"
+#endif
+
+/* Include all user configurable info, including optional assembler routines */
+#include "pngconf.h"
+
+/*
+ * Added at libpng-1.2.8
+ *
+ * Ref MSDN: Private as priority over Special
+ * VS_FF_PRIVATEBUILD File *was not* built using standard release
+ * procedures. If this value is given, the StringFileInfo block must
+ * contain a PrivateBuild string.
+ *
+ * VS_FF_SPECIALBUILD File *was* built by the original company using
+ * standard release procedures but is a variation of the standard
+ * file of the same version number. If this value is given, the
+ * StringFileInfo block must contain a SpecialBuild string.
+ */
+
+#ifdef PNG_USER_PRIVATEBUILD
+#  define PNG_LIBPNG_BUILD_TYPE \
+          (PNG_LIBPNG_BUILD_BASE_TYPE | PNG_LIBPNG_BUILD_PRIVATE)
+#else
+#  ifdef PNG_LIBPNG_SPECIALBUILD
+#    define PNG_LIBPNG_BUILD_TYPE \
+            (PNG_LIBPNG_BUILD_BASE_TYPE | PNG_LIBPNG_BUILD_SPECIAL)
+#  else
+#    define PNG_LIBPNG_BUILD_TYPE (PNG_LIBPNG_BUILD_BASE_TYPE)
+#  endif
+#endif
+
+#ifndef PNG_VERSION_INFO_ONLY
+
+/* Inhibit C++ name-mangling for libpng functions but not for system calls. */
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
+
+/* This file is arranged in several sections.  The first section contains
+ * structure and type definitions.  The second section contains the external
+ * library functions, while the third has the internal library functions,
+ * which applications aren't expected to use directly.
+ */
+
+/* Variables declared in png.c - only it needs to define PNG_NO_EXTERN */
+#if !defined(PNG_NO_EXTERN) || defined(PNG_ALWAYS_EXTERN)
+/* Version information for C files, stored in png.c.  This had better match
+ * the version above.
+ */
+#define png_libpng_ver png_get_header_ver(NULL)
+
+#endif /* PNG_NO_EXTERN */
+
+/* Three color definitions.  The order of the red, green, and blue, (and the
+ * exact size) is not important, although the size of the fields need to
+ * be png_byte or png_uint_16 (as defined below).
+ */
+typedef struct png_color_struct
+{
+   png_byte red;
+   png_byte green;
+   png_byte blue;
+} png_color;
+typedef png_color FAR * png_colorp;
+typedef png_color FAR * FAR * png_colorpp;
+
+typedef struct png_color_16_struct
+{
+   png_byte index;    /* used for palette files */
+   png_uint_16 red;   /* for use in red green blue files */
+   png_uint_16 green;
+   png_uint_16 blue;
+   png_uint_16 gray;  /* for use in grayscale files */
+} png_color_16;
+typedef png_color_16 FAR * png_color_16p;
+typedef png_color_16 FAR * FAR * png_color_16pp;
+
+typedef struct png_color_8_struct
+{
+   png_byte red;   /* for use in red green blue files */
+   png_byte green;
+   png_byte blue;
+   png_byte gray;  /* for use in grayscale files */
+   png_byte alpha; /* for alpha channel files */
+} png_color_8;
+typedef png_color_8 FAR * png_color_8p;
+typedef png_color_8 FAR * FAR * png_color_8pp;
+
+/*
+ * The following two structures are used for the in-core representation
+ * of sPLT chunks.
+ */
+typedef struct png_sPLT_entry_struct
+{
+   png_uint_16 red;
+   png_uint_16 green;
+   png_uint_16 blue;
+   png_uint_16 alpha;
+   png_uint_16 frequency;
+} png_sPLT_entry;
+typedef png_sPLT_entry FAR * png_sPLT_entryp;
+typedef png_sPLT_entry FAR * FAR * png_sPLT_entrypp;
+
+/*  When the depth of the sPLT palette is 8 bits, the color and alpha samples
+ *  occupy the LSB of their respective members, and the MSB of each member
+ *  is zero-filled.  The frequency member always occupies the full 16 bits.
+ */
+
+typedef struct png_sPLT_struct
+{
+   png_charp name;           /* palette name */
+   png_byte depth;           /* depth of palette samples */
+   png_sPLT_entryp entries;  /* palette entries */
+   png_int_32 nentries;      /* number of palette entries */
+} png_sPLT_t;
+typedef png_sPLT_t FAR * png_sPLT_tp;
+typedef png_sPLT_t FAR * FAR * png_sPLT_tpp;
+
+#ifdef PNG_TEXT_SUPPORTED
+/* png_text holds the contents of a text/ztxt/itxt chunk in a PNG file,
+ * and whether that contents is compressed or not.  The "key" field
+ * points to a regular zero-terminated C string.  The "text", "lang", and
+ * "lang_key" fields can be regular C strings, empty strings, or NULL pointers.
+ * However, the * structure returned by png_get_text() will always contain
+ * regular zero-terminated C strings (possibly empty), never NULL pointers,
+ * so they can be safely used in printf() and other string-handling functions.
+ */
+typedef struct png_text_struct
+{
+   int  compression;       /* compression value:
+                             -1: tEXt, none
+                              0: zTXt, deflate
+                              1: iTXt, none
+                              2: iTXt, deflate  */
+   png_charp key;          /* keyword, 1-79 character description of "text" */
+   png_charp text;         /* comment, may be an empty string (ie "")
+                              or a NULL pointer */
+   png_size_t text_length; /* length of the text string */
+#ifdef PNG_iTXt_SUPPORTED
+   png_size_t itxt_length; /* length of the itxt string */
+   png_charp lang;         /* language code, 0-79 characters
+                              or a NULL pointer */
+   png_charp lang_key;     /* keyword translated UTF-8 string, 0 or more
+                              chars or a NULL pointer */
+#endif
+} png_text;
+typedef png_text FAR * png_textp;
+typedef png_text FAR * FAR * png_textpp;
+#endif
+
+/* Supported compression types for text in PNG files (tEXt, and zTXt).
+ * The values of the PNG_TEXT_COMPRESSION_ defines should NOT be changed. */
+#define PNG_TEXT_COMPRESSION_NONE_WR -3
+#define PNG_TEXT_COMPRESSION_zTXt_WR -2
+#define PNG_TEXT_COMPRESSION_NONE    -1
+#define PNG_TEXT_COMPRESSION_zTXt     0
+#define PNG_ITXT_COMPRESSION_NONE     1
+#define PNG_ITXT_COMPRESSION_zTXt     2
+#define PNG_TEXT_COMPRESSION_LAST     3  /* Not a valid value */
+
+/* png_time is a way to hold the time in an machine independent way.
+ * Two conversions are provided, both from time_t and struct tm.  There
+ * is no portable way to convert to either of these structures, as far
+ * as I know.  If you know of a portable way, send it to me.  As a side
+ * note - PNG has always been Year 2000 compliant!
+ */
+typedef struct png_time_struct
+{
+   png_uint_16 year; /* full year, as in, 1995 */
+   png_byte month;   /* month of year, 1 - 12 */
+   png_byte day;     /* day of month, 1 - 31 */
+   png_byte hour;    /* hour of day, 0 - 23 */
+   png_byte minute;  /* minute of hour, 0 - 59 */
+   png_byte second;  /* second of minute, 0 - 60 (for leap seconds) */
+} png_time;
+typedef png_time FAR * png_timep;
+typedef png_time FAR * FAR * png_timepp;
+
+#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED) || \
+ defined(PNG_HANDLE_AS_UNKNOWN_SUPPORTED)
+/* png_unknown_chunk is a structure to hold queued chunks for which there is
+ * no specific support.  The idea is that we can use this to queue
+ * up private chunks for output even though the library doesn't actually
+ * know about their semantics.
+ */
+typedef struct png_unknown_chunk_t
+{
+    png_byte name[5];
+    png_byte *data;
+    png_size_t size;
+
+    /* libpng-using applications should NOT directly modify this byte. */
+    png_byte location; /* mode of operation at read time */
+}
+png_unknown_chunk;
+typedef png_unknown_chunk FAR * png_unknown_chunkp;
+typedef png_unknown_chunk FAR * FAR * png_unknown_chunkpp;
+#endif
+
+/* png_info is a structure that holds the information in a PNG file so
+ * that the application can find out the characteristics of the image.
+ * If you are reading the file, this structure will tell you what is
+ * in the PNG file.  If you are writing the file, fill in the information
+ * you want to put into the PNG file, then call png_write_info().
+ * The names chosen should be very close to the PNG specification, so
+ * consult that document for information about the meaning of each field.
+ *
+ * With libpng < 0.95, it was only possible to directly set and read the
+ * the values in the png_info_struct, which meant that the contents and
+ * order of the values had to remain fixed.  With libpng 0.95 and later,
+ * however, there are now functions that abstract the contents of
+ * png_info_struct from the application, so this makes it easier to use
+ * libpng with dynamic libraries, and even makes it possible to use
+ * libraries that don't have all of the libpng ancillary chunk-handing
+ * functionality.
+ *
+ * In any case, the order of the parameters in png_info_struct should NOT
+ * be changed for as long as possible to keep compatibility with applications
+ * that use the old direct-access method with png_info_struct.
+ *
+ * The following members may have allocated storage attached that should be
+ * cleaned up before the structure is discarded: palette, trans, text,
+ * pcal_purpose, pcal_units, pcal_params, hist, iccp_name, iccp_profile,
+ * splt_palettes, scal_unit, row_pointers, and unknowns.   By default, these
+ * are automatically freed when the info structure is deallocated, if they were
+ * allocated internally by libpng.  This behavior can be changed by means
+ * of the png_data_freer() function.
+ *
+ * More allocation details: all the chunk-reading functions that
+ * change these members go through the corresponding png_set_*
+ * functions.  A function to clear these members is available: see
+ * png_free_data().  The png_set_* functions do not depend on being
+ * able to point info structure members to any of the storage they are
+ * passed (they make their own copies), EXCEPT that the png_set_text
+ * functions use the same storage passed to them in the text_ptr or
+ * itxt_ptr structure argument, and the png_set_rows and png_set_unknowns
+ * functions do not make their own copies.
+ */
+typedef struct png_info_struct
+{
+   /* the following are necessary for every PNG file */
+   png_uint_32 width PNG_DEPSTRUCT;  /* width of image in pixels (from IHDR) */
+   png_uint_32 height PNG_DEPSTRUCT; /* height of image in pixels (from IHDR) */
+   png_uint_32 valid PNG_DEPSTRUCT;  /* valid chunk data (see PNG_INFO_
+                                        below) */
+   png_size_t rowbytes PNG_DEPSTRUCT; /* bytes needed to hold an untransformed
+                                         row */
+   png_colorp palette PNG_DEPSTRUCT;      /* array of color values
+                                             (valid & PNG_INFO_PLTE) */
+   png_uint_16 num_palette PNG_DEPSTRUCT; /* number of color entries in
+                                             "palette" (PLTE) */
+   png_uint_16 num_trans PNG_DEPSTRUCT;   /* number of transparent palette
+                                             color (tRNS) */
+   png_byte bit_depth PNG_DEPSTRUCT;      /* 1, 2, 4, 8, or 16 bits/channel
+                                             (from IHDR) */
+   png_byte color_type PNG_DEPSTRUCT;     /* see PNG_COLOR_TYPE_ below
+                                             (from IHDR) */
+   /* The following three should have been named *_method not *_type */
+   png_byte compression_type PNG_DEPSTRUCT; /* must be
+                                             PNG_COMPRESSION_TYPE_BASE (IHDR) */
+   png_byte filter_type PNG_DEPSTRUCT;    /* must be PNG_FILTER_TYPE_BASE
+                                             (from IHDR) */
+   png_byte interlace_type PNG_DEPSTRUCT; /* One of PNG_INTERLACE_NONE,
+                                             PNG_INTERLACE_ADAM7 */
+
+   /* The following is informational only on read, and not used on writes. */
+   png_byte channels PNG_DEPSTRUCT;       /* number of data channels per
+                                             pixel (1, 2, 3, 4) */
+   png_byte pixel_depth PNG_DEPSTRUCT;    /* number of bits per pixel */
+   png_byte spare_byte PNG_DEPSTRUCT;     /* to align the data, and for
+                                             future use */
+   png_byte signature[8] PNG_DEPSTRUCT;   /* magic bytes read by libpng
+                                             from start of file */
+
+   /* The rest of the data is optional.  If you are reading, check the
+    * valid field to see if the information in these are valid.  If you
+    * are writing, set the valid field to those chunks you want written,
+    * and initialize the appropriate fields below.
+    */
+
+#if defined(PNG_gAMA_SUPPORTED) && defined(PNG_FLOATING_POINT_SUPPORTED)
+   /* The gAMA chunk describes the gamma characteristics of the system
+    * on which the image was created, normally in the range [1.0, 2.5].
+    * Data is valid if (valid & PNG_INFO_gAMA) is non-zero.
+    */
+   float gamma PNG_DEPSTRUCT; /* gamma value of image,
+                                 if (valid & PNG_INFO_gAMA) */
+#endif
+
+#ifdef PNG_sRGB_SUPPORTED
+    /* GR-P, 0.96a */
+    /* Data valid if (valid & PNG_INFO_sRGB) non-zero. */
+   png_byte srgb_intent PNG_DEPSTRUCT; /* sRGB rendering intent
+                                          [0, 1, 2, or 3] */
+#endif
+
+#ifdef PNG_TEXT_SUPPORTED
+   /* The tEXt, and zTXt chunks contain human-readable textual data in
+    * uncompressed, compressed, and optionally compressed forms, respectively.
+    * The data in "text" is an array of pointers to uncompressed,
+    * null-terminated C strings. Each chunk has a keyword that describes the
+    * textual data contained in that chunk.  Keywords are not required to be
+    * unique, and the text string may be empty.  Any number of text chunks may
+    * be in an image.
+    */
+   int num_text PNG_DEPSTRUCT; /* number of comments read/to write */
+   int max_text PNG_DEPSTRUCT; /* current size of text array */
+   png_textp text PNG_DEPSTRUCT; /* array of comments read/to write */
+#endif /* PNG_TEXT_SUPPORTED */
+
+#ifdef PNG_tIME_SUPPORTED
+   /* The tIME chunk holds the last time the displayed image data was
+    * modified.  See the png_time struct for the contents of this struct.
+    */
+   png_time mod_time PNG_DEPSTRUCT;
+#endif
+
+#ifdef PNG_sBIT_SUPPORTED
+   /* The sBIT chunk specifies the number of significant high-order bits
+    * in the pixel data.  Values are in the range [1, bit_depth], and are
+    * only specified for the channels in the pixel data.  The contents of
+    * the low-order bits is not specified.  Data is valid if
+    * (valid & PNG_INFO_sBIT) is non-zero.
+    */
+   png_color_8 sig_bit PNG_DEPSTRUCT; /* significant bits in color channels */
+#endif
+
+#if defined(PNG_tRNS_SUPPORTED) || defined(PNG_READ_EXPAND_SUPPORTED) || \
+defined(PNG_READ_BACKGROUND_SUPPORTED)
+   /* The tRNS chunk supplies transparency data for paletted images and
+    * other image types that don't need a full alpha channel.  There are
+    * "num_trans" transparency values for a paletted image, stored in the
+    * same order as the palette colors, starting from index 0.  Values
+    * for the data are in the range [0, 255], ranging from fully transparent
+    * to fully opaque, respectively.  For non-paletted images, there is a
+    * single color specified that should be treated as fully transparent.
+    * Data is valid if (valid & PNG_INFO_tRNS) is non-zero.
+    */
+   png_bytep trans_alpha PNG_DEPSTRUCT;    /* alpha values for paletted
+                                              image */
+   png_color_16 trans_color PNG_DEPSTRUCT; /* transparent color for
+                                              non-palette image */
+#endif
+
+#if defined(PNG_bKGD_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
+   /* The bKGD chunk gives the suggested image background color if the
+    * display program does not have its own background color and the image
+    * is needs to composited onto a background before display.  The colors
+    * in "background" are normally in the same color space/depth as the
+    * pixel data.  Data is valid if (valid & PNG_INFO_bKGD) is non-zero.
+    */
+   png_color_16 background PNG_DEPSTRUCT;
+#endif
+
+#ifdef PNG_oFFs_SUPPORTED
+   /* The oFFs chunk gives the offset in "offset_unit_type" units rightwards
+    * and downwards from the top-left corner of the display, page, or other
+    * application-specific co-ordinate space.  See the PNG_OFFSET_ defines
+    * below for the unit types.  Valid if (valid & PNG_INFO_oFFs) non-zero.
+    */
+   png_int_32 x_offset PNG_DEPSTRUCT; /* x offset on page */
+   png_int_32 y_offset PNG_DEPSTRUCT; /* y offset on page */
+   png_byte offset_unit_type PNG_DEPSTRUCT; /* offset units type */
+#endif
+
+#ifdef PNG_pHYs_SUPPORTED
+   /* The pHYs chunk gives the physical pixel density of the image for
+    * display or printing in "phys_unit_type" units (see PNG_RESOLUTION_
+    * defines below).  Data is valid if (valid & PNG_INFO_pHYs) is non-zero.
+    */
+   png_uint_32 x_pixels_per_unit PNG_DEPSTRUCT; /* horizontal pixel density */
+   png_uint_32 y_pixels_per_unit PNG_DEPSTRUCT; /* vertical pixel density */
+   png_byte phys_unit_type PNG_DEPSTRUCT; /* resolution type (see
+                                             PNG_RESOLUTION_ below) */
+#endif
+
+#ifdef PNG_hIST_SUPPORTED
+   /* The hIST chunk contains the relative frequency or importance of the
+    * various palette entries, so that a viewer can intelligently select a
+    * reduced-color palette, if required.  Data is an array of "num_palette"
+    * values in the range [0,65535]. Data valid if (valid & PNG_INFO_hIST)
+    * is non-zero.
+    */
+   png_uint_16p hist PNG_DEPSTRUCT;
+#endif
+
+#ifdef PNG_cHRM_SUPPORTED
+   /* The cHRM chunk describes the CIE color characteristics of the monitor
+    * on which the PNG was created.  This data allows the viewer to do gamut
+    * mapping of the input image to ensure that the viewer sees the same
+    * colors in the image as the creator.  Values are in the range
+    * [0.0, 0.8].  Data valid if (valid & PNG_INFO_cHRM) non-zero.
+    */
+#ifdef PNG_FLOATING_POINT_SUPPORTED
+   float x_white PNG_DEPSTRUCT;
+   float y_white PNG_DEPSTRUCT;
+   float x_red PNG_DEPSTRUCT;
+   float y_red PNG_DEPSTRUCT;
+   float x_green PNG_DEPSTRUCT;
+   float y_green PNG_DEPSTRUCT;
+   float x_blue PNG_DEPSTRUCT;
+   float y_blue PNG_DEPSTRUCT;
+#endif
+#endif
+
+#ifdef PNG_pCAL_SUPPORTED
+   /* The pCAL chunk describes a transformation between the stored pixel
+    * values and original physical data values used to create the image.
+    * The integer range [0, 2^bit_depth - 1] maps to the floating-point
+    * range given by [pcal_X0, pcal_X1], and are further transformed by a
+    * (possibly non-linear) transformation function given by "pcal_type"
+    * and "pcal_params" into "pcal_units".  Please see the PNG_EQUATION_
+    * defines below, and the PNG-Group's PNG extensions document for a
+    * complete description of the transformations and how they should be
+    * implemented, and for a description of the ASCII parameter strings.
+    * Data values are valid if (valid & PNG_INFO_pCAL) non-zero.
+    */
+   png_charp pcal_purpose PNG_DEPSTRUCT;  /* pCAL chunk description string */
+   png_int_32 pcal_X0 PNG_DEPSTRUCT;      /* minimum value */
+   png_int_32 pcal_X1 PNG_DEPSTRUCT;      /* maximum value */
+   png_charp pcal_units PNG_DEPSTRUCT;    /* Latin-1 string giving physical
+                                             units */
+   png_charpp pcal_params PNG_DEPSTRUCT;  /* ASCII strings containing
+                                             parameter values */
+   png_byte pcal_type PNG_DEPSTRUCT;      /* equation type
+                                             (see PNG_EQUATION_ below) */
+   png_byte pcal_nparams PNG_DEPSTRUCT;   /* number of parameters given
+                                             in pcal_params */
+#endif
+
+/* New members added in libpng-1.0.6 */
+   png_uint_32 free_me PNG_DEPSTRUCT;     /* flags items libpng is
+                                             responsible for freeing */
+
+#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED) || \
+ defined(PNG_HANDLE_AS_UNKNOWN_SUPPORTED)
+   /* Storage for unknown chunks that the library doesn't recognize. */
+   png_unknown_chunkp unknown_chunks PNG_DEPSTRUCT;
+   png_size_t unknown_chunks_num PNG_DEPSTRUCT;
+#endif
+
+#ifdef PNG_iCCP_SUPPORTED
+   /* iCCP chunk data. */
+   png_charp iccp_name PNG_DEPSTRUCT;     /* profile name */
+   png_charp iccp_profile PNG_DEPSTRUCT;  /* International Color Consortium
+                                             profile data */
+                            /* Note to maintainer: should be png_bytep */
+   png_uint_32 iccp_proflen PNG_DEPSTRUCT;  /* ICC profile data length */
+   png_byte iccp_compression PNG_DEPSTRUCT; /* Always zero */
+#endif
+
+#ifdef PNG_sPLT_SUPPORTED
+   /* Data on sPLT chunks (there may be more than one). */
+   png_sPLT_tp splt_palettes PNG_DEPSTRUCT;
+   png_uint_32 splt_palettes_num PNG_DEPSTRUCT;
+#endif
+
+#ifdef PNG_sCAL_SUPPORTED
+   /* The sCAL chunk describes the actual physical dimensions of the
+    * subject matter of the graphic.  The chunk contains a unit specification
+    * a byte value, and two ASCII strings representing floating-point
+    * values.  The values are width and height corresponsing to one pixel
+    * in the image.  This external representation is converted to double
+    * here.  Data values are valid if (valid & PNG_INFO_sCAL) is non-zero.
+    */
+   png_byte scal_unit PNG_DEPSTRUCT;         /* unit of physical scale */
+#ifdef PNG_FLOATING_POINT_SUPPORTED
+   double scal_pixel_width PNG_DEPSTRUCT;    /* width of one pixel */
+   double scal_pixel_height PNG_DEPSTRUCT;   /* height of one pixel */
+#endif
+#ifdef PNG_FIXED_POINT_SUPPORTED
+   png_charp scal_s_width PNG_DEPSTRUCT;     /* string containing height */
+   png_charp scal_s_height PNG_DEPSTRUCT;    /* string containing width */
+#endif
+#endif
+
+#ifdef PNG_INFO_IMAGE_SUPPORTED
+   /* Memory has been allocated if (valid & PNG_ALLOCATED_INFO_ROWS)
+      non-zero */
+   /* Data valid if (valid & PNG_INFO_IDAT) non-zero */
+   png_bytepp row_pointers PNG_DEPSTRUCT;        /* the image bits */
+#endif
+
+#if defined(PNG_FIXED_POINT_SUPPORTED) && defined(PNG_gAMA_SUPPORTED)
+   png_fixed_point int_gamma PNG_DEPSTRUCT; /* gamma of image,
+                                               if (valid & PNG_INFO_gAMA) */
+#endif
+
+#if defined(PNG_cHRM_SUPPORTED) && defined(PNG_FIXED_POINT_SUPPORTED)
+   png_fixed_point int_x_white PNG_DEPSTRUCT;
+   png_fixed_point int_y_white PNG_DEPSTRUCT;
+   png_fixed_point int_x_red PNG_DEPSTRUCT;
+   png_fixed_point int_y_red PNG_DEPSTRUCT;
+   png_fixed_point int_x_green PNG_DEPSTRUCT;
+   png_fixed_point int_y_green PNG_DEPSTRUCT;
+   png_fixed_point int_x_blue PNG_DEPSTRUCT;
+   png_fixed_point int_y_blue PNG_DEPSTRUCT;
+#endif
+
+} png_info;
+
+typedef png_info FAR * png_infop;
+typedef png_info FAR * FAR * png_infopp;
+
+/* Maximum positive integer used in PNG is (2^31)-1 */
+#define PNG_UINT_31_MAX ((png_uint_32)0x7fffffffL)
+#define PNG_UINT_32_MAX ((png_uint_32)(-1))
+#define PNG_SIZE_MAX ((png_size_t)(-1))
+
+/* These describe the color_type field in png_info. */
+/* color type masks */
+#define PNG_COLOR_MASK_PALETTE    1
+#define PNG_COLOR_MASK_COLOR      2
+#define PNG_COLOR_MASK_ALPHA      4
+
+/* color types.  Note that not all combinations are legal */
+#define PNG_COLOR_TYPE_GRAY 0
+#define PNG_COLOR_TYPE_PALETTE  (PNG_COLOR_MASK_COLOR | PNG_COLOR_MASK_PALETTE)
+#define PNG_COLOR_TYPE_RGB        (PNG_COLOR_MASK_COLOR)
+#define PNG_COLOR_TYPE_RGB_ALPHA  (PNG_COLOR_MASK_COLOR | PNG_COLOR_MASK_ALPHA)
+#define PNG_COLOR_TYPE_GRAY_ALPHA (PNG_COLOR_MASK_ALPHA)
+/* aliases */
+#define PNG_COLOR_TYPE_RGBA  PNG_COLOR_TYPE_RGB_ALPHA
+#define PNG_COLOR_TYPE_GA  PNG_COLOR_TYPE_GRAY_ALPHA
+
+/* This is for compression type. PNG 1.0-1.2 only define the single type. */
+#define PNG_COMPRESSION_TYPE_BASE 0 /* Deflate method 8, 32K window */
+#define PNG_COMPRESSION_TYPE_DEFAULT PNG_COMPRESSION_TYPE_BASE
+
+/* This is for filter type. PNG 1.0-1.2 only define the single type. */
+#define PNG_FILTER_TYPE_BASE      0 /* Single row per-byte filtering */
+#define PNG_INTRAPIXEL_DIFFERENCING 64 /* Used only in MNG datastreams */
+#define PNG_FILTER_TYPE_DEFAULT   PNG_FILTER_TYPE_BASE
+
+/* These are for the interlacing type.  These values should NOT be changed. */
+#define PNG_INTERLACE_NONE        0 /* Non-interlaced image */
+#define PNG_INTERLACE_ADAM7       1 /* Adam7 interlacing */
+#define PNG_INTERLACE_LAST        2 /* Not a valid value */
+
+/* These are for the oFFs chunk.  These values should NOT be changed. */
+#define PNG_OFFSET_PIXEL          0 /* Offset in pixels */
+#define PNG_OFFSET_MICROMETER     1 /* Offset in micrometers (1/10^6 meter) */
+#define PNG_OFFSET_LAST           2 /* Not a valid value */
+
+/* These are for the pCAL chunk.  These values should NOT be changed. */
+#define PNG_EQUATION_LINEAR       0 /* Linear transformation */
+#define PNG_EQUATION_BASE_E       1 /* Exponential base e transform */
+#define PNG_EQUATION_ARBITRARY    2 /* Arbitrary base exponential transform */
+#define PNG_EQUATION_HYPERBOLIC   3 /* Hyperbolic sine transformation */
+#define PNG_EQUATION_LAST         4 /* Not a valid value */
+
+/* These are for the sCAL chunk.  These values should NOT be changed. */
+#define PNG_SCALE_UNKNOWN         0 /* unknown unit (image scale) */
+#define PNG_SCALE_METER           1 /* meters per pixel */
+#define PNG_SCALE_RADIAN          2 /* radians per pixel */
+#define PNG_SCALE_LAST            3 /* Not a valid value */
+
+/* These are for the pHYs chunk.  These values should NOT be changed. */
+#define PNG_RESOLUTION_UNKNOWN    0 /* pixels/unknown unit (aspect ratio) */
+#define PNG_RESOLUTION_METER      1 /* pixels/meter */
+#define PNG_RESOLUTION_LAST       2 /* Not a valid value */
+
+/* These are for the sRGB chunk.  These values should NOT be changed. */
+#define PNG_sRGB_INTENT_PERCEPTUAL 0
+#define PNG_sRGB_INTENT_RELATIVE   1
+#define PNG_sRGB_INTENT_SATURATION 2
+#define PNG_sRGB_INTENT_ABSOLUTE   3
+#define PNG_sRGB_INTENT_LAST       4 /* Not a valid value */
+
+/* This is for text chunks */
+#define PNG_KEYWORD_MAX_LENGTH     79
+
+/* Maximum number of entries in PLTE/sPLT/tRNS arrays */
+#define PNG_MAX_PALETTE_LENGTH    256
+
+/* These determine if an ancillary chunk's data has been successfully read
+ * from the PNG header, or if the application has filled in the corresponding
+ * data in the info_struct to be written into the output file.  The values
+ * of the PNG_INFO_<chunk> defines should NOT be changed.
+ */
+#define PNG_INFO_gAMA 0x0001
+#define PNG_INFO_sBIT 0x0002
+#define PNG_INFO_cHRM 0x0004
+#define PNG_INFO_PLTE 0x0008
+#define PNG_INFO_tRNS 0x0010
+#define PNG_INFO_bKGD 0x0020
+#define PNG_INFO_hIST 0x0040
+#define PNG_INFO_pHYs 0x0080
+#define PNG_INFO_oFFs 0x0100
+#define PNG_INFO_tIME 0x0200
+#define PNG_INFO_pCAL 0x0400
+#define PNG_INFO_sRGB 0x0800   /* GR-P, 0.96a */
+#define PNG_INFO_iCCP 0x1000   /* ESR, 1.0.6 */
+#define PNG_INFO_sPLT 0x2000   /* ESR, 1.0.6 */
+#define PNG_INFO_sCAL 0x4000   /* ESR, 1.0.6 */
+#define PNG_INFO_IDAT 0x8000L  /* ESR, 1.0.6 */
+
+/* This is used for the transformation routines, as some of them
+ * change these values for the row.  It also should enable using
+ * the routines for other purposes.
+ */
+typedef struct png_row_info_struct
+{
+   png_uint_32 width; /* width of row */
+   png_size_t rowbytes; /* number of bytes in row */
+   png_byte color_type; /* color type of row */
+   png_byte bit_depth; /* bit depth of row */
+   png_byte channels; /* number of channels (1, 2, 3, or 4) */
+   png_byte pixel_depth; /* bits per pixel (depth * channels) */
+} png_row_info;
+
+typedef png_row_info FAR * png_row_infop;
+typedef png_row_info FAR * FAR * png_row_infopp;
+
+/* These are the function types for the I/O functions and for the functions
+ * that allow the user to override the default I/O functions with his or her
+ * own.  The png_error_ptr type should match that of user-supplied warning
+ * and error functions, while the png_rw_ptr type should match that of the
+ * user read/write data functions.
+ */
+typedef struct png_struct_def png_struct;
+typedef png_struct FAR * png_structp;
+
+typedef void (PNGAPI *png_error_ptr) PNGARG((png_structp, png_const_charp));
+typedef void (PNGAPI *png_rw_ptr) PNGARG((png_structp, png_bytep, png_size_t));
+typedef void (PNGAPI *png_flush_ptr) PNGARG((png_structp));
+typedef void (PNGAPI *png_read_status_ptr) PNGARG((png_structp, png_uint_32,
+   int));
+typedef void (PNGAPI *png_write_status_ptr) PNGARG((png_structp, png_uint_32,
+   int));
+
+#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
+typedef void (PNGAPI *png_progressive_info_ptr) PNGARG((png_structp,
+   png_infop));
+typedef void (PNGAPI *png_progressive_end_ptr) PNGARG((png_structp, png_infop));
+typedef void (PNGAPI *png_progressive_row_ptr) PNGARG((png_structp, png_bytep,
+   png_uint_32, int));
+#endif
+
+#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \
+    defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
+typedef void (PNGAPI *png_user_transform_ptr) PNGARG((png_structp,
+    png_row_infop, png_bytep));
+#endif
+
+#ifdef PNG_USER_CHUNKS_SUPPORTED
+typedef int (PNGAPI *png_user_chunk_ptr) PNGARG((png_structp,
+   png_unknown_chunkp));
+#endif
+#ifdef PNG_UNKNOWN_CHUNKS_SUPPORTED
+typedef void (PNGAPI *png_unknown_chunk_ptr) PNGARG((png_structp));
+#endif
+#ifdef PNG_SETJMP_SUPPORTED
+/* This must match the function definition in <setjmp.h>, and the
+ * application must include this before png.h to obtain the definition
+ * of jmp_buf.
+ */
+typedef void (PNGAPI *png_longjmp_ptr) PNGARG((jmp_buf, int));
+#endif
+
+/* Transform masks for the high-level interface */
+#define PNG_TRANSFORM_IDENTITY       0x0000    /* read and write */
+#define PNG_TRANSFORM_STRIP_16       0x0001    /* read only */
+#define PNG_TRANSFORM_STRIP_ALPHA    0x0002    /* read only */
+#define PNG_TRANSFORM_PACKING        0x0004    /* read and write */
+#define PNG_TRANSFORM_PACKSWAP       0x0008    /* read and write */
+#define PNG_TRANSFORM_EXPAND         0x0010    /* read only */
+#define PNG_TRANSFORM_INVERT_MONO    0x0020    /* read and write */
+#define PNG_TRANSFORM_SHIFT          0x0040    /* read and write */
+#define PNG_TRANSFORM_BGR            0x0080    /* read and write */
+#define PNG_TRANSFORM_SWAP_ALPHA     0x0100    /* read and write */
+#define PNG_TRANSFORM_SWAP_ENDIAN    0x0200    /* read and write */
+#define PNG_TRANSFORM_INVERT_ALPHA   0x0400    /* read and write */
+#define PNG_TRANSFORM_STRIP_FILLER   0x0800    /* write only */
+/* Added to libpng-1.2.34 */
+#define PNG_TRANSFORM_STRIP_FILLER_BEFORE PNG_TRANSFORM_STRIP_FILLER
+#define PNG_TRANSFORM_STRIP_FILLER_AFTER 0x1000 /* write only */
+/* Added to libpng-1.4.0 */
+#define PNG_TRANSFORM_GRAY_TO_RGB   0x2000      /* read only */
+
+/* Flags for MNG supported features */
+#define PNG_FLAG_MNG_EMPTY_PLTE     0x01
+#define PNG_FLAG_MNG_FILTER_64      0x04
+#define PNG_ALL_MNG_FEATURES        0x05
+
+typedef png_voidp (*png_malloc_ptr) PNGARG((png_structp, png_alloc_size_t));
+typedef void (*png_free_ptr) PNGARG((png_structp, png_voidp));
+
+/* The structure that holds the information to read and write PNG files.
+ * The only people who need to care about what is inside of this are the
+ * people who will be modifying the library for their own special needs.
+ * It should NOT be accessed directly by an application, except to store
+ * the jmp_buf.
+ */
+
+struct png_struct_def
+{
+#ifdef PNG_SETJMP_SUPPORTED
+   jmp_buf jmpbuf PNG_DEPSTRUCT;            /* used in png_error */
+   png_longjmp_ptr longjmp_fn PNG_DEPSTRUCT;/* setjmp non-local goto
+                                               function. */
+#endif
+   png_error_ptr error_fn PNG_DEPSTRUCT;    /* function for printing
+                                               errors and aborting */
+   png_error_ptr warning_fn PNG_DEPSTRUCT;  /* function for printing
+                                               warnings */
+   png_voidp error_ptr PNG_DEPSTRUCT;       /* user supplied struct for
+                                               error functions */
+   png_rw_ptr write_data_fn PNG_DEPSTRUCT;  /* function for writing
+                                               output data */
+   png_rw_ptr read_data_fn PNG_DEPSTRUCT;   /* function for reading
+                                               input data */
+   png_voidp io_ptr PNG_DEPSTRUCT;          /* ptr to application struct
+                                               for I/O functions */
+
+#ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
+   png_user_transform_ptr read_user_transform_fn PNG_DEPSTRUCT; /* user read
+                                                                 transform */
+#endif
+
+#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
+   png_user_transform_ptr write_user_transform_fn PNG_DEPSTRUCT; /* user write
+                                                                  transform */
+#endif
+
+/* These were added in libpng-1.0.2 */
+#ifdef PNG_USER_TRANSFORM_PTR_SUPPORTED
+#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \
+    defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
+   png_voidp user_transform_ptr PNG_DEPSTRUCT; /* user supplied struct
+                                                  for user transform */
+   png_byte user_transform_depth PNG_DEPSTRUCT;    /* bit depth of user
+                                                      transformed pixels */
+   png_byte user_transform_channels PNG_DEPSTRUCT; /* channels in user
+                                                      transformed pixels */
+#endif
+#endif
+
+   png_uint_32 mode PNG_DEPSTRUCT;          /* tells us where we are in
+                                               the PNG file */
+   png_uint_32 flags PNG_DEPSTRUCT;         /* flags indicating various
+                                               things to libpng */
+   png_uint_32 transformations PNG_DEPSTRUCT; /* which transformations
+                                                 to perform */
+
+   z_stream zstream PNG_DEPSTRUCT;          /* pointer to decompression
+                                               structure (below) */
+   png_bytep zbuf PNG_DEPSTRUCT;            /* buffer for zlib */
+   png_size_t zbuf_size PNG_DEPSTRUCT;      /* size of zbuf */
+   int zlib_level PNG_DEPSTRUCT;            /* holds zlib compression level */
+   int zlib_method PNG_DEPSTRUCT;           /* holds zlib compression method */
+   int zlib_window_bits PNG_DEPSTRUCT;      /* holds zlib compression window
+                                               bits */
+   int zlib_mem_level PNG_DEPSTRUCT;        /* holds zlib compression memory
+                                               level */
+   int zlib_strategy PNG_DEPSTRUCT;         /* holds zlib compression
+                                               strategy */
+
+   png_uint_32 width PNG_DEPSTRUCT;         /* width of image in pixels */
+   png_uint_32 height PNG_DEPSTRUCT;        /* height of image in pixels */
+   png_uint_32 num_rows PNG_DEPSTRUCT;      /* number of rows in current pass */
+   png_uint_32 usr_width PNG_DEPSTRUCT;     /* width of row at start of write */
+   png_size_t rowbytes PNG_DEPSTRUCT;       /* size of row in bytes */
+#if 0 /* Replaced with the following in libpng-1.4.1 */
+   png_size_t irowbytes PNG_DEPSTRUCT;
+#endif
+/* Added in libpng-1.4.1 */
+#ifdef PNG_USER_LIMITS_SUPPORTED
+   /* Total memory that a zTXt, sPLT, iTXt, iCCP, or unknown chunk
+    * can occupy when decompressed.  0 means unlimited.
+    * We will change the typedef from png_size_t to png_alloc_size_t
+    * in libpng-1.6.0
+    */
+   png_alloc_size_t user_chunk_malloc_max PNG_DEPSTRUCT;
+#endif
+   png_uint_32 iwidth PNG_DEPSTRUCT;        /* width of current interlaced
+                                               row in pixels */
+   png_uint_32 row_number PNG_DEPSTRUCT;    /* current row in interlace pass */
+   png_bytep prev_row PNG_DEPSTRUCT;        /* buffer to save previous
+                                               (unfiltered) row */
+   png_bytep row_buf PNG_DEPSTRUCT;         /* buffer to save current
+                                               (unfiltered) row */
+   png_bytep sub_row PNG_DEPSTRUCT;         /* buffer to save "sub" row
+                                               when filtering */
+   png_bytep up_row PNG_DEPSTRUCT;          /* buffer to save "up" row
+                                               when filtering */
+   png_bytep avg_row PNG_DEPSTRUCT;         /* buffer to save "avg" row
+                                               when filtering */
+   png_bytep paeth_row PNG_DEPSTRUCT;       /* buffer to save "Paeth" row
+                                               when filtering */
+   png_row_info row_info PNG_DEPSTRUCT;     /* used for transformation
+                                               routines */
+
+   png_uint_32 idat_size PNG_DEPSTRUCT;     /* current IDAT size for read */
+   png_uint_32 crc PNG_DEPSTRUCT;           /* current chunk CRC value */
+   png_colorp palette PNG_DEPSTRUCT;        /* palette from the input file */
+   png_uint_16 num_palette PNG_DEPSTRUCT;   /* number of color entries in
+                                               palette */
+   png_uint_16 num_trans PNG_DEPSTRUCT;     /* number of transparency values */
+   png_byte chunk_name[5] PNG_DEPSTRUCT;    /* null-terminated name of current
+                                               chunk */
+   png_byte compression PNG_DEPSTRUCT;      /* file compression type
+                                               (always 0) */
+   png_byte filter PNG_DEPSTRUCT;           /* file filter type (always 0) */
+   png_byte interlaced PNG_DEPSTRUCT;       /* PNG_INTERLACE_NONE,
+                                               PNG_INTERLACE_ADAM7 */
+   png_byte pass PNG_DEPSTRUCT;             /* current interlace pass (0 - 6) */
+   png_byte do_filter PNG_DEPSTRUCT;        /* row filter flags (see
+                                               PNG_FILTER_ below ) */
+   png_byte color_type PNG_DEPSTRUCT;       /* color type of file */
+   png_byte bit_depth PNG_DEPSTRUCT;        /* bit depth of file */
+   png_byte usr_bit_depth PNG_DEPSTRUCT;    /* bit depth of users row */
+   png_byte pixel_depth PNG_DEPSTRUCT;      /* number of bits per pixel */
+   png_byte channels PNG_DEPSTRUCT;         /* number of channels in file */
+   png_byte usr_channels PNG_DEPSTRUCT;     /* channels at start of write */
+   png_byte sig_bytes PNG_DEPSTRUCT;        /* magic bytes read/written from
+                                               start of file */
+
+#if defined(PNG_READ_FILLER_SUPPORTED) || defined(PNG_WRITE_FILLER_SUPPORTED)
+   png_uint_16 filler PNG_DEPSTRUCT;           /* filler bytes for pixel
+                                                  expansion */
+#endif
+
+#ifdef PNG_bKGD_SUPPORTED
+   png_byte background_gamma_type PNG_DEPSTRUCT;
+#  ifdef PNG_FLOATING_POINT_SUPPORTED
+   float background_gamma PNG_DEPSTRUCT;
+#  endif
+   png_color_16 background PNG_DEPSTRUCT;   /* background color in
+                                               screen gamma space */
+#ifdef PNG_READ_GAMMA_SUPPORTED
+   png_color_16 background_1 PNG_DEPSTRUCT; /* background normalized
+                                               to gamma 1.0 */
+#endif
+#endif /* PNG_bKGD_SUPPORTED */
+
+#ifdef PNG_WRITE_FLUSH_SUPPORTED
+   png_flush_ptr output_flush_fn PNG_DEPSTRUCT; /* Function for flushing
+                                               output */
+   png_uint_32 flush_dist PNG_DEPSTRUCT;    /* how many rows apart to flush,
+                                               0 - no flush */
+   png_uint_32 flush_rows PNG_DEPSTRUCT;    /* number of rows written since
+                                               last flush */
+#endif
+
+#if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
+   int gamma_shift PNG_DEPSTRUCT;      /* number of "insignificant" bits
+                                          16-bit gamma */
+#ifdef PNG_FLOATING_POINT_SUPPORTED
+   float gamma PNG_DEPSTRUCT;          /* file gamma value */
+   float screen_gamma PNG_DEPSTRUCT;   /* screen gamma value
+                                          (display_exponent) */
+#endif
+#endif
+
+#if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
+   png_bytep gamma_table PNG_DEPSTRUCT;     /* gamma table for 8-bit
+                                               depth files */
+   png_bytep gamma_from_1 PNG_DEPSTRUCT;    /* converts from 1.0 to screen */
+   png_bytep gamma_to_1 PNG_DEPSTRUCT;      /* converts from file to 1.0 */
+   png_uint_16pp gamma_16_table PNG_DEPSTRUCT; /* gamma table for 16-bit
+                                                  depth files */
+   png_uint_16pp gamma_16_from_1 PNG_DEPSTRUCT; /* converts from 1.0 to
+                                                   screen */
+   png_uint_16pp gamma_16_to_1 PNG_DEPSTRUCT; /* converts from file to 1.0 */
+#endif
+
+#if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_sBIT_SUPPORTED)
+   png_color_8 sig_bit PNG_DEPSTRUCT;       /* significant bits in each
+                                               available channel */
+#endif
+
+#if defined(PNG_READ_SHIFT_SUPPORTED) || defined(PNG_WRITE_SHIFT_SUPPORTED)
+   png_color_8 shift PNG_DEPSTRUCT;         /* shift for significant bit
+                                               tranformation */
+#endif
+
+#if defined(PNG_tRNS_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED) \
+ || defined(PNG_READ_EXPAND_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
+   png_bytep trans_alpha PNG_DEPSTRUCT;           /* alpha values for
+                                                     paletted files */
+   png_color_16 trans_color PNG_DEPSTRUCT;  /* transparent color for
+                                               non-paletted files */
+#endif
+
+   png_read_status_ptr read_row_fn PNG_DEPSTRUCT;   /* called after each
+                                                       row is decoded */
+   png_write_status_ptr write_row_fn PNG_DEPSTRUCT; /* called after each
+                                                       row is encoded */
+#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
+   png_progressive_info_ptr info_fn PNG_DEPSTRUCT; /* called after header
+                                                      data fully read */
+   png_progressive_row_ptr row_fn PNG_DEPSTRUCT;   /* called after each
+                                                      prog. row is decoded */
+   png_progressive_end_ptr end_fn PNG_DEPSTRUCT;   /* called after image
+                                                      is complete */
+   png_bytep save_buffer_ptr PNG_DEPSTRUCT;        /* current location in
+                                                      save_buffer */
+   png_bytep save_buffer PNG_DEPSTRUCT;            /* buffer for previously
+                                                      read data */
+   png_bytep current_buffer_ptr PNG_DEPSTRUCT;     /* current location in
+                                                      current_buffer */
+   png_bytep current_buffer PNG_DEPSTRUCT;         /* buffer for recently
+                                                      used data */
+   png_uint_32 push_length PNG_DEPSTRUCT;          /* size of current input
+                                                      chunk */
+   png_uint_32 skip_length PNG_DEPSTRUCT;          /* bytes to skip in
+                                                      input data */
+   png_size_t save_buffer_size PNG_DEPSTRUCT;      /* amount of data now
+                                                      in save_buffer */
+   png_size_t save_buffer_max PNG_DEPSTRUCT;       /* total size of
+                                                      save_buffer */
+   png_size_t buffer_size PNG_DEPSTRUCT;           /* total amount of
+                                                      available input data */
+   png_size_t current_buffer_size PNG_DEPSTRUCT;   /* amount of data now
+                                                      in current_buffer */
+   int process_mode PNG_DEPSTRUCT;                 /* what push library
+                                                      is currently doing */
+   int cur_palette PNG_DEPSTRUCT;                  /* current push library
+                                                      palette index */
+
+#  ifdef PNG_TEXT_SUPPORTED
+     png_size_t current_text_size PNG_DEPSTRUCT;   /* current size of
+                                                      text input data */
+     png_size_t current_text_left PNG_DEPSTRUCT;   /* how much text left
+                                                      to read in input */
+     png_charp current_text PNG_DEPSTRUCT;         /* current text chunk
+                                                      buffer */
+     png_charp current_text_ptr PNG_DEPSTRUCT;     /* current location
+                                                      in current_text */
+#  endif /* PNG_PROGRESSIVE_READ_SUPPORTED && PNG_TEXT_SUPPORTED */
+
+#endif /* PNG_PROGRESSIVE_READ_SUPPORTED */
+
+#if defined(__TURBOC__) && !defined(_Windows) && !defined(__FLAT__)
+/* For the Borland special 64K segment handler */
+   png_bytepp offset_table_ptr PNG_DEPSTRUCT;
+   png_bytep offset_table PNG_DEPSTRUCT;
+   png_uint_16 offset_table_number PNG_DEPSTRUCT;
+   png_uint_16 offset_table_count PNG_DEPSTRUCT;
+   png_uint_16 offset_table_count_free PNG_DEPSTRUCT;
+#endif
+
+#ifdef PNG_READ_QUANTIZE_SUPPORTED
+   png_bytep palette_lookup PNG_DEPSTRUCT; /* lookup table for quantizing */
+   png_bytep quantize_index PNG_DEPSTRUCT; /* index translation for palette
+                                              files */
+#endif
+
+#if defined(PNG_READ_QUANTIZE_SUPPORTED) || defined(PNG_hIST_SUPPORTED)
+   png_uint_16p hist PNG_DEPSTRUCT;                /* histogram */
+#endif
+
+#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
+   png_byte heuristic_method PNG_DEPSTRUCT;        /* heuristic for row
+                                                      filter selection */
+   png_byte num_prev_filters PNG_DEPSTRUCT;        /* number of weights
+                                                      for previous rows */
+   png_bytep prev_filters PNG_DEPSTRUCT;           /* filter type(s) of
+                                                      previous row(s) */
+   png_uint_16p filter_weights PNG_DEPSTRUCT;      /* weight(s) for previous
+                                                      line(s) */
+   png_uint_16p inv_filter_weights PNG_DEPSTRUCT;  /* 1/weight(s) for
+                                                      previous line(s) */
+   png_uint_16p filter_costs PNG_DEPSTRUCT;        /* relative filter
+                                                      calculation cost */
+   png_uint_16p inv_filter_costs PNG_DEPSTRUCT;    /* 1/relative filter
+                                                      calculation cost */
+#endif
+
+#ifdef PNG_TIME_RFC1123_SUPPORTED
+   png_charp time_buffer PNG_DEPSTRUCT; /* String to hold RFC 1123 time text */
+#endif
+
+/* New members added in libpng-1.0.6 */
+
+   png_uint_32 free_me PNG_DEPSTRUCT;    /* flags items libpng is
+                                            responsible for freeing */
+
+#ifdef PNG_USER_CHUNKS_SUPPORTED
+   png_voidp user_chunk_ptr PNG_DEPSTRUCT;
+   png_user_chunk_ptr read_user_chunk_fn PNG_DEPSTRUCT; /* user read
+                                                           chunk handler */
+#endif
+
+#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
+   int num_chunk_list PNG_DEPSTRUCT;
+   png_bytep chunk_list PNG_DEPSTRUCT;
+#endif
+
+/* New members added in libpng-1.0.3 */
+#ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
+   png_byte rgb_to_gray_status PNG_DEPSTRUCT;
+   /* These were changed from png_byte in libpng-1.0.6 */
+   png_uint_16 rgb_to_gray_red_coeff PNG_DEPSTRUCT;
+   png_uint_16 rgb_to_gray_green_coeff PNG_DEPSTRUCT;
+   png_uint_16 rgb_to_gray_blue_coeff PNG_DEPSTRUCT;
+#endif
+
+/* New member added in libpng-1.0.4 (renamed in 1.0.9) */
+#if defined(PNG_MNG_FEATURES_SUPPORTED) || \
+    defined(PNG_READ_EMPTY_PLTE_SUPPORTED) || \
+    defined(PNG_WRITE_EMPTY_PLTE_SUPPORTED)
+/* Changed from png_byte to png_uint_32 at version 1.2.0 */
+   png_uint_32 mng_features_permitted PNG_DEPSTRUCT;
+#endif
+
+/* New member added in libpng-1.0.7 */
+#if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
+   png_fixed_point int_gamma PNG_DEPSTRUCT;
+#endif
+
+/* New member added in libpng-1.0.9, ifdef'ed out in 1.0.12, enabled in 1.2.0 */
+#ifdef PNG_MNG_FEATURES_SUPPORTED
+   png_byte filter_type PNG_DEPSTRUCT;
+#endif
+
+/* New members added in libpng-1.2.0 */
+
+/* New members added in libpng-1.0.2 but first enabled by default in 1.2.0 */
+#ifdef PNG_USER_MEM_SUPPORTED
+   png_voidp mem_ptr PNG_DEPSTRUCT;             /* user supplied struct for
+                                                   mem functions */
+   png_malloc_ptr malloc_fn PNG_DEPSTRUCT;      /* function for
+                                                   allocating memory */
+   png_free_ptr free_fn PNG_DEPSTRUCT;          /* function for
+                                                   freeing memory */
+#endif
+
+/* New member added in libpng-1.0.13 and 1.2.0 */
+   png_bytep big_row_buf PNG_DEPSTRUCT;         /* buffer to save current
+                                                   (unfiltered) row */
+
+#ifdef PNG_READ_QUANTIZE_SUPPORTED
+/* The following three members were added at version 1.0.14 and 1.2.4 */
+   png_bytep quantize_sort PNG_DEPSTRUCT;          /* working sort array */
+   png_bytep index_to_palette PNG_DEPSTRUCT;       /* where the original
+                                                     index currently is
+                                                     in the palette */
+   png_bytep palette_to_index PNG_DEPSTRUCT;       /* which original index
+                                                      points to this
+                                                      palette color */
+#endif
+
+/* New members added in libpng-1.0.16 and 1.2.6 */
+   png_byte compression_type PNG_DEPSTRUCT;
+
+#ifdef PNG_USER_LIMITS_SUPPORTED
+   png_uint_32 user_width_max PNG_DEPSTRUCT;
+   png_uint_32 user_height_max PNG_DEPSTRUCT;
+   /* Added in libpng-1.4.0: Total number of sPLT, text, and unknown
+    * chunks that can be stored (0 means unlimited).
+    */
+   png_uint_32 user_chunk_cache_max PNG_DEPSTRUCT;
+#endif
+
+/* New member added in libpng-1.0.25 and 1.2.17 */
+#ifdef PNG_UNKNOWN_CHUNKS_SUPPORTED
+   /* Storage for unknown chunk that the library doesn't recognize. */
+   png_unknown_chunk unknown_chunk PNG_DEPSTRUCT;
+#endif
+
+/* New members added in libpng-1.2.26 */
+  png_uint_32 old_big_row_buf_size PNG_DEPSTRUCT;
+  png_uint_32 old_prev_row_size PNG_DEPSTRUCT;
+
+/* New member added in libpng-1.2.30 */
+  png_charp chunkdata PNG_DEPSTRUCT;  /* buffer for reading chunk data */
+
+#ifdef PNG_IO_STATE_SUPPORTED
+/* New member added in libpng-1.4.0 */
+   png_uint_32 io_state PNG_DEPSTRUCT;
+#endif
+};
+
+
+/* This triggers a compiler error in png.c, if png.c and png.h
+ * do not agree upon the version number.
+ */
+typedef png_structp version_1_4_3;
+
+typedef png_struct FAR * FAR * png_structpp;
+
+/* Here are the function definitions most commonly used.  This is not
+ * the place to find out how to use libpng.  See libpng.txt for the
+ * full explanation, see example.c for the summary.  This just provides
+ * a simple one line description of the use of each function.
+ */
+
+/* Returns the version number of the library */
+extern PNG_EXPORT(png_uint_32,png_access_version_number) PNGARG((void));
+
+/* Tell lib we have already handled the first <num_bytes> magic bytes.
+ * Handling more than 8 bytes from the beginning of the file is an error.
+ */
+extern PNG_EXPORT(void,png_set_sig_bytes) PNGARG((png_structp png_ptr,
+   int num_bytes));
+
+/* Check sig[start] through sig[start + num_to_check - 1] to see if it's a
+ * PNG file.  Returns zero if the supplied bytes match the 8-byte PNG
+ * signature, and non-zero otherwise.  Having num_to_check == 0 or
+ * start > 7 will always fail (ie return non-zero).
+ */
+extern PNG_EXPORT(int,png_sig_cmp) PNGARG((png_bytep sig, png_size_t start,
+   png_size_t num_to_check));
+
+/* Simple signature checking function.  This is the same as calling
+ * png_check_sig(sig, n) := !png_sig_cmp(sig, 0, n).
+ */
+#define png_check_sig(sig,n) !png_sig_cmp((sig), 0, (n))
+
+/* Allocate and initialize png_ptr struct for reading, and any other memory. */
+extern PNG_EXPORT(png_structp,png_create_read_struct)
+   PNGARG((png_const_charp user_png_ver, png_voidp error_ptr,
+   png_error_ptr error_fn, png_error_ptr warn_fn)) PNG_ALLOCATED;
+
+/* Allocate and initialize png_ptr struct for writing, and any other memory */
+extern PNG_EXPORT(png_structp,png_create_write_struct)
+   PNGARG((png_const_charp user_png_ver, png_voidp error_ptr,
+   png_error_ptr error_fn, png_error_ptr warn_fn)) PNG_ALLOCATED;
+
+extern PNG_EXPORT(png_size_t,png_get_compression_buffer_size)
+   PNGARG((png_structp png_ptr));
+
+extern PNG_EXPORT(void,png_set_compression_buffer_size)
+   PNGARG((png_structp png_ptr, png_size_t size));
+
+/* Moved from pngconf.h in 1.4.0 and modified to ensure setjmp/longjmp
+ * match up.
+ */
+#ifdef PNG_SETJMP_SUPPORTED
+/* This function returns the jmp_buf built in to *png_ptr.  It must be
+ * supplied with an appropriate 'longjmp' function to use on that jmp_buf
+ * unless the default error function is overridden in which case NULL is
+ * acceptable.  The size of the jmp_buf is checked against the actual size
+ * allocated by the library - the call will return NULL on a mismatch
+ * indicating an ABI mismatch.
+ */
+extern PNG_EXPORT(jmp_buf*, png_set_longjmp_fn)
+   PNGARG((png_structp png_ptr, png_longjmp_ptr longjmp_fn, size_t
+       jmp_buf_size));
+#  define png_jmpbuf(png_ptr) \
+   (*png_set_longjmp_fn((png_ptr), longjmp, sizeof (jmp_buf)))
+#else
+#  define png_jmpbuf(png_ptr) \
+   (LIBPNG_WAS_COMPILED_WITH__PNG_NO_SETJMP)
+#endif
+
+#ifdef PNG_READ_SUPPORTED
+/* Reset the compression stream */
+extern PNG_EXPORT(int,png_reset_zstream) PNGARG((png_structp png_ptr));
+#endif
+
+/* New functions added in libpng-1.0.2 (not enabled by default until 1.2.0) */
+#ifdef PNG_USER_MEM_SUPPORTED
+extern PNG_EXPORT(png_structp,png_create_read_struct_2)
+   PNGARG((png_const_charp user_png_ver, png_voidp error_ptr,
+   png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
+   png_malloc_ptr malloc_fn, png_free_ptr free_fn)) PNG_ALLOCATED;
+extern PNG_EXPORT(png_structp,png_create_write_struct_2)
+   PNGARG((png_const_charp user_png_ver, png_voidp error_ptr,
+   png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
+   png_malloc_ptr malloc_fn, png_free_ptr free_fn)) PNG_ALLOCATED;
+#endif
+
+/* Write the PNG file signature. */
+extern PNG_EXPORT(void,png_write_sig) PNGARG((png_structp png_ptr));
+
+/* Write a PNG chunk - size, type, (optional) data, CRC. */
+extern PNG_EXPORT(void,png_write_chunk) PNGARG((png_structp png_ptr,
+   png_bytep chunk_name, png_bytep data, png_size_t length));
+
+/* Write the start of a PNG chunk - length and chunk name. */
+extern PNG_EXPORT(void,png_write_chunk_start) PNGARG((png_structp png_ptr,
+   png_bytep chunk_name, png_uint_32 length));
+
+/* Write the data of a PNG chunk started with png_write_chunk_start(). */
+extern PNG_EXPORT(void,png_write_chunk_data) PNGARG((png_structp png_ptr,
+   png_bytep data, png_size_t length));
+
+/* Finish a chunk started with png_write_chunk_start() (includes CRC). */
+extern PNG_EXPORT(void,png_write_chunk_end) PNGARG((png_structp png_ptr));
+
+/* Allocate and initialize the info structure */
+extern PNG_EXPORT(png_infop,png_create_info_struct)
+   PNGARG((png_structp png_ptr)) PNG_ALLOCATED;
+
+extern PNG_EXPORT(void,png_info_init_3) PNGARG((png_infopp info_ptr,
+    png_size_t png_info_struct_size));
+
+/* Writes all the PNG information before the image. */
+extern PNG_EXPORT(void,png_write_info_before_PLTE) PNGARG((png_structp png_ptr,
+   png_infop info_ptr));
+extern PNG_EXPORT(void,png_write_info) PNGARG((png_structp png_ptr,
+   png_infop info_ptr));
+
+#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
+/* Read the information before the actual image data. */
+extern PNG_EXPORT(void,png_read_info) PNGARG((png_structp png_ptr,
+   png_infop info_ptr));
+#endif
+
+#ifdef PNG_TIME_RFC1123_SUPPORTED
+extern PNG_EXPORT(png_charp,png_convert_to_rfc1123)
+   PNGARG((png_structp png_ptr, png_timep ptime));
+#endif
+
+#ifdef PNG_CONVERT_tIME_SUPPORTED
+/* Convert from a struct tm to png_time */
+extern PNG_EXPORT(void,png_convert_from_struct_tm) PNGARG((png_timep ptime,
+   struct tm FAR * ttime));
+
+/* Convert from time_t to png_time.  Uses gmtime() */
+extern PNG_EXPORT(void,png_convert_from_time_t) PNGARG((png_timep ptime,
+   time_t ttime));
+#endif /* PNG_CONVERT_tIME_SUPPORTED */
+
+#ifdef PNG_READ_EXPAND_SUPPORTED
+/* Expand data to 24-bit RGB, or 8-bit grayscale, with alpha if available. */
+extern PNG_EXPORT(void,png_set_expand) PNGARG((png_structp png_ptr));
+extern PNG_EXPORT(void,png_set_expand_gray_1_2_4_to_8) PNGARG((png_structp
+  png_ptr));
+extern PNG_EXPORT(void,png_set_palette_to_rgb) PNGARG((png_structp png_ptr));
+extern PNG_EXPORT(void,png_set_tRNS_to_alpha) PNGARG((png_structp png_ptr));
+#endif
+
+#if defined(PNG_READ_BGR_SUPPORTED) || defined(PNG_WRITE_BGR_SUPPORTED)
+/* Use blue, green, red order for pixels. */
+extern PNG_EXPORT(void,png_set_bgr) PNGARG((png_structp png_ptr));
+#endif
+
+#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
+/* Expand the grayscale to 24-bit RGB if necessary. */
+extern PNG_EXPORT(void,png_set_gray_to_rgb) PNGARG((png_structp png_ptr));
+#endif
+
+#ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
+/* Reduce RGB to grayscale. */
+#ifdef PNG_FLOATING_POINT_SUPPORTED
+extern PNG_EXPORT(void,png_set_rgb_to_gray) PNGARG((png_structp png_ptr,
+   int error_action, double red, double green ));
+#endif
+extern PNG_EXPORT(void,png_set_rgb_to_gray_fixed) PNGARG((png_structp png_ptr,
+   int error_action, png_fixed_point red, png_fixed_point green ));
+extern PNG_EXPORT(png_byte,png_get_rgb_to_gray_status) PNGARG((png_structp
+   png_ptr));
+#endif
+
+extern PNG_EXPORT(void,png_build_grayscale_palette) PNGARG((int bit_depth,
+   png_colorp palette));
+
+#ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
+extern PNG_EXPORT(void,png_set_strip_alpha) PNGARG((png_structp png_ptr));
+#endif
+
+#if defined(PNG_READ_SWAP_ALPHA_SUPPORTED) || \
+    defined(PNG_WRITE_SWAP_ALPHA_SUPPORTED)
+extern PNG_EXPORT(void,png_set_swap_alpha) PNGARG((png_structp png_ptr));
+#endif
+
+#if defined(PNG_READ_INVERT_ALPHA_SUPPORTED) || \
+    defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED)
+extern PNG_EXPORT(void,png_set_invert_alpha) PNGARG((png_structp png_ptr));
+#endif
+
+#if defined(PNG_READ_FILLER_SUPPORTED) || defined(PNG_WRITE_FILLER_SUPPORTED)
+/* Add a filler byte to 8-bit Gray or 24-bit RGB images. */
+extern PNG_EXPORT(void,png_set_filler) PNGARG((png_structp png_ptr,
+   png_uint_32 filler, int flags));
+/* The values of the PNG_FILLER_ defines should NOT be changed */
+#define PNG_FILLER_BEFORE 0
+#define PNG_FILLER_AFTER 1
+/* Add an alpha byte to 8-bit Gray or 24-bit RGB images. */
+extern PNG_EXPORT(void,png_set_add_alpha) PNGARG((png_structp png_ptr,
+   png_uint_32 filler, int flags));
+#endif /* PNG_READ_FILLER_SUPPORTED || PNG_WRITE_FILLER_SUPPORTED */
+
+#if defined(PNG_READ_SWAP_SUPPORTED) || defined(PNG_WRITE_SWAP_SUPPORTED)
+/* Swap bytes in 16-bit depth files. */
+extern PNG_EXPORT(void,png_set_swap) PNGARG((png_structp png_ptr));
+#endif
+
+#if defined(PNG_READ_PACK_SUPPORTED) || defined(PNG_WRITE_PACK_SUPPORTED)
+/* Use 1 byte per pixel in 1, 2, or 4-bit depth files. */
+extern PNG_EXPORT(void,png_set_packing) PNGARG((png_structp png_ptr));
+#endif
+
+#if defined(PNG_READ_PACKSWAP_SUPPORTED) || \
+    defined(PNG_WRITE_PACKSWAP_SUPPORTED)
+/* Swap packing order of pixels in bytes. */
+extern PNG_EXPORT(void,png_set_packswap) PNGARG((png_structp png_ptr));
+#endif
+
+#if defined(PNG_READ_SHIFT_SUPPORTED) || defined(PNG_WRITE_SHIFT_SUPPORTED)
+/* Converts files to legal bit depths. */
+extern PNG_EXPORT(void,png_set_shift) PNGARG((png_structp png_ptr,
+   png_color_8p true_bits));
+#endif
+
+#if defined(PNG_READ_INTERLACING_SUPPORTED) || \
+    defined(PNG_WRITE_INTERLACING_SUPPORTED)
+/* Have the code handle the interlacing.  Returns the number of passes. */
+extern PNG_EXPORT(int,png_set_interlace_handling) PNGARG((png_structp png_ptr));
+#endif
+
+#if defined(PNG_READ_INVERT_SUPPORTED) || defined(PNG_WRITE_INVERT_SUPPORTED)
+/* Invert monochrome files */
+extern PNG_EXPORT(void,png_set_invert_mono) PNGARG((png_structp png_ptr));
+#endif
+
+#ifdef PNG_READ_BACKGROUND_SUPPORTED
+/* Handle alpha and tRNS by replacing with a background color. */
+#ifdef PNG_FLOATING_POINT_SUPPORTED
+extern PNG_EXPORT(void,png_set_background) PNGARG((png_structp png_ptr,
+   png_color_16p background_color, int background_gamma_code,
+   int need_expand, double background_gamma));
+#endif
+#define PNG_BACKGROUND_GAMMA_UNKNOWN 0
+#define PNG_BACKGROUND_GAMMA_SCREEN  1
+#define PNG_BACKGROUND_GAMMA_FILE    2
+#define PNG_BACKGROUND_GAMMA_UNIQUE  3
+#endif
+
+#ifdef PNG_READ_16_TO_8_SUPPORTED
+/* Strip the second byte of information from a 16-bit depth file. */
+extern PNG_EXPORT(void,png_set_strip_16) PNGARG((png_structp png_ptr));
+#endif
+
+#ifdef PNG_READ_QUANTIZE_SUPPORTED
+/* Turn on quantizing, and reduce the palette to the number of colors
+ * available.  Prior to libpng-1.4.2, this was png_set_dither().
+ */
+extern PNG_EXPORT(void,png_set_quantize) PNGARG((png_structp png_ptr,
+   png_colorp palette, int num_palette, int maximum_colors,
+   png_uint_16p histogram, int full_quantize));
+#endif
+/* This migration aid will be removed from libpng-1.5.0 */
+#define png_set_dither png_set_quantize
+
+#ifdef PNG_READ_GAMMA_SUPPORTED
+/* Handle gamma correction. Screen_gamma=(display_exponent) */
+#ifdef PNG_FLOATING_POINT_SUPPORTED
+extern PNG_EXPORT(void,png_set_gamma) PNGARG((png_structp png_ptr,
+   double screen_gamma, double default_file_gamma));
+#endif
+#endif
+
+
+#ifdef PNG_WRITE_FLUSH_SUPPORTED
+/* Set how many lines between output flushes - 0 for no flushing */
+extern PNG_EXPORT(void,png_set_flush) PNGARG((png_structp png_ptr, int nrows));
+/* Flush the current PNG output buffer */
+extern PNG_EXPORT(void,png_write_flush) PNGARG((png_structp png_ptr));
+#endif
+
+/* Optional update palette with requested transformations */
+extern PNG_EXPORT(void,png_start_read_image) PNGARG((png_structp png_ptr));
+
+/* Optional call to update the users info structure */
+extern PNG_EXPORT(void,png_read_update_info) PNGARG((png_structp png_ptr,
+   png_infop info_ptr));
+
+#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
+/* Read one or more rows of image data. */
+extern PNG_EXPORT(void,png_read_rows) PNGARG((png_structp png_ptr,
+   png_bytepp row, png_bytepp display_row, png_uint_32 num_rows));
+#endif
+
+#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
+/* Read a row of data. */
+extern PNG_EXPORT(void,png_read_row) PNGARG((png_structp png_ptr,
+   png_bytep row,
+   png_bytep display_row));
+#endif
+
+#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
+/* Read the whole image into memory at once. */
+extern PNG_EXPORT(void,png_read_image) PNGARG((png_structp png_ptr,
+   png_bytepp image));
+#endif
+
+/* Write a row of image data */
+extern PNG_EXPORT(void,png_write_row) PNGARG((png_structp png_ptr,
+   png_bytep row));
+
+/* Write a few rows of image data */
+extern PNG_EXPORT(void,png_write_rows) PNGARG((png_structp png_ptr,
+   png_bytepp row, png_uint_32 num_rows));
+
+/* Write the image data */
+extern PNG_EXPORT(void,png_write_image) PNGARG((png_structp png_ptr,
+   png_bytepp image));
+
+/* Write the end of the PNG file. */
+extern PNG_EXPORT(void,png_write_end) PNGARG((png_structp png_ptr,
+   png_infop info_ptr));
+
+#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
+/* Read the end of the PNG file. */
+extern PNG_EXPORT(void,png_read_end) PNGARG((png_structp png_ptr,
+   png_infop info_ptr));
+#endif
+
+/* Free any memory associated with the png_info_struct */
+extern PNG_EXPORT(void,png_destroy_info_struct) PNGARG((png_structp png_ptr,
+   png_infopp info_ptr_ptr));
+
+/* Free any memory associated with the png_struct and the png_info_structs */
+extern PNG_EXPORT(void,png_destroy_read_struct) PNGARG((png_structpp
+   png_ptr_ptr, png_infopp info_ptr_ptr, png_infopp end_info_ptr_ptr));
+
+/* Free any memory associated with the png_struct and the png_info_structs */
+extern PNG_EXPORT(void,png_destroy_write_struct)
+   PNGARG((png_structpp png_ptr_ptr, png_infopp info_ptr_ptr));
+
+/* Set the libpng method of handling chunk CRC errors */
+extern PNG_EXPORT(void,png_set_crc_action) PNGARG((png_structp png_ptr,
+   int crit_action, int ancil_action));
+
+/* Values for png_set_crc_action() to say how to handle CRC errors in
+ * ancillary and critical chunks, and whether to use the data contained
+ * therein.  Note that it is impossible to "discard" data in a critical
+ * chunk.  For versions prior to 0.90, the action was always error/quit,
+ * whereas in version 0.90 and later, the action for CRC errors in ancillary
+ * chunks is warn/discard.  These values should NOT be changed.
+ *
+ *      value                       action:critical     action:ancillary
+ */
+#define PNG_CRC_DEFAULT       0  /* error/quit          warn/discard data */
+#define PNG_CRC_ERROR_QUIT    1  /* error/quit          error/quit        */
+#define PNG_CRC_WARN_DISCARD  2  /* (INVALID)           warn/discard data */
+#define PNG_CRC_WARN_USE      3  /* warn/use data       warn/use data     */
+#define PNG_CRC_QUIET_USE     4  /* quiet/use data      quiet/use data    */
+#define PNG_CRC_NO_CHANGE     5  /* use current value   use current value */
+
+/* These functions give the user control over the scan-line filtering in
+ * libpng and the compression methods used by zlib.  These functions are
+ * mainly useful for testing, as the defaults should work with most users.
+ * Those users who are tight on memory or want faster performance at the
+ * expense of compression can modify them.  See the compression library
+ * header file (zlib.h) for an explination of the compression functions.
+ */
+
+/* Set the filtering method(s) used by libpng.  Currently, the only valid
+ * value for "method" is 0.
+ */
+extern PNG_EXPORT(void,png_set_filter) PNGARG((png_structp png_ptr, int method,
+   int filters));
+
+/* Flags for png_set_filter() to say which filters to use.  The flags
+ * are chosen so that they don't conflict with real filter types
+ * below, in case they are supplied instead of the #defined constants.
+ * These values should NOT be changed.
+ */
+#define PNG_NO_FILTERS     0x00
+#define PNG_FILTER_NONE    0x08
+#define PNG_FILTER_SUB     0x10
+#define PNG_FILTER_UP      0x20
+#define PNG_FILTER_AVG     0x40
+#define PNG_FILTER_PAETH   0x80
+#define PNG_ALL_FILTERS (PNG_FILTER_NONE | PNG_FILTER_SUB | PNG_FILTER_UP | \
+                         PNG_FILTER_AVG | PNG_FILTER_PAETH)
+
+/* Filter values (not flags) - used in pngwrite.c, pngwutil.c for now.
+ * These defines should NOT be changed.
+ */
+#define PNG_FILTER_VALUE_NONE  0
+#define PNG_FILTER_VALUE_SUB   1
+#define PNG_FILTER_VALUE_UP    2
+#define PNG_FILTER_VALUE_AVG   3
+#define PNG_FILTER_VALUE_PAETH 4
+#define PNG_FILTER_VALUE_LAST  5
+
+#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED /* EXPERIMENTAL */
+/* The "heuristic_method" is given by one of the PNG_FILTER_HEURISTIC_
+ * defines, either the default (minimum-sum-of-absolute-differences), or
+ * the experimental method (weighted-minimum-sum-of-absolute-differences).
+ *
+ * Weights are factors >= 1.0, indicating how important it is to keep the
+ * filter type consistent between rows.  Larger numbers mean the current
+ * filter is that many times as likely to be the same as the "num_weights"
+ * previous filters.  This is cumulative for each previous row with a weight.
+ * There needs to be "num_weights" values in "filter_weights", or it can be
+ * NULL if the weights aren't being specified.  Weights have no influence on
+ * the selection of the first row filter.  Well chosen weights can (in theory)
+ * improve the compression for a given image.
+ *
+ * Costs are factors >= 1.0 indicating the relative decoding costs of a
+ * filter type.  Higher costs indicate more decoding expense, and are
+ * therefore less likely to be selected over a filter with lower computational
+ * costs.  There needs to be a value in "filter_costs" for each valid filter
+ * type (given by PNG_FILTER_VALUE_LAST), or it can be NULL if you aren't
+ * setting the costs.  Costs try to improve the speed of decompression without
+ * unduly increasing the compressed image size.
+ *
+ * A negative weight or cost indicates the default value is to be used, and
+ * values in the range [0.0, 1.0) indicate the value is to remain unchanged.
+ * The default values for both weights and costs are currently 1.0, but may
+ * change if good general weighting/cost heuristics can be found.  If both
+ * the weights and costs are set to 1.0, this degenerates the WEIGHTED method
+ * to the UNWEIGHTED method, but with added encoding time/computation.
+ */
+#ifdef PNG_FLOATING_POINT_SUPPORTED
+extern PNG_EXPORT(void,png_set_filter_heuristics) PNGARG((png_structp png_ptr,
+   int heuristic_method, int num_weights, png_doublep filter_weights,
+   png_doublep filter_costs));
+#endif
+#endif /*  PNG_WRITE_WEIGHTED_FILTER_SUPPORTED */
+
+/* Heuristic used for row filter selection.  These defines should NOT be
+ * changed.
+ */
+#define PNG_FILTER_HEURISTIC_DEFAULT    0  /* Currently "UNWEIGHTED" */
+#define PNG_FILTER_HEURISTIC_UNWEIGHTED 1  /* Used by libpng < 0.95 */
+#define PNG_FILTER_HEURISTIC_WEIGHTED   2  /* Experimental feature */
+#define PNG_FILTER_HEURISTIC_LAST       3  /* Not a valid value */
+
+/* Set the library compression level.  Currently, valid values range from
+ * 0 - 9, corresponding directly to the zlib compression levels 0 - 9
+ * (0 - no compression, 9 - "maximal" compression).  Note that tests have
+ * shown that zlib compression levels 3-6 usually perform as well as level 9
+ * for PNG images, and do considerably fewer caclulations.  In the future,
+ * these values may not correspond directly to the zlib compression levels.
+ */
+extern PNG_EXPORT(void,png_set_compression_level) PNGARG((png_structp png_ptr,
+   int level));
+
+extern PNG_EXPORT(void,png_set_compression_mem_level)
+   PNGARG((png_structp png_ptr, int mem_level));
+
+extern PNG_EXPORT(void,png_set_compression_strategy)
+   PNGARG((png_structp png_ptr, int strategy));
+
+extern PNG_EXPORT(void,png_set_compression_window_bits)
+   PNGARG((png_structp png_ptr, int window_bits));
+
+extern PNG_EXPORT(void,png_set_compression_method) PNGARG((png_structp png_ptr,
+   int method));
+
+/* These next functions are called for input/output, memory, and error
+ * handling.  They are in the file pngrio.c, pngwio.c, and pngerror.c,
+ * and call standard C I/O routines such as fread(), fwrite(), and
+ * fprintf().  These functions can be made to use other I/O routines
+ * at run time for those applications that need to handle I/O in a
+ * different manner by calling png_set_???_fn().  See libpng.txt for
+ * more information.
+ */
+
+#ifdef PNG_STDIO_SUPPORTED
+/* Initialize the input/output for the PNG file to the default functions. */
+extern PNG_EXPORT(void,png_init_io) PNGARG((png_structp png_ptr,
+    png_FILE_p fp));
+#endif
+
+/* Replace the (error and abort), and warning functions with user
+ * supplied functions.  If no messages are to be printed you must still
+ * write and use replacement functions. The replacement error_fn should
+ * still do a longjmp to the last setjmp location if you are using this
+ * method of error handling.  If error_fn or warning_fn is NULL, the
+ * default function will be used.
+ */
+
+extern PNG_EXPORT(void,png_set_error_fn) PNGARG((png_structp png_ptr,
+   png_voidp error_ptr, png_error_ptr error_fn, png_error_ptr warning_fn));
+
+/* Return the user pointer associated with the error functions */
+extern PNG_EXPORT(png_voidp,png_get_error_ptr) PNGARG((png_structp png_ptr));
+
+/* Replace the default data output functions with a user supplied one(s).
+ * If buffered output is not used, then output_flush_fn can be set to NULL.
+ * If PNG_WRITE_FLUSH_SUPPORTED is not defined at libpng compile time
+ * output_flush_fn will be ignored (and thus can be NULL).
+ * It is probably a mistake to use NULL for output_flush_fn if
+ * write_data_fn is not also NULL unless you have built libpng with
+ * PNG_WRITE_FLUSH_SUPPORTED undefined, because in this case libpng's
+ * default flush function, which uses the standard *FILE structure, will
+ * be used.
+ */
+extern PNG_EXPORT(void,png_set_write_fn) PNGARG((png_structp png_ptr,
+   png_voidp io_ptr, png_rw_ptr write_data_fn, png_flush_ptr output_flush_fn));
+
+/* Replace the default data input function with a user supplied one. */
+extern PNG_EXPORT(void,png_set_read_fn) PNGARG((png_structp png_ptr,
+   png_voidp io_ptr, png_rw_ptr read_data_fn));
+
+/* Return the user pointer associated with the I/O functions */
+extern PNG_EXPORT(png_voidp,png_get_io_ptr) PNGARG((png_structp png_ptr));
+
+extern PNG_EXPORT(void,png_set_read_status_fn) PNGARG((png_structp png_ptr,
+   png_read_status_ptr read_row_fn));
+
+extern PNG_EXPORT(void,png_set_write_status_fn) PNGARG((png_structp png_ptr,
+   png_write_status_ptr write_row_fn));
+
+#ifdef PNG_USER_MEM_SUPPORTED
+/* Replace the default memory allocation functions with user supplied one(s). */
+extern PNG_EXPORT(void,png_set_mem_fn) PNGARG((png_structp png_ptr,
+   png_voidp mem_ptr, png_malloc_ptr malloc_fn, png_free_ptr free_fn));
+/* Return the user pointer associated with the memory functions */
+extern PNG_EXPORT(png_voidp,png_get_mem_ptr) PNGARG((png_structp png_ptr));
+#endif
+
+#ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
+extern PNG_EXPORT(void,png_set_read_user_transform_fn) PNGARG((png_structp
+   png_ptr, png_user_transform_ptr read_user_transform_fn));
+#endif
+
+#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
+extern PNG_EXPORT(void,png_set_write_user_transform_fn) PNGARG((png_structp
+   png_ptr, png_user_transform_ptr write_user_transform_fn));
+#endif
+
+#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \
+    defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
+extern PNG_EXPORT(void,png_set_user_transform_info) PNGARG((png_structp
+   png_ptr, png_voidp user_transform_ptr, int user_transform_depth,
+   int user_transform_channels));
+/* Return the user pointer associated with the user transform functions */
+extern PNG_EXPORT(png_voidp,png_get_user_transform_ptr)
+   PNGARG((png_structp png_ptr));
+#endif
+
+#ifdef PNG_USER_CHUNKS_SUPPORTED
+extern PNG_EXPORT(void,png_set_read_user_chunk_fn) PNGARG((png_structp png_ptr,
+   png_voidp user_chunk_ptr, png_user_chunk_ptr read_user_chunk_fn));
+extern PNG_EXPORT(png_voidp,png_get_user_chunk_ptr) PNGARG((png_structp
+   png_ptr));
+#endif
+
+#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
+/* Sets the function callbacks for the push reader, and a pointer to a
+ * user-defined structure available to the callback functions.
+ */
+extern PNG_EXPORT(void,png_set_progressive_read_fn) PNGARG((png_structp png_ptr,
+   png_voidp progressive_ptr,
+   png_progressive_info_ptr info_fn, png_progressive_row_ptr row_fn,
+   png_progressive_end_ptr end_fn));
+
+/* Returns the user pointer associated with the push read functions */
+extern PNG_EXPORT(png_voidp,png_get_progressive_ptr)
+   PNGARG((png_structp png_ptr));
+
+/* Function to be called when data becomes available */
+extern PNG_EXPORT(void,png_process_data) PNGARG((png_structp png_ptr,
+   png_infop info_ptr, png_bytep buffer, png_size_t buffer_size));
+
+/* Function that combines rows.  Not very much different than the
+ * png_combine_row() call.  Is this even used?????
+ */
+extern PNG_EXPORT(void,png_progressive_combine_row) PNGARG((png_structp png_ptr,
+   png_bytep old_row, png_bytep new_row));
+#endif /* PNG_PROGRESSIVE_READ_SUPPORTED */
+
+extern PNG_EXPORT(png_voidp,png_malloc) PNGARG((png_structp png_ptr,
+   png_alloc_size_t size)) PNG_ALLOCATED;
+/* Added at libpng version 1.4.0 */
+extern PNG_EXPORT(png_voidp,png_calloc) PNGARG((png_structp png_ptr,
+   png_alloc_size_t size)) PNG_ALLOCATED;
+
+/* Added at libpng version 1.2.4 */
+extern PNG_EXPORT(png_voidp,png_malloc_warn) PNGARG((png_structp png_ptr,
+   png_alloc_size_t size)) PNG_ALLOCATED;
+
+/* Frees a pointer allocated by png_malloc() */
+extern PNG_EXPORT(void,png_free) PNGARG((png_structp png_ptr, png_voidp ptr));
+
+/* Free data that was allocated internally */
+extern PNG_EXPORT(void,png_free_data) PNGARG((png_structp png_ptr,
+   png_infop info_ptr, png_uint_32 free_me, int num));
+/* Reassign responsibility for freeing existing data, whether allocated
+ * by libpng or by the application */
+extern PNG_EXPORT(void,png_data_freer) PNGARG((png_structp png_ptr,
+   png_infop info_ptr, int freer, png_uint_32 mask));
+/* Assignments for png_data_freer */
+#define PNG_DESTROY_WILL_FREE_DATA 1
+#define PNG_SET_WILL_FREE_DATA 1
+#define PNG_USER_WILL_FREE_DATA 2
+/* Flags for png_ptr->free_me and info_ptr->free_me */
+#define PNG_FREE_HIST 0x0008
+#define PNG_FREE_ICCP 0x0010
+#define PNG_FREE_SPLT 0x0020
+#define PNG_FREE_ROWS 0x0040
+#define PNG_FREE_PCAL 0x0080
+#define PNG_FREE_SCAL 0x0100
+#define PNG_FREE_UNKN 0x0200
+#define PNG_FREE_LIST 0x0400
+#define PNG_FREE_PLTE 0x1000
+#define PNG_FREE_TRNS 0x2000
+#define PNG_FREE_TEXT 0x4000
+#define PNG_FREE_ALL  0x7fff
+#define PNG_FREE_MUL  0x4220 /* PNG_FREE_SPLT|PNG_FREE_TEXT|PNG_FREE_UNKN */
+
+#ifdef PNG_USER_MEM_SUPPORTED
+extern PNG_EXPORT(png_voidp,png_malloc_default) PNGARG((png_structp png_ptr,
+   png_alloc_size_t size)) PNG_ALLOCATED;
+extern PNG_EXPORT(void,png_free_default) PNGARG((png_structp png_ptr,
+   png_voidp ptr));
+#endif
+
+#ifndef PNG_NO_ERROR_TEXT
+/* Fatal error in PNG image of libpng - can't continue */
+extern PNG_EXPORT(void,png_error) PNGARG((png_structp png_ptr,
+   png_const_charp error_message)) PNG_NORETURN;
+
+/* The same, but the chunk name is prepended to the error string. */
+extern PNG_EXPORT(void,png_chunk_error) PNGARG((png_structp png_ptr,
+   png_const_charp error_message)) PNG_NORETURN;
+
+#else
+/* Fatal error in PNG image of libpng - can't continue */
+extern PNG_EXPORT(void,png_err) PNGARG((png_structp png_ptr)) PNG_NORETURN;
+#endif
+
+/* Non-fatal error in libpng.  Can continue, but may have a problem. */
+extern PNG_EXPORT(void,png_warning) PNGARG((png_structp png_ptr,
+   png_const_charp warning_message));
+
+/* Non-fatal error in libpng, chunk name is prepended to message. */
+extern PNG_EXPORT(void,png_chunk_warning) PNGARG((png_structp png_ptr,
+   png_const_charp warning_message));
+
+#ifdef PNG_BENIGN_ERRORS_SUPPORTED
+/* Benign error in libpng.  Can continue, but may have a problem.
+ * User can choose whether to handle as a fatal error or as a warning. */
+extern PNG_EXPORT(void,png_benign_error) PNGARG((png_structp png_ptr,
+   png_const_charp warning_message));
+
+/* Same, chunk name is prepended to message. */
+extern PNG_EXPORT(void,png_chunk_benign_error) PNGARG((png_structp png_ptr,
+   png_const_charp warning_message));
+
+extern PNG_EXPORT(void,png_set_benign_errors) PNGARG((png_structp
+   png_ptr, int allowed));
+#endif
+
+/* The png_set_<chunk> functions are for storing values in the png_info_struct.
+ * Similarly, the png_get_<chunk> calls are used to read values from the
+ * png_info_struct, either storing the parameters in the passed variables, or
+ * setting pointers into the png_info_struct where the data is stored.  The
+ * png_get_<chunk> functions return a non-zero value if the data was available
+ * in info_ptr, or return zero and do not change any of the parameters if the
+ * data was not available.
+ *
+ * These functions should be used instead of directly accessing png_info
+ * to avoid problems with future changes in the size and internal layout of
+ * png_info_struct.
+ */
+/* Returns "flag" if chunk data is valid in info_ptr. */
+extern PNG_EXPORT(png_uint_32,png_get_valid) PNGARG((png_structp png_ptr,
+png_infop info_ptr, png_uint_32 flag));
+
+/* Returns number of bytes needed to hold a transformed row. */
+extern PNG_EXPORT(png_size_t,png_get_rowbytes) PNGARG((png_structp png_ptr,
+png_infop info_ptr));
+
+#ifdef PNG_INFO_IMAGE_SUPPORTED
+/* Returns row_pointers, which is an array of pointers to scanlines that was
+ * returned from png_read_png().
+ */
+extern PNG_EXPORT(png_bytepp,png_get_rows) PNGARG((png_structp png_ptr,
+png_infop info_ptr));
+/* Set row_pointers, which is an array of pointers to scanlines for use
+ * by png_write_png().
+ */
+extern PNG_EXPORT(void,png_set_rows) PNGARG((png_structp png_ptr,
+   png_infop info_ptr, png_bytepp row_pointers));
+#endif
+
+/* Returns number of color channels in image. */
+extern PNG_EXPORT(png_byte,png_get_channels) PNGARG((png_structp png_ptr,
+png_infop info_ptr));
+
+#ifdef PNG_EASY_ACCESS_SUPPORTED
+/* Returns image width in pixels. */
+extern PNG_EXPORT(png_uint_32, png_get_image_width) PNGARG((png_structp
+png_ptr, png_infop info_ptr));
+
+/* Returns image height in pixels. */
+extern PNG_EXPORT(png_uint_32, png_get_image_height) PNGARG((png_structp
+png_ptr, png_infop info_ptr));
+
+/* Returns image bit_depth. */
+extern PNG_EXPORT(png_byte, png_get_bit_depth) PNGARG((png_structp
+png_ptr, png_infop info_ptr));
+
+/* Returns image color_type. */
+extern PNG_EXPORT(png_byte, png_get_color_type) PNGARG((png_structp
+png_ptr, png_infop info_ptr));
+
+/* Returns image filter_type. */
+extern PNG_EXPORT(png_byte, png_get_filter_type) PNGARG((png_structp
+png_ptr, png_infop info_ptr));
+
+/* Returns image interlace_type. */
+extern PNG_EXPORT(png_byte, png_get_interlace_type) PNGARG((png_structp
+png_ptr, png_infop info_ptr));
+
+/* Returns image compression_type. */
+extern PNG_EXPORT(png_byte, png_get_compression_type) PNGARG((png_structp
+png_ptr, png_infop info_ptr));
+
+/* Returns image resolution in pixels per meter, from pHYs chunk data. */
+extern PNG_EXPORT(png_uint_32, png_get_pixels_per_meter) PNGARG((png_structp
+png_ptr, png_infop info_ptr));
+extern PNG_EXPORT(png_uint_32, png_get_x_pixels_per_meter) PNGARG((png_structp
+png_ptr, png_infop info_ptr));
+extern PNG_EXPORT(png_uint_32, png_get_y_pixels_per_meter) PNGARG((png_structp
+png_ptr, png_infop info_ptr));
+
+/* Returns pixel aspect ratio, computed from pHYs chunk data.  */
+#ifdef PNG_FLOATING_POINT_SUPPORTED
+extern PNG_EXPORT(float, png_get_pixel_aspect_ratio) PNGARG((png_structp
+png_ptr, png_infop info_ptr));
+#endif
+
+/* Returns image x, y offset in pixels or microns, from oFFs chunk data. */
+extern PNG_EXPORT(png_int_32, png_get_x_offset_pixels) PNGARG((png_structp
+png_ptr, png_infop info_ptr));
+extern PNG_EXPORT(png_int_32, png_get_y_offset_pixels) PNGARG((png_structp
+png_ptr, png_infop info_ptr));
+extern PNG_EXPORT(png_int_32, png_get_x_offset_microns) PNGARG((png_structp
+png_ptr, png_infop info_ptr));
+extern PNG_EXPORT(png_int_32, png_get_y_offset_microns) PNGARG((png_structp
+png_ptr, png_infop info_ptr));
+
+#endif /* PNG_EASY_ACCESS_SUPPORTED */
+
+/* Returns pointer to signature string read from PNG header */
+extern PNG_EXPORT(png_bytep,png_get_signature) PNGARG((png_structp png_ptr,
+png_infop info_ptr));
+
+#ifdef PNG_bKGD_SUPPORTED
+extern PNG_EXPORT(png_uint_32,png_get_bKGD) PNGARG((png_structp png_ptr,
+   png_infop info_ptr, png_color_16p *background));
+#endif
+
+#ifdef PNG_bKGD_SUPPORTED
+extern PNG_EXPORT(void,png_set_bKGD) PNGARG((png_structp png_ptr,
+   png_infop info_ptr, png_color_16p background));
+#endif
+
+#ifdef PNG_cHRM_SUPPORTED
+#ifdef PNG_FLOATING_POINT_SUPPORTED
+extern PNG_EXPORT(png_uint_32,png_get_cHRM) PNGARG((png_structp png_ptr,
+   png_infop info_ptr, double *white_x, double *white_y, double *red_x,
+   double *red_y, double *green_x, double *green_y, double *blue_x,
+   double *blue_y));
+#endif
+#ifdef PNG_FIXED_POINT_SUPPORTED
+extern PNG_EXPORT(png_uint_32,png_get_cHRM_fixed) PNGARG((png_structp png_ptr,
+   png_infop info_ptr, png_fixed_point *int_white_x, png_fixed_point
+   *int_white_y, png_fixed_point *int_red_x, png_fixed_point *int_red_y,
+   png_fixed_point *int_green_x, png_fixed_point *int_green_y, png_fixed_point
+   *int_blue_x, png_fixed_point *int_blue_y));
+#endif
+#endif
+
+#ifdef PNG_cHRM_SUPPORTED
+#ifdef PNG_FLOATING_POINT_SUPPORTED
+extern PNG_EXPORT(void,png_set_cHRM) PNGARG((png_structp png_ptr,
+   png_infop info_ptr, double white_x, double white_y, double red_x,
+   double red_y, double green_x, double green_y, double blue_x, double blue_y));
+#endif
+#ifdef PNG_FIXED_POINT_SUPPORTED
+extern PNG_EXPORT(void,png_set_cHRM_fixed) PNGARG((png_structp png_ptr,
+   png_infop info_ptr, png_fixed_point int_white_x, png_fixed_point int_white_y,
+   png_fixed_point int_red_x, png_fixed_point int_red_y, png_fixed_point
+   int_green_x, png_fixed_point int_green_y, png_fixed_point int_blue_x,
+   png_fixed_point int_blue_y));
+#endif
+#endif
+
+#ifdef PNG_gAMA_SUPPORTED
+#ifdef PNG_FLOATING_POINT_SUPPORTED
+extern PNG_EXPORT(png_uint_32,png_get_gAMA) PNGARG((png_structp png_ptr,
+   png_infop info_ptr, double *file_gamma));
+#endif
+extern PNG_EXPORT(png_uint_32,png_get_gAMA_fixed) PNGARG((png_structp png_ptr,
+   png_infop info_ptr, png_fixed_point *int_file_gamma));
+#endif
+
+#ifdef PNG_gAMA_SUPPORTED
+#ifdef PNG_FLOATING_POINT_SUPPORTED
+extern PNG_EXPORT(void,png_set_gAMA) PNGARG((png_structp png_ptr,
+   png_infop info_ptr, double file_gamma));
+#endif
+extern PNG_EXPORT(void,png_set_gAMA_fixed) PNGARG((png_structp png_ptr,
+   png_infop info_ptr, png_fixed_point int_file_gamma));
+#endif
+
+#ifdef PNG_hIST_SUPPORTED
+extern PNG_EXPORT(png_uint_32,png_get_hIST) PNGARG((png_structp png_ptr,
+   png_infop info_ptr, png_uint_16p *hist));
+#endif
+
+#ifdef PNG_hIST_SUPPORTED
+extern PNG_EXPORT(void,png_set_hIST) PNGARG((png_structp png_ptr,
+   png_infop info_ptr, png_uint_16p hist));
+#endif
+
+extern PNG_EXPORT(png_uint_32,png_get_IHDR) PNGARG((png_structp png_ptr,
+   png_infop info_ptr, png_uint_32 *width, png_uint_32 *height,
+   int *bit_depth, int *color_type, int *interlace_method,
+   int *compression_method, int *filter_method));
+
+extern PNG_EXPORT(void,png_set_IHDR) PNGARG((png_structp png_ptr,
+   png_infop info_ptr, png_uint_32 width, png_uint_32 height, int bit_depth,
+   int color_type, int interlace_method, int compression_method,
+   int filter_method));
+
+#ifdef PNG_oFFs_SUPPORTED
+extern PNG_EXPORT(png_uint_32,png_get_oFFs) PNGARG((png_structp png_ptr,
+   png_infop info_ptr, png_int_32 *offset_x, png_int_32 *offset_y,
+   int *unit_type));
+#endif
+
+#ifdef PNG_oFFs_SUPPORTED
+extern PNG_EXPORT(void,png_set_oFFs) PNGARG((png_structp png_ptr,
+   png_infop info_ptr, png_int_32 offset_x, png_int_32 offset_y,
+   int unit_type));
+#endif
+
+#ifdef PNG_pCAL_SUPPORTED
+extern PNG_EXPORT(png_uint_32,png_get_pCAL) PNGARG((png_structp png_ptr,
+   png_infop info_ptr, png_charp *purpose, png_int_32 *X0, png_int_32 *X1,
+   int *type, int *nparams, png_charp *units, png_charpp *params));
+#endif
+
+#ifdef PNG_pCAL_SUPPORTED
+extern PNG_EXPORT(void,png_set_pCAL) PNGARG((png_structp png_ptr,
+   png_infop info_ptr, png_charp purpose, png_int_32 X0, png_int_32 X1,
+   int type, int nparams, png_charp units, png_charpp params));
+#endif
+
+#ifdef PNG_pHYs_SUPPORTED
+extern PNG_EXPORT(png_uint_32,png_get_pHYs) PNGARG((png_structp png_ptr,
+   png_infop info_ptr, png_uint_32 *res_x, png_uint_32 *res_y, int *unit_type));
+#endif
+
+#ifdef PNG_pHYs_SUPPORTED
+extern PNG_EXPORT(void,png_set_pHYs) PNGARG((png_structp png_ptr,
+   png_infop info_ptr, png_uint_32 res_x, png_uint_32 res_y, int unit_type));
+#endif
+
+extern PNG_EXPORT(png_uint_32,png_get_PLTE) PNGARG((png_structp png_ptr,
+   png_infop info_ptr, png_colorp *palette, int *num_palette));
+
+extern PNG_EXPORT(void,png_set_PLTE) PNGARG((png_structp png_ptr,
+   png_infop info_ptr, png_colorp palette, int num_palette));
+
+#ifdef PNG_sBIT_SUPPORTED
+extern PNG_EXPORT(png_uint_32,png_get_sBIT) PNGARG((png_structp png_ptr,
+   png_infop info_ptr, png_color_8p *sig_bit));
+#endif
+
+#ifdef PNG_sBIT_SUPPORTED
+extern PNG_EXPORT(void,png_set_sBIT) PNGARG((png_structp png_ptr,
+   png_infop info_ptr, png_color_8p sig_bit));
+#endif
+
+#ifdef PNG_sRGB_SUPPORTED
+extern PNG_EXPORT(png_uint_32,png_get_sRGB) PNGARG((png_structp png_ptr,
+   png_infop info_ptr, int *intent));
+#endif
+
+#ifdef PNG_sRGB_SUPPORTED
+extern PNG_EXPORT(void,png_set_sRGB) PNGARG((png_structp png_ptr,
+   png_infop info_ptr, int intent));
+extern PNG_EXPORT(void,png_set_sRGB_gAMA_and_cHRM) PNGARG((png_structp png_ptr,
+   png_infop info_ptr, int intent));
+#endif
+
+#ifdef PNG_iCCP_SUPPORTED
+extern PNG_EXPORT(png_uint_32,png_get_iCCP) PNGARG((png_structp png_ptr,
+   png_infop info_ptr, png_charpp name, int *compression_type,
+   png_charpp profile, png_uint_32 *proflen));
+   /* Note to maintainer: profile should be png_bytepp */
+#endif
+
+#ifdef PNG_iCCP_SUPPORTED
+extern PNG_EXPORT(void,png_set_iCCP) PNGARG((png_structp png_ptr,
+   png_infop info_ptr, png_charp name, int compression_type,
+   png_charp profile, png_uint_32 proflen));
+   /* Note to maintainer: profile should be png_bytep */
+#endif
+
+#ifdef PNG_sPLT_SUPPORTED
+extern PNG_EXPORT(png_uint_32,png_get_sPLT) PNGARG((png_structp png_ptr,
+   png_infop info_ptr, png_sPLT_tpp entries));
+#endif
+
+#ifdef PNG_sPLT_SUPPORTED
+extern PNG_EXPORT(void,png_set_sPLT) PNGARG((png_structp png_ptr,
+   png_infop info_ptr, png_sPLT_tp entries, int nentries));
+#endif
+
+#ifdef PNG_TEXT_SUPPORTED
+/* png_get_text also returns the number of text chunks in *num_text */
+extern PNG_EXPORT(png_uint_32,png_get_text) PNGARG((png_structp png_ptr,
+   png_infop info_ptr, png_textp *text_ptr, int *num_text));
+#endif
+
+/* Note while png_set_text() will accept a structure whose text,
+ * language, and  translated keywords are NULL pointers, the structure
+ * returned by png_get_text will always contain regular
+ * zero-terminated C strings.  They might be empty strings but
+ * they will never be NULL pointers.
+ */
+
+#ifdef PNG_TEXT_SUPPORTED
+extern PNG_EXPORT(void,png_set_text) PNGARG((png_structp png_ptr,
+   png_infop info_ptr, png_textp text_ptr, int num_text));
+#endif
+
+#ifdef PNG_tIME_SUPPORTED
+extern PNG_EXPORT(png_uint_32,png_get_tIME) PNGARG((png_structp png_ptr,
+   png_infop info_ptr, png_timep *mod_time));
+#endif
+
+#ifdef PNG_tIME_SUPPORTED
+extern PNG_EXPORT(void,png_set_tIME) PNGARG((png_structp png_ptr,
+   png_infop info_ptr, png_timep mod_time));
+#endif
+
+#ifdef PNG_tRNS_SUPPORTED
+extern PNG_EXPORT(png_uint_32,png_get_tRNS) PNGARG((png_structp png_ptr,
+   png_infop info_ptr, png_bytep *trans_alpha, int *num_trans,
+   png_color_16p *trans_color));
+#endif
+
+#ifdef PNG_tRNS_SUPPORTED
+extern PNG_EXPORT(void,png_set_tRNS) PNGARG((png_structp png_ptr,
+   png_infop info_ptr, png_bytep trans_alpha, int num_trans,
+   png_color_16p trans_color));
+#endif
+
+#ifdef PNG_tRNS_SUPPORTED
+#endif
+
+#ifdef PNG_sCAL_SUPPORTED
+#ifdef PNG_FLOATING_POINT_SUPPORTED
+extern PNG_EXPORT(png_uint_32,png_get_sCAL) PNGARG((png_structp png_ptr,
+   png_infop info_ptr, int *unit, double *width, double *height));
+#else
+#ifdef PNG_FIXED_POINT_SUPPORTED
+extern PNG_EXPORT(png_uint_32,png_get_sCAL_s) PNGARG((png_structp png_ptr,
+   png_infop info_ptr, int *unit, png_charpp swidth, png_charpp sheight));
+#endif
+#endif
+#endif /* PNG_sCAL_SUPPORTED */
+
+#ifdef PNG_sCAL_SUPPORTED
+#ifdef PNG_FLOATING_POINT_SUPPORTED
+extern PNG_EXPORT(void,png_set_sCAL) PNGARG((png_structp png_ptr,
+   png_infop info_ptr, int unit, double width, double height));
+#else
+#ifdef PNG_FIXED_POINT_SUPPORTED
+extern PNG_EXPORT(void,png_set_sCAL_s) PNGARG((png_structp png_ptr,
+   png_infop info_ptr, int unit, png_charp swidth, png_charp sheight));
+#endif
+#endif
+#endif /* PNG_sCAL_SUPPORTED || PNG_WRITE_sCAL_SUPPORTED */
+
+#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
+/* Provide a list of chunks and how they are to be handled, if the built-in
+   handling or default unknown chunk handling is not desired.  Any chunks not
+   listed will be handled in the default manner.  The IHDR and IEND chunks
+   must not be listed.
+      keep = 0: follow default behaviour
+           = 1: do not keep
+           = 2: keep only if safe-to-copy
+           = 3: keep even if unsafe-to-copy
+*/
+extern PNG_EXPORT(void, png_set_keep_unknown_chunks) PNGARG((png_structp
+   png_ptr, int keep, png_bytep chunk_list, int num_chunks));
+PNG_EXPORT(int,png_handle_as_unknown) PNGARG((png_structp png_ptr, png_bytep
+   chunk_name));
+#endif
+#ifdef PNG_UNKNOWN_CHUNKS_SUPPORTED
+extern PNG_EXPORT(void, png_set_unknown_chunks) PNGARG((png_structp png_ptr,
+   png_infop info_ptr, png_unknown_chunkp unknowns, int num_unknowns));
+extern PNG_EXPORT(void, png_set_unknown_chunk_location)
+   PNGARG((png_structp png_ptr, png_infop info_ptr, int chunk, int location));
+extern PNG_EXPORT(png_uint_32,png_get_unknown_chunks) PNGARG((png_structp
+   png_ptr, png_infop info_ptr, png_unknown_chunkpp entries));
+#endif
+
+/* Png_free_data() will turn off the "valid" flag for anything it frees.
+ * If you need to turn it off for a chunk that your application has freed,
+ * you can use png_set_invalid(png_ptr, info_ptr, PNG_INFO_CHNK);
+ */
+extern PNG_EXPORT(void, png_set_invalid) PNGARG((png_structp png_ptr,
+   png_infop info_ptr, int mask));
+
+#ifdef PNG_INFO_IMAGE_SUPPORTED
+/* The "params" pointer is currently not used and is for future expansion. */
+extern PNG_EXPORT(void, png_read_png) PNGARG((png_structp png_ptr,
+                        png_infop info_ptr,
+                        int transforms,
+                        png_voidp params));
+extern PNG_EXPORT(void, png_write_png) PNGARG((png_structp png_ptr,
+                        png_infop info_ptr,
+                        int transforms,
+                        png_voidp params));
+#endif
+
+extern PNG_EXPORT(png_charp,png_get_copyright) PNGARG((png_structp png_ptr));
+extern PNG_EXPORT(png_charp,png_get_header_ver) PNGARG((png_structp png_ptr));
+extern PNG_EXPORT(png_charp,png_get_header_version) PNGARG((png_structp
+    png_ptr));
+extern PNG_EXPORT(png_charp,png_get_libpng_ver) PNGARG((png_structp png_ptr));
+
+#ifdef PNG_MNG_FEATURES_SUPPORTED
+extern PNG_EXPORT(png_uint_32,png_permit_mng_features) PNGARG((png_structp
+   png_ptr, png_uint_32 mng_features_permitted));
+#endif
+
+/* For use in png_set_keep_unknown, added to version 1.2.6 */
+#define PNG_HANDLE_CHUNK_AS_DEFAULT   0
+#define PNG_HANDLE_CHUNK_NEVER        1
+#define PNG_HANDLE_CHUNK_IF_SAFE      2
+#define PNG_HANDLE_CHUNK_ALWAYS       3
+
+/* Strip the prepended error numbers ("#nnn ") from error and warning
+ * messages before passing them to the error or warning handler.
+ */
+#ifdef PNG_ERROR_NUMBERS_SUPPORTED
+extern PNG_EXPORT(void,png_set_strip_error_numbers) PNGARG((png_structp
+   png_ptr, png_uint_32 strip_mode));
+#endif
+
+/* Added in libpng-1.2.6 */
+#ifdef PNG_SET_USER_LIMITS_SUPPORTED
+extern PNG_EXPORT(void,png_set_user_limits) PNGARG((png_structp
+   png_ptr, png_uint_32 user_width_max, png_uint_32 user_height_max));
+extern PNG_EXPORT(png_uint_32,png_get_user_width_max) PNGARG((png_structp
+   png_ptr));
+extern PNG_EXPORT(png_uint_32,png_get_user_height_max) PNGARG((png_structp
+   png_ptr));
+/* Added in libpng-1.4.0 */
+extern PNG_EXPORT(void,png_set_chunk_cache_max) PNGARG((png_structp
+   png_ptr, png_uint_32 user_chunk_cache_max));
+extern PNG_EXPORT(png_uint_32,png_get_chunk_cache_max)
+   PNGARG((png_structp png_ptr));
+/* Added in libpng-1.4.1 */
+extern PNG_EXPORT(void,png_set_chunk_malloc_max) PNGARG((png_structp
+   png_ptr, png_alloc_size_t user_chunk_cache_max));
+extern PNG_EXPORT(png_alloc_size_t,png_get_chunk_malloc_max)
+   PNGARG((png_structp png_ptr));
+#endif
+
+#if defined(PNG_INCH_CONVERSIONS) && defined(PNG_FLOATING_POINT_SUPPORTED)
+PNG_EXPORT(png_uint_32,png_get_pixels_per_inch) PNGARG((png_structp png_ptr,
+png_infop info_ptr));
+
+PNG_EXPORT(png_uint_32,png_get_x_pixels_per_inch) PNGARG((png_structp png_ptr,
+png_infop info_ptr));
+
+PNG_EXPORT(png_uint_32,png_get_y_pixels_per_inch) PNGARG((png_structp png_ptr,
+png_infop info_ptr));
+
+PNG_EXPORT(float,png_get_x_offset_inches) PNGARG((png_structp png_ptr,
+png_infop info_ptr));
+
+PNG_EXPORT(float,png_get_y_offset_inches) PNGARG((png_structp png_ptr,
+png_infop info_ptr));
+
+#ifdef PNG_pHYs_SUPPORTED
+PNG_EXPORT(png_uint_32,png_get_pHYs_dpi) PNGARG((png_structp png_ptr,
+png_infop info_ptr, png_uint_32 *res_x, png_uint_32 *res_y, int *unit_type));
+#endif /* PNG_pHYs_SUPPORTED */
+#endif  /* PNG_INCH_CONVERSIONS && PNG_FLOATING_POINT_SUPPORTED */
+
+/* Added in libpng-1.4.0 */
+#ifdef PNG_IO_STATE_SUPPORTED
+extern PNG_EXPORT(png_uint_32,png_get_io_state) PNGARG((png_structp png_ptr));
+
+extern PNG_EXPORT(png_bytep,png_get_io_chunk_name)
+   PNGARG((png_structp png_ptr));
+
+/* The flags returned by png_get_io_state() are the following: */
+#define PNG_IO_NONE        0x0000   /* no I/O at this moment */
+#define PNG_IO_READING     0x0001   /* currently reading */
+#define PNG_IO_WRITING     0x0002   /* currently writing */
+#define PNG_IO_SIGNATURE   0x0010   /* currently at the file signature */
+#define PNG_IO_CHUNK_HDR   0x0020   /* currently at the chunk header */
+#define PNG_IO_CHUNK_DATA  0x0040   /* currently at the chunk data */
+#define PNG_IO_CHUNK_CRC   0x0080   /* currently at the chunk crc */
+#define PNG_IO_MASK_OP     0x000f   /* current operation: reading/writing */
+#define PNG_IO_MASK_LOC    0x00f0   /* current location: sig/hdr/data/crc */
+#endif /* ?PNG_IO_STATE_SUPPORTED */
+
+/* Maintainer: Put new public prototypes here ^, in libpng.3, and project
+ * defs
+ */
+
+#ifdef PNG_READ_COMPOSITE_NODIV_SUPPORTED
+/* With these routines we avoid an integer divide, which will be slower on
+ * most machines.  However, it does take more operations than the corresponding
+ * divide method, so it may be slower on a few RISC systems.  There are two
+ * shifts (by 8 or 16 bits) and an addition, versus a single integer divide.
+ *
+ * Note that the rounding factors are NOT supposed to be the same!  128 and
+ * 32768 are correct for the NODIV code; 127 and 32767 are correct for the
+ * standard method.
+ *
+ * [Optimized code by Greg Roelofs and Mark Adler...blame us for bugs. :-) ]
+ */
+
+ /* fg and bg should be in `gamma 1.0' space; alpha is the opacity          */
+
+#  define png_composite(composite, fg, alpha, bg)         \
+     { png_uint_16 temp = (png_uint_16)((png_uint_16)(fg) \
+           * (png_uint_16)(alpha)                         \
+           + (png_uint_16)(bg)*(png_uint_16)(255          \
+           - (png_uint_16)(alpha)) + (png_uint_16)128);   \
+       (composite) = (png_byte)((temp + (temp >> 8)) >> 8); }
+
+#  define png_composite_16(composite, fg, alpha, bg)       \
+     { png_uint_32 temp = (png_uint_32)((png_uint_32)(fg)  \
+           * (png_uint_32)(alpha)                          \
+           + (png_uint_32)(bg)*(png_uint_32)(65535L        \
+           - (png_uint_32)(alpha)) + (png_uint_32)32768L); \
+       (composite) = (png_uint_16)((temp + (temp >> 16)) >> 16); }
+
+#else  /* Standard method using integer division */
+
+#  define png_composite(composite, fg, alpha, bg)                            \
+     (composite) = (png_byte)(((png_uint_16)(fg) * (png_uint_16)(alpha) +    \
+       (png_uint_16)(bg) * (png_uint_16)(255 - (png_uint_16)(alpha)) +       \
+       (png_uint_16)127) / 255)
+
+#  define png_composite_16(composite, fg, alpha, bg)                         \
+     (composite) = (png_uint_16)(((png_uint_32)(fg) * (png_uint_32)(alpha) + \
+       (png_uint_32)(bg)*(png_uint_32)(65535L - (png_uint_32)(alpha)) +      \
+       (png_uint_32)32767) / (png_uint_32)65535L)
+#endif /* PNG_READ_COMPOSITE_NODIV_SUPPORTED */
+
+#ifdef PNG_USE_READ_MACROS
+/* Inline macros to do direct reads of bytes from the input buffer.
+ * The png_get_int_32() routine assumes we are using two's complement
+ * format for negative values, which is almost certainly true.
+ */
+/* We could make special-case BIG_ENDIAN macros that do direct reads here */
+#  define png_get_uint_32(buf) \
+     (((png_uint_32)(*(buf)) << 24) + \
+      ((png_uint_32)(*((buf) + 1)) << 16) + \
+      ((png_uint_32)(*((buf) + 2)) << 8) + \
+      ((png_uint_32)(*((buf) + 3))))
+#  define png_get_uint_16(buf) \
+     (((png_uint_32)(*(buf)) << 8) + \
+      ((png_uint_32)(*((buf) + 1))))
+#ifdef PNG_GET_INT_32_SUPPORTED
+#  define png_get_int_32(buf) \
+     (((png_int_32)(*(buf)) << 24) + \
+      ((png_int_32)(*((buf) + 1)) << 16) + \
+      ((png_int_32)(*((buf) + 2)) << 8) + \
+      ((png_int_32)(*((buf) + 3))))
+#endif
+#else
+extern PNG_EXPORT(png_uint_32,png_get_uint_32) PNGARG((png_bytep buf));
+extern PNG_EXPORT(png_uint_16,png_get_uint_16) PNGARG((png_bytep buf));
+#ifdef PNG_GET_INT_32_SUPPORTED
+extern PNG_EXPORT(png_int_32,png_get_int_32) PNGARG((png_bytep buf));
+#endif
+#endif
+extern PNG_EXPORT(png_uint_32,png_get_uint_31)
+  PNGARG((png_structp png_ptr, png_bytep buf));
+/* No png_get_int_16 -- may be added if there's a real need for it. */
+
+/* Place a 32-bit number into a buffer in PNG byte order (big-endian). */
+extern PNG_EXPORT(void,png_save_uint_32)
+   PNGARG((png_bytep buf, png_uint_32 i));
+extern PNG_EXPORT(void,png_save_int_32)
+   PNGARG((png_bytep buf, png_int_32 i));
+
+/* Place a 16-bit number into a buffer in PNG byte order.
+ * The parameter is declared unsigned int, not png_uint_16,
+ * just to avoid potential problems on pre-ANSI C compilers.
+ */
+extern PNG_EXPORT(void,png_save_uint_16)
+   PNGARG((png_bytep buf, unsigned int i));
+/* No png_save_int_16 -- may be added if there's a real need for it. */
+
+/* ************************************************************************* */
+
+/* Various modes of operation.  Note that after an init, mode is set to
+ * zero automatically when the structure is created.
+ */
+#define PNG_HAVE_IHDR               0x01
+#define PNG_HAVE_PLTE               0x02
+#define PNG_HAVE_IDAT               0x04
+#define PNG_AFTER_IDAT              0x08 /* Have complete zlib datastream */
+#define PNG_HAVE_IEND               0x10
+#define PNG_HAVE_gAMA               0x20
+#define PNG_HAVE_cHRM               0x40
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* PNG_VERSION_INFO_ONLY */
+/* Do not put anything past this line */
+#endif /* PNG_H */
index b364fc0..bb02b27 100644 (file)
-
-/* pngerror.c - stub functions for i/o and memory allocation
- *
- * Last changed in libpng 1.2.22 [October 13, 2007]
- * For conditions of distribution and use, see copyright notice in png.h
- * Copyright (c) 1998-2007 Glenn Randers-Pehrson
- * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
- * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
- *
- * This file provides a location for all error handling.  Users who
- * need special error handling are expected to write replacement functions
- * and use png_set_error_fn() to use those functions.  See the instructions
- * at each function.
- */
-
-#define PNG_INTERNAL
-#include "png.h"
-
-#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
-static void /* PRIVATE */
-png_default_error PNGARG((png_structp png_ptr,
-  png_const_charp error_message));
-#ifndef PNG_NO_WARNINGS
-static void /* PRIVATE */
-png_default_warning PNGARG((png_structp png_ptr,
-  png_const_charp warning_message));
-#endif /* PNG_NO_WARNINGS */
-
-/* This function is called whenever there is a fatal error.  This function
- * should not be changed.  If there is a need to handle errors differently,
- * you should supply a replacement error function and use png_set_error_fn()
- * to replace the error function at run-time.
- */
-#ifndef PNG_NO_ERROR_TEXT
-void PNGAPI
-png_error(png_structp png_ptr, png_const_charp error_message)
-{
-#ifdef PNG_ERROR_NUMBERS_SUPPORTED
-   char msg[16];
-   if (png_ptr != NULL)
-   {
-     if (png_ptr->flags&
-       (PNG_FLAG_STRIP_ERROR_NUMBERS|PNG_FLAG_STRIP_ERROR_TEXT))
-     {
-       if (*error_message == '#')
-       {
-           int offset;
-           for (offset=1; offset<15; offset++)
-              if (*(error_message+offset) == ' ')
-                  break;
-           if (png_ptr->flags&PNG_FLAG_STRIP_ERROR_TEXT)
-           {
-              int i;
-              for (i=0; i<offset-1; i++)
-                 msg[i]=error_message[i+1];
-              msg[i]='\0';
-              error_message=msg;
-           }
-           else
-              error_message+=offset;
-       }
-       else
-       {
-           if (png_ptr->flags&PNG_FLAG_STRIP_ERROR_TEXT)
-           {
-              msg[0]='0';
-              msg[1]='\0';
-              error_message=msg;
-           }
-       }
-     }
-   }
-#endif
-   if (png_ptr != NULL && png_ptr->error_fn != NULL)
-      (*(png_ptr->error_fn))(png_ptr, error_message);
-
-   /* If the custom handler doesn't exist, or if it returns,
-      use the default handler, which will not return. */
-   png_default_error(png_ptr, error_message);
-}
-#else
-void PNGAPI
-png_err(png_structp png_ptr)
-{
-   if (png_ptr != NULL && png_ptr->error_fn != NULL)
-      (*(png_ptr->error_fn))(png_ptr, '\0');
-
-   /* If the custom handler doesn't exist, or if it returns,
-      use the default handler, which will not return. */
-   png_default_error(png_ptr, '\0');
-}
-#endif /* PNG_NO_ERROR_TEXT */
-
-#ifndef PNG_NO_WARNINGS
-/* This function is called whenever there is a non-fatal error.  This function
- * should not be changed.  If there is a need to handle warnings differently,
- * you should supply a replacement warning function and use
- * png_set_error_fn() to replace the warning function at run-time.
- */
-void PNGAPI
-png_warning(png_structp png_ptr, png_const_charp warning_message)
-{
-   int offset = 0;
-   if (png_ptr != NULL)
-   {
-#ifdef PNG_ERROR_NUMBERS_SUPPORTED
-   if (png_ptr->flags&
-     (PNG_FLAG_STRIP_ERROR_NUMBERS|PNG_FLAG_STRIP_ERROR_TEXT))
-#endif
-     {
-       if (*warning_message == '#')
-       {
-           for (offset=1; offset<15; offset++)
-              if (*(warning_message+offset) == ' ')
-                  break;
-       }
-     }
-     if (png_ptr != NULL && png_ptr->warning_fn != NULL)
-        (*(png_ptr->warning_fn))(png_ptr, warning_message+offset);
-   }
-   else
-      png_default_warning(png_ptr, warning_message+offset);
-}
-#endif /* PNG_NO_WARNINGS */
-
-
-/* These utilities are used internally to build an error message that relates
- * to the current chunk.  The chunk name comes from png_ptr->chunk_name,
- * this is used to prefix the message.  The message is limited in length
- * to 63 bytes, the name characters are output as hex digits wrapped in []
- * if the character is invalid.
- */
-#define isnonalpha(c) ((c) < 65 || (c) > 122 || ((c) > 90 && (c) < 97))
-static PNG_CONST char png_digit[16] = {
-   '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
-   'A', 'B', 'C', 'D', 'E', 'F'
-};
-
-#define PNG_MAX_ERROR_TEXT 64
-
-#if !defined(PNG_NO_WARNINGS) || !defined(PNG_NO_ERROR_TEXT)
-static void /* PRIVATE */
-png_format_buffer(png_structp png_ptr, png_charp buffer, png_const_charp
-   error_message)
-{
-   int iout = 0, iin = 0;
-
-   while (iin < 4)
-   {
-      int c = png_ptr->chunk_name[iin++];
-      if (isnonalpha(c))
-      {
-         buffer[iout++] = '[';
-         buffer[iout++] = png_digit[(c & 0xf0) >> 4];
-         buffer[iout++] = png_digit[c & 0x0f];
-         buffer[iout++] = ']';
-      }
-      else
-      {
-         buffer[iout++] = (png_byte)c;
-      }
-   }
-
-   if (error_message == NULL)
-      buffer[iout] = '\0';
-   else
-   {
-      buffer[iout++] = ':';
-      buffer[iout++] = ' ';
-      png_memcpy(buffer+iout, error_message, PNG_MAX_ERROR_TEXT);
-      buffer[iout+PNG_MAX_ERROR_TEXT-1] = '\0';
-   }
-}
-
-#ifdef PNG_READ_SUPPORTED
-void PNGAPI
-png_chunk_error(png_structp png_ptr, png_const_charp error_message)
-{
-   char msg[18+PNG_MAX_ERROR_TEXT];
-   if (png_ptr == NULL)
-     png_error(png_ptr, error_message);
-   else
-   {
-     png_format_buffer(png_ptr, msg, error_message);
-     png_error(png_ptr, msg);
-   }
-}
-#endif /* PNG_READ_SUPPORTED */
-#endif /* !defined(PNG_NO_WARNINGS) || !defined(PNG_NO_ERROR_TEXT) */
-
-#ifndef PNG_NO_WARNINGS
-void PNGAPI
-png_chunk_warning(png_structp png_ptr, png_const_charp warning_message)
-{
-   char msg[18+PNG_MAX_ERROR_TEXT];
-   if (png_ptr == NULL)
-     png_warning(png_ptr, warning_message);
-   else
-   {
-     png_format_buffer(png_ptr, msg, warning_message);
-     png_warning(png_ptr, msg);
-   }
-}
-#endif /* PNG_NO_WARNINGS */
-
-
-/* This is the default error handling function.  Note that replacements for
- * this function MUST NOT RETURN, or the program will likely crash.  This
- * function is used by default, or if the program supplies NULL for the
- * error function pointer in png_set_error_fn().
- */
-static void /* PRIVATE */
-png_default_error(png_structp png_ptr, png_const_charp error_message)
-{
-#ifndef PNG_NO_CONSOLE_IO
-#ifdef PNG_ERROR_NUMBERS_SUPPORTED
-   if (*error_message == '#')
-   {
-     int offset;
-     char error_number[16];
-     for (offset=0; offset<15; offset++)
-     {
-         error_number[offset] = *(error_message+offset+1);
-         if (*(error_message+offset) == ' ')
-             break;
-     }
-     if((offset > 1) && (offset < 15))
-     {
-       error_number[offset-1]='\0';
-       fprintf(stderr, "libpng error no. %s: %s\n", error_number,
-          error_message+offset);
-     }
-     else
-       fprintf(stderr, "libpng error: %s, offset=%d\n", error_message,offset);
-   }
-   else
-#endif
-   fprintf(stderr, "libpng error: %s\n", error_message);
-#endif
-
-#ifdef PNG_SETJMP_SUPPORTED
-   if (png_ptr)
-   {
-#  ifdef USE_FAR_KEYWORD
-   {
-      jmp_buf jmpbuf;
-      png_memcpy(jmpbuf, png_ptr->jmpbuf, png_sizeof(jmp_buf));
-      longjmp(jmpbuf, 1);
-   }
-#  else
-   longjmp(png_ptr->jmpbuf, 1);
-#  endif
-   }
-#else
-   PNG_ABORT();
-#endif
-#ifdef PNG_NO_CONSOLE_IO
-   error_message = error_message; /* make compiler happy */
-#endif
-}
-
-#ifndef PNG_NO_WARNINGS
-/* This function is called when there is a warning, but the library thinks
- * it can continue anyway.  Replacement functions don't have to do anything
- * here if you don't want them to.  In the default configuration, png_ptr is
- * not used, but it is passed in case it may be useful.
- */
-static void /* PRIVATE */
-png_default_warning(png_structp png_ptr, png_const_charp warning_message)
-{
-#ifndef PNG_NO_CONSOLE_IO
-#  ifdef PNG_ERROR_NUMBERS_SUPPORTED
-   if (*warning_message == '#')
-   {
-     int offset;
-     char warning_number[16];
-     for (offset=0; offset<15; offset++)
-     {
-        warning_number[offset]=*(warning_message+offset+1);
-        if (*(warning_message+offset) == ' ')
-            break;
-     }
-     if((offset > 1) && (offset < 15))
-     {
-       warning_number[offset-1]='\0';
-       fprintf(stderr, "libpng warning no. %s: %s\n", warning_number,
-          warning_message+offset);
-     }
-     else
-       fprintf(stderr, "libpng warning: %s\n", warning_message);
-   }
-   else
-#  endif
-     fprintf(stderr, "libpng warning: %s\n", warning_message);
-#else
-   warning_message = warning_message; /* make compiler happy */
-#endif
-   png_ptr = png_ptr; /* make compiler happy */
-}
-#endif /* PNG_NO_WARNINGS */
-
-/* This function is called when the application wants to use another method
- * of handling errors and warnings.  Note that the error function MUST NOT
- * return to the calling routine or serious problems will occur.  The return
- * method used in the default routine calls longjmp(png_ptr->jmpbuf, 1)
- */
-void PNGAPI
-png_set_error_fn(png_structp png_ptr, png_voidp error_ptr,
-   png_error_ptr error_fn, png_error_ptr warning_fn)
-{
-   if (png_ptr == NULL)
-      return;
-   png_ptr->error_ptr = error_ptr;
-   png_ptr->error_fn = error_fn;
-   png_ptr->warning_fn = warning_fn;
-}
-
-
-/* This function returns a pointer to the error_ptr associated with the user
- * functions.  The application should free any memory associated with this
- * pointer before png_write_destroy and png_read_destroy are called.
- */
-png_voidp PNGAPI
-png_get_error_ptr(png_structp png_ptr)
-{
-   if (png_ptr == NULL)
-      return NULL;
-   return ((png_voidp)png_ptr->error_ptr);
-}
-
-
-#ifdef PNG_ERROR_NUMBERS_SUPPORTED
-void PNGAPI
-png_set_strip_error_numbers(png_structp png_ptr, png_uint_32 strip_mode)
-{
-   if(png_ptr != NULL)
-   {
-     png_ptr->flags &=
-       ((~(PNG_FLAG_STRIP_ERROR_NUMBERS|PNG_FLAG_STRIP_ERROR_TEXT))&strip_mode);
-   }
-}
-#endif
-#endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */
+\r
+/* pngerror.c - stub functions for i/o and memory allocation\r
+ *\r
+ * Last changed in libpng 1.4.0 [January 3, 2010]\r
+ * Copyright (c) 1998-2010 Glenn Randers-Pehrson\r
+ * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)\r
+ * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)\r
+ *\r
+ * This code is released under the libpng license.\r
+ * For conditions of distribution and use, see the disclaimer\r
+ * and license in png.h\r
+ *\r
+ * This file provides a location for all error handling.  Users who\r
+ * need special error handling are expected to write replacement functions\r
+ * and use png_set_error_fn() to use those functions.  See the instructions\r
+ * at each function.\r
+ */\r
+\r
+#define PNG_NO_PEDANTIC_WARNINGS\r
+#include "png.h"\r
+#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)\r
+#include "pngpriv.h"\r
+\r
+static void /* PRIVATE */\r
+png_default_error PNGARG((png_structp png_ptr,\r
+  png_const_charp error_message)) PNG_NORETURN;\r
+#ifdef PNG_WARNINGS_SUPPORTED\r
+static void /* PRIVATE */\r
+png_default_warning PNGARG((png_structp png_ptr,\r
+  png_const_charp warning_message));\r
+#endif /* PNG_WARNINGS_SUPPORTED */\r
+\r
+/* This function is called whenever there is a fatal error.  This function\r
+ * should not be changed.  If there is a need to handle errors differently,\r
+ * you should supply a replacement error function and use png_set_error_fn()\r
+ * to replace the error function at run-time.\r
+ */\r
+#ifdef PNG_ERROR_TEXT_SUPPORTED\r
+void PNGAPI\r
+png_error(png_structp png_ptr, png_const_charp error_message)\r
+{\r
+#ifdef PNG_ERROR_NUMBERS_SUPPORTED\r
+   char msg[16];\r
+   if (png_ptr != NULL)\r
+   {\r
+     if (png_ptr->flags&\r
+       (PNG_FLAG_STRIP_ERROR_NUMBERS|PNG_FLAG_STRIP_ERROR_TEXT))\r
+     {\r
+       if (*error_message == PNG_LITERAL_SHARP)\r
+       {\r
+           /* Strip "#nnnn " from beginning of error message. */\r
+           int offset;\r
+           for (offset = 1; offset<15; offset++)\r
+              if (error_message[offset] == ' ')\r
+                  break;\r
+           if (png_ptr->flags&PNG_FLAG_STRIP_ERROR_TEXT)\r
+           {\r
+              int i;\r
+              for (i = 0; i < offset - 1; i++)\r
+                 msg[i] = error_message[i + 1];\r
+              msg[i - 1] = '\0';\r
+              error_message = msg;\r
+           }\r
+           else\r
+              error_message += offset;\r
+       }\r
+       else\r
+       {\r
+           if (png_ptr->flags&PNG_FLAG_STRIP_ERROR_TEXT)\r
+           {\r
+              msg[0] = '0';\r
+              msg[1] = '\0';\r
+              error_message = msg;\r
+           }\r
+       }\r
+     }\r
+   }\r
+#endif\r
+   if (png_ptr != NULL && png_ptr->error_fn != NULL)\r
+      (*(png_ptr->error_fn))(png_ptr, error_message);\r
+\r
+   /* If the custom handler doesn't exist, or if it returns,\r
+      use the default handler, which will not return. */\r
+   png_default_error(png_ptr, error_message);\r
+}\r
+#else\r
+void PNGAPI\r
+png_err(png_structp png_ptr)\r
+{\r
+   if (png_ptr != NULL && png_ptr->error_fn != NULL)\r
+      (*(png_ptr->error_fn))(png_ptr, '\0');\r
+\r
+   /* If the custom handler doesn't exist, or if it returns,\r
+      use the default handler, which will not return. */\r
+   png_default_error(png_ptr, '\0');\r
+}\r
+#endif /* PNG_ERROR_TEXT_SUPPORTED */\r
+\r
+#ifdef PNG_WARNINGS_SUPPORTED\r
+/* This function is called whenever there is a non-fatal error.  This function\r
+ * should not be changed.  If there is a need to handle warnings differently,\r
+ * you should supply a replacement warning function and use\r
+ * png_set_error_fn() to replace the warning function at run-time.\r
+ */\r
+void PNGAPI\r
+png_warning(png_structp png_ptr, png_const_charp warning_message)\r
+{\r
+   int offset = 0;\r
+   if (png_ptr != NULL)\r
+   {\r
+#ifdef PNG_ERROR_NUMBERS_SUPPORTED\r
+   if (png_ptr->flags&\r
+     (PNG_FLAG_STRIP_ERROR_NUMBERS|PNG_FLAG_STRIP_ERROR_TEXT))\r
+#endif\r
+     {\r
+       if (*warning_message == PNG_LITERAL_SHARP)\r
+       {\r
+           for (offset = 1; offset < 15; offset++)\r
+              if (warning_message[offset] == ' ')\r
+                  break;\r
+       }\r
+     }\r
+   }\r
+   if (png_ptr != NULL && png_ptr->warning_fn != NULL)\r
+      (*(png_ptr->warning_fn))(png_ptr, warning_message + offset);\r
+   else\r
+      png_default_warning(png_ptr, warning_message + offset);\r
+}\r
+#endif /* PNG_WARNINGS_SUPPORTED */\r
+\r
+#ifdef PNG_BENIGN_ERRORS_SUPPORTED\r
+void PNGAPI\r
+png_benign_error(png_structp png_ptr, png_const_charp error_message)\r
+{\r
+  if (png_ptr->flags & PNG_FLAG_BENIGN_ERRORS_WARN)\r
+    png_warning(png_ptr, error_message);\r
+  else\r
+    png_error(png_ptr, error_message);\r
+}\r
+#endif\r
+\r
+/* These utilities are used internally to build an error message that relates\r
+ * to the current chunk.  The chunk name comes from png_ptr->chunk_name,\r
+ * this is used to prefix the message.  The message is limited in length\r
+ * to 63 bytes, the name characters are output as hex digits wrapped in []\r
+ * if the character is invalid.\r
+ */\r
+#define isnonalpha(c) ((c) < 65 || (c) > 122 || ((c) > 90 && (c) < 97))\r
+static PNG_CONST char png_digit[16] = {\r
+   '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',\r
+   'A', 'B', 'C', 'D', 'E', 'F'\r
+};\r
+\r
+#define PNG_MAX_ERROR_TEXT 64\r
+#if defined(PNG_WARNINGS_SUPPORTED) || defined(PNG_ERROR_TEXT_SUPPORTED)\r
+static void /* PRIVATE */\r
+png_format_buffer(png_structp png_ptr, png_charp buffer, png_const_charp\r
+   error_message)\r
+{\r
+   int iout = 0, iin = 0;\r
+\r
+   while (iin < 4)\r
+   {\r
+      int c = png_ptr->chunk_name[iin++];\r
+      if (isnonalpha(c))\r
+      {\r
+         buffer[iout++] = PNG_LITERAL_LEFT_SQUARE_BRACKET;\r
+         buffer[iout++] = png_digit[(c & 0xf0) >> 4];\r
+         buffer[iout++] = png_digit[c & 0x0f];\r
+         buffer[iout++] = PNG_LITERAL_RIGHT_SQUARE_BRACKET;\r
+      }\r
+      else\r
+      {\r
+         buffer[iout++] = (png_byte)c;\r
+      }\r
+   }\r
+\r
+   if (error_message == NULL)\r
+      buffer[iout] = '\0';\r
+   else\r
+   {\r
+      buffer[iout++] = ':';\r
+      buffer[iout++] = ' ';\r
+      png_memcpy(buffer + iout, error_message, PNG_MAX_ERROR_TEXT);\r
+      buffer[iout + PNG_MAX_ERROR_TEXT - 1] = '\0';\r
+   }\r
+}\r
+\r
+#ifdef PNG_READ_SUPPORTED\r
+void PNGAPI\r
+png_chunk_error(png_structp png_ptr, png_const_charp error_message)\r
+{\r
+   char msg[18+PNG_MAX_ERROR_TEXT];\r
+   if (png_ptr == NULL)\r
+     png_error(png_ptr, error_message);\r
+   else\r
+   {\r
+     png_format_buffer(png_ptr, msg, error_message);\r
+     png_error(png_ptr, msg);\r
+   }\r
+}\r
+#endif /* PNG_READ_SUPPORTED */\r
+#endif /* PNG_WARNINGS_SUPPORTED || PNG_ERROR_TEXT_SUPPORTED */\r
+\r
+#ifdef PNG_WARNINGS_SUPPORTED\r
+void PNGAPI\r
+png_chunk_warning(png_structp png_ptr, png_const_charp warning_message)\r
+{\r
+   char msg[18+PNG_MAX_ERROR_TEXT];\r
+   if (png_ptr == NULL)\r
+     png_warning(png_ptr, warning_message);\r
+   else\r
+   {\r
+     png_format_buffer(png_ptr, msg, warning_message);\r
+     png_warning(png_ptr, msg);\r
+   }\r
+}\r
+#endif /* PNG_WARNINGS_SUPPORTED */\r
+\r
+#ifdef PNG_READ_SUPPORTED\r
+#ifdef PNG_BENIGN_ERRORS_SUPPORTED\r
+void PNGAPI\r
+png_chunk_benign_error(png_structp png_ptr, png_const_charp error_message)\r
+{\r
+  if (png_ptr->flags & PNG_FLAG_BENIGN_ERRORS_WARN)\r
+    png_chunk_warning(png_ptr, error_message);\r
+  else\r
+    png_chunk_error(png_ptr, error_message);\r
+}\r
+#endif\r
+#endif /* PNG_READ_SUPPORTED */\r
+\r
+#ifdef PNG_SETJMP_SUPPORTED\r
+/* This API only exists if ANSI-C style error handling is used,\r
+ * otherwise it is necessary for png_default_error to be overridden.\r
+ */\r
+jmp_buf* PNGAPI\r
+png_set_longjmp_fn(png_structp png_ptr, png_longjmp_ptr longjmp_fn,\r
+    size_t jmp_buf_size)\r
+{\r
+   if (png_ptr == NULL || jmp_buf_size != png_sizeof(jmp_buf))\r
+      return NULL;\r
+\r
+   png_ptr->longjmp_fn = longjmp_fn;\r
+   return &png_ptr->jmpbuf;\r
+}\r
+#endif\r
+\r
+/* This is the default error handling function.  Note that replacements for\r
+ * this function MUST NOT RETURN, or the program will likely crash.  This\r
+ * function is used by default, or if the program supplies NULL for the\r
+ * error function pointer in png_set_error_fn().\r
+ */\r
+static void /* PRIVATE */\r
+png_default_error(png_structp png_ptr, png_const_charp error_message)\r
+{\r
+#ifdef PNG_CONSOLE_IO_SUPPORTED\r
+#ifdef PNG_ERROR_NUMBERS_SUPPORTED\r
+   if (*error_message == PNG_LITERAL_SHARP)\r
+   {\r
+     /* Strip "#nnnn " from beginning of error message. */\r
+     int offset;\r
+     char error_number[16];\r
+     for (offset = 0; offset<15; offset++)\r
+     {\r
+         error_number[offset] = error_message[offset + 1];\r
+         if (error_message[offset] == ' ')\r
+             break;\r
+     }\r
+     if ((offset > 1) && (offset < 15))\r
+     {\r
+       error_number[offset - 1] = '\0';\r
+       fprintf(stderr, "libpng error no. %s: %s",\r
+          error_number, error_message + offset + 1);\r
+       fprintf(stderr, PNG_STRING_NEWLINE);\r
+     }\r
+     else\r
+     {\r
+       fprintf(stderr, "libpng error: %s, offset=%d",\r
+          error_message, offset);\r
+       fprintf(stderr, PNG_STRING_NEWLINE);\r
+     }\r
+   }\r
+   else\r
+#endif\r
+   {\r
+      fprintf(stderr, "libpng error: %s", error_message);\r
+      fprintf(stderr, PNG_STRING_NEWLINE);\r
+   }\r
+#endif\r
+\r
+#ifdef PNG_SETJMP_SUPPORTED\r
+   if (png_ptr && png_ptr->longjmp_fn)\r
+   {\r
+#  ifdef USE_FAR_KEYWORD\r
+   {\r
+      jmp_buf jmpbuf;\r
+      png_memcpy(jmpbuf, png_ptr->jmpbuf, png_sizeof(jmp_buf));\r
+     png_ptr->longjmp_fn(jmpbuf, 1);\r
+   }\r
+#  else\r
+   png_ptr->longjmp_fn(png_ptr->jmpbuf, 1);\r
+#  endif\r
+   }\r
+#endif\r
+   /* Here if not setjmp support or if png_ptr is null. */\r
+   PNG_ABORT();\r
+#ifndef PNG_CONSOLE_IO_SUPPORTED\r
+   error_message = error_message; /* Make compiler happy */\r
+#endif\r
+}\r
+\r
+#ifdef PNG_WARNINGS_SUPPORTED\r
+/* This function is called when there is a warning, but the library thinks\r
+ * it can continue anyway.  Replacement functions don't have to do anything\r
+ * here if you don't want them to.  In the default configuration, png_ptr is\r
+ * not used, but it is passed in case it may be useful.\r
+ */\r
+static void /* PRIVATE */\r
+png_default_warning(png_structp png_ptr, png_const_charp warning_message)\r
+{\r
+#ifdef PNG_CONSOLE_IO_SUPPORTED\r
+#  ifdef PNG_ERROR_NUMBERS_SUPPORTED\r
+   if (*warning_message == PNG_LITERAL_SHARP)\r
+   {\r
+     int offset;\r
+     char warning_number[16];\r
+     for (offset = 0; offset < 15; offset++)\r
+     {\r
+        warning_number[offset] = warning_message[offset + 1];\r
+        if (warning_message[offset] == ' ')\r
+            break;\r
+     }\r
+     if ((offset > 1) && (offset < 15))\r
+     {\r
+       warning_number[offset + 1] = '\0';\r
+       fprintf(stderr, "libpng warning no. %s: %s",\r
+          warning_number, warning_message + offset);\r
+       fprintf(stderr, PNG_STRING_NEWLINE);\r
+     }\r
+     else\r
+     {\r
+       fprintf(stderr, "libpng warning: %s",\r
+          warning_message);\r
+       fprintf(stderr, PNG_STRING_NEWLINE);\r
+     }\r
+   }\r
+   else\r
+#  endif\r
+   {\r
+     fprintf(stderr, "libpng warning: %s", warning_message);\r
+     fprintf(stderr, PNG_STRING_NEWLINE);\r
+   }\r
+#else\r
+   warning_message = warning_message; /* Make compiler happy */\r
+#endif\r
+   png_ptr = png_ptr; /* Make compiler happy */\r
+}\r
+#endif /* PNG_WARNINGS_SUPPORTED */\r
+\r
+/* This function is called when the application wants to use another method\r
+ * of handling errors and warnings.  Note that the error function MUST NOT\r
+ * return to the calling routine or serious problems will occur.  The return\r
+ * method used in the default routine calls longjmp(png_ptr->jmpbuf, 1)\r
+ */\r
+void PNGAPI\r
+png_set_error_fn(png_structp png_ptr, png_voidp error_ptr,\r
+   png_error_ptr error_fn, png_error_ptr warning_fn)\r
+{\r
+   if (png_ptr == NULL)\r
+      return;\r
+   png_ptr->error_ptr = error_ptr;\r
+   png_ptr->error_fn = error_fn;\r
+   png_ptr->warning_fn = warning_fn;\r
+}\r
+\r
+\r
+/* This function returns a pointer to the error_ptr associated with the user\r
+ * functions.  The application should free any memory associated with this\r
+ * pointer before png_write_destroy and png_read_destroy are called.\r
+ */\r
+png_voidp PNGAPI\r
+png_get_error_ptr(png_structp png_ptr)\r
+{\r
+   if (png_ptr == NULL)\r
+      return NULL;\r
+   return ((png_voidp)png_ptr->error_ptr);\r
+}\r
+\r
+\r
+#ifdef PNG_ERROR_NUMBERS_SUPPORTED\r
+void PNGAPI\r
+png_set_strip_error_numbers(png_structp png_ptr, png_uint_32 strip_mode)\r
+{\r
+   if (png_ptr != NULL)\r
+   {\r
+     png_ptr->flags &=\r
+       ((~(PNG_FLAG_STRIP_ERROR_NUMBERS|PNG_FLAG_STRIP_ERROR_TEXT))&strip_mode);\r
+   }\r
+}\r
+#endif\r
+#endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */\r
diff --git a/3rdparty/libpng/pnggccrd.c b/3rdparty/libpng/pnggccrd.c
deleted file mode 100644 (file)
index e61523e..0000000
+++ /dev/null
@@ -1,103 +0,0 @@
-/* pnggccrd.c was removed from libpng-1.2.20. */
-
-/* This code snippet is for use by configure's compilation test. */
-
-#if (!defined _MSC_VER) && \
-    defined(PNG_ASSEMBLER_CODE_SUPPORTED) && \
-    defined(PNG_MMX_CODE_SUPPORTED)
-
-int PNGAPI png_dummy_mmx_support(void);
-
-static int _mmx_supported = 2; // 0: no MMX; 1: MMX supported; 2: not tested
-
-int PNGAPI
-png_dummy_mmx_support(void) __attribute__((noinline));
-
-int PNGAPI
-png_dummy_mmx_support(void)
-{
-   int result;
-#if defined(PNG_MMX_CODE_SUPPORTED)  // superfluous, but what the heck
-    __asm__ __volatile__ (
-#if defined(__x86_64__)
-        "pushq %%rbx          \n\t"  // rbx gets clobbered by CPUID instruction
-        "pushq %%rcx          \n\t"  // so does rcx...
-        "pushq %%rdx          \n\t"  // ...and rdx (but rcx & rdx safe on Linux)
-        "pushfq               \n\t"  // save Eflag to stack
-        "popq %%rax           \n\t"  // get Eflag from stack into rax
-        "movq %%rax, %%rcx    \n\t"  // make another copy of Eflag in rcx
-        "xorl $0x200000, %%eax \n\t" // toggle ID bit in Eflag (i.e., bit 21)
-        "pushq %%rax          \n\t"  // save modified Eflag back to stack
-        "popfq                \n\t"  // restore modified value to Eflag reg
-        "pushfq               \n\t"  // save Eflag to stack
-        "popq %%rax           \n\t"  // get Eflag from stack
-        "pushq %%rcx          \n\t"  // save original Eflag to stack
-        "popfq                \n\t"  // restore original Eflag
-#else
-        "pushl %%ebx          \n\t"  // ebx gets clobbered by CPUID instruction
-        "pushl %%ecx          \n\t"  // so does ecx...
-        "pushl %%edx          \n\t"  // ...and edx (but ecx & edx safe on Linux)
-        "pushfl               \n\t"  // save Eflag to stack
-        "popl %%eax           \n\t"  // get Eflag from stack into eax
-        "movl %%eax, %%ecx    \n\t"  // make another copy of Eflag in ecx
-        "xorl $0x200000, %%eax \n\t" // toggle ID bit in Eflag (i.e., bit 21)
-        "pushl %%eax          \n\t"  // save modified Eflag back to stack
-        "popfl                \n\t"  // restore modified value to Eflag reg
-        "pushfl               \n\t"  // save Eflag to stack
-        "popl %%eax           \n\t"  // get Eflag from stack
-        "pushl %%ecx          \n\t"  // save original Eflag to stack
-        "popfl                \n\t"  // restore original Eflag
-#endif
-        "xorl %%ecx, %%eax    \n\t"  // compare new Eflag with original Eflag
-        "jz 0f                \n\t"  // if same, CPUID instr. is not supported
-
-        "xorl %%eax, %%eax    \n\t"  // set eax to zero
-//      ".byte  0x0f, 0xa2    \n\t"  // CPUID instruction (two-byte opcode)
-        "cpuid                \n\t"  // get the CPU identification info
-        "cmpl $1, %%eax       \n\t"  // make sure eax return non-zero value
-        "jl 0f                \n\t"  // if eax is zero, MMX is not supported
-
-        "xorl %%eax, %%eax    \n\t"  // set eax to zero and...
-        "incl %%eax           \n\t"  // ...increment eax to 1.  This pair is
-                                     // faster than the instruction "mov eax, 1"
-        "cpuid                \n\t"  // get the CPU identification info again
-        "andl $0x800000, %%edx \n\t" // mask out all bits but MMX bit (23)
-        "cmpl $0, %%edx       \n\t"  // 0 = MMX not supported
-        "jz 0f                \n\t"  // non-zero = yes, MMX IS supported
-
-        "movl $1, %%eax       \n\t"  // set return value to 1
-        "jmp  1f              \n\t"  // DONE:  have MMX support
-
-    "0:                       \n\t"  // .NOT_SUPPORTED: target label for jump instructions
-        "movl $0, %%eax       \n\t"  // set return value to 0
-    "1:                       \n\t"  // .RETURN: target label for jump instructions
-#if defined(__x86_64__)
-        "popq %%rdx           \n\t"  // restore rdx
-        "popq %%rcx           \n\t"  // restore rcx
-        "popq %%rbx           \n\t"  // restore rbx
-#else
-        "popl %%edx           \n\t"  // restore edx
-        "popl %%ecx           \n\t"  // restore ecx
-        "popl %%ebx           \n\t"  // restore ebx
-#endif
-
-//      "ret                  \n\t"  // DONE:  no MMX support
-                                     // (fall through to standard C "ret")
-
-        : "=a" (result)              // output list
-
-        :                            // any variables used on input (none)
-
-                                     // no clobber list
-//      , "%ebx", "%ecx", "%edx"     // GRR:  we handle these manually
-//      , "memory"   // if write to a variable gcc thought was in a reg
-//      , "cc"       // "condition codes" (flag bits)
-    );
-    _mmx_supported = result;
-#else
-    _mmx_supported = 0;
-#endif /* PNG_MMX_CODE_SUPPORTED */
-
-    return _mmx_supported;
-}
-#endif
index a0e90bb..2a43a4d 100644 (file)
-
-/* pngget.c - retrieval of values from info struct
- *
- * Last changed in libpng 1.2.15 January 5, 2007
- * For conditions of distribution and use, see copyright notice in png.h
- * Copyright (c) 1998-2007 Glenn Randers-Pehrson
- * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
- * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
- */
-
-#define PNG_INTERNAL
-#include "png.h"
-
-#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
-
-png_uint_32 PNGAPI
-png_get_valid(png_structp png_ptr, png_infop info_ptr, png_uint_32 flag)
-{
-   if (png_ptr != NULL && info_ptr != NULL)
-      return(info_ptr->valid & flag);
-   else
-      return(0);
-}
-
-png_uint_32 PNGAPI
-png_get_rowbytes(png_structp png_ptr, png_infop info_ptr)
-{
-   if (png_ptr != NULL && info_ptr != NULL)
-      return(info_ptr->rowbytes);
-   else
-      return(0);
-}
-
-#if defined(PNG_INFO_IMAGE_SUPPORTED)
-png_bytepp PNGAPI
-png_get_rows(png_structp png_ptr, png_infop info_ptr)
-{
-   if (png_ptr != NULL && info_ptr != NULL)
-      return(info_ptr->row_pointers);
-   else
-      return(0);
-}
-#endif
-
-#ifdef PNG_EASY_ACCESS_SUPPORTED
-/* easy access to info, added in libpng-0.99 */
-png_uint_32 PNGAPI
-png_get_image_width(png_structp png_ptr, png_infop info_ptr)
-{
-   if (png_ptr != NULL && info_ptr != NULL)
-   {
-      return info_ptr->width;
-   }
-   return (0);
-}
-
-png_uint_32 PNGAPI
-png_get_image_height(png_structp png_ptr, png_infop info_ptr)
-{
-   if (png_ptr != NULL && info_ptr != NULL)
-   {
-      return info_ptr->height;
-   }
-   return (0);
-}
-
-png_byte PNGAPI
-png_get_bit_depth(png_structp png_ptr, png_infop info_ptr)
-{
-   if (png_ptr != NULL && info_ptr != NULL)
-   {
-      return info_ptr->bit_depth;
-   }
-   return (0);
-}
-
-png_byte PNGAPI
-png_get_color_type(png_structp png_ptr, png_infop info_ptr)
-{
-   if (png_ptr != NULL && info_ptr != NULL)
-   {
-      return info_ptr->color_type;
-   }
-   return (0);
-}
-
-png_byte PNGAPI
-png_get_filter_type(png_structp png_ptr, png_infop info_ptr)
-{
-   if (png_ptr != NULL && info_ptr != NULL)
-   {
-      return info_ptr->filter_type;
-   }
-   return (0);
-}
-
-png_byte PNGAPI
-png_get_interlace_type(png_structp png_ptr, png_infop info_ptr)
-{
-   if (png_ptr != NULL && info_ptr != NULL)
-   {
-      return info_ptr->interlace_type;
-   }
-   return (0);
-}
-
-png_byte PNGAPI
-png_get_compression_type(png_structp png_ptr, png_infop info_ptr)
-{
-   if (png_ptr != NULL && info_ptr != NULL)
-   {
-      return info_ptr->compression_type;
-   }
-   return (0);
-}
-
-png_uint_32 PNGAPI
-png_get_x_pixels_per_meter(png_structp png_ptr, png_infop info_ptr)
-{
-   if (png_ptr != NULL && info_ptr != NULL)
-#if defined(PNG_pHYs_SUPPORTED)
-   if (info_ptr->valid & PNG_INFO_pHYs)
-   {
-      png_debug1(1, "in %s retrieval function\n", "png_get_x_pixels_per_meter");
-      if(info_ptr->phys_unit_type != PNG_RESOLUTION_METER)
-          return (0);
-      else return (info_ptr->x_pixels_per_unit);
-   }
-#else
-   return (0);
-#endif
-   return (0);
-}
-
-png_uint_32 PNGAPI
-png_get_y_pixels_per_meter(png_structp png_ptr, png_infop info_ptr)
-{
-   if (png_ptr != NULL && info_ptr != NULL)
-#if defined(PNG_pHYs_SUPPORTED)
-   if (info_ptr->valid & PNG_INFO_pHYs)
-   {
-      png_debug1(1, "in %s retrieval function\n", "png_get_y_pixels_per_meter");
-      if(info_ptr->phys_unit_type != PNG_RESOLUTION_METER)
-          return (0);
-      else return (info_ptr->y_pixels_per_unit);
-   }
-#else
-   return (0);
-#endif
-   return (0);
-}
-
-png_uint_32 PNGAPI
-png_get_pixels_per_meter(png_structp png_ptr, png_infop info_ptr)
-{
-   if (png_ptr != NULL && info_ptr != NULL)
-#if defined(PNG_pHYs_SUPPORTED)
-   if (info_ptr->valid & PNG_INFO_pHYs)
-   {
-      png_debug1(1, "in %s retrieval function\n", "png_get_pixels_per_meter");
-      if(info_ptr->phys_unit_type != PNG_RESOLUTION_METER ||
-         info_ptr->x_pixels_per_unit != info_ptr->y_pixels_per_unit)
-          return (0);
-      else return (info_ptr->x_pixels_per_unit);
-   }
-#else
-   return (0);
-#endif
-   return (0);
-}
-
-#ifdef PNG_FLOATING_POINT_SUPPORTED
-float PNGAPI
-png_get_pixel_aspect_ratio(png_structp png_ptr, png_infop info_ptr)
-   {
-   if (png_ptr != NULL && info_ptr != NULL)
-#if defined(PNG_pHYs_SUPPORTED)
-   if (info_ptr->valid & PNG_INFO_pHYs)
-   {
-      png_debug1(1, "in %s retrieval function\n", "png_get_aspect_ratio");
-      if (info_ptr->x_pixels_per_unit == 0)
-         return ((float)0.0);
-      else
-         return ((float)((float)info_ptr->y_pixels_per_unit
-            /(float)info_ptr->x_pixels_per_unit));
-   }
-#else
-   return (0.0);
-#endif
-   return ((float)0.0);
-}
-#endif
-
-png_int_32 PNGAPI
-png_get_x_offset_microns(png_structp png_ptr, png_infop info_ptr)
-{
-   if (png_ptr != NULL && info_ptr != NULL)
-#if defined(PNG_oFFs_SUPPORTED)
-   if (info_ptr->valid & PNG_INFO_oFFs)
-   {
-      png_debug1(1, "in %s retrieval function\n", "png_get_x_offset_microns");
-      if(info_ptr->offset_unit_type != PNG_OFFSET_MICROMETER)
-          return (0);
-      else return (info_ptr->x_offset);
-   }
-#else
-   return (0);
-#endif
-   return (0);
-}
-
-png_int_32 PNGAPI
-png_get_y_offset_microns(png_structp png_ptr, png_infop info_ptr)
-{
-   if (png_ptr != NULL && info_ptr != NULL)
-#if defined(PNG_oFFs_SUPPORTED)
-   if (info_ptr->valid & PNG_INFO_oFFs)
-   {
-      png_debug1(1, "in %s retrieval function\n", "png_get_y_offset_microns");
-      if(info_ptr->offset_unit_type != PNG_OFFSET_MICROMETER)
-          return (0);
-      else return (info_ptr->y_offset);
-   }
-#else
-   return (0);
-#endif
-   return (0);
-}
-
-png_int_32 PNGAPI
-png_get_x_offset_pixels(png_structp png_ptr, png_infop info_ptr)
-{
-   if (png_ptr != NULL && info_ptr != NULL)
-#if defined(PNG_oFFs_SUPPORTED)
-   if (info_ptr->valid & PNG_INFO_oFFs)
-   {
-      png_debug1(1, "in %s retrieval function\n", "png_get_x_offset_microns");
-      if(info_ptr->offset_unit_type != PNG_OFFSET_PIXEL)
-          return (0);
-      else return (info_ptr->x_offset);
-   }
-#else
-   return (0);
-#endif
-   return (0);
-}
-
-png_int_32 PNGAPI
-png_get_y_offset_pixels(png_structp png_ptr, png_infop info_ptr)
-{
-   if (png_ptr != NULL && info_ptr != NULL)
-#if defined(PNG_oFFs_SUPPORTED)
-   if (info_ptr->valid & PNG_INFO_oFFs)
-   {
-      png_debug1(1, "in %s retrieval function\n", "png_get_y_offset_microns");
-      if(info_ptr->offset_unit_type != PNG_OFFSET_PIXEL)
-          return (0);
-      else return (info_ptr->y_offset);
-   }
-#else
-   return (0);
-#endif
-   return (0);
-}
-
-#if defined(PNG_INCH_CONVERSIONS) && defined(PNG_FLOATING_POINT_SUPPORTED)
-png_uint_32 PNGAPI
-png_get_pixels_per_inch(png_structp png_ptr, png_infop info_ptr)
-{
-   return ((png_uint_32)((float)png_get_pixels_per_meter(png_ptr, info_ptr)
-     *.0254 +.5));
-}
-
-png_uint_32 PNGAPI
-png_get_x_pixels_per_inch(png_structp png_ptr, png_infop info_ptr)
-{
-   return ((png_uint_32)((float)png_get_x_pixels_per_meter(png_ptr, info_ptr)
-     *.0254 +.5));
-}
-
-png_uint_32 PNGAPI
-png_get_y_pixels_per_inch(png_structp png_ptr, png_infop info_ptr)
-{
-   return ((png_uint_32)((float)png_get_y_pixels_per_meter(png_ptr, info_ptr)
-     *.0254 +.5));
-}
-
-float PNGAPI
-png_get_x_offset_inches(png_structp png_ptr, png_infop info_ptr)
-{
-   return ((float)png_get_x_offset_microns(png_ptr, info_ptr)
-     *.00003937);
-}
-
-float PNGAPI
-png_get_y_offset_inches(png_structp png_ptr, png_infop info_ptr)
-{
-   return ((float)png_get_y_offset_microns(png_ptr, info_ptr)
-     *.00003937);
-}
-
-#if defined(PNG_pHYs_SUPPORTED)
-png_uint_32 PNGAPI
-png_get_pHYs_dpi(png_structp png_ptr, png_infop info_ptr,
-   png_uint_32 *res_x, png_uint_32 *res_y, int *unit_type)
-{
-   png_uint_32 retval = 0;
-
-   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_pHYs))
-   {
-      png_debug1(1, "in %s retrieval function\n", "pHYs");
-      if (res_x != NULL)
-      {
-         *res_x = info_ptr->x_pixels_per_unit;
-         retval |= PNG_INFO_pHYs;
-      }
-      if (res_y != NULL)
-      {
-         *res_y = info_ptr->y_pixels_per_unit;
-         retval |= PNG_INFO_pHYs;
-      }
-      if (unit_type != NULL)
-      {
-         *unit_type = (int)info_ptr->phys_unit_type;
-         retval |= PNG_INFO_pHYs;
-         if(*unit_type == 1)
-         {
-            if (res_x != NULL) *res_x = (png_uint_32)(*res_x * .0254 + .50);
-            if (res_y != NULL) *res_y = (png_uint_32)(*res_y * .0254 + .50);
-         }
-      }
-   }
-   return (retval);
-}
-#endif /* PNG_pHYs_SUPPORTED */
-#endif  /* PNG_INCH_CONVERSIONS && PNG_FLOATING_POINT_SUPPORTED */
-
-/* png_get_channels really belongs in here, too, but it's been around longer */
-
-#endif  /* PNG_EASY_ACCESS_SUPPORTED */
-
-png_byte PNGAPI
-png_get_channels(png_structp png_ptr, png_infop info_ptr)
-{
-   if (png_ptr != NULL && info_ptr != NULL)
-      return(info_ptr->channels);
-   else
-      return (0);
-}
-
-png_bytep PNGAPI
-png_get_signature(png_structp png_ptr, png_infop info_ptr)
-{
-   if (png_ptr != NULL && info_ptr != NULL)
-      return(info_ptr->signature);
-   else
-      return (NULL);
-}
-
-#if defined(PNG_bKGD_SUPPORTED)
-png_uint_32 PNGAPI
-png_get_bKGD(png_structp png_ptr, png_infop info_ptr,
-   png_color_16p *background)
-{
-   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_bKGD)
-      && background != NULL)
-   {
-      png_debug1(1, "in %s retrieval function\n", "bKGD");
-      *background = &(info_ptr->background);
-      return (PNG_INFO_bKGD);
-   }
-   return (0);
-}
-#endif
-
-#if defined(PNG_cHRM_SUPPORTED)
-#ifdef PNG_FLOATING_POINT_SUPPORTED
-png_uint_32 PNGAPI
-png_get_cHRM(png_structp png_ptr, png_infop info_ptr,
-   double *white_x, double *white_y, double *red_x, double *red_y,
-   double *green_x, double *green_y, double *blue_x, double *blue_y)
-{
-   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_cHRM))
-   {
-      png_debug1(1, "in %s retrieval function\n", "cHRM");
-      if (white_x != NULL)
-         *white_x = (double)info_ptr->x_white;
-      if (white_y != NULL)
-         *white_y = (double)info_ptr->y_white;
-      if (red_x != NULL)
-         *red_x = (double)info_ptr->x_red;
-      if (red_y != NULL)
-         *red_y = (double)info_ptr->y_red;
-      if (green_x != NULL)
-         *green_x = (double)info_ptr->x_green;
-      if (green_y != NULL)
-         *green_y = (double)info_ptr->y_green;
-      if (blue_x != NULL)
-         *blue_x = (double)info_ptr->x_blue;
-      if (blue_y != NULL)
-         *blue_y = (double)info_ptr->y_blue;
-      return (PNG_INFO_cHRM);
-   }
-   return (0);
-}
-#endif
-#ifdef PNG_FIXED_POINT_SUPPORTED
-png_uint_32 PNGAPI
-png_get_cHRM_fixed(png_structp png_ptr, png_infop info_ptr,
-   png_fixed_point *white_x, png_fixed_point *white_y, png_fixed_point *red_x,
-   png_fixed_point *red_y, png_fixed_point *green_x, png_fixed_point *green_y,
-   png_fixed_point *blue_x, png_fixed_point *blue_y)
-{
-   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_cHRM))
-   {
-      png_debug1(1, "in %s retrieval function\n", "cHRM");
-      if (white_x != NULL)
-         *white_x = info_ptr->int_x_white;
-      if (white_y != NULL)
-         *white_y = info_ptr->int_y_white;
-      if (red_x != NULL)
-         *red_x = info_ptr->int_x_red;
-      if (red_y != NULL)
-         *red_y = info_ptr->int_y_red;
-      if (green_x != NULL)
-         *green_x = info_ptr->int_x_green;
-      if (green_y != NULL)
-         *green_y = info_ptr->int_y_green;
-      if (blue_x != NULL)
-         *blue_x = info_ptr->int_x_blue;
-      if (blue_y != NULL)
-         *blue_y = info_ptr->int_y_blue;
-      return (PNG_INFO_cHRM);
-   }
-   return (0);
-}
-#endif
-#endif
-
-#if defined(PNG_gAMA_SUPPORTED)
-#ifdef PNG_FLOATING_POINT_SUPPORTED
-png_uint_32 PNGAPI
-png_get_gAMA(png_structp png_ptr, png_infop info_ptr, double *file_gamma)
-{
-   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_gAMA)
-      && file_gamma != NULL)
-   {
-      png_debug1(1, "in %s retrieval function\n", "gAMA");
-      *file_gamma = (double)info_ptr->gamma;
-      return (PNG_INFO_gAMA);
-   }
-   return (0);
-}
-#endif
-#ifdef PNG_FIXED_POINT_SUPPORTED
-png_uint_32 PNGAPI
-png_get_gAMA_fixed(png_structp png_ptr, png_infop info_ptr,
-    png_fixed_point *int_file_gamma)
-{
-   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_gAMA)
-      && int_file_gamma != NULL)
-   {
-      png_debug1(1, "in %s retrieval function\n", "gAMA");
-      *int_file_gamma = info_ptr->int_gamma;
-      return (PNG_INFO_gAMA);
-   }
-   return (0);
-}
-#endif
-#endif
-
-#if defined(PNG_sRGB_SUPPORTED)
-png_uint_32 PNGAPI
-png_get_sRGB(png_structp png_ptr, png_infop info_ptr, int *file_srgb_intent)
-{
-   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_sRGB)
-      && file_srgb_intent != NULL)
-   {
-      png_debug1(1, "in %s retrieval function\n", "sRGB");
-      *file_srgb_intent = (int)info_ptr->srgb_intent;
-      return (PNG_INFO_sRGB);
-   }
-   return (0);
-}
-#endif
-
-#if defined(PNG_iCCP_SUPPORTED)
-png_uint_32 PNGAPI
-png_get_iCCP(png_structp png_ptr, png_infop info_ptr,
-             png_charpp name, int *compression_type,
-             png_charpp profile, png_uint_32 *proflen)
-{
-   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_iCCP)
-      && name != NULL && profile != NULL && proflen != NULL)
-   {
-      png_debug1(1, "in %s retrieval function\n", "iCCP");
-      *name = info_ptr->iccp_name;
-      *profile = info_ptr->iccp_profile;
-      /* compression_type is a dummy so the API won't have to change
-         if we introduce multiple compression types later. */
-      *proflen = (int)info_ptr->iccp_proflen;
-      *compression_type = (int)info_ptr->iccp_compression;
-      return (PNG_INFO_iCCP);
-   }
-   return (0);
-}
-#endif
-
-#if defined(PNG_sPLT_SUPPORTED)
-png_uint_32 PNGAPI
-png_get_sPLT(png_structp png_ptr, png_infop info_ptr,
-             png_sPLT_tpp spalettes)
-{
-   if (png_ptr != NULL && info_ptr != NULL && spalettes != NULL)
-   {
-     *spalettes = info_ptr->splt_palettes;
-     return ((png_uint_32)info_ptr->splt_palettes_num);
-   }
-   return (0);
-}
-#endif
-
-#if defined(PNG_hIST_SUPPORTED)
-png_uint_32 PNGAPI
-png_get_hIST(png_structp png_ptr, png_infop info_ptr, png_uint_16p *hist)
-{
-   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_hIST)
-      && hist != NULL)
-   {
-      png_debug1(1, "in %s retrieval function\n", "hIST");
-      *hist = info_ptr->hist;
-      return (PNG_INFO_hIST);
-   }
-   return (0);
-}
-#endif
-
-png_uint_32 PNGAPI
-png_get_IHDR(png_structp png_ptr, png_infop info_ptr,
-   png_uint_32 *width, png_uint_32 *height, int *bit_depth,
-   int *color_type, int *interlace_type, int *compression_type,
-   int *filter_type)
-
-{
-   if (png_ptr != NULL && info_ptr != NULL && width != NULL && height != NULL &&
-      bit_depth != NULL && color_type != NULL)
-   {
-      png_debug1(1, "in %s retrieval function\n", "IHDR");
-      *width = info_ptr->width;
-      *height = info_ptr->height;
-      *bit_depth = info_ptr->bit_depth;
-      if (info_ptr->bit_depth < 1 || info_ptr->bit_depth > 16)
-        png_error(png_ptr, "Invalid bit depth");
-      *color_type = info_ptr->color_type;
-      if (info_ptr->color_type > 6)
-        png_error(png_ptr, "Invalid color type");
-      if (compression_type != NULL)
-         *compression_type = info_ptr->compression_type;
-      if (filter_type != NULL)
-         *filter_type = info_ptr->filter_type;
-      if (interlace_type != NULL)
-         *interlace_type = info_ptr->interlace_type;
-
-      /* check for potential overflow of rowbytes */
-      if (*width == 0 || *width > PNG_UINT_31_MAX)
-        png_error(png_ptr, "Invalid image width");
-      if (*height == 0 || *height > PNG_UINT_31_MAX)
-        png_error(png_ptr, "Invalid image height");
-      if (info_ptr->width > (PNG_UINT_32_MAX
-                 >> 3)      /* 8-byte RGBA pixels */
-                 - 64       /* bigrowbuf hack */
-                 - 1        /* filter byte */
-                 - 7*8      /* rounding of width to multiple of 8 pixels */
-                 - 8)       /* extra max_pixel_depth pad */
-      {
-         png_warning(png_ptr,
-            "Width too large for libpng to process image data.");
-      }
-      return (1);
-   }
-   return (0);
-}
-
-#if defined(PNG_oFFs_SUPPORTED)
-png_uint_32 PNGAPI
-png_get_oFFs(png_structp png_ptr, png_infop info_ptr,
-   png_int_32 *offset_x, png_int_32 *offset_y, int *unit_type)
-{
-   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_oFFs)
-      && offset_x != NULL && offset_y != NULL && unit_type != NULL)
-   {
-      png_debug1(1, "in %s retrieval function\n", "oFFs");
-      *offset_x = info_ptr->x_offset;
-      *offset_y = info_ptr->y_offset;
-      *unit_type = (int)info_ptr->offset_unit_type;
-      return (PNG_INFO_oFFs);
-   }
-   return (0);
-}
-#endif
-
-#if defined(PNG_pCAL_SUPPORTED)
-png_uint_32 PNGAPI
-png_get_pCAL(png_structp png_ptr, png_infop info_ptr,
-   png_charp *purpose, png_int_32 *X0, png_int_32 *X1, int *type, int *nparams,
-   png_charp *units, png_charpp *params)
-{
-   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_pCAL)
-      && purpose != NULL && X0 != NULL && X1 != NULL && type != NULL &&
-      nparams != NULL && units != NULL && params != NULL)
-   {
-      png_debug1(1, "in %s retrieval function\n", "pCAL");
-      *purpose = info_ptr->pcal_purpose;
-      *X0 = info_ptr->pcal_X0;
-      *X1 = info_ptr->pcal_X1;
-      *type = (int)info_ptr->pcal_type;
-      *nparams = (int)info_ptr->pcal_nparams;
-      *units = info_ptr->pcal_units;
-      *params = info_ptr->pcal_params;
-      return (PNG_INFO_pCAL);
-   }
-   return (0);
-}
-#endif
-
-#if defined(PNG_sCAL_SUPPORTED)
-#ifdef PNG_FLOATING_POINT_SUPPORTED
-png_uint_32 PNGAPI
-png_get_sCAL(png_structp png_ptr, png_infop info_ptr,
-             int *unit, double *width, double *height)
-{
-    if (png_ptr != NULL && info_ptr != NULL &&
-       (info_ptr->valid & PNG_INFO_sCAL))
-    {
-        *unit = info_ptr->scal_unit;
-        *width = info_ptr->scal_pixel_width;
-        *height = info_ptr->scal_pixel_height;
-        return (PNG_INFO_sCAL);
-    }
-    return(0);
-}
-#else
-#ifdef PNG_FIXED_POINT_SUPPORTED
-png_uint_32 PNGAPI
-png_get_sCAL_s(png_structp png_ptr, png_infop info_ptr,
-             int *unit, png_charpp width, png_charpp height)
-{
-    if (png_ptr != NULL && info_ptr != NULL &&
-       (info_ptr->valid & PNG_INFO_sCAL))
-    {
-        *unit = info_ptr->scal_unit;
-        *width = info_ptr->scal_s_width;
-        *height = info_ptr->scal_s_height;
-        return (PNG_INFO_sCAL);
-    }
-    return(0);
-}
-#endif
-#endif
-#endif
-
-#if defined(PNG_pHYs_SUPPORTED)
-png_uint_32 PNGAPI
-png_get_pHYs(png_structp png_ptr, png_infop info_ptr,
-   png_uint_32 *res_x, png_uint_32 *res_y, int *unit_type)
-{
-   png_uint_32 retval = 0;
-
-   if (png_ptr != NULL && info_ptr != NULL &&
-      (info_ptr->valid & PNG_INFO_pHYs))
-   {
-      png_debug1(1, "in %s retrieval function\n", "pHYs");
-      if (res_x != NULL)
-      {
-         *res_x = info_ptr->x_pixels_per_unit;
-         retval |= PNG_INFO_pHYs;
-      }
-      if (res_y != NULL)
-      {
-         *res_y = info_ptr->y_pixels_per_unit;
-         retval |= PNG_INFO_pHYs;
-      }
-      if (unit_type != NULL)
-      {
-         *unit_type = (int)info_ptr->phys_unit_type;
-         retval |= PNG_INFO_pHYs;
-      }
-   }
-   return (retval);
-}
-#endif
-
-png_uint_32 PNGAPI
-png_get_PLTE(png_structp png_ptr, png_infop info_ptr, png_colorp *palette,
-   int *num_palette)
-{
-   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_PLTE)
-       && palette != NULL)
-   {
-      png_debug1(1, "in %s retrieval function\n", "PLTE");
-      *palette = info_ptr->palette;
-      *num_palette = info_ptr->num_palette;
-      png_debug1(3, "num_palette = %d\n", *num_palette);
-      return (PNG_INFO_PLTE);
-   }
-   return (0);
-}
-
-#if defined(PNG_sBIT_SUPPORTED)
-png_uint_32 PNGAPI
-png_get_sBIT(png_structp png_ptr, png_infop info_ptr, png_color_8p *sig_bit)
-{
-   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_sBIT)
-      && sig_bit != NULL)
-   {
-      png_debug1(1, "in %s retrieval function\n", "sBIT");
-      *sig_bit = &(info_ptr->sig_bit);
-      return (PNG_INFO_sBIT);
-   }
-   return (0);
-}
-#endif
-
-#if defined(PNG_TEXT_SUPPORTED)
-png_uint_32 PNGAPI
-png_get_text(png_structp png_ptr, png_infop info_ptr, png_textp *text_ptr,
-   int *num_text)
-{
-   if (png_ptr != NULL && info_ptr != NULL && info_ptr->num_text > 0)
-   {
-      png_debug1(1, "in %s retrieval function\n",
-         (png_ptr->chunk_name[0] == '\0' ? "text"
-             : (png_const_charp)png_ptr->chunk_name));
-      if (text_ptr != NULL)
-         *text_ptr = info_ptr->text;
-      if (num_text != NULL)
-         *num_text = info_ptr->num_text;
-      return ((png_uint_32)info_ptr->num_text);
-   }
-   if (num_text != NULL)
-     *num_text = 0;
-   return(0);
-}
-#endif
-
-#if defined(PNG_tIME_SUPPORTED)
-png_uint_32 PNGAPI
-png_get_tIME(png_structp png_ptr, png_infop info_ptr, png_timep *mod_time)
-{
-   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_tIME)
-       && mod_time != NULL)
-   {
-      png_debug1(1, "in %s retrieval function\n", "tIME");
-      *mod_time = &(info_ptr->mod_time);
-      return (PNG_INFO_tIME);
-   }
-   return (0);
-}
-#endif
-
-#if defined(PNG_tRNS_SUPPORTED)
-png_uint_32 PNGAPI
-png_get_tRNS(png_structp png_ptr, png_infop info_ptr,
-   png_bytep *trans, int *num_trans, png_color_16p *trans_values)
-{
-   png_uint_32 retval = 0;
-   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS))
-   {
-      png_debug1(1, "in %s retrieval function\n", "tRNS");
-      if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
-      {
-          if (trans != NULL)
-          {
-             *trans = info_ptr->trans;
-             retval |= PNG_INFO_tRNS;
-          }
-          if (trans_values != NULL)
-             *trans_values = &(info_ptr->trans_values);
-      }
-      else /* if (info_ptr->color_type != PNG_COLOR_TYPE_PALETTE) */
-      {
-          if (trans_values != NULL)
-          {
-             *trans_values = &(info_ptr->trans_values);
-             retval |= PNG_INFO_tRNS;
-          }
-          if(trans != NULL)
-             *trans = NULL;
-      }
-      if(num_trans != NULL)
-      {
-         *num_trans = info_ptr->num_trans;
-         retval |= PNG_INFO_tRNS;
-      }
-   }
-   return (retval);
-}
-#endif
-
-#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
-png_uint_32 PNGAPI
-png_get_unknown_chunks(png_structp png_ptr, png_infop info_ptr,
-             png_unknown_chunkpp unknowns)
-{
-   if (png_ptr != NULL && info_ptr != NULL && unknowns != NULL)
-   {
-     *unknowns = info_ptr->unknown_chunks;
-     return ((png_uint_32)info_ptr->unknown_chunks_num);
-   }
-   return (0);
-}
-#endif
-
-#if defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
-png_byte PNGAPI
-png_get_rgb_to_gray_status (png_structp png_ptr)
-{
-   return (png_byte)(png_ptr? png_ptr->rgb_to_gray_status : 0);
-}
-#endif
-
-#if defined(PNG_USER_CHUNKS_SUPPORTED)
-png_voidp PNGAPI
-png_get_user_chunk_ptr(png_structp png_ptr)
-{
-   return (png_ptr? png_ptr->user_chunk_ptr : NULL);
-}
-#endif
-
-#ifdef PNG_WRITE_SUPPORTED
-png_uint_32 PNGAPI
-png_get_compression_buffer_size(png_structp png_ptr)
-{
-   return (png_uint_32)(png_ptr? png_ptr->zbuf_size : 0L);
-}
-#endif
-
-#ifdef PNG_ASSEMBLER_CODE_SUPPORTED
-#ifndef PNG_1_0_X
-/* this function was added to libpng 1.2.0 and should exist by default */
-png_uint_32 PNGAPI
-png_get_asm_flags (png_structp png_ptr)
-{
-    /* obsolete, to be removed from libpng-1.4.0 */
-    return (png_ptr? 0L: 0L);
-}
-
-/* this function was added to libpng 1.2.0 and should exist by default */
-png_uint_32 PNGAPI
-png_get_asm_flagmask (int flag_select)
-{
-    /* obsolete, to be removed from libpng-1.4.0 */
-    flag_select=flag_select;
-    return 0L;
-}
-
-    /* GRR:  could add this:   && defined(PNG_MMX_CODE_SUPPORTED) */
-/* this function was added to libpng 1.2.0 */
-png_uint_32 PNGAPI
-png_get_mmx_flagmask (int flag_select, int *compilerID)
-{
-    /* obsolete, to be removed from libpng-1.4.0 */
-    flag_select=flag_select;
-    *compilerID = -1;   /* unknown (i.e., no asm/MMX code compiled) */
-    return 0L;
-}
-
-/* this function was added to libpng 1.2.0 */
-png_byte PNGAPI
-png_get_mmx_bitdepth_threshold (png_structp png_ptr)
-{
-    /* obsolete, to be removed from libpng-1.4.0 */
-    return (png_ptr? 0: 0);
-}
-
-/* this function was added to libpng 1.2.0 */
-png_uint_32 PNGAPI
-png_get_mmx_rowbytes_threshold (png_structp png_ptr)
-{
-    /* obsolete, to be removed from libpng-1.4.0 */
-    return (png_ptr? 0L: 0L);
-}
-#endif /* ?PNG_1_0_X */
-#endif /* ?PNG_ASSEMBLER_CODE_SUPPORTED */
-
-#ifdef PNG_SET_USER_LIMITS_SUPPORTED
-/* these functions were added to libpng 1.2.6 */
-png_uint_32 PNGAPI
-png_get_user_width_max (png_structp png_ptr)
-{
-    return (png_ptr? png_ptr->user_width_max : 0);
-}
-png_uint_32 PNGAPI
-png_get_user_height_max (png_structp png_ptr)
-{
-    return (png_ptr? png_ptr->user_height_max : 0);
-}
-#endif /* ?PNG_SET_USER_LIMITS_SUPPORTED */
-
-#endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */
+\r
+/* pngget.c - retrieval of values from info struct\r
+ *\r
+ * Last changed in libpng 1.4.2 [May 6, 2010]\r
+ * Copyright (c) 1998-2010 Glenn Randers-Pehrson\r
+ * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)\r
+ * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)\r
+ *\r
+ * This code is released under the libpng license.\r
+ * For conditions of distribution and use, see the disclaimer\r
+ * and license in png.h\r
+ *\r
+ */\r
+\r
+#define PNG_NO_PEDANTIC_WARNINGS\r
+#include "png.h"\r
+#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)\r
+#include "pngpriv.h"\r
+\r
+png_uint_32 PNGAPI\r
+png_get_valid(png_structp png_ptr, png_infop info_ptr, png_uint_32 flag)\r
+{\r
+   if (png_ptr != NULL && info_ptr != NULL)\r
+      return(info_ptr->valid & flag);\r
+\r
+   else\r
+      return(0);\r
+}\r
+\r
+png_size_t PNGAPI\r
+png_get_rowbytes(png_structp png_ptr, png_infop info_ptr)\r
+{\r
+   if (png_ptr != NULL && info_ptr != NULL)\r
+      return(info_ptr->rowbytes);\r
+\r
+   else\r
+      return(0);\r
+}\r
+\r
+#ifdef PNG_INFO_IMAGE_SUPPORTED\r
+png_bytepp PNGAPI\r
+png_get_rows(png_structp png_ptr, png_infop info_ptr)\r
+{\r
+   if (png_ptr != NULL && info_ptr != NULL)\r
+      return(info_ptr->row_pointers);\r
+\r
+   else\r
+      return(0);\r
+}\r
+#endif\r
+\r
+#ifdef PNG_EASY_ACCESS_SUPPORTED\r
+/* Easy access to info, added in libpng-0.99 */\r
+png_uint_32 PNGAPI\r
+png_get_image_width(png_structp png_ptr, png_infop info_ptr)\r
+{\r
+   if (png_ptr != NULL && info_ptr != NULL)\r
+      return info_ptr->width;\r
+\r
+   return (0);\r
+}\r
+\r
+png_uint_32 PNGAPI\r
+png_get_image_height(png_structp png_ptr, png_infop info_ptr)\r
+{\r
+   if (png_ptr != NULL && info_ptr != NULL)\r
+      return info_ptr->height;\r
+\r
+   return (0);\r
+}\r
+\r
+png_byte PNGAPI\r
+png_get_bit_depth(png_structp png_ptr, png_infop info_ptr)\r
+{\r
+   if (png_ptr != NULL && info_ptr != NULL)\r
+      return info_ptr->bit_depth;\r
+\r
+   return (0);\r
+}\r
+\r
+png_byte PNGAPI\r
+png_get_color_type(png_structp png_ptr, png_infop info_ptr)\r
+{\r
+   if (png_ptr != NULL && info_ptr != NULL)\r
+      return info_ptr->color_type;\r
+\r
+   return (0);\r
+}\r
+\r
+png_byte PNGAPI\r
+png_get_filter_type(png_structp png_ptr, png_infop info_ptr)\r
+{\r
+   if (png_ptr != NULL && info_ptr != NULL)\r
+      return info_ptr->filter_type;\r
+\r
+   return (0);\r
+}\r
+\r
+png_byte PNGAPI\r
+png_get_interlace_type(png_structp png_ptr, png_infop info_ptr)\r
+{\r
+   if (png_ptr != NULL && info_ptr != NULL)\r
+      return info_ptr->interlace_type;\r
+\r
+   return (0);\r
+}\r
+\r
+png_byte PNGAPI\r
+png_get_compression_type(png_structp png_ptr, png_infop info_ptr)\r
+{\r
+   if (png_ptr != NULL && info_ptr != NULL)\r
+      return info_ptr->compression_type;\r
+\r
+   return (0);\r
+}\r
+\r
+png_uint_32 PNGAPI\r
+png_get_x_pixels_per_meter(png_structp png_ptr, png_infop info_ptr)\r
+{\r
+   if (png_ptr != NULL && info_ptr != NULL)\r
+#ifdef PNG_pHYs_SUPPORTED\r
+   if (info_ptr->valid & PNG_INFO_pHYs)\r
+   {\r
+      png_debug1(1, "in %s retrieval function", "png_get_x_pixels_per_meter");\r
+\r
+      if (info_ptr->phys_unit_type != PNG_RESOLUTION_METER)\r
+          return (0);\r
+\r
+      else\r
+          return (info_ptr->x_pixels_per_unit);\r
+   }\r
+#else\r
+   return (0);\r
+#endif\r
+   return (0);\r
+}\r
+\r
+png_uint_32 PNGAPI\r
+png_get_y_pixels_per_meter(png_structp png_ptr, png_infop info_ptr)\r
+{\r
+   if (png_ptr != NULL && info_ptr != NULL)\r
+#ifdef PNG_pHYs_SUPPORTED\r
+   if (info_ptr->valid & PNG_INFO_pHYs)\r
+   {\r
+      png_debug1(1, "in %s retrieval function", "png_get_y_pixels_per_meter");\r
+\r
+      if (info_ptr->phys_unit_type != PNG_RESOLUTION_METER)\r
+          return (0);\r
+\r
+      else\r
+          return (info_ptr->y_pixels_per_unit);\r
+   }\r
+#else\r
+   return (0);\r
+#endif\r
+   return (0);\r
+}\r
+\r
+png_uint_32 PNGAPI\r
+png_get_pixels_per_meter(png_structp png_ptr, png_infop info_ptr)\r
+{\r
+   if (png_ptr != NULL && info_ptr != NULL)\r
+#ifdef PNG_pHYs_SUPPORTED\r
+   if (info_ptr->valid & PNG_INFO_pHYs)\r
+   {\r
+      png_debug1(1, "in %s retrieval function", "png_get_pixels_per_meter");\r
+\r
+      if (info_ptr->phys_unit_type != PNG_RESOLUTION_METER ||\r
+         info_ptr->x_pixels_per_unit != info_ptr->y_pixels_per_unit)\r
+          return (0);\r
+\r
+      else\r
+          return (info_ptr->x_pixels_per_unit);\r
+   }\r
+#else\r
+   return (0);\r
+#endif\r
+   return (0);\r
+}\r
+\r
+#ifdef PNG_FLOATING_POINT_SUPPORTED\r
+float PNGAPI\r
+png_get_pixel_aspect_ratio(png_structp png_ptr, png_infop info_ptr)\r
+   {\r
+   if (png_ptr != NULL && info_ptr != NULL)\r
+#ifdef PNG_pHYs_SUPPORTED\r
+\r
+   if (info_ptr->valid & PNG_INFO_pHYs)\r
+   {\r
+      png_debug1(1, "in %s retrieval function", "png_get_aspect_ratio");\r
+\r
+      if (info_ptr->x_pixels_per_unit == 0)\r
+         return ((float)0.0);\r
+\r
+      else\r
+         return ((float)((float)info_ptr->y_pixels_per_unit\r
+            /(float)info_ptr->x_pixels_per_unit));\r
+   }\r
+#else\r
+      return (0.0);\r
+#endif\r
+   return ((float)0.0);\r
+}\r
+#endif\r
+\r
+png_int_32 PNGAPI\r
+png_get_x_offset_microns(png_structp png_ptr, png_infop info_ptr)\r
+{\r
+   if (png_ptr != NULL && info_ptr != NULL)\r
+#ifdef PNG_oFFs_SUPPORTED\r
+\r
+   if (info_ptr->valid & PNG_INFO_oFFs)\r
+   {\r
+      png_debug1(1, "in %s retrieval function", "png_get_x_offset_microns");\r
+\r
+      if (info_ptr->offset_unit_type != PNG_OFFSET_MICROMETER)\r
+          return (0);\r
+\r
+      else\r
+          return (info_ptr->x_offset);\r
+   }\r
+#else\r
+      return (0);\r
+#endif\r
+   return (0);\r
+}\r
+\r
+png_int_32 PNGAPI\r
+png_get_y_offset_microns(png_structp png_ptr, png_infop info_ptr)\r
+{\r
+   if (png_ptr != NULL && info_ptr != NULL)\r
+\r
+#ifdef PNG_oFFs_SUPPORTED\r
+   if (info_ptr->valid & PNG_INFO_oFFs)\r
+   {\r
+      png_debug1(1, "in %s retrieval function", "png_get_y_offset_microns");\r
+\r
+      if (info_ptr->offset_unit_type != PNG_OFFSET_MICROMETER)\r
+          return (0);\r
+\r
+      else\r
+          return (info_ptr->y_offset);\r
+   }\r
+#else\r
+   return (0);\r
+#endif\r
+   return (0);\r
+}\r
+\r
+png_int_32 PNGAPI\r
+png_get_x_offset_pixels(png_structp png_ptr, png_infop info_ptr)\r
+{\r
+   if (png_ptr != NULL && info_ptr != NULL)\r
+\r
+#ifdef PNG_oFFs_SUPPORTED\r
+   if (info_ptr->valid & PNG_INFO_oFFs)\r
+   {\r
+      png_debug1(1, "in %s retrieval function", "png_get_x_offset_microns");\r
+\r
+      if (info_ptr->offset_unit_type != PNG_OFFSET_PIXEL)\r
+          return (0);\r
+\r
+      else\r
+          return (info_ptr->x_offset);\r
+   }\r
+#else\r
+   return (0);\r
+#endif\r
+   return (0);\r
+}\r
+\r
+png_int_32 PNGAPI\r
+png_get_y_offset_pixels(png_structp png_ptr, png_infop info_ptr)\r
+{\r
+   if (png_ptr != NULL && info_ptr != NULL)\r
+\r
+#ifdef PNG_oFFs_SUPPORTED\r
+   if (info_ptr->valid & PNG_INFO_oFFs)\r
+   {\r
+      png_debug1(1, "in %s retrieval function", "png_get_y_offset_microns");\r
+\r
+      if (info_ptr->offset_unit_type != PNG_OFFSET_PIXEL)\r
+          return (0);\r
+\r
+      else\r
+          return (info_ptr->y_offset);\r
+   }\r
+#else\r
+   return (0);\r
+#endif\r
+   return (0);\r
+}\r
+\r
+#if defined(PNG_INCH_CONVERSIONS) && defined(PNG_FLOATING_POINT_SUPPORTED)\r
+png_uint_32 PNGAPI\r
+png_get_pixels_per_inch(png_structp png_ptr, png_infop info_ptr)\r
+{\r
+   return ((png_uint_32)((float)png_get_pixels_per_meter(png_ptr, info_ptr)\r
+     *.0254 +.5));\r
+}\r
+\r
+png_uint_32 PNGAPI\r
+png_get_x_pixels_per_inch(png_structp png_ptr, png_infop info_ptr)\r
+{\r
+   return ((png_uint_32)((float)png_get_x_pixels_per_meter(png_ptr, info_ptr)\r
+     *.0254 +.5));\r
+}\r
+\r
+png_uint_32 PNGAPI\r
+png_get_y_pixels_per_inch(png_structp png_ptr, png_infop info_ptr)\r
+{\r
+   return ((png_uint_32)((float)png_get_y_pixels_per_meter(png_ptr, info_ptr)\r
+     *.0254 +.5));\r
+}\r
+\r
+float PNGAPI\r
+png_get_x_offset_inches(png_structp png_ptr, png_infop info_ptr)\r
+{\r
+   return ((float)png_get_x_offset_microns(png_ptr, info_ptr)\r
+     *.00003937);\r
+}\r
+\r
+float PNGAPI\r
+png_get_y_offset_inches(png_structp png_ptr, png_infop info_ptr)\r
+{\r
+   return ((float)png_get_y_offset_microns(png_ptr, info_ptr)\r
+     *.00003937);\r
+}\r
+\r
+#ifdef PNG_pHYs_SUPPORTED\r
+png_uint_32 PNGAPI\r
+png_get_pHYs_dpi(png_structp png_ptr, png_infop info_ptr,\r
+   png_uint_32 *res_x, png_uint_32 *res_y, int *unit_type)\r
+{\r
+   png_uint_32 retval = 0;\r
+\r
+   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_pHYs))\r
+   {\r
+      png_debug1(1, "in %s retrieval function", "pHYs");\r
+\r
+      if (res_x != NULL)\r
+      {\r
+         *res_x = info_ptr->x_pixels_per_unit;\r
+         retval |= PNG_INFO_pHYs;\r
+      }\r
+      if (res_y != NULL)\r
+      {\r
+         *res_y = info_ptr->y_pixels_per_unit;\r
+         retval |= PNG_INFO_pHYs;\r
+      }\r
+      if (unit_type != NULL)\r
+      {\r
+         *unit_type = (int)info_ptr->phys_unit_type;\r
+         retval |= PNG_INFO_pHYs;\r
+         if (*unit_type == 1)\r
+         {\r
+            if (res_x != NULL) *res_x = (png_uint_32)(*res_x * .0254 + .50);\r
+            if (res_y != NULL) *res_y = (png_uint_32)(*res_y * .0254 + .50);\r
+         }\r
+      }\r
+   }\r
+   return (retval);\r
+}\r
+#endif /* PNG_pHYs_SUPPORTED */\r
+#endif  /* PNG_INCH_CONVERSIONS && PNG_FLOATING_POINT_SUPPORTED */\r
+\r
+/* png_get_channels really belongs in here, too, but it's been around longer */\r
+\r
+#endif  /* PNG_EASY_ACCESS_SUPPORTED */\r
+\r
+png_byte PNGAPI\r
+png_get_channels(png_structp png_ptr, png_infop info_ptr)\r
+{\r
+   if (png_ptr != NULL && info_ptr != NULL)\r
+      return(info_ptr->channels);\r
+   else\r
+      return (0);\r
+}\r
+\r
+png_bytep PNGAPI\r
+png_get_signature(png_structp png_ptr, png_infop info_ptr)\r
+{\r
+   if (png_ptr != NULL && info_ptr != NULL)\r
+      return(info_ptr->signature);\r
+   else\r
+      return (NULL);\r
+}\r
+\r
+#ifdef PNG_bKGD_SUPPORTED\r
+png_uint_32 PNGAPI\r
+png_get_bKGD(png_structp png_ptr, png_infop info_ptr,\r
+   png_color_16p *background)\r
+{\r
+   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_bKGD)\r
+      && background != NULL)\r
+   {\r
+      png_debug1(1, "in %s retrieval function", "bKGD");\r
+\r
+      *background = &(info_ptr->background);\r
+      return (PNG_INFO_bKGD);\r
+   }\r
+   return (0);\r
+}\r
+#endif\r
+\r
+#ifdef PNG_cHRM_SUPPORTED\r
+#ifdef PNG_FLOATING_POINT_SUPPORTED\r
+png_uint_32 PNGAPI\r
+png_get_cHRM(png_structp png_ptr, png_infop info_ptr,\r
+   double *white_x, double *white_y, double *red_x, double *red_y,\r
+   double *green_x, double *green_y, double *blue_x, double *blue_y)\r
+{\r
+   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_cHRM))\r
+   {\r
+      png_debug1(1, "in %s retrieval function", "cHRM");\r
+\r
+      if (white_x != NULL)\r
+         *white_x = (double)info_ptr->x_white;\r
+      if (white_y != NULL)\r
+         *white_y = (double)info_ptr->y_white;\r
+      if (red_x != NULL)\r
+         *red_x = (double)info_ptr->x_red;\r
+      if (red_y != NULL)\r
+         *red_y = (double)info_ptr->y_red;\r
+      if (green_x != NULL)\r
+         *green_x = (double)info_ptr->x_green;\r
+      if (green_y != NULL)\r
+         *green_y = (double)info_ptr->y_green;\r
+      if (blue_x != NULL)\r
+         *blue_x = (double)info_ptr->x_blue;\r
+      if (blue_y != NULL)\r
+         *blue_y = (double)info_ptr->y_blue;\r
+      return (PNG_INFO_cHRM);\r
+   }\r
+   return (0);\r
+}\r
+#endif\r
+#ifdef PNG_FIXED_POINT_SUPPORTED\r
+png_uint_32 PNGAPI\r
+png_get_cHRM_fixed(png_structp png_ptr, png_infop info_ptr,\r
+   png_fixed_point *white_x, png_fixed_point *white_y, png_fixed_point *red_x,\r
+   png_fixed_point *red_y, png_fixed_point *green_x, png_fixed_point *green_y,\r
+   png_fixed_point *blue_x, png_fixed_point *blue_y)\r
+{\r
+   png_debug1(1, "in %s retrieval function", "cHRM");\r
+\r
+   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_cHRM))\r
+   {\r
+      if (white_x != NULL)\r
+         *white_x = info_ptr->int_x_white;\r
+      if (white_y != NULL)\r
+         *white_y = info_ptr->int_y_white;\r
+      if (red_x != NULL)\r
+         *red_x = info_ptr->int_x_red;\r
+      if (red_y != NULL)\r
+         *red_y = info_ptr->int_y_red;\r
+      if (green_x != NULL)\r
+         *green_x = info_ptr->int_x_green;\r
+      if (green_y != NULL)\r
+         *green_y = info_ptr->int_y_green;\r
+      if (blue_x != NULL)\r
+         *blue_x = info_ptr->int_x_blue;\r
+      if (blue_y != NULL)\r
+         *blue_y = info_ptr->int_y_blue;\r
+      return (PNG_INFO_cHRM);\r
+   }\r
+   return (0);\r
+}\r
+#endif\r
+#endif\r
+\r
+#ifdef PNG_gAMA_SUPPORTED\r
+#ifdef PNG_FLOATING_POINT_SUPPORTED\r
+png_uint_32 PNGAPI\r
+png_get_gAMA(png_structp png_ptr, png_infop info_ptr, double *file_gamma)\r
+{\r
+   png_debug1(1, "in %s retrieval function", "gAMA");\r
+\r
+   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_gAMA)\r
+      && file_gamma != NULL)\r
+   {\r
+      *file_gamma = (double)info_ptr->gamma;\r
+      return (PNG_INFO_gAMA);\r
+   }\r
+   return (0);\r
+}\r
+#endif\r
+#ifdef PNG_FIXED_POINT_SUPPORTED\r
+png_uint_32 PNGAPI\r
+png_get_gAMA_fixed(png_structp png_ptr, png_infop info_ptr,\r
+    png_fixed_point *int_file_gamma)\r
+{\r
+   png_debug1(1, "in %s retrieval function", "gAMA");\r
+\r
+   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_gAMA)\r
+      && int_file_gamma != NULL)\r
+   {\r
+      *int_file_gamma = info_ptr->int_gamma;\r
+      return (PNG_INFO_gAMA);\r
+   }\r
+   return (0);\r
+}\r
+#endif\r
+#endif\r
+\r
+#ifdef PNG_sRGB_SUPPORTED\r
+png_uint_32 PNGAPI\r
+png_get_sRGB(png_structp png_ptr, png_infop info_ptr, int *file_srgb_intent)\r
+{\r
+   png_debug1(1, "in %s retrieval function", "sRGB");\r
+\r
+   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_sRGB)\r
+      && file_srgb_intent != NULL)\r
+   {\r
+      *file_srgb_intent = (int)info_ptr->srgb_intent;\r
+      return (PNG_INFO_sRGB);\r
+   }\r
+   return (0);\r
+}\r
+#endif\r
+\r
+#ifdef PNG_iCCP_SUPPORTED\r
+png_uint_32 PNGAPI\r
+png_get_iCCP(png_structp png_ptr, png_infop info_ptr,\r
+             png_charpp name, int *compression_type,\r
+             png_charpp profile, png_uint_32 *proflen)\r
+{\r
+   png_debug1(1, "in %s retrieval function", "iCCP");\r
+\r
+   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_iCCP)\r
+      && name != NULL && profile != NULL && proflen != NULL)\r
+   {\r
+      *name = info_ptr->iccp_name;\r
+      *profile = info_ptr->iccp_profile;\r
+      /* Compression_type is a dummy so the API won't have to change\r
+       * if we introduce multiple compression types later.\r
+       */\r
+      *proflen = (int)info_ptr->iccp_proflen;\r
+      *compression_type = (int)info_ptr->iccp_compression;\r
+      return (PNG_INFO_iCCP);\r
+   }\r
+   return (0);\r
+}\r
+#endif\r
+\r
+#ifdef PNG_sPLT_SUPPORTED\r
+png_uint_32 PNGAPI\r
+png_get_sPLT(png_structp png_ptr, png_infop info_ptr,\r
+             png_sPLT_tpp spalettes)\r
+{\r
+   if (png_ptr != NULL && info_ptr != NULL && spalettes != NULL)\r
+   {\r
+     *spalettes = info_ptr->splt_palettes;\r
+     return ((png_uint_32)info_ptr->splt_palettes_num);\r
+   }\r
+   return (0);\r
+}\r
+#endif\r
+\r
+#ifdef PNG_hIST_SUPPORTED\r
+png_uint_32 PNGAPI\r
+png_get_hIST(png_structp png_ptr, png_infop info_ptr, png_uint_16p *hist)\r
+{\r
+   png_debug1(1, "in %s retrieval function", "hIST");\r
+\r
+   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_hIST)\r
+      && hist != NULL)\r
+   {\r
+      *hist = info_ptr->hist;\r
+      return (PNG_INFO_hIST);\r
+   }\r
+   return (0);\r
+}\r
+#endif\r
+\r
+png_uint_32 PNGAPI\r
+png_get_IHDR(png_structp png_ptr, png_infop info_ptr,\r
+   png_uint_32 *width, png_uint_32 *height, int *bit_depth,\r
+   int *color_type, int *interlace_type, int *compression_type,\r
+   int *filter_type)\r
+\r
+{\r
+   png_debug1(1, "in %s retrieval function", "IHDR");\r
+\r
+   if (png_ptr == NULL || info_ptr == NULL || width == NULL ||\r
+       height == NULL || bit_depth == NULL || color_type == NULL)\r
+      return (0);\r
+\r
+   *width = info_ptr->width;\r
+   *height = info_ptr->height;\r
+   *bit_depth = info_ptr->bit_depth;\r
+   *color_type = info_ptr->color_type;\r
+\r
+   if (compression_type != NULL)\r
+      *compression_type = info_ptr->compression_type;\r
+\r
+   if (filter_type != NULL)\r
+      *filter_type = info_ptr->filter_type;\r
+\r
+   if (interlace_type != NULL)\r
+      *interlace_type = info_ptr->interlace_type;\r
+\r
+   /* This is redundant if we can be sure that the info_ptr values were all\r
+    * assigned in png_set_IHDR().  We do the check anyhow in case an\r
+    * application has ignored our advice not to mess with the members\r
+    * of info_ptr directly.\r
+    */\r
+   png_check_IHDR (png_ptr, info_ptr->width, info_ptr->height,\r
+       info_ptr->bit_depth, info_ptr->color_type, info_ptr->interlace_type,\r
+       info_ptr->compression_type, info_ptr->filter_type);\r
+\r
+   return (1);\r
+}\r
+\r
+#ifdef PNG_oFFs_SUPPORTED\r
+png_uint_32 PNGAPI\r
+png_get_oFFs(png_structp png_ptr, png_infop info_ptr,\r
+   png_int_32 *offset_x, png_int_32 *offset_y, int *unit_type)\r
+{\r
+   png_debug1(1, "in %s retrieval function", "oFFs");\r
+\r
+   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_oFFs)\r
+      && offset_x != NULL && offset_y != NULL && unit_type != NULL)\r
+   {\r
+      *offset_x = info_ptr->x_offset;\r
+      *offset_y = info_ptr->y_offset;\r
+      *unit_type = (int)info_ptr->offset_unit_type;\r
+      return (PNG_INFO_oFFs);\r
+   }\r
+   return (0);\r
+}\r
+#endif\r
+\r
+#ifdef PNG_pCAL_SUPPORTED\r
+png_uint_32 PNGAPI\r
+png_get_pCAL(png_structp png_ptr, png_infop info_ptr,\r
+   png_charp *purpose, png_int_32 *X0, png_int_32 *X1, int *type, int *nparams,\r
+   png_charp *units, png_charpp *params)\r
+{\r
+   png_debug1(1, "in %s retrieval function", "pCAL");\r
+\r
+   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_pCAL)\r
+       && purpose != NULL && X0 != NULL && X1 != NULL && type != NULL &&\r
+       nparams != NULL && units != NULL && params != NULL)\r
+   {\r
+      *purpose = info_ptr->pcal_purpose;\r
+      *X0 = info_ptr->pcal_X0;\r
+      *X1 = info_ptr->pcal_X1;\r
+      *type = (int)info_ptr->pcal_type;\r
+      *nparams = (int)info_ptr->pcal_nparams;\r
+      *units = info_ptr->pcal_units;\r
+      *params = info_ptr->pcal_params;\r
+      return (PNG_INFO_pCAL);\r
+   }\r
+   return (0);\r
+}\r
+#endif\r
+\r
+#ifdef PNG_sCAL_SUPPORTED\r
+#ifdef PNG_FLOATING_POINT_SUPPORTED\r
+png_uint_32 PNGAPI\r
+png_get_sCAL(png_structp png_ptr, png_infop info_ptr,\r
+             int *unit, double *width, double *height)\r
+{\r
+    if (png_ptr != NULL && info_ptr != NULL &&\r
+        (info_ptr->valid & PNG_INFO_sCAL))\r
+    {\r
+        *unit = info_ptr->scal_unit;\r
+        *width = info_ptr->scal_pixel_width;\r
+        *height = info_ptr->scal_pixel_height;\r
+        return (PNG_INFO_sCAL);\r
+    }\r
+    return(0);\r
+}\r
+#else\r
+#ifdef PNG_FIXED_POINT_SUPPORTED\r
+png_uint_32 PNGAPI\r
+png_get_sCAL_s(png_structp png_ptr, png_infop info_ptr,\r
+             int *unit, png_charpp width, png_charpp height)\r
+{\r
+    if (png_ptr != NULL && info_ptr != NULL &&\r
+        (info_ptr->valid & PNG_INFO_sCAL))\r
+    {\r
+        *unit = info_ptr->scal_unit;\r
+        *width = info_ptr->scal_s_width;\r
+        *height = info_ptr->scal_s_height;\r
+        return (PNG_INFO_sCAL);\r
+    }\r
+    return(0);\r
+}\r
+#endif\r
+#endif\r
+#endif\r
+\r
+#ifdef PNG_pHYs_SUPPORTED\r
+png_uint_32 PNGAPI\r
+png_get_pHYs(png_structp png_ptr, png_infop info_ptr,\r
+   png_uint_32 *res_x, png_uint_32 *res_y, int *unit_type)\r
+{\r
+   png_uint_32 retval = 0;\r
+\r
+   png_debug1(1, "in %s retrieval function", "pHYs");\r
+\r
+   if (png_ptr != NULL && info_ptr != NULL &&\r
+      (info_ptr->valid & PNG_INFO_pHYs))\r
+   {\r
+      if (res_x != NULL)\r
+      {\r
+         *res_x = info_ptr->x_pixels_per_unit;\r
+         retval |= PNG_INFO_pHYs;\r
+      }\r
+\r
+      if (res_y != NULL)\r
+      {\r
+         *res_y = info_ptr->y_pixels_per_unit;\r
+         retval |= PNG_INFO_pHYs;\r
+      }\r
+\r
+      if (unit_type != NULL)\r
+      {\r
+         *unit_type = (int)info_ptr->phys_unit_type;\r
+         retval |= PNG_INFO_pHYs;\r
+      }\r
+   }\r
+   return (retval);\r
+}\r
+#endif\r
+\r
+png_uint_32 PNGAPI\r
+png_get_PLTE(png_structp png_ptr, png_infop info_ptr, png_colorp *palette,\r
+   int *num_palette)\r
+{\r
+   png_debug1(1, "in %s retrieval function", "PLTE");\r
+\r
+   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_PLTE)\r
+       && palette != NULL)\r
+   {\r
+      *palette = info_ptr->palette;\r
+      *num_palette = info_ptr->num_palette;\r
+      png_debug1(3, "num_palette = %d", *num_palette);\r
+      return (PNG_INFO_PLTE);\r
+   }\r
+   return (0);\r
+}\r
+\r
+#ifdef PNG_sBIT_SUPPORTED\r
+png_uint_32 PNGAPI\r
+png_get_sBIT(png_structp png_ptr, png_infop info_ptr, png_color_8p *sig_bit)\r
+{\r
+   png_debug1(1, "in %s retrieval function", "sBIT");\r
+\r
+   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_sBIT)\r
+      && sig_bit != NULL)\r
+   {\r
+      *sig_bit = &(info_ptr->sig_bit);\r
+      return (PNG_INFO_sBIT);\r
+   }\r
+   return (0);\r
+}\r
+#endif\r
+\r
+#ifdef PNG_TEXT_SUPPORTED\r
+png_uint_32 PNGAPI\r
+png_get_text(png_structp png_ptr, png_infop info_ptr, png_textp *text_ptr,\r
+   int *num_text)\r
+{\r
+   if (png_ptr != NULL && info_ptr != NULL && info_ptr->num_text > 0)\r
+   {\r
+      png_debug1(1, "in %s retrieval function",\r
+         (png_ptr->chunk_name[0] == '\0' ? "text"\r
+             : (png_const_charp)png_ptr->chunk_name));\r
+\r
+      if (text_ptr != NULL)\r
+         *text_ptr = info_ptr->text;\r
+\r
+      if (num_text != NULL)\r
+         *num_text = info_ptr->num_text;\r
+\r
+      return ((png_uint_32)info_ptr->num_text);\r
+   }\r
+   if (num_text != NULL)\r
+     *num_text = 0;\r
+   return(0);\r
+}\r
+#endif\r
+\r
+#ifdef PNG_tIME_SUPPORTED\r
+png_uint_32 PNGAPI\r
+png_get_tIME(png_structp png_ptr, png_infop info_ptr, png_timep *mod_time)\r
+{\r
+   png_debug1(1, "in %s retrieval function", "tIME");\r
+\r
+   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_tIME)\r
+       && mod_time != NULL)\r
+   {\r
+      *mod_time = &(info_ptr->mod_time);\r
+      return (PNG_INFO_tIME);\r
+   }\r
+   return (0);\r
+}\r
+#endif\r
+\r
+#ifdef PNG_tRNS_SUPPORTED\r
+png_uint_32 PNGAPI\r
+png_get_tRNS(png_structp png_ptr, png_infop info_ptr,\r
+   png_bytep *trans_alpha, int *num_trans, png_color_16p *trans_color)\r
+{\r
+   png_uint_32 retval = 0;\r
+   if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS))\r
+   {\r
+      png_debug1(1, "in %s retrieval function", "tRNS");\r
+\r
+      if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)\r
+      {\r
+          if (trans_alpha != NULL)\r
+          {\r
+             *trans_alpha = info_ptr->trans_alpha;\r
+             retval |= PNG_INFO_tRNS;\r
+          }\r
+\r
+          if (trans_color != NULL)\r
+             *trans_color = &(info_ptr->trans_color);\r
+      }\r
+      else /* if (info_ptr->color_type != PNG_COLOR_TYPE_PALETTE) */\r
+      {\r
+          if (trans_color != NULL)\r
+          {\r
+             *trans_color = &(info_ptr->trans_color);\r
+             retval |= PNG_INFO_tRNS;\r
+          }\r
+\r
+          if (trans_alpha != NULL)\r
+             *trans_alpha = NULL;\r
+      }\r
+      if (num_trans != NULL)\r
+      {\r
+         *num_trans = info_ptr->num_trans;\r
+         retval |= PNG_INFO_tRNS;\r
+      }\r
+   }\r
+   return (retval);\r
+}\r
+#endif\r
+\r
+#ifdef PNG_UNKNOWN_CHUNKS_SUPPORTED\r
+png_uint_32 PNGAPI\r
+png_get_unknown_chunks(png_structp png_ptr, png_infop info_ptr,\r
+             png_unknown_chunkpp unknowns)\r
+{\r
+   if (png_ptr != NULL && info_ptr != NULL && unknowns != NULL)\r
+   {\r
+     *unknowns = info_ptr->unknown_chunks;\r
+     return ((png_uint_32)info_ptr->unknown_chunks_num);\r
+   }\r
+   return (0);\r
+}\r
+#endif\r
+\r
+#ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED\r
+png_byte PNGAPI\r
+png_get_rgb_to_gray_status (png_structp png_ptr)\r
+{\r
+   return (png_byte)(png_ptr? png_ptr->rgb_to_gray_status : 0);\r
+}\r
+#endif\r
+\r
+#ifdef PNG_USER_CHUNKS_SUPPORTED\r
+png_voidp PNGAPI\r
+png_get_user_chunk_ptr(png_structp png_ptr)\r
+{\r
+   return (png_ptr? png_ptr->user_chunk_ptr : NULL);\r
+}\r
+#endif\r
+\r
+png_size_t PNGAPI\r
+png_get_compression_buffer_size(png_structp png_ptr)\r
+{\r
+   return (png_ptr ? png_ptr->zbuf_size : 0L);\r
+}\r
+\r
+\r
+#ifdef PNG_SET_USER_LIMITS_SUPPORTED\r
+/* These functions were added to libpng 1.2.6 and were enabled\r
+ * by default in libpng-1.4.0 */\r
+png_uint_32 PNGAPI\r
+png_get_user_width_max (png_structp png_ptr)\r
+{\r
+    return (png_ptr? png_ptr->user_width_max : 0);\r
+}\r
+png_uint_32 PNGAPI\r
+png_get_user_height_max (png_structp png_ptr)\r
+{\r
+    return (png_ptr? png_ptr->user_height_max : 0);\r
+}\r
+/* This function was added to libpng 1.4.0 */\r
+png_uint_32 PNGAPI\r
+png_get_chunk_cache_max (png_structp png_ptr)\r
+{\r
+    return (png_ptr? png_ptr->user_chunk_cache_max : 0);\r
+}\r
+/* This function was added to libpng 1.4.1 */\r
+png_alloc_size_t PNGAPI\r
+png_get_chunk_malloc_max (png_structp png_ptr)\r
+{\r
+    return (png_ptr?\r
+       png_ptr->user_chunk_malloc_max : 0);\r
+}\r
+#endif /* ?PNG_SET_USER_LIMITS_SUPPORTED */\r
+\r
+/* These functions were added to libpng 1.4.0 */\r
+#ifdef PNG_IO_STATE_SUPPORTED\r
+png_uint_32 PNGAPI\r
+png_get_io_state (png_structp png_ptr)\r
+{\r
+    return png_ptr->io_state;\r
+}\r
+\r
+png_bytep PNGAPI\r
+png_get_io_chunk_name (png_structp png_ptr)\r
+{\r
+   return png_ptr->chunk_name;\r
+}\r
+#endif /* ?PNG_IO_STATE_SUPPORTED */\r
+\r
+#endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */\r
index 13cc60c..a7a79ab 100644 (file)
-
-/* pngmem.c - stub functions for memory allocation
- *
- * Last changed in libpng 1.2.27 [April 29, 2008]
- * For conditions of distribution and use, see copyright notice in png.h
- * Copyright (c) 1998-2008 Glenn Randers-Pehrson
- * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
- * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
- *
- * This file provides a location for all memory allocation.  Users who
- * need special memory handling are expected to supply replacement
- * functions for png_malloc() and png_free(), and to use
- * png_create_read_struct_2() and png_create_write_struct_2() to
- * identify the replacement functions.
- */
-
-#define PNG_INTERNAL
-#include "png.h"
-
-#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
-
-/* Borland DOS special memory handler */
-#if defined(__TURBOC__) && !defined(_Windows) && !defined(__FLAT__)
-/* if you change this, be sure to change the one in png.h also */
-
-/* Allocate memory for a png_struct.  The malloc and memset can be replaced
-   by a single call to calloc() if this is thought to improve performance. */
-png_voidp /* PRIVATE */
-png_create_struct(int type)
-{
-#ifdef PNG_USER_MEM_SUPPORTED
-   return (png_create_struct_2(type, png_malloc_ptr_NULL, png_voidp_NULL));
-}
-
-/* Alternate version of png_create_struct, for use with user-defined malloc. */
-png_voidp /* PRIVATE */
-png_create_struct_2(int type, png_malloc_ptr malloc_fn, png_voidp mem_ptr)
-{
-#endif /* PNG_USER_MEM_SUPPORTED */
-   png_size_t size;
-   png_voidp struct_ptr;
-
-   if (type == PNG_STRUCT_INFO)
-     size = png_sizeof(png_info);
-   else if (type == PNG_STRUCT_PNG)
-     size = png_sizeof(png_struct);
-   else
-     return (png_get_copyright(NULL));
-
-#ifdef PNG_USER_MEM_SUPPORTED
-   if(malloc_fn != NULL)
-   {
-      png_struct dummy_struct;
-      png_structp png_ptr = &dummy_struct;
-      png_ptr->mem_ptr=mem_ptr;
-      struct_ptr = (*(malloc_fn))(png_ptr, (png_uint_32)size);
-   }
-   else
-#endif /* PNG_USER_MEM_SUPPORTED */
-      struct_ptr = (png_voidp)farmalloc(size);
-   if (struct_ptr != NULL)
-      png_memset(struct_ptr, 0, size);
-   return (struct_ptr);
-}
-
-/* Free memory allocated by a png_create_struct() call */
-void /* PRIVATE */
-png_destroy_struct(png_voidp struct_ptr)
-{
-#ifdef PNG_USER_MEM_SUPPORTED
-   png_destroy_struct_2(struct_ptr, png_free_ptr_NULL, png_voidp_NULL);
-}
-
-/* Free memory allocated by a png_create_struct() call */
-void /* PRIVATE */
-png_destroy_struct_2(png_voidp struct_ptr, png_free_ptr free_fn,
-    png_voidp mem_ptr)
-{
-#endif
-   if (struct_ptr != NULL)
-   {
-#ifdef PNG_USER_MEM_SUPPORTED
-      if(free_fn != NULL)
-      {
-         png_struct dummy_struct;
-         png_structp png_ptr = &dummy_struct;
-         png_ptr->mem_ptr=mem_ptr;
-         (*(free_fn))(png_ptr, struct_ptr);
-         return;
-      }
-#endif /* PNG_USER_MEM_SUPPORTED */
-      farfree (struct_ptr);
-   }
-}
-
-/* Allocate memory.  For reasonable files, size should never exceed
- * 64K.  However, zlib may allocate more then 64K if you don't tell
- * it not to.  See zconf.h and png.h for more information. zlib does
- * need to allocate exactly 64K, so whatever you call here must
- * have the ability to do that.
- *
- * Borland seems to have a problem in DOS mode for exactly 64K.
- * It gives you a segment with an offset of 8 (perhaps to store its
- * memory stuff).  zlib doesn't like this at all, so we have to
- * detect and deal with it.  This code should not be needed in
- * Windows or OS/2 modes, and only in 16 bit mode.  This code has
- * been updated by Alexander Lehmann for version 0.89 to waste less
- * memory.
- *
- * Note that we can't use png_size_t for the "size" declaration,
- * since on some systems a png_size_t is a 16-bit quantity, and as a
- * result, we would be truncating potentially larger memory requests
- * (which should cause a fatal error) and introducing major problems.
- */
-
-png_voidp PNGAPI
-png_malloc(png_structp png_ptr, png_uint_32 size)
-{
-   png_voidp ret;
-
-   if (png_ptr == NULL || size == 0)
-      return (NULL);
-
-#ifdef PNG_USER_MEM_SUPPORTED
-   if(png_ptr->malloc_fn != NULL)
-       ret = ((png_voidp)(*(png_ptr->malloc_fn))(png_ptr, (png_size_t)size));
-   else
-       ret = (png_malloc_default(png_ptr, size));
-   if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
-       png_error(png_ptr, "Out of memory!");
-   return (ret);
-}
-
-png_voidp PNGAPI
-png_malloc_default(png_structp png_ptr, png_uint_32 size)
-{
-   png_voidp ret;
-#endif /* PNG_USER_MEM_SUPPORTED */
-
-   if (png_ptr == NULL || size == 0)
-      return (NULL);
-
-#ifdef PNG_MAX_MALLOC_64K
-   if (size > (png_uint_32)65536L)
-   {
-      png_warning(png_ptr, "Cannot Allocate > 64K");
-      ret = NULL;
-   }
-   else
-#endif
-
-   if (size != (size_t)size)
-     ret = NULL;
-   else if (size == (png_uint_32)65536L)
-   {
-      if (png_ptr->offset_table == NULL)
-      {
-         /* try to see if we need to do any of this fancy stuff */
-         ret = farmalloc(size);
-         if (ret == NULL || ((png_size_t)ret & 0xffff))
-         {
-            int num_blocks;
-            png_uint_32 total_size;
-            png_bytep table;
-            int i;
-            png_byte huge * hptr;
-
-            if (ret != NULL)
-            {
-               farfree(ret);
-               ret = NULL;
-            }
-
-            if(png_ptr->zlib_window_bits > 14)
-               num_blocks = (int)(1 << (png_ptr->zlib_window_bits - 14));
-            else
-               num_blocks = 1;
-            if (png_ptr->zlib_mem_level >= 7)
-               num_blocks += (int)(1 << (png_ptr->zlib_mem_level - 7));
-            else
-               num_blocks++;
-
-            total_size = ((png_uint_32)65536L) * (png_uint_32)num_blocks+16;
-
-            table = farmalloc(total_size);
-
-            if (table == NULL)
-            {
-#ifndef PNG_USER_MEM_SUPPORTED
-               if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
-                  png_error(png_ptr, "Out Of Memory."); /* Note "O" and "M" */
-               else
-                  png_warning(png_ptr, "Out Of Memory.");
-#endif
-               return (NULL);
-            }
-
-            if ((png_size_t)table & 0xfff0)
-            {
-#ifndef PNG_USER_MEM_SUPPORTED
-               if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
-                  png_error(png_ptr,
-                    "Farmalloc didn't return normalized pointer");
-               else
-                  png_warning(png_ptr,
-                    "Farmalloc didn't return normalized pointer");
-#endif
-               return (NULL);
-            }
-
-            png_ptr->offset_table = table;
-            png_ptr->offset_table_ptr = farmalloc(num_blocks *
-               png_sizeof (png_bytep));
-
-            if (png_ptr->offset_table_ptr == NULL)
-            {
-#ifndef PNG_USER_MEM_SUPPORTED
-               if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
-                  png_error(png_ptr, "Out Of memory."); /* Note "O" and "M" */
-               else
-                  png_warning(png_ptr, "Out Of memory.");
-#endif
-               return (NULL);
-            }
-
-            hptr = (png_byte huge *)table;
-            if ((png_size_t)hptr & 0xf)
-            {
-               hptr = (png_byte huge *)((long)(hptr) & 0xfffffff0L);
-               hptr = hptr + 16L;  /* "hptr += 16L" fails on Turbo C++ 3.0 */
-            }
-            for (i = 0; i < num_blocks; i++)
-            {
-               png_ptr->offset_table_ptr[i] = (png_bytep)hptr;
-               hptr = hptr + (png_uint_32)65536L;  /* "+=" fails on TC++3.0 */
-            }
-
-            png_ptr->offset_table_number = num_blocks;
-            png_ptr->offset_table_count = 0;
-            png_ptr->offset_table_count_free = 0;
-         }
-      }
-
-      if (png_ptr->offset_table_count >= png_ptr->offset_table_number)
-      {
-#ifndef PNG_USER_MEM_SUPPORTED
-         if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
-            png_error(png_ptr, "Out of Memory."); /* Note "o" and "M" */
-         else
-            png_warning(png_ptr, "Out of Memory.");
-#endif
-         return (NULL);
-      }
-
-      ret = png_ptr->offset_table_ptr[png_ptr->offset_table_count++];
-   }
-   else
-      ret = farmalloc(size);
-
-#ifndef PNG_USER_MEM_SUPPORTED
-   if (ret == NULL)
-   {
-      if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
-         png_error(png_ptr, "Out of memory."); /* Note "o" and "m" */
-      else
-         png_warning(png_ptr, "Out of memory."); /* Note "o" and "m" */
-   }
-#endif
-
-   return (ret);
-}
-
-/* free a pointer allocated by png_malloc().  In the default
-   configuration, png_ptr is not used, but is passed in case it
-   is needed.  If ptr is NULL, return without taking any action. */
-void PNGAPI
-png_free(png_structp png_ptr, png_voidp ptr)
-{
-   if (png_ptr == NULL || ptr == NULL)
-      return;
-
-#ifdef PNG_USER_MEM_SUPPORTED
-   if (png_ptr->free_fn != NULL)
-   {
-      (*(png_ptr->free_fn))(png_ptr, ptr);
-      return;
-   }
-   else png_free_default(png_ptr, ptr);
-}
-
-void PNGAPI
-png_free_default(png_structp png_ptr, png_voidp ptr)
-{
-#endif /* PNG_USER_MEM_SUPPORTED */
-
-   if(png_ptr == NULL || ptr == NULL) return;
-
-   if (png_ptr->offset_table != NULL)
-   {
-      int i;
-
-      for (i = 0; i < png_ptr->offset_table_count; i++)
-      {
-         if (ptr == png_ptr->offset_table_ptr[i])
-         {
-            ptr = NULL;
-            png_ptr->offset_table_count_free++;
-            break;
-         }
-      }
-      if (png_ptr->offset_table_count_free == png_ptr->offset_table_count)
-      {
-         farfree(png_ptr->offset_table);
-         farfree(png_ptr->offset_table_ptr);
-         png_ptr->offset_table = NULL;
-         png_ptr->offset_table_ptr = NULL;
-      }
-   }
-
-   if (ptr != NULL)
-   {
-      farfree(ptr);
-   }
-}
-
-#else /* Not the Borland DOS special memory handler */
-
-/* Allocate memory for a png_struct or a png_info.  The malloc and
-   memset can be replaced by a single call to calloc() if this is thought
-   to improve performance noticably. */
-png_voidp /* PRIVATE */
-png_create_struct(int type)
-{
-#ifdef PNG_USER_MEM_SUPPORTED
-   return (png_create_struct_2(type, png_malloc_ptr_NULL, png_voidp_NULL));
-}
-
-/* Allocate memory for a png_struct or a png_info.  The malloc and
-   memset can be replaced by a single call to calloc() if this is thought
-   to improve performance noticably. */
-png_voidp /* PRIVATE */
-png_create_struct_2(int type, png_malloc_ptr malloc_fn, png_voidp mem_ptr)
-{
-#endif /* PNG_USER_MEM_SUPPORTED */
-   png_size_t size;
-   png_voidp struct_ptr;
-
-   if (type == PNG_STRUCT_INFO)
-      size = png_sizeof(png_info);
-   else if (type == PNG_STRUCT_PNG)
-      size = png_sizeof(png_struct);
-   else
-      return (NULL);
-
-#ifdef PNG_USER_MEM_SUPPORTED
-   if(malloc_fn != NULL)
-   {
-      png_struct dummy_struct;
-      png_structp png_ptr = &dummy_struct;
-      png_ptr->mem_ptr=mem_ptr;
-      struct_ptr = (*(malloc_fn))(png_ptr, size);
-      if (struct_ptr != NULL)
-         png_memset(struct_ptr, 0, size);
-      return (struct_ptr);
-   }
-#endif /* PNG_USER_MEM_SUPPORTED */
-
-#if defined(__TURBOC__) && !defined(__FLAT__)
-   struct_ptr = (png_voidp)farmalloc(size);
-#else
-# if defined(_MSC_VER) && defined(MAXSEG_64K)
-   struct_ptr = (png_voidp)halloc(size,1);
-# else
-   struct_ptr = (png_voidp)malloc(size);
-# endif
-#endif
-   if (struct_ptr != NULL)
-      png_memset(struct_ptr, 0, size);
-
-   return (struct_ptr);
-}
-
-
-/* Free memory allocated by a png_create_struct() call */
-void /* PRIVATE */
-png_destroy_struct(png_voidp struct_ptr)
-{
-#ifdef PNG_USER_MEM_SUPPORTED
-   png_destroy_struct_2(struct_ptr, png_free_ptr_NULL, png_voidp_NULL);
-}
-
-/* Free memory allocated by a png_create_struct() call */
-void /* PRIVATE */
-png_destroy_struct_2(png_voidp struct_ptr, png_free_ptr free_fn,
-    png_voidp mem_ptr)
-{
-#endif /* PNG_USER_MEM_SUPPORTED */
-   if (struct_ptr != NULL)
-   {
-#ifdef PNG_USER_MEM_SUPPORTED
-      if(free_fn != NULL)
-      {
-         png_struct dummy_struct;
-         png_structp png_ptr = &dummy_struct;
-         png_ptr->mem_ptr=mem_ptr;
-         (*(free_fn))(png_ptr, struct_ptr);
-         return;
-      }
-#endif /* PNG_USER_MEM_SUPPORTED */
-#if defined(__TURBOC__) && !defined(__FLAT__)
-      farfree(struct_ptr);
-#else
-# if defined(_MSC_VER) && defined(MAXSEG_64K)
-      hfree(struct_ptr);
-# else
-      free(struct_ptr);
-# endif
-#endif
-   }
-}
-
-/* Allocate memory.  For reasonable files, size should never exceed
-   64K.  However, zlib may allocate more then 64K if you don't tell
-   it not to.  See zconf.h and png.h for more information.  zlib does
-   need to allocate exactly 64K, so whatever you call here must
-   have the ability to do that. */
-
-png_voidp PNGAPI
-png_malloc(png_structp png_ptr, png_uint_32 size)
-{
-   png_voidp ret;
-
-#ifdef PNG_USER_MEM_SUPPORTED
-   if (png_ptr == NULL || size == 0)
-      return (NULL);
-
-   if(png_ptr->malloc_fn != NULL)
-       ret = ((png_voidp)(*(png_ptr->malloc_fn))(png_ptr, (png_size_t)size));
-   else
-       ret = (png_malloc_default(png_ptr, size));
-   if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
-       png_error(png_ptr, "Out of Memory!");
-   return (ret);
-}
-
-png_voidp PNGAPI
-png_malloc_default(png_structp png_ptr, png_uint_32 size)
-{
-   png_voidp ret;
-#endif /* PNG_USER_MEM_SUPPORTED */
-
-   if (png_ptr == NULL || size == 0)
-      return (NULL);
-
-#ifdef PNG_MAX_MALLOC_64K
-   if (size > (png_uint_32)65536L)
-   {
-#ifndef PNG_USER_MEM_SUPPORTED
-      if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
-         png_error(png_ptr, "Cannot Allocate > 64K");
-      else
-#endif
-         return NULL;
-   }
-#endif
-
- /* Check for overflow */
-#if defined(__TURBOC__) && !defined(__FLAT__)
- if (size != (unsigned long)size)
-   ret = NULL;
- else
-   ret = farmalloc(size);
-#else
-# if defined(_MSC_VER) && defined(MAXSEG_64K)
- if (size != (unsigned long)size)
-   ret = NULL;
- else
-   ret = halloc(size, 1);
-# else
- if (size != (size_t)size)
-   ret = NULL;
- else
-   ret = malloc((size_t)size);
-# endif
-#endif
-
-#ifndef PNG_USER_MEM_SUPPORTED
-   if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
-      png_error(png_ptr, "Out of Memory");
-#endif
-
-   return (ret);
-}
-
-/* Free a pointer allocated by png_malloc().  If ptr is NULL, return
-   without taking any action. */
-void PNGAPI
-png_free(png_structp png_ptr, png_voidp ptr)
-{
-   if (png_ptr == NULL || ptr == NULL)
-      return;
-
-#ifdef PNG_USER_MEM_SUPPORTED
-   if (png_ptr->free_fn != NULL)
-   {
-      (*(png_ptr->free_fn))(png_ptr, ptr);
-      return;
-   }
-   else png_free_default(png_ptr, ptr);
-}
-void PNGAPI
-png_free_default(png_structp png_ptr, png_voidp ptr)
-{
-   if (png_ptr == NULL || ptr == NULL)
-      return;
-
-#endif /* PNG_USER_MEM_SUPPORTED */
-
-#if defined(__TURBOC__) && !defined(__FLAT__)
-   farfree(ptr);
-#else
-# if defined(_MSC_VER) && defined(MAXSEG_64K)
-   hfree(ptr);
-# else
-   free(ptr);
-# endif
-#endif
-}
-
-#endif /* Not Borland DOS special memory handler */
-
-#if defined(PNG_1_0_X)
-#  define png_malloc_warn png_malloc
-#else
-/* This function was added at libpng version 1.2.3.  The png_malloc_warn()
- * function will set up png_malloc() to issue a png_warning and return NULL
- * instead of issuing a png_error, if it fails to allocate the requested
- * memory.
- */
-png_voidp PNGAPI
-png_malloc_warn(png_structp png_ptr, png_uint_32 size)
-{
-   png_voidp ptr;
-   png_uint_32 save_flags;
-   if(png_ptr == NULL) return (NULL);
-
-   save_flags=png_ptr->flags;
-   png_ptr->flags|=PNG_FLAG_MALLOC_NULL_MEM_OK;
-   ptr = (png_voidp)png_malloc((png_structp)png_ptr, size);
-   png_ptr->flags=save_flags;
-   return(ptr);
-}
-#endif
-
-png_voidp PNGAPI
-png_memcpy_check (png_structp png_ptr, png_voidp s1, png_voidp s2,
-   png_uint_32 length)
-{
-   png_size_t size;
-
-   size = (png_size_t)length;
-   if ((png_uint_32)size != length)
-      png_error(png_ptr,"Overflow in png_memcpy_check.");
-
-   return(png_memcpy (s1, s2, size));
-}
-
-png_voidp PNGAPI
-png_memset_check (png_structp png_ptr, png_voidp s1, int value,
-   png_uint_32 length)
-{
-   png_size_t size;
-
-   size = (png_size_t)length;
-   if ((png_uint_32)size != length)
-      png_error(png_ptr,"Overflow in png_memset_check.");
-
-   return (png_memset (s1, value, size));
-
-}
-
-#ifdef PNG_USER_MEM_SUPPORTED
-/* This function is called when the application wants to use another method
- * of allocating and freeing memory.
- */
-void PNGAPI
-png_set_mem_fn(png_structp png_ptr, png_voidp mem_ptr, png_malloc_ptr
-  malloc_fn, png_free_ptr free_fn)
-{
-   if(png_ptr != NULL) {
-   png_ptr->mem_ptr = mem_ptr;
-   png_ptr->malloc_fn = malloc_fn;
-   png_ptr->free_fn = free_fn;
-   }
-}
-
-/* This function returns a pointer to the mem_ptr associated with the user
- * functions.  The application should free any memory associated with this
- * pointer before png_write_destroy and png_read_destroy are called.
- */
-png_voidp PNGAPI
-png_get_mem_ptr(png_structp png_ptr)
-{
-   if(png_ptr == NULL) return (NULL);
-   return ((png_voidp)png_ptr->mem_ptr);
-}
-#endif /* PNG_USER_MEM_SUPPORTED */
-#endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */
+\r
+/* pngmem.c - stub functions for memory allocation\r
+ *\r
+ * Last changed in libpng 1.4.2 [May 6, 2010]\r
+ * Copyright (c) 1998-2010 Glenn Randers-Pehrson\r
+ * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)\r
+ * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)\r
+ *\r
+ * This code is released under the libpng license.\r
+ * For conditions of distribution and use, see the disclaimer\r
+ * and license in png.h\r
+ *\r
+ * This file provides a location for all memory allocation.  Users who\r
+ * need special memory handling are expected to supply replacement\r
+ * functions for png_malloc() and png_free(), and to use\r
+ * png_create_read_struct_2() and png_create_write_struct_2() to\r
+ * identify the replacement functions.\r
+ */\r
+\r
+#define PNG_NO_PEDANTIC_WARNINGS\r
+#include "png.h"\r
+#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)\r
+#include "pngpriv.h"\r
+\r
+/* Borland DOS special memory handler */\r
+#if defined(__TURBOC__) && !defined(_Windows) && !defined(__FLAT__)\r
+/* If you change this, be sure to change the one in png.h also */\r
+\r
+/* Allocate memory for a png_struct.  The malloc and memset can be replaced\r
+   by a single call to calloc() if this is thought to improve performance. */\r
+png_voidp /* PRIVATE */\r
+png_create_struct(int type)\r
+{\r
+#ifdef PNG_USER_MEM_SUPPORTED\r
+   return (png_create_struct_2(type, NULL, NULL));\r
+}\r
+\r
+/* Alternate version of png_create_struct, for use with user-defined malloc. */\r
+png_voidp /* PRIVATE */\r
+png_create_struct_2(int type, png_malloc_ptr malloc_fn, png_voidp mem_ptr)\r
+{\r
+#endif /* PNG_USER_MEM_SUPPORTED */\r
+   png_size_t size;\r
+   png_voidp struct_ptr;\r
+\r
+   if (type == PNG_STRUCT_INFO)\r
+      size = png_sizeof(png_info);\r
+   else if (type == PNG_STRUCT_PNG)\r
+      size = png_sizeof(png_struct);\r
+   else\r
+      return (png_get_copyright(NULL));\r
+\r
+#ifdef PNG_USER_MEM_SUPPORTED\r
+   if (malloc_fn != NULL)\r
+   {\r
+      png_struct dummy_struct;\r
+      png_structp png_ptr = &dummy_struct;\r
+      png_ptr->mem_ptr=mem_ptr;\r
+      struct_ptr = (*(malloc_fn))(png_ptr, (png_uint_32)size);\r
+   }\r
+   else\r
+#endif /* PNG_USER_MEM_SUPPORTED */\r
+   struct_ptr = (png_voidp)farmalloc(size);\r
+   if (struct_ptr != NULL)\r
+      png_memset(struct_ptr, 0, size);\r
+   return (struct_ptr);\r
+}\r
+\r
+/* Free memory allocated by a png_create_struct() call */\r
+void /* PRIVATE */\r
+png_destroy_struct(png_voidp struct_ptr)\r
+{\r
+#ifdef PNG_USER_MEM_SUPPORTED\r
+   png_destroy_struct_2(struct_ptr, NULL, NULL);\r
+}\r
+\r
+/* Free memory allocated by a png_create_struct() call */\r
+void /* PRIVATE */\r
+png_destroy_struct_2(png_voidp struct_ptr, png_free_ptr free_fn,\r
+    png_voidp mem_ptr)\r
+{\r
+#endif\r
+   if (struct_ptr != NULL)\r
+   {\r
+#ifdef PNG_USER_MEM_SUPPORTED\r
+      if (free_fn != NULL)\r
+      {\r
+         png_struct dummy_struct;\r
+         png_structp png_ptr = &dummy_struct;\r
+         png_ptr->mem_ptr=mem_ptr;\r
+         (*(free_fn))(png_ptr, struct_ptr);\r
+         return;\r
+      }\r
+#endif /* PNG_USER_MEM_SUPPORTED */\r
+      farfree (struct_ptr);\r
+   }\r
+}\r
+\r
+/* Allocate memory.  For reasonable files, size should never exceed\r
+ * 64K.  However, zlib may allocate more then 64K if you don't tell\r
+ * it not to.  See zconf.h and png.h for more information. zlib does\r
+ * need to allocate exactly 64K, so whatever you call here must\r
+ * have the ability to do that.\r
+ *\r
+ * Borland seems to have a problem in DOS mode for exactly 64K.\r
+ * It gives you a segment with an offset of 8 (perhaps to store its\r
+ * memory stuff).  zlib doesn't like this at all, so we have to\r
+ * detect and deal with it.  This code should not be needed in\r
+ * Windows or OS/2 modes, and only in 16 bit mode.  This code has\r
+ * been updated by Alexander Lehmann for version 0.89 to waste less\r
+ * memory.\r
+ *\r
+ * Note that we can't use png_size_t for the "size" declaration,\r
+ * since on some systems a png_size_t is a 16-bit quantity, and as a\r
+ * result, we would be truncating potentially larger memory requests\r
+ * (which should cause a fatal error) and introducing major problems.\r
+ */\r
+png_voidp PNGAPI\r
+png_calloc(png_structp png_ptr, png_alloc_size_t size)\r
+{\r
+   png_voidp ret;\r
+\r
+   ret = (png_malloc(png_ptr, size));\r
+   if (ret != NULL)\r
+      png_memset(ret,0,(png_size_t)size);\r
+   return (ret);\r
+}\r
+\r
+png_voidp PNGAPI\r
+png_malloc(png_structp png_ptr, png_alloc_size_t size)\r
+{\r
+   png_voidp ret;\r
+\r
+   if (png_ptr == NULL || size == 0)\r
+      return (NULL);\r
+\r
+#ifdef PNG_USER_MEM_SUPPORTED\r
+   if (png_ptr->malloc_fn != NULL)\r
+      ret = ((png_voidp)(*(png_ptr->malloc_fn))(png_ptr, (png_size_t)size));\r
+   else\r
+      ret = (png_malloc_default(png_ptr, size));\r
+   if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)\r
+       png_error(png_ptr, "Out of memory");\r
+   return (ret);\r
+}\r
+\r
+png_voidp PNGAPI\r
+png_malloc_default(png_structp png_ptr, png_alloc_size_t size)\r
+{\r
+   png_voidp ret;\r
+#endif /* PNG_USER_MEM_SUPPORTED */\r
+\r
+   if (png_ptr == NULL || size == 0)\r
+      return (NULL);\r
+\r
+#ifdef PNG_MAX_MALLOC_64K\r
+   if (size > (png_uint_32)65536L)\r
+   {\r
+      png_warning(png_ptr, "Cannot Allocate > 64K");\r
+      ret = NULL;\r
+   }\r
+   else\r
+#endif\r
+\r
+   if (size != (size_t)size)\r
+      ret = NULL;\r
+   else if (size == (png_uint_32)65536L)\r
+   {\r
+      if (png_ptr->offset_table == NULL)\r
+      {\r
+         /* Try to see if we need to do any of this fancy stuff */\r
+         ret = farmalloc(size);\r
+         if (ret == NULL || ((png_size_t)ret & 0xffff))\r
+         {\r
+            int num_blocks;\r
+            png_uint_32 total_size;\r
+            png_bytep table;\r
+            int i;\r
+            png_byte huge * hptr;\r
+\r
+            if (ret != NULL)\r
+            {\r
+               farfree(ret);\r
+               ret = NULL;\r
+            }\r
+\r
+            if (png_ptr->zlib_window_bits > 14)\r
+               num_blocks = (int)(1 << (png_ptr->zlib_window_bits - 14));\r
+            else\r
+               num_blocks = 1;\r
+            if (png_ptr->zlib_mem_level >= 7)\r
+               num_blocks += (int)(1 << (png_ptr->zlib_mem_level - 7));\r
+            else\r
+               num_blocks++;\r
+\r
+            total_size = ((png_uint_32)65536L) * (png_uint_32)num_blocks+16;\r
+\r
+            table = farmalloc(total_size);\r
+\r
+            if (table == NULL)\r
+            {\r
+#ifndef PNG_USER_MEM_SUPPORTED\r
+               if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)\r
+                  png_error(png_ptr, "Out Of Memory"); /* Note "O", "M" */\r
+               else\r
+                  png_warning(png_ptr, "Out Of Memory");\r
+#endif\r
+               return (NULL);\r
+            }\r
+\r
+            if ((png_size_t)table & 0xfff0)\r
+            {\r
+#ifndef PNG_USER_MEM_SUPPORTED\r
+               if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)\r
+                  png_error(png_ptr,\r
+                    "Farmalloc didn't return normalized pointer");\r
+               else\r
+                  png_warning(png_ptr,\r
+                    "Farmalloc didn't return normalized pointer");\r
+#endif\r
+               return (NULL);\r
+            }\r
+\r
+            png_ptr->offset_table = table;\r
+            png_ptr->offset_table_ptr = farmalloc(num_blocks *\r
+               png_sizeof(png_bytep));\r
+\r
+            if (png_ptr->offset_table_ptr == NULL)\r
+            {\r
+#ifndef PNG_USER_MEM_SUPPORTED\r
+               if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)\r
+                  png_error(png_ptr, "Out Of memory"); /* Note "O", "m" */\r
+               else\r
+                  png_warning(png_ptr, "Out Of memory");\r
+#endif\r
+               return (NULL);\r
+            }\r
+\r
+            hptr = (png_byte huge *)table;\r
+            if ((png_size_t)hptr & 0xf)\r
+            {\r
+               hptr = (png_byte huge *)((long)(hptr) & 0xfffffff0L);\r
+               hptr = hptr + 16L;  /* "hptr += 16L" fails on Turbo C++ 3.0 */\r
+            }\r
+            for (i = 0; i < num_blocks; i++)\r
+            {\r
+               png_ptr->offset_table_ptr[i] = (png_bytep)hptr;\r
+               hptr = hptr + (png_uint_32)65536L;  /* "+=" fails on TC++3.0 */\r
+            }\r
+\r
+            png_ptr->offset_table_number = num_blocks;\r
+            png_ptr->offset_table_count = 0;\r
+            png_ptr->offset_table_count_free = 0;\r
+         }\r
+      }\r
+\r
+      if (png_ptr->offset_table_count >= png_ptr->offset_table_number)\r
+      {\r
+#ifndef PNG_USER_MEM_SUPPORTED\r
+         if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)\r
+            png_error(png_ptr, "Out of Memory"); /* Note "o" and "M" */\r
+         else\r
+            png_warning(png_ptr, "Out of Memory");\r
+#endif\r
+         return (NULL);\r
+      }\r
+\r
+      ret = png_ptr->offset_table_ptr[png_ptr->offset_table_count++];\r
+   }\r
+   else\r
+      ret = farmalloc(size);\r
+\r
+#ifndef PNG_USER_MEM_SUPPORTED\r
+   if (ret == NULL)\r
+   {\r
+      if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)\r
+         png_error(png_ptr, "Out of memory"); /* Note "o" and "m" */\r
+      else\r
+         png_warning(png_ptr, "Out of memory"); /* Note "o" and "m" */\r
+   }\r
+#endif\r
+\r
+   return (ret);\r
+}\r
+\r
+/* Free a pointer allocated by png_malloc().  In the default\r
+ * configuration, png_ptr is not used, but is passed in case it\r
+ * is needed.  If ptr is NULL, return without taking any action.\r
+ */\r
+void PNGAPI\r
+png_free(png_structp png_ptr, png_voidp ptr)\r
+{\r
+   if (png_ptr == NULL || ptr == NULL)\r
+      return;\r
+\r
+#ifdef PNG_USER_MEM_SUPPORTED\r
+   if (png_ptr->free_fn != NULL)\r
+   {\r
+      (*(png_ptr->free_fn))(png_ptr, ptr);\r
+      return;\r
+   }\r
+   else\r
+      png_free_default(png_ptr, ptr);\r
+}\r
+\r
+void PNGAPI\r
+png_free_default(png_structp png_ptr, png_voidp ptr)\r
+{\r
+#endif /* PNG_USER_MEM_SUPPORTED */\r
+\r
+   if (png_ptr == NULL || ptr == NULL)\r
+      return;\r
+\r
+   if (png_ptr->offset_table != NULL)\r
+   {\r
+      int i;\r
+\r
+      for (i = 0; i < png_ptr->offset_table_count; i++)\r
+      {\r
+         if (ptr == png_ptr->offset_table_ptr[i])\r
+         {\r
+            ptr = NULL;\r
+            png_ptr->offset_table_count_free++;\r
+            break;\r
+         }\r
+      }\r
+      if (png_ptr->offset_table_count_free == png_ptr->offset_table_count)\r
+      {\r
+         farfree(png_ptr->offset_table);\r
+         farfree(png_ptr->offset_table_ptr);\r
+         png_ptr->offset_table = NULL;\r
+         png_ptr->offset_table_ptr = NULL;\r
+      }\r
+   }\r
+\r
+   if (ptr != NULL)\r
+   {\r
+      farfree(ptr);\r
+   }\r
+}\r
+\r
+#else /* Not the Borland DOS special memory handler */\r
+\r
+/* Allocate memory for a png_struct or a png_info.  The malloc and\r
+   memset can be replaced by a single call to calloc() if this is thought\r
+   to improve performance noticably. */\r
+png_voidp /* PRIVATE */\r
+png_create_struct(int type)\r
+{\r
+#ifdef PNG_USER_MEM_SUPPORTED\r
+   return (png_create_struct_2(type, NULL, NULL));\r
+}\r
+\r
+/* Allocate memory for a png_struct or a png_info.  The malloc and\r
+   memset can be replaced by a single call to calloc() if this is thought\r
+   to improve performance noticably. */\r
+png_voidp /* PRIVATE */\r
+png_create_struct_2(int type, png_malloc_ptr malloc_fn, png_voidp mem_ptr)\r
+{\r
+#endif /* PNG_USER_MEM_SUPPORTED */\r
+   png_size_t size;\r
+   png_voidp struct_ptr;\r
+\r
+   if (type == PNG_STRUCT_INFO)\r
+      size = png_sizeof(png_info);\r
+   else if (type == PNG_STRUCT_PNG)\r
+      size = png_sizeof(png_struct);\r
+   else\r
+      return (NULL);\r
+\r
+#ifdef PNG_USER_MEM_SUPPORTED\r
+   if (malloc_fn != NULL)\r
+   {\r
+      png_struct dummy_struct;\r
+      png_structp png_ptr = &dummy_struct;\r
+      png_ptr->mem_ptr=mem_ptr;\r
+      struct_ptr = (*(malloc_fn))(png_ptr, size);\r
+      if (struct_ptr != NULL)\r
+         png_memset(struct_ptr, 0, size);\r
+      return (struct_ptr);\r
+   }\r
+#endif /* PNG_USER_MEM_SUPPORTED */\r
+\r
+#if defined(__TURBOC__) && !defined(__FLAT__)\r
+   struct_ptr = (png_voidp)farmalloc(size);\r
+#else\r
+# if defined(_MSC_VER) && defined(MAXSEG_64K)\r
+   struct_ptr = (png_voidp)halloc(size, 1);\r
+# else\r
+   struct_ptr = (png_voidp)malloc(size);\r
+# endif\r
+#endif\r
+   if (struct_ptr != NULL)\r
+      png_memset(struct_ptr, 0, size);\r
+\r
+   return (struct_ptr);\r
+}\r
+\r
+\r
+/* Free memory allocated by a png_create_struct() call */\r
+void /* PRIVATE */\r
+png_destroy_struct(png_voidp struct_ptr)\r
+{\r
+#ifdef PNG_USER_MEM_SUPPORTED\r
+   png_destroy_struct_2(struct_ptr, NULL, NULL);\r
+}\r
+\r
+/* Free memory allocated by a png_create_struct() call */\r
+void /* PRIVATE */\r
+png_destroy_struct_2(png_voidp struct_ptr, png_free_ptr free_fn,\r
+    png_voidp mem_ptr)\r
+{\r
+#endif /* PNG_USER_MEM_SUPPORTED */\r
+   if (struct_ptr != NULL)\r
+   {\r
+#ifdef PNG_USER_MEM_SUPPORTED\r
+      if (free_fn != NULL)\r
+      {\r
+         png_struct dummy_struct;\r
+         png_structp png_ptr = &dummy_struct;\r
+         png_ptr->mem_ptr=mem_ptr;\r
+         (*(free_fn))(png_ptr, struct_ptr);\r
+         return;\r
+      }\r
+#endif /* PNG_USER_MEM_SUPPORTED */\r
+#if defined(__TURBOC__) && !defined(__FLAT__)\r
+      farfree(struct_ptr);\r
+#else\r
+# if defined(_MSC_VER) && defined(MAXSEG_64K)\r
+      hfree(struct_ptr);\r
+# else\r
+      free(struct_ptr);\r
+# endif\r
+#endif\r
+   }\r
+}\r
+\r
+/* Allocate memory.  For reasonable files, size should never exceed\r
+ * 64K.  However, zlib may allocate more then 64K if you don't tell\r
+ * it not to.  See zconf.h and png.h for more information.  zlib does\r
+ * need to allocate exactly 64K, so whatever you call here must\r
+ * have the ability to do that.\r
+ */\r
+\r
+png_voidp PNGAPI\r
+png_calloc(png_structp png_ptr, png_alloc_size_t size)\r
+{\r
+   png_voidp ret;\r
+\r
+   ret = (png_malloc(png_ptr, size));\r
+   if (ret != NULL)\r
+      png_memset(ret,0,(png_size_t)size);\r
+   return (ret);\r
+}\r
+\r
+png_voidp PNGAPI\r
+png_malloc(png_structp png_ptr, png_alloc_size_t size)\r
+{\r
+   png_voidp ret;\r
+\r
+#ifdef PNG_USER_MEM_SUPPORTED\r
+   if (png_ptr == NULL || size == 0)\r
+      return (NULL);\r
+\r
+   if (png_ptr->malloc_fn != NULL)\r
+      ret = ((png_voidp)(*(png_ptr->malloc_fn))(png_ptr, (png_size_t)size));\r
+   else\r
+      ret = (png_malloc_default(png_ptr, size));\r
+   if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)\r
+       png_error(png_ptr, "Out of Memory");\r
+   return (ret);\r
+}\r
+\r
+png_voidp PNGAPI\r
+png_malloc_default(png_structp png_ptr, png_alloc_size_t size)\r
+{\r
+   png_voidp ret;\r
+#endif /* PNG_USER_MEM_SUPPORTED */\r
+\r
+   if (png_ptr == NULL || size == 0)\r
+      return (NULL);\r
+\r
+#ifdef PNG_MAX_MALLOC_64K\r
+   if (size > (png_uint_32)65536L)\r
+   {\r
+#ifndef PNG_USER_MEM_SUPPORTED\r
+      if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)\r
+         png_error(png_ptr, "Cannot Allocate > 64K");\r
+      else\r
+#endif\r
+         return NULL;\r
+   }\r
+#endif\r
+\r
+   /* Check for overflow */\r
+#if defined(__TURBOC__) && !defined(__FLAT__)\r
+   if (size != (unsigned long)size)\r
+      ret = NULL;\r
+   else\r
+      ret = farmalloc(size);\r
+#else\r
+# if defined(_MSC_VER) && defined(MAXSEG_64K)\r
+   if (size != (unsigned long)size)\r
+      ret = NULL;\r
+   else\r
+      ret = halloc(size, 1);\r
+# else\r
+   if (size != (size_t)size)\r
+      ret = NULL;\r
+   else\r
+      ret = malloc((size_t)size);\r
+# endif\r
+#endif\r
+\r
+#ifndef PNG_USER_MEM_SUPPORTED\r
+   if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)\r
+      png_error(png_ptr, "Out of Memory");\r
+#endif\r
+\r
+   return (ret);\r
+}\r
+\r
+/* Free a pointer allocated by png_malloc().  If ptr is NULL, return\r
+ * without taking any action.\r
+ */\r
+void PNGAPI\r
+png_free(png_structp png_ptr, png_voidp ptr)\r
+{\r
+   if (png_ptr == NULL || ptr == NULL)\r
+      return;\r
+\r
+#ifdef PNG_USER_MEM_SUPPORTED\r
+   if (png_ptr->free_fn != NULL)\r
+   {\r
+      (*(png_ptr->free_fn))(png_ptr, ptr);\r
+      return;\r
+   }\r
+   else\r
+      png_free_default(png_ptr, ptr);\r
+}\r
+void PNGAPI\r
+png_free_default(png_structp png_ptr, png_voidp ptr)\r
+{\r
+   if (png_ptr == NULL || ptr == NULL)\r
+      return;\r
+\r
+#endif /* PNG_USER_MEM_SUPPORTED */\r
+\r
+#if defined(__TURBOC__) && !defined(__FLAT__)\r
+   farfree(ptr);\r
+#else\r
+# if defined(_MSC_VER) && defined(MAXSEG_64K)\r
+   hfree(ptr);\r
+# else\r
+   free(ptr);\r
+# endif\r
+#endif\r
+}\r
+\r
+#endif /* Not Borland DOS special memory handler */\r
+\r
+/* This function was added at libpng version 1.2.3.  The png_malloc_warn()\r
+ * function will set up png_malloc() to issue a png_warning and return NULL\r
+ * instead of issuing a png_error, if it fails to allocate the requested\r
+ * memory.\r
+ */\r
+png_voidp PNGAPI\r
+png_malloc_warn(png_structp png_ptr, png_alloc_size_t size)\r
+{\r
+   png_voidp ptr;\r
+   png_uint_32 save_flags;\r
+   if (png_ptr == NULL)\r
+      return (NULL);\r
+\r
+   save_flags = png_ptr->flags;\r
+   png_ptr->flags|=PNG_FLAG_MALLOC_NULL_MEM_OK;\r
+   ptr = (png_voidp)png_malloc((png_structp)png_ptr, size);\r
+   png_ptr->flags=save_flags;\r
+   return(ptr);\r
+}\r
+\r
+\r
+#ifdef PNG_USER_MEM_SUPPORTED\r
+/* This function is called when the application wants to use another method\r
+ * of allocating and freeing memory.\r
+ */\r
+void PNGAPI\r
+png_set_mem_fn(png_structp png_ptr, png_voidp mem_ptr, png_malloc_ptr\r
+  malloc_fn, png_free_ptr free_fn)\r
+{\r
+   if (png_ptr != NULL)\r
+   {\r
+      png_ptr->mem_ptr = mem_ptr;\r
+      png_ptr->malloc_fn = malloc_fn;\r
+      png_ptr->free_fn = free_fn;\r
+   }\r
+}\r
+\r
+/* This function returns a pointer to the mem_ptr associated with the user\r
+ * functions.  The application should free any memory associated with this\r
+ * pointer before png_write_destroy and png_read_destroy are called.\r
+ */\r
+png_voidp PNGAPI\r
+png_get_mem_ptr(png_structp png_ptr)\r
+{\r
+   if (png_ptr == NULL)\r
+      return (NULL);\r
+   return ((png_voidp)png_ptr->mem_ptr);\r
+}\r
+#endif /* PNG_USER_MEM_SUPPORTED */\r
+#endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */\r
index aa7151c..ed5d82c 100644 (file)
-
-/* pngpread.c - read a png file in push mode
- *
- * Last changed in libpng 1.2.27 [April 29, 2008]
- * For conditions of distribution and use, see copyright notice in png.h
- * Copyright (c) 1998-2008 Glenn Randers-Pehrson
- * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
- * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
- */
-
-#define PNG_INTERNAL
-#include "png.h"
-
-#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
-
-/* push model modes */
-#define PNG_READ_SIG_MODE   0
-#define PNG_READ_CHUNK_MODE 1
-#define PNG_READ_IDAT_MODE  2
-#define PNG_SKIP_MODE       3
-#define PNG_READ_tEXt_MODE  4
-#define PNG_READ_zTXt_MODE  5
-#define PNG_READ_DONE_MODE  6
-#define PNG_READ_iTXt_MODE  7
-#define PNG_ERROR_MODE      8
-
-void PNGAPI
-png_process_data(png_structp png_ptr, png_infop info_ptr,
-   png_bytep buffer, png_size_t buffer_size)
-{
-   if(png_ptr == NULL || info_ptr == NULL) return;
-   png_push_restore_buffer(png_ptr, buffer, buffer_size);
-
-   while (png_ptr->buffer_size)
-   {
-      png_process_some_data(png_ptr, info_ptr);
-   }
-}
-
-/* What we do with the incoming data depends on what we were previously
- * doing before we ran out of data...
- */
-void /* PRIVATE */
-png_process_some_data(png_structp png_ptr, png_infop info_ptr)
-{
-   if(png_ptr == NULL) return;
-   switch (png_ptr->process_mode)
-   {
-      case PNG_READ_SIG_MODE:
-      {
-         png_push_read_sig(png_ptr, info_ptr);
-         break;
-      }
-      case PNG_READ_CHUNK_MODE:
-      {
-         png_push_read_chunk(png_ptr, info_ptr);
-         break;
-      }
-      case PNG_READ_IDAT_MODE:
-      {
-         png_push_read_IDAT(png_ptr);
-         break;
-      }
-#if defined(PNG_READ_tEXt_SUPPORTED)
-      case PNG_READ_tEXt_MODE:
-      {
-         png_push_read_tEXt(png_ptr, info_ptr);
-         break;
-      }
-#endif
-#if defined(PNG_READ_zTXt_SUPPORTED)
-      case PNG_READ_zTXt_MODE:
-      {
-         png_push_read_zTXt(png_ptr, info_ptr);
-         break;
-      }
-#endif
-#if defined(PNG_READ_iTXt_SUPPORTED)
-      case PNG_READ_iTXt_MODE:
-      {
-         png_push_read_iTXt(png_ptr, info_ptr);
-         break;
-      }
-#endif
-      case PNG_SKIP_MODE:
-      {
-         png_push_crc_finish(png_ptr);
-         break;
-      }
-      default:
-      {
-         png_ptr->buffer_size = 0;
-         break;
-      }
-   }
-}
-
-/* Read any remaining signature bytes from the stream and compare them with
- * the correct PNG signature.  It is possible that this routine is called
- * with bytes already read from the signature, either because they have been
- * checked by the calling application, or because of multiple calls to this
- * routine.
- */
-void /* PRIVATE */
-png_push_read_sig(png_structp png_ptr, png_infop info_ptr)
-{
-   png_size_t num_checked = png_ptr->sig_bytes,
-             num_to_check = 8 - num_checked;
-
-   if (png_ptr->buffer_size < num_to_check)
-   {
-      num_to_check = png_ptr->buffer_size;
-   }
-
-   png_push_fill_buffer(png_ptr, &(info_ptr->signature[num_checked]),
-      num_to_check);
-   png_ptr->sig_bytes = (png_byte)(png_ptr->sig_bytes+num_to_check);
-
-   if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check))
-   {
-      if (num_checked < 4 &&
-          png_sig_cmp(info_ptr->signature, num_checked, num_to_check - 4))
-         png_error(png_ptr, "Not a PNG file");
-      else
-         png_error(png_ptr, "PNG file corrupted by ASCII conversion");
-   }
-   else
-   {
-      if (png_ptr->sig_bytes >= 8)
-      {
-         png_ptr->process_mode = PNG_READ_CHUNK_MODE;
-      }
-   }
-}
-
-void /* PRIVATE */
-png_push_read_chunk(png_structp png_ptr, png_infop info_ptr)
-{
-#ifdef PNG_USE_LOCAL_ARRAYS
-      PNG_CONST PNG_IHDR;
-      PNG_CONST PNG_IDAT;
-      PNG_CONST PNG_IEND;
-      PNG_CONST PNG_PLTE;
-#if defined(PNG_READ_bKGD_SUPPORTED)
-      PNG_CONST PNG_bKGD;
-#endif
-#if defined(PNG_READ_cHRM_SUPPORTED)
-      PNG_CONST PNG_cHRM;
-#endif
-#if defined(PNG_READ_gAMA_SUPPORTED)
-      PNG_CONST PNG_gAMA;
-#endif
-#if defined(PNG_READ_hIST_SUPPORTED)
-      PNG_CONST PNG_hIST;
-#endif
-#if defined(PNG_READ_iCCP_SUPPORTED)
-      PNG_CONST PNG_iCCP;
-#endif
-#if defined(PNG_READ_iTXt_SUPPORTED)
-      PNG_CONST PNG_iTXt;
-#endif
-#if defined(PNG_READ_oFFs_SUPPORTED)
-      PNG_CONST PNG_oFFs;
-#endif
-#if defined(PNG_READ_pCAL_SUPPORTED)
-      PNG_CONST PNG_pCAL;
-#endif
-#if defined(PNG_READ_pHYs_SUPPORTED)
-      PNG_CONST PNG_pHYs;
-#endif
-#if defined(PNG_READ_sBIT_SUPPORTED)
-      PNG_CONST PNG_sBIT;
-#endif
-#if defined(PNG_READ_sCAL_SUPPORTED)
-      PNG_CONST PNG_sCAL;
-#endif
-#if defined(PNG_READ_sRGB_SUPPORTED)
-      PNG_CONST PNG_sRGB;
-#endif
-#if defined(PNG_READ_sPLT_SUPPORTED)
-      PNG_CONST PNG_sPLT;
-#endif
-#if defined(PNG_READ_tEXt_SUPPORTED)
-      PNG_CONST PNG_tEXt;
-#endif
-#if defined(PNG_READ_tIME_SUPPORTED)
-      PNG_CONST PNG_tIME;
-#endif
-#if defined(PNG_READ_tRNS_SUPPORTED)
-      PNG_CONST PNG_tRNS;
-#endif
-#if defined(PNG_READ_zTXt_SUPPORTED)
-      PNG_CONST PNG_zTXt;
-#endif
-#endif /* PNG_USE_LOCAL_ARRAYS */
-   /* First we make sure we have enough data for the 4 byte chunk name
-    * and the 4 byte chunk length before proceeding with decoding the
-    * chunk data.  To fully decode each of these chunks, we also make
-    * sure we have enough data in the buffer for the 4 byte CRC at the
-    * end of every chunk (except IDAT, which is handled separately).
-    */
-   if (!(png_ptr->mode & PNG_HAVE_CHUNK_HEADER))
-   {
-      png_byte chunk_length[4];
-
-      if (png_ptr->buffer_size < 8)
-      {
-         png_push_save_buffer(png_ptr);
-         return;
-      }
-
-      png_push_fill_buffer(png_ptr, chunk_length, 4);
-      png_ptr->push_length = png_get_uint_31(png_ptr,chunk_length);
-      png_reset_crc(png_ptr);
-      png_crc_read(png_ptr, png_ptr->chunk_name, 4);
-      png_ptr->mode |= PNG_HAVE_CHUNK_HEADER;
-   }
-
-   if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
-     if(png_ptr->mode & PNG_AFTER_IDAT)
-        png_ptr->mode |= PNG_HAVE_CHUNK_AFTER_IDAT;
-
-   if (!png_memcmp(png_ptr->chunk_name, png_IHDR, 4))
-   {
-      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
-      {
-         if (png_ptr->push_length != 13)
-            png_error(png_ptr, "Invalid IHDR length");
-         png_push_save_buffer(png_ptr);
-         return;
-      }
-      png_handle_IHDR(png_ptr, info_ptr, png_ptr->push_length);
-   }
-   else if (!png_memcmp(png_ptr->chunk_name, png_IEND, 4))
-   {
-      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
-      {
-         png_push_save_buffer(png_ptr);
-         return;
-      }
-      png_handle_IEND(png_ptr, info_ptr, png_ptr->push_length);
-
-      png_ptr->process_mode = PNG_READ_DONE_MODE;
-      png_push_have_end(png_ptr, info_ptr);
-   }
-#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
-   else if (png_handle_as_unknown(png_ptr, png_ptr->chunk_name))
-   {
-      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
-      {
-         png_push_save_buffer(png_ptr);
-         return;
-      }
-      if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
-         png_ptr->mode |= PNG_HAVE_IDAT;
-      png_handle_unknown(png_ptr, info_ptr, png_ptr->push_length);
-      if (!png_memcmp(png_ptr->chunk_name, png_PLTE, 4))
-         png_ptr->mode |= PNG_HAVE_PLTE;
-      else if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
-      {
-         if (!(png_ptr->mode & PNG_HAVE_IHDR))
-            png_error(png_ptr, "Missing IHDR before IDAT");
-         else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
-                  !(png_ptr->mode & PNG_HAVE_PLTE))
-            png_error(png_ptr, "Missing PLTE before IDAT");
-      }
-   }
-#endif
-   else if (!png_memcmp(png_ptr->chunk_name, png_PLTE, 4))
-   {
-      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
-      {
-         png_push_save_buffer(png_ptr);
-         return;
-      }
-      png_handle_PLTE(png_ptr, info_ptr, png_ptr->push_length);
-   }
-   else if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
-   {
-      /* If we reach an IDAT chunk, this means we have read all of the
-       * header chunks, and we can start reading the image (or if this
-       * is called after the image has been read - we have an error).
-       */
-     if (!(png_ptr->mode & PNG_HAVE_IHDR))
-       png_error(png_ptr, "Missing IHDR before IDAT");
-     else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
-         !(png_ptr->mode & PNG_HAVE_PLTE))
-       png_error(png_ptr, "Missing PLTE before IDAT");
-
-      if (png_ptr->mode & PNG_HAVE_IDAT)
-      {
-         if (!(png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT))
-           if (png_ptr->push_length == 0)
-              return;
-
-         if (png_ptr->mode & PNG_AFTER_IDAT)
-            png_error(png_ptr, "Too many IDAT's found");
-      }
-
-      png_ptr->idat_size = png_ptr->push_length;
-      png_ptr->mode |= PNG_HAVE_IDAT;
-      png_ptr->process_mode = PNG_READ_IDAT_MODE;
-      png_push_have_info(png_ptr, info_ptr);
-      png_ptr->zstream.avail_out = (uInt)png_ptr->irowbytes;
-      png_ptr->zstream.next_out = png_ptr->row_buf;
-      return;
-   }
-#if defined(PNG_READ_gAMA_SUPPORTED)
-   else if (!png_memcmp(png_ptr->chunk_name, png_gAMA, 4))
-   {
-      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
-      {
-         png_push_save_buffer(png_ptr);
-         return;
-      }
-      png_handle_gAMA(png_ptr, info_ptr, png_ptr->push_length);
-   }
-#endif
-#if defined(PNG_READ_sBIT_SUPPORTED)
-   else if (!png_memcmp(png_ptr->chunk_name, png_sBIT, 4))
-   {
-      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
-      {
-         png_push_save_buffer(png_ptr);
-         return;
-      }
-      png_handle_sBIT(png_ptr, info_ptr, png_ptr->push_length);
-   }
-#endif
-#if defined(PNG_READ_cHRM_SUPPORTED)
-   else if (!png_memcmp(png_ptr->chunk_name, png_cHRM, 4))
-   {
-      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
-      {
-         png_push_save_buffer(png_ptr);
-         return;
-      }
-      png_handle_cHRM(png_ptr, info_ptr, png_ptr->push_length);
-   }
-#endif
-#if defined(PNG_READ_sRGB_SUPPORTED)
-   else if (!png_memcmp(png_ptr->chunk_name, png_sRGB, 4))
-   {
-      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
-      {
-         png_push_save_buffer(png_ptr);
-         return;
-      }
-      png_handle_sRGB(png_ptr, info_ptr, png_ptr->push_length);
-   }
-#endif
-#if defined(PNG_READ_iCCP_SUPPORTED)
-   else if (!png_memcmp(png_ptr->chunk_name, png_iCCP, 4))
-   {
-      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
-      {
-         png_push_save_buffer(png_ptr);
-         return;
-      }
-      png_handle_iCCP(png_ptr, info_ptr, png_ptr->push_length);
-   }
-#endif
-#if defined(PNG_READ_sPLT_SUPPORTED)
-   else if (!png_memcmp(png_ptr->chunk_name, png_sPLT, 4))
-   {
-      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
-      {
-         png_push_save_buffer(png_ptr);
-         return;
-      }
-      png_handle_sPLT(png_ptr, info_ptr, png_ptr->push_length);
-   }
-#endif
-#if defined(PNG_READ_tRNS_SUPPORTED)
-   else if (!png_memcmp(png_ptr->chunk_name, png_tRNS, 4))
-   {
-      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
-      {
-         png_push_save_buffer(png_ptr);
-         return;
-      }
-      png_handle_tRNS(png_ptr, info_ptr, png_ptr->push_length);
-   }
-#endif
-#if defined(PNG_READ_bKGD_SUPPORTED)
-   else if (!png_memcmp(png_ptr->chunk_name, png_bKGD, 4))
-   {
-      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
-      {
-         png_push_save_buffer(png_ptr);
-         return;
-      }
-      png_handle_bKGD(png_ptr, info_ptr, png_ptr->push_length);
-   }
-#endif
-#if defined(PNG_READ_hIST_SUPPORTED)
-   else if (!png_memcmp(png_ptr->chunk_name, png_hIST, 4))
-   {
-      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
-      {
-         png_push_save_buffer(png_ptr);
-         return;
-      }
-      png_handle_hIST(png_ptr, info_ptr, png_ptr->push_length);
-   }
-#endif
-#if defined(PNG_READ_pHYs_SUPPORTED)
-   else if (!png_memcmp(png_ptr->chunk_name, png_pHYs, 4))
-   {
-      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
-      {
-         png_push_save_buffer(png_ptr);
-         return;
-      }
-      png_handle_pHYs(png_ptr, info_ptr, png_ptr->push_length);
-   }
-#endif
-#if defined(PNG_READ_oFFs_SUPPORTED)
-   else if (!png_memcmp(png_ptr->chunk_name, png_oFFs, 4))
-   {
-      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
-      {
-         png_push_save_buffer(png_ptr);
-         return;
-      }
-      png_handle_oFFs(png_ptr, info_ptr, png_ptr->push_length);
-   }
-#endif
-#if defined(PNG_READ_pCAL_SUPPORTED)
-   else if (!png_memcmp(png_ptr->chunk_name, png_pCAL, 4))
-   {
-      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
-      {
-         png_push_save_buffer(png_ptr);
-         return;
-      }
-      png_handle_pCAL(png_ptr, info_ptr, png_ptr->push_length);
-   }
-#endif
-#if defined(PNG_READ_sCAL_SUPPORTED)
-   else if (!png_memcmp(png_ptr->chunk_name, png_sCAL, 4))
-   {
-      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
-      {
-         png_push_save_buffer(png_ptr);
-         return;
-      }
-      png_handle_sCAL(png_ptr, info_ptr, png_ptr->push_length);
-   }
-#endif
-#if defined(PNG_READ_tIME_SUPPORTED)
-   else if (!png_memcmp(png_ptr->chunk_name, png_tIME, 4))
-   {
-      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
-      {
-         png_push_save_buffer(png_ptr);
-         return;
-      }
-      png_handle_tIME(png_ptr, info_ptr, png_ptr->push_length);
-   }
-#endif
-#if defined(PNG_READ_tEXt_SUPPORTED)
-   else if (!png_memcmp(png_ptr->chunk_name, png_tEXt, 4))
-   {
-      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
-      {
-         png_push_save_buffer(png_ptr);
-         return;
-      }
-      png_push_handle_tEXt(png_ptr, info_ptr, png_ptr->push_length);
-   }
-#endif
-#if defined(PNG_READ_zTXt_SUPPORTED)
-   else if (!png_memcmp(png_ptr->chunk_name, png_zTXt, 4))
-   {
-      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
-      {
-         png_push_save_buffer(png_ptr);
-         return;
-      }
-      png_push_handle_zTXt(png_ptr, info_ptr, png_ptr->push_length);
-   }
-#endif
-#if defined(PNG_READ_iTXt_SUPPORTED)
-   else if (!png_memcmp(png_ptr->chunk_name, png_iTXt, 4))
-   {
-      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
-      {
-         png_push_save_buffer(png_ptr);
-         return;
-      }
-      png_push_handle_iTXt(png_ptr, info_ptr, png_ptr->push_length);
-   }
-#endif
-   else
-   {
-      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
-      {
-         png_push_save_buffer(png_ptr);
-         return;
-      }
-      png_push_handle_unknown(png_ptr, info_ptr, png_ptr->push_length);
-   }
-
-   png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
-}
-
-void /* PRIVATE */
-png_push_crc_skip(png_structp png_ptr, png_uint_32 skip)
-{
-   png_ptr->process_mode = PNG_SKIP_MODE;
-   png_ptr->skip_length = skip;
-}
-
-void /* PRIVATE */
-png_push_crc_finish(png_structp png_ptr)
-{
-   if (png_ptr->skip_length && png_ptr->save_buffer_size)
-   {
-      png_size_t save_size;
-
-      if (png_ptr->skip_length < (png_uint_32)png_ptr->save_buffer_size)
-         save_size = (png_size_t)png_ptr->skip_length;
-      else
-         save_size = png_ptr->save_buffer_size;
-
-      png_calculate_crc(png_ptr, png_ptr->save_buffer_ptr, save_size);
-
-      png_ptr->skip_length -= save_size;
-      png_ptr->buffer_size -= save_size;
-      png_ptr->save_buffer_size -= save_size;
-      png_ptr->save_buffer_ptr += save_size;
-   }
-   if (png_ptr->skip_length && png_ptr->current_buffer_size)
-   {
-      png_size_t save_size;
-
-      if (png_ptr->skip_length < (png_uint_32)png_ptr->current_buffer_size)
-         save_size = (png_size_t)png_ptr->skip_length;
-      else
-         save_size = png_ptr->current_buffer_size;
-
-      png_calculate_crc(png_ptr, png_ptr->current_buffer_ptr, save_size);
-
-      png_ptr->skip_length -= save_size;
-      png_ptr->buffer_size -= save_size;
-      png_ptr->current_buffer_size -= save_size;
-      png_ptr->current_buffer_ptr += save_size;
-   }
-   if (!png_ptr->skip_length)
-   {
-      if (png_ptr->buffer_size < 4)
-      {
-         png_push_save_buffer(png_ptr);
-         return;
-      }
-
-      png_crc_finish(png_ptr, 0);
-      png_ptr->process_mode = PNG_READ_CHUNK_MODE;
-   }
-}
-
-void PNGAPI
-png_push_fill_buffer(png_structp png_ptr, png_bytep buffer, png_size_t length)
-{
-   png_bytep ptr;
-
-   if(png_ptr == NULL) return;
-   ptr = buffer;
-   if (png_ptr->save_buffer_size)
-   {
-      png_size_t save_size;
-
-      if (length < png_ptr->save_buffer_size)
-         save_size = length;
-      else
-         save_size = png_ptr->save_buffer_size;
-
-      png_memcpy(ptr, png_ptr->save_buffer_ptr, save_size);
-      length -= save_size;
-      ptr += save_size;
-      png_ptr->buffer_size -= save_size;
-      png_ptr->save_buffer_size -= save_size;
-      png_ptr->save_buffer_ptr += save_size;
-   }
-   if (length && png_ptr->current_buffer_size)
-   {
-      png_size_t save_size;
-
-      if (length < png_ptr->current_buffer_size)
-         save_size = length;
-      else
-         save_size = png_ptr->current_buffer_size;
-
-      png_memcpy(ptr, png_ptr->current_buffer_ptr, save_size);
-      png_ptr->buffer_size -= save_size;
-      png_ptr->current_buffer_size -= save_size;
-      png_ptr->current_buffer_ptr += save_size;
-   }
-}
-
-void /* PRIVATE */
-png_push_save_buffer(png_structp png_ptr)
-{
-   if (png_ptr->save_buffer_size)
-   {
-      if (png_ptr->save_buffer_ptr != png_ptr->save_buffer)
-      {
-         png_size_t i,istop;
-         png_bytep sp;
-         png_bytep dp;
-
-         istop = png_ptr->save_buffer_size;
-         for (i = 0, sp = png_ptr->save_buffer_ptr, dp = png_ptr->save_buffer;
-            i < istop; i++, sp++, dp++)
-         {
-            *dp = *sp;
-         }
-      }
-   }
-   if (png_ptr->save_buffer_size + png_ptr->current_buffer_size >
-      png_ptr->save_buffer_max)
-   {
-      png_size_t new_max;
-      png_bytep old_buffer;
-
-      if (png_ptr->save_buffer_size > PNG_SIZE_MAX -
-         (png_ptr->current_buffer_size + 256))
-      {
-        png_error(png_ptr, "Potential overflow of save_buffer");
-      }
-      new_max = png_ptr->save_buffer_size + png_ptr->current_buffer_size + 256;
-      old_buffer = png_ptr->save_buffer;
-      png_ptr->save_buffer = (png_bytep)png_malloc(png_ptr,
-         (png_uint_32)new_max);
-      png_memcpy(png_ptr->save_buffer, old_buffer, png_ptr->save_buffer_size);
-      png_free(png_ptr, old_buffer);
-      png_ptr->save_buffer_max = new_max;
-   }
-   if (png_ptr->current_buffer_size)
-   {
-      png_memcpy(png_ptr->save_buffer + png_ptr->save_buffer_size,
-         png_ptr->current_buffer_ptr, png_ptr->current_buffer_size);
-      png_ptr->save_buffer_size += png_ptr->current_buffer_size;
-      png_ptr->current_buffer_size = 0;
-   }
-   png_ptr->save_buffer_ptr = png_ptr->save_buffer;
-   png_ptr->buffer_size = 0;
-}
-
-void /* PRIVATE */
-png_push_restore_buffer(png_structp png_ptr, png_bytep buffer,
-   png_size_t buffer_length)
-{
-   png_ptr->current_buffer = buffer;
-   png_ptr->current_buffer_size = buffer_length;
-   png_ptr->buffer_size = buffer_length + png_ptr->save_buffer_size;
-   png_ptr->current_buffer_ptr = png_ptr->current_buffer;
-}
-
-void /* PRIVATE */
-png_push_read_IDAT(png_structp png_ptr)
-{
-#ifdef PNG_USE_LOCAL_ARRAYS
-   PNG_CONST PNG_IDAT;
-#endif
-   if (!(png_ptr->mode & PNG_HAVE_CHUNK_HEADER))
-   {
-      png_byte chunk_length[4];
-
-      if (png_ptr->buffer_size < 8)
-      {
-         png_push_save_buffer(png_ptr);
-         return;
-      }
-
-      png_push_fill_buffer(png_ptr, chunk_length, 4);
-      png_ptr->push_length = png_get_uint_31(png_ptr,chunk_length);
-      png_reset_crc(png_ptr);
-      png_crc_read(png_ptr, png_ptr->chunk_name, 4);
-      png_ptr->mode |= PNG_HAVE_CHUNK_HEADER;
-
-      if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
-      {
-         png_ptr->process_mode = PNG_READ_CHUNK_MODE;
-         if (!(png_ptr->flags & PNG_FLAG_ZLIB_FINISHED))
-            png_error(png_ptr, "Not enough compressed data");
-         return;
-      }
-
-      png_ptr->idat_size = png_ptr->push_length;
-   }
-   if (png_ptr->idat_size && png_ptr->save_buffer_size)
-   {
-      png_size_t save_size;
-
-      if (png_ptr->idat_size < (png_uint_32)png_ptr->save_buffer_size)
-      {
-         save_size = (png_size_t)png_ptr->idat_size;
-         /* check for overflow */
-         if((png_uint_32)save_size != png_ptr->idat_size)
-            png_error(png_ptr, "save_size overflowed in pngpread");
-      }
-      else
-         save_size = png_ptr->save_buffer_size;
-
-      png_calculate_crc(png_ptr, png_ptr->save_buffer_ptr, save_size);
-      if (!(png_ptr->flags & PNG_FLAG_ZLIB_FINISHED))
-         png_process_IDAT_data(png_ptr, png_ptr->save_buffer_ptr, save_size);
-      png_ptr->idat_size -= save_size;
-      png_ptr->buffer_size -= save_size;
-      png_ptr->save_buffer_size -= save_size;
-      png_ptr->save_buffer_ptr += save_size;
-   }
-   if (png_ptr->idat_size && png_ptr->current_buffer_size)
-   {
-      png_size_t save_size;
-
-      if (png_ptr->idat_size < (png_uint_32)png_ptr->current_buffer_size)
-      {
-         save_size = (png_size_t)png_ptr->idat_size;
-         /* check for overflow */
-         if((png_uint_32)save_size != png_ptr->idat_size)
-            png_error(png_ptr, "save_size overflowed in pngpread");
-      }
-      else
-         save_size = png_ptr->current_buffer_size;
-
-      png_calculate_crc(png_ptr, png_ptr->current_buffer_ptr, save_size);
-      if (!(png_ptr->flags & PNG_FLAG_ZLIB_FINISHED))
-        png_process_IDAT_data(png_ptr, png_ptr->current_buffer_ptr, save_size);
-
-      png_ptr->idat_size -= save_size;
-      png_ptr->buffer_size -= save_size;
-      png_ptr->current_buffer_size -= save_size;
-      png_ptr->current_buffer_ptr += save_size;
-   }
-   if (!png_ptr->idat_size)
-   {
-      if (png_ptr->buffer_size < 4)
-      {
-         png_push_save_buffer(png_ptr);
-         return;
-      }
-
-      png_crc_finish(png_ptr, 0);
-      png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
-      png_ptr->mode |= PNG_AFTER_IDAT;
-   }
-}
-
-void /* PRIVATE */
-png_process_IDAT_data(png_structp png_ptr, png_bytep buffer,
-   png_size_t buffer_length)
-{
-   int ret;
-
-   if ((png_ptr->flags & PNG_FLAG_ZLIB_FINISHED) && buffer_length)
-      png_error(png_ptr, "Extra compression data");
-
-   png_ptr->zstream.next_in = buffer;
-   png_ptr->zstream.avail_in = (uInt)buffer_length;
-   for(;;)
-   {
-      ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);
-      if (ret != Z_OK)
-      {
-         if (ret == Z_STREAM_END)
-         {
-            if (png_ptr->zstream.avail_in)
-               png_error(png_ptr, "Extra compressed data");
-            if (!(png_ptr->zstream.avail_out))
-            {
-               png_push_process_row(png_ptr);
-            }
-
-            png_ptr->mode |= PNG_AFTER_IDAT;
-            png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
-            break;
-         }
-         else if (ret == Z_BUF_ERROR)
-            break;
-         else
-            png_error(png_ptr, "Decompression Error");
-      }
-      if (!(png_ptr->zstream.avail_out))
-      {
-         if ((
-#if defined(PNG_READ_INTERLACING_SUPPORTED)
-             png_ptr->interlaced && png_ptr->pass > 6) ||
-             (!png_ptr->interlaced &&
-#endif
-             png_ptr->row_number == png_ptr->num_rows))
-         {
-           if (png_ptr->zstream.avail_in)
-             png_warning(png_ptr, "Too much data in IDAT chunks");
-           png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
-           break;
-         }
-         png_push_process_row(png_ptr);
-         png_ptr->zstream.avail_out = (uInt)png_ptr->irowbytes;
-         png_ptr->zstream.next_out = png_ptr->row_buf;
-      }
-      else
-         break;
-   }
-}
-
-void /* PRIVATE */
-png_push_process_row(png_structp png_ptr)
-{
-   png_ptr->row_info.color_type = png_ptr->color_type;
-   png_ptr->row_info.width = png_ptr->iwidth;
-   png_ptr->row_info.channels = png_ptr->channels;
-   png_ptr->row_info.bit_depth = png_ptr->bit_depth;
-   png_ptr->row_info.pixel_depth = png_ptr->pixel_depth;
-
-   png_ptr->row_info.rowbytes = PNG_ROWBYTES(png_ptr->row_info.pixel_depth,
-       png_ptr->row_info.width);
-
-   png_read_filter_row(png_ptr, &(png_ptr->row_info),
-      png_ptr->row_buf + 1, png_ptr->prev_row + 1,
-      (int)(png_ptr->row_buf[0]));
-
-   png_memcpy_check(png_ptr, png_ptr->prev_row, png_ptr->row_buf,
-      png_ptr->rowbytes + 1);
-
-   if (png_ptr->transformations || (png_ptr->flags&PNG_FLAG_STRIP_ALPHA))
-      png_do_read_transformations(png_ptr);
-
-#if defined(PNG_READ_INTERLACING_SUPPORTED)
-   /* blow up interlaced rows to full size */
-   if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
-   {
-      if (png_ptr->pass < 6)
-/*       old interface (pre-1.0.9):
-         png_do_read_interlace(&(png_ptr->row_info),
-            png_ptr->row_buf + 1, png_ptr->pass, png_ptr->transformations);
- */
-         png_do_read_interlace(png_ptr);
-
-    switch (png_ptr->pass)
-    {
-         case 0:
-         {
-            int i;
-            for (i = 0; i < 8 && png_ptr->pass == 0; i++)
-            {
-               png_push_have_row(png_ptr, png_ptr->row_buf + 1);
-               png_read_push_finish_row(png_ptr); /* updates png_ptr->pass */
-            }
-            if (png_ptr->pass == 2) /* pass 1 might be empty */
-            {
-               for (i = 0; i < 4 && png_ptr->pass == 2; i++)
-               {
-                  png_push_have_row(png_ptr, png_bytep_NULL);
-                  png_read_push_finish_row(png_ptr);
-               }
-            }
-            if (png_ptr->pass == 4 && png_ptr->height <= 4)
-            {
-               for (i = 0; i < 2 && png_ptr->pass == 4; i++)
-               {
-                  png_push_have_row(png_ptr, png_bytep_NULL);
-                  png_read_push_finish_row(png_ptr);
-               }
-            }
-            if (png_ptr->pass == 6 && png_ptr->height <= 4)
-            {
-                png_push_have_row(png_ptr, png_bytep_NULL);
-                png_read_push_finish_row(png_ptr);
-            }
-            break;
-         }
-         case 1:
-         {
-            int i;
-            for (i = 0; i < 8 && png_ptr->pass == 1; i++)
-            {
-               png_push_have_row(png_ptr, png_ptr->row_buf + 1);
-               png_read_push_finish_row(png_ptr);
-            }
-            if (png_ptr->pass == 2) /* skip top 4 generated rows */
-            {
-               for (i = 0; i < 4 && png_ptr->pass == 2; i++)
-               {
-                  png_push_have_row(png_ptr, png_bytep_NULL);
-                  png_read_push_finish_row(png_ptr);
-               }
-            }
-            break;
-         }
-         case 2:
-         {
-            int i;
-            for (i = 0; i < 4 && png_ptr->pass == 2; i++)
-            {
-               png_push_have_row(png_ptr, png_ptr->row_buf + 1);
-               png_read_push_finish_row(png_ptr);
-            }
-            for (i = 0; i < 4 && png_ptr->pass == 2; i++)
-            {
-               png_push_have_row(png_ptr, png_bytep_NULL);
-               png_read_push_finish_row(png_ptr);
-            }
-            if (png_ptr->pass == 4) /* pass 3 might be empty */
-            {
-               for (i = 0; i < 2 && png_ptr->pass == 4; i++)
-               {
-                  png_push_have_row(png_ptr, png_bytep_NULL);
-                  png_read_push_finish_row(png_ptr);
-               }
-            }
-            break;
-         }
-         case 3:
-         {
-            int i;
-            for (i = 0; i < 4 && png_ptr->pass == 3; i++)
-            {
-               png_push_have_row(png_ptr, png_ptr->row_buf + 1);
-               png_read_push_finish_row(png_ptr);
-            }
-            if (png_ptr->pass == 4) /* skip top two generated rows */
-            {
-               for (i = 0; i < 2 && png_ptr->pass == 4; i++)
-               {
-                  png_push_have_row(png_ptr, png_bytep_NULL);
-                  png_read_push_finish_row(png_ptr);
-               }
-            }
-            break;
-         }
-         case 4:
-         {
-            int i;
-            for (i = 0; i < 2 && png_ptr->pass == 4; i++)
-            {
-               png_push_have_row(png_ptr, png_ptr->row_buf + 1);
-               png_read_push_finish_row(png_ptr);
-            }
-            for (i = 0; i < 2 && png_ptr->pass == 4; i++)
-            {
-               png_push_have_row(png_ptr, png_bytep_NULL);
-               png_read_push_finish_row(png_ptr);
-            }
-            if (png_ptr->pass == 6) /* pass 5 might be empty */
-            {
-               png_push_have_row(png_ptr, png_bytep_NULL);
-               png_read_push_finish_row(png_ptr);
-            }
-            break;
-         }
-         case 5:
-         {
-            int i;
-            for (i = 0; i < 2 && png_ptr->pass == 5; i++)
-            {
-               png_push_have_row(png_ptr, png_ptr->row_buf + 1);
-               png_read_push_finish_row(png_ptr);
-            }
-            if (png_ptr->pass == 6) /* skip top generated row */
-            {
-               png_push_have_row(png_ptr, png_bytep_NULL);
-               png_read_push_finish_row(png_ptr);
-            }
-            break;
-         }
-         case 6:
-         {
-            png_push_have_row(png_ptr, png_ptr->row_buf + 1);
-            png_read_push_finish_row(png_ptr);
-            if (png_ptr->pass != 6)
-               break;
-            png_push_have_row(png_ptr, png_bytep_NULL);
-            png_read_push_finish_row(png_ptr);
-         }
-      }
-   }
-   else
-#endif
-   {
-      png_push_have_row(png_ptr, png_ptr->row_buf + 1);
-      png_read_push_finish_row(png_ptr);
-   }
-}
-
-void /* PRIVATE */
-png_read_push_finish_row(png_structp png_ptr)
-{
-#ifdef PNG_USE_LOCAL_ARRAYS
-   /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
-
-   /* start of interlace block */
-   PNG_CONST int FARDATA png_pass_start[] = {0, 4, 0, 2, 0, 1, 0};
-
-   /* offset to next interlace block */
-   PNG_CONST int FARDATA png_pass_inc[] = {8, 8, 4, 4, 2, 2, 1};
-
-   /* start of interlace block in the y direction */
-   PNG_CONST int FARDATA png_pass_ystart[] = {0, 0, 4, 0, 2, 0, 1};
-
-   /* offset to next interlace block in the y direction */
-   PNG_CONST int FARDATA png_pass_yinc[] = {8, 8, 8, 4, 4, 2, 2};
-
-   /* Height of interlace block.  This is not currently used - if you need
-    * it, uncomment it here and in png.h
-   PNG_CONST int FARDATA png_pass_height[] = {8, 8, 4, 4, 2, 2, 1};
-   */
-#endif
-
-   png_ptr->row_number++;
-   if (png_ptr->row_number < png_ptr->num_rows)
-      return;
-
-   if (png_ptr->interlaced)
-   {
-      png_ptr->row_number = 0;
-      png_memset_check(png_ptr, png_ptr->prev_row, 0,
-         png_ptr->rowbytes + 1);
-      do
-      {
-         int pass;
-         pass = png_ptr->pass;
-         pass++;
-         if ((pass == 1 && png_ptr->width < 5) ||
-             (pass == 3 && png_ptr->width < 3) ||
-             (pass == 5 && png_ptr->width < 2))
-           pass++;
-
-         if (pass > 7)
-            pass--;
-         png_ptr->pass = (png_byte) pass;
-         if (pass < 7)
-           {
-             png_ptr->iwidth = (png_ptr->width +
-                png_pass_inc[pass] - 1 -
-                png_pass_start[pass]) /
-                png_pass_inc[pass];
-
-             png_ptr->irowbytes = PNG_ROWBYTES(png_ptr->pixel_depth,
-                png_ptr->iwidth) + 1;
-
-             if (png_ptr->transformations & PNG_INTERLACE)
-                break;
-
-             png_ptr->num_rows = (png_ptr->height +
-                png_pass_yinc[pass] - 1 -
-                png_pass_ystart[pass]) /
-                png_pass_yinc[pass];
-           }
-         else
-           break;
-
-      } while (png_ptr->iwidth == 0 || png_ptr->num_rows == 0);
-   }
-}
-
-#if defined(PNG_READ_tEXt_SUPPORTED)
-void /* PRIVATE */
-png_push_handle_tEXt(png_structp png_ptr, png_infop info_ptr, png_uint_32
-   length)
-{
-   if (!(png_ptr->mode & PNG_HAVE_IHDR) || (png_ptr->mode & PNG_HAVE_IEND))
-      {
-         png_error(png_ptr, "Out of place tEXt");
-         info_ptr = info_ptr; /* to quiet some compiler warnings */
-      }
-
-#ifdef PNG_MAX_MALLOC_64K
-   png_ptr->skip_length = 0;  /* This may not be necessary */
-
-   if (length > (png_uint_32)65535L) /* Can't hold entire string in memory */
-   {
-      png_warning(png_ptr, "tEXt chunk too large to fit in memory");
-      png_ptr->skip_length = length - (png_uint_32)65535L;
-      length = (png_uint_32)65535L;
-   }
-#endif
-
-   png_ptr->current_text = (png_charp)png_malloc(png_ptr,
-         (png_uint_32)(length+1));
-   png_ptr->current_text[length] = '\0';
-   png_ptr->current_text_ptr = png_ptr->current_text;
-   png_ptr->current_text_size = (png_size_t)length;
-   png_ptr->current_text_left = (png_size_t)length;
-   png_ptr->process_mode = PNG_READ_tEXt_MODE;
-}
-
-void /* PRIVATE */
-png_push_read_tEXt(png_structp png_ptr, png_infop info_ptr)
-{
-   if (png_ptr->buffer_size && png_ptr->current_text_left)
-   {
-      png_size_t text_size;
-
-      if (png_ptr->buffer_size < png_ptr->current_text_left)
-         text_size = png_ptr->buffer_size;
-      else
-         text_size = png_ptr->current_text_left;
-      png_crc_read(png_ptr, (png_bytep)png_ptr->current_text_ptr, text_size);
-      png_ptr->current_text_left -= text_size;
-      png_ptr->current_text_ptr += text_size;
-   }
-   if (!(png_ptr->current_text_left))
-   {
-      png_textp text_ptr;
-      png_charp text;
-      png_charp key;
-      int ret;
-
-      if (png_ptr->buffer_size < 4)
-      {
-         png_push_save_buffer(png_ptr);
-         return;
-      }
-
-      png_push_crc_finish(png_ptr);
-
-#if defined(PNG_MAX_MALLOC_64K)
-      if (png_ptr->skip_length)
-         return;
-#endif
-
-      key = png_ptr->current_text;
-
-      for (text = key; *text; text++)
-         /* empty loop */ ;
-
-      if (text < key + png_ptr->current_text_size)
-         text++;
-
-      text_ptr = (png_textp)png_malloc(png_ptr,
-         (png_uint_32)png_sizeof(png_text));
-      text_ptr->compression = PNG_TEXT_COMPRESSION_NONE;
-      text_ptr->key = key;
-#ifdef PNG_iTXt_SUPPORTED
-      text_ptr->lang = NULL;
-      text_ptr->lang_key = NULL;
-#endif
-      text_ptr->text = text;
-
-      ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1);
-
-      png_free(png_ptr, key);
-      png_free(png_ptr, text_ptr);
-      png_ptr->current_text = NULL;
-
-      if (ret)
-        png_warning(png_ptr, "Insufficient memory to store text chunk.");
-   }
-}
-#endif
-
-#if defined(PNG_READ_zTXt_SUPPORTED)
-void /* PRIVATE */
-png_push_handle_zTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32
-   length)
-{
-   if (!(png_ptr->mode & PNG_HAVE_IHDR) || (png_ptr->mode & PNG_HAVE_IEND))
-      {
-         png_error(png_ptr, "Out of place zTXt");
-         info_ptr = info_ptr; /* to quiet some compiler warnings */
-      }
-
-#ifdef PNG_MAX_MALLOC_64K
-   /* We can't handle zTXt chunks > 64K, since we don't have enough space
-    * to be able to store the uncompressed data.  Actually, the threshold
-    * is probably around 32K, but it isn't as definite as 64K is.
-    */
-   if (length > (png_uint_32)65535L)
-   {
-      png_warning(png_ptr, "zTXt chunk too large to fit in memory");
-      png_push_crc_skip(png_ptr, length);
-      return;
-   }
-#endif
-
-   png_ptr->current_text = (png_charp)png_malloc(png_ptr,
-       (png_uint_32)(length+1));
-   png_ptr->current_text[length] = '\0';
-   png_ptr->current_text_ptr = png_ptr->current_text;
-   png_ptr->current_text_size = (png_size_t)length;
-   png_ptr->current_text_left = (png_size_t)length;
-   png_ptr->process_mode = PNG_READ_zTXt_MODE;
-}
-
-void /* PRIVATE */
-png_push_read_zTXt(png_structp png_ptr, png_infop info_ptr)
-{
-   if (png_ptr->buffer_size && png_ptr->current_text_left)
-   {
-      png_size_t text_size;
-
-      if (png_ptr->buffer_size < (png_uint_32)png_ptr->current_text_left)
-         text_size = png_ptr->buffer_size;
-      else
-         text_size = png_ptr->current_text_left;
-      png_crc_read(png_ptr, (png_bytep)png_ptr->current_text_ptr, text_size);
-      png_ptr->current_text_left -= text_size;
-      png_ptr->current_text_ptr += text_size;
-   }
-   if (!(png_ptr->current_text_left))
-   {
-      png_textp text_ptr;
-      png_charp text;
-      png_charp key;
-      int ret;
-      png_size_t text_size, key_size;
-
-      if (png_ptr->buffer_size < 4)
-      {
-         png_push_save_buffer(png_ptr);
-         return;
-      }
-
-      png_push_crc_finish(png_ptr);
-
-      key = png_ptr->current_text;
-
-      for (text = key; *text; text++)
-         /* empty loop */ ;
-
-      /* zTXt can't have zero text */
-      if (text >= key + png_ptr->current_text_size)
-      {
-         png_ptr->current_text = NULL;
-         png_free(png_ptr, key);
-         return;
-      }
-
-      text++;
-
-      if (*text != PNG_TEXT_COMPRESSION_zTXt) /* check compression byte */
-      {
-         png_ptr->current_text = NULL;
-         png_free(png_ptr, key);
-         return;
-      }
-
-      text++;
-
-      png_ptr->zstream.next_in = (png_bytep )text;
-      png_ptr->zstream.avail_in = (uInt)(png_ptr->current_text_size -
-         (text - key));
-      png_ptr->zstream.next_out = png_ptr->zbuf;
-      png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
-
-      key_size = text - key;
-      text_size = 0;
-      text = NULL;
-      ret = Z_STREAM_END;
-
-      while (png_ptr->zstream.avail_in)
-      {
-         ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);
-         if (ret != Z_OK && ret != Z_STREAM_END)
-         {
-            inflateReset(&png_ptr->zstream);
-            png_ptr->zstream.avail_in = 0;
-            png_ptr->current_text = NULL;
-            png_free(png_ptr, key);
-            png_free(png_ptr, text);
-            return;
-         }
-         if (!(png_ptr->zstream.avail_out) || ret == Z_STREAM_END)
-         {
-            if (text == NULL)
-            {
-               text = (png_charp)png_malloc(png_ptr,
-                  (png_uint_32)(png_ptr->zbuf_size - png_ptr->zstream.avail_out
-                     + key_size + 1));
-               png_memcpy(text + key_size, png_ptr->zbuf,
-                  png_ptr->zbuf_size - png_ptr->zstream.avail_out);
-               png_memcpy(text, key, key_size);
-               text_size = key_size + png_ptr->zbuf_size -
-                  png_ptr->zstream.avail_out;
-               *(text + text_size) = '\0';
-            }
-            else
-            {
-               png_charp tmp;
-
-               tmp = text;
-               text = (png_charp)png_malloc(png_ptr, text_size +
-                  (png_uint_32)(png_ptr->zbuf_size - png_ptr->zstream.avail_out
-                   + 1));
-               png_memcpy(text, tmp, text_size);
-               png_free(png_ptr, tmp);
-               png_memcpy(text + text_size, png_ptr->zbuf,
-                  png_ptr->zbuf_size - png_ptr->zstream.avail_out);
-               text_size += png_ptr->zbuf_size - png_ptr->zstream.avail_out;
-               *(text + text_size) = '\0';
-            }
-            if (ret != Z_STREAM_END)
-            {
-               png_ptr->zstream.next_out = png_ptr->zbuf;
-               png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
-            }
-         }
-         else
-         {
-            break;
-         }
-
-         if (ret == Z_STREAM_END)
-            break;
-      }
-
-      inflateReset(&png_ptr->zstream);
-      png_ptr->zstream.avail_in = 0;
-
-      if (ret != Z_STREAM_END)
-      {
-         png_ptr->current_text = NULL;
-         png_free(png_ptr, key);
-         png_free(png_ptr, text);
-         return;
-      }
-
-      png_ptr->current_text = NULL;
-      png_free(png_ptr, key);
-      key = text;
-      text += key_size;
-
-      text_ptr = (png_textp)png_malloc(png_ptr,
-          (png_uint_32)png_sizeof(png_text));
-      text_ptr->compression = PNG_TEXT_COMPRESSION_zTXt;
-      text_ptr->key = key;
-#ifdef PNG_iTXt_SUPPORTED
-      text_ptr->lang = NULL;
-      text_ptr->lang_key = NULL;
-#endif
-      text_ptr->text = text;
-
-      ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1);
-
-      png_free(png_ptr, key);
-      png_free(png_ptr, text_ptr);
-
-      if (ret)
-        png_warning(png_ptr, "Insufficient memory to store text chunk.");
-   }
-}
-#endif
-
-#if defined(PNG_READ_iTXt_SUPPORTED)
-void /* PRIVATE */
-png_push_handle_iTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32
-   length)
-{
-   if (!(png_ptr->mode & PNG_HAVE_IHDR) || (png_ptr->mode & PNG_HAVE_IEND))
-      {
-         png_error(png_ptr, "Out of place iTXt");
-         info_ptr = info_ptr; /* to quiet some compiler warnings */
-      }
-
-#ifdef PNG_MAX_MALLOC_64K
-   png_ptr->skip_length = 0;  /* This may not be necessary */
-
-   if (length > (png_uint_32)65535L) /* Can't hold entire string in memory */
-   {
-      png_warning(png_ptr, "iTXt chunk too large to fit in memory");
-      png_ptr->skip_length = length - (png_uint_32)65535L;
-      length = (png_uint_32)65535L;
-   }
-#endif
-
-   png_ptr->current_text = (png_charp)png_malloc(png_ptr,
-         (png_uint_32)(length+1));
-   png_ptr->current_text[length] = '\0';
-   png_ptr->current_text_ptr = png_ptr->current_text;
-   png_ptr->current_text_size = (png_size_t)length;
-   png_ptr->current_text_left = (png_size_t)length;
-   png_ptr->process_mode = PNG_READ_iTXt_MODE;
-}
-
-void /* PRIVATE */
-png_push_read_iTXt(png_structp png_ptr, png_infop info_ptr)
-{
-
-   if (png_ptr->buffer_size && png_ptr->current_text_left)
-   {
-      png_size_t text_size;
-
-      if (png_ptr->buffer_size < png_ptr->current_text_left)
-         text_size = png_ptr->buffer_size;
-      else
-         text_size = png_ptr->current_text_left;
-      png_crc_read(png_ptr, (png_bytep)png_ptr->current_text_ptr, text_size);
-      png_ptr->current_text_left -= text_size;
-      png_ptr->current_text_ptr += text_size;
-   }
-   if (!(png_ptr->current_text_left))
-   {
-      png_textp text_ptr;
-      png_charp key;
-      int comp_flag;
-      png_charp lang;
-      png_charp lang_key;
-      png_charp text;
-      int ret;
-
-      if (png_ptr->buffer_size < 4)
-      {
-         png_push_save_buffer(png_ptr);
-         return;
-      }
-
-      png_push_crc_finish(png_ptr);
-
-#if defined(PNG_MAX_MALLOC_64K)
-      if (png_ptr->skip_length)
-         return;
-#endif
-
-      key = png_ptr->current_text;
-
-      for (lang = key; *lang; lang++)
-         /* empty loop */ ;
-
-      if (lang < key + png_ptr->current_text_size - 3)
-         lang++;
-
-      comp_flag = *lang++;
-      lang++;     /* skip comp_type, always zero */
-
-      for (lang_key = lang; *lang_key; lang_key++)
-         /* empty loop */ ;
-      lang_key++;        /* skip NUL separator */
-
-      text=lang_key;
-      if (lang_key < key + png_ptr->current_text_size - 1)
-      {
-        for (; *text; text++)
-           /* empty loop */ ;
-      }
-
-      if (text < key + png_ptr->current_text_size)
-         text++;
-
-      text_ptr = (png_textp)png_malloc(png_ptr,
-         (png_uint_32)png_sizeof(png_text));
-      text_ptr->compression = comp_flag + 2;
-      text_ptr->key = key;
-      text_ptr->lang = lang;
-      text_ptr->lang_key = lang_key;
-      text_ptr->text = text;
-      text_ptr->text_length = 0;
-      text_ptr->itxt_length = png_strlen(text);
-
-      ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1);
-
-      png_ptr->current_text = NULL;
-
-      png_free(png_ptr, text_ptr);
-      if (ret)
-        png_warning(png_ptr, "Insufficient memory to store iTXt chunk.");
-   }
-}
-#endif
-
-/* This function is called when we haven't found a handler for this
- * chunk.  If there isn't a problem with the chunk itself (ie a bad chunk
- * name or a critical chunk), the chunk is (currently) silently ignored.
- */
-void /* PRIVATE */
-png_push_handle_unknown(png_structp png_ptr, png_infop info_ptr, png_uint_32
-   length)
-{
-   png_uint_32 skip=0;
-   png_check_chunk_name(png_ptr, png_ptr->chunk_name);
-
-   if (!(png_ptr->chunk_name[0] & 0x20))
-   {
-#if defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
-     if(png_handle_as_unknown(png_ptr, png_ptr->chunk_name) !=
-          PNG_HANDLE_CHUNK_ALWAYS
-#if defined(PNG_READ_USER_CHUNKS_SUPPORTED)
-          && png_ptr->read_user_chunk_fn == NULL
-#endif
-        )
-#endif
-        png_chunk_error(png_ptr, "unknown critical chunk");
-
-     info_ptr = info_ptr; /* to quiet some compiler warnings */
-   }
-
-#if defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
-   if (png_ptr->flags & PNG_FLAG_KEEP_UNKNOWN_CHUNKS)
-   {
-#ifdef PNG_MAX_MALLOC_64K
-      if (length > (png_uint_32)65535L)
-      {
-          png_warning(png_ptr, "unknown chunk too large to fit in memory");
-          skip = length - (png_uint_32)65535L;
-          length = (png_uint_32)65535L;
-      }
-#endif
-      png_memcpy((png_charp)png_ptr->unknown_chunk.name,
-                 (png_charp)png_ptr->chunk_name, 
-                 png_sizeof(png_ptr->unknown_chunk.name));
-      png_ptr->unknown_chunk.name[png_sizeof(png_ptr->unknown_chunk.name)-1]='\0';
-
-      png_ptr->unknown_chunk.size = (png_size_t)length;
-      if (length == 0)
-         png_ptr->unknown_chunk.data = NULL;
-      else
-      {
-         png_ptr->unknown_chunk.data = (png_bytep)png_malloc(png_ptr, length);
-         png_crc_read(png_ptr, (png_bytep)png_ptr->unknown_chunk.data, length);
-      }
-#if defined(PNG_READ_USER_CHUNKS_SUPPORTED)
-      if(png_ptr->read_user_chunk_fn != NULL)
-      {
-         /* callback to user unknown chunk handler */
-         int ret;
-         ret = (*(png_ptr->read_user_chunk_fn))
-           (png_ptr, &png_ptr->unknown_chunk);
-         if (ret < 0)
-            png_chunk_error(png_ptr, "error in user chunk");
-         if (ret == 0)
-         {
-            if (!(png_ptr->chunk_name[0] & 0x20))
-               if(png_handle_as_unknown(png_ptr, png_ptr->chunk_name) !=
-                    PNG_HANDLE_CHUNK_ALWAYS)
-                  png_chunk_error(png_ptr, "unknown critical chunk");
-            png_set_unknown_chunks(png_ptr, info_ptr,
-               &png_ptr->unknown_chunk, 1);
-         }
-      }
-      else
-#endif
-        png_set_unknown_chunks(png_ptr, info_ptr, &png_ptr->unknown_chunk, 1);
-      png_free(png_ptr, png_ptr->unknown_chunk.data);
-      png_ptr->unknown_chunk.data = NULL;
-   }
-   else
-#endif
-      skip=length;
-   png_push_crc_skip(png_ptr, skip);
-}
-
-void /* PRIVATE */
-png_push_have_info(png_structp png_ptr, png_infop info_ptr)
-{
-   if (png_ptr->info_fn != NULL)
-      (*(png_ptr->info_fn))(png_ptr, info_ptr);
-}
-
-void /* PRIVATE */
-png_push_have_end(png_structp png_ptr, png_infop info_ptr)
-{
-   if (png_ptr->end_fn != NULL)
-      (*(png_ptr->end_fn))(png_ptr, info_ptr);
-}
-
-void /* PRIVATE */
-png_push_have_row(png_structp png_ptr, png_bytep row)
-{
-   if (png_ptr->row_fn != NULL)
-      (*(png_ptr->row_fn))(png_ptr, row, png_ptr->row_number,
-         (int)png_ptr->pass);
-}
-
-void PNGAPI
-png_progressive_combine_row (png_structp png_ptr,
-   png_bytep old_row, png_bytep new_row)
-{
-#ifdef PNG_USE_LOCAL_ARRAYS
-   PNG_CONST int FARDATA png_pass_dsp_mask[7] =
-      {0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff};
-#endif
-   if(png_ptr == NULL) return;
-   if (new_row != NULL)    /* new_row must == png_ptr->row_buf here. */
-      png_combine_row(png_ptr, old_row, png_pass_dsp_mask[png_ptr->pass]);
-}
-
-void PNGAPI
-png_set_progressive_read_fn(png_structp png_ptr, png_voidp progressive_ptr,
-   png_progressive_info_ptr info_fn, png_progressive_row_ptr row_fn,
-   png_progressive_end_ptr end_fn)
-{
-   if(png_ptr == NULL) return;
-   png_ptr->info_fn = info_fn;
-   png_ptr->row_fn = row_fn;
-   png_ptr->end_fn = end_fn;
-
-   png_set_read_fn(png_ptr, progressive_ptr, png_push_fill_buffer);
-}
-
-png_voidp PNGAPI
-png_get_progressive_ptr(png_structp png_ptr)
-{
-   if(png_ptr == NULL) return (NULL);
-   return png_ptr->io_ptr;
-}
-#endif /* PNG_PROGRESSIVE_READ_SUPPORTED */
+\r
+/* pngpread.c - read a png file in push mode\r
+ *\r
+ * Last changed in libpng 1.4.3 [June 26, 2010]\r
+ * Copyright (c) 1998-2010 Glenn Randers-Pehrson\r
+ * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)\r
+ * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)\r
+ *\r
+ * This code is released under the libpng license.\r
+ * For conditions of distribution and use, see the disclaimer\r
+ * and license in png.h\r
+ */\r
+\r
+#define PNG_NO_PEDANTIC_WARNINGS\r
+#include "png.h"\r
+#ifdef PNG_PROGRESSIVE_READ_SUPPORTED\r
+#include "pngpriv.h"\r
+\r
+/* Push model modes */\r
+#define PNG_READ_SIG_MODE   0\r
+#define PNG_READ_CHUNK_MODE 1\r
+#define PNG_READ_IDAT_MODE  2\r
+#define PNG_SKIP_MODE       3\r
+#define PNG_READ_tEXt_MODE  4\r
+#define PNG_READ_zTXt_MODE  5\r
+#define PNG_READ_DONE_MODE  6\r
+#define PNG_READ_iTXt_MODE  7\r
+#define PNG_ERROR_MODE      8\r
+\r
+void PNGAPI\r
+png_process_data(png_structp png_ptr, png_infop info_ptr,\r
+   png_bytep buffer, png_size_t buffer_size)\r
+{\r
+   if (png_ptr == NULL || info_ptr == NULL)\r
+      return;\r
+\r
+   png_push_restore_buffer(png_ptr, buffer, buffer_size);\r
+\r
+   while (png_ptr->buffer_size)\r
+   {\r
+      png_process_some_data(png_ptr, info_ptr);\r
+   }\r
+}\r
+\r
+/* What we do with the incoming data depends on what we were previously\r
+ * doing before we ran out of data...\r
+ */\r
+void /* PRIVATE */\r
+png_process_some_data(png_structp png_ptr, png_infop info_ptr)\r
+{\r
+   if (png_ptr == NULL)\r
+      return;\r
+\r
+   switch (png_ptr->process_mode)\r
+   {\r
+      case PNG_READ_SIG_MODE:\r
+      {\r
+         png_push_read_sig(png_ptr, info_ptr);\r
+         break;\r
+      }\r
+\r
+      case PNG_READ_CHUNK_MODE:\r
+      {\r
+         png_push_read_chunk(png_ptr, info_ptr);\r
+         break;\r
+      }\r
+\r
+      case PNG_READ_IDAT_MODE:\r
+      {\r
+         png_push_read_IDAT(png_ptr);\r
+         break;\r
+      }\r
+\r
+#ifdef PNG_READ_tEXt_SUPPORTED\r
+      case PNG_READ_tEXt_MODE:\r
+      {\r
+         png_push_read_tEXt(png_ptr, info_ptr);\r
+         break;\r
+      }\r
+\r
+#endif\r
+#ifdef PNG_READ_zTXt_SUPPORTED\r
+      case PNG_READ_zTXt_MODE:\r
+      {\r
+         png_push_read_zTXt(png_ptr, info_ptr);\r
+         break;\r
+      }\r
+\r
+#endif\r
+#ifdef PNG_READ_iTXt_SUPPORTED\r
+      case PNG_READ_iTXt_MODE:\r
+      {\r
+         png_push_read_iTXt(png_ptr, info_ptr);\r
+         break;\r
+      }\r
+\r
+#endif\r
+      case PNG_SKIP_MODE:\r
+      {\r
+         png_push_crc_finish(png_ptr);\r
+         break;\r
+      }\r
+\r
+      default:\r
+      {\r
+         png_ptr->buffer_size = 0;\r
+         break;\r
+      }\r
+   }\r
+}\r
+\r
+/* Read any remaining signature bytes from the stream and compare them with\r
+ * the correct PNG signature.  It is possible that this routine is called\r
+ * with bytes already read from the signature, either because they have been\r
+ * checked by the calling application, or because of multiple calls to this\r
+ * routine.\r
+ */\r
+void /* PRIVATE */\r
+png_push_read_sig(png_structp png_ptr, png_infop info_ptr)\r
+{\r
+   png_size_t num_checked = png_ptr->sig_bytes,\r
+             num_to_check = 8 - num_checked;\r
+\r
+   if (png_ptr->buffer_size < num_to_check)\r
+   {\r
+      num_to_check = png_ptr->buffer_size;\r
+   }\r
+\r
+   png_push_fill_buffer(png_ptr, &(info_ptr->signature[num_checked]),\r
+      num_to_check);\r
+   png_ptr->sig_bytes = (png_byte)(png_ptr->sig_bytes + num_to_check);\r
+\r
+   if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check))\r
+   {\r
+      if (num_checked < 4 &&\r
+          png_sig_cmp(info_ptr->signature, num_checked, num_to_check - 4))\r
+         png_error(png_ptr, "Not a PNG file");\r
+      else\r
+         png_error(png_ptr, "PNG file corrupted by ASCII conversion");\r
+   }\r
+   else\r
+   {\r
+      if (png_ptr->sig_bytes >= 8)\r
+      {\r
+         png_ptr->process_mode = PNG_READ_CHUNK_MODE;\r
+      }\r
+   }\r
+}\r
+\r
+void /* PRIVATE */\r
+png_push_read_chunk(png_structp png_ptr, png_infop info_ptr)\r
+{\r
+      PNG_IHDR;\r
+      PNG_IDAT;\r
+      PNG_IEND;\r
+      PNG_PLTE;\r
+#ifdef PNG_READ_bKGD_SUPPORTED\r
+      PNG_bKGD;\r
+#endif\r
+#ifdef PNG_READ_cHRM_SUPPORTED\r
+      PNG_cHRM;\r
+#endif\r
+#ifdef PNG_READ_gAMA_SUPPORTED\r
+      PNG_gAMA;\r
+#endif\r
+#ifdef PNG_READ_hIST_SUPPORTED\r
+      PNG_hIST;\r
+#endif\r
+#ifdef PNG_READ_iCCP_SUPPORTED\r
+      PNG_iCCP;\r
+#endif\r
+#ifdef PNG_READ_iTXt_SUPPORTED\r
+      PNG_iTXt;\r
+#endif\r
+#ifdef PNG_READ_oFFs_SUPPORTED\r
+      PNG_oFFs;\r
+#endif\r
+#ifdef PNG_READ_pCAL_SUPPORTED\r
+      PNG_pCAL;\r
+#endif\r
+#ifdef PNG_READ_pHYs_SUPPORTED\r
+      PNG_pHYs;\r
+#endif\r
+#ifdef PNG_READ_sBIT_SUPPORTED\r
+      PNG_sBIT;\r
+#endif\r
+#ifdef PNG_READ_sCAL_SUPPORTED\r
+      PNG_sCAL;\r
+#endif\r
+#ifdef PNG_READ_sRGB_SUPPORTED\r
+      PNG_sRGB;\r
+#endif\r
+#ifdef PNG_READ_sPLT_SUPPORTED\r
+      PNG_sPLT;\r
+#endif\r
+#ifdef PNG_READ_tEXt_SUPPORTED\r
+      PNG_tEXt;\r
+#endif\r
+#ifdef PNG_READ_tIME_SUPPORTED\r
+      PNG_tIME;\r
+#endif\r
+#ifdef PNG_READ_tRNS_SUPPORTED\r
+      PNG_tRNS;\r
+#endif\r
+#ifdef PNG_READ_zTXt_SUPPORTED\r
+      PNG_zTXt;\r
+#endif\r
+\r
+   /* First we make sure we have enough data for the 4 byte chunk name\r
+    * and the 4 byte chunk length before proceeding with decoding the\r
+    * chunk data.  To fully decode each of these chunks, we also make\r
+    * sure we have enough data in the buffer for the 4 byte CRC at the\r
+    * end of every chunk (except IDAT, which is handled separately).\r
+    */\r
+   if (!(png_ptr->mode & PNG_HAVE_CHUNK_HEADER))\r
+   {\r
+      png_byte chunk_length[4];\r
+\r
+      if (png_ptr->buffer_size < 8)\r
+      {\r
+         png_push_save_buffer(png_ptr);\r
+         return;\r
+      }\r
+\r
+      png_push_fill_buffer(png_ptr, chunk_length, 4);\r
+      png_ptr->push_length = png_get_uint_31(png_ptr, chunk_length);\r
+      png_reset_crc(png_ptr);\r
+      png_crc_read(png_ptr, png_ptr->chunk_name, 4);\r
+      png_check_chunk_name(png_ptr, png_ptr->chunk_name);\r
+      png_ptr->mode |= PNG_HAVE_CHUNK_HEADER;\r
+   }\r
+\r
+   if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))\r
+     if (png_ptr->mode & PNG_AFTER_IDAT)\r
+        png_ptr->mode |= PNG_HAVE_CHUNK_AFTER_IDAT;\r
+\r
+   if (!png_memcmp(png_ptr->chunk_name, png_IHDR, 4))\r
+   {\r
+      if (png_ptr->push_length != 13)\r
+         png_error(png_ptr, "Invalid IHDR length");\r
+\r
+      if (png_ptr->push_length + 4 > png_ptr->buffer_size)\r
+      {\r
+         png_push_save_buffer(png_ptr);\r
+         return;\r
+      }\r
+\r
+      png_handle_IHDR(png_ptr, info_ptr, png_ptr->push_length);\r
+   }\r
+\r
+   else if (!png_memcmp(png_ptr->chunk_name, png_IEND, 4))\r
+   {\r
+      if (png_ptr->push_length + 4 > png_ptr->buffer_size)\r
+      {\r
+         png_push_save_buffer(png_ptr);\r
+         return;\r
+      }\r
+\r
+      png_handle_IEND(png_ptr, info_ptr, png_ptr->push_length);\r
+\r
+      png_ptr->process_mode = PNG_READ_DONE_MODE;\r
+      png_push_have_end(png_ptr, info_ptr);\r
+   }\r
+\r
+#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED\r
+   else if (png_handle_as_unknown(png_ptr, png_ptr->chunk_name))\r
+   {\r
+      if (png_ptr->push_length + 4 > png_ptr->buffer_size)\r
+      {\r
+         png_push_save_buffer(png_ptr);\r
+         return;\r
+      }\r
+\r
+      if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))\r
+         png_ptr->mode |= PNG_HAVE_IDAT;\r
+\r
+      png_handle_unknown(png_ptr, info_ptr, png_ptr->push_length);\r
+\r
+      if (!png_memcmp(png_ptr->chunk_name, png_PLTE, 4))\r
+         png_ptr->mode |= PNG_HAVE_PLTE;\r
+\r
+      else if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))\r
+      {\r
+         if (!(png_ptr->mode & PNG_HAVE_IHDR))\r
+            png_error(png_ptr, "Missing IHDR before IDAT");\r
+\r
+         else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&\r
+                  !(png_ptr->mode & PNG_HAVE_PLTE))\r
+            png_error(png_ptr, "Missing PLTE before IDAT");\r
+      }\r
+   }\r
+\r
+#endif\r
+   else if (!png_memcmp(png_ptr->chunk_name, png_PLTE, 4))\r
+   {\r
+      if (png_ptr->push_length + 4 > png_ptr->buffer_size)\r
+      {\r
+         png_push_save_buffer(png_ptr);\r
+         return;\r
+      }\r
+      png_handle_PLTE(png_ptr, info_ptr, png_ptr->push_length);\r
+   }\r
+\r
+   else if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))\r
+   {\r
+      /* If we reach an IDAT chunk, this means we have read all of the\r
+       * header chunks, and we can start reading the image (or if this\r
+       * is called after the image has been read - we have an error).\r
+       */\r
+\r
+      if (!(png_ptr->mode & PNG_HAVE_IHDR))\r
+         png_error(png_ptr, "Missing IHDR before IDAT");\r
+\r
+      else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&\r
+          !(png_ptr->mode & PNG_HAVE_PLTE))\r
+         png_error(png_ptr, "Missing PLTE before IDAT");\r
+\r
+      if (png_ptr->mode & PNG_HAVE_IDAT)\r
+      {\r
+         if (!(png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT))\r
+            if (png_ptr->push_length == 0)\r
+               return;\r
+\r
+         if (png_ptr->mode & PNG_AFTER_IDAT)\r
+            png_benign_error(png_ptr, "Too many IDATs found");\r
+      }\r
+\r
+      png_ptr->idat_size = png_ptr->push_length;\r
+      png_ptr->mode |= PNG_HAVE_IDAT;\r
+      png_ptr->process_mode = PNG_READ_IDAT_MODE;\r
+      png_push_have_info(png_ptr, info_ptr);\r
+      png_ptr->zstream.avail_out =\r
+          (uInt) PNG_ROWBYTES(png_ptr->pixel_depth,\r
+          png_ptr->iwidth) + 1;\r
+      png_ptr->zstream.next_out = png_ptr->row_buf;\r
+      return;\r
+   }\r
+\r
+#ifdef PNG_READ_gAMA_SUPPORTED\r
+   else if (!png_memcmp(png_ptr->chunk_name, png_gAMA, 4))\r
+   {\r
+      if (png_ptr->push_length + 4 > png_ptr->buffer_size)\r
+      {\r
+         png_push_save_buffer(png_ptr);\r
+         return;\r
+      }\r
+\r
+      png_handle_gAMA(png_ptr, info_ptr, png_ptr->push_length);\r
+   }\r
+\r
+#endif\r
+#ifdef PNG_READ_sBIT_SUPPORTED\r
+   else if (!png_memcmp(png_ptr->chunk_name, png_sBIT, 4))\r
+   {\r
+      if (png_ptr->push_length + 4 > png_ptr->buffer_size)\r
+      {\r
+         png_push_save_buffer(png_ptr);\r
+         return;\r
+      }\r
+\r
+      png_handle_sBIT(png_ptr, info_ptr, png_ptr->push_length);\r
+   }\r
+\r
+#endif\r
+#ifdef PNG_READ_cHRM_SUPPORTED\r
+   else if (!png_memcmp(png_ptr->chunk_name, png_cHRM, 4))\r
+   {\r
+      if (png_ptr->push_length + 4 > png_ptr->buffer_size)\r
+      {\r
+         png_push_save_buffer(png_ptr);\r
+         return;\r
+      }\r
+\r
+      png_handle_cHRM(png_ptr, info_ptr, png_ptr->push_length);\r
+   }\r
+\r
+#endif\r
+#ifdef PNG_READ_sRGB_SUPPORTED\r
+   else if (!png_memcmp(png_ptr->chunk_name, png_sRGB, 4))\r
+   {\r
+      if (png_ptr->push_length + 4 > png_ptr->buffer_size)\r
+      {\r
+         png_push_save_buffer(png_ptr);\r
+         return;\r
+      }\r
+\r
+      png_handle_sRGB(png_ptr, info_ptr, png_ptr->push_length);\r
+   }\r
+\r
+#endif\r
+#ifdef PNG_READ_iCCP_SUPPORTED\r
+   else if (!png_memcmp(png_ptr->chunk_name, png_iCCP, 4))\r
+   {\r
+      if (png_ptr->push_length + 4 > png_ptr->buffer_size)\r
+      {\r
+         png_push_save_buffer(png_ptr);\r
+         return;\r
+      }\r
+\r
+      png_handle_iCCP(png_ptr, info_ptr, png_ptr->push_length);\r
+   }\r
+\r
+#endif\r
+#ifdef PNG_READ_sPLT_SUPPORTED\r
+   else if (!png_memcmp(png_ptr->chunk_name, png_sPLT, 4))\r
+   {\r
+      if (png_ptr->push_length + 4 > png_ptr->buffer_size)\r
+      {\r
+         png_push_save_buffer(png_ptr);\r
+         return;\r
+      }\r
+\r
+      png_handle_sPLT(png_ptr, info_ptr, png_ptr->push_length);\r
+   }\r
+\r
+#endif\r
+#ifdef PNG_READ_tRNS_SUPPORTED\r
+   else if (!png_memcmp(png_ptr->chunk_name, png_tRNS, 4))\r
+   {\r
+      if (png_ptr->push_length + 4 > png_ptr->buffer_size)\r
+      {\r
+         png_push_save_buffer(png_ptr);\r
+         return;\r
+      }\r
+\r
+      png_handle_tRNS(png_ptr, info_ptr, png_ptr->push_length);\r
+   }\r
+\r
+#endif\r
+#ifdef PNG_READ_bKGD_SUPPORTED\r
+   else if (!png_memcmp(png_ptr->chunk_name, png_bKGD, 4))\r
+   {\r
+      if (png_ptr->push_length + 4 > png_ptr->buffer_size)\r
+      {\r
+         png_push_save_buffer(png_ptr);\r
+         return;\r
+      }\r
+\r
+      png_handle_bKGD(png_ptr, info_ptr, png_ptr->push_length);\r
+   }\r
+\r
+#endif\r
+#ifdef PNG_READ_hIST_SUPPORTED\r
+   else if (!png_memcmp(png_ptr->chunk_name, png_hIST, 4))\r
+   {\r
+      if (png_ptr->push_length + 4 > png_ptr->buffer_size)\r
+      {\r
+         png_push_save_buffer(png_ptr);\r
+         return;\r
+      }\r
+\r
+      png_handle_hIST(png_ptr, info_ptr, png_ptr->push_length);\r
+   }\r
+\r
+#endif\r
+#ifdef PNG_READ_pHYs_SUPPORTED\r
+   else if (!png_memcmp(png_ptr->chunk_name, png_pHYs, 4))\r
+   {\r
+      if (png_ptr->push_length + 4 > png_ptr->buffer_size)\r
+      {\r
+         png_push_save_buffer(png_ptr);\r
+         return;\r
+      }\r
+\r
+      png_handle_pHYs(png_ptr, info_ptr, png_ptr->push_length);\r
+   }\r
+\r
+#endif\r
+#ifdef PNG_READ_oFFs_SUPPORTED\r
+   else if (!png_memcmp(png_ptr->chunk_name, png_oFFs, 4))\r
+   {\r
+      if (png_ptr->push_length + 4 > png_ptr->buffer_size)\r
+      {\r
+         png_push_save_buffer(png_ptr);\r
+         return;\r
+      }\r
+\r
+      png_handle_oFFs(png_ptr, info_ptr, png_ptr->push_length);\r
+   }\r
+#endif\r
+\r
+#ifdef PNG_READ_pCAL_SUPPORTED\r
+   else if (!png_memcmp(png_ptr->chunk_name, png_pCAL, 4))\r
+   {\r
+      if (png_ptr->push_length + 4 > png_ptr->buffer_size)\r
+      {\r
+         png_push_save_buffer(png_ptr);\r
+         return;\r
+      }\r
+\r
+      png_handle_pCAL(png_ptr, info_ptr, png_ptr->push_length);\r
+   }\r
+\r
+#endif\r
+#ifdef PNG_READ_sCAL_SUPPORTED\r
+   else if (!png_memcmp(png_ptr->chunk_name, png_sCAL, 4))\r
+   {\r
+      if (png_ptr->push_length + 4 > png_ptr->buffer_size)\r
+      {\r
+         png_push_save_buffer(png_ptr);\r
+         return;\r
+      }\r
+\r
+      png_handle_sCAL(png_ptr, info_ptr, png_ptr->push_length);\r
+   }\r
+\r
+#endif\r
+#ifdef PNG_READ_tIME_SUPPORTED\r
+   else if (!png_memcmp(png_ptr->chunk_name, png_tIME, 4))\r
+   {\r
+      if (png_ptr->push_length + 4 > png_ptr->buffer_size)\r
+      {\r
+         png_push_save_buffer(png_ptr);\r
+         return;\r
+      }\r
+\r
+      png_handle_tIME(png_ptr, info_ptr, png_ptr->push_length);\r
+   }\r
+\r
+#endif\r
+#ifdef PNG_READ_tEXt_SUPPORTED\r
+   else if (!png_memcmp(png_ptr->chunk_name, png_tEXt, 4))\r
+   {\r
+      if (png_ptr->push_length + 4 > png_ptr->buffer_size)\r
+      {\r
+         png_push_save_buffer(png_ptr);\r
+         return;\r
+      }\r
+\r
+      png_push_handle_tEXt(png_ptr, info_ptr, png_ptr->push_length);\r
+   }\r
+\r
+#endif\r
+#ifdef PNG_READ_zTXt_SUPPORTED\r
+   else if (!png_memcmp(png_ptr->chunk_name, png_zTXt, 4))\r
+   {\r
+      if (png_ptr->push_length + 4 > png_ptr->buffer_size)\r
+      {\r
+         png_push_save_buffer(png_ptr);\r
+         return;\r
+      }\r
+\r
+      png_push_handle_zTXt(png_ptr, info_ptr, png_ptr->push_length);\r
+   }\r
+\r
+#endif\r
+#ifdef PNG_READ_iTXt_SUPPORTED\r
+   else if (!png_memcmp(png_ptr->chunk_name, png_iTXt, 4))\r
+   {\r
+      if (png_ptr->push_length + 4 > png_ptr->buffer_size)\r
+      {\r
+         png_push_save_buffer(png_ptr);\r
+         return;\r
+      }\r
+\r
+      png_push_handle_iTXt(png_ptr, info_ptr, png_ptr->push_length);\r
+   }\r
+\r
+#endif\r
+   else\r
+   {\r
+      if (png_ptr->push_length + 4 > png_ptr->buffer_size)\r
+      {\r
+         png_push_save_buffer(png_ptr);\r
+         return;\r
+      }\r
+      png_push_handle_unknown(png_ptr, info_ptr, png_ptr->push_length);\r
+   }\r
+\r
+   png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;\r
+}\r
+\r
+void /* PRIVATE */\r
+png_push_crc_skip(png_structp png_ptr, png_uint_32 skip)\r
+{\r
+   png_ptr->process_mode = PNG_SKIP_MODE;\r
+   png_ptr->skip_length = skip;\r
+}\r
+\r
+void /* PRIVATE */\r
+png_push_crc_finish(png_structp png_ptr)\r
+{\r
+   if (png_ptr->skip_length && png_ptr->save_buffer_size)\r
+   {\r
+      png_size_t save_size;\r
+\r
+      if (png_ptr->skip_length < (png_uint_32)png_ptr->save_buffer_size)\r
+         save_size = (png_size_t)png_ptr->skip_length;\r
+      else\r
+         save_size = png_ptr->save_buffer_size;\r
+\r
+      png_calculate_crc(png_ptr, png_ptr->save_buffer_ptr, save_size);\r
+\r
+      png_ptr->skip_length -= save_size;\r
+      png_ptr->buffer_size -= save_size;\r
+      png_ptr->save_buffer_size -= save_size;\r
+      png_ptr->save_buffer_ptr += save_size;\r
+   }\r
+   if (png_ptr->skip_length && png_ptr->current_buffer_size)\r
+   {\r
+      png_size_t save_size;\r
+\r
+      if (png_ptr->skip_length < (png_uint_32)png_ptr->current_buffer_size)\r
+         save_size = (png_size_t)png_ptr->skip_length;\r
+      else\r
+         save_size = png_ptr->current_buffer_size;\r
+\r
+      png_calculate_crc(png_ptr, png_ptr->current_buffer_ptr, save_size);\r
+\r
+      png_ptr->skip_length -= save_size;\r
+      png_ptr->buffer_size -= save_size;\r
+      png_ptr->current_buffer_size -= save_size;\r
+      png_ptr->current_buffer_ptr += save_size;\r
+   }\r
+   if (!png_ptr->skip_length)\r
+   {\r
+      if (png_ptr->buffer_size < 4)\r
+      {\r
+         png_push_save_buffer(png_ptr);\r
+         return;\r
+      }\r
+\r
+      png_crc_finish(png_ptr, 0);\r
+      png_ptr->process_mode = PNG_READ_CHUNK_MODE;\r
+   }\r
+}\r
+\r
+void PNGAPI\r
+png_push_fill_buffer(png_structp png_ptr, png_bytep buffer, png_size_t length)\r
+{\r
+   png_bytep ptr;\r
+\r
+   if (png_ptr == NULL)\r
+      return;\r
+\r
+   ptr = buffer;\r
+   if (png_ptr->save_buffer_size)\r
+   {\r
+      png_size_t save_size;\r
+\r
+      if (length < png_ptr->save_buffer_size)\r
+         save_size = length;\r
+      else\r
+         save_size = png_ptr->save_buffer_size;\r
+\r
+      png_memcpy(ptr, png_ptr->save_buffer_ptr, save_size);\r
+      length -= save_size;\r
+      ptr += save_size;\r
+      png_ptr->buffer_size -= save_size;\r
+      png_ptr->save_buffer_size -= save_size;\r
+      png_ptr->save_buffer_ptr += save_size;\r
+   }\r
+   if (length && png_ptr->current_buffer_size)\r
+   {\r
+      png_size_t save_size;\r
+\r
+      if (length < png_ptr->current_buffer_size)\r
+         save_size = length;\r
+\r
+      else\r
+         save_size = png_ptr->current_buffer_size;\r
+\r
+      png_memcpy(ptr, png_ptr->current_buffer_ptr, save_size);\r
+      png_ptr->buffer_size -= save_size;\r
+      png_ptr->current_buffer_size -= save_size;\r
+      png_ptr->current_buffer_ptr += save_size;\r
+   }\r
+}\r
+\r
+void /* PRIVATE */\r
+png_push_save_buffer(png_structp png_ptr)\r
+{\r
+   if (png_ptr->save_buffer_size)\r
+   {\r
+      if (png_ptr->save_buffer_ptr != png_ptr->save_buffer)\r
+      {\r
+         png_size_t i, istop;\r
+         png_bytep sp;\r
+         png_bytep dp;\r
+\r
+         istop = png_ptr->save_buffer_size;\r
+         for (i = 0, sp = png_ptr->save_buffer_ptr, dp = png_ptr->save_buffer;\r
+            i < istop; i++, sp++, dp++)\r
+         {\r
+            *dp = *sp;\r
+         }\r
+      }\r
+   }\r
+   if (png_ptr->save_buffer_size + png_ptr->current_buffer_size >\r
+      png_ptr->save_buffer_max)\r
+   {\r
+      png_size_t new_max;\r
+      png_bytep old_buffer;\r
+\r
+      if (png_ptr->save_buffer_size > PNG_SIZE_MAX -\r
+         (png_ptr->current_buffer_size + 256))\r
+      {\r
+        png_error(png_ptr, "Potential overflow of save_buffer");\r
+      }\r
+\r
+      new_max = png_ptr->save_buffer_size + png_ptr->current_buffer_size + 256;\r
+      old_buffer = png_ptr->save_buffer;\r
+      png_ptr->save_buffer = (png_bytep)png_malloc_warn(png_ptr,\r
+         (png_size_t)new_max);\r
+      if (png_ptr->save_buffer == NULL)\r
+      {\r
+        png_free(png_ptr, old_buffer);\r
+        png_error(png_ptr, "Insufficient memory for save_buffer");\r
+      }\r
+      png_memcpy(png_ptr->save_buffer, old_buffer, png_ptr->save_buffer_size);\r
+      png_free(png_ptr, old_buffer);\r
+      png_ptr->save_buffer_max = new_max;\r
+   }\r
+   if (png_ptr->current_buffer_size)\r
+   {\r
+      png_memcpy(png_ptr->save_buffer + png_ptr->save_buffer_size,\r
+         png_ptr->current_buffer_ptr, png_ptr->current_buffer_size);\r
+      png_ptr->save_buffer_size += png_ptr->current_buffer_size;\r
+      png_ptr->current_buffer_size = 0;\r
+   }\r
+   png_ptr->save_buffer_ptr = png_ptr->save_buffer;\r
+   png_ptr->buffer_size = 0;\r
+}\r
+\r
+void /* PRIVATE */\r
+png_push_restore_buffer(png_structp png_ptr, png_bytep buffer,\r
+   png_size_t buffer_length)\r
+{\r
+   png_ptr->current_buffer = buffer;\r
+   png_ptr->current_buffer_size = buffer_length;\r
+   png_ptr->buffer_size = buffer_length + png_ptr->save_buffer_size;\r
+   png_ptr->current_buffer_ptr = png_ptr->current_buffer;\r
+}\r
+\r
+void /* PRIVATE */\r
+png_push_read_IDAT(png_structp png_ptr)\r
+{\r
+   PNG_IDAT;\r
+   if (!(png_ptr->mode & PNG_HAVE_CHUNK_HEADER))\r
+   {\r
+      png_byte chunk_length[4];\r
+\r
+      if (png_ptr->buffer_size < 8)\r
+      {\r
+         png_push_save_buffer(png_ptr);\r
+         return;\r
+      }\r
+\r
+      png_push_fill_buffer(png_ptr, chunk_length, 4);\r
+      png_ptr->push_length = png_get_uint_31(png_ptr, chunk_length);\r
+      png_reset_crc(png_ptr);\r
+      png_crc_read(png_ptr, png_ptr->chunk_name, 4);\r
+      png_ptr->mode |= PNG_HAVE_CHUNK_HEADER;\r
+\r
+      if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4))\r
+      {\r
+         png_ptr->process_mode = PNG_READ_CHUNK_MODE;\r
+         if (!(png_ptr->flags & PNG_FLAG_ZLIB_FINISHED))\r
+            png_error(png_ptr, "Not enough compressed data");\r
+         return;\r
+      }\r
+\r
+      png_ptr->idat_size = png_ptr->push_length;\r
+   }\r
+   if (png_ptr->idat_size && png_ptr->save_buffer_size)\r
+   {\r
+      png_size_t save_size;\r
+\r
+      if (png_ptr->idat_size < (png_uint_32)png_ptr->save_buffer_size)\r
+      {\r
+         save_size = (png_size_t)png_ptr->idat_size;\r
+\r
+         /* Check for overflow */\r
+         if ((png_uint_32)save_size != png_ptr->idat_size)\r
+            png_error(png_ptr, "save_size overflowed in pngpread");\r
+      }\r
+      else\r
+         save_size = png_ptr->save_buffer_size;\r
+\r
+      png_calculate_crc(png_ptr, png_ptr->save_buffer_ptr, save_size);\r
+\r
+      png_process_IDAT_data(png_ptr, png_ptr->save_buffer_ptr, save_size);\r
+\r
+      png_ptr->idat_size -= save_size;\r
+      png_ptr->buffer_size -= save_size;\r
+      png_ptr->save_buffer_size -= save_size;\r
+      png_ptr->save_buffer_ptr += save_size;\r
+   }\r
+   if (png_ptr->idat_size && png_ptr->current_buffer_size)\r
+   {\r
+      png_size_t save_size;\r
+\r
+      if (png_ptr->idat_size < (png_uint_32)png_ptr->current_buffer_size)\r
+      {\r
+         save_size = (png_size_t)png_ptr->idat_size;\r
+\r
+         /* Check for overflow */\r
+         if ((png_uint_32)save_size != png_ptr->idat_size)\r
+            png_error(png_ptr, "save_size overflowed in pngpread");\r
+      }\r
+      else\r
+         save_size = png_ptr->current_buffer_size;\r
+\r
+      png_calculate_crc(png_ptr, png_ptr->current_buffer_ptr, save_size);\r
+\r
+      png_process_IDAT_data(png_ptr, png_ptr->current_buffer_ptr, save_size);\r
+\r
+      png_ptr->idat_size -= save_size;\r
+      png_ptr->buffer_size -= save_size;\r
+      png_ptr->current_buffer_size -= save_size;\r
+      png_ptr->current_buffer_ptr += save_size;\r
+   }\r
+   if (!png_ptr->idat_size)\r
+   {\r
+      if (png_ptr->buffer_size < 4)\r
+      {\r
+         png_push_save_buffer(png_ptr);\r
+         return;\r
+      }\r
+\r
+      png_crc_finish(png_ptr, 0);\r
+      png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;\r
+      png_ptr->mode |= PNG_AFTER_IDAT;\r
+   }\r
+}\r
+\r
+void /* PRIVATE */\r
+png_process_IDAT_data(png_structp png_ptr, png_bytep buffer,\r
+   png_size_t buffer_length)\r
+{\r
+   /* The caller checks for a non-zero buffer length. */\r
+   if (!(buffer_length > 0) || buffer == NULL)\r
+      png_error(png_ptr, "No IDAT data (internal error)");\r
+\r
+   /* This routine must process all the data it has been given\r
+    * before returning, calling the row callback as required to\r
+    * handle the uncompressed results.\r
+    */\r
+   png_ptr->zstream.next_in = buffer;\r
+   png_ptr->zstream.avail_in = (uInt)buffer_length;\r
+\r
+   /* Keep going until the decompressed data is all processed\r
+    * or the stream marked as finished.\r
+    */\r
+   while (png_ptr->zstream.avail_in > 0 &&\r
+         !(png_ptr->flags & PNG_FLAG_ZLIB_FINISHED))\r
+   {\r
+      int ret;\r
+\r
+      /* We have data for zlib, but we must check that zlib\r
+       * has somewhere to put the results.  It doesn't matter\r
+       * if we don't expect any results -- it may be the input\r
+       * data is just the LZ end code.\r
+       */\r
+      if (!(png_ptr->zstream.avail_out > 0))\r
+      {\r
+         png_ptr->zstream.avail_out =\r
+             (uInt) PNG_ROWBYTES(png_ptr->pixel_depth,\r
+             png_ptr->iwidth) + 1;\r
+         png_ptr->zstream.next_out = png_ptr->row_buf;\r
+      }\r
+\r
+      /* Using Z_SYNC_FLUSH here means that an unterminated\r
+       * LZ stream can still be handled (a stream with a missing\r
+       * end code), otherwise (Z_NO_FLUSH) a future zlib\r
+       * implementation might defer output and, therefore,\r
+       * change the current behavior.  (See comments in inflate.c\r
+       * for why this doesn't happen at present with zlib 1.2.5.)\r
+       */\r
+      ret = inflate(&png_ptr->zstream, Z_SYNC_FLUSH);\r
+\r
+      /* Check for any failure before proceeding. */\r
+      if (ret != Z_OK && ret != Z_STREAM_END)\r
+      {\r
+        /* Terminate the decompression. */\r
+        png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;\r
+\r
+         /* This may be a truncated stream (missing or\r
+         * damaged end code).  Treat that as a warning.\r
+         */\r
+         if (png_ptr->row_number >= png_ptr->num_rows ||\r
+            png_ptr->pass > 6)\r
+           png_warning(png_ptr, "Truncated compressed data in IDAT");\r
+        else\r
+           png_error(png_ptr, "Decompression error in IDAT");\r
+\r
+        /* Skip the check on unprocessed input */\r
+         return;\r
+      }\r
+\r
+      /* Did inflate output any data? */\r
+      if (png_ptr->zstream.next_out != png_ptr->row_buf)\r
+      {\r
+        /* Is this unexpected data after the last row?\r
+         * If it is, artificially terminate the LZ output\r
+         * here.\r
+         */\r
+         if (png_ptr->row_number >= png_ptr->num_rows ||\r
+            png_ptr->pass > 6)\r
+         {\r
+           /* Extra data. */\r
+           png_warning(png_ptr, "Extra compressed data in IDAT");\r
+            png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;\r
+           /* Do no more processing; skip the unprocessed\r
+            * input check below.\r
+            */\r
+            return;\r
+        }\r
+\r
+        /* Do we have a complete row? */\r
+        if (png_ptr->zstream.avail_out == 0)\r
+           png_push_process_row(png_ptr);\r
+      }\r
+\r
+      /* And check for the end of the stream. */\r
+      if (ret == Z_STREAM_END)\r
+        png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;\r
+   }\r
+\r
+   /* All the data should have been processed, if anything\r
+    * is left at this point we have bytes of IDAT data\r
+    * after the zlib end code.\r
+    */\r
+   if (png_ptr->zstream.avail_in > 0)\r
+      png_warning(png_ptr, "Extra compression data");\r
+}\r
+\r
+void /* PRIVATE */\r
+png_push_process_row(png_structp png_ptr)\r
+{\r
+   png_ptr->row_info.color_type = png_ptr->color_type;\r
+   png_ptr->row_info.width = png_ptr->iwidth;\r
+   png_ptr->row_info.channels = png_ptr->channels;\r
+   png_ptr->row_info.bit_depth = png_ptr->bit_depth;\r
+   png_ptr->row_info.pixel_depth = png_ptr->pixel_depth;\r
+\r
+   png_ptr->row_info.rowbytes = PNG_ROWBYTES(png_ptr->row_info.pixel_depth,\r
+       png_ptr->row_info.width);\r
+\r
+   png_read_filter_row(png_ptr, &(png_ptr->row_info),\r
+       png_ptr->row_buf + 1, png_ptr->prev_row + 1,\r
+       (int)(png_ptr->row_buf[0]));\r
+\r
+   png_memcpy(png_ptr->prev_row, png_ptr->row_buf, png_ptr->rowbytes + 1);\r
+\r
+   if (png_ptr->transformations || (png_ptr->flags&PNG_FLAG_STRIP_ALPHA))\r
+      png_do_read_transformations(png_ptr);\r
+\r
+#ifdef PNG_READ_INTERLACING_SUPPORTED\r
+   /* Blow up interlaced rows to full size */\r
+   if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))\r
+   {\r
+      if (png_ptr->pass < 6)\r
+/*       old interface (pre-1.0.9):\r
+         png_do_read_interlace(&(png_ptr->row_info),\r
+             png_ptr->row_buf + 1, png_ptr->pass, png_ptr->transformations);\r
+ */\r
+         png_do_read_interlace(png_ptr);\r
+\r
+    switch (png_ptr->pass)\r
+    {\r
+         case 0:\r
+         {\r
+            int i;\r
+            for (i = 0; i < 8 && png_ptr->pass == 0; i++)\r
+            {\r
+               png_push_have_row(png_ptr, png_ptr->row_buf + 1);\r
+               png_read_push_finish_row(png_ptr); /* Updates png_ptr->pass */\r
+            }\r
+\r
+            if (png_ptr->pass == 2) /* Pass 1 might be empty */\r
+            {\r
+               for (i = 0; i < 4 && png_ptr->pass == 2; i++)\r
+               {\r
+                  png_push_have_row(png_ptr, NULL);\r
+                  png_read_push_finish_row(png_ptr);\r
+               }\r
+            }\r
+\r
+            if (png_ptr->pass == 4 && png_ptr->height <= 4)\r
+            {\r
+               for (i = 0; i < 2 && png_ptr->pass == 4; i++)\r
+               {\r
+                  png_push_have_row(png_ptr, NULL);\r
+                  png_read_push_finish_row(png_ptr);\r
+               }\r
+            }\r
+\r
+            if (png_ptr->pass == 6 && png_ptr->height <= 4)\r
+            {\r
+                  png_push_have_row(png_ptr, NULL);\r
+                png_read_push_finish_row(png_ptr);\r
+            }\r
+\r
+            break;\r
+         }\r
+\r
+         case 1:\r
+         {\r
+            int i;\r
+            for (i = 0; i < 8 && png_ptr->pass == 1; i++)\r
+            {\r
+               png_push_have_row(png_ptr, png_ptr->row_buf + 1);\r
+               png_read_push_finish_row(png_ptr);\r
+            }\r
+\r
+            if (png_ptr->pass == 2) /* Skip top 4 generated rows */\r
+            {\r
+               for (i = 0; i < 4 && png_ptr->pass == 2; i++)\r
+               {\r
+                  png_push_have_row(png_ptr, NULL);\r
+                  png_read_push_finish_row(png_ptr);\r
+               }\r
+            }\r
+\r
+            break;\r
+         }\r
+\r
+         case 2:\r
+         {\r
+            int i;\r
+\r
+            for (i = 0; i < 4 && png_ptr->pass == 2; i++)\r
+            {\r
+               png_push_have_row(png_ptr, png_ptr->row_buf + 1);\r
+               png_read_push_finish_row(png_ptr);\r
+            }\r
+\r
+            for (i = 0; i < 4 && png_ptr->pass == 2; i++)\r
+            {\r
+                  png_push_have_row(png_ptr, NULL);\r
+               png_read_push_finish_row(png_ptr);\r
+            }\r
+\r
+            if (png_ptr->pass == 4) /* Pass 3 might be empty */\r
+            {\r
+               for (i = 0; i < 2 && png_ptr->pass == 4; i++)\r
+               {\r
+                  png_push_have_row(png_ptr, NULL);\r
+                  png_read_push_finish_row(png_ptr);\r
+               }\r
+            }\r
+\r
+            break;\r
+         }\r
+\r
+         case 3:\r
+         {\r
+            int i;\r
+\r
+            for (i = 0; i < 4 && png_ptr->pass == 3; i++)\r
+            {\r
+               png_push_have_row(png_ptr, png_ptr->row_buf + 1);\r
+               png_read_push_finish_row(png_ptr);\r
+            }\r
+\r
+            if (png_ptr->pass == 4) /* Skip top two generated rows */\r
+            {\r
+               for (i = 0; i < 2 && png_ptr->pass == 4; i++)\r
+               {\r
+                  png_push_have_row(png_ptr, NULL);\r
+                  png_read_push_finish_row(png_ptr);\r
+               }\r
+            }\r
+\r
+            break;\r
+         }\r
+\r
+         case 4:\r
+         {\r
+            int i;\r
+\r
+            for (i = 0; i < 2 && png_ptr->pass == 4; i++)\r
+            {\r
+               png_push_have_row(png_ptr, png_ptr->row_buf + 1);\r
+               png_read_push_finish_row(png_ptr);\r
+            }\r
+\r
+            for (i = 0; i < 2 && png_ptr->pass == 4; i++)\r
+            {\r
+                  png_push_have_row(png_ptr, NULL);\r
+               png_read_push_finish_row(png_ptr);\r
+            }\r
+\r
+            if (png_ptr->pass == 6) /* Pass 5 might be empty */\r
+            {\r
+                  png_push_have_row(png_ptr, NULL);\r
+               png_read_push_finish_row(png_ptr);\r
+            }\r
+\r
+            break;\r
+         }\r
+\r
+         case 5:\r
+         {\r
+            int i;\r
+\r
+            for (i = 0; i < 2 && png_ptr->pass == 5; i++)\r
+            {\r
+               png_push_have_row(png_ptr, png_ptr->row_buf + 1);\r
+               png_read_push_finish_row(png_ptr);\r
+            }\r
+\r
+            if (png_ptr->pass == 6) /* Skip top generated row */\r
+            {\r
+                  png_push_have_row(png_ptr, NULL);\r
+               png_read_push_finish_row(png_ptr);\r
+            }\r
+\r
+            break;\r
+         }\r
+         case 6:\r
+         {\r
+            png_push_have_row(png_ptr, png_ptr->row_buf + 1);\r
+            png_read_push_finish_row(png_ptr);\r
+\r
+            if (png_ptr->pass != 6)\r
+               break;\r
+\r
+                  png_push_have_row(png_ptr, NULL);\r
+            png_read_push_finish_row(png_ptr);\r
+         }\r
+      }\r
+   }\r
+   else\r
+#endif\r
+   {\r
+      png_push_have_row(png_ptr, png_ptr->row_buf + 1);\r
+      png_read_push_finish_row(png_ptr);\r
+   }\r
+}\r
+\r
+void /* PRIVATE */\r
+png_read_push_finish_row(png_structp png_ptr)\r
+{\r
+   /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */\r
+\r
+   /* Start of interlace block */\r
+   PNG_CONST int FARDATA png_pass_start[] = {0, 4, 0, 2, 0, 1, 0};\r
+\r
+   /* Offset to next interlace block */\r
+   PNG_CONST int FARDATA png_pass_inc[] = {8, 8, 4, 4, 2, 2, 1};\r
+\r
+   /* Start of interlace block in the y direction */\r
+   PNG_CONST int FARDATA png_pass_ystart[] = {0, 0, 4, 0, 2, 0, 1};\r
+\r
+   /* Offset to next interlace block in the y direction */\r
+   PNG_CONST int FARDATA png_pass_yinc[] = {8, 8, 8, 4, 4, 2, 2};\r
+\r
+   /* Height of interlace block.  This is not currently used - if you need\r
+    * it, uncomment it here and in png.h\r
+   PNG_CONST int FARDATA png_pass_height[] = {8, 8, 4, 4, 2, 2, 1};\r
+   */\r
+\r
+   png_ptr->row_number++;\r
+   if (png_ptr->row_number < png_ptr->num_rows)\r
+      return;\r
+\r
+#ifdef PNG_READ_INTERLACING_SUPPORTED\r
+   if (png_ptr->interlaced)\r
+   {\r
+      png_ptr->row_number = 0;\r
+      png_memset(png_ptr->prev_row, 0,\r
+         png_ptr->rowbytes + 1);\r
+      do\r
+      {\r
+         png_ptr->pass++;\r
+         if ((png_ptr->pass == 1 && png_ptr->width < 5) ||\r
+             (png_ptr->pass == 3 && png_ptr->width < 3) ||\r
+             (png_ptr->pass == 5 && png_ptr->width < 2))\r
+           png_ptr->pass++;\r
+\r
+         if (png_ptr->pass > 7)\r
+            png_ptr->pass--;\r
+\r
+         if (png_ptr->pass >= 7)\r
+            break;\r
+\r
+         png_ptr->iwidth = (png_ptr->width +\r
+            png_pass_inc[png_ptr->pass] - 1 -\r
+            png_pass_start[png_ptr->pass]) /\r
+            png_pass_inc[png_ptr->pass];\r
+\r
+         if (png_ptr->transformations & PNG_INTERLACE)\r
+            break;\r
+\r
+         png_ptr->num_rows = (png_ptr->height +\r
+            png_pass_yinc[png_ptr->pass] - 1 -\r
+            png_pass_ystart[png_ptr->pass]) /\r
+            png_pass_yinc[png_ptr->pass];\r
+\r
+      } while (png_ptr->iwidth == 0 || png_ptr->num_rows == 0);\r
+   }\r
+#endif /* PNG_READ_INTERLACING_SUPPORTED */\r
+}\r
+\r
+#ifdef PNG_READ_tEXt_SUPPORTED\r
+void /* PRIVATE */\r
+png_push_handle_tEXt(png_structp png_ptr, png_infop info_ptr, png_uint_32\r
+   length)\r
+{\r
+   if (!(png_ptr->mode & PNG_HAVE_IHDR) || (png_ptr->mode & PNG_HAVE_IEND))\r
+      {\r
+         png_error(png_ptr, "Out of place tEXt");\r
+         info_ptr = info_ptr; /* To quiet some compiler warnings */\r
+      }\r
+\r
+#ifdef PNG_MAX_MALLOC_64K\r
+   png_ptr->skip_length = 0;  /* This may not be necessary */\r
+\r
+   if (length > (png_uint_32)65535L) /* Can't hold entire string in memory */\r
+   {\r
+      png_warning(png_ptr, "tEXt chunk too large to fit in memory");\r
+      png_ptr->skip_length = length - (png_uint_32)65535L;\r
+      length = (png_uint_32)65535L;\r
+   }\r
+#endif\r
+\r
+   png_ptr->current_text = (png_charp)png_malloc(png_ptr,\r
+      (png_size_t)(length + 1));\r
+   png_ptr->current_text[length] = '\0';\r
+   png_ptr->current_text_ptr = png_ptr->current_text;\r
+   png_ptr->current_text_size = (png_size_t)length;\r
+   png_ptr->current_text_left = (png_size_t)length;\r
+   png_ptr->process_mode = PNG_READ_tEXt_MODE;\r
+}\r
+\r
+void /* PRIVATE */\r
+png_push_read_tEXt(png_structp png_ptr, png_infop info_ptr)\r
+{\r
+   if (png_ptr->buffer_size && png_ptr->current_text_left)\r
+   {\r
+      png_size_t text_size;\r
+\r
+      if (png_ptr->buffer_size < png_ptr->current_text_left)\r
+         text_size = png_ptr->buffer_size;\r
+\r
+      else\r
+         text_size = png_ptr->current_text_left;\r
+\r
+      png_crc_read(png_ptr, (png_bytep)png_ptr->current_text_ptr, text_size);\r
+      png_ptr->current_text_left -= text_size;\r
+      png_ptr->current_text_ptr += text_size;\r
+   }\r
+   if (!(png_ptr->current_text_left))\r
+   {\r
+      png_textp text_ptr;\r
+      png_charp text;\r
+      png_charp key;\r
+      int ret;\r
+\r
+      if (png_ptr->buffer_size < 4)\r
+      {\r
+         png_push_save_buffer(png_ptr);\r
+         return;\r
+      }\r
+\r
+      png_push_crc_finish(png_ptr);\r
+\r
+#ifdef PNG_MAX_MALLOC_64K\r
+      if (png_ptr->skip_length)\r
+         return;\r
+#endif\r
+\r
+      key = png_ptr->current_text;\r
+\r
+      for (text = key; *text; text++)\r
+         /* Empty loop */ ;\r
+\r
+      if (text < key + png_ptr->current_text_size)\r
+         text++;\r
+\r
+      text_ptr = (png_textp)png_malloc(png_ptr,\r
+         png_sizeof(png_text));\r
+      text_ptr->compression = PNG_TEXT_COMPRESSION_NONE;\r
+      text_ptr->key = key;\r
+#ifdef PNG_iTXt_SUPPORTED\r
+      text_ptr->lang = NULL;\r
+      text_ptr->lang_key = NULL;\r
+#endif\r
+      text_ptr->text = text;\r
+\r
+      ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1);\r
+\r
+      png_free(png_ptr, key);\r
+      png_free(png_ptr, text_ptr);\r
+      png_ptr->current_text = NULL;\r
+\r
+      if (ret)\r
+        png_warning(png_ptr, "Insufficient memory to store text chunk");\r
+   }\r
+}\r
+#endif\r
+\r
+#ifdef PNG_READ_zTXt_SUPPORTED\r
+void /* PRIVATE */\r
+png_push_handle_zTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32\r
+   length)\r
+{\r
+   if (!(png_ptr->mode & PNG_HAVE_IHDR) || (png_ptr->mode & PNG_HAVE_IEND))\r
+      {\r
+         png_error(png_ptr, "Out of place zTXt");\r
+         info_ptr = info_ptr; /* To quiet some compiler warnings */\r
+      }\r
+\r
+#ifdef PNG_MAX_MALLOC_64K\r
+   /* We can't handle zTXt chunks > 64K, since we don't have enough space\r
+    * to be able to store the uncompressed data.  Actually, the threshold\r
+    * is probably around 32K, but it isn't as definite as 64K is.\r
+    */\r
+   if (length > (png_uint_32)65535L)\r
+   {\r
+      png_warning(png_ptr, "zTXt chunk too large to fit in memory");\r
+      png_push_crc_skip(png_ptr, length);\r
+      return;\r
+   }\r
+#endif\r
+\r
+   png_ptr->current_text = (png_charp)png_malloc(png_ptr,\r
+      (png_size_t)(length + 1));\r
+   png_ptr->current_text[length] = '\0';\r
+   png_ptr->current_text_ptr = png_ptr->current_text;\r
+   png_ptr->current_text_size = (png_size_t)length;\r
+   png_ptr->current_text_left = (png_size_t)length;\r
+   png_ptr->process_mode = PNG_READ_zTXt_MODE;\r
+}\r
+\r
+void /* PRIVATE */\r
+png_push_read_zTXt(png_structp png_ptr, png_infop info_ptr)\r
+{\r
+   if (png_ptr->buffer_size && png_ptr->current_text_left)\r
+   {\r
+      png_size_t text_size;\r
+\r
+      if (png_ptr->buffer_size < (png_uint_32)png_ptr->current_text_left)\r
+         text_size = png_ptr->buffer_size;\r
+\r
+      else\r
+         text_size = png_ptr->current_text_left;\r
+\r
+      png_crc_read(png_ptr, (png_bytep)png_ptr->current_text_ptr, text_size);\r
+      png_ptr->current_text_left -= text_size;\r
+      png_ptr->current_text_ptr += text_size;\r
+   }\r
+   if (!(png_ptr->current_text_left))\r
+   {\r
+      png_textp text_ptr;\r
+      png_charp text;\r
+      png_charp key;\r
+      int ret;\r
+      png_size_t text_size, key_size;\r
+\r
+      if (png_ptr->buffer_size < 4)\r
+      {\r
+         png_push_save_buffer(png_ptr);\r
+         return;\r
+      }\r
+\r
+      png_push_crc_finish(png_ptr);\r
+\r
+      key = png_ptr->current_text;\r
+\r
+      for (text = key; *text; text++)\r
+         /* Empty loop */ ;\r
+\r
+      /* zTXt can't have zero text */\r
+      if (text >= key + png_ptr->current_text_size)\r
+      {\r
+         png_ptr->current_text = NULL;\r
+         png_free(png_ptr, key);\r
+         return;\r
+      }\r
+\r
+      text++;\r
+\r
+      if (*text != PNG_TEXT_COMPRESSION_zTXt) /* Check compression byte */\r
+      {\r
+         png_ptr->current_text = NULL;\r
+         png_free(png_ptr, key);\r
+         return;\r
+      }\r
+\r
+      text++;\r
+\r
+      png_ptr->zstream.next_in = (png_bytep )text;\r
+      png_ptr->zstream.avail_in = (uInt)(png_ptr->current_text_size -\r
+         (text - key));\r
+      png_ptr->zstream.next_out = png_ptr->zbuf;\r
+      png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;\r
+\r
+      key_size = text - key;\r
+      text_size = 0;\r
+      text = NULL;\r
+      ret = Z_STREAM_END;\r
+\r
+      while (png_ptr->zstream.avail_in)\r
+      {\r
+         ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);\r
+         if (ret != Z_OK && ret != Z_STREAM_END)\r
+         {\r
+            inflateReset(&png_ptr->zstream);\r
+            png_ptr->zstream.avail_in = 0;\r
+            png_ptr->current_text = NULL;\r
+            png_free(png_ptr, key);\r
+            png_free(png_ptr, text);\r
+            return;\r
+         }\r
+         if (!(png_ptr->zstream.avail_out) || ret == Z_STREAM_END)\r
+         {\r
+            if (text == NULL)\r
+            {\r
+               text = (png_charp)png_malloc(png_ptr,\r
+                     (png_ptr->zbuf_size\r
+                     - png_ptr->zstream.avail_out + key_size + 1));\r
+\r
+               png_memcpy(text + key_size, png_ptr->zbuf,\r
+                  png_ptr->zbuf_size - png_ptr->zstream.avail_out);\r
+\r
+               png_memcpy(text, key, key_size);\r
+\r
+               text_size = key_size + png_ptr->zbuf_size -\r
+                  png_ptr->zstream.avail_out;\r
+\r
+               *(text + text_size) = '\0';\r
+            }\r
+            else\r
+            {\r
+               png_charp tmp;\r
+\r
+               tmp = text;\r
+               text = (png_charp)png_malloc(png_ptr, text_size +\r
+                  (png_ptr->zbuf_size\r
+                  - png_ptr->zstream.avail_out + 1));\r
+\r
+               png_memcpy(text, tmp, text_size);\r
+               png_free(png_ptr, tmp);\r
+\r
+               png_memcpy(text + text_size, png_ptr->zbuf,\r
+                  png_ptr->zbuf_size - png_ptr->zstream.avail_out);\r
+\r
+               text_size += png_ptr->zbuf_size - png_ptr->zstream.avail_out;\r
+               *(text + text_size) = '\0';\r
+            }\r
+            if (ret != Z_STREAM_END)\r
+            {\r
+               png_ptr->zstream.next_out = png_ptr->zbuf;\r
+               png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;\r
+            }\r
+         }\r
+         else\r
+         {\r
+            break;\r
+         }\r
+\r
+         if (ret == Z_STREAM_END)\r
+            break;\r
+      }\r
+\r
+      inflateReset(&png_ptr->zstream);\r
+      png_ptr->zstream.avail_in = 0;\r
+\r
+      if (ret != Z_STREAM_END)\r
+      {\r
+         png_ptr->current_text = NULL;\r
+         png_free(png_ptr, key);\r
+         png_free(png_ptr, text);\r
+         return;\r
+      }\r
+\r
+      png_ptr->current_text = NULL;\r
+      png_free(png_ptr, key);\r
+      key = text;\r
+      text += key_size;\r
+\r
+      text_ptr = (png_textp)png_malloc(png_ptr,\r
+          png_sizeof(png_text));\r
+      text_ptr->compression = PNG_TEXT_COMPRESSION_zTXt;\r
+      text_ptr->key = key;\r
+#ifdef PNG_iTXt_SUPPORTED\r
+      text_ptr->lang = NULL;\r
+      text_ptr->lang_key = NULL;\r
+#endif\r
+      text_ptr->text = text;\r
+\r
+      ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1);\r
+\r
+      png_free(png_ptr, key);\r
+      png_free(png_ptr, text_ptr);\r
+\r
+      if (ret)\r
+        png_warning(png_ptr, "Insufficient memory to store text chunk");\r
+   }\r
+}\r
+#endif\r
+\r
+#ifdef PNG_READ_iTXt_SUPPORTED\r
+void /* PRIVATE */\r
+png_push_handle_iTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32\r
+   length)\r
+{\r
+   if (!(png_ptr->mode & PNG_HAVE_IHDR) || (png_ptr->mode & PNG_HAVE_IEND))\r
+      {\r
+         png_error(png_ptr, "Out of place iTXt");\r
+         info_ptr = info_ptr; /* To quiet some compiler warnings */\r
+      }\r
+\r
+#ifdef PNG_MAX_MALLOC_64K\r
+   png_ptr->skip_length = 0;  /* This may not be necessary */\r
+\r
+   if (length > (png_uint_32)65535L) /* Can't hold entire string in memory */\r
+   {\r
+      png_warning(png_ptr, "iTXt chunk too large to fit in memory");\r
+      png_ptr->skip_length = length - (png_uint_32)65535L;\r
+      length = (png_uint_32)65535L;\r
+   }\r
+#endif\r
+\r
+   png_ptr->current_text = (png_charp)png_malloc(png_ptr,\r
+      (png_size_t)(length + 1));\r
+   png_ptr->current_text[length] = '\0';\r
+   png_ptr->current_text_ptr = png_ptr->current_text;\r
+   png_ptr->current_text_size = (png_size_t)length;\r
+   png_ptr->current_text_left = (png_size_t)length;\r
+   png_ptr->process_mode = PNG_READ_iTXt_MODE;\r
+}\r
+\r
+void /* PRIVATE */\r
+png_push_read_iTXt(png_structp png_ptr, png_infop info_ptr)\r
+{\r
+\r
+   if (png_ptr->buffer_size && png_ptr->current_text_left)\r
+   {\r
+      png_size_t text_size;\r
+\r
+      if (png_ptr->buffer_size < png_ptr->current_text_left)\r
+         text_size = png_ptr->buffer_size;\r
+\r
+      else\r
+         text_size = png_ptr->current_text_left;\r
+\r
+      png_crc_read(png_ptr, (png_bytep)png_ptr->current_text_ptr, text_size);\r
+      png_ptr->current_text_left -= text_size;\r
+      png_ptr->current_text_ptr += text_size;\r
+   }\r
+   if (!(png_ptr->current_text_left))\r
+   {\r
+      png_textp text_ptr;\r
+      png_charp key;\r
+      int comp_flag;\r
+      png_charp lang;\r
+      png_charp lang_key;\r
+      png_charp text;\r
+      int ret;\r
+\r
+      if (png_ptr->buffer_size < 4)\r
+      {\r
+         png_push_save_buffer(png_ptr);\r
+         return;\r
+      }\r
+\r
+      png_push_crc_finish(png_ptr);\r
+\r
+#ifdef PNG_MAX_MALLOC_64K\r
+      if (png_ptr->skip_length)\r
+         return;\r
+#endif\r
+\r
+      key = png_ptr->current_text;\r
+\r
+      for (lang = key; *lang; lang++)\r
+         /* Empty loop */ ;\r
+\r
+      if (lang < key + png_ptr->current_text_size - 3)\r
+         lang++;\r
+\r
+      comp_flag = *lang++;\r
+      lang++;     /* Skip comp_type, always zero */\r
+\r
+      for (lang_key = lang; *lang_key; lang_key++)\r
+         /* Empty loop */ ;\r
+\r
+      lang_key++;        /* Skip NUL separator */\r
+\r
+      text=lang_key;\r
+\r
+      if (lang_key < key + png_ptr->current_text_size - 1)\r
+      {\r
+        for (; *text; text++)\r
+           /* Empty loop */ ;\r
+      }\r
+\r
+      if (text < key + png_ptr->current_text_size)\r
+         text++;\r
+\r
+      text_ptr = (png_textp)png_malloc(png_ptr,\r
+         png_sizeof(png_text));\r
+\r
+      text_ptr->compression = comp_flag + 2;\r
+      text_ptr->key = key;\r
+      text_ptr->lang = lang;\r
+      text_ptr->lang_key = lang_key;\r
+      text_ptr->text = text;\r
+      text_ptr->text_length = 0;\r
+      text_ptr->itxt_length = png_strlen(text);\r
+\r
+      ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1);\r
+\r
+      png_ptr->current_text = NULL;\r
+\r
+      png_free(png_ptr, text_ptr);\r
+      if (ret)\r
+         png_warning(png_ptr, "Insufficient memory to store iTXt chunk");\r
+   }\r
+}\r
+#endif\r
+\r
+/* This function is called when we haven't found a handler for this\r
+ * chunk.  If there isn't a problem with the chunk itself (ie a bad chunk\r
+ * name or a critical chunk), the chunk is (currently) silently ignored.\r
+ */\r
+void /* PRIVATE */\r
+png_push_handle_unknown(png_structp png_ptr, png_infop info_ptr, png_uint_32\r
+   length)\r
+{\r
+   png_uint_32 skip = 0;\r
+\r
+   if (!(png_ptr->chunk_name[0] & 0x20))\r
+   {\r
+#ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED\r
+      if (png_handle_as_unknown(png_ptr, png_ptr->chunk_name) !=\r
+         PNG_HANDLE_CHUNK_ALWAYS\r
+#ifdef PNG_READ_USER_CHUNKS_SUPPORTED\r
+         && png_ptr->read_user_chunk_fn == NULL\r
+#endif\r
+         )\r
+#endif\r
+         png_chunk_error(png_ptr, "unknown critical chunk");\r
+\r
+      info_ptr = info_ptr; /* To quiet some compiler warnings */\r
+   }\r
+\r
+#ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED\r
+   if (png_ptr->flags & PNG_FLAG_KEEP_UNKNOWN_CHUNKS)\r
+   {\r
+#ifdef PNG_MAX_MALLOC_64K\r
+      if (length > (png_uint_32)65535L)\r
+      {\r
+          png_warning(png_ptr, "unknown chunk too large to fit in memory");\r
+          skip = length - (png_uint_32)65535L;\r
+          length = (png_uint_32)65535L;\r
+      }\r
+#endif\r
+      png_memcpy((png_charp)png_ptr->unknown_chunk.name,\r
+                 (png_charp)png_ptr->chunk_name,\r
+                 png_sizeof(png_ptr->unknown_chunk.name));\r
+      png_ptr->unknown_chunk.name[png_sizeof(png_ptr->unknown_chunk.name) - 1]\r
+        = '\0';\r
+\r
+      png_ptr->unknown_chunk.size = (png_size_t)length;\r
+\r
+      if (length == 0)\r
+         png_ptr->unknown_chunk.data = NULL;\r
+\r
+      else\r
+      {\r
+         png_ptr->unknown_chunk.data = (png_bytep)png_malloc(png_ptr,\r
+            (png_size_t)length);\r
+         png_crc_read(png_ptr, (png_bytep)png_ptr->unknown_chunk.data, length);\r
+      }\r
+\r
+#ifdef PNG_READ_USER_CHUNKS_SUPPORTED\r
+      if (png_ptr->read_user_chunk_fn != NULL)\r
+      {\r
+         /* Callback to user unknown chunk handler */\r
+         int ret;\r
+         ret = (*(png_ptr->read_user_chunk_fn))\r
+           (png_ptr, &png_ptr->unknown_chunk);\r
+\r
+         if (ret < 0)\r
+            png_chunk_error(png_ptr, "error in user chunk");\r
+\r
+         if (ret == 0)\r
+         {\r
+            if (!(png_ptr->chunk_name[0] & 0x20))\r
+               if (png_handle_as_unknown(png_ptr, png_ptr->chunk_name) !=\r
+                    PNG_HANDLE_CHUNK_ALWAYS)\r
+                  png_chunk_error(png_ptr, "unknown critical chunk");\r
+            png_set_unknown_chunks(png_ptr, info_ptr,\r
+               &png_ptr->unknown_chunk, 1);\r
+         }\r
+      }\r
+\r
+      else\r
+#endif\r
+        png_set_unknown_chunks(png_ptr, info_ptr, &png_ptr->unknown_chunk, 1);\r
+      png_free(png_ptr, png_ptr->unknown_chunk.data);\r
+      png_ptr->unknown_chunk.data = NULL;\r
+   }\r
+\r
+   else\r
+#endif\r
+      skip=length;\r
+   png_push_crc_skip(png_ptr, skip);\r
+}\r
+\r
+void /* PRIVATE */\r
+png_push_have_info(png_structp png_ptr, png_infop info_ptr)\r
+{\r
+   if (png_ptr->info_fn != NULL)\r
+      (*(png_ptr->info_fn))(png_ptr, info_ptr);\r
+}\r
+\r
+void /* PRIVATE */\r
+png_push_have_end(png_structp png_ptr, png_infop info_ptr)\r
+{\r
+   if (png_ptr->end_fn != NULL)\r
+      (*(png_ptr->end_fn))(png_ptr, info_ptr);\r
+}\r
+\r
+void /* PRIVATE */\r
+png_push_have_row(png_structp png_ptr, png_bytep row)\r
+{\r
+   if (png_ptr->row_fn != NULL)\r
+      (*(png_ptr->row_fn))(png_ptr, row, png_ptr->row_number,\r
+         (int)png_ptr->pass);\r
+}\r
+\r
+void PNGAPI\r
+png_progressive_combine_row (png_structp png_ptr,\r
+   png_bytep old_row, png_bytep new_row)\r
+{\r
+   PNG_CONST int FARDATA png_pass_dsp_mask[7] =\r
+      {0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff};\r
+\r
+   if (png_ptr == NULL)\r
+      return;\r
+\r
+   if (new_row != NULL)    /* new_row must == png_ptr->row_buf here. */\r
+      png_combine_row(png_ptr, old_row, png_pass_dsp_mask[png_ptr->pass]);\r
+}\r
+\r
+void PNGAPI\r
+png_set_progressive_read_fn(png_structp png_ptr, png_voidp progressive_ptr,\r
+   png_progressive_info_ptr info_fn, png_progressive_row_ptr row_fn,\r
+   png_progressive_end_ptr end_fn)\r
+{\r
+   if (png_ptr == NULL)\r
+      return;\r
+\r
+   png_ptr->info_fn = info_fn;\r
+   png_ptr->row_fn = row_fn;\r
+   png_ptr->end_fn = end_fn;\r
+\r
+   png_set_read_fn(png_ptr, progressive_ptr, png_push_fill_buffer);\r
+}\r
+\r
+png_voidp PNGAPI\r
+png_get_progressive_ptr(png_structp png_ptr)\r
+{\r
+   if (png_ptr == NULL)\r
+      return (NULL);\r
+\r
+   return png_ptr->io_ptr;\r
+}\r
+#endif /* PNG_PROGRESSIVE_READ_SUPPORTED */\r
diff --git a/3rdparty/libpng/pngpriv.h b/3rdparty/libpng/pngpriv.h
new file mode 100644 (file)
index 0000000..1521bee
--- /dev/null
@@ -0,0 +1,960 @@
+
+/* pngpriv.h - private declarations for use inside libpng
+ *
+ * libpng version 1.4.3 - June 26, 2010
+ * For conditions of distribution and use, see copyright notice in png.h
+ * Copyright (c) 1998-2010 Glenn Randers-Pehrson
+ * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
+ * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
+ *
+ * This code is released under the libpng license.
+ * For conditions of distribution and use, see the disclaimer
+ * and license in png.h
+ */
+
+/* The symbols declared in this file (including the functions declared
+ * as PNG_EXTERN) are PRIVATE.  They are not part of the libpng public
+ * interface, and are not recommended for use by regular applications.
+ * Some of them may become public in the future; others may stay private,
+ * change in an incompatible way, or even disappear.
+ * Although the libpng users are not forbidden to include this header,
+ * they should be well aware of the issues that may arise from doing so.
+ */
+
+#ifndef PNGPRIV_H
+#define PNGPRIV_H
+
+#ifndef PNG_VERSION_INFO_ONLY
+
+#include <stdlib.h>
+
+#if defined _MSC_VER && _MSC_VER >= 1400
+#pragma warning(disable: 4267)
+#endif
+
+/* The functions exported by PNG_EXTERN are internal functions, which
+ * aren't usually used outside the library (as far as I know), so it is
+ * debatable if they should be exported at all.  In the future, when it
+ * is possible to have run-time registry of chunk-handling functions,
+ * some of these will be made available again.
+#define PNG_EXTERN extern
+ */
+#define PNG_EXTERN
+
+/* Other defines specific to compilers can go here.  Try to keep
+ * them inside an appropriate ifdef/endif pair for portability.
+ */
+
+#ifdef PNG_FLOATING_POINT_SUPPORTED
+#  ifdef MACOS
+     /* We need to check that <math.h> hasn't already been included earlier
+      * as it seems it doesn't agree with <fp.h>, yet we should really use
+      * <fp.h> if possible.
+      */
+#    if !defined(__MATH_H__) && !defined(__MATH_H) && !defined(__cmath__)
+#      include <fp.h>
+#    endif
+#  else
+#    include <math.h>
+#  endif
+#  if defined(_AMIGA) && defined(__SASC) && defined(_M68881)
+     /* Amiga SAS/C: We must include builtin FPU functions when compiling using
+      * MATH=68881
+      */
+#    include <m68881.h>
+#  endif
+#endif
+
+/* Codewarrior on NT has linking problems without this. */
+#if (defined(__MWERKS__) && defined(WIN32)) || defined(__STDC__)
+#  define PNG_ALWAYS_EXTERN
+#endif
+
+/* This provides the non-ANSI (far) memory allocation routines. */
+#if defined(__TURBOC__) && defined(__MSDOS__)
+#  include <mem.h>
+#  include <alloc.h>
+#endif
+
+#if defined(WIN32) || defined(_Windows) || defined(_WINDOWS) || \
+    defined(_WIN32) || defined(__WIN32__)
+#  include <windows.h>  /* defines _WINDOWS_ macro */
+/* I have no idea why is this necessary... */
+#  ifdef _MSC_VER
+#    include <malloc.h>
+#  endif
+#endif
+
+/* Various modes of operation.  Note that after an init, mode is set to
+ * zero automatically when the structure is created.
+ */
+#define PNG_HAVE_IHDR               0x01
+#define PNG_HAVE_PLTE               0x02
+#define PNG_HAVE_IDAT               0x04
+#define PNG_AFTER_IDAT              0x08 /* Have complete zlib datastream */
+#define PNG_HAVE_IEND               0x10
+#define PNG_HAVE_gAMA               0x20
+#define PNG_HAVE_cHRM               0x40
+#define PNG_HAVE_sRGB               0x80
+#define PNG_HAVE_CHUNK_HEADER      0x100
+#define PNG_WROTE_tIME             0x200
+#define PNG_WROTE_INFO_BEFORE_PLTE 0x400
+#define PNG_BACKGROUND_IS_GRAY     0x800
+#define PNG_HAVE_PNG_SIGNATURE    0x1000
+#define PNG_HAVE_CHUNK_AFTER_IDAT 0x2000 /* Have another chunk after IDAT */
+
+/* Flags for the transformations the PNG library does on the image data */
+#define PNG_BGR                 0x0001
+#define PNG_INTERLACE           0x0002
+#define PNG_PACK                0x0004
+#define PNG_SHIFT               0x0008
+#define PNG_SWAP_BYTES          0x0010
+#define PNG_INVERT_MONO         0x0020
+#define PNG_QUANTIZE            0x0040 /* formerly PNG_DITHER */
+#define PNG_BACKGROUND          0x0080
+#define PNG_BACKGROUND_EXPAND   0x0100
+                          /*    0x0200 unused */
+#define PNG_16_TO_8             0x0400
+#define PNG_RGBA                0x0800
+#define PNG_EXPAND              0x1000
+#define PNG_GAMMA               0x2000
+#define PNG_GRAY_TO_RGB         0x4000
+#define PNG_FILLER              0x8000L
+#define PNG_PACKSWAP           0x10000L
+#define PNG_SWAP_ALPHA         0x20000L
+#define PNG_STRIP_ALPHA        0x40000L
+#define PNG_INVERT_ALPHA       0x80000L
+#define PNG_USER_TRANSFORM    0x100000L
+#define PNG_RGB_TO_GRAY_ERR   0x200000L
+#define PNG_RGB_TO_GRAY_WARN  0x400000L
+#define PNG_RGB_TO_GRAY       0x600000L  /* two bits, RGB_TO_GRAY_ERR|WARN */
+                       /*     0x800000L     Unused */
+#define PNG_ADD_ALPHA         0x1000000L  /* Added to libpng-1.2.7 */
+#define PNG_EXPAND_tRNS       0x2000000L  /* Added to libpng-1.2.9 */
+                       /*   0x4000000L  unused */
+                       /*   0x8000000L  unused */
+                       /*  0x10000000L  unused */
+                       /*  0x20000000L  unused */
+                       /*  0x40000000L  unused */
+
+/* Flags for png_create_struct */
+#define PNG_STRUCT_PNG   0x0001
+#define PNG_STRUCT_INFO  0x0002
+
+/* Scaling factor for filter heuristic weighting calculations */
+#define PNG_WEIGHT_SHIFT 8
+#define PNG_WEIGHT_FACTOR (1<<(PNG_WEIGHT_SHIFT))
+#define PNG_COST_SHIFT 3
+#define PNG_COST_FACTOR (1<<(PNG_COST_SHIFT))
+
+/* Flags for the png_ptr->flags rather than declaring a byte for each one */
+#define PNG_FLAG_ZLIB_CUSTOM_STRATEGY     0x0001
+#define PNG_FLAG_ZLIB_CUSTOM_LEVEL        0x0002
+#define PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL    0x0004
+#define PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS  0x0008
+#define PNG_FLAG_ZLIB_CUSTOM_METHOD       0x0010
+#define PNG_FLAG_ZLIB_FINISHED            0x0020
+#define PNG_FLAG_ROW_INIT                 0x0040
+#define PNG_FLAG_FILLER_AFTER             0x0080
+#define PNG_FLAG_CRC_ANCILLARY_USE        0x0100
+#define PNG_FLAG_CRC_ANCILLARY_NOWARN     0x0200
+#define PNG_FLAG_CRC_CRITICAL_USE         0x0400
+#define PNG_FLAG_CRC_CRITICAL_IGNORE      0x0800
+                                /*        0x1000  unused */
+                                /*        0x2000  unused */
+                                /*        0x4000  unused */
+#define PNG_FLAG_KEEP_UNKNOWN_CHUNKS      0x8000L
+#define PNG_FLAG_KEEP_UNSAFE_CHUNKS       0x10000L
+#define PNG_FLAG_LIBRARY_MISMATCH         0x20000L
+#define PNG_FLAG_STRIP_ERROR_NUMBERS      0x40000L
+#define PNG_FLAG_STRIP_ERROR_TEXT         0x80000L
+#define PNG_FLAG_MALLOC_NULL_MEM_OK       0x100000L
+#define PNG_FLAG_ADD_ALPHA                0x200000L  /* Added to libpng-1.2.8 */
+#define PNG_FLAG_STRIP_ALPHA              0x400000L  /* Added to libpng-1.2.8 */
+#define PNG_FLAG_BENIGN_ERRORS_WARN       0x800000L  /* Added to libpng-1.4.0 */
+                                  /*     0x1000000L  unused */
+                                  /*     0x2000000L  unused */
+                                  /*     0x4000000L  unused */
+                                  /*     0x8000000L  unused */
+                                  /*    0x10000000L  unused */
+                                  /*    0x20000000L  unused */
+                                  /*    0x40000000L  unused */
+
+#define PNG_FLAG_CRC_ANCILLARY_MASK (PNG_FLAG_CRC_ANCILLARY_USE | \
+                                     PNG_FLAG_CRC_ANCILLARY_NOWARN)
+
+#define PNG_FLAG_CRC_CRITICAL_MASK  (PNG_FLAG_CRC_CRITICAL_USE | \
+                                     PNG_FLAG_CRC_CRITICAL_IGNORE)
+
+#define PNG_FLAG_CRC_MASK           (PNG_FLAG_CRC_ANCILLARY_MASK | \
+                                     PNG_FLAG_CRC_CRITICAL_MASK)
+
+/* Save typing and make code easier to understand */
+
+#define PNG_COLOR_DIST(c1, c2) (abs((int)((c1).red) - (int)((c2).red)) + \
+   abs((int)((c1).green) - (int)((c2).green)) + \
+   abs((int)((c1).blue) - (int)((c2).blue)))
+
+/* Added to libpng-1.2.6 JB */
+#define PNG_ROWBYTES(pixel_bits, width) \
+    ((pixel_bits) >= 8 ? \
+    ((png_size_t)(width) * (((png_size_t)(pixel_bits)) >> 3)) : \
+    (( ((png_size_t)(width) * ((png_size_t)(pixel_bits))) + 7) >> 3) )
+
+/* PNG_OUT_OF_RANGE returns true if value is outside the range
+ * ideal-delta..ideal+delta.  Each argument is evaluated twice.
+ * "ideal" and "delta" should be constants, normally simple
+ * integers, "value" a variable. Added to libpng-1.2.6 JB
+ */
+#define PNG_OUT_OF_RANGE(value, ideal, delta) \
+        ( (value) < (ideal)-(delta) || (value) > (ideal)+(delta) )
+
+/* Constant strings for known chunk types.  If you need to add a chunk,
+ * define the name here, and add an invocation of the macro wherever it's
+ * needed.
+ */
+#define PNG_IHDR PNG_CONST png_byte png_IHDR[5] = { 73,  72,  68,  82, '\0'}
+#define PNG_IDAT PNG_CONST png_byte png_IDAT[5] = { 73,  68,  65,  84, '\0'}
+#define PNG_IEND PNG_CONST png_byte png_IEND[5] = { 73,  69,  78,  68, '\0'}
+#define PNG_PLTE PNG_CONST png_byte png_PLTE[5] = { 80,  76,  84,  69, '\0'}
+#define PNG_bKGD PNG_CONST png_byte png_bKGD[5] = { 98,  75,  71,  68, '\0'}
+#define PNG_cHRM PNG_CONST png_byte png_cHRM[5] = { 99,  72,  82,  77, '\0'}
+#define PNG_gAMA PNG_CONST png_byte png_gAMA[5] = {103,  65,  77,  65, '\0'}
+#define PNG_hIST PNG_CONST png_byte png_hIST[5] = {104,  73,  83,  84, '\0'}
+#define PNG_iCCP PNG_CONST png_byte png_iCCP[5] = {105,  67,  67,  80, '\0'}
+#define PNG_iTXt PNG_CONST png_byte png_iTXt[5] = {105,  84,  88, 116, '\0'}
+#define PNG_oFFs PNG_CONST png_byte png_oFFs[5] = {111,  70,  70, 115, '\0'}
+#define PNG_pCAL PNG_CONST png_byte png_pCAL[5] = {112,  67,  65,  76, '\0'}
+#define PNG_sCAL PNG_CONST png_byte png_sCAL[5] = {115,  67,  65,  76, '\0'}
+#define PNG_pHYs PNG_CONST png_byte png_pHYs[5] = {112,  72,  89, 115, '\0'}
+#define PNG_sBIT PNG_CONST png_byte png_sBIT[5] = {115,  66,  73,  84, '\0'}
+#define PNG_sPLT PNG_CONST png_byte png_sPLT[5] = {115,  80,  76,  84, '\0'}
+#define PNG_sRGB PNG_CONST png_byte png_sRGB[5] = {115,  82,  71,  66, '\0'}
+#define PNG_sTER PNG_CONST png_byte png_sTER[5] = {115,  84,  69,  82, '\0'}
+#define PNG_tEXt PNG_CONST png_byte png_tEXt[5] = {116,  69,  88, 116, '\0'}
+#define PNG_tIME PNG_CONST png_byte png_tIME[5] = {116,  73,  77,  69, '\0'}
+#define PNG_tRNS PNG_CONST png_byte png_tRNS[5] = {116,  82,  78,  83, '\0'}
+#define PNG_zTXt PNG_CONST png_byte png_zTXt[5] = {122,  84,  88, 116, '\0'}
+
+
+/* Inhibit C++ name-mangling for libpng functions but not for system calls. */
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
+
+/* These functions are used internally in the code.  They generally
+ * shouldn't be used unless you are writing code to add or replace some
+ * functionality in libpng.  More information about most functions can
+ * be found in the files where the functions are located.
+ */
+
+/* Allocate memory for an internal libpng struct */
+PNG_EXTERN png_voidp png_create_struct PNGARG((int type));
+
+/* Free memory from internal libpng struct */
+PNG_EXTERN void png_destroy_struct PNGARG((png_voidp struct_ptr));
+
+PNG_EXTERN png_voidp png_create_struct_2 PNGARG((int type, png_malloc_ptr
+  malloc_fn, png_voidp mem_ptr));
+PNG_EXTERN void png_destroy_struct_2 PNGARG((png_voidp struct_ptr,
+   png_free_ptr free_fn, png_voidp mem_ptr));
+
+/* Free any memory that info_ptr points to and reset struct. */
+PNG_EXTERN void png_info_destroy PNGARG((png_structp png_ptr,
+   png_infop info_ptr));
+
+/* Function to allocate memory for zlib.  PNGAPI is disallowed. */
+PNG_EXTERN voidpf png_zalloc PNGARG((voidpf png_ptr, uInt items, uInt size));
+
+/* Function to free memory for zlib.  PNGAPI is disallowed. */
+PNG_EXTERN void png_zfree PNGARG((voidpf png_ptr, voidpf ptr));
+
+/* Next four functions are used internally as callbacks.  PNGAPI is required
+ * but not PNG_EXPORT.  PNGAPI added at libpng version 1.2.3. */
+
+PNG_EXTERN void PNGAPI png_default_read_data PNGARG((png_structp png_ptr,
+   png_bytep data, png_size_t length));
+
+#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
+PNG_EXTERN void PNGAPI png_push_fill_buffer PNGARG((png_structp png_ptr,
+   png_bytep buffer, png_size_t length));
+#endif
+
+PNG_EXTERN void PNGAPI png_default_write_data PNGARG((png_structp png_ptr,
+   png_bytep data, png_size_t length));
+
+#ifdef PNG_WRITE_FLUSH_SUPPORTED
+#ifdef PNG_STDIO_SUPPORTED
+PNG_EXTERN void PNGAPI png_default_flush PNGARG((png_structp png_ptr));
+#endif
+#endif
+
+/* Reset the CRC variable */
+PNG_EXTERN void png_reset_crc PNGARG((png_structp png_ptr));
+
+/* Write the "data" buffer to whatever output you are using */
+PNG_EXTERN void png_write_data PNGARG((png_structp png_ptr, png_bytep data,
+   png_size_t length));
+
+/* Read the chunk header (length + type name) */
+PNG_EXTERN png_uint_32 png_read_chunk_header PNGARG((png_structp png_ptr));
+
+/* Read data from whatever input you are using into the "data" buffer */
+PNG_EXTERN void png_read_data PNGARG((png_structp png_ptr, png_bytep data,
+   png_size_t length));
+
+/* Read bytes into buf, and update png_ptr->crc */
+PNG_EXTERN void png_crc_read PNGARG((png_structp png_ptr, png_bytep buf,
+   png_size_t length));
+
+/* Decompress data in a chunk that uses compression */
+#if defined(PNG_zTXt_SUPPORTED) || defined(PNG_iTXt_SUPPORTED) || \
+    defined(PNG_iCCP_SUPPORTED) || defined(PNG_sPLT_SUPPORTED)
+PNG_EXTERN void png_decompress_chunk PNGARG((png_structp png_ptr,
+   int comp_type, png_size_t chunklength, png_size_t prefix_length,
+   png_size_t *data_length));
+#endif
+
+/* Read "skip" bytes, read the file crc, and (optionally) verify png_ptr->crc */
+PNG_EXTERN int png_crc_finish PNGARG((png_structp png_ptr, png_uint_32 skip));
+
+/* Read the CRC from the file and compare it to the libpng calculated CRC */
+PNG_EXTERN int png_crc_error PNGARG((png_structp png_ptr));
+
+/* Calculate the CRC over a section of data.  Note that we are only
+ * passing a maximum of 64K on systems that have this as a memory limit,
+ * since this is the maximum buffer size we can specify.
+ */
+PNG_EXTERN void png_calculate_crc PNGARG((png_structp png_ptr, png_bytep ptr,
+   png_size_t length));
+
+#ifdef PNG_WRITE_FLUSH_SUPPORTED
+PNG_EXTERN void png_flush PNGARG((png_structp png_ptr));
+#endif
+
+/* Write various chunks */
+
+/* Write the IHDR chunk, and update the png_struct with the necessary
+ * information.
+ */
+PNG_EXTERN void png_write_IHDR PNGARG((png_structp png_ptr, png_uint_32 width,
+   png_uint_32 height,
+   int bit_depth, int color_type, int compression_method, int filter_method,
+   int interlace_method));
+
+PNG_EXTERN void png_write_PLTE PNGARG((png_structp png_ptr, png_colorp palette,
+   png_uint_32 num_pal));
+
+PNG_EXTERN void png_write_IDAT PNGARG((png_structp png_ptr, png_bytep data,
+   png_size_t length));
+
+PNG_EXTERN void png_write_IEND PNGARG((png_structp png_ptr));
+
+#ifdef PNG_WRITE_gAMA_SUPPORTED
+#ifdef PNG_FLOATING_POINT_SUPPORTED
+PNG_EXTERN void png_write_gAMA PNGARG((png_structp png_ptr, double file_gamma));
+#endif
+#ifdef PNG_FIXED_POINT_SUPPORTED
+PNG_EXTERN void png_write_gAMA_fixed PNGARG((png_structp png_ptr,
+    png_fixed_point file_gamma));
+#endif
+#endif
+
+#ifdef PNG_WRITE_sBIT_SUPPORTED
+PNG_EXTERN void png_write_sBIT PNGARG((png_structp png_ptr, png_color_8p sbit,
+   int color_type));
+#endif
+
+#ifdef PNG_WRITE_cHRM_SUPPORTED
+#ifdef PNG_FLOATING_POINT_SUPPORTED
+PNG_EXTERN void png_write_cHRM PNGARG((png_structp png_ptr,
+   double white_x, double white_y,
+   double red_x, double red_y, double green_x, double green_y,
+   double blue_x, double blue_y));
+#endif
+PNG_EXTERN void png_write_cHRM_fixed PNGARG((png_structp png_ptr,
+   png_fixed_point int_white_x, png_fixed_point int_white_y,
+   png_fixed_point int_red_x, png_fixed_point int_red_y, png_fixed_point
+   int_green_x, png_fixed_point int_green_y, png_fixed_point int_blue_x,
+   png_fixed_point int_blue_y));
+#endif
+
+#ifdef PNG_WRITE_sRGB_SUPPORTED
+PNG_EXTERN void png_write_sRGB PNGARG((png_structp png_ptr,
+   int intent));
+#endif
+
+#ifdef PNG_WRITE_iCCP_SUPPORTED
+PNG_EXTERN void png_write_iCCP PNGARG((png_structp png_ptr,
+   png_charp name, int compression_type,
+   png_charp profile, int proflen));
+   /* Note to maintainer: profile should be png_bytep */
+#endif
+
+#ifdef PNG_WRITE_sPLT_SUPPORTED
+PNG_EXTERN void png_write_sPLT PNGARG((png_structp png_ptr,
+   png_sPLT_tp palette));
+#endif
+
+#ifdef PNG_WRITE_tRNS_SUPPORTED
+PNG_EXTERN void png_write_tRNS PNGARG((png_structp png_ptr, png_bytep trans,
+   png_color_16p values, int number, int color_type));
+#endif
+
+#ifdef PNG_WRITE_bKGD_SUPPORTED
+PNG_EXTERN void png_write_bKGD PNGARG((png_structp png_ptr,
+   png_color_16p values, int color_type));
+#endif
+
+#ifdef PNG_WRITE_hIST_SUPPORTED
+PNG_EXTERN void png_write_hIST PNGARG((png_structp png_ptr, png_uint_16p hist,
+   int num_hist));
+#endif
+
+#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_pCAL_SUPPORTED) || \
+    defined(PNG_WRITE_iCCP_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
+PNG_EXTERN png_size_t png_check_keyword PNGARG((png_structp png_ptr,
+   png_charp key, png_charpp new_key));
+#endif
+
+#ifdef PNG_WRITE_tEXt_SUPPORTED
+PNG_EXTERN void png_write_tEXt PNGARG((png_structp png_ptr, png_charp key,
+   png_charp text, png_size_t text_len));
+#endif
+
+#ifdef PNG_WRITE_zTXt_SUPPORTED
+PNG_EXTERN void png_write_zTXt PNGARG((png_structp png_ptr, png_charp key,
+   png_charp text, png_size_t text_len, int compression));
+#endif
+
+#ifdef PNG_WRITE_iTXt_SUPPORTED
+PNG_EXTERN void png_write_iTXt PNGARG((png_structp png_ptr,
+   int compression, png_charp key, png_charp lang, png_charp lang_key,
+   png_charp text));
+#endif
+
+#ifdef PNG_TEXT_SUPPORTED  /* Added at version 1.0.14 and 1.2.4 */
+PNG_EXTERN int png_set_text_2 PNGARG((png_structp png_ptr,
+   png_infop info_ptr, png_textp text_ptr, int num_text));
+#endif
+
+#ifdef PNG_WRITE_oFFs_SUPPORTED
+PNG_EXTERN void png_write_oFFs PNGARG((png_structp png_ptr,
+   png_int_32 x_offset, png_int_32 y_offset, int unit_type));
+#endif
+
+#ifdef PNG_WRITE_pCAL_SUPPORTED
+PNG_EXTERN void png_write_pCAL PNGARG((png_structp png_ptr, png_charp purpose,
+   png_int_32 X0, png_int_32 X1, int type, int nparams,
+   png_charp units, png_charpp params));
+#endif
+
+#ifdef PNG_WRITE_pHYs_SUPPORTED
+PNG_EXTERN void png_write_pHYs PNGARG((png_structp png_ptr,
+   png_uint_32 x_pixels_per_unit, png_uint_32 y_pixels_per_unit,
+   int unit_type));
+#endif
+
+#ifdef PNG_WRITE_tIME_SUPPORTED
+PNG_EXTERN void png_write_tIME PNGARG((png_structp png_ptr,
+   png_timep mod_time));
+#endif
+
+#ifdef PNG_WRITE_sCAL_SUPPORTED
+#if defined(PNG_FLOATING_POINT_SUPPORTED) && defined(PNG_STDIO_SUPPORTED)
+PNG_EXTERN void png_write_sCAL PNGARG((png_structp png_ptr,
+   int unit, double width, double height));
+#else
+#ifdef PNG_FIXED_POINT_SUPPORTED
+PNG_EXTERN void png_write_sCAL_s PNGARG((png_structp png_ptr,
+   int unit, png_charp width, png_charp height));
+#endif
+#endif
+#endif
+
+/* Called when finished processing a row of data */
+PNG_EXTERN void png_write_finish_row PNGARG((png_structp png_ptr));
+
+/* Internal use only.   Called before first row of data */
+PNG_EXTERN void png_write_start_row PNGARG((png_structp png_ptr));
+
+#ifdef PNG_READ_GAMMA_SUPPORTED
+PNG_EXTERN void png_build_gamma_table PNGARG((png_structp png_ptr,
+   png_byte bit_depth));
+#endif
+
+/* Combine a row of data, dealing with alpha, etc. if requested */
+PNG_EXTERN void png_combine_row PNGARG((png_structp png_ptr, png_bytep row,
+   int mask));
+
+#ifdef PNG_READ_INTERLACING_SUPPORTED
+/* Expand an interlaced row */
+/* OLD pre-1.0.9 interface:
+PNG_EXTERN void png_do_read_interlace PNGARG((png_row_infop row_info,
+   png_bytep row, int pass, png_uint_32 transformations));
+ */
+PNG_EXTERN void png_do_read_interlace PNGARG((png_structp png_ptr));
+#endif
+
+/* GRR TO DO (2.0 or whenever):  simplify other internal calling interfaces */
+
+#ifdef PNG_WRITE_INTERLACING_SUPPORTED
+/* Grab pixels out of a row for an interlaced pass */
+PNG_EXTERN void png_do_write_interlace PNGARG((png_row_infop row_info,
+   png_bytep row, int pass));
+#endif
+
+/* Unfilter a row */
+PNG_EXTERN void png_read_filter_row PNGARG((png_structp png_ptr,
+   png_row_infop row_info, png_bytep row, png_bytep prev_row, int filter));
+
+/* Choose the best filter to use and filter the row data */
+PNG_EXTERN void png_write_find_filter PNGARG((png_structp png_ptr,
+   png_row_infop row_info));
+
+/* Write out the filtered row. */
+PNG_EXTERN void png_write_filtered_row PNGARG((png_structp png_ptr,
+   png_bytep filtered_row));
+/* Finish a row while reading, dealing with interlacing passes, etc. */
+PNG_EXTERN void png_read_finish_row PNGARG((png_structp png_ptr));
+
+/* Initialize the row buffers, etc. */
+PNG_EXTERN void png_read_start_row PNGARG((png_structp png_ptr));
+/* Optional call to update the users info structure */
+PNG_EXTERN void png_read_transform_info PNGARG((png_structp png_ptr,
+   png_infop info_ptr));
+
+/* These are the functions that do the transformations */
+#ifdef PNG_READ_FILLER_SUPPORTED
+PNG_EXTERN void png_do_read_filler PNGARG((png_row_infop row_info,
+   png_bytep row, png_uint_32 filler, png_uint_32 flags));
+#endif
+
+#ifdef PNG_READ_SWAP_ALPHA_SUPPORTED
+PNG_EXTERN void png_do_read_swap_alpha PNGARG((png_row_infop row_info,
+   png_bytep row));
+#endif
+
+#ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED
+PNG_EXTERN void png_do_write_swap_alpha PNGARG((png_row_infop row_info,
+   png_bytep row));
+#endif
+
+#ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
+PNG_EXTERN void png_do_read_invert_alpha PNGARG((png_row_infop row_info,
+   png_bytep row));
+#endif
+
+#ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
+PNG_EXTERN void png_do_write_invert_alpha PNGARG((png_row_infop row_info,
+   png_bytep row));
+#endif
+
+#if defined(PNG_WRITE_FILLER_SUPPORTED) || \
+    defined(PNG_READ_STRIP_ALPHA_SUPPORTED)
+PNG_EXTERN void png_do_strip_filler PNGARG((png_row_infop row_info,
+   png_bytep row, png_uint_32 flags));
+#endif
+
+#if defined(PNG_READ_SWAP_SUPPORTED) || defined(PNG_WRITE_SWAP_SUPPORTED)
+PNG_EXTERN void png_do_swap PNGARG((png_row_infop row_info, png_bytep row));
+#endif
+
+#if defined(PNG_READ_PACKSWAP_SUPPORTED) || \
+    defined(PNG_WRITE_PACKSWAP_SUPPORTED)
+PNG_EXTERN void png_do_packswap PNGARG((png_row_infop row_info, png_bytep row));
+#endif
+
+#ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
+PNG_EXTERN int png_do_rgb_to_gray PNGARG((png_structp png_ptr, png_row_infop
+   row_info, png_bytep row));
+#endif
+
+#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
+PNG_EXTERN void png_do_gray_to_rgb PNGARG((png_row_infop row_info,
+   png_bytep row));
+#endif
+
+#ifdef PNG_READ_PACK_SUPPORTED
+PNG_EXTERN void png_do_unpack PNGARG((png_row_infop row_info, png_bytep row));
+#endif
+
+#ifdef PNG_READ_SHIFT_SUPPORTED
+PNG_EXTERN void png_do_unshift PNGARG((png_row_infop row_info, png_bytep row,
+   png_color_8p sig_bits));
+#endif
+
+#if defined(PNG_READ_INVERT_SUPPORTED) || defined(PNG_WRITE_INVERT_SUPPORTED)
+PNG_EXTERN void png_do_invert PNGARG((png_row_infop row_info, png_bytep row));
+#endif
+
+#ifdef PNG_READ_16_TO_8_SUPPORTED
+PNG_EXTERN void png_do_chop PNGARG((png_row_infop row_info, png_bytep row));
+#endif
+
+#ifdef PNG_READ_QUANTIZE_SUPPORTED
+PNG_EXTERN void png_do_quantize PNGARG((png_row_infop row_info,
+   png_bytep row, png_bytep palette_lookup, png_bytep quantize_lookup));
+
+#  ifdef PNG_CORRECT_PALETTE_SUPPORTED
+PNG_EXTERN void png_correct_palette PNGARG((png_structp png_ptr,
+   png_colorp palette, int num_palette));
+#  endif
+#endif
+
+#if defined(PNG_READ_BGR_SUPPORTED) || defined(PNG_WRITE_BGR_SUPPORTED)
+PNG_EXTERN void png_do_bgr PNGARG((png_row_infop row_info, png_bytep row));
+#endif
+
+#ifdef PNG_WRITE_PACK_SUPPORTED
+PNG_EXTERN void png_do_pack PNGARG((png_row_infop row_info,
+   png_bytep row, png_uint_32 bit_depth));
+#endif
+
+#ifdef PNG_WRITE_SHIFT_SUPPORTED
+PNG_EXTERN void png_do_shift PNGARG((png_row_infop row_info, png_bytep row,
+   png_color_8p bit_depth));
+#endif
+
+#ifdef PNG_READ_BACKGROUND_SUPPORTED
+#ifdef PNG_READ_GAMMA_SUPPORTED
+PNG_EXTERN void png_do_background PNGARG((png_row_infop row_info, png_bytep row,
+   png_color_16p trans_color, png_color_16p background,
+   png_color_16p background_1,
+   png_bytep gamma_table, png_bytep gamma_from_1, png_bytep gamma_to_1,
+   png_uint_16pp gamma_16, png_uint_16pp gamma_16_from_1,
+   png_uint_16pp gamma_16_to_1, int gamma_shift));
+#else
+PNG_EXTERN void png_do_background PNGARG((png_row_infop row_info, png_bytep row,
+   png_color_16p trans_color, png_color_16p background));
+#endif
+#endif
+
+#ifdef PNG_READ_GAMMA_SUPPORTED
+PNG_EXTERN void png_do_gamma PNGARG((png_row_infop row_info, png_bytep row,
+   png_bytep gamma_table, png_uint_16pp gamma_16_table,
+   int gamma_shift));
+#endif
+
+#ifdef PNG_READ_EXPAND_SUPPORTED
+PNG_EXTERN void png_do_expand_palette PNGARG((png_row_infop row_info,
+   png_bytep row, png_colorp palette, png_bytep trans, int num_trans));
+PNG_EXTERN void png_do_expand PNGARG((png_row_infop row_info,
+   png_bytep row, png_color_16p trans_value));
+#endif
+
+/* The following decodes the appropriate chunks, and does error correction,
+ * then calls the appropriate callback for the chunk if it is valid.
+ */
+
+/* Decode the IHDR chunk */
+PNG_EXTERN void png_handle_IHDR PNGARG((png_structp png_ptr, png_infop info_ptr,
+   png_uint_32 length));
+PNG_EXTERN void png_handle_PLTE PNGARG((png_structp png_ptr, png_infop info_ptr,
+   png_uint_32 length));
+PNG_EXTERN void png_handle_IEND PNGARG((png_structp png_ptr, png_infop info_ptr,
+   png_uint_32 length));
+
+#ifdef PNG_READ_bKGD_SUPPORTED
+PNG_EXTERN void png_handle_bKGD PNGARG((png_structp png_ptr, png_infop info_ptr,
+   png_uint_32 length));
+#endif
+
+#ifdef PNG_READ_cHRM_SUPPORTED
+PNG_EXTERN void png_handle_cHRM PNGARG((png_structp png_ptr, png_infop info_ptr,
+   png_uint_32 length));
+#endif
+
+#ifdef PNG_READ_gAMA_SUPPORTED
+PNG_EXTERN void png_handle_gAMA PNGARG((png_structp png_ptr, png_infop info_ptr,
+   png_uint_32 length));
+#endif
+
+#ifdef PNG_READ_hIST_SUPPORTED
+PNG_EXTERN void png_handle_hIST PNGARG((png_structp png_ptr, png_infop info_ptr,
+   png_uint_32 length));
+#endif
+
+#ifdef PNG_READ_iCCP_SUPPORTED
+extern void png_handle_iCCP PNGARG((png_structp png_ptr, png_infop info_ptr,
+   png_uint_32 length));
+#endif /* PNG_READ_iCCP_SUPPORTED */
+
+#ifdef PNG_READ_iTXt_SUPPORTED
+PNG_EXTERN void png_handle_iTXt PNGARG((png_structp png_ptr, png_infop info_ptr,
+   png_uint_32 length));
+#endif
+
+#ifdef PNG_READ_oFFs_SUPPORTED
+PNG_EXTERN void png_handle_oFFs PNGARG((png_structp png_ptr, png_infop info_ptr,
+   png_uint_32 length));
+#endif
+
+#ifdef PNG_READ_pCAL_SUPPORTED
+PNG_EXTERN void png_handle_pCAL PNGARG((png_structp png_ptr, png_infop info_ptr,
+   png_uint_32 length));
+#endif
+
+#ifdef PNG_READ_pHYs_SUPPORTED
+PNG_EXTERN void png_handle_pHYs PNGARG((png_structp png_ptr, png_infop info_ptr,
+   png_uint_32 length));
+#endif
+
+#ifdef PNG_READ_sBIT_SUPPORTED
+PNG_EXTERN void png_handle_sBIT PNGARG((png_structp png_ptr, png_infop info_ptr,
+   png_uint_32 length));
+#endif
+
+#ifdef PNG_READ_sCAL_SUPPORTED
+PNG_EXTERN void png_handle_sCAL PNGARG((png_structp png_ptr, png_infop info_ptr,
+   png_uint_32 length));
+#endif
+
+#ifdef PNG_READ_sPLT_SUPPORTED
+extern void png_handle_sPLT PNGARG((png_structp png_ptr, png_infop info_ptr,
+   png_uint_32 length));
+#endif /* PNG_READ_sPLT_SUPPORTED */
+
+#ifdef PNG_READ_sRGB_SUPPORTED
+PNG_EXTERN void png_handle_sRGB PNGARG((png_structp png_ptr, png_infop info_ptr,
+   png_uint_32 length));
+#endif
+
+#ifdef PNG_READ_tEXt_SUPPORTED
+PNG_EXTERN void png_handle_tEXt PNGARG((png_structp png_ptr, png_infop info_ptr,
+   png_uint_32 length));
+#endif
+
+#ifdef PNG_READ_tIME_SUPPORTED
+PNG_EXTERN void png_handle_tIME PNGARG((png_structp png_ptr, png_infop info_ptr,
+   png_uint_32 length));
+#endif
+
+#ifdef PNG_READ_tRNS_SUPPORTED
+PNG_EXTERN void png_handle_tRNS PNGARG((png_structp png_ptr, png_infop info_ptr,
+   png_uint_32 length));
+#endif
+
+#ifdef PNG_READ_zTXt_SUPPORTED
+PNG_EXTERN void png_handle_zTXt PNGARG((png_structp png_ptr, png_infop info_ptr,
+   png_uint_32 length));
+#endif
+
+PNG_EXTERN void png_handle_unknown PNGARG((png_structp png_ptr,
+   png_infop info_ptr, png_uint_32 length));
+
+PNG_EXTERN void png_check_chunk_name PNGARG((png_structp png_ptr,
+   png_bytep chunk_name));
+
+/* Handle the transformations for reading and writing */
+PNG_EXTERN void png_do_read_transformations PNGARG((png_structp png_ptr));
+PNG_EXTERN void png_do_write_transformations PNGARG((png_structp png_ptr));
+
+PNG_EXTERN void png_init_read_transformations PNGARG((png_structp png_ptr));
+
+#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
+PNG_EXTERN void png_push_read_chunk PNGARG((png_structp png_ptr,
+   png_infop info_ptr));
+PNG_EXTERN void png_push_read_sig PNGARG((png_structp png_ptr,
+   png_infop info_ptr));
+PNG_EXTERN void png_push_check_crc PNGARG((png_structp png_ptr));
+PNG_EXTERN void png_push_crc_skip PNGARG((png_structp png_ptr,
+   png_uint_32 length));
+PNG_EXTERN void png_push_crc_finish PNGARG((png_structp png_ptr));
+PNG_EXTERN void png_push_save_buffer PNGARG((png_structp png_ptr));
+PNG_EXTERN void png_push_restore_buffer PNGARG((png_structp png_ptr,
+   png_bytep buffer, png_size_t buffer_length));
+PNG_EXTERN void png_push_read_IDAT PNGARG((png_structp png_ptr));
+PNG_EXTERN void png_process_IDAT_data PNGARG((png_structp png_ptr,
+   png_bytep buffer, png_size_t buffer_length));
+PNG_EXTERN void png_push_process_row PNGARG((png_structp png_ptr));
+PNG_EXTERN void png_push_handle_unknown PNGARG((png_structp png_ptr,
+   png_infop info_ptr, png_uint_32 length));
+PNG_EXTERN void png_push_have_info PNGARG((png_structp png_ptr,
+   png_infop info_ptr));
+PNG_EXTERN void png_push_have_end PNGARG((png_structp png_ptr,
+   png_infop info_ptr));
+PNG_EXTERN void png_push_have_row PNGARG((png_structp png_ptr, png_bytep row));
+PNG_EXTERN void png_push_read_end PNGARG((png_structp png_ptr,
+   png_infop info_ptr));
+PNG_EXTERN void png_process_some_data PNGARG((png_structp png_ptr,
+   png_infop info_ptr));
+PNG_EXTERN void png_read_push_finish_row PNGARG((png_structp png_ptr));
+#ifdef PNG_READ_tEXt_SUPPORTED
+PNG_EXTERN void png_push_handle_tEXt PNGARG((png_structp png_ptr,
+   png_infop info_ptr, png_uint_32 length));
+PNG_EXTERN void png_push_read_tEXt PNGARG((png_structp png_ptr,
+   png_infop info_ptr));
+#endif
+#ifdef PNG_READ_zTXt_SUPPORTED
+PNG_EXTERN void png_push_handle_zTXt PNGARG((png_structp png_ptr,
+   png_infop info_ptr, png_uint_32 length));
+PNG_EXTERN void png_push_read_zTXt PNGARG((png_structp png_ptr,
+   png_infop info_ptr));
+#endif
+#ifdef PNG_READ_iTXt_SUPPORTED
+PNG_EXTERN void png_push_handle_iTXt PNGARG((png_structp png_ptr,
+   png_infop info_ptr, png_uint_32 length));
+PNG_EXTERN void png_push_read_iTXt PNGARG((png_structp png_ptr,
+   png_infop info_ptr));
+#endif
+
+#endif /* PNG_PROGRESSIVE_READ_SUPPORTED */
+
+#ifdef PNG_MNG_FEATURES_SUPPORTED
+PNG_EXTERN void png_do_read_intrapixel PNGARG((png_row_infop row_info,
+   png_bytep row));
+PNG_EXTERN void png_do_write_intrapixel PNGARG((png_row_infop row_info,
+   png_bytep row));
+#endif
+
+/* Added at libpng version 1.4.0 */
+#ifdef PNG_cHRM_SUPPORTED
+PNG_EXTERN int png_check_cHRM_fixed PNGARG((png_structp png_ptr,
+   png_fixed_point int_white_x, png_fixed_point int_white_y,
+   png_fixed_point int_red_x, png_fixed_point int_red_y, png_fixed_point
+   int_green_x, png_fixed_point int_green_y, png_fixed_point int_blue_x,
+   png_fixed_point int_blue_y));
+#endif
+
+#ifdef PNG_cHRM_SUPPORTED
+#ifdef PNG_CHECK_cHRM_SUPPORTED
+/* Added at libpng version 1.2.34 and 1.4.0 */
+PNG_EXTERN void png_64bit_product PNGARG((long v1, long v2,
+   unsigned long *hi_product, unsigned long *lo_product));
+#endif
+#endif
+
+/* Added at libpng version 1.4.0 */
+PNG_EXTERN void png_check_IHDR PNGARG((png_structp png_ptr,
+   png_uint_32 width, png_uint_32 height, int bit_depth,
+   int color_type, int interlace_type, int compression_type,
+   int filter_type));
+
+/* Free all memory used by the read (old method - NOT DLL EXPORTED) */
+extern void png_read_destroy PNGARG((png_structp png_ptr, png_infop info_ptr,
+   png_infop end_info_ptr));
+
+/* Free any memory used in png_ptr struct (old method - NOT DLL EXPORTED) */
+extern void png_write_destroy PNGARG((png_structp png_ptr));
+
+#ifdef USE_FAR_KEYWORD  /* memory model conversion function */
+extern void *png_far_to_near PNGARG((png_structp png_ptr,png_voidp ptr,
+   int check));
+#endif /* USE_FAR_KEYWORD */
+
+/* Define PNG_DEBUG at compile time for debugging information.  Higher
+ * numbers for PNG_DEBUG mean more debugging information.  This has
+ * only been added since version 0.95 so it is not implemented throughout
+ * libpng yet, but more support will be added as needed.
+ */
+#ifdef PNG_DEBUG
+#if (PNG_DEBUG > 0)
+#if !defined(PNG_DEBUG_FILE) && defined(_MSC_VER)
+#include <crtdbg.h>
+#if (PNG_DEBUG > 1)
+#ifndef _DEBUG
+#  define _DEBUG
+#endif
+#ifndef png_debug
+#define png_debug(l,m)  _RPT0(_CRT_WARN,m PNG_STRING_NEWLINE)
+#endif
+#ifndef png_debug1
+#define png_debug1(l,m,p1)  _RPT1(_CRT_WARN,m PNG_STRING_NEWLINE,p1)
+#endif
+#ifndef png_debug2
+#define png_debug2(l,m,p1,p2) _RPT2(_CRT_WARN,m PNG_STRING_NEWLINE,p1,p2)
+#endif
+#endif
+#else /* PNG_DEBUG_FILE || !_MSC_VER */
+#ifndef PNG_DEBUG_FILE
+#define PNG_DEBUG_FILE stderr
+#endif /* PNG_DEBUG_FILE */
+
+#if (PNG_DEBUG > 1)
+/* Note: ["%s"m PNG_STRING_NEWLINE] probably does not work on
+ * non-ISO compilers
+ */
+#  ifdef __STDC__
+#    ifndef png_debug
+#      define png_debug(l,m) \
+       { \
+       int num_tabs=l; \
+       fprintf(PNG_DEBUG_FILE,"%s"m PNG_STRING_NEWLINE,(num_tabs==1 ? "\t" : \
+         (num_tabs==2 ? "\t\t":(num_tabs>2 ? "\t\t\t":"")))); \
+       }
+#    endif
+#    ifndef png_debug1
+#      define png_debug1(l,m,p1) \
+       { \
+       int num_tabs=l; \
+       fprintf(PNG_DEBUG_FILE,"%s"m PNG_STRING_NEWLINE,(num_tabs==1 ? "\t" : \
+         (num_tabs==2 ? "\t\t":(num_tabs>2 ? "\t\t\t":""))),p1); \
+       }
+#    endif
+#    ifndef png_debug2
+#      define png_debug2(l,m,p1,p2) \
+       { \
+       int num_tabs=l; \
+       fprintf(PNG_DEBUG_FILE,"%s"m PNG_STRING_NEWLINE,(num_tabs==1 ? "\t" : \
+         (num_tabs==2 ? "\t\t":(num_tabs>2 ? "\t\t\t":""))),p1,p2); \
+       }
+#    endif
+#  else /* __STDC __ */
+#    ifndef png_debug
+#      define png_debug(l,m) \
+       { \
+       int num_tabs=l; \
+       char format[256]; \
+       snprintf(format,256,"%s%s%s",(num_tabs==1 ? "\t" : \
+         (num_tabs==2 ? "\t\t":(num_tabs>2 ? "\t\t\t":""))), \
+         m,PNG_STRING_NEWLINE); \
+       fprintf(PNG_DEBUG_FILE,format); \
+       }
+#    endif
+#    ifndef png_debug1
+#      define png_debug1(l,m,p1) \
+       { \
+       int num_tabs=l; \
+       char format[256]; \
+       snprintf(format,256,"%s%s%s",(num_tabs==1 ? "\t" : \
+         (num_tabs==2 ? "\t\t":(num_tabs>2 ? "\t\t\t":""))), \
+         m,PNG_STRING_NEWLINE); \
+       fprintf(PNG_DEBUG_FILE,format,p1); \
+       }
+#    endif
+#    ifndef png_debug2
+#      define png_debug2(l,m,p1,p2) \
+       { \
+       int num_tabs=l; \
+       char format[256]; \
+       snprintf(format,256,"%s%s%s",(num_tabs==1 ? "\t" : \
+         (num_tabs==2 ? "\t\t":(num_tabs>2 ? "\t\t\t":""))), \
+         m,PNG_STRING_NEWLINE); \
+       fprintf(PNG_DEBUG_FILE,format,p1,p2); \
+       }
+#    endif
+#  endif /* __STDC __ */
+#endif /* (PNG_DEBUG > 1) */
+
+#endif /* _MSC_VER */
+#endif /* (PNG_DEBUG > 0) */
+#endif /* PNG_DEBUG */
+#ifndef png_debug
+#define png_debug(l, m)
+#endif
+#ifndef png_debug1
+#define png_debug1(l, m, p1)
+#endif
+#ifndef png_debug2
+#define png_debug2(l, m, p1, p2)
+#endif
+
+/* Maintainer: Put new private prototypes here ^ and in libpngpf.3 */
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* PNG_VERSION_INFO_ONLY */
+#endif /* PNGPRIV_H */
index bd8bcd9..3ec2df4 100644 (file)
-
-/* pngread.c - read a PNG file
- *
- * Last changed in libpng 1.2.25 [February 18, 2008]
- * For conditions of distribution and use, see copyright notice in png.h
- * Copyright (c) 1998-2008 Glenn Randers-Pehrson
- * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
- * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
- *
- * This file contains routines that an application calls directly to
- * read a PNG file or stream.
- */
-
-#define PNG_INTERNAL
-#include "png.h"
-
-#if defined(PNG_READ_SUPPORTED)
-
-/* Create a PNG structure for reading, and allocate any memory needed. */
-png_structp PNGAPI
-png_create_read_struct(png_const_charp user_png_ver, png_voidp error_ptr,
-   png_error_ptr error_fn, png_error_ptr warn_fn)
-{
-
-#ifdef PNG_USER_MEM_SUPPORTED
-   return (png_create_read_struct_2(user_png_ver, error_ptr, error_fn,
-      warn_fn, png_voidp_NULL, png_malloc_ptr_NULL, png_free_ptr_NULL));
-}
-
-/* Alternate create PNG structure for reading, and allocate any memory needed. */
-png_structp PNGAPI
-png_create_read_struct_2(png_const_charp user_png_ver, png_voidp error_ptr,
-   png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
-   png_malloc_ptr malloc_fn, png_free_ptr free_fn)
-{
-#endif /* PNG_USER_MEM_SUPPORTED */
-
-   png_structp png_ptr;
-
-#ifdef PNG_SETJMP_SUPPORTED
-#ifdef USE_FAR_KEYWORD
-   jmp_buf jmpbuf;
-#endif
-#endif
-
-   int i;
-
-   png_debug(1, "in png_create_read_struct\n");
-#ifdef PNG_USER_MEM_SUPPORTED
-   png_ptr = (png_structp)png_create_struct_2(PNG_STRUCT_PNG,
-      (png_malloc_ptr)malloc_fn, (png_voidp)mem_ptr);
-#else
-   png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG);
-#endif
-   if (png_ptr == NULL)
-      return (NULL);
-
-   /* added at libpng-1.2.6 */
-#ifdef PNG_SET_USER_LIMITS_SUPPORTED
-   png_ptr->user_width_max=PNG_USER_WIDTH_MAX;
-   png_ptr->user_height_max=PNG_USER_HEIGHT_MAX;
-#endif
-
-#ifdef PNG_SETJMP_SUPPORTED
-#ifdef USE_FAR_KEYWORD
-   if (setjmp(jmpbuf))
-#else
-   if (setjmp(png_ptr->jmpbuf))
-#endif
-   {
-      png_free(png_ptr, png_ptr->zbuf);
-      png_ptr->zbuf=NULL;
-#ifdef PNG_USER_MEM_SUPPORTED
-      png_destroy_struct_2((png_voidp)png_ptr,
-         (png_free_ptr)free_fn, (png_voidp)mem_ptr);
-#else
-      png_destroy_struct((png_voidp)png_ptr);
-#endif
-      return (NULL);
-   }
-#ifdef USE_FAR_KEYWORD
-   png_memcpy(png_ptr->jmpbuf,jmpbuf,png_sizeof(jmp_buf));
-#endif
-#endif
-
-#ifdef PNG_USER_MEM_SUPPORTED
-   png_set_mem_fn(png_ptr, mem_ptr, malloc_fn, free_fn);
-#endif
-
-   png_set_error_fn(png_ptr, error_ptr, error_fn, warn_fn);
-
-   if(user_png_ver)
-   {
-     i=0;
-     do
-     {
-       if(user_png_ver[i] != png_libpng_ver[i])
-          png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
-     } while (png_libpng_ver[i++]);
-   }
-   else
-        png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
-   
-
-   if (png_ptr->flags & PNG_FLAG_LIBRARY_MISMATCH)
-   {
-     /* Libpng 0.90 and later are binary incompatible with libpng 0.89, so
-      * we must recompile any applications that use any older library version.
-      * For versions after libpng 1.0, we will be compatible, so we need
-      * only check the first digit.
-      */
-     if (user_png_ver == NULL || user_png_ver[0] != png_libpng_ver[0] ||
-         (user_png_ver[0] == '1' && user_png_ver[2] != png_libpng_ver[2]) ||
-         (user_png_ver[0] == '0' && user_png_ver[2] < '9'))
-     {
-#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
-        char msg[80];
-        if (user_png_ver)
-        {
-          png_snprintf(msg, 80,
-             "Application was compiled with png.h from libpng-%.20s",
-             user_png_ver);
-          png_warning(png_ptr, msg);
-        }
-        png_snprintf(msg, 80,
-             "Application  is  running with png.c from libpng-%.20s",
-           png_libpng_ver);
-        png_warning(png_ptr, msg);
-#endif
-#ifdef PNG_ERROR_NUMBERS_SUPPORTED
-        png_ptr->flags=0;
-#endif
-        png_error(png_ptr,
-           "Incompatible libpng version in application and library");
-     }
-   }
-
-   /* initialize zbuf - compression buffer */
-   png_ptr->zbuf_size = PNG_ZBUF_SIZE;
-   png_ptr->zbuf = (png_bytep)png_malloc(png_ptr,
-     (png_uint_32)png_ptr->zbuf_size);
-   png_ptr->zstream.zalloc = png_zalloc;
-   png_ptr->zstream.zfree = png_zfree;
-   png_ptr->zstream.opaque = (voidpf)png_ptr;
-
-   switch (inflateInit(&png_ptr->zstream))
-   {
-     case Z_OK: /* Do nothing */ break;
-     case Z_MEM_ERROR:
-     case Z_STREAM_ERROR: png_error(png_ptr, "zlib memory error"); break;
-     case Z_VERSION_ERROR: png_error(png_ptr, "zlib version error"); break;
-     default: png_error(png_ptr, "Unknown zlib error");
-   }
-
-   png_ptr->zstream.next_out = png_ptr->zbuf;
-   png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
-
-   png_set_read_fn(png_ptr, png_voidp_NULL, png_rw_ptr_NULL);
-
-#ifdef PNG_SETJMP_SUPPORTED
-/* Applications that neglect to set up their own setjmp() and then encounter
-   a png_error() will longjmp here.  Since the jmpbuf is then meaningless we
-   abort instead of returning. */
-#ifdef USE_FAR_KEYWORD
-   if (setjmp(jmpbuf))
-      PNG_ABORT();
-   png_memcpy(png_ptr->jmpbuf,jmpbuf,png_sizeof(jmp_buf));
-#else
-   if (setjmp(png_ptr->jmpbuf))
-      PNG_ABORT();
-#endif
-#endif
-   return (png_ptr);
-}
-
-#if defined(PNG_1_0_X) || defined(PNG_1_2_X)
-/* Initialize PNG structure for reading, and allocate any memory needed.
-   This interface is deprecated in favour of the png_create_read_struct(),
-   and it will disappear as of libpng-1.3.0. */
-#undef png_read_init
-void PNGAPI
-png_read_init(png_structp png_ptr)
-{
-   /* We only come here via pre-1.0.7-compiled applications */
-   png_read_init_2(png_ptr, "1.0.6 or earlier", 0, 0);
-}
-
-void PNGAPI
-png_read_init_2(png_structp png_ptr, png_const_charp user_png_ver,
-   png_size_t png_struct_size, png_size_t png_info_size)
-{
-   /* We only come here via pre-1.0.12-compiled applications */
-   if(png_ptr == NULL) return;
-#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
-   if(png_sizeof(png_struct) > png_struct_size ||
-      png_sizeof(png_info) > png_info_size)
-   {
-      char msg[80];
-      png_ptr->warning_fn=NULL;
-      if (user_png_ver)
-      {
-        png_snprintf(msg, 80,
-           "Application was compiled with png.h from libpng-%.20s",
-           user_png_ver);
-        png_warning(png_ptr, msg);
-      }
-      png_snprintf(msg, 80,
-         "Application  is  running with png.c from libpng-%.20s",
-         png_libpng_ver);
-      png_warning(png_ptr, msg);
-   }
-#endif
-   if(png_sizeof(png_struct) > png_struct_size)
-     {
-       png_ptr->error_fn=NULL;
-#ifdef PNG_ERROR_NUMBERS_SUPPORTED
-       png_ptr->flags=0;
-#endif
-       png_error(png_ptr,
-       "The png struct allocated by the application for reading is too small.");
-     }
-   if(png_sizeof(png_info) > png_info_size)
-     {
-       png_ptr->error_fn=NULL;
-#ifdef PNG_ERROR_NUMBERS_SUPPORTED
-       png_ptr->flags=0;
-#endif
-       png_error(png_ptr,
-         "The info struct allocated by application for reading is too small.");
-     }
-   png_read_init_3(&png_ptr, user_png_ver, png_struct_size);
-}
-#endif /* PNG_1_0_X || PNG_1_2_X */
-
-void PNGAPI
-png_read_init_3(png_structpp ptr_ptr, png_const_charp user_png_ver,
-   png_size_t png_struct_size)
-{
-#ifdef PNG_SETJMP_SUPPORTED
-   jmp_buf tmp_jmp;  /* to save current jump buffer */
-#endif
-
-   int i=0;
-
-   png_structp png_ptr=*ptr_ptr;
-
-   if(png_ptr == NULL) return;
-
-   do
-   {
-     if(user_png_ver[i] != png_libpng_ver[i])
-     {
-#ifdef PNG_LEGACY_SUPPORTED
-       png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
-#else
-       png_ptr->warning_fn=NULL;
-       png_warning(png_ptr,
-        "Application uses deprecated png_read_init() and should be recompiled.");
-       break;
-#endif
-     }
-   } while (png_libpng_ver[i++]);
-
-   png_debug(1, "in png_read_init_3\n");
-
-#ifdef PNG_SETJMP_SUPPORTED
-   /* save jump buffer and error functions */
-   png_memcpy(tmp_jmp, png_ptr->jmpbuf, png_sizeof (jmp_buf));
-#endif
-
-   if(png_sizeof(png_struct) > png_struct_size)
-     {
-       png_destroy_struct(png_ptr);
-       *ptr_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG);
-       png_ptr = *ptr_ptr;
-     }
-
-   /* reset all variables to 0 */
-   png_memset(png_ptr, 0, png_sizeof (png_struct));
-
-#ifdef PNG_SETJMP_SUPPORTED
-   /* restore jump buffer */
-   png_memcpy(png_ptr->jmpbuf, tmp_jmp, png_sizeof (jmp_buf));
-#endif
-
-   /* added at libpng-1.2.6 */
-#ifdef PNG_SET_USER_LIMITS_SUPPORTED
-   png_ptr->user_width_max=PNG_USER_WIDTH_MAX;
-   png_ptr->user_height_max=PNG_USER_HEIGHT_MAX;
-#endif
-
-   /* initialize zbuf - compression buffer */
-   png_ptr->zbuf_size = PNG_ZBUF_SIZE;
-   png_ptr->zbuf = (png_bytep)png_malloc(png_ptr,
-     (png_uint_32)png_ptr->zbuf_size);
-   png_ptr->zstream.zalloc = png_zalloc;
-   png_ptr->zstream.zfree = png_zfree;
-   png_ptr->zstream.opaque = (voidpf)png_ptr;
-
-   switch (inflateInit(&png_ptr->zstream))
-   {
-     case Z_OK: /* Do nothing */ break;
-     case Z_MEM_ERROR:
-     case Z_STREAM_ERROR: png_error(png_ptr, "zlib memory"); break;
-     case Z_VERSION_ERROR: png_error(png_ptr, "zlib version"); break;
-     default: png_error(png_ptr, "Unknown zlib error");
-   }
-
-   png_ptr->zstream.next_out = png_ptr->zbuf;
-   png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
-
-   png_set_read_fn(png_ptr, png_voidp_NULL, png_rw_ptr_NULL);
-}
-
-#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
-/* Read the information before the actual image data.  This has been
- * changed in v0.90 to allow reading a file that already has the magic
- * bytes read from the stream.  You can tell libpng how many bytes have
- * been read from the beginning of the stream (up to the maximum of 8)
- * via png_set_sig_bytes(), and we will only check the remaining bytes
- * here.  The application can then have access to the signature bytes we
- * read if it is determined that this isn't a valid PNG file.
- */
-void PNGAPI
-png_read_info(png_structp png_ptr, png_infop info_ptr)
-{
-   if(png_ptr == NULL || info_ptr == NULL) return;
-   png_debug(1, "in png_read_info\n");
-   /* If we haven't checked all of the PNG signature bytes, do so now. */
-   if (png_ptr->sig_bytes < 8)
-   {
-      png_size_t num_checked = png_ptr->sig_bytes,
-                 num_to_check = 8 - num_checked;
-
-      png_read_data(png_ptr, &(info_ptr->signature[num_checked]), num_to_check);
-      png_ptr->sig_bytes = 8;
-
-      if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check))
-      {
-         if (num_checked < 4 &&
-             png_sig_cmp(info_ptr->signature, num_checked, num_to_check - 4))
-            png_error(png_ptr, "Not a PNG file");
-         else
-            png_error(png_ptr, "PNG file corrupted by ASCII conversion");
-      }
-      if (num_checked < 3)
-         png_ptr->mode |= PNG_HAVE_PNG_SIGNATURE;
-   }
-
-   for(;;)
-   {
-#ifdef PNG_USE_LOCAL_ARRAYS
-      PNG_CONST PNG_IHDR;
-      PNG_CONST PNG_IDAT;
-      PNG_CONST PNG_IEND;
-      PNG_CONST PNG_PLTE;
-#if defined(PNG_READ_bKGD_SUPPORTED)
-      PNG_CONST PNG_bKGD;
-#endif
-#if defined(PNG_READ_cHRM_SUPPORTED)
-      PNG_CONST PNG_cHRM;
-#endif
-#if defined(PNG_READ_gAMA_SUPPORTED)
-      PNG_CONST PNG_gAMA;
-#endif
-#if defined(PNG_READ_hIST_SUPPORTED)
-      PNG_CONST PNG_hIST;
-#endif
-#if defined(PNG_READ_iCCP_SUPPORTED)
-      PNG_CONST PNG_iCCP;
-#endif
-#if defined(PNG_READ_iTXt_SUPPORTED)
-      PNG_CONST PNG_iTXt;
-#endif
-#if defined(PNG_READ_oFFs_SUPPORTED)
-      PNG_CONST PNG_oFFs;
-#endif
-#if defined(PNG_READ_pCAL_SUPPORTED)
-      PNG_CONST PNG_pCAL;
-#endif
-#if defined(PNG_READ_pHYs_SUPPORTED)
-      PNG_CONST PNG_pHYs;
-#endif
-#if defined(PNG_READ_sBIT_SUPPORTED)
-      PNG_CONST PNG_sBIT;
-#endif
-#if defined(PNG_READ_sCAL_SUPPORTED)
-      PNG_CONST PNG_sCAL;
-#endif
-#if defined(PNG_READ_sPLT_SUPPORTED)
-      PNG_CONST PNG_sPLT;
-#endif
-#if defined(PNG_READ_sRGB_SUPPORTED)
-      PNG_CONST PNG_sRGB;
-#endif
-#if defined(PNG_READ_tEXt_SUPPORTED)
-      PNG_CONST PNG_tEXt;
-#endif
-#if defined(PNG_READ_tIME_SUPPORTED)
-      PNG_CONST PNG_tIME;
-#endif
-#if defined(PNG_READ_tRNS_SUPPORTED)
-      PNG_CONST PNG_tRNS;
-#endif
-#if defined(PNG_READ_zTXt_SUPPORTED)
-      PNG_CONST PNG_zTXt;
-#endif
-#endif /* PNG_USE_LOCAL_ARRAYS */
-      png_byte chunk_length[4];
-      png_uint_32 length;
-
-      png_read_data(png_ptr, chunk_length, 4);
-      length = png_get_uint_31(png_ptr,chunk_length);
-
-      png_reset_crc(png_ptr);
-      png_crc_read(png_ptr, png_ptr->chunk_name, 4);
-
-      png_debug2(0, "Reading %s chunk, length=%lu.\n", png_ptr->chunk_name,
-         length);
-
-      /* This should be a binary subdivision search or a hash for
-       * matching the chunk name rather than a linear search.
-       */
-      if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
-        if(png_ptr->mode & PNG_AFTER_IDAT)
-          png_ptr->mode |= PNG_HAVE_CHUNK_AFTER_IDAT;
-
-      if (!png_memcmp(png_ptr->chunk_name, png_IHDR, 4))
-         png_handle_IHDR(png_ptr, info_ptr, length);
-      else if (!png_memcmp(png_ptr->chunk_name, png_IEND, 4))
-         png_handle_IEND(png_ptr, info_ptr, length);
-#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
-      else if (png_handle_as_unknown(png_ptr, png_ptr->chunk_name))
-      {
-         if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
-            png_ptr->mode |= PNG_HAVE_IDAT;
-         png_handle_unknown(png_ptr, info_ptr, length);
-         if (!png_memcmp(png_ptr->chunk_name, png_PLTE, 4))
-            png_ptr->mode |= PNG_HAVE_PLTE;
-         else if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
-         {
-            if (!(png_ptr->mode & PNG_HAVE_IHDR))
-               png_error(png_ptr, "Missing IHDR before IDAT");
-            else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
-                     !(png_ptr->mode & PNG_HAVE_PLTE))
-               png_error(png_ptr, "Missing PLTE before IDAT");
-            break;
-         }
-      }
-#endif
-      else if (!png_memcmp(png_ptr->chunk_name, png_PLTE, 4))
-         png_handle_PLTE(png_ptr, info_ptr, length);
-      else if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
-      {
-         if (!(png_ptr->mode & PNG_HAVE_IHDR))
-            png_error(png_ptr, "Missing IHDR before IDAT");
-         else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
-                  !(png_ptr->mode & PNG_HAVE_PLTE))
-            png_error(png_ptr, "Missing PLTE before IDAT");
-
-         png_ptr->idat_size = length;
-         png_ptr->mode |= PNG_HAVE_IDAT;
-         break;
-      }
-#if defined(PNG_READ_bKGD_SUPPORTED)
-      else if (!png_memcmp(png_ptr->chunk_name, png_bKGD, 4))
-         png_handle_bKGD(png_ptr, info_ptr, length);
-#endif
-#if defined(PNG_READ_cHRM_SUPPORTED)
-      else if (!png_memcmp(png_ptr->chunk_name, png_cHRM, 4))
-         png_handle_cHRM(png_ptr, info_ptr, length);
-#endif
-#if defined(PNG_READ_gAMA_SUPPORTED)
-      else if (!png_memcmp(png_ptr->chunk_name, png_gAMA, 4))
-         png_handle_gAMA(png_ptr, info_ptr, length);
-#endif
-#if defined(PNG_READ_hIST_SUPPORTED)
-      else if (!png_memcmp(png_ptr->chunk_name, png_hIST, 4))
-         png_handle_hIST(png_ptr, info_ptr, length);
-#endif
-#if defined(PNG_READ_oFFs_SUPPORTED)
-      else if (!png_memcmp(png_ptr->chunk_name, png_oFFs, 4))
-         png_handle_oFFs(png_ptr, info_ptr, length);
-#endif
-#if defined(PNG_READ_pCAL_SUPPORTED)
-      else if (!png_memcmp(png_ptr->chunk_name, png_pCAL, 4))
-         png_handle_pCAL(png_ptr, info_ptr, length);
-#endif
-#if defined(PNG_READ_sCAL_SUPPORTED)
-      else if (!png_memcmp(png_ptr->chunk_name, png_sCAL, 4))
-         png_handle_sCAL(png_ptr, info_ptr, length);
-#endif
-#if defined(PNG_READ_pHYs_SUPPORTED)
-      else if (!png_memcmp(png_ptr->chunk_name, png_pHYs, 4))
-         png_handle_pHYs(png_ptr, info_ptr, length);
-#endif
-#if defined(PNG_READ_sBIT_SUPPORTED)
-      else if (!png_memcmp(png_ptr->chunk_name, png_sBIT, 4))
-         png_handle_sBIT(png_ptr, info_ptr, length);
-#endif
-#if defined(PNG_READ_sRGB_SUPPORTED)
-      else if (!png_memcmp(png_ptr->chunk_name, png_sRGB, 4))
-         png_handle_sRGB(png_ptr, info_ptr, length);
-#endif
-#if defined(PNG_READ_iCCP_SUPPORTED)
-      else if (!png_memcmp(png_ptr->chunk_name, png_iCCP, 4))
-         png_handle_iCCP(png_ptr, info_ptr, length);
-#endif
-#if defined(PNG_READ_sPLT_SUPPORTED)
-      else if (!png_memcmp(png_ptr->chunk_name, png_sPLT, 4))
-         png_handle_sPLT(png_ptr, info_ptr, length);
-#endif
-#if defined(PNG_READ_tEXt_SUPPORTED)
-      else if (!png_memcmp(png_ptr->chunk_name, png_tEXt, 4))
-         png_handle_tEXt(png_ptr, info_ptr, length);
-#endif
-#if defined(PNG_READ_tIME_SUPPORTED)
-      else if (!png_memcmp(png_ptr->chunk_name, png_tIME, 4))
-         png_handle_tIME(png_ptr, info_ptr, length);
-#endif
-#if defined(PNG_READ_tRNS_SUPPORTED)
-      else if (!png_memcmp(png_ptr->chunk_name, png_tRNS, 4))
-         png_handle_tRNS(png_ptr, info_ptr, length);
-#endif
-#if defined(PNG_READ_zTXt_SUPPORTED)
-      else if (!png_memcmp(png_ptr->chunk_name, png_zTXt, 4))
-         png_handle_zTXt(png_ptr, info_ptr, length);
-#endif
-#if defined(PNG_READ_iTXt_SUPPORTED)
-      else if (!png_memcmp(png_ptr->chunk_name, png_iTXt, 4))
-         png_handle_iTXt(png_ptr, info_ptr, length);
-#endif
-      else
-         png_handle_unknown(png_ptr, info_ptr, length);
-   }
-}
-#endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
-
-/* optional call to update the users info_ptr structure */
-void PNGAPI
-png_read_update_info(png_structp png_ptr, png_infop info_ptr)
-{
-   png_debug(1, "in png_read_update_info\n");
-   if(png_ptr == NULL) return;
-   if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
-      png_read_start_row(png_ptr);
-   else
-      png_warning(png_ptr,
-      "Ignoring extra png_read_update_info() call; row buffer not reallocated");
-   png_read_transform_info(png_ptr, info_ptr);
-}
-
-#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
-/* Initialize palette, background, etc, after transformations
- * are set, but before any reading takes place.  This allows
- * the user to obtain a gamma-corrected palette, for example.
- * If the user doesn't call this, we will do it ourselves.
- */
-void PNGAPI
-png_start_read_image(png_structp png_ptr)
-{
-   png_debug(1, "in png_start_read_image\n");
-   if(png_ptr == NULL) return;
-   if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
-      png_read_start_row(png_ptr);
-}
-#endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
-
-#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
-void PNGAPI
-png_read_row(png_structp png_ptr, png_bytep row, png_bytep dsp_row)
-{
-#ifdef PNG_USE_LOCAL_ARRAYS
-   PNG_CONST PNG_IDAT;
-   PNG_CONST int png_pass_dsp_mask[7] = {0xff, 0x0f, 0xff, 0x33, 0xff, 0x55,
-     0xff};
-   PNG_CONST int png_pass_mask[7] = {0x80, 0x08, 0x88, 0x22, 0xaa, 0x55, 0xff};
-#endif
-   int ret;
-   if(png_ptr == NULL) return;
-   png_debug2(1, "in png_read_row (row %lu, pass %d)\n",
-      png_ptr->row_number, png_ptr->pass);
-   if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
-      png_read_start_row(png_ptr);
-   if (png_ptr->row_number == 0 && png_ptr->pass == 0)
-   {
-   /* check for transforms that have been set but were defined out */
-#if defined(PNG_WRITE_INVERT_SUPPORTED) && !defined(PNG_READ_INVERT_SUPPORTED)
-   if (png_ptr->transformations & PNG_INVERT_MONO)
-      png_warning(png_ptr, "PNG_READ_INVERT_SUPPORTED is not defined.");
-#endif
-#if defined(PNG_WRITE_FILLER_SUPPORTED) && !defined(PNG_READ_FILLER_SUPPORTED)
-   if (png_ptr->transformations & PNG_FILLER)
-      png_warning(png_ptr, "PNG_READ_FILLER_SUPPORTED is not defined.");
-#endif
-#if defined(PNG_WRITE_PACKSWAP_SUPPORTED) && !defined(PNG_READ_PACKSWAP_SUPPORTED)
-   if (png_ptr->transformations & PNG_PACKSWAP)
-      png_warning(png_ptr, "PNG_READ_PACKSWAP_SUPPORTED is not defined.");
-#endif
-#if defined(PNG_WRITE_PACK_SUPPORTED) && !defined(PNG_READ_PACK_SUPPORTED)
-   if (png_ptr->transformations & PNG_PACK)
-      png_warning(png_ptr, "PNG_READ_PACK_SUPPORTED is not defined.");
-#endif
-#if defined(PNG_WRITE_SHIFT_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED)
-   if (png_ptr->transformations & PNG_SHIFT)
-      png_warning(png_ptr, "PNG_READ_SHIFT_SUPPORTED is not defined.");
-#endif
-#if defined(PNG_WRITE_BGR_SUPPORTED) && !defined(PNG_READ_BGR_SUPPORTED)
-   if (png_ptr->transformations & PNG_BGR)
-      png_warning(png_ptr, "PNG_READ_BGR_SUPPORTED is not defined.");
-#endif
-#if defined(PNG_WRITE_SWAP_SUPPORTED) && !defined(PNG_READ_SWAP_SUPPORTED)
-   if (png_ptr->transformations & PNG_SWAP_BYTES)
-      png_warning(png_ptr, "PNG_READ_SWAP_SUPPORTED is not defined.");
-#endif
-   }
-
-#if defined(PNG_READ_INTERLACING_SUPPORTED)
-   /* if interlaced and we do not need a new row, combine row and return */
-   if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
-   {
-      switch (png_ptr->pass)
-      {
-         case 0:
-            if (png_ptr->row_number & 0x07)
-            {
-               if (dsp_row != NULL)
-                  png_combine_row(png_ptr, dsp_row,
-                     png_pass_dsp_mask[png_ptr->pass]);
-               png_read_finish_row(png_ptr);
-               return;
-            }
-            break;
-         case 1:
-            if ((png_ptr->row_number & 0x07) || png_ptr->width < 5)
-            {
-               if (dsp_row != NULL)
-                  png_combine_row(png_ptr, dsp_row,
-                     png_pass_dsp_mask[png_ptr->pass]);
-               png_read_finish_row(png_ptr);
-               return;
-            }
-            break;
-         case 2:
-            if ((png_ptr->row_number & 0x07) != 4)
-            {
-               if (dsp_row != NULL && (png_ptr->row_number & 4))
-                  png_combine_row(png_ptr, dsp_row,
-                     png_pass_dsp_mask[png_ptr->pass]);
-               png_read_finish_row(png_ptr);
-               return;
-            }
-            break;
-         case 3:
-            if ((png_ptr->row_number & 3) || png_ptr->width < 3)
-            {
-               if (dsp_row != NULL)
-                  png_combine_row(png_ptr, dsp_row,
-                     png_pass_dsp_mask[png_ptr->pass]);
-               png_read_finish_row(png_ptr);
-               return;
-            }
-            break;
-         case 4:
-            if ((png_ptr->row_number & 3) != 2)
-            {
-               if (dsp_row != NULL && (png_ptr->row_number & 2))
-                  png_combine_row(png_ptr, dsp_row,
-                     png_pass_dsp_mask[png_ptr->pass]);
-               png_read_finish_row(png_ptr);
-               return;
-            }
-            break;
-         case 5:
-            if ((png_ptr->row_number & 1) || png_ptr->width < 2)
-            {
-               if (dsp_row != NULL)
-                  png_combine_row(png_ptr, dsp_row,
-                     png_pass_dsp_mask[png_ptr->pass]);
-               png_read_finish_row(png_ptr);
-               return;
-            }
-            break;
-         case 6:
-            if (!(png_ptr->row_number & 1))
-            {
-               png_read_finish_row(png_ptr);
-               return;
-            }
-            break;
-      }
-   }
-#endif
-
-   if (!(png_ptr->mode & PNG_HAVE_IDAT))
-      png_error(png_ptr, "Invalid attempt to read row data");
-
-   png_ptr->zstream.next_out = png_ptr->row_buf;
-   png_ptr->zstream.avail_out = (uInt)png_ptr->irowbytes;
-   do
-   {
-      if (!(png_ptr->zstream.avail_in))
-      {
-         while (!png_ptr->idat_size)
-         {
-            png_byte chunk_length[4];
-
-            png_crc_finish(png_ptr, 0);
-
-            png_read_data(png_ptr, chunk_length, 4);
-            png_ptr->idat_size = png_get_uint_31(png_ptr,chunk_length);
-
-            png_reset_crc(png_ptr);
-            png_crc_read(png_ptr, png_ptr->chunk_name, 4);
-            if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
-               png_error(png_ptr, "Not enough image data");
-         }
-         png_ptr->zstream.avail_in = (uInt)png_ptr->zbuf_size;
-         png_ptr->zstream.next_in = png_ptr->zbuf;
-         if (png_ptr->zbuf_size > png_ptr->idat_size)
-            png_ptr->zstream.avail_in = (uInt)png_ptr->idat_size;
-         png_crc_read(png_ptr, png_ptr->zbuf,
-            (png_size_t)png_ptr->zstream.avail_in);
-         png_ptr->idat_size -= png_ptr->zstream.avail_in;
-      }
-      ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);
-      if (ret == Z_STREAM_END)
-      {
-         if (png_ptr->zstream.avail_out || png_ptr->zstream.avail_in ||
-            png_ptr->idat_size)
-            png_error(png_ptr, "Extra compressed data");
-         png_ptr->mode |= PNG_AFTER_IDAT;
-         png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
-         break;
-      }
-      if (ret != Z_OK)
-         png_error(png_ptr, png_ptr->zstream.msg ? png_ptr->zstream.msg :
-                   "Decompression error");
-
-   } while (png_ptr->zstream.avail_out);
-
-   png_ptr->row_info.color_type = png_ptr->color_type;
-   png_ptr->row_info.width = png_ptr->iwidth;
-   png_ptr->row_info.channels = png_ptr->channels;
-   png_ptr->row_info.bit_depth = png_ptr->bit_depth;
-   png_ptr->row_info.pixel_depth = png_ptr->pixel_depth;
-   png_ptr->row_info.rowbytes = PNG_ROWBYTES(png_ptr->row_info.pixel_depth,
-       png_ptr->row_info.width);
-
-   if(png_ptr->row_buf[0])
-   png_read_filter_row(png_ptr, &(png_ptr->row_info),
-      png_ptr->row_buf + 1, png_ptr->prev_row + 1,
-      (int)(png_ptr->row_buf[0]));
-
-   png_memcpy_check(png_ptr, png_ptr->prev_row, png_ptr->row_buf,
-      png_ptr->rowbytes + 1);
-
-#if defined(PNG_MNG_FEATURES_SUPPORTED)
-   if((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
-      (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING))
-   {
-      /* Intrapixel differencing */
-      png_do_read_intrapixel(&(png_ptr->row_info), png_ptr->row_buf + 1);
-   }
-#endif
-
-
-   if (png_ptr->transformations || (png_ptr->flags&PNG_FLAG_STRIP_ALPHA))
-      png_do_read_transformations(png_ptr);
-
-#if defined(PNG_READ_INTERLACING_SUPPORTED)
-   /* blow up interlaced rows to full size */
-   if (png_ptr->interlaced &&
-      (png_ptr->transformations & PNG_INTERLACE))
-   {
-      if (png_ptr->pass < 6)
-/*       old interface (pre-1.0.9):
-         png_do_read_interlace(&(png_ptr->row_info),
-            png_ptr->row_buf + 1, png_ptr->pass, png_ptr->transformations);
- */
-         png_do_read_interlace(png_ptr);
-
-      if (dsp_row != NULL)
-         png_combine_row(png_ptr, dsp_row,
-            png_pass_dsp_mask[png_ptr->pass]);
-      if (row != NULL)
-         png_combine_row(png_ptr, row,
-            png_pass_mask[png_ptr->pass]);
-   }
-   else
-#endif
-   {
-      if (row != NULL)
-         png_combine_row(png_ptr, row, 0xff);
-      if (dsp_row != NULL)
-         png_combine_row(png_ptr, dsp_row, 0xff);
-   }
-   png_read_finish_row(png_ptr);
-
-   if (png_ptr->read_row_fn != NULL)
-      (*(png_ptr->read_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
-}
-#endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
-
-#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
-/* Read one or more rows of image data.  If the image is interlaced,
- * and png_set_interlace_handling() has been called, the rows need to
- * contain the contents of the rows from the previous pass.  If the
- * image has alpha or transparency, and png_handle_alpha()[*] has been
- * called, the rows contents must be initialized to the contents of the
- * screen.
- *
- * "row" holds the actual image, and pixels are placed in it
- * as they arrive.  If the image is displayed after each pass, it will
- * appear to "sparkle" in.  "display_row" can be used to display a
- * "chunky" progressive image, with finer detail added as it becomes
- * available.  If you do not want this "chunky" display, you may pass
- * NULL for display_row.  If you do not want the sparkle display, and
- * you have not called png_handle_alpha(), you may pass NULL for rows.
- * If you have called png_handle_alpha(), and the image has either an
- * alpha channel or a transparency chunk, you must provide a buffer for
- * rows.  In this case, you do not have to provide a display_row buffer
- * also, but you may.  If the image is not interlaced, or if you have
- * not called png_set_interlace_handling(), the display_row buffer will
- * be ignored, so pass NULL to it.
- *
- * [*] png_handle_alpha() does not exist yet, as of this version of libpng
- */
-
-void PNGAPI
-png_read_rows(png_structp png_ptr, png_bytepp row,
-   png_bytepp display_row, png_uint_32 num_rows)
-{
-   png_uint_32 i;
-   png_bytepp rp;
-   png_bytepp dp;
-
-   png_debug(1, "in png_read_rows\n");
-   if(png_ptr == NULL) return;
-   rp = row;
-   dp = display_row;
-   if (rp != NULL && dp != NULL)
-      for (i = 0; i < num_rows; i++)
-      {
-         png_bytep rptr = *rp++;
-         png_bytep dptr = *dp++;
-
-         png_read_row(png_ptr, rptr, dptr);
-      }
-   else if(rp != NULL)
-      for (i = 0; i < num_rows; i++)
-      {
-         png_bytep rptr = *rp;
-         png_read_row(png_ptr, rptr, png_bytep_NULL);
-         rp++;
-      }
-   else if(dp != NULL)
-      for (i = 0; i < num_rows; i++)
-      {
-         png_bytep dptr = *dp;
-         png_read_row(png_ptr, png_bytep_NULL, dptr);
-         dp++;
-      }
-}
-#endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
-
-#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
-/* Read the entire image.  If the image has an alpha channel or a tRNS
- * chunk, and you have called png_handle_alpha()[*], you will need to
- * initialize the image to the current image that PNG will be overlaying.
- * We set the num_rows again here, in case it was incorrectly set in
- * png_read_start_row() by a call to png_read_update_info() or
- * png_start_read_image() if png_set_interlace_handling() wasn't called
- * prior to either of these functions like it should have been.  You can
- * only call this function once.  If you desire to have an image for
- * each pass of a interlaced image, use png_read_rows() instead.
- *
- * [*] png_handle_alpha() does not exist yet, as of this version of libpng
- */
-void PNGAPI
-png_read_image(png_structp png_ptr, png_bytepp image)
-{
-   png_uint_32 i,image_height;
-   int pass, j;
-   png_bytepp rp;
-
-   png_debug(1, "in png_read_image\n");
-   if(png_ptr == NULL) return;
-
-#ifdef PNG_READ_INTERLACING_SUPPORTED
-   pass = png_set_interlace_handling(png_ptr);
-#else
-   if (png_ptr->interlaced)
-      png_error(png_ptr,
-        "Cannot read interlaced image -- interlace handler disabled.");
-   pass = 1;
-#endif
-
-
-   image_height=png_ptr->height;
-   png_ptr->num_rows = image_height; /* Make sure this is set correctly */
-
-   for (j = 0; j < pass; j++)
-   {
-      rp = image;
-      for (i = 0; i < image_height; i++)
-      {
-         png_read_row(png_ptr, *rp, png_bytep_NULL);
-         rp++;
-      }
-   }
-}
-#endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
-
-#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
-/* Read the end of the PNG file.  Will not read past the end of the
- * file, will verify the end is accurate, and will read any comments
- * or time information at the end of the file, if info is not NULL.
- */
-void PNGAPI
-png_read_end(png_structp png_ptr, png_infop info_ptr)
-{
-   png_byte chunk_length[4];
-   png_uint_32 length;
-
-   png_debug(1, "in png_read_end\n");
-   if(png_ptr == NULL) return;
-   png_crc_finish(png_ptr, 0); /* Finish off CRC from last IDAT chunk */
-
-   do
-   {
-#ifdef PNG_USE_LOCAL_ARRAYS
-      PNG_CONST PNG_IHDR;
-      PNG_CONST PNG_IDAT;
-      PNG_CONST PNG_IEND;
-      PNG_CONST PNG_PLTE;
-#if defined(PNG_READ_bKGD_SUPPORTED)
-      PNG_CONST PNG_bKGD;
-#endif
-#if defined(PNG_READ_cHRM_SUPPORTED)
-      PNG_CONST PNG_cHRM;
-#endif
-#if defined(PNG_READ_gAMA_SUPPORTED)
-      PNG_CONST PNG_gAMA;
-#endif
-#if defined(PNG_READ_hIST_SUPPORTED)
-      PNG_CONST PNG_hIST;
-#endif
-#if defined(PNG_READ_iCCP_SUPPORTED)
-      PNG_CONST PNG_iCCP;
-#endif
-#if defined(PNG_READ_iTXt_SUPPORTED)
-      PNG_CONST PNG_iTXt;
-#endif
-#if defined(PNG_READ_oFFs_SUPPORTED)
-      PNG_CONST PNG_oFFs;
-#endif
-#if defined(PNG_READ_pCAL_SUPPORTED)
-      PNG_CONST PNG_pCAL;
-#endif
-#if defined(PNG_READ_pHYs_SUPPORTED)
-      PNG_CONST PNG_pHYs;
-#endif
-#if defined(PNG_READ_sBIT_SUPPORTED)
-      PNG_CONST PNG_sBIT;
-#endif
-#if defined(PNG_READ_sCAL_SUPPORTED)
-      PNG_CONST PNG_sCAL;
-#endif
-#if defined(PNG_READ_sPLT_SUPPORTED)
-      PNG_CONST PNG_sPLT;
-#endif
-#if defined(PNG_READ_sRGB_SUPPORTED)
-      PNG_CONST PNG_sRGB;
-#endif
-#if defined(PNG_READ_tEXt_SUPPORTED)
-      PNG_CONST PNG_tEXt;
-#endif
-#if defined(PNG_READ_tIME_SUPPORTED)
-      PNG_CONST PNG_tIME;
-#endif
-#if defined(PNG_READ_tRNS_SUPPORTED)
-      PNG_CONST PNG_tRNS;
-#endif
-#if defined(PNG_READ_zTXt_SUPPORTED)
-      PNG_CONST PNG_zTXt;
-#endif
-#endif /* PNG_USE_LOCAL_ARRAYS */
-
-      png_read_data(png_ptr, chunk_length, 4);
-      length = png_get_uint_31(png_ptr,chunk_length);
-
-      png_reset_crc(png_ptr);
-      png_crc_read(png_ptr, png_ptr->chunk_name, 4);
-
-      png_debug1(0, "Reading %s chunk.\n", png_ptr->chunk_name);
-
-      if (!png_memcmp(png_ptr->chunk_name, png_IHDR, 4))
-         png_handle_IHDR(png_ptr, info_ptr, length);
-      else if (!png_memcmp(png_ptr->chunk_name, png_IEND, 4))
-         png_handle_IEND(png_ptr, info_ptr, length);
-#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
-      else if (png_handle_as_unknown(png_ptr, png_ptr->chunk_name))
-      {
-         if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
-         {
-            if ((length > 0) || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT))
-               png_error(png_ptr, "Too many IDAT's found");
-         }
-         png_handle_unknown(png_ptr, info_ptr, length);
-         if (!png_memcmp(png_ptr->chunk_name, png_PLTE, 4))
-            png_ptr->mode |= PNG_HAVE_PLTE;
-      }
-#endif
-      else if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
-      {
-         /* Zero length IDATs are legal after the last IDAT has been
-          * read, but not after other chunks have been read.
-          */
-         if ((length > 0) || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT))
-            png_error(png_ptr, "Too many IDAT's found");
-         png_crc_finish(png_ptr, length);
-      }
-      else if (!png_memcmp(png_ptr->chunk_name, png_PLTE, 4))
-         png_handle_PLTE(png_ptr, info_ptr, length);
-#if defined(PNG_READ_bKGD_SUPPORTED)
-      else if (!png_memcmp(png_ptr->chunk_name, png_bKGD, 4))
-         png_handle_bKGD(png_ptr, info_ptr, length);
-#endif
-#if defined(PNG_READ_cHRM_SUPPORTED)
-      else if (!png_memcmp(png_ptr->chunk_name, png_cHRM, 4))
-         png_handle_cHRM(png_ptr, info_ptr, length);
-#endif
-#if defined(PNG_READ_gAMA_SUPPORTED)
-      else if (!png_memcmp(png_ptr->chunk_name, png_gAMA, 4))
-         png_handle_gAMA(png_ptr, info_ptr, length);
-#endif
-#if defined(PNG_READ_hIST_SUPPORTED)
-      else if (!png_memcmp(png_ptr->chunk_name, png_hIST, 4))
-         png_handle_hIST(png_ptr, info_ptr, length);
-#endif
-#if defined(PNG_READ_oFFs_SUPPORTED)
-      else if (!png_memcmp(png_ptr->chunk_name, png_oFFs, 4))
-         png_handle_oFFs(png_ptr, info_ptr, length);
-#endif
-#if defined(PNG_READ_pCAL_SUPPORTED)
-      else if (!png_memcmp(png_ptr->chunk_name, png_pCAL, 4))
-         png_handle_pCAL(png_ptr, info_ptr, length);
-#endif
-#if defined(PNG_READ_sCAL_SUPPORTED)
-      else if (!png_memcmp(png_ptr->chunk_name, png_sCAL, 4))
-         png_handle_sCAL(png_ptr, info_ptr, length);
-#endif
-#if defined(PNG_READ_pHYs_SUPPORTED)
-      else if (!png_memcmp(png_ptr->chunk_name, png_pHYs, 4))
-         png_handle_pHYs(png_ptr, info_ptr, length);
-#endif
-#if defined(PNG_READ_sBIT_SUPPORTED)
-      else if (!png_memcmp(png_ptr->chunk_name, png_sBIT, 4))
-         png_handle_sBIT(png_ptr, info_ptr, length);
-#endif
-#if defined(PNG_READ_sRGB_SUPPORTED)
-      else if (!png_memcmp(png_ptr->chunk_name, png_sRGB, 4))
-         png_handle_sRGB(png_ptr, info_ptr, length);
-#endif
-#if defined(PNG_READ_iCCP_SUPPORTED)
-      else if (!png_memcmp(png_ptr->chunk_name, png_iCCP, 4))
-         png_handle_iCCP(png_ptr, info_ptr, length);
-#endif
-#if defined(PNG_READ_sPLT_SUPPORTED)
-      else if (!png_memcmp(png_ptr->chunk_name, png_sPLT, 4))
-         png_handle_sPLT(png_ptr, info_ptr, length);
-#endif
-#if defined(PNG_READ_tEXt_SUPPORTED)
-      else if (!png_memcmp(png_ptr->chunk_name, png_tEXt, 4))
-         png_handle_tEXt(png_ptr, info_ptr, length);
-#endif
-#if defined(PNG_READ_tIME_SUPPORTED)
-      else if (!png_memcmp(png_ptr->chunk_name, png_tIME, 4))
-         png_handle_tIME(png_ptr, info_ptr, length);
-#endif
-#if defined(PNG_READ_tRNS_SUPPORTED)
-      else if (!png_memcmp(png_ptr->chunk_name, png_tRNS, 4))
-         png_handle_tRNS(png_ptr, info_ptr, length);
-#endif
-#if defined(PNG_READ_zTXt_SUPPORTED)
-      else if (!png_memcmp(png_ptr->chunk_name, png_zTXt, 4))
-         png_handle_zTXt(png_ptr, info_ptr, length);
-#endif
-#if defined(PNG_READ_iTXt_SUPPORTED)
-      else if (!png_memcmp(png_ptr->chunk_name, png_iTXt, 4))
-         png_handle_iTXt(png_ptr, info_ptr, length);
-#endif
-      else
-         png_handle_unknown(png_ptr, info_ptr, length);
-   } while (!(png_ptr->mode & PNG_HAVE_IEND));
-}
-#endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
-
-/* free all memory used by the read */
-void PNGAPI
-png_destroy_read_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr,
-   png_infopp end_info_ptr_ptr)
-{
-   png_structp png_ptr = NULL;
-   png_infop info_ptr = NULL, end_info_ptr = NULL;
-#ifdef PNG_USER_MEM_SUPPORTED
-   png_free_ptr free_fn = NULL;
-   png_voidp mem_ptr = NULL;
-#endif
-
-   png_debug(1, "in png_destroy_read_struct\n");
-   if (png_ptr_ptr != NULL)
-   {
-      png_ptr = *png_ptr_ptr;
-   }
-   if (png_ptr == NULL)
-      return;
-
-#ifdef PNG_USER_MEM_SUPPORTED
-   free_fn = png_ptr->free_fn;
-   mem_ptr = png_ptr->mem_ptr;
-#endif
-
-   if (info_ptr_ptr != NULL)
-      info_ptr = *info_ptr_ptr;
-
-   if (end_info_ptr_ptr != NULL)
-      end_info_ptr = *end_info_ptr_ptr;
-
-   png_read_destroy(png_ptr, info_ptr, end_info_ptr);
-
-   if (info_ptr != NULL)
-   {
-#if defined(PNG_TEXT_SUPPORTED)
-      png_free_data(png_ptr, info_ptr, PNG_FREE_TEXT, -1);
-#endif
-
-#ifdef PNG_USER_MEM_SUPPORTED
-      png_destroy_struct_2((png_voidp)info_ptr, (png_free_ptr)free_fn,
-          (png_voidp)mem_ptr);
-#else
-      png_destroy_struct((png_voidp)info_ptr);
-#endif
-      *info_ptr_ptr = NULL;
-   }
-
-   if (end_info_ptr != NULL)
-   {
-#if defined(PNG_READ_TEXT_SUPPORTED)
-      png_free_data(png_ptr, end_info_ptr, PNG_FREE_TEXT, -1);
-#endif
-#ifdef PNG_USER_MEM_SUPPORTED
-      png_destroy_struct_2((png_voidp)end_info_ptr, (png_free_ptr)free_fn,
-         (png_voidp)mem_ptr);
-#else
-      png_destroy_struct((png_voidp)end_info_ptr);
-#endif
-      *end_info_ptr_ptr = NULL;
-   }
-
-#ifdef PNG_USER_MEM_SUPPORTED
-   png_destroy_struct_2((png_voidp)png_ptr, (png_free_ptr)free_fn,
-       (png_voidp)mem_ptr);
-#else
-   png_destroy_struct((png_voidp)png_ptr);
-#endif
-   *png_ptr_ptr = NULL;
-}
-
-/* free all memory used by the read (old method) */
-void /* PRIVATE */
-png_read_destroy(png_structp png_ptr, png_infop info_ptr, png_infop end_info_ptr)
-{
-#ifdef PNG_SETJMP_SUPPORTED
-   jmp_buf tmp_jmp;
-#endif
-   png_error_ptr error_fn;
-   png_error_ptr warning_fn;
-   png_voidp error_ptr;
-#ifdef PNG_USER_MEM_SUPPORTED
-   png_free_ptr free_fn;
-#endif
-
-   png_debug(1, "in png_read_destroy\n");
-   if (info_ptr != NULL)
-      png_info_destroy(png_ptr, info_ptr);
-
-   if (end_info_ptr != NULL)
-      png_info_destroy(png_ptr, end_info_ptr);
-
-   png_free(png_ptr, png_ptr->zbuf);
-   png_free(png_ptr, png_ptr->big_row_buf);
-   png_free(png_ptr, png_ptr->prev_row);
-#if defined(PNG_READ_DITHER_SUPPORTED)
-   png_free(png_ptr, png_ptr->palette_lookup);
-   png_free(png_ptr, png_ptr->dither_index);
-#endif
-#if defined(PNG_READ_GAMMA_SUPPORTED)
-   png_free(png_ptr, png_ptr->gamma_table);
-#endif
-#if defined(PNG_READ_BACKGROUND_SUPPORTED)
-   png_free(png_ptr, png_ptr->gamma_from_1);
-   png_free(png_ptr, png_ptr->gamma_to_1);
-#endif
-#ifdef PNG_FREE_ME_SUPPORTED
-   if (png_ptr->free_me & PNG_FREE_PLTE)
-      png_zfree(png_ptr, png_ptr->palette);
-   png_ptr->free_me &= ~PNG_FREE_PLTE;
-#else
-   if (png_ptr->flags & PNG_FLAG_FREE_PLTE)
-      png_zfree(png_ptr, png_ptr->palette);
-   png_ptr->flags &= ~PNG_FLAG_FREE_PLTE;
-#endif
-#if defined(PNG_tRNS_SUPPORTED) || \
-    defined(PNG_READ_EXPAND_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
-#ifdef PNG_FREE_ME_SUPPORTED
-   if (png_ptr->free_me & PNG_FREE_TRNS)
-      png_free(png_ptr, png_ptr->trans);
-   png_ptr->free_me &= ~PNG_FREE_TRNS;
-#else
-   if (png_ptr->flags & PNG_FLAG_FREE_TRNS)
-      png_free(png_ptr, png_ptr->trans);
-   png_ptr->flags &= ~PNG_FLAG_FREE_TRNS;
-#endif
-#endif
-#if defined(PNG_READ_hIST_SUPPORTED)
-#ifdef PNG_FREE_ME_SUPPORTED
-   if (png_ptr->free_me & PNG_FREE_HIST)
-      png_free(png_ptr, png_ptr->hist);
-   png_ptr->free_me &= ~PNG_FREE_HIST;
-#else
-   if (png_ptr->flags & PNG_FLAG_FREE_HIST)
-      png_free(png_ptr, png_ptr->hist);
-   png_ptr->flags &= ~PNG_FLAG_FREE_HIST;
-#endif
-#endif
-#if defined(PNG_READ_GAMMA_SUPPORTED)
-   if (png_ptr->gamma_16_table != NULL)
-   {
-      int i;
-      int istop = (1 << (8 - png_ptr->gamma_shift));
-      for (i = 0; i < istop; i++)
-      {
-         png_free(png_ptr, png_ptr->gamma_16_table[i]);
-      }
-   png_free(png_ptr, png_ptr->gamma_16_table);
-   }
-#if defined(PNG_READ_BACKGROUND_SUPPORTED)
-   if (png_ptr->gamma_16_from_1 != NULL)
-   {
-      int i;
-      int istop = (1 << (8 - png_ptr->gamma_shift));
-      for (i = 0; i < istop; i++)
-      {
-         png_free(png_ptr, png_ptr->gamma_16_from_1[i]);
-      }
-   png_free(png_ptr, png_ptr->gamma_16_from_1);
-   }
-   if (png_ptr->gamma_16_to_1 != NULL)
-   {
-      int i;
-      int istop = (1 << (8 - png_ptr->gamma_shift));
-      for (i = 0; i < istop; i++)
-      {
-         png_free(png_ptr, png_ptr->gamma_16_to_1[i]);
-      }
-   png_free(png_ptr, png_ptr->gamma_16_to_1);
-   }
-#endif
-#endif
-#if defined(PNG_TIME_RFC1123_SUPPORTED)
-   png_free(png_ptr, png_ptr->time_buffer);
-#endif
-
-   inflateEnd(&png_ptr->zstream);
-#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
-   png_free(png_ptr, png_ptr->save_buffer);
-#endif
-
-#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
-#ifdef PNG_TEXT_SUPPORTED
-   png_free(png_ptr, png_ptr->current_text);
-#endif /* PNG_TEXT_SUPPORTED */
-#endif /* PNG_PROGRESSIVE_READ_SUPPORTED */
-
-   /* Save the important info out of the png_struct, in case it is
-    * being used again.
-    */
-#ifdef PNG_SETJMP_SUPPORTED
-   png_memcpy(tmp_jmp, png_ptr->jmpbuf, png_sizeof (jmp_buf));
-#endif
-
-   error_fn = png_ptr->error_fn;
-   warning_fn = png_ptr->warning_fn;
-   error_ptr = png_ptr->error_ptr;
-#ifdef PNG_USER_MEM_SUPPORTED
-   free_fn = png_ptr->free_fn;
-#endif
-
-   png_memset(png_ptr, 0, png_sizeof (png_struct));
-
-   png_ptr->error_fn = error_fn;
-   png_ptr->warning_fn = warning_fn;
-   png_ptr->error_ptr = error_ptr;
-#ifdef PNG_USER_MEM_SUPPORTED
-   png_ptr->free_fn = free_fn;
-#endif
-
-#ifdef PNG_SETJMP_SUPPORTED
-   png_memcpy(png_ptr->jmpbuf, tmp_jmp, png_sizeof (jmp_buf));
-#endif
-
-}
-
-void PNGAPI
-png_set_read_status_fn(png_structp png_ptr, png_read_status_ptr read_row_fn)
-{
-   if(png_ptr == NULL) return;
-   png_ptr->read_row_fn = read_row_fn;
-}
-
-
-#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
-#if defined(PNG_INFO_IMAGE_SUPPORTED)
-void PNGAPI
-png_read_png(png_structp png_ptr, png_infop info_ptr,
-                           int transforms,
-                           voidp params)
-{
-   int row;
-
-   if(png_ptr == NULL) return;
-#if defined(PNG_READ_INVERT_ALPHA_SUPPORTED)
-   /* invert the alpha channel from opacity to transparency
-    */
-   if (transforms & PNG_TRANSFORM_INVERT_ALPHA)
-       png_set_invert_alpha(png_ptr);
-#endif
-
-   /* png_read_info() gives us all of the information from the
-    * PNG file before the first IDAT (image data chunk).
-    */
-   png_read_info(png_ptr, info_ptr);
-   if (info_ptr->height > PNG_UINT_32_MAX/png_sizeof(png_bytep))
-      png_error(png_ptr,"Image is too high to process with png_read_png()");
-
-   /* -------------- image transformations start here ------------------- */
-
-#if defined(PNG_READ_16_TO_8_SUPPORTED)
-   /* tell libpng to strip 16 bit/color files down to 8 bits per color
-    */
-   if (transforms & PNG_TRANSFORM_STRIP_16)
-       png_set_strip_16(png_ptr);
-#endif
-
-#if defined(PNG_READ_STRIP_ALPHA_SUPPORTED)
-   /* Strip alpha bytes from the input data without combining with
-    * the background (not recommended).
-    */
-   if (transforms & PNG_TRANSFORM_STRIP_ALPHA)
-       png_set_strip_alpha(png_ptr);
-#endif
-
-#if defined(PNG_READ_PACK_SUPPORTED) && !defined(PNG_READ_EXPAND_SUPPORTED)
-   /* Extract multiple pixels with bit depths of 1, 2, or 4 from a single
-    * byte into separate bytes (useful for paletted and grayscale images).
-    */
-   if (transforms & PNG_TRANSFORM_PACKING)
-       png_set_packing(png_ptr);
-#endif
-
-#if defined(PNG_READ_PACKSWAP_SUPPORTED)
-   /* Change the order of packed pixels to least significant bit first
-    * (not useful if you are using png_set_packing).
-    */
-   if (transforms & PNG_TRANSFORM_PACKSWAP)
-       png_set_packswap(png_ptr);
-#endif
-
-#if defined(PNG_READ_EXPAND_SUPPORTED)
-   /* Expand paletted colors into true RGB triplets
-    * Expand grayscale images to full 8 bits from 1, 2, or 4 bits/pixel
-    * Expand paletted or RGB images with transparency to full alpha
-    * channels so the data will be available as RGBA quartets.
-    */
-   if (transforms & PNG_TRANSFORM_EXPAND)
-       if ((png_ptr->bit_depth < 8) ||
-           (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) ||
-           (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)))
-         png_set_expand(png_ptr);
-#endif
-
-   /* We don't handle background color or gamma transformation or dithering.
-    */
-
-#if defined(PNG_READ_INVERT_SUPPORTED)
-   /* invert monochrome files to have 0 as white and 1 as black
-    */
-   if (transforms & PNG_TRANSFORM_INVERT_MONO)
-       png_set_invert_mono(png_ptr);
-#endif
-
-#if defined(PNG_READ_SHIFT_SUPPORTED)
-   /* If you want to shift the pixel values from the range [0,255] or
-    * [0,65535] to the original [0,7] or [0,31], or whatever range the
-    * colors were originally in:
-    */
-   if ((transforms & PNG_TRANSFORM_SHIFT)
-       && png_get_valid(png_ptr, info_ptr, PNG_INFO_sBIT))
-   {
-      png_color_8p sig_bit;
-
-      png_get_sBIT(png_ptr, info_ptr, &sig_bit);
-      png_set_shift(png_ptr, sig_bit);
-   }
-#endif
-
-#if defined(PNG_READ_BGR_SUPPORTED)
-   /* flip the RGB pixels to BGR (or RGBA to BGRA)
-    */
-   if (transforms & PNG_TRANSFORM_BGR)
-       png_set_bgr(png_ptr);
-#endif
-
-#if defined(PNG_READ_SWAP_ALPHA_SUPPORTED)
-   /* swap the RGBA or GA data to ARGB or AG (or BGRA to ABGR)
-    */
-   if (transforms & PNG_TRANSFORM_SWAP_ALPHA)
-       png_set_swap_alpha(png_ptr);
-#endif
-
-#if defined(PNG_READ_SWAP_SUPPORTED)
-   /* swap bytes of 16 bit files to least significant byte first
-    */
-   if (transforms & PNG_TRANSFORM_SWAP_ENDIAN)
-       png_set_swap(png_ptr);
-#endif
-
-   /* We don't handle adding filler bytes */
-
-   /* Optional call to gamma correct and add the background to the palette
-    * and update info structure.  REQUIRED if you are expecting libpng to
-    * update the palette for you (i.e., you selected such a transform above).
-    */
-   png_read_update_info(png_ptr, info_ptr);
-
-   /* -------------- image transformations end here ------------------- */
-
-#ifdef PNG_FREE_ME_SUPPORTED
-   png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0);
-#endif
-   if(info_ptr->row_pointers == NULL)
-   {
-      info_ptr->row_pointers = (png_bytepp)png_malloc(png_ptr,
-         info_ptr->height * png_sizeof(png_bytep));
-#ifdef PNG_FREE_ME_SUPPORTED
-      info_ptr->free_me |= PNG_FREE_ROWS;
-#endif
-      for (row = 0; row < (int)info_ptr->height; row++)
-      {
-         info_ptr->row_pointers[row] = (png_bytep)png_malloc(png_ptr,
-            png_get_rowbytes(png_ptr, info_ptr));
-      }
-   }
-
-   png_read_image(png_ptr, info_ptr->row_pointers);
-   info_ptr->valid |= PNG_INFO_IDAT;
-
-   /* read rest of file, and get additional chunks in info_ptr - REQUIRED */
-   png_read_end(png_ptr, info_ptr);
-
-   transforms = transforms; /* quiet compiler warnings */
-   params = params;
-
-}
-#endif /* PNG_INFO_IMAGE_SUPPORTED */
-#endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
-#endif /* PNG_READ_SUPPORTED */
+\r
+/* pngread.c - read a PNG file\r
+ *\r
+ * Last changed in libpng 1.4.1 [February 25, 2010]\r
+ * Copyright (c) 1998-2010 Glenn Randers-Pehrson\r
+ * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)\r
+ * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)\r
+ *\r
+ * This code is released under the libpng license.\r
+ * For conditions of distribution and use, see the disclaimer\r
+ * and license in png.h\r
+ *\r
+ * This file contains routines that an application calls directly to\r
+ * read a PNG file or stream.\r
+ */\r
+\r
+#define PNG_NO_PEDANTIC_WARNINGS\r
+#include "png.h"\r
+#ifdef PNG_READ_SUPPORTED\r
+#include "pngpriv.h"\r
+\r
+\r
+/* Create a PNG structure for reading, and allocate any memory needed. */\r
+png_structp PNGAPI\r
+png_create_read_struct(png_const_charp user_png_ver, png_voidp error_ptr,\r
+   png_error_ptr error_fn, png_error_ptr warn_fn)\r
+{\r
+\r
+#ifdef PNG_USER_MEM_SUPPORTED\r
+   return (png_create_read_struct_2(user_png_ver, error_ptr, error_fn,\r
+      warn_fn, NULL, NULL, NULL));\r
+}\r
+\r
+/* Alternate create PNG structure for reading, and allocate any memory\r
+ * needed.\r
+ */\r
+png_structp PNGAPI\r
+png_create_read_struct_2(png_const_charp user_png_ver, png_voidp error_ptr,\r
+   png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,\r
+   png_malloc_ptr malloc_fn, png_free_ptr free_fn)\r
+{\r
+#endif /* PNG_USER_MEM_SUPPORTED */\r
+\r
+#ifdef PNG_SETJMP_SUPPORTED\r
+   volatile\r
+#endif\r
+   png_structp png_ptr;\r
+   volatile int png_cleanup_needed = 0;\r
+\r
+#ifdef PNG_SETJMP_SUPPORTED\r
+#ifdef USE_FAR_KEYWORD\r
+   jmp_buf jmpbuf;\r
+#endif\r
+#endif\r
+\r
+   int i;\r
+\r
+   png_debug(1, "in png_create_read_struct");\r
+\r
+#ifdef PNG_USER_MEM_SUPPORTED\r
+   png_ptr = (png_structp)png_create_struct_2(PNG_STRUCT_PNG,\r
+      malloc_fn, mem_ptr);\r
+#else\r
+   png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG);\r
+#endif\r
+   if (png_ptr == NULL)\r
+      return (NULL);\r
+\r
+   /* Added at libpng-1.2.6 */\r
+#ifdef PNG_USER_LIMITS_SUPPORTED\r
+   png_ptr->user_width_max = PNG_USER_WIDTH_MAX;\r
+   png_ptr->user_height_max = PNG_USER_HEIGHT_MAX;\r
+#  ifdef PNG_USER_CHUNK_CACHE_MAX\r
+   /* Added at libpng-1.2.43 and 1.4.0 */\r
+   png_ptr->user_chunk_cache_max = PNG_USER_CHUNK_CACHE_MAX;\r
+#  endif\r
+#  ifdef PNG_SET_USER_CHUNK_MALLOC_MAX\r
+   /* Added at libpng-1.2.43 and 1.4.1 */\r
+   png_ptr->user_chunk_malloc_max = PNG_USER_CHUNK_MALLOC_MAX;\r
+#  endif\r
+#endif\r
+\r
+#ifdef PNG_SETJMP_SUPPORTED\r
+/* Applications that neglect to set up their own setjmp() and then\r
+   encounter a png_error() will longjmp here.  Since the jmpbuf is\r
+   then meaningless we abort instead of returning. */\r
+#ifdef USE_FAR_KEYWORD\r
+   if (setjmp(jmpbuf))\r
+#else\r
+   if (setjmp(png_jmpbuf(png_ptr))) /* Sets longjmp to match setjmp */\r
+#endif\r
+      PNG_ABORT();\r
+#ifdef USE_FAR_KEYWORD\r
+   png_memcpy(png_jmpbuf(png_ptr), jmpbuf, png_sizeof(jmp_buf));\r
+#endif\r
+#endif /* PNG_SETJMP_SUPPORTED */\r
+\r
+#ifdef PNG_USER_MEM_SUPPORTED\r
+   png_set_mem_fn(png_ptr, mem_ptr, malloc_fn, free_fn);\r
+#endif\r
+\r
+   png_set_error_fn(png_ptr, error_ptr, error_fn, warn_fn);\r
+\r
+   if (user_png_ver)\r
+   {\r
+      i = 0;\r
+      do\r
+      {\r
+         if (user_png_ver[i] != png_libpng_ver[i])\r
+            png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;\r
+      } while (png_libpng_ver[i++]);\r
+    }\r
+    else\r
+         png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;\r
+\r
+\r
+    if (png_ptr->flags & PNG_FLAG_LIBRARY_MISMATCH)\r
+    {\r
+       /* Libpng 0.90 and later are binary incompatible with libpng 0.89, so\r
+       * we must recompile any applications that use any older library version.\r
+       * For versions after libpng 1.0, we will be compatible, so we need\r
+       * only check the first digit.\r
+       */\r
+      if (user_png_ver == NULL || user_png_ver[0] != png_libpng_ver[0] ||\r
+          (user_png_ver[0] == '1' && user_png_ver[2] != png_libpng_ver[2]) ||\r
+          (user_png_ver[0] == '0' && user_png_ver[2] < '9'))\r
+      {\r
+#ifdef PNG_STDIO_SUPPORTED\r
+         char msg[80];\r
+         if (user_png_ver)\r
+         {\r
+           png_snprintf(msg, 80,\r
+              "Application was compiled with png.h from libpng-%.20s",\r
+              user_png_ver);\r
+           png_warning(png_ptr, msg);\r
+         }\r
+         png_snprintf(msg, 80,\r
+             "Application  is  running with png.c from libpng-%.20s",\r
+             png_libpng_ver);\r
+         png_warning(png_ptr, msg);\r
+#endif\r
+#ifdef PNG_ERROR_NUMBERS_SUPPORTED\r
+         png_ptr->flags = 0;\r
+#endif\r
+         png_warning(png_ptr,\r
+            "Incompatible libpng version in application and library");\r
+\r
+         png_cleanup_needed = 1;\r
+      }\r
+   }\r
+\r
+   if (!png_cleanup_needed)\r
+   {\r
+   /* Initialize zbuf - compression buffer */\r
+   png_ptr->zbuf_size = PNG_ZBUF_SIZE;\r
+   png_ptr->zbuf = (png_bytep)png_malloc_warn(png_ptr,\r
+     png_ptr->zbuf_size);\r
+   if (png_ptr->zbuf == NULL)\r
+        png_cleanup_needed = 1;\r
+   }\r
+   png_ptr->zstream.zalloc = png_zalloc;\r
+   png_ptr->zstream.zfree = png_zfree;\r
+   png_ptr->zstream.opaque = (voidpf)png_ptr;\r
+\r
+   if (!png_cleanup_needed)\r
+   {\r
+      switch (inflateInit(&png_ptr->zstream))\r
+      {\r
+         case Z_OK: /* Do nothing */ break;\r
+         case Z_MEM_ERROR:\r
+         case Z_STREAM_ERROR: png_warning(png_ptr, "zlib memory error");\r
+            png_cleanup_needed = 1; break;\r
+         case Z_VERSION_ERROR: png_warning(png_ptr, "zlib version error");\r
+            png_cleanup_needed = 1; break;\r
+         default: png_warning(png_ptr, "Unknown zlib error");\r
+            png_cleanup_needed = 1;\r
+      }\r
+   }\r
+\r
+   if (png_cleanup_needed)\r
+   {\r
+      /* Clean up PNG structure and deallocate any memory. */\r
+      png_free(png_ptr, png_ptr->zbuf);\r
+      png_ptr->zbuf = NULL;\r
+#ifdef PNG_USER_MEM_SUPPORTED\r
+      png_destroy_struct_2((png_voidp)png_ptr,\r
+         (png_free_ptr)free_fn, (png_voidp)mem_ptr);\r
+#else\r
+      png_destroy_struct((png_voidp)png_ptr);\r
+#endif\r
+      return (NULL);\r
+   }\r
+\r
+   png_ptr->zstream.next_out = png_ptr->zbuf;\r
+   png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;\r
+\r
+   png_set_read_fn(png_ptr, NULL, NULL);\r
+\r
+\r
+   return (png_ptr);\r
+}\r
+\r
+\r
+#ifdef PNG_SEQUENTIAL_READ_SUPPORTED\r
+/* Read the information before the actual image data.  This has been\r
+ * changed in v0.90 to allow reading a file that already has the magic\r
+ * bytes read from the stream.  You can tell libpng how many bytes have\r
+ * been read from the beginning of the stream (up to the maximum of 8)\r
+ * via png_set_sig_bytes(), and we will only check the remaining bytes\r
+ * here.  The application can then have access to the signature bytes we\r
+ * read if it is determined that this isn't a valid PNG file.\r
+ */\r
+void PNGAPI\r
+png_read_info(png_structp png_ptr, png_infop info_ptr)\r
+{\r
+   png_debug(1, "in png_read_info");\r
\r
+   if (png_ptr == NULL || info_ptr == NULL)\r
+      return;\r
\r
+   /* If we haven't checked all of the PNG signature bytes, do so now. */\r
+   if (png_ptr->sig_bytes < 8)\r
+   {\r
+      png_size_t num_checked = png_ptr->sig_bytes,\r
+                 num_to_check = 8 - num_checked;\r
+\r
+#ifdef PNG_IO_STATE_SUPPORTED\r
+      png_ptr->io_state = PNG_IO_READING | PNG_IO_SIGNATURE;\r
+#endif\r
+\r
+      png_read_data(png_ptr, &(info_ptr->signature[num_checked]), num_to_check);\r
+      png_ptr->sig_bytes = 8;\r
+\r
+      if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check))\r
+      {\r
+         if (num_checked < 4 &&\r
+             png_sig_cmp(info_ptr->signature, num_checked, num_to_check - 4))\r
+            png_error(png_ptr, "Not a PNG file");\r
+         else\r
+            png_error(png_ptr, "PNG file corrupted by ASCII conversion");\r
+      }\r
+      if (num_checked < 3)\r
+         png_ptr->mode |= PNG_HAVE_PNG_SIGNATURE;\r
+   }\r
+\r
+   for (;;)\r
+   {\r
+      PNG_IHDR;\r
+      PNG_IDAT;\r
+      PNG_IEND;\r
+      PNG_PLTE;\r
+#ifdef PNG_READ_bKGD_SUPPORTED\r
+      PNG_bKGD;\r
+#endif\r
+#ifdef PNG_READ_cHRM_SUPPORTED\r
+      PNG_cHRM;\r
+#endif\r
+#ifdef PNG_READ_gAMA_SUPPORTED\r
+      PNG_gAMA;\r
+#endif\r
+#ifdef PNG_READ_hIST_SUPPORTED\r
+      PNG_hIST;\r
+#endif\r
+#ifdef PNG_READ_iCCP_SUPPORTED\r
+      PNG_iCCP;\r
+#endif\r
+#ifdef PNG_READ_iTXt_SUPPORTED\r
+      PNG_iTXt;\r
+#endif\r
+#ifdef PNG_READ_oFFs_SUPPORTED\r
+      PNG_oFFs;\r
+#endif\r
+#ifdef PNG_READ_pCAL_SUPPORTED\r
+      PNG_pCAL;\r
+#endif\r
+#ifdef PNG_READ_pHYs_SUPPORTED\r
+      PNG_pHYs;\r
+#endif\r
+#ifdef PNG_READ_sBIT_SUPPORTED\r
+      PNG_sBIT;\r
+#endif\r
+#ifdef PNG_READ_sCAL_SUPPORTED\r
+      PNG_sCAL;\r
+#endif\r
+#ifdef PNG_READ_sPLT_SUPPORTED\r
+      PNG_sPLT;\r
+#endif\r
+#ifdef PNG_READ_sRGB_SUPPORTED\r
+      PNG_sRGB;\r
+#endif\r
+#ifdef PNG_READ_tEXt_SUPPORTED\r
+      PNG_tEXt;\r
+#endif\r
+#ifdef PNG_READ_tIME_SUPPORTED\r
+      PNG_tIME;\r
+#endif\r
+#ifdef PNG_READ_tRNS_SUPPORTED\r
+      PNG_tRNS;\r
+#endif\r
+#ifdef PNG_READ_zTXt_SUPPORTED\r
+      PNG_zTXt;\r
+#endif\r
+      png_uint_32 length = png_read_chunk_header(png_ptr);\r
+      PNG_CONST png_bytep chunk_name = png_ptr->chunk_name;\r
+\r
+      /* This should be a binary subdivision search or a hash for\r
+       * matching the chunk name rather than a linear search.\r
+       */\r
+      if (!png_memcmp(chunk_name, png_IDAT, 4))\r
+        if (png_ptr->mode & PNG_AFTER_IDAT)\r
+          png_ptr->mode |= PNG_HAVE_CHUNK_AFTER_IDAT;\r
+\r
+      if (!png_memcmp(chunk_name, png_IHDR, 4))\r
+         png_handle_IHDR(png_ptr, info_ptr, length);\r
+      else if (!png_memcmp(chunk_name, png_IEND, 4))\r
+         png_handle_IEND(png_ptr, info_ptr, length);\r
+#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED\r
+      else if (png_handle_as_unknown(png_ptr, chunk_name))\r
+      {\r
+         if (!png_memcmp(chunk_name, png_IDAT, 4))\r
+            png_ptr->mode |= PNG_HAVE_IDAT;\r
+         png_handle_unknown(png_ptr, info_ptr, length);\r
+         if (!png_memcmp(chunk_name, png_PLTE, 4))\r
+            png_ptr->mode |= PNG_HAVE_PLTE;\r
+         else if (!png_memcmp(chunk_name, png_IDAT, 4))\r
+         {\r
+            if (!(png_ptr->mode & PNG_HAVE_IHDR))\r
+               png_error(png_ptr, "Missing IHDR before IDAT");\r
+            else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&\r
+                     !(png_ptr->mode & PNG_HAVE_PLTE))\r
+               png_error(png_ptr, "Missing PLTE before IDAT");\r
+            break;\r
+         }\r
+      }\r
+#endif\r
+      else if (!png_memcmp(chunk_name, png_PLTE, 4))\r
+         png_handle_PLTE(png_ptr, info_ptr, length);\r
+      else if (!png_memcmp(chunk_name, png_IDAT, 4))\r
+      {\r
+         if (!(png_ptr->mode & PNG_HAVE_IHDR))\r
+            png_error(png_ptr, "Missing IHDR before IDAT");\r
+         else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&\r
+                  !(png_ptr->mode & PNG_HAVE_PLTE))\r
+            png_error(png_ptr, "Missing PLTE before IDAT");\r
+\r
+         png_ptr->idat_size = length;\r
+         png_ptr->mode |= PNG_HAVE_IDAT;\r
+         break;\r
+      }\r
+#ifdef PNG_READ_bKGD_SUPPORTED\r
+      else if (!png_memcmp(chunk_name, png_bKGD, 4))\r
+         png_handle_bKGD(png_ptr, info_ptr, length);\r
+#endif\r
+#ifdef PNG_READ_cHRM_SUPPORTED\r
+      else if (!png_memcmp(chunk_name, png_cHRM, 4))\r
+         png_handle_cHRM(png_ptr, info_ptr, length);\r
+#endif\r
+#ifdef PNG_READ_gAMA_SUPPORTED\r
+      else if (!png_memcmp(chunk_name, png_gAMA, 4))\r
+         png_handle_gAMA(png_ptr, info_ptr, length);\r
+#endif\r
+#ifdef PNG_READ_hIST_SUPPORTED\r
+      else if (!png_memcmp(chunk_name, png_hIST, 4))\r
+         png_handle_hIST(png_ptr, info_ptr, length);\r
+#endif\r
+#ifdef PNG_READ_oFFs_SUPPORTED\r
+      else if (!png_memcmp(chunk_name, png_oFFs, 4))\r
+         png_handle_oFFs(png_ptr, info_ptr, length);\r
+#endif\r
+#ifdef PNG_READ_pCAL_SUPPORTED\r
+      else if (!png_memcmp(chunk_name, png_pCAL, 4))\r
+         png_handle_pCAL(png_ptr, info_ptr, length);\r
+#endif\r
+#ifdef PNG_READ_sCAL_SUPPORTED\r
+      else if (!png_memcmp(chunk_name, png_sCAL, 4))\r
+         png_handle_sCAL(png_ptr, info_ptr, length);\r
+#endif\r
+#ifdef PNG_READ_pHYs_SUPPORTED\r
+      else if (!png_memcmp(chunk_name, png_pHYs, 4))\r
+         png_handle_pHYs(png_ptr, info_ptr, length);\r
+#endif\r
+#ifdef PNG_READ_sBIT_SUPPORTED\r
+      else if (!png_memcmp(chunk_name, png_sBIT, 4))\r
+         png_handle_sBIT(png_ptr, info_ptr, length);\r
+#endif\r
+#ifdef PNG_READ_sRGB_SUPPORTED\r
+      else if (!png_memcmp(chunk_name, png_sRGB, 4))\r
+         png_handle_sRGB(png_ptr, info_ptr, length);\r
+#endif\r
+#ifdef PNG_READ_iCCP_SUPPORTED\r
+      else if (!png_memcmp(chunk_name, png_iCCP, 4))\r
+         png_handle_iCCP(png_ptr, info_ptr, length);\r
+#endif\r
+#ifdef PNG_READ_sPLT_SUPPORTED\r
+      else if (!png_memcmp(chunk_name, png_sPLT, 4))\r
+         png_handle_sPLT(png_ptr, info_ptr, length);\r
+#endif\r
+#ifdef PNG_READ_tEXt_SUPPORTED\r
+      else if (!png_memcmp(chunk_name, png_tEXt, 4))\r
+         png_handle_tEXt(png_ptr, info_ptr, length);\r
+#endif\r
+#ifdef PNG_READ_tIME_SUPPORTED\r
+      else if (!png_memcmp(chunk_name, png_tIME, 4))\r
+         png_handle_tIME(png_ptr, info_ptr, length);\r
+#endif\r
+#ifdef PNG_READ_tRNS_SUPPORTED\r
+      else if (!png_memcmp(chunk_name, png_tRNS, 4))\r
+         png_handle_tRNS(png_ptr, info_ptr, length);\r
+#endif\r
+#ifdef PNG_READ_zTXt_SUPPORTED\r
+      else if (!png_memcmp(chunk_name, png_zTXt, 4))\r
+         png_handle_zTXt(png_ptr, info_ptr, length);\r
+#endif\r
+#ifdef PNG_READ_iTXt_SUPPORTED\r
+      else if (!png_memcmp(chunk_name, png_iTXt, 4))\r
+         png_handle_iTXt(png_ptr, info_ptr, length);\r
+#endif\r
+      else\r
+         png_handle_unknown(png_ptr, info_ptr, length);\r
+   }\r
+}\r
+#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */\r
+\r
+/* Optional call to update the users info_ptr structure */\r
+void PNGAPI\r
+png_read_update_info(png_structp png_ptr, png_infop info_ptr)\r
+{\r
+   png_debug(1, "in png_read_update_info");\r
\r
+   if (png_ptr == NULL)\r
+      return;\r
+   if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))\r
+      png_read_start_row(png_ptr);\r
+   else\r
+      png_warning(png_ptr,\r
+      "Ignoring extra png_read_update_info() call; row buffer not reallocated");\r
+\r
+   png_read_transform_info(png_ptr, info_ptr);\r
+}\r
+\r
+#ifdef PNG_SEQUENTIAL_READ_SUPPORTED\r
+/* Initialize palette, background, etc, after transformations\r
+ * are set, but before any reading takes place.  This allows\r
+ * the user to obtain a gamma-corrected palette, for example.\r
+ * If the user doesn't call this, we will do it ourselves.\r
+ */\r
+void PNGAPI\r
+png_start_read_image(png_structp png_ptr)\r
+{\r
+   png_debug(1, "in png_start_read_image");\r
\r
+   if (png_ptr == NULL)\r
+      return;\r
+   if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))\r
+      png_read_start_row(png_ptr);\r
+}\r
+#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */\r
+\r
+#ifdef PNG_SEQUENTIAL_READ_SUPPORTED\r
+void PNGAPI\r
+png_read_row(png_structp png_ptr, png_bytep row, png_bytep dsp_row)\r
+{\r
+   PNG_IDAT;\r
+   PNG_CONST int png_pass_dsp_mask[7] = {0xff, 0x0f, 0xff, 0x33, 0xff, 0x55,\r
+      0xff};\r
+   PNG_CONST int png_pass_mask[7] = {0x80, 0x08, 0x88, 0x22, 0xaa, 0x55, 0xff};\r
+   int ret;\r
\r
+   if (png_ptr == NULL)\r
+      return;\r
\r
+   png_debug2(1, "in png_read_row (row %lu, pass %d)",\r
+      (unsigned long) png_ptr->row_number, png_ptr->pass);\r
+\r
+   if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))\r
+      png_read_start_row(png_ptr);\r
+   if (png_ptr->row_number == 0 && png_ptr->pass == 0)\r
+   {\r
+   /* Check for transforms that have been set but were defined out */\r
+#if defined(PNG_WRITE_INVERT_SUPPORTED) && !defined(PNG_READ_INVERT_SUPPORTED)\r
+   if (png_ptr->transformations & PNG_INVERT_MONO)\r
+      png_warning(png_ptr, "PNG_READ_INVERT_SUPPORTED is not defined");\r
+#endif\r
+#if defined(PNG_WRITE_FILLER_SUPPORTED) && !defined(PNG_READ_FILLER_SUPPORTED)\r
+   if (png_ptr->transformations & PNG_FILLER)\r
+      png_warning(png_ptr, "PNG_READ_FILLER_SUPPORTED is not defined");\r
+#endif\r
+#if defined(PNG_WRITE_PACKSWAP_SUPPORTED) && \\r
+    !defined(PNG_READ_PACKSWAP_SUPPORTED)\r
+   if (png_ptr->transformations & PNG_PACKSWAP)\r
+      png_warning(png_ptr, "PNG_READ_PACKSWAP_SUPPORTED is not defined");\r
+#endif\r
+#if defined(PNG_WRITE_PACK_SUPPORTED) && !defined(PNG_READ_PACK_SUPPORTED)\r
+   if (png_ptr->transformations & PNG_PACK)\r
+      png_warning(png_ptr, "PNG_READ_PACK_SUPPORTED is not defined");\r
+#endif\r
+#if defined(PNG_WRITE_SHIFT_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED)\r
+   if (png_ptr->transformations & PNG_SHIFT)\r
+      png_warning(png_ptr, "PNG_READ_SHIFT_SUPPORTED is not defined");\r
+#endif\r
+#if defined(PNG_WRITE_BGR_SUPPORTED) && !defined(PNG_READ_BGR_SUPPORTED)\r
+   if (png_ptr->transformations & PNG_BGR)\r
+      png_warning(png_ptr, "PNG_READ_BGR_SUPPORTED is not defined");\r
+#endif\r
+#if defined(PNG_WRITE_SWAP_SUPPORTED) && !defined(PNG_READ_SWAP_SUPPORTED)\r
+   if (png_ptr->transformations & PNG_SWAP_BYTES)\r
+      png_warning(png_ptr, "PNG_READ_SWAP_SUPPORTED is not defined");\r
+#endif\r
+   }\r
+\r
+#ifdef PNG_READ_INTERLACING_SUPPORTED\r
+   /* If interlaced and we do not need a new row, combine row and return */\r
+   if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))\r
+   {\r
+      switch (png_ptr->pass)\r
+      {\r
+         case 0:\r
+            if (png_ptr->row_number & 0x07)\r
+            {\r
+               if (dsp_row != NULL)\r
+                  png_combine_row(png_ptr, dsp_row,\r
+                     png_pass_dsp_mask[png_ptr->pass]);\r
+               png_read_finish_row(png_ptr);\r
+               return;\r
+            }\r
+            break;\r
+         case 1:\r
+            if ((png_ptr->row_number & 0x07) || png_ptr->width < 5)\r
+            {\r
+               if (dsp_row != NULL)\r
+                  png_combine_row(png_ptr, dsp_row,\r
+                     png_pass_dsp_mask[png_ptr->pass]);\r
+               png_read_finish_row(png_ptr);\r
+               return;\r
+            }\r
+            break;\r
+         case 2:\r
+            if ((png_ptr->row_number & 0x07) != 4)\r
+            {\r
+               if (dsp_row != NULL && (png_ptr->row_number & 4))\r
+                  png_combine_row(png_ptr, dsp_row,\r
+                     png_pass_dsp_mask[png_ptr->pass]);\r
+               png_read_finish_row(png_ptr);\r
+               return;\r
+            }\r
+            break;\r
+         case 3:\r
+            if ((png_ptr->row_number & 3) || png_ptr->width < 3)\r
+            {\r
+               if (dsp_row != NULL)\r
+                  png_combine_row(png_ptr, dsp_row,\r
+                     png_pass_dsp_mask[png_ptr->pass]);\r
+               png_read_finish_row(png_ptr);\r
+               return;\r
+            }\r
+            break;\r
+         case 4:\r
+            if ((png_ptr->row_number & 3) != 2)\r
+            {\r
+               if (dsp_row != NULL && (png_ptr->row_number & 2))\r
+                  png_combine_row(png_ptr, dsp_row,\r
+                     png_pass_dsp_mask[png_ptr->pass]);\r
+               png_read_finish_row(png_ptr);\r
+               return;\r
+            }\r
+            break;\r
+         case 5:\r
+            if ((png_ptr->row_number & 1) || png_ptr->width < 2)\r
+            {\r
+               if (dsp_row != NULL)\r
+                  png_combine_row(png_ptr, dsp_row,\r
+                     png_pass_dsp_mask[png_ptr->pass]);\r
+               png_read_finish_row(png_ptr);\r
+               return;\r
+            }\r
+            break;\r
+         case 6:\r
+            if (!(png_ptr->row_number & 1))\r
+            {\r
+               png_read_finish_row(png_ptr);\r
+               return;\r
+            }\r
+            break;\r
+      }\r
+   }\r
+#endif\r
+\r
+   if (!(png_ptr->mode & PNG_HAVE_IDAT))\r
+      png_error(png_ptr, "Invalid attempt to read row data");\r
+\r
+   png_ptr->zstream.next_out = png_ptr->row_buf;\r
+   png_ptr->zstream.avail_out =\r
+       (uInt)(PNG_ROWBYTES(png_ptr->pixel_depth,\r
+       png_ptr->iwidth) + 1);\r
+   do\r
+   {\r
+      if (!(png_ptr->zstream.avail_in))\r
+      {\r
+         while (!png_ptr->idat_size)\r
+         {\r
+            png_crc_finish(png_ptr, 0);\r
+\r
+            png_ptr->idat_size = png_read_chunk_header(png_ptr);\r
+            if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4))\r
+               png_error(png_ptr, "Not enough image data");\r
+         }\r
+         png_ptr->zstream.avail_in = (uInt)png_ptr->zbuf_size;\r
+         png_ptr->zstream.next_in = png_ptr->zbuf;\r
+         if (png_ptr->zbuf_size > png_ptr->idat_size)\r
+            png_ptr->zstream.avail_in = (uInt)png_ptr->idat_size;\r
+         png_crc_read(png_ptr, png_ptr->zbuf,\r
+            (png_size_t)png_ptr->zstream.avail_in);\r
+         png_ptr->idat_size -= png_ptr->zstream.avail_in;\r
+      }\r
+      ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);\r
+      if (ret == Z_STREAM_END)\r
+      {\r
+         if (png_ptr->zstream.avail_out || png_ptr->zstream.avail_in ||\r
+            png_ptr->idat_size)\r
+            png_benign_error(png_ptr, "Extra compressed data");\r
+         png_ptr->mode |= PNG_AFTER_IDAT;\r
+         png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;\r
+         break;\r
+      }\r
+      if (ret != Z_OK)\r
+         png_error(png_ptr, png_ptr->zstream.msg ? png_ptr->zstream.msg :\r
+                   "Decompression error");\r
+\r
+   } while (png_ptr->zstream.avail_out);\r
+\r
+   png_ptr->row_info.color_type = png_ptr->color_type;\r
+   png_ptr->row_info.width = png_ptr->iwidth;\r
+   png_ptr->row_info.channels = png_ptr->channels;\r
+   png_ptr->row_info.bit_depth = png_ptr->bit_depth;\r
+   png_ptr->row_info.pixel_depth = png_ptr->pixel_depth;\r
+   png_ptr->row_info.rowbytes = PNG_ROWBYTES(png_ptr->row_info.pixel_depth,\r
+       png_ptr->row_info.width);\r
+\r
+   if (png_ptr->row_buf[0])\r
+   png_read_filter_row(png_ptr, &(png_ptr->row_info),\r
+      png_ptr->row_buf + 1, png_ptr->prev_row + 1,\r
+      (int)(png_ptr->row_buf[0]));\r
+\r
+   png_memcpy(png_ptr->prev_row, png_ptr->row_buf, png_ptr->rowbytes + 1);\r
+\r
+#ifdef PNG_MNG_FEATURES_SUPPORTED\r
+   if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&\r
+      (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING))\r
+   {\r
+      /* Intrapixel differencing */\r
+      png_do_read_intrapixel(&(png_ptr->row_info), png_ptr->row_buf + 1);\r
+   }\r
+#endif\r
+\r
+\r
+   if (png_ptr->transformations || (png_ptr->flags&PNG_FLAG_STRIP_ALPHA))\r
+      png_do_read_transformations(png_ptr);\r
+\r
+#ifdef PNG_READ_INTERLACING_SUPPORTED\r
+   /* Blow up interlaced rows to full size */\r
+   if (png_ptr->interlaced &&\r
+      (png_ptr->transformations & PNG_INTERLACE))\r
+   {\r
+      if (png_ptr->pass < 6)\r
+         /* Old interface (pre-1.0.9):\r
+          * png_do_read_interlace(&(png_ptr->row_info),\r
+          *    png_ptr->row_buf + 1, png_ptr->pass, png_ptr->transformations);\r
+          */\r
+         png_do_read_interlace(png_ptr);\r
+\r
+      if (dsp_row != NULL)\r
+         png_combine_row(png_ptr, dsp_row,\r
+            png_pass_dsp_mask[png_ptr->pass]);\r
+      if (row != NULL)\r
+         png_combine_row(png_ptr, row,\r
+            png_pass_mask[png_ptr->pass]);\r
+   }\r
+   else\r
+#endif\r
+   {\r
+      if (row != NULL)\r
+         png_combine_row(png_ptr, row, 0xff);\r
+      if (dsp_row != NULL)\r
+         png_combine_row(png_ptr, dsp_row, 0xff);\r
+   }\r
+   png_read_finish_row(png_ptr);\r
+\r
+   if (png_ptr->read_row_fn != NULL)\r
+      (*(png_ptr->read_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);\r
+}\r
+#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */\r
+\r
+#ifdef PNG_SEQUENTIAL_READ_SUPPORTED\r
+/* Read one or more rows of image data.  If the image is interlaced,\r
+ * and png_set_interlace_handling() has been called, the rows need to\r
+ * contain the contents of the rows from the previous pass.  If the\r
+ * image has alpha or transparency, and png_handle_alpha()[*] has been\r
+ * called, the rows contents must be initialized to the contents of the\r
+ * screen.\r
+ *\r
+ * "row" holds the actual image, and pixels are placed in it\r
+ * as they arrive.  If the image is displayed after each pass, it will\r
+ * appear to "sparkle" in.  "display_row" can be used to display a\r
+ * "chunky" progressive image, with finer detail added as it becomes\r
+ * available.  If you do not want this "chunky" display, you may pass\r
+ * NULL for display_row.  If you do not want the sparkle display, and\r
+ * you have not called png_handle_alpha(), you may pass NULL for rows.\r
+ * If you have called png_handle_alpha(), and the image has either an\r
+ * alpha channel or a transparency chunk, you must provide a buffer for\r
+ * rows.  In this case, you do not have to provide a display_row buffer\r
+ * also, but you may.  If the image is not interlaced, or if you have\r
+ * not called png_set_interlace_handling(), the display_row buffer will\r
+ * be ignored, so pass NULL to it.\r
+ *\r
+ * [*] png_handle_alpha() does not exist yet, as of this version of libpng\r
+ */\r
+\r
+void PNGAPI\r
+png_read_rows(png_structp png_ptr, png_bytepp row,\r
+   png_bytepp display_row, png_uint_32 num_rows)\r
+{\r
+   png_uint_32 i;\r
+   png_bytepp rp;\r
+   png_bytepp dp;\r
+\r
+   png_debug(1, "in png_read_rows");\r
\r
+   if (png_ptr == NULL)\r
+      return;\r
+   rp = row;\r
+   dp = display_row;\r
+   if (rp != NULL && dp != NULL)\r
+      for (i = 0; i < num_rows; i++)\r
+      {\r
+         png_bytep rptr = *rp++;\r
+         png_bytep dptr = *dp++;\r
+\r
+         png_read_row(png_ptr, rptr, dptr);\r
+      }\r
+   else if (rp != NULL)\r
+      for (i = 0; i < num_rows; i++)\r
+      {\r
+         png_bytep rptr = *rp;\r
+         png_read_row(png_ptr, rptr, NULL);\r
+         rp++;\r
+      }\r
+   else if (dp != NULL)\r
+      for (i = 0; i < num_rows; i++)\r
+      {\r
+         png_bytep dptr = *dp;\r
+         png_read_row(png_ptr, NULL, dptr);\r
+         dp++;\r
+      }\r
+}\r
+#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */\r
+\r
+#ifdef PNG_SEQUENTIAL_READ_SUPPORTED\r
+/* Read the entire image.  If the image has an alpha channel or a tRNS\r
+ * chunk, and you have called png_handle_alpha()[*], you will need to\r
+ * initialize the image to the current image that PNG will be overlaying.\r
+ * We set the num_rows again here, in case it was incorrectly set in\r
+ * png_read_start_row() by a call to png_read_update_info() or\r
+ * png_start_read_image() if png_set_interlace_handling() wasn't called\r
+ * prior to either of these functions like it should have been.  You can\r
+ * only call this function once.  If you desire to have an image for\r
+ * each pass of a interlaced image, use png_read_rows() instead.\r
+ *\r
+ * [*] png_handle_alpha() does not exist yet, as of this version of libpng\r
+ */\r
+void PNGAPI\r
+png_read_image(png_structp png_ptr, png_bytepp image)\r
+{\r
+   png_uint_32 i, image_height;\r
+   int pass, j;\r
+   png_bytepp rp;\r
+\r
+   png_debug(1, "in png_read_image");\r
\r
+   if (png_ptr == NULL)\r
+      return;\r
+\r
+#ifdef PNG_READ_INTERLACING_SUPPORTED\r
+   pass = png_set_interlace_handling(png_ptr);\r
+#else\r
+   if (png_ptr->interlaced)\r
+      png_error(png_ptr,\r
+        "Cannot read interlaced image -- interlace handler disabled");\r
+   pass = 1;\r
+#endif\r
+\r
+\r
+   image_height=png_ptr->height;\r
+   png_ptr->num_rows = image_height; /* Make sure this is set correctly */\r
+\r
+   for (j = 0; j < pass; j++)\r
+   {\r
+      rp = image;\r
+      for (i = 0; i < image_height; i++)\r
+      {\r
+         png_read_row(png_ptr, *rp, NULL);\r
+         rp++;\r
+      }\r
+   }\r
+}\r
+#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */\r
+\r
+#ifdef PNG_SEQUENTIAL_READ_SUPPORTED\r
+/* Read the end of the PNG file.  Will not read past the end of the\r
+ * file, will verify the end is accurate, and will read any comments\r
+ * or time information at the end of the file, if info is not NULL.\r
+ */\r
+void PNGAPI\r
+png_read_end(png_structp png_ptr, png_infop info_ptr)\r
+{\r
+   png_debug(1, "in png_read_end");\r
\r
+   if (png_ptr == NULL)\r
+      return;\r
+   png_crc_finish(png_ptr, 0); /* Finish off CRC from last IDAT chunk */\r
+\r
+   do\r
+   {\r
+      PNG_IHDR;\r
+      PNG_IDAT;\r
+      PNG_IEND;\r
+      PNG_PLTE;\r
+#ifdef PNG_READ_bKGD_SUPPORTED\r
+      PNG_bKGD;\r
+#endif\r
+#ifdef PNG_READ_cHRM_SUPPORTED\r
+      PNG_cHRM;\r
+#endif\r
+#ifdef PNG_READ_gAMA_SUPPORTED\r
+      PNG_gAMA;\r
+#endif\r
+#ifdef PNG_READ_hIST_SUPPORTED\r
+      PNG_hIST;\r
+#endif\r
+#ifdef PNG_READ_iCCP_SUPPORTED\r
+      PNG_iCCP;\r
+#endif\r
+#ifdef PNG_READ_iTXt_SUPPORTED\r
+      PNG_iTXt;\r
+#endif\r
+#ifdef PNG_READ_oFFs_SUPPORTED\r
+      PNG_oFFs;\r
+#endif\r
+#ifdef PNG_READ_pCAL_SUPPORTED\r
+      PNG_pCAL;\r
+#endif\r
+#ifdef PNG_READ_pHYs_SUPPORTED\r
+      PNG_pHYs;\r
+#endif\r
+#ifdef PNG_READ_sBIT_SUPPORTED\r
+      PNG_sBIT;\r
+#endif\r
+#ifdef PNG_READ_sCAL_SUPPORTED\r
+      PNG_sCAL;\r
+#endif\r
+#ifdef PNG_READ_sPLT_SUPPORTED\r
+      PNG_sPLT;\r
+#endif\r
+#ifdef PNG_READ_sRGB_SUPPORTED\r
+      PNG_sRGB;\r
+#endif\r
+#ifdef PNG_READ_tEXt_SUPPORTED\r
+      PNG_tEXt;\r
+#endif\r
+#ifdef PNG_READ_tIME_SUPPORTED\r
+      PNG_tIME;\r
+#endif\r
+#ifdef PNG_READ_tRNS_SUPPORTED\r
+      PNG_tRNS;\r
+#endif\r
+#ifdef PNG_READ_zTXt_SUPPORTED\r
+      PNG_zTXt;\r
+#endif\r
+      png_uint_32 length = png_read_chunk_header(png_ptr);\r
+      PNG_CONST png_bytep chunk_name = png_ptr->chunk_name;\r
+\r
+      if (!png_memcmp(chunk_name, png_IHDR, 4))\r
+         png_handle_IHDR(png_ptr, info_ptr, length);\r
+      else if (!png_memcmp(chunk_name, png_IEND, 4))\r
+         png_handle_IEND(png_ptr, info_ptr, length);\r
+#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED\r
+      else if (png_handle_as_unknown(png_ptr, chunk_name))\r
+      {\r
+         if (!png_memcmp(chunk_name, png_IDAT, 4))\r
+         {\r
+            if ((length > 0) || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT))\r
+               png_benign_error(png_ptr, "Too many IDATs found");\r
+         }\r
+         png_handle_unknown(png_ptr, info_ptr, length);\r
+         if (!png_memcmp(chunk_name, png_PLTE, 4))\r
+            png_ptr->mode |= PNG_HAVE_PLTE;\r
+      }\r
+#endif\r
+      else if (!png_memcmp(chunk_name, png_IDAT, 4))\r
+      {\r
+         /* Zero length IDATs are legal after the last IDAT has been\r
+          * read, but not after other chunks have been read.\r
+          */\r
+         if ((length > 0) || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT))\r
+            png_benign_error(png_ptr, "Too many IDATs found");\r
+         png_crc_finish(png_ptr, length);\r
+      }\r
+      else if (!png_memcmp(chunk_name, png_PLTE, 4))\r
+         png_handle_PLTE(png_ptr, info_ptr, length);\r
+#ifdef PNG_READ_bKGD_SUPPORTED\r
+      else if (!png_memcmp(chunk_name, png_bKGD, 4))\r
+         png_handle_bKGD(png_ptr, info_ptr, length);\r
+#endif\r
+#ifdef PNG_READ_cHRM_SUPPORTED\r
+      else if (!png_memcmp(chunk_name, png_cHRM, 4))\r
+         png_handle_cHRM(png_ptr, info_ptr, length);\r
+#endif\r
+#ifdef PNG_READ_gAMA_SUPPORTED\r
+      else if (!png_memcmp(chunk_name, png_gAMA, 4))\r
+         png_handle_gAMA(png_ptr, info_ptr, length);\r
+#endif\r
+#ifdef PNG_READ_hIST_SUPPORTED\r
+      else if (!png_memcmp(chunk_name, png_hIST, 4))\r
+         png_handle_hIST(png_ptr, info_ptr, length);\r
+#endif\r
+#ifdef PNG_READ_oFFs_SUPPORTED\r
+      else if (!png_memcmp(chunk_name, png_oFFs, 4))\r
+         png_handle_oFFs(png_ptr, info_ptr, length);\r
+#endif\r
+#ifdef PNG_READ_pCAL_SUPPORTED\r
+      else if (!png_memcmp(chunk_name, png_pCAL, 4))\r
+         png_handle_pCAL(png_ptr, info_ptr, length);\r
+#endif\r
+#ifdef PNG_READ_sCAL_SUPPORTED\r
+      else if (!png_memcmp(chunk_name, png_sCAL, 4))\r
+         png_handle_sCAL(png_ptr, info_ptr, length);\r
+#endif\r
+#ifdef PNG_READ_pHYs_SUPPORTED\r
+      else if (!png_memcmp(chunk_name, png_pHYs, 4))\r
+         png_handle_pHYs(png_ptr, info_ptr, length);\r
+#endif\r
+#ifdef PNG_READ_sBIT_SUPPORTED\r
+      else if (!png_memcmp(chunk_name, png_sBIT, 4))\r
+         png_handle_sBIT(png_ptr, info_ptr, length);\r
+#endif\r
+#ifdef PNG_READ_sRGB_SUPPORTED\r
+      else if (!png_memcmp(chunk_name, png_sRGB, 4))\r
+         png_handle_sRGB(png_ptr, info_ptr, length);\r
+#endif\r
+#ifdef PNG_READ_iCCP_SUPPORTED\r
+      else if (!png_memcmp(chunk_name, png_iCCP, 4))\r
+         png_handle_iCCP(png_ptr, info_ptr, length);\r
+#endif\r
+#ifdef PNG_READ_sPLT_SUPPORTED\r
+      else if (!png_memcmp(chunk_name, png_sPLT, 4))\r
+         png_handle_sPLT(png_ptr, info_ptr, length);\r
+#endif\r
+#ifdef PNG_READ_tEXt_SUPPORTED\r
+      else if (!png_memcmp(chunk_name, png_tEXt, 4))\r
+         png_handle_tEXt(png_ptr, info_ptr, length);\r
+#endif\r
+#ifdef PNG_READ_tIME_SUPPORTED\r
+      else if (!png_memcmp(chunk_name, png_tIME, 4))\r
+         png_handle_tIME(png_ptr, info_ptr, length);\r
+#endif\r
+#ifdef PNG_READ_tRNS_SUPPORTED\r
+      else if (!png_memcmp(chunk_name, png_tRNS, 4))\r
+         png_handle_tRNS(png_ptr, info_ptr, length);\r
+#endif\r
+#ifdef PNG_READ_zTXt_SUPPORTED\r
+      else if (!png_memcmp(chunk_name, png_zTXt, 4))\r
+         png_handle_zTXt(png_ptr, info_ptr, length);\r
+#endif\r
+#ifdef PNG_READ_iTXt_SUPPORTED\r
+      else if (!png_memcmp(chunk_name, png_iTXt, 4))\r
+         png_handle_iTXt(png_ptr, info_ptr, length);\r
+#endif\r
+      else\r
+         png_handle_unknown(png_ptr, info_ptr, length);\r
+   } while (!(png_ptr->mode & PNG_HAVE_IEND));\r
+}\r
+#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */\r
+\r
+/* Free all memory used by the read */\r
+void PNGAPI\r
+png_destroy_read_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr,\r
+   png_infopp end_info_ptr_ptr)\r
+{\r
+   png_structp png_ptr = NULL;\r
+   png_infop info_ptr = NULL, end_info_ptr = NULL;\r
+#ifdef PNG_USER_MEM_SUPPORTED\r
+   png_free_ptr free_fn = NULL;\r
+   png_voidp mem_ptr = NULL;\r
+#endif\r
+\r
+   png_debug(1, "in png_destroy_read_struct");\r
\r
+   if (png_ptr_ptr != NULL)\r
+      png_ptr = *png_ptr_ptr;\r
+   if (png_ptr == NULL)\r
+      return;\r
+\r
+#ifdef PNG_USER_MEM_SUPPORTED\r
+   free_fn = png_ptr->free_fn;\r
+   mem_ptr = png_ptr->mem_ptr;\r
+#endif\r
+\r
+   if (info_ptr_ptr != NULL)\r
+      info_ptr = *info_ptr_ptr;\r
+\r
+   if (end_info_ptr_ptr != NULL)\r
+      end_info_ptr = *end_info_ptr_ptr;\r
+\r
+   png_read_destroy(png_ptr, info_ptr, end_info_ptr);\r
+\r
+   if (info_ptr != NULL)\r
+   {\r
+#ifdef PNG_TEXT_SUPPORTED\r
+      png_free_data(png_ptr, info_ptr, PNG_FREE_TEXT, -1);\r
+#endif\r
+\r
+#ifdef PNG_USER_MEM_SUPPORTED\r
+      png_destroy_struct_2((png_voidp)info_ptr, (png_free_ptr)free_fn,\r
+          (png_voidp)mem_ptr);\r
+#else\r
+      png_destroy_struct((png_voidp)info_ptr);\r
+#endif\r
+      *info_ptr_ptr = NULL;\r
+   }\r
+\r
+   if (end_info_ptr != NULL)\r
+   {\r
+#ifdef PNG_READ_TEXT_SUPPORTED\r
+      png_free_data(png_ptr, end_info_ptr, PNG_FREE_TEXT, -1);\r
+#endif\r
+#ifdef PNG_USER_MEM_SUPPORTED\r
+      png_destroy_struct_2((png_voidp)end_info_ptr, (png_free_ptr)free_fn,\r
+         (png_voidp)mem_ptr);\r
+#else\r
+      png_destroy_struct((png_voidp)end_info_ptr);\r
+#endif\r
+      *end_info_ptr_ptr = NULL;\r
+   }\r
+\r
+   if (png_ptr != NULL)\r
+   {\r
+#ifdef PNG_USER_MEM_SUPPORTED\r
+      png_destroy_struct_2((png_voidp)png_ptr, (png_free_ptr)free_fn,\r
+          (png_voidp)mem_ptr);\r
+#else\r
+      png_destroy_struct((png_voidp)png_ptr);\r
+#endif\r
+      *png_ptr_ptr = NULL;\r
+   }\r
+}\r
+\r
+/* Free all memory used by the read (old method) */\r
+void /* PRIVATE */\r
+png_read_destroy(png_structp png_ptr, png_infop info_ptr,\r
+    png_infop end_info_ptr)\r
+{\r
+#ifdef PNG_SETJMP_SUPPORTED\r
+   jmp_buf tmp_jmp;\r
+#endif\r
+   png_error_ptr error_fn;\r
+   png_error_ptr warning_fn;\r
+   png_voidp error_ptr;\r
+#ifdef PNG_USER_MEM_SUPPORTED\r
+   png_free_ptr free_fn;\r
+#endif\r
+\r
+   png_debug(1, "in png_read_destroy");\r
\r
+   if (info_ptr != NULL)\r
+      png_info_destroy(png_ptr, info_ptr);\r
+\r
+   if (end_info_ptr != NULL)\r
+      png_info_destroy(png_ptr, end_info_ptr);\r
+\r
+   png_free(png_ptr, png_ptr->zbuf);\r
+   png_free(png_ptr, png_ptr->big_row_buf);\r
+   png_free(png_ptr, png_ptr->prev_row);\r
+   png_free(png_ptr, png_ptr->chunkdata);\r
+#ifdef PNG_READ_QUANTIZE_SUPPORTED\r
+   png_free(png_ptr, png_ptr->palette_lookup);\r
+   png_free(png_ptr, png_ptr->quantize_index);\r
+#endif\r
+#ifdef PNG_READ_GAMMA_SUPPORTED\r
+   png_free(png_ptr, png_ptr->gamma_table);\r
+#endif\r
+#ifdef PNG_READ_BACKGROUND_SUPPORTED\r
+   png_free(png_ptr, png_ptr->gamma_from_1);\r
+   png_free(png_ptr, png_ptr->gamma_to_1);\r
+#endif\r
+   if (png_ptr->free_me & PNG_FREE_PLTE)\r
+      png_zfree(png_ptr, png_ptr->palette);\r
+   png_ptr->free_me &= ~PNG_FREE_PLTE;\r
+#if defined(PNG_tRNS_SUPPORTED) || \\r
+    defined(PNG_READ_EXPAND_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)\r
+   if (png_ptr->free_me & PNG_FREE_TRNS)\r
+      png_free(png_ptr, png_ptr->trans_alpha);\r
+   png_ptr->free_me &= ~PNG_FREE_TRNS;\r
+#endif\r
+#ifdef PNG_READ_hIST_SUPPORTED\r
+   if (png_ptr->free_me & PNG_FREE_HIST)\r
+      png_free(png_ptr, png_ptr->hist);\r
+   png_ptr->free_me &= ~PNG_FREE_HIST;\r
+#endif\r
+#ifdef PNG_READ_GAMMA_SUPPORTED\r
+   if (png_ptr->gamma_16_table != NULL)\r
+   {\r
+      int i;\r
+      int istop = (1 << (8 - png_ptr->gamma_shift));\r
+      for (i = 0; i < istop; i++)\r
+      {\r
+         png_free(png_ptr, png_ptr->gamma_16_table[i]);\r
+      }\r
+   png_free(png_ptr, png_ptr->gamma_16_table);\r
+   }\r
+#ifdef PNG_READ_BACKGROUND_SUPPORTED\r
+   if (png_ptr->gamma_16_from_1 != NULL)\r
+   {\r
+      int i;\r
+      int istop = (1 << (8 - png_ptr->gamma_shift));\r
+      for (i = 0; i < istop; i++)\r
+      {\r
+         png_free(png_ptr, png_ptr->gamma_16_from_1[i]);\r
+      }\r
+   png_free(png_ptr, png_ptr->gamma_16_from_1);\r
+   }\r
+   if (png_ptr->gamma_16_to_1 != NULL)\r
+   {\r
+      int i;\r
+      int istop = (1 << (8 - png_ptr->gamma_shift));\r
+      for (i = 0; i < istop; i++)\r
+      {\r
+         png_free(png_ptr, png_ptr->gamma_16_to_1[i]);\r
+      }\r
+   png_free(png_ptr, png_ptr->gamma_16_to_1);\r
+   }\r
+#endif\r
+#endif\r
+#ifdef PNG_TIME_RFC1123_SUPPORTED\r
+   png_free(png_ptr, png_ptr->time_buffer);\r
+#endif\r
+\r
+   inflateEnd(&png_ptr->zstream);\r
+#ifdef PNG_PROGRESSIVE_READ_SUPPORTED\r
+   png_free(png_ptr, png_ptr->save_buffer);\r
+#endif\r
+\r
+#ifdef PNG_PROGRESSIVE_READ_SUPPORTED\r
+#ifdef PNG_TEXT_SUPPORTED\r
+   png_free(png_ptr, png_ptr->current_text);\r
+#endif /* PNG_TEXT_SUPPORTED */\r
+#endif /* PNG_PROGRESSIVE_READ_SUPPORTED */\r
+\r
+   /* Save the important info out of the png_struct, in case it is\r
+    * being used again.\r
+    */\r
+#ifdef PNG_SETJMP_SUPPORTED\r
+   png_memcpy(tmp_jmp, png_ptr->jmpbuf, png_sizeof(jmp_buf));\r
+#endif\r
+\r
+   error_fn = png_ptr->error_fn;\r
+   warning_fn = png_ptr->warning_fn;\r
+   error_ptr = png_ptr->error_ptr;\r
+#ifdef PNG_USER_MEM_SUPPORTED\r
+   free_fn = png_ptr->free_fn;\r
+#endif\r
+\r
+   png_memset(png_ptr, 0, png_sizeof(png_struct));\r
+\r
+   png_ptr->error_fn = error_fn;\r
+   png_ptr->warning_fn = warning_fn;\r
+   png_ptr->error_ptr = error_ptr;\r
+#ifdef PNG_USER_MEM_SUPPORTED\r
+   png_ptr->free_fn = free_fn;\r
+#endif\r
+\r
+#ifdef PNG_SETJMP_SUPPORTED\r
+   png_memcpy(png_ptr->jmpbuf, tmp_jmp, png_sizeof(jmp_buf));\r
+#endif\r
+\r
+}\r
+\r
+void PNGAPI\r
+png_set_read_status_fn(png_structp png_ptr, png_read_status_ptr read_row_fn)\r
+{\r
+   if (png_ptr == NULL)\r
+      return;\r
+   png_ptr->read_row_fn = read_row_fn;\r
+}\r
+\r
+\r
+#ifdef PNG_SEQUENTIAL_READ_SUPPORTED\r
+#ifdef PNG_INFO_IMAGE_SUPPORTED\r
+void PNGAPI\r
+png_read_png(png_structp png_ptr, png_infop info_ptr,\r
+                           int transforms,\r
+                           voidp params)\r
+{\r
+   int row;\r
+\r
+   if (png_ptr == NULL)\r
+      return;\r
+\r
+   /* png_read_info() gives us all of the information from the\r
+    * PNG file before the first IDAT (image data chunk).\r
+    */\r
+   png_read_info(png_ptr, info_ptr);\r
+   if (info_ptr->height > PNG_UINT_32_MAX/png_sizeof(png_bytep))\r
+      png_error(png_ptr, "Image is too high to process with png_read_png()");\r
+\r
+   /* -------------- image transformations start here ------------------- */\r
+\r
+#ifdef PNG_READ_16_TO_8_SUPPORTED\r
+   /* Tell libpng to strip 16 bit/color files down to 8 bits per color.\r
+    */\r
+   if (transforms & PNG_TRANSFORM_STRIP_16)\r
+      png_set_strip_16(png_ptr);\r
+#endif\r
+\r
+#ifdef PNG_READ_STRIP_ALPHA_SUPPORTED\r
+   /* Strip alpha bytes from the input data without combining with\r
+    * the background (not recommended).\r
+    */\r
+   if (transforms & PNG_TRANSFORM_STRIP_ALPHA)\r
+      png_set_strip_alpha(png_ptr);\r
+#endif\r
+\r
+#if defined(PNG_READ_PACK_SUPPORTED) && !defined(PNG_READ_EXPAND_SUPPORTED)\r
+   /* Extract multiple pixels with bit depths of 1, 2, or 4 from a single\r
+    * byte into separate bytes (useful for paletted and grayscale images).\r
+    */\r
+   if (transforms & PNG_TRANSFORM_PACKING)\r
+      png_set_packing(png_ptr);\r
+#endif\r
+\r
+#ifdef PNG_READ_PACKSWAP_SUPPORTED\r
+   /* Change the order of packed pixels to least significant bit first\r
+    * (not useful if you are using png_set_packing).\r
+    */\r
+   if (transforms & PNG_TRANSFORM_PACKSWAP)\r
+      png_set_packswap(png_ptr);\r
+#endif\r
+\r
+#ifdef PNG_READ_EXPAND_SUPPORTED\r
+   /* Expand paletted colors into true RGB triplets\r
+    * Expand grayscale images to full 8 bits from 1, 2, or 4 bits/pixel\r
+    * Expand paletted or RGB images with transparency to full alpha\r
+    * channels so the data will be available as RGBA quartets.\r
+    */\r
+   if (transforms & PNG_TRANSFORM_EXPAND)\r
+      if ((png_ptr->bit_depth < 8) ||\r
+          (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) ||\r
+          (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)))\r
+         png_set_expand(png_ptr);\r
+#endif\r
+\r
+   /* We don't handle background color or gamma transformation or quantizing.\r
+    */\r
+\r
+#ifdef PNG_READ_INVERT_SUPPORTED\r
+   /* Invert monochrome files to have 0 as white and 1 as black\r
+    */\r
+   if (transforms & PNG_TRANSFORM_INVERT_MONO)\r
+      png_set_invert_mono(png_ptr);\r
+#endif\r
+\r
+#ifdef PNG_READ_SHIFT_SUPPORTED\r
+   /* If you want to shift the pixel values from the range [0,255] or\r
+    * [0,65535] to the original [0,7] or [0,31], or whatever range the\r
+    * colors were originally in:\r
+    */\r
+   if ((transforms & PNG_TRANSFORM_SHIFT)\r
+       && png_get_valid(png_ptr, info_ptr, PNG_INFO_sBIT))\r
+   {\r
+      png_color_8p sig_bit;\r
+\r
+      png_get_sBIT(png_ptr, info_ptr, &sig_bit);\r
+      png_set_shift(png_ptr, sig_bit);\r
+   }\r
+#endif\r
+\r
+#ifdef PNG_READ_BGR_SUPPORTED\r
+   /* Flip the RGB pixels to BGR (or RGBA to BGRA)\r
+    */\r
+   if (transforms & PNG_TRANSFORM_BGR)\r
+      png_set_bgr(png_ptr);\r
+#endif\r
+\r
+#ifdef PNG_READ_SWAP_ALPHA_SUPPORTED\r
+   /* Swap the RGBA or GA data to ARGB or AG (or BGRA to ABGR)\r
+    */\r
+   if (transforms & PNG_TRANSFORM_SWAP_ALPHA)\r
+       png_set_swap_alpha(png_ptr);\r
+#endif\r
+\r
+#ifdef PNG_READ_SWAP_SUPPORTED\r
+   /* Swap bytes of 16 bit files to least significant byte first\r
+    */\r
+   if (transforms & PNG_TRANSFORM_SWAP_ENDIAN)\r
+      png_set_swap(png_ptr);\r
+#endif\r
+\r
+/* Added at libpng-1.2.41 */\r
+#ifdef PNG_READ_INVERT_ALPHA_SUPPORTED\r
+   /* Invert the alpha channel from opacity to transparency\r
+    */\r
+   if (transforms & PNG_TRANSFORM_INVERT_ALPHA)\r
+       png_set_invert_alpha(png_ptr);\r
+#endif\r
+\r
+/* Added at libpng-1.2.41 */\r
+#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED\r
+   /* Expand grayscale image to RGB\r
+    */\r
+   if (transforms & PNG_TRANSFORM_GRAY_TO_RGB)\r
+       png_set_gray_to_rgb(png_ptr);\r
+#endif\r
+\r
+   /* We don't handle adding filler bytes */\r
+\r
+   /* Optional call to gamma correct and add the background to the palette\r
+    * and update info structure.  REQUIRED if you are expecting libpng to\r
+    * update the palette for you (i.e., you selected such a transform above).\r
+    */\r
+   png_read_update_info(png_ptr, info_ptr);\r
+\r
+   /* -------------- image transformations end here ------------------- */\r
+\r
+   png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0);\r
+   if (info_ptr->row_pointers == NULL)\r
+   {\r
+    png_uint_32 iptr;\r
+\r
+      info_ptr->row_pointers = (png_bytepp)png_malloc(png_ptr,\r
+         info_ptr->height * png_sizeof(png_bytep));\r
+      for (iptr=0; iptr<info_ptr->height; iptr++)\r
+         info_ptr->row_pointers[iptr] = NULL;\r
+\r
+      info_ptr->free_me |= PNG_FREE_ROWS;\r
+\r
+      for (row = 0; row < (int)info_ptr->height; row++)\r
+         info_ptr->row_pointers[row] = (png_bytep)png_malloc(png_ptr,\r
+            png_get_rowbytes(png_ptr, info_ptr));\r
+   }\r
+\r
+   png_read_image(png_ptr, info_ptr->row_pointers);\r
+   info_ptr->valid |= PNG_INFO_IDAT;\r
+\r
+   /* Read rest of file, and get additional chunks in info_ptr - REQUIRED */\r
+   png_read_end(png_ptr, info_ptr);\r
+\r
+   transforms = transforms; /* Quiet compiler warnings */\r
+   params = params;\r
+\r
+}\r
+#endif /* PNG_INFO_IMAGE_SUPPORTED */\r
+#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */\r
+#endif /* PNG_READ_SUPPORTED */\r
index 7d2522f..990fe3f 100644 (file)
-
-/* pngrio.c - functions for data input
- *
- * Last changed in libpng 1.2.13 November 13, 2006
- * For conditions of distribution and use, see copyright notice in png.h
- * Copyright (c) 1998-2006 Glenn Randers-Pehrson
- * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
- * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
- *
- * This file provides a location for all input.  Users who need
- * special handling are expected to write a function that has the same
- * arguments as this and performs a similar function, but that possibly
- * has a different input method.  Note that you shouldn't change this
- * function, but rather write a replacement function and then make
- * libpng use it at run time with png_set_read_fn(...).
- */
-
-#define PNG_INTERNAL
-#include "png.h"
-
-#if defined(PNG_READ_SUPPORTED)
-
-/* Read the data from whatever input you are using.  The default routine
-   reads from a file pointer.  Note that this routine sometimes gets called
-   with very small lengths, so you should implement some kind of simple
-   buffering if you are using unbuffered reads.  This should never be asked
-   to read more then 64K on a 16 bit machine. */
-void /* PRIVATE */
-png_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
-{
-   png_debug1(4,"reading %d bytes\n", (int)length);
-   if (png_ptr->read_data_fn != NULL)
-      (*(png_ptr->read_data_fn))(png_ptr, data, length);
-   else
-      png_error(png_ptr, "Call to NULL read function");
-}
-
-#if !defined(PNG_NO_STDIO)
-/* This is the function that does the actual reading of data.  If you are
-   not reading from a standard C stream, you should create a replacement
-   read_data function and use it at run time with png_set_read_fn(), rather
-   than changing the library. */
-#ifndef USE_FAR_KEYWORD
-void PNGAPI
-png_default_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
-{
-   png_size_t check;
-
-   if(png_ptr == NULL) return;
-   /* fread() returns 0 on error, so it is OK to store this in a png_size_t
-    * instead of an int, which is what fread() actually returns.
-    */
-#if defined(_WIN32_WCE)
-   if ( !ReadFile((HANDLE)(png_ptr->io_ptr), data, length, &check, NULL) )
-      check = 0;
-#else
-   check = (png_size_t)fread(data, (png_size_t)1, length,
-      (png_FILE_p)png_ptr->io_ptr);
-#endif
-
-   if (check != length)
-      png_error(png_ptr, "Read Error");
-}
-#else
-/* this is the model-independent version. Since the standard I/O library
-   can't handle far buffers in the medium and small models, we have to copy
-   the data.
-*/
-
-#define NEAR_BUF_SIZE 1024
-#define MIN(a,b) (a <= b ? a : b)
-
-static void PNGAPI
-png_default_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
-{
-   int check;
-   png_byte *n_data;
-   png_FILE_p io_ptr;
-
-   if(png_ptr == NULL) return;
-   /* Check if data really is near. If so, use usual code. */
-   n_data = (png_byte *)CVT_PTR_NOCHECK(data);
-   io_ptr = (png_FILE_p)CVT_PTR(png_ptr->io_ptr);
-   if ((png_bytep)n_data == data)
-   {
-#if defined(_WIN32_WCE)
-      if ( !ReadFile((HANDLE)(png_ptr->io_ptr), data, length, &check, NULL) )
-         check = 0;
-#else
-      check = fread(n_data, 1, length, io_ptr);
-#endif
-   }
-   else
-   {
-      png_byte buf[NEAR_BUF_SIZE];
-      png_size_t read, remaining, err;
-      check = 0;
-      remaining = length;
-      do
-      {
-         read = MIN(NEAR_BUF_SIZE, remaining);
-#if defined(_WIN32_WCE)
-         if ( !ReadFile((HANDLE)(io_ptr), buf, read, &err, NULL) )
-            err = 0;
-#else
-         err = fread(buf, (png_size_t)1, read, io_ptr);
-#endif
-         png_memcpy(data, buf, read); /* copy far buffer to near buffer */
-         if(err != read)
-            break;
-         else
-            check += err;
-         data += read;
-         remaining -= read;
-      }
-      while (remaining != 0);
-   }
-   if ((png_uint_32)check != (png_uint_32)length)
-      png_error(png_ptr, "read Error");
-}
-#endif
-#endif
-
-/* This function allows the application to supply a new input function
-   for libpng if standard C streams aren't being used.
-
-   This function takes as its arguments:
-   png_ptr      - pointer to a png input data structure
-   io_ptr       - pointer to user supplied structure containing info about
-                  the input functions.  May be NULL.
-   read_data_fn - pointer to a new input function that takes as its
-                  arguments a pointer to a png_struct, a pointer to
-                  a location where input data can be stored, and a 32-bit
-                  unsigned int that is the number of bytes to be read.
-                  To exit and output any fatal error messages the new write
-                  function should call png_error(png_ptr, "Error msg"). */
-void PNGAPI
-png_set_read_fn(png_structp png_ptr, png_voidp io_ptr,
-   png_rw_ptr read_data_fn)
-{
-   if(png_ptr == NULL) return;
-   png_ptr->io_ptr = io_ptr;
-
-#if !defined(PNG_NO_STDIO)
-   if (read_data_fn != NULL)
-      png_ptr->read_data_fn = read_data_fn;
-   else
-      png_ptr->read_data_fn = png_default_read_data;
-#else
-   png_ptr->read_data_fn = read_data_fn;
-#endif
-
-   /* It is an error to write to a read device */
-   if (png_ptr->write_data_fn != NULL)
-   {
-      png_ptr->write_data_fn = NULL;
-      png_warning(png_ptr,
-         "It's an error to set both read_data_fn and write_data_fn in the ");
-      png_warning(png_ptr,
-         "same structure.  Resetting write_data_fn to NULL.");
-   }
-
-#if defined(PNG_WRITE_FLUSH_SUPPORTED)
-   png_ptr->output_flush_fn = NULL;
-#endif
-}
-#endif /* PNG_READ_SUPPORTED */
+\r
+/* pngrio.c - functions for data input\r
+ *\r
+ * Last changed in libpng 1.4.1 [February 25, 2010]\r
+ * Copyright (c) 1998-2010 Glenn Randers-Pehrson\r
+ * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)\r
+ * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)\r
+ *\r
+ * This code is released under the libpng license.\r
+ * For conditions of distribution and use, see the disclaimer\r
+ * and license in png.h\r
+ *\r
+ * This file provides a location for all input.  Users who need\r
+ * special handling are expected to write a function that has the same\r
+ * arguments as this and performs a similar function, but that possibly\r
+ * has a different input method.  Note that you shouldn't change this\r
+ * function, but rather write a replacement function and then make\r
+ * libpng use it at run time with png_set_read_fn(...).\r
+ */\r
+\r
+#define PNG_NO_PEDANTIC_WARNINGS\r
+#include "png.h"\r
+#ifdef PNG_READ_SUPPORTED\r
+#include "pngpriv.h"\r
+\r
+/* Read the data from whatever input you are using.  The default routine\r
+ * reads from a file pointer.  Note that this routine sometimes gets called\r
+ * with very small lengths, so you should implement some kind of simple\r
+ * buffering if you are using unbuffered reads.  This should never be asked\r
+ * to read more then 64K on a 16 bit machine.\r
+ */\r
+void /* PRIVATE */\r
+png_read_data(png_structp png_ptr, png_bytep data, png_size_t length)\r
+{\r
+   png_debug1(4, "reading %d bytes", (int)length);\r
\r
+   if (png_ptr->read_data_fn != NULL)\r
+      (*(png_ptr->read_data_fn))(png_ptr, data, length);\r
+   else\r
+      png_error(png_ptr, "Call to NULL read function");\r
+}\r
+\r
+#ifdef PNG_STDIO_SUPPORTED\r
+/* This is the function that does the actual reading of data.  If you are\r
+ * not reading from a standard C stream, you should create a replacement\r
+ * read_data function and use it at run time with png_set_read_fn(), rather\r
+ * than changing the library.\r
+ */\r
+#ifndef USE_FAR_KEYWORD\r
+void PNGAPI\r
+png_default_read_data(png_structp png_ptr, png_bytep data, png_size_t length)\r
+{\r
+   png_size_t check;\r
+\r
+   if (png_ptr == NULL)\r
+      return;\r
+   /* fread() returns 0 on error, so it is OK to store this in a png_size_t\r
+    * instead of an int, which is what fread() actually returns.\r
+    */\r
+   check = fread(data, 1, length, (png_FILE_p)png_ptr->io_ptr);\r
+\r
+   if (check != length)\r
+      png_error(png_ptr, "Read Error");\r
+}\r
+#else\r
+/* This is the model-independent version. Since the standard I/O library\r
+   can't handle far buffers in the medium and small models, we have to copy\r
+   the data.\r
+*/\r
+\r
+#define NEAR_BUF_SIZE 1024\r
+#define MIN(a,b) (a <= b ? a : b)\r
+\r
+static void PNGAPI\r
+png_default_read_data(png_structp png_ptr, png_bytep data, png_size_t length)\r
+{\r
+   png_size_t check;\r
+   png_byte *n_data;\r
+   png_FILE_p io_ptr;\r
+\r
+   if (png_ptr == NULL)\r
+      return;\r
+   /* Check if data really is near. If so, use usual code. */\r
+   n_data = (png_byte *)CVT_PTR_NOCHECK(data);\r
+   io_ptr = (png_FILE_p)CVT_PTR(png_ptr->io_ptr);\r
+   if ((png_bytep)n_data == data)\r
+   {\r
+      check = fread(n_data, 1, length, io_ptr);\r
+   }\r
+   else\r
+   {\r
+      png_byte buf[NEAR_BUF_SIZE];\r
+      png_size_t read, remaining, err;\r
+      check = 0;\r
+      remaining = length;\r
+      do\r
+      {\r
+         read = MIN(NEAR_BUF_SIZE, remaining);\r
+         err = fread(buf, 1, read, io_ptr);\r
+         png_memcpy(data, buf, read); /* copy far buffer to near buffer */\r
+         if (err != read)\r
+            break;\r
+         else\r
+            check += err;\r
+         data += read;\r
+         remaining -= read;\r
+      }\r
+      while (remaining != 0);\r
+   }\r
+   if ((png_uint_32)check != (png_uint_32)length)\r
+      png_error(png_ptr, "read Error");\r
+}\r
+#endif\r
+#endif\r
+\r
+/* This function allows the application to supply a new input function\r
+ * for libpng if standard C streams aren't being used.\r
+ *\r
+ * This function takes as its arguments:\r
+ * png_ptr      - pointer to a png input data structure\r
+ * io_ptr       - pointer to user supplied structure containing info about\r
+ *                the input functions.  May be NULL.\r
+ * read_data_fn - pointer to a new input function that takes as its\r
+ *                arguments a pointer to a png_struct, a pointer to\r
+ *                a location where input data can be stored, and a 32-bit\r
+ *                unsigned int that is the number of bytes to be read.\r
+ *                To exit and output any fatal error messages the new write\r
+ *                function should call png_error(png_ptr, "Error msg").\r
+ *                May be NULL, in which case libpng's default function will\r
+ *                be used.\r
+ */\r
+void PNGAPI\r
+png_set_read_fn(png_structp png_ptr, png_voidp io_ptr,\r
+   png_rw_ptr read_data_fn)\r
+{\r
+   if (png_ptr == NULL)\r
+      return;\r
+   png_ptr->io_ptr = io_ptr;\r
+\r
+#ifdef PNG_STDIO_SUPPORTED\r
+   if (read_data_fn != NULL)\r
+      png_ptr->read_data_fn = read_data_fn;\r
+   else\r
+      png_ptr->read_data_fn = png_default_read_data;\r
+#else\r
+   png_ptr->read_data_fn = read_data_fn;\r
+#endif\r
+\r
+   /* It is an error to write to a read device */\r
+   if (png_ptr->write_data_fn != NULL)\r
+   {\r
+      png_ptr->write_data_fn = NULL;\r
+      png_warning(png_ptr,\r
+         "It's an error to set both read_data_fn and write_data_fn in the ");\r
+      png_warning(png_ptr,\r
+         "same structure.  Resetting write_data_fn to NULL");\r
+   }\r
+\r
+#ifdef PNG_WRITE_FLUSH_SUPPORTED\r
+   png_ptr->output_flush_fn = NULL;\r
+#endif\r
+}\r
+#endif /* PNG_READ_SUPPORTED */\r
index 873b22c..63ac38b 100644 (file)
-
-/* pngrtran.c - transforms the data in a row for PNG readers
- *
- * Last changed in libpng 1.2.27 [April 29, 2008]
- * For conditions of distribution and use, see copyright notice in png.h
- * Copyright (c) 1998-2008 Glenn Randers-Pehrson
- * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
- * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
- *
- * This file contains functions optionally called by an application
- * in order to tell libpng how to handle data when reading a PNG.
- * Transformations that are used in both reading and writing are
- * in pngtrans.c.
- */
-
-#define PNG_INTERNAL
-#include "png.h"
-
-#if defined(PNG_READ_SUPPORTED)
-
-/* Set the action on getting a CRC error for an ancillary or critical chunk. */
-void PNGAPI
-png_set_crc_action(png_structp png_ptr, int crit_action, int ancil_action)
-{
-   png_debug(1, "in png_set_crc_action\n");
-   /* Tell libpng how we react to CRC errors in critical chunks */
-   if(png_ptr == NULL) return;
-   switch (crit_action)
-   {
-      case PNG_CRC_NO_CHANGE:                        /* leave setting as is */
-         break;
-      case PNG_CRC_WARN_USE:                               /* warn/use data */
-         png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK;
-         png_ptr->flags |= PNG_FLAG_CRC_CRITICAL_USE;
-         break;
-      case PNG_CRC_QUIET_USE:                             /* quiet/use data */
-         png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK;
-         png_ptr->flags |= PNG_FLAG_CRC_CRITICAL_USE |
-                           PNG_FLAG_CRC_CRITICAL_IGNORE;
-         break;
-      case PNG_CRC_WARN_DISCARD:    /* not a valid action for critical data */
-         png_warning(png_ptr, "Can't discard critical data on CRC error.");
-      case PNG_CRC_ERROR_QUIT:                                /* error/quit */
-      case PNG_CRC_DEFAULT:
-      default:
-         png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK;
-         break;
-   }
-
-   switch (ancil_action)
-   {
-      case PNG_CRC_NO_CHANGE:                       /* leave setting as is */
-         break;
-      case PNG_CRC_WARN_USE:                              /* warn/use data */
-         png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK;
-         png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_USE;
-         break;
-      case PNG_CRC_QUIET_USE:                            /* quiet/use data */
-         png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK;
-         png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_USE |
-                           PNG_FLAG_CRC_ANCILLARY_NOWARN;
-         break;
-      case PNG_CRC_ERROR_QUIT:                               /* error/quit */
-         png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK;
-         png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_NOWARN;
-         break;
-      case PNG_CRC_WARN_DISCARD:                      /* warn/discard data */
-      case PNG_CRC_DEFAULT:
-      default:
-         png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK;
-         break;
-   }
-}
-
-#if defined(PNG_READ_BACKGROUND_SUPPORTED) && \
-    defined(PNG_FLOATING_POINT_SUPPORTED)
-/* handle alpha and tRNS via a background color */
-void PNGAPI
-png_set_background(png_structp png_ptr,
-   png_color_16p background_color, int background_gamma_code,
-   int need_expand, double background_gamma)
-{
-   png_debug(1, "in png_set_background\n");
-   if(png_ptr == NULL) return;
-   if (background_gamma_code == PNG_BACKGROUND_GAMMA_UNKNOWN)
-   {
-      png_warning(png_ptr, "Application must supply a known background gamma");
-      return;
-   }
-
-   png_ptr->transformations |= PNG_BACKGROUND;
-   png_memcpy(&(png_ptr->background), background_color,
-      png_sizeof(png_color_16));
-   png_ptr->background_gamma = (float)background_gamma;
-   png_ptr->background_gamma_type = (png_byte)(background_gamma_code);
-   png_ptr->transformations |= (need_expand ? PNG_BACKGROUND_EXPAND : 0);
-}
-#endif
-
-#if defined(PNG_READ_16_TO_8_SUPPORTED)
-/* strip 16 bit depth files to 8 bit depth */
-void PNGAPI
-png_set_strip_16(png_structp png_ptr)
-{
-   png_debug(1, "in png_set_strip_16\n");
-   if(png_ptr == NULL) return;
-   png_ptr->transformations |= PNG_16_TO_8;
-}
-#endif
-
-#if defined(PNG_READ_STRIP_ALPHA_SUPPORTED)
-void PNGAPI
-png_set_strip_alpha(png_structp png_ptr)
-{
-   png_debug(1, "in png_set_strip_alpha\n");
-   if(png_ptr == NULL) return;
-   png_ptr->flags |= PNG_FLAG_STRIP_ALPHA;
-}
-#endif
-
-#if defined(PNG_READ_DITHER_SUPPORTED)
-/* Dither file to 8 bit.  Supply a palette, the current number
- * of elements in the palette, the maximum number of elements
- * allowed, and a histogram if possible.  If the current number
- * of colors is greater then the maximum number, the palette will be
- * modified to fit in the maximum number.  "full_dither" indicates
- * whether we need a dithering cube set up for RGB images, or if we
- * simply are reducing the number of colors in a paletted image.
- */
-
-typedef struct png_dsort_struct
-{
-   struct png_dsort_struct FAR * next;
-   png_byte left;
-   png_byte right;
-} png_dsort;
-typedef png_dsort FAR *       png_dsortp;
-typedef png_dsort FAR * FAR * png_dsortpp;
-
-void PNGAPI
-png_set_dither(png_structp png_ptr, png_colorp palette,
-   int num_palette, int maximum_colors, png_uint_16p histogram,
-   int full_dither)
-{
-   png_debug(1, "in png_set_dither\n");
-   if(png_ptr == NULL) return;
-   png_ptr->transformations |= PNG_DITHER;
-
-   if (!full_dither)
-   {
-      int i;
-
-      png_ptr->dither_index = (png_bytep)png_malloc(png_ptr,
-         (png_uint_32)(num_palette * png_sizeof (png_byte)));
-      for (i = 0; i < num_palette; i++)
-         png_ptr->dither_index[i] = (png_byte)i;
-   }
-
-   if (num_palette > maximum_colors)
-   {
-      if (histogram != NULL)
-      {
-         /* This is easy enough, just throw out the least used colors.
-            Perhaps not the best solution, but good enough. */
-
-         int i;
-
-         /* initialize an array to sort colors */
-         png_ptr->dither_sort = (png_bytep)png_malloc(png_ptr,
-            (png_uint_32)(num_palette * png_sizeof (png_byte)));
-
-         /* initialize the dither_sort array */
-         for (i = 0; i < num_palette; i++)
-            png_ptr->dither_sort[i] = (png_byte)i;
-
-         /* Find the least used palette entries by starting a
-            bubble sort, and running it until we have sorted
-            out enough colors.  Note that we don't care about
-            sorting all the colors, just finding which are
-            least used. */
-
-         for (i = num_palette - 1; i >= maximum_colors; i--)
-         {
-            int done; /* to stop early if the list is pre-sorted */
-            int j;
-
-            done = 1;
-            for (j = 0; j < i; j++)
-            {
-               if (histogram[png_ptr->dither_sort[j]]
-                   < histogram[png_ptr->dither_sort[j + 1]])
-               {
-                  png_byte t;
-
-                  t = png_ptr->dither_sort[j];
-                  png_ptr->dither_sort[j] = png_ptr->dither_sort[j + 1];
-                  png_ptr->dither_sort[j + 1] = t;
-                  done = 0;
-               }
-            }
-            if (done)
-               break;
-         }
-
-         /* swap the palette around, and set up a table, if necessary */
-         if (full_dither)
-         {
-            int j = num_palette;
-
-            /* put all the useful colors within the max, but don't
-               move the others */
-            for (i = 0; i < maximum_colors; i++)
-            {
-               if ((int)png_ptr->dither_sort[i] >= maximum_colors)
-               {
-                  do
-                     j--;
-                  while ((int)png_ptr->dither_sort[j] >= maximum_colors);
-                  palette[i] = palette[j];
-               }
-            }
-         }
-         else
-         {
-            int j = num_palette;
-
-            /* move all the used colors inside the max limit, and
-               develop a translation table */
-            for (i = 0; i < maximum_colors; i++)
-            {
-               /* only move the colors we need to */
-               if ((int)png_ptr->dither_sort[i] >= maximum_colors)
-               {
-                  png_color tmp_color;
-
-                  do
-                     j--;
-                  while ((int)png_ptr->dither_sort[j] >= maximum_colors);
-
-                  tmp_color = palette[j];
-                  palette[j] = palette[i];
-                  palette[i] = tmp_color;
-                  /* indicate where the color went */
-                  png_ptr->dither_index[j] = (png_byte)i;
-                  png_ptr->dither_index[i] = (png_byte)j;
-               }
-            }
-
-            /* find closest color for those colors we are not using */
-            for (i = 0; i < num_palette; i++)
-            {
-               if ((int)png_ptr->dither_index[i] >= maximum_colors)
-               {
-                  int min_d, k, min_k, d_index;
-
-                  /* find the closest color to one we threw out */
-                  d_index = png_ptr->dither_index[i];
-                  min_d = PNG_COLOR_DIST(palette[d_index], palette[0]);
-                  for (k = 1, min_k = 0; k < maximum_colors; k++)
-                  {
-                     int d;
-
-                     d = PNG_COLOR_DIST(palette[d_index], palette[k]);
-
-                     if (d < min_d)
-                     {
-                        min_d = d;
-                        min_k = k;
-                     }
-                  }
-                  /* point to closest color */
-                  png_ptr->dither_index[i] = (png_byte)min_k;
-               }
-            }
-         }
-         png_free(png_ptr, png_ptr->dither_sort);
-         png_ptr->dither_sort=NULL;
-      }
-      else
-      {
-         /* This is much harder to do simply (and quickly).  Perhaps
-            we need to go through a median cut routine, but those
-            don't always behave themselves with only a few colors
-            as input.  So we will just find the closest two colors,
-            and throw out one of them (chosen somewhat randomly).
-            [We don't understand this at all, so if someone wants to
-             work on improving it, be our guest - AED, GRP]
-            */
-         int i;
-         int max_d;
-         int num_new_palette;
-         png_dsortp t;
-         png_dsortpp hash;
-
-         t=NULL;
-
-         /* initialize palette index arrays */
-         png_ptr->index_to_palette = (png_bytep)png_malloc(png_ptr,
-            (png_uint_32)(num_palette * png_sizeof (png_byte)));
-         png_ptr->palette_to_index = (png_bytep)png_malloc(png_ptr,
-            (png_uint_32)(num_palette * png_sizeof (png_byte)));
-
-         /* initialize the sort array */
-         for (i = 0; i < num_palette; i++)
-         {
-            png_ptr->index_to_palette[i] = (png_byte)i;
-            png_ptr->palette_to_index[i] = (png_byte)i;
-         }
-
-         hash = (png_dsortpp)png_malloc(png_ptr, (png_uint_32)(769 *
-            png_sizeof (png_dsortp)));
-         for (i = 0; i < 769; i++)
-            hash[i] = NULL;
-/*         png_memset(hash, 0, 769 * png_sizeof (png_dsortp)); */
-
-         num_new_palette = num_palette;
-
-         /* initial wild guess at how far apart the farthest pixel
-            pair we will be eliminating will be.  Larger
-            numbers mean more areas will be allocated, Smaller
-            numbers run the risk of not saving enough data, and
-            having to do this all over again.
-
-            I have not done extensive checking on this number.
-            */
-         max_d = 96;
-
-         while (num_new_palette > maximum_colors)
-         {
-            for (i = 0; i < num_new_palette - 1; i++)
-            {
-               int j;
-
-               for (j = i + 1; j < num_new_palette; j++)
-               {
-                  int d;
-
-                  d = PNG_COLOR_DIST(palette[i], palette[j]);
-
-                  if (d <= max_d)
-                  {
-
-                     t = (png_dsortp)png_malloc_warn(png_ptr,
-                         (png_uint_32)(png_sizeof(png_dsort)));
-                     if (t == NULL)
-                         break;
-                     t->next = hash[d];
-                     t->left = (png_byte)i;
-                     t->right = (png_byte)j;
-                     hash[d] = t;
-                  }
-               }
-               if (t == NULL)
-                  break;
-            }
-
-            if (t != NULL)
-            for (i = 0; i <= max_d; i++)
-            {
-               if (hash[i] != NULL)
-               {
-                  png_dsortp p;
-
-                  for (p = hash[i]; p; p = p->next)
-                  {
-                     if ((int)png_ptr->index_to_palette[p->left]
-                        < num_new_palette &&
-                        (int)png_ptr->index_to_palette[p->right]
-                        < num_new_palette)
-                     {
-                        int j, next_j;
-
-                        if (num_new_palette & 0x01)
-                        {
-                           j = p->left;
-                           next_j = p->right;
-                        }
-                        else
-                        {
-                           j = p->right;
-                           next_j = p->left;
-                        }
-
-                        num_new_palette--;
-                        palette[png_ptr->index_to_palette[j]]
-                          = palette[num_new_palette];
-                        if (!full_dither)
-                        {
-                           int k;
-
-                           for (k = 0; k < num_palette; k++)
-                           {
-                              if (png_ptr->dither_index[k] ==
-                                 png_ptr->index_to_palette[j])
-                                 png_ptr->dither_index[k] =
-                                    png_ptr->index_to_palette[next_j];
-                              if ((int)png_ptr->dither_index[k] ==
-                                 num_new_palette)
-                                 png_ptr->dither_index[k] =
-                                    png_ptr->index_to_palette[j];
-                           }
-                        }
-
-                        png_ptr->index_to_palette[png_ptr->palette_to_index
-                           [num_new_palette]] = png_ptr->index_to_palette[j];
-                        png_ptr->palette_to_index[png_ptr->index_to_palette[j]]
-                           = png_ptr->palette_to_index[num_new_palette];
-
-                        png_ptr->index_to_palette[j] = (png_byte)num_new_palette;
-                        png_ptr->palette_to_index[num_new_palette] = (png_byte)j;
-                     }
-                     if (num_new_palette <= maximum_colors)
-                        break;
-                  }
-                  if (num_new_palette <= maximum_colors)
-                     break;
-               }
-            }
-
-            for (i = 0; i < 769; i++)
-            {
-               if (hash[i] != NULL)
-               {
-                  png_dsortp p = hash[i];
-                  while (p)
-                  {
-                     t = p->next;
-                     png_free(png_ptr, p);
-                     p = t;
-                  }
-               }
-               hash[i] = 0;
-            }
-            max_d += 96;
-         }
-         png_free(png_ptr, hash);
-         png_free(png_ptr, png_ptr->palette_to_index);
-         png_free(png_ptr, png_ptr->index_to_palette);
-         png_ptr->palette_to_index=NULL;
-         png_ptr->index_to_palette=NULL;
-      }
-      num_palette = maximum_colors;
-   }
-   if (png_ptr->palette == NULL)
-   {
-      png_ptr->palette = palette;
-   }
-   png_ptr->num_palette = (png_uint_16)num_palette;
-
-   if (full_dither)
-   {
-      int i;
-      png_bytep distance;
-      int total_bits = PNG_DITHER_RED_BITS + PNG_DITHER_GREEN_BITS +
-         PNG_DITHER_BLUE_BITS;
-      int num_red = (1 << PNG_DITHER_RED_BITS);
-      int num_green = (1 << PNG_DITHER_GREEN_BITS);
-      int num_blue = (1 << PNG_DITHER_BLUE_BITS);
-      png_size_t num_entries = ((png_size_t)1 << total_bits);
-
-      png_ptr->palette_lookup = (png_bytep )png_malloc(png_ptr,
-         (png_uint_32)(num_entries * png_sizeof (png_byte)));
-
-      png_memset(png_ptr->palette_lookup, 0, num_entries *
-         png_sizeof (png_byte));
-
-      distance = (png_bytep)png_malloc(png_ptr, (png_uint_32)(num_entries *
-         png_sizeof(png_byte)));
-
-      png_memset(distance, 0xff, num_entries * png_sizeof(png_byte));
-
-      for (i = 0; i < num_palette; i++)
-      {
-         int ir, ig, ib;
-         int r = (palette[i].red >> (8 - PNG_DITHER_RED_BITS));
-         int g = (palette[i].green >> (8 - PNG_DITHER_GREEN_BITS));
-         int b = (palette[i].blue >> (8 - PNG_DITHER_BLUE_BITS));
-
-         for (ir = 0; ir < num_red; ir++)
-         {
-            /* int dr = abs(ir - r); */
-            int dr = ((ir > r) ? ir - r : r - ir);
-            int index_r = (ir << (PNG_DITHER_BLUE_BITS + PNG_DITHER_GREEN_BITS));
-
-            for (ig = 0; ig < num_green; ig++)
-            {
-               /* int dg = abs(ig - g); */
-               int dg = ((ig > g) ? ig - g : g - ig);
-               int dt = dr + dg;
-               int dm = ((dr > dg) ? dr : dg);
-               int index_g = index_r | (ig << PNG_DITHER_BLUE_BITS);
-
-               for (ib = 0; ib < num_blue; ib++)
-               {
-                  int d_index = index_g | ib;
-                  /* int db = abs(ib - b); */
-                  int db = ((ib > b) ? ib - b : b - ib);
-                  int dmax = ((dm > db) ? dm : db);
-                  int d = dmax + dt + db;
-
-                  if (d < (int)distance[d_index])
-                  {
-                     distance[d_index] = (png_byte)d;
-                     png_ptr->palette_lookup[d_index] = (png_byte)i;
-                  }
-               }
-            }
-         }
-      }
-
-      png_free(png_ptr, distance);
-   }
-}
-#endif
-
-#if defined(PNG_READ_GAMMA_SUPPORTED) && defined(PNG_FLOATING_POINT_SUPPORTED)
-/* Transform the image from the file_gamma to the screen_gamma.  We
- * only do transformations on images where the file_gamma and screen_gamma
- * are not close reciprocals, otherwise it slows things down slightly, and
- * also needlessly introduces small errors.
- *
- * We will turn off gamma transformation later if no semitransparent entries
- * are present in the tRNS array for palette images.  We can't do it here
- * because we don't necessarily have the tRNS chunk yet.
- */
-void PNGAPI
-png_set_gamma(png_structp png_ptr, double scrn_gamma, double file_gamma)
-{
-   png_debug(1, "in png_set_gamma\n");
-   if(png_ptr == NULL) return;
-   if ((fabs(scrn_gamma * file_gamma - 1.0) > PNG_GAMMA_THRESHOLD) ||
-       (png_ptr->color_type & PNG_COLOR_MASK_ALPHA) ||
-       (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE))
-     png_ptr->transformations |= PNG_GAMMA;
-   png_ptr->gamma = (float)file_gamma;
-   png_ptr->screen_gamma = (float)scrn_gamma;
-}
-#endif
-
-#if defined(PNG_READ_EXPAND_SUPPORTED)
-/* Expand paletted images to RGB, expand grayscale images of
- * less than 8-bit depth to 8-bit depth, and expand tRNS chunks
- * to alpha channels.
- */
-void PNGAPI
-png_set_expand(png_structp png_ptr)
-{
-   png_debug(1, "in png_set_expand\n");
-   if(png_ptr == NULL) return;
-   png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS);
-   png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
-}
-
-/* GRR 19990627:  the following three functions currently are identical
- *  to png_set_expand().  However, it is entirely reasonable that someone
- *  might wish to expand an indexed image to RGB but *not* expand a single,
- *  fully transparent palette entry to a full alpha channel--perhaps instead
- *  convert tRNS to the grayscale/RGB format (16-bit RGB value), or replace
- *  the transparent color with a particular RGB value, or drop tRNS entirely.
- *  IOW, a future version of the library may make the transformations flag
- *  a bit more fine-grained, with separate bits for each of these three
- *  functions.
- *
- *  More to the point, these functions make it obvious what libpng will be
- *  doing, whereas "expand" can (and does) mean any number of things.
- *
- *  GRP 20060307: In libpng-1.4.0, png_set_gray_1_2_4_to_8() was modified
- *  to expand only the sample depth but not to expand the tRNS to alpha.
- */
-
-/* Expand paletted images to RGB. */
-void PNGAPI
-png_set_palette_to_rgb(png_structp png_ptr)
-{
-   png_debug(1, "in png_set_palette_to_rgb\n");
-   if(png_ptr == NULL) return;
-   png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS);
-   png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
-}
-
-#if !defined(PNG_1_0_X)
-/* Expand grayscale images of less than 8-bit depth to 8 bits. */
-void PNGAPI
-png_set_expand_gray_1_2_4_to_8(png_structp png_ptr)
-{
-   png_debug(1, "in png_set_expand_gray_1_2_4_to_8\n");
-   if(png_ptr == NULL) return;
-   png_ptr->transformations |= PNG_EXPAND;
-   png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
-}
-#endif
-
-#if defined(PNG_1_0_X) || defined(PNG_1_2_X)
-/* Expand grayscale images of less than 8-bit depth to 8 bits. */
-/* Deprecated as of libpng-1.2.9 */
-void PNGAPI
-png_set_gray_1_2_4_to_8(png_structp png_ptr)
-{
-   png_debug(1, "in png_set_gray_1_2_4_to_8\n");
-   if(png_ptr == NULL) return;
-   png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS);
-}
-#endif
-
-
-/* Expand tRNS chunks to alpha channels. */
-void PNGAPI
-png_set_tRNS_to_alpha(png_structp png_ptr)
-{
-   png_debug(1, "in png_set_tRNS_to_alpha\n");
-   png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS);
-   png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
-}
-#endif /* defined(PNG_READ_EXPAND_SUPPORTED) */
-
-#if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED)
-void PNGAPI
-png_set_gray_to_rgb(png_structp png_ptr)
-{
-   png_debug(1, "in png_set_gray_to_rgb\n");
-   png_ptr->transformations |= PNG_GRAY_TO_RGB;
-   png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
-}
-#endif
-
-#if defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
-#if defined(PNG_FLOATING_POINT_SUPPORTED)
-/* Convert a RGB image to a grayscale of the same width.  This allows us,
- * for example, to convert a 24 bpp RGB image into an 8 bpp grayscale image.
- */
-
-void PNGAPI
-png_set_rgb_to_gray(png_structp png_ptr, int error_action, double red,
-   double green)
-{
-      int red_fixed = (int)((float)red*100000.0 + 0.5);
-      int green_fixed = (int)((float)green*100000.0 + 0.5);
-      if(png_ptr == NULL) return;
-      png_set_rgb_to_gray_fixed(png_ptr, error_action, red_fixed, green_fixed);
-}
-#endif
-
-void PNGAPI
-png_set_rgb_to_gray_fixed(png_structp png_ptr, int error_action,
-   png_fixed_point red, png_fixed_point green)
-{
-   png_debug(1, "in png_set_rgb_to_gray\n");
-   if(png_ptr == NULL) return;
-   switch(error_action)
-   {
-      case 1: png_ptr->transformations |= PNG_RGB_TO_GRAY;
-              break;
-      case 2: png_ptr->transformations |= PNG_RGB_TO_GRAY_WARN;
-              break;
-      case 3: png_ptr->transformations |= PNG_RGB_TO_GRAY_ERR;
-   }
-   if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
-#if defined(PNG_READ_EXPAND_SUPPORTED)
-      png_ptr->transformations |= PNG_EXPAND;
-#else
-   {
-      png_warning(png_ptr, "Cannot do RGB_TO_GRAY without EXPAND_SUPPORTED.");
-      png_ptr->transformations &= ~PNG_RGB_TO_GRAY;
-   }
-#endif
-   {
-      png_uint_16 red_int, green_int;
-      if(red < 0 || green < 0)
-      {
-         red_int   =  6968; /* .212671 * 32768 + .5 */
-         green_int = 23434; /* .715160 * 32768 + .5 */
-      }
-      else if(red + green < 100000L)
-      {
-        red_int = (png_uint_16)(((png_uint_32)red*32768L)/100000L);
-        green_int = (png_uint_16)(((png_uint_32)green*32768L)/100000L);
-      }
-      else
-      {
-         png_warning(png_ptr, "ignoring out of range rgb_to_gray coefficients");
-         red_int   =  6968;
-         green_int = 23434;
-      }
-      png_ptr->rgb_to_gray_red_coeff   = red_int;
-      png_ptr->rgb_to_gray_green_coeff = green_int;
-      png_ptr->rgb_to_gray_blue_coeff  = (png_uint_16)(32768-red_int-green_int);
-   }
-}
-#endif
-
-#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \
-    defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED) || \
-    defined(PNG_LEGACY_SUPPORTED)
-void PNGAPI
-png_set_read_user_transform_fn(png_structp png_ptr, png_user_transform_ptr
-   read_user_transform_fn)
-{
-   png_debug(1, "in png_set_read_user_transform_fn\n");
-   if(png_ptr == NULL) return;
-#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
-   png_ptr->transformations |= PNG_USER_TRANSFORM;
-   png_ptr->read_user_transform_fn = read_user_transform_fn;
-#endif
-#ifdef PNG_LEGACY_SUPPORTED
-   if(read_user_transform_fn)
-      png_warning(png_ptr,
-        "This version of libpng does not support user transforms");
-#endif
-}
-#endif
-
-/* Initialize everything needed for the read.  This includes modifying
- * the palette.
- */
-void /* PRIVATE */
-png_init_read_transformations(png_structp png_ptr)
-{
-   png_debug(1, "in png_init_read_transformations\n");
-#if defined(PNG_USELESS_TESTS_SUPPORTED)
-   if(png_ptr != NULL)
-#endif
-  {
-#if defined(PNG_READ_BACKGROUND_SUPPORTED) || defined(PNG_READ_SHIFT_SUPPORTED) \
- || defined(PNG_READ_GAMMA_SUPPORTED)
-   int color_type = png_ptr->color_type;
-#endif
-
-#if defined(PNG_READ_EXPAND_SUPPORTED) && defined(PNG_READ_BACKGROUND_SUPPORTED)
-
-#if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED)
-   /* Detect gray background and attempt to enable optimization
-    * for gray --> RGB case */
-   /* Note:  if PNG_BACKGROUND_EXPAND is set and color_type is either RGB or
-    * RGB_ALPHA (in which case need_expand is superfluous anyway), the
-    * background color might actually be gray yet not be flagged as such.
-    * This is not a problem for the current code, which uses
-    * PNG_BACKGROUND_IS_GRAY only to decide when to do the
-    * png_do_gray_to_rgb() transformation.
-    */
-   if ((png_ptr->transformations & PNG_BACKGROUND_EXPAND) &&
-       !(color_type & PNG_COLOR_MASK_COLOR))
-   {
-          png_ptr->mode |= PNG_BACKGROUND_IS_GRAY;
-   } else if ((png_ptr->transformations & PNG_BACKGROUND) &&
-              !(png_ptr->transformations & PNG_BACKGROUND_EXPAND) &&
-              (png_ptr->transformations & PNG_GRAY_TO_RGB) &&
-              png_ptr->background.red == png_ptr->background.green &&
-              png_ptr->background.red == png_ptr->background.blue)
-   {
-          png_ptr->mode |= PNG_BACKGROUND_IS_GRAY;
-          png_ptr->background.gray = png_ptr->background.red;
-   }
-#endif
-
-   if ((png_ptr->transformations & PNG_BACKGROUND_EXPAND) &&
-       (png_ptr->transformations & PNG_EXPAND))
-   {
-      if (!(color_type & PNG_COLOR_MASK_COLOR))  /* i.e., GRAY or GRAY_ALPHA */
-      {
-         /* expand background and tRNS chunks */
-         switch (png_ptr->bit_depth)
-         {
-            case 1:
-               png_ptr->background.gray *= (png_uint_16)0xff;
-               png_ptr->background.red = png_ptr->background.green
-                 =  png_ptr->background.blue = png_ptr->background.gray;
-               if (!(png_ptr->transformations & PNG_EXPAND_tRNS))
-               {
-                 png_ptr->trans_values.gray *= (png_uint_16)0xff;
-                 png_ptr->trans_values.red = png_ptr->trans_values.green
-                   = png_ptr->trans_values.blue = png_ptr->trans_values.gray;
-               }
-               break;
-            case 2:
-               png_ptr->background.gray *= (png_uint_16)0x55;
-               png_ptr->background.red = png_ptr->background.green
-                 = png_ptr->background.blue = png_ptr->background.gray;
-               if (!(png_ptr->transformations & PNG_EXPAND_tRNS))
-               {
-                 png_ptr->trans_values.gray *= (png_uint_16)0x55;
-                 png_ptr->trans_values.red = png_ptr->trans_values.green
-                   = png_ptr->trans_values.blue = png_ptr->trans_values.gray;
-               }
-               break;
-            case 4:
-               png_ptr->background.gray *= (png_uint_16)0x11;
-               png_ptr->background.red = png_ptr->background.green
-                 = png_ptr->background.blue = png_ptr->background.gray;
-               if (!(png_ptr->transformations & PNG_EXPAND_tRNS))
-               {
-                 png_ptr->trans_values.gray *= (png_uint_16)0x11;
-                 png_ptr->trans_values.red = png_ptr->trans_values.green
-                   = png_ptr->trans_values.blue = png_ptr->trans_values.gray;
-               }
-               break;
-            case 8:
-            case 16:
-               png_ptr->background.red = png_ptr->background.green
-                 = png_ptr->background.blue = png_ptr->background.gray;
-               break;
-         }
-      }
-      else if (color_type == PNG_COLOR_TYPE_PALETTE)
-      {
-         png_ptr->background.red   =
-            png_ptr->palette[png_ptr->background.index].red;
-         png_ptr->background.green =
-            png_ptr->palette[png_ptr->background.index].green;
-         png_ptr->background.blue  =
-            png_ptr->palette[png_ptr->background.index].blue;
-
-#if defined(PNG_READ_INVERT_ALPHA_SUPPORTED)
-        if (png_ptr->transformations & PNG_INVERT_ALPHA)
-        {
-#if defined(PNG_READ_EXPAND_SUPPORTED)
-           if (!(png_ptr->transformations & PNG_EXPAND_tRNS))
-#endif
-           {
-           /* invert the alpha channel (in tRNS) unless the pixels are
-              going to be expanded, in which case leave it for later */
-              int i,istop;
-              istop=(int)png_ptr->num_trans;
-              for (i=0; i<istop; i++)
-                 png_ptr->trans[i] = (png_byte)(255 - png_ptr->trans[i]);
-           }
-        }
-#endif
-
-      }
-   }
-#endif
-
-#if defined(PNG_READ_BACKGROUND_SUPPORTED) && defined(PNG_READ_GAMMA_SUPPORTED)
-   png_ptr->background_1 = png_ptr->background;
-#endif
-#if defined(PNG_READ_GAMMA_SUPPORTED) && defined(PNG_FLOATING_POINT_SUPPORTED)
-
-   if ((color_type == PNG_COLOR_TYPE_PALETTE && png_ptr->num_trans != 0)
-       && (fabs(png_ptr->screen_gamma * png_ptr->gamma - 1.0)
-         < PNG_GAMMA_THRESHOLD))
-   {
-    int i,k;
-    k=0;
-    for (i=0; i<png_ptr->num_trans; i++)
-    {
-      if (png_ptr->trans[i] != 0 && png_ptr->trans[i] != 0xff)
-        k=1; /* partial transparency is present */
-    }
-    if (k == 0)
-      png_ptr->transformations &= ~PNG_GAMMA;
-   }
-
-   if ((png_ptr->transformations & (PNG_GAMMA | PNG_RGB_TO_GRAY)) &&
-        png_ptr->gamma != 0.0)
-   {
-      png_build_gamma_table(png_ptr);
-#if defined(PNG_READ_BACKGROUND_SUPPORTED)
-      if (png_ptr->transformations & PNG_BACKGROUND)
-      {
-         if (color_type == PNG_COLOR_TYPE_PALETTE)
-         {
-           /* could skip if no transparency and
-           */
-            png_color back, back_1;
-            png_colorp palette = png_ptr->palette;
-            int num_palette = png_ptr->num_palette;
-            int i;
-            if (png_ptr->background_gamma_type == PNG_BACKGROUND_GAMMA_FILE)
-            {
-               back.red = png_ptr->gamma_table[png_ptr->background.red];
-               back.green = png_ptr->gamma_table[png_ptr->background.green];
-               back.blue = png_ptr->gamma_table[png_ptr->background.blue];
-
-               back_1.red = png_ptr->gamma_to_1[png_ptr->background.red];
-               back_1.green = png_ptr->gamma_to_1[png_ptr->background.green];
-               back_1.blue = png_ptr->gamma_to_1[png_ptr->background.blue];
-            }
-            else
-            {
-               double g, gs;
-
-               switch (png_ptr->background_gamma_type)
-               {
-                  case PNG_BACKGROUND_GAMMA_SCREEN:
-                     g = (png_ptr->screen_gamma);
-                     gs = 1.0;
-                     break;
-                  case PNG_BACKGROUND_GAMMA_FILE:
-                     g = 1.0 / (png_ptr->gamma);
-                     gs = 1.0 / (png_ptr->gamma * png_ptr->screen_gamma);
-                     break;
-                  case PNG_BACKGROUND_GAMMA_UNIQUE:
-                     g = 1.0 / (png_ptr->background_gamma);
-                     gs = 1.0 / (png_ptr->background_gamma *
-                                 png_ptr->screen_gamma);
-                     break;
-                  default:
-                     g = 1.0;    /* back_1 */
-                     gs = 1.0;   /* back */
-               }
-
-               if ( fabs(gs - 1.0) < PNG_GAMMA_THRESHOLD)
-               {
-                  back.red   = (png_byte)png_ptr->background.red;
-                  back.green = (png_byte)png_ptr->background.green;
-                  back.blue  = (png_byte)png_ptr->background.blue;
-               }
-               else
-               {
-                  back.red = (png_byte)(pow(
-                     (double)png_ptr->background.red/255, gs) * 255.0 + .5);
-                  back.green = (png_byte)(pow(
-                     (double)png_ptr->background.green/255, gs) * 255.0 + .5);
-                  back.blue = (png_byte)(pow(
-                     (double)png_ptr->background.blue/255, gs) * 255.0 + .5);
-               }
-
-               back_1.red = (png_byte)(pow(
-                  (double)png_ptr->background.red/255, g) * 255.0 + .5);
-               back_1.green = (png_byte)(pow(
-                  (double)png_ptr->background.green/255, g) * 255.0 + .5);
-               back_1.blue = (png_byte)(pow(
-                  (double)png_ptr->background.blue/255, g) * 255.0 + .5);
-            }
-            for (i = 0; i < num_palette; i++)
-            {
-               if (i < (int)png_ptr->num_trans && png_ptr->trans[i] != 0xff)
-               {
-                  if (png_ptr->trans[i] == 0)
-                  {
-                     palette[i] = back;
-                  }
-                  else /* if (png_ptr->trans[i] != 0xff) */
-                  {
-                     png_byte v, w;
-
-                     v = png_ptr->gamma_to_1[palette[i].red];
-                     png_composite(w, v, png_ptr->trans[i], back_1.red);
-                     palette[i].red = png_ptr->gamma_from_1[w];
-
-                     v = png_ptr->gamma_to_1[palette[i].green];
-                     png_composite(w, v, png_ptr->trans[i], back_1.green);
-                     palette[i].green = png_ptr->gamma_from_1[w];
-
-                     v = png_ptr->gamma_to_1[palette[i].blue];
-                     png_composite(w, v, png_ptr->trans[i], back_1.blue);
-                     palette[i].blue = png_ptr->gamma_from_1[w];
-                  }
-               }
-               else
-               {
-                  palette[i].red = png_ptr->gamma_table[palette[i].red];
-                  palette[i].green = png_ptr->gamma_table[palette[i].green];
-                  palette[i].blue = png_ptr->gamma_table[palette[i].blue];
-               }
-            }
-           /* Prevent the transformations being done again, and make sure
-            * that the now spurious alpha channel is stripped - the code
-            * has just reduced background composition and gamma correction
-            * to a simple alpha channel strip.
-            */
-           png_ptr->transformations &= ~PNG_BACKGROUND;
-           png_ptr->transformations &= ~PNG_GAMMA;
-           png_ptr->transformations |= PNG_STRIP_ALPHA;
-         }
-         /* if (png_ptr->background_gamma_type!=PNG_BACKGROUND_GAMMA_UNKNOWN) */
-         else
-         /* color_type != PNG_COLOR_TYPE_PALETTE */
-         {
-            double m = (double)(((png_uint_32)1 << png_ptr->bit_depth) - 1);
-            double g = 1.0;
-            double gs = 1.0;
-
-            switch (png_ptr->background_gamma_type)
-            {
-               case PNG_BACKGROUND_GAMMA_SCREEN:
-                  g = (png_ptr->screen_gamma);
-                  gs = 1.0;
-                  break;
-               case PNG_BACKGROUND_GAMMA_FILE:
-                  g = 1.0 / (png_ptr->gamma);
-                  gs = 1.0 / (png_ptr->gamma * png_ptr->screen_gamma);
-                  break;
-               case PNG_BACKGROUND_GAMMA_UNIQUE:
-                  g = 1.0 / (png_ptr->background_gamma);
-                  gs = 1.0 / (png_ptr->background_gamma *
-                     png_ptr->screen_gamma);
-                  break;
-            }
-
-            png_ptr->background_1.gray = (png_uint_16)(pow(
-               (double)png_ptr->background.gray / m, g) * m + .5);
-            png_ptr->background.gray = (png_uint_16)(pow(
-               (double)png_ptr->background.gray / m, gs) * m + .5);
-
-            if ((png_ptr->background.red != png_ptr->background.green) ||
-                (png_ptr->background.red != png_ptr->background.blue) ||
-                (png_ptr->background.red != png_ptr->background.gray))
-            {
-               /* RGB or RGBA with color background */
-               png_ptr->background_1.red = (png_uint_16)(pow(
-                  (double)png_ptr->background.red / m, g) * m + .5);
-               png_ptr->background_1.green = (png_uint_16)(pow(
-                  (double)png_ptr->background.green / m, g) * m + .5);
-               png_ptr->background_1.blue = (png_uint_16)(pow(
-                  (double)png_ptr->background.blue / m, g) * m + .5);
-               png_ptr->background.red = (png_uint_16)(pow(
-                  (double)png_ptr->background.red / m, gs) * m + .5);
-               png_ptr->background.green = (png_uint_16)(pow(
-                  (double)png_ptr->background.green / m, gs) * m + .5);
-               png_ptr->background.blue = (png_uint_16)(pow(
-                  (double)png_ptr->background.blue / m, gs) * m + .5);
-            }
-            else
-            {
-               /* GRAY, GRAY ALPHA, RGB, or RGBA with gray background */
-               png_ptr->background_1.red = png_ptr->background_1.green
-                 = png_ptr->background_1.blue = png_ptr->background_1.gray;
-               png_ptr->background.red = png_ptr->background.green
-                 = png_ptr->background.blue = png_ptr->background.gray;
-            }
-         }
-      }
-      else
-      /* transformation does not include PNG_BACKGROUND */
-#endif /* PNG_READ_BACKGROUND_SUPPORTED */
-      if (color_type == PNG_COLOR_TYPE_PALETTE)
-      {
-         png_colorp palette = png_ptr->palette;
-         int num_palette = png_ptr->num_palette;
-         int i;
-
-         for (i = 0; i < num_palette; i++)
-         {
-            palette[i].red = png_ptr->gamma_table[palette[i].red];
-            palette[i].green = png_ptr->gamma_table[palette[i].green];
-            palette[i].blue = png_ptr->gamma_table[palette[i].blue];
-         }
-
-        /* Done the gamma correction. */
-        png_ptr->transformations &= ~PNG_GAMMA;
-      }
-   }
-#if defined(PNG_READ_BACKGROUND_SUPPORTED)
-   else
-#endif
-#endif /* PNG_READ_GAMMA_SUPPORTED && PNG_FLOATING_POINT_SUPPORTED */
-#if defined(PNG_READ_BACKGROUND_SUPPORTED)
-   /* No GAMMA transformation */
-   if ((png_ptr->transformations & PNG_BACKGROUND) &&
-       (color_type == PNG_COLOR_TYPE_PALETTE))
-   {
-      int i;
-      int istop = (int)png_ptr->num_trans;
-      png_color back;
-      png_colorp palette = png_ptr->palette;
-
-      back.red   = (png_byte)png_ptr->background.red;
-      back.green = (png_byte)png_ptr->background.green;
-      back.blue  = (png_byte)png_ptr->background.blue;
-
-      for (i = 0; i < istop; i++)
-      {
-         if (png_ptr->trans[i] == 0)
-         {
-            palette[i] = back;
-         }
-         else if (png_ptr->trans[i] != 0xff)
-         {
-            /* The png_composite() macro is defined in png.h */
-            png_composite(palette[i].red, palette[i].red,
-               png_ptr->trans[i], back.red);
-            png_composite(palette[i].green, palette[i].green,
-               png_ptr->trans[i], back.green);
-            png_composite(palette[i].blue, palette[i].blue,
-               png_ptr->trans[i], back.blue);
-         }
-      }
-
-      /* Handled alpha, still need to strip the channel. */
-      png_ptr->transformations &= ~PNG_BACKGROUND;
-      png_ptr->transformations |= PNG_STRIP_ALPHA;
-   }
-#endif /* PNG_READ_BACKGROUND_SUPPORTED */
-
-#if defined(PNG_READ_SHIFT_SUPPORTED)
-   if ((png_ptr->transformations & PNG_SHIFT) &&
-      (color_type == PNG_COLOR_TYPE_PALETTE))
-   {
-      png_uint_16 i;
-      png_uint_16 istop = png_ptr->num_palette;
-      int sr = 8 - png_ptr->sig_bit.red;
-      int sg = 8 - png_ptr->sig_bit.green;
-      int sb = 8 - png_ptr->sig_bit.blue;
-
-      if (sr < 0 || sr > 8)
-         sr = 0;
-      if (sg < 0 || sg > 8)
-         sg = 0;
-      if (sb < 0 || sb > 8)
-         sb = 0;
-      for (i = 0; i < istop; i++)
-      {
-         png_ptr->palette[i].red >>= sr;
-         png_ptr->palette[i].green >>= sg;
-         png_ptr->palette[i].blue >>= sb;
-      }
-   }
-#endif  /* PNG_READ_SHIFT_SUPPORTED */
- }
-#if !defined(PNG_READ_GAMMA_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED) \
- && !defined(PNG_READ_BACKGROUND_SUPPORTED)
-   if(png_ptr)
-      return;
-#endif
-}
-
-/* Modify the info structure to reflect the transformations.  The
- * info should be updated so a PNG file could be written with it,
- * assuming the transformations result in valid PNG data.
- */
-void /* PRIVATE */
-png_read_transform_info(png_structp png_ptr, png_infop info_ptr)
-{
-   png_debug(1, "in png_read_transform_info\n");
-#if defined(PNG_READ_EXPAND_SUPPORTED)
-   if (png_ptr->transformations & PNG_EXPAND)
-   {
-      if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
-      {
-         if (png_ptr->num_trans &&
-              (png_ptr->transformations & PNG_EXPAND_tRNS))
-            info_ptr->color_type = PNG_COLOR_TYPE_RGB_ALPHA;
-         else
-            info_ptr->color_type = PNG_COLOR_TYPE_RGB;
-         info_ptr->bit_depth = 8;
-         info_ptr->num_trans = 0;
-      }
-      else
-      {
-         if (png_ptr->num_trans)
-         {
-            if (png_ptr->transformations & PNG_EXPAND_tRNS)
-              info_ptr->color_type |= PNG_COLOR_MASK_ALPHA;
-#if 0 /* Removed from libpng-1.2.27 */
-            else
-              info_ptr->color_type |= PNG_COLOR_MASK_COLOR;
-#endif
-         }
-         if (info_ptr->bit_depth < 8)
-            info_ptr->bit_depth = 8;
-         info_ptr->num_trans = 0;
-      }
-   }
-#endif
-
-#if defined(PNG_READ_BACKGROUND_SUPPORTED)
-   if (png_ptr->transformations & PNG_BACKGROUND)
-   {
-      info_ptr->color_type &= ~PNG_COLOR_MASK_ALPHA;
-      info_ptr->num_trans = 0;
-      info_ptr->background = png_ptr->background;
-   }
-#endif
-
-#if defined(PNG_READ_GAMMA_SUPPORTED)
-   if (png_ptr->transformations & PNG_GAMMA)
-   {
-#ifdef PNG_FLOATING_POINT_SUPPORTED
-      info_ptr->gamma = png_ptr->gamma;
-#endif
-#ifdef PNG_FIXED_POINT_SUPPORTED
-      info_ptr->int_gamma = png_ptr->int_gamma;
-#endif
-   }
-#endif
-
-#if defined(PNG_READ_16_TO_8_SUPPORTED)
-   if ((png_ptr->transformations & PNG_16_TO_8) && (info_ptr->bit_depth == 16))
-      info_ptr->bit_depth = 8;
-#endif
-
-#if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED)
-   if (png_ptr->transformations & PNG_GRAY_TO_RGB)
-      info_ptr->color_type |= PNG_COLOR_MASK_COLOR;
-#endif
-
-#if defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
-   if (png_ptr->transformations & PNG_RGB_TO_GRAY)
-      info_ptr->color_type &= ~PNG_COLOR_MASK_COLOR;
-#endif
-
-#if defined(PNG_READ_DITHER_SUPPORTED)
-   if (png_ptr->transformations & PNG_DITHER)
-   {
-      if (((info_ptr->color_type == PNG_COLOR_TYPE_RGB) ||
-         (info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)) &&
-         png_ptr->palette_lookup && info_ptr->bit_depth == 8)
-      {
-         info_ptr->color_type = PNG_COLOR_TYPE_PALETTE;
-      }
-   }
-#endif
-
-#if defined(PNG_READ_PACK_SUPPORTED)
-   if ((png_ptr->transformations & PNG_PACK) && (info_ptr->bit_depth < 8))
-      info_ptr->bit_depth = 8;
-#endif
-
-   if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
-      info_ptr->channels = 1;
-   else if (info_ptr->color_type & PNG_COLOR_MASK_COLOR)
-      info_ptr->channels = 3;
-   else
-      info_ptr->channels = 1;
-
-#if defined(PNG_READ_STRIP_ALPHA_SUPPORTED)
-   if (png_ptr->flags & PNG_FLAG_STRIP_ALPHA)
-      info_ptr->color_type &= ~PNG_COLOR_MASK_ALPHA;
-#endif
-
-   if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA)
-      info_ptr->channels++;
-
-#if defined(PNG_READ_FILLER_SUPPORTED)
-   /* STRIP_ALPHA and FILLER allowed:  MASK_ALPHA bit stripped above */
-   if ((png_ptr->transformations & PNG_FILLER) &&
-       ((info_ptr->color_type == PNG_COLOR_TYPE_RGB) ||
-       (info_ptr->color_type == PNG_COLOR_TYPE_GRAY)))
-   {
-      info_ptr->channels++;
-      /* if adding a true alpha channel not just filler */
-#if !defined(PNG_1_0_X)
-      if (png_ptr->transformations & PNG_ADD_ALPHA)
-        info_ptr->color_type |= PNG_COLOR_MASK_ALPHA;
-#endif
-   }
-#endif
-
-#if defined(PNG_USER_TRANSFORM_PTR_SUPPORTED) && \
-defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
-   if(png_ptr->transformations & PNG_USER_TRANSFORM)
-     {
-       if(info_ptr->bit_depth < png_ptr->user_transform_depth)
-         info_ptr->bit_depth = png_ptr->user_transform_depth;
-       if(info_ptr->channels < png_ptr->user_transform_channels)
-         info_ptr->channels = png_ptr->user_transform_channels;
-     }
-#endif
-
-   info_ptr->pixel_depth = (png_byte)(info_ptr->channels *
-      info_ptr->bit_depth);
-
-   info_ptr->rowbytes = PNG_ROWBYTES(info_ptr->pixel_depth,info_ptr->width);
-
-#if !defined(PNG_READ_EXPAND_SUPPORTED)
-   if(png_ptr)
-      return;
-#endif
-}
-
-/* Transform the row.  The order of transformations is significant,
- * and is very touchy.  If you add a transformation, take care to
- * decide how it fits in with the other transformations here.
- */
-void /* PRIVATE */
-png_do_read_transformations(png_structp png_ptr)
-{
-   png_debug(1, "in png_do_read_transformations\n");
-   if (png_ptr->row_buf == NULL)
-   {
-#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
-      char msg[50];
-
-      png_snprintf2(msg, 50,
-         "NULL row buffer for row %ld, pass %d", png_ptr->row_number,
-         png_ptr->pass);
-      png_error(png_ptr, msg);
-#else
-      png_error(png_ptr, "NULL row buffer");
-#endif
-   }
-#ifdef PNG_WARN_UNINITIALIZED_ROW
-   if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
-      /* Application has failed to call either png_read_start_image()
-       * or png_read_update_info() after setting transforms that expand
-       * pixels.  This check added to libpng-1.2.19 */
-#if (PNG_WARN_UNINITIALIZED_ROW==1)
-      png_error(png_ptr, "Uninitialized row");
-#else
-      png_warning(png_ptr, "Uninitialized row");
-#endif
-#endif
-
-#if defined(PNG_READ_EXPAND_SUPPORTED)
-   if (png_ptr->transformations & PNG_EXPAND)
-   {
-      if (png_ptr->row_info.color_type == PNG_COLOR_TYPE_PALETTE)
-      {
-         png_do_expand_palette(&(png_ptr->row_info), png_ptr->row_buf + 1,
-            png_ptr->palette, png_ptr->trans, png_ptr->num_trans);
-      }
-      else
-      {
-         if (png_ptr->num_trans &&
-             (png_ptr->transformations & PNG_EXPAND_tRNS))
-            png_do_expand(&(png_ptr->row_info), png_ptr->row_buf + 1,
-               &(png_ptr->trans_values));
-         else
-            png_do_expand(&(png_ptr->row_info), png_ptr->row_buf + 1,
-               NULL);
-      }
-   }
-#endif
-
-#if defined(PNG_READ_STRIP_ALPHA_SUPPORTED)
-   if (png_ptr->flags & PNG_FLAG_STRIP_ALPHA)
-      png_do_strip_filler(&(png_ptr->row_info), png_ptr->row_buf + 1,
-         PNG_FLAG_FILLER_AFTER | (png_ptr->flags & PNG_FLAG_STRIP_ALPHA));
-#endif
-
-#if defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
-   if (png_ptr->transformations & PNG_RGB_TO_GRAY)
-   {
-      int rgb_error =
-         png_do_rgb_to_gray(png_ptr, &(png_ptr->row_info), png_ptr->row_buf + 1);
-      if(rgb_error)
-      {
-         png_ptr->rgb_to_gray_status=1;
-         if((png_ptr->transformations & PNG_RGB_TO_GRAY) == 
-             PNG_RGB_TO_GRAY_WARN)
-            png_warning(png_ptr, "png_do_rgb_to_gray found nongray pixel");
-         if((png_ptr->transformations & PNG_RGB_TO_GRAY) ==
-             PNG_RGB_TO_GRAY_ERR)
-            png_error(png_ptr, "png_do_rgb_to_gray found nongray pixel");
-      }
-   }
-#endif
-
-/*
-From Andreas Dilger e-mail to png-implement, 26 March 1998:
-
-  In most cases, the "simple transparency" should be done prior to doing
-  gray-to-RGB, or you will have to test 3x as many bytes to check if a
-  pixel is transparent.  You would also need to make sure that the
-  transparency information is upgraded to RGB.
-
-  To summarize, the current flow is:
-  - Gray + simple transparency -> compare 1 or 2 gray bytes and composite
-                                  with background "in place" if transparent,
-                                  convert to RGB if necessary
-  - Gray + alpha -> composite with gray background and remove alpha bytes,
-                                  convert to RGB if necessary
-
-  To support RGB backgrounds for gray images we need:
-  - Gray + simple transparency -> convert to RGB + simple transparency, compare
-                                  3 or 6 bytes and composite with background
-                                  "in place" if transparent (3x compare/pixel
-                                  compared to doing composite with gray bkgrnd)
-  - Gray + alpha -> convert to RGB + alpha, composite with background and
-                                  remove alpha bytes (3x float operations/pixel
-                                  compared with composite on gray background)
-
-  Greg's change will do this.  The reason it wasn't done before is for
-  performance, as this increases the per-pixel operations.  If we would check
-  in advance if the background was gray or RGB, and position the gray-to-RGB
-  transform appropriately, then it would save a lot of work/time.
- */
-
-#if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED)
-   /* if gray -> RGB, do so now only if background is non-gray; else do later
-    * for performance reasons */
-   if ((png_ptr->transformations & PNG_GRAY_TO_RGB) &&
-       !(png_ptr->mode & PNG_BACKGROUND_IS_GRAY))
-      png_do_gray_to_rgb(&(png_ptr->row_info), png_ptr->row_buf + 1);
-#endif
-
-#if defined(PNG_READ_BACKGROUND_SUPPORTED)
-   if ((png_ptr->transformations & PNG_BACKGROUND) &&
-      ((png_ptr->num_trans != 0 ) ||
-      (png_ptr->color_type & PNG_COLOR_MASK_ALPHA)))
-      png_do_background(&(png_ptr->row_info), png_ptr->row_buf + 1,
-         &(png_ptr->trans_values), &(png_ptr->background)
-#if defined(PNG_READ_GAMMA_SUPPORTED)
-         , &(png_ptr->background_1),
-         png_ptr->gamma_table, png_ptr->gamma_from_1,
-         png_ptr->gamma_to_1, png_ptr->gamma_16_table,
-         png_ptr->gamma_16_from_1, png_ptr->gamma_16_to_1,
-         png_ptr->gamma_shift
-#endif
-);
-#endif
-
-#if defined(PNG_READ_GAMMA_SUPPORTED)
-   if ((png_ptr->transformations & PNG_GAMMA) &&
-#if defined(PNG_READ_BACKGROUND_SUPPORTED)
-      !((png_ptr->transformations & PNG_BACKGROUND) &&
-      ((png_ptr->num_trans != 0) ||
-      (png_ptr->color_type & PNG_COLOR_MASK_ALPHA))) &&
-#endif
-      (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE))
-      png_do_gamma(&(png_ptr->row_info), png_ptr->row_buf + 1,
-         png_ptr->gamma_table, png_ptr->gamma_16_table,
-         png_ptr->gamma_shift);
-#endif
-
-#if defined(PNG_READ_16_TO_8_SUPPORTED)
-   if (png_ptr->transformations & PNG_16_TO_8)
-      png_do_chop(&(png_ptr->row_info), png_ptr->row_buf + 1);
-#endif
-
-#if defined(PNG_READ_DITHER_SUPPORTED)
-   if (png_ptr->transformations & PNG_DITHER)
-   {
-      png_do_dither((png_row_infop)&(png_ptr->row_info), png_ptr->row_buf + 1,
-         png_ptr->palette_lookup, png_ptr->dither_index);
-      if(png_ptr->row_info.rowbytes == (png_uint_32)0)
-         png_error(png_ptr, "png_do_dither returned rowbytes=0");
-   }
-#endif
-
-#if defined(PNG_READ_INVERT_SUPPORTED)
-   if (png_ptr->transformations & PNG_INVERT_MONO)
-      png_do_invert(&(png_ptr->row_info), png_ptr->row_buf + 1);
-#endif
-
-#if defined(PNG_READ_SHIFT_SUPPORTED)
-   if (png_ptr->transformations & PNG_SHIFT)
-      png_do_unshift(&(png_ptr->row_info), png_ptr->row_buf + 1,
-         &(png_ptr->shift));
-#endif
-
-#if defined(PNG_READ_PACK_SUPPORTED)
-   if (png_ptr->transformations & PNG_PACK)
-      png_do_unpack(&(png_ptr->row_info), png_ptr->row_buf + 1);
-#endif
-
-#if defined(PNG_READ_BGR_SUPPORTED)
-   if (png_ptr->transformations & PNG_BGR)
-      png_do_bgr(&(png_ptr->row_info), png_ptr->row_buf + 1);
-#endif
-
-#if defined(PNG_READ_PACKSWAP_SUPPORTED)
-   if (png_ptr->transformations & PNG_PACKSWAP)
-      png_do_packswap(&(png_ptr->row_info), png_ptr->row_buf + 1);
-#endif
-
-#if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED)
-   /* if gray -> RGB, do so now only if we did not do so above */
-   if ((png_ptr->transformations & PNG_GRAY_TO_RGB) &&
-       (png_ptr->mode & PNG_BACKGROUND_IS_GRAY))
-      png_do_gray_to_rgb(&(png_ptr->row_info), png_ptr->row_buf + 1);
-#endif
-
-#if defined(PNG_READ_FILLER_SUPPORTED)
-   if (png_ptr->transformations & PNG_FILLER)
-      png_do_read_filler(&(png_ptr->row_info), png_ptr->row_buf + 1,
-         (png_uint_32)png_ptr->filler, png_ptr->flags);
-#endif
-
-#if defined(PNG_READ_INVERT_ALPHA_SUPPORTED)
-   if (png_ptr->transformations & PNG_INVERT_ALPHA)
-      png_do_read_invert_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1);
-#endif
-
-#if defined(PNG_READ_SWAP_ALPHA_SUPPORTED)
-   if (png_ptr->transformations & PNG_SWAP_ALPHA)
-      png_do_read_swap_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1);
-#endif
-
-#if defined(PNG_READ_SWAP_SUPPORTED)
-   if (png_ptr->transformations & PNG_SWAP_BYTES)
-      png_do_swap(&(png_ptr->row_info), png_ptr->row_buf + 1);
-#endif
-
-#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
-   if (png_ptr->transformations & PNG_USER_TRANSFORM)
-    {
-      if(png_ptr->read_user_transform_fn != NULL)
-        (*(png_ptr->read_user_transform_fn)) /* user read transform function */
-          (png_ptr,                    /* png_ptr */
-           &(png_ptr->row_info),       /* row_info:     */
-             /*  png_uint_32 width;          width of row */
-             /*  png_uint_32 rowbytes;       number of bytes in row */
-             /*  png_byte color_type;        color type of pixels */
-             /*  png_byte bit_depth;         bit depth of samples */
-             /*  png_byte channels;          number of channels (1-4) */
-             /*  png_byte pixel_depth;       bits per pixel (depth*channels) */
-           png_ptr->row_buf + 1);      /* start of pixel data for row */
-#if defined(PNG_USER_TRANSFORM_PTR_SUPPORTED)
-      if(png_ptr->user_transform_depth)
-         png_ptr->row_info.bit_depth = png_ptr->user_transform_depth;
-      if(png_ptr->user_transform_channels)
-         png_ptr->row_info.channels = png_ptr->user_transform_channels;
-#endif
-      png_ptr->row_info.pixel_depth = (png_byte)(png_ptr->row_info.bit_depth *
-         png_ptr->row_info.channels);
-      png_ptr->row_info.rowbytes = PNG_ROWBYTES(png_ptr->row_info.pixel_depth,
-         png_ptr->row_info.width);
-   }
-#endif
-
-}
-
-#if defined(PNG_READ_PACK_SUPPORTED)
-/* Unpack pixels of 1, 2, or 4 bits per pixel into 1 byte per pixel,
- * without changing the actual values.  Thus, if you had a row with
- * a bit depth of 1, you would end up with bytes that only contained
- * the numbers 0 or 1.  If you would rather they contain 0 and 255, use
- * png_do_shift() after this.
- */
-void /* PRIVATE */
-png_do_unpack(png_row_infop row_info, png_bytep row)
-{
-   png_debug(1, "in png_do_unpack\n");
-#if defined(PNG_USELESS_TESTS_SUPPORTED)
-   if (row != NULL && row_info != NULL && row_info->bit_depth < 8)
-#else
-   if (row_info->bit_depth < 8)
-#endif
-   {
-      png_uint_32 i;
-      png_uint_32 row_width=row_info->width;
-
-      switch (row_info->bit_depth)
-      {
-         case 1:
-         {
-            png_bytep sp = row + (png_size_t)((row_width - 1) >> 3);
-            png_bytep dp = row + (png_size_t)row_width - 1;
-            png_uint_32 shift = 7 - (int)((row_width + 7) & 0x07);
-            for (i = 0; i < row_width; i++)
-            {
-               *dp = (png_byte)((*sp >> shift) & 0x01);
-               if (shift == 7)
-               {
-                  shift = 0;
-                  sp--;
-               }
-               else
-                  shift++;
-
-               dp--;
-            }
-            break;
-         }
-         case 2:
-         {
-
-            png_bytep sp = row + (png_size_t)((row_width - 1) >> 2);
-            png_bytep dp = row + (png_size_t)row_width - 1;
-            png_uint_32 shift = (int)((3 - ((row_width + 3) & 0x03)) << 1);
-            for (i = 0; i < row_width; i++)
-            {
-               *dp = (png_byte)((*sp >> shift) & 0x03);
-               if (shift == 6)
-               {
-                  shift = 0;
-                  sp--;
-               }
-               else
-                  shift += 2;
-
-               dp--;
-            }
-            break;
-         }
-         case 4:
-         {
-            png_bytep sp = row + (png_size_t)((row_width - 1) >> 1);
-            png_bytep dp = row + (png_size_t)row_width - 1;
-            png_uint_32 shift = (int)((1 - ((row_width + 1) & 0x01)) << 2);
-            for (i = 0; i < row_width; i++)
-            {
-               *dp = (png_byte)((*sp >> shift) & 0x0f);
-               if (shift == 4)
-               {
-                  shift = 0;
-                  sp--;
-               }
-               else
-                  shift = 4;
-
-               dp--;
-            }
-            break;
-         }
-      }
-      row_info->bit_depth = 8;
-      row_info->pixel_depth = (png_byte)(8 * row_info->channels);
-      row_info->rowbytes = row_width * row_info->channels;
-   }
-}
-#endif
-
-#if defined(PNG_READ_SHIFT_SUPPORTED)
-/* Reverse the effects of png_do_shift.  This routine merely shifts the
- * pixels back to their significant bits values.  Thus, if you have
- * a row of bit depth 8, but only 5 are significant, this will shift
- * the values back to 0 through 31.
- */
-void /* PRIVATE */
-png_do_unshift(png_row_infop row_info, png_bytep row, png_color_8p sig_bits)
-{
-   png_debug(1, "in png_do_unshift\n");
-   if (
-#if defined(PNG_USELESS_TESTS_SUPPORTED)
-       row != NULL && row_info != NULL && sig_bits != NULL &&
-#endif
-       row_info->color_type != PNG_COLOR_TYPE_PALETTE)
-   {
-      int shift[4];
-      int channels = 0;
-      int c;
-      png_uint_16 value = 0;
-      png_uint_32 row_width = row_info->width;
-
-      if (row_info->color_type & PNG_COLOR_MASK_COLOR)
-      {
-         shift[channels++] = row_info->bit_depth - sig_bits->red;
-         shift[channels++] = row_info->bit_depth - sig_bits->green;
-         shift[channels++] = row_info->bit_depth - sig_bits->blue;
-      }
-      else
-      {
-         shift[channels++] = row_info->bit_depth - sig_bits->gray;
-      }
-      if (row_info->color_type & PNG_COLOR_MASK_ALPHA)
-      {
-         shift[channels++] = row_info->bit_depth - sig_bits->alpha;
-      }
-
-      for (c = 0; c < channels; c++)
-      {
-         if (shift[c] <= 0)
-            shift[c] = 0;
-         else
-            value = 1;
-      }
-
-      if (!value)
-         return;
-
-      switch (row_info->bit_depth)
-      {
-         case 2:
-         {
-            png_bytep bp;
-            png_uint_32 i;
-            png_uint_32 istop = row_info->rowbytes;
-
-            for (bp = row, i = 0; i < istop; i++)
-            {
-               *bp >>= 1;
-               *bp++ &= 0x55;
-            }
-            break;
-         }
-         case 4:
-         {
-            png_bytep bp = row;
-            png_uint_32 i;
-            png_uint_32 istop = row_info->rowbytes;
-            png_byte mask = (png_byte)((((int)0xf0 >> shift[0]) & (int)0xf0) |
-               (png_byte)((int)0xf >> shift[0]));
-
-            for (i = 0; i < istop; i++)
-            {
-               *bp >>= shift[0];
-               *bp++ &= mask;
-            }
-            break;
-         }
-         case 8:
-         {
-            png_bytep bp = row;
-            png_uint_32 i;
-            png_uint_32 istop = row_width * channels;
-
-            for (i = 0; i < istop; i++)
-            {
-               *bp++ >>= shift[i%channels];
-            }
-            break;
-         }
-         case 16:
-         {
-            png_bytep bp = row;
-            png_uint_32 i;
-            png_uint_32 istop = channels * row_width;
-
-            for (i = 0; i < istop; i++)
-            {
-               value = (png_uint_16)((*bp << 8) + *(bp + 1));
-               value >>= shift[i%channels];
-               *bp++ = (png_byte)(value >> 8);
-               *bp++ = (png_byte)(value & 0xff);
-            }
-            break;
-         }
-      }
-   }
-}
-#endif
-
-#if defined(PNG_READ_16_TO_8_SUPPORTED)
-/* chop rows of bit depth 16 down to 8 */
-void /* PRIVATE */
-png_do_chop(png_row_infop row_info, png_bytep row)
-{
-   png_debug(1, "in png_do_chop\n");
-#if defined(PNG_USELESS_TESTS_SUPPORTED)
-   if (row != NULL && row_info != NULL && row_info->bit_depth == 16)
-#else
-   if (row_info->bit_depth == 16)
-#endif
-   {
-      png_bytep sp = row;
-      png_bytep dp = row;
-      png_uint_32 i;
-      png_uint_32 istop = row_info->width * row_info->channels;
-
-      for (i = 0; i<istop; i++, sp += 2, dp++)
-      {
-#if defined(PNG_READ_16_TO_8_ACCURATE_SCALE_SUPPORTED)
-      /* This does a more accurate scaling of the 16-bit color
-       * value, rather than a simple low-byte truncation.
-       *
-       * What the ideal calculation should be:
-       *   *dp = (((((png_uint_32)(*sp) << 8) |
-       *          (png_uint_32)(*(sp + 1))) * 255 + 127) / (png_uint_32)65535L;
-       *
-       * GRR: no, I think this is what it really should be:
-       *   *dp = (((((png_uint_32)(*sp) << 8) |
-       *           (png_uint_32)(*(sp + 1))) + 128L) / (png_uint_32)257L;
-       *
-       * GRR: here's the exact calculation with shifts:
-       *   temp = (((png_uint_32)(*sp) << 8) | (png_uint_32)(*(sp + 1))) + 128L;
-       *   *dp = (temp - (temp >> 8)) >> 8;
-       *
-       * Approximate calculation with shift/add instead of multiply/divide:
-       *   *dp = ((((png_uint_32)(*sp) << 8) |
-       *          (png_uint_32)((int)(*(sp + 1)) - *sp)) + 128) >> 8;
-       *
-       * What we actually do to avoid extra shifting and conversion:
-       */
-
-         *dp = *sp + ((((int)(*(sp + 1)) - *sp) > 128) ? 1 : 0);
-#else
-       /* Simply discard the low order byte */
-         *dp = *sp;
-#endif
-      }
-      row_info->bit_depth = 8;
-      row_info->pixel_depth = (png_byte)(8 * row_info->channels);
-      row_info->rowbytes = row_info->width * row_info->channels;
-   }
-}
-#endif
-
-#if defined(PNG_READ_SWAP_ALPHA_SUPPORTED)
-void /* PRIVATE */
-png_do_read_swap_alpha(png_row_infop row_info, png_bytep row)
-{
-   png_debug(1, "in png_do_read_swap_alpha\n");
-#if defined(PNG_USELESS_TESTS_SUPPORTED)
-   if (row != NULL && row_info != NULL)
-#endif
-   {
-      png_uint_32 row_width = row_info->width;
-      if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
-      {
-         /* This converts from RGBA to ARGB */
-         if (row_info->bit_depth == 8)
-         {
-            png_bytep sp = row + row_info->rowbytes;
-            png_bytep dp = sp;
-            png_byte save;
-            png_uint_32 i;
-
-            for (i = 0; i < row_width; i++)
-            {
-               save = *(--sp);
-               *(--dp) = *(--sp);
-               *(--dp) = *(--sp);
-               *(--dp) = *(--sp);
-               *(--dp) = save;
-            }
-         }
-         /* This converts from RRGGBBAA to AARRGGBB */
-         else
-         {
-            png_bytep sp = row + row_info->rowbytes;
-            png_bytep dp = sp;
-            png_byte save[2];
-            png_uint_32 i;
-
-            for (i = 0; i < row_width; i++)
-            {
-               save[0] = *(--sp);
-               save[1] = *(--sp);
-               *(--dp) = *(--sp);
-               *(--dp) = *(--sp);
-               *(--dp) = *(--sp);
-               *(--dp) = *(--sp);
-               *(--dp) = *(--sp);
-               *(--dp) = *(--sp);
-               *(--dp) = save[0];
-               *(--dp) = save[1];
-            }
-         }
-      }
-      else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
-      {
-         /* This converts from GA to AG */
-         if (row_info->bit_depth == 8)
-         {
-            png_bytep sp = row + row_info->rowbytes;
-            png_bytep dp = sp;
-            png_byte save;
-            png_uint_32 i;
-
-            for (i = 0; i < row_width; i++)
-            {
-               save = *(--sp);
-               *(--dp) = *(--sp);
-               *(--dp) = save;
-            }
-         }
-         /* This converts from GGAA to AAGG */
-         else
-         {
-            png_bytep sp = row + row_info->rowbytes;
-            png_bytep dp = sp;
-            png_byte save[2];
-            png_uint_32 i;
-
-            for (i = 0; i < row_width; i++)
-            {
-               save[0] = *(--sp);
-               save[1] = *(--sp);
-               *(--dp) = *(--sp);
-               *(--dp) = *(--sp);
-               *(--dp) = save[0];
-               *(--dp) = save[1];
-            }
-         }
-      }
-   }
-}
-#endif
-
-#if defined(PNG_READ_INVERT_ALPHA_SUPPORTED)
-void /* PRIVATE */
-png_do_read_invert_alpha(png_row_infop row_info, png_bytep row)
-{
-   png_debug(1, "in png_do_read_invert_alpha\n");
-#if defined(PNG_USELESS_TESTS_SUPPORTED)
-   if (row != NULL && row_info != NULL)
-#endif
-   {
-      png_uint_32 row_width = row_info->width;
-      if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
-      {
-         /* This inverts the alpha channel in RGBA */
-         if (row_info->bit_depth == 8)
-         {
-            png_bytep sp = row + row_info->rowbytes;
-            png_bytep dp = sp;
-            png_uint_32 i;
-
-            for (i = 0; i < row_width; i++)
-            {
-               *(--dp) = (png_byte)(255 - *(--sp));
-
-/*             This does nothing:
-               *(--dp) = *(--sp);
-               *(--dp) = *(--sp);
-               *(--dp) = *(--sp);
-               We can replace it with:
-*/
-               sp-=3;
-               dp=sp;
-            }
-         }
-         /* This inverts the alpha channel in RRGGBBAA */
-         else
-         {
-            png_bytep sp = row + row_info->rowbytes;
-            png_bytep dp = sp;
-            png_uint_32 i;
-
-            for (i = 0; i < row_width; i++)
-            {
-               *(--dp) = (png_byte)(255 - *(--sp));
-               *(--dp) = (png_byte)(255 - *(--sp));
-
-/*             This does nothing:
-               *(--dp) = *(--sp);
-               *(--dp) = *(--sp);
-               *(--dp) = *(--sp);
-               *(--dp) = *(--sp);
-               *(--dp) = *(--sp);
-               *(--dp) = *(--sp);
-               We can replace it with:
-*/
-               sp-=6;
-               dp=sp;
-            }
-         }
-      }
-      else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
-      {
-         /* This inverts the alpha channel in GA */
-         if (row_info->bit_depth == 8)
-         {
-            png_bytep sp = row + row_info->rowbytes;
-            png_bytep dp = sp;
-            png_uint_32 i;
-
-            for (i = 0; i < row_width; i++)
-            {
-               *(--dp) = (png_byte)(255 - *(--sp));
-               *(--dp) = *(--sp);
-            }
-         }
-         /* This inverts the alpha channel in GGAA */
-         else
-         {
-            png_bytep sp  = row + row_info->rowbytes;
-            png_bytep dp = sp;
-            png_uint_32 i;
-
-            for (i = 0; i < row_width; i++)
-            {
-               *(--dp) = (png_byte)(255 - *(--sp));
-               *(--dp) = (png_byte)(255 - *(--sp));
-/*
-               *(--dp) = *(--sp);
-               *(--dp) = *(--sp);
-*/
-               sp-=2;
-               dp=sp;
-            }
-         }
-      }
-   }
-}
-#endif
-
-#if defined(PNG_READ_FILLER_SUPPORTED)
-/* Add filler channel if we have RGB color */
-void /* PRIVATE */
-png_do_read_filler(png_row_infop row_info, png_bytep row,
-   png_uint_32 filler, png_uint_32 flags)
-{
-   png_uint_32 i;
-   png_uint_32 row_width = row_info->width;
-
-   png_byte hi_filler = (png_byte)((filler>>8) & 0xff);
-   png_byte lo_filler = (png_byte)(filler & 0xff);
-
-   png_debug(1, "in png_do_read_filler\n");
-   if (
-#if defined(PNG_USELESS_TESTS_SUPPORTED)
-       row != NULL  && row_info != NULL &&
-#endif
-       row_info->color_type == PNG_COLOR_TYPE_GRAY)
-   {
-      if(row_info->bit_depth == 8)
-      {
-         /* This changes the data from G to GX */
-         if (flags & PNG_FLAG_FILLER_AFTER)
-         {
-            png_bytep sp = row + (png_size_t)row_width;
-            png_bytep dp =  sp + (png_size_t)row_width;
-            for (i = 1; i < row_width; i++)
-            {
-               *(--dp) = lo_filler;
-               *(--dp) = *(--sp);
-            }
-            *(--dp) = lo_filler;
-            row_info->channels = 2;
-            row_info->pixel_depth = 16;
-            row_info->rowbytes = row_width * 2;
-         }
-      /* This changes the data from G to XG */
-         else
-         {
-            png_bytep sp = row + (png_size_t)row_width;
-            png_bytep dp = sp  + (png_size_t)row_width;
-            for (i = 0; i < row_width; i++)
-            {
-               *(--dp) = *(--sp);
-               *(--dp) = lo_filler;
-            }
-            row_info->channels = 2;
-            row_info->pixel_depth = 16;
-            row_info->rowbytes = row_width * 2;
-         }
-      }
-      else if(row_info->bit_depth == 16)
-      {
-         /* This changes the data from GG to GGXX */
-         if (flags & PNG_FLAG_FILLER_AFTER)
-         {
-            png_bytep sp = row + (png_size_t)row_width * 2;
-            png_bytep dp = sp  + (png_size_t)row_width * 2;
-            for (i = 1; i < row_width; i++)
-            {
-               *(--dp) = hi_filler;
-               *(--dp) = lo_filler;
-               *(--dp) = *(--sp);
-               *(--dp) = *(--sp);
-            }
-            *(--dp) = hi_filler;
-            *(--dp) = lo_filler;
-            row_info->channels = 2;
-            row_info->pixel_depth = 32;
-            row_info->rowbytes = row_width * 4;
-         }
-         /* This changes the data from GG to XXGG */
-         else
-         {
-            png_bytep sp = row + (png_size_t)row_width * 2;
-            png_bytep dp = sp  + (png_size_t)row_width * 2;
-            for (i = 0; i < row_width; i++)
-            {
-               *(--dp) = *(--sp);
-               *(--dp) = *(--sp);
-               *(--dp) = hi_filler;
-               *(--dp) = lo_filler;
-            }
-            row_info->channels = 2;
-            row_info->pixel_depth = 32;
-            row_info->rowbytes = row_width * 4;
-         }
-      }
-   } /* COLOR_TYPE == GRAY */
-   else if (row_info->color_type == PNG_COLOR_TYPE_RGB)
-   {
-      if(row_info->bit_depth == 8)
-      {
-         /* This changes the data from RGB to RGBX */
-         if (flags & PNG_FLAG_FILLER_AFTER)
-         {
-            png_bytep sp = row + (png_size_t)row_width * 3;
-            png_bytep dp = sp  + (png_size_t)row_width;
-            for (i = 1; i < row_width; i++)
-            {
-               *(--dp) = lo_filler;
-               *(--dp) = *(--sp);
-               *(--dp) = *(--sp);
-               *(--dp) = *(--sp);
-            }
-            *(--dp) = lo_filler;
-            row_info->channels = 4;
-            row_info->pixel_depth = 32;
-            row_info->rowbytes = row_width * 4;
-         }
-      /* This changes the data from RGB to XRGB */
-         else
-         {
-            png_bytep sp = row + (png_size_t)row_width * 3;
-            png_bytep dp = sp + (png_size_t)row_width;
-            for (i = 0; i < row_width; i++)
-            {
-               *(--dp) = *(--sp);
-               *(--dp) = *(--sp);
-               *(--dp) = *(--sp);
-               *(--dp) = lo_filler;
-            }
-            row_info->channels = 4;
-            row_info->pixel_depth = 32;
-            row_info->rowbytes = row_width * 4;
-         }
-      }
-      else if(row_info->bit_depth == 16)
-      {
-         /* This changes the data from RRGGBB to RRGGBBXX */
-         if (flags & PNG_FLAG_FILLER_AFTER)
-         {
-            png_bytep sp = row + (png_size_t)row_width * 6;
-            png_bytep dp = sp  + (png_size_t)row_width * 2;
-            for (i = 1; i < row_width; i++)
-            {
-               *(--dp) = hi_filler;
-               *(--dp) = lo_filler;
-               *(--dp) = *(--sp);
-               *(--dp) = *(--sp);
-               *(--dp) = *(--sp);
-               *(--dp) = *(--sp);
-               *(--dp) = *(--sp);
-               *(--dp) = *(--sp);
-            }
-            *(--dp) = hi_filler;
-            *(--dp) = lo_filler;
-            row_info->channels = 4;
-            row_info->pixel_depth = 64;
-            row_info->rowbytes = row_width * 8;
-         }
-         /* This changes the data from RRGGBB to XXRRGGBB */
-         else
-         {
-            png_bytep sp = row + (png_size_t)row_width * 6;
-            png_bytep dp = sp  + (png_size_t)row_width * 2;
-            for (i = 0; i < row_width; i++)
-            {
-               *(--dp) = *(--sp);
-               *(--dp) = *(--sp);
-               *(--dp) = *(--sp);
-               *(--dp) = *(--sp);
-               *(--dp) = *(--sp);
-               *(--dp) = *(--sp);
-               *(--dp) = hi_filler;
-               *(--dp) = lo_filler;
-            }
-            row_info->channels = 4;
-            row_info->pixel_depth = 64;
-            row_info->rowbytes = row_width * 8;
-         }
-      }
-   } /* COLOR_TYPE == RGB */
-}
-#endif
-
-#if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED)
-/* expand grayscale files to RGB, with or without alpha */
-void /* PRIVATE */
-png_do_gray_to_rgb(png_row_infop row_info, png_bytep row)
-{
-   png_uint_32 i;
-   png_uint_32 row_width = row_info->width;
-
-   png_debug(1, "in png_do_gray_to_rgb\n");
-   if (row_info->bit_depth >= 8 &&
-#if defined(PNG_USELESS_TESTS_SUPPORTED)
-       row != NULL && row_info != NULL &&
-#endif
-      !(row_info->color_type & PNG_COLOR_MASK_COLOR))
-   {
-      if (row_info->color_type == PNG_COLOR_TYPE_GRAY)
-      {
-         if (row_info->bit_depth == 8)
-         {
-            png_bytep sp = row + (png_size_t)row_width - 1;
-            png_bytep dp = sp  + (png_size_t)row_width * 2;
-            for (i = 0; i < row_width; i++)
-            {
-               *(dp--) = *sp;
-               *(dp--) = *sp;
-               *(dp--) = *(sp--);
-            }
-         }
-         else
-         {
-            png_bytep sp = row + (png_size_t)row_width * 2 - 1;
-            png_bytep dp = sp  + (png_size_t)row_width * 4;
-            for (i = 0; i < row_width; i++)
-            {
-               *(dp--) = *sp;
-               *(dp--) = *(sp - 1);
-               *(dp--) = *sp;
-               *(dp--) = *(sp - 1);
-               *(dp--) = *(sp--);
-               *(dp--) = *(sp--);
-            }
-         }
-      }
-      else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
-      {
-         if (row_info->bit_depth == 8)
-         {
-            png_bytep sp = row + (png_size_t)row_width * 2 - 1;
-            png_bytep dp = sp  + (png_size_t)row_width * 2;
-            for (i = 0; i < row_width; i++)
-            {
-               *(dp--) = *(sp--);
-               *(dp--) = *sp;
-               *(dp--) = *sp;
-               *(dp--) = *(sp--);
-            }
-         }
-         else
-         {
-            png_bytep sp = row + (png_size_t)row_width * 4 - 1;
-            png_bytep dp = sp  + (png_size_t)row_width * 4;
-            for (i = 0; i < row_width; i++)
-            {
-               *(dp--) = *(sp--);
-               *(dp--) = *(sp--);
-               *(dp--) = *sp;
-               *(dp--) = *(sp - 1);
-               *(dp--) = *sp;
-               *(dp--) = *(sp - 1);
-               *(dp--) = *(sp--);
-               *(dp--) = *(sp--);
-            }
-         }
-      }
-      row_info->channels += (png_byte)2;
-      row_info->color_type |= PNG_COLOR_MASK_COLOR;
-      row_info->pixel_depth = (png_byte)(row_info->channels *
-         row_info->bit_depth);
-      row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,row_width);
-   }
-}
-#endif
-
-#if defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
-/* reduce RGB files to grayscale, with or without alpha
- * using the equation given in Poynton's ColorFAQ at
- * <http://www.inforamp.net/~poynton/>
- * Copyright (c) 1998-01-04 Charles Poynton poynton at inforamp.net
- *
- *     Y = 0.212671 * R + 0.715160 * G + 0.072169 * B
- *
- *  We approximate this with
- *
- *     Y = 0.21268 * R    + 0.7151 * G    + 0.07217 * B
- *
- *  which can be expressed with integers as
- *
- *     Y = (6969 * R + 23434 * G + 2365 * B)/32768
- *
- *  The calculation is to be done in a linear colorspace.
- *
- *  Other integer coefficents can be used via png_set_rgb_to_gray().
- */
-int /* PRIVATE */
-png_do_rgb_to_gray(png_structp png_ptr, png_row_infop row_info, png_bytep row)
-
-{
-   png_uint_32 i;
-
-   png_uint_32 row_width = row_info->width;
-   int rgb_error = 0;
-
-   png_debug(1, "in png_do_rgb_to_gray\n");
-   if (
-#if defined(PNG_USELESS_TESTS_SUPPORTED)
-       row != NULL && row_info != NULL &&
-#endif
-      (row_info->color_type & PNG_COLOR_MASK_COLOR))
-   {
-      png_uint_32 rc = png_ptr->rgb_to_gray_red_coeff;
-      png_uint_32 gc = png_ptr->rgb_to_gray_green_coeff;
-      png_uint_32 bc = png_ptr->rgb_to_gray_blue_coeff;
-
-      if (row_info->color_type == PNG_COLOR_TYPE_RGB)
-      {
-         if (row_info->bit_depth == 8)
-         {
-#if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
-            if (png_ptr->gamma_from_1 != NULL && png_ptr->gamma_to_1 != NULL)
-            {
-               png_bytep sp = row;
-               png_bytep dp = row;
-
-               for (i = 0; i < row_width; i++)
-               {
-                  png_byte red   = png_ptr->gamma_to_1[*(sp++)];
-                  png_byte green = png_ptr->gamma_to_1[*(sp++)];
-                  png_byte blue  = png_ptr->gamma_to_1[*(sp++)];
-                  if(red != green || red != blue)
-                  {
-                     rgb_error |= 1;
-                     *(dp++) = png_ptr->gamma_from_1[
-                       (rc*red+gc*green+bc*blue)>>15];
-                  }
-                  else
-                     *(dp++) = *(sp-1);
-               }
-            }
-            else
-#endif
-            {
-               png_bytep sp = row;
-               png_bytep dp = row;
-               for (i = 0; i < row_width; i++)
-               {
-                  png_byte red   = *(sp++);
-                  png_byte green = *(sp++);
-                  png_byte blue  = *(sp++);
-                  if(red != green || red != blue)
-                  {
-                     rgb_error |= 1;
-                     *(dp++) = (png_byte)((rc*red+gc*green+bc*blue)>>15);
-                  }
-                  else
-                     *(dp++) = *(sp-1);
-               }
-            }
-         }
-
-         else /* RGB bit_depth == 16 */
-         {
-#if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
-            if (png_ptr->gamma_16_to_1 != NULL &&
-                png_ptr->gamma_16_from_1 != NULL)
-            {
-               png_bytep sp = row;
-               png_bytep dp = row;
-               for (i = 0; i < row_width; i++)
-               {
-                  png_uint_16 red, green, blue, w;
-
-                  red   = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2;
-                  green = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2;
-                  blue  = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2;
-
-                  if(red == green && red == blue)
-                     w = red;
-                  else
-                  {
-                     png_uint_16 red_1   = png_ptr->gamma_16_to_1[(red&0xff) >>
-                                  png_ptr->gamma_shift][red>>8];
-                     png_uint_16 green_1 = png_ptr->gamma_16_to_1[(green&0xff) >>
-                                  png_ptr->gamma_shift][green>>8];
-                     png_uint_16 blue_1  = png_ptr->gamma_16_to_1[(blue&0xff) >>
-                                  png_ptr->gamma_shift][blue>>8];
-                     png_uint_16 gray16  = (png_uint_16)((rc*red_1 + gc*green_1
-                                  + bc*blue_1)>>15);
-                     w = png_ptr->gamma_16_from_1[(gray16&0xff) >>
-                         png_ptr->gamma_shift][gray16 >> 8];
-                     rgb_error |= 1;
-                  }
-
-                  *(dp++) = (png_byte)((w>>8) & 0xff);
-                  *(dp++) = (png_byte)(w & 0xff);
-               }
-            }
-            else
-#endif
-            {
-               png_bytep sp = row;
-               png_bytep dp = row;
-               for (i = 0; i < row_width; i++)
-               {
-                  png_uint_16 red, green, blue, gray16;
-
-                  red   = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2;
-                  green = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2;
-                  blue  = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2;
-
-                  if(red != green || red != blue)
-                     rgb_error |= 1;
-                  gray16  = (png_uint_16)((rc*red + gc*green + bc*blue)>>15);
-                  *(dp++) = (png_byte)((gray16>>8) & 0xff);
-                  *(dp++) = (png_byte)(gray16 & 0xff);
-               }
-            }
-         }
-      }
-      if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
-      {
-         if (row_info->bit_depth == 8)
-         {
-#if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
-            if (png_ptr->gamma_from_1 != NULL && png_ptr->gamma_to_1 != NULL)
-            {
-               png_bytep sp = row;
-               png_bytep dp = row;
-               for (i = 0; i < row_width; i++)
-               {
-                  png_byte red   = png_ptr->gamma_to_1[*(sp++)];
-                  png_byte green = png_ptr->gamma_to_1[*(sp++)];
-                  png_byte blue  = png_ptr->gamma_to_1[*(sp++)];
-                  if(red != green || red != blue)
-                     rgb_error |= 1;
-                  *(dp++) =  png_ptr->gamma_from_1
-                             [(rc*red + gc*green + bc*blue)>>15];
-                  *(dp++) = *(sp++);  /* alpha */
-               }
-            }
-            else
-#endif
-            {
-               png_bytep sp = row;
-               png_bytep dp = row;
-               for (i = 0; i < row_width; i++)
-               {
-                  png_byte red   = *(sp++);
-                  png_byte green = *(sp++);
-                  png_byte blue  = *(sp++);
-                  if(red != green || red != blue)
-                     rgb_error |= 1;
-                  *(dp++) =  (png_byte)((rc*red + gc*green + bc*blue)>>15);
-                  *(dp++) = *(sp++);  /* alpha */
-               }
-            }
-         }
-         else /* RGBA bit_depth == 16 */
-         {
-#if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
-            if (png_ptr->gamma_16_to_1 != NULL &&
-                png_ptr->gamma_16_from_1 != NULL)
-            {
-               png_bytep sp = row;
-               png_bytep dp = row;
-               for (i = 0; i < row_width; i++)
-               {
-                  png_uint_16 red, green, blue, w;
-
-                  red   = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2;
-                  green = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2;
-                  blue  = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2;
-
-                  if(red == green && red == blue)
-                     w = red;
-                  else
-                  {
-                     png_uint_16 red_1   = png_ptr->gamma_16_to_1[(red&0xff) >>
-                                  png_ptr->gamma_shift][red>>8];
-                     png_uint_16 green_1 = png_ptr->gamma_16_to_1[(green&0xff) >>
-                                  png_ptr->gamma_shift][green>>8];
-                     png_uint_16 blue_1  = png_ptr->gamma_16_to_1[(blue&0xff) >>
-                                  png_ptr->gamma_shift][blue>>8];
-                     png_uint_16 gray16  = (png_uint_16)((rc * red_1
-                                  + gc * green_1 + bc * blue_1)>>15);
-                     w = png_ptr->gamma_16_from_1[(gray16&0xff) >>
-                         png_ptr->gamma_shift][gray16 >> 8];
-                     rgb_error |= 1;
-                  }
-
-                  *(dp++) = (png_byte)((w>>8) & 0xff);
-                  *(dp++) = (png_byte)(w & 0xff);
-                  *(dp++) = *(sp++);  /* alpha */
-                  *(dp++) = *(sp++);
-               }
-            }
-            else
-#endif
-            {
-               png_bytep sp = row;
-               png_bytep dp = row;
-               for (i = 0; i < row_width; i++)
-               {
-                  png_uint_16 red, green, blue, gray16;
-                  red   = (png_uint_16)((*(sp)<<8) | *(sp+1)); sp+=2;
-                  green = (png_uint_16)((*(sp)<<8) | *(sp+1)); sp+=2;
-                  blue  = (png_uint_16)((*(sp)<<8) | *(sp+1)); sp+=2;
-                  if(red != green || red != blue)
-                     rgb_error |= 1;
-                  gray16  = (png_uint_16)((rc*red + gc*green + bc*blue)>>15);
-                  *(dp++) = (png_byte)((gray16>>8) & 0xff);
-                  *(dp++) = (png_byte)(gray16 & 0xff);
-                  *(dp++) = *(sp++);  /* alpha */
-                  *(dp++) = *(sp++);
-               }
-            }
-         }
-      }
-   row_info->channels -= (png_byte)2;
-      row_info->color_type &= ~PNG_COLOR_MASK_COLOR;
-      row_info->pixel_depth = (png_byte)(row_info->channels *
-         row_info->bit_depth);
-      row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,row_width);
-   }
-   return rgb_error;
-}
-#endif
-
-/* Build a grayscale palette.  Palette is assumed to be 1 << bit_depth
- * large of png_color.  This lets grayscale images be treated as
- * paletted.  Most useful for gamma correction and simplification
- * of code.
- */
-void PNGAPI
-png_build_grayscale_palette(int bit_depth, png_colorp palette)
-{
-   int num_palette;
-   int color_inc;
-   int i;
-   int v;
-
-   png_debug(1, "in png_do_build_grayscale_palette\n");
-   if (palette == NULL)
-      return;
-
-   switch (bit_depth)
-   {
-      case 1:
-         num_palette = 2;
-         color_inc = 0xff;
-         break;
-      case 2:
-         num_palette = 4;
-         color_inc = 0x55;
-         break;
-      case 4:
-         num_palette = 16;
-         color_inc = 0x11;
-         break;
-      case 8:
-         num_palette = 256;
-         color_inc = 1;
-         break;
-      default:
-         num_palette = 0;
-         color_inc = 0;
-         break;
-   }
-
-   for (i = 0, v = 0; i < num_palette; i++, v += color_inc)
-   {
-      palette[i].red = (png_byte)v;
-      palette[i].green = (png_byte)v;
-      palette[i].blue = (png_byte)v;
-   }
-}
-
-/* This function is currently unused.  Do we really need it? */
-#if defined(PNG_READ_DITHER_SUPPORTED) && defined(PNG_CORRECT_PALETTE_SUPPORTED)
-void /* PRIVATE */
-png_correct_palette(png_structp png_ptr, png_colorp palette,
-   int num_palette)
-{
-   png_debug(1, "in png_correct_palette\n");
-#if defined(PNG_READ_BACKGROUND_SUPPORTED) && \
-    defined(PNG_READ_GAMMA_SUPPORTED) && defined(PNG_FLOATING_POINT_SUPPORTED)
-   if (png_ptr->transformations & (PNG_GAMMA | PNG_BACKGROUND))
-   {
-      png_color back, back_1;
-
-      if (png_ptr->background_gamma_type == PNG_BACKGROUND_GAMMA_FILE)
-      {
-         back.red = png_ptr->gamma_table[png_ptr->background.red];
-         back.green = png_ptr->gamma_table[png_ptr->background.green];
-         back.blue = png_ptr->gamma_table[png_ptr->background.blue];
-
-         back_1.red = png_ptr->gamma_to_1[png_ptr->background.red];
-         back_1.green = png_ptr->gamma_to_1[png_ptr->background.green];
-         back_1.blue = png_ptr->gamma_to_1[png_ptr->background.blue];
-      }
-      else
-      {
-         double g;
-
-         g = 1.0 / (png_ptr->background_gamma * png_ptr->screen_gamma);
-
-         if (png_ptr->background_gamma_type == PNG_BACKGROUND_GAMMA_SCREEN ||
-             fabs(g - 1.0) < PNG_GAMMA_THRESHOLD)
-         {
-            back.red = png_ptr->background.red;
-            back.green = png_ptr->background.green;
-            back.blue = png_ptr->background.blue;
-         }
-         else
-         {
-            back.red =
-               (png_byte)(pow((double)png_ptr->background.red/255, g) *
-                255.0 + 0.5);
-            back.green =
-               (png_byte)(pow((double)png_ptr->background.green/255, g) *
-                255.0 + 0.5);
-            back.blue =
-               (png_byte)(pow((double)png_ptr->background.blue/255, g) *
-                255.0 + 0.5);
-         }
-
-         g = 1.0 / png_ptr->background_gamma;
-
-         back_1.red =
-            (png_byte)(pow((double)png_ptr->background.red/255, g) *
-             255.0 + 0.5);
-         back_1.green =
-            (png_byte)(pow((double)png_ptr->background.green/255, g) *
-             255.0 + 0.5);
-         back_1.blue =
-            (png_byte)(pow((double)png_ptr->background.blue/255, g) *
-             255.0 + 0.5);
-      }
-
-      if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
-      {
-         png_uint_32 i;
-
-         for (i = 0; i < (png_uint_32)num_palette; i++)
-         {
-            if (i < png_ptr->num_trans && png_ptr->trans[i] == 0)
-            {
-               palette[i] = back;
-            }
-            else if (i < png_ptr->num_trans && png_ptr->trans[i] != 0xff)
-            {
-               png_byte v, w;
-
-               v = png_ptr->gamma_to_1[png_ptr->palette[i].red];
-               png_composite(w, v, png_ptr->trans[i], back_1.red);
-               palette[i].red = png_ptr->gamma_from_1[w];
-
-               v = png_ptr->gamma_to_1[png_ptr->palette[i].green];
-               png_composite(w, v, png_ptr->trans[i], back_1.green);
-               palette[i].green = png_ptr->gamma_from_1[w];
-
-               v = png_ptr->gamma_to_1[png_ptr->palette[i].blue];
-               png_composite(w, v, png_ptr->trans[i], back_1.blue);
-               palette[i].blue = png_ptr->gamma_from_1[w];
-            }
-            else
-            {
-               palette[i].red = png_ptr->gamma_table[palette[i].red];
-               palette[i].green = png_ptr->gamma_table[palette[i].green];
-               palette[i].blue = png_ptr->gamma_table[palette[i].blue];
-            }
-         }
-      }
-      else
-      {
-         int i;
-
-         for (i = 0; i < num_palette; i++)
-         {
-            if (palette[i].red == (png_byte)png_ptr->trans_values.gray)
-            {
-               palette[i] = back;
-            }
-            else
-            {
-               palette[i].red = png_ptr->gamma_table[palette[i].red];
-               palette[i].green = png_ptr->gamma_table[palette[i].green];
-               palette[i].blue = png_ptr->gamma_table[palette[i].blue];
-            }
-         }
-      }
-   }
-   else
-#endif
-#if defined(PNG_READ_GAMMA_SUPPORTED)
-   if (png_ptr->transformations & PNG_GAMMA)
-   {
-      int i;
-
-      for (i = 0; i < num_palette; i++)
-      {
-         palette[i].red = png_ptr->gamma_table[palette[i].red];
-         palette[i].green = png_ptr->gamma_table[palette[i].green];
-         palette[i].blue = png_ptr->gamma_table[palette[i].blue];
-      }
-   }
-#if defined(PNG_READ_BACKGROUND_SUPPORTED)
-   else
-#endif
-#endif
-#if defined(PNG_READ_BACKGROUND_SUPPORTED)
-   if (png_ptr->transformations & PNG_BACKGROUND)
-   {
-      if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
-      {
-         png_color back;
-
-         back.red   = (png_byte)png_ptr->background.red;
-         back.green = (png_byte)png_ptr->background.green;
-         back.blue  = (png_byte)png_ptr->background.blue;
-
-         for (i = 0; i < (int)png_ptr->num_trans; i++)
-         {
-            if (png_ptr->trans[i] == 0)
-            {
-               palette[i].red = back.red;
-               palette[i].green = back.green;
-               palette[i].blue = back.blue;
-            }
-            else if (png_ptr->trans[i] != 0xff)
-            {
-               png_composite(palette[i].red, png_ptr->palette[i].red,
-                  png_ptr->trans[i], back.red);
-               png_composite(palette[i].green, png_ptr->palette[i].green,
-                  png_ptr->trans[i], back.green);
-               png_composite(palette[i].blue, png_ptr->palette[i].blue,
-                  png_ptr->trans[i], back.blue);
-            }
-         }
-      }
-      else /* assume grayscale palette (what else could it be?) */
-      {
-         int i;
-
-         for (i = 0; i < num_palette; i++)
-         {
-            if (i == (png_byte)png_ptr->trans_values.gray)
-            {
-               palette[i].red = (png_byte)png_ptr->background.red;
-               palette[i].green = (png_byte)png_ptr->background.green;
-               palette[i].blue = (png_byte)png_ptr->background.blue;
-            }
-         }
-      }
-   }
-#endif
-}
-#endif
-
-#if defined(PNG_READ_BACKGROUND_SUPPORTED)
-/* Replace any alpha or transparency with the supplied background color.
- * "background" is already in the screen gamma, while "background_1" is
- * at a gamma of 1.0.  Paletted files have already been taken care of.
- */
-void /* PRIVATE */
-png_do_background(png_row_infop row_info, png_bytep row,
-   png_color_16p trans_values, png_color_16p background
-#if defined(PNG_READ_GAMMA_SUPPORTED)
-   , png_color_16p background_1,
-   png_bytep gamma_table, png_bytep gamma_from_1, png_bytep gamma_to_1,
-   png_uint_16pp gamma_16, png_uint_16pp gamma_16_from_1,
-   png_uint_16pp gamma_16_to_1, int gamma_shift
-#endif
-   )
-{
-   png_bytep sp, dp;
-   png_uint_32 i;
-   png_uint_32 row_width=row_info->width;
-   int shift;
-
-   png_debug(1, "in png_do_background\n");
-   if (background != NULL &&
-#if defined(PNG_USELESS_TESTS_SUPPORTED)
-       row != NULL && row_info != NULL &&
-#endif
-      (!(row_info->color_type & PNG_COLOR_MASK_ALPHA) ||
-      (row_info->color_type != PNG_COLOR_TYPE_PALETTE && trans_values)))
-   {
-      switch (row_info->color_type)
-      {
-         case PNG_COLOR_TYPE_GRAY:
-         {
-            switch (row_info->bit_depth)
-            {
-               case 1:
-               {
-                  sp = row;
-                  shift = 7;
-                  for (i = 0; i < row_width; i++)
-                  {
-                     if ((png_uint_16)((*sp >> shift) & 0x01)
-                        == trans_values->gray)
-                     {
-                        *sp &= (png_byte)((0x7f7f >> (7 - shift)) & 0xff);
-                        *sp |= (png_byte)(background->gray << shift);
-                     }
-                     if (!shift)
-                     {
-                        shift = 7;
-                        sp++;
-                     }
-                     else
-                        shift--;
-                  }
-                  break;
-               }
-               case 2:
-               {
-#if defined(PNG_READ_GAMMA_SUPPORTED)
-                  if (gamma_table != NULL)
-                  {
-                     sp = row;
-                     shift = 6;
-                     for (i = 0; i < row_width; i++)
-                     {
-                        if ((png_uint_16)((*sp >> shift) & 0x03)
-                            == trans_values->gray)
-                        {
-                           *sp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff);
-                           *sp |= (png_byte)(background->gray << shift);
-                        }
-                        else
-                        {
-                           png_byte p = (png_byte)((*sp >> shift) & 0x03);
-                           png_byte g = (png_byte)((gamma_table [p | (p << 2) |
-                               (p << 4) | (p << 6)] >> 6) & 0x03);
-                           *sp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff);
-                           *sp |= (png_byte)(g << shift);
-                        }
-                        if (!shift)
-                        {
-                           shift = 6;
-                           sp++;
-                        }
-                        else
-                           shift -= 2;
-                     }
-                  }
-                  else
-#endif
-                  {
-                     sp = row;
-                     shift = 6;
-                     for (i = 0; i < row_width; i++)
-                     {
-                        if ((png_uint_16)((*sp >> shift) & 0x03)
-                            == trans_values->gray)
-                        {
-                           *sp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff);
-                           *sp |= (png_byte)(background->gray << shift);
-                        }
-                        if (!shift)
-                        {
-                           shift = 6;
-                           sp++;
-                        }
-                        else
-                           shift -= 2;
-                     }
-                  }
-                  break;
-               }
-               case 4:
-               {
-#if defined(PNG_READ_GAMMA_SUPPORTED)
-                  if (gamma_table != NULL)
-                  {
-                     sp = row;
-                     shift = 4;
-                     for (i = 0; i < row_width; i++)
-                     {
-                        if ((png_uint_16)((*sp >> shift) & 0x0f)
-                            == trans_values->gray)
-                        {
-                           *sp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff);
-                           *sp |= (png_byte)(background->gray << shift);
-                        }
-                        else
-                        {
-                           png_byte p = (png_byte)((*sp >> shift) & 0x0f);
-                           png_byte g = (png_byte)((gamma_table[p |
-                             (p << 4)] >> 4) & 0x0f);
-                           *sp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff);
-                           *sp |= (png_byte)(g << shift);
-                        }
-                        if (!shift)
-                        {
-                           shift = 4;
-                           sp++;
-                        }
-                        else
-                           shift -= 4;
-                     }
-                  }
-                  else
-#endif
-                  {
-                     sp = row;
-                     shift = 4;
-                     for (i = 0; i < row_width; i++)
-                     {
-                        if ((png_uint_16)((*sp >> shift) & 0x0f)
-                            == trans_values->gray)
-                        {
-                           *sp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff);
-                           *sp |= (png_byte)(background->gray << shift);
-                        }
-                        if (!shift)
-                        {
-                           shift = 4;
-                           sp++;
-                        }
-                        else
-                           shift -= 4;
-                     }
-                  }
-                  break;
-               }
-               case 8:
-               {
-#if defined(PNG_READ_GAMMA_SUPPORTED)
-                  if (gamma_table != NULL)
-                  {
-                     sp = row;
-                     for (i = 0; i < row_width; i++, sp++)
-                     {
-                        if (*sp == trans_values->gray)
-                        {
-                           *sp = (png_byte)background->gray;
-                        }
-                        else
-                        {
-                           *sp = gamma_table[*sp];
-                        }
-                     }
-                  }
-                  else
-#endif
-                  {
-                     sp = row;
-                     for (i = 0; i < row_width; i++, sp++)
-                     {
-                        if (*sp == trans_values->gray)
-                        {
-                           *sp = (png_byte)background->gray;
-                        }
-                     }
-                  }
-                  break;
-               }
-               case 16:
-               {
-#if defined(PNG_READ_GAMMA_SUPPORTED)
-                  if (gamma_16 != NULL)
-                  {
-                     sp = row;
-                     for (i = 0; i < row_width; i++, sp += 2)
-                     {
-                        png_uint_16 v;
-
-                        v = (png_uint_16)(((*sp) << 8) + *(sp + 1));
-                        if (v == trans_values->gray)
-                        {
-                           /* background is already in screen gamma */
-                           *sp = (png_byte)((background->gray >> 8) & 0xff);
-                           *(sp + 1) = (png_byte)(background->gray & 0xff);
-                        }
-                        else
-                        {
-                           v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
-                           *sp = (png_byte)((v >> 8) & 0xff);
-                           *(sp + 1) = (png_byte)(v & 0xff);
-                        }
-                     }
-                  }
-                  else
-#endif
-                  {
-                     sp = row;
-                     for (i = 0; i < row_width; i++, sp += 2)
-                     {
-                        png_uint_16 v;
-
-                        v = (png_uint_16)(((*sp) << 8) + *(sp + 1));
-                        if (v == trans_values->gray)
-                        {
-                           *sp = (png_byte)((background->gray >> 8) & 0xff);
-                           *(sp + 1) = (png_byte)(background->gray & 0xff);
-                        }
-                     }
-                  }
-                  break;
-               }
-            }
-            break;
-         }
-         case PNG_COLOR_TYPE_RGB:
-         {
-            if (row_info->bit_depth == 8)
-            {
-#if defined(PNG_READ_GAMMA_SUPPORTED)
-               if (gamma_table != NULL)
-               {
-                  sp = row;
-                  for (i = 0; i < row_width; i++, sp += 3)
-                  {
-                     if (*sp == trans_values->red &&
-                        *(sp + 1) == trans_values->green &&
-                        *(sp + 2) == trans_values->blue)
-                     {
-                        *sp = (png_byte)background->red;
-                        *(sp + 1) = (png_byte)background->green;
-                        *(sp + 2) = (png_byte)background->blue;
-                     }
-                     else
-                     {
-                        *sp = gamma_table[*sp];
-                        *(sp + 1) = gamma_table[*(sp + 1)];
-                        *(sp + 2) = gamma_table[*(sp + 2)];
-                     }
-                  }
-               }
-               else
-#endif
-               {
-                  sp = row;
-                  for (i = 0; i < row_width; i++, sp += 3)
-                  {
-                     if (*sp == trans_values->red &&
-                        *(sp + 1) == trans_values->green &&
-                        *(sp + 2) == trans_values->blue)
-                     {
-                        *sp = (png_byte)background->red;
-                        *(sp + 1) = (png_byte)background->green;
-                        *(sp + 2) = (png_byte)background->blue;
-                     }
-                  }
-               }
-            }
-            else /* if (row_info->bit_depth == 16) */
-            {
-#if defined(PNG_READ_GAMMA_SUPPORTED)
-               if (gamma_16 != NULL)
-               {
-                  sp = row;
-                  for (i = 0; i < row_width; i++, sp += 6)
-                  {
-                     png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp + 1));
-                     png_uint_16 g = (png_uint_16)(((*(sp+2)) << 8) + *(sp+3));
-                     png_uint_16 b = (png_uint_16)(((*(sp+4)) << 8) + *(sp+5));
-                     if (r == trans_values->red && g == trans_values->green &&
-                        b == trans_values->blue)
-                     {
-                        /* background is already in screen gamma */
-                        *sp = (png_byte)((background->red >> 8) & 0xff);
-                        *(sp + 1) = (png_byte)(background->red & 0xff);
-                        *(sp + 2) = (png_byte)((background->green >> 8) & 0xff);
-                        *(sp + 3) = (png_byte)(background->green & 0xff);
-                        *(sp + 4) = (png_byte)((background->blue >> 8) & 0xff);
-                        *(sp + 5) = (png_byte)(background->blue & 0xff);
-                     }
-                     else
-                     {
-                        png_uint_16 v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
-                        *sp = (png_byte)((v >> 8) & 0xff);
-                        *(sp + 1) = (png_byte)(v & 0xff);
-                        v = gamma_16[*(sp + 3) >> gamma_shift][*(sp + 2)];
-                        *(sp + 2) = (png_byte)((v >> 8) & 0xff);
-                        *(sp + 3) = (png_byte)(v & 0xff);
-                        v = gamma_16[*(sp + 5) >> gamma_shift][*(sp + 4)];
-                        *(sp + 4) = (png_byte)((v >> 8) & 0xff);
-                        *(sp + 5) = (png_byte)(v & 0xff);
-                     }
-                  }
-               }
-               else
-#endif
-               {
-                  sp = row;
-                  for (i = 0; i < row_width; i++, sp += 6)
-                  {
-                     png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp+1));
-                     png_uint_16 g = (png_uint_16)(((*(sp+2)) << 8) + *(sp+3));
-                     png_uint_16 b = (png_uint_16)(((*(sp+4)) << 8) + *(sp+5));
-
-                     if (r == trans_values->red && g == trans_values->green &&
-                        b == trans_values->blue)
-                     {
-                        *sp = (png_byte)((background->red >> 8) & 0xff);
-                        *(sp + 1) = (png_byte)(background->red & 0xff);
-                        *(sp + 2) = (png_byte)((background->green >> 8) & 0xff);
-                        *(sp + 3) = (png_byte)(background->green & 0xff);
-                        *(sp + 4) = (png_byte)((background->blue >> 8) & 0xff);
-                        *(sp + 5) = (png_byte)(background->blue & 0xff);
-                     }
-                  }
-               }
-            }
-            break;
-         }
-         case PNG_COLOR_TYPE_GRAY_ALPHA:
-         {
-            if (row_info->bit_depth == 8)
-            {
-#if defined(PNG_READ_GAMMA_SUPPORTED)
-               if (gamma_to_1 != NULL && gamma_from_1 != NULL &&
-                   gamma_table != NULL)
-               {
-                  sp = row;
-                  dp = row;
-                  for (i = 0; i < row_width; i++, sp += 2, dp++)
-                  {
-                     png_uint_16 a = *(sp + 1);
-
-                     if (a == 0xff)
-                     {
-                        *dp = gamma_table[*sp];
-                     }
-                     else if (a == 0)
-                     {
-                        /* background is already in screen gamma */
-                        *dp = (png_byte)background->gray;
-                     }
-                     else
-                     {
-                        png_byte v, w;
-
-                        v = gamma_to_1[*sp];
-                        png_composite(w, v, a, background_1->gray);
-                        *dp = gamma_from_1[w];
-                     }
-                  }
-               }
-               else
-#endif
-               {
-                  sp = row;
-                  dp = row;
-                  for (i = 0; i < row_width; i++, sp += 2, dp++)
-                  {
-                     png_byte a = *(sp + 1);
-
-                     if (a == 0xff)
-                     {
-                        *dp = *sp;
-                     }
-#if defined(PNG_READ_GAMMA_SUPPORTED)
-                     else if (a == 0)
-                     {
-                        *dp = (png_byte)background->gray;
-                     }
-                     else
-                     {
-                        png_composite(*dp, *sp, a, background_1->gray);
-                     }
-#else
-                     *dp = (png_byte)background->gray;
-#endif
-                  }
-               }
-            }
-            else /* if (png_ptr->bit_depth == 16) */
-            {
-#if defined(PNG_READ_GAMMA_SUPPORTED)
-               if (gamma_16 != NULL && gamma_16_from_1 != NULL &&
-                   gamma_16_to_1 != NULL)
-               {
-                  sp = row;
-                  dp = row;
-                  for (i = 0; i < row_width; i++, sp += 4, dp += 2)
-                  {
-                     png_uint_16 a = (png_uint_16)(((*(sp+2)) << 8) + *(sp+3));
-
-                     if (a == (png_uint_16)0xffff)
-                     {
-                        png_uint_16 v;
-
-                        v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
-                        *dp = (png_byte)((v >> 8) & 0xff);
-                        *(dp + 1) = (png_byte)(v & 0xff);
-                     }
-#if defined(PNG_READ_GAMMA_SUPPORTED)
-                     else if (a == 0)
-#else
-                     else
-#endif
-                     {
-                        /* background is already in screen gamma */
-                        *dp = (png_byte)((background->gray >> 8) & 0xff);
-                        *(dp + 1) = (png_byte)(background->gray & 0xff);
-                     }
-#if defined(PNG_READ_GAMMA_SUPPORTED)
-                     else
-                     {
-                        png_uint_16 g, v, w;
-
-                        g = gamma_16_to_1[*(sp + 1) >> gamma_shift][*sp];
-                        png_composite_16(v, g, a, background_1->gray);
-                        w = gamma_16_from_1[(v&0xff) >> gamma_shift][v >> 8];
-                        *dp = (png_byte)((w >> 8) & 0xff);
-                        *(dp + 1) = (png_byte)(w & 0xff);
-                     }
-#endif
-                  }
-               }
-               else
-#endif
-               {
-                  sp = row;
-                  dp = row;
-                  for (i = 0; i < row_width; i++, sp += 4, dp += 2)
-                  {
-                     png_uint_16 a = (png_uint_16)(((*(sp+2)) << 8) + *(sp+3));
-                     if (a == (png_uint_16)0xffff)
-                     {
-                        png_memcpy(dp, sp, 2);
-                     }
-#if defined(PNG_READ_GAMMA_SUPPORTED)
-                     else if (a == 0)
-#else
-                     else
-#endif
-                     {
-                        *dp = (png_byte)((background->gray >> 8) & 0xff);
-                        *(dp + 1) = (png_byte)(background->gray & 0xff);
-                     }
-#if defined(PNG_READ_GAMMA_SUPPORTED)
-                     else
-                     {
-                        png_uint_16 g, v;
-
-                        g = (png_uint_16)(((*sp) << 8) + *(sp + 1));
-                        png_composite_16(v, g, a, background_1->gray);
-                        *dp = (png_byte)((v >> 8) & 0xff);
-                        *(dp + 1) = (png_byte)(v & 0xff);
-                     }
-#endif
-                  }
-               }
-            }
-            break;
-         }
-         case PNG_COLOR_TYPE_RGB_ALPHA:
-         {
-            if (row_info->bit_depth == 8)
-            {
-#if defined(PNG_READ_GAMMA_SUPPORTED)
-               if (gamma_to_1 != NULL && gamma_from_1 != NULL &&
-                   gamma_table != NULL)
-               {
-                  sp = row;
-                  dp = row;
-                  for (i = 0; i < row_width; i++, sp += 4, dp += 3)
-                  {
-                     png_byte a = *(sp + 3);
-
-                     if (a == 0xff)
-                     {
-                        *dp = gamma_table[*sp];
-                        *(dp + 1) = gamma_table[*(sp + 1)];
-                        *(dp + 2) = gamma_table[*(sp + 2)];
-                     }
-                     else if (a == 0)
-                     {
-                        /* background is already in screen gamma */
-                        *dp = (png_byte)background->red;
-                        *(dp + 1) = (png_byte)background->green;
-                        *(dp + 2) = (png_byte)background->blue;
-                     }
-                     else
-                     {
-                        png_byte v, w;
-
-                        v = gamma_to_1[*sp];
-                        png_composite(w, v, a, background_1->red);
-                        *dp = gamma_from_1[w];
-                        v = gamma_to_1[*(sp + 1)];
-                        png_composite(w, v, a, background_1->green);
-                        *(dp + 1) = gamma_from_1[w];
-                        v = gamma_to_1[*(sp + 2)];
-                        png_composite(w, v, a, background_1->blue);
-                        *(dp + 2) = gamma_from_1[w];
-                     }
-                  }
-               }
-               else
-#endif
-               {
-                  sp = row;
-                  dp = row;
-                  for (i = 0; i < row_width; i++, sp += 4, dp += 3)
-                  {
-                     png_byte a = *(sp + 3);
-
-                     if (a == 0xff)
-                     {
-                        *dp = *sp;
-                        *(dp + 1) = *(sp + 1);
-                        *(dp + 2) = *(sp + 2);
-                     }
-                     else if (a == 0)
-                     {
-                        *dp = (png_byte)background->red;
-                        *(dp + 1) = (png_byte)background->green;
-                        *(dp + 2) = (png_byte)background->blue;
-                     }
-                     else
-                     {
-                        png_composite(*dp, *sp, a, background->red);
-                        png_composite(*(dp + 1), *(sp + 1), a,
-                           background->green);
-                        png_composite(*(dp + 2), *(sp + 2), a,
-                           background->blue);
-                     }
-                  }
-               }
-            }
-            else /* if (row_info->bit_depth == 16) */
-            {
-#if defined(PNG_READ_GAMMA_SUPPORTED)
-               if (gamma_16 != NULL && gamma_16_from_1 != NULL &&
-                   gamma_16_to_1 != NULL)
-               {
-                  sp = row;
-                  dp = row;
-                  for (i = 0; i < row_width; i++, sp += 8, dp += 6)
-                  {
-                     png_uint_16 a = (png_uint_16)(((png_uint_16)(*(sp + 6))
-                         << 8) + (png_uint_16)(*(sp + 7)));
-                     if (a == (png_uint_16)0xffff)
-                     {
-                        png_uint_16 v;
-
-                        v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
-                        *dp = (png_byte)((v >> 8) & 0xff);
-                        *(dp + 1) = (png_byte)(v & 0xff);
-                        v = gamma_16[*(sp + 3) >> gamma_shift][*(sp + 2)];
-                        *(dp + 2) = (png_byte)((v >> 8) & 0xff);
-                        *(dp + 3) = (png_byte)(v & 0xff);
-                        v = gamma_16[*(sp + 5) >> gamma_shift][*(sp + 4)];
-                        *(dp + 4) = (png_byte)((v >> 8) & 0xff);
-                        *(dp + 5) = (png_byte)(v & 0xff);
-                     }
-                     else if (a == 0)
-                     {
-                        /* background is already in screen gamma */
-                        *dp = (png_byte)((background->red >> 8) & 0xff);
-                        *(dp + 1) = (png_byte)(background->red & 0xff);
-                        *(dp + 2) = (png_byte)((background->green >> 8) & 0xff);
-                        *(dp + 3) = (png_byte)(background->green & 0xff);
-                        *(dp + 4) = (png_byte)((background->blue >> 8) & 0xff);
-                        *(dp + 5) = (png_byte)(background->blue & 0xff);
-                     }
-                     else
-                     {
-                        png_uint_16 v, w, x;
-
-                        v = gamma_16_to_1[*(sp + 1) >> gamma_shift][*sp];
-                        png_composite_16(w, v, a, background_1->red);
-                        x = gamma_16_from_1[((w&0xff) >> gamma_shift)][w >> 8];
-                        *dp = (png_byte)((x >> 8) & 0xff);
-                        *(dp + 1) = (png_byte)(x & 0xff);
-                        v = gamma_16_to_1[*(sp + 3) >> gamma_shift][*(sp + 2)];
-                        png_composite_16(w, v, a, background_1->green);
-                        x = gamma_16_from_1[((w&0xff) >> gamma_shift)][w >> 8];
-                        *(dp + 2) = (png_byte)((x >> 8) & 0xff);
-                        *(dp + 3) = (png_byte)(x & 0xff);
-                        v = gamma_16_to_1[*(sp + 5) >> gamma_shift][*(sp + 4)];
-                        png_composite_16(w, v, a, background_1->blue);
-                        x = gamma_16_from_1[(w & 0xff) >> gamma_shift][w >> 8];
-                        *(dp + 4) = (png_byte)((x >> 8) & 0xff);
-                        *(dp + 5) = (png_byte)(x & 0xff);
-                     }
-                  }
-               }
-               else
-#endif
-               {
-                  sp = row;
-                  dp = row;
-                  for (i = 0; i < row_width; i++, sp += 8, dp += 6)
-                  {
-                     png_uint_16 a = (png_uint_16)(((png_uint_16)(*(sp + 6))
-                        << 8) + (png_uint_16)(*(sp + 7)));
-                     if (a == (png_uint_16)0xffff)
-                     {
-                        png_memcpy(dp, sp, 6);
-                     }
-                     else if (a == 0)
-                     {
-                        *dp = (png_byte)((background->red >> 8) & 0xff);
-                        *(dp + 1) = (png_byte)(background->red & 0xff);
-                        *(dp + 2) = (png_byte)((background->green >> 8) & 0xff);
-                        *(dp + 3) = (png_byte)(background->green & 0xff);
-                        *(dp + 4) = (png_byte)((background->blue >> 8) & 0xff);
-                        *(dp + 5) = (png_byte)(background->blue & 0xff);
-                     }
-                     else
-                     {
-                        png_uint_16 v;
-
-                        png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp + 1));
-                        png_uint_16 g = (png_uint_16)(((*(sp + 2)) << 8)
-                            + *(sp + 3));
-                        png_uint_16 b = (png_uint_16)(((*(sp + 4)) << 8)
-                            + *(sp + 5));
-
-                        png_composite_16(v, r, a, background->red);
-                        *dp = (png_byte)((v >> 8) & 0xff);
-                        *(dp + 1) = (png_byte)(v & 0xff);
-                        png_composite_16(v, g, a, background->green);
-                        *(dp + 2) = (png_byte)((v >> 8) & 0xff);
-                        *(dp + 3) = (png_byte)(v & 0xff);
-                        png_composite_16(v, b, a, background->blue);
-                        *(dp + 4) = (png_byte)((v >> 8) & 0xff);
-                        *(dp + 5) = (png_byte)(v & 0xff);
-                     }
-                  }
-               }
-            }
-            break;
-         }
-      }
-
-      if (row_info->color_type & PNG_COLOR_MASK_ALPHA)
-      {
-         row_info->color_type &= ~PNG_COLOR_MASK_ALPHA;
-         row_info->channels--;
-         row_info->pixel_depth = (png_byte)(row_info->channels *
-            row_info->bit_depth);
-         row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,row_width);
-      }
-   }
-}
-#endif
-
-#if defined(PNG_READ_GAMMA_SUPPORTED)
-/* Gamma correct the image, avoiding the alpha channel.  Make sure
- * you do this after you deal with the transparency issue on grayscale
- * or RGB images. If your bit depth is 8, use gamma_table, if it
- * is 16, use gamma_16_table and gamma_shift.  Build these with
- * build_gamma_table().
- */
-void /* PRIVATE */
-png_do_gamma(png_row_infop row_info, png_bytep row,
-   png_bytep gamma_table, png_uint_16pp gamma_16_table,
-   int gamma_shift)
-{
-   png_bytep sp;
-   png_uint_32 i;
-   png_uint_32 row_width=row_info->width;
-
-   png_debug(1, "in png_do_gamma\n");
-   if (
-#if defined(PNG_USELESS_TESTS_SUPPORTED)
-       row != NULL && row_info != NULL &&
-#endif
-       ((row_info->bit_depth <= 8 && gamma_table != NULL) ||
-        (row_info->bit_depth == 16 && gamma_16_table != NULL)))
-   {
-      switch (row_info->color_type)
-      {
-         case PNG_COLOR_TYPE_RGB:
-         {
-            if (row_info->bit_depth == 8)
-            {
-               sp = row;
-               for (i = 0; i < row_width; i++)
-               {
-                  *sp = gamma_table[*sp];
-                  sp++;
-                  *sp = gamma_table[*sp];
-                  sp++;
-                  *sp = gamma_table[*sp];
-                  sp++;
-               }
-            }
-            else /* if (row_info->bit_depth == 16) */
-            {
-               sp = row;
-               for (i = 0; i < row_width; i++)
-               {
-                  png_uint_16 v;
-
-                  v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
-                  *sp = (png_byte)((v >> 8) & 0xff);
-                  *(sp + 1) = (png_byte)(v & 0xff);
-                  sp += 2;
-                  v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
-                  *sp = (png_byte)((v >> 8) & 0xff);
-                  *(sp + 1) = (png_byte)(v & 0xff);
-                  sp += 2;
-                  v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
-                  *sp = (png_byte)((v >> 8) & 0xff);
-                  *(sp + 1) = (png_byte)(v & 0xff);
-                  sp += 2;
-               }
-            }
-            break;
-         }
-         case PNG_COLOR_TYPE_RGB_ALPHA:
-         {
-            if (row_info->bit_depth == 8)
-            {
-               sp = row;
-               for (i = 0; i < row_width; i++)
-               {
-                  *sp = gamma_table[*sp];
-                  sp++;
-                  *sp = gamma_table[*sp];
-                  sp++;
-                  *sp = gamma_table[*sp];
-                  sp++;
-                  sp++;
-               }
-            }
-            else /* if (row_info->bit_depth == 16) */
-            {
-               sp = row;
-               for (i = 0; i < row_width; i++)
-               {
-                  png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
-                  *sp = (png_byte)((v >> 8) & 0xff);
-                  *(sp + 1) = (png_byte)(v & 0xff);
-                  sp += 2;
-                  v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
-                  *sp = (png_byte)((v >> 8) & 0xff);
-                  *(sp + 1) = (png_byte)(v & 0xff);
-                  sp += 2;
-                  v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
-                  *sp = (png_byte)((v >> 8) & 0xff);
-                  *(sp + 1) = (png_byte)(v & 0xff);
-                  sp += 4;
-               }
-            }
-            break;
-         }
-         case PNG_COLOR_TYPE_GRAY_ALPHA:
-         {
-            if (row_info->bit_depth == 8)
-            {
-               sp = row;
-               for (i = 0; i < row_width; i++)
-               {
-                  *sp = gamma_table[*sp];
-                  sp += 2;
-               }
-            }
-            else /* if (row_info->bit_depth == 16) */
-            {
-               sp = row;
-               for (i = 0; i < row_width; i++)
-               {
-                  png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
-                  *sp = (png_byte)((v >> 8) & 0xff);
-                  *(sp + 1) = (png_byte)(v & 0xff);
-                  sp += 4;
-               }
-            }
-            break;
-         }
-         case PNG_COLOR_TYPE_GRAY:
-         {
-            if (row_info->bit_depth == 2)
-            {
-               sp = row;
-               for (i = 0; i < row_width; i += 4)
-               {
-                  int a = *sp & 0xc0;
-                  int b = *sp & 0x30;
-                  int c = *sp & 0x0c;
-                  int d = *sp & 0x03;
-
-                  *sp = (png_byte)(
-                        ((((int)gamma_table[a|(a>>2)|(a>>4)|(a>>6)])   ) & 0xc0)|
-                        ((((int)gamma_table[(b<<2)|b|(b>>2)|(b>>4)])>>2) & 0x30)|
-                        ((((int)gamma_table[(c<<4)|(c<<2)|c|(c>>2)])>>4) & 0x0c)|
-                        ((((int)gamma_table[(d<<6)|(d<<4)|(d<<2)|d])>>6) ));
-                  sp++;
-               }
-            }
-            if (row_info->bit_depth == 4)
-            {
-               sp = row;
-               for (i = 0; i < row_width; i += 2)
-               {
-                  int msb = *sp & 0xf0;
-                  int lsb = *sp & 0x0f;
-
-                  *sp = (png_byte)((((int)gamma_table[msb | (msb >> 4)]) & 0xf0)
-                          | (((int)gamma_table[(lsb << 4) | lsb]) >> 4));
-                  sp++;
-               }
-            }
-            else if (row_info->bit_depth == 8)
-            {
-               sp = row;
-               for (i = 0; i < row_width; i++)
-               {
-                  *sp = gamma_table[*sp];
-                  sp++;
-               }
-            }
-            else if (row_info->bit_depth == 16)
-            {
-               sp = row;
-               for (i = 0; i < row_width; i++)
-               {
-                  png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
-                  *sp = (png_byte)((v >> 8) & 0xff);
-                  *(sp + 1) = (png_byte)(v & 0xff);
-                  sp += 2;
-               }
-            }
-            break;
-         }
-      }
-   }
-}
-#endif
-
-#if defined(PNG_READ_EXPAND_SUPPORTED)
-/* Expands a palette row to an RGB or RGBA row depending
- * upon whether you supply trans and num_trans.
- */
-void /* PRIVATE */
-png_do_expand_palette(png_row_infop row_info, png_bytep row,
-   png_colorp palette, png_bytep trans, int num_trans)
-{
-   int shift, value;
-   png_bytep sp, dp;
-   png_uint_32 i;
-   png_uint_32 row_width=row_info->width;
-
-   png_debug(1, "in png_do_expand_palette\n");
-   if (
-#if defined(PNG_USELESS_TESTS_SUPPORTED)
-       row != NULL && row_info != NULL &&
-#endif
-       row_info->color_type == PNG_COLOR_TYPE_PALETTE)
-   {
-      if (row_info->bit_depth < 8)
-      {
-         switch (row_info->bit_depth)
-         {
-            case 1:
-            {
-               sp = row + (png_size_t)((row_width - 1) >> 3);
-               dp = row + (png_size_t)row_width - 1;
-               shift = 7 - (int)((row_width + 7) & 0x07);
-               for (i = 0; i < row_width; i++)
-               {
-                  if ((*sp >> shift) & 0x01)
-                     *dp = 1;
-                  else
-                     *dp = 0;
-                  if (shift == 7)
-                  {
-                     shift = 0;
-                     sp--;
-                  }
-                  else
-                     shift++;
-
-                  dp--;
-               }
-               break;
-            }
-            case 2:
-            {
-               sp = row + (png_size_t)((row_width - 1) >> 2);
-               dp = row + (png_size_t)row_width - 1;
-               shift = (int)((3 - ((row_width + 3) & 0x03)) << 1);
-               for (i = 0; i < row_width; i++)
-               {
-                  value = (*sp >> shift) & 0x03;
-                  *dp = (png_byte)value;
-                  if (shift == 6)
-                  {
-                     shift = 0;
-                     sp--;
-                  }
-                  else
-                     shift += 2;
-
-                  dp--;
-               }
-               break;
-            }
-            case 4:
-            {
-               sp = row + (png_size_t)((row_width - 1) >> 1);
-               dp = row + (png_size_t)row_width - 1;
-               shift = (int)((row_width & 0x01) << 2);
-               for (i = 0; i < row_width; i++)
-               {
-                  value = (*sp >> shift) & 0x0f;
-                  *dp = (png_byte)value;
-                  if (shift == 4)
-                  {
-                     shift = 0;
-                     sp--;
-                  }
-                  else
-                     shift += 4;
-
-                  dp--;
-               }
-               break;
-            }
-         }
-         row_info->bit_depth = 8;
-         row_info->pixel_depth = 8;
-         row_info->rowbytes = row_width;
-      }
-      switch (row_info->bit_depth)
-      {
-         case 8:
-         {
-            if (trans != NULL)
-            {
-               sp = row + (png_size_t)row_width - 1;
-               dp = row + (png_size_t)(row_width << 2) - 1;
-
-               for (i = 0; i < row_width; i++)
-               {
-                  if ((int)(*sp) >= num_trans)
-                     *dp-- = 0xff;
-                  else
-                     *dp-- = trans[*sp];
-                  *dp-- = palette[*sp].blue;
-                  *dp-- = palette[*sp].green;
-                  *dp-- = palette[*sp].red;
-                  sp--;
-               }
-               row_info->bit_depth = 8;
-               row_info->pixel_depth = 32;
-               row_info->rowbytes = row_width * 4;
-               row_info->color_type = 6;
-               row_info->channels = 4;
-            }
-            else
-            {
-               sp = row + (png_size_t)row_width - 1;
-               dp = row + (png_size_t)(row_width * 3) - 1;
-
-               for (i = 0; i < row_width; i++)
-               {
-                  *dp-- = palette[*sp].blue;
-                  *dp-- = palette[*sp].green;
-                  *dp-- = palette[*sp].red;
-                  sp--;
-               }
-               row_info->bit_depth = 8;
-               row_info->pixel_depth = 24;
-               row_info->rowbytes = row_width * 3;
-               row_info->color_type = 2;
-               row_info->channels = 3;
-            }
-            break;
-         }
-      }
-   }
-}
-
-/* If the bit depth < 8, it is expanded to 8.  Also, if the already
- * expanded transparency value is supplied, an alpha channel is built.
- */
-void /* PRIVATE */
-png_do_expand(png_row_infop row_info, png_bytep row,
-   png_color_16p trans_value)
-{
-   int shift, value;
-   png_bytep sp, dp;
-   png_uint_32 i;
-   png_uint_32 row_width=row_info->width;
-
-   png_debug(1, "in png_do_expand\n");
-#if defined(PNG_USELESS_TESTS_SUPPORTED)
-   if (row != NULL && row_info != NULL)
-#endif
-   {
-      if (row_info->color_type == PNG_COLOR_TYPE_GRAY)
-      {
-         png_uint_16 gray = (png_uint_16)(trans_value ? trans_value->gray : 0);
-
-         if (row_info->bit_depth < 8)
-         {
-            switch (row_info->bit_depth)
-            {
-               case 1:
-               {
-                  gray = (png_uint_16)((gray&0x01)*0xff);
-                  sp = row + (png_size_t)((row_width - 1) >> 3);
-                  dp = row + (png_size_t)row_width - 1;
-                  shift = 7 - (int)((row_width + 7) & 0x07);
-                  for (i = 0; i < row_width; i++)
-                  {
-                     if ((*sp >> shift) & 0x01)
-                        *dp = 0xff;
-                     else
-                        *dp = 0;
-                     if (shift == 7)
-                     {
-                        shift = 0;
-                        sp--;
-                     }
-                     else
-                        shift++;
-
-                     dp--;
-                  }
-                  break;
-               }
-               case 2:
-               {
-                  gray = (png_uint_16)((gray&0x03)*0x55);
-                  sp = row + (png_size_t)((row_width - 1) >> 2);
-                  dp = row + (png_size_t)row_width - 1;
-                  shift = (int)((3 - ((row_width + 3) & 0x03)) << 1);
-                  for (i = 0; i < row_width; i++)
-                  {
-                     value = (*sp >> shift) & 0x03;
-                     *dp = (png_byte)(value | (value << 2) | (value << 4) |
-                        (value << 6));
-                     if (shift == 6)
-                     {
-                        shift = 0;
-                        sp--;
-                     }
-                     else
-                        shift += 2;
-
-                     dp--;
-                  }
-                  break;
-               }
-               case 4:
-               {
-                  gray = (png_uint_16)((gray&0x0f)*0x11);
-                  sp = row + (png_size_t)((row_width - 1) >> 1);
-                  dp = row + (png_size_t)row_width - 1;
-                  shift = (int)((1 - ((row_width + 1) & 0x01)) << 2);
-                  for (i = 0; i < row_width; i++)
-                  {
-                     value = (*sp >> shift) & 0x0f;
-                     *dp = (png_byte)(value | (value << 4));
-                     if (shift == 4)
-                     {
-                        shift = 0;
-                        sp--;
-                     }
-                     else
-                        shift = 4;
-
-                     dp--;
-                  }
-                  break;
-               }
-            }
-            row_info->bit_depth = 8;
-            row_info->pixel_depth = 8;
-            row_info->rowbytes = row_width;
-         }
-
-         if (trans_value != NULL)
-         {
-            if (row_info->bit_depth == 8)
-            {
-               gray = gray & 0xff;
-               sp = row + (png_size_t)row_width - 1;
-               dp = row + (png_size_t)(row_width << 1) - 1;
-               for (i = 0; i < row_width; i++)
-               {
-                  if (*sp == gray)
-                     *dp-- = 0;
-                  else
-                     *dp-- = 0xff;
-                  *dp-- = *sp--;
-               }
-            }
-            else if (row_info->bit_depth == 16)
-            {
-               png_byte gray_high = (gray >> 8) & 0xff;
-               png_byte gray_low = gray & 0xff;
-               sp = row + row_info->rowbytes - 1;
-               dp = row + (row_info->rowbytes << 1) - 1;
-               for (i = 0; i < row_width; i++)
-               {
-                  if (*(sp-1) == gray_high && *(sp) == gray_low) 
-                  {
-                     *dp-- = 0;
-                     *dp-- = 0;
-                  }
-                  else
-                  {
-                     *dp-- = 0xff;
-                     *dp-- = 0xff;
-                  }
-                  *dp-- = *sp--;
-                  *dp-- = *sp--;
-               }
-            }
-            row_info->color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
-            row_info->channels = 2;
-            row_info->pixel_depth = (png_byte)(row_info->bit_depth << 1);
-            row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,
-               row_width);
-         }
-      }
-      else if (row_info->color_type == PNG_COLOR_TYPE_RGB && trans_value)
-      {
-         if (row_info->bit_depth == 8)
-         {
-            png_byte red = trans_value->red & 0xff;
-            png_byte green = trans_value->green & 0xff;
-            png_byte blue = trans_value->blue & 0xff;
-            sp = row + (png_size_t)row_info->rowbytes - 1;
-            dp = row + (png_size_t)(row_width << 2) - 1;
-            for (i = 0; i < row_width; i++)
-            {
-               if (*(sp - 2) == red && *(sp - 1) == green && *(sp) == blue)
-                  *dp-- = 0;
-               else
-                  *dp-- = 0xff;
-               *dp-- = *sp--;
-               *dp-- = *sp--;
-               *dp-- = *sp--;
-            }
-         }
-         else if (row_info->bit_depth == 16)
-         {
-            png_byte red_high = (trans_value->red >> 8) & 0xff;
-            png_byte green_high = (trans_value->green >> 8) & 0xff;
-            png_byte blue_high = (trans_value->blue >> 8) & 0xff;
-            png_byte red_low = trans_value->red & 0xff;
-            png_byte green_low = trans_value->green & 0xff;
-            png_byte blue_low = trans_value->blue & 0xff;
-            sp = row + row_info->rowbytes - 1;
-            dp = row + (png_size_t)(row_width << 3) - 1;
-            for (i = 0; i < row_width; i++)
-            {
-               if (*(sp - 5) == red_high &&
-                  *(sp - 4) == red_low &&
-                  *(sp - 3) == green_high &&
-                  *(sp - 2) == green_low &&
-                  *(sp - 1) == blue_high &&
-                  *(sp    ) == blue_low)
-               {
-                  *dp-- = 0;
-                  *dp-- = 0;
-               }
-               else
-               {
-                  *dp-- = 0xff;
-                  *dp-- = 0xff;
-               }
-               *dp-- = *sp--;
-               *dp-- = *sp--;
-               *dp-- = *sp--;
-               *dp-- = *sp--;
-               *dp-- = *sp--;
-               *dp-- = *sp--;
-            }
-         }
-         row_info->color_type = PNG_COLOR_TYPE_RGB_ALPHA;
-         row_info->channels = 4;
-         row_info->pixel_depth = (png_byte)(row_info->bit_depth << 2);
-         row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,row_width);
-      }
-   }
-}
-#endif
-
-#if defined(PNG_READ_DITHER_SUPPORTED)
-void /* PRIVATE */
-png_do_dither(png_row_infop row_info, png_bytep row,
-    png_bytep palette_lookup, png_bytep dither_lookup)
-{
-   png_bytep sp, dp;
-   png_uint_32 i;
-   png_uint_32 row_width=row_info->width;
-
-   png_debug(1, "in png_do_dither\n");
-#if defined(PNG_USELESS_TESTS_SUPPORTED)
-   if (row != NULL && row_info != NULL)
-#endif
-   {
-      if (row_info->color_type == PNG_COLOR_TYPE_RGB &&
-         palette_lookup && row_info->bit_depth == 8)
-      {
-         int r, g, b, p;
-         sp = row;
-         dp = row;
-         for (i = 0; i < row_width; i++)
-         {
-            r = *sp++;
-            g = *sp++;
-            b = *sp++;
-
-            /* this looks real messy, but the compiler will reduce
-               it down to a reasonable formula.  For example, with
-               5 bits per color, we get:
-               p = (((r >> 3) & 0x1f) << 10) |
-                  (((g >> 3) & 0x1f) << 5) |
-                  ((b >> 3) & 0x1f);
-               */
-            p = (((r >> (8 - PNG_DITHER_RED_BITS)) &
-               ((1 << PNG_DITHER_RED_BITS) - 1)) <<
-               (PNG_DITHER_GREEN_BITS + PNG_DITHER_BLUE_BITS)) |
-               (((g >> (8 - PNG_DITHER_GREEN_BITS)) &
-               ((1 << PNG_DITHER_GREEN_BITS) - 1)) <<
-               (PNG_DITHER_BLUE_BITS)) |
-               ((b >> (8 - PNG_DITHER_BLUE_BITS)) &
-               ((1 << PNG_DITHER_BLUE_BITS) - 1));
-
-            *dp++ = palette_lookup[p];
-         }
-         row_info->color_type = PNG_COLOR_TYPE_PALETTE;
-         row_info->channels = 1;
-         row_info->pixel_depth = row_info->bit_depth;
-         row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,row_width);
-      }
-      else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA &&
-         palette_lookup != NULL && row_info->bit_depth == 8)
-      {
-         int r, g, b, p;
-         sp = row;
-         dp = row;
-         for (i = 0; i < row_width; i++)
-         {
-            r = *sp++;
-            g = *sp++;
-            b = *sp++;
-            sp++;
-
-            p = (((r >> (8 - PNG_DITHER_RED_BITS)) &
-               ((1 << PNG_DITHER_RED_BITS) - 1)) <<
-               (PNG_DITHER_GREEN_BITS + PNG_DITHER_BLUE_BITS)) |
-               (((g >> (8 - PNG_DITHER_GREEN_BITS)) &
-               ((1 << PNG_DITHER_GREEN_BITS) - 1)) <<
-               (PNG_DITHER_BLUE_BITS)) |
-               ((b >> (8 - PNG_DITHER_BLUE_BITS)) &
-               ((1 << PNG_DITHER_BLUE_BITS) - 1));
-
-            *dp++ = palette_lookup[p];
-         }
-         row_info->color_type = PNG_COLOR_TYPE_PALETTE;
-         row_info->channels = 1;
-         row_info->pixel_depth = row_info->bit_depth;
-         row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,row_width);
-      }
-      else if (row_info->color_type == PNG_COLOR_TYPE_PALETTE &&
-         dither_lookup && row_info->bit_depth == 8)
-      {
-         sp = row;
-         for (i = 0; i < row_width; i++, sp++)
-         {
-            *sp = dither_lookup[*sp];
-         }
-      }
-   }
-}
-#endif
-
-#ifdef PNG_FLOATING_POINT_SUPPORTED
-#if defined(PNG_READ_GAMMA_SUPPORTED)
-static PNG_CONST int png_gamma_shift[] =
-   {0x10, 0x21, 0x42, 0x84, 0x110, 0x248, 0x550, 0xff0, 0x00};
-
-/* We build the 8- or 16-bit gamma tables here.  Note that for 16-bit
- * tables, we don't make a full table if we are reducing to 8-bit in
- * the future.  Note also how the gamma_16 tables are segmented so that
- * we don't need to allocate > 64K chunks for a full 16-bit table.
- */
-void /* PRIVATE */
-png_build_gamma_table(png_structp png_ptr)
-{
-  png_debug(1, "in png_build_gamma_table\n");
-
-  if (png_ptr->bit_depth <= 8)
-  {
-     int i;
-     double g;
-
-     if (png_ptr->screen_gamma > .000001)
-        g = 1.0 / (png_ptr->gamma * png_ptr->screen_gamma);
-     else
-        g = 1.0;
-
-     png_ptr->gamma_table = (png_bytep)png_malloc(png_ptr,
-        (png_uint_32)256);
-
-     for (i = 0; i < 256; i++)
-     {
-        png_ptr->gamma_table[i] = (png_byte)(pow((double)i / 255.0,
-           g) * 255.0 + .5);
-     }
-
-#if defined(PNG_READ_BACKGROUND_SUPPORTED) || \
-   defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
-     if (png_ptr->transformations & ((PNG_BACKGROUND) | PNG_RGB_TO_GRAY))
-     {
-
-        g = 1.0 / (png_ptr->gamma);
-
-        png_ptr->gamma_to_1 = (png_bytep)png_malloc(png_ptr,
-           (png_uint_32)256);
-
-        for (i = 0; i < 256; i++)
-        {
-           png_ptr->gamma_to_1[i] = (png_byte)(pow((double)i / 255.0,
-              g) * 255.0 + .5);
-        }
-
-
-        png_ptr->gamma_from_1 = (png_bytep)png_malloc(png_ptr,
-           (png_uint_32)256);
-
-        if(png_ptr->screen_gamma > 0.000001)
-           g = 1.0 / png_ptr->screen_gamma;
-        else
-           g = png_ptr->gamma;   /* probably doing rgb_to_gray */
-
-        for (i = 0; i < 256; i++)
-        {
-           png_ptr->gamma_from_1[i] = (png_byte)(pow((double)i / 255.0,
-              g) * 255.0 + .5);
-
-        }
-     }
-#endif /* PNG_READ_BACKGROUND_SUPPORTED || PNG_RGB_TO_GRAY_SUPPORTED */
-  }
-  else
-  {
-     double g;
-     int i, j, shift, num;
-     int sig_bit;
-     png_uint_32 ig;
-
-     if (png_ptr->color_type & PNG_COLOR_MASK_COLOR)
-     {
-        sig_bit = (int)png_ptr->sig_bit.red;
-        if ((int)png_ptr->sig_bit.green > sig_bit)
-           sig_bit = png_ptr->sig_bit.green;
-        if ((int)png_ptr->sig_bit.blue > sig_bit)
-           sig_bit = png_ptr->sig_bit.blue;
-     }
-     else
-     {
-        sig_bit = (int)png_ptr->sig_bit.gray;
-     }
-
-     if (sig_bit > 0)
-        shift = 16 - sig_bit;
-     else
-        shift = 0;
-
-     if (png_ptr->transformations & PNG_16_TO_8)
-     {
-        if (shift < (16 - PNG_MAX_GAMMA_8))
-           shift = (16 - PNG_MAX_GAMMA_8);
-     }
-
-     if (shift > 8)
-        shift = 8;
-     if (shift < 0)
-        shift = 0;
-
-     png_ptr->gamma_shift = (png_byte)shift;
-
-     num = (1 << (8 - shift));
-
-     if (png_ptr->screen_gamma > .000001)
-        g = 1.0 / (png_ptr->gamma * png_ptr->screen_gamma);
-     else
-        g = 1.0;
-
-     png_ptr->gamma_16_table = (png_uint_16pp)png_malloc(png_ptr,
-        (png_uint_32)(num * png_sizeof (png_uint_16p)));
-
-     if (png_ptr->transformations & (PNG_16_TO_8 | PNG_BACKGROUND))
-     {
-        double fin, fout;
-        png_uint_32 last, max;
-
-        for (i = 0; i < num; i++)
-        {
-           png_ptr->gamma_16_table[i] = (png_uint_16p)png_malloc(png_ptr,
-              (png_uint_32)(256 * png_sizeof (png_uint_16)));
-        }
-
-        g = 1.0 / g;
-        last = 0;
-        for (i = 0; i < 256; i++)
-        {
-           fout = ((double)i + 0.5) / 256.0;
-           fin = pow(fout, g);
-           max = (png_uint_32)(fin * (double)((png_uint_32)num << 8));
-           while (last <= max)
-           {
-              png_ptr->gamma_16_table[(int)(last & (0xff >> shift))]
-                 [(int)(last >> (8 - shift))] = (png_uint_16)(
-                 (png_uint_16)i | ((png_uint_16)i << 8));
-              last++;
-           }
-        }
-        while (last < ((png_uint_32)num << 8))
-        {
-           png_ptr->gamma_16_table[(int)(last & (0xff >> shift))]
-              [(int)(last >> (8 - shift))] = (png_uint_16)65535L;
-           last++;
-        }
-     }
-     else
-     {
-        for (i = 0; i < num; i++)
-        {
-           png_ptr->gamma_16_table[i] = (png_uint_16p)png_malloc(png_ptr,
-              (png_uint_32)(256 * png_sizeof (png_uint_16)));
-
-           ig = (((png_uint_32)i * (png_uint_32)png_gamma_shift[shift]) >> 4);
-           for (j = 0; j < 256; j++)
-           {
-              png_ptr->gamma_16_table[i][j] =
-                 (png_uint_16)(pow((double)(ig + ((png_uint_32)j << 8)) /
-                    65535.0, g) * 65535.0 + .5);
-           }
-        }
-     }
-
-#if defined(PNG_READ_BACKGROUND_SUPPORTED) || \
-   defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
-     if (png_ptr->transformations & (PNG_BACKGROUND | PNG_RGB_TO_GRAY))
-     {
-
-        g = 1.0 / (png_ptr->gamma);
-
-        png_ptr->gamma_16_to_1 = (png_uint_16pp)png_malloc(png_ptr,
-           (png_uint_32)(num * png_sizeof (png_uint_16p )));
-
-        for (i = 0; i < num; i++)
-        {
-           png_ptr->gamma_16_to_1[i] = (png_uint_16p)png_malloc(png_ptr,
-              (png_uint_32)(256 * png_sizeof (png_uint_16)));
-
-           ig = (((png_uint_32)i *
-              (png_uint_32)png_gamma_shift[shift]) >> 4);
-           for (j = 0; j < 256; j++)
-           {
-              png_ptr->gamma_16_to_1[i][j] =
-                 (png_uint_16)(pow((double)(ig + ((png_uint_32)j << 8)) /
-                    65535.0, g) * 65535.0 + .5);
-           }
-        }
-
-        if(png_ptr->screen_gamma > 0.000001)
-           g = 1.0 / png_ptr->screen_gamma;
-        else
-           g = png_ptr->gamma;   /* probably doing rgb_to_gray */
-
-        png_ptr->gamma_16_from_1 = (png_uint_16pp)png_malloc(png_ptr,
-           (png_uint_32)(num * png_sizeof (png_uint_16p)));
-
-        for (i = 0; i < num; i++)
-        {
-           png_ptr->gamma_16_from_1[i] = (png_uint_16p)png_malloc(png_ptr,
-              (png_uint_32)(256 * png_sizeof (png_uint_16)));
-
-           ig = (((png_uint_32)i *
-              (png_uint_32)png_gamma_shift[shift]) >> 4);
-           for (j = 0; j < 256; j++)
-           {
-              png_ptr->gamma_16_from_1[i][j] =
-                 (png_uint_16)(pow((double)(ig + ((png_uint_32)j << 8)) /
-                    65535.0, g) * 65535.0 + .5);
-           }
-        }
-     }
-#endif /* PNG_READ_BACKGROUND_SUPPORTED || PNG_RGB_TO_GRAY_SUPPORTED */
-  }
-}
-#endif
-/* To do: install integer version of png_build_gamma_table here */
-#endif
-
-#if defined(PNG_MNG_FEATURES_SUPPORTED)
-/* undoes intrapixel differencing  */
-void /* PRIVATE */
-png_do_read_intrapixel(png_row_infop row_info, png_bytep row)
-{
-   png_debug(1, "in png_do_read_intrapixel\n");
-   if (
-#if defined(PNG_USELESS_TESTS_SUPPORTED)
-       row != NULL && row_info != NULL &&
-#endif
-       (row_info->color_type & PNG_COLOR_MASK_COLOR))
-   {
-      int bytes_per_pixel;
-      png_uint_32 row_width = row_info->width;
-      if (row_info->bit_depth == 8)
-      {
-         png_bytep rp;
-         png_uint_32 i;
-
-         if (row_info->color_type == PNG_COLOR_TYPE_RGB)
-            bytes_per_pixel = 3;
-         else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
-            bytes_per_pixel = 4;
-         else
-            return;
-
-         for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
-         {
-            *(rp) = (png_byte)((256 + *rp + *(rp+1))&0xff);
-            *(rp+2) = (png_byte)((256 + *(rp+2) + *(rp+1))&0xff);
-         }
-      }
-      else if (row_info->bit_depth == 16)
-      {
-         png_bytep rp;
-         png_uint_32 i;
-
-         if (row_info->color_type == PNG_COLOR_TYPE_RGB)
-            bytes_per_pixel = 6;
-         else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
-            bytes_per_pixel = 8;
-         else
-            return;
-
-         for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
-         {
-            png_uint_32 s0   = (*(rp  ) << 8) | *(rp+1);
-            png_uint_32 s1   = (*(rp+2) << 8) | *(rp+3);
-            png_uint_32 s2   = (*(rp+4) << 8) | *(rp+5);
-            png_uint_32 red  = (png_uint_32)((s0+s1+65536L) & 0xffffL);
-            png_uint_32 blue = (png_uint_32)((s2+s1+65536L) & 0xffffL);
-            *(rp  ) = (png_byte)((red >> 8) & 0xff);
-            *(rp+1) = (png_byte)(red & 0xff);
-            *(rp+4) = (png_byte)((blue >> 8) & 0xff);
-            *(rp+5) = (png_byte)(blue & 0xff);
-         }
-      }
-   }
-}
-#endif /* PNG_MNG_FEATURES_SUPPORTED */
-#endif /* PNG_READ_SUPPORTED */
+\r
+/* pngrtran.c - transforms the data in a row for PNG readers\r
+ *\r
+ * Last changed in libpng 1.4.2 [May 6, 2010]\r
+ * Copyright (c) 1998-2010 Glenn Randers-Pehrson\r
+ * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)\r
+ * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)\r
+ *\r
+ * This code is released under the libpng license.\r
+ * For conditions of distribution and use, see the disclaimer\r
+ * and license in png.h\r
+ *\r
+ * This file contains functions optionally called by an application\r
+ * in order to tell libpng how to handle data when reading a PNG.\r
+ * Transformations that are used in both reading and writing are\r
+ * in pngtrans.c.\r
+ */\r
+\r
+#define PNG_NO_PEDANTIC_WARNINGS\r
+#include "png.h"\r
+#ifdef PNG_READ_SUPPORTED\r
+#include "pngpriv.h"\r
+\r
+/* Set the action on getting a CRC error for an ancillary or critical chunk. */\r
+void PNGAPI\r
+png_set_crc_action(png_structp png_ptr, int crit_action, int ancil_action)\r
+{\r
+   png_debug(1, "in png_set_crc_action");\r
\r
+   if (png_ptr == NULL)\r
+      return;\r
+\r
+   /* Tell libpng how we react to CRC errors in critical chunks */\r
+   switch (crit_action)\r
+   {\r
+      case PNG_CRC_NO_CHANGE:                        /* Leave setting as is */\r
+         break;\r
+\r
+      case PNG_CRC_WARN_USE:                               /* Warn/use data */\r
+         png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK;\r
+         png_ptr->flags |= PNG_FLAG_CRC_CRITICAL_USE;\r
+         break;\r
+\r
+      case PNG_CRC_QUIET_USE:                             /* Quiet/use data */\r
+         png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK;\r
+         png_ptr->flags |= PNG_FLAG_CRC_CRITICAL_USE |\r
+                           PNG_FLAG_CRC_CRITICAL_IGNORE;\r
+         break;\r
+\r
+      case PNG_CRC_WARN_DISCARD:    /* Not a valid action for critical data */\r
+         png_warning(png_ptr,\r
+            "Can't discard critical data on CRC error");\r
+      case PNG_CRC_ERROR_QUIT:                                /* Error/quit */\r
+\r
+      case PNG_CRC_DEFAULT:\r
+      default:\r
+         png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK;\r
+         break;\r
+   }\r
+\r
+   /* Tell libpng how we react to CRC errors in ancillary chunks */\r
+   switch (ancil_action)\r
+   {\r
+      case PNG_CRC_NO_CHANGE:                       /* Leave setting as is */\r
+         break;\r
+\r
+      case PNG_CRC_WARN_USE:                              /* Warn/use data */\r
+         png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK;\r
+         png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_USE;\r
+         break;\r
+\r
+      case PNG_CRC_QUIET_USE:                            /* Quiet/use data */\r
+         png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK;\r
+         png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_USE |\r
+                           PNG_FLAG_CRC_ANCILLARY_NOWARN;\r
+         break;\r
+\r
+      case PNG_CRC_ERROR_QUIT:                               /* Error/quit */\r
+         png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK;\r
+         png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_NOWARN;\r
+         break;\r
+\r
+      case PNG_CRC_WARN_DISCARD:                      /* Warn/discard data */\r
+\r
+      case PNG_CRC_DEFAULT:\r
+      default:\r
+         png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK;\r
+         break;\r
+   }\r
+}\r
+\r
+#if defined(PNG_READ_BACKGROUND_SUPPORTED) && \\r
+    defined(PNG_FLOATING_POINT_SUPPORTED)\r
+/* Handle alpha and tRNS via a background color */\r
+void PNGAPI\r
+png_set_background(png_structp png_ptr,\r
+   png_color_16p background_color, int background_gamma_code,\r
+   int need_expand, double background_gamma)\r
+{\r
+   png_debug(1, "in png_set_background");\r
\r
+   if (png_ptr == NULL)\r
+      return;\r
+   if (background_gamma_code == PNG_BACKGROUND_GAMMA_UNKNOWN)\r
+   {\r
+      png_warning(png_ptr, "Application must supply a known background gamma");\r
+      return;\r
+   }\r
+\r
+   png_ptr->transformations |= PNG_BACKGROUND;\r
+   png_memcpy(&(png_ptr->background), background_color,\r
+      png_sizeof(png_color_16));\r
+   png_ptr->background_gamma = (float)background_gamma;\r
+   png_ptr->background_gamma_type = (png_byte)(background_gamma_code);\r
+   png_ptr->transformations |= (need_expand ? PNG_BACKGROUND_EXPAND : 0);\r
+}\r
+#endif\r
+\r
+#ifdef PNG_READ_16_TO_8_SUPPORTED\r
+/* Strip 16 bit depth files to 8 bit depth */\r
+void PNGAPI\r
+png_set_strip_16(png_structp png_ptr)\r
+{\r
+   png_debug(1, "in png_set_strip_16");\r
+\r
+   if (png_ptr == NULL)\r
+      return;\r
+   png_ptr->transformations |= PNG_16_TO_8;\r
+}\r
+#endif\r
+\r
+#ifdef PNG_READ_STRIP_ALPHA_SUPPORTED\r
+void PNGAPI\r
+png_set_strip_alpha(png_structp png_ptr)\r
+{\r
+   png_debug(1, "in png_set_strip_alpha");\r
+\r
+   if (png_ptr == NULL)\r
+      return;\r
+   png_ptr->flags |= PNG_FLAG_STRIP_ALPHA;\r
+}\r
+#endif\r
+\r
+#ifdef PNG_READ_QUANTIZE_SUPPORTED\r
+/* Quantize file to 8 bit.  Supply a palette, the current number\r
+ * of elements in the palette, the maximum number of elements\r
+ * allowed, and a histogram if possible.  If the current number\r
+ * of colors is greater then the maximum number, the palette will be\r
+ * modified to fit in the maximum number.  "full_quantize" indicates\r
+ * whether we need a quantizeing cube set up for RGB images, or if we\r
+ * simply are reducing the number of colors in a paletted image.\r
+ */\r
+\r
+typedef struct png_dsort_struct\r
+{\r
+   struct png_dsort_struct FAR * next;\r
+   png_byte left;\r
+   png_byte right;\r
+} png_dsort;\r
+typedef png_dsort FAR *       png_dsortp;\r
+typedef png_dsort FAR * FAR * png_dsortpp;\r
+\r
+void PNGAPI\r
+png_set_quantize(png_structp png_ptr, png_colorp palette,\r
+   int num_palette, int maximum_colors, png_uint_16p histogram,\r
+   int full_quantize)\r
+{\r
+   png_debug(1, "in png_set_quantize");\r
+\r
+   if (png_ptr == NULL)\r
+      return;\r
+   png_ptr->transformations |= PNG_QUANTIZE;\r
+\r
+   if (!full_quantize)\r
+   {\r
+      int i;\r
+\r
+      png_ptr->quantize_index = (png_bytep)png_malloc(png_ptr,\r
+         (png_uint_32)(num_palette * png_sizeof(png_byte)));\r
+      for (i = 0; i < num_palette; i++)\r
+         png_ptr->quantize_index[i] = (png_byte)i;\r
+   }\r
+\r
+   if (num_palette > maximum_colors)\r
+   {\r
+      if (histogram != NULL)\r
+      {\r
+         /* This is easy enough, just throw out the least used colors.\r
+          * Perhaps not the best solution, but good enough.\r
+          */\r
+\r
+         int i;\r
+\r
+         /* Initialize an array to sort colors */\r
+         png_ptr->quantize_sort = (png_bytep)png_malloc(png_ptr,\r
+            (png_uint_32)(num_palette * png_sizeof(png_byte)));\r
+\r
+         /* Initialize the quantize_sort array */\r
+         for (i = 0; i < num_palette; i++)\r
+            png_ptr->quantize_sort[i] = (png_byte)i;\r
+\r
+         /* Find the least used palette entries by starting a\r
+          * bubble sort, and running it until we have sorted\r
+          * out enough colors.  Note that we don't care about\r
+          * sorting all the colors, just finding which are\r
+          * least used.\r
+          */\r
+\r
+         for (i = num_palette - 1; i >= maximum_colors; i--)\r
+         {\r
+            int done; /* To stop early if the list is pre-sorted */\r
+            int j;\r
+\r
+            done = 1;\r
+            for (j = 0; j < i; j++)\r
+            {\r
+               if (histogram[png_ptr->quantize_sort[j]]\r
+                   < histogram[png_ptr->quantize_sort[j + 1]])\r
+               {\r
+                  png_byte t;\r
+\r
+                  t = png_ptr->quantize_sort[j];\r
+                  png_ptr->quantize_sort[j] = png_ptr->quantize_sort[j + 1];\r
+                  png_ptr->quantize_sort[j + 1] = t;\r
+                  done = 0;\r
+               }\r
+            }\r
+            if (done)\r
+               break;\r
+         }\r
+\r
+         /* Swap the palette around, and set up a table, if necessary */\r
+         if (full_quantize)\r
+         {\r
+            int j = num_palette;\r
+\r
+            /* Put all the useful colors within the max, but don't\r
+             * move the others.\r
+             */\r
+            for (i = 0; i < maximum_colors; i++)\r
+            {\r
+               if ((int)png_ptr->quantize_sort[i] >= maximum_colors)\r
+               {\r
+                  do\r
+                     j--;\r
+                  while ((int)png_ptr->quantize_sort[j] >= maximum_colors);\r
+                  palette[i] = palette[j];\r
+               }\r
+            }\r
+         }\r
+         else\r
+         {\r
+            int j = num_palette;\r
+\r
+            /* Move all the used colors inside the max limit, and\r
+             * develop a translation table.\r
+             */\r
+            for (i = 0; i < maximum_colors; i++)\r
+            {\r
+               /* Only move the colors we need to */\r
+               if ((int)png_ptr->quantize_sort[i] >= maximum_colors)\r
+               {\r
+                  png_color tmp_color;\r
+\r
+                  do\r
+                     j--;\r
+                  while ((int)png_ptr->quantize_sort[j] >= maximum_colors);\r
+\r
+                  tmp_color = palette[j];\r
+                  palette[j] = palette[i];\r
+                  palette[i] = tmp_color;\r
+                  /* Indicate where the color went */\r
+                  png_ptr->quantize_index[j] = (png_byte)i;\r
+                  png_ptr->quantize_index[i] = (png_byte)j;\r
+               }\r
+            }\r
+\r
+            /* Find closest color for those colors we are not using */\r
+            for (i = 0; i < num_palette; i++)\r
+            {\r
+               if ((int)png_ptr->quantize_index[i] >= maximum_colors)\r
+               {\r
+                  int min_d, k, min_k, d_index;\r
+\r
+                  /* Find the closest color to one we threw out */\r
+                  d_index = png_ptr->quantize_index[i];\r
+                  min_d = PNG_COLOR_DIST(palette[d_index], palette[0]);\r
+                  for (k = 1, min_k = 0; k < maximum_colors; k++)\r
+                  {\r
+                     int d;\r
+\r
+                     d = PNG_COLOR_DIST(palette[d_index], palette[k]);\r
+\r
+                     if (d < min_d)\r
+                     {\r
+                        min_d = d;\r
+                        min_k = k;\r
+                     }\r
+                  }\r
+                  /* Point to closest color */\r
+                  png_ptr->quantize_index[i] = (png_byte)min_k;\r
+               }\r
+            }\r
+         }\r
+         png_free(png_ptr, png_ptr->quantize_sort);\r
+         png_ptr->quantize_sort = NULL;\r
+      }\r
+      else\r
+      {\r
+         /* This is much harder to do simply (and quickly).  Perhaps\r
+          * we need to go through a median cut routine, but those\r
+          * don't always behave themselves with only a few colors\r
+          * as input.  So we will just find the closest two colors,\r
+          * and throw out one of them (chosen somewhat randomly).\r
+          * [We don't understand this at all, so if someone wants to\r
+          *  work on improving it, be our guest - AED, GRP]\r
+          */\r
+         int i;\r
+         int max_d;\r
+         int num_new_palette;\r
+         png_dsortp t;\r
+         png_dsortpp hash;\r
+\r
+         t = NULL;\r
+\r
+         /* Initialize palette index arrays */\r
+         png_ptr->index_to_palette = (png_bytep)png_malloc(png_ptr,\r
+            (png_uint_32)(num_palette * png_sizeof(png_byte)));\r
+         png_ptr->palette_to_index = (png_bytep)png_malloc(png_ptr,\r
+            (png_uint_32)(num_palette * png_sizeof(png_byte)));\r
+\r
+         /* Initialize the sort array */\r
+         for (i = 0; i < num_palette; i++)\r
+         {\r
+            png_ptr->index_to_palette[i] = (png_byte)i;\r
+            png_ptr->palette_to_index[i] = (png_byte)i;\r
+         }\r
+\r
+         hash = (png_dsortpp)png_calloc(png_ptr, (png_uint_32)(769 *\r
+            png_sizeof(png_dsortp)));\r
+\r
+         num_new_palette = num_palette;\r
+\r
+         /* Initial wild guess at how far apart the farthest pixel\r
+          * pair we will be eliminating will be.  Larger\r
+          * numbers mean more areas will be allocated, Smaller\r
+          * numbers run the risk of not saving enough data, and\r
+          * having to do this all over again.\r
+          *\r
+          * I have not done extensive checking on this number.\r
+          */\r
+         max_d = 96;\r
+\r
+         while (num_new_palette > maximum_colors)\r
+         {\r
+            for (i = 0; i < num_new_palette - 1; i++)\r
+            {\r
+               int j;\r
+\r
+               for (j = i + 1; j < num_new_palette; j++)\r
+               {\r
+                  int d;\r
+\r
+                  d = PNG_COLOR_DIST(palette[i], palette[j]);\r
+\r
+                  if (d <= max_d)\r
+                  {\r
+\r
+                     t = (png_dsortp)png_malloc_warn(png_ptr,\r
+                         (png_uint_32)(png_sizeof(png_dsort)));\r
+                     if (t == NULL)\r
+                         break;\r
+                     t->next = hash[d];\r
+                     t->left = (png_byte)i;\r
+                     t->right = (png_byte)j;\r
+                     hash[d] = t;\r
+                  }\r
+               }\r
+               if (t == NULL)\r
+                  break;\r
+            }\r
+\r
+            if (t != NULL)\r
+            for (i = 0; i <= max_d; i++)\r
+            {\r
+               if (hash[i] != NULL)\r
+               {\r
+                  png_dsortp p;\r
+\r
+                  for (p = hash[i]; p; p = p->next)\r
+                  {\r
+                     if ((int)png_ptr->index_to_palette[p->left]\r
+                        < num_new_palette &&\r
+                        (int)png_ptr->index_to_palette[p->right]\r
+                        < num_new_palette)\r
+                     {\r
+                        int j, next_j;\r
+\r
+                        if (num_new_palette & 0x01)\r
+                        {\r
+                           j = p->left;\r
+                           next_j = p->right;\r
+                        }\r
+                        else\r
+                        {\r
+                           j = p->right;\r
+                           next_j = p->left;\r
+                        }\r
+\r
+                        num_new_palette--;\r
+                        palette[png_ptr->index_to_palette[j]]\r
+                          = palette[num_new_palette];\r
+                        if (!full_quantize)\r
+                        {\r
+                           int k;\r
+\r
+                           for (k = 0; k < num_palette; k++)\r
+                           {\r
+                              if (png_ptr->quantize_index[k] ==\r
+                                 png_ptr->index_to_palette[j])\r
+                                 png_ptr->quantize_index[k] =\r
+                                    png_ptr->index_to_palette[next_j];\r
+                              if ((int)png_ptr->quantize_index[k] ==\r
+                                 num_new_palette)\r
+                                 png_ptr->quantize_index[k] =\r
+                                    png_ptr->index_to_palette[j];\r
+                           }\r
+                        }\r
+\r
+                        png_ptr->index_to_palette[png_ptr->palette_to_index\r
+                           [num_new_palette]] = png_ptr->index_to_palette[j];\r
+                        png_ptr->palette_to_index[png_ptr->index_to_palette[j]]\r
+                           = png_ptr->palette_to_index[num_new_palette];\r
+\r
+                        png_ptr->index_to_palette[j] =\r
+                            (png_byte)num_new_palette;\r
+                        png_ptr->palette_to_index[num_new_palette] =\r
+                            (png_byte)j;\r
+                     }\r
+                     if (num_new_palette <= maximum_colors)\r
+                        break;\r
+                  }\r
+                  if (num_new_palette <= maximum_colors)\r
+                     break;\r
+               }\r
+            }\r
+\r
+            for (i = 0; i < 769; i++)\r
+            {\r
+               if (hash[i] != NULL)\r
+               {\r
+                  png_dsortp p = hash[i];\r
+                  while (p)\r
+                  {\r
+                     t = p->next;\r
+                     png_free(png_ptr, p);\r
+                     p = t;\r
+                  }\r
+               }\r
+               hash[i] = 0;\r
+            }\r
+            max_d += 96;\r
+         }\r
+         png_free(png_ptr, hash);\r
+         png_free(png_ptr, png_ptr->palette_to_index);\r
+         png_free(png_ptr, png_ptr->index_to_palette);\r
+         png_ptr->palette_to_index = NULL;\r
+         png_ptr->index_to_palette = NULL;\r
+      }\r
+      num_palette = maximum_colors;\r
+   }\r
+   if (png_ptr->palette == NULL)\r
+   {\r
+      png_ptr->palette = palette;\r
+   }\r
+   png_ptr->num_palette = (png_uint_16)num_palette;\r
+\r
+   if (full_quantize)\r
+   {\r
+      int i;\r
+      png_bytep distance;\r
+      int total_bits = PNG_QUANTIZE_RED_BITS + PNG_QUANTIZE_GREEN_BITS +\r
+         PNG_QUANTIZE_BLUE_BITS;\r
+      int num_red = (1 << PNG_QUANTIZE_RED_BITS);\r
+      int num_green = (1 << PNG_QUANTIZE_GREEN_BITS);\r
+      int num_blue = (1 << PNG_QUANTIZE_BLUE_BITS);\r
+      png_size_t num_entries = ((png_size_t)1 << total_bits);\r
+\r
+      png_ptr->palette_lookup = (png_bytep )png_calloc(png_ptr,\r
+         (png_uint_32)(num_entries * png_sizeof(png_byte)));\r
+\r
+      distance = (png_bytep)png_malloc(png_ptr, (png_uint_32)(num_entries *\r
+         png_sizeof(png_byte)));\r
+      png_memset(distance, 0xff, num_entries * png_sizeof(png_byte));\r
+\r
+      for (i = 0; i < num_palette; i++)\r
+      {\r
+         int ir, ig, ib;\r
+         int r = (palette[i].red >> (8 - PNG_QUANTIZE_RED_BITS));\r
+         int g = (palette[i].green >> (8 - PNG_QUANTIZE_GREEN_BITS));\r
+         int b = (palette[i].blue >> (8 - PNG_QUANTIZE_BLUE_BITS));\r
+\r
+         for (ir = 0; ir < num_red; ir++)\r
+         {\r
+            /* int dr = abs(ir - r); */\r
+            int dr = ((ir > r) ? ir - r : r - ir);\r
+            int index_r = (ir << (PNG_QUANTIZE_BLUE_BITS +\r
+                PNG_QUANTIZE_GREEN_BITS));\r
+\r
+            for (ig = 0; ig < num_green; ig++)\r
+            {\r
+               /* int dg = abs(ig - g); */\r
+               int dg = ((ig > g) ? ig - g : g - ig);\r
+               int dt = dr + dg;\r
+               int dm = ((dr > dg) ? dr : dg);\r
+               int index_g = index_r | (ig << PNG_QUANTIZE_BLUE_BITS);\r
+\r
+               for (ib = 0; ib < num_blue; ib++)\r
+               {\r
+                  int d_index = index_g | ib;\r
+                  /* int db = abs(ib - b); */\r
+                  int db = ((ib > b) ? ib - b : b - ib);\r
+                  int dmax = ((dm > db) ? dm : db);\r
+                  int d = dmax + dt + db;\r
+\r
+                  if (d < (int)distance[d_index])\r
+                  {\r
+                     distance[d_index] = (png_byte)d;\r
+                     png_ptr->palette_lookup[d_index] = (png_byte)i;\r
+                  }\r
+               }\r
+            }\r
+         }\r
+      }\r
+\r
+      png_free(png_ptr, distance);\r
+   }\r
+}\r
+#endif /* PNG_READ_QUANTIZE_SUPPORTED */\r
+\r
+#if defined(PNG_READ_GAMMA_SUPPORTED) && defined(PNG_FLOATING_POINT_SUPPORTED)\r
+/* Transform the image from the file_gamma to the screen_gamma.  We\r
+ * only do transformations on images where the file_gamma and screen_gamma\r
+ * are not close reciprocals, otherwise it slows things down slightly, and\r
+ * also needlessly introduces small errors.\r
+ *\r
+ * We will turn off gamma transformation later if no semitransparent entries\r
+ * are present in the tRNS array for palette images.  We can't do it here\r
+ * because we don't necessarily have the tRNS chunk yet.\r
+ */\r
+void PNGAPI\r
+png_set_gamma(png_structp png_ptr, double scrn_gamma, double file_gamma)\r
+{\r
+   png_debug(1, "in png_set_gamma");\r
+\r
+   if (png_ptr == NULL)\r
+      return;\r
+\r
+   if ((fabs(scrn_gamma * file_gamma - 1.0) > PNG_GAMMA_THRESHOLD) ||\r
+       (png_ptr->color_type & PNG_COLOR_MASK_ALPHA) ||\r
+       (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE))\r
+     png_ptr->transformations |= PNG_GAMMA;\r
+   png_ptr->gamma = (float)file_gamma;\r
+   png_ptr->screen_gamma = (float)scrn_gamma;\r
+}\r
+#endif\r
+\r
+#ifdef PNG_READ_EXPAND_SUPPORTED\r
+/* Expand paletted images to RGB, expand grayscale images of\r
+ * less than 8-bit depth to 8-bit depth, and expand tRNS chunks\r
+ * to alpha channels.\r
+ */\r
+void PNGAPI\r
+png_set_expand(png_structp png_ptr)\r
+{\r
+   png_debug(1, "in png_set_expand");\r
+\r
+   if (png_ptr == NULL)\r
+      return;\r
+\r
+   png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS);\r
+   png_ptr->flags &= ~PNG_FLAG_ROW_INIT;\r
+}\r
+\r
+/* GRR 19990627:  the following three functions currently are identical\r
+ *  to png_set_expand().  However, it is entirely reasonable that someone\r
+ *  might wish to expand an indexed image to RGB but *not* expand a single,\r
+ *  fully transparent palette entry to a full alpha channel--perhaps instead\r
+ *  convert tRNS to the grayscale/RGB format (16-bit RGB value), or replace\r
+ *  the transparent color with a particular RGB value, or drop tRNS entirely.\r
+ *  IOW, a future version of the library may make the transformations flag\r
+ *  a bit more fine-grained, with separate bits for each of these three\r
+ *  functions.\r
+ *\r
+ *  More to the point, these functions make it obvious what libpng will be\r
+ *  doing, whereas "expand" can (and does) mean any number of things.\r
+ *\r
+ *  GRP 20060307: In libpng-1.2.9, png_set_gray_1_2_4_to_8() was modified\r
+ *  to expand only the sample depth but not to expand the tRNS to alpha\r
+ *  and its name was changed to png_set_expand_gray_1_2_4_to_8().\r
+ */\r
+\r
+/* Expand paletted images to RGB. */\r
+void PNGAPI\r
+png_set_palette_to_rgb(png_structp png_ptr)\r
+{\r
+   png_debug(1, "in png_set_palette_to_rgb");\r
+\r
+   if (png_ptr == NULL)\r
+      return;\r
+\r
+   png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS);\r
+   png_ptr->flags &= ~PNG_FLAG_ROW_INIT;\r
+}\r
+\r
+/* Expand grayscale images of less than 8-bit depth to 8 bits. */\r
+void PNGAPI\r
+png_set_expand_gray_1_2_4_to_8(png_structp png_ptr)\r
+{\r
+   png_debug(1, "in png_set_expand_gray_1_2_4_to_8");\r
+\r
+   if (png_ptr == NULL)\r
+      return;\r
+\r
+   png_ptr->transformations |= PNG_EXPAND;\r
+   png_ptr->flags &= ~PNG_FLAG_ROW_INIT;\r
+}\r
+\r
+\r
+\r
+/* Expand tRNS chunks to alpha channels. */\r
+void PNGAPI\r
+png_set_tRNS_to_alpha(png_structp png_ptr)\r
+{\r
+   png_debug(1, "in png_set_tRNS_to_alpha");\r
+\r
+   png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS);\r
+   png_ptr->flags &= ~PNG_FLAG_ROW_INIT;\r
+}\r
+#endif /* defined(PNG_READ_EXPAND_SUPPORTED) */\r
+\r
+#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED\r
+void PNGAPI\r
+png_set_gray_to_rgb(png_structp png_ptr)\r
+{\r
+   png_debug(1, "in png_set_gray_to_rgb");\r
+\r
+   png_ptr->transformations |= PNG_GRAY_TO_RGB;\r
+   png_ptr->flags &= ~PNG_FLAG_ROW_INIT;\r
+}\r
+#endif\r
+\r
+#ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED\r
+#ifdef PNG_FLOATING_POINT_SUPPORTED\r
+/* Convert a RGB image to a grayscale of the same width.  This allows us,\r
+ * for example, to convert a 24 bpp RGB image into an 8 bpp grayscale image.\r
+ */\r
+\r
+void PNGAPI\r
+png_set_rgb_to_gray(png_structp png_ptr, int error_action, double red,\r
+   double green)\r
+{\r
+   int red_fixed = (int)((float)red*100000.0 + 0.5);\r
+   int green_fixed = (int)((float)green*100000.0 + 0.5);\r
+   if (png_ptr == NULL)\r
+      return;\r
+   png_set_rgb_to_gray_fixed(png_ptr, error_action, red_fixed, green_fixed);\r
+}\r
+#endif\r
+\r
+void PNGAPI\r
+png_set_rgb_to_gray_fixed(png_structp png_ptr, int error_action,\r
+   png_fixed_point red, png_fixed_point green)\r
+{\r
+   png_debug(1, "in png_set_rgb_to_gray");\r
+\r
+   if (png_ptr == NULL)\r
+      return;\r
+\r
+   switch(error_action)\r
+   {\r
+      case 1: png_ptr->transformations |= PNG_RGB_TO_GRAY;\r
+              break;\r
+\r
+      case 2: png_ptr->transformations |= PNG_RGB_TO_GRAY_WARN;\r
+              break;\r
+\r
+      case 3: png_ptr->transformations |= PNG_RGB_TO_GRAY_ERR;\r
+   }\r
+   if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)\r
+#ifdef PNG_READ_EXPAND_SUPPORTED\r
+      png_ptr->transformations |= PNG_EXPAND;\r
+#else\r
+   {\r
+      png_warning(png_ptr,\r
+        "Cannot do RGB_TO_GRAY without EXPAND_SUPPORTED");\r
+      png_ptr->transformations &= ~PNG_RGB_TO_GRAY;\r
+   }\r
+#endif\r
+   {\r
+      png_uint_16 red_int, green_int;\r
+      if (red < 0 || green < 0)\r
+      {\r
+         red_int   =  6968; /* .212671 * 32768 + .5 */\r
+         green_int = 23434; /* .715160 * 32768 + .5 */\r
+      }\r
+      else if (red + green < 100000L)\r
+      {\r
+         red_int = (png_uint_16)(((png_uint_32)red*32768L)/100000L);\r
+         green_int = (png_uint_16)(((png_uint_32)green*32768L)/100000L);\r
+      }\r
+      else\r
+      {\r
+         png_warning(png_ptr, "ignoring out of range rgb_to_gray coefficients");\r
+         red_int   =  6968;\r
+         green_int = 23434;\r
+      }\r
+      png_ptr->rgb_to_gray_red_coeff   = red_int;\r
+      png_ptr->rgb_to_gray_green_coeff = green_int;\r
+      png_ptr->rgb_to_gray_blue_coeff  =\r
+         (png_uint_16)(32768 - red_int - green_int);\r
+   }\r
+}\r
+#endif\r
+\r
+#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \\r
+    defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)\r
+void PNGAPI\r
+png_set_read_user_transform_fn(png_structp png_ptr, png_user_transform_ptr\r
+   read_user_transform_fn)\r
+{\r
+   png_debug(1, "in png_set_read_user_transform_fn");\r
+\r
+   if (png_ptr == NULL)\r
+      return;\r
+\r
+#ifdef PNG_READ_USER_TRANSFORM_SUPPORTED\r
+   png_ptr->transformations |= PNG_USER_TRANSFORM;\r
+   png_ptr->read_user_transform_fn = read_user_transform_fn;\r
+#endif\r
+}\r
+#endif\r
+\r
+/* Initialize everything needed for the read.  This includes modifying\r
+ * the palette.\r
+ */\r
+void /* PRIVATE */\r
+png_init_read_transformations(png_structp png_ptr)\r
+{\r
+   png_debug(1, "in png_init_read_transformations");\r
+\r
+  {\r
+#if defined(PNG_READ_BACKGROUND_SUPPORTED) || \\r
+    defined(PNG_READ_SHIFT_SUPPORTED) || \\r
+    defined(PNG_READ_GAMMA_SUPPORTED)\r
+   int color_type = png_ptr->color_type;\r
+#endif\r
+\r
+#if defined(PNG_READ_EXPAND_SUPPORTED) && defined(PNG_READ_BACKGROUND_SUPPORTED)\r
+\r
+#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED\r
+   /* Detect gray background and attempt to enable optimization\r
+    * for gray --> RGB case\r
+    *\r
+    * Note:  if PNG_BACKGROUND_EXPAND is set and color_type is either RGB or\r
+    * RGB_ALPHA (in which case need_expand is superfluous anyway), the\r
+    * background color might actually be gray yet not be flagged as such.\r
+    * This is not a problem for the current code, which uses\r
+    * PNG_BACKGROUND_IS_GRAY only to decide when to do the\r
+    * png_do_gray_to_rgb() transformation.\r
+    */\r
+   if ((png_ptr->transformations & PNG_BACKGROUND_EXPAND) &&\r
+       !(color_type & PNG_COLOR_MASK_COLOR))\r
+   {\r
+          png_ptr->mode |= PNG_BACKGROUND_IS_GRAY;\r
+   } else if ((png_ptr->transformations & PNG_BACKGROUND) &&\r
+              !(png_ptr->transformations & PNG_BACKGROUND_EXPAND) &&\r
+              (png_ptr->transformations & PNG_GRAY_TO_RGB) &&\r
+              png_ptr->background.red == png_ptr->background.green &&\r
+              png_ptr->background.red == png_ptr->background.blue)\r
+   {\r
+          png_ptr->mode |= PNG_BACKGROUND_IS_GRAY;\r
+          png_ptr->background.gray = png_ptr->background.red;\r
+   }\r
+#endif\r
+\r
+   if ((png_ptr->transformations & PNG_BACKGROUND_EXPAND) &&\r
+       (png_ptr->transformations & PNG_EXPAND))\r
+   {\r
+      if (!(color_type & PNG_COLOR_MASK_COLOR))  /* i.e., GRAY or GRAY_ALPHA */\r
+      {\r
+         /* Expand background and tRNS chunks */\r
+         switch (png_ptr->bit_depth)\r
+         {\r
+            case 1:\r
+               png_ptr->background.gray *= (png_uint_16)0xff;\r
+               png_ptr->background.red = png_ptr->background.green\r
+                 =  png_ptr->background.blue = png_ptr->background.gray;\r
+               if (!(png_ptr->transformations & PNG_EXPAND_tRNS))\r
+               {\r
+                 png_ptr->trans_color.gray *= (png_uint_16)0xff;\r
+                 png_ptr->trans_color.red = png_ptr->trans_color.green\r
+                   = png_ptr->trans_color.blue = png_ptr->trans_color.gray;\r
+               }\r
+               break;\r
+\r
+            case 2:\r
+               png_ptr->background.gray *= (png_uint_16)0x55;\r
+               png_ptr->background.red = png_ptr->background.green\r
+                 = png_ptr->background.blue = png_ptr->background.gray;\r
+               if (!(png_ptr->transformations & PNG_EXPAND_tRNS))\r
+               {\r
+                 png_ptr->trans_color.gray *= (png_uint_16)0x55;\r
+                 png_ptr->trans_color.red = png_ptr->trans_color.green\r
+                   = png_ptr->trans_color.blue = png_ptr->trans_color.gray;\r
+               }\r
+               break;\r
+\r
+            case 4:\r
+               png_ptr->background.gray *= (png_uint_16)0x11;\r
+               png_ptr->background.red = png_ptr->background.green\r
+                 = png_ptr->background.blue = png_ptr->background.gray;\r
+               if (!(png_ptr->transformations & PNG_EXPAND_tRNS))\r
+               {\r
+                 png_ptr->trans_color.gray *= (png_uint_16)0x11;\r
+                 png_ptr->trans_color.red = png_ptr->trans_color.green\r
+                   = png_ptr->trans_color.blue = png_ptr->trans_color.gray;\r
+               }\r
+               break;\r
+\r
+            case 8:\r
+\r
+            case 16:\r
+               png_ptr->background.red = png_ptr->background.green\r
+                 = png_ptr->background.blue = png_ptr->background.gray;\r
+               break;\r
+         }\r
+      }\r
+      else if (color_type == PNG_COLOR_TYPE_PALETTE)\r
+      {\r
+         png_ptr->background.red   =\r
+            png_ptr->palette[png_ptr->background.index].red;\r
+         png_ptr->background.green =\r
+            png_ptr->palette[png_ptr->background.index].green;\r
+         png_ptr->background.blue  =\r
+            png_ptr->palette[png_ptr->background.index].blue;\r
+\r
+#ifdef PNG_READ_INVERT_ALPHA_SUPPORTED\r
+        if (png_ptr->transformations & PNG_INVERT_ALPHA)\r
+        {\r
+#ifdef PNG_READ_EXPAND_SUPPORTED\r
+           if (!(png_ptr->transformations & PNG_EXPAND_tRNS))\r
+#endif\r
+           {\r
+           /* Invert the alpha channel (in tRNS) unless the pixels are\r
+            * going to be expanded, in which case leave it for later\r
+            */\r
+              int i, istop;\r
+              istop=(int)png_ptr->num_trans;\r
+              for (i=0; i<istop; i++)\r
+                 png_ptr->trans_alpha[i] = (png_byte)(255 - png_ptr->trans_alpha[i]);\r
+           }\r
+        }\r
+#endif\r
+\r
+      }\r
+   }\r
+#endif\r
+\r
+#if defined(PNG_READ_BACKGROUND_SUPPORTED) && defined(PNG_READ_GAMMA_SUPPORTED)\r
+   png_ptr->background_1 = png_ptr->background;\r
+#endif\r
+#if defined(PNG_READ_GAMMA_SUPPORTED) && defined(PNG_FLOATING_POINT_SUPPORTED)\r
+\r
+   if ((color_type == PNG_COLOR_TYPE_PALETTE && png_ptr->num_trans != 0)\r
+       && (fabs(png_ptr->screen_gamma * png_ptr->gamma - 1.0)\r
+         < PNG_GAMMA_THRESHOLD))\r
+   {\r
+    int i, k;\r
+    k=0;\r
+    for (i=0; i<png_ptr->num_trans; i++)\r
+    {\r
+      if (png_ptr->trans_alpha[i] != 0 && png_ptr->trans_alpha[i] != 0xff)\r
+        k=1; /* Partial transparency is present */\r
+    }\r
+    if (k == 0)\r
+      png_ptr->transformations &= ~PNG_GAMMA;\r
+   }\r
+\r
+   if ((png_ptr->transformations & (PNG_GAMMA | PNG_RGB_TO_GRAY)) &&\r
+        png_ptr->gamma != 0.0)\r
+   {\r
+      png_build_gamma_table(png_ptr, png_ptr->bit_depth);\r
+\r
+#ifdef PNG_READ_BACKGROUND_SUPPORTED\r
+      if (png_ptr->transformations & PNG_BACKGROUND)\r
+      {\r
+         if (color_type == PNG_COLOR_TYPE_PALETTE)\r
+         {\r
+           /* Could skip if no transparency */\r
+            png_color back, back_1;\r
+            png_colorp palette = png_ptr->palette;\r
+            int num_palette = png_ptr->num_palette;\r
+            int i;\r
+            if (png_ptr->background_gamma_type == PNG_BACKGROUND_GAMMA_FILE)\r
+            {\r
+               back.red = png_ptr->gamma_table[png_ptr->background.red];\r
+               back.green = png_ptr->gamma_table[png_ptr->background.green];\r
+               back.blue = png_ptr->gamma_table[png_ptr->background.blue];\r
+\r
+               back_1.red = png_ptr->gamma_to_1[png_ptr->background.red];\r
+               back_1.green = png_ptr->gamma_to_1[png_ptr->background.green];\r
+               back_1.blue = png_ptr->gamma_to_1[png_ptr->background.blue];\r
+            }\r
+            else\r
+            {\r
+               double g, gs;\r
+\r
+               switch (png_ptr->background_gamma_type)\r
+               {\r
+                  case PNG_BACKGROUND_GAMMA_SCREEN:\r
+                     g = (png_ptr->screen_gamma);\r
+                     gs = 1.0;\r
+                     break;\r
+\r
+                  case PNG_BACKGROUND_GAMMA_FILE:\r
+                     g = 1.0 / (png_ptr->gamma);\r
+                     gs = 1.0 / (png_ptr->gamma * png_ptr->screen_gamma);\r
+                     break;\r
+\r
+                  case PNG_BACKGROUND_GAMMA_UNIQUE:\r
+                     g = 1.0 / (png_ptr->background_gamma);\r
+                     gs = 1.0 / (png_ptr->background_gamma *\r
+                                 png_ptr->screen_gamma);\r
+                     break;\r
+                  default:\r
+                     g = 1.0;    /* back_1 */\r
+                     gs = 1.0;   /* back */\r
+               }\r
+\r
+               if ( fabs(gs - 1.0) < PNG_GAMMA_THRESHOLD)\r
+               {\r
+                  back.red   = (png_byte)png_ptr->background.red;\r
+                  back.green = (png_byte)png_ptr->background.green;\r
+                  back.blue  = (png_byte)png_ptr->background.blue;\r
+               }\r
+               else\r
+               {\r
+                  back.red = (png_byte)(pow(\r
+                     (double)png_ptr->background.red/255.0, gs) * 255.0 + .5);\r
+                  back.green = (png_byte)(pow(\r
+                     (double)png_ptr->background.green/255.0, gs) * 255.0\r
+                         + .5);\r
+                  back.blue = (png_byte)(pow(\r
+                     (double)png_ptr->background.blue/255.0, gs) * 255.0 + .5);\r
+               }\r
+\r
+               back_1.red = (png_byte)(pow(\r
+                  (double)png_ptr->background.red/255.0, g) * 255.0 + .5);\r
+               back_1.green = (png_byte)(pow(\r
+                  (double)png_ptr->background.green/255.0, g) * 255.0 + .5);\r
+               back_1.blue = (png_byte)(pow(\r
+                  (double)png_ptr->background.blue/255.0, g) * 255.0 + .5);\r
+            }\r
+            for (i = 0; i < num_palette; i++)\r
+            {\r
+               if (i < (int)png_ptr->num_trans && png_ptr->trans_alpha[i] != 0xff)\r
+               {\r
+                  if (png_ptr->trans_alpha[i] == 0)\r
+                  {\r
+                     palette[i] = back;\r
+                  }\r
+                  else /* if (png_ptr->trans_alpha[i] != 0xff) */\r
+                  {\r
+                     png_byte v, w;\r
+\r
+                     v = png_ptr->gamma_to_1[palette[i].red];\r
+                     png_composite(w, v, png_ptr->trans_alpha[i], back_1.red);\r
+                     palette[i].red = png_ptr->gamma_from_1[w];\r
+\r
+                     v = png_ptr->gamma_to_1[palette[i].green];\r
+                     png_composite(w, v, png_ptr->trans_alpha[i], back_1.green);\r
+                     palette[i].green = png_ptr->gamma_from_1[w];\r
+\r
+                     v = png_ptr->gamma_to_1[palette[i].blue];\r
+                     png_composite(w, v, png_ptr->trans_alpha[i], back_1.blue);\r
+                     palette[i].blue = png_ptr->gamma_from_1[w];\r
+                  }\r
+               }\r
+               else\r
+               {\r
+                  palette[i].red = png_ptr->gamma_table[palette[i].red];\r
+                  palette[i].green = png_ptr->gamma_table[palette[i].green];\r
+                  palette[i].blue = png_ptr->gamma_table[palette[i].blue];\r
+               }\r
+            }\r
+            /* Prevent the transformations being done again, and make sure\r
+             * that the now spurious alpha channel is stripped - the code\r
+             * has just reduced background composition and gamma correction\r
+             * to a simple alpha channel strip.\r
+             */\r
+            png_ptr->transformations &= ~PNG_BACKGROUND;\r
+            png_ptr->transformations &= ~PNG_GAMMA;\r
+            png_ptr->transformations |= PNG_STRIP_ALPHA;\r
+         }\r
+         /* if (png_ptr->background_gamma_type!=PNG_BACKGROUND_GAMMA_UNKNOWN) */\r
+         else\r
+         /* color_type != PNG_COLOR_TYPE_PALETTE */\r
+         {\r
+            double m = (double)(((png_uint_32)1 << png_ptr->bit_depth) - 1);\r
+            double g = 1.0;\r
+            double gs = 1.0;\r
+\r
+            switch (png_ptr->background_gamma_type)\r
+            {\r
+               case PNG_BACKGROUND_GAMMA_SCREEN:\r
+                  g = (png_ptr->screen_gamma);\r
+                  gs = 1.0;\r
+                  break;\r
+\r
+               case PNG_BACKGROUND_GAMMA_FILE:\r
+                  g = 1.0 / (png_ptr->gamma);\r
+                  gs = 1.0 / (png_ptr->gamma * png_ptr->screen_gamma);\r
+                  break;\r
+\r
+               case PNG_BACKGROUND_GAMMA_UNIQUE:\r
+                  g = 1.0 / (png_ptr->background_gamma);\r
+                  gs = 1.0 / (png_ptr->background_gamma *\r
+                     png_ptr->screen_gamma);\r
+                  break;\r
+            }\r
+\r
+            png_ptr->background_1.gray = (png_uint_16)(pow(\r
+               (double)png_ptr->background.gray / m, g) * m + .5);\r
+            png_ptr->background.gray = (png_uint_16)(pow(\r
+               (double)png_ptr->background.gray / m, gs) * m + .5);\r
+\r
+            if ((png_ptr->background.red != png_ptr->background.green) ||\r
+                (png_ptr->background.red != png_ptr->background.blue) ||\r
+                (png_ptr->background.red != png_ptr->background.gray))\r
+            {\r
+               /* RGB or RGBA with color background */\r
+               png_ptr->background_1.red = (png_uint_16)(pow(\r
+                  (double)png_ptr->background.red / m, g) * m + .5);\r
+               png_ptr->background_1.green = (png_uint_16)(pow(\r
+                  (double)png_ptr->background.green / m, g) * m + .5);\r
+               png_ptr->background_1.blue = (png_uint_16)(pow(\r
+                  (double)png_ptr->background.blue / m, g) * m + .5);\r
+               png_ptr->background.red = (png_uint_16)(pow(\r
+                  (double)png_ptr->background.red / m, gs) * m + .5);\r
+               png_ptr->background.green = (png_uint_16)(pow(\r
+                  (double)png_ptr->background.green / m, gs) * m + .5);\r
+               png_ptr->background.blue = (png_uint_16)(pow(\r
+                  (double)png_ptr->background.blue / m, gs) * m + .5);\r
+            }\r
+            else\r
+            {\r
+               /* GRAY, GRAY ALPHA, RGB, or RGBA with gray background */\r
+               png_ptr->background_1.red = png_ptr->background_1.green\r
+                 = png_ptr->background_1.blue = png_ptr->background_1.gray;\r
+               png_ptr->background.red = png_ptr->background.green\r
+                 = png_ptr->background.blue = png_ptr->background.gray;\r
+            }\r
+         }\r
+      }\r
+      else\r
+      /* Transformation does not include PNG_BACKGROUND */\r
+#endif /* PNG_READ_BACKGROUND_SUPPORTED */\r
+      if (color_type == PNG_COLOR_TYPE_PALETTE)\r
+      {\r
+         png_colorp palette = png_ptr->palette;\r
+         int num_palette = png_ptr->num_palette;\r
+         int i;\r
+\r
+         for (i = 0; i < num_palette; i++)\r
+         {\r
+            palette[i].red = png_ptr->gamma_table[palette[i].red];\r
+            palette[i].green = png_ptr->gamma_table[palette[i].green];\r
+            palette[i].blue = png_ptr->gamma_table[palette[i].blue];\r
+         }\r
+\r
+         /* Done the gamma correction. */\r
+         png_ptr->transformations &= ~PNG_GAMMA;\r
+      }\r
+   }\r
+#ifdef PNG_READ_BACKGROUND_SUPPORTED\r
+   else\r
+#endif\r
+#endif /* PNG_READ_GAMMA_SUPPORTED && PNG_FLOATING_POINT_SUPPORTED */\r
+#ifdef PNG_READ_BACKGROUND_SUPPORTED\r
+   /* No GAMMA transformation */\r
+   if ((png_ptr->transformations & PNG_BACKGROUND) &&\r
+       (color_type == PNG_COLOR_TYPE_PALETTE))\r
+   {\r
+      int i;\r
+      int istop = (int)png_ptr->num_trans;\r
+      png_color back;\r
+      png_colorp palette = png_ptr->palette;\r
+\r
+      back.red   = (png_byte)png_ptr->background.red;\r
+      back.green = (png_byte)png_ptr->background.green;\r
+      back.blue  = (png_byte)png_ptr->background.blue;\r
+\r
+      for (i = 0; i < istop; i++)\r
+      {\r
+         if (png_ptr->trans_alpha[i] == 0)\r
+         {\r
+            palette[i] = back;\r
+         }\r
+         else if (png_ptr->trans_alpha[i] != 0xff)\r
+         {\r
+            /* The png_composite() macro is defined in png.h */\r
+            png_composite(palette[i].red, palette[i].red,\r
+               png_ptr->trans_alpha[i], back.red);\r
+            png_composite(palette[i].green, palette[i].green,\r
+               png_ptr->trans_alpha[i], back.green);\r
+            png_composite(palette[i].blue, palette[i].blue,\r
+               png_ptr->trans_alpha[i], back.blue);\r
+         }\r
+      }\r
+\r
+      /* Handled alpha, still need to strip the channel. */\r
+      png_ptr->transformations &= ~PNG_BACKGROUND;\r
+      png_ptr->transformations |= PNG_STRIP_ALPHA;\r
+   }\r
+#endif /* PNG_READ_BACKGROUND_SUPPORTED */\r
+\r
+#ifdef PNG_READ_SHIFT_SUPPORTED\r
+   if ((png_ptr->transformations & PNG_SHIFT) &&\r
+      (color_type == PNG_COLOR_TYPE_PALETTE))\r
+   {\r
+      png_uint_16 i;\r
+      png_uint_16 istop = png_ptr->num_palette;\r
+      int sr = 8 - png_ptr->sig_bit.red;\r
+      int sg = 8 - png_ptr->sig_bit.green;\r
+      int sb = 8 - png_ptr->sig_bit.blue;\r
+\r
+      if (sr < 0 || sr > 8)\r
+         sr = 0;\r
+      if (sg < 0 || sg > 8)\r
+         sg = 0;\r
+      if (sb < 0 || sb > 8)\r
+         sb = 0;\r
+      for (i = 0; i < istop; i++)\r
+      {\r
+         png_ptr->palette[i].red >>= sr;\r
+         png_ptr->palette[i].green >>= sg;\r
+         png_ptr->palette[i].blue >>= sb;\r
+      }\r
+   }\r
+#endif  /* PNG_READ_SHIFT_SUPPORTED */\r
+ }\r
+#if !defined(PNG_READ_GAMMA_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED) \\r
+ && !defined(PNG_READ_BACKGROUND_SUPPORTED)\r
+   if (png_ptr)\r
+      return;\r
+#endif\r
+}\r
+\r
+/* Modify the info structure to reflect the transformations.  The\r
+ * info should be updated so a PNG file could be written with it,\r
+ * assuming the transformations result in valid PNG data.\r
+ */\r
+void /* PRIVATE */\r
+png_read_transform_info(png_structp png_ptr, png_infop info_ptr)\r
+{\r
+   png_debug(1, "in png_read_transform_info");\r
+\r
+#ifdef PNG_READ_EXPAND_SUPPORTED\r
+   if (png_ptr->transformations & PNG_EXPAND)\r
+   {\r
+      if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)\r
+      {\r
+         if (png_ptr->num_trans &&\r
+              (png_ptr->transformations & PNG_EXPAND_tRNS))\r
+            info_ptr->color_type = PNG_COLOR_TYPE_RGB_ALPHA;\r
+         else\r
+            info_ptr->color_type = PNG_COLOR_TYPE_RGB;\r
+         info_ptr->bit_depth = 8;\r
+         info_ptr->num_trans = 0;\r
+      }\r
+      else\r
+      {\r
+         if (png_ptr->num_trans)\r
+         {\r
+            if (png_ptr->transformations & PNG_EXPAND_tRNS)\r
+              info_ptr->color_type |= PNG_COLOR_MASK_ALPHA;\r
+         }\r
+         if (info_ptr->bit_depth < 8)\r
+            info_ptr->bit_depth = 8;\r
+         info_ptr->num_trans = 0;\r
+      }\r
+   }\r
+#endif\r
+\r
+#ifdef PNG_READ_BACKGROUND_SUPPORTED\r
+   if (png_ptr->transformations & PNG_BACKGROUND)\r
+   {\r
+      info_ptr->color_type &= ~PNG_COLOR_MASK_ALPHA;\r
+      info_ptr->num_trans = 0;\r
+      info_ptr->background = png_ptr->background;\r
+   }\r
+#endif\r
+\r
+#ifdef PNG_READ_GAMMA_SUPPORTED\r
+   if (png_ptr->transformations & PNG_GAMMA)\r
+   {\r
+#ifdef PNG_FLOATING_POINT_SUPPORTED\r
+      info_ptr->gamma = png_ptr->gamma;\r
+#endif\r
+#ifdef PNG_FIXED_POINT_SUPPORTED\r
+      info_ptr->int_gamma = png_ptr->int_gamma;\r
+#endif\r
+   }\r
+#endif\r
+\r
+#ifdef PNG_READ_16_TO_8_SUPPORTED\r
+   if ((png_ptr->transformations & PNG_16_TO_8) && (info_ptr->bit_depth == 16))\r
+      info_ptr->bit_depth = 8;\r
+#endif\r
+\r
+#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED\r
+   if (png_ptr->transformations & PNG_GRAY_TO_RGB)\r
+      info_ptr->color_type |= PNG_COLOR_MASK_COLOR;\r
+#endif\r
+\r
+#ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED\r
+   if (png_ptr->transformations & PNG_RGB_TO_GRAY)\r
+      info_ptr->color_type &= ~PNG_COLOR_MASK_COLOR;\r
+#endif\r
+\r
+#ifdef PNG_READ_QUANTIZE_SUPPORTED\r
+   if (png_ptr->transformations & PNG_QUANTIZE)\r
+   {\r
+      if (((info_ptr->color_type == PNG_COLOR_TYPE_RGB) ||\r
+          (info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)) &&\r
+          png_ptr->palette_lookup && info_ptr->bit_depth == 8)\r
+      {\r
+         info_ptr->color_type = PNG_COLOR_TYPE_PALETTE;\r
+      }\r
+   }\r
+#endif\r
+\r
+#ifdef PNG_READ_PACK_SUPPORTED\r
+   if ((png_ptr->transformations & PNG_PACK) && (info_ptr->bit_depth < 8))\r
+      info_ptr->bit_depth = 8;\r
+#endif\r
+\r
+   if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)\r
+      info_ptr->channels = 1;\r
+   else if (info_ptr->color_type & PNG_COLOR_MASK_COLOR)\r
+      info_ptr->channels = 3;\r
+   else\r
+      info_ptr->channels = 1;\r
+\r
+#ifdef PNG_READ_STRIP_ALPHA_SUPPORTED\r
+   if (png_ptr->flags & PNG_FLAG_STRIP_ALPHA)\r
+      info_ptr->color_type &= ~PNG_COLOR_MASK_ALPHA;\r
+#endif\r
+\r
+   if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA)\r
+      info_ptr->channels++;\r
+\r
+#ifdef PNG_READ_FILLER_SUPPORTED\r
+   /* STRIP_ALPHA and FILLER allowed:  MASK_ALPHA bit stripped above */\r
+   if ((png_ptr->transformations & PNG_FILLER) &&\r
+       ((info_ptr->color_type == PNG_COLOR_TYPE_RGB) ||\r
+       (info_ptr->color_type == PNG_COLOR_TYPE_GRAY)))\r
+   {\r
+      info_ptr->channels++;\r
+      /* If adding a true alpha channel not just filler */\r
+      if (png_ptr->transformations & PNG_ADD_ALPHA)\r
+        info_ptr->color_type |= PNG_COLOR_MASK_ALPHA;\r
+   }\r
+#endif\r
+\r
+#if defined(PNG_USER_TRANSFORM_PTR_SUPPORTED) && \\r
+defined(PNG_READ_USER_TRANSFORM_SUPPORTED)\r
+   if (png_ptr->transformations & PNG_USER_TRANSFORM)\r
+     {\r
+       if (info_ptr->bit_depth < png_ptr->user_transform_depth)\r
+         info_ptr->bit_depth = png_ptr->user_transform_depth;\r
+       if (info_ptr->channels < png_ptr->user_transform_channels)\r
+         info_ptr->channels = png_ptr->user_transform_channels;\r
+     }\r
+#endif\r
+\r
+   info_ptr->pixel_depth = (png_byte)(info_ptr->channels *\r
+      info_ptr->bit_depth);\r
+\r
+   info_ptr->rowbytes = PNG_ROWBYTES(info_ptr->pixel_depth, info_ptr->width);\r
+\r
+#ifndef PNG_READ_EXPAND_SUPPORTED\r
+   if (png_ptr)\r
+      return;\r
+#endif\r
+}\r
+\r
+/* Transform the row.  The order of transformations is significant,\r
+ * and is very touchy.  If you add a transformation, take care to\r
+ * decide how it fits in with the other transformations here.\r
+ */\r
+void /* PRIVATE */\r
+png_do_read_transformations(png_structp png_ptr)\r
+{\r
+   png_debug(1, "in png_do_read_transformations");\r
+\r
+   if (png_ptr->row_buf == NULL)\r
+   {\r
+#ifdef PNG_STDIO_SUPPORTED\r
+      char msg[50];\r
+\r
+      png_snprintf2(msg, 50,\r
+         "NULL row buffer for row %ld, pass %d", (long)png_ptr->row_number,\r
+         png_ptr->pass);\r
+      png_error(png_ptr, msg);\r
+#else\r
+      png_error(png_ptr, "NULL row buffer");\r
+#endif\r
+   }\r
+#ifdef PNG_WARN_UNINITIALIZED_ROW\r
+   if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))\r
+      /* Application has failed to call either png_read_start_image()\r
+       * or png_read_update_info() after setting transforms that expand\r
+       * pixels.  This check added to libpng-1.2.19\r
+       */\r
+#if (PNG_WARN_UNINITIALIZED_ROW==1)\r
+      png_error(png_ptr, "Uninitialized row");\r
+#else\r
+      png_warning(png_ptr, "Uninitialized row");\r
+#endif\r
+#endif\r
+\r
+#ifdef PNG_READ_EXPAND_SUPPORTED\r
+   if (png_ptr->transformations & PNG_EXPAND)\r
+   {\r
+      if (png_ptr->row_info.color_type == PNG_COLOR_TYPE_PALETTE)\r
+      {\r
+         png_do_expand_palette(&(png_ptr->row_info), png_ptr->row_buf + 1,\r
+            png_ptr->palette, png_ptr->trans_alpha, png_ptr->num_trans);\r
+      }\r
+      else\r
+      {\r
+         if (png_ptr->num_trans &&\r
+             (png_ptr->transformations & PNG_EXPAND_tRNS))\r
+            png_do_expand(&(png_ptr->row_info), png_ptr->row_buf + 1,\r
+               &(png_ptr->trans_color));\r
+         else\r
+            png_do_expand(&(png_ptr->row_info), png_ptr->row_buf + 1,\r
+               NULL);\r
+      }\r
+   }\r
+#endif\r
+\r
+#ifdef PNG_READ_STRIP_ALPHA_SUPPORTED\r
+   if (png_ptr->flags & PNG_FLAG_STRIP_ALPHA)\r
+      png_do_strip_filler(&(png_ptr->row_info), png_ptr->row_buf + 1,\r
+         PNG_FLAG_FILLER_AFTER | (png_ptr->flags & PNG_FLAG_STRIP_ALPHA));\r
+#endif\r
+\r
+#ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED\r
+   if (png_ptr->transformations & PNG_RGB_TO_GRAY)\r
+   {\r
+      int rgb_error =\r
+         png_do_rgb_to_gray(png_ptr, &(png_ptr->row_info),\r
+             png_ptr->row_buf + 1);\r
+      if (rgb_error)\r
+      {\r
+         png_ptr->rgb_to_gray_status=1;\r
+         if ((png_ptr->transformations & PNG_RGB_TO_GRAY) ==\r
+             PNG_RGB_TO_GRAY_WARN)\r
+            png_warning(png_ptr, "png_do_rgb_to_gray found nongray pixel");\r
+         if ((png_ptr->transformations & PNG_RGB_TO_GRAY) ==\r
+             PNG_RGB_TO_GRAY_ERR)\r
+            png_error(png_ptr, "png_do_rgb_to_gray found nongray pixel");\r
+      }\r
+   }\r
+#endif\r
+\r
+/* From Andreas Dilger e-mail to png-implement, 26 March 1998:\r
+ *\r
+ *   In most cases, the "simple transparency" should be done prior to doing\r
+ *   gray-to-RGB, or you will have to test 3x as many bytes to check if a\r
+ *   pixel is transparent.  You would also need to make sure that the\r
+ *   transparency information is upgraded to RGB.\r
+ *\r
+ *   To summarize, the current flow is:\r
+ *   - Gray + simple transparency -> compare 1 or 2 gray bytes and composite\r
+ *                                   with background "in place" if transparent,\r
+ *                                   convert to RGB if necessary\r
+ *   - Gray + alpha -> composite with gray background and remove alpha bytes,\r
+ *                                   convert to RGB if necessary\r
+ *\r
+ *   To support RGB backgrounds for gray images we need:\r
+ *   - Gray + simple transparency -> convert to RGB + simple transparency,\r
+ *                                   compare 3 or 6 bytes and composite with\r
+ *                                   background "in place" if transparent\r
+ *                                   (3x compare/pixel compared to doing\r
+ *                                   composite with gray bkgrnd)\r
+ *   - Gray + alpha -> convert to RGB + alpha, composite with background and\r
+ *                                   remove alpha bytes (3x float\r
+ *                                   operations/pixel compared with composite\r
+ *                                   on gray background)\r
+ *\r
+ *  Greg's change will do this.  The reason it wasn't done before is for\r
+ *  performance, as this increases the per-pixel operations.  If we would check\r
+ *  in advance if the background was gray or RGB, and position the gray-to-RGB\r
+ *  transform appropriately, then it would save a lot of work/time.\r
+ */\r
+\r
+#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED\r
+   /* If gray -> RGB, do so now only if background is non-gray; else do later\r
+    * for performance reasons\r
+    */\r
+   if ((png_ptr->transformations & PNG_GRAY_TO_RGB) &&\r
+       !(png_ptr->mode & PNG_BACKGROUND_IS_GRAY))\r
+      png_do_gray_to_rgb(&(png_ptr->row_info), png_ptr->row_buf + 1);\r
+#endif\r
+\r
+#ifdef PNG_READ_BACKGROUND_SUPPORTED\r
+   if ((png_ptr->transformations & PNG_BACKGROUND) &&\r
+      ((png_ptr->num_trans != 0 ) ||\r
+      (png_ptr->color_type & PNG_COLOR_MASK_ALPHA)))\r
+      png_do_background(&(png_ptr->row_info), png_ptr->row_buf + 1,\r
+         &(png_ptr->trans_color), &(png_ptr->background)\r
+#ifdef PNG_READ_GAMMA_SUPPORTED\r
+         , &(png_ptr->background_1),\r
+         png_ptr->gamma_table, png_ptr->gamma_from_1,\r
+         png_ptr->gamma_to_1, png_ptr->gamma_16_table,\r
+         png_ptr->gamma_16_from_1, png_ptr->gamma_16_to_1,\r
+         png_ptr->gamma_shift\r
+#endif\r
+);\r
+#endif\r
+\r
+#ifdef PNG_READ_GAMMA_SUPPORTED\r
+   if ((png_ptr->transformations & PNG_GAMMA) &&\r
+#ifdef PNG_READ_BACKGROUND_SUPPORTED\r
+       !((png_ptr->transformations & PNG_BACKGROUND) &&\r
+       ((png_ptr->num_trans != 0) ||\r
+       (png_ptr->color_type & PNG_COLOR_MASK_ALPHA))) &&\r
+#endif\r
+       (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE))\r
+      png_do_gamma(&(png_ptr->row_info), png_ptr->row_buf + 1,\r
+          png_ptr->gamma_table, png_ptr->gamma_16_table,\r
+          png_ptr->gamma_shift);\r
+#endif\r
+\r
+#ifdef PNG_READ_16_TO_8_SUPPORTED\r
+   if (png_ptr->transformations & PNG_16_TO_8)\r
+      png_do_chop(&(png_ptr->row_info), png_ptr->row_buf + 1);\r
+#endif\r
+\r
+#ifdef PNG_READ_QUANTIZE_SUPPORTED\r
+   if (png_ptr->transformations & PNG_QUANTIZE)\r
+   {\r
+      png_do_quantize((png_row_infop)&(png_ptr->row_info), png_ptr->row_buf + 1,\r
+         png_ptr->palette_lookup, png_ptr->quantize_index);\r
+      if (png_ptr->row_info.rowbytes == (png_uint_32)0)\r
+         png_error(png_ptr, "png_do_quantize returned rowbytes=0");\r
+   }\r
+#endif\r
+\r
+#ifdef PNG_READ_INVERT_SUPPORTED\r
+   if (png_ptr->transformations & PNG_INVERT_MONO)\r
+      png_do_invert(&(png_ptr->row_info), png_ptr->row_buf + 1);\r
+#endif\r
+\r
+#ifdef PNG_READ_SHIFT_SUPPORTED\r
+   if (png_ptr->transformations & PNG_SHIFT)\r
+      png_do_unshift(&(png_ptr->row_info), png_ptr->row_buf + 1,\r
+         &(png_ptr->shift));\r
+#endif\r
+\r
+#ifdef PNG_READ_PACK_SUPPORTED\r
+   if (png_ptr->transformations & PNG_PACK)\r
+      png_do_unpack(&(png_ptr->row_info), png_ptr->row_buf + 1);\r
+#endif\r
+\r
+#ifdef PNG_READ_BGR_SUPPORTED\r
+   if (png_ptr->transformations & PNG_BGR)\r
+      png_do_bgr(&(png_ptr->row_info), png_ptr->row_buf + 1);\r
+#endif\r
+\r
+#ifdef PNG_READ_PACKSWAP_SUPPORTED\r
+   if (png_ptr->transformations & PNG_PACKSWAP)\r
+      png_do_packswap(&(png_ptr->row_info), png_ptr->row_buf + 1);\r
+#endif\r
+\r
+#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED\r
+   /* If gray -> RGB, do so now only if we did not do so above */\r
+   if ((png_ptr->transformations & PNG_GRAY_TO_RGB) &&\r
+       (png_ptr->mode & PNG_BACKGROUND_IS_GRAY))\r
+      png_do_gray_to_rgb(&(png_ptr->row_info), png_ptr->row_buf + 1);\r
+#endif\r
+\r
+#ifdef PNG_READ_FILLER_SUPPORTED\r
+   if (png_ptr->transformations & PNG_FILLER)\r
+      png_do_read_filler(&(png_ptr->row_info), png_ptr->row_buf + 1,\r
+         (png_uint_32)png_ptr->filler, png_ptr->flags);\r
+#endif\r
+\r
+#ifdef PNG_READ_INVERT_ALPHA_SUPPORTED\r
+   if (png_ptr->transformations & PNG_INVERT_ALPHA)\r
+      png_do_read_invert_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1);\r
+#endif\r
+\r
+#ifdef PNG_READ_SWAP_ALPHA_SUPPORTED\r
+   if (png_ptr->transformations & PNG_SWAP_ALPHA)\r
+      png_do_read_swap_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1);\r
+#endif\r
+\r
+#ifdef PNG_READ_SWAP_SUPPORTED\r
+   if (png_ptr->transformations & PNG_SWAP_BYTES)\r
+      png_do_swap(&(png_ptr->row_info), png_ptr->row_buf + 1);\r
+#endif\r
+\r
+#ifdef PNG_READ_USER_TRANSFORM_SUPPORTED\r
+   if (png_ptr->transformations & PNG_USER_TRANSFORM)\r
+    {\r
+      if (png_ptr->read_user_transform_fn != NULL)\r
+         (*(png_ptr->read_user_transform_fn)) /* User read transform function */\r
+            (png_ptr,                    /* png_ptr */\r
+               &(png_ptr->row_info),     /* row_info: */\r
+               /*  png_uint_32 width;       width of row */\r
+               /*  png_uint_32 rowbytes;    number of bytes in row */\r
+               /*  png_byte color_type;     color type of pixels */\r
+               /*  png_byte bit_depth;      bit depth of samples */\r
+               /*  png_byte channels;       number of channels (1-4) */\r
+               /*  png_byte pixel_depth;    bits per pixel (depth*channels) */\r
+               png_ptr->row_buf + 1);    /* start of pixel data for row */\r
+#ifdef PNG_USER_TRANSFORM_PTR_SUPPORTED\r
+      if (png_ptr->user_transform_depth)\r
+         png_ptr->row_info.bit_depth = png_ptr->user_transform_depth;\r
+      if (png_ptr->user_transform_channels)\r
+         png_ptr->row_info.channels = png_ptr->user_transform_channels;\r
+#endif\r
+      png_ptr->row_info.pixel_depth = (png_byte)(png_ptr->row_info.bit_depth *\r
+         png_ptr->row_info.channels);\r
+      png_ptr->row_info.rowbytes = PNG_ROWBYTES(png_ptr->row_info.pixel_depth,\r
+         png_ptr->row_info.width);\r
+   }\r
+#endif\r
+\r
+}\r
+\r
+#ifdef PNG_READ_PACK_SUPPORTED\r
+/* Unpack pixels of 1, 2, or 4 bits per pixel into 1 byte per pixel,\r
+ * without changing the actual values.  Thus, if you had a row with\r
+ * a bit depth of 1, you would end up with bytes that only contained\r
+ * the numbers 0 or 1.  If you would rather they contain 0 and 255, use\r
+ * png_do_shift() after this.\r
+ */\r
+void /* PRIVATE */\r
+png_do_unpack(png_row_infop row_info, png_bytep row)\r
+{\r
+   png_debug(1, "in png_do_unpack");\r
+\r
+   if (row_info->bit_depth < 8)\r
+   {\r
+      png_uint_32 i;\r
+      png_uint_32 row_width=row_info->width;\r
+\r
+      switch (row_info->bit_depth)\r
+      {\r
+         case 1:\r
+         {\r
+            png_bytep sp = row + (png_size_t)((row_width - 1) >> 3);\r
+            png_bytep dp = row + (png_size_t)row_width - 1;\r
+            png_uint_32 shift = 7 - (int)((row_width + 7) & 0x07);\r
+            for (i = 0; i < row_width; i++)\r
+            {\r
+               *dp = (png_byte)((*sp >> shift) & 0x01);\r
+               if (shift == 7)\r
+               {\r
+                  shift = 0;\r
+                  sp--;\r
+               }\r
+               else\r
+                  shift++;\r
+\r
+               dp--;\r
+            }\r
+            break;\r
+         }\r
+\r
+         case 2:\r
+         {\r
+\r
+            png_bytep sp = row + (png_size_t)((row_width - 1) >> 2);\r
+            png_bytep dp = row + (png_size_t)row_width - 1;\r
+            png_uint_32 shift = (int)((3 - ((row_width + 3) & 0x03)) << 1);\r
+            for (i = 0; i < row_width; i++)\r
+            {\r
+               *dp = (png_byte)((*sp >> shift) & 0x03);\r
+               if (shift == 6)\r
+               {\r
+                  shift = 0;\r
+                  sp--;\r
+               }\r
+               else\r
+                  shift += 2;\r
+\r
+               dp--;\r
+            }\r
+            break;\r
+         }\r
+\r
+         case 4:\r
+         {\r
+            png_bytep sp = row + (png_size_t)((row_width - 1) >> 1);\r
+            png_bytep dp = row + (png_size_t)row_width - 1;\r
+            png_uint_32 shift = (int)((1 - ((row_width + 1) & 0x01)) << 2);\r
+            for (i = 0; i < row_width; i++)\r
+            {\r
+               *dp = (png_byte)((*sp >> shift) & 0x0f);\r
+               if (shift == 4)\r
+               {\r
+                  shift = 0;\r
+                  sp--;\r
+               }\r
+               else\r
+                  shift = 4;\r
+\r
+               dp--;\r
+            }\r
+            break;\r
+         }\r
+      }\r
+      row_info->bit_depth = 8;\r
+      row_info->pixel_depth = (png_byte)(8 * row_info->channels);\r
+      row_info->rowbytes = row_width * row_info->channels;\r
+   }\r
+}\r
+#endif\r
+\r
+#ifdef PNG_READ_SHIFT_SUPPORTED\r
+/* Reverse the effects of png_do_shift.  This routine merely shifts the\r
+ * pixels back to their significant bits values.  Thus, if you have\r
+ * a row of bit depth 8, but only 5 are significant, this will shift\r
+ * the values back to 0 through 31.\r
+ */\r
+void /* PRIVATE */\r
+png_do_unshift(png_row_infop row_info, png_bytep row, png_color_8p sig_bits)\r
+{\r
+   png_debug(1, "in png_do_unshift");\r
+\r
+   if (\r
+       row_info->color_type != PNG_COLOR_TYPE_PALETTE)\r
+   {\r
+      int shift[4];\r
+      int channels = 0;\r
+      int c;\r
+      png_uint_16 value = 0;\r
+      png_uint_32 row_width = row_info->width;\r
+\r
+      if (row_info->color_type & PNG_COLOR_MASK_COLOR)\r
+      {\r
+         shift[channels++] = row_info->bit_depth - sig_bits->red;\r
+         shift[channels++] = row_info->bit_depth - sig_bits->green;\r
+         shift[channels++] = row_info->bit_depth - sig_bits->blue;\r
+      }\r
+      else\r
+      {\r
+         shift[channels++] = row_info->bit_depth - sig_bits->gray;\r
+      }\r
+      if (row_info->color_type & PNG_COLOR_MASK_ALPHA)\r
+      {\r
+         shift[channels++] = row_info->bit_depth - sig_bits->alpha;\r
+      }\r
+\r
+      for (c = 0; c < channels; c++)\r
+      {\r
+         if (shift[c] <= 0)\r
+            shift[c] = 0;\r
+         else\r
+            value = 1;\r
+      }\r
+\r
+      if (!value)\r
+         return;\r
+\r
+      switch (row_info->bit_depth)\r
+      {\r
+         case 2:\r
+         {\r
+            png_bytep bp;\r
+            png_uint_32 i;\r
+            png_uint_32 istop = row_info->rowbytes;\r
+\r
+            for (bp = row, i = 0; i < istop; i++)\r
+            {\r
+               *bp >>= 1;\r
+               *bp++ &= 0x55;\r
+            }\r
+            break;\r
+         }\r
+\r
+         case 4:\r
+         {\r
+            png_bytep bp = row;\r
+            png_uint_32 i;\r
+            png_uint_32 istop = row_info->rowbytes;\r
+            png_byte mask = (png_byte)((((int)0xf0 >> shift[0]) & (int)0xf0) |\r
+               (png_byte)((int)0xf >> shift[0]));\r
+\r
+            for (i = 0; i < istop; i++)\r
+            {\r
+               *bp >>= shift[0];\r
+               *bp++ &= mask;\r
+            }\r
+            break;\r
+         }\r
+\r
+         case 8:\r
+         {\r
+            png_bytep bp = row;\r
+            png_uint_32 i;\r
+            png_uint_32 istop = row_width * channels;\r
+\r
+            for (i = 0; i < istop; i++)\r
+            {\r
+               *bp++ >>= shift[i%channels];\r
+            }\r
+            break;\r
+         }\r
+\r
+         case 16:\r
+         {\r
+            png_bytep bp = row;\r
+            png_uint_32 i;\r
+            png_uint_32 istop = channels * row_width;\r
+\r
+            for (i = 0; i < istop; i++)\r
+            {\r
+               value = (png_uint_16)((*bp << 8) + *(bp + 1));\r
+               value >>= shift[i%channels];\r
+               *bp++ = (png_byte)(value >> 8);\r
+               *bp++ = (png_byte)(value & 0xff);\r
+            }\r
+            break;\r
+         }\r
+      }\r
+   }\r
+}\r
+#endif\r
+\r
+#ifdef PNG_READ_16_TO_8_SUPPORTED\r
+/* Chop rows of bit depth 16 down to 8 */\r
+void /* PRIVATE */\r
+png_do_chop(png_row_infop row_info, png_bytep row)\r
+{\r
+   png_debug(1, "in png_do_chop");\r
+\r
+   if (row_info->bit_depth == 16)\r
+   {\r
+      png_bytep sp = row;\r
+      png_bytep dp = row;\r
+      png_uint_32 i;\r
+      png_uint_32 istop = row_info->width * row_info->channels;\r
+\r
+      for (i = 0; i<istop; i++, sp += 2, dp++)\r
+      {\r
+#ifdef PNG_READ_16_TO_8_ACCURATE_SCALE_SUPPORTED\r
+      /* This does a more accurate scaling of the 16-bit color\r
+       * value, rather than a simple low-byte truncation.\r
+       *\r
+       * What the ideal calculation should be:\r
+       *   *dp = (((((png_uint_32)(*sp) << 8) |\r
+       *          (png_uint_32)(*(sp + 1))) * 255 + 127)\r
+       *          / (png_uint_32)65535L;\r
+       *\r
+       * GRR: no, I think this is what it really should be:\r
+       *   *dp = (((((png_uint_32)(*sp) << 8) |\r
+       *           (png_uint_32)(*(sp + 1))) + 128L)\r
+       *           / (png_uint_32)257L;\r
+       *\r
+       * GRR: here's the exact calculation with shifts:\r
+       *   temp = (((png_uint_32)(*sp) << 8) |\r
+       *           (png_uint_32)(*(sp + 1))) + 128L;\r
+       *   *dp = (temp - (temp >> 8)) >> 8;\r
+       *\r
+       * Approximate calculation with shift/add instead of multiply/divide:\r
+       *   *dp = ((((png_uint_32)(*sp) << 8) |\r
+       *          (png_uint_32)((int)(*(sp + 1)) - *sp)) + 128) >> 8;\r
+       *\r
+       * What we actually do to avoid extra shifting and conversion:\r
+       */\r
+\r
+         *dp = *sp + ((((int)(*(sp + 1)) - *sp) > 128) ? 1 : 0);\r
+#else\r
+       /* Simply discard the low order byte */\r
+         *dp = *sp;\r
+#endif\r
+      }\r
+      row_info->bit_depth = 8;\r
+      row_info->pixel_depth = (png_byte)(8 * row_info->channels);\r
+      row_info->rowbytes = row_info->width * row_info->channels;\r
+   }\r
+}\r
+#endif\r
+\r
+#ifdef PNG_READ_SWAP_ALPHA_SUPPORTED\r
+void /* PRIVATE */\r
+png_do_read_swap_alpha(png_row_infop row_info, png_bytep row)\r
+{\r
+   png_debug(1, "in png_do_read_swap_alpha");\r
+\r
+   {\r
+      png_uint_32 row_width = row_info->width;\r
+      if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)\r
+      {\r
+         /* This converts from RGBA to ARGB */\r
+         if (row_info->bit_depth == 8)\r
+         {\r
+            png_bytep sp = row + row_info->rowbytes;\r
+            png_bytep dp = sp;\r
+            png_byte save;\r
+            png_uint_32 i;\r
+\r
+            for (i = 0; i < row_width; i++)\r
+            {\r
+               save = *(--sp);\r
+               *(--dp) = *(--sp);\r
+               *(--dp) = *(--sp);\r
+               *(--dp) = *(--sp);\r
+               *(--dp) = save;\r
+            }\r
+         }\r
+         /* This converts from RRGGBBAA to AARRGGBB */\r
+         else\r
+         {\r
+            png_bytep sp = row + row_info->rowbytes;\r
+            png_bytep dp = sp;\r
+            png_byte save[2];\r
+            png_uint_32 i;\r
+\r
+            for (i = 0; i < row_width; i++)\r
+            {\r
+               save[0] = *(--sp);\r
+               save[1] = *(--sp);\r
+               *(--dp) = *(--sp);\r
+               *(--dp) = *(--sp);\r
+               *(--dp) = *(--sp);\r
+               *(--dp) = *(--sp);\r
+               *(--dp) = *(--sp);\r
+               *(--dp) = *(--sp);\r
+               *(--dp) = save[0];\r
+               *(--dp) = save[1];\r
+            }\r
+         }\r
+      }\r
+      else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)\r
+      {\r
+         /* This converts from GA to AG */\r
+         if (row_info->bit_depth == 8)\r
+         {\r
+            png_bytep sp = row + row_info->rowbytes;\r
+            png_bytep dp = sp;\r
+            png_byte save;\r
+            png_uint_32 i;\r
+\r
+            for (i = 0; i < row_width; i++)\r
+            {\r
+               save = *(--sp);\r
+               *(--dp) = *(--sp);\r
+               *(--dp) = save;\r
+            }\r
+         }\r
+         /* This converts from GGAA to AAGG */\r
+         else\r
+         {\r
+            png_bytep sp = row + row_info->rowbytes;\r
+            png_bytep dp = sp;\r
+            png_byte save[2];\r
+            png_uint_32 i;\r
+\r
+            for (i = 0; i < row_width; i++)\r
+            {\r
+               save[0] = *(--sp);\r
+               save[1] = *(--sp);\r
+               *(--dp) = *(--sp);\r
+               *(--dp) = *(--sp);\r
+               *(--dp) = save[0];\r
+               *(--dp) = save[1];\r
+            }\r
+         }\r
+      }\r
+   }\r
+}\r
+#endif\r
+\r
+#ifdef PNG_READ_INVERT_ALPHA_SUPPORTED\r
+void /* PRIVATE */\r
+png_do_read_invert_alpha(png_row_infop row_info, png_bytep row)\r
+{\r
+   png_debug(1, "in png_do_read_invert_alpha");\r
+\r
+   {\r
+      png_uint_32 row_width = row_info->width;\r
+      if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)\r
+      {\r
+         /* This inverts the alpha channel in RGBA */\r
+         if (row_info->bit_depth == 8)\r
+         {\r
+            png_bytep sp = row + row_info->rowbytes;\r
+            png_bytep dp = sp;\r
+            png_uint_32 i;\r
+\r
+            for (i = 0; i < row_width; i++)\r
+            {\r
+               *(--dp) = (png_byte)(255 - *(--sp));\r
+\r
+/*             This does nothing:\r
+               *(--dp) = *(--sp);\r
+               *(--dp) = *(--sp);\r
+               *(--dp) = *(--sp);\r
+               We can replace it with:\r
+*/\r
+               sp-=3;\r
+               dp=sp;\r
+            }\r
+         }\r
+         /* This inverts the alpha channel in RRGGBBAA */\r
+         else\r
+         {\r
+            png_bytep sp = row + row_info->rowbytes;\r
+            png_bytep dp = sp;\r
+            png_uint_32 i;\r
+\r
+            for (i = 0; i < row_width; i++)\r
+            {\r
+               *(--dp) = (png_byte)(255 - *(--sp));\r
+               *(--dp) = (png_byte)(255 - *(--sp));\r
+\r
+/*             This does nothing:\r
+               *(--dp) = *(--sp);\r
+               *(--dp) = *(--sp);\r
+               *(--dp) = *(--sp);\r
+               *(--dp) = *(--sp);\r
+               *(--dp) = *(--sp);\r
+               *(--dp) = *(--sp);\r
+               We can replace it with:\r
+*/\r
+               sp-=6;\r
+               dp=sp;\r
+            }\r
+         }\r
+      }\r
+      else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)\r
+      {\r
+         /* This inverts the alpha channel in GA */\r
+         if (row_info->bit_depth == 8)\r
+         {\r
+            png_bytep sp = row + row_info->rowbytes;\r
+            png_bytep dp = sp;\r
+            png_uint_32 i;\r
+\r
+            for (i = 0; i < row_width; i++)\r
+            {\r
+               *(--dp) = (png_byte)(255 - *(--sp));\r
+               *(--dp) = *(--sp);\r
+            }\r
+         }\r
+         /* This inverts the alpha channel in GGAA */\r
+         else\r
+         {\r
+            png_bytep sp  = row + row_info->rowbytes;\r
+            png_bytep dp = sp;\r
+            png_uint_32 i;\r
+\r
+            for (i = 0; i < row_width; i++)\r
+            {\r
+               *(--dp) = (png_byte)(255 - *(--sp));\r
+               *(--dp) = (png_byte)(255 - *(--sp));\r
+/*\r
+               *(--dp) = *(--sp);\r
+               *(--dp) = *(--sp);\r
+*/\r
+               sp-=2;\r
+               dp=sp;\r
+            }\r
+         }\r
+      }\r
+   }\r
+}\r
+#endif\r
+\r
+#ifdef PNG_READ_FILLER_SUPPORTED\r
+/* Add filler channel if we have RGB color */\r
+void /* PRIVATE */\r
+png_do_read_filler(png_row_infop row_info, png_bytep row,\r
+   png_uint_32 filler, png_uint_32 flags)\r
+{\r
+   png_uint_32 i;\r
+   png_uint_32 row_width = row_info->width;\r
+\r
+   png_byte hi_filler = (png_byte)((filler>>8) & 0xff);\r
+   png_byte lo_filler = (png_byte)(filler & 0xff);\r
+\r
+   png_debug(1, "in png_do_read_filler");\r
+\r
+   if (\r
+       row_info->color_type == PNG_COLOR_TYPE_GRAY)\r
+   {\r
+      if (row_info->bit_depth == 8)\r
+      {\r
+         /* This changes the data from G to GX */\r
+         if (flags & PNG_FLAG_FILLER_AFTER)\r
+         {\r
+            png_bytep sp = row + (png_size_t)row_width;\r
+            png_bytep dp =  sp + (png_size_t)row_width;\r
+            for (i = 1; i < row_width; i++)\r
+            {\r
+               *(--dp) = lo_filler;\r
+               *(--dp) = *(--sp);\r
+            }\r
+            *(--dp) = lo_filler;\r
+            row_info->channels = 2;\r
+            row_info->pixel_depth = 16;\r
+            row_info->rowbytes = row_width * 2;\r
+         }\r
+      /* This changes the data from G to XG */\r
+         else\r
+         {\r
+            png_bytep sp = row + (png_size_t)row_width;\r
+            png_bytep dp = sp  + (png_size_t)row_width;\r
+            for (i = 0; i < row_width; i++)\r
+            {\r
+               *(--dp) = *(--sp);\r
+               *(--dp) = lo_filler;\r
+            }\r
+            row_info->channels = 2;\r
+            row_info->pixel_depth = 16;\r
+            row_info->rowbytes = row_width * 2;\r
+         }\r
+      }\r
+      else if (row_info->bit_depth == 16)\r
+      {\r
+         /* This changes the data from GG to GGXX */\r
+         if (flags & PNG_FLAG_FILLER_AFTER)\r
+         {\r
+            png_bytep sp = row + (png_size_t)row_width * 2;\r
+            png_bytep dp = sp  + (png_size_t)row_width * 2;\r
+            for (i = 1; i < row_width; i++)\r
+            {\r
+               *(--dp) = hi_filler;\r
+               *(--dp) = lo_filler;\r
+               *(--dp) = *(--sp);\r
+               *(--dp) = *(--sp);\r
+            }\r
+            *(--dp) = hi_filler;\r
+            *(--dp) = lo_filler;\r
+            row_info->channels = 2;\r
+            row_info->pixel_depth = 32;\r
+            row_info->rowbytes = row_width * 4;\r
+         }\r
+         /* This changes the data from GG to XXGG */\r
+         else\r
+         {\r
+            png_bytep sp = row + (png_size_t)row_width * 2;\r
+            png_bytep dp = sp  + (png_size_t)row_width * 2;\r
+            for (i = 0; i < row_width; i++)\r
+            {\r
+               *(--dp) = *(--sp);\r
+               *(--dp) = *(--sp);\r
+               *(--dp) = hi_filler;\r
+               *(--dp) = lo_filler;\r
+            }\r
+            row_info->channels = 2;\r
+            row_info->pixel_depth = 32;\r
+            row_info->rowbytes = row_width * 4;\r
+         }\r
+      }\r
+   } /* COLOR_TYPE == GRAY */\r
+   else if (row_info->color_type == PNG_COLOR_TYPE_RGB)\r
+   {\r
+      if (row_info->bit_depth == 8)\r
+      {\r
+         /* This changes the data from RGB to RGBX */\r
+         if (flags & PNG_FLAG_FILLER_AFTER)\r
+         {\r
+            png_bytep sp = row + (png_size_t)row_width * 3;\r
+            png_bytep dp = sp  + (png_size_t)row_width;\r
+            for (i = 1; i < row_width; i++)\r
+            {\r
+               *(--dp) = lo_filler;\r
+               *(--dp) = *(--sp);\r
+               *(--dp) = *(--sp);\r
+               *(--dp) = *(--sp);\r
+            }\r
+            *(--dp) = lo_filler;\r
+            row_info->channels = 4;\r
+            row_info->pixel_depth = 32;\r
+            row_info->rowbytes = row_width * 4;\r
+         }\r
+      /* This changes the data from RGB to XRGB */\r
+         else\r
+         {\r
+            png_bytep sp = row + (png_size_t)row_width * 3;\r
+            png_bytep dp = sp + (png_size_t)row_width;\r
+            for (i = 0; i < row_width; i++)\r
+            {\r
+               *(--dp) = *(--sp);\r
+               *(--dp) = *(--sp);\r
+               *(--dp) = *(--sp);\r
+               *(--dp) = lo_filler;\r
+            }\r
+            row_info->channels = 4;\r
+            row_info->pixel_depth = 32;\r
+            row_info->rowbytes = row_width * 4;\r
+         }\r
+      }\r
+      else if (row_info->bit_depth == 16)\r
+      {\r
+         /* This changes the data from RRGGBB to RRGGBBXX */\r
+         if (flags & PNG_FLAG_FILLER_AFTER)\r
+         {\r
+            png_bytep sp = row + (png_size_t)row_width * 6;\r
+            png_bytep dp = sp  + (png_size_t)row_width * 2;\r
+            for (i = 1; i < row_width; i++)\r
+            {\r
+               *(--dp) = hi_filler;\r
+               *(--dp) = lo_filler;\r
+               *(--dp) = *(--sp);\r
+               *(--dp) = *(--sp);\r
+               *(--dp) = *(--sp);\r
+               *(--dp) = *(--sp);\r
+               *(--dp) = *(--sp);\r
+               *(--dp) = *(--sp);\r
+            }\r
+            *(--dp) = hi_filler;\r
+            *(--dp) = lo_filler;\r
+            row_info->channels = 4;\r
+            row_info->pixel_depth = 64;\r
+            row_info->rowbytes = row_width * 8;\r
+         }\r
+         /* This changes the data from RRGGBB to XXRRGGBB */\r
+         else\r
+         {\r
+            png_bytep sp = row + (png_size_t)row_width * 6;\r
+            png_bytep dp = sp  + (png_size_t)row_width * 2;\r
+            for (i = 0; i < row_width; i++)\r
+            {\r
+               *(--dp) = *(--sp);\r
+               *(--dp) = *(--sp);\r
+               *(--dp) = *(--sp);\r
+               *(--dp) = *(--sp);\r
+               *(--dp) = *(--sp);\r
+               *(--dp) = *(--sp);\r
+               *(--dp) = hi_filler;\r
+               *(--dp) = lo_filler;\r
+            }\r
+            row_info->channels = 4;\r
+            row_info->pixel_depth = 64;\r
+            row_info->rowbytes = row_width * 8;\r
+         }\r
+      }\r
+   } /* COLOR_TYPE == RGB */\r
+}\r
+#endif\r
+\r
+#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED\r
+/* Expand grayscale files to RGB, with or without alpha */\r
+void /* PRIVATE */\r
+png_do_gray_to_rgb(png_row_infop row_info, png_bytep row)\r
+{\r
+   png_uint_32 i;\r
+   png_uint_32 row_width = row_info->width;\r
+\r
+   png_debug(1, "in png_do_gray_to_rgb");\r
+\r
+   if (row_info->bit_depth >= 8 &&\r
+      !(row_info->color_type & PNG_COLOR_MASK_COLOR))\r
+   {\r
+      if (row_info->color_type == PNG_COLOR_TYPE_GRAY)\r
+      {\r
+         if (row_info->bit_depth == 8)\r
+         {\r
+            png_bytep sp = row + (png_size_t)row_width - 1;\r
+            png_bytep dp = sp  + (png_size_t)row_width * 2;\r
+            for (i = 0; i < row_width; i++)\r
+            {\r
+               *(dp--) = *sp;\r
+               *(dp--) = *sp;\r
+               *(dp--) = *(sp--);\r
+            }\r
+         }\r
+         else\r
+         {\r
+            png_bytep sp = row + (png_size_t)row_width * 2 - 1;\r
+            png_bytep dp = sp  + (png_size_t)row_width * 4;\r
+            for (i = 0; i < row_width; i++)\r
+            {\r
+               *(dp--) = *sp;\r
+               *(dp--) = *(sp - 1);\r
+               *(dp--) = *sp;\r
+               *(dp--) = *(sp - 1);\r
+               *(dp--) = *(sp--);\r
+               *(dp--) = *(sp--);\r
+            }\r
+         }\r
+      }\r
+      else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)\r
+      {\r
+         if (row_info->bit_depth == 8)\r
+         {\r
+            png_bytep sp = row + (png_size_t)row_width * 2 - 1;\r
+            png_bytep dp = sp  + (png_size_t)row_width * 2;\r
+            for (i = 0; i < row_width; i++)\r
+            {\r
+               *(dp--) = *(sp--);\r
+               *(dp--) = *sp;\r
+               *(dp--) = *sp;\r
+               *(dp--) = *(sp--);\r
+            }\r
+         }\r
+         else\r
+         {\r
+            png_bytep sp = row + (png_size_t)row_width * 4 - 1;\r
+            png_bytep dp = sp  + (png_size_t)row_width * 4;\r
+            for (i = 0; i < row_width; i++)\r
+            {\r
+               *(dp--) = *(sp--);\r
+               *(dp--) = *(sp--);\r
+               *(dp--) = *sp;\r
+               *(dp--) = *(sp - 1);\r
+               *(dp--) = *sp;\r
+               *(dp--) = *(sp - 1);\r
+               *(dp--) = *(sp--);\r
+               *(dp--) = *(sp--);\r
+            }\r
+         }\r
+      }\r
+      row_info->channels += (png_byte)2;\r
+      row_info->color_type |= PNG_COLOR_MASK_COLOR;\r
+      row_info->pixel_depth = (png_byte)(row_info->channels *\r
+         row_info->bit_depth);\r
+      row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);\r
+   }\r
+}\r
+#endif\r
+\r
+#ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED\r
+/* Reduce RGB files to grayscale, with or without alpha\r
+ * using the equation given in Poynton's ColorFAQ at\r
+ * <http://www.inforamp.net/~poynton/>  (THIS LINK IS DEAD June 2008)\r
+ * New link:\r
+ * <http://www.poynton.com/notes/colour_and_gamma/>\r
+ * Charles Poynton poynton at poynton.com\r
+ *\r
+ *     Y = 0.212671 * R + 0.715160 * G + 0.072169 * B\r
+ *\r
+ *  We approximate this with\r
+ *\r
+ *     Y = 0.21268 * R    + 0.7151 * G    + 0.07217 * B\r
+ *\r
+ *  which can be expressed with integers as\r
+ *\r
+ *     Y = (6969 * R + 23434 * G + 2365 * B)/32768\r
+ *\r
+ *  The calculation is to be done in a linear colorspace.\r
+ *\r
+ *  Other integer coefficents can be used via png_set_rgb_to_gray().\r
+ */\r
+int /* PRIVATE */\r
+png_do_rgb_to_gray(png_structp png_ptr, png_row_infop row_info, png_bytep row)\r
+\r
+{\r
+   png_uint_32 i;\r
+\r
+   png_uint_32 row_width = row_info->width;\r
+   int rgb_error = 0;\r
+\r
+   png_debug(1, "in png_do_rgb_to_gray");\r
+\r
+   if (\r
+      (row_info->color_type & PNG_COLOR_MASK_COLOR))\r
+   {\r
+      png_uint_32 rc = png_ptr->rgb_to_gray_red_coeff;\r
+      png_uint_32 gc = png_ptr->rgb_to_gray_green_coeff;\r
+      png_uint_32 bc = png_ptr->rgb_to_gray_blue_coeff;\r
+\r
+      if (row_info->color_type == PNG_COLOR_TYPE_RGB)\r
+      {\r
+         if (row_info->bit_depth == 8)\r
+         {\r
+#if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)\r
+            if (png_ptr->gamma_from_1 != NULL && png_ptr->gamma_to_1 != NULL)\r
+            {\r
+               png_bytep sp = row;\r
+               png_bytep dp = row;\r
+\r
+               for (i = 0; i < row_width; i++)\r
+               {\r
+                  png_byte red   = png_ptr->gamma_to_1[*(sp++)];\r
+                  png_byte green = png_ptr->gamma_to_1[*(sp++)];\r
+                  png_byte blue  = png_ptr->gamma_to_1[*(sp++)];\r
+                  if (red != green || red != blue)\r
+                  {\r
+                     rgb_error |= 1;\r
+                     *(dp++) = png_ptr->gamma_from_1[\r
+                       (rc*red + gc*green + bc*blue)>>15];\r
+                  }\r
+                  else\r
+                     *(dp++) = *(sp - 1);\r
+               }\r
+            }\r
+            else\r
+#endif\r
+            {\r
+               png_bytep sp = row;\r
+               png_bytep dp = row;\r
+               for (i = 0; i < row_width; i++)\r
+               {\r
+                  png_byte red   = *(sp++);\r
+                  png_byte green = *(sp++);\r
+                  png_byte blue  = *(sp++);\r
+                  if (red != green || red != blue)\r
+                  {\r
+                     rgb_error |= 1;\r
+                     *(dp++) = (png_byte)((rc*red + gc*green + bc*blue)>>15);\r
+                  }\r
+                  else\r
+                     *(dp++) = *(sp - 1);\r
+               }\r
+            }\r
+         }\r
+\r
+         else /* RGB bit_depth == 16 */\r
+         {\r
+#if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)\r
+            if (png_ptr->gamma_16_to_1 != NULL &&\r
+                png_ptr->gamma_16_from_1 != NULL)\r
+            {\r
+               png_bytep sp = row;\r
+               png_bytep dp = row;\r
+               for (i = 0; i < row_width; i++)\r
+               {\r
+                  png_uint_16 red, green, blue, w;\r
+\r
+                  red   = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2;\r
+                  green = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2;\r
+                  blue  = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2;\r
+\r
+                  if (red == green && red == blue)\r
+                     w = red;\r
+                  else\r
+                  {\r
+                     png_uint_16 red_1   = png_ptr->gamma_16_to_1[(red&0xff) >>\r
+                                  png_ptr->gamma_shift][red>>8];\r
+                     png_uint_16 green_1 =\r
+                         png_ptr->gamma_16_to_1[(green&0xff) >>\r
+                                  png_ptr->gamma_shift][green>>8];\r
+                     png_uint_16 blue_1  = png_ptr->gamma_16_to_1[(blue&0xff) >>\r
+                                  png_ptr->gamma_shift][blue>>8];\r
+                     png_uint_16 gray16  = (png_uint_16)((rc*red_1 + gc*green_1\r
+                                  + bc*blue_1)>>15);\r
+                     w = png_ptr->gamma_16_from_1[(gray16&0xff) >>\r
+                         png_ptr->gamma_shift][gray16 >> 8];\r
+                     rgb_error |= 1;\r
+                  }\r
+\r
+                  *(dp++) = (png_byte)((w>>8) & 0xff);\r
+                  *(dp++) = (png_byte)(w & 0xff);\r
+               }\r
+            }\r
+            else\r
+#endif\r
+            {\r
+               png_bytep sp = row;\r
+               png_bytep dp = row;\r
+               for (i = 0; i < row_width; i++)\r
+               {\r
+                  png_uint_16 red, green, blue, gray16;\r
+\r
+                  red   = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2;\r
+                  green = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2;\r
+                  blue  = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2;\r
+\r
+                  if (red != green || red != blue)\r
+                     rgb_error |= 1;\r
+                  gray16  = (png_uint_16)((rc*red + gc*green + bc*blue)>>15);\r
+                  *(dp++) = (png_byte)((gray16>>8) & 0xff);\r
+                  *(dp++) = (png_byte)(gray16 & 0xff);\r
+               }\r
+            }\r
+         }\r
+      }\r
+      if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)\r
+      {\r
+         if (row_info->bit_depth == 8)\r
+         {\r
+#if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)\r
+            if (png_ptr->gamma_from_1 != NULL && png_ptr->gamma_to_1 != NULL)\r
+            {\r
+               png_bytep sp = row;\r
+               png_bytep dp = row;\r
+               for (i = 0; i < row_width; i++)\r
+               {\r
+                  png_byte red   = png_ptr->gamma_to_1[*(sp++)];\r
+                  png_byte green = png_ptr->gamma_to_1[*(sp++)];\r
+                  png_byte blue  = png_ptr->gamma_to_1[*(sp++)];\r
+                  if (red != green || red != blue)\r
+                     rgb_error |= 1;\r
+                  *(dp++) =  png_ptr->gamma_from_1\r
+                             [(rc*red + gc*green + bc*blue)>>15];\r
+                  *(dp++) = *(sp++);  /* alpha */\r
+               }\r
+            }\r
+            else\r
+#endif\r
+            {\r
+               png_bytep sp = row;\r
+               png_bytep dp = row;\r
+               for (i = 0; i < row_width; i++)\r
+               {\r
+                  png_byte red   = *(sp++);\r
+                  png_byte green = *(sp++);\r
+                  png_byte blue  = *(sp++);\r
+                  if (red != green || red != blue)\r
+                     rgb_error |= 1;\r
+                  *(dp++) =  (png_byte)((rc*red + gc*green + bc*blue)>>15);\r
+                  *(dp++) = *(sp++);  /* alpha */\r
+               }\r
+            }\r
+         }\r
+         else /* RGBA bit_depth == 16 */\r
+         {\r
+#if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)\r
+            if (png_ptr->gamma_16_to_1 != NULL &&\r
+                png_ptr->gamma_16_from_1 != NULL)\r
+            {\r
+               png_bytep sp = row;\r
+               png_bytep dp = row;\r
+               for (i = 0; i < row_width; i++)\r
+               {\r
+                  png_uint_16 red, green, blue, w;\r
+\r
+                  red   = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2;\r
+                  green = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2;\r
+                  blue  = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2;\r
+\r
+                  if (red == green && red == blue)\r
+                     w = red;\r
+                  else\r
+                  {\r
+                     png_uint_16 red_1   = png_ptr->gamma_16_to_1[(red&0xff) >>\r
+                         png_ptr->gamma_shift][red>>8];\r
+                     png_uint_16 green_1 =\r
+                         png_ptr->gamma_16_to_1[(green&0xff) >>\r
+                         png_ptr->gamma_shift][green>>8];\r
+                     png_uint_16 blue_1  = png_ptr->gamma_16_to_1[(blue&0xff) >>\r
+                         png_ptr->gamma_shift][blue>>8];\r
+                     png_uint_16 gray16  = (png_uint_16)((rc * red_1\r
+                         + gc * green_1 + bc * blue_1)>>15);\r
+                     w = png_ptr->gamma_16_from_1[(gray16&0xff) >>\r
+                         png_ptr->gamma_shift][gray16 >> 8];\r
+                     rgb_error |= 1;\r
+                  }\r
+\r
+                  *(dp++) = (png_byte)((w>>8) & 0xff);\r
+                  *(dp++) = (png_byte)(w & 0xff);\r
+                  *(dp++) = *(sp++);  /* alpha */\r
+                  *(dp++) = *(sp++);\r
+               }\r
+            }\r
+            else\r
+#endif\r
+            {\r
+               png_bytep sp = row;\r
+               png_bytep dp = row;\r
+               for (i = 0; i < row_width; i++)\r
+               {\r
+                  png_uint_16 red, green, blue, gray16;\r
+                  red   = (png_uint_16)((*(sp)<<8) | *(sp+1)); sp+=2;\r
+                  green = (png_uint_16)((*(sp)<<8) | *(sp+1)); sp+=2;\r
+                  blue  = (png_uint_16)((*(sp)<<8) | *(sp+1)); sp+=2;\r
+                  if (red != green || red != blue)\r
+                     rgb_error |= 1;\r
+                  gray16  = (png_uint_16)((rc*red + gc*green + bc*blue)>>15);\r
+                  *(dp++) = (png_byte)((gray16>>8) & 0xff);\r
+                  *(dp++) = (png_byte)(gray16 & 0xff);\r
+                  *(dp++) = *(sp++);  /* alpha */\r
+                  *(dp++) = *(sp++);\r
+               }\r
+            }\r
+         }\r
+      }\r
+   row_info->channels -= (png_byte)2;\r
+      row_info->color_type &= ~PNG_COLOR_MASK_COLOR;\r
+      row_info->pixel_depth = (png_byte)(row_info->channels *\r
+         row_info->bit_depth);\r
+      row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);\r
+   }\r
+   return rgb_error;\r
+}\r
+#endif\r
+\r
+/* Build a grayscale palette.  Palette is assumed to be 1 << bit_depth\r
+ * large of png_color.  This lets grayscale images be treated as\r
+ * paletted.  Most useful for gamma correction and simplification\r
+ * of code.\r
+ */\r
+void PNGAPI\r
+png_build_grayscale_palette(int bit_depth, png_colorp palette)\r
+{\r
+   int num_palette;\r
+   int color_inc;\r
+   int i;\r
+   int v;\r
+\r
+   png_debug(1, "in png_do_build_grayscale_palette");\r
+\r
+   if (palette == NULL)\r
+      return;\r
+\r
+   switch (bit_depth)\r
+   {\r
+      case 1:\r
+         num_palette = 2;\r
+         color_inc = 0xff;\r
+         break;\r
+\r
+      case 2:\r
+         num_palette = 4;\r
+         color_inc = 0x55;\r
+         break;\r
+\r
+      case 4:\r
+         num_palette = 16;\r
+         color_inc = 0x11;\r
+         break;\r
+\r
+      case 8:\r
+         num_palette = 256;\r
+         color_inc = 1;\r
+         break;\r
+\r
+      default:\r
+         num_palette = 0;\r
+         color_inc = 0;\r
+         break;\r
+   }\r
+\r
+   for (i = 0, v = 0; i < num_palette; i++, v += color_inc)\r
+   {\r
+      palette[i].red = (png_byte)v;\r
+      palette[i].green = (png_byte)v;\r
+      palette[i].blue = (png_byte)v;\r
+   }\r
+}\r
+\r
+\r
+#ifdef PNG_READ_BACKGROUND_SUPPORTED\r
+/* Replace any alpha or transparency with the supplied background color.\r
+ * "background" is already in the screen gamma, while "background_1" is\r
+ * at a gamma of 1.0.  Paletted files have already been taken care of.\r
+ */\r
+void /* PRIVATE */\r
+png_do_background(png_row_infop row_info, png_bytep row,\r
+   png_color_16p trans_color, png_color_16p background\r
+#ifdef PNG_READ_GAMMA_SUPPORTED\r
+   , png_color_16p background_1,\r
+   png_bytep gamma_table, png_bytep gamma_from_1, png_bytep gamma_to_1,\r
+   png_uint_16pp gamma_16, png_uint_16pp gamma_16_from_1,\r
+   png_uint_16pp gamma_16_to_1, int gamma_shift\r
+#endif\r
+   )\r
+{\r
+   png_bytep sp, dp;\r
+   png_uint_32 i;\r
+   png_uint_32 row_width=row_info->width;\r
+   int shift;\r
+\r
+   png_debug(1, "in png_do_background");\r
+\r
+   if (background != NULL &&\r
+      (!(row_info->color_type & PNG_COLOR_MASK_ALPHA) ||\r
+      (row_info->color_type != PNG_COLOR_TYPE_PALETTE && trans_color)))\r
+   {\r
+      switch (row_info->color_type)\r
+      {\r
+         case PNG_COLOR_TYPE_GRAY:\r
+         {\r
+            switch (row_info->bit_depth)\r
+            {\r
+               case 1:\r
+               {\r
+                  sp = row;\r
+                  shift = 7;\r
+                  for (i = 0; i < row_width; i++)\r
+                  {\r
+                     if ((png_uint_16)((*sp >> shift) & 0x01)\r
+                        == trans_color->gray)\r
+                     {\r
+                        *sp &= (png_byte)((0x7f7f >> (7 - shift)) & 0xff);\r
+                        *sp |= (png_byte)(background->gray << shift);\r
+                     }\r
+                     if (!shift)\r
+                     {\r
+                        shift = 7;\r
+                        sp++;\r
+                     }\r
+                     else\r
+                        shift--;\r
+                  }\r
+                  break;\r
+               }\r
+\r
+               case 2:\r
+               {\r
+#ifdef PNG_READ_GAMMA_SUPPORTED\r
+                  if (gamma_table != NULL)\r
+                  {\r
+                     sp = row;\r
+                     shift = 6;\r
+                     for (i = 0; i < row_width; i++)\r
+                     {\r
+                        if ((png_uint_16)((*sp >> shift) & 0x03)\r
+                            == trans_color->gray)\r
+                        {\r
+                           *sp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff);\r
+                           *sp |= (png_byte)(background->gray << shift);\r
+                        }\r
+                        else\r
+                        {\r
+                           png_byte p = (png_byte)((*sp >> shift) & 0x03);\r
+                           png_byte g = (png_byte)((gamma_table [p | (p << 2) |\r
+                               (p << 4) | (p << 6)] >> 6) & 0x03);\r
+                           *sp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff);\r
+                           *sp |= (png_byte)(g << shift);\r
+                        }\r
+                        if (!shift)\r
+                        {\r
+                           shift = 6;\r
+                           sp++;\r
+                        }\r
+                        else\r
+                           shift -= 2;\r
+                     }\r
+                  }\r
+                  else\r
+#endif\r
+                  {\r
+                     sp = row;\r
+                     shift = 6;\r
+                     for (i = 0; i < row_width; i++)\r
+                     {\r
+                        if ((png_uint_16)((*sp >> shift) & 0x03)\r
+                            == trans_color->gray)\r
+                        {\r
+                           *sp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff);\r
+                           *sp |= (png_byte)(background->gray << shift);\r
+                        }\r
+                        if (!shift)\r
+                        {\r
+                           shift = 6;\r
+                           sp++;\r
+                        }\r
+                        else\r
+                           shift -= 2;\r
+                     }\r
+                  }\r
+                  break;\r
+               }\r
+\r
+               case 4:\r
+               {\r
+#ifdef PNG_READ_GAMMA_SUPPORTED\r
+                  if (gamma_table != NULL)\r
+                  {\r
+                     sp = row;\r
+                     shift = 4;\r
+                     for (i = 0; i < row_width; i++)\r
+                     {\r
+                        if ((png_uint_16)((*sp >> shift) & 0x0f)\r
+                            == trans_color->gray)\r
+                        {\r
+                           *sp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff);\r
+                           *sp |= (png_byte)(background->gray << shift);\r
+                        }\r
+                        else\r
+                        {\r
+                           png_byte p = (png_byte)((*sp >> shift) & 0x0f);\r
+                           png_byte g = (png_byte)((gamma_table[p |\r
+                             (p << 4)] >> 4) & 0x0f);\r
+                           *sp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff);\r
+                           *sp |= (png_byte)(g << shift);\r
+                        }\r
+                        if (!shift)\r
+                        {\r
+                           shift = 4;\r
+                           sp++;\r
+                        }\r
+                        else\r
+                           shift -= 4;\r
+                     }\r
+                  }\r
+                  else\r
+#endif\r
+                  {\r
+                     sp = row;\r
+                     shift = 4;\r
+                     for (i = 0; i < row_width; i++)\r
+                     {\r
+                        if ((png_uint_16)((*sp >> shift) & 0x0f)\r
+                            == trans_color->gray)\r
+                        {\r
+                           *sp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff);\r
+                           *sp |= (png_byte)(background->gray << shift);\r
+                        }\r
+                        if (!shift)\r
+                        {\r
+                           shift = 4;\r
+                           sp++;\r
+                        }\r
+                        else\r
+                           shift -= 4;\r
+                     }\r
+                  }\r
+                  break;\r
+               }\r
+\r
+               case 8:\r
+               {\r
+#ifdef PNG_READ_GAMMA_SUPPORTED\r
+                  if (gamma_table != NULL)\r
+                  {\r
+                     sp = row;\r
+                     for (i = 0; i < row_width; i++, sp++)\r
+                     {\r
+                        if (*sp == trans_color->gray)\r
+                        {\r
+                           *sp = (png_byte)background->gray;\r
+                        }\r
+                        else\r
+                        {\r
+                           *sp = gamma_table[*sp];\r
+                        }\r
+                     }\r
+                  }\r
+                  else\r
+#endif\r
+                  {\r
+                     sp = row;\r
+                     for (i = 0; i < row_width; i++, sp++)\r
+                     {\r
+                        if (*sp == trans_color->gray)\r
+                        {\r
+                           *sp = (png_byte)background->gray;\r
+                        }\r
+                     }\r
+                  }\r
+                  break;\r
+               }\r
+\r
+               case 16:\r
+               {\r
+#ifdef PNG_READ_GAMMA_SUPPORTED\r
+                  if (gamma_16 != NULL)\r
+                  {\r
+                     sp = row;\r
+                     for (i = 0; i < row_width; i++, sp += 2)\r
+                     {\r
+                        png_uint_16 v;\r
+\r
+                        v = (png_uint_16)(((*sp) << 8) + *(sp + 1));\r
+                        if (v == trans_color->gray)\r
+                        {\r
+                           /* Background is already in screen gamma */\r
+                           *sp = (png_byte)((background->gray >> 8) & 0xff);\r
+                           *(sp + 1) = (png_byte)(background->gray & 0xff);\r
+                        }\r
+                        else\r
+                        {\r
+                           v = gamma_16[*(sp + 1) >> gamma_shift][*sp];\r
+                           *sp = (png_byte)((v >> 8) & 0xff);\r
+                           *(sp + 1) = (png_byte)(v & 0xff);\r
+                        }\r
+                     }\r
+                  }\r
+                  else\r
+#endif\r
+                  {\r
+                     sp = row;\r
+                     for (i = 0; i < row_width; i++, sp += 2)\r
+                     {\r
+                        png_uint_16 v;\r
+\r
+                        v = (png_uint_16)(((*sp) << 8) + *(sp + 1));\r
+                        if (v == trans_color->gray)\r
+                        {\r
+                           *sp = (png_byte)((background->gray >> 8) & 0xff);\r
+                           *(sp + 1) = (png_byte)(background->gray & 0xff);\r
+                        }\r
+                     }\r
+                  }\r
+                  break;\r
+               }\r
+            }\r
+            break;\r
+         }\r
+\r
+         case PNG_COLOR_TYPE_RGB:\r
+         {\r
+            if (row_info->bit_depth == 8)\r
+            {\r
+#ifdef PNG_READ_GAMMA_SUPPORTED\r
+               if (gamma_table != NULL)\r
+               {\r
+                  sp = row;\r
+                  for (i = 0; i < row_width; i++, sp += 3)\r
+                  {\r
+                     if (*sp == trans_color->red &&\r
+                        *(sp + 1) == trans_color->green &&\r
+                        *(sp + 2) == trans_color->blue)\r
+                     {\r
+                        *sp = (png_byte)background->red;\r
+                        *(sp + 1) = (png_byte)background->green;\r
+                        *(sp + 2) = (png_byte)background->blue;\r
+                     }\r
+                     else\r
+                     {\r
+                        *sp = gamma_table[*sp];\r
+                        *(sp + 1) = gamma_table[*(sp + 1)];\r
+                        *(sp + 2) = gamma_table[*(sp + 2)];\r
+                     }\r
+                  }\r
+               }\r
+               else\r
+#endif\r
+               {\r
+                  sp = row;\r
+                  for (i = 0; i < row_width; i++, sp += 3)\r
+                  {\r
+                     if (*sp == trans_color->red &&\r
+                        *(sp + 1) == trans_color->green &&\r
+                        *(sp + 2) == trans_color->blue)\r
+                     {\r
+                        *sp = (png_byte)background->red;\r
+                        *(sp + 1) = (png_byte)background->green;\r
+                        *(sp + 2) = (png_byte)background->blue;\r
+                     }\r
+                  }\r
+               }\r
+            }\r
+            else /* if (row_info->bit_depth == 16) */\r
+            {\r
+#ifdef PNG_READ_GAMMA_SUPPORTED\r
+               if (gamma_16 != NULL)\r
+               {\r
+                  sp = row;\r
+                  for (i = 0; i < row_width; i++, sp += 6)\r
+                  {\r
+                     png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp + 1));\r
+                     png_uint_16 g = (png_uint_16)(((*(sp+2)) << 8) + *(sp+3));\r
+                     png_uint_16 b = (png_uint_16)(((*(sp+4)) << 8) + *(sp+5));\r
+                     if (r == trans_color->red && g == trans_color->green &&\r
+                        b == trans_color->blue)\r
+                     {\r
+                        /* Background is already in screen gamma */\r
+                        *sp = (png_byte)((background->red >> 8) & 0xff);\r
+                        *(sp + 1) = (png_byte)(background->red & 0xff);\r
+                        *(sp + 2) = (png_byte)((background->green >> 8) & 0xff);\r
+                        *(sp + 3) = (png_byte)(background->green & 0xff);\r
+                        *(sp + 4) = (png_byte)((background->blue >> 8) & 0xff);\r
+                        *(sp + 5) = (png_byte)(background->blue & 0xff);\r
+                     }\r
+                     else\r
+                     {\r
+                        png_uint_16 v = gamma_16[*(sp + 1) >> gamma_shift][*sp];\r
+                        *sp = (png_byte)((v >> 8) & 0xff);\r
+                        *(sp + 1) = (png_byte)(v & 0xff);\r
+                        v = gamma_16[*(sp + 3) >> gamma_shift][*(sp + 2)];\r
+                        *(sp + 2) = (png_byte)((v >> 8) & 0xff);\r
+                        *(sp + 3) = (png_byte)(v & 0xff);\r
+                        v = gamma_16[*(sp + 5) >> gamma_shift][*(sp + 4)];\r
+                        *(sp + 4) = (png_byte)((v >> 8) & 0xff);\r
+                        *(sp + 5) = (png_byte)(v & 0xff);\r
+                     }\r
+                  }\r
+               }\r
+               else\r
+#endif\r
+               {\r
+                  sp = row;\r
+                  for (i = 0; i < row_width; i++, sp += 6)\r
+                  {\r
+                     png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp+1));\r
+                     png_uint_16 g = (png_uint_16)(((*(sp+2)) << 8) + *(sp+3));\r
+                     png_uint_16 b = (png_uint_16)(((*(sp+4)) << 8) + *(sp+5));\r
+\r
+                     if (r == trans_color->red && g == trans_color->green &&\r
+                        b == trans_color->blue)\r
+                     {\r
+                        *sp = (png_byte)((background->red >> 8) & 0xff);\r
+                        *(sp + 1) = (png_byte)(background->red & 0xff);\r
+                        *(sp + 2) = (png_byte)((background->green >> 8) & 0xff);\r
+                        *(sp + 3) = (png_byte)(background->green & 0xff);\r
+                        *(sp + 4) = (png_byte)((background->blue >> 8) & 0xff);\r
+                        *(sp + 5) = (png_byte)(background->blue & 0xff);\r
+                     }\r
+                  }\r
+               }\r
+            }\r
+            break;\r
+         }\r
+\r
+         case PNG_COLOR_TYPE_GRAY_ALPHA:\r
+         {\r
+            if (row_info->bit_depth == 8)\r
+            {\r
+#ifdef PNG_READ_GAMMA_SUPPORTED\r
+               if (gamma_to_1 != NULL && gamma_from_1 != NULL &&\r
+                   gamma_table != NULL)\r
+               {\r
+                  sp = row;\r
+                  dp = row;\r
+                  for (i = 0; i < row_width; i++, sp += 2, dp++)\r
+                  {\r
+                     png_uint_16 a = *(sp + 1);\r
+\r
+                     if (a == 0xff)\r
+                     {\r
+                        *dp = gamma_table[*sp];\r
+                     }\r
+                     else if (a == 0)\r
+                     {\r
+                        /* Background is already in screen gamma */\r
+                        *dp = (png_byte)background->gray;\r
+                     }\r
+                     else\r
+                     {\r
+                        png_byte v, w;\r
+\r
+                        v = gamma_to_1[*sp];\r
+                        png_composite(w, v, a, background_1->gray);\r
+                        *dp = gamma_from_1[w];\r
+                     }\r
+                  }\r
+               }\r
+               else\r
+#endif\r
+               {\r
+                  sp = row;\r
+                  dp = row;\r
+                  for (i = 0; i < row_width; i++, sp += 2, dp++)\r
+                  {\r
+                     png_byte a = *(sp + 1);\r
+\r
+                     if (a == 0xff)\r
+                     {\r
+                        *dp = *sp;\r
+                     }\r
+#ifdef PNG_READ_GAMMA_SUPPORTED\r
+                     else if (a == 0)\r
+                     {\r
+                        *dp = (png_byte)background->gray;\r
+                     }\r
+                     else\r
+                     {\r
+                        png_composite(*dp, *sp, a, background_1->gray);\r
+                     }\r
+#else\r
+                     *dp = (png_byte)background->gray;\r
+#endif\r
+                  }\r
+               }\r
+            }\r
+            else /* if (png_ptr->bit_depth == 16) */\r
+            {\r
+#ifdef PNG_READ_GAMMA_SUPPORTED\r
+               if (gamma_16 != NULL && gamma_16_from_1 != NULL &&\r
+                   gamma_16_to_1 != NULL)\r
+               {\r
+                  sp = row;\r
+                  dp = row;\r
+                  for (i = 0; i < row_width; i++, sp += 4, dp += 2)\r
+                  {\r
+                     png_uint_16 a = (png_uint_16)(((*(sp+2)) << 8) + *(sp+3));\r
+\r
+                     if (a == (png_uint_16)0xffff)\r
+                     {\r
+                        png_uint_16 v;\r
+\r
+                        v = gamma_16[*(sp + 1) >> gamma_shift][*sp];\r
+                        *dp = (png_byte)((v >> 8) & 0xff);\r
+                        *(dp + 1) = (png_byte)(v & 0xff);\r
+                     }\r
+#ifdef PNG_READ_GAMMA_SUPPORTED\r
+                     else if (a == 0)\r
+#else\r
+                     else\r
+#endif\r
+                     {\r
+                        /* Background is already in screen gamma */\r
+                        *dp = (png_byte)((background->gray >> 8) & 0xff);\r
+                        *(dp + 1) = (png_byte)(background->gray & 0xff);\r
+                     }\r
+#ifdef PNG_READ_GAMMA_SUPPORTED\r
+                     else\r
+                     {\r
+                        png_uint_16 g, v, w;\r
+\r
+                        g = gamma_16_to_1[*(sp + 1) >> gamma_shift][*sp];\r
+                        png_composite_16(v, g, a, background_1->gray);\r
+                        w = gamma_16_from_1[(v&0xff) >> gamma_shift][v >> 8];\r
+                        *dp = (png_byte)((w >> 8) & 0xff);\r
+                        *(dp + 1) = (png_byte)(w & 0xff);\r
+                     }\r
+#endif\r
+                  }\r
+               }\r
+               else\r
+#endif\r
+               {\r
+                  sp = row;\r
+                  dp = row;\r
+                  for (i = 0; i < row_width; i++, sp += 4, dp += 2)\r
+                  {\r
+                     png_uint_16 a = (png_uint_16)(((*(sp+2)) << 8) + *(sp+3));\r
+                     if (a == (png_uint_16)0xffff)\r
+                     {\r
+                        png_memcpy(dp, sp, 2);\r
+                     }\r
+#ifdef PNG_READ_GAMMA_SUPPORTED\r
+                     else if (a == 0)\r
+#else\r
+                     else\r
+#endif\r
+                     {\r
+                        *dp = (png_byte)((background->gray >> 8) & 0xff);\r
+                        *(dp + 1) = (png_byte)(background->gray & 0xff);\r
+                     }\r
+#ifdef PNG_READ_GAMMA_SUPPORTED\r
+                     else\r
+                     {\r
+                        png_uint_16 g, v;\r
+\r
+                        g = (png_uint_16)(((*sp) << 8) + *(sp + 1));\r
+                        png_composite_16(v, g, a, background_1->gray);\r
+                        *dp = (png_byte)((v >> 8) & 0xff);\r
+                        *(dp + 1) = (png_byte)(v & 0xff);\r
+                     }\r
+#endif\r
+                  }\r
+               }\r
+            }\r
+            break;\r
+         }\r
+\r
+         case PNG_COLOR_TYPE_RGB_ALPHA:\r
+         {\r
+            if (row_info->bit_depth == 8)\r
+            {\r
+#ifdef PNG_READ_GAMMA_SUPPORTED\r
+               if (gamma_to_1 != NULL && gamma_from_1 != NULL &&\r
+                   gamma_table != NULL)\r
+               {\r
+                  sp = row;\r
+                  dp = row;\r
+                  for (i = 0; i < row_width; i++, sp += 4, dp += 3)\r
+                  {\r
+                     png_byte a = *(sp + 3);\r
+\r
+                     if (a == 0xff)\r
+                     {\r
+                        *dp = gamma_table[*sp];\r
+                        *(dp + 1) = gamma_table[*(sp + 1)];\r
+                        *(dp + 2) = gamma_table[*(sp + 2)];\r
+                     }\r
+                     else if (a == 0)\r
+                     {\r
+                        /* Background is already in screen gamma */\r
+                        *dp = (png_byte)background->red;\r
+                        *(dp + 1) = (png_byte)background->green;\r
+                        *(dp + 2) = (png_byte)background->blue;\r
+                     }\r
+                     else\r
+                     {\r
+                        png_byte v, w;\r
+\r
+                        v = gamma_to_1[*sp];\r
+                        png_composite(w, v, a, background_1->red);\r
+                        *dp = gamma_from_1[w];\r
+                        v = gamma_to_1[*(sp + 1)];\r
+                        png_composite(w, v, a, background_1->green);\r
+                        *(dp + 1) = gamma_from_1[w];\r
+                        v = gamma_to_1[*(sp + 2)];\r
+                        png_composite(w, v, a, background_1->blue);\r
+                        *(dp + 2) = gamma_from_1[w];\r
+                     }\r
+                  }\r
+               }\r
+               else\r
+#endif\r
+               {\r
+                  sp = row;\r
+                  dp = row;\r
+                  for (i = 0; i < row_width; i++, sp += 4, dp += 3)\r
+                  {\r
+                     png_byte a = *(sp + 3);\r
+\r
+                     if (a == 0xff)\r
+                     {\r
+                        *dp = *sp;\r
+                        *(dp + 1) = *(sp + 1);\r
+                        *(dp + 2) = *(sp + 2);\r
+                     }\r
+                     else if (a == 0)\r
+                     {\r
+                        *dp = (png_byte)background->red;\r
+                        *(dp + 1) = (png_byte)background->green;\r
+                        *(dp + 2) = (png_byte)background->blue;\r
+                     }\r
+                     else\r
+                     {\r
+                        png_composite(*dp, *sp, a, background->red);\r
+                        png_composite(*(dp + 1), *(sp + 1), a,\r
+                           background->green);\r
+                        png_composite(*(dp + 2), *(sp + 2), a,\r
+                           background->blue);\r
+                     }\r
+                  }\r
+               }\r
+            }\r
+            else /* if (row_info->bit_depth == 16) */\r
+            {\r
+#ifdef PNG_READ_GAMMA_SUPPORTED\r
+               if (gamma_16 != NULL && gamma_16_from_1 != NULL &&\r
+                   gamma_16_to_1 != NULL)\r
+               {\r
+                  sp = row;\r
+                  dp = row;\r
+                  for (i = 0; i < row_width; i++, sp += 8, dp += 6)\r
+                  {\r
+                     png_uint_16 a = (png_uint_16)(((png_uint_16)(*(sp + 6))\r
+                         << 8) + (png_uint_16)(*(sp + 7)));\r
+                     if (a == (png_uint_16)0xffff)\r
+                     {\r
+                        png_uint_16 v;\r
+\r
+                        v = gamma_16[*(sp + 1) >> gamma_shift][*sp];\r
+                        *dp = (png_byte)((v >> 8) & 0xff);\r
+                        *(dp + 1) = (png_byte)(v & 0xff);\r
+                        v = gamma_16[*(sp + 3) >> gamma_shift][*(sp + 2)];\r
+                        *(dp + 2) = (png_byte)((v >> 8) & 0xff);\r
+                        *(dp + 3) = (png_byte)(v & 0xff);\r
+                        v = gamma_16[*(sp + 5) >> gamma_shift][*(sp + 4)];\r
+                        *(dp + 4) = (png_byte)((v >> 8) & 0xff);\r
+                        *(dp + 5) = (png_byte)(v & 0xff);\r
+                     }\r
+                     else if (a == 0)\r
+                     {\r
+                        /* Background is already in screen gamma */\r
+                        *dp = (png_byte)((background->red >> 8) & 0xff);\r
+                        *(dp + 1) = (png_byte)(background->red & 0xff);\r
+                        *(dp + 2) = (png_byte)((background->green >> 8) & 0xff);\r
+                        *(dp + 3) = (png_byte)(background->green & 0xff);\r
+                        *(dp + 4) = (png_byte)((background->blue >> 8) & 0xff);\r
+                        *(dp + 5) = (png_byte)(background->blue & 0xff);\r
+                     }\r
+                     else\r
+                     {\r
+                        png_uint_16 v, w, x;\r
+\r
+                        v = gamma_16_to_1[*(sp + 1) >> gamma_shift][*sp];\r
+                        png_composite_16(w, v, a, background_1->red);\r
+                        x = gamma_16_from_1[((w&0xff) >> gamma_shift)][w >> 8];\r
+                        *dp = (png_byte)((x >> 8) & 0xff);\r
+                        *(dp + 1) = (png_byte)(x & 0xff);\r
+                        v = gamma_16_to_1[*(sp + 3) >> gamma_shift][*(sp + 2)];\r
+                        png_composite_16(w, v, a, background_1->green);\r
+                        x = gamma_16_from_1[((w&0xff) >> gamma_shift)][w >> 8];\r
+                        *(dp + 2) = (png_byte)((x >> 8) & 0xff);\r
+                        *(dp + 3) = (png_byte)(x & 0xff);\r
+                        v = gamma_16_to_1[*(sp + 5) >> gamma_shift][*(sp + 4)];\r
+                        png_composite_16(w, v, a, background_1->blue);\r
+                        x = gamma_16_from_1[(w & 0xff) >> gamma_shift][w >> 8];\r
+                        *(dp + 4) = (png_byte)((x >> 8) & 0xff);\r
+                        *(dp + 5) = (png_byte)(x & 0xff);\r
+                     }\r
+                  }\r
+               }\r
+               else\r
+#endif\r
+               {\r
+                  sp = row;\r
+                  dp = row;\r
+                  for (i = 0; i < row_width; i++, sp += 8, dp += 6)\r
+                  {\r
+                     png_uint_16 a = (png_uint_16)(((png_uint_16)(*(sp + 6))\r
+                        << 8) + (png_uint_16)(*(sp + 7)));\r
+                     if (a == (png_uint_16)0xffff)\r
+                     {\r
+                        png_memcpy(dp, sp, 6);\r
+                     }\r
+                     else if (a == 0)\r
+                     {\r
+                        *dp = (png_byte)((background->red >> 8) & 0xff);\r
+                        *(dp + 1) = (png_byte)(background->red & 0xff);\r
+                        *(dp + 2) = (png_byte)((background->green >> 8) & 0xff);\r
+                        *(dp + 3) = (png_byte)(background->green & 0xff);\r
+                        *(dp + 4) = (png_byte)((background->blue >> 8) & 0xff);\r
+                        *(dp + 5) = (png_byte)(background->blue & 0xff);\r
+                     }\r
+                     else\r
+                     {\r
+                        png_uint_16 v;\r
+\r
+                        png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp + 1));\r
+                        png_uint_16 g = (png_uint_16)(((*(sp + 2)) << 8)\r
+                            + *(sp + 3));\r
+                        png_uint_16 b = (png_uint_16)(((*(sp + 4)) << 8)\r
+                            + *(sp + 5));\r
+\r
+                        png_composite_16(v, r, a, background->red);\r
+                        *dp = (png_byte)((v >> 8) & 0xff);\r
+                        *(dp + 1) = (png_byte)(v & 0xff);\r
+                        png_composite_16(v, g, a, background->green);\r
+                        *(dp + 2) = (png_byte)((v >> 8) & 0xff);\r
+                        *(dp + 3) = (png_byte)(v & 0xff);\r
+                        png_composite_16(v, b, a, background->blue);\r
+                        *(dp + 4) = (png_byte)((v >> 8) & 0xff);\r
+                        *(dp + 5) = (png_byte)(v & 0xff);\r
+                     }\r
+                  }\r
+               }\r
+            }\r
+            break;\r
+         }\r
+      }\r
+\r
+      if (row_info->color_type & PNG_COLOR_MASK_ALPHA)\r
+      {\r
+         row_info->color_type &= ~PNG_COLOR_MASK_ALPHA;\r
+         row_info->channels--;\r
+         row_info->pixel_depth = (png_byte)(row_info->channels *\r
+            row_info->bit_depth);\r
+         row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);\r
+      }\r
+   }\r
+}\r
+#endif\r
+\r
+#ifdef PNG_READ_GAMMA_SUPPORTED\r
+/* Gamma correct the image, avoiding the alpha channel.  Make sure\r
+ * you do this after you deal with the transparency issue on grayscale\r
+ * or RGB images. If your bit depth is 8, use gamma_table, if it\r
+ * is 16, use gamma_16_table and gamma_shift.  Build these with\r
+ * build_gamma_table().\r
+ */\r
+void /* PRIVATE */\r
+png_do_gamma(png_row_infop row_info, png_bytep row,\r
+   png_bytep gamma_table, png_uint_16pp gamma_16_table,\r
+   int gamma_shift)\r
+{\r
+   png_bytep sp;\r
+   png_uint_32 i;\r
+   png_uint_32 row_width=row_info->width;\r
+\r
+   png_debug(1, "in png_do_gamma");\r
+\r
+   if (\r
+       ((row_info->bit_depth <= 8 && gamma_table != NULL) ||\r
+        (row_info->bit_depth == 16 && gamma_16_table != NULL)))\r
+   {\r
+      switch (row_info->color_type)\r
+      {\r
+         case PNG_COLOR_TYPE_RGB:\r
+         {\r
+            if (row_info->bit_depth == 8)\r
+            {\r
+               sp = row;\r
+               for (i = 0; i < row_width; i++)\r
+               {\r
+                  *sp = gamma_table[*sp];\r
+                  sp++;\r
+                  *sp = gamma_table[*sp];\r
+                  sp++;\r
+                  *sp = gamma_table[*sp];\r
+                  sp++;\r
+               }\r
+            }\r
+            else /* if (row_info->bit_depth == 16) */\r
+            {\r
+               sp = row;\r
+               for (i = 0; i < row_width; i++)\r
+               {\r
+                  png_uint_16 v;\r
+\r
+                  v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];\r
+                  *sp = (png_byte)((v >> 8) & 0xff);\r
+                  *(sp + 1) = (png_byte)(v & 0xff);\r
+                  sp += 2;\r
+                  v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];\r
+                  *sp = (png_byte)((v >> 8) & 0xff);\r
+                  *(sp + 1) = (png_byte)(v & 0xff);\r
+                  sp += 2;\r
+                  v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];\r
+                  *sp = (png_byte)((v >> 8) & 0xff);\r
+                  *(sp + 1) = (png_byte)(v & 0xff);\r
+                  sp += 2;\r
+               }\r
+            }\r
+            break;\r
+         }\r
+\r
+         case PNG_COLOR_TYPE_RGB_ALPHA:\r
+         {\r
+            if (row_info->bit_depth == 8)\r
+            {\r
+               sp = row;\r
+               for (i = 0; i < row_width; i++)\r
+               {\r
+                  *sp = gamma_table[*sp];\r
+                  sp++;\r
+                  *sp = gamma_table[*sp];\r
+                  sp++;\r
+                  *sp = gamma_table[*sp];\r
+                  sp++;\r
+                  sp++;\r
+               }\r
+            }\r
+            else /* if (row_info->bit_depth == 16) */\r
+            {\r
+               sp = row;\r
+               for (i = 0; i < row_width; i++)\r
+               {\r
+                  png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];\r
+                  *sp = (png_byte)((v >> 8) & 0xff);\r
+                  *(sp + 1) = (png_byte)(v & 0xff);\r
+                  sp += 2;\r
+                  v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];\r
+                  *sp = (png_byte)((v >> 8) & 0xff);\r
+                  *(sp + 1) = (png_byte)(v & 0xff);\r
+                  sp += 2;\r
+                  v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];\r
+                  *sp = (png_byte)((v >> 8) & 0xff);\r
+                  *(sp + 1) = (png_byte)(v & 0xff);\r
+                  sp += 4;\r
+               }\r
+            }\r
+            break;\r
+         }\r
+\r
+         case PNG_COLOR_TYPE_GRAY_ALPHA:\r
+         {\r
+            if (row_info->bit_depth == 8)\r
+            {\r
+               sp = row;\r
+               for (i = 0; i < row_width; i++)\r
+               {\r
+                  *sp = gamma_table[*sp];\r
+                  sp += 2;\r
+               }\r
+            }\r
+            else /* if (row_info->bit_depth == 16) */\r
+            {\r
+               sp = row;\r
+               for (i = 0; i < row_width; i++)\r
+               {\r
+                  png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];\r
+                  *sp = (png_byte)((v >> 8) & 0xff);\r
+                  *(sp + 1) = (png_byte)(v & 0xff);\r
+                  sp += 4;\r
+               }\r
+            }\r
+            break;\r
+         }\r
+\r
+         case PNG_COLOR_TYPE_GRAY:\r
+         {\r
+            if (row_info->bit_depth == 2)\r
+            {\r
+               sp = row;\r
+               for (i = 0; i < row_width; i += 4)\r
+               {\r
+                  int a = *sp & 0xc0;\r
+                  int b = *sp & 0x30;\r
+                  int c = *sp & 0x0c;\r
+                  int d = *sp & 0x03;\r
+\r
+                  *sp = (png_byte)(\r
+                      ((((int)gamma_table[a|(a>>2)|(a>>4)|(a>>6)])   ) & 0xc0)|\r
+                      ((((int)gamma_table[(b<<2)|b|(b>>2)|(b>>4)])>>2) & 0x30)|\r
+                      ((((int)gamma_table[(c<<4)|(c<<2)|c|(c>>2)])>>4) & 0x0c)|\r
+                      ((((int)gamma_table[(d<<6)|(d<<4)|(d<<2)|d])>>6) ));\r
+                  sp++;\r
+               }\r
+            }\r
+\r
+            if (row_info->bit_depth == 4)\r
+            {\r
+               sp = row;\r
+               for (i = 0; i < row_width; i += 2)\r
+               {\r
+                  int msb = *sp & 0xf0;\r
+                  int lsb = *sp & 0x0f;\r
+\r
+                  *sp = (png_byte)((((int)gamma_table[msb | (msb >> 4)]) & 0xf0)\r
+                      | (((int)gamma_table[(lsb << 4) | lsb]) >> 4));\r
+                  sp++;\r
+               }\r
+            }\r
+\r
+            else if (row_info->bit_depth == 8)\r
+            {\r
+               sp = row;\r
+               for (i = 0; i < row_width; i++)\r
+               {\r
+                  *sp = gamma_table[*sp];\r
+                  sp++;\r
+               }\r
+            }\r
+\r
+            else if (row_info->bit_depth == 16)\r
+            {\r
+               sp = row;\r
+               for (i = 0; i < row_width; i++)\r
+               {\r
+                  png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];\r
+                  *sp = (png_byte)((v >> 8) & 0xff);\r
+                  *(sp + 1) = (png_byte)(v & 0xff);\r
+                  sp += 2;\r
+               }\r
+            }\r
+            break;\r
+         }\r
+      }\r
+   }\r
+}\r
+#endif\r
+\r
+#ifdef PNG_READ_EXPAND_SUPPORTED\r
+/* Expands a palette row to an RGB or RGBA row depending\r
+ * upon whether you supply trans and num_trans.\r
+ */\r
+void /* PRIVATE */\r
+png_do_expand_palette(png_row_infop row_info, png_bytep row,\r
+   png_colorp palette, png_bytep trans_alpha, int num_trans)\r
+{\r
+   int shift, value;\r
+   png_bytep sp, dp;\r
+   png_uint_32 i;\r
+   png_uint_32 row_width=row_info->width;\r
+\r
+   png_debug(1, "in png_do_expand_palette");\r
+\r
+   if (\r
+       row_info->color_type == PNG_COLOR_TYPE_PALETTE)\r
+   {\r
+      if (row_info->bit_depth < 8)\r
+      {\r
+         switch (row_info->bit_depth)\r
+         {\r
+            case 1:\r
+            {\r
+               sp = row + (png_size_t)((row_width - 1) >> 3);\r
+               dp = row + (png_size_t)row_width - 1;\r
+               shift = 7 - (int)((row_width + 7) & 0x07);\r
+               for (i = 0; i < row_width; i++)\r
+               {\r
+                  if ((*sp >> shift) & 0x01)\r
+                     *dp = 1;\r
+                  else\r
+                     *dp = 0;\r
+                  if (shift == 7)\r
+                  {\r
+                     shift = 0;\r
+                     sp--;\r
+                  }\r
+                  else\r
+                     shift++;\r
+\r
+                  dp--;\r
+               }\r
+               break;\r
+            }\r
+\r
+            case 2:\r
+            {\r
+               sp = row + (png_size_t)((row_width - 1) >> 2);\r
+               dp = row + (png_size_t)row_width - 1;\r
+               shift = (int)((3 - ((row_width + 3) & 0x03)) << 1);\r
+               for (i = 0; i < row_width; i++)\r
+               {\r
+                  value = (*sp >> shift) & 0x03;\r
+                  *dp = (png_byte)value;\r
+                  if (shift == 6)\r
+                  {\r
+                     shift = 0;\r
+                     sp--;\r
+                  }\r
+                  else\r
+                     shift += 2;\r
+\r
+                  dp--;\r
+               }\r
+               break;\r
+            }\r
+\r
+            case 4:\r
+            {\r
+               sp = row + (png_size_t)((row_width - 1) >> 1);\r
+               dp = row + (png_size_t)row_width - 1;\r
+               shift = (int)((row_width & 0x01) << 2);\r
+               for (i = 0; i < row_width; i++)\r
+               {\r
+                  value = (*sp >> shift) & 0x0f;\r
+                  *dp = (png_byte)value;\r
+                  if (shift == 4)\r
+                  {\r
+                     shift = 0;\r
+                     sp--;\r
+                  }\r
+                  else\r
+                     shift += 4;\r
+\r
+                  dp--;\r
+               }\r
+               break;\r
+            }\r
+         }\r
+         row_info->bit_depth = 8;\r
+         row_info->pixel_depth = 8;\r
+         row_info->rowbytes = row_width;\r
+      }\r
+      switch (row_info->bit_depth)\r
+      {\r
+         case 8:\r
+         {\r
+            if (trans_alpha != NULL)\r
+            {\r
+               sp = row + (png_size_t)row_width - 1;\r
+               dp = row + (png_size_t)(row_width << 2) - 1;\r
+\r
+               for (i = 0; i < row_width; i++)\r
+               {\r
+                  if ((int)(*sp) >= num_trans)\r
+                     *dp-- = 0xff;\r
+                  else\r
+                     *dp-- = trans_alpha[*sp];\r
+                  *dp-- = palette[*sp].blue;\r
+                  *dp-- = palette[*sp].green;\r
+                  *dp-- = palette[*sp].red;\r
+                  sp--;\r
+               }\r
+               row_info->bit_depth = 8;\r
+               row_info->pixel_depth = 32;\r
+               row_info->rowbytes = row_width * 4;\r
+               row_info->color_type = 6;\r
+               row_info->channels = 4;\r
+            }\r
+            else\r
+            {\r
+               sp = row + (png_size_t)row_width - 1;\r
+               dp = row + (png_size_t)(row_width * 3) - 1;\r
+\r
+               for (i = 0; i < row_width; i++)\r
+               {\r
+                  *dp-- = palette[*sp].blue;\r
+                  *dp-- = palette[*sp].green;\r
+                  *dp-- = palette[*sp].red;\r
+                  sp--;\r
+               }\r
+\r
+               row_info->bit_depth = 8;\r
+               row_info->pixel_depth = 24;\r
+               row_info->rowbytes = row_width * 3;\r
+               row_info->color_type = 2;\r
+               row_info->channels = 3;\r
+            }\r
+            break;\r
+         }\r
+      }\r
+   }\r
+}\r
+\r
+/* If the bit depth < 8, it is expanded to 8.  Also, if the already\r
+ * expanded transparency value is supplied, an alpha channel is built.\r
+ */\r
+void /* PRIVATE */\r
+png_do_expand(png_row_infop row_info, png_bytep row,\r
+   png_color_16p trans_value)\r
+{\r
+   int shift, value;\r
+   png_bytep sp, dp;\r
+   png_uint_32 i;\r
+   png_uint_32 row_width=row_info->width;\r
+\r
+   png_debug(1, "in png_do_expand");\r
+\r
+   {\r
+      if (row_info->color_type == PNG_COLOR_TYPE_GRAY)\r
+      {\r
+         png_uint_16 gray = (png_uint_16)(trans_value ? trans_value->gray : 0);\r
+\r
+         if (row_info->bit_depth < 8)\r
+         {\r
+            switch (row_info->bit_depth)\r
+            {\r
+               case 1:\r
+               {\r
+                  gray = (png_uint_16)((gray&0x01)*0xff);\r
+                  sp = row + (png_size_t)((row_width - 1) >> 3);\r
+                  dp = row + (png_size_t)row_width - 1;\r
+                  shift = 7 - (int)((row_width + 7) & 0x07);\r
+                  for (i = 0; i < row_width; i++)\r
+                  {\r
+                     if ((*sp >> shift) & 0x01)\r
+                        *dp = 0xff;\r
+                     else\r
+                        *dp = 0;\r
+                     if (shift == 7)\r
+                     {\r
+                        shift = 0;\r
+                        sp--;\r
+                     }\r
+                     else\r
+                        shift++;\r
+\r
+                     dp--;\r
+                  }\r
+                  break;\r
+               }\r
+\r
+               case 2:\r
+               {\r
+                  gray = (png_uint_16)((gray&0x03)*0x55);\r
+                  sp = row + (png_size_t)((row_width - 1) >> 2);\r
+                  dp = row + (png_size_t)row_width - 1;\r
+                  shift = (int)((3 - ((row_width + 3) & 0x03)) << 1);\r
+                  for (i = 0; i < row_width; i++)\r
+                  {\r
+                     value = (*sp >> shift) & 0x03;\r
+                     *dp = (png_byte)(value | (value << 2) | (value << 4) |\r
+                        (value << 6));\r
+                     if (shift == 6)\r
+                     {\r
+                        shift = 0;\r
+                        sp--;\r
+                     }\r
+                     else\r
+                        shift += 2;\r
+\r
+                     dp--;\r
+                  }\r
+                  break;\r
+               }\r
+\r
+               case 4:\r
+               {\r
+                  gray = (png_uint_16)((gray&0x0f)*0x11);\r
+                  sp = row + (png_size_t)((row_width - 1) >> 1);\r
+                  dp = row + (png_size_t)row_width - 1;\r
+                  shift = (int)((1 - ((row_width + 1) & 0x01)) << 2);\r
+                  for (i = 0; i < row_width; i++)\r
+                  {\r
+                     value = (*sp >> shift) & 0x0f;\r
+                     *dp = (png_byte)(value | (value << 4));\r
+                     if (shift == 4)\r
+                     {\r
+                        shift = 0;\r
+                        sp--;\r
+                     }\r
+                     else\r
+                        shift = 4;\r
+\r
+                     dp--;\r
+                  }\r
+                  break;\r
+               }\r
+            }\r
+\r
+            row_info->bit_depth = 8;\r
+            row_info->pixel_depth = 8;\r
+            row_info->rowbytes = row_width;\r
+         }\r
+\r
+         if (trans_value != NULL)\r
+         {\r
+            if (row_info->bit_depth == 8)\r
+            {\r
+               gray = gray & 0xff;\r
+               sp = row + (png_size_t)row_width - 1;\r
+               dp = row + (png_size_t)(row_width << 1) - 1;\r
+               for (i = 0; i < row_width; i++)\r
+               {\r
+                  if (*sp == gray)\r
+                     *dp-- = 0;\r
+                  else\r
+                     *dp-- = 0xff;\r
+                  *dp-- = *sp--;\r
+               }\r
+            }\r
+\r
+            else if (row_info->bit_depth == 16)\r
+            {\r
+               png_byte gray_high = (gray >> 8) & 0xff;\r
+               png_byte gray_low = gray & 0xff;\r
+               sp = row + row_info->rowbytes - 1;\r
+               dp = row + (row_info->rowbytes << 1) - 1;\r
+               for (i = 0; i < row_width; i++)\r
+               {\r
+                  if (*(sp - 1) == gray_high && *(sp) == gray_low)\r
+                  {\r
+                     *dp-- = 0;\r
+                     *dp-- = 0;\r
+                  }\r
+                  else\r
+                  {\r
+                     *dp-- = 0xff;\r
+                     *dp-- = 0xff;\r
+                  }\r
+                  *dp-- = *sp--;\r
+                  *dp-- = *sp--;\r
+               }\r
+            }\r
+\r
+            row_info->color_type = PNG_COLOR_TYPE_GRAY_ALPHA;\r
+            row_info->channels = 2;\r
+            row_info->pixel_depth = (png_byte)(row_info->bit_depth << 1);\r
+            row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,\r
+               row_width);\r
+         }\r
+      }\r
+      else if (row_info->color_type == PNG_COLOR_TYPE_RGB && trans_value)\r
+      {\r
+         if (row_info->bit_depth == 8)\r
+         {\r
+            png_byte red = trans_value->red & 0xff;\r
+            png_byte green = trans_value->green & 0xff;\r
+            png_byte blue = trans_value->blue & 0xff;\r
+            sp = row + (png_size_t)row_info->rowbytes - 1;\r
+            dp = row + (png_size_t)(row_width << 2) - 1;\r
+            for (i = 0; i < row_width; i++)\r
+            {\r
+               if (*(sp - 2) == red && *(sp - 1) == green && *(sp) == blue)\r
+                  *dp-- = 0;\r
+               else\r
+                  *dp-- = 0xff;\r
+               *dp-- = *sp--;\r
+               *dp-- = *sp--;\r
+               *dp-- = *sp--;\r
+            }\r
+         }\r
+         else if (row_info->bit_depth == 16)\r
+         {\r
+            png_byte red_high = (trans_value->red >> 8) & 0xff;\r
+            png_byte green_high = (trans_value->green >> 8) & 0xff;\r
+            png_byte blue_high = (trans_value->blue >> 8) & 0xff;\r
+            png_byte red_low = trans_value->red & 0xff;\r
+            png_byte green_low = trans_value->green & 0xff;\r
+            png_byte blue_low = trans_value->blue & 0xff;\r
+            sp = row + row_info->rowbytes - 1;\r
+            dp = row + (png_size_t)(row_width << 3) - 1;\r
+            for (i = 0; i < row_width; i++)\r
+            {\r
+               if (*(sp - 5) == red_high &&\r
+                  *(sp - 4) == red_low &&\r
+                  *(sp - 3) == green_high &&\r
+                  *(sp - 2) == green_low &&\r
+                  *(sp - 1) == blue_high &&\r
+                  *(sp    ) == blue_low)\r
+               {\r
+                  *dp-- = 0;\r
+                  *dp-- = 0;\r
+               }\r
+               else\r
+               {\r
+                  *dp-- = 0xff;\r
+                  *dp-- = 0xff;\r
+               }\r
+               *dp-- = *sp--;\r
+               *dp-- = *sp--;\r
+               *dp-- = *sp--;\r
+               *dp-- = *sp--;\r
+               *dp-- = *sp--;\r
+               *dp-- = *sp--;\r
+            }\r
+         }\r
+         row_info->color_type = PNG_COLOR_TYPE_RGB_ALPHA;\r
+         row_info->channels = 4;\r
+         row_info->pixel_depth = (png_byte)(row_info->bit_depth << 2);\r
+         row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);\r
+      }\r
+   }\r
+}\r
+#endif\r
+\r
+#ifdef PNG_READ_QUANTIZE_SUPPORTED\r
+void /* PRIVATE */\r
+png_do_quantize(png_row_infop row_info, png_bytep row,\r
+    png_bytep palette_lookup, png_bytep quantize_lookup)\r
+{\r
+   png_bytep sp, dp;\r
+   png_uint_32 i;\r
+   png_uint_32 row_width=row_info->width;\r
+\r
+   png_debug(1, "in png_do_quantize");\r
+\r
+   {\r
+      if (row_info->color_type == PNG_COLOR_TYPE_RGB &&\r
+         palette_lookup && row_info->bit_depth == 8)\r
+      {\r
+         int r, g, b, p;\r
+         sp = row;\r
+         dp = row;\r
+         for (i = 0; i < row_width; i++)\r
+         {\r
+            r = *sp++;\r
+            g = *sp++;\r
+            b = *sp++;\r
+\r
+            /* This looks real messy, but the compiler will reduce\r
+             * it down to a reasonable formula.  For example, with\r
+             * 5 bits per color, we get:\r
+             * p = (((r >> 3) & 0x1f) << 10) |\r
+             *    (((g >> 3) & 0x1f) << 5) |\r
+             *    ((b >> 3) & 0x1f);\r
+             */\r
+            p = (((r >> (8 - PNG_QUANTIZE_RED_BITS)) &\r
+               ((1 << PNG_QUANTIZE_RED_BITS) - 1)) <<\r
+               (PNG_QUANTIZE_GREEN_BITS + PNG_QUANTIZE_BLUE_BITS)) |\r
+               (((g >> (8 - PNG_QUANTIZE_GREEN_BITS)) &\r
+               ((1 << PNG_QUANTIZE_GREEN_BITS) - 1)) <<\r
+               (PNG_QUANTIZE_BLUE_BITS)) |\r
+               ((b >> (8 - PNG_QUANTIZE_BLUE_BITS)) &\r
+               ((1 << PNG_QUANTIZE_BLUE_BITS) - 1));\r
+\r
+            *dp++ = palette_lookup[p];\r
+         }\r
+         row_info->color_type = PNG_COLOR_TYPE_PALETTE;\r
+         row_info->channels = 1;\r
+         row_info->pixel_depth = row_info->bit_depth;\r
+         row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);\r
+      }\r
+      else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA &&\r
+         palette_lookup != NULL && row_info->bit_depth == 8)\r
+      {\r
+         int r, g, b, p;\r
+         sp = row;\r
+         dp = row;\r
+         for (i = 0; i < row_width; i++)\r
+         {\r
+            r = *sp++;\r
+            g = *sp++;\r
+            b = *sp++;\r
+            sp++;\r
+\r
+            p = (((r >> (8 - PNG_QUANTIZE_RED_BITS)) &\r
+               ((1 << PNG_QUANTIZE_RED_BITS) - 1)) <<\r
+               (PNG_QUANTIZE_GREEN_BITS + PNG_QUANTIZE_BLUE_BITS)) |\r
+               (((g >> (8 - PNG_QUANTIZE_GREEN_BITS)) &\r
+               ((1 << PNG_QUANTIZE_GREEN_BITS) - 1)) <<\r
+               (PNG_QUANTIZE_BLUE_BITS)) |\r
+               ((b >> (8 - PNG_QUANTIZE_BLUE_BITS)) &\r
+               ((1 << PNG_QUANTIZE_BLUE_BITS) - 1));\r
+\r
+            *dp++ = palette_lookup[p];\r
+         }\r
+         row_info->color_type = PNG_COLOR_TYPE_PALETTE;\r
+         row_info->channels = 1;\r
+         row_info->pixel_depth = row_info->bit_depth;\r
+         row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);\r
+      }\r
+      else if (row_info->color_type == PNG_COLOR_TYPE_PALETTE &&\r
+         quantize_lookup && row_info->bit_depth == 8)\r
+      {\r
+         sp = row;\r
+         for (i = 0; i < row_width; i++, sp++)\r
+         {\r
+            *sp = quantize_lookup[*sp];\r
+         }\r
+      }\r
+   }\r
+}\r
+#endif\r
+\r
+#ifdef PNG_FLOATING_POINT_SUPPORTED\r
+#ifdef PNG_READ_GAMMA_SUPPORTED\r
+static PNG_CONST int png_gamma_shift[] =\r
+   {0x10, 0x21, 0x42, 0x84, 0x110, 0x248, 0x550, 0xff0, 0x00};\r
+\r
+/* We build the 8- or 16-bit gamma tables here.  Note that for 16-bit\r
+ * tables, we don't make a full table if we are reducing to 8-bit in\r
+ * the future.  Note also how the gamma_16 tables are segmented so that\r
+ * we don't need to allocate > 64K chunks for a full 16-bit table.\r
+ *\r
+ * See the PNG extensions document for an integer algorithm for creating\r
+ * the gamma tables.  Maybe we will implement that here someday.\r
+ *\r
+ * We should only reach this point if\r
+ *\r
+ *      the file_gamma is known (i.e., the gAMA or sRGB chunk is present,\r
+ *      or the application has provided a file_gamma)\r
+ *\r
+ *   AND\r
+ *      {\r
+ *         the screen_gamma is known\r
+ *\r
+ *      OR\r
+ *\r
+ *         RGB_to_gray transformation is being performed\r
+ *      }\r
+ *\r
+ *   AND\r
+ *      {\r
+ *         the screen_gamma is different from the reciprocal of the\r
+ *         file_gamma by more than the specified threshold\r
+ *\r
+ *      OR\r
+ *\r
+ *         a background color has been specified and the file_gamma\r
+ *         and screen_gamma are not 1.0, within the specified threshold.\r
+ *      }\r
+ */\r
+\r
+void /* PRIVATE */\r
+png_build_gamma_table(png_structp png_ptr, png_byte bit_depth)\r
+{\r
+  png_debug(1, "in png_build_gamma_table");\r
+\r
+  if (bit_depth <= 8)\r
+  {\r
+     int i;\r
+     double g;\r
+\r
+     if (png_ptr->screen_gamma > .000001)\r
+        g = 1.0 / (png_ptr->gamma * png_ptr->screen_gamma);\r
+\r
+     else\r
+        g = 1.0;\r
+\r
+     png_ptr->gamma_table = (png_bytep)png_malloc(png_ptr,\r
+        (png_uint_32)256);\r
+\r
+     for (i = 0; i < 256; i++)\r
+     {\r
+        png_ptr->gamma_table[i] = (png_byte)(pow((double)i / 255.0,\r
+           g) * 255.0 + .5);\r
+     }\r
+\r
+#if defined(PNG_READ_BACKGROUND_SUPPORTED) || \\r
+   defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)\r
+     if (png_ptr->transformations & ((PNG_BACKGROUND) | PNG_RGB_TO_GRAY))\r
+     {\r
+\r
+        g = 1.0 / (png_ptr->gamma);\r
+\r
+        png_ptr->gamma_to_1 = (png_bytep)png_malloc(png_ptr,\r
+           (png_uint_32)256);\r
+\r
+        for (i = 0; i < 256; i++)\r
+        {\r
+           png_ptr->gamma_to_1[i] = (png_byte)(pow((double)i / 255.0,\r
+              g) * 255.0 + .5);\r
+        }\r
+\r
+\r
+        png_ptr->gamma_from_1 = (png_bytep)png_malloc(png_ptr,\r
+           (png_uint_32)256);\r
+\r
+        if (png_ptr->screen_gamma > 0.000001)\r
+           g = 1.0 / png_ptr->screen_gamma;\r
+\r
+        else\r
+           g = png_ptr->gamma;   /* Probably doing rgb_to_gray */\r
+\r
+        for (i = 0; i < 256; i++)\r
+        {\r
+           png_ptr->gamma_from_1[i] = (png_byte)(pow((double)i / 255.0,\r
+              g) * 255.0 + .5);\r
+\r
+        }\r
+     }\r
+#endif /* PNG_READ_BACKGROUND_SUPPORTED || PNG_RGB_TO_GRAY_SUPPORTED */\r
+  }\r
+  else\r
+  {\r
+     double g;\r
+     int i, j, shift, num;\r
+     int sig_bit;\r
+     png_uint_32 ig;\r
+\r
+     if (png_ptr->color_type & PNG_COLOR_MASK_COLOR)\r
+     {\r
+        sig_bit = (int)png_ptr->sig_bit.red;\r
+\r
+        if ((int)png_ptr->sig_bit.green > sig_bit)\r
+           sig_bit = png_ptr->sig_bit.green;\r
+\r
+        if ((int)png_ptr->sig_bit.blue > sig_bit)\r
+           sig_bit = png_ptr->sig_bit.blue;\r
+     }\r
+     else\r
+     {\r
+        sig_bit = (int)png_ptr->sig_bit.gray;\r
+     }\r
+\r
+     if (sig_bit > 0)\r
+        shift = 16 - sig_bit;\r
+\r
+     else\r
+        shift = 0;\r
+\r
+     if (png_ptr->transformations & PNG_16_TO_8)\r
+     {\r
+        if (shift < (16 - PNG_MAX_GAMMA_8))\r
+           shift = (16 - PNG_MAX_GAMMA_8);\r
+     }\r
+\r
+     if (shift > 8)\r
+        shift = 8;\r
+\r
+     if (shift < 0)\r
+        shift = 0;\r
+\r
+     png_ptr->gamma_shift = (png_byte)shift;\r
+\r
+     num = (1 << (8 - shift));\r
+\r
+     if (png_ptr->screen_gamma > .000001)\r
+        g = 1.0 / (png_ptr->gamma * png_ptr->screen_gamma);\r
+     else\r
+        g = 1.0;\r
+\r
+     png_ptr->gamma_16_table = (png_uint_16pp)png_calloc(png_ptr,\r
+        (png_uint_32)(num * png_sizeof(png_uint_16p)));\r
+\r
+     if (png_ptr->transformations & (PNG_16_TO_8 | PNG_BACKGROUND))\r
+     {\r
+        double fin, fout;\r
+        png_uint_32 last, max;\r
+\r
+        for (i = 0; i < num; i++)\r
+        {\r
+           png_ptr->gamma_16_table[i] = (png_uint_16p)png_malloc(png_ptr,\r
+              (png_uint_32)(256 * png_sizeof(png_uint_16)));\r
+        }\r
+\r
+        g = 1.0 / g;\r
+        last = 0;\r
+        for (i = 0; i < 256; i++)\r
+        {\r
+           fout = ((double)i + 0.5) / 256.0;\r
+           fin = pow(fout, g);\r
+           max = (png_uint_32)(fin * (double)((png_uint_32)num << 8));\r
+           while (last <= max)\r
+           {\r
+              png_ptr->gamma_16_table[(int)(last & (0xff >> shift))]\r
+                 [(int)(last >> (8 - shift))] = (png_uint_16)(\r
+                 (png_uint_16)i | ((png_uint_16)i << 8));\r
+              last++;\r
+           }\r
+        }\r
+        while (last < ((png_uint_32)num << 8))\r
+        {\r
+           png_ptr->gamma_16_table[(int)(last & (0xff >> shift))]\r
+              [(int)(last >> (8 - shift))] = (png_uint_16)65535L;\r
+           last++;\r
+        }\r
+     }\r
+     else\r
+     {\r
+        for (i = 0; i < num; i++)\r
+        {\r
+           png_ptr->gamma_16_table[i] = (png_uint_16p)png_malloc(png_ptr,\r
+              (png_uint_32)(256 * png_sizeof(png_uint_16)));\r
+\r
+           ig = (((png_uint_32)i * (png_uint_32)png_gamma_shift[shift]) >> 4);\r
+\r
+           for (j = 0; j < 256; j++)\r
+           {\r
+              png_ptr->gamma_16_table[i][j] =\r
+                 (png_uint_16)(pow((double)(ig + ((png_uint_32)j << 8)) /\r
+                    65535.0, g) * 65535.0 + .5);\r
+           }\r
+        }\r
+     }\r
+\r
+#if defined(PNG_READ_BACKGROUND_SUPPORTED) || \\r
+   defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)\r
+     if (png_ptr->transformations & (PNG_BACKGROUND | PNG_RGB_TO_GRAY))\r
+     {\r
+\r
+        g = 1.0 / (png_ptr->gamma);\r
+\r
+        png_ptr->gamma_16_to_1 = (png_uint_16pp)png_calloc(png_ptr,\r
+           (png_uint_32)(num * png_sizeof(png_uint_16p )));\r
+\r
+        for (i = 0; i < num; i++)\r
+        {\r
+           png_ptr->gamma_16_to_1[i] = (png_uint_16p)png_malloc(png_ptr,\r
+              (png_uint_32)(256 * png_sizeof(png_uint_16)));\r
+\r
+           ig = (((png_uint_32)i *\r
+              (png_uint_32)png_gamma_shift[shift]) >> 4);\r
+           for (j = 0; j < 256; j++)\r
+           {\r
+              png_ptr->gamma_16_to_1[i][j] =\r
+                 (png_uint_16)(pow((double)(ig + ((png_uint_32)j << 8)) /\r
+                    65535.0, g) * 65535.0 + .5);\r
+           }\r
+        }\r
+\r
+        if (png_ptr->screen_gamma > 0.000001)\r
+           g = 1.0 / png_ptr->screen_gamma;\r
+\r
+        else\r
+           g = png_ptr->gamma;   /* Probably doing rgb_to_gray */\r
+\r
+        png_ptr->gamma_16_from_1 = (png_uint_16pp)png_calloc(png_ptr,\r
+           (png_uint_32)(num * png_sizeof(png_uint_16p)));\r
+\r
+        for (i = 0; i < num; i++)\r
+        {\r
+           png_ptr->gamma_16_from_1[i] = (png_uint_16p)png_malloc(png_ptr,\r
+              (png_uint_32)(256 * png_sizeof(png_uint_16)));\r
+\r
+           ig = (((png_uint_32)i *\r
+              (png_uint_32)png_gamma_shift[shift]) >> 4);\r
+\r
+           for (j = 0; j < 256; j++)\r
+           {\r
+              png_ptr->gamma_16_from_1[i][j] =\r
+                 (png_uint_16)(pow((double)(ig + ((png_uint_32)j << 8)) /\r
+                    65535.0, g) * 65535.0 + .5);\r
+           }\r
+        }\r
+     }\r
+#endif /* PNG_READ_BACKGROUND_SUPPORTED || PNG_RGB_TO_GRAY_SUPPORTED */\r
+  }\r
+}\r
+#endif\r
+/* To do: install integer version of png_build_gamma_table here */\r
+#endif\r
+\r
+#ifdef PNG_MNG_FEATURES_SUPPORTED\r
+/* Undoes intrapixel differencing  */\r
+void /* PRIVATE */\r
+png_do_read_intrapixel(png_row_infop row_info, png_bytep row)\r
+{\r
+   png_debug(1, "in png_do_read_intrapixel");\r
+\r
+   if (\r
+       (row_info->color_type & PNG_COLOR_MASK_COLOR))\r
+   {\r
+      int bytes_per_pixel;\r
+      png_uint_32 row_width = row_info->width;\r
+      if (row_info->bit_depth == 8)\r
+      {\r
+         png_bytep rp;\r
+         png_uint_32 i;\r
+\r
+         if (row_info->color_type == PNG_COLOR_TYPE_RGB)\r
+            bytes_per_pixel = 3;\r
+\r
+         else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)\r
+            bytes_per_pixel = 4;\r
+\r
+         else\r
+            return;\r
+\r
+         for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)\r
+         {\r
+            *(rp) = (png_byte)((256 + *rp + *(rp+1))&0xff);\r
+            *(rp+2) = (png_byte)((256 + *(rp+2) + *(rp+1))&0xff);\r
+         }\r
+      }\r
+      else if (row_info->bit_depth == 16)\r
+      {\r
+         png_bytep rp;\r
+         png_uint_32 i;\r
+\r
+         if (row_info->color_type == PNG_COLOR_TYPE_RGB)\r
+            bytes_per_pixel = 6;\r
+\r
+         else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)\r
+            bytes_per_pixel = 8;\r
+\r
+         else\r
+            return;\r
+\r
+         for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)\r
+         {\r
+            png_uint_32 s0   = (*(rp    ) << 8) | *(rp + 1);\r
+            png_uint_32 s1   = (*(rp + 2) << 8) | *(rp + 3);\r
+            png_uint_32 s2   = (*(rp + 4) << 8) | *(rp + 5);\r
+            png_uint_32 red  = (png_uint_32)((s0 + s1 + 65536L) & 0xffffL);\r
+            png_uint_32 blue = (png_uint_32)((s2 + s1 + 65536L) & 0xffffL);\r
+            *(rp  ) = (png_byte)((red >> 8) & 0xff);\r
+            *(rp+1) = (png_byte)(red & 0xff);\r
+            *(rp+4) = (png_byte)((blue >> 8) & 0xff);\r
+            *(rp+5) = (png_byte)(blue & 0xff);\r
+         }\r
+      }\r
+   }\r
+}\r
+#endif /* PNG_MNG_FEATURES_SUPPORTED */\r
+#endif /* PNG_READ_SUPPORTED */\r
index 531cb05..db5ec0c 100644 (file)
-
-/* pngrutil.c - utilities to read a PNG file
- *
- * Last changed in libpng 1.2.27 [April 29, 2008]
- * For conditions of distribution and use, see copyright notice in png.h
- * Copyright (c) 1998-2008 Glenn Randers-Pehrson
- * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
- * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
- *
- * This file contains routines that are only called from within
- * libpng itself during the course of reading an image.
- */
-
-#define PNG_INTERNAL
-#include "png.h"
-
-#if defined(PNG_READ_SUPPORTED)
-
-#if defined(_WIN32_WCE) && (_WIN32_WCE<0x500)
-#  define WIN32_WCE_OLD
-#endif
-
-#ifdef PNG_FLOATING_POINT_SUPPORTED
-#  if defined(WIN32_WCE_OLD)
-/* strtod() function is not supported on WindowsCE */
-__inline double png_strtod(png_structp png_ptr, PNG_CONST char *nptr, char **endptr)
-{
-   double result = 0;
-   int len;
-   wchar_t *str, *end;
-
-   len = MultiByteToWideChar(CP_ACP, 0, nptr, -1, NULL, 0);
-   str = (wchar_t *)png_malloc(png_ptr, len * sizeof(wchar_t));
-   if ( NULL != str )
-   {
-      MultiByteToWideChar(CP_ACP, 0, nptr, -1, str, len);
-      result = wcstod(str, &end);
-      len = WideCharToMultiByte(CP_ACP, 0, end, -1, NULL, 0, NULL, NULL);
-      *endptr = (char *)nptr + (png_strlen(nptr) - len + 1);
-      png_free(png_ptr, str);
-   }
-   return result;
-}
-#  else
-#    define png_strtod(p,a,b) strtod(a,b)
-#  endif
-#endif
-
-png_uint_32 PNGAPI
-png_get_uint_31(png_structp png_ptr, png_bytep buf)
-{
-   png_uint_32 i = png_get_uint_32(buf);
-   if (i > PNG_UINT_31_MAX)
-     png_error(png_ptr, "PNG unsigned integer out of range.");
-   return (i);
-}
-#ifndef PNG_READ_BIG_ENDIAN_SUPPORTED
-/* Grab an unsigned 32-bit integer from a buffer in big-endian format. */
-png_uint_32 PNGAPI
-png_get_uint_32(png_bytep buf)
-{
-   png_uint_32 i = ((png_uint_32)(*buf) << 24) +
-      ((png_uint_32)(*(buf + 1)) << 16) +
-      ((png_uint_32)(*(buf + 2)) << 8) +
-      (png_uint_32)(*(buf + 3));
-
-   return (i);
-}
-
-/* Grab a signed 32-bit integer from a buffer in big-endian format.  The
- * data is stored in the PNG file in two's complement format, and it is
- * assumed that the machine format for signed integers is the same. */
-png_int_32 PNGAPI
-png_get_int_32(png_bytep buf)
-{
-   png_int_32 i = ((png_int_32)(*buf) << 24) +
-      ((png_int_32)(*(buf + 1)) << 16) +
-      ((png_int_32)(*(buf + 2)) << 8) +
-      (png_int_32)(*(buf + 3));
-
-   return (i);
-}
-
-/* Grab an unsigned 16-bit integer from a buffer in big-endian format. */
-png_uint_16 PNGAPI
-png_get_uint_16(png_bytep buf)
-{
-   png_uint_16 i = (png_uint_16)(((png_uint_16)(*buf) << 8) +
-      (png_uint_16)(*(buf + 1)));
-
-   return (i);
-}
-#endif /* PNG_READ_BIG_ENDIAN_SUPPORTED */
-
-/* Read data, and (optionally) run it through the CRC. */
-void /* PRIVATE */
-png_crc_read(png_structp png_ptr, png_bytep buf, png_size_t length)
-{
-   if(png_ptr == NULL) return;
-   png_read_data(png_ptr, buf, length);
-   png_calculate_crc(png_ptr, buf, length);
-}
-
-/* Optionally skip data and then check the CRC.  Depending on whether we
-   are reading a ancillary or critical chunk, and how the program has set
-   things up, we may calculate the CRC on the data and print a message.
-   Returns '1' if there was a CRC error, '0' otherwise. */
-int /* PRIVATE */
-png_crc_finish(png_structp png_ptr, png_uint_32 skip)
-{
-   png_size_t i;
-   png_size_t istop = png_ptr->zbuf_size;
-
-   for (i = (png_size_t)skip; i > istop; i -= istop)
-   {
-      png_crc_read(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
-   }
-   if (i)
-   {
-      png_crc_read(png_ptr, png_ptr->zbuf, i);
-   }
-
-   if (png_crc_error(png_ptr))
-   {
-      if (((png_ptr->chunk_name[0] & 0x20) &&                /* Ancillary */
-           !(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN)) ||
-          (!(png_ptr->chunk_name[0] & 0x20) &&             /* Critical  */
-          (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_USE)))
-      {
-         png_chunk_warning(png_ptr, "CRC error");
-      }
-      else
-      {
-         png_chunk_error(png_ptr, "CRC error");
-      }
-      return (1);
-   }
-
-   return (0);
-}
-
-/* Compare the CRC stored in the PNG file with that calculated by libpng from
-   the data it has read thus far. */
-int /* PRIVATE */
-png_crc_error(png_structp png_ptr)
-{
-   png_byte crc_bytes[4];
-   png_uint_32 crc;
-   int need_crc = 1;
-
-   if (png_ptr->chunk_name[0] & 0x20)                     /* ancillary */
-   {
-      if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_MASK) ==
-          (PNG_FLAG_CRC_ANCILLARY_USE | PNG_FLAG_CRC_ANCILLARY_NOWARN))
-         need_crc = 0;
-   }
-   else                                                    /* critical */
-   {
-      if (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_IGNORE)
-         need_crc = 0;
-   }
-
-   png_read_data(png_ptr, crc_bytes, 4);
-
-   if (need_crc)
-   {
-      crc = png_get_uint_32(crc_bytes);
-      return ((int)(crc != png_ptr->crc));
-   }
-   else
-      return (0);
-}
-
-#if defined(PNG_READ_zTXt_SUPPORTED) || defined(PNG_READ_iTXt_SUPPORTED) || \
-    defined(PNG_READ_iCCP_SUPPORTED)
-/*
- * Decompress trailing data in a chunk.  The assumption is that chunkdata
- * points at an allocated area holding the contents of a chunk with a
- * trailing compressed part.  What we get back is an allocated area
- * holding the original prefix part and an uncompressed version of the
- * trailing part (the malloc area passed in is freed).
- */
-png_charp /* PRIVATE */
-png_decompress_chunk(png_structp png_ptr, int comp_type,
-                              png_charp chunkdata, png_size_t chunklength,
-                              png_size_t prefix_size, png_size_t *newlength)
-{
-   static PNG_CONST char msg[] = "Error decoding compressed text";
-   png_charp text;
-   png_size_t text_size;
-
-   if (comp_type == PNG_COMPRESSION_TYPE_BASE)
-   {
-      int ret = Z_OK;
-      png_ptr->zstream.next_in = (png_bytep)(chunkdata + prefix_size);
-      png_ptr->zstream.avail_in = (uInt)(chunklength - prefix_size);
-      png_ptr->zstream.next_out = png_ptr->zbuf;
-      png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
-
-      text_size = 0;
-      text = NULL;
-
-      while (png_ptr->zstream.avail_in)
-      {
-         ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);
-         if (ret != Z_OK && ret != Z_STREAM_END)
-         {
-            if (png_ptr->zstream.msg != NULL)
-               png_warning(png_ptr, png_ptr->zstream.msg);
-            else
-               png_warning(png_ptr, msg);
-            inflateReset(&png_ptr->zstream);
-            png_ptr->zstream.avail_in = 0;
-
-            if (text ==  NULL)
-            {
-               text_size = prefix_size + png_sizeof(msg) + 1;
-               text = (png_charp)png_malloc_warn(png_ptr, text_size);
-               if (text ==  NULL)
-                 {
-                    png_free(png_ptr,chunkdata);
-                    png_error(png_ptr,"Not enough memory to decompress chunk");
-                 }
-               png_memcpy(text, chunkdata, prefix_size);
-            }
-
-            text[text_size - 1] = 0x00;
-
-            /* Copy what we can of the error message into the text chunk */
-            text_size = (png_size_t)(chunklength - (text - chunkdata) - 1);
-            text_size = png_sizeof(msg) > text_size ? text_size :
-               png_sizeof(msg);
-            png_memcpy(text + prefix_size, msg, text_size);
-            break;
-         }
-         if (!png_ptr->zstream.avail_out || ret == Z_STREAM_END)
-         {
-            if (text == NULL)
-            {
-               text_size = prefix_size +
-                   png_ptr->zbuf_size - png_ptr->zstream.avail_out;
-               text = (png_charp)png_malloc_warn(png_ptr, text_size + 1);
-               if (text ==  NULL)
-                 {
-                    png_free(png_ptr,chunkdata);
-                    png_error(png_ptr,"Not enough memory to decompress chunk.");
-                 }
-               png_memcpy(text + prefix_size, png_ptr->zbuf,
-                    text_size - prefix_size);
-               png_memcpy(text, chunkdata, prefix_size);
-               *(text + text_size) = 0x00;
-            }
-            else
-            {
-               png_charp tmp;
-
-               tmp = text;
-               text = (png_charp)png_malloc_warn(png_ptr,
-                  (png_uint_32)(text_size +
-                  png_ptr->zbuf_size - png_ptr->zstream.avail_out + 1));
-               if (text == NULL)
-               {
-                  png_free(png_ptr, tmp);
-                  png_free(png_ptr, chunkdata);
-                  png_error(png_ptr,"Not enough memory to decompress chunk..");
-               }
-               png_memcpy(text, tmp, text_size);
-               png_free(png_ptr, tmp);
-               png_memcpy(text + text_size, png_ptr->zbuf,
-                  (png_ptr->zbuf_size - png_ptr->zstream.avail_out));
-               text_size += png_ptr->zbuf_size - png_ptr->zstream.avail_out;
-               *(text + text_size) = 0x00;
-            }
-            if (ret == Z_STREAM_END)
-               break;
-            else
-            {
-               png_ptr->zstream.next_out = png_ptr->zbuf;
-               png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
-            }
-         }
-      }
-      if (ret != Z_STREAM_END)
-      {
-#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
-         char umsg[52];
-
-         if (ret == Z_BUF_ERROR)
-            png_snprintf(umsg, 52,
-                "Buffer error in compressed datastream in %s chunk",
-                png_ptr->chunk_name);
-         else if (ret == Z_DATA_ERROR)
-            png_snprintf(umsg, 52,
-                "Data error in compressed datastream in %s chunk",
-                png_ptr->chunk_name);
-         else
-            png_snprintf(umsg, 52,
-                "Incomplete compressed datastream in %s chunk",
-                png_ptr->chunk_name);
-         png_warning(png_ptr, umsg);
-#else
-         png_warning(png_ptr,
-            "Incomplete compressed datastream in chunk other than IDAT");
-#endif
-         text_size=prefix_size;
-         if (text ==  NULL)
-         {
-            text = (png_charp)png_malloc_warn(png_ptr, text_size+1);
-            if (text == NULL)
-              {
-                png_free(png_ptr, chunkdata);
-                png_error(png_ptr,"Not enough memory for text.");
-              }
-            png_memcpy(text, chunkdata, prefix_size);
-         }
-         *(text + text_size) = 0x00;
-      }
-
-      inflateReset(&png_ptr->zstream);
-      png_ptr->zstream.avail_in = 0;
-
-      png_free(png_ptr, chunkdata);
-      chunkdata = text;
-      *newlength=text_size;
-   }
-   else /* if (comp_type != PNG_COMPRESSION_TYPE_BASE) */
-   {
-#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
-      char umsg[50];
-
-      png_snprintf(umsg, 50,
-         "Unknown zTXt compression type %d", comp_type);
-      png_warning(png_ptr, umsg);
-#else
-      png_warning(png_ptr, "Unknown zTXt compression type");
-#endif
-
-      *(chunkdata + prefix_size) = 0x00;
-      *newlength=prefix_size;
-   }
-
-   return chunkdata;
-}
-#endif
-
-/* read and check the IDHR chunk */
-void /* PRIVATE */
-png_handle_IHDR(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
-{
-   png_byte buf[13];
-   png_uint_32 width, height;
-   int bit_depth, color_type, compression_type, filter_type;
-   int interlace_type;
-
-   png_debug(1, "in png_handle_IHDR\n");
-
-   if (png_ptr->mode & PNG_HAVE_IHDR)
-      png_error(png_ptr, "Out of place IHDR");
-
-   /* check the length */
-   if (length != 13)
-      png_error(png_ptr, "Invalid IHDR chunk");
-
-   png_ptr->mode |= PNG_HAVE_IHDR;
-
-   png_crc_read(png_ptr, buf, 13);
-   png_crc_finish(png_ptr, 0);
-
-   width = png_get_uint_31(png_ptr, buf);
-   height = png_get_uint_31(png_ptr, buf + 4);
-   bit_depth = buf[8];
-   color_type = buf[9];
-   compression_type = buf[10];
-   filter_type = buf[11];
-   interlace_type = buf[12];
-
-   /* set internal variables */
-   png_ptr->width = width;
-   png_ptr->height = height;
-   png_ptr->bit_depth = (png_byte)bit_depth;
-   png_ptr->interlaced = (png_byte)interlace_type;
-   png_ptr->color_type = (png_byte)color_type;
-#if defined(PNG_MNG_FEATURES_SUPPORTED)
-   png_ptr->filter_type = (png_byte)filter_type;
-#endif
-   png_ptr->compression_type = (png_byte)compression_type;
-
-   /* find number of channels */
-   switch (png_ptr->color_type)
-   {
-      case PNG_COLOR_TYPE_GRAY:
-      case PNG_COLOR_TYPE_PALETTE:
-         png_ptr->channels = 1;
-         break;
-      case PNG_COLOR_TYPE_RGB:
-         png_ptr->channels = 3;
-         break;
-      case PNG_COLOR_TYPE_GRAY_ALPHA:
-         png_ptr->channels = 2;
-         break;
-      case PNG_COLOR_TYPE_RGB_ALPHA:
-         png_ptr->channels = 4;
-         break;
-   }
-
-   /* set up other useful info */
-   png_ptr->pixel_depth = (png_byte)(png_ptr->bit_depth *
-   png_ptr->channels);
-   png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth,png_ptr->width);
-   png_debug1(3,"bit_depth = %d\n", png_ptr->bit_depth);
-   png_debug1(3,"channels = %d\n", png_ptr->channels);
-   png_debug1(3,"rowbytes = %lu\n", png_ptr->rowbytes);
-   png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth,
-      color_type, interlace_type, compression_type, filter_type);
-}
-
-/* read and check the palette */
-void /* PRIVATE */
-png_handle_PLTE(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
-{
-   png_color palette[PNG_MAX_PALETTE_LENGTH];
-   int num, i;
-#ifndef PNG_NO_POINTER_INDEXING
-   png_colorp pal_ptr;
-#endif
-
-   png_debug(1, "in png_handle_PLTE\n");
-
-   if (!(png_ptr->mode & PNG_HAVE_IHDR))
-      png_error(png_ptr, "Missing IHDR before PLTE");
-   else if (png_ptr->mode & PNG_HAVE_IDAT)
-   {
-      png_warning(png_ptr, "Invalid PLTE after IDAT");
-      png_crc_finish(png_ptr, length);
-      return;
-   }
-   else if (png_ptr->mode & PNG_HAVE_PLTE)
-      png_error(png_ptr, "Duplicate PLTE chunk");
-
-   png_ptr->mode |= PNG_HAVE_PLTE;
-
-   if (!(png_ptr->color_type&PNG_COLOR_MASK_COLOR))
-   {
-      png_warning(png_ptr,
-        "Ignoring PLTE chunk in grayscale PNG");
-      png_crc_finish(png_ptr, length);
-      return;
-   }
-#if !defined(PNG_READ_OPT_PLTE_SUPPORTED)
-   if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE)
-   {
-      png_crc_finish(png_ptr, length);
-      return;
-   }
-#endif
-
-   if (length > 3*PNG_MAX_PALETTE_LENGTH || length % 3)
-   {
-      if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE)
-      {
-         png_warning(png_ptr, "Invalid palette chunk");
-         png_crc_finish(png_ptr, length);
-         return;
-      }
-      else
-      {
-         png_error(png_ptr, "Invalid palette chunk");
-      }
-   }
-
-   num = (int)length / 3;
-
-#ifndef PNG_NO_POINTER_INDEXING
-   for (i = 0, pal_ptr = palette; i < num; i++, pal_ptr++)
-   {
-      png_byte buf[3];
-
-      png_crc_read(png_ptr, buf, 3);
-      pal_ptr->red = buf[0];
-      pal_ptr->green = buf[1];
-      pal_ptr->blue = buf[2];
-   }
-#else
-   for (i = 0; i < num; i++)
-   {
-      png_byte buf[3];
-
-      png_crc_read(png_ptr, buf, 3);
-      /* don't depend upon png_color being any order */
-      palette[i].red = buf[0];
-      palette[i].green = buf[1];
-      palette[i].blue = buf[2];
-   }
-#endif
-
-   /* If we actually NEED the PLTE chunk (ie for a paletted image), we do
-      whatever the normal CRC configuration tells us.  However, if we
-      have an RGB image, the PLTE can be considered ancillary, so
-      we will act as though it is. */
-#if !defined(PNG_READ_OPT_PLTE_SUPPORTED)
-   if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
-#endif
-   {
-      png_crc_finish(png_ptr, 0);
-   }
-#if !defined(PNG_READ_OPT_PLTE_SUPPORTED)
-   else if (png_crc_error(png_ptr))  /* Only if we have a CRC error */
-   {
-      /* If we don't want to use the data from an ancillary chunk,
-         we have two options: an error abort, or a warning and we
-         ignore the data in this chunk (which should be OK, since
-         it's considered ancillary for a RGB or RGBA image). */
-      if (!(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_USE))
-      {
-         if (png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN)
-         {
-            png_chunk_error(png_ptr, "CRC error");
-         }
-         else
-         {
-            png_chunk_warning(png_ptr, "CRC error");
-            return;
-         }
-      }
-      /* Otherwise, we (optionally) emit a warning and use the chunk. */
-      else if (!(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN))
-      {
-         png_chunk_warning(png_ptr, "CRC error");
-      }
-   }
-#endif
-
-   png_set_PLTE(png_ptr, info_ptr, palette, num);
-
-#if defined(PNG_READ_tRNS_SUPPORTED)
-   if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
-   {
-      if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS))
-      {
-         if (png_ptr->num_trans > (png_uint_16)num)
-         {
-            png_warning(png_ptr, "Truncating incorrect tRNS chunk length");
-            png_ptr->num_trans = (png_uint_16)num;
-         }
-         if (info_ptr->num_trans > (png_uint_16)num)
-         {
-            png_warning(png_ptr, "Truncating incorrect info tRNS chunk length");
-            info_ptr->num_trans = (png_uint_16)num;
-         }
-      }
-   }
-#endif
-
-}
-
-void /* PRIVATE */
-png_handle_IEND(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
-{
-   png_debug(1, "in png_handle_IEND\n");
-
-   if (!(png_ptr->mode & PNG_HAVE_IHDR) || !(png_ptr->mode & PNG_HAVE_IDAT))
-   {
-      png_error(png_ptr, "No image in file");
-   }
-
-   png_ptr->mode |= (PNG_AFTER_IDAT | PNG_HAVE_IEND);
-
-   if (length != 0)
-   {
-      png_warning(png_ptr, "Incorrect IEND chunk length");
-   }
-   png_crc_finish(png_ptr, length);
-
-   info_ptr =info_ptr; /* quiet compiler warnings about unused info_ptr */
-}
-
-#if defined(PNG_READ_gAMA_SUPPORTED)
-void /* PRIVATE */
-png_handle_gAMA(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
-{
-   png_fixed_point igamma;
-#ifdef PNG_FLOATING_POINT_SUPPORTED
-   float file_gamma;
-#endif
-   png_byte buf[4];
-
-   png_debug(1, "in png_handle_gAMA\n");
-
-   if (!(png_ptr->mode & PNG_HAVE_IHDR))
-      png_error(png_ptr, "Missing IHDR before gAMA");
-   else if (png_ptr->mode & PNG_HAVE_IDAT)
-   {
-      png_warning(png_ptr, "Invalid gAMA after IDAT");
-      png_crc_finish(png_ptr, length);
-      return;
-   }
-   else if (png_ptr->mode & PNG_HAVE_PLTE)
-      /* Should be an error, but we can cope with it */
-      png_warning(png_ptr, "Out of place gAMA chunk");
-
-   if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_gAMA)
-#if defined(PNG_READ_sRGB_SUPPORTED)
-      && !(info_ptr->valid & PNG_INFO_sRGB)
-#endif
-      )
-   {
-      png_warning(png_ptr, "Duplicate gAMA chunk");
-      png_crc_finish(png_ptr, length);
-      return;
-   }
-
-   if (length != 4)
-   {
-      png_warning(png_ptr, "Incorrect gAMA chunk length");
-      png_crc_finish(png_ptr, length);
-      return;
-   }
-
-   png_crc_read(png_ptr, buf, 4);
-   if (png_crc_finish(png_ptr, 0))
-      return;
-
-   igamma = (png_fixed_point)png_get_uint_32(buf);
-   /* check for zero gamma */
-   if (igamma == 0)
-      {
-         png_warning(png_ptr,
-           "Ignoring gAMA chunk with gamma=0");
-         return;
-      }
-
-#if defined(PNG_READ_sRGB_SUPPORTED)
-   if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sRGB))
-      if (PNG_OUT_OF_RANGE(igamma, 45500L, 500))
-      {
-         png_warning(png_ptr,
-           "Ignoring incorrect gAMA value when sRGB is also present");
-#ifndef PNG_NO_CONSOLE_IO
-         fprintf(stderr, "gamma = (%d/100000)\n", (int)igamma);
-#endif
-         return;
-      }
-#endif /* PNG_READ_sRGB_SUPPORTED */
-
-#ifdef PNG_FLOATING_POINT_SUPPORTED
-   file_gamma = (float)igamma / (float)100000.0;
-#  ifdef PNG_READ_GAMMA_SUPPORTED
-     png_ptr->gamma = file_gamma;
-#  endif
-     png_set_gAMA(png_ptr, info_ptr, file_gamma);
-#endif
-#ifdef PNG_FIXED_POINT_SUPPORTED
-   png_set_gAMA_fixed(png_ptr, info_ptr, igamma);
-#endif
-}
-#endif
-
-#if defined(PNG_READ_sBIT_SUPPORTED)
-void /* PRIVATE */
-png_handle_sBIT(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
-{
-   png_size_t truelen;
-   png_byte buf[4];
-
-   png_debug(1, "in png_handle_sBIT\n");
-
-   buf[0] = buf[1] = buf[2] = buf[3] = 0;
-
-   if (!(png_ptr->mode & PNG_HAVE_IHDR))
-      png_error(png_ptr, "Missing IHDR before sBIT");
-   else if (png_ptr->mode & PNG_HAVE_IDAT)
-   {
-      png_warning(png_ptr, "Invalid sBIT after IDAT");
-      png_crc_finish(png_ptr, length);
-      return;
-   }
-   else if (png_ptr->mode & PNG_HAVE_PLTE)
-   {
-      /* Should be an error, but we can cope with it */
-      png_warning(png_ptr, "Out of place sBIT chunk");
-   }
-   if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sBIT))
-   {
-      png_warning(png_ptr, "Duplicate sBIT chunk");
-      png_crc_finish(png_ptr, length);
-      return;
-   }
-
-   if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
-      truelen = 3;
-   else
-      truelen = (png_size_t)png_ptr->channels;
-
-   if (length != truelen || length > 4)
-   {
-      png_warning(png_ptr, "Incorrect sBIT chunk length");
-      png_crc_finish(png_ptr, length);
-      return;
-   }
-
-   png_crc_read(png_ptr, buf, truelen);
-   if (png_crc_finish(png_ptr, 0))
-      return;
-
-   if (png_ptr->color_type & PNG_COLOR_MASK_COLOR)
-   {
-      png_ptr->sig_bit.red = buf[0];
-      png_ptr->sig_bit.green = buf[1];
-      png_ptr->sig_bit.blue = buf[2];
-      png_ptr->sig_bit.alpha = buf[3];
-   }
-   else
-   {
-      png_ptr->sig_bit.gray = buf[0];
-      png_ptr->sig_bit.red = buf[0];
-      png_ptr->sig_bit.green = buf[0];
-      png_ptr->sig_bit.blue = buf[0];
-      png_ptr->sig_bit.alpha = buf[1];
-   }
-   png_set_sBIT(png_ptr, info_ptr, &(png_ptr->sig_bit));
-}
-#endif
-
-#if defined(PNG_READ_cHRM_SUPPORTED)
-void /* PRIVATE */
-png_handle_cHRM(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
-{
-   png_byte buf[4];
-#ifdef PNG_FLOATING_POINT_SUPPORTED
-   float white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y;
-#endif
-   png_fixed_point int_x_white, int_y_white, int_x_red, int_y_red, int_x_green,
-      int_y_green, int_x_blue, int_y_blue;
-
-   png_uint_32 uint_x, uint_y;
-
-   png_debug(1, "in png_handle_cHRM\n");
-
-   if (!(png_ptr->mode & PNG_HAVE_IHDR))
-      png_error(png_ptr, "Missing IHDR before cHRM");
-   else if (png_ptr->mode & PNG_HAVE_IDAT)
-   {
-      png_warning(png_ptr, "Invalid cHRM after IDAT");
-      png_crc_finish(png_ptr, length);
-      return;
-   }
-   else if (png_ptr->mode & PNG_HAVE_PLTE)
-      /* Should be an error, but we can cope with it */
-      png_warning(png_ptr, "Missing PLTE before cHRM");
-
-   if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_cHRM)
-#if defined(PNG_READ_sRGB_SUPPORTED)
-      && !(info_ptr->valid & PNG_INFO_sRGB)
-#endif
-      )
-   {
-      png_warning(png_ptr, "Duplicate cHRM chunk");
-      png_crc_finish(png_ptr, length);
-      return;
-   }
-
-   if (length != 32)
-   {
-      png_warning(png_ptr, "Incorrect cHRM chunk length");
-      png_crc_finish(png_ptr, length);
-      return;
-   }
-
-   png_crc_read(png_ptr, buf, 4);
-   uint_x = png_get_uint_32(buf);
-
-   png_crc_read(png_ptr, buf, 4);
-   uint_y = png_get_uint_32(buf);
-
-   if (uint_x > 80000L || uint_y > 80000L ||
-      uint_x + uint_y > 100000L)
-   {
-      png_warning(png_ptr, "Invalid cHRM white point");
-      png_crc_finish(png_ptr, 24);
-      return;
-   }
-   int_x_white = (png_fixed_point)uint_x;
-   int_y_white = (png_fixed_point)uint_y;
-
-   png_crc_read(png_ptr, buf, 4);
-   uint_x = png_get_uint_32(buf);
-
-   png_crc_read(png_ptr, buf, 4);
-   uint_y = png_get_uint_32(buf);
-
-   if (uint_x + uint_y > 100000L)
-   {
-      png_warning(png_ptr, "Invalid cHRM red point");
-      png_crc_finish(png_ptr, 16);
-      return;
-   }
-   int_x_red = (png_fixed_point)uint_x;
-   int_y_red = (png_fixed_point)uint_y;
-
-   png_crc_read(png_ptr, buf, 4);
-   uint_x = png_get_uint_32(buf);
-
-   png_crc_read(png_ptr, buf, 4);
-   uint_y = png_get_uint_32(buf);
-
-   if (uint_x + uint_y > 100000L)
-   {
-      png_warning(png_ptr, "Invalid cHRM green point");
-      png_crc_finish(png_ptr, 8);
-      return;
-   }
-   int_x_green = (png_fixed_point)uint_x;
-   int_y_green = (png_fixed_point)uint_y;
-
-   png_crc_read(png_ptr, buf, 4);
-   uint_x = png_get_uint_32(buf);
-
-   png_crc_read(png_ptr, buf, 4);
-   uint_y = png_get_uint_32(buf);
-
-   if (uint_x + uint_y > 100000L)
-   {
-      png_warning(png_ptr, "Invalid cHRM blue point");
-      png_crc_finish(png_ptr, 0);
-      return;
-   }
-   int_x_blue = (png_fixed_point)uint_x;
-   int_y_blue = (png_fixed_point)uint_y;
-
-#ifdef PNG_FLOATING_POINT_SUPPORTED
-   white_x = (float)int_x_white / (float)100000.0;
-   white_y = (float)int_y_white / (float)100000.0;
-   red_x   = (float)int_x_red   / (float)100000.0;
-   red_y   = (float)int_y_red   / (float)100000.0;
-   green_x = (float)int_x_green / (float)100000.0;
-   green_y = (float)int_y_green / (float)100000.0;
-   blue_x  = (float)int_x_blue  / (float)100000.0;
-   blue_y  = (float)int_y_blue  / (float)100000.0;
-#endif
-
-#if defined(PNG_READ_sRGB_SUPPORTED)
-   if ((info_ptr != NULL) && (info_ptr->valid & PNG_INFO_sRGB))
-      {
-      if (PNG_OUT_OF_RANGE(int_x_white, 31270,  1000) ||
-          PNG_OUT_OF_RANGE(int_y_white, 32900,  1000) ||
-          PNG_OUT_OF_RANGE(int_x_red,   64000L, 1000) ||
-          PNG_OUT_OF_RANGE(int_y_red,   33000,  1000) ||
-          PNG_OUT_OF_RANGE(int_x_green, 30000,  1000) ||
-          PNG_OUT_OF_RANGE(int_y_green, 60000L, 1000) ||
-          PNG_OUT_OF_RANGE(int_x_blue,  15000,  1000) ||
-          PNG_OUT_OF_RANGE(int_y_blue,   6000,  1000))
-         {
-            png_warning(png_ptr,
-              "Ignoring incorrect cHRM value when sRGB is also present");
-#ifndef PNG_NO_CONSOLE_IO
-#ifdef PNG_FLOATING_POINT_SUPPORTED
-            fprintf(stderr,"wx=%f, wy=%f, rx=%f, ry=%f\n",
-               white_x, white_y, red_x, red_y);
-            fprintf(stderr,"gx=%f, gy=%f, bx=%f, by=%f\n",
-               green_x, green_y, blue_x, blue_y);
-#else
-            fprintf(stderr,"wx=%ld, wy=%ld, rx=%ld, ry=%ld\n",
-               int_x_white, int_y_white, int_x_red, int_y_red);
-            fprintf(stderr,"gx=%ld, gy=%ld, bx=%ld, by=%ld\n",
-               int_x_green, int_y_green, int_x_blue, int_y_blue);
-#endif
-#endif /* PNG_NO_CONSOLE_IO */
-         }
-         png_crc_finish(png_ptr, 0);
-         return;
-      }
-#endif /* PNG_READ_sRGB_SUPPORTED */
-
-#ifdef PNG_FLOATING_POINT_SUPPORTED
-   png_set_cHRM(png_ptr, info_ptr,
-      white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y);
-#endif
-#ifdef PNG_FIXED_POINT_SUPPORTED
-   png_set_cHRM_fixed(png_ptr, info_ptr,
-      int_x_white, int_y_white, int_x_red, int_y_red, int_x_green,
-      int_y_green, int_x_blue, int_y_blue);
-#endif
-   if (png_crc_finish(png_ptr, 0))
-      return;
-}
-#endif
-
-#if defined(PNG_READ_sRGB_SUPPORTED)
-void /* PRIVATE */
-png_handle_sRGB(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
-{
-   int intent;
-   png_byte buf[1];
-
-   png_debug(1, "in png_handle_sRGB\n");
-
-   if (!(png_ptr->mode & PNG_HAVE_IHDR))
-      png_error(png_ptr, "Missing IHDR before sRGB");
-   else if (png_ptr->mode & PNG_HAVE_IDAT)
-   {
-      png_warning(png_ptr, "Invalid sRGB after IDAT");
-      png_crc_finish(png_ptr, length);
-      return;
-   }
-   else if (png_ptr->mode & PNG_HAVE_PLTE)
-      /* Should be an error, but we can cope with it */
-      png_warning(png_ptr, "Out of place sRGB chunk");
-
-   if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sRGB))
-   {
-      png_warning(png_ptr, "Duplicate sRGB chunk");
-      png_crc_finish(png_ptr, length);
-      return;
-   }
-
-   if (length != 1)
-   {
-      png_warning(png_ptr, "Incorrect sRGB chunk length");
-      png_crc_finish(png_ptr, length);
-      return;
-   }
-
-   png_crc_read(png_ptr, buf, 1);
-   if (png_crc_finish(png_ptr, 0))
-      return;
-
-   intent = buf[0];
-   /* check for bad intent */
-   if (intent >= PNG_sRGB_INTENT_LAST)
-   {
-      png_warning(png_ptr, "Unknown sRGB intent");
-      return;
-   }
-
-#if defined(PNG_READ_gAMA_SUPPORTED) && defined(PNG_READ_GAMMA_SUPPORTED)
-   if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_gAMA))
-   {
-   png_fixed_point igamma;
-#ifdef PNG_FIXED_POINT_SUPPORTED
-      igamma=info_ptr->int_gamma;
-#else
-#  ifdef PNG_FLOATING_POINT_SUPPORTED
-      igamma=(png_fixed_point)(info_ptr->gamma * 100000.);
-#  endif
-#endif
-      if (PNG_OUT_OF_RANGE(igamma, 45500L, 500))
-      {
-         png_warning(png_ptr,
-           "Ignoring incorrect gAMA value when sRGB is also present");
-#ifndef PNG_NO_CONSOLE_IO
-#  ifdef PNG_FIXED_POINT_SUPPORTED
-         fprintf(stderr,"incorrect gamma=(%d/100000)\n",(int)png_ptr->int_gamma);
-#  else
-#    ifdef PNG_FLOATING_POINT_SUPPORTED
-         fprintf(stderr,"incorrect gamma=%f\n",png_ptr->gamma);
-#    endif
-#  endif
-#endif
-      }
-   }
-#endif /* PNG_READ_gAMA_SUPPORTED */
-
-#ifdef PNG_READ_cHRM_SUPPORTED
-#ifdef PNG_FIXED_POINT_SUPPORTED
-   if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_cHRM))
-      if (PNG_OUT_OF_RANGE(info_ptr->int_x_white, 31270,  1000) ||
-          PNG_OUT_OF_RANGE(info_ptr->int_y_white, 32900,  1000) ||
-          PNG_OUT_OF_RANGE(info_ptr->int_x_red,   64000L, 1000) ||
-          PNG_OUT_OF_RANGE(info_ptr->int_y_red,   33000,  1000) ||
-          PNG_OUT_OF_RANGE(info_ptr->int_x_green, 30000,  1000) ||
-          PNG_OUT_OF_RANGE(info_ptr->int_y_green, 60000L, 1000) ||
-          PNG_OUT_OF_RANGE(info_ptr->int_x_blue,  15000,  1000) ||
-          PNG_OUT_OF_RANGE(info_ptr->int_y_blue,   6000,  1000))
-         {
-            png_warning(png_ptr,
-              "Ignoring incorrect cHRM value when sRGB is also present");
-         }
-#endif /* PNG_FIXED_POINT_SUPPORTED */
-#endif /* PNG_READ_cHRM_SUPPORTED */
-
-   png_set_sRGB_gAMA_and_cHRM(png_ptr, info_ptr, intent);
-}
-#endif /* PNG_READ_sRGB_SUPPORTED */
-
-#if defined(PNG_READ_iCCP_SUPPORTED)
-void /* PRIVATE */
-png_handle_iCCP(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
-/* Note: this does not properly handle chunks that are > 64K under DOS */
-{
-   png_charp chunkdata;
-   png_byte compression_type;
-   png_bytep pC;
-   png_charp profile;
-   png_uint_32 skip = 0;
-   png_uint_32 profile_size, profile_length;
-   png_size_t slength, prefix_length, data_length;
-
-   png_debug(1, "in png_handle_iCCP\n");
-
-   if (!(png_ptr->mode & PNG_HAVE_IHDR))
-      png_error(png_ptr, "Missing IHDR before iCCP");
-   else if (png_ptr->mode & PNG_HAVE_IDAT)
-   {
-      png_warning(png_ptr, "Invalid iCCP after IDAT");
-      png_crc_finish(png_ptr, length);
-      return;
-   }
-   else if (png_ptr->mode & PNG_HAVE_PLTE)
-      /* Should be an error, but we can cope with it */
-      png_warning(png_ptr, "Out of place iCCP chunk");
-
-   if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_iCCP))
-   {
-      png_warning(png_ptr, "Duplicate iCCP chunk");
-      png_crc_finish(png_ptr, length);
-      return;
-   }
-
-#ifdef PNG_MAX_MALLOC_64K
-   if (length > (png_uint_32)65535L)
-   {
-      png_warning(png_ptr, "iCCP chunk too large to fit in memory");
-      skip = length - (png_uint_32)65535L;
-      length = (png_uint_32)65535L;
-   }
-#endif
-
-   chunkdata = (png_charp)png_malloc(png_ptr, length + 1);
-   slength = (png_size_t)length;
-   png_crc_read(png_ptr, (png_bytep)chunkdata, slength);
-
-   if (png_crc_finish(png_ptr, skip))
-   {
-      png_free(png_ptr, chunkdata);
-      return;
-   }
-
-   chunkdata[slength] = 0x00;
-
-   for (profile = chunkdata; *profile; profile++)
-      /* empty loop to find end of name */ ;
-
-   ++profile;
-
-   /* there should be at least one zero (the compression type byte)
-      following the separator, and we should be on it  */
-   if ( profile >= chunkdata + slength - 1)
-   {
-      png_free(png_ptr, chunkdata);
-      png_warning(png_ptr, "Malformed iCCP chunk");
-      return;
-   }
-
-   /* compression_type should always be zero */
-   compression_type = *profile++;
-   if (compression_type)
-   {
-      png_warning(png_ptr, "Ignoring nonzero compression type in iCCP chunk");
-      compression_type=0x00;  /* Reset it to zero (libpng-1.0.6 through 1.0.8
-                                 wrote nonzero) */
-   }
-
-   prefix_length = profile - chunkdata;
-   chunkdata = png_decompress_chunk(png_ptr, compression_type, chunkdata,
-                                    slength, prefix_length, &data_length);
-
-   profile_length = data_length - prefix_length;
-
-   if ( prefix_length > data_length || profile_length < 4)
-   {
-      png_free(png_ptr, chunkdata);
-      png_warning(png_ptr, "Profile size field missing from iCCP chunk");
-      return;
-   }
-
-   /* Check the profile_size recorded in the first 32 bits of the ICC profile */
-   pC = (png_bytep)(chunkdata+prefix_length);
-   profile_size = ((*(pC  ))<<24) |
-                  ((*(pC+1))<<16) |
-                  ((*(pC+2))<< 8) |
-                  ((*(pC+3))    );
-
-   if(profile_size < profile_length)
-      profile_length = profile_size;
-
-   if(profile_size > profile_length)
-   {
-      png_free(png_ptr, chunkdata);
-      png_warning(png_ptr, "Ignoring truncated iCCP profile.");
-      return;
-   }
-
-   png_set_iCCP(png_ptr, info_ptr, chunkdata, compression_type,
-                chunkdata + prefix_length, profile_length);
-   png_free(png_ptr, chunkdata);
-}
-#endif /* PNG_READ_iCCP_SUPPORTED */
-
-#if defined(PNG_READ_sPLT_SUPPORTED)
-void /* PRIVATE */
-png_handle_sPLT(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
-/* Note: this does not properly handle chunks that are > 64K under DOS */
-{
-   png_bytep chunkdata;
-   png_bytep entry_start;
-   png_sPLT_t new_palette;
-#ifdef PNG_NO_POINTER_INDEXING
-   png_sPLT_entryp pp;
-#endif
-   int data_length, entry_size, i;
-   png_uint_32 skip = 0;
-   png_size_t slength;
-
-   png_debug(1, "in png_handle_sPLT\n");
-
-   if (!(png_ptr->mode & PNG_HAVE_IHDR))
-      png_error(png_ptr, "Missing IHDR before sPLT");
-   else if (png_ptr->mode & PNG_HAVE_IDAT)
-   {
-      png_warning(png_ptr, "Invalid sPLT after IDAT");
-      png_crc_finish(png_ptr, length);
-      return;
-   }
-
-#ifdef PNG_MAX_MALLOC_64K
-   if (length > (png_uint_32)65535L)
-   {
-      png_warning(png_ptr, "sPLT chunk too large to fit in memory");
-      skip = length - (png_uint_32)65535L;
-      length = (png_uint_32)65535L;
-   }
-#endif
-
-   chunkdata = (png_bytep)png_malloc(png_ptr, length + 1);
-   slength = (png_size_t)length;
-   png_crc_read(png_ptr, (png_bytep)chunkdata, slength);
-
-   if (png_crc_finish(png_ptr, skip))
-   {
-      png_free(png_ptr, chunkdata);
-      return;
-   }
-
-   chunkdata[slength] = 0x00;
-
-   for (entry_start = chunkdata; *entry_start; entry_start++)
-      /* empty loop to find end of name */ ;
-   ++entry_start;
-
-   /* a sample depth should follow the separator, and we should be on it  */
-   if (entry_start > chunkdata + slength - 2)
-   {
-      png_free(png_ptr, chunkdata);
-      png_warning(png_ptr, "malformed sPLT chunk");
-      return;
-   }
-
-   new_palette.depth = *entry_start++;
-   entry_size = (new_palette.depth == 8 ? 6 : 10);
-   data_length = (slength - (entry_start - chunkdata));
-
-   /* integrity-check the data length */
-   if (data_length % entry_size)
-   {
-      png_free(png_ptr, chunkdata);
-      png_warning(png_ptr, "sPLT chunk has bad length");
-      return;
-   }
-
-   new_palette.nentries = (png_int_32) ( data_length / entry_size);
-   if ((png_uint_32) new_palette.nentries > (png_uint_32) (PNG_SIZE_MAX /
-       png_sizeof(png_sPLT_entry)))
-   {
-       png_warning(png_ptr, "sPLT chunk too long");
-       return;
-   }
-   new_palette.entries = (png_sPLT_entryp)png_malloc_warn(
-       png_ptr, new_palette.nentries * png_sizeof(png_sPLT_entry));
-   if (new_palette.entries == NULL)
-   {
-       png_warning(png_ptr, "sPLT chunk requires too much memory");
-       return;
-   }
-
-#ifndef PNG_NO_POINTER_INDEXING
-   for (i = 0; i < new_palette.nentries; i++)
-   {
-      png_sPLT_entryp pp = new_palette.entries + i;
-
-      if (new_palette.depth == 8)
-      {
-          pp->red = *entry_start++;
-          pp->green = *entry_start++;
-          pp->blue = *entry_start++;
-          pp->alpha = *entry_start++;
-      }
-      else
-      {
-          pp->red   = png_get_uint_16(entry_start); entry_start += 2;
-          pp->green = png_get_uint_16(entry_start); entry_start += 2;
-          pp->blue  = png_get_uint_16(entry_start); entry_start += 2;
-          pp->alpha = png_get_uint_16(entry_start); entry_start += 2;
-      }
-      pp->frequency = png_get_uint_16(entry_start); entry_start += 2;
-   }
-#else
-   pp = new_palette.entries;
-   for (i = 0; i < new_palette.nentries; i++)
-   {
-
-      if (new_palette.depth == 8)
-      {
-          pp[i].red   = *entry_start++;
-          pp[i].green = *entry_start++;
-          pp[i].blue  = *entry_start++;
-          pp[i].alpha = *entry_start++;
-      }
-      else
-      {
-          pp[i].red   = png_get_uint_16(entry_start); entry_start += 2;
-          pp[i].green = png_get_uint_16(entry_start); entry_start += 2;
-          pp[i].blue  = png_get_uint_16(entry_start); entry_start += 2;
-          pp[i].alpha = png_get_uint_16(entry_start); entry_start += 2;
-      }
-      pp->frequency = png_get_uint_16(entry_start); entry_start += 2;
-   }
-#endif
-
-   /* discard all chunk data except the name and stash that */
-   new_palette.name = (png_charp)chunkdata;
-
-   png_set_sPLT(png_ptr, info_ptr, &new_palette, 1);
-
-   png_free(png_ptr, chunkdata);
-   png_free(png_ptr, new_palette.entries);
-}
-#endif /* PNG_READ_sPLT_SUPPORTED */
-
-#if defined(PNG_READ_tRNS_SUPPORTED)
-void /* PRIVATE */
-png_handle_tRNS(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
-{
-   png_byte readbuf[PNG_MAX_PALETTE_LENGTH];
-
-   png_debug(1, "in png_handle_tRNS\n");
-
-   if (!(png_ptr->mode & PNG_HAVE_IHDR))
-      png_error(png_ptr, "Missing IHDR before tRNS");
-   else if (png_ptr->mode & PNG_HAVE_IDAT)
-   {
-      png_warning(png_ptr, "Invalid tRNS after IDAT");
-      png_crc_finish(png_ptr, length);
-      return;
-   }
-   else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS))
-   {
-      png_warning(png_ptr, "Duplicate tRNS chunk");
-      png_crc_finish(png_ptr, length);
-      return;
-   }
-
-   if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
-   {
-      png_byte buf[2];
-
-      if (length != 2)
-      {
-         png_warning(png_ptr, "Incorrect tRNS chunk length");
-         png_crc_finish(png_ptr, length);
-         return;
-      }
-
-      png_crc_read(png_ptr, buf, 2);
-      png_ptr->num_trans = 1;
-      png_ptr->trans_values.gray = png_get_uint_16(buf);
-   }
-   else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
-   {
-      png_byte buf[6];
-
-      if (length != 6)
-      {
-         png_warning(png_ptr, "Incorrect tRNS chunk length");
-         png_crc_finish(png_ptr, length);
-         return;
-      }
-      png_crc_read(png_ptr, buf, (png_size_t)length);
-      png_ptr->num_trans = 1;
-      png_ptr->trans_values.red = png_get_uint_16(buf);
-      png_ptr->trans_values.green = png_get_uint_16(buf + 2);
-      png_ptr->trans_values.blue = png_get_uint_16(buf + 4);
-   }
-   else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
-   {
-      if (!(png_ptr->mode & PNG_HAVE_PLTE))
-      {
-         /* Should be an error, but we can cope with it. */
-         png_warning(png_ptr, "Missing PLTE before tRNS");
-      }
-      if (length > (png_uint_32)png_ptr->num_palette ||
-          length > PNG_MAX_PALETTE_LENGTH)
-      {
-         png_warning(png_ptr, "Incorrect tRNS chunk length");
-         png_crc_finish(png_ptr, length);
-         return;
-      }
-      if (length == 0)
-      {
-         png_warning(png_ptr, "Zero length tRNS chunk");
-         png_crc_finish(png_ptr, length);
-         return;
-      }
-      png_crc_read(png_ptr, readbuf, (png_size_t)length);
-      png_ptr->num_trans = (png_uint_16)length;
-   }
-   else
-   {
-      png_warning(png_ptr, "tRNS chunk not allowed with alpha channel");
-      png_crc_finish(png_ptr, length);
-      return;
-   }
-
-   if (png_crc_finish(png_ptr, 0))
-   {
-      png_ptr->num_trans = 0;
-      return;
-   }
-
-   png_set_tRNS(png_ptr, info_ptr, readbuf, png_ptr->num_trans,
-      &(png_ptr->trans_values));
-}
-#endif
-
-#if defined(PNG_READ_bKGD_SUPPORTED)
-void /* PRIVATE */
-png_handle_bKGD(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
-{
-   png_size_t truelen;
-   png_byte buf[6];
-
-   png_debug(1, "in png_handle_bKGD\n");
-
-   if (!(png_ptr->mode & PNG_HAVE_IHDR))
-      png_error(png_ptr, "Missing IHDR before bKGD");
-   else if (png_ptr->mode & PNG_HAVE_IDAT)
-   {
-      png_warning(png_ptr, "Invalid bKGD after IDAT");
-      png_crc_finish(png_ptr, length);
-      return;
-   }
-   else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
-            !(png_ptr->mode & PNG_HAVE_PLTE))
-   {
-      png_warning(png_ptr, "Missing PLTE before bKGD");
-      png_crc_finish(png_ptr, length);
-      return;
-   }
-   else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_bKGD))
-   {
-      png_warning(png_ptr, "Duplicate bKGD chunk");
-      png_crc_finish(png_ptr, length);
-      return;
-   }
-
-   if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
-      truelen = 1;
-   else if (png_ptr->color_type & PNG_COLOR_MASK_COLOR)
-      truelen = 6;
-   else
-      truelen = 2;
-
-   if (length != truelen)
-   {
-      png_warning(png_ptr, "Incorrect bKGD chunk length");
-      png_crc_finish(png_ptr, length);
-      return;
-   }
-
-   png_crc_read(png_ptr, buf, truelen);
-   if (png_crc_finish(png_ptr, 0))
-      return;
-
-   /* We convert the index value into RGB components so that we can allow
-    * arbitrary RGB values for background when we have transparency, and
-    * so it is easy to determine the RGB values of the background color
-    * from the info_ptr struct. */
-   if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
-   {
-      png_ptr->background.index = buf[0];
-      if (info_ptr && info_ptr->num_palette)
-      {
-          if(buf[0] > info_ptr->num_palette)
-          {
-             png_warning(png_ptr, "Incorrect bKGD chunk index value");
-             return;
-          }
-          png_ptr->background.red =
-             (png_uint_16)png_ptr->palette[buf[0]].red;
-          png_ptr->background.green =
-             (png_uint_16)png_ptr->palette[buf[0]].green;
-          png_ptr->background.blue =
-             (png_uint_16)png_ptr->palette[buf[0]].blue;
-      }
-   }
-   else if (!(png_ptr->color_type & PNG_COLOR_MASK_COLOR)) /* GRAY */
-   {
-      png_ptr->background.red =
-      png_ptr->background.green =
-      png_ptr->background.blue =
-      png_ptr->background.gray = png_get_uint_16(buf);
-   }
-   else
-   {
-      png_ptr->background.red = png_get_uint_16(buf);
-      png_ptr->background.green = png_get_uint_16(buf + 2);
-      png_ptr->background.blue = png_get_uint_16(buf + 4);
-   }
-
-   png_set_bKGD(png_ptr, info_ptr, &(png_ptr->background));
-}
-#endif
-
-#if defined(PNG_READ_hIST_SUPPORTED)
-void /* PRIVATE */
-png_handle_hIST(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
-{
-   unsigned int num, i;
-   png_uint_16 readbuf[PNG_MAX_PALETTE_LENGTH];
-
-   png_debug(1, "in png_handle_hIST\n");
-
-   if (!(png_ptr->mode & PNG_HAVE_IHDR))
-      png_error(png_ptr, "Missing IHDR before hIST");
-   else if (png_ptr->mode & PNG_HAVE_IDAT)
-   {
-      png_warning(png_ptr, "Invalid hIST after IDAT");
-      png_crc_finish(png_ptr, length);
-      return;
-   }
-   else if (!(png_ptr->mode & PNG_HAVE_PLTE))
-   {
-      png_warning(png_ptr, "Missing PLTE before hIST");
-      png_crc_finish(png_ptr, length);
-      return;
-   }
-   else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_hIST))
-   {
-      png_warning(png_ptr, "Duplicate hIST chunk");
-      png_crc_finish(png_ptr, length);
-      return;
-   }
-
-   num = length / 2 ;
-   if (num != (unsigned int) png_ptr->num_palette || num >
-      (unsigned int) PNG_MAX_PALETTE_LENGTH)
-   {
-      png_warning(png_ptr, "Incorrect hIST chunk length");
-      png_crc_finish(png_ptr, length);
-      return;
-   }
-
-   for (i = 0; i < num; i++)
-   {
-      png_byte buf[2];
-
-      png_crc_read(png_ptr, buf, 2);
-      readbuf[i] = png_get_uint_16(buf);
-   }
-
-   if (png_crc_finish(png_ptr, 0))
-      return;
-
-   png_set_hIST(png_ptr, info_ptr, readbuf);
-}
-#endif
-
-#if defined(PNG_READ_pHYs_SUPPORTED)
-void /* PRIVATE */
-png_handle_pHYs(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
-{
-   png_byte buf[9];
-   png_uint_32 res_x, res_y;
-   int unit_type;
-
-   png_debug(1, "in png_handle_pHYs\n");
-
-   if (!(png_ptr->mode & PNG_HAVE_IHDR))
-      png_error(png_ptr, "Missing IHDR before pHYs");
-   else if (png_ptr->mode & PNG_HAVE_IDAT)
-   {
-      png_warning(png_ptr, "Invalid pHYs after IDAT");
-      png_crc_finish(png_ptr, length);
-      return;
-   }
-   else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pHYs))
-   {
-      png_warning(png_ptr, "Duplicate pHYs chunk");
-      png_crc_finish(png_ptr, length);
-      return;
-   }
-
-   if (length != 9)
-   {
-      png_warning(png_ptr, "Incorrect pHYs chunk length");
-      png_crc_finish(png_ptr, length);
-      return;
-   }
-
-   png_crc_read(png_ptr, buf, 9);
-   if (png_crc_finish(png_ptr, 0))
-      return;
-
-   res_x = png_get_uint_32(buf);
-   res_y = png_get_uint_32(buf + 4);
-   unit_type = buf[8];
-   png_set_pHYs(png_ptr, info_ptr, res_x, res_y, unit_type);
-}
-#endif
-
-#if defined(PNG_READ_oFFs_SUPPORTED)
-void /* PRIVATE */
-png_handle_oFFs(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
-{
-   png_byte buf[9];
-   png_int_32 offset_x, offset_y;
-   int unit_type;
-
-   png_debug(1, "in png_handle_oFFs\n");
-
-   if (!(png_ptr->mode & PNG_HAVE_IHDR))
-      png_error(png_ptr, "Missing IHDR before oFFs");
-   else if (png_ptr->mode & PNG_HAVE_IDAT)
-   {
-      png_warning(png_ptr, "Invalid oFFs after IDAT");
-      png_crc_finish(png_ptr, length);
-      return;
-   }
-   else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_oFFs))
-   {
-      png_warning(png_ptr, "Duplicate oFFs chunk");
-      png_crc_finish(png_ptr, length);
-      return;
-   }
-
-   if (length != 9)
-   {
-      png_warning(png_ptr, "Incorrect oFFs chunk length");
-      png_crc_finish(png_ptr, length);
-      return;
-   }
-
-   png_crc_read(png_ptr, buf, 9);
-   if (png_crc_finish(png_ptr, 0))
-      return;
-
-   offset_x = png_get_int_32(buf);
-   offset_y = png_get_int_32(buf + 4);
-   unit_type = buf[8];
-   png_set_oFFs(png_ptr, info_ptr, offset_x, offset_y, unit_type);
-}
-#endif
-
-#if defined(PNG_READ_pCAL_SUPPORTED)
-/* read the pCAL chunk (described in the PNG Extensions document) */
-void /* PRIVATE */
-png_handle_pCAL(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
-{
-   png_charp purpose;
-   png_int_32 X0, X1;
-   png_byte type, nparams;
-   png_charp buf, units, endptr;
-   png_charpp params;
-   png_size_t slength;
-   int i;
-
-   png_debug(1, "in png_handle_pCAL\n");
-
-   if (!(png_ptr->mode & PNG_HAVE_IHDR))
-      png_error(png_ptr, "Missing IHDR before pCAL");
-   else if (png_ptr->mode & PNG_HAVE_IDAT)
-   {
-      png_warning(png_ptr, "Invalid pCAL after IDAT");
-      png_crc_finish(png_ptr, length);
-      return;
-   }
-   else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pCAL))
-   {
-      png_warning(png_ptr, "Duplicate pCAL chunk");
-      png_crc_finish(png_ptr, length);
-      return;
-   }
-
-   png_debug1(2, "Allocating and reading pCAL chunk data (%lu bytes)\n",
-      length + 1);
-   purpose = (png_charp)png_malloc_warn(png_ptr, length + 1);
-   if (purpose == NULL)
-     {
-       png_warning(png_ptr, "No memory for pCAL purpose.");
-       return;
-     }
-   slength = (png_size_t)length;
-   png_crc_read(png_ptr, (png_bytep)purpose, slength);
-
-   if (png_crc_finish(png_ptr, 0))
-   {
-      png_free(png_ptr, purpose);
-      return;
-   }
-
-   purpose[slength] = 0x00; /* null terminate the last string */
-
-   png_debug(3, "Finding end of pCAL purpose string\n");
-   for (buf = purpose; *buf; buf++)
-      /* empty loop */ ;
-
-   endptr = purpose + slength;
-
-   /* We need to have at least 12 bytes after the purpose string
-      in order to get the parameter information. */
-   if (endptr <= buf + 12)
-   {
-      png_warning(png_ptr, "Invalid pCAL data");
-      png_free(png_ptr, purpose);
-      return;
-   }
-
-   png_debug(3, "Reading pCAL X0, X1, type, nparams, and units\n");
-   X0 = png_get_int_32((png_bytep)buf+1);
-   X1 = png_get_int_32((png_bytep)buf+5);
-   type = buf[9];
-   nparams = buf[10];
-   units = buf + 11;
-
-   png_debug(3, "Checking pCAL equation type and number of parameters\n");
-   /* Check that we have the right number of parameters for known
-      equation types. */
-   if ((type == PNG_EQUATION_LINEAR && nparams != 2) ||
-       (type == PNG_EQUATION_BASE_E && nparams != 3) ||
-       (type == PNG_EQUATION_ARBITRARY && nparams != 3) ||
-       (type == PNG_EQUATION_HYPERBOLIC && nparams != 4))
-   {
-      png_warning(png_ptr, "Invalid pCAL parameters for equation type");
-      png_free(png_ptr, purpose);
-      return;
-   }
-   else if (type >= PNG_EQUATION_LAST)
-   {
-      png_warning(png_ptr, "Unrecognized equation type for pCAL chunk");
-   }
-
-   for (buf = units; *buf; buf++)
-      /* Empty loop to move past the units string. */ ;
-
-   png_debug(3, "Allocating pCAL parameters array\n");
-   params = (png_charpp)png_malloc_warn(png_ptr, (png_uint_32)(nparams
-      *png_sizeof(png_charp))) ;
-   if (params == NULL)
-     {
-       png_free(png_ptr, purpose);
-       png_warning(png_ptr, "No memory for pCAL params.");
-       return;
-     }
-
-   /* Get pointers to the start of each parameter string. */
-   for (i = 0; i < (int)nparams; i++)
-   {
-      buf++; /* Skip the null string terminator from previous parameter. */
-
-      png_debug1(3, "Reading pCAL parameter %d\n", i);
-      for (params[i] = buf; buf <= endptr && *buf != 0x00; buf++)
-         /* Empty loop to move past each parameter string */ ;
-
-      /* Make sure we haven't run out of data yet */
-      if (buf > endptr)
-      {
-         png_warning(png_ptr, "Invalid pCAL data");
-         png_free(png_ptr, purpose);
-         png_free(png_ptr, params);
-         return;
-      }
-   }
-
-   png_set_pCAL(png_ptr, info_ptr, purpose, X0, X1, type, nparams,
-      units, params);
-
-   png_free(png_ptr, purpose);
-   png_free(png_ptr, params);
-}
-#endif
-
-#if defined(PNG_READ_sCAL_SUPPORTED)
-/* read the sCAL chunk */
-void /* PRIVATE */
-png_handle_sCAL(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
-{
-   png_charp buffer, ep;
-#ifdef PNG_FLOATING_POINT_SUPPORTED
-   double width, height;
-   png_charp vp;
-#else
-#ifdef PNG_FIXED_POINT_SUPPORTED
-   png_charp swidth, sheight;
-#endif
-#endif
-   png_size_t slength;
-
-   png_debug(1, "in png_handle_sCAL\n");
-
-   if (!(png_ptr->mode & PNG_HAVE_IHDR))
-      png_error(png_ptr, "Missing IHDR before sCAL");
-   else if (png_ptr->mode & PNG_HAVE_IDAT)
-   {
-      png_warning(png_ptr, "Invalid sCAL after IDAT");
-      png_crc_finish(png_ptr, length);
-      return;
-   }
-   else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sCAL))
-   {
-      png_warning(png_ptr, "Duplicate sCAL chunk");
-      png_crc_finish(png_ptr, length);
-      return;
-   }
-
-   png_debug1(2, "Allocating and reading sCAL chunk data (%lu bytes)\n",
-      length + 1);
-   buffer = (png_charp)png_malloc_warn(png_ptr, length + 1);
-   if (buffer == NULL)
-     {
-       png_warning(png_ptr, "Out of memory while processing sCAL chunk");
-       return;
-     }
-   slength = (png_size_t)length;
-   png_crc_read(png_ptr, (png_bytep)buffer, slength);
-
-   if (png_crc_finish(png_ptr, 0))
-   {
-      png_free(png_ptr, buffer);
-      return;
-   }
-
-   buffer[slength] = 0x00; /* null terminate the last string */
-
-   ep = buffer + 1;        /* skip unit byte */
-
-#ifdef PNG_FLOATING_POINT_SUPPORTED
-   width = png_strtod(png_ptr, ep, &vp);
-   if (*vp)
-   {
-       png_warning(png_ptr, "malformed width string in sCAL chunk");
-       return;
-   }
-#else
-#ifdef PNG_FIXED_POINT_SUPPORTED
-   swidth = (png_charp)png_malloc_warn(png_ptr, png_strlen(ep) + 1);
-   if (swidth == NULL)
-     {
-       png_warning(png_ptr, "Out of memory while processing sCAL chunk width");
-       return;
-     }
-   png_memcpy(swidth, ep, (png_size_t)png_strlen(ep));
-#endif
-#endif
-
-   for (ep = buffer; *ep; ep++)
-      /* empty loop */ ;
-   ep++;
-
-   if (buffer + slength < ep)
-   {
-       png_warning(png_ptr, "Truncated sCAL chunk");
-#if defined(PNG_FIXED_POINT_SUPPORTED) && \
-    !defined(PNG_FLOATING_POINT_SUPPORTED)
-       png_free(png_ptr, swidth);
-#endif
-      png_free(png_ptr, buffer);
-       return;
-   }
-
-#ifdef PNG_FLOATING_POINT_SUPPORTED
-   height = png_strtod(png_ptr, ep, &vp);
-   if (*vp)
-   {
-       png_warning(png_ptr, "malformed height string in sCAL chunk");
-       return;
-   }
-#else
-#ifdef PNG_FIXED_POINT_SUPPORTED
-   sheight = (png_charp)png_malloc_warn(png_ptr, png_strlen(ep) + 1);
-   if (sheight == NULL)
-     {
-       png_warning(png_ptr, "Out of memory while processing sCAL chunk height");
-       return;
-     }
-   png_memcpy(sheight, ep, (png_size_t)png_strlen(ep));
-#endif
-#endif
-
-   if (buffer + slength < ep
-#ifdef PNG_FLOATING_POINT_SUPPORTED
-      || width <= 0. || height <= 0.
-#endif
-      )
-   {
-      png_warning(png_ptr, "Invalid sCAL data");
-      png_free(png_ptr, buffer);
-#if defined(PNG_FIXED_POINT_SUPPORTED) && !defined(PNG_FLOATING_POINT_SUPPORTED)
-      png_free(png_ptr, swidth);
-      png_free(png_ptr, sheight);
-#endif
-      return;
-   }
-
-
-#ifdef PNG_FLOATING_POINT_SUPPORTED
-   png_set_sCAL(png_ptr, info_ptr, buffer[0], width, height);
-#else
-#ifdef PNG_FIXED_POINT_SUPPORTED
-   png_set_sCAL_s(png_ptr, info_ptr, buffer[0], swidth, sheight);
-#endif
-#endif
-
-   png_free(png_ptr, buffer);
-#if defined(PNG_FIXED_POINT_SUPPORTED) && !defined(PNG_FLOATING_POINT_SUPPORTED)
-   png_free(png_ptr, swidth);
-   png_free(png_ptr, sheight);
-#endif
-}
-#endif
-
-#if defined(PNG_READ_tIME_SUPPORTED)
-void /* PRIVATE */
-png_handle_tIME(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
-{
-   png_byte buf[7];
-   png_time mod_time;
-
-   png_debug(1, "in png_handle_tIME\n");
-
-   if (!(png_ptr->mode & PNG_HAVE_IHDR))
-      png_error(png_ptr, "Out of place tIME chunk");
-   else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tIME))
-   {
-      png_warning(png_ptr, "Duplicate tIME chunk");
-      png_crc_finish(png_ptr, length);
-      return;
-   }
-
-   if (png_ptr->mode & PNG_HAVE_IDAT)
-      png_ptr->mode |= PNG_AFTER_IDAT;
-
-   if (length != 7)
-   {
-      png_warning(png_ptr, "Incorrect tIME chunk length");
-      png_crc_finish(png_ptr, length);
-      return;
-   }
-
-   png_crc_read(png_ptr, buf, 7);
-   if (png_crc_finish(png_ptr, 0))
-      return;
-
-   mod_time.second = buf[6];
-   mod_time.minute = buf[5];
-   mod_time.hour = buf[4];
-   mod_time.day = buf[3];
-   mod_time.month = buf[2];
-   mod_time.year = png_get_uint_16(buf);
-
-   png_set_tIME(png_ptr, info_ptr, &mod_time);
-}
-#endif
-
-#if defined(PNG_READ_tEXt_SUPPORTED)
-/* Note: this does not properly handle chunks that are > 64K under DOS */
-void /* PRIVATE */
-png_handle_tEXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
-{
-   png_textp text_ptr;
-   png_charp key;
-   png_charp text;
-   png_uint_32 skip = 0;
-   png_size_t slength;
-   int ret;
-
-   png_debug(1, "in png_handle_tEXt\n");
-
-   if (!(png_ptr->mode & PNG_HAVE_IHDR))
-      png_error(png_ptr, "Missing IHDR before tEXt");
-
-   if (png_ptr->mode & PNG_HAVE_IDAT)
-      png_ptr->mode |= PNG_AFTER_IDAT;
-
-#ifdef PNG_MAX_MALLOC_64K
-   if (length > (png_uint_32)65535L)
-   {
-      png_warning(png_ptr, "tEXt chunk too large to fit in memory");
-      skip = length - (png_uint_32)65535L;
-      length = (png_uint_32)65535L;
-   }
-#endif
-
-   key = (png_charp)png_malloc_warn(png_ptr, length + 1);
-   if (key == NULL)
-   {
-     png_warning(png_ptr, "No memory to process text chunk.");
-     return;
-   }
-   slength = (png_size_t)length;
-   png_crc_read(png_ptr, (png_bytep)key, slength);
-
-   if (png_crc_finish(png_ptr, skip))
-   {
-      png_free(png_ptr, key);
-      return;
-   }
-
-   key[slength] = 0x00;
-
-   for (text = key; *text; text++)
-      /* empty loop to find end of key */ ;
-
-   if (text != key + slength)
-      text++;
-
-   text_ptr = (png_textp)png_malloc_warn(png_ptr,
-      (png_uint_32)png_sizeof(png_text));
-   if (text_ptr == NULL)
-   {
-     png_warning(png_ptr, "Not enough memory to process text chunk.");
-     png_free(png_ptr, key);
-     return;
-   }
-   text_ptr->compression = PNG_TEXT_COMPRESSION_NONE;
-   text_ptr->key = key;
-#ifdef PNG_iTXt_SUPPORTED
-   text_ptr->lang = NULL;
-   text_ptr->lang_key = NULL;
-   text_ptr->itxt_length = 0;
-#endif
-   text_ptr->text = text;
-   text_ptr->text_length = png_strlen(text);
-
-   ret=png_set_text_2(png_ptr, info_ptr, text_ptr, 1);
-
-   png_free(png_ptr, key);
-   png_free(png_ptr, text_ptr);
-   if (ret)
-     png_warning(png_ptr, "Insufficient memory to process text chunk.");
-}
-#endif
-
-#if defined(PNG_READ_zTXt_SUPPORTED)
-/* note: this does not correctly handle chunks that are > 64K under DOS */
-void /* PRIVATE */
-png_handle_zTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
-{
-   png_textp text_ptr;
-   png_charp chunkdata;
-   png_charp text;
-   int comp_type;
-   int ret;
-   png_size_t slength, prefix_len, data_len;
-
-   png_debug(1, "in png_handle_zTXt\n");
-   if (!(png_ptr->mode & PNG_HAVE_IHDR))
-      png_error(png_ptr, "Missing IHDR before zTXt");
-
-   if (png_ptr->mode & PNG_HAVE_IDAT)
-      png_ptr->mode |= PNG_AFTER_IDAT;
-
-#ifdef PNG_MAX_MALLOC_64K
-   /* We will no doubt have problems with chunks even half this size, but
-      there is no hard and fast rule to tell us where to stop. */
-   if (length > (png_uint_32)65535L)
-   {
-     png_warning(png_ptr,"zTXt chunk too large to fit in memory");
-     png_crc_finish(png_ptr, length);
-     return;
-   }
-#endif
-
-   chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1);
-   if (chunkdata == NULL)
-   {
-     png_warning(png_ptr,"Out of memory processing zTXt chunk.");
-     return;
-   }
-   slength = (png_size_t)length;
-   png_crc_read(png_ptr, (png_bytep)chunkdata, slength);
-   if (png_crc_finish(png_ptr, 0))
-   {
-      png_free(png_ptr, chunkdata);
-      return;
-   }
-
-   chunkdata[slength] = 0x00;
-
-   for (text = chunkdata; *text; text++)
-      /* empty loop */ ;
-
-   /* zTXt must have some text after the chunkdataword */
-   if (text >= chunkdata + slength - 2)
-   {
-      png_warning(png_ptr, "Truncated zTXt chunk");
-      png_free(png_ptr, chunkdata);
-      return;
-   }
-   else
-   {
-       comp_type = *(++text);
-       if (comp_type != PNG_TEXT_COMPRESSION_zTXt)
-       {
-          png_warning(png_ptr, "Unknown compression type in zTXt chunk");
-          comp_type = PNG_TEXT_COMPRESSION_zTXt;
-       }
-       text++;        /* skip the compression_method byte */
-   }
-   prefix_len = text - chunkdata;
-
-   chunkdata = (png_charp)png_decompress_chunk(png_ptr, comp_type, chunkdata,
-                                    (png_size_t)length, prefix_len, &data_len);
-
-   text_ptr = (png_textp)png_malloc_warn(png_ptr,
-     (png_uint_32)png_sizeof(png_text));
-   if (text_ptr == NULL)
-   {
-     png_warning(png_ptr,"Not enough memory to process zTXt chunk.");
-     png_free(png_ptr, chunkdata);
-     return;
-   }
-   text_ptr->compression = comp_type;
-   text_ptr->key = chunkdata;
-#ifdef PNG_iTXt_SUPPORTED
-   text_ptr->lang = NULL;
-   text_ptr->lang_key = NULL;
-   text_ptr->itxt_length = 0;
-#endif
-   text_ptr->text = chunkdata + prefix_len;
-   text_ptr->text_length = data_len;
-
-   ret=png_set_text_2(png_ptr, info_ptr, text_ptr, 1);
-
-   png_free(png_ptr, text_ptr);
-   png_free(png_ptr, chunkdata);
-   if (ret)
-     png_error(png_ptr, "Insufficient memory to store zTXt chunk.");
-}
-#endif
-
-#if defined(PNG_READ_iTXt_SUPPORTED)
-/* note: this does not correctly handle chunks that are > 64K under DOS */
-void /* PRIVATE */
-png_handle_iTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
-{
-   png_textp text_ptr;
-   png_charp chunkdata;
-   png_charp key, lang, text, lang_key;
-   int comp_flag;
-   int comp_type = 0;
-   int ret;
-   png_size_t slength, prefix_len, data_len;
-
-   png_debug(1, "in png_handle_iTXt\n");
-
-   if (!(png_ptr->mode & PNG_HAVE_IHDR))
-      png_error(png_ptr, "Missing IHDR before iTXt");
-
-   if (png_ptr->mode & PNG_HAVE_IDAT)
-      png_ptr->mode |= PNG_AFTER_IDAT;
-
-#ifdef PNG_MAX_MALLOC_64K
-   /* We will no doubt have problems with chunks even half this size, but
-      there is no hard and fast rule to tell us where to stop. */
-   if (length > (png_uint_32)65535L)
-   {
-     png_warning(png_ptr,"iTXt chunk too large to fit in memory");
-     png_crc_finish(png_ptr, length);
-     return;
-   }
-#endif
-
-   chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1);
-   if (chunkdata == NULL)
-   {
-     png_warning(png_ptr, "No memory to process iTXt chunk.");
-     return;
-   }
-   slength = (png_size_t)length;
-   png_crc_read(png_ptr, (png_bytep)chunkdata, slength);
-   if (png_crc_finish(png_ptr, 0))
-   {
-      png_free(png_ptr, chunkdata);
-      return;
-   }
-
-   chunkdata[slength] = 0x00;
-
-   for (lang = chunkdata; *lang; lang++)
-      /* empty loop */ ;
-   lang++;        /* skip NUL separator */
-
-   /* iTXt must have a language tag (possibly empty), two compression bytes,
-      translated keyword (possibly empty), and possibly some text after the
-      keyword */
-
-   if (lang >= chunkdata + slength - 3)
-   {
-      png_warning(png_ptr, "Truncated iTXt chunk");
-      png_free(png_ptr, chunkdata);
-      return;
-   }
-   else
-   {
-       comp_flag = *lang++;
-       comp_type = *lang++;
-   }
-
-   for (lang_key = lang; *lang_key; lang_key++)
-      /* empty loop */ ;
-   lang_key++;        /* skip NUL separator */
-
-   if (lang_key >= chunkdata + slength)
-   {
-      png_warning(png_ptr, "Truncated iTXt chunk");
-      png_free(png_ptr, chunkdata);
-      return;
-   }
-
-   for (text = lang_key; *text; text++)
-      /* empty loop */ ;
-   text++;        /* skip NUL separator */
-   if (text >= chunkdata + slength)
-   {
-      png_warning(png_ptr, "Malformed iTXt chunk");
-      png_free(png_ptr, chunkdata);
-      return;
-   }
-
-   prefix_len = text - chunkdata;
-
-   key=chunkdata;
-   if (comp_flag)
-       chunkdata = png_decompress_chunk(png_ptr, comp_type, chunkdata,
-          (size_t)length, prefix_len, &data_len);
-   else
-       data_len=png_strlen(chunkdata + prefix_len);
-   text_ptr = (png_textp)png_malloc_warn(png_ptr,
-      (png_uint_32)png_sizeof(png_text));
-   if (text_ptr == NULL)
-   {
-     png_warning(png_ptr,"Not enough memory to process iTXt chunk.");
-     png_free(png_ptr, chunkdata);
-     return;
-   }
-   text_ptr->compression = (int)comp_flag + 1;
-   text_ptr->lang_key = chunkdata+(lang_key-key);
-   text_ptr->lang = chunkdata+(lang-key);
-   text_ptr->itxt_length = data_len;
-   text_ptr->text_length = 0;
-   text_ptr->key = chunkdata;
-   text_ptr->text = chunkdata + prefix_len;
-
-   ret=png_set_text_2(png_ptr, info_ptr, text_ptr, 1);
-
-   png_free(png_ptr, text_ptr);
-   png_free(png_ptr, chunkdata);
-   if (ret)
-     png_error(png_ptr, "Insufficient memory to store iTXt chunk.");
-}
-#endif
-
-/* This function is called when we haven't found a handler for a
-   chunk.  If there isn't a problem with the chunk itself (ie bad
-   chunk name, CRC, or a critical chunk), the chunk is silently ignored
-   -- unless the PNG_FLAG_UNKNOWN_CHUNKS_SUPPORTED flag is on in which
-   case it will be saved away to be written out later. */
-void /* PRIVATE */
-png_handle_unknown(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
-{
-   png_uint_32 skip = 0;
-
-   png_debug(1, "in png_handle_unknown\n");
-
-   if (png_ptr->mode & PNG_HAVE_IDAT)
-   {
-#ifdef PNG_USE_LOCAL_ARRAYS
-      PNG_CONST PNG_IDAT;
-#endif
-      if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4))  /* not an IDAT */
-         png_ptr->mode |= PNG_AFTER_IDAT;
-   }
-
-   png_check_chunk_name(png_ptr, png_ptr->chunk_name);
-
-   if (!(png_ptr->chunk_name[0] & 0x20))
-   {
-#if defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
-      if(png_handle_as_unknown(png_ptr, png_ptr->chunk_name) !=
-           PNG_HANDLE_CHUNK_ALWAYS
-#if defined(PNG_READ_USER_CHUNKS_SUPPORTED)
-           && png_ptr->read_user_chunk_fn == NULL
-#endif
-        )
-#endif
-          png_chunk_error(png_ptr, "unknown critical chunk");
-   }
-
-#if defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
-   if ((png_ptr->flags & PNG_FLAG_KEEP_UNKNOWN_CHUNKS) ||
-       (png_ptr->read_user_chunk_fn != NULL))
-   {
-#ifdef PNG_MAX_MALLOC_64K
-       if (length > (png_uint_32)65535L)
-       {
-           png_warning(png_ptr, "unknown chunk too large to fit in memory");
-           skip = length - (png_uint_32)65535L;
-           length = (png_uint_32)65535L;
-       }
-#endif
-       png_memcpy((png_charp)png_ptr->unknown_chunk.name,
-                  (png_charp)png_ptr->chunk_name, 
-                  png_sizeof(png_ptr->unknown_chunk.name));
-       png_ptr->unknown_chunk.name[png_sizeof(png_ptr->unknown_chunk.name)-1] = '\0';
-       png_ptr->unknown_chunk.size = (png_size_t)length;
-       if (length == 0)
-         png_ptr->unknown_chunk.data = NULL;
-       else
-       {
-         png_ptr->unknown_chunk.data = (png_bytep)png_malloc(png_ptr, length);
-         png_crc_read(png_ptr, (png_bytep)png_ptr->unknown_chunk.data, length);
-       }
-#if defined(PNG_READ_USER_CHUNKS_SUPPORTED)
-       if(png_ptr->read_user_chunk_fn != NULL)
-       {
-          /* callback to user unknown chunk handler */
-          int ret;
-          ret = (*(png_ptr->read_user_chunk_fn))
-            (png_ptr, &png_ptr->unknown_chunk);
-          if (ret < 0)
-             png_chunk_error(png_ptr, "error in user chunk");
-          if (ret == 0)
-          {
-             if (!(png_ptr->chunk_name[0] & 0x20))
-                if(png_handle_as_unknown(png_ptr, png_ptr->chunk_name) !=
-                     PNG_HANDLE_CHUNK_ALWAYS)
-                   png_chunk_error(png_ptr, "unknown critical chunk");
-             png_set_unknown_chunks(png_ptr, info_ptr,
-               &png_ptr->unknown_chunk, 1);
-          }
-       }
-       else
-#endif
-       png_set_unknown_chunks(png_ptr, info_ptr, &png_ptr->unknown_chunk, 1);
-       png_free(png_ptr, png_ptr->unknown_chunk.data);
-       png_ptr->unknown_chunk.data = NULL;
-   }
-   else
-#endif
-      skip = length;
-
-   png_crc_finish(png_ptr, skip);
-
-#if !defined(PNG_READ_USER_CHUNKS_SUPPORTED)
-   info_ptr = info_ptr; /* quiet compiler warnings about unused info_ptr */
-#endif
-}
-
-/* This function is called to verify that a chunk name is valid.
-   This function can't have the "critical chunk check" incorporated
-   into it, since in the future we will need to be able to call user
-   functions to handle unknown critical chunks after we check that
-   the chunk name itself is valid. */
-
-#define isnonalpha(c) ((c) < 65 || (c) > 122 || ((c) > 90 && (c) < 97))
-
-void /* PRIVATE */
-png_check_chunk_name(png_structp png_ptr, png_bytep chunk_name)
-{
-   png_debug(1, "in png_check_chunk_name\n");
-   if (isnonalpha(chunk_name[0]) || isnonalpha(chunk_name[1]) ||
-       isnonalpha(chunk_name[2]) || isnonalpha(chunk_name[3]))
-   {
-      png_chunk_error(png_ptr, "invalid chunk type");
-   }
-}
-
-/* Combines the row recently read in with the existing pixels in the
-   row.  This routine takes care of alpha and transparency if requested.
-   This routine also handles the two methods of progressive display
-   of interlaced images, depending on the mask value.
-   The mask value describes which pixels are to be combined with
-   the row.  The pattern always repeats every 8 pixels, so just 8
-   bits are needed.  A one indicates the pixel is to be combined,
-   a zero indicates the pixel is to be skipped.  This is in addition
-   to any alpha or transparency value associated with the pixel.  If
-   you want all pixels to be combined, pass 0xff (255) in mask.  */
-
-void /* PRIVATE */
-png_combine_row(png_structp png_ptr, png_bytep row, int mask)
-{
-   png_debug(1,"in png_combine_row\n");
-   if (mask == 0xff)
-   {
-      png_memcpy(row, png_ptr->row_buf + 1,
-         PNG_ROWBYTES(png_ptr->row_info.pixel_depth, png_ptr->width));
-   }
-   else
-   {
-      switch (png_ptr->row_info.pixel_depth)
-      {
-         case 1:
-         {
-            png_bytep sp = png_ptr->row_buf + 1;
-            png_bytep dp = row;
-            int s_inc, s_start, s_end;
-            int m = 0x80;
-            int shift;
-            png_uint_32 i;
-            png_uint_32 row_width = png_ptr->width;
-
-#if defined(PNG_READ_PACKSWAP_SUPPORTED)
-            if (png_ptr->transformations & PNG_PACKSWAP)
-            {
-                s_start = 0;
-                s_end = 7;
-                s_inc = 1;
-            }
-            else
-#endif
-            {
-                s_start = 7;
-                s_end = 0;
-                s_inc = -1;
-            }
-
-            shift = s_start;
-
-            for (i = 0; i < row_width; i++)
-            {
-               if (m & mask)
-               {
-                  int value;
-
-                  value = (*sp >> shift) & 0x01;
-                  *dp &= (png_byte)((0x7f7f >> (7 - shift)) & 0xff);
-                  *dp |= (png_byte)(value << shift);
-               }
-
-               if (shift == s_end)
-               {
-                  shift = s_start;
-                  sp++;
-                  dp++;
-               }
-               else
-                  shift += s_inc;
-
-               if (m == 1)
-                  m = 0x80;
-               else
-                  m >>= 1;
-            }
-            break;
-         }
-         case 2:
-         {
-            png_bytep sp = png_ptr->row_buf + 1;
-            png_bytep dp = row;
-            int s_start, s_end, s_inc;
-            int m = 0x80;
-            int shift;
-            png_uint_32 i;
-            png_uint_32 row_width = png_ptr->width;
-            int value;
-
-#if defined(PNG_READ_PACKSWAP_SUPPORTED)
-            if (png_ptr->transformations & PNG_PACKSWAP)
-            {
-               s_start = 0;
-               s_end = 6;
-               s_inc = 2;
-            }
-            else
-#endif
-            {
-               s_start = 6;
-               s_end = 0;
-               s_inc = -2;
-            }
-
-            shift = s_start;
-
-            for (i = 0; i < row_width; i++)
-            {
-               if (m & mask)
-               {
-                  value = (*sp >> shift) & 0x03;
-                  *dp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff);
-                  *dp |= (png_byte)(value << shift);
-               }
-
-               if (shift == s_end)
-               {
-                  shift = s_start;
-                  sp++;
-                  dp++;
-               }
-               else
-                  shift += s_inc;
-               if (m == 1)
-                  m = 0x80;
-               else
-                  m >>= 1;
-            }
-            break;
-         }
-         case 4:
-         {
-            png_bytep sp = png_ptr->row_buf + 1;
-            png_bytep dp = row;
-            int s_start, s_end, s_inc;
-            int m = 0x80;
-            int shift;
-            png_uint_32 i;
-            png_uint_32 row_width = png_ptr->width;
-            int value;
-
-#if defined(PNG_READ_PACKSWAP_SUPPORTED)
-            if (png_ptr->transformations & PNG_PACKSWAP)
-            {
-               s_start = 0;
-               s_end = 4;
-               s_inc = 4;
-            }
-            else
-#endif
-            {
-               s_start = 4;
-               s_end = 0;
-               s_inc = -4;
-            }
-            shift = s_start;
-
-            for (i = 0; i < row_width; i++)
-            {
-               if (m & mask)
-               {
-                  value = (*sp >> shift) & 0xf;
-                  *dp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff);
-                  *dp |= (png_byte)(value << shift);
-               }
-
-               if (shift == s_end)
-               {
-                  shift = s_start;
-                  sp++;
-                  dp++;
-               }
-               else
-                  shift += s_inc;
-               if (m == 1)
-                  m = 0x80;
-               else
-                  m >>= 1;
-            }
-            break;
-         }
-         default:
-         {
-            png_bytep sp = png_ptr->row_buf + 1;
-            png_bytep dp = row;
-            png_size_t pixel_bytes = (png_ptr->row_info.pixel_depth >> 3);
-            png_uint_32 i;
-            png_uint_32 row_width = png_ptr->width;
-            png_byte m = 0x80;
-
-
-            for (i = 0; i < row_width; i++)
-            {
-               if (m & mask)
-               {
-                  png_memcpy(dp, sp, pixel_bytes);
-               }
-
-               sp += pixel_bytes;
-               dp += pixel_bytes;
-
-               if (m == 1)
-                  m = 0x80;
-               else
-                  m >>= 1;
-            }
-            break;
-         }
-      }
-   }
-}
-
-#ifdef PNG_READ_INTERLACING_SUPPORTED
-/* OLD pre-1.0.9 interface:
-void png_do_read_interlace(png_row_infop row_info, png_bytep row, int pass,
-   png_uint_32 transformations)
- */
-void /* PRIVATE */
-png_do_read_interlace(png_structp png_ptr)
-{
-   png_row_infop row_info = &(png_ptr->row_info);
-   png_bytep row = png_ptr->row_buf + 1;
-   int pass = png_ptr->pass;
-   png_uint_32 transformations = png_ptr->transformations;
-#ifdef PNG_USE_LOCAL_ARRAYS
-   /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
-   /* offset to next interlace block */
-   PNG_CONST int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
-#endif
-
-   png_debug(1,"in png_do_read_interlace\n");
-   if (row != NULL && row_info != NULL)
-   {
-      png_uint_32 final_width;
-
-      final_width = row_info->width * png_pass_inc[pass];
-
-      switch (row_info->pixel_depth)
-      {
-         case 1:
-         {
-            png_bytep sp = row + (png_size_t)((row_info->width - 1) >> 3);
-            png_bytep dp = row + (png_size_t)((final_width - 1) >> 3);
-            int sshift, dshift;
-            int s_start, s_end, s_inc;
-            int jstop = png_pass_inc[pass];
-            png_byte v;
-            png_uint_32 i;
-            int j;
-
-#if defined(PNG_READ_PACKSWAP_SUPPORTED)
-            if (transformations & PNG_PACKSWAP)
-            {
-                sshift = (int)((row_info->width + 7) & 0x07);
-                dshift = (int)((final_width + 7) & 0x07);
-                s_start = 7;
-                s_end = 0;
-                s_inc = -1;
-            }
-            else
-#endif
-            {
-                sshift = 7 - (int)((row_info->width + 7) & 0x07);
-                dshift = 7 - (int)((final_width + 7) & 0x07);
-                s_start = 0;
-                s_end = 7;
-                s_inc = 1;
-            }
-
-            for (i = 0; i < row_info->width; i++)
-            {
-               v = (png_byte)((*sp >> sshift) & 0x01);
-               for (j = 0; j < jstop; j++)
-               {
-                  *dp &= (png_byte)((0x7f7f >> (7 - dshift)) & 0xff);
-                  *dp |= (png_byte)(v << dshift);
-                  if (dshift == s_end)
-                  {
-                     dshift = s_start;
-                     dp--;
-                  }
-                  else
-                     dshift += s_inc;
-               }
-               if (sshift == s_end)
-               {
-                  sshift = s_start;
-                  sp--;
-               }
-               else
-                  sshift += s_inc;
-            }
-            break;
-         }
-         case 2:
-         {
-            png_bytep sp = row + (png_uint_32)((row_info->width - 1) >> 2);
-            png_bytep dp = row + (png_uint_32)((final_width - 1) >> 2);
-            int sshift, dshift;
-            int s_start, s_end, s_inc;
-            int jstop = png_pass_inc[pass];
-            png_uint_32 i;
-
-#if defined(PNG_READ_PACKSWAP_SUPPORTED)
-            if (transformations & PNG_PACKSWAP)
-            {
-               sshift = (int)(((row_info->width + 3) & 0x03) << 1);
-               dshift = (int)(((final_width + 3) & 0x03) << 1);
-               s_start = 6;
-               s_end = 0;
-               s_inc = -2;
-            }
-            else
-#endif
-            {
-               sshift = (int)((3 - ((row_info->width + 3) & 0x03)) << 1);
-               dshift = (int)((3 - ((final_width + 3) & 0x03)) << 1);
-               s_start = 0;
-               s_end = 6;
-               s_inc = 2;
-            }
-
-            for (i = 0; i < row_info->width; i++)
-            {
-               png_byte v;
-               int j;
-
-               v = (png_byte)((*sp >> sshift) & 0x03);
-               for (j = 0; j < jstop; j++)
-               {
-                  *dp &= (png_byte)((0x3f3f >> (6 - dshift)) & 0xff);
-                  *dp |= (png_byte)(v << dshift);
-                  if (dshift == s_end)
-                  {
-                     dshift = s_start;
-                     dp--;
-                  }
-                  else
-                     dshift += s_inc;
-               }
-               if (sshift == s_end)
-               {
-                  sshift = s_start;
-                  sp--;
-               }
-               else
-                  sshift += s_inc;
-            }
-            break;
-         }
-         case 4:
-         {
-            png_bytep sp = row + (png_size_t)((row_info->width - 1) >> 1);
-            png_bytep dp = row + (png_size_t)((final_width - 1) >> 1);
-            int sshift, dshift;
-            int s_start, s_end, s_inc;
-            png_uint_32 i;
-            int jstop = png_pass_inc[pass];
-
-#if defined(PNG_READ_PACKSWAP_SUPPORTED)
-            if (transformations & PNG_PACKSWAP)
-            {
-               sshift = (int)(((row_info->width + 1) & 0x01) << 2);
-               dshift = (int)(((final_width + 1) & 0x01) << 2);
-               s_start = 4;
-               s_end = 0;
-               s_inc = -4;
-            }
-            else
-#endif
-            {
-               sshift = (int)((1 - ((row_info->width + 1) & 0x01)) << 2);
-               dshift = (int)((1 - ((final_width + 1) & 0x01)) << 2);
-               s_start = 0;
-               s_end = 4;
-               s_inc = 4;
-            }
-
-            for (i = 0; i < row_info->width; i++)
-            {
-               png_byte v = (png_byte)((*sp >> sshift) & 0xf);
-               int j;
-
-               for (j = 0; j < jstop; j++)
-               {
-                  *dp &= (png_byte)((0xf0f >> (4 - dshift)) & 0xff);
-                  *dp |= (png_byte)(v << dshift);
-                  if (dshift == s_end)
-                  {
-                     dshift = s_start;
-                     dp--;
-                  }
-                  else
-                     dshift += s_inc;
-               }
-               if (sshift == s_end)
-               {
-                  sshift = s_start;
-                  sp--;
-               }
-               else
-                  sshift += s_inc;
-            }
-            break;
-         }
-         default:
-         {
-            png_size_t pixel_bytes = (row_info->pixel_depth >> 3);
-            png_bytep sp = row + (png_size_t)(row_info->width - 1) * pixel_bytes;
-            png_bytep dp = row + (png_size_t)(final_width - 1) * pixel_bytes;
-
-            int jstop = png_pass_inc[pass];
-            png_uint_32 i;
-
-            for (i = 0; i < row_info->width; i++)
-            {
-               png_byte v[8];
-               int j;
-
-               png_memcpy(v, sp, pixel_bytes);
-               for (j = 0; j < jstop; j++)
-               {
-                  png_memcpy(dp, v, pixel_bytes);
-                  dp -= pixel_bytes;
-               }
-               sp -= pixel_bytes;
-            }
-            break;
-         }
-      }
-      row_info->width = final_width;
-      row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,final_width);
-   }
-#if !defined(PNG_READ_PACKSWAP_SUPPORTED)
-   transformations = transformations; /* silence compiler warning */
-#endif
-}
-#endif /* PNG_READ_INTERLACING_SUPPORTED */
-
-void /* PRIVATE */
-png_read_filter_row(png_structp png_ptr, png_row_infop row_info, png_bytep row,
-   png_bytep prev_row, int filter)
-{
-   png_debug(1, "in png_read_filter_row\n");
-   png_debug2(2,"row = %lu, filter = %d\n", png_ptr->row_number, filter);
-   switch (filter)
-   {
-      case PNG_FILTER_VALUE_NONE:
-         break;
-      case PNG_FILTER_VALUE_SUB:
-      {
-         png_uint_32 i;
-         png_uint_32 istop = row_info->rowbytes;
-         png_uint_32 bpp = (row_info->pixel_depth + 7) >> 3;
-         png_bytep rp = row + bpp;
-         png_bytep lp = row;
-
-         for (i = bpp; i < istop; i++)
-         {
-            *rp = (png_byte)(((int)(*rp) + (int)(*lp++)) & 0xff);
-            rp++;
-         }
-         break;
-      }
-      case PNG_FILTER_VALUE_UP:
-      {
-         png_uint_32 i;
-         png_uint_32 istop = row_info->rowbytes;
-         png_bytep rp = row;
-         png_bytep pp = prev_row;
-
-         for (i = 0; i < istop; i++)
-         {
-            *rp = (png_byte)(((int)(*rp) + (int)(*pp++)) & 0xff);
-            rp++;
-         }
-         break;
-      }
-      case PNG_FILTER_VALUE_AVG:
-      {
-         png_uint_32 i;
-         png_bytep rp = row;
-         png_bytep pp = prev_row;
-         png_bytep lp = row;
-         png_uint_32 bpp = (row_info->pixel_depth + 7) >> 3;
-         png_uint_32 istop = row_info->rowbytes - bpp;
-
-         for (i = 0; i < bpp; i++)
-         {
-            *rp = (png_byte)(((int)(*rp) +
-               ((int)(*pp++) / 2 )) & 0xff);
-            rp++;
-         }
-
-         for (i = 0; i < istop; i++)
-         {
-            *rp = (png_byte)(((int)(*rp) +
-               (int)(*pp++ + *lp++) / 2 ) & 0xff);
-            rp++;
-         }
-         break;
-      }
-      case PNG_FILTER_VALUE_PAETH:
-      {
-         png_uint_32 i;
-         png_bytep rp = row;
-         png_bytep pp = prev_row;
-         png_bytep lp = row;
-         png_bytep cp = prev_row;
-         png_uint_32 bpp = (row_info->pixel_depth + 7) >> 3;
-         png_uint_32 istop=row_info->rowbytes - bpp;
-
-         for (i = 0; i < bpp; i++)
-         {
-            *rp = (png_byte)(((int)(*rp) + (int)(*pp++)) & 0xff);
-            rp++;
-         }
-
-         for (i = 0; i < istop; i++)   /* use leftover rp,pp */
-         {
-            int a, b, c, pa, pb, pc, p;
-
-            a = *lp++;
-            b = *pp++;
-            c = *cp++;
-
-            p = b - c;
-            pc = a - c;
-
-#ifdef PNG_USE_ABS
-            pa = abs(p);
-            pb = abs(pc);
-            pc = abs(p + pc);
-#else
-            pa = p < 0 ? -p : p;
-            pb = pc < 0 ? -pc : pc;
-            pc = (p + pc) < 0 ? -(p + pc) : p + pc;
-#endif
-
-            /*
-               if (pa <= pb && pa <= pc)
-                  p = a;
-               else if (pb <= pc)
-                  p = b;
-               else
-                  p = c;
-             */
-
-            p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
-
-            *rp = (png_byte)(((int)(*rp) + p) & 0xff);
-            rp++;
-         }
-         break;
-      }
-      default:
-         png_warning(png_ptr, "Ignoring bad adaptive filter type");
-         *row=0;
-         break;
-   }
-}
-
-void /* PRIVATE */
-png_read_finish_row(png_structp png_ptr)
-{
-#ifdef PNG_USE_LOCAL_ARRAYS
-#ifdef PNG_READ_INTERLACING_SUPPORTED
-   /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
-
-   /* start of interlace block */
-   PNG_CONST int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
-
-   /* offset to next interlace block */
-   PNG_CONST int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
-
-   /* start of interlace block in the y direction */
-   PNG_CONST int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
-
-   /* offset to next interlace block in the y direction */
-   PNG_CONST int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
-#endif /* PNG_READ_INTERLACING_SUPPORTED */
-#endif
-
-   png_debug(1, "in png_read_finish_row\n");
-   png_ptr->row_number++;
-   if (png_ptr->row_number < png_ptr->num_rows)
-      return;
-
-#ifdef PNG_READ_INTERLACING_SUPPORTED
-   if (png_ptr->interlaced)
-   {
-      png_ptr->row_number = 0;
-      png_memset_check(png_ptr, png_ptr->prev_row, 0,
-         png_ptr->rowbytes + 1);
-      do
-      {
-         png_ptr->pass++;
-         if (png_ptr->pass >= 7)
-            break;
-         png_ptr->iwidth = (png_ptr->width +
-            png_pass_inc[png_ptr->pass] - 1 -
-            png_pass_start[png_ptr->pass]) /
-            png_pass_inc[png_ptr->pass];
-
-         png_ptr->irowbytes = PNG_ROWBYTES(png_ptr->pixel_depth,
-            png_ptr->iwidth) + 1;
-
-         if (!(png_ptr->transformations & PNG_INTERLACE))
-         {
-            png_ptr->num_rows = (png_ptr->height +
-               png_pass_yinc[png_ptr->pass] - 1 -
-               png_pass_ystart[png_ptr->pass]) /
-               png_pass_yinc[png_ptr->pass];
-            if (!(png_ptr->num_rows))
-               continue;
-         }
-         else  /* if (png_ptr->transformations & PNG_INTERLACE) */
-            break;
-      } while (png_ptr->iwidth == 0);
-
-      if (png_ptr->pass < 7)
-         return;
-   }
-#endif /* PNG_READ_INTERLACING_SUPPORTED */
-
-   if (!(png_ptr->flags & PNG_FLAG_ZLIB_FINISHED))
-   {
-#ifdef PNG_USE_LOCAL_ARRAYS
-      PNG_CONST PNG_IDAT;
-#endif
-      char extra;
-      int ret;
-
-      png_ptr->zstream.next_out = (Byte *)&extra;
-      png_ptr->zstream.avail_out = (uInt)1;
-      for(;;)
-      {
-         if (!(png_ptr->zstream.avail_in))
-         {
-            while (!png_ptr->idat_size)
-            {
-               png_byte chunk_length[4];
-
-               png_crc_finish(png_ptr, 0);
-
-               png_read_data(png_ptr, chunk_length, 4);
-               png_ptr->idat_size = png_get_uint_31(png_ptr, chunk_length);
-               png_reset_crc(png_ptr);
-               png_crc_read(png_ptr, png_ptr->chunk_name, 4);
-               if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
-                  png_error(png_ptr, "Not enough image data");
-
-            }
-            png_ptr->zstream.avail_in = (uInt)png_ptr->zbuf_size;
-            png_ptr->zstream.next_in = png_ptr->zbuf;
-            if (png_ptr->zbuf_size > png_ptr->idat_size)
-               png_ptr->zstream.avail_in = (uInt)png_ptr->idat_size;
-            png_crc_read(png_ptr, png_ptr->zbuf, png_ptr->zstream.avail_in);
-            png_ptr->idat_size -= png_ptr->zstream.avail_in;
-         }
-         ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);
-         if (ret == Z_STREAM_END)
-         {
-            if (!(png_ptr->zstream.avail_out) || png_ptr->zstream.avail_in ||
-               png_ptr->idat_size)
-               png_warning(png_ptr, "Extra compressed data");
-            png_ptr->mode |= PNG_AFTER_IDAT;
-            png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
-            break;
-         }
-         if (ret != Z_OK)
-            png_error(png_ptr, png_ptr->zstream.msg ? png_ptr->zstream.msg :
-                      "Decompression Error");
-
-         if (!(png_ptr->zstream.avail_out))
-         {
-            png_warning(png_ptr, "Extra compressed data.");
-            png_ptr->mode |= PNG_AFTER_IDAT;
-            png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
-            break;
-         }
-
-      }
-      png_ptr->zstream.avail_out = 0;
-   }
-
-   if (png_ptr->idat_size || png_ptr->zstream.avail_in)
-      png_warning(png_ptr, "Extra compression data");
-
-   inflateReset(&png_ptr->zstream);
-
-   png_ptr->mode |= PNG_AFTER_IDAT;
-}
-
-void /* PRIVATE */
-png_read_start_row(png_structp png_ptr)
-{
-#ifdef PNG_USE_LOCAL_ARRAYS
-#ifdef PNG_READ_INTERLACING_SUPPORTED
-   /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
-
-   /* start of interlace block */
-   PNG_CONST int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
-
-   /* offset to next interlace block */
-   PNG_CONST int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
-
-   /* start of interlace block in the y direction */
-   PNG_CONST int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
-
-   /* offset to next interlace block in the y direction */
-   PNG_CONST int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
-#endif
-#endif
-
-   int max_pixel_depth;
-   png_uint_32 row_bytes;
-
-   png_debug(1, "in png_read_start_row\n");
-   png_ptr->zstream.avail_in = 0;
-   png_init_read_transformations(png_ptr);
-#ifdef PNG_READ_INTERLACING_SUPPORTED
-   if (png_ptr->interlaced)
-   {
-      if (!(png_ptr->transformations & PNG_INTERLACE))
-         png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
-            png_pass_ystart[0]) / png_pass_yinc[0];
-      else
-         png_ptr->num_rows = png_ptr->height;
-
-      png_ptr->iwidth = (png_ptr->width +
-         png_pass_inc[png_ptr->pass] - 1 -
-         png_pass_start[png_ptr->pass]) /
-         png_pass_inc[png_ptr->pass];
-
-         row_bytes = PNG_ROWBYTES(png_ptr->pixel_depth,png_ptr->iwidth) + 1;
-
-         png_ptr->irowbytes = (png_size_t)row_bytes;
-         if((png_uint_32)png_ptr->irowbytes != row_bytes)
-            png_error(png_ptr, "Rowbytes overflow in png_read_start_row");
-   }
-   else
-#endif /* PNG_READ_INTERLACING_SUPPORTED */
-   {
-      png_ptr->num_rows = png_ptr->height;
-      png_ptr->iwidth = png_ptr->width;
-      png_ptr->irowbytes = png_ptr->rowbytes + 1;
-   }
-   max_pixel_depth = png_ptr->pixel_depth;
-
-#if defined(PNG_READ_PACK_SUPPORTED)
-   if ((png_ptr->transformations & PNG_PACK) && png_ptr->bit_depth < 8)
-      max_pixel_depth = 8;
-#endif
-
-#if defined(PNG_READ_EXPAND_SUPPORTED)
-   if (png_ptr->transformations & PNG_EXPAND)
-   {
-      if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
-      {
-         if (png_ptr->num_trans)
-            max_pixel_depth = 32;
-         else
-            max_pixel_depth = 24;
-      }
-      else if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
-      {
-         if (max_pixel_depth < 8)
-            max_pixel_depth = 8;
-         if (png_ptr->num_trans)
-            max_pixel_depth *= 2;
-      }
-      else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
-      {
-         if (png_ptr->num_trans)
-         {
-            max_pixel_depth *= 4;
-            max_pixel_depth /= 3;
-         }
-      }
-   }
-#endif
-
-#if defined(PNG_READ_FILLER_SUPPORTED)
-   if (png_ptr->transformations & (PNG_FILLER))
-   {
-      if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
-         max_pixel_depth = 32;
-      else if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
-      {
-         if (max_pixel_depth <= 8)
-            max_pixel_depth = 16;
-         else
-            max_pixel_depth = 32;
-      }
-      else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
-      {
-         if (max_pixel_depth <= 32)
-            max_pixel_depth = 32;
-         else
-            max_pixel_depth = 64;
-      }
-   }
-#endif
-
-#if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED)
-   if (png_ptr->transformations & PNG_GRAY_TO_RGB)
-   {
-      if (
-#if defined(PNG_READ_EXPAND_SUPPORTED)
-        (png_ptr->num_trans && (png_ptr->transformations & PNG_EXPAND)) ||
-#endif
-#if defined(PNG_READ_FILLER_SUPPORTED)
-        (png_ptr->transformations & (PNG_FILLER)) ||
-#endif
-        png_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
-      {
-         if (max_pixel_depth <= 16)
-            max_pixel_depth = 32;
-         else
-            max_pixel_depth = 64;
-      }
-      else
-      {
-         if (max_pixel_depth <= 8)
-           {
-             if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
-               max_pixel_depth = 32;
-             else
-               max_pixel_depth = 24;
-           }
-         else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
-            max_pixel_depth = 64;
-         else
-            max_pixel_depth = 48;
-      }
-   }
-#endif
-
-#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) && \
-defined(PNG_USER_TRANSFORM_PTR_SUPPORTED)
-   if(png_ptr->transformations & PNG_USER_TRANSFORM)
-     {
-       int user_pixel_depth=png_ptr->user_transform_depth*
-         png_ptr->user_transform_channels;
-       if(user_pixel_depth > max_pixel_depth)
-         max_pixel_depth=user_pixel_depth;
-     }
-#endif
-
-   /* align the width on the next larger 8 pixels.  Mainly used
-      for interlacing */
-   row_bytes = ((png_ptr->width + 7) & ~((png_uint_32)7));
-   /* calculate the maximum bytes needed, adding a byte and a pixel
-      for safety's sake */
-   row_bytes = PNG_ROWBYTES(max_pixel_depth,row_bytes) +
-      1 + ((max_pixel_depth + 7) >> 3);
-#ifdef PNG_MAX_MALLOC_64K
-   if (row_bytes > (png_uint_32)65536L)
-      png_error(png_ptr, "This image requires a row greater than 64KB");
-#endif
-
-   if(row_bytes + 64 > png_ptr->old_big_row_buf_size)
-   {
-     png_free(png_ptr,png_ptr->big_row_buf);
-     png_ptr->big_row_buf = (png_bytep)png_malloc(png_ptr, row_bytes+64);
-     png_ptr->row_buf = png_ptr->big_row_buf+32;
-     png_ptr->old_big_row_buf_size = row_bytes+64;
-   }
-
-#ifdef PNG_MAX_MALLOC_64K
-   if ((png_uint_32)png_ptr->rowbytes + 1 > (png_uint_32)65536L)
-      png_error(png_ptr, "This image requires a row greater than 64KB");
-#endif
-   if ((png_uint_32)png_ptr->rowbytes > (png_uint_32)(PNG_SIZE_MAX - 1))
-      png_error(png_ptr, "Row has too many bytes to allocate in memory.");
-
-   if(png_ptr->rowbytes+1 > png_ptr->old_prev_row_size)
-   {
-     png_free(png_ptr,png_ptr->prev_row);
-     png_ptr->prev_row = (png_bytep)png_malloc(png_ptr, (png_uint_32)(
-        png_ptr->rowbytes + 1));
-     png_ptr->old_prev_row_size = png_ptr->rowbytes+1;
-   }
-
-   png_memset_check(png_ptr, png_ptr->prev_row, 0, png_ptr->rowbytes + 1);
-
-   png_debug1(3, "width = %lu,\n", png_ptr->width);
-   png_debug1(3, "height = %lu,\n", png_ptr->height);
-   png_debug1(3, "iwidth = %lu,\n", png_ptr->iwidth);
-   png_debug1(3, "num_rows = %lu\n", png_ptr->num_rows);
-   png_debug1(3, "rowbytes = %lu,\n", png_ptr->rowbytes);
-   png_debug1(3, "irowbytes = %lu,\n", png_ptr->irowbytes);
-
-   png_ptr->flags |= PNG_FLAG_ROW_INIT;
-}
-#endif /* PNG_READ_SUPPORTED */
+\r
+/* pngrutil.c - utilities to read a PNG file\r
+ *\r
+ * Last changed in libpng 1.4.3 [June 26, 2010]\r
+ * Copyright (c) 1998-2010 Glenn Randers-Pehrson\r
+ * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)\r
+ * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)\r
+ *\r
+ * This code is released under the libpng license.\r
+ * For conditions of distribution and use, see the disclaimer\r
+ * and license in png.h\r
+ *\r
+ * This file contains routines that are only called from within\r
+ * libpng itself during the course of reading an image.\r
+ */\r
+\r
+#define PNG_NO_PEDANTIC_WARNINGS\r
+#include "png.h"\r
+#ifdef PNG_READ_SUPPORTED\r
+#include "pngpriv.h"\r
+\r
+#    define png_strtod(p,a,b) strtod(a,b)\r
+png_uint_32 PNGAPI\r
+png_get_uint_31(png_structp png_ptr, png_bytep buf)\r
+{\r
+   png_uint_32 i = png_get_uint_32(buf);\r
+   if (i > PNG_UINT_31_MAX)\r
+     png_error(png_ptr, "PNG unsigned integer out of range");\r
+   return (i);\r
+}\r
+#ifndef PNG_USE_READ_MACROS\r
+/* Grab an unsigned 32-bit integer from a buffer in big-endian format. */\r
+png_uint_32 PNGAPI\r
+png_get_uint_32(png_bytep buf)\r
+{\r
+   png_uint_32 i = ((png_uint_32)(*buf) << 24) +\r
+      ((png_uint_32)(*(buf + 1)) << 16) +\r
+      ((png_uint_32)(*(buf + 2)) << 8) +\r
+      (png_uint_32)(*(buf + 3));\r
+\r
+   return (i);\r
+}\r
+\r
+/* Grab a signed 32-bit integer from a buffer in big-endian format.  The\r
+ * data is stored in the PNG file in two's complement format, and it is\r
+ * assumed that the machine format for signed integers is the same.\r
+ */\r
+png_int_32 PNGAPI\r
+png_get_int_32(png_bytep buf)\r
+{\r
+   png_int_32 i = ((png_int_32)(*buf) << 24) +\r
+      ((png_int_32)(*(buf + 1)) << 16) +\r
+      ((png_int_32)(*(buf + 2)) << 8) +\r
+      (png_int_32)(*(buf + 3));\r
+\r
+   return (i);\r
+}\r
+\r
+/* Grab an unsigned 16-bit integer from a buffer in big-endian format. */\r
+png_uint_16 PNGAPI\r
+png_get_uint_16(png_bytep buf)\r
+{\r
+   png_uint_16 i = (png_uint_16)(((png_uint_16)(*buf) << 8) +\r
+      (png_uint_16)(*(buf + 1)));\r
+\r
+   return (i);\r
+}\r
+#endif /* PNG_USE_READ_MACROS */\r
+\r
+/* Read the chunk header (length + type name).\r
+ * Put the type name into png_ptr->chunk_name, and return the length.\r
+ */\r
+png_uint_32 /* PRIVATE */\r
+png_read_chunk_header(png_structp png_ptr)\r
+{\r
+   png_byte buf[8];\r
+   png_uint_32 length;\r
+\r
+#ifdef PNG_IO_STATE_SUPPORTED\r
+   /* Inform the I/O callback that the chunk header is being read.\r
+    * PNG_IO_CHUNK_HDR requires a single I/O call.\r
+    */\r
+   png_ptr->io_state = PNG_IO_READING | PNG_IO_CHUNK_HDR;\r
+#endif\r
+\r
+   /* Read the length and the chunk name */\r
+   png_read_data(png_ptr, buf, 8);\r
+   length = png_get_uint_31(png_ptr, buf);\r
+\r
+   /* Put the chunk name into png_ptr->chunk_name */\r
+   png_memcpy(png_ptr->chunk_name, buf + 4, 4);\r
+\r
+   png_debug2(0, "Reading %s chunk, length = %lu",\r
+      png_ptr->chunk_name, length);\r
+\r
+   /* Reset the crc and run it over the chunk name */\r
+   png_reset_crc(png_ptr);\r
+   png_calculate_crc(png_ptr, png_ptr->chunk_name, 4);\r
+\r
+   /* Check to see if chunk name is valid */\r
+   png_check_chunk_name(png_ptr, png_ptr->chunk_name);\r
+\r
+#ifdef PNG_IO_STATE_SUPPORTED\r
+   /* Inform the I/O callback that chunk data will (possibly) be read.\r
+    * PNG_IO_CHUNK_DATA does NOT require a specific number of I/O calls.\r
+    */\r
+   png_ptr->io_state = PNG_IO_READING | PNG_IO_CHUNK_DATA;\r
+#endif\r
+\r
+   return length;\r
+}\r
+\r
+/* Read data, and (optionally) run it through the CRC. */\r
+void /* PRIVATE */\r
+png_crc_read(png_structp png_ptr, png_bytep buf, png_size_t length)\r
+{\r
+   if (png_ptr == NULL)\r
+      return;\r
+   png_read_data(png_ptr, buf, length);\r
+   png_calculate_crc(png_ptr, buf, length);\r
+}\r
+\r
+/* Optionally skip data and then check the CRC.  Depending on whether we\r
+ * are reading a ancillary or critical chunk, and how the program has set\r
+ * things up, we may calculate the CRC on the data and print a message.\r
+ * Returns '1' if there was a CRC error, '0' otherwise.\r
+ */\r
+int /* PRIVATE */\r
+png_crc_finish(png_structp png_ptr, png_uint_32 skip)\r
+{\r
+   png_size_t i;\r
+   png_size_t istop = png_ptr->zbuf_size;\r
+\r
+   for (i = (png_size_t)skip; i > istop; i -= istop)\r
+   {\r
+      png_crc_read(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);\r
+   }\r
+   if (i)\r
+   {\r
+      png_crc_read(png_ptr, png_ptr->zbuf, i);\r
+   }\r
+\r
+   if (png_crc_error(png_ptr))\r
+   {\r
+      if (((png_ptr->chunk_name[0] & 0x20) &&                /* Ancillary */\r
+          !(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN)) ||\r
+          (!(png_ptr->chunk_name[0] & 0x20) &&             /* Critical  */\r
+          (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_USE)))\r
+      {\r
+         png_chunk_warning(png_ptr, "CRC error");\r
+      }\r
+      else\r
+      {\r
+         png_chunk_benign_error(png_ptr, "CRC error");\r
+         return (0);\r
+      }\r
+      return (1);\r
+   }\r
+\r
+   return (0);\r
+}\r
+\r
+/* Compare the CRC stored in the PNG file with that calculated by libpng from\r
+ * the data it has read thus far.\r
+ */\r
+int /* PRIVATE */\r
+png_crc_error(png_structp png_ptr)\r
+{\r
+   png_byte crc_bytes[4];\r
+   png_uint_32 crc;\r
+   int need_crc = 1;\r
+\r
+   if (png_ptr->chunk_name[0] & 0x20)                     /* ancillary */\r
+   {\r
+      if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_MASK) ==\r
+          (PNG_FLAG_CRC_ANCILLARY_USE | PNG_FLAG_CRC_ANCILLARY_NOWARN))\r
+         need_crc = 0;\r
+   }\r
+   else                                                    /* critical */\r
+   {\r
+      if (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_IGNORE)\r
+         need_crc = 0;\r
+   }\r
+\r
+#ifdef PNG_IO_STATE_SUPPORTED\r
+   /* Inform the I/O callback that the chunk CRC is being read */\r
+   /* PNG_IO_CHUNK_CRC requires the I/O to be done at once */\r
+   png_ptr->io_state = PNG_IO_READING | PNG_IO_CHUNK_CRC;\r
+#endif\r
+\r
+   png_read_data(png_ptr, crc_bytes, 4);\r
+\r
+   if (need_crc)\r
+   {\r
+      crc = png_get_uint_32(crc_bytes);\r
+      return ((int)(crc != png_ptr->crc));\r
+   }\r
+   else\r
+      return (0);\r
+}\r
+\r
+#if defined(PNG_READ_zTXt_SUPPORTED) || defined(PNG_READ_iTXt_SUPPORTED) || \\r
+    defined(PNG_READ_iCCP_SUPPORTED)\r
+static png_size_t\r
+png_inflate(png_structp png_ptr, const png_byte *data, png_size_t size,\r
+        png_bytep output, png_size_t output_size)\r
+{\r
+   png_size_t count = 0;\r
+\r
+   png_ptr->zstream.next_in = (png_bytep)data; /* const_cast: VALID */\r
+   png_ptr->zstream.avail_in = size;\r
+\r
+   while (1)\r
+   {\r
+      int ret, avail;\r
+\r
+      /* Reset the output buffer each time round - we empty it\r
+       * after every inflate call.\r
+       */\r
+      png_ptr->zstream.next_out = png_ptr->zbuf;\r
+      png_ptr->zstream.avail_out = png_ptr->zbuf_size;\r
+\r
+      ret = inflate(&png_ptr->zstream, Z_NO_FLUSH);\r
+      avail = png_ptr->zbuf_size - png_ptr->zstream.avail_out;\r
+\r
+      /* First copy/count any new output - but only if we didn't\r
+       * get an error code.\r
+       */\r
+      if ((ret == Z_OK || ret == Z_STREAM_END) && avail > 0)\r
+      {\r
+         if (output != 0 && output_size > count)\r
+         {\r
+            int copy = output_size - count;\r
+            if (avail < copy) copy = avail;\r
+            png_memcpy(output + count, png_ptr->zbuf, copy);\r
+         }\r
+         count += avail;\r
+      }\r
+\r
+      if (ret == Z_OK)\r
+         continue;\r
+\r
+      /* Termination conditions - always reset the zstream, it\r
+       * must be left in inflateInit state.\r
+       */\r
+      png_ptr->zstream.avail_in = 0;\r
+      inflateReset(&png_ptr->zstream);\r
+\r
+      if (ret == Z_STREAM_END)\r
+         return count; /* NOTE: may be zero. */\r
+\r
+      /* Now handle the error codes - the API always returns 0\r
+       * and the error message is dumped into the uncompressed\r
+       * buffer if available.\r
+       */\r
+      {\r
+         PNG_CONST char *msg;\r
+         if (png_ptr->zstream.msg != 0)\r
+            msg = png_ptr->zstream.msg;\r
+         else\r
+         {\r
+#ifdef PNG_STDIO_SUPPORTED\r
+            char umsg[52];\r
+\r
+            switch (ret)\r
+            {\r
+               case Z_BUF_ERROR:\r
+                  msg = "Buffer error in compressed datastream in %s chunk";\r
+                  break;\r
+               case Z_DATA_ERROR:\r
+                  msg = "Data error in compressed datastream in %s chunk";\r
+                  break;\r
+               default:\r
+                  msg = "Incomplete compressed datastream in %s chunk";\r
+                  break;\r
+            }\r
+\r
+            png_snprintf(umsg, sizeof umsg, msg, png_ptr->chunk_name);\r
+            msg = umsg;\r
+#else\r
+            msg = "Damaged compressed datastream in chunk other than IDAT";\r
+#endif\r
+         }\r
+\r
+         png_warning(png_ptr, msg);\r
+      }\r
+\r
+      /* 0 means an error - notice that this code simple ignores\r
+       * zero length compressed chunks as a result.\r
+       */\r
+      return 0;\r
+   }\r
+}\r
+\r
+/*\r
+ * Decompress trailing data in a chunk.  The assumption is that chunkdata\r
+ * points at an allocated area holding the contents of a chunk with a\r
+ * trailing compressed part.  What we get back is an allocated area\r
+ * holding the original prefix part and an uncompressed version of the\r
+ * trailing part (the malloc area passed in is freed).\r
+ */\r
+void /* PRIVATE */\r
+png_decompress_chunk(png_structp png_ptr, int comp_type,\r
+    png_size_t chunklength,\r
+    png_size_t prefix_size, png_size_t *newlength)\r
+{\r
+   /* The caller should guarantee this */\r
+   if (prefix_size > chunklength)\r
+   {\r
+      /* The recovery is to delete the chunk. */\r
+      png_warning(png_ptr, "invalid chunklength");\r
+      prefix_size = 0; /* To delete everything */\r
+   }\r
+\r
+   else if (comp_type == PNG_COMPRESSION_TYPE_BASE)\r
+   {\r
+      png_size_t expanded_size = png_inflate(png_ptr,\r
+                (png_bytep)(png_ptr->chunkdata + prefix_size),\r
+                chunklength - prefix_size,\r
+                0/*output*/, 0/*output size*/);\r
+\r
+      /* Now check the limits on this chunk - if the limit fails the\r
+       * compressed data will be removed, the prefix will remain.\r
+       */\r
+#ifdef PNG_SET_CHUNK_MALLOC_LIMIT_SUPPORTED\r
+      if (png_ptr->user_chunk_malloc_max &&\r
+          (prefix_size + expanded_size >= png_ptr->user_chunk_malloc_max - 1))\r
+#else\r
+#  ifdef PNG_USER_CHUNK_MALLOC_MAX\r
+      if ((PNG_USER_CHUNK_MALLOC_MAX > 0) &&\r
+          prefix_size + expanded_size >= PNG_USER_CHUNK_MALLOC_MAX - 1)\r
+#  endif\r
+#endif\r
+         png_warning(png_ptr, "Exceeded size limit while expanding chunk");\r
+\r
+      /* If the size is zero either there was an error and a message\r
+       * has already been output (warning) or the size really is zero\r
+       * and we have nothing to do - the code will exit through the\r
+       * error case below.\r
+       */\r
+#if defined(PNG_SET_CHUNK_MALLOC_LIMIT_SUPPORTED) || \\r
+    defined(PNG_USER_CHUNK_MALLOC_MAX)\r
+      else\r
+#endif\r
+      if (expanded_size > 0)\r
+      {\r
+         /* Success (maybe) - really uncompress the chunk. */\r
+         png_size_t new_size = 0;\r
+         png_charp text = png_malloc_warn(png_ptr,\r
+                        prefix_size + expanded_size + 1);\r
+\r
+         if (text != NULL)\r
+         {\r
+            png_memcpy(text, png_ptr->chunkdata, prefix_size);\r
+            new_size = png_inflate(png_ptr,\r
+                (png_bytep)(png_ptr->chunkdata + prefix_size),\r
+                chunklength - prefix_size,\r
+                (png_bytep)(text + prefix_size), expanded_size);\r
+            text[prefix_size + expanded_size] = 0; /* just in case */\r
+\r
+            if (new_size == expanded_size)\r
+            {\r
+               png_free(png_ptr, png_ptr->chunkdata);\r
+               png_ptr->chunkdata = text;\r
+               *newlength = prefix_size + expanded_size;\r
+               return; /* The success return! */\r
+            }\r
+\r
+            png_warning(png_ptr, "png_inflate logic error");\r
+            png_free(png_ptr, text);\r
+         }\r
+         else\r
+          png_warning(png_ptr, "Not enough memory to decompress chunk");\r
+      }\r
+   }\r
+\r
+   else /* if (comp_type != PNG_COMPRESSION_TYPE_BASE) */\r
+   {\r
+#ifdef PNG_STDIO_SUPPORTED\r
+      char umsg[50];\r
+\r
+      png_snprintf(umsg, sizeof umsg, "Unknown zTXt compression type %d",\r
+          comp_type);\r
+      png_warning(png_ptr, umsg);\r
+#else\r
+      png_warning(png_ptr, "Unknown zTXt compression type");\r
+#endif\r
+\r
+      /* The recovery is to simply drop the data. */\r
+   }\r
+\r
+   /* Generic error return - leave the prefix, delete the compressed\r
+    * data, reallocate the chunkdata to remove the potentially large\r
+    * amount of compressed data.\r
+    */\r
+   {\r
+      png_charp text = png_malloc_warn(png_ptr, prefix_size + 1);\r
+      if (text != NULL)\r
+      {\r
+         if (prefix_size > 0)\r
+            png_memcpy(text, png_ptr->chunkdata, prefix_size);\r
+         png_free(png_ptr, png_ptr->chunkdata);\r
+         png_ptr->chunkdata = text;\r
+\r
+         /* This is an extra zero in the 'uncompressed' part. */\r
+         *(png_ptr->chunkdata + prefix_size) = 0x00;\r
+      }\r
+      /* Ignore a malloc error here - it is safe. */\r
+   }\r
+\r
+   *newlength = prefix_size;\r
+}\r
+#endif\r
+\r
+/* Read and check the IDHR chunk */\r
+void /* PRIVATE */\r
+png_handle_IHDR(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)\r
+{\r
+   png_byte buf[13];\r
+   png_uint_32 width, height;\r
+   int bit_depth, color_type, compression_type, filter_type;\r
+   int interlace_type;\r
+\r
+   png_debug(1, "in png_handle_IHDR");\r
+\r
+   if (png_ptr->mode & PNG_HAVE_IHDR)\r
+      png_error(png_ptr, "Out of place IHDR");\r
+\r
+   /* Check the length */\r
+   if (length != 13)\r
+      png_error(png_ptr, "Invalid IHDR chunk");\r
+\r
+   png_ptr->mode |= PNG_HAVE_IHDR;\r
+\r
+   png_crc_read(png_ptr, buf, 13);\r
+   png_crc_finish(png_ptr, 0);\r
+\r
+   width = png_get_uint_31(png_ptr, buf);\r
+   height = png_get_uint_31(png_ptr, buf + 4);\r
+   bit_depth = buf[8];\r
+   color_type = buf[9];\r
+   compression_type = buf[10];\r
+   filter_type = buf[11];\r
+   interlace_type = buf[12];\r
+\r
+   /* Set internal variables */\r
+   png_ptr->width = width;\r
+   png_ptr->height = height;\r
+   png_ptr->bit_depth = (png_byte)bit_depth;\r
+   png_ptr->interlaced = (png_byte)interlace_type;\r
+   png_ptr->color_type = (png_byte)color_type;\r
+#ifdef PNG_MNG_FEATURES_SUPPORTED\r
+   png_ptr->filter_type = (png_byte)filter_type;\r
+#endif\r
+   png_ptr->compression_type = (png_byte)compression_type;\r
+\r
+   /* Find number of channels */\r
+   switch (png_ptr->color_type)\r
+   {\r
+      case PNG_COLOR_TYPE_GRAY:\r
+      case PNG_COLOR_TYPE_PALETTE:\r
+         png_ptr->channels = 1;\r
+         break;\r
+\r
+      case PNG_COLOR_TYPE_RGB:\r
+         png_ptr->channels = 3;\r
+         break;\r
+\r
+      case PNG_COLOR_TYPE_GRAY_ALPHA:\r
+         png_ptr->channels = 2;\r
+         break;\r
+\r
+      case PNG_COLOR_TYPE_RGB_ALPHA:\r
+         png_ptr->channels = 4;\r
+         break;\r
+   }\r
+\r
+   /* Set up other useful info */\r
+   png_ptr->pixel_depth = (png_byte)(png_ptr->bit_depth *\r
+   png_ptr->channels);\r
+   png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->width);\r
+   png_debug1(3, "bit_depth = %d", png_ptr->bit_depth);\r
+   png_debug1(3, "channels = %d", png_ptr->channels);\r
+   png_debug1(3, "rowbytes = %lu", png_ptr->rowbytes);\r
+   png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth,\r
+      color_type, interlace_type, compression_type, filter_type);\r
+}\r
+\r
+/* Read and check the palette */\r
+void /* PRIVATE */\r
+png_handle_PLTE(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)\r
+{\r
+   png_color palette[PNG_MAX_PALETTE_LENGTH];\r
+   int num, i;\r
+#ifdef PNG_POINTER_INDEXING_SUPPORTED\r
+   png_colorp pal_ptr;\r
+#endif\r
+\r
+   png_debug(1, "in png_handle_PLTE");\r
+\r
+   if (!(png_ptr->mode & PNG_HAVE_IHDR))\r
+      png_error(png_ptr, "Missing IHDR before PLTE");\r
+\r
+   else if (png_ptr->mode & PNG_HAVE_IDAT)\r
+   {\r
+      png_warning(png_ptr, "Invalid PLTE after IDAT");\r
+      png_crc_finish(png_ptr, length);\r
+      return;\r
+   }\r
+\r
+   else if (png_ptr->mode & PNG_HAVE_PLTE)\r
+      png_error(png_ptr, "Duplicate PLTE chunk");\r
+\r
+   png_ptr->mode |= PNG_HAVE_PLTE;\r
+\r
+   if (!(png_ptr->color_type&PNG_COLOR_MASK_COLOR))\r
+   {\r
+      png_warning(png_ptr,\r
+        "Ignoring PLTE chunk in grayscale PNG");\r
+      png_crc_finish(png_ptr, length);\r
+      return;\r
+   }\r
+#ifndef PNG_READ_OPT_PLTE_SUPPORTED\r
+   if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE)\r
+   {\r
+      png_crc_finish(png_ptr, length);\r
+      return;\r
+   }\r
+#endif\r
+\r
+   if (length > 3*PNG_MAX_PALETTE_LENGTH || length % 3)\r
+   {\r
+      if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE)\r
+      {\r
+         png_warning(png_ptr, "Invalid palette chunk");\r
+         png_crc_finish(png_ptr, length);\r
+         return;\r
+      }\r
+\r
+      else\r
+      {\r
+         png_error(png_ptr, "Invalid palette chunk");\r
+      }\r
+   }\r
+\r
+   num = (int)length / 3;\r
+\r
+#ifdef PNG_POINTER_INDEXING_SUPPORTED\r
+   for (i = 0, pal_ptr = palette; i < num; i++, pal_ptr++)\r
+   {\r
+      png_byte buf[3];\r
+\r
+      png_crc_read(png_ptr, buf, 3);\r
+      pal_ptr->red = buf[0];\r
+      pal_ptr->green = buf[1];\r
+      pal_ptr->blue = buf[2];\r
+   }\r
+#else\r
+   for (i = 0; i < num; i++)\r
+   {\r
+      png_byte buf[3];\r
+\r
+      png_crc_read(png_ptr, buf, 3);\r
+      /* Don't depend upon png_color being any order */\r
+      palette[i].red = buf[0];\r
+      palette[i].green = buf[1];\r
+      palette[i].blue = buf[2];\r
+   }\r
+#endif\r
+\r
+   /* If we actually NEED the PLTE chunk (ie for a paletted image), we do\r
+    * whatever the normal CRC configuration tells us.  However, if we\r
+    * have an RGB image, the PLTE can be considered ancillary, so\r
+    * we will act as though it is.\r
+    */\r
+#ifndef PNG_READ_OPT_PLTE_SUPPORTED\r
+   if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)\r
+#endif\r
+   {\r
+      png_crc_finish(png_ptr, 0);\r
+   }\r
+#ifndef PNG_READ_OPT_PLTE_SUPPORTED\r
+   else if (png_crc_error(png_ptr))  /* Only if we have a CRC error */\r
+   {\r
+      /* If we don't want to use the data from an ancillary chunk,\r
+         we have two options: an error abort, or a warning and we\r
+         ignore the data in this chunk (which should be OK, since\r
+         it's considered ancillary for a RGB or RGBA image). */\r
+      if (!(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_USE))\r
+      {\r
+         if (png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN)\r
+         {\r
+            png_chunk_benign_error(png_ptr, "CRC error");\r
+         }\r
+         else\r
+         {\r
+            png_chunk_warning(png_ptr, "CRC error");\r
+            return;\r
+         }\r
+      }\r
+      /* Otherwise, we (optionally) emit a warning and use the chunk. */\r
+      else if (!(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN))\r
+      {\r
+         png_chunk_warning(png_ptr, "CRC error");\r
+      }\r
+   }\r
+#endif\r
+\r
+   png_set_PLTE(png_ptr, info_ptr, palette, num);\r
+\r
+#ifdef PNG_READ_tRNS_SUPPORTED\r
+   if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)\r
+   {\r
+      if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS))\r
+      {\r
+         if (png_ptr->num_trans > (png_uint_16)num)\r
+         {\r
+            png_warning(png_ptr, "Truncating incorrect tRNS chunk length");\r
+            png_ptr->num_trans = (png_uint_16)num;\r
+         }\r
+         if (info_ptr->num_trans > (png_uint_16)num)\r
+         {\r
+            png_warning(png_ptr, "Truncating incorrect info tRNS chunk length");\r
+            info_ptr->num_trans = (png_uint_16)num;\r
+         }\r
+      }\r
+   }\r
+#endif\r
+\r
+}\r
+\r
+void /* PRIVATE */\r
+png_handle_IEND(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)\r
+{\r
+   png_debug(1, "in png_handle_IEND");\r
+\r
+   if (!(png_ptr->mode & PNG_HAVE_IHDR) || !(png_ptr->mode & PNG_HAVE_IDAT))\r
+   {\r
+      png_error(png_ptr, "No image in file");\r
+   }\r
+\r
+   png_ptr->mode |= (PNG_AFTER_IDAT | PNG_HAVE_IEND);\r
+\r
+   if (length != 0)\r
+   {\r
+      png_warning(png_ptr, "Incorrect IEND chunk length");\r
+   }\r
+   png_crc_finish(png_ptr, length);\r
+\r
+   info_ptr = info_ptr; /* Quiet compiler warnings about unused info_ptr */\r
+}\r
+\r
+#ifdef PNG_READ_gAMA_SUPPORTED\r
+void /* PRIVATE */\r
+png_handle_gAMA(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)\r
+{\r
+   png_fixed_point igamma;\r
+#ifdef PNG_FLOATING_POINT_SUPPORTED\r
+   float file_gamma;\r
+#endif\r
+   png_byte buf[4];\r
+\r
+   png_debug(1, "in png_handle_gAMA");\r
+\r
+   if (!(png_ptr->mode & PNG_HAVE_IHDR))\r
+      png_error(png_ptr, "Missing IHDR before gAMA");\r
+   else if (png_ptr->mode & PNG_HAVE_IDAT)\r
+   {\r
+      png_warning(png_ptr, "Invalid gAMA after IDAT");\r
+      png_crc_finish(png_ptr, length);\r
+      return;\r
+   }\r
+   else if (png_ptr->mode & PNG_HAVE_PLTE)\r
+      /* Should be an error, but we can cope with it */\r
+      png_warning(png_ptr, "Out of place gAMA chunk");\r
+\r
+   if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_gAMA)\r
+#ifdef PNG_READ_sRGB_SUPPORTED\r
+      && !(info_ptr->valid & PNG_INFO_sRGB)\r
+#endif\r
+      )\r
+   {\r
+      png_warning(png_ptr, "Duplicate gAMA chunk");\r
+      png_crc_finish(png_ptr, length);\r
+      return;\r
+   }\r
+\r
+   if (length != 4)\r
+   {\r
+      png_warning(png_ptr, "Incorrect gAMA chunk length");\r
+      png_crc_finish(png_ptr, length);\r
+      return;\r
+   }\r
+\r
+   png_crc_read(png_ptr, buf, 4);\r
+   if (png_crc_finish(png_ptr, 0))\r
+      return;\r
+\r
+   igamma = (png_fixed_point)png_get_uint_32(buf);\r
+   /* Check for zero gamma */\r
+   if (igamma == 0)\r
+      {\r
+         png_warning(png_ptr,\r
+           "Ignoring gAMA chunk with gamma=0");\r
+         return;\r
+      }\r
+\r
+#ifdef PNG_READ_sRGB_SUPPORTED\r
+   if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sRGB))\r
+      if (PNG_OUT_OF_RANGE(igamma, 45500L, 500))\r
+      {\r
+         png_warning(png_ptr,\r
+           "Ignoring incorrect gAMA value when sRGB is also present");\r
+#ifdef PNG_CONSOLE_IO_SUPPORTED\r
+         fprintf(stderr, "gamma = (%d/100000)", (int)igamma);\r
+#endif\r
+         return;\r
+      }\r
+#endif /* PNG_READ_sRGB_SUPPORTED */\r
+\r
+#ifdef PNG_FLOATING_POINT_SUPPORTED\r
+   file_gamma = (float)igamma / (float)100000.0;\r
+#  ifdef PNG_READ_GAMMA_SUPPORTED\r
+     png_ptr->gamma = file_gamma;\r
+#  endif\r
+     png_set_gAMA(png_ptr, info_ptr, file_gamma);\r
+#endif\r
+#ifdef PNG_FIXED_POINT_SUPPORTED\r
+   png_set_gAMA_fixed(png_ptr, info_ptr, igamma);\r
+#endif\r
+}\r
+#endif\r
+\r
+#ifdef PNG_READ_sBIT_SUPPORTED\r
+void /* PRIVATE */\r
+png_handle_sBIT(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)\r
+{\r
+   png_size_t truelen;\r
+   png_byte buf[4];\r
+\r
+   png_debug(1, "in png_handle_sBIT");\r
+\r
+   buf[0] = buf[1] = buf[2] = buf[3] = 0;\r
+\r
+   if (!(png_ptr->mode & PNG_HAVE_IHDR))\r
+      png_error(png_ptr, "Missing IHDR before sBIT");\r
+   else if (png_ptr->mode & PNG_HAVE_IDAT)\r
+   {\r
+      png_warning(png_ptr, "Invalid sBIT after IDAT");\r
+      png_crc_finish(png_ptr, length);\r
+      return;\r
+   }\r
+   else if (png_ptr->mode & PNG_HAVE_PLTE)\r
+   {\r
+      /* Should be an error, but we can cope with it */\r
+      png_warning(png_ptr, "Out of place sBIT chunk");\r
+   }\r
+   if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sBIT))\r
+   {\r
+      png_warning(png_ptr, "Duplicate sBIT chunk");\r
+      png_crc_finish(png_ptr, length);\r
+      return;\r
+   }\r
+\r
+   if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)\r
+      truelen = 3;\r
+   else\r
+      truelen = (png_size_t)png_ptr->channels;\r
+\r
+   if (length != truelen || length > 4)\r
+   {\r
+      png_warning(png_ptr, "Incorrect sBIT chunk length");\r
+      png_crc_finish(png_ptr, length);\r
+      return;\r
+   }\r
+\r
+   png_crc_read(png_ptr, buf, truelen);\r
+   if (png_crc_finish(png_ptr, 0))\r
+      return;\r
+\r
+   if (png_ptr->color_type & PNG_COLOR_MASK_COLOR)\r
+   {\r
+      png_ptr->sig_bit.red = buf[0];\r
+      png_ptr->sig_bit.green = buf[1];\r
+      png_ptr->sig_bit.blue = buf[2];\r
+      png_ptr->sig_bit.alpha = buf[3];\r
+   }\r
+   else\r
+   {\r
+      png_ptr->sig_bit.gray = buf[0];\r
+      png_ptr->sig_bit.red = buf[0];\r
+      png_ptr->sig_bit.green = buf[0];\r
+      png_ptr->sig_bit.blue = buf[0];\r
+      png_ptr->sig_bit.alpha = buf[1];\r
+   }\r
+   png_set_sBIT(png_ptr, info_ptr, &(png_ptr->sig_bit));\r
+}\r
+#endif\r
+\r
+#ifdef PNG_READ_cHRM_SUPPORTED\r
+void /* PRIVATE */\r
+png_handle_cHRM(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)\r
+{\r
+   png_byte buf[32];\r
+#ifdef PNG_FLOATING_POINT_SUPPORTED\r
+   float white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y;\r
+#endif\r
+   png_fixed_point int_x_white, int_y_white, int_x_red, int_y_red, int_x_green,\r
+      int_y_green, int_x_blue, int_y_blue;\r
+\r
+   png_uint_32 uint_x, uint_y;\r
+\r
+   png_debug(1, "in png_handle_cHRM");\r
+\r
+   if (!(png_ptr->mode & PNG_HAVE_IHDR))\r
+      png_error(png_ptr, "Missing IHDR before cHRM");\r
+   else if (png_ptr->mode & PNG_HAVE_IDAT)\r
+   {\r
+      png_warning(png_ptr, "Invalid cHRM after IDAT");\r
+      png_crc_finish(png_ptr, length);\r
+      return;\r
+   }\r
+   else if (png_ptr->mode & PNG_HAVE_PLTE)\r
+      /* Should be an error, but we can cope with it */\r
+      png_warning(png_ptr, "Missing PLTE before cHRM");\r
+\r
+   if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_cHRM)\r
+#ifdef PNG_READ_sRGB_SUPPORTED\r
+      && !(info_ptr->valid & PNG_INFO_sRGB)\r
+#endif\r
+      )\r
+   {\r
+      png_warning(png_ptr, "Duplicate cHRM chunk");\r
+      png_crc_finish(png_ptr, length);\r
+      return;\r
+   }\r
+\r
+   if (length != 32)\r
+   {\r
+      png_warning(png_ptr, "Incorrect cHRM chunk length");\r
+      png_crc_finish(png_ptr, length);\r
+      return;\r
+   }\r
+\r
+   png_crc_read(png_ptr, buf, 32);\r
+   if (png_crc_finish(png_ptr, 0))\r
+      return;\r
+\r
+   uint_x = png_get_uint_32(buf);\r
+   uint_y = png_get_uint_32(buf + 4);\r
+   int_x_white = (png_fixed_point)uint_x;\r
+   int_y_white = (png_fixed_point)uint_y;\r
+\r
+   uint_x = png_get_uint_32(buf + 8);\r
+   uint_y = png_get_uint_32(buf + 12);\r
+   int_x_red = (png_fixed_point)uint_x;\r
+   int_y_red = (png_fixed_point)uint_y;\r
+\r
+   uint_x = png_get_uint_32(buf + 16);\r
+   uint_y = png_get_uint_32(buf + 20);\r
+   int_x_green = (png_fixed_point)uint_x;\r
+   int_y_green = (png_fixed_point)uint_y;\r
+\r
+   uint_x = png_get_uint_32(buf + 24);\r
+   uint_y = png_get_uint_32(buf + 28);\r
+   int_x_blue = (png_fixed_point)uint_x;\r
+   int_y_blue = (png_fixed_point)uint_y;\r
+\r
+#ifdef PNG_FLOATING_POINT_SUPPORTED\r
+   white_x = (float)int_x_white / (float)100000.0;\r
+   white_y = (float)int_y_white / (float)100000.0;\r
+   red_x   = (float)int_x_red   / (float)100000.0;\r
+   red_y   = (float)int_y_red   / (float)100000.0;\r
+   green_x = (float)int_x_green / (float)100000.0;\r
+   green_y = (float)int_y_green / (float)100000.0;\r
+   blue_x  = (float)int_x_blue  / (float)100000.0;\r
+   blue_y  = (float)int_y_blue  / (float)100000.0;\r
+#endif\r
+\r
+#ifdef PNG_READ_sRGB_SUPPORTED\r
+   if ((info_ptr != NULL) && (info_ptr->valid & PNG_INFO_sRGB))\r
+      {\r
+      if (PNG_OUT_OF_RANGE(int_x_white, 31270,  1000) ||\r
+          PNG_OUT_OF_RANGE(int_y_white, 32900,  1000) ||\r
+          PNG_OUT_OF_RANGE(int_x_red,   64000L, 1000) ||\r
+          PNG_OUT_OF_RANGE(int_y_red,   33000,  1000) ||\r
+          PNG_OUT_OF_RANGE(int_x_green, 30000,  1000) ||\r
+          PNG_OUT_OF_RANGE(int_y_green, 60000L, 1000) ||\r
+          PNG_OUT_OF_RANGE(int_x_blue,  15000,  1000) ||\r
+          PNG_OUT_OF_RANGE(int_y_blue,   6000,  1000))\r
+         {\r
+            png_warning(png_ptr,\r
+              "Ignoring incorrect cHRM value when sRGB is also present");\r
+#ifdef PNG_CONSOLE_IO_SUPPORTED\r
+#ifdef PNG_FLOATING_POINT_SUPPORTED\r
+            fprintf(stderr, "wx=%f, wy=%f, rx=%f, ry=%f\n",\r
+               white_x, white_y, red_x, red_y);\r
+            fprintf(stderr, "gx=%f, gy=%f, bx=%f, by=%f\n",\r
+               green_x, green_y, blue_x, blue_y);\r
+#else\r
+            fprintf(stderr, "wx=%ld, wy=%ld, rx=%ld, ry=%ld\n",\r
+               (long)int_x_white, (long)int_y_white,\r
+               (long)int_x_red, (long)int_y_red);\r
+            fprintf(stderr, "gx=%ld, gy=%ld, bx=%ld, by=%ld\n",\r
+               (long)int_x_green, (long)int_y_green,\r
+               (long)int_x_blue, (long)int_y_blue);\r
+#endif\r
+#endif /* PNG_CONSOLE_IO_SUPPORTED */\r
+         }\r
+         return;\r
+      }\r
+#endif /* PNG_READ_sRGB_SUPPORTED */\r
+\r
+#ifdef PNG_FLOATING_POINT_SUPPORTED\r
+   png_set_cHRM(png_ptr, info_ptr,\r
+      white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y);\r
+#endif\r
+#ifdef PNG_FIXED_POINT_SUPPORTED\r
+   png_set_cHRM_fixed(png_ptr, info_ptr,\r
+      int_x_white, int_y_white, int_x_red, int_y_red, int_x_green,\r
+      int_y_green, int_x_blue, int_y_blue);\r
+#endif\r
+}\r
+#endif\r
+\r
+#ifdef PNG_READ_sRGB_SUPPORTED\r
+void /* PRIVATE */\r
+png_handle_sRGB(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)\r
+{\r
+   int intent;\r
+   png_byte buf[1];\r
+\r
+   png_debug(1, "in png_handle_sRGB");\r
+\r
+   if (!(png_ptr->mode & PNG_HAVE_IHDR))\r
+      png_error(png_ptr, "Missing IHDR before sRGB");\r
+   else if (png_ptr->mode & PNG_HAVE_IDAT)\r
+   {\r
+      png_warning(png_ptr, "Invalid sRGB after IDAT");\r
+      png_crc_finish(png_ptr, length);\r
+      return;\r
+   }\r
+   else if (png_ptr->mode & PNG_HAVE_PLTE)\r
+      /* Should be an error, but we can cope with it */\r
+      png_warning(png_ptr, "Out of place sRGB chunk");\r
+\r
+   if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sRGB))\r
+   {\r
+      png_warning(png_ptr, "Duplicate sRGB chunk");\r
+      png_crc_finish(png_ptr, length);\r
+      return;\r
+   }\r
+\r
+   if (length != 1)\r
+   {\r
+      png_warning(png_ptr, "Incorrect sRGB chunk length");\r
+      png_crc_finish(png_ptr, length);\r
+      return;\r
+   }\r
+\r
+   png_crc_read(png_ptr, buf, 1);\r
+   if (png_crc_finish(png_ptr, 0))\r
+      return;\r
+\r
+   intent = buf[0];\r
+   /* Check for bad intent */\r
+   if (intent >= PNG_sRGB_INTENT_LAST)\r
+   {\r
+      png_warning(png_ptr, "Unknown sRGB intent");\r
+      return;\r
+   }\r
+\r
+#if defined(PNG_READ_gAMA_SUPPORTED) && defined(PNG_READ_GAMMA_SUPPORTED)\r
+   if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_gAMA))\r
+   {\r
+   png_fixed_point igamma;\r
+#ifdef PNG_FIXED_POINT_SUPPORTED\r
+      igamma=info_ptr->int_gamma;\r
+#else\r
+#  ifdef PNG_FLOATING_POINT_SUPPORTED\r
+      igamma=(png_fixed_point)(info_ptr->gamma * 100000.);\r
+#  endif\r
+#endif\r
+      if (PNG_OUT_OF_RANGE(igamma, 45500L, 500))\r
+      {\r
+         png_warning(png_ptr,\r
+           "Ignoring incorrect gAMA value when sRGB is also present");\r
+#ifdef PNG_CONSOLE_IO_SUPPORTED\r
+#  ifdef PNG_FIXED_POINT_SUPPORTED\r
+         fprintf(stderr, "incorrect gamma=(%d/100000)\n",\r
+            (int)png_ptr->int_gamma);\r
+#  else\r
+#    ifdef PNG_FLOATING_POINT_SUPPORTED\r
+         fprintf(stderr, "incorrect gamma=%f\n", png_ptr->gamma);\r
+#    endif\r
+#  endif\r
+#endif\r
+      }\r
+   }\r
+#endif /* PNG_READ_gAMA_SUPPORTED */\r
+\r
+#ifdef PNG_READ_cHRM_SUPPORTED\r
+#ifdef PNG_FIXED_POINT_SUPPORTED\r
+   if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_cHRM))\r
+      if (PNG_OUT_OF_RANGE(info_ptr->int_x_white, 31270,  1000) ||\r
+          PNG_OUT_OF_RANGE(info_ptr->int_y_white, 32900,  1000) ||\r
+          PNG_OUT_OF_RANGE(info_ptr->int_x_red,   64000L, 1000) ||\r
+          PNG_OUT_OF_RANGE(info_ptr->int_y_red,   33000,  1000) ||\r
+          PNG_OUT_OF_RANGE(info_ptr->int_x_green, 30000,  1000) ||\r
+          PNG_OUT_OF_RANGE(info_ptr->int_y_green, 60000L, 1000) ||\r
+          PNG_OUT_OF_RANGE(info_ptr->int_x_blue,  15000,  1000) ||\r
+          PNG_OUT_OF_RANGE(info_ptr->int_y_blue,   6000,  1000))\r
+         {\r
+            png_warning(png_ptr,\r
+              "Ignoring incorrect cHRM value when sRGB is also present");\r
+         }\r
+#endif /* PNG_FIXED_POINT_SUPPORTED */\r
+#endif /* PNG_READ_cHRM_SUPPORTED */\r
+\r
+   png_set_sRGB_gAMA_and_cHRM(png_ptr, info_ptr, intent);\r
+}\r
+#endif /* PNG_READ_sRGB_SUPPORTED */\r
+\r
+#ifdef PNG_READ_iCCP_SUPPORTED\r
+void /* PRIVATE */\r
+png_handle_iCCP(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)\r
+/* Note: this does not properly handle chunks that are > 64K under DOS */\r
+{\r
+   png_byte compression_type;\r
+   png_bytep pC;\r
+   png_charp profile;\r
+   png_uint_32 skip = 0;\r
+   png_uint_32 profile_size, profile_length;\r
+   png_size_t slength, prefix_length, data_length;\r
+\r
+   png_debug(1, "in png_handle_iCCP");\r
+\r
+   if (!(png_ptr->mode & PNG_HAVE_IHDR))\r
+      png_error(png_ptr, "Missing IHDR before iCCP");\r
+   else if (png_ptr->mode & PNG_HAVE_IDAT)\r
+   {\r
+      png_warning(png_ptr, "Invalid iCCP after IDAT");\r
+      png_crc_finish(png_ptr, length);\r
+      return;\r
+   }\r
+   else if (png_ptr->mode & PNG_HAVE_PLTE)\r
+      /* Should be an error, but we can cope with it */\r
+      png_warning(png_ptr, "Out of place iCCP chunk");\r
+\r
+   if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_iCCP))\r
+   {\r
+      png_warning(png_ptr, "Duplicate iCCP chunk");\r
+      png_crc_finish(png_ptr, length);\r
+      return;\r
+   }\r
+\r
+#ifdef PNG_MAX_MALLOC_64K\r
+   if (length > (png_uint_32)65535L)\r
+   {\r
+      png_warning(png_ptr, "iCCP chunk too large to fit in memory");\r
+      skip = length - (png_uint_32)65535L;\r
+      length = (png_uint_32)65535L;\r
+   }\r
+#endif\r
+\r
+   png_free(png_ptr, png_ptr->chunkdata);\r
+   png_ptr->chunkdata = (png_charp)png_malloc(png_ptr, length + 1);\r
+   slength = (png_size_t)length;\r
+   png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);\r
+\r
+   if (png_crc_finish(png_ptr, skip))\r
+   {\r
+      png_free(png_ptr, png_ptr->chunkdata);\r
+      png_ptr->chunkdata = NULL;\r
+      return;\r
+   }\r
+\r
+   png_ptr->chunkdata[slength] = 0x00;\r
+\r
+   for (profile = png_ptr->chunkdata; *profile; profile++)\r
+      /* Empty loop to find end of name */ ;\r
+\r
+   ++profile;\r
+\r
+   /* There should be at least one zero (the compression type byte)\r
+    * following the separator, and we should be on it\r
+    */\r
+   if ( profile >= png_ptr->chunkdata + slength - 1)\r
+   {\r
+      png_free(png_ptr, png_ptr->chunkdata);\r
+      png_ptr->chunkdata = NULL;\r
+      png_warning(png_ptr, "Malformed iCCP chunk");\r
+      return;\r
+   }\r
+\r
+   /* Compression_type should always be zero */\r
+   compression_type = *profile++;\r
+   if (compression_type)\r
+   {\r
+      png_warning(png_ptr, "Ignoring nonzero compression type in iCCP chunk");\r
+      compression_type = 0x00;  /* Reset it to zero (libpng-1.0.6 through 1.0.8\r
+                                 wrote nonzero) */\r
+   }\r
+\r
+   prefix_length = profile - png_ptr->chunkdata;\r
+   png_decompress_chunk(png_ptr, compression_type,\r
+     slength, prefix_length, &data_length);\r
+\r
+   profile_length = data_length - prefix_length;\r
+\r
+   if ( prefix_length > data_length || profile_length < 4)\r
+   {\r
+      png_free(png_ptr, png_ptr->chunkdata);\r
+      png_ptr->chunkdata = NULL;\r
+      png_warning(png_ptr, "Profile size field missing from iCCP chunk");\r
+      return;\r
+   }\r
+\r
+   /* Check the profile_size recorded in the first 32 bits of the ICC profile */\r
+   pC = (png_bytep)(png_ptr->chunkdata + prefix_length);\r
+   profile_size = ((*(pC    ))<<24) |\r
+                  ((*(pC + 1))<<16) |\r
+                  ((*(pC + 2))<< 8) |\r
+                  ((*(pC + 3))    );\r
+\r
+   if (profile_size < profile_length)\r
+      profile_length = profile_size;\r
+\r
+   if (profile_size > profile_length)\r
+   {\r
+      png_free(png_ptr, png_ptr->chunkdata);\r
+      png_ptr->chunkdata = NULL;\r
+      png_warning(png_ptr, "Ignoring truncated iCCP profile");\r
+#ifdef PNG_STDIO_SUPPORTED\r
+ {\r
+    char umsg[50];\r
+\r
+    png_snprintf(umsg, 50, "declared profile size = %lu",\r
+        (unsigned long)profile_size);\r
+    png_warning(png_ptr, umsg);\r
+    png_snprintf(umsg, 50, "actual profile length = %lu",\r
+        (unsigned long)profile_length);\r
+    png_warning(png_ptr, umsg);\r
+ }\r
+#endif\r
+      return;\r
+   }\r
+\r
+   png_set_iCCP(png_ptr, info_ptr, png_ptr->chunkdata,\r
+     compression_type, png_ptr->chunkdata + prefix_length, profile_length);\r
+   png_free(png_ptr, png_ptr->chunkdata);\r
+   png_ptr->chunkdata = NULL;\r
+}\r
+#endif /* PNG_READ_iCCP_SUPPORTED */\r
+\r
+#ifdef PNG_READ_sPLT_SUPPORTED\r
+void /* PRIVATE */\r
+png_handle_sPLT(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)\r
+/* Note: this does not properly handle chunks that are > 64K under DOS */\r
+{\r
+   png_bytep entry_start;\r
+   png_sPLT_t new_palette;\r
+#ifdef PNG_POINTER_INDEXING_SUPPORTED\r
+   png_sPLT_entryp pp;\r
+#endif\r
+   int data_length, entry_size, i;\r
+   png_uint_32 skip = 0;\r
+   png_size_t slength;\r
+\r
+   png_debug(1, "in png_handle_sPLT");\r
+\r
+#ifdef PNG_USER_LIMITS_SUPPORTED\r
+\r
+   if (png_ptr->user_chunk_cache_max != 0)\r
+   {\r
+      if (png_ptr->user_chunk_cache_max == 1)\r
+      {\r
+         png_crc_finish(png_ptr, length);\r
+         return;\r
+      }\r
+      if (--png_ptr->user_chunk_cache_max == 1)\r
+      {\r
+         png_warning(png_ptr, "No space in chunk cache for sPLT");\r
+         png_crc_finish(png_ptr, length);\r
+         return;\r
+      }\r
+   }\r
+#endif\r
+\r
+   if (!(png_ptr->mode & PNG_HAVE_IHDR))\r
+      png_error(png_ptr, "Missing IHDR before sPLT");\r
+   else if (png_ptr->mode & PNG_HAVE_IDAT)\r
+   {\r
+      png_warning(png_ptr, "Invalid sPLT after IDAT");\r
+      png_crc_finish(png_ptr, length);\r
+      return;\r
+   }\r
+\r
+#ifdef PNG_MAX_MALLOC_64K\r
+   if (length > (png_uint_32)65535L)\r
+   {\r
+      png_warning(png_ptr, "sPLT chunk too large to fit in memory");\r
+      skip = length - (png_uint_32)65535L;\r
+      length = (png_uint_32)65535L;\r
+   }\r
+#endif\r
+\r
+   png_free(png_ptr, png_ptr->chunkdata);\r
+   png_ptr->chunkdata = (png_charp)png_malloc(png_ptr, length + 1);\r
+   slength = (png_size_t)length;\r
+   png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);\r
+\r
+   if (png_crc_finish(png_ptr, skip))\r
+   {\r
+      png_free(png_ptr, png_ptr->chunkdata);\r
+      png_ptr->chunkdata = NULL;\r
+      return;\r
+   }\r
+\r
+   png_ptr->chunkdata[slength] = 0x00;\r
+\r
+   for (entry_start = (png_bytep)png_ptr->chunkdata; *entry_start;\r
+       entry_start++)\r
+      /* Empty loop to find end of name */ ;\r
+   ++entry_start;\r
+\r
+   /* A sample depth should follow the separator, and we should be on it  */\r
+   if (entry_start > (png_bytep)png_ptr->chunkdata + slength - 2)\r
+   {\r
+      png_free(png_ptr, png_ptr->chunkdata);\r
+      png_ptr->chunkdata = NULL;\r
+      png_warning(png_ptr, "malformed sPLT chunk");\r
+      return;\r
+   }\r
+\r
+   new_palette.depth = *entry_start++;\r
+   entry_size = (new_palette.depth == 8 ? 6 : 10);\r
+   data_length = (slength - (entry_start - (png_bytep)png_ptr->chunkdata));\r
+\r
+   /* Integrity-check the data length */\r
+   if (data_length % entry_size)\r
+   {\r
+      png_free(png_ptr, png_ptr->chunkdata);\r
+      png_ptr->chunkdata = NULL;\r
+      png_warning(png_ptr, "sPLT chunk has bad length");\r
+      return;\r
+   }\r
+\r
+   new_palette.nentries = (png_int_32) ( data_length / entry_size);\r
+   if ((png_uint_32) new_palette.nentries >\r
+       (png_uint_32) (PNG_SIZE_MAX / png_sizeof(png_sPLT_entry)))\r
+   {\r
+       png_warning(png_ptr, "sPLT chunk too long");\r
+       return;\r
+   }\r
+   new_palette.entries = (png_sPLT_entryp)png_malloc_warn(\r
+       png_ptr, new_palette.nentries * png_sizeof(png_sPLT_entry));\r
+   if (new_palette.entries == NULL)\r
+   {\r
+       png_warning(png_ptr, "sPLT chunk requires too much memory");\r
+       return;\r
+   }\r
+\r
+#ifdef PNG_POINTER_INDEXING_SUPPORTED\r
+   for (i = 0; i < new_palette.nentries; i++)\r
+   {\r
+      pp = new_palette.entries + i;\r
+\r
+      if (new_palette.depth == 8)\r
+      {\r
+          pp->red = *entry_start++;\r
+          pp->green = *entry_start++;\r
+          pp->blue = *entry_start++;\r
+          pp->alpha = *entry_start++;\r
+      }\r
+      else\r
+      {\r
+          pp->red   = png_get_uint_16(entry_start); entry_start += 2;\r
+          pp->green = png_get_uint_16(entry_start); entry_start += 2;\r
+          pp->blue  = png_get_uint_16(entry_start); entry_start += 2;\r
+          pp->alpha = png_get_uint_16(entry_start); entry_start += 2;\r
+      }\r
+      pp->frequency = png_get_uint_16(entry_start); entry_start += 2;\r
+   }\r
+#else\r
+   pp = new_palette.entries;\r
+   for (i = 0; i < new_palette.nentries; i++)\r
+   {\r
+\r
+      if (new_palette.depth == 8)\r
+      {\r
+          pp[i].red   = *entry_start++;\r
+          pp[i].green = *entry_start++;\r
+          pp[i].blue  = *entry_start++;\r
+          pp[i].alpha = *entry_start++;\r
+      }\r
+      else\r
+      {\r
+          pp[i].red   = png_get_uint_16(entry_start); entry_start += 2;\r
+          pp[i].green = png_get_uint_16(entry_start); entry_start += 2;\r
+          pp[i].blue  = png_get_uint_16(entry_start); entry_start += 2;\r
+          pp[i].alpha = png_get_uint_16(entry_start); entry_start += 2;\r
+      }\r
+      pp->frequency = png_get_uint_16(entry_start); entry_start += 2;\r
+   }\r
+#endif\r
+\r
+   /* Discard all chunk data except the name and stash that */\r
+   new_palette.name = png_ptr->chunkdata;\r
+\r
+   png_set_sPLT(png_ptr, info_ptr, &new_palette, 1);\r
+\r
+   png_free(png_ptr, png_ptr->chunkdata);\r
+   png_ptr->chunkdata = NULL;\r
+   png_free(png_ptr, new_palette.entries);\r
+}\r
+#endif /* PNG_READ_sPLT_SUPPORTED */\r
+\r
+#ifdef PNG_READ_tRNS_SUPPORTED\r
+void /* PRIVATE */\r
+png_handle_tRNS(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)\r
+{\r
+   png_byte readbuf[PNG_MAX_PALETTE_LENGTH];\r
+\r
+   png_debug(1, "in png_handle_tRNS");\r
+\r
+   if (!(png_ptr->mode & PNG_HAVE_IHDR))\r
+      png_error(png_ptr, "Missing IHDR before tRNS");\r
+   else if (png_ptr->mode & PNG_HAVE_IDAT)\r
+   {\r
+      png_warning(png_ptr, "Invalid tRNS after IDAT");\r
+      png_crc_finish(png_ptr, length);\r
+      return;\r
+   }\r
+   else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS))\r
+   {\r
+      png_warning(png_ptr, "Duplicate tRNS chunk");\r
+      png_crc_finish(png_ptr, length);\r
+      return;\r
+   }\r
+\r
+   if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)\r
+   {\r
+      png_byte buf[2];\r
+\r
+      if (length != 2)\r
+      {\r
+         png_warning(png_ptr, "Incorrect tRNS chunk length");\r
+         png_crc_finish(png_ptr, length);\r
+         return;\r
+      }\r
+\r
+      png_crc_read(png_ptr, buf, 2);\r
+      png_ptr->num_trans = 1;\r
+      png_ptr->trans_color.gray = png_get_uint_16(buf);\r
+   }\r
+   else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)\r
+   {\r
+      png_byte buf[6];\r
+\r
+      if (length != 6)\r
+      {\r
+         png_warning(png_ptr, "Incorrect tRNS chunk length");\r
+         png_crc_finish(png_ptr, length);\r
+         return;\r
+      }\r
+      png_crc_read(png_ptr, buf, (png_size_t)length);\r
+      png_ptr->num_trans = 1;\r
+      png_ptr->trans_color.red = png_get_uint_16(buf);\r
+      png_ptr->trans_color.green = png_get_uint_16(buf + 2);\r
+      png_ptr->trans_color.blue = png_get_uint_16(buf + 4);\r
+   }\r
+   else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)\r
+   {\r
+      if (!(png_ptr->mode & PNG_HAVE_PLTE))\r
+      {\r
+         /* Should be an error, but we can cope with it. */\r
+         png_warning(png_ptr, "Missing PLTE before tRNS");\r
+      }\r
+      if (length > (png_uint_32)png_ptr->num_palette ||\r
+          length > PNG_MAX_PALETTE_LENGTH)\r
+      {\r
+         png_warning(png_ptr, "Incorrect tRNS chunk length");\r
+         png_crc_finish(png_ptr, length);\r
+         return;\r
+      }\r
+      if (length == 0)\r
+      {\r
+         png_warning(png_ptr, "Zero length tRNS chunk");\r
+         png_crc_finish(png_ptr, length);\r
+         return;\r
+      }\r
+      png_crc_read(png_ptr, readbuf, (png_size_t)length);\r
+      png_ptr->num_trans = (png_uint_16)length;\r
+   }\r
+   else\r
+   {\r
+      png_warning(png_ptr, "tRNS chunk not allowed with alpha channel");\r
+      png_crc_finish(png_ptr, length);\r
+      return;\r
+   }\r
+\r
+   if (png_crc_finish(png_ptr, 0))\r
+   {\r
+      png_ptr->num_trans = 0;\r
+      return;\r
+   }\r
+\r
+   png_set_tRNS(png_ptr, info_ptr, readbuf, png_ptr->num_trans,\r
+      &(png_ptr->trans_color));\r
+}\r
+#endif\r
+\r
+#ifdef PNG_READ_bKGD_SUPPORTED\r
+void /* PRIVATE */\r
+png_handle_bKGD(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)\r
+{\r
+   png_size_t truelen;\r
+   png_byte buf[6];\r
+\r
+   png_debug(1, "in png_handle_bKGD");\r
+\r
+   if (!(png_ptr->mode & PNG_HAVE_IHDR))\r
+      png_error(png_ptr, "Missing IHDR before bKGD");\r
+   else if (png_ptr->mode & PNG_HAVE_IDAT)\r
+   {\r
+      png_warning(png_ptr, "Invalid bKGD after IDAT");\r
+      png_crc_finish(png_ptr, length);\r
+      return;\r
+   }\r
+   else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&\r
+            !(png_ptr->mode & PNG_HAVE_PLTE))\r
+   {\r
+      png_warning(png_ptr, "Missing PLTE before bKGD");\r
+      png_crc_finish(png_ptr, length);\r
+      return;\r
+   }\r
+   else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_bKGD))\r
+   {\r
+      png_warning(png_ptr, "Duplicate bKGD chunk");\r
+      png_crc_finish(png_ptr, length);\r
+      return;\r
+   }\r
+\r
+   if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)\r
+      truelen = 1;\r
+   else if (png_ptr->color_type & PNG_COLOR_MASK_COLOR)\r
+      truelen = 6;\r
+   else\r
+      truelen = 2;\r
+\r
+   if (length != truelen)\r
+   {\r
+      png_warning(png_ptr, "Incorrect bKGD chunk length");\r
+      png_crc_finish(png_ptr, length);\r
+      return;\r
+   }\r
+\r
+   png_crc_read(png_ptr, buf, truelen);\r
+   if (png_crc_finish(png_ptr, 0))\r
+      return;\r
+\r
+   /* We convert the index value into RGB components so that we can allow\r
+    * arbitrary RGB values for background when we have transparency, and\r
+    * so it is easy to determine the RGB values of the background color\r
+    * from the info_ptr struct. */\r
+   if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)\r
+   {\r
+      png_ptr->background.index = buf[0];\r
+      if (info_ptr && info_ptr->num_palette)\r
+      {\r
+          if (buf[0] >= info_ptr->num_palette)\r
+          {\r
+             png_warning(png_ptr, "Incorrect bKGD chunk index value");\r
+             return;\r
+          }\r
+          png_ptr->background.red =\r
+             (png_uint_16)png_ptr->palette[buf[0]].red;\r
+          png_ptr->background.green =\r
+             (png_uint_16)png_ptr->palette[buf[0]].green;\r
+          png_ptr->background.blue =\r
+             (png_uint_16)png_ptr->palette[buf[0]].blue;\r
+      }\r
+   }\r
+   else if (!(png_ptr->color_type & PNG_COLOR_MASK_COLOR)) /* GRAY */\r
+   {\r
+      png_ptr->background.red =\r
+      png_ptr->background.green =\r
+      png_ptr->background.blue =\r
+      png_ptr->background.gray = png_get_uint_16(buf);\r
+   }\r
+   else\r
+   {\r
+      png_ptr->background.red = png_get_uint_16(buf);\r
+      png_ptr->background.green = png_get_uint_16(buf + 2);\r
+      png_ptr->background.blue = png_get_uint_16(buf + 4);\r
+   }\r
+\r
+   png_set_bKGD(png_ptr, info_ptr, &(png_ptr->background));\r
+}\r
+#endif\r
+\r
+#ifdef PNG_READ_hIST_SUPPORTED\r
+void /* PRIVATE */\r
+png_handle_hIST(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)\r
+{\r
+   unsigned int num, i;\r
+   png_uint_16 readbuf[PNG_MAX_PALETTE_LENGTH];\r
+\r
+   png_debug(1, "in png_handle_hIST");\r
+\r
+   if (!(png_ptr->mode & PNG_HAVE_IHDR))\r
+      png_error(png_ptr, "Missing IHDR before hIST");\r
+   else if (png_ptr->mode & PNG_HAVE_IDAT)\r
+   {\r
+      png_warning(png_ptr, "Invalid hIST after IDAT");\r
+      png_crc_finish(png_ptr, length);\r
+      return;\r
+   }\r
+   else if (!(png_ptr->mode & PNG_HAVE_PLTE))\r
+   {\r
+      png_warning(png_ptr, "Missing PLTE before hIST");\r
+      png_crc_finish(png_ptr, length);\r
+      return;\r
+   }\r
+   else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_hIST))\r
+   {\r
+      png_warning(png_ptr, "Duplicate hIST chunk");\r
+      png_crc_finish(png_ptr, length);\r
+      return;\r
+   }\r
+\r
+   num = length / 2 ;\r
+   if (num != (unsigned int) png_ptr->num_palette || num >\r
+      (unsigned int) PNG_MAX_PALETTE_LENGTH)\r
+   {\r
+      png_warning(png_ptr, "Incorrect hIST chunk length");\r
+      png_crc_finish(png_ptr, length);\r
+      return;\r
+   }\r
+\r
+   for (i = 0; i < num; i++)\r
+   {\r
+      png_byte buf[2];\r
+\r
+      png_crc_read(png_ptr, buf, 2);\r
+      readbuf[i] = png_get_uint_16(buf);\r
+   }\r
+\r
+   if (png_crc_finish(png_ptr, 0))\r
+      return;\r
+\r
+   png_set_hIST(png_ptr, info_ptr, readbuf);\r
+}\r
+#endif\r
+\r
+#ifdef PNG_READ_pHYs_SUPPORTED\r
+void /* PRIVATE */\r
+png_handle_pHYs(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)\r
+{\r
+   png_byte buf[9];\r
+   png_uint_32 res_x, res_y;\r
+   int unit_type;\r
+\r
+   png_debug(1, "in png_handle_pHYs");\r
+\r
+   if (!(png_ptr->mode & PNG_HAVE_IHDR))\r
+      png_error(png_ptr, "Missing IHDR before pHYs");\r
+   else if (png_ptr->mode & PNG_HAVE_IDAT)\r
+   {\r
+      png_warning(png_ptr, "Invalid pHYs after IDAT");\r
+      png_crc_finish(png_ptr, length);\r
+      return;\r
+   }\r
+   else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pHYs))\r
+   {\r
+      png_warning(png_ptr, "Duplicate pHYs chunk");\r
+      png_crc_finish(png_ptr, length);\r
+      return;\r
+   }\r
+\r
+   if (length != 9)\r
+   {\r
+      png_warning(png_ptr, "Incorrect pHYs chunk length");\r
+      png_crc_finish(png_ptr, length);\r
+      return;\r
+   }\r
+\r
+   png_crc_read(png_ptr, buf, 9);\r
+   if (png_crc_finish(png_ptr, 0))\r
+      return;\r
+\r
+   res_x = png_get_uint_32(buf);\r
+   res_y = png_get_uint_32(buf + 4);\r
+   unit_type = buf[8];\r
+   png_set_pHYs(png_ptr, info_ptr, res_x, res_y, unit_type);\r
+}\r
+#endif\r
+\r
+#ifdef PNG_READ_oFFs_SUPPORTED\r
+void /* PRIVATE */\r
+png_handle_oFFs(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)\r
+{\r
+   png_byte buf[9];\r
+   png_int_32 offset_x, offset_y;\r
+   int unit_type;\r
+\r
+   png_debug(1, "in png_handle_oFFs");\r
+\r
+   if (!(png_ptr->mode & PNG_HAVE_IHDR))\r
+      png_error(png_ptr, "Missing IHDR before oFFs");\r
+   else if (png_ptr->mode & PNG_HAVE_IDAT)\r
+   {\r
+      png_warning(png_ptr, "Invalid oFFs after IDAT");\r
+      png_crc_finish(png_ptr, length);\r
+      return;\r
+   }\r
+   else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_oFFs))\r
+   {\r
+      png_warning(png_ptr, "Duplicate oFFs chunk");\r
+      png_crc_finish(png_ptr, length);\r
+      return;\r
+   }\r
+\r
+   if (length != 9)\r
+   {\r
+      png_warning(png_ptr, "Incorrect oFFs chunk length");\r
+      png_crc_finish(png_ptr, length);\r
+      return;\r
+   }\r
+\r
+   png_crc_read(png_ptr, buf, 9);\r
+   if (png_crc_finish(png_ptr, 0))\r
+      return;\r
+\r
+   offset_x = png_get_int_32(buf);\r
+   offset_y = png_get_int_32(buf + 4);\r
+   unit_type = buf[8];\r
+   png_set_oFFs(png_ptr, info_ptr, offset_x, offset_y, unit_type);\r
+}\r
+#endif\r
+\r
+#ifdef PNG_READ_pCAL_SUPPORTED\r
+/* Read the pCAL chunk (described in the PNG Extensions document) */\r
+void /* PRIVATE */\r
+png_handle_pCAL(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)\r
+{\r
+   png_int_32 X0, X1;\r
+   png_byte type, nparams;\r
+   png_charp buf, units, endptr;\r
+   png_charpp params;\r
+   png_size_t slength;\r
+   int i;\r
+\r
+   png_debug(1, "in png_handle_pCAL");\r
+\r
+   if (!(png_ptr->mode & PNG_HAVE_IHDR))\r
+      png_error(png_ptr, "Missing IHDR before pCAL");\r
+   else if (png_ptr->mode & PNG_HAVE_IDAT)\r
+   {\r
+      png_warning(png_ptr, "Invalid pCAL after IDAT");\r
+      png_crc_finish(png_ptr, length);\r
+      return;\r
+   }\r
+   else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pCAL))\r
+   {\r
+      png_warning(png_ptr, "Duplicate pCAL chunk");\r
+      png_crc_finish(png_ptr, length);\r
+      return;\r
+   }\r
+\r
+   png_debug1(2, "Allocating and reading pCAL chunk data (%lu bytes)",\r
+      length + 1);\r
+   png_free(png_ptr, png_ptr->chunkdata);\r
+   png_ptr->chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1);\r
+   if (png_ptr->chunkdata == NULL)\r
+     {\r
+       png_warning(png_ptr, "No memory for pCAL purpose");\r
+       return;\r
+     }\r
+   slength = (png_size_t)length;\r
+   png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);\r
+\r
+   if (png_crc_finish(png_ptr, 0))\r
+   {\r
+      png_free(png_ptr, png_ptr->chunkdata);\r
+      png_ptr->chunkdata = NULL;\r
+      return;\r
+   }\r
+\r
+   png_ptr->chunkdata[slength] = 0x00; /* Null terminate the last string */\r
+\r
+   png_debug(3, "Finding end of pCAL purpose string");\r
+   for (buf = png_ptr->chunkdata; *buf; buf++)\r
+      /* Empty loop */ ;\r
+\r
+   endptr = png_ptr->chunkdata + slength;\r
+\r
+   /* We need to have at least 12 bytes after the purpose string\r
+      in order to get the parameter information. */\r
+   if (endptr <= buf + 12)\r
+   {\r
+      png_warning(png_ptr, "Invalid pCAL data");\r
+      png_free(png_ptr, png_ptr->chunkdata);\r
+      png_ptr->chunkdata = NULL;\r
+      return;\r
+   }\r
+\r
+   png_debug(3, "Reading pCAL X0, X1, type, nparams, and units");\r
+   X0 = png_get_int_32((png_bytep)buf+1);\r
+   X1 = png_get_int_32((png_bytep)buf+5);\r
+   type = buf[9];\r
+   nparams = buf[10];\r
+   units = buf + 11;\r
+\r
+   png_debug(3, "Checking pCAL equation type and number of parameters");\r
+   /* Check that we have the right number of parameters for known\r
+      equation types. */\r
+   if ((type == PNG_EQUATION_LINEAR && nparams != 2) ||\r
+       (type == PNG_EQUATION_BASE_E && nparams != 3) ||\r
+       (type == PNG_EQUATION_ARBITRARY && nparams != 3) ||\r
+       (type == PNG_EQUATION_HYPERBOLIC && nparams != 4))\r
+   {\r
+      png_warning(png_ptr, "Invalid pCAL parameters for equation type");\r
+      png_free(png_ptr, png_ptr->chunkdata);\r
+      png_ptr->chunkdata = NULL;\r
+      return;\r
+   }\r
+   else if (type >= PNG_EQUATION_LAST)\r
+   {\r
+      png_warning(png_ptr, "Unrecognized equation type for pCAL chunk");\r
+   }\r
+\r
+   for (buf = units; *buf; buf++)\r
+      /* Empty loop to move past the units string. */ ;\r
+\r
+   png_debug(3, "Allocating pCAL parameters array");\r
+   params = (png_charpp)png_malloc_warn(png_ptr,\r
+      (png_size_t)(nparams * png_sizeof(png_charp)));\r
+   if (params == NULL)\r
+     {\r
+       png_free(png_ptr, png_ptr->chunkdata);\r
+       png_ptr->chunkdata = NULL;\r
+       png_warning(png_ptr, "No memory for pCAL params");\r
+       return;\r
+     }\r
+\r
+   /* Get pointers to the start of each parameter string. */\r
+   for (i = 0; i < (int)nparams; i++)\r
+   {\r
+      buf++; /* Skip the null string terminator from previous parameter. */\r
+\r
+      png_debug1(3, "Reading pCAL parameter %d", i);\r
+      for (params[i] = buf; buf <= endptr && *buf != 0x00; buf++)\r
+         /* Empty loop to move past each parameter string */ ;\r
+\r
+      /* Make sure we haven't run out of data yet */\r
+      if (buf > endptr)\r
+      {\r
+         png_warning(png_ptr, "Invalid pCAL data");\r
+         png_free(png_ptr, png_ptr->chunkdata);\r
+         png_ptr->chunkdata = NULL;\r
+         png_free(png_ptr, params);\r
+         return;\r
+      }\r
+   }\r
+\r
+   png_set_pCAL(png_ptr, info_ptr, png_ptr->chunkdata, X0, X1, type, nparams,\r
+      units, params);\r
+\r
+   png_free(png_ptr, png_ptr->chunkdata);\r
+   png_ptr->chunkdata = NULL;\r
+   png_free(png_ptr, params);\r
+}\r
+#endif\r
+\r
+#ifdef PNG_READ_sCAL_SUPPORTED\r
+/* Read the sCAL chunk */\r
+void /* PRIVATE */\r
+png_handle_sCAL(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)\r
+{\r
+   png_charp ep;\r
+#ifdef PNG_FLOATING_POINT_SUPPORTED\r
+   double width, height;\r
+   png_charp vp;\r
+#else\r
+#ifdef PNG_FIXED_POINT_SUPPORTED\r
+   png_charp swidth, sheight;\r
+#endif\r
+#endif\r
+   png_size_t slength;\r
+\r
+   png_debug(1, "in png_handle_sCAL");\r
+\r
+   if (!(png_ptr->mode & PNG_HAVE_IHDR))\r
+      png_error(png_ptr, "Missing IHDR before sCAL");\r
+   else if (png_ptr->mode & PNG_HAVE_IDAT)\r
+   {\r
+      png_warning(png_ptr, "Invalid sCAL after IDAT");\r
+      png_crc_finish(png_ptr, length);\r
+      return;\r
+   }\r
+   else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sCAL))\r
+   {\r
+      png_warning(png_ptr, "Duplicate sCAL chunk");\r
+      png_crc_finish(png_ptr, length);\r
+      return;\r
+   }\r
+\r
+   png_debug1(2, "Allocating and reading sCAL chunk data (%lu bytes)",\r
+      length + 1);\r
+   png_ptr->chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1);\r
+   if (png_ptr->chunkdata == NULL)\r
+   {\r
+      png_warning(png_ptr, "Out of memory while processing sCAL chunk");\r
+      png_crc_finish(png_ptr, length);\r
+      return;\r
+   }\r
+   slength = (png_size_t)length;\r
+   png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);\r
+\r
+   if (png_crc_finish(png_ptr, 0))\r
+   {\r
+      png_free(png_ptr, png_ptr->chunkdata);\r
+      png_ptr->chunkdata = NULL;\r
+      return;\r
+   }\r
+\r
+   png_ptr->chunkdata[slength] = 0x00; /* Null terminate the last string */\r
+\r
+   ep = png_ptr->chunkdata + 1;        /* Skip unit byte */\r
+\r
+#ifdef PNG_FLOATING_POINT_SUPPORTED\r
+   width = png_strtod(png_ptr, ep, &vp);\r
+   if (*vp)\r
+   {\r
+      png_warning(png_ptr, "malformed width string in sCAL chunk");\r
+      png_free(png_ptr, png_ptr->chunkdata);\r
+      png_ptr->chunkdata = NULL;\r
+      return;\r
+   }\r
+#else\r
+#ifdef PNG_FIXED_POINT_SUPPORTED\r
+   swidth = (png_charp)png_malloc_warn(png_ptr, png_strlen(ep) + 1);\r
+   if (swidth == NULL)\r
+   {\r
+      png_warning(png_ptr, "Out of memory while processing sCAL chunk width");\r
+      png_free(png_ptr, png_ptr->chunkdata);\r
+      png_ptr->chunkdata = NULL;\r
+      return;\r
+   }\r
+   png_memcpy(swidth, ep, png_strlen(ep));\r
+#endif\r
+#endif\r
+\r
+   for (ep = png_ptr->chunkdata; *ep; ep++)\r
+      /* Empty loop */ ;\r
+   ep++;\r
+\r
+   if (png_ptr->chunkdata + slength < ep)\r
+   {\r
+      png_warning(png_ptr, "Truncated sCAL chunk");\r
+#if defined(PNG_FIXED_POINT_SUPPORTED) && !defined(PNG_FLOATING_POINT_SUPPORTED)\r
+      png_free(png_ptr, swidth);\r
+#endif\r
+      png_free(png_ptr, png_ptr->chunkdata);\r
+      png_ptr->chunkdata = NULL;\r
+      return;\r
+   }\r
+\r
+#ifdef PNG_FLOATING_POINT_SUPPORTED\r
+   height = png_strtod(png_ptr, ep, &vp);\r
+   if (*vp)\r
+   {\r
+      png_warning(png_ptr, "malformed height string in sCAL chunk");\r
+      png_free(png_ptr, png_ptr->chunkdata);\r
+      png_ptr->chunkdata = NULL;\r
+#if defined(PNG_FIXED_POINT_SUPPORTED) && !defined(PNG_FLOATING_POINT_SUPPORTED)\r
+      png_free(png_ptr, swidth);\r
+#endif\r
+      return;\r
+   }\r
+#else\r
+#ifdef PNG_FIXED_POINT_SUPPORTED\r
+   sheight = (png_charp)png_malloc_warn(png_ptr, png_strlen(ep) + 1);\r
+   if (sheight == NULL)\r
+   {\r
+      png_warning(png_ptr, "Out of memory while processing sCAL chunk height");\r
+      png_free(png_ptr, png_ptr->chunkdata);\r
+      png_ptr->chunkdata = NULL;\r
+#if defined(PNG_FIXED_POINT_SUPPORTED) && !defined(PNG_FLOATING_POINT_SUPPORTED)\r
+      png_free(png_ptr, swidth);\r
+#endif\r
+      return;\r
+   }\r
+   png_memcpy(sheight, ep, png_strlen(ep));\r
+#endif\r
+#endif\r
+\r
+   if (png_ptr->chunkdata + slength < ep\r
+#ifdef PNG_FLOATING_POINT_SUPPORTED\r
+      || width <= 0. || height <= 0.\r
+#endif\r
+      )\r
+   {\r
+      png_warning(png_ptr, "Invalid sCAL data");\r
+      png_free(png_ptr, png_ptr->chunkdata);\r
+      png_ptr->chunkdata = NULL;\r
+#if defined(PNG_FIXED_POINT_SUPPORTED) && !defined(PNG_FLOATING_POINT_SUPPORTED)\r
+      png_free(png_ptr, swidth);\r
+      png_free(png_ptr, sheight);\r
+#endif\r
+      return;\r
+   }\r
+\r
+\r
+#ifdef PNG_FLOATING_POINT_SUPPORTED\r
+   png_set_sCAL(png_ptr, info_ptr, png_ptr->chunkdata[0], width, height);\r
+#else\r
+#ifdef PNG_FIXED_POINT_SUPPORTED\r
+   png_set_sCAL_s(png_ptr, info_ptr, png_ptr->chunkdata[0], swidth, sheight);\r
+#endif\r
+#endif\r
+\r
+   png_free(png_ptr, png_ptr->chunkdata);\r
+   png_ptr->chunkdata = NULL;\r
+#if defined(PNG_FIXED_POINT_SUPPORTED) && !defined(PNG_FLOATING_POINT_SUPPORTED)\r
+   png_free(png_ptr, swidth);\r
+   png_free(png_ptr, sheight);\r
+#endif\r
+}\r
+#endif\r
+\r
+#ifdef PNG_READ_tIME_SUPPORTED\r
+void /* PRIVATE */\r
+png_handle_tIME(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)\r
+{\r
+   png_byte buf[7];\r
+   png_time mod_time;\r
+\r
+   png_debug(1, "in png_handle_tIME");\r
+\r
+   if (!(png_ptr->mode & PNG_HAVE_IHDR))\r
+      png_error(png_ptr, "Out of place tIME chunk");\r
+   else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tIME))\r
+   {\r
+      png_warning(png_ptr, "Duplicate tIME chunk");\r
+      png_crc_finish(png_ptr, length);\r
+      return;\r
+   }\r
+\r
+   if (png_ptr->mode & PNG_HAVE_IDAT)\r
+      png_ptr->mode |= PNG_AFTER_IDAT;\r
+\r
+   if (length != 7)\r
+   {\r
+      png_warning(png_ptr, "Incorrect tIME chunk length");\r
+      png_crc_finish(png_ptr, length);\r
+      return;\r
+   }\r
+\r
+   png_crc_read(png_ptr, buf, 7);\r
+   if (png_crc_finish(png_ptr, 0))\r
+      return;\r
+\r
+   mod_time.second = buf[6];\r
+   mod_time.minute = buf[5];\r
+   mod_time.hour = buf[4];\r
+   mod_time.day = buf[3];\r
+   mod_time.month = buf[2];\r
+   mod_time.year = png_get_uint_16(buf);\r
+\r
+   png_set_tIME(png_ptr, info_ptr, &mod_time);\r
+}\r
+#endif\r
+\r
+#ifdef PNG_READ_tEXt_SUPPORTED\r
+/* Note: this does not properly handle chunks that are > 64K under DOS */\r
+void /* PRIVATE */\r
+png_handle_tEXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)\r
+{\r
+   png_textp text_ptr;\r
+   png_charp key;\r
+   png_charp text;\r
+   png_uint_32 skip = 0;\r
+   png_size_t slength;\r
+   int ret;\r
+\r
+   png_debug(1, "in png_handle_tEXt");\r
+\r
+#ifdef PNG_USER_LIMITS_SUPPORTED\r
+   if (png_ptr->user_chunk_cache_max != 0)\r
+   {\r
+      if (png_ptr->user_chunk_cache_max == 1)\r
+      {\r
+         png_crc_finish(png_ptr, length);\r
+         return;\r
+      }\r
+      if (--png_ptr->user_chunk_cache_max == 1)\r
+      {\r
+         png_warning(png_ptr, "No space in chunk cache for tEXt");\r
+         png_crc_finish(png_ptr, length);\r
+         return;\r
+      }\r
+   }\r
+#endif\r
+\r
+   if (!(png_ptr->mode & PNG_HAVE_IHDR))\r
+      png_error(png_ptr, "Missing IHDR before tEXt");\r
+\r
+   if (png_ptr->mode & PNG_HAVE_IDAT)\r
+      png_ptr->mode |= PNG_AFTER_IDAT;\r
+\r
+#ifdef PNG_MAX_MALLOC_64K\r
+   if (length > (png_uint_32)65535L)\r
+   {\r
+      png_warning(png_ptr, "tEXt chunk too large to fit in memory");\r
+      skip = length - (png_uint_32)65535L;\r
+      length = (png_uint_32)65535L;\r
+   }\r
+#endif\r
+\r
+   png_free(png_ptr, png_ptr->chunkdata);\r
+\r
+   png_ptr->chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1);\r
+   if (png_ptr->chunkdata == NULL)\r
+   {\r
+     png_warning(png_ptr, "No memory to process text chunk");\r
+     return;\r
+   }\r
+   slength = (png_size_t)length;\r
+   png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);\r
+\r
+   if (png_crc_finish(png_ptr, skip))\r
+   {\r
+      png_free(png_ptr, png_ptr->chunkdata);\r
+      png_ptr->chunkdata = NULL;\r
+      return;\r
+   }\r
+\r
+   key = png_ptr->chunkdata;\r
+\r
+   key[slength] = 0x00;\r
+\r
+   for (text = key; *text; text++)\r
+      /* Empty loop to find end of key */ ;\r
+\r
+   if (text != key + slength)\r
+      text++;\r
+\r
+   text_ptr = (png_textp)png_malloc_warn(png_ptr,\r
+      png_sizeof(png_text));\r
+   if (text_ptr == NULL)\r
+   {\r
+     png_warning(png_ptr, "Not enough memory to process text chunk");\r
+     png_free(png_ptr, png_ptr->chunkdata);\r
+     png_ptr->chunkdata = NULL;\r
+     return;\r
+   }\r
+   text_ptr->compression = PNG_TEXT_COMPRESSION_NONE;\r
+   text_ptr->key = key;\r
+#ifdef PNG_iTXt_SUPPORTED\r
+   text_ptr->lang = NULL;\r
+   text_ptr->lang_key = NULL;\r
+   text_ptr->itxt_length = 0;\r
+#endif\r
+   text_ptr->text = text;\r
+   text_ptr->text_length = png_strlen(text);\r
+\r
+   ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1);\r
+\r
+   png_free(png_ptr, png_ptr->chunkdata);\r
+   png_ptr->chunkdata = NULL;\r
+   png_free(png_ptr, text_ptr);\r
+   if (ret)\r
+     png_warning(png_ptr, "Insufficient memory to process text chunk");\r
+}\r
+#endif\r
+\r
+#ifdef PNG_READ_zTXt_SUPPORTED\r
+/* Note: this does not correctly handle chunks that are > 64K under DOS */\r
+void /* PRIVATE */\r
+png_handle_zTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)\r
+{\r
+   png_textp text_ptr;\r
+   png_charp text;\r
+   int comp_type;\r
+   int ret;\r
+   png_size_t slength, prefix_len, data_len;\r
+\r
+   png_debug(1, "in png_handle_zTXt");\r
+\r
+#ifdef PNG_USER_LIMITS_SUPPORTED\r
+   if (png_ptr->user_chunk_cache_max != 0)\r
+   {\r
+      if (png_ptr->user_chunk_cache_max == 1)\r
+      {\r
+         png_crc_finish(png_ptr, length);\r
+         return;\r
+      }\r
+      if (--png_ptr->user_chunk_cache_max == 1)\r
+      {\r
+         png_warning(png_ptr, "No space in chunk cache for zTXt");\r
+         png_crc_finish(png_ptr, length);\r
+         return;\r
+      }\r
+   }\r
+#endif\r
+\r
+   if (!(png_ptr->mode & PNG_HAVE_IHDR))\r
+      png_error(png_ptr, "Missing IHDR before zTXt");\r
+\r
+   if (png_ptr->mode & PNG_HAVE_IDAT)\r
+      png_ptr->mode |= PNG_AFTER_IDAT;\r
+\r
+#ifdef PNG_MAX_MALLOC_64K\r
+   /* We will no doubt have problems with chunks even half this size, but\r
+      there is no hard and fast rule to tell us where to stop. */\r
+   if (length > (png_uint_32)65535L)\r
+   {\r
+     png_warning(png_ptr, "zTXt chunk too large to fit in memory");\r
+     png_crc_finish(png_ptr, length);\r
+     return;\r
+   }\r
+#endif\r
+\r
+   png_free(png_ptr, png_ptr->chunkdata);\r
+   png_ptr->chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1);\r
+   if (png_ptr->chunkdata == NULL)\r
+   {\r
+     png_warning(png_ptr, "Out of memory processing zTXt chunk");\r
+     return;\r
+   }\r
+   slength = (png_size_t)length;\r
+   png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);\r
+   if (png_crc_finish(png_ptr, 0))\r
+   {\r
+      png_free(png_ptr, png_ptr->chunkdata);\r
+      png_ptr->chunkdata = NULL;\r
+      return;\r
+   }\r
+\r
+   png_ptr->chunkdata[slength] = 0x00;\r
+\r
+   for (text = png_ptr->chunkdata; *text; text++)\r
+      /* Empty loop */ ;\r
+\r
+   /* zTXt must have some text after the chunkdataword */\r
+   if (text >= png_ptr->chunkdata + slength - 2)\r
+   {\r
+      png_warning(png_ptr, "Truncated zTXt chunk");\r
+      png_free(png_ptr, png_ptr->chunkdata);\r
+      png_ptr->chunkdata = NULL;\r
+      return;\r
+   }\r
+   else\r
+   {\r
+       comp_type = *(++text);\r
+       if (comp_type != PNG_TEXT_COMPRESSION_zTXt)\r
+       {\r
+          png_warning(png_ptr, "Unknown compression type in zTXt chunk");\r
+          comp_type = PNG_TEXT_COMPRESSION_zTXt;\r
+       }\r
+       text++;        /* Skip the compression_method byte */\r
+   }\r
+   prefix_len = text - png_ptr->chunkdata;\r
+\r
+   png_decompress_chunk(png_ptr, comp_type,\r
+     (png_size_t)length, prefix_len, &data_len);\r
+\r
+   text_ptr = (png_textp)png_malloc_warn(png_ptr,\r
+      png_sizeof(png_text));\r
+   if (text_ptr == NULL)\r
+   {\r
+     png_warning(png_ptr, "Not enough memory to process zTXt chunk");\r
+     png_free(png_ptr, png_ptr->chunkdata);\r
+     png_ptr->chunkdata = NULL;\r
+     return;\r
+   }\r
+   text_ptr->compression = comp_type;\r
+   text_ptr->key = png_ptr->chunkdata;\r
+#ifdef PNG_iTXt_SUPPORTED\r
+   text_ptr->lang = NULL;\r
+   text_ptr->lang_key = NULL;\r
+   text_ptr->itxt_length = 0;\r
+#endif\r
+   text_ptr->text = png_ptr->chunkdata + prefix_len;\r
+   text_ptr->text_length = data_len;\r
+\r
+   ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1);\r
+\r
+   png_free(png_ptr, text_ptr);\r
+   png_free(png_ptr, png_ptr->chunkdata);\r
+   png_ptr->chunkdata = NULL;\r
+   if (ret)\r
+     png_error(png_ptr, "Insufficient memory to store zTXt chunk");\r
+}\r
+#endif\r
+\r
+#ifdef PNG_READ_iTXt_SUPPORTED\r
+/* Note: this does not correctly handle chunks that are > 64K under DOS */\r
+void /* PRIVATE */\r
+png_handle_iTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)\r
+{\r
+   png_textp text_ptr;\r
+   png_charp key, lang, text, lang_key;\r
+   int comp_flag;\r
+   int comp_type = 0;\r
+   int ret;\r
+   png_size_t slength, prefix_len, data_len;\r
+\r
+   png_debug(1, "in png_handle_iTXt");\r
+\r
+#ifdef PNG_USER_LIMITS_SUPPORTED\r
+   if (png_ptr->user_chunk_cache_max != 0)\r
+   {\r
+      if (png_ptr->user_chunk_cache_max == 1)\r
+      {\r
+         png_crc_finish(png_ptr, length);\r
+         return;\r
+      }\r
+      if (--png_ptr->user_chunk_cache_max == 1)\r
+      {\r
+         png_warning(png_ptr, "No space in chunk cache for iTXt");\r
+         png_crc_finish(png_ptr, length);\r
+         return;\r
+      }\r
+   }\r
+#endif\r
+\r
+   if (!(png_ptr->mode & PNG_HAVE_IHDR))\r
+      png_error(png_ptr, "Missing IHDR before iTXt");\r
+\r
+   if (png_ptr->mode & PNG_HAVE_IDAT)\r
+      png_ptr->mode |= PNG_AFTER_IDAT;\r
+\r
+#ifdef PNG_MAX_MALLOC_64K\r
+   /* We will no doubt have problems with chunks even half this size, but\r
+      there is no hard and fast rule to tell us where to stop. */\r
+   if (length > (png_uint_32)65535L)\r
+   {\r
+     png_warning(png_ptr, "iTXt chunk too large to fit in memory");\r
+     png_crc_finish(png_ptr, length);\r
+     return;\r
+   }\r
+#endif\r
+\r
+   png_free(png_ptr, png_ptr->chunkdata);\r
+   png_ptr->chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1);\r
+   if (png_ptr->chunkdata == NULL)\r
+   {\r
+     png_warning(png_ptr, "No memory to process iTXt chunk");\r
+     return;\r
+   }\r
+   slength = (png_size_t)length;\r
+   png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);\r
+   if (png_crc_finish(png_ptr, 0))\r
+   {\r
+      png_free(png_ptr, png_ptr->chunkdata);\r
+      png_ptr->chunkdata = NULL;\r
+      return;\r
+   }\r
+\r
+   png_ptr->chunkdata[slength] = 0x00;\r
+\r
+   for (lang = png_ptr->chunkdata; *lang; lang++)\r
+      /* Empty loop */ ;\r
+   lang++;        /* Skip NUL separator */\r
+\r
+   /* iTXt must have a language tag (possibly empty), two compression bytes,\r
+    * translated keyword (possibly empty), and possibly some text after the\r
+    * keyword\r
+    */\r
+\r
+   if (lang >= png_ptr->chunkdata + slength - 3)\r
+   {\r
+      png_warning(png_ptr, "Truncated iTXt chunk");\r
+      png_free(png_ptr, png_ptr->chunkdata);\r
+      png_ptr->chunkdata = NULL;\r
+      return;\r
+   }\r
+   else\r
+   {\r
+       comp_flag = *lang++;\r
+       comp_type = *lang++;\r
+   }\r
+\r
+   for (lang_key = lang; *lang_key; lang_key++)\r
+      /* Empty loop */ ;\r
+   lang_key++;        /* Skip NUL separator */\r
+\r
+   if (lang_key >= png_ptr->chunkdata + slength)\r
+   {\r
+      png_warning(png_ptr, "Truncated iTXt chunk");\r
+      png_free(png_ptr, png_ptr->chunkdata);\r
+      png_ptr->chunkdata = NULL;\r
+      return;\r
+   }\r
+\r
+   for (text = lang_key; *text; text++)\r
+      /* Empty loop */ ;\r
+   text++;        /* Skip NUL separator */\r
+   if (text >= png_ptr->chunkdata + slength)\r
+   {\r
+      png_warning(png_ptr, "Malformed iTXt chunk");\r
+      png_free(png_ptr, png_ptr->chunkdata);\r
+      png_ptr->chunkdata = NULL;\r
+      return;\r
+   }\r
+\r
+   prefix_len = text - png_ptr->chunkdata;\r
+\r
+   key=png_ptr->chunkdata;\r
+   if (comp_flag)\r
+       png_decompress_chunk(png_ptr, comp_type,\r
+         (size_t)length, prefix_len, &data_len);\r
+   else\r
+       data_len = png_strlen(png_ptr->chunkdata + prefix_len);\r
+   text_ptr = (png_textp)png_malloc_warn(png_ptr,\r
+      png_sizeof(png_text));\r
+   if (text_ptr == NULL)\r
+   {\r
+     png_warning(png_ptr, "Not enough memory to process iTXt chunk");\r
+     png_free(png_ptr, png_ptr->chunkdata);\r
+     png_ptr->chunkdata = NULL;\r
+     return;\r
+   }\r
+   text_ptr->compression = (int)comp_flag + 1;\r
+   text_ptr->lang_key = png_ptr->chunkdata + (lang_key - key);\r
+   text_ptr->lang = png_ptr->chunkdata + (lang - key);\r
+   text_ptr->itxt_length = data_len;\r
+   text_ptr->text_length = 0;\r
+   text_ptr->key = png_ptr->chunkdata;\r
+   text_ptr->text = png_ptr->chunkdata + prefix_len;\r
+\r
+   ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1);\r
+\r
+   png_free(png_ptr, text_ptr);\r
+   png_free(png_ptr, png_ptr->chunkdata);\r
+   png_ptr->chunkdata = NULL;\r
+   if (ret)\r
+     png_error(png_ptr, "Insufficient memory to store iTXt chunk");\r
+}\r
+#endif\r
+\r
+/* This function is called when we haven't found a handler for a\r
+   chunk.  If there isn't a problem with the chunk itself (ie bad\r
+   chunk name, CRC, or a critical chunk), the chunk is silently ignored\r
+   -- unless the PNG_FLAG_UNKNOWN_CHUNKS_SUPPORTED flag is on in which\r
+   case it will be saved away to be written out later. */\r
+void /* PRIVATE */\r
+png_handle_unknown(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)\r
+{\r
+   png_uint_32 skip = 0;\r
+\r
+   png_debug(1, "in png_handle_unknown");\r
+\r
+#ifdef PNG_USER_LIMITS_SUPPORTED\r
+   if (png_ptr->user_chunk_cache_max != 0)\r
+   {\r
+      if (png_ptr->user_chunk_cache_max == 1)\r
+      {\r
+         png_crc_finish(png_ptr, length);\r
+         return;\r
+      }\r
+      if (--png_ptr->user_chunk_cache_max == 1)\r
+      {\r
+         png_warning(png_ptr, "No space in chunk cache for unknown chunk");\r
+         png_crc_finish(png_ptr, length);\r
+         return;\r
+      }\r
+   }\r
+#endif\r
+\r
+   if (png_ptr->mode & PNG_HAVE_IDAT)\r
+   {\r
+      PNG_IDAT;\r
+      if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4))  /* Not an IDAT */\r
+         png_ptr->mode |= PNG_AFTER_IDAT;\r
+   }\r
+\r
+   if (!(png_ptr->chunk_name[0] & 0x20))\r
+   {\r
+#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED\r
+      if (png_handle_as_unknown(png_ptr, png_ptr->chunk_name) !=\r
+           PNG_HANDLE_CHUNK_ALWAYS\r
+#ifdef PNG_READ_USER_CHUNKS_SUPPORTED\r
+           && png_ptr->read_user_chunk_fn == NULL\r
+#endif\r
+        )\r
+#endif\r
+          png_chunk_error(png_ptr, "unknown critical chunk");\r
+   }\r
+\r
+#ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED\r
+   if ((png_ptr->flags & PNG_FLAG_KEEP_UNKNOWN_CHUNKS)\r
+#ifdef PNG_READ_USER_CHUNKS_SUPPORTED\r
+       || (png_ptr->read_user_chunk_fn != NULL)\r
+#endif\r
+        )\r
+   {\r
+#ifdef PNG_MAX_MALLOC_64K\r
+       if (length > (png_uint_32)65535L)\r
+       {\r
+           png_warning(png_ptr, "unknown chunk too large to fit in memory");\r
+           skip = length - (png_uint_32)65535L;\r
+           length = (png_uint_32)65535L;\r
+       }\r
+#endif\r
+       png_memcpy((png_charp)png_ptr->unknown_chunk.name,\r
+                  (png_charp)png_ptr->chunk_name,\r
+                  png_sizeof(png_ptr->unknown_chunk.name));\r
+       png_ptr->unknown_chunk.name[png_sizeof(png_ptr->unknown_chunk.name)-1]\r
+           = '\0';\r
+       png_ptr->unknown_chunk.size = (png_size_t)length;\r
+       if (length == 0)\r
+         png_ptr->unknown_chunk.data = NULL;\r
+       else\r
+       {\r
+         png_ptr->unknown_chunk.data = (png_bytep)png_malloc(png_ptr, length);\r
+         png_crc_read(png_ptr, (png_bytep)png_ptr->unknown_chunk.data, length);\r
+       }\r
+#ifdef PNG_READ_USER_CHUNKS_SUPPORTED\r
+       if (png_ptr->read_user_chunk_fn != NULL)\r
+       {\r
+          /* Callback to user unknown chunk handler */\r
+          int ret;\r
+          ret = (*(png_ptr->read_user_chunk_fn))\r
+            (png_ptr, &png_ptr->unknown_chunk);\r
+          if (ret < 0)\r
+             png_chunk_error(png_ptr, "error in user chunk");\r
+          if (ret == 0)\r
+          {\r
+             if (!(png_ptr->chunk_name[0] & 0x20))\r
+#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED\r
+                if (png_handle_as_unknown(png_ptr, png_ptr->chunk_name) !=\r
+                     PNG_HANDLE_CHUNK_ALWAYS)\r
+#endif\r
+                   png_chunk_error(png_ptr, "unknown critical chunk");\r
+             png_set_unknown_chunks(png_ptr, info_ptr,\r
+               &png_ptr->unknown_chunk, 1);\r
+          }\r
+       }\r
+       else\r
+#endif\r
+       png_set_unknown_chunks(png_ptr, info_ptr, &png_ptr->unknown_chunk, 1);\r
+       png_free(png_ptr, png_ptr->unknown_chunk.data);\r
+       png_ptr->unknown_chunk.data = NULL;\r
+   }\r
+   else\r
+#endif\r
+      skip = length;\r
+\r
+   png_crc_finish(png_ptr, skip);\r
+\r
+#ifndef PNG_READ_USER_CHUNKS_SUPPORTED\r
+   info_ptr = info_ptr; /* Quiet compiler warnings about unused info_ptr */\r
+#endif\r
+}\r
+\r
+/* This function is called to verify that a chunk name is valid.\r
+   This function can't have the "critical chunk check" incorporated\r
+   into it, since in the future we will need to be able to call user\r
+   functions to handle unknown critical chunks after we check that\r
+   the chunk name itself is valid. */\r
+\r
+#define isnonalpha(c) ((c) < 65 || (c) > 122 || ((c) > 90 && (c) < 97))\r
+\r
+void /* PRIVATE */\r
+png_check_chunk_name(png_structp png_ptr, png_bytep chunk_name)\r
+{\r
+   png_debug(1, "in png_check_chunk_name");\r
+   if (isnonalpha(chunk_name[0]) || isnonalpha(chunk_name[1]) ||\r
+       isnonalpha(chunk_name[2]) || isnonalpha(chunk_name[3]))\r
+   {\r
+      png_chunk_error(png_ptr, "invalid chunk type");\r
+   }\r
+}\r
+\r
+/* Combines the row recently read in with the existing pixels in the\r
+   row.  This routine takes care of alpha and transparency if requested.\r
+   This routine also handles the two methods of progressive display\r
+   of interlaced images, depending on the mask value.\r
+   The mask value describes which pixels are to be combined with\r
+   the row.  The pattern always repeats every 8 pixels, so just 8\r
+   bits are needed.  A one indicates the pixel is to be combined,\r
+   a zero indicates the pixel is to be skipped.  This is in addition\r
+   to any alpha or transparency value associated with the pixel.  If\r
+   you want all pixels to be combined, pass 0xff (255) in mask.  */\r
+\r
+void /* PRIVATE */\r
+png_combine_row(png_structp png_ptr, png_bytep row, int mask)\r
+{\r
+   png_debug(1, "in png_combine_row");\r
+   if (mask == 0xff)\r
+   {\r
+      png_memcpy(row, png_ptr->row_buf + 1,\r
+         PNG_ROWBYTES(png_ptr->row_info.pixel_depth, png_ptr->width));\r
+   }\r
+   else\r
+   {\r
+      switch (png_ptr->row_info.pixel_depth)\r
+      {\r
+         case 1:\r
+         {\r
+            png_bytep sp = png_ptr->row_buf + 1;\r
+            png_bytep dp = row;\r
+            int s_inc, s_start, s_end;\r
+            int m = 0x80;\r
+            int shift;\r
+            png_uint_32 i;\r
+            png_uint_32 row_width = png_ptr->width;\r
+\r
+#ifdef PNG_READ_PACKSWAP_SUPPORTED\r
+            if (png_ptr->transformations & PNG_PACKSWAP)\r
+            {\r
+                s_start = 0;\r
+                s_end = 7;\r
+                s_inc = 1;\r
+            }\r
+            else\r
+#endif\r
+            {\r
+                s_start = 7;\r
+                s_end = 0;\r
+                s_inc = -1;\r
+            }\r
+\r
+            shift = s_start;\r
+\r
+            for (i = 0; i < row_width; i++)\r
+            {\r
+               if (m & mask)\r
+               {\r
+                  int value;\r
+\r
+                  value = (*sp >> shift) & 0x01;\r
+                  *dp &= (png_byte)((0x7f7f >> (7 - shift)) & 0xff);\r
+                  *dp |= (png_byte)(value << shift);\r
+               }\r
+\r
+               if (shift == s_end)\r
+               {\r
+                  shift = s_start;\r
+                  sp++;\r
+                  dp++;\r
+               }\r
+               else\r
+                  shift += s_inc;\r
+\r
+               if (m == 1)\r
+                  m = 0x80;\r
+               else\r
+                  m >>= 1;\r
+            }\r
+            break;\r
+         }\r
+         case 2:\r
+         {\r
+            png_bytep sp = png_ptr->row_buf + 1;\r
+            png_bytep dp = row;\r
+            int s_start, s_end, s_inc;\r
+            int m = 0x80;\r
+            int shift;\r
+            png_uint_32 i;\r
+            png_uint_32 row_width = png_ptr->width;\r
+            int value;\r
+\r
+#ifdef PNG_READ_PACKSWAP_SUPPORTED\r
+            if (png_ptr->transformations & PNG_PACKSWAP)\r
+            {\r
+               s_start = 0;\r
+               s_end = 6;\r
+               s_inc = 2;\r
+            }\r
+            else\r
+#endif\r
+            {\r
+               s_start = 6;\r
+               s_end = 0;\r
+               s_inc = -2;\r
+            }\r
+\r
+            shift = s_start;\r
+\r
+            for (i = 0; i < row_width; i++)\r
+            {\r
+               if (m & mask)\r
+               {\r
+                  value = (*sp >> shift) & 0x03;\r
+                  *dp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff);\r
+                  *dp |= (png_byte)(value << shift);\r
+               }\r
+\r
+               if (shift == s_end)\r
+               {\r
+                  shift = s_start;\r
+                  sp++;\r
+                  dp++;\r
+               }\r
+               else\r
+                  shift += s_inc;\r
+               if (m == 1)\r
+                  m = 0x80;\r
+               else\r
+                  m >>= 1;\r
+            }\r
+            break;\r
+         }\r
+         case 4:\r
+         {\r
+            png_bytep sp = png_ptr->row_buf + 1;\r
+            png_bytep dp = row;\r
+            int s_start, s_end, s_inc;\r
+            int m = 0x80;\r
+            int shift;\r
+            png_uint_32 i;\r
+            png_uint_32 row_width = png_ptr->width;\r
+            int value;\r
+\r
+#ifdef PNG_READ_PACKSWAP_SUPPORTED\r
+            if (png_ptr->transformations & PNG_PACKSWAP)\r
+            {\r
+               s_start = 0;\r
+               s_end = 4;\r
+               s_inc = 4;\r
+            }\r
+            else\r
+#endif\r
+            {\r
+               s_start = 4;\r
+               s_end = 0;\r
+               s_inc = -4;\r
+            }\r
+            shift = s_start;\r
+\r
+            for (i = 0; i < row_width; i++)\r
+            {\r
+               if (m & mask)\r
+               {\r
+                  value = (*sp >> shift) & 0xf;\r
+                  *dp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff);\r
+                  *dp |= (png_byte)(value << shift);\r
+               }\r
+\r
+               if (shift == s_end)\r
+               {\r
+                  shift = s_start;\r
+                  sp++;\r
+                  dp++;\r
+               }\r
+               else\r
+                  shift += s_inc;\r
+               if (m == 1)\r
+                  m = 0x80;\r
+               else\r
+                  m >>= 1;\r
+            }\r
+            break;\r
+         }\r
+         default:\r
+         {\r
+            png_bytep sp = png_ptr->row_buf + 1;\r
+            png_bytep dp = row;\r
+            png_size_t pixel_bytes = (png_ptr->row_info.pixel_depth >> 3);\r
+            png_uint_32 i;\r
+            png_uint_32 row_width = png_ptr->width;\r
+            png_byte m = 0x80;\r
+\r
+\r
+            for (i = 0; i < row_width; i++)\r
+            {\r
+               if (m & mask)\r
+               {\r
+                  png_memcpy(dp, sp, pixel_bytes);\r
+               }\r
+\r
+               sp += pixel_bytes;\r
+               dp += pixel_bytes;\r
+\r
+               if (m == 1)\r
+                  m = 0x80;\r
+               else\r
+                  m >>= 1;\r
+            }\r
+            break;\r
+         }\r
+      }\r
+   }\r
+}\r
+\r
+#ifdef PNG_READ_INTERLACING_SUPPORTED\r
+/* OLD pre-1.0.9 interface:\r
+void png_do_read_interlace(png_row_infop row_info, png_bytep row, int pass,\r
+   png_uint_32 transformations)\r
+ */\r
+void /* PRIVATE */\r
+png_do_read_interlace(png_structp png_ptr)\r
+{\r
+   png_row_infop row_info = &(png_ptr->row_info);\r
+   png_bytep row = png_ptr->row_buf + 1;\r
+   int pass = png_ptr->pass;\r
+   png_uint_32 transformations = png_ptr->transformations;\r
+   /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */\r
+   /* Offset to next interlace block */\r
+   PNG_CONST int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};\r
+\r
+   png_debug(1, "in png_do_read_interlace");\r
+   if (row != NULL && row_info != NULL)\r
+   {\r
+      png_uint_32 final_width;\r
+\r
+      final_width = row_info->width * png_pass_inc[pass];\r
+\r
+      switch (row_info->pixel_depth)\r
+      {\r
+         case 1:\r
+         {\r
+            png_bytep sp = row + (png_size_t)((row_info->width - 1) >> 3);\r
+            png_bytep dp = row + (png_size_t)((final_width - 1) >> 3);\r
+            int sshift, dshift;\r
+            int s_start, s_end, s_inc;\r
+            int jstop = png_pass_inc[pass];\r
+            png_byte v;\r
+            png_uint_32 i;\r
+            int j;\r
+\r
+#ifdef PNG_READ_PACKSWAP_SUPPORTED\r
+            if (transformations & PNG_PACKSWAP)\r
+            {\r
+                sshift = (int)((row_info->width + 7) & 0x07);\r
+                dshift = (int)((final_width + 7) & 0x07);\r
+                s_start = 7;\r
+                s_end = 0;\r
+                s_inc = -1;\r
+            }\r
+            else\r
+#endif\r
+            {\r
+                sshift = 7 - (int)((row_info->width + 7) & 0x07);\r
+                dshift = 7 - (int)((final_width + 7) & 0x07);\r
+                s_start = 0;\r
+                s_end = 7;\r
+                s_inc = 1;\r
+            }\r
+\r
+            for (i = 0; i < row_info->width; i++)\r
+            {\r
+               v = (png_byte)((*sp >> sshift) & 0x01);\r
+               for (j = 0; j < jstop; j++)\r
+               {\r
+                  *dp &= (png_byte)((0x7f7f >> (7 - dshift)) & 0xff);\r
+                  *dp |= (png_byte)(v << dshift);\r
+                  if (dshift == s_end)\r
+                  {\r
+                     dshift = s_start;\r
+                     dp--;\r
+                  }\r
+                  else\r
+                     dshift += s_inc;\r
+               }\r
+               if (sshift == s_end)\r
+               {\r
+                  sshift = s_start;\r
+                  sp--;\r
+               }\r
+               else\r
+                  sshift += s_inc;\r
+            }\r
+            break;\r
+         }\r
+         case 2:\r
+         {\r
+            png_bytep sp = row + (png_uint_32)((row_info->width - 1) >> 2);\r
+            png_bytep dp = row + (png_uint_32)((final_width - 1) >> 2);\r
+            int sshift, dshift;\r
+            int s_start, s_end, s_inc;\r
+            int jstop = png_pass_inc[pass];\r
+            png_uint_32 i;\r
+\r
+#ifdef PNG_READ_PACKSWAP_SUPPORTED\r
+            if (transformations & PNG_PACKSWAP)\r
+            {\r
+               sshift = (int)(((row_info->width + 3) & 0x03) << 1);\r
+               dshift = (int)(((final_width + 3) & 0x03) << 1);\r
+               s_start = 6;\r
+               s_end = 0;\r
+               s_inc = -2;\r
+            }\r
+            else\r
+#endif\r
+            {\r
+               sshift = (int)((3 - ((row_info->width + 3) & 0x03)) << 1);\r
+               dshift = (int)((3 - ((final_width + 3) & 0x03)) << 1);\r
+               s_start = 0;\r
+               s_end = 6;\r
+               s_inc = 2;\r
+            }\r
+\r
+            for (i = 0; i < row_info->width; i++)\r
+            {\r
+               png_byte v;\r
+               int j;\r
+\r
+               v = (png_byte)((*sp >> sshift) & 0x03);\r
+               for (j = 0; j < jstop; j++)\r
+               {\r
+                  *dp &= (png_byte)((0x3f3f >> (6 - dshift)) & 0xff);\r
+                  *dp |= (png_byte)(v << dshift);\r
+                  if (dshift == s_end)\r
+                  {\r
+                     dshift = s_start;\r
+                     dp--;\r
+                  }\r
+                  else\r
+                     dshift += s_inc;\r
+               }\r
+               if (sshift == s_end)\r
+               {\r
+                  sshift = s_start;\r
+                  sp--;\r
+               }\r
+               else\r
+                  sshift += s_inc;\r
+            }\r
+            break;\r
+         }\r
+         case 4:\r
+         {\r
+            png_bytep sp = row + (png_size_t)((row_info->width - 1) >> 1);\r
+            png_bytep dp = row + (png_size_t)((final_width - 1) >> 1);\r
+            int sshift, dshift;\r
+            int s_start, s_end, s_inc;\r
+            png_uint_32 i;\r
+            int jstop = png_pass_inc[pass];\r
+\r
+#ifdef PNG_READ_PACKSWAP_SUPPORTED\r
+            if (transformations & PNG_PACKSWAP)\r
+            {\r
+               sshift = (int)(((row_info->width + 1) & 0x01) << 2);\r
+               dshift = (int)(((final_width + 1) & 0x01) << 2);\r
+               s_start = 4;\r
+               s_end = 0;\r
+               s_inc = -4;\r
+            }\r
+            else\r
+#endif\r
+            {\r
+               sshift = (int)((1 - ((row_info->width + 1) & 0x01)) << 2);\r
+               dshift = (int)((1 - ((final_width + 1) & 0x01)) << 2);\r
+               s_start = 0;\r
+               s_end = 4;\r
+               s_inc = 4;\r
+            }\r
+\r
+            for (i = 0; i < row_info->width; i++)\r
+            {\r
+               png_byte v = (png_byte)((*sp >> sshift) & 0xf);\r
+               int j;\r
+\r
+               for (j = 0; j < jstop; j++)\r
+               {\r
+                  *dp &= (png_byte)((0xf0f >> (4 - dshift)) & 0xff);\r
+                  *dp |= (png_byte)(v << dshift);\r
+                  if (dshift == s_end)\r
+                  {\r
+                     dshift = s_start;\r
+                     dp--;\r
+                  }\r
+                  else\r
+                     dshift += s_inc;\r
+               }\r
+               if (sshift == s_end)\r
+               {\r
+                  sshift = s_start;\r
+                  sp--;\r
+               }\r
+               else\r
+                  sshift += s_inc;\r
+            }\r
+            break;\r
+         }\r
+         default:\r
+         {\r
+            png_size_t pixel_bytes = (row_info->pixel_depth >> 3);\r
+            png_bytep sp = row + (png_size_t)(row_info->width - 1)\r
+                * pixel_bytes;\r
+            png_bytep dp = row + (png_size_t)(final_width - 1) * pixel_bytes;\r
+\r
+            int jstop = png_pass_inc[pass];\r
+            png_uint_32 i;\r
+\r
+            for (i = 0; i < row_info->width; i++)\r
+            {\r
+               png_byte v[8];\r
+               int j;\r
+\r
+               png_memcpy(v, sp, pixel_bytes);\r
+               for (j = 0; j < jstop; j++)\r
+               {\r
+                  png_memcpy(dp, v, pixel_bytes);\r
+                  dp -= pixel_bytes;\r
+               }\r
+               sp -= pixel_bytes;\r
+            }\r
+            break;\r
+         }\r
+      }\r
+      row_info->width = final_width;\r
+      row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, final_width);\r
+   }\r
+#ifndef PNG_READ_PACKSWAP_SUPPORTED\r
+   transformations = transformations; /* Silence compiler warning */\r
+#endif\r
+}\r
+#endif /* PNG_READ_INTERLACING_SUPPORTED */\r
+\r
+void /* PRIVATE */\r
+png_read_filter_row(png_structp png_ptr, png_row_infop row_info, png_bytep row,\r
+   png_bytep prev_row, int filter)\r
+{\r
+   png_debug(1, "in png_read_filter_row");\r
+   png_debug2(2, "row = %lu, filter = %d", png_ptr->row_number, filter);\r
+   switch (filter)\r
+   {\r
+      case PNG_FILTER_VALUE_NONE:\r
+         break;\r
+      case PNG_FILTER_VALUE_SUB:\r
+      {\r
+         png_uint_32 i;\r
+         png_uint_32 istop = row_info->rowbytes;\r
+         png_uint_32 bpp = (row_info->pixel_depth + 7) >> 3;\r
+         png_bytep rp = row + bpp;\r
+         png_bytep lp = row;\r
+\r
+         for (i = bpp; i < istop; i++)\r
+         {\r
+            *rp = (png_byte)(((int)(*rp) + (int)(*lp++)) & 0xff);\r
+            rp++;\r
+         }\r
+         break;\r
+      }\r
+      case PNG_FILTER_VALUE_UP:\r
+      {\r
+         png_uint_32 i;\r
+         png_uint_32 istop = row_info->rowbytes;\r
+         png_bytep rp = row;\r
+         png_bytep pp = prev_row;\r
+\r
+         for (i = 0; i < istop; i++)\r
+         {\r
+            *rp = (png_byte)(((int)(*rp) + (int)(*pp++)) & 0xff);\r
+            rp++;\r
+         }\r
+         break;\r
+      }\r
+      case PNG_FILTER_VALUE_AVG:\r
+      {\r
+         png_uint_32 i;\r
+         png_bytep rp = row;\r
+         png_bytep pp = prev_row;\r
+         png_bytep lp = row;\r
+         png_uint_32 bpp = (row_info->pixel_depth + 7) >> 3;\r
+         png_uint_32 istop = row_info->rowbytes - bpp;\r
+\r
+         for (i = 0; i < bpp; i++)\r
+         {\r
+            *rp = (png_byte)(((int)(*rp) +\r
+               ((int)(*pp++) / 2 )) & 0xff);\r
+            rp++;\r
+         }\r
+\r
+         for (i = 0; i < istop; i++)\r
+         {\r
+            *rp = (png_byte)(((int)(*rp) +\r
+               (int)(*pp++ + *lp++) / 2 ) & 0xff);\r
+            rp++;\r
+         }\r
+         break;\r
+      }\r
+      case PNG_FILTER_VALUE_PAETH:\r
+      {\r
+         png_uint_32 i;\r
+         png_bytep rp = row;\r
+         png_bytep pp = prev_row;\r
+         png_bytep lp = row;\r
+         png_bytep cp = prev_row;\r
+         png_uint_32 bpp = (row_info->pixel_depth + 7) >> 3;\r
+         png_uint_32 istop=row_info->rowbytes - bpp;\r
+\r
+         for (i = 0; i < bpp; i++)\r
+         {\r
+            *rp = (png_byte)(((int)(*rp) + (int)(*pp++)) & 0xff);\r
+            rp++;\r
+         }\r
+\r
+         for (i = 0; i < istop; i++)   /* Use leftover rp,pp */\r
+         {\r
+            int a, b, c, pa, pb, pc, p;\r
+\r
+            a = *lp++;\r
+            b = *pp++;\r
+            c = *cp++;\r
+\r
+            p = b - c;\r
+            pc = a - c;\r
+\r
+#ifdef PNG_USE_ABS\r
+            pa = abs(p);\r
+            pb = abs(pc);\r
+            pc = abs(p + pc);\r
+#else\r
+            pa = p < 0 ? -p : p;\r
+            pb = pc < 0 ? -pc : pc;\r
+            pc = (p + pc) < 0 ? -(p + pc) : p + pc;\r
+#endif\r
+\r
+            /*\r
+               if (pa <= pb && pa <= pc)\r
+                  p = a;\r
+               else if (pb <= pc)\r
+                  p = b;\r
+               else\r
+                  p = c;\r
+             */\r
+\r
+            p = (pa <= pb && pa <= pc) ? a : (pb <= pc) ? b : c;\r
+\r
+            *rp = (png_byte)(((int)(*rp) + p) & 0xff);\r
+            rp++;\r
+         }\r
+         break;\r
+      }\r
+      default:\r
+         png_warning(png_ptr, "Ignoring bad adaptive filter type");\r
+         *row = 0;\r
+         break;\r
+   }\r
+}\r
+\r
+#ifdef PNG_SEQUENTIAL_READ_SUPPORTED\r
+void /* PRIVATE */\r
+png_read_finish_row(png_structp png_ptr)\r
+{\r
+#ifdef PNG_READ_INTERLACING_SUPPORTED\r
+   /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */\r
+\r
+   /* Start of interlace block */\r
+   PNG_CONST int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};\r
+\r
+   /* Offset to next interlace block */\r
+   PNG_CONST int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};\r
+\r
+   /* Start of interlace block in the y direction */\r
+   PNG_CONST int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};\r
+\r
+   /* Offset to next interlace block in the y direction */\r
+   PNG_CONST int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};\r
+#endif /* PNG_READ_INTERLACING_SUPPORTED */\r
+\r
+   png_debug(1, "in png_read_finish_row");\r
+   png_ptr->row_number++;\r
+   if (png_ptr->row_number < png_ptr->num_rows)\r
+      return;\r
+\r
+#ifdef PNG_READ_INTERLACING_SUPPORTED\r
+   if (png_ptr->interlaced)\r
+   {\r
+      png_ptr->row_number = 0;\r
+      png_memset(png_ptr->prev_row, 0,\r
+         png_ptr->rowbytes + 1);\r
+      do\r
+      {\r
+         png_ptr->pass++;\r
+         if (png_ptr->pass >= 7)\r
+            break;\r
+         png_ptr->iwidth = (png_ptr->width +\r
+            png_pass_inc[png_ptr->pass] - 1 -\r
+            png_pass_start[png_ptr->pass]) /\r
+            png_pass_inc[png_ptr->pass];\r
+\r
+         if (!(png_ptr->transformations & PNG_INTERLACE))\r
+         {\r
+            png_ptr->num_rows = (png_ptr->height +\r
+               png_pass_yinc[png_ptr->pass] - 1 -\r
+               png_pass_ystart[png_ptr->pass]) /\r
+               png_pass_yinc[png_ptr->pass];\r
+            if (!(png_ptr->num_rows))\r
+               continue;\r
+         }\r
+         else  /* if (png_ptr->transformations & PNG_INTERLACE) */\r
+            break;\r
+      } while (png_ptr->iwidth == 0);\r
+\r
+      if (png_ptr->pass < 7)\r
+         return;\r
+   }\r
+#endif /* PNG_READ_INTERLACING_SUPPORTED */\r
+\r
+   if (!(png_ptr->flags & PNG_FLAG_ZLIB_FINISHED))\r
+   {\r
+      PNG_IDAT;\r
+      char extra;\r
+      int ret;\r
+\r
+      png_ptr->zstream.next_out = (Byte *)&extra;\r
+      png_ptr->zstream.avail_out = (uInt)1;\r
+      for (;;)\r
+      {\r
+         if (!(png_ptr->zstream.avail_in))\r
+         {\r
+            while (!png_ptr->idat_size)\r
+            {\r
+               png_byte chunk_length[4];\r
+\r
+               png_crc_finish(png_ptr, 0);\r
+\r
+               png_read_data(png_ptr, chunk_length, 4);\r
+               png_ptr->idat_size = png_get_uint_31(png_ptr, chunk_length);\r
+               png_reset_crc(png_ptr);\r
+               png_crc_read(png_ptr, png_ptr->chunk_name, 4);\r
+               if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4))\r
+                  png_error(png_ptr, "Not enough image data");\r
+\r
+            }\r
+            png_ptr->zstream.avail_in = (uInt)png_ptr->zbuf_size;\r
+            png_ptr->zstream.next_in = png_ptr->zbuf;\r
+            if (png_ptr->zbuf_size > png_ptr->idat_size)\r
+               png_ptr->zstream.avail_in = (uInt)png_ptr->idat_size;\r
+            png_crc_read(png_ptr, png_ptr->zbuf, png_ptr->zstream.avail_in);\r
+            png_ptr->idat_size -= png_ptr->zstream.avail_in;\r
+         }\r
+         ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);\r
+         if (ret == Z_STREAM_END)\r
+         {\r
+            if (!(png_ptr->zstream.avail_out) || png_ptr->zstream.avail_in ||\r
+               png_ptr->idat_size)\r
+               png_warning(png_ptr, "Extra compressed data");\r
+            png_ptr->mode |= PNG_AFTER_IDAT;\r
+            png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;\r
+            break;\r
+         }\r
+         if (ret != Z_OK)\r
+            png_error(png_ptr, png_ptr->zstream.msg ? png_ptr->zstream.msg :\r
+                      "Decompression Error");\r
+\r
+         if (!(png_ptr->zstream.avail_out))\r
+         {\r
+            png_warning(png_ptr, "Extra compressed data");\r
+            png_ptr->mode |= PNG_AFTER_IDAT;\r
+            png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;\r
+            break;\r
+         }\r
+\r
+      }\r
+      png_ptr->zstream.avail_out = 0;\r
+   }\r
+\r
+   if (png_ptr->idat_size || png_ptr->zstream.avail_in)\r
+      png_warning(png_ptr, "Extra compression data");\r
+\r
+   inflateReset(&png_ptr->zstream);\r
+\r
+   png_ptr->mode |= PNG_AFTER_IDAT;\r
+}\r
+#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */\r
+\r
+void /* PRIVATE */\r
+png_read_start_row(png_structp png_ptr)\r
+{\r
+#ifdef PNG_READ_INTERLACING_SUPPORTED\r
+   /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */\r
+\r
+   /* Start of interlace block */\r
+   PNG_CONST int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};\r
+\r
+   /* Offset to next interlace block */\r
+   PNG_CONST int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};\r
+\r
+   /* Start of interlace block in the y direction */\r
+   PNG_CONST int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};\r
+\r
+   /* Offset to next interlace block in the y direction */\r
+   PNG_CONST int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};\r
+#endif\r
+\r
+   int max_pixel_depth;\r
+   png_size_t row_bytes;\r
+\r
+   png_debug(1, "in png_read_start_row");\r
+   png_ptr->zstream.avail_in = 0;\r
+   png_init_read_transformations(png_ptr);\r
+#ifdef PNG_READ_INTERLACING_SUPPORTED\r
+   if (png_ptr->interlaced)\r
+   {\r
+      if (!(png_ptr->transformations & PNG_INTERLACE))\r
+         png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -\r
+            png_pass_ystart[0]) / png_pass_yinc[0];\r
+      else\r
+         png_ptr->num_rows = png_ptr->height;\r
+\r
+      png_ptr->iwidth = (png_ptr->width +\r
+         png_pass_inc[png_ptr->pass] - 1 -\r
+         png_pass_start[png_ptr->pass]) /\r
+         png_pass_inc[png_ptr->pass];\r
+   }\r
+   else\r
+#endif /* PNG_READ_INTERLACING_SUPPORTED */\r
+   {\r
+      png_ptr->num_rows = png_ptr->height;\r
+      png_ptr->iwidth = png_ptr->width;\r
+   }\r
+   max_pixel_depth = png_ptr->pixel_depth;\r
+\r
+#ifdef PNG_READ_PACK_SUPPORTED\r
+   if ((png_ptr->transformations & PNG_PACK) && png_ptr->bit_depth < 8)\r
+      max_pixel_depth = 8;\r
+#endif\r
+\r
+#ifdef PNG_READ_EXPAND_SUPPORTED\r
+   if (png_ptr->transformations & PNG_EXPAND)\r
+   {\r
+      if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)\r
+      {\r
+         if (png_ptr->num_trans)\r
+            max_pixel_depth = 32;\r
+         else\r
+            max_pixel_depth = 24;\r
+      }\r
+      else if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)\r
+      {\r
+         if (max_pixel_depth < 8)\r
+            max_pixel_depth = 8;\r
+         if (png_ptr->num_trans)\r
+            max_pixel_depth *= 2;\r
+      }\r
+      else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)\r
+      {\r
+         if (png_ptr->num_trans)\r
+         {\r
+            max_pixel_depth *= 4;\r
+            max_pixel_depth /= 3;\r
+         }\r
+      }\r
+   }\r
+#endif\r
+\r
+#ifdef PNG_READ_FILLER_SUPPORTED\r
+   if (png_ptr->transformations & (PNG_FILLER))\r
+   {\r
+      if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)\r
+         max_pixel_depth = 32;\r
+      else if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)\r
+      {\r
+         if (max_pixel_depth <= 8)\r
+            max_pixel_depth = 16;\r
+         else\r
+            max_pixel_depth = 32;\r
+      }\r
+      else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)\r
+      {\r
+         if (max_pixel_depth <= 32)\r
+            max_pixel_depth = 32;\r
+         else\r
+            max_pixel_depth = 64;\r
+      }\r
+   }\r
+#endif\r
+\r
+#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED\r
+   if (png_ptr->transformations & PNG_GRAY_TO_RGB)\r
+   {\r
+      if (\r
+#ifdef PNG_READ_EXPAND_SUPPORTED\r
+        (png_ptr->num_trans && (png_ptr->transformations & PNG_EXPAND)) ||\r
+#endif\r
+#ifdef PNG_READ_FILLER_SUPPORTED\r
+        (png_ptr->transformations & (PNG_FILLER)) ||\r
+#endif\r
+        png_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)\r
+      {\r
+         if (max_pixel_depth <= 16)\r
+            max_pixel_depth = 32;\r
+         else\r
+            max_pixel_depth = 64;\r
+      }\r
+      else\r
+      {\r
+         if (max_pixel_depth <= 8)\r
+           {\r
+             if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)\r
+               max_pixel_depth = 32;\r
+             else\r
+               max_pixel_depth = 24;\r
+           }\r
+         else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)\r
+            max_pixel_depth = 64;\r
+         else\r
+            max_pixel_depth = 48;\r
+      }\r
+   }\r
+#endif\r
+\r
+#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) && \\r
+defined(PNG_USER_TRANSFORM_PTR_SUPPORTED)\r
+   if (png_ptr->transformations & PNG_USER_TRANSFORM)\r
+     {\r
+       int user_pixel_depth = png_ptr->user_transform_depth*\r
+         png_ptr->user_transform_channels;\r
+       if (user_pixel_depth > max_pixel_depth)\r
+         max_pixel_depth=user_pixel_depth;\r
+     }\r
+#endif\r
+\r
+   /* Align the width on the next larger 8 pixels.  Mainly used\r
+    * for interlacing\r
+    */\r
+   row_bytes = ((png_ptr->width + 7) & ~((png_uint_32)7));\r
+   /* Calculate the maximum bytes needed, adding a byte and a pixel\r
+    * for safety's sake\r
+    */\r
+   row_bytes = PNG_ROWBYTES(max_pixel_depth, row_bytes) +\r
+      1 + ((max_pixel_depth + 7) >> 3);\r
+#ifdef PNG_MAX_MALLOC_64K\r
+   if (row_bytes > (png_uint_32)65536L)\r
+      png_error(png_ptr, "This image requires a row greater than 64KB");\r
+#endif\r
+\r
+   if (row_bytes + 48 > png_ptr->old_big_row_buf_size)\r
+   {\r
+     png_free(png_ptr, png_ptr->big_row_buf);\r
+     if (png_ptr->interlaced)\r
+        png_ptr->big_row_buf = (png_bytep)png_calloc(png_ptr,\r
+            row_bytes + 48);\r
+     else\r
+        png_ptr->big_row_buf = (png_bytep)png_malloc(png_ptr,\r
+            row_bytes + 48);\r
+     png_ptr->old_big_row_buf_size = row_bytes + 48;\r
+\r
+#ifdef PNG_ALIGNED_MEMORY_SUPPORTED\r
+     /* Use 16-byte aligned memory for row_buf with at least 16 bytes\r
+      * of padding before and after row_buf.\r
+      */\r
+     png_ptr->row_buf = png_ptr->big_row_buf + 32\r
+         - (((png_alloc_size_t)&(png_ptr->big_row_buf[0]) + 15) % 16);\r
+     png_ptr->old_big_row_buf_size = row_bytes + 48;\r
+#else\r
+     /* Use 32 bytes of padding before and 16 bytes after row_buf. */\r
+     png_ptr->row_buf = png_ptr->big_row_buf + 32;\r
+#endif\r
+     png_ptr->old_big_row_buf_size = row_bytes + 48;\r
+   }\r
+\r
+#ifdef PNG_MAX_MALLOC_64K\r
+   if ((png_uint_32)png_ptr->rowbytes + 1 > (png_uint_32)65536L)\r
+      png_error(png_ptr, "This image requires a row greater than 64KB");\r
+#endif\r
+   if ((png_uint_32)png_ptr->rowbytes > (png_uint_32)(PNG_SIZE_MAX - 1))\r
+      png_error(png_ptr, "Row has too many bytes to allocate in memory");\r
+\r
+   if (png_ptr->rowbytes + 1 > png_ptr->old_prev_row_size)\r
+   {\r
+      png_free(png_ptr, png_ptr->prev_row);\r
+      png_ptr->prev_row = (png_bytep)png_malloc(png_ptr, (png_uint_32)(\r
+        png_ptr->rowbytes + 1));\r
+      png_ptr->old_prev_row_size = png_ptr->rowbytes + 1;\r
+   }\r
+\r
+   png_memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1);\r
+\r
+   png_debug1(3, "width = %lu,", png_ptr->width);\r
+   png_debug1(3, "height = %lu,", png_ptr->height);\r
+   png_debug1(3, "iwidth = %lu,", png_ptr->iwidth);\r
+   png_debug1(3, "num_rows = %lu,", png_ptr->num_rows);\r
+   png_debug1(3, "rowbytes = %lu,", png_ptr->rowbytes);\r
+   png_debug1(3, "irowbytes = %lu",\r
+       PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->iwidth) + 1);\r
+\r
+   png_ptr->flags |= PNG_FLAG_ROW_INIT;\r
+}\r
+#endif /* PNG_READ_SUPPORTED */\r
index 8b25ca5..90ef1b4 100644 (file)
-
-/* pngset.c - storage of image information into info struct
- *
- * Last changed in libpng 1.2.27 [April 29, 2008]
- * For conditions of distribution and use, see copyright notice in png.h
- * Copyright (c) 1998-2008 Glenn Randers-Pehrson
- * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
- * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
- *
- * The functions here are used during reads to store data from the file
- * into the info struct, and during writes to store application data
- * into the info struct for writing into the file.  This abstracts the
- * info struct and allows us to change the structure in the future.
- */
-
-#define PNG_INTERNAL
-#include "png.h"
-
-#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
-
-#if defined(PNG_bKGD_SUPPORTED)
-void PNGAPI
-png_set_bKGD(png_structp png_ptr, png_infop info_ptr, png_color_16p background)
-{
-   png_debug1(1, "in %s storage function\n", "bKGD");
-   if (png_ptr == NULL || info_ptr == NULL)
-      return;
-
-   png_memcpy(&(info_ptr->background), background, png_sizeof(png_color_16));
-   info_ptr->valid |= PNG_INFO_bKGD;
-}
-#endif
-
-#if defined(PNG_cHRM_SUPPORTED)
-#ifdef PNG_FLOATING_POINT_SUPPORTED
-void PNGAPI
-png_set_cHRM(png_structp png_ptr, png_infop info_ptr,
-   double white_x, double white_y, double red_x, double red_y,
-   double green_x, double green_y, double blue_x, double blue_y)
-{
-   png_debug1(1, "in %s storage function\n", "cHRM");
-   if (png_ptr == NULL || info_ptr == NULL)
-      return;
-   if (!(white_x || white_y || red_x || red_y || green_x || green_y ||
-       blue_x || blue_y))
-   {
-      png_warning(png_ptr,
-        "Ignoring attempt to set all-zero chromaticity values");
-      return;
-   }
-   if (white_x < 0.0 || white_y < 0.0 ||
-         red_x < 0.0 ||   red_y < 0.0 ||
-       green_x < 0.0 || green_y < 0.0 ||
-        blue_x < 0.0 ||  blue_y < 0.0)
-   {
-      png_warning(png_ptr,
-        "Ignoring attempt to set negative chromaticity value");
-      return;
-   }
-   if (white_x > 21474.83 || white_y > 21474.83 ||
-         red_x > 21474.83 ||   red_y > 21474.83 ||
-       green_x > 21474.83 || green_y > 21474.83 ||
-        blue_x > 21474.83 ||  blue_y > 21474.83)
-   {
-      png_warning(png_ptr,
-        "Ignoring attempt to set chromaticity value exceeding 21474.83");
-      return;
-   }
-
-   info_ptr->x_white = (float)white_x;
-   info_ptr->y_white = (float)white_y;
-   info_ptr->x_red   = (float)red_x;
-   info_ptr->y_red   = (float)red_y;
-   info_ptr->x_green = (float)green_x;
-   info_ptr->y_green = (float)green_y;
-   info_ptr->x_blue  = (float)blue_x;
-   info_ptr->y_blue  = (float)blue_y;
-#ifdef PNG_FIXED_POINT_SUPPORTED
-   info_ptr->int_x_white = (png_fixed_point)(white_x*100000.+0.5);
-   info_ptr->int_y_white = (png_fixed_point)(white_y*100000.+0.5);
-   info_ptr->int_x_red   = (png_fixed_point)(  red_x*100000.+0.5);
-   info_ptr->int_y_red   = (png_fixed_point)(  red_y*100000.+0.5);
-   info_ptr->int_x_green = (png_fixed_point)(green_x*100000.+0.5);
-   info_ptr->int_y_green = (png_fixed_point)(green_y*100000.+0.5);
-   info_ptr->int_x_blue  = (png_fixed_point)( blue_x*100000.+0.5);
-   info_ptr->int_y_blue  = (png_fixed_point)( blue_y*100000.+0.5);
-#endif
-   info_ptr->valid |= PNG_INFO_cHRM;
-}
-#endif
-#ifdef PNG_FIXED_POINT_SUPPORTED
-void PNGAPI
-png_set_cHRM_fixed(png_structp png_ptr, png_infop info_ptr,
-   png_fixed_point white_x, png_fixed_point white_y, png_fixed_point red_x,
-   png_fixed_point red_y, png_fixed_point green_x, png_fixed_point green_y,
-   png_fixed_point blue_x, png_fixed_point blue_y)
-{
-   png_debug1(1, "in %s storage function\n", "cHRM");
-   if (png_ptr == NULL || info_ptr == NULL)
-      return;
-
-   if (!(white_x || white_y || red_x || red_y || green_x || green_y ||
-       blue_x || blue_y))
-   {
-      png_warning(png_ptr,
-        "Ignoring attempt to set all-zero chromaticity values");
-      return;
-   }
-   if (white_x < 0 || white_y < 0 ||
-         red_x < 0 ||   red_y < 0 ||
-       green_x < 0 || green_y < 0 ||
-        blue_x < 0 ||  blue_y < 0)
-   {
-      png_warning(png_ptr,
-        "Ignoring attempt to set negative chromaticity value");
-      return;
-   }
-   if (white_x > (png_fixed_point) PNG_UINT_31_MAX ||
-       white_y > (png_fixed_point) PNG_UINT_31_MAX ||
-         red_x > (png_fixed_point) PNG_UINT_31_MAX ||
-         red_y > (png_fixed_point) PNG_UINT_31_MAX ||
-       green_x > (png_fixed_point) PNG_UINT_31_MAX ||
-       green_y > (png_fixed_point) PNG_UINT_31_MAX ||
-        blue_x > (png_fixed_point) PNG_UINT_31_MAX ||
-        blue_y > (png_fixed_point) PNG_UINT_31_MAX )
-   {
-      png_warning(png_ptr,
-        "Ignoring attempt to set chromaticity value exceeding 21474.83");
-      return;
-   }
-   info_ptr->int_x_white = white_x;
-   info_ptr->int_y_white = white_y;
-   info_ptr->int_x_red   = red_x;
-   info_ptr->int_y_red   = red_y;
-   info_ptr->int_x_green = green_x;
-   info_ptr->int_y_green = green_y;
-   info_ptr->int_x_blue  = blue_x;
-   info_ptr->int_y_blue  = blue_y;
-#ifdef PNG_FLOATING_POINT_SUPPORTED
-   info_ptr->x_white = (float)(white_x/100000.);
-   info_ptr->y_white = (float)(white_y/100000.);
-   info_ptr->x_red   = (float)(  red_x/100000.);
-   info_ptr->y_red   = (float)(  red_y/100000.);
-   info_ptr->x_green = (float)(green_x/100000.);
-   info_ptr->y_green = (float)(green_y/100000.);
-   info_ptr->x_blue  = (float)( blue_x/100000.);
-   info_ptr->y_blue  = (float)( blue_y/100000.);
-#endif
-   info_ptr->valid |= PNG_INFO_cHRM;
-}
-#endif
-#endif
-
-#if defined(PNG_gAMA_SUPPORTED)
-#ifdef PNG_FLOATING_POINT_SUPPORTED
-void PNGAPI
-png_set_gAMA(png_structp png_ptr, png_infop info_ptr, double file_gamma)
-{
-   double gamma;
-   png_debug1(1, "in %s storage function\n", "gAMA");
-   if (png_ptr == NULL || info_ptr == NULL)
-      return;
-
-   /* Check for overflow */
-   if (file_gamma > 21474.83)
-   {
-      png_warning(png_ptr, "Limiting gamma to 21474.83");
-      gamma=21474.83;
-   }
-   else
-      gamma=file_gamma;
-   info_ptr->gamma = (float)gamma;
-#ifdef PNG_FIXED_POINT_SUPPORTED
-   info_ptr->int_gamma = (int)(gamma*100000.+.5);
-#endif
-   info_ptr->valid |= PNG_INFO_gAMA;
-   if(gamma == 0.0)
-      png_warning(png_ptr, "Setting gamma=0");
-}
-#endif
-void PNGAPI
-png_set_gAMA_fixed(png_structp png_ptr, png_infop info_ptr, png_fixed_point
-   int_gamma)
-{
-   png_fixed_point gamma;
-
-   png_debug1(1, "in %s storage function\n", "gAMA");
-   if (png_ptr == NULL || info_ptr == NULL)
-      return;
-
-   if (int_gamma > (png_fixed_point) PNG_UINT_31_MAX)
-   {
-     png_warning(png_ptr, "Limiting gamma to 21474.83");
-     gamma=PNG_UINT_31_MAX;
-   }
-   else
-   {
-     if (int_gamma < 0)
-     {
-       png_warning(png_ptr, "Setting negative gamma to zero");
-       gamma=0;
-     }
-     else
-       gamma=int_gamma;
-   }
-#ifdef PNG_FLOATING_POINT_SUPPORTED
-   info_ptr->gamma = (float)(gamma/100000.);
-#endif
-#ifdef PNG_FIXED_POINT_SUPPORTED
-   info_ptr->int_gamma = gamma;
-#endif
-   info_ptr->valid |= PNG_INFO_gAMA;
-   if(gamma == 0)
-      png_warning(png_ptr, "Setting gamma=0");
-}
-#endif
-
-#if defined(PNG_hIST_SUPPORTED)
-void PNGAPI
-png_set_hIST(png_structp png_ptr, png_infop info_ptr, png_uint_16p hist)
-{
-   int i;
-
-   png_debug1(1, "in %s storage function\n", "hIST");
-   if (png_ptr == NULL || info_ptr == NULL)
-      return;
-   if (info_ptr->num_palette == 0 || info_ptr->num_palette
-       > PNG_MAX_PALETTE_LENGTH)
-   {
-       png_warning(png_ptr,
-          "Invalid palette size, hIST allocation skipped.");
-       return;
-   }
-
-#ifdef PNG_FREE_ME_SUPPORTED
-   png_free_data(png_ptr, info_ptr, PNG_FREE_HIST, 0);
-#endif
-   /* Changed from info->num_palette to PNG_MAX_PALETTE_LENGTH in version
-      1.2.1 */
-   png_ptr->hist = (png_uint_16p)png_malloc_warn(png_ptr,
-      (png_uint_32)(PNG_MAX_PALETTE_LENGTH * png_sizeof (png_uint_16)));
-   if (png_ptr->hist == NULL)
-     {
-       png_warning(png_ptr, "Insufficient memory for hIST chunk data.");
-       return;
-     }
-
-   for (i = 0; i < info_ptr->num_palette; i++)
-       png_ptr->hist[i] = hist[i];
-   info_ptr->hist = png_ptr->hist;
-   info_ptr->valid |= PNG_INFO_hIST;
-
-#ifdef PNG_FREE_ME_SUPPORTED
-   info_ptr->free_me |= PNG_FREE_HIST;
-#else
-   png_ptr->flags |= PNG_FLAG_FREE_HIST;
-#endif
-}
-#endif
-
-void PNGAPI
-png_set_IHDR(png_structp png_ptr, png_infop info_ptr,
-   png_uint_32 width, png_uint_32 height, int bit_depth,
-   int color_type, int interlace_type, int compression_type,
-   int filter_type)
-{
-   png_debug1(1, "in %s storage function\n", "IHDR");
-   if (png_ptr == NULL || info_ptr == NULL)
-      return;
-
-   /* check for width and height valid values */
-   if (width == 0 || height == 0)
-      png_error(png_ptr, "Image width or height is zero in IHDR");
-#ifdef PNG_SET_USER_LIMITS_SUPPORTED
-   if (width > png_ptr->user_width_max || height > png_ptr->user_height_max)
-      png_error(png_ptr, "image size exceeds user limits in IHDR");
-#else
-   if (width > PNG_USER_WIDTH_MAX || height > PNG_USER_HEIGHT_MAX)
-      png_error(png_ptr, "image size exceeds user limits in IHDR");
-#endif
-   if (width > PNG_UINT_31_MAX || height > PNG_UINT_31_MAX)
-      png_error(png_ptr, "Invalid image size in IHDR");
-   if ( width > (PNG_UINT_32_MAX
-                 >> 3)      /* 8-byte RGBA pixels */
-                 - 64       /* bigrowbuf hack */
-                 - 1        /* filter byte */
-                 - 7*8      /* rounding of width to multiple of 8 pixels */
-                 - 8)       /* extra max_pixel_depth pad */
-      png_warning(png_ptr, "Width is too large for libpng to process pixels");
-
-   /* check other values */
-   if (bit_depth != 1 && bit_depth != 2 && bit_depth != 4 &&
-      bit_depth != 8 && bit_depth != 16)
-      png_error(png_ptr, "Invalid bit depth in IHDR");
-
-   if (color_type < 0 || color_type == 1 ||
-      color_type == 5 || color_type > 6)
-      png_error(png_ptr, "Invalid color type in IHDR");
-
-   if (((color_type == PNG_COLOR_TYPE_PALETTE) && bit_depth > 8) ||
-       ((color_type == PNG_COLOR_TYPE_RGB ||
-         color_type == PNG_COLOR_TYPE_GRAY_ALPHA ||
-         color_type == PNG_COLOR_TYPE_RGB_ALPHA) && bit_depth < 8))
-      png_error(png_ptr, "Invalid color type/bit depth combination in IHDR");
-
-   if (interlace_type >= PNG_INTERLACE_LAST)
-      png_error(png_ptr, "Unknown interlace method in IHDR");
-
-   if (compression_type != PNG_COMPRESSION_TYPE_BASE)
-      png_error(png_ptr, "Unknown compression method in IHDR");
-
-#if defined(PNG_MNG_FEATURES_SUPPORTED)
-   /* Accept filter_method 64 (intrapixel differencing) only if
-    * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
-    * 2. Libpng did not read a PNG signature (this filter_method is only
-    *    used in PNG datastreams that are embedded in MNG datastreams) and
-    * 3. The application called png_permit_mng_features with a mask that
-    *    included PNG_FLAG_MNG_FILTER_64 and
-    * 4. The filter_method is 64 and
-    * 5. The color_type is RGB or RGBA
-    */
-   if((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE)&&png_ptr->mng_features_permitted)
-      png_warning(png_ptr,"MNG features are not allowed in a PNG datastream");
-   if(filter_type != PNG_FILTER_TYPE_BASE)
-   {
-     if(!((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
-        (filter_type == PNG_INTRAPIXEL_DIFFERENCING) &&
-        ((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE) == 0) &&
-        (color_type == PNG_COLOR_TYPE_RGB ||
-         color_type == PNG_COLOR_TYPE_RGB_ALPHA)))
-        png_error(png_ptr, "Unknown filter method in IHDR");
-     if(png_ptr->mode&PNG_HAVE_PNG_SIGNATURE)
-        png_warning(png_ptr, "Invalid filter method in IHDR");
-   }
-#else
-   if(filter_type != PNG_FILTER_TYPE_BASE)
-      png_error(png_ptr, "Unknown filter method in IHDR");
-#endif
-
-   info_ptr->width = width;
-   info_ptr->height = height;
-   info_ptr->bit_depth = (png_byte)bit_depth;
-   info_ptr->color_type =(png_byte) color_type;
-   info_ptr->compression_type = (png_byte)compression_type;
-   info_ptr->filter_type = (png_byte)filter_type;
-   info_ptr->interlace_type = (png_byte)interlace_type;
-   if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
-      info_ptr->channels = 1;
-   else if (info_ptr->color_type & PNG_COLOR_MASK_COLOR)
-      info_ptr->channels = 3;
-   else
-      info_ptr->channels = 1;
-   if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA)
-      info_ptr->channels++;
-   info_ptr->pixel_depth = (png_byte)(info_ptr->channels * info_ptr->bit_depth);
-
-   /* check for potential overflow */
-   if (width > (PNG_UINT_32_MAX
-                 >> 3)      /* 8-byte RGBA pixels */
-                 - 64       /* bigrowbuf hack */
-                 - 1        /* filter byte */
-                 - 7*8      /* rounding of width to multiple of 8 pixels */
-                 - 8)       /* extra max_pixel_depth pad */
-      info_ptr->rowbytes = (png_size_t)0;
-   else
-      info_ptr->rowbytes = PNG_ROWBYTES(info_ptr->pixel_depth,width);
-}
-
-#if defined(PNG_oFFs_SUPPORTED)
-void PNGAPI
-png_set_oFFs(png_structp png_ptr, png_infop info_ptr,
-   png_int_32 offset_x, png_int_32 offset_y, int unit_type)
-{
-   png_debug1(1, "in %s storage function\n", "oFFs");
-   if (png_ptr == NULL || info_ptr == NULL)
-      return;
-
-   info_ptr->x_offset = offset_x;
-   info_ptr->y_offset = offset_y;
-   info_ptr->offset_unit_type = (png_byte)unit_type;
-   info_ptr->valid |= PNG_INFO_oFFs;
-}
-#endif
-
-#if defined(PNG_pCAL_SUPPORTED)
-void PNGAPI
-png_set_pCAL(png_structp png_ptr, png_infop info_ptr,
-   png_charp purpose, png_int_32 X0, png_int_32 X1, int type, int nparams,
-   png_charp units, png_charpp params)
-{
-   png_uint_32 length;
-   int i;
-
-   png_debug1(1, "in %s storage function\n", "pCAL");
-   if (png_ptr == NULL || info_ptr == NULL)
-      return;
-
-   length = png_strlen(purpose) + 1;
-   png_debug1(3, "allocating purpose for info (%lu bytes)\n", length);
-   info_ptr->pcal_purpose = (png_charp)png_malloc_warn(png_ptr, length);
-   if (info_ptr->pcal_purpose == NULL)
-     {
-       png_warning(png_ptr, "Insufficient memory for pCAL purpose.");
-       return;
-     }
-   png_memcpy(info_ptr->pcal_purpose, purpose, (png_size_t)length);
-
-   png_debug(3, "storing X0, X1, type, and nparams in info\n");
-   info_ptr->pcal_X0 = X0;
-   info_ptr->pcal_X1 = X1;
-   info_ptr->pcal_type = (png_byte)type;
-   info_ptr->pcal_nparams = (png_byte)nparams;
-
-   length = png_strlen(units) + 1;
-   png_debug1(3, "allocating units for info (%lu bytes)\n", length);
-   info_ptr->pcal_units = (png_charp)png_malloc_warn(png_ptr, length);
-   if (info_ptr->pcal_units == NULL)
-     {
-       png_warning(png_ptr, "Insufficient memory for pCAL units.");
-       return;
-     }
-   png_memcpy(info_ptr->pcal_units, units, (png_size_t)length);
-
-   info_ptr->pcal_params = (png_charpp)png_malloc_warn(png_ptr,
-      (png_uint_32)((nparams + 1) * png_sizeof(png_charp)));
-   if (info_ptr->pcal_params == NULL)
-     {
-       png_warning(png_ptr, "Insufficient memory for pCAL params.");
-       return;
-     }
-
-   info_ptr->pcal_params[nparams] = NULL;
-
-   for (i = 0; i < nparams; i++)
-   {
-      length = png_strlen(params[i]) + 1;
-      png_debug2(3, "allocating parameter %d for info (%lu bytes)\n", i, length);
-      info_ptr->pcal_params[i] = (png_charp)png_malloc_warn(png_ptr, length);
-      if (info_ptr->pcal_params[i] == NULL)
-        {
-          png_warning(png_ptr, "Insufficient memory for pCAL parameter.");
-          return;
-        }
-      png_memcpy(info_ptr->pcal_params[i], params[i], (png_size_t)length);
-   }
-
-   info_ptr->valid |= PNG_INFO_pCAL;
-#ifdef PNG_FREE_ME_SUPPORTED
-   info_ptr->free_me |= PNG_FREE_PCAL;
-#endif
-}
-#endif
-
-#if defined(PNG_READ_sCAL_SUPPORTED) || defined(PNG_WRITE_sCAL_SUPPORTED)
-#ifdef PNG_FLOATING_POINT_SUPPORTED
-void PNGAPI
-png_set_sCAL(png_structp png_ptr, png_infop info_ptr,
-             int unit, double width, double height)
-{
-   png_debug1(1, "in %s storage function\n", "sCAL");
-   if (png_ptr == NULL || info_ptr == NULL)
-      return;
-
-   info_ptr->scal_unit = (png_byte)unit;
-   info_ptr->scal_pixel_width = width;
-   info_ptr->scal_pixel_height = height;
-
-   info_ptr->valid |= PNG_INFO_sCAL;
-}
-#else
-#ifdef PNG_FIXED_POINT_SUPPORTED
-void PNGAPI
-png_set_sCAL_s(png_structp png_ptr, png_infop info_ptr,
-             int unit, png_charp swidth, png_charp sheight)
-{
-   png_uint_32 length;
-
-   png_debug1(1, "in %s storage function\n", "sCAL");
-   if (png_ptr == NULL || info_ptr == NULL)
-      return;
-
-   info_ptr->scal_unit = (png_byte)unit;
-
-   length = png_strlen(swidth) + 1;
-   png_debug1(3, "allocating unit for info (%d bytes)\n", length);
-   info_ptr->scal_s_width = (png_charp)png_malloc_warn(png_ptr, length);
-   if (info_ptr->scal_s_width == NULL)
-   {
-      png_warning(png_ptr,
-       "Memory allocation failed while processing sCAL.");
-      return;
-   }
-   png_memcpy(info_ptr->scal_s_width, swidth, (png_size_t)length);
-
-   length = png_strlen(sheight) + 1;
-   png_debug1(3, "allocating unit for info (%d bytes)\n", length);
-   info_ptr->scal_s_height = (png_charp)png_malloc_warn(png_ptr, length);
-   if (info_ptr->scal_s_height == NULL)
-   {
-      png_free (png_ptr, info_ptr->scal_s_width);
-      png_warning(png_ptr,
-       "Memory allocation failed while processing sCAL.");
-      return;
-   }
-   png_memcpy(info_ptr->scal_s_height, sheight, (png_size_t)length);
-   info_ptr->valid |= PNG_INFO_sCAL;
-#ifdef PNG_FREE_ME_SUPPORTED
-   info_ptr->free_me |= PNG_FREE_SCAL;
-#endif
-}
-#endif
-#endif
-#endif
-
-#if defined(PNG_pHYs_SUPPORTED)
-void PNGAPI
-png_set_pHYs(png_structp png_ptr, png_infop info_ptr,
-   png_uint_32 res_x, png_uint_32 res_y, int unit_type)
-{
-   png_debug1(1, "in %s storage function\n", "pHYs");
-   if (png_ptr == NULL || info_ptr == NULL)
-      return;
-
-   info_ptr->x_pixels_per_unit = res_x;
-   info_ptr->y_pixels_per_unit = res_y;
-   info_ptr->phys_unit_type = (png_byte)unit_type;
-   info_ptr->valid |= PNG_INFO_pHYs;
-}
-#endif
-
-void PNGAPI
-png_set_PLTE(png_structp png_ptr, png_infop info_ptr,
-   png_colorp palette, int num_palette)
-{
-
-   png_debug1(1, "in %s storage function\n", "PLTE");
-   if (png_ptr == NULL || info_ptr == NULL)
-      return;
-
-   if (num_palette < 0 || num_palette > PNG_MAX_PALETTE_LENGTH)
-     {
-       if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
-         png_error(png_ptr, "Invalid palette length");
-       else
-       {
-         png_warning(png_ptr, "Invalid palette length");
-         return;
-       }
-     }
-
-   /*
-    * It may not actually be necessary to set png_ptr->palette here;
-    * we do it for backward compatibility with the way the png_handle_tRNS
-    * function used to do the allocation.
-    */
-#ifdef PNG_FREE_ME_SUPPORTED
-   png_free_data(png_ptr, info_ptr, PNG_FREE_PLTE, 0);
-#endif
-
-   /* Changed in libpng-1.2.1 to allocate PNG_MAX_PALETTE_LENGTH instead
-      of num_palette entries,
-      in case of an invalid PNG file that has too-large sample values. */
-   png_ptr->palette = (png_colorp)png_malloc(png_ptr,
-      PNG_MAX_PALETTE_LENGTH * png_sizeof(png_color));
-   png_memset(png_ptr->palette, 0, PNG_MAX_PALETTE_LENGTH *
-      png_sizeof(png_color));
-   png_memcpy(png_ptr->palette, palette, num_palette * png_sizeof (png_color));
-   info_ptr->palette = png_ptr->palette;
-   info_ptr->num_palette = png_ptr->num_palette = (png_uint_16)num_palette;
-
-#ifdef PNG_FREE_ME_SUPPORTED
-   info_ptr->free_me |= PNG_FREE_PLTE;
-#else
-   png_ptr->flags |= PNG_FLAG_FREE_PLTE;
-#endif
-
-   info_ptr->valid |= PNG_INFO_PLTE;
-}
-
-#if defined(PNG_sBIT_SUPPORTED)
-void PNGAPI
-png_set_sBIT(png_structp png_ptr, png_infop info_ptr,
-   png_color_8p sig_bit)
-{
-   png_debug1(1, "in %s storage function\n", "sBIT");
-   if (png_ptr == NULL || info_ptr == NULL)
-      return;
-
-   png_memcpy(&(info_ptr->sig_bit), sig_bit, png_sizeof (png_color_8));
-   info_ptr->valid |= PNG_INFO_sBIT;
-}
-#endif
-
-#if defined(PNG_sRGB_SUPPORTED)
-void PNGAPI
-png_set_sRGB(png_structp png_ptr, png_infop info_ptr, int intent)
-{
-   png_debug1(1, "in %s storage function\n", "sRGB");
-   if (png_ptr == NULL || info_ptr == NULL)
-      return;
-
-   info_ptr->srgb_intent = (png_byte)intent;
-   info_ptr->valid |= PNG_INFO_sRGB;
-}
-
-void PNGAPI
-png_set_sRGB_gAMA_and_cHRM(png_structp png_ptr, png_infop info_ptr,
-   int intent)
-{
-#if defined(PNG_gAMA_SUPPORTED)
-#ifdef PNG_FLOATING_POINT_SUPPORTED
-   float file_gamma;
-#endif
-#ifdef PNG_FIXED_POINT_SUPPORTED
-   png_fixed_point int_file_gamma;
-#endif
-#endif
-#if defined(PNG_cHRM_SUPPORTED)
-#ifdef PNG_FLOATING_POINT_SUPPORTED
-   float white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y;
-#endif
-#ifdef PNG_FIXED_POINT_SUPPORTED
-   png_fixed_point int_white_x, int_white_y, int_red_x, int_red_y, int_green_x,
-      int_green_y, int_blue_x, int_blue_y;
-#endif
-#endif
-   png_debug1(1, "in %s storage function\n", "sRGB_gAMA_and_cHRM");
-   if (png_ptr == NULL || info_ptr == NULL)
-      return;
-
-   png_set_sRGB(png_ptr, info_ptr, intent);
-
-#if defined(PNG_gAMA_SUPPORTED)
-#ifdef PNG_FLOATING_POINT_SUPPORTED
-   file_gamma = (float).45455;
-   png_set_gAMA(png_ptr, info_ptr, file_gamma);
-#endif
-#ifdef PNG_FIXED_POINT_SUPPORTED
-   int_file_gamma = 45455L;
-   png_set_gAMA_fixed(png_ptr, info_ptr, int_file_gamma);
-#endif
-#endif
-
-#if defined(PNG_cHRM_SUPPORTED)
-#ifdef PNG_FIXED_POINT_SUPPORTED
-   int_white_x = 31270L;
-   int_white_y = 32900L;
-   int_red_x   = 64000L;
-   int_red_y   = 33000L;
-   int_green_x = 30000L;
-   int_green_y = 60000L;
-   int_blue_x  = 15000L;
-   int_blue_y  =  6000L;
-
-   png_set_cHRM_fixed(png_ptr, info_ptr,
-      int_white_x, int_white_y, int_red_x, int_red_y, int_green_x, int_green_y,
-      int_blue_x, int_blue_y);
-#endif
-#ifdef PNG_FLOATING_POINT_SUPPORTED
-   white_x = (float).3127;
-   white_y = (float).3290;
-   red_x   = (float).64;
-   red_y   = (float).33;
-   green_x = (float).30;
-   green_y = (float).60;
-   blue_x  = (float).15;
-   blue_y  = (float).06;
-
-   png_set_cHRM(png_ptr, info_ptr,
-      white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y);
-#endif
-#endif
-}
-#endif
-
-
-#if defined(PNG_iCCP_SUPPORTED)
-void PNGAPI
-png_set_iCCP(png_structp png_ptr, png_infop info_ptr,
-             png_charp name, int compression_type,
-             png_charp profile, png_uint_32 proflen)
-{
-   png_charp new_iccp_name;
-   png_charp new_iccp_profile;
-   png_uint_32 length;
-
-   png_debug1(1, "in %s storage function\n", "iCCP");
-   if (png_ptr == NULL || info_ptr == NULL || name == NULL || profile == NULL)
-      return;
-
-   length = png_strlen(name)+1;
-   new_iccp_name = (png_charp)png_malloc_warn(png_ptr, length);
-   if (new_iccp_name == NULL)
-   {
-      png_warning(png_ptr, "Insufficient memory to process iCCP chunk.");
-      return;
-   }
-   png_memcpy(new_iccp_name, name, length);
-   new_iccp_profile = (png_charp)png_malloc_warn(png_ptr, proflen);
-   if (new_iccp_profile == NULL)
-   {
-      png_free (png_ptr, new_iccp_name);
-      png_warning(png_ptr, "Insufficient memory to process iCCP profile.");
-      return;
-   }
-   png_memcpy(new_iccp_profile, profile, (png_size_t)proflen);
-
-   png_free_data(png_ptr, info_ptr, PNG_FREE_ICCP, 0);
-
-   info_ptr->iccp_proflen = proflen;
-   info_ptr->iccp_name = new_iccp_name;
-   info_ptr->iccp_profile = new_iccp_profile;
-   /* Compression is always zero but is here so the API and info structure
-    * does not have to change if we introduce multiple compression types */
-   info_ptr->iccp_compression = (png_byte)compression_type;
-#ifdef PNG_FREE_ME_SUPPORTED
-   info_ptr->free_me |= PNG_FREE_ICCP;
-#endif
-   info_ptr->valid |= PNG_INFO_iCCP;
-}
-#endif
-
-#if defined(PNG_TEXT_SUPPORTED)
-void PNGAPI
-png_set_text(png_structp png_ptr, png_infop info_ptr, png_textp text_ptr,
-   int num_text)
-{
-   int ret;
-   ret=png_set_text_2(png_ptr, info_ptr, text_ptr, num_text);
-   if (ret)
-     png_error(png_ptr, "Insufficient memory to store text");
-}
-
-int /* PRIVATE */
-png_set_text_2(png_structp png_ptr, png_infop info_ptr, png_textp text_ptr,
-   int num_text)
-{
-   int i;
-
-   png_debug1(1, "in %s storage function\n", (png_ptr->chunk_name[0] == '\0' ?
-      "text" : (png_const_charp)png_ptr->chunk_name));
-
-   if (png_ptr == NULL || info_ptr == NULL || num_text == 0)
-      return(0);
-
-   /* Make sure we have enough space in the "text" array in info_struct
-    * to hold all of the incoming text_ptr objects.
-    */
-   if (info_ptr->num_text + num_text > info_ptr->max_text)
-   {
-      if (info_ptr->text != NULL)
-      {
-         png_textp old_text;
-         int old_max;
-
-         old_max = info_ptr->max_text;
-         info_ptr->max_text = info_ptr->num_text + num_text + 8;
-         old_text = info_ptr->text;
-         info_ptr->text = (png_textp)png_malloc_warn(png_ptr,
-            (png_uint_32)(info_ptr->max_text * png_sizeof (png_text)));
-         if (info_ptr->text == NULL)
-           {
-             png_free(png_ptr, old_text);
-             return(1);
-           }
-         png_memcpy(info_ptr->text, old_text, (png_size_t)(old_max *
-            png_sizeof(png_text)));
-         png_free(png_ptr, old_text);
-      }
-      else
-      {
-         info_ptr->max_text = num_text + 8;
-         info_ptr->num_text = 0;
-         info_ptr->text = (png_textp)png_malloc_warn(png_ptr,
-            (png_uint_32)(info_ptr->max_text * png_sizeof (png_text)));
-         if (info_ptr->text == NULL)
-           return(1);
-#ifdef PNG_FREE_ME_SUPPORTED
-         info_ptr->free_me |= PNG_FREE_TEXT;
-#endif
-      }
-      png_debug1(3, "allocated %d entries for info_ptr->text\n",
-         info_ptr->max_text);
-   }
-   for (i = 0; i < num_text; i++)
-   {
-      png_size_t text_length,key_len;
-      png_size_t lang_len,lang_key_len;
-      png_textp textp = &(info_ptr->text[info_ptr->num_text]);
-
-      if (text_ptr[i].key == NULL)
-          continue;
-
-      key_len = png_strlen(text_ptr[i].key);
-
-      if(text_ptr[i].compression <= 0)
-      {
-        lang_len = 0;
-        lang_key_len = 0;
-      }
-      else
-#ifdef PNG_iTXt_SUPPORTED
-      {
-        /* set iTXt data */
-        if (text_ptr[i].lang != NULL)
-          lang_len = png_strlen(text_ptr[i].lang);
-        else
-          lang_len = 0;
-        if (text_ptr[i].lang_key != NULL)
-          lang_key_len = png_strlen(text_ptr[i].lang_key);
-        else
-          lang_key_len = 0;
-      }
-#else
-      {
-        png_warning(png_ptr, "iTXt chunk not supported.");
-        continue;
-      }
-#endif
-
-      if (text_ptr[i].text == NULL || text_ptr[i].text[0] == '\0')
-      {
-         text_length = 0;
-#ifdef PNG_iTXt_SUPPORTED
-         if(text_ptr[i].compression > 0)
-            textp->compression = PNG_ITXT_COMPRESSION_NONE;
-         else
-#endif
-            textp->compression = PNG_TEXT_COMPRESSION_NONE;
-      }
-      else
-      {
-         text_length = png_strlen(text_ptr[i].text);
-         textp->compression = text_ptr[i].compression;
-      }
-
-      textp->key = (png_charp)png_malloc_warn(png_ptr,
-         (png_uint_32)(key_len + text_length + lang_len + lang_key_len + 4));
-      if (textp->key == NULL)
-        return(1);
-      png_debug2(2, "Allocated %lu bytes at %x in png_set_text\n",
-         (png_uint_32)(key_len + lang_len + lang_key_len + text_length + 4),
-         (int)textp->key);
-
-      png_memcpy(textp->key, text_ptr[i].key,
-         (png_size_t)(key_len));
-      *(textp->key+key_len) = '\0';
-#ifdef PNG_iTXt_SUPPORTED
-      if (text_ptr[i].compression > 0)
-      {
-         textp->lang=textp->key + key_len + 1;
-         png_memcpy(textp->lang, text_ptr[i].lang, lang_len);
-         *(textp->lang+lang_len) = '\0';
-         textp->lang_key=textp->lang + lang_len + 1;
-         png_memcpy(textp->lang_key, text_ptr[i].lang_key, lang_key_len);
-         *(textp->lang_key+lang_key_len) = '\0';
-         textp->text=textp->lang_key + lang_key_len + 1;
-      }
-      else
-#endif
-      {
-#ifdef PNG_iTXt_SUPPORTED
-         textp->lang=NULL;
-         textp->lang_key=NULL;
-#endif
-         textp->text=textp->key + key_len + 1;
-      }
-      if(text_length)
-         png_memcpy(textp->text, text_ptr[i].text,
-            (png_size_t)(text_length));
-      *(textp->text+text_length) = '\0';
-
-#ifdef PNG_iTXt_SUPPORTED
-      if(textp->compression > 0)
-      {
-         textp->text_length = 0;
-         textp->itxt_length = text_length;
-      }
-      else
-#endif
-      {
-         textp->text_length = text_length;
-#ifdef PNG_iTXt_SUPPORTED
-         textp->itxt_length = 0;
-#endif
-      }
-      info_ptr->num_text++;
-      png_debug1(3, "transferred text chunk %d\n", info_ptr->num_text);
-   }
-   return(0);
-}
-#endif
-
-#if defined(PNG_tIME_SUPPORTED)
-void PNGAPI
-png_set_tIME(png_structp png_ptr, png_infop info_ptr, png_timep mod_time)
-{
-   png_debug1(1, "in %s storage function\n", "tIME");
-   if (png_ptr == NULL || info_ptr == NULL ||
-       (png_ptr->mode & PNG_WROTE_tIME))
-      return;
-
-   png_memcpy(&(info_ptr->mod_time), mod_time, png_sizeof (png_time));
-   info_ptr->valid |= PNG_INFO_tIME;
-}
-#endif
-
-#if defined(PNG_tRNS_SUPPORTED)
-void PNGAPI
-png_set_tRNS(png_structp png_ptr, png_infop info_ptr,
-   png_bytep trans, int num_trans, png_color_16p trans_values)
-{
-   png_debug1(1, "in %s storage function\n", "tRNS");
-   if (png_ptr == NULL || info_ptr == NULL)
-      return;
-
-   png_free_data(png_ptr, info_ptr, PNG_FREE_TRNS, 0);
-
-   if (trans != NULL)
-   {
-       /*
-        * It may not actually be necessary to set png_ptr->trans here;
-        * we do it for backward compatibility with the way the png_handle_tRNS
-        * function used to do the allocation.
-        */
-
-       /* Changed from num_trans to PNG_MAX_PALETTE_LENGTH in version 1.2.1 */
-       png_ptr->trans = info_ptr->trans = (png_bytep)png_malloc(png_ptr,
-           (png_uint_32)PNG_MAX_PALETTE_LENGTH);
-       if (num_trans > 0 && num_trans <= PNG_MAX_PALETTE_LENGTH)
-         png_memcpy(info_ptr->trans, trans, (png_size_t)num_trans);
-   }
-
-   if (trans_values != NULL)
-   {
-      int sample_max = (1 << info_ptr->bit_depth);
-      if ((info_ptr->color_type == PNG_COLOR_TYPE_GRAY &&
-          (int)trans_values->gray > sample_max) ||
-          (info_ptr->color_type == PNG_COLOR_TYPE_RGB &&
-          ((int)trans_values->red > sample_max ||
-          (int)trans_values->green > sample_max ||
-          (int)trans_values->blue > sample_max)))
-        png_warning(png_ptr,
-           "tRNS chunk has out-of-range samples for bit_depth");
-      png_memcpy(&(info_ptr->trans_values), trans_values,
-         png_sizeof(png_color_16));
-      if (num_trans == 0)
-        num_trans = 1;
-   }
-
-   info_ptr->num_trans = (png_uint_16)num_trans;
-   if (num_trans != 0)
-   {
-      info_ptr->valid |= PNG_INFO_tRNS;
-#ifdef PNG_FREE_ME_SUPPORTED
-      info_ptr->free_me |= PNG_FREE_TRNS;
-#else
-      png_ptr->flags |= PNG_FLAG_FREE_TRNS;
-#endif
-   }
-}
-#endif
-
-#if defined(PNG_sPLT_SUPPORTED)
-void PNGAPI
-png_set_sPLT(png_structp png_ptr,
-             png_infop info_ptr, png_sPLT_tp entries, int nentries)
-{
-    png_sPLT_tp np;
-    int i;
-
-    if (png_ptr == NULL || info_ptr == NULL)
-       return;
-
-    np = (png_sPLT_tp)png_malloc_warn(png_ptr,
-        (info_ptr->splt_palettes_num + nentries) * png_sizeof(png_sPLT_t));
-    if (np == NULL)
-    {
-      png_warning(png_ptr, "No memory for sPLT palettes.");
-      return;
-    }
-
-    png_memcpy(np, info_ptr->splt_palettes,
-           info_ptr->splt_palettes_num * png_sizeof(png_sPLT_t));
-    png_free(png_ptr, info_ptr->splt_palettes);
-    info_ptr->splt_palettes=NULL;
-
-    for (i = 0; i < nentries; i++)
-    {
-        png_sPLT_tp to = np + info_ptr->splt_palettes_num + i;
-        png_sPLT_tp from = entries + i;
-        png_uint_32 length;
-
-        length = png_strlen(from->name) + 1;
-        to->name = (png_charp)png_malloc_warn(png_ptr, length);
-        if (to->name == NULL)
-        {
-           png_warning(png_ptr,
-             "Out of memory while processing sPLT chunk");
-           continue;
-        }
-        png_memcpy(to->name, from->name, length);
-        to->entries = (png_sPLT_entryp)png_malloc_warn(png_ptr,
-            from->nentries * png_sizeof(png_sPLT_entry));
-        if (to->entries == NULL)
-        {
-           png_warning(png_ptr,
-             "Out of memory while processing sPLT chunk");
-           png_free(png_ptr,to->name);
-           to->name = NULL;
-           continue;
-        }
-        png_memcpy(to->entries, from->entries,
-            from->nentries * png_sizeof(png_sPLT_entry));
-        to->nentries = from->nentries;
-        to->depth = from->depth;
-    }
-
-    info_ptr->splt_palettes = np;
-    info_ptr->splt_palettes_num += nentries;
-    info_ptr->valid |= PNG_INFO_sPLT;
-#ifdef PNG_FREE_ME_SUPPORTED
-    info_ptr->free_me |= PNG_FREE_SPLT;
-#endif
-}
-#endif /* PNG_sPLT_SUPPORTED */
-
-#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
-void PNGAPI
-png_set_unknown_chunks(png_structp png_ptr,
-   png_infop info_ptr, png_unknown_chunkp unknowns, int num_unknowns)
-{
-    png_unknown_chunkp np;
-    int i;
-
-    if (png_ptr == NULL || info_ptr == NULL || num_unknowns == 0)
-        return;
-
-    np = (png_unknown_chunkp)png_malloc_warn(png_ptr,
-        (info_ptr->unknown_chunks_num + num_unknowns) *
-        png_sizeof(png_unknown_chunk));
-    if (np == NULL)
-    {
-       png_warning(png_ptr,
-          "Out of memory while processing unknown chunk.");
-       return;
-    }
-
-    png_memcpy(np, info_ptr->unknown_chunks,
-           info_ptr->unknown_chunks_num * png_sizeof(png_unknown_chunk));
-    png_free(png_ptr, info_ptr->unknown_chunks);
-    info_ptr->unknown_chunks=NULL;
-
-    for (i = 0; i < num_unknowns; i++)
-    {
-       png_unknown_chunkp to = np + info_ptr->unknown_chunks_num + i;
-       png_unknown_chunkp from = unknowns + i;
-
-       png_memcpy((png_charp)to->name, 
-                  (png_charp)from->name, 
-                  png_sizeof(from->name));
-       to->name[png_sizeof(to->name)-1] = '\0';
-       to->size = from->size;
-       /* note our location in the read or write sequence */
-       to->location = (png_byte)(png_ptr->mode & 0xff);
-
-       if (from->size == 0)
-          to->data=NULL;
-       else
-       {
-          to->data = (png_bytep)png_malloc_warn(png_ptr, from->size);
-          if (to->data == NULL)
-          {
-             png_warning(png_ptr,
-              "Out of memory while processing unknown chunk.");
-             to->size=0;
-          }
-          else
-             png_memcpy(to->data, from->data, from->size);
-       }
-    }
-
-    info_ptr->unknown_chunks = np;
-    info_ptr->unknown_chunks_num += num_unknowns;
-#ifdef PNG_FREE_ME_SUPPORTED
-    info_ptr->free_me |= PNG_FREE_UNKN;
-#endif
-}
-void PNGAPI
-png_set_unknown_chunk_location(png_structp png_ptr, png_infop info_ptr,
-   int chunk, int location)
-{
-   if(png_ptr != NULL && info_ptr != NULL && chunk >= 0 && chunk <
-         (int)info_ptr->unknown_chunks_num)
-      info_ptr->unknown_chunks[chunk].location = (png_byte)location;
-}
-#endif
-
-#if defined(PNG_1_0_X) || defined(PNG_1_2_X)
-#if defined(PNG_READ_EMPTY_PLTE_SUPPORTED) || \
-    defined(PNG_WRITE_EMPTY_PLTE_SUPPORTED)
-void PNGAPI
-png_permit_empty_plte (png_structp png_ptr, int empty_plte_permitted)
-{
-   /* This function is deprecated in favor of png_permit_mng_features()
-      and will be removed from libpng-1.3.0 */
-   png_debug(1, "in png_permit_empty_plte, DEPRECATED.\n");
-   if (png_ptr == NULL)
-      return;
-   png_ptr->mng_features_permitted = (png_byte)
-     ((png_ptr->mng_features_permitted & (~PNG_FLAG_MNG_EMPTY_PLTE)) |
-     ((empty_plte_permitted & PNG_FLAG_MNG_EMPTY_PLTE)));
-}
-#endif
-#endif
-
-#if defined(PNG_MNG_FEATURES_SUPPORTED)
-png_uint_32 PNGAPI
-png_permit_mng_features (png_structp png_ptr, png_uint_32 mng_features)
-{
-   png_debug(1, "in png_permit_mng_features\n");
-   if (png_ptr == NULL)
-      return (png_uint_32)0;
-   png_ptr->mng_features_permitted =
-     (png_byte)(mng_features & PNG_ALL_MNG_FEATURES);
-   return (png_uint_32)png_ptr->mng_features_permitted;
-}
-#endif
-
-#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
-void PNGAPI
-png_set_keep_unknown_chunks(png_structp png_ptr, int keep, png_bytep
-   chunk_list, int num_chunks)
-{
-    png_bytep new_list, p;
-    int i, old_num_chunks;
-    if (png_ptr == NULL)
-       return;
-    if (num_chunks == 0)
-    {
-      if(keep == PNG_HANDLE_CHUNK_ALWAYS || keep == PNG_HANDLE_CHUNK_IF_SAFE)
-        png_ptr->flags |= PNG_FLAG_KEEP_UNKNOWN_CHUNKS;
-      else
-        png_ptr->flags &= ~PNG_FLAG_KEEP_UNKNOWN_CHUNKS;
-
-      if(keep == PNG_HANDLE_CHUNK_ALWAYS)
-        png_ptr->flags |= PNG_FLAG_KEEP_UNSAFE_CHUNKS;
-      else
-        png_ptr->flags &= ~PNG_FLAG_KEEP_UNSAFE_CHUNKS;
-      return;
-    }
-    if (chunk_list == NULL)
-      return;
-    old_num_chunks=png_ptr->num_chunk_list;
-    new_list=(png_bytep)png_malloc(png_ptr,
-       (png_uint_32)(5*(num_chunks+old_num_chunks)));
-    if(png_ptr->chunk_list != NULL)
-    {
-       png_memcpy(new_list, png_ptr->chunk_list,
-          (png_size_t)(5*old_num_chunks));
-       png_free(png_ptr, png_ptr->chunk_list);
-       png_ptr->chunk_list=NULL;
-    }
-    png_memcpy(new_list+5*old_num_chunks, chunk_list,
-       (png_size_t)(5*num_chunks));
-    for (p=new_list+5*old_num_chunks+4, i=0; i<num_chunks; i++, p+=5)
-       *p=(png_byte)keep;
-    png_ptr->num_chunk_list=old_num_chunks+num_chunks;
-    png_ptr->chunk_list=new_list;
-#ifdef PNG_FREE_ME_SUPPORTED
-    png_ptr->free_me |= PNG_FREE_LIST;
-#endif
-}
-#endif
-
-#if defined(PNG_READ_USER_CHUNKS_SUPPORTED)
-void PNGAPI
-png_set_read_user_chunk_fn(png_structp png_ptr, png_voidp user_chunk_ptr,
-   png_user_chunk_ptr read_user_chunk_fn)
-{
-   png_debug(1, "in png_set_read_user_chunk_fn\n");
-   if (png_ptr == NULL)
-      return;
-   png_ptr->read_user_chunk_fn = read_user_chunk_fn;
-   png_ptr->user_chunk_ptr = user_chunk_ptr;
-}
-#endif
-
-#if defined(PNG_INFO_IMAGE_SUPPORTED)
-void PNGAPI
-png_set_rows(png_structp png_ptr, png_infop info_ptr, png_bytepp row_pointers)
-{
-   png_debug1(1, "in %s storage function\n", "rows");
-
-   if (png_ptr == NULL || info_ptr == NULL)
-      return;
-
-   if(info_ptr->row_pointers && (info_ptr->row_pointers != row_pointers))
-      png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0);
-   info_ptr->row_pointers = row_pointers;
-   if(row_pointers)
-      info_ptr->valid |= PNG_INFO_IDAT;
-}
-#endif
-
-#ifdef PNG_WRITE_SUPPORTED
-void PNGAPI
-png_set_compression_buffer_size(png_structp png_ptr, png_uint_32 size)
-{
-    if (png_ptr == NULL)
-       return;
-    png_free(png_ptr, png_ptr->zbuf);
-    png_ptr->zbuf_size = (png_size_t)size;
-    png_ptr->zbuf = (png_bytep)png_malloc(png_ptr, size);
-    png_ptr->zstream.next_out = png_ptr->zbuf;
-    png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
-}
-#endif
-
-void PNGAPI
-png_set_invalid(png_structp png_ptr, png_infop info_ptr, int mask)
-{
-   if (png_ptr && info_ptr)
-      info_ptr->valid &= ~mask;
-}
-
-
-#ifndef PNG_1_0_X
-#ifdef PNG_ASSEMBLER_CODE_SUPPORTED
-/* function was added to libpng 1.2.0 and should always exist by default */
-void PNGAPI
-png_set_asm_flags (png_structp png_ptr, png_uint_32 asm_flags)
-{
-/* Obsolete as of libpng-1.2.20 and will be removed from libpng-1.4.0 */
-    if (png_ptr != NULL)
-    png_ptr->asm_flags = 0;
-}
-
-/* this function was added to libpng 1.2.0 */
-void PNGAPI
-png_set_mmx_thresholds (png_structp png_ptr,
-                        png_byte mmx_bitdepth_threshold,
-                        png_uint_32 mmx_rowbytes_threshold)
-{
-/* Obsolete as of libpng-1.2.20 and will be removed from libpng-1.4.0 */
-    if (png_ptr == NULL)
-       return;
-}
-#endif /* ?PNG_ASSEMBLER_CODE_SUPPORTED */
-
-#ifdef PNG_SET_USER_LIMITS_SUPPORTED
-/* this function was added to libpng 1.2.6 */
-void PNGAPI
-png_set_user_limits (png_structp png_ptr, png_uint_32 user_width_max,
-    png_uint_32 user_height_max)
-{
-    /* Images with dimensions larger than these limits will be
-     * rejected by png_set_IHDR().  To accept any PNG datastream
-     * regardless of dimensions, set both limits to 0x7ffffffL.
-     */
-    if(png_ptr == NULL) return;
-    png_ptr->user_width_max = user_width_max;
-    png_ptr->user_height_max = user_height_max;
-}
-#endif /* ?PNG_SET_USER_LIMITS_SUPPORTED */
-
-#endif /* ?PNG_1_0_X */
-#endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */
+\r
+/* pngset.c - storage of image information into info struct\r
+ *\r
+ * Last changed in libpng 1.4.1 [February 25, 2010]\r
+ * Copyright (c) 1998-2010 Glenn Randers-Pehrson\r
+ * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)\r
+ * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)\r
+ *\r
+ * This code is released under the libpng license.\r
+ * For conditions of distribution and use, see the disclaimer\r
+ * and license in png.h\r
+ *\r
+ * The functions here are used during reads to store data from the file\r
+ * into the info struct, and during writes to store application data\r
+ * into the info struct for writing into the file.  This abstracts the\r
+ * info struct and allows us to change the structure in the future.\r
+ */\r
+\r
+#define PNG_NO_PEDANTIC_WARNINGS\r
+#include "png.h"\r
+#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)\r
+#include "pngpriv.h"\r
+\r
+#ifdef PNG_bKGD_SUPPORTED\r
+void PNGAPI\r
+png_set_bKGD(png_structp png_ptr, png_infop info_ptr, png_color_16p background)\r
+{\r
+   png_debug1(1, "in %s storage function", "bKGD");\r
+\r
+   if (png_ptr == NULL || info_ptr == NULL)\r
+      return;\r
+\r
+   png_memcpy(&(info_ptr->background), background, png_sizeof(png_color_16));\r
+   info_ptr->valid |= PNG_INFO_bKGD;\r
+}\r
+#endif\r
+\r
+#ifdef PNG_cHRM_SUPPORTED\r
+#ifdef PNG_FLOATING_POINT_SUPPORTED\r
+void PNGAPI\r
+png_set_cHRM(png_structp png_ptr, png_infop info_ptr,\r
+   double white_x, double white_y, double red_x, double red_y,\r
+   double green_x, double green_y, double blue_x, double blue_y)\r
+{\r
+   png_debug1(1, "in %s storage function", "cHRM");\r
+\r
+   if (png_ptr == NULL || info_ptr == NULL)\r
+      return;\r
+\r
+   info_ptr->x_white = (float)white_x;\r
+   info_ptr->y_white = (float)white_y;\r
+   info_ptr->x_red   = (float)red_x;\r
+   info_ptr->y_red   = (float)red_y;\r
+   info_ptr->x_green = (float)green_x;\r
+   info_ptr->y_green = (float)green_y;\r
+   info_ptr->x_blue  = (float)blue_x;\r
+   info_ptr->y_blue  = (float)blue_y;\r
+#ifdef PNG_FIXED_POINT_SUPPORTED\r
+   info_ptr->int_x_white = (png_fixed_point)(white_x*100000.+0.5);\r
+   info_ptr->int_y_white = (png_fixed_point)(white_y*100000.+0.5);\r
+   info_ptr->int_x_red   = (png_fixed_point)(  red_x*100000.+0.5);\r
+   info_ptr->int_y_red   = (png_fixed_point)(  red_y*100000.+0.5);\r
+   info_ptr->int_x_green = (png_fixed_point)(green_x*100000.+0.5);\r
+   info_ptr->int_y_green = (png_fixed_point)(green_y*100000.+0.5);\r
+   info_ptr->int_x_blue  = (png_fixed_point)( blue_x*100000.+0.5);\r
+   info_ptr->int_y_blue  = (png_fixed_point)( blue_y*100000.+0.5);\r
+#endif\r
+   info_ptr->valid |= PNG_INFO_cHRM;\r
+}\r
+#endif /* PNG_FLOATING_POINT_SUPPORTED */\r
+\r
+#ifdef PNG_FIXED_POINT_SUPPORTED\r
+void PNGAPI\r
+png_set_cHRM_fixed(png_structp png_ptr, png_infop info_ptr,\r
+   png_fixed_point white_x, png_fixed_point white_y, png_fixed_point red_x,\r
+   png_fixed_point red_y, png_fixed_point green_x, png_fixed_point green_y,\r
+   png_fixed_point blue_x, png_fixed_point blue_y)\r
+{\r
+   png_debug1(1, "in %s storage function", "cHRM fixed");\r
+\r
+   if (png_ptr == NULL || info_ptr == NULL)\r
+      return;\r
+\r
+#ifdef PNG_CHECK_cHRM_SUPPORTED\r
+   if (png_check_cHRM_fixed(png_ptr,\r
+      white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y))\r
+#endif\r
+   {\r
+      info_ptr->int_x_white = white_x;\r
+      info_ptr->int_y_white = white_y;\r
+      info_ptr->int_x_red   = red_x;\r
+      info_ptr->int_y_red   = red_y;\r
+      info_ptr->int_x_green = green_x;\r
+      info_ptr->int_y_green = green_y;\r
+      info_ptr->int_x_blue  = blue_x;\r
+      info_ptr->int_y_blue  = blue_y;\r
+#ifdef  PNG_FLOATING_POINT_SUPPORTED\r
+      info_ptr->x_white = (float)(white_x/100000.);\r
+      info_ptr->y_white = (float)(white_y/100000.);\r
+      info_ptr->x_red   = (float)(  red_x/100000.);\r
+      info_ptr->y_red   = (float)(  red_y/100000.);\r
+      info_ptr->x_green = (float)(green_x/100000.);\r
+      info_ptr->y_green = (float)(green_y/100000.);\r
+      info_ptr->x_blue  = (float)( blue_x/100000.);\r
+      info_ptr->y_blue  = (float)( blue_y/100000.);\r
+#endif\r
+      info_ptr->valid |= PNG_INFO_cHRM;\r
+   }\r
+}\r
+#endif /* PNG_FIXED_POINT_SUPPORTED */\r
+#endif /* PNG_cHRM_SUPPORTED */\r
+\r
+#ifdef PNG_gAMA_SUPPORTED\r
+#ifdef PNG_FLOATING_POINT_SUPPORTED\r
+void PNGAPI\r
+png_set_gAMA(png_structp png_ptr, png_infop info_ptr, double file_gamma)\r
+{\r
+   double png_gamma;\r
+\r
+   png_debug1(1, "in %s storage function", "gAMA");\r
+\r
+   if (png_ptr == NULL || info_ptr == NULL)\r
+      return;\r
+\r
+   /* Check for overflow */\r
+   if (file_gamma > 21474.83)\r
+   {\r
+      png_warning(png_ptr, "Limiting gamma to 21474.83");\r
+      png_gamma=21474.83;\r
+   }\r
+   else\r
+      png_gamma = file_gamma;\r
+   info_ptr->gamma = (float)png_gamma;\r
+#ifdef PNG_FIXED_POINT_SUPPORTED\r
+   info_ptr->int_gamma = (int)(png_gamma*100000.+.5);\r
+#endif\r
+   info_ptr->valid |= PNG_INFO_gAMA;\r
+   if (png_gamma == 0.0)\r
+      png_warning(png_ptr, "Setting gamma=0");\r
+}\r
+#endif\r
+void PNGAPI\r
+png_set_gAMA_fixed(png_structp png_ptr, png_infop info_ptr, png_fixed_point\r
+   int_gamma)\r
+{\r
+   png_fixed_point png_gamma;\r
+\r
+   png_debug1(1, "in %s storage function", "gAMA");\r
+\r
+   if (png_ptr == NULL || info_ptr == NULL)\r
+      return;\r
+\r
+   if (int_gamma > (png_fixed_point)PNG_UINT_31_MAX)\r
+   {\r
+      png_warning(png_ptr, "Limiting gamma to 21474.83");\r
+      png_gamma=PNG_UINT_31_MAX;\r
+   }\r
+   else\r
+   {\r
+      if (int_gamma < 0)\r
+      {\r
+         png_warning(png_ptr, "Setting negative gamma to zero");\r
+         png_gamma = 0;\r
+      }\r
+      else\r
+         png_gamma = int_gamma;\r
+   }\r
+#ifdef PNG_FLOATING_POINT_SUPPORTED\r
+   info_ptr->gamma = (float)(png_gamma/100000.);\r
+#endif\r
+#ifdef PNG_FIXED_POINT_SUPPORTED\r
+   info_ptr->int_gamma = png_gamma;\r
+#endif\r
+   info_ptr->valid |= PNG_INFO_gAMA;\r
+   if (png_gamma == 0)\r
+      png_warning(png_ptr, "Setting gamma=0");\r
+}\r
+#endif\r
+\r
+#ifdef PNG_hIST_SUPPORTED\r
+void PNGAPI\r
+png_set_hIST(png_structp png_ptr, png_infop info_ptr, png_uint_16p hist)\r
+{\r
+   int i;\r
+\r
+   png_debug1(1, "in %s storage function", "hIST");\r
+\r
+   if (png_ptr == NULL || info_ptr == NULL)\r
+      return;\r
+\r
+   if (info_ptr->num_palette == 0 || info_ptr->num_palette\r
+       > PNG_MAX_PALETTE_LENGTH)\r
+   {\r
+      png_warning(png_ptr,\r
+         "Invalid palette size, hIST allocation skipped");\r
+      return;\r
+   }\r
+\r
+   png_free_data(png_ptr, info_ptr, PNG_FREE_HIST, 0);\r
+   /* Changed from info->num_palette to PNG_MAX_PALETTE_LENGTH in\r
+    * version 1.2.1\r
+    */\r
+   png_ptr->hist = (png_uint_16p)png_malloc_warn(png_ptr,\r
+      PNG_MAX_PALETTE_LENGTH * png_sizeof(png_uint_16));\r
+   if (png_ptr->hist == NULL)\r
+   {\r
+      png_warning(png_ptr, "Insufficient memory for hIST chunk data");\r
+      return;\r
+   }\r
+\r
+   for (i = 0; i < info_ptr->num_palette; i++)\r
+      png_ptr->hist[i] = hist[i];\r
+   info_ptr->hist = png_ptr->hist;\r
+   info_ptr->valid |= PNG_INFO_hIST;\r
+\r
+   info_ptr->free_me |= PNG_FREE_HIST;\r
+}\r
+#endif\r
+\r
+void PNGAPI\r
+png_set_IHDR(png_structp png_ptr, png_infop info_ptr,\r
+   png_uint_32 width, png_uint_32 height, int bit_depth,\r
+   int color_type, int interlace_type, int compression_type,\r
+   int filter_type)\r
+{\r
+   png_debug1(1, "in %s storage function", "IHDR");\r
+\r
+   if (png_ptr == NULL || info_ptr == NULL)\r
+      return;\r
+\r
+   info_ptr->width = width;\r
+   info_ptr->height = height;\r
+   info_ptr->bit_depth = (png_byte)bit_depth;\r
+   info_ptr->color_type = (png_byte)color_type;\r
+   info_ptr->compression_type = (png_byte)compression_type;\r
+   info_ptr->filter_type = (png_byte)filter_type;\r
+   info_ptr->interlace_type = (png_byte)interlace_type;\r
+\r
+   png_check_IHDR (png_ptr, info_ptr->width, info_ptr->height,\r
+       info_ptr->bit_depth, info_ptr->color_type, info_ptr->interlace_type,\r
+       info_ptr->compression_type, info_ptr->filter_type);\r
+\r
+   if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)\r
+      info_ptr->channels = 1;\r
+   else if (info_ptr->color_type & PNG_COLOR_MASK_COLOR)\r
+      info_ptr->channels = 3;\r
+   else\r
+      info_ptr->channels = 1;\r
+   if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA)\r
+      info_ptr->channels++;\r
+   info_ptr->pixel_depth = (png_byte)(info_ptr->channels * info_ptr->bit_depth);\r
+\r
+   /* Check for potential overflow */\r
+   if (width > (PNG_UINT_32_MAX\r
+                 >> 3)      /* 8-byte RGBA pixels */\r
+                 - 64       /* bigrowbuf hack */\r
+                 - 1        /* filter byte */\r
+                 - 7*8      /* rounding of width to multiple of 8 pixels */\r
+                 - 8)       /* extra max_pixel_depth pad */\r
+      info_ptr->rowbytes = 0;\r
+   else\r
+      info_ptr->rowbytes = PNG_ROWBYTES(info_ptr->pixel_depth, width);\r
+}\r
+\r
+#ifdef PNG_oFFs_SUPPORTED\r
+void PNGAPI\r
+png_set_oFFs(png_structp png_ptr, png_infop info_ptr,\r
+   png_int_32 offset_x, png_int_32 offset_y, int unit_type)\r
+{\r
+   png_debug1(1, "in %s storage function", "oFFs");\r
+\r
+   if (png_ptr == NULL || info_ptr == NULL)\r
+      return;\r
+\r
+   info_ptr->x_offset = offset_x;\r
+   info_ptr->y_offset = offset_y;\r
+   info_ptr->offset_unit_type = (png_byte)unit_type;\r
+   info_ptr->valid |= PNG_INFO_oFFs;\r
+}\r
+#endif\r
+\r
+#ifdef PNG_pCAL_SUPPORTED\r
+void PNGAPI\r
+png_set_pCAL(png_structp png_ptr, png_infop info_ptr,\r
+   png_charp purpose, png_int_32 X0, png_int_32 X1, int type, int nparams,\r
+   png_charp units, png_charpp params)\r
+{\r
+   png_size_t length;\r
+   int i;\r
+\r
+   png_debug1(1, "in %s storage function", "pCAL");\r
+\r
+   if (png_ptr == NULL || info_ptr == NULL)\r
+      return;\r
+\r
+   length = png_strlen(purpose) + 1;\r
+   png_debug1(3, "allocating purpose for info (%lu bytes)",\r
+     (unsigned long)length);\r
+   info_ptr->pcal_purpose = (png_charp)png_malloc_warn(png_ptr, length);\r
+   if (info_ptr->pcal_purpose == NULL)\r
+   {\r
+      png_warning(png_ptr, "Insufficient memory for pCAL purpose");\r
+      return;\r
+   }\r
+   png_memcpy(info_ptr->pcal_purpose, purpose, length);\r
+\r
+   png_debug(3, "storing X0, X1, type, and nparams in info");\r
+   info_ptr->pcal_X0 = X0;\r
+   info_ptr->pcal_X1 = X1;\r
+   info_ptr->pcal_type = (png_byte)type;\r
+   info_ptr->pcal_nparams = (png_byte)nparams;\r
+\r
+   length = png_strlen(units) + 1;\r
+   png_debug1(3, "allocating units for info (%lu bytes)",\r
+     (unsigned long)length);\r
+   info_ptr->pcal_units = (png_charp)png_malloc_warn(png_ptr, length);\r
+   if (info_ptr->pcal_units == NULL)\r
+   {\r
+      png_warning(png_ptr, "Insufficient memory for pCAL units");\r
+      return;\r
+   }\r
+   png_memcpy(info_ptr->pcal_units, units, length);\r
+\r
+   info_ptr->pcal_params = (png_charpp)png_malloc_warn(png_ptr,\r
+      (png_size_t)((nparams + 1) * png_sizeof(png_charp)));\r
+   if (info_ptr->pcal_params == NULL)\r
+   {\r
+      png_warning(png_ptr, "Insufficient memory for pCAL params");\r
+      return;\r
+   }\r
+\r
+   png_memset(info_ptr->pcal_params, 0, (nparams + 1) * png_sizeof(png_charp));\r
+\r
+   for (i = 0; i < nparams; i++)\r
+   {\r
+      length = png_strlen(params[i]) + 1;\r
+      png_debug2(3, "allocating parameter %d for info (%lu bytes)", i,\r
+        (unsigned long)length);\r
+      info_ptr->pcal_params[i] = (png_charp)png_malloc_warn(png_ptr, length);\r
+      if (info_ptr->pcal_params[i] == NULL)\r
+      {\r
+         png_warning(png_ptr, "Insufficient memory for pCAL parameter");\r
+         return;\r
+      }\r
+      png_memcpy(info_ptr->pcal_params[i], params[i], length);\r
+   }\r
+\r
+   info_ptr->valid |= PNG_INFO_pCAL;\r
+   info_ptr->free_me |= PNG_FREE_PCAL;\r
+}\r
+#endif\r
+\r
+#if defined(PNG_READ_sCAL_SUPPORTED) || defined(PNG_WRITE_sCAL_SUPPORTED)\r
+#ifdef PNG_FLOATING_POINT_SUPPORTED\r
+void PNGAPI\r
+png_set_sCAL(png_structp png_ptr, png_infop info_ptr,\r
+             int unit, double width, double height)\r
+{\r
+   png_debug1(1, "in %s storage function", "sCAL");\r
+\r
+   if (png_ptr == NULL || info_ptr == NULL)\r
+      return;\r
+\r
+   info_ptr->scal_unit = (png_byte)unit;\r
+   info_ptr->scal_pixel_width = width;\r
+   info_ptr->scal_pixel_height = height;\r
+\r
+   info_ptr->valid |= PNG_INFO_sCAL;\r
+}\r
+#else\r
+#ifdef PNG_FIXED_POINT_SUPPORTED\r
+void PNGAPI\r
+png_set_sCAL_s(png_structp png_ptr, png_infop info_ptr,\r
+             int unit, png_charp swidth, png_charp sheight)\r
+{\r
+   png_size_t length;\r
+\r
+   png_debug1(1, "in %s storage function", "sCAL");\r
+\r
+   if (png_ptr == NULL || info_ptr == NULL)\r
+      return;\r
+\r
+   info_ptr->scal_unit = (png_byte)unit;\r
+\r
+   length = png_strlen(swidth) + 1;\r
+   png_debug1(3, "allocating unit for info (%u bytes)",\r
+      (unsigned int)length);\r
+   info_ptr->scal_s_width = (png_charp)png_malloc_warn(png_ptr, length);\r
+   if (info_ptr->scal_s_width == NULL)\r
+   {\r
+      png_warning(png_ptr,\r
+         "Memory allocation failed while processing sCAL");\r
+      return;\r
+   }\r
+   png_memcpy(info_ptr->scal_s_width, swidth, length);\r
+\r
+   length = png_strlen(sheight) + 1;\r
+   png_debug1(3, "allocating unit for info (%u bytes)",\r
+      (unsigned int)length);\r
+   info_ptr->scal_s_height = (png_charp)png_malloc_warn(png_ptr, length);\r
+   if (info_ptr->scal_s_height == NULL)\r
+   {\r
+      png_free (png_ptr, info_ptr->scal_s_width);\r
+      info_ptr->scal_s_width = NULL;\r
+      png_warning(png_ptr,\r
+         "Memory allocation failed while processing sCAL");\r
+      return;\r
+   }\r
+   png_memcpy(info_ptr->scal_s_height, sheight, length);\r
+   info_ptr->valid |= PNG_INFO_sCAL;\r
+   info_ptr->free_me |= PNG_FREE_SCAL;\r
+}\r
+#endif\r
+#endif\r
+#endif\r
+\r
+#ifdef PNG_pHYs_SUPPORTED\r
+void PNGAPI\r
+png_set_pHYs(png_structp png_ptr, png_infop info_ptr,\r
+   png_uint_32 res_x, png_uint_32 res_y, int unit_type)\r
+{\r
+   png_debug1(1, "in %s storage function", "pHYs");\r
+\r
+   if (png_ptr == NULL || info_ptr == NULL)\r
+      return;\r
+\r
+   info_ptr->x_pixels_per_unit = res_x;\r
+   info_ptr->y_pixels_per_unit = res_y;\r
+   info_ptr->phys_unit_type = (png_byte)unit_type;\r
+   info_ptr->valid |= PNG_INFO_pHYs;\r
+}\r
+#endif\r
+\r
+void PNGAPI\r
+png_set_PLTE(png_structp png_ptr, png_infop info_ptr,\r
+   png_colorp palette, int num_palette)\r
+{\r
+\r
+   png_debug1(1, "in %s storage function", "PLTE");\r
+\r
+   if (png_ptr == NULL || info_ptr == NULL)\r
+      return;\r
+\r
+   if (num_palette < 0 || num_palette > PNG_MAX_PALETTE_LENGTH)\r
+   {\r
+      if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)\r
+         png_error(png_ptr, "Invalid palette length");\r
+      else\r
+      {\r
+         png_warning(png_ptr, "Invalid palette length");\r
+         return;\r
+      }\r
+   }\r
+\r
+   /* It may not actually be necessary to set png_ptr->palette here;\r
+    * we do it for backward compatibility with the way the png_handle_tRNS\r
+    * function used to do the allocation.\r
+    */\r
+   png_free_data(png_ptr, info_ptr, PNG_FREE_PLTE, 0);\r
+\r
+   /* Changed in libpng-1.2.1 to allocate PNG_MAX_PALETTE_LENGTH instead\r
+    * of num_palette entries, in case of an invalid PNG file that has\r
+    * too-large sample values.\r
+    */\r
+   png_ptr->palette = (png_colorp)png_calloc(png_ptr,\r
+      PNG_MAX_PALETTE_LENGTH * png_sizeof(png_color));\r
+   png_memcpy(png_ptr->palette, palette, num_palette * png_sizeof(png_color));\r
+   info_ptr->palette = png_ptr->palette;\r
+   info_ptr->num_palette = png_ptr->num_palette = (png_uint_16)num_palette;\r
+\r
+   info_ptr->free_me |= PNG_FREE_PLTE;\r
+\r
+   info_ptr->valid |= PNG_INFO_PLTE;\r
+}\r
+\r
+#ifdef PNG_sBIT_SUPPORTED\r
+void PNGAPI\r
+png_set_sBIT(png_structp png_ptr, png_infop info_ptr,\r
+   png_color_8p sig_bit)\r
+{\r
+   png_debug1(1, "in %s storage function", "sBIT");\r
+\r
+   if (png_ptr == NULL || info_ptr == NULL)\r
+      return;\r
+\r
+   png_memcpy(&(info_ptr->sig_bit), sig_bit, png_sizeof(png_color_8));\r
+   info_ptr->valid |= PNG_INFO_sBIT;\r
+}\r
+#endif\r
+\r
+#ifdef PNG_sRGB_SUPPORTED\r
+void PNGAPI\r
+png_set_sRGB(png_structp png_ptr, png_infop info_ptr, int intent)\r
+{\r
+   png_debug1(1, "in %s storage function", "sRGB");\r
+\r
+   if (png_ptr == NULL || info_ptr == NULL)\r
+      return;\r
+\r
+   info_ptr->srgb_intent = (png_byte)intent;\r
+   info_ptr->valid |= PNG_INFO_sRGB;\r
+}\r
+\r
+void PNGAPI\r
+png_set_sRGB_gAMA_and_cHRM(png_structp png_ptr, png_infop info_ptr,\r
+   int intent)\r
+{\r
+#ifdef PNG_gAMA_SUPPORTED\r
+#ifdef PNG_FLOATING_POINT_SUPPORTED\r
+   float file_gamma;\r
+#endif\r
+#ifdef PNG_FIXED_POINT_SUPPORTED\r
+   png_fixed_point int_file_gamma;\r
+#endif\r
+#endif\r
+#ifdef PNG_cHRM_SUPPORTED\r
+#ifdef PNG_FLOATING_POINT_SUPPORTED\r
+   float white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y;\r
+#endif\r
+   png_fixed_point int_white_x, int_white_y, int_red_x, int_red_y, int_green_x,\r
+      int_green_y, int_blue_x, int_blue_y;\r
+#endif\r
+   png_debug1(1, "in %s storage function", "sRGB_gAMA_and_cHRM");\r
+\r
+   if (png_ptr == NULL || info_ptr == NULL)\r
+      return;\r
+\r
+   png_set_sRGB(png_ptr, info_ptr, intent);\r
+\r
+#ifdef PNG_gAMA_SUPPORTED\r
+#ifdef PNG_FLOATING_POINT_SUPPORTED\r
+   file_gamma = (float).45455;\r
+   png_set_gAMA(png_ptr, info_ptr, file_gamma);\r
+#endif\r
+#ifdef PNG_FIXED_POINT_SUPPORTED\r
+   int_file_gamma = 45455L;\r
+   png_set_gAMA_fixed(png_ptr, info_ptr, int_file_gamma);\r
+#endif\r
+#endif\r
+\r
+#ifdef PNG_cHRM_SUPPORTED\r
+   int_white_x = 31270L;\r
+   int_white_y = 32900L;\r
+   int_red_x   = 64000L;\r
+   int_red_y   = 33000L;\r
+   int_green_x = 30000L;\r
+   int_green_y = 60000L;\r
+   int_blue_x  = 15000L;\r
+   int_blue_y  =  6000L;\r
+\r
+#ifdef PNG_FLOATING_POINT_SUPPORTED\r
+   white_x = (float).3127;\r
+   white_y = (float).3290;\r
+   red_x   = (float).64;\r
+   red_y   = (float).33;\r
+   green_x = (float).30;\r
+   green_y = (float).60;\r
+   blue_x  = (float).15;\r
+   blue_y  = (float).06;\r
+#endif\r
+\r
+#ifdef PNG_FIXED_POINT_SUPPORTED\r
+   png_set_cHRM_fixed(png_ptr, info_ptr,\r
+       int_white_x, int_white_y, int_red_x, int_red_y, int_green_x,\r
+       int_green_y, int_blue_x, int_blue_y);\r
+#endif\r
+#ifdef PNG_FLOATING_POINT_SUPPORTED\r
+   png_set_cHRM(png_ptr, info_ptr,\r
+       white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y);\r
+#endif\r
+#endif /* cHRM */\r
+}\r
+#endif /* sRGB */\r
+\r
+\r
+#ifdef PNG_iCCP_SUPPORTED\r
+void PNGAPI\r
+png_set_iCCP(png_structp png_ptr, png_infop info_ptr,\r
+             png_charp name, int compression_type,\r
+             png_charp profile, png_uint_32 proflen)\r
+{\r
+   png_charp new_iccp_name;\r
+   png_charp new_iccp_profile;\r
+   png_uint_32 length;\r
+\r
+   png_debug1(1, "in %s storage function", "iCCP");\r
+\r
+   if (png_ptr == NULL || info_ptr == NULL || name == NULL || profile == NULL)\r
+      return;\r
+\r
+   length = png_strlen(name)+1;\r
+   new_iccp_name = (png_charp)png_malloc_warn(png_ptr, length);\r
+   if (new_iccp_name == NULL)\r
+   {\r
+        png_warning(png_ptr, "Insufficient memory to process iCCP chunk");\r
+      return;\r
+   }\r
+   png_memcpy(new_iccp_name, name, length);\r
+   new_iccp_profile = (png_charp)png_malloc_warn(png_ptr, proflen);\r
+   if (new_iccp_profile == NULL)\r
+   {\r
+      png_free (png_ptr, new_iccp_name);\r
+      png_warning(png_ptr,\r
+          "Insufficient memory to process iCCP profile");\r
+      return;\r
+   }\r
+   png_memcpy(new_iccp_profile, profile, (png_size_t)proflen);\r
+\r
+   png_free_data(png_ptr, info_ptr, PNG_FREE_ICCP, 0);\r
+\r
+   info_ptr->iccp_proflen = proflen;\r
+   info_ptr->iccp_name = new_iccp_name;\r
+   info_ptr->iccp_profile = new_iccp_profile;\r
+   /* Compression is always zero but is here so the API and info structure\r
+    * does not have to change if we introduce multiple compression types\r
+    */\r
+   info_ptr->iccp_compression = (png_byte)compression_type;\r
+   info_ptr->free_me |= PNG_FREE_ICCP;\r
+   info_ptr->valid |= PNG_INFO_iCCP;\r
+}\r
+#endif\r
+\r
+#ifdef PNG_TEXT_SUPPORTED\r
+void PNGAPI\r
+png_set_text(png_structp png_ptr, png_infop info_ptr, png_textp text_ptr,\r
+             int num_text)\r
+{\r
+   int ret;\r
+   ret = png_set_text_2(png_ptr, info_ptr, text_ptr, num_text);\r
+   if (ret)\r
+      png_error(png_ptr, "Insufficient memory to store text");\r
+}\r
+\r
+int /* PRIVATE */\r
+png_set_text_2(png_structp png_ptr, png_infop info_ptr, png_textp text_ptr,\r
+               int num_text)\r
+{\r
+   int i;\r
+\r
+   png_debug1(1, "in %s storage function", ((png_ptr == NULL ||\r
+      png_ptr->chunk_name[0] == '\0') ?\r
+      "text" : (png_const_charp)png_ptr->chunk_name));\r
+\r
+   if (png_ptr == NULL || info_ptr == NULL || num_text == 0)\r
+      return(0);\r
+\r
+   /* Make sure we have enough space in the "text" array in info_struct\r
+    * to hold all of the incoming text_ptr objects.\r
+    */\r
+   if (info_ptr->num_text + num_text > info_ptr->max_text)\r
+   {\r
+      if (info_ptr->text != NULL)\r
+      {\r
+         png_textp old_text;\r
+         int old_max;\r
+\r
+         old_max = info_ptr->max_text;\r
+         info_ptr->max_text = info_ptr->num_text + num_text + 8;\r
+         old_text = info_ptr->text;\r
+         info_ptr->text = (png_textp)png_malloc_warn(png_ptr,\r
+            (png_size_t)(info_ptr->max_text * png_sizeof(png_text)));\r
+         if (info_ptr->text == NULL)\r
+         {\r
+            png_free(png_ptr, old_text);\r
+            return(1);\r
+         }\r
+         png_memcpy(info_ptr->text, old_text, (png_size_t)(old_max *\r
+            png_sizeof(png_text)));\r
+         png_free(png_ptr, old_text);\r
+      }\r
+      else\r
+      {\r
+         info_ptr->max_text = num_text + 8;\r
+         info_ptr->num_text = 0;\r
+         info_ptr->text = (png_textp)png_malloc_warn(png_ptr,\r
+            (png_size_t)(info_ptr->max_text * png_sizeof(png_text)));\r
+         if (info_ptr->text == NULL)\r
+            return(1);\r
+         info_ptr->free_me |= PNG_FREE_TEXT;\r
+      }\r
+      png_debug1(3, "allocated %d entries for info_ptr->text",\r
+         info_ptr->max_text);\r
+   }\r
+   for (i = 0; i < num_text; i++)\r
+   {\r
+      png_size_t text_length, key_len;\r
+      png_size_t lang_len, lang_key_len;\r
+      png_textp textp = &(info_ptr->text[info_ptr->num_text]);\r
+\r
+      if (text_ptr[i].key == NULL)\r
+          continue;\r
+\r
+      key_len = png_strlen(text_ptr[i].key);\r
+\r
+      if (text_ptr[i].compression <= 0)\r
+      {\r
+         lang_len = 0;\r
+         lang_key_len = 0;\r
+      }\r
+\r
+      else\r
+#ifdef PNG_iTXt_SUPPORTED\r
+      {\r
+         /* Set iTXt data */\r
+\r
+         if (text_ptr[i].lang != NULL)\r
+            lang_len = png_strlen(text_ptr[i].lang);\r
+         else\r
+            lang_len = 0;\r
+         if (text_ptr[i].lang_key != NULL)\r
+            lang_key_len = png_strlen(text_ptr[i].lang_key);\r
+         else\r
+            lang_key_len = 0;\r
+      }\r
+#else /* PNG_iTXt_SUPPORTED */\r
+      {\r
+         png_warning(png_ptr, "iTXt chunk not supported");\r
+         continue;\r
+      }\r
+#endif\r
+\r
+      if (text_ptr[i].text == NULL || text_ptr[i].text[0] == '\0')\r
+      {\r
+         text_length = 0;\r
+#ifdef PNG_iTXt_SUPPORTED\r
+         if (text_ptr[i].compression > 0)\r
+            textp->compression = PNG_ITXT_COMPRESSION_NONE;\r
+         else\r
+#endif\r
+            textp->compression = PNG_TEXT_COMPRESSION_NONE;\r
+      }\r
+\r
+      else\r
+      {\r
+         text_length = png_strlen(text_ptr[i].text);\r
+         textp->compression = text_ptr[i].compression;\r
+      }\r
+\r
+      textp->key = (png_charp)png_malloc_warn(png_ptr,\r
+         (png_size_t)\r
+         (key_len + text_length + lang_len + lang_key_len + 4));\r
+      if (textp->key == NULL)\r
+         return(1);\r
+      png_debug2(2, "Allocated %lu bytes at %x in png_set_text",\r
+                 (unsigned long)(png_uint_32)\r
+                 (key_len + lang_len + lang_key_len + text_length + 4),\r
+                 (int)textp->key);\r
+\r
+      png_memcpy(textp->key, text_ptr[i].key,(png_size_t)(key_len));\r
+      *(textp->key + key_len) = '\0';\r
+#ifdef PNG_iTXt_SUPPORTED\r
+      if (text_ptr[i].compression > 0)\r
+      {\r
+         textp->lang = textp->key + key_len + 1;\r
+         png_memcpy(textp->lang, text_ptr[i].lang, lang_len);\r
+         *(textp->lang + lang_len) = '\0';\r
+         textp->lang_key = textp->lang + lang_len + 1;\r
+         png_memcpy(textp->lang_key, text_ptr[i].lang_key, lang_key_len);\r
+         *(textp->lang_key + lang_key_len) = '\0';\r
+         textp->text = textp->lang_key + lang_key_len + 1;\r
+      }\r
+      else\r
+#endif\r
+      {\r
+#ifdef PNG_iTXt_SUPPORTED\r
+         textp->lang=NULL;\r
+         textp->lang_key=NULL;\r
+#endif\r
+         textp->text = textp->key + key_len + 1;\r
+      }\r
+      if (text_length)\r
+         png_memcpy(textp->text, text_ptr[i].text,\r
+            (png_size_t)(text_length));\r
+      *(textp->text + text_length) = '\0';\r
+\r
+#ifdef PNG_iTXt_SUPPORTED\r
+      if (textp->compression > 0)\r
+      {\r
+         textp->text_length = 0;\r
+         textp->itxt_length = text_length;\r
+      }\r
+      else\r
+#endif\r
+\r
+      {\r
+         textp->text_length = text_length;\r
+#ifdef PNG_iTXt_SUPPORTED\r
+         textp->itxt_length = 0;\r
+#endif\r
+      }\r
+      info_ptr->num_text++;\r
+      png_debug1(3, "transferred text chunk %d", info_ptr->num_text);\r
+   }\r
+   return(0);\r
+}\r
+#endif\r
+\r
+#ifdef PNG_tIME_SUPPORTED\r
+void PNGAPI\r
+png_set_tIME(png_structp png_ptr, png_infop info_ptr, png_timep mod_time)\r
+{\r
+   png_debug1(1, "in %s storage function", "tIME");\r
+\r
+   if (png_ptr == NULL || info_ptr == NULL ||\r
+       (png_ptr->mode & PNG_WROTE_tIME))\r
+      return;\r
+\r
+   png_memcpy(&(info_ptr->mod_time), mod_time, png_sizeof(png_time));\r
+   info_ptr->valid |= PNG_INFO_tIME;\r
+}\r
+#endif\r
+\r
+#ifdef PNG_tRNS_SUPPORTED\r
+void PNGAPI\r
+png_set_tRNS(png_structp png_ptr, png_infop info_ptr,\r
+   png_bytep trans_alpha, int num_trans, png_color_16p trans_color)\r
+{\r
+   png_debug1(1, "in %s storage function", "tRNS");\r
+\r
+   if (png_ptr == NULL || info_ptr == NULL)\r
+      return;\r
+\r
+   if (trans_alpha != NULL)\r
+   {\r
+       /* It may not actually be necessary to set png_ptr->trans_alpha here;\r
+        * we do it for backward compatibility with the way the png_handle_tRNS\r
+        * function used to do the allocation.\r
+        */\r
+\r
+       png_free_data(png_ptr, info_ptr, PNG_FREE_TRNS, 0);\r
+\r
+       /* Changed from num_trans to PNG_MAX_PALETTE_LENGTH in version 1.2.1 */\r
+       png_ptr->trans_alpha = info_ptr->trans_alpha = (png_bytep)png_malloc(png_ptr,\r
+           (png_size_t)PNG_MAX_PALETTE_LENGTH);\r
+       if (num_trans > 0 && num_trans <= PNG_MAX_PALETTE_LENGTH)\r
+          png_memcpy(info_ptr->trans_alpha, trans_alpha, (png_size_t)num_trans);\r
+   }\r
+\r
+   if (trans_color != NULL)\r
+   {\r
+      int sample_max = (1 << info_ptr->bit_depth);\r
+      if ((info_ptr->color_type == PNG_COLOR_TYPE_GRAY &&\r
+          (int)trans_color->gray > sample_max) ||\r
+          (info_ptr->color_type == PNG_COLOR_TYPE_RGB &&\r
+          ((int)trans_color->red > sample_max ||\r
+          (int)trans_color->green > sample_max ||\r
+          (int)trans_color->blue > sample_max)))\r
+         png_warning(png_ptr,\r
+            "tRNS chunk has out-of-range samples for bit_depth");\r
+      png_memcpy(&(info_ptr->trans_color), trans_color,\r
+         png_sizeof(png_color_16));\r
+      if (num_trans == 0)\r
+         num_trans = 1;\r
+   }\r
+\r
+   info_ptr->num_trans = (png_uint_16)num_trans;\r
+   if (num_trans != 0)\r
+   {\r
+      info_ptr->valid |= PNG_INFO_tRNS;\r
+      info_ptr->free_me |= PNG_FREE_TRNS;\r
+   }\r
+}\r
+#endif\r
+\r
+#ifdef PNG_sPLT_SUPPORTED\r
+void PNGAPI\r
+png_set_sPLT(png_structp png_ptr,\r
+             png_infop info_ptr, png_sPLT_tp entries, int nentries)\r
+/*\r
+ *  entries        - array of png_sPLT_t structures\r
+ *                   to be added to the list of palettes\r
+ *                   in the info structure.\r
+ *  nentries       - number of palette structures to be\r
+ *                   added.\r
+ */\r
+{\r
+   png_sPLT_tp np;\r
+   int i;\r
+\r
+   if (png_ptr == NULL || info_ptr == NULL)\r
+      return;\r
+\r
+   np = (png_sPLT_tp)png_malloc_warn(png_ptr,\r
+       (info_ptr->splt_palettes_num + nentries) *\r
+        (png_size_t)png_sizeof(png_sPLT_t));\r
+   if (np == NULL)\r
+   {\r
+      png_warning(png_ptr, "No memory for sPLT palettes");\r
+      return;\r
+   }\r
+\r
+   png_memcpy(np, info_ptr->splt_palettes,\r
+       info_ptr->splt_palettes_num * png_sizeof(png_sPLT_t));\r
+   png_free(png_ptr, info_ptr->splt_palettes);\r
+   info_ptr->splt_palettes=NULL;\r
+\r
+   for (i = 0; i < nentries; i++)\r
+   {\r
+      png_sPLT_tp to = np + info_ptr->splt_palettes_num + i;\r
+      png_sPLT_tp from = entries + i;\r
+      png_uint_32 length;\r
+\r
+      length = png_strlen(from->name) + 1;\r
+      to->name = (png_charp)png_malloc_warn(png_ptr, (png_size_t)length);\r
+      if (to->name == NULL)\r
+      {\r
+         png_warning(png_ptr,\r
+           "Out of memory while processing sPLT chunk");\r
+         continue;\r
+      }\r
+      png_memcpy(to->name, from->name, length);\r
+      to->entries = (png_sPLT_entryp)png_malloc_warn(png_ptr,\r
+          (png_size_t)(from->nentries * png_sizeof(png_sPLT_entry)));\r
+      if (to->entries == NULL)\r
+      {\r
+         png_warning(png_ptr,\r
+           "Out of memory while processing sPLT chunk");\r
+         png_free(png_ptr, to->name);\r
+         to->name = NULL;\r
+         continue;\r
+      }\r
+      png_memcpy(to->entries, from->entries,\r
+          from->nentries * png_sizeof(png_sPLT_entry));\r
+      to->nentries = from->nentries;\r
+      to->depth = from->depth;\r
+   }\r
+\r
+   info_ptr->splt_palettes = np;\r
+   info_ptr->splt_palettes_num += nentries;\r
+   info_ptr->valid |= PNG_INFO_sPLT;\r
+   info_ptr->free_me |= PNG_FREE_SPLT;\r
+}\r
+#endif /* PNG_sPLT_SUPPORTED */\r
+\r
+#ifdef PNG_UNKNOWN_CHUNKS_SUPPORTED\r
+void PNGAPI\r
+png_set_unknown_chunks(png_structp png_ptr,\r
+   png_infop info_ptr, png_unknown_chunkp unknowns, int num_unknowns)\r
+{\r
+   png_unknown_chunkp np;\r
+   int i;\r
+\r
+   if (png_ptr == NULL || info_ptr == NULL || num_unknowns == 0)\r
+      return;\r
+\r
+   np = (png_unknown_chunkp)png_malloc_warn(png_ptr,\r
+       (png_size_t)((info_ptr->unknown_chunks_num + num_unknowns) *\r
+       png_sizeof(png_unknown_chunk)));\r
+   if (np == NULL)\r
+   {\r
+      png_warning(png_ptr,\r
+          "Out of memory while processing unknown chunk");\r
+      return;\r
+   }\r
+\r
+   png_memcpy(np, info_ptr->unknown_chunks,\r
+       info_ptr->unknown_chunks_num * png_sizeof(png_unknown_chunk));\r
+   png_free(png_ptr, info_ptr->unknown_chunks);\r
+   info_ptr->unknown_chunks = NULL;\r
+\r
+   for (i = 0; i < num_unknowns; i++)\r
+   {\r
+      png_unknown_chunkp to = np + info_ptr->unknown_chunks_num + i;\r
+      png_unknown_chunkp from = unknowns + i;\r
+\r
+      png_memcpy((png_charp)to->name, (png_charp)from->name,\r
+          png_sizeof(from->name));\r
+      to->name[png_sizeof(to->name)-1] = '\0';\r
+      to->size = from->size;\r
+      /* Note our location in the read or write sequence */\r
+      to->location = (png_byte)(png_ptr->mode & 0xff);\r
+\r
+      if (from->size == 0)\r
+         to->data=NULL;\r
+      else\r
+      {\r
+         to->data = (png_bytep)png_malloc_warn(png_ptr,\r
+           (png_size_t)from->size);\r
+         if (to->data == NULL)\r
+         {\r
+            png_warning(png_ptr,\r
+             "Out of memory while processing unknown chunk");\r
+            to->size = 0;\r
+         }\r
+         else\r
+            png_memcpy(to->data, from->data, from->size);\r
+      }\r
+   }\r
+\r
+   info_ptr->unknown_chunks = np;\r
+   info_ptr->unknown_chunks_num += num_unknowns;\r
+   info_ptr->free_me |= PNG_FREE_UNKN;\r
+}\r
+void PNGAPI\r
+png_set_unknown_chunk_location(png_structp png_ptr, png_infop info_ptr,\r
+   int chunk, int location)\r
+{\r
+   if (png_ptr != NULL && info_ptr != NULL && chunk >= 0 && chunk <\r
+       (int)info_ptr->unknown_chunks_num)\r
+      info_ptr->unknown_chunks[chunk].location = (png_byte)location;\r
+}\r
+#endif\r
+\r
+\r
+#ifdef PNG_MNG_FEATURES_SUPPORTED\r
+png_uint_32 PNGAPI\r
+png_permit_mng_features (png_structp png_ptr, png_uint_32 mng_features)\r
+{\r
+   png_debug(1, "in png_permit_mng_features");\r
+\r
+   if (png_ptr == NULL)\r
+      return (png_uint_32)0;\r
+   png_ptr->mng_features_permitted =\r
+     (png_byte)(mng_features & PNG_ALL_MNG_FEATURES);\r
+   return (png_uint_32)png_ptr->mng_features_permitted;\r
+}\r
+#endif\r
+\r
+#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED\r
+void PNGAPI\r
+png_set_keep_unknown_chunks(png_structp png_ptr, int keep, png_bytep\r
+   chunk_list, int num_chunks)\r
+{\r
+   png_bytep new_list, p;\r
+   int i, old_num_chunks;\r
+   if (png_ptr == NULL)\r
+      return;\r
+   if (num_chunks == 0)\r
+   {\r
+      if (keep == PNG_HANDLE_CHUNK_ALWAYS || keep == PNG_HANDLE_CHUNK_IF_SAFE)\r
+         png_ptr->flags |= PNG_FLAG_KEEP_UNKNOWN_CHUNKS;\r
+      else\r
+         png_ptr->flags &= ~PNG_FLAG_KEEP_UNKNOWN_CHUNKS;\r
+\r
+      if (keep == PNG_HANDLE_CHUNK_ALWAYS)\r
+         png_ptr->flags |= PNG_FLAG_KEEP_UNSAFE_CHUNKS;\r
+      else\r
+         png_ptr->flags &= ~PNG_FLAG_KEEP_UNSAFE_CHUNKS;\r
+      return;\r
+   }\r
+   if (chunk_list == NULL)\r
+      return;\r
+   old_num_chunks = png_ptr->num_chunk_list;\r
+   new_list=(png_bytep)png_malloc(png_ptr,\r
+      (png_size_t)\r
+       (5*(num_chunks + old_num_chunks)));\r
+   if (png_ptr->chunk_list != NULL)\r
+   {\r
+      png_memcpy(new_list, png_ptr->chunk_list,\r
+          (png_size_t)(5*old_num_chunks));\r
+      png_free(png_ptr, png_ptr->chunk_list);\r
+      png_ptr->chunk_list=NULL;\r
+   }\r
+   png_memcpy(new_list + 5*old_num_chunks, chunk_list,\r
+       (png_size_t)(5*num_chunks));\r
+   for (p = new_list + 5*old_num_chunks + 4, i = 0; i<num_chunks; i++, p += 5)\r
+      *p=(png_byte)keep;\r
+   png_ptr->num_chunk_list = old_num_chunks + num_chunks;\r
+   png_ptr->chunk_list = new_list;\r
+   png_ptr->free_me |= PNG_FREE_LIST;\r
+}\r
+#endif\r
+\r
+#ifdef PNG_READ_USER_CHUNKS_SUPPORTED\r
+void PNGAPI\r
+png_set_read_user_chunk_fn(png_structp png_ptr, png_voidp user_chunk_ptr,\r
+   png_user_chunk_ptr read_user_chunk_fn)\r
+{\r
+   png_debug(1, "in png_set_read_user_chunk_fn");\r
+\r
+   if (png_ptr == NULL)\r
+      return;\r
+\r
+   png_ptr->read_user_chunk_fn = read_user_chunk_fn;\r
+   png_ptr->user_chunk_ptr = user_chunk_ptr;\r
+}\r
+#endif\r
+\r
+#ifdef PNG_INFO_IMAGE_SUPPORTED\r
+void PNGAPI\r
+png_set_rows(png_structp png_ptr, png_infop info_ptr, png_bytepp row_pointers)\r
+{\r
+   png_debug1(1, "in %s storage function", "rows");\r
+\r
+   if (png_ptr == NULL || info_ptr == NULL)\r
+      return;\r
+\r
+   if (info_ptr->row_pointers && (info_ptr->row_pointers != row_pointers))\r
+      png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0);\r
+   info_ptr->row_pointers = row_pointers;\r
+   if (row_pointers)\r
+      info_ptr->valid |= PNG_INFO_IDAT;\r
+}\r
+#endif\r
+\r
+void PNGAPI\r
+png_set_compression_buffer_size(png_structp png_ptr,\r
+    png_size_t size)\r
+{\r
+    if (png_ptr == NULL)\r
+       return;\r
+    png_free(png_ptr, png_ptr->zbuf);\r
+    png_ptr->zbuf_size = size;\r
+    png_ptr->zbuf = (png_bytep)png_malloc(png_ptr, size);\r
+    png_ptr->zstream.next_out = png_ptr->zbuf;\r
+    png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;\r
+}\r
+\r
+void PNGAPI\r
+png_set_invalid(png_structp png_ptr, png_infop info_ptr, int mask)\r
+{\r
+   if (png_ptr && info_ptr)\r
+      info_ptr->valid &= ~mask;\r
+}\r
+\r
+\r
+\r
+#ifdef PNG_SET_USER_LIMITS_SUPPORTED\r
+/* This function was added to libpng 1.2.6 */\r
+void PNGAPI\r
+png_set_user_limits (png_structp png_ptr, png_uint_32 user_width_max,\r
+    png_uint_32 user_height_max)\r
+{\r
+   /* Images with dimensions larger than these limits will be\r
+    * rejected by png_set_IHDR().  To accept any PNG datastream\r
+    * regardless of dimensions, set both limits to 0x7ffffffL.\r
+    */\r
+   if (png_ptr == NULL)\r
+      return;\r
+   png_ptr->user_width_max = user_width_max;\r
+   png_ptr->user_height_max = user_height_max;\r
+}\r
+\r
+/* This function was added to libpng 1.4.0 */\r
+void PNGAPI\r
+png_set_chunk_cache_max (png_structp png_ptr,\r
+   png_uint_32 user_chunk_cache_max)\r
+{\r
+    if (png_ptr)\r
+       png_ptr->user_chunk_cache_max = user_chunk_cache_max;\r
+}\r
+\r
+/* This function was added to libpng 1.4.1 */\r
+void PNGAPI\r
+png_set_chunk_malloc_max (png_structp png_ptr,\r
+   png_alloc_size_t user_chunk_malloc_max)\r
+{\r
+    if (png_ptr)\r
+       png_ptr->user_chunk_malloc_max =\r
+          (png_size_t)user_chunk_malloc_max;\r
+}\r
+#endif /* ?PNG_SET_USER_LIMITS_SUPPORTED */\r
+\r
+\r
+#ifdef PNG_BENIGN_ERRORS_SUPPORTED\r
+void PNGAPI\r
+png_set_benign_errors(png_structp png_ptr, int allowed)\r
+{\r
+   png_debug(1, "in png_set_benign_errors");\r
+\r
+   if (allowed)\r
+      png_ptr->flags |= PNG_FLAG_BENIGN_ERRORS_WARN;\r
+   else\r
+      png_ptr->flags &= ~PNG_FLAG_BENIGN_ERRORS_WARN;\r
+}\r
+#endif /* PNG_BENIGN_ERRORS_SUPPORTED */\r
+#endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */\r
index 60980c2..35a4c7d 100644 (file)
-
-/* pngtest.c - a simple test program to test libpng
- *
- * Last changed in libpng 1.2.27 - [April 29, 2008]
- * For conditions of distribution and use, see copyright notice in png.h
- * Copyright (c) 1998-2008 Glenn Randers-Pehrson
- * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
- * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
- *
- * This program reads in a PNG image, writes it out again, and then
- * compares the two files.  If the files are identical, this shows that
- * the basic chunk handling, filtering, and (de)compression code is working
- * properly.  It does not currently test all of the transforms, although
- * it probably should.
- *
- * The program will report "FAIL" in certain legitimate cases:
- * 1) when the compression level or filter selection method is changed.
- * 2) when the maximum IDAT size (PNG_ZBUF_SIZE in pngconf.h) is not 8192.
- * 3) unknown unsafe-to-copy ancillary chunks or unknown critical chunks
- *    exist in the input file.
- * 4) others not listed here...
- * In these cases, it is best to check with another tool such as "pngcheck"
- * to see what the differences between the two files are.
- *
- * If a filename is given on the command-line, then this file is used
- * for the input, rather than the default "pngtest.png".  This allows
- * testing a wide variety of files easily.  You can also test a number
- * of files at once by typing "pngtest -m file1.png file2.png ..."
- */
-
-#include "png.h"
-
-#if defined(_WIN32_WCE)
-#  if _WIN32_WCE < 211
-     __error__ (f|w)printf functions are not supported on old WindowsCE.;
-#  endif
-#  include <windows.h>
-#  include <stdlib.h>
-#  define READFILE(file, data, length, check) \
-     if (ReadFile(file, data, length, &check,NULL)) check = 0
-#  define WRITEFILE(file, data, length, check)) \
-     if (WriteFile(file, data, length, &check, NULL)) check = 0
-#  define FCLOSE(file) CloseHandle(file)
-#else
-#  include <stdio.h>
-#  include <stdlib.h>
-#  define READFILE(file, data, length, check) \
-     check=(png_size_t)fread(data,(png_size_t)1,length,file)
-#  define WRITEFILE(file, data, length, check) \
-     check=(png_size_t)fwrite(data,(png_size_t)1, length, file)
-#  define FCLOSE(file) fclose(file)
-#endif
-
-#if defined(PNG_NO_STDIO)
-#  if defined(_WIN32_WCE)
-     typedef HANDLE                png_FILE_p;
-#  else
-     typedef FILE                * png_FILE_p;
-#  endif
-#endif
-
-/* Makes pngtest verbose so we can find problems (needs to be before png.h) */
-#ifndef PNG_DEBUG
-#  define PNG_DEBUG 0
-#endif
-
-#if !PNG_DEBUG
-#  define SINGLE_ROWBUF_ALLOC  /* makes buffer overruns easier to nail */
-#endif
-
-/* Turn on CPU timing
-#define PNGTEST_TIMING
-*/
-
-#ifdef PNG_NO_FLOATING_POINT_SUPPORTED
-#undef PNGTEST_TIMING
-#endif
-
-#ifdef PNGTEST_TIMING
-static float t_start, t_stop, t_decode, t_encode, t_misc;
-#include <time.h>
-#endif
-
-#if defined(PNG_TIME_RFC1123_SUPPORTED)
-#define PNG_tIME_STRING_LENGTH 30
-static int tIME_chunk_present=0;
-static char tIME_string[PNG_tIME_STRING_LENGTH] = "no tIME chunk present in file";
-#endif
-
-static int verbose = 0;
-
-int test_one_file PNGARG((PNG_CONST char *inname, PNG_CONST char *outname));
-
-#ifdef __TURBOC__
-#include <mem.h>
-#endif
-
-/* defined so I can write to a file on gui/windowing platforms */
-/*  #define STDERR stderr  */
-#define STDERR stdout   /* for DOS */
-
-/* example of using row callbacks to make a simple progress meter */
-static int status_pass=1;
-static int status_dots_requested=0;
-static int status_dots=1;
-
-/* In case a system header (e.g., on AIX) defined jmpbuf */
-#ifdef jmpbuf
-#  undef jmpbuf
-#endif
-
-/* Define png_jmpbuf() in case we are using a pre-1.0.6 version of libpng */
-#ifndef png_jmpbuf
-#  define png_jmpbuf(png_ptr) png_ptr->jmpbuf
-#endif
-
-void
-#ifdef PNG_1_0_X
-PNGAPI
-#endif
-read_row_callback(png_structp png_ptr, png_uint_32 row_number, int pass);
-void
-#ifdef PNG_1_0_X
-PNGAPI
-#endif
-read_row_callback(png_structp png_ptr, png_uint_32 row_number, int pass)
-{
-    if(png_ptr == NULL || row_number > PNG_UINT_31_MAX) return;
-    if(status_pass != pass)
-    {
-       fprintf(stdout,"\n Pass %d: ",pass);
-       status_pass = pass;
-       status_dots = 31;
-    }
-    status_dots--;
-    if(status_dots == 0)
-    {
-       fprintf(stdout, "\n         ");
-       status_dots=30;
-    }
-    fprintf(stdout, "r");
-}
-
-void
-#ifdef PNG_1_0_X
-PNGAPI
-#endif
-write_row_callback(png_structp png_ptr, png_uint_32 row_number, int pass);
-void
-#ifdef PNG_1_0_X
-PNGAPI
-#endif
-write_row_callback(png_structp png_ptr, png_uint_32 row_number, int pass)
-{
-    if(png_ptr == NULL || row_number > PNG_UINT_31_MAX || pass > 7) return;
-    fprintf(stdout, "w");
-}
-
-
-#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
-/* Example of using user transform callback (we don't transform anything,
-   but merely examine the row filters.  We set this to 256 rather than
-   5 in case illegal filter values are present.) */
-static png_uint_32 filters_used[256];
-void
-#ifdef PNG_1_0_X
-PNGAPI
-#endif
-count_filters(png_structp png_ptr, png_row_infop row_info, png_bytep data);
-void
-#ifdef PNG_1_0_X
-PNGAPI
-#endif
-count_filters(png_structp png_ptr, png_row_infop row_info, png_bytep data)
-{
-    if(png_ptr != NULL && row_info != NULL)
-      ++filters_used[*(data-1)];
-}
-#endif
-
-#if defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
-/* example of using user transform callback (we don't transform anything,
-   but merely count the zero samples) */
-
-static png_uint_32 zero_samples;
-
-void
-#ifdef PNG_1_0_X
-PNGAPI
-#endif
-count_zero_samples(png_structp png_ptr, png_row_infop row_info, png_bytep data);
-void
-#ifdef PNG_1_0_X
-PNGAPI
-#endif
-count_zero_samples(png_structp png_ptr, png_row_infop row_info, png_bytep data)
-{
-   png_bytep dp = data;
-   if(png_ptr == NULL)return;
-
-   /* contents of row_info:
-    *  png_uint_32 width      width of row
-    *  png_uint_32 rowbytes   number of bytes in row
-    *  png_byte color_type    color type of pixels
-    *  png_byte bit_depth     bit depth of samples
-    *  png_byte channels      number of channels (1-4)
-    *  png_byte pixel_depth   bits per pixel (depth*channels)
-    */
-
-
-    /* counts the number of zero samples (or zero pixels if color_type is 3 */
-
-    if(row_info->color_type == 0 || row_info->color_type == 3)
-    {
-       int pos=0;
-       png_uint_32 n, nstop;
-       for (n=0, nstop=row_info->width; n<nstop; n++)
-       {
-          if(row_info->bit_depth == 1)
-          {
-             if(((*dp << pos++ ) & 0x80) == 0) zero_samples++;
-             if(pos == 8)
-             {
-                pos = 0;
-                dp++;
-             }
-          }
-          if(row_info->bit_depth == 2)
-          {
-             if(((*dp << (pos+=2)) & 0xc0) == 0) zero_samples++;
-             if(pos == 8)
-             {
-                pos = 0;
-                dp++;
-             }
-          }
-          if(row_info->bit_depth == 4)
-          {
-             if(((*dp << (pos+=4)) & 0xf0) == 0) zero_samples++;
-             if(pos == 8)
-             {
-                pos = 0;
-                dp++;
-             }
-          }
-          if(row_info->bit_depth == 8)
-             if(*dp++ == 0) zero_samples++;
-          if(row_info->bit_depth == 16)
-          {
-             if((*dp | *(dp+1)) == 0) zero_samples++;
-             dp+=2;
-          }
-       }
-    }
-    else /* other color types */
-    {
-       png_uint_32 n, nstop;
-       int channel;
-       int color_channels = row_info->channels;
-       if(row_info->color_type > 3)color_channels--;
-
-       for (n=0, nstop=row_info->width; n<nstop; n++)
-       {
-          for (channel = 0; channel < color_channels; channel++)
-          {
-             if(row_info->bit_depth == 8)
-                if(*dp++ == 0) zero_samples++;
-             if(row_info->bit_depth == 16)
-             {
-                if((*dp | *(dp+1)) == 0) zero_samples++;
-                dp+=2;
-             }
-          }
-          if(row_info->color_type > 3)
-          {
-             dp++;
-             if(row_info->bit_depth == 16)dp++;
-          }
-       }
-    }
-}
-#endif /* PNG_WRITE_USER_TRANSFORM_SUPPORTED */
-
-static int wrote_question = 0;
-
-#if defined(PNG_NO_STDIO)
-/* START of code to validate stdio-free compilation */
-/* These copies of the default read/write functions come from pngrio.c and */
-/* pngwio.c.  They allow "don't include stdio" testing of the library. */
-/* This is the function that does the actual reading of data.  If you are
-   not reading from a standard C stream, you should create a replacement
-   read_data function and use it at run time with png_set_read_fn(), rather
-   than changing the library. */
-
-#ifndef USE_FAR_KEYWORD
-static void
-pngtest_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
-{
-   png_size_t check;
-
-   /* fread() returns 0 on error, so it is OK to store this in a png_size_t
-    * instead of an int, which is what fread() actually returns.
-    */
-   READFILE((png_FILE_p)png_ptr->io_ptr, data, length, check);
-
-   if (check != length)
-   {
-      png_error(png_ptr, "Read Error!");
-   }
-}
-#else
-/* this is the model-independent version. Since the standard I/O library
-   can't handle far buffers in the medium and small models, we have to copy
-   the data.
-*/
-
-#define NEAR_BUF_SIZE 1024
-#define MIN(a,b) (a <= b ? a : b)
-
-static void
-pngtest_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
-{
-   int check;
-   png_byte *n_data;
-   png_FILE_p io_ptr;
-
-   /* Check if data really is near. If so, use usual code. */
-   n_data = (png_byte *)CVT_PTR_NOCHECK(data);
-   io_ptr = (png_FILE_p)CVT_PTR(png_ptr->io_ptr);
-   if ((png_bytep)n_data == data)
-   {
-      READFILE(io_ptr, n_data, length, check);
-   }
-   else
-   {
-      png_byte buf[NEAR_BUF_SIZE];
-      png_size_t read, remaining, err;
-      check = 0;
-      remaining = length;
-      do
-      {
-         read = MIN(NEAR_BUF_SIZE, remaining);
-         READFILE(io_ptr, buf, 1, err);
-         png_memcpy(data, buf, read); /* copy far buffer to near buffer */
-         if(err != read)
-            break;
-         else
-            check += err;
-         data += read;
-         remaining -= read;
-      }
-      while (remaining != 0);
-   }
-   if (check != length)
-   {
-      png_error(png_ptr, "read Error");
-   }
-}
-#endif /* USE_FAR_KEYWORD */
-
-#if defined(PNG_WRITE_FLUSH_SUPPORTED)
-static void
-pngtest_flush(png_structp png_ptr)
-{
-#if !defined(_WIN32_WCE)
-   png_FILE_p io_ptr;
-   io_ptr = (png_FILE_p)CVT_PTR((png_ptr->io_ptr));
-   if (io_ptr != NULL)
-      fflush(io_ptr);
-#endif
-}
-#endif
-
-/* This is the function that does the actual writing of data.  If you are
-   not writing to a standard C stream, you should create a replacement
-   write_data function and use it at run time with png_set_write_fn(), rather
-   than changing the library. */
-#ifndef USE_FAR_KEYWORD
-static void
-pngtest_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
-{
-   png_uint_32 check;
-
-   WRITEFILE((png_FILE_p)png_ptr->io_ptr,  data, length, check);
-   if (check != length)
-   {
-      png_error(png_ptr, "Write Error");
-   }
-}
-#else
-/* this is the model-independent version. Since the standard I/O library
-   can't handle far buffers in the medium and small models, we have to copy
-   the data.
-*/
-
-#define NEAR_BUF_SIZE 1024
-#define MIN(a,b) (a <= b ? a : b)
-
-static void
-pngtest_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
-{
-   png_uint_32 check;
-   png_byte *near_data;  /* Needs to be "png_byte *" instead of "png_bytep" */
-   png_FILE_p io_ptr;
-
-   /* Check if data really is near. If so, use usual code. */
-   near_data = (png_byte *)CVT_PTR_NOCHECK(data);
-   io_ptr = (png_FILE_p)CVT_PTR(png_ptr->io_ptr);
-   if ((png_bytep)near_data == data)
-   {
-      WRITEFILE(io_ptr, near_data, length, check);
-   }
-   else
-   {
-      png_byte buf[NEAR_BUF_SIZE];
-      png_size_t written, remaining, err;
-      check = 0;
-      remaining = length;
-      do
-      {
-         written = MIN(NEAR_BUF_SIZE, remaining);
-         png_memcpy(buf, data, written); /* copy far buffer to near buffer */
-         WRITEFILE(io_ptr, buf, written, err);
-         if (err != written)
-            break;
-         else
-            check += err;
-         data += written;
-         remaining -= written;
-      }
-      while (remaining != 0);
-   }
-   if (check != length)
-   {
-      png_error(png_ptr, "Write Error");
-   }
-}
-#endif /* USE_FAR_KEYWORD */
-#endif /* PNG_NO_STDIO */
-/* END of code to validate stdio-free compilation */
-
-/* This function is called when there is a warning, but the library thinks
- * it can continue anyway.  Replacement functions don't have to do anything
- * here if you don't want to.  In the default configuration, png_ptr is
- * not used, but it is passed in case it may be useful.
- */
-static void
-pngtest_warning(png_structp png_ptr, png_const_charp message)
-{
-   PNG_CONST char *name = "UNKNOWN (ERROR!)";
-   if (png_ptr != NULL && png_ptr->error_ptr != NULL)
-      name = png_ptr->error_ptr;
-   fprintf(STDERR, "%s: libpng warning: %s\n", name, message);
-}
-
-/* This is the default error handling function.  Note that replacements for
- * this function MUST NOT RETURN, or the program will likely crash.  This
- * function is used by default, or if the program supplies NULL for the
- * error function pointer in png_set_error_fn().
- */
-static void
-pngtest_error(png_structp png_ptr, png_const_charp message)
-{
-   pngtest_warning(png_ptr, message);
-   /* We can return because png_error calls the default handler, which is
-    * actually OK in this case. */
-}
-
-/* START of code to validate memory allocation and deallocation */
-#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
-
-/* Allocate memory.  For reasonable files, size should never exceed
-   64K.  However, zlib may allocate more then 64K if you don't tell
-   it not to.  See zconf.h and png.h for more information.  zlib does
-   need to allocate exactly 64K, so whatever you call here must
-   have the ability to do that.
-
-   This piece of code can be compiled to validate max 64K allocations
-   by setting MAXSEG_64K in zlib zconf.h *or* PNG_MAX_MALLOC_64K. */
-typedef struct memory_information
-{
-   png_uint_32               size;
-   png_voidp                 pointer;
-   struct memory_information FAR *next;
-} memory_information;
-typedef memory_information FAR *memory_infop;
-
-static memory_infop pinformation = NULL;
-static int current_allocation = 0;
-static int maximum_allocation = 0;
-static int total_allocation = 0;
-static int num_allocations = 0;
-
-png_voidp png_debug_malloc PNGARG((png_structp png_ptr, png_uint_32 size));
-void png_debug_free PNGARG((png_structp png_ptr, png_voidp ptr));
-
-png_voidp
-png_debug_malloc(png_structp png_ptr, png_uint_32 size)
-{
-
-   /* png_malloc has already tested for NULL; png_create_struct calls
-      png_debug_malloc directly, with png_ptr == NULL which is OK */
-
-   if (size == 0)
-      return (NULL);
-
-   /* This calls the library allocator twice, once to get the requested
-      buffer and once to get a new free list entry. */
-   {
-      /* Disable malloc_fn and free_fn */
-      memory_infop pinfo;
-      png_set_mem_fn(png_ptr, NULL, NULL, NULL);
-      pinfo = (memory_infop)png_malloc(png_ptr,
-         (png_uint_32)png_sizeof (*pinfo));
-      pinfo->size = size;
-      current_allocation += size;
-      total_allocation += size;
-      num_allocations ++;
-      if (current_allocation > maximum_allocation)
-         maximum_allocation = current_allocation;
-      pinfo->pointer = (png_voidp)png_malloc(png_ptr, size);
-      /* Restore malloc_fn and free_fn */
-      png_set_mem_fn(png_ptr, png_voidp_NULL, (png_malloc_ptr)png_debug_malloc,
-         (png_free_ptr)png_debug_free);
-      if (size != 0 && pinfo->pointer == NULL)
-      {
-         current_allocation -= size;
-         total_allocation -= size;
-         png_error(png_ptr,
-           "out of memory in pngtest->png_debug_malloc.");
-      }
-      pinfo->next = pinformation;
-      pinformation = pinfo;
-      /* Make sure the caller isn't assuming zeroed memory. */
-      png_memset(pinfo->pointer, 0xdd, pinfo->size);
-      if(verbose)
-         printf("png_malloc %lu bytes at %x\n",(unsigned long)size,
-          pinfo->pointer);
-      return (png_voidp)(pinfo->pointer);
-   }
-}
-
-/* Free a pointer.  It is removed from the list at the same time. */
-void
-png_debug_free(png_structp png_ptr, png_voidp ptr)
-{
-   if (png_ptr == NULL)
-      fprintf(STDERR, "NULL pointer to png_debug_free.\n");
-   if (ptr == 0)
-   {
-#if 0 /* This happens all the time. */
-      fprintf(STDERR, "WARNING: freeing NULL pointer\n");
-#endif
-      return;
-   }
-
-   /* Unlink the element from the list. */
-   {
-      memory_infop FAR *ppinfo = &pinformation;
-      for (;;)
-      {
-         memory_infop pinfo = *ppinfo;
-         if (pinfo->pointer == ptr)
-         {
-            *ppinfo = pinfo->next;
-            current_allocation -= pinfo->size;
-            if (current_allocation < 0)
-               fprintf(STDERR, "Duplicate free of memory\n");
-            /* We must free the list element too, but first kill
-               the memory that is to be freed. */
-            png_memset(ptr, 0x55, pinfo->size);
-            png_free_default(png_ptr, pinfo);
-            pinfo=NULL;
-            break;
-         }
-         if (pinfo->next == NULL)
-         {
-            fprintf(STDERR, "Pointer %x not found\n", (unsigned int)ptr);
-            break;
-         }
-         ppinfo = &pinfo->next;
-      }
-   }
-
-   /* Finally free the data. */
-   if(verbose)
-      printf("Freeing %x\n",ptr);
-   png_free_default(png_ptr, ptr);
-   ptr=NULL;
-}
-#endif /* PNG_USER_MEM_SUPPORTED && PNG_DEBUG */
-/* END of code to test memory allocation/deallocation */
-
-/* Test one file */
-int
-test_one_file(PNG_CONST char *inname, PNG_CONST char *outname)
-{
-   static png_FILE_p fpin;
-   static png_FILE_p fpout;  /* "static" prevents setjmp corruption */
-   png_structp read_ptr;
-   png_infop read_info_ptr, end_info_ptr;
-#ifdef PNG_WRITE_SUPPORTED
-   png_structp write_ptr;
-   png_infop write_info_ptr;
-   png_infop write_end_info_ptr;
-#else
-   png_structp write_ptr = NULL;
-   png_infop write_info_ptr = NULL;
-   png_infop write_end_info_ptr = NULL;
-#endif
-   png_bytep row_buf;
-   png_uint_32 y;
-   png_uint_32 width, height;
-   int num_pass, pass;
-   int bit_depth, color_type;
-#ifdef PNG_SETJMP_SUPPORTED
-#ifdef USE_FAR_KEYWORD
-   jmp_buf jmpbuf;
-#endif
-#endif
-
-#if defined(_WIN32_WCE)
-   TCHAR path[MAX_PATH];
-#endif
-   char inbuf[256], outbuf[256];
-
-   row_buf = NULL;
-
-#if defined(_WIN32_WCE)
-   MultiByteToWideChar(CP_ACP, 0, inname, -1, path, MAX_PATH);
-   if ((fpin = CreateFile(path, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL)) == INVALID_HANDLE_VALUE)
-#else
-   if ((fpin = fopen(inname, "rb")) == NULL)
-#endif
-   {
-      fprintf(STDERR, "Could not find input file %s\n", inname);
-      return (1);
-   }
-
-#if defined(_WIN32_WCE)
-   MultiByteToWideChar(CP_ACP, 0, outname, -1, path, MAX_PATH);
-   if ((fpout = CreateFile(path, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL)) == INVALID_HANDLE_VALUE)
-#else
-   if ((fpout = fopen(outname, "wb")) == NULL)
-#endif
-   {
-      fprintf(STDERR, "Could not open output file %s\n", outname);
-      FCLOSE(fpin);
-      return (1);
-   }
-
-   png_debug(0, "Allocating read and write structures\n");
-#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
-   read_ptr = png_create_read_struct_2(PNG_LIBPNG_VER_STRING, png_voidp_NULL,
-      png_error_ptr_NULL, png_error_ptr_NULL, png_voidp_NULL,
-      (png_malloc_ptr)png_debug_malloc, (png_free_ptr)png_debug_free);
-#else
-   read_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, png_voidp_NULL,
-      png_error_ptr_NULL, png_error_ptr_NULL);
-#endif
-   png_set_error_fn(read_ptr, (png_voidp)inname, pngtest_error,
-       pngtest_warning);
-#ifdef PNG_WRITE_SUPPORTED
-#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
-   write_ptr = png_create_write_struct_2(PNG_LIBPNG_VER_STRING, png_voidp_NULL,
-      png_error_ptr_NULL, png_error_ptr_NULL, png_voidp_NULL,
-      (png_malloc_ptr)png_debug_malloc, (png_free_ptr)png_debug_free);
-#else
-   write_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, png_voidp_NULL,
-      png_error_ptr_NULL, png_error_ptr_NULL);
-#endif
-   png_set_error_fn(write_ptr, (png_voidp)inname, pngtest_error,
-       pngtest_warning);
-#endif
-   png_debug(0, "Allocating read_info, write_info and end_info structures\n");
-   read_info_ptr = png_create_info_struct(read_ptr);
-   end_info_ptr = png_create_info_struct(read_ptr);
-#ifdef PNG_WRITE_SUPPORTED
-   write_info_ptr = png_create_info_struct(write_ptr);
-   write_end_info_ptr = png_create_info_struct(write_ptr);
-#endif
-
-#ifdef PNG_SETJMP_SUPPORTED
-   png_debug(0, "Setting jmpbuf for read struct\n");
-#ifdef USE_FAR_KEYWORD
-   if (setjmp(jmpbuf))
-#else
-   if (setjmp(png_jmpbuf(read_ptr)))
-#endif
-   {
-      fprintf(STDERR, "%s -> %s: libpng read error\n", inname, outname);
-      png_free(read_ptr, row_buf);
-      png_destroy_read_struct(&read_ptr, &read_info_ptr, &end_info_ptr);
-#ifdef PNG_WRITE_SUPPORTED
-      png_destroy_info_struct(write_ptr, &write_end_info_ptr);
-      png_destroy_write_struct(&write_ptr, &write_info_ptr);
-#endif
-      FCLOSE(fpin);
-      FCLOSE(fpout);
-      return (1);
-   }
-#ifdef USE_FAR_KEYWORD
-   png_memcpy(png_jmpbuf(read_ptr),jmpbuf,png_sizeof(jmp_buf));
-#endif
-
-#ifdef PNG_WRITE_SUPPORTED
-   png_debug(0, "Setting jmpbuf for write struct\n");
-#ifdef USE_FAR_KEYWORD
-   if (setjmp(jmpbuf))
-#else
-   if (setjmp(png_jmpbuf(write_ptr)))
-#endif
-   {
-      fprintf(STDERR, "%s -> %s: libpng write error\n", inname, outname);
-      png_destroy_read_struct(&read_ptr, &read_info_ptr, &end_info_ptr);
-      png_destroy_info_struct(write_ptr, &write_end_info_ptr);
-#ifdef PNG_WRITE_SUPPORTED
-      png_destroy_write_struct(&write_ptr, &write_info_ptr);
-#endif
-      FCLOSE(fpin);
-      FCLOSE(fpout);
-      return (1);
-   }
-#ifdef USE_FAR_KEYWORD
-   png_memcpy(png_jmpbuf(write_ptr),jmpbuf,png_sizeof(jmp_buf));
-#endif
-#endif
-#endif
-
-   png_debug(0, "Initializing input and output streams\n");
-#if !defined(PNG_NO_STDIO)
-   png_init_io(read_ptr, fpin);
-#  ifdef PNG_WRITE_SUPPORTED
-   png_init_io(write_ptr, fpout);
-#  endif
-#else
-   png_set_read_fn(read_ptr, (png_voidp)fpin, pngtest_read_data);
-#  ifdef PNG_WRITE_SUPPORTED
-   png_set_write_fn(write_ptr, (png_voidp)fpout,  pngtest_write_data,
-#    if defined(PNG_WRITE_FLUSH_SUPPORTED)
-      pngtest_flush);
-#    else
-      NULL);
-#    endif
-#  endif
-#endif
-   if(status_dots_requested == 1)
-   {
-#ifdef PNG_WRITE_SUPPORTED
-      png_set_write_status_fn(write_ptr, write_row_callback);
-#endif
-      png_set_read_status_fn(read_ptr, read_row_callback);
-   }
-   else
-   {
-#ifdef PNG_WRITE_SUPPORTED
-      png_set_write_status_fn(write_ptr, png_write_status_ptr_NULL);
-#endif
-      png_set_read_status_fn(read_ptr, png_read_status_ptr_NULL);
-   }
-
-#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
-   {
-     int i;
-     for(i=0; i<256; i++)
-        filters_used[i]=0;
-     png_set_read_user_transform_fn(read_ptr, count_filters);
-   }
-#endif
-#if defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
-   zero_samples=0;
-   png_set_write_user_transform_fn(write_ptr, count_zero_samples);
-#endif
-
-#if defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
-#  ifndef PNG_HANDLE_CHUNK_ALWAYS
-#    define PNG_HANDLE_CHUNK_ALWAYS       3
-#  endif
-   png_set_keep_unknown_chunks(read_ptr, PNG_HANDLE_CHUNK_ALWAYS,
-      png_bytep_NULL, 0);
-#endif
-#if defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED)
-#  ifndef PNG_HANDLE_CHUNK_IF_SAFE
-#    define PNG_HANDLE_CHUNK_IF_SAFE      2
-#  endif
-   png_set_keep_unknown_chunks(write_ptr, PNG_HANDLE_CHUNK_IF_SAFE,
-      png_bytep_NULL, 0);
-#endif
-
-   png_debug(0, "Reading info struct\n");
-   png_read_info(read_ptr, read_info_ptr);
-
-   png_debug(0, "Transferring info struct\n");
-   {
-      int interlace_type, compression_type, filter_type;
-
-      if (png_get_IHDR(read_ptr, read_info_ptr, &width, &height, &bit_depth,
-          &color_type, &interlace_type, &compression_type, &filter_type))
-      {
-         png_set_IHDR(write_ptr, write_info_ptr, width, height, bit_depth,
-#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
-            color_type, interlace_type, compression_type, filter_type);
-#else
-            color_type, PNG_INTERLACE_NONE, compression_type, filter_type);
-#endif
-      }
-   }
-#if defined(PNG_FIXED_POINT_SUPPORTED)
-#if defined(PNG_cHRM_SUPPORTED)
-   {
-      png_fixed_point white_x, white_y, red_x, red_y, green_x, green_y, blue_x,
-         blue_y;
-      if (png_get_cHRM_fixed(read_ptr, read_info_ptr, &white_x, &white_y, &red_x,
-         &red_y, &green_x, &green_y, &blue_x, &blue_y))
-      {
-         png_set_cHRM_fixed(write_ptr, write_info_ptr, white_x, white_y, red_x,
-            red_y, green_x, green_y, blue_x, blue_y);
-      }
-   }
-#endif
-#if defined(PNG_gAMA_SUPPORTED)
-   {
-      png_fixed_point gamma;
-
-      if (png_get_gAMA_fixed(read_ptr, read_info_ptr, &gamma))
-      {
-         png_set_gAMA_fixed(write_ptr, write_info_ptr, gamma);
-      }
-   }
-#endif
-#else /* Use floating point versions */
-#if defined(PNG_FLOATING_POINT_SUPPORTED)
-#if defined(PNG_cHRM_SUPPORTED)
-   {
-      double white_x, white_y, red_x, red_y, green_x, green_y, blue_x,
-         blue_y;
-      if (png_get_cHRM(read_ptr, read_info_ptr, &white_x, &white_y, &red_x,
-         &red_y, &green_x, &green_y, &blue_x, &blue_y))
-      {
-         png_set_cHRM(write_ptr, write_info_ptr, white_x, white_y, red_x,
-            red_y, green_x, green_y, blue_x, blue_y);
-      }
-   }
-#endif
-#if defined(PNG_gAMA_SUPPORTED)
-   {
-      double gamma;
-
-      if (png_get_gAMA(read_ptr, read_info_ptr, &gamma))
-      {
-         png_set_gAMA(write_ptr, write_info_ptr, gamma);
-      }
-   }
-#endif
-#endif /* floating point */
-#endif /* fixed point */
-#if defined(PNG_iCCP_SUPPORTED)
-   {
-      png_charp name;
-      png_charp profile;
-      png_uint_32 proflen;
-      int compression_type;
-
-      if (png_get_iCCP(read_ptr, read_info_ptr, &name, &compression_type,
-                      &profile, &proflen))
-      {
-         png_set_iCCP(write_ptr, write_info_ptr, name, compression_type,
-                      profile, proflen);
-      }
-   }
-#endif
-#if defined(PNG_sRGB_SUPPORTED)
-   {
-      int intent;
-
-      if (png_get_sRGB(read_ptr, read_info_ptr, &intent))
-      {
-         png_set_sRGB(write_ptr, write_info_ptr, intent);
-      }
-   }
-#endif
-   {
-      png_colorp palette;
-      int num_palette;
-
-      if (png_get_PLTE(read_ptr, read_info_ptr, &palette, &num_palette))
-      {
-         png_set_PLTE(write_ptr, write_info_ptr, palette, num_palette);
-      }
-   }
-#if defined(PNG_bKGD_SUPPORTED)
-   {
-      png_color_16p background;
-
-      if (png_get_bKGD(read_ptr, read_info_ptr, &background))
-      {
-         png_set_bKGD(write_ptr, write_info_ptr, background);
-      }
-   }
-#endif
-#if defined(PNG_hIST_SUPPORTED)
-   {
-      png_uint_16p hist;
-
-      if (png_get_hIST(read_ptr, read_info_ptr, &hist))
-      {
-         png_set_hIST(write_ptr, write_info_ptr, hist);
-      }
-   }
-#endif
-#if defined(PNG_oFFs_SUPPORTED)
-   {
-      png_int_32 offset_x, offset_y;
-      int unit_type;
-
-      if (png_get_oFFs(read_ptr, read_info_ptr,&offset_x,&offset_y,&unit_type))
-      {
-         png_set_oFFs(write_ptr, write_info_ptr, offset_x, offset_y, unit_type);
-      }
-   }
-#endif
-#if defined(PNG_pCAL_SUPPORTED)
-   {
-      png_charp purpose, units;
-      png_charpp params;
-      png_int_32 X0, X1;
-      int type, nparams;
-
-      if (png_get_pCAL(read_ptr, read_info_ptr, &purpose, &X0, &X1, &type,
-         &nparams, &units, &params))
-      {
-         png_set_pCAL(write_ptr, write_info_ptr, purpose, X0, X1, type,
-            nparams, units, params);
-      }
-   }
-#endif
-#if defined(PNG_pHYs_SUPPORTED)
-   {
-      png_uint_32 res_x, res_y;
-      int unit_type;
-
-      if (png_get_pHYs(read_ptr, read_info_ptr, &res_x, &res_y, &unit_type))
-      {
-         png_set_pHYs(write_ptr, write_info_ptr, res_x, res_y, unit_type);
-      }
-   }
-#endif
-#if defined(PNG_sBIT_SUPPORTED)
-   {
-      png_color_8p sig_bit;
-
-      if (png_get_sBIT(read_ptr, read_info_ptr, &sig_bit))
-      {
-         png_set_sBIT(write_ptr, write_info_ptr, sig_bit);
-      }
-   }
-#endif
-#if defined(PNG_sCAL_SUPPORTED)
-#ifdef PNG_FLOATING_POINT_SUPPORTED
-   {
-      int unit;
-      double scal_width, scal_height;
-
-      if (png_get_sCAL(read_ptr, read_info_ptr, &unit, &scal_width,
-         &scal_height))
-      {
-         png_set_sCAL(write_ptr, write_info_ptr, unit, scal_width, scal_height);
-      }
-   }
-#else
-#ifdef PNG_FIXED_POINT_SUPPORTED
-   {
-      int unit;
-      png_charp scal_width, scal_height;
-
-      if (png_get_sCAL_s(read_ptr, read_info_ptr, &unit, &scal_width,
-          &scal_height))
-      {
-         png_set_sCAL_s(write_ptr, write_info_ptr, unit, scal_width, scal_height);
-      }
-   }
-#endif
-#endif
-#endif
-#if defined(PNG_TEXT_SUPPORTED)
-   {
-      png_textp text_ptr;
-      int num_text;
-
-      if (png_get_text(read_ptr, read_info_ptr, &text_ptr, &num_text) > 0)
-      {
-         png_debug1(0, "Handling %d iTXt/tEXt/zTXt chunks\n", num_text);
-         png_set_text(write_ptr, write_info_ptr, text_ptr, num_text);
-      }
-   }
-#endif
-#if defined(PNG_tIME_SUPPORTED)
-   {
-      png_timep mod_time;
-
-      if (png_get_tIME(read_ptr, read_info_ptr, &mod_time))
-      {
-         png_set_tIME(write_ptr, write_info_ptr, mod_time);
-#if defined(PNG_TIME_RFC1123_SUPPORTED)
-         /* we have to use png_memcpy instead of "=" because the string
-            pointed to by png_convert_to_rfc1123() gets free'ed before
-            we use it */
-         png_memcpy(tIME_string,
-                    png_convert_to_rfc1123(read_ptr, mod_time), 
-                    png_sizeof(tIME_string));
-         tIME_string[png_sizeof(tIME_string)-1] = '\0';
-         tIME_chunk_present++;
-#endif /* PNG_TIME_RFC1123_SUPPORTED */
-      }
-   }
-#endif
-#if defined(PNG_tRNS_SUPPORTED)
-   {
-      png_bytep trans;
-      int num_trans;
-      png_color_16p trans_values;
-
-      if (png_get_tRNS(read_ptr, read_info_ptr, &trans, &num_trans,
-         &trans_values))
-      {
-         int sample_max = (1 << read_info_ptr->bit_depth);
-         /* libpng doesn't reject a tRNS chunk with out-of-range samples */
-         if (!((read_info_ptr->color_type == PNG_COLOR_TYPE_GRAY &&
-            (int)trans_values->gray > sample_max) ||
-            (read_info_ptr->color_type == PNG_COLOR_TYPE_RGB &&
-            ((int)trans_values->red > sample_max ||
-            (int)trans_values->green > sample_max ||
-            (int)trans_values->blue > sample_max))))
-           png_set_tRNS(write_ptr, write_info_ptr, trans, num_trans,
-              trans_values);
-      }
-   }
-#endif
-#if defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED)
-   {
-      png_unknown_chunkp unknowns;
-      int num_unknowns = (int)png_get_unknown_chunks(read_ptr, read_info_ptr,
-         &unknowns);
-      if (num_unknowns)
-      {
-         png_size_t i;
-         png_set_unknown_chunks(write_ptr, write_info_ptr, unknowns,
-           num_unknowns);
-         /* copy the locations from the read_info_ptr.  The automatically
-            generated locations in write_info_ptr are wrong because we
-            haven't written anything yet */
-         for (i = 0; i < (png_size_t)num_unknowns; i++)
-           png_set_unknown_chunk_location(write_ptr, write_info_ptr, i,
-             unknowns[i].location);
-      }
-   }
-#endif
-
-#ifdef PNG_WRITE_SUPPORTED
-   png_debug(0, "\nWriting info struct\n");
-
-/* If we wanted, we could write info in two steps:
-   png_write_info_before_PLTE(write_ptr, write_info_ptr);
- */
-   png_write_info(write_ptr, write_info_ptr);
-#endif
-
-#ifdef SINGLE_ROWBUF_ALLOC
-   png_debug(0, "\nAllocating row buffer...");
-   row_buf = (png_bytep)png_malloc(read_ptr,
-      png_get_rowbytes(read_ptr, read_info_ptr));
-   png_debug1(0, "0x%08lx\n\n", (unsigned long)row_buf);
-#endif /* SINGLE_ROWBUF_ALLOC */
-   png_debug(0, "Writing row data\n");
-
-#if defined(PNG_READ_INTERLACING_SUPPORTED) || \
-  defined(PNG_WRITE_INTERLACING_SUPPORTED)
-   num_pass = png_set_interlace_handling(read_ptr);
-#  ifdef PNG_WRITE_SUPPORTED
-   png_set_interlace_handling(write_ptr);
-#  endif
-#else
-   num_pass=1;
-#endif
-
-#ifdef PNGTEST_TIMING
-   t_stop = (float)clock();
-   t_misc += (t_stop - t_start);
-   t_start = t_stop;
-#endif
-   for (pass = 0; pass < num_pass; pass++)
-   {
-      png_debug1(0, "Writing row data for pass %d\n",pass);
-      for (y = 0; y < height; y++)
-      {
-#ifndef SINGLE_ROWBUF_ALLOC
-         png_debug2(0, "\nAllocating row buffer (pass %d, y = %ld)...", pass,y);
-         row_buf = (png_bytep)png_malloc(read_ptr,
-            png_get_rowbytes(read_ptr, read_info_ptr));
-         png_debug2(0, "0x%08lx (%ld bytes)\n", (unsigned long)row_buf,
-            png_get_rowbytes(read_ptr, read_info_ptr));
-#endif /* !SINGLE_ROWBUF_ALLOC */
-         png_read_rows(read_ptr, (png_bytepp)&row_buf, png_bytepp_NULL, 1);
-
-#ifdef PNG_WRITE_SUPPORTED
-#ifdef PNGTEST_TIMING
-         t_stop = (float)clock();
-         t_decode += (t_stop - t_start);
-         t_start = t_stop;
-#endif
-         png_write_rows(write_ptr, (png_bytepp)&row_buf, 1);
-#ifdef PNGTEST_TIMING
-         t_stop = (float)clock();
-         t_encode += (t_stop - t_start);
-         t_start = t_stop;
-#endif
-#endif /* PNG_WRITE_SUPPORTED */
-
-#ifndef SINGLE_ROWBUF_ALLOC
-         png_debug2(0, "Freeing row buffer (pass %d, y = %ld)\n\n", pass, y);
-         png_free(read_ptr, row_buf);
-#endif /* !SINGLE_ROWBUF_ALLOC */
-      }
-   }
-
-#if defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
-   png_free_data(read_ptr, read_info_ptr, PNG_FREE_UNKN, -1);
-#endif
-#if defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED)
-   png_free_data(write_ptr, write_info_ptr, PNG_FREE_UNKN, -1);
-#endif
-
-   png_debug(0, "Reading and writing end_info data\n");
-
-   png_read_end(read_ptr, end_info_ptr);
-#if defined(PNG_TEXT_SUPPORTED)
-   {
-      png_textp text_ptr;
-      int num_text;
-
-      if (png_get_text(read_ptr, end_info_ptr, &text_ptr, &num_text) > 0)
-      {
-         png_debug1(0, "Handling %d iTXt/tEXt/zTXt chunks\n", num_text);
-         png_set_text(write_ptr, write_end_info_ptr, text_ptr, num_text);
-      }
-   }
-#endif
-#if defined(PNG_tIME_SUPPORTED)
-   {
-      png_timep mod_time;
-
-      if (png_get_tIME(read_ptr, end_info_ptr, &mod_time))
-      {
-         png_set_tIME(write_ptr, write_end_info_ptr, mod_time);
-#if defined(PNG_TIME_RFC1123_SUPPORTED)
-         /* we have to use png_memcpy instead of "=" because the string
-            pointed to by png_convert_to_rfc1123() gets free'ed before
-            we use it */
-         png_memcpy(tIME_string,
-                    png_convert_to_rfc1123(read_ptr, mod_time),
-                    png_sizeof(tIME_string));
-         tIME_string[png_sizeof(tIME_string)-1] = '\0';
-         tIME_chunk_present++;
-#endif /* PNG_TIME_RFC1123_SUPPORTED */
-      }
-   }
-#endif
-#if defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED)
-   {
-      png_unknown_chunkp unknowns;
-      int num_unknowns;
-      num_unknowns = (int)png_get_unknown_chunks(read_ptr, end_info_ptr,
-         &unknowns);
-      if (num_unknowns)
-      {
-         png_size_t i;
-         png_set_unknown_chunks(write_ptr, write_end_info_ptr, unknowns,
-           num_unknowns);
-         /* copy the locations from the read_info_ptr.  The automatically
-            generated locations in write_end_info_ptr are wrong because we
-            haven't written the end_info yet */
-         for (i = 0; i < (png_size_t)num_unknowns; i++)
-           png_set_unknown_chunk_location(write_ptr, write_end_info_ptr, i,
-             unknowns[i].location);
-      }
-   }
-#endif
-#ifdef PNG_WRITE_SUPPORTED
-   png_write_end(write_ptr, write_end_info_ptr);
-#endif
-
-#ifdef PNG_EASY_ACCESS_SUPPORTED
-   if(verbose)
-   {
-      png_uint_32 iwidth, iheight;
-      iwidth = png_get_image_width(write_ptr, write_info_ptr);
-      iheight = png_get_image_height(write_ptr, write_info_ptr);
-      fprintf(STDERR, "Image width = %lu, height = %lu\n",
-         (unsigned long)iwidth, (unsigned long)iheight);
-   }
-#endif
-
-   png_debug(0, "Destroying data structs\n");
-#ifdef SINGLE_ROWBUF_ALLOC
-   png_debug(1, "destroying row_buf for read_ptr\n");
-   png_free(read_ptr, row_buf);
-   row_buf=NULL;
-#endif /* SINGLE_ROWBUF_ALLOC */
-   png_debug(1, "destroying read_ptr, read_info_ptr, end_info_ptr\n");
-   png_destroy_read_struct(&read_ptr, &read_info_ptr, &end_info_ptr);
-#ifdef PNG_WRITE_SUPPORTED
-   png_debug(1, "destroying write_end_info_ptr\n");
-   png_destroy_info_struct(write_ptr, &write_end_info_ptr);
-   png_debug(1, "destroying write_ptr, write_info_ptr\n");
-   png_destroy_write_struct(&write_ptr, &write_info_ptr);
-#endif
-   png_debug(0, "Destruction complete.\n");
-
-   FCLOSE(fpin);
-   FCLOSE(fpout);
-
-   png_debug(0, "Opening files for comparison\n");
-#if defined(_WIN32_WCE)
-   MultiByteToWideChar(CP_ACP, 0, inname, -1, path, MAX_PATH);
-   if ((fpin = CreateFile(path, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL)) == INVALID_HANDLE_VALUE)
-#else
-   if ((fpin = fopen(inname, "rb")) == NULL)
-#endif
-   {
-      fprintf(STDERR, "Could not find file %s\n", inname);
-      return (1);
-   }
-
-#if defined(_WIN32_WCE)
-   MultiByteToWideChar(CP_ACP, 0, outname, -1, path, MAX_PATH);
-   if ((fpout = CreateFile(path, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL)) == INVALID_HANDLE_VALUE)
-#else
-   if ((fpout = fopen(outname, "rb")) == NULL)
-#endif
-   {
-      fprintf(STDERR, "Could not find file %s\n", outname);
-      FCLOSE(fpin);
-      return (1);
-   }
-
-   for(;;)
-   {
-      png_size_t num_in, num_out;
-
-      READFILE(fpin, inbuf, 1, num_in);
-      READFILE(fpout, outbuf, 1, num_out);
-
-      if (num_in != num_out)
-      {
-         fprintf(STDERR, "\nFiles %s and %s are of a different size\n",
-                 inname, outname);
-         if(wrote_question == 0)
-         {
-            fprintf(STDERR,
-         "   Was %s written with the same maximum IDAT chunk size (%d bytes),",
-              inname,PNG_ZBUF_SIZE);
-            fprintf(STDERR,
-              "\n   filtering heuristic (libpng default), compression");
-            fprintf(STDERR,
-              " level (zlib default),\n   and zlib version (%s)?\n\n",
-              ZLIB_VERSION);
-            wrote_question=1;
-         }
-         FCLOSE(fpin);
-         FCLOSE(fpout);
-         return (0);
-      }
-
-      if (!num_in)
-         break;
-
-      if (png_memcmp(inbuf, outbuf, num_in))
-      {
-         fprintf(STDERR, "\nFiles %s and %s are different\n", inname, outname);
-         if(wrote_question == 0)
-         {
-            fprintf(STDERR,
-         "   Was %s written with the same maximum IDAT chunk size (%d bytes),",
-                 inname,PNG_ZBUF_SIZE);
-            fprintf(STDERR,
-              "\n   filtering heuristic (libpng default), compression");
-            fprintf(STDERR,
-              " level (zlib default),\n   and zlib version (%s)?\n\n",
-              ZLIB_VERSION);
-            wrote_question=1;
-         }
-         FCLOSE(fpin);
-         FCLOSE(fpout);
-         return (0);
-      }
-   }
-
-   FCLOSE(fpin);
-   FCLOSE(fpout);
-
-   return (0);
-}
-
-/* input and output filenames */
-#ifdef RISCOS
-static PNG_CONST char *inname = "pngtest/png";
-static PNG_CONST char *outname = "pngout/png";
-#else
-static PNG_CONST char *inname = "pngtest.png";
-static PNG_CONST char *outname = "pngout.png";
-#endif
-
-int
-main(int argc, char *argv[])
-{
-   int multiple = 0;
-   int ierror = 0;
-
-   fprintf(STDERR, "Testing libpng version %s\n", PNG_LIBPNG_VER_STRING);
-   fprintf(STDERR, "   with zlib   version %s\n", ZLIB_VERSION);
-   fprintf(STDERR,"%s",png_get_copyright(NULL));
-   /* Show the version of libpng used in building the library */
-   fprintf(STDERR," library (%lu):%s",
-      (unsigned long)png_access_version_number(),
-      png_get_header_version(NULL));
-   /* Show the version of libpng used in building the application */
-   fprintf(STDERR," pngtest (%lu):%s", (unsigned long)PNG_LIBPNG_VER,
-      PNG_HEADER_VERSION_STRING);
-   fprintf(STDERR," png_sizeof(png_struct)=%ld, png_sizeof(png_info)=%ld\n",
-                    (long)png_sizeof(png_struct), (long)png_sizeof(png_info));
-
-   /* Do some consistency checking on the memory allocation settings, I'm
-      not sure this matters, but it is nice to know, the first of these
-      tests should be impossible because of the way the macros are set
-      in pngconf.h */
-#if defined(MAXSEG_64K) && !defined(PNG_MAX_MALLOC_64K)
-      fprintf(STDERR, " NOTE: Zlib compiled for max 64k, libpng not\n");
-#endif
-   /* I think the following can happen. */
-#if !defined(MAXSEG_64K) && defined(PNG_MAX_MALLOC_64K)
-      fprintf(STDERR, " NOTE: libpng compiled for max 64k, zlib not\n");
-#endif
-
-   if (strcmp(png_libpng_ver, PNG_LIBPNG_VER_STRING))
-   {
-      fprintf(STDERR,
-         "Warning: versions are different between png.h and png.c\n");
-      fprintf(STDERR, "  png.h version: %s\n", PNG_LIBPNG_VER_STRING);
-      fprintf(STDERR, "  png.c version: %s\n\n", png_libpng_ver);
-      ++ierror;
-   }
-
-   if (argc > 1)
-   {
-      if (strcmp(argv[1], "-m") == 0)
-      {
-         multiple = 1;
-         status_dots_requested = 0;
-      }
-      else if (strcmp(argv[1], "-mv") == 0 ||
-               strcmp(argv[1], "-vm") == 0 )
-      {
-         multiple = 1;
-         verbose = 1;
-         status_dots_requested = 1;
-      }
-      else if (strcmp(argv[1], "-v") == 0)
-      {
-         verbose = 1;
-         status_dots_requested = 1;
-         inname = argv[2];
-      }
-      else
-      {
-         inname = argv[1];
-         status_dots_requested = 0;
-      }
-   }
-
-   if (!multiple && argc == 3+verbose)
-     outname = argv[2+verbose];
-
-   if ((!multiple && argc > 3+verbose) || (multiple && argc < 2))
-   {
-     fprintf(STDERR,
-       "usage: %s [infile.png] [outfile.png]\n\t%s -m {infile.png}\n",
-        argv[0], argv[0]);
-     fprintf(STDERR,
-       "  reads/writes one PNG file (without -m) or multiple files (-m)\n");
-     fprintf(STDERR,
-       "  with -m %s is used as a temporary file\n", outname);
-     exit(1);
-   }
-
-   if (multiple)
-   {
-      int i;
-#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
-      int allocation_now = current_allocation;
-#endif
-      for (i=2; i<argc; ++i)
-      {
-#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
-         int k;
-#endif
-         int kerror;
-         fprintf(STDERR, "Testing %s:",argv[i]);
-         kerror = test_one_file(argv[i], outname);
-         if (kerror == 0)
-         {
-#if defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
-            fprintf(STDERR, "\n PASS (%lu zero samples)\n",
-               (unsigned long)zero_samples);
-#else
-            fprintf(STDERR, " PASS\n");
-#endif
-#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
-            for (k=0; k<256; k++)
-               if(filters_used[k])
-                  fprintf(STDERR, " Filter %d was used %lu times\n",
-                     k,(unsigned long)filters_used[k]);
-#endif
-#if defined(PNG_TIME_RFC1123_SUPPORTED)
-         if(tIME_chunk_present != 0)
-            fprintf(STDERR, " tIME = %s\n",tIME_string);
-         tIME_chunk_present = 0;
-#endif /* PNG_TIME_RFC1123_SUPPORTED */
-         }
-         else
-         {
-            fprintf(STDERR, " FAIL\n");
-            ierror += kerror;
-         }
-#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
-         if (allocation_now != current_allocation)
-            fprintf(STDERR, "MEMORY ERROR: %d bytes lost\n",
-               current_allocation-allocation_now);
-         if (current_allocation != 0)
-         {
-            memory_infop pinfo = pinformation;
-
-            fprintf(STDERR, "MEMORY ERROR: %d bytes still allocated\n",
-               current_allocation);
-            while (pinfo != NULL)
-            {
-               fprintf(STDERR, " %lu bytes at %x\n", (unsigned long)pinfo->size,
-                 (unsigned int) pinfo->pointer);
-               pinfo = pinfo->next;
-            }
-         }
-#endif
-      }
-#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
-         fprintf(STDERR, " Current memory allocation: %10d bytes\n",
-            current_allocation);
-         fprintf(STDERR, " Maximum memory allocation: %10d bytes\n",
-            maximum_allocation);
-         fprintf(STDERR, " Total   memory allocation: %10d bytes\n",
-            total_allocation);
-         fprintf(STDERR, "     Number of allocations: %10d\n",
-            num_allocations);
-#endif
-   }
-   else
-   {
-      int i;
-      for (i=0; i<3; ++i)
-      {
-         int kerror;
-#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
-         int allocation_now = current_allocation;
-#endif
-         if (i == 1) status_dots_requested = 1;
-         else if(verbose == 0)status_dots_requested = 0;
-         if (i == 0 || verbose == 1 || ierror != 0)
-            fprintf(STDERR, "Testing %s:",inname);
-         kerror = test_one_file(inname, outname);
-         if(kerror == 0)
-         {
-            if(verbose == 1 || i == 2)
-            {
-#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
-                int k;
-#endif
-#if defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
-                fprintf(STDERR, "\n PASS (%lu zero samples)\n",
-                   (unsigned long)zero_samples);
-#else
-                fprintf(STDERR, " PASS\n");
-#endif
-#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
-                for (k=0; k<256; k++)
-                   if(filters_used[k])
-                      fprintf(STDERR, " Filter %d was used %lu times\n",
-                         k,(unsigned long)filters_used[k]);
-#endif
-#if defined(PNG_TIME_RFC1123_SUPPORTED)
-             if(tIME_chunk_present != 0)
-                fprintf(STDERR, " tIME = %s\n",tIME_string);
-#endif /* PNG_TIME_RFC1123_SUPPORTED */
-            }
-         }
-         else
-         {
-            if(verbose == 0 && i != 2)
-               fprintf(STDERR, "Testing %s:",inname);
-            fprintf(STDERR, " FAIL\n");
-            ierror += kerror;
-         }
-#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
-         if (allocation_now != current_allocation)
-             fprintf(STDERR, "MEMORY ERROR: %d bytes lost\n",
-               current_allocation-allocation_now);
-         if (current_allocation != 0)
-         {
-             memory_infop pinfo = pinformation;
-
-             fprintf(STDERR, "MEMORY ERROR: %d bytes still allocated\n",
-                current_allocation);
-             while (pinfo != NULL)
-             {
-                fprintf(STDERR," %lu bytes at %x\n",
-                   (unsigned long)pinfo->size, (unsigned int)pinfo->pointer);
-                pinfo = pinfo->next;
-             }
-          }
-#endif
-       }
-#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
-       fprintf(STDERR, " Current memory allocation: %10d bytes\n",
-          current_allocation);
-       fprintf(STDERR, " Maximum memory allocation: %10d bytes\n",
-          maximum_allocation);
-       fprintf(STDERR, " Total   memory allocation: %10d bytes\n",
-          total_allocation);
-       fprintf(STDERR, "     Number of allocations: %10d\n",
-            num_allocations);
-#endif
-   }
-
-#ifdef PNGTEST_TIMING
-   t_stop = (float)clock();
-   t_misc += (t_stop - t_start);
-   t_start = t_stop;
-   fprintf(STDERR," CPU time used = %.3f seconds",
-      (t_misc+t_decode+t_encode)/(float)CLOCKS_PER_SEC);
-   fprintf(STDERR," (decoding %.3f,\n",
-      t_decode/(float)CLOCKS_PER_SEC);
-   fprintf(STDERR,"        encoding %.3f ,",
-      t_encode/(float)CLOCKS_PER_SEC);
-   fprintf(STDERR," other %.3f seconds)\n\n",
-      t_misc/(float)CLOCKS_PER_SEC);
-#endif
-
-   if (ierror == 0)
-      fprintf(STDERR, "libpng passes test\n");
-   else
-      fprintf(STDERR, "libpng FAILS test\n");
-   return (int)(ierror != 0);
-}
-
-/* Generate a compiler error if there is an old png.h in the search path. */
-typedef version_1_2_29 your_png_h_is_not_version_1_2_29;
+\r
+/* pngtest.c - a simple test program to test libpng\r
+ *\r
+ * Last changed in libpng 1.4.1 [February 25, 2010]\r
+ * Copyright (c) 1998-2010 Glenn Randers-Pehrson\r
+ * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)\r
+ * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)\r
+ *\r
+ * This code is released under the libpng license.\r
+ * For conditions of distribution and use, see the disclaimer\r
+ * and license in png.h\r
+ *\r
+ * This program reads in a PNG image, writes it out again, and then\r
+ * compares the two files.  If the files are identical, this shows that\r
+ * the basic chunk handling, filtering, and (de)compression code is working\r
+ * properly.  It does not currently test all of the transforms, although\r
+ * it probably should.\r
+ *\r
+ * The program will report "FAIL" in certain legitimate cases:\r
+ * 1) when the compression level or filter selection method is changed.\r
+ * 2) when the maximum IDAT size (PNG_ZBUF_SIZE in pngconf.h) is not 8192.\r
+ * 3) unknown unsafe-to-copy ancillary chunks or unknown critical chunks\r
+ *    exist in the input file.\r
+ * 4) others not listed here...\r
+ * In these cases, it is best to check with another tool such as "pngcheck"\r
+ * to see what the differences between the two files are.\r
+ *\r
+ * If a filename is given on the command-line, then this file is used\r
+ * for the input, rather than the default "pngtest.png".  This allows\r
+ * testing a wide variety of files easily.  You can also test a number\r
+ * of files at once by typing "pngtest -m file1.png file2.png ..."\r
+ */\r
+\r
+#include "png.h"\r
+#include "pngpriv.h"\r
+\r
+#  include <stdio.h>\r
+#  include <stdlib.h>\r
+#  define FCLOSE(file) fclose(file)\r
+\r
+#ifndef PNG_STDIO_SUPPORTED\r
+     typedef FILE                * png_FILE_p;\r
+#endif\r
+\r
+/* Makes pngtest verbose so we can find problems (needs to be before png.h) */\r
+#ifndef PNG_DEBUG\r
+#  define PNG_DEBUG 0\r
+#endif\r
+\r
+#if !PNG_DEBUG\r
+#  define SINGLE_ROWBUF_ALLOC  /* Makes buffer overruns easier to nail */\r
+#endif\r
+\r
+/* Turn on CPU timing\r
+#define PNGTEST_TIMING\r
+*/\r
+\r
+#ifndef PNG_FLOATING_POINT_SUPPORTED\r
+#undef PNGTEST_TIMING\r
+#endif\r
+\r
+#ifdef PNGTEST_TIMING\r
+static float t_start, t_stop, t_decode, t_encode, t_misc;\r
+#include <time.h>\r
+#endif\r
+\r
+#ifdef PNG_TIME_RFC1123_SUPPORTED\r
+#define PNG_tIME_STRING_LENGTH 29\r
+static int tIME_chunk_present = 0;\r
+static char tIME_string[PNG_tIME_STRING_LENGTH] = "tIME chunk is not present";\r
+#endif\r
+\r
+static int verbose = 0;\r
+\r
+int test_one_file PNGARG((PNG_CONST char *inname, PNG_CONST char *outname));\r
+\r
+#ifdef __TURBOC__\r
+#include <mem.h>\r
+#endif\r
+\r
+/* Defined so I can write to a file on gui/windowing platforms */\r
+/*  #define STDERR stderr  */\r
+#define STDERR stdout   /* For DOS */\r
+\r
+/* In case a system header (e.g., on AIX) defined jmpbuf */\r
+#ifdef jmpbuf\r
+#  undef jmpbuf\r
+#endif\r
+\r
+/* Define png_jmpbuf() in case we are using a pre-1.0.6 version of libpng */\r
+#ifndef png_jmpbuf\r
+#  define png_jmpbuf(png_ptr) png_ptr->jmpbuf\r
+#endif\r
+\r
+/* Example of using row callbacks to make a simple progress meter */\r
+static int status_pass = 1;\r
+static int status_dots_requested = 0;\r
+static int status_dots = 1;\r
+\r
+void\r
+read_row_callback(png_structp png_ptr, png_uint_32 row_number, int pass);\r
+void\r
+read_row_callback(png_structp png_ptr, png_uint_32 row_number, int pass)\r
+{\r
+   if (png_ptr == NULL || row_number > PNG_UINT_31_MAX)\r
+      return;\r
+   if (status_pass != pass)\r
+   {\r
+      fprintf(stdout, "\n Pass %d: ", pass);\r
+      status_pass = pass;\r
+      status_dots = 31;\r
+   }\r
+   status_dots--;\r
+   if (status_dots == 0)\r
+   {\r
+      fprintf(stdout, "\n         ");\r
+      status_dots=30;\r
+   }\r
+   fprintf(stdout, "r");\r
+}\r
+\r
+void\r
+write_row_callback(png_structp png_ptr, png_uint_32 row_number, int pass);\r
+void\r
+write_row_callback(png_structp png_ptr, png_uint_32 row_number, int pass)\r
+{\r
+   if (png_ptr == NULL || row_number > PNG_UINT_31_MAX || pass > 7)\r
+      return;\r
+   fprintf(stdout, "w");\r
+}\r
+\r
+\r
+#ifdef PNG_READ_USER_TRANSFORM_SUPPORTED\r
+/* Example of using user transform callback (we don't transform anything,\r
+ * but merely examine the row filters.  We set this to 256 rather than\r
+ * 5 in case illegal filter values are present.)\r
+ */\r
+static png_uint_32 filters_used[256];\r
+void\r
+count_filters(png_structp png_ptr, png_row_infop row_info, png_bytep data);\r
+void\r
+count_filters(png_structp png_ptr, png_row_infop row_info, png_bytep data)\r
+{\r
+   if (png_ptr != NULL && row_info != NULL)\r
+      ++filters_used[*(data - 1)];\r
+}\r
+#endif\r
+\r
+#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED\r
+/* Example of using user transform callback (we don't transform anything,\r
+ * but merely count the zero samples)\r
+ */\r
+\r
+static png_uint_32 zero_samples;\r
+\r
+void\r
+count_zero_samples(png_structp png_ptr, png_row_infop row_info, png_bytep data);\r
+void\r
+count_zero_samples(png_structp png_ptr, png_row_infop row_info, png_bytep data)\r
+{\r
+   png_bytep dp = data;\r
+   if (png_ptr == NULL)return;\r
+\r
+   /* Contents of row_info:\r
+    *  png_uint_32 width      width of row\r
+    *  png_uint_32 rowbytes   number of bytes in row\r
+    *  png_byte color_type    color type of pixels\r
+    *  png_byte bit_depth     bit depth of samples\r
+    *  png_byte channels      number of channels (1-4)\r
+    *  png_byte pixel_depth   bits per pixel (depth*channels)\r
+    */\r
+\r
+    /* Counts the number of zero samples (or zero pixels if color_type is 3 */\r
+\r
+    if (row_info->color_type == 0 || row_info->color_type == 3)\r
+    {\r
+       int pos = 0;\r
+       png_uint_32 n, nstop;\r
+       for (n = 0, nstop=row_info->width; n<nstop; n++)\r
+       {\r
+          if (row_info->bit_depth == 1)\r
+          {\r
+             if (((*dp << pos++ ) & 0x80) == 0)\r
+                zero_samples++;\r
+             if (pos == 8)\r
+             {\r
+                pos = 0;\r
+                dp++;\r
+             }\r
+          }\r
+          if (row_info->bit_depth == 2)\r
+          {\r
+             if (((*dp << (pos+=2)) & 0xc0) == 0)\r
+                zero_samples++;\r
+             if (pos == 8)\r
+             {\r
+                pos = 0;\r
+                dp++;\r
+             }\r
+          }\r
+          if (row_info->bit_depth == 4)\r
+          {\r
+             if (((*dp << (pos+=4)) & 0xf0) == 0)\r
+                zero_samples++;\r
+             if (pos == 8)\r
+             {\r
+                pos = 0;\r
+                dp++;\r
+             }\r
+          }\r
+          if (row_info->bit_depth == 8)\r
+             if (*dp++ == 0)\r
+                zero_samples++;\r
+          if (row_info->bit_depth == 16)\r
+          {\r
+             if ((*dp | *(dp+1)) == 0)\r
+                zero_samples++;\r
+             dp+=2;\r
+          }\r
+       }\r
+    }\r
+    else /* Other color types */\r
+    {\r
+       png_uint_32 n, nstop;\r
+       int channel;\r
+       int color_channels = row_info->channels;\r
+       if (row_info->color_type > 3)color_channels--;\r
+\r
+       for (n = 0, nstop=row_info->width; n<nstop; n++)\r
+       {\r
+          for (channel = 0; channel < color_channels; channel++)\r
+          {\r
+             if (row_info->bit_depth == 8)\r
+                if (*dp++ == 0)\r
+                   zero_samples++;\r
+             if (row_info->bit_depth == 16)\r
+             {\r
+                if ((*dp | *(dp+1)) == 0)\r
+                   zero_samples++;\r
+                dp+=2;\r
+             }\r
+          }\r
+          if (row_info->color_type > 3)\r
+          {\r
+             dp++;\r
+             if (row_info->bit_depth == 16)\r
+                dp++;\r
+          }\r
+       }\r
+    }\r
+}\r
+#endif /* PNG_WRITE_USER_TRANSFORM_SUPPORTED */\r
+\r
+static int wrote_question = 0;\r
+\r
+#ifndef PNG_STDIO_SUPPORTED\r
+/* START of code to validate stdio-free compilation */\r
+/* These copies of the default read/write functions come from pngrio.c and\r
+ * pngwio.c.  They allow "don't include stdio" testing of the library.\r
+ * This is the function that does the actual reading of data.  If you are\r
+ * not reading from a standard C stream, you should create a replacement\r
+ * read_data function and use it at run time with png_set_read_fn(), rather\r
+ * than changing the library.\r
+ */\r
+\r
+#ifndef USE_FAR_KEYWORD\r
+static void\r
+pngtest_read_data(png_structp png_ptr, png_bytep data, png_size_t length)\r
+{\r
+   png_size_t check = 0;\r
+   png_voidp io_ptr;\r
+\r
+   /* fread() returns 0 on error, so it is OK to store this in a png_size_t\r
+    * instead of an int, which is what fread() actually returns.\r
+    */\r
+   io_ptr = png_get_io_ptr(png_ptr);\r
+   if (io_ptr != NULL)\r
+   {\r
+      check = fread(data, 1, length, (png_FILE_p)io_ptr);\r
+   }\r
+\r
+   if (check != length)\r
+   {\r
+      png_error(png_ptr, "Read Error!");\r
+   }\r
+}\r
+#else\r
+/* This is the model-independent version. Since the standard I/O library\r
+   can't handle far buffers in the medium and small models, we have to copy\r
+   the data.\r
+*/\r
+\r
+#define NEAR_BUF_SIZE 1024\r
+#define MIN(a,b) (a <= b ? a : b)\r
+\r
+static void\r
+pngtest_read_data(png_structp png_ptr, png_bytep data, png_size_t length)\r
+{\r
+   png_size_t check;\r
+   png_byte *n_data;\r
+   png_FILE_p io_ptr;\r
+\r
+   /* Check if data really is near. If so, use usual code. */\r
+   n_data = (png_byte *)CVT_PTR_NOCHECK(data);\r
+   io_ptr = (png_FILE_p)CVT_PTR(png_ptr->io_ptr);\r
+   if ((png_bytep)n_data == data)\r
+   {\r
+      check = fread(n_data, 1, length, io_ptr);\r
+   }\r
+   else\r
+   {\r
+      png_byte buf[NEAR_BUF_SIZE];\r
+      png_size_t read, remaining, err;\r
+      check = 0;\r
+      remaining = length;\r
+      do\r
+      {\r
+         read = MIN(NEAR_BUF_SIZE, remaining);\r
+         err = fread(buf, 1, 1, io_ptr);\r
+         png_memcpy(data, buf, read); /* Copy far buffer to near buffer */\r
+         if (err != read)\r
+            break;\r
+         else\r
+            check += err;\r
+         data += read;\r
+         remaining -= read;\r
+      }\r
+      while (remaining != 0);\r
+   }\r
+   if (check != length)\r
+      png_error(png_ptr, "read Error");\r
+}\r
+#endif /* USE_FAR_KEYWORD */\r
+\r
+#ifdef PNG_WRITE_FLUSH_SUPPORTED\r
+static void\r
+pngtest_flush(png_structp png_ptr)\r
+{\r
+   /* Do nothing; fflush() is said to be just a waste of energy. */\r
+   png_ptr = png_ptr;  /* Stifle compiler warning */\r
+}\r
+#endif\r
+\r
+/* This is the function that does the actual writing of data.  If you are\r
+ * not writing to a standard C stream, you should create a replacement\r
+ * write_data function and use it at run time with png_set_write_fn(), rather\r
+ * than changing the library.\r
+ */\r
+#ifndef USE_FAR_KEYWORD\r
+static void\r
+pngtest_write_data(png_structp png_ptr, png_bytep data, png_size_t length)\r
+{\r
+   png_size_t check;\r
+\r
+   check = fwrite(data, 1, length, (png_FILE_p)png_ptr->io_ptr);\r
+   if (check != length)\r
+   {\r
+      png_error(png_ptr, "Write Error");\r
+   }\r
+}\r
+#else\r
+/* This is the model-independent version. Since the standard I/O library\r
+   can't handle far buffers in the medium and small models, we have to copy\r
+   the data.\r
+*/\r
+\r
+#define NEAR_BUF_SIZE 1024\r
+#define MIN(a,b) (a <= b ? a : b)\r
+\r
+static void\r
+pngtest_write_data(png_structp png_ptr, png_bytep data, png_size_t length)\r
+{\r
+   png_size_t check;\r
+   png_byte *near_data;  /* Needs to be "png_byte *" instead of "png_bytep" */\r
+   png_FILE_p io_ptr;\r
+\r
+   /* Check if data really is near. If so, use usual code. */\r
+   near_data = (png_byte *)CVT_PTR_NOCHECK(data);\r
+   io_ptr = (png_FILE_p)CVT_PTR(png_ptr->io_ptr);\r
+   if ((png_bytep)near_data == data)\r
+   {\r
+      check = fwrite(near_data, 1, length, io_ptr);\r
+   }\r
+   else\r
+   {\r
+      png_byte buf[NEAR_BUF_SIZE];\r
+      png_size_t written, remaining, err;\r
+      check = 0;\r
+      remaining = length;\r
+      do\r
+      {\r
+         written = MIN(NEAR_BUF_SIZE, remaining);\r
+         png_memcpy(buf, data, written); /* Copy far buffer to near buffer */\r
+         err = fwrite(buf, 1, written, io_ptr);\r
+         if (err != written)\r
+            break;\r
+         else\r
+            check += err;\r
+         data += written;\r
+         remaining -= written;\r
+      }\r
+      while (remaining != 0);\r
+   }\r
+   if (check != length)\r
+   {\r
+      png_error(png_ptr, "Write Error");\r
+   }\r
+}\r
+#endif /* USE_FAR_KEYWORD */\r
+\r
+/* This function is called when there is a warning, but the library thinks\r
+ * it can continue anyway.  Replacement functions don't have to do anything\r
+ * here if you don't want to.  In the default configuration, png_ptr is\r
+ * not used, but it is passed in case it may be useful.\r
+ */\r
+static void\r
+pngtest_warning(png_structp png_ptr, png_const_charp message)\r
+{\r
+   PNG_CONST char *name = "UNKNOWN (ERROR!)";\r
+   char *test;\r
+   test = png_get_error_ptr(png_ptr);\r
+   if (test == NULL)\r
+     fprintf(STDERR, "%s: libpng warning: %s\n", name, message);\r
+   else\r
+     fprintf(STDERR, "%s: libpng warning: %s\n", test, message);\r
+}\r
+\r
+/* This is the default error handling function.  Note that replacements for\r
+ * this function MUST NOT RETURN, or the program will likely crash.  This\r
+ * function is used by default, or if the program supplies NULL for the\r
+ * error function pointer in png_set_error_fn().\r
+ */\r
+static void\r
+pngtest_error(png_structp png_ptr, png_const_charp message)\r
+{\r
+   pngtest_warning(png_ptr, message);\r
+   /* We can return because png_error calls the default handler, which is\r
+    * actually OK in this case.\r
+    */\r
+}\r
+#endif /* !PNG_STDIO_SUPPORTED */\r
+/* END of code to validate stdio-free compilation */\r
+\r
+/* START of code to validate memory allocation and deallocation */\r
+#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG\r
+\r
+/* Allocate memory.  For reasonable files, size should never exceed\r
+ * 64K.  However, zlib may allocate more then 64K if you don't tell\r
+ * it not to.  See zconf.h and png.h for more information.  zlib does\r
+ * need to allocate exactly 64K, so whatever you call here must\r
+ * have the ability to do that.\r
+ *\r
+ * This piece of code can be compiled to validate max 64K allocations\r
+ * by setting MAXSEG_64K in zlib zconf.h *or* PNG_MAX_MALLOC_64K.\r
+ */\r
+typedef struct memory_information\r
+{\r
+   png_alloc_size_t          size;\r
+   png_voidp                 pointer;\r
+   struct memory_information FAR *next;\r
+} memory_information;\r
+typedef memory_information FAR *memory_infop;\r
+\r
+static memory_infop pinformation = NULL;\r
+static int current_allocation = 0;\r
+static int maximum_allocation = 0;\r
+static int total_allocation = 0;\r
+static int num_allocations = 0;\r
+\r
+png_voidp png_debug_malloc\r
+   PNGARG((png_structp png_ptr, png_alloc_size_t size));\r
+void png_debug_free PNGARG((png_structp png_ptr, png_voidp ptr));\r
+\r
+png_voidp\r
+png_debug_malloc(png_structp png_ptr, png_alloc_size_t size)\r
+{\r
+\r
+   /* png_malloc has already tested for NULL; png_create_struct calls\r
+    * png_debug_malloc directly, with png_ptr == NULL which is OK\r
+    */\r
+\r
+   if (size == 0)\r
+      return (NULL);\r
+\r
+   /* This calls the library allocator twice, once to get the requested\r
+      buffer and once to get a new free list entry. */\r
+   {\r
+      /* Disable malloc_fn and free_fn */\r
+      memory_infop pinfo;\r
+      png_set_mem_fn(png_ptr, NULL, NULL, NULL);\r
+      pinfo = (memory_infop)png_malloc(png_ptr,\r
+         png_sizeof(*pinfo));\r
+      pinfo->size = size;\r
+      current_allocation += size;\r
+      total_allocation += size;\r
+      num_allocations ++;\r
+      if (current_allocation > maximum_allocation)\r
+         maximum_allocation = current_allocation;\r
+      pinfo->pointer = png_malloc(png_ptr, size);\r
+      /* Restore malloc_fn and free_fn */\r
+      png_set_mem_fn(png_ptr,\r
+          NULL, png_debug_malloc, png_debug_free);\r
+      if (size != 0 && pinfo->pointer == NULL)\r
+      {\r
+         current_allocation -= size;\r
+         total_allocation -= size;\r
+         png_error(png_ptr,\r
+           "out of memory in pngtest->png_debug_malloc");\r
+      }\r
+      pinfo->next = pinformation;\r
+      pinformation = pinfo;\r
+      /* Make sure the caller isn't assuming zeroed memory. */\r
+      png_memset(pinfo->pointer, 0xdd, pinfo->size);\r
+      if (verbose)\r
+         printf("png_malloc %lu bytes at %x\n", (unsigned long)size,\r
+            pinfo->pointer);\r
+      return (png_voidp)(pinfo->pointer);\r
+   }\r
+}\r
+\r
+/* Free a pointer.  It is removed from the list at the same time. */\r
+void\r
+png_debug_free(png_structp png_ptr, png_voidp ptr)\r
+{\r
+   if (png_ptr == NULL)\r
+      fprintf(STDERR, "NULL pointer to png_debug_free.\n");\r
+   if (ptr == 0)\r
+   {\r
+#if 0 /* This happens all the time. */\r
+      fprintf(STDERR, "WARNING: freeing NULL pointer\n");\r
+#endif\r
+      return;\r
+   }\r
+\r
+   /* Unlink the element from the list. */\r
+   {\r
+      memory_infop FAR *ppinfo = &pinformation;\r
+      for (;;)\r
+      {\r
+         memory_infop pinfo = *ppinfo;\r
+         if (pinfo->pointer == ptr)\r
+         {\r
+            *ppinfo = pinfo->next;\r
+            current_allocation -= pinfo->size;\r
+            if (current_allocation < 0)\r
+               fprintf(STDERR, "Duplicate free of memory\n");\r
+            /* We must free the list element too, but first kill\r
+               the memory that is to be freed. */\r
+            png_memset(ptr, 0x55, pinfo->size);\r
+            png_free_default(png_ptr, pinfo);\r
+            pinfo = NULL;\r
+            break;\r
+         }\r
+         if (pinfo->next == NULL)\r
+         {\r
+            fprintf(STDERR, "Pointer %x not found\n", (unsigned int)ptr);\r
+            break;\r
+         }\r
+         ppinfo = &pinfo->next;\r
+      }\r
+   }\r
+\r
+   /* Finally free the data. */\r
+   if (verbose)\r
+      printf("Freeing %x\n", ptr);\r
+   png_free_default(png_ptr, ptr);\r
+   ptr = NULL;\r
+}\r
+#endif /* PNG_USER_MEM_SUPPORTED && PNG_DEBUG */\r
+/* END of code to test memory allocation/deallocation */\r
+\r
+\r
+/* Demonstration of user chunk support of the sTER and vpAg chunks */\r
+#ifdef PNG_UNKNOWN_CHUNKS_SUPPORTED\r
+\r
+/* (sTER is a public chunk not yet known by libpng.  vpAg is a private\r
+chunk used in ImageMagick to store "virtual page" size).  */\r
+\r
+static png_uint_32 user_chunk_data[4];\r
+\r
+    /* 0: sTER mode + 1\r
+     * 1: vpAg width\r
+     * 2: vpAg height\r
+     * 3: vpAg units\r
+     */\r
+\r
+static int read_user_chunk_callback(png_struct *png_ptr,\r
+   png_unknown_chunkp chunk)\r
+{\r
+   png_uint_32\r
+     *my_user_chunk_data;\r
+\r
+   /* Return one of the following:\r
+    *    return (-n);  chunk had an error\r
+    *    return (0);  did not recognize\r
+    *    return (n);  success\r
+    *\r
+    * The unknown chunk structure contains the chunk data:\r
+    * png_byte name[5];\r
+    * png_byte *data;\r
+    * png_size_t size;\r
+    *\r
+    * Note that libpng has already taken care of the CRC handling.\r
+    */\r
+\r
+   if (chunk->name[0] == 115 && chunk->name[1] ==  84 &&     /* s  T */\r
+       chunk->name[2] ==  69 && chunk->name[3] ==  82)       /* E  R */\r
+      {\r
+         /* Found sTER chunk */\r
+         if (chunk->size != 1)\r
+            return (-1); /* Error return */\r
+         if (chunk->data[0] != 0 && chunk->data[0] != 1)\r
+            return (-1);  /* Invalid mode */\r
+         my_user_chunk_data=(png_uint_32 *) png_get_user_chunk_ptr(png_ptr);\r
+         my_user_chunk_data[0]=chunk->data[0]+1;\r
+         return (1);\r
+      }\r
+\r
+   if (chunk->name[0] != 118 || chunk->name[1] != 112 ||    /* v  p */\r
+       chunk->name[2] !=  65 || chunk->name[3] != 103)      /* A  g */\r
+      return (0); /* Did not recognize */\r
+\r
+   /* Found ImageMagick vpAg chunk */\r
+\r
+   if (chunk->size != 9)\r
+      return (-1); /* Error return */\r
+\r
+   my_user_chunk_data=(png_uint_32 *) png_get_user_chunk_ptr(png_ptr);\r
+\r
+   my_user_chunk_data[1]=png_get_uint_31(png_ptr, chunk->data);\r
+   my_user_chunk_data[2]=png_get_uint_31(png_ptr, chunk->data + 4);\r
+   my_user_chunk_data[3]=(png_uint_32)chunk->data[8];\r
+\r
+   return (1);\r
+\r
+}\r
+#endif\r
+/* END of code to demonstrate user chunk support */\r
+\r
+/* Test one file */\r
+int\r
+test_one_file(PNG_CONST char *inname, PNG_CONST char *outname)\r
+{\r
+   static png_FILE_p fpin;\r
+   static png_FILE_p fpout;  /* "static" prevents setjmp corruption */\r
+   png_structp read_ptr;\r
+   png_infop read_info_ptr, end_info_ptr;\r
+#ifdef PNG_WRITE_SUPPORTED\r
+   png_structp write_ptr;\r
+   png_infop write_info_ptr;\r
+   png_infop write_end_info_ptr;\r
+#else\r
+   png_structp write_ptr = NULL;\r
+   png_infop write_info_ptr = NULL;\r
+   png_infop write_end_info_ptr = NULL;\r
+#endif\r
+   png_bytep row_buf;\r
+   png_uint_32 y;\r
+   png_uint_32 width, height;\r
+   int num_pass, pass;\r
+   int bit_depth, color_type;\r
+#ifdef PNG_SETJMP_SUPPORTED\r
+#ifdef USE_FAR_KEYWORD\r
+   jmp_buf jmpbuf;\r
+#endif\r
+#endif\r
+\r
+   char inbuf[256], outbuf[256];\r
+\r
+   row_buf = NULL;\r
+\r
+   if ((fpin = fopen(inname, "rb")) == NULL)\r
+   {\r
+      fprintf(STDERR, "Could not find input file %s\n", inname);\r
+      return (1);\r
+   }\r
+\r
+   if ((fpout = fopen(outname, "wb")) == NULL)\r
+   {\r
+      fprintf(STDERR, "Could not open output file %s\n", outname);\r
+      FCLOSE(fpin);\r
+      return (1);\r
+   }\r
+\r
+   png_debug(0, "Allocating read and write structures");\r
+#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG\r
+   read_ptr =\r
+      png_create_read_struct_2(PNG_LIBPNG_VER_STRING, NULL,\r
+      NULL, NULL, NULL,\r
+      (png_malloc_ptr)png_debug_malloc, (png_free_ptr)png_debug_free);\r
+#else\r
+   read_ptr =\r
+      png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);\r
+#endif\r
+#ifndef PNG_STDIO_SUPPORTED\r
+   png_set_error_fn(read_ptr, (png_voidp)inname, pngtest_error,\r
+       pngtest_warning);\r
+#endif\r
+\r
+#ifdef PNG_UNKNOWN_CHUNKS_SUPPORTED\r
+   user_chunk_data[0] = 0;\r
+   user_chunk_data[1] = 0;\r
+   user_chunk_data[2] = 0;\r
+   user_chunk_data[3] = 0;\r
+   png_set_read_user_chunk_fn(read_ptr, user_chunk_data,\r
+     read_user_chunk_callback);\r
+\r
+#endif\r
+#ifdef PNG_WRITE_SUPPORTED\r
+#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG\r
+   write_ptr =\r
+      png_create_write_struct_2(PNG_LIBPNG_VER_STRING, NULL,\r
+      NULL, NULL, NULL, png_debug_malloc, png_debug_free);\r
+#else\r
+   write_ptr =\r
+      png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);\r
+#endif\r
+#ifndef PNG_STDIO_SUPPORTED\r
+   png_set_error_fn(write_ptr, (png_voidp)inname, pngtest_error,\r
+       pngtest_warning);\r
+#endif\r
+#endif\r
+   png_debug(0, "Allocating read_info, write_info and end_info structures");\r
+   read_info_ptr = png_create_info_struct(read_ptr);\r
+   end_info_ptr = png_create_info_struct(read_ptr);\r
+#ifdef PNG_WRITE_SUPPORTED\r
+   write_info_ptr = png_create_info_struct(write_ptr);\r
+   write_end_info_ptr = png_create_info_struct(write_ptr);\r
+#endif\r
+\r
+#ifdef PNG_SETJMP_SUPPORTED\r
+   png_debug(0, "Setting jmpbuf for read struct");\r
+#ifdef USE_FAR_KEYWORD\r
+   if (setjmp(jmpbuf))\r
+#else\r
+   if (setjmp(png_jmpbuf(read_ptr)))\r
+#endif\r
+   {\r
+      fprintf(STDERR, "%s -> %s: libpng read error\n", inname, outname);\r
+      png_free(read_ptr, row_buf);\r
+      row_buf = NULL;\r
+      png_destroy_read_struct(&read_ptr, &read_info_ptr, &end_info_ptr);\r
+#ifdef PNG_WRITE_SUPPORTED\r
+      png_destroy_info_struct(write_ptr, &write_end_info_ptr);\r
+      png_destroy_write_struct(&write_ptr, &write_info_ptr);\r
+#endif\r
+      FCLOSE(fpin);\r
+      FCLOSE(fpout);\r
+      return (1);\r
+   }\r
+#ifdef USE_FAR_KEYWORD\r
+   png_memcpy(png_jmpbuf(read_ptr), jmpbuf, png_sizeof(jmp_buf));\r
+#endif\r
+\r
+#ifdef PNG_WRITE_SUPPORTED\r
+   png_debug(0, "Setting jmpbuf for write struct");\r
+#ifdef USE_FAR_KEYWORD\r
+   if (setjmp(jmpbuf))\r
+#else\r
+   if (setjmp(png_jmpbuf(write_ptr)))\r
+#endif\r
+   {\r
+      fprintf(STDERR, "%s -> %s: libpng write error\n", inname, outname);\r
+      png_destroy_read_struct(&read_ptr, &read_info_ptr, &end_info_ptr);\r
+      png_destroy_info_struct(write_ptr, &write_end_info_ptr);\r
+#ifdef PNG_WRITE_SUPPORTED\r
+      png_destroy_write_struct(&write_ptr, &write_info_ptr);\r
+#endif\r
+      FCLOSE(fpin);\r
+      FCLOSE(fpout);\r
+      return (1);\r
+   }\r
+#ifdef USE_FAR_KEYWORD\r
+   png_memcpy(png_jmpbuf(write_ptr), jmpbuf, png_sizeof(jmp_buf));\r
+#endif\r
+#endif\r
+#endif\r
+\r
+   png_debug(0, "Initializing input and output streams");\r
+#ifdef PNG_STDIO_SUPPORTED\r
+   png_init_io(read_ptr, fpin);\r
+#  ifdef PNG_WRITE_SUPPORTED\r
+   png_init_io(write_ptr, fpout);\r
+#  endif\r
+#else\r
+   png_set_read_fn(read_ptr, (png_voidp)fpin, pngtest_read_data);\r
+#  ifdef PNG_WRITE_SUPPORTED\r
+   png_set_write_fn(write_ptr, (png_voidp)fpout,  pngtest_write_data,\r
+#    ifdef PNG_WRITE_FLUSH_SUPPORTED\r
+      pngtest_flush);\r
+#    else\r
+      NULL);\r
+#    endif\r
+#  endif\r
+#endif\r
+   if (status_dots_requested == 1)\r
+   {\r
+#ifdef PNG_WRITE_SUPPORTED\r
+      png_set_write_status_fn(write_ptr, write_row_callback);\r
+#endif\r
+      png_set_read_status_fn(read_ptr, read_row_callback);\r
+   }\r
+   else\r
+   {\r
+#ifdef PNG_WRITE_SUPPORTED\r
+      png_set_write_status_fn(write_ptr, NULL);\r
+#endif\r
+      png_set_read_status_fn(read_ptr, NULL);\r
+   }\r
+\r
+#ifdef PNG_READ_USER_TRANSFORM_SUPPORTED\r
+   {\r
+      int i;\r
+      for (i = 0; i<256; i++)\r
+         filters_used[i] = 0;\r
+      png_set_read_user_transform_fn(read_ptr, count_filters);\r
+   }\r
+#endif\r
+#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED\r
+   zero_samples = 0;\r
+   png_set_write_user_transform_fn(write_ptr, count_zero_samples);\r
+#endif\r
+\r
+#ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED\r
+#  ifndef PNG_HANDLE_CHUNK_ALWAYS\r
+#    define PNG_HANDLE_CHUNK_ALWAYS       3\r
+#  endif\r
+   png_set_keep_unknown_chunks(read_ptr, PNG_HANDLE_CHUNK_ALWAYS,\r
+      NULL, 0);\r
+#endif\r
+#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED\r
+#  ifndef PNG_HANDLE_CHUNK_IF_SAFE\r
+#    define PNG_HANDLE_CHUNK_IF_SAFE      2\r
+#  endif\r
+   png_set_keep_unknown_chunks(write_ptr, PNG_HANDLE_CHUNK_IF_SAFE,\r
+      NULL, 0);\r
+#endif\r
+\r
+   png_debug(0, "Reading info struct");\r
+   png_read_info(read_ptr, read_info_ptr);\r
+\r
+   png_debug(0, "Transferring info struct");\r
+   {\r
+      int interlace_type, compression_type, filter_type;\r
+\r
+      if (png_get_IHDR(read_ptr, read_info_ptr, &width, &height, &bit_depth,\r
+          &color_type, &interlace_type, &compression_type, &filter_type))\r
+      {\r
+         png_set_IHDR(write_ptr, write_info_ptr, width, height, bit_depth,\r
+#ifdef PNG_WRITE_INTERLACING_SUPPORTED\r
+            color_type, interlace_type, compression_type, filter_type);\r
+#else\r
+            color_type, PNG_INTERLACE_NONE, compression_type, filter_type);\r
+#endif\r
+      }\r
+   }\r
+#ifdef PNG_FIXED_POINT_SUPPORTED\r
+#ifdef PNG_cHRM_SUPPORTED\r
+   {\r
+      png_fixed_point white_x, white_y, red_x, red_y, green_x, green_y, blue_x,\r
+         blue_y;\r
+      if (png_get_cHRM_fixed(read_ptr, read_info_ptr, &white_x, &white_y,\r
+         &red_x, &red_y, &green_x, &green_y, &blue_x, &blue_y))\r
+      {\r
+         png_set_cHRM_fixed(write_ptr, write_info_ptr, white_x, white_y, red_x,\r
+            red_y, green_x, green_y, blue_x, blue_y);\r
+      }\r
+   }\r
+#endif\r
+#ifdef PNG_gAMA_SUPPORTED\r
+   {\r
+      png_fixed_point gamma;\r
+\r
+      if (png_get_gAMA_fixed(read_ptr, read_info_ptr, &gamma))\r
+         png_set_gAMA_fixed(write_ptr, write_info_ptr, gamma);\r
+   }\r
+#endif\r
+#else /* Use floating point versions */\r
+#ifdef PNG_FLOATING_POINT_SUPPORTED\r
+#ifdef PNG_cHRM_SUPPORTED\r
+   {\r
+      double white_x, white_y, red_x, red_y, green_x, green_y, blue_x,\r
+         blue_y;\r
+      if (png_get_cHRM(read_ptr, read_info_ptr, &white_x, &white_y, &red_x,\r
+         &red_y, &green_x, &green_y, &blue_x, &blue_y))\r
+      {\r
+         png_set_cHRM(write_ptr, write_info_ptr, white_x, white_y, red_x,\r
+            red_y, green_x, green_y, blue_x, blue_y);\r
+      }\r
+   }\r
+#endif\r
+#ifdef PNG_gAMA_SUPPORTED\r
+   {\r
+      double gamma;\r
+\r
+      if (png_get_gAMA(read_ptr, read_info_ptr, &gamma))\r
+         png_set_gAMA(write_ptr, write_info_ptr, gamma);\r
+   }\r
+#endif\r
+#endif /* Floating point */\r
+#endif /* Fixed point */\r
+#ifdef PNG_iCCP_SUPPORTED\r
+   {\r
+      png_charp name;\r
+      png_charp profile;\r
+      png_uint_32 proflen;\r
+      int compression_type;\r
+\r
+      if (png_get_iCCP(read_ptr, read_info_ptr, &name, &compression_type,\r
+                      &profile, &proflen))\r
+      {\r
+         png_set_iCCP(write_ptr, write_info_ptr, name, compression_type,\r
+                      profile, proflen);\r
+      }\r
+   }\r
+#endif\r
+#ifdef PNG_sRGB_SUPPORTED\r
+   {\r
+      int intent;\r
+\r
+      if (png_get_sRGB(read_ptr, read_info_ptr, &intent))\r
+         png_set_sRGB(write_ptr, write_info_ptr, intent);\r
+   }\r
+#endif\r
+   {\r
+      png_colorp palette;\r
+      int num_palette;\r
+\r
+      if (png_get_PLTE(read_ptr, read_info_ptr, &palette, &num_palette))\r
+         png_set_PLTE(write_ptr, write_info_ptr, palette, num_palette);\r
+   }\r
+#ifdef PNG_bKGD_SUPPORTED\r
+   {\r
+      png_color_16p background;\r
+\r
+      if (png_get_bKGD(read_ptr, read_info_ptr, &background))\r
+      {\r
+         png_set_bKGD(write_ptr, write_info_ptr, background);\r
+      }\r
+   }\r
+#endif\r
+#ifdef PNG_hIST_SUPPORTED\r
+   {\r
+      png_uint_16p hist;\r
+\r
+      if (png_get_hIST(read_ptr, read_info_ptr, &hist))\r
+         png_set_hIST(write_ptr, write_info_ptr, hist);\r
+   }\r
+#endif\r
+#ifdef PNG_oFFs_SUPPORTED\r
+   {\r
+      png_int_32 offset_x, offset_y;\r
+      int unit_type;\r
+\r
+      if (png_get_oFFs(read_ptr, read_info_ptr, &offset_x, &offset_y,\r
+          &unit_type))\r
+      {\r
+         png_set_oFFs(write_ptr, write_info_ptr, offset_x, offset_y, unit_type);\r
+      }\r
+   }\r
+#endif\r
+#ifdef PNG_pCAL_SUPPORTED\r
+   {\r
+      png_charp purpose, units;\r
+      png_charpp params;\r
+      png_int_32 X0, X1;\r
+      int type, nparams;\r
+\r
+      if (png_get_pCAL(read_ptr, read_info_ptr, &purpose, &X0, &X1, &type,\r
+         &nparams, &units, &params))\r
+      {\r
+         png_set_pCAL(write_ptr, write_info_ptr, purpose, X0, X1, type,\r
+            nparams, units, params);\r
+      }\r
+   }\r
+#endif\r
+#ifdef PNG_pHYs_SUPPORTED\r
+   {\r
+      png_uint_32 res_x, res_y;\r
+      int unit_type;\r
+\r
+      if (png_get_pHYs(read_ptr, read_info_ptr, &res_x, &res_y, &unit_type))\r
+         png_set_pHYs(write_ptr, write_info_ptr, res_x, res_y, unit_type);\r
+   }\r
+#endif\r
+#ifdef PNG_sBIT_SUPPORTED\r
+   {\r
+      png_color_8p sig_bit;\r
+\r
+      if (png_get_sBIT(read_ptr, read_info_ptr, &sig_bit))\r
+         png_set_sBIT(write_ptr, write_info_ptr, sig_bit);\r
+   }\r
+#endif\r
+#ifdef PNG_sCAL_SUPPORTED\r
+#ifdef PNG_FLOATING_POINT_SUPPORTED\r
+   {\r
+      int unit;\r
+      double scal_width, scal_height;\r
+\r
+      if (png_get_sCAL(read_ptr, read_info_ptr, &unit, &scal_width,\r
+         &scal_height))\r
+      {\r
+         png_set_sCAL(write_ptr, write_info_ptr, unit, scal_width, scal_height);\r
+      }\r
+   }\r
+#else\r
+#ifdef PNG_FIXED_POINT_SUPPORTED\r
+   {\r
+      int unit;\r
+      png_charp scal_width, scal_height;\r
+\r
+      if (png_get_sCAL_s(read_ptr, read_info_ptr, &unit, &scal_width,\r
+          &scal_height))\r
+      {\r
+         png_set_sCAL_s(write_ptr, write_info_ptr, unit, scal_width,\r
+             scal_height);\r
+      }\r
+   }\r
+#endif\r
+#endif\r
+#endif\r
+#ifdef PNG_TEXT_SUPPORTED\r
+   {\r
+      png_textp text_ptr;\r
+      int num_text;\r
+\r
+      if (png_get_text(read_ptr, read_info_ptr, &text_ptr, &num_text) > 0)\r
+      {\r
+         png_debug1(0, "Handling %d iTXt/tEXt/zTXt chunks", num_text);\r
+         png_set_text(write_ptr, write_info_ptr, text_ptr, num_text);\r
+      }\r
+   }\r
+#endif\r
+#ifdef PNG_tIME_SUPPORTED\r
+   {\r
+      png_timep mod_time;\r
+\r
+      if (png_get_tIME(read_ptr, read_info_ptr, &mod_time))\r
+      {\r
+         png_set_tIME(write_ptr, write_info_ptr, mod_time);\r
+#ifdef PNG_TIME_RFC1123_SUPPORTED\r
+         /* We have to use png_memcpy instead of "=" because the string\r
+          * pointed to by png_convert_to_rfc1123() gets free'ed before\r
+          * we use it.\r
+          */\r
+         png_memcpy(tIME_string,\r
+                    png_convert_to_rfc1123(read_ptr, mod_time),\r
+                    png_sizeof(tIME_string));\r
+         tIME_string[png_sizeof(tIME_string) - 1] = '\0';\r
+         tIME_chunk_present++;\r
+#endif /* PNG_TIME_RFC1123_SUPPORTED */\r
+      }\r
+   }\r
+#endif\r
+#ifdef PNG_tRNS_SUPPORTED\r
+   {\r
+      png_bytep trans_alpha;\r
+      int num_trans;\r
+      png_color_16p trans_color;\r
+\r
+      if (png_get_tRNS(read_ptr, read_info_ptr, &trans_alpha, &num_trans,\r
+         &trans_color))\r
+      {\r
+         int sample_max = (1 << bit_depth);\r
+         /* libpng doesn't reject a tRNS chunk with out-of-range samples */\r
+         if (!((color_type == PNG_COLOR_TYPE_GRAY &&\r
+             (int)trans_color->gray > sample_max) ||\r
+             (color_type == PNG_COLOR_TYPE_RGB &&\r
+             ((int)trans_color->red > sample_max ||\r
+             (int)trans_color->green > sample_max ||\r
+             (int)trans_color->blue > sample_max))))\r
+            png_set_tRNS(write_ptr, write_info_ptr, trans_alpha, num_trans,\r
+               trans_color);\r
+      }\r
+   }\r
+#endif\r
+#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED\r
+   {\r
+      png_unknown_chunkp unknowns;\r
+      int num_unknowns = (int)png_get_unknown_chunks(read_ptr, read_info_ptr,\r
+         &unknowns);\r
+      if (num_unknowns)\r
+      {\r
+         png_size_t i;\r
+         png_set_unknown_chunks(write_ptr, write_info_ptr, unknowns,\r
+           num_unknowns);\r
+         /* Copy the locations from the read_info_ptr.  The automatically\r
+          * generated locations in write_info_ptr are wrong because we\r
+          * haven't written anything yet.\r
+          */\r
+         for (i = 0; i < (png_size_t)num_unknowns; i++)\r
+           png_set_unknown_chunk_location(write_ptr, write_info_ptr, i,\r
+             unknowns[i].location);\r
+      }\r
+   }\r
+#endif\r
+\r
+#ifdef PNG_WRITE_SUPPORTED\r
+   png_debug(0, "Writing info struct");\r
+\r
+/* If we wanted, we could write info in two steps:\r
+ * png_write_info_before_PLTE(write_ptr, write_info_ptr);\r
+ */\r
+   png_write_info(write_ptr, write_info_ptr);\r
+\r
+#ifdef PNG_UNKNOWN_CHUNKS_SUPPORTED\r
+   if (user_chunk_data[0] != 0)\r
+   {\r
+      png_byte png_sTER[5] = {115,  84,  69,  82, '\0'};\r
+\r
+      unsigned char\r
+        ster_chunk_data[1];\r
+\r
+      if (verbose)\r
+         fprintf(STDERR, "\n stereo mode = %lu\n",\r
+           (unsigned long)(user_chunk_data[0] - 1));\r
+      ster_chunk_data[0]=(unsigned char)(user_chunk_data[0] - 1);\r
+      png_write_chunk(write_ptr, png_sTER, ster_chunk_data, 1);\r
+   }\r
+   if (user_chunk_data[1] != 0 || user_chunk_data[2] != 0)\r
+   {\r
+      png_byte png_vpAg[5] = {118, 112,  65, 103, '\0'};\r
+\r
+      unsigned char\r
+        vpag_chunk_data[9];\r
+\r
+      if (verbose)\r
+         fprintf(STDERR, " vpAg = %lu x %lu, units = %lu\n",\r
+           (unsigned long)user_chunk_data[1],\r
+           (unsigned long)user_chunk_data[2],\r
+           (unsigned long)user_chunk_data[3]);\r
+      png_save_uint_32(vpag_chunk_data, user_chunk_data[1]);\r
+      png_save_uint_32(vpag_chunk_data + 4, user_chunk_data[2]);\r
+      vpag_chunk_data[8] = (unsigned char)(user_chunk_data[3] & 0xff);\r
+      png_write_chunk(write_ptr, png_vpAg, vpag_chunk_data, 9);\r
+   }\r
+\r
+#endif\r
+#endif\r
+\r
+#ifdef SINGLE_ROWBUF_ALLOC\r
+   png_debug(0, "Allocating row buffer...");\r
+   row_buf = (png_bytep)png_malloc(read_ptr,\r
+      png_get_rowbytes(read_ptr, read_info_ptr));\r
+   png_debug1(0, "0x%08lx", (unsigned long)row_buf);\r
+#endif /* SINGLE_ROWBUF_ALLOC */\r
+   png_debug(0, "Writing row data");\r
+\r
+#if defined(PNG_READ_INTERLACING_SUPPORTED) || \\r
+  defined(PNG_WRITE_INTERLACING_SUPPORTED)\r
+   num_pass = png_set_interlace_handling(read_ptr);\r
+#  ifdef PNG_WRITE_SUPPORTED\r
+   png_set_interlace_handling(write_ptr);\r
+#  endif\r
+#else\r
+   num_pass = 1;\r
+#endif\r
+\r
+#ifdef PNGTEST_TIMING\r
+   t_stop = (float)clock();\r
+   t_misc += (t_stop - t_start);\r
+   t_start = t_stop;\r
+#endif\r
+   for (pass = 0; pass < num_pass; pass++)\r
+   {\r
+      png_debug1(0, "Writing row data for pass %d", pass);\r
+      for (y = 0; y < height; y++)\r
+      {\r
+#ifndef SINGLE_ROWBUF_ALLOC\r
+         png_debug2(0, "Allocating row buffer (pass %d, y = %ld)...", pass, y);\r
+         row_buf = (png_bytep)png_malloc(read_ptr,\r
+            png_get_rowbytes(read_ptr, read_info_ptr));\r
+         png_debug2(0, "0x%08lx (%ld bytes)", (unsigned long)row_buf,\r
+            png_get_rowbytes(read_ptr, read_info_ptr));\r
+#endif /* !SINGLE_ROWBUF_ALLOC */\r
+         png_read_rows(read_ptr, (png_bytepp)&row_buf, NULL, 1);\r
+\r
+#ifdef PNG_WRITE_SUPPORTED\r
+#ifdef PNGTEST_TIMING\r
+         t_stop = (float)clock();\r
+         t_decode += (t_stop - t_start);\r
+         t_start = t_stop;\r
+#endif\r
+         png_write_rows(write_ptr, (png_bytepp)&row_buf, 1);\r
+#ifdef PNGTEST_TIMING\r
+         t_stop = (float)clock();\r
+         t_encode += (t_stop - t_start);\r
+         t_start = t_stop;\r
+#endif\r
+#endif /* PNG_WRITE_SUPPORTED */\r
+\r
+#ifndef SINGLE_ROWBUF_ALLOC\r
+         png_debug2(0, "Freeing row buffer (pass %d, y = %ld)", pass, y);\r
+         png_free(read_ptr, row_buf);\r
+         row_buf = NULL;\r
+#endif /* !SINGLE_ROWBUF_ALLOC */\r
+      }\r
+   }\r
+\r
+#ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED\r
+   png_free_data(read_ptr, read_info_ptr, PNG_FREE_UNKN, -1);\r
+#endif\r
+#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED\r
+   png_free_data(write_ptr, write_info_ptr, PNG_FREE_UNKN, -1);\r
+#endif\r
+\r
+   png_debug(0, "Reading and writing end_info data");\r
+\r
+   png_read_end(read_ptr, end_info_ptr);\r
+#ifdef PNG_TEXT_SUPPORTED\r
+   {\r
+      png_textp text_ptr;\r
+      int num_text;\r
+\r
+      if (png_get_text(read_ptr, end_info_ptr, &text_ptr, &num_text) > 0)\r
+      {\r
+         png_debug1(0, "Handling %d iTXt/tEXt/zTXt chunks", num_text);\r
+         png_set_text(write_ptr, write_end_info_ptr, text_ptr, num_text);\r
+      }\r
+   }\r
+#endif\r
+#ifdef PNG_tIME_SUPPORTED\r
+   {\r
+      png_timep mod_time;\r
+\r
+      if (png_get_tIME(read_ptr, end_info_ptr, &mod_time))\r
+      {\r
+         png_set_tIME(write_ptr, write_end_info_ptr, mod_time);\r
+#ifdef PNG_TIME_RFC1123_SUPPORTED\r
+         /* We have to use png_memcpy instead of "=" because the string\r
+            pointed to by png_convert_to_rfc1123() gets free'ed before\r
+            we use it */\r
+         png_memcpy(tIME_string,\r
+                    png_convert_to_rfc1123(read_ptr, mod_time),\r
+                    png_sizeof(tIME_string));\r
+         tIME_string[png_sizeof(tIME_string) - 1] = '\0';\r
+         tIME_chunk_present++;\r
+#endif /* PNG_TIME_RFC1123_SUPPORTED */\r
+      }\r
+   }\r
+#endif\r
+#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED\r
+   {\r
+      png_unknown_chunkp unknowns;\r
+      int num_unknowns;\r
+      num_unknowns = (int)png_get_unknown_chunks(read_ptr, end_info_ptr,\r
+         &unknowns);\r
+      if (num_unknowns)\r
+      {\r
+         png_size_t i;\r
+         png_set_unknown_chunks(write_ptr, write_end_info_ptr, unknowns,\r
+           num_unknowns);\r
+         /* Copy the locations from the read_info_ptr.  The automatically\r
+          * generated locations in write_end_info_ptr are wrong because we\r
+          * haven't written the end_info yet.\r
+          */\r
+         for (i = 0; i < (png_size_t)num_unknowns; i++)\r
+           png_set_unknown_chunk_location(write_ptr, write_end_info_ptr, i,\r
+             unknowns[i].location);\r
+      }\r
+   }\r
+#endif\r
+#ifdef PNG_WRITE_SUPPORTED\r
+   png_write_end(write_ptr, write_end_info_ptr);\r
+#endif\r
+\r
+#ifdef PNG_EASY_ACCESS_SUPPORTED\r
+   if (verbose)\r
+   {\r
+      png_uint_32 iwidth, iheight;\r
+      iwidth = png_get_image_width(write_ptr, write_info_ptr);\r
+      iheight = png_get_image_height(write_ptr, write_info_ptr);\r
+      fprintf(STDERR, "\n Image width = %lu, height = %lu\n",\r
+         (unsigned long)iwidth, (unsigned long)iheight);\r
+   }\r
+#endif\r
+\r
+   png_debug(0, "Destroying data structs");\r
+#ifdef SINGLE_ROWBUF_ALLOC\r
+   png_debug(1, "destroying row_buf for read_ptr");\r
+   png_free(read_ptr, row_buf);\r
+   row_buf = NULL;\r
+#endif /* SINGLE_ROWBUF_ALLOC */\r
+   png_debug(1, "destroying read_ptr, read_info_ptr, end_info_ptr");\r
+   png_destroy_read_struct(&read_ptr, &read_info_ptr, &end_info_ptr);\r
+#ifdef PNG_WRITE_SUPPORTED\r
+   png_debug(1, "destroying write_end_info_ptr");\r
+   png_destroy_info_struct(write_ptr, &write_end_info_ptr);\r
+   png_debug(1, "destroying write_ptr, write_info_ptr");\r
+   png_destroy_write_struct(&write_ptr, &write_info_ptr);\r
+#endif\r
+   png_debug(0, "Destruction complete.");\r
+\r
+   FCLOSE(fpin);\r
+   FCLOSE(fpout);\r
+\r
+   png_debug(0, "Opening files for comparison");\r
+   if ((fpin = fopen(inname, "rb")) == NULL)\r
+   {\r
+      fprintf(STDERR, "Could not find file %s\n", inname);\r
+      return (1);\r
+   }\r
+\r
+   if ((fpout = fopen(outname, "rb")) == NULL)\r
+   {\r
+      fprintf(STDERR, "Could not find file %s\n", outname);\r
+      FCLOSE(fpin);\r
+      return (1);\r
+   }\r
+\r
+   for (;;)\r
+   {\r
+      png_size_t num_in, num_out;\r
+\r
+         num_in = fread(inbuf, 1, 1, fpin);\r
+         num_out = fread(outbuf, 1, 1, fpout);\r
+\r
+      if (num_in != num_out)\r
+      {\r
+         fprintf(STDERR, "\nFiles %s and %s are of a different size\n",\r
+                 inname, outname);\r
+         if (wrote_question == 0)\r
+         {\r
+            fprintf(STDERR,\r
+         "   Was %s written with the same maximum IDAT chunk size (%d bytes),",\r
+              inname, PNG_ZBUF_SIZE);\r
+            fprintf(STDERR,\r
+              "\n   filtering heuristic (libpng default), compression");\r
+            fprintf(STDERR,\r
+              " level (zlib default),\n   and zlib version (%s)?\n\n",\r
+              ZLIB_VERSION);\r
+            wrote_question = 1;\r
+         }\r
+         FCLOSE(fpin);\r
+         FCLOSE(fpout);\r
+         return (0);\r
+      }\r
+\r
+      if (!num_in)\r
+         break;\r
+\r
+      if (png_memcmp(inbuf, outbuf, num_in))\r
+      {\r
+         fprintf(STDERR, "\nFiles %s and %s are different\n", inname, outname);\r
+         if (wrote_question == 0)\r
+         {\r
+            fprintf(STDERR,\r
+         "   Was %s written with the same maximum IDAT chunk size (%d bytes),",\r
+                 inname, PNG_ZBUF_SIZE);\r
+            fprintf(STDERR,\r
+              "\n   filtering heuristic (libpng default), compression");\r
+            fprintf(STDERR,\r
+              " level (zlib default),\n   and zlib version (%s)?\n\n",\r
+              ZLIB_VERSION);\r
+            wrote_question = 1;\r
+         }\r
+         FCLOSE(fpin);\r
+         FCLOSE(fpout);\r
+         return (0);\r
+      }\r
+   }\r
+\r
+   FCLOSE(fpin);\r
+   FCLOSE(fpout);\r
+\r
+   return (0);\r
+}\r
+\r
+/* Input and output filenames */\r
+#ifdef RISCOS\r
+static PNG_CONST char *inname = "pngtest/png";\r
+static PNG_CONST char *outname = "pngout/png";\r
+#else\r
+static PNG_CONST char *inname = "pngtest.png";\r
+static PNG_CONST char *outname = "pngout.png";\r
+#endif\r
+\r
+int\r
+main(int argc, char *argv[])\r
+{\r
+   int multiple = 0;\r
+   int ierror = 0;\r
+\r
+   fprintf(STDERR, "\n Testing libpng version %s\n", PNG_LIBPNG_VER_STRING);\r
+   fprintf(STDERR, "   with zlib   version %s\n", ZLIB_VERSION);\r
+   fprintf(STDERR, "%s", png_get_copyright(NULL));\r
+   /* Show the version of libpng used in building the library */\r
+   fprintf(STDERR, " library (%lu):%s",\r
+      (unsigned long)png_access_version_number(),\r
+      png_get_header_version(NULL));\r
+   /* Show the version of libpng used in building the application */\r
+   fprintf(STDERR, " pngtest (%lu):%s", (unsigned long)PNG_LIBPNG_VER,\r
+      PNG_HEADER_VERSION_STRING);\r
+   fprintf(STDERR, " sizeof(png_struct)=%ld, sizeof(png_info)=%ld\n",\r
+                    (long)png_sizeof(png_struct), (long)png_sizeof(png_info));\r
+\r
+   /* Do some consistency checking on the memory allocation settings, I'm\r
+    * not sure this matters, but it is nice to know, the first of these\r
+    * tests should be impossible because of the way the macros are set\r
+    * in pngconf.h\r
+    */\r
+#if defined(MAXSEG_64K) && !defined(PNG_MAX_MALLOC_64K)\r
+      fprintf(STDERR, " NOTE: Zlib compiled for max 64k, libpng not\n");\r
+#endif\r
+   /* I think the following can happen. */\r
+#if !defined(MAXSEG_64K) && defined(PNG_MAX_MALLOC_64K)\r
+      fprintf(STDERR, " NOTE: libpng compiled for max 64k, zlib not\n");\r
+#endif\r
+\r
+   if (strcmp(png_libpng_ver, PNG_LIBPNG_VER_STRING))\r
+   {\r
+      fprintf(STDERR,\r
+         "Warning: versions are different between png.h and png.c\n");\r
+      fprintf(STDERR, "  png.h version: %s\n", PNG_LIBPNG_VER_STRING);\r
+      fprintf(STDERR, "  png.c version: %s\n\n", png_libpng_ver);\r
+      ++ierror;\r
+   }\r
+\r
+   if (argc > 1)\r
+   {\r
+      if (strcmp(argv[1], "-m") == 0)\r
+      {\r
+         multiple = 1;\r
+         status_dots_requested = 0;\r
+      }\r
+      else if (strcmp(argv[1], "-mv") == 0 ||\r
+               strcmp(argv[1], "-vm") == 0 )\r
+      {\r
+         multiple = 1;\r
+         verbose = 1;\r
+         status_dots_requested = 1;\r
+      }\r
+      else if (strcmp(argv[1], "-v") == 0)\r
+      {\r
+         verbose = 1;\r
+         status_dots_requested = 1;\r
+         inname = argv[2];\r
+      }\r
+      else\r
+      {\r
+         inname = argv[1];\r
+         status_dots_requested = 0;\r
+      }\r
+   }\r
+\r
+   if (!multiple && argc == 3 + verbose)\r
+     outname = argv[2 + verbose];\r
+\r
+   if ((!multiple && argc > 3 + verbose) || (multiple && argc < 2))\r
+   {\r
+     fprintf(STDERR,\r
+       "usage: %s [infile.png] [outfile.png]\n\t%s -m {infile.png}\n",\r
+        argv[0], argv[0]);\r
+     fprintf(STDERR,\r
+       "  reads/writes one PNG file (without -m) or multiple files (-m)\n");\r
+     fprintf(STDERR,\r
+       "  with -m %s is used as a temporary file\n", outname);\r
+     exit(1);\r
+   }\r
+\r
+   if (multiple)\r
+   {\r
+      int i;\r
+#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG\r
+      int allocation_now = current_allocation;\r
+#endif\r
+      for (i=2; i<argc; ++i)\r
+      {\r
+         int kerror;\r
+         fprintf(STDERR, "\n Testing %s:", argv[i]);\r
+         kerror = test_one_file(argv[i], outname);\r
+         if (kerror == 0)\r
+         {\r
+#ifdef PNG_READ_USER_TRANSFORM_SUPPORTED\r
+            int k;\r
+#endif\r
+#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED\r
+            fprintf(STDERR, "\n PASS (%lu zero samples)\n",\r
+               (unsigned long)zero_samples);\r
+#else\r
+            fprintf(STDERR, " PASS\n");\r
+#endif\r
+#ifdef PNG_READ_USER_TRANSFORM_SUPPORTED\r
+            for (k = 0; k<256; k++)\r
+               if (filters_used[k])\r
+                  fprintf(STDERR, " Filter %d was used %lu times\n",\r
+                     k, (unsigned long)filters_used[k]);\r
+#endif\r
+#ifdef PNG_TIME_RFC1123_SUPPORTED\r
+         if (tIME_chunk_present != 0)\r
+            fprintf(STDERR, " tIME = %s\n", tIME_string);\r
+         tIME_chunk_present = 0;\r
+#endif /* PNG_TIME_RFC1123_SUPPORTED */\r
+         }\r
+         else\r
+         {\r
+            fprintf(STDERR, " FAIL\n");\r
+            ierror += kerror;\r
+         }\r
+#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG\r
+         if (allocation_now != current_allocation)\r
+            fprintf(STDERR, "MEMORY ERROR: %d bytes lost\n",\r
+               current_allocation - allocation_now);\r
+         if (current_allocation != 0)\r
+         {\r
+            memory_infop pinfo = pinformation;\r
+\r
+            fprintf(STDERR, "MEMORY ERROR: %d bytes still allocated\n",\r
+               current_allocation);\r
+            while (pinfo != NULL)\r
+            {\r
+               fprintf(STDERR, " %lu bytes at %x\n",\r
+                 (unsigned long)pinfo->size,\r
+                 (unsigned int) pinfo->pointer);\r
+               pinfo = pinfo->next;\r
+            }\r
+         }\r
+#endif\r
+      }\r
+#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG\r
+         fprintf(STDERR, " Current memory allocation: %10d bytes\n",\r
+            current_allocation);\r
+         fprintf(STDERR, " Maximum memory allocation: %10d bytes\n",\r
+            maximum_allocation);\r
+         fprintf(STDERR, " Total   memory allocation: %10d bytes\n",\r
+            total_allocation);\r
+         fprintf(STDERR, "     Number of allocations: %10d\n",\r
+            num_allocations);\r
+#endif\r
+   }\r
+   else\r
+   {\r
+      int i;\r
+      for (i = 0; i<3; ++i)\r
+      {\r
+         int kerror;\r
+#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG\r
+         int allocation_now = current_allocation;\r
+#endif\r
+         if (i == 1) status_dots_requested = 1;\r
+         else if (verbose == 0)status_dots_requested = 0;\r
+         if (i == 0 || verbose == 1 || ierror != 0)\r
+            fprintf(STDERR, "\n Testing %s:", inname);\r
+         kerror = test_one_file(inname, outname);\r
+         if (kerror == 0)\r
+         {\r
+            if (verbose == 1 || i == 2)\r
+            {\r
+#ifdef PNG_READ_USER_TRANSFORM_SUPPORTED\r
+                int k;\r
+#endif\r
+#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED\r
+                fprintf(STDERR, "\n PASS (%lu zero samples)\n",\r
+                   (unsigned long)zero_samples);\r
+#else\r
+                fprintf(STDERR, " PASS\n");\r
+#endif\r
+#ifdef PNG_READ_USER_TRANSFORM_SUPPORTED\r
+                for (k = 0; k<256; k++)\r
+                   if (filters_used[k])\r
+                      fprintf(STDERR, " Filter %d was used %lu times\n",\r
+                         k, (unsigned long)filters_used[k]);\r
+#endif\r
+#ifdef PNG_TIME_RFC1123_SUPPORTED\r
+             if (tIME_chunk_present != 0)\r
+                fprintf(STDERR, " tIME = %s\n", tIME_string);\r
+#endif /* PNG_TIME_RFC1123_SUPPORTED */\r
+            }\r
+         }\r
+         else\r
+         {\r
+            if (verbose == 0 && i != 2)\r
+               fprintf(STDERR, "\n Testing %s:", inname);\r
+            fprintf(STDERR, " FAIL\n");\r
+            ierror += kerror;\r
+         }\r
+#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG\r
+         if (allocation_now != current_allocation)\r
+             fprintf(STDERR, "MEMORY ERROR: %d bytes lost\n",\r
+               current_allocation - allocation_now);\r
+         if (current_allocation != 0)\r
+         {\r
+             memory_infop pinfo = pinformation;\r
+\r
+             fprintf(STDERR, "MEMORY ERROR: %d bytes still allocated\n",\r
+                current_allocation);\r
+             while (pinfo != NULL)\r
+             {\r
+                fprintf(STDERR, " %lu bytes at %x\n",\r
+                   (unsigned long)pinfo->size, (unsigned int)pinfo->pointer);\r
+                pinfo = pinfo->next;\r
+             }\r
+          }\r
+#endif\r
+       }\r
+#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG\r
+       fprintf(STDERR, " Current memory allocation: %10d bytes\n",\r
+          current_allocation);\r
+       fprintf(STDERR, " Maximum memory allocation: %10d bytes\n",\r
+          maximum_allocation);\r
+       fprintf(STDERR, " Total   memory allocation: %10d bytes\n",\r
+          total_allocation);\r
+       fprintf(STDERR, "     Number of allocations: %10d\n",\r
+            num_allocations);\r
+#endif\r
+   }\r
+\r
+#ifdef PNGTEST_TIMING\r
+   t_stop = (float)clock();\r
+   t_misc += (t_stop - t_start);\r
+   t_start = t_stop;\r
+   fprintf(STDERR, " CPU time used = %.3f seconds",\r
+      (t_misc+t_decode+t_encode)/(float)CLOCKS_PER_SEC);\r
+   fprintf(STDERR, " (decoding %.3f,\n",\r
+      t_decode/(float)CLOCKS_PER_SEC);\r
+   fprintf(STDERR, "        encoding %.3f ,",\r
+      t_encode/(float)CLOCKS_PER_SEC);\r
+   fprintf(STDERR, " other %.3f seconds)\n\n",\r
+      t_misc/(float)CLOCKS_PER_SEC);\r
+#endif\r
+\r
+   if (ierror == 0)\r
+      fprintf(STDERR, " libpng passes test\n");\r
+   else\r
+      fprintf(STDERR, " libpng FAILS test\n");\r
+   return (int)(ierror != 0);\r
+}\r
+\r
+/* Generate a compiler error if there is an old png.h in the search path. */\r
+typedef version_1_4_3 your_png_h_is_not_version_1_4_3;\r
diff --git a/3rdparty/libpng/pngtest.png b/3rdparty/libpng/pngtest.png
new file mode 100644 (file)
index 0000000..cfdd36f
Binary files /dev/null and b/3rdparty/libpng/pngtest.png differ
index 1640095..d604a7a 100644 (file)
-
-/* pngtrans.c - transforms the data in a row (used by both readers and writers)
- *
- * Last changed in libpng 1.2.17 May 15, 2007
- * For conditions of distribution and use, see copyright notice in png.h
- * Copyright (c) 1998-2007 Glenn Randers-Pehrson
- * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
- * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
- */
-
-#define PNG_INTERNAL
-#include "png.h"
-
-#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
-#if defined(PNG_READ_BGR_SUPPORTED) || defined(PNG_WRITE_BGR_SUPPORTED)
-/* turn on BGR-to-RGB mapping */
-void PNGAPI
-png_set_bgr(png_structp png_ptr)
-{
-   png_debug(1, "in png_set_bgr\n");
-   if(png_ptr == NULL) return;
-   png_ptr->transformations |= PNG_BGR;
-}
-#endif
-
-#if defined(PNG_READ_SWAP_SUPPORTED) || defined(PNG_WRITE_SWAP_SUPPORTED)
-/* turn on 16 bit byte swapping */
-void PNGAPI
-png_set_swap(png_structp png_ptr)
-{
-   png_debug(1, "in png_set_swap\n");
-   if(png_ptr == NULL) return;
-   if (png_ptr->bit_depth == 16)
-      png_ptr->transformations |= PNG_SWAP_BYTES;
-}
-#endif
-
-#if defined(PNG_READ_PACK_SUPPORTED) || defined(PNG_WRITE_PACK_SUPPORTED)
-/* turn on pixel packing */
-void PNGAPI
-png_set_packing(png_structp png_ptr)
-{
-   png_debug(1, "in png_set_packing\n");
-   if(png_ptr == NULL) return;
-   if (png_ptr->bit_depth < 8)
-   {
-      png_ptr->transformations |= PNG_PACK;
-      png_ptr->usr_bit_depth = 8;
-   }
-}
-#endif
-
-#if defined(PNG_READ_PACKSWAP_SUPPORTED)||defined(PNG_WRITE_PACKSWAP_SUPPORTED)
-/* turn on packed pixel swapping */
-void PNGAPI
-png_set_packswap(png_structp png_ptr)
-{
-   png_debug(1, "in png_set_packswap\n");
-   if(png_ptr == NULL) return;
-   if (png_ptr->bit_depth < 8)
-      png_ptr->transformations |= PNG_PACKSWAP;
-}
-#endif
-
-#if defined(PNG_READ_SHIFT_SUPPORTED) || defined(PNG_WRITE_SHIFT_SUPPORTED)
-void PNGAPI
-png_set_shift(png_structp png_ptr, png_color_8p true_bits)
-{
-   png_debug(1, "in png_set_shift\n");
-   if(png_ptr == NULL) return;
-   png_ptr->transformations |= PNG_SHIFT;
-   png_ptr->shift = *true_bits;
-}
-#endif
-
-#if defined(PNG_READ_INTERLACING_SUPPORTED) || \
-    defined(PNG_WRITE_INTERLACING_SUPPORTED)
-int PNGAPI
-png_set_interlace_handling(png_structp png_ptr)
-{
-   png_debug(1, "in png_set_interlace handling\n");
-   if (png_ptr && png_ptr->interlaced)
-   {
-      png_ptr->transformations |= PNG_INTERLACE;
-      return (7);
-   }
-
-   return (1);
-}
-#endif
-
-#if defined(PNG_READ_FILLER_SUPPORTED) || defined(PNG_WRITE_FILLER_SUPPORTED)
-/* Add a filler byte on read, or remove a filler or alpha byte on write.
- * The filler type has changed in v0.95 to allow future 2-byte fillers
- * for 48-bit input data, as well as to avoid problems with some compilers
- * that don't like bytes as parameters.
- */
-void PNGAPI
-png_set_filler(png_structp png_ptr, png_uint_32 filler, int filler_loc)
-{
-   png_debug(1, "in png_set_filler\n");
-   if(png_ptr == NULL) return;
-   png_ptr->transformations |= PNG_FILLER;
-   png_ptr->filler = (png_byte)filler;
-   if (filler_loc == PNG_FILLER_AFTER)
-      png_ptr->flags |= PNG_FLAG_FILLER_AFTER;
-   else
-      png_ptr->flags &= ~PNG_FLAG_FILLER_AFTER;
-
-   /* This should probably go in the "do_read_filler" routine.
-    * I attempted to do that in libpng-1.0.1a but that caused problems
-    * so I restored it in libpng-1.0.2a
-   */
-
-   if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
-   {
-      png_ptr->usr_channels = 4;
-   }
-
-   /* Also I added this in libpng-1.0.2a (what happens when we expand
-    * a less-than-8-bit grayscale to GA? */
-
-   if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY && png_ptr->bit_depth >= 8)
-   {
-      png_ptr->usr_channels = 2;
-   }
-}
-
-#if !defined(PNG_1_0_X)
-/* Added to libpng-1.2.7 */
-void PNGAPI
-png_set_add_alpha(png_structp png_ptr, png_uint_32 filler, int filler_loc)
-{
-   png_debug(1, "in png_set_add_alpha\n");
-   if(png_ptr == NULL) return;
-   png_set_filler(png_ptr, filler, filler_loc);
-   png_ptr->transformations |= PNG_ADD_ALPHA;
-}
-#endif
-
-#endif
-
-#if defined(PNG_READ_SWAP_ALPHA_SUPPORTED) || \
-    defined(PNG_WRITE_SWAP_ALPHA_SUPPORTED)
-void PNGAPI
-png_set_swap_alpha(png_structp png_ptr)
-{
-   png_debug(1, "in png_set_swap_alpha\n");
-   if(png_ptr == NULL) return;
-   png_ptr->transformations |= PNG_SWAP_ALPHA;
-}
-#endif
-
-#if defined(PNG_READ_INVERT_ALPHA_SUPPORTED) || \
-    defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED)
-void PNGAPI
-png_set_invert_alpha(png_structp png_ptr)
-{
-   png_debug(1, "in png_set_invert_alpha\n");
-   if(png_ptr == NULL) return;
-   png_ptr->transformations |= PNG_INVERT_ALPHA;
-}
-#endif
-
-#if defined(PNG_READ_INVERT_SUPPORTED) || defined(PNG_WRITE_INVERT_SUPPORTED)
-void PNGAPI
-png_set_invert_mono(png_structp png_ptr)
-{
-   png_debug(1, "in png_set_invert_mono\n");
-   if(png_ptr == NULL) return;
-   png_ptr->transformations |= PNG_INVERT_MONO;
-}
-
-/* invert monochrome grayscale data */
-void /* PRIVATE */
-png_do_invert(png_row_infop row_info, png_bytep row)
-{
-   png_debug(1, "in png_do_invert\n");
-  /* This test removed from libpng version 1.0.13 and 1.2.0:
-   *   if (row_info->bit_depth == 1 &&
-   */
-#if defined(PNG_USELESS_TESTS_SUPPORTED)
-   if (row == NULL || row_info == NULL)
-     return;
-#endif
-   if (row_info->color_type == PNG_COLOR_TYPE_GRAY)
-   {
-      png_bytep rp = row;
-      png_uint_32 i;
-      png_uint_32 istop = row_info->rowbytes;
-
-      for (i = 0; i < istop; i++)
-      {
-         *rp = (png_byte)(~(*rp));
-         rp++;
-      }
-   }
-   else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA &&
-      row_info->bit_depth == 8)
-   {
-      png_bytep rp = row;
-      png_uint_32 i;
-      png_uint_32 istop = row_info->rowbytes;
-
-      for (i = 0; i < istop; i+=2)
-      {
-         *rp = (png_byte)(~(*rp));
-         rp+=2;
-      }
-   }
-   else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA &&
-      row_info->bit_depth == 16)
-   {
-      png_bytep rp = row;
-      png_uint_32 i;
-      png_uint_32 istop = row_info->rowbytes;
-
-      for (i = 0; i < istop; i+=4)
-      {
-         *rp = (png_byte)(~(*rp));
-         *(rp+1) = (png_byte)(~(*(rp+1)));
-         rp+=4;
-      }
-   }
-}
-#endif
-
-#if defined(PNG_READ_SWAP_SUPPORTED) || defined(PNG_WRITE_SWAP_SUPPORTED)
-/* swaps byte order on 16 bit depth images */
-void /* PRIVATE */
-png_do_swap(png_row_infop row_info, png_bytep row)
-{
-   png_debug(1, "in png_do_swap\n");
-   if (
-#if defined(PNG_USELESS_TESTS_SUPPORTED)
-       row != NULL && row_info != NULL &&
-#endif
-       row_info->bit_depth == 16)
-   {
-      png_bytep rp = row;
-      png_uint_32 i;
-      png_uint_32 istop= row_info->width * row_info->channels;
-
-      for (i = 0; i < istop; i++, rp += 2)
-      {
-         png_byte t = *rp;
-         *rp = *(rp + 1);
-         *(rp + 1) = t;
-      }
-   }
-}
-#endif
-
-#if defined(PNG_READ_PACKSWAP_SUPPORTED)||defined(PNG_WRITE_PACKSWAP_SUPPORTED)
-static PNG_CONST png_byte onebppswaptable[256] = {
-   0x00, 0x80, 0x40, 0xC0, 0x20, 0xA0, 0x60, 0xE0,
-   0x10, 0x90, 0x50, 0xD0, 0x30, 0xB0, 0x70, 0xF0,
-   0x08, 0x88, 0x48, 0xC8, 0x28, 0xA8, 0x68, 0xE8,
-   0x18, 0x98, 0x58, 0xD8, 0x38, 0xB8, 0x78, 0xF8,
-   0x04, 0x84, 0x44, 0xC4, 0x24, 0xA4, 0x64, 0xE4,
-   0x14, 0x94, 0x54, 0xD4, 0x34, 0xB4, 0x74, 0xF4,
-   0x0C, 0x8C, 0x4C, 0xCC, 0x2C, 0xAC, 0x6C, 0xEC,
-   0x1C, 0x9C, 0x5C, 0xDC, 0x3C, 0xBC, 0x7C, 0xFC,
-   0x02, 0x82, 0x42, 0xC2, 0x22, 0xA2, 0x62, 0xE2,
-   0x12, 0x92, 0x52, 0xD2, 0x32, 0xB2, 0x72, 0xF2,
-   0x0A, 0x8A, 0x4A, 0xCA, 0x2A, 0xAA, 0x6A, 0xEA,
-   0x1A, 0x9A, 0x5A, 0xDA, 0x3A, 0xBA, 0x7A, 0xFA,
-   0x06, 0x86, 0x46, 0xC6, 0x26, 0xA6, 0x66, 0xE6,
-   0x16, 0x96, 0x56, 0xD6, 0x36, 0xB6, 0x76, 0xF6,
-   0x0E, 0x8E, 0x4E, 0xCE, 0x2E, 0xAE, 0x6E, 0xEE,
-   0x1E, 0x9E, 0x5E, 0xDE, 0x3E, 0xBE, 0x7E, 0xFE,
-   0x01, 0x81, 0x41, 0xC1, 0x21, 0xA1, 0x61, 0xE1,
-   0x11, 0x91, 0x51, 0xD1, 0x31, 0xB1, 0x71, 0xF1,
-   0x09, 0x89, 0x49, 0xC9, 0x29, 0xA9, 0x69, 0xE9,
-   0x19, 0x99, 0x59, 0xD9, 0x39, 0xB9, 0x79, 0xF9,
-   0x05, 0x85, 0x45, 0xC5, 0x25, 0xA5, 0x65, 0xE5,
-   0x15, 0x95, 0x55, 0xD5, 0x35, 0xB5, 0x75, 0xF5,
-   0x0D, 0x8D, 0x4D, 0xCD, 0x2D, 0xAD, 0x6D, 0xED,
-   0x1D, 0x9D, 0x5D, 0xDD, 0x3D, 0xBD, 0x7D, 0xFD,
-   0x03, 0x83, 0x43, 0xC3, 0x23, 0xA3, 0x63, 0xE3,
-   0x13, 0x93, 0x53, 0xD3, 0x33, 0xB3, 0x73, 0xF3,
-   0x0B, 0x8B, 0x4B, 0xCB, 0x2B, 0xAB, 0x6B, 0xEB,
-   0x1B, 0x9B, 0x5B, 0xDB, 0x3B, 0xBB, 0x7B, 0xFB,
-   0x07, 0x87, 0x47, 0xC7, 0x27, 0xA7, 0x67, 0xE7,
-   0x17, 0x97, 0x57, 0xD7, 0x37, 0xB7, 0x77, 0xF7,
-   0x0F, 0x8F, 0x4F, 0xCF, 0x2F, 0xAF, 0x6F, 0xEF,
-   0x1F, 0x9F, 0x5F, 0xDF, 0x3F, 0xBF, 0x7F, 0xFF
-};
-
-static PNG_CONST png_byte twobppswaptable[256] = {
-   0x00, 0x40, 0x80, 0xC0, 0x10, 0x50, 0x90, 0xD0,
-   0x20, 0x60, 0xA0, 0xE0, 0x30, 0x70, 0xB0, 0xF0,
-   0x04, 0x44, 0x84, 0xC4, 0x14, 0x54, 0x94, 0xD4,
-   0x24, 0x64, 0xA4, 0xE4, 0x34, 0x74, 0xB4, 0xF4,
-   0x08, 0x48, 0x88, 0xC8, 0x18, 0x58, 0x98, 0xD8,
-   0x28, 0x68, 0xA8, 0xE8, 0x38, 0x78, 0xB8, 0xF8,
-   0x0C, 0x4C, 0x8C, 0xCC, 0x1C, 0x5C, 0x9C, 0xDC,
-   0x2C, 0x6C, 0xAC, 0xEC, 0x3C, 0x7C, 0xBC, 0xFC,
-   0x01, 0x41, 0x81, 0xC1, 0x11, 0x51, 0x91, 0xD1,
-   0x21, 0x61, 0xA1, 0xE1, 0x31, 0x71, 0xB1, 0xF1,
-   0x05, 0x45, 0x85, 0xC5, 0x15, 0x55, 0x95, 0xD5,
-   0x25, 0x65, 0xA5, 0xE5, 0x35, 0x75, 0xB5, 0xF5,
-   0x09, 0x49, 0x89, 0xC9, 0x19, 0x59, 0x99, 0xD9,
-   0x29, 0x69, 0xA9, 0xE9, 0x39, 0x79, 0xB9, 0xF9,
-   0x0D, 0x4D, 0x8D, 0xCD, 0x1D, 0x5D, 0x9D, 0xDD,
-   0x2D, 0x6D, 0xAD, 0xED, 0x3D, 0x7D, 0xBD, 0xFD,
-   0x02, 0x42, 0x82, 0xC2, 0x12, 0x52, 0x92, 0xD2,
-   0x22, 0x62, 0xA2, 0xE2, 0x32, 0x72, 0xB2, 0xF2,
-   0x06, 0x46, 0x86, 0xC6, 0x16, 0x56, 0x96, 0xD6,
-   0x26, 0x66, 0xA6, 0xE6, 0x36, 0x76, 0xB6, 0xF6,
-   0x0A, 0x4A, 0x8A, 0xCA, 0x1A, 0x5A, 0x9A, 0xDA,
-   0x2A, 0x6A, 0xAA, 0xEA, 0x3A, 0x7A, 0xBA, 0xFA,
-   0x0E, 0x4E, 0x8E, 0xCE, 0x1E, 0x5E, 0x9E, 0xDE,
-   0x2E, 0x6E, 0xAE, 0xEE, 0x3E, 0x7E, 0xBE, 0xFE,
-   0x03, 0x43, 0x83, 0xC3, 0x13, 0x53, 0x93, 0xD3,
-   0x23, 0x63, 0xA3, 0xE3, 0x33, 0x73, 0xB3, 0xF3,
-   0x07, 0x47, 0x87, 0xC7, 0x17, 0x57, 0x97, 0xD7,
-   0x27, 0x67, 0xA7, 0xE7, 0x37, 0x77, 0xB7, 0xF7,
-   0x0B, 0x4B, 0x8B, 0xCB, 0x1B, 0x5B, 0x9B, 0xDB,
-   0x2B, 0x6B, 0xAB, 0xEB, 0x3B, 0x7B, 0xBB, 0xFB,
-   0x0F, 0x4F, 0x8F, 0xCF, 0x1F, 0x5F, 0x9F, 0xDF,
-   0x2F, 0x6F, 0xAF, 0xEF, 0x3F, 0x7F, 0xBF, 0xFF
-};
-
-static PNG_CONST png_byte fourbppswaptable[256] = {
-   0x00, 0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70,
-   0x80, 0x90, 0xA0, 0xB0, 0xC0, 0xD0, 0xE0, 0xF0,
-   0x01, 0x11, 0x21, 0x31, 0x41, 0x51, 0x61, 0x71,
-   0x81, 0x91, 0xA1, 0xB1, 0xC1, 0xD1, 0xE1, 0xF1,
-   0x02, 0x12, 0x22, 0x32, 0x42, 0x52, 0x62, 0x72,
-   0x82, 0x92, 0xA2, 0xB2, 0xC2, 0xD2, 0xE2, 0xF2,
-   0x03, 0x13, 0x23, 0x33, 0x43, 0x53, 0x63, 0x73,
-   0x83, 0x93, 0xA3, 0xB3, 0xC3, 0xD3, 0xE3, 0xF3,
-   0x04, 0x14, 0x24, 0x34, 0x44, 0x54, 0x64, 0x74,
-   0x84, 0x94, 0xA4, 0xB4, 0xC4, 0xD4, 0xE4, 0xF4,
-   0x05, 0x15, 0x25, 0x35, 0x45, 0x55, 0x65, 0x75,
-   0x85, 0x95, 0xA5, 0xB5, 0xC5, 0xD5, 0xE5, 0xF5,
-   0x06, 0x16, 0x26, 0x36, 0x46, 0x56, 0x66, 0x76,
-   0x86, 0x96, 0xA6, 0xB6, 0xC6, 0xD6, 0xE6, 0xF6,
-   0x07, 0x17, 0x27, 0x37, 0x47, 0x57, 0x67, 0x77,
-   0x87, 0x97, 0xA7, 0xB7, 0xC7, 0xD7, 0xE7, 0xF7,
-   0x08, 0x18, 0x28, 0x38, 0x48, 0x58, 0x68, 0x78,
-   0x88, 0x98, 0xA8, 0xB8, 0xC8, 0xD8, 0xE8, 0xF8,
-   0x09, 0x19, 0x29, 0x39, 0x49, 0x59, 0x69, 0x79,
-   0x89, 0x99, 0xA9, 0xB9, 0xC9, 0xD9, 0xE9, 0xF9,
-   0x0A, 0x1A, 0x2A, 0x3A, 0x4A, 0x5A, 0x6A, 0x7A,
-   0x8A, 0x9A, 0xAA, 0xBA, 0xCA, 0xDA, 0xEA, 0xFA,
-   0x0B, 0x1B, 0x2B, 0x3B, 0x4B, 0x5B, 0x6B, 0x7B,
-   0x8B, 0x9B, 0xAB, 0xBB, 0xCB, 0xDB, 0xEB, 0xFB,
-   0x0C, 0x1C, 0x2C, 0x3C, 0x4C, 0x5C, 0x6C, 0x7C,
-   0x8C, 0x9C, 0xAC, 0xBC, 0xCC, 0xDC, 0xEC, 0xFC,
-   0x0D, 0x1D, 0x2D, 0x3D, 0x4D, 0x5D, 0x6D, 0x7D,
-   0x8D, 0x9D, 0xAD, 0xBD, 0xCD, 0xDD, 0xED, 0xFD,
-   0x0E, 0x1E, 0x2E, 0x3E, 0x4E, 0x5E, 0x6E, 0x7E,
-   0x8E, 0x9E, 0xAE, 0xBE, 0xCE, 0xDE, 0xEE, 0xFE,
-   0x0F, 0x1F, 0x2F, 0x3F, 0x4F, 0x5F, 0x6F, 0x7F,
-   0x8F, 0x9F, 0xAF, 0xBF, 0xCF, 0xDF, 0xEF, 0xFF
-};
-
-/* swaps pixel packing order within bytes */
-void /* PRIVATE */
-png_do_packswap(png_row_infop row_info, png_bytep row)
-{
-   png_debug(1, "in png_do_packswap\n");
-   if (
-#if defined(PNG_USELESS_TESTS_SUPPORTED)
-       row != NULL && row_info != NULL &&
-#endif
-       row_info->bit_depth < 8)
-   {
-      png_bytep rp, end, table;
-
-      end = row + row_info->rowbytes;
-
-      if (row_info->bit_depth == 1)
-         table = (png_bytep)onebppswaptable;
-      else if (row_info->bit_depth == 2)
-         table = (png_bytep)twobppswaptable;
-      else if (row_info->bit_depth == 4)
-         table = (png_bytep)fourbppswaptable;
-      else
-         return;
-
-      for (rp = row; rp < end; rp++)
-         *rp = table[*rp];
-   }
-}
-#endif /* PNG_READ_PACKSWAP_SUPPORTED or PNG_WRITE_PACKSWAP_SUPPORTED */
-
-#if defined(PNG_WRITE_FILLER_SUPPORTED) || \
-    defined(PNG_READ_STRIP_ALPHA_SUPPORTED)
-/* remove filler or alpha byte(s) */
-void /* PRIVATE */
-png_do_strip_filler(png_row_infop row_info, png_bytep row, png_uint_32 flags)
-{
-   png_debug(1, "in png_do_strip_filler\n");
-#if defined(PNG_USELESS_TESTS_SUPPORTED)
-   if (row != NULL && row_info != NULL)
-#endif
-   {
-      png_bytep sp=row;
-      png_bytep dp=row;
-      png_uint_32 row_width=row_info->width;
-      png_uint_32 i;
-
-      if ((row_info->color_type == PNG_COLOR_TYPE_RGB ||
-         (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA &&
-         (flags & PNG_FLAG_STRIP_ALPHA))) &&
-         row_info->channels == 4)
-      {
-         if (row_info->bit_depth == 8)
-         {
-            /* This converts from RGBX or RGBA to RGB */
-            if (flags & PNG_FLAG_FILLER_AFTER)
-            {
-               dp+=3; sp+=4;
-               for (i = 1; i < row_width; i++)
-               {
-                  *dp++ = *sp++;
-                  *dp++ = *sp++;
-                  *dp++ = *sp++;
-                  sp++;
-               }
-            }
-            /* This converts from XRGB or ARGB to RGB */
-            else
-            {
-               for (i = 0; i < row_width; i++)
-               {
-                  sp++;
-                  *dp++ = *sp++;
-                  *dp++ = *sp++;
-                  *dp++ = *sp++;
-               }
-            }
-            row_info->pixel_depth = 24;
-            row_info->rowbytes = row_width * 3;
-         }
-         else /* if (row_info->bit_depth == 16) */
-         {
-            if (flags & PNG_FLAG_FILLER_AFTER)
-            {
-               /* This converts from RRGGBBXX or RRGGBBAA to RRGGBB */
-               sp += 8; dp += 6;
-               for (i = 1; i < row_width; i++)
-               {
-                  /* This could be (although png_memcpy is probably slower):
-                  png_memcpy(dp, sp, 6);
-                  sp += 8;
-                  dp += 6;
-                  */
-
-                  *dp++ = *sp++;
-                  *dp++ = *sp++;
-                  *dp++ = *sp++;
-                  *dp++ = *sp++;
-                  *dp++ = *sp++;
-                  *dp++ = *sp++;
-                  sp += 2;
-               }
-            }
-            else
-            {
-               /* This converts from XXRRGGBB or AARRGGBB to RRGGBB */
-               for (i = 0; i < row_width; i++)
-               {
-                  /* This could be (although png_memcpy is probably slower):
-                  png_memcpy(dp, sp, 6);
-                  sp += 8;
-                  dp += 6;
-                  */
-
-                  sp+=2;
-                  *dp++ = *sp++;
-                  *dp++ = *sp++;
-                  *dp++ = *sp++;
-                  *dp++ = *sp++;
-                  *dp++ = *sp++;
-                  *dp++ = *sp++;
-               }
-            }
-            row_info->pixel_depth = 48;
-            row_info->rowbytes = row_width * 6;
-         }
-         row_info->channels = 3;
-      }
-      else if ((row_info->color_type == PNG_COLOR_TYPE_GRAY ||
-         (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA &&
-         (flags & PNG_FLAG_STRIP_ALPHA))) &&
-          row_info->channels == 2)
-      {
-         if (row_info->bit_depth == 8)
-         {
-            /* This converts from GX or GA to G */
-            if (flags & PNG_FLAG_FILLER_AFTER)
-            {
-               for (i = 0; i < row_width; i++)
-               {
-                  *dp++ = *sp++;
-                  sp++;
-               }
-            }
-            /* This converts from XG or AG to G */
-            else
-            {
-               for (i = 0; i < row_width; i++)
-               {
-                  sp++;
-                  *dp++ = *sp++;
-               }
-            }
-            row_info->pixel_depth = 8;
-            row_info->rowbytes = row_width;
-         }
-         else /* if (row_info->bit_depth == 16) */
-         {
-            if (flags & PNG_FLAG_FILLER_AFTER)
-            {
-               /* This converts from GGXX or GGAA to GG */
-               sp += 4; dp += 2;
-               for (i = 1; i < row_width; i++)
-               {
-                  *dp++ = *sp++;
-                  *dp++ = *sp++;
-                  sp += 2;
-               }
-            }
-            else
-            {
-               /* This converts from XXGG or AAGG to GG */
-               for (i = 0; i < row_width; i++)
-               {
-                  sp += 2;
-                  *dp++ = *sp++;
-                  *dp++ = *sp++;
-               }
-            }
-            row_info->pixel_depth = 16;
-            row_info->rowbytes = row_width * 2;
-         }
-         row_info->channels = 1;
-      }
-      if (flags & PNG_FLAG_STRIP_ALPHA)
-        row_info->color_type &= ~PNG_COLOR_MASK_ALPHA;
-   }
-}
-#endif
-
-#if defined(PNG_READ_BGR_SUPPORTED) || defined(PNG_WRITE_BGR_SUPPORTED)
-/* swaps red and blue bytes within a pixel */
-void /* PRIVATE */
-png_do_bgr(png_row_infop row_info, png_bytep row)
-{
-   png_debug(1, "in png_do_bgr\n");
-   if (
-#if defined(PNG_USELESS_TESTS_SUPPORTED)
-       row != NULL && row_info != NULL &&
-#endif
-       (row_info->color_type & PNG_COLOR_MASK_COLOR))
-   {
-      png_uint_32 row_width = row_info->width;
-      if (row_info->bit_depth == 8)
-      {
-         if (row_info->color_type == PNG_COLOR_TYPE_RGB)
-         {
-            png_bytep rp;
-            png_uint_32 i;
-
-            for (i = 0, rp = row; i < row_width; i++, rp += 3)
-            {
-               png_byte save = *rp;
-               *rp = *(rp + 2);
-               *(rp + 2) = save;
-            }
-         }
-         else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
-         {
-            png_bytep rp;
-            png_uint_32 i;
-
-            for (i = 0, rp = row; i < row_width; i++, rp += 4)
-            {
-               png_byte save = *rp;
-               *rp = *(rp + 2);
-               *(rp + 2) = save;
-            }
-         }
-      }
-      else if (row_info->bit_depth == 16)
-      {
-         if (row_info->color_type == PNG_COLOR_TYPE_RGB)
-         {
-            png_bytep rp;
-            png_uint_32 i;
-
-            for (i = 0, rp = row; i < row_width; i++, rp += 6)
-            {
-               png_byte save = *rp;
-               *rp = *(rp + 4);
-               *(rp + 4) = save;
-               save = *(rp + 1);
-               *(rp + 1) = *(rp + 5);
-               *(rp + 5) = save;
-            }
-         }
-         else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
-         {
-            png_bytep rp;
-            png_uint_32 i;
-
-            for (i = 0, rp = row; i < row_width; i++, rp += 8)
-            {
-               png_byte save = *rp;
-               *rp = *(rp + 4);
-               *(rp + 4) = save;
-               save = *(rp + 1);
-               *(rp + 1) = *(rp + 5);
-               *(rp + 5) = save;
-            }
-         }
-      }
-   }
-}
-#endif /* PNG_READ_BGR_SUPPORTED or PNG_WRITE_BGR_SUPPORTED */
-
-#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \
-    defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED) || \
-    defined(PNG_LEGACY_SUPPORTED)
-void PNGAPI
-png_set_user_transform_info(png_structp png_ptr, png_voidp
-   user_transform_ptr, int user_transform_depth, int user_transform_channels)
-{
-   png_debug(1, "in png_set_user_transform_info\n");
-   if(png_ptr == NULL) return;
-#if defined(PNG_USER_TRANSFORM_PTR_SUPPORTED)
-   png_ptr->user_transform_ptr = user_transform_ptr;
-   png_ptr->user_transform_depth = (png_byte)user_transform_depth;
-   png_ptr->user_transform_channels = (png_byte)user_transform_channels;
-#else
-   if(user_transform_ptr || user_transform_depth || user_transform_channels)
-      png_warning(png_ptr,
-        "This version of libpng does not support user transform info");
-#endif
-}
-#endif
-
-/* This function returns a pointer to the user_transform_ptr associated with
- * the user transform functions.  The application should free any memory
- * associated with this pointer before png_write_destroy and png_read_destroy
- * are called.
- */
-png_voidp PNGAPI
-png_get_user_transform_ptr(png_structp png_ptr)
-{
-#if defined(PNG_USER_TRANSFORM_PTR_SUPPORTED)
-   if (png_ptr == NULL) return (NULL);
-   return ((png_voidp)png_ptr->user_transform_ptr);
-#else
-   return (NULL);
-#endif
-}
-#endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */
+\r
+/* pngtrans.c - transforms the data in a row (used by both readers and writers)\r
+ *\r
+ * Last changed in libpng 1.4.2 [April 29, 2010]\r
+ * Copyright (c) 1998-2010 Glenn Randers-Pehrson\r
+ * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)\r
+ * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)\r
+ *\r
+ * This code is released under the libpng license.\r
+ * For conditions of distribution and use, see the disclaimer\r
+ * and license in png.h\r
+ */\r
+\r
+#define PNG_NO_PEDANTIC_WARNINGS\r
+#include "png.h"\r
+#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)\r
+#include "pngpriv.h"\r
+\r
+#if defined(PNG_READ_BGR_SUPPORTED) || defined(PNG_WRITE_BGR_SUPPORTED)\r
+/* Turn on BGR-to-RGB mapping */\r
+void PNGAPI\r
+png_set_bgr(png_structp png_ptr)\r
+{\r
+   png_debug(1, "in png_set_bgr");\r
+\r
+   if (png_ptr == NULL)\r
+      return;\r
+   png_ptr->transformations |= PNG_BGR;\r
+}\r
+#endif\r
+\r
+#if defined(PNG_READ_SWAP_SUPPORTED) || defined(PNG_WRITE_SWAP_SUPPORTED)\r
+/* Turn on 16 bit byte swapping */\r
+void PNGAPI\r
+png_set_swap(png_structp png_ptr)\r
+{\r
+   png_debug(1, "in png_set_swap");\r
+\r
+   if (png_ptr == NULL)\r
+      return;\r
+   if (png_ptr->bit_depth == 16)\r
+      png_ptr->transformations |= PNG_SWAP_BYTES;\r
+}\r
+#endif\r
+\r
+#if defined(PNG_READ_PACK_SUPPORTED) || defined(PNG_WRITE_PACK_SUPPORTED)\r
+/* Turn on pixel packing */\r
+void PNGAPI\r
+png_set_packing(png_structp png_ptr)\r
+{\r
+   png_debug(1, "in png_set_packing");\r
+\r
+   if (png_ptr == NULL)\r
+      return;\r
+   if (png_ptr->bit_depth < 8)\r
+   {\r
+      png_ptr->transformations |= PNG_PACK;\r
+      png_ptr->usr_bit_depth = 8;\r
+   }\r
+}\r
+#endif\r
+\r
+#if defined(PNG_READ_PACKSWAP_SUPPORTED)||defined(PNG_WRITE_PACKSWAP_SUPPORTED)\r
+/* Turn on packed pixel swapping */\r
+void PNGAPI\r
+png_set_packswap(png_structp png_ptr)\r
+{\r
+   png_debug(1, "in png_set_packswap");\r
+\r
+   if (png_ptr == NULL)\r
+      return;\r
+   if (png_ptr->bit_depth < 8)\r
+      png_ptr->transformations |= PNG_PACKSWAP;\r
+}\r
+#endif\r
+\r
+#if defined(PNG_READ_SHIFT_SUPPORTED) || defined(PNG_WRITE_SHIFT_SUPPORTED)\r
+void PNGAPI\r
+png_set_shift(png_structp png_ptr, png_color_8p true_bits)\r
+{\r
+   png_debug(1, "in png_set_shift");\r
+\r
+   if (png_ptr == NULL)\r
+      return;\r
+   png_ptr->transformations |= PNG_SHIFT;\r
+   png_ptr->shift = *true_bits;\r
+}\r
+#endif\r
+\r
+#if defined(PNG_READ_INTERLACING_SUPPORTED) || \\r
+    defined(PNG_WRITE_INTERLACING_SUPPORTED)\r
+int PNGAPI\r
+png_set_interlace_handling(png_structp png_ptr)\r
+{\r
+   png_debug(1, "in png_set_interlace handling");\r
+\r
+   if (png_ptr && png_ptr->interlaced)\r
+   {\r
+      png_ptr->transformations |= PNG_INTERLACE;\r
+      return (7);\r
+   }\r
+\r
+   return (1);\r
+}\r
+#endif\r
+\r
+#if defined(PNG_READ_FILLER_SUPPORTED) || defined(PNG_WRITE_FILLER_SUPPORTED)\r
+/* Add a filler byte on read, or remove a filler or alpha byte on write.\r
+ * The filler type has changed in v0.95 to allow future 2-byte fillers\r
+ * for 48-bit input data, as well as to avoid problems with some compilers\r
+ * that don't like bytes as parameters.\r
+ */\r
+void PNGAPI\r
+png_set_filler(png_structp png_ptr, png_uint_32 filler, int filler_loc)\r
+{\r
+   png_debug(1, "in png_set_filler");\r
+\r
+   if (png_ptr == NULL)\r
+      return;\r
+   png_ptr->transformations |= PNG_FILLER;\r
+   png_ptr->filler = (png_uint_16)filler;\r
+   if (filler_loc == PNG_FILLER_AFTER)\r
+      png_ptr->flags |= PNG_FLAG_FILLER_AFTER;\r
+   else\r
+      png_ptr->flags &= ~PNG_FLAG_FILLER_AFTER;\r
+\r
+   /* This should probably go in the "do_read_filler" routine.\r
+    * I attempted to do that in libpng-1.0.1a but that caused problems\r
+    * so I restored it in libpng-1.0.2a\r
+   */\r
+\r
+   if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)\r
+   {\r
+      png_ptr->usr_channels = 4;\r
+   }\r
+\r
+   /* Also I added this in libpng-1.0.2a (what happens when we expand\r
+    * a less-than-8-bit grayscale to GA? */\r
+\r
+   if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY && png_ptr->bit_depth >= 8)\r
+   {\r
+      png_ptr->usr_channels = 2;\r
+   }\r
+}\r
+\r
+/* Added to libpng-1.2.7 */\r
+void PNGAPI\r
+png_set_add_alpha(png_structp png_ptr, png_uint_32 filler, int filler_loc)\r
+{\r
+   png_debug(1, "in png_set_add_alpha");\r
+\r
+   if (png_ptr == NULL)\r
+      return;\r
+   png_set_filler(png_ptr, filler, filler_loc);\r
+   png_ptr->transformations |= PNG_ADD_ALPHA;\r
+}\r
+\r
+#endif\r
+\r
+#if defined(PNG_READ_SWAP_ALPHA_SUPPORTED) || \\r
+    defined(PNG_WRITE_SWAP_ALPHA_SUPPORTED)\r
+void PNGAPI\r
+png_set_swap_alpha(png_structp png_ptr)\r
+{\r
+   png_debug(1, "in png_set_swap_alpha");\r
+\r
+   if (png_ptr == NULL)\r
+      return;\r
+   png_ptr->transformations |= PNG_SWAP_ALPHA;\r
+}\r
+#endif\r
+\r
+#if defined(PNG_READ_INVERT_ALPHA_SUPPORTED) || \\r
+    defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED)\r
+void PNGAPI\r
+png_set_invert_alpha(png_structp png_ptr)\r
+{\r
+   png_debug(1, "in png_set_invert_alpha");\r
+\r
+   if (png_ptr == NULL)\r
+      return;\r
+   png_ptr->transformations |= PNG_INVERT_ALPHA;\r
+}\r
+#endif\r
+\r
+#if defined(PNG_READ_INVERT_SUPPORTED) || defined(PNG_WRITE_INVERT_SUPPORTED)\r
+void PNGAPI\r
+png_set_invert_mono(png_structp png_ptr)\r
+{\r
+   png_debug(1, "in png_set_invert_mono");\r
+\r
+   if (png_ptr == NULL)\r
+      return;\r
+   png_ptr->transformations |= PNG_INVERT_MONO;\r
+}\r
+\r
+/* Invert monochrome grayscale data */\r
+void /* PRIVATE */\r
+png_do_invert(png_row_infop row_info, png_bytep row)\r
+{\r
+   png_debug(1, "in png_do_invert");\r
+\r
+  /* This test removed from libpng version 1.0.13 and 1.2.0:\r
+   *   if (row_info->bit_depth == 1 &&\r
+   */\r
+   if (row_info->color_type == PNG_COLOR_TYPE_GRAY)\r
+   {\r
+      png_bytep rp = row;\r
+      png_uint_32 i;\r
+      png_uint_32 istop = row_info->rowbytes;\r
+\r
+      for (i = 0; i < istop; i++)\r
+      {\r
+         *rp = (png_byte)(~(*rp));\r
+         rp++;\r
+      }\r
+   }\r
+   else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA &&\r
+      row_info->bit_depth == 8)\r
+   {\r
+      png_bytep rp = row;\r
+      png_uint_32 i;\r
+      png_uint_32 istop = row_info->rowbytes;\r
+\r
+      for (i = 0; i < istop; i+=2)\r
+      {\r
+         *rp = (png_byte)(~(*rp));\r
+         rp+=2;\r
+      }\r
+   }\r
+   else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA &&\r
+      row_info->bit_depth == 16)\r
+   {\r
+      png_bytep rp = row;\r
+      png_uint_32 i;\r
+      png_uint_32 istop = row_info->rowbytes;\r
+\r
+      for (i = 0; i < istop; i+=4)\r
+      {\r
+         *rp = (png_byte)(~(*rp));\r
+         *(rp+1) = (png_byte)(~(*(rp+1)));\r
+         rp+=4;\r
+      }\r
+   }\r
+}\r
+#endif\r
+\r
+#if defined(PNG_READ_SWAP_SUPPORTED) || defined(PNG_WRITE_SWAP_SUPPORTED)\r
+/* Swaps byte order on 16 bit depth images */\r
+void /* PRIVATE */\r
+png_do_swap(png_row_infop row_info, png_bytep row)\r
+{\r
+   png_debug(1, "in png_do_swap");\r
+\r
+   if (\r
+       row_info->bit_depth == 16)\r
+   {\r
+      png_bytep rp = row;\r
+      png_uint_32 i;\r
+      png_uint_32 istop= row_info->width * row_info->channels;\r
+\r
+      for (i = 0; i < istop; i++, rp += 2)\r
+      {\r
+         png_byte t = *rp;\r
+         *rp = *(rp + 1);\r
+         *(rp + 1) = t;\r
+      }\r
+   }\r
+}\r
+#endif\r
+\r
+#if defined(PNG_READ_PACKSWAP_SUPPORTED)||defined(PNG_WRITE_PACKSWAP_SUPPORTED)\r
+static PNG_CONST png_byte onebppswaptable[256] = {\r
+   0x00, 0x80, 0x40, 0xC0, 0x20, 0xA0, 0x60, 0xE0,\r
+   0x10, 0x90, 0x50, 0xD0, 0x30, 0xB0, 0x70, 0xF0,\r
+   0x08, 0x88, 0x48, 0xC8, 0x28, 0xA8, 0x68, 0xE8,\r
+   0x18, 0x98, 0x58, 0xD8, 0x38, 0xB8, 0x78, 0xF8,\r
+   0x04, 0x84, 0x44, 0xC4, 0x24, 0xA4, 0x64, 0xE4,\r
+   0x14, 0x94, 0x54, 0xD4, 0x34, 0xB4, 0x74, 0xF4,\r
+   0x0C, 0x8C, 0x4C, 0xCC, 0x2C, 0xAC, 0x6C, 0xEC,\r
+   0x1C, 0x9C, 0x5C, 0xDC, 0x3C, 0xBC, 0x7C, 0xFC,\r
+   0x02, 0x82, 0x42, 0xC2, 0x22, 0xA2, 0x62, 0xE2,\r
+   0x12, 0x92, 0x52, 0xD2, 0x32, 0xB2, 0x72, 0xF2,\r
+   0x0A, 0x8A, 0x4A, 0xCA, 0x2A, 0xAA, 0x6A, 0xEA,\r
+   0x1A, 0x9A, 0x5A, 0xDA, 0x3A, 0xBA, 0x7A, 0xFA,\r
+   0x06, 0x86, 0x46, 0xC6, 0x26, 0xA6, 0x66, 0xE6,\r
+   0x16, 0x96, 0x56, 0xD6, 0x36, 0xB6, 0x76, 0xF6,\r
+   0x0E, 0x8E, 0x4E, 0xCE, 0x2E, 0xAE, 0x6E, 0xEE,\r
+   0x1E, 0x9E, 0x5E, 0xDE, 0x3E, 0xBE, 0x7E, 0xFE,\r
+   0x01, 0x81, 0x41, 0xC1, 0x21, 0xA1, 0x61, 0xE1,\r
+   0x11, 0x91, 0x51, 0xD1, 0x31, 0xB1, 0x71, 0xF1,\r
+   0x09, 0x89, 0x49, 0xC9, 0x29, 0xA9, 0x69, 0xE9,\r
+   0x19, 0x99, 0x59, 0xD9, 0x39, 0xB9, 0x79, 0xF9,\r
+   0x05, 0x85, 0x45, 0xC5, 0x25, 0xA5, 0x65, 0xE5,\r
+   0x15, 0x95, 0x55, 0xD5, 0x35, 0xB5, 0x75, 0xF5,\r
+   0x0D, 0x8D, 0x4D, 0xCD, 0x2D, 0xAD, 0x6D, 0xED,\r
+   0x1D, 0x9D, 0x5D, 0xDD, 0x3D, 0xBD, 0x7D, 0xFD,\r
+   0x03, 0x83, 0x43, 0xC3, 0x23, 0xA3, 0x63, 0xE3,\r
+   0x13, 0x93, 0x53, 0xD3, 0x33, 0xB3, 0x73, 0xF3,\r
+   0x0B, 0x8B, 0x4B, 0xCB, 0x2B, 0xAB, 0x6B, 0xEB,\r
+   0x1B, 0x9B, 0x5B, 0xDB, 0x3B, 0xBB, 0x7B, 0xFB,\r
+   0x07, 0x87, 0x47, 0xC7, 0x27, 0xA7, 0x67, 0xE7,\r
+   0x17, 0x97, 0x57, 0xD7, 0x37, 0xB7, 0x77, 0xF7,\r
+   0x0F, 0x8F, 0x4F, 0xCF, 0x2F, 0xAF, 0x6F, 0xEF,\r
+   0x1F, 0x9F, 0x5F, 0xDF, 0x3F, 0xBF, 0x7F, 0xFF\r
+};\r
+\r
+static PNG_CONST png_byte twobppswaptable[256] = {\r
+   0x00, 0x40, 0x80, 0xC0, 0x10, 0x50, 0x90, 0xD0,\r
+   0x20, 0x60, 0xA0, 0xE0, 0x30, 0x70, 0xB0, 0xF0,\r
+   0x04, 0x44, 0x84, 0xC4, 0x14, 0x54, 0x94, 0xD4,\r
+   0x24, 0x64, 0xA4, 0xE4, 0x34, 0x74, 0xB4, 0xF4,\r
+   0x08, 0x48, 0x88, 0xC8, 0x18, 0x58, 0x98, 0xD8,\r
+   0x28, 0x68, 0xA8, 0xE8, 0x38, 0x78, 0xB8, 0xF8,\r
+   0x0C, 0x4C, 0x8C, 0xCC, 0x1C, 0x5C, 0x9C, 0xDC,\r
+   0x2C, 0x6C, 0xAC, 0xEC, 0x3C, 0x7C, 0xBC, 0xFC,\r
+   0x01, 0x41, 0x81, 0xC1, 0x11, 0x51, 0x91, 0xD1,\r
+   0x21, 0x61, 0xA1, 0xE1, 0x31, 0x71, 0xB1, 0xF1,\r
+   0x05, 0x45, 0x85, 0xC5, 0x15, 0x55, 0x95, 0xD5,\r
+   0x25, 0x65, 0xA5, 0xE5, 0x35, 0x75, 0xB5, 0xF5,\r
+   0x09, 0x49, 0x89, 0xC9, 0x19, 0x59, 0x99, 0xD9,\r
+   0x29, 0x69, 0xA9, 0xE9, 0x39, 0x79, 0xB9, 0xF9,\r
+   0x0D, 0x4D, 0x8D, 0xCD, 0x1D, 0x5D, 0x9D, 0xDD,\r
+   0x2D, 0x6D, 0xAD, 0xED, 0x3D, 0x7D, 0xBD, 0xFD,\r
+   0x02, 0x42, 0x82, 0xC2, 0x12, 0x52, 0x92, 0xD2,\r
+   0x22, 0x62, 0xA2, 0xE2, 0x32, 0x72, 0xB2, 0xF2,\r
+   0x06, 0x46, 0x86, 0xC6, 0x16, 0x56, 0x96, 0xD6,\r
+   0x26, 0x66, 0xA6, 0xE6, 0x36, 0x76, 0xB6, 0xF6,\r
+   0x0A, 0x4A, 0x8A, 0xCA, 0x1A, 0x5A, 0x9A, 0xDA,\r
+   0x2A, 0x6A, 0xAA, 0xEA, 0x3A, 0x7A, 0xBA, 0xFA,\r
+   0x0E, 0x4E, 0x8E, 0xCE, 0x1E, 0x5E, 0x9E, 0xDE,\r
+   0x2E, 0x6E, 0xAE, 0xEE, 0x3E, 0x7E, 0xBE, 0xFE,\r
+   0x03, 0x43, 0x83, 0xC3, 0x13, 0x53, 0x93, 0xD3,\r
+   0x23, 0x63, 0xA3, 0xE3, 0x33, 0x73, 0xB3, 0xF3,\r
+   0x07, 0x47, 0x87, 0xC7, 0x17, 0x57, 0x97, 0xD7,\r
+   0x27, 0x67, 0xA7, 0xE7, 0x37, 0x77, 0xB7, 0xF7,\r
+   0x0B, 0x4B, 0x8B, 0xCB, 0x1B, 0x5B, 0x9B, 0xDB,\r
+   0x2B, 0x6B, 0xAB, 0xEB, 0x3B, 0x7B, 0xBB, 0xFB,\r
+   0x0F, 0x4F, 0x8F, 0xCF, 0x1F, 0x5F, 0x9F, 0xDF,\r
+   0x2F, 0x6F, 0xAF, 0xEF, 0x3F, 0x7F, 0xBF, 0xFF\r
+};\r
+\r
+static PNG_CONST png_byte fourbppswaptable[256] = {\r
+   0x00, 0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70,\r
+   0x80, 0x90, 0xA0, 0xB0, 0xC0, 0xD0, 0xE0, 0xF0,\r
+   0x01, 0x11, 0x21, 0x31, 0x41, 0x51, 0x61, 0x71,\r
+   0x81, 0x91, 0xA1, 0xB1, 0xC1, 0xD1, 0xE1, 0xF1,\r
+   0x02, 0x12, 0x22, 0x32, 0x42, 0x52, 0x62, 0x72,\r
+   0x82, 0x92, 0xA2, 0xB2, 0xC2, 0xD2, 0xE2, 0xF2,\r
+   0x03, 0x13, 0x23, 0x33, 0x43, 0x53, 0x63, 0x73,\r
+   0x83, 0x93, 0xA3, 0xB3, 0xC3, 0xD3, 0xE3, 0xF3,\r
+   0x04, 0x14, 0x24, 0x34, 0x44, 0x54, 0x64, 0x74,\r
+   0x84, 0x94, 0xA4, 0xB4, 0xC4, 0xD4, 0xE4, 0xF4,\r
+   0x05, 0x15, 0x25, 0x35, 0x45, 0x55, 0x65, 0x75,\r
+   0x85, 0x95, 0xA5, 0xB5, 0xC5, 0xD5, 0xE5, 0xF5,\r
+   0x06, 0x16, 0x26, 0x36, 0x46, 0x56, 0x66, 0x76,\r
+   0x86, 0x96, 0xA6, 0xB6, 0xC6, 0xD6, 0xE6, 0xF6,\r
+   0x07, 0x17, 0x27, 0x37, 0x47, 0x57, 0x67, 0x77,\r
+   0x87, 0x97, 0xA7, 0xB7, 0xC7, 0xD7, 0xE7, 0xF7,\r
+   0x08, 0x18, 0x28, 0x38, 0x48, 0x58, 0x68, 0x78,\r
+   0x88, 0x98, 0xA8, 0xB8, 0xC8, 0xD8, 0xE8, 0xF8,\r
+   0x09, 0x19, 0x29, 0x39, 0x49, 0x59, 0x69, 0x79,\r
+   0x89, 0x99, 0xA9, 0xB9, 0xC9, 0xD9, 0xE9, 0xF9,\r
+   0x0A, 0x1A, 0x2A, 0x3A, 0x4A, 0x5A, 0x6A, 0x7A,\r
+   0x8A, 0x9A, 0xAA, 0xBA, 0xCA, 0xDA, 0xEA, 0xFA,\r
+   0x0B, 0x1B, 0x2B, 0x3B, 0x4B, 0x5B, 0x6B, 0x7B,\r
+   0x8B, 0x9B, 0xAB, 0xBB, 0xCB, 0xDB, 0xEB, 0xFB,\r
+   0x0C, 0x1C, 0x2C, 0x3C, 0x4C, 0x5C, 0x6C, 0x7C,\r
+   0x8C, 0x9C, 0xAC, 0xBC, 0xCC, 0xDC, 0xEC, 0xFC,\r
+   0x0D, 0x1D, 0x2D, 0x3D, 0x4D, 0x5D, 0x6D, 0x7D,\r
+   0x8D, 0x9D, 0xAD, 0xBD, 0xCD, 0xDD, 0xED, 0xFD,\r
+   0x0E, 0x1E, 0x2E, 0x3E, 0x4E, 0x5E, 0x6E, 0x7E,\r
+   0x8E, 0x9E, 0xAE, 0xBE, 0xCE, 0xDE, 0xEE, 0xFE,\r
+   0x0F, 0x1F, 0x2F, 0x3F, 0x4F, 0x5F, 0x6F, 0x7F,\r
+   0x8F, 0x9F, 0xAF, 0xBF, 0xCF, 0xDF, 0xEF, 0xFF\r
+};\r
+\r
+/* Swaps pixel packing order within bytes */\r
+void /* PRIVATE */\r
+png_do_packswap(png_row_infop row_info, png_bytep row)\r
+{\r
+   png_debug(1, "in png_do_packswap");\r
+\r
+   if (\r
+       row_info->bit_depth < 8)\r
+   {\r
+      png_bytep rp, end, table;\r
+\r
+      end = row + row_info->rowbytes;\r
+\r
+      if (row_info->bit_depth == 1)\r
+         table = (png_bytep)onebppswaptable;\r
+      else if (row_info->bit_depth == 2)\r
+         table = (png_bytep)twobppswaptable;\r
+      else if (row_info->bit_depth == 4)\r
+         table = (png_bytep)fourbppswaptable;\r
+      else\r
+         return;\r
+\r
+      for (rp = row; rp < end; rp++)\r
+         *rp = table[*rp];\r
+   }\r
+}\r
+#endif /* PNG_READ_PACKSWAP_SUPPORTED or PNG_WRITE_PACKSWAP_SUPPORTED */\r
+\r
+#if defined(PNG_WRITE_FILLER_SUPPORTED) || \\r
+    defined(PNG_READ_STRIP_ALPHA_SUPPORTED)\r
+/* Remove filler or alpha byte(s) */\r
+void /* PRIVATE */\r
+png_do_strip_filler(png_row_infop row_info, png_bytep row, png_uint_32 flags)\r
+{\r
+   png_debug(1, "in png_do_strip_filler");\r
+\r
+   {\r
+      png_bytep sp=row;\r
+      png_bytep dp=row;\r
+      png_uint_32 row_width=row_info->width;\r
+      png_uint_32 i;\r
+\r
+      if ((row_info->color_type == PNG_COLOR_TYPE_RGB ||\r
+          (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA &&\r
+          (flags & PNG_FLAG_STRIP_ALPHA))) &&\r
+          row_info->channels == 4)\r
+      {\r
+         if (row_info->bit_depth == 8)\r
+         {\r
+            /* This converts from RGBX or RGBA to RGB */\r
+            if (flags & PNG_FLAG_FILLER_AFTER)\r
+            {\r
+               dp+=3; sp+=4;\r
+               for (i = 1; i < row_width; i++)\r
+               {\r
+                  *dp++ = *sp++;\r
+                  *dp++ = *sp++;\r
+                  *dp++ = *sp++;\r
+                  sp++;\r
+               }\r
+            }\r
+            /* This converts from XRGB or ARGB to RGB */\r
+            else\r
+            {\r
+               for (i = 0; i < row_width; i++)\r
+               {\r
+                  sp++;\r
+                  *dp++ = *sp++;\r
+                  *dp++ = *sp++;\r
+                  *dp++ = *sp++;\r
+               }\r
+            }\r
+            row_info->pixel_depth = 24;\r
+            row_info->rowbytes = row_width * 3;\r
+         }\r
+         else /* if (row_info->bit_depth == 16) */\r
+         {\r
+            if (flags & PNG_FLAG_FILLER_AFTER)\r
+            {\r
+               /* This converts from RRGGBBXX or RRGGBBAA to RRGGBB */\r
+               sp += 8; dp += 6;\r
+               for (i = 1; i < row_width; i++)\r
+               {\r
+                  /* This could be (although png_memcpy is probably slower):\r
+                  png_memcpy(dp, sp, 6);\r
+                  sp += 8;\r
+                  dp += 6;\r
+                  */\r
+\r
+                  *dp++ = *sp++;\r
+                  *dp++ = *sp++;\r
+                  *dp++ = *sp++;\r
+                  *dp++ = *sp++;\r
+                  *dp++ = *sp++;\r
+                  *dp++ = *sp++;\r
+                  sp += 2;\r
+               }\r
+            }\r
+            else\r
+            {\r
+               /* This converts from XXRRGGBB or AARRGGBB to RRGGBB */\r
+               for (i = 0; i < row_width; i++)\r
+               {\r
+                  /* This could be (although png_memcpy is probably slower):\r
+                  png_memcpy(dp, sp, 6);\r
+                  sp += 8;\r
+                  dp += 6;\r
+                  */\r
+\r
+                  sp+=2;\r
+                  *dp++ = *sp++;\r
+                  *dp++ = *sp++;\r
+                  *dp++ = *sp++;\r
+                  *dp++ = *sp++;\r
+                  *dp++ = *sp++;\r
+                  *dp++ = *sp++;\r
+               }\r
+            }\r
+            row_info->pixel_depth = 48;\r
+            row_info->rowbytes = row_width * 6;\r
+         }\r
+         row_info->channels = 3;\r
+      }\r
+      else if ((row_info->color_type == PNG_COLOR_TYPE_GRAY ||\r
+         (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA &&\r
+         (flags & PNG_FLAG_STRIP_ALPHA))) &&\r
+          row_info->channels == 2)\r
+      {\r
+         if (row_info->bit_depth == 8)\r
+         {\r
+            /* This converts from GX or GA to G */\r
+            if (flags & PNG_FLAG_FILLER_AFTER)\r
+            {\r
+               for (i = 0; i < row_width; i++)\r
+               {\r
+                  *dp++ = *sp++;\r
+                  sp++;\r
+               }\r
+            }\r
+            /* This converts from XG or AG to G */\r
+            else\r
+            {\r
+               for (i = 0; i < row_width; i++)\r
+               {\r
+                  sp++;\r
+                  *dp++ = *sp++;\r
+               }\r
+            }\r
+            row_info->pixel_depth = 8;\r
+            row_info->rowbytes = row_width;\r
+         }\r
+         else /* if (row_info->bit_depth == 16) */\r
+         {\r
+            if (flags & PNG_FLAG_FILLER_AFTER)\r
+            {\r
+               /* This converts from GGXX or GGAA to GG */\r
+               sp += 4; dp += 2;\r
+               for (i = 1; i < row_width; i++)\r
+               {\r
+                  *dp++ = *sp++;\r
+                  *dp++ = *sp++;\r
+                  sp += 2;\r
+               }\r
+            }\r
+            else\r
+            {\r
+               /* This converts from XXGG or AAGG to GG */\r
+               for (i = 0; i < row_width; i++)\r
+               {\r
+                  sp += 2;\r
+                  *dp++ = *sp++;\r
+                  *dp++ = *sp++;\r
+               }\r
+            }\r
+            row_info->pixel_depth = 16;\r
+            row_info->rowbytes = row_width * 2;\r
+         }\r
+         row_info->channels = 1;\r
+      }\r
+      if (flags & PNG_FLAG_STRIP_ALPHA)\r
+        row_info->color_type &= ~PNG_COLOR_MASK_ALPHA;\r
+   }\r
+}\r
+#endif\r
+\r
+#if defined(PNG_READ_BGR_SUPPORTED) || defined(PNG_WRITE_BGR_SUPPORTED)\r
+/* Swaps red and blue bytes within a pixel */\r
+void /* PRIVATE */\r
+png_do_bgr(png_row_infop row_info, png_bytep row)\r
+{\r
+   png_debug(1, "in png_do_bgr");\r
+\r
+   if (\r
+       (row_info->color_type & PNG_COLOR_MASK_COLOR))\r
+   {\r
+      png_uint_32 row_width = row_info->width;\r
+      if (row_info->bit_depth == 8)\r
+      {\r
+         if (row_info->color_type == PNG_COLOR_TYPE_RGB)\r
+         {\r
+            png_bytep rp;\r
+            png_uint_32 i;\r
+\r
+            for (i = 0, rp = row; i < row_width; i++, rp += 3)\r
+            {\r
+               png_byte save = *rp;\r
+               *rp = *(rp + 2);\r
+               *(rp + 2) = save;\r
+            }\r
+         }\r
+         else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)\r
+         {\r
+            png_bytep rp;\r
+            png_uint_32 i;\r
+\r
+            for (i = 0, rp = row; i < row_width; i++, rp += 4)\r
+            {\r
+               png_byte save = *rp;\r
+               *rp = *(rp + 2);\r
+               *(rp + 2) = save;\r
+            }\r
+         }\r
+      }\r
+      else if (row_info->bit_depth == 16)\r
+      {\r
+         if (row_info->color_type == PNG_COLOR_TYPE_RGB)\r
+         {\r
+            png_bytep rp;\r
+            png_uint_32 i;\r
+\r
+            for (i = 0, rp = row; i < row_width; i++, rp += 6)\r
+            {\r
+               png_byte save = *rp;\r
+               *rp = *(rp + 4);\r
+               *(rp + 4) = save;\r
+               save = *(rp + 1);\r
+               *(rp + 1) = *(rp + 5);\r
+               *(rp + 5) = save;\r
+            }\r
+         }\r
+         else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)\r
+         {\r
+            png_bytep rp;\r
+            png_uint_32 i;\r
+\r
+            for (i = 0, rp = row; i < row_width; i++, rp += 8)\r
+            {\r
+               png_byte save = *rp;\r
+               *rp = *(rp + 4);\r
+               *(rp + 4) = save;\r
+               save = *(rp + 1);\r
+               *(rp + 1) = *(rp + 5);\r
+               *(rp + 5) = save;\r
+            }\r
+         }\r
+      }\r
+   }\r
+}\r
+#endif /* PNG_READ_BGR_SUPPORTED or PNG_WRITE_BGR_SUPPORTED */\r
+\r
+#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \\r
+    defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)\r
+void PNGAPI\r
+png_set_user_transform_info(png_structp png_ptr, png_voidp\r
+   user_transform_ptr, int user_transform_depth, int user_transform_channels)\r
+{\r
+   png_debug(1, "in png_set_user_transform_info");\r
+\r
+   if (png_ptr == NULL)\r
+      return;\r
+#ifdef PNG_USER_TRANSFORM_PTR_SUPPORTED\r
+   png_ptr->user_transform_ptr = user_transform_ptr;\r
+   png_ptr->user_transform_depth = (png_byte)user_transform_depth;\r
+   png_ptr->user_transform_channels = (png_byte)user_transform_channels;\r
+#else\r
+   if (user_transform_ptr || user_transform_depth || user_transform_channels)\r
+      png_warning(png_ptr,\r
+        "This version of libpng does not support user transform info");\r
+#endif\r
+}\r
+\r
+/* This function returns a pointer to the user_transform_ptr associated with\r
+ * the user transform functions.  The application should free any memory\r
+ * associated with this pointer before png_write_destroy and png_read_destroy\r
+ * are called.\r
+ */\r
+png_voidp PNGAPI\r
+png_get_user_transform_ptr(png_structp png_ptr)\r
+{\r
+   if (png_ptr == NULL)\r
+      return (NULL);\r
+#ifdef PNG_USER_TRANSFORM_PTR_SUPPORTED\r
+   return ((png_voidp)png_ptr->user_transform_ptr);\r
+#else\r
+   return (NULL);\r
+#endif\r
+}\r
+#endif /* PNG_READ_USER_TRANSFORM_SUPPORTED ||\r
+         PNG_WRITE_USER_TRANSFORM_SUPPORTED */\r
+#endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */\r
diff --git a/3rdparty/libpng/pngvcrd.c b/3rdparty/libpng/pngvcrd.c
deleted file mode 100644 (file)
index ce4233e..0000000
+++ /dev/null
@@ -1 +0,0 @@
-/* pnggvrd.c was removed from libpng-1.2.20. */
index 371a4fa..d7b42fd 100644 (file)
-
-/* pngwio.c - functions for data output
- *
- * Last changed in libpng 1.2.13 November 13, 2006
- * For conditions of distribution and use, see copyright notice in png.h
- * Copyright (c) 1998-2006 Glenn Randers-Pehrson
- * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
- * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
- *
- * This file provides a location for all output.  Users who need
- * special handling are expected to write functions that have the same
- * arguments as these and perform similar functions, but that possibly
- * use different output methods.  Note that you shouldn't change these
- * functions, but rather write replacement functions and then change
- * them at run time with png_set_write_fn(...).
- */
-
-#define PNG_INTERNAL
-#include "png.h"
-#ifdef PNG_WRITE_SUPPORTED
-
-/* Write the data to whatever output you are using.  The default routine
-   writes to a file pointer.  Note that this routine sometimes gets called
-   with very small lengths, so you should implement some kind of simple
-   buffering if you are using unbuffered writes.  This should never be asked
-   to write more than 64K on a 16 bit machine.  */
-
-void /* PRIVATE */
-png_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
-{
-   if (png_ptr->write_data_fn != NULL )
-      (*(png_ptr->write_data_fn))(png_ptr, data, length);
-   else
-      png_error(png_ptr, "Call to NULL write function");
-}
-
-#if !defined(PNG_NO_STDIO)
-/* This is the function that does the actual writing of data.  If you are
-   not writing to a standard C stream, you should create a replacement
-   write_data function and use it at run time with png_set_write_fn(), rather
-   than changing the library. */
-#ifndef USE_FAR_KEYWORD
-void PNGAPI
-png_default_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
-{
-   png_uint_32 check;
-
-   if(png_ptr == NULL) return;
-#if defined(_WIN32_WCE)
-   if ( !WriteFile((HANDLE)(png_ptr->io_ptr), data, length, &check, NULL) )
-      check = 0;
-#else
-   check = fwrite(data, 1, length, (png_FILE_p)(png_ptr->io_ptr));
-#endif
-   if (check != length)
-      png_error(png_ptr, "Write Error");
-}
-#else
-/* this is the model-independent version. Since the standard I/O library
-   can't handle far buffers in the medium and small models, we have to copy
-   the data.
-*/
-
-#define NEAR_BUF_SIZE 1024
-#define MIN(a,b) (a <= b ? a : b)
-
-void PNGAPI
-png_default_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
-{
-   png_uint_32 check;
-   png_byte *near_data;  /* Needs to be "png_byte *" instead of "png_bytep" */
-   png_FILE_p io_ptr;
-
-   if(png_ptr == NULL) return;
-   /* Check if data really is near. If so, use usual code. */
-   near_data = (png_byte *)CVT_PTR_NOCHECK(data);
-   io_ptr = (png_FILE_p)CVT_PTR(png_ptr->io_ptr);
-   if ((png_bytep)near_data == data)
-   {
-#if defined(_WIN32_WCE)
-      if ( !WriteFile(io_ptr, near_data, length, &check, NULL) )
-         check = 0;
-#else
-      check = fwrite(near_data, 1, length, io_ptr);
-#endif
-   }
-   else
-   {
-      png_byte buf[NEAR_BUF_SIZE];
-      png_size_t written, remaining, err;
-      check = 0;
-      remaining = length;
-      do
-      {
-         written = MIN(NEAR_BUF_SIZE, remaining);
-         png_memcpy(buf, data, written); /* copy far buffer to near buffer */
-#if defined(_WIN32_WCE)
-         if ( !WriteFile(io_ptr, buf, written, &err, NULL) )
-            err = 0;
-#else
-         err = fwrite(buf, 1, written, io_ptr);
-#endif
-         if (err != written)
-            break;
-         else
-            check += err;
-         data += written;
-         remaining -= written;
-      }
-      while (remaining != 0);
-   }
-   if (check != length)
-      png_error(png_ptr, "Write Error");
-}
-
-#endif
-#endif
-
-/* This function is called to output any data pending writing (normally
-   to disk).  After png_flush is called, there should be no data pending
-   writing in any buffers. */
-#if defined(PNG_WRITE_FLUSH_SUPPORTED)
-void /* PRIVATE */
-png_flush(png_structp png_ptr)
-{
-   if (png_ptr->output_flush_fn != NULL)
-      (*(png_ptr->output_flush_fn))(png_ptr);
-}
-
-#if !defined(PNG_NO_STDIO)
-void PNGAPI
-png_default_flush(png_structp png_ptr)
-{
-#if !defined(_WIN32_WCE)
-   png_FILE_p io_ptr;
-#endif
-   if(png_ptr == NULL) return;
-#if !defined(_WIN32_WCE)
-   io_ptr = (png_FILE_p)CVT_PTR((png_ptr->io_ptr));
-   if (io_ptr != NULL)
-      fflush(io_ptr);
-#endif
-}
-#endif
-#endif
-
-/* This function allows the application to supply new output functions for
-   libpng if standard C streams aren't being used.
-
-   This function takes as its arguments:
-   png_ptr       - pointer to a png output data structure
-   io_ptr        - pointer to user supplied structure containing info about
-                   the output functions.  May be NULL.
-   write_data_fn - pointer to a new output function that takes as its
-                   arguments a pointer to a png_struct, a pointer to
-                   data to be written, and a 32-bit unsigned int that is
-                   the number of bytes to be written.  The new write
-                   function should call png_error(png_ptr, "Error msg")
-                   to exit and output any fatal error messages.
-   flush_data_fn - pointer to a new flush function that takes as its
-                   arguments a pointer to a png_struct.  After a call to
-                   the flush function, there should be no data in any buffers
-                   or pending transmission.  If the output method doesn't do
-                   any buffering of ouput, a function prototype must still be
-                   supplied although it doesn't have to do anything.  If
-                   PNG_WRITE_FLUSH_SUPPORTED is not defined at libpng compile
-                   time, output_flush_fn will be ignored, although it must be
-                   supplied for compatibility. */
-void PNGAPI
-png_set_write_fn(png_structp png_ptr, png_voidp io_ptr,
-   png_rw_ptr write_data_fn, png_flush_ptr output_flush_fn)
-{
-   if(png_ptr == NULL) return;
-   png_ptr->io_ptr = io_ptr;
-
-#if !defined(PNG_NO_STDIO)
-   if (write_data_fn != NULL)
-      png_ptr->write_data_fn = write_data_fn;
-   else
-      png_ptr->write_data_fn = png_default_write_data;
-#else
-   png_ptr->write_data_fn = write_data_fn;
-#endif
-
-#if defined(PNG_WRITE_FLUSH_SUPPORTED)
-#if !defined(PNG_NO_STDIO)
-   if (output_flush_fn != NULL)
-      png_ptr->output_flush_fn = output_flush_fn;
-   else
-      png_ptr->output_flush_fn = png_default_flush;
-#else
-   png_ptr->output_flush_fn = output_flush_fn;
-#endif
-#endif /* PNG_WRITE_FLUSH_SUPPORTED */
-
-   /* It is an error to read while writing a png file */
-   if (png_ptr->read_data_fn != NULL)
-   {
-      png_ptr->read_data_fn = NULL;
-      png_warning(png_ptr,
-         "Attempted to set both read_data_fn and write_data_fn in");
-      png_warning(png_ptr,
-         "the same structure.  Resetting read_data_fn to NULL.");
-   }
-}
-
-#if defined(USE_FAR_KEYWORD)
-#if defined(_MSC_VER)
-void *png_far_to_near(png_structp png_ptr,png_voidp ptr, int check)
-{
-   void *near_ptr;
-   void FAR *far_ptr;
-   FP_OFF(near_ptr) = FP_OFF(ptr);
-   far_ptr = (void FAR *)near_ptr;
-   if(check != 0)
-      if(FP_SEG(ptr) != FP_SEG(far_ptr))
-         png_error(png_ptr,"segment lost in conversion");
-   return(near_ptr);
-}
-#  else
-void *png_far_to_near(png_structp png_ptr,png_voidp ptr, int check)
-{
-   void *near_ptr;
-   void FAR *far_ptr;
-   near_ptr = (void FAR *)ptr;
-   far_ptr = (void FAR *)near_ptr;
-   if(check != 0)
-      if(far_ptr != ptr)
-         png_error(png_ptr,"segment lost in conversion");
-   return(near_ptr);
-}
-#   endif
-#   endif
-#endif /* PNG_WRITE_SUPPORTED */
+\r
+/* pngwio.c - functions for data output\r
+ *\r
+ * Last changed in libpng 1.4.0 [January 3, 2010]\r
+ * Copyright (c) 1998-2010 Glenn Randers-Pehrson\r
+ * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)\r
+ * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)\r
+ *\r
+ * This code is released under the libpng license.\r
+ * For conditions of distribution and use, see the disclaimer\r
+ * and license in png.h\r
+ *\r
+ * This file provides a location for all output.  Users who need\r
+ * special handling are expected to write functions that have the same\r
+ * arguments as these and perform similar functions, but that possibly\r
+ * use different output methods.  Note that you shouldn't change these\r
+ * functions, but rather write replacement functions and then change\r
+ * them at run time with png_set_write_fn(...).\r
+ */\r
+\r
+#define PNG_NO_PEDANTIC_WARNINGS\r
+#include "png.h"\r
+#ifdef PNG_WRITE_SUPPORTED\r
+#include "pngpriv.h"\r
+\r
+/* Write the data to whatever output you are using.  The default routine\r
+ * writes to a file pointer.  Note that this routine sometimes gets called\r
+ * with very small lengths, so you should implement some kind of simple\r
+ * buffering if you are using unbuffered writes.  This should never be asked\r
+ * to write more than 64K on a 16 bit machine.\r
+ */\r
+\r
+void /* PRIVATE */\r
+png_write_data(png_structp png_ptr, png_bytep data, png_size_t length)\r
+{\r
+   if (png_ptr->write_data_fn != NULL )\r
+      (*(png_ptr->write_data_fn))(png_ptr, data, length);\r
+   else\r
+      png_error(png_ptr, "Call to NULL write function");\r
+}\r
+\r
+#ifdef PNG_STDIO_SUPPORTED\r
+/* This is the function that does the actual writing of data.  If you are\r
+ * not writing to a standard C stream, you should create a replacement\r
+ * write_data function and use it at run time with png_set_write_fn(), rather\r
+ * than changing the library.\r
+ */\r
+#ifndef USE_FAR_KEYWORD\r
+void PNGAPI\r
+png_default_write_data(png_structp png_ptr, png_bytep data, png_size_t length)\r
+{\r
+   png_uint_32 check;\r
+\r
+   if (png_ptr == NULL)\r
+      return;\r
+   check = fwrite(data, 1, length, (png_FILE_p)(png_ptr->io_ptr));\r
+   if (check != length)\r
+      png_error(png_ptr, "Write Error");\r
+}\r
+#else\r
+/* This is the model-independent version. Since the standard I/O library\r
+ * can't handle far buffers in the medium and small models, we have to copy\r
+ * the data.\r
+ */\r
+\r
+#define NEAR_BUF_SIZE 1024\r
+#define MIN(a,b) (a <= b ? a : b)\r
+\r
+void PNGAPI\r
+png_default_write_data(png_structp png_ptr, png_bytep data, png_size_t length)\r
+{\r
+   png_uint_32 check;\r
+   png_byte *near_data;  /* Needs to be "png_byte *" instead of "png_bytep" */\r
+   png_FILE_p io_ptr;\r
+\r
+   if (png_ptr == NULL)\r
+      return;\r
+   /* Check if data really is near. If so, use usual code. */\r
+   near_data = (png_byte *)CVT_PTR_NOCHECK(data);\r
+   io_ptr = (png_FILE_p)CVT_PTR(png_ptr->io_ptr);\r
+   if ((png_bytep)near_data == data)\r
+   {\r
+      check = fwrite(near_data, 1, length, io_ptr);\r
+   }\r
+   else\r
+   {\r
+      png_byte buf[NEAR_BUF_SIZE];\r
+      png_size_t written, remaining, err;\r
+      check = 0;\r
+      remaining = length;\r
+      do\r
+      {\r
+         written = MIN(NEAR_BUF_SIZE, remaining);\r
+         png_memcpy(buf, data, written); /* Copy far buffer to near buffer */\r
+         err = fwrite(buf, 1, written, io_ptr);\r
+         if (err != written)\r
+            break;\r
+\r
+         else\r
+            check += err;\r
+\r
+         data += written;\r
+         remaining -= written;\r
+      }\r
+      while (remaining != 0);\r
+   }\r
+   if (check != length)\r
+      png_error(png_ptr, "Write Error");\r
+}\r
+\r
+#endif\r
+#endif\r
+\r
+/* This function is called to output any data pending writing (normally\r
+ * to disk).  After png_flush is called, there should be no data pending\r
+ * writing in any buffers.\r
+ */\r
+#ifdef PNG_WRITE_FLUSH_SUPPORTED\r
+void /* PRIVATE */\r
+png_flush(png_structp png_ptr)\r
+{\r
+   if (png_ptr->output_flush_fn != NULL)\r
+      (*(png_ptr->output_flush_fn))(png_ptr);\r
+}\r
+\r
+#ifdef PNG_STDIO_SUPPORTED\r
+void PNGAPI\r
+png_default_flush(png_structp png_ptr)\r
+{\r
+   png_FILE_p io_ptr;\r
+   if (png_ptr == NULL)\r
+      return;\r
+   io_ptr = (png_FILE_p)CVT_PTR((png_ptr->io_ptr));\r
+   fflush(io_ptr);\r
+}\r
+#endif\r
+#endif\r
+\r
+/* This function allows the application to supply new output functions for\r
+ * libpng if standard C streams aren't being used.\r
+ *\r
+ * This function takes as its arguments:\r
+ * png_ptr       - pointer to a png output data structure\r
+ * io_ptr        - pointer to user supplied structure containing info about\r
+ *                 the output functions.  May be NULL.\r
+ * write_data_fn - pointer to a new output function that takes as its\r
+ *                 arguments a pointer to a png_struct, a pointer to\r
+ *                 data to be written, and a 32-bit unsigned int that is\r
+ *                 the number of bytes to be written.  The new write\r
+ *                 function should call png_error(png_ptr, "Error msg")\r
+ *                 to exit and output any fatal error messages.  May be\r
+ *                 NULL, in which case libpng's default function will\r
+ *                 be used.\r
+ * flush_data_fn - pointer to a new flush function that takes as its\r
+ *                 arguments a pointer to a png_struct.  After a call to\r
+ *                 the flush function, there should be no data in any buffers\r
+ *                 or pending transmission.  If the output method doesn't do\r
+ *                 any buffering of output, a function prototype must still be\r
+ *                 supplied although it doesn't have to do anything.  If\r
+ *                 PNG_WRITE_FLUSH_SUPPORTED is not defined at libpng compile\r
+ *                 time, output_flush_fn will be ignored, although it must be\r
+ *                 supplied for compatibility.  May be NULL, in which case\r
+ *                 libpng's default function will be used, if\r
+ *                 PNG_WRITE_FLUSH_SUPPORTED is defined.  This is not\r
+ *                 a good idea if io_ptr does not point to a standard\r
+ *                 *FILE structure.\r
+ */\r
+void PNGAPI\r
+png_set_write_fn(png_structp png_ptr, png_voidp io_ptr,\r
+   png_rw_ptr write_data_fn, png_flush_ptr output_flush_fn)\r
+{\r
+   if (png_ptr == NULL)\r
+      return;\r
+\r
+   png_ptr->io_ptr = io_ptr;\r
+\r
+#ifdef PNG_STDIO_SUPPORTED\r
+   if (write_data_fn != NULL)\r
+      png_ptr->write_data_fn = write_data_fn;\r
+\r
+   else\r
+      png_ptr->write_data_fn = png_default_write_data;\r
+#else\r
+   png_ptr->write_data_fn = write_data_fn;\r
+#endif\r
+\r
+#ifdef PNG_WRITE_FLUSH_SUPPORTED\r
+#ifdef PNG_STDIO_SUPPORTED\r
+   if (output_flush_fn != NULL)\r
+      png_ptr->output_flush_fn = output_flush_fn;\r
+\r
+   else\r
+      png_ptr->output_flush_fn = png_default_flush;\r
+#else\r
+   png_ptr->output_flush_fn = output_flush_fn;\r
+#endif\r
+#endif /* PNG_WRITE_FLUSH_SUPPORTED */\r
+\r
+   /* It is an error to read while writing a png file */\r
+   if (png_ptr->read_data_fn != NULL)\r
+   {\r
+      png_ptr->read_data_fn = NULL;\r
+      png_warning(png_ptr,\r
+         "Attempted to set both read_data_fn and write_data_fn in");\r
+      png_warning(png_ptr,\r
+         "the same structure.  Resetting read_data_fn to NULL");\r
+   }\r
+}\r
+\r
+#ifdef USE_FAR_KEYWORD\r
+#ifdef _MSC_VER\r
+void *png_far_to_near(png_structp png_ptr, png_voidp ptr, int check)\r
+{\r
+   void *near_ptr;\r
+   void FAR *far_ptr;\r
+   FP_OFF(near_ptr) = FP_OFF(ptr);\r
+   far_ptr = (void FAR *)near_ptr;\r
+\r
+   if (check != 0)\r
+      if (FP_SEG(ptr) != FP_SEG(far_ptr))\r
+         png_error(png_ptr, "segment lost in conversion");\r
+\r
+   return(near_ptr);\r
+}\r
+#  else\r
+void *png_far_to_near(png_structp png_ptr, png_voidp ptr, int check)\r
+{\r
+   void *near_ptr;\r
+   void FAR *far_ptr;\r
+   near_ptr = (void FAR *)ptr;\r
+   far_ptr = (void FAR *)near_ptr;\r
+\r
+   if (check != 0)\r
+      if (far_ptr != ptr)\r
+         png_error(png_ptr, "segment lost in conversion");\r
+\r
+   return(near_ptr);\r
+}\r
+#   endif\r
+#   endif\r
+#endif /* PNG_WRITE_SUPPORTED */\r
index 7d02ad7..75bfe18 100644 (file)
-
-/* pngwrite.c - general routines to write a PNG file
- *
- * Last changed in libpng 1.2.27 [April 29, 2008]
- * For conditions of distribution and use, see copyright notice in png.h
- * Copyright (c) 1998-2008 Glenn Randers-Pehrson
- * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
- * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
- */
-
-/* get internal access to png.h */
-#define PNG_INTERNAL
-#include "png.h"
-#ifdef PNG_WRITE_SUPPORTED
-
-/* Writes all the PNG information.  This is the suggested way to use the
- * library.  If you have a new chunk to add, make a function to write it,
- * and put it in the correct location here.  If you want the chunk written
- * after the image data, put it in png_write_end().  I strongly encourage
- * you to supply a PNG_INFO_ flag, and check info_ptr->valid before writing
- * the chunk, as that will keep the code from breaking if you want to just
- * write a plain PNG file.  If you have long comments, I suggest writing
- * them in png_write_end(), and compressing them.
- */
-void PNGAPI
-png_write_info_before_PLTE(png_structp png_ptr, png_infop info_ptr)
-{
-   png_debug(1, "in png_write_info_before_PLTE\n");
-   if (png_ptr == NULL || info_ptr == NULL)
-      return;
-   if (!(png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE))
-   {
-   png_write_sig(png_ptr); /* write PNG signature */
-#if defined(PNG_MNG_FEATURES_SUPPORTED)
-   if((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE)&&(png_ptr->mng_features_permitted))
-   {
-      png_warning(png_ptr,"MNG features are not allowed in a PNG datastream");
-      png_ptr->mng_features_permitted=0;
-   }
-#endif
-   /* write IHDR information. */
-   png_write_IHDR(png_ptr, info_ptr->width, info_ptr->height,
-      info_ptr->bit_depth, info_ptr->color_type, info_ptr->compression_type,
-      info_ptr->filter_type,
-#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
-      info_ptr->interlace_type);
-#else
-      0);
-#endif
-   /* the rest of these check to see if the valid field has the appropriate
-      flag set, and if it does, writes the chunk. */
-#if defined(PNG_WRITE_gAMA_SUPPORTED)
-   if (info_ptr->valid & PNG_INFO_gAMA)
-   {
-#  ifdef PNG_FLOATING_POINT_SUPPORTED
-      png_write_gAMA(png_ptr, info_ptr->gamma);
-#else
-#ifdef PNG_FIXED_POINT_SUPPORTED
-      png_write_gAMA_fixed(png_ptr, info_ptr->int_gamma);
-#  endif
-#endif
-   }
-#endif
-#if defined(PNG_WRITE_sRGB_SUPPORTED)
-   if (info_ptr->valid & PNG_INFO_sRGB)
-      png_write_sRGB(png_ptr, (int)info_ptr->srgb_intent);
-#endif
-#if defined(PNG_WRITE_iCCP_SUPPORTED)
-   if (info_ptr->valid & PNG_INFO_iCCP)
-      png_write_iCCP(png_ptr, info_ptr->iccp_name, PNG_COMPRESSION_TYPE_BASE,
-                     info_ptr->iccp_profile, (int)info_ptr->iccp_proflen);
-#endif
-#if defined(PNG_WRITE_sBIT_SUPPORTED)
-   if (info_ptr->valid & PNG_INFO_sBIT)
-      png_write_sBIT(png_ptr, &(info_ptr->sig_bit), info_ptr->color_type);
-#endif
-#if defined(PNG_WRITE_cHRM_SUPPORTED)
-   if (info_ptr->valid & PNG_INFO_cHRM)
-   {
-#ifdef PNG_FLOATING_POINT_SUPPORTED
-      png_write_cHRM(png_ptr,
-         info_ptr->x_white, info_ptr->y_white,
-         info_ptr->x_red, info_ptr->y_red,
-         info_ptr->x_green, info_ptr->y_green,
-         info_ptr->x_blue, info_ptr->y_blue);
-#else
-#  ifdef PNG_FIXED_POINT_SUPPORTED
-      png_write_cHRM_fixed(png_ptr,
-         info_ptr->int_x_white, info_ptr->int_y_white,
-         info_ptr->int_x_red, info_ptr->int_y_red,
-         info_ptr->int_x_green, info_ptr->int_y_green,
-         info_ptr->int_x_blue, info_ptr->int_y_blue);
-#  endif
-#endif
-   }
-#endif
-#if defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED)
-   if (info_ptr->unknown_chunks_num)
-   {
-       png_unknown_chunk *up;
-
-       png_debug(5, "writing extra chunks\n");
-
-       for (up = info_ptr->unknown_chunks;
-            up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
-            up++)
-       {
-         int keep=png_handle_as_unknown(png_ptr, up->name);
-         if (keep != PNG_HANDLE_CHUNK_NEVER &&
-            up->location && !(up->location & PNG_HAVE_PLTE) &&
-            !(up->location & PNG_HAVE_IDAT) &&
-            ((up->name[3] & 0x20) || keep == PNG_HANDLE_CHUNK_ALWAYS ||
-            (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS)))
-         {
-            if (up->size == 0)
-               png_warning(png_ptr, "Writing zero-length unknown chunk");
-            png_write_chunk(png_ptr, up->name, up->data, up->size);
-         }
-       }
-   }
-#endif
-      png_ptr->mode |= PNG_WROTE_INFO_BEFORE_PLTE;
-   }
-}
-
-void PNGAPI
-png_write_info(png_structp png_ptr, png_infop info_ptr)
-{
-#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
-   int i;
-#endif
-
-   png_debug(1, "in png_write_info\n");
-
-   if (png_ptr == NULL || info_ptr == NULL)
-      return;
-
-   png_write_info_before_PLTE(png_ptr, info_ptr);
-
-   if (info_ptr->valid & PNG_INFO_PLTE)
-      png_write_PLTE(png_ptr, info_ptr->palette,
-         (png_uint_32)info_ptr->num_palette);
-   else if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
-      png_error(png_ptr, "Valid palette required for paletted images");
-
-#if defined(PNG_WRITE_tRNS_SUPPORTED)
-   if (info_ptr->valid & PNG_INFO_tRNS)
-      {
-#if defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED)
-         /* invert the alpha channel (in tRNS) */
-         if ((png_ptr->transformations & PNG_INVERT_ALPHA) &&
-            info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
-         {
-            int j;
-            for (j=0; j<(int)info_ptr->num_trans; j++)
-               info_ptr->trans[j] = (png_byte)(255 - info_ptr->trans[j]);
-         }
-#endif
-      png_write_tRNS(png_ptr, info_ptr->trans, &(info_ptr->trans_values),
-         info_ptr->num_trans, info_ptr->color_type);
-      }
-#endif
-#if defined(PNG_WRITE_bKGD_SUPPORTED)
-   if (info_ptr->valid & PNG_INFO_bKGD)
-      png_write_bKGD(png_ptr, &(info_ptr->background), info_ptr->color_type);
-#endif
-#if defined(PNG_WRITE_hIST_SUPPORTED)
-   if (info_ptr->valid & PNG_INFO_hIST)
-      png_write_hIST(png_ptr, info_ptr->hist, info_ptr->num_palette);
-#endif
-#if defined(PNG_WRITE_oFFs_SUPPORTED)
-   if (info_ptr->valid & PNG_INFO_oFFs)
-      png_write_oFFs(png_ptr, info_ptr->x_offset, info_ptr->y_offset,
-         info_ptr->offset_unit_type);
-#endif
-#if defined(PNG_WRITE_pCAL_SUPPORTED)
-   if (info_ptr->valid & PNG_INFO_pCAL)
-      png_write_pCAL(png_ptr, info_ptr->pcal_purpose, info_ptr->pcal_X0,
-         info_ptr->pcal_X1, info_ptr->pcal_type, info_ptr->pcal_nparams,
-         info_ptr->pcal_units, info_ptr->pcal_params);
-#endif
-#if defined(PNG_WRITE_sCAL_SUPPORTED)
-   if (info_ptr->valid & PNG_INFO_sCAL)
-#if defined(PNG_FLOATING_POINT_SUPPORTED) && !defined(PNG_NO_STDIO)
-      png_write_sCAL(png_ptr, (int)info_ptr->scal_unit,
-          info_ptr->scal_pixel_width, info_ptr->scal_pixel_height);
-#else
-#ifdef PNG_FIXED_POINT_SUPPORTED
-      png_write_sCAL_s(png_ptr, (int)info_ptr->scal_unit,
-          info_ptr->scal_s_width, info_ptr->scal_s_height);
-#else
-      png_warning(png_ptr,
-          "png_write_sCAL not supported; sCAL chunk not written.");
-#endif
-#endif
-#endif
-#if defined(PNG_WRITE_pHYs_SUPPORTED)
-   if (info_ptr->valid & PNG_INFO_pHYs)
-      png_write_pHYs(png_ptr, info_ptr->x_pixels_per_unit,
-         info_ptr->y_pixels_per_unit, info_ptr->phys_unit_type);
-#endif
-#if defined(PNG_WRITE_tIME_SUPPORTED)
-   if (info_ptr->valid & PNG_INFO_tIME)
-   {
-      png_write_tIME(png_ptr, &(info_ptr->mod_time));
-      png_ptr->mode |= PNG_WROTE_tIME;
-   }
-#endif
-#if defined(PNG_WRITE_sPLT_SUPPORTED)
-   if (info_ptr->valid & PNG_INFO_sPLT)
-     for (i = 0; i < (int)info_ptr->splt_palettes_num; i++)
-       png_write_sPLT(png_ptr, info_ptr->splt_palettes + i);
-#endif
-#if defined(PNG_WRITE_TEXT_SUPPORTED)
-   /* Check to see if we need to write text chunks */
-   for (i = 0; i < info_ptr->num_text; i++)
-   {
-      png_debug2(2, "Writing header text chunk %d, type %d\n", i,
-         info_ptr->text[i].compression);
-      /* an internationalized chunk? */
-      if (info_ptr->text[i].compression > 0)
-      {
-#if defined(PNG_WRITE_iTXt_SUPPORTED)
-          /* write international chunk */
-          png_write_iTXt(png_ptr,
-                         info_ptr->text[i].compression,
-                         info_ptr->text[i].key,
-                         info_ptr->text[i].lang,
-                         info_ptr->text[i].lang_key,
-                         info_ptr->text[i].text);
-#else
-          png_warning(png_ptr, "Unable to write international text");
-#endif
-          /* Mark this chunk as written */
-          info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
-      }
-      /* If we want a compressed text chunk */
-      else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_zTXt)
-      {
-#if defined(PNG_WRITE_zTXt_SUPPORTED)
-         /* write compressed chunk */
-         png_write_zTXt(png_ptr, info_ptr->text[i].key,
-            info_ptr->text[i].text, 0,
-            info_ptr->text[i].compression);
-#else
-         png_warning(png_ptr, "Unable to write compressed text");
-#endif
-         /* Mark this chunk as written */
-         info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
-      }
-      else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
-      {
-#if defined(PNG_WRITE_tEXt_SUPPORTED)
-         /* write uncompressed chunk */
-         png_write_tEXt(png_ptr, info_ptr->text[i].key,
-                         info_ptr->text[i].text,
-                         0);
-#else
-         png_warning(png_ptr, "Unable to write uncompressed text");
-#endif
-         /* Mark this chunk as written */
-         info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
-      }
-   }
-#endif
-#if defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED)
-   if (info_ptr->unknown_chunks_num)
-   {
-       png_unknown_chunk *up;
-
-       png_debug(5, "writing extra chunks\n");
-
-       for (up = info_ptr->unknown_chunks;
-            up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
-            up++)
-       {
-         int keep=png_handle_as_unknown(png_ptr, up->name);
-         if (keep != PNG_HANDLE_CHUNK_NEVER &&
-            up->location && (up->location & PNG_HAVE_PLTE) &&
-            !(up->location & PNG_HAVE_IDAT) &&
-            ((up->name[3] & 0x20) || keep == PNG_HANDLE_CHUNK_ALWAYS ||
-            (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS)))
-         {
-            png_write_chunk(png_ptr, up->name, up->data, up->size);
-         }
-       }
-   }
-#endif
-}
-
-/* Writes the end of the PNG file.  If you don't want to write comments or
- * time information, you can pass NULL for info.  If you already wrote these
- * in png_write_info(), do not write them again here.  If you have long
- * comments, I suggest writing them here, and compressing them.
- */
-void PNGAPI
-png_write_end(png_structp png_ptr, png_infop info_ptr)
-{
-   png_debug(1, "in png_write_end\n");
-   if (png_ptr == NULL)
-      return;
-   if (!(png_ptr->mode & PNG_HAVE_IDAT))
-      png_error(png_ptr, "No IDATs written into file");
-
-   /* see if user wants us to write information chunks */
-   if (info_ptr != NULL)
-   {
-#if defined(PNG_WRITE_TEXT_SUPPORTED)
-      int i; /* local index variable */
-#endif
-#if defined(PNG_WRITE_tIME_SUPPORTED)
-      /* check to see if user has supplied a time chunk */
-      if ((info_ptr->valid & PNG_INFO_tIME) &&
-         !(png_ptr->mode & PNG_WROTE_tIME))
-         png_write_tIME(png_ptr, &(info_ptr->mod_time));
-#endif
-#if defined(PNG_WRITE_TEXT_SUPPORTED)
-      /* loop through comment chunks */
-      for (i = 0; i < info_ptr->num_text; i++)
-      {
-         png_debug2(2, "Writing trailer text chunk %d, type %d\n", i,
-            info_ptr->text[i].compression);
-         /* an internationalized chunk? */
-         if (info_ptr->text[i].compression > 0)
-         {
-#if defined(PNG_WRITE_iTXt_SUPPORTED)
-             /* write international chunk */
-             png_write_iTXt(png_ptr,
-                         info_ptr->text[i].compression,
-                         info_ptr->text[i].key,
-                         info_ptr->text[i].lang,
-                         info_ptr->text[i].lang_key,
-                         info_ptr->text[i].text);
-#else
-             png_warning(png_ptr, "Unable to write international text");
-#endif
-             /* Mark this chunk as written */
-             info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
-         }
-         else if (info_ptr->text[i].compression >= PNG_TEXT_COMPRESSION_zTXt)
-         {
-#if defined(PNG_WRITE_zTXt_SUPPORTED)
-            /* write compressed chunk */
-            png_write_zTXt(png_ptr, info_ptr->text[i].key,
-               info_ptr->text[i].text, 0,
-               info_ptr->text[i].compression);
-#else
-            png_warning(png_ptr, "Unable to write compressed text");
-#endif
-            /* Mark this chunk as written */
-            info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
-         }
-         else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
-         {
-#if defined(PNG_WRITE_tEXt_SUPPORTED)
-            /* write uncompressed chunk */
-            png_write_tEXt(png_ptr, info_ptr->text[i].key,
-               info_ptr->text[i].text, 0);
-#else
-            png_warning(png_ptr, "Unable to write uncompressed text");
-#endif
-
-            /* Mark this chunk as written */
-            info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
-         }
-      }
-#endif
-#if defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED)
-   if (info_ptr->unknown_chunks_num)
-   {
-       png_unknown_chunk *up;
-
-       png_debug(5, "writing extra chunks\n");
-
-       for (up = info_ptr->unknown_chunks;
-            up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
-            up++)
-       {
-         int keep=png_handle_as_unknown(png_ptr, up->name);
-         if (keep != PNG_HANDLE_CHUNK_NEVER &&
-            up->location && (up->location & PNG_AFTER_IDAT) &&
-            ((up->name[3] & 0x20) || keep == PNG_HANDLE_CHUNK_ALWAYS ||
-            (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS)))
-         {
-            png_write_chunk(png_ptr, up->name, up->data, up->size);
-         }
-       }
-   }
-#endif
-   }
-
-   png_ptr->mode |= PNG_AFTER_IDAT;
-
-   /* write end of PNG file */
-   png_write_IEND(png_ptr);
-}
-
-#if defined(PNG_WRITE_tIME_SUPPORTED)
-#if !defined(_WIN32_WCE)
-/* "time.h" functions are not supported on WindowsCE */
-void PNGAPI
-png_convert_from_struct_tm(png_timep ptime, struct tm FAR * ttime)
-{
-   png_debug(1, "in png_convert_from_struct_tm\n");
-   ptime->year = (png_uint_16)(1900 + ttime->tm_year);
-   ptime->month = (png_byte)(ttime->tm_mon + 1);
-   ptime->day = (png_byte)ttime->tm_mday;
-   ptime->hour = (png_byte)ttime->tm_hour;
-   ptime->minute = (png_byte)ttime->tm_min;
-   ptime->second = (png_byte)ttime->tm_sec;
-}
-
-void PNGAPI
-png_convert_from_time_t(png_timep ptime, time_t ttime)
-{
-   struct tm *tbuf;
-
-   png_debug(1, "in png_convert_from_time_t\n");
-   tbuf = gmtime(&ttime);
-   png_convert_from_struct_tm(ptime, tbuf);
-}
-#endif
-#endif
-
-/* Initialize png_ptr structure, and allocate any memory needed */
-png_structp PNGAPI
-png_create_write_struct(png_const_charp user_png_ver, png_voidp error_ptr,
-   png_error_ptr error_fn, png_error_ptr warn_fn)
-{
-#ifdef PNG_USER_MEM_SUPPORTED
-   return (png_create_write_struct_2(user_png_ver, error_ptr, error_fn,
-      warn_fn, png_voidp_NULL, png_malloc_ptr_NULL, png_free_ptr_NULL));
-}
-
-/* Alternate initialize png_ptr structure, and allocate any memory needed */
-png_structp PNGAPI
-png_create_write_struct_2(png_const_charp user_png_ver, png_voidp error_ptr,
-   png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
-   png_malloc_ptr malloc_fn, png_free_ptr free_fn)
-{
-#endif /* PNG_USER_MEM_SUPPORTED */
-   png_structp png_ptr;
-#ifdef PNG_SETJMP_SUPPORTED
-#ifdef USE_FAR_KEYWORD
-   jmp_buf jmpbuf;
-#endif
-#endif
-   int i;
-   png_debug(1, "in png_create_write_struct\n");
-#ifdef PNG_USER_MEM_SUPPORTED
-   png_ptr = (png_structp)png_create_struct_2(PNG_STRUCT_PNG,
-      (png_malloc_ptr)malloc_fn, (png_voidp)mem_ptr);
-#else
-   png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG);
-#endif /* PNG_USER_MEM_SUPPORTED */
-   if (png_ptr == NULL)
-      return (NULL);
-
-   /* added at libpng-1.2.6 */
-#ifdef PNG_SET_USER_LIMITS_SUPPORTED
-   png_ptr->user_width_max=PNG_USER_WIDTH_MAX;
-   png_ptr->user_height_max=PNG_USER_HEIGHT_MAX;
-#endif
-
-#ifdef PNG_SETJMP_SUPPORTED
-#ifdef USE_FAR_KEYWORD
-   if (setjmp(jmpbuf))
-#else
-   if (setjmp(png_ptr->jmpbuf))
-#endif
-   {
-      png_free(png_ptr, png_ptr->zbuf);
-      png_ptr->zbuf=NULL;
-      png_destroy_struct(png_ptr);
-      return (NULL);
-   }
-#ifdef USE_FAR_KEYWORD
-   png_memcpy(png_ptr->jmpbuf,jmpbuf,png_sizeof(jmp_buf));
-#endif
-#endif
-
-#ifdef PNG_USER_MEM_SUPPORTED
-   png_set_mem_fn(png_ptr, mem_ptr, malloc_fn, free_fn);
-#endif /* PNG_USER_MEM_SUPPORTED */
-   png_set_error_fn(png_ptr, error_ptr, error_fn, warn_fn);
-
-   if(user_png_ver)
-   {
-     i=0;
-     do
-     {
-       if(user_png_ver[i] != png_libpng_ver[i])
-          png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
-     } while (png_libpng_ver[i++]);
-   }
-
-   if (png_ptr->flags & PNG_FLAG_LIBRARY_MISMATCH)
-   {
-     /* Libpng 0.90 and later are binary incompatible with libpng 0.89, so
-      * we must recompile any applications that use any older library version.
-      * For versions after libpng 1.0, we will be compatible, so we need
-      * only check the first digit.
-      */
-     if (user_png_ver == NULL || user_png_ver[0] != png_libpng_ver[0] ||
-         (user_png_ver[0] == '1' && user_png_ver[2] != png_libpng_ver[2]) ||
-         (user_png_ver[0] == '0' && user_png_ver[2] < '9'))
-     {
-#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
-        char msg[80];
-        if (user_png_ver)
-        {
-          png_snprintf(msg, 80,
-             "Application was compiled with png.h from libpng-%.20s",
-             user_png_ver);
-          png_warning(png_ptr, msg);
-        }
-        png_snprintf(msg, 80,
-           "Application  is  running with png.c from libpng-%.20s",
-           png_libpng_ver);
-        png_warning(png_ptr, msg);
-#endif
-#ifdef PNG_ERROR_NUMBERS_SUPPORTED
-        png_ptr->flags=0;
-#endif
-        png_error(png_ptr,
-           "Incompatible libpng version in application and library");
-     }
-   }
-
-   /* initialize zbuf - compression buffer */
-   png_ptr->zbuf_size = PNG_ZBUF_SIZE;
-   png_ptr->zbuf = (png_bytep)png_malloc(png_ptr,
-      (png_uint_32)png_ptr->zbuf_size);
-
-   png_set_write_fn(png_ptr, png_voidp_NULL, png_rw_ptr_NULL,
-      png_flush_ptr_NULL);
-
-#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
-   png_set_filter_heuristics(png_ptr, PNG_FILTER_HEURISTIC_DEFAULT,
-      1, png_doublep_NULL, png_doublep_NULL);
-#endif
-
-#ifdef PNG_SETJMP_SUPPORTED
-/* Applications that neglect to set up their own setjmp() and then encounter
-   a png_error() will longjmp here.  Since the jmpbuf is then meaningless we
-   abort instead of returning. */
-#ifdef USE_FAR_KEYWORD
-   if (setjmp(jmpbuf))
-      PNG_ABORT();
-   png_memcpy(png_ptr->jmpbuf,jmpbuf,png_sizeof(jmp_buf));
-#else
-   if (setjmp(png_ptr->jmpbuf))
-      PNG_ABORT();
-#endif
-#endif
-   return (png_ptr);
-}
-
-/* Initialize png_ptr structure, and allocate any memory needed */
-#if defined(PNG_1_0_X) || defined(PNG_1_2_X)
-/* Deprecated. */
-#undef png_write_init
-void PNGAPI
-png_write_init(png_structp png_ptr)
-{
-   /* We only come here via pre-1.0.7-compiled applications */
-   png_write_init_2(png_ptr, "1.0.6 or earlier", 0, 0);
-}
-
-void PNGAPI
-png_write_init_2(png_structp png_ptr, png_const_charp user_png_ver,
-   png_size_t png_struct_size, png_size_t png_info_size)
-{
-   /* We only come here via pre-1.0.12-compiled applications */
-   if(png_ptr == NULL) return;
-#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
-   if(png_sizeof(png_struct) > png_struct_size ||
-      png_sizeof(png_info) > png_info_size)
-   {
-      char msg[80];
-      png_ptr->warning_fn=NULL;
-      if (user_png_ver)
-      {
-        png_snprintf(msg, 80,
-           "Application was compiled with png.h from libpng-%.20s",
-           user_png_ver);
-        png_warning(png_ptr, msg);
-      }
-      png_snprintf(msg, 80,
-         "Application  is  running with png.c from libpng-%.20s",
-         png_libpng_ver);
-      png_warning(png_ptr, msg);
-   }
-#endif
-   if(png_sizeof(png_struct) > png_struct_size)
-     {
-       png_ptr->error_fn=NULL;
-#ifdef PNG_ERROR_NUMBERS_SUPPORTED
-       png_ptr->flags=0;
-#endif
-       png_error(png_ptr,
-       "The png struct allocated by the application for writing is too small.");
-     }
-   if(png_sizeof(png_info) > png_info_size)
-     {
-       png_ptr->error_fn=NULL;
-#ifdef PNG_ERROR_NUMBERS_SUPPORTED
-       png_ptr->flags=0;
-#endif
-       png_error(png_ptr,
-       "The info struct allocated by the application for writing is too small.");
-     }
-   png_write_init_3(&png_ptr, user_png_ver, png_struct_size);
-}
-#endif /* PNG_1_0_X || PNG_1_2_X */
-
-
-void PNGAPI
-png_write_init_3(png_structpp ptr_ptr, png_const_charp user_png_ver,
-   png_size_t png_struct_size)
-{
-   png_structp png_ptr=*ptr_ptr;
-#ifdef PNG_SETJMP_SUPPORTED
-   jmp_buf tmp_jmp; /* to save current jump buffer */
-#endif
-
-   int i = 0;
-
-   if (png_ptr == NULL)
-      return;
-
-   do
-   {
-     if (user_png_ver[i] != png_libpng_ver[i])
-     {
-#ifdef PNG_LEGACY_SUPPORTED
-       png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
-#else
-       png_ptr->warning_fn=NULL;
-       png_warning(png_ptr,
-     "Application uses deprecated png_write_init() and should be recompiled.");
-       break;
-#endif
-     }
-   } while (png_libpng_ver[i++]);
-
-   png_debug(1, "in png_write_init_3\n");
-
-#ifdef PNG_SETJMP_SUPPORTED
-   /* save jump buffer and error functions */
-   png_memcpy(tmp_jmp, png_ptr->jmpbuf, png_sizeof (jmp_buf));
-#endif
-
-   if (png_sizeof(png_struct) > png_struct_size)
-     {
-       png_destroy_struct(png_ptr);
-       png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG);
-       *ptr_ptr = png_ptr;
-     }
-
-   /* reset all variables to 0 */
-   png_memset(png_ptr, 0, png_sizeof (png_struct));
-
-   /* added at libpng-1.2.6 */
-#ifdef PNG_SET_USER_LIMITS_SUPPORTED
-   png_ptr->user_width_max=PNG_USER_WIDTH_MAX;
-   png_ptr->user_height_max=PNG_USER_HEIGHT_MAX;
-#endif
-
-#ifdef PNG_SETJMP_SUPPORTED
-   /* restore jump buffer */
-   png_memcpy(png_ptr->jmpbuf, tmp_jmp, png_sizeof (jmp_buf));
-#endif
-
-   png_set_write_fn(png_ptr, png_voidp_NULL, png_rw_ptr_NULL,
-      png_flush_ptr_NULL);
-
-   /* initialize zbuf - compression buffer */
-   png_ptr->zbuf_size = PNG_ZBUF_SIZE;
-   png_ptr->zbuf = (png_bytep)png_malloc(png_ptr,
-      (png_uint_32)png_ptr->zbuf_size);
-
-#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
-   png_set_filter_heuristics(png_ptr, PNG_FILTER_HEURISTIC_DEFAULT,
-      1, png_doublep_NULL, png_doublep_NULL);
-#endif
-}
-
-/* Write a few rows of image data.  If the image is interlaced,
- * either you will have to write the 7 sub images, or, if you
- * have called png_set_interlace_handling(), you will have to
- * "write" the image seven times.
- */
-void PNGAPI
-png_write_rows(png_structp png_ptr, png_bytepp row,
-   png_uint_32 num_rows)
-{
-   png_uint_32 i; /* row counter */
-   png_bytepp rp; /* row pointer */
-
-   png_debug(1, "in png_write_rows\n");
-
-   if (png_ptr == NULL)
-      return;
-
-   /* loop through the rows */
-   for (i = 0, rp = row; i < num_rows; i++, rp++)
-   {
-      png_write_row(png_ptr, *rp);
-   }
-}
-
-/* Write the image.  You only need to call this function once, even
- * if you are writing an interlaced image.
- */
-void PNGAPI
-png_write_image(png_structp png_ptr, png_bytepp image)
-{
-   png_uint_32 i; /* row index */
-   int pass, num_pass; /* pass variables */
-   png_bytepp rp; /* points to current row */
-
-   if (png_ptr == NULL)
-      return;
-
-   png_debug(1, "in png_write_image\n");
-#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
-   /* intialize interlace handling.  If image is not interlaced,
-      this will set pass to 1 */
-   num_pass = png_set_interlace_handling(png_ptr);
-#else
-   num_pass = 1;
-#endif
-   /* loop through passes */
-   for (pass = 0; pass < num_pass; pass++)
-   {
-      /* loop through image */
-      for (i = 0, rp = image; i < png_ptr->height; i++, rp++)
-      {
-         png_write_row(png_ptr, *rp);
-      }
-   }
-}
-
-/* called by user to write a row of image data */
-void PNGAPI
-png_write_row(png_structp png_ptr, png_bytep row)
-{
-   if (png_ptr == NULL)
-      return;
-   png_debug2(1, "in png_write_row (row %ld, pass %d)\n",
-      png_ptr->row_number, png_ptr->pass);
-
-   /* initialize transformations and other stuff if first time */
-   if (png_ptr->row_number == 0 && png_ptr->pass == 0)
-   {
-   /* make sure we wrote the header info */
-   if (!(png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE))
-      png_error(png_ptr,
-         "png_write_info was never called before png_write_row.");
-
-   /* check for transforms that have been set but were defined out */
-#if !defined(PNG_WRITE_INVERT_SUPPORTED) && defined(PNG_READ_INVERT_SUPPORTED)
-   if (png_ptr->transformations & PNG_INVERT_MONO)
-      png_warning(png_ptr, "PNG_WRITE_INVERT_SUPPORTED is not defined.");
-#endif
-#if !defined(PNG_WRITE_FILLER_SUPPORTED) && defined(PNG_READ_FILLER_SUPPORTED)
-   if (png_ptr->transformations & PNG_FILLER)
-      png_warning(png_ptr, "PNG_WRITE_FILLER_SUPPORTED is not defined.");
-#endif
-#if !defined(PNG_WRITE_PACKSWAP_SUPPORTED) && defined(PNG_READ_PACKSWAP_SUPPORTED)
-   if (png_ptr->transformations & PNG_PACKSWAP)
-      png_warning(png_ptr, "PNG_WRITE_PACKSWAP_SUPPORTED is not defined.");
-#endif
-#if !defined(PNG_WRITE_PACK_SUPPORTED) && defined(PNG_READ_PACK_SUPPORTED)
-   if (png_ptr->transformations & PNG_PACK)
-      png_warning(png_ptr, "PNG_WRITE_PACK_SUPPORTED is not defined.");
-#endif
-#if !defined(PNG_WRITE_SHIFT_SUPPORTED) && defined(PNG_READ_SHIFT_SUPPORTED)
-   if (png_ptr->transformations & PNG_SHIFT)
-      png_warning(png_ptr, "PNG_WRITE_SHIFT_SUPPORTED is not defined.");
-#endif
-#if !defined(PNG_WRITE_BGR_SUPPORTED) && defined(PNG_READ_BGR_SUPPORTED)
-   if (png_ptr->transformations & PNG_BGR)
-      png_warning(png_ptr, "PNG_WRITE_BGR_SUPPORTED is not defined.");
-#endif
-#if !defined(PNG_WRITE_SWAP_SUPPORTED) && defined(PNG_READ_SWAP_SUPPORTED)
-   if (png_ptr->transformations & PNG_SWAP_BYTES)
-      png_warning(png_ptr, "PNG_WRITE_SWAP_SUPPORTED is not defined.");
-#endif
-
-      png_write_start_row(png_ptr);
-   }
-
-#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
-   /* if interlaced and not interested in row, return */
-   if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
-   {
-      switch (png_ptr->pass)
-      {
-         case 0:
-            if (png_ptr->row_number & 0x07)
-            {
-               png_write_finish_row(png_ptr);
-               return;
-            }
-            break;
-         case 1:
-            if ((png_ptr->row_number & 0x07) || png_ptr->width < 5)
-            {
-               png_write_finish_row(png_ptr);
-               return;
-            }
-            break;
-         case 2:
-            if ((png_ptr->row_number & 0x07) != 4)
-            {
-               png_write_finish_row(png_ptr);
-               return;
-            }
-            break;
-         case 3:
-            if ((png_ptr->row_number & 0x03) || png_ptr->width < 3)
-            {
-               png_write_finish_row(png_ptr);
-               return;
-            }
-            break;
-         case 4:
-            if ((png_ptr->row_number & 0x03) != 2)
-            {
-               png_write_finish_row(png_ptr);
-               return;
-            }
-            break;
-         case 5:
-            if ((png_ptr->row_number & 0x01) || png_ptr->width < 2)
-            {
-               png_write_finish_row(png_ptr);
-               return;
-            }
-            break;
-         case 6:
-            if (!(png_ptr->row_number & 0x01))
-            {
-               png_write_finish_row(png_ptr);
-               return;
-            }
-            break;
-      }
-   }
-#endif
-
-   /* set up row info for transformations */
-   png_ptr->row_info.color_type = png_ptr->color_type;
-   png_ptr->row_info.width = png_ptr->usr_width;
-   png_ptr->row_info.channels = png_ptr->usr_channels;
-   png_ptr->row_info.bit_depth = png_ptr->usr_bit_depth;
-   png_ptr->row_info.pixel_depth = (png_byte)(png_ptr->row_info.bit_depth *
-      png_ptr->row_info.channels);
-
-   png_ptr->row_info.rowbytes = PNG_ROWBYTES(png_ptr->row_info.pixel_depth,
-      png_ptr->row_info.width);
-
-   png_debug1(3, "row_info->color_type = %d\n", png_ptr->row_info.color_type);
-   png_debug1(3, "row_info->width = %lu\n", png_ptr->row_info.width);
-   png_debug1(3, "row_info->channels = %d\n", png_ptr->row_info.channels);
-   png_debug1(3, "row_info->bit_depth = %d\n", png_ptr->row_info.bit_depth);
-   png_debug1(3, "row_info->pixel_depth = %d\n", png_ptr->row_info.pixel_depth);
-   png_debug1(3, "row_info->rowbytes = %lu\n", png_ptr->row_info.rowbytes);
-
-   /* Copy user's row into buffer, leaving room for filter byte. */
-   png_memcpy_check(png_ptr, png_ptr->row_buf + 1, row,
-      png_ptr->row_info.rowbytes);
-
-#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
-   /* handle interlacing */
-   if (png_ptr->interlaced && png_ptr->pass < 6 &&
-      (png_ptr->transformations & PNG_INTERLACE))
-   {
-      png_do_write_interlace(&(png_ptr->row_info),
-         png_ptr->row_buf + 1, png_ptr->pass);
-      /* this should always get caught above, but still ... */
-      if (!(png_ptr->row_info.width))
-      {
-         png_write_finish_row(png_ptr);
-         return;
-      }
-   }
-#endif
-
-   /* handle other transformations */
-   if (png_ptr->transformations)
-      png_do_write_transformations(png_ptr);
-
-#if defined(PNG_MNG_FEATURES_SUPPORTED)
-   /* Write filter_method 64 (intrapixel differencing) only if
-    * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
-    * 2. Libpng did not write a PNG signature (this filter_method is only
-    *    used in PNG datastreams that are embedded in MNG datastreams) and
-    * 3. The application called png_permit_mng_features with a mask that
-    *    included PNG_FLAG_MNG_FILTER_64 and
-    * 4. The filter_method is 64 and
-    * 5. The color_type is RGB or RGBA
-    */
-   if((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
-      (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING))
-   {
-      /* Intrapixel differencing */
-      png_do_write_intrapixel(&(png_ptr->row_info), png_ptr->row_buf + 1);
-   }
-#endif
-
-   /* Find a filter if necessary, filter the row and write it out. */
-   png_write_find_filter(png_ptr, &(png_ptr->row_info));
-
-   if (png_ptr->write_row_fn != NULL)
-      (*(png_ptr->write_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
-}
-
-#if defined(PNG_WRITE_FLUSH_SUPPORTED)
-/* Set the automatic flush interval or 0 to turn flushing off */
-void PNGAPI
-png_set_flush(png_structp png_ptr, int nrows)
-{
-   png_debug(1, "in png_set_flush\n");
-   if (png_ptr == NULL)
-      return;
-   png_ptr->flush_dist = (nrows < 0 ? 0 : nrows);
-}
-
-/* flush the current output buffers now */
-void PNGAPI
-png_write_flush(png_structp png_ptr)
-{
-   int wrote_IDAT;
-
-   png_debug(1, "in png_write_flush\n");
-   if (png_ptr == NULL)
-      return;
-   /* We have already written out all of the data */
-   if (png_ptr->row_number >= png_ptr->num_rows)
-     return;
-
-   do
-   {
-      int ret;
-
-      /* compress the data */
-      ret = deflate(&png_ptr->zstream, Z_SYNC_FLUSH);
-      wrote_IDAT = 0;
-
-      /* check for compression errors */
-      if (ret != Z_OK)
-      {
-         if (png_ptr->zstream.msg != NULL)
-            png_error(png_ptr, png_ptr->zstream.msg);
-         else
-            png_error(png_ptr, "zlib error");
-      }
-
-      if (!(png_ptr->zstream.avail_out))
-      {
-         /* write the IDAT and reset the zlib output buffer */
-         png_write_IDAT(png_ptr, png_ptr->zbuf,
-                        png_ptr->zbuf_size);
-         png_ptr->zstream.next_out = png_ptr->zbuf;
-         png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
-         wrote_IDAT = 1;
-      }
-   } while(wrote_IDAT == 1);
-
-   /* If there is any data left to be output, write it into a new IDAT */
-   if (png_ptr->zbuf_size != png_ptr->zstream.avail_out)
-   {
-      /* write the IDAT and reset the zlib output buffer */
-      png_write_IDAT(png_ptr, png_ptr->zbuf,
-                     png_ptr->zbuf_size - png_ptr->zstream.avail_out);
-      png_ptr->zstream.next_out = png_ptr->zbuf;
-      png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
-   }
-   png_ptr->flush_rows = 0;
-   png_flush(png_ptr);
-}
-#endif /* PNG_WRITE_FLUSH_SUPPORTED */
-
-/* free all memory used by the write */
-void PNGAPI
-png_destroy_write_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr)
-{
-   png_structp png_ptr = NULL;
-   png_infop info_ptr = NULL;
-#ifdef PNG_USER_MEM_SUPPORTED
-   png_free_ptr free_fn = NULL;
-   png_voidp mem_ptr = NULL;
-#endif
-
-   png_debug(1, "in png_destroy_write_struct\n");
-   if (png_ptr_ptr != NULL)
-   {
-      png_ptr = *png_ptr_ptr;
-#ifdef PNG_USER_MEM_SUPPORTED
-      free_fn = png_ptr->free_fn;
-      mem_ptr = png_ptr->mem_ptr;
-#endif
-   }
-
-#ifdef PNG_USER_MEM_SUPPORTED
-   if (png_ptr != NULL)
-   {
-      free_fn = png_ptr->free_fn;
-      mem_ptr = png_ptr->mem_ptr;
-   }
-#endif
-
-   if (info_ptr_ptr != NULL)
-      info_ptr = *info_ptr_ptr;
-
-   if (info_ptr != NULL)
-   {
-      if (png_ptr != NULL)
-      {
-        png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
-
-#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
-        if (png_ptr->num_chunk_list)
-        {
-           png_free(png_ptr, png_ptr->chunk_list);
-           png_ptr->chunk_list=NULL;
-           png_ptr->num_chunk_list=0;
-        }
-#endif
-      }
-
-#ifdef PNG_USER_MEM_SUPPORTED
-      png_destroy_struct_2((png_voidp)info_ptr, (png_free_ptr)free_fn,
-         (png_voidp)mem_ptr);
-#else
-      png_destroy_struct((png_voidp)info_ptr);
-#endif
-      *info_ptr_ptr = NULL;
-   }
-
-   if (png_ptr != NULL)
-   {
-      png_write_destroy(png_ptr);
-#ifdef PNG_USER_MEM_SUPPORTED
-      png_destroy_struct_2((png_voidp)png_ptr, (png_free_ptr)free_fn,
-         (png_voidp)mem_ptr);
-#else
-      png_destroy_struct((png_voidp)png_ptr);
-#endif
-      *png_ptr_ptr = NULL;
-   }
-}
-
-
-/* Free any memory used in png_ptr struct (old method) */
-void /* PRIVATE */
-png_write_destroy(png_structp png_ptr)
-{
-#ifdef PNG_SETJMP_SUPPORTED
-   jmp_buf tmp_jmp; /* save jump buffer */
-#endif
-   png_error_ptr error_fn;
-   png_error_ptr warning_fn;
-   png_voidp error_ptr;
-#ifdef PNG_USER_MEM_SUPPORTED
-   png_free_ptr free_fn;
-#endif
-
-   png_debug(1, "in png_write_destroy\n");
-   /* free any memory zlib uses */
-   deflateEnd(&png_ptr->zstream);
-
-   /* free our memory.  png_free checks NULL for us. */
-   png_free(png_ptr, png_ptr->zbuf);
-   png_free(png_ptr, png_ptr->row_buf);
-#ifndef PNG_NO_WRITE_FILTERING
-   png_free(png_ptr, png_ptr->prev_row);
-   png_free(png_ptr, png_ptr->sub_row);
-   png_free(png_ptr, png_ptr->up_row);
-   png_free(png_ptr, png_ptr->avg_row);
-   png_free(png_ptr, png_ptr->paeth_row);
-#endif
-
-#if defined(PNG_TIME_RFC1123_SUPPORTED)
-   png_free(png_ptr, png_ptr->time_buffer);
-#endif
-
-#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
-   png_free(png_ptr, png_ptr->prev_filters);
-   png_free(png_ptr, png_ptr->filter_weights);
-   png_free(png_ptr, png_ptr->inv_filter_weights);
-   png_free(png_ptr, png_ptr->filter_costs);
-   png_free(png_ptr, png_ptr->inv_filter_costs);
-#endif
-
-#ifdef PNG_SETJMP_SUPPORTED
-   /* reset structure */
-   png_memcpy(tmp_jmp, png_ptr->jmpbuf, png_sizeof (jmp_buf));
-#endif
-
-   error_fn = png_ptr->error_fn;
-   warning_fn = png_ptr->warning_fn;
-   error_ptr = png_ptr->error_ptr;
-#ifdef PNG_USER_MEM_SUPPORTED
-   free_fn = png_ptr->free_fn;
-#endif
-
-   png_memset(png_ptr, 0, png_sizeof (png_struct));
-
-   png_ptr->error_fn = error_fn;
-   png_ptr->warning_fn = warning_fn;
-   png_ptr->error_ptr = error_ptr;
-#ifdef PNG_USER_MEM_SUPPORTED
-   png_ptr->free_fn = free_fn;
-#endif
-
-#ifdef PNG_SETJMP_SUPPORTED
-   png_memcpy(png_ptr->jmpbuf, tmp_jmp, png_sizeof (jmp_buf));
-#endif
-}
-
-/* Allow the application to select one or more row filters to use. */
-void PNGAPI
-png_set_filter(png_structp png_ptr, int method, int filters)
-{
-   png_debug(1, "in png_set_filter\n");
-   if (png_ptr == NULL)
-      return;
-#if defined(PNG_MNG_FEATURES_SUPPORTED)
-   if((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
-      (method == PNG_INTRAPIXEL_DIFFERENCING))
-         method = PNG_FILTER_TYPE_BASE;
-#endif
-   if (method == PNG_FILTER_TYPE_BASE)
-   {
-      switch (filters & (PNG_ALL_FILTERS | 0x07))
-      {
-#ifndef PNG_NO_WRITE_FILTER
-         case 5:
-         case 6:
-         case 7: png_warning(png_ptr, "Unknown row filter for method 0");
-#endif /* PNG_NO_WRITE_FILTER */
-         case PNG_FILTER_VALUE_NONE:
-              png_ptr->do_filter=PNG_FILTER_NONE; break;
-#ifndef PNG_NO_WRITE_FILTER
-         case PNG_FILTER_VALUE_SUB:
-              png_ptr->do_filter=PNG_FILTER_SUB; break;
-         case PNG_FILTER_VALUE_UP:
-              png_ptr->do_filter=PNG_FILTER_UP; break;
-         case PNG_FILTER_VALUE_AVG:
-              png_ptr->do_filter=PNG_FILTER_AVG; break;
-         case PNG_FILTER_VALUE_PAETH:
-              png_ptr->do_filter=PNG_FILTER_PAETH; break;
-         default: png_ptr->do_filter = (png_byte)filters; break;
-#else
-         default: png_warning(png_ptr, "Unknown row filter for method 0");
-#endif /* PNG_NO_WRITE_FILTER */
-      }
-
-      /* If we have allocated the row_buf, this means we have already started
-       * with the image and we should have allocated all of the filter buffers
-       * that have been selected.  If prev_row isn't already allocated, then
-       * it is too late to start using the filters that need it, since we
-       * will be missing the data in the previous row.  If an application
-       * wants to start and stop using particular filters during compression,
-       * it should start out with all of the filters, and then add and
-       * remove them after the start of compression.
-       */
-      if (png_ptr->row_buf != NULL)
-      {
-#ifndef PNG_NO_WRITE_FILTER
-         if ((png_ptr->do_filter & PNG_FILTER_SUB) && png_ptr->sub_row == NULL)
-         {
-            png_ptr->sub_row = (png_bytep)png_malloc(png_ptr,
-              (png_ptr->rowbytes + 1));
-            png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
-         }
-
-         if ((png_ptr->do_filter & PNG_FILTER_UP) && png_ptr->up_row == NULL)
-         {
-            if (png_ptr->prev_row == NULL)
-            {
-               png_warning(png_ptr, "Can't add Up filter after starting");
-               png_ptr->do_filter &= ~PNG_FILTER_UP;
-            }
-            else
-            {
-               png_ptr->up_row = (png_bytep)png_malloc(png_ptr,
-                  (png_ptr->rowbytes + 1));
-               png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
-            }
-         }
-
-         if ((png_ptr->do_filter & PNG_FILTER_AVG) && png_ptr->avg_row == NULL)
-         {
-            if (png_ptr->prev_row == NULL)
-            {
-               png_warning(png_ptr, "Can't add Average filter after starting");
-               png_ptr->do_filter &= ~PNG_FILTER_AVG;
-            }
-            else
-            {
-               png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
-                  (png_ptr->rowbytes + 1));
-               png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
-            }
-         }
-
-         if ((png_ptr->do_filter & PNG_FILTER_PAETH) &&
-             png_ptr->paeth_row == NULL)
-         {
-            if (png_ptr->prev_row == NULL)
-            {
-               png_warning(png_ptr, "Can't add Paeth filter after starting");
-               png_ptr->do_filter &= (png_byte)(~PNG_FILTER_PAETH);
-            }
-            else
-            {
-               png_ptr->paeth_row = (png_bytep)png_malloc(png_ptr,
-                  (png_ptr->rowbytes + 1));
-               png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
-            }
-         }
-
-         if (png_ptr->do_filter == PNG_NO_FILTERS)
-#endif /* PNG_NO_WRITE_FILTER */
-            png_ptr->do_filter = PNG_FILTER_NONE;
-      }
-   }
-   else
-      png_error(png_ptr, "Unknown custom filter method");
-}
-
-/* This allows us to influence the way in which libpng chooses the "best"
- * filter for the current scanline.  While the "minimum-sum-of-absolute-
- * differences metric is relatively fast and effective, there is some
- * question as to whether it can be improved upon by trying to keep the
- * filtered data going to zlib more consistent, hopefully resulting in
- * better compression.
- */
-#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)      /* GRR 970116 */
-void PNGAPI
-png_set_filter_heuristics(png_structp png_ptr, int heuristic_method,
-   int num_weights, png_doublep filter_weights,
-   png_doublep filter_costs)
-{
-   int i;
-
-   png_debug(1, "in png_set_filter_heuristics\n");
-   if (png_ptr == NULL)
-      return;
-   if (heuristic_method >= PNG_FILTER_HEURISTIC_LAST)
-   {
-      png_warning(png_ptr, "Unknown filter heuristic method");
-      return;
-   }
-
-   if (heuristic_method == PNG_FILTER_HEURISTIC_DEFAULT)
-   {
-      heuristic_method = PNG_FILTER_HEURISTIC_UNWEIGHTED;
-   }
-
-   if (num_weights < 0 || filter_weights == NULL ||
-      heuristic_method == PNG_FILTER_HEURISTIC_UNWEIGHTED)
-   {
-      num_weights = 0;
-   }
-
-   png_ptr->num_prev_filters = (png_byte)num_weights;
-   png_ptr->heuristic_method = (png_byte)heuristic_method;
-
-   if (num_weights > 0)
-   {
-      if (png_ptr->prev_filters == NULL)
-      {
-         png_ptr->prev_filters = (png_bytep)png_malloc(png_ptr,
-            (png_uint_32)(png_sizeof(png_byte) * num_weights));
-
-         /* To make sure that the weighting starts out fairly */
-         for (i = 0; i < num_weights; i++)
-         {
-            png_ptr->prev_filters[i] = 255;
-         }
-      }
-
-      if (png_ptr->filter_weights == NULL)
-      {
-         png_ptr->filter_weights = (png_uint_16p)png_malloc(png_ptr,
-            (png_uint_32)(png_sizeof(png_uint_16) * num_weights));
-
-         png_ptr->inv_filter_weights = (png_uint_16p)png_malloc(png_ptr,
-            (png_uint_32)(png_sizeof(png_uint_16) * num_weights));
-         for (i = 0; i < num_weights; i++)
-         {
-            png_ptr->inv_filter_weights[i] =
-            png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
-         }
-      }
-
-      for (i = 0; i < num_weights; i++)
-      {
-         if (filter_weights[i] < 0.0)
-         {
-            png_ptr->inv_filter_weights[i] =
-            png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
-         }
-         else
-         {
-            png_ptr->inv_filter_weights[i] =
-               (png_uint_16)((double)PNG_WEIGHT_FACTOR*filter_weights[i]+0.5);
-            png_ptr->filter_weights[i] =
-               (png_uint_16)((double)PNG_WEIGHT_FACTOR/filter_weights[i]+0.5);
-         }
-      }
-   }
-
-   /* If, in the future, there are other filter methods, this would
-    * need to be based on png_ptr->filter.
-    */
-   if (png_ptr->filter_costs == NULL)
-   {
-      png_ptr->filter_costs = (png_uint_16p)png_malloc(png_ptr,
-         (png_uint_32)(png_sizeof(png_uint_16) * PNG_FILTER_VALUE_LAST));
-
-      png_ptr->inv_filter_costs = (png_uint_16p)png_malloc(png_ptr,
-         (png_uint_32)(png_sizeof(png_uint_16) * PNG_FILTER_VALUE_LAST));
-
-      for (i = 0; i < PNG_FILTER_VALUE_LAST; i++)
-      {
-         png_ptr->inv_filter_costs[i] =
-         png_ptr->filter_costs[i] = PNG_COST_FACTOR;
-      }
-   }
-
-   /* Here is where we set the relative costs of the different filters.  We
-    * should take the desired compression level into account when setting
-    * the costs, so that Paeth, for instance, has a high relative cost at low
-    * compression levels, while it has a lower relative cost at higher
-    * compression settings.  The filter types are in order of increasing
-    * relative cost, so it would be possible to do this with an algorithm.
-    */
-   for (i = 0; i < PNG_FILTER_VALUE_LAST; i++)
-   {
-      if (filter_costs == NULL || filter_costs[i] < 0.0)
-      {
-         png_ptr->inv_filter_costs[i] =
-         png_ptr->filter_costs[i] = PNG_COST_FACTOR;
-      }
-      else if (filter_costs[i] >= 1.0)
-      {
-         png_ptr->inv_filter_costs[i] =
-            (png_uint_16)((double)PNG_COST_FACTOR / filter_costs[i] + 0.5);
-         png_ptr->filter_costs[i] =
-            (png_uint_16)((double)PNG_COST_FACTOR * filter_costs[i] + 0.5);
-      }
-   }
-}
-#endif /* PNG_WRITE_WEIGHTED_FILTER_SUPPORTED */
-
-void PNGAPI
-png_set_compression_level(png_structp png_ptr, int level)
-{
-   png_debug(1, "in png_set_compression_level\n");
-   if (png_ptr == NULL)
-      return;
-   png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_LEVEL;
-   png_ptr->zlib_level = level;
-}
-
-void PNGAPI
-png_set_compression_mem_level(png_structp png_ptr, int mem_level)
-{
-   png_debug(1, "in png_set_compression_mem_level\n");
-   if (png_ptr == NULL)
-      return;
-   png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL;
-   png_ptr->zlib_mem_level = mem_level;
-}
-
-void PNGAPI
-png_set_compression_strategy(png_structp png_ptr, int strategy)
-{
-   png_debug(1, "in png_set_compression_strategy\n");
-   if (png_ptr == NULL)
-      return;
-   png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_STRATEGY;
-   png_ptr->zlib_strategy = strategy;
-}
-
-void PNGAPI
-png_set_compression_window_bits(png_structp png_ptr, int window_bits)
-{
-   if (png_ptr == NULL)
-      return;
-   if (window_bits > 15)
-      png_warning(png_ptr, "Only compression windows <= 32k supported by PNG");
-   else if (window_bits < 8)
-      png_warning(png_ptr, "Only compression windows >= 256 supported by PNG");
-#ifndef WBITS_8_OK
-   /* avoid libpng bug with 256-byte windows */
-   if (window_bits == 8)
-     {
-       png_warning(png_ptr, "Compression window is being reset to 512");
-       window_bits=9;
-     }
-#endif
-   png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS;
-   png_ptr->zlib_window_bits = window_bits;
-}
-
-void PNGAPI
-png_set_compression_method(png_structp png_ptr, int method)
-{
-   png_debug(1, "in png_set_compression_method\n");
-   if (png_ptr == NULL)
-      return;
-   if (method != 8)
-      png_warning(png_ptr, "Only compression method 8 is supported by PNG");
-   png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_METHOD;
-   png_ptr->zlib_method = method;
-}
-
-void PNGAPI
-png_set_write_status_fn(png_structp png_ptr, png_write_status_ptr write_row_fn)
-{
-   if (png_ptr == NULL)
-      return;
-   png_ptr->write_row_fn = write_row_fn;
-}
-
-#if defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
-void PNGAPI
-png_set_write_user_transform_fn(png_structp png_ptr, png_user_transform_ptr
-   write_user_transform_fn)
-{
-   png_debug(1, "in png_set_write_user_transform_fn\n");
-   if (png_ptr == NULL)
-      return;
-   png_ptr->transformations |= PNG_USER_TRANSFORM;
-   png_ptr->write_user_transform_fn = write_user_transform_fn;
-}
-#endif
-
-
-#if defined(PNG_INFO_IMAGE_SUPPORTED)
-void PNGAPI
-png_write_png(png_structp png_ptr, png_infop info_ptr,
-              int transforms, voidp params)
-{
-   if (png_ptr == NULL || info_ptr == NULL)
-      return;
-#if defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED)
-   /* invert the alpha channel from opacity to transparency */
-   if (transforms & PNG_TRANSFORM_INVERT_ALPHA)
-       png_set_invert_alpha(png_ptr);
-#endif
-
-   /* Write the file header information. */
-   png_write_info(png_ptr, info_ptr);
-
-   /* ------ these transformations don't touch the info structure ------- */
-
-#if defined(PNG_WRITE_INVERT_SUPPORTED)
-   /* invert monochrome pixels */
-   if (transforms & PNG_TRANSFORM_INVERT_MONO)
-       png_set_invert_mono(png_ptr);
-#endif
-
-#if defined(PNG_WRITE_SHIFT_SUPPORTED)
-   /* Shift the pixels up to a legal bit depth and fill in
-    * as appropriate to correctly scale the image.
-    */
-   if ((transforms & PNG_TRANSFORM_SHIFT)
-               && (info_ptr->valid & PNG_INFO_sBIT))
-       png_set_shift(png_ptr, &info_ptr->sig_bit);
-#endif
-
-#if defined(PNG_WRITE_PACK_SUPPORTED)
-   /* pack pixels into bytes */
-   if (transforms & PNG_TRANSFORM_PACKING)
-       png_set_packing(png_ptr);
-#endif
-
-#if defined(PNG_WRITE_SWAP_ALPHA_SUPPORTED)
-   /* swap location of alpha bytes from ARGB to RGBA */
-   if (transforms & PNG_TRANSFORM_SWAP_ALPHA)
-       png_set_swap_alpha(png_ptr);
-#endif
-
-#if defined(PNG_WRITE_FILLER_SUPPORTED)
-   /* Get rid of filler (OR ALPHA) bytes, pack XRGB/RGBX/ARGB/RGBA into
-    * RGB (4 channels -> 3 channels). The second parameter is not used.
-    */
-   if (transforms & PNG_TRANSFORM_STRIP_FILLER)
-       png_set_filler(png_ptr, 0, PNG_FILLER_BEFORE);
-#endif
-
-#if defined(PNG_WRITE_BGR_SUPPORTED)
-   /* flip BGR pixels to RGB */
-   if (transforms & PNG_TRANSFORM_BGR)
-       png_set_bgr(png_ptr);
-#endif
-
-#if defined(PNG_WRITE_SWAP_SUPPORTED)
-   /* swap bytes of 16-bit files to most significant byte first */
-   if (transforms & PNG_TRANSFORM_SWAP_ENDIAN)
-       png_set_swap(png_ptr);
-#endif
-
-#if defined(PNG_WRITE_PACKSWAP_SUPPORTED)
-   /* swap bits of 1, 2, 4 bit packed pixel formats */
-   if (transforms & PNG_TRANSFORM_PACKSWAP)
-       png_set_packswap(png_ptr);
-#endif
-
-   /* ----------------------- end of transformations ------------------- */
-
-   /* write the bits */
-   if (info_ptr->valid & PNG_INFO_IDAT)
-       png_write_image(png_ptr, info_ptr->row_pointers);
-
-   /* It is REQUIRED to call this to finish writing the rest of the file */
-   png_write_end(png_ptr, info_ptr);
-
-   transforms = transforms; /* quiet compiler warnings */
-   params = params;
-}
-#endif
-#endif /* PNG_WRITE_SUPPORTED */
+\r
+/* pngwrite.c - general routines to write a PNG file\r
+ *\r
+ * Last changed in libpng 1.4.0 [January 3, 2010]\r
+ * Copyright (c) 1998-2010 Glenn Randers-Pehrson\r
+ * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)\r
+ * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)\r
+ *\r
+ * This code is released under the libpng license.\r
+ * For conditions of distribution and use, see the disclaimer\r
+ * and license in png.h\r
+ */\r
+\r
+/* Get internal access to png.h */\r
+#define PNG_NO_PEDANTIC_WARNINGS\r
+#include "png.h"\r
+#ifdef PNG_WRITE_SUPPORTED\r
+#include "pngpriv.h"\r
+\r
+/* Writes all the PNG information.  This is the suggested way to use the\r
+ * library.  If you have a new chunk to add, make a function to write it,\r
+ * and put it in the correct location here.  If you want the chunk written\r
+ * after the image data, put it in png_write_end().  I strongly encourage\r
+ * you to supply a PNG_INFO_ flag, and check info_ptr->valid before writing\r
+ * the chunk, as that will keep the code from breaking if you want to just\r
+ * write a plain PNG file.  If you have long comments, I suggest writing\r
+ * them in png_write_end(), and compressing them.\r
+ */\r
+void PNGAPI\r
+png_write_info_before_PLTE(png_structp png_ptr, png_infop info_ptr)\r
+{\r
+   png_debug(1, "in png_write_info_before_PLTE");\r
+\r
+   if (png_ptr == NULL || info_ptr == NULL)\r
+      return;\r
+   if (!(png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE))\r
+   {\r
+   /* Write PNG signature */\r
+   png_write_sig(png_ptr);\r
+#ifdef PNG_MNG_FEATURES_SUPPORTED\r
+   if ((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE) && \\r
+      (png_ptr->mng_features_permitted))\r
+   {\r
+      png_warning(png_ptr, "MNG features are not allowed in a PNG datastream");\r
+      png_ptr->mng_features_permitted = 0;\r
+   }\r
+#endif\r
+   /* Write IHDR information. */\r
+   png_write_IHDR(png_ptr, info_ptr->width, info_ptr->height,\r
+      info_ptr->bit_depth, info_ptr->color_type, info_ptr->compression_type,\r
+      info_ptr->filter_type,\r
+#ifdef PNG_WRITE_INTERLACING_SUPPORTED\r
+      info_ptr->interlace_type);\r
+#else\r
+      0);\r
+#endif\r
+   /* The rest of these check to see if the valid field has the appropriate\r
+    * flag set, and if it does, writes the chunk.\r
+    */\r
+#ifdef PNG_WRITE_gAMA_SUPPORTED\r
+   if (info_ptr->valid & PNG_INFO_gAMA)\r
+   {\r
+#  ifdef PNG_FLOATING_POINT_SUPPORTED\r
+      png_write_gAMA(png_ptr, info_ptr->gamma);\r
+#else\r
+#ifdef PNG_FIXED_POINT_SUPPORTED\r
+      png_write_gAMA_fixed(png_ptr, info_ptr->int_gamma);\r
+#  endif\r
+#endif\r
+   }\r
+#endif\r
+#ifdef PNG_WRITE_sRGB_SUPPORTED\r
+   if (info_ptr->valid & PNG_INFO_sRGB)\r
+      png_write_sRGB(png_ptr, (int)info_ptr->srgb_intent);\r
+#endif\r
+#ifdef PNG_WRITE_iCCP_SUPPORTED\r
+   if (info_ptr->valid & PNG_INFO_iCCP)\r
+      png_write_iCCP(png_ptr, info_ptr->iccp_name, PNG_COMPRESSION_TYPE_BASE,\r
+                     info_ptr->iccp_profile, (int)info_ptr->iccp_proflen);\r
+#endif\r
+#ifdef PNG_WRITE_sBIT_SUPPORTED\r
+   if (info_ptr->valid & PNG_INFO_sBIT)\r
+      png_write_sBIT(png_ptr, &(info_ptr->sig_bit), info_ptr->color_type);\r
+#endif\r
+#ifdef PNG_WRITE_cHRM_SUPPORTED\r
+   if (info_ptr->valid & PNG_INFO_cHRM)\r
+   {\r
+#ifdef PNG_FLOATING_POINT_SUPPORTED\r
+      png_write_cHRM(png_ptr,\r
+         info_ptr->x_white, info_ptr->y_white,\r
+         info_ptr->x_red, info_ptr->y_red,\r
+         info_ptr->x_green, info_ptr->y_green,\r
+         info_ptr->x_blue, info_ptr->y_blue);\r
+#else\r
+#  ifdef PNG_FIXED_POINT_SUPPORTED\r
+      png_write_cHRM_fixed(png_ptr,\r
+         info_ptr->int_x_white, info_ptr->int_y_white,\r
+         info_ptr->int_x_red, info_ptr->int_y_red,\r
+         info_ptr->int_x_green, info_ptr->int_y_green,\r
+         info_ptr->int_x_blue, info_ptr->int_y_blue);\r
+#  endif\r
+#endif\r
+   }\r
+#endif\r
+#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED\r
+   if (info_ptr->unknown_chunks_num)\r
+   {\r
+      png_unknown_chunk *up;\r
+\r
+      png_debug(5, "writing extra chunks");\r
+\r
+      for (up = info_ptr->unknown_chunks;\r
+           up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;\r
+           up++)\r
+      {\r
+         int keep = png_handle_as_unknown(png_ptr, up->name);\r
+         if (keep != PNG_HANDLE_CHUNK_NEVER &&\r
+            up->location && !(up->location & PNG_HAVE_PLTE) &&\r
+            !(up->location & PNG_HAVE_IDAT) &&\r
+            ((up->name[3] & 0x20) || keep == PNG_HANDLE_CHUNK_ALWAYS ||\r
+            (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS)))\r
+         {\r
+            if (up->size == 0)\r
+               png_warning(png_ptr, "Writing zero-length unknown chunk");\r
+            png_write_chunk(png_ptr, up->name, up->data, up->size);\r
+         }\r
+      }\r
+   }\r
+#endif\r
+      png_ptr->mode |= PNG_WROTE_INFO_BEFORE_PLTE;\r
+   }\r
+}\r
+\r
+void PNGAPI\r
+png_write_info(png_structp png_ptr, png_infop info_ptr)\r
+{\r
+#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)\r
+   int i;\r
+#endif\r
+\r
+   png_debug(1, "in png_write_info");\r
+\r
+   if (png_ptr == NULL || info_ptr == NULL)\r
+      return;\r
+\r
+   png_write_info_before_PLTE(png_ptr, info_ptr);\r
+\r
+   if (info_ptr->valid & PNG_INFO_PLTE)\r
+      png_write_PLTE(png_ptr, info_ptr->palette,\r
+         (png_uint_32)info_ptr->num_palette);\r
+   else if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)\r
+      png_error(png_ptr, "Valid palette required for paletted images");\r
+\r
+#ifdef PNG_WRITE_tRNS_SUPPORTED\r
+   if (info_ptr->valid & PNG_INFO_tRNS)\r
+   {\r
+#ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED\r
+      /* Invert the alpha channel (in tRNS) */\r
+      if ((png_ptr->transformations & PNG_INVERT_ALPHA) &&\r
+         info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)\r
+      {\r
+         int j;\r
+         for (j = 0; j<(int)info_ptr->num_trans; j++)\r
+            info_ptr->trans_alpha[j] = (png_byte)(255 - info_ptr->trans_alpha[j]);\r
+      }\r
+#endif\r
+      png_write_tRNS(png_ptr, info_ptr->trans_alpha, &(info_ptr->trans_color),\r
+         info_ptr->num_trans, info_ptr->color_type);\r
+   }\r
+#endif\r
+#ifdef PNG_WRITE_bKGD_SUPPORTED\r
+   if (info_ptr->valid & PNG_INFO_bKGD)\r
+      png_write_bKGD(png_ptr, &(info_ptr->background), info_ptr->color_type);\r
+#endif\r
+#ifdef PNG_WRITE_hIST_SUPPORTED\r
+   if (info_ptr->valid & PNG_INFO_hIST)\r
+      png_write_hIST(png_ptr, info_ptr->hist, info_ptr->num_palette);\r
+#endif\r
+#ifdef PNG_WRITE_oFFs_SUPPORTED\r
+   if (info_ptr->valid & PNG_INFO_oFFs)\r
+      png_write_oFFs(png_ptr, info_ptr->x_offset, info_ptr->y_offset,\r
+         info_ptr->offset_unit_type);\r
+#endif\r
+#ifdef PNG_WRITE_pCAL_SUPPORTED\r
+   if (info_ptr->valid & PNG_INFO_pCAL)\r
+      png_write_pCAL(png_ptr, info_ptr->pcal_purpose, info_ptr->pcal_X0,\r
+         info_ptr->pcal_X1, info_ptr->pcal_type, info_ptr->pcal_nparams,\r
+         info_ptr->pcal_units, info_ptr->pcal_params);\r
+#endif\r
+\r
+#ifdef PNG_sCAL_SUPPORTED\r
+   if (info_ptr->valid & PNG_INFO_sCAL)\r
+#ifdef PNG_WRITE_sCAL_SUPPORTED\r
+#if defined(PNG_FLOATING_POINT_SUPPORTED) && defined(PNG_STDIO_SUPPORTED)\r
+      png_write_sCAL(png_ptr, (int)info_ptr->scal_unit,\r
+          info_ptr->scal_pixel_width, info_ptr->scal_pixel_height);\r
+#else /* !FLOATING_POINT */\r
+#ifdef PNG_FIXED_POINT_SUPPORTED\r
+      png_write_sCAL_s(png_ptr, (int)info_ptr->scal_unit,\r
+          info_ptr->scal_s_width, info_ptr->scal_s_height);\r
+#endif /* FIXED_POINT */\r
+#endif /* FLOATING_POINT */\r
+#else  /* !WRITE_sCAL */\r
+      png_warning(png_ptr,\r
+          "png_write_sCAL not supported; sCAL chunk not written");\r
+#endif /* WRITE_sCAL */\r
+#endif /* sCAL */\r
+\r
+#ifdef PNG_WRITE_pHYs_SUPPORTED\r
+   if (info_ptr->valid & PNG_INFO_pHYs)\r
+      png_write_pHYs(png_ptr, info_ptr->x_pixels_per_unit,\r
+         info_ptr->y_pixels_per_unit, info_ptr->phys_unit_type);\r
+#endif /* pHYs */\r
+\r
+#ifdef PNG_WRITE_tIME_SUPPORTED\r
+   if (info_ptr->valid & PNG_INFO_tIME)\r
+   {\r
+      png_write_tIME(png_ptr, &(info_ptr->mod_time));\r
+      png_ptr->mode |= PNG_WROTE_tIME;\r
+   }\r
+#endif /* tIME */\r
+\r
+#ifdef PNG_WRITE_sPLT_SUPPORTED\r
+   if (info_ptr->valid & PNG_INFO_sPLT)\r
+     for (i = 0; i < (int)info_ptr->splt_palettes_num; i++)\r
+       png_write_sPLT(png_ptr, info_ptr->splt_palettes + i);\r
+#endif /* sPLT */\r
+\r
+#ifdef PNG_WRITE_TEXT_SUPPORTED\r
+   /* Check to see if we need to write text chunks */\r
+   for (i = 0; i < info_ptr->num_text; i++)\r
+   {\r
+      png_debug2(2, "Writing header text chunk %d, type %d", i,\r
+         info_ptr->text[i].compression);\r
+      /* An internationalized chunk? */\r
+      if (info_ptr->text[i].compression > 0)\r
+      {\r
+#ifdef PNG_WRITE_iTXt_SUPPORTED\r
+          /* Write international chunk */\r
+          png_write_iTXt(png_ptr,\r
+                         info_ptr->text[i].compression,\r
+                         info_ptr->text[i].key,\r
+                         info_ptr->text[i].lang,\r
+                         info_ptr->text[i].lang_key,\r
+                         info_ptr->text[i].text);\r
+#else\r
+          png_warning(png_ptr, "Unable to write international text");\r
+#endif\r
+          /* Mark this chunk as written */\r
+          info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;\r
+      }\r
+      /* If we want a compressed text chunk */\r
+      else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_zTXt)\r
+      {\r
+#ifdef PNG_WRITE_zTXt_SUPPORTED\r
+         /* Write compressed chunk */\r
+         png_write_zTXt(png_ptr, info_ptr->text[i].key,\r
+            info_ptr->text[i].text, 0,\r
+            info_ptr->text[i].compression);\r
+#else\r
+         png_warning(png_ptr, "Unable to write compressed text");\r
+#endif\r
+         /* Mark this chunk as written */\r
+         info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;\r
+      }\r
+      else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)\r
+      {\r
+#ifdef PNG_WRITE_tEXt_SUPPORTED\r
+         /* Write uncompressed chunk */\r
+         png_write_tEXt(png_ptr, info_ptr->text[i].key,\r
+                         info_ptr->text[i].text,\r
+                         0);\r
+         /* Mark this chunk as written */\r
+         info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;\r
+#else\r
+         /* Can't get here */\r
+         png_warning(png_ptr, "Unable to write uncompressed text");\r
+#endif\r
+      }\r
+   }\r
+#endif /* tEXt */\r
+\r
+#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED\r
+   if (info_ptr->unknown_chunks_num)\r
+   {\r
+      png_unknown_chunk *up;\r
+\r
+      png_debug(5, "writing extra chunks");\r
+\r
+      for (up = info_ptr->unknown_chunks;\r
+           up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;\r
+           up++)\r
+      {\r
+         int keep = png_handle_as_unknown(png_ptr, up->name);\r
+         if (keep != PNG_HANDLE_CHUNK_NEVER &&\r
+            up->location && (up->location & PNG_HAVE_PLTE) &&\r
+            !(up->location & PNG_HAVE_IDAT) &&\r
+            ((up->name[3] & 0x20) || keep == PNG_HANDLE_CHUNK_ALWAYS ||\r
+            (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS)))\r
+         {\r
+            png_write_chunk(png_ptr, up->name, up->data, up->size);\r
+         }\r
+      }\r
+   }\r
+#endif\r
+}\r
+\r
+/* Writes the end of the PNG file.  If you don't want to write comments or\r
+ * time information, you can pass NULL for info.  If you already wrote these\r
+ * in png_write_info(), do not write them again here.  If you have long\r
+ * comments, I suggest writing them here, and compressing them.\r
+ */\r
+void PNGAPI\r
+png_write_end(png_structp png_ptr, png_infop info_ptr)\r
+{\r
+   png_debug(1, "in png_write_end");\r
+\r
+   if (png_ptr == NULL)\r
+      return;\r
+   if (!(png_ptr->mode & PNG_HAVE_IDAT))\r
+      png_error(png_ptr, "No IDATs written into file");\r
+\r
+   /* See if user wants us to write information chunks */\r
+   if (info_ptr != NULL)\r
+   {\r
+#ifdef PNG_WRITE_TEXT_SUPPORTED\r
+      int i; /* local index variable */\r
+#endif\r
+#ifdef PNG_WRITE_tIME_SUPPORTED\r
+      /* Check to see if user has supplied a time chunk */\r
+      if ((info_ptr->valid & PNG_INFO_tIME) &&\r
+         !(png_ptr->mode & PNG_WROTE_tIME))\r
+         png_write_tIME(png_ptr, &(info_ptr->mod_time));\r
+#endif\r
+#ifdef PNG_WRITE_TEXT_SUPPORTED\r
+      /* Loop through comment chunks */\r
+      for (i = 0; i < info_ptr->num_text; i++)\r
+      {\r
+         png_debug2(2, "Writing trailer text chunk %d, type %d", i,\r
+            info_ptr->text[i].compression);\r
+         /* An internationalized chunk? */\r
+         if (info_ptr->text[i].compression > 0)\r
+         {\r
+#ifdef PNG_WRITE_iTXt_SUPPORTED\r
+            /* Write international chunk */\r
+            png_write_iTXt(png_ptr,\r
+                        info_ptr->text[i].compression,\r
+                        info_ptr->text[i].key,\r
+                        info_ptr->text[i].lang,\r
+                        info_ptr->text[i].lang_key,\r
+                        info_ptr->text[i].text);\r
+#else\r
+            png_warning(png_ptr, "Unable to write international text");\r
+#endif\r
+            /* Mark this chunk as written */\r
+            info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;\r
+         }\r
+         else if (info_ptr->text[i].compression >= PNG_TEXT_COMPRESSION_zTXt)\r
+         {\r
+#ifdef PNG_WRITE_zTXt_SUPPORTED\r
+            /* Write compressed chunk */\r
+            png_write_zTXt(png_ptr, info_ptr->text[i].key,\r
+               info_ptr->text[i].text, 0,\r
+               info_ptr->text[i].compression);\r
+#else\r
+            png_warning(png_ptr, "Unable to write compressed text");\r
+#endif\r
+            /* Mark this chunk as written */\r
+            info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;\r
+         }\r
+         else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)\r
+         {\r
+#ifdef PNG_WRITE_tEXt_SUPPORTED\r
+            /* Write uncompressed chunk */\r
+            png_write_tEXt(png_ptr, info_ptr->text[i].key,\r
+               info_ptr->text[i].text, 0);\r
+#else\r
+            png_warning(png_ptr, "Unable to write uncompressed text");\r
+#endif\r
+\r
+            /* Mark this chunk as written */\r
+            info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;\r
+         }\r
+      }\r
+#endif\r
+#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED\r
+   if (info_ptr->unknown_chunks_num)\r
+   {\r
+      png_unknown_chunk *up;\r
+\r
+      png_debug(5, "writing extra chunks");\r
+\r
+      for (up = info_ptr->unknown_chunks;\r
+           up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;\r
+           up++)\r
+      {\r
+         int keep = png_handle_as_unknown(png_ptr, up->name);\r
+         if (keep != PNG_HANDLE_CHUNK_NEVER &&\r
+            up->location && (up->location & PNG_AFTER_IDAT) &&\r
+            ((up->name[3] & 0x20) || keep == PNG_HANDLE_CHUNK_ALWAYS ||\r
+            (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS)))\r
+         {\r
+            png_write_chunk(png_ptr, up->name, up->data, up->size);\r
+         }\r
+      }\r
+   }\r
+#endif\r
+   }\r
+\r
+   png_ptr->mode |= PNG_AFTER_IDAT;\r
+\r
+   /* Write end of PNG file */\r
+   png_write_IEND(png_ptr);\r
+   /* This flush, added in libpng-1.0.8, removed from libpng-1.0.9beta03,\r
+    * and restored again in libpng-1.2.30, may cause some applications that\r
+    * do not set png_ptr->output_flush_fn to crash.  If your application\r
+    * experiences a problem, please try building libpng with\r
+    * PNG_WRITE_FLUSH_AFTER_IEND_SUPPORTED defined, and report the event to\r
+    * png-mng-implement at lists.sf.net .\r
+    */\r
+#ifdef PNG_WRITE_FLUSH_SUPPORTED\r
+#  ifdef PNG_WRITE_FLUSH_AFTER_IEND_SUPPORTED\r
+   png_flush(png_ptr);\r
+#  endif\r
+#endif\r
+}\r
+\r
+#ifdef PNG_CONVERT_tIME_SUPPORTED\r
+/* "tm" structure is not supported on WindowsCE */\r
+void PNGAPI\r
+png_convert_from_struct_tm(png_timep ptime, struct tm FAR * ttime)\r
+{\r
+   png_debug(1, "in png_convert_from_struct_tm");\r
+\r
+   ptime->year = (png_uint_16)(1900 + ttime->tm_year);\r
+   ptime->month = (png_byte)(ttime->tm_mon + 1);\r
+   ptime->day = (png_byte)ttime->tm_mday;\r
+   ptime->hour = (png_byte)ttime->tm_hour;\r
+   ptime->minute = (png_byte)ttime->tm_min;\r
+   ptime->second = (png_byte)ttime->tm_sec;\r
+}\r
+\r
+void PNGAPI\r
+png_convert_from_time_t(png_timep ptime, time_t ttime)\r
+{\r
+   struct tm *tbuf;\r
+\r
+   png_debug(1, "in png_convert_from_time_t");\r
+\r
+   tbuf = gmtime(&ttime);\r
+   png_convert_from_struct_tm(ptime, tbuf);\r
+}\r
+#endif\r
+\r
+/* Initialize png_ptr structure, and allocate any memory needed */\r
+png_structp PNGAPI\r
+png_create_write_struct(png_const_charp user_png_ver, png_voidp error_ptr,\r
+   png_error_ptr error_fn, png_error_ptr warn_fn)\r
+{\r
+#ifdef PNG_USER_MEM_SUPPORTED\r
+   return (png_create_write_struct_2(user_png_ver, error_ptr, error_fn,\r
+      warn_fn, NULL, NULL, NULL));\r
+}\r
+\r
+/* Alternate initialize png_ptr structure, and allocate any memory needed */\r
+png_structp PNGAPI\r
+png_create_write_struct_2(png_const_charp user_png_ver, png_voidp error_ptr,\r
+   png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,\r
+   png_malloc_ptr malloc_fn, png_free_ptr free_fn)\r
+{\r
+#endif /* PNG_USER_MEM_SUPPORTED */\r
+   volatile int png_cleanup_needed = 0;\r
+#ifdef PNG_SETJMP_SUPPORTED\r
+   volatile\r
+#endif\r
+   png_structp png_ptr;\r
+#ifdef PNG_SETJMP_SUPPORTED\r
+#ifdef USE_FAR_KEYWORD\r
+   jmp_buf jmpbuf;\r
+#endif\r
+#endif\r
+   int i;\r
+\r
+   png_debug(1, "in png_create_write_struct");\r
+\r
+#ifdef PNG_USER_MEM_SUPPORTED\r
+   png_ptr = (png_structp)png_create_struct_2(PNG_STRUCT_PNG,\r
+      (png_malloc_ptr)malloc_fn, (png_voidp)mem_ptr);\r
+#else\r
+   png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG);\r
+#endif /* PNG_USER_MEM_SUPPORTED */\r
+   if (png_ptr == NULL)\r
+      return (NULL);\r
+\r
+   /* Added at libpng-1.2.6 */\r
+#ifdef PNG_SET_USER_LIMITS_SUPPORTED\r
+   png_ptr->user_width_max = PNG_USER_WIDTH_MAX;\r
+   png_ptr->user_height_max = PNG_USER_HEIGHT_MAX;\r
+#endif\r
+\r
+#ifdef PNG_SETJMP_SUPPORTED\r
+/* Applications that neglect to set up their own setjmp() and then\r
+   encounter a png_error() will longjmp here.  Since the jmpbuf is\r
+   then meaningless we abort instead of returning. */\r
+#ifdef USE_FAR_KEYWORD\r
+   if (setjmp(jmpbuf))\r
+#else\r
+   if (setjmp(png_jmpbuf(png_ptr))) /* sets longjmp to match setjmp */\r
+#endif\r
+#ifdef USE_FAR_KEYWORD\r
+   png_memcpy(png_jmpbuf(png_ptr), jmpbuf, png_sizeof(jmp_buf));\r
+#endif\r
+      PNG_ABORT();\r
+#endif\r
+\r
+#ifdef PNG_USER_MEM_SUPPORTED\r
+   png_set_mem_fn(png_ptr, mem_ptr, malloc_fn, free_fn);\r
+#endif /* PNG_USER_MEM_SUPPORTED */\r
+   png_set_error_fn(png_ptr, error_ptr, error_fn, warn_fn);\r
+\r
+   if (user_png_ver)\r
+   {\r
+      i = 0;\r
+      do\r
+      {\r
+         if (user_png_ver[i] != png_libpng_ver[i])\r
+            png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;\r
+      } while (png_libpng_ver[i++]);\r
+   }\r
+\r
+   if (png_ptr->flags & PNG_FLAG_LIBRARY_MISMATCH)\r
+   {\r
+     /* Libpng 0.90 and later are binary incompatible with libpng 0.89, so\r
+      * we must recompile any applications that use any older library version.\r
+      * For versions after libpng 1.0, we will be compatible, so we need\r
+      * only check the first digit.\r
+      */\r
+     if (user_png_ver == NULL || user_png_ver[0] != png_libpng_ver[0] ||\r
+         (user_png_ver[0] == '1' && user_png_ver[2] != png_libpng_ver[2]) ||\r
+         (user_png_ver[0] == '0' && user_png_ver[2] < '9'))\r
+     {\r
+#ifdef PNG_STDIO_SUPPORTED\r
+        char msg[80];\r
+        if (user_png_ver)\r
+        {\r
+           png_snprintf(msg, 80,\r
+              "Application was compiled with png.h from libpng-%.20s",\r
+              user_png_ver);\r
+           png_warning(png_ptr, msg);\r
+        }\r
+        png_snprintf(msg, 80,\r
+           "Application  is  running with png.c from libpng-%.20s",\r
+           png_libpng_ver);\r
+        png_warning(png_ptr, msg);\r
+#endif\r
+#ifdef PNG_ERROR_NUMBERS_SUPPORTED\r
+        png_ptr->flags = 0;\r
+#endif\r
+        png_warning(png_ptr,\r
+           "Incompatible libpng version in application and library");\r
+        png_cleanup_needed = 1;\r
+     }\r
+   }\r
+\r
+   /* Initialize zbuf - compression buffer */\r
+   png_ptr->zbuf_size = PNG_ZBUF_SIZE;\r
+   if (!png_cleanup_needed)\r
+   {\r
+      png_ptr->zbuf = (png_bytep)png_malloc_warn(png_ptr,\r
+         png_ptr->zbuf_size);\r
+      if (png_ptr->zbuf == NULL)\r
+         png_cleanup_needed = 1;\r
+   }\r
+   if (png_cleanup_needed)\r
+   {\r
+       /* Clean up PNG structure and deallocate any memory. */\r
+       png_free(png_ptr, png_ptr->zbuf);\r
+       png_ptr->zbuf = NULL;\r
+#ifdef PNG_USER_MEM_SUPPORTED\r
+       png_destroy_struct_2((png_voidp)png_ptr,\r
+          (png_free_ptr)free_fn, (png_voidp)mem_ptr);\r
+#else\r
+       png_destroy_struct((png_voidp)png_ptr);\r
+#endif\r
+       return (NULL);\r
+   }\r
+\r
+   png_set_write_fn(png_ptr, NULL, NULL, NULL);\r
+\r
+#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED\r
+   png_set_filter_heuristics(png_ptr, PNG_FILTER_HEURISTIC_DEFAULT,\r
+      1, NULL, NULL);\r
+#endif\r
+\r
+   return (png_ptr);\r
+}\r
+\r
+\r
+/* Write a few rows of image data.  If the image is interlaced,\r
+ * either you will have to write the 7 sub images, or, if you\r
+ * have called png_set_interlace_handling(), you will have to\r
+ * "write" the image seven times.\r
+ */\r
+void PNGAPI\r
+png_write_rows(png_structp png_ptr, png_bytepp row,\r
+   png_uint_32 num_rows)\r
+{\r
+   png_uint_32 i; /* row counter */\r
+   png_bytepp rp; /* row pointer */\r
+\r
+   png_debug(1, "in png_write_rows");\r
+\r
+   if (png_ptr == NULL)\r
+      return;\r
+\r
+   /* Loop through the rows */\r
+   for (i = 0, rp = row; i < num_rows; i++, rp++)\r
+   {\r
+      png_write_row(png_ptr, *rp);\r
+   }\r
+}\r
+\r
+/* Write the image.  You only need to call this function once, even\r
+ * if you are writing an interlaced image.\r
+ */\r
+void PNGAPI\r
+png_write_image(png_structp png_ptr, png_bytepp image)\r
+{\r
+   png_uint_32 i; /* row index */\r
+   int pass, num_pass; /* pass variables */\r
+   png_bytepp rp; /* points to current row */\r
+\r
+   if (png_ptr == NULL)\r
+      return;\r
+\r
+   png_debug(1, "in png_write_image");\r
+\r
+#ifdef PNG_WRITE_INTERLACING_SUPPORTED\r
+   /* Initialize interlace handling.  If image is not interlaced,\r
+    * this will set pass to 1\r
+    */\r
+   num_pass = png_set_interlace_handling(png_ptr);\r
+#else\r
+   num_pass = 1;\r
+#endif\r
+   /* Loop through passes */\r
+   for (pass = 0; pass < num_pass; pass++)\r
+   {\r
+      /* Loop through image */\r
+      for (i = 0, rp = image; i < png_ptr->height; i++, rp++)\r
+      {\r
+         png_write_row(png_ptr, *rp);\r
+      }\r
+   }\r
+}\r
+\r
+/* Called by user to write a row of image data */\r
+void PNGAPI\r
+png_write_row(png_structp png_ptr, png_bytep row)\r
+{\r
+   if (png_ptr == NULL)\r
+      return;\r
+\r
+   png_debug2(1, "in png_write_row (row %ld, pass %d)",\r
+      png_ptr->row_number, png_ptr->pass);\r
+\r
+   /* Initialize transformations and other stuff if first time */\r
+   if (png_ptr->row_number == 0 && png_ptr->pass == 0)\r
+   {\r
+      /* Make sure we wrote the header info */\r
+      if (!(png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE))\r
+         png_error(png_ptr,\r
+            "png_write_info was never called before png_write_row");\r
+\r
+      /* Check for transforms that have been set but were defined out */\r
+#if !defined(PNG_WRITE_INVERT_SUPPORTED) && defined(PNG_READ_INVERT_SUPPORTED)\r
+      if (png_ptr->transformations & PNG_INVERT_MONO)\r
+         png_warning(png_ptr, "PNG_WRITE_INVERT_SUPPORTED is not defined");\r
+#endif\r
+#if !defined(PNG_WRITE_FILLER_SUPPORTED) && defined(PNG_READ_FILLER_SUPPORTED)\r
+      if (png_ptr->transformations & PNG_FILLER)\r
+         png_warning(png_ptr, "PNG_WRITE_FILLER_SUPPORTED is not defined");\r
+#endif\r
+#if !defined(PNG_WRITE_PACKSWAP_SUPPORTED) && \\r
+    defined(PNG_READ_PACKSWAP_SUPPORTED)\r
+      if (png_ptr->transformations & PNG_PACKSWAP)\r
+         png_warning(png_ptr,\r
+             "PNG_WRITE_PACKSWAP_SUPPORTED is not defined");\r
+#endif\r
+#if !defined(PNG_WRITE_PACK_SUPPORTED) && defined(PNG_READ_PACK_SUPPORTED)\r
+      if (png_ptr->transformations & PNG_PACK)\r
+         png_warning(png_ptr, "PNG_WRITE_PACK_SUPPORTED is not defined");\r
+#endif\r
+#if !defined(PNG_WRITE_SHIFT_SUPPORTED) && defined(PNG_READ_SHIFT_SUPPORTED)\r
+      if (png_ptr->transformations & PNG_SHIFT)\r
+         png_warning(png_ptr, "PNG_WRITE_SHIFT_SUPPORTED is not defined");\r
+#endif\r
+#if !defined(PNG_WRITE_BGR_SUPPORTED) && defined(PNG_READ_BGR_SUPPORTED)\r
+      if (png_ptr->transformations & PNG_BGR)\r
+         png_warning(png_ptr, "PNG_WRITE_BGR_SUPPORTED is not defined");\r
+#endif\r
+#if !defined(PNG_WRITE_SWAP_SUPPORTED) && defined(PNG_READ_SWAP_SUPPORTED)\r
+      if (png_ptr->transformations & PNG_SWAP_BYTES)\r
+         png_warning(png_ptr, "PNG_WRITE_SWAP_SUPPORTED is not defined");\r
+#endif\r
+\r
+      png_write_start_row(png_ptr);\r
+   }\r
+\r
+#ifdef PNG_WRITE_INTERLACING_SUPPORTED\r
+   /* If interlaced and not interested in row, return */\r
+   if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))\r
+   {\r
+      switch (png_ptr->pass)\r
+      {\r
+         case 0:\r
+            if (png_ptr->row_number & 0x07)\r
+            {\r
+               png_write_finish_row(png_ptr);\r
+               return;\r
+            }\r
+            break;\r
+         case 1:\r
+            if ((png_ptr->row_number & 0x07) || png_ptr->width < 5)\r
+            {\r
+               png_write_finish_row(png_ptr);\r
+               return;\r
+            }\r
+            break;\r
+         case 2:\r
+            if ((png_ptr->row_number & 0x07) != 4)\r
+            {\r
+               png_write_finish_row(png_ptr);\r
+               return;\r
+            }\r
+            break;\r
+         case 3:\r
+            if ((png_ptr->row_number & 0x03) || png_ptr->width < 3)\r
+            {\r
+               png_write_finish_row(png_ptr);\r
+               return;\r
+            }\r
+            break;\r
+         case 4:\r
+            if ((png_ptr->row_number & 0x03) != 2)\r
+            {\r
+               png_write_finish_row(png_ptr);\r
+               return;\r
+            }\r
+            break;\r
+         case 5:\r
+            if ((png_ptr->row_number & 0x01) || png_ptr->width < 2)\r
+            {\r
+               png_write_finish_row(png_ptr);\r
+               return;\r
+            }\r
+            break;\r
+         case 6:\r
+            if (!(png_ptr->row_number & 0x01))\r
+            {\r
+               png_write_finish_row(png_ptr);\r
+               return;\r
+            }\r
+            break;\r
+      }\r
+   }\r
+#endif\r
+\r
+   /* Set up row info for transformations */\r
+   png_ptr->row_info.color_type = png_ptr->color_type;\r
+   png_ptr->row_info.width = png_ptr->usr_width;\r
+   png_ptr->row_info.channels = png_ptr->usr_channels;\r
+   png_ptr->row_info.bit_depth = png_ptr->usr_bit_depth;\r
+   png_ptr->row_info.pixel_depth = (png_byte)(png_ptr->row_info.bit_depth *\r
+      png_ptr->row_info.channels);\r
+\r
+   png_ptr->row_info.rowbytes = PNG_ROWBYTES(png_ptr->row_info.pixel_depth,\r
+      png_ptr->row_info.width);\r
+\r
+   png_debug1(3, "row_info->color_type = %d", png_ptr->row_info.color_type);\r
+   png_debug1(3, "row_info->width = %lu", png_ptr->row_info.width);\r
+   png_debug1(3, "row_info->channels = %d", png_ptr->row_info.channels);\r
+   png_debug1(3, "row_info->bit_depth = %d", png_ptr->row_info.bit_depth);\r
+   png_debug1(3, "row_info->pixel_depth = %d", png_ptr->row_info.pixel_depth);\r
+   png_debug1(3, "row_info->rowbytes = %lu", png_ptr->row_info.rowbytes);\r
+\r
+   /* Copy user's row into buffer, leaving room for filter byte. */\r
+   png_memcpy(png_ptr->row_buf + 1, row, png_ptr->row_info.rowbytes);\r
+\r
+#ifdef PNG_WRITE_INTERLACING_SUPPORTED\r
+   /* Handle interlacing */\r
+   if (png_ptr->interlaced && png_ptr->pass < 6 &&\r
+      (png_ptr->transformations & PNG_INTERLACE))\r
+   {\r
+      png_do_write_interlace(&(png_ptr->row_info),\r
+         png_ptr->row_buf + 1, png_ptr->pass);\r
+      /* This should always get caught above, but still ... */\r
+      if (!(png_ptr->row_info.width))\r
+      {\r
+         png_write_finish_row(png_ptr);\r
+         return;\r
+      }\r
+   }\r
+#endif\r
+\r
+   /* Handle other transformations */\r
+   if (png_ptr->transformations)\r
+      png_do_write_transformations(png_ptr);\r
+\r
+#ifdef PNG_MNG_FEATURES_SUPPORTED\r
+   /* Write filter_method 64 (intrapixel differencing) only if\r
+    * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and\r
+    * 2. Libpng did not write a PNG signature (this filter_method is only\r
+    *    used in PNG datastreams that are embedded in MNG datastreams) and\r
+    * 3. The application called png_permit_mng_features with a mask that\r
+    *    included PNG_FLAG_MNG_FILTER_64 and\r
+    * 4. The filter_method is 64 and\r
+    * 5. The color_type is RGB or RGBA\r
+    */\r
+   if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&\r
+      (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING))\r
+   {\r
+      /* Intrapixel differencing */\r
+      png_do_write_intrapixel(&(png_ptr->row_info), png_ptr->row_buf + 1);\r
+   }\r
+#endif\r
+\r
+   /* Find a filter if necessary, filter the row and write it out. */\r
+   png_write_find_filter(png_ptr, &(png_ptr->row_info));\r
+\r
+   if (png_ptr->write_row_fn != NULL)\r
+      (*(png_ptr->write_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);\r
+}\r
+\r
+#ifdef PNG_WRITE_FLUSH_SUPPORTED\r
+/* Set the automatic flush interval or 0 to turn flushing off */\r
+void PNGAPI\r
+png_set_flush(png_structp png_ptr, int nrows)\r
+{\r
+   png_debug(1, "in png_set_flush");\r
+\r
+   if (png_ptr == NULL)\r
+      return;\r
+   png_ptr->flush_dist = (nrows < 0 ? 0 : nrows);\r
+}\r
+\r
+/* Flush the current output buffers now */\r
+void PNGAPI\r
+png_write_flush(png_structp png_ptr)\r
+{\r
+   int wrote_IDAT;\r
+\r
+   png_debug(1, "in png_write_flush");\r
+\r
+   if (png_ptr == NULL)\r
+      return;\r
+   /* We have already written out all of the data */\r
+   if (png_ptr->row_number >= png_ptr->num_rows)\r
+      return;\r
+\r
+   do\r
+   {\r
+      int ret;\r
+\r
+      /* Compress the data */\r
+      ret = deflate(&png_ptr->zstream, Z_SYNC_FLUSH);\r
+      wrote_IDAT = 0;\r
+\r
+      /* Check for compression errors */\r
+      if (ret != Z_OK)\r
+      {\r
+         if (png_ptr->zstream.msg != NULL)\r
+            png_error(png_ptr, png_ptr->zstream.msg);\r
+         else\r
+            png_error(png_ptr, "zlib error");\r
+      }\r
+\r
+      if (!(png_ptr->zstream.avail_out))\r
+      {\r
+         /* Write the IDAT and reset the zlib output buffer */\r
+         png_write_IDAT(png_ptr, png_ptr->zbuf,\r
+                        png_ptr->zbuf_size);\r
+         png_ptr->zstream.next_out = png_ptr->zbuf;\r
+         png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;\r
+         wrote_IDAT = 1;\r
+      }\r
+   } while(wrote_IDAT == 1);\r
+\r
+   /* If there is any data left to be output, write it into a new IDAT */\r
+   if (png_ptr->zbuf_size != png_ptr->zstream.avail_out)\r
+   {\r
+      /* Write the IDAT and reset the zlib output buffer */\r
+      png_write_IDAT(png_ptr, png_ptr->zbuf,\r
+                     png_ptr->zbuf_size - png_ptr->zstream.avail_out);\r
+      png_ptr->zstream.next_out = png_ptr->zbuf;\r
+      png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;\r
+   }\r
+   png_ptr->flush_rows = 0;\r
+   png_flush(png_ptr);\r
+}\r
+#endif /* PNG_WRITE_FLUSH_SUPPORTED */\r
+\r
+/* Free all memory used by the write */\r
+void PNGAPI\r
+png_destroy_write_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr)\r
+{\r
+   png_structp png_ptr = NULL;\r
+   png_infop info_ptr = NULL;\r
+#ifdef PNG_USER_MEM_SUPPORTED\r
+   png_free_ptr free_fn = NULL;\r
+   png_voidp mem_ptr = NULL;\r
+#endif\r
+\r
+   png_debug(1, "in png_destroy_write_struct");\r
+\r
+   if (png_ptr_ptr != NULL)\r
+   {\r
+      png_ptr = *png_ptr_ptr;\r
+#ifdef PNG_USER_MEM_SUPPORTED\r
+      free_fn = png_ptr->free_fn;\r
+      mem_ptr = png_ptr->mem_ptr;\r
+#endif\r
+   }\r
+\r
+#ifdef PNG_USER_MEM_SUPPORTED\r
+   if (png_ptr != NULL)\r
+   {\r
+      free_fn = png_ptr->free_fn;\r
+      mem_ptr = png_ptr->mem_ptr;\r
+   }\r
+#endif\r
+\r
+   if (info_ptr_ptr != NULL)\r
+      info_ptr = *info_ptr_ptr;\r
+\r
+   if (info_ptr != NULL)\r
+   {\r
+      if (png_ptr != NULL)\r
+      {\r
+        png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);\r
+\r
+#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED\r
+        if (png_ptr->num_chunk_list)\r
+        {\r
+           png_free(png_ptr, png_ptr->chunk_list);\r
+           png_ptr->num_chunk_list = 0;\r
+        }\r
+#endif\r
+      }\r
+\r
+#ifdef PNG_USER_MEM_SUPPORTED\r
+      png_destroy_struct_2((png_voidp)info_ptr, (png_free_ptr)free_fn,\r
+         (png_voidp)mem_ptr);\r
+#else\r
+      png_destroy_struct((png_voidp)info_ptr);\r
+#endif\r
+      *info_ptr_ptr = NULL;\r
+   }\r
+\r
+   if (png_ptr != NULL)\r
+   {\r
+      png_write_destroy(png_ptr);\r
+#ifdef PNG_USER_MEM_SUPPORTED\r
+      png_destroy_struct_2((png_voidp)png_ptr, (png_free_ptr)free_fn,\r
+         (png_voidp)mem_ptr);\r
+#else\r
+      png_destroy_struct((png_voidp)png_ptr);\r
+#endif\r
+      *png_ptr_ptr = NULL;\r
+   }\r
+}\r
+\r
+\r
+/* Free any memory used in png_ptr struct (old method) */\r
+void /* PRIVATE */\r
+png_write_destroy(png_structp png_ptr)\r
+{\r
+#ifdef PNG_SETJMP_SUPPORTED\r
+   jmp_buf tmp_jmp; /* Save jump buffer */\r
+#endif\r
+   png_error_ptr error_fn;\r
+   png_error_ptr warning_fn;\r
+   png_voidp error_ptr;\r
+#ifdef PNG_USER_MEM_SUPPORTED\r
+   png_free_ptr free_fn;\r
+#endif\r
+\r
+   png_debug(1, "in png_write_destroy");\r
+\r
+   /* Free any memory zlib uses */\r
+   deflateEnd(&png_ptr->zstream);\r
+\r
+   /* Free our memory.  png_free checks NULL for us. */\r
+   png_free(png_ptr, png_ptr->zbuf);\r
+   png_free(png_ptr, png_ptr->row_buf);\r
+#ifdef PNG_WRITE_FILTER_SUPPORTED\r
+   png_free(png_ptr, png_ptr->prev_row);\r
+   png_free(png_ptr, png_ptr->sub_row);\r
+   png_free(png_ptr, png_ptr->up_row);\r
+   png_free(png_ptr, png_ptr->avg_row);\r
+   png_free(png_ptr, png_ptr->paeth_row);\r
+#endif\r
+\r
+#ifdef PNG_TIME_RFC1123_SUPPORTED\r
+   png_free(png_ptr, png_ptr->time_buffer);\r
+#endif\r
+\r
+#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED\r
+   png_free(png_ptr, png_ptr->prev_filters);\r
+   png_free(png_ptr, png_ptr->filter_weights);\r
+   png_free(png_ptr, png_ptr->inv_filter_weights);\r
+   png_free(png_ptr, png_ptr->filter_costs);\r
+   png_free(png_ptr, png_ptr->inv_filter_costs);\r
+#endif\r
+\r
+#ifdef PNG_SETJMP_SUPPORTED\r
+   /* Reset structure */\r
+   png_memcpy(tmp_jmp, png_ptr->jmpbuf, png_sizeof(jmp_buf));\r
+#endif\r
+\r
+   error_fn = png_ptr->error_fn;\r
+   warning_fn = png_ptr->warning_fn;\r
+   error_ptr = png_ptr->error_ptr;\r
+#ifdef PNG_USER_MEM_SUPPORTED\r
+   free_fn = png_ptr->free_fn;\r
+#endif\r
+\r
+   png_memset(png_ptr, 0, png_sizeof(png_struct));\r
+\r
+   png_ptr->error_fn = error_fn;\r
+   png_ptr->warning_fn = warning_fn;\r
+   png_ptr->error_ptr = error_ptr;\r
+#ifdef PNG_USER_MEM_SUPPORTED\r
+   png_ptr->free_fn = free_fn;\r
+#endif\r
+\r
+#ifdef PNG_SETJMP_SUPPORTED\r
+   png_memcpy(png_ptr->jmpbuf, tmp_jmp, png_sizeof(jmp_buf));\r
+#endif\r
+}\r
+\r
+/* Allow the application to select one or more row filters to use. */\r
+void PNGAPI\r
+png_set_filter(png_structp png_ptr, int method, int filters)\r
+{\r
+   png_debug(1, "in png_set_filter");\r
+\r
+   if (png_ptr == NULL)\r
+      return;\r
+#ifdef PNG_MNG_FEATURES_SUPPORTED\r
+   if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&\r
+      (method == PNG_INTRAPIXEL_DIFFERENCING))\r
+         method = PNG_FILTER_TYPE_BASE;\r
+#endif\r
+   if (method == PNG_FILTER_TYPE_BASE)\r
+   {\r
+      switch (filters & (PNG_ALL_FILTERS | 0x07))\r
+      {\r
+#ifdef PNG_WRITE_FILTER_SUPPORTED\r
+         case 5:\r
+         case 6:\r
+         case 7: png_warning(png_ptr, "Unknown row filter for method 0");\r
+#endif /* PNG_WRITE_FILTER_SUPPORTED */\r
+         case PNG_FILTER_VALUE_NONE:\r
+              png_ptr->do_filter = PNG_FILTER_NONE; break;\r
+#ifdef PNG_WRITE_FILTER_SUPPORTED\r
+         case PNG_FILTER_VALUE_SUB:\r
+              png_ptr->do_filter = PNG_FILTER_SUB; break;\r
+         case PNG_FILTER_VALUE_UP:\r
+              png_ptr->do_filter = PNG_FILTER_UP; break;\r
+         case PNG_FILTER_VALUE_AVG:\r
+              png_ptr->do_filter = PNG_FILTER_AVG; break;\r
+         case PNG_FILTER_VALUE_PAETH:\r
+              png_ptr->do_filter = PNG_FILTER_PAETH; break;\r
+         default: png_ptr->do_filter = (png_byte)filters; break;\r
+#else\r
+         default: png_warning(png_ptr, "Unknown row filter for method 0");\r
+#endif /* PNG_WRITE_FILTER_SUPPORTED */\r
+      }\r
+\r
+      /* If we have allocated the row_buf, this means we have already started\r
+       * with the image and we should have allocated all of the filter buffers\r
+       * that have been selected.  If prev_row isn't already allocated, then\r
+       * it is too late to start using the filters that need it, since we\r
+       * will be missing the data in the previous row.  If an application\r
+       * wants to start and stop using particular filters during compression,\r
+       * it should start out with all of the filters, and then add and\r
+       * remove them after the start of compression.\r
+       */\r
+      if (png_ptr->row_buf != NULL)\r
+      {\r
+#ifdef PNG_WRITE_FILTER_SUPPORTED\r
+         if ((png_ptr->do_filter & PNG_FILTER_SUB) && png_ptr->sub_row == NULL)\r
+         {\r
+            png_ptr->sub_row = (png_bytep)png_malloc(png_ptr,\r
+              (png_ptr->rowbytes + 1));\r
+            png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;\r
+         }\r
+\r
+         if ((png_ptr->do_filter & PNG_FILTER_UP) && png_ptr->up_row == NULL)\r
+         {\r
+            if (png_ptr->prev_row == NULL)\r
+            {\r
+               png_warning(png_ptr, "Can't add Up filter after starting");\r
+               png_ptr->do_filter &= ~PNG_FILTER_UP;\r
+            }\r
+            else\r
+            {\r
+               png_ptr->up_row = (png_bytep)png_malloc(png_ptr,\r
+                  (png_ptr->rowbytes + 1));\r
+               png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;\r
+            }\r
+         }\r
+\r
+         if ((png_ptr->do_filter & PNG_FILTER_AVG) && png_ptr->avg_row == NULL)\r
+         {\r
+            if (png_ptr->prev_row == NULL)\r
+            {\r
+               png_warning(png_ptr, "Can't add Average filter after starting");\r
+               png_ptr->do_filter &= ~PNG_FILTER_AVG;\r
+            }\r
+            else\r
+            {\r
+               png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,\r
+                  (png_ptr->rowbytes + 1));\r
+               png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;\r
+            }\r
+         }\r
+\r
+         if ((png_ptr->do_filter & PNG_FILTER_PAETH) &&\r
+             png_ptr->paeth_row == NULL)\r
+         {\r
+            if (png_ptr->prev_row == NULL)\r
+            {\r
+               png_warning(png_ptr, "Can't add Paeth filter after starting");\r
+               png_ptr->do_filter &= (png_byte)(~PNG_FILTER_PAETH);\r
+            }\r
+            else\r
+            {\r
+               png_ptr->paeth_row = (png_bytep)png_malloc(png_ptr,\r
+                  (png_ptr->rowbytes + 1));\r
+               png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;\r
+            }\r
+         }\r
+\r
+         if (png_ptr->do_filter == PNG_NO_FILTERS)\r
+#endif /* PNG_WRITE_FILTER_SUPPORTED */\r
+            png_ptr->do_filter = PNG_FILTER_NONE;\r
+      }\r
+   }\r
+   else\r
+      png_error(png_ptr, "Unknown custom filter method");\r
+}\r
+\r
+/* This allows us to influence the way in which libpng chooses the "best"\r
+ * filter for the current scanline.  While the "minimum-sum-of-absolute-\r
+ * differences metric is relatively fast and effective, there is some\r
+ * question as to whether it can be improved upon by trying to keep the\r
+ * filtered data going to zlib more consistent, hopefully resulting in\r
+ * better compression.\r
+ */\r
+#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED      /* GRR 970116 */\r
+void PNGAPI\r
+png_set_filter_heuristics(png_structp png_ptr, int heuristic_method,\r
+   int num_weights, png_doublep filter_weights,\r
+   png_doublep filter_costs)\r
+{\r
+   int i;\r
+\r
+   png_debug(1, "in png_set_filter_heuristics");\r
+\r
+   if (png_ptr == NULL)\r
+      return;\r
+   if (heuristic_method >= PNG_FILTER_HEURISTIC_LAST)\r
+   {\r
+      png_warning(png_ptr, "Unknown filter heuristic method");\r
+      return;\r
+   }\r
+\r
+   if (heuristic_method == PNG_FILTER_HEURISTIC_DEFAULT)\r
+   {\r
+      heuristic_method = PNG_FILTER_HEURISTIC_UNWEIGHTED;\r
+   }\r
+\r
+   if (num_weights < 0 || filter_weights == NULL ||\r
+      heuristic_method == PNG_FILTER_HEURISTIC_UNWEIGHTED)\r
+   {\r
+      num_weights = 0;\r
+   }\r
+\r
+   png_ptr->num_prev_filters = (png_byte)num_weights;\r
+   png_ptr->heuristic_method = (png_byte)heuristic_method;\r
+\r
+   if (num_weights > 0)\r
+   {\r
+      if (png_ptr->prev_filters == NULL)\r
+      {\r
+         png_ptr->prev_filters = (png_bytep)png_malloc(png_ptr,\r
+            (png_uint_32)(png_sizeof(png_byte) * num_weights));\r
+\r
+         /* To make sure that the weighting starts out fairly */\r
+         for (i = 0; i < num_weights; i++)\r
+         {\r
+            png_ptr->prev_filters[i] = 255;\r
+         }\r
+      }\r
+\r
+      if (png_ptr->filter_weights == NULL)\r
+      {\r
+         png_ptr->filter_weights = (png_uint_16p)png_malloc(png_ptr,\r
+            (png_uint_32)(png_sizeof(png_uint_16) * num_weights));\r
+\r
+         png_ptr->inv_filter_weights = (png_uint_16p)png_malloc(png_ptr,\r
+            (png_uint_32)(png_sizeof(png_uint_16) * num_weights));\r
+         for (i = 0; i < num_weights; i++)\r
+         {\r
+            png_ptr->inv_filter_weights[i] =\r
+            png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;\r
+         }\r
+      }\r
+\r
+      for (i = 0; i < num_weights; i++)\r
+      {\r
+         if (filter_weights[i] < 0.0)\r
+         {\r
+            png_ptr->inv_filter_weights[i] =\r
+            png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;\r
+         }\r
+         else\r
+         {\r
+            png_ptr->inv_filter_weights[i] =\r
+               (png_uint_16)((double)PNG_WEIGHT_FACTOR*filter_weights[i]+0.5);\r
+            png_ptr->filter_weights[i] =\r
+               (png_uint_16)((double)PNG_WEIGHT_FACTOR/filter_weights[i]+0.5);\r
+         }\r
+      }\r
+   }\r
+\r
+   /* If, in the future, there are other filter methods, this would\r
+    * need to be based on png_ptr->filter.\r
+    */\r
+   if (png_ptr->filter_costs == NULL)\r
+   {\r
+      png_ptr->filter_costs = (png_uint_16p)png_malloc(png_ptr,\r
+         (png_uint_32)(png_sizeof(png_uint_16) * PNG_FILTER_VALUE_LAST));\r
+\r
+      png_ptr->inv_filter_costs = (png_uint_16p)png_malloc(png_ptr,\r
+         (png_uint_32)(png_sizeof(png_uint_16) * PNG_FILTER_VALUE_LAST));\r
+\r
+      for (i = 0; i < PNG_FILTER_VALUE_LAST; i++)\r
+      {\r
+         png_ptr->inv_filter_costs[i] =\r
+         png_ptr->filter_costs[i] = PNG_COST_FACTOR;\r
+      }\r
+   }\r
+\r
+   /* Here is where we set the relative costs of the different filters.  We\r
+    * should take the desired compression level into account when setting\r
+    * the costs, so that Paeth, for instance, has a high relative cost at low\r
+    * compression levels, while it has a lower relative cost at higher\r
+    * compression settings.  The filter types are in order of increasing\r
+    * relative cost, so it would be possible to do this with an algorithm.\r
+    */\r
+   for (i = 0; i < PNG_FILTER_VALUE_LAST; i++)\r
+   {\r
+      if (filter_costs == NULL || filter_costs[i] < 0.0)\r
+      {\r
+         png_ptr->inv_filter_costs[i] =\r
+         png_ptr->filter_costs[i] = PNG_COST_FACTOR;\r
+      }\r
+      else if (filter_costs[i] >= 1.0)\r
+      {\r
+         png_ptr->inv_filter_costs[i] =\r
+            (png_uint_16)((double)PNG_COST_FACTOR / filter_costs[i] + 0.5);\r
+         png_ptr->filter_costs[i] =\r
+            (png_uint_16)((double)PNG_COST_FACTOR * filter_costs[i] + 0.5);\r
+      }\r
+   }\r
+}\r
+#endif /* PNG_WRITE_WEIGHTED_FILTER_SUPPORTED */\r
+\r
+void PNGAPI\r
+png_set_compression_level(png_structp png_ptr, int level)\r
+{\r
+   png_debug(1, "in png_set_compression_level");\r
+\r
+   if (png_ptr == NULL)\r
+      return;\r
+   png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_LEVEL;\r
+   png_ptr->zlib_level = level;\r
+}\r
+\r
+void PNGAPI\r
+png_set_compression_mem_level(png_structp png_ptr, int mem_level)\r
+{\r
+   png_debug(1, "in png_set_compression_mem_level");\r
+\r
+   if (png_ptr == NULL)\r
+      return;\r
+   png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL;\r
+   png_ptr->zlib_mem_level = mem_level;\r
+}\r
+\r
+void PNGAPI\r
+png_set_compression_strategy(png_structp png_ptr, int strategy)\r
+{\r
+   png_debug(1, "in png_set_compression_strategy");\r
+\r
+   if (png_ptr == NULL)\r
+      return;\r
+   png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_STRATEGY;\r
+   png_ptr->zlib_strategy = strategy;\r
+}\r
+\r
+void PNGAPI\r
+png_set_compression_window_bits(png_structp png_ptr, int window_bits)\r
+{\r
+   if (png_ptr == NULL)\r
+      return;\r
+   if (window_bits > 15)\r
+      png_warning(png_ptr, "Only compression windows <= 32k supported by PNG");\r
+   else if (window_bits < 8)\r
+      png_warning(png_ptr, "Only compression windows >= 256 supported by PNG");\r
+#ifndef WBITS_8_OK\r
+   /* Avoid libpng bug with 256-byte windows */\r
+   if (window_bits == 8)\r
+     {\r
+       png_warning(png_ptr, "Compression window is being reset to 512");\r
+       window_bits = 9;\r
+     }\r
+#endif\r
+   png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS;\r
+   png_ptr->zlib_window_bits = window_bits;\r
+}\r
+\r
+void PNGAPI\r
+png_set_compression_method(png_structp png_ptr, int method)\r
+{\r
+   png_debug(1, "in png_set_compression_method");\r
+\r
+   if (png_ptr == NULL)\r
+      return;\r
+   if (method != 8)\r
+      png_warning(png_ptr, "Only compression method 8 is supported by PNG");\r
+   png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_METHOD;\r
+   png_ptr->zlib_method = method;\r
+}\r
+\r
+void PNGAPI\r
+png_set_write_status_fn(png_structp png_ptr, png_write_status_ptr write_row_fn)\r
+{\r
+   if (png_ptr == NULL)\r
+      return;\r
+   png_ptr->write_row_fn = write_row_fn;\r
+}\r
+\r
+#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED\r
+void PNGAPI\r
+png_set_write_user_transform_fn(png_structp png_ptr, png_user_transform_ptr\r
+   write_user_transform_fn)\r
+{\r
+   png_debug(1, "in png_set_write_user_transform_fn");\r
+\r
+   if (png_ptr == NULL)\r
+      return;\r
+   png_ptr->transformations |= PNG_USER_TRANSFORM;\r
+   png_ptr->write_user_transform_fn = write_user_transform_fn;\r
+}\r
+#endif\r
+\r
+\r
+#ifdef PNG_INFO_IMAGE_SUPPORTED\r
+void PNGAPI\r
+png_write_png(png_structp png_ptr, png_infop info_ptr,\r
+              int transforms, voidp params)\r
+{\r
+   if (png_ptr == NULL || info_ptr == NULL)\r
+      return;\r
+\r
+   /* Write the file header information. */\r
+   png_write_info(png_ptr, info_ptr);\r
+\r
+   /* ------ these transformations don't touch the info structure ------- */\r
+\r
+#ifdef PNG_WRITE_INVERT_SUPPORTED\r
+   /* Invert monochrome pixels */\r
+   if (transforms & PNG_TRANSFORM_INVERT_MONO)\r
+      png_set_invert_mono(png_ptr);\r
+#endif\r
+\r
+#ifdef PNG_WRITE_SHIFT_SUPPORTED\r
+   /* Shift the pixels up to a legal bit depth and fill in\r
+    * as appropriate to correctly scale the image.\r
+    */\r
+   if ((transforms & PNG_TRANSFORM_SHIFT)\r
+               && (info_ptr->valid & PNG_INFO_sBIT))\r
+      png_set_shift(png_ptr, &info_ptr->sig_bit);\r
+#endif\r
+\r
+#ifdef PNG_WRITE_PACK_SUPPORTED\r
+   /* Pack pixels into bytes */\r
+   if (transforms & PNG_TRANSFORM_PACKING)\r
+       png_set_packing(png_ptr);\r
+#endif\r
+\r
+#ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED\r
+   /* Swap location of alpha bytes from ARGB to RGBA */\r
+   if (transforms & PNG_TRANSFORM_SWAP_ALPHA)\r
+      png_set_swap_alpha(png_ptr);\r
+#endif\r
+\r
+#ifdef PNG_WRITE_FILLER_SUPPORTED\r
+   /* Pack XRGB/RGBX/ARGB/RGBA into * RGB (4 channels -> 3 channels) */\r
+   if (transforms & PNG_TRANSFORM_STRIP_FILLER_AFTER)\r
+      png_set_filler(png_ptr, 0, PNG_FILLER_AFTER);\r
+   else if (transforms & PNG_TRANSFORM_STRIP_FILLER_BEFORE)\r
+      png_set_filler(png_ptr, 0, PNG_FILLER_BEFORE);\r
+#endif\r
+\r
+#ifdef PNG_WRITE_BGR_SUPPORTED\r
+   /* Flip BGR pixels to RGB */\r
+   if (transforms & PNG_TRANSFORM_BGR)\r
+      png_set_bgr(png_ptr);\r
+#endif\r
+\r
+#ifdef PNG_WRITE_SWAP_SUPPORTED\r
+   /* Swap bytes of 16-bit files to most significant byte first */\r
+   if (transforms & PNG_TRANSFORM_SWAP_ENDIAN)\r
+      png_set_swap(png_ptr);\r
+#endif\r
+\r
+#ifdef PNG_WRITE_PACKSWAP_SUPPORTED\r
+   /* Swap bits of 1, 2, 4 bit packed pixel formats */\r
+   if (transforms & PNG_TRANSFORM_PACKSWAP)\r
+      png_set_packswap(png_ptr);\r
+#endif\r
+\r
+#ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED\r
+   /* Invert the alpha channel from opacity to transparency */\r
+   if (transforms & PNG_TRANSFORM_INVERT_ALPHA)\r
+      png_set_invert_alpha(png_ptr);\r
+#endif\r
+\r
+   /* ----------------------- end of transformations ------------------- */\r
+\r
+   /* Write the bits */\r
+   if (info_ptr->valid & PNG_INFO_IDAT)\r
+       png_write_image(png_ptr, info_ptr->row_pointers);\r
+\r
+   /* It is REQUIRED to call this to finish writing the rest of the file */\r
+   png_write_end(png_ptr, info_ptr);\r
+\r
+   transforms = transforms; /* Quiet compiler warnings */\r
+   params = params;\r
+}\r
+#endif\r
+#endif /* PNG_WRITE_SUPPORTED */\r
index 0372fe6..9cf095b 100644 (file)
-
-/* pngwtran.c - transforms the data in a row for PNG writers
- *
- * Last changed in libpng 1.2.9 April 14, 2006
- * For conditions of distribution and use, see copyright notice in png.h
- * Copyright (c) 1998-2006 Glenn Randers-Pehrson
- * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
- * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
- */
-
-#define PNG_INTERNAL
-#include "png.h"
-#ifdef PNG_WRITE_SUPPORTED
-
-/* Transform the data according to the user's wishes.  The order of
- * transformations is significant.
- */
-void /* PRIVATE */
-png_do_write_transformations(png_structp png_ptr)
-{
-   png_debug(1, "in png_do_write_transformations\n");
-
-   if (png_ptr == NULL)
-      return;
-
-#if defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
-   if (png_ptr->transformations & PNG_USER_TRANSFORM)
-      if(png_ptr->write_user_transform_fn != NULL)
-        (*(png_ptr->write_user_transform_fn)) /* user write transform function */
-          (png_ptr,                    /* png_ptr */
-           &(png_ptr->row_info),       /* row_info:     */
-             /*  png_uint_32 width;          width of row */
-             /*  png_uint_32 rowbytes;       number of bytes in row */
-             /*  png_byte color_type;        color type of pixels */
-             /*  png_byte bit_depth;         bit depth of samples */
-             /*  png_byte channels;          number of channels (1-4) */
-             /*  png_byte pixel_depth;       bits per pixel (depth*channels) */
-           png_ptr->row_buf + 1);      /* start of pixel data for row */
-#endif
-#if defined(PNG_WRITE_FILLER_SUPPORTED)
-   if (png_ptr->transformations & PNG_FILLER)
-      png_do_strip_filler(&(png_ptr->row_info), png_ptr->row_buf + 1,
-         png_ptr->flags);
-#endif
-#if defined(PNG_WRITE_PACKSWAP_SUPPORTED)
-   if (png_ptr->transformations & PNG_PACKSWAP)
-      png_do_packswap(&(png_ptr->row_info), png_ptr->row_buf + 1);
-#endif
-#if defined(PNG_WRITE_PACK_SUPPORTED)
-   if (png_ptr->transformations & PNG_PACK)
-      png_do_pack(&(png_ptr->row_info), png_ptr->row_buf + 1,
-         (png_uint_32)png_ptr->bit_depth);
-#endif
-#if defined(PNG_WRITE_SWAP_SUPPORTED)
-   if (png_ptr->transformations & PNG_SWAP_BYTES)
-      png_do_swap(&(png_ptr->row_info), png_ptr->row_buf + 1);
-#endif
-#if defined(PNG_WRITE_SHIFT_SUPPORTED)
-   if (png_ptr->transformations & PNG_SHIFT)
-      png_do_shift(&(png_ptr->row_info), png_ptr->row_buf + 1,
-         &(png_ptr->shift));
-#endif
-#if defined(PNG_WRITE_SWAP_ALPHA_SUPPORTED)
-   if (png_ptr->transformations & PNG_SWAP_ALPHA)
-      png_do_write_swap_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1);
-#endif
-#if defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED)
-   if (png_ptr->transformations & PNG_INVERT_ALPHA)
-      png_do_write_invert_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1);
-#endif
-#if defined(PNG_WRITE_BGR_SUPPORTED)
-   if (png_ptr->transformations & PNG_BGR)
-      png_do_bgr(&(png_ptr->row_info), png_ptr->row_buf + 1);
-#endif
-#if defined(PNG_WRITE_INVERT_SUPPORTED)
-   if (png_ptr->transformations & PNG_INVERT_MONO)
-      png_do_invert(&(png_ptr->row_info), png_ptr->row_buf + 1);
-#endif
-}
-
-#if defined(PNG_WRITE_PACK_SUPPORTED)
-/* Pack pixels into bytes.  Pass the true bit depth in bit_depth.  The
- * row_info bit depth should be 8 (one pixel per byte).  The channels
- * should be 1 (this only happens on grayscale and paletted images).
- */
-void /* PRIVATE */
-png_do_pack(png_row_infop row_info, png_bytep row, png_uint_32 bit_depth)
-{
-   png_debug(1, "in png_do_pack\n");
-   if (row_info->bit_depth == 8 &&
-#if defined(PNG_USELESS_TESTS_SUPPORTED)
-       row != NULL && row_info != NULL &&
-#endif
-      row_info->channels == 1)
-   {
-      switch ((int)bit_depth)
-      {
-         case 1:
-         {
-            png_bytep sp, dp;
-            int mask, v;
-            png_uint_32 i;
-            png_uint_32 row_width = row_info->width;
-
-            sp = row;
-            dp = row;
-            mask = 0x80;
-            v = 0;
-
-            for (i = 0; i < row_width; i++)
-            {
-               if (*sp != 0)
-                  v |= mask;
-               sp++;
-               if (mask > 1)
-                  mask >>= 1;
-               else
-               {
-                  mask = 0x80;
-                  *dp = (png_byte)v;
-                  dp++;
-                  v = 0;
-               }
-            }
-            if (mask != 0x80)
-               *dp = (png_byte)v;
-            break;
-         }
-         case 2:
-         {
-            png_bytep sp, dp;
-            int shift, v;
-            png_uint_32 i;
-            png_uint_32 row_width = row_info->width;
-
-            sp = row;
-            dp = row;
-            shift = 6;
-            v = 0;
-            for (i = 0; i < row_width; i++)
-            {
-               png_byte value;
-
-               value = (png_byte)(*sp & 0x03);
-               v |= (value << shift);
-               if (shift == 0)
-               {
-                  shift = 6;
-                  *dp = (png_byte)v;
-                  dp++;
-                  v = 0;
-               }
-               else
-                  shift -= 2;
-               sp++;
-            }
-            if (shift != 6)
-               *dp = (png_byte)v;
-            break;
-         }
-         case 4:
-         {
-            png_bytep sp, dp;
-            int shift, v;
-            png_uint_32 i;
-            png_uint_32 row_width = row_info->width;
-
-            sp = row;
-            dp = row;
-            shift = 4;
-            v = 0;
-            for (i = 0; i < row_width; i++)
-            {
-               png_byte value;
-
-               value = (png_byte)(*sp & 0x0f);
-               v |= (value << shift);
-
-               if (shift == 0)
-               {
-                  shift = 4;
-                  *dp = (png_byte)v;
-                  dp++;
-                  v = 0;
-               }
-               else
-                  shift -= 4;
-
-               sp++;
-            }
-            if (shift != 4)
-               *dp = (png_byte)v;
-            break;
-         }
-      }
-      row_info->bit_depth = (png_byte)bit_depth;
-      row_info->pixel_depth = (png_byte)(bit_depth * row_info->channels);
-      row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,
-         row_info->width);
-   }
-}
-#endif
-
-#if defined(PNG_WRITE_SHIFT_SUPPORTED)
-/* Shift pixel values to take advantage of whole range.  Pass the
- * true number of bits in bit_depth.  The row should be packed
- * according to row_info->bit_depth.  Thus, if you had a row of
- * bit depth 4, but the pixels only had values from 0 to 7, you
- * would pass 3 as bit_depth, and this routine would translate the
- * data to 0 to 15.
- */
-void /* PRIVATE */
-png_do_shift(png_row_infop row_info, png_bytep row, png_color_8p bit_depth)
-{
-   png_debug(1, "in png_do_shift\n");
-#if defined(PNG_USELESS_TESTS_SUPPORTED)
-   if (row != NULL && row_info != NULL &&
-#else
-   if (
-#endif
-      row_info->color_type != PNG_COLOR_TYPE_PALETTE)
-   {
-      int shift_start[4], shift_dec[4];
-      int channels = 0;
-
-      if (row_info->color_type & PNG_COLOR_MASK_COLOR)
-      {
-         shift_start[channels] = row_info->bit_depth - bit_depth->red;
-         shift_dec[channels] = bit_depth->red;
-         channels++;
-         shift_start[channels] = row_info->bit_depth - bit_depth->green;
-         shift_dec[channels] = bit_depth->green;
-         channels++;
-         shift_start[channels] = row_info->bit_depth - bit_depth->blue;
-         shift_dec[channels] = bit_depth->blue;
-         channels++;
-      }
-      else
-      {
-         shift_start[channels] = row_info->bit_depth - bit_depth->gray;
-         shift_dec[channels] = bit_depth->gray;
-         channels++;
-      }
-      if (row_info->color_type & PNG_COLOR_MASK_ALPHA)
-      {
-         shift_start[channels] = row_info->bit_depth - bit_depth->alpha;
-         shift_dec[channels] = bit_depth->alpha;
-         channels++;
-      }
-
-      /* with low row depths, could only be grayscale, so one channel */
-      if (row_info->bit_depth < 8)
-      {
-         png_bytep bp = row;
-         png_uint_32 i;
-         png_byte mask;
-         png_uint_32 row_bytes = row_info->rowbytes;
-
-         if (bit_depth->gray == 1 && row_info->bit_depth == 2)
-            mask = 0x55;
-         else if (row_info->bit_depth == 4 && bit_depth->gray == 3)
-            mask = 0x11;
-         else
-            mask = 0xff;
-
-         for (i = 0; i < row_bytes; i++, bp++)
-         {
-            png_uint_16 v;
-            int j;
-
-            v = *bp;
-            *bp = 0;
-            for (j = shift_start[0]; j > -shift_dec[0]; j -= shift_dec[0])
-            {
-               if (j > 0)
-                  *bp |= (png_byte)((v << j) & 0xff);
-               else
-                  *bp |= (png_byte)((v >> (-j)) & mask);
-            }
-         }
-      }
-      else if (row_info->bit_depth == 8)
-      {
-         png_bytep bp = row;
-         png_uint_32 i;
-         png_uint_32 istop = channels * row_info->width;
-
-         for (i = 0; i < istop; i++, bp++)
-         {
-
-            png_uint_16 v;
-            int j;
-            int c = (int)(i%channels);
-
-            v = *bp;
-            *bp = 0;
-            for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c])
-            {
-               if (j > 0)
-                  *bp |= (png_byte)((v << j) & 0xff);
-               else
-                  *bp |= (png_byte)((v >> (-j)) & 0xff);
-            }
-         }
-      }
-      else
-      {
-         png_bytep bp;
-         png_uint_32 i;
-         png_uint_32 istop = channels * row_info->width;
-
-         for (bp = row, i = 0; i < istop; i++)
-         {
-            int c = (int)(i%channels);
-            png_uint_16 value, v;
-            int j;
-
-            v = (png_uint_16)(((png_uint_16)(*bp) << 8) + *(bp + 1));
-            value = 0;
-            for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c])
-            {
-               if (j > 0)
-                  value |= (png_uint_16)((v << j) & (png_uint_16)0xffff);
-               else
-                  value |= (png_uint_16)((v >> (-j)) & (png_uint_16)0xffff);
-            }
-            *bp++ = (png_byte)(value >> 8);
-            *bp++ = (png_byte)(value & 0xff);
-         }
-      }
-   }
-}
-#endif
-
-#if defined(PNG_WRITE_SWAP_ALPHA_SUPPORTED)
-void /* PRIVATE */
-png_do_write_swap_alpha(png_row_infop row_info, png_bytep row)
-{
-   png_debug(1, "in png_do_write_swap_alpha\n");
-#if defined(PNG_USELESS_TESTS_SUPPORTED)
-   if (row != NULL && row_info != NULL)
-#endif
-   {
-      if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
-      {
-         /* This converts from ARGB to RGBA */
-         if (row_info->bit_depth == 8)
-         {
-            png_bytep sp, dp;
-            png_uint_32 i;
-            png_uint_32 row_width = row_info->width;
-            for (i = 0, sp = dp = row; i < row_width; i++)
-            {
-               png_byte save = *(sp++);
-               *(dp++) = *(sp++);
-               *(dp++) = *(sp++);
-               *(dp++) = *(sp++);
-               *(dp++) = save;
-            }
-         }
-         /* This converts from AARRGGBB to RRGGBBAA */
-         else
-         {
-            png_bytep sp, dp;
-            png_uint_32 i;
-            png_uint_32 row_width = row_info->width;
-
-            for (i = 0, sp = dp = row; i < row_width; i++)
-            {
-               png_byte save[2];
-               save[0] = *(sp++);
-               save[1] = *(sp++);
-               *(dp++) = *(sp++);
-               *(dp++) = *(sp++);
-               *(dp++) = *(sp++);
-               *(dp++) = *(sp++);
-               *(dp++) = *(sp++);
-               *(dp++) = *(sp++);
-               *(dp++) = save[0];
-               *(dp++) = save[1];
-            }
-         }
-      }
-      else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
-      {
-         /* This converts from AG to GA */
-         if (row_info->bit_depth == 8)
-         {
-            png_bytep sp, dp;
-            png_uint_32 i;
-            png_uint_32 row_width = row_info->width;
-
-            for (i = 0, sp = dp = row; i < row_width; i++)
-            {
-               png_byte save = *(sp++);
-               *(dp++) = *(sp++);
-               *(dp++) = save;
-            }
-         }
-         /* This converts from AAGG to GGAA */
-         else
-         {
-            png_bytep sp, dp;
-            png_uint_32 i;
-            png_uint_32 row_width = row_info->width;
-
-            for (i = 0, sp = dp = row; i < row_width; i++)
-            {
-               png_byte save[2];
-               save[0] = *(sp++);
-               save[1] = *(sp++);
-               *(dp++) = *(sp++);
-               *(dp++) = *(sp++);
-               *(dp++) = save[0];
-               *(dp++) = save[1];
-            }
-         }
-      }
-   }
-}
-#endif
-
-#if defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED)
-void /* PRIVATE */
-png_do_write_invert_alpha(png_row_infop row_info, png_bytep row)
-{
-   png_debug(1, "in png_do_write_invert_alpha\n");
-#if defined(PNG_USELESS_TESTS_SUPPORTED)
-   if (row != NULL && row_info != NULL)
-#endif
-   {
-      if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
-      {
-         /* This inverts the alpha channel in RGBA */
-         if (row_info->bit_depth == 8)
-         {
-            png_bytep sp, dp;
-            png_uint_32 i;
-            png_uint_32 row_width = row_info->width;
-            for (i = 0, sp = dp = row; i < row_width; i++)
-            {
-               /* does nothing
-               *(dp++) = *(sp++);
-               *(dp++) = *(sp++);
-               *(dp++) = *(sp++);
-               */
-               sp+=3; dp = sp;
-               *(dp++) = (png_byte)(255 - *(sp++));
-            }
-         }
-         /* This inverts the alpha channel in RRGGBBAA */
-         else
-         {
-            png_bytep sp, dp;
-            png_uint_32 i;
-            png_uint_32 row_width = row_info->width;
-
-            for (i = 0, sp = dp = row; i < row_width; i++)
-            {
-               /* does nothing
-               *(dp++) = *(sp++);
-               *(dp++) = *(sp++);
-               *(dp++) = *(sp++);
-               *(dp++) = *(sp++);
-               *(dp++) = *(sp++);
-               *(dp++) = *(sp++);
-               */
-               sp+=6; dp = sp;
-               *(dp++) = (png_byte)(255 - *(sp++));
-               *(dp++) = (png_byte)(255 - *(sp++));
-            }
-         }
-      }
-      else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
-      {
-         /* This inverts the alpha channel in GA */
-         if (row_info->bit_depth == 8)
-         {
-            png_bytep sp, dp;
-            png_uint_32 i;
-            png_uint_32 row_width = row_info->width;
-
-            for (i = 0, sp = dp = row; i < row_width; i++)
-            {
-               *(dp++) = *(sp++);
-               *(dp++) = (png_byte)(255 - *(sp++));
-            }
-         }
-         /* This inverts the alpha channel in GGAA */
-         else
-         {
-            png_bytep sp, dp;
-            png_uint_32 i;
-            png_uint_32 row_width = row_info->width;
-
-            for (i = 0, sp = dp = row; i < row_width; i++)
-            {
-               /* does nothing
-               *(dp++) = *(sp++);
-               *(dp++) = *(sp++);
-               */
-               sp+=2; dp = sp;
-               *(dp++) = (png_byte)(255 - *(sp++));
-               *(dp++) = (png_byte)(255 - *(sp++));
-            }
-         }
-      }
-   }
-}
-#endif
-
-#if defined(PNG_MNG_FEATURES_SUPPORTED)
-/* undoes intrapixel differencing  */
-void /* PRIVATE */
-png_do_write_intrapixel(png_row_infop row_info, png_bytep row)
-{
-   png_debug(1, "in png_do_write_intrapixel\n");
-   if (
-#if defined(PNG_USELESS_TESTS_SUPPORTED)
-       row != NULL && row_info != NULL &&
-#endif
-       (row_info->color_type & PNG_COLOR_MASK_COLOR))
-   {
-      int bytes_per_pixel;
-      png_uint_32 row_width = row_info->width;
-      if (row_info->bit_depth == 8)
-      {
-         png_bytep rp;
-         png_uint_32 i;
-
-         if (row_info->color_type == PNG_COLOR_TYPE_RGB)
-            bytes_per_pixel = 3;
-         else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
-            bytes_per_pixel = 4;
-         else
-            return;
-
-         for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
-         {
-            *(rp)   = (png_byte)((*rp     - *(rp+1))&0xff);
-            *(rp+2) = (png_byte)((*(rp+2) - *(rp+1))&0xff);
-         }
-      }
-      else if (row_info->bit_depth == 16)
-      {
-         png_bytep rp;
-         png_uint_32 i;
-
-         if (row_info->color_type == PNG_COLOR_TYPE_RGB)
-            bytes_per_pixel = 6;
-         else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
-            bytes_per_pixel = 8;
-         else
-            return;
-
-         for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
-         {
-            png_uint_32 s0   = (*(rp  ) << 8) | *(rp+1);
-            png_uint_32 s1   = (*(rp+2) << 8) | *(rp+3);
-            png_uint_32 s2   = (*(rp+4) << 8) | *(rp+5);
-            png_uint_32 red  = (png_uint_32)((s0-s1) & 0xffffL);
-            png_uint_32 blue = (png_uint_32)((s2-s1) & 0xffffL);
-            *(rp  ) = (png_byte)((red >> 8) & 0xff);
-            *(rp+1) = (png_byte)(red & 0xff);
-            *(rp+4) = (png_byte)((blue >> 8) & 0xff);
-            *(rp+5) = (png_byte)(blue & 0xff);
-         }
-      }
-   }
-}
-#endif /* PNG_MNG_FEATURES_SUPPORTED */
-#endif /* PNG_WRITE_SUPPORTED */
+\r
+/* pngwtran.c - transforms the data in a row for PNG writers\r
+ *\r
+ * Last changed in libpng 1.4.1 [February 25, 2010]\r
+ * Copyright (c) 1998-2010 Glenn Randers-Pehrson\r
+ * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)\r
+ * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)\r
+ *\r
+ * This code is released under the libpng license.\r
+ * For conditions of distribution and use, see the disclaimer\r
+ * and license in png.h\r
+ */\r
+\r
+#define PNG_NO_PEDANTIC_WARNINGS\r
+#include "png.h"\r
+#ifdef PNG_WRITE_SUPPORTED\r
+#include "pngpriv.h"\r
+\r
+/* Transform the data according to the user's wishes.  The order of\r
+ * transformations is significant.\r
+ */\r
+void /* PRIVATE */\r
+png_do_write_transformations(png_structp png_ptr)\r
+{\r
+   png_debug(1, "in png_do_write_transformations");\r
+\r
+   if (png_ptr == NULL)\r
+      return;\r
+\r
+#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED\r
+   if (png_ptr->transformations & PNG_USER_TRANSFORM)\r
+      if (png_ptr->write_user_transform_fn != NULL)\r
+        (*(png_ptr->write_user_transform_fn)) /* User write transform\r
+                                                 function */\r
+          (png_ptr,                    /* png_ptr */\r
+           &(png_ptr->row_info),       /* row_info:     */\r
+             /*  png_uint_32 width;          width of row */\r
+             /*  png_uint_32 rowbytes;       number of bytes in row */\r
+             /*  png_byte color_type;        color type of pixels */\r
+             /*  png_byte bit_depth;         bit depth of samples */\r
+             /*  png_byte channels;          number of channels (1-4) */\r
+             /*  png_byte pixel_depth;       bits per pixel (depth*channels) */\r
+           png_ptr->row_buf + 1);      /* start of pixel data for row */\r
+#endif\r
+#ifdef PNG_WRITE_FILLER_SUPPORTED\r
+   if (png_ptr->transformations & PNG_FILLER)\r
+      png_do_strip_filler(&(png_ptr->row_info), png_ptr->row_buf + 1,\r
+         png_ptr->flags);\r
+#endif\r
+#ifdef PNG_WRITE_PACKSWAP_SUPPORTED\r
+   if (png_ptr->transformations & PNG_PACKSWAP)\r
+      png_do_packswap(&(png_ptr->row_info), png_ptr->row_buf + 1);\r
+#endif\r
+#ifdef PNG_WRITE_PACK_SUPPORTED\r
+   if (png_ptr->transformations & PNG_PACK)\r
+      png_do_pack(&(png_ptr->row_info), png_ptr->row_buf + 1,\r
+         (png_uint_32)png_ptr->bit_depth);\r
+#endif\r
+#ifdef PNG_WRITE_SWAP_SUPPORTED\r
+   if (png_ptr->transformations & PNG_SWAP_BYTES)\r
+      png_do_swap(&(png_ptr->row_info), png_ptr->row_buf + 1);\r
+#endif\r
+#ifdef PNG_WRITE_SHIFT_SUPPORTED\r
+   if (png_ptr->transformations & PNG_SHIFT)\r
+      png_do_shift(&(png_ptr->row_info), png_ptr->row_buf + 1,\r
+         &(png_ptr->shift));\r
+#endif\r
+#ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED\r
+   if (png_ptr->transformations & PNG_SWAP_ALPHA)\r
+      png_do_write_swap_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1);\r
+#endif\r
+#ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED\r
+   if (png_ptr->transformations & PNG_INVERT_ALPHA)\r
+      png_do_write_invert_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1);\r
+#endif\r
+#ifdef PNG_WRITE_BGR_SUPPORTED\r
+   if (png_ptr->transformations & PNG_BGR)\r
+      png_do_bgr(&(png_ptr->row_info), png_ptr->row_buf + 1);\r
+#endif\r
+#ifdef PNG_WRITE_INVERT_SUPPORTED\r
+   if (png_ptr->transformations & PNG_INVERT_MONO)\r
+      png_do_invert(&(png_ptr->row_info), png_ptr->row_buf + 1);\r
+#endif\r
+}\r
+\r
+#ifdef PNG_WRITE_PACK_SUPPORTED\r
+/* Pack pixels into bytes.  Pass the true bit depth in bit_depth.  The\r
+ * row_info bit depth should be 8 (one pixel per byte).  The channels\r
+ * should be 1 (this only happens on grayscale and paletted images).\r
+ */\r
+void /* PRIVATE */\r
+png_do_pack(png_row_infop row_info, png_bytep row, png_uint_32 bit_depth)\r
+{\r
+   png_debug(1, "in png_do_pack");\r
+\r
+   if (row_info->bit_depth == 8 &&\r
+      row_info->channels == 1)\r
+   {\r
+      switch ((int)bit_depth)\r
+      {\r
+         case 1:\r
+         {\r
+            png_bytep sp, dp;\r
+            int mask, v;\r
+            png_uint_32 i;\r
+            png_uint_32 row_width = row_info->width;\r
+\r
+            sp = row;\r
+            dp = row;\r
+            mask = 0x80;\r
+            v = 0;\r
+\r
+            for (i = 0; i < row_width; i++)\r
+            {\r
+               if (*sp != 0)\r
+                  v |= mask;\r
+               sp++;\r
+               if (mask > 1)\r
+                  mask >>= 1;\r
+               else\r
+               {\r
+                  mask = 0x80;\r
+                  *dp = (png_byte)v;\r
+                  dp++;\r
+                  v = 0;\r
+               }\r
+            }\r
+            if (mask != 0x80)\r
+               *dp = (png_byte)v;\r
+            break;\r
+         }\r
+         case 2:\r
+         {\r
+            png_bytep sp, dp;\r
+            int shift, v;\r
+            png_uint_32 i;\r
+            png_uint_32 row_width = row_info->width;\r
+\r
+            sp = row;\r
+            dp = row;\r
+            shift = 6;\r
+            v = 0;\r
+            for (i = 0; i < row_width; i++)\r
+            {\r
+               png_byte value;\r
+\r
+               value = (png_byte)(*sp & 0x03);\r
+               v |= (value << shift);\r
+               if (shift == 0)\r
+               {\r
+                  shift = 6;\r
+                  *dp = (png_byte)v;\r
+                  dp++;\r
+                  v = 0;\r
+               }\r
+               else\r
+                  shift -= 2;\r
+               sp++;\r
+            }\r
+            if (shift != 6)\r
+               *dp = (png_byte)v;\r
+            break;\r
+         }\r
+         case 4:\r
+         {\r
+            png_bytep sp, dp;\r
+            int shift, v;\r
+            png_uint_32 i;\r
+            png_uint_32 row_width = row_info->width;\r
+\r
+            sp = row;\r
+            dp = row;\r
+            shift = 4;\r
+            v = 0;\r
+            for (i = 0; i < row_width; i++)\r
+            {\r
+               png_byte value;\r
+\r
+               value = (png_byte)(*sp & 0x0f);\r
+               v |= (value << shift);\r
+\r
+               if (shift == 0)\r
+               {\r
+                  shift = 4;\r
+                  *dp = (png_byte)v;\r
+                  dp++;\r
+                  v = 0;\r
+               }\r
+               else\r
+                  shift -= 4;\r
+\r
+               sp++;\r
+            }\r
+            if (shift != 4)\r
+               *dp = (png_byte)v;\r
+            break;\r
+         }\r
+      }\r
+      row_info->bit_depth = (png_byte)bit_depth;\r
+      row_info->pixel_depth = (png_byte)(bit_depth * row_info->channels);\r
+      row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,\r
+         row_info->width);\r
+   }\r
+}\r
+#endif\r
+\r
+#ifdef PNG_WRITE_SHIFT_SUPPORTED\r
+/* Shift pixel values to take advantage of whole range.  Pass the\r
+ * true number of bits in bit_depth.  The row should be packed\r
+ * according to row_info->bit_depth.  Thus, if you had a row of\r
+ * bit depth 4, but the pixels only had values from 0 to 7, you\r
+ * would pass 3 as bit_depth, and this routine would translate the\r
+ * data to 0 to 15.\r
+ */\r
+void /* PRIVATE */\r
+png_do_shift(png_row_infop row_info, png_bytep row, png_color_8p bit_depth)\r
+{\r
+   png_debug(1, "in png_do_shift");\r
+\r
+   if (\r
+      row_info->color_type != PNG_COLOR_TYPE_PALETTE)\r
+   {\r
+      int shift_start[4], shift_dec[4];\r
+      int channels = 0;\r
+\r
+      if (row_info->color_type & PNG_COLOR_MASK_COLOR)\r
+      {\r
+         shift_start[channels] = row_info->bit_depth - bit_depth->red;\r
+         shift_dec[channels] = bit_depth->red;\r
+         channels++;\r
+         shift_start[channels] = row_info->bit_depth - bit_depth->green;\r
+         shift_dec[channels] = bit_depth->green;\r
+         channels++;\r
+         shift_start[channels] = row_info->bit_depth - bit_depth->blue;\r
+         shift_dec[channels] = bit_depth->blue;\r
+         channels++;\r
+      }\r
+      else\r
+      {\r
+         shift_start[channels] = row_info->bit_depth - bit_depth->gray;\r
+         shift_dec[channels] = bit_depth->gray;\r
+         channels++;\r
+      }\r
+      if (row_info->color_type & PNG_COLOR_MASK_ALPHA)\r
+      {\r
+         shift_start[channels] = row_info->bit_depth - bit_depth->alpha;\r
+         shift_dec[channels] = bit_depth->alpha;\r
+         channels++;\r
+      }\r
+\r
+      /* With low row depths, could only be grayscale, so one channel */\r
+      if (row_info->bit_depth < 8)\r
+      {\r
+         png_bytep bp = row;\r
+         png_uint_32 i;\r
+         png_byte mask;\r
+         png_uint_32 row_bytes = row_info->rowbytes;\r
+\r
+         if (bit_depth->gray == 1 && row_info->bit_depth == 2)\r
+            mask = 0x55;\r
+         else if (row_info->bit_depth == 4 && bit_depth->gray == 3)\r
+            mask = 0x11;\r
+         else\r
+            mask = 0xff;\r
+\r
+         for (i = 0; i < row_bytes; i++, bp++)\r
+         {\r
+            png_uint_16 v;\r
+            int j;\r
+\r
+            v = *bp;\r
+            *bp = 0;\r
+            for (j = shift_start[0]; j > -shift_dec[0]; j -= shift_dec[0])\r
+            {\r
+               if (j > 0)\r
+                  *bp |= (png_byte)((v << j) & 0xff);\r
+               else\r
+                  *bp |= (png_byte)((v >> (-j)) & mask);\r
+            }\r
+         }\r
+      }\r
+      else if (row_info->bit_depth == 8)\r
+      {\r
+         png_bytep bp = row;\r
+         png_uint_32 i;\r
+         png_uint_32 istop = channels * row_info->width;\r
+\r
+         for (i = 0; i < istop; i++, bp++)\r
+         {\r
+\r
+            png_uint_16 v;\r
+            int j;\r
+            int c = (int)(i%channels);\r
+\r
+            v = *bp;\r
+            *bp = 0;\r
+            for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c])\r
+            {\r
+               if (j > 0)\r
+                  *bp |= (png_byte)((v << j) & 0xff);\r
+               else\r
+                  *bp |= (png_byte)((v >> (-j)) & 0xff);\r
+            }\r
+         }\r
+      }\r
+      else\r
+      {\r
+         png_bytep bp;\r
+         png_uint_32 i;\r
+         png_uint_32 istop = channels * row_info->width;\r
+\r
+         for (bp = row, i = 0; i < istop; i++)\r
+         {\r
+            int c = (int)(i%channels);\r
+            png_uint_16 value, v;\r
+            int j;\r
+\r
+            v = (png_uint_16)(((png_uint_16)(*bp) << 8) + *(bp + 1));\r
+            value = 0;\r
+            for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c])\r
+            {\r
+               if (j > 0)\r
+                  value |= (png_uint_16)((v << j) & (png_uint_16)0xffff);\r
+               else\r
+                  value |= (png_uint_16)((v >> (-j)) & (png_uint_16)0xffff);\r
+            }\r
+            *bp++ = (png_byte)(value >> 8);\r
+            *bp++ = (png_byte)(value & 0xff);\r
+         }\r
+      }\r
+   }\r
+}\r
+#endif\r
+\r
+#ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED\r
+void /* PRIVATE */\r
+png_do_write_swap_alpha(png_row_infop row_info, png_bytep row)\r
+{\r
+   png_debug(1, "in png_do_write_swap_alpha");\r
+\r
+   {\r
+      if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)\r
+      {\r
+         /* This converts from ARGB to RGBA */\r
+         if (row_info->bit_depth == 8)\r
+         {\r
+            png_bytep sp, dp;\r
+            png_uint_32 i;\r
+            png_uint_32 row_width = row_info->width;\r
+            for (i = 0, sp = dp = row; i < row_width; i++)\r
+            {\r
+               png_byte save = *(sp++);\r
+               *(dp++) = *(sp++);\r
+               *(dp++) = *(sp++);\r
+               *(dp++) = *(sp++);\r
+               *(dp++) = save;\r
+            }\r
+         }\r
+         /* This converts from AARRGGBB to RRGGBBAA */\r
+         else\r
+         {\r
+            png_bytep sp, dp;\r
+            png_uint_32 i;\r
+            png_uint_32 row_width = row_info->width;\r
+\r
+            for (i = 0, sp = dp = row; i < row_width; i++)\r
+            {\r
+               png_byte save[2];\r
+               save[0] = *(sp++);\r
+               save[1] = *(sp++);\r
+               *(dp++) = *(sp++);\r
+               *(dp++) = *(sp++);\r
+               *(dp++) = *(sp++);\r
+               *(dp++) = *(sp++);\r
+               *(dp++) = *(sp++);\r
+               *(dp++) = *(sp++);\r
+               *(dp++) = save[0];\r
+               *(dp++) = save[1];\r
+            }\r
+         }\r
+      }\r
+      else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)\r
+      {\r
+         /* This converts from AG to GA */\r
+         if (row_info->bit_depth == 8)\r
+         {\r
+            png_bytep sp, dp;\r
+            png_uint_32 i;\r
+            png_uint_32 row_width = row_info->width;\r
+\r
+            for (i = 0, sp = dp = row; i < row_width; i++)\r
+            {\r
+               png_byte save = *(sp++);\r
+               *(dp++) = *(sp++);\r
+               *(dp++) = save;\r
+            }\r
+         }\r
+         /* This converts from AAGG to GGAA */\r
+         else\r
+         {\r
+            png_bytep sp, dp;\r
+            png_uint_32 i;\r
+            png_uint_32 row_width = row_info->width;\r
+\r
+            for (i = 0, sp = dp = row; i < row_width; i++)\r
+            {\r
+               png_byte save[2];\r
+               save[0] = *(sp++);\r
+               save[1] = *(sp++);\r
+               *(dp++) = *(sp++);\r
+               *(dp++) = *(sp++);\r
+               *(dp++) = save[0];\r
+               *(dp++) = save[1];\r
+            }\r
+         }\r
+      }\r
+   }\r
+}\r
+#endif\r
+\r
+#ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED\r
+void /* PRIVATE */\r
+png_do_write_invert_alpha(png_row_infop row_info, png_bytep row)\r
+{\r
+   png_debug(1, "in png_do_write_invert_alpha");\r
+\r
+   {\r
+      if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)\r
+      {\r
+         /* This inverts the alpha channel in RGBA */\r
+         if (row_info->bit_depth == 8)\r
+         {\r
+            png_bytep sp, dp;\r
+            png_uint_32 i;\r
+            png_uint_32 row_width = row_info->width;\r
+            for (i = 0, sp = dp = row; i < row_width; i++)\r
+            {\r
+               /* Does nothing\r
+               *(dp++) = *(sp++);\r
+               *(dp++) = *(sp++);\r
+               *(dp++) = *(sp++);\r
+               */\r
+               sp+=3; dp = sp;\r
+               *(dp++) = (png_byte)(255 - *(sp++));\r
+            }\r
+         }\r
+         /* This inverts the alpha channel in RRGGBBAA */\r
+         else\r
+         {\r
+            png_bytep sp, dp;\r
+            png_uint_32 i;\r
+            png_uint_32 row_width = row_info->width;\r
+\r
+            for (i = 0, sp = dp = row; i < row_width; i++)\r
+            {\r
+               /* Does nothing\r
+               *(dp++) = *(sp++);\r
+               *(dp++) = *(sp++);\r
+               *(dp++) = *(sp++);\r
+               *(dp++) = *(sp++);\r
+               *(dp++) = *(sp++);\r
+               *(dp++) = *(sp++);\r
+               */\r
+               sp+=6; dp = sp;\r
+               *(dp++) = (png_byte)(255 - *(sp++));\r
+               *(dp++) = (png_byte)(255 - *(sp++));\r
+            }\r
+         }\r
+      }\r
+      else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)\r
+      {\r
+         /* This inverts the alpha channel in GA */\r
+         if (row_info->bit_depth == 8)\r
+         {\r
+            png_bytep sp, dp;\r
+            png_uint_32 i;\r
+            png_uint_32 row_width = row_info->width;\r
+\r
+            for (i = 0, sp = dp = row; i < row_width; i++)\r
+            {\r
+               *(dp++) = *(sp++);\r
+               *(dp++) = (png_byte)(255 - *(sp++));\r
+            }\r
+         }\r
+         /* This inverts the alpha channel in GGAA */\r
+         else\r
+         {\r
+            png_bytep sp, dp;\r
+            png_uint_32 i;\r
+            png_uint_32 row_width = row_info->width;\r
+\r
+            for (i = 0, sp = dp = row; i < row_width; i++)\r
+            {\r
+               /* Does nothing\r
+               *(dp++) = *(sp++);\r
+               *(dp++) = *(sp++);\r
+               */\r
+               sp+=2; dp = sp;\r
+               *(dp++) = (png_byte)(255 - *(sp++));\r
+               *(dp++) = (png_byte)(255 - *(sp++));\r
+            }\r
+         }\r
+      }\r
+   }\r
+}\r
+#endif\r
+\r
+#ifdef PNG_MNG_FEATURES_SUPPORTED\r
+/* Undoes intrapixel differencing  */\r
+void /* PRIVATE */\r
+png_do_write_intrapixel(png_row_infop row_info, png_bytep row)\r
+{\r
+   png_debug(1, "in png_do_write_intrapixel");\r
+\r
+   if (\r
+       (row_info->color_type & PNG_COLOR_MASK_COLOR))\r
+   {\r
+      int bytes_per_pixel;\r
+      png_uint_32 row_width = row_info->width;\r
+      if (row_info->bit_depth == 8)\r
+      {\r
+         png_bytep rp;\r
+         png_uint_32 i;\r
+\r
+         if (row_info->color_type == PNG_COLOR_TYPE_RGB)\r
+            bytes_per_pixel = 3;\r
+         else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)\r
+            bytes_per_pixel = 4;\r
+         else\r
+            return;\r
+\r
+         for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)\r
+         {\r
+            *(rp)   = (png_byte)((*rp     - *(rp+1))&0xff);\r
+            *(rp+2) = (png_byte)((*(rp+2) - *(rp+1))&0xff);\r
+         }\r
+      }\r
+      else if (row_info->bit_depth == 16)\r
+      {\r
+         png_bytep rp;\r
+         png_uint_32 i;\r
+\r
+         if (row_info->color_type == PNG_COLOR_TYPE_RGB)\r
+            bytes_per_pixel = 6;\r
+         else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)\r
+            bytes_per_pixel = 8;\r
+         else\r
+            return;\r
+\r
+         for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)\r
+         {\r
+            png_uint_32 s0   = (*(rp  ) << 8) | *(rp+1);\r
+            png_uint_32 s1   = (*(rp+2) << 8) | *(rp+3);\r
+            png_uint_32 s2   = (*(rp+4) << 8) | *(rp+5);\r
+            png_uint_32 red  = (png_uint_32)((s0 - s1) & 0xffffL);\r
+            png_uint_32 blue = (png_uint_32)((s2 - s1) & 0xffffL);\r
+            *(rp  ) = (png_byte)((red >> 8) & 0xff);\r
+            *(rp+1) = (png_byte)(red & 0xff);\r
+            *(rp+4) = (png_byte)((blue >> 8) & 0xff);\r
+            *(rp+5) = (png_byte)(blue & 0xff);\r
+         }\r
+      }\r
+   }\r
+}\r
+#endif /* PNG_MNG_FEATURES_SUPPORTED */\r
+#endif /* PNG_WRITE_SUPPORTED */\r
index 0774080..dc4aa14 100644 (file)
-
-/* pngwutil.c - utilities to write a PNG file
- *
- * Last changed in libpng 1.2.27 [April 29, 2008]
- * For conditions of distribution and use, see copyright notice in png.h
- * Copyright (c) 1998-2008 Glenn Randers-Pehrson
- * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
- * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
- */
-
-#define PNG_INTERNAL
-#include "png.h"
-#ifdef PNG_WRITE_SUPPORTED
-
-/* Place a 32-bit number into a buffer in PNG byte order.  We work
- * with unsigned numbers for convenience, although one supported
- * ancillary chunk uses signed (two's complement) numbers.
- */
-void PNGAPI
-png_save_uint_32(png_bytep buf, png_uint_32 i)
-{
-   buf[0] = (png_byte)((i >> 24) & 0xff);
-   buf[1] = (png_byte)((i >> 16) & 0xff);
-   buf[2] = (png_byte)((i >> 8) & 0xff);
-   buf[3] = (png_byte)(i & 0xff);
-}
-
-/* The png_save_int_32 function assumes integers are stored in two's
- * complement format.  If this isn't the case, then this routine needs to
- * be modified to write data in two's complement format.
- */
-void PNGAPI
-png_save_int_32(png_bytep buf, png_int_32 i)
-{
-   buf[0] = (png_byte)((i >> 24) & 0xff);
-   buf[1] = (png_byte)((i >> 16) & 0xff);
-   buf[2] = (png_byte)((i >> 8) & 0xff);
-   buf[3] = (png_byte)(i & 0xff);
-}
-
-/* Place a 16-bit number into a buffer in PNG byte order.
- * The parameter is declared unsigned int, not png_uint_16,
- * just to avoid potential problems on pre-ANSI C compilers.
- */
-void PNGAPI
-png_save_uint_16(png_bytep buf, unsigned int i)
-{
-   buf[0] = (png_byte)((i >> 8) & 0xff);
-   buf[1] = (png_byte)(i & 0xff);
-}
-
-/* Write a PNG chunk all at once.  The type is an array of ASCII characters
- * representing the chunk name.  The array must be at least 4 bytes in
- * length, and does not need to be null terminated.  To be safe, pass the
- * pre-defined chunk names here, and if you need a new one, define it
- * where the others are defined.  The length is the length of the data.
- * All the data must be present.  If that is not possible, use the
- * png_write_chunk_start(), png_write_chunk_data(), and png_write_chunk_end()
- * functions instead.
- */
-void PNGAPI
-png_write_chunk(png_structp png_ptr, png_bytep chunk_name,
-   png_bytep data, png_size_t length)
-{
-   if(png_ptr == NULL) return;
-   png_write_chunk_start(png_ptr, chunk_name, (png_uint_32)length);
-   png_write_chunk_data(png_ptr, data, length);
-   png_write_chunk_end(png_ptr);
-}
-
-/* Write the start of a PNG chunk.  The type is the chunk type.
- * The total_length is the sum of the lengths of all the data you will be
- * passing in png_write_chunk_data().
- */
-void PNGAPI
-png_write_chunk_start(png_structp png_ptr, png_bytep chunk_name,
-   png_uint_32 length)
-{
-   png_byte buf[4];
-   png_debug2(0, "Writing %s chunk (%lu bytes)\n", chunk_name, length);
-   if(png_ptr == NULL) return;
-
-   /* write the length */
-   png_save_uint_32(buf, length);
-   png_write_data(png_ptr, buf, (png_size_t)4);
-
-   /* write the chunk name */
-   png_write_data(png_ptr, chunk_name, (png_size_t)4);
-   /* reset the crc and run it over the chunk name */
-   png_reset_crc(png_ptr);
-   png_calculate_crc(png_ptr, chunk_name, (png_size_t)4);
-}
-
-/* Write the data of a PNG chunk started with png_write_chunk_start().
- * Note that multiple calls to this function are allowed, and that the
- * sum of the lengths from these calls *must* add up to the total_length
- * given to png_write_chunk_start().
- */
-void PNGAPI
-png_write_chunk_data(png_structp png_ptr, png_bytep data, png_size_t length)
-{
-   /* write the data, and run the CRC over it */
-   if(png_ptr == NULL) return;
-   if (data != NULL && length > 0)
-   {
-      png_calculate_crc(png_ptr, data, length);
-      png_write_data(png_ptr, data, length);
-   }
-}
-
-/* Finish a chunk started with png_write_chunk_start(). */
-void PNGAPI
-png_write_chunk_end(png_structp png_ptr)
-{
-   png_byte buf[4];
-
-   if(png_ptr == NULL) return;
-
-   /* write the crc */
-   png_save_uint_32(buf, png_ptr->crc);
-
-   png_write_data(png_ptr, buf, (png_size_t)4);
-}
-
-/* Simple function to write the signature.  If we have already written
- * the magic bytes of the signature, or more likely, the PNG stream is
- * being embedded into another stream and doesn't need its own signature,
- * we should call png_set_sig_bytes() to tell libpng how many of the
- * bytes have already been written.
- */
-void /* PRIVATE */
-png_write_sig(png_structp png_ptr)
-{
-   png_byte png_signature[8] = {137, 80, 78, 71, 13, 10, 26, 10};
-   /* write the rest of the 8 byte signature */
-   png_write_data(png_ptr, &png_signature[png_ptr->sig_bytes],
-      (png_size_t)8 - png_ptr->sig_bytes);
-   if(png_ptr->sig_bytes < 3)
-      png_ptr->mode |= PNG_HAVE_PNG_SIGNATURE;
-}
-
-#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_iCCP_SUPPORTED)
-/*
- * This pair of functions encapsulates the operation of (a) compressing a
- * text string, and (b) issuing it later as a series of chunk data writes.
- * The compression_state structure is shared context for these functions
- * set up by the caller in order to make the whole mess thread-safe.
- */
-
-typedef struct
-{
-    char *input;   /* the uncompressed input data */
-    int input_len;   /* its length */
-    int num_output_ptr; /* number of output pointers used */
-    int max_output_ptr; /* size of output_ptr */
-    png_charpp output_ptr; /* array of pointers to output */
-} compression_state;
-
-/* compress given text into storage in the png_ptr structure */
-static int /* PRIVATE */
-png_text_compress(png_structp png_ptr,
-        png_charp text, png_size_t text_len, int compression,
-        compression_state *comp)
-{
-   int ret;
-
-   comp->num_output_ptr = 0;
-   comp->max_output_ptr = 0;
-   comp->output_ptr = NULL;
-   comp->input = NULL;
-   comp->input_len = 0;
-
-   /* we may just want to pass the text right through */
-   if (compression == PNG_TEXT_COMPRESSION_NONE)
-   {
-       comp->input = text;
-       comp->input_len = text_len;
-       return((int)text_len);
-   }
-
-   if (compression >= PNG_TEXT_COMPRESSION_LAST)
-   {
-#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
-      char msg[50];
-      png_snprintf(msg, 50, "Unknown compression type %d", compression);
-      png_warning(png_ptr, msg);
-#else
-      png_warning(png_ptr, "Unknown compression type");
-#endif
-   }
-
-   /* We can't write the chunk until we find out how much data we have,
-    * which means we need to run the compressor first and save the
-    * output.  This shouldn't be a problem, as the vast majority of
-    * comments should be reasonable, but we will set up an array of
-    * malloc'd pointers to be sure.
-    *
-    * If we knew the application was well behaved, we could simplify this
-    * greatly by assuming we can always malloc an output buffer large
-    * enough to hold the compressed text ((1001 * text_len / 1000) + 12)
-    * and malloc this directly.  The only time this would be a bad idea is
-    * if we can't malloc more than 64K and we have 64K of random input
-    * data, or if the input string is incredibly large (although this
-    * wouldn't cause a failure, just a slowdown due to swapping).
-    */
-
-   /* set up the compression buffers */
-   png_ptr->zstream.avail_in = (uInt)text_len;
-   png_ptr->zstream.next_in = (Bytef *)text;
-   png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
-   png_ptr->zstream.next_out = (Bytef *)png_ptr->zbuf;
-
-   /* this is the same compression loop as in png_write_row() */
-   do
-   {
-      /* compress the data */
-      ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);
-      if (ret != Z_OK)
-      {
-         /* error */
-         if (png_ptr->zstream.msg != NULL)
-            png_error(png_ptr, png_ptr->zstream.msg);
-         else
-            png_error(png_ptr, "zlib error");
-      }
-      /* check to see if we need more room */
-      if (!(png_ptr->zstream.avail_out))
-      {
-         /* make sure the output array has room */
-         if (comp->num_output_ptr >= comp->max_output_ptr)
-         {
-            int old_max;
-
-            old_max = comp->max_output_ptr;
-            comp->max_output_ptr = comp->num_output_ptr + 4;
-            if (comp->output_ptr != NULL)
-            {
-               png_charpp old_ptr;
-
-               old_ptr = comp->output_ptr;
-               comp->output_ptr = (png_charpp)png_malloc(png_ptr,
-                  (png_uint_32)(comp->max_output_ptr *
-                  png_sizeof (png_charpp)));
-               png_memcpy(comp->output_ptr, old_ptr, old_max
-                  * png_sizeof (png_charp));
-               png_free(png_ptr, old_ptr);
-            }
-            else
-               comp->output_ptr = (png_charpp)png_malloc(png_ptr,
-                  (png_uint_32)(comp->max_output_ptr *
-                  png_sizeof (png_charp)));
-         }
-
-         /* save the data */
-         comp->output_ptr[comp->num_output_ptr] = (png_charp)png_malloc(png_ptr,
-            (png_uint_32)png_ptr->zbuf_size);
-         png_memcpy(comp->output_ptr[comp->num_output_ptr], png_ptr->zbuf,
-            png_ptr->zbuf_size);
-         comp->num_output_ptr++;
-
-         /* and reset the buffer */
-         png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
-         png_ptr->zstream.next_out = png_ptr->zbuf;
-      }
-   /* continue until we don't have any more to compress */
-   } while (png_ptr->zstream.avail_in);
-
-   /* finish the compression */
-   do
-   {
-      /* tell zlib we are finished */
-      ret = deflate(&png_ptr->zstream, Z_FINISH);
-
-      if (ret == Z_OK)
-      {
-         /* check to see if we need more room */
-         if (!(png_ptr->zstream.avail_out))
-         {
-            /* check to make sure our output array has room */
-            if (comp->num_output_ptr >= comp->max_output_ptr)
-            {
-               int old_max;
-
-               old_max = comp->max_output_ptr;
-               comp->max_output_ptr = comp->num_output_ptr + 4;
-               if (comp->output_ptr != NULL)
-               {
-                  png_charpp old_ptr;
-
-                  old_ptr = comp->output_ptr;
-                  /* This could be optimized to realloc() */
-                  comp->output_ptr = (png_charpp)png_malloc(png_ptr,
-                     (png_uint_32)(comp->max_output_ptr *
-                     png_sizeof (png_charpp)));
-                  png_memcpy(comp->output_ptr, old_ptr,
-                     old_max * png_sizeof (png_charp));
-                  png_free(png_ptr, old_ptr);
-               }
-               else
-                  comp->output_ptr = (png_charpp)png_malloc(png_ptr,
-                     (png_uint_32)(comp->max_output_ptr *
-                     png_sizeof (png_charp)));
-            }
-
-            /* save off the data */
-            comp->output_ptr[comp->num_output_ptr] =
-               (png_charp)png_malloc(png_ptr, (png_uint_32)png_ptr->zbuf_size);
-            png_memcpy(comp->output_ptr[comp->num_output_ptr], png_ptr->zbuf,
-               png_ptr->zbuf_size);
-            comp->num_output_ptr++;
-
-            /* and reset the buffer pointers */
-            png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
-            png_ptr->zstream.next_out = png_ptr->zbuf;
-         }
-      }
-      else if (ret != Z_STREAM_END)
-      {
-         /* we got an error */
-         if (png_ptr->zstream.msg != NULL)
-            png_error(png_ptr, png_ptr->zstream.msg);
-         else
-            png_error(png_ptr, "zlib error");
-      }
-   } while (ret != Z_STREAM_END);
-
-   /* text length is number of buffers plus last buffer */
-   text_len = png_ptr->zbuf_size * comp->num_output_ptr;
-   if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
-      text_len += png_ptr->zbuf_size - (png_size_t)png_ptr->zstream.avail_out;
-
-   return((int)text_len);
-}
-
-/* ship the compressed text out via chunk writes */
-static void /* PRIVATE */
-png_write_compressed_data_out(png_structp png_ptr, compression_state *comp)
-{
-   int i;
-
-   /* handle the no-compression case */
-   if (comp->input)
-   {
-       png_write_chunk_data(png_ptr, (png_bytep)comp->input,
-                            (png_size_t)comp->input_len);
-       return;
-   }
-
-   /* write saved output buffers, if any */
-   for (i = 0; i < comp->num_output_ptr; i++)
-   {
-      png_write_chunk_data(png_ptr,(png_bytep)comp->output_ptr[i],
-         png_ptr->zbuf_size);
-      png_free(png_ptr, comp->output_ptr[i]);
-      comp->output_ptr[i]=NULL;
-   }
-   if (comp->max_output_ptr != 0)
-      png_free(png_ptr, comp->output_ptr);
-      comp->output_ptr=NULL;
-   /* write anything left in zbuf */
-   if (png_ptr->zstream.avail_out < (png_uint_32)png_ptr->zbuf_size)
-      png_write_chunk_data(png_ptr, png_ptr->zbuf,
-         png_ptr->zbuf_size - png_ptr->zstream.avail_out);
-
-   /* reset zlib for another zTXt/iTXt or image data */
-   deflateReset(&png_ptr->zstream);
-   png_ptr->zstream.data_type = Z_BINARY;
-}
-#endif
-
-/* Write the IHDR chunk, and update the png_struct with the necessary
- * information.  Note that the rest of this code depends upon this
- * information being correct.
- */
-void /* PRIVATE */
-png_write_IHDR(png_structp png_ptr, png_uint_32 width, png_uint_32 height,
-   int bit_depth, int color_type, int compression_type, int filter_type,
-   int interlace_type)
-{
-#ifdef PNG_USE_LOCAL_ARRAYS
-   PNG_IHDR;
-#endif
-   int ret;
-
-   png_byte buf[13]; /* buffer to store the IHDR info */
-
-   png_debug(1, "in png_write_IHDR\n");
-   /* Check that we have valid input data from the application info */
-   switch (color_type)
-   {
-      case PNG_COLOR_TYPE_GRAY:
-         switch (bit_depth)
-         {
-            case 1:
-            case 2:
-            case 4:
-            case 8:
-            case 16: png_ptr->channels = 1; break;
-            default: png_error(png_ptr,"Invalid bit depth for grayscale image");
-         }
-         break;
-      case PNG_COLOR_TYPE_RGB:
-         if (bit_depth != 8 && bit_depth != 16)
-            png_error(png_ptr, "Invalid bit depth for RGB image");
-         png_ptr->channels = 3;
-         break;
-      case PNG_COLOR_TYPE_PALETTE:
-         switch (bit_depth)
-         {
-            case 1:
-            case 2:
-            case 4:
-            case 8: png_ptr->channels = 1; break;
-            default: png_error(png_ptr, "Invalid bit depth for paletted image");
-         }
-         break;
-      case PNG_COLOR_TYPE_GRAY_ALPHA:
-         if (bit_depth != 8 && bit_depth != 16)
-            png_error(png_ptr, "Invalid bit depth for grayscale+alpha image");
-         png_ptr->channels = 2;
-         break;
-      case PNG_COLOR_TYPE_RGB_ALPHA:
-         if (bit_depth != 8 && bit_depth != 16)
-            png_error(png_ptr, "Invalid bit depth for RGBA image");
-         png_ptr->channels = 4;
-         break;
-      default:
-         png_error(png_ptr, "Invalid image color type specified");
-   }
-
-   if (compression_type != PNG_COMPRESSION_TYPE_BASE)
-   {
-      png_warning(png_ptr, "Invalid compression type specified");
-      compression_type = PNG_COMPRESSION_TYPE_BASE;
-   }
-
-   /* Write filter_method 64 (intrapixel differencing) only if
-    * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
-    * 2. Libpng did not write a PNG signature (this filter_method is only
-    *    used in PNG datastreams that are embedded in MNG datastreams) and
-    * 3. The application called png_permit_mng_features with a mask that
-    *    included PNG_FLAG_MNG_FILTER_64 and
-    * 4. The filter_method is 64 and
-    * 5. The color_type is RGB or RGBA
-    */
-   if (
-#if defined(PNG_MNG_FEATURES_SUPPORTED)
-      !((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
-      ((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE) == 0) &&
-      (color_type == PNG_COLOR_TYPE_RGB ||
-       color_type == PNG_COLOR_TYPE_RGB_ALPHA) &&
-      (filter_type == PNG_INTRAPIXEL_DIFFERENCING)) &&
-#endif
-      filter_type != PNG_FILTER_TYPE_BASE)
-   {
-      png_warning(png_ptr, "Invalid filter type specified");
-      filter_type = PNG_FILTER_TYPE_BASE;
-   }
-
-#ifdef PNG_WRITE_INTERLACING_SUPPORTED
-   if (interlace_type != PNG_INTERLACE_NONE &&
-      interlace_type != PNG_INTERLACE_ADAM7)
-   {
-      png_warning(png_ptr, "Invalid interlace type specified");
-      interlace_type = PNG_INTERLACE_ADAM7;
-   }
-#else
-   interlace_type=PNG_INTERLACE_NONE;
-#endif
-
-   /* save off the relevent information */
-   png_ptr->bit_depth = (png_byte)bit_depth;
-   png_ptr->color_type = (png_byte)color_type;
-   png_ptr->interlaced = (png_byte)interlace_type;
-#if defined(PNG_MNG_FEATURES_SUPPORTED)
-   png_ptr->filter_type = (png_byte)filter_type;
-#endif
-   png_ptr->compression_type = (png_byte)compression_type;
-   png_ptr->width = width;
-   png_ptr->height = height;
-
-   png_ptr->pixel_depth = (png_byte)(bit_depth * png_ptr->channels);
-   png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, width);
-   /* set the usr info, so any transformations can modify it */
-   png_ptr->usr_width = png_ptr->width;
-   png_ptr->usr_bit_depth = png_ptr->bit_depth;
-   png_ptr->usr_channels = png_ptr->channels;
-
-   /* pack the header information into the buffer */
-   png_save_uint_32(buf, width);
-   png_save_uint_32(buf + 4, height);
-   buf[8] = (png_byte)bit_depth;
-   buf[9] = (png_byte)color_type;
-   buf[10] = (png_byte)compression_type;
-   buf[11] = (png_byte)filter_type;
-   buf[12] = (png_byte)interlace_type;
-
-   /* write the chunk */
-   png_write_chunk(png_ptr, png_IHDR, buf, (png_size_t)13);
-
-   /* initialize zlib with PNG info */
-   png_ptr->zstream.zalloc = png_zalloc;
-   png_ptr->zstream.zfree = png_zfree;
-   png_ptr->zstream.opaque = (voidpf)png_ptr;
-   if (!(png_ptr->do_filter))
-   {
-      if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE ||
-         png_ptr->bit_depth < 8)
-         png_ptr->do_filter = PNG_FILTER_NONE;
-      else
-         png_ptr->do_filter = PNG_ALL_FILTERS;
-   }
-   if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_STRATEGY))
-   {
-      if (png_ptr->do_filter != PNG_FILTER_NONE)
-         png_ptr->zlib_strategy = Z_FILTERED;
-      else
-         png_ptr->zlib_strategy = Z_DEFAULT_STRATEGY;
-   }
-   if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_LEVEL))
-      png_ptr->zlib_level = Z_DEFAULT_COMPRESSION;
-   if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL))
-      png_ptr->zlib_mem_level = 8;
-   if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS))
-      png_ptr->zlib_window_bits = 15;
-   if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_METHOD))
-      png_ptr->zlib_method = 8;
-   ret = deflateInit2(&png_ptr->zstream, png_ptr->zlib_level,
-         png_ptr->zlib_method, png_ptr->zlib_window_bits,
-         png_ptr->zlib_mem_level, png_ptr->zlib_strategy);
-   if (ret != Z_OK)
-   {
-      if (ret == Z_VERSION_ERROR) png_error(png_ptr,
-          "zlib failed to initialize compressor -- version error");
-      if (ret == Z_STREAM_ERROR) png_error(png_ptr,
-           "zlib failed to initialize compressor -- stream error");
-      if (ret == Z_MEM_ERROR) png_error(png_ptr,
-           "zlib failed to initialize compressor -- mem error");
-      png_error(png_ptr, "zlib failed to initialize compressor");
-   }
-   png_ptr->zstream.next_out = png_ptr->zbuf;
-   png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
-   /* libpng is not interested in zstream.data_type */
-   /* set it to a predefined value, to avoid its evaluation inside zlib */
-   png_ptr->zstream.data_type = Z_BINARY;
-
-   png_ptr->mode = PNG_HAVE_IHDR;
-}
-
-/* write the palette.  We are careful not to trust png_color to be in the
- * correct order for PNG, so people can redefine it to any convenient
- * structure.
- */
-void /* PRIVATE */
-png_write_PLTE(png_structp png_ptr, png_colorp palette, png_uint_32 num_pal)
-{
-#ifdef PNG_USE_LOCAL_ARRAYS
-   PNG_PLTE;
-#endif
-   png_uint_32 i;
-   png_colorp pal_ptr;
-   png_byte buf[3];
-
-   png_debug(1, "in png_write_PLTE\n");
-   if ((
-#if defined(PNG_MNG_FEATURES_SUPPORTED)
-        !(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE) &&
-#endif
-        num_pal == 0) || num_pal > 256)
-   {
-     if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
-     {
-        png_error(png_ptr, "Invalid number of colors in palette");
-     }
-     else
-     {
-        png_warning(png_ptr, "Invalid number of colors in palette");
-        return;
-     }
-   }
-
-   if (!(png_ptr->color_type&PNG_COLOR_MASK_COLOR))
-   {
-      png_warning(png_ptr,
-        "Ignoring request to write a PLTE chunk in grayscale PNG");
-      return;
-   }
-
-   png_ptr->num_palette = (png_uint_16)num_pal;
-   png_debug1(3, "num_palette = %d\n", png_ptr->num_palette);
-
-   png_write_chunk_start(png_ptr, png_PLTE, num_pal * 3);
-#ifndef PNG_NO_POINTER_INDEXING
-   for (i = 0, pal_ptr = palette; i < num_pal; i++, pal_ptr++)
-   {
-      buf[0] = pal_ptr->red;
-      buf[1] = pal_ptr->green;
-      buf[2] = pal_ptr->blue;
-      png_write_chunk_data(png_ptr, buf, (png_size_t)3);
-   }
-#else
-   /* This is a little slower but some buggy compilers need to do this instead */
-   pal_ptr=palette;
-   for (i = 0; i < num_pal; i++)
-   {
-      buf[0] = pal_ptr[i].red;
-      buf[1] = pal_ptr[i].green;
-      buf[2] = pal_ptr[i].blue;
-      png_write_chunk_data(png_ptr, buf, (png_size_t)3);
-   }
-#endif
-   png_write_chunk_end(png_ptr);
-   png_ptr->mode |= PNG_HAVE_PLTE;
-}
-
-/* write an IDAT chunk */
-void /* PRIVATE */
-png_write_IDAT(png_structp png_ptr, png_bytep data, png_size_t length)
-{
-#ifdef PNG_USE_LOCAL_ARRAYS
-   PNG_IDAT;
-#endif
-   png_debug(1, "in png_write_IDAT\n");
-
-   /* Optimize the CMF field in the zlib stream. */
-   /* This hack of the zlib stream is compliant to the stream specification. */
-   if (!(png_ptr->mode & PNG_HAVE_IDAT) &&
-       png_ptr->compression_type == PNG_COMPRESSION_TYPE_BASE)
-   {
-      unsigned int z_cmf = data[0];  /* zlib compression method and flags */
-      if ((z_cmf & 0x0f) == 8 && (z_cmf & 0xf0) <= 0x70)
-      {
-         /* Avoid memory underflows and multiplication overflows. */
-         /* The conditions below are practically always satisfied;
-            however, they still must be checked. */
-         if (length >= 2 &&
-             png_ptr->height < 16384 && png_ptr->width < 16384)
-         {
-            png_uint_32 uncompressed_idat_size = png_ptr->height *
-               ((png_ptr->width *
-               png_ptr->channels * png_ptr->bit_depth + 15) >> 3);
-            unsigned int z_cinfo = z_cmf >> 4;
-            unsigned int half_z_window_size = 1 << (z_cinfo + 7);
-            while (uncompressed_idat_size <= half_z_window_size &&
-                   half_z_window_size >= 256)
-            {
-               z_cinfo--;
-               half_z_window_size >>= 1;
-            }
-            z_cmf = (z_cmf & 0x0f) | (z_cinfo << 4);
-            if (data[0] != (png_byte)z_cmf)
-            {
-               data[0] = (png_byte)z_cmf;
-               data[1] &= 0xe0;
-               data[1] += (png_byte)(0x1f - ((z_cmf << 8) + data[1]) % 0x1f);
-            }
-         }
-      }
-      else
-         png_error(png_ptr,
-            "Invalid zlib compression method or flags in IDAT");
-   }
-
-   png_write_chunk(png_ptr, png_IDAT, data, length);
-   png_ptr->mode |= PNG_HAVE_IDAT;
-}
-
-/* write an IEND chunk */
-void /* PRIVATE */
-png_write_IEND(png_structp png_ptr)
-{
-#ifdef PNG_USE_LOCAL_ARRAYS
-   PNG_IEND;
-#endif
-   png_debug(1, "in png_write_IEND\n");
-   png_write_chunk(png_ptr, png_IEND, png_bytep_NULL,
-     (png_size_t)0);
-   png_ptr->mode |= PNG_HAVE_IEND;
-}
-
-#if defined(PNG_WRITE_gAMA_SUPPORTED)
-/* write a gAMA chunk */
-#ifdef PNG_FLOATING_POINT_SUPPORTED
-void /* PRIVATE */
-png_write_gAMA(png_structp png_ptr, double file_gamma)
-{
-#ifdef PNG_USE_LOCAL_ARRAYS
-   PNG_gAMA;
-#endif
-   png_uint_32 igamma;
-   png_byte buf[4];
-
-   png_debug(1, "in png_write_gAMA\n");
-   /* file_gamma is saved in 1/100,000ths */
-   igamma = (png_uint_32)(file_gamma * 100000.0 + 0.5);
-   png_save_uint_32(buf, igamma);
-   png_write_chunk(png_ptr, png_gAMA, buf, (png_size_t)4);
-}
-#endif
-#ifdef PNG_FIXED_POINT_SUPPORTED
-void /* PRIVATE */
-png_write_gAMA_fixed(png_structp png_ptr, png_fixed_point file_gamma)
-{
-#ifdef PNG_USE_LOCAL_ARRAYS
-   PNG_gAMA;
-#endif
-   png_byte buf[4];
-
-   png_debug(1, "in png_write_gAMA\n");
-   /* file_gamma is saved in 1/100,000ths */
-   png_save_uint_32(buf, (png_uint_32)file_gamma);
-   png_write_chunk(png_ptr, png_gAMA, buf, (png_size_t)4);
-}
-#endif
-#endif
-
-#if defined(PNG_WRITE_sRGB_SUPPORTED)
-/* write a sRGB chunk */
-void /* PRIVATE */
-png_write_sRGB(png_structp png_ptr, int srgb_intent)
-{
-#ifdef PNG_USE_LOCAL_ARRAYS
-   PNG_sRGB;
-#endif
-   png_byte buf[1];
-
-   png_debug(1, "in png_write_sRGB\n");
-   if(srgb_intent >= PNG_sRGB_INTENT_LAST)
-         png_warning(png_ptr,
-            "Invalid sRGB rendering intent specified");
-   buf[0]=(png_byte)srgb_intent;
-   png_write_chunk(png_ptr, png_sRGB, buf, (png_size_t)1);
-}
-#endif
-
-#if defined(PNG_WRITE_iCCP_SUPPORTED)
-/* write an iCCP chunk */
-void /* PRIVATE */
-png_write_iCCP(png_structp png_ptr, png_charp name, int compression_type,
-   png_charp profile, int profile_len)
-{
-#ifdef PNG_USE_LOCAL_ARRAYS
-   PNG_iCCP;
-#endif
-   png_size_t name_len;
-   png_charp new_name;
-   compression_state comp;
-   int embedded_profile_len = 0;
-
-   png_debug(1, "in png_write_iCCP\n");
-
-   comp.num_output_ptr = 0;
-   comp.max_output_ptr = 0;
-   comp.output_ptr = NULL;
-   comp.input = NULL;
-   comp.input_len = 0;
-
-   if (name == NULL || (name_len = png_check_keyword(png_ptr, name,
-      &new_name)) == 0)
-   {
-      png_warning(png_ptr, "Empty keyword in iCCP chunk");
-      return;
-   }
-
-   if (compression_type != PNG_COMPRESSION_TYPE_BASE)
-      png_warning(png_ptr, "Unknown compression type in iCCP chunk");
-
-   if (profile == NULL)
-      profile_len = 0;
-
-   if (profile_len > 3)
-      embedded_profile_len =
-          ((*( (png_bytep)profile  ))<<24) |
-          ((*( (png_bytep)profile+1))<<16) |
-          ((*( (png_bytep)profile+2))<< 8) |
-          ((*( (png_bytep)profile+3))    );
-
-   if (profile_len < embedded_profile_len)
-     {
-        png_warning(png_ptr,
-          "Embedded profile length too large in iCCP chunk");
-        return;
-     }
-
-   if (profile_len > embedded_profile_len)
-     {
-        png_warning(png_ptr,
-          "Truncating profile to actual length in iCCP chunk");
-        profile_len = embedded_profile_len;
-     }
-
-   if (profile_len)
-       profile_len = png_text_compress(png_ptr, profile, (png_size_t)profile_len,
-          PNG_COMPRESSION_TYPE_BASE, &comp);
-
-   /* make sure we include the NULL after the name and the compression type */
-   png_write_chunk_start(png_ptr, png_iCCP,
-          (png_uint_32)name_len+profile_len+2);
-   new_name[name_len+1]=0x00;
-   png_write_chunk_data(png_ptr, (png_bytep)new_name, name_len + 2);
-
-   if (profile_len)
-      png_write_compressed_data_out(png_ptr, &comp);
-
-   png_write_chunk_end(png_ptr);
-   png_free(png_ptr, new_name);
-}
-#endif
-
-#if defined(PNG_WRITE_sPLT_SUPPORTED)
-/* write a sPLT chunk */
-void /* PRIVATE */
-png_write_sPLT(png_structp png_ptr, png_sPLT_tp spalette)
-{
-#ifdef PNG_USE_LOCAL_ARRAYS
-   PNG_sPLT;
-#endif
-   png_size_t name_len;
-   png_charp new_name;
-   png_byte entrybuf[10];
-   int entry_size = (spalette->depth == 8 ? 6 : 10);
-   int palette_size = entry_size * spalette->nentries;
-   png_sPLT_entryp ep;
-#ifdef PNG_NO_POINTER_INDEXING
-   int i;
-#endif
-
-   png_debug(1, "in png_write_sPLT\n");
-   if (spalette->name == NULL || (name_len = png_check_keyword(png_ptr,
-      spalette->name, &new_name))==0)
-   {
-      png_warning(png_ptr, "Empty keyword in sPLT chunk");
-      return;
-   }
-
-   /* make sure we include the NULL after the name */
-   png_write_chunk_start(png_ptr, png_sPLT,
-          (png_uint_32)(name_len + 2 + palette_size));
-   png_write_chunk_data(png_ptr, (png_bytep)new_name, name_len + 1);
-   png_write_chunk_data(png_ptr, (png_bytep)&spalette->depth, 1);
-
-   /* loop through each palette entry, writing appropriately */
-#ifndef PNG_NO_POINTER_INDEXING
-   for (ep = spalette->entries; ep<spalette->entries+spalette->nentries; ep++)
-   {
-       if (spalette->depth == 8)
-       {
-           entrybuf[0] = (png_byte)ep->red;
-           entrybuf[1] = (png_byte)ep->green;
-           entrybuf[2] = (png_byte)ep->blue;
-           entrybuf[3] = (png_byte)ep->alpha;
-           png_save_uint_16(entrybuf + 4, ep->frequency);
-       }
-       else
-       {
-           png_save_uint_16(entrybuf + 0, ep->red);
-           png_save_uint_16(entrybuf + 2, ep->green);
-           png_save_uint_16(entrybuf + 4, ep->blue);
-           png_save_uint_16(entrybuf + 6, ep->alpha);
-           png_save_uint_16(entrybuf + 8, ep->frequency);
-       }
-       png_write_chunk_data(png_ptr, entrybuf, (png_size_t)entry_size);
-   }
-#else
-   ep=spalette->entries;
-   for (i=0; i>spalette->nentries; i++)
-   {
-       if (spalette->depth == 8)
-       {
-           entrybuf[0] = (png_byte)ep[i].red;
-           entrybuf[1] = (png_byte)ep[i].green;
-           entrybuf[2] = (png_byte)ep[i].blue;
-           entrybuf[3] = (png_byte)ep[i].alpha;
-           png_save_uint_16(entrybuf + 4, ep[i].frequency);
-       }
-       else
-       {
-           png_save_uint_16(entrybuf + 0, ep[i].red);
-           png_save_uint_16(entrybuf + 2, ep[i].green);
-           png_save_uint_16(entrybuf + 4, ep[i].blue);
-           png_save_uint_16(entrybuf + 6, ep[i].alpha);
-           png_save_uint_16(entrybuf + 8, ep[i].frequency);
-       }
-       png_write_chunk_data(png_ptr, entrybuf, entry_size);
-   }
-#endif
-
-   png_write_chunk_end(png_ptr);
-   png_free(png_ptr, new_name);
-}
-#endif
-
-#if defined(PNG_WRITE_sBIT_SUPPORTED)
-/* write the sBIT chunk */
-void /* PRIVATE */
-png_write_sBIT(png_structp png_ptr, png_color_8p sbit, int color_type)
-{
-#ifdef PNG_USE_LOCAL_ARRAYS
-   PNG_sBIT;
-#endif
-   png_byte buf[4];
-   png_size_t size;
-
-   png_debug(1, "in png_write_sBIT\n");
-   /* make sure we don't depend upon the order of PNG_COLOR_8 */
-   if (color_type & PNG_COLOR_MASK_COLOR)
-   {
-      png_byte maxbits;
-
-      maxbits = (png_byte)(color_type==PNG_COLOR_TYPE_PALETTE ? 8 :
-                png_ptr->usr_bit_depth);
-      if (sbit->red == 0 || sbit->red > maxbits ||
-          sbit->green == 0 || sbit->green > maxbits ||
-          sbit->blue == 0 || sbit->blue > maxbits)
-      {
-         png_warning(png_ptr, "Invalid sBIT depth specified");
-         return;
-      }
-      buf[0] = sbit->red;
-      buf[1] = sbit->green;
-      buf[2] = sbit->blue;
-      size = 3;
-   }
-   else
-   {
-      if (sbit->gray == 0 || sbit->gray > png_ptr->usr_bit_depth)
-      {
-         png_warning(png_ptr, "Invalid sBIT depth specified");
-         return;
-      }
-      buf[0] = sbit->gray;
-      size = 1;
-   }
-
-   if (color_type & PNG_COLOR_MASK_ALPHA)
-   {
-      if (sbit->alpha == 0 || sbit->alpha > png_ptr->usr_bit_depth)
-      {
-         png_warning(png_ptr, "Invalid sBIT depth specified");
-         return;
-      }
-      buf[size++] = sbit->alpha;
-   }
-
-   png_write_chunk(png_ptr, png_sBIT, buf, size);
-}
-#endif
-
-#if defined(PNG_WRITE_cHRM_SUPPORTED)
-/* write the cHRM chunk */
-#ifdef PNG_FLOATING_POINT_SUPPORTED
-void /* PRIVATE */
-png_write_cHRM(png_structp png_ptr, double white_x, double white_y,
-   double red_x, double red_y, double green_x, double green_y,
-   double blue_x, double blue_y)
-{
-#ifdef PNG_USE_LOCAL_ARRAYS
-   PNG_cHRM;
-#endif
-   png_byte buf[32];
-   png_uint_32 itemp;
-
-   png_debug(1, "in png_write_cHRM\n");
-   /* each value is saved in 1/100,000ths */
-   if (white_x < 0 || white_x > 0.8 || white_y < 0 || white_y > 0.8 ||
-       white_x + white_y > 1.0)
-   {
-      png_warning(png_ptr, "Invalid cHRM white point specified");
-#if !defined(PNG_NO_CONSOLE_IO)
-      fprintf(stderr,"white_x=%f, white_y=%f\n",white_x, white_y);
-#endif
-      return;
-   }
-   itemp = (png_uint_32)(white_x * 100000.0 + 0.5);
-   png_save_uint_32(buf, itemp);
-   itemp = (png_uint_32)(white_y * 100000.0 + 0.5);
-   png_save_uint_32(buf + 4, itemp);
-
-   if (red_x < 0 ||  red_y < 0 || red_x + red_y > 1.0)
-   {
-      png_warning(png_ptr, "Invalid cHRM red point specified");
-      return;
-   }
-   itemp = (png_uint_32)(red_x * 100000.0 + 0.5);
-   png_save_uint_32(buf + 8, itemp);
-   itemp = (png_uint_32)(red_y * 100000.0 + 0.5);
-   png_save_uint_32(buf + 12, itemp);
-
-   if (green_x < 0 || green_y < 0 || green_x + green_y > 1.0)
-   {
-      png_warning(png_ptr, "Invalid cHRM green point specified");
-      return;
-   }
-   itemp = (png_uint_32)(green_x * 100000.0 + 0.5);
-   png_save_uint_32(buf + 16, itemp);
-   itemp = (png_uint_32)(green_y * 100000.0 + 0.5);
-   png_save_uint_32(buf + 20, itemp);
-
-   if (blue_x < 0 || blue_y < 0 || blue_x + blue_y > 1.0)
-   {
-      png_warning(png_ptr, "Invalid cHRM blue point specified");
-      return;
-   }
-   itemp = (png_uint_32)(blue_x * 100000.0 + 0.5);
-   png_save_uint_32(buf + 24, itemp);
-   itemp = (png_uint_32)(blue_y * 100000.0 + 0.5);
-   png_save_uint_32(buf + 28, itemp);
-
-   png_write_chunk(png_ptr, png_cHRM, buf, (png_size_t)32);
-}
-#endif
-#ifdef PNG_FIXED_POINT_SUPPORTED
-void /* PRIVATE */
-png_write_cHRM_fixed(png_structp png_ptr, png_fixed_point white_x,
-   png_fixed_point white_y, png_fixed_point red_x, png_fixed_point red_y,
-   png_fixed_point green_x, png_fixed_point green_y, png_fixed_point blue_x,
-   png_fixed_point blue_y)
-{
-#ifdef PNG_USE_LOCAL_ARRAYS
-   PNG_cHRM;
-#endif
-   png_byte buf[32];
-
-   png_debug(1, "in png_write_cHRM\n");
-   /* each value is saved in 1/100,000ths */
-   if (white_x > 80000L || white_y > 80000L || white_x + white_y > 100000L)
-   {
-      png_warning(png_ptr, "Invalid fixed cHRM white point specified");
-#if !defined(PNG_NO_CONSOLE_IO)
-      fprintf(stderr,"white_x=%ld, white_y=%ld\n",white_x, white_y);
-#endif
-      return;
-   }
-   png_save_uint_32(buf, (png_uint_32)white_x);
-   png_save_uint_32(buf + 4, (png_uint_32)white_y);
-
-   if (red_x + red_y > 100000L)
-   {
-      png_warning(png_ptr, "Invalid cHRM fixed red point specified");
-      return;
-   }
-   png_save_uint_32(buf + 8, (png_uint_32)red_x);
-   png_save_uint_32(buf + 12, (png_uint_32)red_y);
-
-   if (green_x + green_y > 100000L)
-   {
-      png_warning(png_ptr, "Invalid fixed cHRM green point specified");
-      return;
-   }
-   png_save_uint_32(buf + 16, (png_uint_32)green_x);
-   png_save_uint_32(buf + 20, (png_uint_32)green_y);
-
-   if (blue_x + blue_y > 100000L)
-   {
-      png_warning(png_ptr, "Invalid fixed cHRM blue point specified");
-      return;
-   }
-   png_save_uint_32(buf + 24, (png_uint_32)blue_x);
-   png_save_uint_32(buf + 28, (png_uint_32)blue_y);
-
-   png_write_chunk(png_ptr, png_cHRM, buf, (png_size_t)32);
-}
-#endif
-#endif
-
-#if defined(PNG_WRITE_tRNS_SUPPORTED)
-/* write the tRNS chunk */
-void /* PRIVATE */
-png_write_tRNS(png_structp png_ptr, png_bytep trans, png_color_16p tran,
-   int num_trans, int color_type)
-{
-#ifdef PNG_USE_LOCAL_ARRAYS
-   PNG_tRNS;
-#endif
-   png_byte buf[6];
-
-   png_debug(1, "in png_write_tRNS\n");
-   if (color_type == PNG_COLOR_TYPE_PALETTE)
-   {
-      if (num_trans <= 0 || num_trans > (int)png_ptr->num_palette)
-      {
-         png_warning(png_ptr,"Invalid number of transparent colors specified");
-         return;
-      }
-      /* write the chunk out as it is */
-      png_write_chunk(png_ptr, png_tRNS, trans, (png_size_t)num_trans);
-   }
-   else if (color_type == PNG_COLOR_TYPE_GRAY)
-   {
-      /* one 16 bit value */
-      if(tran->gray >= (1 << png_ptr->bit_depth))
-      {
-         png_warning(png_ptr,
-           "Ignoring attempt to write tRNS chunk out-of-range for bit_depth");
-         return;
-      }
-      png_save_uint_16(buf, tran->gray);
-      png_write_chunk(png_ptr, png_tRNS, buf, (png_size_t)2);
-   }
-   else if (color_type == PNG_COLOR_TYPE_RGB)
-   {
-      /* three 16 bit values */
-      png_save_uint_16(buf, tran->red);
-      png_save_uint_16(buf + 2, tran->green);
-      png_save_uint_16(buf + 4, tran->blue);
-      if(png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4]))
-         {
-            png_warning(png_ptr,
-              "Ignoring attempt to write 16-bit tRNS chunk when bit_depth is 8");
-            return;
-         }
-      png_write_chunk(png_ptr, png_tRNS, buf, (png_size_t)6);
-   }
-   else
-   {
-      png_warning(png_ptr, "Can't write tRNS with an alpha channel");
-   }
-}
-#endif
-
-#if defined(PNG_WRITE_bKGD_SUPPORTED)
-/* write the background chunk */
-void /* PRIVATE */
-png_write_bKGD(png_structp png_ptr, png_color_16p back, int color_type)
-{
-#ifdef PNG_USE_LOCAL_ARRAYS
-   PNG_bKGD;
-#endif
-   png_byte buf[6];
-
-   png_debug(1, "in png_write_bKGD\n");
-   if (color_type == PNG_COLOR_TYPE_PALETTE)
-   {
-      if (
-#if defined(PNG_MNG_FEATURES_SUPPORTED)
-          (png_ptr->num_palette ||
-          (!(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE))) &&
-#endif
-         back->index > png_ptr->num_palette)
-      {
-         png_warning(png_ptr, "Invalid background palette index");
-         return;
-      }
-      buf[0] = back->index;
-      png_write_chunk(png_ptr, png_bKGD, buf, (png_size_t)1);
-   }
-   else if (color_type & PNG_COLOR_MASK_COLOR)
-   {
-      png_save_uint_16(buf, back->red);
-      png_save_uint_16(buf + 2, back->green);
-      png_save_uint_16(buf + 4, back->blue);
-      if(png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4]))
-         {
-            png_warning(png_ptr,
-              "Ignoring attempt to write 16-bit bKGD chunk when bit_depth is 8");
-            return;
-         }
-      png_write_chunk(png_ptr, png_bKGD, buf, (png_size_t)6);
-   }
-   else
-   {
-      if(back->gray >= (1 << png_ptr->bit_depth))
-      {
-         png_warning(png_ptr,
-           "Ignoring attempt to write bKGD chunk out-of-range for bit_depth");
-         return;
-      }
-      png_save_uint_16(buf, back->gray);
-      png_write_chunk(png_ptr, png_bKGD, buf, (png_size_t)2);
-   }
-}
-#endif
-
-#if defined(PNG_WRITE_hIST_SUPPORTED)
-/* write the histogram */
-void /* PRIVATE */
-png_write_hIST(png_structp png_ptr, png_uint_16p hist, int num_hist)
-{
-#ifdef PNG_USE_LOCAL_ARRAYS
-   PNG_hIST;
-#endif
-   int i;
-   png_byte buf[3];
-
-   png_debug(1, "in png_write_hIST\n");
-   if (num_hist > (int)png_ptr->num_palette)
-   {
-      png_debug2(3, "num_hist = %d, num_palette = %d\n", num_hist,
-         png_ptr->num_palette);
-      png_warning(png_ptr, "Invalid number of histogram entries specified");
-      return;
-   }
-
-   png_write_chunk_start(png_ptr, png_hIST, (png_uint_32)(num_hist * 2));
-   for (i = 0; i < num_hist; i++)
-   {
-      png_save_uint_16(buf, hist[i]);
-      png_write_chunk_data(png_ptr, buf, (png_size_t)2);
-   }
-   png_write_chunk_end(png_ptr);
-}
-#endif
-
-#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_pCAL_SUPPORTED) || \
-    defined(PNG_WRITE_iCCP_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
-/* Check that the tEXt or zTXt keyword is valid per PNG 1.0 specification,
- * and if invalid, correct the keyword rather than discarding the entire
- * chunk.  The PNG 1.0 specification requires keywords 1-79 characters in
- * length, forbids leading or trailing whitespace, multiple internal spaces,
- * and the non-break space (0x80) from ISO 8859-1.  Returns keyword length.
- *
- * The new_key is allocated to hold the corrected keyword and must be freed
- * by the calling routine.  This avoids problems with trying to write to
- * static keywords without having to have duplicate copies of the strings.
- */
-png_size_t /* PRIVATE */
-png_check_keyword(png_structp png_ptr, png_charp key, png_charpp new_key)
-{
-   png_size_t key_len;
-   png_charp kp, dp;
-   int kflag;
-   int kwarn=0;
-
-   png_debug(1, "in png_check_keyword\n");
-   *new_key = NULL;
-
-   if (key == NULL || (key_len = png_strlen(key)) == 0)
-   {
-      png_warning(png_ptr, "zero length keyword");
-      return ((png_size_t)0);
-   }
-
-   png_debug1(2, "Keyword to be checked is '%s'\n", key);
-
-   *new_key = (png_charp)png_malloc_warn(png_ptr, (png_uint_32)(key_len + 2));
-   if (*new_key == NULL)
-   {
-      png_warning(png_ptr, "Out of memory while procesing keyword");
-      return ((png_size_t)0);
-   }
-
-   /* Replace non-printing characters with a blank and print a warning */
-   for (kp = key, dp = *new_key; *kp != '\0'; kp++, dp++)
-   {
-      if ((png_byte)*kp < 0x20 ||
-         ((png_byte)*kp > 0x7E && (png_byte)*kp < 0xA1))
-      {
-#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
-         char msg[40];
-
-         png_snprintf(msg, 40,
-           "invalid keyword character 0x%02X", (png_byte)*kp);
-         png_warning(png_ptr, msg);
-#else
-         png_warning(png_ptr, "invalid character in keyword");
-#endif
-         *dp = ' ';
-      }
-      else
-      {
-         *dp = *kp;
-      }
-   }
-   *dp = '\0';
-
-   /* Remove any trailing white space. */
-   kp = *new_key + key_len - 1;
-   if (*kp == ' ')
-   {
-      png_warning(png_ptr, "trailing spaces removed from keyword");
-
-      while (*kp == ' ')
-      {
-        *(kp--) = '\0';
-        key_len--;
-      }
-   }
-
-   /* Remove any leading white space. */
-   kp = *new_key;
-   if (*kp == ' ')
-   {
-      png_warning(png_ptr, "leading spaces removed from keyword");
-
-      while (*kp == ' ')
-      {
-        kp++;
-        key_len--;
-      }
-   }
-
-   png_debug1(2, "Checking for multiple internal spaces in '%s'\n", kp);
-
-   /* Remove multiple internal spaces. */
-   for (kflag = 0, dp = *new_key; *kp != '\0'; kp++)
-   {
-      if (*kp == ' ' && kflag == 0)
-      {
-         *(dp++) = *kp;
-         kflag = 1;
-      }
-      else if (*kp == ' ')
-      {
-         key_len--;
-         kwarn=1;
-      }
-      else
-      {
-         *(dp++) = *kp;
-         kflag = 0;
-      }
-   }
-   *dp = '\0';
-   if(kwarn)
-      png_warning(png_ptr, "extra interior spaces removed from keyword");
-
-   if (key_len == 0)
-   {
-      png_free(png_ptr, *new_key);
-      *new_key=NULL;
-      png_warning(png_ptr, "Zero length keyword");
-   }
-
-   if (key_len > 79)
-   {
-      png_warning(png_ptr, "keyword length must be 1 - 79 characters");
-      new_key[79] = '\0';
-      key_len = 79;
-   }
-
-   return (key_len);
-}
-#endif
-
-#if defined(PNG_WRITE_tEXt_SUPPORTED)
-/* write a tEXt chunk */
-void /* PRIVATE */
-png_write_tEXt(png_structp png_ptr, png_charp key, png_charp text,
-   png_size_t text_len)
-{
-#ifdef PNG_USE_LOCAL_ARRAYS
-   PNG_tEXt;
-#endif
-   png_size_t key_len;
-   png_charp new_key;
-
-   png_debug(1, "in png_write_tEXt\n");
-   if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
-   {
-      png_warning(png_ptr, "Empty keyword in tEXt chunk");
-      return;
-   }
-
-   if (text == NULL || *text == '\0')
-      text_len = 0;
-   else
-      text_len = png_strlen(text);
-
-   /* make sure we include the 0 after the key */
-   png_write_chunk_start(png_ptr, png_tEXt, (png_uint_32)key_len+text_len+1);
-   /*
-    * We leave it to the application to meet PNG-1.0 requirements on the
-    * contents of the text.  PNG-1.0 through PNG-1.2 discourage the use of
-    * any non-Latin-1 characters except for NEWLINE.  ISO PNG will forbid them.
-    * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
-    */
-   png_write_chunk_data(png_ptr, (png_bytep)new_key, key_len + 1);
-   if (text_len)
-      png_write_chunk_data(png_ptr, (png_bytep)text, text_len);
-
-   png_write_chunk_end(png_ptr);
-   png_free(png_ptr, new_key);
-}
-#endif
-
-#if defined(PNG_WRITE_zTXt_SUPPORTED)
-/* write a compressed text chunk */
-void /* PRIVATE */
-png_write_zTXt(png_structp png_ptr, png_charp key, png_charp text,
-   png_size_t text_len, int compression)
-{
-#ifdef PNG_USE_LOCAL_ARRAYS
-   PNG_zTXt;
-#endif
-   png_size_t key_len;
-   char buf[1];
-   png_charp new_key;
-   compression_state comp;
-
-   png_debug(1, "in png_write_zTXt\n");
-
-   comp.num_output_ptr = 0;
-   comp.max_output_ptr = 0;
-   comp.output_ptr = NULL;
-   comp.input = NULL;
-   comp.input_len = 0;
-
-   if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
-   {
-      png_warning(png_ptr, "Empty keyword in zTXt chunk");
-      return;
-   }
-
-   if (text == NULL || *text == '\0' || compression==PNG_TEXT_COMPRESSION_NONE)
-   {
-      png_write_tEXt(png_ptr, new_key, text, (png_size_t)0);
-      png_free(png_ptr, new_key);
-      return;
-   }
-
-   text_len = png_strlen(text);
-
-   /* compute the compressed data; do it now for the length */
-   text_len = png_text_compress(png_ptr, text, text_len, compression,
-       &comp);
-
-   /* write start of chunk */
-   png_write_chunk_start(png_ptr, png_zTXt, (png_uint_32)
-      (key_len+text_len+2));
-   /* write key */
-   png_write_chunk_data(png_ptr, (png_bytep)new_key, key_len + 1);
-   png_free(png_ptr, new_key);
-
-   buf[0] = (png_byte)compression;
-   /* write compression */
-   png_write_chunk_data(png_ptr, (png_bytep)buf, (png_size_t)1);
-   /* write the compressed data */
-   png_write_compressed_data_out(png_ptr, &comp);
-
-   /* close the chunk */
-   png_write_chunk_end(png_ptr);
-}
-#endif
-
-#if defined(PNG_WRITE_iTXt_SUPPORTED)
-/* write an iTXt chunk */
-void /* PRIVATE */
-png_write_iTXt(png_structp png_ptr, int compression, png_charp key,
-    png_charp lang, png_charp lang_key, png_charp text)
-{
-#ifdef PNG_USE_LOCAL_ARRAYS
-   PNG_iTXt;
-#endif
-   png_size_t lang_len, key_len, lang_key_len, text_len;
-   png_charp new_lang, new_key;
-   png_byte cbuf[2];
-   compression_state comp;
-
-   png_debug(1, "in png_write_iTXt\n");
-
-   comp.num_output_ptr = 0;
-   comp.max_output_ptr = 0;
-   comp.output_ptr = NULL;
-   comp.input = NULL;
-
-   if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
-   {
-      png_warning(png_ptr, "Empty keyword in iTXt chunk");
-      return;
-   }
-   if (lang == NULL || (lang_len = png_check_keyword(png_ptr, lang, &new_lang))==0)
-   {
-      png_warning(png_ptr, "Empty language field in iTXt chunk");
-      new_lang = NULL;
-      lang_len = 0;
-   }
-
-   if (lang_key == NULL)
-     lang_key_len = 0;
-   else
-     lang_key_len = png_strlen(lang_key);
-
-   if (text == NULL)
-      text_len = 0;
-   else
-     text_len = png_strlen(text);
-
-   /* compute the compressed data; do it now for the length */
-   text_len = png_text_compress(png_ptr, text, text_len, compression-2,
-      &comp);
-
-
-   /* make sure we include the compression flag, the compression byte,
-    * and the NULs after the key, lang, and lang_key parts */
-
-   png_write_chunk_start(png_ptr, png_iTXt,
-          (png_uint_32)(
-        5 /* comp byte, comp flag, terminators for key, lang and lang_key */
-        + key_len
-        + lang_len
-        + lang_key_len
-        + text_len));
-
-   /*
-    * We leave it to the application to meet PNG-1.0 requirements on the
-    * contents of the text.  PNG-1.0 through PNG-1.2 discourage the use of
-    * any non-Latin-1 characters except for NEWLINE.  ISO PNG will forbid them.
-    * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
-    */
-   png_write_chunk_data(png_ptr, (png_bytep)new_key, key_len + 1);
-
-   /* set the compression flag */
-   if (compression == PNG_ITXT_COMPRESSION_NONE || \
-       compression == PNG_TEXT_COMPRESSION_NONE)
-       cbuf[0] = 0;
-   else /* compression == PNG_ITXT_COMPRESSION_zTXt */
-       cbuf[0] = 1;
-   /* set the compression method */
-   cbuf[1] = 0;
-   png_write_chunk_data(png_ptr, cbuf, 2);
-
-   cbuf[0] = 0;
-   png_write_chunk_data(png_ptr, (new_lang ? (png_bytep)new_lang : cbuf), lang_len + 1);
-   png_write_chunk_data(png_ptr, (lang_key ? (png_bytep)lang_key : cbuf), lang_key_len + 1);
-   png_write_compressed_data_out(png_ptr, &comp);
-
-   png_write_chunk_end(png_ptr);
-   png_free(png_ptr, new_key);
-   png_free(png_ptr, new_lang);
-}
-#endif
-
-#if defined(PNG_WRITE_oFFs_SUPPORTED)
-/* write the oFFs chunk */
-void /* PRIVATE */
-png_write_oFFs(png_structp png_ptr, png_int_32 x_offset, png_int_32 y_offset,
-   int unit_type)
-{
-#ifdef PNG_USE_LOCAL_ARRAYS
-   PNG_oFFs;
-#endif
-   png_byte buf[9];
-
-   png_debug(1, "in png_write_oFFs\n");
-   if (unit_type >= PNG_OFFSET_LAST)
-      png_warning(png_ptr, "Unrecognized unit type for oFFs chunk");
-
-   png_save_int_32(buf, x_offset);
-   png_save_int_32(buf + 4, y_offset);
-   buf[8] = (png_byte)unit_type;
-
-   png_write_chunk(png_ptr, png_oFFs, buf, (png_size_t)9);
-}
-#endif
-#if defined(PNG_WRITE_pCAL_SUPPORTED)
-/* write the pCAL chunk (described in the PNG extensions document) */
-void /* PRIVATE */
-png_write_pCAL(png_structp png_ptr, png_charp purpose, png_int_32 X0,
-   png_int_32 X1, int type, int nparams, png_charp units, png_charpp params)
-{
-#ifdef PNG_USE_LOCAL_ARRAYS
-   PNG_pCAL;
-#endif
-   png_size_t purpose_len, units_len, total_len;
-   png_uint_32p params_len;
-   png_byte buf[10];
-   png_charp new_purpose;
-   int i;
-
-   png_debug1(1, "in png_write_pCAL (%d parameters)\n", nparams);
-   if (type >= PNG_EQUATION_LAST)
-      png_warning(png_ptr, "Unrecognized equation type for pCAL chunk");
-
-   purpose_len = png_check_keyword(png_ptr, purpose, &new_purpose) + 1;
-   png_debug1(3, "pCAL purpose length = %d\n", (int)purpose_len);
-   units_len = png_strlen(units) + (nparams == 0 ? 0 : 1);
-   png_debug1(3, "pCAL units length = %d\n", (int)units_len);
-   total_len = purpose_len + units_len + 10;
-
-   params_len = (png_uint_32p)png_malloc(png_ptr, (png_uint_32)(nparams
-      *png_sizeof(png_uint_32)));
-
-   /* Find the length of each parameter, making sure we don't count the
-      null terminator for the last parameter. */
-   for (i = 0; i < nparams; i++)
-   {
-      params_len[i] = png_strlen(params[i]) + (i == nparams - 1 ? 0 : 1);
-      png_debug2(3, "pCAL parameter %d length = %lu\n", i, params_len[i]);
-      total_len += (png_size_t)params_len[i];
-   }
-
-   png_debug1(3, "pCAL total length = %d\n", (int)total_len);
-   png_write_chunk_start(png_ptr, png_pCAL, (png_uint_32)total_len);
-   png_write_chunk_data(png_ptr, (png_bytep)new_purpose, purpose_len);
-   png_save_int_32(buf, X0);
-   png_save_int_32(buf + 4, X1);
-   buf[8] = (png_byte)type;
-   buf[9] = (png_byte)nparams;
-   png_write_chunk_data(png_ptr, buf, (png_size_t)10);
-   png_write_chunk_data(png_ptr, (png_bytep)units, (png_size_t)units_len);
-
-   png_free(png_ptr, new_purpose);
-
-   for (i = 0; i < nparams; i++)
-   {
-      png_write_chunk_data(png_ptr, (png_bytep)params[i],
-         (png_size_t)params_len[i]);
-   }
-
-   png_free(png_ptr, params_len);
-   png_write_chunk_end(png_ptr);
-}
-#endif
-
-#if defined(PNG_WRITE_sCAL_SUPPORTED)
-/* write the sCAL chunk */
-#if defined(PNG_FLOATING_POINT_SUPPORTED) && !defined(PNG_NO_STDIO)
-void /* PRIVATE */
-png_write_sCAL(png_structp png_ptr, int unit, double width, double height)
-{
-#ifdef PNG_USE_LOCAL_ARRAYS
-   PNG_sCAL;
-#endif
-   char buf[64];
-   png_size_t total_len;
-
-   png_debug(1, "in png_write_sCAL\n");
-
-   buf[0] = (char)unit;
-#if defined(_WIN32_WCE)
-/* sprintf() function is not supported on WindowsCE */
-   {
-      wchar_t wc_buf[32];
-      size_t wc_len;
-      swprintf(wc_buf, TEXT("%12.12e"), width);
-      wc_len = wcslen(wc_buf);
-      WideCharToMultiByte(CP_ACP, 0, wc_buf, -1, buf + 1, wc_len, NULL, NULL);
-      total_len = wc_len + 2;
-      swprintf(wc_buf, TEXT("%12.12e"), height);
-      wc_len = wcslen(wc_buf);
-      WideCharToMultiByte(CP_ACP, 0, wc_buf, -1, buf + total_len, wc_len,
-         NULL, NULL);
-      total_len += wc_len;
-   }
-#else
-   png_snprintf(buf + 1, 63, "%12.12e", width);
-   total_len = 1 + png_strlen(buf + 1) + 1;
-   png_snprintf(buf + total_len, 64-total_len, "%12.12e", height);
-   total_len += png_strlen(buf + total_len);
-#endif
-
-   png_debug1(3, "sCAL total length = %u\n", (unsigned int)total_len);
-   png_write_chunk(png_ptr, png_sCAL, (png_bytep)buf, total_len);
-}
-#else
-#ifdef PNG_FIXED_POINT_SUPPORTED
-void /* PRIVATE */
-png_write_sCAL_s(png_structp png_ptr, int unit, png_charp width,
-   png_charp height)
-{
-#ifdef PNG_USE_LOCAL_ARRAYS
-   PNG_sCAL;
-#endif
-   png_byte buf[64];
-   png_size_t wlen, hlen, total_len;
-
-   png_debug(1, "in png_write_sCAL_s\n");
-
-   wlen = png_strlen(width);
-   hlen = png_strlen(height);
-   total_len = wlen + hlen + 2;
-   if (total_len > 64)
-   {
-      png_warning(png_ptr, "Can't write sCAL (buffer too small)");
-      return;
-   }
-
-   buf[0] = (png_byte)unit;
-   png_memcpy(buf + 1, width, wlen + 1);      /* append the '\0' here */
-   png_memcpy(buf + wlen + 2, height, hlen);  /* do NOT append the '\0' here */
-
-   png_debug1(3, "sCAL total length = %u\n", (unsigned int)total_len);
-   png_write_chunk(png_ptr, png_sCAL, buf, total_len);
-}
-#endif
-#endif
-#endif
-
-#if defined(PNG_WRITE_pHYs_SUPPORTED)
-/* write the pHYs chunk */
-void /* PRIVATE */
-png_write_pHYs(png_structp png_ptr, png_uint_32 x_pixels_per_unit,
-   png_uint_32 y_pixels_per_unit,
-   int unit_type)
-{
-#ifdef PNG_USE_LOCAL_ARRAYS
-   PNG_pHYs;
-#endif
-   png_byte buf[9];
-
-   png_debug(1, "in png_write_pHYs\n");
-   if (unit_type >= PNG_RESOLUTION_LAST)
-      png_warning(png_ptr, "Unrecognized unit type for pHYs chunk");
-
-   png_save_uint_32(buf, x_pixels_per_unit);
-   png_save_uint_32(buf + 4, y_pixels_per_unit);
-   buf[8] = (png_byte)unit_type;
-
-   png_write_chunk(png_ptr, png_pHYs, buf, (png_size_t)9);
-}
-#endif
-
-#if defined(PNG_WRITE_tIME_SUPPORTED)
-/* Write the tIME chunk.  Use either png_convert_from_struct_tm()
- * or png_convert_from_time_t(), or fill in the structure yourself.
- */
-void /* PRIVATE */
-png_write_tIME(png_structp png_ptr, png_timep mod_time)
-{
-#ifdef PNG_USE_LOCAL_ARRAYS
-   PNG_tIME;
-#endif
-   png_byte buf[7];
-
-   png_debug(1, "in png_write_tIME\n");
-   if (mod_time->month  > 12 || mod_time->month  < 1 ||
-       mod_time->day    > 31 || mod_time->day    < 1 ||
-       mod_time->hour   > 23 || mod_time->second > 60)
-   {
-      png_warning(png_ptr, "Invalid time specified for tIME chunk");
-      return;
-   }
-
-   png_save_uint_16(buf, mod_time->year);
-   buf[2] = mod_time->month;
-   buf[3] = mod_time->day;
-   buf[4] = mod_time->hour;
-   buf[5] = mod_time->minute;
-   buf[6] = mod_time->second;
-
-   png_write_chunk(png_ptr, png_tIME, buf, (png_size_t)7);
-}
-#endif
-
-/* initializes the row writing capability of libpng */
-void /* PRIVATE */
-png_write_start_row(png_structp png_ptr)
-{
-#ifdef PNG_WRITE_INTERLACING_SUPPORTED
-#ifdef PNG_USE_LOCAL_ARRAYS
-   /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
-
-   /* start of interlace block */
-   int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
-
-   /* offset to next interlace block */
-   int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
-
-   /* start of interlace block in the y direction */
-   int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
-
-   /* offset to next interlace block in the y direction */
-   int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
-#endif
-#endif
-
-   png_size_t buf_size;
-
-   png_debug(1, "in png_write_start_row\n");
-   buf_size = (png_size_t)(PNG_ROWBYTES(
-      png_ptr->usr_channels*png_ptr->usr_bit_depth,png_ptr->width)+1);
-
-   /* set up row buffer */
-   png_ptr->row_buf = (png_bytep)png_malloc(png_ptr, (png_uint_32)buf_size);
-   png_ptr->row_buf[0] = PNG_FILTER_VALUE_NONE;
-
-#ifndef PNG_NO_WRITE_FILTERING
-   /* set up filtering buffer, if using this filter */
-   if (png_ptr->do_filter & PNG_FILTER_SUB)
-   {
-      png_ptr->sub_row = (png_bytep)png_malloc(png_ptr,
-         (png_ptr->rowbytes + 1));
-      png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
-   }
-
-   /* We only need to keep the previous row if we are using one of these. */
-   if (png_ptr->do_filter & (PNG_FILTER_AVG | PNG_FILTER_UP | PNG_FILTER_PAETH))
-   {
-     /* set up previous row buffer */
-      png_ptr->prev_row = (png_bytep)png_malloc(png_ptr, (png_uint_32)buf_size);
-      png_memset(png_ptr->prev_row, 0, buf_size);
-
-      if (png_ptr->do_filter & PNG_FILTER_UP)
-      {
-         png_ptr->up_row = (png_bytep)png_malloc(png_ptr,
-            (png_ptr->rowbytes + 1));
-         png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
-      }
-
-      if (png_ptr->do_filter & PNG_FILTER_AVG)
-      {
-         png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
-            (png_ptr->rowbytes + 1));
-         png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
-      }
-
-      if (png_ptr->do_filter & PNG_FILTER_PAETH)
-      {
-         png_ptr->paeth_row = (png_bytep)png_malloc(png_ptr,
-            (png_ptr->rowbytes + 1));
-         png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
-      }
-#endif /* PNG_NO_WRITE_FILTERING */
-   }
-
-#ifdef PNG_WRITE_INTERLACING_SUPPORTED
-   /* if interlaced, we need to set up width and height of pass */
-   if (png_ptr->interlaced)
-   {
-      if (!(png_ptr->transformations & PNG_INTERLACE))
-      {
-         png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
-            png_pass_ystart[0]) / png_pass_yinc[0];
-         png_ptr->usr_width = (png_ptr->width + png_pass_inc[0] - 1 -
-            png_pass_start[0]) / png_pass_inc[0];
-      }
-      else
-      {
-         png_ptr->num_rows = png_ptr->height;
-         png_ptr->usr_width = png_ptr->width;
-      }
-   }
-   else
-#endif
-   {
-      png_ptr->num_rows = png_ptr->height;
-      png_ptr->usr_width = png_ptr->width;
-   }
-   png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
-   png_ptr->zstream.next_out = png_ptr->zbuf;
-}
-
-/* Internal use only.  Called when finished processing a row of data. */
-void /* PRIVATE */
-png_write_finish_row(png_structp png_ptr)
-{
-#ifdef PNG_WRITE_INTERLACING_SUPPORTED
-#ifdef PNG_USE_LOCAL_ARRAYS
-   /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
-
-   /* start of interlace block */
-   int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
-
-   /* offset to next interlace block */
-   int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
-
-   /* start of interlace block in the y direction */
-   int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
-
-   /* offset to next interlace block in the y direction */
-   int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
-#endif
-#endif
-
-   int ret;
-
-   png_debug(1, "in png_write_finish_row\n");
-   /* next row */
-   png_ptr->row_number++;
-
-   /* see if we are done */
-   if (png_ptr->row_number < png_ptr->num_rows)
-      return;
-
-#ifdef PNG_WRITE_INTERLACING_SUPPORTED
-   /* if interlaced, go to next pass */
-   if (png_ptr->interlaced)
-   {
-      png_ptr->row_number = 0;
-      if (png_ptr->transformations & PNG_INTERLACE)
-      {
-         png_ptr->pass++;
-      }
-      else
-      {
-         /* loop until we find a non-zero width or height pass */
-         do
-         {
-            png_ptr->pass++;
-            if (png_ptr->pass >= 7)
-               break;
-            png_ptr->usr_width = (png_ptr->width +
-               png_pass_inc[png_ptr->pass] - 1 -
-               png_pass_start[png_ptr->pass]) /
-               png_pass_inc[png_ptr->pass];
-            png_ptr->num_rows = (png_ptr->height +
-               png_pass_yinc[png_ptr->pass] - 1 -
-               png_pass_ystart[png_ptr->pass]) /
-               png_pass_yinc[png_ptr->pass];
-            if (png_ptr->transformations & PNG_INTERLACE)
-               break;
-         } while (png_ptr->usr_width == 0 || png_ptr->num_rows == 0);
-
-      }
-
-      /* reset the row above the image for the next pass */
-      if (png_ptr->pass < 7)
-      {
-         if (png_ptr->prev_row != NULL)
-            png_memset(png_ptr->prev_row, 0,
-               (png_size_t)(PNG_ROWBYTES(png_ptr->usr_channels*
-               png_ptr->usr_bit_depth,png_ptr->width))+1);
-         return;
-      }
-   }
-#endif
-
-   /* if we get here, we've just written the last row, so we need
-      to flush the compressor */
-   do
-   {
-      /* tell the compressor we are done */
-      ret = deflate(&png_ptr->zstream, Z_FINISH);
-      /* check for an error */
-      if (ret == Z_OK)
-      {
-         /* check to see if we need more room */
-         if (!(png_ptr->zstream.avail_out))
-         {
-            png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
-            png_ptr->zstream.next_out = png_ptr->zbuf;
-            png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
-         }
-      }
-      else if (ret != Z_STREAM_END)
-      {
-         if (png_ptr->zstream.msg != NULL)
-            png_error(png_ptr, png_ptr->zstream.msg);
-         else
-            png_error(png_ptr, "zlib error");
-      }
-   } while (ret != Z_STREAM_END);
-
-   /* write any extra space */
-   if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
-   {
-      png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size -
-         png_ptr->zstream.avail_out);
-   }
-
-   deflateReset(&png_ptr->zstream);
-   png_ptr->zstream.data_type = Z_BINARY;
-}
-
-#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
-/* Pick out the correct pixels for the interlace pass.
- * The basic idea here is to go through the row with a source
- * pointer and a destination pointer (sp and dp), and copy the
- * correct pixels for the pass.  As the row gets compacted,
- * sp will always be >= dp, so we should never overwrite anything.
- * See the default: case for the easiest code to understand.
- */
-void /* PRIVATE */
-png_do_write_interlace(png_row_infop row_info, png_bytep row, int pass)
-{
-#ifdef PNG_USE_LOCAL_ARRAYS
-   /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
-
-   /* start of interlace block */
-   int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
-
-   /* offset to next interlace block */
-   int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
-#endif
-
-   png_debug(1, "in png_do_write_interlace\n");
-   /* we don't have to do anything on the last pass (6) */
-#if defined(PNG_USELESS_TESTS_SUPPORTED)
-   if (row != NULL && row_info != NULL && pass < 6)
-#else
-   if (pass < 6)
-#endif
-   {
-      /* each pixel depth is handled separately */
-      switch (row_info->pixel_depth)
-      {
-         case 1:
-         {
-            png_bytep sp;
-            png_bytep dp;
-            int shift;
-            int d;
-            int value;
-            png_uint_32 i;
-            png_uint_32 row_width = row_info->width;
-
-            dp = row;
-            d = 0;
-            shift = 7;
-            for (i = png_pass_start[pass]; i < row_width;
-               i += png_pass_inc[pass])
-            {
-               sp = row + (png_size_t)(i >> 3);
-               value = (int)(*sp >> (7 - (int)(i & 0x07))) & 0x01;
-               d |= (value << shift);
-
-               if (shift == 0)
-               {
-                  shift = 7;
-                  *dp++ = (png_byte)d;
-                  d = 0;
-               }
-               else
-                  shift--;
-
-            }
-            if (shift != 7)
-               *dp = (png_byte)d;
-            break;
-         }
-         case 2:
-         {
-            png_bytep sp;
-            png_bytep dp;
-            int shift;
-            int d;
-            int value;
-            png_uint_32 i;
-            png_uint_32 row_width = row_info->width;
-
-            dp = row;
-            shift = 6;
-            d = 0;
-            for (i = png_pass_start[pass]; i < row_width;
-               i += png_pass_inc[pass])
-            {
-               sp = row + (png_size_t)(i >> 2);
-               value = (*sp >> ((3 - (int)(i & 0x03)) << 1)) & 0x03;
-               d |= (value << shift);
-
-               if (shift == 0)
-               {
-                  shift = 6;
-                  *dp++ = (png_byte)d;
-                  d = 0;
-               }
-               else
-                  shift -= 2;
-            }
-            if (shift != 6)
-                   *dp = (png_byte)d;
-            break;
-         }
-         case 4:
-         {
-            png_bytep sp;
-            png_bytep dp;
-            int shift;
-            int d;
-            int value;
-            png_uint_32 i;
-            png_uint_32 row_width = row_info->width;
-
-            dp = row;
-            shift = 4;
-            d = 0;
-            for (i = png_pass_start[pass]; i < row_width;
-               i += png_pass_inc[pass])
-            {
-               sp = row + (png_size_t)(i >> 1);
-               value = (*sp >> ((1 - (int)(i & 0x01)) << 2)) & 0x0f;
-               d |= (value << shift);
-
-               if (shift == 0)
-               {
-                  shift = 4;
-                  *dp++ = (png_byte)d;
-                  d = 0;
-               }
-               else
-                  shift -= 4;
-            }
-            if (shift != 4)
-               *dp = (png_byte)d;
-            break;
-         }
-         default:
-         {
-            png_bytep sp;
-            png_bytep dp;
-            png_uint_32 i;
-            png_uint_32 row_width = row_info->width;
-            png_size_t pixel_bytes;
-
-            /* start at the beginning */
-            dp = row;
-            /* find out how many bytes each pixel takes up */
-            pixel_bytes = (row_info->pixel_depth >> 3);
-            /* loop through the row, only looking at the pixels that
-               matter */
-            for (i = png_pass_start[pass]; i < row_width;
-               i += png_pass_inc[pass])
-            {
-               /* find out where the original pixel is */
-               sp = row + (png_size_t)i * pixel_bytes;
-               /* move the pixel */
-               if (dp != sp)
-                  png_memcpy(dp, sp, pixel_bytes);
-               /* next pixel */
-               dp += pixel_bytes;
-            }
-            break;
-         }
-      }
-      /* set new row width */
-      row_info->width = (row_info->width +
-         png_pass_inc[pass] - 1 -
-         png_pass_start[pass]) /
-         png_pass_inc[pass];
-         row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,
-            row_info->width);
-   }
-}
-#endif
-
-/* This filters the row, chooses which filter to use, if it has not already
- * been specified by the application, and then writes the row out with the
- * chosen filter.
- */
-#define PNG_MAXSUM (((png_uint_32)(-1)) >> 1)
-#define PNG_HISHIFT 10
-#define PNG_LOMASK ((png_uint_32)0xffffL)
-#define PNG_HIMASK ((png_uint_32)(~PNG_LOMASK >> PNG_HISHIFT))
-void /* PRIVATE */
-png_write_find_filter(png_structp png_ptr, png_row_infop row_info)
-{
-   png_bytep best_row;
-#ifndef PNG_NO_WRITE_FILTER
-   png_bytep prev_row, row_buf;
-   png_uint_32 mins, bpp;
-   png_byte filter_to_do = png_ptr->do_filter;
-   png_uint_32 row_bytes = row_info->rowbytes;
-#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
-   int num_p_filters = (int)png_ptr->num_prev_filters;
-#endif
-
-   png_debug(1, "in png_write_find_filter\n");
-   /* find out how many bytes offset each pixel is */
-   bpp = (row_info->pixel_depth + 7) >> 3;
-
-   prev_row = png_ptr->prev_row;
-#endif
-   best_row = png_ptr->row_buf;
-#ifndef PNG_NO_WRITE_FILTER
-   row_buf = best_row;
-   mins = PNG_MAXSUM;
-
-   /* The prediction method we use is to find which method provides the
-    * smallest value when summing the absolute values of the distances
-    * from zero, using anything >= 128 as negative numbers.  This is known
-    * as the "minimum sum of absolute differences" heuristic.  Other
-    * heuristics are the "weighted minimum sum of absolute differences"
-    * (experimental and can in theory improve compression), and the "zlib
-    * predictive" method (not implemented yet), which does test compressions
-    * of lines using different filter methods, and then chooses the
-    * (series of) filter(s) that give minimum compressed data size (VERY
-    * computationally expensive).
-    *
-    * GRR 980525:  consider also
-    *   (1) minimum sum of absolute differences from running average (i.e.,
-    *       keep running sum of non-absolute differences & count of bytes)
-    *       [track dispersion, too?  restart average if dispersion too large?]
-    *  (1b) minimum sum of absolute differences from sliding average, probably
-    *       with window size <= deflate window (usually 32K)
-    *   (2) minimum sum of squared differences from zero or running average
-    *       (i.e., ~ root-mean-square approach)
-    */
-
-
-   /* We don't need to test the 'no filter' case if this is the only filter
-    * that has been chosen, as it doesn't actually do anything to the data.
-    */
-   if ((filter_to_do & PNG_FILTER_NONE) &&
-       filter_to_do != PNG_FILTER_NONE)
-   {
-      png_bytep rp;
-      png_uint_32 sum = 0;
-      png_uint_32 i;
-      int v;
-
-      for (i = 0, rp = row_buf + 1; i < row_bytes; i++, rp++)
-      {
-         v = *rp;
-         sum += (v < 128) ? v : 256 - v;
-      }
-
-#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
-      if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
-      {
-         png_uint_32 sumhi, sumlo;
-         int j;
-         sumlo = sum & PNG_LOMASK;
-         sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; /* Gives us some footroom */
-
-         /* Reduce the sum if we match any of the previous rows */
-         for (j = 0; j < num_p_filters; j++)
-         {
-            if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
-            {
-               sumlo = (sumlo * png_ptr->filter_weights[j]) >>
-                  PNG_WEIGHT_SHIFT;
-               sumhi = (sumhi * png_ptr->filter_weights[j]) >>
-                  PNG_WEIGHT_SHIFT;
-            }
-         }
-
-         /* Factor in the cost of this filter (this is here for completeness,
-          * but it makes no sense to have a "cost" for the NONE filter, as
-          * it has the minimum possible computational cost - none).
-          */
-         sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
-            PNG_COST_SHIFT;
-         sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
-            PNG_COST_SHIFT;
-
-         if (sumhi > PNG_HIMASK)
-            sum = PNG_MAXSUM;
-         else
-            sum = (sumhi << PNG_HISHIFT) + sumlo;
-      }
-#endif
-      mins = sum;
-   }
-
-   /* sub filter */
-   if (filter_to_do == PNG_FILTER_SUB)
-   /* it's the only filter so no testing is needed */
-   {
-      png_bytep rp, lp, dp;
-      png_uint_32 i;
-      for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
-           i++, rp++, dp++)
-      {
-         *dp = *rp;
-      }
-      for (lp = row_buf + 1; i < row_bytes;
-         i++, rp++, lp++, dp++)
-      {
-         *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
-      }
-      best_row = png_ptr->sub_row;
-   }
-
-   else if (filter_to_do & PNG_FILTER_SUB)
-   {
-      png_bytep rp, dp, lp;
-      png_uint_32 sum = 0, lmins = mins;
-      png_uint_32 i;
-      int v;
-
-#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
-      /* We temporarily increase the "minimum sum" by the factor we
-       * would reduce the sum of this filter, so that we can do the
-       * early exit comparison without scaling the sum each time.
-       */
-      if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
-      {
-         int j;
-         png_uint_32 lmhi, lmlo;
-         lmlo = lmins & PNG_LOMASK;
-         lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
-
-         for (j = 0; j < num_p_filters; j++)
-         {
-            if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
-            {
-               lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
-                  PNG_WEIGHT_SHIFT;
-               lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
-                  PNG_WEIGHT_SHIFT;
-            }
-         }
-
-         lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
-            PNG_COST_SHIFT;
-         lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
-            PNG_COST_SHIFT;
-
-         if (lmhi > PNG_HIMASK)
-            lmins = PNG_MAXSUM;
-         else
-            lmins = (lmhi << PNG_HISHIFT) + lmlo;
-      }
-#endif
-
-      for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
-           i++, rp++, dp++)
-      {
-         v = *dp = *rp;
-
-         sum += (v < 128) ? v : 256 - v;
-      }
-      for (lp = row_buf + 1; i < row_bytes;
-         i++, rp++, lp++, dp++)
-      {
-         v = *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
-
-         sum += (v < 128) ? v : 256 - v;
-
-         if (sum > lmins)  /* We are already worse, don't continue. */
-            break;
-      }
-
-#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
-      if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
-      {
-         int j;
-         png_uint_32 sumhi, sumlo;
-         sumlo = sum & PNG_LOMASK;
-         sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
-
-         for (j = 0; j < num_p_filters; j++)
-         {
-            if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
-            {
-               sumlo = (sumlo * png_ptr->inv_filter_weights[j]) >>
-                  PNG_WEIGHT_SHIFT;
-               sumhi = (sumhi * png_ptr->inv_filter_weights[j]) >>
-                  PNG_WEIGHT_SHIFT;
-            }
-         }
-
-         sumlo = (sumlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
-            PNG_COST_SHIFT;
-         sumhi = (sumhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
-            PNG_COST_SHIFT;
-
-         if (sumhi > PNG_HIMASK)
-            sum = PNG_MAXSUM;
-         else
-            sum = (sumhi << PNG_HISHIFT) + sumlo;
-      }
-#endif
-
-      if (sum < mins)
-      {
-         mins = sum;
-         best_row = png_ptr->sub_row;
-      }
-   }
-
-   /* up filter */
-   if (filter_to_do == PNG_FILTER_UP)
-   {
-      png_bytep rp, dp, pp;
-      png_uint_32 i;
-
-      for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
-           pp = prev_row + 1; i < row_bytes;
-           i++, rp++, pp++, dp++)
-      {
-         *dp = (png_byte)(((int)*rp - (int)*pp) & 0xff);
-      }
-      best_row = png_ptr->up_row;
-   }
-
-   else if (filter_to_do & PNG_FILTER_UP)
-   {
-      png_bytep rp, dp, pp;
-      png_uint_32 sum = 0, lmins = mins;
-      png_uint_32 i;
-      int v;
-
-
-#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
-      if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
-      {
-         int j;
-         png_uint_32 lmhi, lmlo;
-         lmlo = lmins & PNG_LOMASK;
-         lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
-
-         for (j = 0; j < num_p_filters; j++)
-         {
-            if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
-            {
-               lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
-                  PNG_WEIGHT_SHIFT;
-               lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
-                  PNG_WEIGHT_SHIFT;
-            }
-         }
-
-         lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
-            PNG_COST_SHIFT;
-         lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
-            PNG_COST_SHIFT;
-
-         if (lmhi > PNG_HIMASK)
-            lmins = PNG_MAXSUM;
-         else
-            lmins = (lmhi << PNG_HISHIFT) + lmlo;
-      }
-#endif
-
-      for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
-           pp = prev_row + 1; i < row_bytes; i++)
-      {
-         v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
-
-         sum += (v < 128) ? v : 256 - v;
-
-         if (sum > lmins)  /* We are already worse, don't continue. */
-            break;
-      }
-
-#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
-      if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
-      {
-         int j;
-         png_uint_32 sumhi, sumlo;
-         sumlo = sum & PNG_LOMASK;
-         sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
-
-         for (j = 0; j < num_p_filters; j++)
-         {
-            if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
-            {
-               sumlo = (sumlo * png_ptr->filter_weights[j]) >>
-                  PNG_WEIGHT_SHIFT;
-               sumhi = (sumhi * png_ptr->filter_weights[j]) >>
-                  PNG_WEIGHT_SHIFT;
-            }
-         }
-
-         sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
-            PNG_COST_SHIFT;
-         sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
-            PNG_COST_SHIFT;
-
-         if (sumhi > PNG_HIMASK)
-            sum = PNG_MAXSUM;
-         else
-            sum = (sumhi << PNG_HISHIFT) + sumlo;
-      }
-#endif
-
-      if (sum < mins)
-      {
-         mins = sum;
-         best_row = png_ptr->up_row;
-      }
-   }
-
-   /* avg filter */
-   if (filter_to_do == PNG_FILTER_AVG)
-   {
-      png_bytep rp, dp, pp, lp;
-      png_uint_32 i;
-      for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
-           pp = prev_row + 1; i < bpp; i++)
-      {
-         *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
-      }
-      for (lp = row_buf + 1; i < row_bytes; i++)
-      {
-         *dp++ = (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2))
-                 & 0xff);
-      }
-      best_row = png_ptr->avg_row;
-   }
-
-   else if (filter_to_do & PNG_FILTER_AVG)
-   {
-      png_bytep rp, dp, pp, lp;
-      png_uint_32 sum = 0, lmins = mins;
-      png_uint_32 i;
-      int v;
-
-#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
-      if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
-      {
-         int j;
-         png_uint_32 lmhi, lmlo;
-         lmlo = lmins & PNG_LOMASK;
-         lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
-
-         for (j = 0; j < num_p_filters; j++)
-         {
-            if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_AVG)
-            {
-               lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
-                  PNG_WEIGHT_SHIFT;
-               lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
-                  PNG_WEIGHT_SHIFT;
-            }
-         }
-
-         lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
-            PNG_COST_SHIFT;
-         lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
-            PNG_COST_SHIFT;
-
-         if (lmhi > PNG_HIMASK)
-            lmins = PNG_MAXSUM;
-         else
-            lmins = (lmhi << PNG_HISHIFT) + lmlo;
-      }
-#endif
-
-      for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
-           pp = prev_row + 1; i < bpp; i++)
-      {
-         v = *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
-
-         sum += (v < 128) ? v : 256 - v;
-      }
-      for (lp = row_buf + 1; i < row_bytes; i++)
-      {
-         v = *dp++ =
-          (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2)) & 0xff);
-
-         sum += (v < 128) ? v : 256 - v;
-
-         if (sum > lmins)  /* We are already worse, don't continue. */
-            break;
-      }
-
-#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
-      if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
-      {
-         int j;
-         png_uint_32 sumhi, sumlo;
-         sumlo = sum & PNG_LOMASK;
-         sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
-
-         for (j = 0; j < num_p_filters; j++)
-         {
-            if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
-            {
-               sumlo = (sumlo * png_ptr->filter_weights[j]) >>
-                  PNG_WEIGHT_SHIFT;
-               sumhi = (sumhi * png_ptr->filter_weights[j]) >>
-                  PNG_WEIGHT_SHIFT;
-            }
-         }
-
-         sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
-            PNG_COST_SHIFT;
-         sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
-            PNG_COST_SHIFT;
-
-         if (sumhi > PNG_HIMASK)
-            sum = PNG_MAXSUM;
-         else
-            sum = (sumhi << PNG_HISHIFT) + sumlo;
-      }
-#endif
-
-      if (sum < mins)
-      {
-         mins = sum;
-         best_row = png_ptr->avg_row;
-      }
-   }
-
-   /* Paeth filter */
-   if (filter_to_do == PNG_FILTER_PAETH)
-   {
-      png_bytep rp, dp, pp, cp, lp;
-      png_uint_32 i;
-      for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
-           pp = prev_row + 1; i < bpp; i++)
-      {
-         *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
-      }
-
-      for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
-      {
-         int a, b, c, pa, pb, pc, p;
-
-         b = *pp++;
-         c = *cp++;
-         a = *lp++;
-
-         p = b - c;
-         pc = a - c;
-
-#ifdef PNG_USE_ABS
-         pa = abs(p);
-         pb = abs(pc);
-         pc = abs(p + pc);
-#else
-         pa = p < 0 ? -p : p;
-         pb = pc < 0 ? -pc : pc;
-         pc = (p + pc) < 0 ? -(p + pc) : p + pc;
-#endif
-
-         p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
-
-         *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
-      }
-      best_row = png_ptr->paeth_row;
-   }
-
-   else if (filter_to_do & PNG_FILTER_PAETH)
-   {
-      png_bytep rp, dp, pp, cp, lp;
-      png_uint_32 sum = 0, lmins = mins;
-      png_uint_32 i;
-      int v;
-
-#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
-      if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
-      {
-         int j;
-         png_uint_32 lmhi, lmlo;
-         lmlo = lmins & PNG_LOMASK;
-         lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
-
-         for (j = 0; j < num_p_filters; j++)
-         {
-            if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
-            {
-               lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
-                  PNG_WEIGHT_SHIFT;
-               lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
-                  PNG_WEIGHT_SHIFT;
-            }
-         }
-
-         lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
-            PNG_COST_SHIFT;
-         lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
-            PNG_COST_SHIFT;
-
-         if (lmhi > PNG_HIMASK)
-            lmins = PNG_MAXSUM;
-         else
-            lmins = (lmhi << PNG_HISHIFT) + lmlo;
-      }
-#endif
-
-      for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
-           pp = prev_row + 1; i < bpp; i++)
-      {
-         v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
-
-         sum += (v < 128) ? v : 256 - v;
-      }
-
-      for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
-      {
-         int a, b, c, pa, pb, pc, p;
-
-         b = *pp++;
-         c = *cp++;
-         a = *lp++;
-
-#ifndef PNG_SLOW_PAETH
-         p = b - c;
-         pc = a - c;
-#ifdef PNG_USE_ABS
-         pa = abs(p);
-         pb = abs(pc);
-         pc = abs(p + pc);
-#else
-         pa = p < 0 ? -p : p;
-         pb = pc < 0 ? -pc : pc;
-         pc = (p + pc) < 0 ? -(p + pc) : p + pc;
-#endif
-         p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
-#else /* PNG_SLOW_PAETH */
-         p = a + b - c;
-         pa = abs(p - a);
-         pb = abs(p - b);
-         pc = abs(p - c);
-         if (pa <= pb && pa <= pc)
-            p = a;
-         else if (pb <= pc)
-            p = b;
-         else
-            p = c;
-#endif /* PNG_SLOW_PAETH */
-
-         v = *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
-
-         sum += (v < 128) ? v : 256 - v;
-
-         if (sum > lmins)  /* We are already worse, don't continue. */
-            break;
-      }
-
-#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
-      if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
-      {
-         int j;
-         png_uint_32 sumhi, sumlo;
-         sumlo = sum & PNG_LOMASK;
-         sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
-
-         for (j = 0; j < num_p_filters; j++)
-         {
-            if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
-            {
-               sumlo = (sumlo * png_ptr->filter_weights[j]) >>
-                  PNG_WEIGHT_SHIFT;
-               sumhi = (sumhi * png_ptr->filter_weights[j]) >>
-                  PNG_WEIGHT_SHIFT;
-            }
-         }
-
-         sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
-            PNG_COST_SHIFT;
-         sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
-            PNG_COST_SHIFT;
-
-         if (sumhi > PNG_HIMASK)
-            sum = PNG_MAXSUM;
-         else
-            sum = (sumhi << PNG_HISHIFT) + sumlo;
-      }
-#endif
-
-      if (sum < mins)
-      {
-         best_row = png_ptr->paeth_row;
-      }
-   }
-#endif /* PNG_NO_WRITE_FILTER */
-   /* Do the actual writing of the filtered row data from the chosen filter. */
-
-   png_write_filtered_row(png_ptr, best_row);
-
-#ifndef PNG_NO_WRITE_FILTER
-#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
-   /* Save the type of filter we picked this time for future calculations */
-   if (png_ptr->num_prev_filters > 0)
-   {
-      int j;
-      for (j = 1; j < num_p_filters; j++)
-      {
-         png_ptr->prev_filters[j] = png_ptr->prev_filters[j - 1];
-      }
-      png_ptr->prev_filters[j] = best_row[0];
-   }
-#endif
-#endif /* PNG_NO_WRITE_FILTER */
-}
-
-
-/* Do the actual writing of a previously filtered row. */
-void /* PRIVATE */
-png_write_filtered_row(png_structp png_ptr, png_bytep filtered_row)
-{
-   png_debug(1, "in png_write_filtered_row\n");
-   png_debug1(2, "filter = %d\n", filtered_row[0]);
-   /* set up the zlib input buffer */
-
-   png_ptr->zstream.next_in = filtered_row;
-   png_ptr->zstream.avail_in = (uInt)png_ptr->row_info.rowbytes + 1;
-   /* repeat until we have compressed all the data */
-   do
-   {
-      int ret; /* return of zlib */
-
-      /* compress the data */
-      ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);
-      /* check for compression errors */
-      if (ret != Z_OK)
-      {
-         if (png_ptr->zstream.msg != NULL)
-            png_error(png_ptr, png_ptr->zstream.msg);
-         else
-            png_error(png_ptr, "zlib error");
-      }
-
-      /* see if it is time to write another IDAT */
-      if (!(png_ptr->zstream.avail_out))
-      {
-         /* write the IDAT and reset the zlib output buffer */
-         png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
-         png_ptr->zstream.next_out = png_ptr->zbuf;
-         png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
-      }
-   /* repeat until all data has been compressed */
-   } while (png_ptr->zstream.avail_in);
-
-   /* swap the current and previous rows */
-   if (png_ptr->prev_row != NULL)
-   {
-      png_bytep tptr;
-
-      tptr = png_ptr->prev_row;
-      png_ptr->prev_row = png_ptr->row_buf;
-      png_ptr->row_buf = tptr;
-   }
-
-   /* finish row - updates counters and flushes zlib if last row */
-   png_write_finish_row(png_ptr);
-
-#if defined(PNG_WRITE_FLUSH_SUPPORTED)
-   png_ptr->flush_rows++;
-
-   if (png_ptr->flush_dist > 0 &&
-       png_ptr->flush_rows >= png_ptr->flush_dist)
-   {
-      png_write_flush(png_ptr);
-   }
-#endif
-}
-#endif /* PNG_WRITE_SUPPORTED */
+\r
+/* pngwutil.c - utilities to write a PNG file\r
+ *\r
+ * Last changed in libpng 1.4.1 [February 25, 2010]\r
+ * Copyright (c) 1998-2010 Glenn Randers-Pehrson\r
+ * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)\r
+ * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)\r
+ *\r
+ * This code is released under the libpng license.\r
+ * For conditions of distribution and use, see the disclaimer\r
+ * and license in png.h\r
+ */\r
+\r
+#define PNG_NO_PEDANTIC_WARNINGS\r
+#include "png.h"\r
+#ifdef PNG_WRITE_SUPPORTED\r
+#include "pngpriv.h"\r
+\r
+/* Place a 32-bit number into a buffer in PNG byte order.  We work\r
+ * with unsigned numbers for convenience, although one supported\r
+ * ancillary chunk uses signed (two's complement) numbers.\r
+ */\r
+void PNGAPI\r
+png_save_uint_32(png_bytep buf, png_uint_32 i)\r
+{\r
+   buf[0] = (png_byte)((i >> 24) & 0xff);\r
+   buf[1] = (png_byte)((i >> 16) & 0xff);\r
+   buf[2] = (png_byte)((i >> 8) & 0xff);\r
+   buf[3] = (png_byte)(i & 0xff);\r
+}\r
+\r
+#ifdef PNG_SAVE_INT_32_SUPPORTED\r
+/* The png_save_int_32 function assumes integers are stored in two's\r
+ * complement format.  If this isn't the case, then this routine needs to\r
+ * be modified to write data in two's complement format.\r
+ */\r
+void PNGAPI\r
+png_save_int_32(png_bytep buf, png_int_32 i)\r
+{\r
+   buf[0] = (png_byte)((i >> 24) & 0xff);\r
+   buf[1] = (png_byte)((i >> 16) & 0xff);\r
+   buf[2] = (png_byte)((i >> 8) & 0xff);\r
+   buf[3] = (png_byte)(i & 0xff);\r
+}\r
+#endif\r
+\r
+/* Place a 16-bit number into a buffer in PNG byte order.\r
+ * The parameter is declared unsigned int, not png_uint_16,\r
+ * just to avoid potential problems on pre-ANSI C compilers.\r
+ */\r
+void PNGAPI\r
+png_save_uint_16(png_bytep buf, unsigned int i)\r
+{\r
+   buf[0] = (png_byte)((i >> 8) & 0xff);\r
+   buf[1] = (png_byte)(i & 0xff);\r
+}\r
+\r
+/* Simple function to write the signature.  If we have already written\r
+ * the magic bytes of the signature, or more likely, the PNG stream is\r
+ * being embedded into another stream and doesn't need its own signature,\r
+ * we should call png_set_sig_bytes() to tell libpng how many of the\r
+ * bytes have already been written.\r
+ */\r
+void PNGAPI\r
+png_write_sig(png_structp png_ptr)\r
+{\r
+   png_byte png_signature[8] = {137, 80, 78, 71, 13, 10, 26, 10};\r
+\r
+#ifdef PNG_IO_STATE_SUPPORTED\r
+   /* Inform the I/O callback that the signature is being written */\r
+   png_ptr->io_state = PNG_IO_WRITING | PNG_IO_SIGNATURE;\r
+#endif\r
+\r
+   /* Write the rest of the 8 byte signature */\r
+   png_write_data(png_ptr, &png_signature[png_ptr->sig_bytes],\r
+      (png_size_t)(8 - png_ptr->sig_bytes));\r
+   if (png_ptr->sig_bytes < 3)\r
+      png_ptr->mode |= PNG_HAVE_PNG_SIGNATURE;\r
+}\r
+\r
+/* Write a PNG chunk all at once.  The type is an array of ASCII characters\r
+ * representing the chunk name.  The array must be at least 4 bytes in\r
+ * length, and does not need to be null terminated.  To be safe, pass the\r
+ * pre-defined chunk names here, and if you need a new one, define it\r
+ * where the others are defined.  The length is the length of the data.\r
+ * All the data must be present.  If that is not possible, use the\r
+ * png_write_chunk_start(), png_write_chunk_data(), and png_write_chunk_end()\r
+ * functions instead.\r
+ */\r
+void PNGAPI\r
+png_write_chunk(png_structp png_ptr, png_bytep chunk_name,\r
+   png_bytep data, png_size_t length)\r
+{\r
+   if (png_ptr == NULL)\r
+      return;\r
+   png_write_chunk_start(png_ptr, chunk_name, (png_uint_32)length);\r
+   png_write_chunk_data(png_ptr, data, (png_size_t)length);\r
+   png_write_chunk_end(png_ptr);\r
+}\r
+\r
+/* Write the start of a PNG chunk.  The type is the chunk type.\r
+ * The total_length is the sum of the lengths of all the data you will be\r
+ * passing in png_write_chunk_data().\r
+ */\r
+void PNGAPI\r
+png_write_chunk_start(png_structp png_ptr, png_bytep chunk_name,\r
+   png_uint_32 length)\r
+{\r
+   png_byte buf[8];\r
+\r
+   png_debug2(0, "Writing %s chunk, length = %lu", chunk_name,\r
+      (unsigned long)length);\r
+\r
+   if (png_ptr == NULL)\r
+      return;\r
+\r
+#ifdef PNG_IO_STATE_SUPPORTED\r
+   /* Inform the I/O callback that the chunk header is being written.\r
+    * PNG_IO_CHUNK_HDR requires a single I/O call.\r
+    */\r
+   png_ptr->io_state = PNG_IO_WRITING | PNG_IO_CHUNK_HDR;\r
+#endif\r
+\r
+   /* Write the length and the chunk name */\r
+   png_save_uint_32(buf, length);\r
+   png_memcpy(buf + 4, chunk_name, 4);\r
+   png_write_data(png_ptr, buf, (png_size_t)8);\r
+   /* Put the chunk name into png_ptr->chunk_name */\r
+   png_memcpy(png_ptr->chunk_name, chunk_name, 4);\r
+   /* Reset the crc and run it over the chunk name */\r
+   png_reset_crc(png_ptr);\r
+   png_calculate_crc(png_ptr, chunk_name, 4);\r
+\r
+#ifdef PNG_IO_STATE_SUPPORTED\r
+   /* Inform the I/O callback that chunk data will (possibly) be written.\r
+    * PNG_IO_CHUNK_DATA does NOT require a specific number of I/O calls.\r
+    */\r
+   png_ptr->io_state = PNG_IO_WRITING | PNG_IO_CHUNK_DATA;\r
+#endif\r
+}\r
+\r
+/* Write the data of a PNG chunk started with png_write_chunk_start().\r
+ * Note that multiple calls to this function are allowed, and that the\r
+ * sum of the lengths from these calls *must* add up to the total_length\r
+ * given to png_write_chunk_start().\r
+ */\r
+void PNGAPI\r
+png_write_chunk_data(png_structp png_ptr, png_bytep data, png_size_t length)\r
+{\r
+   /* Write the data, and run the CRC over it */\r
+   if (png_ptr == NULL)\r
+      return;\r
+   if (data != NULL && length > 0)\r
+   {\r
+      png_write_data(png_ptr, data, length);\r
+      /* Update the CRC after writing the data,\r
+       * in case that the user I/O routine alters it.\r
+       */\r
+      png_calculate_crc(png_ptr, data, length);\r
+   }\r
+}\r
+\r
+/* Finish a chunk started with png_write_chunk_start(). */\r
+void PNGAPI\r
+png_write_chunk_end(png_structp png_ptr)\r
+{\r
+   png_byte buf[4];\r
+\r
+   if (png_ptr == NULL) return;\r
+\r
+#ifdef PNG_IO_STATE_SUPPORTED\r
+   /* Inform the I/O callback that the chunk CRC is being written.\r
+    * PNG_IO_CHUNK_CRC requires a single I/O function call.\r
+    */\r
+   png_ptr->io_state = PNG_IO_WRITING | PNG_IO_CHUNK_CRC;\r
+#endif\r
+\r
+   /* Write the crc in a single operation */\r
+   png_save_uint_32(buf, png_ptr->crc);\r
+\r
+   png_write_data(png_ptr, buf, (png_size_t)4);\r
+}\r
+\r
+#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_iCCP_SUPPORTED)\r
+/* This pair of functions encapsulates the operation of (a) compressing a\r
+ * text string, and (b) issuing it later as a series of chunk data writes.\r
+ * The compression_state structure is shared context for these functions\r
+ * set up by the caller in order to make the whole mess thread-safe.\r
+ */\r
+\r
+typedef struct\r
+{\r
+   char *input;   /* The uncompressed input data */\r
+   int input_len;   /* Its length */\r
+   int num_output_ptr; /* Number of output pointers used */\r
+   int max_output_ptr; /* Size of output_ptr */\r
+   png_charpp output_ptr; /* Array of pointers to output */\r
+} compression_state;\r
+\r
+/* Compress given text into storage in the png_ptr structure */\r
+static int /* PRIVATE */\r
+png_text_compress(png_structp png_ptr,\r
+        png_charp text, png_size_t text_len, int compression,\r
+        compression_state *comp)\r
+{\r
+   int ret;\r
+\r
+   comp->num_output_ptr = 0;\r
+   comp->max_output_ptr = 0;\r
+   comp->output_ptr = NULL;\r
+   comp->input = NULL;\r
+   comp->input_len = 0;\r
+\r
+   /* We may just want to pass the text right through */\r
+   if (compression == PNG_TEXT_COMPRESSION_NONE)\r
+   {\r
+       comp->input = text;\r
+       comp->input_len = text_len;\r
+       return((int)text_len);\r
+   }\r
+\r
+   if (compression >= PNG_TEXT_COMPRESSION_LAST)\r
+   {\r
+#ifdef PNG_STDIO_SUPPORTED\r
+      char msg[50];\r
+      png_snprintf(msg, 50, "Unknown compression type %d", compression);\r
+      png_warning(png_ptr, msg);\r
+#else\r
+      png_warning(png_ptr, "Unknown compression type");\r
+#endif\r
+   }\r
+\r
+   /* We can't write the chunk until we find out how much data we have,\r
+    * which means we need to run the compressor first and save the\r
+    * output.  This shouldn't be a problem, as the vast majority of\r
+    * comments should be reasonable, but we will set up an array of\r
+    * malloc'd pointers to be sure.\r
+    *\r
+    * If we knew the application was well behaved, we could simplify this\r
+    * greatly by assuming we can always malloc an output buffer large\r
+    * enough to hold the compressed text ((1001 * text_len / 1000) + 12)\r
+    * and malloc this directly.  The only time this would be a bad idea is\r
+    * if we can't malloc more than 64K and we have 64K of random input\r
+    * data, or if the input string is incredibly large (although this\r
+    * wouldn't cause a failure, just a slowdown due to swapping).\r
+    */\r
+\r
+   /* Set up the compression buffers */\r
+   png_ptr->zstream.avail_in = (uInt)text_len;\r
+   png_ptr->zstream.next_in = (Bytef *)text;\r
+   png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;\r
+   png_ptr->zstream.next_out = (Bytef *)png_ptr->zbuf;\r
+\r
+   /* This is the same compression loop as in png_write_row() */\r
+   do\r
+   {\r
+      /* Compress the data */\r
+      ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);\r
+      if (ret != Z_OK)\r
+      {\r
+         /* Error */\r
+         if (png_ptr->zstream.msg != NULL)\r
+            png_error(png_ptr, png_ptr->zstream.msg);\r
+         else\r
+            png_error(png_ptr, "zlib error");\r
+      }\r
+      /* Check to see if we need more room */\r
+      if (!(png_ptr->zstream.avail_out))\r
+      {\r
+         /* Make sure the output array has room */\r
+         if (comp->num_output_ptr >= comp->max_output_ptr)\r
+         {\r
+            int old_max;\r
+\r
+            old_max = comp->max_output_ptr;\r
+            comp->max_output_ptr = comp->num_output_ptr + 4;\r
+            if (comp->output_ptr != NULL)\r
+            {\r
+               png_charpp old_ptr;\r
+\r
+               old_ptr = comp->output_ptr;\r
+               comp->output_ptr = (png_charpp)png_malloc(png_ptr,\r
+                  (png_alloc_size_t)\r
+                  (comp->max_output_ptr * png_sizeof(png_charpp)));\r
+               png_memcpy(comp->output_ptr, old_ptr, old_max\r
+                  * png_sizeof(png_charp));\r
+               png_free(png_ptr, old_ptr);\r
+            }\r
+            else\r
+               comp->output_ptr = (png_charpp)png_malloc(png_ptr,\r
+                  (png_alloc_size_t)\r
+                  (comp->max_output_ptr * png_sizeof(png_charp)));\r
+         }\r
+\r
+         /* Save the data */\r
+         comp->output_ptr[comp->num_output_ptr] =\r
+            (png_charp)png_malloc(png_ptr,\r
+            (png_alloc_size_t)png_ptr->zbuf_size);\r
+         png_memcpy(comp->output_ptr[comp->num_output_ptr], png_ptr->zbuf,\r
+            png_ptr->zbuf_size);\r
+         comp->num_output_ptr++;\r
+\r
+         /* and reset the buffer */\r
+         png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;\r
+         png_ptr->zstream.next_out = png_ptr->zbuf;\r
+      }\r
+   /* Continue until we don't have any more to compress */\r
+   } while (png_ptr->zstream.avail_in);\r
+\r
+   /* Finish the compression */\r
+   do\r
+   {\r
+      /* Tell zlib we are finished */\r
+      ret = deflate(&png_ptr->zstream, Z_FINISH);\r
+\r
+      if (ret == Z_OK)\r
+      {\r
+         /* Check to see if we need more room */\r
+         if (!(png_ptr->zstream.avail_out))\r
+         {\r
+            /* Check to make sure our output array has room */\r
+            if (comp->num_output_ptr >= comp->max_output_ptr)\r
+            {\r
+               int old_max;\r
+\r
+               old_max = comp->max_output_ptr;\r
+               comp->max_output_ptr = comp->num_output_ptr + 4;\r
+               if (comp->output_ptr != NULL)\r
+               {\r
+                  png_charpp old_ptr;\r
+\r
+                  old_ptr = comp->output_ptr;\r
+                  /* This could be optimized to realloc() */\r
+                  comp->output_ptr = (png_charpp)png_malloc(png_ptr,\r
+                     (png_alloc_size_t)(comp->max_output_ptr *\r
+                     png_sizeof(png_charp)));\r
+                  png_memcpy(comp->output_ptr, old_ptr,\r
+                     old_max * png_sizeof(png_charp));\r
+                  png_free(png_ptr, old_ptr);\r
+               }\r
+               else\r
+                  comp->output_ptr = (png_charpp)png_malloc(png_ptr,\r
+                     (png_alloc_size_t)(comp->max_output_ptr *\r
+                     png_sizeof(png_charp)));\r
+            }\r
+\r
+            /* Save the data */\r
+            comp->output_ptr[comp->num_output_ptr] =\r
+               (png_charp)png_malloc(png_ptr,\r
+               (png_alloc_size_t)png_ptr->zbuf_size);\r
+            png_memcpy(comp->output_ptr[comp->num_output_ptr], png_ptr->zbuf,\r
+               png_ptr->zbuf_size);\r
+            comp->num_output_ptr++;\r
+\r
+            /* and reset the buffer pointers */\r
+            png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;\r
+            png_ptr->zstream.next_out = png_ptr->zbuf;\r
+         }\r
+      }\r
+      else if (ret != Z_STREAM_END)\r
+      {\r
+         /* We got an error */\r
+         if (png_ptr->zstream.msg != NULL)\r
+            png_error(png_ptr, png_ptr->zstream.msg);\r
+         else\r
+            png_error(png_ptr, "zlib error");\r
+      }\r
+   } while (ret != Z_STREAM_END);\r
+\r
+   /* Text length is number of buffers plus last buffer */\r
+   text_len = png_ptr->zbuf_size * comp->num_output_ptr;\r
+   if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)\r
+      text_len += png_ptr->zbuf_size - (png_size_t)png_ptr->zstream.avail_out;\r
+\r
+   return((int)text_len);\r
+}\r
+\r
+/* Ship the compressed text out via chunk writes */\r
+static void /* PRIVATE */\r
+png_write_compressed_data_out(png_structp png_ptr, compression_state *comp)\r
+{\r
+   int i;\r
+\r
+   /* Handle the no-compression case */\r
+   if (comp->input)\r
+   {\r
+      png_write_chunk_data(png_ptr, (png_bytep)comp->input,\r
+                            (png_size_t)comp->input_len);\r
+      return;\r
+   }\r
+\r
+   /* Write saved output buffers, if any */\r
+   for (i = 0; i < comp->num_output_ptr; i++)\r
+   {\r
+      png_write_chunk_data(png_ptr, (png_bytep)comp->output_ptr[i],\r
+         (png_size_t)png_ptr->zbuf_size);\r
+      png_free(png_ptr, comp->output_ptr[i]);\r
+   }\r
+   if (comp->max_output_ptr != 0)\r
+      png_free(png_ptr, comp->output_ptr);\r
+   /* Write anything left in zbuf */\r
+   if (png_ptr->zstream.avail_out < (png_uint_32)png_ptr->zbuf_size)\r
+      png_write_chunk_data(png_ptr, png_ptr->zbuf,\r
+         (png_size_t)(png_ptr->zbuf_size - png_ptr->zstream.avail_out));\r
+\r
+   /* Reset zlib for another zTXt/iTXt or image data */\r
+   deflateReset(&png_ptr->zstream);\r
+   png_ptr->zstream.data_type = Z_BINARY;\r
+}\r
+#endif\r
+\r
+/* Write the IHDR chunk, and update the png_struct with the necessary\r
+ * information.  Note that the rest of this code depends upon this\r
+ * information being correct.\r
+ */\r
+void /* PRIVATE */\r
+png_write_IHDR(png_structp png_ptr, png_uint_32 width, png_uint_32 height,\r
+   int bit_depth, int color_type, int compression_type, int filter_type,\r
+   int interlace_type)\r
+{\r
+   PNG_IHDR;\r
+   int ret;\r
+\r
+   png_byte buf[13]; /* Buffer to store the IHDR info */\r
+\r
+   png_debug(1, "in png_write_IHDR");\r
+\r
+   /* Check that we have valid input data from the application info */\r
+   switch (color_type)\r
+   {\r
+      case PNG_COLOR_TYPE_GRAY:\r
+         switch (bit_depth)\r
+         {\r
+            case 1:\r
+            case 2:\r
+            case 4:\r
+            case 8:\r
+            case 16: png_ptr->channels = 1; break;\r
+            default: png_error(png_ptr,\r
+                         "Invalid bit depth for grayscale image");\r
+         }\r
+         break;\r
+      case PNG_COLOR_TYPE_RGB:\r
+         if (bit_depth != 8 && bit_depth != 16)\r
+            png_error(png_ptr, "Invalid bit depth for RGB image");\r
+         png_ptr->channels = 3;\r
+         break;\r
+      case PNG_COLOR_TYPE_PALETTE:\r
+         switch (bit_depth)\r
+         {\r
+            case 1:\r
+            case 2:\r
+            case 4:\r
+            case 8: png_ptr->channels = 1; break;\r
+            default: png_error(png_ptr, "Invalid bit depth for paletted image");\r
+         }\r
+         break;\r
+      case PNG_COLOR_TYPE_GRAY_ALPHA:\r
+         if (bit_depth != 8 && bit_depth != 16)\r
+            png_error(png_ptr, "Invalid bit depth for grayscale+alpha image");\r
+         png_ptr->channels = 2;\r
+         break;\r
+      case PNG_COLOR_TYPE_RGB_ALPHA:\r
+         if (bit_depth != 8 && bit_depth != 16)\r
+            png_error(png_ptr, "Invalid bit depth for RGBA image");\r
+         png_ptr->channels = 4;\r
+         break;\r
+      default:\r
+         png_error(png_ptr, "Invalid image color type specified");\r
+   }\r
+\r
+   if (compression_type != PNG_COMPRESSION_TYPE_BASE)\r
+   {\r
+      png_warning(png_ptr, "Invalid compression type specified");\r
+      compression_type = PNG_COMPRESSION_TYPE_BASE;\r
+   }\r
+\r
+   /* Write filter_method 64 (intrapixel differencing) only if\r
+    * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and\r
+    * 2. Libpng did not write a PNG signature (this filter_method is only\r
+    *    used in PNG datastreams that are embedded in MNG datastreams) and\r
+    * 3. The application called png_permit_mng_features with a mask that\r
+    *    included PNG_FLAG_MNG_FILTER_64 and\r
+    * 4. The filter_method is 64 and\r
+    * 5. The color_type is RGB or RGBA\r
+    */\r
+   if (\r
+#ifdef PNG_MNG_FEATURES_SUPPORTED\r
+      !((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&\r
+      ((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE) == 0) &&\r
+      (color_type == PNG_COLOR_TYPE_RGB ||\r
+       color_type == PNG_COLOR_TYPE_RGB_ALPHA) &&\r
+      (filter_type == PNG_INTRAPIXEL_DIFFERENCING)) &&\r
+#endif\r
+      filter_type != PNG_FILTER_TYPE_BASE)\r
+   {\r
+      png_warning(png_ptr, "Invalid filter type specified");\r
+      filter_type = PNG_FILTER_TYPE_BASE;\r
+   }\r
+\r
+#ifdef PNG_WRITE_INTERLACING_SUPPORTED\r
+   if (interlace_type != PNG_INTERLACE_NONE &&\r
+      interlace_type != PNG_INTERLACE_ADAM7)\r
+   {\r
+      png_warning(png_ptr, "Invalid interlace type specified");\r
+      interlace_type = PNG_INTERLACE_ADAM7;\r
+   }\r
+#else\r
+   interlace_type=PNG_INTERLACE_NONE;\r
+#endif\r
+\r
+   /* Save the relevent information */\r
+   png_ptr->bit_depth = (png_byte)bit_depth;\r
+   png_ptr->color_type = (png_byte)color_type;\r
+   png_ptr->interlaced = (png_byte)interlace_type;\r
+#ifdef PNG_MNG_FEATURES_SUPPORTED\r
+   png_ptr->filter_type = (png_byte)filter_type;\r
+#endif\r
+   png_ptr->compression_type = (png_byte)compression_type;\r
+   png_ptr->width = width;\r
+   png_ptr->height = height;\r
+\r
+   png_ptr->pixel_depth = (png_byte)(bit_depth * png_ptr->channels);\r
+   png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, width);\r
+   /* Set the usr info, so any transformations can modify it */\r
+   png_ptr->usr_width = png_ptr->width;\r
+   png_ptr->usr_bit_depth = png_ptr->bit_depth;\r
+   png_ptr->usr_channels = png_ptr->channels;\r
+\r
+   /* Pack the header information into the buffer */\r
+   png_save_uint_32(buf, width);\r
+   png_save_uint_32(buf + 4, height);\r
+   buf[8] = (png_byte)bit_depth;\r
+   buf[9] = (png_byte)color_type;\r
+   buf[10] = (png_byte)compression_type;\r
+   buf[11] = (png_byte)filter_type;\r
+   buf[12] = (png_byte)interlace_type;\r
+\r
+   /* Write the chunk */\r
+   png_write_chunk(png_ptr, (png_bytep)png_IHDR, buf, (png_size_t)13);\r
+\r
+   /* Initialize zlib with PNG info */\r
+   png_ptr->zstream.zalloc = png_zalloc;\r
+   png_ptr->zstream.zfree = png_zfree;\r
+   png_ptr->zstream.opaque = (voidpf)png_ptr;\r
+   if (!(png_ptr->do_filter))\r
+   {\r
+      if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE ||\r
+         png_ptr->bit_depth < 8)\r
+         png_ptr->do_filter = PNG_FILTER_NONE;\r
+      else\r
+         png_ptr->do_filter = PNG_ALL_FILTERS;\r
+   }\r
+   if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_STRATEGY))\r
+   {\r
+      if (png_ptr->do_filter != PNG_FILTER_NONE)\r
+         png_ptr->zlib_strategy = Z_FILTERED;\r
+      else\r
+         png_ptr->zlib_strategy = Z_DEFAULT_STRATEGY;\r
+   }\r
+   if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_LEVEL))\r
+      png_ptr->zlib_level = Z_DEFAULT_COMPRESSION;\r
+   if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL))\r
+      png_ptr->zlib_mem_level = 8;\r
+   if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS))\r
+      png_ptr->zlib_window_bits = 15;\r
+   if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_METHOD))\r
+      png_ptr->zlib_method = 8;\r
+   ret = deflateInit2(&png_ptr->zstream, png_ptr->zlib_level,\r
+         png_ptr->zlib_method, png_ptr->zlib_window_bits,\r
+         png_ptr->zlib_mem_level, png_ptr->zlib_strategy);\r
+   if (ret != Z_OK)\r
+   {\r
+      if (ret == Z_VERSION_ERROR) png_error(png_ptr,\r
+          "zlib failed to initialize compressor -- version error");\r
+      if (ret == Z_STREAM_ERROR) png_error(png_ptr,\r
+           "zlib failed to initialize compressor -- stream error");\r
+      if (ret == Z_MEM_ERROR) png_error(png_ptr,\r
+           "zlib failed to initialize compressor -- mem error");\r
+      png_error(png_ptr, "zlib failed to initialize compressor");\r
+   }\r
+   png_ptr->zstream.next_out = png_ptr->zbuf;\r
+   png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;\r
+   /* libpng is not interested in zstream.data_type */\r
+   /* Set it to a predefined value, to avoid its evaluation inside zlib */\r
+   png_ptr->zstream.data_type = Z_BINARY;\r
+\r
+   png_ptr->mode = PNG_HAVE_IHDR;\r
+}\r
+\r
+/* Write the palette.  We are careful not to trust png_color to be in the\r
+ * correct order for PNG, so people can redefine it to any convenient\r
+ * structure.\r
+ */\r
+void /* PRIVATE */\r
+png_write_PLTE(png_structp png_ptr, png_colorp palette, png_uint_32 num_pal)\r
+{\r
+   PNG_PLTE;\r
+   png_uint_32 i;\r
+   png_colorp pal_ptr;\r
+   png_byte buf[3];\r
+\r
+   png_debug(1, "in png_write_PLTE");\r
+\r
+   if ((\r
+#ifdef PNG_MNG_FEATURES_SUPPORTED\r
+        !(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE) &&\r
+#endif\r
+        num_pal == 0) || num_pal > 256)\r
+   {\r
+     if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)\r
+     {\r
+        png_error(png_ptr, "Invalid number of colors in palette");\r
+     }\r
+     else\r
+     {\r
+        png_warning(png_ptr, "Invalid number of colors in palette");\r
+        return;\r
+     }\r
+   }\r
+\r
+   if (!(png_ptr->color_type&PNG_COLOR_MASK_COLOR))\r
+   {\r
+      png_warning(png_ptr,\r
+        "Ignoring request to write a PLTE chunk in grayscale PNG");\r
+      return;\r
+   }\r
+\r
+   png_ptr->num_palette = (png_uint_16)num_pal;\r
+   png_debug1(3, "num_palette = %d", png_ptr->num_palette);\r
+\r
+   png_write_chunk_start(png_ptr, (png_bytep)png_PLTE,\r
+     (png_uint_32)(num_pal * 3));\r
+#ifdef PNG_POINTER_INDEXING_SUPPORTED\r
+   for (i = 0, pal_ptr = palette; i < num_pal; i++, pal_ptr++)\r
+   {\r
+      buf[0] = pal_ptr->red;\r
+      buf[1] = pal_ptr->green;\r
+      buf[2] = pal_ptr->blue;\r
+      png_write_chunk_data(png_ptr, buf, (png_size_t)3);\r
+   }\r
+#else\r
+   /* This is a little slower but some buggy compilers need to do this\r
+    * instead\r
+    */\r
+   pal_ptr=palette;\r
+   for (i = 0; i < num_pal; i++)\r
+   {\r
+      buf[0] = pal_ptr[i].red;\r
+      buf[1] = pal_ptr[i].green;\r
+      buf[2] = pal_ptr[i].blue;\r
+      png_write_chunk_data(png_ptr, buf, (png_size_t)3);\r
+   }\r
+#endif\r
+   png_write_chunk_end(png_ptr);\r
+   png_ptr->mode |= PNG_HAVE_PLTE;\r
+}\r
+\r
+/* Write an IDAT chunk */\r
+void /* PRIVATE */\r
+png_write_IDAT(png_structp png_ptr, png_bytep data, png_size_t length)\r
+{\r
+   PNG_IDAT;\r
+\r
+   png_debug(1, "in png_write_IDAT");\r
+\r
+   /* Optimize the CMF field in the zlib stream. */\r
+   /* This hack of the zlib stream is compliant to the stream specification. */\r
+   if (!(png_ptr->mode & PNG_HAVE_IDAT) &&\r
+       png_ptr->compression_type == PNG_COMPRESSION_TYPE_BASE)\r
+   {\r
+      unsigned int z_cmf = data[0];  /* zlib compression method and flags */\r
+      if ((z_cmf & 0x0f) == 8 && (z_cmf & 0xf0) <= 0x70)\r
+      {\r
+         /* Avoid memory underflows and multiplication overflows.\r
+          *\r
+          * The conditions below are practically always satisfied;\r
+          * however, they still must be checked.\r
+          */\r
+         if (length >= 2 &&\r
+             png_ptr->height < 16384 && png_ptr->width < 16384)\r
+         {\r
+            png_uint_32 uncompressed_idat_size = png_ptr->height *\r
+               ((png_ptr->width *\r
+               png_ptr->channels * png_ptr->bit_depth + 15) >> 3);\r
+            unsigned int z_cinfo = z_cmf >> 4;\r
+            unsigned int half_z_window_size = 1 << (z_cinfo + 7);\r
+            while (uncompressed_idat_size <= half_z_window_size &&\r
+                   half_z_window_size >= 256)\r
+            {\r
+               z_cinfo--;\r
+               half_z_window_size >>= 1;\r
+            }\r
+            z_cmf = (z_cmf & 0x0f) | (z_cinfo << 4);\r
+            if (data[0] != (png_byte)z_cmf)\r
+            {\r
+               data[0] = (png_byte)z_cmf;\r
+               data[1] &= 0xe0;\r
+               data[1] += (png_byte)(0x1f - ((z_cmf << 8) + data[1]) % 0x1f);\r
+            }\r
+         }\r
+      }\r
+      else\r
+         png_error(png_ptr,\r
+            "Invalid zlib compression method or flags in IDAT");\r
+   }\r
+\r
+   png_write_chunk(png_ptr, (png_bytep)png_IDAT, data, length);\r
+   png_ptr->mode |= PNG_HAVE_IDAT;\r
+}\r
+\r
+/* Write an IEND chunk */\r
+void /* PRIVATE */\r
+png_write_IEND(png_structp png_ptr)\r
+{\r
+   PNG_IEND;\r
+\r
+   png_debug(1, "in png_write_IEND");\r
+\r
+   png_write_chunk(png_ptr, (png_bytep)png_IEND, NULL,\r
+     (png_size_t)0);\r
+   png_ptr->mode |= PNG_HAVE_IEND;\r
+}\r
+\r
+#ifdef PNG_WRITE_gAMA_SUPPORTED\r
+/* Write a gAMA chunk */\r
+#ifdef PNG_FLOATING_POINT_SUPPORTED\r
+void /* PRIVATE */\r
+png_write_gAMA(png_structp png_ptr, double file_gamma)\r
+{\r
+   PNG_gAMA;\r
+   png_uint_32 igamma;\r
+   png_byte buf[4];\r
+\r
+   png_debug(1, "in png_write_gAMA");\r
+\r
+   /* file_gamma is saved in 1/100,000ths */\r
+   igamma = (png_uint_32)(file_gamma * 100000.0 + 0.5);\r
+   png_save_uint_32(buf, igamma);\r
+   png_write_chunk(png_ptr, (png_bytep)png_gAMA, buf, (png_size_t)4);\r
+}\r
+#endif\r
+#ifdef PNG_FIXED_POINT_SUPPORTED\r
+void /* PRIVATE */\r
+png_write_gAMA_fixed(png_structp png_ptr, png_fixed_point file_gamma)\r
+{\r
+   PNG_gAMA;\r
+   png_byte buf[4];\r
+\r
+   png_debug(1, "in png_write_gAMA");\r
+\r
+   /* file_gamma is saved in 1/100,000ths */\r
+   png_save_uint_32(buf, (png_uint_32)file_gamma);\r
+   png_write_chunk(png_ptr, (png_bytep)png_gAMA, buf, (png_size_t)4);\r
+}\r
+#endif\r
+#endif\r
+\r
+#ifdef PNG_WRITE_sRGB_SUPPORTED\r
+/* Write a sRGB chunk */\r
+void /* PRIVATE */\r
+png_write_sRGB(png_structp png_ptr, int srgb_intent)\r
+{\r
+   PNG_sRGB;\r
+   png_byte buf[1];\r
+\r
+   png_debug(1, "in png_write_sRGB");\r
+\r
+   if (srgb_intent >= PNG_sRGB_INTENT_LAST)\r
+         png_warning(png_ptr,\r
+            "Invalid sRGB rendering intent specified");\r
+   buf[0]=(png_byte)srgb_intent;\r
+   png_write_chunk(png_ptr, (png_bytep)png_sRGB, buf, (png_size_t)1);\r
+}\r
+#endif\r
+\r
+#ifdef PNG_WRITE_iCCP_SUPPORTED\r
+/* Write an iCCP chunk */\r
+void /* PRIVATE */\r
+png_write_iCCP(png_structp png_ptr, png_charp name, int compression_type,\r
+   png_charp profile, int profile_len)\r
+{\r
+   PNG_iCCP;\r
+   png_size_t name_len;\r
+   png_charp new_name;\r
+   compression_state comp;\r
+   int embedded_profile_len = 0;\r
+\r
+   png_debug(1, "in png_write_iCCP");\r
+\r
+   comp.num_output_ptr = 0;\r
+   comp.max_output_ptr = 0;\r
+   comp.output_ptr = NULL;\r
+   comp.input = NULL;\r
+   comp.input_len = 0;\r
+\r
+   if ((name_len = png_check_keyword(png_ptr, name,\r
+      &new_name)) == 0)\r
+      return;\r
+\r
+   if (compression_type != PNG_COMPRESSION_TYPE_BASE)\r
+      png_warning(png_ptr, "Unknown compression type in iCCP chunk");\r
+\r
+   if (profile == NULL)\r
+      profile_len = 0;\r
+\r
+   if (profile_len > 3)\r
+      embedded_profile_len =\r
+          ((*( (png_bytep)profile    ))<<24) |\r
+          ((*( (png_bytep)profile + 1))<<16) |\r
+          ((*( (png_bytep)profile + 2))<< 8) |\r
+          ((*( (png_bytep)profile + 3))    );\r
+\r
+   if (embedded_profile_len < 0)\r
+   {\r
+      png_warning(png_ptr,\r
+        "Embedded profile length in iCCP chunk is negative");\r
+      png_free(png_ptr, new_name);\r
+      return;\r
+   }\r
+\r
+   if (profile_len < embedded_profile_len)\r
+   {\r
+      png_warning(png_ptr,\r
+        "Embedded profile length too large in iCCP chunk");\r
+      png_free(png_ptr, new_name);\r
+      return;\r
+   }\r
+\r
+   if (profile_len > embedded_profile_len)\r
+   {\r
+      png_warning(png_ptr,\r
+        "Truncating profile to actual length in iCCP chunk");\r
+      profile_len = embedded_profile_len;\r
+   }\r
+\r
+   if (profile_len)\r
+      profile_len = png_text_compress(png_ptr, profile,\r
+        (png_size_t)profile_len, PNG_COMPRESSION_TYPE_BASE, &comp);\r
+\r
+   /* Make sure we include the NULL after the name and the compression type */\r
+   png_write_chunk_start(png_ptr, (png_bytep)png_iCCP,\r
+          (png_uint_32)(name_len + profile_len + 2));\r
+   new_name[name_len + 1] = 0x00;\r
+   png_write_chunk_data(png_ptr, (png_bytep)new_name,\r
+     (png_size_t)(name_len + 2));\r
+\r
+   if (profile_len)\r
+      png_write_compressed_data_out(png_ptr, &comp);\r
+\r
+   png_write_chunk_end(png_ptr);\r
+   png_free(png_ptr, new_name);\r
+}\r
+#endif\r
+\r
+#ifdef PNG_WRITE_sPLT_SUPPORTED\r
+/* Write a sPLT chunk */\r
+void /* PRIVATE */\r
+png_write_sPLT(png_structp png_ptr, png_sPLT_tp spalette)\r
+{\r
+   PNG_sPLT;\r
+   png_size_t name_len;\r
+   png_charp new_name;\r
+   png_byte entrybuf[10];\r
+   png_size_t entry_size = (spalette->depth == 8 ? 6 : 10);\r
+   png_size_t palette_size = entry_size * spalette->nentries;\r
+   png_sPLT_entryp ep;\r
+#ifndef PNG_POINTER_INDEXING_SUPPORTED\r
+   int i;\r
+#endif\r
+\r
+   png_debug(1, "in png_write_sPLT");\r
+\r
+   if ((name_len = png_check_keyword(png_ptr,spalette->name, &new_name))==0)\r
+      return;\r
+\r
+   /* Make sure we include the NULL after the name */\r
+   png_write_chunk_start(png_ptr, (png_bytep)png_sPLT,\r
+     (png_uint_32)(name_len + 2 + palette_size));\r
+   png_write_chunk_data(png_ptr, (png_bytep)new_name,\r
+     (png_size_t)(name_len + 1));\r
+   png_write_chunk_data(png_ptr, (png_bytep)&spalette->depth, (png_size_t)1);\r
+\r
+   /* Loop through each palette entry, writing appropriately */\r
+#ifdef PNG_POINTER_INDEXING_SUPPORTED\r
+   for (ep = spalette->entries; ep<spalette->entries + spalette->nentries; ep++)\r
+   {\r
+      if (spalette->depth == 8)\r
+      {\r
+          entrybuf[0] = (png_byte)ep->red;\r
+          entrybuf[1] = (png_byte)ep->green;\r
+          entrybuf[2] = (png_byte)ep->blue;\r
+          entrybuf[3] = (png_byte)ep->alpha;\r
+          png_save_uint_16(entrybuf + 4, ep->frequency);\r
+      }\r
+      else\r
+      {\r
+          png_save_uint_16(entrybuf + 0, ep->red);\r
+          png_save_uint_16(entrybuf + 2, ep->green);\r
+          png_save_uint_16(entrybuf + 4, ep->blue);\r
+          png_save_uint_16(entrybuf + 6, ep->alpha);\r
+          png_save_uint_16(entrybuf + 8, ep->frequency);\r
+      }\r
+      png_write_chunk_data(png_ptr, entrybuf, (png_size_t)entry_size);\r
+   }\r
+#else\r
+   ep=spalette->entries;\r
+   for (i=0; i>spalette->nentries; i++)\r
+   {\r
+      if (spalette->depth == 8)\r
+      {\r
+          entrybuf[0] = (png_byte)ep[i].red;\r
+          entrybuf[1] = (png_byte)ep[i].green;\r
+          entrybuf[2] = (png_byte)ep[i].blue;\r
+          entrybuf[3] = (png_byte)ep[i].alpha;\r
+          png_save_uint_16(entrybuf + 4, ep[i].frequency);\r
+      }\r
+      else\r
+      {\r
+          png_save_uint_16(entrybuf + 0, ep[i].red);\r
+          png_save_uint_16(entrybuf + 2, ep[i].green);\r
+          png_save_uint_16(entrybuf + 4, ep[i].blue);\r
+          png_save_uint_16(entrybuf + 6, ep[i].alpha);\r
+          png_save_uint_16(entrybuf + 8, ep[i].frequency);\r
+      }\r
+      png_write_chunk_data(png_ptr, entrybuf, (png_size_t)entry_size);\r
+   }\r
+#endif\r
+\r
+   png_write_chunk_end(png_ptr);\r
+   png_free(png_ptr, new_name);\r
+}\r
+#endif\r
+\r
+#ifdef PNG_WRITE_sBIT_SUPPORTED\r
+/* Write the sBIT chunk */\r
+void /* PRIVATE */\r
+png_write_sBIT(png_structp png_ptr, png_color_8p sbit, int color_type)\r
+{\r
+   PNG_sBIT;\r
+   png_byte buf[4];\r
+   png_size_t size;\r
+\r
+   png_debug(1, "in png_write_sBIT");\r
+\r
+   /* Make sure we don't depend upon the order of PNG_COLOR_8 */\r
+   if (color_type & PNG_COLOR_MASK_COLOR)\r
+   {\r
+      png_byte maxbits;\r
+\r
+      maxbits = (png_byte)(color_type==PNG_COLOR_TYPE_PALETTE ? 8 :\r
+                png_ptr->usr_bit_depth);\r
+      if (sbit->red == 0 || sbit->red > maxbits ||\r
+          sbit->green == 0 || sbit->green > maxbits ||\r
+          sbit->blue == 0 || sbit->blue > maxbits)\r
+      {\r
+         png_warning(png_ptr, "Invalid sBIT depth specified");\r
+         return;\r
+      }\r
+      buf[0] = sbit->red;\r
+      buf[1] = sbit->green;\r
+      buf[2] = sbit->blue;\r
+      size = 3;\r
+   }\r
+   else\r
+   {\r
+      if (sbit->gray == 0 || sbit->gray > png_ptr->usr_bit_depth)\r
+      {\r
+         png_warning(png_ptr, "Invalid sBIT depth specified");\r
+         return;\r
+      }\r
+      buf[0] = sbit->gray;\r
+      size = 1;\r
+   }\r
+\r
+   if (color_type & PNG_COLOR_MASK_ALPHA)\r
+   {\r
+      if (sbit->alpha == 0 || sbit->alpha > png_ptr->usr_bit_depth)\r
+      {\r
+         png_warning(png_ptr, "Invalid sBIT depth specified");\r
+         return;\r
+      }\r
+      buf[size++] = sbit->alpha;\r
+   }\r
+\r
+   png_write_chunk(png_ptr, (png_bytep)png_sBIT, buf, size);\r
+}\r
+#endif\r
+\r
+#ifdef PNG_WRITE_cHRM_SUPPORTED\r
+/* Write the cHRM chunk */\r
+#ifdef PNG_FLOATING_POINT_SUPPORTED\r
+void /* PRIVATE */\r
+png_write_cHRM(png_structp png_ptr, double white_x, double white_y,\r
+   double red_x, double red_y, double green_x, double green_y,\r
+   double blue_x, double blue_y)\r
+{\r
+   PNG_cHRM;\r
+   png_byte buf[32];\r
+\r
+   png_fixed_point int_white_x, int_white_y, int_red_x, int_red_y,\r
+      int_green_x, int_green_y, int_blue_x, int_blue_y;\r
+\r
+   png_debug(1, "in png_write_cHRM");\r
+\r
+   int_white_x = (png_uint_32)(white_x * 100000.0 + 0.5);\r
+   int_white_y = (png_uint_32)(white_y * 100000.0 + 0.5);\r
+   int_red_x   = (png_uint_32)(red_x   * 100000.0 + 0.5);\r
+   int_red_y   = (png_uint_32)(red_y   * 100000.0 + 0.5);\r
+   int_green_x = (png_uint_32)(green_x * 100000.0 + 0.5);\r
+   int_green_y = (png_uint_32)(green_y * 100000.0 + 0.5);\r
+   int_blue_x  = (png_uint_32)(blue_x  * 100000.0 + 0.5);\r
+   int_blue_y  = (png_uint_32)(blue_y  * 100000.0 + 0.5);\r
+\r
+#ifdef PNG_CHECK_cHRM_SUPPORTED\r
+   if (png_check_cHRM_fixed(png_ptr, int_white_x, int_white_y,\r
+      int_red_x, int_red_y, int_green_x, int_green_y, int_blue_x, int_blue_y))\r
+#endif\r
+   {\r
+      /* Each value is saved in 1/100,000ths */\r
+\r
+      png_save_uint_32(buf, int_white_x);\r
+      png_save_uint_32(buf + 4, int_white_y);\r
+\r
+      png_save_uint_32(buf + 8, int_red_x);\r
+      png_save_uint_32(buf + 12, int_red_y);\r
+\r
+      png_save_uint_32(buf + 16, int_green_x);\r
+      png_save_uint_32(buf + 20, int_green_y);\r
+\r
+      png_save_uint_32(buf + 24, int_blue_x);\r
+      png_save_uint_32(buf + 28, int_blue_y);\r
+\r
+      png_write_chunk(png_ptr, (png_bytep)png_cHRM, buf, (png_size_t)32);\r
+   }\r
+}\r
+#endif\r
+#ifdef PNG_FIXED_POINT_SUPPORTED\r
+void /* PRIVATE */\r
+png_write_cHRM_fixed(png_structp png_ptr, png_fixed_point white_x,\r
+   png_fixed_point white_y, png_fixed_point red_x, png_fixed_point red_y,\r
+   png_fixed_point green_x, png_fixed_point green_y, png_fixed_point blue_x,\r
+   png_fixed_point blue_y)\r
+{\r
+   PNG_cHRM;\r
+   png_byte buf[32];\r
+\r
+   png_debug(1, "in png_write_cHRM");\r
+\r
+   /* Each value is saved in 1/100,000ths */\r
+#ifdef PNG_CHECK_cHRM_SUPPORTED\r
+   if (png_check_cHRM_fixed(png_ptr, white_x, white_y, red_x, red_y,\r
+      green_x, green_y, blue_x, blue_y))\r
+#endif\r
+   {\r
+      png_save_uint_32(buf, (png_uint_32)white_x);\r
+      png_save_uint_32(buf + 4, (png_uint_32)white_y);\r
+\r
+      png_save_uint_32(buf + 8, (png_uint_32)red_x);\r
+      png_save_uint_32(buf + 12, (png_uint_32)red_y);\r
+\r
+      png_save_uint_32(buf + 16, (png_uint_32)green_x);\r
+      png_save_uint_32(buf + 20, (png_uint_32)green_y);\r
+\r
+      png_save_uint_32(buf + 24, (png_uint_32)blue_x);\r
+      png_save_uint_32(buf + 28, (png_uint_32)blue_y);\r
+\r
+      png_write_chunk(png_ptr, (png_bytep)png_cHRM, buf, (png_size_t)32);\r
+   }\r
+}\r
+#endif\r
+#endif\r
+\r
+#ifdef PNG_WRITE_tRNS_SUPPORTED\r
+/* Write the tRNS chunk */\r
+void /* PRIVATE */\r
+png_write_tRNS(png_structp png_ptr, png_bytep trans_alpha, png_color_16p tran,\r
+   int num_trans, int color_type)\r
+{\r
+   PNG_tRNS;\r
+   png_byte buf[6];\r
+\r
+   png_debug(1, "in png_write_tRNS");\r
+\r
+   if (color_type == PNG_COLOR_TYPE_PALETTE)\r
+   {\r
+      if (num_trans <= 0 || num_trans > (int)png_ptr->num_palette)\r
+      {\r
+         png_warning(png_ptr, "Invalid number of transparent colors specified");\r
+         return;\r
+      }\r
+      /* Write the chunk out as it is */\r
+      png_write_chunk(png_ptr, (png_bytep)png_tRNS, trans_alpha,\r
+        (png_size_t)num_trans);\r
+   }\r
+   else if (color_type == PNG_COLOR_TYPE_GRAY)\r
+   {\r
+      /* One 16 bit value */\r
+      if (tran->gray >= (1 << png_ptr->bit_depth))\r
+      {\r
+         png_warning(png_ptr,\r
+           "Ignoring attempt to write tRNS chunk out-of-range for bit_depth");\r
+         return;\r
+      }\r
+      png_save_uint_16(buf, tran->gray);\r
+      png_write_chunk(png_ptr, (png_bytep)png_tRNS, buf, (png_size_t)2);\r
+   }\r
+   else if (color_type == PNG_COLOR_TYPE_RGB)\r
+   {\r
+      /* Three 16 bit values */\r
+      png_save_uint_16(buf, tran->red);\r
+      png_save_uint_16(buf + 2, tran->green);\r
+      png_save_uint_16(buf + 4, tran->blue);\r
+      if (png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4]))\r
+      {\r
+         png_warning(png_ptr,\r
+           "Ignoring attempt to write 16-bit tRNS chunk when bit_depth is 8");\r
+         return;\r
+      }\r
+      png_write_chunk(png_ptr, (png_bytep)png_tRNS, buf, (png_size_t)6);\r
+   }\r
+   else\r
+   {\r
+      png_warning(png_ptr, "Can't write tRNS with an alpha channel");\r
+   }\r
+}\r
+#endif\r
+\r
+#ifdef PNG_WRITE_bKGD_SUPPORTED\r
+/* Write the background chunk */\r
+void /* PRIVATE */\r
+png_write_bKGD(png_structp png_ptr, png_color_16p back, int color_type)\r
+{\r
+   PNG_bKGD;\r
+   png_byte buf[6];\r
+\r
+   png_debug(1, "in png_write_bKGD");\r
+\r
+   if (color_type == PNG_COLOR_TYPE_PALETTE)\r
+   {\r
+      if (\r
+#ifdef PNG_MNG_FEATURES_SUPPORTED\r
+          (png_ptr->num_palette ||\r
+          (!(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE))) &&\r
+#endif\r
+         back->index >= png_ptr->num_palette)\r
+      {\r
+         png_warning(png_ptr, "Invalid background palette index");\r
+         return;\r
+      }\r
+      buf[0] = back->index;\r
+      png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)1);\r
+   }\r
+   else if (color_type & PNG_COLOR_MASK_COLOR)\r
+   {\r
+      png_save_uint_16(buf, back->red);\r
+      png_save_uint_16(buf + 2, back->green);\r
+      png_save_uint_16(buf + 4, back->blue);\r
+      if (png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4]))\r
+      {\r
+         png_warning(png_ptr,\r
+           "Ignoring attempt to write 16-bit bKGD chunk when bit_depth is 8");\r
+         return;\r
+      }\r
+      png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)6);\r
+   }\r
+   else\r
+   {\r
+      if (back->gray >= (1 << png_ptr->bit_depth))\r
+      {\r
+         png_warning(png_ptr,\r
+           "Ignoring attempt to write bKGD chunk out-of-range for bit_depth");\r
+         return;\r
+      }\r
+      png_save_uint_16(buf, back->gray);\r
+      png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)2);\r
+   }\r
+}\r
+#endif\r
+\r
+#ifdef PNG_WRITE_hIST_SUPPORTED\r
+/* Write the histogram */\r
+void /* PRIVATE */\r
+png_write_hIST(png_structp png_ptr, png_uint_16p hist, int num_hist)\r
+{\r
+   PNG_hIST;\r
+   int i;\r
+   png_byte buf[3];\r
+\r
+   png_debug(1, "in png_write_hIST");\r
+\r
+   if (num_hist > (int)png_ptr->num_palette)\r
+   {\r
+      png_debug2(3, "num_hist = %d, num_palette = %d", num_hist,\r
+         png_ptr->num_palette);\r
+      png_warning(png_ptr, "Invalid number of histogram entries specified");\r
+      return;\r
+   }\r
+\r
+   png_write_chunk_start(png_ptr, (png_bytep)png_hIST,\r
+     (png_uint_32)(num_hist * 2));\r
+   for (i = 0; i < num_hist; i++)\r
+   {\r
+      png_save_uint_16(buf, hist[i]);\r
+      png_write_chunk_data(png_ptr, buf, (png_size_t)2);\r
+   }\r
+   png_write_chunk_end(png_ptr);\r
+}\r
+#endif\r
+\r
+#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_pCAL_SUPPORTED) || \\r
+    defined(PNG_WRITE_iCCP_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)\r
+/* Check that the tEXt or zTXt keyword is valid per PNG 1.0 specification,\r
+ * and if invalid, correct the keyword rather than discarding the entire\r
+ * chunk.  The PNG 1.0 specification requires keywords 1-79 characters in\r
+ * length, forbids leading or trailing whitespace, multiple internal spaces,\r
+ * and the non-break space (0x80) from ISO 8859-1.  Returns keyword length.\r
+ *\r
+ * The new_key is allocated to hold the corrected keyword and must be freed\r
+ * by the calling routine.  This avoids problems with trying to write to\r
+ * static keywords without having to have duplicate copies of the strings.\r
+ */\r
+png_size_t /* PRIVATE */\r
+png_check_keyword(png_structp png_ptr, png_charp key, png_charpp new_key)\r
+{\r
+   png_size_t key_len;\r
+   png_charp kp, dp;\r
+   int kflag;\r
+   int kwarn=0;\r
+\r
+   png_debug(1, "in png_check_keyword");\r
+\r
+   *new_key = NULL;\r
+\r
+   if (key == NULL || (key_len = png_strlen(key)) == 0)\r
+   {\r
+      png_warning(png_ptr, "zero length keyword");\r
+      return ((png_size_t)0);\r
+   }\r
+\r
+   png_debug1(2, "Keyword to be checked is '%s'", key);\r
+\r
+   *new_key = (png_charp)png_malloc_warn(png_ptr, (png_uint_32)(key_len + 2));\r
+   if (*new_key == NULL)\r
+   {\r
+      png_warning(png_ptr, "Out of memory while procesing keyword");\r
+      return ((png_size_t)0);\r
+   }\r
+\r
+   /* Replace non-printing characters with a blank and print a warning */\r
+   for (kp = key, dp = *new_key; *kp != '\0'; kp++, dp++)\r
+   {\r
+      if ((png_byte)*kp < 0x20 ||\r
+         ((png_byte)*kp > 0x7E && (png_byte)*kp < 0xA1))\r
+      {\r
+#ifdef PNG_STDIO_SUPPORTED\r
+         char msg[40];\r
+\r
+         png_snprintf(msg, 40,\r
+           "invalid keyword character 0x%02X", (png_byte)*kp);\r
+         png_warning(png_ptr, msg);\r
+#else\r
+         png_warning(png_ptr, "invalid character in keyword");\r
+#endif\r
+         *dp = ' ';\r
+      }\r
+      else\r
+      {\r
+         *dp = *kp;\r
+      }\r
+   }\r
+   *dp = '\0';\r
+\r
+   /* Remove any trailing white space. */\r
+   kp = *new_key + key_len - 1;\r
+   if (*kp == ' ')\r
+   {\r
+      png_warning(png_ptr, "trailing spaces removed from keyword");\r
+\r
+      while (*kp == ' ')\r
+      {\r
+         *(kp--) = '\0';\r
+         key_len--;\r
+      }\r
+   }\r
+\r
+   /* Remove any leading white space. */\r
+   kp = *new_key;\r
+   if (*kp == ' ')\r
+   {\r
+      png_warning(png_ptr, "leading spaces removed from keyword");\r
+\r
+      while (*kp == ' ')\r
+      {\r
+         kp++;\r
+         key_len--;\r
+      }\r
+   }\r
+\r
+   png_debug1(2, "Checking for multiple internal spaces in '%s'", kp);\r
+\r
+   /* Remove multiple internal spaces. */\r
+   for (kflag = 0, dp = *new_key; *kp != '\0'; kp++)\r
+   {\r
+      if (*kp == ' ' && kflag == 0)\r
+      {\r
+         *(dp++) = *kp;\r
+         kflag = 1;\r
+      }\r
+      else if (*kp == ' ')\r
+      {\r
+         key_len--;\r
+         kwarn=1;\r
+      }\r
+      else\r
+      {\r
+         *(dp++) = *kp;\r
+         kflag = 0;\r
+      }\r
+   }\r
+   *dp = '\0';\r
+   if (kwarn)\r
+      png_warning(png_ptr, "extra interior spaces removed from keyword");\r
+\r
+   if (key_len == 0)\r
+   {\r
+      png_free(png_ptr, *new_key);\r
+      png_warning(png_ptr, "Zero length keyword");\r
+   }\r
+\r
+   if (key_len > 79)\r
+   {\r
+      png_warning(png_ptr, "keyword length must be 1 - 79 characters");\r
+      (*new_key)[79] = '\0';\r
+      key_len = 79;\r
+   }\r
+\r
+   return (key_len);\r
+}\r
+#endif\r
+\r
+#ifdef PNG_WRITE_tEXt_SUPPORTED\r
+/* Write a tEXt chunk */\r
+void /* PRIVATE */\r
+png_write_tEXt(png_structp png_ptr, png_charp key, png_charp text,\r
+   png_size_t text_len)\r
+{\r
+   PNG_tEXt;\r
+   png_size_t key_len;\r
+   png_charp new_key;\r
+\r
+   png_debug(1, "in png_write_tEXt");\r
+\r
+   if ((key_len = png_check_keyword(png_ptr, key, &new_key))==0)\r
+      return;\r
+\r
+   if (text == NULL || *text == '\0')\r
+      text_len = 0;\r
+   else\r
+      text_len = png_strlen(text);\r
+\r
+   /* Make sure we include the 0 after the key */\r
+   png_write_chunk_start(png_ptr, (png_bytep)png_tEXt,\r
+      (png_uint_32)(key_len + text_len + 1));\r
+   /*\r
+    * We leave it to the application to meet PNG-1.0 requirements on the\r
+    * contents of the text.  PNG-1.0 through PNG-1.2 discourage the use of\r
+    * any non-Latin-1 characters except for NEWLINE.  ISO PNG will forbid them.\r
+    * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.\r
+    */\r
+   png_write_chunk_data(png_ptr, (png_bytep)new_key,\r
+     (png_size_t)(key_len + 1));\r
+   if (text_len)\r
+      png_write_chunk_data(png_ptr, (png_bytep)text, (png_size_t)text_len);\r
+\r
+   png_write_chunk_end(png_ptr);\r
+   png_free(png_ptr, new_key);\r
+}\r
+#endif\r
+\r
+#ifdef PNG_WRITE_zTXt_SUPPORTED\r
+/* Write a compressed text chunk */\r
+void /* PRIVATE */\r
+png_write_zTXt(png_structp png_ptr, png_charp key, png_charp text,\r
+   png_size_t text_len, int compression)\r
+{\r
+   PNG_zTXt;\r
+   png_size_t key_len;\r
+   char buf[1];\r
+   png_charp new_key;\r
+   compression_state comp;\r
+\r
+   png_debug(1, "in png_write_zTXt");\r
+\r
+   comp.num_output_ptr = 0;\r
+   comp.max_output_ptr = 0;\r
+   comp.output_ptr = NULL;\r
+   comp.input = NULL;\r
+   comp.input_len = 0;\r
+\r
+   if ((key_len = png_check_keyword(png_ptr, key, &new_key))==0)\r
+   {\r
+      png_free(png_ptr, new_key);\r
+      return;\r
+   }\r
+\r
+   if (text == NULL || *text == '\0' || compression==PNG_TEXT_COMPRESSION_NONE)\r
+   {\r
+      png_write_tEXt(png_ptr, new_key, text, (png_size_t)0);\r
+      png_free(png_ptr, new_key);\r
+      return;\r
+   }\r
+\r
+   text_len = png_strlen(text);\r
+\r
+   /* Compute the compressed data; do it now for the length */\r
+   text_len = png_text_compress(png_ptr, text, text_len, compression,\r
+       &comp);\r
+\r
+   /* Write start of chunk */\r
+   png_write_chunk_start(png_ptr, (png_bytep)png_zTXt,\r
+     (png_uint_32)(key_len+text_len + 2));\r
+   /* Write key */\r
+   png_write_chunk_data(png_ptr, (png_bytep)new_key,\r
+     (png_size_t)(key_len + 1));\r
+   png_free(png_ptr, new_key);\r
+\r
+   buf[0] = (png_byte)compression;\r
+   /* Write compression */\r
+   png_write_chunk_data(png_ptr, (png_bytep)buf, (png_size_t)1);\r
+   /* Write the compressed data */\r
+   png_write_compressed_data_out(png_ptr, &comp);\r
+\r
+   /* Close the chunk */\r
+   png_write_chunk_end(png_ptr);\r
+}\r
+#endif\r
+\r
+#ifdef PNG_WRITE_iTXt_SUPPORTED\r
+/* Write an iTXt chunk */\r
+void /* PRIVATE */\r
+png_write_iTXt(png_structp png_ptr, int compression, png_charp key,\r
+    png_charp lang, png_charp lang_key, png_charp text)\r
+{\r
+   PNG_iTXt;\r
+   png_size_t lang_len, key_len, lang_key_len, text_len;\r
+   png_charp new_lang;\r
+   png_charp new_key = NULL;\r
+   png_byte cbuf[2];\r
+   compression_state comp;\r
+\r
+   png_debug(1, "in png_write_iTXt");\r
+\r
+   comp.num_output_ptr = 0;\r
+   comp.max_output_ptr = 0;\r
+   comp.output_ptr = NULL;\r
+   comp.input = NULL;\r
+\r
+   if ((key_len = png_check_keyword(png_ptr, key, &new_key))==0)\r
+      return;\r
+\r
+   if ((lang_len = png_check_keyword(png_ptr, lang, &new_lang))==0)\r
+   {\r
+      png_warning(png_ptr, "Empty language field in iTXt chunk");\r
+      new_lang = NULL;\r
+      lang_len = 0;\r
+   }\r
+\r
+   if (lang_key == NULL)\r
+      lang_key_len = 0;\r
+   else\r
+      lang_key_len = png_strlen(lang_key);\r
+\r
+   if (text == NULL)\r
+      text_len = 0;\r
+   else\r
+      text_len = png_strlen(text);\r
+\r
+   /* Compute the compressed data; do it now for the length */\r
+   text_len = png_text_compress(png_ptr, text, text_len, compression-2,\r
+      &comp);\r
+\r
+\r
+   /* Make sure we include the compression flag, the compression byte,\r
+    * and the NULs after the key, lang, and lang_key parts */\r
+\r
+   png_write_chunk_start(png_ptr, (png_bytep)png_iTXt,\r
+          (png_uint_32)(\r
+        5 /* comp byte, comp flag, terminators for key, lang and lang_key */\r
+        + key_len\r
+        + lang_len\r
+        + lang_key_len\r
+        + text_len));\r
+\r
+   /* We leave it to the application to meet PNG-1.0 requirements on the\r
+    * contents of the text.  PNG-1.0 through PNG-1.2 discourage the use of\r
+    * any non-Latin-1 characters except for NEWLINE.  ISO PNG will forbid them.\r
+    * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.\r
+    */\r
+   png_write_chunk_data(png_ptr, (png_bytep)new_key,\r
+     (png_size_t)(key_len + 1));\r
+\r
+   /* Set the compression flag */\r
+   if (compression == PNG_ITXT_COMPRESSION_NONE || \\r
+       compression == PNG_TEXT_COMPRESSION_NONE)\r
+       cbuf[0] = 0;\r
+   else /* compression == PNG_ITXT_COMPRESSION_zTXt */\r
+       cbuf[0] = 1;\r
+   /* Set the compression method */\r
+   cbuf[1] = 0;\r
+   png_write_chunk_data(png_ptr, cbuf, (png_size_t)2);\r
+\r
+   cbuf[0] = 0;\r
+   png_write_chunk_data(png_ptr, (new_lang ? (png_bytep)new_lang : cbuf),\r
+     (png_size_t)(lang_len + 1));\r
+   png_write_chunk_data(png_ptr, (lang_key ? (png_bytep)lang_key : cbuf),\r
+     (png_size_t)(lang_key_len + 1));\r
+   png_write_compressed_data_out(png_ptr, &comp);\r
+\r
+   png_write_chunk_end(png_ptr);\r
+   png_free(png_ptr, new_key);\r
+   png_free(png_ptr, new_lang);\r
+}\r
+#endif\r
+\r
+#ifdef PNG_WRITE_oFFs_SUPPORTED\r
+/* Write the oFFs chunk */\r
+void /* PRIVATE */\r
+png_write_oFFs(png_structp png_ptr, png_int_32 x_offset, png_int_32 y_offset,\r
+   int unit_type)\r
+{\r
+   PNG_oFFs;\r
+   png_byte buf[9];\r
+\r
+   png_debug(1, "in png_write_oFFs");\r
+\r
+   if (unit_type >= PNG_OFFSET_LAST)\r
+      png_warning(png_ptr, "Unrecognized unit type for oFFs chunk");\r
+\r
+   png_save_int_32(buf, x_offset);\r
+   png_save_int_32(buf + 4, y_offset);\r
+   buf[8] = (png_byte)unit_type;\r
+\r
+   png_write_chunk(png_ptr, (png_bytep)png_oFFs, buf, (png_size_t)9);\r
+}\r
+#endif\r
+#ifdef PNG_WRITE_pCAL_SUPPORTED\r
+/* Write the pCAL chunk (described in the PNG extensions document) */\r
+void /* PRIVATE */\r
+png_write_pCAL(png_structp png_ptr, png_charp purpose, png_int_32 X0,\r
+   png_int_32 X1, int type, int nparams, png_charp units, png_charpp params)\r
+{\r
+   PNG_pCAL;\r
+   png_size_t purpose_len, units_len, total_len;\r
+   png_uint_32p params_len;\r
+   png_byte buf[10];\r
+   png_charp new_purpose;\r
+   int i;\r
+\r
+   png_debug1(1, "in png_write_pCAL (%d parameters)", nparams);\r
+\r
+   if (type >= PNG_EQUATION_LAST)\r
+      png_warning(png_ptr, "Unrecognized equation type for pCAL chunk");\r
+\r
+   purpose_len = png_check_keyword(png_ptr, purpose, &new_purpose) + 1;\r
+   png_debug1(3, "pCAL purpose length = %d", (int)purpose_len);\r
+   units_len = png_strlen(units) + (nparams == 0 ? 0 : 1);\r
+   png_debug1(3, "pCAL units length = %d", (int)units_len);\r
+   total_len = purpose_len + units_len + 10;\r
+\r
+   params_len = (png_uint_32p)png_malloc(png_ptr,\r
+      (png_alloc_size_t)(nparams * png_sizeof(png_uint_32)));\r
+\r
+   /* Find the length of each parameter, making sure we don't count the\r
+      null terminator for the last parameter. */\r
+   for (i = 0; i < nparams; i++)\r
+   {\r
+      params_len[i] = png_strlen(params[i]) + (i == nparams - 1 ? 0 : 1);\r
+      png_debug2(3, "pCAL parameter %d length = %lu", i,\r
+        (unsigned long) params_len[i]);\r
+      total_len += (png_size_t)params_len[i];\r
+   }\r
+\r
+   png_debug1(3, "pCAL total length = %d", (int)total_len);\r
+   png_write_chunk_start(png_ptr, (png_bytep)png_pCAL, (png_uint_32)total_len);\r
+   png_write_chunk_data(png_ptr, (png_bytep)new_purpose,\r
+     (png_size_t)purpose_len);\r
+   png_save_int_32(buf, X0);\r
+   png_save_int_32(buf + 4, X1);\r
+   buf[8] = (png_byte)type;\r
+   buf[9] = (png_byte)nparams;\r
+   png_write_chunk_data(png_ptr, buf, (png_size_t)10);\r
+   png_write_chunk_data(png_ptr, (png_bytep)units, (png_size_t)units_len);\r
+\r
+   png_free(png_ptr, new_purpose);\r
+\r
+   for (i = 0; i < nparams; i++)\r
+   {\r
+      png_write_chunk_data(png_ptr, (png_bytep)params[i],\r
+         (png_size_t)params_len[i]);\r
+   }\r
+\r
+   png_free(png_ptr, params_len);\r
+   png_write_chunk_end(png_ptr);\r
+}\r
+#endif\r
+\r
+#ifdef PNG_WRITE_sCAL_SUPPORTED\r
+/* Write the sCAL chunk */\r
+#if defined(PNG_FLOATING_POINT_SUPPORTED) && defined(PNG_STDIO_SUPPORTED)\r
+void /* PRIVATE */\r
+png_write_sCAL(png_structp png_ptr, int unit, double width, double height)\r
+{\r
+   PNG_sCAL;\r
+   char buf[64];\r
+   png_size_t total_len;\r
+\r
+   png_debug(1, "in png_write_sCAL");\r
+\r
+   buf[0] = (char)unit;\r
+   png_snprintf(buf + 1, 63, "%12.12e", width);\r
+   total_len = 1 + png_strlen(buf + 1) + 1;\r
+   png_snprintf(buf + total_len, 64-total_len, "%12.12e", height);\r
+   total_len += png_strlen(buf + total_len);\r
+\r
+   png_debug1(3, "sCAL total length = %u", (unsigned int)total_len);\r
+   png_write_chunk(png_ptr, (png_bytep)png_sCAL, (png_bytep)buf, total_len);\r
+}\r
+#else\r
+#ifdef PNG_FIXED_POINT_SUPPORTED\r
+void /* PRIVATE */\r
+png_write_sCAL_s(png_structp png_ptr, int unit, png_charp width,\r
+   png_charp height)\r
+{\r
+   PNG_sCAL;\r
+   png_byte buf[64];\r
+   png_size_t wlen, hlen, total_len;\r
+\r
+   png_debug(1, "in png_write_sCAL_s");\r
+\r
+   wlen = png_strlen(width);\r
+   hlen = png_strlen(height);\r
+   total_len = wlen + hlen + 2;\r
+   if (total_len > 64)\r
+   {\r
+      png_warning(png_ptr, "Can't write sCAL (buffer too small)");\r
+      return;\r
+   }\r
+\r
+   buf[0] = (png_byte)unit;\r
+   png_memcpy(buf + 1, width, wlen + 1);      /* Append the '\0' here */\r
+   png_memcpy(buf + wlen + 2, height, hlen);  /* Do NOT append the '\0' here */\r
+\r
+   png_debug1(3, "sCAL total length = %u", (unsigned int)total_len);\r
+   png_write_chunk(png_ptr, (png_bytep)png_sCAL, buf, total_len);\r
+}\r
+#endif\r
+#endif\r
+#endif\r
+\r
+#ifdef PNG_WRITE_pHYs_SUPPORTED\r
+/* Write the pHYs chunk */\r
+void /* PRIVATE */\r
+png_write_pHYs(png_structp png_ptr, png_uint_32 x_pixels_per_unit,\r
+   png_uint_32 y_pixels_per_unit,\r
+   int unit_type)\r
+{\r
+   PNG_pHYs;\r
+   png_byte buf[9];\r
+\r
+   png_debug(1, "in png_write_pHYs");\r
+\r
+   if (unit_type >= PNG_RESOLUTION_LAST)\r
+      png_warning(png_ptr, "Unrecognized unit type for pHYs chunk");\r
+\r
+   png_save_uint_32(buf, x_pixels_per_unit);\r
+   png_save_uint_32(buf + 4, y_pixels_per_unit);\r
+   buf[8] = (png_byte)unit_type;\r
+\r
+   png_write_chunk(png_ptr, (png_bytep)png_pHYs, buf, (png_size_t)9);\r
+}\r
+#endif\r
+\r
+#ifdef PNG_WRITE_tIME_SUPPORTED\r
+/* Write the tIME chunk.  Use either png_convert_from_struct_tm()\r
+ * or png_convert_from_time_t(), or fill in the structure yourself.\r
+ */\r
+void /* PRIVATE */\r
+png_write_tIME(png_structp png_ptr, png_timep mod_time)\r
+{\r
+   PNG_tIME;\r
+   png_byte buf[7];\r
+\r
+   png_debug(1, "in png_write_tIME");\r
+\r
+   if (mod_time->month  > 12 || mod_time->month  < 1 ||\r
+       mod_time->day    > 31 || mod_time->day    < 1 ||\r
+       mod_time->hour   > 23 || mod_time->second > 60)\r
+   {\r
+      png_warning(png_ptr, "Invalid time specified for tIME chunk");\r
+      return;\r
+   }\r
+\r
+   png_save_uint_16(buf, mod_time->year);\r
+   buf[2] = mod_time->month;\r
+   buf[3] = mod_time->day;\r
+   buf[4] = mod_time->hour;\r
+   buf[5] = mod_time->minute;\r
+   buf[6] = mod_time->second;\r
+\r
+   png_write_chunk(png_ptr, (png_bytep)png_tIME, buf, (png_size_t)7);\r
+}\r
+#endif\r
+\r
+/* Initializes the row writing capability of libpng */\r
+void /* PRIVATE */\r
+png_write_start_row(png_structp png_ptr)\r
+{\r
+#ifdef PNG_WRITE_INTERLACING_SUPPORTED\r
+   /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */\r
+\r
+   /* Start of interlace block */\r
+   int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};\r
+\r
+   /* Offset to next interlace block */\r
+   int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};\r
+\r
+   /* Start of interlace block in the y direction */\r
+   int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};\r
+\r
+   /* Offset to next interlace block in the y direction */\r
+   int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};\r
+#endif\r
+\r
+   png_size_t buf_size;\r
+\r
+   png_debug(1, "in png_write_start_row");\r
+\r
+   buf_size = (png_size_t)(PNG_ROWBYTES(\r
+      png_ptr->usr_channels*png_ptr->usr_bit_depth, png_ptr->width) + 1);\r
+\r
+   /* Set up row buffer */\r
+   png_ptr->row_buf = (png_bytep)png_malloc(png_ptr,\r
+     (png_alloc_size_t)buf_size);\r
+   png_ptr->row_buf[0] = PNG_FILTER_VALUE_NONE;\r
+\r
+#ifdef PNG_WRITE_FILTER_SUPPORTED\r
+   /* Set up filtering buffer, if using this filter */\r
+   if (png_ptr->do_filter & PNG_FILTER_SUB)\r
+   {\r
+      png_ptr->sub_row = (png_bytep)png_malloc(png_ptr,\r
+         (png_alloc_size_t)(png_ptr->rowbytes + 1));\r
+      png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;\r
+   }\r
+\r
+   /* We only need to keep the previous row if we are using one of these. */\r
+   if (png_ptr->do_filter & (PNG_FILTER_AVG | PNG_FILTER_UP | PNG_FILTER_PAETH))\r
+   {\r
+      /* Set up previous row buffer */\r
+      png_ptr->prev_row = (png_bytep)png_calloc(png_ptr,\r
+         (png_alloc_size_t)buf_size);\r
+\r
+      if (png_ptr->do_filter & PNG_FILTER_UP)\r
+      {\r
+         png_ptr->up_row = (png_bytep)png_malloc(png_ptr,\r
+            (png_size_t)(png_ptr->rowbytes + 1));\r
+         png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;\r
+      }\r
+\r
+      if (png_ptr->do_filter & PNG_FILTER_AVG)\r
+      {\r
+         png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,\r
+            (png_alloc_size_t)(png_ptr->rowbytes + 1));\r
+         png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;\r
+      }\r
+\r
+      if (png_ptr->do_filter & PNG_FILTER_PAETH)\r
+      {\r
+         png_ptr->paeth_row = (png_bytep)png_malloc(png_ptr,\r
+            (png_size_t)(png_ptr->rowbytes + 1));\r
+         png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;\r
+      }\r
+   }\r
+#endif /* PNG_WRITE_FILTER_SUPPORTED */\r
+\r
+#ifdef PNG_WRITE_INTERLACING_SUPPORTED\r
+   /* If interlaced, we need to set up width and height of pass */\r
+   if (png_ptr->interlaced)\r
+   {\r
+      if (!(png_ptr->transformations & PNG_INTERLACE))\r
+      {\r
+         png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -\r
+            png_pass_ystart[0]) / png_pass_yinc[0];\r
+         png_ptr->usr_width = (png_ptr->width + png_pass_inc[0] - 1 -\r
+            png_pass_start[0]) / png_pass_inc[0];\r
+      }\r
+      else\r
+      {\r
+         png_ptr->num_rows = png_ptr->height;\r
+         png_ptr->usr_width = png_ptr->width;\r
+      }\r
+   }\r
+   else\r
+#endif\r
+   {\r
+      png_ptr->num_rows = png_ptr->height;\r
+      png_ptr->usr_width = png_ptr->width;\r
+   }\r
+   png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;\r
+   png_ptr->zstream.next_out = png_ptr->zbuf;\r
+}\r
+\r
+/* Internal use only.  Called when finished processing a row of data. */\r
+void /* PRIVATE */\r
+png_write_finish_row(png_structp png_ptr)\r
+{\r
+#ifdef PNG_WRITE_INTERLACING_SUPPORTED\r
+   /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */\r
+\r
+   /* Start of interlace block */\r
+   int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};\r
+\r
+   /* Offset to next interlace block */\r
+   int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};\r
+\r
+   /* Start of interlace block in the y direction */\r
+   int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};\r
+\r
+   /* Offset to next interlace block in the y direction */\r
+   int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};\r
+#endif\r
+\r
+   int ret;\r
+\r
+   png_debug(1, "in png_write_finish_row");\r
+\r
+   /* Next row */\r
+   png_ptr->row_number++;\r
+\r
+   /* See if we are done */\r
+   if (png_ptr->row_number < png_ptr->num_rows)\r
+      return;\r
+\r
+#ifdef PNG_WRITE_INTERLACING_SUPPORTED\r
+   /* If interlaced, go to next pass */\r
+   if (png_ptr->interlaced)\r
+   {\r
+      png_ptr->row_number = 0;\r
+      if (png_ptr->transformations & PNG_INTERLACE)\r
+      {\r
+         png_ptr->pass++;\r
+      }\r
+      else\r
+      {\r
+         /* Loop until we find a non-zero width or height pass */\r
+         do\r
+         {\r
+            png_ptr->pass++;\r
+            if (png_ptr->pass >= 7)\r
+               break;\r
+            png_ptr->usr_width = (png_ptr->width +\r
+               png_pass_inc[png_ptr->pass] - 1 -\r
+               png_pass_start[png_ptr->pass]) /\r
+               png_pass_inc[png_ptr->pass];\r
+            png_ptr->num_rows = (png_ptr->height +\r
+               png_pass_yinc[png_ptr->pass] - 1 -\r
+               png_pass_ystart[png_ptr->pass]) /\r
+               png_pass_yinc[png_ptr->pass];\r
+            if (png_ptr->transformations & PNG_INTERLACE)\r
+               break;\r
+         } while (png_ptr->usr_width == 0 || png_ptr->num_rows == 0);\r
+\r
+      }\r
+\r
+      /* Reset the row above the image for the next pass */\r
+      if (png_ptr->pass < 7)\r
+      {\r
+         if (png_ptr->prev_row != NULL)\r
+            png_memset(png_ptr->prev_row, 0,\r
+               (png_size_t)(PNG_ROWBYTES(png_ptr->usr_channels*\r
+               png_ptr->usr_bit_depth, png_ptr->width)) + 1);\r
+         return;\r
+      }\r
+   }\r
+#endif\r
+\r
+   /* If we get here, we've just written the last row, so we need\r
+      to flush the compressor */\r
+   do\r
+   {\r
+      /* Tell the compressor we are done */\r
+      ret = deflate(&png_ptr->zstream, Z_FINISH);\r
+      /* Check for an error */\r
+      if (ret == Z_OK)\r
+      {\r
+         /* Check to see if we need more room */\r
+         if (!(png_ptr->zstream.avail_out))\r
+         {\r
+            png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);\r
+            png_ptr->zstream.next_out = png_ptr->zbuf;\r
+            png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;\r
+         }\r
+      }\r
+      else if (ret != Z_STREAM_END)\r
+      {\r
+         if (png_ptr->zstream.msg != NULL)\r
+            png_error(png_ptr, png_ptr->zstream.msg);\r
+         else\r
+            png_error(png_ptr, "zlib error");\r
+      }\r
+   } while (ret != Z_STREAM_END);\r
+\r
+   /* Write any extra space */\r
+   if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)\r
+   {\r
+      png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size -\r
+         png_ptr->zstream.avail_out);\r
+   }\r
+\r
+   deflateReset(&png_ptr->zstream);\r
+   png_ptr->zstream.data_type = Z_BINARY;\r
+}\r
+\r
+#ifdef PNG_WRITE_INTERLACING_SUPPORTED\r
+/* Pick out the correct pixels for the interlace pass.\r
+ * The basic idea here is to go through the row with a source\r
+ * pointer and a destination pointer (sp and dp), and copy the\r
+ * correct pixels for the pass.  As the row gets compacted,\r
+ * sp will always be >= dp, so we should never overwrite anything.\r
+ * See the default: case for the easiest code to understand.\r
+ */\r
+void /* PRIVATE */\r
+png_do_write_interlace(png_row_infop row_info, png_bytep row, int pass)\r
+{\r
+   /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */\r
+\r
+   /* Start of interlace block */\r
+   int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};\r
+\r
+   /* Offset to next interlace block */\r
+   int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};\r
+\r
+   png_debug(1, "in png_do_write_interlace");\r
+\r
+   /* We don't have to do anything on the last pass (6) */\r
+   if (pass < 6)\r
+   {\r
+      /* Each pixel depth is handled separately */\r
+      switch (row_info->pixel_depth)\r
+      {\r
+         case 1:\r
+         {\r
+            png_bytep sp;\r
+            png_bytep dp;\r
+            int shift;\r
+            int d;\r
+            int value;\r
+            png_uint_32 i;\r
+            png_uint_32 row_width = row_info->width;\r
+\r
+            dp = row;\r
+            d = 0;\r
+            shift = 7;\r
+            for (i = png_pass_start[pass]; i < row_width;\r
+               i += png_pass_inc[pass])\r
+            {\r
+               sp = row + (png_size_t)(i >> 3);\r
+               value = (int)(*sp >> (7 - (int)(i & 0x07))) & 0x01;\r
+               d |= (value << shift);\r
+\r
+               if (shift == 0)\r
+               {\r
+                  shift = 7;\r
+                  *dp++ = (png_byte)d;\r
+                  d = 0;\r
+               }\r
+               else\r
+                  shift--;\r
+\r
+            }\r
+            if (shift != 7)\r
+               *dp = (png_byte)d;\r
+            break;\r
+         }\r
+         case 2:\r
+         {\r
+            png_bytep sp;\r
+            png_bytep dp;\r
+            int shift;\r
+            int d;\r
+            int value;\r
+            png_uint_32 i;\r
+            png_uint_32 row_width = row_info->width;\r
+\r
+            dp = row;\r
+            shift = 6;\r
+            d = 0;\r
+            for (i = png_pass_start[pass]; i < row_width;\r
+               i += png_pass_inc[pass])\r
+            {\r
+               sp = row + (png_size_t)(i >> 2);\r
+               value = (*sp >> ((3 - (int)(i & 0x03)) << 1)) & 0x03;\r
+               d |= (value << shift);\r
+\r
+               if (shift == 0)\r
+               {\r
+                  shift = 6;\r
+                  *dp++ = (png_byte)d;\r
+                  d = 0;\r
+               }\r
+               else\r
+                  shift -= 2;\r
+            }\r
+            if (shift != 6)\r
+                   *dp = (png_byte)d;\r
+            break;\r
+         }\r
+         case 4:\r
+         {\r
+            png_bytep sp;\r
+            png_bytep dp;\r
+            int shift;\r
+            int d;\r
+            int value;\r
+            png_uint_32 i;\r
+            png_uint_32 row_width = row_info->width;\r
+\r
+            dp = row;\r
+            shift = 4;\r
+            d = 0;\r
+            for (i = png_pass_start[pass]; i < row_width;\r
+               i += png_pass_inc[pass])\r
+            {\r
+               sp = row + (png_size_t)(i >> 1);\r
+               value = (*sp >> ((1 - (int)(i & 0x01)) << 2)) & 0x0f;\r
+               d |= (value << shift);\r
+\r
+               if (shift == 0)\r
+               {\r
+                  shift = 4;\r
+                  *dp++ = (png_byte)d;\r
+                  d = 0;\r
+               }\r
+               else\r
+                  shift -= 4;\r
+            }\r
+            if (shift != 4)\r
+               *dp = (png_byte)d;\r
+            break;\r
+         }\r
+         default:\r
+         {\r
+            png_bytep sp;\r
+            png_bytep dp;\r
+            png_uint_32 i;\r
+            png_uint_32 row_width = row_info->width;\r
+            png_size_t pixel_bytes;\r
+\r
+            /* Start at the beginning */\r
+            dp = row;\r
+            /* Find out how many bytes each pixel takes up */\r
+            pixel_bytes = (row_info->pixel_depth >> 3);\r
+            /* Loop through the row, only looking at the pixels that\r
+               matter */\r
+            for (i = png_pass_start[pass]; i < row_width;\r
+               i += png_pass_inc[pass])\r
+            {\r
+               /* Find out where the original pixel is */\r
+               sp = row + (png_size_t)i * pixel_bytes;\r
+               /* Move the pixel */\r
+               if (dp != sp)\r
+                  png_memcpy(dp, sp, pixel_bytes);\r
+               /* Next pixel */\r
+               dp += pixel_bytes;\r
+            }\r
+            break;\r
+         }\r
+      }\r
+      /* Set new row width */\r
+      row_info->width = (row_info->width +\r
+         png_pass_inc[pass] - 1 -\r
+         png_pass_start[pass]) /\r
+         png_pass_inc[pass];\r
+         row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,\r
+            row_info->width);\r
+   }\r
+}\r
+#endif\r
+\r
+/* This filters the row, chooses which filter to use, if it has not already\r
+ * been specified by the application, and then writes the row out with the\r
+ * chosen filter.\r
+ */\r
+#define PNG_MAXSUM (((png_uint_32)(-1)) >> 1)\r
+#define PNG_HISHIFT 10\r
+#define PNG_LOMASK ((png_uint_32)0xffffL)\r
+#define PNG_HIMASK ((png_uint_32)(~PNG_LOMASK >> PNG_HISHIFT))\r
+void /* PRIVATE */\r
+png_write_find_filter(png_structp png_ptr, png_row_infop row_info)\r
+{\r
+   png_bytep best_row;\r
+#ifdef PNG_WRITE_FILTER_SUPPORTED\r
+   png_bytep prev_row, row_buf;\r
+   png_uint_32 mins, bpp;\r
+   png_byte filter_to_do = png_ptr->do_filter;\r
+   png_uint_32 row_bytes = row_info->rowbytes;\r
+#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED\r
+   int num_p_filters = (int)png_ptr->num_prev_filters;\r
+#endif\r
+\r
+   png_debug(1, "in png_write_find_filter");\r
+\r
+#ifndef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED\r
+  if (png_ptr->row_number == 0 && filter_to_do == PNG_ALL_FILTERS)\r
+  {\r
+      /* These will never be selected so we need not test them. */\r
+      filter_to_do &= ~(PNG_FILTER_UP | PNG_FILTER_PAETH);\r
+  }\r
+#endif\r
+\r
+   /* Find out how many bytes offset each pixel is */\r
+   bpp = (row_info->pixel_depth + 7) >> 3;\r
+\r
+   prev_row = png_ptr->prev_row;\r
+#endif\r
+   best_row = png_ptr->row_buf;\r
+#ifdef PNG_WRITE_FILTER_SUPPORTED\r
+   row_buf = best_row;\r
+   mins = PNG_MAXSUM;\r
+\r
+   /* The prediction method we use is to find which method provides the\r
+    * smallest value when summing the absolute values of the distances\r
+    * from zero, using anything >= 128 as negative numbers.  This is known\r
+    * as the "minimum sum of absolute differences" heuristic.  Other\r
+    * heuristics are the "weighted minimum sum of absolute differences"\r
+    * (experimental and can in theory improve compression), and the "zlib\r
+    * predictive" method (not implemented yet), which does test compressions\r
+    * of lines using different filter methods, and then chooses the\r
+    * (series of) filter(s) that give minimum compressed data size (VERY\r
+    * computationally expensive).\r
+    *\r
+    * GRR 980525:  consider also\r
+    *   (1) minimum sum of absolute differences from running average (i.e.,\r
+    *       keep running sum of non-absolute differences & count of bytes)\r
+    *       [track dispersion, too?  restart average if dispersion too large?]\r
+    *  (1b) minimum sum of absolute differences from sliding average, probably\r
+    *       with window size <= deflate window (usually 32K)\r
+    *   (2) minimum sum of squared differences from zero or running average\r
+    *       (i.e., ~ root-mean-square approach)\r
+    */\r
+\r
+\r
+   /* We don't need to test the 'no filter' case if this is the only filter\r
+    * that has been chosen, as it doesn't actually do anything to the data.\r
+    */\r
+   if ((filter_to_do & PNG_FILTER_NONE) &&\r
+       filter_to_do != PNG_FILTER_NONE)\r
+   {\r
+      png_bytep rp;\r
+      png_uint_32 sum = 0;\r
+      png_uint_32 i;\r
+      int v;\r
+\r
+      for (i = 0, rp = row_buf + 1; i < row_bytes; i++, rp++)\r
+      {\r
+         v = *rp;\r
+         sum += (v < 128) ? v : 256 - v;\r
+      }\r
+\r
+#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED\r
+      if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)\r
+      {\r
+         png_uint_32 sumhi, sumlo;\r
+         int j;\r
+         sumlo = sum & PNG_LOMASK;\r
+         sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; /* Gives us some footroom */\r
+\r
+         /* Reduce the sum if we match any of the previous rows */\r
+         for (j = 0; j < num_p_filters; j++)\r
+         {\r
+            if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)\r
+            {\r
+               sumlo = (sumlo * png_ptr->filter_weights[j]) >>\r
+                  PNG_WEIGHT_SHIFT;\r
+               sumhi = (sumhi * png_ptr->filter_weights[j]) >>\r
+                  PNG_WEIGHT_SHIFT;\r
+            }\r
+         }\r
+\r
+         /* Factor in the cost of this filter (this is here for completeness,\r
+          * but it makes no sense to have a "cost" for the NONE filter, as\r
+          * it has the minimum possible computational cost - none).\r
+          */\r
+         sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>\r
+            PNG_COST_SHIFT;\r
+         sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>\r
+            PNG_COST_SHIFT;\r
+\r
+         if (sumhi > PNG_HIMASK)\r
+            sum = PNG_MAXSUM;\r
+         else\r
+            sum = (sumhi << PNG_HISHIFT) + sumlo;\r
+      }\r
+#endif\r
+      mins = sum;\r
+   }\r
+\r
+   /* Sub filter */\r
+   if (filter_to_do == PNG_FILTER_SUB)\r
+   /* It's the only filter so no testing is needed */\r
+   {\r
+      png_bytep rp, lp, dp;\r
+      png_uint_32 i;\r
+      for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;\r
+           i++, rp++, dp++)\r
+      {\r
+         *dp = *rp;\r
+      }\r
+      for (lp = row_buf + 1; i < row_bytes;\r
+         i++, rp++, lp++, dp++)\r
+      {\r
+         *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);\r
+      }\r
+      best_row = png_ptr->sub_row;\r
+   }\r
+\r
+   else if (filter_to_do & PNG_FILTER_SUB)\r
+   {\r
+      png_bytep rp, dp, lp;\r
+      png_uint_32 sum = 0, lmins = mins;\r
+      png_uint_32 i;\r
+      int v;\r
+\r
+#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED\r
+      /* We temporarily increase the "minimum sum" by the factor we\r
+       * would reduce the sum of this filter, so that we can do the\r
+       * early exit comparison without scaling the sum each time.\r
+       */\r
+      if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)\r
+      {\r
+         int j;\r
+         png_uint_32 lmhi, lmlo;\r
+         lmlo = lmins & PNG_LOMASK;\r
+         lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;\r
+\r
+         for (j = 0; j < num_p_filters; j++)\r
+         {\r
+            if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)\r
+            {\r
+               lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>\r
+                  PNG_WEIGHT_SHIFT;\r
+               lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>\r
+                  PNG_WEIGHT_SHIFT;\r
+            }\r
+         }\r
+\r
+         lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>\r
+            PNG_COST_SHIFT;\r
+         lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>\r
+            PNG_COST_SHIFT;\r
+\r
+         if (lmhi > PNG_HIMASK)\r
+            lmins = PNG_MAXSUM;\r
+         else\r
+            lmins = (lmhi << PNG_HISHIFT) + lmlo;\r
+      }\r
+#endif\r
+\r
+      for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;\r
+           i++, rp++, dp++)\r
+      {\r
+         v = *dp = *rp;\r
+\r
+         sum += (v < 128) ? v : 256 - v;\r
+      }\r
+      for (lp = row_buf + 1; i < row_bytes;\r
+         i++, rp++, lp++, dp++)\r
+      {\r
+         v = *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);\r
+\r
+         sum += (v < 128) ? v : 256 - v;\r
+\r
+         if (sum > lmins)  /* We are already worse, don't continue. */\r
+            break;\r
+      }\r
+\r
+#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED\r
+      if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)\r
+      {\r
+         int j;\r
+         png_uint_32 sumhi, sumlo;\r
+         sumlo = sum & PNG_LOMASK;\r
+         sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;\r
+\r
+         for (j = 0; j < num_p_filters; j++)\r
+         {\r
+            if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)\r
+            {\r
+               sumlo = (sumlo * png_ptr->inv_filter_weights[j]) >>\r
+                  PNG_WEIGHT_SHIFT;\r
+               sumhi = (sumhi * png_ptr->inv_filter_weights[j]) >>\r
+                  PNG_WEIGHT_SHIFT;\r
+            }\r
+         }\r
+\r
+         sumlo = (sumlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>\r
+            PNG_COST_SHIFT;\r
+         sumhi = (sumhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>\r
+            PNG_COST_SHIFT;\r
+\r
+         if (sumhi > PNG_HIMASK)\r
+            sum = PNG_MAXSUM;\r
+         else\r
+            sum = (sumhi << PNG_HISHIFT) + sumlo;\r
+      }\r
+#endif\r
+\r
+      if (sum < mins)\r
+      {\r
+         mins = sum;\r
+         best_row = png_ptr->sub_row;\r
+      }\r
+   }\r
+\r
+   /* Up filter */\r
+   if (filter_to_do == PNG_FILTER_UP)\r
+   {\r
+      png_bytep rp, dp, pp;\r
+      png_uint_32 i;\r
+\r
+      for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,\r
+           pp = prev_row + 1; i < row_bytes;\r
+           i++, rp++, pp++, dp++)\r
+      {\r
+         *dp = (png_byte)(((int)*rp - (int)*pp) & 0xff);\r
+      }\r
+      best_row = png_ptr->up_row;\r
+   }\r
+\r
+   else if (filter_to_do & PNG_FILTER_UP)\r
+   {\r
+      png_bytep rp, dp, pp;\r
+      png_uint_32 sum = 0, lmins = mins;\r
+      png_uint_32 i;\r
+      int v;\r
+\r
+\r
+#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED\r
+      if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)\r
+      {\r
+         int j;\r
+         png_uint_32 lmhi, lmlo;\r
+         lmlo = lmins & PNG_LOMASK;\r
+         lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;\r
+\r
+         for (j = 0; j < num_p_filters; j++)\r
+         {\r
+            if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)\r
+            {\r
+               lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>\r
+                  PNG_WEIGHT_SHIFT;\r
+               lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>\r
+                  PNG_WEIGHT_SHIFT;\r
+            }\r
+         }\r
+\r
+         lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>\r
+            PNG_COST_SHIFT;\r
+         lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>\r
+            PNG_COST_SHIFT;\r
+\r
+         if (lmhi > PNG_HIMASK)\r
+            lmins = PNG_MAXSUM;\r
+         else\r
+            lmins = (lmhi << PNG_HISHIFT) + lmlo;\r
+      }\r
+#endif\r
+\r
+      for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,\r
+           pp = prev_row + 1; i < row_bytes; i++)\r
+      {\r
+         v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);\r
+\r
+         sum += (v < 128) ? v : 256 - v;\r
+\r
+         if (sum > lmins)  /* We are already worse, don't continue. */\r
+            break;\r
+      }\r
+\r
+#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED\r
+      if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)\r
+      {\r
+         int j;\r
+         png_uint_32 sumhi, sumlo;\r
+         sumlo = sum & PNG_LOMASK;\r
+         sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;\r
+\r
+         for (j = 0; j < num_p_filters; j++)\r
+         {\r
+            if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)\r
+            {\r
+               sumlo = (sumlo * png_ptr->filter_weights[j]) >>\r
+                  PNG_WEIGHT_SHIFT;\r
+               sumhi = (sumhi * png_ptr->filter_weights[j]) >>\r
+                  PNG_WEIGHT_SHIFT;\r
+            }\r
+         }\r
+\r
+         sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>\r
+            PNG_COST_SHIFT;\r
+         sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>\r
+            PNG_COST_SHIFT;\r
+\r
+         if (sumhi > PNG_HIMASK)\r
+            sum = PNG_MAXSUM;\r
+         else\r
+            sum = (sumhi << PNG_HISHIFT) + sumlo;\r
+      }\r
+#endif\r
+\r
+      if (sum < mins)\r
+      {\r
+         mins = sum;\r
+         best_row = png_ptr->up_row;\r
+      }\r
+   }\r
+\r
+   /* Avg filter */\r
+   if (filter_to_do == PNG_FILTER_AVG)\r
+   {\r
+      png_bytep rp, dp, pp, lp;\r
+      png_uint_32 i;\r
+      for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,\r
+           pp = prev_row + 1; i < bpp; i++)\r
+      {\r
+         *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);\r
+      }\r
+      for (lp = row_buf + 1; i < row_bytes; i++)\r
+      {\r
+         *dp++ = (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2))\r
+                 & 0xff);\r
+      }\r
+      best_row = png_ptr->avg_row;\r
+   }\r
+\r
+   else if (filter_to_do & PNG_FILTER_AVG)\r
+   {\r
+      png_bytep rp, dp, pp, lp;\r
+      png_uint_32 sum = 0, lmins = mins;\r
+      png_uint_32 i;\r
+      int v;\r
+\r
+#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED\r
+      if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)\r
+      {\r
+         int j;\r
+         png_uint_32 lmhi, lmlo;\r
+         lmlo = lmins & PNG_LOMASK;\r
+         lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;\r
+\r
+         for (j = 0; j < num_p_filters; j++)\r
+         {\r
+            if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_AVG)\r
+            {\r
+               lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>\r
+                  PNG_WEIGHT_SHIFT;\r
+               lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>\r
+                  PNG_WEIGHT_SHIFT;\r
+            }\r
+         }\r
+\r
+         lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>\r
+            PNG_COST_SHIFT;\r
+         lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>\r
+            PNG_COST_SHIFT;\r
+\r
+         if (lmhi > PNG_HIMASK)\r
+            lmins = PNG_MAXSUM;\r
+         else\r
+            lmins = (lmhi << PNG_HISHIFT) + lmlo;\r
+      }\r
+#endif\r
+\r
+      for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,\r
+           pp = prev_row + 1; i < bpp; i++)\r
+      {\r
+         v = *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);\r
+\r
+         sum += (v < 128) ? v : 256 - v;\r
+      }\r
+      for (lp = row_buf + 1; i < row_bytes; i++)\r
+      {\r
+         v = *dp++ =\r
+          (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2)) & 0xff);\r
+\r
+         sum += (v < 128) ? v : 256 - v;\r
+\r
+         if (sum > lmins)  /* We are already worse, don't continue. */\r
+            break;\r
+      }\r
+\r
+#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED\r
+      if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)\r
+      {\r
+         int j;\r
+         png_uint_32 sumhi, sumlo;\r
+         sumlo = sum & PNG_LOMASK;\r
+         sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;\r
+\r
+         for (j = 0; j < num_p_filters; j++)\r
+         {\r
+            if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)\r
+            {\r
+               sumlo = (sumlo * png_ptr->filter_weights[j]) >>\r
+                  PNG_WEIGHT_SHIFT;\r
+               sumhi = (sumhi * png_ptr->filter_weights[j]) >>\r
+                  PNG_WEIGHT_SHIFT;\r
+            }\r
+         }\r
+\r
+         sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>\r
+            PNG_COST_SHIFT;\r
+         sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>\r
+            PNG_COST_SHIFT;\r
+\r
+         if (sumhi > PNG_HIMASK)\r
+            sum = PNG_MAXSUM;\r
+         else\r
+            sum = (sumhi << PNG_HISHIFT) + sumlo;\r
+      }\r
+#endif\r
+\r
+      if (sum < mins)\r
+      {\r
+         mins = sum;\r
+         best_row = png_ptr->avg_row;\r
+      }\r
+   }\r
+\r
+   /* Paeth filter */\r
+   if (filter_to_do == PNG_FILTER_PAETH)\r
+   {\r
+      png_bytep rp, dp, pp, cp, lp;\r
+      png_uint_32 i;\r
+      for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,\r
+           pp = prev_row + 1; i < bpp; i++)\r
+      {\r
+         *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);\r
+      }\r
+\r
+      for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)\r
+      {\r
+         int a, b, c, pa, pb, pc, p;\r
+\r
+         b = *pp++;\r
+         c = *cp++;\r
+         a = *lp++;\r
+\r
+         p = b - c;\r
+         pc = a - c;\r
+\r
+#ifdef PNG_USE_ABS\r
+         pa = abs(p);\r
+         pb = abs(pc);\r
+         pc = abs(p + pc);\r
+#else\r
+         pa = p < 0 ? -p : p;\r
+         pb = pc < 0 ? -pc : pc;\r
+         pc = (p + pc) < 0 ? -(p + pc) : p + pc;\r
+#endif\r
+\r
+         p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;\r
+\r
+         *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);\r
+      }\r
+      best_row = png_ptr->paeth_row;\r
+   }\r
+\r
+   else if (filter_to_do & PNG_FILTER_PAETH)\r
+   {\r
+      png_bytep rp, dp, pp, cp, lp;\r
+      png_uint_32 sum = 0, lmins = mins;\r
+      png_uint_32 i;\r
+      int v;\r
+\r
+#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED\r
+      if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)\r
+      {\r
+         int j;\r
+         png_uint_32 lmhi, lmlo;\r
+         lmlo = lmins & PNG_LOMASK;\r
+         lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;\r
+\r
+         for (j = 0; j < num_p_filters; j++)\r
+         {\r
+            if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)\r
+            {\r
+               lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>\r
+                  PNG_WEIGHT_SHIFT;\r
+               lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>\r
+                  PNG_WEIGHT_SHIFT;\r
+            }\r
+         }\r
+\r
+         lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>\r
+            PNG_COST_SHIFT;\r
+         lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>\r
+            PNG_COST_SHIFT;\r
+\r
+         if (lmhi > PNG_HIMASK)\r
+            lmins = PNG_MAXSUM;\r
+         else\r
+            lmins = (lmhi << PNG_HISHIFT) + lmlo;\r
+      }\r
+#endif\r
+\r
+      for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,\r
+           pp = prev_row + 1; i < bpp; i++)\r
+      {\r
+         v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);\r
+\r
+         sum += (v < 128) ? v : 256 - v;\r
+      }\r
+\r
+      for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)\r
+      {\r
+         int a, b, c, pa, pb, pc, p;\r
+\r
+         b = *pp++;\r
+         c = *cp++;\r
+         a = *lp++;\r
+\r
+#ifndef PNG_SLOW_PAETH\r
+         p = b - c;\r
+         pc = a - c;\r
+#ifdef PNG_USE_ABS\r
+         pa = abs(p);\r
+         pb = abs(pc);\r
+         pc = abs(p + pc);\r
+#else\r
+         pa = p < 0 ? -p : p;\r
+         pb = pc < 0 ? -pc : pc;\r
+         pc = (p + pc) < 0 ? -(p + pc) : p + pc;\r
+#endif\r
+         p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;\r
+#else /* PNG_SLOW_PAETH */\r
+         p = a + b - c;\r
+         pa = abs(p - a);\r
+         pb = abs(p - b);\r
+         pc = abs(p - c);\r
+         if (pa <= pb && pa <= pc)\r
+            p = a;\r
+         else if (pb <= pc)\r
+            p = b;\r
+         else\r
+            p = c;\r
+#endif /* PNG_SLOW_PAETH */\r
+\r
+         v = *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);\r
+\r
+         sum += (v < 128) ? v : 256 - v;\r
+\r
+         if (sum > lmins)  /* We are already worse, don't continue. */\r
+            break;\r
+      }\r
+\r
+#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED\r
+      if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)\r
+      {\r
+         int j;\r
+         png_uint_32 sumhi, sumlo;\r
+         sumlo = sum & PNG_LOMASK;\r
+         sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;\r
+\r
+         for (j = 0; j < num_p_filters; j++)\r
+         {\r
+            if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)\r
+            {\r
+               sumlo = (sumlo * png_ptr->filter_weights[j]) >>\r
+                  PNG_WEIGHT_SHIFT;\r
+               sumhi = (sumhi * png_ptr->filter_weights[j]) >>\r
+                  PNG_WEIGHT_SHIFT;\r
+            }\r
+         }\r
+\r
+         sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>\r
+            PNG_COST_SHIFT;\r
+         sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>\r
+            PNG_COST_SHIFT;\r
+\r
+         if (sumhi > PNG_HIMASK)\r
+            sum = PNG_MAXSUM;\r
+         else\r
+            sum = (sumhi << PNG_HISHIFT) + sumlo;\r
+      }\r
+#endif\r
+\r
+      if (sum < mins)\r
+      {\r
+         best_row = png_ptr->paeth_row;\r
+      }\r
+   }\r
+#endif /* PNG_WRITE_FILTER_SUPPORTED */\r
+   /* Do the actual writing of the filtered row data from the chosen filter. */\r
+\r
+   png_write_filtered_row(png_ptr, best_row);\r
+\r
+#ifdef PNG_WRITE_FILTER_SUPPORTED\r
+#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED\r
+   /* Save the type of filter we picked this time for future calculations */\r
+   if (png_ptr->num_prev_filters > 0)\r
+   {\r
+      int j;\r
+      for (j = 1; j < num_p_filters; j++)\r
+      {\r
+         png_ptr->prev_filters[j] = png_ptr->prev_filters[j - 1];\r
+      }\r
+      png_ptr->prev_filters[j] = best_row[0];\r
+   }\r
+#endif\r
+#endif /* PNG_WRITE_FILTER_SUPPORTED */\r
+}\r
+\r
+\r
+/* Do the actual writing of a previously filtered row. */\r
+void /* PRIVATE */\r
+png_write_filtered_row(png_structp png_ptr, png_bytep filtered_row)\r
+{\r
+   png_debug(1, "in png_write_filtered_row");\r
+\r
+   png_debug1(2, "filter = %d", filtered_row[0]);\r
+   /* Set up the zlib input buffer */\r
+\r
+   png_ptr->zstream.next_in = filtered_row;\r
+   png_ptr->zstream.avail_in = (uInt)png_ptr->row_info.rowbytes + 1;\r
+   /* Repeat until we have compressed all the data */\r
+   do\r
+   {\r
+      int ret; /* Return of zlib */\r
+\r
+      /* Compress the data */\r
+      ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);\r
+      /* Check for compression errors */\r
+      if (ret != Z_OK)\r
+      {\r
+         if (png_ptr->zstream.msg != NULL)\r
+            png_error(png_ptr, png_ptr->zstream.msg);\r
+         else\r
+            png_error(png_ptr, "zlib error");\r
+      }\r
+\r
+      /* See if it is time to write another IDAT */\r
+      if (!(png_ptr->zstream.avail_out))\r
+      {\r
+         /* Write the IDAT and reset the zlib output buffer */\r
+         png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);\r
+         png_ptr->zstream.next_out = png_ptr->zbuf;\r
+         png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;\r
+      }\r
+   /* Repeat until all data has been compressed */\r
+   } while (png_ptr->zstream.avail_in);\r
+\r
+   /* Swap the current and previous rows */\r
+   if (png_ptr->prev_row != NULL)\r
+   {\r
+      png_bytep tptr;\r
+\r
+      tptr = png_ptr->prev_row;\r
+      png_ptr->prev_row = png_ptr->row_buf;\r
+      png_ptr->row_buf = tptr;\r
+   }\r
+\r
+   /* Finish row - updates counters and flushes zlib if last row */\r
+   png_write_finish_row(png_ptr);\r
+\r
+#ifdef PNG_WRITE_FLUSH_SUPPORTED\r
+   png_ptr->flush_rows++;\r
+\r
+   if (png_ptr->flush_dist > 0 &&\r
+       png_ptr->flush_rows >= png_ptr->flush_dist)\r
+   {\r
+      png_write_flush(png_ptr);\r
+   }\r
+#endif\r
+}\r
+#endif /* PNG_WRITE_SUPPORTED */\r
index 8e2688f..a207bb5 100644 (file)
@@ -16,43 +16,51 @@ add_definitions(-DHAVE_STRING_H=1)
 # The .cpp files:
 
 set(lib_srcs
-       tif_stream.cxx
-       tif_zip.c
-       tif_write.c
-       tif_warning.c
-       tif_version.c
-       tif_tile.c
-       tif_thunder.c
-       tif_swab.c
-       tif_strip.c
-       tif_read.c
-       tif_print.c
-       tif_predict.c
-       tif_pixarlog.c
-       tif_packbits.c
-       tif_open.c
-       tif_ojpeg.c
-       tif_next.c
-       tif_lzw.c
-       tif_luv.c
-       tif_jpeg.c
-       tif_getimage.c
-       tif_flush.c
-       tif_fax3sm.c
-       tif_fax3.c
-       tif_extension.c
-       tif_error.c
-       tif_dumpmode.c
-       tif_dirwrite.c
-       tif_dirread.c
-       tif_dirinfo.c
-       tif_dir.c
-       tif_compress.c
-       tif_color.c
-       tif_codec.c
-       tif_close.c
-       tif_aux.c
-       )
+       t4.h
+    tiffio.hxx
+    tiffiop.h
+    tif_aux.c
+    tif_close.c
+    tif_codec.c
+    tif_color.c
+    tif_compress.c
+    tif_dir.c
+    tif_dir.h
+    tif_dirinfo.c
+    tif_dirread.c
+    tif_dirwrite.c
+    tif_dumpmode.c
+    tif_error.c
+    tif_extension.c
+    tif_fax3.c
+    tif_fax3.h
+    tif_fax3sm.c
+    tif_flush.c
+    tif_getimage.c
+    tif_jbig.c
+    tif_jpeg.c
+    tif_luv.c
+    tif_lzw.c
+    tif_next.c
+    tif_ojpeg.c
+    tif_open.c
+    tif_packbits.c
+    tif_pixarlog.c
+    tif_predict.c
+    tif_predict.h
+    tif_print.c
+    tif_read.c
+    tif_stream.cxx
+    tif_strip.c
+    tif_swab.c
+    tif_thunder.c
+    tif_tile.c
+    tif_version.c
+    tif_warning.c
+    tif_write.c
+    tif_zip.c
+    uvcode.h
+    )
 
 if(UNIX)
     set(lib_srcs ${lib_srcs} tif_unix.c)
@@ -68,12 +76,13 @@ endif(WIN32)
 
 file(GLOB lib_hdrs *.h*)
 
-set(lib_ext_hdrs "../include/tiff.h" "../include/tiffio.h" "../include/tiffvers.h")
+set(lib_ext_hdrs "../include/tiff.h" "../include/tiffio.h" "../include/tiffvers.h" "../include/tiffconf.h")
 
 set(the_target "libtiff")
 
 if(MSVC)
-    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /W3")
+    string(REPLACE "/W4" "/W0" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
+    string(REPLACE "/W4" "/W0" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
 endif()
 
 if(UNIX)
diff --git a/3rdparty/libtiff/Makefile.vc b/3rdparty/libtiff/Makefile.vc
deleted file mode 100644 (file)
index 530168a..0000000
+++ /dev/null
@@ -1,97 +0,0 @@
-# $Id: Makefile.vc,v 1.1 2005-06-17 13:54:52 vp153 Exp $
-#
-# Copyright (C) 2004, Andrey Kiselev <dron@remotesensing.org>
-#
-# Permission to use, copy, modify, distribute, and sell this software and 
-# its documentation for any purpose is hereby granted without fee, provided
-# that (i) the above copyright notices and this permission notice appear in
-# all copies of the software and related documentation, and (ii) the names of
-# Sam Leffler and Silicon Graphics may not be used in any advertising or
-# publicity relating to the software without the specific, prior written
-# permission of Sam Leffler and Silicon Graphics.
-# 
-# THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
-# EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
-# WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
-# 
-# IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
-# ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
-# OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
-# WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
-# LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
-# OF THIS SOFTWARE.
-#
-# Makefile for MS Visual C and Watcom C compilers.
-#
-# To build:
-# C:\libtiff\libtiff> nmake /f makefile.vc all
-#
-
-!INCLUDE ..\nmake.opt
-
-INCL   = -I. $(JPEG_INCLUDE) $(ZLIB_INCLUDE)
-
-!IFDEF USE_WIN_CRT_LIB
-OBJ_SYSDEP_MODULE = tif_unix.obj
-!ELSE
-OBJ_SYSDEP_MODULE = tif_win32.obj
-!ENDIF
-
-OBJ    = \
-       tif_aux.obj \
-       tif_close.obj \
-       tif_codec.obj \
-       tif_color.obj \
-       tif_compress.obj \
-       tif_dir.obj \
-       tif_dirinfo.obj \
-       tif_dirread.obj \
-       tif_dirwrite.obj \
-       tif_dumpmode.obj \
-       tif_error.obj \
-       tif_extension.obj \
-       tif_fax3.obj \
-       tif_fax3sm.obj \
-       tif_getimage.obj \
-       tif_jpeg.obj \
-       tif_ojpeg.obj \
-       tif_flush.obj \
-       tif_luv.obj \
-       tif_lzw.obj \
-       tif_next.obj \
-       tif_open.obj \
-       tif_packbits.obj \
-       tif_pixarlog.obj \
-       tif_predict.obj \
-       tif_print.obj \
-       tif_read.obj \
-       tif_stream.obj \
-       tif_swab.obj \
-       tif_strip.obj \
-       tif_thunder.obj \
-       tif_tile.obj \
-       tif_version.obj \
-       tif_warning.obj \
-       tif_write.obj \
-       tif_zip.obj \
-       $(OBJ_SYSDEP_MODULE)
-
-all:   libtiff.lib $(DLLNAME)
-
-tif_config.h:
-       copy tif_config.h.vc tif_config.h
-
-tiffconf.h:
-       copy tiffconf.h.vc tiffconf.h
-
-libtiff.lib:   tif_config.h $(OBJ)
-       $(AR) /out:libtiff.lib $(OBJ) $(LIBS)
-
-$(DLLNAME):    tif_config.h $(OBJ)
-       $(LD) /debug /dll /def:libtiff.def /out:$(DLLNAME) \
-       /implib:libtiff_i.lib $(OBJ) $(LIBS)
-       
-clean:
-       -del *.obj *.lib *.dll *.exe
-
-
diff --git a/3rdparty/libtiff/libtiff.def b/3rdparty/libtiff/libtiff.def
deleted file mode 100644 (file)
index 42690dc..0000000
+++ /dev/null
@@ -1,121 +0,0 @@
-EXPORTS TIFFOpen
-       TIFFOpenW
-       TIFFGetVersion
-       TIFFCleanup
-       TIFFClose
-       TIFFFlush
-       TIFFFlushData
-       TIFFGetField
-       TIFFVGetField
-       TIFFGetFieldDefaulted
-       TIFFVGetFieldDefaulted
-       TIFFGetTagListEntry
-       TIFFGetTagListCount
-       TIFFReadDirectory
-       TIFFScanlineSize
-       TIFFStripSize
-       TIFFVStripSize
-       TIFFRawStripSize
-       TIFFTileRowSize
-       TIFFTileSize
-       TIFFVTileSize
-       TIFFFileno
-       TIFFGetMode
-       TIFFIsTiled
-       TIFFIsByteSwapped
-       TIFFIsBigEndian
-       TIFFCIELabToRGBInit
-       TIFFCIELabToXYZ
-       TIFFXYZToRGB
-       TIFFYCbCrToRGBInit
-       TIFFYCbCrtoRGB
-       TIFFCurrentRow
-       TIFFCurrentDirectory
-       TIFFCurrentStrip
-       TIFFCurrentTile
-       TIFFDataWidth
-       TIFFReadBufferSetup
-       TIFFLastDirectory
-       TIFFSetDirectory
-       TIFFSetSubDirectory
-       TIFFUnlinkDirectory
-       TIFFSetField
-       TIFFVSetField
-       TIFFCheckpointDirectory
-       TIFFWriteDirectory
-       TIFFRewriteDirectory
-       TIFFPrintDirectory
-       TIFFReadScanline
-       TIFFWriteScanline
-       TIFFReadRGBAImage
-       TIFFReadRGBAImageOriented
-       TIFFPrintDirectory
-       TIFFReadScanline
-       TIFFWriteScanline
-       TIFFReadRGBAImage
-       TIFFFdOpen
-       TIFFClientOpen
-       TIFFFileName
-       TIFFError
-       TIFFWarning
-       TIFFSetErrorHandler
-       TIFFSetWarningHandler
-       TIFFComputeTile
-       TIFFCheckTile
-       TIFFNumberOfTiles
-       TIFFReadTile
-       TIFFWriteTile
-       TIFFComputeStrip
-       TIFFNumberOfStrips
-       TIFFRGBAImageBegin
-       TIFFRGBAImageEnd
-       TIFFReadEncodedStrip
-       TIFFReadRawStrip
-       TIFFReadEncodedTile
-       TIFFReadRawTile
-       TIFFReadRGBATile
-       TIFFReadRGBAStrip
-       TIFFWriteEncodedStrip
-       TIFFWriteRawStrip
-       TIFFWriteEncodedTile
-       TIFFWriteRawTile
-       TIFFSetWriteOffset
-       TIFFSwabDouble
-       TIFFSwabShort
-       TIFFSwabLong
-       TIFFSwabArrayOfShort
-       TIFFSwabArrayOfLong
-       TIFFSwabArrayOfDouble
-       TIFFReverseBits
-       TIFFGetBitRevTable
-       TIFFDefaultStripSize
-       TIFFDefaultTileSize
-       TIFFRasterScanlineSize
-       _TIFFmalloc
-       _TIFFrealloc
-       _TIFFfree
-       _TIFFmemset
-       _TIFFmemcpy
-       _TIFFmemcmp
-       TIFFCreateDirectory
-       TIFFDefaultStripSize
-       TIFFSetTagExtender
-       TIFFMergeFieldInfo
-       TIFFCurrentDirOffset
-       TIFFWriteCheck
-       TIFFRGBAImageOK
-       TIFFNumberOfDirectories
-       TIFFSetFileName
-       TIFFSetClientdata
-       TIFFSetMode
-       TIFFClientdata
-       TIFFGetReadProc
-       TIFFGetWriteProc
-       TIFFGetSeekProc
-       TIFFGetCloseProc
-       TIFFGetSizeProc
-       TIFFGetMapFileProc
-       TIFFGetUnmapFileProc
-       TIFFIsCODECConfigured
-       TIFFGetConfiguredCODECs
-
index 19da69b..870704f 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: t4.h,v 1.1 2005-06-17 13:54:52 vp153 Exp $ */
+/* $Id: t4.h,v 1.1.1.1.2.1 2010-06-08 18:50:41 bfriesen Exp $ */
 
 /*
  * Copyright (c) 1988-1997 Sam Leffler
@@ -283,3 +283,10 @@ extern     const tableentry TIFFFaxWhiteCodes[];
 extern const tableentry TIFFFaxBlackCodes[];
 #endif
 #endif /* _T4_ */
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
index f262da1..8c48228 100644 (file)
@@ -1,4 +1,4 @@
-/* $Header: /home/vp/work/opencv-cvsbackup/opencv/3rdparty/libtiff/tif_apple.c,v 1.1 2005-06-17 13:54:52 vp153 Exp $ */
+/* $Header: /cvs/maptools/cvsroot/libtiff/libtiff/Attic/tif_apple.c,v 1.3.2.1 2010-06-08 18:50:41 bfriesen Exp $ */
 
 /*
  * Copyright (c) 1988-1997 Sam Leffler
@@ -125,7 +125,7 @@ _tiffSizeProc(thandle_t fd)
        long size;
 
        if (GetEOF((short) fd, &size) != noErr) {
-               TIFFError("_tiffSizeProc", "%s: Cannot get file size");
+               TIFFErrorExt(fd, "_tiffSizeProc", "%s: Cannot get file size");
                return (-1L);
        }
        return ((toff_t) size);
@@ -202,10 +202,10 @@ TIFFOpen(const char* name, const char* mode)
        }
        return (TIFFFdOpen((int) fref, name, mode));
 badCreate:
-       TIFFError(module, "%s: Cannot create", name);
+       TIFFErrorExt(0, module, "%s: Cannot create", name);
        return ((TIFF*) 0);
 badOpen:
-       TIFFError(module, "%s: Cannot open", name);
+       TIFFErrorExt(0, module, "%s: Cannot open", name);
        return ((TIFF*) 0);
 }
 
@@ -272,3 +272,10 @@ appleErrorHandler(const char* module, const char* fmt, va_list ap)
        fprintf(stderr, ".\n");
 }
 TIFFErrorHandler _TIFFerrorHandler = appleErrorHandler;
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
index 8ec496d..272f0d9 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: tif_aux.c,v 1.1 2005-06-17 13:54:52 vp153 Exp $ */
+/* $Id: tif_aux.c,v 1.20.2.3 2010-06-09 21:15:27 bfriesen Exp $ */
 
 /*
  * Copyright (c) 1991-1997 Sam Leffler
 #include "tif_predict.h"
 #include <math.h>
 
+tdata_t
+_TIFFCheckRealloc(TIFF* tif, tdata_t buffer,
+                 size_t nmemb, size_t elem_size, const char* what)
+{
+       tdata_t cp = NULL;
+       tsize_t bytes = nmemb * elem_size;
+
+       /*
+        * XXX: Check for integer overflow.
+        */
+       if (nmemb && elem_size && bytes / elem_size == nmemb)
+               cp = _TIFFrealloc(buffer, bytes);
+
+       if (cp == NULL)
+               TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+                            "Failed to allocate memory for %s "
+                            "(%ld elements of %ld bytes each)",
+                            what,(long) nmemb, (long) elem_size);
+
+       return cp;
+}
+
+tdata_t
+_TIFFCheckMalloc(TIFF* tif, size_t nmemb, size_t elem_size, const char* what)
+{
+       return _TIFFCheckRealloc(tif, NULL, nmemb, elem_size, what);
+}
+
 static int
 TIFFDefaultTransferFunction(TIFFDirectory* td)
 {
@@ -156,17 +184,17 @@ TIFFVGetFieldDefaulted(TIFF* tif, ttag_t tag, va_list ap)
                 {
                        TIFFPredictorState* sp = (TIFFPredictorState*) tif->tif_data;
                        *va_arg(ap, uint16*) = (uint16) sp->predictor;
-                       return (1);
+                       return 1;
                 }
        case TIFFTAG_DOTRANGE:
                *va_arg(ap, uint16 *) = 0;
                *va_arg(ap, uint16 *) = (1<<td->td_bitspersample)-1;
                return (1);
        case TIFFTAG_INKSET:
-               *va_arg(ap, uint16 *) = td->td_inkset;
-               return (1);
+               *va_arg(ap, uint16 *) = INKSET_CMYK;
+               return 1;
        case TIFFTAG_NUMBEROFINKS:
-               *va_arg(ap, uint16 *) = td->td_ninks;
+               *va_arg(ap, uint16 *) = 4;
                return (1);
        case TIFFTAG_EXTRASAMPLES:
                *va_arg(ap, uint16 *) = td->td_extrasamples;
@@ -190,18 +218,12 @@ TIFFVGetFieldDefaulted(TIFF* tif, ttag_t tag, va_list ap)
                *va_arg(ap, uint32 *) = td->td_imagedepth;
                return (1);
        case TIFFTAG_YCBCRCOEFFICIENTS:
-               if (!td->td_ycbcrcoeffs) {
-                       td->td_ycbcrcoeffs = (float *)
-                           _TIFFmalloc(3*sizeof (float));
-                       if (!td->td_ycbcrcoeffs)
-                               return (0);
+               {
                        /* defaults are from CCIR Recommendation 601-1 */
-                       td->td_ycbcrcoeffs[0] = 0.299f;
-                       td->td_ycbcrcoeffs[1] = 0.587f;
-                       td->td_ycbcrcoeffs[2] = 0.114f;
+                       static float ycbcrcoeffs[] = { 0.299f, 0.587f, 0.114f };
+                       *va_arg(ap, float **) = ycbcrcoeffs;
+                       return 1;
                }
-               *va_arg(ap, float **) = td->td_ycbcrcoeffs;
-               return (1);
        case TIFFTAG_YCBCRSUBSAMPLING:
                *va_arg(ap, uint16 *) = td->td_ycbcrsubsampling[0];
                *va_arg(ap, uint16 *) = td->td_ycbcrsubsampling[1];
@@ -210,25 +232,21 @@ TIFFVGetFieldDefaulted(TIFF* tif, ttag_t tag, va_list ap)
                *va_arg(ap, uint16 *) = td->td_ycbcrpositioning;
                return (1);
        case TIFFTAG_WHITEPOINT:
-               if (!td->td_whitepoint) {
-                       td->td_whitepoint = (float *)
-                               _TIFFmalloc(2 * sizeof (float));
-                       if (!td->td_whitepoint)
-                               return (0);
-                       /* TIFF 6.0 specification says that it is no default
+               {
+                       static float whitepoint[2];
+
+                       /* TIFF 6.0 specification tells that it is no default
                           value for the WhitePoint, but AdobePhotoshop TIFF
                           Technical Note tells that it should be CIE D50. */
-                       td->td_whitepoint[0] =
-                               D50_X0 / (D50_X0 + D50_Y0 + D50_Z0);
-                       td->td_whitepoint[1] =
-                               D50_Y0 / (D50_X0 + D50_Y0 + D50_Z0);
+                       whitepoint[0] = D50_X0 / (D50_X0 + D50_Y0 + D50_Z0);
+                       whitepoint[1] = D50_Y0 / (D50_X0 + D50_Y0 + D50_Z0);
+                       *va_arg(ap, float **) = whitepoint;
+                       return 1;
                }
-               *va_arg(ap, float **) = td->td_whitepoint;
-               return (1);
        case TIFFTAG_TRANSFERFUNCTION:
                if (!td->td_transferfunction[0] &&
                    !TIFFDefaultTransferFunction(td)) {
-                       TIFFError(tif->tif_name, "No space for \"TransferFunction\" tag");
+                       TIFFErrorExt(tif->tif_clientdata, tif->tif_name, "No space for \"TransferFunction\" tag");
                        return (0);
                }
                *va_arg(ap, uint16 **) = td->td_transferfunction[0];
@@ -243,7 +261,7 @@ TIFFVGetFieldDefaulted(TIFF* tif, ttag_t tag, va_list ap)
                *va_arg(ap, float **) = td->td_refblackwhite;
                return (1);
        }
-       return (0);
+       return 0;
 }
 
 /*
@@ -263,3 +281,10 @@ TIFFGetFieldDefaulted(TIFF* tif, ttag_t tag, ...)
 }
 
 /* vim: set ts=8 sts=8 sw=8 noet: */
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
index dcafc5b..02591ba 100644 (file)
@@ -1,4 +1,4 @@
-/* $Header: /home/vp/work/opencv-cvsbackup/opencv/3rdparty/libtiff/tif_close.c,v 1.1 2005-06-17 13:54:52 vp153 Exp $ */
+/* $Id: tif_close.c,v 1.10.2.1 2010-06-08 18:50:41 bfriesen Exp $ */
 
 /*
  * Copyright (c) 1988-1997 Sam Leffler
@@ -54,27 +54,27 @@ TIFFCleanup(TIFF* tif)
        TIFFFreeDirectory(tif);
 
        if (tif->tif_dirlist)
-           _TIFFfree(tif->tif_dirlist);
-           
+               _TIFFfree(tif->tif_dirlist);
+
        /* Clean up client info links */
        while( tif->tif_clientinfo )
        {
-           TIFFClientInfoLink *link = tif->tif_clientinfo;
+               TIFFClientInfoLink *link = tif->tif_clientinfo;
 
-           tif->tif_clientinfo = link->next;
-           _TIFFfree( link->name );
-           _TIFFfree( link );
+               tif->tif_clientinfo = link->next;
+               _TIFFfree( link->name );
+               _TIFFfree( link );
        }
 
        if (tif->tif_rawdata && (tif->tif_flags&TIFF_MYBUFFER))
-           _TIFFfree(tif->tif_rawdata);
+               _TIFFfree(tif->tif_rawdata);
        if (isMapped(tif))
-           TIFFUnmapFileContents(tif, tif->tif_base, tif->tif_size);
+               TIFFUnmapFileContents(tif, tif->tif_base, tif->tif_size);
 
        /* Clean up custom fields */
-       if (tif->tif_nfields > 0) 
+       if (tif->tif_nfields > 0)
        {
-           int  i;
+               size_t  i;
 
            for (i = 0; i < tif->tif_nfields; i++) 
            {
@@ -117,3 +117,10 @@ TIFFClose(TIFF* tif)
        (void) (*closeproc)(fd);
 }
 
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
index e1136c6..d5c6fd1 100644 (file)
@@ -1,4 +1,4 @@
-/* $Header: /home/vp/work/opencv-cvsbackup/opencv/3rdparty/libtiff/tif_codec.c,v 1.1 2005-06-17 13:54:52 vp153 Exp $ */
+/* $Id: tif_codec.c,v 1.10.2.2 2010-06-08 18:50:41 bfriesen Exp $ */
 
 /*
  * Copyright (c) 1988-1997 Sam Leffler
@@ -95,16 +95,19 @@ TIFFCodec _TIFFBuiltinCODECS[] = {
     { "PixarLog",      COMPRESSION_PIXARLOG,   TIFFInitPixarLog },
     { "SGILog",                COMPRESSION_SGILOG,     TIFFInitSGILog },
     { "SGILog24",      COMPRESSION_SGILOG24,   TIFFInitSGILog },
-    { NULL }
+    { NULL,             0,                      NULL }
 };
 
 static int
 _notConfigured(TIFF* tif)
 {
        const TIFFCodec* c = TIFFFindCODEC(tif->tif_dir.td_compression);
-
-       TIFFError(tif->tif_name,
-           "%s compression support is not configured", c->name);
+        char compression_code[20];
+        
+        sprintf( compression_code, "%d", tif->tif_dir.td_compression );
+       TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+                     "%s compression support is not configured", 
+                     c ? c->name : compression_code );
        return (0);
 }
 
@@ -148,3 +151,10 @@ TIFFIsCODECConfigured(uint16 scheme)
        return 0;
 }
 
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
index aad1c75..02eb346 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: tif_color.c,v 1.1 2005-06-17 13:54:52 vp153 Exp $ */
+/* $Id: tif_color.c,v 1.12.2.1 2010-06-08 18:50:41 bfriesen Exp $ */
 
 /*
  * Copyright (c) 1988-1997 Sam Leffler
@@ -88,27 +88,32 @@ TIFFXYZToRGB(TIFFCIELabToRGB *cielab, float X, float Y, float Z,
        Yb =  matrix[6] * X + matrix[7] * Y + matrix[8] * Z;
 
        /* Clip input */
-       Yr = TIFFmax( Yr, cielab->display.d_Y0R );
-       Yg = TIFFmax( Yg, cielab->display.d_Y0G );
-       Yb = TIFFmax( Yb, cielab->display.d_Y0B );
+       Yr = TIFFmax(Yr, cielab->display.d_Y0R);
+       Yg = TIFFmax(Yg, cielab->display.d_Y0G);
+       Yb = TIFFmax(Yb, cielab->display.d_Y0B);
+
+       /* Avoid overflow in case of wrong input values */
+       Yr = TIFFmin(Yr, cielab->display.d_YCR);
+       Yg = TIFFmin(Yg, cielab->display.d_YCG);
+       Yb = TIFFmin(Yb, cielab->display.d_YCB);
 
        /* Turn luminosity to colour value. */
-       i = TIFFmin(cielab->range,
-                   (int)((Yr - cielab->display.d_Y0R) / cielab->rstep));
+       i = (int)((Yr - cielab->display.d_Y0R) / cielab->rstep);
+       i = TIFFmin(cielab->range, i);
        *r = RINT(cielab->Yr2r[i]);
 
-       i = TIFFmin(cielab->range,
-                   (int)((Yg - cielab->display.d_Y0G) / cielab->gstep));
+       i = (int)((Yg - cielab->display.d_Y0G) / cielab->gstep);
+       i = TIFFmin(cielab->range, i);
        *g = RINT(cielab->Yg2g[i]);
 
-       i = TIFFmin(cielab->range,
-                   (int)((Yb - cielab->display.d_Y0B) / cielab->bstep));
+       i = (int)((Yb - cielab->display.d_Y0B) / cielab->bstep);
+       i = TIFFmin(cielab->range, i);
        *b = RINT(cielab->Yb2b[i]);
 
        /* Clip output. */
-       *r = TIFFmin( *r, cielab->display.d_Vrwr );
-       *g = TIFFmin( *g, cielab->display.d_Vrwg );
-       *b = TIFFmin( *b, cielab->display.d_Vrwb );
+       *r = TIFFmin(*r, cielab->display.d_Vrwr);
+       *g = TIFFmin(*g, cielab->display.d_Vrwg);
+       *b = TIFFmin(*b, cielab->display.d_Vrwb);
 }
 #undef RINT
 
@@ -172,13 +177,14 @@ TIFFCIELabToRGBInit(TIFFCIELabToRGB* cielab,
 #define        ONE_HALF                ((int32)(1<<(SHIFT-1)))
 #define        Code2V(c, RB, RW, CR)   ((((c)-(int32)(RB))*(float)(CR))/(float)(((RW)-(RB)) ? ((RW)-(RB)) : 1))
 #define        CLAMP(f,min,max)        ((f)<(min)?(min):(f)>(max)?(max):(f))
+#define HICLAMP(f,max)         ((f)>(max)?(max):(f))
 
 void
 TIFFYCbCrtoRGB(TIFFYCbCrToRGB *ycbcr, uint32 Y, int32 Cb, int32 Cr,
               uint32 *r, uint32 *g, uint32 *b)
 {
        /* XXX: Only 8-bit YCbCr input supported for now */
-       Y = CLAMP(Y, 0, 255), Cb = CLAMP(Cb, 0, 255), Cr = CLAMP(Cr, 0, 255);
+       Y = HICLAMP(Y, 255), Cb = CLAMP(Cb, 0, 255), Cr = CLAMP(Cr, 0, 255);
 
        *r = ycbcr->clamptab[ycbcr->Y_tab[Y] + ycbcr->Cr_r_tab[Cr]];
        *g = ycbcr->clamptab[ycbcr->Y_tab[Y]
@@ -259,6 +265,7 @@ TIFFYCbCrToRGBInit(TIFFYCbCrToRGB* ycbcr, float *luma, float *refBlackWhite)
 
     return 0;
 }
+#undef HICLAMP
 #undef CLAMP
 #undef Code2V
 #undef SHIFT
@@ -266,3 +273,10 @@ TIFFYCbCrToRGBInit(TIFFYCbCrToRGB* ycbcr, float *luma, float *refBlackWhite)
 #undef FIX
 
 /* vim: set ts=8 sts=8 sw=8 noet: */
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
index 5f16c49..0ce509b 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: tif_compress.c,v 1.1 2005-06-17 13:54:52 vp153 Exp $ */
+/* $Id: tif_compress.c,v 1.13.2.1 2010-06-08 18:50:41 bfriesen Exp $ */
 
 /*
  * Copyright (c) 1988-1997 Sam Leffler
 #include "tiffiop.h"
 
 static int
-TIFFNoEncode(TIFF* tif, char* method)
+TIFFNoEncode(TIFF* tif, const char* method)
 {
        const TIFFCodec* c = TIFFFindCODEC(tif->tif_dir.td_compression);
 
        if (c) { 
-               TIFFError(tif->tif_name, "%s %s encoding is not implemented",
-                          c->name, method);
+               TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+                            "%s %s encoding is not implemented",
+                            c->name, method);
        } else { 
-               TIFFError(tif->tif_name,
-                         "Compression scheme %u %s encoding is not implemented",
-                   tif->tif_dir.td_compression, method);
+               TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+                       "Compression scheme %u %s encoding is not implemented",
+                            tif->tif_dir.td_compression, method);
        }
        return (-1);
 }
@@ -69,17 +70,18 @@ _TIFFNoTileEncode(TIFF* tif, tidata_t pp, tsize_t cc, tsample_t s)
 }
 
 static int
-TIFFNoDecode(TIFF* tif, char* method)
+TIFFNoDecode(TIFF* tif, const char* method)
 {
        const TIFFCodec* c = TIFFFindCODEC(tif->tif_dir.td_compression);
 
        if (c)
-               TIFFError(tif->tif_name, "%s %s decoding is not implemented",
-                   c->name, method);
+               TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+                            "%s %s decoding is not implemented",
+                            c->name, method);
        else
-               TIFFError(tif->tif_name,
-                   "Compression scheme %u %s decoding is not implemented",
-                   tif->tif_dir.td_compression, method);
+               TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+                            "Compression scheme %u %s decoding is not implemented",
+                            tif->tif_dir.td_compression, method);
        return (-1);
 }
 
@@ -108,8 +110,8 @@ int
 _TIFFNoSeek(TIFF* tif, uint32 off)
 {
        (void) off;
-       TIFFError(tif->tif_name,
-           "Compression algorithm does not support random access");
+       TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+                    "Compression algorithm does not support random access");
        return (0);
 }
 
@@ -144,7 +146,7 @@ _TIFFSetDefaultCompressionState(TIFF* tif)
        tif->tif_cleanup = _TIFFvoid;
        tif->tif_defstripsize = _TIFFDefaultStripSize;
        tif->tif_deftilesize = _TIFFDefaultTileSize;
-       tif->tif_flags &= ~TIFF_NOBITREV;
+       tif->tif_flags &= ~(TIFF_NOBITREV|TIFF_NOREADRAW);
 }
 
 int
@@ -204,7 +206,7 @@ TIFFRegisterCODEC(uint16 scheme, const char* name, TIFFInitMethod init)
                cd->next = registeredCODECS;
                registeredCODECS = cd;
        } else {
-               TIFFError("TIFFRegisterCODEC",
+               TIFFErrorExt(0, "TIFFRegisterCODEC",
                    "No space to register compression scheme %s", name);
                return NULL;
        }
@@ -223,7 +225,7 @@ TIFFUnRegisterCODEC(TIFFCodec* c)
                        _TIFFfree(cd);
                        return;
                }
-       TIFFError("TIFFUnRegisterCODEC",
+       TIFFErrorExt(0, "TIFFUnRegisterCODEC",
            "Cannot remove compression scheme %s; not registered", c->name);
 }
 
@@ -248,7 +250,8 @@ TIFFGetConfiguredCODECs()
        TIFFCodec       *codecs = NULL, *new_codecs;
 
         for (cd = registeredCODECS; cd; cd = cd->next) {
-                new_codecs = _TIFFrealloc(codecs, i * sizeof(TIFFCodec));
+                new_codecs = (TIFFCodec *)
+                       _TIFFrealloc(codecs, i * sizeof(TIFFCodec));
                if (!new_codecs) {
                        _TIFFfree (codecs);
                        return NULL;
@@ -259,7 +262,8 @@ TIFFGetConfiguredCODECs()
        }
         for (c = _TIFFBuiltinCODECS; c->name; c++) {
                 if (TIFFIsCODECConfigured(c->scheme)) {
-                        new_codecs = _TIFFrealloc(codecs, i * sizeof(TIFFCodec));
+                        new_codecs = (TIFFCodec *)
+                               _TIFFrealloc(codecs, i * sizeof(TIFFCodec));
                        if (!new_codecs) {
                                _TIFFfree (codecs);
                                return NULL;
@@ -270,7 +274,7 @@ TIFFGetConfiguredCODECs()
                }
        }
 
-       new_codecs = _TIFFrealloc(codecs, i * sizeof(TIFFCodec));
+       new_codecs = (TIFFCodec *) _TIFFrealloc(codecs, i * sizeof(TIFFCodec));
        if (!new_codecs) {
                _TIFFfree (codecs);
                return NULL;
@@ -282,3 +286,10 @@ TIFFGetConfiguredCODECs()
 }
 
 /* vim: set ts=8 sts=8 sw=8 noet: */
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
index c5692af..4dd77dd 100644 (file)
@@ -8,11 +8,23 @@
    machine */
 #define HAVE_IEEEFP 1
 
+/* Define to 1 if you have the `jbg_newlen' function. */
+#define HAVE_JBG_NEWLEN 1
+
+/* Define to 1 if you have the <string.h> header file. */
+#define HAVE_STRING_H 1
+
 /* Define to 1 if you have the <sys/types.h> header file. */
 #define HAVE_SYS_TYPES_H 1
 
 /* Define to 1 if you have the <io.h> header file. */
-#define HAVE_IO_H 0
+#define HAVE_IO_H 1
+
+/* Define to 1 if you have the <search.h> header file. */
+#define HAVE_SEARCH_H 1
+
+/* Define to 1 if you have the `setmode' function. */
+#define HAVE_SETMODE 1
 
 /* The size of a `int', as computed by sizeof. */
 #define SIZEOF_INT 4
 /* The size of a `long', as computed by sizeof. */
 #define SIZEOF_LONG 4
 
+/* Signed 64-bit type */
+#define TIFF_INT64_T signed __int64
+
+/* Unsigned 64-bit type */
+#define TIFF_UINT64_T unsigned __int64
+
 /* Set the native cpu bit order */
 #define HOST_FILLORDER FILLORDER_LSB2MSB
 
 /* Define to `__inline__' or `__inline' if that's what the C compiler
    calls it, or to nothing if 'inline' is not supported under any name.  */
 #ifndef __cplusplus
-#define inline __inline
+# ifndef inline
+#  define inline __inline
+# endif
 #endif
 
+#define lfind _lfind
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
index ef1ffa2..ac44b38 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: tif_dir.c,v 1.1 2005-06-17 13:54:52 vp153 Exp $ */
+/* $Id: tif_dir.c,v 1.75.2.5 2010-06-09 21:15:27 bfriesen Exp $ */
 
 /*
  * Copyright (c) 1988-1997 Sam Leffler
@@ -53,49 +53,65 @@ setByteArray(void** vpp, void* vp, size_t nmemb, size_t elem_size)
                        _TIFFmemcpy(*vpp, vp, bytes);
        }
 }
-void _TIFFsetByteArray(void** vpp, void* vp, long n)
+void _TIFFsetByteArray(void** vpp, void* vp, uint32 n)
     { setByteArray(vpp, vp, n, 1); }
 void _TIFFsetString(char** cpp, char* cp)
     { setByteArray((void**) cpp, (void*) cp, strlen(cp)+1, 1); }
-void _TIFFsetNString(char** cpp, char* cp, long n)
+void _TIFFsetNString(char** cpp, char* cp, uint32 n)
     { setByteArray((void**) cpp, (void*) cp, n, 1); }
-void _TIFFsetShortArray(uint16** wpp, uint16* wp, long n)
+void _TIFFsetShortArray(uint16** wpp, uint16* wp, uint32 n)
     { setByteArray((void**) wpp, (void*) wp, n, sizeof (uint16)); }
-void _TIFFsetLongArray(uint32** lpp, uint32* lp, long n)
+void _TIFFsetLongArray(uint32** lpp, uint32* lp, uint32 n)
     { setByteArray((void**) lpp, (void*) lp, n, sizeof (uint32)); }
-void _TIFFsetFloatArray(float** fpp, float* fp, long n)
+void _TIFFsetFloatArray(float** fpp, float* fp, uint32 n)
     { setByteArray((void**) fpp, (void*) fp, n, sizeof (float)); }
-void _TIFFsetDoubleArray(double** dpp, double* dp, long n)
+void _TIFFsetDoubleArray(double** dpp, double* dp, uint32 n)
     { setByteArray((void**) dpp, (void*) dp, n, sizeof (double)); }
 
 /*
  * Install extra samples information.
  */
 static int
-setExtraSamples(TIFFDirectory* td, va_list ap, int* v)
+setExtraSamples(TIFFDirectory* td, va_list ap, uint32* v)
 {
+/* XXX: Unassociated alpha data == 999 is a known Corel Draw bug, see below */
+#define EXTRASAMPLE_COREL_UNASSALPHA 999 
+
        uint16* va;
-       int i;
+       uint32 i;
 
-       *v = va_arg(ap, int);
+       *v = va_arg(ap, uint32);
        if ((uint16) *v > td->td_samplesperpixel)
-               return (0);
+               return 0;
        va = va_arg(ap, uint16*);
        if (*v > 0 && va == NULL)               /* typically missing param */
-               return (0);
-       for (i = 0; i < *v; i++)
-               if (va[i] > EXTRASAMPLE_UNASSALPHA)
-                       return (0);
+               return 0;
+       for (i = 0; i < *v; i++) {
+               if (va[i] > EXTRASAMPLE_UNASSALPHA) {
+                       /*
+                        * XXX: Corel Draw is known to produce incorrect
+                        * ExtraSamples tags which must be patched here if we
+                        * want to be able to open some of the damaged TIFF
+                        * files: 
+                        */
+                       if (va[i] == EXTRASAMPLE_COREL_UNASSALPHA)
+                               va[i] = EXTRASAMPLE_UNASSALPHA;
+                       else
+                               return 0;
+               }
+       }
        td->td_extrasamples = (uint16) *v;
        _TIFFsetShortArray(&td->td_sampleinfo, va, td->td_extrasamples);
-       return (1);
+       return 1;
+
+#undef EXTRASAMPLE_COREL_UNASSALPHA
 }
 
-static int
-checkInkNamesString(TIFF* tif, int slen, const char* s)
+static uint32
+checkInkNamesString(TIFF* tif, uint32 slen, const char* s)
 {
        TIFFDirectory* td = &tif->tif_dir;
-       int i = td->td_samplesperpixel;
+       uint16 i = td->td_samplesperpixel;
 
        if (slen > 0) {
                const char* ep = s+slen;
@@ -109,7 +125,7 @@ checkInkNamesString(TIFF* tif, int slen, const char* s)
                return (cp-s);
        }
 bad:
-       TIFFError("TIFFSetField",
+       TIFFErrorExt(tif->tif_clientdata, "TIFFSetField",
            "%s: Invalid InkNames value; expecting %d names, found %d",
            tif->tif_name,
            td->td_samplesperpixel,
@@ -121,12 +137,10 @@ static int
 _TIFFVSetField(TIFF* tif, ttag_t tag, va_list ap)
 {
        static const char module[] = "_TIFFVSetField";
-       
+
        TIFFDirectory* td = &tif->tif_dir;
        int status = 1;
-       uint32 v32;
-       int i, v;
-       double d;
+       uint32 v32, i, v;
        char* s;
 
        switch (tag) {
@@ -142,28 +156,31 @@ _TIFFVSetField(TIFF* tif, ttag_t tag, va_list ap)
        case TIFFTAG_BITSPERSAMPLE:
                td->td_bitspersample = (uint16) va_arg(ap, int);
                /*
-                * If the data require post-decoding processing
-                * to byte-swap samples, set it up here.  Note
-                * that since tags are required to be ordered,
-                * compression code can override this behaviour
-                * in the setup method if it wants to roll the
-                * post decoding work in with its normal work.
+                * If the data require post-decoding processing to byte-swap
+                * samples, set it up here.  Note that since tags are required
+                * to be ordered, compression code can override this behaviour
+                * in the setup method if it wants to roll the post decoding
+                * work in with its normal work.
                 */
                if (tif->tif_flags & TIFF_SWAB) {
                        if (td->td_bitspersample == 16)
                                tif->tif_postdecode = _TIFFSwab16BitData;
+                       else if (td->td_bitspersample == 24)
+                               tif->tif_postdecode = _TIFFSwab24BitData;
                        else if (td->td_bitspersample == 32)
                                tif->tif_postdecode = _TIFFSwab32BitData;
                        else if (td->td_bitspersample == 64)
                                tif->tif_postdecode = _TIFFSwab64BitData;
+                       else if (td->td_bitspersample == 128) /* two 64's */
+                               tif->tif_postdecode = _TIFFSwab64BitData;
                }
                break;
        case TIFFTAG_COMPRESSION:
-               v = va_arg(ap, int) & 0xffff;
+               v = va_arg(ap, uint32) & 0xffff;
                /*
-                * If we're changing the compression scheme,
-                * the notify the previous module so that it
-                * can cleanup any state it's setup.
+                * If we're changing the compression scheme, the notify the
+                * previous module so that it can cleanup any state it's
+                * setup.
                 */
                if (TIFFFieldSet(tif, FIELD_COMPRESSION)) {
                        if (td->td_compression == v)
@@ -186,47 +203,21 @@ _TIFFVSetField(TIFF* tif, ttag_t tag, va_list ap)
                td->td_threshholding = (uint16) va_arg(ap, int);
                break;
        case TIFFTAG_FILLORDER:
-               v = va_arg(ap, int);
+               v = va_arg(ap, uint32);
                if (v != FILLORDER_LSB2MSB && v != FILLORDER_MSB2LSB)
                        goto badvalue;
                td->td_fillorder = (uint16) v;
                break;
-       case TIFFTAG_DOCUMENTNAME:
-               _TIFFsetString(&td->td_documentname, va_arg(ap, char*));
-               break;
-       case TIFFTAG_ARTIST:
-               _TIFFsetString(&td->td_artist, va_arg(ap, char*));
-               break;
-       case TIFFTAG_DATETIME:
-               _TIFFsetString(&td->td_datetime, va_arg(ap, char*));
-               break;
-       case TIFFTAG_HOSTCOMPUTER:
-               _TIFFsetString(&td->td_hostcomputer, va_arg(ap, char*));
-               break;
-       case TIFFTAG_IMAGEDESCRIPTION:
-               _TIFFsetString(&td->td_imagedescription, va_arg(ap, char*));
-               break;
-       case TIFFTAG_MAKE:
-               _TIFFsetString(&td->td_make, va_arg(ap, char*));
-               break;
-       case TIFFTAG_MODEL:
-               _TIFFsetString(&td->td_model, va_arg(ap, char*));
-               break;
-       case TIFFTAG_COPYRIGHT:
-               _TIFFsetString(&td->td_copyright, va_arg(ap, char*));
-               break;
        case TIFFTAG_ORIENTATION:
-               v = va_arg(ap, int);
-               if (v < ORIENTATION_TOPLEFT || ORIENTATION_LEFTBOT < v) {
-                       TIFFWarning(tif->tif_name,
-                           "Bad value %ld for \"%s\" tag ignored",
-                           v, _TIFFFieldWithTag(tif, tag)->field_name);
-               } else
+               v = va_arg(ap, uint32);
+               if (v < ORIENTATION_TOPLEFT || ORIENTATION_LEFTBOT < v)
+                       goto badvalue;
+               else
                        td->td_orientation = (uint16) v;
                break;
        case TIFFTAG_SAMPLESPERPIXEL:
                /* XXX should cross check -- e.g. if pallette, then 1 */
-               v = va_arg(ap, int);
+               v = va_arg(ap, uint32);
                if (v == 0)
                        goto badvalue;
                td->td_samplesperpixel = (uint16) v;
@@ -248,34 +239,31 @@ _TIFFVSetField(TIFF* tif, ttag_t tag, va_list ap)
                td->td_maxsamplevalue = (uint16) va_arg(ap, int);
                break;
        case TIFFTAG_SMINSAMPLEVALUE:
-               td->td_sminsamplevalue = (double) va_arg(ap, dblparam_t);
+               td->td_sminsamplevalue = va_arg(ap, double);
                break;
        case TIFFTAG_SMAXSAMPLEVALUE:
-               td->td_smaxsamplevalue = (double) va_arg(ap, dblparam_t);
+               td->td_smaxsamplevalue = va_arg(ap, double);
                break;
        case TIFFTAG_XRESOLUTION:
-               td->td_xresolution = (float) va_arg(ap, dblparam_t);
+               td->td_xresolution = (float) va_arg(ap, double);
                break;
        case TIFFTAG_YRESOLUTION:
-               td->td_yresolution = (float) va_arg(ap, dblparam_t);
+               td->td_yresolution = (float) va_arg(ap, double);
                break;
        case TIFFTAG_PLANARCONFIG:
-               v = va_arg(ap, int);
+               v = va_arg(ap, uint32);
                if (v != PLANARCONFIG_CONTIG && v != PLANARCONFIG_SEPARATE)
                        goto badvalue;
                td->td_planarconfig = (uint16) v;
                break;
-       case TIFFTAG_PAGENAME:
-               _TIFFsetString(&td->td_pagename, va_arg(ap, char*));
-               break;
        case TIFFTAG_XPOSITION:
-               td->td_xposition = (float) va_arg(ap, dblparam_t);
+               td->td_xposition = (float) va_arg(ap, double);
                break;
        case TIFFTAG_YPOSITION:
-               td->td_yposition = (float) va_arg(ap, dblparam_t);
+               td->td_yposition = (float) va_arg(ap, double);
                break;
        case TIFFTAG_RESOLUTIONUNIT:
-               v = va_arg(ap, int);
+               v = va_arg(ap, uint32);
                if (v < RESUNIT_NONE || RESUNIT_CENTIMETER < v)
                        goto badvalue;
                td->td_resolutionunit = (uint16) v;
@@ -310,8 +298,8 @@ _TIFFVSetField(TIFF* tif, ttag_t tag, va_list ap)
                if (v32 % 16) {
                        if (tif->tif_mode != O_RDONLY)
                                goto badvalue32;
-                       TIFFWarning(tif->tif_name,
-                           "Nonstandard tile width %d, convert file", v32);
+                       TIFFWarningExt(tif->tif_clientdata, tif->tif_name,
+                               "Nonstandard tile width %d, convert file", v32);
                }
                td->td_tilewidth = v32;
                tif->tif_flags |= TIFF_ISTILED;
@@ -321,7 +309,7 @@ _TIFFVSetField(TIFF* tif, ttag_t tag, va_list ap)
                if (v32 % 16) {
                        if (tif->tif_mode != O_RDONLY)
                                goto badvalue32;
-                       TIFFWarning(tif->tif_name,
+                       TIFFWarningExt(tif->tif_clientdata, tif->tif_name,
                            "Nonstandard tile length %d, convert file", v32);
                }
                td->td_tilelength = v32;
@@ -334,7 +322,7 @@ _TIFFVSetField(TIFF* tif, ttag_t tag, va_list ap)
                td->td_tiledepth = v32;
                break;
        case TIFFTAG_DATATYPE:
-               v = va_arg(ap, int);
+               v = va_arg(ap, uint32);
                switch (v) {
                case DATATYPE_VOID:     v = SAMPLEFORMAT_VOID;  break;
                case DATATYPE_INT:      v = SAMPLEFORMAT_INT;   break;
@@ -345,7 +333,7 @@ _TIFFVSetField(TIFF* tif, ttag_t tag, va_list ap)
                td->td_sampleformat = (uint16) v;
                break;
        case TIFFTAG_SAMPLEFORMAT:
-               v = va_arg(ap, int);
+               v = va_arg(ap, uint32);
                if (v < SAMPLEFORMAT_UINT || SAMPLEFORMAT_COMPLEXIEEEFP < v)
                        goto badvalue;
                td->td_sampleformat = (uint16) v;
@@ -360,60 +348,22 @@ _TIFFVSetField(TIFF* tif, ttag_t tag, va_list ap)
                          && td->td_bitspersample == 64
                          && tif->tif_postdecode == _TIFFSwab64BitData )
                     tif->tif_postdecode = _TIFFSwab32BitData;
-                else if( td->td_sampleformat == SAMPLEFORMAT_COMPLEXIEEEFP
-                         && td->td_bitspersample == 128
-                         && tif->tif_postdecode == NULL )
-                    tif->tif_postdecode = _TIFFSwab64BitData;
                break;
        case TIFFTAG_IMAGEDEPTH:
                td->td_imagedepth = va_arg(ap, uint32);
                break;
-       case TIFFTAG_STONITS:
-               d = va_arg(ap, dblparam_t);
-               if (d <= 0.)
-                       goto badvaluedbl;
-               td->td_stonits = d;
-               break;
-       /* Begin Pixar Tags */
-       case TIFFTAG_PIXAR_IMAGEFULLWIDTH:
-               td->td_imagefullwidth = va_arg(ap, uint32);
-               break;
-       case TIFFTAG_PIXAR_IMAGEFULLLENGTH:
-               td->td_imagefulllength = va_arg(ap, uint32);
-               break;
-       case TIFFTAG_PIXAR_TEXTUREFORMAT:
-               _TIFFsetString(&td->td_textureformat, va_arg(ap, char*));
-               break;
-       case TIFFTAG_PIXAR_WRAPMODES:
-               _TIFFsetString(&td->td_wrapmodes, va_arg(ap, char*));
-               break;
-       case TIFFTAG_PIXAR_FOVCOT:
-               td->td_fovcot = (float) va_arg(ap, dblparam_t);
-               break;
-       case TIFFTAG_PIXAR_MATRIX_WORLDTOSCREEN:
-               _TIFFsetFloatArray(&td->td_matrixWorldToScreen,
-                       va_arg(ap, float*), 16);
-               break;
-       case TIFFTAG_PIXAR_MATRIX_WORLDTOCAMERA:
-               _TIFFsetFloatArray(&td->td_matrixWorldToCamera,
-                       va_arg(ap, float*), 16);
-               break;
-       /* End Pixar Tags */           
-
        case TIFFTAG_SUBIFD:
                if ((tif->tif_flags & TIFF_INSUBIFD) == 0) {
                        td->td_nsubifd = (uint16) va_arg(ap, int);
                        _TIFFsetLongArray(&td->td_subifd, va_arg(ap, uint32*),
                            (long) td->td_nsubifd);
                } else {
-                       TIFFError(module, "%s: Sorry, cannot nest SubIFDs",
-                                 tif->tif_name);
+                       TIFFErrorExt(tif->tif_clientdata, module,
+                                    "%s: Sorry, cannot nest SubIFDs",
+                                    tif->tif_name);
                        status = 0;
                }
                break;
-       case TIFFTAG_YCBCRCOEFFICIENTS:
-               _TIFFsetFloatArray(&td->td_ycbcrcoeffs, va_arg(ap, float*), 3);
-               break;
        case TIFFTAG_YCBCRPOSITIONING:
                td->td_ycbcrpositioning = (uint16) va_arg(ap, int);
                break;
@@ -421,12 +371,6 @@ _TIFFVSetField(TIFF* tif, ttag_t tag, va_list ap)
                td->td_ycbcrsubsampling[0] = (uint16) va_arg(ap, int);
                td->td_ycbcrsubsampling[1] = (uint16) va_arg(ap, int);
                break;
-       case TIFFTAG_WHITEPOINT:
-               _TIFFsetFloatArray(&td->td_whitepoint, va_arg(ap, float*), 2);
-               break;
-       case TIFFTAG_PRIMARYCHROMATICITIES:
-               _TIFFsetFloatArray(&td->td_primarychromas, va_arg(ap, float*), 6);
-               break;
        case TIFFTAG_TRANSFERFUNCTION:
                v = (td->td_samplesperpixel - td->td_extrasamples) > 1 ? 3 : 1;
                for (i = 0; i < v; i++)
@@ -437,73 +381,35 @@ _TIFFVSetField(TIFF* tif, ttag_t tag, va_list ap)
                /* XXX should check for null range */
                _TIFFsetFloatArray(&td->td_refblackwhite, va_arg(ap, float*), 6);
                break;
-       case TIFFTAG_INKSET:
-               td->td_inkset = (uint16) va_arg(ap, int);
-               break;
-       case TIFFTAG_DOTRANGE:
-               /* XXX should check for null range */
-               td->td_dotrange[0] = (uint16) va_arg(ap, int);
-               td->td_dotrange[1] = (uint16) va_arg(ap, int);
-               break;
        case TIFFTAG_INKNAMES:
-               i = va_arg(ap, int);
+               v = va_arg(ap, uint32);
                s = va_arg(ap, char*);
-               i = checkInkNamesString(tif, i, s);
-                status = i > 0;
-               if( i > 0 ) {
-                       _TIFFsetNString(&td->td_inknames, s, i);
-                       td->td_inknameslen = i;
+               v = checkInkNamesString(tif, v, s);
+                status = v > 0;
+               if( v > 0 ) {
+                       _TIFFsetNString(&td->td_inknames, s, v);
+                       td->td_inknameslen = v;
                }
                break;
-       case TIFFTAG_NUMBEROFINKS:
-               td->td_ninks = (uint16) va_arg(ap, int);
-               break;
-       case TIFFTAG_TARGETPRINTER:
-               _TIFFsetString(&td->td_targetprinter, va_arg(ap, char*));
-               break;
-       case TIFFTAG_ICCPROFILE:
-               td->td_profileLength = (uint32) va_arg(ap, uint32);
-               _TIFFsetByteArray(&td->td_profileData, va_arg(ap, void*),
-                   td->td_profileLength);
-               break;
-       case TIFFTAG_PHOTOSHOP:
-               td->td_photoshopLength = (uint32) va_arg(ap, uint32);
-               _TIFFsetByteArray (&td->td_photoshopData, va_arg(ap, void*),
-                       td->td_photoshopLength);
-               break;
-       case TIFFTAG_RICHTIFFIPTC: 
-               td->td_richtiffiptcLength = (uint32) va_arg(ap, uint32);
-               _TIFFsetLongArray ((uint32**)&td->td_richtiffiptcData,
-                                  va_arg(ap, uint32*),
-                                  td->td_richtiffiptcLength);
-               break;
-       case TIFFTAG_XMLPACKET:
-               td->td_xmlpacketLength = (uint32) va_arg(ap, uint32);
-               _TIFFsetByteArray(&td->td_xmlpacketData, va_arg(ap, void*),
-                   td->td_xmlpacketLength);
-               break;
         default: {
-            const TIFFFieldInfo* fip = _TIFFFindFieldInfo(tif, tag, TIFF_ANY);
             TIFFTagValue *tv;
             int tv_size, iCustom;
+           const TIFFFieldInfo* fip = _TIFFFindFieldInfo(tif, tag, TIFF_ANY);
 
             /*
-             * This can happen if multiple images are open with
-             * different codecs which have private tags.  The
-             * global tag information table may then have tags
-             * that are valid for one file but not the other. 
-             * If the client tries to set a tag that is not valid
-             * for the image's codec then we'll arrive here.  This
-             * happens, for example, when tiffcp is used to convert
-             * between compression schemes and codec-specific tags
-             * are blindly copied.
+            * This can happen if multiple images are open with different
+            * codecs which have private tags.  The global tag information
+            * table may then have tags that are valid for one file but not
+            * the other. If the client tries to set a tag that is not valid
+            * for the image's codec then we'll arrive here.  This
+            * happens, for example, when tiffcp is used to convert between
+            * compression schemes and codec-specific tags are blindly copied.
              */
-            if( fip == NULL || fip->field_bit != FIELD_CUSTOM )
-            {
-               TIFFError(module,
-                   "%s: Invalid %stag \"%s\" (not supported by codec)",
-                   tif->tif_name, isPseudoTag(tag) ? "pseudo-" : "",
-                   _TIFFFieldWithTag(tif, tag)->field_name);
+            if(fip == NULL || fip->field_bit != FIELD_CUSTOM) {
+               TIFFErrorExt(tif->tif_clientdata, module,
+                            "%s: Invalid %stag \"%s\" (not supported by codec)",
+                            tif->tif_name, isPseudoTag(tag) ? "pseudo-" : "",
+                            fip ? fip->field_name : "Unknown");
                status = 0;
                break;
             }
@@ -512,22 +418,21 @@ _TIFFVSetField(TIFF* tif, ttag_t tag, va_list ap)
              * Find the existing entry for this custom value.
              */
             tv = NULL;
-            for( iCustom = 0; iCustom < td->td_customValueCount; iCustom++ )
-            {
-                if( td->td_customValues[iCustom].info == fip )
-                {
-                    tv = td->td_customValues + iCustom;
-                    if( tv->value != NULL )
-                        _TIFFfree( tv->value );
-                    break;
-                }
+            for (iCustom = 0; iCustom < td->td_customValueCount; iCustom++) {
+                   if (td->td_customValues[iCustom].info->field_tag == tag) {
+                           tv = td->td_customValues + iCustom;
+                           if (tv->value != NULL) {
+                                   _TIFFfree(tv->value);
+                                   tv->value = NULL;
+                           }
+                           break;
+                   }
             }
 
             /*
              * Grow the custom list if the entry was not found.
              */
-            if( tv == NULL )
-            {
+            if(tv == NULL) {
                TIFFTagValue    *new_customValues;
                
                td->td_customValueCount++;
@@ -535,7 +440,7 @@ _TIFFVSetField(TIFF* tif, ttag_t tag, va_list ap)
                        _TIFFrealloc(td->td_customValues,
                                     sizeof(TIFFTagValue) * td->td_customValueCount);
                if (!new_customValues) {
-                       TIFFError(module,
+                       TIFFErrorExt(tif->tif_clientdata, module,
                "%s: Failed to allocate space for list of custom values",
                                  tif->tif_name);
                        status = 0;
@@ -544,7 +449,7 @@ _TIFFVSetField(TIFF* tif, ttag_t tag, va_list ap)
 
                td->td_customValues = new_customValues;
 
-                tv = td->td_customValues + (td->td_customValueCount-1);
+                tv = td->td_customValues + (td->td_customValueCount - 1);
                 tv->info = fip;
                 tv->value = NULL;
                 tv->count = 0;
@@ -553,106 +458,148 @@ _TIFFVSetField(TIFF* tif, ttag_t tag, va_list ap)
             /*
              * Set custom value ... save a copy of the custom tag value.
              */
-           switch (fip->field_type) {
-                   /*
-                    * XXX: We can't use TIFFDataWidth() to determine the
-                    * space needed to store the value. For TIFF_RATIONAL
-                    * values TIFFDataWidth() returns 8, but we use 4-byte
-                    * float to represent rationals.
-                    */
-                   case TIFF_BYTE:
-                   case TIFF_ASCII:
-                   case TIFF_SBYTE:
-                   case TIFF_UNDEFINED:
-                       tv_size = 1;
-                       break;
-
-                   case TIFF_SHORT:
-                   case TIFF_SSHORT:
-                       tv_size = 2;
-                       break;
-
-                   case TIFF_LONG:
-                   case TIFF_SLONG:
-                   case TIFF_FLOAT:
-                   case TIFF_IFD:
-                   case TIFF_RATIONAL:
-                   case TIFF_SRATIONAL:
-                       tv_size = 4;
-                       break;
-
-                   case TIFF_DOUBLE:
-                       tv_size = 8;
-                       break;
-
-                   default:
-                       status = 0;
-                       TIFFError(module, "%s: Bad field type %d for \"%s\"",
-                                 tif->tif_name, fip->field_type,
-                                 fip->field_name);
-                       goto end;
-                   
+           tv_size = _TIFFDataSize(fip->field_type);
+           if (tv_size == 0) {
+                   status = 0;
+                   TIFFErrorExt(tif->tif_clientdata, module,
+                                "%s: Bad field type %d for \"%s\"",
+                                tif->tif_name, fip->field_type,
+                                fip->field_name);
+                   goto end;
            }
+           
+            if(fip->field_passcount) {
+                   if (fip->field_writecount == TIFF_VARIABLE2)
+                       tv->count = (uint32) va_arg(ap, uint32);
+                   else
+                       tv->count = (int) va_arg(ap, int);
+           } else if (fip->field_writecount == TIFF_VARIABLE
+                      || fip->field_writecount == TIFF_VARIABLE2)
+               tv->count = 1;
+           else if (fip->field_writecount == TIFF_SPP)
+               tv->count = td->td_samplesperpixel;
+           else
+                tv->count = fip->field_writecount;
             
-            if(fip->field_passcount)
-                tv->count = (int) va_arg(ap, int);
-            else
-                tv->count = 1;
-            
-           if (fip->field_passcount) {
-                tv->value = _TIFFmalloc(tv_size * tv->count);
-               if ( !tv->value ) {
-                       status = 0;
-                       goto end;
-               }
-                _TIFFmemcpy(tv->value, (void *) va_arg(ap,void*),
-                            tv->count * tv_size);
-            } else if (fip->field_type == TIFF_ASCII) {
-                const char *value = (const char *) va_arg(ap,const char *);
-                tv->count = strlen(value)+1;
-                tv->value = _TIFFmalloc(tv->count);
+    
+           if (fip->field_type == TIFF_ASCII)
+                   _TIFFsetString((char **)&tv->value, va_arg(ap, char *));
+           else {
+               tv->value = _TIFFCheckMalloc(tif, tv_size, tv->count,
+                                            "Tag Value");
                if (!tv->value) {
-                       status = 0;
-                       goto end;
+                   status = 0;
+                   goto end;
                }
-                strcpy(tv->value, value);
-            } else {
-                /* not supporting "pass by value" types yet */
-               TIFFError(module,
-                         "%s: Pass by value is not implemented.",
-                         tif->tif_name);
 
-                tv->value = _TIFFmalloc(tv_size * tv->count);
-               if (!tv->value) {
-                       status = 0;
-                       goto end;
+               if ((fip->field_passcount
+                   || fip->field_writecount == TIFF_VARIABLE
+                   || fip->field_writecount == TIFF_VARIABLE2
+                   || fip->field_writecount == TIFF_SPP
+                   || tv->count > 1)
+                   && fip->field_tag != TIFFTAG_PAGENUMBER
+                   && fip->field_tag != TIFFTAG_HALFTONEHINTS
+                   && fip->field_tag != TIFFTAG_YCBCRSUBSAMPLING
+                   && fip->field_tag != TIFFTAG_DOTRANGE) {
+                    _TIFFmemcpy(tv->value, va_arg(ap, void *),
+                               tv->count * tv_size);
+               } else {
+                   /*
+                    * XXX: The following loop required to handle
+                    * TIFFTAG_PAGENUMBER, TIFFTAG_HALFTONEHINTS,
+                    * TIFFTAG_YCBCRSUBSAMPLING and TIFFTAG_DOTRANGE tags.
+                    * These tags are actually arrays and should be passed as
+                    * array pointers to TIFFSetField() function, but actually
+                    * passed as a list of separate values. This behaviour
+                    * must be changed in the future!
+                    */
+                   int i;
+                   char *val = (char *)tv->value;
+
+                   for (i = 0; i < tv->count; i++, val += tv_size) {
+                           switch (fip->field_type) {
+                               case TIFF_BYTE:
+                               case TIFF_UNDEFINED:
+                                   {
+                                       uint8 v = (uint8)va_arg(ap, int);
+                                       _TIFFmemcpy(val, &v, tv_size);
+                                   }
+                                   break;
+                               case TIFF_SBYTE:
+                                   {
+                                       int8 v = (int8)va_arg(ap, int);
+                                       _TIFFmemcpy(val, &v, tv_size);
+                                   }
+                                   break;
+                               case TIFF_SHORT:
+                                   {
+                                       uint16 v = (uint16)va_arg(ap, int);
+                                       _TIFFmemcpy(val, &v, tv_size);
+                                   }
+                                   break;
+                               case TIFF_SSHORT:
+                                   {
+                                       int16 v = (int16)va_arg(ap, int);
+                                       _TIFFmemcpy(val, &v, tv_size);
+                                   }
+                                   break;
+                               case TIFF_LONG:
+                               case TIFF_IFD:
+                                   {
+                                       uint32 v = va_arg(ap, uint32);
+                                       _TIFFmemcpy(val, &v, tv_size);
+                                   }
+                                   break;
+                               case TIFF_SLONG:
+                                   {
+                                       int32 v = va_arg(ap, int32);
+                                       _TIFFmemcpy(val, &v, tv_size);
+                                   }
+                                   break;
+                               case TIFF_RATIONAL:
+                               case TIFF_SRATIONAL:
+                               case TIFF_FLOAT:
+                                   {
+                                       float v = (float)va_arg(ap, double);
+                                       _TIFFmemcpy(val, &v, tv_size);
+                                   }
+                                   break;
+                               case TIFF_DOUBLE:
+                                   {
+                                       double v = va_arg(ap, double);
+                                       _TIFFmemcpy(val, &v, tv_size);
+                                   }
+                                   break;
+                               default:
+                                   _TIFFmemset(val, 0, tv_size);
+                                   status = 0;
+                                   break;
+                           }
+                   }
                }
-                _TIFFmemset(tv->value, 0, tv->count * tv_size);
-                status = 0;
-            }
+           }
           }
        }
        if (status) {
-            TIFFSetFieldBit(tif, _TIFFFieldWithTag(tif, tag)->field_bit);
-            tif->tif_flags |= TIFF_DIRTYDIRECT;
+               TIFFSetFieldBit(tif, _TIFFFieldWithTag(tif, tag)->field_bit);
+               tif->tif_flags |= TIFF_DIRTYDIRECT;
        }
 
 end:
        va_end(ap);
        return (status);
 badvalue:
-       TIFFError(module, "%s: Bad value %d for \"%s\"",
-                 tif->tif_name, v, _TIFFFieldWithTag(tif, tag)->field_name);
+       TIFFErrorExt(tif->tif_clientdata, module,
+                    "%s: Bad value %d for \"%s\" tag",
+                    tif->tif_name, v,
+                    _TIFFFieldWithTag(tif, tag)->field_name);
        va_end(ap);
        return (0);
 badvalue32:
-       TIFFError(module, "%s: Bad value %ld for \"%s\"",
-                  tif->tif_name, v32, _TIFFFieldWithTag(tif, tag)->field_name);
-       va_end(ap);
-       return (0);
-badvaluedbl:
-       TIFFError(module, "%s: Bad value %f for \"%s\"",
-                 tif->tif_name, d, _TIFFFieldWithTag(tif, tag)->field_name);
+       TIFFErrorExt(tif->tif_clientdata, module,
+                    "%s: Bad value %u for \"%s\" tag",
+                    tif->tif_name, v32,
+                    _TIFFFieldWithTag(tif, tag)->field_name);
        va_end(ap);
        return (0);
 }
@@ -671,7 +618,7 @@ OkToChangeTag(TIFF* tif, ttag_t tag)
 {
        const TIFFFieldInfo* fip = _TIFFFindFieldInfo(tif, tag, TIFF_ANY);
        if (!fip) {                     /* unknown tag */
-               TIFFError("TIFFSetField", "%s: Unknown %stag %u",
+               TIFFErrorExt(tif->tif_clientdata, "TIFFSetField", "%s: Unknown %stag %u",
                    tif->tif_name, isPseudoTag(tag) ? "pseudo-" : "", tag);
                return (0);
        }
@@ -683,7 +630,7 @@ OkToChangeTag(TIFF* tif, ttag_t tag)
                 * to those tags that don't/shouldn't affect the
                 * compression and/or format of the data.
                 */
-               TIFFError("TIFFSetField",
+               TIFFErrorExt(tif->tif_clientdata, "TIFFSetField",
                    "%s: Cannot modify tag \"%s\" while writing",
                    tif->tif_name, fip->field_name);
                return (0);
@@ -754,30 +701,6 @@ _TIFFVGetField(TIFF* tif, ttag_t tag, va_list ap)
        case TIFFTAG_FILLORDER:
             *va_arg(ap, uint16*) = td->td_fillorder;
             break;
-       case TIFFTAG_DOCUMENTNAME:
-            *va_arg(ap, char**) = td->td_documentname;
-            break;
-       case TIFFTAG_ARTIST:
-            *va_arg(ap, char**) = td->td_artist;
-            break;
-       case TIFFTAG_DATETIME:
-            *va_arg(ap, char**) = td->td_datetime;
-            break;
-       case TIFFTAG_HOSTCOMPUTER:
-            *va_arg(ap, char**) = td->td_hostcomputer;
-            break;
-       case TIFFTAG_IMAGEDESCRIPTION:
-            *va_arg(ap, char**) = td->td_imagedescription;
-            break;
-       case TIFFTAG_MAKE:
-            *va_arg(ap, char**) = td->td_make;
-            break;
-       case TIFFTAG_MODEL:
-            *va_arg(ap, char**) = td->td_model;
-            break;
-       case TIFFTAG_COPYRIGHT:
-            *va_arg(ap, char**) = td->td_copyright;
-            break;
        case TIFFTAG_ORIENTATION:
             *va_arg(ap, uint16*) = td->td_orientation;
             break;
@@ -814,9 +737,6 @@ _TIFFVGetField(TIFF* tif, ttag_t tag, va_list ap)
        case TIFFTAG_YPOSITION:
             *va_arg(ap, float*) = td->td_yposition;
             break;
-       case TIFFTAG_PAGENAME:
-            *va_arg(ap, char**) = td->td_pagename;
-            break;
        case TIFFTAG_RESOLUTIONUNIT:
             *va_arg(ap, uint16*) = td->td_resolutionunit;
             break;
@@ -881,16 +801,10 @@ _TIFFVGetField(TIFF* tif, ttag_t tag, va_list ap)
        case TIFFTAG_IMAGEDEPTH:
             *va_arg(ap, uint32*) = td->td_imagedepth;
             break;
-       case TIFFTAG_STONITS:
-            *va_arg(ap, double*) = td->td_stonits;
-            break;
        case TIFFTAG_SUBIFD:
             *va_arg(ap, uint16*) = td->td_nsubifd;
             *va_arg(ap, uint32**) = td->td_subifd;
             break;
-       case TIFFTAG_YCBCRCOEFFICIENTS:
-            *va_arg(ap, float**) = td->td_ycbcrcoeffs;
-            break;
        case TIFFTAG_YCBCRPOSITIONING:
             *va_arg(ap, uint16*) = td->td_ycbcrpositioning;
             break;
@@ -898,12 +812,6 @@ _TIFFVGetField(TIFF* tif, ttag_t tag, va_list ap)
             *va_arg(ap, uint16*) = td->td_ycbcrsubsampling[0];
             *va_arg(ap, uint16*) = td->td_ycbcrsubsampling[1];
             break;
-       case TIFFTAG_WHITEPOINT:
-            *va_arg(ap, float**) = td->td_whitepoint;
-            break;
-       case TIFFTAG_PRIMARYCHROMATICITIES:
-            *va_arg(ap, float**) = td->td_primarychromas;
-            break;
        case TIFFTAG_TRANSFERFUNCTION:
             *va_arg(ap, uint16**) = td->td_transferfunction[0];
             if (td->td_samplesperpixel - td->td_extrasamples > 1) {
@@ -912,93 +820,40 @@ _TIFFVGetField(TIFF* tif, ttag_t tag, va_list ap)
             }
             break;
        case TIFFTAG_REFERENCEBLACKWHITE:
-            *va_arg(ap, float**) = td->td_refblackwhite;
-            break;
-       case TIFFTAG_INKSET:
-            *va_arg(ap, uint16*) = td->td_inkset;
-            break;
-       case TIFFTAG_DOTRANGE:
-            *va_arg(ap, uint16*) = td->td_dotrange[0];
-            *va_arg(ap, uint16*) = td->td_dotrange[1];
-            break;
+           *va_arg(ap, float**) = td->td_refblackwhite;
+           break;
        case TIFFTAG_INKNAMES:
             *va_arg(ap, char**) = td->td_inknames;
             break;
-       case TIFFTAG_NUMBEROFINKS:
-            *va_arg(ap, uint16*) = td->td_ninks;
-            break;
-       case TIFFTAG_TARGETPRINTER:
-            *va_arg(ap, char**) = td->td_targetprinter;
-            break;
-       case TIFFTAG_ICCPROFILE:
-            *va_arg(ap, uint32*) = td->td_profileLength;
-            *va_arg(ap, void**) = td->td_profileData;
-            break;
-       case TIFFTAG_PHOTOSHOP:
-            *va_arg(ap, uint32*) = td->td_photoshopLength;
-            *va_arg(ap, void**) = td->td_photoshopData;
-            break;
-       case TIFFTAG_RICHTIFFIPTC:
-            *va_arg(ap, uint32*) = td->td_richtiffiptcLength;
-            *va_arg(ap, void**) = td->td_richtiffiptcData;
-            break;
-       case TIFFTAG_XMLPACKET:
-            *va_arg(ap, uint32*) = td->td_xmlpacketLength;
-            *va_arg(ap, void**) = td->td_xmlpacketData;
-            break;
-            /* Begin Pixar Tags */
-       case TIFFTAG_PIXAR_IMAGEFULLWIDTH:
-            *va_arg(ap, uint32*) = td->td_imagefullwidth;
-            break;
-       case TIFFTAG_PIXAR_IMAGEFULLLENGTH:
-            *va_arg(ap, uint32*) = td->td_imagefulllength;
-            break;
-       case TIFFTAG_PIXAR_TEXTUREFORMAT:
-            *va_arg(ap, char**) = td->td_textureformat;
-            break;
-       case TIFFTAG_PIXAR_WRAPMODES:
-            *va_arg(ap, char**) = td->td_wrapmodes;
-            break;
-       case TIFFTAG_PIXAR_FOVCOT:
-            *va_arg(ap, float*) = td->td_fovcot;
-            break;
-       case TIFFTAG_PIXAR_MATRIX_WORLDTOSCREEN:
-            *va_arg(ap, float**) = td->td_matrixWorldToScreen;
-            break;
-       case TIFFTAG_PIXAR_MATRIX_WORLDTOCAMERA:
-            *va_arg(ap, float**) = td->td_matrixWorldToCamera;
-            break;
-            /* End Pixar Tags */
-
         default:
         {
             const TIFFFieldInfo* fip = _TIFFFindFieldInfo(tif, tag, TIFF_ANY);
             int           i;
             
             /*
-             * This can happen if multiple images are open with
-             * different codecs which have private tags.  The
-             * global tag information table may then have tags
-             * that are valid for one file but not the other. 
-             * If the client tries to get a tag that is not valid
-             * for the image's codec then we'll arrive here.
+            * This can happen if multiple images are open with different
+            * codecs which have private tags.  The global tag information
+            * table may then have tags that are valid for one file but not
+            * the other. If the client tries to get a tag that is not valid
+            * for the image's codec then we'll arrive here.
              */
             if( fip == NULL || fip->field_bit != FIELD_CUSTOM )
             {
-                TIFFError("_TIFFVGetField",
-                          "%s: Invalid %stag \"%s\" (not supported by codec)",
-                          tif->tif_name, isPseudoTag(tag) ? "pseudo-" : "",
-                          _TIFFFieldWithTag(tif, tag)->field_name);
-                ret_val = 0;
-                break;
+                   TIFFErrorExt(tif->tif_clientdata, "_TIFFVGetField",
+                                "%s: Invalid %stag \"%s\" "
+                                "(not supported by codec)",
+                                tif->tif_name,
+                                isPseudoTag(tag) ? "pseudo-" : "",
+                                fip ? fip->field_name : "Unknown");
+                   ret_val = 0;
+                   break;
             }
 
             /*
-            ** Do we have a custom value?
-            */
+            * Do we have a custom value?
+            */
             ret_val = 0;
-            for (i = 0; i < td->td_customValueCount; i++)
-            {
+            for (i = 0; i < td->td_customValueCount; i++) {
                TIFFTagValue *tv = td->td_customValues + i;
 
                if (tv->info->field_tag != tag)
@@ -1011,17 +866,77 @@ _TIFFVGetField(TIFF* tif, ttag_t tag, va_list ap)
                                *va_arg(ap, uint16*) = (uint16)tv->count;
                        *va_arg(ap, void **) = tv->value;
                        ret_val = 1;
-                       break;
-                } else if (fip->field_type == TIFF_ASCII) {
-                       *va_arg(ap, void **) = tv->value;
-                       ret_val = 1;
-                       break;
                 } else {
-                       TIFFError("_TIFFVGetField",
-                                 "%s: Pass by value is not implemented.",
-                                 tif->tif_name);
-                       break;
+                       if ((fip->field_type == TIFF_ASCII
+                           || fip->field_readcount == TIFF_VARIABLE
+                           || fip->field_readcount == TIFF_VARIABLE2
+                           || fip->field_readcount == TIFF_SPP
+                           || tv->count > 1)
+                           && fip->field_tag != TIFFTAG_PAGENUMBER
+                           && fip->field_tag != TIFFTAG_HALFTONEHINTS
+                           && fip->field_tag != TIFFTAG_YCBCRSUBSAMPLING
+                           && fip->field_tag != TIFFTAG_DOTRANGE) {
+                               *va_arg(ap, void **) = tv->value;
+                               ret_val = 1;
+                       } else {
+                           int j;
+                           char *val = (char *)tv->value;
+
+                           for (j = 0; j < tv->count;
+                                j++, val += _TIFFDataSize(tv->info->field_type)) {
+                               switch (fip->field_type) {
+                                       case TIFF_BYTE:
+                                       case TIFF_UNDEFINED:
+                                               *va_arg(ap, uint8*) =
+                                                       *(uint8 *)val;
+                                               ret_val = 1;
+                                               break;
+                                       case TIFF_SBYTE:
+                                               *va_arg(ap, int8*) =
+                                                       *(int8 *)val;
+                                               ret_val = 1;
+                                               break;
+                                       case TIFF_SHORT:
+                                               *va_arg(ap, uint16*) =
+                                                       *(uint16 *)val;
+                                               ret_val = 1;
+                                               break;
+                                       case TIFF_SSHORT:
+                                               *va_arg(ap, int16*) =
+                                                       *(int16 *)val;
+                                               ret_val = 1;
+                                               break;
+                                       case TIFF_LONG:
+                                       case TIFF_IFD:
+                                               *va_arg(ap, uint32*) =
+                                                       *(uint32 *)val;
+                                               ret_val = 1;
+                                               break;
+                                       case TIFF_SLONG:
+                                               *va_arg(ap, int32*) =
+                                                       *(int32 *)val;
+                                               ret_val = 1;
+                                               break;
+                                       case TIFF_RATIONAL:
+                                       case TIFF_SRATIONAL:
+                                       case TIFF_FLOAT:
+                                               *va_arg(ap, float*) =
+                                                       *(float *)val;
+                                               ret_val = 1;
+                                               break;
+                                       case TIFF_DOUBLE:
+                                               *va_arg(ap, double*) =
+                                                       *(double *)val;
+                                               ret_val = 1;
+                                               break;
+                                       default:
+                                               ret_val = 0;
+                                               break;
+                               }
+                           }
+                       }
                 }
+               break;
             }
         }
     }
@@ -1074,41 +989,21 @@ TIFFFreeDirectory(TIFF* tif)
        TIFFDirectory *td = &tif->tif_dir;
        int            i;
 
+       _TIFFmemset(td->td_fieldsset, 0, FIELD_SETLONGS);
        CleanupField(td_colormap[0]);
        CleanupField(td_colormap[1]);
        CleanupField(td_colormap[2]);
-       CleanupField(td_documentname);
-       CleanupField(td_artist);
-       CleanupField(td_datetime);
-       CleanupField(td_hostcomputer);
-       CleanupField(td_imagedescription);
-       CleanupField(td_make);
-       CleanupField(td_model);
-       CleanupField(td_copyright);
-       CleanupField(td_pagename);
        CleanupField(td_sampleinfo);
        CleanupField(td_subifd);
-       CleanupField(td_ycbcrcoeffs);
        CleanupField(td_inknames);
-       CleanupField(td_targetprinter);
-       CleanupField(td_whitepoint);
-       CleanupField(td_primarychromas);
        CleanupField(td_refblackwhite);
        CleanupField(td_transferfunction[0]);
        CleanupField(td_transferfunction[1]);
        CleanupField(td_transferfunction[2]);
-       CleanupField(td_profileData);
-       CleanupField(td_photoshopData);
-       CleanupField(td_richtiffiptcData);
-       CleanupField(td_xmlpacketData);
        CleanupField(td_stripoffset);
        CleanupField(td_stripbytecount);
-       /* Begin Pixar Tags */
-       CleanupField(td_textureformat);
-       CleanupField(td_wrapmodes);
-       CleanupField(td_matrixWorldToScreen);
-       CleanupField(td_matrixWorldToCamera);
-       /* End Pixar Tags */
+       TIFFClrFieldBit(tif, FIELD_YCBCRSUBSAMPLING);
+       TIFFClrFieldBit(tif, FIELD_YCBCRPOSITIONING);
 
        /* Cleanup custom tag values */
        for( i = 0; i < td->td_customValueCount; i++ ) {
@@ -1162,7 +1057,11 @@ TIFFDefaultDirectory(TIFF* tif)
 {
        register TIFFDirectory* td = &tif->tif_dir;
 
-       _TIFFSetupFieldInfo(tif);
+       size_t tiffFieldInfoCount;
+       const TIFFFieldInfo *tiffFieldInfo =
+           _TIFFGetFieldInfo(&tiffFieldInfoCount);
+       _TIFFSetupFieldInfo(tif, tiffFieldInfo, tiffFieldInfoCount);
+
        _TIFFmemset(td, 0, sizeof (*td));
        td->td_fillorder = FILLORDER_MSB2LSB;
        td->td_bitspersample = 1;
@@ -1180,10 +1079,8 @@ TIFFDefaultDirectory(TIFF* tif)
        td->td_ycbcrsubsampling[0] = 2;
        td->td_ycbcrsubsampling[1] = 2;
        td->td_ycbcrpositioning = YCBCRPOSITION_CENTERED;
-       td->td_inkset = INKSET_CMYK;
-       td->td_ninks = 4;
        tif->tif_postdecode = _TIFFNoPostDecode;
-        tif->tif_foundfield = NULL;
+       tif->tif_foundfield = NULL;
        tif->tif_tagmethods.vsetfield = _TIFFVSetField;
        tif->tif_tagmethods.vgetfield = _TIFFVGetField;
        tif->tif_tagmethods.printdir = NULL;
@@ -1204,12 +1101,17 @@ TIFFDefaultDirectory(TIFF* tif)
         */
        tif->tif_flags &= ~TIFF_DIRTYDIRECT;
 
+       /*
+        * As per http://bugzilla.remotesensing.org/show_bug.cgi?id=19
+        * we clear the ISTILED flag when setting up a new directory.
+        * Should we also be clearing stuff like INSUBIFD?
+        */
+       tif->tif_flags &= ~TIFF_ISTILED;
         /*
-         * As per http://bugzilla.remotesensing.org/show_bug.cgi?id=19
-         * we clear the ISTILED flag when setting up a new directory.
-         * Should we also be clearing stuff like INSUBIFD?
+         * Clear other directory-specific fields.
          */
-        tif->tif_flags &= ~TIFF_ISTILED;
+        tif->tif_tilesize = -1;
+        tif->tif_scanlinesize = -1;
 
        return (1);
 }
@@ -1217,59 +1119,59 @@ TIFFDefaultDirectory(TIFF* tif)
 static int
 TIFFAdvanceDirectory(TIFF* tif, uint32* nextdir, toff_t* off)
 {
-    static const char module[] = "TIFFAdvanceDirectory";
-    uint16 dircount;
-    if (isMapped(tif))
-    {
-        toff_t poff=*nextdir;
-        if (poff+sizeof(uint16) > tif->tif_size)
-        {
-            TIFFError(module, "%s: Error fetching directory count",
-                      tif->tif_name);
-            return (0);
-        }
-        _TIFFmemcpy(&dircount, tif->tif_base+poff, sizeof (uint16));
-        if (tif->tif_flags & TIFF_SWAB)
-            TIFFSwabShort(&dircount);
-        poff+=sizeof (uint16)+dircount*sizeof (TIFFDirEntry);
-        if (off != NULL)
-            *off = poff;
-        if (((toff_t) (poff+sizeof (uint32))) > tif->tif_size)
-        {
-            TIFFError(module, "%s: Error fetching directory link",
-                      tif->tif_name);
-            return (0);
-        }
-        _TIFFmemcpy(nextdir, tif->tif_base+poff, sizeof (uint32));
-        if (tif->tif_flags & TIFF_SWAB)
-            TIFFSwabLong(nextdir);
-        return (1);
-    }
-    else
-    {
-        if (!SeekOK(tif, *nextdir) ||
-            !ReadOK(tif, &dircount, sizeof (uint16))) {
-            TIFFError(module, "%s: Error fetching directory count",
-                      tif->tif_name);
-            return (0);
-        }
-        if (tif->tif_flags & TIFF_SWAB)
-            TIFFSwabShort(&dircount);
-        if (off != NULL)
-            *off = TIFFSeekFile(tif,
-                                dircount*sizeof (TIFFDirEntry), SEEK_CUR);
-        else
-            (void) TIFFSeekFile(tif,
-                                dircount*sizeof (TIFFDirEntry), SEEK_CUR);
-        if (!ReadOK(tif, nextdir, sizeof (uint32))) {
-            TIFFError(module, "%s: Error fetching directory link",
-                      tif->tif_name);
-            return (0);
-        }
-        if (tif->tif_flags & TIFF_SWAB)
-            TIFFSwabLong(nextdir);
-        return (1);
-    }
+       static const char module[] = "TIFFAdvanceDirectory";
+       uint16 dircount;
+       if (isMapped(tif))
+       {
+               toff_t poff=*nextdir;
+               if (poff+sizeof(uint16) > tif->tif_size)
+               {
+                       TIFFErrorExt(tif->tif_clientdata, module, "%s: Error fetching directory count",
+                           tif->tif_name);
+                       return (0);
+               }
+               _TIFFmemcpy(&dircount, tif->tif_base+poff, sizeof (uint16));
+               if (tif->tif_flags & TIFF_SWAB)
+                       TIFFSwabShort(&dircount);
+               poff+=sizeof (uint16)+dircount*sizeof (TIFFDirEntry);
+               if (off != NULL)
+                       *off = poff;
+               if (((toff_t) (poff+sizeof (uint32))) > tif->tif_size)
+               {
+                       TIFFErrorExt(tif->tif_clientdata, module, "%s: Error fetching directory link",
+                           tif->tif_name);
+                       return (0);
+               }
+               _TIFFmemcpy(nextdir, tif->tif_base+poff, sizeof (uint32));
+               if (tif->tif_flags & TIFF_SWAB)
+                       TIFFSwabLong(nextdir);
+               return (1);
+       }
+       else
+       {
+               if (!SeekOK(tif, *nextdir) ||
+                   !ReadOK(tif, &dircount, sizeof (uint16))) {
+                       TIFFErrorExt(tif->tif_clientdata, module, "%s: Error fetching directory count",
+                           tif->tif_name);
+                       return (0);
+               }
+               if (tif->tif_flags & TIFF_SWAB)
+                       TIFFSwabShort(&dircount);
+               if (off != NULL)
+                       *off = TIFFSeekFile(tif,
+                           dircount*sizeof (TIFFDirEntry), SEEK_CUR);
+               else
+                       (void) TIFFSeekFile(tif,
+                           dircount*sizeof (TIFFDirEntry), SEEK_CUR);
+               if (!ReadOK(tif, nextdir, sizeof (uint32))) {
+                       TIFFErrorExt(tif->tif_clientdata, module, "%s: Error fetching directory link",
+                           tif->tif_name);
+                       return (0);
+               }
+               if (tif->tif_flags & TIFF_SWAB)
+                       TIFFSwabLong(nextdir);
+               return (1);
+       }
 }
 
 /*
@@ -1364,7 +1266,8 @@ TIFFUnlinkDirectory(TIFF* tif, tdir_t dirn)
        tdir_t n;
 
        if (tif->tif_mode == O_RDONLY) {
-               TIFFError(module, "Can not unlink directory in read-only file");
+               TIFFErrorExt(tif->tif_clientdata, module,
+                             "Can not unlink directory in read-only file");
                return (0);
        }
        /*
@@ -1376,7 +1279,7 @@ TIFFUnlinkDirectory(TIFF* tif, tdir_t dirn)
        off = sizeof (uint16) + sizeof (uint16);
        for (n = dirn-1; n > 0; n--) {
                if (nextdir == 0) {
-                       TIFFError(module, "Directory %d does not exist", dirn);
+                       TIFFErrorExt(tif->tif_clientdata, module, "Directory %d does not exist", dirn);
                        return (0);
                }
                if (!TIFFAdvanceDirectory(tif, &nextdir, &off))
@@ -1397,7 +1300,7 @@ TIFFUnlinkDirectory(TIFF* tif, tdir_t dirn)
        if (tif->tif_flags & TIFF_SWAB)
                TIFFSwabLong(&nextdir);
        if (!WriteOK(tif, &nextdir, sizeof (uint32))) {
-               TIFFError(module, "Error writing directory link");
+               TIFFErrorExt(tif->tif_clientdata, module, "Error writing directory link");
                return (0);
        }
        /*
@@ -1477,3 +1380,10 @@ TIFFReassignTagToIgnore (enum TIFFIgnoreSense task, int TIFFtagID)
 }
 
 /* vim: set ts=8 sts=8 sw=8 noet: */
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
index f3d7fa9..515af19 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: tif_dir.h,v 1.1 2005-06-17 13:54:52 vp153 Exp $ */
+/* $Id: tif_dir.h,v 1.30.2.3 2010-06-09 21:15:27 bfriesen Exp $ */
 
 /*
  * Copyright (c) 1988-1997 Sam Leffler
@@ -38,81 +38,49 @@ typedef     struct {
        /* bit vector of fields that are set */
        unsigned long   td_fieldsset[FIELD_SETLONGS];
 
-       uint32  td_imagewidth, td_imagelength, td_imagedepth;
-       uint32  td_tilewidth, td_tilelength, td_tiledepth;
-       uint32  td_subfiletype;
-       uint16  td_bitspersample;
-       uint16  td_sampleformat;
-       uint16  td_compression;
-       uint16  td_photometric;
-       uint16  td_threshholding;
-       uint16  td_fillorder;
-       uint16  td_orientation;
-       uint16  td_samplesperpixel;
-       uint32  td_rowsperstrip;
-       uint16  td_minsamplevalue, td_maxsamplevalue;
-       double  td_sminsamplevalue, td_smaxsamplevalue;
-       float   td_xresolution, td_yresolution;
-       uint16  td_resolutionunit;
-       uint16  td_planarconfig;
-       float   td_xposition, td_yposition;
-       uint16  td_pagenumber[2];
-       uint16* td_colormap[3];
-       uint16  td_halftonehints[2];
-       uint16  td_extrasamples;
-       uint16* td_sampleinfo;
-       double  td_stonits;
-       char*   td_documentname;
-       char*   td_artist;
-       char*   td_datetime;
-       char*   td_hostcomputer;
-       char*   td_imagedescription;
-       char*   td_make;
-       char*   td_model;
-        char*   td_copyright;
-       char*   td_pagename;
-       tstrip_t td_stripsperimage;
-       tstrip_t td_nstrips;            /* size of offset & bytecount arrays */
-       uint32* td_stripoffset;
-       uint32* td_stripbytecount;
-       int     td_stripbytecountsorted; /* is the bytecount array sorted ascending? */
-       uint16  td_nsubifd;
-       uint32* td_subifd;
+       uint32  td_imagewidth, td_imagelength, td_imagedepth;
+       uint32  td_tilewidth, td_tilelength, td_tiledepth;
+       uint32  td_subfiletype;
+       uint16  td_bitspersample;
+       uint16  td_sampleformat;
+       uint16  td_compression;
+       uint16  td_photometric;
+       uint16  td_threshholding;
+       uint16  td_fillorder;
+       uint16  td_orientation;
+       uint16  td_samplesperpixel;
+       uint32  td_rowsperstrip;
+       uint16  td_minsamplevalue, td_maxsamplevalue;
+       double  td_sminsamplevalue, td_smaxsamplevalue;
+       float   td_xresolution, td_yresolution;
+       uint16  td_resolutionunit;
+       uint16  td_planarconfig;
+       float   td_xposition, td_yposition;
+       uint16  td_pagenumber[2];
+       uint16* td_colormap[3];
+       uint16  td_halftonehints[2];
+       uint16  td_extrasamples;
+       uint16* td_sampleinfo;
+       /* even though the name is misleading, td_stripsperimage is the number
+        * of striles (=strips or tiles) per plane, and td_nstrips the total
+        * number of striles */
+       tstrile_t td_stripsperimage;
+       tstrile_t td_nstrips;            /* size of offset & bytecount arrays */
+       toff_t* td_stripoffset;
+       toff_t* td_stripbytecount;       /* FIXME: it should be tsize_t array */
+       int     td_stripbytecountsorted; /* is the bytecount array sorted ascending? */
+       uint16  td_nsubifd;
+       uint32* td_subifd;
        /* YCbCr parameters */
-       float*  td_ycbcrcoeffs;
-       uint16  td_ycbcrsubsampling[2];
-       uint16  td_ycbcrpositioning;
+       uint16  td_ycbcrsubsampling[2];
+       uint16  td_ycbcrpositioning;
        /* Colorimetry parameters */
-       float*  td_whitepoint;
-       float*  td_primarychromas;
        float*  td_refblackwhite;
-       uint16* td_transferfunction[3];
+       uint16* td_transferfunction[3];
        /* CMYK parameters */
-       uint16  td_inkset;
-       uint16  td_ninks;
-       uint16  td_dotrange[2];
-       int     td_inknameslen;
-       char*   td_inknames;
-       char*   td_targetprinter;
-       /* ICC parameters */
-       uint32  td_profileLength;
-       void    *td_profileData;
-       /* Adobe Photoshop tag handling */
-       uint32  td_photoshopLength;
-       void    *td_photoshopData;
-       /* IPTC parameters */
-       uint32  td_richtiffiptcLength;
-       void    *td_richtiffiptcData;
-        /* Begin Pixar Tag values. */
-        uint32 td_imagefullwidth, td_imagefulllength;
-       char*   td_textureformat;
-       char*   td_wrapmodes;
-       float   td_fovcot;
-       float*  td_matrixWorldToScreen;
-       float*  td_matrixWorldToCamera;
-       /* End Pixar Tag Values. */
-       uint32  td_xmlpacketLength;
-       void    *td_xmlpacketData;
+       int     td_inknameslen;
+       char*   td_inknames;
+
        int     td_customValueCount;
         TIFFTagValue *td_customValues;
 } TIFFDirectory;
@@ -146,26 +114,17 @@ typedef   struct {
 #define        FIELD_PHOTOMETRIC               8
 #define        FIELD_THRESHHOLDING             9
 #define        FIELD_FILLORDER                 10
-#define        FIELD_DOCUMENTNAME              11
-#define        FIELD_IMAGEDESCRIPTION          12
-#define        FIELD_MAKE                      13
-#define        FIELD_MODEL                     14
 #define        FIELD_ORIENTATION               15
 #define        FIELD_SAMPLESPERPIXEL           16
 #define        FIELD_ROWSPERSTRIP              17
 #define        FIELD_MINSAMPLEVALUE            18
 #define        FIELD_MAXSAMPLEVALUE            19
 #define        FIELD_PLANARCONFIG              20
-#define        FIELD_PAGENAME                  21
 #define        FIELD_RESOLUTIONUNIT            22
 #define        FIELD_PAGENUMBER                23
 #define        FIELD_STRIPBYTECOUNTS           24
 #define        FIELD_STRIPOFFSETS              25
 #define        FIELD_COLORMAP                  26
-#define FIELD_ARTIST                   27
-#define FIELD_DATETIME                 28
-#define FIELD_HOSTCOMPUTER             29
-/* unused - was FIELD_SOFTWARE          30 */
 #define        FIELD_EXTRASAMPLES              31
 #define FIELD_SAMPLEFORMAT             32
 #define        FIELD_SMINSAMPLEVALUE           33
@@ -173,33 +132,12 @@ typedef   struct {
 #define FIELD_IMAGEDEPTH               35
 #define FIELD_TILEDEPTH                        36
 #define        FIELD_HALFTONEHINTS             37
-#define FIELD_YCBCRCOEFFICIENTS                38
 #define FIELD_YCBCRSUBSAMPLING         39
 #define FIELD_YCBCRPOSITIONING         40
 #define        FIELD_REFBLACKWHITE             41
-#define        FIELD_WHITEPOINT                42
-#define        FIELD_PRIMARYCHROMAS            43
 #define        FIELD_TRANSFERFUNCTION          44
-#define        FIELD_INKSET                    45
 #define        FIELD_INKNAMES                  46
-#define        FIELD_DOTRANGE                  47
-#define        FIELD_TARGETPRINTER             48
 #define        FIELD_SUBIFD                    49
-#define        FIELD_NUMBEROFINKS              50
-#define FIELD_ICCPROFILE               51
-#define FIELD_PHOTOSHOP                        52
-#define FIELD_RICHTIFFIPTC             53
-#define FIELD_STONITS                  54
-/* Begin PIXAR */
-#define        FIELD_IMAGEFULLWIDTH            55
-#define        FIELD_IMAGEFULLLENGTH           56
-#define FIELD_TEXTUREFORMAT            57
-#define FIELD_WRAPMODES                        58
-#define FIELD_FOVCOT                   59
-#define FIELD_MATRIX_WORLDTOSCREEN     60
-#define FIELD_MATRIX_WORLDTOCAMERA     61
-#define FIELD_COPYRIGHT                        62
-#define FIELD_XMLPACKET                        63
 /*      FIELD_CUSTOM (see tiffio.h)     65 */
 /* end of support for well-known tags; codec-private tags follow */
 #define        FIELD_CODEC                     66      /* base of codec-private tags */
@@ -241,7 +179,10 @@ typedef    struct {
 #if defined(__cplusplus)
 extern "C" {
 #endif
-extern void _TIFFSetupFieldInfo(TIFF*);
+extern const TIFFFieldInfo *_TIFFGetFieldInfo(size_t *);
+extern const TIFFFieldInfo *_TIFFGetExifFieldInfo(size_t *);
+extern void _TIFFSetupFieldInfo(TIFF*, const TIFFFieldInfo[], size_t);
+extern int _TIFFMergeFieldInfo(TIFF*, const TIFFFieldInfo[], int);
 extern void _TIFFPrintFieldInfo(TIFF*, FILE*);
 extern TIFFDataType _TIFFSampleToTagType(TIFF*);
 extern  const TIFFFieldInfo* _TIFFFindOrRegisterFieldInfo( TIFF *tif,
@@ -250,7 +191,6 @@ extern  const TIFFFieldInfo* _TIFFFindOrRegisterFieldInfo( TIFF *tif,
 extern  TIFFFieldInfo* _TIFFCreateAnonFieldInfo( TIFF *tif, ttag_t tag,
                                                  TIFFDataType dt );
 
-#define _TIFFMergeFieldInfo        TIFFMergeFieldInfo
 #define _TIFFFindFieldInfo         TIFFFindFieldInfo
 #define _TIFFFindFieldInfoByName    TIFFFindFieldInfoByName
 #define _TIFFFieldWithTag          TIFFFieldWithTag
@@ -262,3 +202,10 @@ extern  TIFFFieldInfo* _TIFFCreateAnonFieldInfo( TIFF *tif, ttag_t tag,
 #endif /* _TIFFDIR_ */
 
 /* vim: set ts=8 sts=8 sw=8 noet: */
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
index 0cd506d..0a77c97 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: tif_dirinfo.c,v 1.1 2005-06-17 13:54:52 vp153 Exp $ */
+/* $Id: tif_dirinfo.c,v 1.65.2.9 2010-06-09 21:15:27 bfriesen Exp $ */
 
 /*
  * Copyright (c) 1988-1997 Sam Leffler
  */
 #include "tiffiop.h"
 #include <stdlib.h>
-#include <stdio.h>
+#include <string.h>
 
 /*
  * NB: NB: THIS ARRAY IS ASSUMED TO BE SORTED BY TAG.
- *     If a tag can have both LONG and SHORT types
- *     then the LONG must be placed before the SHORT for
- *     writing to work properly.
+ *       If a tag can have both LONG and SHORT types then the LONG must be
+ *       placed before the SHORT for writing to work properly.
  *
  * NOTE: The second field (field_readcount) and third field (field_writecount)
  *       sometimes use the values TIFF_VARIABLE (-1), TIFF_VARIABLE2 (-3)
  *       the formatting of the code, so please interprete the -1, -2 and -3 
  *       values accordingly.
  */
-#ifndef VMS
-static 
-#endif
-const TIFFFieldInfo tiffFieldInfo[] = {
-    { TIFFTAG_SUBFILETYPE,      1, 1, TIFF_LONG,       FIELD_SUBFILETYPE,
-      TRUE,    FALSE,  "SubfileType" },
+static const TIFFFieldInfo
+tiffFieldInfo[] = {
+    { TIFFTAG_SUBFILETYPE,      1, 1,  TIFF_LONG,      FIELD_SUBFILETYPE,
+      1,       0,      "SubfileType" },
 /* XXX SHORT for compatibility w/ old versions of the library */
-    { TIFFTAG_SUBFILETYPE,      1, 1, TIFF_SHORT,      FIELD_SUBFILETYPE,
-      TRUE,    FALSE,  "SubfileType" },
-    { TIFFTAG_OSUBFILETYPE,     1, 1, TIFF_SHORT,      FIELD_SUBFILETYPE,
-      TRUE,    FALSE,  "OldSubfileType" },
-    { TIFFTAG_IMAGEWIDTH,       1, 1, TIFF_LONG,       FIELD_IMAGEDIMENSIONS,
-      FALSE,   FALSE,  "ImageWidth" },
-    { TIFFTAG_IMAGEWIDTH,       1, 1, TIFF_SHORT,      FIELD_IMAGEDIMENSIONS,
-      FALSE,   FALSE,  "ImageWidth" },
-    { TIFFTAG_IMAGELENGTH,      1, 1, TIFF_LONG,       FIELD_IMAGEDIMENSIONS,
-      TRUE,    FALSE,  "ImageLength" },
-    { TIFFTAG_IMAGELENGTH,      1, 1, TIFF_SHORT,      FIELD_IMAGEDIMENSIONS,
-      TRUE,    FALSE,  "ImageLength" },
-    { TIFFTAG_BITSPERSAMPLE,   -1,-1, TIFF_SHORT,      FIELD_BITSPERSAMPLE,
-      FALSE,   FALSE,  "BitsPerSample" },
+    { TIFFTAG_SUBFILETYPE,      1, 1,  TIFF_SHORT,     FIELD_SUBFILETYPE,
+      1,       0,      "SubfileType" },
+    { TIFFTAG_OSUBFILETYPE,     1, 1,  TIFF_SHORT,     FIELD_SUBFILETYPE,
+      1,       0,      "OldSubfileType" },
+    { TIFFTAG_IMAGEWIDTH,       1, 1,  TIFF_LONG,      FIELD_IMAGEDIMENSIONS,
+      0,       0,      "ImageWidth" },
+    { TIFFTAG_IMAGEWIDTH,       1, 1,  TIFF_SHORT,     FIELD_IMAGEDIMENSIONS,
+      0,       0,      "ImageWidth" },
+    { TIFFTAG_IMAGELENGTH,      1, 1,  TIFF_LONG,      FIELD_IMAGEDIMENSIONS,
+      1,       0,      "ImageLength" },
+    { TIFFTAG_IMAGELENGTH,      1, 1,  TIFF_SHORT,     FIELD_IMAGEDIMENSIONS,
+      1,       0,      "ImageLength" },
+    { TIFFTAG_BITSPERSAMPLE,   -1,-1,  TIFF_SHORT,     FIELD_BITSPERSAMPLE,
+      0,       0,      "BitsPerSample" },
 /* XXX LONG for compatibility with some broken TIFF writers */
-    { TIFFTAG_BITSPERSAMPLE,   -1,-1, TIFF_LONG,       FIELD_BITSPERSAMPLE,
-      FALSE,   FALSE,  "BitsPerSample" },
-    { TIFFTAG_COMPRESSION,     -1, 1, TIFF_SHORT,      FIELD_COMPRESSION,
-      FALSE,   FALSE,  "Compression" },
+    { TIFFTAG_BITSPERSAMPLE,   -1,-1,  TIFF_LONG,      FIELD_BITSPERSAMPLE,
+      0,       0,      "BitsPerSample" },
+    { TIFFTAG_COMPRESSION,     -1, 1,  TIFF_SHORT,     FIELD_COMPRESSION,
+      0,       0,      "Compression" },
 /* XXX LONG for compatibility with some broken TIFF writers */
-    { TIFFTAG_COMPRESSION,     -1, 1, TIFF_LONG,       FIELD_COMPRESSION,
-      FALSE,   FALSE,  "Compression" },
-    { TIFFTAG_PHOTOMETRIC,      1, 1, TIFF_SHORT,      FIELD_PHOTOMETRIC,
-      FALSE,   FALSE,  "PhotometricInterpretation" },
+    { TIFFTAG_COMPRESSION,     -1, 1,  TIFF_LONG,      FIELD_COMPRESSION,
+      0,       0,      "Compression" },
+    { TIFFTAG_PHOTOMETRIC,      1, 1,  TIFF_SHORT,     FIELD_PHOTOMETRIC,
+      0,       0,      "PhotometricInterpretation" },
 /* XXX LONG for compatibility with some broken TIFF writers */
-    { TIFFTAG_PHOTOMETRIC,      1, 1, TIFF_LONG,       FIELD_PHOTOMETRIC,
-      FALSE,   FALSE,  "PhotometricInterpretation" },
-    { TIFFTAG_THRESHHOLDING,    1, 1, TIFF_SHORT,      FIELD_THRESHHOLDING,
-      TRUE,    FALSE,  "Threshholding" },
-    { TIFFTAG_CELLWIDTH,        1, 1, TIFF_SHORT,      FIELD_IGNORE,
-      TRUE,    FALSE,  "CellWidth" },
-    { TIFFTAG_CELLLENGTH,       1, 1, TIFF_SHORT,      FIELD_IGNORE,
-      TRUE,    FALSE,  "CellLength" },
-    { TIFFTAG_FILLORDER,        1, 1, TIFF_SHORT,      FIELD_FILLORDER,
-      FALSE,   FALSE,  "FillOrder" },
-    { TIFFTAG_DOCUMENTNAME,    -1,-1, TIFF_ASCII,      FIELD_DOCUMENTNAME,
-      TRUE,    FALSE,  "DocumentName" },
-    { TIFFTAG_IMAGEDESCRIPTION,        -1,-1, TIFF_ASCII,      FIELD_IMAGEDESCRIPTION,
-      TRUE,    FALSE,  "ImageDescription" },
-    { TIFFTAG_MAKE,            -1,-1, TIFF_ASCII,      FIELD_MAKE,
-      TRUE,    FALSE,  "Make" },
-    { TIFFTAG_MODEL,           -1,-1, TIFF_ASCII,      FIELD_MODEL,
-      TRUE,    FALSE,  "Model" },
-    { TIFFTAG_STRIPOFFSETS,    -1,-1, TIFF_LONG,       FIELD_STRIPOFFSETS,
-      FALSE,   FALSE,  "StripOffsets" },
-    { TIFFTAG_STRIPOFFSETS,    -1,-1, TIFF_SHORT,      FIELD_STRIPOFFSETS,
-      FALSE,   FALSE,  "StripOffsets" },
-    { TIFFTAG_ORIENTATION,      1, 1, TIFF_SHORT,      FIELD_ORIENTATION,
-      FALSE,   FALSE,  "Orientation" },
-    { TIFFTAG_SAMPLESPERPIXEL,  1, 1, TIFF_SHORT,      FIELD_SAMPLESPERPIXEL,
-      FALSE,   FALSE,  "SamplesPerPixel" },
-    { TIFFTAG_ROWSPERSTRIP,     1, 1, TIFF_LONG,       FIELD_ROWSPERSTRIP,
-      FALSE,   FALSE,  "RowsPerStrip" },
-    { TIFFTAG_ROWSPERSTRIP,     1, 1, TIFF_SHORT,      FIELD_ROWSPERSTRIP,
-      FALSE,   FALSE,  "RowsPerStrip" },
-    { TIFFTAG_STRIPBYTECOUNTS, -1,-1, TIFF_LONG,       FIELD_STRIPBYTECOUNTS,
-      FALSE,   FALSE,  "StripByteCounts" },
-    { TIFFTAG_STRIPBYTECOUNTS, -1,-1, TIFF_SHORT,      FIELD_STRIPBYTECOUNTS,
-      FALSE,   FALSE,  "StripByteCounts" },
-    { TIFFTAG_MINSAMPLEVALUE,  -2,-1, TIFF_SHORT,      FIELD_MINSAMPLEVALUE,
-      TRUE,    FALSE,  "MinSampleValue" },
-    { TIFFTAG_MAXSAMPLEVALUE,  -2,-1, TIFF_SHORT,      FIELD_MAXSAMPLEVALUE,
-      TRUE,    FALSE,  "MaxSampleValue" },
-    { TIFFTAG_XRESOLUTION,      1, 1, TIFF_RATIONAL,   FIELD_RESOLUTION,
-      FALSE,   FALSE,  "XResolution" },
-    { TIFFTAG_YRESOLUTION,      1, 1, TIFF_RATIONAL,   FIELD_RESOLUTION,
-      FALSE,   FALSE,  "YResolution" },
-    { TIFFTAG_PLANARCONFIG,     1, 1, TIFF_SHORT,      FIELD_PLANARCONFIG,
-      FALSE,   FALSE,  "PlanarConfiguration" },
-    { TIFFTAG_PAGENAME,                -1,-1, TIFF_ASCII,      FIELD_PAGENAME,
-      TRUE,    FALSE,  "PageName" },
-    { TIFFTAG_XPOSITION,        1, 1, TIFF_RATIONAL,   FIELD_POSITION,
-      TRUE,    FALSE,  "XPosition" },
-    { TIFFTAG_YPOSITION,        1, 1, TIFF_RATIONAL,   FIELD_POSITION,
-      TRUE,    FALSE,  "YPosition" },
-    { TIFFTAG_FREEOFFSETS,     -1,-1, TIFF_LONG,       FIELD_IGNORE,
-      FALSE,   FALSE,  "FreeOffsets" },
-    { TIFFTAG_FREEBYTECOUNTS,  -1,-1, TIFF_LONG,       FIELD_IGNORE,
-      FALSE,   FALSE,  "FreeByteCounts" },
-    { TIFFTAG_GRAYRESPONSEUNIT,         1, 1, TIFF_SHORT,      FIELD_IGNORE,
-      TRUE,    FALSE,  "GrayResponseUnit" },
-    { TIFFTAG_GRAYRESPONSECURVE,-1,-1, TIFF_SHORT,     FIELD_IGNORE,
-      TRUE,    FALSE,  "GrayResponseCurve" },
-    { TIFFTAG_RESOLUTIONUNIT,   1, 1, TIFF_SHORT,      FIELD_RESOLUTIONUNIT,
-      FALSE,   FALSE,  "ResolutionUnit" },
-    { TIFFTAG_PAGENUMBER,       2, 2, TIFF_SHORT,      FIELD_PAGENUMBER,
-      TRUE,    FALSE,  "PageNumber" },
-    { TIFFTAG_COLORRESPONSEUNIT, 1, 1, TIFF_SHORT,     FIELD_IGNORE,
-      TRUE,    FALSE,  "ColorResponseUnit" },
-    { TIFFTAG_TRANSFERFUNCTION,        -1,-1, TIFF_SHORT,      FIELD_TRANSFERFUNCTION,
-      TRUE,    FALSE,  "TransferFunction" },
-    { TIFFTAG_SOFTWARE,                -1,-1, TIFF_ASCII,      FIELD_CUSTOM,
-      TRUE,    FALSE,  "Software" },
-    { TIFFTAG_DATETIME,                20,20, TIFF_ASCII,      FIELD_DATETIME,
-      TRUE,    FALSE,  "DateTime" },
-    { TIFFTAG_ARTIST,          -1,-1, TIFF_ASCII,      FIELD_ARTIST,
-      TRUE,    FALSE,  "Artist" },
-    { TIFFTAG_HOSTCOMPUTER,    -1,-1, TIFF_ASCII,      FIELD_HOSTCOMPUTER,
-      TRUE,    FALSE,  "HostComputer" },
-    { TIFFTAG_WHITEPOINT,       2, 2, TIFF_RATIONAL,FIELD_WHITEPOINT,
-      TRUE,    FALSE,  "WhitePoint" },
-    { TIFFTAG_PRIMARYCHROMATICITIES,6,6,TIFF_RATIONAL,FIELD_PRIMARYCHROMAS,
-      TRUE,    FALSE,  "PrimaryChromaticities" },
-    { TIFFTAG_COLORMAP,                -1,-1, TIFF_SHORT,      FIELD_COLORMAP,
-      TRUE,    FALSE,  "ColorMap" },
-    { TIFFTAG_HALFTONEHINTS,    2, 2, TIFF_SHORT,      FIELD_HALFTONEHINTS,
-      TRUE,    FALSE,  "HalftoneHints" },
-    { TIFFTAG_TILEWIDTH,        1, 1, TIFF_LONG,       FIELD_TILEDIMENSIONS,
-      FALSE,   FALSE,  "TileWidth" },
-    { TIFFTAG_TILEWIDTH,        1, 1, TIFF_SHORT,      FIELD_TILEDIMENSIONS,
-      FALSE,   FALSE,  "TileWidth" },
-    { TIFFTAG_TILELENGTH,       1, 1, TIFF_LONG,       FIELD_TILEDIMENSIONS,
-      FALSE,   FALSE,  "TileLength" },
-    { TIFFTAG_TILELENGTH,       1, 1, TIFF_SHORT,      FIELD_TILEDIMENSIONS,
-      FALSE,   FALSE,  "TileLength" },
-    { TIFFTAG_TILEOFFSETS,     -1, 1, TIFF_LONG,       FIELD_STRIPOFFSETS,
-      FALSE,   FALSE,  "TileOffsets" },
-    { TIFFTAG_TILEBYTECOUNTS,  -1, 1, TIFF_LONG,       FIELD_STRIPBYTECOUNTS,
-      FALSE,   FALSE,  "TileByteCounts" },
-    { TIFFTAG_TILEBYTECOUNTS,  -1, 1, TIFF_SHORT,      FIELD_STRIPBYTECOUNTS,
-      FALSE,   FALSE,  "TileByteCounts" },
-    { TIFFTAG_SUBIFD,          -1,-1, TIFF_IFD,        FIELD_SUBIFD,
-      TRUE,    TRUE,   "SubIFD" },
-    { TIFFTAG_SUBIFD,          -1,-1, TIFF_LONG,       FIELD_SUBIFD,
-      TRUE,    TRUE,   "SubIFD" },
-    { TIFFTAG_INKSET,           1, 1, TIFF_SHORT,      FIELD_INKSET,
-      FALSE,   FALSE,  "InkSet" },
-    { TIFFTAG_INKNAMES,                -1,-1, TIFF_ASCII,      FIELD_INKNAMES,
-      TRUE,    TRUE,   "InkNames" },
-    { TIFFTAG_NUMBEROFINKS,     1, 1, TIFF_SHORT,      FIELD_NUMBEROFINKS,
-      TRUE,    FALSE,  "NumberOfInks" },
-    { TIFFTAG_DOTRANGE,                 2, 2, TIFF_SHORT,      FIELD_DOTRANGE,
-      FALSE,   FALSE,  "DotRange" },
-    { TIFFTAG_DOTRANGE,                 2, 2, TIFF_BYTE,       FIELD_DOTRANGE,
-      FALSE,   FALSE,  "DotRange" },
-    { TIFFTAG_TARGETPRINTER,   -1,-1, TIFF_ASCII,      FIELD_TARGETPRINTER,
-      TRUE,    FALSE,  "TargetPrinter" },
-    { TIFFTAG_EXTRASAMPLES,    -1,-1, TIFF_SHORT,      FIELD_EXTRASAMPLES,
-      FALSE,   TRUE,   "ExtraSamples" },
+    { TIFFTAG_PHOTOMETRIC,      1, 1,  TIFF_LONG,      FIELD_PHOTOMETRIC,
+      0,       0,      "PhotometricInterpretation" },
+    { TIFFTAG_THRESHHOLDING,    1, 1,  TIFF_SHORT,     FIELD_THRESHHOLDING,
+      1,       0,      "Threshholding" },
+    { TIFFTAG_CELLWIDTH,        1, 1,  TIFF_SHORT,     FIELD_IGNORE,
+      1,       0,      "CellWidth" },
+    { TIFFTAG_CELLLENGTH,       1, 1,  TIFF_SHORT,     FIELD_IGNORE,
+      1,       0,      "CellLength" },
+    { TIFFTAG_FILLORDER,        1, 1,  TIFF_SHORT,     FIELD_FILLORDER,
+      0,       0,      "FillOrder" },
+    { TIFFTAG_DOCUMENTNAME,    -1,-1,  TIFF_ASCII,     FIELD_CUSTOM,
+      1,       0,      "DocumentName" },
+    { TIFFTAG_IMAGEDESCRIPTION,        -1,-1,  TIFF_ASCII,     FIELD_CUSTOM,
+      1,       0,      "ImageDescription" },
+    { TIFFTAG_MAKE,            -1,-1,  TIFF_ASCII,     FIELD_CUSTOM,
+      1,       0,      "Make" },
+    { TIFFTAG_MODEL,           -1,-1,  TIFF_ASCII,     FIELD_CUSTOM,
+      1,       0,      "Model" },
+    { TIFFTAG_STRIPOFFSETS,    -1,-1,  TIFF_LONG,      FIELD_STRIPOFFSETS,
+      0,       0,      "StripOffsets" },
+    { TIFFTAG_STRIPOFFSETS,    -1,-1,  TIFF_SHORT,     FIELD_STRIPOFFSETS,
+      0,       0,      "StripOffsets" },
+    { TIFFTAG_ORIENTATION,      1, 1,  TIFF_SHORT,     FIELD_ORIENTATION,
+      0,       0,      "Orientation" },
+    { TIFFTAG_SAMPLESPERPIXEL,  1, 1,  TIFF_SHORT,     FIELD_SAMPLESPERPIXEL,
+      0,       0,      "SamplesPerPixel" },
+    { TIFFTAG_ROWSPERSTRIP,     1, 1,  TIFF_LONG,      FIELD_ROWSPERSTRIP,
+      0,       0,      "RowsPerStrip" },
+    { TIFFTAG_ROWSPERSTRIP,     1, 1,  TIFF_SHORT,     FIELD_ROWSPERSTRIP,
+      0,       0,      "RowsPerStrip" },
+    { TIFFTAG_STRIPBYTECOUNTS, -1,-1,  TIFF_LONG,      FIELD_STRIPBYTECOUNTS,
+      0,       0,      "StripByteCounts" },
+    { TIFFTAG_STRIPBYTECOUNTS, -1,-1,  TIFF_SHORT,     FIELD_STRIPBYTECOUNTS,
+      0,       0,      "StripByteCounts" },
+    { TIFFTAG_MINSAMPLEVALUE,  -2,-1,  TIFF_SHORT,     FIELD_MINSAMPLEVALUE,
+      1,       0,      "MinSampleValue" },
+    { TIFFTAG_MAXSAMPLEVALUE,  -2,-1,  TIFF_SHORT,     FIELD_MAXSAMPLEVALUE,
+      1,       0,      "MaxSampleValue" },
+    { TIFFTAG_XRESOLUTION,      1, 1,  TIFF_RATIONAL,  FIELD_RESOLUTION,
+      1,       0,      "XResolution" },
+    { TIFFTAG_YRESOLUTION,      1, 1,  TIFF_RATIONAL,  FIELD_RESOLUTION,
+      1,       0,      "YResolution" },
+    { TIFFTAG_PLANARCONFIG,     1, 1,  TIFF_SHORT,     FIELD_PLANARCONFIG,
+      0,       0,      "PlanarConfiguration" },
+    { TIFFTAG_PAGENAME,                -1,-1,  TIFF_ASCII,     FIELD_CUSTOM,
+      1,       0,      "PageName" },
+    { TIFFTAG_XPOSITION,        1, 1,  TIFF_RATIONAL,  FIELD_POSITION,
+      1,       0,      "XPosition" },
+    { TIFFTAG_YPOSITION,        1, 1,  TIFF_RATIONAL,  FIELD_POSITION,
+      1,       0,      "YPosition" },
+    { TIFFTAG_FREEOFFSETS,     -1,-1,  TIFF_LONG,      FIELD_IGNORE,
+      0,       0,      "FreeOffsets" },
+    { TIFFTAG_FREEBYTECOUNTS,  -1,-1,  TIFF_LONG,      FIELD_IGNORE,
+      0,       0,      "FreeByteCounts" },
+    { TIFFTAG_GRAYRESPONSEUNIT,         1, 1,  TIFF_SHORT,     FIELD_IGNORE,
+      1,       0,      "GrayResponseUnit" },
+    { TIFFTAG_GRAYRESPONSECURVE,-1,-1, TIFF_SHORT,     FIELD_IGNORE,
+      1,       0,      "GrayResponseCurve" },
+    { TIFFTAG_RESOLUTIONUNIT,   1, 1,  TIFF_SHORT,     FIELD_RESOLUTIONUNIT,
+      1,       0,      "ResolutionUnit" },
+    { TIFFTAG_PAGENUMBER,       2, 2,  TIFF_SHORT,     FIELD_PAGENUMBER,
+      1,       0,      "PageNumber" },
+    { TIFFTAG_COLORRESPONSEUNIT, 1, 1, TIFF_SHORT,     FIELD_IGNORE,
+      1,       0,      "ColorResponseUnit" },
+    { TIFFTAG_TRANSFERFUNCTION,        -1,-1,  TIFF_SHORT,     FIELD_TRANSFERFUNCTION,
+      1,       0,      "TransferFunction" },
+    { TIFFTAG_SOFTWARE,                -1,-1,  TIFF_ASCII,     FIELD_CUSTOM,
+      1,       0,      "Software" },
+    { TIFFTAG_DATETIME,                20,20,  TIFF_ASCII,     FIELD_CUSTOM,
+      1,       0,      "DateTime" },
+    { TIFFTAG_ARTIST,          -1,-1,  TIFF_ASCII,     FIELD_CUSTOM,
+      1,       0,      "Artist" },
+    { TIFFTAG_HOSTCOMPUTER,    -1,-1,  TIFF_ASCII,     FIELD_CUSTOM,
+      1,       0,      "HostComputer" },
+    { TIFFTAG_WHITEPOINT,       2, 2,  TIFF_RATIONAL,  FIELD_CUSTOM,
+      1,       0,      "WhitePoint" },
+    { TIFFTAG_PRIMARYCHROMATICITIES,6,6,TIFF_RATIONAL, FIELD_CUSTOM,
+      1,       0,      "PrimaryChromaticities" },
+    { TIFFTAG_COLORMAP,                -1,-1,  TIFF_SHORT,     FIELD_COLORMAP,
+      1,       0,      "ColorMap" },
+    { TIFFTAG_HALFTONEHINTS,    2, 2,  TIFF_SHORT,     FIELD_HALFTONEHINTS,
+      1,       0,      "HalftoneHints" },
+    { TIFFTAG_TILEWIDTH,        1, 1,  TIFF_LONG,      FIELD_TILEDIMENSIONS,
+      0,       0,      "TileWidth" },
+    { TIFFTAG_TILEWIDTH,        1, 1,  TIFF_SHORT,     FIELD_TILEDIMENSIONS,
+      0,       0,      "TileWidth" },
+    { TIFFTAG_TILELENGTH,       1, 1,  TIFF_LONG,      FIELD_TILEDIMENSIONS,
+      0,       0,      "TileLength" },
+    { TIFFTAG_TILELENGTH,       1, 1,  TIFF_SHORT,     FIELD_TILEDIMENSIONS,
+      0,       0,      "TileLength" },
+    { TIFFTAG_TILEOFFSETS,     -1, 1,  TIFF_LONG,      FIELD_STRIPOFFSETS,
+      0,       0,      "TileOffsets" },
+    { TIFFTAG_TILEBYTECOUNTS,  -1, 1,  TIFF_LONG,      FIELD_STRIPBYTECOUNTS,
+      0,       0,      "TileByteCounts" },
+    { TIFFTAG_TILEBYTECOUNTS,  -1, 1,  TIFF_SHORT,     FIELD_STRIPBYTECOUNTS,
+      0,       0,      "TileByteCounts" },
+    { TIFFTAG_SUBIFD,          -1,-1,  TIFF_IFD,       FIELD_SUBIFD,
+      1,       1,      "SubIFD" },
+    { TIFFTAG_SUBIFD,          -1,-1,  TIFF_LONG,      FIELD_SUBIFD,
+      1,       1,      "SubIFD" },
+    { TIFFTAG_INKSET,           1, 1,  TIFF_SHORT,     FIELD_CUSTOM,
+      0,       0,      "InkSet" },
+    { TIFFTAG_INKNAMES,                -1,-1,  TIFF_ASCII,     FIELD_INKNAMES,
+      1,       1,      "InkNames" },
+    { TIFFTAG_NUMBEROFINKS,     1, 1,  TIFF_SHORT,     FIELD_CUSTOM,
+      1,       0,      "NumberOfInks" },
+    { TIFFTAG_DOTRANGE,                 2, 2,  TIFF_SHORT,     FIELD_CUSTOM,
+      0,       0,      "DotRange" },
+    { TIFFTAG_DOTRANGE,                 2, 2,  TIFF_BYTE,      FIELD_CUSTOM,
+      0,       0,      "DotRange" },
+    { TIFFTAG_TARGETPRINTER,   -1,-1,  TIFF_ASCII,     FIELD_CUSTOM,
+      1,       0,      "TargetPrinter" },
+    { TIFFTAG_EXTRASAMPLES,    -1,-1,  TIFF_SHORT,     FIELD_EXTRASAMPLES,
+      0,       1,      "ExtraSamples" },
 /* XXX for bogus Adobe Photoshop v2.5 files */
-    { TIFFTAG_EXTRASAMPLES,    -1,-1, TIFF_BYTE,       FIELD_EXTRASAMPLES,
-      FALSE,   TRUE,   "ExtraSamples" },
-    { TIFFTAG_SAMPLEFORMAT,    -1,-1, TIFF_SHORT,      FIELD_SAMPLEFORMAT,
-      FALSE,   FALSE,  "SampleFormat" },
-    { TIFFTAG_SMINSAMPLEVALUE, -2,-1, TIFF_ANY,        FIELD_SMINSAMPLEVALUE,
-      TRUE,    FALSE,  "SMinSampleValue" },
-    { TIFFTAG_SMAXSAMPLEVALUE, -2,-1, TIFF_ANY,        FIELD_SMAXSAMPLEVALUE,
-      TRUE,    FALSE,  "SMaxSampleValue" },
-    { TIFFTAG_YCBCRCOEFFICIENTS, 3, 3, TIFF_RATIONAL,  FIELD_YCBCRCOEFFICIENTS,
-      FALSE,   FALSE,  "YCbCrCoefficients" },
-    { TIFFTAG_YCBCRSUBSAMPLING,         2, 2, TIFF_SHORT,      FIELD_YCBCRSUBSAMPLING,
-      FALSE,   FALSE,  "YCbCrSubsampling" },
-    { TIFFTAG_YCBCRPOSITIONING,         1, 1, TIFF_SHORT,      FIELD_YCBCRPOSITIONING,
-      FALSE,   FALSE,  "YCbCrPositioning" },
-    { TIFFTAG_REFERENCEBLACKWHITE,6,6,TIFF_RATIONAL,   FIELD_REFBLACKWHITE,
-      TRUE,    FALSE,  "ReferenceBlackWhite" },
+    { TIFFTAG_EXTRASAMPLES,    -1,-1,  TIFF_BYTE,      FIELD_EXTRASAMPLES,
+      0,       1,      "ExtraSamples" },
+    { TIFFTAG_SAMPLEFORMAT,    -1,-1,  TIFF_SHORT,     FIELD_SAMPLEFORMAT,
+      0,       0,      "SampleFormat" },
+    { TIFFTAG_SMINSAMPLEVALUE, -2,-1,  TIFF_ANY,       FIELD_SMINSAMPLEVALUE,
+      1,       0,      "SMinSampleValue" },
+    { TIFFTAG_SMAXSAMPLEVALUE, -2,-1,  TIFF_ANY,       FIELD_SMAXSAMPLEVALUE,
+      1,       0,      "SMaxSampleValue" },
+    { TIFFTAG_CLIPPATH,                -1, -3, TIFF_BYTE,      FIELD_CUSTOM,
+      0,       1,      "ClipPath" },
+    { TIFFTAG_XCLIPPATHUNITS,   1, 1,  TIFF_SLONG,     FIELD_CUSTOM,
+      0,       0,      "XClipPathUnits" },
+    { TIFFTAG_XCLIPPATHUNITS,   1, 1,  TIFF_SSHORT,    FIELD_CUSTOM,
+      0,       0,      "XClipPathUnits" },
+    { TIFFTAG_XCLIPPATHUNITS,   1, 1,  TIFF_SBYTE,     FIELD_CUSTOM,
+      0,       0,      "XClipPathUnits" },
+    { TIFFTAG_YCLIPPATHUNITS,   1, 1,  TIFF_SLONG,     FIELD_CUSTOM,
+      0,       0,      "YClipPathUnits" },
+    { TIFFTAG_YCLIPPATHUNITS,   1, 1,  TIFF_SSHORT,    FIELD_CUSTOM,
+      0,       0,      "YClipPathUnits" },
+    { TIFFTAG_YCLIPPATHUNITS,   1, 1,  TIFF_SBYTE,     FIELD_CUSTOM,
+      0,       0,      "YClipPathUnits" },
+    { TIFFTAG_YCBCRCOEFFICIENTS, 3, 3, TIFF_RATIONAL,  FIELD_CUSTOM,
+      0,       0,      "YCbCrCoefficients" },
+    { TIFFTAG_YCBCRSUBSAMPLING,         2, 2,  TIFF_SHORT,     FIELD_YCBCRSUBSAMPLING,
+      0,       0,      "YCbCrSubsampling" },
+    { TIFFTAG_YCBCRPOSITIONING,         1, 1,  TIFF_SHORT,     FIELD_YCBCRPOSITIONING,
+      0,       0,      "YCbCrPositioning" },
+    { TIFFTAG_REFERENCEBLACKWHITE, 6, 6, TIFF_RATIONAL,        FIELD_REFBLACKWHITE,
+      1,       0,      "ReferenceBlackWhite" },
 /* XXX temporarily accept LONG for backwards compatibility */
-    { TIFFTAG_REFERENCEBLACKWHITE,6,6,TIFF_LONG,       FIELD_REFBLACKWHITE,
-      TRUE,    FALSE,  "ReferenceBlackWhite" },
-    { TIFFTAG_XMLPACKET,       -1,-3, TIFF_BYTE,       FIELD_XMLPACKET,
-      FALSE,   TRUE,   "XMLPacket" },
+    { TIFFTAG_REFERENCEBLACKWHITE, 6, 6, TIFF_LONG,    FIELD_REFBLACKWHITE,
+      1,       0,      "ReferenceBlackWhite" },
+    { TIFFTAG_XMLPACKET,       -3,-3,  TIFF_BYTE,      FIELD_CUSTOM,
+      0,       1,      "XMLPacket" },
 /* begin SGI tags */
-    { TIFFTAG_MATTEING,                 1, 1, TIFF_SHORT,      FIELD_EXTRASAMPLES,
-      FALSE,   FALSE,  "Matteing" },
-    { TIFFTAG_DATATYPE,                -2,-1, TIFF_SHORT,      FIELD_SAMPLEFORMAT,
-      FALSE,   FALSE,  "DataType" },
-    { TIFFTAG_IMAGEDEPTH,       1, 1, TIFF_LONG,       FIELD_IMAGEDEPTH,
-      FALSE,   FALSE,  "ImageDepth" },
-    { TIFFTAG_IMAGEDEPTH,       1, 1, TIFF_SHORT,      FIELD_IMAGEDEPTH,
-      FALSE,   FALSE,  "ImageDepth" },
-    { TIFFTAG_TILEDEPTH,        1, 1, TIFF_LONG,       FIELD_TILEDEPTH,
-      FALSE,   FALSE,  "TileDepth" },
-    { TIFFTAG_TILEDEPTH,        1, 1, TIFF_SHORT,      FIELD_TILEDEPTH,
-      FALSE,   FALSE,  "TileDepth" },
+    { TIFFTAG_MATTEING,                 1, 1,  TIFF_SHORT,     FIELD_EXTRASAMPLES,
+      0,       0,      "Matteing" },
+    { TIFFTAG_DATATYPE,                -2,-1,  TIFF_SHORT,     FIELD_SAMPLEFORMAT,
+      0,       0,      "DataType" },
+    { TIFFTAG_IMAGEDEPTH,       1, 1,  TIFF_LONG,      FIELD_IMAGEDEPTH,
+      0,       0,      "ImageDepth" },
+    { TIFFTAG_IMAGEDEPTH,       1, 1,  TIFF_SHORT,     FIELD_IMAGEDEPTH,
+      0,       0,      "ImageDepth" },
+    { TIFFTAG_TILEDEPTH,        1, 1,  TIFF_LONG,      FIELD_TILEDEPTH,
+      0,       0,      "TileDepth" },
+    { TIFFTAG_TILEDEPTH,        1, 1,  TIFF_SHORT,     FIELD_TILEDEPTH,
+      0,       0,      "TileDepth" },
 /* end SGI tags */
 /* begin Pixar tags */
-    { TIFFTAG_PIXAR_IMAGEFULLWIDTH,  1, 1, TIFF_LONG,  FIELD_IMAGEFULLWIDTH,
-      TRUE,    FALSE,  "ImageFullWidth" },
-    { TIFFTAG_PIXAR_IMAGEFULLLENGTH, 1, 1, TIFF_LONG,  FIELD_IMAGEFULLLENGTH,
-      TRUE,    FALSE,  "ImageFullLength" },
-    { TIFFTAG_PIXAR_TEXTUREFORMAT,  -1,-1, TIFF_ASCII, FIELD_TEXTUREFORMAT,
-      TRUE,    FALSE,  "TextureFormat" },
-    { TIFFTAG_PIXAR_WRAPMODES,     -1,-1, TIFF_ASCII,  FIELD_WRAPMODES,
-      TRUE,    FALSE,  "TextureWrapModes" },
-    { TIFFTAG_PIXAR_FOVCOT,         1, 1, TIFF_FLOAT,  FIELD_FOVCOT,
-      TRUE,    FALSE,  "FieldOfViewCotan" },
+    { TIFFTAG_PIXAR_IMAGEFULLWIDTH,  1, 1, TIFF_LONG,  FIELD_CUSTOM,
+      1,       0,      "ImageFullWidth" },
+    { TIFFTAG_PIXAR_IMAGEFULLLENGTH, 1, 1, TIFF_LONG,  FIELD_CUSTOM,
+      1,       0,      "ImageFullLength" },
+    { TIFFTAG_PIXAR_TEXTUREFORMAT,  -1, -1, TIFF_ASCII,        FIELD_CUSTOM,
+      1,       0,      "TextureFormat" },
+    { TIFFTAG_PIXAR_WRAPMODES,     -1, -1, TIFF_ASCII, FIELD_CUSTOM,
+      1,       0,      "TextureWrapModes" },
+    { TIFFTAG_PIXAR_FOVCOT,         1, 1, TIFF_FLOAT,  FIELD_CUSTOM,
+      1,       0,      "FieldOfViewCotangent" },
     { TIFFTAG_PIXAR_MATRIX_WORLDTOSCREEN,      16,16,  TIFF_FLOAT,
-      FIELD_MATRIX_WORLDTOSCREEN,      TRUE,   FALSE,  "MatrixWorldToScreen" },
+      FIELD_CUSTOM,    1,      0,      "MatrixWorldToScreen" },
     { TIFFTAG_PIXAR_MATRIX_WORLDTOCAMERA,      16,16,  TIFF_FLOAT,
-       FIELD_MATRIX_WORLDTOCAMERA,     TRUE,   FALSE,  "MatrixWorldToCamera" },
-    { TIFFTAG_COPYRIGHT,       -1,-1, TIFF_ASCII,      FIELD_COPYRIGHT,
-      TRUE,    FALSE,  "Copyright" },
+       FIELD_CUSTOM,   1,      0,      "MatrixWorldToCamera" },
+    { TIFFTAG_COPYRIGHT,       -1, -1, TIFF_ASCII,     FIELD_CUSTOM,
+      1,       0,      "Copyright" },
 /* end Pixar tags */
-    { TIFFTAG_RICHTIFFIPTC, -1,-3, TIFF_LONG,   FIELD_RICHTIFFIPTC, 
-      FALSE,    TRUE,   "RichTIFFIPTC" },
-    { TIFFTAG_PHOTOSHOP,    -1,-3, TIFF_BYTE,   FIELD_PHOTOSHOP, 
-      FALSE,    TRUE,   "Photoshop" },
-    { TIFFTAG_ICCPROFILE,      -1,-3, TIFF_UNDEFINED,  FIELD_ICCPROFILE,
-      FALSE,   TRUE,   "ICC Profile" },
-    { TIFFTAG_STONITS,          1, 1, TIFF_DOUBLE,     FIELD_STONITS,
-      FALSE,   FALSE,  "StoNits" },
+    { TIFFTAG_RICHTIFFIPTC, -3, -3,    TIFF_LONG,      FIELD_CUSTOM, 
+      0,    1,   "RichTIFFIPTC" },
+    { TIFFTAG_PHOTOSHOP,    -3, -3,    TIFF_BYTE,      FIELD_CUSTOM, 
+      0,    1,   "Photoshop" },
+    { TIFFTAG_EXIFIFD,         1, 1,   TIFF_LONG,      FIELD_CUSTOM,
+      0,       0,      "EXIFIFDOffset" },
+    { TIFFTAG_ICCPROFILE,      -3, -3, TIFF_UNDEFINED, FIELD_CUSTOM,
+      0,       1,      "ICC Profile" },
+    { TIFFTAG_GPSIFD,          1, 1,   TIFF_LONG,      FIELD_CUSTOM,
+      0,       0,      "GPSIFDOffset" },
+    { TIFFTAG_STONITS,          1, 1,  TIFF_DOUBLE,    FIELD_CUSTOM,
+      0,       0,      "StoNits" },
+    { TIFFTAG_INTEROPERABILITYIFD, 1, 1, TIFF_LONG,    FIELD_CUSTOM,
+      0,       0,      "InteroperabilityIFDOffset" },
+/* begin DNG tags */
+    { TIFFTAG_DNGVERSION,      4, 4,   TIFF_BYTE,      FIELD_CUSTOM, 
+      0,       0,      "DNGVersion" },
+    { TIFFTAG_DNGBACKWARDVERSION, 4, 4,        TIFF_BYTE,      FIELD_CUSTOM, 
+      0,       0,      "DNGBackwardVersion" },
+    { TIFFTAG_UNIQUECAMERAMODEL,    -1, -1, TIFF_ASCII,        FIELD_CUSTOM,
+      1,       0,      "UniqueCameraModel" },
+    { TIFFTAG_LOCALIZEDCAMERAMODEL, -1, -1, TIFF_ASCII,        FIELD_CUSTOM,
+      1,       0,      "LocalizedCameraModel" },
+    { TIFFTAG_LOCALIZEDCAMERAMODEL, -1, -1, TIFF_BYTE, FIELD_CUSTOM,
+      1,       1,      "LocalizedCameraModel" },
+    { TIFFTAG_CFAPLANECOLOR,   -1, -1, TIFF_BYTE,      FIELD_CUSTOM, 
+      0,       1,      "CFAPlaneColor" },
+    { TIFFTAG_CFALAYOUT,       1, 1,   TIFF_SHORT,     FIELD_CUSTOM, 
+      0,       0,      "CFALayout" },
+    { TIFFTAG_LINEARIZATIONTABLE, -1, -1, TIFF_SHORT,  FIELD_CUSTOM, 
+      0,       1,      "LinearizationTable" },
+    { TIFFTAG_BLACKLEVELREPEATDIM, 2, 2, TIFF_SHORT,   FIELD_CUSTOM, 
+      0,       0,      "BlackLevelRepeatDim" },
+    { TIFFTAG_BLACKLEVEL,      -1, -1, TIFF_LONG,      FIELD_CUSTOM, 
+      0,       1,      "BlackLevel" },
+    { TIFFTAG_BLACKLEVEL,      -1, -1, TIFF_SHORT,     FIELD_CUSTOM, 
+      0,       1,      "BlackLevel" },
+    { TIFFTAG_BLACKLEVEL,      -1, -1, TIFF_RATIONAL,  FIELD_CUSTOM, 
+      0,       1,      "BlackLevel" },
+    { TIFFTAG_BLACKLEVELDELTAH,        -1, -1, TIFF_SRATIONAL, FIELD_CUSTOM, 
+      0,       1,      "BlackLevelDeltaH" },
+    { TIFFTAG_BLACKLEVELDELTAV,        -1, -1, TIFF_SRATIONAL, FIELD_CUSTOM, 
+      0,       1,      "BlackLevelDeltaV" },
+    { TIFFTAG_WHITELEVEL,      -2, -2, TIFF_LONG,      FIELD_CUSTOM, 
+      0,       0,      "WhiteLevel" },
+    { TIFFTAG_WHITELEVEL,      -2, -2, TIFF_SHORT,     FIELD_CUSTOM, 
+      0,       0,      "WhiteLevel" },
+    { TIFFTAG_DEFAULTSCALE,    2, 2,   TIFF_RATIONAL,  FIELD_CUSTOM, 
+      0,       0,      "DefaultScale" },
+    { TIFFTAG_BESTQUALITYSCALE,        1, 1,   TIFF_RATIONAL,  FIELD_CUSTOM, 
+      0,       0,      "BestQualityScale" },
+    { TIFFTAG_DEFAULTCROPORIGIN,       2, 2,   TIFF_LONG,      FIELD_CUSTOM, 
+      0,       0,      "DefaultCropOrigin" },
+    { TIFFTAG_DEFAULTCROPORIGIN,       2, 2,   TIFF_SHORT,     FIELD_CUSTOM, 
+      0,       0,      "DefaultCropOrigin" },
+    { TIFFTAG_DEFAULTCROPORIGIN,       2, 2,   TIFF_RATIONAL,  FIELD_CUSTOM, 
+      0,       0,      "DefaultCropOrigin" },
+    { TIFFTAG_DEFAULTCROPSIZE, 2, 2,   TIFF_LONG,      FIELD_CUSTOM, 
+      0,       0,      "DefaultCropSize" },
+    { TIFFTAG_DEFAULTCROPSIZE, 2, 2,   TIFF_SHORT,     FIELD_CUSTOM, 
+      0,       0,      "DefaultCropSize" },
+    { TIFFTAG_DEFAULTCROPSIZE, 2, 2,   TIFF_RATIONAL,  FIELD_CUSTOM, 
+      0,       0,      "DefaultCropSize" },
+    { TIFFTAG_COLORMATRIX1,    -1, -1, TIFF_SRATIONAL, FIELD_CUSTOM, 
+      0,       1,      "ColorMatrix1" },
+    { TIFFTAG_COLORMATRIX2,    -1, -1, TIFF_SRATIONAL, FIELD_CUSTOM, 
+      0,       1,      "ColorMatrix2" },
+    { TIFFTAG_CAMERACALIBRATION1,      -1, -1, TIFF_SRATIONAL, FIELD_CUSTOM, 
+      0,       1,      "CameraCalibration1" },
+    { TIFFTAG_CAMERACALIBRATION2,      -1, -1, TIFF_SRATIONAL, FIELD_CUSTOM, 
+      0,       1,      "CameraCalibration2" },
+    { TIFFTAG_REDUCTIONMATRIX1,        -1, -1, TIFF_SRATIONAL, FIELD_CUSTOM, 
+      0,       1,      "ReductionMatrix1" },
+    { TIFFTAG_REDUCTIONMATRIX2,        -1, -1, TIFF_SRATIONAL, FIELD_CUSTOM, 
+      0,       1,      "ReductionMatrix2" },
+    { TIFFTAG_ANALOGBALANCE,   -1, -1, TIFF_RATIONAL,  FIELD_CUSTOM, 
+      0,       1,      "AnalogBalance" },
+    { TIFFTAG_ASSHOTNEUTRAL,   -1, -1, TIFF_SHORT,     FIELD_CUSTOM, 
+      0,       1,      "AsShotNeutral" },
+    { TIFFTAG_ASSHOTNEUTRAL,   -1, -1, TIFF_RATIONAL,  FIELD_CUSTOM, 
+      0,       1,      "AsShotNeutral" },
+    { TIFFTAG_ASSHOTWHITEXY,   2, 2,   TIFF_RATIONAL,  FIELD_CUSTOM, 
+      0,       0,      "AsShotWhiteXY" },
+    { TIFFTAG_BASELINEEXPOSURE,        1, 1,   TIFF_SRATIONAL, FIELD_CUSTOM, 
+      0,       0,      "BaselineExposure" },
+    { TIFFTAG_BASELINENOISE,   1, 1,   TIFF_RATIONAL,  FIELD_CUSTOM, 
+      0,       0,      "BaselineNoise" },
+    { TIFFTAG_BASELINESHARPNESS,       1, 1,   TIFF_RATIONAL,  FIELD_CUSTOM, 
+      0,       0,      "BaselineSharpness" },
+    { TIFFTAG_BAYERGREENSPLIT, 1, 1,   TIFF_LONG,      FIELD_CUSTOM, 
+      0,       0,      "BayerGreenSplit" },
+    { TIFFTAG_LINEARRESPONSELIMIT,     1, 1,   TIFF_RATIONAL,  FIELD_CUSTOM, 
+      0,       0,      "LinearResponseLimit" },
+    { TIFFTAG_CAMERASERIALNUMBER,    -1, -1, TIFF_ASCII,       FIELD_CUSTOM,
+      1,       0,      "CameraSerialNumber" },
+    { TIFFTAG_LENSINFO,        4, 4,   TIFF_RATIONAL,  FIELD_CUSTOM, 
+      0,       0,      "LensInfo" },
+    { TIFFTAG_CHROMABLURRADIUS,        1, 1,   TIFF_RATIONAL,  FIELD_CUSTOM, 
+      0,       0,      "ChromaBlurRadius" },
+    { TIFFTAG_ANTIALIASSTRENGTH,       1, 1,   TIFF_RATIONAL,  FIELD_CUSTOM, 
+      0,       0,      "AntiAliasStrength" },
+    { TIFFTAG_SHADOWSCALE,     1, 1,   TIFF_RATIONAL,  FIELD_CUSTOM, 
+      0,       0,      "ShadowScale" },
+    { TIFFTAG_DNGPRIVATEDATA,    -1, -1, TIFF_BYTE,    FIELD_CUSTOM,
+      0,       1,      "DNGPrivateData" },
+    { TIFFTAG_MAKERNOTESAFETY, 1, 1,   TIFF_SHORT,     FIELD_CUSTOM, 
+      0,       0,      "MakerNoteSafety" },
+    { TIFFTAG_CALIBRATIONILLUMINANT1,  1, 1,   TIFF_SHORT,     FIELD_CUSTOM, 
+      0,       0,      "CalibrationIlluminant1" },
+    { TIFFTAG_CALIBRATIONILLUMINANT2,  1, 1,   TIFF_SHORT,     FIELD_CUSTOM, 
+      0,       0,      "CalibrationIlluminant2" },
+    { TIFFTAG_RAWDATAUNIQUEID, 16, 16, TIFF_BYTE,      FIELD_CUSTOM, 
+      0,       0,      "RawDataUniqueID" },
+    { TIFFTAG_ORIGINALRAWFILENAME,    -1, -1, TIFF_ASCII,      FIELD_CUSTOM,
+      1,       0,      "OriginalRawFileName" },
+    { TIFFTAG_ORIGINALRAWFILENAME,    -1, -1, TIFF_BYTE,       FIELD_CUSTOM,
+      1,       1,      "OriginalRawFileName" },
+    { TIFFTAG_ORIGINALRAWFILEDATA,    -1, -1, TIFF_UNDEFINED,  FIELD_CUSTOM,
+      0,       1,      "OriginalRawFileData" },
+    { TIFFTAG_ACTIVEAREA,      4, 4,   TIFF_LONG,      FIELD_CUSTOM, 
+      0,       0,      "ActiveArea" },
+    { TIFFTAG_ACTIVEAREA,      4, 4,   TIFF_SHORT,     FIELD_CUSTOM, 
+      0,       0,      "ActiveArea" },
+    { TIFFTAG_MASKEDAREAS,     -1, -1, TIFF_LONG,      FIELD_CUSTOM, 
+      0,       1,      "MaskedAreas" },
+    { TIFFTAG_ASSHOTICCPROFILE,    -1, -1, TIFF_UNDEFINED,     FIELD_CUSTOM,
+      0,       1,      "AsShotICCProfile" },
+    { TIFFTAG_ASSHOTPREPROFILEMATRIX,  -1, -1, TIFF_SRATIONAL, FIELD_CUSTOM, 
+      0,       1,      "AsShotPreProfileMatrix" },
+    { TIFFTAG_CURRENTICCPROFILE,    -1, -1, TIFF_UNDEFINED,    FIELD_CUSTOM,
+      0,       1,      "CurrentICCProfile" },
+    { TIFFTAG_CURRENTPREPROFILEMATRIX, -1, -1, TIFF_SRATIONAL, FIELD_CUSTOM, 
+      0,       1,      "CurrentPreProfileMatrix" },
+/* end DNG tags */
 };
-#define        N(a)    (sizeof (a) / sizeof (a[0]))
+
+static const TIFFFieldInfo
+exifFieldInfo[] = {
+    { EXIFTAG_EXPOSURETIME,    1, 1,           TIFF_RATIONAL,  FIELD_CUSTOM, 
+      1,       0,      "ExposureTime" },
+    { EXIFTAG_FNUMBER,         1, 1,           TIFF_RATIONAL,  FIELD_CUSTOM, 
+      1,       0,      "FNumber" },
+    { EXIFTAG_EXPOSUREPROGRAM, 1, 1,           TIFF_SHORT,     FIELD_CUSTOM,
+      1,       0,      "ExposureProgram" },
+    { EXIFTAG_SPECTRALSENSITIVITY,    -1, -1,  TIFF_ASCII,     FIELD_CUSTOM,
+      1,       0,      "SpectralSensitivity" },
+    { EXIFTAG_ISOSPEEDRATINGS,  -1, -1,                TIFF_SHORT,     FIELD_CUSTOM,
+      1,       1,      "ISOSpeedRatings" },
+    { EXIFTAG_OECF,    -1, -1,                 TIFF_UNDEFINED, FIELD_CUSTOM,
+      1,       1,      "OptoelectricConversionFactor" },
+    { EXIFTAG_EXIFVERSION,     4, 4,           TIFF_UNDEFINED, FIELD_CUSTOM,
+      1,       0,      "ExifVersion" },
+    { EXIFTAG_DATETIMEORIGINAL,        20, 20,         TIFF_ASCII,     FIELD_CUSTOM,
+      1,       0,      "DateTimeOriginal" },
+    { EXIFTAG_DATETIMEDIGITIZED, 20, 20,       TIFF_ASCII,     FIELD_CUSTOM,
+      1,       0,      "DateTimeDigitized" },
+    { EXIFTAG_COMPONENTSCONFIGURATION,  4, 4,  TIFF_UNDEFINED, FIELD_CUSTOM,
+      1,       0,      "ComponentsConfiguration" },
+    { EXIFTAG_COMPRESSEDBITSPERPIXEL,   1, 1,  TIFF_RATIONAL,  FIELD_CUSTOM,
+      1,       0,      "CompressedBitsPerPixel" },
+    { EXIFTAG_SHUTTERSPEEDVALUE,       1, 1,   TIFF_SRATIONAL, FIELD_CUSTOM, 
+      1,       0,      "ShutterSpeedValue" },
+    { EXIFTAG_APERTUREVALUE,   1, 1,           TIFF_RATIONAL,  FIELD_CUSTOM, 
+      1,       0,      "ApertureValue" },
+    { EXIFTAG_BRIGHTNESSVALUE, 1, 1,           TIFF_SRATIONAL, FIELD_CUSTOM, 
+      1,       0,      "BrightnessValue" },
+    { EXIFTAG_EXPOSUREBIASVALUE,       1, 1,   TIFF_SRATIONAL, FIELD_CUSTOM, 
+      1,       0,      "ExposureBiasValue" },
+    { EXIFTAG_MAXAPERTUREVALUE,        1, 1,           TIFF_RATIONAL,  FIELD_CUSTOM, 
+      1,       0,      "MaxApertureValue" },
+    { EXIFTAG_SUBJECTDISTANCE, 1, 1,           TIFF_RATIONAL,  FIELD_CUSTOM, 
+      1,       0,      "SubjectDistance" },
+    { EXIFTAG_METERINGMODE,    1, 1,           TIFF_SHORT,     FIELD_CUSTOM,
+      1,       0,      "MeteringMode" },
+    { EXIFTAG_LIGHTSOURCE,     1, 1,           TIFF_SHORT,     FIELD_CUSTOM,
+      1,       0,      "LightSource" },
+    { EXIFTAG_FLASH,   1, 1,                   TIFF_SHORT,     FIELD_CUSTOM,
+      1,       0,      "Flash" },
+    { EXIFTAG_FOCALLENGTH,     1, 1,           TIFF_RATIONAL,  FIELD_CUSTOM, 
+      1,       0,      "FocalLength" },
+    { EXIFTAG_SUBJECTAREA,     -1, -1,         TIFF_SHORT,     FIELD_CUSTOM,
+      1,       1,      "SubjectArea" },
+    { EXIFTAG_MAKERNOTE,       -1, -1,         TIFF_UNDEFINED, FIELD_CUSTOM,
+      1,       1,      "MakerNote" },
+    { EXIFTAG_USERCOMMENT,     -1, -1,         TIFF_UNDEFINED, FIELD_CUSTOM,
+      1,       1,      "UserComment" },
+    { EXIFTAG_SUBSECTIME,    -1, -1,           TIFF_ASCII,     FIELD_CUSTOM,
+      1,       0,      "SubSecTime" },
+    { EXIFTAG_SUBSECTIMEORIGINAL, -1, -1,      TIFF_ASCII,     FIELD_CUSTOM,
+      1,       0,      "SubSecTimeOriginal" },
+    { EXIFTAG_SUBSECTIMEDIGITIZED,-1, -1,      TIFF_ASCII,     FIELD_CUSTOM,
+      1,       0,      "SubSecTimeDigitized" },
+    { EXIFTAG_FLASHPIXVERSION, 4, 4,           TIFF_UNDEFINED, FIELD_CUSTOM,
+      1,       0,      "FlashpixVersion" },
+    { EXIFTAG_COLORSPACE,      1, 1,           TIFF_SHORT,     FIELD_CUSTOM,
+      1,       0,      "ColorSpace" },
+    { EXIFTAG_PIXELXDIMENSION, 1, 1,           TIFF_LONG,      FIELD_CUSTOM,
+      1,       0,      "PixelXDimension" },
+    { EXIFTAG_PIXELXDIMENSION, 1, 1,           TIFF_SHORT,     FIELD_CUSTOM,
+      1,       0,      "PixelXDimension" },
+    { EXIFTAG_PIXELYDIMENSION, 1, 1,           TIFF_LONG,      FIELD_CUSTOM,
+      1,       0,      "PixelYDimension" },
+    { EXIFTAG_PIXELYDIMENSION, 1, 1,           TIFF_SHORT,     FIELD_CUSTOM,
+      1,       0,      "PixelYDimension" },
+    { EXIFTAG_RELATEDSOUNDFILE,        13, 13,         TIFF_ASCII,     FIELD_CUSTOM,
+      1,       0,      "RelatedSoundFile" },
+    { EXIFTAG_FLASHENERGY,     1, 1,           TIFF_RATIONAL,  FIELD_CUSTOM, 
+      1,       0,      "FlashEnergy" },
+    { EXIFTAG_SPATIALFREQUENCYRESPONSE,        -1, -1, TIFF_UNDEFINED, FIELD_CUSTOM,
+      1,       1,      "SpatialFrequencyResponse" },
+    { EXIFTAG_FOCALPLANEXRESOLUTION,   1, 1,   TIFF_RATIONAL,  FIELD_CUSTOM, 
+      1,       0,      "FocalPlaneXResolution" },
+    { EXIFTAG_FOCALPLANEYRESOLUTION,   1, 1,   TIFF_RATIONAL,  FIELD_CUSTOM, 
+      1,       0,      "FocalPlaneYResolution" },
+    { EXIFTAG_FOCALPLANERESOLUTIONUNIT,        1, 1,   TIFF_SHORT,     FIELD_CUSTOM,
+      1,       0,      "FocalPlaneResolutionUnit" },
+    { EXIFTAG_SUBJECTLOCATION, 2, 2,           TIFF_SHORT,     FIELD_CUSTOM,
+      1,       0,      "SubjectLocation" },
+    { EXIFTAG_EXPOSUREINDEX,   1, 1,           TIFF_RATIONAL,  FIELD_CUSTOM, 
+      1,       0,      "ExposureIndex" },
+    { EXIFTAG_SENSINGMETHOD,   1, 1,           TIFF_SHORT,     FIELD_CUSTOM,
+      1,       0,      "SensingMethod" },
+    { EXIFTAG_FILESOURCE,      1, 1,           TIFF_UNDEFINED, FIELD_CUSTOM,
+      1,       0,      "FileSource" },
+    { EXIFTAG_SCENETYPE,       1, 1,           TIFF_UNDEFINED, FIELD_CUSTOM,
+      1,       0,      "SceneType" },
+    { EXIFTAG_CFAPATTERN,      -1, -1,         TIFF_UNDEFINED, FIELD_CUSTOM,
+      1,       1,      "CFAPattern" },
+    { EXIFTAG_CUSTOMRENDERED,  1, 1,           TIFF_SHORT,     FIELD_CUSTOM,
+      1,       0,      "CustomRendered" },
+    { EXIFTAG_EXPOSUREMODE,    1, 1,           TIFF_SHORT,     FIELD_CUSTOM,
+      1,       0,      "ExposureMode" },
+    { EXIFTAG_WHITEBALANCE,    1, 1,           TIFF_SHORT,     FIELD_CUSTOM,
+      1,       0,      "WhiteBalance" },
+    { EXIFTAG_DIGITALZOOMRATIO,        1, 1,           TIFF_RATIONAL,  FIELD_CUSTOM, 
+      1,       0,      "DigitalZoomRatio" },
+    { EXIFTAG_FOCALLENGTHIN35MMFILM, 1, 1,     TIFF_SHORT,     FIELD_CUSTOM,
+      1,       0,      "FocalLengthIn35mmFilm" },
+    { EXIFTAG_SCENECAPTURETYPE,        1, 1,           TIFF_SHORT,     FIELD_CUSTOM,
+      1,       0,      "SceneCaptureType" },
+    { EXIFTAG_GAINCONTROL,     1, 1,           TIFF_RATIONAL,  FIELD_CUSTOM, 
+      1,       0,      "GainControl" },
+    { EXIFTAG_CONTRAST,                1, 1,           TIFF_SHORT,     FIELD_CUSTOM,
+      1,       0,      "Contrast" },
+    { EXIFTAG_SATURATION,      1, 1,           TIFF_SHORT,     FIELD_CUSTOM,
+      1,       0,      "Saturation" },
+    { EXIFTAG_SHARPNESS,       1, 1,           TIFF_SHORT,     FIELD_CUSTOM,
+      1,       0,      "Sharpness" },
+    { EXIFTAG_DEVICESETTINGDESCRIPTION,        -1, -1, TIFF_UNDEFINED, FIELD_CUSTOM,
+      1,       1,      "DeviceSettingDescription" },
+    { EXIFTAG_SUBJECTDISTANCERANGE, 1, 1,      TIFF_SHORT,     FIELD_CUSTOM,
+      1,       0,      "SubjectDistanceRange" },
+    { EXIFTAG_IMAGEUNIQUEID,   33, 33,         TIFF_ASCII,     FIELD_CUSTOM,
+      1,       0,      "ImageUniqueID" }
+};
+
+const TIFFFieldInfo *
+_TIFFGetFieldInfo(size_t *size)
+{
+       *size = TIFFArrayCount(tiffFieldInfo);
+       return tiffFieldInfo;
+}
+
+const TIFFFieldInfo *
+_TIFFGetExifFieldInfo(size_t *size)
+{
+       *size = TIFFArrayCount(exifFieldInfo);
+       return exifFieldInfo;
+}
 
 void
-_TIFFSetupFieldInfo(TIFF* tif)
+_TIFFSetupFieldInfo(TIFF* tif, const TIFFFieldInfo info[], size_t n)
 {
        if (tif->tif_fieldinfo) {
-               int  i;
+               size_t  i;
 
                for (i = 0; i < tif->tif_nfields; i++) 
                {
                        TIFFFieldInfo *fld = tif->tif_fieldinfo[i];
                        if (fld->field_bit == FIELD_CUSTOM && 
-                               strncmp("Tag ", fld->field_name, 4) == 0) 
-                               {
-                               _TIFFfree(fld->field_name);
-                               _TIFFfree(fld);
+                               strncmp("Tag ", fld->field_name, 4) == 0) {
+                                       _TIFFfree(fld->field_name);
+                                       _TIFFfree(fld);
                                }
                }   
       
                _TIFFfree(tif->tif_fieldinfo);
                tif->tif_nfields = 0;
        }
-       _TIFFMergeFieldInfo(tif, tiffFieldInfo, N(tiffFieldInfo));
+       if (!_TIFFMergeFieldInfo(tif, info, n))
+       {
+               TIFFErrorExt(tif->tif_clientdata, "_TIFFSetupFieldInfo",
+                            "Setting up field info failed");
+       }
 }
 
 static int
@@ -286,9 +559,10 @@ tagCompare(const void* a, const void* b)
        const TIFFFieldInfo* tb = *(const TIFFFieldInfo**) b;
        /* NB: be careful of return values for 16-bit platforms */
        if (ta->field_tag != tb->field_tag)
-               return (ta->field_tag < tb->field_tag ? -1 : 1);
+               return (int)ta->field_tag - (int)tb->field_tag;
        else
-               return ((int)tb->field_type - (int)ta->field_type);
+               return (ta->field_type == TIFF_ANY) ?
+                       0 : ((int)tb->field_type - (int)ta->field_type);
 }
 
 static int
@@ -296,13 +570,30 @@ tagNameCompare(const void* a, const void* b)
 {
        const TIFFFieldInfo* ta = *(const TIFFFieldInfo**) a;
        const TIFFFieldInfo* tb = *(const TIFFFieldInfo**) b;
+       int ret = strcmp(ta->field_name, tb->field_name);
 
-        return strcmp(ta->field_name, tb->field_name);
+       if (ret)
+               return ret;
+       else
+               return (ta->field_type == TIFF_ANY) ?
+                       0 : ((int)tb->field_type - (int)ta->field_type);
 }
 
 void
+TIFFMergeFieldInfo(TIFF* tif, const TIFFFieldInfo info[], int n)
+{
+       if (_TIFFMergeFieldInfo(tif, info, n) < 0)
+       {
+               TIFFErrorExt(tif->tif_clientdata, "TIFFMergeFieldInfo",
+                            "Merging block of %d fields failed", n);
+       }
+}
+
+int
 _TIFFMergeFieldInfo(TIFF* tif, const TIFFFieldInfo info[], int n)
 {
+       static const char module[] = "_TIFFMergeFieldInfo";
+       static const char reason[] = "for field info array";
        TIFFFieldInfo** tp;
        int i;
 
@@ -310,32 +601,49 @@ _TIFFMergeFieldInfo(TIFF* tif, const TIFFFieldInfo info[], int n)
 
        if (tif->tif_nfields > 0) {
                tif->tif_fieldinfo = (TIFFFieldInfo**)
-                   _TIFFrealloc(tif->tif_fieldinfo,
-                       (tif->tif_nfields+n) * sizeof (TIFFFieldInfo*));
+                       _TIFFCheckRealloc(tif, tif->tif_fieldinfo,
+                                         (tif->tif_nfields + n),
+                                         sizeof (TIFFFieldInfo*), reason);
        } else {
                tif->tif_fieldinfo = (TIFFFieldInfo**)
-                   _TIFFmalloc(n * sizeof (TIFFFieldInfo*));
+                       _TIFFCheckMalloc(tif, n, sizeof (TIFFFieldInfo*),
+                                        reason);
        }
-       assert(tif->tif_fieldinfo != NULL);
-       tp = &tif->tif_fieldinfo[tif->tif_nfields];
+       if (!tif->tif_fieldinfo) {
+               TIFFErrorExt(tif->tif_clientdata, module,
+                            "Failed to allocate field info array");
+               return 0;
+       }
+       tp = tif->tif_fieldinfo + tif->tif_nfields;
        for (i = 0; i < n; i++)
-               tp[i] = (TIFFFieldInfo*) &info[i];      /* XXX */
+        {
+            const TIFFFieldInfo *fip =
+                _TIFFFindFieldInfo(tif, info[i].field_tag, info[i].field_type);
+
+            /* only add definitions that aren't already present */
+            if (!fip) {
+                *tp++ = (TIFFFieldInfo*) (info + i);
+                tif->tif_nfields++;
+            }
+        }
 
         /* Sort the field info by tag number */
-        qsort(tif->tif_fieldinfo, (size_t) (tif->tif_nfields += n),
-              sizeof (TIFFFieldInfo*), tagCompare);
+        qsort(tif->tif_fieldinfo, tif->tif_nfields,
+             sizeof (TIFFFieldInfo*), tagCompare);
+
+       return n;
 }
 
 void
 _TIFFPrintFieldInfo(TIFF* tif, FILE* fd)
 {
-       int i;
+       size_t i;
 
        fprintf(fd, "%s: \n", tif->tif_name);
        for (i = 0; i < tif->tif_nfields; i++) {
                const TIFFFieldInfo* fip = tif->tif_fieldinfo[i];
                fprintf(fd, "field[%2d] %5lu, %2d, %2d, %d, %2d, %5s, %5s, %s\n"
-                       , i
+                       , (int)i
                        , (unsigned long) fip->field_tag
                        , fip->field_readcount, fip->field_writecount
                        , fip->field_type
@@ -379,6 +687,39 @@ TIFFDataWidth(TIFFDataType type)
 }
 
 /*
+ * Return size of TIFFDataType in bytes.
+ *
+ * XXX: We need a separate function to determine the space needed
+ * to store the value. For TIFF_RATIONAL values TIFFDataWidth() returns 8,
+ * but we use 4-byte float to represent rationals.
+ */
+int
+_TIFFDataSize(TIFFDataType type)
+{
+       switch (type) {
+               case TIFF_BYTE:
+               case TIFF_SBYTE:
+               case TIFF_ASCII:
+               case TIFF_UNDEFINED:
+                   return 1;
+               case TIFF_SHORT:
+               case TIFF_SSHORT:
+                   return 2;
+               case TIFF_LONG:
+               case TIFF_SLONG:
+               case TIFF_FLOAT:
+               case TIFF_IFD:
+               case TIFF_RATIONAL:
+               case TIFF_SRATIONAL:
+                   return 4;
+               case TIFF_DOUBLE:
+                   return 8;
+               default:
+                   return 0;
+       }
+}
+
+/*
  * Return nearest TIFFDataType to the sample type of an image.
  */
 TIFFDataType
@@ -405,56 +746,58 @@ _TIFFSampleToTagType(TIFF* tif)
 const TIFFFieldInfo*
 _TIFFFindFieldInfo(TIFF* tif, ttag_t tag, TIFFDataType dt)
 {
-       int i, n;
+        TIFFFieldInfo key = {0, 0, 0, TIFF_NOTYPE, 0, 0, 0, 0};
+       TIFFFieldInfo* pkey = &key;
+       const TIFFFieldInfo **ret;
 
        if (tif->tif_foundfield && tif->tif_foundfield->field_tag == tag &&
            (dt == TIFF_ANY || dt == tif->tif_foundfield->field_type))
-               return (tif->tif_foundfield);
-       /* NB: use sorted search (e.g. binary search) */
-       if(dt != TIFF_ANY) {
-            TIFFFieldInfo key = {0, 0, 0, 0, 0, 0, 0, 0};
-            key.field_tag = tag;
-            key.field_type = dt;
-            return((const TIFFFieldInfo *) bsearch(&key, 
-                                                  tif->tif_fieldinfo, 
-                                                  tif->tif_nfields,
-                                                  sizeof(TIFFFieldInfo), 
-                                                  tagCompare));
-        } else for (i = 0, n = tif->tif_nfields; i < n; i++) {
-               const TIFFFieldInfo* fip = tif->tif_fieldinfo[i];
-               if (fip->field_tag == tag &&
-                   (dt == TIFF_ANY || fip->field_type == dt))
-                       return (tif->tif_foundfield = fip);
+               return tif->tif_foundfield;
+
+       /* If we are invoked with no field information, then just return. */
+       if ( !tif->tif_fieldinfo ) {
+               return NULL;
        }
-       return ((const TIFFFieldInfo *)0);
+
+       /* NB: use sorted search (e.g. binary search) */
+       key.field_tag = tag;
+        key.field_type = dt;
+
+       ret = (const TIFFFieldInfo **) bsearch(&pkey,
+                                              tif->tif_fieldinfo, 
+                                              tif->tif_nfields,
+                                              sizeof(TIFFFieldInfo *), 
+                                              tagCompare);
+       return tif->tif_foundfield = (ret ? *ret : NULL);
 }
 
 const TIFFFieldInfo*
 _TIFFFindFieldInfoByName(TIFF* tif, const char *field_name, TIFFDataType dt)
 {
-       int i, n;
+        TIFFFieldInfo key = {0, 0, 0, TIFF_NOTYPE, 0, 0, 0, 0};
+       TIFFFieldInfo* pkey = &key;
+       const TIFFFieldInfo **ret;
 
        if (tif->tif_foundfield
            && streq(tif->tif_foundfield->field_name, field_name)
            && (dt == TIFF_ANY || dt == tif->tif_foundfield->field_type))
                return (tif->tif_foundfield);
-       /* NB: use sorted search (e.g. binary search) */
-       if(dt != TIFF_ANY) {
-            TIFFFieldInfo key = {0, 0, 0, 0, 0, 0, 0, 0};
-            key.field_name = (char *)field_name;
-            key.field_type = dt;
-            return((const TIFFFieldInfo *) bsearch(&key, 
-                                                  tif->tif_fieldinfo, 
-                                                  tif->tif_nfields,
-                                                  sizeof(TIFFFieldInfo), 
-                                                  tagNameCompare));
-        } else for (i = 0, n = tif->tif_nfields; i < n; i++) {
-               const TIFFFieldInfo* fip = tif->tif_fieldinfo[i];
-               if (streq(fip->field_name, field_name) &&
-                   (dt == TIFF_ANY || fip->field_type == dt))
-                       return (tif->tif_foundfield = fip);
+
+       /* If we are invoked with no field information, then just return. */
+       if ( !tif->tif_fieldinfo ) {
+               return NULL;
        }
-       return ((const TIFFFieldInfo *)0);
+
+       /* NB: use sorted search (e.g. binary search) */
+        key.field_name = (char *)field_name;
+        key.field_type = dt;
+
+        ret = (const TIFFFieldInfo **) lfind(&pkey,
+                                            tif->tif_fieldinfo, 
+                                            &tif->tif_nfields,
+                                            sizeof(TIFFFieldInfo *),
+                                            tagNameCompare);
+       return tif->tif_foundfield = (ret ? *ret : NULL);
 }
 
 const TIFFFieldInfo*
@@ -462,9 +805,9 @@ _TIFFFieldWithTag(TIFF* tif, ttag_t tag)
 {
        const TIFFFieldInfo* fip = _TIFFFindFieldInfo(tif, tag, TIFF_ANY);
        if (!fip) {
-               TIFFError("TIFFFieldWithTag",
-                         "Internal error, unknown tag 0x%x",
-                          (unsigned int) tag);
+               TIFFErrorExt(tif->tif_clientdata, "TIFFFieldWithTag",
+                            "Internal error, unknown tag 0x%x",
+                            (unsigned int) tag);
                assert(fip != NULL);
                /*NOTREACHED*/
        }
@@ -477,8 +820,8 @@ _TIFFFieldWithName(TIFF* tif, const char *field_name)
        const TIFFFieldInfo* fip =
                _TIFFFindFieldInfoByName(tif, field_name, TIFF_ANY);
        if (!fip) {
-               TIFFError("TIFFFieldWithName",
-                         "Internal error, unknown tag %s", field_name);
+               TIFFErrorExt(tif->tif_clientdata, "TIFFFieldWithName",
+                            "Internal error, unknown tag %s", field_name);
                assert(fip != NULL);
                /*NOTREACHED*/
        }
@@ -495,7 +838,8 @@ _TIFFFindOrRegisterFieldInfo( TIFF *tif, ttag_t tag, TIFFDataType dt )
     if( fld == NULL )
     {
         fld = _TIFFCreateAnonFieldInfo( tif, tag, dt );
-        _TIFFMergeFieldInfo( tif, fld, 1 );
+        if (!_TIFFMergeFieldInfo(tif, fld, 1))
+               return NULL;
     }
 
     return fld;
@@ -504,32 +848,41 @@ _TIFFFindOrRegisterFieldInfo( TIFF *tif, ttag_t tag, TIFFDataType dt )
 TIFFFieldInfo*
 _TIFFCreateAnonFieldInfo(TIFF *tif, ttag_t tag, TIFFDataType field_type)
 {
-    TIFFFieldInfo *fld;
-
-    fld = (TIFFFieldInfo *) _TIFFmalloc(sizeof (TIFFFieldInfo));
-    if (fld == NULL)
-       return NULL;
-    _TIFFmemset( fld, 0, sizeof(TIFFFieldInfo) );
-
-    fld->field_tag = tag;
-    fld->field_readcount = TIFF_VARIABLE;
-    fld->field_writecount = TIFF_VARIABLE;
-    fld->field_type = field_type;
-    fld->field_bit = FIELD_CUSTOM;
-    fld->field_oktochange = TRUE;
-    fld->field_passcount = TRUE;
-    fld->field_name = (char *) _TIFFmalloc(32);
-    if (fld->field_name == NULL) {
-       _TIFFfree(fld);
-       return NULL;
-    }
+       TIFFFieldInfo *fld;
+       (void) tif;
+
+       fld = (TIFFFieldInfo *) _TIFFmalloc(sizeof (TIFFFieldInfo));
+       if (fld == NULL)
+           return NULL;
+       _TIFFmemset( fld, 0, sizeof(TIFFFieldInfo) );
+
+       fld->field_tag = tag;
+       fld->field_readcount = TIFF_VARIABLE2;
+       fld->field_writecount = TIFF_VARIABLE2;
+       fld->field_type = field_type;
+       fld->field_bit = FIELD_CUSTOM;
+       fld->field_oktochange = TRUE;
+       fld->field_passcount = TRUE;
+       fld->field_name = (char *) _TIFFmalloc(32);
+       if (fld->field_name == NULL) {
+           _TIFFfree(fld);
+           return NULL;
+       }
 
-    /* note that this name is a special sign to TIFFClose() and
-     * _TIFFSetupFieldInfo() to free the field
-     */
-    sprintf(fld->field_name, "Tag %d", (int) tag);
+       /* 
+        * note that this name is a special sign to TIFFClose() and
+        * _TIFFSetupFieldInfo() to free the field
+        */
+       sprintf(fld->field_name, "Tag %d", (int) tag);
 
-    return fld;    
+       return fld;    
 }
 
 /* vim: set ts=8 sts=8 sw=8 noet: */
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
index f8636ef..907b531 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: tif_dirread.c,v 1.1 2005-06-17 13:54:52 vp153 Exp $ */
+/* $Id: tif_dirread.c,v 1.92.2.9 2010-06-14 00:21:46 fwarmerdam Exp $ */
 
 /*
  * Copyright (c) 1988-1997 Sam Leffler
 
 #define        IGNORE  0               /* tag placeholder used below */
 
-#if HAVE_IEEEFP
-#define        TIFFCvtIEEEFloatToNative(tif, n, fp)
-#define        TIFFCvtIEEEDoubleToNative(tif, n, dp)
+#ifdef HAVE_IEEEFP
+# define       TIFFCvtIEEEFloatToNative(tif, n, fp)
+# define       TIFFCvtIEEEDoubleToNative(tif, n, dp)
 #else
 extern void TIFFCvtIEEEFloatToNative(TIFF*, uint32, float*);
 extern void TIFFCvtIEEEDoubleToNative(TIFF*, uint32, double*);
 #endif
 
+static  TIFFDirEntry* TIFFReadDirectoryFind(TIFFDirEntry* dir,
+                                           uint16 dircount, uint16 tagid);
 static int EstimateStripByteCounts(TIFF*, TIFFDirEntry*, uint16);
 static void MissingRequired(TIFF*, const char*);
+static int TIFFCheckDirOffset(TIFF*, toff_t);
 static int CheckDirCount(TIFF*, TIFFDirEntry*, uint32);
+static uint16 TIFFFetchDirectory(TIFF*, toff_t, TIFFDirEntry**, toff_t *);
 static tsize_t TIFFFetchData(TIFF*, TIFFDirEntry*, char*);
 static tsize_t TIFFFetchString(TIFF*, TIFFDirEntry*, char*);
 static float TIFFFetchRational(TIFF*, TIFFDirEntry*);
@@ -53,8 +57,8 @@ static        int TIFFFetchPerSampleLongs(TIFF*, TIFFDirEntry*, uint32*);
 static int TIFFFetchPerSampleAnys(TIFF*, TIFFDirEntry*, double*);
 static int TIFFFetchShortArray(TIFF*, TIFFDirEntry*, uint16*);
 static int TIFFFetchStripThing(TIFF*, TIFFDirEntry*, long, uint32**);
-static int TIFFFetchExtraSamples(TIFF*, TIFFDirEntry*);
 static int TIFFFetchRefBlackWhite(TIFF*, TIFFDirEntry*);
+static int TIFFFetchSubjectDistance(TIFF*, TIFFDirEntry*);
 static float TIFFFetchFloat(TIFF*, TIFFDirEntry*);
 static int TIFFFetchFloatArray(TIFF*, TIFFDirEntry*, float*);
 static int TIFFFetchDoubleArray(TIFF*, TIFFDirEntry*, double*);
@@ -62,144 +66,45 @@ static     int TIFFFetchAnyArray(TIFF*, TIFFDirEntry*, double*);
 static int TIFFFetchShortPair(TIFF*, TIFFDirEntry*);
 static void ChopUpSingleUncompressedStrip(TIFF*);
 
-static char *
-CheckMalloc(TIFF* tif, size_t nmemb, size_t elem_size, const char* what)
-{
-       char    *cp = NULL;
-       tsize_t bytes = nmemb * elem_size;
-
-       /*
-        * XXX: Check for integer overflow.
-        */
-       if (nmemb && elem_size && bytes / elem_size == nmemb)
-               cp = (char*)_TIFFmalloc(bytes);
-
-       if (cp == NULL)
-               TIFFError(tif->tif_name, "No space %s", what);
-       
-       return (cp);
-}
-
 /*
- * Read the next TIFF directory from a file
- * and convert it to the internal format.
- * We read directories sequentially.
+ * Read the next TIFF directory from a file and convert it to the internal
+ * format. We read directories sequentially.
  */
 int
 TIFFReadDirectory(TIFF* tif)
 {
        static const char module[] = "TIFFReadDirectory";
 
-       register TIFFDirEntry* dp;
-       register int n;
-       register TIFFDirectory* td;
-       TIFFDirEntry* dir;
+       int n;
+       TIFFDirectory* td;
+       TIFFDirEntry *dp, *dir = NULL;
        uint16 iv;
        uint32 v;
-       double dv;
        const TIFFFieldInfo* fip;
-       int fix;
+       size_t fix;
        uint16 dircount;
-       toff_t nextdiroff;
-       char* cp;
-       int diroutoforderwarning = 0;
-       toff_t* new_dirlist;
+       int diroutoforderwarning = 0, compressionknown = 0;
+       int haveunknowntags = 0;
 
        tif->tif_diroff = tif->tif_nextdiroff;
-       if (tif->tif_diroff == 0)               /* no more directories */
-               return (0);
-
        /*
-        * XXX: Trick to prevent IFD looping. The one can create TIFF file
-        * with looped directory pointers. We will maintain a list of already
-        * seen directories and check every IFD offset against this list.
+        * Check whether we have the last offset or bad offset (IFD looping).
         */
-       for (n = 0; n < tif->tif_dirnumber; n++) {
-               if (tif->tif_dirlist[n] == tif->tif_diroff)
-                       return (0);
-       }
-       tif->tif_dirnumber++;
-       new_dirlist = _TIFFrealloc(tif->tif_dirlist,
-                                  tif->tif_dirnumber * sizeof(toff_t));
-       if (!new_dirlist) {
-               TIFFError(module,
-                         "%s: Failed to allocate space for IFD list",
-                         tif->tif_name);
-               return (0);
-       }
-       tif->tif_dirlist = new_dirlist;
-       tif->tif_dirlist[tif->tif_dirnumber - 1] = tif->tif_diroff;
-
+       if (!TIFFCheckDirOffset(tif, tif->tif_nextdiroff))
+               return 0;
        /*
         * Cleanup any previous compression state.
         */
        (*tif->tif_cleanup)(tif);
        tif->tif_curdir++;
-       nextdiroff = 0;
-       if (!isMapped(tif)) {
-               if (!SeekOK(tif, tif->tif_diroff)) {
-                       TIFFError(module,
-                           "%s: Seek error accessing TIFF directory",
-                            tif->tif_name);
-                       return (0);
-               }
-               if (!ReadOK(tif, &dircount, sizeof (uint16))) {
-                       TIFFError(module,
-                           "%s: Can not read TIFF directory count",
-                            tif->tif_name);
-                       return (0);
-               }
-               if (tif->tif_flags & TIFF_SWAB)
-                       TIFFSwabShort(&dircount);
-               dir = (TIFFDirEntry *)CheckMalloc(tif,
-                                                 dircount,
-                                                 sizeof (TIFFDirEntry),
-                                                 "to read TIFF directory");
-               if (dir == NULL)
-                       return (0);
-               if (!ReadOK(tif, dir, dircount*sizeof (TIFFDirEntry))) {
-                       TIFFError(module,
-                                  "%.100s: Can not read TIFF directory",
-                                  tif->tif_name);
-                       goto bad;
-               }
-               /*
-                * Read offset to next directory for sequential scans.
-                */
-               (void) ReadOK(tif, &nextdiroff, sizeof (uint32));
-       } else {
-               toff_t off = tif->tif_diroff;
-
-               if (off + sizeof (uint16) > tif->tif_size) {
-                       TIFFError(module,
-                           "%s: Can not read TIFF directory count",
-                            tif->tif_name);
-                       return (0);
-               } else
-                       _TIFFmemcpy(&dircount, tif->tif_base + off, sizeof (uint16));
-               off += sizeof (uint16);
-               if (tif->tif_flags & TIFF_SWAB)
-                       TIFFSwabShort(&dircount);
-               dir = (TIFFDirEntry *)CheckMalloc(tif,
-                   dircount, sizeof (TIFFDirEntry), "to read TIFF directory");
-               if (dir == NULL)
-                       return (0);
-               if (off + dircount*sizeof (TIFFDirEntry) > tif->tif_size) {
-                       TIFFError(module,
-                                  "%s: Can not read TIFF directory",
-                                  tif->tif_name);
-                       goto bad;
-               } else {
-                       _TIFFmemcpy(dir, tif->tif_base + off,
-                                   dircount*sizeof (TIFFDirEntry));
-               }
-               off += dircount* sizeof (TIFFDirEntry);
-               if (off + sizeof (uint32) <= tif->tif_size)
-                       _TIFFmemcpy(&nextdiroff, tif->tif_base+off, sizeof (uint32));
+       dircount = TIFFFetchDirectory(tif, tif->tif_nextdiroff,
+                                     &dir, &tif->tif_nextdiroff);
+       if (!dircount) {
+               TIFFErrorExt(tif->tif_clientdata, module,
+                            "%s: Failed to read directory at offset %u",
+                            tif->tif_name, tif->tif_nextdiroff);
+               return 0;
        }
-       if (tif->tif_flags & TIFF_SWAB)
-               TIFFSwabLong(&nextdiroff);
-       tif->tif_nextdiroff = nextdiroff;
 
        tif->tif_flags &= ~TIFF_BEENWRITING;    /* reset before new dir */
        /*
@@ -238,7 +143,7 @@ TIFFReadDirectory(TIFF* tif)
         *
         * It sure would have been nice if Aldus had really thought
         * this stuff through carefully.
-        */ 
+        */
        for (dp = dir, n = dircount; n > 0; n--, dp++) {
                if (tif->tif_flags & TIFF_SWAB) {
                        TIFFSwabArrayOfShort(&dp->tdir_tag, 2);
@@ -256,9 +161,11 @@ TIFFReadDirectory(TIFF* tif)
        fix = 0;
        for (dp = dir, n = dircount; n > 0; n--, dp++) {
 
-               if (fix >= tif->tif_nfields || dp->tdir_tag == IGNORE)
+               if (dp->tdir_tag == IGNORE)
                        continue;
-               
+               if (fix >= tif->tif_nfields)
+                       fix = 0;
+
                /*
                 * Silicon Beach (at least) writes unordered
                 * directory tags (violating the spec).  Handle
@@ -266,33 +173,21 @@ TIFFReadDirectory(TIFF* tif)
                 */
                if (dp->tdir_tag < tif->tif_fieldinfo[fix]->field_tag) {
                        if (!diroutoforderwarning) {
-                               TIFFWarning(module,
-"%s: invalid TIFF directory; tags are not sorted in ascending order",
-                                            tif->tif_name);
+                               TIFFWarningExt(tif->tif_clientdata, module,
+       "%s: invalid TIFF directory; tags are not sorted in ascending order",
+                                           tif->tif_name);
                                diroutoforderwarning = 1;
                        }
                        fix = 0;                        /* O(n^2) */
                }
                while (fix < tif->tif_nfields &&
-                      tif->tif_fieldinfo[fix]->field_tag < dp->tdir_tag)
+                   tif->tif_fieldinfo[fix]->field_tag < dp->tdir_tag)
                        fix++;
                if (fix >= tif->tif_nfields ||
                    tif->tif_fieldinfo[fix]->field_tag != dp->tdir_tag) {
-
-                    TIFFWarning(module,
-                        "%s: unknown field with tag %d (0x%x) encountered",
-                                tif->tif_name, dp->tdir_tag, dp->tdir_tag,
-                                dp->tdir_type);
-
-                    TIFFMergeFieldInfo( tif,
-                                        _TIFFCreateAnonFieldInfo( tif,
-                                              dp->tdir_tag,
-                                             (TIFFDataType) dp->tdir_type ),
-                                        1 );
-                    fix = 0;
-                    while (fix < tif->tif_nfields &&
-                           tif->tif_fieldinfo[fix]->field_tag < dp->tdir_tag)
-                       fix++;
+                       /* Unknown tag ... we'll deal with it below */
+                       haveunknowntags = 1;
+                       continue;
                }
                /*
                 * Null out old tags that we ignore.
@@ -307,13 +202,13 @@ TIFFReadDirectory(TIFF* tif)
                 */
                fip = tif->tif_fieldinfo[fix];
                while (dp->tdir_type != (unsigned short) fip->field_type
-                       && fix < tif->tif_nfields) {
+                   && fix < tif->tif_nfields) {
                        if (fip->field_type == TIFF_ANY)        /* wildcard */
                                break;
-                        fip = tif->tif_fieldinfo[++fix];
+                       fip = tif->tif_fieldinfo[++fix];
                        if (fix >= tif->tif_nfields ||
                            fip->field_tag != dp->tdir_tag) {
-                               TIFFWarning(module,
+                               TIFFWarningExt(tif->tif_clientdata, module,
                        "%s: wrong data type %d for \"%s\"; tag ignored",
                                            tif->tif_name, dp->tdir_type,
                                            tif->tif_fieldinfo[fix-1]->field_name);
@@ -345,6 +240,8 @@ TIFFReadDirectory(TIFF* tif)
                                    dp->tdir_type, dp->tdir_offset);
                                if (!TIFFSetField(tif, dp->tdir_tag, (uint16)v))
                                        goto bad;
+                               else
+                                       compressionknown = 1;
                                break;
                        /* XXX: workaround for broken TIFFs */
                        } else if (dp->tdir_type == TIFF_LONG) {
@@ -372,14 +269,105 @@ TIFFReadDirectory(TIFF* tif)
                case TIFFTAG_TILEDEPTH:
                case TIFFTAG_PLANARCONFIG:
                case TIFFTAG_ROWSPERSTRIP:
+               case TIFFTAG_EXTRASAMPLES:
                        if (!TIFFFetchNormalTag(tif, dp))
                                goto bad;
                        dp->tdir_tag = IGNORE;
                        break;
-               case TIFFTAG_EXTRASAMPLES:
-                       (void) TIFFFetchExtraSamples(tif, dp);
-                       dp->tdir_tag = IGNORE;
-                       break;
+               }
+       }
+
+       /*
+        * If we saw any unknown tags, make an extra pass over the directory
+        * to deal with them.  This must be done separately because the tags
+        * could have become known when we registered a codec after finding
+        * the Compression tag.  In a correctly-sorted directory there's
+        * no problem because Compression will come before any codec-private
+        * tags, but if the sorting is wrong that might not hold.
+        */
+       if (haveunknowntags) {
+           fix = 0;
+           for (dp = dir, n = dircount; n > 0; n--, dp++) {
+               if (dp->tdir_tag == IGNORE)
+                       continue;
+               if (fix >= tif->tif_nfields ||
+                   dp->tdir_tag < tif->tif_fieldinfo[fix]->field_tag)
+                       fix = 0;                        /* O(n^2) */
+               while (fix < tif->tif_nfields &&
+                   tif->tif_fieldinfo[fix]->field_tag < dp->tdir_tag)
+                       fix++;
+               if (fix >= tif->tif_nfields ||
+                   tif->tif_fieldinfo[fix]->field_tag != dp->tdir_tag) {
+
+                                       TIFFWarningExt(tif->tif_clientdata,
+                                                      module,
+                        "%s: unknown field with tag %d (0x%x) encountered",
+                                                      tif->tif_name,
+                                                      dp->tdir_tag,
+                                                      dp->tdir_tag);
+
+                                       if (!_TIFFMergeFieldInfo(tif,
+                                               _TIFFCreateAnonFieldInfo(tif,
+                                               dp->tdir_tag,
+                                               (TIFFDataType) dp->tdir_type),
+                                               1))
+                                       {
+                                       TIFFWarningExt(tif->tif_clientdata,
+                                                      module,
+                       "Registering anonymous field with tag %d (0x%x) failed",
+                                                      dp->tdir_tag,
+                                                      dp->tdir_tag);
+                                       dp->tdir_tag = IGNORE;
+                                       continue;
+                                       }
+                       fix = 0;
+                       while (fix < tif->tif_nfields &&
+                              tif->tif_fieldinfo[fix]->field_tag < dp->tdir_tag)
+                               fix++;
+               }
+               /*
+                * Check data type.
+                */
+               fip = tif->tif_fieldinfo[fix];
+               while (dp->tdir_type != (unsigned short) fip->field_type
+                   && fix < tif->tif_nfields) {
+                       if (fip->field_type == TIFF_ANY)        /* wildcard */
+                               break;
+                       fip = tif->tif_fieldinfo[++fix];
+                       if (fix >= tif->tif_nfields ||
+                           fip->field_tag != dp->tdir_tag) {
+                               TIFFWarningExt(tif->tif_clientdata, module,
+                       "%s: wrong data type %d for \"%s\"; tag ignored",
+                                           tif->tif_name, dp->tdir_type,
+                                           tif->tif_fieldinfo[fix-1]->field_name);
+                               dp->tdir_tag = IGNORE;
+                               break;
+                       }
+               }
+           }
+       }
+
+       /*
+        * XXX: OJPEG hack.
+        * If a) compression is OJPEG, b) planarconfig tag says it's separate,
+        * c) strip offsets/bytecounts tag are both present and
+        * d) both contain exactly one value, then we consistently find
+        * that the buggy implementation of the buggy compression scheme
+        * matches contig planarconfig best. So we 'fix-up' the tag here
+        */
+       if ((td->td_compression==COMPRESSION_OJPEG) &&
+           (td->td_planarconfig==PLANARCONFIG_SEPARATE)) {
+               dp = TIFFReadDirectoryFind(dir,dircount,TIFFTAG_STRIPOFFSETS);
+               if ((dp!=0) && (dp->tdir_count==1)) {
+                       dp = TIFFReadDirectoryFind(dir, dircount,
+                                                  TIFFTAG_STRIPBYTECOUNTS);
+                       if ((dp!=0) && (dp->tdir_count==1)) {
+                               td->td_planarconfig=PLANARCONFIG_CONTIG;
+                               TIFFWarningExt(tif->tif_clientdata,
+                                              "TIFFReadDirectory",
+                               "Planarconfig tag value assumed incorrect, "
+                               "assuming data is contig instead of chunky");
+                       }
                }
        }
 
@@ -390,12 +378,8 @@ TIFFReadDirectory(TIFF* tif)
                MissingRequired(tif, "ImageLength");
                goto bad;
        }
-       if (!TIFFFieldSet(tif, FIELD_PLANARCONFIG)) {
-               MissingRequired(tif, "PlanarConfiguration");
-               goto bad;
-       }
        /* 
-        * Setup appropriate structures (by strip or by tile)
+        * Setup appropriate structures (by strip or by tile)
         */
        if (!TIFFFieldSet(tif, FIELD_TILEDIMENSIONS)) {
                td->td_nstrips = TIFFNumberOfStrips(tif);
@@ -408,17 +392,32 @@ TIFFReadDirectory(TIFF* tif)
                tif->tif_flags |= TIFF_ISTILED;
        }
        if (!td->td_nstrips) {
-               TIFFError(module, "%s: cannot handle zero number of %s",
-                         tif->tif_name, isTiled(tif) ? "tiles" : "strips");
+               TIFFErrorExt(tif->tif_clientdata, module,
+                            "%s: cannot handle zero number of %s",
+                            tif->tif_name, isTiled(tif) ? "tiles" : "strips");
                goto bad;
        }
        td->td_stripsperimage = td->td_nstrips;
        if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
                td->td_stripsperimage /= td->td_samplesperpixel;
        if (!TIFFFieldSet(tif, FIELD_STRIPOFFSETS)) {
-               MissingRequired(tif,
-                   isTiled(tif) ? "TileOffsets" : "StripOffsets");
-               goto bad;
+               if ((td->td_compression==COMPRESSION_OJPEG) &&
+                   (isTiled(tif)==0) &&
+                   (td->td_nstrips==1)) {
+                       /*
+                        * XXX: OJPEG hack.
+                        * If a) compression is OJPEG, b) it's not a tiled TIFF,
+                        * and c) the number of strips is 1,
+                        * then we tolerate the absence of stripoffsets tag,
+                        * because, presumably, all required data is in the
+                        * JpegInterchangeFormat stream.
+                        */
+                       TIFFSetFieldBit(tif, FIELD_STRIPOFFSETS);
+               } else {
+                       MissingRequired(tif,
+                               isTiled(tif) ? "TileOffsets" : "StripOffsets");
+                       goto bad;
+               }
        }
 
        /*
@@ -439,11 +438,11 @@ TIFFReadDirectory(TIFF* tif)
                         * one value per sample.  Because of this, we
                         * accept the tag if one value is supplied.
                         *
-                         * The MinSampleValue, MaxSampleValue, BitsPerSample
-                         * DataType and SampleFormat tags are supposed to be
-                         * written as one value/sample, but some vendors
-                         * incorrectly write one value only -- so we accept
-                         * that as well (yech). Other vendors write correct
+                        * The MinSampleValue, MaxSampleValue, BitsPerSample
+                        * DataType and SampleFormat tags are supposed to be
+                        * written as one value/sample, but some vendors
+                        * incorrectly write one value only -- so we accept
+                        * that as well (yech). Other vendors write correct
                         * value for NumberOfSamples, but incorrect one for
                         * BitsPerSample and friends, and we will read this
                         * too.
@@ -467,9 +466,12 @@ TIFFReadDirectory(TIFF* tif)
                        break;
                case TIFFTAG_SMINSAMPLEVALUE:
                case TIFFTAG_SMAXSAMPLEVALUE:
-                       if (!TIFFFetchPerSampleAnys(tif, dp, &dv) ||
-                           !TIFFSetField(tif, dp->tdir_tag, dv))
-                               goto bad;
+                       {
+                               double dv = 0.0;
+                               if (!TIFFFetchPerSampleAnys(tif, dp, &dv) ||
+                                   !TIFFSetField(tif, dp->tdir_tag, dv))
+                                       goto bad;
+                       }
                        break;
                case TIFFTAG_STRIPOFFSETS:
                case TIFFTAG_TILEOFFSETS:
@@ -485,34 +487,41 @@ TIFFReadDirectory(TIFF* tif)
                        break;
                case TIFFTAG_COLORMAP:
                case TIFFTAG_TRANSFERFUNCTION:
-                       /*
-                        * TransferFunction can have either 1x or 3x data
-                        * values; Colormap can have only 3x items.
-                        */
-                       v = 1L<<td->td_bitspersample;
-                       if (dp->tdir_tag == TIFFTAG_COLORMAP ||
-                           dp->tdir_count != v) {
-                               if (!CheckDirCount(tif, dp, 3 * v))
-                                       break;
-                       }
-                       v *= sizeof(uint16);
-                       cp = CheckMalloc(tif, dp->tdir_count, sizeof (uint16),
-                           "to read \"TransferFunction\" tag");
-                       if (cp != NULL) {
-                               if (TIFFFetchData(tif, dp, cp)) {
-                                       /*
-                                        * This deals with there being only
-                                        * one array to apply to all samples.
-                                        */
-                                       uint32 c = 1L << td->td_bitspersample;
-                                       if (dp->tdir_count == c)
-                                               v = 0L;
-                                       TIFFSetField(tif, dp->tdir_tag,
-                                           cp, cp+v, cp+2*v);
+                       {
+                               char* cp;
+                               /*
+                                * TransferFunction can have either 1x or 3x
+                                * data values; Colormap can have only 3x
+                                * items.
+                                */
+                               v = 1L<<td->td_bitspersample;
+                               if (dp->tdir_tag == TIFFTAG_COLORMAP ||
+                                   dp->tdir_count != v) {
+                                       if (!CheckDirCount(tif, dp, 3 * v))
+                                               break;
                                }
-                               _TIFFfree(cp);
+                               v *= sizeof(uint16);
+                               cp = (char *)_TIFFCheckMalloc(tif,
+                                                             dp->tdir_count,
+                                                             sizeof (uint16),
+                                       "to read \"TransferFunction\" tag");
+                               if (cp != NULL) {
+                                       if (TIFFFetchData(tif, dp, cp)) {
+                                               /*
+                                                * This deals with there being
+                                                * only one array to apply to
+                                                * all samples.
+                                                */
+                                               uint32 c = 1L << td->td_bitspersample;
+                                               if (dp->tdir_count == c)
+                                                       v = 0L;
+                                               TIFFSetField(tif, dp->tdir_tag,
+                                                   cp, cp+v, cp+2*v);
+                                       }
+                                       _TIFFfree(cp);
+                               }
+                               break;
                        }
-                       break;
                case TIFFTAG_PAGENUMBER:
                case TIFFTAG_HALFTONEHINTS:
                case TIFFTAG_YCBCRSUBSAMPLING:
@@ -544,6 +553,69 @@ TIFFReadDirectory(TIFF* tif)
                }
        }
        /*
+        * OJPEG hack:
+        * - If a) compression is OJPEG, and b) photometric tag is missing,
+        * then we consistently find that photometric should be YCbCr
+        * - If a) compression is OJPEG, and b) photometric tag says it's RGB,
+        * then we consistently find that the buggy implementation of the
+        * buggy compression scheme matches photometric YCbCr instead.
+        * - If a) compression is OJPEG, and b) bitspersample tag is missing,
+        * then we consistently find bitspersample should be 8.
+        * - If a) compression is OJPEG, b) samplesperpixel tag is missing,
+        * and c) photometric is RGB or YCbCr, then we consistently find
+        * samplesperpixel should be 3
+        * - If a) compression is OJPEG, b) samplesperpixel tag is missing,
+        * and c) photometric is MINISWHITE or MINISBLACK, then we consistently
+        * find samplesperpixel should be 3
+        */
+       if (td->td_compression==COMPRESSION_OJPEG)
+       {
+               if (!TIFFFieldSet(tif,FIELD_PHOTOMETRIC))
+               {
+                       TIFFWarningExt(tif->tif_clientdata, "TIFFReadDirectory",
+                       "Photometric tag is missing, assuming data is YCbCr");
+                       if (!TIFFSetField(tif,TIFFTAG_PHOTOMETRIC,PHOTOMETRIC_YCBCR))
+                               goto bad;
+               }
+               else if (td->td_photometric==PHOTOMETRIC_RGB)
+               {
+                       td->td_photometric=PHOTOMETRIC_YCBCR;
+                       TIFFWarningExt(tif->tif_clientdata, "TIFFReadDirectory",
+                       "Photometric tag value assumed incorrect, "
+                       "assuming data is YCbCr instead of RGB");
+               }
+               if (!TIFFFieldSet(tif,FIELD_BITSPERSAMPLE))
+               {
+                       TIFFWarningExt(tif->tif_clientdata,"TIFFReadDirectory",
+               "BitsPerSample tag is missing, assuming 8 bits per sample");
+                       if (!TIFFSetField(tif,TIFFTAG_BITSPERSAMPLE,8))
+                               goto bad;
+               }
+               if (!TIFFFieldSet(tif,FIELD_SAMPLESPERPIXEL))
+               {
+                       if ((td->td_photometric==PHOTOMETRIC_RGB)
+                           || (td->td_photometric==PHOTOMETRIC_YCBCR))
+                       {
+                               TIFFWarningExt(tif->tif_clientdata,
+                                              "TIFFReadDirectory",
+                               "SamplesPerPixel tag is missing, "
+                               "assuming correct SamplesPerPixel value is 3");
+                               if (!TIFFSetField(tif,TIFFTAG_SAMPLESPERPIXEL,3))
+                                       goto bad;
+                       }
+                       else if ((td->td_photometric==PHOTOMETRIC_MINISWHITE)
+                                || (td->td_photometric==PHOTOMETRIC_MINISBLACK))
+                       {
+                               TIFFWarningExt(tif->tif_clientdata,
+                                              "TIFFReadDirectory",
+                               "SamplesPerPixel tag is missing, "
+                               "assuming correct SamplesPerPixel value is 1");
+                               if (!TIFFSetField(tif,TIFFTAG_SAMPLESPERPIXEL,1))
+                                       goto bad;
+                       }
+               }
+       }
+       /*
         * Verify Palette image has a Colormap.
         */
        if (td->td_photometric == PHOTOMETRIC_PALETTE &&
@@ -552,59 +624,90 @@ TIFFReadDirectory(TIFF* tif)
                goto bad;
        }
        /*
-        * Attempt to deal with a missing StripByteCounts tag.
+        * OJPEG hack:
+        * We do no further messing with strip/tile offsets/bytecounts in OJPEG
+        * TIFFs
         */
-       if (!TIFFFieldSet(tif, FIELD_STRIPBYTECOUNTS)) {
+       if (td->td_compression!=COMPRESSION_OJPEG)
+       {
                /*
-                * Some manufacturers violate the spec by not giving
-                * the size of the strips.  In this case, assume there
-                * is one uncompressed strip of data.
+                * Attempt to deal with a missing StripByteCounts tag.
                 */
-               if ((td->td_planarconfig == PLANARCONFIG_CONTIG &&
-                   td->td_nstrips > 1) ||
-                   (td->td_planarconfig == PLANARCONFIG_SEPARATE &&
-                    td->td_nstrips != td->td_samplesperpixel)) {
-                   MissingRequired(tif, "StripByteCounts");
-                   goto bad;
-               }
-               TIFFWarning(module,
-                       "%s: TIFF directory is missing required "
-                       "\"%s\" field, calculating from imagelength",
-                       tif->tif_name,
-                       _TIFFFieldWithTag(tif,TIFFTAG_STRIPBYTECOUNTS)->field_name);
-               if (EstimateStripByteCounts(tif, dir, dircount) < 0)
-                   goto bad;
-/* 
- * Assume we have wrong StripByteCount value (in case of single strip) in
- * following cases:
- *   - it is equal to zero along with StripOffset;
- *   - it is larger than file itself (in case of uncompressed image);
- *   - it is smaller than the size of the bytes per row multiplied on the
- *     number of rows.  The last case should not be checked in the case of
- *     writing new image, because we may do not know the exact strip size
- *     until the whole image will be written and directory dumped out.
- */
-#define        BYTECOUNTLOOKSBAD \
-    ( (td->td_stripbytecount[0] == 0 && td->td_stripoffset[0] != 0) || \
-      (td->td_compression == COMPRESSION_NONE && \
-       td->td_stripbytecount[0] > TIFFGetFileSize(tif) - td->td_stripoffset[0]) || \
-      (tif->tif_mode == O_RDONLY && \
-       td->td_compression == COMPRESSION_NONE && \
-       td->td_stripbytecount[0] < TIFFScanlineSize(tif) * td->td_imagelength) )
-       } else if (td->td_nstrips == 1 && BYTECOUNTLOOKSBAD) {
+               if (!TIFFFieldSet(tif, FIELD_STRIPBYTECOUNTS)) {
+                       /*
+                        * Some manufacturers violate the spec by not giving
+                        * the size of the strips.  In this case, assume there
+                        * is one uncompressed strip of data.
+                        */
+                       if ((td->td_planarconfig == PLANARCONFIG_CONTIG &&
+                           td->td_nstrips > 1) ||
+                           (td->td_planarconfig == PLANARCONFIG_SEPARATE &&
+                            td->td_nstrips != td->td_samplesperpixel)) {
+                           MissingRequired(tif, "StripByteCounts");
+                           goto bad;
+                       }
+                       TIFFWarningExt(tif->tif_clientdata, module,
+                               "%s: TIFF directory is missing required "
+                               "\"%s\" field, calculating from imagelength",
+                               tif->tif_name,
+                               _TIFFFieldWithTag(tif,TIFFTAG_STRIPBYTECOUNTS)->field_name);
+                       if (EstimateStripByteCounts(tif, dir, dircount) < 0)
+                           goto bad;
                /*
-                * Plexus (and others) sometimes give a value
-                * of zero for a tag when they don't know what
-                * the correct value is!  Try and handle the
-                * simple case of estimating the size of a one
-                * strip image.
+                * Assume we have wrong StripByteCount value (in case
+                * of single strip) in following cases:
+                *   - it is equal to zero along with StripOffset;
+                *   - it is larger than file itself (in case of uncompressed
+                *     image);
+                *   - it is smaller than the size of the bytes per row
+                *     multiplied on the number of rows.  The last case should
+                *     not be checked in the case of writing new image,
+                *     because we may do not know the exact strip size
+                *     until the whole image will be written and directory
+                *     dumped out.
                 */
-               TIFFWarning(module,
+               #define BYTECOUNTLOOKSBAD \
+                   ( (td->td_stripbytecount[0] == 0 && td->td_stripoffset[0] != 0) || \
+                     (td->td_compression == COMPRESSION_NONE && \
+                      td->td_stripbytecount[0] > TIFFGetFileSize(tif) - td->td_stripoffset[0]) || \
+                     (tif->tif_mode == O_RDONLY && \
+                      td->td_compression == COMPRESSION_NONE && \
+                      td->td_stripbytecount[0] < TIFFScanlineSize(tif) * td->td_imagelength) )
+
+               } else if (td->td_nstrips == 1
+                          && td->td_stripoffset[0] != 0
+                          && BYTECOUNTLOOKSBAD) {
+                       /*
+                        * XXX: Plexus (and others) sometimes give a value of
+                        * zero for a tag when they don't know what the
+                        * correct value is!  Try and handle the simple case
+                        * of estimating the size of a one strip image.
+                        */
+                       TIFFWarningExt(tif->tif_clientdata, module,
        "%s: Bogus \"%s\" field, ignoring and calculating from imagelength",
-                            tif->tif_name,
-                           _TIFFFieldWithTag(tif,TIFFTAG_STRIPBYTECOUNTS)->field_name);
-               if(EstimateStripByteCounts(tif, dir, dircount) < 0)
-                   goto bad;
+                                   tif->tif_name,
+                                   _TIFFFieldWithTag(tif,TIFFTAG_STRIPBYTECOUNTS)->field_name);
+                       if(EstimateStripByteCounts(tif, dir, dircount) < 0)
+                           goto bad;
+               } else if (td->td_planarconfig == PLANARCONFIG_CONTIG
+                          && td->td_nstrips > 2
+                          && td->td_compression == COMPRESSION_NONE
+                          && td->td_stripbytecount[0] != td->td_stripbytecount[1]
+                           && td->td_stripbytecount[0] != 0 
+                           && td->td_stripbytecount[1] != 0 ) {
+                       /*
+                        * XXX: Some vendors fill StripByteCount array with 
+                         * absolutely wrong values (it can be equal to 
+                         * StripOffset array, for example). Catch this case 
+                         * here.
+                        */
+                       TIFFWarningExt(tif->tif_clientdata, module,
+       "%s: Wrong \"%s\" field, ignoring and calculating from imagelength",
+                                   tif->tif_name,
+                                   _TIFFFieldWithTag(tif,TIFFTAG_STRIPBYTECOUNTS)->field_name);
+                       if (EstimateStripByteCounts(tif, dir, dircount) < 0)
+                           goto bad;
+               }
        }
        if (dir) {
                _TIFFfree((char *)dir);
@@ -636,15 +739,15 @@ TIFFReadDirectory(TIFF* tif)
 
        if (!TIFFFieldSet(tif, FIELD_COMPRESSION))
                TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
-        /*
-         * Some manufacturers make life difficult by writing
+       /*
+        * Some manufacturers make life difficult by writing
         * large amounts of uncompressed data as a single strip.
         * This is contrary to the recommendations of the spec.
-         * The following makes an attempt at breaking such images
+        * The following makes an attempt at breaking such images
         * into strips closer to the recommended 8k bytes.  A
         * side effect, however, is that the RowsPerStrip tag
         * value may be changed.
-         */
+        */
        if (td->td_nstrips == 1 && td->td_compression == COMPRESSION_NONE &&
            (tif->tif_flags & (TIFF_STRIPCHOP|TIFF_ISTILED)) == TIFF_STRIPCHOP)
                ChopUpSingleUncompressedStrip(tif);
@@ -660,22 +763,25 @@ TIFFReadDirectory(TIFF* tif)
 
        tif->tif_scanlinesize = TIFFScanlineSize(tif);
        if (!tif->tif_scanlinesize) {
-               TIFFError(module, "%s: cannot handle zero scanline size",
-                         tif->tif_name);
+               TIFFErrorExt(tif->tif_clientdata, module,
+                            "%s: cannot handle zero scanline size",
+                            tif->tif_name);
                return (0);
        }
 
        if (isTiled(tif)) {
                tif->tif_tilesize = TIFFTileSize(tif);
                if (!tif->tif_tilesize) {
-                       TIFFError(module, "%s: cannot handle zero tile size",
-                                 tif->tif_name);
+                       TIFFErrorExt(tif->tif_clientdata, module,
+                                    "%s: cannot handle zero tile size",
+                                    tif->tif_name);
                        return (0);
                }
        } else {
                if (!TIFFStripSize(tif)) {
-                       TIFFError(module, "%s: cannot handle zero strip size",
-                                 tif->tif_name);
+                       TIFFErrorExt(tif->tif_clientdata, module,
+                                    "%s: cannot handle zero strip size",
+                                    tif->tif_name);
                        return (0);
                }
        }
@@ -686,20 +792,172 @@ bad:
        return (0);
 }
 
+static TIFFDirEntry*
+TIFFReadDirectoryFind(TIFFDirEntry* dir, uint16 dircount, uint16 tagid)
+{
+       TIFFDirEntry* m;
+       uint16 n;
+       for (m=dir, n=0; n<dircount; m++, n++)
+       {
+               if (m->tdir_tag==tagid)
+                       return(m);
+       }
+       return(0);
+}
+
+/*
+ * Read custom directory from the arbitarry offset.
+ * The code is very similar to TIFFReadDirectory().
+ */
+int
+TIFFReadCustomDirectory(TIFF* tif, toff_t diroff,
+                       const TIFFFieldInfo info[], size_t n)
+{
+       static const char module[] = "TIFFReadCustomDirectory";
+
+       TIFFDirectory* td = &tif->tif_dir;
+       TIFFDirEntry *dp, *dir = NULL;
+       const TIFFFieldInfo* fip;
+       size_t fix;
+       uint16 i, dircount;
+
+       _TIFFSetupFieldInfo(tif, info, n);
+
+       dircount = TIFFFetchDirectory(tif, diroff, &dir, NULL);
+       if (!dircount) {
+               TIFFErrorExt(tif->tif_clientdata, module,
+                       "%s: Failed to read custom directory at offset %u",
+                            tif->tif_name, diroff);
+               return 0;
+       }
+
+       TIFFFreeDirectory(tif);
+        _TIFFmemset(&tif->tif_dir, 0, sizeof(TIFFDirectory));
+
+       fix = 0;
+       for (dp = dir, i = dircount; i > 0; i--, dp++) {
+               if (tif->tif_flags & TIFF_SWAB) {
+                       TIFFSwabArrayOfShort(&dp->tdir_tag, 2);
+                       TIFFSwabArrayOfLong(&dp->tdir_count, 2);
+               }
+
+               if (fix >= tif->tif_nfields || dp->tdir_tag == IGNORE)
+                       continue;
+
+               while (fix < tif->tif_nfields &&
+                      tif->tif_fieldinfo[fix]->field_tag < dp->tdir_tag)
+                       fix++;
+
+               if (fix >= tif->tif_nfields ||
+                   tif->tif_fieldinfo[fix]->field_tag != dp->tdir_tag) {
+
+                       TIFFWarningExt(tif->tif_clientdata, module,
+                        "%s: unknown field with tag %d (0x%x) encountered",
+                                   tif->tif_name, dp->tdir_tag, dp->tdir_tag);
+                       if (!_TIFFMergeFieldInfo(tif,
+                                                _TIFFCreateAnonFieldInfo(tif,
+                                                dp->tdir_tag,
+                                                (TIFFDataType) dp->tdir_type),
+                                                1))
+                       {
+                               TIFFWarningExt(tif->tif_clientdata, module,
+                       "Registering anonymous field with tag %d (0x%x) failed",
+                                               dp->tdir_tag, dp->tdir_tag);
+                               goto ignore;
+                       }
+
+                       fix = 0;
+                       while (fix < tif->tif_nfields &&
+                              tif->tif_fieldinfo[fix]->field_tag < dp->tdir_tag)
+                               fix++;
+               }
+               /*
+                * Null out old tags that we ignore.
+                */
+               if (tif->tif_fieldinfo[fix]->field_bit == FIELD_IGNORE) {
+       ignore:
+                       dp->tdir_tag = IGNORE;
+                       continue;
+               }
+               /*
+                * Check data type.
+                */
+               fip = tif->tif_fieldinfo[fix];
+               while (dp->tdir_type != (unsigned short) fip->field_type
+                       && fix < tif->tif_nfields) {
+                       if (fip->field_type == TIFF_ANY)        /* wildcard */
+                               break;
+                        fip = tif->tif_fieldinfo[++fix];
+                       if (fix >= tif->tif_nfields ||
+                           fip->field_tag != dp->tdir_tag) {
+                               TIFFWarningExt(tif->tif_clientdata, module,
+                       "%s: wrong data type %d for \"%s\"; tag ignored",
+                                           tif->tif_name, dp->tdir_type,
+                                           tif->tif_fieldinfo[fix-1]->field_name);
+                               goto ignore;
+                       }
+               }
+               /*
+                * Check count if known in advance.
+                */
+               if (fip->field_readcount != TIFF_VARIABLE
+                   && fip->field_readcount != TIFF_VARIABLE2) {
+                       uint32 expected = (fip->field_readcount == TIFF_SPP) ?
+                           (uint32) td->td_samplesperpixel :
+                           (uint32) fip->field_readcount;
+                       if (!CheckDirCount(tif, dp, expected))
+                               goto ignore;
+               }
+
+               /*
+                * EXIF tags which need to be specifically processed.
+                */
+               switch (dp->tdir_tag) {
+                       case EXIFTAG_SUBJECTDISTANCE:
+                               (void) TIFFFetchSubjectDistance(tif, dp);
+                               break;
+                       default:
+                               (void) TIFFFetchNormalTag(tif, dp);
+                               break;
+               }
+       }
+       
+       if (dir)
+               _TIFFfree(dir);
+       return 1;
+}
+
+/*
+ * EXIF is important special case of custom IFD, so we have a special
+ * function to read it.
+ */
+int
+TIFFReadEXIFDirectory(TIFF* tif, toff_t diroff)
+{
+       size_t exifFieldInfoCount;
+       const TIFFFieldInfo *exifFieldInfo =
+               _TIFFGetExifFieldInfo(&exifFieldInfoCount);
+       return TIFFReadCustomDirectory(tif, diroff, exifFieldInfo,
+                                      exifFieldInfoCount);
+}
+
 static int
 EstimateStripByteCounts(TIFF* tif, TIFFDirEntry* dir, uint16 dircount)
 {
        static const char module[] = "EstimateStripByteCounts";
 
-       register TIFFDirEntry *dp;
-       register TIFFDirectory *td = &tif->tif_dir;
-       uint16 i;
+       TIFFDirEntry *dp;
+       TIFFDirectory *td = &tif->tif_dir;
+       uint32 strip;
 
        if (td->td_stripbytecount)
                _TIFFfree(td->td_stripbytecount);
        td->td_stripbytecount = (uint32*)
-           CheckMalloc(tif, td->td_nstrips, sizeof (uint32),
+           _TIFFCheckMalloc(tif, td->td_nstrips, sizeof (uint32),
                "for \"StripByteCounts\" array");
+        if( td->td_stripbytecount == NULL )
+            return -1;
+
        if (td->td_compression != COMPRESSION_NONE) {
                uint32 space = (uint32)(sizeof (TIFFHeader)
                    + sizeof (uint16)
@@ -713,7 +971,7 @@ EstimateStripByteCounts(TIFF* tif, TIFFDirEntry* dir, uint16 dircount)
                {
                        uint32 cc = TIFFDataWidth((TIFFDataType) dp->tdir_type);
                        if (cc == 0) {
-                               TIFFError(module,
+                               TIFFErrorExt(tif->tif_clientdata, module,
                        "%s: Cannot determine size of unknown tag type %d",
                                          tif->tif_name, dp->tdir_type);
                                return -1;
@@ -725,8 +983,8 @@ EstimateStripByteCounts(TIFF* tif, TIFFDirEntry* dir, uint16 dircount)
                space = filesize - space;
                if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
                        space /= td->td_samplesperpixel;
-               for (i = 0; i < td->td_nstrips; i++)
-                       td->td_stripbytecount[i] = space;
+               for (strip = 0; strip < td->td_nstrips; strip++)
+                       td->td_stripbytecount[strip] = space;
                /*
                 * This gross hack handles the case were the offset to
                 * the last strip is past the place where we think the strip
@@ -734,16 +992,21 @@ EstimateStripByteCounts(TIFF* tif, TIFFDirEntry* dir, uint16 dircount)
                 * it's safe to assume that we've overestimated the amount
                 * of data in the strip and trim this number back accordingly.
                 */ 
-               i--;
-               if (((toff_t)(td->td_stripoffset[i]+td->td_stripbytecount[i]))
-                                                               > filesize)
-                       td->td_stripbytecount[i] =
-                           filesize - td->td_stripoffset[i];
+               strip--;
+               if (((toff_t)(td->td_stripoffset[strip]+
+                             td->td_stripbytecount[strip])) > filesize)
+                       td->td_stripbytecount[strip] =
+                           filesize - td->td_stripoffset[strip];
+       } else if (isTiled(tif)) {
+               uint32 bytespertile = TIFFTileSize(tif);
+
+               for (strip = 0; strip < td->td_nstrips; strip++)
+                    td->td_stripbytecount[strip] = bytespertile;
        } else {
                uint32 rowbytes = TIFFScanlineSize(tif);
                uint32 rowsperstrip = td->td_imagelength/td->td_stripsperimage;
-               for (i = 0; i < td->td_nstrips; i++)
-                       td->td_stripbytecount[i] = rowbytes*rowsperstrip;
+               for (strip = 0; strip < td->td_nstrips; strip++)
+                       td->td_stripbytecount[strip] = rowbytes * rowsperstrip;
        }
        TIFFSetFieldBit(tif, FIELD_STRIPBYTECOUNTS);
        if (!TIFFFieldSet(tif, FIELD_ROWSPERSTRIP))
@@ -756,29 +1019,71 @@ MissingRequired(TIFF* tif, const char* tagname)
 {
        static const char module[] = "MissingRequired";
 
-       TIFFError(module,
+       TIFFErrorExt(tif->tif_clientdata, module,
                  "%s: TIFF directory is missing required \"%s\" field",
                  tif->tif_name, tagname);
 }
 
 /*
- * Check the count field of a directory
- * entry against a known value.  The caller
- * is expected to skip/ignore the tag if
- * there is a mismatch.
+ * Check the directory offset against the list of already seen directory
+ * offsets. This is a trick to prevent IFD looping. The one can create TIFF
+ * file with looped directory pointers. We will maintain a list of already
+ * seen directories and check every IFD offset against that list.
+ */
+static int
+TIFFCheckDirOffset(TIFF* tif, toff_t diroff)
+{
+       uint16 n;
+
+       if (diroff == 0)                        /* no more directories */
+               return 0;
+
+       for (n = 0; n < tif->tif_dirnumber && tif->tif_dirlist; n++) {
+               if (tif->tif_dirlist[n] == diroff)
+                       return 0;
+       }
+
+       tif->tif_dirnumber++;
+
+       if (tif->tif_dirnumber > tif->tif_dirlistsize) {
+               toff_t* new_dirlist;
+
+               /*
+                * XXX: Reduce memory allocation granularity of the dirlist
+                * array.
+                */
+               new_dirlist = (toff_t *)_TIFFCheckRealloc(tif,
+                                                         tif->tif_dirlist,
+                                                         tif->tif_dirnumber,
+                                                         2 * sizeof(toff_t),
+                                                         "for IFD list");
+               if (!new_dirlist)
+                       return 0;
+               tif->tif_dirlistsize = 2 * tif->tif_dirnumber;
+               tif->tif_dirlist = new_dirlist;
+       }
+
+       tif->tif_dirlist[tif->tif_dirnumber - 1] = diroff;
+
+       return 1;
+}
+
+/*
+ * Check the count field of a directory entry against a known value.  The
+ * caller is expected to skip/ignore the tag if there is a mismatch.
  */
 static int
 CheckDirCount(TIFF* tif, TIFFDirEntry* dir, uint32 count)
 {
        if (count > dir->tdir_count) {
-               TIFFWarning(tif->tif_name,
-       "incorrect count for field \"%s\" (%lu, expecting %lu); tag ignored",
+               TIFFWarningExt(tif->tif_clientdata, tif->tif_name,
+       "incorrect count for field \"%s\" (%u, expecting %u); tag ignored",
                    _TIFFFieldWithTag(tif, dir->tdir_tag)->field_name,
                    dir->tdir_count, count);
                return (0);
        } else if (count < dir->tdir_count) {
-               TIFFWarning(tif->tif_name,
-       "incorrect count for field \"%s\" (%lu, expecting %lu); tag trimmed",
+               TIFFWarningExt(tif->tif_clientdata, tif->tif_name,
+       "incorrect count for field \"%s\" (%u, expecting %u); tag trimmed",
                    _TIFFFieldWithTag(tif, dir->tdir_tag)->field_name,
                    dir->tdir_count, count);
                return (1);
@@ -787,13 +1092,129 @@ CheckDirCount(TIFF* tif, TIFFDirEntry* dir, uint32 count)
 }
 
 /*
+ * Read IFD structure from the specified offset. If the pointer to
+ * nextdiroff variable has been specified, read it too. Function returns a
+ * number of fields in the directory or 0 if failed.
+ */
+static uint16
+TIFFFetchDirectory(TIFF* tif, toff_t diroff, TIFFDirEntry **pdir,
+                  toff_t *nextdiroff)
+{
+       static const char module[] = "TIFFFetchDirectory";
+
+       TIFFDirEntry *dir;
+       uint16 dircount;
+
+       assert(pdir);
+
+       tif->tif_diroff = diroff;
+       if (nextdiroff)
+               *nextdiroff = 0;
+       if (!isMapped(tif)) {
+               if (!SeekOK(tif, tif->tif_diroff)) {
+                       TIFFErrorExt(tif->tif_clientdata, module,
+                               "%s: Seek error accessing TIFF directory",
+                               tif->tif_name);
+                       return 0;
+               }
+               if (!ReadOK(tif, &dircount, sizeof (uint16))) {
+                       TIFFErrorExt(tif->tif_clientdata, module,
+                               "%s: Can not read TIFF directory count",
+                               tif->tif_name);
+                       return 0;
+               }
+               if (tif->tif_flags & TIFF_SWAB)
+                       TIFFSwabShort(&dircount);
+               dir = (TIFFDirEntry *)_TIFFCheckMalloc(tif, dircount,
+                                               sizeof (TIFFDirEntry),
+                                               "to read TIFF directory");
+               if (dir == NULL)
+                       return 0;
+               if (!ReadOK(tif, dir, dircount*sizeof (TIFFDirEntry))) {
+                       TIFFErrorExt(tif->tif_clientdata, module,
+                               "%.100s: Can not read TIFF directory",
+                               tif->tif_name);
+                       _TIFFfree(dir);
+                       return 0;
+               }
+               /*
+                * Read offset to next directory for sequential scans if
+                * needed.
+                */
+               if (nextdiroff)
+                       (void) ReadOK(tif, nextdiroff, sizeof(uint32));
+       } else {
+               toff_t off = tif->tif_diroff;
+
+               /*
+                * Check for integer overflow when validating the dir_off,
+                * otherwise a very high offset may cause an OOB read and
+                * crash the client. Make two comparisons instead of
+                *
+                *  off + sizeof(uint16) > tif->tif_size
+                *
+                * to avoid overflow.
+                */
+               if (tif->tif_size < sizeof (uint16) ||
+                   off > tif->tif_size - sizeof(uint16)) {
+                       TIFFErrorExt(tif->tif_clientdata, module,
+                               "%s: Can not read TIFF directory count",
+                               tif->tif_name);
+                       return 0;
+               } else {
+                       _TIFFmemcpy(&dircount, tif->tif_base + off,
+                                   sizeof(uint16));
+               }
+               off += sizeof (uint16);
+               if (tif->tif_flags & TIFF_SWAB)
+                       TIFFSwabShort(&dircount);
+               dir = (TIFFDirEntry *)_TIFFCheckMalloc(tif, dircount,
+                                               sizeof(TIFFDirEntry),
+                                               "to read TIFF directory");
+               if (dir == NULL)
+                       return 0;
+               if (off + dircount * sizeof (TIFFDirEntry) > tif->tif_size) {
+                       TIFFErrorExt(tif->tif_clientdata, module,
+                                    "%s: Can not read TIFF directory",
+                                    tif->tif_name);
+                       _TIFFfree(dir);
+                       return 0;
+               } else {
+                       _TIFFmemcpy(dir, tif->tif_base + off,
+                                   dircount * sizeof(TIFFDirEntry));
+               }
+               if (nextdiroff) {
+                       off += dircount * sizeof (TIFFDirEntry);
+                       if (off + sizeof (uint32) <= tif->tif_size) {
+                               _TIFFmemcpy(nextdiroff, tif->tif_base + off,
+                                           sizeof (uint32));
+                       }
+               }
+       }
+       if (nextdiroff && tif->tif_flags & TIFF_SWAB)
+               TIFFSwabLong(nextdiroff);
+       *pdir = dir;
+       return dircount;
+}
+
+/*
  * Fetch a contiguous directory item.
  */
 static tsize_t
 TIFFFetchData(TIFF* tif, TIFFDirEntry* dir, char* cp)
 {
-       int w = TIFFDataWidth((TIFFDataType) dir->tdir_type);
-       tsize_t cc = dir->tdir_count * w;
+       uint32 w = TIFFDataWidth((TIFFDataType) dir->tdir_type);
+       /* 
+        * FIXME: butecount should have tsize_t type, but for now libtiff
+        * defines tsize_t as a signed 32-bit integer and we are losing
+        * ability to read arrays larger than 2^31 bytes. So we are using
+        * uint32 instead of tsize_t here.
+        */
+       uint32 cc = dir->tdir_count * w;
+
+       /* Check for overflow. */
+       if (!dir->tdir_count || !w || cc / w != dir->tdir_count)
+               goto bad;
 
        if (!isMapped(tif)) {
                if (!SeekOK(tif, dir->tdir_offset))
@@ -801,7 +1222,10 @@ TIFFFetchData(TIFF* tif, TIFFDirEntry* dir, char* cp)
                if (!ReadOK(tif, cp, cc))
                        goto bad;
        } else {
-               if (dir->tdir_offset + cc > tif->tif_size)
+               /* Check for overflow. */
+               if (dir->tdir_offset + cc < dir->tdir_offset
+                   || dir->tdir_offset + cc < cc
+                   || dir->tdir_offset + cc > tif->tif_size)
                        goto bad;
                _TIFFmemcpy(cp, tif->tif_base + dir->tdir_offset, cc);
        }
@@ -827,9 +1251,10 @@ TIFFFetchData(TIFF* tif, TIFFDirEntry* dir, char* cp)
        }
        return (cc);
 bad:
-       TIFFError(tif->tif_name, "Error fetching data for field \"%s\"",
-           _TIFFFieldWithTag(tif, dir->tdir_tag)->field_name);
-       return ((tsize_t) 0);
+       TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+                    "Error fetching data for field \"%s\"",
+                    _TIFFFieldWithTag(tif, dir->tdir_tag)->field_name);
+       return (tsize_t) 0;
 }
 
 /*
@@ -855,8 +1280,8 @@ static int
 cvtRational(TIFF* tif, TIFFDirEntry* dir, uint32 num, uint32 denom, float* rv)
 {
        if (denom == 0) {
-               TIFFError(tif->tif_name,
-                   "%s: Rational with zero denominator (num = %lu)",
+               TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+                   "%s: Rational with zero denominator (num = %u)",
                    _TIFFFieldWithTag(tif, dir->tdir_tag)->field_name, num);
                return (0);
        } else {
@@ -869,9 +1294,8 @@ cvtRational(TIFF* tif, TIFFDirEntry* dir, uint32 num, uint32 denom, float* rv)
 }
 
 /*
- * Fetch a rational item from the file
- * at offset off and return the value
- * as a floating point number.
+ * Fetch a rational item from the file at offset off and return the value as a
+ * floating point number.
  */
 static float
 TIFFFetchRational(TIFF* tif, TIFFDirEntry* dir)
@@ -884,9 +1308,8 @@ TIFFFetchRational(TIFF* tif, TIFFDirEntry* dir)
 }
 
 /*
- * Fetch a single floating point value
- * from the offset field and return it
- * as a native float.
+ * Fetch a single floating point value from the offset field and return it as
+ * a native float.
  */
 static float
 TIFFFetchFloat(TIFF* tif, TIFFDirEntry* dir)
@@ -902,7 +1325,7 @@ TIFFFetchFloat(TIFF* tif, TIFFDirEntry* dir)
  * Fetch an array of BYTE or SBYTE values.
  */
 static int
-TIFFFetchByteArray(TIFF* tif, TIFFDirEntry* dir, uint16* v)
+TIFFFetchByteArray(TIFF* tif, TIFFDirEntry* dir, uint8* v)
 {
     if (dir->tdir_count <= 4) {
         /*
@@ -911,32 +1334,32 @@ TIFFFetchByteArray(TIFF* tif, TIFFDirEntry* dir, uint16* v)
         if (tif->tif_header.tiff_magic == TIFF_BIGENDIAN) {
            if (dir->tdir_type == TIFF_SBYTE)
                 switch (dir->tdir_count) {
-                    case 4: v[3] = (signed char)(dir->tdir_offset & 0xff);
-                    case 3: v[2] = (signed char)((dir->tdir_offset >> 8) & 0xff);
-                    case 2: v[1] = (signed char)((dir->tdir_offset >> 16) & 0xff);
-                   case 1: v[0] = (signed char)(dir->tdir_offset >> 24);       
+                    case 4: v[3] = dir->tdir_offset & 0xff;
+                    case 3: v[2] = (dir->tdir_offset >> 8) & 0xff;
+                    case 2: v[1] = (dir->tdir_offset >> 16) & 0xff;
+                   case 1: v[0] = dir->tdir_offset >> 24;
                 }
            else
                 switch (dir->tdir_count) {
-                    case 4: v[3] = (uint16)(dir->tdir_offset & 0xff);
-                    case 3: v[2] = (uint16)((dir->tdir_offset >> 8) & 0xff);
-                    case 2: v[1] = (uint16)((dir->tdir_offset >> 16) & 0xff);
-                   case 1: v[0] = (uint16)(dir->tdir_offset >> 24);    
+                    case 4: v[3] = dir->tdir_offset & 0xff;
+                    case 3: v[2] = (dir->tdir_offset >> 8) & 0xff;
+                    case 2: v[1] = (dir->tdir_offset >> 16) & 0xff;
+                   case 1: v[0] = dir->tdir_offset >> 24;
                 }
        } else {
            if (dir->tdir_type == TIFF_SBYTE)
                 switch (dir->tdir_count) {
-                    case 4: v[3] = (signed char)(dir->tdir_offset >> 24);
-                    case 3: v[2] = (signed char)((dir->tdir_offset >> 16) & 0xff);
-                    case 2: v[1] = (signed char)((dir->tdir_offset >> 8) & 0xff);
-                    case 1: v[0] = (signed char)(dir->tdir_offset & 0xff);
+                    case 4: v[3] = dir->tdir_offset >> 24;
+                    case 3: v[2] = (dir->tdir_offset >> 16) & 0xff;
+                    case 2: v[1] = (dir->tdir_offset >> 8) & 0xff;
+                    case 1: v[0] = dir->tdir_offset & 0xff;
                }
            else
                 switch (dir->tdir_count) {
-                    case 4: v[3] = (uint16)(dir->tdir_offset >> 24);
-                    case 3: v[2] = (uint16)((dir->tdir_offset >> 16) & 0xff);
-                    case 2: v[1] = (uint16)((dir->tdir_offset >> 8) & 0xff);
-                    case 1: v[0] = (uint16)(dir->tdir_offset & 0xff);
+                    case 4: v[3] = dir->tdir_offset >> 24;
+                    case 3: v[2] = (dir->tdir_offset >> 16) & 0xff;
+                    case 2: v[1] = (dir->tdir_offset >> 8) & 0xff;
+                    case 1: v[0] = dir->tdir_offset & 0xff;
                }
        }
         return (1);
@@ -968,27 +1391,42 @@ TIFFFetchShortArray(TIFF* tif, TIFFDirEntry* dir, uint16* v)
 }
 
 /*
- * Fetch a pair of SHORT or BYTE values.
+ * Fetch a pair of SHORT or BYTE values. Some tags may have either BYTE
+ * or SHORT type and this function works with both ones.
  */
 static int
 TIFFFetchShortPair(TIFF* tif, TIFFDirEntry* dir)
 {
-       uint16 v[4];
-       int ok = 0;
+       /*
+        * Prevent overflowing the v stack arrays below by performing a sanity
+        * check on tdir_count, this should never be greater than two.
+        */
+       if (dir->tdir_count > 2) {
+               TIFFWarningExt(tif->tif_clientdata, tif->tif_name,
+               "unexpected count for field \"%s\", %u, expected 2; ignored",
+                       _TIFFFieldWithTag(tif, dir->tdir_tag)->field_name,
+                       dir->tdir_count);
+               return 0;
+       }
 
        switch (dir->tdir_type) {
-       case TIFF_SHORT:
-       case TIFF_SSHORT:
-               ok = TIFFFetchShortArray(tif, dir, v);
-               break;
-       case TIFF_BYTE:
-       case TIFF_SBYTE:
-               ok  = TIFFFetchByteArray(tif, dir, v);
-               break;
+               case TIFF_BYTE:
+               case TIFF_SBYTE:
+                       {
+                       uint8 v[4];
+                       return TIFFFetchByteArray(tif, dir, v)
+                               && TIFFSetField(tif, dir->tdir_tag, v[0], v[1]);
+                       }
+               case TIFF_SHORT:
+               case TIFF_SSHORT:
+                       {
+                       uint16 v[2];
+                       return TIFFFetchShortArray(tif, dir, v)
+                               && TIFFSetField(tif, dir->tdir_tag, v[0], v[1]);
+                       }
+               default:
+                       return 0;
        }
-       if (ok)
-               TIFFSetField(tif, dir->tdir_tag, v[0], v[1]);
-       return (ok);
 }
 
 /*
@@ -1013,7 +1451,7 @@ TIFFFetchRationalArray(TIFF* tif, TIFFDirEntry* dir, float* v)
        int ok = 0;
        uint32* l;
 
-       l = (uint32*)CheckMalloc(tif,
+       l = (uint32*)_TIFFCheckMalloc(tif,
            dir->tdir_count, TIFFDataWidth((TIFFDataType) dir->tdir_type),
            "to fetch array of rationals");
        if (l) {
@@ -1039,7 +1477,14 @@ TIFFFetchFloatArray(TIFF* tif, TIFFDirEntry* dir, float* v)
 {
 
        if (dir->tdir_count == 1) {
-               v[0] = *(float*) &dir->tdir_offset;
+               union
+               {
+                 float  f;
+                 uint32 i;
+               } float_union;
+
+               float_union.i=dir->tdir_offset;
+               v[0]=float_union.f;
                TIFFCvtIEEEFloatToNative(tif, dir->tdir_count, v);
                return (1);
        } else  if (TIFFFetchData(tif, dir, (char*) v)) {
@@ -1063,15 +1508,13 @@ TIFFFetchDoubleArray(TIFF* tif, TIFFDirEntry* dir, double* v)
 }
 
 /*
- * Fetch an array of ANY values.  The actual values are
- * returned as doubles which should be able hold all the
- * types.  Yes, there really should be an tany_t to avoid
- * this potential non-portability ...  Note in particular
- * that we assume that the double return value vector is
- * large enough to read in any fundamental type.  We use
- * that vector as a buffer to read in the base type vector
- * and then convert it in place to double (from end
- * to front of course).
+ * Fetch an array of ANY values.  The actual values are returned as doubles
+ * which should be able hold all the types.  Yes, there really should be an
+ * tany_t to avoid this potential non-portability ...  Note in particular that
+ * we assume that the double return value vector is large enough to read in
+ * any fundamental type.  We use that vector as a buffer to read in the base
+ * type vector and then convert it in place to double (from end to front of
+ * course).
  */
 static int
 TIFFFetchAnyArray(TIFF* tif, TIFFDirEntry* dir, double* v)
@@ -1081,14 +1524,14 @@ TIFFFetchAnyArray(TIFF* tif, TIFFDirEntry* dir, double* v)
        switch (dir->tdir_type) {
        case TIFF_BYTE:
        case TIFF_SBYTE:
-               if (!TIFFFetchByteArray(tif, dir, (uint16*) v))
+               if (!TIFFFetchByteArray(tif, dir, (uint8*) v))
                        return (0);
                if (dir->tdir_type == TIFF_BYTE) {
-                       uint16* vp = (uint16*) v;
+                       uint8* vp = (uint8*) v;
                        for (i = dir->tdir_count-1; i >= 0; i--)
                                v[i] = vp[i];
                } else {
-                       int16* vp = (int16*) v;
+                       int8* vp = (int8*) v;
                        for (i = dir->tdir_count-1; i >= 0; i--)
                                v[i] = vp[i];
                }
@@ -1144,9 +1587,10 @@ TIFFFetchAnyArray(TIFF* tif, TIFFDirEntry* dir, double* v)
                /* TIFF_NOTYPE */
                /* TIFF_ASCII */
                /* TIFF_UNDEFINED */
-               TIFFError(tif->tif_name,
-                   "cannot read TIFF_ANY type %d for field \"%s\"",
-                   _TIFFFieldWithTag(tif, dir->tdir_tag)->field_name);
+               TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+                            "cannot read TIFF_ANY type %d for field \"%s\"",
+                            dir->tdir_type,
+                            _TIFFFieldWithTag(tif, dir->tdir_tag)->field_name);
                return (0);
        }
        return (1);
@@ -1168,36 +1612,35 @@ TIFFFetchNormalTag(TIFF* tif, TIFFDirEntry* dp)
                switch (dp->tdir_type) {
                case TIFF_BYTE:
                case TIFF_SBYTE:
-                       /* NB: always expand BYTE values to shorts */
-                       cp = CheckMalloc(tif,
-                           dp->tdir_count, sizeof (uint16), mesg);
-                       ok = cp && TIFFFetchByteArray(tif, dp, (uint16*) cp);
+                       cp = (char *)_TIFFCheckMalloc(tif,
+                           dp->tdir_count, sizeof (uint8), mesg);
+                       ok = cp && TIFFFetchByteArray(tif, dp, (uint8*) cp);
                        break;
                case TIFF_SHORT:
                case TIFF_SSHORT:
-                       cp = CheckMalloc(tif,
+                       cp = (char *)_TIFFCheckMalloc(tif,
                            dp->tdir_count, sizeof (uint16), mesg);
                        ok = cp && TIFFFetchShortArray(tif, dp, (uint16*) cp);
                        break;
                case TIFF_LONG:
                case TIFF_SLONG:
-                       cp = CheckMalloc(tif,
+                       cp = (char *)_TIFFCheckMalloc(tif,
                            dp->tdir_count, sizeof (uint32), mesg);
                        ok = cp && TIFFFetchLongArray(tif, dp, (uint32*) cp);
                        break;
                case TIFF_RATIONAL:
                case TIFF_SRATIONAL:
-                       cp = CheckMalloc(tif,
+                       cp = (char *)_TIFFCheckMalloc(tif,
                            dp->tdir_count, sizeof (float), mesg);
                        ok = cp && TIFFFetchRationalArray(tif, dp, (float*) cp);
                        break;
                case TIFF_FLOAT:
-                       cp = CheckMalloc(tif,
+                       cp = (char *)_TIFFCheckMalloc(tif,
                            dp->tdir_count, sizeof (float), mesg);
                        ok = cp && TIFFFetchFloatArray(tif, dp, (float*) cp);
                        break;
                case TIFF_DOUBLE:
-                       cp = CheckMalloc(tif,
+                       cp = (char *)_TIFFCheckMalloc(tif,
                            dp->tdir_count, sizeof (double), mesg);
                        ok = cp && TIFFFetchDoubleArray(tif, dp, (double*) cp);
                        break;
@@ -1207,7 +1650,8 @@ TIFFFetchNormalTag(TIFF* tif, TIFFDirEntry* dp)
                         * Some vendors write strings w/o the trailing
                         * NULL byte, so always append one just in case.
                         */
-                       cp = CheckMalloc(tif, dp->tdir_count+1, 1, mesg);
+                       cp = (char *)_TIFFCheckMalloc(tif, dp->tdir_count + 1,
+                                                     1, mesg);
                        if( (ok = (cp && TIFFFetchString(tif, dp, cp))) != 0 )
                                cp[dp->tdir_count] = '\0';      /* XXX */
                        break;
@@ -1303,33 +1747,37 @@ TIFFFetchNormalTag(TIFF* tif, TIFFDirEntry* dp)
 static int
 TIFFFetchPerSampleShorts(TIFF* tif, TIFFDirEntry* dir, uint16* pl)
 {
-       uint16 samples = tif->tif_dir.td_samplesperpixel;
-       int status = 0;
-
-       if (CheckDirCount(tif, dir, (uint32) samples)) {
-               uint16 buf[10];
-               uint16* v = buf;
-
-               if (samples > NITEMS(buf))
-                       v = (uint16*) CheckMalloc(tif, samples, sizeof(uint16),
-                                                 "to fetch per-sample values");
-               if (v && TIFFFetchShortArray(tif, dir, v)) {
-                       uint16 i;
-                       for (i = 1; i < samples; i++)
-                               if (v[i] != v[0]) {
-                                       TIFFError(tif->tif_name,
-               "Cannot handle different per-sample values for field \"%s\"",
-                          _TIFFFieldWithTag(tif, dir->tdir_tag)->field_name);
-                                       goto bad;
-                               }
-                       *pl = v[0];
-                       status = 1;
-               }
-       bad:
-               if (v && v != buf)
-                       _TIFFfree(v);
-       }
-       return (status);
+    uint16 samples = tif->tif_dir.td_samplesperpixel;
+    int status = 0;
+
+    if (CheckDirCount(tif, dir, (uint32) samples)) {
+        uint16 buf[10];
+        uint16* v = buf;
+
+        if (dir->tdir_count > NITEMS(buf))
+            v = (uint16*) _TIFFCheckMalloc(tif, dir->tdir_count, sizeof(uint16),
+                                      "to fetch per-sample values");
+        if (v && TIFFFetchShortArray(tif, dir, v)) {
+            uint16 i;
+            int check_count = dir->tdir_count;
+            if( samples < check_count )
+                check_count = samples;
+
+            for (i = 1; i < check_count; i++)
+                if (v[i] != v[0]) {
+                       TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+                "Cannot handle different per-sample values for field \"%s\"",
+                       _TIFFFieldWithTag(tif, dir->tdir_tag)->field_name);
+                       goto bad;
+                }
+            *pl = v[0];
+            status = 1;
+        }
+      bad:
+        if (v && v != buf)
+            _TIFFfree(v);
+    }
+    return (status);
 }
 
 /*
@@ -1340,70 +1788,77 @@ TIFFFetchPerSampleShorts(TIFF* tif, TIFFDirEntry* dir, uint16* pl)
 static int
 TIFFFetchPerSampleLongs(TIFF* tif, TIFFDirEntry* dir, uint32* pl)
 {
-       uint16 samples = tif->tif_dir.td_samplesperpixel;
-       int status = 0;
-
-       if (CheckDirCount(tif, dir, (uint32) samples)) {
-               uint32 buf[10];
-               uint32* v = buf;
-
-               if (samples > NITEMS(buf))
-                       v = (uint32*) CheckMalloc(tif, samples, sizeof(uint32),
-                                                 "to fetch per-sample values");
-               if (v && TIFFFetchLongArray(tif, dir, v)) {
-                       uint16 i;
-                       for (i = 1; i < samples; i++)
-                               if (v[i] != v[0]) {
-                                       TIFFError(tif->tif_name,
-               "Cannot handle different per-sample values for field \"%s\"",
-                          _TIFFFieldWithTag(tif, dir->tdir_tag)->field_name);
-                                       goto bad;
-                               }
-                       *pl = v[0];
-                       status = 1;
-               }
-       bad:
-               if (v && v != buf)
-                       _TIFFfree(v);
-       }
-       return (status);
+    uint16 samples = tif->tif_dir.td_samplesperpixel;
+    int status = 0;
+
+    if (CheckDirCount(tif, dir, (uint32) samples)) {
+        uint32 buf[10];
+        uint32* v = buf;
+
+        if (dir->tdir_count > NITEMS(buf))
+            v = (uint32*) _TIFFCheckMalloc(tif, dir->tdir_count, sizeof(uint32),
+                                      "to fetch per-sample values");
+        if (v && TIFFFetchLongArray(tif, dir, v)) {
+            uint16 i;
+            int check_count = dir->tdir_count;
+
+            if( samples < check_count )
+                check_count = samples;
+            for (i = 1; i < check_count; i++)
+                if (v[i] != v[0]) {
+                       TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+                "Cannot handle different per-sample values for field \"%s\"",
+                       _TIFFFieldWithTag(tif, dir->tdir_tag)->field_name);
+                       goto bad;
+                }
+            *pl = v[0];
+            status = 1;
+        }
+      bad:
+        if (v && v != buf)
+            _TIFFfree(v);
+    }
+    return (status);
 }
 
 /*
- * Fetch samples/pixel ANY values for 
- * the specified tag and verify that
- * all values are the same.
+ * Fetch samples/pixel ANY values for the specified tag and verify that all
+ * values are the same.
  */
 static int
 TIFFFetchPerSampleAnys(TIFF* tif, TIFFDirEntry* dir, double* pl)
 {
-       uint16 samples = tif->tif_dir.td_samplesperpixel;
-       int status = 0;
-
-       if (CheckDirCount(tif, dir, (uint32) samples)) {
-               double buf[10];
-               double* v = buf;
-
-               if (samples > NITEMS(buf))
-                       v = (double*) CheckMalloc(tif, samples, sizeof (double),
-                                                 "to fetch per-sample values");
-               if (v && TIFFFetchAnyArray(tif, dir, v)) {
-                       uint16 i;
-                       for (i = 1; i < samples; i++)
-                               if (v[i] != v[0]) {
-                                       TIFFError(tif->tif_name,
+    uint16 samples = tif->tif_dir.td_samplesperpixel;
+    int status = 0;
+
+    if (CheckDirCount(tif, dir, (uint32) samples)) {
+        double buf[10];
+        double* v = buf;
+
+        if (dir->tdir_count > NITEMS(buf))
+            v = (double*) _TIFFCheckMalloc(tif, dir->tdir_count, sizeof (double),
+                                      "to fetch per-sample values");
+        if (v && TIFFFetchAnyArray(tif, dir, v)) {
+            uint16 i;
+            int check_count = dir->tdir_count;
+            if( samples < check_count )
+                check_count = samples;
+
+            for (i = 1; i < check_count; i++)
+                if (v[i] != v[0]) {
+                       TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
                "Cannot handle different per-sample values for field \"%s\"",
-                          _TIFFFieldWithTag(tif, dir->tdir_tag)->field_name);
-                                       goto bad;
-                               }
-                       *pl = v[0];
-                       status = 1;
-               }
-       bad:
-               if (v && v != buf)
-                       _TIFFfree(v);
-       }
-       return (status);
+                       _TIFFFieldWithTag(tif, dir->tdir_tag)->field_name);
+                       goto bad;
+                }
+            *pl = v[0];
+            status = 1;
+        }
+      bad:
+        if (v && v != buf)
+            _TIFFfree(v);
+    }
+    return (status);
 }
 #undef NITEMS
 
@@ -1423,7 +1878,7 @@ TIFFFetchStripThing(TIFF* tif, TIFFDirEntry* dir, long nstrips, uint32** lpp)
         * Allocate space for strip information.
         */
        if (*lpp == NULL &&
-           (*lpp = (uint32 *)CheckMalloc(tif,
+           (*lpp = (uint32 *)_TIFFCheckMalloc(tif,
              nstrips, sizeof (uint32), "for strip array")) == NULL)
                return (0);
        lp = *lpp;
@@ -1433,7 +1888,7 @@ TIFFFetchStripThing(TIFF* tif, TIFFDirEntry* dir, long nstrips, uint32** lpp)
                /*
                 * Handle uint16->uint32 expansion.
                 */
-               uint16* dp = (uint16*) CheckMalloc(tif,
+               uint16* dp = (uint16*) _TIFFCheckMalloc(tif,
                    dir->tdir_count, sizeof (uint16), "to fetch strip tag");
                if (dp == NULL)
                        return (0);
@@ -1450,7 +1905,7 @@ TIFFFetchStripThing(TIFF* tif, TIFFDirEntry* dir, long nstrips, uint32** lpp)
         } else if( nstrips != (int) dir->tdir_count ) {
             /* Special case to correct length */
 
-            uint32* dp = (uint32*) CheckMalloc(tif,
+            uint32* dp = (uint32*) _TIFFCheckMalloc(tif,
                    dir->tdir_count, sizeof (uint32), "to fetch strip tag");
             if (dp == NULL)
                 return (0);
@@ -1472,35 +1927,6 @@ TIFFFetchStripThing(TIFF* tif, TIFFDirEntry* dir, long nstrips, uint32** lpp)
        return (status);
 }
 
-#define        NITEMS(x)       (sizeof (x) / sizeof (x[0]))
-/*
- * Fetch and set the ExtraSamples tag.
- */
-static int
-TIFFFetchExtraSamples(TIFF* tif, TIFFDirEntry* dir)
-{
-       uint16 buf[10];
-       uint16* v = buf;
-       int status;
-
-       if (dir->tdir_count > NITEMS(buf)) {
-               v = (uint16*) CheckMalloc(tif, dir->tdir_count, sizeof (uint16),
-                                         "to fetch extra samples");
-               if (!v)
-                       return (0);
-       }
-       if (dir->tdir_type == TIFF_BYTE)
-               status = TIFFFetchByteArray(tif, dir, v);
-       else
-               status = TIFFFetchShortArray(tif, dir, v);
-       if (status)
-               status = TIFFSetField(tif, dir->tdir_tag, dir->tdir_count, v);
-       if (v != buf)
-               _TIFFfree((char*) v);
-       return (status);
-}
-#undef NITEMS
-
 /*
  * Fetch and set the RefBlackWhite tag.
  */
@@ -1516,10 +1942,11 @@ TIFFFetchRefBlackWhite(TIFF* tif, TIFFDirEntry* dir)
        /*
         * Handle LONG's for backward compatibility.
         */
-       cp = CheckMalloc(tif, dir->tdir_count, sizeof (uint32), mesg);
+       cp = (char *)_TIFFCheckMalloc(tif, dir->tdir_count,
+                                     sizeof (uint32), mesg);
        if( (ok = (cp && TIFFFetchLongArray(tif, dir, (uint32*) cp))) != 0) {
                float* fp = (float*)
-                   CheckMalloc(tif, dir->tdir_count, sizeof (float), mesg);
+                   _TIFFCheckMalloc(tif, dir->tdir_count, sizeof (float), mesg);
                if( (ok = (fp != NULL)) != 0 ) {
                        uint32 i;
                        for (i = 0; i < dir->tdir_count; i++)
@@ -1534,11 +1961,41 @@ TIFFFetchRefBlackWhite(TIFF* tif, TIFFDirEntry* dir)
 }
 
 /*
- * Replace a single strip (tile) of uncompressed data by
- * multiple strips (tiles), each approximately 8Kbytes.
- * This is useful for dealing with large images or
- * for dealing with machines with a limited amount
- * memory.
+ * Fetch and set the SubjectDistance EXIF tag.
+ */
+static int
+TIFFFetchSubjectDistance(TIFF* tif, TIFFDirEntry* dir)
+{
+       uint32 l[2];
+       float v;
+       int ok = 0;
+
+    if( dir->tdir_count != 1 || dir->tdir_type != TIFF_RATIONAL )
+    {
+               TIFFWarningExt(tif->tif_clientdata, tif->tif_name,
+                       "incorrect count or type for SubjectDistance, tag ignored" );
+               return (0);
+    }
+
+       if (TIFFFetchData(tif, dir, (char *)l)
+           && cvtRational(tif, dir, l[0], l[1], &v)) {
+               /*
+                * XXX: Numerator 0xFFFFFFFF means that we have infinite
+                * distance. Indicate that with a negative floating point
+                * SubjectDistance value.
+                */
+               ok = TIFFSetField(tif, dir->tdir_tag,
+                                 (l[0] != 0xFFFFFFFF) ? v : -v);
+       }
+
+       return ok;
+}
+
+/*
+ * Replace a single strip (tile) of uncompressed data by multiple strips
+ * (tiles), each approximately STRIP_SIZE_DEFAULT bytes. This is useful for
+ * dealing with large images or for dealing with machines with a limited
+ * amount memory.
  */
 static void
 ChopUpSingleUncompressedStrip(TIFF* tif)
@@ -1552,31 +2009,36 @@ ChopUpSingleUncompressedStrip(TIFF* tif)
        uint32* newoffsets;
 
        /*
-        * Make the rows hold at least one
-        * scanline, but fill 8k if possible.
+        * Make the rows hold at least one scanline, but fill specified amount
+        * of data if possible.
         */
-       if (rowbytes > 8192) {
+       if (rowbytes > STRIP_SIZE_DEFAULT) {
                stripbytes = rowbytes;
                rowsperstrip = 1;
        } else if (rowbytes > 0 ) {
-               rowsperstrip = 8192 / rowbytes;
+               rowsperstrip = STRIP_SIZE_DEFAULT / rowbytes;
                stripbytes = rowbytes * rowsperstrip;
        }
         else
             return;
 
-       /* never increase the number of strips in an image */
+       /* 
+        * never increase the number of strips in an image
+        */
        if (rowsperstrip >= td->td_rowsperstrip)
                return;
        nstrips = (tstrip_t) TIFFhowmany(bytecount, stripbytes);
-       newcounts = (uint32*) CheckMalloc(tif, nstrips, sizeof (uint32),
+        if( nstrips == 0 ) /* something is wonky, do nothing. */
+            return;
+
+       newcounts = (uint32*) _TIFFCheckMalloc(tif, nstrips, sizeof (uint32),
                                "for chopped \"StripByteCounts\" array");
-       newoffsets = (uint32*) CheckMalloc(tif, nstrips, sizeof (uint32),
+       newoffsets = (uint32*) _TIFFCheckMalloc(tif, nstrips, sizeof (uint32),
                                "for chopped \"StripOffsets\" array");
        if (newcounts == NULL || newoffsets == NULL) {
                /*
-                * Unable to allocate new strip information, give
-                * up and use the original one strip information.
+                * Unable to allocate new strip information, give up and use
+                * the original one strip information.
                 */
                if (newcounts != NULL)
                        _TIFFfree(newcounts);
@@ -1585,12 +2047,11 @@ ChopUpSingleUncompressedStrip(TIFF* tif)
                return;
        }
        /*
-        * Fill the strip information arrays with
-        * new bytecounts and offsets that reflect
-        * the broken-up format.
+        * Fill the strip information arrays with new bytecounts and offsets
+        * that reflect the broken-up format.
         */
        for (strip = 0; strip < nstrips; strip++) {
-               if (stripbytes > (tsize_t) bytecount)
+               if ((uint32)stripbytes > bytecount)
                        stripbytes = bytecount;
                newcounts[strip] = stripbytes;
                newoffsets[strip] = offset;
@@ -1611,3 +2072,10 @@ ChopUpSingleUncompressedStrip(TIFF* tif)
 }
 
 /* vim: set ts=8 sts=8 sw=8 noet: */
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
index 3870bb8..8d308c4 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: tif_dirwrite.c,v 1.1 2005-06-17 13:54:52 vp153 Exp $ */
+/* $Id: tif_dirwrite.c,v 1.37.2.7 2010-06-08 18:50:42 bfriesen Exp $ */
 
 /*
  * Copyright (c) 1988-1997 Sam Leffler
@@ -31,9 +31,9 @@
  */
 #include "tiffiop.h"
 
-#if HAVE_IEEEFP
-#define        TIFFCvtNativeToIEEEFloat(tif, n, fp)
-#define        TIFFCvtNativeToIEEEDouble(tif, n, dp)
+#ifdef HAVE_IEEEFP
+# define       TIFFCvtNativeToIEEEFloat(tif, n, fp)
+# define       TIFFCvtNativeToIEEEDouble(tif, n, dp)
 #else
 extern void TIFFCvtNativeToIEEEFloat(TIFF*, uint32, float*);
 extern void TIFFCvtNativeToIEEEDouble(TIFF*, uint32, double*);
@@ -100,31 +100,34 @@ _TIFFWriteDirectory(TIFF* tif, int done)
         */
        if (done)
        {
-           if (tif->tif_flags & TIFF_POSTENCODE) {
-                   tif->tif_flags &= ~TIFF_POSTENCODE;
-                   if (!(*tif->tif_postencode)(tif)) {
-                           TIFFError(tif->tif_name,
+               if (tif->tif_flags & TIFF_POSTENCODE) {
+                       tif->tif_flags &= ~TIFF_POSTENCODE;
+                       if (!(*tif->tif_postencode)(tif)) {
+                               TIFFErrorExt(tif->tif_clientdata,
+                                            tif->tif_name,
                                "Error post-encoding before directory write");
-                           return (0);
-                   }
-           }
-           (*tif->tif_close)(tif);             /* shutdown encoder */
-           /*
-            * Flush any data that might have been written
-            * by the compression close+cleanup routines.
-            */
-           if (tif->tif_rawcc > 0 && !TIFFFlushData1(tif)) {
-                   TIFFError(tif->tif_name,
-                       "Error flushing data before directory write");
-                   return (0);
-           }
-           if ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata) {
-                   _TIFFfree(tif->tif_rawdata);
-                   tif->tif_rawdata = NULL;
-                   tif->tif_rawcc = 0;
-                   tif->tif_rawdatasize = 0;
-           }
-           tif->tif_flags &= ~(TIFF_BEENWRITING|TIFF_BUFFERSETUP);
+                               return (0);
+                       }
+               }
+               (*tif->tif_close)(tif);         /* shutdown encoder */
+               /*
+                * Flush any data that might have been written
+                * by the compression close+cleanup routines.
+                */
+               if (tif->tif_rawcc > 0
+                    && (tif->tif_flags & TIFF_BEENWRITING) != 0
+                    && !TIFFFlushData1(tif)) {
+                       TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+                           "Error flushing data before directory write");
+                       return (0);
+               }
+               if ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata) {
+                       _TIFFfree(tif->tif_rawdata);
+                       tif->tif_rawdata = NULL;
+                       tif->tif_rawcc = 0;
+                       tif->tif_rawdatasize = 0;
+               }
+               tif->tif_flags &= ~(TIFF_BEENWRITING|TIFF_BUFFERSETUP);
        }
 
        td = &tif->tif_dir;
@@ -137,12 +140,12 @@ _TIFFWriteDirectory(TIFF* tif, int done)
        for (b = 0; b <= FIELD_LAST; b++)
                if (TIFFFieldSet(tif, b) && b != FIELD_CUSTOM)
                        nfields += (b < FIELD_SUBFILETYPE ? 2 : 1);
-        nfields += td->td_customValueCount;
+       nfields += td->td_customValueCount;
        dirsize = nfields * sizeof (TIFFDirEntry);
        data = (char*) _TIFFmalloc(dirsize);
        if (data == NULL) {
-               TIFFError(tif->tif_name,
-                   "Cannot write directory, out of space");
+               TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+                            "Cannot write directory, out of space");
                return (0);
        }
        /*
@@ -176,30 +179,29 @@ _TIFFWriteDirectory(TIFF* tif, int done)
        for (fi = 0, nfi = tif->tif_nfields; nfi > 0; nfi--, fi++) {
                const TIFFFieldInfo* fip = tif->tif_fieldinfo[fi];
 
-                /*
-                ** For custom fields, we test to see if the custom field
-                ** is set or not.  For normal fields, we just use the
-                ** FieldSet test. 
-                */
-                if( fip->field_bit == FIELD_CUSTOM )
-                {
-                    int ci, is_set = FALSE;
+               /*
+                * For custom fields, we test to see if the custom field
+                * is set or not.  For normal fields, we just use the
+                * FieldSet test.
+               */
+               if( fip->field_bit == FIELD_CUSTOM )
+               {
+                       int ci, is_set = FALSE;
 
-                    for( ci = 0; ci < td->td_customValueCount; ci++ )
-                        is_set |= (td->td_customValues[ci].info == fip);
+                       for( ci = 0; ci < td->td_customValueCount; ci++ )
+                               is_set |= (td->td_customValues[ci].info == fip);
 
-                    if( !is_set )
-                        continue;
-                }
+                       if( !is_set )
+                               continue;
+               }
                else if (!FieldSet(fields, fip->field_bit))
-                    continue;
-
+                       continue;
 
-                /*
-                ** Handle other fields.
-                */
+               /*
+                * Handle other fields.
+                */
                switch (fip->field_bit)
-                {
+               {
                case FIELD_STRIPOFFSETS:
                        /*
                         * We use one field bit for both strip and tile
@@ -234,8 +236,7 @@ _TIFFWriteDirectory(TIFF* tif, int done)
                        dir->tdir_tag = (uint16) tag;
                        dir->tdir_type = (uint16) TIFF_LONG;
                        dir->tdir_count = (uint32) td->td_nstrips;
-                       if (!TIFFWriteLongArray(tif, dir,
-                                               td->td_stripbytecount))
+                       if (!TIFFWriteLongArray(tif, dir, td->td_stripbytecount))
                                goto bad;
                        break;
                case FIELD_ROWSPERSTRIP:
@@ -293,7 +294,6 @@ _TIFFWriteDirectory(TIFF* tif, int done)
                case FIELD_PAGENUMBER:
                case FIELD_HALFTONEHINTS:
                case FIELD_YCBCRSUBSAMPLING:
-               case FIELD_DOTRANGE:
                        if (!TIFFSetupShortPair(tif, fip->field_tag, dir))
                                goto bad;
                        break;
@@ -336,14 +336,19 @@ _TIFFWriteDirectory(TIFF* tif, int done)
                        }
                        break;
                default:
-                       if (!TIFFWriteNormalTag(tif, dir, fip))
+                       /* XXX: Should be fixed and removed. */
+                       if (fip->field_tag == TIFFTAG_DOTRANGE) {
+                               if (!TIFFSetupShortPair(tif, fip->field_tag, dir))
+                                       goto bad;
+                       }
+                       else if (!TIFFWriteNormalTag(tif, dir, fip))
                                goto bad;
                        break;
                }
                dir++;
                 
-                if( fip->field_bit != FIELD_CUSTOM )
-                    ResetFieldBit(fields, fip->field_bit);
+               if( fip->field_bit != FIELD_CUSTOM )
+                       ResetFieldBit(fields, fip->field_bit);
        }
 
        /*
@@ -372,15 +377,18 @@ _TIFFWriteDirectory(TIFF* tif, int done)
        }
        (void) TIFFSeekFile(tif, tif->tif_diroff, SEEK_SET);
        if (!WriteOK(tif, &dircount, sizeof (dircount))) {
-               TIFFError(tif->tif_name, "Error writing directory count");
+               TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+                            "Error writing directory count");
                goto bad;
        }
        if (!WriteOK(tif, data, dirsize)) {
-               TIFFError(tif->tif_name, "Error writing directory contents");
+               TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+                            "Error writing directory contents");
                goto bad;
        }
-       if (!WriteOK(tif, &diroff, sizeof (diroff))) {
-               TIFFError(tif->tif_name, "Error writing directory link");
+       if (!WriteOK(tif, &diroff, sizeof (uint32))) {
+               TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+                            "Error writing directory link");
                goto bad;
        }
        if (done) {
@@ -426,6 +434,133 @@ TIFFCheckpointDirectory(TIFF* tif)
        return rc;
 }
 
+static int
+_TIFFWriteCustomDirectory(TIFF* tif, toff_t *pdiroff)
+{
+       uint16 dircount;
+       uint32 nfields;
+       tsize_t dirsize;
+       char* data;
+       TIFFDirEntry* dir;
+       TIFFDirectory* td;
+       unsigned long b, fields[FIELD_SETLONGS];
+       int fi, nfi;
+
+       if (tif->tif_mode == O_RDONLY)
+               return (1);
+
+       td = &tif->tif_dir;
+       /*
+        * Size the directory so that we can calculate
+        * offsets for the data items that aren't kept
+        * in-place in each field.
+        */
+       nfields = 0;
+       for (b = 0; b <= FIELD_LAST; b++)
+               if (TIFFFieldSet(tif, b) && b != FIELD_CUSTOM)
+                       nfields += (b < FIELD_SUBFILETYPE ? 2 : 1);
+       nfields += td->td_customValueCount;
+       dirsize = nfields * sizeof (TIFFDirEntry);
+       data = (char*) _TIFFmalloc(dirsize);
+       if (data == NULL) {
+               TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+                            "Cannot write directory, out of space");
+               return (0);
+       }
+       /*
+        * Put the directory  at the end of the file.
+        */
+       tif->tif_diroff = (TIFFSeekFile(tif, (toff_t) 0, SEEK_END)+1) &~ 1;
+       tif->tif_dataoff = (toff_t)(
+           tif->tif_diroff + sizeof (uint16) + dirsize + sizeof (toff_t));
+       if (tif->tif_dataoff & 1)
+               tif->tif_dataoff++;
+       (void) TIFFSeekFile(tif, tif->tif_dataoff, SEEK_SET);
+       dir = (TIFFDirEntry*) data;
+       /*
+        * Setup external form of directory
+        * entries and write data items.
+        */
+       _TIFFmemcpy(fields, td->td_fieldsset, sizeof (fields));
+
+       for (fi = 0, nfi = tif->tif_nfields; nfi > 0; nfi--, fi++) {
+               const TIFFFieldInfo* fip = tif->tif_fieldinfo[fi];
+
+               /*
+                * For custom fields, we test to see if the custom field
+                * is set or not.  For normal fields, we just use the
+                * FieldSet test.
+               */
+               if( fip->field_bit == FIELD_CUSTOM )
+               {
+                       int ci, is_set = FALSE;
+
+                       for( ci = 0; ci < td->td_customValueCount; ci++ )
+                               is_set |= (td->td_customValues[ci].info == fip);
+
+                       if( !is_set )
+                               continue;
+               }
+               else if (!FieldSet(fields, fip->field_bit))
+                       continue;
+                
+               if( fip->field_bit != FIELD_CUSTOM )
+                       ResetFieldBit(fields, fip->field_bit);
+       }
+
+       /*
+        * Write directory.
+        */
+       dircount = (uint16) nfields;
+       *pdiroff = (uint32) tif->tif_nextdiroff;
+       if (tif->tif_flags & TIFF_SWAB) {
+               /*
+                * The file's byte order is opposite to the
+                * native machine architecture.  We overwrite
+                * the directory information with impunity
+                * because it'll be released below after we
+                * write it to the file.  Note that all the
+                * other tag construction routines assume that
+                * we do this byte-swapping; i.e. they only
+                * byte-swap indirect data.
+                */
+               for (dir = (TIFFDirEntry*) data; dircount; dir++, dircount--) {
+                       TIFFSwabArrayOfShort(&dir->tdir_tag, 2);
+                       TIFFSwabArrayOfLong(&dir->tdir_count, 2);
+               }
+               dircount = (uint16) nfields;
+               TIFFSwabShort(&dircount);
+               TIFFSwabLong(pdiroff);
+       }
+       (void) TIFFSeekFile(tif, tif->tif_diroff, SEEK_SET);
+       if (!WriteOK(tif, &dircount, sizeof (dircount))) {
+               TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+                            "Error writing directory count");
+               goto bad;
+       }
+       if (!WriteOK(tif, data, dirsize)) {
+               TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+                            "Error writing directory contents");
+               goto bad;
+       }
+       if (!WriteOK(tif, pdiroff, sizeof (uint32))) {
+               TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+                            "Error writing directory link");
+               goto bad;
+       }
+       _TIFFfree(data);
+       return (1);
+bad:
+       _TIFFfree(data);
+       return (0);
+}
+
+int
+TIFFWriteCustomDirectory(TIFF* tif, toff_t *pdiroff)
+{
+       return _TIFFWriteCustomDirectory(tif, pdiroff);
+}
+
 /*
  * Process tags that are not special cased.
  */
@@ -577,7 +712,12 @@ TIFFWriteNormalTag(TIFF* tif, TIFFDirEntry* dir, const TIFFFieldInfo* fip)
                { 
                     char* cp;
                     if (fip->field_passcount)
-                        TIFFGetField(tif, fip->field_tag, &wc, &cp);
+                    {
+                        if( wc == (uint16) TIFF_VARIABLE2 )
+                            TIFFGetField(tif, fip->field_tag, &wc2, &cp);
+                        else
+                            TIFFGetField(tif, fip->field_tag, &wc, &cp);
+                    }
                     else
                         TIFFGetField(tif, fip->field_tag, &cp);
 
@@ -679,12 +819,13 @@ TIFFWritePerSampleShorts(TIFF* tif, ttag_t tag, TIFFDirEntry* dir)
 {
        uint16 buf[10], v;
        uint16* w = buf;
-       int i, status, samples = tif->tif_dir.td_samplesperpixel;
+       uint16 i, samples = tif->tif_dir.td_samplesperpixel;
+       int status;
 
        if (samples > NITEMS(buf)) {
                w = (uint16*) _TIFFmalloc(samples * sizeof (uint16));
                if (w == NULL) {
-                       TIFFError(tif->tif_name,
+                       TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
                            "No space to write per-sample shorts");
                        return (0);
                }
@@ -713,13 +854,13 @@ TIFFWritePerSampleAnys(TIFF* tif,
 {
        double buf[10], v;
        double* w = buf;
-       int i, status;
-       int samples = (int) tif->tif_dir.td_samplesperpixel;
+       uint16 i, samples = tif->tif_dir.td_samplesperpixel;
+       int status;
 
        if (samples > NITEMS(buf)) {
                w = (double*) _TIFFmalloc(samples * sizeof (double));
                if (w == NULL) {
-                       TIFFError(tif->tif_name,
+                       TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
                            "No space to write per-sample values");
                        return (0);
                }
@@ -781,12 +922,27 @@ TIFFWriteShortTable(TIFF* tif,
 static int
 TIFFWriteByteArray(TIFF* tif, TIFFDirEntry* dir, char* cp)
 {
-       if (dir->tdir_count > 4) {
-               if (!TIFFWriteData(tif, dir, cp))
-                       return (0);
+       if (dir->tdir_count <= 4) {
+               if (tif->tif_header.tiff_magic == TIFF_BIGENDIAN) {
+                       dir->tdir_offset = (uint32)cp[0] << 24;
+                       if (dir->tdir_count >= 2)
+                               dir->tdir_offset |= (uint32)cp[1] << 16;
+                       if (dir->tdir_count >= 3)
+                               dir->tdir_offset |= (uint32)cp[2] << 8;
+                       if (dir->tdir_count == 4)
+                               dir->tdir_offset |= cp[3];
+               } else {
+                       dir->tdir_offset = cp[0];
+                       if (dir->tdir_count >= 2)
+                               dir->tdir_offset |= (uint32) cp[1] << 8;
+                       if (dir->tdir_count >= 3)
+                               dir->tdir_offset |= (uint32) cp[2] << 16;
+                       if (dir->tdir_count == 4)
+                               dir->tdir_offset |= (uint32) cp[3] << 24;
+               }
+               return 1;
        } else
-               _TIFFmemcpy(&dir->tdir_offset, cp, dir->tdir_count);
-       return (1);
+               return TIFFWriteData(tif, dir, cp);
 }
 
 /*
@@ -798,13 +954,13 @@ TIFFWriteShortArray(TIFF* tif, TIFFDirEntry* dir, uint16* v)
 {
        if (dir->tdir_count <= 2) {
                if (tif->tif_header.tiff_magic == TIFF_BIGENDIAN) {
-                       dir->tdir_offset = (uint32) ((long) v[0] << 16);
+                       dir->tdir_offset = (uint32) v[0] << 16;
                        if (dir->tdir_count == 2)
                                dir->tdir_offset |= v[1] & 0xffff;
                } else {
                        dir->tdir_offset = v[0] & 0xffff;
                        if (dir->tdir_count == 2)
-                               dir->tdir_offset |= (long) v[1] << 16;
+                               dir->tdir_offset |= (uint32) v[1] << 16;
                }
                return (1);
        } else
@@ -838,7 +994,7 @@ TIFFWriteRationalArray(TIFF* tif, TIFFDirEntry* dir, float* v)
 
        t = (uint32*) _TIFFmalloc(2 * dir->tdir_count * sizeof (uint32));
        if (t == NULL) {
-               TIFFError(tif->tif_name,
+               TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
                    "No space to write RATIONAL array");
                return (0);
        }
@@ -849,10 +1005,11 @@ TIFFWriteRationalArray(TIFF* tif, TIFFDirEntry* dir, float* v)
 
                if (fv < 0) {
                        if (dir->tdir_type == TIFF_RATIONAL) {
-                               TIFFWarning(tif->tif_name,
+                               TIFFWarningExt(tif->tif_clientdata,
+                                              tif->tif_name,
        "\"%s\": Information lost writing value (%g) as (unsigned) RATIONAL",
                                _TIFFFieldWithTag(tif,dir->tdir_tag)->field_name,
-                               fv);
+                                               fv);
                                fv = 0;
                        } else
                                fv = -fv, sign = -1;
@@ -884,7 +1041,7 @@ TIFFWriteFloatArray(TIFF* tif, TIFFDirEntry* dir, float* v)
 static int
 TIFFWriteDoubleArray(TIFF* tif, TIFFDirEntry* dir, double* v)
 {
-       TIFFCvtNativeToIEEEDouble(tif, n, v);
+       TIFFCvtNativeToIEEEDouble(tif, dir->tdir_count, v);
        return (TIFFWriteData(tif, dir, (char*) v));
 }
 
@@ -908,8 +1065,8 @@ TIFFWriteAnyArray(TIFF* tif,
        if (n * TIFFDataWidth(type) > sizeof buf) {
                w = (char*) _TIFFmalloc(n * TIFFDataWidth(type));
                if (w == NULL) {
-                       TIFFError(tif->tif_name,
-                           "No space to write array");
+                       TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+                                    "No space to write array");
                        return (0);
                }
        }
@@ -983,7 +1140,11 @@ TIFFWriteAnyArray(TIFF* tif,
                }
                break;
        case TIFF_DOUBLE:
-               return (TIFFWriteDoubleArray(tif, dir, v));
+                {
+                    if( !TIFFWriteDoubleArray(tif, dir, v))
+                        goto out;
+                }
+               break;
        default:
                /* TIFF_NOTYPE */
                /* TIFF_ASCII */
@@ -1068,8 +1229,9 @@ TIFFWriteData(TIFF* tif, TIFFDirEntry* dir, char* cp)
                tif->tif_dataoff += (cc + 1) & ~1;
                return (1);
        }
-       TIFFError(tif->tif_name, "Error writing data for field \"%s\"",
-           _TIFFFieldWithTag(tif, dir->tdir_tag)->field_name);
+       TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+                    "Error writing data for field \"%s\"",
+       _TIFFFieldWithTag(tif, dir->tdir_tag)->field_name);
        return (0);
 }
 
@@ -1105,7 +1267,8 @@ TIFFRewriteDirectory( TIFF *tif )
         if (!WriteOK(tif, &(tif->tif_header.tiff_diroff), 
                      sizeof (tif->tif_diroff))) 
         {
-            TIFFError(tif->tif_name, "Error updating TIFF header");
+                       TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+                                    "Error updating TIFF header");
             return (0);
         }
     }
@@ -1119,7 +1282,8 @@ TIFFRewriteDirectory( TIFF *tif )
 
                if (!SeekOK(tif, nextdir) ||
                    !ReadOK(tif, &dircount, sizeof (dircount))) {
-                       TIFFError(module, "Error fetching directory count");
+                       TIFFErrorExt(tif->tif_clientdata, module,
+                                    "Error fetching directory count");
                        return (0);
                }
                if (tif->tif_flags & TIFF_SWAB)
@@ -1127,7 +1291,8 @@ TIFFRewriteDirectory( TIFF *tif )
                (void) TIFFSeekFile(tif,
                    dircount * sizeof (TIFFDirEntry), SEEK_CUR);
                if (!ReadOK(tif, &nextdir, sizeof (nextdir))) {
-                       TIFFError(module, "Error fetching directory link");
+                       TIFFErrorExt(tif->tif_clientdata, module,
+                                    "Error fetching directory link");
                        return (0);
                }
                if (tif->tif_flags & TIFF_SWAB)
@@ -1137,7 +1302,8 @@ TIFFRewriteDirectory( TIFF *tif )
         (void) TIFFSeekFile(tif, off - (toff_t)sizeof(nextdir), SEEK_SET);
         tif->tif_diroff = 0;
        if (!WriteOK(tif, &(tif->tif_diroff), sizeof (nextdir))) {
-               TIFFError(module, "Error writing directory link");
+               TIFFErrorExt(tif->tif_clientdata, module,
+                            "Error writing directory link");
                return (0);
        }
     }
@@ -1151,8 +1317,7 @@ TIFFRewriteDirectory( TIFF *tif )
 
 
 /*
- * Link the current directory into the
- * directory chain for the file.
+ * Link the current directory into the directory chain for the file.
  */
 static int
 TIFFLinkDirectory(TIFF* tif)
@@ -1172,9 +1337,9 @@ TIFFLinkDirectory(TIFF* tif)
         if (tif->tif_flags & TIFF_INSUBIFD) {
                (void) TIFFSeekFile(tif, tif->tif_subifdoff, SEEK_SET);
                if (!WriteOK(tif, &diroff, sizeof (diroff))) {
-                       TIFFError(module,
-                           "%s: Error writing SubIFD directory link",
-                           tif->tif_name);
+                       TIFFErrorExt(tif->tif_clientdata, module,
+                                    "%s: Error writing SubIFD directory link",
+                                    tif->tif_name);
                        return (0);
                }
                /*
@@ -1198,7 +1363,8 @@ TIFFLinkDirectory(TIFF* tif)
                                    (toff_t)(TIFF_MAGIC_SIZE+TIFF_VERSION_SIZE),
                                     SEEK_SET);
                if (!WriteOK(tif, &diroff, sizeof (diroff))) {
-                       TIFFError(tif->tif_name, "Error writing TIFF header");
+                       TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+                                    "Error writing TIFF header");
                        return (0);
                }
                return (1);
@@ -1212,7 +1378,8 @@ TIFFLinkDirectory(TIFF* tif)
 
                if (!SeekOK(tif, nextdir) ||
                    !ReadOK(tif, &dircount, sizeof (dircount))) {
-                       TIFFError(module, "Error fetching directory count");
+                       TIFFErrorExt(tif->tif_clientdata, module,
+                                    "Error fetching directory count");
                        return (0);
                }
                if (tif->tif_flags & TIFF_SWAB)
@@ -1220,7 +1387,8 @@ TIFFLinkDirectory(TIFF* tif)
                (void) TIFFSeekFile(tif,
                    dircount * sizeof (TIFFDirEntry), SEEK_CUR);
                if (!ReadOK(tif, &nextdir, sizeof (nextdir))) {
-                       TIFFError(module, "Error fetching directory link");
+                       TIFFErrorExt(tif->tif_clientdata, module,
+                                    "Error fetching directory link");
                        return (0);
                }
                if (tif->tif_flags & TIFF_SWAB)
@@ -1229,10 +1397,18 @@ TIFFLinkDirectory(TIFF* tif)
         off = TIFFSeekFile(tif, 0, SEEK_CUR); /* get current offset */
         (void) TIFFSeekFile(tif, off - (toff_t)sizeof(nextdir), SEEK_SET);
        if (!WriteOK(tif, &diroff, sizeof (diroff))) {
-               TIFFError(module, "Error writing directory link");
+               TIFFErrorExt(tif->tif_clientdata, module,
+                            "Error writing directory link");
                return (0);
        }
        return (1);
 }
 
 /* vim: set ts=8 sts=8 sw=8 noet: */
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
index 0a07fe2..da86150 100644 (file)
@@ -1,4 +1,4 @@
-/* $Header: /home/vp/work/opencv-cvsbackup/opencv/3rdparty/libtiff/tif_dumpmode.c,v 1.1 2005-06-17 13:54:52 vp153 Exp $ */
+/* $Header: /cvs/maptools/cvsroot/libtiff/libtiff/tif_dumpmode.c,v 1.5.2.2 2010-06-08 18:50:42 bfriesen Exp $ */
 
 /*
  * Copyright (c) 1988-1997 Sam Leffler
@@ -45,7 +45,7 @@ DumpModeEncode(TIFF* tif, tidata_t pp, tsize_t cc, tsample_t s)
                if (tif->tif_rawcc + n > tif->tif_rawdatasize)
                        n = tif->tif_rawdatasize - tif->tif_rawcc;
 
-                assert( n > 0 );
+               assert( n > 0 );
                 
                /*
                 * Avoid copy if client has setup raw
@@ -71,8 +71,10 @@ static int
 DumpModeDecode(TIFF* tif, tidata_t buf, tsize_t cc, tsample_t s)
 {
        (void) s;
+/*         fprintf(stderr,"DumpModeDecode: scanline %ld, expected %ld bytes, got %ld bytes\n", */
+/*                 (long) tif->tif_row, (long) tif->tif_rawcc, (long) cc); */
        if (tif->tif_rawcc < cc) {
-               TIFFError(tif->tif_name,
+               TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
                    "DumpModeDecode: Not enough data for scanline %d",
                    tif->tif_row);
                return (0);
@@ -115,3 +117,10 @@ TIFFInitDumpMode(TIFF* tif, int scheme)
        tif->tif_seek = DumpModeSeek;
        return (1);
 }
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
index 2024485..2377abd 100644 (file)
@@ -1,4 +1,4 @@
-/* $Header: /home/vp/work/opencv-cvsbackup/opencv/3rdparty/libtiff/tif_error.c,v 1.1 2005-06-17 13:54:52 vp153 Exp $ */
+/* $Header: /cvs/maptools/cvsroot/libtiff/libtiff/tif_error.c,v 1.4.2.1 2010-06-08 18:50:42 bfriesen Exp $ */
 
 /*
  * Copyright (c) 1988-1997 Sam Leffler
@@ -29,6 +29,8 @@
  */
 #include "tiffiop.h"
 
+TIFFErrorHandlerExt _TIFFerrorHandlerExt = NULL;
+
 TIFFErrorHandler
 TIFFSetErrorHandler(TIFFErrorHandler handler)
 {
@@ -37,13 +39,42 @@ TIFFSetErrorHandler(TIFFErrorHandler handler)
        return (prev);
 }
 
+TIFFErrorHandlerExt
+TIFFSetErrorHandlerExt(TIFFErrorHandlerExt handler)
+{
+       TIFFErrorHandlerExt prev = _TIFFerrorHandlerExt;
+       _TIFFerrorHandlerExt = handler;
+       return (prev);
+}
+
 void
 TIFFError(const char* module, const char* fmt, ...)
 {
-       if (_TIFFerrorHandler) {
-               va_list ap;
-               va_start(ap, fmt);
+       va_list ap;
+       va_start(ap, fmt);
+       if (_TIFFerrorHandler)
                (*_TIFFerrorHandler)(module, fmt, ap);
-               va_end(ap);
-       }
+       if (_TIFFerrorHandlerExt)
+               (*_TIFFerrorHandlerExt)(0, module, fmt, ap);
+       va_end(ap);
 }
+
+void
+TIFFErrorExt(thandle_t fd, const char* module, const char* fmt, ...)
+{
+       va_list ap;
+       va_start(ap, fmt);
+       if (_TIFFerrorHandler)
+               (*_TIFFerrorHandler)(module, fmt, ap);
+       if (_TIFFerrorHandlerExt)
+               (*_TIFFerrorHandlerExt)(fd, module, fmt, ap);
+       va_end(ap);
+}
+
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
index d76131b..b67c0f0 100644 (file)
@@ -1,4 +1,4 @@
-/* $Header: /home/vp/work/opencv-cvsbackup/opencv/3rdparty/libtiff/tif_extension.c,v 1.1 2005-06-17 13:54:52 vp153 Exp $ */
+/* $Header: /cvs/maptools/cvsroot/libtiff/libtiff/tif_extension.c,v 1.4.2.1 2010-06-08 18:50:42 bfriesen Exp $ */
 
 /*
  * Copyright (c) 1988-1997 Sam Leffler
@@ -109,3 +109,10 @@ void TIFFSetClientInfo( TIFF *tif, void *data, const char *name )
 
     tif->tif_clientinfo = link;
 }
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
index 4131a76..9eec4ab 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: tif_fax3.c,v 1.1 2005-06-17 13:54:52 vp153 Exp $ */
+/* $Id: tif_fax3.c,v 1.43.2.10 2010-06-09 17:16:58 bfriesen Exp $ */
 
 /*
  * Copyright (c) 1990-1997 Sam Leffler
@@ -63,6 +63,7 @@ typedef struct {
        char*   faxdcs;                 /* Table 2/T.30 encoded session params */
        TIFFVGetMethod vgetparent;      /* super-class method */
        TIFFVSetMethod vsetparent;      /* super-class method */
+       TIFFPrintMethod printdir;       /* super-class method */
 } Fax3BaseState;
 #define        Fax3State(tif)          ((Fax3BaseState*) (tif)->tif_data)
 
@@ -85,6 +86,8 @@ typedef struct {
        unsigned char*  refline;        /* reference line for 2d decoding */
        int     k;                      /* #rows left that can be 2d encoded */
        int     maxk;                   /* max #rows that can be 2d encoded */
+
+       int line;
 } Fax3CodecState;
 #define        DecoderState(tif)       ((Fax3CodecState*) Fax3State(tif))
 #define        EncoderState(tif)       ((Fax3CodecState*) Fax3State(tif))
@@ -167,6 +170,7 @@ Fax3PreDecode(TIFF* tif, tsample_t s)
                sp->refruns[0] = (uint32) sp->b.rowpixels;
                sp->refruns[1] = 0;
        }
+       sp->line = 0;
        return (1);
 }
 
@@ -177,39 +181,48 @@ Fax3PreDecode(TIFF* tif, tsample_t s)
  */
 
 static void
-Fax3Unexpected(const char* module, TIFF* tif, uint32 a0)
+Fax3Unexpected(const char* module, TIFF* tif, uint32 line, uint32 a0)
 {
-       TIFFError(module, "%s: Bad code word at scanline %d (x %lu)",
-           tif->tif_name, tif->tif_row, (unsigned long) a0);
+       TIFFErrorExt(tif->tif_clientdata, module, "%s: Bad code word at line %u of %s %u (x %u)",
+                    tif->tif_name, line, isTiled(tif) ? "tile" : "strip",
+                    (isTiled(tif) ? tif->tif_curtile : tif->tif_curstrip),
+                    a0);
 }
-#define        unexpected(table, a0)   Fax3Unexpected(module, tif, a0)
+#define        unexpected(table, a0)   Fax3Unexpected(module, tif, sp->line, a0)
 
 static void
-Fax3Extension(const char* module, TIFF* tif, uint32 a0)
+Fax3Extension(const char* module, TIFF* tif, uint32 line, uint32 a0)
 {
-       TIFFError(module,
-           "%s: Uncompressed data (not supported) at scanline %d (x %lu)",
-           tif->tif_name, tif->tif_row, (unsigned long) a0);
+       TIFFErrorExt(tif->tif_clientdata, module,
+                    "%s: Uncompressed data (not supported) at line %u of %s %u (x %u)",
+                    tif->tif_name, line, isTiled(tif) ? "tile" : "strip",
+                    (isTiled(tif) ? tif->tif_curtile : tif->tif_curstrip),
+                    a0);
 }
-#define        extension(a0)   Fax3Extension(module, tif, a0)
+#define        extension(a0)   Fax3Extension(module, tif, sp->line, a0)
 
 static void
-Fax3BadLength(const char* module, TIFF* tif, uint32 a0, uint32 lastx)
+Fax3BadLength(const char* module, TIFF* tif, uint32 line, uint32 a0, uint32 lastx)
 {
-       TIFFWarning(module, "%s: %s at scanline %d (got %lu, expected %lu)",
-           tif->tif_name,
-           a0 < lastx ? "Premature EOL" : "Line length mismatch",
-           tif->tif_row, (unsigned long) a0, (unsigned long) lastx);
+       TIFFWarningExt(tif->tif_clientdata, module, "%s: %s at line %u of %s %u (got %u, expected %u)",
+                      tif->tif_name,
+                      a0 < lastx ? "Premature EOL" : "Line length mismatch",
+                      line, isTiled(tif) ? "tile" : "strip",
+                      (isTiled(tif) ? tif->tif_curtile : tif->tif_curstrip),
+                      a0, lastx);
 }
-#define        badlength(a0,lastx)     Fax3BadLength(module, tif, a0, lastx)
+#define        badlength(a0,lastx)     Fax3BadLength(module, tif, sp->line, a0, lastx)
 
 static void
-Fax3PrematureEOF(const char* module, TIFF* tif, uint32 a0)
+Fax3PrematureEOF(const char* module, TIFF* tif, uint32 line, uint32 a0)
 {
-       TIFFWarning(module, "%s: Premature EOF at scanline %d (x %lu)",
-           tif->tif_name, tif->tif_row, (unsigned long) a0);
+       TIFFWarningExt(tif->tif_clientdata, module, "%s: Premature EOF at line %u of %s %u (x %u)",
+           tif->tif_name,
+                      line, isTiled(tif) ? "tile" : "strip",
+                      (isTiled(tif) ? tif->tif_curtile : tif->tif_curstrip),
+                      a0);
 }
-#define        prematureEOF(a0)        Fax3PrematureEOF(module, tif, a0)
+#define        prematureEOF(a0)        Fax3PrematureEOF(module, tif, sp->line, a0)
 
 #define        Nop
 
@@ -238,6 +251,7 @@ Fax3Decode1D(TIFF* tif, tidata_t buf, tsize_t occ, tsample_t s)
                (*sp->fill)(buf, thisrun, pa, lastx);
                buf += sp->b.rowbytes;
                occ -= sp->b.rowbytes;
+               sp->line++;
                continue;
        EOF1D:                          /* premature EOF */
                CLEANUP_RUNS();
@@ -286,10 +300,11 @@ Fax3Decode2D(TIFF* tif, tidata_t buf, tsize_t occ, tsample_t s)
                else
                        EXPAND2D(EOF2Da);
                (*sp->fill)(buf, thisrun, pa, lastx);
-               SETVAL(0);              /* imaginary change for reference */
+               SETVALUE(0);            /* imaginary change for reference */
                SWAP(uint32*, sp->curruns, sp->refruns);
                buf += sp->b.rowbytes;
                occ -= sp->b.rowbytes;
+               sp->line++;
                continue;
        EOF2D:                          /* premature EOF */
                CLEANUP_RUNS();
@@ -437,21 +452,6 @@ _TIFFFax3fillruns(unsigned char* buf, uint32* runs, uint32* erun, uint32 lastx)
 #undef ZERO
 #undef FILL
 
-static char *
-CheckMalloc(TIFF* tif, size_t nmemb, size_t elem_size, const char* what)
-{
-       char    *cp = NULL;
-       tsize_t bytes = nmemb * elem_size;
-
-       if (nmemb && elem_size && bytes / elem_size == nmemb)
-               cp = (char*) _TIFFmalloc(bytes);
-
-       if (cp == NULL)
-               TIFFError(tif->tif_name, "No space %s", what);
-       
-       return (cp);
-}
-
 /*
  * Setup G3/G4-related compression/decompression state
  * before data is processed.  This routine is called once
@@ -464,13 +464,12 @@ Fax3SetupState(TIFF* tif)
 {
        TIFFDirectory* td = &tif->tif_dir;
        Fax3BaseState* sp = Fax3State(tif);
-       long rowbytes, rowpixels;
        int needsRefLine;
        Fax3CodecState* dsp = (Fax3CodecState*) Fax3State(tif);
-       uint32 nruns;
+       uint32 rowbytes, rowpixels, nruns;
 
        if (td->td_bitspersample != 1) {
-               TIFFError(tif->tif_name,
+               TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
                    "Bits/sample must be 1 for Group 3/4 encoding/decoding");
                return (0);
        }
@@ -494,15 +493,31 @@ Fax3SetupState(TIFF* tif)
            td->td_compression == COMPRESSION_CCITTFAX4
        );
 
-       nruns = needsRefLine ? 2*TIFFroundup(rowpixels,32) : rowpixels;
-
-       dsp->runs = (uint32*) CheckMalloc(tif, 2*nruns+3, sizeof (uint32),
-                                         "for Group 3/4 run arrays");
+       /*
+         Assure that allocation computations do not overflow.
+  
+         TIFFroundup and TIFFSafeMultiply return zero on integer overflow
+       */
+       dsp->runs=(uint32*) NULL;
+       nruns = TIFFroundup(rowpixels,32);
+       if (needsRefLine) {
+               nruns = TIFFSafeMultiply(uint32,nruns,2);
+       }
+       if ((nruns == 0) || (TIFFSafeMultiply(uint32,nruns,2) == 0)) {
+               TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+                            "Row pixels integer overflow (rowpixels %u)",
+                            rowpixels);
+               return (0);
+       }
+       dsp->runs = (uint32*) _TIFFCheckMalloc(tif,
+                                              TIFFSafeMultiply(uint32,nruns,2),
+                                              sizeof (uint32),
+                                              "for Group 3/4 run arrays");
        if (dsp->runs == NULL)
                return (0);
        dsp->curruns = dsp->runs;
        if (needsRefLine)
-               dsp->refruns = dsp->runs + (nruns>>1);
+               dsp->refruns = dsp->runs + nruns;
        else
                dsp->refruns = NULL;
        if (td->td_compression == COMPRESSION_CCITTFAX3
@@ -523,7 +538,7 @@ Fax3SetupState(TIFF* tif)
                 */
                esp->refline = (unsigned char*) _TIFFmalloc(rowbytes);
                if (esp->refline == NULL) {
-                       TIFFError("Fax3SetupState",
+                       TIFFErrorExt(tif->tif_clientdata, "Fax3SetupState",
                            "%s: No space for Group 3/4 reference line",
                            tif->tif_name);
                        return (0);
@@ -721,6 +736,7 @@ Fax3PreEncode(TIFF* tif, tsample_t s)
                sp->k = sp->maxk-1;
        } else
                sp->k = sp->maxk = 0;
+       sp->line = 0;
        return (1);
 }
 
@@ -776,7 +792,7 @@ static      int32 find1span(unsigned char*, int32, int32);
  * table.  The ``base'' of the bit string is supplied
  * along with the start+end bit indices.
  */
-inline static int32
+static int32
 find0span(unsigned char* bp, int32 bs, int32 be)
 {
        int32 bits = be - bs;
@@ -798,7 +814,7 @@ find0span(unsigned char* bp, int32 bs, int32 be)
                bp++;
        } else
                span = 0;
-       if (bits >= 2*8*sizeof (long)) {
+       if (bits >= (int32)(2 * 8 * sizeof(long))) {
                long* lp;
                /*
                 * Align to longword boundary and check longwords.
@@ -810,7 +826,7 @@ find0span(unsigned char* bp, int32 bs, int32 be)
                        bp++;
                }
                lp = (long*) bp;
-               while (bits >= 8*sizeof (long) && *lp == 0) {
+               while ((bits >= (int32)(8 * sizeof(long))) && (0 == *lp)) {
                        span += 8*sizeof (long), bits -= 8*sizeof (long);
                        lp++;
                }
@@ -835,7 +851,7 @@ find0span(unsigned char* bp, int32 bs, int32 be)
        return (span);
 }
 
-inline static int32
+static int32
 find1span(unsigned char* bp, int32 bs, int32 be)
 {
        int32 bits = be - bs;
@@ -857,7 +873,7 @@ find1span(unsigned char* bp, int32 bs, int32 be)
                bp++;
        } else
                span = 0;
-       if (bits >= 2*8*sizeof (long)) {
+       if (bits >= (int32)(2 * 8 * sizeof(long))) {
                long* lp;
                /*
                 * Align to longword boundary and check longwords.
@@ -869,7 +885,7 @@ find1span(unsigned char* bp, int32 bs, int32 be)
                        bp++;
                }
                lp = (long*) bp;
-               while (bits >= 8*sizeof (long) && *lp == ~0) {
+               while ((bits >= (int32)(8 * sizeof(long))) && (~0 == *lp)) {
                        span += 8*sizeof (long), bits -= 8*sizeof (long);
                        lp++;
                }
@@ -944,17 +960,17 @@ Fax3Encode1DRow(TIFF* tif, unsigned char* bp, uint32 bits)
 }
 
 static const tableentry horizcode =
-    { 3, 0x1 };                /* 001 */
+    { 3, 0x1, 0 };     /* 001 */
 static const tableentry passcode =
-    { 4, 0x1 };                /* 0001 */
+    { 4, 0x1, 0 };     /* 0001 */
 static const tableentry vcodes[7] = {
-    { 7, 0x03 },       /* 0000 011 */
-    { 6, 0x03 },       /* 0000 11 */
-    { 3, 0x03 },       /* 011 */
-    { 1, 0x1 },                /* 1 */
-    { 3, 0x2 },                /* 010 */
-    { 6, 0x02 },       /* 0000 10 */
-    { 7, 0x02 }                /* 0000 010 */
+    { 7, 0x03, 0 },    /* 0000 011 */
+    { 6, 0x03, 0 },    /* 0000 11 */
+    { 3, 0x03, 0 },    /* 011 */
+    { 1, 0x1, 0 },     /* 1 */
+    { 3, 0x2, 0 },     /* 010 */
+    { 6, 0x02, 0 },    /* 0000 10 */
+    { 7, 0x02, 0 }     /* 0000 010 */
 };
 
 /*
@@ -1071,19 +1087,28 @@ Fax3Close(TIFF* tif)
 static void
 Fax3Cleanup(TIFF* tif)
 {
-       if (tif->tif_data) {
-               Fax3CodecState* sp = DecoderState(tif);
-
-               if (sp->runs)
-                       _TIFFfree(sp->runs);
-               if (sp->refline)
-                       _TIFFfree(sp->refline);
-
-               if (Fax3State(tif)->subaddress)
-                       _TIFFfree(Fax3State(tif)->subaddress);
-               _TIFFfree(tif->tif_data);
-               tif->tif_data = NULL;
-       }
+       Fax3CodecState* sp = DecoderState(tif);
+       
+       assert(sp != 0);
+
+       tif->tif_tagmethods.vgetfield = sp->b.vgetparent;
+       tif->tif_tagmethods.vsetfield = sp->b.vsetparent;
+       tif->tif_tagmethods.printdir = sp->b.printdir;
+
+       if (sp->runs)
+               _TIFFfree(sp->runs);
+       if (sp->refline)
+               _TIFFfree(sp->refline);
+
+       if (Fax3State(tif)->subaddress)
+               _TIFFfree(Fax3State(tif)->subaddress);
+       if (Fax3State(tif)->faxdcs)
+               _TIFFfree(Fax3State(tif)->faxdcs);
+
+       _TIFFfree(tif->tif_data);
+       tif->tif_data = NULL;
+
+       _TIFFSetDefaultCompressionState(tif);
 }
 
 #define        FIELD_BADFAXLINES       (FIELD_CODEC+0)
@@ -1134,14 +1159,18 @@ static int
 Fax3VSetField(TIFF* tif, ttag_t tag, va_list ap)
 {
        Fax3BaseState* sp = Fax3State(tif);
+       const TIFFFieldInfo* fip;
+
+       assert(sp != 0);
+       assert(sp->vsetparent != 0);
 
        switch (tag) {
        case TIFFTAG_FAXMODE:
                sp->mode = va_arg(ap, int);
-               return (1);                     /* NB: pseudo tag */
+               return 1;                       /* NB: pseudo tag */
        case TIFFTAG_FAXFILLFUNC:
                DecoderState(tif)->fill = va_arg(ap, TIFFFaxFillFunc);
-               return (1);                     /* NB: pseudo tag */
+               return 1;                       /* NB: pseudo tag */
        case TIFFTAG_GROUP3OPTIONS:
                /* XXX: avoid reading options if compression mismatches. */
                if (tif->tif_dir.td_compression == COMPRESSION_CCITTFAX3)
@@ -1176,9 +1205,14 @@ Fax3VSetField(TIFF* tif, ttag_t tag, va_list ap)
        default:
                return (*sp->vsetparent)(tif, tag, ap);
        }
-       TIFFSetFieldBit(tif, _TIFFFieldWithTag(tif, tag)->field_bit);
+       
+       if ((fip = _TIFFFieldWithTag(tif, tag)))
+               TIFFSetFieldBit(tif, fip->field_bit);
+       else
+               return 0;
+
        tif->tif_flags |= TIFF_DIRTYDIRECT;
-       return (1);
+       return 1;
 }
 
 static int
@@ -1186,6 +1220,8 @@ Fax3VGetField(TIFF* tif, ttag_t tag, va_list ap)
 {
        Fax3BaseState* sp = Fax3State(tif);
 
+       assert(sp != 0);
+
        switch (tag) {
        case TIFFTAG_FAXMODE:
                *va_arg(ap, int*) = sp->mode;
@@ -1229,6 +1265,8 @@ Fax3PrintDir(TIFF* tif, FILE* fd, long flags)
 {
        Fax3BaseState* sp = Fax3State(tif);
 
+       assert(sp != 0);
+
        (void) flags;
        if (TIFFFieldSet(tif,FIELD_OPTIONS)) {
                const char* sep = " ";
@@ -1290,13 +1328,22 @@ InitCCITTFax3(TIFF* tif)
        Fax3BaseState* sp;
 
        /*
+        * Merge codec-specific tag information.
+        */
+       if (!_TIFFMergeFieldInfo(tif, faxFieldInfo, N(faxFieldInfo))) {
+               TIFFErrorExt(tif->tif_clientdata, "InitCCITTFax3",
+                       "Merging common CCITT Fax codec-specific tags failed");
+               return 0;
+       }
+
+       /*
         * Allocate state block so tag methods have storage to record values.
         */
        tif->tif_data = (tidata_t)
                _TIFFmalloc(sizeof (Fax3CodecState));
 
        if (tif->tif_data == NULL) {
-               TIFFError("TIFFInitCCITTFax3",
+               TIFFErrorExt(tif->tif_clientdata, "TIFFInitCCITTFax3",
                    "%s: No space for state block", tif->tif_name);
                return (0);
        }
@@ -1305,14 +1352,13 @@ InitCCITTFax3(TIFF* tif)
         sp->rw_mode = tif->tif_mode;
 
        /*
-        * Merge codec-specific tag information and
-        * override parent get/set field methods.
+        * Override parent get/set field methods.
         */
-       _TIFFMergeFieldInfo(tif, faxFieldInfo, N(faxFieldInfo));
        sp->vgetparent = tif->tif_tagmethods.vgetfield;
        tif->tif_tagmethods.vgetfield = Fax3VGetField; /* hook for codec tags */
        sp->vsetparent = tif->tif_tagmethods.vsetfield;
        tif->tif_tagmethods.vsetfield = Fax3VSetField; /* hook for codec tags */
+       sp->printdir = tif->tif_tagmethods.printdir;
        tif->tif_tagmethods.printdir = Fax3PrintDir;   /* hook for codec tags */
        sp->groupoptions = 0;   
        sp->recvparams = 0;
@@ -1348,15 +1394,23 @@ InitCCITTFax3(TIFF* tif)
 int
 TIFFInitCCITTFax3(TIFF* tif, int scheme)
 {
+       (void) scheme;
        if (InitCCITTFax3(tif)) {
-               _TIFFMergeFieldInfo(tif, fax3FieldInfo, N(fax3FieldInfo));
+               /*
+                * Merge codec-specific tag information.
+                */
+               if (!_TIFFMergeFieldInfo(tif, fax3FieldInfo, N(fax3FieldInfo))) {
+                       TIFFErrorExt(tif->tif_clientdata, "TIFFInitCCITTFax3",
+                       "Merging CCITT Fax 3 codec-specific tags failed");
+                       return 0;
+               }
 
                /*
                 * The default format is Class/F-style w/o RTC.
                 */
                return TIFFSetField(tif, TIFFTAG_FAXMODE, FAXMODE_CLASSF);
        } else
-               return (0);
+               return 01;
 }
 
 /*
@@ -1390,22 +1444,23 @@ Fax4Decode(TIFF* tif, tidata_t buf, tsize_t occ, tsample_t s)
                 if (EOLcnt)
                     goto EOFG4;
                (*sp->fill)(buf, thisrun, pa, lastx);
-               SETVAL(0);              /* imaginary change for reference */
+               SETVALUE(0);            /* imaginary change for reference */
                SWAP(uint32*, sp->curruns, sp->refruns);
                buf += sp->b.rowbytes;
                occ -= sp->b.rowbytes;
+               sp->line++;
                continue;
        EOFG4:
                 NeedBits16( 13, BADG4 );
         BADG4:
 #ifdef FAX3_DEBUG
                 if( GetBits(13) != 0x1001 )
-                    fputs( "Bad RTC\n", stderr );
+                    fputs( "Bad EOFB\n", stderr );
 #endif                
                 ClrBits( 13 );
                (*sp->fill)(buf, thisrun, pa, lastx);
                UNCACHE_STATE(tif, sp);
-               return (-1);
+               return ( sp->line ? 1 : -1);    /* don't error on badly-terminated strips */
        }
        UNCACHE_STATE(tif, sp);
        return (1);
@@ -1447,8 +1502,16 @@ Fax4PostEncode(TIFF* tif)
 int
 TIFFInitCCITTFax4(TIFF* tif, int scheme)
 {
+       (void) scheme;
        if (InitCCITTFax3(tif)) {               /* reuse G3 support */
-               _TIFFMergeFieldInfo(tif, fax4FieldInfo, N(fax4FieldInfo));
+               /*
+                * Merge codec-specific tag information.
+                */
+               if (!_TIFFMergeFieldInfo(tif, fax4FieldInfo, N(fax4FieldInfo))) {
+                       TIFFErrorExt(tif->tif_clientdata, "TIFFInitCCITTFax4",
+                       "Merging CCITT Fax 4 codec-specific tags failed");
+                       return 0;
+               }
 
                tif->tif_decoderow = Fax4Decode;
                tif->tif_decodestrip = Fax4Decode;
@@ -1507,6 +1570,7 @@ Fax3DecodeRLE(TIFF* tif, tidata_t buf, tsize_t occ, tsample_t s)
                }
                buf += sp->b.rowbytes;
                occ -= sp->b.rowbytes;
+               sp->line++;
                continue;
        EOFRLE:                         /* premature EOF */
                (*sp->fill)(buf, thisrun, pa, lastx);
@@ -1520,6 +1584,7 @@ Fax3DecodeRLE(TIFF* tif, tidata_t buf, tsize_t occ, tsample_t s)
 int
 TIFFInitCCITTRLE(TIFF* tif, int scheme)
 {
+       (void) scheme;
        if (InitCCITTFax3(tif)) {               /* reuse G3 support */
                tif->tif_decoderow = Fax3DecodeRLE;
                tif->tif_decodestrip = Fax3DecodeRLE;
@@ -1536,6 +1601,7 @@ TIFFInitCCITTRLE(TIFF* tif, int scheme)
 int
 TIFFInitCCITTRLEW(TIFF* tif, int scheme)
 {
+       (void) scheme;
        if (InitCCITTFax3(tif)) {               /* reuse G3 support */
                tif->tif_decoderow = Fax3DecodeRLE;
                tif->tif_decodestrip = Fax3DecodeRLE;
@@ -1551,3 +1617,10 @@ TIFFInitCCITTRLEW(TIFF* tif, int scheme)
 #endif /* CCITT_SUPPORT */
 
 /* vim: set ts=8 sts=8 sw=8 noet: */
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
index f02ddec..40718bc 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: tif_fax3.h,v 1.1 2005-06-17 13:54:52 vp153 Exp $ */
+/* $Id: tif_fax3.h,v 1.5.2.1 2010-06-08 18:50:42 bfriesen Exp $ */
 
 /*
  * Copyright (c) 1990-1997 Sam Leffler
@@ -217,9 +217,9 @@ static const char* StateNames[] = {
     ClrBits(TabEnt->Width);                                            \
 } while (0)
 
-#define SETVAL(x) do {                                                 \
+#define SETVALUE(x) do {                                                       \
     *pa++ = RunLength + (x);                                           \
-    printf("SETVAL: %d\t%d\n", RunLength + (x), a0);                   \
+    printf("SETVALUE: %d\t%d\n", RunLength + (x), a0);                 \
     a0 += x;                                                           \
     RunLength = 0;                                                     \
 } while (0)
@@ -239,7 +239,7 @@ static const char* StateNames[] = {
  * Append a run to the run length array for the
  * current row and reset decoding state.
  */
-#define SETVAL(x) do {                                                 \
+#define SETVALUE(x) do {                                                       \
     *pa++ = RunLength + (x);                                           \
     a0 += (x);                                                         \
     RunLength = 0;                                                     \
@@ -284,7 +284,7 @@ static const char* StateNames[] = {
  */
 #define        CLEANUP_RUNS() do {                                             \
     if (RunLength)                                                     \
-       SETVAL(0);                                                      \
+       SETVALUE(0);                                                    \
     if (a0 != lastx) {                                                 \
        badlength(a0, lastx);                                           \
        while (a0 > lastx && pa > thisrun)                              \
@@ -293,11 +293,11 @@ static const char* StateNames[] = {
            if (a0 < 0)                                                 \
                a0 = 0;                                                 \
            if ((pa-thisrun)&1)                                         \
-               SETVAL(0);                                              \
-           SETVAL(lastx - a0);                                         \
+               SETVALUE(0);                                            \
+           SETVALUE(lastx - a0);                                               \
        } else if (a0 > lastx) {                                        \
-           SETVAL(lastx);                                              \
-           SETVAL(0);                                                  \
+           SETVALUE(lastx);                                            \
+           SETVALUE(0);                                                        \
        }                                                               \
     }                                                                  \
 } while (0)
@@ -323,7 +323,7 @@ static const char* StateNames[] = {
                EOLcnt = 1;                                             \
                goto done1d;                                            \
            case S_TermW:                                               \
-               SETVAL(TabEnt->Param);                                  \
+               SETVALUE(TabEnt->Param);                                        \
                goto doneWhite1d;                                       \
            case S_MakeUpW:                                             \
            case S_MakeUp:                                              \
@@ -345,7 +345,7 @@ static const char* StateNames[] = {
                EOLcnt = 1;                                             \
                goto done1d;                                            \
            case S_TermB:                                               \
-               SETVAL(TabEnt->Param);                                  \
+               SETVALUE(TabEnt->Param);                                        \
                goto doneBlack1d;                                       \
            case S_MakeUpB:                                             \
            case S_MakeUp:                                              \
@@ -402,7 +402,7 @@ done1d:                                                                     \
                    LOOKUP16(13, TIFFFaxBlackTable, eof2d);             \
                    switch (TabEnt->State) {                            \
                    case S_TermB:                                       \
-                       SETVAL(TabEnt->Param);                          \
+                       SETVALUE(TabEnt->Param);                                \
                        goto doneWhite2da;                              \
                    case S_MakeUpB:                                     \
                    case S_MakeUp:                                      \
@@ -418,7 +418,7 @@ done1d:                                                                     \
                    LOOKUP16(12, TIFFFaxWhiteTable, eof2d);             \
                    switch (TabEnt->State) {                            \
                    case S_TermW:                                       \
-                       SETVAL(TabEnt->Param);                          \
+                       SETVALUE(TabEnt->Param);                                \
                        goto doneBlack2da;                              \
                    case S_MakeUpW:                                     \
                    case S_MakeUp:                                      \
@@ -435,7 +435,7 @@ done1d:                                                                     \
                    LOOKUP16(12, TIFFFaxWhiteTable, eof2d);             \
                    switch (TabEnt->State) {                            \
                    case S_TermW:                                       \
-                       SETVAL(TabEnt->Param);                          \
+                       SETVALUE(TabEnt->Param);                                \
                        goto doneWhite2db;                              \
                    case S_MakeUpW:                                     \
                    case S_MakeUp:                                      \
@@ -451,7 +451,7 @@ done1d:                                                                     \
                    LOOKUP16(13, TIFFFaxBlackTable, eof2d);             \
                    switch (TabEnt->State) {                            \
                    case S_TermB:                                       \
-                       SETVAL(TabEnt->Param);                          \
+                       SETVALUE(TabEnt->Param);                                \
                        goto doneBlack2db;                              \
                    case S_MakeUpB:                                     \
                    case S_MakeUp:                                      \
@@ -468,17 +468,17 @@ done1d:                                                                   \
            break;                                                      \
        case S_V0:                                                      \
            CHECK_b1;                                                   \
-           SETVAL(b1 - a0);                                            \
+           SETVALUE(b1 - a0);                                          \
            b1 += *pb++;                                                \
            break;                                                      \
        case S_VR:                                                      \
            CHECK_b1;                                                   \
-           SETVAL(b1 - a0 + TabEnt->Param);                            \
+           SETVALUE(b1 - a0 + TabEnt->Param);                          \
            b1 += *pb++;                                                \
            break;                                                      \
        case S_VL:                                                      \
            CHECK_b1;                                                   \
-           SETVAL(b1 - a0 - TabEnt->Param);                            \
+           SETVALUE(b1 - a0 - TabEnt->Param);                          \
            b1 -= *--pb;                                                \
            break;                                                      \
        case S_Ext:                                                     \
@@ -517,9 +517,16 @@ done1d:                                                                    \
                goto badMain2d;                                         \
            ClrBits(1);                                                 \
        }                                                               \
-       SETVAL(0);                                                      \
+       SETVALUE(0);                                                    \
     }                                                                  \
 eol2d:                                                                 \
     CLEANUP_RUNS();                                                    \
 } while (0)
 #endif /* _FAX3_ */
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
index 08ce1ad..822191e 100644 (file)
 {8,4,6},{8,2,3},{8,3,4},{8,2,2},{8,5,7},{8,2,3},{8,3,1},{8,2,2},{8,4,5},{8,2,3},
 {8,3,4},{8,2,2}
 };
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
index 981d5dd..7ecc4d8 100644 (file)
@@ -1,4 +1,4 @@
-/* $Header: /home/vp/work/opencv-cvsbackup/opencv/3rdparty/libtiff/tif_flush.c,v 1.1 2005-06-17 13:54:52 vp153 Exp $ */
+/* $Header: /cvs/maptools/cvsroot/libtiff/libtiff/tif_flush.c,v 1.3.2.1 2010-06-08 18:50:42 bfriesen Exp $ */
 
 /*
  * Copyright (c) 1988-1997 Sam Leffler
@@ -65,3 +65,10 @@ TIFFFlushData(TIFF* tif)
        return (TIFFFlushData1(tif));
 }
 
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
index 209bca6..38455fb 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: tif_getimage.c,v 1.1 2005-06-17 13:54:52 vp153 Exp $ */
+/* $Id: tif_getimage.c,v 1.63.2.4 2010-06-08 18:50:42 bfriesen Exp $ */
 
 /*
  * Copyright (c) 1991-1997 Sam Leffler
 #include "tiffiop.h"
 #include <stdio.h>
 
-static int gtTileContig(TIFFRGBAImage*, uint32*, uint32, uint32);
-static int gtTileSeparate(TIFFRGBAImage*, uint32*, uint32, uint32);
-static int gtStripContig(TIFFRGBAImage*, uint32*, uint32, uint32);
-static int gtStripSeparate(TIFFRGBAImage*, uint32*, uint32, uint32);
-static int pickTileContigCase(TIFFRGBAImage*);
-static int pickTileSeparateCase(TIFFRGBAImage*);
-
-static const char photoTag[] = "PhotometricInterpretation";
+static int gtTileContig(TIFFRGBAImage*, uint32*, uint32, uint32);
+static int gtTileSeparate(TIFFRGBAImage*, uint32*, uint32, uint32);
+static int gtStripContig(TIFFRGBAImage*, uint32*, uint32, uint32);
+static int gtStripSeparate(TIFFRGBAImage*, uint32*, uint32, uint32);
+static int PickContigCase(TIFFRGBAImage*);
+static int PickSeparateCase(TIFFRGBAImage*);
+static const char photoTag[] = "PhotometricInterpretation";
 
 /* 
  * Helper constants used in Orientation tag handling
@@ -72,112 +71,122 @@ TIFFDisplay display_sRGB = {
 int
 TIFFRGBAImageOK(TIFF* tif, char emsg[1024])
 {
-    TIFFDirectory* td = &tif->tif_dir;
-    uint16 photometric;
-    int colorchannels;
+       TIFFDirectory* td = &tif->tif_dir;
+       uint16 photometric;
+       int colorchannels;
 
-    if (!tif->tif_decodestatus) {
-       sprintf(emsg, "Sorry, requested compression method is not configured");
-       return (0);
-    }
-    switch (td->td_bitspersample) {
-    case 1: case 2: case 4:
-    case 8: case 16:
-       break;
-    default:
-       sprintf(emsg, "Sorry, can not handle images with %d-bit samples",
-           td->td_bitspersample);
-       return (0);
-    }
-    colorchannels = td->td_samplesperpixel - td->td_extrasamples;
-    if (!TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &photometric)) {
-       switch (colorchannels) {
-       case 1:
-           photometric = PHOTOMETRIC_MINISBLACK;
-           break;
-       case 3:
-           photometric = PHOTOMETRIC_RGB;
-           break;
-       default:
-           sprintf(emsg, "Missing needed %s tag", photoTag);
-           return (0);
+       if (!tif->tif_decodestatus) {
+               sprintf(emsg, "Sorry, requested compression method is not configured");
+               return (0);
        }
-    }
-    switch (photometric) {
-    case PHOTOMETRIC_MINISWHITE:
-    case PHOTOMETRIC_MINISBLACK:
-    case PHOTOMETRIC_PALETTE:
-       if (td->td_planarconfig == PLANARCONFIG_CONTIG 
-            && td->td_samplesperpixel != 1
-            && td->td_bitspersample < 8 ) {
-           sprintf(emsg,
-                    "Sorry, can not handle contiguous data with %s=%d, "
-                    "and %s=%d and Bits/Sample=%d",
-                    photoTag, photometric,
-                    "Samples/pixel", td->td_samplesperpixel,
-                    td->td_bitspersample);
-           return (0);
-       }
-        /*
-        ** We should likely validate that any extra samples are either
-        ** to be ignored, or are alpha, and if alpha we should try to use
-        ** them.  But for now we won't bother with this. 
-        */
-       break;
-    case PHOTOMETRIC_YCBCR:
-       if (td->td_planarconfig != PLANARCONFIG_CONTIG) {
-           sprintf(emsg, "Sorry, can not handle YCbCr images with %s=%d",
-               "Planarconfiguration", td->td_planarconfig);
-           return (0);
+       switch (td->td_bitspersample) {
+               case 1:
+               case 2:
+               case 4:
+               case 8:
+               case 16:
+                       break;
+               default:
+                       sprintf(emsg, "Sorry, can not handle images with %d-bit samples",
+                           td->td_bitspersample);
+                       return (0);
        }
-       break;
-    case PHOTOMETRIC_RGB: 
-       if (colorchannels < 3) {
-           sprintf(emsg, "Sorry, can not handle RGB image with %s=%d",
-               "Color channels", colorchannels);
-           return (0);
-       }
-       break;
-    case PHOTOMETRIC_SEPARATED:
-       if (td->td_inkset != INKSET_CMYK) {
-           sprintf(emsg, "Sorry, can not handle separated image with %s=%d",
-               "InkSet", td->td_inkset);
-           return (0);
-       }
-       if (td->td_samplesperpixel < 4) {
-           sprintf(emsg, "Sorry, can not handle separated image with %s=%d",
-               "Samples/pixel", td->td_samplesperpixel);
-           return (0);
-       }
-       break;
-    case PHOTOMETRIC_LOGL:
-       if (td->td_compression != COMPRESSION_SGILOG) {
-           sprintf(emsg, "Sorry, LogL data must have %s=%d",
-               "Compression", COMPRESSION_SGILOG);
-           return (0);
-       }
-       break;
-    case PHOTOMETRIC_LOGLUV:
-       if (td->td_compression != COMPRESSION_SGILOG &&
-               td->td_compression != COMPRESSION_SGILOG24) {
-           sprintf(emsg, "Sorry, LogLuv data must have %s=%d or %d",
-               "Compression", COMPRESSION_SGILOG, COMPRESSION_SGILOG24);
-           return (0);
+       colorchannels = td->td_samplesperpixel - td->td_extrasamples;
+       if (!TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &photometric)) {
+               switch (colorchannels) {
+                       case 1:
+                               photometric = PHOTOMETRIC_MINISBLACK;
+                               break;
+                       case 3:
+                               photometric = PHOTOMETRIC_RGB;
+                               break;
+                       default:
+                               sprintf(emsg, "Missing needed %s tag", photoTag);
+                               return (0);
+               }
        }
-       if (td->td_planarconfig != PLANARCONFIG_CONTIG) {
-           sprintf(emsg, "Sorry, can not handle LogLuv images with %s=%d",
-               "Planarconfiguration", td->td_planarconfig);
-           return (0);
+       switch (photometric) {
+               case PHOTOMETRIC_MINISWHITE:
+               case PHOTOMETRIC_MINISBLACK:
+               case PHOTOMETRIC_PALETTE:
+                       if (td->td_planarconfig == PLANARCONFIG_CONTIG
+                           && td->td_samplesperpixel != 1
+                           && td->td_bitspersample < 8 ) {
+                               sprintf(emsg,
+                                   "Sorry, can not handle contiguous data with %s=%d, "
+                                   "and %s=%d and Bits/Sample=%d",
+                                   photoTag, photometric,
+                                   "Samples/pixel", td->td_samplesperpixel,
+                                   td->td_bitspersample);
+                               return (0);
+                       }
+                       /*
+                        * We should likely validate that any extra samples are either
+                        * to be ignored, or are alpha, and if alpha we should try to use
+                        * them.  But for now we won't bother with this.
+                       */
+                       break;
+               case PHOTOMETRIC_YCBCR:
+                       /*
+                        * TODO: if at all meaningful and useful, make more complete
+                        * support check here, or better still, refactor to let supporting
+                        * code decide whether there is support and what meaningfull
+                        * error to return
+                        */
+                       break;
+               case PHOTOMETRIC_RGB:
+                       if (colorchannels < 3) {
+                               sprintf(emsg, "Sorry, can not handle RGB image with %s=%d",
+                                   "Color channels", colorchannels);
+                               return (0);
+                       }
+                       break;
+               case PHOTOMETRIC_SEPARATED:
+                       {
+                               uint16 inkset;
+                               TIFFGetFieldDefaulted(tif, TIFFTAG_INKSET, &inkset);
+                               if (inkset != INKSET_CMYK) {
+                                       sprintf(emsg,
+                                           "Sorry, can not handle separated image with %s=%d",
+                                           "InkSet", inkset);
+                                       return 0;
+                               }
+                               if (td->td_samplesperpixel < 4) {
+                                       sprintf(emsg,
+                                           "Sorry, can not handle separated image with %s=%d",
+                                           "Samples/pixel", td->td_samplesperpixel);
+                                       return 0;
+                               }
+                               break;
+                       }
+               case PHOTOMETRIC_LOGL:
+                       if (td->td_compression != COMPRESSION_SGILOG) {
+                               sprintf(emsg, "Sorry, LogL data must have %s=%d",
+                                   "Compression", COMPRESSION_SGILOG);
+                               return (0);
+                       }
+                       break;
+               case PHOTOMETRIC_LOGLUV:
+                       if (td->td_compression != COMPRESSION_SGILOG &&
+                           td->td_compression != COMPRESSION_SGILOG24) {
+                               sprintf(emsg, "Sorry, LogLuv data must have %s=%d or %d",
+                                   "Compression", COMPRESSION_SGILOG, COMPRESSION_SGILOG24);
+                               return (0);
+                       }
+                       if (td->td_planarconfig != PLANARCONFIG_CONTIG) {
+                               sprintf(emsg, "Sorry, can not handle LogLuv images with %s=%d",
+                                   "Planarconfiguration", td->td_planarconfig);
+                               return (0);
+                       }
+                       break;
+               case PHOTOMETRIC_CIELAB:
+                       break;
+               default:
+                       sprintf(emsg, "Sorry, can not handle image with %s=%d",
+                           photoTag, photometric);
+                       return (0);
        }
-       break;
-    case PHOTOMETRIC_CIELAB:
-       break;
-    default:
-       sprintf(emsg, "Sorry, can not handle image with %s=%d",
-           photoTag, photometric);
-       return (0);
-    }
-    return (1);
+       return (1);
 }
 
 void
@@ -193,7 +202,6 @@ TIFFRGBAImageEnd(TIFFRGBAImage* img)
                _TIFFfree(img->ycbcr), img->ycbcr = NULL;
        if (img->cielab)
                _TIFFfree(img->cielab), img->cielab = NULL;
-
        if( img->redcmap ) {
                _TIFFfree( img->redcmap );
                _TIFFfree( img->greencmap );
@@ -215,234 +223,240 @@ isCCITTCompression(TIFF* tif)
 int
 TIFFRGBAImageBegin(TIFFRGBAImage* img, TIFF* tif, int stop, char emsg[1024])
 {
-    uint16* sampleinfo;
-    uint16 extrasamples;
-    uint16 planarconfig;
-    uint16 compress;
-    int colorchannels;
-    uint16 *red_orig, *green_orig, *blue_orig;
-    int n_color;
-
-    /* Initialize to normal values */
-    img->row_offset = 0;
-    img->col_offset = 0;
-    img->redcmap = NULL;
-    img->greencmap = NULL;
-    img->bluecmap = NULL;
-    img->req_orientation = ORIENTATION_BOTLEFT;            /* It is the default */
-    
-    img->tif = tif;
-    img->stoponerr = stop;
-    TIFFGetFieldDefaulted(tif, TIFFTAG_BITSPERSAMPLE, &img->bitspersample);
-    switch (img->bitspersample) {
-    case 1: case 2: case 4:
-    case 8: case 16:
-       break;
-    default:
-       sprintf(emsg, "Sorry, can not handle images with %d-bit samples",
-           img->bitspersample);
-       return (0);
-    }
-    img->alpha = 0;
-    TIFFGetFieldDefaulted(tif, TIFFTAG_SAMPLESPERPIXEL, &img->samplesperpixel);
-    TIFFGetFieldDefaulted(tif, TIFFTAG_EXTRASAMPLES,
-       &extrasamples, &sampleinfo);
-    if (extrasamples >= 1)
-    {
-       switch (sampleinfo[0]) {
-       case EXTRASAMPLE_UNSPECIFIED:   /* Workaround for some images without */
-               if (img->samplesperpixel > 3)   /* correct info about alpha channel */
-                       img->alpha = EXTRASAMPLE_ASSOCALPHA;
-               break;
-       case EXTRASAMPLE_ASSOCALPHA:    /* data is pre-multiplied */
-       case EXTRASAMPLE_UNASSALPHA:    /* data is not pre-multiplied */
-               img->alpha = sampleinfo[0];
-               break;
+       uint16* sampleinfo;
+       uint16 extrasamples;
+       uint16 planarconfig;
+       uint16 compress;
+       int colorchannels;
+       uint16 *red_orig, *green_orig, *blue_orig;
+       int n_color;
+
+       /* Initialize to normal values */
+       img->row_offset = 0;
+       img->col_offset = 0;
+       img->redcmap = NULL;
+       img->greencmap = NULL;
+       img->bluecmap = NULL;
+       img->req_orientation = ORIENTATION_BOTLEFT;     /* It is the default */
+
+       img->tif = tif;
+       img->stoponerr = stop;
+       TIFFGetFieldDefaulted(tif, TIFFTAG_BITSPERSAMPLE, &img->bitspersample);
+       switch (img->bitspersample) {
+               case 1:
+               case 2:
+               case 4:
+               case 8:
+               case 16:
+                       break;
+               default:
+                       sprintf(emsg, "Sorry, can not handle images with %d-bit samples",
+                           img->bitspersample);
+                       return (0);
+       }
+       img->alpha = 0;
+       TIFFGetFieldDefaulted(tif, TIFFTAG_SAMPLESPERPIXEL, &img->samplesperpixel);
+       TIFFGetFieldDefaulted(tif, TIFFTAG_EXTRASAMPLES,
+           &extrasamples, &sampleinfo);
+       if (extrasamples >= 1)
+       {
+               switch (sampleinfo[0]) {
+                       case EXTRASAMPLE_UNSPECIFIED:          /* Workaround for some images without */
+                               if (img->samplesperpixel > 3)  /* correct info about alpha channel */
+                                       img->alpha = EXTRASAMPLE_ASSOCALPHA;
+                               break;
+                       case EXTRASAMPLE_ASSOCALPHA:           /* data is pre-multiplied */
+                       case EXTRASAMPLE_UNASSALPHA:           /* data is not pre-multiplied */
+                               img->alpha = sampleinfo[0];
+                               break;
+               }
        }
-    }
 
 #ifdef DEFAULT_EXTRASAMPLE_AS_ALPHA
-    if( !TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &img->photometric))
-        img->photometric = PHOTOMETRIC_MINISWHITE;
-
-    if( extrasamples == 0 
-        && img->samplesperpixel == 4 
-        && img->photometric == PHOTOMETRIC_RGB )
-    {
-        img->alpha = EXTRASAMPLE_ASSOCALPHA;
-        extrasamples = 1;
-    }
-#endif
-
-    colorchannels = img->samplesperpixel - extrasamples;
-    TIFFGetFieldDefaulted(tif, TIFFTAG_COMPRESSION, &compress);
-    TIFFGetFieldDefaulted(tif, TIFFTAG_PLANARCONFIG, &planarconfig);
-    if (!TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &img->photometric)) {
-       switch (colorchannels) {
-       case 1:
-           if (isCCITTCompression(tif))
+       if( !TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &img->photometric))
                img->photometric = PHOTOMETRIC_MINISWHITE;
-           else
-               img->photometric = PHOTOMETRIC_MINISBLACK;
-           break;
-       case 3:
-           img->photometric = PHOTOMETRIC_RGB;
-           break;
-       default:
-           sprintf(emsg, "Missing needed %s tag", photoTag);
-           return (0);
-       }
-    }
-    switch (img->photometric) {
-    case PHOTOMETRIC_PALETTE:
-       if (!TIFFGetField(tif, TIFFTAG_COLORMAP,
-           &red_orig, &green_orig, &blue_orig)) {
-           sprintf(emsg, "Missing required \"Colormap\" tag");
-           return (0);
-       }
 
-        /* copy the colormaps so we can modify them */
-        n_color = (1L << img->bitspersample);
-        img->redcmap = (uint16 *) _TIFFmalloc(sizeof(uint16)*n_color);
-        img->greencmap = (uint16 *) _TIFFmalloc(sizeof(uint16)*n_color);
-        img->bluecmap = (uint16 *) _TIFFmalloc(sizeof(uint16)*n_color);
-        if( !img->redcmap || !img->greencmap || !img->bluecmap ) {
-           sprintf(emsg, "Out of memory for colormap copy");
-           return (0);
-        }
-
-        _TIFFmemcpy( img->redcmap, red_orig, n_color * 2 );
-        _TIFFmemcpy( img->greencmap, green_orig, n_color * 2 );
-        _TIFFmemcpy( img->bluecmap, blue_orig, n_color * 2 );
-        
-       /* fall thru... */
-    case PHOTOMETRIC_MINISWHITE:
-    case PHOTOMETRIC_MINISBLACK:
-       if (planarconfig == PLANARCONFIG_CONTIG 
-            && img->samplesperpixel != 1
-            && img->bitspersample < 8 ) {
-           sprintf(emsg,
-                    "Sorry, can not handle contiguous data with %s=%d, "
-                    "and %s=%d and Bits/Sample=%d",
-                    photoTag, img->photometric,
-                    "Samples/pixel", img->samplesperpixel,
-                    img->bitspersample);
-           return (0);
+       if( extrasamples == 0
+           && img->samplesperpixel == 4
+           && img->photometric == PHOTOMETRIC_RGB )
+       {
+               img->alpha = EXTRASAMPLE_ASSOCALPHA;
+               extrasamples = 1;
        }
-       break;
-    case PHOTOMETRIC_YCBCR:
-       if (planarconfig != PLANARCONFIG_CONTIG) {
-           sprintf(emsg, "Sorry, can not handle YCbCr images with %s=%d",
-               "Planarconfiguration", planarconfig);
-           return (0);
-       }
-       /* It would probably be nice to have a reality check here. */
-       if (planarconfig == PLANARCONFIG_CONTIG)
-           /* can rely on libjpeg to convert to RGB */
-           /* XXX should restore current state on exit */
-           switch (compress) {
-               case COMPRESSION_OJPEG:
-               case COMPRESSION_JPEG:
-                   TIFFSetField(tif, TIFFTAG_JPEGCOLORMODE, JPEGCOLORMODE_RGB);
-                   img->photometric = PHOTOMETRIC_RGB;
-                    break;
+#endif
 
-                default:
-                    /* do nothing */;
-                    break;
-           }
-       break;
-    case PHOTOMETRIC_RGB: 
-       if (colorchannels < 3) {
-           sprintf(emsg, "Sorry, can not handle RGB image with %s=%d",
-               "Color channels", colorchannels);
-           return (0);
-       }
-       break;
-    case PHOTOMETRIC_SEPARATED: {
-       uint16 inkset;
-       TIFFGetFieldDefaulted(tif, TIFFTAG_INKSET, &inkset);
-       if (inkset != INKSET_CMYK) {
-           sprintf(emsg, "Sorry, can not handle separated image with %s=%d",
-               "InkSet", inkset);
-           return (0);
-       }
-       if (img->samplesperpixel < 4) {
-           sprintf(emsg, "Sorry, can not handle separated image with %s=%d",
-               "Samples/pixel", img->samplesperpixel);
-           return (0);
-       }
-       break;
-    }
-    case PHOTOMETRIC_LOGL:
-       if (compress != COMPRESSION_SGILOG) {
-           sprintf(emsg, "Sorry, LogL data must have %s=%d",
-               "Compression", COMPRESSION_SGILOG);
-           return (0);
-       }
-       TIFFSetField(tif, TIFFTAG_SGILOGDATAFMT, SGILOGDATAFMT_8BIT);
-       img->photometric = PHOTOMETRIC_MINISBLACK;      /* little white lie */
-       img->bitspersample = 8;
-       break;
-    case PHOTOMETRIC_LOGLUV:
-       if (compress != COMPRESSION_SGILOG && compress != COMPRESSION_SGILOG24) {
-           sprintf(emsg, "Sorry, LogLuv data must have %s=%d or %d",
-               "Compression", COMPRESSION_SGILOG, COMPRESSION_SGILOG24);
-           return (0);
-       }
-       if (planarconfig != PLANARCONFIG_CONTIG) {
-           sprintf(emsg, "Sorry, can not handle LogLuv images with %s=%d",
-               "Planarconfiguration", planarconfig);
-           return (0);
+       colorchannels = img->samplesperpixel - extrasamples;
+       TIFFGetFieldDefaulted(tif, TIFFTAG_COMPRESSION, &compress);
+       TIFFGetFieldDefaulted(tif, TIFFTAG_PLANARCONFIG, &planarconfig);
+       if (!TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &img->photometric)) {
+               switch (colorchannels) {
+                       case 1:
+                               if (isCCITTCompression(tif))
+                                       img->photometric = PHOTOMETRIC_MINISWHITE;
+                               else
+                                       img->photometric = PHOTOMETRIC_MINISBLACK;
+                               break;
+                       case 3:
+                               img->photometric = PHOTOMETRIC_RGB;
+                               break;
+                       default:
+                               sprintf(emsg, "Missing needed %s tag", photoTag);
+                               return (0);
+               }
        }
-       TIFFSetField(tif, TIFFTAG_SGILOGDATAFMT, SGILOGDATAFMT_8BIT);
-       img->photometric = PHOTOMETRIC_RGB;             /* little white lie */
-       img->bitspersample = 8;
-       break;
-    case PHOTOMETRIC_CIELAB:
-       break;
-    default:
-       sprintf(emsg, "Sorry, can not handle image with %s=%d",
-           photoTag, img->photometric);
-       return (0);
-    }
-    img->Map = NULL;
-    img->BWmap = NULL;
-    img->PALmap = NULL;
-    img->ycbcr = NULL;
-    img->cielab = NULL;
-    TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &img->width);
-    TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &img->height);
-    TIFFGetFieldDefaulted(tif, TIFFTAG_ORIENTATION, &img->orientation);
-    img->isContig =
-       !(planarconfig == PLANARCONFIG_SEPARATE && colorchannels > 1);
-    if (img->isContig) {
-       img->get = TIFFIsTiled(tif) ? gtTileContig : gtStripContig;
-       if (!pickTileContigCase(img)) {
-               sprintf(emsg, "Sorry, can not handle image");
-               return 0;
+       switch (img->photometric) {
+               case PHOTOMETRIC_PALETTE:
+                       if (!TIFFGetField(tif, TIFFTAG_COLORMAP,
+                           &red_orig, &green_orig, &blue_orig)) {
+                               sprintf(emsg, "Missing required \"Colormap\" tag");
+                               return (0);
+                       }
+
+                       /* copy the colormaps so we can modify them */
+                       n_color = (1L << img->bitspersample);
+                       img->redcmap = (uint16 *) _TIFFmalloc(sizeof(uint16)*n_color);
+                       img->greencmap = (uint16 *) _TIFFmalloc(sizeof(uint16)*n_color);
+                       img->bluecmap = (uint16 *) _TIFFmalloc(sizeof(uint16)*n_color);
+                       if( !img->redcmap || !img->greencmap || !img->bluecmap ) {
+                               sprintf(emsg, "Out of memory for colormap copy");
+                               return (0);
+                       }
+
+                       _TIFFmemcpy( img->redcmap, red_orig, n_color * 2 );
+                       _TIFFmemcpy( img->greencmap, green_orig, n_color * 2 );
+                       _TIFFmemcpy( img->bluecmap, blue_orig, n_color * 2 );
+
+                       /* fall thru... */
+               case PHOTOMETRIC_MINISWHITE:
+               case PHOTOMETRIC_MINISBLACK:
+                       if (planarconfig == PLANARCONFIG_CONTIG
+                           && img->samplesperpixel != 1
+                           && img->bitspersample < 8 ) {
+                               sprintf(emsg,
+                                   "Sorry, can not handle contiguous data with %s=%d, "
+                                   "and %s=%d and Bits/Sample=%d",
+                                   photoTag, img->photometric,
+                                   "Samples/pixel", img->samplesperpixel,
+                                   img->bitspersample);
+                               return (0);
+                       }
+                       break;
+               case PHOTOMETRIC_YCBCR:
+                       /* It would probably be nice to have a reality check here. */
+                       if (planarconfig == PLANARCONFIG_CONTIG)
+                               /* can rely on libjpeg to convert to RGB */
+                               /* XXX should restore current state on exit */
+                               switch (compress) {
+                                       case COMPRESSION_JPEG:
+                                               /*
+                                                * TODO: when complete tests verify complete desubsampling
+                                                * and YCbCr handling, remove use of TIFFTAG_JPEGCOLORMODE in
+                                                * favor of tif_getimage.c native handling
+                                                */
+                                               TIFFSetField(tif, TIFFTAG_JPEGCOLORMODE, JPEGCOLORMODE_RGB);
+                                               img->photometric = PHOTOMETRIC_RGB;
+                                               break;
+                                       default:
+                                               /* do nothing */;
+                                               break;
+                               }
+                       /*
+                        * TODO: if at all meaningful and useful, make more complete
+                        * support check here, or better still, refactor to let supporting
+                        * code decide whether there is support and what meaningfull
+                        * error to return
+                        */
+                       break;
+               case PHOTOMETRIC_RGB:
+                       if (colorchannels < 3) {
+                               sprintf(emsg, "Sorry, can not handle RGB image with %s=%d",
+                                   "Color channels", colorchannels);
+                               return (0);
+                       }
+                       break;
+               case PHOTOMETRIC_SEPARATED:
+                       {
+                               uint16 inkset;
+                               TIFFGetFieldDefaulted(tif, TIFFTAG_INKSET, &inkset);
+                               if (inkset != INKSET_CMYK) {
+                                       sprintf(emsg, "Sorry, can not handle separated image with %s=%d",
+                                           "InkSet", inkset);
+                                       return (0);
+                               }
+                               if (img->samplesperpixel < 4) {
+                                       sprintf(emsg, "Sorry, can not handle separated image with %s=%d",
+                                           "Samples/pixel", img->samplesperpixel);
+                                       return (0);
+                               }
+                       }
+                       break;
+               case PHOTOMETRIC_LOGL:
+                       if (compress != COMPRESSION_SGILOG) {
+                               sprintf(emsg, "Sorry, LogL data must have %s=%d",
+                                   "Compression", COMPRESSION_SGILOG);
+                               return (0);
+                       }
+                       TIFFSetField(tif, TIFFTAG_SGILOGDATAFMT, SGILOGDATAFMT_8BIT);
+                       img->photometric = PHOTOMETRIC_MINISBLACK;      /* little white lie */
+                       img->bitspersample = 8;
+                       break;
+               case PHOTOMETRIC_LOGLUV:
+                       if (compress != COMPRESSION_SGILOG && compress != COMPRESSION_SGILOG24) {
+                               sprintf(emsg, "Sorry, LogLuv data must have %s=%d or %d",
+                                   "Compression", COMPRESSION_SGILOG, COMPRESSION_SGILOG24);
+                               return (0);
+                       }
+                       if (planarconfig != PLANARCONFIG_CONTIG) {
+                               sprintf(emsg, "Sorry, can not handle LogLuv images with %s=%d",
+                                   "Planarconfiguration", planarconfig);
+                               return (0);
+                       }
+                       TIFFSetField(tif, TIFFTAG_SGILOGDATAFMT, SGILOGDATAFMT_8BIT);
+                       img->photometric = PHOTOMETRIC_RGB;             /* little white lie */
+                       img->bitspersample = 8;
+                       break;
+               case PHOTOMETRIC_CIELAB:
+                       break;
+               default:
+                       sprintf(emsg, "Sorry, can not handle image with %s=%d",
+                           photoTag, img->photometric);
+                       return (0);
        }
-    } else {
-       img->get = TIFFIsTiled(tif) ? gtTileSeparate : gtStripSeparate;
-       if (!pickTileSeparateCase(img)) {
-               sprintf(emsg, "Sorry, can not handle image");
-               return 0;
+       img->Map = NULL;
+       img->BWmap = NULL;
+       img->PALmap = NULL;
+       img->ycbcr = NULL;
+       img->cielab = NULL;
+       TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &img->width);
+       TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &img->height);
+       TIFFGetFieldDefaulted(tif, TIFFTAG_ORIENTATION, &img->orientation);
+       img->isContig =
+           !(planarconfig == PLANARCONFIG_SEPARATE && colorchannels > 1);
+       if (img->isContig) {
+               if (!PickContigCase(img)) {
+                       sprintf(emsg, "Sorry, can not handle image");
+                       return 0;
+               }
+       } else {
+               if (!PickSeparateCase(img)) {
+                       sprintf(emsg, "Sorry, can not handle image");
+                       return 0;
+               }
        }
-    }
-    return 1;
+       return 1;
 }
 
 int
 TIFFRGBAImageGet(TIFFRGBAImage* img, uint32* raster, uint32 w, uint32 h)
 {
     if (img->get == NULL) {
-       TIFFError(TIFFFileName(img->tif), "No \"get\" routine setup");
-       return (0);
-    }
-    if (img->put.any == NULL) {
-       TIFFError(TIFFFileName(img->tif),
-           "No \"put\" routine setupl; probably can not handle image format");
-       return (0);
+               TIFFErrorExt(img->tif->tif_clientdata, TIFFFileName(img->tif), "No \"get\" routine setup");
+               return (0);
+       }
+       if (img->put.any == NULL) {
+               TIFFErrorExt(img->tif->tif_clientdata, TIFFFileName(img->tif),
+               "No \"put\" routine setupl; probably can not handle image format");
+               return (0);
     }
     return (*img->get)(img, raster, w, h);
 }
@@ -460,16 +474,15 @@ TIFFReadRGBAImageOriented(TIFF* tif,
     TIFFRGBAImage img;
     int ok;
 
-    if (TIFFRGBAImageOK(tif, emsg) &&
-       TIFFRGBAImageBegin(&img, tif, stop, emsg)) {
-       img.req_orientation = orientation;
-       /* XXX verify rwidth and rheight against width and height */
-       ok = TIFFRGBAImageGet(&img, raster+(rheight-img.height)*rwidth,
-           rwidth, img.height);
-       TIFFRGBAImageEnd(&img);
-    } else {
-       TIFFError(TIFFFileName(tif), emsg);
-       ok = 0;
+       if (TIFFRGBAImageOK(tif, emsg) && TIFFRGBAImageBegin(&img, tif, stop, emsg)) {
+               img.req_orientation = orientation;
+               /* XXX verify rwidth and rheight against width and height */
+               ok = TIFFRGBAImageGet(&img, raster+(rheight-img.height)*rwidth,
+                       rwidth, img.height);
+               TIFFRGBAImageEnd(&img);
+       } else {
+               TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "%s", emsg);
+               ok = 0;
     }
     return (ok);
 }
@@ -568,8 +581,8 @@ gtTileContig(TIFFRGBAImage* img, uint32* raster, uint32 w, uint32 h)
 
     buf = (unsigned char*) _TIFFmalloc(TIFFTileSize(tif));
     if (buf == 0) {
-       TIFFError(TIFFFileName(tif), "No space for tile buffer");
-       return (0);
+               TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "No space for tile buffer");
+               return (0);
     }
     _TIFFmemset(buf, 0, TIFFTileSize(tif));
     TIFFGetField(tif, TIFFTAG_TILEWIDTH, &tw);
@@ -649,119 +662,120 @@ gtTileContig(TIFFRGBAImage* img, uint32* raster, uint32 w, uint32 h)
 static int
 gtTileSeparate(TIFFRGBAImage* img, uint32* raster, uint32 w, uint32 h)
 {
-    TIFF* tif = img->tif;
-    tileSeparateRoutine put = img->put.separate;
-    uint32 col, row, y, rowstoread;
-    uint32 pos;
-    uint32 tw, th;
-    unsigned char* buf;
-    unsigned char* r;
-    unsigned char* g;
-    unsigned char* b;
-    unsigned char* a;
-    tsize_t tilesize;
-    int32 fromskew, toskew;
-    int alpha = img->alpha;
-    uint32 nrow;
-    int ret = 1, flip;
-
-    tilesize = TIFFTileSize(tif);
-    buf = (unsigned char*) _TIFFmalloc(4*tilesize);
-    if (buf == 0) {
-       TIFFError(TIFFFileName(tif), "No space for tile buffer");
-       return (0);
-    }
-    _TIFFmemset(buf, 0, 4*tilesize);
-    r = buf;
-    g = r + tilesize;
-    b = g + tilesize;
-    a = b + tilesize;
-    if (!alpha)
-       _TIFFmemset(a, 0xff, tilesize);
-    TIFFGetField(tif, TIFFTAG_TILEWIDTH, &tw);
-    TIFFGetField(tif, TIFFTAG_TILELENGTH, &th);
-
-    flip = setorientation(img);
-    if (flip & FLIP_VERTICALLY) {
-           y = h - 1;
-           toskew = -(int32)(tw + w);
-    }
-    else {
-           y = 0;
-           toskew = -(int32)(tw - w);
-    }
-
-    for (row = 0; row < h; row += nrow) 
-    {
-        rowstoread = th - (row + img->row_offset) % th;
-       nrow = (row + rowstoread > h ? h - row : rowstoread);
-        for (col = 0; col < w; col += tw) 
-        {
-            if (TIFFReadTile(tif, r, col+img->col_offset,
-                             row+img->row_offset,0,0) < 0 && img->stoponerr)
-            {
-                ret = 0;
-                break;
-            }
-            if (TIFFReadTile(tif, g, col+img->col_offset,
-                             row+img->row_offset,0,1) < 0 && img->stoponerr)
-            {
-                ret = 0;
-                break;
-            }
-            if (TIFFReadTile(tif, b, col+img->col_offset,
-                             row+img->row_offset,0,2) < 0 && img->stoponerr)
-            {
-                ret = 0;
-                break;
-            }
-            if (alpha && TIFFReadTile(tif,a,col+img->col_offset,
-                                      row+img->row_offset,0,3) < 0 && img->stoponerr)
-            {
-                ret = 0;
-                break;
-            }
+       TIFF* tif = img->tif;
+       tileSeparateRoutine put = img->put.separate;
+       uint32 col, row, y, rowstoread;
+       uint32 pos;
+       uint32 tw, th;
+       unsigned char* buf;
+       unsigned char* p0;
+       unsigned char* p1;
+       unsigned char* p2;
+       unsigned char* pa;
+       tsize_t tilesize;
+       int32 fromskew, toskew;
+       int alpha = img->alpha;
+       uint32 nrow;
+       int ret = 1, flip;
+
+       tilesize = TIFFTileSize(tif);
+       buf = (unsigned char*) _TIFFmalloc((alpha?4:3)*tilesize);
+       if (buf == 0) {
+               TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "No space for tile buffer");
+               return (0);
+       }
+       _TIFFmemset(buf, 0, (alpha?4:3)*tilesize);
+       p0 = buf;
+       p1 = p0 + tilesize;
+       p2 = p1 + tilesize;
+       pa = (alpha?(p2+tilesize):NULL);
+       TIFFGetField(tif, TIFFTAG_TILEWIDTH, &tw);
+       TIFFGetField(tif, TIFFTAG_TILELENGTH, &th);
+
+       flip = setorientation(img);
+       if (flip & FLIP_VERTICALLY) {
+               y = h - 1;
+               toskew = -(int32)(tw + w);
+       }
+       else {
+               y = 0;
+               toskew = -(int32)(tw - w);
+       }
 
-            pos = ((row+img->row_offset) % th) * TIFFTileRowSize(tif);
+       for (row = 0; row < h; row += nrow)
+       {
+               rowstoread = th - (row + img->row_offset) % th;
+               nrow = (row + rowstoread > h ? h - row : rowstoread);
+               for (col = 0; col < w; col += tw)
+               {
+                       if (TIFFReadTile(tif, p0, col+img->col_offset,
+                           row+img->row_offset,0,0) < 0 && img->stoponerr)
+                       {
+                               ret = 0;
+                               break;
+                       }
+                       if (TIFFReadTile(tif, p1, col+img->col_offset,
+                           row+img->row_offset,0,1) < 0 && img->stoponerr)
+                       {
+                               ret = 0;
+                               break;
+                       }
+                       if (TIFFReadTile(tif, p2, col+img->col_offset,
+                           row+img->row_offset,0,2) < 0 && img->stoponerr)
+                       {
+                               ret = 0;
+                               break;
+                       }
+                       if (alpha)
+                       {
+                               if (TIFFReadTile(tif,pa,col+img->col_offset,
+                                   row+img->row_offset,0,3) < 0 && img->stoponerr)
+                               {
+                                       ret = 0;
+                                       break;
+                               }
+                       }
+
+                       pos = ((row+img->row_offset) % th) * TIFFTileRowSize(tif);
+
+                       if (col + tw > w)
+                       {
+                               /*
+                                * Tile is clipped horizontally.  Calculate
+                                * visible portion and skewing factors.
+                                */
+                               uint32 npix = w - col;
+                               fromskew = tw - npix;
+                               (*put)(img, raster+y*w+col, col, y,
+                                   npix, nrow, fromskew, toskew + fromskew,
+                                   p0 + pos, p1 + pos, p2 + pos, (alpha?(pa+pos):NULL));
+                       } else {
+                               (*put)(img, raster+y*w+col, col, y,
+                                   tw, nrow, 0, toskew, p0 + pos, p1 + pos, p2 + pos, (alpha?(pa+pos):NULL));
+                       }
+               }
 
-            if (col + tw > w) 
-            {
-                /*
-                 * Tile is clipped horizontally.  Calculate
-                 * visible portion and skewing factors.
-                 */
-                uint32 npix = w - col;
-                fromskew = tw - npix;
-                (*put)(img, raster+y*w+col, col, y,
-                       npix, nrow, fromskew, toskew + fromskew, 
-                       r + pos, g + pos, b + pos, a + pos);
-            } else {
-                (*put)(img, raster+y*w+col, col, y,
-                       tw, nrow, 0, toskew, r + pos, g + pos, b + pos, a + pos);
-            }
-        }
+               y += (flip & FLIP_VERTICALLY ?-(int32) nrow : (int32) nrow);
+       }
 
-        y += (flip & FLIP_VERTICALLY ?-(int32) nrow : (int32) nrow);
-    }
+       if (flip & FLIP_HORIZONTALLY) {
+               uint32 line;
 
-    if (flip & FLIP_HORIZONTALLY) {
-           uint32 line;
+               for (line = 0; line < h; line++) {
+                       uint32 *left = raster + (line * w);
+                       uint32 *right = left + w - 1;
 
-           for (line = 0; line < h; line++) {
-                   uint32 *left = raster + (line * w);
-                   uint32 *right = left + w - 1;
-                   
-                   while ( left < right ) {
-                           uint32 temp = *left;
-                           *left = *right;
-                           *right = temp;
-                           left++, right--;
-                   }
-           }
-    }
+                       while ( left < right ) {
+                               uint32 temp = *left;
+                               *left = *right;
+                               *right = temp;
+                               left++, right--;
+                       }
+               }
+       }
 
-    _TIFFfree(buf);
-    return (ret);
+       _TIFFfree(buf);
+       return (ret);
 }
 
 /*
@@ -773,73 +787,78 @@ gtTileSeparate(TIFFRGBAImage* img, uint32* raster, uint32 w, uint32 h)
 static int
 gtStripContig(TIFFRGBAImage* img, uint32* raster, uint32 w, uint32 h)
 {
-    TIFF* tif = img->tif;
-    tileContigRoutine put = img->put.contig;
-    uint32 row, y, nrow, rowstoread;
-    uint32 pos;
-    unsigned char* buf;
-    uint32 rowsperstrip;
-    uint32 imagewidth = img->width;
-    tsize_t scanline;
-    int32 fromskew, toskew;
-    int ret = 1, flip;
-
-    buf = (unsigned char*) _TIFFmalloc(TIFFStripSize(tif));
-    if (buf == 0) {
-       TIFFError(TIFFFileName(tif), "No space for strip buffer");
-       return (0);
-    }
-    _TIFFmemset(buf, 0, TIFFStripSize(tif));
+       TIFF* tif = img->tif;
+       tileContigRoutine put = img->put.contig;
+       uint32 row, y, nrow, nrowsub, rowstoread;
+       uint32 pos;
+       unsigned char* buf;
+       uint32 rowsperstrip;
+       uint16 subsamplinghor,subsamplingver;
+       uint32 imagewidth = img->width;
+       tsize_t scanline;
+       int32 fromskew, toskew;
+       int ret = 1, flip;
+
+       buf = (unsigned char*) _TIFFmalloc(TIFFStripSize(tif));
+       if (buf == 0) {
+               TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "No space for strip buffer");
+               return (0);
+       }
+       _TIFFmemset(buf, 0, TIFFStripSize(tif));
+
+       flip = setorientation(img);
+       if (flip & FLIP_VERTICALLY) {
+               y = h - 1;
+               toskew = -(int32)(w + w);
+       } else {
+               y = 0;
+               toskew = -(int32)(w - w);
+       }
 
-    flip = setorientation(img);
-    if (flip & FLIP_VERTICALLY) {
-           y = h - 1;
-           toskew = -(int32)(w + w);
-    } else {
-           y = 0;
-           toskew = -(int32)(w - w);
-    }
+       TIFFGetFieldDefaulted(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);
+       TIFFGetFieldDefaulted(tif, TIFFTAG_YCBCRSUBSAMPLING, &subsamplinghor, &subsamplingver);
+       scanline = TIFFNewScanlineSize(tif);
+       fromskew = (w < imagewidth ? imagewidth - w : 0);
+       for (row = 0; row < h; row += nrow)
+       {
+               rowstoread = rowsperstrip - (row + img->row_offset) % rowsperstrip;
+               nrow = (row + rowstoread > h ? h - row : rowstoread);
+               nrowsub = nrow;
+               if ((nrowsub%subsamplingver)!=0)
+                       nrowsub+=subsamplingver-nrowsub%subsamplingver;
+               if (TIFFReadEncodedStrip(tif,
+                   TIFFComputeStrip(tif,row+img->row_offset, 0),
+                   buf,
+                   ((row + img->row_offset)%rowsperstrip + nrowsub) * scanline) < 0
+                   && img->stoponerr)
+               {
+                       ret = 0;
+                       break;
+               }
 
-    TIFFGetFieldDefaulted(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);
-    scanline = TIFFScanlineSize(tif);
-    fromskew = (w < imagewidth ? imagewidth - w : 0);
-    for (row = 0; row < h; row += nrow) 
-    {
-        rowstoread = rowsperstrip - (row + img->row_offset) % rowsperstrip;
-        nrow = (row + rowstoread > h ? h - row : rowstoread);
-        if (TIFFReadEncodedStrip(tif,
-                                 TIFFComputeStrip(tif,row+img->row_offset, 0),
-                                 buf, 
-                                 ((row + img->row_offset)%rowsperstrip + nrow) * scanline) < 0
-            && img->stoponerr)
-        {
-            ret = 0;
-            break;
-        }
+               pos = ((row + img->row_offset) % rowsperstrip) * scanline;
+               (*put)(img, raster+y*w, 0, y, w, nrow, fromskew, toskew, buf + pos);
+               y += (flip & FLIP_VERTICALLY ? -(int32) nrow : (int32) nrow);
+       }
 
-        pos = ((row + img->row_offset) % rowsperstrip) * scanline;
-        (*put)(img, raster+y*w, 0, y, w, nrow, fromskew, toskew, buf + pos);
-        y += (flip & FLIP_VERTICALLY ? -(int32) nrow : (int32) nrow);
-    }
+       if (flip & FLIP_HORIZONTALLY) {
+               uint32 line;
 
-    if (flip & FLIP_HORIZONTALLY) {
-           uint32 line;
+               for (line = 0; line < h; line++) {
+                       uint32 *left = raster + (line * w);
+                       uint32 *right = left + w - 1;
 
-           for (line = 0; line < h; line++) {
-                   uint32 *left = raster + (line * w);
-                   uint32 *right = left + w - 1;
-                   
-                   while ( left < right ) {
-                           uint32 temp = *left;
-                           *left = *right;
-                           *right = temp;
-                           left++, right--;
-                   }
-           }
-    }
+                       while ( left < right ) {
+                               uint32 temp = *left;
+                               *left = *right;
+                               *right = temp;
+                               left++, right--;
+                       }
+               }
+       }
 
-    _TIFFfree(buf);
-    return (ret);
+       _TIFFfree(buf);
+       return (ret);
 }
 
 /*
@@ -851,105 +870,105 @@ gtStripContig(TIFFRGBAImage* img, uint32* raster, uint32 w, uint32 h)
 static int
 gtStripSeparate(TIFFRGBAImage* img, uint32* raster, uint32 w, uint32 h)
 {
-    TIFF* tif = img->tif;
-    tileSeparateRoutine put = img->put.separate;
-    unsigned char *buf;
-    unsigned char *r, *g, *b, *a;
-    uint32 row, y, nrow, rowstoread;
-    uint32 pos;
-    tsize_t scanline;
-    uint32 rowsperstrip, offset_row;
-    uint32 imagewidth = img->width;
-    tsize_t stripsize;
-    int32 fromskew, toskew;
-    int alpha = img->alpha;
-    int        ret = 1, flip;
-
-    stripsize = TIFFStripSize(tif);
-    r = buf = (unsigned char *)_TIFFmalloc(4*stripsize);
-    if (buf == 0) {
-       TIFFError(TIFFFileName(tif), "No space for tile buffer");
-       return (0);
-    }
-    _TIFFmemset(buf, 0, 4*stripsize);
-    g = r + stripsize;
-    b = g + stripsize;
-    a = b + stripsize;
-    if (!alpha)
-       _TIFFmemset(a, 0xff, stripsize);
+       TIFF* tif = img->tif;
+       tileSeparateRoutine put = img->put.separate;
+       unsigned char *buf;
+       unsigned char *p0, *p1, *p2, *pa;
+       uint32 row, y, nrow, rowstoread;
+       uint32 pos;
+       tsize_t scanline;
+       uint32 rowsperstrip, offset_row;
+       uint32 imagewidth = img->width;
+       tsize_t stripsize;
+       int32 fromskew, toskew;
+       int alpha = img->alpha;
+       int ret = 1, flip;
+
+       stripsize = TIFFStripSize(tif);
+       p0 = buf = (unsigned char *)_TIFFmalloc((alpha?4:3)*stripsize);
+       if (buf == 0) {
+               TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "No space for tile buffer");
+               return (0);
+       }
+       _TIFFmemset(buf, 0, (alpha?4:3)*stripsize);
+       p1 = p0 + stripsize;
+       p2 = p1 + stripsize;
+       pa = (alpha?(p2+stripsize):NULL);
+
+       flip = setorientation(img);
+       if (flip & FLIP_VERTICALLY) {
+               y = h - 1;
+               toskew = -(int32)(w + w);
+       }
+       else {
+               y = 0;
+               toskew = -(int32)(w - w);
+       }
 
-    flip = setorientation(img);
-    if (flip & FLIP_VERTICALLY) {
-           y = h - 1;
-           toskew = -(int32)(w + w);
-    }
-    else {
-           y = 0;
-           toskew = -(int32)(w - w);
-    }
+       TIFFGetFieldDefaulted(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);
+       scanline = TIFFScanlineSize(tif);
+       fromskew = (w < imagewidth ? imagewidth - w : 0);
+       for (row = 0; row < h; row += nrow)
+       {
+               rowstoread = rowsperstrip - (row + img->row_offset) % rowsperstrip;
+               nrow = (row + rowstoread > h ? h - row : rowstoread);
+               offset_row = row + img->row_offset;
+               if (TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, offset_row, 0),
+                   p0, ((row + img->row_offset)%rowsperstrip + nrow) * scanline) < 0
+                   && img->stoponerr)
+               {
+                       ret = 0;
+                       break;
+               }
+               if (TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, offset_row, 1),
+                   p1, ((row + img->row_offset)%rowsperstrip + nrow) * scanline) < 0
+                   && img->stoponerr)
+               {
+                       ret = 0;
+                       break;
+               }
+               if (TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, offset_row, 2),
+                   p2, ((row + img->row_offset)%rowsperstrip + nrow) * scanline) < 0
+                   && img->stoponerr)
+               {
+                       ret = 0;
+                       break;
+               }
+               if (alpha)
+               {
+                       if (TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, offset_row, 3),
+                           pa, ((row + img->row_offset)%rowsperstrip + nrow) * scanline) < 0
+                           && img->stoponerr)
+                       {
+                               ret = 0;
+                               break;
+                       }
+               }
 
-    TIFFGetFieldDefaulted(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);
-    scanline = TIFFScanlineSize(tif);
-    fromskew = (w < imagewidth ? imagewidth - w : 0);
-    for (row = 0; row < h; row += nrow) 
-    {
-        rowstoread = rowsperstrip - (row + img->row_offset) % rowsperstrip;            
-        nrow = (row + rowstoread > h ? h - row : rowstoread);
-        offset_row = row + img->row_offset;
-       if (TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, offset_row, 0),
-                                 r, ((row + img->row_offset)%rowsperstrip + nrow) * scanline) < 0 
-            && img->stoponerr)
-        {
-            ret = 0;
-            break;
-        }
-        if (TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, offset_row, 1),
-                                 g, ((row + img->row_offset)%rowsperstrip + nrow) * scanline) < 0 
-            && img->stoponerr)
-        {
-            ret = 0;
-            break;
-        }
-        if (TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, offset_row, 2),
-                                 b, ((row + img->row_offset)%rowsperstrip + nrow) * scanline) < 0 
-            && img->stoponerr)
-        {
-            ret = 0;
-            break;
-        }
-        if (alpha &&
-            (TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, offset_row, 3),
-                                  a, ((row + img->row_offset)%rowsperstrip + nrow) * scanline) < 0 
-             && img->stoponerr))
-        {
-            ret = 0;
-            break;
-        }
+               pos = ((row + img->row_offset) % rowsperstrip) * scanline;
+               (*put)(img, raster+y*w, 0, y, w, nrow, fromskew, toskew, p0 + pos, p1 + pos,
+                   p2 + pos, (alpha?(pa+pos):NULL));
+               y += (flip & FLIP_VERTICALLY ? -(int32) nrow : (int32) nrow);
+       }
 
-        pos = ((row + img->row_offset) % rowsperstrip) * scanline;
-        (*put)(img, raster+y*w, 0, y, w, nrow, fromskew, toskew, r + pos, g + pos, 
-               b + pos, a + pos);
-        y += (flip & FLIP_VERTICALLY ? -(int32) nrow : (int32) nrow);
-    }
+       if (flip & FLIP_HORIZONTALLY) {
+               uint32 line;
 
-    if (flip & FLIP_HORIZONTALLY) {
-           uint32 line;
+               for (line = 0; line < h; line++) {
+                       uint32 *left = raster + (line * w);
+                       uint32 *right = left + w - 1;
 
-           for (line = 0; line < h; line++) {
-                   uint32 *left = raster + (line * w);
-                   uint32 *right = left + w - 1;
-                   
-                   while ( left < right ) {
-                           uint32 temp = *left;
-                           *left = *right;
-                           *right = temp;
-                           left++, right--;
-                   }
-           }
-    }
+                       while ( left < right ) {
+                               uint32 temp = *left;
+                               *left = *right;
+                               *right = temp;
+                               left++, right--;
+                       }
+               }
+       }
 
-    _TIFFfree(buf);
-    return (ret);
+       _TIFFfree(buf);
+       return (ret);
 }
 
 /*
@@ -958,9 +977,9 @@ gtStripSeparate(TIFFRGBAImage* img, uint32* raster, uint32 w, uint32 h)
  * ABGR pixels (i.e. suitable for passing to lrecwrite.)
  *
  * The routines have been created according to the most
- * important cases and optimized.  pickTileContigCase and
- * pickTileSeparateCase analyze the parameters and select
- * the appropriate "put" routine to use.
+ * important cases and optimized.  PickContigCase and
+ * PickSeparateCase analyze the parameters and select
+ * the appropriate "get" and "put" routine to use.
  */
 #define        REPEAT8(op)     REPEAT4(op); REPEAT4(op)
 #define        REPEAT4(op)     REPEAT2(op); REPEAT2(op)
@@ -1218,26 +1237,6 @@ DECLAREContigPutFunc(putRGBcontig8bittile)
 }
 
 /*
- * 8-bit packed samples, w/ Map => RGB
- */
-DECLAREContigPutFunc(putRGBcontig8bitMaptile)
-{
-    TIFFRGBValue* Map = img->Map;
-    int samplesperpixel = img->samplesperpixel;
-
-    (void) y;
-    fromskew *= samplesperpixel;
-    while (h-- > 0) {
-       for (x = w; x-- > 0;) {
-           *cp++ = PACK(Map[pp[0]], Map[pp[1]], Map[pp[2]]);
-           pp += samplesperpixel;
-       }
-       pp += fromskew;
-       cp += toskew;
-    }
-}
-
-/*
  * 8-bit packed samples => RGBA w/ associated alpha
  * (known to have Map == NULL)
  */
@@ -1262,23 +1261,22 @@ DECLAREContigPutFunc(putRGBAAcontig8bittile)
  */
 DECLAREContigPutFunc(putRGBUAcontig8bittile)
 {
-    int samplesperpixel = img->samplesperpixel;
-
-    (void) y;
-    fromskew *= samplesperpixel;
-    while (h-- > 0) {
-       uint32 r, g, b, a;
-       for (x = w; x-- > 0;) {
-           a = pp[3];
-           r = (pp[0] * a) / 255;
-           g = (pp[1] * a) / 255;
-           b = (pp[2] * a) / 255;
-           *cp++ = PACK4(r,g,b,a);
-           pp += samplesperpixel;
+       int samplesperpixel = img->samplesperpixel;
+       (void) y;
+       fromskew *= samplesperpixel;
+       while (h-- > 0) {
+               uint32 r, g, b, a;
+               for (x = w; x-- > 0;) {
+                       a = pp[3];
+                        r = (a*pp[0] + 127) / 255;
+                        g = (a*pp[1] + 127) / 255;
+                        b = (a*pp[2] + 127) / 255;
+                       *cp++ = PACK4(r,g,b,a);
+                       pp += samplesperpixel;
+               }
+               cp += toskew;
+               pp += fromskew;
        }
-       cp += toskew;
-       pp += fromskew;
-    }
 }
 
 /*
@@ -1286,19 +1284,18 @@ DECLAREContigPutFunc(putRGBUAcontig8bittile)
  */
 DECLAREContigPutFunc(putRGBcontig16bittile)
 {
-    int samplesperpixel = img->samplesperpixel;
-    uint16 *wp = (uint16 *)pp;
-
-    (void) y;
-    fromskew *= samplesperpixel;
-    while (h-- > 0) {
-       for (x = w; x-- > 0;) {
-           *cp++ = PACKW(wp[0], wp[1], wp[2]);
-           wp += samplesperpixel;
+       int samplesperpixel = img->samplesperpixel;
+       uint16 *wp = (uint16 *)pp;
+       (void) y;
+       fromskew *= samplesperpixel;
+       while (h-- > 0) {
+               for (x = w; x-- > 0;) {
+                    *cp++ = PACKW(wp[0],wp[1],wp[2]);
+                    wp += samplesperpixel;
+               }
+               cp += toskew;
+               wp += fromskew;
        }
-       cp += toskew;
-       wp += fromskew;
-    }
 }
 
 /*
@@ -1307,19 +1304,18 @@ DECLAREContigPutFunc(putRGBcontig16bittile)
  */
 DECLAREContigPutFunc(putRGBAAcontig16bittile)
 {
-    int samplesperpixel = img->samplesperpixel;
-    uint16 *wp = (uint16 *)pp;
-
-    (void) y;
-    fromskew *= samplesperpixel;
-    while (h-- > 0) {
-       for (x = w; x-- > 0;) {
-           *cp++ = PACKW4(wp[0], wp[1], wp[2], wp[3]);
-           wp += samplesperpixel;
+       int samplesperpixel = img->samplesperpixel;
+       uint16 *wp = (uint16 *)pp;
+       (void) y;
+       fromskew *= samplesperpixel;
+       while (h-- > 0) {
+               for (x = w; x-- > 0;) {
+                    *cp++ = PACKW4(wp[0],wp[1],wp[2],wp[3]);
+                    wp += samplesperpixel;
+               }
+               cp += toskew;
+               wp += fromskew;
        }
-       cp += toskew;
-       wp += fromskew;
-    }
 }
 
 /*
@@ -1328,32 +1324,23 @@ DECLAREContigPutFunc(putRGBAAcontig16bittile)
  */
 DECLAREContigPutFunc(putRGBUAcontig16bittile)
 {
-    int samplesperpixel = img->samplesperpixel;
-    uint16 *wp = (uint16 *)pp;
-
-    (void) y;
-    fromskew *= samplesperpixel;
-    while (h-- > 0) {
-       uint32 r,g,b,a;
-       /*
-        * We shift alpha down four bits just in case unsigned
-        * arithmetic doesn't handle the full range.
-        * We still have plenty of accuracy, since the output is 8 bits.
-        * So we have (r * 0xffff) * (a * 0xfff)) = r*a * (0xffff*0xfff)
-        * Since we want r*a * 0xff for eight bit output,
-        * we divide by (0xffff * 0xfff) / 0xff == 0x10eff.
-        */
-       for (x = w; x-- > 0;) {
-           a = wp[3] >> 4; 
-           r = (wp[0] * a) / 0x10eff;
-           g = (wp[1] * a) / 0x10eff;
-           b = (wp[2] * a) / 0x10eff;
-           *cp++ = PACK4(r,g,b,a);
-           wp += samplesperpixel;
+       int samplesperpixel = img->samplesperpixel;
+       uint16 *wp = (uint16 *)pp;
+       (void) y;
+       fromskew *= samplesperpixel;
+       while (h-- > 0) {
+               uint32 r,g,b,a;
+               for (x = w; x-- > 0;) {
+                    a = W2B(wp[3]);
+                    r = (a*W2B(wp[0]) + 127) / 255;
+                    g = (a*W2B(wp[1]) + 127) / 255;
+                    b = (a*W2B(wp[2]) + 127) / 255;
+                    *cp++ = PACK4(r,g,b,a);
+                    wp += samplesperpixel;
+               }
+               cp += toskew;
+               wp += fromskew;
        }
-       cp += toskew;
-       wp += fromskew;
-    }
 }
 
 /*
@@ -1432,32 +1419,16 @@ DECLARESepPutFunc(putRGBseparate8bittile)
 }
 
 /*
- * 8-bit unpacked samples => RGB
- */
-DECLARESepPutFunc(putRGBseparate8bitMaptile)
-{
-    TIFFRGBValue* Map = img->Map;
-
-    (void) y; (void) a;
-    while (h-- > 0) {
-       for (x = w; x > 0; x--)
-           *cp++ = PACK(Map[*r++], Map[*g++], Map[*b++]);
-       SKEW(r, g, b, fromskew);
-       cp += toskew;
-    }
-}
-
-/*
  * 8-bit unpacked samples => RGBA w/ associated alpha
  */
 DECLARESepPutFunc(putRGBAAseparate8bittile)
 {
-    (void) img; (void) x; (void) y;
-    while (h-- > 0) {
-       UNROLL8(w, NOP, *cp++ = PACK4(*r++, *g++, *b++, *a++));
-       SKEW4(r, g, b, a, fromskew);
-       cp += toskew;
-    }
+       (void) img; (void) x; (void) y;
+       while (h-- > 0) {
+               UNROLL8(w, NOP, *cp++ = PACK4(*r++, *g++, *b++, *a++));
+               SKEW4(r, g, b, a, fromskew);
+               cp += toskew;
+       }
 }
 
 /*
@@ -1465,19 +1436,19 @@ DECLARESepPutFunc(putRGBAAseparate8bittile)
  */
 DECLARESepPutFunc(putRGBUAseparate8bittile)
 {
-    (void) img; (void) y;
-    while (h-- > 0) {
-       uint32 rv, gv, bv, av;
-       for (x = w; x-- > 0;) {
-           av = *a++;
-           rv = (*r++ * av) / 255;
-           gv = (*g++ * av) / 255;
-           bv = (*b++ * av) / 255;
-           *cp++ = PACK4(rv,gv,bv,av);
+       (void) img; (void) y;
+       while (h-- > 0) {
+               uint32 rv, gv, bv, av;
+               for (x = w; x-- > 0;) {
+                       av = *a++;
+                        rv = (av* *r++ + 127) / 255;
+                        gv = (av* *g++ + 127) / 255;
+                        bv = (av* *b++ + 127) / 255;
+                       *cp++ = PACK4(rv,gv,bv,av);
+               }
+               SKEW4(r, g, b, a, fromskew);
+               cp += toskew;
        }
-       SKEW4(r, g, b, a, fromskew);
-       cp += toskew;
-    }
 }
 
 /*
@@ -1485,17 +1456,16 @@ DECLARESepPutFunc(putRGBUAseparate8bittile)
  */
 DECLARESepPutFunc(putRGBseparate16bittile)
 {
-    uint16 *wr = (uint16*) r;
-    uint16 *wg = (uint16*) g;
-    uint16 *wb = (uint16*) b;
-
-    (void) img; (void) y; (void) a;
-    while (h-- > 0) {
-       for (x = 0; x < w; x++)
-           *cp++ = PACKW(*wr++, *wg++, *wb++);
-       SKEW(wr, wg, wb, fromskew);
-       cp += toskew;
-    }
+       uint16 *wr = (uint16*) r;
+       uint16 *wg = (uint16*) g;
+       uint16 *wb = (uint16*) b;
+       (void) img; (void) y; (void) a;
+       while (h-- > 0) {
+               for (x = 0; x < w; x++)
+                    *cp++ = PACKW(*wr++,*wg++,*wb++);
+               SKEW(wr, wg, wb, fromskew);
+               cp += toskew;
+       }
 }
 
 /*
@@ -1503,18 +1473,17 @@ DECLARESepPutFunc(putRGBseparate16bittile)
  */
 DECLARESepPutFunc(putRGBAAseparate16bittile)
 {
-    uint16 *wr = (uint16*) r;
-    uint16 *wg = (uint16*) g;
-    uint16 *wb = (uint16*) b;
-    uint16 *wa = (uint16*) a;
-
-    (void) img; (void) y;
-    while (h-- > 0) {
-       for (x = 0; x < w; x++)
-           *cp++ = PACKW4(*wr++, *wg++, *wb++, *wa++);
-       SKEW4(wr, wg, wb, wa, fromskew);
-       cp += toskew;
-    }
+       uint16 *wr = (uint16*) r;
+       uint16 *wg = (uint16*) g;
+       uint16 *wb = (uint16*) b;
+       uint16 *wa = (uint16*) a;
+       (void) img; (void) y;
+       while (h-- > 0) {
+               for (x = 0; x < w; x++)
+                    *cp++ = PACKW4(*wr++,*wg++,*wb++,*wa++);
+               SKEW4(wr, wg, wb, wa, fromskew);
+               cp += toskew;
+       }
 }
 
 /*
@@ -1522,32 +1491,23 @@ DECLARESepPutFunc(putRGBAAseparate16bittile)
  */
 DECLARESepPutFunc(putRGBUAseparate16bittile)
 {
-    uint16 *wr = (uint16*) r;
-    uint16 *wg = (uint16*) g;
-    uint16 *wb = (uint16*) b;
-    uint16 *wa = (uint16*) a;
-
-    (void) img; (void) y;
-    while (h-- > 0) {
-       uint32 r,g,b,a;
-       /*
-        * We shift alpha down four bits just in case unsigned
-        * arithmetic doesn't handle the full range.
-        * We still have plenty of accuracy, since the output is 8 bits.
-        * So we have (r * 0xffff) * (a * 0xfff)) = r*a * (0xffff*0xfff)
-        * Since we want r*a * 0xff for eight bit output,
-        * we divide by (0xffff * 0xfff) / 0xff == 0x10eff.
-        */
-       for (x = w; x-- > 0;) {
-           a = *wa++ >> 4; 
-           r = (*wr++ * a) / 0x10eff;
-           g = (*wg++ * a) / 0x10eff;
-           b = (*wb++ * a) / 0x10eff;
-           *cp++ = PACK4(r,g,b,a);
+       uint16 *wr = (uint16*) r;
+       uint16 *wg = (uint16*) g;
+       uint16 *wb = (uint16*) b;
+       uint16 *wa = (uint16*) a;
+       (void) img; (void) y;
+       while (h-- > 0) {
+               uint32 r,g,b,a;
+               for (x = w; x-- > 0;) {
+                    a = W2B(*wa++);
+                    r = (a*W2B(*wr++) + 127) / 255;
+                    g = (a*W2B(*wg++) + 127) / 255;
+                    b = (a*W2B(*wb++) + 127) / 255;
+                    *cp++ = PACK4(r,g,b,a);
+               }
+               SKEW4(wr, wg, wb, wa, fromskew);
+               cp += toskew;
        }
-       SKEW4(wr, wg, wb, wa, fromskew);
-       cp += toskew;
-    }
 }
 
 /*
@@ -1885,63 +1845,56 @@ DECLAREContigPutFunc(putcontig8bitYCbCr41tile)
  */
 DECLAREContigPutFunc(putcontig8bitYCbCr22tile)
 {
-    uint32* cp1 = cp+w+toskew;
-    int32 incr = 2*toskew+w;
-
-    (void) y;
-    fromskew = (fromskew * 6) / 2;
-    if ((h & 1) == 0 && (w & 1) == 0) {
-        for (; h >= 2; h -= 2) {
-            x = w>>1;
-            do {
-                int32 Cb = pp[4];
-                int32 Cr = pp[5];
-
-                YCbCrtoRGB(cp [0], pp[0]);
-                YCbCrtoRGB(cp [1], pp[1]);
-                YCbCrtoRGB(cp1[0], pp[2]);
-                YCbCrtoRGB(cp1[1], pp[3]);
-
-                cp += 2, cp1 += 2;
-                pp += 6;
-            } while (--x);
-            cp += incr, cp1 += incr;
-            pp += fromskew;
-        }
-    } else {
-        while (h > 0) {
-            for (x = w; x > 0;) {
-                int32 Cb = pp[4];
-                int32 Cr = pp[5];
-                switch (x) {
-                default:
-                    switch (h) {
-                    default: YCbCrtoRGB(cp1[1], pp[ 3]); /* FALLTHROUGH */
-                    case 1:  YCbCrtoRGB(cp [1], pp[ 1]); /* FALLTHROUGH */
-                    }                                    /* FALLTHROUGH */
-                case 1:
-                    switch (h) {
-                    default: YCbCrtoRGB(cp1[0], pp[ 2]); /* FALLTHROUGH */
-                    case 1:  YCbCrtoRGB(cp [0], pp[ 0]); /* FALLTHROUGH */
-                    }                                    /* FALLTHROUGH */
-                }
-                if (x < 2) {
-                    cp += x; cp1 += x;
-                    x = 0;
-                }
-                else {
-                    cp += 2; cp1 += 2;
-                    x -= 2;
-                }
-                pp += 6;
-            }
-            if (h <= 2)
-                break;
-            h -= 2;
-            cp += incr, cp1 += incr;
-            pp += fromskew;
-        }
-    }
+       uint32* cp2;
+       (void) y;
+       fromskew = (fromskew / 2) * 6;
+       cp2 = cp+w+toskew;
+       while (h>=2) {
+               x = w;
+               while (x>=2) {
+                       uint32 Cb = pp[4];
+                       uint32 Cr = pp[5];
+                       YCbCrtoRGB(cp[0], pp[0]);
+                       YCbCrtoRGB(cp[1], pp[1]);
+                       YCbCrtoRGB(cp2[0], pp[2]);
+                       YCbCrtoRGB(cp2[1], pp[3]);
+                       cp += 2;
+                       cp2 += 2;
+                       pp += 6;
+                       x -= 2;
+               }
+               if (x==1) {
+                       uint32 Cb = pp[4];
+                       uint32 Cr = pp[5];
+                       YCbCrtoRGB(cp[0], pp[0]);
+                       YCbCrtoRGB(cp2[0], pp[2]);
+                       cp ++ ;
+                       cp2 ++ ;
+                       pp += 6;
+               }
+               cp += toskew*2+w;
+               cp2 += toskew*2+w;
+               pp += fromskew;
+               h-=2;
+       }
+       if (h==1) {
+               x = w;
+               while (x>=2) {
+                       uint32 Cb = pp[4];
+                       uint32 Cr = pp[5];
+                       YCbCrtoRGB(cp[0], pp[0]);
+                       YCbCrtoRGB(cp[1], pp[1]);
+                       cp += 2;
+                       cp2 += 2;
+                       pp += 6;
+                       x -= 2;
+               }
+               if (x==1) {
+                       uint32 Cb = pp[4];
+                       uint32 Cr = pp[5];
+                       YCbCrtoRGB(cp[0], pp[0]);
+               }
+       }
 }
 
 /*
@@ -1949,35 +1902,72 @@ DECLAREContigPutFunc(putcontig8bitYCbCr22tile)
  */
 DECLAREContigPutFunc(putcontig8bitYCbCr21tile)
 {
-    (void) y;
-    fromskew = (fromskew * 4) / 2;
-    do {
-       x = w>>1;
+       (void) y;
+       fromskew = (fromskew * 4) / 2;
        do {
-           int32 Cb = pp[2];
-           int32 Cr = pp[3];
+               x = w>>1;
+               do {
+                       int32 Cb = pp[2];
+                       int32 Cr = pp[3];
 
-           YCbCrtoRGB(cp[0], pp[0]); 
-           YCbCrtoRGB(cp[1], pp[1]);
+                       YCbCrtoRGB(cp[0], pp[0]);
+                       YCbCrtoRGB(cp[1], pp[1]);
 
-           cp += 2;
-           pp += 4;
-       } while (--x);
+                       cp += 2;
+                       pp += 4;
+               } while (--x);
 
-        if( (w&1) != 0 )
-        {
-           int32 Cb = pp[2];
-           int32 Cr = pp[3];
-            
-            YCbCrtoRGB(cp [0], pp[0]);
+               if( (w&1) != 0 )
+               {
+                       int32 Cb = pp[2];
+                       int32 Cr = pp[3];
 
-           cp += 1;
-           pp += 4;
-        }
+                       YCbCrtoRGB(cp[0], pp[0]);
 
-       cp += toskew;
-       pp += fromskew;
-    } while (--h);
+                       cp += 1;
+                       pp += 4;
+               }
+
+               cp += toskew;
+               pp += fromskew;
+       } while (--h);
+}
+
+/*
+ * 8-bit packed YCbCr samples w/ 1,2 subsampling => RGB
+ */
+DECLAREContigPutFunc(putcontig8bitYCbCr12tile)
+{
+       uint32* cp2;
+       (void) y;
+       fromskew = (fromskew / 2) * 4;
+       cp2 = cp+w+toskew;
+       while (h>=2) {
+               x = w;
+               do {
+                       uint32 Cb = pp[2];
+                       uint32 Cr = pp[3];
+                       YCbCrtoRGB(cp[0], pp[0]);
+                       YCbCrtoRGB(cp2[0], pp[1]);
+                       cp ++;
+                       cp2 ++;
+                       pp += 4;
+               } while (--x);
+               cp += toskew*2+w;
+               cp2 += toskew*2+w;
+               pp += fromskew;
+               h-=2;
+       }
+       if (h==1) {
+               x = w;
+               do {
+                       uint32 Cb = pp[2];
+                       uint32 Cr = pp[3];
+                       YCbCrtoRGB(cp[0], pp[0]);
+                       cp ++;
+                       pp += 4;
+               } while (--x);
+       }
 }
 
 /*
@@ -1985,70 +1975,71 @@ DECLAREContigPutFunc(putcontig8bitYCbCr21tile)
  */
 DECLAREContigPutFunc(putcontig8bitYCbCr11tile)
 {
-    (void) y;
-    fromskew *= 3;
-    do {
-        x = w; /* was x = w>>1; patched 2000/09/25 warmerda@home.com */ 
+       (void) y;
+       fromskew *= 3;
        do {
-           int32 Cb = pp[1];
-           int32 Cr = pp[2];
+               x = w; /* was x = w>>1; patched 2000/09/25 warmerda@home.com */
+               do {
+                       int32 Cb = pp[1];
+                       int32 Cr = pp[2];
 
-           YCbCrtoRGB(*cp++, pp[0]);
+                       YCbCrtoRGB(*cp++, pp[0]);
 
-           pp += 3;
-       } while (--x);
-       cp += toskew;
-       pp += fromskew;
-    } while (--h);
+                       pp += 3;
+               } while (--x);
+               cp += toskew;
+               pp += fromskew;
+       } while (--h);
 }
-#undef YCbCrtoRGB
 
-static tileContigRoutine
+/*
+ * 8-bit packed YCbCr samples w/ no subsampling => RGB
+ */
+DECLARESepPutFunc(putseparate8bitYCbCr11tile)
+{
+       (void) y;
+       (void) a;
+       /* TODO: naming of input vars is still off, change obfuscating declaration inside define, or resolve obfuscation */
+       while (h-- > 0) {
+               x = w;
+               do {
+                       uint32 dr, dg, db;
+                       TIFFYCbCrtoRGB(img->ycbcr,*r++,*g++,*b++,&dr,&dg,&db);
+                       *cp++ = PACK(dr,dg,db);
+               } while (--x);
+               SKEW(r, g, b, fromskew);
+               cp += toskew;
+       }
+}
+#undef YCbCrtoRGB
+
+static int
 initYCbCrConversion(TIFFRGBAImage* img)
 {
-       static char module[] = "initCIELabConversion";
+       static char module[] = "initYCbCrConversion";
 
        float *luma, *refBlackWhite;
-       uint16 hs, vs;
 
        if (img->ycbcr == NULL) {
-           img->ycbcr = (TIFFYCbCrToRGB*) _TIFFmalloc(
+               img->ycbcr = (TIFFYCbCrToRGB*) _TIFFmalloc(
                    TIFFroundup(sizeof (TIFFYCbCrToRGB), sizeof (long))
                    + 4*256*sizeof (TIFFRGBValue)
                    + 2*256*sizeof (int)
                    + 3*256*sizeof (int32)
-           );
-           if (img->ycbcr == NULL) {
-                   TIFFError(module,
-                             "No space for YCbCr->RGB conversion state");
-                   return (NULL);
-           }
+                   );
+               if (img->ycbcr == NULL) {
+                       TIFFErrorExt(img->tif->tif_clientdata, module,
+                           "No space for YCbCr->RGB conversion state");
+                       return (0);
+               }
        }
 
        TIFFGetFieldDefaulted(img->tif, TIFFTAG_YCBCRCOEFFICIENTS, &luma);
        TIFFGetFieldDefaulted(img->tif, TIFFTAG_REFERENCEBLACKWHITE,
-                             &refBlackWhite);
+           &refBlackWhite);
        if (TIFFYCbCrToRGBInit(img->ycbcr, luma, refBlackWhite) < 0)
-               return NULL;
-
-       /*
-        * The 6.0 spec says that subsampling must be
-        * one of 1, 2, or 4, and that vertical subsampling
-        * must always be <= horizontal subsampling; so
-        * there are only a few possibilities and we just
-        * enumerate the cases.
-        */
-       TIFFGetFieldDefaulted(img->tif, TIFFTAG_YCBCRSUBSAMPLING, &hs, &vs);
-       switch ((hs<<4)|vs) {
-               case 0x44: return (tileContigRoutine)(putcontig8bitYCbCr44tile);
-               case 0x42: return (tileContigRoutine)(putcontig8bitYCbCr42tile);
-               case 0x41: return (tileContigRoutine)(putcontig8bitYCbCr41tile);
-               case 0x22: return (tileContigRoutine)(putcontig8bitYCbCr22tile);
-               case 0x21: return (tileContigRoutine)(putcontig8bitYCbCr21tile);
-               case 0x11: return (tileContigRoutine)(putcontig8bitYCbCr11tile);
-       }
-
-       return (NULL);
+               return(0);
+       return (1);
 }
 
 static tileContigRoutine
@@ -2063,7 +2054,7 @@ initCIELabConversion(TIFFRGBAImage* img)
                img->cielab = (TIFFCIELabToRGB *)
                        _TIFFmalloc(sizeof(TIFFCIELabToRGB));
                if (!img->cielab) {
-                       TIFFError(module,
+                       TIFFErrorExt(img->tif->tif_clientdata, module,
                            "No space for CIE L*a*b*->RGB conversion state.");
                        return NULL;
                }
@@ -2075,13 +2066,13 @@ initCIELabConversion(TIFFRGBAImage* img)
        refWhite[2] = (1.0F - whitePoint[0] - whitePoint[1])
                      / whitePoint[1] * refWhite[1];
        if (TIFFCIELabToRGBInit(img->cielab, &display_sRGB, refWhite) < 0) {
-               TIFFError(module,
+               TIFFErrorExt(img->tif->tif_clientdata, module,
                    "Failed to initialize CIE L*a*b*->RGB conversion state.");
                _TIFFfree(img->cielab);
                return NULL;
        }
 
-       return (tileContigRoutine)putcontig8bitCIELab;
+       return putcontig8bitCIELab;
 }
 
 /*
@@ -2106,8 +2097,8 @@ makebwmap(TIFFRGBAImage* img)
     img->BWmap = (uint32**) _TIFFmalloc(
        256*sizeof (uint32 *)+(256*nsamples*sizeof(uint32)));
     if (img->BWmap == NULL) {
-       TIFFError(TIFFFileName(img->tif), "No space for B&W mapping table");
-       return (0);
+               TIFFErrorExt(img->tif->tif_clientdata, TIFFFileName(img->tif), "No space for B&W mapping table");
+               return (0);
     }
     p = (uint32*)(img->BWmap + 256);
     for (i = 0; i < 256; i++) {
@@ -2163,9 +2154,9 @@ setupMap(TIFFRGBAImage* img)
 
     img->Map = (TIFFRGBValue*) _TIFFmalloc((range+1) * sizeof (TIFFRGBValue));
     if (img->Map == NULL) {
-       TIFFError(TIFFFileName(img->tif),
-           "No space for photometric conversion table");
-       return (0);
+               TIFFErrorExt(img->tif->tif_clientdata, TIFFFileName(img->tif),
+                       "No space for photometric conversion table");
+               return (0);
     }
     if (img->photometric == PHOTOMETRIC_MINISWHITE) {
        for (x = 0; x <= range; x++)
@@ -2241,9 +2232,9 @@ makecmap(TIFFRGBAImage* img)
     img->PALmap = (uint32**) _TIFFmalloc(
        256*sizeof (uint32 *)+(256*nsamples*sizeof(uint32)));
     if (img->PALmap == NULL) {
-       TIFFError(TIFFFileName(img->tif), "No space for Palette mapping table");
-       return (0);
-    }
+               TIFFErrorExt(img->tif->tif_clientdata, TIFFFileName(img->tif), "No space for Palette mapping table");
+               return (0);
+       }
     p = (uint32*)(img->PALmap + 256);
     for (i = 0; i < 256; i++) {
        TIFFRGBValue c;
@@ -2306,7 +2297,7 @@ buildMap(TIFFRGBAImage* img)
        if (checkcmap(img) == 16)
            cvtcmap(img);
        else
-           TIFFWarning(TIFFFileName(img->tif), "Assuming 8-bit colormap");
+           TIFFWarningExt(img->tif->tif_clientdata, TIFFFileName(img->tif), "Assuming 8-bit colormap");
        /*
         * Use mapping table and colormap to construct
         * unpacking tables for samples < 8 bits.
@@ -2322,73 +2313,140 @@ buildMap(TIFFRGBAImage* img)
  * Select the appropriate conversion routine for packed data.
  */
 static int
-pickTileContigCase(TIFFRGBAImage* img)
+PickContigCase(TIFFRGBAImage* img)
 {
-    tileContigRoutine put = 0;
-
-    if (buildMap(img)) {
+       img->get = TIFFIsTiled(img->tif) ? gtTileContig : gtStripContig;
+       img->put.contig = NULL;
        switch (img->photometric) {
-       case PHOTOMETRIC_RGB:
-           switch (img->bitspersample) {
-           case 8:
-               if (!img->Map) {
-                   if (img->alpha == EXTRASAMPLE_ASSOCALPHA)
-                       put = putRGBAAcontig8bittile;
-                   else if (img->alpha == EXTRASAMPLE_UNASSALPHA)
-                       put = putRGBUAcontig8bittile;
-                   else
-                       put = putRGBcontig8bittile;
-               } else
-                   put = putRGBcontig8bitMaptile;
-               break;
-           case 16:
-               put = putRGBcontig16bittile;
-               if (!img->Map) {
-                   if (img->alpha == EXTRASAMPLE_ASSOCALPHA)
-                       put = putRGBAAcontig16bittile;
-                   else if (img->alpha == EXTRASAMPLE_UNASSALPHA)
-                       put = putRGBUAcontig16bittile;
-               }
-               break;
-           }
-           break;
-       case PHOTOMETRIC_SEPARATED:
-           if (img->bitspersample == 8) {
-               if (!img->Map)
-                   put = putRGBcontig8bitCMYKtile;
-               else
-                   put = putRGBcontig8bitCMYKMaptile;
-           }
-           break;
-       case PHOTOMETRIC_PALETTE:
-           switch (img->bitspersample) {
-           case 8:     put = put8bitcmaptile; break;
-           case 4: put = put4bitcmaptile; break;
-           case 2: put = put2bitcmaptile; break;
-           case 1: put = put1bitcmaptile; break;
-           }
-           break;
-       case PHOTOMETRIC_MINISWHITE:
-       case PHOTOMETRIC_MINISBLACK:
-           switch (img->bitspersample) {
-            case 16: put = put16bitbwtile; break;
-           case 8:  put = putgreytile; break;
-           case 4:  put = put4bitbwtile; break;
-           case 2:  put = put2bitbwtile; break;
-           case 1:  put = put1bitbwtile; break;
-           }
-           break;
-       case PHOTOMETRIC_YCBCR:
-           if (img->bitspersample == 8)
-               put = initYCbCrConversion(img);
-           break;
-       case PHOTOMETRIC_CIELAB:
-           if (img->bitspersample == 8)
-               put = initCIELabConversion(img);
-           break;
+               case PHOTOMETRIC_RGB:
+                       switch (img->bitspersample) {
+                               case 8:
+                                       if (img->alpha == EXTRASAMPLE_ASSOCALPHA)
+                                               img->put.contig = putRGBAAcontig8bittile;
+                                       else if (img->alpha == EXTRASAMPLE_UNASSALPHA)
+                                       {
+                                            img->put.contig = putRGBUAcontig8bittile;
+                                       }
+                                       else
+                                            img->put.contig = putRGBcontig8bittile;
+                                       break;
+                               case 16:
+                                       if (img->alpha == EXTRASAMPLE_ASSOCALPHA)
+                                       {
+                                            img->put.contig = putRGBAAcontig16bittile;
+                                       }
+                                       else if (img->alpha == EXTRASAMPLE_UNASSALPHA)
+                                       {
+                                            img->put.contig = putRGBUAcontig16bittile;
+                                       }
+                                       else
+                                       {
+                                            img->put.contig = putRGBcontig16bittile;
+                                       }
+                                       break;
+                       }
+                       break;
+               case PHOTOMETRIC_SEPARATED:
+                       if (buildMap(img)) {
+                               if (img->bitspersample == 8) {
+                                       if (!img->Map)
+                                               img->put.contig = putRGBcontig8bitCMYKtile;
+                                       else
+                                               img->put.contig = putRGBcontig8bitCMYKMaptile;
+                               }
+                       }
+                       break;
+               case PHOTOMETRIC_PALETTE:
+                       if (buildMap(img)) {
+                               switch (img->bitspersample) {
+                                       case 8:
+                                               img->put.contig = put8bitcmaptile;
+                                               break;
+                                       case 4:
+                                               img->put.contig = put4bitcmaptile;
+                                               break;
+                                       case 2:
+                                               img->put.contig = put2bitcmaptile;
+                                               break;
+                                       case 1:
+                                               img->put.contig = put1bitcmaptile;
+                                               break;
+                               }
+                       }
+                       break;
+               case PHOTOMETRIC_MINISWHITE:
+               case PHOTOMETRIC_MINISBLACK:
+                       if (buildMap(img)) {
+                               switch (img->bitspersample) {
+                                       case 16:
+                                               img->put.contig = put16bitbwtile;
+                                               break;
+                                       case 8:
+                                               img->put.contig = putgreytile;
+                                               break;
+                                       case 4:
+                                               img->put.contig = put4bitbwtile;
+                                               break;
+                                       case 2:
+                                               img->put.contig = put2bitbwtile;
+                                               break;
+                                       case 1:
+                                               img->put.contig = put1bitbwtile;
+                                               break;
+                               }
+                       }
+                       break;
+               case PHOTOMETRIC_YCBCR:
+                       if (img->bitspersample == 8)
+                       {
+                               if (initYCbCrConversion(img)!=0)
+                               {
+                                       /*
+                                        * The 6.0 spec says that subsampling must be
+                                        * one of 1, 2, or 4, and that vertical subsampling
+                                        * must always be <= horizontal subsampling; so
+                                        * there are only a few possibilities and we just
+                                        * enumerate the cases.
+                                        * Joris: added support for the [1,2] case, nonetheless, to accomodate
+                                        * some OJPEG files
+                                        */
+                                       uint16 SubsamplingHor;
+                                       uint16 SubsamplingVer;
+                                       TIFFGetFieldDefaulted(img->tif, TIFFTAG_YCBCRSUBSAMPLING, &SubsamplingHor, &SubsamplingVer);
+                                       switch ((SubsamplingHor<<4)|SubsamplingVer) {
+                                               case 0x44:
+                                                       img->put.contig = putcontig8bitYCbCr44tile;
+                                                       break;
+                                               case 0x42:
+                                                       img->put.contig = putcontig8bitYCbCr42tile;
+                                                       break;
+                                               case 0x41:
+                                                       img->put.contig = putcontig8bitYCbCr41tile;
+                                                       break;
+                                               case 0x22:
+                                                       img->put.contig = putcontig8bitYCbCr22tile;
+                                                       break;
+                                               case 0x21:
+                                                       img->put.contig = putcontig8bitYCbCr21tile;
+                                                       break;
+                                               case 0x12:
+                                                       img->put.contig = putcontig8bitYCbCr12tile;
+                                                       break;
+                                               case 0x11:
+                                                       img->put.contig = putcontig8bitYCbCr11tile;
+                                                       break;
+                                       }
+                               }
+                       }
+                       break;
+               case PHOTOMETRIC_CIELAB:
+                       if (buildMap(img)) {
+                               if (img->bitspersample == 8)
+                                       img->put.contig = initCIELabConversion(img);
+                               break;
+                       }
        }
-    }
-    return ((img->put.contig = put) != 0);
+       return ((img->get!=NULL) && (img->put.contig!=NULL));
 }
 
 /*
@@ -2398,39 +2456,57 @@ pickTileContigCase(TIFFRGBAImage* img)
  *      to the "packed routines.
  */
 static int
-pickTileSeparateCase(TIFFRGBAImage* img)
+PickSeparateCase(TIFFRGBAImage* img)
 {
-    tileSeparateRoutine put = 0;
-
-    if (buildMap(img)) {
+       img->get = TIFFIsTiled(img->tif) ? gtTileSeparate : gtStripSeparate;
+       img->put.separate = NULL;
        switch (img->photometric) {
-       case PHOTOMETRIC_RGB:
-           switch (img->bitspersample) {
-           case 8:
-               if (!img->Map) {
-                   if (img->alpha == EXTRASAMPLE_ASSOCALPHA)
-                       put = putRGBAAseparate8bittile;
-                   else if (img->alpha == EXTRASAMPLE_UNASSALPHA)
-                       put = putRGBUAseparate8bittile;
-                   else
-                       put = putRGBseparate8bittile;
-               } else
-                   put = putRGBseparate8bitMaptile;
-               break;
-           case 16:
-               put = putRGBseparate16bittile;
-               if (!img->Map) {
-                   if (img->alpha == EXTRASAMPLE_ASSOCALPHA)
-                       put = putRGBAAseparate16bittile;
-                   else if (img->alpha == EXTRASAMPLE_UNASSALPHA)
-                       put = putRGBUAseparate16bittile;
-               }
-               break;
-           }
-           break;
+               case PHOTOMETRIC_RGB:
+                       switch (img->bitspersample) {
+                               case 8:
+                                       if (img->alpha == EXTRASAMPLE_ASSOCALPHA)
+                                               img->put.separate = putRGBAAseparate8bittile;
+                                       else if (img->alpha == EXTRASAMPLE_UNASSALPHA)
+                                       {
+                                            img->put.separate = putRGBUAseparate8bittile;
+                                       }
+                                       else
+                                               img->put.separate = putRGBseparate8bittile;
+                                       break;
+                               case 16:
+                                       if (img->alpha == EXTRASAMPLE_ASSOCALPHA)
+                                       {
+                                            img->put.separate = putRGBAAseparate16bittile;
+                                       }
+                                       else if (img->alpha == EXTRASAMPLE_UNASSALPHA)
+                                       {
+                                            img->put.separate = putRGBUAseparate16bittile;
+                                       }
+                                       else
+                                       {
+                                            img->put.separate = putRGBseparate16bittile;
+                                       }
+                                       break;
+                       }
+                       break;
+               case PHOTOMETRIC_YCBCR:
+                       if ((img->bitspersample==8) && (img->samplesperpixel==3))
+                       {
+                               if (initYCbCrConversion(img)!=0)
+                               {
+                                       uint16 hs, vs;
+                                       TIFFGetFieldDefaulted(img->tif, TIFFTAG_YCBCRSUBSAMPLING, &hs, &vs);
+                                       switch ((hs<<4)|vs) {
+                                               case 0x11:
+                                                       img->put.separate = putseparate8bitYCbCr11tile;
+                                                       break;
+                                               /* TODO: add other cases here */
+                                       }
+                               }
+                       }
+                       break;
        }
-    }
-    return ((img->put.separate = put) != 0);
+       return ((img->get!=NULL) && (img->put.separate!=NULL));
 }
 
 /*
@@ -2452,7 +2528,7 @@ TIFFReadRGBAStrip(TIFF* tif, uint32 row, uint32 * raster )
 
     if( TIFFIsTiled( tif ) )
     {
-        TIFFError(TIFFFileName(tif),
+               TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif),
                   "Can't use TIFFReadRGBAStrip() with tiled file.");
        return (0);
     }
@@ -2460,9 +2536,9 @@ TIFFReadRGBAStrip(TIFF* tif, uint32 row, uint32 * raster )
     TIFFGetFieldDefaulted(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);
     if( (row % rowsperstrip) != 0 )
     {
-        TIFFError(TIFFFileName(tif),
-                "Row passed to TIFFReadRGBAStrip() must be first in a strip.");
-       return (0);
+               TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif),
+                               "Row passed to TIFFReadRGBAStrip() must be first in a strip.");
+               return (0);
     }
 
     if (TIFFRGBAImageOK(tif, emsg) && TIFFRGBAImageBegin(&img, tif, 0, emsg)) {
@@ -2479,8 +2555,8 @@ TIFFReadRGBAStrip(TIFF* tif, uint32 row, uint32 * raster )
         
        TIFFRGBAImageEnd(&img);
     } else {
-       TIFFError(TIFFFileName(tif), emsg);
-       ok = 0;
+               TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "%s", emsg);
+               ok = 0;
     }
     
     return (ok);
@@ -2510,16 +2586,16 @@ TIFFReadRGBATile(TIFF* tif, uint32 col, uint32 row, uint32 * raster)
     
     if( !TIFFIsTiled( tif ) )
     {
-        TIFFError(TIFFFileName(tif),
-                  "Can't use TIFFReadRGBATile() with stripped file.");
-       return (0);
+               TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif),
+                                 "Can't use TIFFReadRGBATile() with stripped file.");
+               return (0);
     }
     
     TIFFGetFieldDefaulted(tif, TIFFTAG_TILEWIDTH, &tile_xsize);
     TIFFGetFieldDefaulted(tif, TIFFTAG_TILELENGTH, &tile_ysize);
     if( (col % tile_xsize) != 0 || (row % tile_ysize) != 0 )
     {
-        TIFFError(TIFFFileName(tif),
+               TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif),
                   "Row/col passed to TIFFReadRGBATile() must be top"
                   "left corner of a tile.");
        return (0);
@@ -2531,7 +2607,7 @@ TIFFReadRGBATile(TIFF* tif, uint32 col, uint32 row, uint32 * raster)
     
     if (!TIFFRGBAImageOK(tif, emsg) 
        || !TIFFRGBAImageBegin(&img, tif, 0, emsg)) {
-           TIFFError(TIFFFileName(tif), emsg);
+           TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "%s", emsg);
            return( 0 );
     }
 
@@ -2591,3 +2667,10 @@ TIFFReadRGBATile(TIFF* tif, uint32 col, uint32 row, uint32 * raster)
 }
 
 /* vim: set ts=8 sts=8 sw=8 noet: */
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
diff --git a/3rdparty/libtiff/tif_jbig.c b/3rdparty/libtiff/tif_jbig.c
new file mode 100644 (file)
index 0000000..c92ee3d
--- /dev/null
@@ -0,0 +1,385 @@
+/* $Id: tif_jbig.c,v 1.2.2.3 2010-06-08 18:50:42 bfriesen Exp $ */
+
+/*
+ * Copyright (c) 1988-1997 Sam Leffler
+ * Copyright (c) 1991-1997 Silicon Graphics, Inc.
+ *
+ * Permission to use, copy, modify, distribute, and sell this software and 
+ * its documentation for any purpose is hereby granted without fee, provided
+ * that (i) the above copyright notices and this permission notice appear in
+ * all copies of the software and related documentation, and (ii) the names of
+ * Sam Leffler and Silicon Graphics may not be used in any advertising or
+ * publicity relating to the software without the specific, prior written
+ * permission of Sam Leffler and Silicon Graphics.
+ * 
+ * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
+ * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
+ * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
+ * 
+ * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
+ * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
+ * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
+ * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
+ * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
+ * OF THIS SOFTWARE.
+ */
+
+/*
+ * TIFF Library.
+ *
+ * JBIG Compression Algorithm Support.
+ * Contributed by Lee Howard <faxguy@deanox.com>
+ * 
+ */
+
+#include "tiffiop.h"
+
+#ifdef JBIG_SUPPORT
+#include "jbig.h"
+
+typedef struct
+{
+        uint32  recvparams;     /* encoded Class 2 session params             */
+        char*   subaddress;     /* subaddress string                          */
+        uint32  recvtime;       /* time spend receiving in seconds            */
+        char*   faxdcs;         /* encoded fax parameters (DCS, Table 2/T.30) */
+
+        TIFFVGetMethod vgetparent;
+        TIFFVSetMethod vsetparent;
+} JBIGState;
+
+#define GetJBIGState(tif) ((JBIGState*)(tif)->tif_data)
+
+#define FIELD_RECVPARAMS        (FIELD_CODEC+0)
+#define FIELD_SUBADDRESS        (FIELD_CODEC+1)
+#define FIELD_RECVTIME          (FIELD_CODEC+2)
+#define FIELD_FAXDCS            (FIELD_CODEC+3)
+
+static const TIFFFieldInfo jbigFieldInfo[] = 
+{
+        {TIFFTAG_FAXRECVPARAMS,  1,  1, TIFF_LONG,  FIELD_RECVPARAMS, TRUE, FALSE, "FaxRecvParams"},
+        {TIFFTAG_FAXSUBADDRESS, -1, -1, TIFF_ASCII, FIELD_SUBADDRESS, TRUE, FALSE, "FaxSubAddress"},
+        {TIFFTAG_FAXRECVTIME,    1,  1, TIFF_LONG,  FIELD_RECVTIME,   TRUE, FALSE, "FaxRecvTime"},
+        {TIFFTAG_FAXDCS,        -1, -1, TIFF_ASCII, FIELD_FAXDCS,     TRUE, FALSE, "FaxDcs"},
+};
+
+static int JBIGSetupDecode(TIFF* tif)
+{
+        if (TIFFNumberOfStrips(tif) != 1)
+        {
+                TIFFError("JBIG", "Multistrip images not supported in decoder");
+                return 0;
+        }
+
+        return 1;
+}
+
+static int JBIGDecode(TIFF* tif, tidata_t buffer, tsize_t size, tsample_t s)
+{
+        struct jbg_dec_state decoder;
+        int decodeStatus = 0;
+        unsigned char* pImage = NULL;
+       (void) size, (void) s;
+
+        if (isFillOrder(tif, tif->tif_dir.td_fillorder))
+        {
+                TIFFReverseBits(tif->tif_rawdata, tif->tif_rawdatasize);
+        }
+
+        jbg_dec_init(&decoder);
+
+#if defined(HAVE_JBG_NEWLEN)
+        jbg_newlen(tif->tif_rawdata, tif->tif_rawdatasize);
+        /*
+         * I do not check the return status of jbg_newlen because even if this
+         * function fails it does not necessarily mean that decoding the image
+         * will fail.  It is generally only needed for received fax images
+         * that do not contain the actual length of the image in the BIE
+         * header.  I do not log when an error occurs because that will cause
+         * problems when converting JBIG encoded TIFF's to 
+         * PostScript.  As long as the actual image length is contained in the
+         * BIE header jbg_dec_in should succeed.
+         */
+#endif /* HAVE_JBG_NEWLEN */
+
+        decodeStatus = jbg_dec_in(&decoder, tif->tif_rawdata,
+                                  tif->tif_rawdatasize, NULL);
+        if (JBG_EOK != decodeStatus)
+        {
+               /*
+                * XXX: JBG_EN constant was defined in pre-2.0 releases of the
+                * JBIG-KIT. Since the 2.0 the error reporting functions were
+                * changed. We will handle both cases here.
+                */
+                TIFFError("JBIG", "Error (%d) decoding: %s", decodeStatus,
+#if defined(JBG_EN)
+                         jbg_strerror(decodeStatus, JBG_EN)
+#else
+                          jbg_strerror(decodeStatus)
+#endif
+                        );
+                return 0;
+        }
+        
+        pImage = jbg_dec_getimage(&decoder, 0);
+        _TIFFmemcpy(buffer, pImage, jbg_dec_getsize(&decoder));
+        jbg_dec_free(&decoder);
+        return 1;
+}
+
+static int JBIGSetupEncode(TIFF* tif)
+{
+        if (TIFFNumberOfStrips(tif) != 1)
+        {
+                TIFFError("JBIG", "Multistrip images not supported in encoder");
+                return 0;
+        }
+
+        return 1;
+}
+
+static int JBIGCopyEncodedData(TIFF* tif, tidata_t pp, tsize_t cc, tsample_t s)
+{
+        (void) s;
+        while (cc > 0) 
+        {
+                tsize_t n = cc;
+
+                if (tif->tif_rawcc + n > tif->tif_rawdatasize)
+                {
+                        n = tif->tif_rawdatasize - tif->tif_rawcc;
+                }
+
+                assert(n > 0);
+                _TIFFmemcpy(tif->tif_rawcp, pp, n);
+                tif->tif_rawcp += n;
+                tif->tif_rawcc += n;
+                pp += n;
+                cc -= n;
+                if (tif->tif_rawcc >= tif->tif_rawdatasize &&
+                    !TIFFFlushData1(tif))
+                {
+                        return (-1);
+                }
+        }
+
+        return (1);
+}
+
+static void JBIGOutputBie(unsigned char* buffer, size_t len, void *userData)
+{
+        TIFF* tif = (TIFF*)userData;
+
+        if (isFillOrder(tif, tif->tif_dir.td_fillorder))
+        {
+                TIFFReverseBits(buffer, len);
+        }
+
+        JBIGCopyEncodedData(tif, buffer, len, 0);
+}
+
+static int JBIGEncode(TIFF* tif, tidata_t buffer, tsize_t size, tsample_t s)
+{
+        TIFFDirectory* dir = &tif->tif_dir;
+        struct jbg_enc_state encoder;
+
+       (void) size, (void) s;
+
+        jbg_enc_init(&encoder, 
+                     dir->td_imagewidth, 
+                     dir->td_imagelength, 
+                     1, 
+                     &buffer,
+                     JBIGOutputBie,
+                     tif);
+        /* 
+         * jbg_enc_out does the "real" encoding.  As data is encoded,
+         * JBIGOutputBie is called, which writes the data to the directory.
+         */
+        jbg_enc_out(&encoder);
+        jbg_enc_free(&encoder);
+
+        return 1;
+}
+
+static void JBIGCleanup(TIFF* tif)
+{
+        JBIGState *sp = GetJBIGState(tif);
+
+        assert(sp != 0);
+
+        tif->tif_tagmethods.vgetfield = sp->vgetparent;
+        tif->tif_tagmethods.vsetfield = sp->vsetparent;
+
+       _TIFFfree(tif->tif_data);
+       tif->tif_data = NULL;
+
+       _TIFFSetDefaultCompressionState(tif);
+}
+
+static void JBIGPrintDir(TIFF* tif, FILE* fd, long flags)
+{
+        JBIGState* codec = GetJBIGState(tif);
+        (void)flags;
+
+        if (TIFFFieldSet(tif, FIELD_RECVPARAMS))
+        {
+                fprintf(fd, 
+                        "  Fax Receive Parameters: %08lx\n",
+                        (unsigned long)codec->recvparams);
+        }
+
+        if (TIFFFieldSet(tif, FIELD_SUBADDRESS))
+        {
+                fprintf(fd, 
+                        "  Fax SubAddress: %s\n", 
+                        codec->subaddress);
+        }
+
+        if (TIFFFieldSet(tif, FIELD_RECVTIME))
+        {
+                fprintf(fd, 
+                        "  Fax Receive Time: %lu secs\n",
+                        (unsigned long)codec->recvtime);
+        }
+
+        if (TIFFFieldSet(tif, FIELD_FAXDCS))
+        {
+                fprintf(fd, 
+                        "  Fax DCS: %s\n", 
+                        codec->faxdcs);
+        }
+}
+
+static int JBIGVGetField(TIFF* tif, ttag_t tag, va_list ap)
+{
+        JBIGState* codec = GetJBIGState(tif);
+
+        switch (tag)
+        {
+                case TIFFTAG_FAXRECVPARAMS:
+                        *va_arg(ap, uint32*) = codec->recvparams;
+                        break;
+                
+                case TIFFTAG_FAXSUBADDRESS:
+                        *va_arg(ap, char**) = codec->subaddress;
+                        break;
+
+                case TIFFTAG_FAXRECVTIME:
+                        *va_arg(ap, uint32*) = codec->recvtime;
+                        break;
+
+                case TIFFTAG_FAXDCS:
+                        *va_arg(ap, char**) = codec->faxdcs;
+                        break;
+
+                default:
+                        return (*codec->vgetparent)(tif, tag, ap);
+        }
+
+        return 1;
+}
+
+static int JBIGVSetField(TIFF* tif, ttag_t tag, va_list ap)
+{
+        JBIGState* codec = GetJBIGState(tif);
+
+        switch (tag)
+        {
+                case TIFFTAG_FAXRECVPARAMS:
+                        codec->recvparams = va_arg(ap, uint32);
+                        break;
+
+                case TIFFTAG_FAXSUBADDRESS:
+                        _TIFFsetString(&codec->subaddress, va_arg(ap, char*));
+                        break;
+
+                case TIFFTAG_FAXRECVTIME:
+                        codec->recvtime = va_arg(ap, uint32);
+                        break;
+
+                case TIFFTAG_FAXDCS:
+                        _TIFFsetString(&codec->faxdcs, va_arg(ap, char*));
+                        break;
+
+                default:
+                        return (*codec->vsetparent)(tif, tag, ap);
+        }
+
+        TIFFSetFieldBit(tif, _TIFFFieldWithTag(tif, tag)->field_bit);
+        tif->tif_flags |= TIFF_DIRTYDIRECT;
+        return 1;
+}
+
+int TIFFInitJBIG(TIFF* tif, int scheme)
+{
+        JBIGState* codec = NULL;
+
+       assert(scheme == COMPRESSION_JBIG);
+
+       /*
+        * Merge codec-specific tag information.
+        */
+       if (!_TIFFMergeFieldInfo(tif, jbigFieldInfo,
+                                TIFFArrayCount(jbigFieldInfo))) {
+               TIFFErrorExt(tif->tif_clientdata, "TIFFInitJBIG",
+                            "Merging JBIG codec-specific tags failed");
+               return 0;
+       }
+
+        /* Allocate memory for the JBIGState structure.*/
+        tif->tif_data = (tdata_t)_TIFFmalloc(sizeof(JBIGState));
+        if (tif->tif_data == NULL)
+        {
+                TIFFError("TIFFInitJBIG", "Not enough memory for JBIGState");
+                return 0;
+        }
+        _TIFFmemset(tif->tif_data, 0, sizeof(JBIGState));
+        codec = GetJBIGState(tif);
+
+        /* Initialize codec private fields */
+        codec->recvparams = 0;
+        codec->subaddress = NULL;
+        codec->faxdcs = NULL;
+        codec->recvtime = 0;
+
+       /* 
+        * Override parent get/set field methods.
+        */
+        codec->vgetparent = tif->tif_tagmethods.vgetfield;
+        codec->vsetparent = tif->tif_tagmethods.vsetfield;
+        tif->tif_tagmethods.vgetfield = JBIGVGetField;
+        tif->tif_tagmethods.vsetfield = JBIGVSetField;
+        tif->tif_tagmethods.printdir = JBIGPrintDir;
+
+        /*
+         * These flags are set so the JBIG Codec can control when to reverse
+         * bits and when not to and to allow the jbig decoder and bit reverser
+         * to write to memory when necessary.
+         */
+        tif->tif_flags |= TIFF_NOBITREV;
+        tif->tif_flags &= ~TIFF_MAPPED;
+
+        /* Setup the function pointers for encode, decode, and cleanup. */
+        tif->tif_setupdecode = JBIGSetupDecode;
+        tif->tif_decodestrip = JBIGDecode;
+
+        tif->tif_setupencode = JBIGSetupEncode;
+        tif->tif_encodestrip = JBIGEncode;
+        
+        tif->tif_cleanup = JBIGCleanup;
+
+        return 1;
+}
+
+#endif /* JBIG_SUPPORT */
+
+/* vim: set ts=8 sts=8 sw=8 noet: */
+
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
index d4d757e..a967827 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: tif_jpeg.c,v 1.1 2005-06-17 13:54:52 vp153 Exp $ */
+/* $Id: tif_jpeg.c,v 1.50.2.9 2010-06-14 02:47:16 fwarmerdam Exp $ */
 
 /*
  * Copyright (c) 1994-1997 Sam Leffler
  * OF THIS SOFTWARE.
  */
 
+#define WIN32_LEAN_AND_MEAN
+#define VC_EXTRALEAN
+
 #include "tiffiop.h"
 #ifdef JPEG_SUPPORT
+
 /*
  * TIFF Library
  *
@@ -49,6 +53,15 @@ int TIFFFillTile(TIFF*, ttile_t);
 #undef FAR
 #endif
 
+/*
+  Libjpeg's jmorecfg.h defines INT16 and INT32, but only if XMD_H is
+  not defined.  Unfortunately, the MinGW and Borland compilers include
+  a typedef for INT32, which causes a conflict.  MSVC does not include
+  a conficting typedef given the headers which are included.
+*/
+#if defined(__BORLANDC__) || defined(__MINGW32__)
+# define XMD_H 1
+#endif
 
 /*
    The windows RPCNDR.H file defines boolean, but defines it with the
@@ -64,7 +77,7 @@ int TIFFFillTile(TIFF*, ttile_t);
 */
 
 /* Define "boolean" as unsigned char, not int, per Windows custom. */
-#if defined(WIN32)
+#if defined(WIN32) && !defined(__MINGW32__)
 # ifndef __RPCNDR_H__            /* don't conflict if rpcndr.h already read */
    typedef unsigned char boolean;
 # endif
@@ -139,6 +152,7 @@ typedef     struct {
 
        TIFFVGetMethod  vgetparent;     /* super-class method */
        TIFFVSetMethod  vsetparent;     /* super-class method */
+       TIFFPrintMethod printdir;       /* super-class method */
        TIFFStripMethod defsparent;     /* super-class method */
        TIFFTileMethod  deftparent;     /* super-class method */
                                        /* pseudo-tag fields */
@@ -161,7 +175,8 @@ static      int JPEGDecode(TIFF*, tidata_t, tsize_t, tsample_t);
 static int JPEGDecodeRaw(TIFF*, tidata_t, tsize_t, tsample_t);
 static int JPEGEncode(TIFF*, tidata_t, tsize_t, tsample_t);
 static int JPEGEncodeRaw(TIFF*, tidata_t, tsize_t, tsample_t);
-static  int JPEGInitializeLibJPEG( TIFF * tif );
+static  int JPEGInitializeLibJPEG( TIFF * tif,
+                                                                  int force_encode, int force_decode );
 
 #define        FIELD_JPEGTABLES        (FIELD_CODEC+0)
 #define        FIELD_RECVPARAMS        (FIELD_CODEC+1)
@@ -186,7 +201,7 @@ static const TIFFFieldInfo jpegFieldInfo[] = {
     { TIFFTAG_FAXRECVTIME,      1, 1, TIFF_LONG,       FIELD_RECVTIME,
       TRUE,    FALSE,  "FaxRecvTime" },
     { TIFFTAG_FAXDCS,          -1, -1, TIFF_ASCII,     FIELD_FAXDCS,
-      TRUE,    FALSE,  "FaxDcs" },
+         TRUE, FALSE,  "FaxDcs" },
 };
 #define        N(a)    (sizeof (a) / sizeof (a[0]))
 
@@ -211,7 +226,7 @@ TIFFjpeg_error_exit(j_common_ptr cinfo)
        char buffer[JMSG_LENGTH_MAX];
 
        (*cinfo->err->format_message) (cinfo, buffer);
-       TIFFError("JPEGLib", buffer);           /* display the error message */
+       TIFFErrorExt(sp->tif->tif_clientdata, "JPEGLib", "%s", buffer);         /* display the error message */
        jpeg_abort(cinfo);                      /* clean up libjpeg state */
        LONGJMP(sp->exit_jmpbuf, 1);            /* return to libtiff caller */
 }
@@ -227,7 +242,7 @@ TIFFjpeg_output_message(j_common_ptr cinfo)
        char buffer[JMSG_LENGTH_MAX];
 
        (*cinfo->err->format_message) (cinfo, buffer);
-       TIFFWarning("JPEGLib", buffer);
+       TIFFWarningExt(((JPEGState *) cinfo)->tif->tif_clientdata, "JPEGLib", "%s", buffer);
 }
 
 /*
@@ -480,7 +495,7 @@ TIFFjpeg_tables_dest(JPEGState* sp, TIFF* tif)
        sp->jpegtables = (void*) _TIFFmalloc((tsize_t) sp->jpegtables_length);
        if (sp->jpegtables == NULL) {
                sp->jpegtables_length = 0;
-               TIFFError("TIFFjpeg_tables_dest", "No space for JPEGTables");
+               TIFFErrorExt(sp->tif->tif_clientdata, "TIFFjpeg_tables_dest", "No space for JPEGTables");
                return (0);
        }
        sp->cinfo.c.dest = &sp->dest;
@@ -626,7 +641,7 @@ JPEGSetupDecode(TIFF* tif)
        JPEGState* sp = JState(tif);
        TIFFDirectory *td = &tif->tif_dir;
 
-        JPEGInitializeLibJPEG( tif );
+        JPEGInitializeLibJPEG( tif, 0, 1 );
 
        assert(sp != NULL);
        assert(sp->cinfo.comm.is_decompressor);
@@ -635,7 +650,7 @@ JPEGSetupDecode(TIFF* tif)
        if (TIFFFieldSet(tif,FIELD_JPEGTABLES)) {
                TIFFjpeg_tables_src(sp, tif);
                if(TIFFjpeg_read_header(sp,FALSE) != JPEG_HEADER_TABLES_ONLY) {
-                       TIFFError("JPEGSetupDecode", "Bogus JPEGTables field");
+                       TIFFErrorExt(tif->tif_clientdata, "JPEGSetupDecode", "Bogus JPEGTables field");
                        return (0);
                }
        }
@@ -698,7 +713,7 @@ JPEGPreDecode(TIFF* tif, tsample_t s)
        } else {
                if (segment_height > td->td_rowsperstrip)
                        segment_height = td->td_rowsperstrip;
-               sp->bytesperline = TIFFScanlineSize(tif);
+               sp->bytesperline = TIFFOldScanlineSize(tif);
        }
        if (td->td_planarconfig == PLANARCONFIG_SEPARATE && s > 0) {
                /*
@@ -708,36 +723,78 @@ JPEGPreDecode(TIFF* tif, tsample_t s)
                segment_width = TIFFhowmany(segment_width, sp->h_sampling);
                segment_height = TIFFhowmany(segment_height, sp->v_sampling);
        }
-       if (sp->cinfo.d.image_width != segment_width ||
-           sp->cinfo.d.image_height != segment_height) {
-               TIFFWarning(module, 
-                 "Improper JPEG strip/tile size, expected %dx%d, got %dx%d",
-                          segment_width, 
-                          segment_height,
-                          sp->cinfo.d.image_width, 
-                          sp->cinfo.d.image_height);
+       if (sp->cinfo.d.image_width < segment_width ||
+           sp->cinfo.d.image_height < segment_height) {
+               TIFFWarningExt(tif->tif_clientdata, module,
+                              "Improper JPEG strip/tile size, "
+                              "expected %dx%d, got %dx%d",
+                              segment_width, segment_height,
+                              sp->cinfo.d.image_width,
+                              sp->cinfo.d.image_height);
+       } 
+       if (sp->cinfo.d.image_width > segment_width ||
+           sp->cinfo.d.image_height > segment_height) {
+               /*
+                * This case could be dangerous, if the strip or tile size has
+                * been reported as less than the amount of data jpeg will
+                * return, some potential security issues arise. Catch this
+                * case and error out.
+                */
+               TIFFErrorExt(tif->tif_clientdata, module,
+                            "JPEG strip/tile size exceeds expected dimensions,"
+                            " expected %dx%d, got %dx%d",
+                            segment_width, segment_height,
+                            sp->cinfo.d.image_width, sp->cinfo.d.image_height);
+               return (0);
        }
        if (sp->cinfo.d.num_components !=
            (td->td_planarconfig == PLANARCONFIG_CONTIG ?
             td->td_samplesperpixel : 1)) {
-               TIFFError(module, "Improper JPEG component count");
+               TIFFErrorExt(tif->tif_clientdata, module, "Improper JPEG component count");
                return (0);
        }
+#ifdef JPEG_LIB_MK1
+       if (12 != td->td_bitspersample && 8 != td->td_bitspersample) {
+                       TIFFErrorExt(tif->tif_clientdata, module, "Improper JPEG data precision");
+            return (0);
+       }
+        sp->cinfo.d.data_precision = td->td_bitspersample;
+        sp->cinfo.d.bits_in_jsample = td->td_bitspersample;
+#else
        if (sp->cinfo.d.data_precision != td->td_bitspersample) {
-               TIFFError(module, "Improper JPEG data precision");
-               return (0);
+                       TIFFErrorExt(tif->tif_clientdata, module, "Improper JPEG data precision");
+            return (0);
        }
+#endif
        if (td->td_planarconfig == PLANARCONFIG_CONTIG) {
                /* Component 0 should have expected sampling factors */
                if (sp->cinfo.d.comp_info[0].h_samp_factor != sp->h_sampling ||
                    sp->cinfo.d.comp_info[0].v_samp_factor != sp->v_sampling) {
-                           TIFFWarning(module, 
+                               TIFFWarningExt(tif->tif_clientdata, module,
                                     "Improper JPEG sampling factors %d,%d\n"
                                     "Apparently should be %d,%d.",
                                     sp->cinfo.d.comp_info[0].h_samp_factor,
                                     sp->cinfo.d.comp_info[0].v_samp_factor,
                                     sp->h_sampling, sp->v_sampling);
 
+                               /*
+                                * There are potential security issues here
+                                * for decoders that have already allocated
+                                * buffers based on the expected sampling
+                                * factors. Lets check the sampling factors
+                                * dont exceed what we were expecting.
+                                */
+                               if (sp->cinfo.d.comp_info[0].h_samp_factor
+                                       > sp->h_sampling
+                                   || sp->cinfo.d.comp_info[0].v_samp_factor
+                                       > sp->v_sampling) {
+                                       TIFFErrorExt(tif->tif_clientdata,
+                                                    module,
+                                       "Cannot honour JPEG sampling factors"
+                                       " that exceed those specified.");
+                                       return (0);
+                               }
+
                            /*
                             * XXX: Files written by the Intergraph software
                             * has different sampling factors stored in the
@@ -746,7 +803,7 @@ JPEGPreDecode(TIFF* tif, tsample_t s)
                             * of the tag 33918.
                             */
                            if (!_TIFFFindFieldInfo(tif, 33918, TIFF_ANY)) {
-                                   TIFFWarning(module, 
+                                       TIFFWarningExt(tif->tif_clientdata, module,
                                        "Decompressor will try reading with "
                                        "sampling %d,%d.",
                                        sp->cinfo.d.comp_info[0].h_samp_factor,
@@ -762,7 +819,7 @@ JPEGPreDecode(TIFF* tif, tsample_t s)
                for (ci = 1; ci < sp->cinfo.d.num_components; ci++) {
                        if (sp->cinfo.d.comp_info[ci].h_samp_factor != 1 ||
                            sp->cinfo.d.comp_info[ci].v_samp_factor != 1) {
-                               TIFFError(module, "Improper JPEG sampling factors");
+                               TIFFErrorExt(tif->tif_clientdata, module, "Improper JPEG sampling factors");
                                return (0);
                        }
                }
@@ -770,7 +827,7 @@ JPEGPreDecode(TIFF* tif, tsample_t s)
                /* PC 2's single component should have sampling factors 1,1 */
                if (sp->cinfo.d.comp_info[0].h_samp_factor != 1 ||
                    sp->cinfo.d.comp_info[0].v_samp_factor != 1) {
-                       TIFFError(module, "Improper JPEG sampling factors");
+                       TIFFErrorExt(tif->tif_clientdata, module, "Improper JPEG sampling factors");
                        return (0);
                }
        }
@@ -825,10 +882,11 @@ JPEGDecode(TIFF* tif, tidata_t buf, tsize_t cc, tsample_t s)
 {
     JPEGState *sp = JState(tif);
     tsize_t nrows;
+    (void) s;
 
     nrows = cc / sp->bytesperline;
     if (cc % sp->bytesperline)
-        TIFFWarning(tif->tif_name, "fractional scanline not read");
+               TIFFWarningExt(tif->tif_clientdata, tif->tif_name, "fractional scanline not read");
 
     if( nrows > (int) sp->cinfo.d.image_height )
         nrows = sp->cinfo.d.image_height;
@@ -836,16 +894,84 @@ JPEGDecode(TIFF* tif, tidata_t buf, tsize_t cc, tsample_t s)
     /* data is expected to be read in multiples of a scanline */
     if (nrows)
     {
+        JSAMPROW line_work_buf = NULL;
+
+        /*
+        ** For 6B, only use temporary buffer for 12 bit imagery. 
+        ** For Mk1 always use it. 
+        */
+#if !defined(JPEG_LIB_MK1)        
+        if( sp->cinfo.d.data_precision == 12 )
+#endif
+        {
+            line_work_buf = (JSAMPROW) 
+                _TIFFmalloc(sizeof(short) * sp->cinfo.d.output_width 
+                            * sp->cinfo.d.num_components );
+        }
+
         do {
-            JSAMPROW bufptr = (JSAMPROW)buf;
+            if( line_work_buf != NULL )
+            {
+                /* 
+                ** In the MK1 case, we aways read into a 16bit buffer, and then
+                ** pack down to 12bit or 8bit.  In 6B case we only read into 16
+                ** bit buffer for 12bit data, which we need to repack. 
+                */
+                if (TIFFjpeg_read_scanlines(sp, &line_work_buf, 1) != 1)
+                    return (0);
+
+                if( sp->cinfo.d.data_precision == 12 )
+                {
+                    int value_pairs = (sp->cinfo.d.output_width 
+                                       * sp->cinfo.d.num_components) / 2;
+                    int iPair;
+
+                    for( iPair = 0; iPair < value_pairs; iPair++ )
+                    {
+                        unsigned char *out_ptr = 
+                            ((unsigned char *) buf) + iPair * 3;
+                        JSAMPLE *in_ptr = line_work_buf + iPair * 2;
+
+                        out_ptr[0] = (in_ptr[0] & 0xff0) >> 4;
+                        out_ptr[1] = ((in_ptr[0] & 0xf) << 4)
+                            | ((in_ptr[1] & 0xf00) >> 8);
+                        out_ptr[2] = ((in_ptr[1] & 0xff) >> 0);
+                    }
+                }
+                else if( sp->cinfo.d.data_precision == 8 )
+                {
+                    int value_count = (sp->cinfo.d.output_width 
+                                       * sp->cinfo.d.num_components);
+                    int iValue;
+
+                    for( iValue = 0; iValue < value_count; iValue++ )
+                    {
+                        ((unsigned char *) buf)[iValue] = 
+                            line_work_buf[iValue] & 0xff;
+                    }
+                }
+            }
+            else
+            {
+                /*
+                ** In the libjpeg6b 8bit case.  We read directly into the 
+                ** TIFF buffer.
+                */
+                JSAMPROW bufptr = (JSAMPROW)buf;
+  
+                if (TIFFjpeg_read_scanlines(sp, &bufptr, 1) != 1)
+                    return (0);
+            }
 
-            if (TIFFjpeg_read_scanlines(sp, &bufptr, 1) != 1)
-                return (0);
             ++tif->tif_row;
             buf += sp->bytesperline;
             cc -= sp->bytesperline;
         } while (--nrows > 0);
+
+        if( line_work_buf != NULL )
+            _TIFFfree( line_work_buf );
     }
+
     /* Close down the decompressor if we've finished the strip or tile. */
     return sp->cinfo.d.output_scanline < sp->cinfo.d.output_height
         || TIFFjpeg_finish_decompress(sp);
@@ -860,13 +986,20 @@ JPEGDecodeRaw(TIFF* tif, tidata_t buf, tsize_t cc, tsample_t s)
 {
        JPEGState *sp = JState(tif);
        tsize_t nrows;
+       (void) s;
 
        /* data is expected to be read in multiples of a scanline */
        if ( (nrows = sp->cinfo.d.image_height) ) {
                /* Cb,Cr both have sampling factors 1, so this is correct */
-               JDIMENSION clumps_per_line = sp->cinfo.d.comp_info[1].downsampled_width;
+               JDIMENSION clumps_per_line = sp->cinfo.d.comp_info[1].downsampled_width;            
                int samples_per_clump = sp->samplesperclump;
-       
+
+#ifdef JPEG_LIB_MK1
+               unsigned short* tmpbuf = _TIFFmalloc(sizeof(unsigned short) *
+                   sp->cinfo.d.output_width *
+                   sp->cinfo.d.num_components);
+#endif
+
                do {
                        jpeg_component_info *compptr;
                        int ci, clumpoffset;
@@ -874,9 +1007,7 @@ JPEGDecodeRaw(TIFF* tif, tidata_t buf, tsize_t cc, tsample_t s)
                        /* Reload downsampled-data buffer if needed */
                        if (sp->scancount >= DCTSIZE) {
                                int n = sp->cinfo.d.max_v_samp_factor * DCTSIZE;
-
-                               if (TIFFjpeg_read_raw_data(sp, sp->ds_buffer, n)
-                                       != n)
+                               if (TIFFjpeg_read_raw_data(sp, sp->ds_buffer, n) != n)
                                        return (0);
                                sp->scancount = 0;
                        }
@@ -884,47 +1015,89 @@ JPEGDecodeRaw(TIFF* tif, tidata_t buf, tsize_t cc, tsample_t s)
                         * Fastest way to unseparate data is to make one pass
                         * over the scanline for each row of each component.
                         */
-                       clumpoffset = 0;        /* first sample in clump */
+                       clumpoffset = 0;    /* first sample in clump */
                        for (ci = 0, compptr = sp->cinfo.d.comp_info;
-                            ci < sp->cinfo.d.num_components;
-                            ci++, compptr++) {
-                           int hsamp = compptr->h_samp_factor;
-                           int vsamp = compptr->v_samp_factor;
-                           int ypos;
-
-                           for (ypos = 0; ypos < vsamp; ypos++) {
-                               JSAMPLE *inptr = sp->ds_buffer[ci][sp->scancount*vsamp + ypos];
-                               JSAMPLE *outptr = (JSAMPLE*)buf + clumpoffset;
-                               JDIMENSION nclump;
-
-                               if (hsamp == 1) {
-                                   /* fast path for at least Cb and Cr */
-                                   for (nclump = clumps_per_line; nclump-- > 0; ) {
-                                       outptr[0] = *inptr++;
-                                       outptr += samples_per_clump;
-                                   }
-                               } else {
-                                       int xpos;
-
-                                   /* general case */
-                                   for (nclump = clumps_per_line; nclump-- > 0; ) {
-                                       for (xpos = 0; xpos < hsamp; xpos++)
-                                           outptr[xpos] = *inptr++;
-                                       outptr += samples_per_clump;
-                                   }
+                           ci < sp->cinfo.d.num_components;
+                           ci++, compptr++) {
+                               int hsamp = compptr->h_samp_factor;
+                               int vsamp = compptr->v_samp_factor;
+                               int ypos;
+
+                               for (ypos = 0; ypos < vsamp; ypos++) {
+                                       JSAMPLE *inptr = sp->ds_buffer[ci][sp->scancount*vsamp + ypos];
+#ifdef JPEG_LIB_MK1
+                                       JSAMPLE *outptr = (JSAMPLE*)tmpbuf + clumpoffset;
+#else
+                                       JSAMPLE *outptr = (JSAMPLE*)buf + clumpoffset;
+#endif
+                                       JDIMENSION nclump;
+
+                                       if (hsamp == 1) {
+                                               /* fast path for at least Cb and Cr */
+                                               for (nclump = clumps_per_line; nclump-- > 0; ) {
+                                                       outptr[0] = *inptr++;
+                                                       outptr += samples_per_clump;
+                                               }
+                                       } else {
+                                               int xpos;
+
+                       /* general case */
+                                               for (nclump = clumps_per_line; nclump-- > 0; ) {
+                                                       for (xpos = 0; xpos < hsamp; xpos++)
+                                                               outptr[xpos] = *inptr++;
+                                                       outptr += samples_per_clump;
+                                               }
+                                       }
+                                       clumpoffset += hsamp;
+                               }
+                       }
+
+#ifdef JPEG_LIB_MK1
+                       {
+                               if (sp->cinfo.d.data_precision == 8)
+                               {
+                                       int i=0;
+                                       int len = sp->cinfo.d.output_width * sp->cinfo.d.num_components;
+                                       for (i=0; i<len; i++)
+                                       {
+                                               ((unsigned char*)buf)[i] = tmpbuf[i] & 0xff;
+                                       }
+                               }
+                               else
+                               {         // 12-bit
+                                       int value_pairs = (sp->cinfo.d.output_width
+                                           * sp->cinfo.d.num_components) / 2;
+                                       int iPair;
+                                       for( iPair = 0; iPair < value_pairs; iPair++ )
+                                       {
+                                               unsigned char *out_ptr = ((unsigned char *) buf) + iPair * 3;
+                                               JSAMPLE *in_ptr = tmpbuf + iPair * 2;
+                                               out_ptr[0] = (in_ptr[0] & 0xff0) >> 4;
+                                               out_ptr[1] = ((in_ptr[0] & 0xf) << 4)
+                                                   | ((in_ptr[1] & 0xf00) >> 8);
+                                               out_ptr[2] = ((in_ptr[1] & 0xff) >> 0);
+                                       }
                                }
-                               clumpoffset += hsamp;
-                           }
                        }
-                       ++sp->scancount;
-                       ++tif->tif_row;
+#endif
+
+                       sp->scancount ++;
+                       tif->tif_row += sp->v_sampling;
+                       /* increment/decrement of buf and cc is still incorrect, but should not matter
+                        * TODO: resolve this */
                        buf += sp->bytesperline;
                        cc -= sp->bytesperline;
-               } while (--nrows > 0);
+                       nrows -= sp->v_sampling;
+               } while (nrows > 0);
+
+#ifdef JPEG_LIB_MK1
+               _TIFFfree(tmpbuf);
+#endif
+
        }
 
-        /* Close down the decompressor if done. */
-        return sp->cinfo.d.output_scanline < sp->cinfo.d.output_height
+       /* Close down the decompressor if done. */
+       return sp->cinfo.d.output_scanline < sp->cinfo.d.output_height
            || TIFFjpeg_finish_decompress(sp);
 }
 
@@ -958,7 +1131,7 @@ prepare_JPEGTables(TIFF* tif)
 {
        JPEGState* sp = JState(tif);
 
-        JPEGInitializeLibJPEG( tif );
+        JPEGInitializeLibJPEG( tif, 0, 0 );
 
        /* Initialize quant tables for current quality setting */
        if (!TIFFjpeg_set_quality(sp, sp->jpegquality, FALSE))
@@ -994,7 +1167,7 @@ JPEGSetupEncode(TIFF* tif)
        TIFFDirectory *td = &tif->tif_dir;
        static const char module[] = "JPEGSetupEncode";
 
-        JPEGInitializeLibJPEG( tif );
+        JPEGInitializeLibJPEG( tif, 1, 0 );
 
        assert(sp != NULL);
        assert(!sp->cinfo.comm.is_decompressor);
@@ -1019,21 +1192,26 @@ JPEGSetupEncode(TIFF* tif)
                 * default value is inappropriate for YCbCr.  Fill in the
                 * proper value if application didn't set it.
                 */
-               if (!TIFFFieldSet(tif, FIELD_REFBLACKWHITE)) {
-                       float refbw[6];
-                       long top = 1L << td->td_bitspersample;
-                       refbw[0] = 0;
-                       refbw[1] = (float)(top-1L);
-                       refbw[2] = (float)(top>>1);
-                       refbw[3] = refbw[1];
-                       refbw[4] = refbw[2];
-                       refbw[5] = refbw[1];
-                       TIFFSetField(tif, TIFFTAG_REFERENCEBLACKWHITE, refbw);
+               {
+                       float *ref;
+                       if (!TIFFGetField(tif, TIFFTAG_REFERENCEBLACKWHITE,
+                                         &ref)) {
+                               float refbw[6];
+                               long top = 1L << td->td_bitspersample;
+                               refbw[0] = 0;
+                               refbw[1] = (float)(top-1L);
+                               refbw[2] = (float)(top>>1);
+                               refbw[3] = refbw[1];
+                               refbw[4] = refbw[2];
+                               refbw[5] = refbw[1];
+                               TIFFSetField(tif, TIFFTAG_REFERENCEBLACKWHITE,
+                                            refbw);
+                       }
                }
                break;
        case PHOTOMETRIC_PALETTE:               /* disallowed by Tech Note */
        case PHOTOMETRIC_MASK:
-               TIFFError(module,
+               TIFFErrorExt(tif->tif_clientdata, module,
                          "PhotometricInterpretation %d not allowed for JPEG",
                          (int) sp->photometric);
                return (0);
@@ -1043,7 +1221,7 @@ JPEGSetupEncode(TIFF* tif)
                sp->v_sampling = 1;
                break;
        }
-       
+
        /* Verify miscellaneous parameters */
 
        /*
@@ -1051,21 +1229,30 @@ JPEGSetupEncode(TIFF* tif)
         * depths for different components, or if libjpeg ever supports
         * run-time selection of depth.  Neither is imminent.
         */
-       if (td->td_bitspersample != BITS_IN_JSAMPLE) {
-               TIFFError(module, "BitsPerSample %d not allowed for JPEG",
+#ifdef JPEG_LIB_MK1
+        /* BITS_IN_JSAMPLE now permits 8 and 12 --- dgilbert */
+       if (td->td_bitspersample != 8 && td->td_bitspersample != 12) 
+#else
+       if (td->td_bitspersample != BITS_IN_JSAMPLE )
+#endif
+       {
+               TIFFErrorExt(tif->tif_clientdata, module, "BitsPerSample %d not allowed for JPEG",
                          (int) td->td_bitspersample);
                return (0);
        }
        sp->cinfo.c.data_precision = td->td_bitspersample;
+#ifdef JPEG_LIB_MK1
+        sp->cinfo.c.bits_in_jsample = td->td_bitspersample;
+#endif
        if (isTiled(tif)) {
                if ((td->td_tilelength % (sp->v_sampling * DCTSIZE)) != 0) {
-                       TIFFError(module,
+                       TIFFErrorExt(tif->tif_clientdata, module,
                                  "JPEG tile height must be multiple of %d",
                                  sp->v_sampling * DCTSIZE);
                        return (0);
                }
                if ((td->td_tilewidth % (sp->h_sampling * DCTSIZE)) != 0) {
-                       TIFFError(module,
+                       TIFFErrorExt(tif->tif_clientdata, module,
                                  "JPEG tile width must be multiple of %d",
                                  sp->h_sampling * DCTSIZE);
                        return (0);
@@ -1073,7 +1260,7 @@ JPEGSetupEncode(TIFF* tif)
        } else {
                if (td->td_rowsperstrip < td->td_imagelength &&
                    (td->td_rowsperstrip % (sp->v_sampling * DCTSIZE)) != 0) {
-                       TIFFError(module,
+                       TIFFErrorExt(tif->tif_clientdata, module,
                                  "RowsPerStrip must be multiple of %d for JPEG",
                                  sp->v_sampling * DCTSIZE);
                        return (0);
@@ -1082,12 +1269,16 @@ JPEGSetupEncode(TIFF* tif)
 
        /* Create a JPEGTables field if appropriate */
        if (sp->jpegtablesmode & (JPEGTABLESMODE_QUANT|JPEGTABLESMODE_HUFF)) {
-               if (!prepare_JPEGTables(tif))
-                       return (0);
-               /* Mark the field present */
-               /* Can't use TIFFSetField since BEENWRITING is already set! */
-               TIFFSetFieldBit(tif, FIELD_JPEGTABLES);
-               tif->tif_flags |= TIFF_DIRTYDIRECT;
+                if( sp->jpegtables == NULL
+                    || memcmp(sp->jpegtables,"\0\0\0\0\0\0\0\0\0",8) == 0 )
+                {
+                        if (!prepare_JPEGTables(tif))
+                                return (0);
+                        /* Mark the field present */
+                        /* Can't use TIFFSetField since BEENWRITING is already set! */
+                        tif->tif_flags |= TIFF_DIRTYDIRECT;
+                        TIFFSetFieldBit(tif, FIELD_JPEGTABLES);
+                }
        } else {
                /* We do not support application-supplied JPEGTables, */
                /* so mark the field not present */
@@ -1126,7 +1317,7 @@ JPEGPreEncode(TIFF* tif, tsample_t s)
                segment_height = td->td_imagelength - tif->tif_row;
                if (segment_height > td->td_rowsperstrip)
                        segment_height = td->td_rowsperstrip;
-               sp->bytesperline = TIFFScanlineSize(tif);
+               sp->bytesperline = TIFFOldScanlineSize(tif);
        }
        if (td->td_planarconfig == PLANARCONFIG_SEPARATE && s > 0) {
                /* for PC 2, scale down the strip/tile size
@@ -1136,7 +1327,7 @@ JPEGPreEncode(TIFF* tif, tsample_t s)
                segment_height = TIFFhowmany(segment_height, sp->v_sampling);
        }
        if (segment_width > 65535 || segment_height > 65535) {
-               TIFFError(module, "Strip/tile too large for JPEG");
+               TIFFErrorExt(tif->tif_clientdata, module, "Strip/tile too large for JPEG");
                return (0);
        }
        sp->cinfo.c.image_width = segment_width;
@@ -1183,9 +1374,9 @@ JPEGPreEncode(TIFF* tif, tsample_t s)
        sp->cinfo.c.write_JFIF_header = FALSE;
        sp->cinfo.c.write_Adobe_marker = FALSE;
        /* set up table handling correctly */
+        if (!TIFFjpeg_set_quality(sp, sp->jpegquality, FALSE))
+                return (0);
        if (! (sp->jpegtablesmode & JPEGTABLESMODE_QUANT)) {
-               if (!TIFFjpeg_set_quality(sp, sp->jpegquality, FALSE))
-                       return (0);
                unsuppress_quant_table(sp, 0);
                unsuppress_quant_table(sp, 1);
        }
@@ -1236,7 +1427,11 @@ JPEGEncode(TIFF* tif, tidata_t buf, tsize_t cc, tsample_t s)
        /* data is expected to be supplied in multiples of a scanline */
        nrows = cc / sp->bytesperline;
        if (cc % sp->bytesperline)
-               TIFFWarning(tif->tif_name, "fractional scanline discarded");
+               TIFFWarningExt(tif->tif_clientdata, tif->tif_name, "fractional scanline discarded");
+
+        /* The last strip will be limited to image size */
+        if( !isTiled(tif) && tif->tif_row+nrows > tif->tif_dir.td_imagelength )
+            nrows = tif->tif_dir.td_imagelength - tif->tif_row;
 
        while (nrows-- > 0) {
                bufptr[0] = (JSAMPROW) buf;
@@ -1264,18 +1459,25 @@ JPEGEncodeRaw(TIFF* tif, tidata_t buf, tsize_t cc, tsample_t s)
        int clumpoffset, ci, xpos, ypos;
        jpeg_component_info* compptr;
        int samples_per_clump = sp->samplesperclump;
+       tsize_t bytesperclumpline;
 
        (void) s;
        assert(sp != NULL);
-       /* data is expected to be supplied in multiples of a scanline */
-       nrows = cc / sp->bytesperline;
-       if (cc % sp->bytesperline)
-               TIFFWarning(tif->tif_name, "fractional scanline discarded");
+       /* data is expected to be supplied in multiples of a clumpline */
+       /* a clumpline is equivalent to v_sampling desubsampled scanlines */
+       /* TODO: the following calculation of bytesperclumpline, should substitute calculation of sp->bytesperline, except that it is per v_sampling lines */
+       bytesperclumpline = (((sp->cinfo.c.image_width+sp->h_sampling-1)/sp->h_sampling)
+                            *(sp->h_sampling*sp->v_sampling+2)*sp->cinfo.c.data_precision+7)
+                           /8;
+
+       nrows = ( cc / bytesperclumpline ) * sp->v_sampling;
+       if (cc % bytesperclumpline)
+               TIFFWarningExt(tif->tif_clientdata, tif->tif_name, "fractional scanline discarded");
 
        /* Cb,Cr both have sampling factors 1, so this is correct */
        clumps_per_line = sp->cinfo.c.comp_info[1].downsampled_width;
 
-       while (nrows-- > 0) {
+       while (nrows > 0) {
                /*
                 * Fastest way to separate the data is to make one pass
                 * over the scanline for each row of each component.
@@ -1320,9 +1522,9 @@ JPEGEncodeRaw(TIFF* tif, tidata_t buf, tsize_t cc, tsample_t s)
                                return (0);
                        sp->scancount = 0;
                }
-               if (nrows > 0)
-                       tif->tif_row++;
+               tif->tif_row += sp->v_sampling;
                buf += sp->bytesperline;
+               nrows -= sp->v_sampling;
        }
        return (1);
 }
@@ -1368,24 +1570,69 @@ JPEGPostEncode(TIFF* tif)
 static void
 JPEGCleanup(TIFF* tif)
 {
-       if (tif->tif_data) {
-               JPEGState *sp = JState(tif);
-                if( sp->cinfo_initialized )
-                    TIFFjpeg_destroy(sp);      /* release libjpeg resources */
-               if (sp->jpegtables)             /* tag value */
-                       _TIFFfree(sp->jpegtables);
-               _TIFFfree(tif->tif_data);       /* release local state */
-               tif->tif_data = NULL;
+       JPEGState *sp = JState(tif);
+       
+       assert(sp != 0);
+
+       tif->tif_tagmethods.vgetfield = sp->vgetparent;
+       tif->tif_tagmethods.vsetfield = sp->vsetparent;
+       tif->tif_tagmethods.printdir = sp->printdir;
+
+       if( sp->cinfo_initialized )
+           TIFFjpeg_destroy(sp);       /* release libjpeg resources */
+       if (sp->jpegtables)             /* tag value */
+               _TIFFfree(sp->jpegtables);
+       _TIFFfree(tif->tif_data);       /* release local state */
+       tif->tif_data = NULL;
+
+       _TIFFSetDefaultCompressionState(tif);
+}
+
+static void 
+JPEGResetUpsampled( TIFF* tif )
+{
+       JPEGState* sp = JState(tif);
+       TIFFDirectory* td = &tif->tif_dir;
+
+       /*
+        * Mark whether returned data is up-sampled or not so TIFFStripSize
+        * and TIFFTileSize return values that reflect the true amount of
+        * data.
+        */
+       tif->tif_flags &= ~TIFF_UPSAMPLED;
+       if (td->td_planarconfig == PLANARCONFIG_CONTIG) {
+               if (td->td_photometric == PHOTOMETRIC_YCBCR &&
+                   sp->jpegcolormode == JPEGCOLORMODE_RGB) {
+                       tif->tif_flags |= TIFF_UPSAMPLED;
+               } else {
+#ifdef notdef
+                       if (td->td_ycbcrsubsampling[0] != 1 ||
+                           td->td_ycbcrsubsampling[1] != 1)
+                               ; /* XXX what about up-sampling? */
+#endif
+               }
        }
+
+       /*
+        * Must recalculate cached tile size in case sampling state changed.
+        * Should we really be doing this now if image size isn't set? 
+        */
+        if( tif->tif_tilesize > 0 )
+            tif->tif_tilesize = isTiled(tif) ? TIFFTileSize(tif) : (tsize_t) -1;
+
+        if(tif->tif_scanlinesize > 0 )
+            tif->tif_scanlinesize = TIFFScanlineSize(tif); 
 }
 
 static int
 JPEGVSetField(TIFF* tif, ttag_t tag, va_list ap)
 {
        JPEGState* sp = JState(tif);
-       TIFFDirectory* td = &tif->tif_dir;
+       const TIFFFieldInfo* fip;
        uint32 v32;
 
+       assert(sp != NULL);
+
        switch (tag) {
        case TIFFTAG_JPEGTABLES:
                v32 = va_arg(ap, uint32);
@@ -1403,34 +1650,21 @@ JPEGVSetField(TIFF* tif, ttag_t tag, va_list ap)
                return (1);                     /* pseudo tag */
        case TIFFTAG_JPEGCOLORMODE:
                sp->jpegcolormode = va_arg(ap, int);
-               /*
-                * Mark whether returned data is up-sampled or not
-                * so TIFFStripSize and TIFFTileSize return values
-                * that reflect the true amount of data.
-                */
-               tif->tif_flags &= ~TIFF_UPSAMPLED;
-               if (td->td_planarconfig == PLANARCONFIG_CONTIG) {
-                   if (td->td_photometric == PHOTOMETRIC_YCBCR &&
-                     sp->jpegcolormode == JPEGCOLORMODE_RGB) {
-                       tif->tif_flags |= TIFF_UPSAMPLED;
-                   } else {
-                       if (td->td_ycbcrsubsampling[0] != 1 ||
-                           td->td_ycbcrsubsampling[1] != 1)
-                           ; /* XXX what about up-sampling? */
-                   }
-               }
-               /*
-                * Must recalculate cached tile size
-                * in case sampling state changed.
-                */
-               tif->tif_tilesize = isTiled(tif) ? TIFFTileSize(tif) : (tsize_t) -1;
+                JPEGResetUpsampled( tif );
                return (1);                     /* pseudo tag */
+       case TIFFTAG_PHOTOMETRIC:
+        {
+                int ret_value = (*sp->vsetparent)(tif, tag, ap);
+                JPEGResetUpsampled( tif );
+                return ret_value;
+        }
        case TIFFTAG_JPEGTABLESMODE:
                sp->jpegtablesmode = va_arg(ap, int);
                return (1);                     /* pseudo tag */
        case TIFFTAG_YCBCRSUBSAMPLING:
                 /* mark the fact that we have a real ycbcrsubsampling! */
                sp->ycbcrsampling_fetched = 1;
+                /* should we be recomputing upsampling info here? */
                return (*sp->vsetparent)(tif, tag, ap);
        case TIFFTAG_FAXRECVPARAMS:
                sp->recvparams = va_arg(ap, uint32);
@@ -1447,7 +1681,13 @@ JPEGVSetField(TIFF* tif, ttag_t tag, va_list ap)
        default:
                return (*sp->vsetparent)(tif, tag, ap);
        }
-       TIFFSetFieldBit(tif, _TIFFFieldWithTag(tif, tag)->field_bit);
+
+       if ((fip = _TIFFFieldWithTag(tif, tag))) {
+               TIFFSetFieldBit(tif, fip->field_bit);
+       } else {
+               return (0);
+       }
+
        tif->tif_flags |= TIFF_DIRTYDIRECT;
        return (1);
 }
@@ -1489,7 +1729,7 @@ JPEGFixupTestSubsampling( TIFF * tif )
     JPEGState *sp = JState(tif);
     TIFFDirectory *td = &tif->tif_dir;
 
-    JPEGInitializeLibJPEG( tif );
+    JPEGInitializeLibJPEG( tif, 0, 0 );
 
     /*
      * Some JPEG-in-TIFF files don't provide the ycbcrsampling tags, 
@@ -1506,7 +1746,7 @@ JPEGFixupTestSubsampling( TIFF * tif )
     if( TIFFIsTiled( tif ) )
     {
         if( !TIFFFillTile( tif, 0 ) )
-            return;
+                       return;
     }
     else
     {
@@ -1516,6 +1756,14 @@ JPEGFixupTestSubsampling( TIFF * tif )
 
     TIFFSetField( tif, TIFFTAG_YCBCRSUBSAMPLING, 
                   (uint16) sp->h_sampling, (uint16) sp->v_sampling );
+
+    /*
+    ** We want to clear the loaded strip so the application has time
+    ** to set JPEGCOLORMODE or other behavior modifiers.  This essentially
+    ** undoes the JPEGPreDecode triggers by TIFFFileStrip().  (#1936)
+    */
+    tif->tif_curstrip = -1;
+
 #endif /* CHECK_JPEG_YCBCR_SUBSAMPLING */
 }
 
@@ -1524,38 +1772,39 @@ JPEGVGetField(TIFF* tif, ttag_t tag, va_list ap)
 {
        JPEGState* sp = JState(tif);
 
+       assert(sp != NULL);
+
        switch (tag) {
-       case TIFFTAG_JPEGTABLES:
-               *va_arg(ap, uint32*) = sp->jpegtables_length;
-               *va_arg(ap, void**) = sp->jpegtables;
-               break;
-       case TIFFTAG_JPEGQUALITY:
-               *va_arg(ap, int*) = sp->jpegquality;
-               break;
-       case TIFFTAG_JPEGCOLORMODE:
-               *va_arg(ap, int*) = sp->jpegcolormode;
-               break;
-       case TIFFTAG_JPEGTABLESMODE:
-               *va_arg(ap, int*) = sp->jpegtablesmode;
-               break;
-       case TIFFTAG_YCBCRSUBSAMPLING:
-                JPEGFixupTestSubsampling( tif );
-               return (*sp->vgetparent)(tif, tag, ap);
-               break;
-        case TIFFTAG_FAXRECVPARAMS:
-                *va_arg(ap, uint32*) = sp->recvparams;
-                break;
-        case TIFFTAG_FAXSUBADDRESS:
-                *va_arg(ap, char**) = sp->subaddress;
-                break;
-        case TIFFTAG_FAXRECVTIME:
-                *va_arg(ap, uint32*) = sp->recvtime;
-                break;
-        case TIFFTAG_FAXDCS:
-                *va_arg(ap, char**) = sp->faxdcs;
-                break;
-default:
-               return (*sp->vgetparent)(tif, tag, ap);
+               case TIFFTAG_JPEGTABLES:
+                       *va_arg(ap, uint32*) = sp->jpegtables_length;
+                       *va_arg(ap, void**) = sp->jpegtables;
+                       break;
+               case TIFFTAG_JPEGQUALITY:
+                       *va_arg(ap, int*) = sp->jpegquality;
+                       break;
+               case TIFFTAG_JPEGCOLORMODE:
+                       *va_arg(ap, int*) = sp->jpegcolormode;
+                       break;
+               case TIFFTAG_JPEGTABLESMODE:
+                       *va_arg(ap, int*) = sp->jpegtablesmode;
+                       break;
+               case TIFFTAG_YCBCRSUBSAMPLING:
+                       JPEGFixupTestSubsampling( tif );
+                       return (*sp->vgetparent)(tif, tag, ap);
+               case TIFFTAG_FAXRECVPARAMS:
+                       *va_arg(ap, uint32*) = sp->recvparams;
+                       break;
+               case TIFFTAG_FAXSUBADDRESS:
+                       *va_arg(ap, char**) = sp->subaddress;
+                       break;
+               case TIFFTAG_FAXRECVTIME:
+                       *va_arg(ap, uint32*) = sp->recvtime;
+                       break;
+               case TIFFTAG_FAXDCS:
+                       *va_arg(ap, char**) = sp->faxdcs;
+                       break;
+               default:
+                       return (*sp->vgetparent)(tif, tag, ap);
        }
        return (1);
 }
@@ -1565,6 +1814,8 @@ JPEGPrintDir(TIFF* tif, FILE* fd, long flags)
 {
        JPEGState* sp = JState(tif);
 
+       assert(sp != NULL);
+
        (void) flags;
        if (TIFFFieldSet(tif,FIELD_JPEGTABLES))
                fprintf(fd, "  JPEG Tables: (%lu bytes)\n",
@@ -1626,14 +1877,25 @@ JPEGDefaultTileSize(TIFF* tif, uint32* tw, uint32* th)
  * NFW, Feb 3rd, 2003.
  */
 
-static int JPEGInitializeLibJPEG( TIFF * tif )
+static int JPEGInitializeLibJPEG( TIFF * tif, int force_encode, int force_decode )
 {
     JPEGState* sp = JState(tif);
     uint32 *byte_counts = NULL;
     int     data_is_empty = TRUE;
+    int     decompress;
+
 
-    if( sp->cinfo_initialized )
-        return 1;
+    if(sp->cinfo_initialized)
+    {
+        if( force_encode && sp->cinfo.comm.is_decompressor )
+            TIFFjpeg_destroy( sp );
+        else if( force_decode && !sp->cinfo.comm.is_decompressor )
+            TIFFjpeg_destroy( sp );
+        else
+            return 1;
+
+        sp->cinfo_initialized = 0;
+    }
 
     /*
      * Do we have tile data already?  Make sure we initialize the
@@ -1653,10 +1915,21 @@ static int JPEGInitializeLibJPEG( TIFF * tif )
         data_is_empty = byte_counts[0] == 0;
     }
 
+    if( force_decode )
+        decompress = 1;
+    else if( force_encode )
+        decompress = 0;
+    else if( tif->tif_mode == O_RDONLY )
+        decompress = 1;
+    else if( data_is_empty )
+        decompress = 0;
+    else
+        decompress = 1;
+
     /*
      * Initialize libjpeg.
      */
-    if (tif->tif_mode == O_RDONLY || !data_is_empty ) {
+    if ( decompress ) {
         if (!TIFFjpeg_create_decompress(sp))
             return (0);
 
@@ -1678,29 +1951,39 @@ TIFFInitJPEG(TIFF* tif, int scheme)
        assert(scheme == COMPRESSION_JPEG);
 
        /*
+        * Merge codec-specific tag information.
+        */
+       if (!_TIFFMergeFieldInfo(tif, jpegFieldInfo, N(jpegFieldInfo))) {
+               TIFFErrorExt(tif->tif_clientdata,
+                            "TIFFInitJPEG",
+                            "Merging JPEG codec-specific tags failed");
+               return 0;
+       }
+
+       /*
         * Allocate state block so tag methods have storage to record values.
         */
        tif->tif_data = (tidata_t) _TIFFmalloc(sizeof (JPEGState));
 
        if (tif->tif_data == NULL) {
-               TIFFError("TIFFInitJPEG", "No space for JPEG state block");
-               return (0);
+               TIFFErrorExt(tif->tif_clientdata,
+                            "TIFFInitJPEG", "No space for JPEG state block");
+               return 0;
        }
-        _TIFFmemset( tif->tif_data, 0, sizeof(JPEGState));
+        _TIFFmemset(tif->tif_data, 0, sizeof(JPEGState));
 
        sp = JState(tif);
        sp->tif = tif;                          /* back link */
 
        /*
-        * Merge codec-specific tag information and
-        * override parent get/set field methods.
+        * Override parent get/set field methods.
         */
-       _TIFFMergeFieldInfo(tif, jpegFieldInfo, N(jpegFieldInfo));
        sp->vgetparent = tif->tif_tagmethods.vgetfield;
-       tif->tif_tagmethods.vgetfield = JPEGVGetField;  /* hook for codec tags */
+       tif->tif_tagmethods.vgetfield = JPEGVGetField; /* hook for codec tags */
        sp->vsetparent = tif->tif_tagmethods.vsetfield;
-       tif->tif_tagmethods.vsetfield = JPEGVSetField;  /* hook for codec tags */
-       tif->tif_tagmethods.printdir = JPEGPrintDir;    /* hook for codec tags */
+       tif->tif_tagmethods.vsetfield = JPEGVSetField; /* hook for codec tags */
+       sp->printdir = tif->tif_tagmethods.printdir;
+       tif->tif_tagmethods.printdir = JPEGPrintDir;   /* hook for codec tags */
 
        /* Default values for codec-specific fields */
        sp->jpegtables = NULL;
@@ -1738,14 +2021,45 @@ TIFFInitJPEG(TIFF* tif, int scheme)
 
         sp->cinfo_initialized = FALSE;
 
+       /*
+        ** Create a JPEGTables field if no directory has yet been created. 
+        ** We do this just to ensure that sufficient space is reserved for
+        ** the JPEGTables field.  It will be properly created the right
+        ** size later. 
+        */
+        if( tif->tif_diroff == 0 )
+        {
+#define SIZE_OF_JPEGTABLES 2000
+/*
+The following line assumes incorrectly that all JPEG-in-TIFF files will have
+a JPEGTABLES tag generated and causes null-filled JPEGTABLES tags to be written
+when the JPEG data is placed with TIFFWriteRawStrip.  The field bit should be 
+set, anyway, later when actual JPEGTABLES header is generated, so removing it 
+here hopefully is harmless.
+            TIFFSetFieldBit(tif, FIELD_JPEGTABLES);
+*/
+            sp->jpegtables_length = SIZE_OF_JPEGTABLES;
+            sp->jpegtables = (void *) _TIFFmalloc(sp->jpegtables_length);
+           _TIFFmemset(sp->jpegtables, 0, SIZE_OF_JPEGTABLES);
+#undef SIZE_OF_JPEGTABLES
+        }
+
         /*
          * Mark the TIFFTAG_YCBCRSAMPLES as present even if it is not
          * see: JPEGFixupTestSubsampling().
          */
         TIFFSetFieldBit( tif, FIELD_YCBCRSUBSAMPLING );
 
-       return (1);
+       return 1;
 }
 #endif /* JPEG_SUPPORT */
 
 /* vim: set ts=8 sts=8 sw=8 noet: */
+
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
index 899efb8..eb622b9 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: tif_luv.c,v 1.1 2005-06-17 13:54:52 vp153 Exp $ */
+/* $Id: tif_luv.c,v 1.17.2.4 2010-06-08 18:50:42 bfriesen Exp $ */
 
 /*
  * Copyright (c) 1997 Greg Ward Larson
@@ -173,7 +173,6 @@ struct logLuvState {
 #define        DecoderState(tif)       ((LogLuvState*) (tif)->tif_data)
 #define        EncoderState(tif)       ((LogLuvState*) (tif)->tif_data)
 
-#define N(a)   (sizeof(a)/sizeof(a[0]))
 #define SGILOGDATAFMT_UNKNOWN  -1
 
 #define MINRUN         4       /* minimum run length */
@@ -221,7 +220,7 @@ LogL16Decode(TIFF* tif, tidata_t op, tsize_t occ, tsample_t s)
                                        tp[i++] |= (int16)*bp++ << shft;
                        }
                if (i != npixels) {
-                       TIFFError(tif->tif_name,
+                       TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
                "LogL16Decode: Not enough data at row %d (short %d pixels)",
                            tif->tif_row, npixels - i);
                        tif->tif_rawcp = (tidata_t) bp;
@@ -268,7 +267,7 @@ LogLuvDecode24(TIFF* tif, tidata_t op, tsize_t occ, tsample_t s)
        tif->tif_rawcp = (tidata_t) bp;
        tif->tif_rawcc = cc;
        if (i != npixels) {
-               TIFFError(tif->tif_name,
+               TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
            "LogLuvDecode24: Not enough data at row %d (short %d pixels)",
                    tif->tif_row, npixels - i);
                return (0);
@@ -321,7 +320,7 @@ LogLuvDecode32(TIFF* tif, tidata_t op, tsize_t occ, tsample_t s)
                                        tp[i++] |= (uint32)*bp++ << shft;
                        }
                if (i != npixels) {
-                       TIFFError(tif->tif_name,
+                       TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
                "LogLuvDecode32: Not enough data at row %d (short %d pixels)",
                            tif->tif_row, npixels - i);
                        tif->tif_rawcp = (tidata_t) bp;
@@ -452,7 +451,7 @@ LogL16Encode(TIFF* tif, tidata_t bp, tsize_t cc, tsample_t s)
        tif->tif_rawcp = op;
        tif->tif_rawcc = tif->tif_rawdatasize - occ;
 
-       return (0);
+       return (1);
 }
 
 /*
@@ -497,7 +496,7 @@ LogLuvEncode24(TIFF* tif, tidata_t bp, tsize_t cc, tsample_t s)
        tif->tif_rawcp = op;
        tif->tif_rawcc = tif->tif_rawdatasize - occ;
 
-       return (0);
+       return (1);
 }
 
 /*
@@ -586,7 +585,7 @@ LogLuvEncode32(TIFF* tif, tidata_t bp, tsize_t cc, tsample_t s)
        tif->tif_rawcp = op;
        tif->tif_rawcc = tif->tif_rawdatasize - occ;
 
-       return (0);
+       return (1);
 }
 
 /*
@@ -599,7 +598,7 @@ LogLuvEncodeStrip(TIFF* tif, tidata_t bp, tsize_t cc, tsample_t s)
        tsize_t rowlen = TIFFScanlineSize(tif);
 
        assert(cc%rowlen == 0);
-       while (cc && (*tif->tif_encoderow)(tif, bp, rowlen, s) == 0)
+       while (cc && (*tif->tif_encoderow)(tif, bp, rowlen, s) == 1)
                bp += rowlen, cc -= rowlen;
        return (cc == 0);
 }
@@ -614,7 +613,7 @@ LogLuvEncodeTile(TIFF* tif, tidata_t bp, tsize_t cc, tsample_t s)
        tsize_t rowlen = TIFFTileRowSize(tif);
 
        assert(cc%rowlen == 0);
-       while (cc && (*tif->tif_encoderow)(tif, bp, rowlen, s) == 0)
+       while (cc && (*tif->tif_encoderow)(tif, bp, rowlen, s) == 1)
                bp += rowlen, cc -= rowlen;
        return (cc == 0);
 }
@@ -1197,14 +1196,17 @@ LogL16InitState(TIFF* tif)
                sp->pixel_size = sizeof (uint8);
                break;
        default:
-               TIFFError(tif->tif_name,
+               TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
                    "No support for converting user data format to LogL");
                return (0);
        }
-       sp->tbuflen = multiply(td->td_imagewidth, td->td_rowsperstrip);
+        if( isTiled(tif) )
+            sp->tbuflen = multiply(td->td_tilewidth, td->td_tilelength);
+        else
+            sp->tbuflen = multiply(td->td_imagewidth, td->td_rowsperstrip);
        if (multiply(sp->tbuflen, sizeof (int16)) == 0 ||
            (sp->tbuf = (tidata_t*) _TIFFmalloc(sp->tbuflen * sizeof (int16))) == NULL) {
-               TIFFError(module, "%s: No space for SGILog translation buffer",
+               TIFFErrorExt(tif->tif_clientdata, module, "%s: No space for SGILog translation buffer",
                    tif->tif_name);
                return (0);
        }
@@ -1275,7 +1277,7 @@ LogLuvInitState(TIFF* tif)
 
        /* for some reason, we can't do this in TIFFInitLogLuv */
        if (td->td_planarconfig != PLANARCONFIG_CONTIG) {
-               TIFFError(module,
+               TIFFErrorExt(tif->tif_clientdata, module,
                    "SGILog compression cannot handle non-contiguous data");
                return (0);
        }
@@ -1295,14 +1297,17 @@ LogLuvInitState(TIFF* tif)
                sp->pixel_size = 3*sizeof (uint8);
                break;
        default:
-               TIFFError(tif->tif_name,
+               TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
                    "No support for converting user data format to LogLuv");
                return (0);
        }
-       sp->tbuflen = multiply(td->td_imagewidth, td->td_rowsperstrip);
+        if( isTiled(tif) )
+            sp->tbuflen = multiply(td->td_tilewidth, td->td_tilelength);
+        else
+            sp->tbuflen = multiply(td->td_imagewidth, td->td_rowsperstrip);
        if (multiply(sp->tbuflen, sizeof (uint32)) == 0 ||
            (sp->tbuf = (tidata_t*) _TIFFmalloc(sp->tbuflen * sizeof (uint32))) == NULL) {
-               TIFFError(module, "%s: No space for SGILog translation buffer",
+               TIFFErrorExt(tif->tif_clientdata, module, "%s: No space for SGILog translation buffer",
                    tif->tif_name);
                return (0);
        }
@@ -1362,7 +1367,7 @@ LogLuvSetupDecode(TIFF* tif)
                }
                return (1);
        default:
-               TIFFError(tif->tif_name,
+               TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
     "Inappropriate photometric interpretation %d for SGILog compression; %s",
                    td->td_photometric, "must be either LogLUV or LogL");
                break;
@@ -1425,14 +1430,14 @@ LogLuvSetupEncode(TIFF* tif)
                }
                break;
        default:
-               TIFFError(tif->tif_name,
+               TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
     "Inappropriate photometric interpretation %d for SGILog compression; %s",
                    td->td_photometric, "must be either LogLUV or LogL");
                break;
        }
        return (1);
 notsupported:
-       TIFFError(tif->tif_name,
+       TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
            "SGILog compression supported only for %s, or raw data",
            td->td_photometric == PHOTOMETRIC_LOGL ? "Y, L" : "XYZ, Luv");
        return (0);
@@ -1461,12 +1466,17 @@ LogLuvCleanup(TIFF* tif)
 {
        LogLuvState* sp = (LogLuvState *)tif->tif_data;
 
-       if (sp) {
-               if (sp->tbuf)
-                       _TIFFfree(sp->tbuf);
-               _TIFFfree(sp);
-               tif->tif_data = NULL;
-       }
+       assert(sp != 0);
+
+       tif->tif_tagmethods.vgetfield = sp->vgetparent;
+       tif->tif_tagmethods.vsetfield = sp->vsetparent;
+
+       if (sp->tbuf)
+               _TIFFfree(sp->tbuf);
+       _TIFFfree(sp);
+       tif->tif_data = NULL;
+
+       _TIFFSetDefaultCompressionState(tif);
 }
 
 static int
@@ -1499,7 +1509,7 @@ LogLuvVSetField(TIFF* tif, ttag_t tag, va_list ap)
                        bps = 8, fmt = SAMPLEFORMAT_UINT;
                        break;
                default:
-                       TIFFError(tif->tif_name,
+                       TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
                            "Unknown data format %d for LogLuv compression",
                            sp->user_datafmt);
                        return (0);
@@ -1516,7 +1526,7 @@ LogLuvVSetField(TIFF* tif, ttag_t tag, va_list ap)
                sp->encode_meth = va_arg(ap, int);
                if (sp->encode_meth != SGILOGENCODE_NODITHER &&
                                sp->encode_meth != SGILOGENCODE_RANDITHER) {
-                       TIFFError(tif->tif_name,
+                       TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
                                "Unknown encoding %d for LogLuv compression",
                                sp->encode_meth);
                        return (0);
@@ -1557,6 +1567,16 @@ TIFFInitSGILog(TIFF* tif, int scheme)
        assert(scheme == COMPRESSION_SGILOG24 || scheme == COMPRESSION_SGILOG);
 
        /*
+        * Merge codec-specific tag information.
+        */
+       if (!_TIFFMergeFieldInfo(tif, LogLuvFieldInfo,
+                                TIFFArrayCount(LogLuvFieldInfo))) {
+               TIFFErrorExt(tif->tif_clientdata, module,
+                            "Merging SGILog codec-specific tags failed");
+               return 0;
+       }
+
+       /*
         * Allocate state block so tag methods have storage to record values.
         */
        tif->tif_data = (tidata_t) _TIFFmalloc(sizeof (LogLuvState));
@@ -1583,8 +1603,9 @@ TIFFInitSGILog(TIFF* tif, int scheme)
        tif->tif_close = LogLuvClose;
        tif->tif_cleanup = LogLuvCleanup;
 
-       /* override SetField so we can handle our private pseudo-tag */
-       _TIFFMergeFieldInfo(tif, LogLuvFieldInfo, N(LogLuvFieldInfo));
+       /* 
+        * Override parent get/set field methods.
+        */
        sp->vgetparent = tif->tif_tagmethods.vgetfield;
        tif->tif_tagmethods.vgetfield = LogLuvVGetField;   /* hook for codec tags */
        sp->vsetparent = tif->tif_tagmethods.vsetfield;
@@ -1592,9 +1613,17 @@ TIFFInitSGILog(TIFF* tif, int scheme)
 
        return (1);
 bad:
-       TIFFError(module, "%s: No space for LogLuv state block", tif->tif_name);
+       TIFFErrorExt(tif->tif_clientdata, module,
+                    "%s: No space for LogLuv state block", tif->tif_name);
        return (0);
 }
 #endif /* LOGLUV_SUPPORT */
 
 /* vim: set ts=8 sts=8 sw=8 noet: */
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
index 88881ab..d423866 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: tif_lzw.c,v 1.1 2005-06-17 13:54:52 vp153 Exp $ */
+/* $Id: tif_lzw.c,v 1.29.2.6 2010-06-08 18:50:42 bfriesen Exp $ */
 
 /*
  * Copyright (c) 1988-1997 Sam Leffler
@@ -175,7 +175,7 @@ static  void cl_hash(LZWCodecState*);
  */
 #define        NextCode(_tif, _sp, _bp, _code, _get) {                         \
        if ((_sp)->dec_bitsleft < nbits) {                              \
-               TIFFWarning(_tif->tif_name,                             \
+               TIFFWarningExt(_tif->tif_clientdata, _tif->tif_name,                            \
                    "LZWDecode: Strip %d not terminated with EOI code", \
                    _tif->tif_curstrip);                                \
                _code = CODE_EOI;                                       \
@@ -199,12 +199,12 @@ LZWSetupDecode(TIFF* tif)
         {
             /*
              * Allocate state block so tag methods have storage to record 
-             * values.
+                        * values.
              */
             tif->tif_data = (tidata_t) _TIFFmalloc(sizeof(LZWCodecState));
             if (tif->tif_data == NULL)
             {
-                TIFFError("LZWPreDecode", "No space for LZW state block");
+                               TIFFErrorExt(tif->tif_clientdata, "LZWPreDecode", "No space for LZW state block");
                 return (0);
             }
 
@@ -224,7 +224,8 @@ LZWSetupDecode(TIFF* tif)
        if (sp->dec_codetab == NULL) {
                sp->dec_codetab = (code_t*)_TIFFmalloc(CSIZE*sizeof (code_t));
                if (sp->dec_codetab == NULL) {
-                       TIFFError(module, "No space for LZW code table");
+                       TIFFErrorExt(tif->tif_clientdata, module,
+                                    "No space for LZW code table");
                        return (0);
                }
                /*
@@ -237,6 +238,11 @@ LZWSetupDecode(TIFF* tif)
                     sp->dec_codetab[code].length = 1;
                     sp->dec_codetab[code].next = NULL;
                 } while (code--);
+               /*
+                * Zero-out the unused entries
+                 */
+                 _TIFFmemset(&sp->dec_codetab[CODE_CLEAR], 0,
+                            (CODE_FIRST - CODE_CLEAR) * sizeof (code_t));
        }
        return (1);
 }
@@ -251,13 +257,18 @@ LZWPreDecode(TIFF* tif, tsample_t s)
 
        (void) s;
        assert(sp != NULL);
+        if( sp->dec_codetab == NULL )
+        {
+            tif->tif_setupdecode( tif );
+        }
+
        /*
         * Check for old bit-reversed codes.
         */
        if (tif->tif_rawdata[0] == 0 && (tif->tif_rawdata[1] & 0x1)) {
 #ifdef LZW_COMPAT
                if (!sp->dec_decode) {
-                       TIFFWarning(tif->tif_name,
+                       TIFFWarningExt(tif->tif_clientdata, tif->tif_name,
                            "Old-style LZW codes, convert file");
                        /*
                         * Override default decoding methods with
@@ -280,7 +291,7 @@ LZWPreDecode(TIFF* tif, tsample_t s)
                sp->lzw_maxcode = MAXCODE(BITS_MIN);
 #else /* !LZW_COMPAT */
                if (!sp->dec_decode) {
-                       TIFFError(tif->tif_name,
+                       TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
                            "Old-style LZW codes not supported");
                        sp->dec_decode = LZWDecode;
                }
@@ -330,7 +341,7 @@ LZWPreDecode(TIFF* tif, tsample_t s)
 static void
 codeLoop(TIFF* tif)
 {
-       TIFFError(tif->tif_name,
+       TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
            "LZWDecode: Bogus encoding, loop in the code table; scanline %d",
            tif->tif_row);
 }
@@ -350,6 +361,7 @@ LZWDecode(TIFF* tif, tidata_t op0, tsize_t occ0, tsample_t s)
 
        (void) s;
        assert(sp != NULL);
+        assert(sp->dec_codetab != NULL);
        /*
         * Restart interrupted output operation.
         */
@@ -408,12 +420,20 @@ LZWDecode(TIFF* tif, tidata_t op0, tsize_t occ0, tsample_t s)
                        break;
                if (code == CODE_CLEAR) {
                        free_entp = sp->dec_codetab + CODE_FIRST;
+                       _TIFFmemset(free_entp, 0,
+                                   (CSIZE - CODE_FIRST) * sizeof (code_t));
                        nbits = BITS_MIN;
                        nbitsmask = MAXCODE(BITS_MIN);
                        maxcodep = sp->dec_codetab + nbitsmask-1;
                        NextCode(tif, sp, bp, code, GetNextCode);
                        if (code == CODE_EOI)
                                break;
+                       if (code == CODE_CLEAR) {
+                               TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+                               "LZWDecode: Corrupted LZW table at scanline %d",
+                                            tif->tif_row);
+                               return (0);
+                       }
                        *op++ = (char)code, occ--;
                        oldcodep = sp->dec_codetab + code;
                        continue;
@@ -425,7 +445,7 @@ LZWDecode(TIFF* tif, tidata_t op0, tsize_t occ0, tsample_t s)
                 */
                if (free_entp < &sp->dec_codetab[0] ||
                        free_entp >= &sp->dec_codetab[CSIZE]) {
-                       TIFFError(tif->tif_name,
+                       TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
                        "LZWDecode: Corrupted LZW table at scanline %d",
                        tif->tif_row);
                        return (0);
@@ -434,7 +454,7 @@ LZWDecode(TIFF* tif, tidata_t op0, tsize_t occ0, tsample_t s)
                free_entp->next = oldcodep;
                if (free_entp->next < &sp->dec_codetab[0] ||
                        free_entp->next >= &sp->dec_codetab[CSIZE]) {
-                       TIFFError(tif->tif_name,
+                       TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
                        "LZWDecode: Corrupted LZW table at scanline %d",
                        tif->tif_row);
                        return (0);
@@ -456,7 +476,7 @@ LZWDecode(TIFF* tif, tidata_t op0, tsize_t occ0, tsample_t s)
                         * value to output (written in reverse).
                         */
                        if(codep->length == 0) {
-                           TIFFError(tif->tif_name,
+                               TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
                            "LZWDecode: Wrong length of decoded string: "
                            "data probably corrupted at scanline %d",
                            tif->tif_row);      
@@ -513,8 +533,8 @@ LZWDecode(TIFF* tif, tidata_t op0, tsize_t occ0, tsample_t s)
        sp->dec_maxcodep = maxcodep;
 
        if (occ > 0) {
-               TIFFError(tif->tif_name,
-               "LZWDecode: Not enough data at scanline %d (short %d bytes)",
+               TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+               "LZWDecode: Not enough data at scanline %d (short %ld bytes)",
                    tif->tif_row, occ);
                return (0);
        }
@@ -604,12 +624,20 @@ LZWDecodeCompat(TIFF* tif, tidata_t op0, tsize_t occ0, tsample_t s)
                        break;
                if (code == CODE_CLEAR) {
                        free_entp = sp->dec_codetab + CODE_FIRST;
+                       _TIFFmemset(free_entp, 0,
+                                   (CSIZE - CODE_FIRST) * sizeof (code_t));
                        nbits = BITS_MIN;
                        nbitsmask = MAXCODE(BITS_MIN);
                        maxcodep = sp->dec_codetab + nbitsmask;
                        NextCode(tif, sp, bp, code, GetNextCodeCompat);
                        if (code == CODE_EOI)
                                break;
+                       if (code == CODE_CLEAR) {
+                               TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+                               "LZWDecode: Corrupted LZW table at scanline %d",
+                                            tif->tif_row);
+                               return (0);
+                       }
                        *op++ = code, occ--;
                        oldcodep = sp->dec_codetab + code;
                        continue;
@@ -621,7 +649,7 @@ LZWDecodeCompat(TIFF* tif, tidata_t op0, tsize_t occ0, tsample_t s)
                 */
                if (free_entp < &sp->dec_codetab[0] ||
                        free_entp >= &sp->dec_codetab[CSIZE]) {
-                       TIFFError(tif->tif_name,
+                       TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
                        "LZWDecodeCompat: Corrupted LZW table at scanline %d",
                        tif->tif_row);
                        return (0);
@@ -630,7 +658,7 @@ LZWDecodeCompat(TIFF* tif, tidata_t op0, tsize_t occ0, tsample_t s)
                free_entp->next = oldcodep;
                if (free_entp->next < &sp->dec_codetab[0] ||
                        free_entp->next >= &sp->dec_codetab[CSIZE]) {
-                       TIFFError(tif->tif_name,
+                       TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
                        "LZWDecodeCompat: Corrupted LZW table at scanline %d",
                        tif->tif_row);
                        return (0);
@@ -647,12 +675,13 @@ LZWDecodeCompat(TIFF* tif, tidata_t op0, tsize_t occ0, tsample_t s)
                }
                oldcodep = codep;
                if (code >= 256) {
+                       char *op_orig = op;
                        /*
                         * Code maps to a string, copy string
                         * value to output (written in reverse).
                         */
                        if(codep->length == 0) {
-                           TIFFError(tif->tif_name,
+                               TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
                            "LZWDecodeCompat: Wrong length of decoded "
                            "string: data probably corrupted at scanline %d",
                            tif->tif_row);      
@@ -681,7 +710,7 @@ LZWDecodeCompat(TIFF* tif, tidata_t op0, tsize_t occ0, tsample_t s)
                        tp = op;
                        do {
                                *--tp = codep->value;
-                       } while( (codep = codep->next) != NULL);
+                       } while( (codep = codep->next) != NULL && tp > op_orig);
                } else
                        *op++ = code, occ--;
        }
@@ -696,8 +725,8 @@ LZWDecodeCompat(TIFF* tif, tidata_t op0, tsize_t occ0, tsample_t s)
        sp->dec_maxcodep = maxcodep;
 
        if (occ > 0) {
-               TIFFError(tif->tif_name,
-           "LZWDecodeCompat: Not enough data at scanline %d (short %d bytes)",
+               TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+           "LZWDecodeCompat: Not enough data at scanline %d (short %ld bytes)",
                    tif->tif_row, occ);
                return (0);
        }
@@ -718,7 +747,7 @@ LZWSetupEncode(TIFF* tif)
        assert(sp != NULL);
        sp->enc_hashtab = (hash_t*) _TIFFmalloc(HSIZE*sizeof (hash_t));
        if (sp->enc_hashtab == NULL) {
-               TIFFError(module, "No space for LZW hash table");
+               TIFFErrorExt(tif->tif_clientdata, module, "No space for LZW hash table");
                return (0);
        }
        return (1);
@@ -734,6 +763,12 @@ LZWPreEncode(TIFF* tif, tsample_t s)
 
        (void) s;
        assert(sp != NULL);
+        
+        if( sp->enc_hashtab == NULL )
+        {
+            tif->tif_setupencode( tif );
+        }
+
        sp->lzw_nbits = BITS_MIN;
        sp->lzw_maxcode = MAXCODE(BITS_MIN);
        sp->lzw_free_ent = CODE_FIRST;
@@ -803,6 +838,9 @@ LZWEncode(TIFF* tif, tidata_t bp, tsize_t cc, tsample_t s)
        (void) s;
        if (sp == NULL)
                return (0);
+
+        assert(sp->enc_hashtab != NULL);
+
        /*
         * Load local state.
         */
@@ -1002,16 +1040,20 @@ cl_hash(LZWCodecState* sp)
 static void
 LZWCleanup(TIFF* tif)
 {
-       if (tif->tif_data) {
-               if (DecoderState(tif)->dec_codetab)
-                       _TIFFfree(DecoderState(tif)->dec_codetab);
+       (void)TIFFPredictorCleanup(tif);
 
-               if (EncoderState(tif)->enc_hashtab)
-                       _TIFFfree(EncoderState(tif)->enc_hashtab);
+       assert(tif->tif_data != 0);
 
-               _TIFFfree(tif->tif_data);
-               tif->tif_data = NULL;
-       }
+       if (DecoderState(tif)->dec_codetab)
+               _TIFFfree(DecoderState(tif)->dec_codetab);
+
+       if (EncoderState(tif)->enc_hashtab)
+               _TIFFfree(EncoderState(tif)->enc_hashtab);
+
+       _TIFFfree(tif->tif_data);
+       tif->tif_data = NULL;
+
+       _TIFFSetDefaultCompressionState(tif);
 }
 
 int
@@ -1050,7 +1092,8 @@ TIFFInitLZW(TIFF* tif, int scheme)
        (void) TIFFPredictorInit(tif);
        return (1);
 bad:
-       TIFFError("TIFFInitLZW", "No space for LZW state block");
+       TIFFErrorExt(tif->tif_clientdata, "TIFFInitLZW", 
+                    "No space for LZW state block");
        return (0);
 }
 
@@ -1077,3 +1120,10 @@ bad:
 #endif /* LZW_SUPPORT */
 
 /* vim: set ts=8 sts=8 sw=8 noet: */
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
index eeac5a4..d7652bb 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: tif_next.c,v 1.1 2005-06-17 13:54:52 vp153 Exp $ */
+/* $Id: tif_next.c,v 1.8.2.1 2010-06-08 18:50:42 bfriesen Exp $ */
 
 /*
  * Copyright (c) 1988-1997 Sam Leffler
 static int
 NeXTDecode(TIFF* tif, tidata_t buf, tsize_t occ, tsample_t s)
 {
-       register unsigned char *bp, *op;
-       register tsize_t cc;
-       register int n;
+       unsigned char *bp, *op;
+       tsize_t cc;
        tidata_t row;
-       tsize_t scanline;
+       tsize_t scanline, n;
 
        (void) s;
        /*
@@ -66,7 +65,7 @@ NeXTDecode(TIFF* tif, tidata_t buf, tsize_t occ, tsample_t s)
        bp = (unsigned char *)tif->tif_rawcp;
        cc = tif->tif_rawcc;
        scanline = tif->tif_scanlinesize;
-       for (row = buf; (long)occ > 0; occ -= scanline, row += scanline) {
+       for (row = buf; occ > 0; occ -= scanline, row += scanline) {
                n = *bp++, cc--;
                switch (n) {
                case LITERALROW:
@@ -80,10 +79,10 @@ NeXTDecode(TIFF* tif, tidata_t buf, tsize_t occ, tsample_t s)
                        cc -= scanline;
                        break;
                case LITERALSPAN: {
-                       int off;
+                       tsize_t off;
                        /*
-                        * The scanline has a literal span
-                        * that begins at some offset.
+                        * The scanline has a literal span that begins at some
+                        * offset.
                         */
                        off = (bp[0] * 256) + bp[1];
                        n = (bp[2] * 256) + bp[3];
@@ -95,23 +94,27 @@ NeXTDecode(TIFF* tif, tidata_t buf, tsize_t occ, tsample_t s)
                        break;
                }
                default: {
-                       register int npixels = 0, grey;
-                       unsigned long imagewidth = tif->tif_dir.td_imagewidth;
+                       uint32 npixels = 0, grey;
+                       uint32 imagewidth = tif->tif_dir.td_imagewidth;
 
                        /*
-                        * The scanline is composed of a sequence
-                        * of constant color ``runs''.  We shift
-                        * into ``run mode'' and interpret bytes
-                        * as codes of the form <color><npixels>
-                        * until we've filled the scanline.
+                        * The scanline is composed of a sequence of constant
+                        * color ``runs''.  We shift into ``run mode'' and
+                        * interpret bytes as codes of the form
+                        * <color><npixels> until we've filled the scanline.
                         */
                        op = row;
                        for (;;) {
                                grey = (n>>6) & 0x3;
                                n &= 0x3f;
-                               while (n-- > 0)
+                               /*
+                                * Ensure the run does not exceed the scanline
+                                * bounds, potentially resulting in a security
+                                * issue.
+                                */
+                               while (n-- > 0 && npixels < imagewidth)
                                        SETPIXEL(op, grey);
-                               if (npixels >= (int) imagewidth)
+                               if (npixels >= imagewidth)
                                        break;
                                if (cc == 0)
                                        goto bad;
@@ -125,7 +128,7 @@ NeXTDecode(TIFF* tif, tidata_t buf, tsize_t occ, tsample_t s)
        tif->tif_rawcc = cc;
        return (1);
 bad:
-       TIFFError(tif->tif_name, "NeXTDecode: Not enough data for scanline %ld",
+       TIFFErrorExt(tif->tif_clientdata, tif->tif_name, "NeXTDecode: Not enough data for scanline %ld",
            (long) tif->tif_row);
        return (0);
 }
@@ -142,3 +145,10 @@ TIFFInitNeXT(TIFF* tif, int scheme)
 #endif /* NEXT_SUPPORT */
 
 /* vim: set ts=8 sts=8 sw=8 noet: */
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
index 0f05a51..9ae856c 100644 (file)
-/* $Id: tif_ojpeg.c,v 1.1 2005-06-17 13:54:52 vp153 Exp $ */
+/* $Id: tif_ojpeg.c,v 1.24.2.6 2010-06-08 23:29:51 bfriesen Exp $ */
+
+/* WARNING: The type of JPEG encapsulation defined by the TIFF Version 6.0
+   specification is now totally obsolete and deprecated for new applications and
+   images. This file was was created solely in order to read unconverted images
+   still present on some users' computer systems. It will never be extended
+   to write such files. Writing new-style JPEG compressed TIFFs is implemented
+   in tif_jpeg.c.
+
+   The code is carefully crafted to robustly read all gathered JPEG-in-TIFF
+   testfiles, and anticipate as much as possible all other... But still, it may
+   fail on some. If you encounter problems, please report them on the TIFF
+   mailing list and/or to Joris Van Damme <info@awaresystems.be>.
+
+   Please read the file called "TIFF Technical Note #2" if you need to be
+   convinced this compression scheme is bad and breaks TIFF. That document
+   is linked to from the LibTiff site <http://www.remotesensing.org/libtiff/>
+   and from AWare Systems' TIFF section
+   <http://www.awaresystems.be/imaging/tiff.html>. It is also absorbed
+   in Adobe's specification supplements, marked "draft" up to this day, but
+   supported by the TIFF community.
+
+   This file interfaces with Release 6B of the JPEG Library written by the
+   Independent JPEG Group. Previous versions of this file required a hack inside
+   the LibJpeg library. This version no longer requires that. Remember to
+   remove the hack if you update from the old version.
+
+   Copyright (c) Joris Van Damme <info@awaresystems.be>
+   Copyright (c) AWare Systems <http://www.awaresystems.be/>
+
+   The licence agreement for this file is the same as the rest of the LibTiff
+   library.
+
+   IN NO EVENT SHALL JORIS VAN DAMME OR AWARE SYSTEMS BE LIABLE FOR
+   ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
+   OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
+   WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
+   LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
+   OF THIS SOFTWARE.
+
+   Joris Van Damme and/or AWare Systems may be available for custom
+   developement. If you like what you see, and need anything similar or related,
+   contact <info@awaresystems.be>.
+*/
+
+/* What is what, and what is not?
+
+   This decoder starts with an input stream, that is essentially the JpegInterchangeFormat
+   stream, if any, followed by the strile data, if any. This stream is read in
+   OJPEGReadByte and related functions.
+
+   It analyzes the start of this stream, until it encounters non-marker data, i.e.
+   compressed image data. Some of the header markers it sees have no actual content,
+   like the SOI marker, and APP/COM markers that really shouldn't even be there. Some
+   other markers do have content, and the valuable bits and pieces of information
+   in these markers are saved, checking all to verify that the stream is more or
+   less within expected bounds. This happens inside the OJPEGReadHeaderInfoSecStreamXxx
+   functions.
+
+   Some OJPEG imagery contains no valid JPEG header markers. This situation is picked
+   up on if we've seen no SOF marker when we're at the start of the compressed image
+   data. In this case, the tables are read from JpegXxxTables tags, and the other
+   bits and pieces of information is initialized to its most basic value. This is
+   implemented in the OJPEGReadHeaderInfoSecTablesXxx functions.
+
+   When this is complete, a good and valid JPEG header can be assembled, and this is
+   passed through to LibJpeg. When that's done, the remainder of the input stream, i.e.
+   the compressed image data, can be passed through unchanged. This is done in
+   OJPEGWriteStream functions.
+
+   LibTiff rightly expects to know the subsampling values before decompression. Just like
+   in new-style JPEG-in-TIFF, though, or even more so, actually, the YCbCrsubsampling
+   tag is notoriously unreliable. To correct these tag values with the ones inside
+   the JPEG stream, the first part of the input stream is pre-scanned in
+   OJPEGSubsamplingCorrect, making no note of any other data, reporting no warnings
+   or errors, up to the point where either these values are read, or it's clear they
+   aren't there. This means that some of the data is read twice, but we feel speed
+   in correcting these values is important enough to warrant this sacrifice. Allthough
+   there is currently no define or other configuration mechanism to disable this behaviour,
+   the actual header scanning is build to robustly respond with error report if it
+   should encounter an uncorrected mismatch of subsampling values. See
+   OJPEGReadHeaderInfoSecStreamSof.
+
+   The restart interval and restart markers are the most tricky part... The restart
+   interval can be specified in a tag. It can also be set inside the input JPEG stream.
+   It can be used inside the input JPEG stream. If reading from strile data, we've
+   consistenly discovered the need to insert restart markers in between the different
+   striles, as is also probably the most likely interpretation of the original TIFF 6.0
+   specification. With all this setting of interval, and actual use of markers that is not
+   predictable at the time of valid JPEG header assembly, the restart thing may turn
+   out the Achilles heel of this implementation. Fortunately, most OJPEG writer vendors
+   succeed in reading back what they write, which may be the reason why we've been able
+   to discover ways that seem to work.
+
+   Some special provision is made for planarconfig separate OJPEG files. These seem
+   to consistently contain header info, a SOS marker, a plane, SOS marker, plane, SOS,
+   and plane. This may or may not be a valid JPEG configuration, we don't know and don't
+   care. We want LibTiff to be able to access the planes individually, without huge
+   buffering inside LibJpeg, anyway. So we compose headers to feed to LibJpeg, in this
+   case, that allow us to pass a single plane such that LibJpeg sees a valid
+   single-channel JPEG stream. Locating subsequent SOS markers, and thus subsequent
+   planes, is done inside OJPEGReadSecondarySos.
+
+   The benefit of the scheme is... that it works, basically. We know of no other that
+   does. It works without checking software tag, or otherwise going about things in an
+   OJPEG flavor specific manner. Instead, it is a single scheme, that covers the cases
+   with and without JpegInterchangeFormat, with and without striles, with part of
+   the header in JpegInterchangeFormat and remainder in first strile, etc. It is forgiving
+   and robust, may likely work with OJPEG flavors we've not seen yet, and makes most out
+   of the data.
+
+   Another nice side-effect is that a complete JPEG single valid stream is build if
+   planarconfig is not separate (vast majority). We may one day use that to build
+   converters to JPEG, and/or to new-style JPEG compression inside TIFF.
+
+   A dissadvantage is the lack of random access to the individual striles. This is the
+   reason for much of the complicated restart-and-position stuff inside OJPEGPreDecode.
+   Applications would do well accessing all striles in order, as this will result in
+   a single sequential scan of the input stream, and no restarting of LibJpeg decoding
+   session.
+*/
+
 
 #include "tiffiop.h"
 #ifdef OJPEG_SUPPORT
 
-/* JPEG Compression support, as per the original TIFF 6.0 specification.
-
-   WARNING: KLUDGE ALERT!  The type of JPEG encapsulation defined by the TIFF
-                           Version 6.0 specification is now totally obsolete and
-   deprecated for new applications and images.  This file is an unsupported hack
-   that was created solely in order to read (but NOT write!) a few old,
-   unconverted images still present on some users' computer systems.  The code
-   isn't pretty or robust, and it won't read every "old format" JPEG-in-TIFF
-   file (see Samuel Leffler's draft "TIFF Technical Note No. 2" for a long and
-   incomplete list of known problems), but it seems to work well enough in the
-   few cases of practical interest to the author; so, "caveat emptor"!  This
-   file should NEVER be enhanced to write new images using anything other than
-   the latest approved JPEG-in-TIFF encapsulation method, implemented by the
-   "tif_jpeg.c" file elsewhere in this library.
-
-   This file interfaces with Release 6B of the JPEG Library written by theu
-   Independent JPEG Group, which you can find on the Internet at:
-   ftp://ftp.uu.net:/graphics/jpeg/.
-
-   The "C" Preprocessor macros, "[CD]_LOSSLESS_SUPPORTED", are defined by your
-   JPEG Library Version 6B only if you have applied a (massive!) patch by Ken
-   Murchison of Oceana Matrix Ltd. <ken@oceana.com> to support lossless Huffman
-   encoding (TIFF "JPEGProc" tag value = 14).  This patch can be found on the
-   Internet at: ftp://ftp.oceana.com/pub/ljpeg-6b.tar.gz.
-
-   Some old files produced by the Wang Imaging application for Microsoft Windows
-   apparently can be decoded only with a special patch to the JPEG Library,
-   which defines a subroutine named "jpeg_reset_huff_decode()" in its "jdhuff.c"
-   module (the "jdshuff.c" module, if Ken Murchison's patch has been applied).
-   Unfortunately the patch differs slightly in each case, and some TIFF Library
-   have reported problems finding the code, so both versions appear below; you
-   should carefully extract and apply only the version that applies to your JPEG
-   Library!
-
-   Contributed by Scott Marovich <marovich@hpl.hp.com> with considerable help
-   from Charles Auer <Bumble731@msn.com> to unravel the mysteries of image files
-   created by the Wang Imaging application for Microsoft Windows.
-*/
-#if 0  /* Patch for JPEG Library WITHOUT lossless Huffman coding */
-*** jdhuff.c.orig      Mon Oct 20 17:51:10 1997
---- jdhuff.c   Sun Nov 11 17:33:58 2001
-***************
-*** 648,651 ****
---- 648,683 ----
-    for (i = 0; i < NUM_HUFF_TBLS; i++) {
-      entropy->dc_derived_tbls[i] = entropy->ac_derived_tbls[i] = NULL;
-    }
-  }
-+ 
-+ /*
-+  * BEWARE OF KLUDGE:  This subroutine is a hack for decoding illegal JPEG-in-
-+  *                    TIFF encapsulations produced by Microsoft's Wang Imaging
-+  * for Windows application with the public-domain TIFF Library.  Based upon an
-+  * examination of selected output files, this program apparently divides a JPEG
-+  * bit-stream into consecutive horizontal TIFF "strips", such that the JPEG
-+  * encoder's/decoder's DC coefficients for each image component are reset before
-+  * each "strip".  Moreover, a "strip" is not necessarily encoded in a multiple
-+  * of 8 bits, so one must sometimes discard 1-7 bits at the end of each "strip"
-+  * for alignment to the next input-Byte storage boundary.  IJG JPEG Library
-+  * decoder state is not normally exposed to client applications, so this sub-
-+  * routine provides the TIFF Library with a "hook" to make these corrections.
-+  * It should be called after "jpeg_start_decompress()" and before
-+  * "jpeg_finish_decompress()", just before decoding each "strip" using
-+  * "jpeg_read_raw_data()" or "jpeg_read_scanlines()".
-+  *
-+  * This kludge is not sanctioned or supported by the Independent JPEG Group, and
-+  * future changes to the IJG JPEG Library might invalidate it.  Do not send bug
-+  * reports about this code to IJG developers.  Instead, contact the author for
-+  * advice: Scott B. Marovich <marovich@hpl.hp.com>, Hewlett-Packard Labs, 6/01.
-+  */
-+ GLOBAL(void)
-+ jpeg_reset_huff_decode (register j_decompress_ptr cinfo)
-+ { register huff_entropy_ptr entropy = (huff_entropy_ptr)cinfo->entropy;
-+   register int ci = 0;
-+ 
-+   /* Discard encoded input bits, up to the next Byte boundary */
-+   entropy->bitstate.bits_left &= ~7;
-+   /* Re-initialize DC predictions to 0 */
-+   do entropy->saved.last_dc_val[ci] = 0; while (++ci < cinfo->comps_in_scan);
-+ }
-#endif /* Patch for JPEG Library WITHOUT lossless Huffman coding */
-#if 0  /* Patch for JPEG Library WITH lossless Huffman coding */
-*** jdshuff.c.orig     Mon Mar 11 16:44:54 2002
---- jdshuff.c  Mon Mar 11 16:44:54 2002
-***************
-*** 357,360 ****
---- 357,393 ----
-    for (i = 0; i < NUM_HUFF_TBLS; i++) {
-      entropy->dc_derived_tbls[i] = entropy->ac_derived_tbls[i] = NULL;
-    }
-  }
-+ 
-+ /*
-+  * BEWARE OF KLUDGE:  This subroutine is a hack for decoding illegal JPEG-in-
-+  *                    TIFF encapsulations produced by Microsoft's Wang Imaging
-+  * for Windows application with the public-domain TIFF Library.  Based upon an
-+  * examination of selected output files, this program apparently divides a JPEG
-+  * bit-stream into consecutive horizontal TIFF "strips", such that the JPEG
-+  * encoder's/decoder's DC coefficients for each image component are reset before
-+  * each "strip".  Moreover, a "strip" is not necessarily encoded in a multiple
-+  * of 8 bits, so one must sometimes discard 1-7 bits at the end of each "strip"
-+  * for alignment to the next input-Byte storage boundary.  IJG JPEG Library
-+  * decoder state is not normally exposed to client applications, so this sub-
-+  * routine provides the TIFF Library with a "hook" to make these corrections.
-+  * It should be called after "jpeg_start_decompress()" and before
-+  * "jpeg_finish_decompress()", just before decoding each "strip" using
-+  * "jpeg_read_raw_data()" or "jpeg_read_scanlines()".
-+  *
-+  * This kludge is not sanctioned or supported by the Independent JPEG Group, and
-+  * future changes to the IJG JPEG Library might invalidate it.  Do not send bug
-+  * reports about this code to IJG developers.  Instead, contact the author for
-+  * advice: Scott B. Marovich <marovich@hpl.hp.com>, Hewlett-Packard Labs, 6/01.
-+  */
-+ GLOBAL(void)
-+ jpeg_reset_huff_decode (register j_decompress_ptr cinfo)
-+ { register shuff_entropy_ptr entropy = (shuff_entropy_ptr)
-+                                        ((j_lossy_d_ptr)cinfo->codec)->entropy_private;
-+   register int ci = 0;
-+ 
-+   /* Discard encoded input bits, up to the next Byte boundary */
-+   entropy->bitstate.bits_left &= ~7;
-+   /* Re-initialize DC predictions to 0 */
-+   do entropy->saved.last_dc_val[ci] = 0; while (++ci < cinfo->comps_in_scan);
-+ }
-#endif /* Patch for JPEG Library WITH lossless Huffman coding */
+/* Configuration defines here are:
+ * JPEG_ENCAP_EXTERNAL: The normal way to call libjpeg, uses longjump. In some environments,
+ *     like eg LibTiffDelphi, this is not possible. For this reason, the actual calls to
+ *     libjpeg, with longjump stuff, are encapsulated in dedicated functions. When
+ *     JPEG_ENCAP_EXTERNAL is defined, these encapsulating functions are declared external
+ *     to this unit, and can be defined elsewhere to use stuff other then longjump.
+ *     The default mode, without JPEG_ENCAP_EXTERNAL, implements the call encapsulators
+ *     here, internally, with normal longjump.
+ * SETJMP, LONGJMP, JMP_BUF: On some machines/environments a longjump equivalent is
+ *     conviniently available, but still it may be worthwhile to use _setjmp or sigsetjmp
+ *     in place of plain setjmp. These macros will make it easier. It is useless
+ *     to fiddle with these if you define JPEG_ENCAP_EXTERNAL.
+ * OJPEG_BUFFER: Define the size of the desired buffer here. Should be small enough so as to guarantee
+ *     instant processing, optimal streaming and optimal use of processor cache, but also big
+ *     enough so as to not result in significant call overhead. It should be at least a few
+ *     bytes to accomodate some structures (this is verified in asserts), but it would not be
+ *     sensible to make it this small anyway, and it should be at most 64K since it is indexed
+ *     with uint16. We recommend 2K.
+ * EGYPTIANWALK: You could also define EGYPTIANWALK here, but it is not used anywhere and has
+ *     absolutely no effect. That is why most people insist the EGYPTIANWALK is a bit silly.
+ */
+
+/* #define LIBJPEG_ENCAP_EXTERNAL */
+#define SETJMP(jbuf) setjmp(jbuf)
+#define LONGJMP(jbuf,code) longjmp(jbuf,code)
+#define JMP_BUF jmp_buf
+#define OJPEG_BUFFER 2048
+/* define EGYPTIANWALK */
+
+#define JPEG_MARKER_SOF0 0xC0
+#define JPEG_MARKER_SOF1 0xC1
+#define JPEG_MARKER_SOF3 0xC3
+#define JPEG_MARKER_DHT 0xC4
+#define JPEG_MARKER_RST0 0XD0
+#define JPEG_MARKER_SOI 0xD8
+#define JPEG_MARKER_EOI 0xD9
+#define JPEG_MARKER_SOS 0xDA
+#define JPEG_MARKER_DQT 0xDB
+#define JPEG_MARKER_DRI 0xDD
+#define JPEG_MARKER_APP0 0xE0
+#define JPEG_MARKER_COM 0xFE
+
+#define FIELD_OJPEG_JPEGINTERCHANGEFORMAT (FIELD_CODEC+0)
+#define FIELD_OJPEG_JPEGINTERCHANGEFORMATLENGTH (FIELD_CODEC+1)
+#define FIELD_OJPEG_JPEGQTABLES (FIELD_CODEC+2)
+#define FIELD_OJPEG_JPEGDCTABLES (FIELD_CODEC+3)
+#define FIELD_OJPEG_JPEGACTABLES (FIELD_CODEC+4)
+#define FIELD_OJPEG_JPEGPROC (FIELD_CODEC+5)
+#define FIELD_OJPEG_JPEGRESTARTINTERVAL (FIELD_CODEC+6)
+#define FIELD_OJPEG_COUNT 7
+
+static const TIFFFieldInfo ojpeg_field_info[] = {
+       {TIFFTAG_JPEGIFOFFSET,1,1,TIFF_LONG,FIELD_OJPEG_JPEGINTERCHANGEFORMAT,TRUE,FALSE,"JpegInterchangeFormat"},
+       {TIFFTAG_JPEGIFBYTECOUNT,1,1,TIFF_LONG,FIELD_OJPEG_JPEGINTERCHANGEFORMATLENGTH,TRUE,FALSE,"JpegInterchangeFormatLength"},
+       {TIFFTAG_JPEGQTABLES,TIFF_VARIABLE,TIFF_VARIABLE,TIFF_LONG,FIELD_OJPEG_JPEGQTABLES,FALSE,TRUE,"JpegQTables"},
+       {TIFFTAG_JPEGDCTABLES,TIFF_VARIABLE,TIFF_VARIABLE,TIFF_LONG,FIELD_OJPEG_JPEGDCTABLES,FALSE,TRUE,"JpegDcTables"},
+       {TIFFTAG_JPEGACTABLES,TIFF_VARIABLE,TIFF_VARIABLE,TIFF_LONG,FIELD_OJPEG_JPEGACTABLES,FALSE,TRUE,"JpegAcTables"},
+       {TIFFTAG_JPEGPROC,1,1,TIFF_SHORT,FIELD_OJPEG_JPEGPROC,FALSE,FALSE,"JpegProc"},
+       {TIFFTAG_JPEGRESTARTINTERVAL,1,1,TIFF_SHORT,FIELD_OJPEG_JPEGRESTARTINTERVAL,FALSE,FALSE,"JpegRestartInterval"},
+};
+
+#ifndef LIBJPEG_ENCAP_EXTERNAL
 #include <setjmp.h>
-#include <stdio.h>
-#ifdef FAR
-#undef FAR /* Undefine FAR to avoid conflict with JPEG definition */
 #endif
-#define JPEG_INTERNALS /* Include "jpegint.h" for "DSTATE_*" symbols */
-#define JPEG_CJPEG_DJPEG /* Include all Version 6B+ "jconfig.h" options */
-#undef INLINE
+
 #include "jpeglib.h"
-#undef JPEG_CJPEG_DJPEG
-#undef JPEG_INTERNALS
+#include "jerror.h"
 
-/* Hack for files produced by Wang Imaging application on Microsoft Windows */
-extern void jpeg_reset_huff_decode(j_decompress_ptr);
+typedef struct jpeg_error_mgr jpeg_error_mgr;
+typedef struct jpeg_common_struct jpeg_common_struct;
+typedef struct jpeg_decompress_struct jpeg_decompress_struct;
+typedef struct jpeg_source_mgr jpeg_source_mgr;
 
-/* On some machines, it may be worthwhile to use "_setjmp()" or "sigsetjmp()"
-   instead of "setjmp()".  These macros make it easier:
-*/
-#define SETJMP(jbuf)setjmp(jbuf)
-#define LONGJMP(jbuf,code)longjmp(jbuf,code)
-#define JMP_BUF jmp_buf
+typedef enum {
+       osibsNotSetYet,
+       osibsJpegInterchangeFormat,
+       osibsStrile,
+       osibsEof
+} OJPEGStateInBufferSource;
+
+typedef enum {
+       ososSoi,
+       ososQTable0,ososQTable1,ososQTable2,ososQTable3,
+       ososDcTable0,ososDcTable1,ososDcTable2,ososDcTable3,
+       ososAcTable0,ososAcTable1,ososAcTable2,ososAcTable3,
+       ososDri,
+       ososSof,
+       ososSos,
+       ososCompressed,
+       ososRst,
+       ososEoi
+} OJPEGStateOutState;
+
+typedef struct {
+       TIFF* tif;
+       #ifndef LIBJPEG_ENCAP_EXTERNAL
+       JMP_BUF exit_jmpbuf;
+       #endif
+       TIFFVGetMethod vgetparent;
+       TIFFVSetMethod vsetparent;
+       toff_t file_size;
+       uint32 image_width;
+       uint32 image_length;
+       uint32 strile_width;
+       uint32 strile_length;
+       uint32 strile_length_total;
+       uint8 samples_per_pixel;
+       uint8 plane_sample_offset;
+       uint8 samples_per_pixel_per_plane;
+       toff_t jpeg_interchange_format;
+       toff_t jpeg_interchange_format_length;
+       uint8 jpeg_proc;
+       uint8 subsamplingcorrect;
+       uint8 subsamplingcorrect_done;
+       uint8 subsampling_tag;
+       uint8 subsampling_hor;
+       uint8 subsampling_ver;
+       uint8 subsampling_force_desubsampling_inside_decompression;
+       uint8 qtable_offset_count;
+       uint8 dctable_offset_count;
+       uint8 actable_offset_count;
+       toff_t qtable_offset[3];
+       toff_t dctable_offset[3];
+       toff_t actable_offset[3];
+       uint8* qtable[4];
+       uint8* dctable[4];
+       uint8* actable[4];
+       uint16 restart_interval;
+       uint8 restart_index;
+       uint8 sof_log;
+       uint8 sof_marker_id;
+       uint32 sof_x;
+       uint32 sof_y;
+       uint8 sof_c[3];
+       uint8 sof_hv[3];
+       uint8 sof_tq[3];
+       uint8 sos_cs[3];
+       uint8 sos_tda[3];
+       struct {
+               uint8 log;
+               OJPEGStateInBufferSource in_buffer_source;
+               tstrile_t in_buffer_next_strile;
+               toff_t in_buffer_file_pos;
+               toff_t in_buffer_file_togo;
+       } sos_end[3];
+       uint8 readheader_done;
+       uint8 writeheader_done;
+       tsample_t write_cursample;
+       tstrile_t write_curstrile;
+       uint8 libjpeg_session_active;
+       uint8 libjpeg_jpeg_query_style;
+       jpeg_error_mgr libjpeg_jpeg_error_mgr;
+       jpeg_decompress_struct libjpeg_jpeg_decompress_struct;
+       jpeg_source_mgr libjpeg_jpeg_source_mgr;
+       uint8 subsampling_convert_log;
+       uint32 subsampling_convert_ylinelen;
+       uint32 subsampling_convert_ylines;
+       uint32 subsampling_convert_clinelen;
+       uint32 subsampling_convert_clines;
+       uint32 subsampling_convert_ybuflen;
+       uint32 subsampling_convert_cbuflen;
+       uint32 subsampling_convert_ycbcrbuflen;
+       uint8* subsampling_convert_ycbcrbuf;
+       uint8* subsampling_convert_ybuf;
+       uint8* subsampling_convert_cbbuf;
+       uint8* subsampling_convert_crbuf;
+       uint32 subsampling_convert_ycbcrimagelen;
+       uint8** subsampling_convert_ycbcrimage;
+       uint32 subsampling_convert_clinelenout;
+       uint32 subsampling_convert_state;
+       uint32 bytes_per_line;   /* if the codec outputs subsampled data, a 'line' in bytes_per_line */
+       uint32 lines_per_strile; /* and lines_per_strile means subsampling_ver desubsampled rows     */
+       OJPEGStateInBufferSource in_buffer_source;
+       tstrile_t in_buffer_next_strile;
+       tstrile_t in_buffer_strile_count;
+       toff_t in_buffer_file_pos;
+       uint8 in_buffer_file_pos_log;
+       toff_t in_buffer_file_togo;
+       uint16 in_buffer_togo;
+       uint8* in_buffer_cur;
+       uint8 in_buffer[OJPEG_BUFFER];
+       OJPEGStateOutState out_state;
+       uint8 out_buffer[OJPEG_BUFFER];
+       uint8* skip_buffer;
+} OJPEGState;
+
+static int OJPEGVGetField(TIFF* tif, ttag_t tag, va_list ap);
+static int OJPEGVSetField(TIFF* tif, ttag_t tag, va_list ap);
+static void OJPEGPrintDir(TIFF* tif, FILE* fd, long flags);
+
+static int OJPEGSetupDecode(TIFF* tif);
+static int OJPEGPreDecode(TIFF* tif, tsample_t s);
+static int OJPEGPreDecodeSkipRaw(TIFF* tif);
+static int OJPEGPreDecodeSkipScanlines(TIFF* tif);
+static int OJPEGDecode(TIFF* tif, tidata_t buf, tsize_t cc, tsample_t s);
+static int OJPEGDecodeRaw(TIFF* tif, tidata_t buf, tsize_t cc);
+static int OJPEGDecodeScanlines(TIFF* tif, tidata_t buf, tsize_t cc);
+static void OJPEGPostDecode(TIFF* tif, tidata_t buf, tsize_t cc);
+static int OJPEGSetupEncode(TIFF* tif);
+static int OJPEGPreEncode(TIFF* tif, tsample_t s);
+static int OJPEGEncode(TIFF* tif, tidata_t buf, tsize_t cc, tsample_t s);
+static int OJPEGPostEncode(TIFF* tif);
+static void OJPEGCleanup(TIFF* tif);
+
+static void OJPEGSubsamplingCorrect(TIFF* tif);
+static int OJPEGReadHeaderInfo(TIFF* tif);
+static int OJPEGReadSecondarySos(TIFF* tif, tsample_t s);
+static int OJPEGWriteHeaderInfo(TIFF* tif);
+static void OJPEGLibjpegSessionAbort(TIFF* tif);
+
+static int OJPEGReadHeaderInfoSec(TIFF* tif);
+static int OJPEGReadHeaderInfoSecStreamDri(TIFF* tif);
+static int OJPEGReadHeaderInfoSecStreamDqt(TIFF* tif);
+static int OJPEGReadHeaderInfoSecStreamDht(TIFF* tif);
+static int OJPEGReadHeaderInfoSecStreamSof(TIFF* tif, uint8 marker_id);
+static int OJPEGReadHeaderInfoSecStreamSos(TIFF* tif);
+static int OJPEGReadHeaderInfoSecTablesQTable(TIFF* tif);
+static int OJPEGReadHeaderInfoSecTablesDcTable(TIFF* tif);
+static int OJPEGReadHeaderInfoSecTablesAcTable(TIFF* tif);
+
+static int OJPEGReadBufferFill(OJPEGState* sp);
+static int OJPEGReadByte(OJPEGState* sp, uint8* byte);
+static int OJPEGReadBytePeek(OJPEGState* sp, uint8* byte);
+static void OJPEGReadByteAdvance(OJPEGState* sp);
+static int OJPEGReadWord(OJPEGState* sp, uint16* word);
+static int OJPEGReadBlock(OJPEGState* sp, uint16 len, void* mem);
+static void OJPEGReadSkip(OJPEGState* sp, uint16 len);
+
+static int OJPEGWriteStream(TIFF* tif, void** mem, uint32* len);
+static void OJPEGWriteStreamSoi(TIFF* tif, void** mem, uint32* len);
+static void OJPEGWriteStreamQTable(TIFF* tif, uint8 table_index, void** mem, uint32* len);
+static void OJPEGWriteStreamDcTable(TIFF* tif, uint8 table_index, void** mem, uint32* len);
+static void OJPEGWriteStreamAcTable(TIFF* tif, uint8 table_index, void** mem, uint32* len);
+static void OJPEGWriteStreamDri(TIFF* tif, void** mem, uint32* len);
+static void OJPEGWriteStreamSof(TIFF* tif, void** mem, uint32* len);
+static void OJPEGWriteStreamSos(TIFF* tif, void** mem, uint32* len);
+static int OJPEGWriteStreamCompressed(TIFF* tif, void** mem, uint32* len);
+static void OJPEGWriteStreamRst(TIFF* tif, void** mem, uint32* len);
+static void OJPEGWriteStreamEoi(TIFF* tif, void** mem, uint32* len);
+
+#ifdef LIBJPEG_ENCAP_EXTERNAL
+extern int jpeg_create_decompress_encap(OJPEGState* sp, jpeg_decompress_struct* cinfo);
+extern int jpeg_read_header_encap(OJPEGState* sp, jpeg_decompress_struct* cinfo, uint8 require_image);
+extern int jpeg_start_decompress_encap(OJPEGState* sp, jpeg_decompress_struct* cinfo);
+extern int jpeg_read_scanlines_encap(OJPEGState* sp, jpeg_decompress_struct* cinfo, void* scanlines, uint32 max_lines);
+extern int jpeg_read_raw_data_encap(OJPEGState* sp, jpeg_decompress_struct* cinfo, void* data, uint32 max_lines);
+extern void jpeg_encap_unwind(TIFF* tif);
+#else
+static int jpeg_create_decompress_encap(OJPEGState* sp, jpeg_decompress_struct* j);
+static int jpeg_read_header_encap(OJPEGState* sp, jpeg_decompress_struct* cinfo, uint8 require_image);
+static int jpeg_start_decompress_encap(OJPEGState* sp, jpeg_decompress_struct* cinfo);
+static int jpeg_read_scanlines_encap(OJPEGState* sp, jpeg_decompress_struct* cinfo, void* scanlines, uint32 max_lines);
+static int jpeg_read_raw_data_encap(OJPEGState* sp, jpeg_decompress_struct* cinfo, void* data, uint32 max_lines);
+static void jpeg_encap_unwind(TIFF* tif);
+#endif
 
-#define TIFFTAG_WANG_PAGECONTROL 32934
+static void OJPEGLibjpegJpegErrorMgrOutputMessage(jpeg_common_struct* cinfo);
+static void OJPEGLibjpegJpegErrorMgrErrorExit(jpeg_common_struct* cinfo);
+static void OJPEGLibjpegJpegSourceMgrInitSource(jpeg_decompress_struct* cinfo);
+static boolean OJPEGLibjpegJpegSourceMgrFillInputBuffer(jpeg_decompress_struct* cinfo);
+static void OJPEGLibjpegJpegSourceMgrSkipInputData(jpeg_decompress_struct* cinfo, long num_bytes);
+static boolean OJPEGLibjpegJpegSourceMgrResyncToRestart(jpeg_decompress_struct* cinfo, int desired);
+static void OJPEGLibjpegJpegSourceMgrTermSource(jpeg_decompress_struct* cinfo);
 
-/* Bit-vector offsets for keeping track of TIFF records that we've parsed. */
+int
+TIFFInitOJPEG(TIFF* tif, int scheme)
+{
+       static const char module[]="TIFFInitOJPEG";
+       OJPEGState* sp;
+
+       assert(scheme==COMPRESSION_OJPEG);
+
+        /*
+        * Merge codec-specific tag information.
+        */
+       if (!_TIFFMergeFieldInfo(tif,ojpeg_field_info,FIELD_OJPEG_COUNT)) {
+               TIFFErrorExt(tif->tif_clientdata, module,
+                            "Merging Old JPEG codec-specific tags failed");
+               return 0;
+       }
 
-#define FIELD_JPEGPROC FIELD_CODEC
-#define FIELD_JPEGIFOFFSET (FIELD_CODEC+1)
-#define FIELD_JPEGIFBYTECOUNT (FIELD_CODEC+2)
-#define FIELD_JPEGRESTARTINTERVAL (FIELD_CODEC+3)
-#define FIELD_JPEGTABLES (FIELD_CODEC+4) /* New, post-6.0 JPEG-in-TIFF tag! */
-#define FIELD_JPEGLOSSLESSPREDICTORS (FIELD_CODEC+5)
-#define FIELD_JPEGPOINTTRANSFORM (FIELD_CODEC+6)
-#define FIELD_JPEGQTABLES (FIELD_CODEC+7)
-#define FIELD_JPEGDCTABLES (FIELD_CODEC+8)
-#define FIELD_JPEGACTABLES (FIELD_CODEC+9)
-#define FIELD_WANG_PAGECONTROL (FIELD_CODEC+10)
-#define FIELD_JPEGCOLORMODE (FIELD_CODEC+11)
+       /* state block */
+       sp=_TIFFmalloc(sizeof(OJPEGState));
+       if (sp==NULL)
+       {
+               TIFFErrorExt(tif->tif_clientdata,module,"No space for OJPEG state block");
+               return(0);
+       }
+       _TIFFmemset(sp,0,sizeof(OJPEGState));
+       sp->tif=tif;
+       sp->jpeg_proc=1;
+       sp->subsampling_hor=2;
+       sp->subsampling_ver=2;
+       TIFFSetField(tif,TIFFTAG_YCBCRSUBSAMPLING,2,2);
+       /* tif codec methods */
+       tif->tif_setupdecode=OJPEGSetupDecode;
+       tif->tif_predecode=OJPEGPreDecode;
+       tif->tif_postdecode=OJPEGPostDecode;
+       tif->tif_decoderow=OJPEGDecode;
+       tif->tif_decodestrip=OJPEGDecode;
+       tif->tif_decodetile=OJPEGDecode;
+       tif->tif_setupencode=OJPEGSetupEncode;
+       tif->tif_preencode=OJPEGPreEncode;
+       tif->tif_postencode=OJPEGPostEncode;
+       tif->tif_encoderow=OJPEGEncode;
+       tif->tif_encodestrip=OJPEGEncode;
+       tif->tif_encodetile=OJPEGEncode;
+       tif->tif_cleanup=OJPEGCleanup;
+       tif->tif_data=(tidata_t)sp;
+       /* tif tag methods */
+       sp->vgetparent=tif->tif_tagmethods.vgetfield;
+       tif->tif_tagmethods.vgetfield=OJPEGVGetField;
+       sp->vsetparent=tif->tif_tagmethods.vsetfield;
+       tif->tif_tagmethods.vsetfield=OJPEGVSetField;
+       tif->tif_tagmethods.printdir=OJPEGPrintDir;
+       /* Some OJPEG files don't have strip or tile offsets or bytecounts tags.
+          Some others do, but have totally meaningless or corrupt values
+          in these tags. In these cases, the JpegInterchangeFormat stream is
+          reliable. In any case, this decoder reads the compressed data itself,
+          from the most reliable locations, and we need to notify encapsulating
+          LibTiff not to read raw strips or tiles for us. */
+       tif->tif_flags|=TIFF_NOREADRAW;
+       return(1);
+}
 
-typedef struct jpeg_destination_mgr jpeg_destination_mgr;
-typedef struct jpeg_source_mgr jpeg_source_mgr;
-typedef struct jpeg_error_mgr jpeg_error_mgr;
+static int
+OJPEGVGetField(TIFF* tif, ttag_t tag, va_list ap)
+{
+       OJPEGState* sp=(OJPEGState*)tif->tif_data;
+       switch(tag)
+       {
+               case TIFFTAG_JPEGIFOFFSET:
+                       *va_arg(ap,uint32*)=(uint32)sp->jpeg_interchange_format;
+                       break;
+               case TIFFTAG_JPEGIFBYTECOUNT:
+                       *va_arg(ap,uint32*)=(uint32)sp->jpeg_interchange_format_length;
+                       break;
+               case TIFFTAG_YCBCRSUBSAMPLING:
+                       if (sp->subsamplingcorrect_done==0)
+                               OJPEGSubsamplingCorrect(tif);
+                       *va_arg(ap,uint16*)=(uint16)sp->subsampling_hor;
+                       *va_arg(ap,uint16*)=(uint16)sp->subsampling_ver;
+                       break;
+               case TIFFTAG_JPEGQTABLES:
+                       *va_arg(ap,uint32*)=(uint32)sp->qtable_offset_count;
+                       *va_arg(ap,void**)=(void*)sp->qtable_offset;
+                       break;
+               case TIFFTAG_JPEGDCTABLES:
+                       *va_arg(ap,uint32*)=(uint32)sp->dctable_offset_count;
+                       *va_arg(ap,void**)=(void*)sp->dctable_offset;
+                       break;
+               case TIFFTAG_JPEGACTABLES:
+                       *va_arg(ap,uint32*)=(uint32)sp->actable_offset_count;
+                       *va_arg(ap,void**)=(void*)sp->actable_offset;
+                       break;
+               case TIFFTAG_JPEGPROC:
+                       *va_arg(ap,uint16*)=(uint16)sp->jpeg_proc;
+                       break;
+               case TIFFTAG_JPEGRESTARTINTERVAL:
+                       *va_arg(ap,uint16*)=sp->restart_interval;
+                       break;
+               default:
+                       return (*sp->vgetparent)(tif,tag,ap);
+       }
+       return (1);
+}
 
-/* State variable for each open TIFF file that uses "libjpeg" for JPEG
-   decompression.  (Note:  This file should NEVER perform JPEG compression
-   except in the manner implemented by the "tif_jpeg.c" file, elsewhere in this
-   library; see comments above.)  JPEG Library internal state is recorded in a
-   "jpeg_{de}compress_struct", while a "jpeg_common_struct" records a few items
-   common to both compression and expansion.  The "cinfo" field containing JPEG
-   Library state MUST be the 1st member of our own state variable, so that we
-   can safely "cast" pointers back and forth.
-*/
-typedef struct             /* This module's private, per-image state variable */
-  {
-    union         /* JPEG Library state variable; this MUST be our 1st field! */
-      {
-        struct jpeg_compress_struct c;
-        struct jpeg_decompress_struct d;
-        struct jpeg_common_struct comm;
-      } cinfo;
-    jpeg_error_mgr err;                         /* JPEG Library error manager */
-    JMP_BUF exit_jmpbuf;             /* ...for catching JPEG Library failures */
-#   ifdef never
-
- /* (The following two fields could be a "union", but they're small enough that
-    it's not worth the effort.)
- */
-    jpeg_destination_mgr dest;             /* Destination for compressed data */
-#   endif
-    jpeg_source_mgr src;                           /* Source of expanded data */
-    JSAMPARRAY ds_buffer[MAX_COMPONENTS]; /* ->Temporary downsampling buffers */
-    TIFF *tif;                        /* Reverse pointer, needed by some code */
-    TIFFVGetMethod vgetparent;                    /* "Super class" methods... */
-    TIFFVSetMethod vsetparent;
-    TIFFStripMethod defsparent;
-    TIFFTileMethod deftparent;
-    void *jpegtables;           /* ->"New" JPEG tables, if we synthesized any */
-    uint32 is_WANG,    /* <=> Wang Imaging for Microsoft Windows output file? */
-           jpegtables_length;   /* Length of "new" JPEG tables, if they exist */
-    tsize_t bytesperline;          /* No. of decompressed Bytes per scan line */
-    int jpegquality,                             /* Compression quality level */
-        jpegtablesmode,                          /* What to put in JPEGTables */
-        samplesperclump,
-        scancount;                           /* No. of scan lines accumulated */
-    J_COLOR_SPACE photometric;          /* IJG JPEG Library's photometry code */
-    unsigned char h_sampling,                          /* Luminance sampling factors */
-           v_sampling,
-           jpegcolormode;           /* Who performs RGB <-> YCbCr conversion? */
-                       /* JPEGCOLORMODE_RAW <=> TIFF Library or its client */
-                       /* JPEGCOLORMODE_RGB <=> JPEG Library               */
-    /* These fields are added to support TIFFGetField */
-    uint16 jpegproc;
-    uint32 jpegifoffset;
-    uint32 jpegifbytecount;
-    uint32 jpegrestartinterval;
-    void* jpeglosslesspredictors;
-    uint16 jpeglosslesspredictors_length;
-    void* jpegpointtransform;
-    uint32 jpegpointtransform_length;
-    void* jpegqtables;
-    uint32 jpegqtables_length;
-    void* jpegdctables;
-    uint32 jpegdctables_length;
-    void* jpegactables;
-    uint32 jpegactables_length;
-
-  } OJPEGState;
-#define OJState(tif)((OJPEGState*)(tif)->tif_data)
-
-static const TIFFFieldInfo ojpegFieldInfo[]=/* JPEG-specific TIFF-record tags */
-  {
-
- /* This is the current JPEG-in-TIFF metadata-encapsulation tag, and its
-    treatment in this file is idiosyncratic.  It should never appear in a
-    "source" image conforming to the TIFF Version 6.0 specification, so we
-    arrange to report an error if it appears.  But in order to support possible
-    future conversion of "old" JPEG-in-TIFF encapsulations to "new" ones, we
-    might wish to synthesize an equivalent value to be returned by the TIFF
-    Library's "getfield" method.  So, this table tells the TIFF Library to pass
-    these records to us in order to filter them below.
- */
-    {
-      TIFFTAG_JPEGTABLES            ,TIFF_VARIABLE2,TIFF_VARIABLE2,
-      TIFF_UNDEFINED,FIELD_JPEGTABLES            ,FALSE,TRUE ,"JPEGTables"
-    },
-
- /* These tags are defined by the TIFF Version 6.0 specification and are now
-    obsolete.  This module reads them from an old "source" image, but it never
-    writes them to a new "destination" image.
- */
-    {
-      TIFFTAG_JPEGPROC              ,1            ,1            ,
-      TIFF_SHORT    ,FIELD_JPEGPROC              ,FALSE,FALSE,"JPEGProc"
-    },
-    {
-      TIFFTAG_JPEGIFOFFSET          ,1            ,1            ,
-      TIFF_LONG     ,FIELD_JPEGIFOFFSET          ,FALSE,FALSE,"JPEGInterchangeFormat"
-    },
-    {
-      TIFFTAG_JPEGIFBYTECOUNT       ,1            ,1            ,
-      TIFF_LONG     ,FIELD_JPEGIFBYTECOUNT       ,FALSE,FALSE,"JPEGInterchangeFormatLength"
-    },
-    {
-      TIFFTAG_JPEGRESTARTINTERVAL   ,1            ,1            ,
-      TIFF_SHORT    ,FIELD_JPEGRESTARTINTERVAL   ,FALSE,FALSE,"JPEGRestartInterval"
-    },
-    {
-      TIFFTAG_JPEGLOSSLESSPREDICTORS,TIFF_VARIABLE,TIFF_VARIABLE,
-      TIFF_SHORT    ,FIELD_JPEGLOSSLESSPREDICTORS,FALSE,TRUE ,"JPEGLosslessPredictors"
-    },
-    {
-      TIFFTAG_JPEGPOINTTRANSFORM    ,TIFF_VARIABLE,TIFF_VARIABLE,
-      TIFF_SHORT    ,FIELD_JPEGPOINTTRANSFORM    ,FALSE,TRUE ,"JPEGPointTransforms"
-    },
-    {
-      TIFFTAG_JPEGQTABLES           ,TIFF_VARIABLE,TIFF_VARIABLE,
-      TIFF_LONG     ,FIELD_JPEGQTABLES           ,FALSE,TRUE ,"JPEGQTables"
-    },
-    {
-      TIFFTAG_JPEGDCTABLES          ,TIFF_VARIABLE,TIFF_VARIABLE,
-      TIFF_LONG     ,FIELD_JPEGDCTABLES          ,FALSE,TRUE ,"JPEGDCTables"
-    },
-    {
-      TIFFTAG_JPEGACTABLES          ,TIFF_VARIABLE,TIFF_VARIABLE,
-      TIFF_LONG     ,FIELD_JPEGACTABLES          ,FALSE,TRUE ,"JPEGACTables"
-    },
-    {
-      TIFFTAG_WANG_PAGECONTROL      ,TIFF_VARIABLE,1            ,
-      TIFF_LONG     ,FIELD_WANG_PAGECONTROL      ,FALSE,FALSE,"WANG PageControl"
-    },
-
- /* This is a pseudo tag intended for internal use only by the TIFF Library and
-    its clients, which should never appear in an input/output image file.  It
-    specifies whether the TIFF Library (or its client) should do YCbCr <-> RGB
-    color-space conversion (JPEGCOLORMODE_RAW <=> 0) or whether we should ask
-    the JPEG Library to do it (JPEGCOLORMODE_RGB <=> 1).
- */
-    {
-      TIFFTAG_JPEGCOLORMODE         ,0            ,0            ,
-      TIFF_ANY      ,FIELD_PSEUDO                ,FALSE,FALSE,"JPEGColorMode"
-    }
-  };
-static const char JPEGLib_name[]={"JPEG Library"},
-                  bad_bps[]={"%u BitsPerSample not allowed for JPEG"},
-                  bad_photometry[]={"PhotometricInterpretation %u not allowed for JPEG"},
-                  bad_subsampling[]={"invalid YCbCr subsampling factor(s)"},
-#                 ifdef never
-                  no_write_frac[]={"fractional scan line discarded"},
-#                 endif
-                  no_read_frac[]={"fractional scan line not read"},
-                  no_jtable_space[]={"No space for JPEGTables"};
-
-/* The following diagnostic subroutines interface with and replace default
-   subroutines in the JPEG Library.  Our basic strategy is to use "setjmp()"/
-   "longjmp()" in order to return control to the TIFF Library when the JPEG
-   library detects an error, and to use TIFF Library subroutines for displaying
-   diagnostic messages to a client application.
-*/
-static void
-TIFFojpeg_error_exit(register j_common_ptr cinfo)
+static int
+OJPEGVSetField(TIFF* tif, ttag_t tag, va_list ap)
 {
-    char buffer[JMSG_LENGTH_MAX];
-    int code = cinfo->err->msg_code;
-
-    if (((OJPEGState *)cinfo)->is_WANG) {
-       if (code == JERR_SOF_DUPLICATE || code == JERR_SOI_DUPLICATE)
-           return;         /* ignore it */
-    }
-
-    (*cinfo->err->format_message)(cinfo,buffer);
-    TIFFError(JPEGLib_name,buffer); /* Display error message */
-    jpeg_abort(cinfo); /* Clean up JPEG Library state */
-    LONGJMP(((OJPEGState *)cinfo)->exit_jmpbuf,1); /* Return to TIFF client */
+       static const char module[]="OJPEGVSetField";
+       OJPEGState* sp=(OJPEGState*)tif->tif_data;
+       uint32 ma;
+       uint32* mb;
+       uint32 n;
+       switch(tag)
+       {
+               case TIFFTAG_JPEGIFOFFSET:
+                       sp->jpeg_interchange_format=(toff_t)va_arg(ap,uint32);  
+                       break;
+               case TIFFTAG_JPEGIFBYTECOUNT:
+                       sp->jpeg_interchange_format_length=(toff_t)va_arg(ap,uint32);  
+                       break;
+               case TIFFTAG_YCBCRSUBSAMPLING:
+                       sp->subsampling_tag=1;
+                       sp->subsampling_hor=(uint8)va_arg(ap,int);
+                       sp->subsampling_ver=(uint8)va_arg(ap,int);
+                       tif->tif_dir.td_ycbcrsubsampling[0]=sp->subsampling_hor;
+                       tif->tif_dir.td_ycbcrsubsampling[1]=sp->subsampling_ver;
+                       break;
+               case TIFFTAG_JPEGQTABLES:
+                       ma=va_arg(ap,uint32);
+                       if (ma!=0)
+                       {
+                               if (ma>3)
+                               {
+                                       TIFFErrorExt(tif->tif_clientdata,module,"JpegQTables tag has incorrect count");
+                                       return(0);
+                               }
+                               sp->qtable_offset_count=(uint8)ma;
+                               mb=va_arg(ap,uint32*);
+                               for (n=0; n<ma; n++)
+                                       sp->qtable_offset[n]=(toff_t)mb[n];
+                       }
+                       break;
+               case TIFFTAG_JPEGDCTABLES:
+                       ma=va_arg(ap,uint32);
+                       if (ma!=0)
+                       {
+                               if (ma>3)
+                               {
+                                       TIFFErrorExt(tif->tif_clientdata,module,"JpegDcTables tag has incorrect count");
+                                       return(0);
+                               }
+                               sp->dctable_offset_count=(uint8)ma;
+                               mb=va_arg(ap,uint32*);
+                               for (n=0; n<ma; n++)
+                                       sp->dctable_offset[n]=(toff_t)mb[n];
+                       }
+                       break;
+               case TIFFTAG_JPEGACTABLES:
+                       ma=va_arg(ap,uint32);
+                       if (ma!=0)
+                       {
+                               if (ma>3)
+                               {
+                                       TIFFErrorExt(tif->tif_clientdata,module,"JpegAcTables tag has incorrect count");
+                                       return(0);
+                               }
+                               sp->actable_offset_count=(uint8)ma;
+                               mb=va_arg(ap,uint32*);
+                               for (n=0; n<ma; n++)
+                                       sp->actable_offset[n]=(toff_t)mb[n];
+                       }
+                       break;
+               case TIFFTAG_JPEGPROC:
+                       sp->jpeg_proc=(uint8)va_arg(ap,uint32);
+                       break;
+               case TIFFTAG_JPEGRESTARTINTERVAL:
+                       sp->restart_interval=(uint16)va_arg(ap,uint32);
+                       break;
+               default:
+                       return (*sp->vsetparent)(tif,tag,ap);
+       }
+       TIFFSetFieldBit(tif,_TIFFFieldWithTag(tif,tag)->field_bit);
+       tif->tif_flags|=TIFF_DIRTYDIRECT;
+       return(1);
 }
 
 static void
-TIFFojpeg_output_message(register j_common_ptr cinfo)
-  { char buffer[JMSG_LENGTH_MAX];
+OJPEGPrintDir(TIFF* tif, FILE* fd, long flags)
+{
+       OJPEGState* sp=(OJPEGState*)tif->tif_data;
+       uint8 m;
+       (void)flags;
+       assert(sp!=NULL);
+       if (TIFFFieldSet(tif,FIELD_OJPEG_JPEGINTERCHANGEFORMAT))
+               fprintf(fd,"  JpegInterchangeFormat: %lu\n",(unsigned long)sp->jpeg_interchange_format);
+       if (TIFFFieldSet(tif,FIELD_OJPEG_JPEGINTERCHANGEFORMATLENGTH))
+               fprintf(fd,"  JpegInterchangeFormatLength: %lu\n",(unsigned long)sp->jpeg_interchange_format_length);
+       if (TIFFFieldSet(tif,FIELD_OJPEG_JPEGQTABLES))
+       {
+               fprintf(fd,"  JpegQTables:");
+               for (m=0; m<sp->qtable_offset_count; m++)
+                       fprintf(fd," %lu",(unsigned long)sp->qtable_offset[m]);
+               fprintf(fd,"\n");
+       }
+       if (TIFFFieldSet(tif,FIELD_OJPEG_JPEGDCTABLES))
+       {
+               fprintf(fd,"  JpegDcTables:");
+               for (m=0; m<sp->dctable_offset_count; m++)
+                       fprintf(fd," %lu",(unsigned long)sp->dctable_offset[m]);
+               fprintf(fd,"\n");
+       }
+       if (TIFFFieldSet(tif,FIELD_OJPEG_JPEGACTABLES))
+       {
+               fprintf(fd,"  JpegAcTables:");
+               for (m=0; m<sp->actable_offset_count; m++)
+                       fprintf(fd," %lu",(unsigned long)sp->actable_offset[m]);
+               fprintf(fd,"\n");
+       }
+       if (TIFFFieldSet(tif,FIELD_OJPEG_JPEGPROC))
+               fprintf(fd,"  JpegProc: %u\n",(unsigned int)sp->jpeg_proc);
+       if (TIFFFieldSet(tif,FIELD_OJPEG_JPEGRESTARTINTERVAL))
+               fprintf(fd,"  JpegRestartInterval: %u\n",(unsigned int)sp->restart_interval);
+}
 
- /* This subroutine is invoked only for warning messages, since the JPEG
-    Library's "error_exit" method does its own thing and "trace_level" is never
-    set > 0.
- */
-    (*cinfo->err->format_message)(cinfo,buffer);
-    TIFFWarning(JPEGLib_name,buffer);
-  }
+static int
+OJPEGSetupDecode(TIFF* tif)
+{
+       static const char module[]="OJPEGSetupDecode";
+       TIFFWarningExt(tif->tif_clientdata,module,"Depreciated and troublesome old-style JPEG compression mode, please convert to new-style JPEG compression and notify vendor of writing software");
+       return(1);
+}
 
-/* The following subroutines, which also interface with the JPEG Library, exist
-   mainly in limit the side effects of "setjmp()" and convert JPEG normal/error
-   conditions into TIFF Library return codes.
-*/
-#define CALLJPEG(sp,fail,op)(SETJMP((sp)->exit_jmpbuf)?(fail):(op))
-#define CALLVJPEG(sp,op)CALLJPEG(sp,0,((op),1))
-#ifdef never
+static int
+OJPEGPreDecode(TIFF* tif, tsample_t s)
+{
+       OJPEGState* sp=(OJPEGState*)tif->tif_data;
+       tstrile_t m;
+       if (sp->subsamplingcorrect_done==0)
+               OJPEGSubsamplingCorrect(tif);
+       if (sp->readheader_done==0)
+       {
+               if (OJPEGReadHeaderInfo(tif)==0)
+                       return(0);
+       }
+       if (sp->sos_end[s].log==0)
+       {
+               if (OJPEGReadSecondarySos(tif,s)==0)
+                       return(0);
+       }
+       if isTiled(tif)
+               m=(tstrile_t)tif->tif_curtile;
+       else
+               m=(tstrile_t)tif->tif_curstrip;
+       if ((sp->writeheader_done!=0) && ((sp->write_cursample!=s) || (sp->write_curstrile>m)))
+       {
+               if (sp->libjpeg_session_active!=0)
+                       OJPEGLibjpegSessionAbort(tif);
+               sp->writeheader_done=0;
+       }
+       if (sp->writeheader_done==0)
+       {
+               sp->plane_sample_offset=s;
+               sp->write_cursample=s;
+               sp->write_curstrile=s*tif->tif_dir.td_stripsperimage;
+               if ((sp->in_buffer_file_pos_log==0) ||
+                   (sp->in_buffer_file_pos-sp->in_buffer_togo!=sp->sos_end[s].in_buffer_file_pos))
+               {
+                       sp->in_buffer_source=sp->sos_end[s].in_buffer_source;
+                       sp->in_buffer_next_strile=sp->sos_end[s].in_buffer_next_strile;
+                       sp->in_buffer_file_pos=sp->sos_end[s].in_buffer_file_pos;
+                       sp->in_buffer_file_pos_log=0;
+                       sp->in_buffer_file_togo=sp->sos_end[s].in_buffer_file_togo;
+                       sp->in_buffer_togo=0;
+                       sp->in_buffer_cur=0;
+               }
+               if (OJPEGWriteHeaderInfo(tif)==0)
+                       return(0);
+       }
+       while (sp->write_curstrile<m)          
+       {
+               if (sp->libjpeg_jpeg_query_style==0)
+               {
+                       if (OJPEGPreDecodeSkipRaw(tif)==0)
+                               return(0);
+               }
+               else
+               {
+                       if (OJPEGPreDecodeSkipScanlines(tif)==0)
+                               return(0);
+               }
+               sp->write_curstrile++;
+       }
+       return(1);
+}
 
 static int
-TIFFojpeg_create_compress(register OJPEGState *sp)
-  {
-    sp->cinfo.c.err = jpeg_std_error(&sp->err); /* Initialize error handling */
-    sp->err.error_exit = TIFFojpeg_error_exit;
-    sp->err.output_message = TIFFojpeg_output_message;
-    return CALLVJPEG(sp,jpeg_create_compress(&sp->cinfo.c));
-  }
-
-/* The following subroutines comprise a JPEG Library "destination" data manager
-   by directing compressed data from the JPEG Library to a TIFF Library output
-   buffer.
-*/
-static void
-std_init_destination(register j_compress_ptr cinfo){} /* "Dummy" stub */
+OJPEGPreDecodeSkipRaw(TIFF* tif)
+{
+       OJPEGState* sp=(OJPEGState*)tif->tif_data;
+       uint32 m;
+       m=sp->lines_per_strile;
+       if (sp->subsampling_convert_state!=0)
+       {
+               if (sp->subsampling_convert_clines-sp->subsampling_convert_state>=m)
+               {
+                       sp->subsampling_convert_state+=m;
+                       if (sp->subsampling_convert_state==sp->subsampling_convert_clines)
+                               sp->subsampling_convert_state=0;
+                       return(1);
+               }
+               m-=sp->subsampling_convert_clines-sp->subsampling_convert_state;
+               sp->subsampling_convert_state=0;
+       }
+       while (m>=sp->subsampling_convert_clines)
+       {
+               if (jpeg_read_raw_data_encap(sp,&(sp->libjpeg_jpeg_decompress_struct),sp->subsampling_convert_ycbcrimage,sp->subsampling_ver*8)==0)
+                       return(0);
+               m-=sp->subsampling_convert_clines;
+       }
+       if (m>0)
+       {
+               if (jpeg_read_raw_data_encap(sp,&(sp->libjpeg_jpeg_decompress_struct),sp->subsampling_convert_ycbcrimage,sp->subsampling_ver*8)==0)
+                       return(0);
+               sp->subsampling_convert_state=m;
+       }
+       return(1);
+}
 
-static boolean
-std_empty_output_buffer(register j_compress_ptr cinfo)
-  {
-#   define sp ((OJPEGState *)cinfo)
-    register TIFF *tif = sp->tif;
-
-    tif->tif_rawcc = tif->tif_rawdatasize; /* Entire buffer has been filled */
-    TIFFFlushData1(tif);
-    sp->dest.next_output_byte = (JOCTET *)tif->tif_rawdata;
-    sp->dest.free_in_buffer = (size_t)tif->tif_rawdatasize;
-    return TRUE;
-#   undef sp
-  }
+static int
+OJPEGPreDecodeSkipScanlines(TIFF* tif)
+{
+       static const char module[]="OJPEGPreDecodeSkipScanlines";
+       OJPEGState* sp=(OJPEGState*)tif->tif_data;
+       uint32 m;
+       if (sp->skip_buffer==NULL)
+       {
+               sp->skip_buffer=_TIFFmalloc(sp->bytes_per_line);
+               if (sp->skip_buffer==NULL)
+               {
+                       TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
+                       return(0);
+               }
+       }
+       for (m=0; m<sp->lines_per_strile; m++)
+       {
+               if (jpeg_read_scanlines_encap(sp,&(sp->libjpeg_jpeg_decompress_struct),&sp->skip_buffer,1)==0)
+                       return(0);
+       }
+       return(1);
+}
 
-static void
-std_term_destination(register j_compress_ptr cinfo)
-  {
-#   define sp ((OJPEGState *)cinfo)
-    register TIFF *tif = sp->tif;
+static int
+OJPEGDecode(TIFF* tif, tidata_t buf, tsize_t cc, tsample_t s)
+{
+       OJPEGState* sp=(OJPEGState*)tif->tif_data;
+       (void)s;
+       if (sp->libjpeg_jpeg_query_style==0)
+       {
+               if (OJPEGDecodeRaw(tif,buf,cc)==0)
+                       return(0);
+       }
+       else
+       {
+               if (OJPEGDecodeScanlines(tif,buf,cc)==0)
+                       return(0);
+       }
+       return(1);
+}
 
- /* NB: The TIFF Library does the final buffer flush. */
-    tif->tif_rawcp = (tidata_t)sp->dest.next_output_byte;
-    tif->tif_rawcc = tif->tif_rawdatasize - (tsize_t)sp->dest.free_in_buffer;
-#   undef sp
-  }
+static int
+OJPEGDecodeRaw(TIFF* tif, tidata_t buf, tsize_t cc)
+{
+       static const char module[]="OJPEGDecodeRaw";
+       OJPEGState* sp=(OJPEGState*)tif->tif_data;
+       uint8* m;
+       uint32 n;
+       uint8* oy;
+       uint8* ocb;
+       uint8* ocr;
+       uint8* p;
+       uint32 q;
+       uint8* r;
+       uint8 sx,sy;
+       if (cc%sp->bytes_per_line!=0)
+       {
+               TIFFErrorExt(tif->tif_clientdata,module,"Fractional scanline not read");
+               return(0);
+       }
+       assert(cc>0);
+       m=buf;
+       n=cc;
+       do
+       {
+               if (sp->subsampling_convert_state==0)
+               {
+                       if (jpeg_read_raw_data_encap(sp,&(sp->libjpeg_jpeg_decompress_struct),sp->subsampling_convert_ycbcrimage,sp->subsampling_ver*8)==0)
+                               return(0);
+               }
+               oy=sp->subsampling_convert_ybuf+sp->subsampling_convert_state*sp->subsampling_ver*sp->subsampling_convert_ylinelen;
+               ocb=sp->subsampling_convert_cbbuf+sp->subsampling_convert_state*sp->subsampling_convert_clinelen;
+               ocr=sp->subsampling_convert_crbuf+sp->subsampling_convert_state*sp->subsampling_convert_clinelen;
+               p=m;
+               for (q=0; q<sp->subsampling_convert_clinelenout; q++)
+               {
+                       r=oy;
+                       for (sy=0; sy<sp->subsampling_ver; sy++)
+                       {
+                               for (sx=0; sx<sp->subsampling_hor; sx++)
+                                       *p++=*r++;
+                               r+=sp->subsampling_convert_ylinelen-sp->subsampling_hor;
+                       }
+                       oy+=sp->subsampling_hor;
+                       *p++=*ocb++;
+                       *p++=*ocr++;
+               }
+               sp->subsampling_convert_state++;
+               if (sp->subsampling_convert_state==sp->subsampling_convert_clines)
+                       sp->subsampling_convert_state=0;
+               m+=sp->bytes_per_line;
+               n-=sp->bytes_per_line;
+       } while(n>0);
+       return(1);
+}
 
-/* Alternate destination manager to output JPEGTables field: */
+static int
+OJPEGDecodeScanlines(TIFF* tif, tidata_t buf, tsize_t cc)
+{
+       static const char module[]="OJPEGDecodeScanlines";
+       OJPEGState* sp=(OJPEGState*)tif->tif_data;
+       uint8* m;
+       uint32 n;
+       if (cc%sp->bytes_per_line!=0)
+       {
+               TIFFErrorExt(tif->tif_clientdata,module,"Fractional scanline not read");
+               return(0);
+       }
+       assert(cc>0);
+       m=buf;
+       n=cc;
+       do
+       {
+               if (jpeg_read_scanlines_encap(sp,&(sp->libjpeg_jpeg_decompress_struct),&m,1)==0)
+                       return(0);
+               m+=sp->bytes_per_line;
+               n-=sp->bytes_per_line;
+       } while(n>0);
+       return(1);
+}
 
 static void
-tables_init_destination(register j_compress_ptr cinfo)
-  {
-#   define sp ((OJPEGState *)cinfo)
- /* The "jpegtables_length" field is the allocated buffer size while building */
-    sp->dest.next_output_byte = (JOCTET *)sp->jpegtables;
-    sp->dest.free_in_buffer = (size_t)sp->jpegtables_length;
-#   undef sp
-  }
+OJPEGPostDecode(TIFF* tif, tidata_t buf, tsize_t cc)
+{
+       OJPEGState* sp=(OJPEGState*)tif->tif_data;
+       (void)buf;
+       (void)cc;
+       sp->write_curstrile++;
+       if (sp->write_curstrile%tif->tif_dir.td_stripsperimage==0)
+       {
+               assert(sp->libjpeg_session_active!=0);
+               OJPEGLibjpegSessionAbort(tif);
+               sp->writeheader_done=0;
+       }
+}
 
-static boolean
-tables_empty_output_buffer(register j_compress_ptr cinfo)
-  { void *newbuf;
-#   define sp ((OJPEGState *)cinfo)
-
- /* The entire buffer has been filled, so enlarge it by 1000 bytes. */
-    if (!( newbuf = _TIFFrealloc( (tdata_t)sp->jpegtables
-                                , (tsize_t)(sp->jpegtables_length + 1000)
-                                )
-         )
-       ) ERREXIT1(cinfo,JERR_OUT_OF_MEMORY,100);
-    sp->dest.next_output_byte = (JOCTET *)newbuf + sp->jpegtables_length;
-    sp->dest.free_in_buffer = (size_t)1000;
-    sp->jpegtables = newbuf;
-    sp->jpegtables_length += 1000;
-    return TRUE;
-#   undef sp
-  }
+static int
+OJPEGSetupEncode(TIFF* tif)
+{
+       static const char module[]="OJPEGSetupEncode";
+       TIFFErrorExt(tif->tif_clientdata,module,"OJPEG encoding not supported; use new-style JPEG compression instead");
+       return(0);
+}
+
+static int
+OJPEGPreEncode(TIFF* tif, tsample_t s)
+{
+       static const char module[]="OJPEGPreEncode";
+       (void)s;
+       TIFFErrorExt(tif->tif_clientdata,module,"OJPEG encoding not supported; use new-style JPEG compression instead");
+       return(0);
+}
+
+static int
+OJPEGEncode(TIFF* tif, tidata_t buf, tsize_t cc, tsample_t s)
+{
+       static const char module[]="OJPEGEncode";
+       (void)buf;
+       (void)cc;
+       (void)s;
+       TIFFErrorExt(tif->tif_clientdata,module,"OJPEG encoding not supported; use new-style JPEG compression instead");
+       return(0);
+}
+
+static int
+OJPEGPostEncode(TIFF* tif)
+{
+       static const char module[]="OJPEGPostEncode";
+       TIFFErrorExt(tif->tif_clientdata,module,"OJPEG encoding not supported; use new-style JPEG compression instead");
+       return(0);
+}
 
 static void
-tables_term_destination(register j_compress_ptr cinfo)
-  {
-#   define sp ((OJPEGState *)cinfo)
- /* Set tables length to no. of Bytes actually emitted. */
-    sp->jpegtables_length -= sp->dest.free_in_buffer;
-#   undef sp
-  }
-
-/*ARGSUSED*/ static int
-TIFFojpeg_tables_dest(register OJPEGState *sp, TIFF *tif)
-  {
-
- /* Allocate a working buffer for building tables.  The initial size is 1000
-    Bytes, which is usually adequate.
- */
-    if (sp->jpegtables) _TIFFfree(sp->jpegtables);
-    if (!(sp->jpegtables = (void*)
-                           _TIFFmalloc((tsize_t)(sp->jpegtables_length = 1000))
-         )
-       )
-      {
-        sp->jpegtables_length = 0;
-        TIFFError("TIFFojpeg_tables_dest",no_jtable_space);
-        return 0;
-      };
-    sp->cinfo.c.dest = &sp->dest;
-    sp->dest.init_destination = tables_init_destination;
-    sp->dest.empty_output_buffer = tables_empty_output_buffer;
-    sp->dest.term_destination = tables_term_destination;
-    return 1;
-  }
-#else /* well, hardly ever */
+OJPEGCleanup(TIFF* tif)
+{
+       OJPEGState* sp=(OJPEGState*)tif->tif_data;
+       if (sp!=0)
+       {
+               tif->tif_tagmethods.vgetfield=sp->vgetparent;
+               tif->tif_tagmethods.vsetfield=sp->vsetparent;
+               if (sp->qtable[0]!=0)
+                       _TIFFfree(sp->qtable[0]);
+               if (sp->qtable[1]!=0)
+                       _TIFFfree(sp->qtable[1]);
+               if (sp->qtable[2]!=0)
+                       _TIFFfree(sp->qtable[2]);
+               if (sp->qtable[3]!=0)
+                       _TIFFfree(sp->qtable[3]);
+               if (sp->dctable[0]!=0)
+                       _TIFFfree(sp->dctable[0]);
+               if (sp->dctable[1]!=0)
+                       _TIFFfree(sp->dctable[1]);
+               if (sp->dctable[2]!=0)
+                       _TIFFfree(sp->dctable[2]);
+               if (sp->dctable[3]!=0)
+                       _TIFFfree(sp->dctable[3]);
+               if (sp->actable[0]!=0)
+                       _TIFFfree(sp->actable[0]);
+               if (sp->actable[1]!=0)
+                       _TIFFfree(sp->actable[1]);
+               if (sp->actable[2]!=0)
+                       _TIFFfree(sp->actable[2]);
+               if (sp->actable[3]!=0)
+                       _TIFFfree(sp->actable[3]);
+               if (sp->libjpeg_session_active!=0)
+                       OJPEGLibjpegSessionAbort(tif);
+               if (sp->subsampling_convert_ycbcrbuf!=0)
+                       _TIFFfree(sp->subsampling_convert_ycbcrbuf);
+               if (sp->subsampling_convert_ycbcrimage!=0)
+                       _TIFFfree(sp->subsampling_convert_ycbcrimage);
+               if (sp->skip_buffer!=0)
+                       _TIFFfree(sp->skip_buffer);
+               _TIFFfree(sp);
+               tif->tif_data=NULL;
+               _TIFFSetDefaultCompressionState(tif);
+       }
+}
+
+static void
+OJPEGSubsamplingCorrect(TIFF* tif)
+{
+       static const char module[]="OJPEGSubsamplingCorrect";
+       OJPEGState* sp=(OJPEGState*)tif->tif_data;
+       uint8 mh;
+       uint8 mv;
+       assert(sp->subsamplingcorrect_done==0);
+       if ((tif->tif_dir.td_samplesperpixel!=3) || ((tif->tif_dir.td_photometric!=PHOTOMETRIC_YCBCR) &&
+           (tif->tif_dir.td_photometric!=PHOTOMETRIC_ITULAB)))
+       {
+               if (sp->subsampling_tag!=0)
+                       TIFFWarningExt(tif->tif_clientdata,module,"Subsampling tag not appropriate for this Photometric and/or SamplesPerPixel");
+               sp->subsampling_hor=1;
+               sp->subsampling_ver=1;
+               sp->subsampling_force_desubsampling_inside_decompression=0;
+       }
+       else
+       {
+               sp->subsamplingcorrect_done=1;
+               mh=sp->subsampling_hor;
+               mv=sp->subsampling_ver;
+               sp->subsamplingcorrect=1;
+               OJPEGReadHeaderInfoSec(tif);
+               if (sp->subsampling_force_desubsampling_inside_decompression!=0)
+               {
+                       sp->subsampling_hor=1;
+                       sp->subsampling_ver=1;
+               }
+               sp->subsamplingcorrect=0;
+               if (((sp->subsampling_hor!=mh) || (sp->subsampling_ver!=mv)) && (sp->subsampling_force_desubsampling_inside_decompression==0))
+               {
+                       if (sp->subsampling_tag==0)
+                               TIFFWarningExt(tif->tif_clientdata,module,"Subsampling tag is not set, yet subsampling inside JPEG data [%d,%d] does not match default values [2,2]; assuming subsampling inside JPEG data is correct",sp->subsampling_hor,sp->subsampling_ver);
+                       else
+                               TIFFWarningExt(tif->tif_clientdata,module,"Subsampling inside JPEG data [%d,%d] does not match subsampling tag values [%d,%d]; assuming subsampling inside JPEG data is correct",sp->subsampling_hor,sp->subsampling_ver,mh,mv);
+               }
+               if (sp->subsampling_force_desubsampling_inside_decompression!=0)
+               {
+                       if (sp->subsampling_tag==0)
+                               TIFFWarningExt(tif->tif_clientdata,module,"Subsampling tag is not set, yet subsampling inside JPEG data does not match default values [2,2] (nor any other values allowed in TIFF); assuming subsampling inside JPEG data is correct and desubsampling inside JPEG decompression");
+                       else
+                               TIFFWarningExt(tif->tif_clientdata,module,"Subsampling inside JPEG data does not match subsampling tag values [%d,%d] (nor any other values allowed in TIFF); assuming subsampling inside JPEG data is correct and desubsampling inside JPEG decompression",mh,mv);
+               }
+               if (sp->subsampling_force_desubsampling_inside_decompression==0)
+               {
+                       if (sp->subsampling_hor<sp->subsampling_ver)
+                               TIFFWarningExt(tif->tif_clientdata,module,"Subsampling values [%d,%d] are not allowed in TIFF",sp->subsampling_hor,sp->subsampling_ver);
+               }
+       }
+       sp->subsamplingcorrect_done=1;
+}
 
 static int
-_notSupported(register TIFF *tif)
-  { const TIFFCodec *c = TIFFFindCODEC(tif->tif_dir.td_compression);
+OJPEGReadHeaderInfo(TIFF* tif)
+{
+       static const char module[]="OJPEGReadHeaderInfo";
+       OJPEGState* sp=(OJPEGState*)tif->tif_data;
+       assert(sp->readheader_done==0);
+       sp->image_width=tif->tif_dir.td_imagewidth;
+       sp->image_length=tif->tif_dir.td_imagelength;
+       if isTiled(tif)
+       {
+               sp->strile_width=tif->tif_dir.td_tilewidth;
+               sp->strile_length=tif->tif_dir.td_tilelength;
+               sp->strile_length_total=((sp->image_length+sp->strile_length-1)/sp->strile_length)*sp->strile_length;
+       }
+       else
+       {
+               sp->strile_width=sp->image_width;
+               sp->strile_length=tif->tif_dir.td_rowsperstrip;
+               sp->strile_length_total=sp->image_length;
+       }
+       sp->samples_per_pixel=tif->tif_dir.td_samplesperpixel;
+       if (sp->samples_per_pixel==1)
+       {
+               sp->plane_sample_offset=0;
+               sp->samples_per_pixel_per_plane=sp->samples_per_pixel;
+               sp->subsampling_hor=1;
+               sp->subsampling_ver=1;
+       }
+       else
+       {
+               if (sp->samples_per_pixel!=3)
+               {
+                       TIFFErrorExt(tif->tif_clientdata,module,"SamplesPerPixel %d not supported for this compression scheme",sp->samples_per_pixel);
+                       return(0);
+               }
+               sp->plane_sample_offset=0;
+               if (tif->tif_dir.td_planarconfig==PLANARCONFIG_CONTIG)
+                       sp->samples_per_pixel_per_plane=3;
+               else
+                       sp->samples_per_pixel_per_plane=1;
+       }
+       if (sp->strile_length<sp->image_length)
+       {
+               if (sp->strile_length%(sp->subsampling_ver*8)!=0)
+               {
+                       TIFFErrorExt(tif->tif_clientdata,module,"Incompatible vertical subsampling and image strip/tile length");
+                       return(0);
+               }
+               sp->restart_interval=((sp->strile_width+sp->subsampling_hor*8-1)/(sp->subsampling_hor*8))*(sp->strile_length/(sp->subsampling_ver*8));
+       }
+       if (OJPEGReadHeaderInfoSec(tif)==0)
+               return(0);
+       sp->sos_end[0].log=1;
+       sp->sos_end[0].in_buffer_source=sp->in_buffer_source;
+       sp->sos_end[0].in_buffer_next_strile=sp->in_buffer_next_strile;
+       sp->sos_end[0].in_buffer_file_pos=sp->in_buffer_file_pos-sp->in_buffer_togo;
+       sp->sos_end[0].in_buffer_file_togo=sp->in_buffer_file_togo+sp->in_buffer_togo;
+       sp->readheader_done=1;
+       return(1);
+}
 
-    TIFFError(tif->tif_name,"%s compression not supported",c->name);
-    return 0;
-  }
-#endif /* never */
+static int
+OJPEGReadSecondarySos(TIFF* tif, tsample_t s)
+{
+       OJPEGState* sp=(OJPEGState*)tif->tif_data;
+       uint8 m;
+       assert(s>0);
+       assert(s<3);
+       assert(sp->sos_end[0].log!=0);
+       assert(sp->sos_end[s].log==0);
+       sp->plane_sample_offset=s-1;
+       while(sp->sos_end[sp->plane_sample_offset].log==0)
+               sp->plane_sample_offset--;
+       sp->in_buffer_source=sp->sos_end[sp->plane_sample_offset].in_buffer_source;
+       sp->in_buffer_next_strile=sp->sos_end[sp->plane_sample_offset].in_buffer_next_strile;
+       sp->in_buffer_file_pos=sp->sos_end[sp->plane_sample_offset].in_buffer_file_pos;  
+       sp->in_buffer_file_pos_log=0;
+       sp->in_buffer_file_togo=sp->sos_end[sp->plane_sample_offset].in_buffer_file_togo;
+       sp->in_buffer_togo=0;
+       sp->in_buffer_cur=0;
+       while(sp->plane_sample_offset<s)
+       {
+               do
+               {
+                       if (OJPEGReadByte(sp,&m)==0)
+                               return(0);
+                       if (m==255)
+                       {
+                               do
+                               {
+                                       if (OJPEGReadByte(sp,&m)==0)
+                                               return(0);
+                                       if (m!=255)
+                                               break;
+                               } while(1);
+                               if (m==JPEG_MARKER_SOS)
+                                       break;
+                       }
+               } while(1);
+               sp->plane_sample_offset++;
+               if (OJPEGReadHeaderInfoSecStreamSos(tif)==0)
+                       return(0);
+               sp->sos_end[sp->plane_sample_offset].log=1;
+               sp->sos_end[sp->plane_sample_offset].in_buffer_source=sp->in_buffer_source;
+               sp->sos_end[sp->plane_sample_offset].in_buffer_next_strile=sp->in_buffer_next_strile;
+               sp->sos_end[sp->plane_sample_offset].in_buffer_file_pos=sp->in_buffer_file_pos-sp->in_buffer_togo;
+               sp->sos_end[sp->plane_sample_offset].in_buffer_file_togo=sp->in_buffer_file_togo+sp->in_buffer_togo;
+       }
+       return(1);
+}
+
+static int
+OJPEGWriteHeaderInfo(TIFF* tif)
+{
+       static const char module[]="OJPEGWriteHeaderInfo";
+       OJPEGState* sp=(OJPEGState*)tif->tif_data;
+       uint8** m;
+       uint32 n;
+       assert(sp->libjpeg_session_active==0);
+       sp->out_state=ososSoi;
+       sp->restart_index=0;
+       jpeg_std_error(&(sp->libjpeg_jpeg_error_mgr));
+       sp->libjpeg_jpeg_error_mgr.output_message=OJPEGLibjpegJpegErrorMgrOutputMessage;
+       sp->libjpeg_jpeg_error_mgr.error_exit=OJPEGLibjpegJpegErrorMgrErrorExit;
+       sp->libjpeg_jpeg_decompress_struct.err=&(sp->libjpeg_jpeg_error_mgr);
+       sp->libjpeg_jpeg_decompress_struct.client_data=(void*)tif;
+       if (jpeg_create_decompress_encap(sp,&(sp->libjpeg_jpeg_decompress_struct))==0)
+               return(0);
+       sp->libjpeg_session_active=1;
+       sp->libjpeg_jpeg_source_mgr.bytes_in_buffer=0;
+       sp->libjpeg_jpeg_source_mgr.init_source=OJPEGLibjpegJpegSourceMgrInitSource;
+       sp->libjpeg_jpeg_source_mgr.fill_input_buffer=OJPEGLibjpegJpegSourceMgrFillInputBuffer;
+       sp->libjpeg_jpeg_source_mgr.skip_input_data=OJPEGLibjpegJpegSourceMgrSkipInputData;
+       sp->libjpeg_jpeg_source_mgr.resync_to_restart=OJPEGLibjpegJpegSourceMgrResyncToRestart;
+       sp->libjpeg_jpeg_source_mgr.term_source=OJPEGLibjpegJpegSourceMgrTermSource;
+       sp->libjpeg_jpeg_decompress_struct.src=&(sp->libjpeg_jpeg_source_mgr);
+       if (jpeg_read_header_encap(sp,&(sp->libjpeg_jpeg_decompress_struct),1)==0)
+               return(0);
+       if ((sp->subsampling_force_desubsampling_inside_decompression==0) && (sp->samples_per_pixel_per_plane>1))
+       {
+               sp->libjpeg_jpeg_decompress_struct.raw_data_out=1;
+#if JPEG_LIB_VERSION >= 70
+               sp->libjpeg_jpeg_decompress_struct.do_fancy_upsampling=FALSE;
+#endif
+               sp->libjpeg_jpeg_query_style=0;
+               if (sp->subsampling_convert_log==0)
+               {
+                       assert(sp->subsampling_convert_ycbcrbuf==0);
+                       assert(sp->subsampling_convert_ycbcrimage==0);
+                       sp->subsampling_convert_ylinelen=((sp->strile_width+sp->subsampling_hor*8-1)/(sp->subsampling_hor*8)*sp->subsampling_hor*8);
+                       sp->subsampling_convert_ylines=sp->subsampling_ver*8;
+                       sp->subsampling_convert_clinelen=sp->subsampling_convert_ylinelen/sp->subsampling_hor;
+                       sp->subsampling_convert_clines=8;
+                       sp->subsampling_convert_ybuflen=sp->subsampling_convert_ylinelen*sp->subsampling_convert_ylines;
+                       sp->subsampling_convert_cbuflen=sp->subsampling_convert_clinelen*sp->subsampling_convert_clines;
+                       sp->subsampling_convert_ycbcrbuflen=sp->subsampling_convert_ybuflen+2*sp->subsampling_convert_cbuflen;
+                       sp->subsampling_convert_ycbcrbuf=_TIFFmalloc(sp->subsampling_convert_ycbcrbuflen);
+                       if (sp->subsampling_convert_ycbcrbuf==0)
+                       {
+                               TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
+                               return(0);
+                       }
+                       sp->subsampling_convert_ybuf=sp->subsampling_convert_ycbcrbuf;
+                       sp->subsampling_convert_cbbuf=sp->subsampling_convert_ybuf+sp->subsampling_convert_ybuflen;
+                       sp->subsampling_convert_crbuf=sp->subsampling_convert_cbbuf+sp->subsampling_convert_cbuflen;
+                       sp->subsampling_convert_ycbcrimagelen=3+sp->subsampling_convert_ylines+2*sp->subsampling_convert_clines;
+                       sp->subsampling_convert_ycbcrimage=_TIFFmalloc(sp->subsampling_convert_ycbcrimagelen*sizeof(uint8*));
+                       if (sp->subsampling_convert_ycbcrimage==0)
+                       {
+                               TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
+                               return(0);
+                       }
+                       m=sp->subsampling_convert_ycbcrimage;
+                       *m++=(uint8*)(sp->subsampling_convert_ycbcrimage+3);
+                       *m++=(uint8*)(sp->subsampling_convert_ycbcrimage+3+sp->subsampling_convert_ylines);
+                       *m++=(uint8*)(sp->subsampling_convert_ycbcrimage+3+sp->subsampling_convert_ylines+sp->subsampling_convert_clines);
+                       for (n=0; n<sp->subsampling_convert_ylines; n++)
+                               *m++=sp->subsampling_convert_ybuf+n*sp->subsampling_convert_ylinelen;
+                       for (n=0; n<sp->subsampling_convert_clines; n++)
+                               *m++=sp->subsampling_convert_cbbuf+n*sp->subsampling_convert_clinelen;
+                       for (n=0; n<sp->subsampling_convert_clines; n++)
+                               *m++=sp->subsampling_convert_crbuf+n*sp->subsampling_convert_clinelen;
+                       sp->subsampling_convert_clinelenout=((sp->strile_width+sp->subsampling_hor-1)/sp->subsampling_hor);
+                       sp->subsampling_convert_state=0;
+                       sp->bytes_per_line=sp->subsampling_convert_clinelenout*(sp->subsampling_ver*sp->subsampling_hor+2);
+                       sp->lines_per_strile=((sp->strile_length+sp->subsampling_ver-1)/sp->subsampling_ver);
+                       sp->subsampling_convert_log=1;
+               }
+       }
+       else
+       {
+               sp->libjpeg_jpeg_decompress_struct.jpeg_color_space=JCS_UNKNOWN;
+               sp->libjpeg_jpeg_decompress_struct.out_color_space=JCS_UNKNOWN;
+               sp->libjpeg_jpeg_query_style=1;
+               sp->bytes_per_line=sp->samples_per_pixel_per_plane*sp->strile_width;
+               sp->lines_per_strile=sp->strile_length;
+       }
+       if (jpeg_start_decompress_encap(sp,&(sp->libjpeg_jpeg_decompress_struct))==0)
+               return(0);
+       sp->writeheader_done=1;
+       return(1);
+}
 
-/* The following subroutines comprise a JPEG Library "source" data manager by
-   by directing compressed data to the JPEG Library from a TIFF Library input
-   buffer.
-*/
 static void
-std_init_source(register j_decompress_ptr cinfo)
-  {
-#   define sp ((OJPEGState *)cinfo)
-    register TIFF *tif = sp->tif;
-
-    if (sp->src.bytes_in_buffer == 0)
-      {
-        sp->src.next_input_byte = (const JOCTET *)tif->tif_rawdata;
-        sp->src.bytes_in_buffer = (size_t)tif->tif_rawcc;
-      };
-#   undef sp
-  }
+OJPEGLibjpegSessionAbort(TIFF* tif)
+{
+       OJPEGState* sp=(OJPEGState*)tif->tif_data;
+       assert(sp->libjpeg_session_active!=0);
+       jpeg_destroy((jpeg_common_struct*)(&(sp->libjpeg_jpeg_decompress_struct)));
+       sp->libjpeg_session_active=0;
+}
 
-static boolean
-std_fill_input_buffer(register j_decompress_ptr cinfo)
-  { static const JOCTET dummy_EOI[2]={0xFF,JPEG_EOI};
-#   define sp ((OJPEGState *)cinfo)
+static int
+OJPEGReadHeaderInfoSec(TIFF* tif)
+{
+       static const char module[]="OJPEGReadHeaderInfoSec";
+       OJPEGState* sp=(OJPEGState*)tif->tif_data;
+       uint8 m;
+       uint16 n;
+       uint8 o;
+       if (sp->file_size==0)
+               sp->file_size=TIFFGetFileSize(tif);
+       if (sp->jpeg_interchange_format!=0)
+       {
+               if (sp->jpeg_interchange_format>=sp->file_size)
+               {
+                       sp->jpeg_interchange_format=0;
+                       sp->jpeg_interchange_format_length=0;
+               }
+               else
+               {
+                       if ((sp->jpeg_interchange_format_length==0) || (sp->jpeg_interchange_format+sp->jpeg_interchange_format_length>sp->file_size))
+                               sp->jpeg_interchange_format_length=sp->file_size-sp->jpeg_interchange_format;
+               }
+       }
+       sp->in_buffer_source=osibsNotSetYet;
+       sp->in_buffer_next_strile=0;
+       sp->in_buffer_strile_count=tif->tif_dir.td_nstrips;   
+       sp->in_buffer_file_togo=0;
+       sp->in_buffer_togo=0;
+       do
+       {
+               if (OJPEGReadBytePeek(sp,&m)==0)
+                       return(0);
+               if (m!=255)
+                       break;
+               OJPEGReadByteAdvance(sp);
+               do
+               {
+                       if (OJPEGReadByte(sp,&m)==0)
+                               return(0);
+               } while(m==255);
+               switch(m)
+               {
+                       case JPEG_MARKER_SOI:
+                               /* this type of marker has no data, and should be skipped */
+                               break;
+                       case JPEG_MARKER_COM:
+                       case JPEG_MARKER_APP0:
+                       case JPEG_MARKER_APP0+1:
+                       case JPEG_MARKER_APP0+2:
+                       case JPEG_MARKER_APP0+3:
+                       case JPEG_MARKER_APP0+4:
+                       case JPEG_MARKER_APP0+5:
+                       case JPEG_MARKER_APP0+6:
+                       case JPEG_MARKER_APP0+7:
+                       case JPEG_MARKER_APP0+8:
+                       case JPEG_MARKER_APP0+9:
+                       case JPEG_MARKER_APP0+10:
+                       case JPEG_MARKER_APP0+11:
+                       case JPEG_MARKER_APP0+12:
+                       case JPEG_MARKER_APP0+13:
+                       case JPEG_MARKER_APP0+14:
+                       case JPEG_MARKER_APP0+15:
+                               /* this type of marker has data, but it has no use to us (and no place here) and should be skipped */
+                               if (OJPEGReadWord(sp,&n)==0)
+                                       return(0);
+                               if (n<2)
+                               {
+                                       if (sp->subsamplingcorrect==0)
+                                               TIFFErrorExt(tif->tif_clientdata,module,"Corrupt JPEG data");
+                                       return(0);
+                               }
+                               if (n>2)
+                                       OJPEGReadSkip(sp,n-2);
+                               break;
+                       case JPEG_MARKER_DRI:
+                               if (OJPEGReadHeaderInfoSecStreamDri(tif)==0)
+                                       return(0);
+                               break;
+                       case JPEG_MARKER_DQT:
+                               if (OJPEGReadHeaderInfoSecStreamDqt(tif)==0)
+                                       return(0);
+                               break;
+                       case JPEG_MARKER_DHT:
+                               if (OJPEGReadHeaderInfoSecStreamDht(tif)==0)
+                                       return(0);
+                               break;
+                       case JPEG_MARKER_SOF0:
+                       case JPEG_MARKER_SOF1:
+                       case JPEG_MARKER_SOF3:
+                               if (OJPEGReadHeaderInfoSecStreamSof(tif,m)==0)
+                                       return(0);
+                               if (sp->subsamplingcorrect!=0)
+                                       return(1);
+                               break;
+                       case JPEG_MARKER_SOS:
+                               if (sp->subsamplingcorrect!=0)
+                                       return(1);
+                               assert(sp->plane_sample_offset==0);
+                               if (OJPEGReadHeaderInfoSecStreamSos(tif)==0)
+                                       return(0);
+                               break;
+                       default:
+                               TIFFErrorExt(tif->tif_clientdata,module,"Unknown marker type %d in JPEG data",m);
+                               return(0);
+               }
+       } while(m!=JPEG_MARKER_SOS);
+       if (sp->subsamplingcorrect)
+               return(1);
+       if (sp->sof_log==0)
+       {
+               if (OJPEGReadHeaderInfoSecTablesQTable(tif)==0)
+                       return(0);
+               sp->sof_marker_id=JPEG_MARKER_SOF0;
+               for (o=0; o<sp->samples_per_pixel; o++)
+                       sp->sof_c[o]=o;
+               sp->sof_hv[0]=((sp->subsampling_hor<<4)|sp->subsampling_ver);
+               for (o=1; o<sp->samples_per_pixel; o++)
+                       sp->sof_hv[o]=17;
+               sp->sof_x=sp->strile_width;
+               sp->sof_y=sp->strile_length_total;
+               sp->sof_log=1;
+               if (OJPEGReadHeaderInfoSecTablesDcTable(tif)==0)
+                       return(0);
+               if (OJPEGReadHeaderInfoSecTablesAcTable(tif)==0)
+                       return(0);
+               for (o=1; o<sp->samples_per_pixel; o++)
+                       sp->sos_cs[o]=o;
+       }
+       return(1);
+}
 
- /* Control should never get here, since an entire strip/tile is read into
-    memory before the decompressor is called; thus, data should have been
-    supplied by the "init_source" method.  ...But, sometimes things fail.
- */
-    WARNMS(cinfo,JWRN_JPEG_EOF);
-    sp->src.next_input_byte = dummy_EOI; /* Insert a fake EOI marker */
-    sp->src.bytes_in_buffer = sizeof dummy_EOI;
-    return TRUE;
-#   undef sp
-  }
+static int
+OJPEGReadHeaderInfoSecStreamDri(TIFF* tif)
+{
+       /* this could easilly cause trouble in some cases... but no such cases have occured sofar */
+       static const char module[]="OJPEGReadHeaderInfoSecStreamDri";
+       OJPEGState* sp=(OJPEGState*)tif->tif_data;
+       uint16 m;
+       if (OJPEGReadWord(sp,&m)==0)
+               return(0);
+       if (m!=4)
+       {
+               TIFFErrorExt(tif->tif_clientdata,module,"Corrupt DRI marker in JPEG data");
+               return(0);
+       }
+       if (OJPEGReadWord(sp,&m)==0)
+               return(0);
+       sp->restart_interval=m;
+       return(1);
+}
+
+static int
+OJPEGReadHeaderInfoSecStreamDqt(TIFF* tif)
+{
+       /* this is a table marker, and it is to be saved as a whole for exact pushing on the jpeg stream later on */
+       static const char module[]="OJPEGReadHeaderInfoSecStreamDqt";
+       OJPEGState* sp=(OJPEGState*)tif->tif_data;
+       uint16 m;
+       uint32 na;
+       uint8* nb;
+       uint8 o;
+       if (OJPEGReadWord(sp,&m)==0)
+               return(0);
+       if (m<=2)
+       {
+               if (sp->subsamplingcorrect==0)
+                       TIFFErrorExt(tif->tif_clientdata,module,"Corrupt DQT marker in JPEG data");
+               return(0);
+       }
+       if (sp->subsamplingcorrect!=0)
+               OJPEGReadSkip(sp,m-2);
+       else
+       {
+               m-=2;
+               do
+               {
+                       if (m<65)
+                       {
+                               TIFFErrorExt(tif->tif_clientdata,module,"Corrupt DQT marker in JPEG data");
+                               return(0);
+                       }
+                       na=sizeof(uint32)+69;
+                       nb=_TIFFmalloc(na);
+                       if (nb==0)
+                       {
+                               TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
+                               return(0);
+                       }
+                       *(uint32*)nb=na;
+                       nb[sizeof(uint32)]=255;
+                       nb[sizeof(uint32)+1]=JPEG_MARKER_DQT;
+                       nb[sizeof(uint32)+2]=0;
+                       nb[sizeof(uint32)+3]=67;
+                       if (OJPEGReadBlock(sp,65,&nb[sizeof(uint32)+4])==0)
+                               return(0);
+                       o=nb[sizeof(uint32)+4]&15;
+                       if (3<o)
+                       {
+                               TIFFErrorExt(tif->tif_clientdata,module,"Corrupt DQT marker in JPEG data");
+                               return(0);
+                       }
+                       if (sp->qtable[o]!=0)
+                               _TIFFfree(sp->qtable[o]);
+                       sp->qtable[o]=nb;
+                       m-=65;
+               } while(m>0);
+       }
+       return(1);
+}
+
+static int
+OJPEGReadHeaderInfoSecStreamDht(TIFF* tif)
+{
+       /* this is a table marker, and it is to be saved as a whole for exact pushing on the jpeg stream later on */
+       /* TODO: the following assumes there is only one table in this marker... but i'm not quite sure that assumption is guaranteed correct */
+       static const char module[]="OJPEGReadHeaderInfoSecStreamDht";
+       OJPEGState* sp=(OJPEGState*)tif->tif_data;
+       uint16 m;
+       uint32 na;
+       uint8* nb;
+       uint8 o;
+       if (OJPEGReadWord(sp,&m)==0)
+               return(0);
+       if (m<=2)
+       {
+               if (sp->subsamplingcorrect==0)
+                       TIFFErrorExt(tif->tif_clientdata,module,"Corrupt DHT marker in JPEG data");
+               return(0);
+       }
+       if (sp->subsamplingcorrect!=0)
+       {
+               OJPEGReadSkip(sp,m-2);
+       }
+       else
+       {
+               na=sizeof(uint32)+2+m;
+               nb=_TIFFmalloc(na);
+               if (nb==0)
+               {
+                       TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
+                       return(0);
+               }
+               *(uint32*)nb=na;
+               nb[sizeof(uint32)]=255;
+               nb[sizeof(uint32)+1]=JPEG_MARKER_DHT;
+               nb[sizeof(uint32)+2]=(m>>8);
+               nb[sizeof(uint32)+3]=(m&255);
+               if (OJPEGReadBlock(sp,m-2,&nb[sizeof(uint32)+4])==0)
+                       return(0);
+               o=nb[sizeof(uint32)+4];
+               if ((o&240)==0)
+               {
+                       if (3<o)
+                       {
+                               TIFFErrorExt(tif->tif_clientdata,module,"Corrupt DHT marker in JPEG data");
+                               return(0);
+                       }
+                       if (sp->dctable[o]!=0)
+                               _TIFFfree(sp->dctable[o]);
+                       sp->dctable[o]=nb;
+               }
+               else
+               {
+                       if ((o&240)!=16)
+                       {
+                               TIFFErrorExt(tif->tif_clientdata,module,"Corrupt DHT marker in JPEG data");
+                               return(0);
+                       }
+                       o&=15;
+                       if (3<o)
+                       {
+                               TIFFErrorExt(tif->tif_clientdata,module,"Corrupt DHT marker in JPEG data");
+                               return(0);
+                       }
+                       if (sp->actable[o]!=0)
+                               _TIFFfree(sp->actable[o]);
+                       sp->actable[o]=nb;
+               }
+       }
+       return(1);
+}
+
+static int
+OJPEGReadHeaderInfoSecStreamSof(TIFF* tif, uint8 marker_id)
+{
+       /* this marker needs to be checked, and part of its data needs to be saved for regeneration later on */
+       static const char module[]="OJPEGReadHeaderInfoSecStreamSof";
+       OJPEGState* sp=(OJPEGState*)tif->tif_data;
+       uint16 m;
+       uint16 n;
+       uint8 o;
+       uint16 p;
+       uint16 q;
+       if (sp->sof_log!=0)
+       {
+               TIFFErrorExt(tif->tif_clientdata,module,"Corrupt JPEG data");
+               return(0);
+       }
+       if (sp->subsamplingcorrect==0)
+               sp->sof_marker_id=marker_id;
+       /* Lf: data length */
+       if (OJPEGReadWord(sp,&m)==0)
+               return(0);
+       if (m<11)
+       {
+               if (sp->subsamplingcorrect==0)
+                       TIFFErrorExt(tif->tif_clientdata,module,"Corrupt SOF marker in JPEG data");
+               return(0);
+       }
+       m-=8;
+       if (m%3!=0)
+       {
+               if (sp->subsamplingcorrect==0)
+                       TIFFErrorExt(tif->tif_clientdata,module,"Corrupt SOF marker in JPEG data");
+               return(0);
+       }
+       n=m/3;
+       if (sp->subsamplingcorrect==0)
+       {
+               if (n!=sp->samples_per_pixel)
+               {
+                       TIFFErrorExt(tif->tif_clientdata,module,"JPEG compressed data indicates unexpected number of samples");
+                       return(0);
+               }
+       }
+       /* P: Sample precision */
+       if (OJPEGReadByte(sp,&o)==0)
+               return(0);
+       if (o!=8)
+       {
+               if (sp->subsamplingcorrect==0)
+                       TIFFErrorExt(tif->tif_clientdata,module,"JPEG compressed data indicates unexpected number of bits per sample");
+               return(0);
+       }
+       /* Y: Number of lines, X: Number of samples per line */
+       if (sp->subsamplingcorrect)
+               OJPEGReadSkip(sp,4);
+       else
+       {
+               /* TODO: probably best to also add check on allowed upper bound, especially x, may cause buffer overflow otherwise i think */
+               /* Y: Number of lines */
+               if (OJPEGReadWord(sp,&p)==0)
+                       return(0);
+               if ((p<sp->image_length) && (p<sp->strile_length_total))
+               {
+                       TIFFErrorExt(tif->tif_clientdata,module,"JPEG compressed data indicates unexpected height");
+                       return(0);
+               }
+               sp->sof_y=p;
+               /* X: Number of samples per line */
+               if (OJPEGReadWord(sp,&p)==0)
+                       return(0);
+               if ((p<sp->image_width) && (p<sp->strile_width))
+               {
+                       TIFFErrorExt(tif->tif_clientdata,module,"JPEG compressed data indicates unexpected width");
+                       return(0);
+               }
+               sp->sof_x=p;
+       }
+       /* Nf: Number of image components in frame */
+       if (OJPEGReadByte(sp,&o)==0)
+               return(0);
+       if (o!=n)
+       {
+               if (sp->subsamplingcorrect==0)
+                       TIFFErrorExt(tif->tif_clientdata,module,"Corrupt SOF marker in JPEG data");
+               return(0);
+       }
+       /* per component stuff */
+       /* TODO: double-check that flow implies that n cannot be as big as to make us overflow sof_c, sof_hv and sof_tq arrays */
+       for (q=0; q<n; q++)
+       {
+               /* C: Component identifier */
+               if (OJPEGReadByte(sp,&o)==0)
+                       return(0);
+               if (sp->subsamplingcorrect==0)
+                       sp->sof_c[q]=o;
+               /* H: Horizontal sampling factor, and V: Vertical sampling factor */
+               if (OJPEGReadByte(sp,&o)==0)
+                       return(0);
+               if (sp->subsamplingcorrect!=0)
+               {
+                       if (q==0)
+                       {
+                               sp->subsampling_hor=(o>>4);
+                               sp->subsampling_ver=(o&15);
+                               if (((sp->subsampling_hor!=1) && (sp->subsampling_hor!=2) && (sp->subsampling_hor!=4)) ||
+                                       ((sp->subsampling_ver!=1) && (sp->subsampling_ver!=2) && (sp->subsampling_ver!=4)))
+                                       sp->subsampling_force_desubsampling_inside_decompression=1;
+                       }
+                       else
+                       {
+                               if (o!=17)
+                                       sp->subsampling_force_desubsampling_inside_decompression=1;
+                       }
+               }
+               else
+               {
+                       sp->sof_hv[q]=o;
+                       if (sp->subsampling_force_desubsampling_inside_decompression==0)
+                       {
+                               if (q==0)
+                               {
+                                       if (o!=((sp->subsampling_hor<<4)|sp->subsampling_ver))
+                                       {
+                                               TIFFErrorExt(tif->tif_clientdata,module,"JPEG compressed data indicates unexpected subsampling values");
+                                               return(0);
+                                       }
+                               }
+                               else
+                               {
+                                       if (o!=17)
+                                       {
+                                               TIFFErrorExt(tif->tif_clientdata,module,"JPEG compressed data indicates unexpected subsampling values");
+                                               return(0);
+                                       }
+                               }
+                       }
+               }
+               /* Tq: Quantization table destination selector */
+               if (OJPEGReadByte(sp,&o)==0)
+                       return(0);
+               if (sp->subsamplingcorrect==0)
+                       sp->sof_tq[q]=o;
+       }
+       if (sp->subsamplingcorrect==0)
+               sp->sof_log=1;
+       return(1);
+}
+
+static int
+OJPEGReadHeaderInfoSecStreamSos(TIFF* tif)
+{
+       /* this marker needs to be checked, and part of its data needs to be saved for regeneration later on */
+       static const char module[]="OJPEGReadHeaderInfoSecStreamSos";
+       OJPEGState* sp=(OJPEGState*)tif->tif_data;
+       uint16 m;
+       uint8 n;
+       uint8 o;
+       assert(sp->subsamplingcorrect==0);
+       if (sp->sof_log==0)
+       {
+               TIFFErrorExt(tif->tif_clientdata,module,"Corrupt SOS marker in JPEG data");
+               return(0);
+       }
+       /* Ls */
+       if (OJPEGReadWord(sp,&m)==0)
+               return(0);
+       if (m!=6+sp->samples_per_pixel_per_plane*2)
+       {
+               TIFFErrorExt(tif->tif_clientdata,module,"Corrupt SOS marker in JPEG data");
+               return(0);
+       }
+       /* Ns */
+       if (OJPEGReadByte(sp,&n)==0)
+               return(0);
+       if (n!=sp->samples_per_pixel_per_plane)
+       {
+               TIFFErrorExt(tif->tif_clientdata,module,"Corrupt SOS marker in JPEG data");
+               return(0);
+       }
+       /* Cs, Td, and Ta */
+       for (o=0; o<sp->samples_per_pixel_per_plane; o++)
+       {
+               /* Cs */
+               if (OJPEGReadByte(sp,&n)==0)
+                       return(0);
+               sp->sos_cs[sp->plane_sample_offset+o]=n;
+               /* Td and Ta */
+               if (OJPEGReadByte(sp,&n)==0)
+                       return(0);
+               sp->sos_tda[sp->plane_sample_offset+o]=n;
+       }
+       /* skip Ss, Se, Ah, en Al -> no check, as per Tom Lane recommendation, as per LibJpeg source */
+       OJPEGReadSkip(sp,3);
+       return(1);
+}
+
+static int
+OJPEGReadHeaderInfoSecTablesQTable(TIFF* tif)
+{
+       static const char module[]="OJPEGReadHeaderInfoSecTablesQTable";
+       OJPEGState* sp=(OJPEGState*)tif->tif_data;
+       uint8 m;
+       uint8 n;
+       uint32 oa;
+       uint8* ob;
+       uint32 p;
+       if (sp->qtable_offset[0]==0)
+       {
+               TIFFErrorExt(tif->tif_clientdata,module,"Missing JPEG tables");
+               return(0);
+       }
+       sp->in_buffer_file_pos_log=0;
+       for (m=0; m<sp->samples_per_pixel; m++)
+       {
+               if ((sp->qtable_offset[m]!=0) && ((m==0) || (sp->qtable_offset[m]!=sp->qtable_offset[m-1])))
+               {
+                       for (n=0; n<m-1; n++)
+                       {
+                               if (sp->qtable_offset[m]==sp->qtable_offset[n])
+                               {
+                                       TIFFErrorExt(tif->tif_clientdata,module,"Corrupt JpegQTables tag value");
+                                       return(0);
+                               }
+                       }
+                       oa=sizeof(uint32)+69;
+                       ob=_TIFFmalloc(oa);
+                       if (ob==0)
+                       {
+                               TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
+                               return(0);
+                       }
+                       *(uint32*)ob=oa;
+                       ob[sizeof(uint32)]=255;
+                       ob[sizeof(uint32)+1]=JPEG_MARKER_DQT;
+                       ob[sizeof(uint32)+2]=0;
+                       ob[sizeof(uint32)+3]=67;
+                       ob[sizeof(uint32)+4]=m;
+                       TIFFSeekFile(tif,sp->qtable_offset[m],SEEK_SET);
+                       p=TIFFReadFile(tif,&ob[sizeof(uint32)+5],64);
+                       if (p!=64)
+                               return(0);
+                       sp->qtable[m]=ob;
+                       sp->sof_tq[m]=m;
+               }
+               else
+                       sp->sof_tq[m]=sp->sof_tq[m-1];
+       }
+       return(1);
+}
+
+static int
+OJPEGReadHeaderInfoSecTablesDcTable(TIFF* tif)
+{
+       static const char module[]="OJPEGReadHeaderInfoSecTablesDcTable";
+       OJPEGState* sp=(OJPEGState*)tif->tif_data;
+       uint8 m;
+       uint8 n;
+       uint8 o[16];
+       uint32 p;
+       uint32 q;
+       uint32 ra;
+       uint8* rb;
+       if (sp->dctable_offset[0]==0)
+       {
+               TIFFErrorExt(tif->tif_clientdata,module,"Missing JPEG tables");
+               return(0);
+       }
+       sp->in_buffer_file_pos_log=0;
+       for (m=0; m<sp->samples_per_pixel; m++)
+       {
+               if ((sp->dctable_offset[m]!=0) && ((m==0) || (sp->dctable_offset[m]!=sp->dctable_offset[m-1])))
+               {
+                       for (n=0; n<m-1; n++)
+                       {
+                               if (sp->dctable_offset[m]==sp->dctable_offset[n])
+                               {
+                                       TIFFErrorExt(tif->tif_clientdata,module,"Corrupt JpegDcTables tag value");
+                                       return(0);
+                               }
+                       }
+                       TIFFSeekFile(tif,sp->dctable_offset[m],SEEK_SET);
+                       p=TIFFReadFile(tif,o,16);
+                       if (p!=16)
+                               return(0);
+                       q=0;
+                       for (n=0; n<16; n++)
+                               q+=o[n];
+                       ra=sizeof(uint32)+21+q;
+                       rb=_TIFFmalloc(ra);
+                       if (rb==0)
+                       {
+                               TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
+                               return(0);
+                       }
+                       *(uint32*)rb=ra;
+                       rb[sizeof(uint32)]=255;
+                       rb[sizeof(uint32)+1]=JPEG_MARKER_DHT;
+                       rb[sizeof(uint32)+2]=((19+q)>>8);
+                       rb[sizeof(uint32)+3]=((19+q)&255);
+                       rb[sizeof(uint32)+4]=m;
+                       for (n=0; n<16; n++)
+                               rb[sizeof(uint32)+5+n]=o[n];
+                       p=TIFFReadFile(tif,&(rb[sizeof(uint32)+21]),q);
+                       if (p!=q)
+                               return(0);
+                       sp->dctable[m]=rb;
+                       sp->sos_tda[m]=(m<<4);
+               }
+               else
+                       sp->sos_tda[m]=sp->sos_tda[m-1];
+       }
+       return(1);
+}
+
+static int
+OJPEGReadHeaderInfoSecTablesAcTable(TIFF* tif)
+{
+       static const char module[]="OJPEGReadHeaderInfoSecTablesAcTable";
+       OJPEGState* sp=(OJPEGState*)tif->tif_data;
+       uint8 m;
+       uint8 n;
+       uint8 o[16];
+       uint32 p;
+       uint32 q;
+       uint32 ra;
+       uint8* rb;
+       if (sp->actable_offset[0]==0)
+       {
+               TIFFErrorExt(tif->tif_clientdata,module,"Missing JPEG tables");
+               return(0);
+       }
+       sp->in_buffer_file_pos_log=0;
+       for (m=0; m<sp->samples_per_pixel; m++)
+       {
+               if ((sp->actable_offset[m]!=0) && ((m==0) || (sp->actable_offset[m]!=sp->actable_offset[m-1])))
+               {
+                       for (n=0; n<m-1; n++)
+                       {
+                               if (sp->actable_offset[m]==sp->actable_offset[n])
+                               {
+                                       TIFFErrorExt(tif->tif_clientdata,module,"Corrupt JpegAcTables tag value");
+                                       return(0);
+                               }
+                       }
+                       TIFFSeekFile(tif,sp->actable_offset[m],SEEK_SET);
+                       p=TIFFReadFile(tif,o,16);
+                       if (p!=16)
+                               return(0);
+                       q=0;
+                       for (n=0; n<16; n++)
+                               q+=o[n];
+                       ra=sizeof(uint32)+21+q;
+                       rb=_TIFFmalloc(ra);
+                       if (rb==0)
+                       {
+                               TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
+                               return(0);
+                       }
+                       *(uint32*)rb=ra;
+                       rb[sizeof(uint32)]=255;
+                       rb[sizeof(uint32)+1]=JPEG_MARKER_DHT;
+                       rb[sizeof(uint32)+2]=((19+q)>>8);
+                       rb[sizeof(uint32)+3]=((19+q)&255);
+                       rb[sizeof(uint32)+4]=(16|m);
+                       for (n=0; n<16; n++)
+                               rb[sizeof(uint32)+5+n]=o[n];
+                       p=TIFFReadFile(tif,&(rb[sizeof(uint32)+21]),q);
+                       if (p!=q)
+                               return(0);
+                       sp->actable[m]=rb;
+                       sp->sos_tda[m]=(sp->sos_tda[m]|m);
+               }
+               else
+                       sp->sos_tda[m]=(sp->sos_tda[m]|(sp->sos_tda[m-1]&15));
+       }
+       return(1);
+}
+
+static int
+OJPEGReadBufferFill(OJPEGState* sp)
+{
+       uint16 m;
+       tsize_t n;
+       /* TODO: double-check: when subsamplingcorrect is set, no call to TIFFErrorExt or TIFFWarningExt should be made
+        * in any other case, seek or read errors should be passed through */
+       do
+       {
+               if (sp->in_buffer_file_togo!=0)
+               {
+                       if (sp->in_buffer_file_pos_log==0)
+                       {
+                               TIFFSeekFile(sp->tif,sp->in_buffer_file_pos,SEEK_SET);
+                               sp->in_buffer_file_pos_log=1;
+                       }
+                       m=OJPEG_BUFFER;
+                       if (m>sp->in_buffer_file_togo)
+                               m=(uint16)sp->in_buffer_file_togo;
+                       n=TIFFReadFile(sp->tif,sp->in_buffer,(tsize_t)m);
+                       if (n==0)
+                               return(0);
+                       assert(n>0);
+                       assert(n<=OJPEG_BUFFER);
+                       assert(n<65536);
+                       assert((uint16)n<=sp->in_buffer_file_togo);
+                       m=(uint16)n;
+                       sp->in_buffer_togo=m;
+                       sp->in_buffer_cur=sp->in_buffer;
+                       sp->in_buffer_file_togo-=m;
+                       sp->in_buffer_file_pos+=m;
+                       break;
+               }
+               sp->in_buffer_file_pos_log=0;
+               switch(sp->in_buffer_source)
+               {
+                       case osibsNotSetYet:
+                               if (sp->jpeg_interchange_format!=0)
+                               {
+                                       sp->in_buffer_file_pos=sp->jpeg_interchange_format;
+                                       sp->in_buffer_file_togo=sp->jpeg_interchange_format_length;
+                               }
+                               sp->in_buffer_source=osibsJpegInterchangeFormat;
+                               break;
+                       case osibsJpegInterchangeFormat:
+                               sp->in_buffer_source=osibsStrile;
+                       case osibsStrile:
+                               if (sp->in_buffer_next_strile==sp->in_buffer_strile_count)  
+                                       sp->in_buffer_source=osibsEof;
+                               else
+                               {
+                                       if (sp->tif->tif_dir.td_stripoffset == 0) {
+                                               TIFFErrorExt(sp->tif->tif_clientdata,sp->tif->tif_name,"Strip offsets are missing");
+                                               return(0);
+                                       }
+                                       sp->in_buffer_file_pos=sp->tif->tif_dir.td_stripoffset[sp->in_buffer_next_strile];  
+                                       if (sp->in_buffer_file_pos!=0)
+                                       {
+                                               if (sp->in_buffer_file_pos>=sp->file_size)
+                                                       sp->in_buffer_file_pos=0;
+                                               else
+                                               {
+                                                       sp->in_buffer_file_togo=sp->tif->tif_dir.td_stripbytecount[sp->in_buffer_next_strile];  
+                                                       if (sp->in_buffer_file_togo==0)
+                                                               sp->in_buffer_file_pos=0;
+                                                       else if (sp->in_buffer_file_pos+sp->in_buffer_file_togo>sp->file_size)
+                                                               sp->in_buffer_file_togo=sp->file_size-sp->in_buffer_file_pos;
+                                               }
+                                       }
+                                       sp->in_buffer_next_strile++;
+                               }
+                               break;
+                       default:
+                               return(0);
+               }
+       } while (1);
+       return(1);
+}
+
+static int
+OJPEGReadByte(OJPEGState* sp, uint8* byte)
+{
+       if (sp->in_buffer_togo==0)
+       {
+               if (OJPEGReadBufferFill(sp)==0)
+                       return(0);
+               assert(sp->in_buffer_togo>0);
+       }
+       *byte=*(sp->in_buffer_cur);
+       sp->in_buffer_cur++;
+       sp->in_buffer_togo--;
+       return(1);
+}
+
+static int
+OJPEGReadBytePeek(OJPEGState* sp, uint8* byte)
+{
+       if (sp->in_buffer_togo==0)
+       {
+               if (OJPEGReadBufferFill(sp)==0)
+                       return(0);
+               assert(sp->in_buffer_togo>0);
+       }
+       *byte=*(sp->in_buffer_cur);
+       return(1);
+}
 
 static void
-std_skip_input_data(register j_decompress_ptr cinfo, long num_bytes)
-  {
-#   define sp ((OJPEGState *)cinfo)
-
-    if (num_bytes > 0)
-    {
-      if (num_bytes > (long)sp->src.bytes_in_buffer) /* oops: buffer overrun */
-        (void)std_fill_input_buffer(cinfo);
-      else
-        {
-          sp->src.next_input_byte += (size_t)num_bytes;
-          sp->src.bytes_in_buffer -= (size_t)num_bytes;
-        }
-    }
-#   undef sp
-  }
-
-/*ARGSUSED*/ static void
-std_term_source(register j_decompress_ptr cinfo){} /* "Dummy" stub */
-
-/* Allocate temporary I/O buffers for downsampled data, using values computed in
-   "jpeg_start_{de}compress()".  We use the JPEG Library's allocator so that
-   buffers will be released automatically when done with a strip/tile.  This is
-   also a handy place to compute samplesperclump, bytesperline, etc.
-*/
+OJPEGReadByteAdvance(OJPEGState* sp)
+{
+       assert(sp->in_buffer_togo>0);
+       sp->in_buffer_cur++;
+       sp->in_buffer_togo--;
+}
+
 static int
-alloc_downsampled_buffers(TIFF *tif,jpeg_component_info *comp_info,
-                          int num_components)
-  { register OJPEGState *sp = OJState(tif);
-
-    sp->samplesperclump = 0;
-    if (num_components > 0)
-      { tsize_t size = sp->cinfo.comm.is_decompressor
-#                    ifdef D_LOSSLESS_SUPPORTED
-                     ? sp->cinfo.d.min_codec_data_unit
-#                    else
-                     ? DCTSIZE
-#                    endif
-#                    ifdef C_LOSSLESS_SUPPORTED
-                     : sp->cinfo.c.data_unit;
-#                    else
-                     : DCTSIZE;
-#                    endif
-        int ci = 0;
-        register jpeg_component_info *compptr = comp_info;
-
-        do
-          { JSAMPARRAY buf;
-
-            sp->samplesperclump +=
-              compptr->h_samp_factor * compptr->v_samp_factor;
-#           if defined(C_LOSSLESS_SUPPORTED) || defined(D_LOSSLESS_SUPPORTED)
-            if (!(buf = CALLJPEG(sp,0,(*sp->cinfo.comm.mem->alloc_sarray)(&sp->cinfo.comm,JPOOL_IMAGE,compptr->width_in_data_units*size,compptr->v_samp_factor*size))))
-#           else
-            if (!(buf = CALLJPEG(sp,0,(*sp->cinfo.comm.mem->alloc_sarray)(&sp->cinfo.comm,JPOOL_IMAGE,compptr->width_in_blocks*size,compptr->v_samp_factor*size))))
-#           endif
-              return 0;
-            sp->ds_buffer[ci] = buf;
-          }
-        while (++compptr,++ci < num_components);
-      };
-    return 1;
-  }
-#ifdef never
-
-/* JPEG Encoding begins here. */
-
-/*ARGSUSED*/ static int
-OJPEGEncode(register TIFF *tif,tidata_t buf,tsize_t cc,tsample_t s)
-  { tsize_t rows;                          /* No. of unprocessed rows in file */
-    register OJPEGState *sp = OJState(tif);
-
- /* Encode a chunk of pixels, where returned data is NOT down-sampled (the
-    standard case).  The data is expected to be written in scan-line multiples.
- */
-    if (cc % sp->bytesperline) TIFFWarning(tif->tif_name,no_write_frac);
-    if ( (cc /= bytesperline)      /* No. of complete rows in caller's buffer */
-       > (rows = sp->cinfo.c.image_height - sp->cinfo.c.next_scanline)
-       ) cc = rows;
-    while (--cc >= 0)
-      {
-        if (   CALLJPEG(sp,-1,jpeg_write_scanlines(&sp->cinfo.c,(JSAMPARRAY)&buf,1))
-            != 1
-           ) return 0;
-        ++tif->tif_row;
-        buf += sp->bytesperline;
-      };
-    return 1;
-  }
-
-/*ARGSUSED*/ static int
-OJPEGEncodeRaw(register TIFF *tif,tidata_t buf,tsize_t cc,tsample_t s)
-  { tsize_t rows;                          /* No. of unprocessed rows in file */
-    JDIMENSION lines_per_MCU, size;
-    register OJPEGState *sp = OJState(tif);
-
- /* Encode a chunk of pixels, where returned data is down-sampled as per the
-    sampling factors.  The data is expected to be written in scan-line
-    multiples.
- */
-    cc /= sp->bytesperline;
-    if (cc % sp->bytesperline) TIFFWarning(tif->tif_name,no_write_frac);
-    if ( (cc /= bytesperline)      /* No. of complete rows in caller's buffer */
-       > (rows = sp->cinfo.c.image_height - sp->cinfo.c.next_scanline)
-       ) cc = rows;
-#   ifdef C_LOSSLESS_SUPPORTED
-    lines_per_MCU = sp->cinfo.c.max_samp_factor*(size = sp->cinfo.d.data_unit);
-#   else
-    lines_per_MCU = sp->cinfo.c.max_samp_factor*(size = DCTSIZE);
-#   endif
-    while (--cc >= 0)
-      { int ci = 0, clumpoffset = 0;
-        register jpeg_component_info *compptr = sp->cinfo.c.comp_info;
-
-     /* The fastest way to separate the data is to make 1 pass over the scan
-        line for each row of each component.
-     */
-        do
-          { int ypos = 0;
-
-            do
-              { int padding;
-                register JSAMPLE *inptr = (JSAMPLE*)buf + clumpoffset,
-                                 *outptr =
-                  sp->ds_buffer[ci][sp->scancount*compptr->v_samp_factor+ypos];
-             /* Cb,Cr both have sampling factors 1, so this is correct */
-                register int clumps_per_line =
-                  sp->cinfo.c.comp_info[1].downsampled_width,
-                             xpos;
-
-                padding = (int)
-#                         ifdef C_LOSSLESS_SUPPORTED
-                          ( compptr->width_in_data_units * size
-#                         else
-                          ( compptr->width_in_blocks * size
-#                         endif
-                          - clumps_per_line * compptr->h_samp_factor
-                          );
-                if (compptr->h_samp_factor == 1) /* Cb & Cr fast path */
-                  do *outptr++ = *inptr;
-                  while ((inptr += sp->samplesperclump),--clumps_per_line > 0);
-                else /* general case */
-                  do
-                    {
-                      xpos = 0;
-                      do *outptr++ = inptr[xpos];
-                      while (++xpos < compptr->h_samp_factor);
-                    }
-                  while ((inptr += sp->samplesperclump),--clumps_per_line > 0);
-                xpos = 0; /* Pad each scan line as needed */
-                do outptr[0] = outptr[-1]; while (++outptr,++xpos < padding);
-                clumpoffset += compptr->h_samp_factor;
-              }
-            while (++ypos < compptr->v_samp_factor);
-          }
-        while (++compptr,++ci < sp->cinfo.c.num_components);
-        if (++sp->scancount >= size)
-          {
-            if (   CALLJPEG(sp,-1,jpeg_write_raw_data(&sp->cinfo.c,sp->ds_buffer,lines_per_MCU))
-                != lines_per_MCU
-               ) return 0;
-            sp->scancount = 0;
-          };
-        ++tif->tif_row++
-        buf += sp->bytesperline;
-      };
-    return 1;
-  }
+OJPEGReadWord(OJPEGState* sp, uint16* word)
+{
+       uint8 m;
+       if (OJPEGReadByte(sp,&m)==0)
+               return(0);
+       *word=(m<<8);
+       if (OJPEGReadByte(sp,&m)==0)
+               return(0);
+       *word|=m;
+       return(1);
+}
 
 static int
-OJPEGSetupEncode(register TIFF *tif)
-  { static const char module[]={"OJPEGSetupEncode"};
-    uint32 segment_height, segment_width;
-    int status = 1;                              /* Assume success by default */
-    register OJPEGState *sp = OJState(tif);
-#   define td (&tif->tif_dir)
-
- /* Verify miscellaneous parameters.  This will need work if the TIFF Library
-    ever supports different depths for different components, or if the JPEG
-    Library ever supports run-time depth selection.  Neither seems imminent.
- */
-    if (td->td_bitspersample != 8)
-      {
-        TIFFError(module,bad_bps,td->td_bitspersample);
-        status = 0;
-      };
-
- /* The TIFF Version 6.0 specification and IJG JPEG Library accept different
-    sets of color spaces, so verify that our image belongs to the common subset
-    and map its photometry code, then initialize to handle subsampling and
-    optional JPEG Library YCbCr <-> RGB color-space conversion.
- */
-    switch (td->td_photometric)
-      {
-        case PHOTOMETRIC_YCBCR     :
-
-       /* ISO IS 10918-1 requires that JPEG subsampling factors be 1-4, but
-          TIFF Version 6.0 is more restrictive: only 1, 2, and 4 are allowed.
-       */
-          if (   (   td->td_ycbcrsubsampling[0] == 1
-                  || td->td_ycbcrsubsampling[0] == 2
-                  || td->td_ycbcrsubsampling[0] == 4
-                 )
-              && (   td->td_ycbcrsubsampling[1] == 1
-                  || td->td_ycbcrsubsampling[1] == 2
-                  || td->td_ycbcrsubsampling[1] == 4
-                 )
-             )
-            sp->cinfo.c.raw_data_in =
-              ( (sp->h_sampling = td->td_ycbcrsubsampling[0]) << 3
-              | (sp->v_sampling = td->td_ycbcrsubsampling[1])
-              ) != 011;
-          else
-            {
-              TIFFError(module,bad_subsampling);
-              status = 0;
-            };
-
-       /* A ReferenceBlackWhite field MUST be present, since the default value
-          is inapproriate for YCbCr.  Fill in the proper value if the
-          application didn't set it.
-       */
-          if (!TIFFFieldSet(tif,FIELD_REFBLACKWHITE))
-            { float refbw[6];
-              long top = 1L << td->td_bitspersample;
-              refbw[0] = 0;
-              refbw[1] = (float)(top-1L);
-              refbw[2] = (float)(top>>1);
-              refbw[3] = refbw[1];
-              refbw[4] = refbw[2];
-              refbw[5] = refbw[1];
-              TIFFSetField(tif,TIFFTAG_REFERENCEBLACKWHITE,refbw);
-            };
-          sp->cinfo.c.jpeg_color_space = JCS_YCbCr;
-          if (sp->jpegcolormode == JPEGCOLORMODE_RGB)
-            {
-              sp->cinfo.c.raw_data_in = FALSE;
-              sp->in_color_space = JCS_RGB;
-              break;
-            };
-          goto L2;
-        case PHOTOMETRIC_MINISBLACK:
-          sp->cinfo.c.jpeg_color_space = JCS_GRAYSCALE;
-          goto L1;
-        case PHOTOMETRIC_RGB       :
-          sp->cinfo.c.jpeg_color_space = JCS_RGB;
-          goto L1;
-        case PHOTOMETRIC_SEPARATED :
-          sp->cinfo.c.jpeg_color_space = JCS_CMYK;
-      L1: sp->jpegcolormode = JPEGCOLORMODE_RAW; /* No JPEG Lib. conversion */
-      L2: sp->cinfo.d.in_color_space = sp->cinfo.d.jpeg_color-space;
-          break;
-        default                    :
-          TIFFError(module,bad_photometry,td->td_photometric);
-          status = 0;
-      };
-    tif->tif_encoderow = tif->tif_encodestrip = tif->tif_encodetile =
-      sp->cinfo.c.raw_data_in ? OJPEGEncodeRaw : OJPEGEncode;
-    if (isTiled(tif))
-      { tsize_t size;
-
-#       ifdef C_LOSSLESS_SUPPORTED
-        if ((size = sp->v_sampling*sp->cinfo.c.data_unit) < 16) size = 16;
-#       else
-        if ((size = sp->v_sampling*DCTSIZE) < 16) size = 16;
-#       endif
-        if ((segment_height = td->td_tilelength) % size)
-          {
-            TIFFError(module,"JPEG tile height must be multiple of %d",size);
-            status = 0;
-          };
-#       ifdef C_LOSSLESS_SUPPORTED
-        if ((size = sp->h_sampling*sp->cinfo.c.data_unit) < 16) size = 16;
-#       else
-        if ((size = sp->h_sampling*DCTSIZE) < 16) size = 16;
-#       endif
-        if ((segment_width = td->td_tilewidth) % size)
-          {
-            TIFFError(module,"JPEG tile width must be multiple of %d",size);
-            status = 0;
-          };
-        sp->bytesperline = TIFFTileRowSize(tif);
-      }
-    else
-      { tsize_t size;
-
-#       ifdef C_LOSSLESS_SUPPORTED
-        if ((size = sp->v_sampling*sp->cinfo.c.data_unit) < 16) size = 16;
-#       else
-        if ((size = sp->v_sampling*DCTSIZE) < 16) size = 16;
-#       endif
-        if (td->td_rowsperstrip < (segment_height = td->td_imagelength))
-          {
-            if (td->td_rowsperstrip % size)
-              {
-                TIFFError(module,"JPEG RowsPerStrip must be multiple of %d",size);
-                status = 0;
-              };
-            segment_height = td->td_rowsperstrip;
-          };
-        segment_width = td->td_imagewidth;
-        sp->bytesperline = tif->tif_scanlinesize;
-      };
-    if (segment_width > 65535 || segment_height > 65535)
-      {
-        TIFFError(module,"Strip/tile too large for JPEG");
-        status = 0;
-      };
-
- /* Initialize all JPEG parameters to default values.  Note that the JPEG
-    Library's "jpeg_set_defaults()" method needs legal values for the
-    "in_color_space" and "input_components" fields.
- */
-    sp->cinfo.c.input_components = 1; /* Default for JCS_UNKNOWN */
-    if (!CALLVJPEG(sp,jpeg_set_defaults(&sp->cinfo.c))) status = 0;
-    switch (sp->jpegtablesmode & (JPEGTABLESMODE_HUFF|JPEGTABLESMODE_QUANT))
-      { register JHUFF_TBL *htbl;
-        register JQUANT_TBL *qtbl;
-
-        case 0                                       :
-          sp->cinfo.c.optimize_coding = TRUE;
-        case JPEGTABLESMODE_HUFF                     :
-          if (!CALLVJPEG(sp,jpeg_set_quality(&sp->cinfo.c,sp->jpegquality,FALSE)))
-            return 0;
-          if (qtbl = sp->cinfo.c.quant_tbl_ptrs[0]) qtbl->sent_table = FALSE;
-          if (qtbl = sp->cinfo.c.quant_tbl_ptrs[1]) qtbl->sent_table = FALSE;
-          goto L3;
-        case JPEGTABLESMODE_QUANT                    :
-          sp->cinfo.c.optimize_coding = TRUE;
-
-       /* We do not support application-supplied JPEG tables, so mark the field
-          "not present".
-       */
-      L3: TIFFClrFieldBit(tif,FIELD_JPEGTABLES);
-          break;
-        case JPEGTABLESMODE_HUFF|JPEGTABLESMODE_QUANT:
-          if (   !CALLVJPEG(sp,jpeg_set_quality(&sp->cinfo.c,sp->jpegquality,FALSE))
-              || !CALLVJPEG(sp,jpeg_suppress_tables(&sp->cinfo.c,TRUE))
-             )
-            {
-              status = 0;
-              break;
-            };
-          if (qtbl = sp->cinfo.c.quant_tbl_ptrs[0]) qtbl->sent_table = FALSE;
-          if (htbl = sp->cinfo.c.dc_huff_tbl_ptrs[0]) htbl->sent_table = FALSE;
-          if (htbl = sp->cinfo.c.ac_huff_tbl_ptrs[0]) htbl->sent_table = FALSE;
-          if (sp->cinfo.c.jpeg_color_space == JCS_YCbCr)
-            {
-              if (qtbl = sp->cinfo.c.quant_tbl_ptrs[1])
-                qtbl->sent_table = FALSE;
-              if (htbl = sp->cinfo.c.dc_huff_tbl_ptrs[1])
-                htbl->sent_table = FALSE;
-              if (htbl = sp->cinfo.c.ac_huff_tbl_ptrs[1])
-                htbl->sent_table = FALSE;
-            };
-          if (   TIFFojpeg_tables_dest(sp,tif)
-              && CALLVJPEG(sp,jpeg_write_tables(&sp->cinfo.c))
-             )
-            {
-    
-           /* Mark the field "present".  We can't use "TIFFSetField()" because
-              "BEENWRITING" is already set!
-           */
-              TIFFSetFieldBit(tif,FIELD_JPEGTABLES);
-              tif->tif_flags |= TIFF_DIRTYDIRECT;
-            }
-          else status = 0;
-      };
-    if (   sp->cinfo.c.raw_data_in
-        && !alloc_downsampled_buffers(tif,sp->cinfo.c.comp_info,
-                                      sp->cinfo.c.num_components)
-       ) status = 0;
-    if (status == 0) return 0; /* If TIFF errors, don't bother to continue */
- /* Grab parameters that are same for all strips/tiles. */
-
-    sp->dest.init_destination = std_init_destination;
-    sp->dest.empty_output_buffer = std_empty_output_buffer;
-    sp->dest.term_destination = std_term_destination;
-    sp->cinfo.c.dest = &sp->dest;
-    sp->cinfo.c.data_precision = td->td_bitspersample;
-    sp->cinfo.c.write_JFIF_header = /* Don't write extraneous markers */
-    sp->cinfo.c.write_Adobe_marker = FALSE;
-    sp->cinfo.c.image_width = segment_width;
-    sp->cinfo.c.image_height = segment_height;
-    sp->cinfo.c.comp_info[0].h_samp_factor =
-    sp->cinfo.c.comp_info[0].v_samp_factor = 1;
-    return CALLVJPEG(sp,jpeg_start_compress(&sp->cinfo.c,FALSE));
-#   undef td
-  }
+OJPEGReadBlock(OJPEGState* sp, uint16 len, void* mem)
+{
+       uint16 mlen;
+       uint8* mmem;
+       uint16 n;
+       assert(len>0);
+       mlen=len;
+       mmem=mem;
+       do
+       {
+               if (sp->in_buffer_togo==0)
+               {
+                       if (OJPEGReadBufferFill(sp)==0)
+                               return(0);
+                       assert(sp->in_buffer_togo>0);
+               }
+               n=mlen;
+               if (n>sp->in_buffer_togo)
+                       n=sp->in_buffer_togo;
+               _TIFFmemcpy(mmem,sp->in_buffer_cur,n);
+               sp->in_buffer_cur+=n;
+               sp->in_buffer_togo-=n;
+               mlen-=n;
+               mmem+=n;
+       } while(mlen>0);
+       return(1);
+}
+
+static void
+OJPEGReadSkip(OJPEGState* sp, uint16 len)
+{
+       uint16 m;
+       uint16 n;
+       m=len;
+       n=m;
+       if (n>sp->in_buffer_togo)
+               n=sp->in_buffer_togo;
+       sp->in_buffer_cur+=n;
+       sp->in_buffer_togo-=n;
+       m-=n;
+       if (m>0)
+       {
+               assert(sp->in_buffer_togo==0);
+               n=m;
+               if (n>sp->in_buffer_file_togo)
+                       n=sp->in_buffer_file_togo;
+               sp->in_buffer_file_pos+=n;
+               sp->in_buffer_file_togo-=n;
+               sp->in_buffer_file_pos_log=0;
+               /* we don't skip past jpeginterchangeformat/strile block...
+                * if that is asked from us, we're dealing with totally bazurk
+                * data anyway, and we've not seen this happening on any
+                * testfile, so we might as well likely cause some other
+                * meaningless error to be passed at some later time
+                */
+       }
+}
 
 static int
-OJPEGPreEncode(register TIFF *tif,tsample_t s)
-  { register OJPEGState *sp = OJState(tif);
-#   define td (&tif->tif_dir)
-
- /* If we are about to write the first row of an image plane, which should
-    coincide with a JPEG "scan", reset the JPEG Library's compressor.  Otherwise
-    let the compressor run "as is" and return a "success" status without further
-    ado.
- */
-    if (     (isTiled(tif) ? tif->tif_curtile : tif->tif_curstrip)
-           % td->td_stripsperimage
-        == 0
-       )
-      {
-        if (   (sp->cinfo.c.comp_info[0].component_id = s) == 1)
-            && sp->cinfo.c.jpeg_color_space == JCS_YCbCr
-           )
-          {
-            sp->cinfo.c.comp_info[0].quant_tbl_no =
-            sp->cinfo.c.comp_info[0].dc_tbl_no =
-            sp->cinfo.c.comp_info[0].ac_tbl_no = 1;
-            sp->cinfo.c.comp_info[0].h_samp_factor = sp->h_sampling;
-            sp->cinfo.c.comp_info[0].v_samp_factor = sp->v_sampling;
-    
-         /* Scale expected strip/tile size to match a downsampled component. */
-    
-            sp->cinfo.c.image_width = TIFFhowmany(segment_width,sp->h_sampling);
-            sp->cinfo.c.image_height=TIFFhowmany(segment_height,sp->v_sampling);
-          };
-        sp->scancount = 0; /* Mark subsampling buffer(s) empty */
-      };
-    return 1;
-#   undef td
-  }
+OJPEGWriteStream(TIFF* tif, void** mem, uint32* len)
+{
+       OJPEGState* sp=(OJPEGState*)tif->tif_data;
+       *len=0;
+       do
+       {
+               assert(sp->out_state<=ososEoi);
+               switch(sp->out_state)
+               {
+                       case ososSoi:
+                               OJPEGWriteStreamSoi(tif,mem,len);
+                               break;
+                       case ososQTable0:
+                               OJPEGWriteStreamQTable(tif,0,mem,len);
+                               break;
+                       case ososQTable1:
+                               OJPEGWriteStreamQTable(tif,1,mem,len);
+                               break;
+                       case ososQTable2:
+                               OJPEGWriteStreamQTable(tif,2,mem,len);
+                               break;
+                       case ososQTable3:
+                               OJPEGWriteStreamQTable(tif,3,mem,len);
+                               break;
+                       case ososDcTable0:
+                               OJPEGWriteStreamDcTable(tif,0,mem,len);
+                               break;
+                       case ososDcTable1:
+                               OJPEGWriteStreamDcTable(tif,1,mem,len);
+                               break;
+                       case ososDcTable2:
+                               OJPEGWriteStreamDcTable(tif,2,mem,len);
+                               break;
+                       case ososDcTable3:
+                               OJPEGWriteStreamDcTable(tif,3,mem,len);
+                               break;
+                       case ososAcTable0:
+                               OJPEGWriteStreamAcTable(tif,0,mem,len);
+                               break;
+                       case ososAcTable1:
+                               OJPEGWriteStreamAcTable(tif,1,mem,len);
+                               break;
+                       case ososAcTable2:
+                               OJPEGWriteStreamAcTable(tif,2,mem,len);
+                               break;
+                       case ososAcTable3:
+                               OJPEGWriteStreamAcTable(tif,3,mem,len);
+                               break;
+                       case ososDri:
+                               OJPEGWriteStreamDri(tif,mem,len);
+                               break;
+                       case ososSof:
+                               OJPEGWriteStreamSof(tif,mem,len);
+                               break;
+                       case ososSos:
+                               OJPEGWriteStreamSos(tif,mem,len);
+                               break;
+                       case ososCompressed:
+                               if (OJPEGWriteStreamCompressed(tif,mem,len)==0)
+                                       return(0);
+                               break;
+                       case ososRst:
+                               OJPEGWriteStreamRst(tif,mem,len);
+                               break;
+                       case ososEoi:
+                               OJPEGWriteStreamEoi(tif,mem,len);
+                               break;
+               }
+       } while (*len==0);
+       return(1);
+}
+
+static void
+OJPEGWriteStreamSoi(TIFF* tif, void** mem, uint32* len)
+{
+       OJPEGState* sp=(OJPEGState*)tif->tif_data;
+       assert(OJPEG_BUFFER>=2);
+       sp->out_buffer[0]=255;
+       sp->out_buffer[1]=JPEG_MARKER_SOI;
+       *len=2;
+       *mem=(void*)sp->out_buffer;
+       sp->out_state++;
+}
+
+static void
+OJPEGWriteStreamQTable(TIFF* tif, uint8 table_index, void** mem, uint32* len)
+{
+       OJPEGState* sp=(OJPEGState*)tif->tif_data;
+       if (sp->qtable[table_index]!=0)
+       {
+               *mem=(void*)(sp->qtable[table_index]+sizeof(uint32));
+               *len=*((uint32*)sp->qtable[table_index])-sizeof(uint32);
+       }
+       sp->out_state++;
+}
+
+static void
+OJPEGWriteStreamDcTable(TIFF* tif, uint8 table_index, void** mem, uint32* len)
+{
+       OJPEGState* sp=(OJPEGState*)tif->tif_data;
+       if (sp->dctable[table_index]!=0)
+       {
+               *mem=(void*)(sp->dctable[table_index]+sizeof(uint32));
+               *len=*((uint32*)sp->dctable[table_index])-sizeof(uint32);
+       }
+       sp->out_state++;
+}
+
+static void
+OJPEGWriteStreamAcTable(TIFF* tif, uint8 table_index, void** mem, uint32* len)
+{
+       OJPEGState* sp=(OJPEGState*)tif->tif_data;
+       if (sp->actable[table_index]!=0)
+       {
+               *mem=(void*)(sp->actable[table_index]+sizeof(uint32));
+               *len=*((uint32*)sp->actable[table_index])-sizeof(uint32);
+       }
+       sp->out_state++;
+}
+
+static void
+OJPEGWriteStreamDri(TIFF* tif, void** mem, uint32* len)
+{
+       OJPEGState* sp=(OJPEGState*)tif->tif_data;
+       assert(OJPEG_BUFFER>=6);
+       if (sp->restart_interval!=0)
+       {
+               sp->out_buffer[0]=255;
+               sp->out_buffer[1]=JPEG_MARKER_DRI;
+               sp->out_buffer[2]=0;
+               sp->out_buffer[3]=4;
+               sp->out_buffer[4]=(sp->restart_interval>>8);
+               sp->out_buffer[5]=(sp->restart_interval&255);
+               *len=6;
+               *mem=(void*)sp->out_buffer;
+       }
+       sp->out_state++;
+}
+
+static void
+OJPEGWriteStreamSof(TIFF* tif, void** mem, uint32* len)
+{
+       OJPEGState* sp=(OJPEGState*)tif->tif_data;
+       uint8 m;
+       assert(OJPEG_BUFFER>=2+8+sp->samples_per_pixel_per_plane*3);
+       assert(255>=8+sp->samples_per_pixel_per_plane*3);
+       sp->out_buffer[0]=255;
+       sp->out_buffer[1]=sp->sof_marker_id;
+       /* Lf */
+       sp->out_buffer[2]=0;
+       sp->out_buffer[3]=8+sp->samples_per_pixel_per_plane*3;
+       /* P */
+       sp->out_buffer[4]=8;
+       /* Y */
+       sp->out_buffer[5]=(sp->sof_y>>8);
+       sp->out_buffer[6]=(sp->sof_y&255);
+       /* X */
+       sp->out_buffer[7]=(sp->sof_x>>8);
+       sp->out_buffer[8]=(sp->sof_x&255);
+       /* Nf */
+       sp->out_buffer[9]=sp->samples_per_pixel_per_plane;
+       for (m=0; m<sp->samples_per_pixel_per_plane; m++)
+       {
+               /* C */
+               sp->out_buffer[10+m*3]=sp->sof_c[sp->plane_sample_offset+m];
+               /* H and V */
+               sp->out_buffer[10+m*3+1]=sp->sof_hv[sp->plane_sample_offset+m];
+               /* Tq */
+               sp->out_buffer[10+m*3+2]=sp->sof_tq[sp->plane_sample_offset+m];
+       }
+       *len=10+sp->samples_per_pixel_per_plane*3;
+       *mem=(void*)sp->out_buffer;
+       sp->out_state++;
+}
+
+static void
+OJPEGWriteStreamSos(TIFF* tif, void** mem, uint32* len)
+{
+       OJPEGState* sp=(OJPEGState*)tif->tif_data;
+       uint8 m;
+       assert(OJPEG_BUFFER>=2+6+sp->samples_per_pixel_per_plane*2);
+       assert(255>=6+sp->samples_per_pixel_per_plane*2);
+       sp->out_buffer[0]=255;
+       sp->out_buffer[1]=JPEG_MARKER_SOS;
+       /* Ls */
+       sp->out_buffer[2]=0;
+       sp->out_buffer[3]=6+sp->samples_per_pixel_per_plane*2;
+       /* Ns */
+       sp->out_buffer[4]=sp->samples_per_pixel_per_plane;
+       for (m=0; m<sp->samples_per_pixel_per_plane; m++)
+       {
+               /* Cs */
+               sp->out_buffer[5+m*2]=sp->sos_cs[sp->plane_sample_offset+m];
+               /* Td and Ta */
+               sp->out_buffer[5+m*2+1]=sp->sos_tda[sp->plane_sample_offset+m];
+       }
+       /* Ss */
+       sp->out_buffer[5+sp->samples_per_pixel_per_plane*2]=0;
+       /* Se */
+       sp->out_buffer[5+sp->samples_per_pixel_per_plane*2+1]=63;
+       /* Ah and Al */
+       sp->out_buffer[5+sp->samples_per_pixel_per_plane*2+2]=0;
+       *len=8+sp->samples_per_pixel_per_plane*2;
+       *mem=(void*)sp->out_buffer;
+       sp->out_state++;
+}
 
 static int
-OJPEGPostEncode(register TIFF *tif)
-  { register OJPEGState *sp = OJState(tif);
-
- /* Finish up at the end of a strip or tile. */
-
-    if (sp->scancount > 0) /* emit partial buffer of down-sampled data */
-      { JDIMENSION n;
-
-#       ifdef C_LOSSLESS_SUPPORTED
-        if (   sp->scancount < sp->cinfo.c.data_unit
-            && sp->cinfo.c.num_components > 0
-           )
-#       else
-        if (sp->scancount < DCTSIZE && sp->cinfo.c.num_components > 0)
-#       endif
-          { int ci = 0,                            /* Pad the data vertically */
-#           ifdef C_LOSSLESS_SUPPORTED
-                size = sp->cinfo.c.data_unit;
-#           else
-                size = DCTSIZE;
-#           endif
-            register jpeg_component_info *compptr = sp->cinfo.c.comp_info;
-
-            do
-#              ifdef C_LOSSLESS_SUPPORTED
-               { tsize_t row_width = compptr->width_in_data_units
-#              else
-                 tsize_t row_width = compptr->width_in_blocks
-#              endif
-                   *size*sizeof(JSAMPLE);
-                 int ypos = sp->scancount*compptr->v_samp_factor;
-
-                 do _TIFFmemcpy( (tdata_t)sp->ds_buffer[ci][ypos]
-                               , (tdata_t)sp->ds_buffer[ci][ypos-1]
-                               , row_width
-                               );
-                 while (++ypos < compptr->v_samp_factor*size);
-               }
-            while (++compptr,++ci < sp->cinfo.c.num_components);
-          };
-        n = sp->cinfo.c.max_v_samp_factor*size;
-        if (CALLJPEG(sp,-1,jpeg_write_raw_data(&sp->cinfo.c,sp->ds_buffer,n)) != n)
-          return 0;
-      };
-    return CALLVJPEG(sp,jpeg_finish_compress(&sp->cinfo.c));
-  }
-#endif /* never */
-
-/* JPEG Decoding begins here. */
-
-/*ARGSUSED*/ static int
-OJPEGDecode(register TIFF *tif,tidata_t buf,tsize_t cc,tsample_t s)
-  { tsize_t bytesperline = isTiled(tif)
-                         ? TIFFTileRowSize(tif)
-                         : tif->tif_scanlinesize,
-            rows;                          /* No. of unprocessed rows in file */
-    register OJPEGState *sp = OJState(tif);
-
- /* Decode a chunk of pixels, where the input data has not NOT been down-
-    sampled, or else the TIFF Library's client has used the "JPEGColorMode" TIFF
-    pseudo-tag to request that the JPEG Library do color-space conversion; this
-    is the normal case.  The data is expected to be read in scan-line multiples,
-    and this subroutine is called for both pixel-interleaved and separate color
-    planes.
-
-    WARNING:  Unlike "OJPEGDecodeRawContig()", below, the no. of Bytes in each
-              decoded row is calculated here as "bytesperline" instead of
-    using "sp->bytesperline", which might be a little smaller.  This can
-    occur for an old tiled image whose width isn't a multiple of 8 pixels.
-    That's illegal according to the TIFF Version 6 specification, but some
-    test files, like "zackthecat.tif", were built that way.  In those cases,
-    we want to embed the image's true width in our caller's buffer (which is
-    presumably allocated according to the expected tile width) by
-    effectively "padding" it with unused Bytes at the end of each row.
- */
-    if ( (cc /= bytesperline)      /* No. of complete rows in caller's buffer */
-       > (rows = sp->cinfo.d.output_height - sp->cinfo.d.output_scanline)
-       ) cc = rows;
-    while (--cc >= 0)
-      {
-        if (   CALLJPEG(sp,-1,jpeg_read_scanlines(&sp->cinfo.d,(JSAMPARRAY)&buf,1))
-            != 1
-           ) return 0;
-        buf += bytesperline;
-        ++tif->tif_row;
-      };
-
- /* BEWARE OF KLUDGE:  If our input file was produced by Microsoft's Wang
-                       Imaging for Windows application, the DC coefficients of
-    each JPEG image component (Y,Cb,Cr) must be reset at the end of each TIFF
-    "strip", and any JPEG data bits remaining in the current Byte of the
-    decoder's input buffer must be discarded.  To do so, we create an "ad hoc"
-    interface in the "jdhuff.c" module of IJG JPEG Library Version 6 (module
-    "jdshuff.c", if Ken Murchison's lossless-Huffman patch is applied), and we
-    invoke that interface here after decoding each "strip".
- */
-    if (sp->is_WANG) jpeg_reset_huff_decode(&sp->cinfo.d);
-    return 1;
-  }
-
-/*ARGSUSED*/ static int
-OJPEGDecodeRawContig(register TIFF *tif,tidata_t buf,tsize_t cc,tsample_t s)
-  { tsize_t rows;                          /* No. of unprocessed rows in file */
-    JDIMENSION lines_per_MCU, size;
-    register OJPEGState *sp = OJState(tif);
-
- /* Decode a chunk of pixels, where the input data has pixel-interleaved color
-    planes, some of which have been down-sampled, but the TIFF Library's client
-    has NOT used the "JPEGColorMode" TIFF pseudo-tag to request that the JPEG
-    Library do color-space conversion.  In other words, we must up-sample/
-    expand/duplicate image components according to the image's sampling factors,
-    without changing its color space.  The data is expected to be read in scan-
-    line multiples.
- */
-    if ( (cc /= sp->bytesperline)  /* No. of complete rows in caller's buffer */
-       > (rows = sp->cinfo.d.output_height - sp->cinfo.d.output_scanline)
-       ) cc = rows;
-    lines_per_MCU = sp->cinfo.d.max_v_samp_factor
-#   ifdef D_LOSSLESS_SUPPORTED
-                  * (size = sp->cinfo.d.min_codec_data_unit);
-#   else
-                  * (size = DCTSIZE);
-#   endif
-    while (--cc >= 0)
-      { int clumpoffset, ci;
-        register jpeg_component_info *compptr;
-
-        if (sp->scancount >= size) /* reload downsampled-data buffers */
-          {
-            if (   CALLJPEG(sp,-1,jpeg_read_raw_data(&sp->cinfo.d,sp->ds_buffer,lines_per_MCU))
-                != lines_per_MCU
-               ) return 0;
-            sp->scancount = 0;
-          };
-
-     /* The fastest way to separate the data is: make 1 pass over the scan
-        line for each row of each component.
-     */
-        clumpoffset = ci = 0;
-        compptr = sp->cinfo.d.comp_info;
-        do
-          { int ypos = 0;
-
-            if (compptr->h_samp_factor == 1) /* fast path */
-              do
-                { register JSAMPLE *inptr =
-                    sp->ds_buffer[ci][sp->scancount*compptr->v_samp_factor+ypos],
-                                   *outptr = (JSAMPLE *)buf + clumpoffset;
-                  register int clumps_per_line = compptr->downsampled_width;
-
-                  do *outptr = *inptr++;
-                  while ((outptr += sp->samplesperclump),--clumps_per_line > 0);
-                }
-              while ( (clumpoffset += compptr->h_samp_factor)
-                    , ++ypos < compptr->v_samp_factor
-                    );
-            else /* general case */
-              do
-                { register JSAMPLE *inptr =
-                    sp->ds_buffer[ci][sp->scancount*compptr->v_samp_factor+ypos],
-                                   *outptr = (JSAMPLE *)buf + clumpoffset;
-                  register int clumps_per_line = compptr->downsampled_width;
-
-                  do
-                    { register int xpos = 0;
-
-                      do outptr[xpos] = *inptr++;
-                      while (++xpos < compptr->h_samp_factor);
-                    }
-                  while ((outptr += sp->samplesperclump),--clumps_per_line > 0);
-                }
-              while ( (clumpoffset += compptr->h_samp_factor)
-                    , ++ypos < compptr->v_samp_factor
-                    );
-          }
-        while (++compptr,++ci < sp->cinfo.d.num_components);
-        ++sp->scancount;
-        buf += sp->bytesperline;
-        ++tif->tif_row;
-      };
-
- /* BEWARE OF KLUDGE:  If our input file was produced by Microsoft's Wang
-                       Imaging for Windows application, the DC coefficients of
-    each JPEG image component (Y,Cb,Cr) must be reset at the end of each TIFF
-    "strip", and any JPEG data bits remaining in the current Byte of the
-    decoder's input buffer must be discarded.  To do so, we create an "ad hoc"
-    interface in the "jdhuff.c" module of IJG JPEG Library Version 6 (module
-    "jdshuff.c", if Ken Murchison's lossless-Huffman patch is applied), and we
-    invoke that interface here after decoding each "strip".
- */
-    if (sp->is_WANG) jpeg_reset_huff_decode(&sp->cinfo.d);
-    return 1;
-  }
-
-/*ARGSUSED*/ static int
-OJPEGDecodeRawSeparate(TIFF *tif,register tidata_t buf,tsize_t cc,tsample_t s)
-  { tsize_t rows;                          /* No. of unprocessed rows in file */
-    JDIMENSION lines_per_MCU,
-               size,                                             /* ...of MCU */
-               v;                   /* Component's vertical up-sampling ratio */
-    register OJPEGState *sp = OJState(tif);
-    register jpeg_component_info *compptr = sp->cinfo.d.comp_info + s;
-
- /* Decode a chunk of pixels, where the input data has separate color planes,
-    some of which have been down-sampled, but the TIFF Library's client has NOT
-    used the "JPEGColorMode" TIFF pseudo-tag to request that the JPEG Library
-    do color-space conversion.  The data is expected to be read in scan-line
-    multiples.
- */
-    v = sp->cinfo.d.max_v_samp_factor/compptr->v_samp_factor;
-    if ( (cc /= compptr->downsampled_width) /* No. of rows in caller's buffer */
-       > (rows = (sp->cinfo.d.output_height-sp->cinfo.d.output_scanline+v-1)/v)
-       ) cc = rows; /* No. of rows of "clumps" to read */
-    lines_per_MCU = sp->cinfo.d.max_v_samp_factor
-#   ifdef D_LOSSLESS_SUPPORTED
-                  * (size = sp->cinfo.d.min_codec_data_unit);
-#   else
-                  * (size = DCTSIZE);
-#   endif
- L: if (sp->scancount >= size) /* reload downsampled-data buffers */
-      {
-        if (   CALLJPEG(sp,-1,jpeg_read_raw_data(&sp->cinfo.d,sp->ds_buffer,lines_per_MCU))
-            != lines_per_MCU
-           ) return 0;
-        sp->scancount = 0;
-      };
-    rows = 0;
-    do
-      { register JSAMPLE *inptr =
-          sp->ds_buffer[s][sp->scancount*compptr->v_samp_factor + rows];
-        register int clumps_per_line = compptr->downsampled_width;
-
-        do *buf++ = *inptr++; while (--clumps_per_line > 0); /* Copy scanline */
-        tif->tif_row += v;
-        if (--cc <= 0) return 1; /* End of caller's buffer? */
-      }
-    while (++rows < compptr->v_samp_factor);
-    ++sp->scancount;
-    goto L;
-  }
-
-/* "OJPEGSetupDecode()" temporarily forces the JPEG Library to use the following
-   subroutine as a "dummy" input reader in order to fool the library into
-   thinking that it has read the image's first "Start of Scan" (SOS) marker, so
-   that it initializes accordingly.
-*/
-/*ARGSUSED*/ METHODDEF(int)
-fake_SOS_marker(j_decompress_ptr cinfo){return JPEG_REACHED_SOS;}
-
-/*ARGSUSED*/ METHODDEF(int)
-suspend(j_decompress_ptr cinfo){return JPEG_SUSPENDED;}
-
-/* The JPEG Library's "null" color-space converter actually re-packs separate
-   color planes (it's native image representation) into a pixel-interleaved,
-   contiguous plane.  But if our TIFF Library client is tryng to process a
-   PLANARCONFIG_SEPARATE image, we don't want that; so here are modifications of
-   code in the JPEG Library's "jdcolor.c" file, which simply copy Bytes to a
-   color plane specified by the current JPEG "scan".
-*/
-METHODDEF(void)
-ycc_rgb_convert(register j_decompress_ptr cinfo,JSAMPIMAGE in,JDIMENSION row,
-                register JSAMPARRAY out,register int nrows)
-  { typedef struct                /* "jdcolor.c" color-space conversion state */
-      {
-
-     /* WARNING:  This declaration is ugly and dangerous!  It's supposed to be
-                  private to the JPEG Library's "jdcolor.c" module, but we also
-        need it here.  Since the library's copy might change without notice, be
-        sure to keep this one synchronized or the following code will break!
-     */
-        struct jpeg_color_deconverter pub; /* Public fields */
-     /* Private state for YCC->RGB conversion */
-        int *Cr_r_tab,   /* ->Cr to R conversion table */
-            *Cb_b_tab;   /* ->Cb to B conversion table */
-        INT32 *Cr_g_tab, /* ->Cr to G conversion table */
-              *Cb_g_tab; /* ->Cb to G conversion table */
-      } *my_cconvert_ptr;
-    my_cconvert_ptr cconvert = (my_cconvert_ptr)cinfo->cconvert;
-    JSAMPARRAY irow0p = in[0] + row;
-    register JSAMPLE *range_limit = cinfo->sample_range_limit;
-    register JSAMPROW outp, Y;
-
-    switch (cinfo->output_scan_number - 1)
-      { JSAMPARRAY irow1p, irow2p;
-        register INT32 *table0, *table1;
-        SHIFT_TEMPS
-
-        case RGB_RED  : irow2p = in[2] + row;
-                        table0 = (INT32 *)cconvert->Cr_r_tab;
-                        while (--nrows >= 0)
-                          { register JSAMPROW Cr = *irow2p++;
-                             register int i = cinfo->output_width;
-
-                             Y = *irow0p++;
-                             outp = *out++;
-                             while (--i >= 0)
-                               *outp++ = range_limit[*Y++ + table0[*Cr++]];
-                          };
-                        return;
-        case RGB_GREEN: irow1p = in[1] + row;
-                        irow2p = in[2] + row;
-                        table0 = cconvert->Cb_g_tab;
-                        table1 = cconvert->Cr_g_tab;
-                        while (--nrows >= 0)
-                          { register JSAMPROW Cb = *irow1p++,
-                                              Cr = *irow2p++;
-                             register int i = cinfo->output_width;
-
-                             Y = *irow0p++;
-                             outp = *out++;
-                             while (--i >= 0)
-                               *outp++ =
-                                 range_limit[ *Y++
-                                            + RIGHT_SHIFT(table0[*Cb++]+table1[*Cr++],16)
-                                            ];
-                          };
-                        return;
-        case RGB_BLUE : irow1p = in[1] + row;
-                        table0 = (INT32 *)cconvert->Cb_b_tab;
-                        while (--nrows >= 0)
-                          { register JSAMPROW Cb = *irow1p++;
-                             register int i = cinfo->output_width;
-
-                             Y = *irow0p++;
-                             outp = *out++;
-                             while (--i >= 0)
-                               *outp++ = range_limit[*Y++ + table0[*Cb++]];
-                          }
-      }
-  }
-
-METHODDEF(void)
-null_convert(register j_decompress_ptr cinfo,JSAMPIMAGE in,JDIMENSION row,
-             register JSAMPARRAY out,register int nrows)
-  { register JSAMPARRAY irowp = in[cinfo->output_scan_number - 1] + row;
-
-    while (--nrows >= 0) _TIFFmemcpy(*out++,*irowp++,cinfo->output_width);
-  }
+OJPEGWriteStreamCompressed(TIFF* tif, void** mem, uint32* len)
+{
+       OJPEGState* sp=(OJPEGState*)tif->tif_data;
+       if (sp->in_buffer_togo==0)
+       {
+               if (OJPEGReadBufferFill(sp)==0)
+                       return(0);
+               assert(sp->in_buffer_togo>0);
+       }
+       *len=sp->in_buffer_togo;
+       *mem=(void*)sp->in_buffer_cur;
+       sp->in_buffer_togo=0;
+       if (sp->in_buffer_file_togo==0)
+       {
+               switch(sp->in_buffer_source)
+               {
+                       case osibsStrile:
+                               if (sp->in_buffer_next_strile<sp->in_buffer_strile_count)  
+                                       sp->out_state=ososRst;
+                               else
+                                       sp->out_state=ososEoi;
+                               break;
+                       case osibsEof:
+                               sp->out_state=ososEoi;
+                               break;
+                       default:
+                               break;
+               }
+       }
+       return(1);
+}
+
+static void
+OJPEGWriteStreamRst(TIFF* tif, void** mem, uint32* len)
+{
+       OJPEGState* sp=(OJPEGState*)tif->tif_data;
+       assert(OJPEG_BUFFER>=2);
+       sp->out_buffer[0]=255;
+       sp->out_buffer[1]=JPEG_MARKER_RST0+sp->restart_index;
+       sp->restart_index++;
+       if (sp->restart_index==8)
+               sp->restart_index=0;
+       *len=2;
+       *mem=(void*)sp->out_buffer;
+       sp->out_state=ososCompressed;
+}
+
+static void
+OJPEGWriteStreamEoi(TIFF* tif, void** mem, uint32* len)
+{
+       OJPEGState* sp=(OJPEGState*)tif->tif_data;
+       assert(OJPEG_BUFFER>=2);
+       sp->out_buffer[0]=255;
+       sp->out_buffer[1]=JPEG_MARKER_EOI;
+       *len=2;
+       *mem=(void*)sp->out_buffer;
+}
 
+#ifndef LIBJPEG_ENCAP_EXTERNAL
 static int
-OJPEGSetupDecode(register TIFF *tif)
-  { static char module[]={"OJPEGSetupDecode"};
-    J_COLOR_SPACE jpeg_color_space,   /* Color space of JPEG-compressed image */
-                  out_color_space;       /* Color space of decompressed image */
-    uint32 segment_width;
-    int status = 1;                              /* Assume success by default */
-    boolean downsampled_output=FALSE, /* <=> Want JPEG Library's "raw" image? */
-            is_JFIF;                                       /* <=> JFIF image? */
-    register OJPEGState *sp = OJState(tif);
-#   define td (&tif->tif_dir)
-
- /* Verify miscellaneous parameters.  This will need work if the TIFF Library
-    ever supports different depths for different components, or if the JPEG
-    Library ever supports run-time depth selection.  Neither seems imminent.
- */
-    if (td->td_bitspersample != sp->cinfo.d.data_precision)
-      {
-        TIFFError(module,bad_bps,td->td_bitspersample);
-        status = 0;
-      };
-
- /* The TIFF Version 6.0 specification and IJG JPEG Library accept different
-    sets of color spaces, so verify that our image belongs to the common subset
-    and map its photometry code, then initialize to handle subsampling and
-    optional JPEG Library YCbCr <-> RGB color-space conversion.
- */
-    switch (td->td_photometric)
-      {
-        case PHOTOMETRIC_YCBCR     :
-
-       /* ISO IS 10918-1 requires that JPEG subsampling factors be 1-4, but
-          TIFF Version 6.0 is more restrictive: only 1, 2, and 4 are allowed.
-       */
-          if (   (   td->td_ycbcrsubsampling[0] == 1
-                  || td->td_ycbcrsubsampling[0] == 2
-                  || td->td_ycbcrsubsampling[0] == 4
-                 )
-              && (   td->td_ycbcrsubsampling[1] == 1
-                  || td->td_ycbcrsubsampling[1] == 2
-                  || td->td_ycbcrsubsampling[1] == 4
-                 )
-             )
-            downsampled_output =
-              (
-                (sp->h_sampling = td->td_ycbcrsubsampling[0]) << 3
-              | (sp->v_sampling = td->td_ycbcrsubsampling[1])
-              ) != 011;
-          else
-            {
-              TIFFError(module,bad_subsampling);
-              status = 0;
-            };
-          jpeg_color_space = JCS_YCbCr;
-          if (sp->jpegcolormode == JPEGCOLORMODE_RGB)
-            {
-              downsampled_output = FALSE;
-              out_color_space = JCS_RGB;
-              break;
-            };
-          goto L2;
-        case PHOTOMETRIC_MINISBLACK:
-          jpeg_color_space = JCS_GRAYSCALE;
-          goto L1;
-        case PHOTOMETRIC_RGB       :
-          jpeg_color_space = JCS_RGB;
-          goto L1;
-        case PHOTOMETRIC_SEPARATED :
-          jpeg_color_space = JCS_CMYK;
-      L1: sp->jpegcolormode = JPEGCOLORMODE_RAW; /* No JPEG Lib. conversion */
-      L2: out_color_space = jpeg_color_space;
-          break;
-        default                    :
-          TIFFError(module,bad_photometry,td->td_photometric);
-          status = 0;
-      };
-    if (status == 0) return 0; /* If TIFF errors, don't bother to continue */
-
- /* Set parameters that are same for all strips/tiles. */
-
-    sp->cinfo.d.src = &sp->src;
-    sp->src.init_source = std_init_source;
-    sp->src.fill_input_buffer = std_fill_input_buffer;
-    sp->src.skip_input_data = std_skip_input_data;
-    sp->src.resync_to_restart = jpeg_resync_to_restart;
-    sp->src.term_source = std_term_source;
-
- /* BOGOSITY ALERT!  The Wang Imaging application for Microsoft Windows produces
-                     images containing "JPEGInterchangeFormat[Length]" TIFF
-    records that resemble JFIF-in-TIFF encapsulations but, in fact, violate the
-    TIFF Version 6 specification in several ways; nevertheless, we try to handle
-    them gracefully because there are apparently a lot of them around.  The
-    purported "JFIF" data stream in one of these files vaguely resembles a JPEG
-    "tables only" data stream, except that there's no trailing EOI marker.  The
-    rest of the JPEG data stream lies in a discontiguous file region, identified
-    by the 0th Strip offset (which is *also* illegal!), where it begins with an
-    SOS marker and apparently continues to the end of the file.  There is no
-    trailing EOI marker here, either.
- */
-    is_JFIF = !sp->is_WANG && TIFFFieldSet(tif,FIELD_JPEGIFOFFSET);
+jpeg_create_decompress_encap(OJPEGState* sp, jpeg_decompress_struct* cinfo)
+{
+       return(SETJMP(sp->exit_jmpbuf)?0:(jpeg_create_decompress(cinfo),1));
+}
+#endif
 
- /* Initialize decompression parameters that won't be overridden by JPEG Library
-    defaults set during the "jpeg_read_header()" call, below.
- */
-    segment_width = td->td_imagewidth;
-    if (isTiled(tif))
-      {
-        if (sp->is_WANG) /* we don't know how to handle it */
-          {
-            TIFFError(module,"Tiled Wang image not supported");
-            return 0;
-          };
-
-     /* BOGOSITY ALERT!  "TIFFTileRowSize()" seems to work fine for modern JPEG-
-                         in-TIFF encapsulations where the image width--like the
-        tile width--is a multiple of 8 or 16 pixels.  But image widths and
-        heights are aren't restricted to 8- or 16-bit multiples, and we need
-        the exact Byte count of decompressed scan lines when we call the JPEG
-        Library.  At least one old file ("zackthecat.tif") in the TIFF Library
-        test suite has widths and heights slightly less than the tile sizes, and
-        it apparently used the bogus computation below to determine the number
-        of Bytes per scan line (was this due to an old, broken version of
-        "TIFFhowmany()"?).  Before we get here, "OJPEGSetupDecode()" verified
-        that our image uses 8-bit samples, so the following check appears to
-        return the correct answer in all known cases tested to date.
-     */
-        if (is_JFIF || (segment_width & 7) == 0)
-          sp->bytesperline = TIFFTileRowSize(tif); /* Normal case */
-        else
-          {
-            /* Was the file-encoder's segment-width calculation bogus? */
-            segment_width = (segment_width/sp->h_sampling + 1) * sp->h_sampling;
-            sp->bytesperline = segment_width * td->td_samplesperpixel;
-          }
-      }
-    else sp->bytesperline = TIFFVStripSize(tif,1);
-
- /* BEWARE OF KLUDGE:  If we have JPEG Interchange File Format (JFIF) image,
-                       then we want to read "metadata" in the bit-stream's
-    header and validate it against corresponding information in TIFF records.
-    But if we have a *really old* JPEG file that's not JFIF, then we simply
-    assign TIFF-record values to JPEG Library variables without checking.
- */
-    if (is_JFIF) /* JFIF image */
-      { unsigned char *end_of_data;
-        int subsampling_factors;
-        register unsigned char *p;
-        register int i;
-
-     /* WARNING:  Although the image file contains a JFIF bit stream, it might
-                  also contain some old TIFF records causing "OJPEGVSetField()"
-        to have allocated quantization or Huffman decoding tables.  But when the
-        JPEG Library reads and parses the JFIF header below, it reallocate these
-        tables anew without checking for "dangling" pointers, thereby causing a
-        memory "leak".  We have enough information to potentially deallocate the
-        old tables here, but unfortunately JPEG Library Version 6B uses a "pool"
-        allocator for small objects, with no deallocation procedure; instead, it
-        reclaims a whole pool when an image is closed/destroyed, so well-behaved
-        TIFF client applications (i.e., those which close their JPEG images as
-        soon as they're no longer needed) will waste memory for a short time but
-        recover it eventually.  But ill-behaved TIFF clients (i.e., those which
-        keep many JPEG images open gratuitously) can exhaust memory prematurely.
-        If the JPEG Library ever implements a deallocation procedure, insert
-        this clean-up code:
-     */
-#       ifdef someday
-        if (sp->jpegtablesmode & JPEGTABLESMODE_QUANT) /* free quant. tables */
-          { register int i = 0;
-
-            do
-              { register JQUANT_TBL *q;
-
-                if (q = sp->cinfo.d.quant_tbl_ptrs[i])
-                  {
-                    jpeg_free_small(&sp->cinfo.comm,q,sizeof *q);
-                    sp->cinfo.d.quant_tbl_ptrs[i] = 0;
-                  }
-              }
-            while (++i < NUM_QUANT_TBLS);
-          };
-        if (sp->jpegtablesmode & JPEGTABLESMODE_HUFF) /* free Huffman tables */
-          { register int i = 0;
-
-            do
-              { register JHUFF_TBL *h;
-
-                if (h = sp->cinfo.d.dc_huff_tbl_ptrs[i])
-                  {
-                    jpeg_free_small(&sp->cinfo.comm,h,sizeof *h);
-                    sp->cinfo.d.dc_huff_tbl_ptrs[i] = 0;
-                  };
-                if (h = sp->cinfo.d.ac_huff_tbl_ptrs[i])
-                  {
-                    jpeg_free_small(&sp->cinfo.comm,h,sizeof *h);
-                    sp->cinfo.d.ac_huff_tbl_ptrs[i] = 0;
-                  }
-              }
-            while (++i < NUM_HUFF_TBLS);
-          };
-#       endif /* someday */
-
-     /* Since we might someday wish to try rewriting "old format" JPEG-in-TIFF
-        encapsulations in "new format" files, try to synthesize the value of a
-        modern "JPEGTables" TIFF record by scanning the JPEG data from just past
-        the "Start of Information" (SOI) marker until something other than a
-        legitimate "table" marker is found, as defined in ISO IS 10918-1
-        Appending B.2.4; namely:
-
-        -- Define Quantization Table (DQT)
-        -- Define Huffman Table (DHT)
-        -- Define Arithmetic Coding table (DAC)
-        -- Define Restart Interval (DRI)
-        -- Comment (COM)
-        -- Application data (APPn)
-
-        For convenience, we also accept "Expansion" (EXP) markers, although they
-        are apparently not a part of normal "table" data.
-     */
-        sp->jpegtables = p = (unsigned char *)sp->src.next_input_byte;
-        end_of_data = p + sp->src.bytes_in_buffer;
-        p += 2;
-        while (p < end_of_data && p[0] == 0xFF)
-          switch (p[1])
-            {
-              default  : goto L;
-              case 0xC0: /* SOF0  */
-              case 0xC1: /* SOF1  */
-              case 0xC2: /* SOF2  */
-              case 0xC3: /* SOF3  */
-              case 0xC4: /* DHT   */
-              case 0xC5: /* SOF5  */
-              case 0xC6: /* SOF6  */
-              case 0xC7: /* SOF7  */
-              case 0xC9: /* SOF9  */
-              case 0xCA: /* SOF10 */
-              case 0xCB: /* SOF11 */
-              case 0xCC: /* DAC   */
-              case 0xCD: /* SOF13 */
-              case 0xCE: /* SOF14 */
-              case 0xCF: /* SOF15 */
-              case 0xDB: /* DQT   */
-              case 0xDD: /* DRI   */
-              case 0xDF: /* EXP   */
-              case 0xE0: /* APP0  */
-              case 0xE1: /* APP1  */
-              case 0xE2: /* APP2  */
-              case 0xE3: /* APP3  */
-              case 0xE4: /* APP4  */
-              case 0xE5: /* APP5  */
-              case 0xE6: /* APP6  */
-              case 0xE7: /* APP7  */
-              case 0xE8: /* APP8  */
-              case 0xE9: /* APP9  */
-              case 0xEA: /* APP10 */
-              case 0xEB: /* APP11 */
-              case 0xEC: /* APP12 */
-              case 0xED: /* APP13 */
-              case 0xEE: /* APP14 */
-              case 0xEF: /* APP15 */
-              case 0xFE: /* COM   */
-                         p += (p[2] << 8 | p[3]) + 2;
-            };
-     L: if (p - (unsigned char *)sp->jpegtables > 2) /* fake "JPEGTables" */
-          {
-
-         /* In case our client application asks, pretend that this image file
-            contains a modern "JPEGTables" TIFF record by copying to a buffer
-            the initial part of the JFIF bit-stream that we just scanned, from
-            the SOI marker through the "metadata" tables, then append an EOI
-            marker and flag the "JPEGTables" TIFF record as "present".
-         */
-            sp->jpegtables_length = p - (unsigned char*)sp->jpegtables + 2;
-            p = sp->jpegtables;
-            if (!(sp->jpegtables = _TIFFmalloc(sp->jpegtables_length)))
-              {
-                TIFFError(module,no_jtable_space);
-                return 0;
-              };
-            _TIFFmemcpy(sp->jpegtables,p,sp->jpegtables_length-2);
-            p = (unsigned char *)sp->jpegtables + sp->jpegtables_length;
-            p[-2] = 0xFF; p[-1] = JPEG_EOI; /* Append EOI marker */
-            TIFFSetFieldBit(tif,FIELD_JPEGTABLES);
-            tif->tif_flags |= TIFF_DIRTYDIRECT;
-          }
-        else sp->jpegtables = 0; /* Don't simulate "JPEGTables" */
-        if (   CALLJPEG(sp,-1,jpeg_read_header(&sp->cinfo.d,TRUE))
-            != JPEG_HEADER_OK
-           ) return 0;
-        if (   sp->cinfo.d.image_width  != segment_width
-            || sp->cinfo.d.image_height != td->td_imagelength 
-           )
-          {
-            TIFFError(module,"Improper JPEG strip/tile size");
-            return 0;
-          };
-        if (sp->cinfo.d.num_components != td->td_samplesperpixel)
-          {
-            TIFFError(module,"Improper JPEG component count");
-            return 0;
-          };
-        if (sp->cinfo.d.data_precision != td->td_bitspersample)
-          {
-            TIFFError(module,"Improper JPEG data precision");
-            return 0;
-          };
-
-     /* Check that JPEG image components all have the same subsampling factors
-        declared (or defaulted) in the TIFF file, since TIFF Version 6.0 is more
-        restrictive than JPEG:  Only the 0th component may have horizontal and
-        vertical subsampling factors other than <1,1>.
-     */
-        subsampling_factors = sp->h_sampling << 3 | sp->v_sampling;
-        i = 0;
-        do
-          {
-            if (   ( sp->cinfo.d.comp_info[i].h_samp_factor << 3
-                   | sp->cinfo.d.comp_info[i].v_samp_factor
-                   )
-                != subsampling_factors
-               )
-              {
-                TIFFError(module,"Improper JPEG subsampling factors");
-                return 0;
-              };
-            subsampling_factors = 011; /* Required for image components > 0 */
-          }
-        while (++i < sp->cinfo.d.num_components);
-      }
-    else /* not JFIF image */
-      { int (*save)(j_decompress_ptr cinfo) = sp->cinfo.d.marker->read_markers;
-        register int i;
-
-     /* We're not assuming that this file's JPEG bit stream has any header
-        "metadata", so fool the JPEG Library into thinking that we read a
-        "Start of Input" (SOI) marker and a "Start of Frame" (SOFx) marker, then
-        force it to read a simulated "Start of Scan" (SOS) marker when we call
-        "jpeg_read_header()" below.  This should cause the JPEG Library to
-        establish reasonable defaults.
-     */
-        sp->cinfo.d.marker->saw_SOI =       /* Pretend we saw SOI marker */
-        sp->cinfo.d.marker->saw_SOF = TRUE; /* Pretend we saw SOF marker */
-        sp->cinfo.d.marker->read_markers =
-          sp->is_WANG ? suspend : fake_SOS_marker;
-        sp->cinfo.d.global_state = DSTATE_INHEADER;
-        sp->cinfo.d.Se = DCTSIZE2-1; /* Suppress JPEG Library warning */
-        sp->cinfo.d.image_width  = segment_width;
-        sp->cinfo.d.image_height = td->td_imagelength;
-
-     /* The following color-space initialization, including the complicated
-        "switch"-statement below, essentially duplicates the logic used by the
-        JPEG Library's "jpeg_init_colorspace()" subroutine during compression.
-     */
-        sp->cinfo.d.num_components = td->td_samplesperpixel;
-        sp->cinfo.d.comp_info = (jpeg_component_info *)
-          (*sp->cinfo.d.mem->alloc_small)
-            ( &sp->cinfo.comm
-            , JPOOL_IMAGE
-            , sp->cinfo.d.num_components * sizeof *sp->cinfo.d.comp_info
-            );
-        i = 0;
-        do
-          {
-            sp->cinfo.d.comp_info[i].component_index = i;
-            sp->cinfo.d.comp_info[i].component_needed = TRUE;
-            sp->cinfo.d.cur_comp_info[i] = &sp->cinfo.d.comp_info[i];
-          }
-        while (++i < sp->cinfo.d.num_components);
-        switch (jpeg_color_space)
-          {
-            case JCS_UNKNOWN  :
-              i = 0;
-              do
-                {
-                  sp->cinfo.d.comp_info[i].component_id = i;
-                  sp->cinfo.d.comp_info[i].h_samp_factor =
-                  sp->cinfo.d.comp_info[i].v_samp_factor = 1;
-                }
-              while (++i < sp->cinfo.d.num_components);
-              break;
-            case JCS_GRAYSCALE:
-              sp->cinfo.d.comp_info[0].component_id =
-              sp->cinfo.d.comp_info[0].h_samp_factor =
-              sp->cinfo.d.comp_info[0].v_samp_factor = 1;
-              break;
-            case JCS_RGB      :
-              sp->cinfo.d.comp_info[0].component_id = 'R';
-              sp->cinfo.d.comp_info[1].component_id = 'G';
-              sp->cinfo.d.comp_info[2].component_id = 'B';
-              i = 0;
-              do sp->cinfo.d.comp_info[i].h_samp_factor =
-                 sp->cinfo.d.comp_info[i].v_samp_factor = 1;
-              while (++i < sp->cinfo.d.num_components);
-              break;
-            case JCS_CMYK     :
-              sp->cinfo.d.comp_info[0].component_id = 'C';
-              sp->cinfo.d.comp_info[1].component_id = 'M';
-              sp->cinfo.d.comp_info[2].component_id = 'Y';
-              sp->cinfo.d.comp_info[3].component_id = 'K';
-              i = 0;
-              do sp->cinfo.d.comp_info[i].h_samp_factor =
-                 sp->cinfo.d.comp_info[i].v_samp_factor = 1;
-              while (++i < sp->cinfo.d.num_components);
-              break;
-            case JCS_YCbCr    :
-              i = 0;
-              do
-                {
-                  sp->cinfo.d.comp_info[i].component_id = i+1;
-                  sp->cinfo.d.comp_info[i].h_samp_factor =
-                  sp->cinfo.d.comp_info[i].v_samp_factor = 1;
-                  sp->cinfo.d.comp_info[i].quant_tbl_no =
-                  sp->cinfo.d.comp_info[i].dc_tbl_no =
-                  sp->cinfo.d.comp_info[i].ac_tbl_no = i > 0;
-                }
-              while (++i < sp->cinfo.d.num_components);
-              sp->cinfo.d.comp_info[0].h_samp_factor = sp->h_sampling;
-              sp->cinfo.d.comp_info[0].v_samp_factor = sp->v_sampling;
-          };
-        sp->cinfo.d.comps_in_scan = td->td_planarconfig == PLANARCONFIG_CONTIG
-                                  ? sp->cinfo.d.num_components
-                                  : 1;
-        i = CALLJPEG(sp,-1,jpeg_read_header(&sp->cinfo.d,!sp->is_WANG));
-        sp->cinfo.d.marker->read_markers = save; /* Restore input method */
-        if (sp->is_WANG) /* produced by Wang Imaging on Microsoft Windows */
-          {
-            if (i != JPEG_SUSPENDED) return 0;
-
-         /* BOGOSITY ALERT!  Files prooduced by the Wang Imaging application for
-                             Microsoft Windows are a special--and, technically
-            illegal--case.  A JPEG SOS marker and rest of the data stream should
-            be located at the end of the file, in a position identified by the
-            0th Strip offset.
-         */
-            i = td->td_nstrips - 1;
-            sp->src.next_input_byte = tif->tif_base + td->td_stripoffset[0];
-            sp->src.bytes_in_buffer = td->td_stripoffset[i] -
-              td->td_stripoffset[0] + td->td_stripbytecount[i];
-            i = CALLJPEG(sp,-1,jpeg_read_header(&sp->cinfo.d,TRUE));
-          };
-        if (i != JPEG_HEADER_OK) return 0;
-      };
-
- /* Some of our initialization must wait until the JPEG Library is initialized
-    above, in order to override its defaults.
- */
-    if (   (sp->cinfo.d.raw_data_out = downsampled_output)
-        && !alloc_downsampled_buffers(tif,sp->cinfo.d.comp_info,
-                                      sp->cinfo.d.num_components)
-       ) return 0;
-    sp->cinfo.d.jpeg_color_space = jpeg_color_space;
-    sp->cinfo.d.out_color_space = out_color_space;
-    sp->cinfo.d.dither_mode = JDITHER_NONE; /* Reduce image "noise" */
-    sp->cinfo.d.two_pass_quantize = FALSE;
-
- /* If the image consists of separate, discontiguous TIFF "samples" (= color
-    planes, hopefully = JPEG "scans"), then we must use the JPEG Library's
-    "buffered image" mode to decompress the entire image into temporary buffers,
-    because the JPEG Library must parse the entire JPEG bit-stream in order to
-    be satsified that it has a complete set of color components for each pixel,
-    but the TIFF Library must allow our client to extract 1 component at a time.
-    Initializing the JPEG Library's "buffered image" mode is tricky:  First, we
-    start its decompressor, then we tell the decompressor to "consume" (i.e.,
-    buffer) the entire bit-stream.
-
-    WARNING:  Disabling "fancy" up-sampling seems to slightly reduce "noise" for
-              certain old Wang Imaging files, but it absolutely *must* be
-    enabled if the image has separate color planes, since in that case, the JPEG
-    Library doesn't use an "sp->cinfo.d.cconvert" structure (so de-referencing
-    this pointer below will cause a fatal crash) but writing our own code to up-
-    sample separate color planes is too much work for right now.  Maybe someday?
- */
-    sp->cinfo.d.do_fancy_upsampling = /* Always let this default (to TRUE)? */
-    sp->cinfo.d.buffered_image = td->td_planarconfig == PLANARCONFIG_SEPARATE;
-    if (!CALLJPEG(sp,0,jpeg_start_decompress(&sp->cinfo.d))) return 0;
-    if (sp->cinfo.d.buffered_image) /* separate color planes */
-      {
-        if (sp->cinfo.d.raw_data_out)
-          tif->tif_decoderow = tif->tif_decodestrip = tif->tif_decodetile =
-            OJPEGDecodeRawSeparate;
-        else
-          {
-            tif->tif_decoderow = tif->tif_decodestrip = tif->tif_decodetile =
-              OJPEGDecode;
-
-         /* In JPEG Library Version 6B, color-space conversion isn't implemented
-            for separate color planes, so we must do it ourself if our TIFF
-            client doesn't want to:
-         */
-            sp->cinfo.d.cconvert->color_convert =
-              sp->cinfo.d.jpeg_color_space == sp->cinfo.d.out_color_space
-              ? null_convert : ycc_rgb_convert;
-          };
-    L3: switch (CALLJPEG(sp,0,jpeg_consume_input(&sp->cinfo.d)))
-          {
-            default              : goto L3;
-
-         /* If no JPEG "End of Information" (EOI) marker is found when bit-
-            stream parsing ends, check whether we have enough data to proceed
-            before reporting an error.
-         */
-            case JPEG_SUSPENDED  : if (  sp->cinfo.d.input_scan_number
-                                        *sp->cinfo.d.image_height
-                                       + sp->cinfo.d.input_iMCU_row
-                                        *sp->cinfo.d.max_v_samp_factor
-#                                       ifdef D_LOSSLESS_SUPPORTED
-                                        *sp->cinfo.d.data_units_in_MCU
-                                        *sp->cinfo.d.min_codec_data_unit
-#                                       else
-                                        *sp->cinfo.d.blocks_in_MCU
-                                        *DCTSIZE
-#                                       endif
-                                      < td->td_samplesperpixel
-                                       *sp->cinfo.d.image_height
-                                      )
-                                     {
-                                       TIFFError(tif->tif_name,
-                                         "Premature end of JPEG bit-stream");
-                                       return 0;
-                                     }
-            case JPEG_REACHED_EOI: ;
-          }
-      }
-    else /* pixel-interleaved color planes */
-      tif->tif_decoderow = tif->tif_decodestrip = tif->tif_decodetile =
-        downsampled_output ? OJPEGDecodeRawContig : OJPEGDecode;
-    return 1;
-#   undef td
-  }
+#ifndef LIBJPEG_ENCAP_EXTERNAL
+static int
+jpeg_read_header_encap(OJPEGState* sp, jpeg_decompress_struct* cinfo, uint8 require_image)
+{
+       return(SETJMP(sp->exit_jmpbuf)?0:(jpeg_read_header(cinfo,require_image),1));
+}
+#endif
 
+#ifndef LIBJPEG_ENCAP_EXTERNAL
 static int
-OJPEGPreDecode(register TIFF *tif,tsample_t s)
-  { register OJPEGState *sp = OJState(tif);
-#   define td (&tif->tif_dir)
-
- /* If we are about to read the first row of an image plane (hopefully, these
-    are coincident with JPEG "scans"!), reset the JPEG Library's decompressor
-    appropriately.  Otherwise, let the decompressor run "as is" and return a
-    "success" status without further ado.
- */
-    if (     (isTiled(tif) ? tif->tif_curtile : tif->tif_curstrip)
-           % td->td_stripsperimage
-        == 0
-       )
-      {
-        if (   sp->cinfo.d.buffered_image
-            && !CALLJPEG(sp,0,jpeg_start_output(&sp->cinfo.d,s+1))
-           ) return 0;
-        sp->cinfo.d.output_scanline = 0;
-
-     /* Mark subsampling buffers "empty". */
-
-#       ifdef D_LOSSLESS_SUPPORTED
-        sp->scancount = sp->cinfo.d.min_codec_data_unit;
-#       else
-        sp->scancount = DCTSIZE;
-#       endif
-      };
-    return 1;
-#   undef td
-  }
-
-/*ARGSUSED*/ static void
-OJPEGPostDecode(register TIFF *tif,tidata_t buf,tsize_t cc)
-  { register OJPEGState *sp = OJState(tif);
-#   define td (&tif->tif_dir)
-
- /* The JPEG Library decompressor has reached the end of a strip/tile.  If this
-    is the end of a TIFF image "sample" (= JPEG "scan") in a file with separate
-    components (color planes), then end the "scan".  If it ends the image's last
-    sample/scan, then also stop the JPEG Library's decompressor.
- */
-    if (sp->cinfo.d.output_scanline >= sp->cinfo.d.output_height)
-      {
-        if (sp->cinfo.d.buffered_image)
-          CALLJPEG(sp,-1,jpeg_finish_output(&sp->cinfo.d)); /* End JPEG scan */
-        if (   (isTiled(tif) ? tif->tif_curtile : tif->tif_curstrip)
-            >= td->td_nstrips-1
-           ) CALLJPEG(sp,0,jpeg_finish_decompress(&sp->cinfo.d));
-      }
-#   undef td
-  }
+jpeg_start_decompress_encap(OJPEGState* sp, jpeg_decompress_struct* cinfo)
+{
+       return(SETJMP(sp->exit_jmpbuf)?0:(jpeg_start_decompress(cinfo),1));
+}
+#endif
 
+#ifndef LIBJPEG_ENCAP_EXTERNAL
 static int
-OJPEGVSetField(register TIFF *tif,ttag_t tag,va_list ap)
+jpeg_read_scanlines_encap(OJPEGState* sp, jpeg_decompress_struct* cinfo, void* scanlines, uint32 max_lines)
 {
-    uint32 v32;
-    register OJPEGState *sp = OJState(tif);
-#   define td (&tif->tif_dir)
-    toff_t tiffoff=0;
-    uint32 bufoff=0;
-    uint32 code_count=0;
-    int i2=0;
-    int k2=0;
-
-    switch (tag)
-      {
-
-     /* If a "ReferenceBlackWhite" TIFF tag appears in the file explicitly, undo
-        any modified default definition that we might have installed below, then
-        install the real one.
-     */
-        case TIFFTAG_REFERENCEBLACKWHITE   : if (td->td_refblackwhite)
-                                               {
-                                                 _TIFFfree(td->td_refblackwhite);
-                                                 td->td_refblackwhite = 0;
-                                               };
-        default                            : return
-                                               (*sp->vsetparent)(tif,tag,ap);
-
-     /* BEWARE OF KLUDGE:  Some old-format JPEG-in-TIFF files, including those
-                           produced by the Wang Imaging application for Micro-
-        soft Windows, illegally omit a "ReferenceBlackWhite" TIFF tag, even
-        though the TIFF specification's default is intended for the RGB color
-        space and is inappropriate for the YCbCr color space ordinarily used for
-        JPEG images.  Since many TIFF client applications request the value of
-        this tag immediately after a TIFF image directory is parsed, and before
-        any other code in this module receives control, we are forced to fix
-        this problem very early in image-file processing.  Fortunately, legal
-        TIFF files are supposed to store their tags in numeric order, so a
-        mandatory "PhotometricInterpretation" tag should always appear before
-        an optional "ReferenceBlackWhite" tag.  Hence, we slyly peek ahead when
-        we discover the desired photometry, by installing modified black and
-        white reference levels.
-     */
-        case TIFFTAG_PHOTOMETRIC           :
-          if (   (v32 = (*sp->vsetparent)(tif,tag,ap))
-              && td->td_photometric == PHOTOMETRIC_YCBCR
-             )
-         {
-            if ( (td->td_refblackwhite = _TIFFmalloc(6*sizeof(float))) )
-              { register long top = 1 << td->td_bitspersample;
-
-                td->td_refblackwhite[0] = 0;
-                td->td_refblackwhite[1] = td->td_refblackwhite[3] =
-                td->td_refblackwhite[5] = top - 1;
-                td->td_refblackwhite[2] = td->td_refblackwhite[4] = top >> 1;
-              }
-            else
-              {
-                TIFFError(tif->tif_name,
-                  "Cannot set default reference black and white levels");
-                v32 = 0;
-              };
-         }
-          return v32;
-
-     /* BEWARE OF KLUDGE:  According to Charles Auer <Bumble731@msn.com>, if our
-                           input is a multi-image (multi-directory) JPEG-in-TIFF
-        file is produced by the Wang Imaging application on Microsoft Windows,
-        for some reason the first directory excludes the vendor-specific "WANG
-        PageControl" tag (32934) that we check below, so the only other way to
-        identify these directories is apparently to look for a software-
-        identification tag with the substring, "Wang Labs".  Single-image files
-        can apparently pass both tests, which causes no harm here, but what a
-        mess this is!
-     */
-        case TIFFTAG_SOFTWARE              :
-        {
-            char *software;
-
-            v32 = (*sp->vsetparent)(tif,tag,ap);
-            if( TIFFGetField( tif, TIFFTAG_SOFTWARE, &software )
-                && strstr( software, "Wang Labs" ) )
-                sp->is_WANG = 1;
-            return v32;
-        }
-
-        case TIFFTAG_JPEGPROC              :
-        case TIFFTAG_JPEGIFOFFSET          :
-        case TIFFTAG_JPEGIFBYTECOUNT       :
-        case TIFFTAG_JPEGRESTARTINTERVAL   :
-        case TIFFTAG_JPEGLOSSLESSPREDICTORS:
-        case TIFFTAG_JPEGPOINTTRANSFORM    :
-        case TIFFTAG_JPEGQTABLES           :
-        case TIFFTAG_JPEGDCTABLES          :
-        case TIFFTAG_JPEGACTABLES          :
-        case TIFFTAG_WANG_PAGECONTROL      :
-        case TIFFTAG_JPEGCOLORMODE         : ;
-      };
-    v32 = va_arg(ap,uint32); /* No. of values in this TIFF record */
-
-    /* This switch statement is added for OJPEGVSetField */
-    if(v32 !=0){
-        switch(tag){
-            case TIFFTAG_JPEGPROC:
-                sp->jpegproc=v32;
-                break;
-            case TIFFTAG_JPEGIFOFFSET:
-                sp->jpegifoffset=v32;
-               break;
-            case TIFFTAG_JPEGIFBYTECOUNT:
-               sp->jpegifbytecount=v32;
-               break;
-            case TIFFTAG_JPEGRESTARTINTERVAL:
-               sp->jpegrestartinterval=v32;
-               break;
-            case TIFFTAG_JPEGLOSSLESSPREDICTORS:
-               sp->jpeglosslesspredictors_length=v32;
-               break;
-            case TIFFTAG_JPEGPOINTTRANSFORM:
-               sp->jpegpointtransform_length=v32;
-               break;
-            case TIFFTAG_JPEGQTABLES:
-               sp->jpegqtables_length=v32;
-               break;
-            case TIFFTAG_JPEGACTABLES:
-               sp->jpegactables_length=v32;
-               break;
-            case TIFFTAG_JPEGDCTABLES:
-               sp->jpegdctables_length=v32;
-               break;
-            default:
-               break;
-        }
-    }
-
- /* BEWARE:  The following actions apply only if we are reading a "source" TIFF
-             image to be decompressed for a client application program.  If we
-    ever enhance this file's CODEC to write "destination" JPEG-in-TIFF images,
-    we'll need an "if"- and another "switch"-statement below, because we'll
-    probably want to store these records' values in some different places.  Most
-    of these need not be parsed here in order to decode JPEG bit stream, so we
-    set boolean flags to note that they have been seen, but we otherwise ignore
-    them.
- */
-    switch (tag)
-      { JHUFF_TBL **h;
-
-     /* Validate the JPEG-process code. */
-
-        case TIFFTAG_JPEGPROC              :
-          switch (v32)
-            {
-              default               : TIFFError(tif->tif_name,
-                                        "Unknown JPEG process");
-                                      return 0;
-#             ifdef C_LOSSLESS_SUPPORTED
-
-           /* Image uses (lossy) baseline sequential coding. */
-
-              case JPEGPROC_BASELINE: sp->cinfo.d.process = JPROC_SEQUENTIAL;
-                                      sp->cinfo.d.data_unit = DCTSIZE;
-                                      break;
-
-           /* Image uses (lossless) Huffman coding. */
-
-              case JPEGPROC_LOSSLESS: sp->cinfo.d.process = JPROC_LOSSLESS;
-                                      sp->cinfo.d.data_unit = 1;
-#             else /* not C_LOSSLESS_SUPPORTED */
-              case JPEGPROC_LOSSLESS: TIFFError(JPEGLib_name,
-                                        "Does not support lossless Huffman coding");
-                                      return 0;
-              case JPEGPROC_BASELINE: ;
-#             endif /* C_LOSSLESS_SUPPORTED */
-            };
-          break;
-
-     /* The TIFF Version 6.0 specification says that if the value of a TIFF
-        "JPEGInterchangeFormat" record is 0, then we are to behave as if this
-        record were absent; i.e., the data does *not* represent a JPEG Inter-
-        change Format File (JFIF), so don't even set the boolean "I've been
-        here" flag below.  Otherwise, the field's value represents the file
-        offset of the JPEG SOI marker.
-     */
-        case TIFFTAG_JPEGIFOFFSET          :
-          if (v32)
-            {
-              sp->src.next_input_byte = tif->tif_base + v32;
-              break;
-            };
-          return 1;
-        case TIFFTAG_JPEGIFBYTECOUNT       :
-          sp->src.bytes_in_buffer = v32;
-          break;
-
-     /* The TIFF Version 6.0 specification says that if the JPEG "Restart"
-        marker interval is 0, then the data has no "Restart" markers; i.e., we
-        must behave as if this TIFF record were absent.  So, don't even set the
-        boolean "I've been here" flag below.
-     */
-     /*
-      * Instead, set the field bit so TIFFGetField can get whether or not
-      * it was set.
-      */
-        case TIFFTAG_JPEGRESTARTINTERVAL   :
-          if (v32)
-              sp->cinfo.d.restart_interval = v32;
-              break;
-     /* The TIFF Version 6.0 specification says that this tag is supposed to be
-        a vector containing a value for each image component, but for lossless
-        Huffman coding (the only JPEG process defined by the specification for
-        which this tag should be needed), ISO IS 10918-1 uses only a single
-        value, equivalent to the "Ss" field in a JPEG bit-stream's "Start of
-        Scan" (SOS) marker.  So, we extract the first vector element and ignore
-        the rest.  (I hope this is correct!)
-     */
-        case TIFFTAG_JPEGLOSSLESSPREDICTORS:
-           if (v32)
-             {
-               sp->cinfo.d.Ss = *va_arg(ap,uint16 *);
-               sp->jpeglosslesspredictors = 
-                   _TIFFmalloc(sp->jpeglosslesspredictors_length
-                               * sizeof(uint16));
-               if(sp->jpeglosslesspredictors==NULL){return(0);}
-               for(i2=0;i2<sp->jpeglosslesspredictors_length;i2++){
-                ((uint16*)sp->jpeglosslesspredictors)[i2] =
-                       ((uint16*)sp->cinfo.d.Ss)[i2];
-               }
-               sp->jpeglosslesspredictors_length*=sizeof(uint16);
-               break;
-             };
-           return v32;
-
-     /* The TIFF Version 6.0 specification says that this tag is supposed to be
-        a vector containing a value for each image component, but for lossless
-        Huffman coding (the only JPEG process defined by the specification for
-        which this tag should be needed), ISO IS 10918-1 uses only a single
-        value, equivalent to the "Al" field in a JPEG bit-stream's "Start of
-        Scan" (SOS) marker.  So, we extract the first vector element and ignore
-        the rest.  (I hope this is correct!)
-     */
-        case TIFFTAG_JPEGPOINTTRANSFORM    :
-           if (v32)
-             {
-               sp->cinfo.d.Al = *va_arg(ap,uint16 *);
-               sp->jpegpointtransform =
-                   _TIFFmalloc(sp->jpegpointtransform_length*sizeof(uint16));
-               if(sp->jpegpointtransform==NULL){return(0);}
-               for(i2=0;i2<sp->jpegpointtransform_length;i2++) {
-                ((uint16*)sp->jpegpointtransform)[i2] =
-                       ((uint16*)sp->cinfo.d.Al)[i2];
-               }
-               sp->jpegpointtransform_length*=sizeof(uint16);
-               break;
-             }
-           return v32;
-
-     /* We have a vector of offsets to quantization tables, so load 'em! */
-
-        case TIFFTAG_JPEGQTABLES           :
-          if (v32)
-            { uint32 *v;
-              int i;
-              if (v32 > NUM_QUANT_TBLS)
-                {
-                  TIFFError(tif->tif_name,"Too many quantization tables");
-                  return 0;
-                };
-              i = 0;
-              v = va_arg(ap,uint32 *);
-                sp->jpegqtables=_TIFFmalloc(64*sp->jpegqtables_length);
-                if(sp->jpegqtables==NULL){return(0);}
-                tiffoff = TIFFSeekFile(tif, 0, SEEK_CUR);
-                bufoff=0;
-                for(i2=0;i2<sp->jpegqtables_length;i2++){
-                    TIFFSeekFile(tif, v[i2], SEEK_SET);
-                    TIFFReadFile(tif, &(((unsigned char*)(sp->jpegqtables))[bufoff]),
-                                64);
-                    bufoff+=64;
-                }
-                sp->jpegqtables_length=bufoff;
-                TIFFSeekFile(tif, tiffoff, SEEK_SET);
-
-              do /* read quantization table */
-                { register UINT8 *from = tif->tif_base + *v++;
-                  register UINT16 *to;
-                  register int j = DCTSIZE2;
-
-                  if (!( sp->cinfo.d.quant_tbl_ptrs[i]
-                       = CALLJPEG(sp,0,jpeg_alloc_quant_table(&sp->cinfo.comm))
-                       )
-                     )
-                    {
-                      TIFFError(JPEGLib_name,"No space for quantization table");
-                      return 0;
-                    };
-                  to = sp->cinfo.d.quant_tbl_ptrs[i]->quantval;
-                  do *to++ = *from++; while (--j > 0);
-                }
-              while (++i < v32);
-              sp->jpegtablesmode |= JPEGTABLESMODE_QUANT;
-            };
-          break;
-
-     /* We have a vector of offsets to DC Huffman tables, so load 'em! */
-
-        case TIFFTAG_JPEGDCTABLES          :
-          h = sp->cinfo.d.dc_huff_tbl_ptrs;
-          goto L;
-
-     /* We have a vector of offsets to AC Huffman tables, so load 'em! */
-
-        case TIFFTAG_JPEGACTABLES          :
-          h = sp->cinfo.d.ac_huff_tbl_ptrs;
-       L: if (v32)
-            { uint32 *v;
-              int i;
-              if (v32 > NUM_HUFF_TBLS)
-                {
-                  TIFFError(tif->tif_name,"Too many Huffman tables");
-                  return 0;
-                };
-              v = va_arg(ap,uint32 *);
-                if(tag == TIFFTAG_JPEGDCTABLES) {
-                    sp->jpegdctables=_TIFFmalloc(272*sp->jpegdctables_length);
-                    if(sp->jpegdctables==NULL){return(0);}
-                    tiffoff = TIFFSeekFile(tif, 0, SEEK_CUR);
-                    bufoff=0;
-                    code_count=0;                
-                    for(i2=0;i2<sp->jpegdctables_length;i2++){
-                        TIFFSeekFile(tif, v[i2], SEEK_SET);
-                        TIFFReadFile(tif,
-                                    &(((unsigned char*)(sp->jpegdctables))[bufoff]),
-                                    16);
-                        code_count=0;
-                        for(k2=0;k2<16;k2++){
-                            code_count+=((unsigned char*)(sp->jpegdctables))[k2+bufoff];
-                        }
-                        TIFFReadFile(tif,
-                                    &(((unsigned char*)(sp->jpegdctables))[bufoff+16]),
-                                    code_count);
-                        bufoff+=16;
-                        bufoff+=code_count;
-                    }
-                    sp->jpegdctables_length=bufoff;
-                    TIFFSeekFile(tif, tiffoff, SEEK_SET);
-                }
-                if(tag==TIFFTAG_JPEGACTABLES){
-                    sp->jpegactables=_TIFFmalloc(272*sp->jpegactables_length);
-                    if(sp->jpegactables==NULL){return(0);}
-                    tiffoff = TIFFSeekFile(tif, 0, SEEK_CUR);
-                    bufoff=0;
-                    code_count=0;                
-                    for(i2=0;i2<sp->jpegactables_length;i2++){
-                        TIFFSeekFile(tif, v[i2], SEEK_SET);
-                        TIFFReadFile(tif, &(((unsigned char*)(sp->jpegactables))[bufoff]), 16);
-                        code_count=0;
-                        for(k2=0;k2<16;k2++){
-                            code_count+=((unsigned char*)(sp->jpegactables))[k2+bufoff];
-                        }
-                        TIFFReadFile(tif, &(((unsigned char*)(sp->jpegactables))[bufoff+16]), code_count);
-                        bufoff+=16;
-                        bufoff+=code_count;
-                    }
-                    sp->jpegactables_length=bufoff;
-                    TIFFSeekFile(tif, tiffoff, SEEK_SET);
-                }
-              i = 0;
-              do /* copy each Huffman table */
-                { int size = 0;
-                  register UINT8 *from = tif->tif_base + *v++, *to;
-                  register int j = sizeof (*h)->bits;
-
-               /* WARNING:  This code relies on the fact that an image file not
-                            "memory mapped" was read entirely into a single
-                  buffer by "TIFFInitOJPEG()", so we can do a fast memory-to-
-                  memory copy here.  Each table consists of 16 Bytes, which are
-                  suffixed to a 0 Byte when copied, followed by a variable
-                  number of Bytes whose length is the sum of the first 16.
-               */
-                  if (!( *h
-                       = CALLJPEG(sp,0,jpeg_alloc_huff_table(&sp->cinfo.comm))
-                       )
-                     )
-                    {
-                      TIFFError(JPEGLib_name,"No space for Huffman table");
-                      return 0;
-                    };
-                  to = (*h++)->bits;
-                  *to++ = 0;
-                  while (--j > 0) size += *to++ = *from++; /* Copy 16 Bytes */
-                  if (size > sizeof (*h)->huffval/sizeof *(*h)->huffval)
-                    {
-                      TIFFError(tif->tif_name,"Huffman table too big");
-                      return 0;
-                    };
-                  if ((j = size) > 0) do *to++ = *from++; while (--j > 0);
-                  while (++size <= sizeof (*h)->huffval/sizeof *(*h)->huffval)
-                    *to++ = 0; /* Zero the rest of the table for cleanliness */
-                }
-              while (++i < v32);
-              sp->jpegtablesmode |= JPEGTABLESMODE_HUFF;
-            };
-          break;
-
-     /* The following vendor-specific TIFF tag occurs in (highly illegal) files
-        produced by the Wang Imaging application for Microsoft Windows.  These
-        can apparently have several "pages", in which case this tag specifies
-        the offset of a "page control" structure, which we don't currently know
-        how to handle.  0 indicates a 1-page image with no "page control", which
-        we make a feeble effort to handle.
-     */
-        case TIFFTAG_WANG_PAGECONTROL      :
-          if (v32 == 0) v32 = -1;
-          sp->is_WANG = v32;
-          tag = TIFFTAG_JPEGPROC+FIELD_WANG_PAGECONTROL-FIELD_JPEGPROC;
-          break;
-
-     /* This pseudo tag indicates whether our caller is expected to do YCbCr <->
-        RGB color-space conversion (JPEGCOLORMODE_RAW <=> 0) or whether we must
-        ask the JPEG Library to do it (JPEGCOLORMODE_RGB <=> 1).
-     */
-        case TIFFTAG_JPEGCOLORMODE         :
-          sp->jpegcolormode = v32;
-
-       /* Mark the image to indicate whether returned data is up-sampled, so
-          that "TIFF{Strip,Tile}Size()" reflect the true amount of data present.
-       */
-          v32 = tif->tif_flags; /* Save flags temporarily */
-          tif->tif_flags &= ~TIFF_UPSAMPLED;
-          if (   td->td_photometric == PHOTOMETRIC_YCBCR
-              &&    (td->td_ycbcrsubsampling[0]<<3 | td->td_ycbcrsubsampling[1])
-                 != 011
-              && sp->jpegcolormode == JPEGCOLORMODE_RGB
-             ) tif->tif_flags |= TIFF_UPSAMPLED;
-
-       /* If the up-sampling state changed, re-calculate tile size. */
-
-          if ((tif->tif_flags ^ v32) & TIFF_UPSAMPLED)
-            {
-              tif->tif_tilesize = isTiled(tif) ? TIFFTileSize(tif) : (tsize_t) -1;
-              tif->tif_flags |= TIFF_DIRTYDIRECT;
-            };
-          return 1;
-      };
-    TIFFSetFieldBit(tif,tag-TIFFTAG_JPEGPROC+FIELD_JPEGPROC);
-    return 1;
-#   undef td
-  }
+       return(SETJMP(sp->exit_jmpbuf)?0:(jpeg_read_scanlines(cinfo,scanlines,max_lines),1));
+}
+#endif
 
+#ifndef LIBJPEG_ENCAP_EXTERNAL
 static int
-OJPEGVGetField(register TIFF *tif,ttag_t tag,va_list ap)
-  { register OJPEGState *sp = OJState(tif);
-
-    switch (tag)
-      {
-
-     /* If this file has managed to synthesize a set of consolidated "metadata"
-        tables for the current (post-TIFF Version 6.0 specification) JPEG-in-
-        TIFF encapsulation strategy, then tell our caller about them; otherwise,
-        keep mum.
-     */
-        case TIFFTAG_JPEGTABLES            :
-          if (sp->jpegtables_length) /* we have "new"-style JPEG tables */
-            {
-              *va_arg(ap,uint32 *) = sp->jpegtables_length;
-              *va_arg(ap,char **) = sp->jpegtables;
-              return 1;
-            };
-
-     /* This pseudo tag indicates whether our caller is expected to do YCbCr <->
-        RGB color-space conversion (JPEGCOLORMODE_RAW <=> 0) or whether we must
-        ask the JPEG Library to do it (JPEGCOLORMODE_RGB <=> 1).
-     */
-        case TIFFTAG_JPEGCOLORMODE         :
-          *va_arg(ap,uint32 *) = sp->jpegcolormode;
-          return 1;
-
-     /* The following tags are defined by the TIFF Version 6.0 specification
-        and are obsolete.  If our caller asks for information about them, do not
-        return anything, even if we parsed them in an old-format "source" image.
-     */
-        case TIFFTAG_JPEGPROC              :
-               *va_arg(ap, uint16*)=sp->jpegproc;
-               return(1);
-               break;
-        case TIFFTAG_JPEGIFOFFSET          :
-               *va_arg(ap, uint32*)=sp->jpegifoffset;
-               return(1);
-               break;
-        case TIFFTAG_JPEGIFBYTECOUNT       :
-               *va_arg(ap, uint32*)=sp->jpegifbytecount;
-               return(1);
-               break;
-        case TIFFTAG_JPEGRESTARTINTERVAL   :
-               *va_arg(ap, uint32*)=sp->jpegrestartinterval;
-               return(1);
-               break;
-        case TIFFTAG_JPEGLOSSLESSPREDICTORS:
-                *va_arg(ap, uint32*)=sp->jpeglosslesspredictors_length;
-                *va_arg(ap, void**)=sp->jpeglosslesspredictors;
-                return(1);
-                break;
-        case TIFFTAG_JPEGPOINTTRANSFORM    :
-                *va_arg(ap, uint32*)=sp->jpegpointtransform_length;
-                *va_arg(ap, void**)=sp->jpegpointtransform;
-                return(1);
-                break;
-        case TIFFTAG_JPEGQTABLES           :
-                *va_arg(ap, uint32*)=sp->jpegqtables_length;
-                *va_arg(ap, void**)=sp->jpegqtables;
-                return(1);
-                break;
-        case TIFFTAG_JPEGDCTABLES          :
-                *va_arg(ap, uint32*)=sp->jpegdctables_length;
-                *va_arg(ap, void**)=sp->jpegdctables;
-                return(1);
-                break;
-        case TIFFTAG_JPEGACTABLES          : 
-                *va_arg(ap, uint32*)=sp->jpegactables_length;
-                *va_arg(ap, void**)=sp->jpegactables;
-                return(1);
-                break;
-      };
-    return (*sp->vgetparent)(tif,tag,ap);
-  }
+jpeg_read_raw_data_encap(OJPEGState* sp, jpeg_decompress_struct* cinfo, void* data, uint32 max_lines)
+{
+       return(SETJMP(sp->exit_jmpbuf)?0:(jpeg_read_raw_data(cinfo,data,max_lines),1));
+}
+#endif
 
+#ifndef LIBJPEG_ENCAP_EXTERNAL
 static void
-OJPEGPrintDir(register TIFF *tif,FILE *fd,long flags)
-  { register OJPEGState *sp = OJState(tif);
-
-    if (   ( flags
-           & (TIFFPRINT_JPEGQTABLES|TIFFPRINT_JPEGDCTABLES|TIFFPRINT_JPEGACTABLES)
-           )
-        && sp->jpegtables_length
-       )
-      fprintf(fd,"  JPEG Table Data: <present>, %lu bytes\n",
-        sp->jpegtables_length);
-  }
-
-static uint32
-OJPEGDefaultStripSize(register TIFF *tif,register uint32 s)
-  { register OJPEGState *sp = OJState(tif);
-#   define td (&tif->tif_dir)
-
-    if ((s = (*sp->defsparent)(tif,s)) < td->td_imagelength)
-      { register tsize_t size = sp->cinfo.comm.is_decompressor
-#                             ifdef D_LOSSLESS_SUPPORTED
-                              ? sp->cinfo.d.min_codec_data_unit
-#                             else
-                              ? DCTSIZE
-#                             endif
-#                             ifdef C_LOSSLESS_SUPPORTED
-                              : sp->cinfo.c.data_unit;
-#                             else
-                              : DCTSIZE;
-#                             endif
-
-        size = TIFFroundup(size,16);
-        s = TIFFroundup(s,td->td_ycbcrsubsampling[1]*size);
-      };
-    return s;
-#   undef td
-  }
+jpeg_encap_unwind(TIFF* tif)
+{
+       OJPEGState* sp=(OJPEGState*)tif->tif_data;
+       LONGJMP(sp->exit_jmpbuf,1);
+}
+#endif
 
 static void
-OJPEGDefaultTileSize(register TIFF *tif,register uint32 *tw,register uint32 *th)
-  { register OJPEGState *sp = OJState(tif);
-    register tsize_t size;
-#   define td (&tif->tif_dir)
-
-    size = sp->cinfo.comm.is_decompressor
-#        ifdef D_LOSSLESS_SUPPORTED
-         ? sp->cinfo.d.min_codec_data_unit
-#        else
-         ? DCTSIZE
-#        endif
-#        ifdef C_LOSSLESS_SUPPORTED
-         : sp->cinfo.c.data_unit;
-#        else
-         : DCTSIZE;
-#        endif
-    size = TIFFroundup(size,16);
-    (*sp->deftparent)(tif,tw,th);
-    *tw = TIFFroundup(*tw,td->td_ycbcrsubsampling[0]*size);
-    *th = TIFFroundup(*th,td->td_ycbcrsubsampling[1]*size);
-#   undef td
-  }
+OJPEGLibjpegJpegErrorMgrOutputMessage(jpeg_common_struct* cinfo)
+{
+       char buffer[JMSG_LENGTH_MAX];
+       (*cinfo->err->format_message)(cinfo,buffer);
+       TIFFWarningExt(((TIFF*)(cinfo->client_data))->tif_clientdata,"LibJpeg", "%s", buffer);
+}
 
 static void
-OJPEGCleanUp(register TIFF *tif)
-  { register OJPEGState *sp;
-
-    if ( (sp = OJState(tif)) )
-      {
-        CALLVJPEG(sp,jpeg_destroy(&sp->cinfo.comm)); /* Free JPEG Lib. vars. */
-        if (sp->jpegtables) {_TIFFfree(sp->jpegtables);sp->jpegtables=0;}
-        if (sp->jpeglosslesspredictors) {
-               _TIFFfree(sp->jpeglosslesspredictors);
-               sp->jpeglosslesspredictors = 0;
-       }
-        if (sp->jpegpointtransform) {
-               _TIFFfree(sp->jpegpointtransform);
-               sp->jpegpointtransform=0;
-       }
-        if (sp->jpegqtables) {_TIFFfree(sp->jpegqtables);sp->jpegqtables=0;}
-        if (sp->jpegactables) {_TIFFfree(sp->jpegactables);sp->jpegactables=0;}
-        if (sp->jpegdctables) {_TIFFfree(sp->jpegdctables);sp->jpegdctables=0;}
-     /* If the image file isn't "memory mapped" and we read it all into a
-        single, large memory buffer, free the buffer now.
-     */
-        if (!isMapped(tif) && tif->tif_base) /* free whole-file buffer */
-          {
-            _TIFFfree(tif->tif_base);
-            tif->tif_base = 0;
-            tif->tif_size = 0;
-          };
-        _TIFFfree(sp); /* Release local variables */
-        tif->tif_data = 0;
-      }
-  }
+OJPEGLibjpegJpegErrorMgrErrorExit(jpeg_common_struct* cinfo)
+{
+       char buffer[JMSG_LENGTH_MAX];
+       (*cinfo->err->format_message)(cinfo,buffer);
+       TIFFErrorExt(((TIFF*)(cinfo->client_data))->tif_clientdata,"LibJpeg", "%s", buffer);
+       jpeg_encap_unwind((TIFF*)(cinfo->client_data));
+}
+
+static void
+OJPEGLibjpegJpegSourceMgrInitSource(jpeg_decompress_struct* cinfo)
+{
+       (void)cinfo;
+}
+
+static boolean
+OJPEGLibjpegJpegSourceMgrFillInputBuffer(jpeg_decompress_struct* cinfo)
+{
+       TIFF* tif=(TIFF*)cinfo->client_data;
+       OJPEGState* sp=(OJPEGState*)tif->tif_data;
+       void* mem=0;
+       uint32 len=0;
+       if (OJPEGWriteStream(tif,&mem,&len)==0)
+       {
+               TIFFErrorExt(tif->tif_clientdata,"LibJpeg","Premature end of JPEG data");
+               jpeg_encap_unwind(tif);
+       }
+       sp->libjpeg_jpeg_source_mgr.bytes_in_buffer=len;
+       sp->libjpeg_jpeg_source_mgr.next_input_byte=mem;
+       return(1);
+}
+
+static void
+OJPEGLibjpegJpegSourceMgrSkipInputData(jpeg_decompress_struct* cinfo, long num_bytes)
+{
+       TIFF* tif=(TIFF*)cinfo->client_data;
+       (void)num_bytes;
+       TIFFErrorExt(tif->tif_clientdata,"LibJpeg","Unexpected error");
+       jpeg_encap_unwind(tif);
+}
+
+static boolean
+OJPEGLibjpegJpegSourceMgrResyncToRestart(jpeg_decompress_struct* cinfo, int desired)
+{
+       TIFF* tif=(TIFF*)cinfo->client_data;
+       (void)desired;
+       TIFFErrorExt(tif->tif_clientdata,"LibJpeg","Unexpected error");
+       jpeg_encap_unwind(tif);
+       return(0);
+}
+
+static void
+OJPEGLibjpegJpegSourceMgrTermSource(jpeg_decompress_struct* cinfo)
+{
+       (void)cinfo;
+}
+
+#endif
 
-int
-TIFFInitOJPEG(register TIFF *tif,int scheme)
-  { register OJPEGState *sp;
-#   define td (&tif->tif_dir)
-#   ifndef never
-
- /* This module supports a decompression-only CODEC, which is intended strictly
-    for viewing old image files using the obsolete JPEG-in-TIFF encapsulation
-    specified by the TIFF Version 6.0 specification.  It does not, and never
-    should, support compression for new images.  If a client application asks us
-    to, refuse and complain loudly!
- */
-    if (tif->tif_mode != O_RDONLY) return _notSupported(tif);
-#   endif /* never */
-    if (!isMapped(tif))
-      {
-
-     /* BEWARE OF KLUDGE:  If our host operating-system doesn't let an image
-                           file be "memory mapped", then we want to read the
-        entire file into a single (possibly large) memory buffer as if it had
-        been "memory mapped".  Although this is likely to waste space, because
-        analysis of the file's content might cause parts of it to be read into
-        smaller buffers duplicatively, it appears to be the lesser of several
-        evils.  Very old JPEG-in-TIFF encapsulations aren't guaranteed to be
-        JFIF bit streams, or to have a TIFF "JPEGTables" record or much other
-        "metadata" to help us locate the decoding tables and entropy-coded data,
-        so we're likely do a lot of random-access grokking around, and we must
-        ultimately tell the JPEG Library to sequentially scan much of the file
-        anyway.  This is all likely to be easier if we use "brute force" to
-        read the entire file, once, and don't use incremental disc I/O.  If our
-        client application tries to process a file so big that we can't buffer
-        it entirely, then tough shit: we'll give up and exit!
-     */
-        if (!(tif->tif_base = _TIFFmalloc(tif->tif_size=TIFFGetFileSize(tif))))
-          {
-            TIFFError(tif->tif_name,"Cannot allocate file buffer");
-            return 0;
-          };
-        if (!SeekOK(tif,0) || !ReadOK(tif,tif->tif_base,tif->tif_size))
-          {
-            TIFFError(tif->tif_name,"Cannot read file");
-            return 0;
-          }
-      };
-
- /* Allocate storage for this module's per-file variables. */
-
-    if (!(tif->tif_data = (tidata_t)_TIFFmalloc(sizeof *sp)))
-      {
-        TIFFError("TIFFInitOJPEG","No space for JPEG state block");
-        return 0;
-      };
-    (sp = OJState(tif))->tif = tif; /* Initialize reverse pointer */
-    sp->cinfo.d.err = jpeg_std_error(&sp->err); /* Initialize error handling */
-    sp->err.error_exit = TIFFojpeg_error_exit;
-    sp->err.output_message = TIFFojpeg_output_message;
-    if (!CALLVJPEG(sp,jpeg_create_decompress(&sp->cinfo.d))) return 0;
-
- /* Install CODEC-specific tag information and override default TIFF Library
-    "method" subroutines with our own, CODEC-specific methods.  Like all good
-    members of an object-class, we save some of these subroutine pointers for
-    "fall back" in case our own methods fail.
- */
-    _TIFFMergeFieldInfo(tif,ojpegFieldInfo,
-      sizeof ojpegFieldInfo/sizeof *ojpegFieldInfo);
-    sp->defsparent = tif->tif_defstripsize;
-    sp->deftparent = tif->tif_deftilesize;
-    sp->vgetparent = tif->tif_tagmethods.vgetfield;
-    sp->vsetparent = tif->tif_tagmethods.vsetfield;
-    tif->tif_defstripsize = OJPEGDefaultStripSize;
-    tif->tif_deftilesize = OJPEGDefaultTileSize;
-    tif->tif_tagmethods.vgetfield = OJPEGVGetField;
-    tif->tif_tagmethods.vsetfield = OJPEGVSetField;
-    tif->tif_tagmethods.printdir = OJPEGPrintDir;
-#   ifdef never
-    tif->tif_setupencode = OJPEGSetupEncode;
-    tif->tif_preencode = OJPEGPreEncode;
-    tif->tif_postencode = OJPEGPostEncode;
-#   else /* well, hardly ever */
-    tif->tif_setupencode = tif->tif_postencode = _notSupported;
-    tif->tif_preencode = (TIFFPreMethod)_notSupported;
-#   endif /* never */
-    tif->tif_setupdecode = OJPEGSetupDecode;
-    tif->tif_predecode = OJPEGPreDecode;
-    tif->tif_postdecode = OJPEGPostDecode;
-    tif->tif_cleanup = OJPEGCleanUp;
-
- /* If the image file doesn't have "JPEGInterchangeFormat[Length]" TIFF records
-    to guide us, we have few clues about where its encapsulated JPEG bit stream
-    is located, so establish intelligent defaults:  If the Image File Directory
-    doesn't immediately follow the TIFF header, assume that the JPEG data lies
-    in between; otherwise, assume that it follows the Image File Directory.
- */
-    if (tif->tif_header.tiff_diroff > sizeof tif->tif_header)
-      {
-        sp->src.next_input_byte = tif->tif_base + sizeof tif->tif_header;
-        sp->src.bytes_in_buffer = tif->tif_header.tiff_diroff
-                                - sizeof tif->tif_header;
-      }
-    else /* this case is ugly! */
-      { uint32 maxoffset = tif->tif_size;
-        uint16 dircount;
-
-     /* Calculate the offset to the next Image File Directory, if there is one,
-        or to the end of the file, if not.  Then arrange to read the file from
-        the end of the Image File Directory to that offset.
-     */
-        if (tif->tif_nextdiroff) maxoffset = tif->tif_nextdiroff; /* Not EOF */
-        _TIFFmemcpy(&dircount,(const tdata_t)
-          (sp->src.next_input_byte = tif->tif_base+tif->tif_header.tiff_diroff),
-          sizeof dircount);
-        if (tif->tif_flags & TIFF_SWAB) TIFFSwabShort(&dircount);
-        sp->src.next_input_byte += dircount*sizeof(TIFFDirEntry)
-                                + sizeof maxoffset + sizeof dircount;
-        sp->src.bytes_in_buffer = tif->tif_base - sp->src.next_input_byte
-                                + maxoffset;
-      };
-
- /* IJG JPEG Library Version 6B can be configured for either 8- or 12-bit sample
-    precision, but we assume that "old JPEG" TIFF clients only need 8 bits.
- */
-    sp->cinfo.d.data_precision = 8;
-#   ifdef C_LOSSLESS_SUPPORTED
 
- /* If the "JPEGProc" TIFF tag is missing from the Image File Dictionary, the
-    JPEG Library will use its (lossy) baseline sequential process by default.
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
  */
-    sp->cinfo.d.data_unit = DCTSIZE;
-#   endif /* C_LOSSLESS_SUPPORTED */
-
- /* Initialize other CODEC-specific variables requiring default values. */
-
-    tif->tif_flags |= TIFF_NOBITREV; /* No bit-reversal within data bytes */
-    sp->h_sampling = sp->v_sampling = 1; /* No subsampling by default */
-    sp->is_WANG = 0; /* Assume not a MS Windows Wang Imaging file by default */
-    sp->jpegtables = 0; /* No "new"-style JPEG tables synthesized yet */
-    sp->jpegtables_length = 0;
-    sp->jpegquality = 75; /* Default IJG quality */
-    sp->jpegcolormode = JPEGCOLORMODE_RAW;
-    sp->jpegtablesmode = 0; /* No tables found yet */
-    sp->jpeglosslesspredictors=0;
-    sp->jpeglosslesspredictors_length=0;
-    sp->jpegpointtransform=0;
-    sp->jpegpointtransform_length=0;
-    sp->jpegqtables=0;
-    sp->jpegqtables_length=0;
-    sp->jpegdctables=0;
-    sp->jpegdctables_length=0;
-    sp->jpegactables=0;
-    sp->jpegactables_length=0;
-    return 1;
-#   undef td
-  }
-#endif /* OJPEG_SUPPORT */
-
-/* vim: set ts=8 sts=8 sw=8 noet: */
index 13160ec..3b3b2ce 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: tif_open.c,v 1.1 2005-06-17 13:54:52 vp153 Exp $ */
+/* $Id: tif_open.c,v 1.33.2.1 2010-06-08 18:50:42 bfriesen Exp $ */
 
 /*
  * Copyright (c) 1988-1997 Sam Leffler
@@ -29,8 +29,6 @@
  */
 #include "tiffiop.h"
 
-void _TIFFSetDefaultCompressionState(TIFF* tif);
-
 static const long typemask[13] = {
        (long)0L,               /* TIFF_NOTYPE */
        (long)0x000000ffL,      /* TIFF_BYTE */
@@ -83,12 +81,14 @@ static const int litTypeshift[13] = {
 static int
 _tiffDummyMapProc(thandle_t fd, tdata_t* pbase, toff_t* psize)
 {
+       (void) fd; (void) pbase; (void) psize;
        return (0);
 }
 
 static void
 _tiffDummyUnmapProc(thandle_t fd, tdata_t base, toff_t size)
 {
+       (void) fd; (void) base; (void) size;
 }
 
 /*
@@ -97,17 +97,19 @@ _tiffDummyUnmapProc(thandle_t fd, tdata_t base, toff_t size)
  * contents and the machine architecture.
  */
 static void
-TIFFInitOrder(TIFF* tif, int magic, int bigendian)
+TIFFInitOrder(TIFF* tif, int magic)
 {
        tif->tif_typemask = typemask;
        if (magic == TIFF_BIGENDIAN) {
                tif->tif_typeshift = bigTypeshift;
-               if (!bigendian)
-                       tif->tif_flags |= TIFF_SWAB;
+#ifndef WORDS_BIGENDIAN
+               tif->tif_flags |= TIFF_SWAB;
+#endif
        } else {
                tif->tif_typeshift = litTypeshift;
-               if (bigendian)
-                       tif->tif_flags |= TIFF_SWAB;
+#ifdef WORDS_BIGENDIAN
+               tif->tif_flags |= TIFF_SWAB;
+#endif
        }
 }
 
@@ -129,7 +131,7 @@ _TIFFgetMode(const char* mode, const char* module)
                        m |= O_TRUNC;
                break;
        default:
-               TIFFError(module, "\"%s\": Bad mode", mode);
+               TIFFErrorExt(0, module, "\"%s\": Bad mode", mode);
                break;
        }
        return (m);
@@ -150,7 +152,7 @@ TIFFClientOpen(
 {
        static const char module[] = "TIFFClientOpen";
        TIFF *tif;
-       int m, bigendian;
+       int m;
        const char* cp;
 
        m = _TIFFgetMode(mode, module);
@@ -158,7 +160,7 @@ TIFFClientOpen(
                goto bad2;
        tif = (TIFF *)_TIFFmalloc(sizeof (TIFF) + strlen(name) + 1);
        if (tif == NULL) {
-               TIFFError(module, "%s: Out of memory (TIFF structure)", name);
+               TIFFErrorExt(clientdata, module, "%s: Out of memory (TIFF structure)", name);
                goto bad2;
        }
        _TIFFmemset(tif, 0, sizeof (*tif));
@@ -171,7 +173,7 @@ TIFFClientOpen(
        tif->tif_row = (uint32) -1;             /* read/write pre-increment */
        tif->tif_clientdata = clientdata;
        if (!readproc || !writeproc || !seekproc || !closeproc || !sizeproc) {
-               TIFFError(module,
+               TIFFErrorExt(clientdata, module,
                          "One of the client procedures is NULL pointer.");
                goto bad2;
        }
@@ -196,14 +198,13 @@ TIFFClientOpen(
         */
        tif->tif_flags = FILLORDER_MSB2LSB;
        if (m == O_RDONLY )
-            tif->tif_flags |= TIFF_MAPPED;
+               tif->tif_flags |= TIFF_MAPPED;
 
 #ifdef STRIPCHOP_DEFAULT
        if (m == O_RDONLY || m == O_RDWR)
                tif->tif_flags |= STRIPCHOP_DEFAULT;
 #endif
 
-       { union { int32 i; char c[4]; } u; u.i = 1; bigendian = u.c[0] == 0; }
        /*
         * Process library-specific flags in the open mode string.
         * The following flags may be used to control intrinsic library
@@ -221,6 +222,7 @@ TIFFClientOpen(
         * 'm'          disable use of memory-mapped files
         * 'C'          enable strip chopping support when reading
         * 'c'          disable strip chopping support
+        * 'h'          read TIFF header only, do not load the first IFD
         *
         * The use of the 'l' and 'b' flags is strongly discouraged.
         * These flags are provided solely because numerous vendors,
@@ -256,12 +258,16 @@ TIFFClientOpen(
        for (cp = mode; *cp; cp++)
                switch (*cp) {
                case 'b':
-                       if ((m&O_CREAT) && !bigendian)
+#ifndef WORDS_BIGENDIAN
+                   if (m&O_CREAT)
                                tif->tif_flags |= TIFF_SWAB;
+#endif
                        break;
                case 'l':
-                       if ((m&O_CREAT) && bigendian)
+#ifdef WORDS_BIGENDIAN
+                       if ((m&O_CREAT))
                                tif->tif_flags |= TIFF_SWAB;
+#endif
                        break;
                case 'B':
                        tif->tif_flags = (tif->tif_flags &~ TIFF_FILLORDER) |
@@ -291,34 +297,54 @@ TIFFClientOpen(
                        if (m == O_RDONLY)
                                tif->tif_flags &= ~TIFF_STRIPCHOP;
                        break;
+               case 'h':
+                       tif->tif_flags |= TIFF_HEADERONLY;
+                       break;
                }
        /*
         * Read in TIFF header.
         */
-       if (!ReadOK(tif, &tif->tif_header, sizeof (TIFFHeader))) {
+       if (tif->tif_mode & O_TRUNC ||
+           !ReadOK(tif, &tif->tif_header, sizeof (TIFFHeader))) {
                if (tif->tif_mode == O_RDONLY) {
-                       TIFFError(name, "Cannot read TIFF header");
+                       TIFFErrorExt(tif->tif_clientdata, name,
+                                    "Cannot read TIFF header");
                        goto bad;
                }
                /*
                 * Setup header and write.
                 */
+#ifdef WORDS_BIGENDIAN
+               tif->tif_header.tiff_magic = tif->tif_flags & TIFF_SWAB
+                   ? TIFF_LITTLEENDIAN : TIFF_BIGENDIAN;
+#else
                tif->tif_header.tiff_magic = tif->tif_flags & TIFF_SWAB
-                   ? (bigendian ? TIFF_LITTLEENDIAN : TIFF_BIGENDIAN)
-                   : (bigendian ? TIFF_BIGENDIAN : TIFF_LITTLEENDIAN);
+                   ? TIFF_BIGENDIAN : TIFF_LITTLEENDIAN;
+#endif
                tif->tif_header.tiff_version = TIFF_VERSION;
                if (tif->tif_flags & TIFF_SWAB)
                        TIFFSwabShort(&tif->tif_header.tiff_version);
                tif->tif_header.tiff_diroff = 0;        /* filled in later */
 
+
+                /*
+                 * The doc for "fopen" for some STD_C_LIBs says that if you 
+                 * open a file for modify ("+"), then you must fseek (or 
+                 * fflush?) between any freads and fwrites.  This is not
+                 * necessary on most systems, but has been shown to be needed
+                 * on Solaris. 
+                 */
+                TIFFSeekFile( tif, 0, SEEK_SET );
+               
                if (!WriteOK(tif, &tif->tif_header, sizeof (TIFFHeader))) {
-                       TIFFError(name, "Error writing TIFF header");
+                       TIFFErrorExt(tif->tif_clientdata, name,
+                                    "Error writing TIFF header");
                        goto bad;
                }
                /*
                 * Setup the byte order handling.
                 */
-               TIFFInitOrder(tif, tif->tif_header.tiff_magic, bigendian);
+               TIFFInitOrder(tif, tif->tif_header.tiff_magic);
                /*
                 * Setup default directory.
                 */
@@ -326,6 +352,7 @@ TIFFClientOpen(
                        goto bad;
                tif->tif_diroff = 0;
                tif->tif_dirlist = NULL;
+               tif->tif_dirlistsize = 0;
                tif->tif_dirnumber = 0;
                return (tif);
        }
@@ -333,13 +360,27 @@ TIFFClientOpen(
         * Setup the byte order handling.
         */
        if (tif->tif_header.tiff_magic != TIFF_BIGENDIAN &&
-           tif->tif_header.tiff_magic != TIFF_LITTLEENDIAN) {
-               TIFFError(name,  "Not a TIFF file, bad magic number %d (0x%x)",
+           tif->tif_header.tiff_magic != TIFF_LITTLEENDIAN
+#if MDI_SUPPORT
+           &&
+#if HOST_BIGENDIAN
+           tif->tif_header.tiff_magic != MDI_BIGENDIAN
+#else
+           tif->tif_header.tiff_magic != MDI_LITTLEENDIAN
+#endif
+           ) {
+               TIFFErrorExt(tif->tif_clientdata, name,
+                       "Not a TIFF or MDI file, bad magic number %d (0x%x)",
+#else
+           ) {
+               TIFFErrorExt(tif->tif_clientdata, name,
+                            "Not a TIFF file, bad magic number %d (0x%x)",
+#endif
                    tif->tif_header.tiff_magic,
                    tif->tif_header.tiff_magic);
                goto bad;
        }
-       TIFFInitOrder(tif, tif->tif_header.tiff_magic, bigendian);
+       TIFFInitOrder(tif, tif->tif_header.tiff_magic);
        /*
         * Swap header if required.
         */
@@ -353,21 +394,31 @@ TIFFClientOpen(
         * magic number that doesn't change (stupid).
         */
        if (tif->tif_header.tiff_version == TIFF_BIGTIFF_VERSION) {
-               TIFFError(name,
+               TIFFErrorExt(tif->tif_clientdata, name,
                           "This is a BigTIFF file.  This format not supported\n"
                           "by this version of libtiff." );
                goto bad;
        }
        if (tif->tif_header.tiff_version != TIFF_VERSION) {
-               TIFFError(name,
+               TIFFErrorExt(tif->tif_clientdata, name,
                    "Not a TIFF file, bad version number %d (0x%x)",
                    tif->tif_header.tiff_version,
-                   tif->tif_header.tiff_version); 
+                   tif->tif_header.tiff_version);
                goto bad;
        }
        tif->tif_flags |= TIFF_MYBUFFER;
        tif->tif_rawcp = tif->tif_rawdata = 0;
        tif->tif_rawdatasize = 0;
+
+       /*
+        * Sometimes we do not want to read the first directory (for example,
+        * it may be broken) and want to proceed to other directories. I this
+        * case we use the TIFF_HEADERONLY flag to open file and return
+        * immediately after reading TIFF header.
+        */
+       if (tif->tif_flags & TIFF_HEADERONLY)
+               return (tif);
+
        /*
         * Setup initial directory.
         */
@@ -635,3 +686,10 @@ TIFFGetUnmapFileProc(TIFF* tif)
 }
 
 /* vim: set ts=8 sts=8 sw=8 noet: */
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
index 6efa8e6..ee095f5 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: tif_packbits.c,v 1.1 2005-06-17 13:54:52 vp153 Exp $ */
+/* $Id: tif_packbits.c,v 1.13.2.2 2010-06-08 18:50:42 bfriesen Exp $ */
 
 /*
  * Copyright (c) 1988-1997 Sam Leffler
@@ -38,7 +38,7 @@ PackBitsPreEncode(TIFF* tif, tsample_t s)
 {
        (void) s;
 
-        if (!(tif->tif_data = _TIFFmalloc(sizeof(tsize_t))))
+        if (!(tif->tif_data = (tidata_t)_TIFFmalloc(sizeof(tsize_t))))
                return (0);
        /*
         * Calculate the scanline/tile-width size in bytes.
@@ -239,8 +239,8 @@ PackBitsDecode(TIFF* tif, tidata_t op, tsize_t occ, tsample_t s)
                         n = -n + 1;
                         if( occ < n )
                         {
-                            TIFFWarning(tif->tif_name,
-                                        "PackBitsDecode: discarding %d bytes "
+                                                       TIFFWarningExt(tif->tif_clientdata, tif->tif_name,
+                                        "PackBitsDecode: discarding %ld bytes "
                                         "to avoid buffer overrun",
                                         n - occ);
                             n = occ;
@@ -252,8 +252,8 @@ PackBitsDecode(TIFF* tif, tidata_t op, tsize_t occ, tsample_t s)
                } else {                /* copy next n+1 bytes literally */
                        if (occ < n + 1)
                         {
-                            TIFFWarning(tif->tif_name,
-                                        "PackBitsDecode: discarding %d bytes "
+                            TIFFWarningExt(tif->tif_clientdata, tif->tif_name,
+                                        "PackBitsDecode: discarding %ld bytes "
                                         "to avoid buffer overrun",
                                         n - occ + 1);
                             n = occ - 1;
@@ -266,7 +266,7 @@ PackBitsDecode(TIFF* tif, tidata_t op, tsize_t occ, tsample_t s)
        tif->tif_rawcp = (tidata_t) bp;
        tif->tif_rawcc = cc;
        if (occ > 0) {
-               TIFFError(tif->tif_name,
+               TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
                    "PackBitsDecode: Not enough data for scanline %ld",
                    (long) tif->tif_row);
                return (0);
@@ -291,3 +291,10 @@ TIFFInitPackBits(TIFF* tif, int scheme)
 #endif /* PACKBITS_SUPPORT */
 
 /* vim: set ts=8 sts=8 sw=8 noet: */
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
index ae3d38d..ed8eb40 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: tif_pixarlog.c,v 1.1 2005-06-17 13:54:52 vp153 Exp $ */
+/* $Id: tif_pixarlog.c,v 1.15.2.4 2010-06-08 18:50:42 bfriesen Exp $ */
 
 /*
  * Copyright (c) 1996-1997 Sam Leffler
@@ -327,7 +327,7 @@ horizontalAccumulate11(uint16 *wp, int n, int stride, uint16 *op)
            while (n > 0) {
                REPEAT(stride,
                    wp[stride] += *wp; *op = *wp&mask; wp++; op++)
-               n -= stride;
+               n -= stride;
            }
        }
     }
@@ -593,7 +593,6 @@ PixarLogMakeTables(PixarLogState *sp)
 static int PixarLogEncode(TIFF*, tidata_t, tsize_t, tsample_t);
 static int PixarLogDecode(TIFF*, tidata_t, tsize_t, tsample_t);
 
-#define N(a)   (sizeof(a)/sizeof(a[0]))
 #define PIXARLOGDATAFMT_UNKNOWN        -1
 
 static int
@@ -670,14 +669,14 @@ PixarLogSetupDecode(TIFF* tif)
        if (sp->user_datafmt == PIXARLOGDATAFMT_UNKNOWN)
                sp->user_datafmt = PixarLogGuessDataFmt(td);
        if (sp->user_datafmt == PIXARLOGDATAFMT_UNKNOWN) {
-               TIFFError(module, 
+               TIFFErrorExt(tif->tif_clientdata, module,
                        "PixarLog compression can't handle bits depth/data format combination (depth: %d)", 
                        td->td_bitspersample);
                return (0);
        }
 
        if (inflateInit(&sp->stream) != Z_OK) {
-               TIFFError(module, "%s: %s", tif->tif_name, sp->stream.msg);
+               TIFFErrorExt(tif->tif_clientdata, module, "%s: %s", tif->tif_name, sp->stream.msg);
                return (0);
        } else {
                sp->state |= PLSTATE_INIT;
@@ -723,7 +722,7 @@ PixarLogDecode(TIFF* tif, tidata_t op, tsize_t occ, tsample_t s)
                nsamples = occ;
                break;
        default:
-               TIFFError(tif->tif_name,
+               TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
                        "%d bit input not supported in PixarLog",
                        td->td_bitspersample);
                return 0;
@@ -741,7 +740,7 @@ PixarLogDecode(TIFF* tif, tidata_t op, tsize_t occ, tsample_t s)
                        break;                  /* XXX */
                }
                if (state == Z_DATA_ERROR) {
-                       TIFFError(module,
+                       TIFFErrorExt(tif->tif_clientdata, module,
                            "%s: Decoding error at scanline %d, %s",
                            tif->tif_name, tif->tif_row, sp->stream.msg);
                        if (inflateSync(&sp->stream) != Z_OK)
@@ -749,7 +748,7 @@ PixarLogDecode(TIFF* tif, tidata_t op, tsize_t occ, tsample_t s)
                        continue;
                }
                if (state != Z_OK) {
-                       TIFFError(module, "%s: zlib error: %s",
+                       TIFFErrorExt(tif->tif_clientdata, module, "%s: zlib error: %s",
                            tif->tif_name, sp->stream.msg);
                        return (0);
                }
@@ -757,7 +756,7 @@ PixarLogDecode(TIFF* tif, tidata_t op, tsize_t occ, tsample_t s)
 
        /* hopefully, we got all the bytes we needed */
        if (sp->stream.avail_out != 0) {
-               TIFFError(module,
+               TIFFErrorExt(tif->tif_clientdata, module,
                    "%s: Not enough data at scanline %d (short %d bytes)",
                    tif->tif_name, tif->tif_row, sp->stream.avail_out);
                return (0);
@@ -768,6 +767,18 @@ PixarLogDecode(TIFF* tif, tidata_t op, tsize_t occ, tsample_t s)
        if (tif->tif_flags & TIFF_SWAB)
                TIFFSwabArrayOfShort(up, nsamples);
 
+       /* 
+        * if llen is not an exact multiple of nsamples, the decode operation
+        * may overflow the output buffer, so truncate it enough to prevent
+        * that but still salvage as much data as possible.
+        */
+       if (nsamples % llen) { 
+               TIFFWarningExt(tif->tif_clientdata, module,
+                       "%s: stride %d is not a multiple of sample count, "
+                       "%d, data truncated.", tif->tif_name, llen, nsamples);
+               nsamples -= nsamples % llen;
+       }
+
        for (i = 0; i < nsamples; i += llen, up += llen) {
                switch (sp->user_datafmt)  {
                case PIXARLOGDATAFMT_FLOAT:
@@ -801,7 +812,7 @@ PixarLogDecode(TIFF* tif, tidata_t op, tsize_t occ, tsample_t s)
                        op += llen * sizeof(unsigned char);
                        break;
                default:
-                       TIFFError(tif->tif_name,
+                       TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
                                  "PixarLogDecode: unsupported bits/sample: %d", 
                                  td->td_bitspersample);
                        return (0);
@@ -835,12 +846,12 @@ PixarLogSetupEncode(TIFF* tif)
        if (sp->user_datafmt == PIXARLOGDATAFMT_UNKNOWN)
                sp->user_datafmt = PixarLogGuessDataFmt(td);
        if (sp->user_datafmt == PIXARLOGDATAFMT_UNKNOWN) {
-               TIFFError(module, "PixarLog compression can't handle %d bit linear encodings", td->td_bitspersample);
+               TIFFErrorExt(tif->tif_clientdata, module, "PixarLog compression can't handle %d bit linear encodings", td->td_bitspersample);
                return (0);
        }
 
        if (deflateInit(&sp->stream, sp->quality) != Z_OK) {
-               TIFFError(module, "%s: %s", tif->tif_name, sp->stream.msg);
+               TIFFErrorExt(tif->tif_clientdata, module, "%s: %s", tif->tif_name, sp->stream.msg);
                return (0);
        } else {
                sp->state |= PLSTATE_INIT;
@@ -1036,7 +1047,7 @@ PixarLogEncode(TIFF* tif, tidata_t bp, tsize_t cc, tsample_t s)
        TIFFDirectory *td = &tif->tif_dir;
        PixarLogState *sp = EncoderState(tif);
        static const char module[] = "PixarLogEncode";
-       int     i, n, llen;
+       int     i, n, llen;
        unsigned short * up;
 
        (void) s;
@@ -1055,7 +1066,7 @@ PixarLogEncode(TIFF* tif, tidata_t bp, tsize_t cc, tsample_t s)
                n = cc;
                break;
        default:
-               TIFFError(tif->tif_name,
+               TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
                        "%d bit input not supported in PixarLog",
                        td->td_bitspersample);
                return 0;
@@ -1081,7 +1092,7 @@ PixarLogEncode(TIFF* tif, tidata_t bp, tsize_t cc, tsample_t s)
                        bp += llen * sizeof(unsigned char);
                        break;
                default:
-                       TIFFError(tif->tif_name,
+                       TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
                                "%d bit input not supported in PixarLog",
                                td->td_bitspersample);
                        return 0;
@@ -1093,7 +1104,7 @@ PixarLogEncode(TIFF* tif, tidata_t bp, tsize_t cc, tsample_t s)
 
        do {
                if (deflate(&sp->stream, Z_NO_FLUSH) != Z_OK) {
-                       TIFFError(module, "%s: Encoder error: %s",
+                       TIFFErrorExt(tif->tif_clientdata, module, "%s: Encoder error: %s",
                            tif->tif_name, sp->stream.msg);
                        return (0);
                }
@@ -1126,7 +1137,7 @@ PixarLogPostEncode(TIFF* tif)
                switch (state) {
                case Z_STREAM_END:
                case Z_OK:
-                   if (sp->stream.avail_out != tif->tif_rawdatasize) {
+                   if (sp->stream.avail_out != (uint32)tif->tif_rawdatasize) {
                            tif->tif_rawcc =
                                tif->tif_rawdatasize - sp->stream.avail_out;
                            TIFFFlushData1(tif);
@@ -1135,7 +1146,7 @@ PixarLogPostEncode(TIFF* tif)
                    }
                    break;
                default:
-                   TIFFError(module, "%s: zlib error: %s",
+                       TIFFErrorExt(tif->tif_clientdata, module, "%s: zlib error: %s",
                        tif->tif_name, sp->stream.msg);
                    return (0);
                }
@@ -1163,24 +1174,31 @@ PixarLogCleanup(TIFF* tif)
 {
        PixarLogState* sp = (PixarLogState*) tif->tif_data;
 
-       if (sp) {
-               if (sp->FromLT2) _TIFFfree(sp->FromLT2);
-               if (sp->From14) _TIFFfree(sp->From14);
-               if (sp->From8) _TIFFfree(sp->From8);
-               if (sp->ToLinearF) _TIFFfree(sp->ToLinearF);
-               if (sp->ToLinear16) _TIFFfree(sp->ToLinear16);
-               if (sp->ToLinear8) _TIFFfree(sp->ToLinear8);
-               if (sp->state&PLSTATE_INIT) {
-                       if (tif->tif_mode == O_RDONLY)
-                               inflateEnd(&sp->stream);
-                       else
-                               deflateEnd(&sp->stream);
-               }
-               if (sp->tbuf)
-                       _TIFFfree(sp->tbuf);
-               _TIFFfree(sp);
-               tif->tif_data = NULL;
+       assert(sp != 0);
+
+       (void)TIFFPredictorCleanup(tif);
+
+       tif->tif_tagmethods.vgetfield = sp->vgetparent;
+       tif->tif_tagmethods.vsetfield = sp->vsetparent;
+
+       if (sp->FromLT2) _TIFFfree(sp->FromLT2);
+       if (sp->From14) _TIFFfree(sp->From14);
+       if (sp->From8) _TIFFfree(sp->From8);
+       if (sp->ToLinearF) _TIFFfree(sp->ToLinearF);
+       if (sp->ToLinear16) _TIFFfree(sp->ToLinear16);
+       if (sp->ToLinear8) _TIFFfree(sp->ToLinear8);
+       if (sp->state&PLSTATE_INIT) {
+               if (tif->tif_mode == O_RDONLY)
+                       inflateEnd(&sp->stream);
+               else
+                       deflateEnd(&sp->stream);
        }
+       if (sp->tbuf)
+               _TIFFfree(sp->tbuf);
+       _TIFFfree(sp);
+       tif->tif_data = NULL;
+
+       _TIFFSetDefaultCompressionState(tif);
 }
 
 static int
@@ -1196,7 +1214,7 @@ PixarLogVSetField(TIFF* tif, ttag_t tag, va_list ap)
                if (tif->tif_mode != O_RDONLY && (sp->state&PLSTATE_INIT)) {
                        if (deflateParams(&sp->stream,
                            sp->quality, Z_DEFAULT_STRATEGY) != Z_OK) {
-                               TIFFError(module, "%s: zlib error: %s",
+                               TIFFErrorExt(tif->tif_clientdata, module, "%s: zlib error: %s",
                                        tif->tif_name, sp->stream.msg);
                                return (0);
                        }
@@ -1271,11 +1289,23 @@ static const TIFFFieldInfo pixarlogFieldInfo[] = {
 int
 TIFFInitPixarLog(TIFF* tif, int scheme)
 {
+       static const char module[] = "TIFFInitPixarLog";
+
        PixarLogState* sp;
 
        assert(scheme == COMPRESSION_PIXARLOG);
 
        /*
+        * Merge codec-specific tag information.
+        */
+       if (!_TIFFMergeFieldInfo(tif, pixarlogFieldInfo,
+                                TIFFArrayCount(pixarlogFieldInfo))) {
+               TIFFErrorExt(tif->tif_clientdata, module,
+                            "Merging PixarLog codec-specific tags failed");
+               return 0;
+       }
+
+       /*
         * Allocate state block so tag methods have storage to record values.
         */
        tif->tif_data = (tidata_t) _TIFFmalloc(sizeof (PixarLogState));
@@ -1304,7 +1334,6 @@ TIFFInitPixarLog(TIFF* tif, int scheme)
        tif->tif_cleanup = PixarLogCleanup;
 
        /* Override SetField so we can handle our private pseudo-tag */
-       _TIFFMergeFieldInfo(tif, pixarlogFieldInfo, N(pixarlogFieldInfo));
        sp->vgetparent = tif->tif_tagmethods.vgetfield;
        tif->tif_tagmethods.vgetfield = PixarLogVGetField;   /* hook for codec tags */
        sp->vsetparent = tif->tif_tagmethods.vsetfield;
@@ -1326,9 +1355,17 @@ TIFFInitPixarLog(TIFF* tif, int scheme)
 
        return (1);
 bad:
-       TIFFError("TIFFInitPixarLog", "No space for PixarLog state block");
+       TIFFErrorExt(tif->tif_clientdata, module,
+                    "No space for PixarLog state block");
        return (0);
 }
 #endif /* PIXARLOG_SUPPORT */
 
 /* vim: set ts=8 sts=8 sw=8 noet: */
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
index 2481121..bbc221f 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: tif_predict.c,v 1.1 2005-06-17 13:54:52 vp153 Exp $ */
+/* $Id: tif_predict.c,v 1.11.2.4 2010-06-08 18:50:42 bfriesen Exp $ */
 
 /*
  * Copyright (c) 1988-1997 Sam Leffler
 
 static void horAcc8(TIFF*, tidata_t, tsize_t);
 static void horAcc16(TIFF*, tidata_t, tsize_t);
+static void horAcc32(TIFF*, tidata_t, tsize_t);
 static void swabHorAcc16(TIFF*, tidata_t, tsize_t);
+static void swabHorAcc32(TIFF*, tidata_t, tsize_t);
 static void horDiff8(TIFF*, tidata_t, tsize_t);
 static void horDiff16(TIFF*, tidata_t, tsize_t);
+static void horDiff32(TIFF*, tidata_t, tsize_t);
+static void fpAcc(TIFF*, tidata_t, tsize_t);
+static void fpDiff(TIFF*, tidata_t, tsize_t);
 static int PredictorDecodeRow(TIFF*, tidata_t, tsize_t, tsample_t);
 static int PredictorDecodeTile(TIFF*, tidata_t, tsize_t, tsample_t);
 static int PredictorEncodeRow(TIFF*, tidata_t, tsize_t, tsample_t);
@@ -47,21 +52,38 @@ static      int PredictorEncodeTile(TIFF*, tidata_t, tsize_t, tsample_t);
 static int
 PredictorSetup(TIFF* tif)
 {
+       static const char module[] = "PredictorSetup";
+
        TIFFPredictorState* sp = PredictorState(tif);
        TIFFDirectory* td = &tif->tif_dir;
 
-       if (sp->predictor == 1)         /* no differencing */
-               return (1);
-       if (sp->predictor != 2) {
-               TIFFError(tif->tif_name, "\"Predictor\" value %d not supported",
-                   sp->predictor);
-               return (0);
-       }
-       if (td->td_bitspersample != 8 && td->td_bitspersample != 16) {
-               TIFFError(tif->tif_name,
+       switch (sp->predictor)          /* no differencing */
+       {
+               case PREDICTOR_NONE:
+                       return 1;
+               case PREDICTOR_HORIZONTAL:
+                       if (td->td_bitspersample != 8
+                           && td->td_bitspersample != 16
+                           && td->td_bitspersample != 32) {
+                               TIFFErrorExt(tif->tif_clientdata, module,
     "Horizontal differencing \"Predictor\" not supported with %d-bit samples",
-                   td->td_bitspersample);
-               return (0);
+                                         td->td_bitspersample);
+                               return 0;
+                       }
+                       break;
+               case PREDICTOR_FLOATINGPOINT:
+                       if (td->td_sampleformat != SAMPLEFORMAT_IEEEFP) {
+                               TIFFErrorExt(tif->tif_clientdata, module,
+       "Floating point \"Predictor\" not supported with %d data format",
+                                         td->td_sampleformat);
+                               return 0;
+                       }
+                       break;
+               default:
+                       TIFFErrorExt(tif->tif_clientdata, module,
+                                 "\"Predictor\" value %d not supported",
+                                 sp->predictor);
+                       return 0;
        }
        sp->stride = (td->td_planarconfig == PLANARCONFIG_CONTIG ?
            td->td_samplesperpixel : 1);
@@ -72,7 +94,8 @@ PredictorSetup(TIFF* tif)
                sp->rowsize = TIFFTileRowSize(tif);
        else
                sp->rowsize = TIFFScanlineSize(tif);
-       return (1);
+
+       return 1;
 }
 
 static int
@@ -82,39 +105,75 @@ PredictorSetupDecode(TIFF* tif)
        TIFFDirectory* td = &tif->tif_dir;
 
        if (!(*sp->setupdecode)(tif) || !PredictorSetup(tif))
-               return (0);
+               return 0;
+
        if (sp->predictor == 2) {
                switch (td->td_bitspersample) {
-               case 8:  sp->pfunc = horAcc8; break;
-               case 16: sp->pfunc = horAcc16; break;
+                       case 8:  sp->decodepfunc = horAcc8; break;
+                       case 16: sp->decodepfunc = horAcc16; break;
+                       case 32: sp->decodepfunc = horAcc32; break;
                }
                /*
-                * Override default decoding method with
-                * one that does the predictor stuff.
+                * Override default decoding method with one that does the
+                * predictor stuff.
                 */
-               sp->coderow = tif->tif_decoderow;
-               tif->tif_decoderow = PredictorDecodeRow;
-               sp->codestrip = tif->tif_decodestrip;
-               tif->tif_decodestrip = PredictorDecodeTile;
-               sp->codetile = tif->tif_decodetile;
-               tif->tif_decodetile = PredictorDecodeTile;
+                if( tif->tif_decoderow != PredictorDecodeRow )
+                {
+                    sp->decoderow = tif->tif_decoderow;
+                    tif->tif_decoderow = PredictorDecodeRow;
+                    sp->decodestrip = tif->tif_decodestrip;
+                    tif->tif_decodestrip = PredictorDecodeTile;
+                    sp->decodetile = tif->tif_decodetile;
+                    tif->tif_decodetile = PredictorDecodeTile;
+                }
                /*
-                * If the data is horizontally differenced
-                * 16-bit data that requires byte-swapping,
-                * then it must be byte swapped before the
-                * accumulation step.  We do this with a
-                * special-purpose routine and override the
-                * normal post decoding logic that the library
-                * setup when the directory was read.
+                * If the data is horizontally differenced 16-bit data that
+                * requires byte-swapping, then it must be byte swapped before
+                * the accumulation step.  We do this with a special-purpose
+                * routine and override the normal post decoding logic that
+                * the library setup when the directory was read.
                 */
-               if (tif->tif_flags&TIFF_SWAB) {
-                       if (sp->pfunc == horAcc16) {
-                               sp->pfunc = swabHorAcc16;
+               if (tif->tif_flags & TIFF_SWAB) {
+                       if (sp->decodepfunc == horAcc16) {
+                               sp->decodepfunc = swabHorAcc16;
                                tif->tif_postdecode = _TIFFNoPostDecode;
-                       } /* else handle 32-bit case... */
+                       } else if (sp->decodepfunc == horAcc32) {
+                               sp->decodepfunc = swabHorAcc32;
+                               tif->tif_postdecode = _TIFFNoPostDecode;
+                       }
                }
        }
-       return (1);
+
+       else if (sp->predictor == 3) {
+               sp->decodepfunc = fpAcc;
+               /*
+                * Override default decoding method with one that does the
+                * predictor stuff.
+                */
+                if( tif->tif_decoderow != PredictorDecodeRow )
+                {
+                    sp->decoderow = tif->tif_decoderow;
+                    tif->tif_decoderow = PredictorDecodeRow;
+                    sp->decodestrip = tif->tif_decodestrip;
+                    tif->tif_decodestrip = PredictorDecodeTile;
+                    sp->decodetile = tif->tif_decodetile;
+                    tif->tif_decodetile = PredictorDecodeTile;
+                }
+               /*
+                * The data should not be swapped outside of the floating
+                * point predictor, the accumulation routine should return
+                * byres in the native order.
+                */
+               if (tif->tif_flags & TIFF_SWAB) {
+                       tif->tif_postdecode = _TIFFNoPostDecode;
+               }
+               /*
+                * Allocate buffer to keep the decoded bytes before
+                * rearranging in the ight order
+                */
+       }
+
+       return 1;
 }
 
 static int
@@ -124,24 +183,47 @@ PredictorSetupEncode(TIFF* tif)
        TIFFDirectory* td = &tif->tif_dir;
 
        if (!(*sp->setupencode)(tif) || !PredictorSetup(tif))
-               return (0);
+               return 0;
+
        if (sp->predictor == 2) {
                switch (td->td_bitspersample) {
-               case 8:  sp->pfunc = horDiff8; break;
-               case 16: sp->pfunc = horDiff16; break;
+                       case 8:  sp->encodepfunc = horDiff8; break;
+                       case 16: sp->encodepfunc = horDiff16; break;
+                       case 32: sp->encodepfunc = horDiff32; break;
                }
                /*
-                * Override default encoding method with
-                * one that does the predictor stuff.
+                * Override default encoding method with one that does the
+                * predictor stuff.
+                */
+                if( tif->tif_encoderow != PredictorEncodeRow )
+                {
+                    sp->encoderow = tif->tif_encoderow;
+                    tif->tif_encoderow = PredictorEncodeRow;
+                    sp->encodestrip = tif->tif_encodestrip;
+                    tif->tif_encodestrip = PredictorEncodeTile;
+                    sp->encodetile = tif->tif_encodetile;
+                    tif->tif_encodetile = PredictorEncodeTile;
+                }
+       }
+       
+       else if (sp->predictor == 3) {
+               sp->encodepfunc = fpDiff;
+               /*
+                * Override default encoding method with one that does the
+                * predictor stuff.
                 */
-               sp->coderow = tif->tif_encoderow;
-               tif->tif_encoderow = PredictorEncodeRow;
-               sp->codestrip = tif->tif_encodestrip;
-               tif->tif_encodestrip = PredictorEncodeTile;
-               sp->codetile = tif->tif_encodetile;
-               tif->tif_encodetile = PredictorEncodeTile;
+                if( tif->tif_encoderow != PredictorEncodeRow )
+                {
+                    sp->encoderow = tif->tif_encoderow;
+                    tif->tif_encoderow = PredictorEncodeRow;
+                    sp->encodestrip = tif->tif_encodestrip;
+                    tif->tif_encodestrip = PredictorEncodeTile;
+                    sp->encodetile = tif->tif_encodetile;
+                    tif->tif_encodetile = PredictorEncodeTile;
+                }
        }
-       return (1);
+
+       return 1;
 }
 
 #define REPEAT4(n, op)         \
@@ -157,8 +239,7 @@ PredictorSetupEncode(TIFF* tif)
 static void
 horAcc8(TIFF* tif, tidata_t cp0, tsize_t cc)
 {
-       TIFFPredictorState* sp = PredictorState(tif);
-       tsize_t stride = sp->stride;
+       tsize_t stride = PredictorState(tif)->stride;
 
        char* cp = (char*) cp0;
        if (cc > stride) {
@@ -190,7 +271,8 @@ horAcc8(TIFF* tif, tidata_t cp0, tsize_t cc)
                        } while ((int32) cc > 0);
                } else  {
                        do {
-                               REPEAT4(stride, cp[stride] = (char) (cp[stride] + *cp); cp++)
+                               REPEAT4(stride, cp[stride] =
+                                       (char) (cp[stride] + *cp); cp++)
                                cc -= stride;
                        } while ((int32) cc > 0);
                }
@@ -200,8 +282,7 @@ horAcc8(TIFF* tif, tidata_t cp0, tsize_t cc)
 static void
 swabHorAcc16(TIFF* tif, tidata_t cp0, tsize_t cc)
 {
-       TIFFPredictorState* sp = PredictorState(tif);
-       tsize_t stride = sp->stride;
+       tsize_t stride = PredictorState(tif)->stride;
        uint16* wp = (uint16*) cp0;
        tsize_t wc = cc / 2;
 
@@ -231,6 +312,76 @@ horAcc16(TIFF* tif, tidata_t cp0, tsize_t cc)
        }
 }
 
+static void
+swabHorAcc32(TIFF* tif, tidata_t cp0, tsize_t cc)
+{
+       tsize_t stride = PredictorState(tif)->stride;
+       uint32* wp = (uint32*) cp0;
+       tsize_t wc = cc / 4;
+
+       if (wc > stride) {
+               TIFFSwabArrayOfLong(wp, wc);
+               wc -= stride;
+               do {
+                       REPEAT4(stride, wp[stride] += wp[0]; wp++)
+                       wc -= stride;
+               } while ((int32) wc > 0);
+       }
+}
+
+static void
+horAcc32(TIFF* tif, tidata_t cp0, tsize_t cc)
+{
+       tsize_t stride = PredictorState(tif)->stride;
+       uint32* wp = (uint32*) cp0;
+       tsize_t wc = cc / 4;
+
+       if (wc > stride) {
+               wc -= stride;
+               do {
+                       REPEAT4(stride, wp[stride] += wp[0]; wp++)
+                       wc -= stride;
+               } while ((int32) wc > 0);
+       }
+}
+
+/*
+ * Floating point predictor accumulation routine.
+ */
+static void
+fpAcc(TIFF* tif, tidata_t cp0, tsize_t cc)
+{
+       tsize_t stride = PredictorState(tif)->stride;
+       uint32 bps = tif->tif_dir.td_bitspersample / 8;
+       tsize_t wc = cc / bps;
+       tsize_t count = cc;
+       uint8 *cp = (uint8 *) cp0;
+       uint8 *tmp = (uint8 *)_TIFFmalloc(cc);
+
+       if (!tmp)
+               return;
+
+       while (count > stride) {
+               REPEAT4(stride, cp[stride] += cp[0]; cp++)
+               count -= stride;
+       }
+
+       _TIFFmemcpy(tmp, cp0, cc);
+       cp = (uint8 *) cp0;
+       for (count = 0; count < wc; count++) {
+               uint32 byte;
+               for (byte = 0; byte < bps; byte++) {
+#if WORDS_BIGENDIAN
+                       cp[bps * count + byte] = tmp[byte * wc + count];
+#else
+                       cp[bps * count + byte] =
+                               tmp[(bps - byte - 1) * wc + count];
+#endif
+               }
+       }
+       _TIFFfree(tmp);
+}
+
 /*
  * Decode a scanline and apply the predictor routine.
  */
@@ -240,13 +391,14 @@ PredictorDecodeRow(TIFF* tif, tidata_t op0, tsize_t occ0, tsample_t s)
        TIFFPredictorState *sp = PredictorState(tif);
 
        assert(sp != NULL);
-       assert(sp->coderow != NULL);
-       assert(sp->pfunc != NULL);
-       if ((*sp->coderow)(tif, op0, occ0, s)) {
-               (*sp->pfunc)(tif, op0, occ0);
-               return (1);
+       assert(sp->decoderow != NULL);
+       assert(sp->decodepfunc != NULL);
+
+       if ((*sp->decoderow)(tif, op0, occ0, s)) {
+               (*sp->decodepfunc)(tif, op0, occ0);
+               return 1;
        } else
-               return (0);
+               return 0;
 }
 
 /*
@@ -262,19 +414,20 @@ PredictorDecodeTile(TIFF* tif, tidata_t op0, tsize_t occ0, tsample_t s)
        TIFFPredictorState *sp = PredictorState(tif);
 
        assert(sp != NULL);
-       assert(sp->codetile != NULL);
-       if ((*sp->codetile)(tif, op0, occ0, s)) {
+       assert(sp->decodetile != NULL);
+
+       if ((*sp->decodetile)(tif, op0, occ0, s)) {
                tsize_t rowsize = sp->rowsize;
                assert(rowsize > 0);
-               assert(sp->pfunc != NULL);
+               assert(sp->decodepfunc != NULL);
                while ((long)occ0 > 0) {
-                       (*sp->pfunc)(tif, op0, (tsize_t) rowsize);
+                       (*sp->decodepfunc)(tif, op0, (tsize_t) rowsize);
                        occ0 -= rowsize;
                        op0 += rowsize;
                }
-               return (1);
+               return 1;
        } else
-               return (0);
+               return 0;
 }
 
 static void
@@ -340,37 +493,116 @@ horDiff16(TIFF* tif, tidata_t cp0, tsize_t cc)
        }
 }
 
+static void
+horDiff32(TIFF* tif, tidata_t cp0, tsize_t cc)
+{
+       TIFFPredictorState* sp = PredictorState(tif);
+       tsize_t stride = sp->stride;
+       int32 *wp = (int32*) cp0;
+       tsize_t wc = cc/4;
+
+       if (wc > stride) {
+               wc -= stride;
+               wp += wc - 1;
+               do {
+                       REPEAT4(stride, wp[stride] -= wp[0]; wp--)
+                       wc -= stride;
+               } while ((int32) wc > 0);
+       }
+}
+
+/*
+ * Floating point predictor differencing routine.
+ */
+static void
+fpDiff(TIFF* tif, tidata_t cp0, tsize_t cc)
+{
+       tsize_t stride = PredictorState(tif)->stride;
+       uint32 bps = tif->tif_dir.td_bitspersample / 8;
+       tsize_t wc = cc / bps;
+       tsize_t count;
+       uint8 *cp = (uint8 *) cp0;
+       uint8 *tmp = (uint8 *)_TIFFmalloc(cc);
+
+       if (!tmp)
+               return;
+
+       _TIFFmemcpy(tmp, cp0, cc);
+       for (count = 0; count < wc; count++) {
+               uint32 byte;
+               for (byte = 0; byte < bps; byte++) {
+#if WORDS_BIGENDIAN
+                       cp[byte * wc + count] = tmp[bps * count + byte];
+#else
+                       cp[(bps - byte - 1) * wc + count] =
+                               tmp[bps * count + byte];
+#endif
+               }
+       }
+       _TIFFfree(tmp);
+
+       cp = (uint8 *) cp0;
+       cp += cc - stride - 1;
+       for (count = cc; count > stride; count -= stride)
+               REPEAT4(stride, cp[stride] -= cp[0]; cp--)
+}
+
 static int
 PredictorEncodeRow(TIFF* tif, tidata_t bp, tsize_t cc, tsample_t s)
 {
        TIFFPredictorState *sp = PredictorState(tif);
 
        assert(sp != NULL);
-       assert(sp->pfunc != NULL);
-       assert(sp->coderow != NULL);
-/* XXX horizontal differencing alters user's data XXX */
-       (*sp->pfunc)(tif, bp, cc);
-       return ((*sp->coderow)(tif, bp, cc, s));
+       assert(sp->encodepfunc != NULL);
+       assert(sp->encoderow != NULL);
+
+       /* XXX horizontal differencing alters user's data XXX */
+       (*sp->encodepfunc)(tif, bp, cc);
+       return (*sp->encoderow)(tif, bp, cc, s);
 }
 
 static int
 PredictorEncodeTile(TIFF* tif, tidata_t bp0, tsize_t cc0, tsample_t s)
 {
+       static const char module[] = "PredictorEncodeTile";
        TIFFPredictorState *sp = PredictorState(tif);
+        uint8 *working_copy;
        tsize_t cc = cc0, rowsize;
-       unsigned char* bp = bp0;
+       unsigned char* bp;
+        int result_code;
 
        assert(sp != NULL);
-       assert(sp->pfunc != NULL);
-       assert(sp->codetile != NULL);
+       assert(sp->encodepfunc != NULL);
+       assert(sp->encodetile != NULL);
+
+        /* 
+         * Do predictor manipulation in a working buffer to avoid altering
+         * the callers buffer. http://trac.osgeo.org/gdal/ticket/1965
+         */
+        working_copy = (uint8*) _TIFFmalloc(cc0);
+        if( working_copy == NULL )
+        {
+            TIFFErrorExt(tif->tif_clientdata, module, 
+                         "Out of memory allocating %d byte temp buffer.",
+                         cc0 );
+            return 0;
+        }
+        memcpy( working_copy, bp0, cc0 );
+        bp = working_copy;
+
        rowsize = sp->rowsize;
        assert(rowsize > 0);
-       while ((long)cc > 0) {
-               (*sp->pfunc)(tif, bp, (tsize_t) rowsize);
+       assert((cc0%rowsize)==0);
+       while (cc > 0) {
+               (*sp->encodepfunc)(tif, bp, rowsize);
                cc -= rowsize;
                bp += rowsize;
        }
-       return ((*sp->codetile)(tif, bp0, cc0, s));
+       result_code = (*sp->encodetile)(tif, working_copy, cc0, s);
+
+        _TIFFfree( working_copy );
+
+        return result_code;
 }
 
 #define        FIELD_PREDICTOR (FIELD_CODEC+0)         /* XXX */
@@ -379,13 +611,15 @@ static const TIFFFieldInfo predictFieldInfo[] = {
     { TIFFTAG_PREDICTOR,        1, 1, TIFF_SHORT,      FIELD_PREDICTOR,
       FALSE,   FALSE,  "Predictor" },
 };
-#define        N(a)    (sizeof (a) / sizeof (a[0]))
 
 static int
 PredictorVSetField(TIFF* tif, ttag_t tag, va_list ap)
 {
        TIFFPredictorState *sp = PredictorState(tif);
 
+       assert(sp != NULL);
+       assert(sp->vsetparent != NULL);
+
        switch (tag) {
        case TIFFTAG_PREDICTOR:
                sp->predictor = (uint16) va_arg(ap, int);
@@ -395,7 +629,7 @@ PredictorVSetField(TIFF* tif, ttag_t tag, va_list ap)
                return (*sp->vsetparent)(tif, tag, ap);
        }
        tif->tif_flags |= TIFF_DIRTYDIRECT;
-       return (1);
+       return 1;
 }
 
 static int
@@ -403,6 +637,9 @@ PredictorVGetField(TIFF* tif, ttag_t tag, va_list ap)
 {
        TIFFPredictorState *sp = PredictorState(tif);
 
+       assert(sp != NULL);
+       assert(sp->vgetparent != NULL);
+
        switch (tag) {
        case TIFFTAG_PREDICTOR:
                *va_arg(ap, uint16*) = sp->predictor;
@@ -410,7 +647,7 @@ PredictorVGetField(TIFF* tif, ttag_t tag, va_list ap)
        default:
                return (*sp->vgetparent)(tif, tag, ap);
        }
-       return (1);
+       return 1;
 }
 
 static void
@@ -424,6 +661,7 @@ PredictorPrintDir(TIFF* tif, FILE* fd, long flags)
                switch (sp->predictor) {
                case 1: fprintf(fd, "none "); break;
                case 2: fprintf(fd, "horizontal differencing "); break;
+               case 3: fprintf(fd, "floating point predictor "); break;
                }
                fprintf(fd, "%u (0x%x)\n", sp->predictor, sp->predictor);
        }
@@ -436,11 +674,21 @@ TIFFPredictorInit(TIFF* tif)
 {
        TIFFPredictorState* sp = PredictorState(tif);
 
+       assert(sp != 0);
+
        /*
-        * Merge codec-specific tag information and
-        * override parent get/set field methods.
+        * Merge codec-specific tag information.
+        */
+       if (!_TIFFMergeFieldInfo(tif, predictFieldInfo,
+                                TIFFArrayCount(predictFieldInfo))) {
+               TIFFErrorExt(tif->tif_clientdata, "TIFFPredictorInit",
+                            "Merging Predictor codec-specific tags failed");
+               return 0;
+       }
+
+       /*
+        * Override parent get/set field methods.
         */
-       _TIFFMergeFieldInfo(tif, predictFieldInfo, N(predictFieldInfo));
        sp->vgetparent = tif->tif_tagmethods.vgetfield;
        tif->tif_tagmethods.vgetfield =
             PredictorVGetField;/* hook for predictor tag */
@@ -457,8 +705,32 @@ TIFFPredictorInit(TIFF* tif)
        tif->tif_setupencode = PredictorSetupEncode;
 
        sp->predictor = 1;                      /* default value */
-       sp->pfunc = NULL;                       /* no predictor routine */
-       return (1);
+       sp->encodepfunc = NULL;                 /* no predictor routine */
+       sp->decodepfunc = NULL;                 /* no predictor routine */
+       return 1;
+}
+
+int
+TIFFPredictorCleanup(TIFF* tif)
+{
+       TIFFPredictorState* sp = PredictorState(tif);
+
+       assert(sp != 0);
+
+       tif->tif_tagmethods.vgetfield = sp->vgetparent;
+       tif->tif_tagmethods.vsetfield = sp->vsetparent;
+       tif->tif_tagmethods.printdir = sp->printdir;
+       tif->tif_setupdecode = sp->setupdecode;
+       tif->tif_setupencode = sp->setupencode;
+
+       return 1;
 }
 
 /* vim: set ts=8 sts=8 sw=8 noet: */
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
index 4a5cfc3..da0ad98 100644 (file)
@@ -1,4 +1,4 @@
-/* $Header: /home/vp/work/opencv-cvsbackup/opencv/3rdparty/libtiff/tif_predict.h,v 1.1 2005-06-17 13:54:52 vp153 Exp $ */
+/* $Id: tif_predict.h,v 1.3.2.2 2010-06-08 18:50:42 bfriesen Exp $ */
 
 /*
  * Copyright (c) 1995-1997 Sam Leffler
  * the predictor code can cast tif_data to find its state.
  */
 typedef struct {
-       int     predictor;              /* predictor tag value */
-       int     stride;                 /* sample stride over data */
-       tsize_t rowsize;                /* tile/strip row size */
+       int             predictor;      /* predictor tag value */
+       int             stride;         /* sample stride over data */
+       tsize_t         rowsize;        /* tile/strip row size */
+
+       TIFFCodeMethod  encoderow;      /* parent codec encode/decode row */
+       TIFFCodeMethod  encodestrip;    /* parent codec encode/decode strip */
+       TIFFCodeMethod  encodetile;     /* parent codec encode/decode tile */ 
+       TIFFPostMethod  encodepfunc;    /* horizontal differencer */
+       TIFFCodeMethod  decoderow;      /* parent codec encode/decode row */
+       TIFFCodeMethod  decodestrip;    /* parent codec encode/decode strip */
+       TIFFCodeMethod  decodetile;     /* parent codec encode/decode tile */ 
+       TIFFPostMethod  decodepfunc;    /* horizontal accumulator */
 
-       TIFFPostMethod  pfunc;          /* horizontal differencer/accumulator */
-       TIFFCodeMethod  coderow;        /* parent codec encode/decode row */
-       TIFFCodeMethod  codestrip;      /* parent codec encode/decode strip */
-       TIFFCodeMethod  codetile;       /* parent codec encode/decode tile */
        TIFFVGetMethod  vgetparent;     /* super-class method */
        TIFFVSetMethod  vsetparent;     /* super-class method */
        TIFFPrintMethod printdir;       /* super-class method */
@@ -55,7 +61,17 @@ typedef struct {
 extern "C" {
 #endif
 extern int TIFFPredictorInit(TIFF*);
+extern int TIFFPredictorCleanup(TIFF*);
 #if defined(__cplusplus)
 }
 #endif
 #endif /* _TIFFPREDICT_ */
+
+/* vim: set ts=8 sts=8 sw=8 noet: */
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
index 84ee5d0..eb4b1e7 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: tif_print.c,v 1.1 2005-06-17 13:54:52 vp153 Exp $ */
+/* $Id: tif_print.c,v 1.36.2.4 2010-06-08 18:50:42 bfriesen Exp $ */
 
 /*
  * Copyright (c) 1988-1997 Sam Leffler
@@ -31,7 +31,7 @@
  */
 #include "tiffiop.h"
 #include <stdio.h>
-
+#include <string.h>
 #include <ctype.h>
 
 static const char *photoNames[] = {
@@ -60,6 +60,133 @@ static const char *orientNames[] = {
 };
 #define        NORIENTNAMES    (sizeof (orientNames) / sizeof (orientNames[0]))
 
+static void
+_TIFFPrintField(FILE* fd, const TIFFFieldInfo *fip,
+               uint32 value_count, void *raw_data)
+{
+       uint32 j;
+               
+       fprintf(fd, "  %s: ", fip->field_name);
+
+       for(j = 0; j < value_count; j++) {
+               if(fip->field_type == TIFF_BYTE)
+                       fprintf(fd, "%u", ((uint8 *) raw_data)[j]);
+               else if(fip->field_type == TIFF_UNDEFINED)
+                       fprintf(fd, "0x%x",
+                               (unsigned int) ((unsigned char *) raw_data)[j]);
+               else if(fip->field_type == TIFF_SBYTE)
+                       fprintf(fd, "%d", ((int8 *) raw_data)[j]);
+               else if(fip->field_type == TIFF_SHORT)
+                       fprintf(fd, "%u", ((uint16 *) raw_data)[j]);
+               else if(fip->field_type == TIFF_SSHORT)
+                       fprintf(fd, "%d", ((int16 *) raw_data)[j]);
+               else if(fip->field_type == TIFF_LONG)
+                       fprintf(fd, "%lu",
+                               (unsigned long)((uint32 *) raw_data)[j]);
+               else if(fip->field_type == TIFF_SLONG)
+                       fprintf(fd, "%ld", (long)((int32 *) raw_data)[j]);
+               else if(fip->field_type == TIFF_RATIONAL
+                       || fip->field_type == TIFF_SRATIONAL
+                       || fip->field_type == TIFF_FLOAT)
+                       fprintf(fd, "%f", ((float *) raw_data)[j]);
+               else if(fip->field_type == TIFF_IFD)
+                       fprintf(fd, "0x%ulx", ((uint32 *) raw_data)[j]);
+               else if(fip->field_type == TIFF_ASCII) {
+                       fprintf(fd, "%s", (char *) raw_data);
+                       break;
+               }
+               else if(fip->field_type == TIFF_DOUBLE)
+                       fprintf(fd, "%f", ((double *) raw_data)[j]);
+               else if(fip->field_type == TIFF_FLOAT)
+                       fprintf(fd, "%f", ((float *)raw_data)[j]);
+               else {
+                       fprintf(fd, "<unsupported data type in TIFFPrint>");
+                       break;
+               }
+
+               if(j < value_count - 1)
+                       fprintf(fd, ",");
+       }
+
+       fprintf(fd, "\n");
+}
+
+static int
+_TIFFPrettyPrintField(TIFF* tif, FILE* fd, ttag_t tag,
+                     uint32 value_count, void *raw_data)
+{
+       TIFFDirectory *td = &tif->tif_dir;
+
+       switch (tag)
+       {
+               case TIFFTAG_INKSET:
+                       fprintf(fd, "  Ink Set: ");
+                       switch (*((uint16*)raw_data)) {
+                               case INKSET_CMYK:
+                                       fprintf(fd, "CMYK\n");
+                                       break;
+                               default:
+                                       fprintf(fd, "%u (0x%x)\n",
+                                               *((uint16*)raw_data),
+                                               *((uint16*)raw_data));
+                                       break;
+                       }
+                       return 1;
+               case TIFFTAG_DOTRANGE:
+                       fprintf(fd, "  Dot Range: %u-%u\n",
+                               ((uint16*)raw_data)[0], ((uint16*)raw_data)[1]);
+                       return 1;
+               case TIFFTAG_WHITEPOINT:
+                       fprintf(fd, "  White Point: %g-%g\n",
+                               ((float *)raw_data)[0], ((float *)raw_data)[1]);                        return 1;
+               case TIFFTAG_REFERENCEBLACKWHITE:
+               {
+                       uint16 i;
+
+                       fprintf(fd, "  Reference Black/White:\n");
+                       for (i = 0; i < 3; i++)
+                       fprintf(fd, "    %2d: %5g %5g\n", i,
+                               ((float *)raw_data)[2*i+0],
+                               ((float *)raw_data)[2*i+1]);
+                       return 1;
+               }
+               case TIFFTAG_XMLPACKET:
+               {
+                       uint32 i;
+                       
+                       fprintf(fd, "  XMLPacket (XMP Metadata):\n" );
+                       for(i = 0; i < value_count; i++)
+                               fputc(((char *)raw_data)[i], fd);
+                       fprintf( fd, "\n" );
+                       return 1;
+               }
+               case TIFFTAG_RICHTIFFIPTC:
+                       /*
+                        * XXX: for some weird reason RichTIFFIPTC tag
+                        * defined as array of LONG values.
+                        */
+                       fprintf(fd,
+                               "  RichTIFFIPTC Data: <present>, %lu bytes\n",
+                               (unsigned long) value_count * 4);
+                       return 1;
+               case TIFFTAG_PHOTOSHOP:
+                       fprintf(fd, "  Photoshop Data: <present>, %lu bytes\n",
+                               (unsigned long) value_count);
+                       return 1;
+               case TIFFTAG_ICCPROFILE:
+                       fprintf(fd, "  ICC Profile: <present>, %lu bytes\n",
+                               (unsigned long) value_count);
+                       return 1;
+               case TIFFTAG_STONITS:
+                       fprintf(fd,
+                               "  Sample to Nits conversion factor: %.4e\n",
+                               *((double*)raw_data));
+                       return 1;
+        }
+
+       return 0;
+}
+
 /*
  * Print the contents of the current directory
  * to the specified stdio file stream.
@@ -67,14 +194,13 @@ static const char *orientNames[] = {
 void
 TIFFPrintDirectory(TIFF* tif, FILE* fd, long flags)
 {
-       register TIFFDirectory *td;
+       TIFFDirectory *td = &tif->tif_dir;
        char *sep;
        uint16 i;
        long l, n;
 
-       fprintf(fd, "TIFF Directory at offset 0x%lx\n",
-               (unsigned long)tif->tif_diroff);
-       td = &tif->tif_dir;
+       fprintf(fd, "TIFF Directory at offset 0x%lx (%lu)\n",
+               (unsigned long)tif->tif_diroff, (unsigned long)tif->tif_diroff);
        if (TIFFFieldSet(tif,FIELD_SUBFILETYPE)) {
                fprintf(fd, "  Subfile Type:");
                sep = " ";
@@ -99,42 +225,6 @@ TIFFPrintDirectory(TIFF* tif, FILE* fd, long flags)
                            (unsigned long) td->td_imagedepth);
                fprintf(fd, "\n");
        }
-
-       /* Begin Pixar */
-       if (TIFFFieldSet(tif,FIELD_IMAGEFULLWIDTH) ||
-           TIFFFieldSet(tif,FIELD_IMAGEFULLLENGTH)) {
-         fprintf(fd, "  Pixar Full Image Width: %lu Full Image Length: %lu\n",
-                 (unsigned long) td->td_imagefullwidth,
-                 (unsigned long) td->td_imagefulllength);
-       }
-       if (TIFFFieldSet(tif,FIELD_TEXTUREFORMAT))
-         _TIFFprintAsciiTag(fd, "Texture Format", td->td_textureformat);
-       if (TIFFFieldSet(tif,FIELD_WRAPMODES))
-         _TIFFprintAsciiTag(fd, "Texture Wrap Modes", td->td_wrapmodes);
-       if (TIFFFieldSet(tif,FIELD_FOVCOT))
-         fprintf(fd, "  Field of View Cotangent: %g\n", td->td_fovcot);
-       if (TIFFFieldSet(tif,FIELD_MATRIX_WORLDTOSCREEN)) {
-         typedef float Matrix[4][4];
-         Matrix*               m = (Matrix*)td->td_matrixWorldToScreen;
-         
-         fprintf(fd, "  Matrix NP:\n\t%g %g %g %g\n\t%g %g %g %g\n\t%g %g %g %g\n\t%g %g %g %g\n",
-                 (*m)[0][0], (*m)[0][1], (*m)[0][2], (*m)[0][3],
-                 (*m)[1][0], (*m)[1][1], (*m)[1][2], (*m)[1][3],
-                 (*m)[2][0], (*m)[2][1], (*m)[2][2], (*m)[2][3],
-                 (*m)[3][0], (*m)[3][1], (*m)[3][2], (*m)[3][3]);
-       }
-       if (TIFFFieldSet(tif,FIELD_MATRIX_WORLDTOCAMERA)) {
-         typedef float Matrix[4][4];
-         Matrix*               m = (Matrix*)td->td_matrixWorldToCamera;
-         
-         fprintf(fd, "  Matrix Nl:\n\t%g %g %g %g\n\t%g %g %g %g\n\t%g %g %g %g\n\t%g %g %g %g\n",
-                 (*m)[0][0], (*m)[0][1], (*m)[0][2], (*m)[0][3],
-                 (*m)[1][0], (*m)[1][1], (*m)[1][2], (*m)[1][3],
-                 (*m)[2][0], (*m)[2][1], (*m)[2][2], (*m)[2][3],
-                 (*m)[3][0], (*m)[3][1], (*m)[3][2], (*m)[3][3]);
-       }
-       /* End Pixar */
-       
        if (TIFFFieldSet(tif,FIELD_TILEDIMENSIONS)) {
                fprintf(fd, "  Tile Width: %lu Tile Length: %lu",
                    (unsigned long) td->td_tilewidth, (unsigned long) td->td_tilelength);
@@ -249,22 +339,6 @@ TIFFPrintDirectory(TIFF* tif, FILE* fd, long flags)
                }
                fprintf(fd, ">\n");
        }
-       if (TIFFFieldSet(tif,FIELD_STONITS)) {
-               fprintf(fd, "  Sample to Nits conversion factor: %.4e\n",
-                               td->td_stonits);
-       }
-       if (TIFFFieldSet(tif,FIELD_INKSET)) {
-               fprintf(fd, "  Ink Set: ");
-               switch (td->td_inkset) {
-               case INKSET_CMYK:
-                       fprintf(fd, "CMYK\n");
-                       break;
-               default:
-                       fprintf(fd, "%u (0x%x)\n",
-                           td->td_inkset, td->td_inkset);
-                       break;
-               }
-       }
        if (TIFFFieldSet(tif,FIELD_INKNAMES)) {
                char* cp;
                fprintf(fd, "  Ink Names: ");
@@ -277,13 +351,6 @@ TIFFPrintDirectory(TIFF* tif, FILE* fd, long flags)
                }
                 fputs("\n", fd);
        }
-       if (TIFFFieldSet(tif,FIELD_NUMBEROFINKS))
-               fprintf(fd, "  Number of Inks: %u\n", td->td_ninks);
-       if (TIFFFieldSet(tif,FIELD_DOTRANGE))
-               fprintf(fd, "  Dot Range: %u-%u\n",
-                   td->td_dotrange[0], td->td_dotrange[1]);
-       if (TIFFFieldSet(tif,FIELD_TARGETPRINTER))
-               _TIFFprintAsciiTag(fd, "Target Printer", td->td_targetprinter);
        if (TIFFFieldSet(tif,FIELD_THRESHHOLDING)) {
                fprintf(fd, "  Thresholding: ");
                switch (td->td_threshholding) {
@@ -346,30 +413,9 @@ TIFFPrintDirectory(TIFF* tif, FILE* fd, long flags)
                        break;
                }
        }
-       if (TIFFFieldSet(tif,FIELD_YCBCRCOEFFICIENTS))
-               fprintf(fd, "  YCbCr Coefficients: %g, %g, %g\n",
-                   td->td_ycbcrcoeffs[0],
-                   td->td_ycbcrcoeffs[1],
-                   td->td_ycbcrcoeffs[2]);
        if (TIFFFieldSet(tif,FIELD_HALFTONEHINTS))
                fprintf(fd, "  Halftone Hints: light %u dark %u\n",
                    td->td_halftonehints[0], td->td_halftonehints[1]);
-       if (TIFFFieldSet(tif,FIELD_ARTIST))
-               _TIFFprintAsciiTag(fd, "Artist", td->td_artist);
-       if (TIFFFieldSet(tif,FIELD_DATETIME))
-               _TIFFprintAsciiTag(fd, "Date & Time", td->td_datetime);
-       if (TIFFFieldSet(tif,FIELD_HOSTCOMPUTER))
-               _TIFFprintAsciiTag(fd, "Host Computer", td->td_hostcomputer);
-       if (TIFFFieldSet(tif,FIELD_COPYRIGHT))
-               _TIFFprintAsciiTag(fd, "Copyright", td->td_copyright);
-       if (TIFFFieldSet(tif,FIELD_DOCUMENTNAME))
-               _TIFFprintAsciiTag(fd, "Document Name", td->td_documentname);
-       if (TIFFFieldSet(tif,FIELD_IMAGEDESCRIPTION))
-               _TIFFprintAsciiTag(fd, "Image Description", td->td_imagedescription);
-       if (TIFFFieldSet(tif,FIELD_MAKE))
-               _TIFFprintAsciiTag(fd, "Make", td->td_make);
-       if (TIFFFieldSet(tif,FIELD_MODEL))
-               _TIFFprintAsciiTag(fd, "Model", td->td_model);
        if (TIFFFieldSet(tif,FIELD_ORIENTATION)) {
                fprintf(fd, "  Orientation: ");
                if (td->td_orientation < NORIENTNAMES)
@@ -412,8 +458,6 @@ TIFFPrintDirectory(TIFF* tif, FILE* fd, long flags)
                        break;
                }
        }
-       if (TIFFFieldSet(tif,FIELD_PAGENAME))
-               _TIFFprintAsciiTag(fd, "Page Name", td->td_pagename);
        if (TIFFFieldSet(tif,FIELD_PAGENUMBER))
                fprintf(fd, "  Page Number: %u-%u\n",
                    td->td_pagenumber[0], td->td_pagenumber[1]);
@@ -431,22 +475,6 @@ TIFFPrintDirectory(TIFF* tif, FILE* fd, long flags)
                } else
                        fprintf(fd, "(present)\n");
        }
-       if (TIFFFieldSet(tif,FIELD_WHITEPOINT))
-               fprintf(fd, "  White Point: %g-%g\n",
-                   td->td_whitepoint[0], td->td_whitepoint[1]);
-       if (TIFFFieldSet(tif,FIELD_PRIMARYCHROMAS))
-               fprintf(fd, "  Primary Chromaticities: %g,%g %g,%g %g,%g\n",
-                   td->td_primarychromas[0], td->td_primarychromas[1],
-                   td->td_primarychromas[2], td->td_primarychromas[3],
-                   td->td_primarychromas[4], td->td_primarychromas[5]);
-       if (TIFFFieldSet(tif,FIELD_REFBLACKWHITE)) {
-               fprintf(fd, "  Reference Black/White:\n");
-               for (i = 0; i < td->td_samplesperpixel; i++)
-                       fprintf(fd, "    %2d: %5g %5g\n",
-                           i,
-                           td->td_refblackwhite[2*i+0],
-                           td->td_refblackwhite[2*i+1]);
-       }
        if (TIFFFieldSet(tif,FIELD_TRANSFERFUNCTION)) {
                fprintf(fd, "  Transfer Function: ");
                if (flags & TIFFPRINT_CURVES) {
@@ -463,27 +491,12 @@ TIFFPrintDirectory(TIFF* tif, FILE* fd, long flags)
                } else
                        fprintf(fd, "(present)\n");
        }
-       if (TIFFFieldSet(tif,FIELD_ICCPROFILE))
-               fprintf(fd, "  ICC Profile: <present>, %lu bytes\n",
-                   (unsigned long) td->td_profileLength);
-       if (TIFFFieldSet(tif,FIELD_PHOTOSHOP))
-               fprintf(fd, "  Photoshop Data: <present>, %lu bytes\n",
-                   (unsigned long) td->td_photoshopLength);
-       if (TIFFFieldSet(tif,FIELD_RICHTIFFIPTC))
-               fprintf(fd, "  RichTIFFIPTC Data: <present>, %lu bytes\n",
-                   (unsigned long) td->td_richtiffiptcLength);
-       if (TIFFFieldSet(tif, FIELD_SUBIFD)) {
+       if (TIFFFieldSet(tif, FIELD_SUBIFD) && (td->td_subifd)) {
                fprintf(fd, "  SubIFD Offsets:");
                for (i = 0; i < td->td_nsubifd; i++)
                        fprintf(fd, " %5lu", (long) td->td_subifd[i]);
                fputc('\n', fd);
        }
-       if (TIFFFieldSet(tif,FIELD_XMLPACKET)) {
-            fprintf(fd, "  XMLPacket (XMP Metadata):\n" );
-            for( i=0; i < td->td_xmlpacketLength; i++ )
-                fputc( ((char *)td->td_xmlpacketData)[i], fd );
-            fprintf( fd, "\n" );
-        }
 
         /*
         ** Custom tag support.
@@ -492,82 +505,89 @@ TIFFPrintDirectory(TIFF* tif, FILE* fd, long flags)
             int  i;
             short count;
 
-            count = (short) TIFFGetTagListCount( tif );
-            for( i = 0; i < count; i++ )
-            {
-                ttag_t  tag = TIFFGetTagListEntry( tif, i );
-                const TIFFFieldInfo *fld;
-
-                fld = TIFFFieldWithTag( tif, tag );
-                if( fld == NULL )
-                    continue;
+            count = (short) TIFFGetTagListCount(tif);
+            for(i = 0; i < count; i++) {
+                ttag_t  tag = TIFFGetTagListEntry(tif, i);
+                const TIFFFieldInfo *fip;
+                uint32 value_count;
+                int mem_alloc = 0;
+                void *raw_data;
 
-                if( fld->field_passcount )
-                {
-                    short value_count;
-                    int j;
-                    void *raw_data;
-                    
-                    if( TIFFGetField( tif, tag, &value_count, &raw_data ) != 1 )
-                        continue;
+                fip = TIFFFieldWithTag(tif, tag);
+                if(fip == NULL)
+                       continue;
 
-                    fprintf(fd, "  %s: ", fld->field_name );
+               if(fip->field_passcount) {
+                       if(TIFFGetField(tif, tag, &value_count, &raw_data) != 1)
+                               continue;
+               } else {
+                       if (fip->field_readcount == TIFF_VARIABLE
+                           || fip->field_readcount == TIFF_VARIABLE2)
+                               value_count = 1;
+                       else if (fip->field_readcount == TIFF_SPP)
+                               value_count = td->td_samplesperpixel;
+                       else
+                               value_count = fip->field_readcount;
+                       if ((fip->field_type == TIFF_ASCII
+                            || fip->field_readcount == TIFF_VARIABLE
+                            || fip->field_readcount == TIFF_VARIABLE2
+                            || fip->field_readcount == TIFF_SPP
+                            || value_count > 1)
+                           && fip->field_tag != TIFFTAG_PAGENUMBER
+                           && fip->field_tag != TIFFTAG_HALFTONEHINTS
+                           && fip->field_tag != TIFFTAG_YCBCRSUBSAMPLING
+                           && fip->field_tag != TIFFTAG_DOTRANGE) {
+                               if(TIFFGetField(tif, tag, &raw_data) != 1)
+                                       continue;
+                       } else if (fip->field_tag != TIFFTAG_PAGENUMBER
+                                  && fip->field_tag != TIFFTAG_HALFTONEHINTS
+                                  && fip->field_tag != TIFFTAG_YCBCRSUBSAMPLING
+                                  && fip->field_tag != TIFFTAG_DOTRANGE) {
+                               raw_data = _TIFFmalloc(
+                                       _TIFFDataSize(fip->field_type)
+                                       * value_count);
+                               mem_alloc = 1;
+                               if(TIFFGetField(tif, tag, raw_data) != 1) {
+                                       _TIFFfree(raw_data);
+                                       continue;
+                               }
+                       } else {
+                               /* 
+                                * XXX: Should be fixed and removed, see the
+                                * notes related to TIFFTAG_PAGENUMBER,
+                                * TIFFTAG_HALFTONEHINTS,
+                                * TIFFTAG_YCBCRSUBSAMPLING and
+                                * TIFFTAG_DOTRANGE tags in tif_dir.c. */
+                               char *tmp;
+                               raw_data = _TIFFmalloc(
+                                       _TIFFDataSize(fip->field_type)
+                                       * value_count);
+                               tmp = raw_data;
+                               mem_alloc = 1;
+                               if(TIFFGetField(tif, tag, tmp,
+                               tmp + _TIFFDataSize(fip->field_type)) != 1) {
+                                       _TIFFfree(raw_data);
+                                       continue;
+                               }
+                       }
+               }
 
-                    for( j = 0; j < value_count; j++ )
-                    {
-                       if( fld->field_type == TIFF_BYTE )
-                            fprintf( fd, "%d",
-                                     (int) ((char *) raw_data)[j] );
-                       else if( fld->field_type == TIFF_SHORT )
-                            fprintf( fd, "%d",
-                                     (int) ((unsigned short *) raw_data)[j] );
-                       else if( fld->field_type == TIFF_SSHORT )
-                            fprintf( fd, "%d",
-                                     (int) ((short *) raw_data)[j] );
-                        else if( fld->field_type == TIFF_LONG )
-                            fprintf( fd, "%d",
-                                     (int) ((unsigned long *) raw_data)[j] );
-                        else if( fld->field_type == TIFF_SLONG )
-                            fprintf( fd, "%d",
-                                     (int) ((long *) raw_data)[j] );
-                       else if( fld->field_type == TIFF_RATIONAL )
-                           fprintf( fd, "%f",
-                                    ((float *) raw_data)[j] );
-                       else if( fld->field_type == TIFF_SRATIONAL )
-                           fprintf( fd, "%f",
-                                    ((float *) raw_data)[j] );
-                        else if( fld->field_type == TIFF_ASCII )
-                        {
-                            fprintf( fd, "%s",
-                                     (char *) raw_data );
-                            break;
-                        }
-                        else if( fld->field_type == TIFF_DOUBLE )
-                            fprintf( fd, "%f",
-                                     ((double *) raw_data)[j] );
-                        else if( fld->field_type == TIFF_FLOAT )
-                            fprintf( fd, "%f",
-                                     ((float *) raw_data)[j] );
-                        else
-                        {
-                            fprintf( fd,
-                                     "<unsupported data type in TIFFPrint>" );
-                            break;
-                        }
+               /*
+                * Catch the tags which needs to be specially handled and
+                * pretty print them. If tag not handled in
+                * _TIFFPrettyPrintField() fall down and print it as any other
+                * tag.
+                */
+               if (_TIFFPrettyPrintField(tif, fd, tag, value_count, raw_data)) {
+                       if(mem_alloc)
+                               _TIFFfree(raw_data);
+                       continue;
+               }
+               else
+                       _TIFFPrintField(fd, fip, value_count, raw_data);
 
-                        if( j < value_count-1 )
-                            fprintf( fd, "," );
-                    }
-                    fprintf( fd, "\n" );
-                } 
-                else if( !fld->field_passcount
-                         && fld->field_type == TIFF_ASCII )
-                {
-                    char *data;
-                    
-                    if( TIFFGetField( tif, tag, &data ) )
-                        fprintf(fd, "  %s: %s\n", fld->field_name, data );
-                }
+               if(mem_alloc)
+                       _TIFFfree(raw_data);
             }
         }
         
@@ -617,3 +637,10 @@ _TIFFprintAsciiTag(FILE* fd, const char* name, const char* value)
 }
 
 /* vim: set ts=8 sts=8 sw=8 noet: */
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
index 39e0173..8ac0ae6 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: tif_read.c,v 1.1 2005-06-17 13:54:52 vp153 Exp $ */
+/* $Id: tif_read.c,v 1.16.2.3 2010-06-09 14:32:47 bfriesen Exp $ */
 
 /*
  * Copyright (c) 1988-1997 Sam Leffler
@@ -50,13 +50,15 @@ TIFFSeek(TIFF* tif, uint32 row, tsample_t sample)
        tstrip_t strip;
 
        if (row >= td->td_imagelength) {        /* out of range */
-               TIFFError(tif->tif_name, "%lu: Row out of range, max %lu",
-                   (unsigned long) row, (unsigned long) td->td_imagelength);
+               TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+                            "%lu: Row out of range, max %lu",
+                            (unsigned long) row,
+                            (unsigned long) td->td_imagelength);
                return (0);
        }
        if (td->td_planarconfig == PLANARCONFIG_SEPARATE) {
                if (sample >= td->td_samplesperpixel) {
-                       TIFFError(tif->tif_name,
+                       TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
                            "%lu: Sample out of range, max %lu",
                            (unsigned long) sample, (unsigned long) td->td_samplesperpixel);
                        return (0);
@@ -64,7 +66,7 @@ TIFFSeek(TIFF* tif, uint32 row, tsample_t sample)
                strip = sample*td->td_stripsperimage + row/td->td_rowsperstrip;
        } else
                strip = row / td->td_rowsperstrip;
-       if (strip != tif->tif_curstrip) {       /* different strip, refill */
+       if (strip != tif->tif_curstrip) {       /* different strip, refill */
                if (!TIFFFillStrip(tif, strip))
                        return (0);
        } else if (row < tif->tif_row) {
@@ -104,8 +106,8 @@ TIFFReadScanline(TIFF* tif, tdata_t buf, uint32 row, tsample_t sample)
                e = (*tif->tif_decoderow)
                    (tif, (tidata_t) buf, tif->tif_scanlinesize, sample);
 
-                /* we are now poised at the beginning of the next row */
-                tif->tif_row = row + 1;
+               /* we are now poised at the beginning of the next row */
+               tif->tif_row = row + 1;
 
                if (e)
                        (*tif->tif_postdecode)(tif, (tidata_t) buf,
@@ -129,22 +131,23 @@ TIFFReadEncodedStrip(TIFF* tif, tstrip_t strip, tdata_t buf, tsize_t size)
        if (!TIFFCheckRead(tif, 0))
                return (-1);
        if (strip >= td->td_nstrips) {
-               TIFFError(tif->tif_name, "%ld: Strip out of range, max %ld",
-                   (long) strip, (long) td->td_nstrips);
+               TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+                            "%ld: Strip out of range, max %ld",
+                            (long) strip, (long) td->td_nstrips);
                return (-1);
        }
        /*
         * Calculate the strip size according to the number of
         * rows in the strip (check for truncated last strip on any
-         * of the separations).
+        * of the separations).
         */
-        if( td->td_rowsperstrip >= td->td_imagelength )
-            strips_per_sep = 1;
-        else
-            strips_per_sep = (td->td_imagelength+td->td_rowsperstrip-1)
-                / td->td_rowsperstrip;
+       if( td->td_rowsperstrip >= td->td_imagelength )
+               strips_per_sep = 1;
+       else
+               strips_per_sep = (td->td_imagelength+td->td_rowsperstrip-1)
+                   / td->td_rowsperstrip;
 
-        sep_strip = strip % strips_per_sep;
+       sep_strip = strip % strips_per_sep;
 
        if (sep_strip != strips_per_sep-1 ||
            (nrows = td->td_imagelength % td->td_rowsperstrip) == 0)
@@ -155,9 +158,9 @@ TIFFReadEncodedStrip(TIFF* tif, tstrip_t strip, tdata_t buf, tsize_t size)
                size = stripsize;
        else if (size > stripsize)
                size = stripsize;
-       if (TIFFFillStrip(tif, strip) 
-            && (*tif->tif_decodestrip)(tif, (tidata_t) buf, size, 
-                         (tsample_t)(strip / td->td_stripsperimage)) > 0 ) {
+       if (TIFFFillStrip(tif, strip)
+           && (*tif->tif_decodestrip)(tif, (tidata_t) buf, size,   
+           (tsample_t)(strip / td->td_stripsperimage)) > 0 ) {
                (*tif->tif_postdecode)(tif, (tidata_t) buf, size);
                return (size);
        } else
@@ -170,11 +173,12 @@ TIFFReadRawStrip1(TIFF* tif,
 {
        TIFFDirectory *td = &tif->tif_dir;
 
+       assert((tif->tif_flags&TIFF_NOREADRAW)==0);
        if (!isMapped(tif)) {
                tsize_t cc;
 
                if (!SeekOK(tif, td->td_stripoffset[strip])) {
-                       TIFFError(module,
+                       TIFFErrorExt(tif->tif_clientdata, module,
                            "%s: Seek error at scanline %lu, strip %lu",
                            tif->tif_name,
                            (unsigned long) tif->tif_row, (unsigned long) strip);
@@ -182,7 +186,7 @@ TIFFReadRawStrip1(TIFF* tif,
                }
                cc = TIFFReadFile(tif, buf, size);
                if (cc != size) {
-                       TIFFError(module,
+                       TIFFErrorExt(tif->tif_clientdata, module,
                "%s: Read error at scanline %lu; got %lu bytes, expected %lu",
                            tif->tif_name,
                            (unsigned long) tif->tif_row,
@@ -192,7 +196,7 @@ TIFFReadRawStrip1(TIFF* tif,
                }
        } else {
                if (td->td_stripoffset[strip] + size > tif->tif_size) {
-                       TIFFError(module,
+                       TIFFErrorExt(tif->tif_clientdata, module,
     "%s: Read error at scanline %lu, strip %lu; got %lu bytes, expected %lu",
                            tif->tif_name,
                            (unsigned long) tif->tif_row,
@@ -215,104 +219,140 @@ TIFFReadRawStrip(TIFF* tif, tstrip_t strip, tdata_t buf, tsize_t size)
 {
        static const char module[] = "TIFFReadRawStrip";
        TIFFDirectory *td = &tif->tif_dir;
-       tsize_t bytecount;
+       /*
+        * FIXME: butecount should have tsize_t type, but for now libtiff
+        * defines tsize_t as a signed 32-bit integer and we are losing
+        * ability to read arrays larger than 2^31 bytes. So we are using
+        * uint32 instead of tsize_t here.
+        */
+       uint32 bytecount;
 
        if (!TIFFCheckRead(tif, 0))
                return ((tsize_t) -1);
        if (strip >= td->td_nstrips) {
-               TIFFError(tif->tif_name, "%lu: Strip out of range, max %lu",
-                   (unsigned long) strip, (unsigned long) td->td_nstrips);
+               TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+                            "%lu: Strip out of range, max %lu",
+                            (unsigned long) strip,
+                            (unsigned long) td->td_nstrips);
+               return ((tsize_t) -1);
+       }
+       if (tif->tif_flags&TIFF_NOREADRAW)
+       {
+               TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+       "Compression scheme does not support access to raw uncompressed data");
                return ((tsize_t) -1);
        }
        bytecount = td->td_stripbytecount[strip];
        if (bytecount <= 0) {
-               TIFFError(tif->tif_name,
+               TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
                    "%lu: Invalid strip byte count, strip %lu",
                    (unsigned long) bytecount, (unsigned long) strip);
                return ((tsize_t) -1);
        }
-       if (size != (tsize_t)-1 && size < bytecount)
+       if (size != (tsize_t)-1 && (uint32)size < bytecount)
                bytecount = size;
        return (TIFFReadRawStrip1(tif, strip, buf, bytecount, module));
 }
 
 /*
- * Read the specified strip and setup for decoding. 
- * The data buffer is expanded, as necessary, to
- * hold the strip's data.
+ * Read the specified strip and setup for decoding. The data buffer is
+ * expanded, as necessary, to hold the strip's data.
  */
 int
 TIFFFillStrip(TIFF* tif, tstrip_t strip)
 {
        static const char module[] = "TIFFFillStrip";
        TIFFDirectory *td = &tif->tif_dir;
-       tsize_t bytecount;
 
-       bytecount = td->td_stripbytecount[strip];
-       if (bytecount <= 0) {
-               TIFFError(tif->tif_name,
-                   "%lu: Invalid strip byte count, strip %lu",
-                   (unsigned long) bytecount, (unsigned long) strip);
-               return (0);
-       }
-       if (isMapped(tif) &&
-           (isFillOrder(tif, td->td_fillorder)
-             || (tif->tif_flags & TIFF_NOBITREV))) {
+       if ((tif->tif_flags&TIFF_NOREADRAW)==0)
+       {
                /*
-                * The image is mapped into memory and we either don't
-                * need to flip bits or the compression routine is going
-                * to handle this operation itself.  In this case, avoid
-                * copying the raw data and instead just reference the
-                * data from the memory mapped file image.  This assumes
-                * that the decompression routines do not modify the
-                * contents of the raw data buffer (if they try to,
-                * the application will get a fault since the file is
-                * mapped read-only).
+                * FIXME: butecount should have tsize_t type, but for now
+                * libtiff defines tsize_t as a signed 32-bit integer and we
+                * are losing ability to read arrays larger than 2^31 bytes.
+                * So we are using uint32 instead of tsize_t here.
                 */
-               if ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata)
-                       _TIFFfree(tif->tif_rawdata);
-               tif->tif_flags &= ~TIFF_MYBUFFER;
-               if ( td->td_stripoffset[strip] + bytecount > tif->tif_size) {
-                       /*
-                        * This error message might seem strange, but it's
-                        * what would happen if a read were done instead.
-                        */
-                       TIFFError(module,
-                   "%s: Read error on strip %lu; got %lu bytes, expected %lu",
-                           tif->tif_name,
-                           (unsigned long) strip,
-                           (unsigned long) tif->tif_size - td->td_stripoffset[strip],
-                           (unsigned long) bytecount);
-                       tif->tif_curstrip = NOSTRIP;
+               uint32 bytecount = td->td_stripbytecount[strip];
+               if (bytecount <= 0) {
+                       TIFFErrorExt(tif->tif_clientdata, module,
+                           "%s: Invalid strip byte count %lu, strip %lu",
+                           tif->tif_name, (unsigned long) bytecount,
+                           (unsigned long) strip);
                        return (0);
                }
-               tif->tif_rawdatasize = bytecount;
-               tif->tif_rawdata = tif->tif_base + td->td_stripoffset[strip];
-       } else {
-               /*
-                * Expand raw data buffer, if needed, to
-                * hold data strip coming from file
-                * (perhaps should set upper bound on
-                *  the size of a buffer we'll use?).
-                */
-               if (bytecount > tif->tif_rawdatasize) {
-                       tif->tif_curstrip = NOSTRIP;
-                       if ((tif->tif_flags & TIFF_MYBUFFER) == 0) {
-                               TIFFError(module,
-                               "%s: Data buffer too small to hold strip %lu",
-                                   tif->tif_name, (unsigned long) strip);
+               if (isMapped(tif) &&
+                   (isFillOrder(tif, td->td_fillorder)
+                   || (tif->tif_flags & TIFF_NOBITREV))) {
+                       /*
+                        * The image is mapped into memory and we either don't
+                        * need to flip bits or the compression routine is
+                        * going to handle this operation itself.  In this
+                        * case, avoid copying the raw data and instead just
+                        * reference the data from the memory mapped file
+                        * image.  This assumes that the decompression
+                        * routines do not modify the contents of the raw data
+                        * buffer (if they try to, the application will get a
+                        * fault since the file is mapped read-only).
+                        */
+                       if ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata)
+                               _TIFFfree(tif->tif_rawdata);
+                       tif->tif_flags &= ~TIFF_MYBUFFER;
+                       /*
+                        * We must check for overflow, potentially causing
+                        * an OOB read. Instead of simple
+                        *
+                        *  td->td_stripoffset[strip]+bytecount > tif->tif_size
+                        *
+                        * comparison (which can overflow) we do the following
+                        * two comparisons:
+                        */
+                       if (bytecount > tif->tif_size ||
+                           td->td_stripoffset[strip] > tif->tif_size - bytecount) {
+                               /*
+                                * This error message might seem strange, but
+                                * it's what would happen if a read were done
+                                * instead.
+                                */
+                               TIFFErrorExt(tif->tif_clientdata, module,
+
+                                       "%s: Read error on strip %lu; "
+                                       "got %lu bytes, expected %lu",
+                                       tif->tif_name, (unsigned long) strip,
+                                       (unsigned long) tif->tif_size - td->td_stripoffset[strip],
+                                       (unsigned long) bytecount);
+                               tif->tif_curstrip = NOSTRIP;
                                return (0);
                        }
-                       if (!TIFFReadBufferSetup(tif, 0,
-                           TIFFroundup(bytecount, 1024)))
+                       tif->tif_rawdatasize = bytecount;
+                       tif->tif_rawdata = tif->tif_base + td->td_stripoffset[strip];
+               } else {
+                       /*
+                        * Expand raw data buffer, if needed, to hold data
+                        * strip coming from file (perhaps should set upper
+                        * bound on the size of a buffer we'll use?).
+                        */
+                       if (bytecount > (uint32)tif->tif_rawdatasize) {
+                               tif->tif_curstrip = NOSTRIP;
+                               if ((tif->tif_flags & TIFF_MYBUFFER) == 0) {
+                                       TIFFErrorExt(tif->tif_clientdata,
+                                                    module,
+                               "%s: Data buffer too small to hold strip %lu",
+                                                    tif->tif_name,
+                                                    (unsigned long) strip);
+                                       return (0);
+                               }
+                               if (!TIFFReadBufferSetup(tif, 0,
+                                   TIFFroundup(bytecount, 1024)))
+                                       return (0);
+                       }
+                       if ((uint32)TIFFReadRawStrip1(tif, strip,
+                               (unsigned char *)tif->tif_rawdata,
+                               bytecount, module) != bytecount)
                                return (0);
+                       if (!isFillOrder(tif, td->td_fillorder) &&
+                           (tif->tif_flags & TIFF_NOBITREV) == 0)
+                               TIFFReverseBits(tif->tif_rawdata, bytecount);
                }
-               if (TIFFReadRawStrip1(tif, strip, (unsigned char *)tif->tif_rawdata,
-                   bytecount, module) != bytecount)
-                       return (0);
-               if (!isFillOrder(tif, td->td_fillorder) &&
-                   (tif->tif_flags & TIFF_NOBITREV) == 0)
-                       TIFFReverseBits(tif->tif_rawdata, bytecount);
        }
        return (TIFFStartStrip(tif, strip));
 }
@@ -349,8 +389,9 @@ TIFFReadEncodedTile(TIFF* tif, ttile_t tile, tdata_t buf, tsize_t size)
        if (!TIFFCheckRead(tif, 1))
                return (-1);
        if (tile >= td->td_nstrips) {
-               TIFFError(tif->tif_name, "%ld: Tile out of range, max %ld",
-                   (long) tile, (unsigned long) td->td_nstrips);
+               TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+                            "%ld: Tile out of range, max %ld",
+                            (long) tile, (unsigned long) td->td_nstrips);
                return (-1);
        }
        if (size == (tsize_t) -1)
@@ -371,11 +412,12 @@ TIFFReadRawTile1(TIFF* tif,
 {
        TIFFDirectory *td = &tif->tif_dir;
 
+       assert((tif->tif_flags&TIFF_NOREADRAW)==0);
        if (!isMapped(tif)) {
                tsize_t cc;
 
                if (!SeekOK(tif, td->td_stripoffset[tile])) {
-                       TIFFError(module,
+                       TIFFErrorExt(tif->tif_clientdata, module,
                            "%s: Seek error at row %ld, col %ld, tile %ld",
                            tif->tif_name,
                            (long) tif->tif_row,
@@ -385,7 +427,7 @@ TIFFReadRawTile1(TIFF* tif,
                }
                cc = TIFFReadFile(tif, buf, size);
                if (cc != size) {
-                       TIFFError(module,
+                       TIFFErrorExt(tif->tif_clientdata, module,
            "%s: Read error at row %ld, col %ld; got %lu bytes, expected %lu",
                            tif->tif_name,
                            (long) tif->tif_row,
@@ -396,7 +438,7 @@ TIFFReadRawTile1(TIFF* tif,
                }
        } else {
                if (td->td_stripoffset[tile] + size > tif->tif_size) {
-                       TIFFError(module,
+                       TIFFErrorExt(tif->tif_clientdata, module,
     "%s: Read error at row %ld, col %ld, tile %ld; got %lu bytes, expected %lu",
                            tif->tif_name,
                            (long) tif->tif_row,
@@ -419,89 +461,121 @@ TIFFReadRawTile(TIFF* tif, ttile_t tile, tdata_t buf, tsize_t size)
 {
        static const char module[] = "TIFFReadRawTile";
        TIFFDirectory *td = &tif->tif_dir;
-       tsize_t bytecount;
+       /*
+        * FIXME: butecount should have tsize_t type, but for now libtiff
+        * defines tsize_t as a signed 32-bit integer and we are losing
+        * ability to read arrays larger than 2^31 bytes. So we are using
+        * uint32 instead of tsize_t here.
+        */
+       uint32 bytecount;
 
        if (!TIFFCheckRead(tif, 1))
                return ((tsize_t) -1);
        if (tile >= td->td_nstrips) {
-               TIFFError(tif->tif_name, "%lu: Tile out of range, max %lu",
+               TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+                            "%lu: Tile out of range, max %lu",
                    (unsigned long) tile, (unsigned long) td->td_nstrips);
                return ((tsize_t) -1);
        }
+       if (tif->tif_flags&TIFF_NOREADRAW)
+       {
+               TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+       "Compression scheme does not support access to raw uncompressed data");
+               return ((tsize_t) -1);
+       }
        bytecount = td->td_stripbytecount[tile];
-       if (size != (tsize_t) -1 && size < bytecount)
+       if (size != (tsize_t) -1 && (uint32)size < bytecount)
                bytecount = size;
        return (TIFFReadRawTile1(tif, tile, buf, bytecount, module));
 }
 
 /*
- * Read the specified tile and setup for decoding. 
- * The data buffer is expanded, as necessary, to
- * hold the tile's data.
+ * Read the specified tile and setup for decoding. The data buffer is
+ * expanded, as necessary, to hold the tile's data.
  */
 int
 TIFFFillTile(TIFF* tif, ttile_t tile)
 {
        static const char module[] = "TIFFFillTile";
        TIFFDirectory *td = &tif->tif_dir;
-       tsize_t bytecount;
 
-       bytecount = td->td_stripbytecount[tile];
-       if (bytecount <= 0) {
-               TIFFError(tif->tif_name,
-                   "%lu: Invalid tile byte count, tile %lu",
-                   (unsigned long) bytecount, (unsigned long) tile);
-               return (0);
-       }
-       if (isMapped(tif) &&
-           (isFillOrder(tif, td->td_fillorder)
-             || (tif->tif_flags & TIFF_NOBITREV))) {
+       if ((tif->tif_flags&TIFF_NOREADRAW)==0)
+       {
                /*
-                * The image is mapped into memory and we either don't
-                * need to flip bits or the compression routine is going
-                * to handle this operation itself.  In this case, avoid
-                * copying the raw data and instead just reference the
-                * data from the memory mapped file image.  This assumes
-                * that the decompression routines do not modify the
-                * contents of the raw data buffer (if they try to,
-                * the application will get a fault since the file is
-                * mapped read-only).
+                * FIXME: butecount should have tsize_t type, but for now
+                * libtiff defines tsize_t as a signed 32-bit integer and we
+                * are losing ability to read arrays larger than 2^31 bytes.
+                * So we are using uint32 instead of tsize_t here.
                 */
-               if ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata)
-                       _TIFFfree(tif->tif_rawdata);
-               tif->tif_flags &= ~TIFF_MYBUFFER;
-               if ( td->td_stripoffset[tile] + bytecount > tif->tif_size) {
-                       tif->tif_curtile = NOTILE;
+               uint32 bytecount = td->td_stripbytecount[tile];
+               if (bytecount <= 0) {
+                       TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+                           "%lu: Invalid tile byte count, tile %lu",
+                           (unsigned long) bytecount, (unsigned long) tile);
                        return (0);
                }
-               tif->tif_rawdatasize = bytecount;
-               tif->tif_rawdata = tif->tif_base + td->td_stripoffset[tile];
-       } else {
-               /*
-                * Expand raw data buffer, if needed, to
-                * hold data tile coming from file
-                * (perhaps should set upper bound on
-                *  the size of a buffer we'll use?).
-                */
-               if (bytecount > tif->tif_rawdatasize) {
-                       tif->tif_curtile = NOTILE;
-                       if ((tif->tif_flags & TIFF_MYBUFFER) == 0) {
-                               TIFFError(module,
-                               "%s: Data buffer too small to hold tile %ld",
-                                   tif->tif_name, (long) tile);
+               if (isMapped(tif) &&
+                   (isFillOrder(tif, td->td_fillorder)
+                    || (tif->tif_flags & TIFF_NOBITREV))) {
+                       /*
+                        * The image is mapped into memory and we either don't
+                        * need to flip bits or the compression routine is
+                        * going to handle this operation itself.  In this
+                        * case, avoid copying the raw data and instead just
+                        * reference the data from the memory mapped file
+                        * image.  This assumes that the decompression
+                        * routines do not modify the contents of the raw data
+                        * buffer (if they try to, the application will get a
+                        * fault since the file is mapped read-only).
+                        */
+                       if ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata)
+                               _TIFFfree(tif->tif_rawdata);
+                       tif->tif_flags &= ~TIFF_MYBUFFER;
+                       /*
+                        * We must check for overflow, potentially causing
+                        * an OOB read. Instead of simple
+                        *
+                        *  td->td_stripoffset[tile]+bytecount > tif->tif_size
+                        *
+                        * comparison (which can overflow) we do the following
+                        * two comparisons:
+                        */
+                       if (bytecount > tif->tif_size ||
+                           td->td_stripoffset[tile] > tif->tif_size - bytecount) {
+                               tif->tif_curtile = NOTILE;
                                return (0);
                        }
-                       if (!TIFFReadBufferSetup(tif, 0,
-                           TIFFroundup(bytecount, 1024)))
+                       tif->tif_rawdatasize = bytecount;
+                       tif->tif_rawdata =
+                               tif->tif_base + td->td_stripoffset[tile];
+               } else {
+                       /*
+                        * Expand raw data buffer, if needed, to hold data
+                        * tile coming from file (perhaps should set upper
+                        * bound on the size of a buffer we'll use?).
+                        */
+                       if (bytecount > (uint32)tif->tif_rawdatasize) {
+                               tif->tif_curtile = NOTILE;
+                               if ((tif->tif_flags & TIFF_MYBUFFER) == 0) {
+                                       TIFFErrorExt(tif->tif_clientdata,
+                                                    module,
+                               "%s: Data buffer too small to hold tile %ld",
+                                                    tif->tif_name,
+                                                    (long) tile);
+                                       return (0);
+                               }
+                               if (!TIFFReadBufferSetup(tif, 0,
+                                   TIFFroundup(bytecount, 1024)))
+                                       return (0);
+                       }
+                       if ((uint32)TIFFReadRawTile1(tif, tile,
+                               (unsigned char *)tif->tif_rawdata,
+                               bytecount, module) != bytecount)
                                return (0);
+                       if (!isFillOrder(tif, td->td_fillorder) &&
+                           (tif->tif_flags & TIFF_NOBITREV) == 0)
+                               TIFFReverseBits(tif->tif_rawdata, bytecount);
                }
-               if (TIFFReadRawTile1(tif, tile,
-                                     (unsigned char *)tif->tif_rawdata,
-                                     bytecount, module) != bytecount)
-                       return (0);
-               if (!isFillOrder(tif, td->td_fillorder) &&
-                   (tif->tif_flags & TIFF_NOBITREV) == 0)
-                       TIFFReverseBits(tif->tif_rawdata, bytecount);
        }
        return (TIFFStartTile(tif, tile));
 }
@@ -520,22 +594,25 @@ TIFFReadBufferSetup(TIFF* tif, tdata_t bp, tsize_t size)
 {
        static const char module[] = "TIFFReadBufferSetup";
 
+       assert((tif->tif_flags&TIFF_NOREADRAW)==0);
        if (tif->tif_rawdata) {
                if (tif->tif_flags & TIFF_MYBUFFER)
                        _TIFFfree(tif->tif_rawdata);
                tif->tif_rawdata = NULL;
        }
+
        if (bp) {
                tif->tif_rawdatasize = size;
                tif->tif_rawdata = (tidata_t) bp;
                tif->tif_flags &= ~TIFF_MYBUFFER;
        } else {
                tif->tif_rawdatasize = TIFFroundup(size, 1024);
-               tif->tif_rawdata = (tidata_t) _TIFFmalloc(tif->tif_rawdatasize);
+               if (tif->tif_rawdatasize > 0)
+                       tif->tif_rawdata = (tidata_t) _TIFFmalloc(tif->tif_rawdatasize);
                tif->tif_flags |= TIFF_MYBUFFER;
        }
-       if (tif->tif_rawdata == NULL) {
-               TIFFError(module,
+       if ((tif->tif_rawdata == NULL) || (tif->tif_rawdatasize == 0)) {
+               TIFFErrorExt(tif->tif_clientdata, module,
                    "%s: No space for data buffer at scanline %ld",
                    tif->tif_name, (long) tif->tif_row);
                tif->tif_rawdatasize = 0;
@@ -560,8 +637,16 @@ TIFFStartStrip(TIFF* tif, tstrip_t strip)
        }
        tif->tif_curstrip = strip;
        tif->tif_row = (strip % td->td_stripsperimage) * td->td_rowsperstrip;
-       tif->tif_rawcp = tif->tif_rawdata;
-       tif->tif_rawcc = td->td_stripbytecount[strip];
+       if (tif->tif_flags&TIFF_NOREADRAW)
+       {
+               tif->tif_rawcp = NULL;
+               tif->tif_rawcc = 0;
+       }
+       else
+       {
+               tif->tif_rawcp = tif->tif_rawdata;
+               tif->tif_rawcc = td->td_stripbytecount[strip];
+       }
        return ((*tif->tif_predecode)(tif,
                        (tsample_t)(strip / td->td_stripsperimage)));
 }
@@ -587,8 +672,16 @@ TIFFStartTile(TIFF* tif, ttile_t tile)
        tif->tif_col =
            (tile % TIFFhowmany(td->td_imagelength, td->td_tilelength)) *
                td->td_tilewidth;
-       tif->tif_rawcp = tif->tif_rawdata;
-       tif->tif_rawcc = td->td_stripbytecount[tile];
+       if (tif->tif_flags&TIFF_NOREADRAW)
+       {
+               tif->tif_rawcp = NULL;
+               tif->tif_rawcc = 0;
+       }
+       else
+       {
+               tif->tif_rawcp = tif->tif_rawdata;
+               tif->tif_rawcc = td->td_stripbytecount[tile];
+       }
        return ((*tif->tif_predecode)(tif,
                        (tsample_t)(tile/td->td_stripsperimage)));
 }
@@ -597,11 +690,11 @@ static int
 TIFFCheckRead(TIFF* tif, int tiles)
 {
        if (tif->tif_mode == O_WRONLY) {
-               TIFFError(tif->tif_name, "File not open for reading");
+               TIFFErrorExt(tif->tif_clientdata, tif->tif_name, "File not open for reading");
                return (0);
        }
        if (tiles ^ isTiled(tif)) {
-               TIFFError(tif->tif_name, tiles ?
+               TIFFErrorExt(tif->tif_clientdata, tif->tif_name, tiles ?
                    "Can not read tiles from a stripped image" :
                    "Can not read scanlines from a tiled image");
                return (0);
@@ -624,6 +717,14 @@ _TIFFSwab16BitData(TIFF* tif, tidata_t buf, tsize_t cc)
 }
 
 void
+_TIFFSwab24BitData(TIFF* tif, tidata_t buf, tsize_t cc)
+{
+    (void) tif;
+    assert((cc % 3) == 0);
+    TIFFSwabArrayOfTriples((uint8*) buf, cc/3);
+}
+
+void
 _TIFFSwab32BitData(TIFF* tif, tidata_t buf, tsize_t cc)
 {
     (void) tif;
@@ -640,3 +741,10 @@ _TIFFSwab64BitData(TIFF* tif, tidata_t buf, tsize_t cc)
 }
 
 /* vim: set ts=8 sts=8 sw=8 noet: */
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
index 5e75218..2a2351b 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: tif_stream.cxx,v 1.1 2005-06-17 13:54:52 vp153 Exp $ */
+/* $Id: tif_stream.cxx,v 1.6.2.1 2009-01-01 00:10:43 bfriesen Exp $ */
 
 /*
  * Copyright (c) 1988-1996 Sam Leffler
 /*
  * TIFF Library UNIX-specific Routines.
  */
-#include <iostream>
-#include <cstring>
 #include "tiffiop.h"
+#include <iostream>
 
+#ifndef __VMS
 using namespace std;
+#endif
 
 class tiffis_data
 {
@@ -110,8 +111,12 @@ _tiffosSeekProc(thandle_t fd, toff_t off, int whence)
        // ostrstream/ostringstream does. In that situation, add intermediate
        // '\0' characters.
        if( os->fail() ) {
+#ifdef __VMS
+               int             old_state;
+#else
                ios::iostate    old_state;
-               toff_t          origin;
+#endif
+               toff_t          origin=0;
 
                old_state = os->rdstate();
                // reset the fail bit or else tellp() won't work below
index 16ba5cc..63dec6b 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: tif_strip.c,v 1.1 2005-06-17 13:54:52 vp153 Exp $ */
+/* $Id: tif_strip.c,v 1.19.2.1 2010-06-08 18:50:43 bfriesen Exp $ */
 
 /*
  * Copyright (c) 1991-1997 Sam Leffler
@@ -41,7 +41,7 @@ summarize(TIFF* tif, size_t summand1, size_t summand2, const char* where)
        uint32  bytes = summand1 + summand2;
 
        if (bytes - summand1 != summand2) {
-               TIFFError(tif->tif_name, "Integer overflow in %s", where);
+               TIFFErrorExt(tif->tif_clientdata, tif->tif_name, "Integer overflow in %s", where);
                bytes = 0;
        }
 
@@ -54,7 +54,7 @@ multiply(TIFF* tif, size_t nmemb, size_t elem_size, const char* where)
        uint32  bytes = nmemb * elem_size;
 
        if (elem_size && bytes / elem_size != nmemb) {
-               TIFFError(tif->tif_name, "Integer overflow in %s", where);
+               TIFFErrorExt(tif->tif_clientdata, tif->tif_name, "Integer overflow in %s", where);
                bytes = 0;
        }
 
@@ -73,7 +73,7 @@ TIFFComputeStrip(TIFF* tif, uint32 row, tsample_t sample)
        strip = row / td->td_rowsperstrip;
        if (td->td_planarconfig == PLANARCONFIG_SEPARATE) {
                if (sample >= td->td_samplesperpixel) {
-                       TIFFError(tif->tif_name,
+                       TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
                            "%lu: Sample out of range, max %lu",
                            (unsigned long) sample, (unsigned long) td->td_samplesperpixel);
                        return ((tstrip_t) 0);
@@ -121,16 +121,17 @@ TIFFVStripSize(TIFF* tif, uint32 nrows)
                 * horizontal/vertical subsampling area include
                 * YCbCr data for the extended image.
                 */
-                uint16 ycbcrsubsampling[2];
-                tsize_t w, scanline, samplingarea;
+               uint16 ycbcrsubsampling[2];
+               tsize_t w, scanline, samplingarea;
 
-                TIFFGetField( tif, TIFFTAG_YCBCRSUBSAMPLING, 
-                              ycbcrsubsampling + 0, 
-                              ycbcrsubsampling + 1 );
+               TIFFGetField( tif, TIFFTAG_YCBCRSUBSAMPLING,
+                             ycbcrsubsampling + 0,
+                             ycbcrsubsampling + 1 );
 
                samplingarea = ycbcrsubsampling[0]*ycbcrsubsampling[1];
                if (samplingarea == 0) {
-                       TIFFError(tif->tif_name, "Invalid YCbCr subsampling");
+                       TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+                                    "Invalid YCbCr subsampling");
                        return 0;
                }
 
@@ -160,7 +161,7 @@ TIFFRawStripSize(TIFF* tif, tstrip_t strip)
        tsize_t bytecount = td->td_stripbytecount[strip];
 
        if (bytecount <= 0) {
-               TIFFError(tif->tif_name,
+               TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
                          "%lu: Invalid strip byte count, strip %lu",
                          (unsigned long) bytecount, (unsigned long) strip);
                bytecount = (tsize_t) -1;
@@ -205,10 +206,11 @@ _TIFFDefaultStripSize(TIFF* tif, uint32 s)
        if ((int32) s < 1) {
                /*
                 * If RowsPerStrip is unspecified, try to break the
-                * image up into strips that are approximately 8Kbytes.
+                * image up into strips that are approximately
+                * STRIP_SIZE_DEFAULT bytes long.
                 */
                tsize_t scanline = TIFFScanlineSize(tif);
-               s = (uint32)(8*1024) / (scanline == 0 ? 1 : scanline);
+               s = (uint32)STRIP_SIZE_DEFAULT / (scanline == 0 ? 1 : scanline);
                if (s == 0)             /* very wide images */
                        s = 1;
        }
@@ -226,7 +228,55 @@ TIFFScanlineSize(TIFF* tif)
 {
        TIFFDirectory *td = &tif->tif_dir;
        tsize_t scanline;
-       
+
+       if (td->td_planarconfig == PLANARCONFIG_CONTIG) {
+               if (td->td_photometric == PHOTOMETRIC_YCBCR
+                   && !isUpSampled(tif)) {
+                       uint16 ycbcrsubsampling[2];
+
+                       TIFFGetField(tif, TIFFTAG_YCBCRSUBSAMPLING,
+                                    ycbcrsubsampling + 0,
+                                    ycbcrsubsampling + 1);
+
+                       if (ycbcrsubsampling[0] == 0) {
+                               TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+                                            "Invalid YCbCr subsampling");
+                               return 0;
+                       }
+
+                       scanline = TIFFroundup(td->td_imagewidth,
+                                              ycbcrsubsampling[0]);
+                       scanline = TIFFhowmany8(multiply(tif, scanline,
+                                                        td->td_bitspersample,
+                                                        "TIFFScanlineSize"));
+                       return ((tsize_t)
+                               summarize(tif, scanline,
+                                         multiply(tif, 2,
+                                               scanline / ycbcrsubsampling[0],
+                                               "TIFFVStripSize"),
+                                         "TIFFVStripSize"));
+               } else {
+                       scanline = multiply(tif, td->td_imagewidth,
+                                           td->td_samplesperpixel,
+                                           "TIFFScanlineSize");
+               }
+       } else
+               scanline = td->td_imagewidth;
+       return ((tsize_t) TIFFhowmany8(multiply(tif, scanline,
+                                               td->td_bitspersample,
+                                               "TIFFScanlineSize")));
+}
+
+/*
+ * Some stuff depends on this older version of TIFFScanlineSize
+ * TODO: resolve this
+ */
+tsize_t
+TIFFOldScanlineSize(TIFF* tif)
+{
+       TIFFDirectory *td = &tif->tif_dir;
+       tsize_t scanline;
+
        scanline = multiply (tif, td->td_bitspersample, td->td_imagewidth,
                             "TIFFScanlineSize");
        if (td->td_planarconfig == PLANARCONFIG_CONTIG)
@@ -236,6 +286,57 @@ TIFFScanlineSize(TIFF* tif)
 }
 
 /*
+ * Return the number of bytes to read/write in a call to
+ * one of the scanline-oriented i/o routines.  Note that
+ * this number may be 1/samples-per-pixel if data is
+ * stored as separate planes.
+ * The ScanlineSize in case of YCbCrSubsampling is defined as the
+ * strip size divided by the strip height, i.e. the size of a pack of vertical
+ * subsampling lines divided by vertical subsampling. It should thus make
+ * sense when multiplied by a multiple of vertical subsampling.
+ * Some stuff depends on this newer version of TIFFScanlineSize
+ * TODO: resolve this
+ */
+tsize_t
+TIFFNewScanlineSize(TIFF* tif)
+{
+       TIFFDirectory *td = &tif->tif_dir;
+       tsize_t scanline;
+
+       if (td->td_planarconfig == PLANARCONFIG_CONTIG) {
+               if (td->td_photometric == PHOTOMETRIC_YCBCR
+                   && !isUpSampled(tif)) {
+                       uint16 ycbcrsubsampling[2];
+
+                       TIFFGetField(tif, TIFFTAG_YCBCRSUBSAMPLING,
+                                    ycbcrsubsampling + 0,
+                                    ycbcrsubsampling + 1);
+
+                       if (ycbcrsubsampling[0]*ycbcrsubsampling[1] == 0) {
+                               TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+                                            "Invalid YCbCr subsampling");
+                               return 0;
+                       }
+
+                       return((tsize_t) ((((td->td_imagewidth+ycbcrsubsampling[0]-1)
+                                           /ycbcrsubsampling[0])
+                                          *(ycbcrsubsampling[0]*ycbcrsubsampling[1]+2)
+                                          *td->td_bitspersample+7)
+                                         /8)/ycbcrsubsampling[1]);
+
+               } else {
+                       scanline = multiply(tif, td->td_imagewidth,
+                                           td->td_samplesperpixel,
+                                           "TIFFScanlineSize");
+               }
+       } else
+               scanline = td->td_imagewidth;
+       return ((tsize_t) TIFFhowmany8(multiply(tif, scanline,
+                                               td->td_bitspersample,
+                                               "TIFFScanlineSize")));
+}
+
+/*
  * Return the number of bytes required to store a complete
  * decoded and packed raster scanline (as opposed to the
  * I/O size returned by TIFFScanlineSize which may be less
@@ -260,3 +361,10 @@ TIFFRasterScanlineSize(TIFF* tif)
 }
 
 /* vim: set ts=8 sts=8 sw=8 noet: */
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
index 713651c..e4f1a6d 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: tif_swab.c,v 1.1 2005-06-17 13:54:52 vp153 Exp $ */
+/* $Id: tif_swab.c,v 1.4.2.1 2010-06-08 18:50:43 bfriesen Exp $ */
 
 /*
  * Copyright (c) 1988-1997 Sam Leffler
@@ -70,6 +70,22 @@ TIFFSwabArrayOfShort(uint16* wp, register unsigned long n)
 }
 #endif
 
+#ifndef TIFFSwabArrayOfTriples
+void
+TIFFSwabArrayOfTriples(uint8* tp, unsigned long n)
+{
+       unsigned char* cp;
+       unsigned char t;
+
+       /* XXX unroll loop some */
+       while (n-- > 0) {
+               cp = (unsigned char*) tp;
+               t = cp[2]; cp[2] = cp[0]; cp[0] = t;
+               tp += 3;
+       }
+}
+#endif
+
 #ifndef TIFFSwabArrayOfLong
 void
 TIFFSwabArrayOfLong(register uint32* lp, register unsigned long n)
@@ -217,3 +233,10 @@ TIFFReverseBits(register unsigned char* cp, register unsigned long n)
 }
 
 /* vim: set ts=8 sts=8 sw=8 noet: */
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
index 955026d..8e7a125 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: tif_thunder.c,v 1.1 2005-06-17 13:54:52 vp153 Exp $ */
+/* $Id: tif_thunder.c,v 1.5.2.1 2010-06-08 18:50:43 bfriesen Exp $ */
 
 /*
  * Copyright (c) 1988-1997 Sam Leffler
@@ -121,7 +121,7 @@ ThunderDecode(TIFF* tif, tidata_t op, tsize_t maxpixels)
        tif->tif_rawcp = (tidata_t) bp;
        tif->tif_rawcc = cc;
        if (npixels != maxpixels) {
-               TIFFError(tif->tif_name,
+               TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
                    "ThunderDecode: %s data at scanline %ld (%lu != %lu)",
                    npixels < maxpixels ? "Not enough" : "Too much",
                    (long) tif->tif_row, (long) npixels, (long) maxpixels);
@@ -156,3 +156,10 @@ TIFFInitThunderScan(TIFF* tif, int scheme)
 #endif /* THUNDER_SUPPORT */
 
 /* vim: set ts=8 sts=8 sw=8 noet: */
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
index d3826b1..d8379e6 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: tif_tile.c,v 1.1 2005-06-17 13:54:52 vp153 Exp $ */
+/* $Id: tif_tile.c,v 1.12.2.1 2010-06-08 18:50:43 bfriesen Exp $ */
 
 /*
  * Copyright (c) 1991-1997 Sam Leffler
@@ -35,13 +35,13 @@ static uint32
 summarize(TIFF* tif, size_t summand1, size_t summand2, const char* where)
 {
        /*
-        * XXX: We are using casting to uint32 here, bacause sizeof(size_t)
+        * XXX: We are using casting to uint32 here, because sizeof(size_t)
         * may be larger than sizeof(uint32) on 64-bit architectures.
         */
        uint32  bytes = summand1 + summand2;
 
        if (bytes - summand1 != summand2) {
-               TIFFError(tif->tif_name, "Integer overflow in %s", where);
+               TIFFErrorExt(tif->tif_clientdata, tif->tif_name, "Integer overflow in %s", where);
                bytes = 0;
        }
 
@@ -54,7 +54,7 @@ multiply(TIFF* tif, size_t nmemb, size_t elem_size, const char* where)
        uint32  bytes = nmemb * elem_size;
 
        if (elem_size && bytes / elem_size != nmemb) {
-               TIFFError(tif->tif_name, "Integer overflow in %s", where);
+               TIFFErrorExt(tif->tif_clientdata, tif->tif_name, "Integer overflow in %s", where);
                bytes = 0;
        }
 
@@ -107,24 +107,32 @@ TIFFCheckTile(TIFF* tif, uint32 x, uint32 y, uint32 z, tsample_t s)
        TIFFDirectory *td = &tif->tif_dir;
 
        if (x >= td->td_imagewidth) {
-               TIFFError(tif->tif_name, "%lu: Col out of range, max %lu",
-                   (unsigned long) x, (unsigned long) td->td_imagewidth);
+               TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+                            "%lu: Col out of range, max %lu",
+                            (unsigned long) x,
+                            (unsigned long) (td->td_imagewidth - 1));
                return (0);
        }
        if (y >= td->td_imagelength) {
-               TIFFError(tif->tif_name, "%lu: Row out of range, max %lu",
-                   (unsigned long) y, (unsigned long) td->td_imagelength);
+               TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+                            "%lu: Row out of range, max %lu",
+                            (unsigned long) y,
+                            (unsigned long) (td->td_imagelength - 1));
                return (0);
        }
        if (z >= td->td_imagedepth) {
-               TIFFError(tif->tif_name, "%lu: Depth out of range, max %lu",
-                   (unsigned long) z, (unsigned long) td->td_imagedepth);
+               TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+                            "%lu: Depth out of range, max %lu",
+                            (unsigned long) z,
+                            (unsigned long) (td->td_imagedepth - 1));
                return (0);
        }
        if (td->td_planarconfig == PLANARCONFIG_SEPARATE &&
            s >= td->td_samplesperpixel) {
-               TIFFError(tif->tif_name, "%lu: Sample out of range, max %lu",
-                   (unsigned long) s, (unsigned long) td->td_samplesperpixel);
+               TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+                            "%lu: Sample out of range, max %lu",
+                            (unsigned long) s,
+                            (unsigned long) (td->td_samplesperpixel - 1));
                return (0);
        }
        return (1);
@@ -209,7 +217,7 @@ TIFFVTileSize(TIFF* tif, uint32 nrows)
                tsize_t samplingarea =
                    td->td_ycbcrsubsampling[0]*td->td_ycbcrsubsampling[1];
                if (samplingarea == 0) {
-                       TIFFError(tif->tif_name, "Invalid YCbCr subsampling");
+                       TIFFErrorExt(tif->tif_clientdata, tif->tif_name, "Invalid YCbCr subsampling");
                        return 0;
                }
                nrows = TIFFroundup(nrows, td->td_ycbcrsubsampling[1]);
@@ -263,3 +271,10 @@ _TIFFDefaultTileSize(TIFF* tif, uint32* tw, uint32* th)
 }
 
 /* vim: set ts=8 sts=8 sw=8 noet: */
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
index 4f1f6b5..b73e80d 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: tif_unix.c,v 1.1 2005-06-17 13:54:52 vp153 Exp $ */
+/* $Id: tif_unix.c,v 1.12.2.1 2010-06-08 18:50:43 bfriesen Exp $ */
 
 /*
  * Copyright (c) 1988-1997 Sam Leffler
  */
 #include "tif_config.h"
 
+#ifdef HAVE_SYS_TYPES_H
+# include <sys/types.h>
+#endif
+
+#include <stdarg.h>
 #include <stdlib.h>
 #include <sys/stat.h>
 
 # include <unistd.h>
 #endif
 
-#if HAVE_FCNTL_H
+#ifdef HAVE_FCNTL_H
 # include <fcntl.h>
 #endif
 
-#if HAVE_SYS_TYPES_H
-# include <sys/types.h>
-#endif
-
-#if HAVE_IO_H
+#ifdef HAVE_IO_H
 # include <io.h>
 #endif
 
@@ -170,7 +171,7 @@ TIFFOpen(const char* name, const char* mode)
        fd = open(name, m, 0666);
 #endif
        if (fd < 0) {
-               TIFFError(module, "%s: Cannot open", name);
+               TIFFErrorExt(0, module, "%s: Cannot open", name);
                return ((TIFF *)0);
        }
 
@@ -181,6 +182,7 @@ TIFFOpen(const char* name, const char* mode)
 }
 
 #ifdef __WIN32__
+#include <windows.h>
 /*
  * Open a TIFF file with a Unicode filename, for read/writing.
  */
@@ -204,7 +206,7 @@ TIFFOpenW(const wchar_t* name, const char* mode)
         
        fd = _wopen(name, m, 0666);
        if (fd < 0) {
-               TIFFError(module, "%s: Cannot open", name);
+               TIFFErrorExt(0, module, "%s: Cannot open", name);
                return ((TIFF *)0);
        }
 
@@ -213,7 +215,7 @@ TIFFOpenW(const wchar_t* name, const char* mode)
        if (mbsize > 0) {
                mbname = _TIFFmalloc(mbsize);
                if (!mbname) {
-                       TIFFError(module,
+                       TIFFErrorExt(0, module,
                        "Can't allocate space for filename conversion buffer");
                        return ((TIFF*)0);
                }
@@ -289,3 +291,10 @@ unixErrorHandler(const char* module, const char* fmt, va_list ap)
        fprintf(stderr, ".\n");
 }
 TIFFErrorHandler _TIFFerrorHandler = unixErrorHandler;
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
index e02e8b6..218dab5 100644 (file)
@@ -1,4 +1,4 @@
-/* $Header: /home/vp/work/opencv-cvsbackup/opencv/3rdparty/libtiff/tif_version.c,v 1.1 2005-06-17 13:54:52 vp153 Exp $ */
+/* $Header: /cvs/maptools/cvsroot/libtiff/libtiff/tif_version.c,v 1.2.2.1 2010-06-08 18:50:43 bfriesen Exp $ */
 /*
  * Copyright (c) 1992-1997 Sam Leffler
  * Copyright (c) 1992-1997 Silicon Graphics, Inc.
@@ -31,3 +31,10 @@ TIFFGetVersion(void)
 {
        return (TIFFVersion);
 }
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
index 8f9a168..fe974d9 100644 (file)
@@ -1,4 +1,4 @@
-/* $Header: /home/vp/work/opencv-cvsbackup/opencv/3rdparty/libtiff/tif_warning.c,v 1.1 2005-06-17 13:54:52 vp153 Exp $ */
+/* $Header: /cvs/maptools/cvsroot/libtiff/libtiff/tif_warning.c,v 1.2.2.1 2010-06-08 18:50:43 bfriesen Exp $ */
 
 /*
  * Copyright (c) 1988-1997 Sam Leffler
@@ -29,6 +29,8 @@
  */
 #include "tiffiop.h"
 
+TIFFErrorHandlerExt _TIFFwarningHandlerExt = NULL;
+
 TIFFErrorHandler
 TIFFSetWarningHandler(TIFFErrorHandler handler)
 {
@@ -37,13 +39,43 @@ TIFFSetWarningHandler(TIFFErrorHandler handler)
        return (prev);
 }
 
+TIFFErrorHandlerExt
+TIFFSetWarningHandlerExt(TIFFErrorHandlerExt handler)
+{
+       TIFFErrorHandlerExt prev = _TIFFwarningHandlerExt;
+       _TIFFwarningHandlerExt = handler;
+       return (prev);
+}
+
 void
 TIFFWarning(const char* module, const char* fmt, ...)
 {
-       if (_TIFFwarningHandler) {
-               va_list ap;
-               va_start(ap, fmt);
+       va_list ap;
+       va_start(ap, fmt);
+       if (_TIFFwarningHandler)
+               (*_TIFFwarningHandler)(module, fmt, ap);
+       if (_TIFFwarningHandlerExt)
+               (*_TIFFwarningHandlerExt)(0, module, fmt, ap);
+       va_end(ap);
+}
+
+void
+TIFFWarningExt(thandle_t fd, const char* module, const char* fmt, ...)
+{
+       va_list ap;
+       va_start(ap, fmt);
+       if (_TIFFwarningHandler)
                (*_TIFFwarningHandler)(module, fmt, ap);
-               va_end(ap);
-       }
+       if (_TIFFwarningHandlerExt)
+               (*_TIFFwarningHandlerExt)(fd, module, fmt, ap);
+       va_end(ap);
 }
+
+
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
index 92b3582..2ab944b 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: tif_win32.c,v 1.1 2005-06-17 13:54:52 vp153 Exp $ */
+/* $Id: tif_win32.c,v 1.21.2.1 2010-06-08 18:50:43 bfriesen Exp $ */
 
 /*
  * Copyright (c) 1988-1997 Sam Leffler
@@ -30,6 +30,8 @@
  */
 #include "tiffiop.h"
 
+#include <windows.h>
+
 static tsize_t
 _tiffReadProc(thandle_t fd, tdata_t buf, tsize_t size)
 {
@@ -51,11 +53,10 @@ _tiffWriteProc(thandle_t fd, tdata_t buf, tsize_t size)
 static toff_t
 _tiffSeekProc(thandle_t fd, toff_t off, int whence)
 {
-       DWORD dwMoveMethod, dwMoveHigh;
+        ULARGE_INTEGER li;
+       DWORD dwMoveMethod;
 
-        /* we use this as a special code, so avoid accepting it */
-        if( off == 0xFFFFFFFF )
-            return 0xFFFFFFFF;
+       li.QuadPart = off;
         
        switch(whence)
        {
@@ -72,9 +73,8 @@ _tiffSeekProc(thandle_t fd, toff_t off, int whence)
                dwMoveMethod = FILE_BEGIN;
                break;
        }
-        dwMoveHigh = 0;
-       return ((toff_t)SetFilePointer(fd, (LONG) off, (PLONG)&dwMoveHigh,
-                                       dwMoveMethod));
+       return ((toff_t)SetFilePointer(fd, (LONG) li.LowPart,
+                                      (PLONG)&li.HighPart, dwMoveMethod));
 }
 
 static int
@@ -89,12 +89,12 @@ _tiffSizeProc(thandle_t fd)
        return ((toff_t)GetFileSize(fd, NULL));
 }
 
-#ifdef __BORLANDC__
-#pragma argsused
-#endif
 static int
 _tiffDummyMapProc(thandle_t fd, tdata_t* pbase, toff_t* psize)
 {
+       (void) fd;
+       (void) pbase;
+       (void) psize;
        return (0);
 }
 
@@ -128,12 +128,12 @@ _tiffMapProc(thandle_t fd, tdata_t* pbase, toff_t* psize)
        return(1);
 }
 
-#ifdef __BORLANDC__
-#pragma argsused
-#endif
 static void
 _tiffDummyUnmapProc(thandle_t fd, tdata_t base, toff_t size)
 {
+       (void) fd;
+       (void) base;
+       (void) size;
 }
 
 static void
@@ -163,6 +163,8 @@ TIFFFdOpen(int ifd, const char* name, const char* mode)
        return (tif);
 }
 
+#ifndef _WIN32_WCE
+
 /*
  * Open a TIFF file for read/writing.
  */
@@ -199,11 +201,11 @@ TIFFOpen(const char* name, const char* mode)
        }
        fd = (thandle_t)CreateFileA(name,
                (m == O_RDONLY)?GENERIC_READ:(GENERIC_READ | GENERIC_WRITE),
-               FILE_SHARE_READ, NULL, dwMode,
+               FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, dwMode,
                (m == O_RDONLY)?FILE_ATTRIBUTE_READONLY:FILE_ATTRIBUTE_NORMAL,
                NULL);
        if (fd == INVALID_HANDLE_VALUE) {
-               TIFFError(module, "%s: Cannot open", name);
+               TIFFErrorExt(0, module, "%s: Cannot open", name);
                return ((TIFF *)0);
        }
 
@@ -244,16 +246,16 @@ TIFFOpenW(const wchar_t* name, const char* mode)
                (m == O_RDONLY)?FILE_ATTRIBUTE_READONLY:FILE_ATTRIBUTE_NORMAL,
                NULL);
        if (fd == INVALID_HANDLE_VALUE) {
-               TIFFError(module, "%S: Cannot open", name);
+               TIFFErrorExt(0, module, "%S: Cannot open", name);
                return ((TIFF *)0);
        }
 
        mbname = NULL;
        mbsize = WideCharToMultiByte(CP_ACP, 0, name, -1, NULL, 0, NULL, NULL);
        if (mbsize > 0) {
-               mbname = _TIFFmalloc(mbsize);
+               mbname = (char *)_TIFFmalloc(mbsize);
                if (!mbname) {
-                       TIFFError(module,
+                       TIFFErrorExt(0, module,
                        "Can't allocate space for filename conversion buffer");
                        return ((TIFF*)0);
                }
@@ -264,12 +266,17 @@ TIFFOpenW(const wchar_t* name, const char* mode)
 
        tif = TIFFFdOpen((int)fd,
                         (mbname != NULL) ? mbname : "<unknown>", mode);
+       if(!tif)
+               CloseHandle(fd);
 
        _TIFFfree(mbname);
 
        return tif;
 }
 
+#endif /* ndef _WIN32_WCE */
+
+
 tdata_t
 _TIFFmalloc(tsize_t s)
 {
@@ -286,26 +293,26 @@ _TIFFfree(tdata_t p)
 tdata_t
 _TIFFrealloc(tdata_t p, tsize_t s)
 {
-        void* pvTmp;
-        tsize_t old;
-
-        if(p == NULL)
-                return ((tdata_t)GlobalAlloc(GMEM_FIXED, s));
-
-        old = GlobalSize(p);
-
-        if (old>=s) {
-                if ((pvTmp = GlobalAlloc(GMEM_FIXED, s)) != NULL) {
-                       CopyMemory(pvTmp, p, s);
-                       GlobalFree(p);
-                }
-        } else {
-                if ((pvTmp = GlobalAlloc(GMEM_FIXED, s)) != NULL) {
-                       CopyMemory(pvTmp, p, old);
-                       GlobalFree(p);
-                }
-        }
-        return ((tdata_t)pvTmp);
+       void* pvTmp;
+       tsize_t old;
+
+       if(p == NULL)
+               return ((tdata_t)GlobalAlloc(GMEM_FIXED, s));
+
+       old = GlobalSize(p);
+
+       if (old>=s) {
+               if ((pvTmp = GlobalAlloc(GMEM_FIXED, s)) != NULL) {
+                       CopyMemory(pvTmp, p, s);
+                       GlobalFree(p);
+               }
+       } else {
+               if ((pvTmp = GlobalAlloc(GMEM_FIXED, s)) != NULL) {
+                       CopyMemory(pvTmp, p, old);
+                       GlobalFree(p);
+               }
+       }
+       return ((tdata_t)pvTmp);
 }
 
 void
@@ -332,6 +339,8 @@ _TIFFmemcmp(const tdata_t p1, const tdata_t p2, tsize_t c)
        return (iTmp);
 }
 
+#ifndef _WIN32_WCE
+
 static void
 Win32WarningHandler(const char* module, const char* fmt, va_list ap)
 {
@@ -387,4 +396,13 @@ Win32ErrorHandler(const char* module, const char* fmt, va_list ap)
 }
 TIFFErrorHandler _TIFFerrorHandler = Win32ErrorHandler;
 
+#endif /* ndef _WIN32_WCE */
+
 /* vim: set ts=8 sts=8 sw=8 noet: */
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
index d4d3112..bd08418 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: tif_write.c,v 1.1 2005-06-17 13:54:52 vp153 Exp $ */
+/* $Id: tif_write.c,v 1.22.2.5 2010-06-08 18:50:43 bfriesen Exp $ */
 
 /*
  * Copyright (c) 1988-1997 Sam Leffler
@@ -69,7 +69,7 @@ TIFFWriteScanline(TIFF* tif, tdata_t buf, uint32 row, tsample_t sample)
         */
        if (row >= td->td_imagelength) {        /* extend image */
                if (td->td_planarconfig == PLANARCONFIG_SEPARATE) {
-                       TIFFError(tif->tif_name,
+                       TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
                "Can not change \"ImageLength\" when using separate planes");
                        return (-1);
                }
@@ -81,7 +81,7 @@ TIFFWriteScanline(TIFF* tif, tdata_t buf, uint32 row, tsample_t sample)
         */
        if (td->td_planarconfig == PLANARCONFIG_SEPARATE) {
                if (sample >= td->td_samplesperpixel) {
-                       TIFFError(tif->tif_name,
+                       TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
                            "%d: Sample out of range, max %d",
                            sample, td->td_samplesperpixel);
                        return (-1);
@@ -89,6 +89,15 @@ TIFFWriteScanline(TIFF* tif, tdata_t buf, uint32 row, tsample_t sample)
                strip = sample*td->td_stripsperimage + row/td->td_rowsperstrip;
        } else
                strip = row / td->td_rowsperstrip;
+       /*
+        * Check strip array to make sure there's space. We don't support
+        * dynamically growing files that have data organized in separate
+        * bitplanes because it's too painful.  In that case we require that
+        * the imagelength be set properly before the first write (so that the
+        * strips array will be fully allocated above).
+        */
+       if (strip >= td->td_nstrips && !TIFFGrowStrips(tif, 1, module))
+               return (-1);
        if (strip != tif->tif_curstrip) {
                /*
                 * Changing strips -- flush any data present.
@@ -97,9 +106,9 @@ TIFFWriteScanline(TIFF* tif, tdata_t buf, uint32 row, tsample_t sample)
                        return (-1);
                tif->tif_curstrip = strip;
                /*
-                * Watch out for a growing image.  The value of
-                * strips/image will initially be 1 (since it
-                * can't be deduced until the imagelength is known).
+                * Watch out for a growing image.  The value of strips/image
+                * will initially be 1 (since it can't be deduced until the
+                * imagelength is known).
                 */
                if (strip >= td->td_stripsperimage && imagegrew)
                        td->td_stripsperimage =
@@ -129,17 +138,6 @@ TIFFWriteScanline(TIFF* tif, tdata_t buf, uint32 row, tsample_t sample)
                tif->tif_flags |= TIFF_POSTENCODE;
        }
        /*
-        * Check strip array to make sure there's space.
-        * We don't support dynamically growing files that
-        * have data organized in separate bitplanes because
-        * it's too painful.  In that case we require that
-        * the imagelength be set properly before the first
-        * write (so that the strips array will be fully
-        * allocated above).
-        */
-       if (strip >= td->td_nstrips && !TIFFGrowStrips(tif, 1, module))
-               return (-1);
-       /*
         * Ensure the write is either sequential or at the
         * beginning of a strip (or that we can randomly
         * access the data -- i.e. no encoding).
@@ -200,7 +198,7 @@ TIFFWriteEncodedStrip(TIFF* tif, tstrip_t strip, tdata_t data, tsize_t cc)
         */
        if (strip >= td->td_nstrips) {
                if (td->td_planarconfig == PLANARCONFIG_SEPARATE) {
-                       TIFFError(tif->tif_name,
+                       TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
                "Can not grow image by strips when using separate planes");
                        return ((tsize_t) -1);
                }
@@ -229,10 +227,8 @@ TIFFWriteEncodedStrip(TIFF* tif, tstrip_t strip, tdata_t data, tsize_t cc)
 
         if( td->td_stripbytecount[strip] > 0 )
         {
-            /* if we are writing over existing tiles, zero length. */
-            td->td_stripbytecount[strip] = 0;
-
-            /* this forces TIFFAppendToStrip() to do a seek */
+           /* Force TIFFAppendToStrip() to consider placing data at end
+               of file. */
             tif->tif_curoff = 0;
         }
         
@@ -283,7 +279,7 @@ TIFFWriteRawStrip(TIFF* tif, tstrip_t strip, tdata_t data, tsize_t cc)
         */
        if (strip >= td->td_nstrips) {
                if (td->td_planarconfig == PLANARCONFIG_SEPARATE) {
-                       TIFFError(tif->tif_name,
+                       TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
                "Can not grow image by strips when using separate planes");
                        return ((tsize_t) -1);
                }
@@ -347,7 +343,7 @@ TIFFWriteEncodedTile(TIFF* tif, ttile_t tile, tdata_t data, tsize_t cc)
                return ((tsize_t) -1);
        td = &tif->tif_dir;
        if (tile >= td->td_nstrips) {
-               TIFFError(module, "%s: Tile %lu out of range, max %lu",
+               TIFFErrorExt(tif->tif_clientdata, module, "%s: Tile %lu out of range, max %lu",
                    tif->tif_name, (unsigned long) tile, (unsigned long) td->td_nstrips);
                return ((tsize_t) -1);
        }
@@ -365,10 +361,8 @@ TIFFWriteEncodedTile(TIFF* tif, ttile_t tile, tdata_t data, tsize_t cc)
 
         if( td->td_stripbytecount[tile] > 0 )
         {
-            /* if we are writing over existing tiles, zero length. */
-            td->td_stripbytecount[tile] = 0;
-
-            /* this forces TIFFAppendToStrip() to do a seek */
+           /* Force TIFFAppendToStrip() to consider placing data at end
+               of file. */
             tif->tif_curoff = 0;
         }
         
@@ -433,7 +427,7 @@ TIFFWriteRawTile(TIFF* tif, ttile_t tile, tdata_t data, tsize_t cc)
        if (!WRITECHECKTILES(tif, module))
                return ((tsize_t) -1);
        if (tile >= tif->tif_dir.td_nstrips) {
-               TIFFError(module, "%s: Tile %lu out of range, max %lu",
+               TIFFErrorExt(tif->tif_clientdata, module, "%s: Tile %lu out of range, max %lu",
                    tif->tif_name, (unsigned long) tile,
                    (unsigned long) tif->tif_dir.td_nstrips);
                return ((tsize_t) -1);
@@ -489,12 +483,12 @@ int
 TIFFWriteCheck(TIFF* tif, int tiles, const char* module)
 {
        if (tif->tif_mode == O_RDONLY) {
-               TIFFError(module, "%s: File not open for writing",
+               TIFFErrorExt(tif->tif_clientdata, module, "%s: File not open for writing",
                    tif->tif_name);
                return (0);
        }
        if (tiles ^ isTiled(tif)) {
-               TIFFError(tif->tif_name, tiles ?
+               TIFFErrorExt(tif->tif_clientdata, tif->tif_name, tiles ?
                    "Can not write tiles to a stripped image" :
                    "Can not write scanlines to a tiled image");
                return (0);
@@ -511,20 +505,31 @@ TIFFWriteCheck(TIFF* tif, int tiles, const char* module)
         * the image's length to be changed).
         */
        if (!TIFFFieldSet(tif, FIELD_IMAGEDIMENSIONS)) {
-               TIFFError(module,
+               TIFFErrorExt(tif->tif_clientdata, module,
                    "%s: Must set \"ImageWidth\" before writing data",
                    tif->tif_name);
                return (0);
        }
-       if (!TIFFFieldSet(tif, FIELD_PLANARCONFIG)) {
-               TIFFError(module,
-           "%s: Must set \"PlanarConfiguration\" before writing data",
-                   tif->tif_name);
-               return (0);
+       if (tif->tif_dir.td_samplesperpixel == 1) {
+               /* 
+                * Planarconfiguration is irrelevant in case of single band
+                * images and need not be included. We will set it anyway,
+                * because this field is used in other parts of library even
+                * in the single band case.
+                */
+               if (!TIFFFieldSet(tif, FIELD_PLANARCONFIG))
+                    tif->tif_dir.td_planarconfig = PLANARCONFIG_CONTIG;
+       } else {
+               if (!TIFFFieldSet(tif, FIELD_PLANARCONFIG)) {
+                       TIFFErrorExt(tif->tif_clientdata, module,
+                   "%s: Must set \"PlanarConfiguration\" before writing data",
+                           tif->tif_name);
+                       return (0);
+               }
        }
        if (tif->tif_dir.td_stripoffset == NULL && !TIFFSetupStrips(tif)) {
                tif->tif_dir.td_nstrips = 0;
-               TIFFError(module, "%s: No space for %s arrays",
+               TIFFErrorExt(tif->tif_clientdata, module, "%s: No space for %s arrays",
                    tif->tif_name, isTiled(tif) ? "tile" : "strip");
                return (0);
        }
@@ -562,7 +567,7 @@ TIFFWriteBufferSetup(TIFF* tif, tdata_t bp, tsize_t size)
        if (bp == NULL) {
                bp = _TIFFmalloc(size);
                if (bp == NULL) {
-                       TIFFError(module, "%s: No space for output buffer",
+                       TIFFErrorExt(tif->tif_clientdata, module, "%s: No space for output buffer",
                            tif->tif_name);
                        return (0);
                }
@@ -597,7 +602,7 @@ TIFFGrowStrips(TIFF* tif, int delta, const char* module)
                if (new_stripbytecount)
                        _TIFFfree(new_stripbytecount);
                td->td_nstrips = 0;
-               TIFFError(module, "%s: No space to expand strip arrays",
+               TIFFErrorExt(tif->tif_clientdata, module, "%s: No space to expand strip arrays",
                          tif->tif_name);
                return (0);
        }
@@ -617,64 +622,53 @@ TIFFGrowStrips(TIFF* tif, int delta, const char* module)
 static int
 TIFFAppendToStrip(TIFF* tif, tstrip_t strip, tidata_t data, tsize_t cc)
 {
-       TIFFDirectory *td = &tif->tif_dir;
        static const char module[] = "TIFFAppendToStrip";
+       TIFFDirectory *td = &tif->tif_dir;
 
        if (td->td_stripoffset[strip] == 0 || tif->tif_curoff == 0) {
-               /*
-                * No current offset, set the current strip.
-                */
-               assert(td->td_nstrips > 0);
-               if (td->td_stripoffset[strip] != 0) {
-                       /*
-                        * Prevent overlapping of the data chunks. We need
-                         * this to enable in place updating of the compressed
-                         * images. Larger blocks will be moved at the end of
-                         * the file without any optimization of the spare
-                         * space, so such scheme is not too much effective.
-                        */
-                       if (td->td_stripbytecountsorted) {
-                               if (strip == td->td_nstrips - 1
-                                   || td->td_stripoffset[strip + 1] <
-                                       td->td_stripoffset[strip] + cc) {
-                                       td->td_stripoffset[strip] =
-                                               TIFFSeekFile(tif, (toff_t)0,
-                                                            SEEK_END);
-                               }
-                       } else {
-                               tstrip_t i;
-                               for (i = 0; i < td->td_nstrips; i++) {
-                                       if (td->td_stripoffset[i] > 
-                                               td->td_stripoffset[strip]
-                                           && td->td_stripoffset[i] <
-                                               td->td_stripoffset[strip] + cc) {
-                                               td->td_stripoffset[strip] =
-                                                       TIFFSeekFile(tif,
-                                                                    (toff_t)0,
-                                                                    SEEK_END);
-                                       }
-                               }
-                       }
-
-                       if (!SeekOK(tif, td->td_stripoffset[strip])) {
-                               TIFFError(module,
-                                         "%s: Seek error at scanline %lu",
-                                         tif->tif_name,
-                                         (unsigned long)tif->tif_row);
-                               return (0);
-                       }
-               } else
-                       td->td_stripoffset[strip] =
-                           TIFFSeekFile(tif, (toff_t) 0, SEEK_END);
-               tif->tif_curoff = td->td_stripoffset[strip];
+            assert(td->td_nstrips > 0);
+
+            if( td->td_stripbytecount[strip] != 0 
+                && td->td_stripoffset[strip] != 0 
+                && td->td_stripbytecount[strip] >= cc )
+            {
+                /* 
+                 * There is already tile data on disk, and the new tile
+                 * data we have to will fit in the same space.  The only 
+                 * aspect of this that is risky is that there could be
+                 * more data to append to this strip before we are done
+                 * depending on how we are getting called.
+                 */
+                if (!SeekOK(tif, td->td_stripoffset[strip])) {
+                    TIFFErrorExt(tif->tif_clientdata, module,
+                                 "Seek error at scanline %lu",
+                                 (unsigned long)tif->tif_row);
+                    return (0);
+                }
+            }
+            else
+            {
+                /* 
+                 * Seek to end of file, and set that as our location to 
+                 * write this strip.
+                 */
+                td->td_stripoffset[strip] = TIFFSeekFile(tif, 0, SEEK_END);
+            }
+
+            tif->tif_curoff = td->td_stripoffset[strip];
+
+            /*
+             * We are starting a fresh strip/tile, so set the size to zero.
+             */
+            td->td_stripbytecount[strip] = 0;
        }
 
        if (!WriteOK(tif, data, cc)) {
-               TIFFError(module, "%s: Write error at scanline %lu",
-                   tif->tif_name, (unsigned long) tif->tif_row);
-               return (0);
+               TIFFErrorExt(tif->tif_clientdata, module, "Write error at scanline %lu",
+                   (unsigned long) tif->tif_row);
+                   return (0);
        }
-       tif->tif_curoff += cc;
+       tif->tif_curoff =  tif->tif_curoff+cc;
        td->td_stripbytecount[strip] += cc;
        return (1);
 }
@@ -715,3 +709,10 @@ TIFFSetWriteOffset(TIFF* tif, toff_t off)
 }
 
 /* vim: set ts=8 sts=8 sw=8 noet: */
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
index 8e4c6bf..15091f8 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: tif_zip.c,v 1.1 2005-06-17 13:54:52 vp153 Exp $ */
+/* $Id: tif_zip.c,v 1.11.2.4 2010-06-08 18:50:43 bfriesen Exp $ */
 
 /*
  * Copyright (c) 1995-1997 Sam Leffler
@@ -70,7 +70,8 @@ typedef       struct {
        z_stream        stream;
        int             zipquality;             /* compression level */
        int             state;                  /* state flags */
-#define        ZSTATE_INIT     0x1             /* zlib setup successfully */
+#define ZSTATE_INIT_DECODE 0x01
+#define ZSTATE_INIT_ENCODE 0x02
 
        TIFFVGetMethod  vgetparent;             /* super-class method */
        TIFFVSetMethod  vsetparent;             /* super-class method */
@@ -90,11 +91,18 @@ ZIPSetupDecode(TIFF* tif)
        static const char module[] = "ZIPSetupDecode";
 
        assert(sp != NULL);
+        
+        /* if we were last encoding, terminate this mode */
+       if (sp->state & ZSTATE_INIT_ENCODE) {
+            deflateEnd(&sp->stream);
+            sp->state = 0;
+        }
+
        if (inflateInit(&sp->stream) != Z_OK) {
-               TIFFError(module, "%s: %s", tif->tif_name, sp->stream.msg);
+               TIFFErrorExt(tif->tif_clientdata, module, "%s: %s", tif->tif_name, sp->stream.msg);
                return (0);
        } else {
-               sp->state |= ZSTATE_INIT;
+               sp->state |= ZSTATE_INIT_DECODE;
                return (1);
        }
 }
@@ -109,6 +117,10 @@ ZIPPreDecode(TIFF* tif, tsample_t s)
 
        (void) s;
        assert(sp != NULL);
+
+        if( (sp->state & ZSTATE_INIT_DECODE) == 0 )
+            tif->tif_setupdecode( tif );
+
        sp->stream.next_in = tif->tif_rawdata;
        sp->stream.avail_in = tif->tif_rawcc;
        return (inflateReset(&sp->stream) == Z_OK);
@@ -122,6 +134,8 @@ ZIPDecode(TIFF* tif, tidata_t op, tsize_t occ, tsample_t s)
 
        (void) s;
        assert(sp != NULL);
+        assert(sp->state == ZSTATE_INIT_DECODE);
+
        sp->stream.next_out = op;
        sp->stream.avail_out = occ;
        do {
@@ -129,7 +143,7 @@ ZIPDecode(TIFF* tif, tidata_t op, tsize_t occ, tsample_t s)
                if (state == Z_STREAM_END)
                        break;
                if (state == Z_DATA_ERROR) {
-                       TIFFError(module,
+                       TIFFErrorExt(tif->tif_clientdata, module,
                            "%s: Decoding error at scanline %d, %s",
                            tif->tif_name, tif->tif_row, sp->stream.msg);
                        if (inflateSync(&sp->stream) != Z_OK)
@@ -137,13 +151,13 @@ ZIPDecode(TIFF* tif, tidata_t op, tsize_t occ, tsample_t s)
                        continue;
                }
                if (state != Z_OK) {
-                       TIFFError(module, "%s: zlib error: %s",
+                       TIFFErrorExt(tif->tif_clientdata, module, "%s: zlib error: %s",
                            tif->tif_name, sp->stream.msg);
                        return (0);
                }
        } while (sp->stream.avail_out > 0);
        if (sp->stream.avail_out != 0) {
-               TIFFError(module,
+               TIFFErrorExt(tif->tif_clientdata, module,
                    "%s: Not enough data at scanline %d (short %d bytes)",
                    tif->tif_name, tif->tif_row, sp->stream.avail_out);
                return (0);
@@ -158,11 +172,16 @@ ZIPSetupEncode(TIFF* tif)
        static const char module[] = "ZIPSetupEncode";
 
        assert(sp != NULL);
+       if (sp->state & ZSTATE_INIT_DECODE) {
+            inflateEnd(&sp->stream);
+            sp->state = 0;
+        }
+
        if (deflateInit(&sp->stream, sp->zipquality) != Z_OK) {
-               TIFFError(module, "%s: %s", tif->tif_name, sp->stream.msg);
+               TIFFErrorExt(tif->tif_clientdata, module, "%s: %s", tif->tif_name, sp->stream.msg);
                return (0);
        } else {
-               sp->state |= ZSTATE_INIT;
+               sp->state |= ZSTATE_INIT_ENCODE;
                return (1);
        }
 }
@@ -177,6 +196,9 @@ ZIPPreEncode(TIFF* tif, tsample_t s)
 
        (void) s;
        assert(sp != NULL);
+        if( sp->state != ZSTATE_INIT_ENCODE )
+            tif->tif_setupencode( tif );
+
        sp->stream.next_out = tif->tif_rawdata;
        sp->stream.avail_out = tif->tif_rawdatasize;
        return (deflateReset(&sp->stream) == Z_OK);
@@ -191,12 +213,15 @@ ZIPEncode(TIFF* tif, tidata_t bp, tsize_t cc, tsample_t s)
        ZIPState *sp = EncoderState(tif);
        static const char module[] = "ZIPEncode";
 
+        assert(sp != NULL);
+        assert(sp->state == ZSTATE_INIT_ENCODE);
+
        (void) s;
        sp->stream.next_in = bp;
        sp->stream.avail_in = cc;
        do {
                if (deflate(&sp->stream, Z_NO_FLUSH) != Z_OK) {
-                       TIFFError(module, "%s: Encoder error: %s",
+                       TIFFErrorExt(tif->tif_clientdata, module, "%s: Encoder error: %s",
                            tif->tif_name, sp->stream.msg);
                        return (0);
                }
@@ -237,7 +262,7 @@ ZIPPostEncode(TIFF* tif)
                    }
                    break;
                default:
-                   TIFFError(module, "%s: zlib error: %s",
+                       TIFFErrorExt(tif->tif_clientdata, module, "%s: zlib error: %s",
                        tif->tif_name, sp->stream.msg);
                    return (0);
                }
@@ -249,17 +274,25 @@ static void
 ZIPCleanup(TIFF* tif)
 {
        ZIPState* sp = ZState(tif);
-       if (sp) {
-               if (sp->state&ZSTATE_INIT) {
-                       /* NB: avoid problems in the library */
-                       if (tif->tif_mode == O_RDONLY)
-                               inflateEnd(&sp->stream);
-                       else
-                               deflateEnd(&sp->stream);
-               }
-               _TIFFfree(sp);
-               tif->tif_data = NULL;
+
+       assert(sp != 0);
+
+       (void)TIFFPredictorCleanup(tif);
+
+       tif->tif_tagmethods.vgetfield = sp->vgetparent;
+       tif->tif_tagmethods.vsetfield = sp->vsetparent;
+
+       if (sp->state & ZSTATE_INIT_ENCODE) {
+            deflateEnd(&sp->stream);
+            sp->state = 0;
+        } else if( sp->state & ZSTATE_INIT_DECODE) {
+            inflateEnd(&sp->stream);
+            sp->state = 0;
        }
+       _TIFFfree(sp);
+       tif->tif_data = NULL;
+
+       _TIFFSetDefaultCompressionState(tif);
 }
 
 static int
@@ -271,10 +304,10 @@ ZIPVSetField(TIFF* tif, ttag_t tag, va_list ap)
        switch (tag) {
        case TIFFTAG_ZIPQUALITY:
                sp->zipquality = va_arg(ap, int);
-               if (tif->tif_mode != O_RDONLY && (sp->state&ZSTATE_INIT)) {
+               if ( sp->state&ZSTATE_INIT_ENCODE ) {
                        if (deflateParams(&sp->stream,
                            sp->zipquality, Z_DEFAULT_STRATEGY) != Z_OK) {
-                               TIFFError(module, "%s: zlib error: %s",
+                               TIFFErrorExt(tif->tif_clientdata, module, "%s: zlib error: %s",
                                    tif->tif_name, sp->stream.msg);
                                return (0);
                        }
@@ -305,14 +338,25 @@ static const TIFFFieldInfo zipFieldInfo[] = {
     { TIFFTAG_ZIPQUALITY,       0, 0,  TIFF_ANY,       FIELD_PSEUDO,
       TRUE,    FALSE,  "" },
 };
-#define        N(a)    (sizeof (a) / sizeof (a[0]))
 
 int
 TIFFInitZIP(TIFF* tif, int scheme)
 {
+       static const char module[] = "TIFFInitZIP";
        ZIPState* sp;
 
-       assert( (scheme == COMPRESSION_DEFLATE) || (scheme == COMPRESSION_ADOBE_DEFLATE));
+       assert( (scheme == COMPRESSION_DEFLATE)
+               || (scheme == COMPRESSION_ADOBE_DEFLATE));
+
+       /*
+        * Merge codec-specific tag information.
+        */
+       if (!_TIFFMergeFieldInfo(tif, zipFieldInfo,
+                                TIFFArrayCount(zipFieldInfo))) {
+               TIFFErrorExt(tif->tif_clientdata, module,
+                            "Merging Deflate codec-specific tags failed");
+               return 0;
+       }
 
        /*
         * Allocate state block so tag methods have storage to record values.
@@ -327,14 +371,12 @@ TIFFInitZIP(TIFF* tif, int scheme)
        sp->stream.data_type = Z_BINARY;
 
        /*
-        * Merge codec-specific tag information and
-        * override parent get/set field methods.
+        * Override parent get/set field methods.
         */
-       _TIFFMergeFieldInfo(tif, zipFieldInfo, N(zipFieldInfo));
        sp->vgetparent = tif->tif_tagmethods.vgetfield;
-       tif->tif_tagmethods.vgetfield = ZIPVGetField;   /* hook for codec tags */
+       tif->tif_tagmethods.vgetfield = ZIPVGetField; /* hook for codec tags */
        sp->vsetparent = tif->tif_tagmethods.vsetfield;
-       tif->tif_tagmethods.vsetfield = ZIPVSetField;   /* hook for codec tags */
+       tif->tif_tagmethods.vsetfield = ZIPVSetField; /* hook for codec tags */
 
        /* Default values for codec-specific fields */
        sp->zipquality = Z_DEFAULT_COMPRESSION; /* default comp. level */
@@ -361,9 +403,17 @@ TIFFInitZIP(TIFF* tif, int scheme)
        (void) TIFFPredictorInit(tif);
        return (1);
 bad:
-       TIFFError("TIFFInitZIP", "No space for ZIP state block");
+       TIFFErrorExt(tif->tif_clientdata, module,
+                    "No space for ZIP state block");
        return (0);
 }
 #endif /* ZIP_SUPORT */
 
 /* vim: set ts=8 sts=8 sw=8 noet: */
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
index 564c9b5..ee3fd32 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: tiffio.hxx,v 1.1 2005-06-17 13:54:52 vp153 Exp $ */
+/* $Id: tiffio.hxx,v 1.1.2.1 2010-06-08 18:50:43 bfriesen Exp $ */
 
 /*
  * Copyright (c) 1988-1997 Sam Leffler
@@ -40,3 +40,10 @@ extern       TIFF* TIFFStreamOpen(const char*, std::istream *);
 #endif /* _TIFFIO_HXX_ */
 
 /* vim: set ts=8 sts=8 sw=8 noet: */
+/*
+ * Local Variables:
+ * mode: c++
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
index 371532a..a064039 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: tiffiop.h,v 1.1 2005-06-17 13:54:52 vp153 Exp $ */
+/* $Id: tiffiop.h,v 1.51.2.6 2010-06-12 02:55:16 bfriesen Exp $ */
 
 /*
  * Copyright (c) 1988-1997 Sam Leffler
 
 #include "tif_config.h"
 
-#if HAVE_FCNTL_H
+#ifdef HAVE_FCNTL_H
 # include <fcntl.h>
 #endif
 
-#if HAVE_SYS_TYPES_H
+#ifdef HAVE_SYS_TYPES_H
 # include <sys/types.h>
 #endif
 
-#if HAVE_STRING_H
+#ifdef HAVE_STRING_H
 # include <string.h>
 #endif
 
-#if HAVE_ASSERT_H
+#ifdef HAVE_ASSERT_H
 # include <assert.h>
 #else
 # define assert(x) 
 #endif
 
+#ifdef HAVE_SEARCH_H
+# include <search.h>
+#else
+extern void *lfind(const void *, const void *, size_t *, size_t,
+                  int (*)(const void *, const void *));
+#endif
+
+/*
+  Libtiff itself does not require a 64-bit type, but bundled TIFF
+  utilities may use it.
+*/
+typedef TIFF_INT64_T  int64;
+typedef TIFF_UINT64_T uint64;
+
 #include "tiffio.h"
 #include "tif_dir.h"
 
-typedef double dblparam_t;
-
-#define GLOBALDATA(TYPE,NAME)  extern TYPE NAME
+#ifndef STRIP_SIZE_DEFAULT
+# define STRIP_SIZE_DEFAULT 8192
+#endif
 
 #define    streq(a,b)      (strcmp(a,b) == 0)
 
@@ -90,27 +104,35 @@ struct tiff {
        int             tif_fd;         /* open file descriptor */
        int             tif_mode;       /* open mode (O_*) */
        uint32          tif_flags;
-#define        TIFF_FILLORDER          0x0003  /* natural bit fill order for machine */
-#define        TIFF_DIRTYHEADER        0x0004  /* header must be written on close */
-#define        TIFF_DIRTYDIRECT        0x0008  /* current directory must be written */
-#define        TIFF_BUFFERSETUP        0x0010  /* data buffers setup */
-#define        TIFF_CODERSETUP         0x0020  /* encoder/decoder setup done */
-#define        TIFF_BEENWRITING        0x0040  /* written 1+ scanlines to file */
-#define        TIFF_SWAB               0x0080  /* byte swap file information */
-#define        TIFF_NOBITREV           0x0100  /* inhibit bit reversal logic */
-#define        TIFF_MYBUFFER           0x0200  /* my raw data buffer; free on close */
-#define        TIFF_ISTILED            0x0400  /* file is tile, not strip- based */
-#define        TIFF_MAPPED             0x0800  /* file is mapped into memory */
-#define        TIFF_POSTENCODE         0x1000  /* need call to postencode routine */
-#define        TIFF_INSUBIFD           0x2000  /* currently writing a subifd */
-#define        TIFF_UPSAMPLED          0x4000  /* library is doing data up-sampling */ 
-#define        TIFF_STRIPCHOP          0x8000  /* enable strip chopping support */
+#define        TIFF_FILLORDER          0x00003 /* natural bit fill order for machine */
+#define        TIFF_DIRTYHEADER        0x00004 /* header must be written on close */
+#define        TIFF_DIRTYDIRECT        0x00008 /* current directory must be written */
+#define        TIFF_BUFFERSETUP        0x00010 /* data buffers setup */
+#define        TIFF_CODERSETUP         0x00020 /* encoder/decoder setup done */
+#define        TIFF_BEENWRITING        0x00040 /* written 1+ scanlines to file */
+#define        TIFF_SWAB               0x00080 /* byte swap file information */
+#define        TIFF_NOBITREV           0x00100 /* inhibit bit reversal logic */
+#define        TIFF_MYBUFFER           0x00200 /* my raw data buffer; free on close */
+#define        TIFF_ISTILED            0x00400 /* file is tile, not strip- based */
+#define        TIFF_MAPPED             0x00800 /* file is mapped into memory */
+#define        TIFF_POSTENCODE         0x01000 /* need call to postencode routine */
+#define        TIFF_INSUBIFD           0x02000 /* currently writing a subifd */
+#define        TIFF_UPSAMPLED          0x04000 /* library is doing data up-sampling */ 
+#define        TIFF_STRIPCHOP          0x08000 /* enable strip chopping support */
+#define        TIFF_HEADERONLY         0x10000 /* read header only, do not process */
+                                       /* the first directory */
+#define TIFF_NOREADRAW         0x20000 /* skip reading of raw uncompressed */
+                                       /* image data */
+#define        TIFF_INCUSTOMIFD        0x40000 /* currently writing a custom IFD */
        toff_t          tif_diroff;     /* file offset of current directory */
        toff_t          tif_nextdiroff; /* file offset of following directory */
        toff_t*         tif_dirlist;    /* list of offsets to already seen */
                                        /* directories to prevent IFD looping */
+       tsize_t         tif_dirlistsize;/* number of entires in offset list */
        uint16          tif_dirnumber;  /* number of already seen directories */
        TIFFDirectory   tif_dir;        /* internal rep of current directory */
+       TIFFDirectory   tif_customdir;  /* custom IFDs are separated from
+                                          the main ones */
        TIFFHeader      tif_header;     /* file's header block */
        const int*      tif_typeshift;  /* data type shift counts */
        const long*     tif_typemask;   /* data type masks */
@@ -155,7 +177,8 @@ struct tiff {
        tsize_t         tif_rawcc;      /* bytes unread from raw buffer */
 /* memory-mapped file support */
        tidata_t        tif_base;       /* base of mapped file */
-       toff_t          tif_size;       /* size of mapped file region (bytes) */
+       toff_t          tif_size;       /* size of mapped file region (bytes)
+                                          FIXME: it should be tsize_t */
        TIFFMapFileProc tif_mapproc;    /* map file method */
        TIFFUnmapFileProc tif_unmapproc;/* unmap file method */
 /* input/output callback methods */
@@ -169,7 +192,7 @@ struct tiff {
        TIFFPostMethod  tif_postdecode; /* post decoding routine */
 /* tag support */
        TIFFFieldInfo** tif_fieldinfo;  /* sorted table of registered tags */
-       int             tif_nfields;    /* # entries in registered tag table */
+       size_t          tif_nfields;    /* # entries in registered tag table */
        const TIFFFieldInfo *tif_foundfield;/* cached pointer to already found tag */
         TIFFTagMethods  tif_tagmethods; /* tag get/set/print routines */
         TIFFClientInfoLink *tif_clientinfo; /* extra client information. */
@@ -213,13 +236,20 @@ struct tiff {
 #endif
 
 /* NB: the uint32 casts are to silence certain ANSI-C compilers */
-#define TIFFhowmany(x, y) ((((uint32)(x))+(((uint32)(y))-1))/((uint32)(y)))
+#define TIFFhowmany(x, y) (((uint32)x < (0xffffffff - (uint32)(y-1))) ?        \
+                          ((((uint32)(x))+(((uint32)(y))-1))/((uint32)(y))) : \
+                          0U)
 #define TIFFhowmany8(x) (((x)&0x07)?((uint32)(x)>>3)+1:(uint32)(x)>>3)
 #define        TIFFroundup(x, y) (TIFFhowmany(x,y)*(y))
 
+/* Safe multiply which returns zero if there is an integer overflow */
+#define TIFFSafeMultiply(t,v,m) ((((t)m != (t)0) && (((t)((v*m)/m)) == (t)v)) ? (t)(v*m) : (t)0)
+
 #define TIFFmax(A,B) ((A)>(B)?(A):(B))
 #define TIFFmin(A,B) ((A)<(B)?(A):(B))
 
+#define TIFFArrayCount(a) (sizeof (a) / sizeof ((a)[0]))
+
 #if defined(__cplusplus)
 extern "C" {
 #endif
@@ -234,28 +264,35 @@ extern    void _TIFFNoPostDecode(TIFF*, tidata_t, tsize_t);
 extern  int  _TIFFNoPreCode (TIFF*, tsample_t); 
 extern int _TIFFNoSeek(TIFF*, uint32);
 extern void _TIFFSwab16BitData(TIFF*, tidata_t, tsize_t);
+extern void _TIFFSwab24BitData(TIFF*, tidata_t, tsize_t);
 extern void _TIFFSwab32BitData(TIFF*, tidata_t, tsize_t);
 extern void _TIFFSwab64BitData(TIFF*, tidata_t, tsize_t);
 extern int TIFFFlushData1(TIFF*);
-extern void TIFFFreeDirectory(TIFF*);
 extern int TIFFDefaultDirectory(TIFF*);
+extern void _TIFFSetDefaultCompressionState(TIFF*);
 extern int TIFFSetCompressionScheme(TIFF*, int);
 extern int TIFFSetDefaultCompressionState(TIFF*);
 extern uint32 _TIFFDefaultStripSize(TIFF*, uint32);
 extern void _TIFFDefaultTileSize(TIFF*, uint32*, uint32*);
+extern int _TIFFDataSize(TIFFDataType);
 
-extern void _TIFFsetByteArray(void**, void*, long);
+extern void _TIFFsetByteArray(void**, void*, uint32);
 extern void _TIFFsetString(char**, char*);
-extern void _TIFFsetShortArray(uint16**, uint16*, long);
-extern void _TIFFsetLongArray(uint32**, uint32*, long);
-extern void _TIFFsetFloatArray(float**, float*, long);
-extern void _TIFFsetDoubleArray(double**, double*, long);
+extern void _TIFFsetShortArray(uint16**, uint16*, uint32);
+extern void _TIFFsetLongArray(uint32**, uint32*, uint32);
+extern void _TIFFsetFloatArray(float**, float*, uint32);
+extern void _TIFFsetDoubleArray(double**, double*, uint32);
 
 extern void _TIFFprintAscii(FILE*, const char*);
 extern void _TIFFprintAsciiTag(FILE*, const char*, const char*);
 
-GLOBALDATA(TIFFErrorHandler,_TIFFwarningHandler);
-GLOBALDATA(TIFFErrorHandler,_TIFFerrorHandler);
+extern TIFFErrorHandler _TIFFwarningHandler;
+extern TIFFErrorHandler _TIFFerrorHandler;
+extern TIFFErrorHandlerExt _TIFFwarningHandlerExt;
+extern TIFFErrorHandlerExt _TIFFerrorHandlerExt;
+
+extern tdata_t _TIFFCheckMalloc(TIFF*, size_t, size_t, const char*);
+extern tdata_t _TIFFCheckRealloc(TIFF*, tdata_t, size_t, size_t, const char*);
 
 extern int TIFFInitDumpMode(TIFF*, int);
 #ifdef PACKBITS_SUPPORT
@@ -304,3 +341,10 @@ extern     TIFFCodec _TIFFBuiltinCODECS[];
 #endif /* _TIFFIOP_ */
 
 /* vim: set ts=8 sts=8 sw=8 noet: */
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
index 8d96e44..50f11d7 100644 (file)
@@ -7,167 +7,174 @@ static struct {
        float   ustart;
        short   nus, ncum;
 }      uv_row[UV_NVS] = {
-       (float)0.247663,        4,      0,
-       (float)0.243779,        6,      4,
-       (float)0.241684,        7,      10,
-       (float)0.237874,        9,      17,
-       (float)0.235906,        10,     26,
-       (float)0.232153,        12,     36,
-       (float)0.228352,        14,     48,
-       (float)0.226259,        15,     62,
-       (float)0.222371,        17,     77,
-       (float)0.220410,        18,     94,
-       (float)0.214710,        21,     112,
-       (float)0.212714,        22,     133,
-       (float)0.210721,        23,     155,
-       (float)0.204976,        26,     178,
-       (float)0.202986,        27,     204,
-       (float)0.199245,        29,     231,
-       (float)0.195525,        31,     260,
-       (float)0.193560,        32,     291,
-       (float)0.189878,        34,     323,
-       (float)0.186216,        36,     357,
-       (float)0.186216,        36,     393,
-       (float)0.182592,        38,     429,
-       (float)0.179003,        40,     467,
-       (float)0.175466,        42,     507,
-       (float)0.172001,        44,     549,
-       (float)0.172001,        44,     593,
-       (float)0.168612,        46,     637,
-       (float)0.168612,        46,     683,
-       (float)0.163575,        49,     729,
-       (float)0.158642,        52,     778,
-       (float)0.158642,        52,     830,
-       (float)0.158642,        52,     882,
-       (float)0.153815,        55,     934,
-       (float)0.153815,        55,     989,
-       (float)0.149097,        58,     1044,
-       (float)0.149097,        58,     1102,
-       (float)0.142746,        62,     1160,
-       (float)0.142746,        62,     1222,
-       (float)0.142746,        62,     1284,
-       (float)0.138270,        65,     1346,
-       (float)0.138270,        65,     1411,
-       (float)0.138270,        65,     1476,
-       (float)0.132166,        69,     1541,
-       (float)0.132166,        69,     1610,
-       (float)0.126204,        73,     1679,
-       (float)0.126204,        73,     1752,
-       (float)0.126204,        73,     1825,
-       (float)0.120381,        77,     1898,
-       (float)0.120381,        77,     1975,
-       (float)0.120381,        77,     2052,
-       (float)0.120381,        77,     2129,
-       (float)0.112962,        82,     2206,
-       (float)0.112962,        82,     2288,
-       (float)0.112962,        82,     2370,
-       (float)0.107450,        86,     2452,
-       (float)0.107450,        86,     2538,
-       (float)0.107450,        86,     2624,
-       (float)0.107450,        86,     2710,
-       (float)0.100343,        91,     2796,
-       (float)0.100343,        91,     2887,
-       (float)0.100343,        91,     2978,
-       (float)0.095126,        95,     3069,
-       (float)0.095126,        95,     3164,
-       (float)0.095126,        95,     3259,
-       (float)0.095126,        95,     3354,
-       (float)0.088276,        100,    3449,
-       (float)0.088276,        100,    3549,
-       (float)0.088276,        100,    3649,
-       (float)0.088276,        100,    3749,
-       (float)0.081523,        105,    3849,
-       (float)0.081523,        105,    3954,
-       (float)0.081523,        105,    4059,
-       (float)0.081523,        105,    4164,
-       (float)0.074861,        110,    4269,
-       (float)0.074861,        110,    4379,
-       (float)0.074861,        110,    4489,
-       (float)0.074861,        110,    4599,
-       (float)0.068290,        115,    4709,
-       (float)0.068290,        115,    4824,
-       (float)0.068290,        115,    4939,
-       (float)0.068290,        115,    5054,
-       (float)0.063573,        119,    5169,
-       (float)0.063573,        119,    5288,
-       (float)0.063573,        119,    5407,
-       (float)0.063573,        119,    5526,
-       (float)0.057219,        124,    5645,
-       (float)0.057219,        124,    5769,
-       (float)0.057219,        124,    5893,
-       (float)0.057219,        124,    6017,
-       (float)0.050985,        129,    6141,
-       (float)0.050985,        129,    6270,
-       (float)0.050985,        129,    6399,
-       (float)0.050985,        129,    6528,
-       (float)0.050985,        129,    6657,
-       (float)0.044859,        134,    6786,
-       (float)0.044859,        134,    6920,
-       (float)0.044859,        134,    7054,
-       (float)0.044859,        134,    7188,
-       (float)0.040571,        138,    7322,
-       (float)0.040571,        138,    7460,
-       (float)0.040571,        138,    7598,
-       (float)0.040571,        138,    7736,
-       (float)0.036339,        142,    7874,
-       (float)0.036339,        142,    8016,
-       (float)0.036339,        142,    8158,
-       (float)0.036339,        142,    8300,
-       (float)0.032139,        146,    8442,
-       (float)0.032139,        146,    8588,
-       (float)0.032139,        146,    8734,
-       (float)0.032139,        146,    8880,
-       (float)0.027947,        150,    9026,
-       (float)0.027947,        150,    9176,
-       (float)0.027947,        150,    9326,
-       (float)0.023739,        154,    9476,
-       (float)0.023739,        154,    9630,
-       (float)0.023739,        154,    9784,
-       (float)0.023739,        154,    9938,
-       (float)0.019504,        158,    10092,
-       (float)0.019504,        158,    10250,
-       (float)0.019504,        158,    10408,
-       (float)0.016976,        161,    10566,
-       (float)0.016976,        161,    10727,
-       (float)0.016976,        161,    10888,
-       (float)0.016976,        161,    11049,
-       (float)0.012639,        165,    11210,
-       (float)0.012639,        165,    11375,
-       (float)0.012639,        165,    11540,
-       (float)0.009991,        168,    11705,
-       (float)0.009991,        168,    11873,
-       (float)0.009991,        168,    12041,
-       (float)0.009016,        170,    12209,
-       (float)0.009016,        170,    12379,
-       (float)0.009016,        170,    12549,
-       (float)0.006217,        173,    12719,
-       (float)0.006217,        173,    12892,
-       (float)0.005097,        175,    13065,
-       (float)0.005097,        175,    13240,
-       (float)0.005097,        175,    13415,
-       (float)0.003909,        177,    13590,
-       (float)0.003909,        177,    13767,
-       (float)0.002340,        177,    13944,
-       (float)0.002389,        170,    14121,
-       (float)0.001068,        164,    14291,
-       (float)0.001653,        157,    14455,
-       (float)0.000717,        150,    14612,
-       (float)0.001614,        143,    14762,
-       (float)0.000270,        136,    14905,
-       (float)0.000484,        129,    15041,
-       (float)0.001103,        123,    15170,
-       (float)0.001242,        115,    15293,
-       (float)0.001188,        109,    15408,
-       (float)0.001011,        103,    15517,
-       (float)0.000709,        97,     15620,
-       (float)0.000301,        89,     15717,
-       (float)0.002416,        82,     15806,
-       (float)0.003251,        76,     15888,
-       (float)0.003246,        69,     15964,
-       (float)0.004141,        62,     16033,
-       (float)0.005963,        55,     16095,
-       (float)0.008839,        47,     16150,
-       (float)0.010490,        40,     16197,
-       (float)0.016994,        31,     16237,
-       (float)0.023659,        21,     16268,
+       { (float)0.247663,      4,      0 },
+       { (float)0.243779,      6,      4 },
+       { (float)0.241684,      7,      10 },
+       { (float)0.237874,      9,      17 },
+       { (float)0.235906,      10,     26 },
+       { (float)0.232153,      12,     36 },
+       { (float)0.228352,      14,     48 },
+       { (float)0.226259,      15,     62 },
+       { (float)0.222371,      17,     77 },
+       { (float)0.220410,      18,     94 },
+       { (float)0.214710,      21,     112 },
+       { (float)0.212714,      22,     133 },
+       { (float)0.210721,      23,     155 },
+       { (float)0.204976,      26,     178 },
+       { (float)0.202986,      27,     204 },
+       { (float)0.199245,      29,     231 },
+       { (float)0.195525,      31,     260 },
+       { (float)0.193560,      32,     291 },
+       { (float)0.189878,      34,     323 },
+       { (float)0.186216,      36,     357 },
+       { (float)0.186216,      36,     393 },
+       { (float)0.182592,      38,     429 },
+       { (float)0.179003,      40,     467 },
+       { (float)0.175466,      42,     507 },
+       { (float)0.172001,      44,     549 },
+       { (float)0.172001,      44,     593 },
+       { (float)0.168612,      46,     637 },
+       { (float)0.168612,      46,     683 },
+       { (float)0.163575,      49,     729 },
+       { (float)0.158642,      52,     778 },
+       { (float)0.158642,      52,     830 },
+       { (float)0.158642,      52,     882 },
+       { (float)0.153815,      55,     934 },
+       { (float)0.153815,      55,     989 },
+       { (float)0.149097,      58,     1044 },
+       { (float)0.149097,      58,     1102 },
+       { (float)0.142746,      62,     1160 },
+       { (float)0.142746,      62,     1222 },
+       { (float)0.142746,      62,     1284 },
+       { (float)0.138270,      65,     1346 },
+       { (float)0.138270,      65,     1411 },
+       { (float)0.138270,      65,     1476 },
+       { (float)0.132166,      69,     1541 },
+       { (float)0.132166,      69,     1610 },
+       { (float)0.126204,      73,     1679 },
+       { (float)0.126204,      73,     1752 },
+       { (float)0.126204,      73,     1825 },
+       { (float)0.120381,      77,     1898 },
+       { (float)0.120381,      77,     1975 },
+       { (float)0.120381,      77,     2052 },
+       { (float)0.120381,      77,     2129 },
+       { (float)0.112962,      82,     2206 },
+       { (float)0.112962,      82,     2288 },
+       { (float)0.112962,      82,     2370 },
+       { (float)0.107450,      86,     2452 },
+       { (float)0.107450,      86,     2538 },
+       { (float)0.107450,      86,     2624 },
+       { (float)0.107450,      86,     2710 },
+       { (float)0.100343,      91,     2796 },
+       { (float)0.100343,      91,     2887 },
+       { (float)0.100343,      91,     2978 },
+       { (float)0.095126,      95,     3069 },
+       { (float)0.095126,      95,     3164 },
+       { (float)0.095126,      95,     3259 },
+       { (float)0.095126,      95,     3354 },
+       { (float)0.088276,      100,    3449 },
+       { (float)0.088276,      100,    3549 },
+       { (float)0.088276,      100,    3649 },
+       { (float)0.088276,      100,    3749 },
+       { (float)0.081523,      105,    3849 },
+       { (float)0.081523,      105,    3954 },
+       { (float)0.081523,      105,    4059 },
+       { (float)0.081523,      105,    4164 },
+       { (float)0.074861,      110,    4269 },
+       { (float)0.074861,      110,    4379 },
+       { (float)0.074861,      110,    4489 },
+       { (float)0.074861,      110,    4599 },
+       { (float)0.068290,      115,    4709 },
+       { (float)0.068290,      115,    4824 },
+       { (float)0.068290,      115,    4939 },
+       { (float)0.068290,      115,    5054 },
+       { (float)0.063573,      119,    5169 },
+       { (float)0.063573,      119,    5288 },
+       { (float)0.063573,      119,    5407 },
+       { (float)0.063573,      119,    5526 },
+       { (float)0.057219,      124,    5645 },
+       { (float)0.057219,      124,    5769 },
+       { (float)0.057219,      124,    5893 },
+       { (float)0.057219,      124,    6017 },
+       { (float)0.050985,      129,    6141 },
+       { (float)0.050985,      129,    6270 },
+       { (float)0.050985,      129,    6399 },
+       { (float)0.050985,      129,    6528 },
+       { (float)0.050985,      129,    6657 },
+       { (float)0.044859,      134,    6786 },
+       { (float)0.044859,      134,    6920 },
+       { (float)0.044859,      134,    7054 },
+       { (float)0.044859,      134,    7188 },
+       { (float)0.040571,      138,    7322 },
+       { (float)0.040571,      138,    7460 },
+       { (float)0.040571,      138,    7598 },
+       { (float)0.040571,      138,    7736 },
+       { (float)0.036339,      142,    7874 },
+       { (float)0.036339,      142,    8016 },
+       { (float)0.036339,      142,    8158 },
+       { (float)0.036339,      142,    8300 },
+       { (float)0.032139,      146,    8442 },
+       { (float)0.032139,      146,    8588 },
+       { (float)0.032139,      146,    8734 },
+       { (float)0.032139,      146,    8880 },
+       { (float)0.027947,      150,    9026 },
+       { (float)0.027947,      150,    9176 },
+       { (float)0.027947,      150,    9326 },
+       { (float)0.023739,      154,    9476 },
+       { (float)0.023739,      154,    9630 },
+       { (float)0.023739,      154,    9784 },
+       { (float)0.023739,      154,    9938 },
+       { (float)0.019504,      158,    10092 },
+       { (float)0.019504,      158,    10250 },
+       { (float)0.019504,      158,    10408 },
+       { (float)0.016976,      161,    10566 },
+       { (float)0.016976,      161,    10727 },
+       { (float)0.016976,      161,    10888 },
+       { (float)0.016976,      161,    11049 },
+       { (float)0.012639,      165,    11210 },
+       { (float)0.012639,      165,    11375 },
+       { (float)0.012639,      165,    11540 },
+       { (float)0.009991,      168,    11705 },
+       { (float)0.009991,      168,    11873 },
+       { (float)0.009991,      168,    12041 },
+       { (float)0.009016,      170,    12209 },
+       { (float)0.009016,      170,    12379 },
+       { (float)0.009016,      170,    12549 },
+       { (float)0.006217,      173,    12719 },
+       { (float)0.006217,      173,    12892 },
+       { (float)0.005097,      175,    13065 },
+       { (float)0.005097,      175,    13240 },
+       { (float)0.005097,      175,    13415 },
+       { (float)0.003909,      177,    13590 },
+       { (float)0.003909,      177,    13767 },
+       { (float)0.002340,      177,    13944 },
+       { (float)0.002389,      170,    14121 },
+       { (float)0.001068,      164,    14291 },
+       { (float)0.001653,      157,    14455 },
+       { (float)0.000717,      150,    14612 },
+       { (float)0.001614,      143,    14762 },
+       { (float)0.000270,      136,    14905 },
+       { (float)0.000484,      129,    15041 },
+       { (float)0.001103,      123,    15170 },
+       { (float)0.001242,      115,    15293 },
+       { (float)0.001188,      109,    15408 },
+       { (float)0.001011,      103,    15517 },
+       { (float)0.000709,      97,     15620 },
+       { (float)0.000301,      89,     15717 },
+       { (float)0.002416,      82,     15806 },
+       { (float)0.003251,      76,     15888 },
+       { (float)0.003246,      69,     15964 },
+       { (float)0.004141,      62,     16033 },
+       { (float)0.005963,      55,     16095 },
+       { (float)0.008839,      47,     16150 },
+       { (float)0.010490,      40,     16197 },
+       { (float)0.016994,      31,     16237 },
+       { (float)0.023659,      21,     16268 },
 };
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */
index cbd82bf..307e338 100644 (file)
@@ -13,8 +13,8 @@ libjpeg 6b (6.2) - The Independent JPEG Group's JPEG software.
              On UNIX systems configure script takes care of it.\r
 \r
 ------------------------------------------------------------------------------------\r
-libpng 1.2.29 - Portable Network Graphics library.\r
-               Copyright (C) 1998-2001, Glenn Randers-Pehrson.\r
+libpng 1.4.3 - Portable Network Graphics library.\r
+               Copyright (C) 1998-2010, Glenn Randers-Pehrson.\r
                See libpng home page http://www.libpng.org\r
                for details and links to the source code\r
 \r
@@ -22,7 +22,7 @@ libpng 1.2.29 - Portable Network Graphics library.
                On UNIX systems configure script takes care of it.\r
 \r
 ------------------------------------------------------------------------------------\r
-libtiff 3.7.2 - Tag Image File Format (TIFF) Software\r
+libtiff 3.9.4 - Tag Image File Format (TIFF) Software\r
                 Copyright (c) 1988-1997 Sam Leffler\r
                 Copyright (c) 1991-1997 Silicon Graphics, Inc.\r
                 See libtiff home page http://www.libtiff.org\r
@@ -35,8 +35,8 @@ libtiff 3.7.2 - Tag Image File Format (TIFF) Software
                 In this build support for ZIP (LZ77 compression), JPEG and LZW\r
                 are included.\r
 ------------------------------------------------------------------------------------\r
-zlib 1.2.3 - General purpose LZ77 compression library\r
-             Copyright (C) 1995-2002 Jean-loup Gailly and Mark Adler.\r
+zlib 1.2.5 - General purpose LZ77 compression library\r
+             Copyright (C) 1995-2010 Jean-loup Gailly and Mark Adler.\r
              See zlib home page http://www.gzip.org/zlib\r
              for details and links to the source code\r
 \r
@@ -89,7 +89,7 @@ openexr-1.4.0 - OpenEXR is a high dynamic-range (HDR) image file format develope
 \r
 ------------------------------------------------------------------------------------\r
 \r
-ffmpeg-0.5.0 - FFmpeg is a complete, cross-platform solution to record,\r
+ffmpeg-0.5.1 - FFmpeg is a complete, cross-platform solution to record,\r
              convert and stream audio and video. It includes libavcodec -\r
              the leading audio/video codec library, and also libavformat, libavutils and\r
              other helper libraries that are used by OpenCV (in highgui module) to\r
@@ -105,13 +105,13 @@ videoInput-0.1995 - Video capturing library for Windows using DirectShow as back
 \r
 ------------------------------------------------------------------------------------\r
 \r
-clapack-3.1.1.1 - F2C translation of the Linear Algebra PACKage (LAPACK),\r
-                  Copyright (c) 1992-2008 The University of Tennessee. All rights reserved.\r
-                  http://www.netlib.org/lapack/\r
-                  http://www.netlib.org/clapack/\r
+clapack-3.2.1 - F2C translation of the Linear Algebra PACKage (LAPACK),\r
+                Copyright (c) 1992-2010 The University of Tennessee. All rights reserved.\r
+                http://www.netlib.org/lapack/\r
+                http://www.netlib.org/clapack/\r
 \r
-                  Note, that only a subset of package is used in OpenCV.\r
-                  It can be extended and/or replaced with future upstream releases\r
-                  in the future.\r
+                Note, that only a subset of package is used in OpenCV.\r
+                It can be extended and/or replaced with future upstream releases\r
+                in the future.\r
 \r
 ------------------------------------------------------------------------------------\r
index 758cc50..d4219bf 100644 (file)
@@ -1,56 +1,52 @@
 ZLIB DATA COMPRESSION LIBRARY
 
-zlib 1.2.3 is a general purpose data compression library.  All the code is
+zlib 1.2.5 is a general purpose data compression library.  All the code is
 thread safe.  The data format used by the zlib library is described by RFCs
 (Request for Comments) 1950 to 1952 in the files
 http://www.ietf.org/rfc/rfc1950.txt (zlib format), rfc1951.txt (deflate format)
-and rfc1952.txt (gzip format). These documents are also available in other
-formats from ftp://ftp.uu.net/graphics/png/documents/zlib/zdoc-index.html
+and rfc1952.txt (gzip format).
 
 All functions of the compression library are documented in the file zlib.h
-(volunteer to write man pages welcome, contact zlib@gzip.org). A usage example
+(volunteer to write man pages welcome, contact zlib@gzip.org).  A usage example
 of the library is given in the file example.c which also tests that the library
-is working correctly. Another example is given in the file minigzip.c. The
+is working correctly.  Another example is given in the file minigzip.c.  The
 compression library itself is composed of all source files except example.c and
 minigzip.c.
 
 To compile all files and run the test program, follow the instructions given at
-the top of Makefile. In short "make test; make install" should work for most
-machines. For Unix: "./configure; make test; make install". For MSDOS, use one
-of the special makefiles such as Makefile.msc. For VMS, use make_vms.com.
+the top of Makefile.in.  In short "./configure; make test", and if that goes
+well, "make install" should work for most flavors of Unix.  For Windows, use one
+of the special makefiles in win32/ or contrib/vstudio/ .  For VMS, use
+make_vms.com.
 
 Questions about zlib should be sent to <zlib@gzip.org>, or to Gilles Vollant
-<info@winimage.com> for the Windows DLL version. The zlib home page is
-http://www.zlib.org or http://www.gzip.org/zlib/ Before reporting a problem,
-please check this site to verify that you have the latest version of zlib;
-otherwise get the latest version and check whether the problem still exists or
-not.
+<info@winimage.com> for the Windows DLL version.  The zlib home page is
+http://zlib.net/ .  Before reporting a problem, please check this site to
+verify that you have the latest version of zlib; otherwise get the latest
+version and check whether the problem still exists or not.
 
-PLEASE read the zlib FAQ http://www.gzip.org/zlib/zlib_faq.html before asking
-for help.
+PLEASE read the zlib FAQ http://zlib.net/zlib_faq.html before asking for help.
 
-Mark Nelson <markn@ieee.org> wrote an article about zlib for the Jan. 1997
-issue of  Dr. Dobb's Journal; a copy of the article is available in
-http://dogma.net/markn/articles/zlibtool/zlibtool.htm
+Mark Nelson <markn@ieee.org> wrote an article about zlib for the Jan.  1997
+issue of Dr.  Dobb's Journal; a copy of the article is available at
+http://marknelson.us/1997/01/01/zlib-engine/ .
 
-The changes made in version 1.2.3 are documented in the file ChangeLog.
+The changes made in version 1.2.5 are documented in the file ChangeLog.
 
-Unsupported third party contributions are provided in directory "contrib".
+Unsupported third party contributions are provided in directory contrib/ .
 
-A Java implementation of zlib is available in the Java Development Kit
-http://java.sun.com/j2se/1.4.2/docs/api/java/util/zip/package-summary.html
-See the zlib home page http://www.zlib.org for details.
+zlib is available in Java using the java.util.zip package, documented at
+http://java.sun.com/developer/technicalArticles/Programming/compression/ .
 
-A Perl interface to zlib written by Paul Marquess <pmqs@cpan.org> is in the
-CPAN (Comprehensive Perl Archive Network) sites
-http://www.cpan.org/modules/by-module/Compress/
+A Perl interface to zlib written by Paul Marquess <pmqs@cpan.org> is available
+at CPAN (Comprehensive Perl Archive Network) sites, including
+http://search.cpan.org/~pmqs/IO-Compress-Zlib/ .
 
 A Python interface to zlib written by A.M. Kuchling <amk@amk.ca> is
 available in Python 1.5 and later versions, see
-http://www.python.org/doc/lib/module-zlib.html
+http://www.python.org/doc/lib/module-zlib.html .
 
-A zlib binding for TCL written by Andreas Kupries <a.kupries@westend.com> is
-availlable at http://www.oche.de/~akupries/soft/trf/trf_zip.html
+zlib is built into tcl: http://wiki.tcl.tk/4610 .
 
 An experimental package to read and write files in .zip format, written on top
 of zlib by Gilles Vollant <info@winimage.com>, is available in the
@@ -74,25 +70,21 @@ Notes for some targets:
 - zlib doesn't work on HP-UX 9.05 with some versions of /bin/cc. It works with
   other compilers. Use "make test" to check your compiler.
 
-- gzdopen is not supported on RISCOS, BEOS and by some Mac compilers.
+- gzdopen is not supported on RISCOS or BEOS.
 
 - For PalmOs, see http://palmzlib.sourceforge.net/
 
-- When building a shared, i.e. dynamic library on Mac OS X, the library must be
-  installed before testing (do "make install" before "make test"), since the
-  library location is specified in the library.
-
 
 Acknowledgments:
 
-  The deflate format used by zlib was defined by Phil Katz. The deflate
-  and zlib specifications were written by L. Peter Deutsch. Thanks to all the
-  people who reported problems and suggested various improvements in zlib;
-  they are too numerous to cite here.
+  The deflate format used by zlib was defined by Phil Katz.  The deflate and
+  zlib specifications were written by L.  Peter Deutsch.  Thanks to all the
+  people who reported problems and suggested various improvements in zlib; they
+  are too numerous to cite here.
 
 Copyright notice:
 
- (C) 1995-2004 Jean-loup Gailly and Mark Adler
+ (C) 1995-2010 Jean-loup Gailly and Mark Adler
 
   This software is provided 'as-is', without any express or implied
   warranty.  In no event will the authors be held liable for any damages
@@ -113,13 +105,11 @@ Copyright notice:
   Jean-loup Gailly        Mark Adler
   jloup@gzip.org          madler@alumni.caltech.edu
 
-If you use the zlib library in a product, we would appreciate *not*
-receiving lengthy legal documents to sign. The sources are provided
-for free but without warranty of any kind.  The library has been
-entirely written by Jean-loup Gailly and Mark Adler; it does not
-include third-party code.
+If you use the zlib library in a product, we would appreciate *not* receiving
+lengthy legal documents to sign.  The sources are provided for free but without
+warranty of any kind.  The library has been entirely written by Jean-loup
+Gailly and Mark Adler; it does not include third-party code.
 
-If you redistribute modified sources, we would appreciate that you include
-in the file ChangeLog history information documenting your changes. Please
-read the FAQ for more information on the distribution of modified source
-versions.
+If you redistribute modified sources, we would appreciate that you include in
+the file ChangeLog history information documenting your changes.  Please read
+the FAQ for more information on the distribution of modified source versions.
index da5df2c..65ad6a5 100644 (file)
@@ -1,12 +1,15 @@
 /* adler32.c -- compute the Adler-32 checksum of a data stream
- * Copyright (C) 1995-2004 Mark Adler
+ * Copyright (C) 1995-2007 Mark Adler
  * For conditions of distribution and use, see copyright notice in zlib.h
  */
 
-/* @(#) $Id: adler32.c,v 1.2 2008-05-26 19:08:10 vp153 Exp $ */
+/* @(#) $Id$ */
 
-#define ZLIB_INTERNAL
-#include "zlib.h"
+#include "zutil.h"
+
+#define local static
+
+local uLong adler32_combine_(uLong adler1, uLong adler2, z_off64_t len2);
 
 #define BASE 65521UL    /* largest prime smaller than 65536 */
 #define NMAX 5552
@@ -125,10 +128,10 @@ uLong ZEXPORT adler32(adler, buf, len)
 }
 
 /* ========================================================================= */
-uLong ZEXPORT adler32_combine(adler1, adler2, len2)
+local uLong adler32_combine_(adler1, adler2, len2)
     uLong adler1;
     uLong adler2;
-    z_off_t len2;
+    z_off64_t len2;
 {
     unsigned long sum1;
     unsigned long sum2;
@@ -141,9 +144,26 @@ uLong ZEXPORT adler32_combine(adler1, adler2, len2)
     MOD(sum2);
     sum1 += (adler2 & 0xffff) + BASE - 1;
     sum2 += ((adler1 >> 16) & 0xffff) + ((adler2 >> 16) & 0xffff) + BASE - rem;
-    if (sum1 > BASE) sum1 -= BASE;
-    if (sum1 > BASE) sum1 -= BASE;
-    if (sum2 > (BASE << 1)) sum2 -= (BASE << 1);
-    if (sum2 > BASE) sum2 -= BASE;
+    if (sum1 >= BASE) sum1 -= BASE;
+    if (sum1 >= BASE) sum1 -= BASE;
+    if (sum2 >= (BASE << 1)) sum2 -= (BASE << 1);
+    if (sum2 >= BASE) sum2 -= BASE;
     return sum1 | (sum2 << 16);
 }
+
+/* ========================================================================= */
+uLong ZEXPORT adler32_combine(adler1, adler2, len2)
+    uLong adler1;
+    uLong adler2;
+    z_off_t len2;
+{
+    return adler32_combine_(adler1, adler2, len2);
+}
+
+uLong ZEXPORT adler32_combine64(adler1, adler2, len2)
+    uLong adler1;
+    uLong adler2;
+    z_off64_t len2;
+{
+    return adler32_combine_(adler1, adler2, len2);
+}
index ff64826..ea4dfbe 100644 (file)
@@ -1,9 +1,9 @@
 /* compress.c -- compress a memory buffer
- * Copyright (C) 1995-2003 Jean-loup Gailly.
+ * Copyright (C) 1995-2005 Jean-loup Gailly.
  * For conditions of distribution and use, see copyright notice in zlib.h
  */
 
-/* @(#) $Id: compress.c,v 1.2 2008-05-26 19:08:10 vp153 Exp $ */
+/* @(#) $Id$ */
 
 #define ZLIB_INTERNAL
 #include "zlib.h"
@@ -75,5 +75,6 @@ int ZEXPORT compress (dest, destLen, source, sourceLen)
 uLong ZEXPORT compressBound (sourceLen)
     uLong sourceLen;
 {
-    return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) + 11;
+    return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) +
+           (sourceLen >> 25) + 13;
 }
diff --git a/3rdparty/zlib/configure b/3rdparty/zlib/configure
deleted file mode 100644 (file)
index 6e13859..0000000
+++ /dev/null
@@ -1,443 +0,0 @@
-#!/bin/sh
-# configure script for zlib. This script is needed only if
-# you wish to build a shared library and your system supports them,
-# of if you need special compiler, flags or install directory.
-# Otherwise, you can just use directly "make test; make install"
-#
-# To create a shared library, use "configure --shared"; by default a static
-# library is created. If the primitive shared library support provided here
-# does not work, use ftp://prep.ai.mit.edu/pub/gnu/libtool-*.tar.gz
-#
-# To impose specific compiler or flags or install directory, use for example:
-#    prefix=$HOME CC=cc CFLAGS="-O4" ./configure
-# or for csh/tcsh users:
-#    (setenv prefix $HOME; setenv CC cc; setenv CFLAGS "-O4"; ./configure)
-# LDSHARED is the command to be used to create a shared library
-
-# Incorrect settings of CC or CFLAGS may prevent creating a shared library.
-# If you have problems, try without defining CC and CFLAGS before reporting
-# an error.
-
-LIBS=libz.a
-LDFLAGS="-L. ${LIBS}"
-VER=`sed -n -e '/VERSION "/s/.*"\(.*\)".*/\1/p' < zlib.h`
-VER2=`sed -n -e '/VERSION "/s/.*"\([0-9]*\\.[0-9]*\)\\..*/\1/p' < zlib.h`
-VER1=`sed -n -e '/VERSION "/s/.*"\([0-9]*\)\\..*/\1/p' < zlib.h`
-AR=${AR-"ar rc"}
-RANLIB=${RANLIB-"ranlib"}
-prefix=${prefix-/usr/local}
-exec_prefix=${exec_prefix-'${prefix}'}
-libdir=${libdir-'${exec_prefix}/lib'}
-includedir=${includedir-'${prefix}/include'}
-mandir=${mandir-'${prefix}/share/man'}
-shared_ext='.so'
-shared=0
-gcc=0
-old_cc="$CC"
-old_cflags="$CFLAGS"
-
-while test $# -ge 1
-do
-case "$1" in
-    -h* | --h*)
-      echo 'usage:'
-      echo '  configure [--shared] [--prefix=PREFIX]  [--exec_prefix=EXPREFIX]'
-      echo '     [--libdir=LIBDIR] [--includedir=INCLUDEDIR]'
-        exit 0;;
-    -p*=* | --p*=*) prefix=`echo $1 | sed 's/[-a-z_]*=//'`; shift;;
-    -e*=* | --e*=*) exec_prefix=`echo $1 | sed 's/[-a-z_]*=//'`; shift;;
-    -l*=* | --libdir=*) libdir=`echo $1 | sed 's/[-a-z_]*=//'`; shift;;
-    -i*=* | --includedir=*) includedir=`echo $1 | sed 's/[-a-z_]*=//'`;shift;;
-    -p* | --p*) prefix="$2"; shift; shift;;
-    -e* | --e*) exec_prefix="$2"; shift; shift;;
-    -l* | --l*) libdir="$2"; shift; shift;;
-    -i* | --i*) includedir="$2"; shift; shift;;
-    -s* | --s*) shared=1; shift;;
-    *) echo "unknown option: $1"; echo "$0 --help for help"; exit 1;;
-    esac
-done
-
-test=ztest$$
-cat > $test.c <<EOF
-extern int getchar();
-int hello() {return getchar();}
-EOF
-
-test -z "$CC" && echo Checking for gcc...
-cc=${CC-gcc}
-cflags=${CFLAGS-"-O3"}
-# to force the asm version use: CFLAGS="-O3 -DASMV" ./configure
-case "$cc" in
-  *gcc*) gcc=1;;
-esac
-
-if test "$gcc" -eq 1 && ($cc -c $cflags $test.c) 2>/dev/null; then
-  CC="$cc"
-  SFLAGS=${CFLAGS-"-fPIC -O3"}
-  CFLAGS="$cflags"
-  case `(uname -s || echo unknown) 2>/dev/null` in
-  Linux | linux | GNU | GNU/*) LDSHARED=${LDSHARED-"$cc -shared -Wl,-soname,libz.so.1"};;
-  CYGWIN* | Cygwin* | cygwin* | OS/2* )
-             EXE='.exe';;
-  QNX*)  # This is for QNX6. I suppose that the QNX rule below is for QNX2,QNX4
-         # (alain.bonnefoy@icbt.com)
-                 LDSHARED=${LDSHARED-"$cc -shared -Wl,-hlibz.so.1"};;
-  HP-UX*)        LDSHARED=${LDSHARED-"$cc -shared $SFLAGS"}
-                 shared_ext='.sl'
-                 SHAREDLIB='libz.sl';;
-  Darwin*)   shared_ext='.dylib'
-             SHAREDLIB=libz$shared_ext
-             SHAREDLIBV=libz.$VER$shared_ext
-             SHAREDLIBM=libz.$VER1$shared_ext
-             LDSHARED=${LDSHARED-"$cc -dynamiclib -install_name $libdir/$SHAREDLIBV -compatibility_version $VER2 -current_version $VER"};;
-  *)             LDSHARED=${LDSHARED-"$cc -shared"};;
-  esac
-else
-  # find system name and corresponding cc options
-  CC=${CC-cc}
-  case `(uname -sr || echo unknown) 2>/dev/null` in
-  HP-UX*)    SFLAGS=${CFLAGS-"-O +z"}
-             CFLAGS=${CFLAGS-"-O"}
-#            LDSHARED=${LDSHARED-"ld -b +vnocompatwarnings"}
-             LDSHARED=${LDSHARED-"ld -b"}
-             shared_ext='.sl'
-             SHAREDLIB='libz.sl';;
-  IRIX*)     SFLAGS=${CFLAGS-"-ansi -O2 -rpath ."}
-             CFLAGS=${CFLAGS-"-ansi -O2"}
-             LDSHARED=${LDSHARED-"cc -shared"};;
-  OSF1\ V4*) SFLAGS=${CFLAGS-"-O -std1"}
-             CFLAGS=${CFLAGS-"-O -std1"}
-             LDSHARED=${LDSHARED-"cc -shared  -Wl,-soname,libz.so -Wl,-msym -Wl,-rpath,$(libdir) -Wl,-set_version,${VER}:1.0"};;
-  OSF1*)     SFLAGS=${CFLAGS-"-O -std1"}
-             CFLAGS=${CFLAGS-"-O -std1"}
-             LDSHARED=${LDSHARED-"cc -shared"};;
-  QNX*)      SFLAGS=${CFLAGS-"-4 -O"}
-             CFLAGS=${CFLAGS-"-4 -O"}
-             LDSHARED=${LDSHARED-"cc"}
-             RANLIB=${RANLIB-"true"}
-             AR="cc -A";;
-  SCO_SV\ 3.2*) SFLAGS=${CFLAGS-"-O3 -dy -KPIC "}
-             CFLAGS=${CFLAGS-"-O3"}
-             LDSHARED=${LDSHARED-"cc -dy -KPIC -G"};;
-  SunOS\ 5*) SFLAGS=${CFLAGS-"-fast -xcg89 -KPIC -R."}
-             CFLAGS=${CFLAGS-"-fast -xcg89"}
-             LDSHARED=${LDSHARED-"cc -G"};;
-  SunOS\ 4*) SFLAGS=${CFLAGS-"-O2 -PIC"}
-             CFLAGS=${CFLAGS-"-O2"}
-             LDSHARED=${LDSHARED-"ld"};;
-  UNIX_System_V\ 4.2.0)
-             SFLAGS=${CFLAGS-"-KPIC -O"}
-             CFLAGS=${CFLAGS-"-O"}
-             LDSHARED=${LDSHARED-"cc -G"};;
-  UNIX_SV\ 4.2MP)
-             SFLAGS=${CFLAGS-"-Kconform_pic -O"}
-             CFLAGS=${CFLAGS-"-O"}
-             LDSHARED=${LDSHARED-"cc -G"};;
-  OpenUNIX\ 5)
-             SFLAGS=${CFLAGS-"-KPIC -O"}
-             CFLAGS=${CFLAGS-"-O"}
-             LDSHARED=${LDSHARED-"cc -G"};;
-  AIX*)  # Courtesy of dbakker@arrayasolutions.com
-             SFLAGS=${CFLAGS-"-O -qmaxmem=8192"}
-             CFLAGS=${CFLAGS-"-O -qmaxmem=8192"}
-             LDSHARED=${LDSHARED-"xlc -G"};;
-  # send working options for other systems to support@gzip.org
-  *)         SFLAGS=${CFLAGS-"-O"}
-             CFLAGS=${CFLAGS-"-O"}
-             LDSHARED=${LDSHARED-"cc -shared"};;
-  esac
-fi
-
-SHAREDLIB=${SHAREDLIB-"libz$shared_ext"}
-SHAREDLIBV=${SHAREDLIBV-"libz$shared_ext.$VER"}
-SHAREDLIBM=${SHAREDLIBM-"libz$shared_ext.$VER1"}
-
-if test $shared -eq 1; then
-  echo Checking for shared library support...
-  # we must test in two steps (cc then ld), required at least on SunOS 4.x
-  if test "`($CC -c $SFLAGS $test.c) 2>&1`" = "" &&
-     test "`($LDSHARED -o $test$shared_ext $test.o) 2>&1`" = ""; then
-    CFLAGS="$SFLAGS"
-    LIBS="$SHAREDLIBV"
-    echo Building shared library $SHAREDLIBV with $CC.
-  elif test -z "$old_cc" -a -z "$old_cflags"; then
-    echo No shared library support.
-    shared=0;
-  else
-    echo 'No shared library support; try without defining CC and CFLAGS'
-    shared=0;
-  fi
-fi
-if test $shared -eq 0; then
-  LDSHARED="$CC"
-  echo Building static library $LIBS version $VER with $CC.
-else
-  LDFLAGS="-L. ${SHAREDLIBV}"
-fi
-
-cat > $test.c <<EOF
-#include <unistd.h>
-int main() { return 0; }
-EOF
-if test "`($CC -c $CFLAGS $test.c) 2>&1`" = ""; then
-  sed < zconf.in.h "/HAVE_UNISTD_H/s%0%1%" > zconf.h
-  echo "Checking for unistd.h... Yes."
-else
-  cp -p zconf.in.h zconf.h
-  echo "Checking for unistd.h... No."
-fi
-
-cat > $test.c <<EOF
-#include <stdio.h>
-#include <stdarg.h>
-#include "zconf.h"
-
-int main()
-{
-#ifndef STDC
-  choke me
-#endif
-
-  return 0;
-}
-EOF
-
-if test "`($CC -c $CFLAGS $test.c) 2>&1`" = ""; then
-  echo "Checking whether to use vs[n]printf() or s[n]printf()... using vs[n]printf()"
-
-  cat > $test.c <<EOF
-#include <stdio.h>
-#include <stdarg.h>
-
-int mytest(char *fmt, ...)
-{
-  char buf[20];
-  va_list ap;
-
-  va_start(ap, fmt);
-  vsnprintf(buf, sizeof(buf), fmt, ap);
-  va_end(ap);
-  return 0;
-}
-
-int main()
-{
-  return (mytest("Hello%d\n", 1));
-}
-EOF
-
-  if test "`($CC $CFLAGS -o $test $test.c) 2>&1`" = ""; then
-    echo "Checking for vsnprintf() in stdio.h... Yes."
-
-    cat >$test.c <<EOF
-#include <stdio.h>
-#include <stdarg.h>
-
-int mytest(char *fmt, ...)
-{
-  int n;
-  char buf[20];
-  va_list ap;
-
-  va_start(ap, fmt);
-  n = vsnprintf(buf, sizeof(buf), fmt, ap);
-  va_end(ap);
-  return n;
-}
-
-int main()
-{
-  return (mytest("Hello%d\n", 1));
-}
-EOF
-
-    if test "`($CC -c $CFLAGS $test.c) 2>&1`" = ""; then
-      echo "Checking for return value of vsnprintf()... Yes."
-    else
-      CFLAGS="$CFLAGS -DHAS_vsnprintf_void"
-      echo "Checking for return value of vsnprintf()... No."
-      echo "  WARNING: apparently vsnprintf() does not return a value. zlib"
-      echo "  can build but will be open to possible string-format security"
-      echo "  vulnerabilities."
-    fi
-  else
-    CFLAGS="$CFLAGS -DNO_vsnprintf"
-    echo "Checking for vsnprintf() in stdio.h... No."
-    echo "  WARNING: vsnprintf() not found, falling back to vsprintf(). zlib"
-    echo "  can build but will be open to possible buffer-overflow security"
-    echo "  vulnerabilities."
-
-    cat >$test.c <<EOF
-#include <stdio.h>
-#include <stdarg.h>
-
-int mytest(char *fmt, ...)
-{
-  int n;
-  char buf[20];
-  va_list ap;
-
-  va_start(ap, fmt);
-  n = vsprintf(buf, fmt, ap);
-  va_end(ap);
-  return n;
-}
-
-int main()
-{
-  return (mytest("Hello%d\n", 1));
-}
-EOF
-
-    if test "`($CC -c $CFLAGS $test.c) 2>&1`" = ""; then
-      echo "Checking for return value of vsprintf()... Yes."
-    else
-      CFLAGS="$CFLAGS -DHAS_vsprintf_void"
-      echo "Checking for return value of vsprintf()... No."
-      echo "  WARNING: apparently vsprintf() does not return a value. zlib"
-      echo "  can build but will be open to possible string-format security"
-      echo "  vulnerabilities."
-    fi
-  fi
-else
-  echo "Checking whether to use vs[n]printf() or s[n]printf()... using s[n]printf()"
-
-  cat >$test.c <<EOF
-#include <stdio.h>
-
-int mytest()
-{
-  char buf[20];
-
-  snprintf(buf, sizeof(buf), "%s", "foo");
-  return 0;
-}
-
-int main()
-{
-  return (mytest());
-}
-EOF
-
-  if test "`($CC $CFLAGS -o $test $test.c) 2>&1`" = ""; then
-    echo "Checking for snprintf() in stdio.h... Yes."
-
-    cat >$test.c <<EOF
-#include <stdio.h>
-
-int mytest()
-{
-  char buf[20];
-
-  return snprintf(buf, sizeof(buf), "%s", "foo");
-}
-
-int main()
-{
-  return (mytest());
-}
-EOF
-
-    if test "`($CC -c $CFLAGS $test.c) 2>&1`" = ""; then
-      echo "Checking for return value of snprintf()... Yes."
-    else
-      CFLAGS="$CFLAGS -DHAS_snprintf_void"
-      echo "Checking for return value of snprintf()... No."
-      echo "  WARNING: apparently snprintf() does not return a value. zlib"
-      echo "  can build but will be open to possible string-format security"
-      echo "  vulnerabilities."
-    fi
-  else
-    CFLAGS="$CFLAGS -DNO_snprintf"
-    echo "Checking for snprintf() in stdio.h... No."
-    echo "  WARNING: snprintf() not found, falling back to sprintf(). zlib"
-    echo "  can build but will be open to possible buffer-overflow security"
-    echo "  vulnerabilities."
-
-    cat >$test.c <<EOF
-#include <stdio.h>
-
-int mytest()
-{
-  char buf[20];
-
-  return sprintf(buf, "%s", "foo");
-}
-
-int main()
-{
-  return (mytest());
-}
-EOF
-
-    if test "`($CC -c $CFLAGS $test.c) 2>&1`" = ""; then
-      echo "Checking for return value of sprintf()... Yes."
-    else
-      CFLAGS="$CFLAGS -DHAS_sprintf_void"
-      echo "Checking for return value of sprintf()... No."
-      echo "  WARNING: apparently sprintf() does not return a value. zlib"
-      echo "  can build but will be open to possible string-format security"
-      echo "  vulnerabilities."
-    fi
-  fi
-fi
-
-cat >$test.c <<EOF
-#include <errno.h>
-int main() { return 0; }
-EOF
-if test "`($CC -c $CFLAGS $test.c) 2>&1`" = ""; then
-  echo "Checking for errno.h... Yes."
-else
-  echo "Checking for errno.h... No."
-  CFLAGS="$CFLAGS -DNO_ERRNO_H"
-fi
-
-cat > $test.c <<EOF
-#include <sys/types.h>
-#include <sys/mman.h>
-#include <sys/stat.h>
-caddr_t hello() {
-  return mmap((caddr_t)0, (off_t)0, PROT_READ, MAP_SHARED, 0, (off_t)0);
-}
-EOF
-if test "`($CC -c $CFLAGS $test.c) 2>&1`" = ""; then
-  CFLAGS="$CFLAGS -DUSE_MMAP"
-  echo Checking for mmap support... Yes.
-else
-  echo Checking for mmap support... No.
-fi
-
-CPP=${CPP-"$CC -E"}
-case $CFLAGS in
-  *ASMV*)
-    if test "`nm $test.o | grep _hello`" = ""; then
-      CPP="$CPP -DNO_UNDERLINE"
-      echo Checking for underline in external names... No.
-    else
-      echo Checking for underline in external names... Yes.
-    fi;;
-esac
-
-rm -f $test.[co] $test $test$shared_ext
-
-# udpate Makefile
-sed < Makefile.in "
-/^CC *=/s#=.*#=$CC#
-/^CFLAGS *=/s#=.*#=$CFLAGS#
-/^CPP *=/s#=.*#=$CPP#
-/^LDSHARED *=/s#=.*#=$LDSHARED#
-/^LIBS *=/s#=.*#=$LIBS#
-/^SHAREDLIB *=/s#=.*#=$SHAREDLIB#
-/^SHAREDLIBV *=/s#=.*#=$SHAREDLIBV#
-/^SHAREDLIBM *=/s#=.*#=$SHAREDLIBM#
-/^AR *=/s#=.*#=$AR#
-/^RANLIB *=/s#=.*#=$RANLIB#
-/^EXE *=/s#=.*#=$EXE#
-/^prefix *=/s#=.*#=$prefix#
-/^exec_prefix *=/s#=.*#=$exec_prefix#
-/^libdir *=/s#=.*#=$libdir#
-/^includedir *=/s#=.*#=$includedir#
-/^mandir *=/s#=.*#=$mandir#
-/^LDFLAGS *=/s#=.*#=$LDFLAGS#
-" > Makefile
index d6e980c..91be372 100644 (file)
@@ -1,5 +1,5 @@
 /* crc32.c -- compute the CRC-32 of a data stream
- * Copyright (C) 1995-2005 Mark Adler
+ * Copyright (C) 1995-2006, 2010 Mark Adler
  * For conditions of distribution and use, see copyright notice in zlib.h
  *
  * Thanks to Rodney Brown <rbrown64@csc.com.au> for his contribution of faster
@@ -9,7 +9,7 @@
  * factor of two increase in speed on a Power PC G4 (PPC7455) using gcc -O3.
  */
 
-/* @(#) $Id: crc32.c,v 1.2 2008-05-26 19:08:10 vp153 Exp $ */
+/* @(#) $Id$ */
 
 /*
   Note on the use of DYNAMIC_CRC_TABLE: there is no mutex or semaphore
@@ -53,7 +53,7 @@
 
 /* Definitions for doing the crc four data bytes at a time. */
 #ifdef BYFOUR
-#  define REV(w) (((w)>>24)+(((w)>>8)&0xff00)+ \
+#  define REV(w) ((((w)>>24)&0xff)+(((w)>>8)&0xff00)+ \
                 (((w)&0xff00)<<8)+(((w)&0xff)<<24))
    local unsigned long crc32_little OF((unsigned long,
                         const unsigned char FAR *, unsigned));
@@ -68,6 +68,8 @@
 local unsigned long gf2_matrix_times OF((unsigned long *mat,
                                          unsigned long vec));
 local void gf2_matrix_square OF((unsigned long *square, unsigned long *mat));
+local uLong crc32_combine_(uLong crc1, uLong crc2, z_off64_t len2);
+
 
 #ifdef DYNAMIC_CRC_TABLE
 
@@ -219,7 +221,7 @@ const unsigned long FAR * ZEXPORT get_crc_table()
 unsigned long ZEXPORT crc32(crc, buf, len)
     unsigned long crc;
     const unsigned char FAR *buf;
-    unsigned len;
+    uInt len;
 {
     if (buf == Z_NULL) return 0UL;
 
@@ -367,22 +369,22 @@ local void gf2_matrix_square(square, mat)
 }
 
 /* ========================================================================= */
-uLong ZEXPORT crc32_combine(crc1, crc2, len2)
+local uLong crc32_combine_(crc1, crc2, len2)
     uLong crc1;
     uLong crc2;
-    z_off_t len2;
+    z_off64_t len2;
 {
     int n;
     unsigned long row;
     unsigned long even[GF2_DIM];    /* even-power-of-two zeros operator */
     unsigned long odd[GF2_DIM];     /* odd-power-of-two zeros operator */
 
-    /* degenerate case */
-    if (len2 == 0)
+    /* degenerate case (also disallow negative lengths) */
+    if (len2 <= 0)
         return crc1;
 
     /* put operator for one zero bit in odd */
-    odd[0] = 0xedb88320L;           /* CRC-32 polynomial */
+    odd[0] = 0xedb88320UL;          /* CRC-32 polynomial */
     row = 1;
     for (n = 1; n < GF2_DIM; n++) {
         odd[n] = row;
@@ -421,3 +423,20 @@ uLong ZEXPORT crc32_combine(crc1, crc2, len2)
     crc1 ^= crc2;
     return crc1;
 }
+
+/* ========================================================================= */
+uLong ZEXPORT crc32_combine(crc1, crc2, len2)
+    uLong crc1;
+    uLong crc2;
+    z_off_t len2;
+{
+    return crc32_combine_(crc1, crc2, len2);
+}
+
+uLong ZEXPORT crc32_combine64(crc1, crc2, len2)
+    uLong crc1;
+    uLong crc2;
+    z_off64_t len2;
+{
+    return crc32_combine_(crc1, crc2, len2);
+}
index b58e75f..5c4022f 100644 (file)
@@ -1,5 +1,5 @@
 /* deflate.c -- compress data using the deflation algorithm
- * Copyright (C) 1995-2005 Jean-loup Gailly.
+ * Copyright (C) 1995-2010 Jean-loup Gailly and Mark Adler
  * For conditions of distribution and use, see copyright notice in zlib.h
  */
 
  *
  */
 
-/* @(#) $Id: deflate.c,v 1.2 2008-05-26 19:08:10 vp153 Exp $ */
+/* @(#) $Id$ */
 
 #include "deflate.h"
 
 const char deflate_copyright[] =
-   " deflate 1.2.3 Copyright 1995-2005 Jean-loup Gailly ";
+   " deflate 1.2.5 Copyright 1995-2010 Jean-loup Gailly and Mark Adler ";
 /*
   If you use the zlib library in a product, an acknowledgment is welcome
   in the documentation of your product. If for some reason you cannot
@@ -79,19 +79,18 @@ local block_state deflate_fast   OF((deflate_state *s, int flush));
 #ifndef FASTEST
 local block_state deflate_slow   OF((deflate_state *s, int flush));
 #endif
+local block_state deflate_rle    OF((deflate_state *s, int flush));
+local block_state deflate_huff   OF((deflate_state *s, int flush));
 local void lm_init        OF((deflate_state *s));
 local void putShortMSB    OF((deflate_state *s, uInt b));
 local void flush_pending  OF((z_streamp strm));
 local int read_buf        OF((z_streamp strm, Bytef *buf, unsigned size));
-#ifndef FASTEST
 #ifdef ASMV
       void match_init OF((void)); /* asm code initialization */
       uInt longest_match  OF((deflate_state *s, IPos cur_match));
 #else
 local uInt longest_match  OF((deflate_state *s, IPos cur_match));
 #endif
-#endif
-local uInt longest_match_fast OF((deflate_state *s, IPos cur_match));
 
 #ifdef DEBUG
 local  void check_match OF((deflate_state *s, IPos start, IPos match,
@@ -110,11 +109,6 @@ local  void check_match OF((deflate_state *s, IPos start, IPos match,
 #endif
 /* Matches of length 3 are discarded if their distance exceeds TOO_FAR */
 
-#define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1)
-/* Minimum amount of lookahead, except at the end of the input file.
- * See deflate.c for comments about the MIN_MATCH+1.
- */
-
 /* Values for max_lazy_match, good_match and max_chain_length, depending on
  * the desired pack level (0..9). The values given below have been tuned to
  * exclude worst case performance for pathological files. Better values may be
@@ -288,6 +282,8 @@ int ZEXPORT deflateInit2_(strm, level, method, windowBits, memLevel, strategy,
     s->prev   = (Posf *)  ZALLOC(strm, s->w_size, sizeof(Pos));
     s->head   = (Posf *)  ZALLOC(strm, s->hash_size, sizeof(Pos));
 
+    s->high_water = 0;      /* nothing written to s->window yet */
+
     s->lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */
 
     overlay = (ushf *) ZALLOC(strm, s->lit_bufsize, sizeof(ush)+2);
@@ -332,8 +328,8 @@ int ZEXPORT deflateSetDictionary (strm, dictionary, dictLength)
         strm->adler = adler32(strm->adler, dictionary, dictLength);
 
     if (length < MIN_MATCH) return Z_OK;
-    if (length > MAX_DIST(s)) {
-        length = MAX_DIST(s);
+    if (length > s->w_size) {
+        length = s->w_size;
         dictionary += dictLength - length; /* use the tail of the dictionary */
     }
     zmemcpy(s->window, dictionary, length);
@@ -435,9 +431,10 @@ int ZEXPORT deflateParams(strm, level, strategy)
     }
     func = configuration_table[s->level].func;
 
-    if (func != configuration_table[level].func && strm->total_in != 0) {
+    if ((strategy != s->strategy || func != configuration_table[level].func) &&
+        strm->total_in != 0) {
         /* Flush the last buffer: */
-        err = deflate(strm, Z_PARTIAL_FLUSH);
+        err = deflate(strm, Z_BLOCK);
     }
     if (s->level != level) {
         s->level = level;
@@ -481,33 +478,66 @@ int ZEXPORT deflateTune(strm, good_length, max_lazy, nice_length, max_chain)
  * resulting from using fixed blocks instead of stored blocks, which deflate
  * can emit on compressed data for some combinations of the parameters.
  *
- * This function could be more sophisticated to provide closer upper bounds
- * for every combination of windowBits and memLevel, as well as wrap.
- * But even the conservative upper bound of about 14% expansion does not
- * seem onerous for output buffer allocation.
+ * This function could be more sophisticated to provide closer upper bounds for
+ * every combination of windowBits and memLevel.  But even the conservative
+ * upper bound of about 14% expansion does not seem onerous for output buffer
+ * allocation.
  */
 uLong ZEXPORT deflateBound(strm, sourceLen)
     z_streamp strm;
     uLong sourceLen;
 {
     deflate_state *s;
-    uLong destLen;
+    uLong complen, wraplen;
+    Bytef *str;
 
-    /* conservative upper bound */
-    destLen = sourceLen +
-              ((sourceLen + 7) >> 3) + ((sourceLen + 63) >> 6) + 11;
+    /* conservative upper bound for compressed data */
+    complen = sourceLen +
+              ((sourceLen + 7) >> 3) + ((sourceLen + 63) >> 6) + 5;
 
-    /* if can't get parameters, return conservative bound */
+    /* if can't get parameters, return conservative bound plus zlib wrapper */
     if (strm == Z_NULL || strm->state == Z_NULL)
-        return destLen;
+        return complen + 6;
 
-    /* if not default parameters, return conservative bound */
+    /* compute wrapper length */
     s = strm->state;
+    switch (s->wrap) {
+    case 0:                                 /* raw deflate */
+        wraplen = 0;
+        break;
+    case 1:                                 /* zlib wrapper */
+        wraplen = 6 + (s->strstart ? 4 : 0);
+        break;
+    case 2:                                 /* gzip wrapper */
+        wraplen = 18;
+        if (s->gzhead != Z_NULL) {          /* user-supplied gzip header */
+            if (s->gzhead->extra != Z_NULL)
+                wraplen += 2 + s->gzhead->extra_len;
+            str = s->gzhead->name;
+            if (str != Z_NULL)
+                do {
+                    wraplen++;
+                } while (*str++);
+            str = s->gzhead->comment;
+            if (str != Z_NULL)
+                do {
+                    wraplen++;
+                } while (*str++);
+            if (s->gzhead->hcrc)
+                wraplen += 2;
+        }
+        break;
+    default:                                /* for compiler happiness */
+        wraplen = 6;
+    }
+
+    /* if not default parameters, return conservative bound */
     if (s->w_bits != 15 || s->hash_bits != 8 + 7)
-        return destLen;
+        return complen + wraplen;
 
     /* default settings: return tight bound for that case */
-    return compressBound(sourceLen);
+    return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) +
+           (sourceLen >> 25) + 13 - 6 + wraplen;
 }
 
 /* =========================================================================
@@ -557,7 +587,7 @@ int ZEXPORT deflate (strm, flush)
     deflate_state *s;
 
     if (strm == Z_NULL || strm->state == Z_NULL ||
-        flush > Z_FINISH || flush < 0) {
+        flush > Z_BLOCK || flush < 0) {
         return Z_STREAM_ERROR;
     }
     s = strm->state;
@@ -581,7 +611,7 @@ int ZEXPORT deflate (strm, flush)
             put_byte(s, 31);
             put_byte(s, 139);
             put_byte(s, 8);
-            if (s->gzhead == NULL) {
+            if (s->gzhead == Z_NULL) {
                 put_byte(s, 0);
                 put_byte(s, 0);
                 put_byte(s, 0);
@@ -608,7 +638,7 @@ int ZEXPORT deflate (strm, flush)
                             (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ?
                              4 : 0));
                 put_byte(s, s->gzhead->os & 0xff);
-                if (s->gzhead->extra != NULL) {
+                if (s->gzhead->extra != Z_NULL) {
                     put_byte(s, s->gzhead->extra_len & 0xff);
                     put_byte(s, (s->gzhead->extra_len >> 8) & 0xff);
                 }
@@ -650,7 +680,7 @@ int ZEXPORT deflate (strm, flush)
     }
 #ifdef GZIP
     if (s->status == EXTRA_STATE) {
-        if (s->gzhead->extra != NULL) {
+        if (s->gzhead->extra != Z_NULL) {
             uInt beg = s->pending;  /* start of bytes to update crc */
 
             while (s->gzindex < (s->gzhead->extra_len & 0xffff)) {
@@ -678,7 +708,7 @@ int ZEXPORT deflate (strm, flush)
             s->status = NAME_STATE;
     }
     if (s->status == NAME_STATE) {
-        if (s->gzhead->name != NULL) {
+        if (s->gzhead->name != Z_NULL) {
             uInt beg = s->pending;  /* start of bytes to update crc */
             int val;
 
@@ -709,7 +739,7 @@ int ZEXPORT deflate (strm, flush)
             s->status = COMMENT_STATE;
     }
     if (s->status == COMMENT_STATE) {
-        if (s->gzhead->comment != NULL) {
+        if (s->gzhead->comment != Z_NULL) {
             uInt beg = s->pending;  /* start of bytes to update crc */
             int val;
 
@@ -787,7 +817,9 @@ int ZEXPORT deflate (strm, flush)
         (flush != Z_NO_FLUSH && s->status != FINISH_STATE)) {
         block_state bstate;
 
-        bstate = (*(configuration_table[s->level].func))(s, flush);
+        bstate = s->strategy == Z_HUFFMAN_ONLY ? deflate_huff(s, flush) :
+                    (s->strategy == Z_RLE ? deflate_rle(s, flush) :
+                        (*(configuration_table[s->level].func))(s, flush));
 
         if (bstate == finish_started || bstate == finish_done) {
             s->status = FINISH_STATE;
@@ -808,13 +840,17 @@ int ZEXPORT deflate (strm, flush)
         if (bstate == block_done) {
             if (flush == Z_PARTIAL_FLUSH) {
                 _tr_align(s);
-            } else { /* FULL_FLUSH or SYNC_FLUSH */
+            } else if (flush != Z_BLOCK) { /* FULL_FLUSH or SYNC_FLUSH */
                 _tr_stored_block(s, (char*)0, 0L, 0);
                 /* For a full flush, this empty block will be recognized
                  * as a special marker by inflate_sync().
                  */
                 if (flush == Z_FULL_FLUSH) {
                     CLEAR_HASH(s);             /* forget history */
+                    if (s->lookahead == 0) {
+                        s->strstart = 0;
+                        s->block_start = 0L;
+                    }
                 }
             }
             flush_pending(strm);
@@ -1167,12 +1203,13 @@ local uInt longest_match(s, cur_match)
     return s->lookahead;
 }
 #endif /* ASMV */
-#endif /* FASTEST */
+
+#else /* FASTEST */
 
 /* ---------------------------------------------------------------------------
- * Optimized version for level == 1 or strategy == Z_RLE only
+ * Optimized version for FASTEST only
  */
-local uInt longest_match_fast(s, cur_match)
+local uInt longest_match(s, cur_match)
     deflate_state *s;
     IPos cur_match;                             /* current match */
 {
@@ -1225,6 +1262,8 @@ local uInt longest_match_fast(s, cur_match)
     return (uInt)len <= s->lookahead ? (uInt)len : s->lookahead;
 }
 
+#endif /* FASTEST */
+
 #ifdef DEBUG
 /* ===========================================================================
  * Check that the match at match_start is indeed a match.
@@ -1303,7 +1342,6 @@ local void fill_window(s)
                later. (Using level 0 permanently is not an optimal usage of
                zlib, so we don't care about this pathological case.)
              */
-            /* %%% avoid this when Z_RLE */
             n = s->hash_size;
             p = &s->head[n];
             do {
@@ -1355,27 +1393,61 @@ local void fill_window(s)
          */
 
     } while (s->lookahead < MIN_LOOKAHEAD && s->strm->avail_in != 0);
+
+    /* If the WIN_INIT bytes after the end of the current data have never been
+     * written, then zero those bytes in order to avoid memory check reports of
+     * the use of uninitialized (or uninitialised as Julian writes) bytes by
+     * the longest match routines.  Update the high water mark for the next
+     * time through here.  WIN_INIT is set to MAX_MATCH since the longest match
+     * routines allow scanning to strstart + MAX_MATCH, ignoring lookahead.
+     */
+    if (s->high_water < s->window_size) {
+        ulg curr = s->strstart + (ulg)(s->lookahead);
+        ulg init;
+
+        if (s->high_water < curr) {
+            /* Previous high water mark below current data -- zero WIN_INIT
+             * bytes or up to end of window, whichever is less.
+             */
+            init = s->window_size - curr;
+            if (init > WIN_INIT)
+                init = WIN_INIT;
+            zmemzero(s->window + curr, (unsigned)init);
+            s->high_water = curr + init;
+        }
+        else if (s->high_water < (ulg)curr + WIN_INIT) {
+            /* High water mark at or above current data, but below current data
+             * plus WIN_INIT -- zero out to current data plus WIN_INIT, or up
+             * to end of window, whichever is less.
+             */
+            init = (ulg)curr + WIN_INIT - s->high_water;
+            if (init > s->window_size - s->high_water)
+                init = s->window_size - s->high_water;
+            zmemzero(s->window + s->high_water, (unsigned)init);
+            s->high_water += init;
+        }
+    }
 }
 
 /* ===========================================================================
  * Flush the current block, with given end-of-file flag.
  * IN assertion: strstart is set to the end of the current match.
  */
-#define FLUSH_BLOCK_ONLY(s, eof) { \
+#define FLUSH_BLOCK_ONLY(s, last) { \
    _tr_flush_block(s, (s->block_start >= 0L ? \
                    (charf *)&s->window[(unsigned)s->block_start] : \
                    (charf *)Z_NULL), \
                 (ulg)((long)s->strstart - s->block_start), \
-                (eof)); \
+                (last)); \
    s->block_start = s->strstart; \
    flush_pending(s->strm); \
    Tracev((stderr,"[FLUSH]")); \
 }
 
 /* Same but force premature exit if necessary. */
-#define FLUSH_BLOCK(s, eof) { \
-   FLUSH_BLOCK_ONLY(s, eof); \
-   if (s->strm->avail_out == 0) return (eof) ? finish_started : need_more; \
+#define FLUSH_BLOCK(s, last) { \
+   FLUSH_BLOCK_ONLY(s, last); \
+   if (s->strm->avail_out == 0) return (last) ? finish_started : need_more; \
 }
 
 /* ===========================================================================
@@ -1449,7 +1521,7 @@ local block_state deflate_fast(s, flush)
     deflate_state *s;
     int flush;
 {
-    IPos hash_head = NIL; /* head of the hash chain */
+    IPos hash_head;       /* head of the hash chain */
     int bflush;           /* set if current block must be flushed */
 
     for (;;) {
@@ -1469,6 +1541,7 @@ local block_state deflate_fast(s, flush)
         /* Insert the string window[strstart .. strstart+2] in the
          * dictionary, and set hash_head to the head of the hash chain:
          */
+        hash_head = NIL;
         if (s->lookahead >= MIN_MATCH) {
             INSERT_STRING(s, s->strstart, hash_head);
         }
@@ -1481,19 +1554,8 @@ local block_state deflate_fast(s, flush)
              * of window index 0 (in particular we have to avoid a match
              * of the string with itself at the start of the input file).
              */
-#ifdef FASTEST
-            if ((s->strategy != Z_HUFFMAN_ONLY && s->strategy != Z_RLE) ||
-                (s->strategy == Z_RLE && s->strstart - hash_head == 1)) {
-                s->match_length = longest_match_fast (s, hash_head);
-            }
-#else
-            if (s->strategy != Z_HUFFMAN_ONLY && s->strategy != Z_RLE) {
-                s->match_length = longest_match (s, hash_head);
-            } else if (s->strategy == Z_RLE && s->strstart - hash_head == 1) {
-                s->match_length = longest_match_fast (s, hash_head);
-            }
-#endif
-            /* longest_match() or longest_match_fast() sets match_start */
+            s->match_length = longest_match (s, hash_head);
+            /* longest_match() sets match_start */
         }
         if (s->match_length >= MIN_MATCH) {
             check_match(s, s->strstart, s->match_start, s->match_length);
@@ -1555,7 +1617,7 @@ local block_state deflate_slow(s, flush)
     deflate_state *s;
     int flush;
 {
-    IPos hash_head = NIL;    /* head of hash chain */
+    IPos hash_head;          /* head of hash chain */
     int bflush;              /* set if current block must be flushed */
 
     /* Process the input block. */
@@ -1576,6 +1638,7 @@ local block_state deflate_slow(s, flush)
         /* Insert the string window[strstart .. strstart+2] in the
          * dictionary, and set hash_head to the head of the hash chain:
          */
+        hash_head = NIL;
         if (s->lookahead >= MIN_MATCH) {
             INSERT_STRING(s, s->strstart, hash_head);
         }
@@ -1591,12 +1654,8 @@ local block_state deflate_slow(s, flush)
              * of window index 0 (in particular we have to avoid a match
              * of the string with itself at the start of the input file).
              */
-            if (s->strategy != Z_HUFFMAN_ONLY && s->strategy != Z_RLE) {
-                s->match_length = longest_match (s, hash_head);
-            } else if (s->strategy == Z_RLE && s->strstart - hash_head == 1) {
-                s->match_length = longest_match_fast (s, hash_head);
-            }
-            /* longest_match() or longest_match_fast() sets match_start */
+            s->match_length = longest_match (s, hash_head);
+            /* longest_match() sets match_start */
 
             if (s->match_length <= 5 && (s->strategy == Z_FILTERED
 #if TOO_FAR <= 32767
@@ -1674,7 +1733,6 @@ local block_state deflate_slow(s, flush)
 }
 #endif /* FASTEST */
 
-#if 0
 /* ===========================================================================
  * For Z_RLE, simply look for runs of bytes, generate matches only of distance
  * one.  Do not maintain a hash table.  (It will be regenerated if this run of
@@ -1684,11 +1742,9 @@ local block_state deflate_rle(s, flush)
     deflate_state *s;
     int flush;
 {
-    int bflush;         /* set if current block must be flushed */
-    uInt run;           /* length of run */
-    uInt max;           /* maximum length of run */
-    uInt prev;          /* byte at distance one to match */
-    Bytef *scan;        /* scan for end of run */
+    int bflush;             /* set if current block must be flushed */
+    uInt prev;              /* byte at distance one to match */
+    Bytef *scan, *strend;   /* scan goes up to strend for length of run */
 
     for (;;) {
         /* Make sure that we always have enough lookahead, except
@@ -1704,23 +1760,33 @@ local block_state deflate_rle(s, flush)
         }
 
         /* See how many times the previous byte repeats */
-        run = 0;
-        if (s->strstart > 0) {      /* if there is a previous byte, that is */
-            max = s->lookahead < MAX_MATCH ? s->lookahead : MAX_MATCH;
+        s->match_length = 0;
+        if (s->lookahead >= MIN_MATCH && s->strstart > 0) {
             scan = s->window + s->strstart - 1;
-            prev = *scan++;
-            do {
-                if (*scan++ != prev)
-                    break;
-            } while (++run < max);
+            prev = *scan;
+            if (prev == *++scan && prev == *++scan && prev == *++scan) {
+                strend = s->window + s->strstart + MAX_MATCH;
+                do {
+                } while (prev == *++scan && prev == *++scan &&
+                         prev == *++scan && prev == *++scan &&
+                         prev == *++scan && prev == *++scan &&
+                         prev == *++scan && prev == *++scan &&
+                         scan < strend);
+                s->match_length = MAX_MATCH - (int)(strend - scan);
+                if (s->match_length > s->lookahead)
+                    s->match_length = s->lookahead;
+            }
         }
 
         /* Emit match if have run of MIN_MATCH or longer, else emit literal */
-        if (run >= MIN_MATCH) {
-            check_match(s, s->strstart, s->strstart - 1, run);
-            _tr_tally_dist(s, 1, run - MIN_MATCH, bflush);
-            s->lookahead -= run;
-            s->strstart += run;
+        if (s->match_length >= MIN_MATCH) {
+            check_match(s, s->strstart, s->strstart - 1, s->match_length);
+
+            _tr_tally_dist(s, 1, s->match_length - MIN_MATCH, bflush);
+
+            s->lookahead -= s->match_length;
+            s->strstart += s->match_length;
+            s->match_length = 0;
         } else {
             /* No match, output a literal byte */
             Tracevv((stderr,"%c", s->window[s->strstart]));
@@ -1733,4 +1799,36 @@ local block_state deflate_rle(s, flush)
     FLUSH_BLOCK(s, flush == Z_FINISH);
     return flush == Z_FINISH ? finish_done : block_done;
 }
-#endif
+
+/* ===========================================================================
+ * For Z_HUFFMAN_ONLY, do not look for matches.  Do not maintain a hash table.
+ * (It will be regenerated if this run of deflate switches away from Huffman.)
+ */
+local block_state deflate_huff(s, flush)
+    deflate_state *s;
+    int flush;
+{
+    int bflush;             /* set if current block must be flushed */
+
+    for (;;) {
+        /* Make sure that we have a literal to write. */
+        if (s->lookahead == 0) {
+            fill_window(s);
+            if (s->lookahead == 0) {
+                if (flush == Z_NO_FLUSH)
+                    return need_more;
+                break;      /* flush the current block */
+            }
+        }
+
+        /* Output a literal byte */
+        s->match_length = 0;
+        Tracevv((stderr,"%c", s->window[s->strstart]));
+        _tr_tally_lit (s, s->window[s->strstart], bflush);
+        s->lookahead--;
+        s->strstart++;
+        if (bflush) FLUSH_BLOCK(s, 0);
+    }
+    FLUSH_BLOCK(s, flush == Z_FINISH);
+    return flush == Z_FINISH ? finish_done : block_done;
+}
index 91c10fc..cbf0d1e 100644 (file)
@@ -1,5 +1,5 @@
 /* deflate.h -- internal compression state
- * Copyright (C) 1995-2004 Jean-loup Gailly
+ * Copyright (C) 1995-2010 Jean-loup Gailly
  * For conditions of distribution and use, see copyright notice in zlib.h
  */
 
@@ -8,7 +8,7 @@
    subject to change. Applications should only use zlib.h.
  */
 
-/* @(#) $Id: deflate.h,v 1.2 2008-05-26 19:08:10 vp153 Exp $ */
+/* @(#) $Id$ */
 
 #ifndef DEFLATE_H
 #define DEFLATE_H
@@ -260,6 +260,13 @@ typedef struct internal_state {
      * are always zero.
      */
 
+    ulg high_water;
+    /* High water mark offset in window for initialized bytes -- bytes above
+     * this are set to zero in order to avoid memory check warnings when
+     * longest match routines access bytes past the input.  This is then
+     * updated to the new high water mark.
+     */
+
 } FAR deflate_state;
 
 /* Output a byte on the stream.
@@ -278,14 +285,18 @@ typedef struct internal_state {
  * distances are limited to MAX_DIST instead of WSIZE.
  */
 
+#define WIN_INIT MAX_MATCH
+/* Number of bytes after end of data in window to initialize in order to avoid
+   memory checker errors from longest match routines */
+
         /* in trees.c */
-void _tr_init         OF((deflate_state *s));
-int  _tr_tally        OF((deflate_state *s, unsigned dist, unsigned lc));
-void _tr_flush_block  OF((deflate_state *s, charf *buf, ulg stored_len,
-                          int eof));
-void _tr_align        OF((deflate_state *s));
-void _tr_stored_block OF((deflate_state *s, charf *buf, ulg stored_len,
-                          int eof));
+void ZLIB_INTERNAL _tr_init OF((deflate_state *s));
+int ZLIB_INTERNAL _tr_tally OF((deflate_state *s, unsigned dist, unsigned lc));
+void ZLIB_INTERNAL _tr_flush_block OF((deflate_state *s, charf *buf,
+                        ulg stored_len, int last));
+void ZLIB_INTERNAL _tr_align OF((deflate_state *s));
+void ZLIB_INTERNAL _tr_stored_block OF((deflate_state *s, charf *buf,
+                        ulg stored_len, int last));
 
 #define d_code(dist) \
    ((dist) < 256 ? _dist_code[dist] : _dist_code[256+((dist)>>7)])
@@ -298,11 +309,11 @@ void _tr_stored_block OF((deflate_state *s, charf *buf, ulg stored_len,
 /* Inline versions of _tr_tally for speed: */
 
 #if defined(GEN_TREES_H) || !defined(STDC)
-  extern uch _length_code[];
-  extern uch _dist_code[];
+  extern uch ZLIB_INTERNAL _length_code[];
+  extern uch ZLIB_INTERNAL _dist_code[];
 #else
-  extern const uch _length_code[];
-  extern const uch _dist_code[];
+  extern const uch ZLIB_INTERNAL _length_code[];
+  extern const uch ZLIB_INTERNAL _dist_code[];
 #endif
 
 # define _tr_tally_lit(s, c, flush) \
diff --git a/3rdparty/zlib/example.c b/3rdparty/zlib/example.c
deleted file mode 100644 (file)
index 52a77c5..0000000
+++ /dev/null
@@ -1,565 +0,0 @@
-/* example.c -- usage example of the zlib compression library
- * Copyright (C) 1995-2004 Jean-loup Gailly.
- * For conditions of distribution and use, see copyright notice in zlib.h
- */
-
-/* @(#) $Id: example.c,v 1.2 2008-05-26 19:08:10 vp153 Exp $ */
-
-#include <stdio.h>
-#include "zlib.h"
-
-#ifdef STDC
-#  include <string.h>
-#  include <stdlib.h>
-#endif
-
-#if defined(VMS) || defined(RISCOS)
-#  define TESTFILE "foo-gz"
-#else
-#  define TESTFILE "foo.gz"
-#endif
-
-#define CHECK_ERR(err, msg) { \
-    if (err != Z_OK) { \
-        fprintf(stderr, "%s error: %d\n", msg, err); \
-        exit(1); \
-    } \
-}
-
-const char hello[] = "hello, hello!";
-/* "hello world" would be more standard, but the repeated "hello"
- * stresses the compression code better, sorry...
- */
-
-const char dictionary[] = "hello";
-uLong dictId; /* Adler32 value of the dictionary */
-
-void test_compress      OF((Byte *compr, uLong comprLen,
-                            Byte *uncompr, uLong uncomprLen));
-void test_gzio          OF((const char *fname,
-                            Byte *uncompr, uLong uncomprLen));
-void test_deflate       OF((Byte *compr, uLong comprLen));
-void test_inflate       OF((Byte *compr, uLong comprLen,
-                            Byte *uncompr, uLong uncomprLen));
-void test_large_deflate OF((Byte *compr, uLong comprLen,
-                            Byte *uncompr, uLong uncomprLen));
-void test_large_inflate OF((Byte *compr, uLong comprLen,
-                            Byte *uncompr, uLong uncomprLen));
-void test_flush         OF((Byte *compr, uLong *comprLen));
-void test_sync          OF((Byte *compr, uLong comprLen,
-                            Byte *uncompr, uLong uncomprLen));
-void test_dict_deflate  OF((Byte *compr, uLong comprLen));
-void test_dict_inflate  OF((Byte *compr, uLong comprLen,
-                            Byte *uncompr, uLong uncomprLen));
-int  main               OF((int argc, char *argv[]));
-
-/* ===========================================================================
- * Test compress() and uncompress()
- */
-void test_compress(compr, comprLen, uncompr, uncomprLen)
-    Byte *compr, *uncompr;
-    uLong comprLen, uncomprLen;
-{
-    int err;
-    uLong len = (uLong)strlen(hello)+1;
-
-    err = compress(compr, &comprLen, (const Bytef*)hello, len);
-    CHECK_ERR(err, "compress");
-
-    strcpy((char*)uncompr, "garbage");
-
-    err = uncompress(uncompr, &uncomprLen, compr, comprLen);
-    CHECK_ERR(err, "uncompress");
-
-    if (strcmp((char*)uncompr, hello)) {
-        fprintf(stderr, "bad uncompress\n");
-        exit(1);
-    } else {
-        printf("uncompress(): %s\n", (char *)uncompr);
-    }
-}
-
-/* ===========================================================================
- * Test read/write of .gz files
- */
-void test_gzio(fname, uncompr, uncomprLen)
-    const char *fname; /* compressed file name */
-    Byte *uncompr;
-    uLong uncomprLen;
-{
-#ifdef NO_GZCOMPRESS
-    fprintf(stderr, "NO_GZCOMPRESS -- gz* functions cannot compress\n");
-#else
-    int err;
-    int len = (int)strlen(hello)+1;
-    gzFile file;
-    z_off_t pos;
-
-    file = gzopen(fname, "wb");
-    if (file == NULL) {
-        fprintf(stderr, "gzopen error\n");
-        exit(1);
-    }
-    gzputc(file, 'h');
-    if (gzputs(file, "ello") != 4) {
-        fprintf(stderr, "gzputs err: %s\n", gzerror(file, &err));
-        exit(1);
-    }
-    if (gzprintf(file, ", %s!", "hello") != 8) {
-        fprintf(stderr, "gzprintf err: %s\n", gzerror(file, &err));
-        exit(1);
-    }
-    gzseek(file, 1L, SEEK_CUR); /* add one zero byte */
-    gzclose(file);
-
-    file = gzopen(fname, "rb");
-    if (file == NULL) {
-        fprintf(stderr, "gzopen error\n");
-        exit(1);
-    }
-    strcpy((char*)uncompr, "garbage");
-
-    if (gzread(file, uncompr, (unsigned)uncomprLen) != len) {
-        fprintf(stderr, "gzread err: %s\n", gzerror(file, &err));
-        exit(1);
-    }
-    if (strcmp((char*)uncompr, hello)) {
-        fprintf(stderr, "bad gzread: %s\n", (char*)uncompr);
-        exit(1);
-    } else {
-        printf("gzread(): %s\n", (char*)uncompr);
-    }
-
-    pos = gzseek(file, -8L, SEEK_CUR);
-    if (pos != 6 || gztell(file) != pos) {
-        fprintf(stderr, "gzseek error, pos=%ld, gztell=%ld\n",
-                (long)pos, (long)gztell(file));
-        exit(1);
-    }
-
-    if (gzgetc(file) != ' ') {
-        fprintf(stderr, "gzgetc error\n");
-        exit(1);
-    }
-
-    if (gzungetc(' ', file) != ' ') {
-        fprintf(stderr, "gzungetc error\n");
-        exit(1);
-    }
-
-    gzgets(file, (char*)uncompr, (int)uncomprLen);
-    if (strlen((char*)uncompr) != 7) { /* " hello!" */
-        fprintf(stderr, "gzgets err after gzseek: %s\n", gzerror(file, &err));
-        exit(1);
-    }
-    if (strcmp((char*)uncompr, hello + 6)) {
-        fprintf(stderr, "bad gzgets after gzseek\n");
-        exit(1);
-    } else {
-        printf("gzgets() after gzseek: %s\n", (char*)uncompr);
-    }
-
-    gzclose(file);
-#endif
-}
-
-/* ===========================================================================
- * Test deflate() with small buffers
- */
-void test_deflate(compr, comprLen)
-    Byte *compr;
-    uLong comprLen;
-{
-    z_stream c_stream; /* compression stream */
-    int err;
-    uLong len = (uLong)strlen(hello)+1;
-
-    c_stream.zalloc = (alloc_func)0;
-    c_stream.zfree = (free_func)0;
-    c_stream.opaque = (voidpf)0;
-
-    err = deflateInit(&c_stream, Z_DEFAULT_COMPRESSION);
-    CHECK_ERR(err, "deflateInit");
-
-    c_stream.next_in  = (Bytef*)hello;
-    c_stream.next_out = compr;
-
-    while (c_stream.total_in != len && c_stream.total_out < comprLen) {
-        c_stream.avail_in = c_stream.avail_out = 1; /* force small buffers */
-        err = deflate(&c_stream, Z_NO_FLUSH);
-        CHECK_ERR(err, "deflate");
-    }
-    /* Finish the stream, still forcing small buffers: */
-    for (;;) {
-        c_stream.avail_out = 1;
-        err = deflate(&c_stream, Z_FINISH);
-        if (err == Z_STREAM_END) break;
-        CHECK_ERR(err, "deflate");
-    }
-
-    err = deflateEnd(&c_stream);
-    CHECK_ERR(err, "deflateEnd");
-}
-
-/* ===========================================================================
- * Test inflate() with small buffers
- */
-void test_inflate(compr, comprLen, uncompr, uncomprLen)
-    Byte *compr, *uncompr;
-    uLong comprLen, uncomprLen;
-{
-    int err;
-    z_stream d_stream; /* decompression stream */
-
-    strcpy((char*)uncompr, "garbage");
-
-    d_stream.zalloc = (alloc_func)0;
-    d_stream.zfree = (free_func)0;
-    d_stream.opaque = (voidpf)0;
-
-    d_stream.next_in  = compr;
-    d_stream.avail_in = 0;
-    d_stream.next_out = uncompr;
-
-    err = inflateInit(&d_stream);
-    CHECK_ERR(err, "inflateInit");
-
-    while (d_stream.total_out < uncomprLen && d_stream.total_in < comprLen) {
-        d_stream.avail_in = d_stream.avail_out = 1; /* force small buffers */
-        err = inflate(&d_stream, Z_NO_FLUSH);
-        if (err == Z_STREAM_END) break;
-        CHECK_ERR(err, "inflate");
-    }
-
-    err = inflateEnd(&d_stream);
-    CHECK_ERR(err, "inflateEnd");
-
-    if (strcmp((char*)uncompr, hello)) {
-        fprintf(stderr, "bad inflate\n");
-        exit(1);
-    } else {
-        printf("inflate(): %s\n", (char *)uncompr);
-    }
-}
-
-/* ===========================================================================
- * Test deflate() with large buffers and dynamic change of compression level
- */
-void test_large_deflate(compr, comprLen, uncompr, uncomprLen)
-    Byte *compr, *uncompr;
-    uLong comprLen, uncomprLen;
-{
-    z_stream c_stream; /* compression stream */
-    int err;
-
-    c_stream.zalloc = (alloc_func)0;
-    c_stream.zfree = (free_func)0;
-    c_stream.opaque = (voidpf)0;
-
-    err = deflateInit(&c_stream, Z_BEST_SPEED);
-    CHECK_ERR(err, "deflateInit");
-
-    c_stream.next_out = compr;
-    c_stream.avail_out = (uInt)comprLen;
-
-    /* At this point, uncompr is still mostly zeroes, so it should compress
-     * very well:
-     */
-    c_stream.next_in = uncompr;
-    c_stream.avail_in = (uInt)uncomprLen;
-    err = deflate(&c_stream, Z_NO_FLUSH);
-    CHECK_ERR(err, "deflate");
-    if (c_stream.avail_in != 0) {
-        fprintf(stderr, "deflate not greedy\n");
-        exit(1);
-    }
-
-    /* Feed in already compressed data and switch to no compression: */
-    deflateParams(&c_stream, Z_NO_COMPRESSION, Z_DEFAULT_STRATEGY);
-    c_stream.next_in = compr;
-    c_stream.avail_in = (uInt)comprLen/2;
-    err = deflate(&c_stream, Z_NO_FLUSH);
-    CHECK_ERR(err, "deflate");
-
-    /* Switch back to compressing mode: */
-    deflateParams(&c_stream, Z_BEST_COMPRESSION, Z_FILTERED);
-    c_stream.next_in = uncompr;
-    c_stream.avail_in = (uInt)uncomprLen;
-    err = deflate(&c_stream, Z_NO_FLUSH);
-    CHECK_ERR(err, "deflate");
-
-    err = deflate(&c_stream, Z_FINISH);
-    if (err != Z_STREAM_END) {
-        fprintf(stderr, "deflate should report Z_STREAM_END\n");
-        exit(1);
-    }
-    err = deflateEnd(&c_stream);
-    CHECK_ERR(err, "deflateEnd");
-}
-
-/* ===========================================================================
- * Test inflate() with large buffers
- */
-void test_large_inflate(compr, comprLen, uncompr, uncomprLen)
-    Byte *compr, *uncompr;
-    uLong comprLen, uncomprLen;
-{
-    int err;
-    z_stream d_stream; /* decompression stream */
-
-    strcpy((char*)uncompr, "garbage");
-
-    d_stream.zalloc = (alloc_func)0;
-    d_stream.zfree = (free_func)0;
-    d_stream.opaque = (voidpf)0;
-
-    d_stream.next_in  = compr;
-    d_stream.avail_in = (uInt)comprLen;
-
-    err = inflateInit(&d_stream);
-    CHECK_ERR(err, "inflateInit");
-
-    for (;;) {
-        d_stream.next_out = uncompr;            /* discard the output */
-        d_stream.avail_out = (uInt)uncomprLen;
-        err = inflate(&d_stream, Z_NO_FLUSH);
-        if (err == Z_STREAM_END) break;
-        CHECK_ERR(err, "large inflate");
-    }
-
-    err = inflateEnd(&d_stream);
-    CHECK_ERR(err, "inflateEnd");
-
-    if (d_stream.total_out != 2*uncomprLen + comprLen/2) {
-        fprintf(stderr, "bad large inflate: %ld\n", d_stream.total_out);
-        exit(1);
-    } else {
-        printf("large_inflate(): OK\n");
-    }
-}
-
-/* ===========================================================================
- * Test deflate() with full flush
- */
-void test_flush(compr, comprLen)
-    Byte *compr;
-    uLong *comprLen;
-{
-    z_stream c_stream; /* compression stream */
-    int err;
-    uInt len = (uInt)strlen(hello)+1;
-
-    c_stream.zalloc = (alloc_func)0;
-    c_stream.zfree = (free_func)0;
-    c_stream.opaque = (voidpf)0;
-
-    err = deflateInit(&c_stream, Z_DEFAULT_COMPRESSION);
-    CHECK_ERR(err, "deflateInit");
-
-    c_stream.next_in  = (Bytef*)hello;
-    c_stream.next_out = compr;
-    c_stream.avail_in = 3;
-    c_stream.avail_out = (uInt)*comprLen;
-    err = deflate(&c_stream, Z_FULL_FLUSH);
-    CHECK_ERR(err, "deflate");
-
-    compr[3]++; /* force an error in first compressed block */
-    c_stream.avail_in = len - 3;
-
-    err = deflate(&c_stream, Z_FINISH);
-    if (err != Z_STREAM_END) {
-        CHECK_ERR(err, "deflate");
-    }
-    err = deflateEnd(&c_stream);
-    CHECK_ERR(err, "deflateEnd");
-
-    *comprLen = c_stream.total_out;
-}
-
-/* ===========================================================================
- * Test inflateSync()
- */
-void test_sync(compr, comprLen, uncompr, uncomprLen)
-    Byte *compr, *uncompr;
-    uLong comprLen, uncomprLen;
-{
-    int err;
-    z_stream d_stream; /* decompression stream */
-
-    strcpy((char*)uncompr, "garbage");
-
-    d_stream.zalloc = (alloc_func)0;
-    d_stream.zfree = (free_func)0;
-    d_stream.opaque = (voidpf)0;
-
-    d_stream.next_in  = compr;
-    d_stream.avail_in = 2; /* just read the zlib header */
-
-    err = inflateInit(&d_stream);
-    CHECK_ERR(err, "inflateInit");
-
-    d_stream.next_out = uncompr;
-    d_stream.avail_out = (uInt)uncomprLen;
-
-    inflate(&d_stream, Z_NO_FLUSH);
-    CHECK_ERR(err, "inflate");
-
-    d_stream.avail_in = (uInt)comprLen-2;   /* read all compressed data */
-    err = inflateSync(&d_stream);           /* but skip the damaged part */
-    CHECK_ERR(err, "inflateSync");
-
-    err = inflate(&d_stream, Z_FINISH);
-    if (err != Z_DATA_ERROR) {
-        fprintf(stderr, "inflate should report DATA_ERROR\n");
-        /* Because of incorrect adler32 */
-        exit(1);
-    }
-    err = inflateEnd(&d_stream);
-    CHECK_ERR(err, "inflateEnd");
-
-    printf("after inflateSync(): hel%s\n", (char *)uncompr);
-}
-
-/* ===========================================================================
- * Test deflate() with preset dictionary
- */
-void test_dict_deflate(compr, comprLen)
-    Byte *compr;
-    uLong comprLen;
-{
-    z_stream c_stream; /* compression stream */
-    int err;
-
-    c_stream.zalloc = (alloc_func)0;
-    c_stream.zfree = (free_func)0;
-    c_stream.opaque = (voidpf)0;
-
-    err = deflateInit(&c_stream, Z_BEST_COMPRESSION);
-    CHECK_ERR(err, "deflateInit");
-
-    err = deflateSetDictionary(&c_stream,
-                               (const Bytef*)dictionary, sizeof(dictionary));
-    CHECK_ERR(err, "deflateSetDictionary");
-
-    dictId = c_stream.adler;
-    c_stream.next_out = compr;
-    c_stream.avail_out = (uInt)comprLen;
-
-    c_stream.next_in = (Bytef*)hello;
-    c_stream.avail_in = (uInt)strlen(hello)+1;
-
-    err = deflate(&c_stream, Z_FINISH);
-    if (err != Z_STREAM_END) {
-        fprintf(stderr, "deflate should report Z_STREAM_END\n");
-        exit(1);
-    }
-    err = deflateEnd(&c_stream);
-    CHECK_ERR(err, "deflateEnd");
-}
-
-/* ===========================================================================
- * Test inflate() with a preset dictionary
- */
-void test_dict_inflate(compr, comprLen, uncompr, uncomprLen)
-    Byte *compr, *uncompr;
-    uLong comprLen, uncomprLen;
-{
-    int err;
-    z_stream d_stream; /* decompression stream */
-
-    strcpy((char*)uncompr, "garbage");
-
-    d_stream.zalloc = (alloc_func)0;
-    d_stream.zfree = (free_func)0;
-    d_stream.opaque = (voidpf)0;
-
-    d_stream.next_in  = compr;
-    d_stream.avail_in = (uInt)comprLen;
-
-    err = inflateInit(&d_stream);
-    CHECK_ERR(err, "inflateInit");
-
-    d_stream.next_out = uncompr;
-    d_stream.avail_out = (uInt)uncomprLen;
-
-    for (;;) {
-        err = inflate(&d_stream, Z_NO_FLUSH);
-        if (err == Z_STREAM_END) break;
-        if (err == Z_NEED_DICT) {
-            if (d_stream.adler != dictId) {
-                fprintf(stderr, "unexpected dictionary");
-                exit(1);
-            }
-            err = inflateSetDictionary(&d_stream, (const Bytef*)dictionary,
-                                       sizeof(dictionary));
-        }
-        CHECK_ERR(err, "inflate with dict");
-    }
-
-    err = inflateEnd(&d_stream);
-    CHECK_ERR(err, "inflateEnd");
-
-    if (strcmp((char*)uncompr, hello)) {
-        fprintf(stderr, "bad inflate with dict\n");
-        exit(1);
-    } else {
-        printf("inflate with dictionary: %s\n", (char *)uncompr);
-    }
-}
-
-/* ===========================================================================
- * Usage:  example [output.gz  [input.gz]]
- */
-
-int main(argc, argv)
-    int argc;
-    char *argv[];
-{
-    Byte *compr, *uncompr;
-    uLong comprLen = 10000*sizeof(int); /* don't overflow on MSDOS */
-    uLong uncomprLen = comprLen;
-    static const char* myVersion = ZLIB_VERSION;
-
-    if (zlibVersion()[0] != myVersion[0]) {
-        fprintf(stderr, "incompatible zlib version\n");
-        exit(1);
-
-    } else if (strcmp(zlibVersion(), ZLIB_VERSION) != 0) {
-        fprintf(stderr, "warning: different zlib version\n");
-    }
-
-    printf("zlib version %s = 0x%04x, compile flags = 0x%lx\n",
-            ZLIB_VERSION, ZLIB_VERNUM, zlibCompileFlags());
-
-    compr    = (Byte*)calloc((uInt)comprLen, 1);
-    uncompr  = (Byte*)calloc((uInt)uncomprLen, 1);
-    /* compr and uncompr are cleared to avoid reading uninitialized
-     * data and to ensure that uncompr compresses well.
-     */
-    if (compr == Z_NULL || uncompr == Z_NULL) {
-        printf("out of memory\n");
-        exit(1);
-    }
-    test_compress(compr, comprLen, uncompr, uncomprLen);
-
-    test_gzio((argc > 1 ? argv[1] : TESTFILE),
-              uncompr, uncomprLen);
-
-    test_deflate(compr, comprLen);
-    test_inflate(compr, comprLen, uncompr, uncomprLen);
-
-    test_large_deflate(compr, comprLen, uncompr, uncomprLen);
-    test_large_inflate(compr, comprLen, uncompr, uncomprLen);
-
-    test_flush(compr, &comprLen);
-    test_sync(compr, comprLen, uncompr, uncomprLen);
-    comprLen = uncomprLen;
-
-    test_dict_deflate(compr, comprLen);
-    test_dict_inflate(compr, comprLen, uncompr, uncomprLen);
-
-    free(compr);
-    free(uncompr);
-
-    return 0;
-}
diff --git a/3rdparty/zlib/gzclose.c b/3rdparty/zlib/gzclose.c
new file mode 100644 (file)
index 0000000..caeb99a
--- /dev/null
@@ -0,0 +1,25 @@
+/* gzclose.c -- zlib gzclose() function
+ * Copyright (C) 2004, 2010 Mark Adler
+ * For conditions of distribution and use, see copyright notice in zlib.h
+ */
+
+#include "gzguts.h"
+
+/* gzclose() is in a separate file so that it is linked in only if it is used.
+   That way the other gzclose functions can be used instead to avoid linking in
+   unneeded compression or decompression routines. */
+int ZEXPORT gzclose(file)
+    gzFile file;
+{
+#ifndef NO_GZCOMPRESS
+    gz_statep state;
+
+    if (file == NULL)
+        return Z_STREAM_ERROR;
+    state = (gz_statep)file;
+
+    return state->mode == GZ_READ ? gzclose_r(file) : gzclose_w(file);
+#else
+    return gzclose_r(file);
+#endif
+}
diff --git a/3rdparty/zlib/gzguts.h b/3rdparty/zlib/gzguts.h
new file mode 100644 (file)
index 0000000..0f8fb79
--- /dev/null
@@ -0,0 +1,132 @@
+/* gzguts.h -- zlib internal header definitions for gz* operations
+ * Copyright (C) 2004, 2005, 2010 Mark Adler
+ * For conditions of distribution and use, see copyright notice in zlib.h
+ */
+
+#ifdef _LARGEFILE64_SOURCE
+#  ifndef _LARGEFILE_SOURCE
+#    define _LARGEFILE_SOURCE 1
+#  endif
+#  ifdef _FILE_OFFSET_BITS
+#    undef _FILE_OFFSET_BITS
+#  endif
+#endif
+
+#if ((__GNUC__-0) * 10 + __GNUC_MINOR__-0 >= 33) && !defined(NO_VIZ)
+#  define ZLIB_INTERNAL __attribute__((visibility ("hidden")))
+#else
+#  define ZLIB_INTERNAL
+#endif
+
+#include <stdio.h>
+#include "zlib.h"
+#ifdef STDC
+#  include <string.h>
+#  include <stdlib.h>
+#  include <limits.h>
+#endif
+#include <fcntl.h>
+
+#ifdef NO_DEFLATE       /* for compatibility with old definition */
+#  define NO_GZCOMPRESS
+#endif
+
+#ifdef _MSC_VER
+#  include <io.h>
+#  define vsnprintf _vsnprintf
+#endif
+
+#ifndef local
+#  define local static
+#endif
+/* compile with -Dlocal if your debugger can't find static symbols */
+
+/* gz* functions always use library allocation functions */
+#ifndef STDC
+  extern voidp  malloc OF((uInt size));
+  extern void   free   OF((voidpf ptr));
+#endif
+
+/* get errno and strerror definition */
+#if defined UNDER_CE
+#  include <windows.h>
+#  define zstrerror() gz_strwinerror((DWORD)GetLastError())
+#else
+#  ifdef STDC
+#    include <errno.h>
+#    define zstrerror() strerror(errno)
+#  else
+#    define zstrerror() "stdio error (consult errno)"
+#  endif
+#endif
+
+/* provide prototypes for these when building zlib without LFS */
+#if !defined(_LARGEFILE64_SOURCE) || _LFS64_LARGEFILE-0 == 0
+    ZEXTERN gzFile ZEXPORT gzopen64 OF((const char *, const char *));
+    ZEXTERN z_off64_t ZEXPORT gzseek64 OF((gzFile, z_off64_t, int));
+    ZEXTERN z_off64_t ZEXPORT gztell64 OF((gzFile));
+    ZEXTERN z_off64_t ZEXPORT gzoffset64 OF((gzFile));
+#endif
+
+/* default i/o buffer size -- double this for output when reading */
+#define GZBUFSIZE 8192
+
+/* gzip modes, also provide a little integrity check on the passed structure */
+#define GZ_NONE 0
+#define GZ_READ 7247
+#define GZ_WRITE 31153
+#define GZ_APPEND 1     /* mode set to GZ_WRITE after the file is opened */
+
+/* values for gz_state how */
+#define LOOK 0      /* look for a gzip header */
+#define COPY 1      /* copy input directly */
+#define GZIP 2      /* decompress a gzip stream */
+
+/* internal gzip file state data structure */
+typedef struct {
+        /* used for both reading and writing */
+    int mode;               /* see gzip modes above */
+    int fd;                 /* file descriptor */
+    char *path;             /* path or fd for error messages */
+    z_off64_t pos;          /* current position in uncompressed data */
+    unsigned size;          /* buffer size, zero if not allocated yet */
+    unsigned want;          /* requested buffer size, default is GZBUFSIZE */
+    unsigned char *in;      /* input buffer */
+    unsigned char *out;     /* output buffer (double-sized when reading) */
+    unsigned char *next;    /* next output data to deliver or write */
+        /* just for reading */
+    unsigned have;          /* amount of output data unused at next */
+    int eof;                /* true if end of input file reached */
+    z_off64_t start;        /* where the gzip data started, for rewinding */
+    z_off64_t raw;          /* where the raw data started, for seeking */
+    int how;                /* 0: get header, 1: copy, 2: decompress */
+    int direct;             /* true if last read direct, false if gzip */
+        /* just for writing */
+    int level;              /* compression level */
+    int strategy;           /* compression strategy */
+        /* seek request */
+    z_off64_t skip;         /* amount to skip (already rewound if backwards) */
+    int seek;               /* true if seek request pending */
+        /* error information */
+    int err;                /* error code */
+    char *msg;              /* error message */
+        /* zlib inflate or deflate stream */
+    z_stream strm;          /* stream structure in-place (not a pointer) */
+} gz_state;
+typedef gz_state FAR *gz_statep;
+
+/* shared functions */
+void ZLIB_INTERNAL gz_error OF((gz_statep, int, const char *));
+#if defined UNDER_CE
+char ZLIB_INTERNAL *gz_strwinerror OF((DWORD error));
+#endif
+
+/* GT_OFF(x), where x is an unsigned value, is true if x > maximum z_off64_t
+   value -- needed when comparing unsigned to z_off64_t, which is signed
+   (possible z_off64_t types off_t, off64_t, and long are all signed) */
+#ifdef INT_MAX
+#  define GT_OFF(x) (sizeof(int) == sizeof(z_off64_t) && (x) > INT_MAX)
+#else
+unsigned ZLIB_INTERNAL gz_intmax OF((void));
+#  define GT_OFF(x) (sizeof(int) == sizeof(z_off64_t) && (x) > gz_intmax())
+#endif
diff --git a/3rdparty/zlib/gzio.c b/3rdparty/zlib/gzio.c
deleted file mode 100644 (file)
index 868583e..0000000
+++ /dev/null
@@ -1,1026 +0,0 @@
-/* gzio.c -- IO on .gz files
- * Copyright (C) 1995-2005 Jean-loup Gailly.
- * For conditions of distribution and use, see copyright notice in zlib.h
- *
- * Compile this file with -DNO_GZCOMPRESS to avoid the compression code.
- */
-
-/* @(#) $Id: gzio.c,v 1.2 2008-05-26 19:08:10 vp153 Exp $ */
-
-#include <stdio.h>
-
-#include "zutil.h"
-
-#ifdef NO_DEFLATE       /* for compatibility with old definition */
-#  define NO_GZCOMPRESS
-#endif
-
-#ifndef NO_DUMMY_DECL
-struct internal_state {int dummy;}; /* for buggy compilers */
-#endif
-
-#ifndef Z_BUFSIZE
-#  ifdef MAXSEG_64K
-#    define Z_BUFSIZE 4096 /* minimize memory usage for 16-bit DOS */
-#  else
-#    define Z_BUFSIZE 16384
-#  endif
-#endif
-#ifndef Z_PRINTF_BUFSIZE
-#  define Z_PRINTF_BUFSIZE 4096
-#endif
-
-#ifdef __MVS__
-#  pragma map (fdopen , "\174\174FDOPEN")
-   FILE *fdopen(int, const char *);
-#endif
-
-#ifndef STDC
-extern voidp  malloc OF((uInt size));
-extern void   free   OF((voidpf ptr));
-#endif
-
-#define ALLOC(size) malloc(size)
-#define TRYFREE(p) {if (p) free(p);}
-
-static int const gz_magic[2] = {0x1f, 0x8b}; /* gzip magic header */
-
-/* gzip flag byte */
-#define ASCII_FLAG   0x01 /* bit 0 set: file probably ascii text */
-#define HEAD_CRC     0x02 /* bit 1 set: header CRC present */
-#define EXTRA_FIELD  0x04 /* bit 2 set: extra field present */
-#define ORIG_NAME    0x08 /* bit 3 set: original file name present */
-#define COMMENT      0x10 /* bit 4 set: file comment present */
-#define RESERVED     0xE0 /* bits 5..7: reserved */
-
-typedef struct gz_stream {
-    z_stream stream;
-    int      z_err;   /* error code for last stream operation */
-    int      z_eof;   /* set if end of input file */
-    FILE     *file;   /* .gz file */
-    Byte     *inbuf;  /* input buffer */
-    Byte     *outbuf; /* output buffer */
-    uLong    crc;     /* crc32 of uncompressed data */
-    char     *msg;    /* error message */
-    char     *path;   /* path name for debugging only */
-    int      transparent; /* 1 if input file is not a .gz file */
-    char     mode;    /* 'w' or 'r' */
-    z_off_t  start;   /* start of compressed data in file (header skipped) */
-    z_off_t  in;      /* bytes into deflate or inflate */
-    z_off_t  out;     /* bytes out of deflate or inflate */
-    int      back;    /* one character push-back */
-    int      last;    /* true if push-back is last character */
-} gz_stream;
-
-
-local gzFile gz_open      OF((const char *path, const char *mode, int  fd));
-local int do_flush        OF((gzFile file, int flush));
-local int    get_byte     OF((gz_stream *s));
-local void   check_header OF((gz_stream *s));
-local int    destroy      OF((gz_stream *s));
-local void   putLong      OF((FILE *file, uLong x));
-local uLong  getLong      OF((gz_stream *s));
-
-/* ===========================================================================
-     Opens a gzip (.gz) file for reading or writing. The mode parameter
-   is as in fopen ("rb" or "wb"). The file is given either by file descriptor
-   or path name (if fd == -1).
-     gz_open returns NULL if the file could not be opened or if there was
-   insufficient memory to allocate the (de)compression state; errno
-   can be checked to distinguish the two cases (if errno is zero, the
-   zlib error is Z_MEM_ERROR).
-*/
-local gzFile gz_open (path, mode, fd)
-    const char *path;
-    const char *mode;
-    int  fd;
-{
-    int err;
-    int level = Z_DEFAULT_COMPRESSION; /* compression level */
-    int strategy = Z_DEFAULT_STRATEGY; /* compression strategy */
-    char *p = (char*)mode;
-    gz_stream *s;
-    char fmode[80]; /* copy of mode, without the compression level */
-    char *m = fmode;
-
-    if (!path || !mode) return Z_NULL;
-
-    s = (gz_stream *)ALLOC(sizeof(gz_stream));
-    if (!s) return Z_NULL;
-
-    s->stream.zalloc = (alloc_func)0;
-    s->stream.zfree = (free_func)0;
-    s->stream.opaque = (voidpf)0;
-    s->stream.next_in = s->inbuf = Z_NULL;
-    s->stream.next_out = s->outbuf = Z_NULL;
-    s->stream.avail_in = s->stream.avail_out = 0;
-    s->file = NULL;
-    s->z_err = Z_OK;
-    s->z_eof = 0;
-    s->in = 0;
-    s->out = 0;
-    s->back = EOF;
-    s->crc = crc32(0L, Z_NULL, 0);
-    s->msg = NULL;
-    s->transparent = 0;
-
-    s->path = (char*)ALLOC(strlen(path)+1);
-    if (s->path == NULL) {
-        return destroy(s), (gzFile)Z_NULL;
-    }
-    strcpy(s->path, path); /* do this early for debugging */
-
-    s->mode = '\0';
-    do {
-        if (*p == 'r') s->mode = 'r';
-        if (*p == 'w' || *p == 'a') s->mode = 'w';
-        if (*p >= '0' && *p <= '9') {
-            level = *p - '0';
-        } else if (*p == 'f') {
-          strategy = Z_FILTERED;
-        } else if (*p == 'h') {
-          strategy = Z_HUFFMAN_ONLY;
-        } else if (*p == 'R') {
-          strategy = Z_RLE;
-        } else {
-            *m++ = *p; /* copy the mode */
-        }
-    } while (*p++ && m != fmode + sizeof(fmode));
-    if (s->mode == '\0') return destroy(s), (gzFile)Z_NULL;
-
-    if (s->mode == 'w') {
-#ifdef NO_GZCOMPRESS
-        err = Z_STREAM_ERROR;
-#else
-        err = deflateInit2(&(s->stream), level,
-                           Z_DEFLATED, -MAX_WBITS, DEF_MEM_LEVEL, strategy);
-        /* windowBits is passed < 0 to suppress zlib header */
-
-        s->stream.next_out = s->outbuf = (Byte*)ALLOC(Z_BUFSIZE);
-#endif
-        if (err != Z_OK || s->outbuf == Z_NULL) {
-            return destroy(s), (gzFile)Z_NULL;
-        }
-    } else {
-        s->stream.next_in  = s->inbuf = (Byte*)ALLOC(Z_BUFSIZE);
-
-        err = inflateInit2(&(s->stream), -MAX_WBITS);
-        /* windowBits is passed < 0 to tell that there is no zlib header.
-         * Note that in this case inflate *requires* an extra "dummy" byte
-         * after the compressed stream in order to complete decompression and
-         * return Z_STREAM_END. Here the gzip CRC32 ensures that 4 bytes are
-         * present after the compressed stream.
-         */
-        if (err != Z_OK || s->inbuf == Z_NULL) {
-            return destroy(s), (gzFile)Z_NULL;
-        }
-    }
-    s->stream.avail_out = Z_BUFSIZE;
-
-    errno = 0;
-    s->file = fd < 0 ? F_OPEN(path, fmode) : (FILE*)fdopen(fd, fmode);
-
-    if (s->file == NULL) {
-        return destroy(s), (gzFile)Z_NULL;
-    }
-    if (s->mode == 'w') {
-        /* Write a very simple .gz header:
-         */
-        fprintf(s->file, "%c%c%c%c%c%c%c%c%c%c", gz_magic[0], gz_magic[1],
-             Z_DEFLATED, 0 /*flags*/, 0,0,0,0 /*time*/, 0 /*xflags*/, OS_CODE);
-        s->start = 10L;
-        /* We use 10L instead of ftell(s->file) to because ftell causes an
-         * fflush on some systems. This version of the library doesn't use
-         * start anyway in write mode, so this initialization is not
-         * necessary.
-         */
-    } else {
-        check_header(s); /* skip the .gz header */
-        s->start = ftell(s->file) - s->stream.avail_in;
-    }
-
-    return (gzFile)s;
-}
-
-/* ===========================================================================
-     Opens a gzip (.gz) file for reading or writing.
-*/
-gzFile ZEXPORT gzopen (path, mode)
-    const char *path;
-    const char *mode;
-{
-    return gz_open (path, mode, -1);
-}
-
-/* ===========================================================================
-     Associate a gzFile with the file descriptor fd. fd is not dup'ed here
-   to mimic the behavio(u)r of fdopen.
-*/
-gzFile ZEXPORT gzdopen (fd, mode)
-    int fd;
-    const char *mode;
-{
-    char name[46];      /* allow for up to 128-bit integers */
-
-    if (fd < 0) return (gzFile)Z_NULL;
-    sprintf(name, "<fd:%d>", fd); /* for debugging */
-
-    return gz_open (name, mode, fd);
-}
-
-/* ===========================================================================
- * Update the compression level and strategy
- */
-int ZEXPORT gzsetparams (file, level, strategy)
-    gzFile file;
-    int level;
-    int strategy;
-{
-    gz_stream *s = (gz_stream*)file;
-
-    if (s == NULL || s->mode != 'w') return Z_STREAM_ERROR;
-
-    /* Make room to allow flushing */
-    if (s->stream.avail_out == 0) {
-
-        s->stream.next_out = s->outbuf;
-        if (fwrite(s->outbuf, 1, Z_BUFSIZE, s->file) != Z_BUFSIZE) {
-            s->z_err = Z_ERRNO;
-        }
-        s->stream.avail_out = Z_BUFSIZE;
-    }
-
-    return deflateParams (&(s->stream), level, strategy);
-}
-
-/* ===========================================================================
-     Read a byte from a gz_stream; update next_in and avail_in. Return EOF
-   for end of file.
-   IN assertion: the stream s has been sucessfully opened for reading.
-*/
-local int get_byte(s)
-    gz_stream *s;
-{
-    if (s->z_eof) return EOF;
-    if (s->stream.avail_in == 0) {
-        errno = 0;
-        s->stream.avail_in = (uInt)fread(s->inbuf, 1, Z_BUFSIZE, s->file);
-        if (s->stream.avail_in == 0) {
-            s->z_eof = 1;
-            if (ferror(s->file)) s->z_err = Z_ERRNO;
-            return EOF;
-        }
-        s->stream.next_in = s->inbuf;
-    }
-    s->stream.avail_in--;
-    return *(s->stream.next_in)++;
-}
-
-/* ===========================================================================
-      Check the gzip header of a gz_stream opened for reading. Set the stream
-    mode to transparent if the gzip magic header is not present; set s->err
-    to Z_DATA_ERROR if the magic header is present but the rest of the header
-    is incorrect.
-    IN assertion: the stream s has already been created sucessfully;
-       s->stream.avail_in is zero for the first time, but may be non-zero
-       for concatenated .gz files.
-*/
-local void check_header(s)
-    gz_stream *s;
-{
-    int method; /* method byte */
-    int flags;  /* flags byte */
-    uInt len;
-    int c;
-
-    /* Assure two bytes in the buffer so we can peek ahead -- handle case
-       where first byte of header is at the end of the buffer after the last
-       gzip segment */
-    len = s->stream.avail_in;
-    if (len < 2) {
-        if (len) s->inbuf[0] = s->stream.next_in[0];
-        errno = 0;
-        len = (uInt)fread(s->inbuf + len, 1, Z_BUFSIZE >> len, s->file);
-        if (len == 0 && ferror(s->file)) s->z_err = Z_ERRNO;
-        s->stream.avail_in += len;
-        s->stream.next_in = s->inbuf;
-        if (s->stream.avail_in < 2) {
-            s->transparent = s->stream.avail_in;
-            return;
-        }
-    }
-
-    /* Peek ahead to check the gzip magic header */
-    if (s->stream.next_in[0] != gz_magic[0] ||
-        s->stream.next_in[1] != gz_magic[1]) {
-        s->transparent = 1;
-        return;
-    }
-    s->stream.avail_in -= 2;
-    s->stream.next_in += 2;
-
-    /* Check the rest of the gzip header */
-    method = get_byte(s);
-    flags = get_byte(s);
-    if (method != Z_DEFLATED || (flags & RESERVED) != 0) {
-        s->z_err = Z_DATA_ERROR;
-        return;
-    }
-
-    /* Discard time, xflags and OS code: */
-    for (len = 0; len < 6; len++) (void)get_byte(s);
-
-    if ((flags & EXTRA_FIELD) != 0) { /* skip the extra field */
-        len  =  (uInt)get_byte(s);
-        len += ((uInt)get_byte(s))<<8;
-        /* len is garbage if EOF but the loop below will quit anyway */
-        while (len-- != 0 && get_byte(s) != EOF) ;
-    }
-    if ((flags & ORIG_NAME) != 0) { /* skip the original file name */
-        while ((c = get_byte(s)) != 0 && c != EOF) ;
-    }
-    if ((flags & COMMENT) != 0) {   /* skip the .gz file comment */
-        while ((c = get_byte(s)) != 0 && c != EOF) ;
-    }
-    if ((flags & HEAD_CRC) != 0) {  /* skip the header crc */
-        for (len = 0; len < 2; len++) (void)get_byte(s);
-    }
-    s->z_err = s->z_eof ? Z_DATA_ERROR : Z_OK;
-}
-
- /* ===========================================================================
- * Cleanup then free the given gz_stream. Return a zlib error code.
-   Try freeing in the reverse order of allocations.
- */
-local int destroy (s)
-    gz_stream *s;
-{
-    int err = Z_OK;
-
-    if (!s) return Z_STREAM_ERROR;
-
-    TRYFREE(s->msg);
-
-    if (s->stream.state != NULL) {
-        if (s->mode == 'w') {
-#ifdef NO_GZCOMPRESS
-            err = Z_STREAM_ERROR;
-#else
-            err = deflateEnd(&(s->stream));
-#endif
-        } else if (s->mode == 'r') {
-            err = inflateEnd(&(s->stream));
-        }
-    }
-    if (s->file != NULL && fclose(s->file)) {
-#ifdef ESPIPE
-        if (errno != ESPIPE) /* fclose is broken for pipes in HP/UX */
-#endif
-            err = Z_ERRNO;
-    }
-    if (s->z_err < 0) err = s->z_err;
-
-    TRYFREE(s->inbuf);
-    TRYFREE(s->outbuf);
-    TRYFREE(s->path);
-    TRYFREE(s);
-    return err;
-}
-
-/* ===========================================================================
-     Reads the given number of uncompressed bytes from the compressed file.
-   gzread returns the number of bytes actually read (0 for end of file).
-*/
-int ZEXPORT gzread (file, buf, len)
-    gzFile file;
-    voidp buf;
-    unsigned len;
-{
-    gz_stream *s = (gz_stream*)file;
-    Bytef *start = (Bytef*)buf; /* starting point for crc computation */
-    Byte  *next_out; /* == stream.next_out but not forced far (for MSDOS) */
-
-    if (s == NULL || s->mode != 'r') return Z_STREAM_ERROR;
-
-    if (s->z_err == Z_DATA_ERROR || s->z_err == Z_ERRNO) return -1;
-    if (s->z_err == Z_STREAM_END) return 0;  /* EOF */
-
-    next_out = (Byte*)buf;
-    s->stream.next_out = (Bytef*)buf;
-    s->stream.avail_out = len;
-
-    if (s->stream.avail_out && s->back != EOF) {
-        *next_out++ = s->back;
-        s->stream.next_out++;
-        s->stream.avail_out--;
-        s->back = EOF;
-        s->out++;
-        start++;
-        if (s->last) {
-            s->z_err = Z_STREAM_END;
-            return 1;
-        }
-    }
-
-    while (s->stream.avail_out != 0) {
-
-        if (s->transparent) {
-            /* Copy first the lookahead bytes: */
-            uInt n = s->stream.avail_in;
-            if (n > s->stream.avail_out) n = s->stream.avail_out;
-            if (n > 0) {
-                zmemcpy(s->stream.next_out, s->stream.next_in, n);
-                next_out += n;
-                s->stream.next_out = next_out;
-                s->stream.next_in   += n;
-                s->stream.avail_out -= n;
-                s->stream.avail_in  -= n;
-            }
-            if (s->stream.avail_out > 0) {
-                s->stream.avail_out -=
-                    (uInt)fread(next_out, 1, s->stream.avail_out, s->file);
-            }
-            len -= s->stream.avail_out;
-            s->in  += len;
-            s->out += len;
-            if (len == 0) s->z_eof = 1;
-            return (int)len;
-        }
-        if (s->stream.avail_in == 0 && !s->z_eof) {
-
-            errno = 0;
-            s->stream.avail_in = (uInt)fread(s->inbuf, 1, Z_BUFSIZE, s->file);
-            if (s->stream.avail_in == 0) {
-                s->z_eof = 1;
-                if (ferror(s->file)) {
-                    s->z_err = Z_ERRNO;
-                    break;
-                }
-            }
-            s->stream.next_in = s->inbuf;
-        }
-        s->in += s->stream.avail_in;
-        s->out += s->stream.avail_out;
-        s->z_err = inflate(&(s->stream), Z_NO_FLUSH);
-        s->in -= s->stream.avail_in;
-        s->out -= s->stream.avail_out;
-
-        if (s->z_err == Z_STREAM_END) {
-            /* Check CRC and original size */
-            s->crc = crc32(s->crc, start, (uInt)(s->stream.next_out - start));
-            start = s->stream.next_out;
-
-            if (getLong(s) != s->crc) {
-                s->z_err = Z_DATA_ERROR;
-            } else {
-                (void)getLong(s);
-                /* The uncompressed length returned by above getlong() may be
-                 * different from s->out in case of concatenated .gz files.
-                 * Check for such files:
-                 */
-                check_header(s);
-                if (s->z_err == Z_OK) {
-                    inflateReset(&(s->stream));
-                    s->crc = crc32(0L, Z_NULL, 0);
-                }
-            }
-        }
-        if (s->z_err != Z_OK || s->z_eof) break;
-    }
-    s->crc = crc32(s->crc, start, (uInt)(s->stream.next_out - start));
-
-    if (len == s->stream.avail_out &&
-        (s->z_err == Z_DATA_ERROR || s->z_err == Z_ERRNO))
-        return -1;
-    return (int)(len - s->stream.avail_out);
-}
-
-
-/* ===========================================================================
-      Reads one byte from the compressed file. gzgetc returns this byte
-   or -1 in case of end of file or error.
-*/
-int ZEXPORT gzgetc(file)
-    gzFile file;
-{
-    unsigned char c;
-
-    return gzread(file, &c, 1) == 1 ? c : -1;
-}
-
-
-/* ===========================================================================
-      Push one byte back onto the stream.
-*/
-int ZEXPORT gzungetc(c, file)
-    int c;
-    gzFile file;
-{
-    gz_stream *s = (gz_stream*)file;
-
-    if (s == NULL || s->mode != 'r' || c == EOF || s->back != EOF) return EOF;
-    s->back = c;
-    s->out--;
-    s->last = (s->z_err == Z_STREAM_END);
-    if (s->last) s->z_err = Z_OK;
-    s->z_eof = 0;
-    return c;
-}
-
-
-/* ===========================================================================
-      Reads bytes from the compressed file until len-1 characters are
-   read, or a newline character is read and transferred to buf, or an
-   end-of-file condition is encountered.  The string is then terminated
-   with a null character.
-      gzgets returns buf, or Z_NULL in case of error.
-
-      The current implementation is not optimized at all.
-*/
-char * ZEXPORT gzgets(file, buf, len)
-    gzFile file;
-    char *buf;
-    int len;
-{
-    char *b = buf;
-    if (buf == Z_NULL || len <= 0) return Z_NULL;
-
-    while (--len > 0 && gzread(file, buf, 1) == 1 && *buf++ != '\n') ;
-    *buf = '\0';
-    return b == buf && len > 0 ? Z_NULL : b;
-}
-
-
-#ifndef NO_GZCOMPRESS
-/* ===========================================================================
-     Writes the given number of uncompressed bytes into the compressed file.
-   gzwrite returns the number of bytes actually written (0 in case of error).
-*/
-int ZEXPORT gzwrite (file, buf, len)
-    gzFile file;
-    voidpc buf;
-    unsigned len;
-{
-    gz_stream *s = (gz_stream*)file;
-
-    if (s == NULL || s->mode != 'w') return Z_STREAM_ERROR;
-
-    s->stream.next_in = (Bytef*)buf;
-    s->stream.avail_in = len;
-
-    while (s->stream.avail_in != 0) {
-
-        if (s->stream.avail_out == 0) {
-
-            s->stream.next_out = s->outbuf;
-            if (fwrite(s->outbuf, 1, Z_BUFSIZE, s->file) != Z_BUFSIZE) {
-                s->z_err = Z_ERRNO;
-                break;
-            }
-            s->stream.avail_out = Z_BUFSIZE;
-        }
-        s->in += s->stream.avail_in;
-        s->out += s->stream.avail_out;
-        s->z_err = deflate(&(s->stream), Z_NO_FLUSH);
-        s->in -= s->stream.avail_in;
-        s->out -= s->stream.avail_out;
-        if (s->z_err != Z_OK) break;
-    }
-    s->crc = crc32(s->crc, (const Bytef *)buf, len);
-
-    return (int)(len - s->stream.avail_in);
-}
-
-
-/* ===========================================================================
-     Converts, formats, and writes the args to the compressed file under
-   control of the format string, as in fprintf. gzprintf returns the number of
-   uncompressed bytes actually written (0 in case of error).
-*/
-#ifdef STDC
-#include <stdarg.h>
-
-int ZEXPORTVA gzprintf (gzFile file, const char *format, /* args */ ...)
-{
-    char buf[Z_PRINTF_BUFSIZE];
-    va_list va;
-    int len;
-
-    buf[sizeof(buf) - 1] = 0;
-    va_start(va, format);
-#ifdef NO_vsnprintf
-#  ifdef HAS_vsprintf_void
-    (void)vsprintf(buf, format, va);
-    va_end(va);
-    for (len = 0; len < sizeof(buf); len++)
-        if (buf[len] == 0) break;
-#  else
-    len = vsprintf(buf, format, va);
-    va_end(va);
-#  endif
-#else
-#  ifdef HAS_vsnprintf_void
-    (void)vsnprintf(buf, sizeof(buf), format, va);
-    va_end(va);
-    len = strlen(buf);
-#  else
-    len = vsnprintf(buf, sizeof(buf), format, va);
-    va_end(va);
-#  endif
-#endif
-    if (len <= 0 || len >= (int)sizeof(buf) || buf[sizeof(buf) - 1] != 0)
-        return 0;
-    return gzwrite(file, buf, (unsigned)len);
-}
-#else /* not ANSI C */
-
-int ZEXPORTVA gzprintf (file, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10,
-                       a11, a12, a13, a14, a15, a16, a17, a18, a19, a20)
-    gzFile file;
-    const char *format;
-    int a1, a2, a3, a4, a5, a6, a7, a8, a9, a10,
-        a11, a12, a13, a14, a15, a16, a17, a18, a19, a20;
-{
-    char buf[Z_PRINTF_BUFSIZE];
-    int len;
-
-    buf[sizeof(buf) - 1] = 0;
-#ifdef NO_snprintf
-#  ifdef HAS_sprintf_void
-    sprintf(buf, format, a1, a2, a3, a4, a5, a6, a7, a8,
-            a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
-    for (len = 0; len < sizeof(buf); len++)
-        if (buf[len] == 0) break;
-#  else
-    len = sprintf(buf, format, a1, a2, a3, a4, a5, a6, a7, a8,
-                a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
-#  endif
-#else
-#  ifdef HAS_snprintf_void
-    snprintf(buf, sizeof(buf), format, a1, a2, a3, a4, a5, a6, a7, a8,
-             a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
-    len = strlen(buf);
-#  else
-    len = snprintf(buf, sizeof(buf), format, a1, a2, a3, a4, a5, a6, a7, a8,
-                 a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
-#  endif
-#endif
-    if (len <= 0 || len >= sizeof(buf) || buf[sizeof(buf) - 1] != 0)
-        return 0;
-    return gzwrite(file, buf, len);
-}
-#endif
-
-/* ===========================================================================
-      Writes c, converted to an unsigned char, into the compressed file.
-   gzputc returns the value that was written, or -1 in case of error.
-*/
-int ZEXPORT gzputc(file, c)
-    gzFile file;
-    int c;
-{
-    unsigned char cc = (unsigned char) c; /* required for big endian systems */
-
-    return gzwrite(file, &cc, 1) == 1 ? (int)cc : -1;
-}
-
-
-/* ===========================================================================
-      Writes the given null-terminated string to the compressed file, excluding
-   the terminating null character.
-      gzputs returns the number of characters written, or -1 in case of error.
-*/
-int ZEXPORT gzputs(file, s)
-    gzFile file;
-    const char *s;
-{
-    return gzwrite(file, (char*)s, (unsigned)strlen(s));
-}
-
-
-/* ===========================================================================
-     Flushes all pending output into the compressed file. The parameter
-   flush is as in the deflate() function.
-*/
-local int do_flush (file, flush)
-    gzFile file;
-    int flush;
-{
-    uInt len;
-    int done = 0;
-    gz_stream *s = (gz_stream*)file;
-
-    if (s == NULL || s->mode != 'w') return Z_STREAM_ERROR;
-
-    s->stream.avail_in = 0; /* should be zero already anyway */
-
-    for (;;) {
-        len = Z_BUFSIZE - s->stream.avail_out;
-
-        if (len != 0) {
-            if ((uInt)fwrite(s->outbuf, 1, len, s->file) != len) {
-                s->z_err = Z_ERRNO;
-                return Z_ERRNO;
-            }
-            s->stream.next_out = s->outbuf;
-            s->stream.avail_out = Z_BUFSIZE;
-        }
-        if (done) break;
-        s->out += s->stream.avail_out;
-        s->z_err = deflate(&(s->stream), flush);
-        s->out -= s->stream.avail_out;
-
-        /* Ignore the second of two consecutive flushes: */
-        if (len == 0 && s->z_err == Z_BUF_ERROR) s->z_err = Z_OK;
-
-        /* deflate has finished flushing only when it hasn't used up
-         * all the available space in the output buffer:
-         */
-        done = (s->stream.avail_out != 0 || s->z_err == Z_STREAM_END);
-
-        if (s->z_err != Z_OK && s->z_err != Z_STREAM_END) break;
-    }
-    return  s->z_err == Z_STREAM_END ? Z_OK : s->z_err;
-}
-
-int ZEXPORT gzflush (file, flush)
-     gzFile file;
-     int flush;
-{
-    gz_stream *s = (gz_stream*)file;
-    int err = do_flush (file, flush);
-
-    if (err) return err;
-    fflush(s->file);
-    return  s->z_err == Z_STREAM_END ? Z_OK : s->z_err;
-}
-#endif /* NO_GZCOMPRESS */
-
-/* ===========================================================================
-      Sets the starting position for the next gzread or gzwrite on the given
-   compressed file. The offset represents a number of bytes in the
-      gzseek returns the resulting offset location as measured in bytes from
-   the beginning of the uncompressed stream, or -1 in case of error.
-      SEEK_END is not implemented, returns error.
-      In this version of the library, gzseek can be extremely slow.
-*/
-z_off_t ZEXPORT gzseek (file, offset, whence)
-    gzFile file;
-    z_off_t offset;
-    int whence;
-{
-    gz_stream *s = (gz_stream*)file;
-
-    if (s == NULL || whence == SEEK_END ||
-        s->z_err == Z_ERRNO || s->z_err == Z_DATA_ERROR) {
-        return -1L;
-    }
-
-    if (s->mode == 'w') {
-#ifdef NO_GZCOMPRESS
-        return -1L;
-#else
-        if (whence == SEEK_SET) {
-            offset -= s->in;
-        }
-        if (offset < 0) return -1L;
-
-        /* At this point, offset is the number of zero bytes to write. */
-        if (s->inbuf == Z_NULL) {
-            s->inbuf = (Byte*)ALLOC(Z_BUFSIZE); /* for seeking */
-            if (s->inbuf == Z_NULL) return -1L;
-            zmemzero(s->inbuf, Z_BUFSIZE);
-        }
-        while (offset > 0)  {
-            uInt size = Z_BUFSIZE;
-            if (offset < Z_BUFSIZE) size = (uInt)offset;
-
-            size = gzwrite(file, s->inbuf, size);
-            if (size == 0) return -1L;
-
-            offset -= size;
-        }
-        return s->in;
-#endif
-    }
-    /* Rest of function is for reading only */
-
-    /* compute absolute position */
-    if (whence == SEEK_CUR) {
-        offset += s->out;
-    }
-    if (offset < 0) return -1L;
-
-    if (s->transparent) {
-        /* map to fseek */
-        s->back = EOF;
-        s->stream.avail_in = 0;
-        s->stream.next_in = s->inbuf;
-        if (fseek(s->file, offset, SEEK_SET) < 0) return -1L;
-
-        s->in = s->out = offset;
-        return offset;
-    }
-
-    /* For a negative seek, rewind and use positive seek */
-    if (offset >= s->out) {
-        offset -= s->out;
-    } else if (gzrewind(file) < 0) {
-        return -1L;
-    }
-    /* offset is now the number of bytes to skip. */
-
-    if (offset != 0 && s->outbuf == Z_NULL) {
-        s->outbuf = (Byte*)ALLOC(Z_BUFSIZE);
-        if (s->outbuf == Z_NULL) return -1L;
-    }
-    if (offset && s->back != EOF) {
-        s->back = EOF;
-        s->out++;
-        offset--;
-        if (s->last) s->z_err = Z_STREAM_END;
-    }
-    while (offset > 0)  {
-        int size = Z_BUFSIZE;
-        if (offset < Z_BUFSIZE) size = (int)offset;
-
-        size = gzread(file, s->outbuf, (uInt)size);
-        if (size <= 0) return -1L;
-        offset -= size;
-    }
-    return s->out;
-}
-
-/* ===========================================================================
-     Rewinds input file.
-*/
-int ZEXPORT gzrewind (file)
-    gzFile file;
-{
-    gz_stream *s = (gz_stream*)file;
-
-    if (s == NULL || s->mode != 'r') return -1;
-
-    s->z_err = Z_OK;
-    s->z_eof = 0;
-    s->back = EOF;
-    s->stream.avail_in = 0;
-    s->stream.next_in = s->inbuf;
-    s->crc = crc32(0L, Z_NULL, 0);
-    if (!s->transparent) (void)inflateReset(&s->stream);
-    s->in = 0;
-    s->out = 0;
-    return fseek(s->file, s->start, SEEK_SET);
-}
-
-/* ===========================================================================
-     Returns the starting position for the next gzread or gzwrite on the
-   given compressed file. This position represents a number of bytes in the
-   uncompressed data stream.
-*/
-z_off_t ZEXPORT gztell (file)
-    gzFile file;
-{
-    return gzseek(file, 0L, SEEK_CUR);
-}
-
-/* ===========================================================================
-     Returns 1 when EOF has previously been detected reading the given
-   input stream, otherwise zero.
-*/
-int ZEXPORT gzeof (file)
-    gzFile file;
-{
-    gz_stream *s = (gz_stream*)file;
-
-    /* With concatenated compressed files that can have embedded
-     * crc trailers, z_eof is no longer the only/best indicator of EOF
-     * on a gz_stream. Handle end-of-stream error explicitly here.
-     */
-    if (s == NULL || s->mode != 'r') return 0;
-    if (s->z_eof) return 1;
-    return s->z_err == Z_STREAM_END;
-}
-
-/* ===========================================================================
-     Returns 1 if reading and doing so transparently, otherwise zero.
-*/
-int ZEXPORT gzdirect (file)
-    gzFile file;
-{
-    gz_stream *s = (gz_stream*)file;
-
-    if (s == NULL || s->mode != 'r') return 0;
-    return s->transparent;
-}
-
-/* ===========================================================================
-   Outputs a long in LSB order to the given file
-*/
-local void putLong (file, x)
-    FILE *file;
-    uLong x;
-{
-    int n;
-    for (n = 0; n < 4; n++) {
-        fputc((int)(x & 0xff), file);
-        x >>= 8;
-    }
-}
-
-/* ===========================================================================
-   Reads a long in LSB order from the given gz_stream. Sets z_err in case
-   of error.
-*/
-local uLong getLong (s)
-    gz_stream *s;
-{
-    uLong x = (uLong)get_byte(s);
-    int c;
-
-    x += ((uLong)get_byte(s))<<8;
-    x += ((uLong)get_byte(s))<<16;
-    c = get_byte(s);
-    if (c == EOF) s->z_err = Z_DATA_ERROR;
-    x += ((uLong)c)<<24;
-    return x;
-}
-
-/* ===========================================================================
-     Flushes all pending output if necessary, closes the compressed file
-   and deallocates all the (de)compression state.
-*/
-int ZEXPORT gzclose (file)
-    gzFile file;
-{
-    gz_stream *s = (gz_stream*)file;
-
-    if (s == NULL) return Z_STREAM_ERROR;
-
-    if (s->mode == 'w') {
-#ifdef NO_GZCOMPRESS
-        return Z_STREAM_ERROR;
-#else
-        if (do_flush (file, Z_FINISH) != Z_OK)
-            return destroy((gz_stream*)file);
-
-        putLong (s->file, s->crc);
-        putLong (s->file, (uLong)(s->in & 0xffffffff));
-#endif
-    }
-    return destroy((gz_stream*)file);
-}
-
-#ifdef STDC
-#  define zstrerror(errnum) strerror(errnum)
-#else
-#  define zstrerror(errnum) ""
-#endif
-
-/* ===========================================================================
-     Returns the error message for the last error which occurred on the
-   given compressed file. errnum is set to zlib error number. If an
-   error occurred in the file system and not in the compression library,
-   errnum is set to Z_ERRNO and the application may consult errno
-   to get the exact error code.
-*/
-const char * ZEXPORT gzerror (file, errnum)
-    gzFile file;
-    int *errnum;
-{
-    char *m;
-    gz_stream *s = (gz_stream*)file;
-
-    if (s == NULL) {
-        *errnum = Z_STREAM_ERROR;
-        return (const char*)ERR_MSG(Z_STREAM_ERROR);
-    }
-    *errnum = s->z_err;
-    if (*errnum == Z_OK) return (const char*)"";
-
-    m = (char*)(*errnum == Z_ERRNO ? zstrerror(errno) : s->stream.msg);
-
-    if (m == NULL || *m == '\0') m = (char*)ERR_MSG(s->z_err);
-
-    TRYFREE(s->msg);
-    s->msg = (char*)ALLOC(strlen(s->path) + strlen(m) + 3);
-    if (s->msg == Z_NULL) return (const char*)ERR_MSG(Z_MEM_ERROR);
-    strcpy(s->msg, s->path);
-    strcat(s->msg, ": ");
-    strcat(s->msg, m);
-    return (const char*)s->msg;
-}
-
-/* ===========================================================================
-     Clear the error and end-of-file flags, and do the same for the real file.
-*/
-void ZEXPORT gzclearerr (file)
-    gzFile file;
-{
-    gz_stream *s = (gz_stream*)file;
-
-    if (s == NULL) return;
-    if (s->z_err != Z_STREAM_END) s->z_err = Z_OK;
-    s->z_eof = 0;
-    clearerr(s->file);
-}
diff --git a/3rdparty/zlib/gzlib.c b/3rdparty/zlib/gzlib.c
new file mode 100644 (file)
index 0000000..603e60e
--- /dev/null
@@ -0,0 +1,537 @@
+/* gzlib.c -- zlib functions common to reading and writing gzip files
+ * Copyright (C) 2004, 2010 Mark Adler
+ * For conditions of distribution and use, see copyright notice in zlib.h
+ */
+
+#include "gzguts.h"
+
+#if defined(_LARGEFILE64_SOURCE) && _LFS64_LARGEFILE-0
+#  define LSEEK lseek64
+#else
+#  define LSEEK lseek
+#endif
+
+/* Local functions */
+local void gz_reset OF((gz_statep));
+local gzFile gz_open OF((const char *, int, const char *));
+
+#if defined UNDER_CE
+
+/* Map the Windows error number in ERROR to a locale-dependent error message
+   string and return a pointer to it.  Typically, the values for ERROR come
+   from GetLastError.
+
+   The string pointed to shall not be modified by the application, but may be
+   overwritten by a subsequent call to gz_strwinerror
+
+   The gz_strwinerror function does not change the current setting of
+   GetLastError. */
+char ZLIB_INTERNAL *gz_strwinerror (error)
+     DWORD error;
+{
+    static char buf[1024];
+
+    wchar_t *msgbuf;
+    DWORD lasterr = GetLastError();
+    DWORD chars = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM
+        | FORMAT_MESSAGE_ALLOCATE_BUFFER,
+        NULL,
+        error,
+        0, /* Default language */
+        (LPVOID)&msgbuf,
+        0,
+        NULL);
+    if (chars != 0) {
+        /* If there is an \r\n appended, zap it.  */
+        if (chars >= 2
+            && msgbuf[chars - 2] == '\r' && msgbuf[chars - 1] == '\n') {
+            chars -= 2;
+            msgbuf[chars] = 0;
+        }
+
+        if (chars > sizeof (buf) - 1) {
+            chars = sizeof (buf) - 1;
+            msgbuf[chars] = 0;
+        }
+
+        wcstombs(buf, msgbuf, chars + 1);
+        LocalFree(msgbuf);
+    }
+    else {
+        sprintf(buf, "unknown win32 error (%ld)", error);
+    }
+
+    SetLastError(lasterr);
+    return buf;
+}
+
+#endif /* UNDER_CE */
+
+/* Reset gzip file state */
+local void gz_reset(state)
+    gz_statep state;
+{
+    if (state->mode == GZ_READ) {   /* for reading ... */
+        state->have = 0;            /* no output data available */
+        state->eof = 0;             /* not at end of file */
+        state->how = LOOK;          /* look for gzip header */
+        state->direct = 1;          /* default for empty file */
+    }
+    state->seek = 0;                /* no seek request pending */
+    gz_error(state, Z_OK, NULL);    /* clear error */
+    state->pos = 0;                 /* no uncompressed data yet */
+    state->strm.avail_in = 0;       /* no input data yet */
+}
+
+/* Open a gzip file either by name or file descriptor. */
+local gzFile gz_open(path, fd, mode)
+    const char *path;
+    int fd;
+    const char *mode;
+{
+    gz_statep state;
+
+    /* allocate gzFile structure to return */
+    state = malloc(sizeof(gz_state));
+    if (state == NULL)
+        return NULL;
+    state->size = 0;            /* no buffers allocated yet */
+    state->want = GZBUFSIZE;    /* requested buffer size */
+    state->msg = NULL;          /* no error message yet */
+
+    /* interpret mode */
+    state->mode = GZ_NONE;
+    state->level = Z_DEFAULT_COMPRESSION;
+    state->strategy = Z_DEFAULT_STRATEGY;
+    while (*mode) {
+        if (*mode >= '0' && *mode <= '9')
+            state->level = *mode - '0';
+        else
+            switch (*mode) {
+            case 'r':
+                state->mode = GZ_READ;
+                break;
+#ifndef NO_GZCOMPRESS
+            case 'w':
+                state->mode = GZ_WRITE;
+                break;
+            case 'a':
+                state->mode = GZ_APPEND;
+                break;
+#endif
+            case '+':       /* can't read and write at the same time */
+                free(state);
+                return NULL;
+            case 'b':       /* ignore -- will request binary anyway */
+                break;
+            case 'f':
+                state->strategy = Z_FILTERED;
+                break;
+            case 'h':
+                state->strategy = Z_HUFFMAN_ONLY;
+                break;
+            case 'R':
+                state->strategy = Z_RLE;
+                break;
+            case 'F':
+                state->strategy = Z_FIXED;
+            default:        /* could consider as an error, but just ignore */
+                ;
+            }
+        mode++;
+    }
+
+    /* must provide an "r", "w", or "a" */
+    if (state->mode == GZ_NONE) {
+        free(state);
+        return NULL;
+    }
+
+    /* save the path name for error messages */
+    state->path = malloc(strlen(path) + 1);
+    if (state->path == NULL) {
+        free(state);
+        return NULL;
+    }
+    strcpy(state->path, path);
+
+    /* open the file with the appropriate mode (or just use fd) */
+    state->fd = fd != -1 ? fd :
+        open(path,
+#ifdef O_LARGEFILE
+            O_LARGEFILE |
+#endif
+#ifdef O_BINARY
+            O_BINARY |
+#endif
+            (state->mode == GZ_READ ?
+                O_RDONLY :
+                (O_WRONLY | O_CREAT | (
+                    state->mode == GZ_WRITE ?
+                        O_TRUNC :
+                        O_APPEND))),
+            0666);
+    if (state->fd == -1) {
+        free(state->path);
+        free(state);
+        return NULL;
+    }
+    if (state->mode == GZ_APPEND)
+        state->mode = GZ_WRITE;         /* simplify later checks */
+
+    /* save the current position for rewinding (only if reading) */
+    if (state->mode == GZ_READ) {
+        state->start = LSEEK(state->fd, 0, SEEK_CUR);
+        if (state->start == -1) state->start = 0;
+    }
+
+    /* initialize stream */
+    gz_reset(state);
+
+    /* return stream */
+    return (gzFile)state;
+}
+
+/* -- see zlib.h -- */
+gzFile ZEXPORT gzopen(path, mode)
+    const char *path;
+    const char *mode;
+{
+    return gz_open(path, -1, mode);
+}
+
+/* -- see zlib.h -- */
+gzFile ZEXPORT gzopen64(path, mode)
+    const char *path;
+    const char *mode;
+{
+    return gz_open(path, -1, mode);
+}
+
+/* -- see zlib.h -- */
+gzFile ZEXPORT gzdopen(fd, mode)
+    int fd;
+    const char *mode;
+{
+    char *path;         /* identifier for error messages */
+    gzFile gz;
+
+    if (fd == -1 || (path = malloc(7 + 3 * sizeof(int))) == NULL)
+        return NULL;
+    sprintf(path, "<fd:%d>", fd);   /* for debugging */
+    gz = gz_open(path, fd, mode);
+    free(path);
+    return gz;
+}
+
+/* -- see zlib.h -- */
+int ZEXPORT gzbuffer(file, size)
+    gzFile file;
+    unsigned size;
+{
+    gz_statep state;
+
+    /* get internal structure and check integrity */
+    if (file == NULL)
+        return -1;
+    state = (gz_statep)file;
+    if (state->mode != GZ_READ && state->mode != GZ_WRITE)
+        return -1;
+
+    /* make sure we haven't already allocated memory */
+    if (state->size != 0)
+        return -1;
+
+    /* check and set requested size */
+    if (size == 0)
+        return -1;
+    state->want = size;
+    return 0;
+}
+
+/* -- see zlib.h -- */
+int ZEXPORT gzrewind(file)
+    gzFile file;
+{
+    gz_statep state;
+
+    /* get internal structure */
+    if (file == NULL)
+        return -1;
+    state = (gz_statep)file;
+
+    /* check that we're reading and that there's no error */
+    if (state->mode != GZ_READ || state->err != Z_OK)
+        return -1;
+
+    /* back up and start over */
+    if (LSEEK(state->fd, state->start, SEEK_SET) == -1)
+        return -1;
+    gz_reset(state);
+    return 0;
+}
+
+/* -- see zlib.h -- */
+z_off64_t ZEXPORT gzseek64(file, offset, whence)
+    gzFile file;
+    z_off64_t offset;
+    int whence;
+{
+    unsigned n;
+    z_off64_t ret;
+    gz_statep state;
+
+    /* get internal structure and check integrity */
+    if (file == NULL)
+        return -1;
+    state = (gz_statep)file;
+    if (state->mode != GZ_READ && state->mode != GZ_WRITE)
+        return -1;
+
+    /* check that there's no error */
+    if (state->err != Z_OK)
+        return -1;
+
+    /* can only seek from start or relative to current position */
+    if (whence != SEEK_SET && whence != SEEK_CUR)
+        return -1;
+
+    /* normalize offset to a SEEK_CUR specification */
+    if (whence == SEEK_SET)
+        offset -= state->pos;
+    else if (state->seek)
+        offset += state->skip;
+    state->seek = 0;
+
+    /* if within raw area while reading, just go there */
+    if (state->mode == GZ_READ && state->how == COPY &&
+        state->pos + offset >= state->raw) {
+        ret = LSEEK(state->fd, offset - state->have, SEEK_CUR);
+        if (ret == -1)
+            return -1;
+        state->have = 0;
+        state->eof = 0;
+        state->seek = 0;
+        gz_error(state, Z_OK, NULL);
+        state->strm.avail_in = 0;
+        state->pos += offset;
+        return state->pos;
+    }
+
+    /* calculate skip amount, rewinding if needed for back seek when reading */
+    if (offset < 0) {
+        if (state->mode != GZ_READ)         /* writing -- can't go backwards */
+            return -1;
+        offset += state->pos;
+        if (offset < 0)                     /* before start of file! */
+            return -1;
+        if (gzrewind(file) == -1)           /* rewind, then skip to offset */
+            return -1;
+    }
+
+    /* if reading, skip what's in output buffer (one less gzgetc() check) */
+    if (state->mode == GZ_READ) {
+        n = GT_OFF(state->have) || (z_off64_t)state->have > offset ?
+            (unsigned)offset : state->have;
+        state->have -= n;
+        state->next += n;
+        state->pos += n;
+        offset -= n;
+    }
+
+    /* request skip (if not zero) */
+    if (offset) {
+        state->seek = 1;
+        state->skip = offset;
+    }
+    return state->pos + offset;
+}
+
+/* -- see zlib.h -- */
+z_off_t ZEXPORT gzseek(file, offset, whence)
+    gzFile file;
+    z_off_t offset;
+    int whence;
+{
+    z_off64_t ret;
+
+    ret = gzseek64(file, (z_off64_t)offset, whence);
+    return ret == (z_off_t)ret ? (z_off_t)ret : -1;
+}
+
+/* -- see zlib.h -- */
+z_off64_t ZEXPORT gztell64(file)
+    gzFile file;
+{
+    gz_statep state;
+
+    /* get internal structure and check integrity */
+    if (file == NULL)
+        return -1;
+    state = (gz_statep)file;
+    if (state->mode != GZ_READ && state->mode != GZ_WRITE)
+        return -1;
+
+    /* return position */
+    return state->pos + (state->seek ? state->skip : 0);
+}
+
+/* -- see zlib.h -- */
+z_off_t ZEXPORT gztell(file)
+    gzFile file;
+{
+    z_off64_t ret;
+
+    ret = gztell64(file);
+    return ret == (z_off_t)ret ? (z_off_t)ret : -1;
+}
+
+/* -- see zlib.h -- */
+z_off64_t ZEXPORT gzoffset64(file)
+    gzFile file;
+{
+    z_off64_t offset;
+    gz_statep state;
+
+    /* get internal structure and check integrity */
+    if (file == NULL)
+        return -1;
+    state = (gz_statep)file;
+    if (state->mode != GZ_READ && state->mode != GZ_WRITE)
+        return -1;
+
+    /* compute and return effective offset in file */
+    offset = LSEEK(state->fd, 0, SEEK_CUR);
+    if (offset == -1)
+        return -1;
+    if (state->mode == GZ_READ)             /* reading */
+        offset -= state->strm.avail_in;     /* don't count buffered input */
+    return offset;
+}
+
+/* -- see zlib.h -- */
+z_off_t ZEXPORT gzoffset(file)
+    gzFile file;
+{
+    z_off64_t ret;
+
+    ret = gzoffset64(file);
+    return ret == (z_off_t)ret ? (z_off_t)ret : -1;
+}
+
+/* -- see zlib.h -- */
+int ZEXPORT gzeof(file)
+    gzFile file;
+{
+    gz_statep state;
+
+    /* get internal structure and check integrity */
+    if (file == NULL)
+        return 0;
+    state = (gz_statep)file;
+    if (state->mode != GZ_READ && state->mode != GZ_WRITE)
+        return 0;
+
+    /* return end-of-file state */
+    return state->mode == GZ_READ ?
+        (state->eof && state->strm.avail_in == 0 && state->have == 0) : 0;
+}
+
+/* -- see zlib.h -- */
+const char * ZEXPORT gzerror(file, errnum)
+    gzFile file;
+    int *errnum;
+{
+    gz_statep state;
+
+    /* get internal structure and check integrity */
+    if (file == NULL)
+        return NULL;
+    state = (gz_statep)file;
+    if (state->mode != GZ_READ && state->mode != GZ_WRITE)
+        return NULL;
+
+    /* return error information */
+    if (errnum != NULL)
+        *errnum = state->err;
+    return state->msg == NULL ? "" : state->msg;
+}
+
+/* -- see zlib.h -- */
+void ZEXPORT gzclearerr(file)
+    gzFile file;
+{
+    gz_statep state;
+
+    /* get internal structure and check integrity */
+    if (file == NULL)
+        return;
+    state = (gz_statep)file;
+    if (state->mode != GZ_READ && state->mode != GZ_WRITE)
+        return;
+
+    /* clear error and end-of-file */
+    if (state->mode == GZ_READ)
+        state->eof = 0;
+    gz_error(state, Z_OK, NULL);
+}
+
+/* Create an error message in allocated memory and set state->err and
+   state->msg accordingly.  Free any previous error message already there.  Do
+   not try to free or allocate space if the error is Z_MEM_ERROR (out of
+   memory).  Simply save the error message as a static string.  If there is an
+   allocation failure constructing the error message, then convert the error to
+   out of memory. */
+void ZLIB_INTERNAL gz_error(state, err, msg)
+    gz_statep state;
+    int err;
+    const char *msg;
+{
+    /* free previously allocated message and clear */
+    if (state->msg != NULL) {
+        if (state->err != Z_MEM_ERROR)
+            free(state->msg);
+        state->msg = NULL;
+    }
+
+    /* set error code, and if no message, then done */
+    state->err = err;
+    if (msg == NULL)
+        return;
+
+    /* for an out of memory error, save as static string */
+    if (err == Z_MEM_ERROR) {
+        state->msg = (char *)msg;
+        return;
+    }
+
+    /* construct error message with path */
+    if ((state->msg = malloc(strlen(state->path) + strlen(msg) + 3)) == NULL) {
+        state->err = Z_MEM_ERROR;
+        state->msg = (char *)"out of memory";
+        return;
+    }
+    strcpy(state->msg, state->path);
+    strcat(state->msg, ": ");
+    strcat(state->msg, msg);
+    return;
+}
+
+#ifndef INT_MAX
+/* portably return maximum value for an int (when limits.h presumed not
+   available) -- we need to do this to cover cases where 2's complement not
+   used, since C standard permits 1's complement and sign-bit representations,
+   otherwise we could just use ((unsigned)-1) >> 1 */
+unsigned ZLIB_INTERNAL gz_intmax()
+{
+    unsigned p, q;
+
+    p = 1;
+    do {
+        q = p;
+        p <<= 1;
+        p++;
+    } while (p > q);
+    return q >> 1;
+}
+#endif
diff --git a/3rdparty/zlib/gzread.c b/3rdparty/zlib/gzread.c
new file mode 100644 (file)
index 0000000..548201a
--- /dev/null
@@ -0,0 +1,653 @@
+/* gzread.c -- zlib functions for reading gzip files
+ * Copyright (C) 2004, 2005, 2010 Mark Adler
+ * For conditions of distribution and use, see copyright notice in zlib.h
+ */
+
+#include "gzguts.h"
+
+/* Local functions */
+local int gz_load OF((gz_statep, unsigned char *, unsigned, unsigned *));
+local int gz_avail OF((gz_statep));
+local int gz_next4 OF((gz_statep, unsigned long *));
+local int gz_head OF((gz_statep));
+local int gz_decomp OF((gz_statep));
+local int gz_make OF((gz_statep));
+local int gz_skip OF((gz_statep, z_off64_t));
+
+/* Use read() to load a buffer -- return -1 on error, otherwise 0.  Read from
+   state->fd, and update state->eof, state->err, and state->msg as appropriate.
+   This function needs to loop on read(), since read() is not guaranteed to
+   read the number of bytes requested, depending on the type of descriptor. */
+local int gz_load(state, buf, len, have)
+    gz_statep state;
+    unsigned char *buf;
+    unsigned len;
+    unsigned *have;
+{
+    int ret;
+
+    *have = 0;
+    do {
+        ret = read(state->fd, buf + *have, len - *have);
+        if (ret <= 0)
+            break;
+        *have += ret;
+    } while (*have < len);
+    if (ret < 0) {
+        gz_error(state, Z_ERRNO, zstrerror());
+        return -1;
+    }
+    if (ret == 0)
+        state->eof = 1;
+    return 0;
+}
+
+/* Load up input buffer and set eof flag if last data loaded -- return -1 on
+   error, 0 otherwise.  Note that the eof flag is set when the end of the input
+   file is reached, even though there may be unused data in the buffer.  Once
+   that data has been used, no more attempts will be made to read the file.
+   gz_avail() assumes that strm->avail_in == 0. */
+local int gz_avail(state)
+    gz_statep state;
+{
+    z_streamp strm = &(state->strm);
+
+    if (state->err != Z_OK)
+        return -1;
+    if (state->eof == 0) {
+        if (gz_load(state, state->in, state->size,
+                (unsigned *)&(strm->avail_in)) == -1)
+            return -1;
+        strm->next_in = state->in;
+    }
+    return 0;
+}
+
+/* Get next byte from input, or -1 if end or error. */
+#define NEXT() ((strm->avail_in == 0 && gz_avail(state) == -1) ? -1 : \
+                (strm->avail_in == 0 ? -1 : \
+                 (strm->avail_in--, *(strm->next_in)++)))
+
+/* Get a four-byte little-endian integer and return 0 on success and the value
+   in *ret.  Otherwise -1 is returned and *ret is not modified. */
+local int gz_next4(state, ret)
+    gz_statep state;
+    unsigned long *ret;
+{
+    int ch;
+    unsigned long val;
+    z_streamp strm = &(state->strm);
+
+    val = NEXT();
+    val += (unsigned)NEXT() << 8;
+    val += (unsigned long)NEXT() << 16;
+    ch = NEXT();
+    if (ch == -1)
+        return -1;
+    val += (unsigned long)ch << 24;
+    *ret = val;
+    return 0;
+}
+
+/* Look for gzip header, set up for inflate or copy.  state->have must be zero.
+   If this is the first time in, allocate required memory.  state->how will be
+   left unchanged if there is no more input data available, will be set to COPY
+   if there is no gzip header and direct copying will be performed, or it will
+   be set to GZIP for decompression, and the gzip header will be skipped so
+   that the next available input data is the raw deflate stream.  If direct
+   copying, then leftover input data from the input buffer will be copied to
+   the output buffer.  In that case, all further file reads will be directly to
+   either the output buffer or a user buffer.  If decompressing, the inflate
+   state and the check value will be initialized.  gz_head() will return 0 on
+   success or -1 on failure.  Failures may include read errors or gzip header
+   errors.  */
+local int gz_head(state)
+    gz_statep state;
+{
+    z_streamp strm = &(state->strm);
+    int flags;
+    unsigned len;
+
+    /* allocate read buffers and inflate memory */
+    if (state->size == 0) {
+        /* allocate buffers */
+        state->in = malloc(state->want);
+        state->out = malloc(state->want << 1);
+        if (state->in == NULL || state->out == NULL) {
+            if (state->out != NULL)
+                free(state->out);
+            if (state->in != NULL)
+                free(state->in);
+            gz_error(state, Z_MEM_ERROR, "out of memory");
+            return -1;
+        }
+        state->size = state->want;
+
+        /* allocate inflate memory */
+        state->strm.zalloc = Z_NULL;
+        state->strm.zfree = Z_NULL;
+        state->strm.opaque = Z_NULL;
+        state->strm.avail_in = 0;
+        state->strm.next_in = Z_NULL;
+        if (inflateInit2(&(state->strm), -15) != Z_OK) {    /* raw inflate */
+            free(state->out);
+            free(state->in);
+            state->size = 0;
+            gz_error(state, Z_MEM_ERROR, "out of memory");
+            return -1;
+        }
+    }
+
+    /* get some data in the input buffer */
+    if (strm->avail_in == 0) {
+        if (gz_avail(state) == -1)
+            return -1;
+        if (strm->avail_in == 0)
+            return 0;
+    }
+
+    /* look for the gzip magic header bytes 31 and 139 */
+    if (strm->next_in[0] == 31) {
+        strm->avail_in--;
+        strm->next_in++;
+        if (strm->avail_in == 0 && gz_avail(state) == -1)
+            return -1;
+        if (strm->avail_in && strm->next_in[0] == 139) {
+            /* we have a gzip header, woo hoo! */
+            strm->avail_in--;
+            strm->next_in++;
+
+            /* skip rest of header */
+            if (NEXT() != 8) {      /* compression method */
+                gz_error(state, Z_DATA_ERROR, "unknown compression method");
+                return -1;
+            }
+            flags = NEXT();
+            if (flags & 0xe0) {     /* reserved flag bits */
+                gz_error(state, Z_DATA_ERROR, "unknown header flags set");
+                return -1;
+            }
+            NEXT();                 /* modification time */
+            NEXT();
+            NEXT();
+            NEXT();
+            NEXT();                 /* extra flags */
+            NEXT();                 /* operating system */
+            if (flags & 4) {        /* extra field */
+                len = (unsigned)NEXT();
+                len += (unsigned)NEXT() << 8;
+                while (len--)
+                    if (NEXT() < 0)
+                        break;
+            }
+            if (flags & 8)          /* file name */
+                while (NEXT() > 0)
+                    ;
+            if (flags & 16)         /* comment */
+                while (NEXT() > 0)
+                    ;
+            if (flags & 2) {        /* header crc */
+                NEXT();
+                NEXT();
+            }
+            /* an unexpected end of file is not checked for here -- it will be
+               noticed on the first request for uncompressed data */
+
+            /* set up for decompression */
+            inflateReset(strm);
+            strm->adler = crc32(0L, Z_NULL, 0);
+            state->how = GZIP;
+            state->direct = 0;
+            return 0;
+        }
+        else {
+            /* not a gzip file -- save first byte (31) and fall to raw i/o */
+            state->out[0] = 31;
+            state->have = 1;
+        }
+    }
+
+    /* doing raw i/o, save start of raw data for seeking, copy any leftover
+       input to output -- this assumes that the output buffer is larger than
+       the input buffer, which also assures space for gzungetc() */
+    state->raw = state->pos;
+    state->next = state->out;
+    if (strm->avail_in) {
+        memcpy(state->next + state->have, strm->next_in, strm->avail_in);
+        state->have += strm->avail_in;
+        strm->avail_in = 0;
+    }
+    state->how = COPY;
+    state->direct = 1;
+    return 0;
+}
+
+/* Decompress from input to the provided next_out and avail_out in the state.
+   If the end of the compressed data is reached, then verify the gzip trailer
+   check value and length (modulo 2^32).  state->have and state->next are set
+   to point to the just decompressed data, and the crc is updated.  If the
+   trailer is verified, state->how is reset to LOOK to look for the next gzip
+   stream or raw data, once state->have is depleted.  Returns 0 on success, -1
+   on failure.  Failures may include invalid compressed data or a failed gzip
+   trailer verification. */
+local int gz_decomp(state)
+    gz_statep state;
+{
+    int ret;
+    unsigned had;
+    unsigned long crc, len;
+    z_streamp strm = &(state->strm);
+
+    /* fill output buffer up to end of deflate stream */
+    had = strm->avail_out;
+    do {
+        /* get more input for inflate() */
+        if (strm->avail_in == 0 && gz_avail(state) == -1)
+            return -1;
+        if (strm->avail_in == 0) {
+            gz_error(state, Z_DATA_ERROR, "unexpected end of file");
+            return -1;
+        }
+
+        /* decompress and handle errors */
+        ret = inflate(strm, Z_NO_FLUSH);
+        if (ret == Z_STREAM_ERROR || ret == Z_NEED_DICT) {
+            gz_error(state, Z_STREAM_ERROR,
+                      "internal error: inflate stream corrupt");
+            return -1;
+        }
+        if (ret == Z_MEM_ERROR) {
+            gz_error(state, Z_MEM_ERROR, "out of memory");
+            return -1;
+        }
+        if (ret == Z_DATA_ERROR) {              /* deflate stream invalid */
+            gz_error(state, Z_DATA_ERROR,
+                      strm->msg == NULL ? "compressed data error" : strm->msg);
+            return -1;
+        }
+    } while (strm->avail_out && ret != Z_STREAM_END);
+
+    /* update available output and crc check value */
+    state->have = had - strm->avail_out;
+    state->next = strm->next_out - state->have;
+    strm->adler = crc32(strm->adler, state->next, state->have);
+
+    /* check gzip trailer if at end of deflate stream */
+    if (ret == Z_STREAM_END) {
+        if (gz_next4(state, &crc) == -1 || gz_next4(state, &len) == -1) {
+            gz_error(state, Z_DATA_ERROR, "unexpected end of file");
+            return -1;
+        }
+        if (crc != strm->adler) {
+            gz_error(state, Z_DATA_ERROR, "incorrect data check");
+            return -1;
+        }
+        if (len != (strm->total_out & 0xffffffffL)) {
+            gz_error(state, Z_DATA_ERROR, "incorrect length check");
+            return -1;
+        }
+        state->how = LOOK;      /* ready for next stream, once have is 0 (leave
+                                   state->direct unchanged to remember how) */
+    }
+
+    /* good decompression */
+    return 0;
+}
+
+/* Make data and put in the output buffer.  Assumes that state->have == 0.
+   Data is either copied from the input file or decompressed from the input
+   file depending on state->how.  If state->how is LOOK, then a gzip header is
+   looked for (and skipped if found) to determine wither to copy or decompress.
+   Returns -1 on error, otherwise 0.  gz_make() will leave state->have as COPY
+   or GZIP unless the end of the input file has been reached and all data has
+   been processed.  */
+local int gz_make(state)
+    gz_statep state;
+{
+    z_streamp strm = &(state->strm);
+
+    if (state->how == LOOK) {           /* look for gzip header */
+        if (gz_head(state) == -1)
+            return -1;
+        if (state->have)                /* got some data from gz_head() */
+            return 0;
+    }
+    if (state->how == COPY) {           /* straight copy */
+        if (gz_load(state, state->out, state->size << 1, &(state->have)) == -1)
+            return -1;
+        state->next = state->out;
+    }
+    else if (state->how == GZIP) {      /* decompress */
+        strm->avail_out = state->size << 1;
+        strm->next_out = state->out;
+        if (gz_decomp(state) == -1)
+            return -1;
+    }
+    return 0;
+}
+
+/* Skip len uncompressed bytes of output.  Return -1 on error, 0 on success. */
+local int gz_skip(state, len)
+    gz_statep state;
+    z_off64_t len;
+{
+    unsigned n;
+
+    /* skip over len bytes or reach end-of-file, whichever comes first */
+    while (len)
+        /* skip over whatever is in output buffer */
+        if (state->have) {
+            n = GT_OFF(state->have) || (z_off64_t)state->have > len ?
+                (unsigned)len : state->have;
+            state->have -= n;
+            state->next += n;
+            state->pos += n;
+            len -= n;
+        }
+
+        /* output buffer empty -- return if we're at the end of the input */
+        else if (state->eof && state->strm.avail_in == 0)
+            break;
+
+        /* need more data to skip -- load up output buffer */
+        else {
+            /* get more output, looking for header if required */
+            if (gz_make(state) == -1)
+                return -1;
+        }
+    return 0;
+}
+
+/* -- see zlib.h -- */
+int ZEXPORT gzread(file, buf, len)
+    gzFile file;
+    voidp buf;
+    unsigned len;
+{
+    unsigned got, n;
+    gz_statep state;
+    z_streamp strm;
+
+    /* get internal structure */
+    if (file == NULL)
+        return -1;
+    state = (gz_statep)file;
+    strm = &(state->strm);
+
+    /* check that we're reading and that there's no error */
+    if (state->mode != GZ_READ || state->err != Z_OK)
+        return -1;
+
+    /* since an int is returned, make sure len fits in one, otherwise return
+       with an error (this avoids the flaw in the interface) */
+    if ((int)len < 0) {
+        gz_error(state, Z_BUF_ERROR, "requested length does not fit in int");
+        return -1;
+    }
+
+    /* if len is zero, avoid unnecessary operations */
+    if (len == 0)
+        return 0;
+
+    /* process a skip request */
+    if (state->seek) {
+        state->seek = 0;
+        if (gz_skip(state, state->skip) == -1)
+            return -1;
+    }
+
+    /* get len bytes to buf, or less than len if at the end */
+    got = 0;
+    do {
+        /* first just try copying data from the output buffer */
+        if (state->have) {
+            n = state->have > len ? len : state->have;
+            memcpy(buf, state->next, n);
+            state->next += n;
+            state->have -= n;
+        }
+
+        /* output buffer empty -- return if we're at the end of the input */
+        else if (state->eof && strm->avail_in == 0)
+            break;
+
+        /* need output data -- for small len or new stream load up our output
+           buffer */
+        else if (state->how == LOOK || len < (state->size << 1)) {
+            /* get more output, looking for header if required */
+            if (gz_make(state) == -1)
+                return -1;
+            continue;       /* no progress yet -- go back to memcpy() above */
+            /* the copy above assures that we will leave with space in the
+               output buffer, allowing at least one gzungetc() to succeed */
+        }
+
+        /* large len -- read directly into user buffer */
+        else if (state->how == COPY) {      /* read directly */
+            if (gz_load(state, buf, len, &n) == -1)
+                return -1;
+        }
+
+        /* large len -- decompress directly into user buffer */
+        else {  /* state->how == GZIP */
+            strm->avail_out = len;
+            strm->next_out = buf;
+            if (gz_decomp(state) == -1)
+                return -1;
+            n = state->have;
+            state->have = 0;
+        }
+
+        /* update progress */
+        len -= n;
+        buf = (char *)buf + n;
+        got += n;
+        state->pos += n;
+    } while (len);
+
+    /* return number of bytes read into user buffer (will fit in int) */
+    return (int)got;
+}
+
+/* -- see zlib.h -- */
+int ZEXPORT gzgetc(file)
+    gzFile file;
+{
+    int ret;
+    unsigned char buf[1];
+    gz_statep state;
+
+    /* get internal structure */
+    if (file == NULL)
+        return -1;
+    state = (gz_statep)file;
+
+    /* check that we're reading and that there's no error */
+    if (state->mode != GZ_READ || state->err != Z_OK)
+        return -1;
+
+    /* try output buffer (no need to check for skip request) */
+    if (state->have) {
+        state->have--;
+        state->pos++;
+        return *(state->next)++;
+    }
+
+    /* nothing there -- try gzread() */
+    ret = gzread(file, buf, 1);
+    return ret < 1 ? -1 : buf[0];
+}
+
+/* -- see zlib.h -- */
+int ZEXPORT gzungetc(c, file)
+    int c;
+    gzFile file;
+{
+    gz_statep state;
+
+    /* get internal structure */
+    if (file == NULL)
+        return -1;
+    state = (gz_statep)file;
+
+    /* check that we're reading and that there's no error */
+    if (state->mode != GZ_READ || state->err != Z_OK)
+        return -1;
+
+    /* process a skip request */
+    if (state->seek) {
+        state->seek = 0;
+        if (gz_skip(state, state->skip) == -1)
+            return -1;
+    }
+
+    /* can't push EOF */
+    if (c < 0)
+        return -1;
+
+    /* if output buffer empty, put byte at end (allows more pushing) */
+    if (state->have == 0) {
+        state->have = 1;
+        state->next = state->out + (state->size << 1) - 1;
+        state->next[0] = c;
+        state->pos--;
+        return c;
+    }
+
+    /* if no room, give up (must have already done a gzungetc()) */
+    if (state->have == (state->size << 1)) {
+        gz_error(state, Z_BUF_ERROR, "out of room to push characters");
+        return -1;
+    }
+
+    /* slide output data if needed and insert byte before existing data */
+    if (state->next == state->out) {
+        unsigned char *src = state->out + state->have;
+        unsigned char *dest = state->out + (state->size << 1);
+        while (src > state->out)
+            *--dest = *--src;
+        state->next = dest;
+    }
+    state->have++;
+    state->next--;
+    state->next[0] = c;
+    state->pos--;
+    return c;
+}
+
+/* -- see zlib.h -- */
+char * ZEXPORT gzgets(file, buf, len)
+    gzFile file;
+    char *buf;
+    int len;
+{
+    unsigned left, n;
+    char *str;
+    unsigned char *eol;
+    gz_statep state;
+
+    /* check parameters and get internal structure */
+    if (file == NULL || buf == NULL || len < 1)
+        return NULL;
+    state = (gz_statep)file;
+
+    /* check that we're reading and that there's no error */
+    if (state->mode != GZ_READ || state->err != Z_OK)
+        return NULL;
+
+    /* process a skip request */
+    if (state->seek) {
+        state->seek = 0;
+        if (gz_skip(state, state->skip) == -1)
+            return NULL;
+    }
+
+    /* copy output bytes up to new line or len - 1, whichever comes first --
+       append a terminating zero to the string (we don't check for a zero in
+       the contents, let the user worry about that) */
+    str = buf;
+    left = (unsigned)len - 1;
+    if (left) do {
+        /* assure that something is in the output buffer */
+        if (state->have == 0) {
+            if (gz_make(state) == -1)
+                return NULL;            /* error */
+            if (state->have == 0) {     /* end of file */
+                if (buf == str)         /* got bupkus */
+                    return NULL;
+                break;                  /* got something -- return it */
+            }
+        }
+
+        /* look for end-of-line in current output buffer */
+        n = state->have > left ? left : state->have;
+        eol = memchr(state->next, '\n', n);
+        if (eol != NULL)
+            n = (unsigned)(eol - state->next) + 1;
+
+        /* copy through end-of-line, or remainder if not found */
+        memcpy(buf, state->next, n);
+        state->have -= n;
+        state->next += n;
+        state->pos += n;
+        left -= n;
+        buf += n;
+    } while (left && eol == NULL);
+
+    /* found end-of-line or out of space -- terminate string and return it */
+    buf[0] = 0;
+    return str;
+}
+
+/* -- see zlib.h -- */
+int ZEXPORT gzdirect(file)
+    gzFile file;
+{
+    gz_statep state;
+
+    /* get internal structure */
+    if (file == NULL)
+        return 0;
+    state = (gz_statep)file;
+
+    /* check that we're reading */
+    if (state->mode != GZ_READ)
+        return 0;
+
+    /* if the state is not known, but we can find out, then do so (this is
+       mainly for right after a gzopen() or gzdopen()) */
+    if (state->how == LOOK && state->have == 0)
+        (void)gz_head(state);
+
+    /* return 1 if reading direct, 0 if decompressing a gzip stream */
+    return state->direct;
+}
+
+/* -- see zlib.h -- */
+int ZEXPORT gzclose_r(file)
+    gzFile file;
+{
+    int ret;
+    gz_statep state;
+
+    /* get internal structure */
+    if (file == NULL)
+        return Z_STREAM_ERROR;
+    state = (gz_statep)file;
+
+    /* check that we're reading */
+    if (state->mode != GZ_READ)
+        return Z_STREAM_ERROR;
+
+    /* free memory and close file */
+    if (state->size) {
+        inflateEnd(&(state->strm));
+        free(state->out);
+        free(state->in);
+    }
+    gz_error(state, Z_OK, NULL);
+    free(state->path);
+    ret = close(state->fd);
+    free(state);
+    return ret ? Z_ERRNO : Z_OK;
+}
diff --git a/3rdparty/zlib/gzwrite.c b/3rdparty/zlib/gzwrite.c
new file mode 100644 (file)
index 0000000..e8defc6
--- /dev/null
@@ -0,0 +1,531 @@
+/* gzwrite.c -- zlib functions for writing gzip files
+ * Copyright (C) 2004, 2005, 2010 Mark Adler
+ * For conditions of distribution and use, see copyright notice in zlib.h
+ */
+
+#include "gzguts.h"
+
+/* Local functions */
+local int gz_init OF((gz_statep));
+local int gz_comp OF((gz_statep, int));
+local int gz_zero OF((gz_statep, z_off64_t));
+
+/* Initialize state for writing a gzip file.  Mark initialization by setting
+   state->size to non-zero.  Return -1 on failure or 0 on success. */
+local int gz_init(state)
+    gz_statep state;
+{
+    int ret;
+    z_streamp strm = &(state->strm);
+
+    /* allocate input and output buffers */
+    state->in = malloc(state->want);
+    state->out = malloc(state->want);
+    if (state->in == NULL || state->out == NULL) {
+        if (state->out != NULL)
+            free(state->out);
+        if (state->in != NULL)
+            free(state->in);
+        gz_error(state, Z_MEM_ERROR, "out of memory");
+        return -1;
+    }
+
+    /* allocate deflate memory, set up for gzip compression */
+    strm->zalloc = Z_NULL;
+    strm->zfree = Z_NULL;
+    strm->opaque = Z_NULL;
+    ret = deflateInit2(strm, state->level, Z_DEFLATED,
+                       15 + 16, 8, state->strategy);
+    if (ret != Z_OK) {
+        free(state->in);
+        gz_error(state, Z_MEM_ERROR, "out of memory");
+        return -1;
+    }
+
+    /* mark state as initialized */
+    state->size = state->want;
+
+    /* initialize write buffer */
+    strm->avail_out = state->size;
+    strm->next_out = state->out;
+    state->next = strm->next_out;
+    return 0;
+}
+
+/* Compress whatever is at avail_in and next_in and write to the output file.
+   Return -1 if there is an error writing to the output file, otherwise 0.
+   flush is assumed to be a valid deflate() flush value.  If flush is Z_FINISH,
+   then the deflate() state is reset to start a new gzip stream. */
+local int gz_comp(state, flush)
+    gz_statep state;
+    int flush;
+{
+    int ret, got;
+    unsigned have;
+    z_streamp strm = &(state->strm);
+
+    /* allocate memory if this is the first time through */
+    if (state->size == 0 && gz_init(state) == -1)
+        return -1;
+
+    /* run deflate() on provided input until it produces no more output */
+    ret = Z_OK;
+    do {
+        /* write out current buffer contents if full, or if flushing, but if
+           doing Z_FINISH then don't write until we get to Z_STREAM_END */
+        if (strm->avail_out == 0 || (flush != Z_NO_FLUSH &&
+            (flush != Z_FINISH || ret == Z_STREAM_END))) {
+            have = (unsigned)(strm->next_out - state->next);
+            if (have && ((got = write(state->fd, state->next, have)) < 0 ||
+                         (unsigned)got != have)) {
+                gz_error(state, Z_ERRNO, zstrerror());
+                return -1;
+            }
+            if (strm->avail_out == 0) {
+                strm->avail_out = state->size;
+                strm->next_out = state->out;
+            }
+            state->next = strm->next_out;
+        }
+
+        /* compress */
+        have = strm->avail_out;
+        ret = deflate(strm, flush);
+        if (ret == Z_STREAM_ERROR) {
+            gz_error(state, Z_STREAM_ERROR,
+                      "internal error: deflate stream corrupt");
+            return -1;
+        }
+        have -= strm->avail_out;
+    } while (have);
+
+    /* if that completed a deflate stream, allow another to start */
+    if (flush == Z_FINISH)
+        deflateReset(strm);
+
+    /* all done, no errors */
+    return 0;
+}
+
+/* Compress len zeros to output.  Return -1 on error, 0 on success. */
+local int gz_zero(state, len)
+    gz_statep state;
+    z_off64_t len;
+{
+    int first;
+    unsigned n;
+    z_streamp strm = &(state->strm);
+
+    /* consume whatever's left in the input buffer */
+    if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1)
+        return -1;
+
+    /* compress len zeros (len guaranteed > 0) */
+    first = 1;
+    while (len) {
+        n = GT_OFF(state->size) || (z_off64_t)state->size > len ?
+            (unsigned)len : state->size;
+        if (first) {
+            memset(state->in, 0, n);
+            first = 0;
+        }
+        strm->avail_in = n;
+        strm->next_in = state->in;
+        state->pos += n;
+        if (gz_comp(state, Z_NO_FLUSH) == -1)
+            return -1;
+        len -= n;
+    }
+    return 0;
+}
+
+/* -- see zlib.h -- */
+int ZEXPORT gzwrite(file, buf, len)
+    gzFile file;
+    voidpc buf;
+    unsigned len;
+{
+    unsigned put = len;
+    unsigned n;
+    gz_statep state;
+    z_streamp strm;
+
+    /* get internal structure */
+    if (file == NULL)
+        return 0;
+    state = (gz_statep)file;
+    strm = &(state->strm);
+
+    /* check that we're writing and that there's no error */
+    if (state->mode != GZ_WRITE || state->err != Z_OK)
+        return 0;
+
+    /* since an int is returned, make sure len fits in one, otherwise return
+       with an error (this avoids the flaw in the interface) */
+    if ((int)len < 0) {
+        gz_error(state, Z_BUF_ERROR, "requested length does not fit in int");
+        return 0;
+    }
+
+    /* if len is zero, avoid unnecessary operations */
+    if (len == 0)
+        return 0;
+
+    /* allocate memory if this is the first time through */
+    if (state->size == 0 && gz_init(state) == -1)
+        return 0;
+
+    /* check for seek request */
+    if (state->seek) {
+        state->seek = 0;
+        if (gz_zero(state, state->skip) == -1)
+            return 0;
+    }
+
+    /* for small len, copy to input buffer, otherwise compress directly */
+    if (len < state->size) {
+        /* copy to input buffer, compress when full */
+        do {
+            if (strm->avail_in == 0)
+                strm->next_in = state->in;
+            n = state->size - strm->avail_in;
+            if (n > len)
+                n = len;
+            memcpy(strm->next_in + strm->avail_in, buf, n);
+            strm->avail_in += n;
+            state->pos += n;
+            buf = (char *)buf + n;
+            len -= n;
+            if (len && gz_comp(state, Z_NO_FLUSH) == -1)
+                return 0;
+        } while (len);
+    }
+    else {
+        /* consume whatever's left in the input buffer */
+        if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1)
+            return 0;
+
+        /* directly compress user buffer to file */
+        strm->avail_in = len;
+        strm->next_in = (voidp)buf;
+        state->pos += len;
+        if (gz_comp(state, Z_NO_FLUSH) == -1)
+            return 0;
+    }
+
+    /* input was all buffered or compressed (put will fit in int) */
+    return (int)put;
+}
+
+/* -- see zlib.h -- */
+int ZEXPORT gzputc(file, c)
+    gzFile file;
+    int c;
+{
+    unsigned char buf[1];
+    gz_statep state;
+    z_streamp strm;
+
+    /* get internal structure */
+    if (file == NULL)
+        return -1;
+    state = (gz_statep)file;
+    strm = &(state->strm);
+
+    /* check that we're writing and that there's no error */
+    if (state->mode != GZ_WRITE || state->err != Z_OK)
+        return -1;
+
+    /* check for seek request */
+    if (state->seek) {
+        state->seek = 0;
+        if (gz_zero(state, state->skip) == -1)
+            return -1;
+    }
+
+    /* try writing to input buffer for speed (state->size == 0 if buffer not
+       initialized) */
+    if (strm->avail_in < state->size) {
+        if (strm->avail_in == 0)
+            strm->next_in = state->in;
+        strm->next_in[strm->avail_in++] = c;
+        state->pos++;
+        return c;
+    }
+
+    /* no room in buffer or not initialized, use gz_write() */
+    buf[0] = c;
+    if (gzwrite(file, buf, 1) != 1)
+        return -1;
+    return c;
+}
+
+/* -- see zlib.h -- */
+int ZEXPORT gzputs(file, str)
+    gzFile file;
+    const char *str;
+{
+    int ret;
+    unsigned len;
+
+    /* write string */
+    len = (unsigned)strlen(str);
+    ret = gzwrite(file, str, len);
+    return ret == 0 && len != 0 ? -1 : ret;
+}
+
+#ifdef STDC
+#include <stdarg.h>
+
+/* -- see zlib.h -- */
+int ZEXPORTVA gzprintf (gzFile file, const char *format, ...)
+{
+    int size, len;
+    gz_statep state;
+    z_streamp strm;
+    va_list va;
+
+    /* get internal structure */
+    if (file == NULL)
+        return -1;
+    state = (gz_statep)file;
+    strm = &(state->strm);
+
+    /* check that we're writing and that there's no error */
+    if (state->mode != GZ_WRITE || state->err != Z_OK)
+        return 0;
+
+    /* make sure we have some buffer space */
+    if (state->size == 0 && gz_init(state) == -1)
+        return 0;
+
+    /* check for seek request */
+    if (state->seek) {
+        state->seek = 0;
+        if (gz_zero(state, state->skip) == -1)
+            return 0;
+    }
+
+    /* consume whatever's left in the input buffer */
+    if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1)
+        return 0;
+
+    /* do the printf() into the input buffer, put length in len */
+    size = (int)(state->size);
+    state->in[size - 1] = 0;
+    va_start(va, format);
+#ifdef NO_vsnprintf
+#  ifdef HAS_vsprintf_void
+    (void)vsprintf(state->in, format, va);
+    va_end(va);
+    for (len = 0; len < size; len++)
+        if (state->in[len] == 0) break;
+#  else
+    len = vsprintf(state->in, format, va);
+    va_end(va);
+#  endif
+#else
+#  ifdef HAS_vsnprintf_void
+    (void)vsnprintf(state->in, size, format, va);
+    va_end(va);
+    len = strlen(state->in);
+#  else
+    len = vsnprintf((char *)(state->in), size, format, va);
+    va_end(va);
+#  endif
+#endif
+
+    /* check that printf() results fit in buffer */
+    if (len <= 0 || len >= (int)size || state->in[size - 1] != 0)
+        return 0;
+
+    /* update buffer and position, defer compression until needed */
+    strm->avail_in = (unsigned)len;
+    strm->next_in = state->in;
+    state->pos += len;
+    return len;
+}
+
+#else /* !STDC */
+
+/* -- see zlib.h -- */
+int ZEXPORTVA gzprintf (file, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10,
+                       a11, a12, a13, a14, a15, a16, a17, a18, a19, a20)
+    gzFile file;
+    const char *format;
+    int a1, a2, a3, a4, a5, a6, a7, a8, a9, a10,
+        a11, a12, a13, a14, a15, a16, a17, a18, a19, a20;
+{
+    int size, len;
+    gz_statep state;
+    z_streamp strm;
+
+    /* get internal structure */
+    if (file == NULL)
+        return -1;
+    state = (gz_statep)file;
+    strm = &(state->strm);
+
+    /* check that we're writing and that there's no error */
+    if (state->mode != GZ_WRITE || state->err != Z_OK)
+        return 0;
+
+    /* make sure we have some buffer space */
+    if (state->size == 0 && gz_init(state) == -1)
+        return 0;
+
+    /* check for seek request */
+    if (state->seek) {
+        state->seek = 0;
+        if (gz_zero(state, state->skip) == -1)
+            return 0;
+    }
+
+    /* consume whatever's left in the input buffer */
+    if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1)
+        return 0;
+
+    /* do the printf() into the input buffer, put length in len */
+    size = (int)(state->size);
+    state->in[size - 1] = 0;
+#ifdef NO_snprintf
+#  ifdef HAS_sprintf_void
+    sprintf(state->in, format, a1, a2, a3, a4, a5, a6, a7, a8,
+            a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
+    for (len = 0; len < size; len++)
+        if (state->in[len] == 0) break;
+#  else
+    len = sprintf(state->in, format, a1, a2, a3, a4, a5, a6, a7, a8,
+                a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
+#  endif
+#else
+#  ifdef HAS_snprintf_void
+    snprintf(state->in, size, format, a1, a2, a3, a4, a5, a6, a7, a8,
+             a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
+    len = strlen(state->in);
+#  else
+    len = snprintf(state->in, size, format, a1, a2, a3, a4, a5, a6, a7, a8,
+                 a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
+#  endif
+#endif
+
+    /* check that printf() results fit in buffer */
+    if (len <= 0 || len >= (int)size || state->in[size - 1] != 0)
+        return 0;
+
+    /* update buffer and position, defer compression until needed */
+    strm->avail_in = (unsigned)len;
+    strm->next_in = state->in;
+    state->pos += len;
+    return len;
+}
+
+#endif
+
+/* -- see zlib.h -- */
+int ZEXPORT gzflush(file, flush)
+    gzFile file;
+    int flush;
+{
+    gz_statep state;
+
+    /* get internal structure */
+    if (file == NULL)
+        return -1;
+    state = (gz_statep)file;
+
+    /* check that we're writing and that there's no error */
+    if (state->mode != GZ_WRITE || state->err != Z_OK)
+        return Z_STREAM_ERROR;
+
+    /* check flush parameter */
+    if (flush < 0 || flush > Z_FINISH)
+        return Z_STREAM_ERROR;
+
+    /* check for seek request */
+    if (state->seek) {
+        state->seek = 0;
+        if (gz_zero(state, state->skip) == -1)
+            return -1;
+    }
+
+    /* compress remaining data with requested flush */
+    gz_comp(state, flush);
+    return state->err;
+}
+
+/* -- see zlib.h -- */
+int ZEXPORT gzsetparams(file, level, strategy)
+    gzFile file;
+    int level;
+    int strategy;
+{
+    gz_statep state;
+    z_streamp strm;
+
+    /* get internal structure */
+    if (file == NULL)
+        return Z_STREAM_ERROR;
+    state = (gz_statep)file;
+    strm = &(state->strm);
+
+    /* check that we're writing and that there's no error */
+    if (state->mode != GZ_WRITE || state->err != Z_OK)
+        return Z_STREAM_ERROR;
+
+    /* if no change is requested, then do nothing */
+    if (level == state->level && strategy == state->strategy)
+        return Z_OK;
+
+    /* check for seek request */
+    if (state->seek) {
+        state->seek = 0;
+        if (gz_zero(state, state->skip) == -1)
+            return -1;
+    }
+
+    /* change compression parameters for subsequent input */
+    if (state->size) {
+        /* flush previous input with previous parameters before changing */
+        if (strm->avail_in && gz_comp(state, Z_PARTIAL_FLUSH) == -1)
+            return state->err;
+        deflateParams(strm, level, strategy);
+    }
+    state->level = level;
+    state->strategy = strategy;
+    return Z_OK;
+}
+
+/* -- see zlib.h -- */
+int ZEXPORT gzclose_w(file)
+    gzFile file;
+{
+    int ret = 0;
+    gz_statep state;
+
+    /* get internal structure */
+    if (file == NULL)
+        return Z_STREAM_ERROR;
+    state = (gz_statep)file;
+
+    /* check that we're writing */
+    if (state->mode != GZ_WRITE)
+        return Z_STREAM_ERROR;
+
+    /* check for seek request */
+    if (state->seek) {
+        state->seek = 0;
+        ret += gz_zero(state, state->skip);
+    }
+
+    /* flush, free memory, and close file */
+    ret += gz_comp(state, Z_FINISH);
+    (void)deflateEnd(&(state->strm));
+    free(state->out);
+    free(state->in);
+    gz_error(state, Z_OK, NULL);
+    free(state->path);
+    ret += close(state->fd);
+    free(state);
+    return ret ? Z_ERRNO : Z_OK;
+}
index 455dbc9..af3a8c9 100644 (file)
@@ -1,5 +1,5 @@
 /* infback.c -- inflate using a call-back interface
- * Copyright (C) 1995-2005 Mark Adler
+ * Copyright (C) 1995-2009 Mark Adler
  * For conditions of distribution and use, see copyright notice in zlib.h
  */
 
@@ -55,7 +55,7 @@ int stream_size;
     state->wbits = windowBits;
     state->wsize = 1U << windowBits;
     state->window = window;
-    state->write = 0;
+    state->wnext = 0;
     state->whave = 0;
     return Z_OK;
 }
@@ -253,7 +253,7 @@ void FAR *out_desc;
     unsigned bits;              /* bits in bit buffer */
     unsigned copy;              /* number of stored or match bytes to copy */
     unsigned char FAR *from;    /* where to copy match bytes from */
-    code this;                  /* current decoding table entry */
+    code here;                  /* current decoding table entry */
     code last;                  /* parent table entry */
     unsigned len;               /* length to copy for repeats, bits to drop */
     int ret;                    /* return code */
@@ -389,19 +389,19 @@ void FAR *out_desc;
             state->have = 0;
             while (state->have < state->nlen + state->ndist) {
                 for (;;) {
-                    this = state->lencode[BITS(state->lenbits)];
-                    if ((unsigned)(this.bits) <= bits) break;
+                    here = state->lencode[BITS(state->lenbits)];
+                    if ((unsigned)(here.bits) <= bits) break;
                     PULLBYTE();
                 }
-                if (this.val < 16) {
-                    NEEDBITS(this.bits);
-                    DROPBITS(this.bits);
-                    state->lens[state->have++] = this.val;
+                if (here.val < 16) {
+                    NEEDBITS(here.bits);
+                    DROPBITS(here.bits);
+                    state->lens[state->have++] = here.val;
                 }
                 else {
-                    if (this.val == 16) {
-                        NEEDBITS(this.bits + 2);
-                        DROPBITS(this.bits);
+                    if (here.val == 16) {
+                        NEEDBITS(here.bits + 2);
+                        DROPBITS(here.bits);
                         if (state->have == 0) {
                             strm->msg = (char *)"invalid bit length repeat";
                             state->mode = BAD;
@@ -411,16 +411,16 @@ void FAR *out_desc;
                         copy = 3 + BITS(2);
                         DROPBITS(2);
                     }
-                    else if (this.val == 17) {
-                        NEEDBITS(this.bits + 3);
-                        DROPBITS(this.bits);
+                    else if (here.val == 17) {
+                        NEEDBITS(here.bits + 3);
+                        DROPBITS(here.bits);
                         len = 0;
                         copy = 3 + BITS(3);
                         DROPBITS(3);
                     }
                     else {
-                        NEEDBITS(this.bits + 7);
-                        DROPBITS(this.bits);
+                        NEEDBITS(here.bits + 7);
+                        DROPBITS(here.bits);
                         len = 0;
                         copy = 11 + BITS(7);
                         DROPBITS(7);
@@ -438,7 +438,16 @@ void FAR *out_desc;
             /* handle error breaks in while */
             if (state->mode == BAD) break;
 
-            /* build code tables */
+            /* check for end-of-block code (better have one) */
+            if (state->lens[256] == 0) {
+                strm->msg = (char *)"invalid code -- missing end-of-block";
+                state->mode = BAD;
+                break;
+            }
+
+            /* build code tables -- note: do not change the lenbits or distbits
+               values here (9 and 6) without reading the comments in inftrees.h
+               concerning the ENOUGH constants, which depend on those values */
             state->next = state->codes;
             state->lencode = (code const FAR *)(state->next);
             state->lenbits = 9;
@@ -474,28 +483,28 @@ void FAR *out_desc;
 
             /* get a literal, length, or end-of-block code */
             for (;;) {
-                this = state->lencode[BITS(state->lenbits)];
-                if ((unsigned)(this.bits) <= bits) break;
+                here = state->lencode[BITS(state->lenbits)];
+                if ((unsigned)(here.bits) <= bits) break;
                 PULLBYTE();
             }
-            if (this.op && (this.op & 0xf0) == 0) {
-                last = this;
+            if (here.op && (here.op & 0xf0) == 0) {
+                last = here;
                 for (;;) {
-                    this = state->lencode[last.val +
+                    here = state->lencode[last.val +
                             (BITS(last.bits + last.op) >> last.bits)];
-                    if ((unsigned)(last.bits + this.bits) <= bits) break;
+                    if ((unsigned)(last.bits + here.bits) <= bits) break;
                     PULLBYTE();
                 }
                 DROPBITS(last.bits);
             }
-            DROPBITS(this.bits);
-            state->length = (unsigned)this.val;
+            DROPBITS(here.bits);
+            state->length = (unsigned)here.val;
 
             /* process literal */
-            if (this.op == 0) {
-                Tracevv((stderr, this.val >= 0x20 && this.val < 0x7f ?
+            if (here.op == 0) {
+                Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
                         "inflate:         literal '%c'\n" :
-                        "inflate:         literal 0x%02x\n", this.val));
+                        "inflate:         literal 0x%02x\n", here.val));
                 ROOM();
                 *put++ = (unsigned char)(state->length);
                 left--;
@@ -504,21 +513,21 @@ void FAR *out_desc;
             }
 
             /* process end of block */
-            if (this.op & 32) {
+            if (here.op & 32) {
                 Tracevv((stderr, "inflate:         end of block\n"));
                 state->mode = TYPE;
                 break;
             }
 
             /* invalid code */
-            if (this.op & 64) {
+            if (here.op & 64) {
                 strm->msg = (char *)"invalid literal/length code";
                 state->mode = BAD;
                 break;
             }
 
             /* length code -- get extra bits, if any */
-            state->extra = (unsigned)(this.op) & 15;
+            state->extra = (unsigned)(here.op) & 15;
             if (state->extra != 0) {
                 NEEDBITS(state->extra);
                 state->length += BITS(state->extra);
@@ -528,30 +537,30 @@ void FAR *out_desc;
 
             /* get distance code */
             for (;;) {
-                this = state->distcode[BITS(state->distbits)];
-                if ((unsigned)(this.bits) <= bits) break;
+                here = state->distcode[BITS(state->distbits)];
+                if ((unsigned)(here.bits) <= bits) break;
                 PULLBYTE();
             }
-            if ((this.op & 0xf0) == 0) {
-                last = this;
+            if ((here.op & 0xf0) == 0) {
+                last = here;
                 for (;;) {
-                    this = state->distcode[last.val +
+                    here = state->distcode[last.val +
                             (BITS(last.bits + last.op) >> last.bits)];
-                    if ((unsigned)(last.bits + this.bits) <= bits) break;
+                    if ((unsigned)(last.bits + here.bits) <= bits) break;
                     PULLBYTE();
                 }
                 DROPBITS(last.bits);
             }
-            DROPBITS(this.bits);
-            if (this.op & 64) {
+            DROPBITS(here.bits);
+            if (here.op & 64) {
                 strm->msg = (char *)"invalid distance code";
                 state->mode = BAD;
                 break;
             }
-            state->offset = (unsigned)this.val;
+            state->offset = (unsigned)here.val;
 
             /* get distance extra bits, if any */
-            state->extra = (unsigned)(this.op) & 15;
+            state->extra = (unsigned)(here.op) & 15;
             if (state->extra != 0) {
                 NEEDBITS(state->extra);
                 state->offset += BITS(state->extra);
index bbee92e..2f1d60b 100644 (file)
@@ -1,5 +1,5 @@
 /* inffast.c -- fast decoding
- * Copyright (C) 1995-2004 Mark Adler
+ * Copyright (C) 1995-2008, 2010 Mark Adler
  * For conditions of distribution and use, see copyright notice in zlib.h
  */
 
@@ -64,7 +64,7 @@
       requires strm->avail_out >= 258 for each loop to avoid checking for
       output space.
  */
-void inflate_fast(strm, start)
+void ZLIB_INTERNAL inflate_fast(strm, start)
 z_streamp strm;
 unsigned start;         /* inflate()'s starting value for strm->avail_out */
 {
@@ -79,7 +79,7 @@ unsigned start;         /* inflate()'s starting value for strm->avail_out */
 #endif
     unsigned wsize;             /* window size or zero if not using window */
     unsigned whave;             /* valid bytes in the window */
-    unsigned write;             /* window write index */
+    unsigned wnext;             /* window write index */
     unsigned char FAR *window;  /* allocated sliding window, if wsize != 0 */
     unsigned long hold;         /* local strm->hold */
     unsigned bits;              /* local strm->bits */
@@ -87,7 +87,7 @@ unsigned start;         /* inflate()'s starting value for strm->avail_out */
     code const FAR *dcode;      /* local strm->distcode */
     unsigned lmask;             /* mask for first level of length codes */
     unsigned dmask;             /* mask for first level of distance codes */
-    code this;                  /* retrieved table entry */
+    code here;                  /* retrieved table entry */
     unsigned op;                /* code bits, operation, extra bits, or */
                                 /*  window position, window bytes to copy */
     unsigned len;               /* match length, unused bytes */
@@ -106,7 +106,7 @@ unsigned start;         /* inflate()'s starting value for strm->avail_out */
 #endif
     wsize = state->wsize;
     whave = state->whave;
-    write = state->write;
+    wnext = state->wnext;
     window = state->window;
     hold = state->hold;
     bits = state->bits;
@@ -124,20 +124,20 @@ unsigned start;         /* inflate()'s starting value for strm->avail_out */
             hold += (unsigned long)(PUP(in)) << bits;
             bits += 8;
         }
-        this = lcode[hold & lmask];
+        here = lcode[hold & lmask];
       dolen:
-        op = (unsigned)(this.bits);
+        op = (unsigned)(here.bits);
         hold >>= op;
         bits -= op;
-        op = (unsigned)(this.op);
+        op = (unsigned)(here.op);
         if (op == 0) {                          /* literal */
-            Tracevv((stderr, this.val >= 0x20 && this.val < 0x7f ?
+            Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
                     "inflate:         literal '%c'\n" :
-                    "inflate:         literal 0x%02x\n", this.val));
-            PUP(out) = (unsigned char)(this.val);
+                    "inflate:         literal 0x%02x\n", here.val));
+            PUP(out) = (unsigned char)(here.val);
         }
         else if (op & 16) {                     /* length base */
-            len = (unsigned)(this.val);
+            len = (unsigned)(here.val);
             op &= 15;                           /* number of extra bits */
             if (op) {
                 if (bits < op) {
@@ -155,14 +155,14 @@ unsigned start;         /* inflate()'s starting value for strm->avail_out */
                 hold += (unsigned long)(PUP(in)) << bits;
                 bits += 8;
             }
-            this = dcode[hold & dmask];
+            here = dcode[hold & dmask];
           dodist:
-            op = (unsigned)(this.bits);
+            op = (unsigned)(here.bits);
             hold >>= op;
             bits -= op;
-            op = (unsigned)(this.op);
+            op = (unsigned)(here.op);
             if (op & 16) {                      /* distance base */
-                dist = (unsigned)(this.val);
+                dist = (unsigned)(here.val);
                 op &= 15;                       /* number of extra bits */
                 if (bits < op) {
                     hold += (unsigned long)(PUP(in)) << bits;
@@ -187,12 +187,34 @@ unsigned start;         /* inflate()'s starting value for strm->avail_out */
                 if (dist > op) {                /* see if copy from window */
                     op = dist - op;             /* distance back in window */
                     if (op > whave) {
-                        strm->msg = (char *)"invalid distance too far back";
-                        state->mode = BAD;
-                        break;
+                        if (state->sane) {
+                            strm->msg =
+                                (char *)"invalid distance too far back";
+                            state->mode = BAD;
+                            break;
+                        }
+#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
+                        if (len <= op - whave) {
+                            do {
+                                PUP(out) = 0;
+                            } while (--len);
+                            continue;
+                        }
+                        len -= op - whave;
+                        do {
+                            PUP(out) = 0;
+                        } while (--op > whave);
+                        if (op == 0) {
+                            from = out - dist;
+                            do {
+                                PUP(out) = PUP(from);
+                            } while (--len);
+                            continue;
+                        }
+#endif
                     }
                     from = window - OFF;
-                    if (write == 0) {           /* very common case */
+                    if (wnext == 0) {           /* very common case */
                         from += wsize - op;
                         if (op < len) {         /* some from window */
                             len -= op;
@@ -202,17 +224,17 @@ unsigned start;         /* inflate()'s starting value for strm->avail_out */
                             from = out - dist;  /* rest from output */
                         }
                     }
-                    else if (write < op) {      /* wrap around window */
-                        from += wsize + write - op;
-                        op -= write;
+                    else if (wnext < op) {      /* wrap around window */
+                        from += wsize + wnext - op;
+                        op -= wnext;
                         if (op < len) {         /* some from end of window */
                             len -= op;
                             do {
                                 PUP(out) = PUP(from);
                             } while (--op);
                             from = window - OFF;
-                            if (write < len) {  /* some from start of window */
-                                op = write;
+                            if (wnext < len) {  /* some from start of window */
+                                op = wnext;
                                 len -= op;
                                 do {
                                     PUP(out) = PUP(from);
@@ -222,7 +244,7 @@ unsigned start;         /* inflate()'s starting value for strm->avail_out */
                         }
                     }
                     else {                      /* contiguous in window */
-                        from += write - op;
+                        from += wnext - op;
                         if (op < len) {         /* some from window */
                             len -= op;
                             do {
@@ -259,7 +281,7 @@ unsigned start;         /* inflate()'s starting value for strm->avail_out */
                 }
             }
             else if ((op & 64) == 0) {          /* 2nd level distance code */
-                this = dcode[this.val + (hold & ((1U << op) - 1))];
+                here = dcode[here.val + (hold & ((1U << op) - 1))];
                 goto dodist;
             }
             else {
@@ -269,7 +291,7 @@ unsigned start;         /* inflate()'s starting value for strm->avail_out */
             }
         }
         else if ((op & 64) == 0) {              /* 2nd level length code */
-            this = lcode[this.val + (hold & ((1U << op) - 1))];
+            here = lcode[here.val + (hold & ((1U << op) - 1))];
             goto dolen;
         }
         else if (op & 32) {                     /* end-of-block */
@@ -305,7 +327,7 @@ unsigned start;         /* inflate()'s starting value for strm->avail_out */
    inflate_fast() speedups that turned out slower (on a PowerPC G3 750CXe):
    - Using bit fields for code structure
    - Different op definition to avoid & for extra bits (do & for table bits)
-   - Three separate decoding do-loops for direct, window, and write == 0
+   - Three separate decoding do-loops for direct, window, and wnext == 0
    - Special case for distance > 1 copies to do overlapped load and store copy
    - Explicit branch predictions (based on measured branch probabilities)
    - Deferring match copy and interspersed it with decoding subsequent codes
index 1e88d2d..e5c1aa4 100644 (file)
@@ -1,5 +1,5 @@
 /* inffast.h -- header to use inffast.c
- * Copyright (C) 1995-2003 Mark Adler
+ * Copyright (C) 1995-2003, 2010 Mark Adler
  * For conditions of distribution and use, see copyright notice in zlib.h
  */
 
@@ -8,4 +8,4 @@
    subject to change. Applications should only use zlib.h.
  */
 
-void inflate_fast OF((z_streamp strm, unsigned start));
+void ZLIB_INTERNAL inflate_fast OF((z_streamp strm, unsigned start));
index 792fdee..a8431ab 100644 (file)
@@ -1,5 +1,5 @@
 /* inflate.c -- zlib decompression
- * Copyright (C) 1995-2005 Mark Adler
+ * Copyright (C) 1995-2010 Mark Adler
  * For conditions of distribution and use, see copyright notice in zlib.h
  */
 
@@ -45,7 +45,7 @@
  * - Rearrange window copies in inflate_fast() for speed and simplification
  * - Unroll last copy for window match in inflate_fast()
  * - Use local copies of window variables in inflate_fast() for speed
- * - Pull out common write == 0 case for speed in inflate_fast()
+ * - Pull out common wnext == 0 case for speed in inflate_fast()
  * - Make op and len in inflate_fast() unsigned for consistency
  * - Add FAR to lcode and dcode declarations in inflate_fast()
  * - Simplified bad distance check in inflate_fast()
@@ -117,28 +117,52 @@ z_streamp strm;
     state->head = Z_NULL;
     state->wsize = 0;
     state->whave = 0;
-    state->write = 0;
+    state->wnext = 0;
     state->hold = 0;
     state->bits = 0;
     state->lencode = state->distcode = state->next = state->codes;
+    state->sane = 1;
+    state->back = -1;
     Tracev((stderr, "inflate: reset\n"));
     return Z_OK;
 }
 
-int ZEXPORT inflatePrime(strm, bits, value)
+int ZEXPORT inflateReset2(strm, windowBits)
 z_streamp strm;
-int bits;
-int value;
+int windowBits;
 {
+    int wrap;
     struct inflate_state FAR *state;
 
+    /* get the state */
     if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
     state = (struct inflate_state FAR *)strm->state;
-    if (bits > 16 || state->bits + bits > 32) return Z_STREAM_ERROR;
-    value &= (1L << bits) - 1;
-    state->hold += value << state->bits;
-    state->bits += bits;
-    return Z_OK;
+
+    /* extract wrap request from windowBits parameter */
+    if (windowBits < 0) {
+        wrap = 0;
+        windowBits = -windowBits;
+    }
+    else {
+        wrap = (windowBits >> 4) + 1;
+#ifdef GUNZIP
+        if (windowBits < 48)
+            windowBits &= 15;
+#endif
+    }
+
+    /* set number of window bits, free window if different */
+    if (windowBits && (windowBits < 8 || windowBits > 15))
+        return Z_STREAM_ERROR;
+    if (state->window != Z_NULL && state->wbits != (unsigned)windowBits) {
+        ZFREE(strm, state->window);
+        state->window = Z_NULL;
+    }
+
+    /* update state and reset the rest of it */
+    state->wrap = wrap;
+    state->wbits = (unsigned)windowBits;
+    return inflateReset(strm);
 }
 
 int ZEXPORT inflateInit2_(strm, windowBits, version, stream_size)
@@ -147,6 +171,7 @@ int windowBits;
 const char *version;
 int stream_size;
 {
+    int ret;
     struct inflate_state FAR *state;
 
     if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
@@ -164,24 +189,13 @@ int stream_size;
     if (state == Z_NULL) return Z_MEM_ERROR;
     Tracev((stderr, "inflate: allocated\n"));
     strm->state = (struct internal_state FAR *)state;
-    if (windowBits < 0) {
-        state->wrap = 0;
-        windowBits = -windowBits;
-    }
-    else {
-        state->wrap = (windowBits >> 4) + 1;
-#ifdef GUNZIP
-        if (windowBits < 48) windowBits &= 15;
-#endif
-    }
-    if (windowBits < 8 || windowBits > 15) {
+    state->window = Z_NULL;
+    ret = inflateReset2(strm, windowBits);
+    if (ret != Z_OK) {
         ZFREE(strm, state);
         strm->state = Z_NULL;
-        return Z_STREAM_ERROR;
     }
-    state->wbits = (unsigned)windowBits;
-    state->window = Z_NULL;
-    return inflateReset(strm);
+    return ret;
 }
 
 int ZEXPORT inflateInit_(strm, version, stream_size)
@@ -192,6 +206,27 @@ int stream_size;
     return inflateInit2_(strm, DEF_WBITS, version, stream_size);
 }
 
+int ZEXPORT inflatePrime(strm, bits, value)
+z_streamp strm;
+int bits;
+int value;
+{
+    struct inflate_state FAR *state;
+
+    if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
+    state = (struct inflate_state FAR *)strm->state;
+    if (bits < 0) {
+        state->hold = 0;
+        state->bits = 0;
+        return Z_OK;
+    }
+    if (bits > 16 || state->bits + bits > 32) return Z_STREAM_ERROR;
+    value &= (1L << bits) - 1;
+    state->hold += value << state->bits;
+    state->bits += bits;
+    return Z_OK;
+}
+
 /*
    Return state with length and distance decoding tables and index sizes set to
    fixed code decoding.  Normally this returns fixed tables from inffixed.h.
@@ -340,7 +375,7 @@ unsigned out;
     /* if window not in use yet, initialize */
     if (state->wsize == 0) {
         state->wsize = 1U << state->wbits;
-        state->write = 0;
+        state->wnext = 0;
         state->whave = 0;
     }
 
@@ -348,22 +383,22 @@ unsigned out;
     copy = out - strm->avail_out;
     if (copy >= state->wsize) {
         zmemcpy(state->window, strm->next_out - state->wsize, state->wsize);
-        state->write = 0;
+        state->wnext = 0;
         state->whave = state->wsize;
     }
     else {
-        dist = state->wsize - state->write;
+        dist = state->wsize - state->wnext;
         if (dist > copy) dist = copy;
-        zmemcpy(state->window + state->write, strm->next_out - copy, dist);
+        zmemcpy(state->window + state->wnext, strm->next_out - copy, dist);
         copy -= dist;
         if (copy) {
             zmemcpy(state->window, strm->next_out - copy, copy);
-            state->write = copy;
+            state->wnext = copy;
             state->whave = state->wsize;
         }
         else {
-            state->write += dist;
-            if (state->write == state->wsize) state->write = 0;
+            state->wnext += dist;
+            if (state->wnext == state->wsize) state->wnext = 0;
             if (state->whave < state->wsize) state->whave += dist;
         }
     }
@@ -564,7 +599,7 @@ int flush;
     unsigned in, out;           /* save starting available input and output */
     unsigned copy;              /* number of stored or match bytes to copy */
     unsigned char FAR *from;    /* where to copy match bytes from */
-    code this;                  /* current decoding table entry */
+    code here;                  /* current decoding table entry */
     code last;                  /* parent table entry */
     unsigned len;               /* length to copy for repeats, bits to drop */
     int ret;                    /* return code */
@@ -619,7 +654,9 @@ int flush;
             }
             DROPBITS(4);
             len = BITS(4) + 8;
-            if (len > state->wbits) {
+            if (state->wbits == 0)
+                state->wbits = len;
+            else if (len > state->wbits) {
                 strm->msg = (char *)"invalid window size";
                 state->mode = BAD;
                 break;
@@ -771,7 +808,7 @@ int flush;
             strm->adler = state->check = adler32(0L, Z_NULL, 0);
             state->mode = TYPE;
         case TYPE:
-            if (flush == Z_BLOCK) goto inf_leave;
+            if (flush == Z_BLOCK || flush == Z_TREES) goto inf_leave;
         case TYPEDO:
             if (state->last) {
                 BYTEBITS();
@@ -791,7 +828,11 @@ int flush;
                 fixedtables(state);
                 Tracev((stderr, "inflate:     fixed codes block%s\n",
                         state->last ? " (last)" : ""));
-                state->mode = LEN;              /* decode codes */
+                state->mode = LEN_;             /* decode codes */
+                if (flush == Z_TREES) {
+                    DROPBITS(2);
+                    goto inf_leave;
+                }
                 break;
             case 2:                             /* dynamic block */
                 Tracev((stderr, "inflate:     dynamic codes block%s\n",
@@ -816,6 +857,9 @@ int flush;
             Tracev((stderr, "inflate:       stored length %u\n",
                     state->length));
             INITBITS();
+            state->mode = COPY_;
+            if (flush == Z_TREES) goto inf_leave;
+        case COPY_:
             state->mode = COPY;
         case COPY:
             copy = state->length;
@@ -876,19 +920,19 @@ int flush;
         case CODELENS:
             while (state->have < state->nlen + state->ndist) {
                 for (;;) {
-                    this = state->lencode[BITS(state->lenbits)];
-                    if ((unsigned)(this.bits) <= bits) break;
+                    here = state->lencode[BITS(state->lenbits)];
+                    if ((unsigned)(here.bits) <= bits) break;
                     PULLBYTE();
                 }
-                if (this.val < 16) {
-                    NEEDBITS(this.bits);
-                    DROPBITS(this.bits);
-                    state->lens[state->have++] = this.val;
+                if (here.val < 16) {
+                    NEEDBITS(here.bits);
+                    DROPBITS(here.bits);
+                    state->lens[state->have++] = here.val;
                 }
                 else {
-                    if (this.val == 16) {
-                        NEEDBITS(this.bits + 2);
-                        DROPBITS(this.bits);
+                    if (here.val == 16) {
+                        NEEDBITS(here.bits + 2);
+                        DROPBITS(here.bits);
                         if (state->have == 0) {
                             strm->msg = (char *)"invalid bit length repeat";
                             state->mode = BAD;
@@ -898,16 +942,16 @@ int flush;
                         copy = 3 + BITS(2);
                         DROPBITS(2);
                     }
-                    else if (this.val == 17) {
-                        NEEDBITS(this.bits + 3);
-                        DROPBITS(this.bits);
+                    else if (here.val == 17) {
+                        NEEDBITS(here.bits + 3);
+                        DROPBITS(here.bits);
                         len = 0;
                         copy = 3 + BITS(3);
                         DROPBITS(3);
                     }
                     else {
-                        NEEDBITS(this.bits + 7);
-                        DROPBITS(this.bits);
+                        NEEDBITS(here.bits + 7);
+                        DROPBITS(here.bits);
                         len = 0;
                         copy = 11 + BITS(7);
                         DROPBITS(7);
@@ -925,7 +969,16 @@ int flush;
             /* handle error breaks in while */
             if (state->mode == BAD) break;
 
-            /* build code tables */
+            /* check for end-of-block code (better have one) */
+            if (state->lens[256] == 0) {
+                strm->msg = (char *)"invalid code -- missing end-of-block";
+                state->mode = BAD;
+                break;
+            }
+
+            /* build code tables -- note: do not change the lenbits or distbits
+               values here (9 and 6) without reading the comments in inftrees.h
+               concerning the ENOUGH constants, which depend on those values */
             state->next = state->codes;
             state->lencode = (code const FAR *)(state->next);
             state->lenbits = 9;
@@ -946,88 +999,102 @@ int flush;
                 break;
             }
             Tracev((stderr, "inflate:       codes ok\n"));
+            state->mode = LEN_;
+            if (flush == Z_TREES) goto inf_leave;
+        case LEN_:
             state->mode = LEN;
         case LEN:
             if (have >= 6 && left >= 258) {
                 RESTORE();
                 inflate_fast(strm, out);
                 LOAD();
+                if (state->mode == TYPE)
+                    state->back = -1;
                 break;
             }
+            state->back = 0;
             for (;;) {
-                this = state->lencode[BITS(state->lenbits)];
-                if ((unsigned)(this.bits) <= bits) break;
+                here = state->lencode[BITS(state->lenbits)];
+                if ((unsigned)(here.bits) <= bits) break;
                 PULLBYTE();
             }
-            if (this.op && (this.op & 0xf0) == 0) {
-                last = this;
+            if (here.op && (here.op & 0xf0) == 0) {
+                last = here;
                 for (;;) {
-                    this = state->lencode[last.val +
+                    here = state->lencode[last.val +
                             (BITS(last.bits + last.op) >> last.bits)];
-                    if ((unsigned)(last.bits + this.bits) <= bits) break;
+                    if ((unsigned)(last.bits + here.bits) <= bits) break;
                     PULLBYTE();
                 }
                 DROPBITS(last.bits);
+                state->back += last.bits;
             }
-            DROPBITS(this.bits);
-            state->length = (unsigned)this.val;
-            if ((int)(this.op) == 0) {
-                Tracevv((stderr, this.val >= 0x20 && this.val < 0x7f ?
+            DROPBITS(here.bits);
+            state->back += here.bits;
+            state->length = (unsigned)here.val;
+            if ((int)(here.op) == 0) {
+                Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
                         "inflate:         literal '%c'\n" :
-                        "inflate:         literal 0x%02x\n", this.val));
+                        "inflate:         literal 0x%02x\n", here.val));
                 state->mode = LIT;
                 break;
             }
-            if (this.op & 32) {
+            if (here.op & 32) {
                 Tracevv((stderr, "inflate:         end of block\n"));
+                state->back = -1;
                 state->mode = TYPE;
                 break;
             }
-            if (this.op & 64) {
+            if (here.op & 64) {
                 strm->msg = (char *)"invalid literal/length code";
                 state->mode = BAD;
                 break;
             }
-            state->extra = (unsigned)(this.op) & 15;
+            state->extra = (unsigned)(here.op) & 15;
             state->mode = LENEXT;
         case LENEXT:
             if (state->extra) {
                 NEEDBITS(state->extra);
                 state->length += BITS(state->extra);
                 DROPBITS(state->extra);
+                state->back += state->extra;
             }
             Tracevv((stderr, "inflate:         length %u\n", state->length));
+            state->was = state->length;
             state->mode = DIST;
         case DIST:
             for (;;) {
-                this = state->distcode[BITS(state->distbits)];
-                if ((unsigned)(this.bits) <= bits) break;
+                here = state->distcode[BITS(state->distbits)];
+                if ((unsigned)(here.bits) <= bits) break;
                 PULLBYTE();
             }
-            if ((this.op & 0xf0) == 0) {
-                last = this;
+            if ((here.op & 0xf0) == 0) {
+                last = here;
                 for (;;) {
-                    this = state->distcode[last.val +
+                    here = state->distcode[last.val +
                             (BITS(last.bits + last.op) >> last.bits)];
-                    if ((unsigned)(last.bits + this.bits) <= bits) break;
+                    if ((unsigned)(last.bits + here.bits) <= bits) break;
                     PULLBYTE();
                 }
                 DROPBITS(last.bits);
+                state->back += last.bits;
             }
-            DROPBITS(this.bits);
-            if (this.op & 64) {
+            DROPBITS(here.bits);
+            state->back += here.bits;
+            if (here.op & 64) {
                 strm->msg = (char *)"invalid distance code";
                 state->mode = BAD;
                 break;
             }
-            state->offset = (unsigned)this.val;
-            state->extra = (unsigned)(this.op) & 15;
+            state->offset = (unsigned)here.val;
+            state->extra = (unsigned)(here.op) & 15;
             state->mode = DISTEXT;
         case DISTEXT:
             if (state->extra) {
                 NEEDBITS(state->extra);
                 state->offset += BITS(state->extra);
                 DROPBITS(state->extra);
+                state->back += state->extra;
             }
 #ifdef INFLATE_STRICT
             if (state->offset > state->dmax) {
@@ -1036,11 +1103,6 @@ int flush;
                 break;
             }
 #endif
-            if (state->offset > state->whave + out - left) {
-                strm->msg = (char *)"invalid distance too far back";
-                state->mode = BAD;
-                break;
-            }
             Tracevv((stderr, "inflate:         distance %u\n", state->offset));
             state->mode = MATCH;
         case MATCH:
@@ -1048,12 +1110,32 @@ int flush;
             copy = out - left;
             if (state->offset > copy) {         /* copy from window */
                 copy = state->offset - copy;
-                if (copy > state->write) {
-                    copy -= state->write;
+                if (copy > state->whave) {
+                    if (state->sane) {
+                        strm->msg = (char *)"invalid distance too far back";
+                        state->mode = BAD;
+                        break;
+                    }
+#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
+                    Trace((stderr, "inflate.c too far\n"));
+                    copy -= state->whave;
+                    if (copy > state->length) copy = state->length;
+                    if (copy > left) copy = left;
+                    left -= copy;
+                    state->length -= copy;
+                    do {
+                        *put++ = 0;
+                    } while (--copy);
+                    if (state->length == 0) state->mode = LEN;
+                    break;
+#endif
+                }
+                if (copy > state->wnext) {
+                    copy -= state->wnext;
                     from = state->window + (state->wsize - copy);
                 }
                 else
-                    from = state->window + (state->write - copy);
+                    from = state->window + (state->wnext - copy);
                 if (copy > state->length) copy = state->length;
             }
             else {                              /* copy from output */
@@ -1146,7 +1228,8 @@ int flush;
         strm->adler = state->check =
             UPDATE(state->check, strm->next_out - out, out);
     strm->data_type = state->bits + (state->last ? 64 : 0) +
-                      (state->mode == TYPE ? 128 : 0);
+                      (state->mode == TYPE ? 128 : 0) +
+                      (state->mode == LEN_ || state->mode == COPY_ ? 256 : 0);
     if (((in == 0 && out == 0) || flush == Z_FINISH) && ret == Z_OK)
         ret = Z_BUF_ERROR;
     return ret;
@@ -1366,3 +1449,32 @@ z_streamp source;
     dest->state = (struct internal_state FAR *)copy;
     return Z_OK;
 }
+
+int ZEXPORT inflateUndermine(strm, subvert)
+z_streamp strm;
+int subvert;
+{
+    struct inflate_state FAR *state;
+
+    if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
+    state = (struct inflate_state FAR *)strm->state;
+    state->sane = !subvert;
+#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
+    return Z_OK;
+#else
+    state->sane = 1;
+    return Z_DATA_ERROR;
+#endif
+}
+
+long ZEXPORT inflateMark(strm)
+z_streamp strm;
+{
+    struct inflate_state FAR *state;
+
+    if (strm == Z_NULL || strm->state == Z_NULL) return -1L << 16;
+    state = (struct inflate_state FAR *)strm->state;
+    return ((long)(state->back) << 16) +
+        (state->mode == COPY ? state->length :
+            (state->mode == MATCH ? state->was - state->length : 0));
+}
index 07bd3e7..95f4986 100644 (file)
@@ -1,5 +1,5 @@
 /* inflate.h -- internal inflate state definition
- * Copyright (C) 1995-2004 Mark Adler
+ * Copyright (C) 1995-2009 Mark Adler
  * For conditions of distribution and use, see copyright notice in zlib.h
  */
 
@@ -32,11 +32,13 @@ typedef enum {
         TYPE,       /* i: waiting for type bits, including last-flag bit */
         TYPEDO,     /* i: same, but skip check to exit inflate on new block */
         STORED,     /* i: waiting for stored size (length and complement) */
+        COPY_,      /* i/o: same as COPY below, but only first time in */
         COPY,       /* i/o: waiting for input or output to copy stored block */
         TABLE,      /* i: waiting for dynamic block table lengths */
         LENLENS,    /* i: waiting for code length code lengths */
         CODELENS,   /* i: waiting for length/lit and distance code lengths */
-            LEN,        /* i: waiting for length/lit code */
+            LEN_,       /* i: same as LEN below, but only first time in */
+            LEN,        /* i: waiting for length/lit/eob code */
             LENEXT,     /* i: waiting for length extra bits */
             DIST,       /* i: waiting for distance code */
             DISTEXT,    /* i: waiting for distance extra bits */
@@ -53,19 +55,21 @@ typedef enum {
 /*
     State transitions between above modes -
 
-    (most modes can go to the BAD or MEM mode -- not shown for clarity)
+    (most modes can go to BAD or MEM on error -- not shown for clarity)
 
     Process header:
-        HEAD -> (gzip) or (zlib)
-        (gzip) -> FLAGS -> TIME -> OS -> EXLEN -> EXTRA -> NAME
-        NAME -> COMMENT -> HCRC -> TYPE
+        HEAD -> (gzip) or (zlib) or (raw)
+        (gzip) -> FLAGS -> TIME -> OS -> EXLEN -> EXTRA -> NAME -> COMMENT ->
+                  HCRC -> TYPE
         (zlib) -> DICTID or TYPE
         DICTID -> DICT -> TYPE
+        (raw) -> TYPEDO
     Read deflate blocks:
-            TYPE -> STORED or TABLE or LEN or CHECK
-            STORED -> COPY -> TYPE
-            TABLE -> LENLENS -> CODELENS -> LEN
-    Read deflate codes:
+            TYPE -> TYPEDO -> STORED or TABLE or LEN_ or CHECK
+            STORED -> COPY_ -> COPY -> TYPE
+            TABLE -> LENLENS -> CODELENS -> LEN_
+            LEN_ -> LEN
+    Read deflate codes in fixed or dynamic block:
                 LEN -> LENEXT or LIT or TYPE
                 LENEXT -> DIST -> DISTEXT -> MATCH -> LEN
                 LIT -> LEN
@@ -73,7 +77,7 @@ typedef enum {
         CHECK -> LENGTH -> DONE
  */
 
-/* state maintained between inflate() calls.  Approximately 7K bytes. */
+/* state maintained between inflate() calls.  Approximately 10K bytes. */
 struct inflate_state {
     inflate_mode mode;          /* current inflate mode */
     int last;                   /* true if processing last block */
@@ -88,7 +92,7 @@ struct inflate_state {
     unsigned wbits;             /* log base 2 of requested window size */
     unsigned wsize;             /* window size or zero if not using window */
     unsigned whave;             /* valid bytes in the window */
-    unsigned write;             /* window write index */
+    unsigned wnext;             /* window write index */
     unsigned char FAR *window;  /* allocated sliding window, if needed */
         /* bit accumulator */
     unsigned long hold;         /* input bit accumulator */
@@ -112,4 +116,7 @@ struct inflate_state {
     unsigned short lens[320];   /* temporary storage for code lengths */
     unsigned short work[288];   /* work area for code table building */
     code codes[ENOUGH];         /* space for code tables */
+    int sane;                   /* if false, allow invalid distance too far */
+    int back;                   /* bits back of last unprocessed length/lit */
+    unsigned was;               /* initial length of match */
 };
index 8a9c13f..11e9c52 100644 (file)
@@ -1,5 +1,5 @@
 /* inftrees.c -- generate Huffman trees for efficient decoding
- * Copyright (C) 1995-2005 Mark Adler
+ * Copyright (C) 1995-2010 Mark Adler
  * For conditions of distribution and use, see copyright notice in zlib.h
  */
 
@@ -9,7 +9,7 @@
 #define MAXBITS 15
 
 const char inflate_copyright[] =
-   " inflate 1.2.3 Copyright 1995-2005 Mark Adler ";
+   " inflate 1.2.5 Copyright 1995-2010 Mark Adler ";
 /*
   If you use the zlib library in a product, an acknowledgment is welcome
   in the documentation of your product. If for some reason you cannot
@@ -29,7 +29,7 @@ const char inflate_copyright[] =
    table index bits.  It will differ if the request is greater than the
    longest code or if it is less than the shortest code.
  */
-int inflate_table(type, lens, codes, table, bits, work)
+int ZLIB_INTERNAL inflate_table(type, lens, codes, table, bits, work)
 codetype type;
 unsigned short FAR *lens;
 unsigned codes;
@@ -50,7 +50,7 @@ unsigned short FAR *work;
     unsigned fill;              /* index for replicating entries */
     unsigned low;               /* low bits for current root entry */
     unsigned mask;              /* mask for low root bits */
-    code this;                  /* table entry for duplication */
+    code here;                  /* table entry for duplication */
     code FAR *next;             /* next available space in table */
     const unsigned short FAR *base;     /* base value table to use */
     const unsigned short FAR *extra;    /* extra bits table to use */
@@ -62,7 +62,7 @@ unsigned short FAR *work;
         35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
     static const unsigned short lext[31] = { /* Length codes 257..285 extra */
         16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18,
-        19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 16, 201, 196};
+        19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 16, 73, 195};
     static const unsigned short dbase[32] = { /* Distance codes 0..29 base */
         1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
         257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
@@ -115,15 +115,15 @@ unsigned short FAR *work;
         if (count[max] != 0) break;
     if (root > max) root = max;
     if (max == 0) {                     /* no symbols to code at all */
-        this.op = (unsigned char)64;    /* invalid code marker */
-        this.bits = (unsigned char)1;
-        this.val = (unsigned short)0;
-        *(*table)++ = this;             /* make a table to force an error */
-        *(*table)++ = this;
+        here.op = (unsigned char)64;    /* invalid code marker */
+        here.bits = (unsigned char)1;
+        here.val = (unsigned short)0;
+        *(*table)++ = here;             /* make a table to force an error */
+        *(*table)++ = here;
         *bits = 1;
         return 0;     /* no symbols, but wait for decoding to report error */
     }
-    for (min = 1; min <= MAXBITS; min++)
+    for (min = 1; min < max; min++)
         if (count[min] != 0) break;
     if (root < min) root = min;
 
@@ -166,11 +166,10 @@ unsigned short FAR *work;
        entered in the tables.
 
        used keeps track of how many table entries have been allocated from the
-       provided *table space.  It is checked when a LENS table is being made
-       against the space in *table, ENOUGH, minus the maximum space needed by
-       the worst case distance code, MAXD.  This should never happen, but the
-       sufficiency of ENOUGH has not been proven exhaustively, hence the check.
-       This assumes that when type == LENS, bits == 9.
+       provided *table space.  It is checked for LENS and DIST tables against
+       the constants ENOUGH_LENS and ENOUGH_DISTS to guard against changes in
+       the initial root table size constants.  See the comments in inftrees.h
+       for more information.
 
        sym increments through all symbols, and the loop terminates when
        all codes of length max, i.e. all codes, have been processed.  This
@@ -209,24 +208,25 @@ unsigned short FAR *work;
     mask = used - 1;            /* mask for comparing low */
 
     /* check available table space */
-    if (type == LENS && used >= ENOUGH - MAXD)
+    if ((type == LENS && used >= ENOUGH_LENS) ||
+        (type == DISTS && used >= ENOUGH_DISTS))
         return 1;
 
     /* process all codes and make table entries */
     for (;;) {
         /* create table entry */
-        this.bits = (unsigned char)(len - drop);
+        here.bits = (unsigned char)(len - drop);
         if ((int)(work[sym]) < end) {
-            this.op = (unsigned char)0;
-            this.val = work[sym];
+            here.op = (unsigned char)0;
+            here.val = work[sym];
         }
         else if ((int)(work[sym]) > end) {
-            this.op = (unsigned char)(extra[work[sym]]);
-            this.val = base[work[sym]];
+            here.op = (unsigned char)(extra[work[sym]]);
+            here.val = base[work[sym]];
         }
         else {
-            this.op = (unsigned char)(32 + 64);         /* end of block */
-            this.val = 0;
+            here.op = (unsigned char)(32 + 64);         /* end of block */
+            here.val = 0;
         }
 
         /* replicate for those indices with low len bits equal to huff */
@@ -235,7 +235,7 @@ unsigned short FAR *work;
         min = fill;                 /* save offset to next table */
         do {
             fill -= incr;
-            next[(huff >> drop) + fill] = this;
+            next[(huff >> drop) + fill] = here;
         } while (fill != 0);
 
         /* backwards increment the len-bit code huff */
@@ -277,7 +277,8 @@ unsigned short FAR *work;
 
             /* check for enough space */
             used += 1U << curr;
-            if (type == LENS && used >= ENOUGH - MAXD)
+            if ((type == LENS && used >= ENOUGH_LENS) ||
+                (type == DISTS && used >= ENOUGH_DISTS))
                 return 1;
 
             /* point entry in root table to sub-table */
@@ -295,20 +296,20 @@ unsigned short FAR *work;
        through high index bits.  When the current sub-table is filled, the loop
        drops back to the root table to fill in any remaining entries there.
      */
-    this.op = (unsigned char)64;                /* invalid code marker */
-    this.bits = (unsigned char)(len - drop);
-    this.val = (unsigned short)0;
+    here.op = (unsigned char)64;                /* invalid code marker */
+    here.bits = (unsigned char)(len - drop);
+    here.val = (unsigned short)0;
     while (huff != 0) {
         /* when done with sub-table, drop back to root table */
         if (drop != 0 && (huff & mask) != low) {
             drop = 0;
             len = root;
             next = *table;
-            this.bits = (unsigned char)len;
+            here.bits = (unsigned char)len;
         }
 
         /* put invalid code marker in table */
-        next[huff >> drop] = this;
+        next[huff >> drop] = here;
 
         /* backwards increment the len-bit code huff */
         incr = 1U << (len - 1);
index b1104c8..baa53a0 100644 (file)
@@ -1,5 +1,5 @@
 /* inftrees.h -- header to use inftrees.c
- * Copyright (C) 1995-2005 Mark Adler
+ * Copyright (C) 1995-2005, 2010 Mark Adler
  * For conditions of distribution and use, see copyright notice in zlib.h
  */
 
@@ -35,21 +35,28 @@ typedef struct {
     01000000 - invalid code
  */
 
-/* Maximum size of dynamic tree.  The maximum found in a long but non-
-   exhaustive search was 1444 code structures (852 for length/literals
-   and 592 for distances, the latter actually the result of an
-   exhaustive search).  The true maximum is not known, but the value
-   below is more than safe. */
-#define ENOUGH 2048
-#define MAXD 592
+/* Maximum size of the dynamic table.  The maximum number of code structures is
+   1444, which is the sum of 852 for literal/length codes and 592 for distance
+   codes.  These values were found by exhaustive searches using the program
+   examples/enough.c found in the zlib distribtution.  The arguments to that
+   program are the number of symbols, the initial root table size, and the
+   maximum bit length of a code.  "enough 286 9 15" for literal/length codes
+   returns returns 852, and "enough 30 6 15" for distance codes returns 592.
+   The initial root table size (9 or 6) is found in the fifth argument of the
+   inflate_table() calls in inflate.c and infback.c.  If the root table size is
+   changed, then these maximum sizes would be need to be recalculated and
+   updated. */
+#define ENOUGH_LENS 852
+#define ENOUGH_DISTS 592
+#define ENOUGH (ENOUGH_LENS+ENOUGH_DISTS)
 
-/* Type of code to build for inftable() */
+/* Type of code to build for inflate_table() */
 typedef enum {
     CODES,
     LENS,
     DISTS
 } codetype;
 
-extern int inflate_table OF((codetype type, unsigned short FAR *lens,
+int ZLIB_INTERNAL inflate_table OF((codetype type, unsigned short FAR *lens,
                              unsigned codes, code FAR * FAR *table,
                              unsigned FAR *bits, unsigned short FAR *work));
index 7bcc2c8..56e9bb1 100644 (file)
@@ -1,5 +1,6 @@
 /* trees.c -- output deflated data using Huffman coding
- * Copyright (C) 1995-2005 Jean-loup Gailly
+ * Copyright (C) 1995-2010 Jean-loup Gailly
+ * detect_data_type() function provided freely by Cosmin Truta, 2006
  * For conditions of distribution and use, see copyright notice in zlib.h
  */
 
@@ -29,7 +30,7 @@
  *          Addison-Wesley, 1983. ISBN 0-201-06672-6.
  */
 
-/* @(#) $Id: trees.c,v 1.2 2008-05-26 19:08:10 vp153 Exp $ */
+/* @(#) $Id$ */
 
 /* #define GEN_TREES_H */
 
@@ -152,7 +153,7 @@ local void send_all_trees OF((deflate_state *s, int lcodes, int dcodes,
                               int blcodes));
 local void compress_block OF((deflate_state *s, ct_data *ltree,
                               ct_data *dtree));
-local void set_data_type  OF((deflate_state *s));
+local int  detect_data_type OF((deflate_state *s));
 local unsigned bi_reverse OF((unsigned value, int length));
 local void bi_windup      OF((deflate_state *s));
 local void bi_flush       OF((deflate_state *s));
@@ -203,12 +204,12 @@ local void send_bits(s, value, length)
      * unused bits in value.
      */
     if (s->bi_valid > (int)Buf_size - length) {
-        s->bi_buf |= (value << s->bi_valid);
+        s->bi_buf |= (ush)value << s->bi_valid;
         put_short(s, s->bi_buf);
         s->bi_buf = (ush)value >> (Buf_size - s->bi_valid);
         s->bi_valid += length - Buf_size;
     } else {
-        s->bi_buf |= value << s->bi_valid;
+        s->bi_buf |= (ush)value << s->bi_valid;
         s->bi_valid += length;
     }
 }
@@ -218,12 +219,12 @@ local void send_bits(s, value, length)
 { int len = length;\
   if (s->bi_valid > (int)Buf_size - len) {\
     int val = value;\
-    s->bi_buf |= (val << s->bi_valid);\
+    s->bi_buf |= (ush)val << s->bi_valid;\
     put_short(s, s->bi_buf);\
     s->bi_buf = (ush)val >> (Buf_size - s->bi_valid);\
     s->bi_valid += len - Buf_size;\
   } else {\
-    s->bi_buf |= (value) << s->bi_valid;\
+    s->bi_buf |= (ush)(value) << s->bi_valid;\
     s->bi_valid += len;\
   }\
 }
@@ -250,11 +251,13 @@ local void tr_static_init()
     if (static_init_done) return;
 
     /* For some embedded targets, global variables are not initialized: */
+#ifdef NO_INIT_GLOBAL_POINTERS
     static_l_desc.static_tree = static_ltree;
     static_l_desc.extra_bits = extra_lbits;
     static_d_desc.static_tree = static_dtree;
     static_d_desc.extra_bits = extra_dbits;
     static_bl_desc.extra_bits = extra_blbits;
+#endif
 
     /* Initialize the mapping length (0..255) -> length code (0..28) */
     length = 0;
@@ -348,13 +351,14 @@ void gen_trees_header()
                 static_dtree[i].Len, SEPARATOR(i, D_CODES-1, 5));
     }
 
-    fprintf(header, "const uch _dist_code[DIST_CODE_LEN] = {\n");
+    fprintf(header, "const uch ZLIB_INTERNAL _dist_code[DIST_CODE_LEN] = {\n");
     for (i = 0; i < DIST_CODE_LEN; i++) {
         fprintf(header, "%2u%s", _dist_code[i],
                 SEPARATOR(i, DIST_CODE_LEN-1, 20));
     }
 
-    fprintf(header, "const uch _length_code[MAX_MATCH-MIN_MATCH+1]= {\n");
+    fprintf(header,
+        "const uch ZLIB_INTERNAL _length_code[MAX_MATCH-MIN_MATCH+1]= {\n");
     for (i = 0; i < MAX_MATCH-MIN_MATCH+1; i++) {
         fprintf(header, "%2u%s", _length_code[i],
                 SEPARATOR(i, MAX_MATCH-MIN_MATCH, 20));
@@ -379,7 +383,7 @@ void gen_trees_header()
 /* ===========================================================================
  * Initialize the tree data structures for a new zlib stream.
  */
-void _tr_init(s)
+void ZLIB_INTERNAL _tr_init(s)
     deflate_state *s;
 {
     tr_static_init();
@@ -864,13 +868,13 @@ local void send_all_trees(s, lcodes, dcodes, blcodes)
 /* ===========================================================================
  * Send a stored block
  */
-void _tr_stored_block(s, buf, stored_len, eof)
+void ZLIB_INTERNAL _tr_stored_block(s, buf, stored_len, last)
     deflate_state *s;
     charf *buf;       /* input block */
     ulg stored_len;   /* length of input block */
-    int eof;          /* true if this is the last block for a file */
+    int last;         /* one if this is the last block for a file */
 {
-    send_bits(s, (STORED_BLOCK<<1)+eof, 3);  /* send block type */
+    send_bits(s, (STORED_BLOCK<<1)+last, 3);    /* send block type */
 #ifdef DEBUG
     s->compressed_len = (s->compressed_len + 3 + 7) & (ulg)~7L;
     s->compressed_len += (stored_len + 4) << 3;
@@ -889,7 +893,7 @@ void _tr_stored_block(s, buf, stored_len, eof)
  * To simplify the code, we assume the worst case of last real code encoded
  * on one bit only.
  */
-void _tr_align(s)
+void ZLIB_INTERNAL _tr_align(s)
     deflate_state *s;
 {
     send_bits(s, STATIC_TREES<<1, 3);
@@ -918,11 +922,11 @@ void _tr_align(s)
  * Determine the best encoding for the current block: dynamic trees, static
  * trees or store, and output the encoded block to the zip file.
  */
-void _tr_flush_block(s, buf, stored_len, eof)
+void ZLIB_INTERNAL _tr_flush_block(s, buf, stored_len, last)
     deflate_state *s;
     charf *buf;       /* input block, or NULL if too old */
     ulg stored_len;   /* length of input block */
-    int eof;          /* true if this is the last block for a file */
+    int last;         /* one if this is the last block for a file */
 {
     ulg opt_lenb, static_lenb; /* opt_len and static_len in bytes */
     int max_blindex = 0;  /* index of last bit length code of non zero freq */
@@ -931,8 +935,8 @@ void _tr_flush_block(s, buf, stored_len, eof)
     if (s->level > 0) {
 
         /* Check if the file is binary or text */
-        if (stored_len > 0 && s->strm->data_type == Z_UNKNOWN)
-            set_data_type(s);
+        if (s->strm->data_type == Z_UNKNOWN)
+            s->strm->data_type = detect_data_type(s);
 
         /* Construct the literal and distance trees */
         build_tree(s, (tree_desc *)(&(s->l_desc)));
@@ -978,20 +982,20 @@ void _tr_flush_block(s, buf, stored_len, eof)
          * successful. If LIT_BUFSIZE <= WSIZE, it is never too late to
          * transform a block into a stored block.
          */
-        _tr_stored_block(s, buf, stored_len, eof);
+        _tr_stored_block(s, buf, stored_len, last);
 
 #ifdef FORCE_STATIC
     } else if (static_lenb >= 0) { /* force static trees */
 #else
     } else if (s->strategy == Z_FIXED || static_lenb == opt_lenb) {
 #endif
-        send_bits(s, (STATIC_TREES<<1)+eof, 3);
+        send_bits(s, (STATIC_TREES<<1)+last, 3);
         compress_block(s, (ct_data *)static_ltree, (ct_data *)static_dtree);
 #ifdef DEBUG
         s->compressed_len += 3 + s->static_len;
 #endif
     } else {
-        send_bits(s, (DYN_TREES<<1)+eof, 3);
+        send_bits(s, (DYN_TREES<<1)+last, 3);
         send_all_trees(s, s->l_desc.max_code+1, s->d_desc.max_code+1,
                        max_blindex+1);
         compress_block(s, (ct_data *)s->dyn_ltree, (ct_data *)s->dyn_dtree);
@@ -1005,21 +1009,21 @@ void _tr_flush_block(s, buf, stored_len, eof)
      */
     init_block(s);
 
-    if (eof) {
+    if (last) {
         bi_windup(s);
 #ifdef DEBUG
         s->compressed_len += 7;  /* align on byte boundary */
 #endif
     }
     Tracev((stderr,"\ncomprlen %lu(%lu) ", s->compressed_len>>3,
-           s->compressed_len-7*eof));
+           s->compressed_len-7*last));
 }
 
 /* ===========================================================================
  * Save the match info and tally the frequency counts. Return true if
  * the current block must be flushed.
  */
-int _tr_tally (s, dist, lc)
+int ZLIB_INTERNAL _tr_tally (s, dist, lc)
     deflate_state *s;
     unsigned dist;  /* distance of matched string */
     unsigned lc;    /* match length-MIN_MATCH or unmatched char (if dist==0) */
@@ -1118,24 +1122,45 @@ local void compress_block(s, ltree, dtree)
 }
 
 /* ===========================================================================
- * Set the data type to BINARY or TEXT, using a crude approximation:
- * set it to Z_TEXT if all symbols are either printable characters (33 to 255)
- * or white spaces (9 to 13, or 32); or set it to Z_BINARY otherwise.
+ * Check if the data type is TEXT or BINARY, using the following algorithm:
+ * - TEXT if the two conditions below are satisfied:
+ *    a) There are no non-portable control characters belonging to the
+ *       "black list" (0..6, 14..25, 28..31).
+ *    b) There is at least one printable character belonging to the
+ *       "white list" (9 {TAB}, 10 {LF}, 13 {CR}, 32..255).
+ * - BINARY otherwise.
+ * - The following partially-portable control characters form a
+ *   "gray list" that is ignored in this detection algorithm:
+ *   (7 {BEL}, 8 {BS}, 11 {VT}, 12 {FF}, 26 {SUB}, 27 {ESC}).
  * IN assertion: the fields Freq of dyn_ltree are set.
  */
-local void set_data_type(s)
+local int detect_data_type(s)
     deflate_state *s;
 {
+    /* black_mask is the bit mask of black-listed bytes
+     * set bits 0..6, 14..25, and 28..31
+     * 0xf3ffc07f = binary 11110011111111111100000001111111
+     */
+    unsigned long black_mask = 0xf3ffc07fUL;
     int n;
 
-    for (n = 0; n < 9; n++)
+    /* Check for non-textual ("black-listed") bytes. */
+    for (n = 0; n <= 31; n++, black_mask >>= 1)
+        if ((black_mask & 1) && (s->dyn_ltree[n].Freq != 0))
+            return Z_BINARY;
+
+    /* Check for textual ("white-listed") bytes. */
+    if (s->dyn_ltree[9].Freq != 0 || s->dyn_ltree[10].Freq != 0
+            || s->dyn_ltree[13].Freq != 0)
+        return Z_TEXT;
+    for (n = 32; n < LITERALS; n++)
         if (s->dyn_ltree[n].Freq != 0)
-            break;
-    if (n == 9)
-        for (n = 14; n < 32; n++)
-            if (s->dyn_ltree[n].Freq != 0)
-                break;
-    s->strm->data_type = (n == 32) ? Z_TEXT : Z_BINARY;
+            return Z_TEXT;
+
+    /* There are no "black-listed" or "white-listed" bytes:
+     * this stream either is empty or has tolerated ("gray-listed") bytes only.
+     */
+    return Z_BINARY;
 }
 
 /* ===========================================================================
index 72facf9..d35639d 100644 (file)
@@ -70,7 +70,7 @@ local const ct_data static_dtree[D_CODES] = {
 {{19},{ 5}}, {{11},{ 5}}, {{27},{ 5}}, {{ 7},{ 5}}, {{23},{ 5}}
 };
 
-const uch _dist_code[DIST_CODE_LEN] = {
+const uch ZLIB_INTERNAL _dist_code[DIST_CODE_LEN] = {
  0,  1,  2,  3,  4,  4,  5,  5,  6,  6,  6,  6,  7,  7,  7,  7,  8,  8,  8,  8,
  8,  8,  8,  8,  9,  9,  9,  9,  9,  9,  9,  9, 10, 10, 10, 10, 10, 10, 10, 10,
 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
@@ -99,7 +99,7 @@ const uch _dist_code[DIST_CODE_LEN] = {
 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29
 };
 
-const uch _length_code[MAX_MATCH-MIN_MATCH+1]= {
+const uch ZLIB_INTERNAL _length_code[MAX_MATCH-MIN_MATCH+1]= {
  0,  1,  2,  3,  4,  5,  6,  7,  8,  8,  9,  9, 10, 10, 11, 11, 12, 12, 12, 12,
 13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16, 16, 16, 16, 16,
 17, 17, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 18, 18, 18, 19, 19, 19, 19,
index 882c4ea..ad98be3 100644 (file)
@@ -1,9 +1,9 @@
 /* uncompr.c -- decompress a memory buffer
- * Copyright (C) 1995-2003 Jean-loup Gailly.
+ * Copyright (C) 1995-2003, 2010 Jean-loup Gailly.
  * For conditions of distribution and use, see copyright notice in zlib.h
  */
 
-/* @(#) $Id: uncompr.c,v 1.2 2008-05-26 19:08:10 vp153 Exp $ */
+/* @(#) $Id$ */
 
 #define ZLIB_INTERNAL
 #include "zlib.h"
@@ -16,8 +16,6 @@
    been saved previously by the compressor and transmitted to the decompressor
    by some mechanism outside the scope of this compression library.)
    Upon exit, destLen is the actual size of the compressed buffer.
-     This function can be used to decompress a whole file at once if the
-   input file is mmap'ed.
 
      uncompress returns Z_OK if success, Z_MEM_ERROR if there was not
    enough memory, Z_BUF_ERROR if there was not enough room in the output
index 8b5e750..898ed34 100644 (file)
@@ -1,9 +1,9 @@
 /* zutil.c -- target dependent utility functions for the compression library
- * Copyright (C) 1995-2005 Jean-loup Gailly.
+ * Copyright (C) 1995-2005, 2010 Jean-loup Gailly.
  * For conditions of distribution and use, see copyright notice in zlib.h
  */
 
-/* @(#) $Id: zutil.c,v 1.2 2008-05-26 19:08:10 vp153 Exp $ */
+/* @(#) $Id$ */
 
 #include "zutil.h"
 
@@ -34,25 +34,25 @@ uLong ZEXPORT zlibCompileFlags()
     uLong flags;
 
     flags = 0;
-    switch (sizeof(uInt)) {
+    switch ((int)(sizeof(uInt))) {
     case 2:     break;
     case 4:     flags += 1;     break;
     case 8:     flags += 2;     break;
     default:    flags += 3;
     }
-    switch (sizeof(uLong)) {
+    switch ((int)(sizeof(uLong))) {
     case 2:     break;
     case 4:     flags += 1 << 2;        break;
     case 8:     flags += 2 << 2;        break;
     default:    flags += 3 << 2;
     }
-    switch (sizeof(voidpf)) {
+    switch ((int)(sizeof(voidpf))) {
     case 2:     break;
     case 4:     flags += 1 << 4;        break;
     case 8:     flags += 2 << 4;        break;
     default:    flags += 3 << 4;
     }
-    switch (sizeof(z_off_t)) {
+    switch ((int)(sizeof(z_off_t))) {
     case 2:     break;
     case 4:     flags += 1 << 6;        break;
     case 8:     flags += 2 << 6;        break;
@@ -117,9 +117,9 @@ uLong ZEXPORT zlibCompileFlags()
 #  ifndef verbose
 #    define verbose 0
 #  endif
-int z_verbose = verbose;
+int ZLIB_INTERNAL z_verbose = verbose;
 
-void z_error (m)
+void ZLIB_INTERNAL z_error (m)
     char *m;
 {
     fprintf(stderr, "%s\n", m);
@@ -146,7 +146,7 @@ const char * ZEXPORT zError(err)
 
 #ifndef HAVE_MEMCPY
 
-void zmemcpy(dest, source, len)
+void ZLIB_INTERNAL zmemcpy(dest, source, len)
     Bytef* dest;
     const Bytef* source;
     uInt  len;
@@ -157,7 +157,7 @@ void zmemcpy(dest, source, len)
     } while (--len != 0);
 }
 
-int zmemcmp(s1, s2, len)
+int ZLIB_INTERNAL zmemcmp(s1, s2, len)
     const Bytef* s1;
     const Bytef* s2;
     uInt  len;
@@ -170,7 +170,7 @@ int zmemcmp(s1, s2, len)
     return 0;
 }
 
-void zmemzero(dest, len)
+void ZLIB_INTERNAL zmemzero(dest, len)
     Bytef* dest;
     uInt  len;
 {
@@ -213,7 +213,7 @@ local ptr_table table[MAX_PTR];
  * a protected system like OS/2. Use Microsoft C instead.
  */
 
-voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
+voidpf ZLIB_INTERNAL zcalloc (voidpf opaque, unsigned items, unsigned size)
 {
     voidpf buf = opaque; /* just to make some compilers happy */
     ulg bsize = (ulg)items*size;
@@ -237,7 +237,7 @@ voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
     return buf;
 }
 
-void  zcfree (voidpf opaque, voidpf ptr)
+void ZLIB_INTERNAL zcfree (voidpf opaque, voidpf ptr)
 {
     int n;
     if (*(ush*)&ptr != 0) { /* object < 64K */
@@ -272,13 +272,13 @@ void  zcfree (voidpf opaque, voidpf ptr)
 #  define _hfree   hfree
 #endif
 
-voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
+voidpf ZLIB_INTERNAL zcalloc (voidpf opaque, uInt items, uInt size)
 {
     if (opaque) opaque = 0; /* to make compiler happy */
     return _halloc((long)items, size);
 }
 
-void  zcfree (voidpf opaque, voidpf ptr)
+void ZLIB_INTERNAL zcfree (voidpf opaque, voidpf ptr)
 {
     if (opaque) opaque = 0; /* to make compiler happy */
     _hfree(ptr);
@@ -297,7 +297,7 @@ extern voidp  calloc OF((uInt items, uInt size));
 extern void   free   OF((voidpf ptr));
 #endif
 
-voidpf zcalloc (opaque, items, size)
+voidpf ZLIB_INTERNAL zcalloc (opaque, items, size)
     voidpf opaque;
     unsigned items;
     unsigned size;
@@ -307,7 +307,7 @@ voidpf zcalloc (opaque, items, size)
                               (voidpf)calloc(items, size);
 }
 
-void  zcfree (opaque, ptr)
+void ZLIB_INTERNAL zcfree (opaque, ptr)
     voidpf opaque;
     voidpf ptr;
 {
index 8990205..258fa88 100644 (file)
@@ -1,5 +1,5 @@
 /* zutil.h -- internal interface and configuration of the compression library
- * Copyright (C) 1995-2005 Jean-loup Gailly.
+ * Copyright (C) 1995-2010 Jean-loup Gailly.
  * For conditions of distribution and use, see copyright notice in zlib.h
  */
 
@@ -8,36 +8,26 @@
    subject to change. Applications should only use zlib.h.
  */
 
-/* @(#) $Id: zutil.h,v 1.2 2008-05-26 19:08:10 vp153 Exp $ */
+/* @(#) $Id$ */
 
 #ifndef ZUTIL_H
 #define ZUTIL_H
 
-#define ZLIB_INTERNAL
+#if ((__GNUC__-0) * 10 + __GNUC_MINOR__-0 >= 33) && !defined(NO_VIZ)
+#  define ZLIB_INTERNAL __attribute__((visibility ("hidden")))
+#else
+#  define ZLIB_INTERNAL
+#endif
+
 #include "zlib.h"
 
 #ifdef STDC
-#  ifndef _WIN32_WCE
+#  if !(defined(_WIN32_WCE) && defined(_MSC_VER))
 #    include <stddef.h>
 #  endif
 #  include <string.h>
 #  include <stdlib.h>
 #endif
-#ifdef NO_ERRNO_H
-#   ifdef _WIN32_WCE
-      /* The Microsoft C Run-Time Library for Windows CE doesn't have
-       * errno.  We define it as a global variable to simplify porting.
-       * Its value is always 0 and should not be used.  We rename it to
-       * avoid conflict with other libraries that use the same workaround.
-       */
-#     define errno z_errno
-#   endif
-    extern int errno;
-#else
-#  ifndef _WIN32_WCE
-#    include <errno.h>
-#  endif
-#endif
 
 #ifndef local
 #  define local static
@@ -89,7 +79,7 @@ extern const char * const z_errmsg[10]; /* indexed by 2-zlib_error */
 #if defined(MSDOS) || (defined(WINDOWS) && !defined(WIN32))
 #  define OS_CODE  0x00
 #  if defined(__TURBOC__) || defined(__BORLANDC__)
-#    if(__STDC__ == 1) && (defined(__LARGE__) || defined(__COMPACT__))
+#    if (__STDC__ == 1) && (defined(__LARGE__) || defined(__COMPACT__))
        /* Allow compilation with ANSI keywords only enabled */
        void _Cdecl farfree( void *block );
        void *_Cdecl farmalloc( unsigned long nbytes );
@@ -118,7 +108,7 @@ extern const char * const z_errmsg[10]; /* indexed by 2-zlib_error */
 #ifdef OS2
 #  define OS_CODE  0x06
 #  ifdef M_I86
-     #include <malloc.h>
+#    include <malloc.h>
 #  endif
 #endif
 
@@ -151,7 +141,7 @@ extern const char * const z_errmsg[10]; /* indexed by 2-zlib_error */
 #  define fdopen(fd,mode) NULL /* No fdopen() */
 #endif
 
-#if (defined(_MSC_VER) && (_MSC_VER > 600))
+#if (defined(_MSC_VER) && (_MSC_VER > 600)) && !defined __INTERIX
 #  if defined(_WIN32_WCE)
 #    define fdopen(fd,mode) NULL /* No fdopen() */
 #    ifndef _PTRDIFF_T_DEFINED
@@ -163,6 +153,18 @@ extern const char * const z_errmsg[10]; /* indexed by 2-zlib_error */
 #  endif
 #endif
 
+#if defined(__BORLANDC__)
+  #pragma warn -8004
+  #pragma warn -8008
+  #pragma warn -8066
+#endif
+
+/* provide prototypes for these when building zlib without LFS */
+#if !defined(_LARGEFILE64_SOURCE) || _LFS64_LARGEFILE-0 == 0
+    ZEXTERN uLong ZEXPORT adler32_combine64 OF((uLong, uLong, z_off_t));
+    ZEXTERN uLong ZEXPORT crc32_combine64 OF((uLong, uLong, z_off_t));
+#endif
+
         /* common defaults */
 
 #ifndef OS_CODE
@@ -197,7 +199,9 @@ extern const char * const z_errmsg[10]; /* indexed by 2-zlib_error */
 #  ifdef WIN32
      /* In Win32, vsnprintf is available as the "non-ANSI" _vsnprintf. */
 #    if !defined(vsnprintf) && !defined(NO_vsnprintf)
-#      define vsnprintf _vsnprintf
+#      if !defined(_MSC_VER) || ( defined(_MSC_VER) && _MSC_VER < 1500 )
+#         define vsnprintf _vsnprintf
+#      endif
 #    endif
 #  endif
 #  ifdef __SASC
@@ -232,16 +236,16 @@ extern const char * const z_errmsg[10]; /* indexed by 2-zlib_error */
 #    define zmemzero(dest, len) memset(dest, 0, len)
 #  endif
 #else
-   extern void zmemcpy  OF((Bytef* dest, const Bytef* source, uInt len));
-   extern int  zmemcmp  OF((const Bytef* s1, const Bytef* s2, uInt len));
-   extern void zmemzero OF((Bytef* dest, uInt len));
+   void ZLIB_INTERNAL zmemcpy OF((Bytef* dest, const Bytef* source, uInt len));
+   int ZLIB_INTERNAL zmemcmp OF((const Bytef* s1, const Bytef* s2, uInt len));
+   void ZLIB_INTERNAL zmemzero OF((Bytef* dest, uInt len));
 #endif
 
 /* Diagnostic functions */
 #ifdef DEBUG
 #  include <stdio.h>
-   extern int z_verbose;
-   extern void z_error    OF((char *m));
+   extern int ZLIB_INTERNAL z_verbose;
+   extern void ZLIB_INTERNAL z_error OF((char *m));
 #  define Assert(cond,msg) {if(!(cond)) z_error(msg);}
 #  define Trace(x) {if (z_verbose>=0) fprintf x ;}
 #  define Tracev(x) {if (z_verbose>0) fprintf x ;}
@@ -258,8 +262,9 @@ extern const char * const z_errmsg[10]; /* indexed by 2-zlib_error */
 #endif
 
 
-voidpf zcalloc OF((voidpf opaque, unsigned items, unsigned size));
-void   zcfree  OF((voidpf opaque, voidpf ptr));
+voidpf ZLIB_INTERNAL zcalloc OF((voidpf opaque, unsigned items,
+                        unsigned size));
+void ZLIB_INTERNAL zcfree  OF((voidpf opaque, voidpf ptr));
 
 #define ZALLOC(strm, items, size) \
            (*((strm)->zalloc))((strm)->opaque, (items), (size))
index efddfd8..3ffeed8 100644 (file)
@@ -453,7 +453,7 @@ bool cv::Affine3DEstimator::checkSubset( const CvMat* ms1, int count )
 
 int cv::estimateAffine3D(const Mat& from, const Mat& to, Mat& out, vector<uchar>& outliers, double param1, double param2)
 {
-    size_t count = from.cols*from.rows*from.channels()/3;
+    int count = from.cols*from.rows*from.channels()/3;
     
     CV_Assert( count >= 4 && from.isContinuous() && to.isContinuous() &&
                from.depth() == CV_32F && to.depth() == CV_32F &&
index d84d875..485d73d 100644 (file)
@@ -106,7 +106,7 @@ static void prefilterNorm( const Mat& src, Mat& dst, int winsize, int ftzero, uc
     const int OFS = 256*5, TABSZ = OFS*2 + 256;
     uchar tab[TABSZ];
     const uchar* sptr = src.data;
-    int srcstep = src.step;
+    int srcstep = (int)src.step;
     Size size = src.size();
 
     scale_g *= scale_s;
@@ -274,11 +274,11 @@ static void findStereoCorrespondenceBM_SSE2( const Mat& left, const Mat& right,
     const uchar* rptr0 = right.data + rofs;
     const uchar *lptr, *lptr_sub, *rptr;
     short* dptr = (short*)disp.data;
-    int sstep = left.step;
-    int dstep = disp.step/sizeof(dptr[0]);
+    int sstep = (int)left.step;
+    int dstep = (int)(disp.step/sizeof(dptr[0]));
     int cstep = (height + dy0 + dy1)*ndisp;
     short costbuf = 0;
-    int coststep = cost.data ? cost.step/sizeof(costbuf) : 0;
+    int coststep = cost.data ? (int)(cost.step/sizeof(costbuf)) : 0;
     const int TABSZ = 256;
     uchar tab[TABSZ];
     const __m128i d0_8 = _mm_setr_epi16(0,1,2,3,4,5,6,7), dd_8 = _mm_set1_epi16(8);
@@ -529,11 +529,11 @@ findStereoCorrespondenceBM( const Mat& left, const Mat& right,
     const uchar* rptr0 = right.data + rofs;
     const uchar *lptr, *lptr_sub, *rptr;
     short* dptr = (short*)disp.data;
-    int sstep = left.step;
-    int dstep = disp.step/sizeof(dptr[0]);
+    int sstep = (int)left.step;
+    int dstep = (int)(disp.step/sizeof(dptr[0]));
     int cstep = (height+dy0+dy1)*ndisp;
     int costbuf = 0;
-    int coststep = cost.data ? cost.step/sizeof(costbuf) : 0;
+    int coststep = cost.data ? (int)(cost.step/sizeof(costbuf)) : 0;
     const int TABSZ = 256;
     uchar tab[TABSZ];
 
index bfd4951..5b15898 100644 (file)
@@ -372,8 +372,8 @@ static void computeDisparitySGBM( const Mat& img1, const Mat& img2,
     width*(sizeof(CostType) + sizeof(DispType)) + 1024; // disp2cost + disp2
     
     if( !buffer.data || !buffer.isContinuous() ||
-       buffer.cols*buffer.rows*buffer.elemSize() < totalBufSize )
-        buffer.create(1, totalBufSize, CV_8U);
+        buffer.cols*buffer.rows*buffer.elemSize() < totalBufSize )
+        buffer.create(1, (int)totalBufSize, CV_8U);
     
     // summary cost over different (nDirs) directions
     CostType* Cbuf = (CostType*)alignPtr(buffer.data, ALIGN);
@@ -828,10 +828,10 @@ void filterSpeckles( Mat& img, double _newval, int maxSpeckleSize, double _maxDi
     int width = img.cols, height = img.rows, npixels = width*height;
     size_t bufSize = npixels*(int)(sizeof(Point2s) + sizeof(int) + sizeof(uchar));
     if( !_buf.isContinuous() || !_buf.data || _buf.cols*_buf.rows*_buf.elemSize() < bufSize )
-        _buf.create(1, bufSize, CV_8U);
+        _buf.create(1, (int)bufSize, CV_8U);
     
     uchar* buf = _buf.data;
-    int i, j, dstep = img.step/sizeof(short);
+    int i, j, dstep = (int)(img.step/sizeof(short));
     int* labels = (int*)buf;
     buf += npixels*sizeof(labels[0]);
     Point2s* wbuf = (Point2s*)buf;
index 086e07f..f380ff4 100644 (file)
@@ -342,7 +342,7 @@ namespace cv
         Mat packRandomScaledSpins(bool separateScale = false, size_t xCount = 10, size_t yCount = 10) const;
         
         size_t getSpinCount() const { return spinImages.rows; }
-        Mat getSpinImage(size_t index) const { return spinImages.row(index); }
+        Mat getSpinImage(size_t index) const { return spinImages.row((int)index); }
         const Point3f& getSpinVertex(size_t index) const { return mesh.vtx[subset[index]]; }
         const Point3f& getSpinNormal(size_t index) const { return mesh.normals[subset[index]]; }
         
index 1bc7886..e2396a9 100644 (file)
@@ -209,7 +209,7 @@ CvAdaptiveSkinDetector::Histogram::~Histogram()
 
 int CvAdaptiveSkinDetector::Histogram::findCoverageIndex(double surfaceToCover, int defaultValue)
 {
-       float s = 0;
+       double s = 0;
        for (int i = 0; i < HistogramSize; i++)
        {
                s += cvGetReal1D( fHistogram->bins, i );
@@ -223,7 +223,7 @@ int CvAdaptiveSkinDetector::Histogram::findCoverageIndex(double surfaceToCover,
 
 void CvAdaptiveSkinDetector::Histogram::findCurveThresholds(int &x1, int &x2, double percent)
 {
-       float sum = 0;
+       double sum = 0;
 
        for (int i = 0; i < HistogramSize; i++)
        {
index 66bdeba..d4425bf 100644 (file)
@@ -984,8 +984,8 @@ void LevMarqSparse::bundleAdjust( vector<Point3d>& points, //positions of points
                   const TermCriteria& criteria)
                   //,enum{MOTION_AND_STRUCTURE,MOTION,STRUCTURE})
 {     
-    int num_points = points.size();
-    int num_cameras = cameraMatrix.size();
+    int num_points = (int)points.size();
+    int num_cameras = (int)cameraMatrix.size();
 
     CV_Assert( imagePoints.size() == (size_t)num_cameras && 
                visibility.size() == (size_t)num_cameras && 
index fbfdd08..73c846a 100644 (file)
@@ -89,10 +89,11 @@ bool is_smaller(const std::pair<int, float>& p1, const std::pair<int, float>& p2
 void orderContours(const vector<vector<Point> >& contours, Point2f point, vector<std::pair<int, float> >& order)
 {
     order.clear();
-    for(size_t i = 0; i < contours.size(); i++)
+    int i, j, n = (int)contours.size();
+    for(i = 0; i < n; i++)
     {
         double min_dist = std::numeric_limits<double>::max();
-        for(size_t j = 0; j < contours[i].size(); j++)
+        for(j = 0; j < n; j++)
         {
             double dist = norm(Point2f((float)contours[i][j].x, (float)contours[i][j].y) - point);
             min_dist = MIN(min_dist, dist);
@@ -137,7 +138,7 @@ void findCorner(const vector<Point>& contour, Point2f point, Point2f& corner)
         if(dist < min_dist)
         {
             min_dist = dist;
-            min_idx = i;
+            min_idx = (int)i;
         }
     }
     assert(min_idx >= 0);
@@ -162,7 +163,7 @@ void findCorner(const vector<Point2f>& contour, Point2f point, Point2f& corner)
         if(dist < min_dist)
         {
             min_dist = dist;
-            min_idx = i;
+            min_idx = (int)i;
         }
     }
     assert(min_idx >= 0);
index 05326a4..c0eab60 100644 (file)
@@ -124,7 +124,7 @@ Size SelfSimDescriptor::getGridSize( Size imgSize, Size winStride ) const
 void SelfSimDescriptor::SSD(const Mat& img, Point pt, Mat& ssd) const
 {
        int x, y, dx, dy, r0 = largeSize/2, r1 = smallSize/2;
-    int step = img.step;
+    int step = (int)img.step;
     for( y = -r0; y <= r0; y++ )
     {
         float* sptr = ssd.ptr<float>(y+r0) + r0;
index 870a2a3..71d39c9 100644 (file)
@@ -447,7 +447,7 @@ float cv::Mesh3D::estimateResolution(float tryRatio)
     size_t tryNum = static_cast<size_t>(tryRatio * vtx.size());
     tryNum = min(max(tryNum, minReasonable), vtx.size());
 
-    CvMat desc = cvMat(vtx.size(), 3, CV_32F, &vtx[0]);
+    CvMat desc = cvMat((int)vtx.size(), 3, CV_32F, &vtx[0]);
     CvFeatureTree* tr = cvCreateKDTree(&desc);
 
     vector<double> dist(tryNum * neighbors);
@@ -458,9 +458,9 @@ float cv::Mesh3D::estimateResolution(float tryRatio)
     for(size_t i = 0; i < tryNum; ++i)
         query.push_back(vtx[rng.next() % vtx.size()]);
         
-    CvMat cvinds  = cvMat( tryNum, neighbors, CV_32S,  &inds[0] );
-    CvMat cvdist  = cvMat( tryNum, neighbors, CV_64F,  &dist[0] );    
-    CvMat cvquery = cvMat( tryNum,         3, CV_32F, &query[0] );
+    CvMat cvinds  = cvMat( (int)tryNum, neighbors, CV_32S,  &inds[0] );
+    CvMat cvdist  = cvMat( (int)tryNum, neighbors, CV_64F,  &dist[0] );    
+    CvMat cvquery = cvMat( (int)tryNum,         3, CV_32F, &query[0] );
     cvFindFeatures(tr, &cvquery, &cvinds, &cvdist, neighbors, 50);    
     cvReleaseFeatureTree(tr);
 
@@ -753,7 +753,7 @@ Mat cv::SpinImageModel::packRandomScaledSpins(bool separateScale, size_t xCount,
 
     int sz = spins.front().cols;
 
-    Mat result(yCount * sz + (yCount - 1), xCount * sz + (xCount - 1), CV_8UC3);    
+    Mat result((int)(yCount * sz + (yCount - 1)), (int)(xCount * sz + (xCount - 1)), CV_8UC3);    
     result = colors[(static_cast<int64>(cvGetTickCount()/cvGetTickFrequency())/1000) % colors_mum];
 
     size_t pos = 0;
@@ -761,11 +761,11 @@ Mat cv::SpinImageModel::packRandomScaledSpins(bool separateScale, size_t xCount,
         for(size_t x = 0; x < xCount; ++x)        
             if (pos < num)
             {
-                int starty = (y + 0) * sz + y;
-                int endy   = (y + 1) * sz + y;
+                int starty = (int)((y + 0) * sz + y);
+                int endy   = (int)((y + 1) * sz + y);
 
-                int startx = (x + 0) * sz + x;
-                int endx   = (x + 1) * sz + x;
+                int startx = (int)((x + 0) * sz + x);
+                int endx   = (int)((x + 1) * sz + x);
 
                 Mat color;
                 cvtColor(spins[pos++], color, CV_GRAY2BGR);
@@ -802,7 +802,7 @@ void cv::SpinImageModel::selectRandomSubset(float ratio)
         for(size_t i = 0; i < setSize; ++i)
         {
             int pos = rnd.next() % left.size();
-            subset[i] = left[pos];
+            subset[i] = (int)left[pos];
 
             left[pos] = left.back();        
             left.resize(left.size() - 1);        
@@ -821,14 +821,14 @@ void cv::SpinImageModel::repackSpinImages(const vector<uchar>& mask, Mat& spinIm
     if (reAlloc)
     {
         size_t spinCount = mask.size() - count(mask.begin(), mask.end(), (uchar)0);
-        Mat newImgs(spinCount, spinImages.cols, spinImages.type());    
+        Mat newImgs((int)spinCount, spinImages.cols, spinImages.type());    
 
         int pos = 0;
         for(size_t t = 0; t < mask.size(); ++t)
             if (mask[t])
             {
                 Mat row = newImgs.row(pos++);
-                spinImages.row(t).copyTo(row);
+                spinImages.row((int)t).copyTo(row);
             }
         spinImages = newImgs;
     }
@@ -836,7 +836,7 @@ void cv::SpinImageModel::repackSpinImages(const vector<uchar>& mask, Mat& spinIm
     {
         int last = (int)mask.size();
 
-        int dest = find(mask.begin(), mask.end(), (uchar)0) - mask.begin();
+        int dest = (int)(find(mask.begin(), mask.end(), (uchar)0) - mask.begin());
         if (dest == last)
             return;
 
@@ -942,7 +942,7 @@ void cv::SpinImageModel::matchSpinToModel(const Mat& spin, vector<int>& indeces,
         if (masks[i])
             if (/* corrs[i] < histThresLo || */ corrs[i] > histThresHi)
             {
-                indeces.push_back(i);
+                indeces.push_back((int)i);
                 corrCoeffs.push_back(corrs[i]);                
             }
 } 
@@ -975,7 +975,7 @@ struct WgcHelper
     /* Wgc( correspondence_C, group_{C1..Cn} ) = max_i=1..n_( Wgc(C, Ci) ) */
     float Wgc(const size_t corespInd, const group_t& group) const
     {
-        const float* wgcLine = mat.ptr<float>(corespInd);
+        const float* wgcLine = mat.ptr<float>((int)corespInd);
         float maximum = numeric_limits<float>::min();
         
         for(citer pos = group.begin(); pos != group.end(); ++pos)
@@ -1046,17 +1046,17 @@ private:
         allMatches.end());
     if (out) *out << "Matches number [filtered by similarity measure] = " << allMatches.size() << endl;
 
-    size_t matchesSize = allMatches.size();
+    int matchesSize = (int)allMatches.size();
     if(matchesSize == 0)
         return;
     
     /* filtering by geometric consistency */        
-    for(size_t i = 0; i < matchesSize; ++i)
+    for(int i = 0; i < matchesSize; ++i)
     {
         size_t consistNum = 1;
         float gc = float_max;
         
-        for(size_t j = 0; j < matchesSize; ++j)
+        for(int j = 0; j < matchesSize; ++j)
             if (i != j)
             {
                 const Match& mi = allMatches[i];
@@ -1096,18 +1096,18 @@ private:
     if (out) *out << "Matches number [filtered by geometric consistency] = " << allMatches.size() << endl;
 
 
-    matchesSize = allMatches.size();
+    matchesSize = (int)allMatches.size();
     if(matchesSize == 0)
         return;
 
     if (out) *out << "grouping ..." << endl;
 
-    Mat groupingMat(matchesSize, matchesSize, CV_32F);
+    Mat groupingMat((int)matchesSize, (int)matchesSize, CV_32F);
     groupingMat = Scalar(0);        
         
     /* grouping */
-    for(size_t j = 0; j < matchesSize; ++j)
-        for(size_t i = j + 1; i < matchesSize; ++i)        
+    for(int j = 0; j < matchesSize; ++j)
+        for(int i = j + 1; i < matchesSize; ++i)        
         {
             const Match& mi = allMatches[i];
             const Match& mj = allMatches[j];
index 1d73738..26c1e18 100644 (file)
@@ -1816,7 +1816,7 @@ CV_EXPORTS void merge(const Mat* mv, size_t count, Mat& dst);
 CV_EXPORTS void split(const Mat& m, Mat* mvbegin);
 
 //! copies selected channels from the input arrays to the selected channels of the output arrays
-CV_EXPORTS void mixChannels(const Mat* src, int nsrcs, Mat* dst, int ndsts,
+CV_EXPORTS void mixChannels(const Mat* src, size_t nsrcs, Mat* dst, size_t ndsts,
                             const int* fromTo, size_t npairs);
 //! reverses the order of the rows, columns or both in a matrix
 CV_EXPORTS void flip(const Mat& a, Mat& b, int flipCode);
index b164e2e..0e8f49a 100644 (file)
@@ -2372,8 +2372,8 @@ inline LineIterator LineIterator::operator ++(int)
 inline Point LineIterator::pos() const
 {
     Point p;
-    p.y = (ptr - ptr0)/step;
-    p.x = ((ptr - ptr0) - p.y*step)/elemSize;
+    p.y = (int)((ptr - ptr0)/step);
+    p.x = (int)(((ptr - ptr0) - p.y*step)/elemSize);
     return p;
 }
     
index e1924e7..215aa12 100644 (file)
@@ -333,7 +333,7 @@ mixChannels_( const void** _src, const int* sdelta0,
 typedef void (*MixChannelsFunc)( const void** src, const int* sdelta0,
         const int* sdelta1, void** dst, const int* ddelta0, const int* ddelta1, int n, Size size );
 
-void mixChannels( const Mat* src, int nsrcs, Mat* dst, int ndsts, const int* fromTo, size_t npairs )
+void mixChannels( const Mat* src, size_t nsrcs, Mat* dst, size_t ndsts, const int* fromTo, size_t npairs )
 {
     size_t i;
     
index 967cb11..8d4643c 100644 (file)
@@ -3937,9 +3937,9 @@ void KDTree::findOrthoRange(const float* L, const float* R,
     
 void KDTree::getPoints(const int* idx, size_t nidx, Mat& pts) const
 {
-    int dims = points.cols;
-    pts.create( nidx, dims, points.type());
-    for( size_t i = 0; i < nidx; i++ )
+    int dims = points.cols, n = (int)nidx;
+    pts.create( n, dims, points.type());
+    for( int i = 0; i < n; i++ )
     {
         int k = idx[i];
         CV_Assert( (unsigned)k < (unsigned)points.rows );
@@ -3954,7 +3954,7 @@ void KDTree::getPoints(const Mat& idx, Mat& pts) const
     CV_Assert(idx.type() == CV_32S && idx.isContinuous() &&
               (idx.cols == 1 || idx.rows == 1));
     int dims = points.cols;
-    size_t i, nidx = idx.cols + idx.rows - 1;
+    int i, nidx = idx.cols + idx.rows - 1;
     pts.create( nidx, dims, points.type());
     const int* _idx = idx.ptr<int>();
     
index 2a2c143..b439a26 100644 (file)
@@ -121,9 +121,9 @@ Index::~Index()
 void Index::knnSearch(const vector<float>& query, vector<int>& indices, vector<float>& dists, int knn, const SearchParams& searchParams)
 {
 
-       ::cvflann::Matrix<float> m_query(1, query.size(), (float*)&query[0]);
-       ::cvflann::Matrix<int> m_indices(1, indices.size(), &indices[0]);
-       ::cvflann::Matrix<float> m_dists(1, dists.size(), &dists[0]);
+       ::cvflann::Matrix<float> m_query(1, (int)query.size(), (float*)&query[0]);
+       ::cvflann::Matrix<int> m_indices(1, (int)indices.size(), &indices[0]);
+       ::cvflann::Matrix<float> m_dists(1, (int)dists.size(), &dists[0]);
 
        nnIndex->knnSearch(m_query,m_indices,m_dists,knn,::cvflann::SearchParams(searchParams.checks));
 }
@@ -149,9 +149,9 @@ void Index::knnSearch(const Mat& queries, Mat& indices, Mat& dists, int knn, con
 
 int Index::radiusSearch(const vector<float>& query, vector<int>& indices, vector<float>& dists, float radius, const SearchParams& searchParams)
 {
-       ::cvflann::Matrix<float> m_query(1, query.size(), (float*)&query[0]);
-       ::cvflann::Matrix<int> m_indices(1, indices.size(), &indices[0]);
-       ::cvflann::Matrix<float> m_dists(1, dists.size(), &dists[0]);
+       ::cvflann::Matrix<float> m_query(1, (int)query.size(), (float*)&query[0]);
+       ::cvflann::Matrix<int> m_indices(1, (int)indices.size(), &indices[0]);
+       ::cvflann::Matrix<float> m_dists(1, (int)dists.size(), &dists[0]);
 
        return nnIndex->radiusSearch(m_query,m_indices,m_dists,radius,::cvflann::SearchParams(searchParams.checks));
 }
index 595ee59..413b0e6 100644 (file)
@@ -522,7 +522,7 @@ double invert( const Mat& src, Mat& dst, int method )
             else
             {
                 src.copyTo(arr);
-                lda = arr.step/sizeof(double);
+                lda = (integer)(arr.step/sizeof(double));
             }
 
             dgetrf_(&n, &n, (double*)arr.data, &lda, (integer*)buffer, &info);
@@ -545,7 +545,7 @@ double invert( const Mat& src, Mat& dst, int method )
             else
             {
                 src.copyTo(arr);
-                lda = arr.step/sizeof(double);
+                lda = (integer)(arr.step/sizeof(double));
             }
             
             char L[] = {'L', '\0'};
@@ -944,9 +944,9 @@ template<typename Real> bool jacobi(const Mat& _S0, Mat& _e, Mat& matE, bool com
     Real mv;
     Real* E = (Real*)matE.data;
     Real* e = (Real*)_e.data;
-    int Sstep = matS.step/sizeof(Real);
-    int estep = _e.rows == 1 ? 1 : _e.step/sizeof(Real);
-    int Estep = matE.step/sizeof(Real);
+    int Sstep = (int)(matS.step/sizeof(Real));
+    int estep = _e.rows == 1 ? 1 : (int)(_e.step/sizeof(Real));
+    int Estep = (int)(matE.step/sizeof(Real));
     
     for( k = 0; k < n; k++ )
     {
@@ -1144,7 +1144,7 @@ static bool eigen( const Mat& src, Mat& evals, Mat& evects, bool computeEvects,
                      (liwork+2*n+1)*sizeof(integer));
         Mat a(n, n, type, (uchar*)buf);
         src.copyTo(a);
-        lda = a.step1();
+        lda = (integer)a.step1();
         work = a.data + n*n*elem_size;
         if( copy_evals )
             s = (float*)(work + lwork*elem_size);
@@ -1174,7 +1174,7 @@ static bool eigen( const Mat& src, Mat& evals, Mat& evects, bool computeEvects,
                      (liwork+2*n+1)*sizeof(integer));
         Mat a(n, n, type, (uchar*)buf);
         src.copyTo(a);
-        lda = a.step1();
+        lda = (integer)a.step1();
         work = a.data + n*n*elem_size;
 
         if( copy_evals )
@@ -1339,7 +1339,7 @@ SVD& SVD::operator ()(const Mat& a, int flags)
     bool temp_a = false;
     double u1=0, v1=0, work1=0;
     float uf1=0, vf1=0, workf1=0;
-    integer lda, ldu, ldv, lwork=-1, iwork1=0, info=0, *iwork=0;
+    integer lda, ldu, ldv, lwork=-1, iwork1=0, info=0;
     char mode[] = {u.data || vt.data ? 'S' : 'N', '\0'};
 
     if( m != n && !(flags & NO_UV) && (flags & FULL_UV) )
@@ -1378,7 +1378,7 @@ SVD& SVD::operator ()(const Mat& a, int flags)
     }
     work_ofs = buf_size;
     buf_size += lwork*elem_size;
-    buf_size = cvAlign(buf_size, sizeof(iwork[0]));
+    buf_size = cvAlign(buf_size, sizeof(integer));
     iwork_ofs = buf_size;
     buf_size += 8*nm*sizeof(integer);
     
index 8c93b9e..6748259 100644 (file)
@@ -832,7 +832,7 @@ static void generateCentersPP(const Mat& _data, Mat& _out_centers,
 {
     int i, j, k, dims = _data.cols, N = _data.rows;
     const float* data = _data.ptr<float>(0);
-    int step = _data.step/sizeof(data[0]);
+    int step = (int)(_data.step/sizeof(data[0]));
     vector<int> _centers(K);
     int* centers = &_centers[0];
     vector<float> _dist(N*3);
@@ -2851,7 +2851,7 @@ void SparseMat::resizeHashTab(size_t newsize)
 {
     newsize = std::max(newsize, (size_t)8);
     if((newsize & (newsize-1)) != 0)
-        newsize = 1 << cvCeil(std::log((double)newsize)/CV_LOG2);
+        newsize = (size_t)1 << cvCeil(std::log((double)newsize)/CV_LOG2);
 
     size_t i, hsize = hdr->hashtab.size();
     vector<size_t> _newh(newsize);
@@ -2908,14 +2908,14 @@ uchar* SparseMat::newNode(const int* idx, size_t hashval)
     int i, d = hdr->dims;
     for( i = 0; i < d; i++ )
         elem->idx[i] = idx[i];
-    d = elemSize();
+    size_t esz = elemSize();
     uchar* p = &value<uchar>(elem);
-    if( d == sizeof(float) )
+    if( esz == sizeof(float) )
         *((float*)p) = 0.f;
-    else if( d == sizeof(double) )
+    else if( esz == sizeof(double) )
         *((double*)p) = 0.;
     else
-        memset(p, 0, d);
+        memset(p, 0, esz);
     
     return p;
 }
index 20010cf..108ee6d 100644 (file)
@@ -365,7 +365,7 @@ public:
     MSER();
     //! the full constructor
     MSER( int _delta, int _min_area, int _max_area,
-          float _max_variation, float _min_diversity,
+          double _max_variation, double _min_diversity,
           int _max_evolution, double _area_threshold,
           double _min_margin, int _edge_blur_size );
     //! the operator that extracts the MSERs from the image or the specific part of it
@@ -1340,7 +1340,7 @@ class CV_EXPORTS MserFeatureDetector : public FeatureDetector
 {
 public:
     MserFeatureDetector( CvMSERParams params = cvMSERParams () );
-    MserFeatureDetector( int delta, int minArea, int maxArea, float maxVariation, float minDiversity,
+    MserFeatureDetector( int delta, int minArea, int maxArea, double maxVariation, double minDiversity,
                          int maxEvolution, double areaThreshold, double minMargin, int edgeBlurSize );
 
     virtual void read (const FileNode& fn);
index fac0701..7c25d65 100644 (file)
@@ -51,7 +51,9 @@
 
 using namespace cv;
 
-
+#if defined _MSC_VER && _MSC_VER >= 1400
+#pragma warning(disable: 4244 4267)
+#endif
 
 /****************************************************************************************\
 The code below is implementation of Calonder Descriptor and RTree Classifier
index 78aaef9..a68f008 100644 (file)
@@ -85,7 +85,7 @@ void drawMatches( const Mat& img1, const vector<KeyPoint>& keypoints1,
         for( vector<KeyPoint>::const_iterator it = keypoints2.begin(); it < keypoints2.end(); ++it )
         {
             Point p = it->pt;
-            circle( outImg, Point2f(p.x+img1.cols, p.y), 3, isRandSinglePointColor ?
+            circle( outImg, Point(p.x+img1.cols, p.y), 3, isRandSinglePointColor ?
                     Scalar(rng.uniform(0, 256), rng.uniform(0, 256), rng.uniform(0, 256)) : singlePointColor );
         }
      }
@@ -205,7 +205,7 @@ void SurfDescriptorExtractor::compute( const Mat& image,
     bool useProvidedKeypoints = true;
     surf(image, mask, keypoints, _descriptors, useProvidedKeypoints);
 
-    descriptors.create(keypoints.size(), surf.descriptorSize(), CV_32FC1);
+    descriptors.create((int)keypoints.size(), (int)surf.descriptorSize(), CV_32FC1);
     assert( (int)_descriptors.size() == descriptors.rows * descriptors.cols );
     std::copy(_descriptors.begin(), _descriptors.end(), descriptors.begin<float>());
 }
@@ -273,7 +273,7 @@ Ptr<DescriptorMatcher> createDescriptorMatcher( const string& descriptorMatcherT
 
 template<>
 void BruteForceMatcher<L2<float> >::matchImpl( const Mat& descriptors_1, const Mat& descriptors_2,
-                                             const Mat& mask, vector<int>& matches ) const
+                                             const Mat& /*mask*/, vector<int>& matches ) const
 {
     matches.clear();
     matches.reserve( descriptors_1.rows );
@@ -330,7 +330,7 @@ void KeyPointCollection::add( const Mat& _image, const vector<KeyPoint>& _points
     if( startIndices.empty() )
         startIndices.push_back(0);
     else
-        startIndices.push_back(*startIndices.rbegin() + points.rbegin()->size());
+        startIndices.push_back((int)(*startIndices.rbegin() + points.rbegin()->size()));
 
     // add image and keypoints
     images.push_back(_image);
@@ -457,11 +457,11 @@ void OneWayDescriptorMatch::add( const Mat& image, vector<KeyPoint>& keypoints )
 
     size_t trainFeatureCount = keypoints.size();
 
-    base->Allocate( trainFeatureCount );
+    base->Allocate( (int)trainFeatureCount );
 
     IplImage _image = image;
     for( size_t i = 0; i < keypoints.size(); i++ )
-        base->InitializeDescriptor( i, &_image, keypoints[i], "" );
+        base->InitializeDescriptor( (int)i, &_image, keypoints[i], "" );
 
     collection.add( Mat(), keypoints );
 
@@ -478,7 +478,7 @@ void OneWayDescriptorMatch::add( KeyPointCollection& keypoints )
 
     size_t trainFeatureCount = keypoints.calcKeypointCount();
 
-    base->Allocate( trainFeatureCount );
+    base->Allocate( (int)trainFeatureCount );
 
     int count = 0;
     for( size_t i = 0; i < keypoints.points.size(); i++ )
@@ -517,19 +517,17 @@ void OneWayDescriptorMatch::match( const Mat& image, vector<KeyPoint>& points, v
         int poseIdx = -1;
 
         DMatch match;
-        match.indexQuery = i;
+        match.indexQuery = (int)i;
         match.indexTrain = -1;
         base->FindDescriptor( &_image, points[i].pt, match.indexTrain, poseIdx, match.distance );
         matches[i] = match;
     }
 }
 
-void OneWayDescriptorMatch::match( const Mat& image, vector<KeyPoint>& points, vector<vector<DMatch> >& matches, float threshold )
+void OneWayDescriptorMatch::match( const Mat& image, vector<KeyPoint>& points, vector<vector<DMatch> >& matches, float /*threshold*/ )
 {
     matches.clear();
     matches.resize( points.size() );
-    IplImage _image = image;
-
 
     vector<DMatch> dmatches;
     match( image, points, dmatches );
@@ -538,7 +536,6 @@ void OneWayDescriptorMatch::match( const Mat& image, vector<KeyPoint>& points, v
         matches[i].push_back( dmatches[i] );
     }
 
-
     /*
     printf("Start matching %d points\n", points.size());
     //std::cout << "Start matching " << points.size() << "points\n";
@@ -686,7 +683,7 @@ void CalonderDescriptorMatch::calcBestProbAndMatchIdx( const Mat& image, const P
 
     bestProb = 0;
     bestMatchIdx = -1;
-    for( size_t ci = 0; ci < (size_t)classifier->classes(); ci++ )
+    for( int ci = 0; ci < classifier->classes(); ci++ )
     {
         if( signature[ci] > bestProb )
         {
@@ -764,7 +761,7 @@ void CalonderDescriptorMatch::read( const FileNode &fn )
     params.patchSize = fn["patchSize"];
     params.reducedNumDim = (int) fn["reducedNumDim"];
     params.numQuantBits = fn["numQuantBits"];
-    params.printStatus = (int) fn["printStatus"];
+    params.printStatus = (int) fn["printStatus"] != 0;
 }
 
 void CalonderDescriptorMatch::write( FileStorage& fs ) const
@@ -839,7 +836,7 @@ void FernDescriptorMatch::trainFernClassifier()
             {
                 refimgs.push_back(new Mat (collection.images[imageIdx]));
                 points.push_back(collection.points[imageIdx][pointIdx].pt);
-                labels.push_back(pointIdx);
+                labels.push_back((int)pointIdx);
             }
         }
 
@@ -856,7 +853,7 @@ void FernDescriptorMatch::calcBestProbAndMatchIdx( const Mat& image, const Point
 
     bestProb = -FLT_MAX;
     bestMatchIdx = -1;
-    for( size_t ci = 0; ci < (size_t)classifier->getClassCount(); ci++ )
+    for( int ci = 0; ci < classifier->getClassCount(); ci++ )
     {
         if( signature[ci] > bestProb )
         {
@@ -888,7 +885,7 @@ void FernDescriptorMatch::match( const Mat& image, vector<KeyPoint>& keypoints,
     matches.resize( keypoints.size() );
     vector<float> signature( (size_t)classifier->getClassCount() );
 
-    for( size_t pi = 0; pi < keypoints.size(); pi++ )
+    for( int pi = 0; pi < (int)keypoints.size(); pi++ )
     {
         matches[pi].indexQuery = pi;
         calcBestProbAndMatchIdx( image, keypoints[pi].pt, matches[pi].distance, matches[pi].indexTrain, signature );
@@ -904,14 +901,14 @@ void FernDescriptorMatch::match( const Mat& image, vector<KeyPoint>& keypoints,
     matches.resize( keypoints.size() );
     vector<float> signature( (size_t)classifier->getClassCount() );
 
-    for( size_t pi = 0; pi < keypoints.size(); pi++ )
+    for( int pi = 0; pi < (int)keypoints.size(); pi++ )
     {
         (*classifier)( image, keypoints[pi].pt, signature);
 
         DMatch match;
         match.indexQuery = pi;
 
-        for( size_t ci = 0; ci < (size_t)classifier->getClassCount(); ci++ )
+        for( int ci = 0; ci < classifier->getClassCount(); ci++ )
         {
             if( -signature[ci] < threshold )
             {
index 6e16f28..6cee854 100644 (file)
@@ -110,7 +110,7 @@ void GoodFeaturesToTrackDetector::read (const FileNode& fn)
     qualityLevel = fn["qualityLevel"];
     minDistance = fn["minDistance"];
     blockSize = fn["blockSize"];
-    useHarrisDetector = (int) fn["useHarrisDetector"];
+    useHarrisDetector = (int)fn["useHarrisDetector"] != 0;
     k = fn["k"];
 }
 
@@ -135,7 +135,7 @@ void GoodFeaturesToTrackDetector::detectImpl( const Mat& image, const Mat& mask,
     vector<KeyPoint>::iterator keypoint_it = keypoints.begin();
     for( ; corner_it != corners.end(); ++corner_it, ++keypoint_it )
     {
-        *keypoint_it = KeyPoint( *corner_it, blockSize );
+        *keypoint_it = KeyPoint( *corner_it, (float)blockSize );
     }
 }
 
@@ -143,7 +143,7 @@ void GoodFeaturesToTrackDetector::detectImpl( const Mat& image, const Mat& mask,
     MserFeatureDetector
 */
 MserFeatureDetector::MserFeatureDetector( int delta, int minArea, int maxArea,
-                                          float maxVariation, float minDiversity,
+                                          double maxVariation, double minDiversity,
                                           int maxEvolution, double areaThreshold,
                                           double minMargin, int edgeBlurSize )
   : mser( delta, minArea, maxArea, maxVariation, minDiversity,
index a20a1d7..a9e5d0a 100644 (file)
@@ -3000,7 +3000,7 @@ static void makeOffsets(int pixel[], int row_stride)
 static void fast9ComputeScores(const Mat& img, vector<Point>& corners, vector<int>& scores, int b)
 {   
     int pixel[16];
-    makeOffsets(pixel, img.step);
+    makeOffsets(pixel, (int)img.step);
     size_t n, num_corners = corners.size();
 
     scores.resize(num_corners);
@@ -3013,7 +3013,7 @@ static void fast9Detect(const Mat& img, vector<Point>& ret_corners, int b)
 {
     int xsize = img.cols, ysize = img.rows;
     int pixel[16];
-    makeOffsets(pixel, img.step);
+    makeOffsets(pixel, (int)img.step);
 
     ret_corners.reserve(512);
 
index f723eeb..58ab798 100644 (file)
@@ -1270,12 +1270,12 @@ MSER::MSER()
 }
 
 MSER::MSER( int _delta, int _min_area, int _max_area,
-      float _max_variation, float _min_diversity,
+      double _max_variation, double _min_diversity,
       int _max_evolution, double _area_threshold,
       double _min_margin, int _edge_blur_size )
 {
-    *(CvMSERParams*)this = cvMSERParams(_delta, _min_area, _max_area, _max_variation,
-        _min_diversity, _max_evolution, _area_threshold, _min_margin, _edge_blur_size);
+    *(CvMSERParams*)this = cvMSERParams(_delta, _min_area, _max_area, (float)_max_variation,
+        (float)_min_diversity, _max_evolution, _area_threshold, _min_margin, _edge_blur_size);
 }
 
 void MSER::operator()( const Mat& image, vector<vector<Point> >& dstcontours, const Mat& mask ) const
index 97c401a..3e57dbd 100644 (file)
@@ -1704,13 +1704,13 @@ namespace cv{
 
         for (int i = 0; i < patch_count; i++)
         {
-            float sum = cvSum(patches[i]).val[0];
+            float nf = (float)(1./cvSum(patches[i]).val[0]);
             for (int y = 0; y < height; y++)
             {
                 for (int x = 0; x < width; x++)
                 {
                     *((float*)(data->data.ptr + data->step * i) + y * width + x)
-                            = (float)(unsigned char)patches[i]->imageData[y * patches[i]->widthStep + x] / sum;
+                            = (unsigned char)patches[i]->imageData[y * patches[i]->widthStep + x] * nf;
                 }
             }
         }
index fc062be..0ee4d30 100644 (file)
@@ -440,7 +440,7 @@ void LDetector::operator()(const vector<Mat>& pyr, vector<KeyPoint>& keypoints,
     const int lthreshold = 3;
     int L, x, y, i, j, k, tau = lthreshold;
     Mat scoreBuf(pyr[0].size(), CV_16S), maskBuf(pyr[0].size(), CV_8U);
-    int scoreElSize = scoreBuf.elemSize();
+    int scoreElSize = (int)scoreBuf.elemSize();
     vector<Point> circle0;
     vector<int> fhcircle0, circle, fcircle_s, fcircle;
     getDiscreteCircle(radius, circle0, fhcircle0);
@@ -461,13 +461,13 @@ void LDetector::operator()(const vector<Mat>& pyr, vector<KeyPoint>& keypoints,
         Mat scoreLayer(layerSize, scoreBuf.type(), scoreBuf.data);
         Mat maskLayer(layerSize, maskBuf.type(), maskBuf.data);
         const Mat& pyrLayer = pyr[L];
-        int sstep = scoreLayer.step/sizeof(short);
-        int mstep = maskLayer.step;
+        int sstep = (int)(scoreLayer.step/sizeof(short));
+        int mstep = (int)maskLayer.step;
 
         int csize = (int)circle0.size(), csize2 = csize/2;
         circle.resize(csize*3);
         for( i = 0; i < csize; i++ )
-            circle[i] = circle[i+csize] = circle[i+csize*2] = (-circle0[i].y)*pyrLayer.step + circle0[i].x;
+            circle[i] = circle[i+csize] = circle[i+csize*2] = (int)((-circle0[i].y)*pyrLayer.step + circle0[i].x);
         fcircle.clear();
         fcircle_s.clear();
         for( i = -radius; i <= radius; i++ )
@@ -476,7 +476,7 @@ void LDetector::operator()(const vector<Mat>& pyr, vector<KeyPoint>& keypoints,
             for( j = -x; j <= x; j++ )
             {
                 fcircle_s.push_back(i*sstep + j);
-                fcircle.push_back(i*pyrLayer.step + j);
+                fcircle.push_back((int)(i*pyrLayer.step + j));
             }
         }
         int nsize = (int)fcircle.size();
@@ -492,7 +492,7 @@ void LDetector::operator()(const vector<Mat>& pyr, vector<KeyPoint>& keypoints,
             memset( maskLayer.ptr<uchar>(layerSize.height-y-1), 0, layerSize.width );
         }
 
-        int vradius = radius*pyrLayer.step;
+        int vradius = (int)(radius*pyrLayer.step);
 
         for( y = radius; y < layerSize.height - radius; y++ )
         {
@@ -785,7 +785,7 @@ int FernClassifier::getLeaf(int fern, const Mat& _patch) const
         idx = (idx << 1) + f(patch);
     }
 
-    return fern*leavesPerStruct + idx;
+    return (int)(fern*leavesPerStruct + idx);
 }
 
 
@@ -1237,7 +1237,7 @@ void PlanarObjectDetector::train(const vector<Mat>& pyr, int npoints,
     ldetector.setVerbose(verbose);
     ldetector.getMostStable2D(pyr[0], modelPoints, npoints, patchGenerator);
 
-    npoints = modelPoints.size();
+    npoints = (int)modelPoints.size();
     fernClassifier.setVerbose(verbose);
     fernClassifier.trainFromSingleView(pyr[0], modelPoints,
                                        patchSize, (int)modelPoints.size(), nstructs, structSize, nviews,
index 0e82b87..8420c47 100644 (file)
 
 #define log2(a) (log((a))/CV_LOG2)
 
+#if defined _MSC_VER && _MSC_VER >= 1400
+#pragma warning(disable: 4100 4244 4267 4305)
+#endif
+
 /*
  * from sift.hpp of original code
  */
index ece953c..313bea2 100644 (file)
@@ -184,7 +184,7 @@ bool  BmpDecoder::readHeader()
 bool  BmpDecoder::readData( Mat& img )
 {
     uchar* data = img.data;
-    int step = img.step;
+    int step = (int)img.step;
     bool color = img.channels() > 1;
     uchar  gray_palette[256];
     bool   result = false;
index 85029d7..68e3af2 100644 (file)
@@ -370,7 +370,7 @@ bool  JpegDecoder::readData( Mat& img )
 {
     bool result = false;
     uchar* data = img.data;
-    int step = img.step;
+    int step = (int)img.step;
     bool color = img.channels() > 1;
     JpegState* state = (JpegState*)m_state;
 
index 79349cb..f190835 100644 (file)
@@ -161,7 +161,7 @@ bool  Jpeg2KDecoder::readData( Mat& img )
     bool result = false;
     int color = img.channels() > 1;
     uchar* data = img.data;
-    int step = img.step;
+    int step = (int)img.step;
     jas_stream_t* stream = (jas_stream_t*)m_stream;
     jas_image_t* image = (jas_image_t*)m_image;
 
index 440cd40..79fa89e 100644 (file)
@@ -192,7 +192,7 @@ bool  PngDecoder::readData( Mat& img )
     uchar** buffer = _buffer;
     int color = img.channels() > 1;
     uchar* data = img.data;
-    int step = img.step;
+    int step = (int)img.step;
 
     if( m_png_ptr && m_info_ptr && m_end_info && m_width && m_height )
     {
@@ -223,7 +223,7 @@ bool  PngDecoder::readData( Mat& img )
                 png_set_palette_to_rgb( png_ptr );
 
             if( m_color_type == PNG_COLOR_TYPE_GRAY && m_bit_depth < 8 )
-                png_set_gray_1_2_4_to_8( png_ptr );
+                png_set_expand_gray_1_2_4_to_8( png_ptr );
 
             if( CV_MAT_CN(m_type) > 1 && color )
                 png_set_bgr( png_ptr ); // convert RGB to BGR
index f5b77a6..015f6d5 100644 (file)
@@ -190,7 +190,7 @@ bool  PxMDecoder::readData( Mat& img )
 {
     int color = img.channels() > 1;
     uchar* data = img.data;
-    int step = img.step;
+    int step = (int)img.step;
     PaletteEntry palette[256];
     bool   result = false;
     int  bit_depth = CV_ELEM_SIZE1(m_type)*8;
@@ -372,9 +372,9 @@ bool  PxMEncoder::write( const Mat& img, const vector<int>& params )
     bool isBinary = true;
 
     int  width = img.cols, height = img.rows;
-    int  _channels = img.channels(), depth = img.elemSize1()*8;
+    int  _channels = img.channels(), depth = (int)img.elemSize1()*8;
     int  channels = _channels > 1 ? 3 : 1;
-    int  fileStep = width*img.elemSize();
+    int  fileStep = width*(int)img.elemSize();
     int  x, y;
 
     for( size_t i = 0; i < params.size(); i += 2 )
@@ -399,7 +399,7 @@ bool  PxMEncoder::write( const Mat& img, const vector<int>& params )
     int  bufferSize = 128; // buffer that should fit a header
 
     if( isBinary )
-        lineLength = width * img.elemSize();
+        lineLength = width * (int)img.elemSize();
     else
         lineLength = (6 * channels + (channels > 1 ? 2 : 0)) * width + 32;
 
index 4008211..19f8db4 100644 (file)
@@ -156,7 +156,7 @@ bool  SunRasterDecoder::readData( Mat& img )
 {
     int color = img.channels() > 1;
     uchar* data = img.data;
-    int step = img.step;
+    int step = (int)img.step;
     uchar  gray_palette[256];
     bool   result = false;
     int  src_pitch = ((m_width*m_bpp + 7)/8 + 1) & -2;
index 9be2393..357f83b 100644 (file)
@@ -147,7 +147,7 @@ bool  TiffDecoder::readData( Mat& img )
     bool result = false;
     bool color = img.channels() > 1;
     uchar* data = img.data;
-    int step = img.step;
+    int step = (int)img.step;
 
     if( m_tif && m_width && m_height )
     {
index 1516e8f..aa1603c 100644 (file)
@@ -412,7 +412,7 @@ void cvSetModeWindow_W32( const char* name, double prop_value)//Yannick Verdie
                EXIT;
 
        {
-               DWORD dwStyle = GetWindowLongPtr(window->frame, GWL_STYLE);
+               DWORD dwStyle = (DWORD)GetWindowLongPtr(window->frame, GWL_STYLE);
                CvRect position;
 
                if (window->status==CV_WINDOW_FULLSCREEN && prop_value==CV_WINDOW_NORMAL)
index a9895dc..c263b5e 100644 (file)
@@ -3065,7 +3065,7 @@ void invertAffineTransform(const Mat& matM, Mat& _iM)
     {
         const float* M = (const float*)matM.data;
         float* iM = (float*)_iM.data;
-        int step = matM.step/sizeof(M[0]), istep = _iM.step/sizeof(iM[0]);
+        int step = (int)(matM.step/sizeof(M[0])), istep = (int)(_iM.step/sizeof(iM[0]));
         
         double D = M[0]*M[step+1] - M[1]*M[step];
         D = D != 0 ? 1./D : 0;
@@ -3080,7 +3080,7 @@ void invertAffineTransform(const Mat& matM, Mat& _iM)
     {
         const double* M = (const double*)matM.data;
         double* iM = (double*)_iM.data;
-        int step = matM.step/sizeof(M[0]), istep = _iM.step/sizeof(iM[0]);
+        int step = (int)(matM.step/sizeof(M[0])), istep = (int)(_iM.step/sizeof(iM[0]));
         
         double D = M[0]*M[step+1] - M[1]*M[step];
         D = D != 0 ? 1./D : 0;
index 807274d..61c2a94 100644 (file)
@@ -364,7 +364,7 @@ cvMinAreaRect2( const CvArr* array, CvMemStorage* storage )
     if( CV_IS_SEQ(ptseq) )
     {
         if( !CV_IS_SEQ_POINT_SET(ptseq) &&
-            (CV_SEQ_KIND(ptseq) != CV_SEQ_KIND_CURVE || !CV_IS_SEQ_CONVEX(ptseq) ||
+            (CV_SEQ_KIND(ptseq) != CV_SEQ_KIND_CURVE ||
             CV_SEQ_ELTYPE(ptseq) != CV_SEQ_ELTYPE_PPOINT ))
             CV_Error( CV_StsUnsupportedFormat,
                 "Input sequence must consist of 2d points or pointers to 2d points" );
@@ -385,31 +385,7 @@ cvMinAreaRect2( const CvArr* array, CvMemStorage* storage )
         temp_storage = cvCreateMemStorage(1 << 10);
     }
 
-    if( !CV_IS_SEQ_CONVEX( ptseq ))
-    {
-        ptseq = cvConvexHull2( ptseq, temp_storage, CV_CLOCKWISE, 1 );
-    }
-    else if( !CV_IS_SEQ_POINT_SET( ptseq ))
-    {
-        CvSeqWriter writer;
-        
-        if( !CV_IS_SEQ(ptseq->v_prev) || !CV_IS_SEQ_POINT_SET(ptseq->v_prev))
-            CV_Error( CV_StsBadArg,
-            "Convex hull must have valid pointer to point sequence stored in v_prev" );
-        cvStartReadSeq( ptseq, &reader );
-        cvStartWriteSeq( CV_SEQ_KIND_CURVE|CV_SEQ_FLAG_CONVEX|CV_SEQ_ELTYPE(ptseq->v_prev),
-                         sizeof(CvContour), CV_ELEM_SIZE(ptseq->v_prev->flags),
-                         temp_storage, &writer );
-            
-        for( i = 0; i < ptseq->total; i++ )
-        {
-            CvPoint pt = **(CvPoint**)(reader.ptr);
-            CV_WRITE_SEQ_ELEM( pt, writer );
-        }
-
-        ptseq = cvEndWriteSeq( &writer );
-    }
-
+    ptseq = cvConvexHull2( ptseq, temp_storage, CV_CLOCKWISE, 1 );
     n = ptseq->total;
 
     _points.allocate(n);
index 0d0bd7d..17def20 100644 (file)
@@ -384,7 +384,7 @@ cvPyrMeanShiftFiltering( const CvArr* srcarr, CvArr* dstarr,
         cv::Mat src = src_pyramid[level];
         cv::Size size = src.size();
         uchar* sptr = src.data;
-        int sstep = src.step;
+        int sstep = (int)src.step;
         uchar* mask = 0;
         int mstep = 0;
         uchar* dptr;
@@ -396,9 +396,9 @@ cvPyrMeanShiftFiltering( const CvArr* srcarr, CvArr* dstarr,
         {
             cv::Size size1 = dst_pyramid[level+1].size();
             cv::Mat m( size.height, size.width, CV_8UC1, mask0.data );
-            dstep = dst_pyramid[level+1].step;
+            dstep = (int)dst_pyramid[level+1].step;
             dptr = dst_pyramid[level+1].data + dstep + cn;
-            mstep = m.step;
+            mstep = (int)m.step;
             mask = m.data + mstep;
             //cvResize( dst_pyramid[level+1], dst_pyramid[level], CV_INTER_CUBIC );
             cv::pyrUp( dst_pyramid[level+1], dst_pyramid[level] );
@@ -419,7 +419,7 @@ cvPyrMeanShiftFiltering( const CvArr* srcarr, CvArr* dstarr,
         }
 
         dptr = dst_pyramid[level].data;
-        dstep = dst_pyramid[level].step;
+        dstep = (int)dst_pyramid[level].step;
 
         for( i = 0; i < size.height; i++, sptr += sstep - size.width*3,
                                           dptr += dstep - size.width*3,
index 7745dc4..37fb17c 100644 (file)
@@ -47,7 +47,6 @@
 #include "precomp.hpp"
 #include "_vectrack.h"
 
-#define _ASSERT     assert
 #define NUM_FACE_ELEMENTS   3
 enum 
 {
@@ -426,9 +425,9 @@ void CvFaceElement::Energy()
 CV_IMPL CvFaceTracker*
 cvInitFaceTracker(CvFaceTracker* pFaceTracker, const IplImage* imgGray, CvRect* pRects, int nRects)
 {
-    _ASSERT(NULL != imgGray);
-    _ASSERT(NULL != pRects);
-    _ASSERT(nRects >= NUM_FACE_ELEMENTS);
+    assert(NULL != imgGray);
+    assert(NULL != pRects);
+    assert(nRects >= NUM_FACE_ELEMENTS);
     if ((NULL == imgGray) ||
         (NULL == pRects) ||
         (nRects < NUM_FACE_ELEMENTS))
@@ -460,9 +459,9 @@ cvReleaseFaceTracker(CvFaceTracker** ppFaceTracker)
 CV_IMPL int
 cvTrackFace(CvFaceTracker* pFaceTracker, IplImage* imgGray, CvRect* pRects, int nRects, CvPoint* ptRotate, double* dbAngleRotate)
 {
-    _ASSERT(NULL != pFaceTracker);
-    _ASSERT(NULL != imgGray);
-    _ASSERT(NULL != pRects && nRects >= NUM_FACE_ELEMENTS);
+    assert(NULL != pFaceTracker);
+    assert(NULL != imgGray);
+    assert(NULL != pRects && nRects >= NUM_FACE_ELEMENTS);
     if ((NULL == pFaceTracker) ||
         (NULL == imgGray))
         return FALSE;
@@ -612,8 +611,8 @@ RESTART:
 
 void ThresholdingParam(IplImage *imgGray, int iNumLayers, int &iMinLevel, int &iMaxLevel, float &step, float& power, int iHistMin /*= HIST_MIN*/)
 {
-    _ASSERT(imgGray != NULL);
-    _ASSERT(imgGray->nChannels == 1);
+    assert(imgGray != NULL);
+    assert(imgGray->nChannels == 1);
     int i, j; 
     // create histogram
     int histImg[256] = {0};
@@ -782,7 +781,7 @@ int ChoiceTrackingFace2(CvFaceTracker* pTF, const int nElements, const CvFaceEle
         face[element[2]].r.height = int(double(pTF->rTempl[element[2]].height) / (scale) + 0.5);
         face[element[2]].r.x = face[element[2]].ptCenter.x - (face[element[2]].r.width + 1) / 2;
         face[element[2]].r.y = face[element[2]].ptCenter.y - (face[element[2]].r.height + 1) / 2;
-        _ASSERT(face[LEYE].r.x + face[LEYE].r.width <= face[REYE].r.x);
+        assert(face[LEYE].r.x + face[LEYE].r.width <= face[REYE].r.x);
     }
     return found;
 } // int ChoiceTrackingFace3(const CvTrackingRect* tr_face, CvTrackingRect* new_face, int& new_energy)
@@ -860,8 +859,8 @@ inline double CalculateTransformationLMS3( CvPoint* pTemplPoints,
     double dbAverageShiftY = 0;
     double dbLMS = 0;
 
-    _ASSERT( NULL != pTemplPoints);
-    _ASSERT( NULL != pSrcPoints);
+    assert( NULL != pTemplPoints);
+    assert( NULL != pSrcPoints);
 
     double dbXt = double(pTemplPoints[0].x + pTemplPoints[1].x + pTemplPoints[2].x) / 3.0;
     double dbYt = double(pTemplPoints[0].y + pTemplPoints[1].y + pTemplPoints[2].y ) / 3.0;
@@ -919,7 +918,7 @@ inline double CalculateTransformationLMS3( CvPoint* pTemplPoints,
     if( pdbAverageShiftX != NULL ) *pdbAverageShiftX = dbAverageShiftX;
     if( pdbAverageShiftY != NULL ) *pdbAverageShiftY = dbAverageShiftY;
     
-    _ASSERT(dbLMS >= 0);
+    assert(dbLMS >= 0);
     return dbLMS;
 }
 
@@ -927,8 +926,8 @@ inline double CalculateTransformationLMS3_0( CvPoint* pTemplPoints, CvPoint* pSr
 {
     double dbLMS = 0;
 
-    _ASSERT( NULL != pTemplPoints);
-    _ASSERT( NULL != pSrcPoints);
+    assert( NULL != pTemplPoints);
+    assert( NULL != pSrcPoints);
 
     double dbXt = double(pTemplPoints[0].x + pTemplPoints[1].x + pTemplPoints[2].x) / 3.0;
     double dbYt = double(pTemplPoints[0].y + pTemplPoints[1].y + pTemplPoints[2].y ) / 3.0;
index 9d447d0..e6563ba 100644 (file)
@@ -939,7 +939,7 @@ struct HaarDetectObjects_ScaleCascade_Invoker
         const int *p0 = p[0], *p1 = p[1], *p2 = p[2], *p3 = p[3];
         const int *pq0 = pq[0], *pq1 = pq[1], *pq2 = pq[2], *pq3 = pq[3];
         bool doCannyPruning = p0 != 0;
-        int sstep = sumstep/sizeof(p0[0]);
+        int sstep = (int)(sumstep/sizeof(p0[0]));
         
         for( iy = startY; iy < endY; iy++ )
         {
index dda2a77..0dc0a05 100644 (file)
@@ -557,9 +557,9 @@ void CvCascadeBoostTrainData::precalculate()
 
     double proctime = -TIME( 0 );
     parallel_for( BlockedRange(numPrecalcVal, numPrecalcIdx),
-                  FeatureIdxOnlyPrecalc(featureEvaluator, buf, sample_count, is_buf_16u) );
+                  FeatureIdxOnlyPrecalc(featureEvaluator, buf, sample_count, is_buf_16u!=0) );
     parallel_for( BlockedRange(0, minNum),
-                  FeatureValAndIdxPrecalc(featureEvaluator, buf, &valCache, sample_count, is_buf_16u) );
+                  FeatureValAndIdxPrecalc(featureEvaluator, buf, &valCache, sample_count, is_buf_16u!=0) );
     parallel_for( BlockedRange(minNum, numPrecalcVal),
                   FeatureValOnlyPrecalc(featureEvaluator, &valCache, sample_count) );
     cout << "Precalculation time: " << (proctime + TIME( 0 )) << endl;
index 708f031..5771858 100755 (executable)
@@ -401,13 +401,13 @@ public:
 
     inline int NumCameras()
     {
-        return m_cameras.size();
+        return (int)m_cameras.size();
     }
 
     inline int AddCamera(Camera* cam)
     {
         m_cameras.push_back(cam);
-        return m_cameras.size();
+        return (int)m_cameras.size();
     }    
     
     RigidBody* GetBody()
index 35dea06..31752db 100644 (file)
@@ -961,7 +961,7 @@ void calcdfdx( const vector<vector<Point2f> >& leftF, const vector<vector<Point2
        CV_Assert( !leftF.empty() && !rightF.empty() && !leftF[0].empty() && !rightF[0].empty() );\r
        CV_Assert( leftF[0].size() ==  rightF[0].size() );\r
        CV_Assert( fabs(eps) > std::numeric_limits<double>::epsilon() );\r
-       int fcount = leftF[0].size(), xdim = leftF.size();\r
+       int fcount = (int)leftF[0].size(), xdim = (int)leftF.size();\r
 \r
        dfdx.create( fcount*fdim, xdim, CV_64FC1 );\r
 \r
@@ -1397,7 +1397,7 @@ void CV_StereoCalibrationTest::run( int )
                        return;\r
                }\r
 \r
-               size_t nframes = imglist.size()/2;\r
+               int nframes = (int)(imglist.size()/2);\r
                int npoints = patternSize.width*patternSize.height;\r
                vector<vector<Point3f> > objpt(nframes);\r
                vector<vector<Point2f> > imgpt1(nframes);\r
@@ -1426,7 +1426,7 @@ void CV_StereoCalibrationTest::run( int )
                                ts->set_failed_test_info( CvTS::FAIL_INVALID_OUTPUT );\r
                                return;\r
                        }\r
-                       total += imgpt1[i].size();\r
+                       total += (int)imgpt1[i].size();\r
                        for( int j = 0; j < npoints; j++ )\r
                                objpt[i].push_back(Point3f((float)(j%patternSize.width), (float)(j/patternSize.width), 0.f));\r
                }\r
@@ -1514,7 +1514,7 @@ void CV_StereoCalibrationTest::run( int )
 \r
                bool verticalStereo = abs(P2.at<double>(0,3)) < abs(P2.at<double>(1,3));\r
                double maxDiff_c = 0, maxDiff_uc = 0;\r
-               for( size_t i = 0, k = 0; i < nframes; i++ )\r
+               for( int i = 0, k = 0; i < nframes; i++ )\r
                {\r
                        vector<Point2f> temp[2];\r
                        undistortPoints(Mat(imgpt1[i]), temp[0], M1, D1, R1, P1);\r
@@ -1592,10 +1592,10 @@ double CV_StereoCalibrationTest_C::calibrateStereoCamera( const vector<vector<Po
        E.create(3, 3, CV_64F);\r
        F.create(3, 3, CV_64F);\r
 \r
-       int  nimages = objectPoints.size(), total = 0;\r
-       for( int i = 0; i < (int)objectPoints.size(); i++ )\r
+       int  nimages = (int)objectPoints.size(), total = 0;\r
+       for( int i = 0; i < nimages; i++ )\r
        {\r
-               total += objectPoints[i].size();\r
+               total += (int)objectPoints[i].size();\r
        }\r
 \r
        Mat npoints( 1, nimages, CV_32S ),\r
@@ -1608,7 +1608,7 @@ double CV_StereoCalibrationTest_C::calibrateStereoCamera( const vector<vector<Po
        Point2f* imgPtData = imgPt.ptr<Point2f>();\r
        for( int i = 0, ni = 0, j = 0; i < nimages; i++, j += ni )\r
        {\r
-               ni = objectPoints[i].size();\r
+               ni = (int)objectPoints[i].size();\r
                ((int*)npoints.data)[i] = ni;\r
                copy(objectPoints[i].begin(), objectPoints[i].end(), objPtData + j);\r
                copy(imagePoints1[i].begin(), imagePoints1[i].end(), imgPtData + j);\r
index d1d129b..e5f3abc 100644 (file)
@@ -90,7 +90,7 @@ public:
     }\r
     ~CV_CalibrateCameraArtificialTest() {}\r
 protected:     \r
-    size_t r;\r
+    int r;\r
 \r
     const static int JUST_FIND_CORNERS = 0;\r
     const static int USE_CORNERS_SUBPIX = 1;\r
@@ -370,7 +370,7 @@ protected:
         RNG& rng = theRNG();\r
 \r
         int progress = 0;\r
-        size_t repeat_num = 3;\r
+        int repeat_num = 3;\r
         for(r = 0; r < repeat_num; ++r)\r
         {                        \r
             const int brds_num = 20;              \r
index 95aba1e..048a139 100644 (file)
@@ -67,7 +67,7 @@ void show_points( const Mat& gray, const Mat& u, const vector<Point2f>& v, Size
     }
     if (!v.empty())
     {
-        Mat corners(v.size(), 1, CV_32FC2, (void*)&v[0]);     
+        Mat corners((int)v.size(), 1, CV_32FC2, (void*)&v[0]);     
         drawChessboardCorners( rgb, pattern_size, corners, was_found );
     }
     //namedWindow( "test", 0 ); imshow( "test", rgb ); waitKey(0);
@@ -92,14 +92,14 @@ CV_ChessboardDetectorTest::CV_ChessboardDetectorTest():
 
 double calcError(const vector<Point2f>& v, const Mat& u)
 {
-    size_t count_exp = static_cast<size_t>(u.cols * u.rows);
+    int count_exp = u.cols * u.rows;
     const Point2f* u_data = u.ptr<Point2f>();
 
     double err = numeric_limits<double>::max();
-    for( size_t k = 0; k < 2; ++k )
+    for( int k = 0; k < 2; ++k )
     {
         double err1 = 0;
-        for(size_t j = 0; j < count_exp; ++j )
+        for( int j = 0; j < count_exp; ++j )
         {
             int j1 = k == 0 ? j : count_exp - j - 1;
             double dx = fabs( v[j].x - u_data[j1].x );
@@ -324,7 +324,7 @@ bool CV_ChessboardDetectorTest::checkByGenerator()
     const size_t sizes_num = sizeof(sizes)/sizeof(sizes[0]);                
     const size_t test_num = 16;    
     int progress = 0;
-    for(size_t i = 0; i < test_num; ++i)
+    for(int i = 0; i < test_num; ++i)
     {          
         progress = update_progress( progress, i, test_num, 0 );
         ChessBoardGenerator cbg(sizes[i % sizes_num]);
index e6d0b32..4d4bf4d 100644 (file)
@@ -58,7 +58,7 @@ public:
     {                \r
         dr3_dr1.create(3, 3);     dt3_dr1.create(3, 3);\r
                     \r
-        for(size_t i = 0; i < 3; ++i) \r
+        for(int i = 0; i < 3; ++i) \r
         {\r
             ev.setTo(Scalar(0));    ev(i, 0) = eps;                        \r
                         \r
@@ -75,7 +75,7 @@ public:
     {                \r
         dr3_dr2.create(3, 3);     dt3_dr2.create(3, 3);\r
                     \r
-        for(size_t i = 0; i < 3; ++i) \r
+        for(int i = 0; i < 3; ++i) \r
         {\r
             ev.setTo(Scalar(0));    ev(i, 0) = eps;                        \r
                         \r
@@ -92,7 +92,7 @@ public:
     {                \r
         drt3_dt1.create(3, 3);     dt3_dt1.create(3, 3);\r
                     \r
-        for(size_t i = 0; i < 3; ++i) \r
+        for(int i = 0; i < 3; ++i) \r
         {\r
             ev.setTo(Scalar(0));    ev(i, 0) = eps;                        \r
                         \r
@@ -109,7 +109,7 @@ public:
     {                \r
         dr3_dt2.create(3, 3);     dt3_dt2.create(3, 3);\r
                     \r
-        for(size_t i = 0; i < 3; ++i) \r
+        for(int i = 0; i < 3; ++i) \r
         {\r
             ev.setTo(Scalar(0));    ev(i, 0) = eps;                        \r
                         \r
index 698e41a..7267968 100644 (file)
@@ -75,7 +75,7 @@ int calcDistance(const vector<Point2f>& set1, const vector<Point2f>& set2, doubl
         double min_dist = std::numeric_limits<double>::max();
         int min_idx = -1;
         
-        for(size_t j = 0; j < set2.size(); j++)
+        for(int j = 0; j < (int)set2.size(); j++)
         {
             double dist = norm(set1[i] - set2[j]);
             if(dist < min_dist)
@@ -185,7 +185,7 @@ void CV_ChessboardSubpixelTest::run( int )
         
         IplImage chessboard_image_header = chessboard_image;
         cvFindCornerSubPix(&chessboard_image_header, (CvPoint2D32f*)&test_corners[0], 
-            test_corners.size(), cvSize(3, 3), cvSize(1, 1), cvTermCriteria(CV_TERMCRIT_EPS|CV_TERMCRIT_ITER,300,0.1));
+            (int)test_corners.size(), cvSize(3, 3), cvSize(1, 1), cvTermCriteria(CV_TERMCRIT_EPS|CV_TERMCRIT_ITER,300,0.1));
         find4QuadCornerSubpix(chessboard_image, test_corners, Size(5, 5));
         
         double dist2 = 0.0;
index 34ef319..646f8e6 100644 (file)
@@ -101,12 +101,12 @@ protected:
     {\r
         double tmp, sum = 0;\r
         double nsum = 0;\r
-        for(size_t i = 0; i < 3; ++i)\r
+        for(int i = 0; i < 3; ++i)\r
         {\r
-            tmp = static_cast<double>(v1[i]);\r
+            tmp = v1[i];\r
             nsum +=  tmp * tmp;            \r
 \r
-            tmp = tmp - static_cast<double>(v2[i]);\r
+            tmp = tmp - v2[i];\r
             sum += tmp * tmp;\r
             \r
         }        \r
index 5d4310d..4899f98 100755 (executable)
@@ -438,7 +438,7 @@ void CV_StereoMatchingTest::run(int)
         resFS << "stereo_matching" << "{";
     }
 
-    int progress = 0, caseCount = caseNames.size();
+    int progress = 0, caseCount = (int)caseNames.size();
     for( int ci = 0; ci < caseCount; ci++)
     {
         progress = update_progress( progress, ci, caseCount, 0 );
index 046ed4a..283f9ed 100644 (file)
@@ -83,7 +83,7 @@ protected:
                 cvFindChessboardCorners( &arr, pattern_size, out_corners, out_corner_count, flags );\r
             else\r
                 cvDrawChessboardCorners( &drawCorImg, pattern_size, \r
-                    (CvPoint2D32f*)&corners[0], corners.size(), was_found);\r
+                    (CvPoint2D32f*)&corners[0], (int)corners.size(), was_found);\r
     }\r
 };\r
 \r
index 8dbe43f..bf3a656 100644 (file)
@@ -94,8 +94,8 @@ void CV_FastTest::run( int )
         cv::circle(image2, kp.pt, cvRound(kp.size/2), CV_RGB(255, 0, 0));        \r
     }\r
 \r
-    Mat kps1(1, keypoints1.size() * sizeof(KeyPoint), CV_8U, &keypoints1[0]);\r
-    Mat kps2(1, keypoints2.size() * sizeof(KeyPoint), CV_8U, &keypoints2[0]);\r
+    Mat kps1(1, (int)(keypoints1.size() * sizeof(KeyPoint)), CV_8U, &keypoints1[0]);\r
+    Mat kps2(1, (int)(keypoints2.size() * sizeof(KeyPoint)), CV_8U, &keypoints2[0]);\r
 \r
     FileStorage fs(xml, FileStorage::READ);\r
     if (!fs.isOpened())\r
index 91a9c34..08c02a1 100644 (file)
@@ -91,7 +91,7 @@ void CV_WatershedTest::run( int /* start_from */)
             colors.push_back(exp.ptr(p->y)[p->x] - 1);\r
     }\r
     fs.release();\r
-    const int compNum = colors.size() - 1;\r
+    const int compNum = (int)(colors.size() - 1);\r
 \r
     watershed(orig, markers);\r
 \r
index e5f56d3..e461ef5 100644 (file)
@@ -279,7 +279,7 @@ void CV_ArrayOpTest::run( int /* start_from */)
         M.copyTo(Md);
         SparseMat M3; SparseMat(Md).convertTo(M3, Md.type(), 2);
         
-        int nz1 = M.nzcount(), nz2 = M3.nzcount();
+        int nz1 = (int)M.nzcount(), nz2 = (int)M3.nzcount();
         double norm0 = norm(M, CV_C);
         double norm1 = norm(M, CV_L1);
         double norm2 = norm(M, CV_L2);
@@ -360,7 +360,7 @@ void CV_ArrayOpTest::run( int /* start_from */)
             }    
         }
         
-        int nz = M.nzcount();
+        int nz = (int)M.nzcount();
         if( nz != 0 )
         {
             errcount++;
index 5bbc8fb..ff9d7f2 100644 (file)
@@ -176,8 +176,8 @@ void CV_IOTest::run( int )
             multiply(test_mat, test_mat_scale, test_mat);
         }
         
-        CvSeq* seq = cvCreateSeq(test_mat.type(), sizeof(CvSeq),
-            test_mat.elemSize(), storage);
+        CvSeq* seq = cvCreateSeq(test_mat.type(), (int)sizeof(CvSeq),
+            (int)test_mat.elemSize(), storage);
         cvSeqPushMulti(seq, test_mat.data, test_mat.cols*test_mat.rows); 
         
         CvGraph* graph = cvCreateGraph( CV_ORIENTED_GRAPH,