Exemple #1
0
 /// <summary>
 /// Creats an Image in CCITT mode.
 /// </summary>
 /// <param name="width">the exact width of the image</param>
 /// <param name="height">the exact height of the image</param>
 /// <param name="reverseBits">
 /// reverses the bits in data.
 /// Bit 0 is swapped with bit 7 and so on
 /// </param>
 /// <param name="typeCcitt">
 /// the type of compression in data. It can be
 /// CCITTG4, CCITTG31D, CCITTG32D
 /// </param>
 /// <param name="parameters">
 /// parameters associated with this stream. Possible values are
 /// CCITT_BLACKIS1, CCITT_ENCODEDBYTEALIGN, CCITT_ENDOFLINE and CCITT_ENDOFBLOCK or a
 /// combination of them
 /// </param>
 /// <param name="data">the image data</param>
 public ImgCcitt(int width, int height, bool reverseBits, int typeCcitt, int parameters, byte[] data) : base((Uri)null)
 {
     if (typeCcitt != CCITTG4 && typeCcitt != CCITTG3_1D && typeCcitt != CCITTG3_2D)
     {
         throw new BadElementException("The CCITT compression type must be CCITTG4, CCITTG3_1D or CCITTG3_2D");
     }
     if (reverseBits)
     {
         TiffFaxDecoder.ReverseBits(data);
     }
     type         = IMGRAW;
     scaledHeight = height;
     Top          = scaledHeight;
     scaledWidth  = width;
     Right        = scaledWidth;
     colorspace   = parameters;
     bpc          = typeCcitt;
     rawData      = data;
     plainWidth   = Width;
     plainHeight  = Height;
 }
