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); }
/// <summary> /// Given an indexed line with a palette, unpacks as a RGB array /// </summary> /// <param name="line">ImageLine as returned from PngReader</param> /// <param name="pal">Palette chunk</param> /// <param name="trns">TRNS chunk (optional)</param> /// <param name="buf">Preallocated array, optional</param> /// <returns>R G B (one byte per sample)</returns> public static int[] Palette2rgb(ImageLine line, PngChunkPLTE pal, PngChunkTRNS trns, int[] buf) { bool isalpha = trns != null; int channels = isalpha ? 4 : 3; int nsamples = line.ImgInfo.Cols * channels; if (buf == null || buf.Length < nsamples) { buf = new int[nsamples]; } if (!line.SamplesUnpacked) { line = line.unpackToNewImageLine(); } bool isbyte = line.SampleType == Hjg.Pngcs.ImageLine.ESampleType.BYTE; int nindexesWithAlpha = trns != null?trns.GetPalletteAlpha().Length : 0; for (int c = 0; c < line.ImgInfo.Cols; c++) { int index = isbyte ? (line.ScanlineB[c] & 0xFF) : line.Scanline[c]; pal.GetEntryRgb(index, buf, c * channels); if (isalpha) { int alpha = index < nindexesWithAlpha?trns.GetPalletteAlpha()[index] : 255; buf[c * channels + 3] = alpha; } } return(buf); }
public override void CloneDataFromRead(PngChunk other) { PngChunkTRNS otherx = (PngChunkTRNS)other; Gray = otherx.Gray; Rgb = otherx.Rgb; if (otherx.PaletteAlpha != null) { PaletteAlpha = new int[otherx.PaletteAlpha.Length]; System.Array.Copy(otherx.PaletteAlpha, 0, PaletteAlpha, 0, PaletteAlpha.Length); } }
private void CloneData(PngChunkTRNS other) { if (other is null) { throw new System.ArgumentNullException(nameof(other)); } gray = other.gray; red = other.red; green = other.green; blue = other.blue; if (other.paletteAlpha is object) { paletteAlpha = new int[other.paletteAlpha.Length]; System.Array.Copy(other.paletteAlpha, 0, paletteAlpha, 0, paletteAlpha.Length); } }
/// <summary> /// Given an indexed line with a palette, unpacks as a RGB array /// </summary> /// <param name="line">ImageLine as returned from PngReader</param> /// <param name="pal">Palette chunk</param> /// <param name="trns">TRNS chunk (optional)</param> /// <param name="buf">Preallocated array, optional</param> /// <returns>R G B (one byte per sample)</returns> public static int[] Palette2rgb(ImageLine line, PngChunkPLTE pal, PngChunkTRNS trns, int[] buf) { if (line is null) { throw new ArgumentNullException(nameof(line)); } if (pal is null) { throw new ArgumentNullException(nameof(pal)); } var isalpha = trns is object; var channels = isalpha ? 4 : 3; var nsamples = line.ImgInfo.Cols * channels; if (buf is null || buf.Length < nsamples) { buf = new int[nsamples]; } if (!line.SamplesUnpacked) { line = line.UnpackToNewImageLine(); } var isbyte = line.SampleType == Hjg.Pngcs.ImageLine.ESampleType.BYTE; var nindexesWithAlpha = trns?.GetPalletteAlpha().Length ?? 0; for (var c = 0; c < line.ImgInfo.Cols; c++) { var index = isbyte ? (line.ScanlineB[c] & 0xFF) : line.Scanline[c]; pal.GetEntryRgb(index, buf, c * channels); if (isalpha) { buf[(c * channels) + 3] = index < nindexesWithAlpha?trns.GetPalletteAlpha()[index] : 255; } } return(buf); }
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); }
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; } } } } }