Beispiel #1
0
        // Finish off an encoded strip by flushing the last
        // string and tacking on an End Of Information code.
        static bool ZIPPostEncode(TIFF tif)
        {
            ZIPState sp     = tif.tif_data as ZIPState;
            string   module = "ZIPPostEncode";
            int      state;

            sp.stream.avail_in = 0;
            do
            {
                state = zlib.deflate(sp.stream, zlib.Z_FINISH);
                switch (state)
                {
                case zlib.Z_STREAM_END:
                case zlib.Z_OK:
                    if ((int)sp.stream.avail_out != (int)tif.tif_rawdatasize)
                    {
                        tif.tif_rawcc = tif.tif_rawdatasize - sp.stream.avail_out;
                        TIFFFlushData1(tif);
                        sp.stream.out_buf   = tif.tif_rawdata;
                        sp.stream.next_out  = 0;
                        sp.stream.avail_out = tif.tif_rawdatasize;
                    }
                    break;

                default:
                    TIFFErrorExt(tif.tif_clientdata, module, "{0}: zlib error: {1}", tif.tif_name, sp.stream.msg);
                    return(false);
                }
            } while(state != zlib.Z_STREAM_END);
            return(true);
        }
Beispiel #2
0
        static bool ZIPSetupDecode(TIFF tif)
        {
            ZIPState sp     = tif.tif_data as ZIPState;
            string   module = "ZIPSetupDecode";

#if DEBUG
            if (sp == null)
            {
                throw new Exception("sp==null");
            }
#endif

            // if we were last encoding, terminate this mode
            if ((sp.state & ZSTATE.INIT_ENCODE) == ZSTATE.INIT_ENCODE)
            {
                zlib.deflateEnd(sp.stream);
                sp.state = ZSTATE.None;
            }

            if (zlib.inflateInit(sp.stream) != zlib.Z_OK)
            {
                TIFFErrorExt(tif.tif_clientdata, module, "{0}: {1}", tif.tif_name, sp.stream.msg);
                return(false);
            }
            else
            {
                sp.state |= ZSTATE.INIT_DECODE;
                return(true);
            }
        }
Beispiel #3
0
        static void ZIPCleanup(TIFF tif)
        {
            ZIPState sp = tif.tif_data as ZIPState;

#if DEBUG
            if (sp == null)
            {
                throw new Exception("sp==null");
            }
#endif

            TIFFPredictorCleanup(tif);

            tif.tif_tagmethods.vgetfield = sp.vgetparent;
            tif.tif_tagmethods.vsetfield = sp.vsetparent;

            if ((sp.state & ZSTATE.INIT_ENCODE) == ZSTATE.INIT_ENCODE)
            {
                zlib.deflateEnd(sp.stream);
                sp.state = ZSTATE.None;
            }
            else if ((sp.state & ZSTATE.INIT_DECODE) == ZSTATE.INIT_DECODE)
            {
                zlib.inflateEnd(sp.stream);
                sp.state = ZSTATE.None;
            }
            tif.tif_data = null;

            TIFFSetDefaultCompressionState(tif);
        }
Beispiel #4
0
        static bool TIFFInitZIP(TIFF tif, COMPRESSION scheme)
        {
            string module = "TIFFInitZIP";

#if DEBUG
            if (scheme != COMPRESSION.DEFLATE && scheme != COMPRESSION.ADOBE_DEFLATE)
            {
                throw new Exception("scheme!=COMPRESSION.DEFLATE&&scheme!=COMPRESSION.ADOBE_DEFLATE");
            }
#endif

            // Merge codec-specific tag information.
            if (!_TIFFMergeFieldInfo(tif, zipFieldInfo))
            {
                TIFFErrorExt(tif.tif_clientdata, module, "Merging Deflate codec-specific tags failed");
                return(false);
            }

            // Allocate state block so tag methods have storage to record values.
            ZIPState sp = null;
            try
            {
                tif.tif_data = sp = new ZIPState();
                sp.stream    = new zlib.z_stream();
            }
            catch
            {
                TIFFErrorExt(tif.tif_clientdata, module, "No space for ZIP state block");
                return(false);
            }

            // Override parent get/set field methods.
            sp.vgetparent = tif.tif_tagmethods.vgetfield;
            tif.tif_tagmethods.vgetfield = ZIPVGetField;           // hook for codec tags
            sp.vsetparent = tif.tif_tagmethods.vsetfield;
            tif.tif_tagmethods.vsetfield = ZIPVSetField;           // hook for codec tags

            // Default values for codec-specific fields
            sp.zipquality = zlib.Z_DEFAULT_COMPRESSION;           // default comp. level
            sp.state      = ZSTATE.None;

            // Install codec methods.
            tif.tif_setupdecode = ZIPSetupDecode;
            tif.tif_predecode   = ZIPPreDecode;
            tif.tif_decoderow   = ZIPDecode;
            tif.tif_decodestrip = ZIPDecode;
            tif.tif_decodetile  = ZIPDecode;
            tif.tif_setupencode = ZIPSetupEncode;
            tif.tif_preencode   = ZIPPreEncode;
            tif.tif_postencode  = ZIPPostEncode;
            tif.tif_encoderow   = ZIPEncode;
            tif.tif_encodestrip = ZIPEncode;
            tif.tif_encodetile  = ZIPEncode;
            tif.tif_cleanup     = ZIPCleanup;

            // Setup predictor setup.
            TIFFPredictorInit(tif);
            return(true);
        }
