void ReadPng() { for (int i = 0; i < PNGID.Length; i++) { if (PNGID[i] != isp.ReadByte()) { throw new IOException("File is not a valid PNG."); } } byte[] buffer = new byte[TRANSFERSIZE]; while (true) { int len = GetInt(isp); String marker = GetString(isp); if (len < 0 || !CheckMarker(marker)) throw new IOException("Corrupted PNG file."); if (IDAT.Equals(marker)) { int size; while (len != 0) { size = isp.Read(buffer, 0, Math.Min(len, TRANSFERSIZE)); if (size <= 0) return; idat.Write(buffer, 0, size); len -= size; } } else if (tRNS.Equals(marker)) { switch (colorType) { case 0: if (len >= 2) { len -= 2; int gray = GetWord(isp); if (bitDepth == 16) transRedGray = gray; else additional.Put(PdfName.MASK, new PdfLiteral("["+gray+" "+gray+"]")); } break; case 2: if (len >= 6) { len -= 6; int red = GetWord(isp); int green = GetWord(isp); int blue = GetWord(isp); if (bitDepth == 16) { transRedGray = red; transGreen = green; transBlue = blue; } else additional.Put(PdfName.MASK, new PdfLiteral("["+red+" "+red+" "+green+" "+green+" "+blue+" "+blue+"]")); } break; case 3: if (len > 0) { trans = new byte[len]; for (int k = 0; k < len; ++k) trans[k] = (byte)isp.ReadByte(); len = 0; } break; } Utilities.Skip(isp, len); } else if (IHDR.Equals(marker)) { width = GetInt(isp); height = GetInt(isp); bitDepth = isp.ReadByte(); colorType = isp.ReadByte(); compressionMethod = isp.ReadByte(); filterMethod = isp.ReadByte(); interlaceMethod = isp.ReadByte(); } else if (PLTE.Equals(marker)) { if (colorType == 3) { PdfArray colorspace = new PdfArray(); colorspace.Add(PdfName.INDEXED); colorspace.Add(GetColorspace()); colorspace.Add(new PdfNumber(len / 3 - 1)); ByteBuffer colortable = new ByteBuffer(); while ((len--) > 0) { colortable.Append_i(isp.ReadByte()); } colorspace.Add(new PdfString(colorTable = colortable.ToByteArray())); additional.Put(PdfName.COLORSPACE, colorspace); } else { Utilities.Skip(isp, len); } } else if (pHYs.Equals(marker)) { int dx = GetInt(isp); int dy = GetInt(isp); int unit = isp.ReadByte(); if (unit == 1) { dpiX = (int)((float)dx * 0.0254f + 0.5f); dpiY = (int)((float)dy * 0.0254f + 0.5f); } else { if (dy != 0) XYRatio = (float)dx / (float)dy; } } else if (cHRM.Equals(marker)) { xW = (float)GetInt(isp) / 100000f; yW = (float)GetInt(isp) / 100000f; xR = (float)GetInt(isp) / 100000f; yR = (float)GetInt(isp) / 100000f; xG = (float)GetInt(isp) / 100000f; yG = (float)GetInt(isp) / 100000f; xB = (float)GetInt(isp) / 100000f; yB = (float)GetInt(isp) / 100000f; hasCHRM = !(Math.Abs(xW)<0.0001f||Math.Abs(yW)<0.0001f||Math.Abs(xR)<0.0001f||Math.Abs(yR)<0.0001f||Math.Abs(xG)<0.0001f||Math.Abs(yG)<0.0001f||Math.Abs(xB)<0.0001f||Math.Abs(yB)<0.0001f); } else if (sRGB.Equals(marker)) { int ri = isp.ReadByte(); intent = intents[ri]; gamma = 2.2f; xW = 0.3127f; yW = 0.329f; xR = 0.64f; yR = 0.33f; xG = 0.3f; yG = 0.6f; xB = 0.15f; yB = 0.06f; hasCHRM = true; } else if (gAMA.Equals(marker)) { int gm = GetInt(isp); if (gm != 0) { gamma = 100000f / (float)gm; if (!hasCHRM) { xW = 0.3127f; yW = 0.329f; xR = 0.64f; yR = 0.33f; xG = 0.3f; yG = 0.6f; xB = 0.15f; yB = 0.06f; hasCHRM = true; } } } else if (iCCP.Equals(marker)) { do { --len; } while (isp.ReadByte() != 0); isp.ReadByte(); --len; byte[] icccom = new byte[len]; int p = 0; while (len > 0) { int r = isp.Read(icccom, p, len); if (r < 0) throw new IOException("Premature end of file."); p += r; len -= r; } byte[] iccp = PdfReader.FlateDecode(icccom, true); icccom = null; try { icc_profile = ICC_Profile.GetInstance(iccp); } catch { icc_profile = null; } } else if (IEND.Equals(marker)) { break; } else { Utilities.Skip(isp, len); } Utilities.Skip(isp, 4); } }
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 || compression == TIFFConstants.COMPRESSION_DEFLATE || compression == TIFFConstants.COMPRESSION_ADOBE_DEFLATE) { 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)); } } } if (compression == TIFFConstants.COMPRESSION_LZW) { 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 = (int)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 = (int)s.FilePointer; posFilePointer += jpegOffset; s.Seek(posFilePointer); s.ReadFully(jpeg); // if quantization and/or Huffman tables are stored separately in the tiff, // we need to add them to the jpeg data TIFFField jpegtables = dir.GetField(TIFFConstants.TIFFTAG_JPEGTABLES); if (jpegtables != null) { byte[] temp = jpegtables.GetAsBytes(); int tableoffset = 0; int tablelength = temp.Length; // remove FFD8 from start if (temp[0] == (byte)0xFF && temp[1] == (byte)0xD8) { tableoffset = 2; tablelength -= 2; } // remove FFD9 from end if (temp[temp.Length - 2] == (byte)0xFF && temp[temp.Length - 1] == (byte)0xD9) { tablelength -= 2; } byte[] tables = new byte[tablelength]; Array.Copy(temp, tableoffset, tables, 0, tablelength); // TODO insert after JFIF header, instead of at the start byte[] jpegwithtables = new byte[jpeg.Length + tables.Length]; Array.Copy(jpeg, 0, jpegwithtables, 0, 2); Array.Copy(tables, 0, jpegwithtables, 2, tables.Length); Array.Copy(jpeg, 2, jpegwithtables, tables.Length + 2, jpeg.Length - 2); jpeg = jpegwithtables; } 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); if (photometric == TIFFConstants.PHOTOMETRIC_RGB) { img.ColorTransform = 0; } } 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); ApplyPredictor(outBuf, predictor, w, height, samplePerPixel); 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); } // Colormap components are supposed to go from 0 to 655535 but, // as usually, some tiff producers just put values from 0 to 255. // Let's check for these broken tiffs. bool colormapBroken = true; for (int k = 0; k < palette.Length; ++k) { if (palette[k] != 0) { colormapBroken = false; break; } } if (colormapBroken) { for (int k = 0; k < gColor; ++k) { palette[k * 3] = (byte)rgb[k]; palette[k * 3 + 1] = (byte)rgb[k + gColor]; palette[k * 3 + 2] = (byte)rgb[k + bColor]; } } 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); }
// private methods /// <summary> /// This method checks if the image is a valid JPEG and processes some parameters. /// </summary> private void ProcessParameters() { type = Element.JPEG; originalType = ORIGINAL_JPEG; Stream istr = null; try { string errorID; if (rawData == null) { WebRequest w = WebRequest.Create(url); w.Credentials = CredentialCache.DefaultCredentials; istr = w.GetResponse().GetResponseStream(); errorID = url.ToString(); } else { istr = new MemoryStream(rawData); errorID = "Byte array"; } if (istr.ReadByte() != 0xFF || istr.ReadByte() != 0xD8) { throw new BadElementException(MessageLocalization.GetComposedMessage("1.is.not.a.valid.jpeg.file", errorID)); } bool firstPass = true; int len; while (true) { int v = istr.ReadByte(); if (v < 0) { throw new IOException(MessageLocalization.GetComposedMessage("premature.eof.while.reading.jpg")); } if (v == 0xFF) { int marker = istr.ReadByte(); if (firstPass && marker == M_APP0) { firstPass = false; len = GetShort(istr); if (len < 16) { Utilities.Skip(istr, len - 2); continue; } byte[] bcomp = new byte[JFIF_ID.Length]; int r = istr.Read(bcomp, 0, bcomp.Length); if (r != bcomp.Length) { throw new BadElementException(MessageLocalization.GetComposedMessage("1.corrupted.jfif.marker", errorID)); } bool found = true; for (int k = 0; k < bcomp.Length; ++k) { if (bcomp[k] != JFIF_ID[k]) { found = false; break; } } if (!found) { Utilities.Skip(istr, len - 2 - bcomp.Length); continue; } Utilities.Skip(istr, 2); int units = istr.ReadByte(); int dx = GetShort(istr); int dy = GetShort(istr); if (units == 1) { dpiX = dx; dpiY = dy; } else if (units == 2) { dpiX = (int)((float)dx * 2.54f + 0.5f); dpiY = (int)((float)dy * 2.54f + 0.5f); } Utilities.Skip(istr, len - 2 - bcomp.Length - 7); continue; } if (marker == M_APPE) { len = GetShort(istr) - 2; byte[] byteappe = new byte[len]; for (int k = 0; k < len; ++k) { byteappe[k] = (byte)istr.ReadByte(); } if (byteappe.Length >= 12) { string appe = System.Text.ASCIIEncoding.ASCII.GetString(byteappe, 0, 5); if (Util.EqualsIgnoreCase(appe, "adobe")) { invert = true; } } continue; } if (marker == M_APP2) { len = GetShort(istr) - 2; byte[] byteapp2 = new byte[len]; for (int k = 0; k < len; ++k) { byteapp2[k] = (byte)istr.ReadByte(); } if (byteapp2.Length >= 14) { String app2 = System.Text.ASCIIEncoding.ASCII.GetString(byteapp2, 0, 11); if (app2.Equals("ICC_PROFILE")) { int order = byteapp2[12] & 0xff; int count = byteapp2[13] & 0xff; // some jpeg producers don't know how to count to 1 if (order < 1) { order = 1; } if (count < 1) { count = 1; } if (icc == null) { icc = new byte[count][]; } icc[order - 1] = byteapp2; } } continue; } if (marker == M_APPD) { len = GetShort(istr) - 2; byte[] byteappd = new byte[len]; for (int k = 0; k < len; k++) { byteappd[k] = (byte)istr.ReadByte(); } // search for '8BIM Resolution' marker int j = 0; for (j = 0; j < len - PS_8BIM_RESO.Length; j++) { bool found = true; for (int i = 0; i < PS_8BIM_RESO.Length; i++) { if (byteappd[j + i] != PS_8BIM_RESO[i]) { found = false; break; } } if (found) { break; } } j += PS_8BIM_RESO.Length; if (j < len - PS_8BIM_RESO.Length) { // "PASCAL String" for name, i.e. string prefix with length byte // padded to be even length; 2 null bytes if empty byte namelength = byteappd[j]; // add length byte namelength++; // add padding if (namelength % 2 == 1) { namelength++; } // just skip name j += namelength; // size of the resolution data int resosize = (byteappd[j] << 24) + (byteappd[j + 1] << 16) + (byteappd[j + 2] << 8) + byteappd[j + 3]; // should be 16 if (resosize != 16) { // fail silently, for now //System.err.println("DEBUG: unsupported resolution IRB size"); continue; } j += 4; int dx = (byteappd[j] << 8) + (byteappd[j + 1] & 0xff); j += 2; // skip 2 unknown bytes j += 2; int unitsx = (byteappd[j] << 8) + (byteappd[j + 1] & 0xff); j += 2; // skip 2 unknown bytes j += 2; int dy = (byteappd[j] << 8) + (byteappd[j + 1] & 0xff); j += 2; // skip 2 unknown bytes j += 2; int unitsy = (byteappd[j] << 8) + (byteappd[j + 1] & 0xff); if (unitsx == 1 || unitsx == 2) { dx = (unitsx == 2 ? (int)(dx * 2.54f + 0.5f) : dx); // make sure this is consistent with JFIF data if (dpiX != 0 && dpiX != dx) { //System.err.println("DEBUG: inconsistent metadata (dpiX: " + dpiX + " vs " + dx + ")"); } else { dpiX = dx; } } if (unitsy == 1 || unitsy == 2) { dy = (unitsy == 2 ? (int)(dy * 2.54f + 0.5f) : dy); // make sure this is consistent with JFIF data if (dpiY != 0 && dpiY != dy) { //System.err.println("DEBUG: inconsistent metadata (dpiY: " + dpiY + " vs " + dy + ")"); } else { dpiY = dy; } } } continue; } firstPass = false; int markertype = MarkerType(marker); if (markertype == VALID_MARKER) { Utilities.Skip(istr, 2); if (istr.ReadByte() != 0x08) { throw new BadElementException(MessageLocalization.GetComposedMessage("1.must.have.8.bits.per.component", errorID)); } scaledHeight = GetShort(istr); Top = scaledHeight; scaledWidth = GetShort(istr); Right = scaledWidth; colorspace = istr.ReadByte(); bpc = 8; break; } else if (markertype == UNSUPPORTED_MARKER) { throw new BadElementException(MessageLocalization.GetComposedMessage("1.unsupported.jpeg.marker.2", errorID, marker)); } else if (markertype != NOPARAM_MARKER) { Utilities.Skip(istr, GetShort(istr) - 2); } } } } finally { if (istr != null) { istr.Close(); } } plainWidth = this.Width; plainHeight = this.Height; if (icc != null) { int total = 0; for (int k = 0; k < icc.Length; ++k) { if (icc[k] == null) { icc = null; return; } total += icc[k].Length - 14; } byte[] ficc = new byte[total]; total = 0; for (int k = 0; k < icc.Length; ++k) { System.Array.Copy(icc[k], 14, ficc, total, icc[k].Length - 14); total += icc[k].Length - 14; } try { ICC_Profile icc_prof = ICC_Profile.GetInstance(ficc, colorspace); TagICC = icc_prof; } catch {} icc = null; } }
/** 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); }
// private methods /// <summary> /// This method checks if the image is a valid JPEG and processes some parameters. /// </summary> private void ProcessParameters() { type = Element.JPEG; originalType = ORIGINAL_JPEG; Stream istr = null; try { string errorID; if (rawData == null) { WebRequest w = WebRequest.Create(url); w.Credentials = CredentialCache.DefaultCredentials; istr = w.GetResponse().GetResponseStream(); errorID = url.ToString(); } else { istr = new MemoryStream(rawData); errorID = "Byte array"; } if (istr.ReadByte() != 0xFF || istr.ReadByte() != 0xD8) { throw new BadElementException(MessageLocalization.GetComposedMessage("1.is.not.a.valid.jpeg.file", errorID)); } bool firstPass = true; int len; while (true) { int v = istr.ReadByte(); if (v < 0) { throw new IOException(MessageLocalization.GetComposedMessage("premature.eof.while.reading.jpg")); } if (v == 0xFF) { int marker = istr.ReadByte(); if (firstPass && marker == M_APP0) { firstPass = false; len = GetShort(istr); if (len < 16) { Utilities.Skip(istr, len - 2); continue; } byte[] bcomp = new byte[JFIF_ID.Length]; int r = istr.Read(bcomp, 0, bcomp.Length); if (r != bcomp.Length) { throw new BadElementException(MessageLocalization.GetComposedMessage("1.corrupted.jfif.marker", errorID)); } bool found = true; for (int k = 0; k < bcomp.Length; ++k) { if (bcomp[k] != JFIF_ID[k]) { found = false; break; } } if (!found) { Utilities.Skip(istr, len - 2 - bcomp.Length); continue; } Utilities.Skip(istr, 2); int units = istr.ReadByte(); int dx = GetShort(istr); int dy = GetShort(istr); if (units == 1) { dpiX = dx; dpiY = dy; } else if (units == 2) { dpiX = (int)((float)dx * 2.54f + 0.5f); dpiY = (int)((float)dy * 2.54f + 0.5f); } Utilities.Skip(istr, len - 2 - bcomp.Length - 7); continue; } if (marker == M_APPE) { len = GetShort(istr) - 2; byte[] byteappe = new byte[len]; for (int k = 0; k < len; ++k) { byteappe[k] = (byte)istr.ReadByte(); } if (byteappe.Length >= 12) { string appe = System.Text.ASCIIEncoding.ASCII.GetString(byteappe, 0, 5); if (Util.EqualsIgnoreCase(appe, "adobe")) { invert = true; } } continue; } if (marker == M_APP2) { len = GetShort(istr) - 2; byte[] byteapp2 = new byte[len]; for (int k = 0; k < len; ++k) { byteapp2[k] = (byte)istr.ReadByte(); } if (byteapp2.Length >= 14) { String app2 = System.Text.ASCIIEncoding.ASCII.GetString(byteapp2, 0, 11); if (app2.Equals("ICC_PROFILE")) { int order = byteapp2[12] & 0xff; int count = byteapp2[13] & 0xff; // some jpeg producers don't know how to count to 1 if (order < 1) { order = 1; } if (count < 1) { count = 1; } if (icc == null) { icc = new byte[count][]; } icc[order - 1] = byteapp2; } } continue; } firstPass = false; int markertype = MarkerType(marker); if (markertype == VALID_MARKER) { Utilities.Skip(istr, 2); if (istr.ReadByte() != 0x08) { throw new BadElementException(MessageLocalization.GetComposedMessage("1.must.have.8.bits.per.component", errorID)); } scaledHeight = GetShort(istr); Top = scaledHeight; scaledWidth = GetShort(istr); Right = scaledWidth; colorspace = istr.ReadByte(); bpc = 8; break; } else if (markertype == UNSUPPORTED_MARKER) { throw new BadElementException(MessageLocalization.GetComposedMessage("1.unsupported.jpeg.marker.2", errorID, marker)); } else if (markertype != NOPARAM_MARKER) { Utilities.Skip(istr, GetShort(istr) - 2); } } } } finally { if (istr != null) { istr.Close(); } } plainWidth = this.Width; plainHeight = this.Height; if (icc != null) { int total = 0; for (int k = 0; k < icc.Length; ++k) { if (icc[k] == null) { icc = null; return; } total += icc[k].Length - 14; } byte[] ficc = new byte[total]; total = 0; for (int k = 0; k < icc.Length; ++k) { System.Array.Copy(icc[k], 14, ficc, total, icc[k].Length - 14); total += icc[k].Length - 14; } try { ICC_Profile icc_prof = ICC_Profile.GetInstance(ficc); TagICC = icc_prof; } catch {} icc = null; } }