/// <summary> /// Loads the image. /// </summary> private void LoadImage() { for (int i = 0; i < PNGID.Length; i++) { if (PNGID[i] != this.stream.ReadByte()) { throw new PdfUnrecognizedImageFormatException("File is not a valid PNG."); } } byte[] buffer = new byte[0x1000]; while (true) { int @int = GetInt(this.stream); string s = GetString(this.stream); if ((@int < 0) || !CheckMarker(s)) { throw new PdfUnrecognizedImageFormatException("Corrupted PNG file."); } if ("IDAT".Equals(s)) { while (@int != 0) { int count = this.stream.Read(buffer, 0, Math.Min(@int, 0x1000)); if (count <= 0) { return; } this.idat.Write(buffer, 0, count); @int -= count; } } else if (!"tRNS".Equals(s)) { if ("IHDR".Equals(s)) { this.width = GetInt(this.stream); this.height = GetInt(this.stream); this.bitDepth = this.stream.ReadByte(); this.colorType = this.stream.ReadByte(); this.stream.ReadByte(); this.stream.ReadByte(); this.interlaceMethod = this.stream.ReadByte(); } else if ("PLTE".Equals(s)) { if (this.colorType == 3) { List <byte> list = new List <byte>(); while (@int-- > 0) { list.Add((byte)this.stream.ReadByte()); } list.ToArray(); PdfIndexedColorSpace space = new PdfIndexedColorSpace(this.GetColorspace(), (@int / 3) - 1, list.ToArray()); this.properties.Add(PdfName.ColorSpace, space); } else { Image.Skip(this.stream, @int); } } else if ("pHYs".Equals(s)) { int num9 = GetInt(this.stream); int num10 = GetInt(this.stream); if (this.stream.ReadByte() == 1) { this.dpiX = (int)((num9 * 0.0254f) + 0.5f); this.dpiY = (int)((num10 * 0.0254f) + 0.5f); } else if (num10 != 0) { this.XYRatio = ((float)num9) / ((float)num10); } } else if ("cHRM".Equals(s)) { this.xW = ((float)GetInt(this.stream)) / 100000f; this.yW = ((float)GetInt(this.stream)) / 100000f; this.xR = ((float)GetInt(this.stream)) / 100000f; this.yR = ((float)GetInt(this.stream)) / 100000f; this.xG = ((float)GetInt(this.stream)) / 100000f; this.yG = ((float)GetInt(this.stream)) / 100000f; this.xB = ((float)GetInt(this.stream)) / 100000f; this.yB = ((float)GetInt(this.stream)) / 100000f; this.hasCHRM = ((((Math.Abs(this.xW) >= 0.0001f) && (Math.Abs(this.yW) >= 0.0001f)) && ((Math.Abs(this.xR) >= 0.0001f) && (Math.Abs(this.yR) >= 0.0001f))) && (((Math.Abs(this.xG) >= 0.0001f) && (Math.Abs(this.yG) >= 0.0001f)) && (Math.Abs(this.xB) >= 0.0001f))) && (Math.Abs(this.yB) >= 0.0001f); } else if ("sRGB".Equals(s)) { int index = this.stream.ReadByte(); this.intent = intents[index]; this.gamma = 2.2f; this.xW = 0.3127f; this.yW = 0.329f; this.xR = 0.64f; this.yR = 0.33f; this.xG = 0.3f; this.yG = 0.6f; this.xB = 0.15f; this.yB = 0.06f; this.hasCHRM = true; } else if ("gAMA".Equals(s)) { int num13 = GetInt(this.stream); if (num13 != 0) { this.gamma = 100000f / ((float)num13); if (!this.hasCHRM) { this.xW = 0.3127f; this.yW = 0.329f; this.xR = 0.64f; this.yR = 0.33f; this.xG = 0.3f; this.yG = 0.6f; this.xB = 0.15f; this.yB = 0.06f; this.hasCHRM = true; } } } else if (!"iCCP".Equals(s)) { if ("IEND".Equals(s)) { return; } Image.Skip(this.stream, @int); } } else { switch (this.colorType) { case 0: if (@int >= 2) { @int -= 2; int word = GetWord(this.stream); if (this.bitDepth != 0x10) { this.properties.Add(PdfName.Mask, PdfArray.Convert(new int[] { word, word })); break; } this.transRedGray = word; } break; case 2: if (@int >= 6) { @int -= 6; int num5 = GetWord(this.stream); int num6 = GetWord(this.stream); int num7 = GetWord(this.stream); if (this.bitDepth != 0x10) { this.properties.Add(PdfName.Mask, PdfArray.Convert(new int[] { num5, num5, num6, num6, num7, num7 })); break; } this.transRedGray = num5; this.transGreen = num6; this.transBlue = num7; } break; case 3: if (@int > 0) { this.trans = new byte[@int]; for (int j = 0; j < @int; j++) { this.trans[j] = (byte)this.stream.ReadByte(); } @int = 0; } break; } Image.Skip(this.stream, @int); } Image.Skip(this.stream, 4); } }
/// <summary> /// Loads the image. /// </summary> private void LoadImage() { base.type = ImageType.JPEG2000; this.inp = null; try { this.inp = new MemoryStream(base.rawData); this.boxLength = this.Cio_read(4); if (this.boxLength == 12) { this.boxType = this.Cio_read(4); if (0x6a502020 != this.boxType) { throw new PdfUnrecognizedImageFormatException("Expected JP Marker"); } if (0xd0a870a != this.Cio_read(4)) { throw new PdfUnrecognizedImageFormatException("Error with JP Marker"); } this.Jp2_read_boxhdr(); if (0x66747970 != this.boxType) { throw new PdfUnrecognizedImageFormatException("Expected FTYP Marker"); } Image.Skip(this.inp, this.boxLength - 8); this.Jp2_read_boxhdr(); do { if (0x6a703268 != this.boxType) { if (this.boxType == 0x6a703263) { throw new PdfUnrecognizedImageFormatException("Expected JP2H Marker"); } Image.Skip(this.inp, this.boxLength - 8); this.Jp2_read_boxhdr(); } }while (0x6a703268 != this.boxType); this.Jp2_read_boxhdr(); if (0x69686472 != this.boxType) { throw new PdfUnrecognizedImageFormatException("Expected IHDR Marker"); } base.height = this.Cio_read(4); base.width = this.Cio_read(4); base.bpc = -1; } else { if (this.boxLength != -11534511) { throw new PdfUnrecognizedImageFormatException("Not a valid Jpeg2000 file"); } Image.Skip(this.inp, 4); int num = this.Cio_read(4); int num2 = this.Cio_read(4); int num3 = this.Cio_read(4); int num4 = this.Cio_read(4); Image.Skip(this.inp, 0x10); base.colorspace = this.Cio_read(2); base.bpc = 8; base.height = num2 - num4; base.width = num - num3; } } finally { if (this.inp != null) { try { this.inp.Dispose(); } catch { } this.inp = null; } } }
/// <summary> /// Gets the image parameters such as width, height, and colorspace. /// </summary> /// <param name="istr">Input image stream</param> /// <returns>ImageParameters structure contains image details</returns> protected void LoadImage(Stream istr) { base.type = ImageType.JPEG; base.bpc = 8; bool flag = true; while (true) { int @short; while (istr.ReadByte() != 0xff) { } int marker = istr.ReadByte(); if (!flag || (marker != 0xe0)) { if (marker == 0xee) { @short = GetShort(istr); byte[] bytes = new byte[@short]; for (int i = 0; i < @short; i++) { bytes[i] = (byte)istr.ReadByte(); } if (bytes.Length > 12) { if (PdfASCIIEncoding.Instance.GetString(bytes, 0, 5).ToLower() != "adobe") { throw new PdfUnrecognizedImageFormatException("Not a supported Jpeg File."); } do { if (istr.ReadByte() == 0xff) { marker = istr.ReadByte(); } }while (MarkerType(marker) != 0); } } flag = false; int num6 = MarkerType(marker); switch (num6) { case 0: Image.Skip(istr, 2); if (istr.ReadByte() != 8) { throw new PdfUnrecognizedImageFormatException("Must have 8 bits per component."); } base.height = GetShort(istr); base.width = GetShort(istr); base.colorspace = (byte)istr.ReadByte(); return; case 1: throw new PdfUnrecognizedImageFormatException("Unsupported JPEG marker: " + ((int)marker)); } if (num6 != 2) { Image.Skip(istr, GetShort(istr) - 2); } } else { flag = false; @short = GetShort(istr); if (@short < 0x10) { Image.Skip(istr, @short - 2); } else { byte[] buffer = new byte[JFIF_ID.Length]; if (istr.Read(buffer, 0, buffer.Length) != buffer.Length) { throw new PdfUnrecognizedImageFormatException("Corrupted JFIF marker."); } bool flag2 = true; for (int j = 0; j < buffer.Length; j++) { if (buffer[j] != JFIF_ID[j]) { flag2 = false; break; } } if (!flag2) { Image.Skip(istr, (@short - 2) - buffer.Length); } else { Image.Skip(istr, 2); Image.Skip(istr, ((@short - 2) - buffer.Length) - 7); } } } } }