public override void CloneDataFromRead(PngChunk other) { PngChunkPLTE otherx = (PngChunkPLTE)other; this.SetNentries(otherx.GetNentries()); System.Array.Copy(otherx.entries, 0, entries, 0, numEntries); }
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)); }
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); }