/// <summary> /// Creates an ICC stream. /// @since 2.1.3 (replacing the constructor without param compressionLevel) /// </summary> /// <param name="compressionLevel">the compressionLevel</param> /// <param name="profile">an ICC profile</param> public PdfIccBased(IccProfile profile, int compressionLevel) { int numberOfComponents = profile.NumComponents; switch (numberOfComponents) { case 1: Put(PdfName.Alternate, PdfName.Devicegray); break; case 3: Put(PdfName.Alternate, PdfName.Devicergb); break; case 4: Put(PdfName.Alternate, PdfName.Devicecmyk); break; default: throw new PdfException(numberOfComponents + " Component(s) is not supported in iText"); } Put(PdfName.N, new PdfNumber(numberOfComponents)); Bytes = profile.Data; FlateCompress(compressionLevel); }
private void RunTest(String pdfName, String imageName, String colorProfileName, IDictionary <String, Object > customImageAttribute) { String outFileName = destinationFolder + pdfName; String cmpFileName = sourceFolder + "cmp_" + pdfName; String diff = "diff_" + pdfName + "_"; PdfDocument pdf = new PdfDocument(new PdfWriter(outFileName)); Document document = new Document(pdf); ImageData imageData = ImageDataFactory.Create(sourceFolder + imageName); if (customImageAttribute != null) { imageData.GetImageAttributes().AddAll(customImageAttribute); } if (colorProfileName != null) { imageData.SetProfile(IccProfile.GetInstance(sourceFolder + colorProfileName)); } iText.Layout.Element.Image png = new iText.Layout.Element.Image(imageData); png.SetAutoScale(true); document.Add(png); document.Close(); NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder , diff)); }
public static PdfStream GetIccProfileStream(IccProfile iccProfile, float[] range) { PdfStream stream = GetIccProfileStream(iccProfile); stream.Put(PdfName.Range, new PdfArray(range)); return(stream); }
public void ReadProfile_NoEntries() { IccReader reader = this.CreateReader(); IccProfile output = reader.Read(IccTestDataProfiles.Header_Random_Array); Assert.Equal(0, output.Entries.Length); Assert.NotNull(output.Header); IccProfileHeader header = output.Header; IccProfileHeader expected = IccTestDataProfiles.Header_Random_Read; Assert.Equal(header.Class, expected.Class); Assert.Equal(header.CmmType, expected.CmmType); Assert.Equal(header.CreationDate, expected.CreationDate); Assert.Equal(header.CreatorSignature, expected.CreatorSignature); Assert.Equal(header.DataColorSpace, expected.DataColorSpace); Assert.Equal(header.DeviceAttributes, expected.DeviceAttributes); Assert.Equal(header.DeviceManufacturer, expected.DeviceManufacturer); Assert.Equal(header.DeviceModel, expected.DeviceModel); Assert.Equal(header.FileSignature, expected.FileSignature); Assert.Equal(header.Flags, expected.Flags); Assert.Equal(header.Id, expected.Id); Assert.Equal(header.PcsIlluminant, expected.PcsIlluminant); Assert.Equal(header.PrimaryPlatformSignature, expected.PrimaryPlatformSignature); Assert.Equal(header.ProfileConnectionSpace, expected.ProfileConnectionSpace); Assert.Equal(header.RenderingIntent, expected.RenderingIntent); Assert.Equal(header.Size, expected.Size); Assert.Equal(header.Version, expected.Version); }
public static PdfStream GetIccProfileStream(IccProfile iccProfile) { PdfStream stream = new PdfStream(iccProfile.GetData()); stream.Put(PdfName.N, new PdfNumber(iccProfile.GetNumComponents())); switch (iccProfile.GetNumComponents()) { case 1: { stream.Put(PdfName.Alternate, PdfName.DeviceGray); break; } case 3: { stream.Put(PdfName.Alternate, PdfName.DeviceRGB); break; } case 4: { stream.Put(PdfName.Alternate, PdfName.DeviceCMYK); break; } default: { break; } } return(stream); }
public void CheckIsValid_WithProfiles_ReturnsValidity(byte[] data, bool expected) { var profile = new IccProfile(data); bool result = profile.CheckIsValid(); Assert.Equal(expected, result); }
public void ReadProfile_DuplicateEntry() { IccReader reader = this.CreateReader(); IccProfile output = reader.Read(IccTestDataProfiles.Profile_Random_Array); Assert.Equal(2, output.Entries.Length); Assert.True(ReferenceEquals(output.Entries[0], output.Entries[1])); }
public void CalculateHash_WithByteArray_DoesNotModifyData() { byte[] data = IccTestDataProfiles.Profile_Random_Array; byte[] copy = new byte[data.Length]; Buffer.BlockCopy(data, 0, copy, 0, data.Length); IccProfile.CalculateHash(data); Assert.Equal(data, copy); }
private static void TestMetaDataImpl( bool useIdentify, IImageDecoder decoder, string imagePath, int expectedPixelSize, bool exifProfilePresent, bool iccProfilePresent) { var testFile = TestFile.Create(imagePath); using (var stream = new MemoryStream(testFile.Bytes, false)) { IImageInfo imageInfo = useIdentify ? ((IImageInfoDetector)decoder).Identify(Configuration.Default, stream) : decoder.Decode <Rgba32>(Configuration.Default, stream); Assert.NotNull(imageInfo); Assert.NotNull(imageInfo.PixelType); if (useIdentify) { Assert.Equal(expectedPixelSize, imageInfo.PixelType.BitsPerPixel); } else { // When full Image<TPixel> decoding is performed, BitsPerPixel will match TPixel int bpp32 = Unsafe.SizeOf <Rgba32>() * 8; Assert.Equal(bpp32, imageInfo.PixelType.BitsPerPixel); } ExifProfile exifProfile = imageInfo.MetaData.ExifProfile; if (exifProfilePresent) { Assert.NotNull(exifProfile); Assert.NotEmpty(exifProfile.Values); } else { Assert.Null(exifProfile); } IccProfile iccProfile = imageInfo.MetaData.IccProfile; if (iccProfilePresent) { Assert.NotNull(iccProfile); Assert.NotEmpty(iccProfile.Entries); } else { Assert.Null(iccProfile); } } }
/// <summary> /// Initializes the ICC profile. /// </summary> private void InitIccProfile() { if (this.isIcc) { var profile = new IccProfile(this.iccData); if (profile.CheckIsValid()) { this.Metadata.IccProfile = profile; } } }
private static void TestMetadataImpl( bool useIdentify, IImageDecoder decoder, string imagePath, int expectedPixelSize, bool exifProfilePresent, bool iccProfilePresent) { TestImageInfo( imagePath, decoder, useIdentify, imageInfo => { Assert.NotNull(imageInfo); Assert.NotNull(imageInfo.PixelType); if (useIdentify) { Assert.Equal(expectedPixelSize, imageInfo.PixelType.BitsPerPixel); } else { // When full Image<TPixel> decoding is performed, BitsPerPixel will match TPixel int bpp32 = Unsafe.SizeOf <Rgba32>() * 8; Assert.Equal(bpp32, imageInfo.PixelType.BitsPerPixel); } ExifProfile exifProfile = imageInfo.Metadata.ExifProfile; if (exifProfilePresent) { Assert.NotNull(exifProfile); Assert.NotEmpty(exifProfile.Values); } else { Assert.Null(exifProfile); } IccProfile iccProfile = imageInfo.Metadata.IccProfile; if (iccProfilePresent) { Assert.NotNull(iccProfile); Assert.NotEmpty(iccProfile.Entries); } else { Assert.Null(iccProfile); } }); }
private void SetCheckerOutputIntent(PdfDictionary outputIntent) { if (outputIntent != null) { PdfStream destOutputProfile = outputIntent.GetAsStream(PdfName.DestOutputProfile); if (destOutputProfile != null) { String intentCS = IccProfile.GetIccColorSpaceName(destOutputProfile.GetBytes()); this.pdfAOutputIntentColorSpace = intentCS; } } }
public void WriteProfile_NoEntries() { IccWriter writer = CreateWriter(); IccProfile profile = new IccProfile { Header = IccTestDataProfiles.Header_Random_Write }; byte[] output = writer.Write(profile); Assert.Equal(IccTestDataProfiles.Header_Random_Array, output); }
/// <summary> /// Handles the Click event of RemoveInputRgbButton object. /// </summary> private void removeInputRgbButton_Click(object sender, EventArgs e) { // get input RGB profile IccProfile profile = _colorManagementSettings.InputRgbProfile; // if RGB profile is exist if (profile != null) { // remove input RGB profile _colorManagementSettings.InputRgbProfile = null; profile.Dispose(); } // update user interface RemoveProfileDescription(inputRgbTextBox); }
/// <summary> /// Handles the Click event of RemoveOutputGrayscaleButton object. /// </summary> private void removeOutputGrayscaleButton_Click(object sender, EventArgs e) { // get output grayscale profile IccProfile profile = _colorManagementSettings.OutputGrayscaleProfile; // if grayscale profile is exist if (profile != null) { // remove output grayscale profile _colorManagementSettings.OutputGrayscaleProfile = null; profile.Dispose(); } // update user interface RemoveProfileDescription(outputGrayscaleTextBox); }
/// <summary> /// Handles the Click event of SetInputProfileButton object. /// </summary> private void setInputProfileButton_Click(object sender, EventArgs e) { // if input ICC profile is selected if (_openIccFileDialog.ShowDialog() == DialogResult.OK) { try { // create ICC profile IccProfile iccProfile = new IccProfile(_openIccFileDialog.FileName); _openIccFileDialog.InitialDirectory = Path.GetDirectoryName(_openIccFileDialog.FileName); IccProfile oldIccProfile = null; switch (iccProfile.DeviceColorSpace) { case ColorSpaceType.CMYK: oldIccProfile = _colorManagementSettings.InputCmykProfile; _colorManagementSettings.InputCmykProfile = iccProfile; ShowProfileDescription(inputCmykTextBox, iccProfile); break; case ColorSpaceType.sRGB: oldIccProfile = _colorManagementSettings.InputRgbProfile; _colorManagementSettings.InputRgbProfile = iccProfile; ShowProfileDescription(inputRgbTextBox, iccProfile); break; case ColorSpaceType.Gray: oldIccProfile = _colorManagementSettings.InputGrayscaleProfile; _colorManagementSettings.InputGrayscaleProfile = iccProfile; ShowProfileDescription(inputGrayscaleTextBox, iccProfile); break; default: iccProfile.Dispose(); throw new Exception(string.Format("Unexpected profile color space: {0}.", iccProfile.DeviceColorSpace)); } if (oldIccProfile != null) { oldIccProfile.Dispose(); } } catch (Exception ex) { DemosTools.ShowErrorMessage(ex); } } }
/// <summary> /// Handles the Click event of OutputProfileButton object. /// </summary> private void outputProfileButton_Click(object sender, EventArgs e) { if (openFileDialog1.ShowDialog() == DialogResult.OK) { string filePath = openFileDialog1.FileName; openFileDialog1.InitialDirectory = Path.GetDirectoryName(filePath); IccProfile iccProfile = new IccProfile(filePath); IccProfile oldProfile = null; if (iccProfile.DeviceColorSpace != _imageColorSpaceFormat.ColorSpace) { DemosTools.ShowErrorMessage(string.Format("Unexpected profile color space: {0}.\nRequired profile color space: {1}.", iccProfile.DeviceColorSpace, _imageColorSpaceFormat.ColorSpace)); oldProfile = iccProfile; } else { if (iccProfile.DeviceColorSpace == ColorSpaceType.sRGB) { oldProfile = _settings.InputRgbProfile; _settings.OutputRgbProfile = iccProfile; } else { oldProfile = _settings.InputGrayscaleProfile; _settings.OutputGrayscaleProfile = iccProfile; } ExecuteProcessing(); } if (iccProfile != oldProfile) { outputProfileTextBox.Text = filePath; } if (oldProfile != null) { oldProfile.Dispose(); } } }
public void Encode_PreservesIccProfile() { // arrange using var input = new Image <Rgba32>(1, 1); input.Metadata.IccProfile = new IccProfile(IccTestDataProfiles.Profile_Random_Array); // act using var memStream = new MemoryStream(); input.Save(memStream, JpegEncoder); // assert memStream.Position = 0; using var output = Image.Load <Rgba32>(memStream); IccProfile actual = output.Metadata.IccProfile; Assert.NotNull(actual); IccProfile values = input.Metadata.IccProfile; Assert.Equal(values.Entries, actual.Entries); }
public void CloneIsDeep() { // arrange var exifProfile = new ExifProfile(); exifProfile.SetValue(ExifTag.Software, "UnitTest"); exifProfile.SetValue(ExifTag.Artist, "UnitTest"); var xmpProfile = new XmpProfile(new byte[0]); var iccProfile = new IccProfile() { Header = new IccProfileHeader() { CmmType = "Unittest" } }; var iptcProfile = new ImageSharp.Metadata.Profiles.Iptc.IptcProfile(); var metaData = new ImageFrameMetadata() { XmpProfile = xmpProfile, ExifProfile = exifProfile, IccProfile = iccProfile, IptcProfile = iptcProfile }; // act ImageFrameMetadata clone = metaData.DeepClone(); // assert Assert.NotNull(clone); Assert.NotNull(clone.ExifProfile); Assert.NotNull(clone.XmpProfile); Assert.NotNull(clone.IccProfile); Assert.NotNull(clone.IptcProfile); Assert.False(metaData.ExifProfile.Equals(clone.ExifProfile)); Assert.True(metaData.ExifProfile.Values.Count == clone.ExifProfile.Values.Count); Assert.False(ReferenceEquals(metaData.XmpProfile, clone.XmpProfile)); Assert.True(metaData.XmpProfile.Data.Equals(clone.XmpProfile.Data)); Assert.False(metaData.GetGifMetadata().Equals(clone.GetGifMetadata())); Assert.False(metaData.IccProfile.Equals(clone.IccProfile)); Assert.False(metaData.IptcProfile.Equals(clone.IptcProfile)); }
/// <summary> /// Sets PDF/A Conformance ColorProfile. /// </summary> public void SetColorProfile() { if (PageSetup.ConformanceLevel == PdfXConformance.PDFXNONE) { return; } var pdfDictionary = new PdfDictionary(PdfName.Outputintent); pdfDictionary.Put(PdfName.Outputconditionidentifier, new PdfString("sRGB IEC61966-2.1")); pdfDictionary.Put(PdfName.Info, new PdfString("sRGB IEC61966-2.1")); pdfDictionary.Put(PdfName.S, PdfName.GtsPdfa1); var profileStream = StreamHelper.GetResourceByName("PdfRpt.Core.core.helper.srgb.profile"); var pdfICCBased = new PdfIccBased(IccProfile.GetInstance(profileStream)); pdfICCBased.Remove(PdfName.Alternate); pdfDictionary.Put(PdfName.Destoutputprofile, PdfWriter.AddToBody(pdfICCBased).IndirectReference); PdfWriter.ExtraCatalog.Put(PdfName.Outputintents, new PdfArray(pdfDictionary)); }
/// <summary> /// Handles the Click event of RemoveOutputProfileButton object. /// </summary> private void removeOutputProfileButton_Click(object sender, EventArgs e) { IccProfile profile = null; if (_settings.OutputRgbProfile != null) { profile = _settings.OutputRgbProfile; _settings.OutputRgbProfile = null; } else if (_settings.OutputGrayscaleProfile != null) { profile = _settings.OutputGrayscaleProfile; _settings.OutputGrayscaleProfile = null; } if (profile != null) { profile.Dispose(); } outputProfileTextBox.Text = string.Empty; ExecuteProcessing(); }
private static void ProcessTiffImageColor(TIFFDirectory dir, RandomAccessFileOrArray s, TiffImageHelper.TiffParameters tiff) { try { int compression = (int)dir.GetFieldAsLong(TIFFConstants.TIFFTAG_COMPRESSION); int predictor = 1; TIFFLZWDecoder lzwDecoder = null; 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 iText.IO.IOException(iText.IO.IOException.Compression1IsNotSupported).SetMessageParams(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 iText.IO.IOException(iText.IO.IOException.Photometric1IsNotSupported).SetMessageParams(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 iText.IO.IOException(iText.IO.IOException.PlanarImagesAreNotSupported); } 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 iText.IO.IOException(iText.IO.IOException.BitsPerSample1IsNotSupported).SetMessageParams(bitsPerSample ); } } int h = (int)dir.GetFieldAsLong(TIFFConstants.TIFFTAG_IMAGELENGTH); int w = (int)dir.GetFieldAsLong(TIFFConstants.TIFFTAG_IMAGEWIDTH); int dpiX; int dpiY; 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; TIFFField fillOrderField = dir.GetField(TIFFConstants.TIFFTAG_FILLORDER); if (fillOrderField != null) { fillOrder = fillOrderField.GetAsInt(0); } bool 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 iText.IO.IOException(iText.IO.IOException.IllegalValueForPredictorInTiffFile); } if (predictor == 2 && bitsPerSample != 8) { throw new iText.IO.IOException(iText.IO.IOException._1BitSamplesAreNotSupportedForHorizontalDifferencingPredictor ).SetMessageParams(bitsPerSample); } } } if (compression == TIFFConstants.COMPRESSION_LZW) { lzwDecoder = new TIFFLZWDecoder(w, predictor, samplePerPixel); } int rowsLeft = h; ByteArrayOutputStream stream = null; ByteArrayOutputStream mstream = null; DeflaterOutputStream zip = null; DeflaterOutputStream mzip = null; if (extraSamples > 0) { mstream = new ByteArrayOutputStream(); mzip = new DeflaterOutputStream(mstream); } CCITTG4Encoder g4 = null; if (bitsPerSample == 1 && samplePerPixel == 1 && photometric != TIFFConstants.PHOTOMETRIC_PALETTE) { g4 = new CCITTG4Encoder(w); } else { stream = new ByteArrayOutputStream(); if (compression != TIFFConstants.COMPRESSION_OJPEG && compression != TIFFConstants.COMPRESSION_JPEG) { zip = new DeflaterOutputStream(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 iText.IO.IOException(iText.IO.IOException.MissingTagsForOjpegCompression); } 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, (int)s.Length() - jpegOffset)]; int posFilePointer = (int)s.GetPosition(); posFilePointer += jpegOffset; s.Seek(posFilePointer); s.ReadFully(jpeg); tiff.image.data = jpeg; tiff.image.SetOriginalType(ImageType.JPEG); JpegImageHelper.ProcessImage(tiff.image); tiff.jpegProcessing = true; } else { if (compression == TIFFConstants.COMPRESSION_JPEG) { if (size.Length > 1) { throw new iText.IO.IOException(iText.IO.IOException.CompressionJpegIsOnlySupportedWithASingleStripThisImageHas1Strips ).SetMessageParams(size.Length); } byte[] jpeg = new byte[(int)size[0]]; s.Seek(offset[0]); 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]; System.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]; System.Array.Copy(jpeg, 0, jpegwithtables, 0, 2); System.Array.Copy(tables, 0, jpegwithtables, 2, tables.Length); System.Array.Copy(jpeg, 2, jpegwithtables, tables.Length + 2, jpeg.Length - 2); jpeg = jpegwithtables; } tiff.image.data = jpeg; tiff.image.SetOriginalType(ImageType.JPEG); JpegImageHelper.ProcessImage(tiff.image); tiff.jpegProcessing = true; if (photometric == TIFFConstants.PHOTOMETRIC_RGB) { tiff.image.SetColorTransform(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: { FilterUtil.InflateData(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); } } rowsLeft -= rowsStrip; } if (bitsPerSample == 1 && samplePerPixel == 1 && photometric != TIFFConstants.PHOTOMETRIC_PALETTE) { RawImageHelper.UpdateRawImageParameters(tiff.image, w, h, false, RawImageData.CCITTG4, photometric == TIFFConstants .PHOTOMETRIC_MINISBLACK ? RawImageData.CCITT_BLACKIS1 : 0, g4.Close(), null); } else { zip.Close(); RawImageHelper.UpdateRawImageParameters(tiff.image, w, h, samplePerPixel - extraSamples, bitsPerSample, stream .ToArray()); tiff.image.SetDeflated(true); } } } tiff.image.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); IccProfile icc_prof = IccProfile.GetInstance(fd.GetAsBytes()); if (samplePerPixel - extraSamples == icc_prof.GetNumComponents()) { tiff.image.SetProfile(icc_prof); } } catch (Exception) { } } //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) { //there is no sense in >>> for unsigned char 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_1 = 0; k_1 < palette.Length; ++k_1) { if (palette[k_1] != 0) { colormapBroken = false; break; } } if (colormapBroken) { for (int k_2 = 0; k_2 < gColor; ++k_2) { palette[k_2 * 3] = (byte)rgb[k_2]; palette[k_2 * 3 + 1] = (byte)rgb[k_2 + gColor]; palette[k_2 * 3 + 2] = (byte)rgb[k_2 + bColor]; } } Object[] indexed = new Object[4]; indexed[0] = "Indexed"; indexed[1] = "DeviceRGB"; indexed[2] = gColor - 1; indexed[3] = PdfEncodings.ConvertToString(palette, null); tiff.additional = new Dictionary <String, Object>(); tiff.additional["ColorSpace"] = indexed; } } if (photometric == TIFFConstants.PHOTOMETRIC_MINISWHITE) { tiff.image.SetInverted(true); } if (rotation != 0) { tiff.image.SetRotation(rotation); } if (extraSamples > 0) { mzip.Close(); RawImageData mimg = (RawImageData)ImageDataFactory.CreateRawImage(null); RawImageHelper.UpdateRawImageParameters(mimg, w, h, 1, bitsPerSample, mstream.ToArray()); mimg.MakeMask(); mimg.SetDeflated(true); tiff.image.SetImageMask(mimg); } } catch (Exception) { throw new iText.IO.IOException(iText.IO.IOException.CannotGetTiffImageColor); } }
private static void ProcessTiffImage(RandomAccessFileOrArray s, TiffImageHelper.TiffParameters tiff) { bool recoverFromImageError = tiff.image.IsRecoverFromImageError(); int page = tiff.image.GetPage(); bool direct = tiff.image.IsDirect(); if (page < 1) { throw new iText.IO.IOException(iText.IO.IOException.PageNumberMustBeGtEq1); } try { TIFFDirectory dir = new TIFFDirectory(s, page - 1); if (dir.IsTagPresent(TIFFConstants.TIFFTAG_TILEWIDTH)) { throw new iText.IO.IOException(iText.IO.IOException.TilesAreNotSupported); } 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: { ProcessTiffImageColor(dir, s, tiff); return; } } 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); } } } } 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); float XYRatio = 0; int resolutionUnit = TIFFConstants.RESUNIT_INCH; if (dir.IsTagPresent(TIFFConstants.TIFFTAG_RESOLUTIONUNIT)) { resolutionUnit = (int)dir.GetFieldAsLong(TIFFConstants.TIFFTAG_RESOLUTIONUNIT); } int dpiX = GetDpi(dir.GetField(TIFFConstants.TIFFTAG_XRESOLUTION), resolutionUnit); int 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 parameters = 0; if (dir.IsTagPresent(TIFFConstants.TIFFTAG_PHOTOMETRIC)) { long photo = dir.GetFieldAsLong(TIFFConstants.TIFFTAG_PHOTOMETRIC); if (photo == TIFFConstants.PHOTOMETRIC_MINISBLACK) { parameters |= RawImageData.CCITT_BLACKIS1; } } int imagecomp = 0; switch (compression) { case TIFFConstants.COMPRESSION_CCITTRLEW: case TIFFConstants.COMPRESSION_CCITTRLE: { imagecomp = RawImageData.CCITTG3_1D; parameters |= RawImageData.CCITT_ENCODEDBYTEALIGN | RawImageData.CCITT_ENDOFBLOCK; break; } case TIFFConstants.COMPRESSION_CCITTFAX3: { imagecomp = RawImageData.CCITTG3_1D; parameters |= RawImageData.CCITT_ENDOFLINE | RawImageData.CCITT_ENDOFBLOCK; TIFFField t4OptionsField = dir.GetField(TIFFConstants.TIFFTAG_GROUP3OPTIONS); if (t4OptionsField != null) { tiffT4Options = t4OptionsField.GetAsLong(0); if ((tiffT4Options & TIFFConstants.GROUP3OPT_2DENCODING) != 0) { imagecomp = RawImageData.CCITTG3_2D; } if ((tiffT4Options & TIFFConstants.GROUP3OPT_FILLBITS) != 0) { parameters |= RawImageData.CCITT_ENCODEDBYTEALIGN; } } break; } case TIFFConstants.COMPRESSION_CCITTFAX4: { imagecomp = RawImageData.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); RawImageHelper.UpdateRawImageParameters(tiff.image, w, h, false, imagecomp, parameters, im, null); tiff.image.SetInverted(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); decoder.SetRecoverFromImageError(recoverFromImageError); 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 (Exception) { if (!recoverFromImageError) { throw e; } if (rowsStrip == 1) { throw e; } // repeat of reading the tiff directly (the if section of this if else structure) // copy pasted to avoid making a method with 10 tiff im = new byte[(int)size[0]]; s.Seek(offset[0]); s.ReadFully(im); RawImageHelper.UpdateRawImageParameters(tiff.image, w, h, false, imagecomp, parameters, im, null); tiff.image.SetInverted(true); tiff.image.SetDpi(dpiX, dpiY); tiff.image.SetXYRatio(XYRatio); if (rotation != 0) { tiff.image.SetRotation(rotation); } return; } } g4.Fax4Encode(outBuf, height); break; } case TIFFConstants.COMPRESSION_CCITTFAX4: { try { decoder.DecodeT6(outBuf, im, 0, height, tiffT6Options); } catch (iText.IO.IOException e) { if (!recoverFromImageError) { throw; } } g4.Fax4Encode(outBuf, height); break; } } rowsLeft -= rowsStrip; } byte[] g4pic = g4.Close(); RawImageHelper.UpdateRawImageParameters(tiff.image, w, h, false, RawImageData.CCITTG4, parameters & RawImageData .CCITT_BLACKIS1, g4pic, null); } tiff.image.SetDpi(dpiX, dpiY); if (dir.IsTagPresent(TIFFConstants.TIFFTAG_ICCPROFILE)) { try { TIFFField fd = dir.GetField(TIFFConstants.TIFFTAG_ICCPROFILE); IccProfile icc_prof = IccProfile.GetInstance(fd.GetAsBytes()); if (icc_prof.GetNumComponents() == 1) { tiff.image.SetProfile(icc_prof); } } catch (Exception) { } } //empty if (rotation != 0) { tiff.image.SetRotation(rotation); } } catch (Exception) { throw new iText.IO.IOException(iText.IO.IOException.CannotReadTiffImage); } }
/// <summary> /// Creates an ICC stream. /// </summary> /// <param name="profile">an ICC profile</param> public PdfIccBased(IccProfile profile) : this(profile, DEFAULT_COMPRESSION) { }
public virtual void SetProfile(IccProfile profile) { this.profile = profile; }
public void CalculateHash_WithByteArray_CalculatesProfileHash(byte[] data, IccProfileId expected) { IccProfileId result = IccProfile.CalculateHash(data); Assert.Equal(expected, result); }
/// <summary>This method checks if the image is a valid JPEG and processes some parameters.</summary> /// <exception cref="iText.IO.IOException"/> /// <exception cref="System.IO.IOException"/> private static void ProcessParameters(Stream jpegStream, String errorID, ImageData image) { byte[][] icc = null; if (jpegStream.Read() != 0xFF || jpegStream.Read() != 0xD8) { throw new iText.IO.IOException(iText.IO.IOException._1IsNotAValidJpegFile).SetMessageParams(errorID); } bool firstPass = true; int len; while (true) { int v = jpegStream.Read(); if (v < 0) { throw new iText.IO.IOException(iText.IO.IOException.PrematureEofWhileReadingJpeg); } if (v == 0xFF) { int marker = jpegStream.Read(); if (firstPass && marker == M_APP0) { firstPass = false; len = GetShort(jpegStream); if (len < 16) { StreamUtil.Skip(jpegStream, len - 2); continue; } byte[] bcomp = new byte[JFIF_ID.Length]; int r = jpegStream.Read(bcomp); if (r != bcomp.Length) { throw new iText.IO.IOException(iText.IO.IOException._1CorruptedJfifMarker).SetMessageParams(errorID); } bool found = true; for (int k = 0; k < bcomp.Length; ++k) { if (bcomp[k] != JFIF_ID[k]) { found = false; break; } } if (!found) { StreamUtil.Skip(jpegStream, len - 2 - bcomp.Length); continue; } StreamUtil.Skip(jpegStream, 2); int units = jpegStream.Read(); int dx = GetShort(jpegStream); int dy = GetShort(jpegStream); if (units == 1) { image.SetDpi(dx, dy); } else { if (units == 2) { image.SetDpi((int)(dx * 2.54f + 0.5f), (int)(dy * 2.54f + 0.5f)); } } StreamUtil.Skip(jpegStream, len - 2 - bcomp.Length - 7); continue; } if (marker == M_APPE) { len = GetShort(jpegStream) - 2; byte[] byteappe = new byte[len]; for (int k = 0; k < len; ++k) { byteappe[k] = (byte)jpegStream.Read(); } if (byteappe.Length >= 12) { String appe = iText.IO.Util.JavaUtil.GetStringForBytes(byteappe, 0, 5, "ISO-8859-1"); if (appe.Equals("Adobe")) { image.SetInverted(true); } } continue; } if (marker == M_APP2) { len = GetShort(jpegStream) - 2; byte[] byteapp2 = new byte[len]; for (int k = 0; k < len; ++k) { byteapp2[k] = (byte)jpegStream.Read(); } if (byteapp2.Length >= 14) { String app2 = iText.IO.Util.JavaUtil.GetStringForBytes(byteapp2, 0, 11, "ISO-8859-1"); 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(jpegStream) - 2; byte[] byteappd = new byte[len]; for (int k = 0; k < len; k++) { byteappd[k] = (byte)jpegStream.Read(); } // search for '8BIM Resolution' marker int k_1; for (k_1 = 0; k_1 < len - PS_8BIM_RESO.Length; k_1++) { bool found = true; for (int j = 0; j < PS_8BIM_RESO.Length; j++) { if (byteappd[k_1 + j] != PS_8BIM_RESO[j]) { found = false; break; } } if (found) { break; } } k_1 += PS_8BIM_RESO.Length; if (k_1 < 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[k_1]; // add length byte namelength++; // add padding if (namelength % 2 == 1) { namelength++; } // just skip name k_1 += namelength; // size of the resolution data int resosize = (byteappd[k_1] << 24) + (byteappd[k_1 + 1] << 16) + (byteappd[k_1 + 2] << 8) + byteappd[k_1 + 3]; // should be 16 if (resosize != 16) { // fail silently, for now //System.err.println("DEBUG: unsupported resolution IRB size"); continue; } k_1 += 4; int dx = (byteappd[k_1] << 8) + (byteappd[k_1 + 1] & 0xff); k_1 += 2; // skip 2 unknown bytes k_1 += 2; int unitsx = (byteappd[k_1] << 8) + (byteappd[k_1 + 1] & 0xff); k_1 += 2; // skip 2 unknown bytes k_1 += 2; int dy = (byteappd[k_1] << 8) + (byteappd[k_1 + 1] & 0xff); k_1 += 2; // skip 2 unknown bytes k_1 += 2; int unitsy = (byteappd[k_1] << 8) + (byteappd[k_1 + 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 (image.GetDpiX() != 0 && image.GetDpiX() != dx) { ILog logger = LogManager.GetLogger(typeof(JpegImageHelper)); logger.Debug(MessageFormatUtil.Format("Inconsistent metadata (dpiX: {0} vs {1})", image.GetDpiX(), dx)); } else { image.SetDpi(dx, image.GetDpiY()); } } if (unitsy == 1 || unitsy == 2) { dy = (unitsy == 2 ? (int)(dy * 2.54f + 0.5f) : dy); // make sure this is consistent with JFIF data if (image.GetDpiY() != 0 && image.GetDpiY() != dy) { ILog logger = LogManager.GetLogger(typeof(JpegImageHelper)); logger.Debug(MessageFormatUtil.Format("Inconsistent metadata (dpiY: {0} vs {1})", image.GetDpiY(), dy)); } else { image.SetDpi(image.GetDpiX(), dx); } } } continue; } firstPass = false; int markertype = Marker(marker); if (markertype == VALID_MARKER) { StreamUtil.Skip(jpegStream, 2); if (jpegStream.Read() != 0x08) { throw new iText.IO.IOException(iText.IO.IOException._1MustHave8BitsPerComponent).SetMessageParams(errorID); } image.SetHeight(GetShort(jpegStream)); image.SetWidth(GetShort(jpegStream)); image.SetColorSpace(jpegStream.Read()); image.SetBpc(8); break; } else { if (markertype == UNSUPPORTED_MARKER) { throw new iText.IO.IOException(iText.IO.IOException._1UnsupportedJpegMarker2).SetMessageParams(errorID, JavaUtil.IntegerToString (marker)); } else { if (markertype != NOPARAM_MARKER) { StreamUtil.Skip(jpegStream, GetShort(jpegStream) - 2); } } } } } 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) { Array.Copy(icc[k], 14, ficc, total, icc[k].Length - 14); total += icc[k].Length - 14; } try { image.SetProfile(IccProfile.GetInstance(ficc, image.GetColorSpace())); } catch (ArgumentException) { } } }
/// <summary> /// Writes the ICC profile. /// </summary> /// <param name="iccProfile">The ICC profile to write.</param> /// <exception cref="ImageFormatException"> /// Thrown if any of the ICC profiles size exceeds the limit /// </exception> private void WriteIccProfile(IccProfile iccProfile) { // Just incase someone set the value to null by accident. if (iccProfile == null) { return; } const int IccOverheadLength = 14; const int Max = 65533; const int MaxData = Max - IccOverheadLength; byte[] data = iccProfile.ToByteArray(); if (data == null || data.Length == 0) { return; } // Calculate the number of markers we'll need, rounding up of course int dataLength = data.Length; int count = dataLength / MaxData; if (count * MaxData != dataLength) { count++; } // Per spec, counting starts at 1. int current = 1; int offset = 0; while (dataLength > 0) { int length = dataLength; // Number of bytes to write. if (length > MaxData) { length = MaxData; } dataLength -= length; this.buffer[0] = JpegConstants.Markers.XFF; this.buffer[1] = JpegConstants.Markers.APP2; // Application Marker int markerLength = length + 16; this.buffer[2] = (byte)((markerLength >> 8) & 0xFF); this.buffer[3] = (byte)(markerLength & 0xFF); this.outputStream.Write(this.buffer, 0, 4); this.buffer[0] = (byte)'I'; this.buffer[1] = (byte)'C'; this.buffer[2] = (byte)'C'; this.buffer[3] = (byte)'_'; this.buffer[4] = (byte)'P'; this.buffer[5] = (byte)'R'; this.buffer[6] = (byte)'O'; this.buffer[7] = (byte)'F'; this.buffer[8] = (byte)'I'; this.buffer[9] = (byte)'L'; this.buffer[10] = (byte)'E'; this.buffer[11] = 0x00; this.buffer[12] = (byte)current; // The position within the collection. this.buffer[13] = (byte)count; // The total number of profiles. this.outputStream.Write(this.buffer, 0, IccOverheadLength); this.outputStream.Write(data, offset, length); current++; offset += length; } }
/// <exception cref="System.IO.IOException"/> private static void ReadPng(Stream pngStream, PngImageHelper.PngParameters png) { for (int i = 0; i < PNGID.Length; i++) { if (PNGID[i] != pngStream.Read()) { throw new System.IO.IOException("file.is.not.a.valid.png"); } } byte[] buffer = new byte[TRANSFERSIZE]; while (true) { int len = GetInt(pngStream); String marker = GetString(pngStream); if (len < 0 || !CheckMarker(marker)) { throw new System.IO.IOException("corrupted.png.file"); } if (IDAT.Equals(marker)) { int size; while (len != 0) { size = pngStream.JRead(buffer, 0, Math.Min(len, TRANSFERSIZE)); if (size < 0) { return; } png.idat.Write(buffer, 0, size); len -= size; } } else { if (tRNS.Equals(marker)) { switch (png.colorType) { case 0: { if (len >= 2) { len -= 2; int gray = GetWord(pngStream); if (png.bitDepth == 16) { png.transRedGray = gray; } else { png.additional.Put("Mask", MessageFormatUtil.Format("[{0} {1}]", gray, gray)); } } break; } case 2: { if (len >= 6) { len -= 6; int red = GetWord(pngStream); int green = GetWord(pngStream); int blue = GetWord(pngStream); if (png.bitDepth == 16) { png.transRedGray = red; png.transGreen = green; png.transBlue = blue; } else { png.additional.Put("Mask", MessageFormatUtil.Format("[{0} {1} {2} {3} {4} {5}]", red, red, green, green, blue , blue)); } } break; } case 3: { if (len > 0) { png.trans = new byte[len]; for (int k = 0; k < len; ++k) { png.trans[k] = (byte)pngStream.Read(); } len = 0; } break; } } StreamUtil.Skip(pngStream, len); } else { if (IHDR.Equals(marker)) { png.width = GetInt(pngStream); png.height = GetInt(pngStream); png.bitDepth = pngStream.Read(); png.colorType = pngStream.Read(); png.compressionMethod = pngStream.Read(); png.filterMethod = pngStream.Read(); png.interlaceMethod = pngStream.Read(); } else { if (PLTE.Equals(marker)) { if (png.colorType == 3) { Object[] colorspace = new Object[4]; colorspace[0] = "/Indexed"; colorspace[1] = GetColorspace(png); colorspace[2] = len / 3 - 1; ByteBuffer colorTableBuf = new ByteBuffer(); while ((len--) > 0) { colorTableBuf.Append(pngStream.Read()); } png.colorTable = colorTableBuf.ToByteArray(); colorspace[3] = PdfEncodings.ConvertToString(png.colorTable, null); png.additional.Put("ColorSpace", colorspace); } else { StreamUtil.Skip(pngStream, len); } } else { if (pHYs.Equals(marker)) { int dx = GetInt(pngStream); int dy = GetInt(pngStream); int unit = pngStream.Read(); if (unit == 1) { png.dpiX = (int)(dx * 0.0254f + 0.5f); png.dpiY = (int)(dy * 0.0254f + 0.5f); } else { if (dy != 0) { png.XYRatio = (float)dx / (float)dy; } } } else { if (cHRM.Equals(marker)) { png.xW = GetInt(pngStream) / 100000f; png.yW = GetInt(pngStream) / 100000f; png.xR = GetInt(pngStream) / 100000f; png.yR = GetInt(pngStream) / 100000f; png.xG = GetInt(pngStream) / 100000f; png.yG = GetInt(pngStream) / 100000f; png.xB = GetInt(pngStream) / 100000f; png.yB = GetInt(pngStream) / 100000f; png.hasCHRM = !(Math.Abs(png.xW) < 0.0001f || Math.Abs(png.yW) < 0.0001f || Math.Abs(png.xR) < 0.0001f || Math.Abs(png.yR) < 0.0001f || Math.Abs(png.xG) < 0.0001f || Math.Abs(png.yG) < 0.0001f || Math.Abs(png .xB) < 0.0001f || Math.Abs(png.yB) < 0.0001f); } else { if (sRGB.Equals(marker)) { int ri = pngStream.Read(); png.intent = intents[ri]; png.gamma = 2.2f; png.xW = 0.3127f; png.yW = 0.329f; png.xR = 0.64f; png.yR = 0.33f; png.xG = 0.3f; png.yG = 0.6f; png.xB = 0.15f; png.yB = 0.06f; png.hasCHRM = true; } else { if (gAMA.Equals(marker)) { int gm = GetInt(pngStream); if (gm != 0) { png.gamma = 100000f / gm; if (!png.hasCHRM) { png.xW = 0.3127f; png.yW = 0.329f; png.xR = 0.64f; png.yR = 0.33f; png.xG = 0.3f; png.yG = 0.6f; png.xB = 0.15f; png.yB = 0.06f; png.hasCHRM = true; } } } else { if (iCCP.Equals(marker)) { do { --len; }while (pngStream.Read() != 0); pngStream.Read(); --len; byte[] icccom = new byte[len]; int p = 0; while (len > 0) { int r = pngStream.JRead(icccom, p, len); if (r < 0) { throw new System.IO.IOException("premature.end.of.file"); } p += r; len -= r; } byte[] iccp = FilterUtil.FlateDecode(icccom, true); icccom = null; try { png.iccProfile = IccProfile.GetInstance(iccp); } catch (Exception) { png.iccProfile = null; } } else { if (IEND.Equals(marker)) { break; } else { StreamUtil.Skip(pngStream, len); } } } } } } } } } } StreamUtil.Skip(pngStream, 4); } }
/// <summary> /// Shows the profile description in specified text box. /// </summary> private void ShowProfileDescription(TextBox textBox, IccProfile profile) { textBox.Text = profile.ToString(); }