public static void testEqual(String image1, String image2) { PngReader png1 = FileHelper.CreatePngReader(image1); PngHelperInternal.InitCrcForTests(png1); PngReader png2 = FileHelper.CreatePngReader(image2); PngHelperInternal.InitCrcForTests(png2); if (png1.IsInterlaced() != png2.IsInterlaced()) { fatalError("Cannot compare, one is interlaced, the other not:" + png1 + " " + png2, png1, png2); } if (!png1.ImgInfo.Equals(png2.ImgInfo)) { fatalError("Image are of different type", png1, png2); } png1.ReadRow(png1.ImgInfo.Rows - 1); png2.ReadRow(png2.ImgInfo.Rows - 1); png1.End(); png2.End(); long crc1 = PngHelperInternal.GetCrctestVal(png1); long crc2 = PngHelperInternal.GetCrctestVal(png2); if (crc1 != crc2) { fatalError("different crcs " + image1 + "=" + crc1 + " " + image2 + "=" + crc2, png1, png2); } }
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 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(); }
private void btnSave_Click(object sender, RoutedEventArgs e) { CommonSaveFileDialog saveDialog = new CommonSaveFileDialog(); saveDialog.ShowPlacesList = true; saveDialog.AddToMostRecentlyUsedList = true; saveDialog.Filters.Add(new CommonFileDialogFilter("PNG images", "*.png")); saveDialog.DefaultFileName = DateTime.Now.ToString("yyyyMMddhhmmss") + ".png"; if (saveDialog.ShowDialog(this) == CommonFileDialogResult.Ok) { using (FileStream newFileStream = new FileStream(saveDialog.FileName, FileMode.OpenOrCreate, FileAccess.ReadWrite), oldFileStream = new FileStream(soureFilePath, FileMode.Open, FileAccess.Read)) { string semanticInfo = @" sky: blue sky. heart: cloud heart. Meaning: It means love|爱|❤. "; ////// Reference: http://code.google.com/p/pngcs/wiki/Overview // get decoder pngReader = new PngReader(oldFileStream); // create encoder pngWriter = new PngWriter(newFileStream, pngReader.ImgInfo); // copy int chunkBehav = Hjg.Pngcs.Chunks.ChunkCopyBehaviour.COPY_ALL; // tell to copy all 'safe' chunks pngWriter.CopyChunksFirst(pngReader, chunkBehav); // copy some metadata from reader int channels = pngReader.ImgInfo.Channels; if (channels < 3) { throw new Exception("This example works only with RGB/RGBA images"); } for (int row = 0; row < pngReader.ImgInfo.Rows; row++) { ImageLine l1 = pngReader.ReadRow(row); // format: RGBRGB... or RGBARGBA... pngWriter.WriteRow(l1, row); } pngWriter.CopyChunksLast(pngReader, chunkBehav); // metadata after the image pixels? can happen // app info pngWriter.GetMetadata().SetText(Hjg.Pngcs.Chunks.PngChunkTextVar.KEY_Software, "Semantic Image"); // semantic info Hjg.Pngcs.Chunks.PngChunk chunk = pngWriter.GetMetadata().SetText(Key_SemanticInfo, semanticInfo, false, false); chunk.Priority = true; pngWriter.End(); // dont forget this pngReader.End(); oldFileStream.Close(); newFileStream.Close(); } } }
public static void testCrcEquals(string image1, long crc) { PngReader png1 = FileHelper.CreatePngReader(image1); PngHelperInternal.InitCrcForTests(png1); png1.ReadRow(png1.ImgInfo.Rows - 1); png1.End(); long crc1 = PngHelperInternal.GetCrctestVal(png1); if (crc1 != crc) { fatalError("different crcs", png1); } }
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 }
/// <summary>If <paramref name="outFilePath" /> is NULL then file is written in-place.</summary> //public static void WriteZTxtChunk(string inFilePath, string outFilePath, string text) //{ // PngChunk CreateChunk(ImageInfo imgInfo) // { // PngChunkZTXT chunk = new PngChunkZTXT(imgInfo); // chunk.SetKeyVal(data); // chunk.Priority = true; // return chunk; // } // using (var inStream = OpenInputStream(inFilePath, outFilePath == null)) // WriteChunk(inStream, outFilePath ?? inFilePath, CreateChunk); //} private static void WriteChunk(Stream inStream, string outFilePath, Func <ImageInfo, PngChunk> chunkFunc) { PngReader pngr = new PngReader(inStream); PngWriter pngw = FileHelper.CreatePngWriter(outFilePath, pngr.ImgInfo, true); pngw.CopyChunksFirst(pngr, ChunkCopyBehaviour.COPY_ALL); pngw.GetChunksList().Queue(chunkFunc(pngw.ImgInfo)); 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(); }
public static void SaveToPng(string FileName, string ToFileName, string XamlFileName) { PngReader pngr = FileHelper.CreatePngReader(FileName); PngWriter pngw = FileHelper.CreatePngWriter(ToFileName, pngr.ImgInfo, true); pngw.CopyChunksFirst(pngr, ChunkCopyBehaviour.COPY_ALL_SAFE); PngChunkSKIN mychunk = new PngChunkSKIN(pngw.ImgInfo); mychunk.Content = File.ReadAllText(XamlFileName); 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(); }
private void WriteSvgToChunk(string tmpOcclusionFilePath, string svg) { using (Stream inStream = ToMemoryStream(tmpOcclusionFilePath)) { PngReader pngr = new PngReader(inStream); PngWriter pngw = FileHelper.CreatePngWriter(tmpOcclusionFilePath, pngr.ImgInfo, true); pngw.CopyChunksFirst(pngr, ChunkCopyBehaviour.COPY_ALL_SAFE); CreateChunk(pngw, svg); 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(); } }
private bool _autoCrop(string filename) { PngReader pngr = FileHelper.CreatePngReader(filename); int nrr = pngr.ImgInfo.Rows; int nc = pngr.ImgInfo.Cols; int firstNonBlank = -1; int lastNonBlank = -1; for (int i = 0; i < nrr; i++) { ImageLine iLine = pngr.ReadRow(i); // is it a blank? Boolean isBlank = true; for (int j = 0; j < nc; j++) { if (iLine.Scanline[j] != 255) { isBlank = false; break; } } if (!isBlank) { if (firstNonBlank == -1) { firstNonBlank = i; } lastNonBlank = i; } } int minBdr = 20; int row0 = firstNonBlank - minBdr; if (row0 < 0) { row0 = 0; } int row1 = lastNonBlank + minBdr; if (row1 >= nrr) { row1 = nrr - 1; } pngr.End(); if (row0 == 0 && row1 == (nrr - 1)) { return(false); } // start over string tmp = Path.Combine(Path.GetDirectoryName(filename), Path.GetFileNameWithoutExtension(filename) + "_cropped.png"); pngr = FileHelper.CreatePngReader(filename); int h = row1 - row0 + 1; ImageInfo ii = new ImageInfo(nc, h, pngr.ImgInfo.BitDepth, pngr.ImgInfo.Alpha, false, false); PngWriter pngw = FileHelper.CreatePngWriter(tmp, ii, true); int chunkBehav = ChunkCopyBehaviour.COPY_ALL_SAFE; // tell to copy all 'safe' chunks pngw.CopyChunksFirst(pngr, chunkBehav); // copy some metadata from reader int ic = 0; for (int i = row0; i <= row1; i++) { ImageLine iLine = pngr.ReadRow(i); pngw.WriteRow(iLine, ic++); } pngr.End(); pngw.CopyChunksLast(pngr, chunkBehav); // metadata after the image pixels? can happen pngw.End(); // do _moveFile(tmp, filename); return(true); }
/// <summary> Reads samples using given reader </summary> static void ReadSamples(PngReader reader, ReadColorsResult results) { var info = reader.ImgInfo; int channels = info.Channels; int bitDepth = info.BitDepth; int numCols = results.width; int numRows = results.height; Color[] pixels = results.pixels; float max = GetBitDepthMaxValue(bitDepth); if (!info.Indexed) { if (!info.Packed) { for (int row = 0; row < numRows; row++) { ImageLine imageLine = reader.ReadRow(row); ProcessNByteRow(imageLine, pixels); } } else { if (bitDepth == 4) { for (int row = 0; row < numRows; row++) { ImageLine imageLine = reader.ReadRowByte(row); Process4BitRow(imageLine, pixels); } } else if (bitDepth == 2) { throw new System.Exception($"bit depth {bitDepth} for {channels} channels not implemented\n"); } else if (bitDepth == 1) { for (int row = 0; row < numRows; row++) { ImageLine imageLine = reader.ReadRowByte(row); Process1BitRow(imageLine, pixels); } } else { throw new System.Exception($"bit depth {bitDepth} for {channels} channels not implemented\n"); } } } else { var plte = reader.GetMetadata().GetPLTE(); if (bitDepth == 8) { if (info.Alpha) { var trns = reader.GetMetadata().GetTRNS(); // transparency metadata, can be null for (int row = 0; row < numRows; row++) { ImageLine imageLine = reader.ReadRow(row); Process8BitIndexedRow(imageLine, plte, trns, pixels); } } else { for (int row = 0; row < numRows; row++) { ImageLine imageLine = reader.ReadRow(row); Process8BitIndexedRow(imageLine, plte, pixels); } } } else if (bitDepth == 4) { for (int row = 0; row < numRows; row++) { ImageLine imageLine = reader.ReadRow(row); Process4BitIndexedRow(imageLine, plte, pixels); } } else { throw new System.Exception($"bit depth {bitDepth} for {channels} channels not implemented\n"); } } }