public sprite_params setup(Bitmap _bmp, bool _apply_palette, int _palette_slot) { sprite_params spr_params = new sprite_params(this); spr_params.m_CHR_data = this; int img_width = _bmp.Width; int img_height = _bmp.Height; 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 index_byte; byte[] pixels_line = null; Color[] plte = _bmp.Palette.Entries; #if DEF_NES int index_clamp_val = 3; int clrs_cnt = utils.CONST_PALETTE_SMALL_NUM_COLORS; #elif DEF_SMS || DEF_PCE int index_clamp_val = 15; int clrs_cnt = Math.Min(utils.CONST_PALETTE_SMALL_NUM_COLORS * utils.CONST_NUM_SMALL_PALETTES, plte.Length); #endif // detect valid borders of an image { BitmapData bmp_data = _bmp.LockBits(new Rectangle(0, 0, img_width, img_height), ImageLockMode.ReadOnly, _bmp.PixelFormat); if (bmp_data != null) { if (_apply_palette) // && plte.Length <= 16 ) <-- there are 256 colors palette on Linux here... why?!.. { // find nearest colors #if DEF_NES for (i = 0; i < clrs_cnt; i++) { palette_group.Instance.get_palettes_arr()[0].get_color_inds()[i] = utils.find_nearest_color_ind(plte[i % 4].ToArgb()); } palette_group.Instance.get_palettes_arr()[0].update(); palette_group.Instance.active_palette = 0; #elif DEF_SMS for (i = 0; i < clrs_cnt; i++) { 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[i % 16].ToArgb()); } for (i = 0; i < utils.CONST_NUM_SMALL_PALETTES; i++) { palette_group.Instance.get_palettes_arr()[i].update(); } #elif DEF_FIXED_LEN_PALETTE16_ARR palettes_array plt_arr = palettes_array.Instance; plt_arr.palette_index = _palette_slot; for (i = 0; i < clrs_cnt; i++) { plt_arr.update_color(_palette_slot, i, utils.find_nearest_color_ind(plte[i % 16].ToArgb())); } plt_arr.update_palette(); #endif } // fill the lines_arr // (indexed BMP has no transparency in fact) { IntPtr data_ptr = bmp_data.Scan0; for (i = 0; i < img_height; i++) { pixels_line = new byte[img_width]; for (j = 0; j < img_width; j++) { if (_bmp.PixelFormat == System.Drawing.Imaging.PixelFormat.Format8bppIndexed) { index_byte = Marshal.ReadByte(data_ptr, j + (i * bmp_data.Stride)); pixels_line[j] = ( byte )(index_byte & index_clamp_val); } else { index_byte = Marshal.ReadByte(data_ptr, (j >> 1) + (i * bmp_data.Stride)); pixels_line[j] = ( byte )((((j & 0x01) == 0x01) ? (index_byte & 0x0f):((index_byte & 0xf0) >> 4)) & index_clamp_val); } } lines_arr.Add(pixels_line); } } _bmp.UnlockBits(bmp_data); } } return(cut_CHRs(spr_params, min_x, max_x, min_y, max_y, lines_arr, false, -1, -1, false, _palette_slot)); }
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)); }
void BtnOkClick_Event(object sender, EventArgs e) { if (m_color_A >= 0 && m_color_B >= 0) { uint chr_key; CHR_data chr_data; #if DEF_FIXED_LEN_PALETTE16_ARR palettes_array plts_arr = palettes_array.Instance; #endif SortedSet <uint> used_CHR_data = new SortedSet <uint>(); foreach (sprite_data spr in m_sprites_arr) { foreach (CHR_data_attr attr in spr.get_CHR_attr()) { #if DEF_FIXED_LEN_PALETTE16_ARR if (attr.palette_ind == plts_arr.palette_index) { #endif chr_key = ( uint )(((spr.get_CHR_data().id & 0x0000ffff) << 16) | (attr.CHR_ind & 0x0000ffff)); if (!used_CHR_data.Contains(chr_key)) { chr_data = spr.get_CHR_data().get_data()[attr.CHR_ind]; for (int pix_n = 0; pix_n < utils.CONST_CHR_TOTAL_PIXELS_CNT; pix_n++) { if (chr_data.get_data()[pix_n] == m_color_A) { chr_data.get_data()[pix_n] = ( byte )m_color_B; } else if (chr_data.get_data()[pix_n] == m_color_B) { chr_data.get_data()[pix_n] = ( byte )m_color_A; } } used_CHR_data.Add(chr_key); } #if DEF_FIXED_LEN_PALETTE16_ARR } #endif } } #if DEF_FIXED_LEN_PALETTE16_ARR plts_arr.swap_colors(m_color_A, m_color_B); plts_arr.update_palette(); #else palette_group plt_grp = palette_group.Instance; palette_small[] sm_plts_arr = plt_grp.get_palettes_arr(); int ind_A = sm_plts_arr[m_color_A >> 2].get_color_inds()[m_color_A & 0x03]; int ind_B = sm_plts_arr[m_color_B >> 2].get_color_inds()[m_color_B & 0x03]; sm_plts_arr[m_color_A >> 2].get_color_inds()[m_color_A & 0x03] = ind_B; sm_plts_arr[m_color_B >> 2].get_color_inds()[m_color_B & 0x03] = ind_A; for (int j = 0; j < utils.CONST_NUM_SMALL_PALETTES; j++) { sm_plts_arr[j].update(); } #endif } else { MainForm.message_box("Please, select both Color A and Color B!", "Colors Swapping Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }