public Compressor(byte[] image) { buf1 = new MemoryStream(); dos1 = new DeflaterOutputStream(buf1); dos1.Write(image, 0, image.Length); dos1.Finish(); }
internal void addAllPages(int pagesObjNumber, int resObjNumber) { for (int i = 0; i < pages.Count; i++) { Page page = pages[i]; // Page object newobj(); Append("<<\n"); Append("/Type /Page\n"); Append("/Parent "); Append(pagesObjNumber); Append(" 0 R\n"); Append("/MediaBox [0.0 0.0 "); Append(page.width); Append(" "); Append(page.height); Append("]\n"); Append("/Resources "); Append(resObjNumber); Append(" 0 R\n"); Append("/Contents "); Append(objNumber + 1); Append(" 0 R\n"); if (page.annots.Count > 0) { Append("/Annots [ "); for (int j = 0; j < page.annots.Count; j++) { Append(objNumber + 2 + j); Append(" 0 R "); } Append("]\n"); } Append(">>\n"); endobj(); MemoryStream baos = new MemoryStream(); DeflaterOutputStream dos = new DeflaterOutputStream(baos, new Deflater()); dos.Write(page.buf.ToArray(), 0, page.buf.ToArray().Length); dos.Finish(); // Page contents newobj(); Append("<<\n"); Append("/Filter /FlateDecode\n"); Append("/Length "); Append(baos.Length); Append("\n"); Append(">>\n"); Append("stream\n"); Append(baos); Append("\nendstream\n"); endobj(); addAnnotDictionaries(page); } }
internal static byte[] deflate(byte[] data) { MemoryStream buf = new MemoryStream(); DeflaterOutputStream dos = new DeflaterOutputStream(buf); dos.Write(data, 0, data.Length); return(buf.ToArray()); }
public EmbeddedFile(PDF pdf, String fileName, Stream stream, bool compress) { this.fileName = fileName; MemoryStream baos = new MemoryStream(); byte[] buf = new byte[2048]; int number; while ((number = stream.Read(buf, 0, buf.Length)) > 0) { baos.Write(buf, 0, number); } stream.Dispose(); if (compress) { buf = baos.ToArray(); baos = new MemoryStream(); DeflaterOutputStream dos = new DeflaterOutputStream(baos); dos.Write(buf, 0, buf.Length); dos.Finish(); } pdf.Newobj(); pdf.Append("<<\n"); pdf.Append("/Type /EmbeddedFile\n"); if (compress) { pdf.Append("/Filter /FlateDecode\n"); } pdf.Append("/Length "); pdf.Append(baos.Length); pdf.Append("\n"); pdf.Append(">>\n"); pdf.Append("stream\n"); pdf.Append(baos); pdf.Append("\nendstream\n"); pdf.Endobj(); pdf.Newobj(); pdf.Append("<<\n"); pdf.Append("/Type /Filespec\n"); pdf.Append("/F ("); pdf.Append(fileName); pdf.Append(")\n"); pdf.Append("/EF <</F "); pdf.Append(pdf.objNumber - 1); pdf.Append(" 0 R>>\n"); pdf.Append(">>\n"); pdf.Endobj(); this.objNumber = pdf.objNumber; pdf.embeddedFiles.Add(this); }
internal int addOutputIntentObject() { MemoryStream baos = new MemoryStream(); BufferedStream bis = new BufferedStream( new FileStream("icc-profiles/sRGB_IEC61966-2-1_black_scaled.icc", FileMode.Open)); int ch; while ((ch = bis.ReadByte()) != -1) { baos.WriteByte((byte)ch); } bis.Close(); byte[] sRGB = baos.ToArray(); MemoryStream baos2 = new MemoryStream(); DeflaterOutputStream dos = new DeflaterOutputStream(baos2, new Deflater()); dos.Write(sRGB, 0, sRGB.Length); dos.Finish(); newobj(); Append("<<\n"); Append("/N 3\n"); Append("/Length "); Append(baos2.Length); Append("\n"); Append("/Filter /FlateDecode\n"); Append(">>\n"); Append("stream\n"); Append(baos2); Append("\nendstream\n"); endobj(); // OutputIntent object newobj(); Append("<<\n"); Append("/Type /OutputIntent\n"); Append("/S /GTS_PDFA1\n"); Append("/OutputCondition (sRGB IEC61966-2.1)\n"); Append("/OutputConditionIdentifier (sRGB IEC61966-2.1)\n"); Append("/Info (sRGB IEC61966-2.1)\n"); Append("/DestOutputProfile "); Append(objNumber - 1); Append(" 0 R\n"); Append(">>\n"); endobj(); return(objNumber); }
public Compressor(byte[] image) { buf1 = new MemoryStream(); if (PDF.original_zlib) { ods1 = new OptionalDeflateStream(buf1); ods1.Write(image, 0, image.Length); ods1.Close(); } else { dos1 = new DeflaterOutputStream(buf1, new Deflater()); dos1.Write(image, 0, image.Length); dos1.Finish(); } }
internal void AddPageContent(Page page) { MemoryStream memoryStream = new MemoryStream(); DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(memoryStream); byte[] array = page.buf.ToArray(); deflaterOutputStream.Write(array, 0, array.Length); deflaterOutputStream.Finish(); page.buf = null; this.Newobj(); this.Append("<<\n"); this.Append("/Filter /FlateDecode\n"); this.Append("/Length "); this.Append((double)memoryStream.Length); this.Append("\n"); this.Append(">>\n"); this.Append("stream\n"); this.Append(memoryStream); this.Append("\nendstream\n"); this.Endobj(); this.contents.Add(this.objNumber); }
internal void AddPageContent(Page page) { MemoryStream baos = new MemoryStream(); DeflaterOutputStream dos = new DeflaterOutputStream(baos); byte[] buf = page.buf.ToArray(); dos.Write(buf, 0, buf.Length); dos.Finish(); page.buf = null; // Release the page content memory! Newobj(); Append("<<\n"); Append("/Filter /FlateDecode\n"); Append("/Length "); Append(baos.Length); Append("\n"); Append(">>\n"); Append("stream\n"); Append(baos); Append("\nendstream\n"); Endobj(); contents.Add(objNumber); }
public OTF(Stream stream) { this.stream = stream; this.baos = new MemoryStream(); byte[] buffer = new byte[0x10000]; int count; while ((count = stream.Read(buffer, 0, buffer.Length)) > 0) { baos.Write(buffer, 0, count); } stream.Dispose(); buf = baos.ToArray(); baos = new MemoryStream(); dos = new DeflaterOutputStream(baos); // Extract OTF metadata long version = ReadUInt32(); if (version == 0x00010000L || // Win OTF version == 0x74727565L || // Mac TTF version == 0x4F54544FL) // CFF OTF // We should be able to read this font { } else { throw new Exception( "OTF version == " + version + " is not supported."); } int numOfTables = ReadUInt16(); int searchRange = ReadUInt16(); int entrySelector = ReadUInt16(); int rangeShift = ReadUInt16(); FontTable cmapTable = null; for (int i = 0; i < numOfTables; i++) { char[] name = new char[4]; for (int j = 0; j < 4; j++) { name[j] = (char)ReadByte(); } FontTable table = new FontTable(); table.name = new String(name); table.checkSum = ReadUInt32(); table.offset = (int)ReadUInt32(); table.length = (int)ReadUInt32(); int k = index; // Save the current index if (table.name.Equals("head")) { Head(table); } else if (table.name.Equals("hhea")) { Hhea(table); } else if (table.name.Equals("OS/2")) { OS_2(table); } else if (table.name.Equals("name")) { Name(table); } else if (table.name.Equals("hmtx")) { Hmtx(table); } else if (table.name.Equals("post")) { Post(table); } else if (table.name.Equals("CFF ")) { CFF_(table); } else if (table.name.Equals("GSUB")) { GSUB(table); } else if (table.name.Equals("cmap")) { cmapTable = table; } index = k; // Restore the index } // This table must be processed last Cmap(cmapTable); if (cff) { dos.Write(buf, cff_off, cff_len); } else { dos.Write(buf, 0, buf.Length); } dos.Finish(); }
private void parseData(Stream stream) { this.image = new byte[this.w * this.h * 3]; int length = 4 * (int)Math.Ceiling((double)(this.bpp * this.w) / 32.0); try { int i = 0; while (i < this.h) { byte[] array = this.getBytes(stream, length); int num = this.bpp; if (num <= 8) { if (num != 1) { if (num != 4) { if (num != 8) { goto IL_D0; } } else { array = BMPImage.bit4to8(array, this.w); } } else { array = BMPImage.bit1to8(array, this.w); } } else { if (num != 16) { if (num != 24) { if (num != 32) { goto IL_D0; } array = BMPImage.bit32to24(array, this.w); } } else { if (this.r5g6b5) { array = BMPImage.bit16to24(array, this.w); } else { array = BMPImage.bit16to24b(array, this.w); } } } int num2 = this.w * (this.h - i - 1) * 3; if (this.palette != null) { for (int j = 0; j < this.w; j++) { this.image[num2++] = this.palette[(array[j] < 0) ? ((int)array[j] + 256) : ((int)array[j])][2]; this.image[num2++] = this.palette[(array[j] < 0) ? ((int)array[j] + 256) : ((int)array[j])][1]; this.image[num2++] = this.palette[(array[j] < 0) ? ((int)array[j] + 256) : ((int)array[j])][0]; } } else { for (int k = 0; k < this.w * 3; k += 3) { this.image[num2++] = array[k + 2]; this.image[num2++] = array[k + 1]; this.image[num2++] = array[k]; } } i++; continue; IL_D0: throw new Exception("Can only parse 1 bit, 4bit, 8bit, 16bit, 24bit and 32bit images"); } } catch (Exception ex) { throw new Exception(ex.ToString() + " : BMP parse error: imagedata not correct"); } MemoryStream memoryStream = new MemoryStream(32768); DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(memoryStream); deflaterOutputStream.Write(this.image, 0, this.image.Length); deflaterOutputStream.Finish(); this.deflated = memoryStream.ToArray(); }
private void parseData(System.IO.Stream stream) { // rowsize is 4 * ceil (bpp*width/32.0) image = new byte[w * h * 3]; int rowsize = 4 * (int)Math.Ceiling(bpp * w / 32.0);// 4 byte alignment // hiv hver r�kke ud: byte[] row; int index; try { for (int i = 0; i < h; i++) { row = getBytes(stream, rowsize); switch (bpp) { case 1: row = bit1to8(row, w); break; // opslag i palette case 4: row = bit4to8(row, w); break; // opslag i palette case 8: break; // opslag i palette case 16: if (r5g6b5) { row = bit16to24(row, w); // 5,6,5 bit } else { row = bit16to24b(row, w); } break; case 24: break; // bytes are correct case 32: row = bit32to24(row, w); break; default: throw new Exception( "Can only parse 1 bit, 4bit, 8bit, 16bit, 24bit and 32bit images"); } index = w * (h - i - 1) * 3; if (palette != null) // indexed { for (int j = 0; j < w; j++) { image[index++] = palette[(row[j] < 0)?row[j] + 256:row[j]][2]; image[index++] = palette[(row[j] < 0)?row[j] + 256:row[j]][1]; image[index++] = palette[(row[j] < 0)?row[j] + 256:row[j]][0]; } } else // not indexed { for (int j = 0; j < w * 3; j += 3) { image[index++] = row[j + 2]; image[index++] = row[j + 1]; image[index++] = row[j]; } } } } catch (Exception e) { throw new Exception(e.ToString() + " : BMP parse error: imagedata not correct"); } MemoryStream data2 = new MemoryStream(32768); DeflaterOutputStream dos = new DeflaterOutputStream(data2); dos.Write(image, 0, image.Length); dos.Finish(); deflated = data2.ToArray(); }
private void AddPageContent(Page page) { MemoryStream baos = new MemoryStream(); DeflaterOutputStream dos = new DeflaterOutputStream(baos); byte[] buf = page.buf.ToArray(); dos.Write(buf, 0, buf.Length); dos.Finish(); page.buf = null; // Release the page content memory! Newobj(); Append("<<\n"); Append("/Filter /FlateDecode\n"); Append("/Length "); Append(baos.Length); Append("\n"); Append(">>\n"); Append("stream\n"); Append(baos); Append("\nendstream\n"); Endobj(); page.contents.Add(objNumber); }
public OTF(Stream stream) { this.stream = stream; this.baos = new MemoryStream(); byte[] buffer = new byte[0x10000]; int count; while ((count = stream.Read(buffer, 0, buffer.Length)) > 0) { baos.Write(buffer, 0, count); } stream.Dispose(); buf = baos.ToArray(); baos = new MemoryStream(); dos = new DeflaterOutputStream(baos); // Extract OTF metadata long version = ReadUInt32(); if (version == 0x00010000L || // Win OTF version == 0x74727565L || // Mac TTF version == 0x4F54544FL) { // CFF OTF // We should be able to read this font } else { throw new Exception( "OTF version == " + version + " is not supported."); } int numOfTables = ReadUInt16(); int searchRange = ReadUInt16(); int entrySelector = ReadUInt16(); int rangeShift = ReadUInt16(); FontTable cmapTable = null; for (int i = 0; i < numOfTables; i++) { char[] name = new char[4]; for (int j = 0; j < 4; j++) { name[j] = (char) ReadByte(); } FontTable table = new FontTable(); table.name = new String(name); table.checkSum = ReadUInt32(); table.offset = (int) ReadUInt32(); table.length = (int) ReadUInt32(); int k = index; // Save the current index if (table.name.Equals("head")) { Head(table); } else if (table.name.Equals("hhea")) { Hhea(table); } else if (table.name.Equals("OS/2")) { OS_2(table); } else if (table.name.Equals("name")) { Name(table); } else if (table.name.Equals("hmtx")) { Hmtx(table); } else if (table.name.Equals("post")) { Post(table); } else if (table.name.Equals("CFF ")) { CFF_(table); } else if (table.name.Equals("GSUB")) { GSUB(table); } else if (table.name.Equals("cmap")) { cmapTable = table; } index = k; // Restore the index } // This table must be processed last Cmap(cmapTable); if (cff) { dos.Write(buf, cff_off, cff_len); } else { dos.Write(buf, 0, buf.Length); } dos.Finish(); }
internal int addOutputIntentObject() { MemoryStream baos = new MemoryStream(); BufferedStream bis = new BufferedStream( new FileStream("icc-profiles/sRGB_IEC61966-2-1_black_scaled.icc", FileMode.Open)); int ch; while ((ch = bis.ReadByte()) != -1) { baos.WriteByte((byte) ch); } bis.Close(); byte[] sRGB = baos.ToArray(); MemoryStream baos2 = new MemoryStream(); DeflaterOutputStream dos = new DeflaterOutputStream(baos2, new Deflater()); dos.Write(sRGB, 0, sRGB.Length); dos.Finish(); newobj(); Append("<<\n"); Append("/N 3\n"); Append("/Length "); Append(baos2.Length); Append("\n"); Append("/Filter /FlateDecode\n"); Append(">>\n"); Append("stream\n"); Append(baos2); Append("\nendstream\n"); endobj(); // OutputIntent object newobj(); Append("<<\n"); Append("/Type /OutputIntent\n"); Append("/S /GTS_PDFA1\n"); Append("/OutputCondition (sRGB IEC61966-2.1)\n"); Append("/OutputConditionIdentifier (sRGB IEC61966-2.1)\n"); Append("/Info (sRGB IEC61966-2.1)\n"); Append("/DestOutputProfile "); Append(objNumber - 1); Append(" 0 R\n"); Append(">>\n"); endobj(); return objNumber; }
private void parseData(System.IO.Stream stream) { // rowsize is 4 * ceil (bpp*width/32.0) image = new byte[w * h * 3]; int rowsize = 4 * (int)Math.Ceiling(bpp*w/32.0);// 4 byte alignment // hiv hver r�kke ud: byte[] row; int index; try { for (int i = 0; i < h; i++) { row = getBytes(stream, rowsize); switch (bpp) { case 1: row = bit1to8(row, w); break; // opslag i palette case 4: row = bit4to8(row, w); break; // opslag i palette case 8: break; // opslag i palette case 16: if(r5g6b5) row = bit16to24(row, w); // 5,6,5 bit else row = bit16to24b(row, w); break; case 24: break; // bytes are correct case 32: row = bit32to24(row, w); break; default: throw new Exception( "Can only parse 1 bit, 4bit, 8bit, 16bit, 24bit and 32bit images"); } index = w*(h-i-1)*3; if (palette != null) { // indexed for (int j = 0; j < w; j++) { image[index++] = palette[(row[j]<0)?row[j]+256:row[j]][2]; image[index++] = palette[(row[j]<0)?row[j]+256:row[j]][1]; image[index++] = palette[(row[j]<0)?row[j]+256:row[j]][0]; } } else { // not indexed for (int j = 0; j < w*3; j+=3) { image[index++] = row[j+2]; image[index++] = row[j+1]; image[index++] = row[j]; } } } } catch (Exception e) { throw new Exception(e.ToString() + " : BMP parse error: imagedata not correct"); } MemoryStream data2 = new MemoryStream(32768); DeflaterOutputStream dos = new DeflaterOutputStream(data2); dos.Write(image, 0, image.Length); dos.Finish(); deflated = data2.ToArray(); }