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); }
public sprite_data load_sprite_png(string _filename, string _name, bool _apply_palette, bool _crop_image, int _palette_slot) { PngReader png_reader = FileHelper.CreatePngReader(_filename); try { if (!png_reader.ImgInfo.Indexed) { throw new Exception(_filename + "\n\nNot indexed image!"); } if (png_reader.IsInterlaced()) { throw new Exception(_filename + "\n\nOnly non interlaced .PNG images are supported!"); } // if( ( png_reader.ImgInfo.Cols & 0x07 ) != 0 || ( png_reader.ImgInfo.Rows & 0x07 ) != 0 ) // { // png_reader.End(); // throw new Exception( _filename + "\n\nThe image size must be a multiple of 8 !" ); // } // if( ( ( png_reader.ImgInfo.Cols >> 3 ) * ( png_reader.ImgInfo.Rows >> 3 ) ) > utils.CONST_CHR_BANK_MAX_SPRITES_CNT ) // { // png_reader.End(); // throw new Exception( _filename + "\n\nThe imported image contains more than " + utils.CONST_CHR_BANK_MAX_SPRITES_CNT + " CHRs!" ); // } #if DEF_NES if (png_reader.GetMetadata().GetPLTE().MinBitDepth() != 2) { throw new Exception(_filename + "\n\nThe PNG image must have a 4 colors palette!"); } #elif DEF_SMS || DEF_PCE int img_bit_depth = png_reader.GetMetadata().GetPLTE().MinBitDepth(); if (img_bit_depth != 4 && img_bit_depth != 2) { throw new Exception(_filename + "\n\nThe PNG image must have a 4 or 2 bpp color depth!"); } if (png_reader.GetMetadata().GetPLTE().GetNentries() > 16) { throw new Exception(_filename + "\n\nThe PNG image must have a 16 or 4 colors palette!"); } #else ... #endif sprite_params spr_params = m_CHR_data_storage.create(png_reader, _apply_palette, _crop_image, _palette_slot); sprite_data spr = new sprite_data(_name); spr.setup(spr_params); spr.update_dimensions(); png_reader.End(); return(spr); }
private void btnOpen_Click(object sender, RoutedEventArgs e) { CommonOpenFileDialog openDialog = new CommonOpenFileDialog(); openDialog.ShowPlacesList = true; openDialog.Multiselect = false; openDialog.IsFolderPicker = false; openDialog.AddToMostRecentlyUsedList = true; openDialog.Filters.Add(new CommonFileDialogFilter("PNG images", "*.png")); if (openDialog.ShowDialog(this) == CommonFileDialogResult.Ok) { soureFilePath = openDialog.FileName; // get comment meta using (FileStream fileStream = new FileStream(soureFilePath, FileMode.Open, FileAccess.Read)) { pngReader = new PngReader(fileStream); // 参考自Hjg.Pngcs的SampleCustomChunk项目 // get last line: this forces loading all chunks pngReader.ReadChunksOnly(); tblkComment.Text = pngReader.GetMetadata().GetTxtForKey(Key_SemanticInfo); pngReader.End(); fileStream.Close(); } image.BeginInit(); image.Source = new BitmapImage(new Uri(soureFilePath)); image.EndInit(); } }
private IEnumerable <Bitmap> ReadRowChunk(PngReader reader, int chunkIndex) { if (!reader.ImgInfo.Indexed) { throw new ArgumentException("Only indexed color PNGs are supported."); } var lines = new byte[32][]; var pal = reader.GetMetadata().GetPLTE(); for (var i = 0; i < 32; i++) { lines[i] = reader.ReadRowByte( new byte[reader.ImgInfo.Cols], (chunkIndex * 32) + i); } var bitmapsPerRow = reader.ImgInfo.Cols / 32; for (var i = 0; i < bitmapsPerRow; i++) { var bmp = ReadBitmap(i, lines, pal); this.tileCache.GetOrAddBitmap(bmp); yield return(bmp); } }
public string getMetadata(string file, string key) { PngReader pngr = FileHelper.CreatePngReader(file); //pngr.MaxTotalBytesRead = 1024 * 1024 * 1024L * 3; // 3Gb! //pngr.ReadSkippingAllRows(); string data = pngr.GetMetadata().GetTxtForKey(key); pngr.End(); return(data);; }
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); }
static public PalettizedImage LoadPNG(string _path, PalettizedImageConfig _config) { PngReader pngr = FileHelper.CreatePngReader(_path); if (!pngr.ImgInfo.Indexed) { Debug.LogException(new UnityException("Image wasn't indexed")); return(null); } PngChunkPLTE palette = pngr.GetMetadata().GetPLTE(); PalettizedImage image = new PalettizedImage(pngr.ImgInfo.Cols, pngr.ImgInfo.Rows, palette.GetNentries()); image.SetConfig(_config); image.m_fileName = System.IO.Path.GetFileNameWithoutExtension(_path); image.ReadPalette(palette); image.ReadImage(pngr); return(image); }
public sprite_params setup(PngReader _png_reader, bool _apply_palette, bool _crop_image, int _palette_slot) { sprite_params spr_params = new sprite_params(this); if (_png_reader == null) { return(spr_params); } int img_width = _png_reader.ImgInfo.Cols; int img_height = _png_reader.ImgInfo.Rows; List <byte[]> lines_arr = new List <byte[]>(img_height); int i; int j; int min_x = 0; int max_x = img_width - 1; int min_y = 0; int max_y = img_height - 1; byte[] pixels_line = null; PngChunkPLTE plte = _png_reader.GetMetadata().GetPLTE(); PngChunkTRNS trns = _png_reader.GetMetadata().GetTRNS(); ImageLine line = null; int alpha_ind = -1; int num_colors = plte.GetNentries(); // detect useful image borders { if (trns != null) { int size; alpha_ind = trns.GetPalletteAlpha().Length - 1; if (_crop_image) { min_x = img_width + 1; max_x = -1; min_y = img_height + 1; max_y = -1; } bool transp_line = false; for (i = 0; i < img_height; i++) { line = _png_reader.ReadRowByte(i); if (line.ImgInfo.Packed) { line = line.unpackToNewImageLine(); } pixels_line = new byte[img_width]; Array.Copy(line.GetScanlineByte(), pixels_line, img_width); lines_arr.Add(pixels_line); size = pixels_line.Length; transp_line = true; for (j = 0; j < size; j++) { // if pixel is not transparent if (_crop_image && (pixels_line[j] != alpha_ind)) { if (min_x > j) { min_x = j; } if (max_x < j) { max_x = j; } transp_line = false; } } // if line is not transparent if (_crop_image && !transp_line) { if (min_y > i) { min_y = i; } if (max_y < i) { max_y = i; } } } } // find nearest colors if (_apply_palette) { #if DEF_FIXED_LEN_PALETTE16_ARR palettes_array plt_arr = palettes_array.Instance; plt_arr.palette_index = _palette_slot; #endif for (i = 0; i < num_colors; i++) { #if DEF_FIXED_LEN_PALETTE16_ARR plt_arr.update_color(_palette_slot, i, utils.find_nearest_color_ind(plte.GetEntry((trns != null) ? (i + alpha_ind) % num_colors:i))); #else palette_group.Instance.get_palettes_arr()[i / utils.CONST_NUM_SMALL_PALETTES].get_color_inds()[i % utils.CONST_NUM_SMALL_PALETTES] = utils.find_nearest_color_ind(plte.GetEntry((trns != null) ? (i + alpha_ind) % num_colors:i)); #endif } #if DEF_FIXED_LEN_PALETTE16_ARR plt_arr.update_palette(); #else for (i = 0; i < utils.CONST_NUM_SMALL_PALETTES; i++) { palette_group.Instance.get_palettes_arr()[i].update(); } #endif #if DEF_NES palette_group.Instance.active_palette = 0; #endif } // fill the lines_arr if sprite has no transparency if (lines_arr.Count == 0) { for (i = 0; i < img_height; i++) { line = _png_reader.ReadRowByte(i); if (line.ImgInfo.Packed) { line = line.unpackToNewImageLine(); } pixels_line = new byte[img_width]; Array.Copy(line.GetScanlineByte(), pixels_line, img_width); lines_arr.Add(pixels_line); } } } return(cut_CHRs(spr_params, min_x, max_x, min_y, max_y, lines_arr, (trns != null), alpha_ind, num_colors, _crop_image, _palette_slot)); }
public IEnumerable <RGBA> EnRGBA() { PngChunkPLTE oplte = null; PngChunkTRNS otrns = null; // transparency metadata, can be null if (pngr.ImgInfo.Indexed) { oplte = pngr.GetMetadata().GetPLTE(); otrns = pngr.GetMetadata().GetTRNS(); } for (var row = 0; row < pngr.ImgInfo.Rows; row++) { var line = pngr.ReadRowInt(row); if (pngr.ImgInfo.Indexed) { var rgrgba = ImageLineHelper.Palette2rgb(line, oplte, otrns, null); for (var irgba = 0; irgba < rgrgba.Length;) { yield return(new RGBA { r = rgrgba[irgba++], g = rgrgba[irgba++], b = rgrgba[irgba++], a = (otrns != null ? rgrgba[irgba++] : 255), }); } } else { if (pngr.ImgInfo.Packed) { line = line.unpackToNewImageLine(); } for (var col = 0; col < pngr.ImgInfo.Cols; col++) { switch (pngr.ImgInfo.Channels) { case 1: yield return(new RGBA { r = Read8(col, line), g = Read8(col, line), b = Read8(col, line), }); break; case 2: yield return(new RGBA { r = Read8(col * 2, line), g = Read8(col * 2, line), b = Read8(col * 2, line), a = Read8(col * 2 + 1, line), }); break; case 3: yield return(new RGBA { r = Read8(col * 3, line), g = Read8(col * 3 + 1, line), b = Read8(col * 3 + 2, line), }); break; case 4: yield return(new RGBA { r = Read8(col * 4, line), g = Read8(col * 4 + 1, line), b = Read8(col * 4 + 2, line), a = Read8(col * 4 + 3, line), }); break; } } } } }
/// <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"); } } }