public static IElement AddImage(string filePath) { var image = Image.GetInstance(filePath); var imgRaw = new ImgRaw(image); imgRaw.Alignment = Image.ALIGN_CENTER; imgRaw.ScaleToFit(PageSize.A4.Width - (PageSize.A4.Width * 0.15f), PageSize.A4.Height); return imgRaw; }
/// <summary> /// Gets an instance of an Image in raw mode. /// </summary> /// <param name="width">the width of the image in pixels</param> /// <param name="height">the height of the image in pixels</param> /// <param name="components">1,3 or 4 for GrayScale, RGB and CMYK</param> /// <param name="bpc">bits per component</param> /// <param name="data">the image data</param> /// <param name="transparency"> /// transparency information in the Mask format of the /// image dictionary /// </param> /// <returns>an object of type ImgRaw</returns> public static Image getInstance(int width, int height, int components, int bpc, byte[] data, int[] transparency) { if (transparency != null && transparency.Length != components * 2) { throw new BadElementException("Transparency length must be equal to (componentes * 2)"); } if (components == 1 && bpc == 1) { byte[] g4 = CCITTG4Encoder.compress(data, width, height); return(Image.getInstance(width, height, false, Element.CCITTG4, Element.CCITT_BLACKIS1, g4, transparency)); } Image img = new ImgRaw(width, height, components, bpc, data); img.transparency = transparency; return(img); }
/** * Reads next frame image */ virtual protected void ReadImage() { ix = ReadShort(); // (sub)image position & size iy = ReadShort(); iw = ReadShort(); ih = ReadShort(); int packed = inp.ReadByte(); lctFlag = (packed & 0x80) != 0; // 1 - local color table flag interlace = (packed & 0x40) != 0; // 2 - interlace flag // 3 - sort flag // 4-5 - reserved lctSize = 2 << (packed & 7); // 6-8 - local color table size m_bpc = NewBpc(m_gbpc); if (lctFlag) { m_curr_table = ReadColorTable((packed & 7) + 1); // read table m_bpc = NewBpc((packed & 7) + 1); } else { m_curr_table = m_global_table; } if (transparency && transIndex >= m_curr_table.Length / 3) transparency = false; if (transparency && m_bpc == 1) { // Acrobat 5.05 doesn't like this combination byte[] tp = new byte[12]; Array.Copy(m_curr_table, 0, tp, 0, 6); m_curr_table = tp; m_bpc = 2; } bool skipZero = DecodeImageData(); // decode pixel data if (!skipZero) Skip(); Image img = null; img = new ImgRaw(iw, ih, 1, m_bpc, m_out); PdfArray colorspace = new PdfArray(); colorspace.Add(PdfName.INDEXED); colorspace.Add(PdfName.DEVICERGB); int len = m_curr_table.Length; colorspace.Add(new PdfNumber(len / 3 - 1)); colorspace.Add(new PdfString(m_curr_table)); PdfDictionary ad = new PdfDictionary(); ad.Put(PdfName.COLORSPACE, colorspace); img.Additional = ad; if (transparency) { img.Transparency = new int[]{transIndex, transIndex}; } img.OriginalType = Image.ORIGINAL_GIF; img.OriginalData = fromData; img.Url = fromUrl; GifFrame gf = new GifFrame(); gf.image = img; gf.ix = ix; gf.iy = iy; frames.Add(gf); // add image to frame list //ResetFrame(); }
private Image IndexedModel(byte[] bdata, int bpc, int paletteEntries) { Image img = new ImgRaw(width, height, 1, bpc, bdata); PdfArray colorspace = new PdfArray(); colorspace.Add(PdfName.INDEXED); colorspace.Add(PdfName.DEVICERGB); byte[] np = GetPalette(paletteEntries); int len = np.Length; colorspace.Add(new PdfNumber(len / 3 - 1)); colorspace.Add(new PdfString(np)); PdfDictionary ad = new PdfDictionary(); ad.Put(PdfName.COLORSPACE, colorspace); img.Additional = ad; return img; }
/// <summary> /// Gets an instance of an Image in raw mode. /// </summary> /// <param name="width">the width of the image in pixels</param> /// <param name="height">the height of the image in pixels</param> /// <param name="components">1,3 or 4 for GrayScale, RGB and CMYK</param> /// <param name="bpc">bits per component</param> /// <param name="data">the image data</param> /// <param name="transparency"> /// transparency information in the Mask format of the /// image dictionary /// </param> /// <returns>an object of type ImgRaw</returns> public static Image GetInstance(int width, int height, int components, int bpc, byte[] data, int[] transparency) { if (transparency != null && transparency.Length != components * 2) throw new BadElementException(MessageLocalization.GetComposedMessage("transparency.length.must.be.equal.to.componentes.2")); if (components == 1 && bpc == 1) { byte[] g4 = CCITTG4Encoder.Compress(data, width, height); return Image.GetInstance(width, height, false, Element.CCITTG4, Element.CCITT_BLACKIS1, g4, transparency); } Image img = new ImgRaw(width, height, components, bpc, data); img.transparency = transparency; return img; }
/** * Reuses an existing image. * @param ref the reference to the image dictionary * @throws BadElementException on error * @return the image */ public static Image GetInstance(PRIndirectReference iref) { PdfDictionary dic = (PdfDictionary)PdfReader.GetPdfObjectRelease(iref); int width = ((PdfNumber)PdfReader.GetPdfObjectRelease(dic.Get(PdfName.WIDTH))).IntValue; int height = ((PdfNumber)PdfReader.GetPdfObjectRelease(dic.Get(PdfName.HEIGHT))).IntValue; Image imask = null; PdfObject obj = dic.Get(PdfName.SMASK); if (obj != null && obj.IsIndirect()) { imask = GetInstance((PRIndirectReference)obj); } else { obj = dic.Get(PdfName.MASK); if (obj != null && obj.IsIndirect()) { PdfObject obj2 = PdfReader.GetPdfObjectRelease(obj); if (obj2 is PdfDictionary) imask = GetInstance((PRIndirectReference)obj); } } Image img = new ImgRaw(width, height, 1, 1, null); img.imageMask = imask; img.directReference = iref; return img; }
Image GetImage() { ReadPng(); int pal0 = 0; int palIdx = 0; palShades = false; if (trans != null) { for (int k = 0; k < trans.Length; ++k) { int n = trans[k] & 0xff; if (n == 0) { ++pal0; palIdx = k; } if (n != 0 && n != 255) { palShades = true; break; } } } if ((colorType & 4) != 0) palShades = true; genBWMask = (!palShades && (pal0 > 1 || transRedGray >= 0)); if (!palShades && !genBWMask && pal0 == 1) { additional.Put(PdfName.MASK, new PdfLiteral("["+palIdx+" "+palIdx+"]")); } bool needDecode = (interlaceMethod == 1) || (bitDepth == 16) || ((colorType & 4) != 0) || palShades || genBWMask; switch (colorType) { case 0: inputBands = 1; break; case 2: inputBands = 3; break; case 3: inputBands = 1; break; case 4: inputBands = 2; break; case 6: inputBands = 4; break; } if (needDecode) DecodeIdat(); int components = inputBands; if ((colorType & 4) != 0) --components; int bpc = bitDepth; if (bpc == 16) bpc = 8; Image img; if (image != null) { if (colorType == 3) img = new ImgRaw(width, height, components, bpc, image); else img = Image.GetInstance(width, height, components, bpc, image); } else { img = new ImgRaw(width, height, components, bpc, idat.ToArray()); img.Deflated = true; PdfDictionary decodeparms = new PdfDictionary(); decodeparms.Put(PdfName.BITSPERCOMPONENT, new PdfNumber(bitDepth)); decodeparms.Put(PdfName.PREDICTOR, new PdfNumber(15)); decodeparms.Put(PdfName.COLUMNS, new PdfNumber(width)); decodeparms.Put(PdfName.COLORS, new PdfNumber((colorType == 3 || (colorType & 2) == 0) ? 1 : 3)); additional.Put(PdfName.DECODEPARMS, decodeparms); } if (additional.Get(PdfName.COLORSPACE) == null) additional.Put(PdfName.COLORSPACE, GetColorspace()); if (intent != null) additional.Put(PdfName.INTENT, intent); if (additional.Size > 0) img.Additional = additional; if (icc_profile != null) img.TagICC = icc_profile; if (palShades) { Image im2 = Image.GetInstance(width, height, 1, 8, smask); im2.MakeMask(); img.ImageMask = im2; } if (genBWMask) { Image im2 = Image.GetInstance(width, height, 1, 1, smask); im2.MakeMask(); img.ImageMask = im2; } img.SetDpi(dpiX, dpiY); img.XYRatio = XYRatio; img.OriginalType = Image.ORIGINAL_PNG; return img; }
protected static Image GetTiffImageColor(TIFFDirectory dir, RandomAccessFileOrArray s) { int predictor = 1; TIFFLZWDecoder lzwDecoder = null; int 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 ArgumentException(MessageLocalization.GetComposedMessage("the.compression.1.is.not.supported", compression)); } int 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 ArgumentException(MessageLocalization.GetComposedMessage("the.photometric.1.is.not.supported", photometric)); break; } 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); } if (dir.IsTagPresent(TIFFConstants.TIFFTAG_PLANARCONFIG) && dir.GetFieldAsLong(TIFFConstants.TIFFTAG_PLANARCONFIG) == TIFFConstants.PLANARCONFIG_SEPARATE) throw new ArgumentException(MessageLocalization.GetComposedMessage("planar.images.are.not.supported")); int extraSamples = 0; if (dir.IsTagPresent(TIFFConstants.TIFFTAG_EXTRASAMPLES)) extraSamples = 1; int samplePerPixel = 1; if (dir.IsTagPresent(TIFFConstants.TIFFTAG_SAMPLESPERPIXEL)) // 1,3,4 samplePerPixel = (int)dir.GetFieldAsLong(TIFFConstants.TIFFTAG_SAMPLESPERPIXEL); int 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 ArgumentException(MessageLocalization.GetComposedMessage("bits.per.sample.1.is.not.supported", bitsPerSample)); } Image img = null; int h = (int)dir.GetFieldAsLong(TIFFConstants.TIFFTAG_IMAGELENGTH); int w = (int)dir.GetFieldAsLong(TIFFConstants.TIFFTAG_IMAGEWIDTH); int dpiX = 0; int dpiY = 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); int fillOrder = 1; bool reverse = false; TIFFField fillOrderField = dir.GetField(TIFFConstants.TIFFTAG_FILLORDER); if (fillOrderField != null) fillOrder = fillOrderField.GetAsInt(0); reverse = (fillOrder == TIFFConstants.FILLORDER_LSB2MSB); int 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; 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]}; } if (compression == TIFFConstants.COMPRESSION_LZW) { TIFFField predictorField = dir.GetField(TIFFConstants.TIFFTAG_PREDICTOR); if (predictorField != null) { predictor = predictorField.GetAsInt(0); if (predictor != 1 && predictor != 2) { throw new Exception(MessageLocalization.GetComposedMessage("illegal.value.for.predictor.in.tiff.file")); } if (predictor == 2 && bitsPerSample != 8) { throw new Exception(MessageLocalization.GetComposedMessage("1.bit.samples.are.not.supported.for.horizontal.differencing.predictor", bitsPerSample)); } } lzwDecoder = new TIFFLZWDecoder(w, predictor, samplePerPixel); } int rowsLeft = h; MemoryStream stream = null; MemoryStream mstream = null; ZDeflaterOutputStream zip = null; ZDeflaterOutputStream mzip = null; if (extraSamples > 0) { mstream = new MemoryStream(); mzip = new ZDeflaterOutputStream(mstream); } CCITTG4Encoder g4 = null; if (bitsPerSample == 1 && samplePerPixel == 1 && photometric != TIFFConstants.PHOTOMETRIC_PALETTE) { 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(MessageLocalization.GetComposedMessage("missing.tag.s.for.ojpeg.compression")); } int jpegOffset = (int)dir.GetFieldAsLong(TIFFConstants.TIFFTAG_JPEGIFOFFSET); int jpegLength = s.Length - jpegOffset; if (dir.IsTagPresent(TIFFConstants.TIFFTAG_JPEGIFBYTECOUNT)) { jpegLength = (int)dir.GetFieldAsLong(TIFFConstants.TIFFTAG_JPEGIFBYTECOUNT) + (int)size[0]; } byte[] jpeg = new byte[Math.Min(jpegLength, s.Length - jpegOffset)]; int 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(MessageLocalization.GetComposedMessage("compression.jpeg.is.only.supported.with.a.single.strip.this.image.has.1.strips", size.Length)); byte[] jpeg = new byte[(int)size[0]]; s.Seek(offset[0]); s.ReadFully(jpeg); img = new Jpeg(jpeg); } else { 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); 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 && photometric != TIFFConstants.PHOTOMETRIC_PALETTE) { g4.Fax4Encode(outBuf, height); } else { if (extraSamples > 0) ProcessExtraSamples(zip, mzip, outBuf, samplePerPixel, bitsPerSample, w, height); else zip.Write(outBuf, 0, outBuf.Length); } rowsLeft -= rowsStrip; } if (bitsPerSample == 1 && samplePerPixel == 1 && photometric != TIFFConstants.PHOTOMETRIC_PALETTE) { img = Image.GetInstance(w, h, false, Image.CCITTG4, photometric == TIFFConstants.PHOTOMETRIC_MINISBLACK ? Image.CCITT_BLACKIS1 : 0, g4.Close()); } else { zip.Close(); img = new ImgRaw(w, h, samplePerPixel - extraSamples, 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 { TIFFField fd = dir.GetField(TIFFConstants.TIFFTAG_ICCPROFILE); ICC_Profile icc_prof = ICC_Profile.GetInstance(fd.GetAsBytes()); if (samplePerPixel - extraSamples == icc_prof.NumComponents) img.TagICC = icc_prof; } catch { //empty } } if (dir.IsTagPresent(TIFFConstants.TIFFTAG_COLORMAP)) { TIFFField fd = dir.GetField(TIFFConstants.TIFFTAG_COLORMAP); char[] rgb = fd.GetAsChars(); byte[] palette = new byte[rgb.Length]; int gColor = rgb.Length / 3; int bColor = gColor * 2; for (int 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); } PdfArray indexed = new PdfArray(); indexed.Add(PdfName.INDEXED); indexed.Add(PdfName.DEVICERGB); indexed.Add(new PdfNumber(gColor - 1)); indexed.Add(new PdfString(palette)); PdfDictionary 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 != 0) img.InitialRotation = rotation; if (extraSamples > 0) { mzip.Close(); Image mimg = Image.GetInstance(w, h, 1, bitsPerSample, mstream.ToArray()); mimg.MakeMask(); mimg.Deflated = true; img.ImageMask = mimg; } return img; }