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 static Image Load(Stream stream) { if (stream == null) { throw new ArgumentNullException("stream"); } PngReader pngReader = new PngReader(stream); pngReader.ShouldCloseStream = false; pngReader.ChunkLoadBehaviour = ChunkLoadBehaviour.LOAD_CHUNK_NEVER; pngReader.MaxTotalBytesRead = long.MaxValue; ImageLines imageLines = pngReader.ReadRowsByte(); pngReader.End(); if (imageLines.ImgInfo.BitDepth == 8 && imageLines.ImgInfo.Channels == 4) { Image image = new Image(pngReader.ImgInfo.Cols, pngReader.ImgInfo.Rows); int i = 0; int num = 0; for (; i < image.Height; i++) { byte[] array = imageLines.ScanlinesB[i]; int j = 0; int num2 = 0; for (; j < image.Width; j++) { byte r = array[num2++]; byte g = array[num2++]; byte b = array[num2++]; byte a = array[num2++]; image.Pixels[num++] = new Color(r, g, b, a); } } return(image); } if (imageLines.ImgInfo.BitDepth == 8 && imageLines.ImgInfo.Channels == 3) { Image image2 = new Image(pngReader.ImgInfo.Cols, pngReader.ImgInfo.Rows); int k = 0; int num3 = 0; for (; k < image2.Height; k++) { byte[] array2 = imageLines.ScanlinesB[k]; int l = 0; int num4 = 0; for (; l < image2.Width; l++) { byte r2 = array2[num4++]; byte g2 = array2[num4++]; byte b2 = array2[num4++]; image2.Pixels[num3++] = new Color(r2, g2, b2); } } return(image2); } if (imageLines.ImgInfo.BitDepth == 8 && imageLines.ImgInfo.Channels == 2 && imageLines.ImgInfo.Greyscale) { Image image3 = new Image(pngReader.ImgInfo.Cols, pngReader.ImgInfo.Rows); int m = 0; int num5 = 0; for (; m < image3.Height; m++) { byte[] array3 = imageLines.ScanlinesB[m]; int n = 0; int num6 = 0; for (; n < image3.Width; n++) { byte b3 = array3[num6++]; byte a2 = array3[num6++]; image3.Pixels[num5++] = new Color(b3, b3, b3, a2); } } return(image3); } if (imageLines.ImgInfo.BitDepth == 8 && imageLines.ImgInfo.Channels == 1 && imageLines.ImgInfo.Greyscale) { Image image4 = new Image(pngReader.ImgInfo.Cols, pngReader.ImgInfo.Rows); int num7 = 0; int num8 = 0; for (; num7 < image4.Height; num7++) { byte[] array4 = imageLines.ScanlinesB[num7]; int num9 = 0; int num10 = 0; for (; num9 < image4.Width; num9++) { byte b4 = array4[num10++]; image4.Pixels[num8++] = new Color(b4, b4, b4); } } return(image4); } if (imageLines.ImgInfo.BitDepth == 8 && imageLines.ImgInfo.Channels == 1 && imageLines.ImgInfo.Indexed) { PngChunkPLTE pngChunkPLTE = (PngChunkPLTE)pngReader.GetChunksList().GetById1("PLTE"); if (pngChunkPLTE == null) { throw new InvalidOperationException("PLTE chunk not found in indexed PNG."); } Image image5 = new Image(pngReader.ImgInfo.Cols, pngReader.ImgInfo.Rows); int num11 = 0; int num12 = 0; for (; num11 < image5.Height; num11++) { byte[] array5 = imageLines.ScanlinesB[num11]; int num13 = 0; int num14 = 0; for (; num13 < image5.Width; num13++) { byte n2 = array5[num14++]; int entry = pngChunkPLTE.GetEntry(n2); image5.Pixels[num12++] = new Color((entry >> 16) & 0xFF, (entry >> 8) & 0xFF, entry & 0xFF); } } return(image5); } throw new InvalidOperationException("Unsupported PNG pixel format."); }