private void Button1_Click(object sender, EventArgs e) { using (OpenFileDialog fd = new OpenFileDialog()) { fd.DefaultExt = "map"; fd.Filter = "Map files|*.map"; if (fd.ShowDialog() == DialogResult.OK) { TextBox1.Text = fd.FileName; byte[] tmp = File.ReadAllBytes(fd.FileName); map = new TileIndex[128, 128]; for (int lr = 0; lr < 128; lr++) { for (int lc = 0; lc < 128; lc++) { if (((lr * 128) + lc) * 2 < tmp.Length) { map[lc, lr] = new TileIndex(tmp, ((lr * 128) + lc) * 2); } } } LevelBmp = new Bitmap(Panel1.Width, Panel1.Height); LevelImg8bpp = new BitmapBits(Panel1.Width, Panel1.Height); LevelGfx = Graphics.FromImage(LevelBmp); LevelGfx.SetOptions(); PanelGfx = Panel1.CreateGraphics(); PanelGfx.SetOptions(); loaded = true; Button2.Enabled = true; DrawLevel(); } } }
internal void DrawLevel() { if (!loaded) { return; } LevelGfx.Clear(palette[0]); LevelImg8bpp.Clear(); Point pnlcur = Panel1.PointToClient(Cursor.Position); for (int y = Math.Max(VScrollBar1.Value / 32, 0); y <= Math.Min((VScrollBar1.Value + (Panel1.Height - 1)) / 32, map.GetLength(1) - 1); y++) { for (int x = Math.Max(HScrollBar1.Value / 32, 0); x <= Math.Min((HScrollBar1.Value + (Panel1.Width - 1)) / 32, map.GetLength(0) - 1); x++) { if (map[x, y].Tile < tiles.Count) { LevelImg8bpp.DrawBitmapComposited(flippedtiles[map[x, y].List][map[x, y].Tile], new Point(x * 32 - HScrollBar1.Value, y * 32 - VScrollBar1.Value)); } } } LevelGfx.DrawImage(LevelImg8bpp.ToBitmap(palette), 0, 0, LevelImg8bpp.Width, LevelImg8bpp.Height); BitmapBits bmp2 = new BitmapBits(tiles[tileList1.SelectedIndex]); bmp2.Rotate((byte)domainUpDown1.SelectedIndex); bmp2.Flip(checkBox1.Checked, false); LevelGfx.DrawImage(bmp2.ToBitmap(palette), new Rectangle((((pnlcur.X + HScrollBar1.Value) / 32) * 32) - HScrollBar1.Value, (((pnlcur.Y + VScrollBar1.Value) / 32) * 32) - VScrollBar1.Value, 32, 32), 0, 0, 32, 32, GraphicsUnit.Pixel, imageTransparency); PanelGfx.DrawImage(LevelBmp, 0, 0, Panel1.Width, Panel1.Height); }
private static void LoadBitmap8BppIndexed(BitmapBits bmp, byte[] Bits, int Stride) { for (int y = 0; y < bmp.Height; y++) { int srcaddr = y * Math.Abs(Stride); for (int x = 0; x < bmp.Width; x++) { bmp[x, y] = (byte)(Bits[srcaddr + x] & 0x3F); } } }
private static void LoadBitmap64BppArgb(BitmapBits bmp, byte[] Bits, int Stride) { for (int y = 0; y < bmp.Height; y++) { int srcaddr = y * Math.Abs(Stride); for (int x = 0; x < bmp.Width; x++) { bmp[x, y] = (byte)Array.IndexOf(MainForm.palette, Color.FromArgb(BitConverter.ToUInt16(Bits, srcaddr + (x * 8) + 6) / 255, BitConverter.ToUInt16(Bits, srcaddr + (x * 8) + 4) / 255, BitConverter.ToUInt16(Bits, srcaddr + (x * 8) + 2) / 255, BitConverter.ToUInt16(Bits, srcaddr + (x * 8)) / 255).FindNearestMatch(MainForm.palette)); } } }
private static void LoadBitmap32BppRgb(BitmapBits bmp, byte[] Bits, int Stride) { for (int y = 0; y < bmp.Height; y++) { int srcaddr = y * Math.Abs(Stride); for (int x = 0; x < bmp.Width; x++) { bmp[x, y] = (byte)Array.IndexOf(MainForm.palette, Color.FromArgb(Bits[srcaddr + (x * 4) + 2], Bits[srcaddr + (x * 4) + 1], Bits[srcaddr + (x * 4)]).FindNearestMatch(MainForm.palette)); } } }
public void DrawBitmap(BitmapBits source, Point location) { int dstx = location.X; int dsty = location.Y; for (int i = 0; i < source.Height; i++) { int di = ((dsty + i) * Width) + dstx; int si = i * source.Width; Array.Copy(source.Bits, si, Bits, di, source.Width); } }
private static void LoadBitmap32BppArgb(BitmapBits bmp, byte[] Bits, int Stride) { for (int y = 0; y < bmp.Height; y++) { int srcaddr = y * Math.Abs(Stride); for (int x = 0; x < bmp.Width; x++) { Color col = Color.FromArgb(BitConverter.ToInt32(Bits, srcaddr + (x * 4))); bmp[x, y] = (byte)Array.IndexOf(MainForm.palette, col.FindNearestMatch(MainForm.palette)); } } }
private static void LoadBitmap16BppGrayScale(BitmapBits bmp, byte[] Bits, int Stride) { for (int y = 0; y < bmp.Height; y++) { int srcaddr = y * Math.Abs(Stride); for (int x = 0; x < bmp.Width; x++) { ushort pix = BitConverter.ToUInt16(Bits, srcaddr + (x * 2)); bmp[x, y] = (byte)Array.IndexOf(MainForm.palette, Color.FromArgb(pix >> 8, pix >> 8, pix >> 8).FindNearestMatch(MainForm.palette)); } } }
public BitmapBits Scale(int factor) { BitmapBits res = new BitmapBits(Width * factor, Height * factor); for (int y = 0; y < res.Height; y++) { for (int x = 0; x < res.Width; x++) { res[x, y] = this[(x / factor), (y / factor)]; } } return(res); }
private static void LoadBitmap1BppIndexed(BitmapBits bmp, byte[] Bits, int Stride) { for (int y = 0; y < bmp.Height; y++) { int srcaddr = y * Math.Abs(Stride); for (int x = 0; x < bmp.Width; x += 8) { bmp[x + 0, y] = (byte)((Bits[srcaddr + (x / 8)] >> 7) & 1); bmp[x + 1, y] = (byte)((Bits[srcaddr + (x / 8)] >> 6) & 1); bmp[x + 2, y] = (byte)((Bits[srcaddr + (x / 8)] >> 5) & 1); bmp[x + 3, y] = (byte)((Bits[srcaddr + (x / 8)] >> 4) & 1); bmp[x + 4, y] = (byte)((Bits[srcaddr + (x / 8)] >> 3) & 1); bmp[x + 5, y] = (byte)((Bits[srcaddr + (x / 8)] >> 2) & 1); bmp[x + 6, y] = (byte)((Bits[srcaddr + (x / 8)] >> 1) & 1); bmp[x + 7, y] = (byte)(Bits[srcaddr + (x / 8)] & 1); } } }
private static void LoadBitmap16BppRgb565(BitmapBits bmp, byte[] Bits, int Stride) { for (int y = 0; y < bmp.Height; y++) { int srcaddr = y * Math.Abs(Stride); for (int x = 0; x < bmp.Width; x++) { ushort pix = BitConverter.ToUInt16(Bits, srcaddr + (x * 2)); int R = (pix >> 11) & 0x1F; R = R << 3 | R >> 2; int G = (pix >> 5) & 0x3F; G = G << 2 | G >> 4; int B = pix & 0x1F; B = B << 3 | B >> 2; bmp[x, y] = (byte)Array.IndexOf(MainForm.palette, Color.FromArgb(R, G, B).FindNearestMatch(MainForm.palette)); } } }
private void Button3_Click(object sender, EventArgs e) { using (OpenFileDialog fd = new OpenFileDialog()) { fd.DefaultExt = "cm_"; fd.Filter = "CM_ Files|*.cm_|All Files|*.*"; if (fd.ShowDialog() == DialogResult.OK) { TextBox2.Text = fd.FileName; byte[] file = SZDDComp.SZDDComp.Decompress(fd.FileName); if (BitConverter.ToInt32(file, 0) != 0x4C524353) { MessageBox.Show("Not a valid tile file."); return; } tiles = new List <BitmapBits>(); tiles.Add(new BitmapBits(32, 32)); int numSprites = BitConverter.ToInt32(file, 8); int spriteOff = BitConverter.ToInt32(file, 0xC); for (int i = 0; i < numSprites; i++) { int data = (i * 4) + 0x18; int width = BitConverter.ToInt16(file, data); int height = BitConverter.ToInt16(file, data + 2); byte[] sprite = new byte[(width / 2) * height]; Array.Copy(file, spriteOff, sprite, 0, sprite.Length); spriteOff += sprite.Length; BitmapBits bmp = new BitmapBits(width, height, sprite); tiles.Add(bmp); for (int l = 0; l < 8; l++) { BitmapBits bmp2 = new BitmapBits(bmp); bmp2.Rotate(l & 3); bmp2.Flip((l & 4) == 4, false); flippedtiles[l].Add(bmp2); } } tileList1.SelectedIndex = 0; tileList1.ChangeSize(); tileList1.Invalidate(); DrawLevel(); } } }
private void MainForm_Load(object sender, EventArgs e) { ColorMatrix x = new System.Drawing.Imaging.ColorMatrix(); x.Matrix33 = 0.75f; imageTransparency.SetColorMatrix(x, System.Drawing.Imaging.ColorMatrixFlag.Default, System.Drawing.Imaging.ColorAdjustType.Bitmap); BitmapBits bits = new BitmapBits(32, 32); tiles.Add(bits); for (int i = 0; i < 8; i++) { flippedtiles[i] = new List <BitmapBits>() { bits } } ; tileList1.SelectedIndex = 0; domainUpDown1.SelectedIndex = 0; Bitmap bmp = new Bitmap(1, 1, System.Drawing.Imaging.PixelFormat.Format4bppIndexed); palette = bmp.Palette.Entries; }
public void DrawBitmapComposited(BitmapBits source, Point location) { int srcl = 0; if (location.X < 0) { srcl = -location.X; } int srct = 0; if (location.Y < 0) { srct = -location.Y; } int srcr = source.Width; if (srcr > Width - location.X) { srcr = Width - location.X; } int srcb = source.Height; if (srcb > Height - location.Y) { srcb = Height - location.Y; } for (int i = srct; i < srcb; i++) { for (int x = srcl; x < srcr; x++) { if (source[x, i] != 0) { this[location.X + x, location.Y + i] = source[x, i]; } } } }
public static BitmapBits FromBitmap(Bitmap bmp, out int palette) { BitmapBits bmpbits = new BitmapBits(8, 8); BitmapData bmpd = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, bmp.PixelFormat); byte[] Bits = new byte[Math.Abs(bmpd.Stride) * bmpd.Height]; System.Runtime.InteropServices.Marshal.Copy(bmpd.Scan0, Bits, 0, Bits.Length); bmp.UnlockBits(bmpd); switch (bmpd.PixelFormat) { case PixelFormat.Format16bppArgb1555: LoadBitmap16BppArgb1555(bmpbits, Bits, bmpd.Stride); break; case PixelFormat.Format16bppGrayScale: LoadBitmap16BppGrayScale(bmpbits, Bits, bmpd.Stride); break; case PixelFormat.Format16bppRgb555: LoadBitmap16BppRgb555(bmpbits, Bits, bmpd.Stride); break; case PixelFormat.Format16bppRgb565: LoadBitmap16BppRgb565(bmpbits, Bits, bmpd.Stride); break; case PixelFormat.Format1bppIndexed: LoadBitmap1BppIndexed(bmpbits, Bits, bmpd.Stride); palette = 0; return(bmpbits); case PixelFormat.Format24bppRgb: LoadBitmap24BppRgb(bmpbits, Bits, bmpd.Stride); break; case PixelFormat.Format32bppArgb: LoadBitmap32BppArgb(bmpbits, Bits, bmpd.Stride); break; case PixelFormat.Format32bppPArgb: throw new NotImplementedException(); case PixelFormat.Format32bppRgb: LoadBitmap32BppRgb(bmpbits, Bits, bmpd.Stride); break; case PixelFormat.Format48bppRgb: LoadBitmap48BppRgb(bmpbits, Bits, bmpd.Stride); break; case PixelFormat.Format4bppIndexed: LoadBitmap4BppIndexed(bmpbits, Bits, bmpd.Stride); palette = 0; return(bmpbits); case PixelFormat.Format64bppArgb: LoadBitmap64BppArgb(bmpbits, Bits, bmpd.Stride); break; case PixelFormat.Format64bppPArgb: throw new NotImplementedException(); case PixelFormat.Format8bppIndexed: LoadBitmap8BppIndexed(bmpbits, Bits, bmpd.Stride); break; } int[] palcnt = new int[4]; for (int y = 0; y < 8; y++) { for (int x = 0; x < 8; x++) { palcnt[bmpbits[x, y] / 16]++; bmpbits[x, y] &= 15; } } palette = 0; if (palcnt[1] > palcnt[palette]) { palette = 1; } if (palcnt[2] > palcnt[palette]) { palette = 2; } if (palcnt[3] > palcnt[palette]) { palette = 3; } return(bmpbits); }