public static Bitmap New(BitmapData data_bmp) { Bitmap ans; New(out ans,data_bmp.Width, data_bmp.Height); BitmapData data_ans = ans.GetBitmapData(); byte* ptr_bmp = data_bmp.GetPointer(); byte* ptr_ans = data_ans.GetPointer(); Parallel.For(0, data_ans.Height, h => { int i = data_ans.Stride * h; for (int w = 0; w < data_ans.Width; w++) { ptr_ans[i] = ptr_bmp[i]; i++; ptr_ans[i] = ptr_bmp[i]; i++; ptr_ans[i] = ptr_bmp[i]; i++; ptr_ans[i] = ptr_bmp[i]; i++; } }); ans.UnlockBits(data_ans); return ans; }
public static void DrawOpaque(this BitmapData data_bac,BitmapData data_bmp) { if(data_bac.Width!=data_bmp.Width||data_bac.Height!=data_bmp.Height) { throw new ArgumentException("Both Size must be same"); } byte* ptr_bac = data_bac.GetPointer(); byte* ptr_bmp = data_bmp.GetPointer(); Parallel.For(0, data_bac.Height, h => { int i1 = h * data_bac.Stride; int i2 = h * data_bmp.Stride; for (int w = 0; w < data_bac.Width; w++) { if (ptr_bac[i1 + 3] == 0) { i1 += 4; i2 += 4; } else if (ptr_bac[i1 + 3] == 255) { ptr_bac[i1++] = ptr_bmp[i2++]; ptr_bac[i1++] = ptr_bmp[i2++]; ptr_bac[i1++] = ptr_bmp[i2++]; ptr_bac[i1++] = ptr_bmp[i2++]; } else throw new ArgumentException("Alpha value must be 0 or 255"); } }); }
static void Paste_Transparent(this BitmapData data_bac, BitmapData data_bmp, Point p, Rectangle region = default(Rectangle)) { if (region == default(Rectangle)) region = new Rectangle(0, 0, data_bac.Width, data_bac.Height); byte* ptr_bac = data_bac.GetPointer(); byte* ptr_bmp = data_bmp.GetPointer(); int w1 = Math.Max(Math.Max(-p.X, 0), region.X - p.X); int w2 = Math.Min(Math.Min(data_bmp.Width, data_bac.Width - p.X), region.X + region.Width - p.X); int h1 = Math.Max(Math.Max(-p.Y, 0), region.Y - p.Y); int h2 = Math.Min(Math.Min(data_bmp.Height, data_bac.Height - p.Y), region.Y + region.Height - p.Y); if (w1 >= w2 || h1 >= h2) return; ptr_bac += p.Y * data_bac.Stride + 4 * (w1 + p.X); ptr_bmp += 4 * w1; Parallel.For(h1, h2, h => { int i1 = data_bac.Stride * h; int i2 = data_bmp.Stride * h; for (int w = w1; w < w2; w++) { if (ptr_bmp[i2 + 3] == 255) { ptr_bac[i1++] = ptr_bmp[i2++]; ptr_bac[i1++] = ptr_bmp[i2++]; ptr_bac[i1++] = ptr_bmp[i2++]; ptr_bac[i1++] = ptr_bmp[i2++]; } else if (ptr_bmp[i2 + 3] == 0) { i1 += 4; i2 += 4; } else { Bitmap bmp = BITMAP.New(data_bmp); bmp.Save("ErrorImage", ImageFormat.Png); BitmapBox.Show(bmp); throw new ArgumentException("Alpha Value must be 0 or 255: " + ptr_bmp[i2 + 3].ToString() + "(" + w.ToString() + "," + h.ToString() + ")"); } } }); }
static void Paste_Gradient(this BitmapData data_bac, BitmapData data_bmp, Point p, Rectangle region = default(Rectangle)) { if (region == default(Rectangle)) region = new Rectangle(0, 0, data_bac.Width, data_bac.Height); byte* ptr_bac = data_bac.GetPointer(); byte* ptr_bmp = data_bmp.GetPointer(); int w1 = Math.Max(Math.Max(-p.X, 0), region.X - p.X); int w2 = Math.Min(Math.Min(data_bmp.Width, data_bac.Width - p.X), region.X + region.Width - p.X); int h1 = Math.Max(Math.Max(-p.Y, 0), region.Y - p.Y); int h2 = Math.Min(Math.Min(data_bmp.Height, data_bac.Height - p.Y), region.Y + region.Height - p.Y); if (w1 >= w2 || h1 >= h2) return; ptr_bac += p.Y * data_bac.Stride + 4 * (w1 + p.X); ptr_bmp += 4 * w1; Parallel.For(h1, h2, h => { int i1 = data_bac.Stride * h; int i2 = data_bmp.Stride * h; int b; for (int w = w1; w < w2; w++, i1++, i2++) { b = ptr_bmp[i2 + 3]; ptr_bac[i1] = b.Approach_Byte(ptr_bac[i1], ptr_bmp[i2]); i1++; i2++; ptr_bac[i1] = b.Approach_Byte(ptr_bac[i1], ptr_bmp[i2]); i1++; i2++; ptr_bac[i1] = b.Approach_Byte(ptr_bac[i1], ptr_bmp[i2]); i1++; i2++; } }); }
unsafe static void DrawBackgroundImage(BitmapData data_bac) { byte* ptr_bac = data_bac.GetPointer(); Parallel.For(0, data_bac.Height, h => { int i = h * data_bac.Stride; Color c = GetColorByScreenY(h); for (int w = 0; w < data_bac.Width; w++) { ptr_bac[i++] = c.B; ptr_bac[i++] = c.G; ptr_bac[i++] = c.R; ptr_bac[i++] = c.A; } }); foreach (var a in UNDER) { a.DrawImage(data_bac); } Sky.DrawImage(data_bac); }