DecodeT6() 공개 메소드

public DecodeT6 ( byte buffer, byte compData, int startX, int height, long tiffT6Options ) : void
buffer byte
compData byte
startX int
height int
tiffT6Options long
리턴 void
예제 #1
0
        /** Reads a page from a TIFF image.
        * @param s the file source
        * @param page the page to get. The first page is 1
        * @param direct for single strip, CCITT images, generate the image
        * by direct byte copying. It's faster but may not work
        * every time
        * @return the <CODE>Image</CODE>
        */
        public static Image GetTiffImage(RandomAccessFileOrArray s, int page, bool direct)
        {
            if (page < 1)
                throw new ArgumentException(MessageLocalization.GetComposedMessage("the.page.number.must.be.gt.eq.1"));
            TIFFDirectory dir = new TIFFDirectory(s, page - 1);
            if (dir.IsTagPresent(TIFFConstants.TIFFTAG_TILEWIDTH))
                throw new ArgumentException(MessageLocalization.GetComposedMessage("tiles.are.not.supported"));
            int compression = (int)dir.GetFieldAsLong(TIFFConstants.TIFFTAG_COMPRESSION);
            switch (compression) {
                case TIFFConstants.COMPRESSION_CCITTRLEW:
                case TIFFConstants.COMPRESSION_CCITTRLE:
                case TIFFConstants.COMPRESSION_CCITTFAX3:
                case TIFFConstants.COMPRESSION_CCITTFAX4:
                    break;
                default:
                    return GetTiffImageColor(dir, s);
            }
            float rotation = 0;
            if (dir.IsTagPresent(TIFFConstants.TIFFTAG_ORIENTATION)) {
                int rot = (int)dir.GetFieldAsLong(TIFFConstants.TIFFTAG_ORIENTATION);
                if (rot == TIFFConstants.ORIENTATION_BOTRIGHT || rot == TIFFConstants.ORIENTATION_BOTLEFT)
                    rotation = (float)Math.PI;
                else if (rot == TIFFConstants.ORIENTATION_LEFTTOP || rot == TIFFConstants.ORIENTATION_LEFTBOT)
                    rotation = (float)(Math.PI / 2.0);
                else if (rot == TIFFConstants.ORIENTATION_RIGHTTOP || rot == TIFFConstants.ORIENTATION_RIGHTBOT)
                    rotation = -(float)(Math.PI / 2.0);
            }

            Image img = null;
            long tiffT4Options = 0;
            long tiffT6Options = 0;
            int fillOrder = 1;
            int h = (int)dir.GetFieldAsLong(TIFFConstants.TIFFTAG_IMAGELENGTH);
            int w = (int)dir.GetFieldAsLong(TIFFConstants.TIFFTAG_IMAGEWIDTH);
            int dpiX = 0;
            int dpiY = 0;
            float XYRatio = 0;
            int resolutionUnit = TIFFConstants.RESUNIT_INCH;
            if (dir.IsTagPresent(TIFFConstants.TIFFTAG_RESOLUTIONUNIT))
                resolutionUnit = (int)dir.GetFieldAsLong(TIFFConstants.TIFFTAG_RESOLUTIONUNIT);
            dpiX = GetDpi(dir.GetField(TIFFConstants.TIFFTAG_XRESOLUTION), resolutionUnit);
            dpiY = GetDpi(dir.GetField(TIFFConstants.TIFFTAG_YRESOLUTION), resolutionUnit);
            if (resolutionUnit == TIFFConstants.RESUNIT_NONE) {
                if (dpiY != 0)
                    XYRatio = (float)dpiX / (float)dpiY;
                dpiX = 0;
                dpiY = 0;
            }
            int rowsStrip = h;
            if (dir.IsTagPresent(TIFFConstants.TIFFTAG_ROWSPERSTRIP))
                rowsStrip = (int)dir.GetFieldAsLong(TIFFConstants.TIFFTAG_ROWSPERSTRIP);
            if (rowsStrip <= 0 || rowsStrip > h)
                rowsStrip = h;
            long[] offset = GetArrayLongShort(dir, TIFFConstants.TIFFTAG_STRIPOFFSETS);
            long[] size = GetArrayLongShort(dir, TIFFConstants.TIFFTAG_STRIPBYTECOUNTS);
            if ((size == null || (size.Length == 1 && (size[0] == 0 || size[0] + offset[0] > s.Length))) && h == rowsStrip) { // some TIFF producers are really lousy, so...
                size = new long[]{s.Length - (int)offset[0]};
            }
            bool reverse = false;
            TIFFField fillOrderField =  dir.GetField(TIFFConstants.TIFFTAG_FILLORDER);
            if (fillOrderField != null)
                fillOrder = fillOrderField.GetAsInt(0);
            reverse = (fillOrder == TIFFConstants.FILLORDER_LSB2MSB);
            int paramsn = 0;
            if (dir.IsTagPresent(TIFFConstants.TIFFTAG_PHOTOMETRIC)) {
                long photo = dir.GetFieldAsLong(TIFFConstants.TIFFTAG_PHOTOMETRIC);
                if (photo == TIFFConstants.PHOTOMETRIC_MINISBLACK)
                    paramsn |= Image.CCITT_BLACKIS1;
            }
            int imagecomp = 0;
            switch (compression) {
                case TIFFConstants.COMPRESSION_CCITTRLEW:
                case TIFFConstants.COMPRESSION_CCITTRLE:
                    imagecomp = Image.CCITTG3_1D;
                    paramsn |= Image.CCITT_ENCODEDBYTEALIGN | Image.CCITT_ENDOFBLOCK;
                    break;
                case TIFFConstants.COMPRESSION_CCITTFAX3:
                    imagecomp = Image.CCITTG3_1D;
                    paramsn |= Image.CCITT_ENDOFLINE | Image.CCITT_ENDOFBLOCK;
                    TIFFField t4OptionsField = dir.GetField(TIFFConstants.TIFFTAG_GROUP3OPTIONS);
                    if (t4OptionsField != null) {
                        tiffT4Options = t4OptionsField.GetAsLong(0);
                    if ((tiffT4Options & TIFFConstants.GROUP3OPT_2DENCODING) != 0)
                        imagecomp = Image.CCITTG3_2D;
                    if ((tiffT4Options & TIFFConstants.GROUP3OPT_FILLBITS) != 0)
                        paramsn |= Image.CCITT_ENCODEDBYTEALIGN;
                    }
                    break;
                case TIFFConstants.COMPRESSION_CCITTFAX4:
                    imagecomp = Image.CCITTG4;
                    TIFFField t6OptionsField = dir.GetField(TIFFConstants.TIFFTAG_GROUP4OPTIONS);
                    if (t6OptionsField != null)
                        tiffT6Options = t6OptionsField.GetAsLong(0);
                    break;
            }
            if (direct && rowsStrip == h) { //single strip, direct
                byte[] im = new byte[(int)size[0]];
                s.Seek(offset[0]);
                s.ReadFully(im);
                img = Image.GetInstance(w, h, false, imagecomp, paramsn, im);
                img.Inverted = true;
            }
            else {
                int rowsLeft = h;
                CCITTG4Encoder g4 = new CCITTG4Encoder(w);
                for (int k = 0; k < offset.Length; ++k) {
                    byte[] im = new byte[(int)size[k]];
                    s.Seek(offset[k]);
                    s.ReadFully(im);
                    int height = Math.Min(rowsStrip, rowsLeft);
                    TIFFFaxDecoder decoder = new TIFFFaxDecoder(fillOrder, w, height);
                    byte[] outBuf = new byte[(w + 7) / 8 * height];
                    switch (compression) {
                        case TIFFConstants.COMPRESSION_CCITTRLEW:
                        case TIFFConstants.COMPRESSION_CCITTRLE:
                            decoder.Decode1D(outBuf, im, 0, height);
                            g4.Fax4Encode(outBuf, height);
                            break;
                        case TIFFConstants.COMPRESSION_CCITTFAX3:
                            try {
                                decoder.Decode2D(outBuf, im, 0, height, tiffT4Options);
                            }
                            catch (Exception e) {
                                // let's flip the fill bits and try again...
                                tiffT4Options ^= TIFFConstants.GROUP3OPT_FILLBITS;
                                try {
                                    decoder.Decode2D(outBuf, im, 0, height, tiffT4Options);
                                }
                                catch {
                                    throw e;
                                }
                            }
                            g4.Fax4Encode(outBuf, height);
                            break;
                        case TIFFConstants.COMPRESSION_CCITTFAX4:
                            decoder.DecodeT6(outBuf, im, 0, height, tiffT6Options);
                            g4.Fax4Encode(outBuf, height);
                            break;
                    }
                    rowsLeft -= rowsStrip;
                }
                byte[] g4pic = g4.Close();
                img = Image.GetInstance(w, h, false, Image.CCITTG4, paramsn & Image.CCITT_BLACKIS1, g4pic);
            }
            img.SetDpi(dpiX, dpiY);
            img.XYRatio = XYRatio;
            if (dir.IsTagPresent(TIFFConstants.TIFFTAG_ICCPROFILE)) {
                try {
                    TIFFField fd = dir.GetField(TIFFConstants.TIFFTAG_ICCPROFILE);
                    ICC_Profile icc_prof = ICC_Profile.GetInstance(fd.GetAsBytes());
                    if (icc_prof.NumComponents == 1)
                        img.TagICC = icc_prof;
                }
                catch {
                    //empty
                }
            }
            img.OriginalType = Image.ORIGINAL_TIFF;
            if (rotation != 0)
                img.InitialRotation = rotation;
            return img;
        }
