public static void tile(String orig, String dest, int factor) { if (orig.Equals(dest)) { throw new PngjException("input and output file cannot coincide"); } if (factor < 2 || factor > 100) { throw new PngjException("bad factor "); } PngReader pngr = FileHelper.CreatePngReader(orig); var x = pngr.ImgInfo; PngWriter pngw = FileHelper.CreatePngWriter(dest, pngr.ImgInfo, true); pngr.SetUnpackedMode(true); // we dont want to do the unpacking ourselves, we want a sample per array element pngw.SetUseUnPackedMode(true); // not really necesary here, as we pass the ImageLine, but anyway... pngw.CopyChunksFirst(pngr, ChunkCopyBehaviour.COPY_ALL_SAFE); for (int row = 0; row < pngr.ImgInfo.Rows; row++) { ImageLine l1 = pngr.ReadRowInt(row); mirrorLineInt(pngr.ImgInfo, l1.Scanline); pngw.WriteRow(l1, row); } pngw.CopyChunksLast(pngr, ChunkCopyBehaviour.COPY_ALL_SAFE); pngw.End(); }
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); }
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); }
/// <summary> /// Decodes a raw file buffer of PNG data into raw image buffer, with width and height saved. /// </summary> /// <param name="stream">Png bytes.</param> public ImageLines Deserialize(Stream stream) { if (stream is null) { throw new System.ArgumentNullException(nameof(stream)); } using var png = new PngReader(stream); png.SetUnpackedMode(true); var image = png.ReadRowsByte(); png.End(); return(image); }
public static Unity3DTileIndex LoadFromPNG(Stream stream) { #if IMAGE_SHARP_PNG //requires extra dependency DLLs which bloat the webgl build using (var png = SixLabors.ImageSharp.Image.Load <Rgba64>(stream)) var index = new Unity3DTileIndex(png.Width, png.Height); { for (int r = 0; r < png.Height; r++) { for (int c = 0; c < png.Width; c++) { var pixel = png[c, r]; index[0, r, c] = pixel.R; index[1, r, c] = pixel.G; index[2, r, c] = pixel.B; } } } return(index); #elif PNGCS_PNG var png = new PngReader(stream); png.SetUnpackedMode(true); var info = png.ImgInfo; if (info.Channels != 3) { throw new Exception("expected 3 channel PNG, got " + info.Channels); } var index = new Unity3DTileIndex(info.Cols, info.Rows); var buf = new int[3 * info.Cols]; for (int r = 0; r < info.Rows; r++) { png.ReadRow(buf, r); for (int c = 0; c < info.Cols; c++) { index[0, r, c] = (uint)buf[3 * c + 0]; index[1, r, c] = (uint)buf[3 * c + 1]; index[2, r, c] = (uint)buf[3 * c + 2]; } } png.End(); return(index); #else return(null); #endif }