public static string createWaves(String suffix, double scale, ImageInfo imi) { string f = getTmpFile(suffix); // open image for writing to a output stream PngWriter png = FileHelper.CreatePngWriter(f, imi, true); png.GetMetadata().SetText("key1", "val1"); ImageLine iline = new ImageLine(imi, ImageLine.ESampleType.BYTE, true); for (int row = 0; row < png.ImgInfo.Rows; row++) { for (int x = 0; x < imi.Cols; x++) { int r = (int)((Math.Sin((row + x) * 0.073 * scale) + 1) * 128); int g = (int)((Math.Sin((row + x * 0.22) * 0.08 * scale) + 1) * 128); int b = (int)((Math.Sin((row * 0.52 - x * 0.2) * 0.21 * scale) + 1) * 128); iline.ScanlineB[x * imi.Channels] = (byte)r; iline.ScanlineB[x * imi.Channels + 1] = (byte)g; iline.ScanlineB[x * imi.Channels + 2] = (byte)b; if (imi.Channels == 4) { iline.ScanlineB[x * imi.Channels + 3] = (byte)((b + g) / 2); } } png.WriteRow(iline, row); } png.End(); return(f); }
private static void additionalTestInterlaced(string orig, string origni) { // tests also read/write in packed format PngReader pngr = FileHelper.CreatePngReader(orig); string copy = TestsHelper.addSuffixToName(orig, "_icopy"); pngr.SetUnpackedMode(false); PngWriter pngw = FileHelper.CreatePngWriter(copy, pngr.ImgInfo, true); pngw.CopyChunksFirst(pngr, ChunkCopyBehaviour.COPY_ALL); pngw.SetUseUnPackedMode(false); Random random = new Random(); bool useByte = random.NextDouble() > 0.5 && pngr.ImgInfo.BitDepth < 16; for (int row = 0; row < pngr.ImgInfo.Rows; row++) { if (useByte) { ImageLine line = pngr.ReadRowByte(row); pngw.WriteRow(line, row); } else { ImageLine line = pngr.ReadRowInt(row); pngw.WriteRow(line, row); } } pngr.End(); pngw.End(); TestsHelper.testEqual(copy, origni); System.IO.File.Delete(copy); }
public static void Create(string filename, int cols, int rows) { ImageInfo imi = new ImageInfo(cols, rows, 8, false); // 8 bits per channel, no alpha // open image for writing PngWriter png = FileHelper.CreatePngWriter(filename, imi, true); // add some optional metadata (chunks) png.GetMetadata().SetDpi(100.0); png.GetMetadata().SetTimeNow(0); // 0 seconds fron now = now png.GetMetadata().SetText(PngChunkTextVar.KEY_Title, "Just a text image"); PngChunk chunk = png.GetMetadata().SetText("my key", "my text .. bla bla"); chunk.Priority = true; // this chunk will be written as soon as possible ImageLine iline = new ImageLine(imi); for (int col = 0; col < imi.Cols; col++) // this line will be written to all rows { int r = 255; int g = 127; int b = 255 * col / imi.Cols; ImageLineHelper.SetPixel(iline, col, r, g, b); // orange-ish gradient } for (int row = 0; row < png.ImgInfo.Rows; row++) { png.WriteRow(iline, row); } png.End(); }
public static void DecreaseRed(String origFilename, String destFilename) { if (origFilename.Equals(destFilename)) { throw new PngjException("input and output file cannot coincide"); } PngReader pngr = FileHelper.CreatePngReader(origFilename); PngWriter pngw = FileHelper.CreatePngWriter(destFilename, pngr.ImgInfo, true); Console.WriteLine(pngr.ToString()); int chunkBehav = ChunkCopyBehaviour.COPY_ALL_SAFE; // copy all 'safe' chunks // this can copy some metadata from reader pngw.CopyChunksFirst(pngr, chunkBehav); int channels = pngr.ImgInfo.Channels; if (channels < 3) { throw new Exception("This method is for RGB/RGBA images"); } for (int row = 0; row < pngr.ImgInfo.Rows; row++) { ImageLine l1 = pngr.ReadRow(row); for (int j = 0; j < pngr.ImgInfo.Cols; j++) { l1.Scanline[j * channels] /= 2; } pngw.WriteRow(l1, row); } // just in case some new metadata has been read after the image pngw.CopyChunksLast(pngr, chunkBehav); pngw.End(); }
public static void testWrite(string src, string target) { // for writing is not necesary to register DummyClass c = new DummyClass(); c.name = "Hernán"; c.age = 45; PngReader pngr = FileHelper.CreatePngReader(src); PngWriter pngw = FileHelper.CreatePngWriter(target, pngr.ImgInfo, true); pngw.CopyChunksFirst(pngr, ChunkCopyBehaviour.COPY_ALL_SAFE); PngChunkSERI mychunk = new PngChunkSERI(pngw.ImgInfo); mychunk.SetObj(c); mychunk.Priority = true; // if we want it to be written as soon as possible pngw.GetChunksList().Queue(mychunk); for (int row = 0; row < pngr.ImgInfo.Rows; row++) { ImageLine l1 = pngr.ReadRow(row); pngw.WriteRow(l1, row); } pngw.CopyChunksLast(pngr, ChunkCopyBehaviour.COPY_ALL); pngr.End(); pngw.End(); Console.Out.WriteLine("Done. Writen : " + target); }
public static void doit(String orig) { string copy = TestsHelper.addSuffixToName(orig, "_tc"); PngReader pngr = FileHelper.CreatePngReader(orig); if (!pngr.ImgInfo.Indexed) { throw new Exception("Not indexed image"); } PngChunkPLTE plte = pngr.GetMetadata().GetPLTE(); PngChunkTRNS trns = pngr.GetMetadata().GetTRNS(); // transparency metadata, can be null bool alpha = trns != null; ImageInfo im2 = new ImageInfo(pngr.ImgInfo.Cols, pngr.ImgInfo.Rows, 8, alpha); PngWriter pngw = FileHelper.CreatePngWriter(copy, im2, true); pngw.CopyChunksFirst(pngr, ChunkCopyBehaviour.COPY_ALL_SAFE); int[] buf = null; for (int row = 0; row < pngr.ImgInfo.Rows; row++) { ImageLine line = pngr.ReadRowInt(row); buf = ImageLineHelper.Palette2rgb(line, plte, trns, buf); pngw.WriteRowInt(buf, row); } pngw.CopyChunksLast(pngr, ChunkCopyBehaviour.COPY_ALL_SAFE); pngr.End(); pngw.End(); Console.WriteLine("True color: " + copy); }
static void testmirror(string orig, string origni, string truecolor) { string mirror = TestsHelper.addSuffixToName(orig, "_mirror"); string recov = TestsHelper.addSuffixToName(orig, "_recov"); long crc0 = 0; bool interlaced; bool palete; { PngReader pngr = FileHelper.CreatePngReader(orig); palete = pngr.ImgInfo.Indexed; PngHelperInternal.InitCrcForTests(pngr); pngr.SetUnpackedMode(true); interlaced = pngr.IsInterlaced(); PngWriter pngw = FileHelper.CreatePngWriter(mirror, pngr.ImgInfo, true); pngw.SetFilterType(FilterType.FILTER_CYCLIC); // just to test all filters pngw.CopyChunksFirst(pngr, ChunkCopyBehaviour.COPY_ALL); pngw.SetUseUnPackedMode(true); for (int row = 0; row < pngr.ImgInfo.Rows; row++) { ImageLine line = pngr.ReadRowInt(row); mirrorLine(line); pngw.WriteRow(line, row); } pngr.End(); crc0 = PngHelperInternal.GetCrctestVal(pngr); pngw.CopyChunksFirst(pngr, ChunkCopyBehaviour.COPY_ALL); pngw.End(); } // mirror again, now with BYTE (if depth<16) and loading all rows { PngReader pngr2 = FileHelper.CreatePngReader(mirror); pngr2.SetUnpackedMode(true); PngWriter pngw = FileHelper.CreatePngWriter(recov, pngr2.ImgInfo, true); pngw.SetFilterType(FilterType.FILTER_AGGRESSIVE); pngw.CopyChunksFirst(pngr2, ChunkCopyBehaviour.COPY_ALL); pngw.SetUseUnPackedMode(true); ImageLines lines = pngr2.ImgInfo.BitDepth < 16 ? pngr2.ReadRowsByte() : pngr2 .ReadRowsInt(); for (int row = 0; row < pngr2.ImgInfo.Rows; row++) { ImageLine line = lines.GetImageLineAtMatrixRow(row); mirrorLine(line); pngw.WriteRow(line, row); } pngr2.End(); pngw.End(); } // now check if (orig[11] != 'i') { TestsHelper.testCrcEquals(recov, crc0); } //if (interlaced) // additionalTestInterlaced(orig, origni); //if (palete && System.IO.File.Exists(truecolor)) // additionalTestPalette(orig, truecolor); }
private static void additionalTestPalette(string orig, string truecolor) { // covnert to true color 8 bits and check equality PngReader pngr = FileHelper.CreatePngReader(orig); PngChunkPLTE plte = pngr.GetMetadata().GetPLTE(); PngChunkTRNS trns = pngr.GetMetadata().GetTRNS(); string copy = TestsHelper.addSuffixToName(orig, "_tccopy"); bool alpha = trns != null; ImageInfo im2 = new ImageInfo(pngr.ImgInfo.Cols, pngr.ImgInfo.Rows, 8, alpha); PngWriter pngw = FileHelper.CreatePngWriter(copy, im2, true); pngw.CopyChunksFirst(pngr, ChunkCopyBehaviour.COPY_ALL_SAFE); int[] buf = null; for (int row = 0; row < pngr.ImgInfo.Rows; row++) { ImageLine line = pngr.ReadRowInt(row); buf = ImageLineHelper.Palette2rgb(line, plte, trns, buf); pngw.WriteRowInt(buf, row); } pngr.End(); pngw.End(); TestsHelper.testEqual(copy, truecolor); System.IO.File.Delete(copy); }
/** * Creates a dummy temp png You should call endFileTmp after adding chunks, etc * */ public static PngWriter prepareFileTmp(String suffix, ImageInfo imi) { PngWriter png = FileHelper.CreatePngWriter(getTmpFile(suffix), imi, true); return(png); }