Exemple #2
0
        protected static Image GetTiffImageColor(TiffDirectory dir, RandomAccessFileOrArray s)
        {
            var            predictor   = 1;
            TifflzwDecoder lzwDecoder  = null;
            var            compression = (int)dir.GetFieldAsLong(TiffConstants.TIFFTAG_COMPRESSION);

            switch (compression)
            {
            case TiffConstants.COMPRESSION_NONE:
            case TiffConstants.COMPRESSION_LZW:
            case TiffConstants.COMPRESSION_PACKBITS:
            case TiffConstants.COMPRESSION_DEFLATE:
            case TiffConstants.COMPRESSION_ADOBE_DEFLATE:
            case TiffConstants.COMPRESSION_OJPEG:
            case TiffConstants.COMPRESSION_JPEG:
                break;

            default:
                throw new InvalidOperationException("The compression " + compression + " is not supported.");
            }
            var photometric = (int)dir.GetFieldAsLong(TiffConstants.TIFFTAG_PHOTOMETRIC);

            switch (photometric)
            {
            case TiffConstants.PHOTOMETRIC_MINISWHITE:
            case TiffConstants.PHOTOMETRIC_MINISBLACK:
            case TiffConstants.PHOTOMETRIC_RGB:
            case TiffConstants.PHOTOMETRIC_SEPARATED:
            case TiffConstants.PHOTOMETRIC_PALETTE:
                break;

            default:
                if (compression != TiffConstants.COMPRESSION_OJPEG && compression != TiffConstants.COMPRESSION_JPEG)
                {
                    throw new InvalidOperationException("The photometric " + photometric + " is not supported.");
                }

                break;
            }
            float rotation = 0;

            if (dir.IsTagPresent(TiffConstants.TIFFTAG_ORIENTATION))
            {
                var 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);
                }
            }

            if (dir.IsTagPresent(TiffConstants.TIFFTAG_PLANARCONFIG) &&
                dir.GetFieldAsLong(TiffConstants.TIFFTAG_PLANARCONFIG) == TiffConstants.PLANARCONFIG_SEPARATE)
            {
                throw new InvalidOperationException("Planar images are not supported.");
            }

            if (dir.IsTagPresent(TiffConstants.TIFFTAG_EXTRASAMPLES))
            {
                throw new InvalidOperationException("Extra samples are not supported.");
            }

            var samplePerPixel = 1;

            if (dir.IsTagPresent(TiffConstants.TIFFTAG_SAMPLESPERPIXEL)) // 1,3,4
            {
                samplePerPixel = (int)dir.GetFieldAsLong(TiffConstants.TIFFTAG_SAMPLESPERPIXEL);
            }

            var bitsPerSample = 1;

            if (dir.IsTagPresent(TiffConstants.TIFFTAG_BITSPERSAMPLE))
            {
                bitsPerSample = (int)dir.GetFieldAsLong(TiffConstants.TIFFTAG_BITSPERSAMPLE);
            }

            switch (bitsPerSample)
            {
            case 1:
            case 2:
            case 4:
            case 8:
                break;

            default:
                throw new InvalidOperationException("Bits per sample " + bitsPerSample + " is not supported.");
            }
            Image img = null;

            var h              = (int)dir.GetFieldAsLong(TiffConstants.TIFFTAG_IMAGELENGTH);
            var w              = (int)dir.GetFieldAsLong(TiffConstants.TIFFTAG_IMAGEWIDTH);
            var dpiX           = 0;
            var dpiY           = 0;
            var 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);
            var fillOrder      = 1;
            var reverse        = false;
            var fillOrderField = dir.GetField(TiffConstants.TIFFTAG_FILLORDER);

            if (fillOrderField != null)
            {
                fillOrder = fillOrderField.GetAsInt(0);
            }

            reverse = (fillOrder == TiffConstants.FILLORDER_LSB2MSB);
            var rowsStrip = h;

            if (dir.IsTagPresent(TiffConstants.TIFFTAG_ROWSPERSTRIP)) //another hack for broken tiffs
            {
                rowsStrip = (int)dir.GetFieldAsLong(TiffConstants.TIFFTAG_ROWSPERSTRIP);
            }

            if (rowsStrip <= 0 || rowsStrip > h)
            {
                rowsStrip = h;
            }

            var offset = getArrayLongShort(dir, TiffConstants.TIFFTAG_STRIPOFFSETS);
            var 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] };
            }
            if (compression == TiffConstants.COMPRESSION_LZW)
            {
                var predictorField = dir.GetField(TiffConstants.TIFFTAG_PREDICTOR);
                if (predictorField != null)
                {
                    predictor = predictorField.GetAsInt(0);
                    if (predictor != 1 && predictor != 2)
                    {
                        throw new InvalidOperationException("Illegal value for Predictor in TIFF file.");
                    }
                    if (predictor == 2 && bitsPerSample != 8)
                    {
                        throw new InvalidOperationException(bitsPerSample + "-bit samples are not supported for Horizontal differencing Predictor.");
                    }
                }
                lzwDecoder = new TifflzwDecoder(w, predictor,
                                                samplePerPixel);
            }
            var                   rowsLeft = h;
            MemoryStream          stream   = null;
            ZDeflaterOutputStream zip      = null;
            Ccittg4Encoder        g4       = null;

            if (bitsPerSample == 1 && samplePerPixel == 1)
            {
                g4 = new Ccittg4Encoder(w);
            }
            else
            {
                stream = new MemoryStream();
                if (compression != TiffConstants.COMPRESSION_OJPEG && compression != TiffConstants.COMPRESSION_JPEG)
                {
                    zip = new ZDeflaterOutputStream(stream);
                }
            }
            if (compression == TiffConstants.COMPRESSION_OJPEG)
            {
                // Assume that the TIFFTAG_JPEGIFBYTECOUNT tag is optional, since it's obsolete and
                // is often missing

                if ((!dir.IsTagPresent(TiffConstants.TIFFTAG_JPEGIFOFFSET)))
                {
                    throw new IOException("Missing tag(s) for OJPEG compression.");
                }
                var jpegOffset = (int)dir.GetFieldAsLong(TiffConstants.TIFFTAG_JPEGIFOFFSET);
                var jpegLength = s.Length - jpegOffset;

                if (dir.IsTagPresent(TiffConstants.TIFFTAG_JPEGIFBYTECOUNT))
                {
                    jpegLength = (int)dir.GetFieldAsLong(TiffConstants.TIFFTAG_JPEGIFBYTECOUNT) +
                                 (int)size[0];
                }

                var jpeg = new byte[Math.Min(jpegLength, s.Length - jpegOffset)];

                var posFilePointer = s.FilePointer;
                posFilePointer += jpegOffset;
                s.Seek(posFilePointer);
                s.ReadFully(jpeg);
                img = new Jpeg(jpeg);
            }
            else if (compression == TiffConstants.COMPRESSION_JPEG)
            {
                if (size.Length > 1)
                {
                    throw new IOException("Compression JPEG is only supported with a single strip. This image has " + size.Length + " strips.");
                }

                var jpeg = new byte[(int)size[0]];
                s.Seek(offset[0]);
                s.ReadFully(jpeg);
                img = new Jpeg(jpeg);
            }
            else
            {
                for (var k = 0; k < offset.Length; ++k)
                {
                    var im = new byte[(int)size[k]];
                    s.Seek(offset[k]);
                    s.ReadFully(im);
                    var    height = Math.Min(rowsStrip, rowsLeft);
                    byte[] outBuf = null;
                    if (compression != TiffConstants.COMPRESSION_NONE)
                    {
                        outBuf = new byte[(w * bitsPerSample * samplePerPixel + 7) / 8 * height];
                    }

                    if (reverse)
                    {
                        TiffFaxDecoder.ReverseBits(im);
                    }

                    switch (compression)
                    {
                    case TiffConstants.COMPRESSION_DEFLATE:
                    case TiffConstants.COMPRESSION_ADOBE_DEFLATE:
                        Inflate(im, outBuf);
                        break;

                    case TiffConstants.COMPRESSION_NONE:
                        outBuf = im;
                        break;

                    case TiffConstants.COMPRESSION_PACKBITS:
                        DecodePackbits(im, outBuf);
                        break;

                    case TiffConstants.COMPRESSION_LZW:
                        lzwDecoder.Decode(im, outBuf, height);
                        break;
                    }
                    if (bitsPerSample == 1 && samplePerPixel == 1)
                    {
                        g4.Fax4Encode(outBuf, height);
                    }
                    else
                    {
                        zip.Write(outBuf, 0, outBuf.Length);
                    }
                    rowsLeft -= rowsStrip;
                }
                if (bitsPerSample == 1 && samplePerPixel == 1)
                {
                    img = Image.GetInstance(w, h, false, Element.CCITTG4,
                                            photometric == TiffConstants.PHOTOMETRIC_MINISBLACK ? Element.CCITT_BLACKIS1 : 0, g4.Close());
                }
                else
                {
                    zip.Close();
                    img          = Image.GetInstance(w, h, samplePerPixel, bitsPerSample, stream.ToArray());
                    img.Deflated = true;
                }
            }
            img.SetDpi(dpiX, dpiY);
            if (compression != TiffConstants.COMPRESSION_OJPEG && compression != TiffConstants.COMPRESSION_JPEG)
            {
                if (dir.IsTagPresent(TiffConstants.TIFFTAG_ICCPROFILE))
                {
                    try
                    {
                        var fd      = dir.GetField(TiffConstants.TIFFTAG_ICCPROFILE);
                        var iccProf = IccProfile.GetInstance(fd.GetAsBytes());
                        if (samplePerPixel == iccProf.NumComponents)
                        {
                            img.TagIcc = iccProf;
                        }
                    }
                    catch
                    {
                        //empty
                    }
                }
                if (dir.IsTagPresent(TiffConstants.TIFFTAG_COLORMAP))
                {
                    var fd      = dir.GetField(TiffConstants.TIFFTAG_COLORMAP);
                    var rgb     = fd.GetAsChars();
                    var palette = new byte[rgb.Length];
                    var gColor  = rgb.Length / 3;
                    var bColor  = gColor * 2;
                    for (var k = 0; k < gColor; ++k)
                    {
                        palette[k * 3]     = (byte)(rgb[k] >> 8);
                        palette[k * 3 + 1] = (byte)(rgb[k + gColor] >> 8);
                        palette[k * 3 + 2] = (byte)(rgb[k + bColor] >> 8);
                    }
                    var indexed = new PdfArray();
                    indexed.Add(PdfName.Indexed);
                    indexed.Add(PdfName.Devicergb);
                    indexed.Add(new PdfNumber(gColor - 1));
                    indexed.Add(new PdfString(palette));
                    var additional = new PdfDictionary();
                    additional.Put(PdfName.Colorspace, indexed);
                    img.Additional = additional;
                }
                img.OriginalType = Image.ORIGINAL_TIFF;
            }
            if (photometric == TiffConstants.PHOTOMETRIC_MINISWHITE)
            {
                img.Inverted = true;
            }

            if (rotation.ApproxNotEqual(0))
            {
                img.InitialRotation = rotation;
            }

            return(img);
        }