예제 #2
0
 /**
  * Decode a byte[] applying the filters specified in the provided dictionary.
  * @param b the bytes to decode
  * @param streamDictionary the dictionary that contains filter information
  * @return the decoded bytes
  * @throws IOException if there are any problems decoding the bytes
  * @since 5.0.4
  */
 public static byte[] DecodeBytes(byte[] b, PdfDictionary streamDictionary) {
     PdfObject filter = GetPdfObjectRelease(streamDictionary.Get(PdfName.FILTER));
     List<PdfObject> filters = new List<PdfObject>();
     if (filter != null) {
         if (filter.IsName())
             filters.Add(filter);
         else if (filter.IsArray())
             filters = ((PdfArray)filter).ArrayList;
     }
     List<PdfObject> dp = new List<PdfObject>();
     PdfObject dpo = GetPdfObjectRelease(streamDictionary.Get(PdfName.DECODEPARMS));
     if (dpo == null || (!dpo.IsDictionary() && !dpo.IsArray()))
         dpo = GetPdfObjectRelease(streamDictionary.Get(PdfName.DP));
     if (dpo != null) {
         if (dpo.IsDictionary())
             dp.Add(dpo);
         else if (dpo.IsArray())
             dp = ((PdfArray)dpo).ArrayList;
     }
     PdfName name;
     for (int j = 0; j < filters.Count; ++j) {
         name = ((PdfName)GetPdfObjectRelease(filters[j]));
         if (PdfName.FLATEDECODE.Equals(name) || PdfName.FL.Equals(name)) {
             b = FlateDecode(b);
             PdfObject dicParam = null;
             if (j < dp.Count) {
                 dicParam = (PdfObject)dp[j];
                 b = DecodePredictor(b, dicParam);
             }
         }
         else if (PdfName.ASCIIHEXDECODE.Equals(name) || PdfName.AHX.Equals(name))
             b = ASCIIHexDecode(b);
         else if (PdfName.ASCII85DECODE.Equals(name) || PdfName.A85.Equals(name))
             b = ASCII85Decode(b);
         else if (PdfName.LZWDECODE.Equals(name)) {
             b = LZWDecode(b);
             PdfObject dicParam = null;
             if (j < dp.Count) {
                 dicParam = (PdfObject)dp[j];
                 b = DecodePredictor(b, dicParam);
             }
         }
         else if (PdfName.CCITTFAXDECODE.Equals(name)) {
             PdfNumber wn = (PdfNumber)GetPdfObjectRelease(streamDictionary.Get(PdfName.WIDTH));
             PdfNumber hn = (PdfNumber)GetPdfObjectRelease(streamDictionary.Get(PdfName.HEIGHT));
             if (wn == null || hn == null)
                 throw new UnsupportedPdfException(MessageLocalization.GetComposedMessage("filter.ccittfaxdecode.is.only.supported.for.images"));
             int width = wn.IntValue;
             int height = hn.IntValue;
             PdfDictionary param = null;
             if (j < dp.Count) {
                 PdfObject objParam = GetPdfObjectRelease((PdfObject)dp[j]);
                 if (objParam != null && (objParam is PdfDictionary))
                     param = (PdfDictionary)objParam;
             }
             int k = 0;
             bool blackIs1 = false;
             bool byteAlign = false;
             if (param != null) {
                 PdfNumber kn = param.GetAsNumber(PdfName.K);
                 if (kn != null)
                     k = kn.IntValue;
                 PdfBoolean bo = param.GetAsBoolean(PdfName.BLACKIS1);
                 if (bo != null)
                     blackIs1 = bo.BooleanValue;
                 bo = param.GetAsBoolean(PdfName.ENCODEDBYTEALIGN);
                 if (bo != null)
                     byteAlign = bo.BooleanValue;
             }
             byte[] outBuf = new byte[(width + 7) / 8 * height];
             TIFFFaxDecompressor decoder = new TIFFFaxDecompressor();
             if (k == 0 || k > 0) {
                 int tiffT4Options = k > 0 ? TIFFConstants.GROUP3OPT_2DENCODING : 0;
                 tiffT4Options |= byteAlign ? TIFFConstants.GROUP3OPT_FILLBITS : 0;
                 decoder.SetOptions(1, TIFFConstants.COMPRESSION_CCITTFAX3, tiffT4Options, 0);
                 decoder.DecodeRaw(outBuf, b, width, height);
                 if (decoder.fails > 0) {
                     byte[] outBuf2 = new byte[(width + 7) / 8 * height];
                     int oldFails = decoder.fails;
                     decoder.SetOptions(1, TIFFConstants.COMPRESSION_CCITTRLE, tiffT4Options, 0);
                     decoder.DecodeRaw(outBuf2, b, width, height);
                     if (decoder.fails < oldFails) {
                         outBuf = outBuf2;
                     }
                 }
             }
             else {
                 TIFFFaxDecoder deca = new TIFFFaxDecoder(1, width, height);
                 deca.DecodeT6(outBuf, b, 0, height, 0);
             }
             if (!blackIs1) {
                 int len = outBuf.Length;
                 for (int t = 0; t < len; ++t) {
                     outBuf[t] ^= 0xff;
                 }
             }
             b = outBuf;
         }
         else if (PdfName.CRYPT.Equals(name)) {
         }
         else
             throw new UnsupportedPdfException(MessageLocalization.GetComposedMessage("the.filter.1.is.not.supported", name));
     }
     return b;
 }
