private void initNotAlphaIDAT(FileStream fs, string colorspace, int n) { if (Dictionary["ColorSpace"] == null) { Dictionary.AddItem("ColorSpace", new PDFName(colorspace)); } if (_interface == 1) { MemoryStream compressionStream = new MemoryStream(); compressionStream.Position = 0; writeIDAT(compressionStream, fs); MemoryStream decodeStream = FlateDecoder.Decode(compressionStream, null); if (_bitdepth == 16) { n *= 2; } byte[] pdfData = toPDFData(decodeStream, n); GetStream().Write(pdfData, 0, pdfData.Length); } else { writeIDAT(GetStream(), fs); PDFDictionary dict = new PDFDictionary(); dict.AddItem("Predictor", new PDFNumber(15)); dict.AddItem("Colors", new PDFNumber(n)); dict.AddItem("BitsPerComponent", new PDFNumber(_bitdepth)); dict.AddItem("Columns", new PDFNumber(_width)); Dictionary.AddItem("Filter", new PDFName("FlateDecode")); Dictionary.AddItem("DecodeParms", dict); } }
private void initAlphaIDAT(FileStream fs, string colorspace, int n) { if (Dictionary["ColorSpace"] == null) { Dictionary.AddItem("ColorSpace", new PDFName(colorspace)); } MemoryStream streamSMask = new MemoryStream(); PDFDictionaryStream dictSMask = new PDFDictionaryStream(new PDFDictionary(), streamSMask); initSMask(dictSMask); Dictionary.AddItem("SMask", dictSMask); MemoryStream compressionStream = new MemoryStream(); compressionStream.Position = 0; writeIDAT(compressionStream, fs); MemoryStream decodeStream = FlateDecoder.Decode(compressionStream, null); byte[] pdfData = toPDFData(decodeStream, (n + 1) * (_bitdepth / 8)); for (int i = 0; i < pdfData.Length; i += (n + 1) * (_bitdepth / 8)) { GetStream().Write(pdfData, i, n * (_bitdepth / 8)); byte b = pdfData[i + n * (_bitdepth / 8)]; if (_bitdepth == 16) { b = (byte)((((int)b << 8) | (int)pdfData[i + n * (_bitdepth / 8) + 1]) / 256); } streamSMask.WriteByte(b); } }
private void initialize3IDAT(FileStream fs, long positionTRNS) { if (positionTRNS != 0) { long oldPosition = fs.Position; fs.Position = positionTRNS; byte[] buf = new byte[4]; fs.Read(buf, 0, buf.Length); fs.Position += 4; byte[] data = new byte[toInt32(buf)]; fs.Read(data, 0, data.Length); fs.Position = oldPosition; byte max = 0; for (int i = 0; i < data.Length; ++i) { if (max < data[i]) { max = data[i]; } } PDFArray array = new PDFArray(); array.AddItem(new PDFNumber(max)); array.AddItem(new PDFNumber(max)); Dictionary.AddItem("Mask", array); } if (_interface == 1) { MemoryStream compressionStream = new MemoryStream(); compressionStream.Position = 0; writeIDAT(compressionStream, fs); MemoryStream decodeStream = FlateDecoder.Decode(compressionStream, null); byte[] pdfData = toPDFData(decodeStream, 1); GetStream().Write(pdfData, 0, pdfData.Length); } else { writeIDAT(GetStream(), fs); PDFDictionary dict = new PDFDictionary(); dict.AddItem("Predictor", new PDFNumber(15)); dict.AddItem("Colors", new PDFNumber(1)); dict.AddItem("BitsPerComponent", new PDFNumber(_bitdepth)); dict.AddItem("Columns", new PDFNumber(_width)); Dictionary.AddItem("Filter", new PDFName("FlateDecode")); Dictionary.AddItem("DecodeParms", dict); } }
public void Decode() { Filter[] filters = getFilters(); if (filters.Length == 0) { return; } _stream.Position = 0; try { for (int i = 0; i < filters.Length; ++i) { switch (filters[i]) { case Filter.ASCII85: _stream = ASCII85Decoder.Decode(_stream); break; case Filter.ASCIIHex: _stream = ASCIIHexDecoder.Decode(_stream); break; case Filter.RunLength: _stream = RunLengthDecoder.Decode(_stream); break; case Filter.JPX: _stream = JPXDecoder.Decode(_stream); break; case Filter.CCITTFax: _stream = CCITTFaxDecoder.Decode(_stream, getDecodeParameters(i)); break; case Filter.DCT: _stream = DCTDecoder.Decode(_stream, getDecodeParameters(i)); break; case Filter.Flate: _stream = FlateDecoder.Decode(_stream, getDecodeParameters(i)); break; case Filter.JBIG2: _stream = JBIG2Decoder.Decode(_stream, getDecodeParameters(i)); break; case Filter.LZW: _stream = LZWDecoder.Decode(_stream, getDecodeParameters(i)); break; } _stream.Position = 0; } } catch { return; } _dictionary.RemoveItem("Filter"); _dictionary.RemoveItem("DecodeParms"); }
public void Write(SaveParameters param) { _stream.Position = 0; MemoryStream output = _stream; //set compression bool filter = false; if (param.Compression == Compression.Flate) { PDFName subtype = (PDFName)_dictionary["Subtype"]; Filter[] filters = getFilters(); if (subtype != null && subtype.GetValue() == "Image" && filters.Length > 0 && filters[0] == Filter.DCT) { output = param.Buffer; output.SetLength(0); _stream.WriteTo(output); filter = true; } else if (param.Compression != Compression.None && getFilters().Length == 0) { output = param.Buffer; output.SetLength(0); FlateDecoder.Code(_stream, output); _dictionary.AddItem("Filter", new PDFName("FlateDecode")); filter = true; } } //set encryption if (param.Encryptor != null) { param.Encryptor.ResetObjectReference(param.ObjNo, param.GenNo, DataType.Stream); if (filter) { byte[] buffer = output.GetBuffer(); int length = (int)output.Length; output.SetLength(0); param.Encryptor.Encrypt(buffer, 0, length, output, DataType.Stream); } else { output = param.Buffer; output.SetLength(0); param.Encryptor.Encrypt(_stream.GetBuffer(), 0, (int)_stream.Length, output, DataType.Stream); } } output.Position = 0; _dictionary.AddItem("Length", new PDFNumber(output.Length)); _dictionary.Write(param); param.Stream.Write(StartStream, 0, StartStream.Length); param.Stream.Write(output.GetBuffer(), 0, (int)output.Length); param.Stream.Write(EndStream, 0, EndStream.Length); if (filter) { _dictionary.RemoveItem("Filter"); } }