Beispiel #5
0
        static bool ZIPVGetField(TIFF tif, TIFFTAG tag, object[] ap)
        {
            ZIPState sp = tif.tif_data as ZIPState;

            switch (tag)
            {
            case TIFFTAG.ZIPQUALITY: ap[0] = sp.zipquality; break;

            default: return(sp.vgetparent(tif, tag, ap));
            }
            return(true);
        }
Beispiel #6
0
        static bool ZIPDecode(TIFF tif, byte[] op, int occ, ushort s)
        {
            ZIPState sp     = tif.tif_data as ZIPState;
            string   module = "ZIPDecode";

#if DEBUG
            if (sp == null)
            {
                throw new Exception("sp==null");
            }
            if ((sp.state & ZSTATE.INIT_DECODE) != ZSTATE.INIT_DECODE)
            {
                throw new Exception("sp.state!=ZSTATE.INIT_DECODE");
            }
#endif

            sp.stream.out_buf   = op;
            sp.stream.next_out  = 0;
            sp.stream.avail_out = (uint)occ;
            do
            {
                int state = zlib.inflate(sp.stream, zlib.Z_PARTIAL_FLUSH);
                if (state == zlib.Z_STREAM_END)
                {
                    break;
                }
                if (state == zlib.Z_DATA_ERROR)
                {
                    TIFFErrorExt(tif.tif_clientdata, module, "{0}: Decoding error at scanline {1}, {2}", tif.tif_name, tif.tif_row, sp.stream.msg);
                    if (zlib.inflateSync(sp.stream) != zlib.Z_OK)
                    {
                        return(false);
                    }
                    continue;
                }
                if (state != zlib.Z_OK)
                {
                    TIFFErrorExt(tif.tif_clientdata, module, "{0}: zlib error: {1}", tif.tif_name, sp.stream.msg);
                    return(false);
                }
            } while(sp.stream.avail_out > 0);

            if (sp.stream.avail_out != 0)
            {
                TIFFErrorExt(tif.tif_clientdata, module, "{0}: Not enough data at scanline {1} (short {2} bytes)", tif.tif_name, tif.tif_row, sp.stream.avail_out);
                return(false);
            }
            return(true);
        }
Beispiel #7
0
        // Encode a chunk of pixels.
        static bool ZIPEncode(TIFF tif, byte[] bp, int cc, ushort s)
        {
            ZIPState sp     = tif.tif_data as ZIPState;
            string   module = "ZIPEncode";

#if DEBUG
            if (sp == null)
            {
                throw new Exception("sp==null");
            }
            if ((sp.state & ZSTATE.INIT_ENCODE) != ZSTATE.INIT_ENCODE)
            {
                throw new Exception("sp.state!=ZSTATE.INIT_ENCODE");
            }
#endif

            sp.stream.in_buf   = bp;
            sp.stream.next_in  = 0;
            sp.stream.avail_in = (uint)cc;

            do
            {
                if (zlib.deflate(sp.stream, zlib.Z_NO_FLUSH) != zlib.Z_OK)
                {
                    TIFFErrorExt(tif.tif_clientdata, module, "{0}: Encoder error: {1}", tif.tif_name, sp.stream.msg);
                    return(false);
                }

                if (sp.stream.avail_out == 0)
                {
                    tif.tif_rawcc = tif.tif_rawdatasize;
                    TIFFFlushData1(tif);

                    sp.stream.out_buf   = tif.tif_rawdata;
                    sp.stream.next_out  = 0;
                    sp.stream.avail_out = tif.tif_rawdatasize;
                }
            } while(sp.stream.avail_in > 0);

            return(true);
        }