예제 #3
0
 public byte[] Decode(byte[] b, PdfName filterName, PdfObject decodeParams, PdfDictionary streamDictionary) {
     PdfNumber wn = (PdfNumber)PdfReader.GetPdfObjectRelease(streamDictionary.Get(PdfName.WIDTH));
     PdfNumber hn = (PdfNumber)PdfReader.GetPdfObjectRelease(streamDictionary.Get(PdfName.HEIGHT));
     if (wn == null || hn == null)
         throw new UnsupportedPdfException(MessageLocalization.GetComposedMessage("filter.ccittfaxdecode.is.only.supported.for.images"));
     int width = wn.IntValue;
     int height = hn.IntValue;
     
     PdfDictionary param = decodeParams is PdfDictionary ? (PdfDictionary)decodeParams : null;
     int k = 0;
     bool blackIs1 = false;
     bool byteAlign = false;
     if (param != null) {
         PdfNumber kn = param.GetAsNumber(PdfName.K);
         if (kn != null)
             k = kn.IntValue;
         PdfBoolean bo = param.GetAsBoolean(PdfName.BLACKIS1);
         if (bo != null)
             blackIs1 = bo.BooleanValue;
         bo = param.GetAsBoolean(PdfName.ENCODEDBYTEALIGN);
         if (bo != null)
             byteAlign = bo.BooleanValue;
     }
     byte[] outBuf = new byte[(width + 7) / 8 * height];
     TIFFFaxDecompressor decoder = new TIFFFaxDecompressor();
     if (k == 0 || k > 0) {
         int tiffT4Options = k > 0 ? TIFFConstants.GROUP3OPT_2DENCODING : 0;
         tiffT4Options |= byteAlign ? TIFFConstants.GROUP3OPT_FILLBITS : 0;
         decoder.SetOptions(1, TIFFConstants.COMPRESSION_CCITTFAX3, tiffT4Options, 0);
         decoder.DecodeRaw(outBuf, b, width, height);
         if (decoder.fails > 0) {
             byte[] outBuf2 = new byte[(width + 7) / 8 * height];
             int oldFails = decoder.fails;
             decoder.SetOptions(1, TIFFConstants.COMPRESSION_CCITTRLE, tiffT4Options, 0);
             decoder.DecodeRaw(outBuf2, b, width, height);
             if (decoder.fails < oldFails) {
                 outBuf = outBuf2;
             }
         }
     }
     else {
         TIFFFaxDecoder deca = new TIFFFaxDecoder(1, width, height);
         deca.DecodeT6(outBuf, b, 0, height, 0);
     }
     if (!blackIs1) {
         int len = outBuf.Length;
         for (int t = 0; t < len; ++t) {
             outBuf[t] ^= 0xff;
         }
     }
     b = outBuf;       
     return b;
 }