Beispiel #8
0
        // Reset encoding state at the start of a strip.
        static bool ZIPPreEncode(TIFF tif, ushort sampleNumber)
        {
            ZIPState sp = tif.tif_data as ZIPState;

#if DEBUG
            if (sp == null)
            {
                throw new Exception("sp==null");
            }
#endif

            if ((sp.state & ZSTATE.INIT_ENCODE) != ZSTATE.INIT_ENCODE)
            {
                tif.tif_setupencode(tif);
            }

            sp.stream.out_buf   = tif.tif_rawdata;
            sp.stream.next_out  = 0;
            sp.stream.avail_out = tif.tif_rawdatasize;
            return(zlib.deflateReset(sp.stream) == zlib.Z_OK);
        }
Beispiel #9
0
        // Setup state for decoding a strip.
        static bool ZIPPreDecode(TIFF tif, ushort sampleNumber)
        {
            ZIPState sp = tif.tif_data as ZIPState;

#if DEBUG
            if (sp == null)
            {
                throw new Exception("sp==null");
            }
#endif

            if ((sp.state & ZSTATE.INIT_DECODE) != ZSTATE.INIT_DECODE)
            {
                tif.tif_setupdecode(tif);
            }

            sp.stream.in_buf   = tif.tif_rawdata;
            sp.stream.next_in  = 0;
            sp.stream.avail_in = tif.tif_rawcc;
            return(zlib.inflateReset(sp.stream) == zlib.Z_OK);
        }
Beispiel #10
0
        static bool ZIPVSetField(TIFF tif, TIFFTAG tag, TIFFDataType dt, object[] ap)
        {
            ZIPState sp     = tif.tif_data as ZIPState;
            string   module = "ZIPVSetField";

            switch (tag)
            {
            case TIFFTAG.ZIPQUALITY:
                sp.zipquality = __GetAsInt(ap, 0);
                if ((sp.state & ZSTATE.INIT_ENCODE) == ZSTATE.INIT_ENCODE)
                {
                    if (zlib.deflateParams(sp.stream, sp.zipquality, zlib.Z_DEFAULT_STRATEGY) != zlib.Z_OK)
                    {
                        TIFFErrorExt(tif.tif_clientdata, module, "{0}: zlib error: {1}", tif.tif_name, sp.stream.msg);
                        return(false);
                    }
                }
                return(true);

            default: return(sp.vsetparent(tif, tag, dt, ap));
            }
            //NOTREACHED
        }
Beispiel #11
0
        static bool TIFFInitZIP(TIFF tif, COMPRESSION scheme)
        {
            string module="TIFFInitZIP";

            #if DEBUG
            if(scheme!=COMPRESSION.DEFLATE&&scheme!=COMPRESSION.ADOBE_DEFLATE) throw new Exception("scheme!=COMPRESSION.DEFLATE&&scheme!=COMPRESSION.ADOBE_DEFLATE");
            #endif

            // Merge codec-specific tag information.
            if(!_TIFFMergeFieldInfo(tif, zipFieldInfo))
            {
                TIFFErrorExt(tif.tif_clientdata, module, "Merging Deflate codec-specific tags failed");
                return false;
            }

            // Allocate state block so tag methods have storage to record values.
            ZIPState sp=null;
            try
            {
                tif.tif_data=sp=new ZIPState();
                sp.stream=new zlib.z_stream();
            }
            catch
            {
                TIFFErrorExt(tif.tif_clientdata, module, "No space for ZIP state block");
                return false;
            }

            // Override parent get/set field methods.
            sp.vgetparent=tif.tif_tagmethods.vgetfield;
            tif.tif_tagmethods.vgetfield=ZIPVGetField; // hook for codec tags
            sp.vsetparent=tif.tif_tagmethods.vsetfield;
            tif.tif_tagmethods.vsetfield=ZIPVSetField; // hook for codec tags

            // Default values for codec-specific fields
            sp.zipquality=zlib.Z_DEFAULT_COMPRESSION; // default comp. level
            sp.state=ZSTATE.None;

            // Install codec methods.
            tif.tif_setupdecode=ZIPSetupDecode;
            tif.tif_predecode=ZIPPreDecode;
            tif.tif_decoderow=ZIPDecode;
            tif.tif_decodestrip=ZIPDecode;
            tif.tif_decodetile=ZIPDecode;
            tif.tif_setupencode=ZIPSetupEncode;
            tif.tif_preencode=ZIPPreEncode;
            tif.tif_postencode=ZIPPostEncode;
            tif.tif_encoderow=ZIPEncode;
            tif.tif_encodestrip=ZIPEncode;
            tif.tif_encodetile=ZIPEncode;
            tif.tif_cleanup=ZIPCleanup;

            // Setup predictor setup.
            TIFFPredictorInit(tif);
            return true;
        }