public CapturePluginPageViewModel() { clientCore = MainWindowViewModel.clientCore; clientCore.CaptureRefreshHandler += new System.EventHandler <CaptureRefreshEventArgs>(OnCaptureRefresh); GlobalBMP = new Bitmap(((1366 + 127) / 128 * 128), ((768 + 127) / 128) * 128); GlobalLocker = new BitmapLocker(GlobalBMP); }
private protected override Bitmap Process(Bitmap bmp, Rectangle region) { if (bmp.PixelFormat != PixelFormat.Format32bppArgb) { bmp = bmp.ToARGB32(); } Bitmap result = new(bmp.Width, bmp.Height, PixelFormat.Format32bppArgb); BitmapLocker src = bmp; BitmapLocker dst = result; src.LockRGBAPixels((ps, ws, hs) => dst.LockRGBAPixels((pd, wd, hd) => { Process(bmp, ps, pd, region); Parallel.For(0, ws * hs, i => { if (!region.Contains(i % ws, i / ws)) { pd[i] = ps[i]; } }); })); return(result); }
public SpriteSheet GetSpriteSheet(FrameGroupType groupType) { FrameGroup frameGroup = this.GetFrameGroup(groupType); if (frameGroup == null) { return(null); } Sprite[] sprites = this.sprites[groupType]; int totalX = frameGroup.PatternZ * frameGroup.PatternX * frameGroup.Layers; int totalY = frameGroup.Frames * frameGroup.PatternY; int bitmapWidth = (totalX * frameGroup.Width) * Sprite.DefaultSize; int bitmapHeight = (totalY * frameGroup.Height) * Sprite.DefaultSize; int pixelsWidth = frameGroup.Width * Sprite.DefaultSize; int pixelsHeight = frameGroup.Height * Sprite.DefaultSize; Bitmap bitmap = new Bitmap(bitmapWidth, bitmapHeight, PixelFormat.Format32bppArgb); Dictionary <int, Rect> rectList = new Dictionary <int, Rect>(); BitmapLocker lockBitmap = new BitmapLocker(bitmap); lockBitmap.LockBits(); for (int f = 0; f < frameGroup.Frames; f++) { for (int z = 0; z < frameGroup.PatternZ; z++) { for (int y = 0; y < frameGroup.PatternY; y++) { for (int x = 0; x < frameGroup.PatternX; x++) { for (int l = 0; l < frameGroup.Layers; l++) { int index = frameGroup.GetTextureIndex(l, x, y, z, f); int fx = (index % totalX) * pixelsWidth; int fy = (int)(Math.Floor((decimal)(index / totalX)) * pixelsHeight); rectList.Add(index, new Rect(fx, fy, pixelsWidth, pixelsHeight)); for (int w = 0; w < frameGroup.Width; w++) { for (int h = 0; h < frameGroup.Height; h++) { index = frameGroup.GetSpriteIndex(w, h, l, x, y, z, f); int px = (frameGroup.Width - w - 1) * Sprite.DefaultSize; int py = (frameGroup.Height - h - 1) * Sprite.DefaultSize; lockBitmap.CopyPixels(sprites[index].GetBitmap(), px + fx, py + fy); } } } } } } } lockBitmap.UnlockBits(); lockBitmap.Dispose(); return(new SpriteSheet(bitmap, rectList)); }
public SpriteSheet GetSpriteSheet(ushort id, ThingCategory category, FrameGroupType groupType) { ThingType thing = this.Things.GetThing(id, category); if (thing == null) { return(null); } FrameGroup group = thing.GetFrameGroup(groupType); int totalX = group.PatternZ * group.PatternX * group.Layers; int totalY = group.Frames * group.PatternY; int bitmapWidth = (totalX * group.Width) * Sprite.DefaultSize; int bitmapHeight = (totalY * group.Height) * Sprite.DefaultSize; int pixelsWidth = group.Width * Sprite.DefaultSize; int pixelsHeight = group.Height * Sprite.DefaultSize; Bitmap bitmap = new Bitmap(bitmapWidth, bitmapHeight, PixelFormat.Format32bppArgb); Dictionary <int, Rect> rectList = new Dictionary <int, Rect>(); BitmapLocker lockBitmap = new BitmapLocker(bitmap); lockBitmap.LockBits(); for (int f = 0; f < group.Frames; f++) { for (int z = 0; z < group.PatternZ; z++) { for (int y = 0; y < group.PatternY; y++) { for (int x = 0; x < group.PatternX; x++) { for (int l = 0; l < group.Layers; l++) { int index = group.GetTextureIndex(l, x, y, z, f); int fx = (index % totalX) * pixelsWidth; int fy = (int)(Math.Floor((decimal)(index / totalX)) * pixelsHeight); rectList.Add(index, new Rect(fx, fy, pixelsWidth, pixelsHeight)); for (int w = 0; w < group.Width; w++) { for (int h = 0; h < group.Height; h++) { index = group.GetSpriteIndex(w, h, l, x, y, z, f); int px = ((group.Width - w - 1) * Sprite.DefaultSize); int py = ((group.Height - h - 1) * Sprite.DefaultSize); uint spriteId = group.SpriteIDs[index]; lockBitmap.CopyPixels(this.Sprites.GetSpriteBitmap(spriteId), px + fx, py + fy); } } } } } } } lockBitmap.UnlockBits(); return(new SpriteSheet(bitmap, rectList)); }
public Bitmap GetObjectImage(ushort id, ThingCategory category, FrameGroupType groupType) { ThingType thing = this.Things.GetThing(id, category); if (thing == null) { return(null); } Bitmap bitmap = this.spriteCache.GetPicture(id, category); if (bitmap != null) { return(bitmap); } FrameGroup group = thing.GetFrameGroup(groupType); int width = Sprite.DefaultSize * group.Width; int height = Sprite.DefaultSize * group.Height; bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb); BitmapLocker lockBitmap = new BitmapLocker(bitmap); lockBitmap.LockBits(); byte layers = group.Layers; byte x = 0; if (category == ThingCategory.Outfit) { layers = 1; x = (byte)(2 % group.PatternX); } // draw sprite for (byte l = 0; l < layers; l++) { for (byte w = 0; w < group.Width; w++) { for (byte h = 0; h < group.Height; h++) { int index = group.GetSpriteIndex(w, h, l, x, 0, 0, 0); uint spriteId = group.SpriteIDs[index]; int px = (group.Width - w - 1) * Sprite.DefaultSize; int py = (group.Height - h - 1) * Sprite.DefaultSize; lockBitmap.CopyPixels(this.Sprites.GetSpriteBitmap(spriteId), px, py); } } } lockBitmap.UnlockBits(); lockBitmap.Dispose(); this.spriteCache.SetPicture(id, category, bitmap); return(bitmap); }
public void ShouldChangeColor() { var bitmap = new Bitmap(1, 1); var newColor = Color.FromArgb(100, 150, 200); using (var bl = new BitmapLocker(bitmap)) { bl.SetPixel(0, 0, newColor); } Assert.AreEqual(newColor, bitmap.GetPixel(0, 0)); }
internal protected override unsafe void Process(Bitmap bmp, RGBAColor *p_top, RGBAColor *p_dest, Rectangle region) { if (bmp.Size != BaseLayer.Size) { throw new ArgumentException($"The top layer ('{nameof(bmp)}') must have the same dimensions as the base layer. The required dimensions are: {BaseLayer.Width}x{BaseLayer.Height}.", nameof(bmp)); } int[] indices = GetIndices(BaseLayer, region); BitmapLocker lck = BaseLayer; BlendMode mode = Mode; lck.LockRGBAPixels((p_base, w, h) => Parallel.For(0, indices.Length, i => { int idx = indices[i]; p_dest[idx] = RGBAColor.Blend(p_base[idx], p_top[idx], mode); })); }
private void ReadFromBitmap(Bitmap bmp) { using Bitmap tmp = new(bmp); using Bitmap copy = tmp.Clone(new Rectangle(0, 0, tmp.Width, tmp.Height), PixelFormat.Format32bppArgb); BitmapLocker lck = copy; Scalar factor = new Scalar(255d).Inverse; lck.LockPixels((px, w, h) => { for (int i = 0, l = w * h; i < l; ++i) { _coefficients[i] = new Vector4( px[i * 4 + 2], // r px[i * 4 + 1], // g px[i * 4 + 0], // b px[i * 4 + 3] // a ) * factor; } }); }
public Bitmap ToBitmap() { Bitmap bmp = new(Width, Height, PixelFormat.Format32bppArgb); BitmapLocker lck = bmp; Scalar factor = 255; lck.LockPixels((px, w, h) => { for (int i = 0, l = w * h; i < l; ++i) { Vector4 col = _coefficients[i].Value; px[i * 4 + 2] = (byte)col.X.Clamp().Multiply(factor); // r px[i * 4 + 1] = (byte)col.Y.Clamp().Multiply(factor); // g px[i * 4 + 0] = (byte)col.Z.Clamp().Multiply(factor); // b px[i * 4 + 3] = (byte)col.W.Clamp().Multiply(factor); // a } }); return(bmp); }
private void OnCaptureRefresh(object sender, CaptureRefreshEventArgs msg) { using (MemoryStream ms = new MemoryStream(msg.Data)) { int x = msg.MetaData.Item1; int y = msg.MetaData.Item2; Bitmap recvbmp = (Bitmap)Image.FromStream(ms); BitmapLocker recvdata = new BitmapLocker(recvbmp); recvdata.LockBits(); GlobalLocker.LockBits(); for (int i = 0; i < recvdata.Width; ++i) { for (int j = 0; j < recvdata.Height; ++j) { GlobalLocker.SetPixel(i + x, j + y, recvdata.GetPixel(i, j)); } } GlobalLocker.UnlockBits(); recvdata.UnlockBits(); using (MemoryStream ims = new MemoryStream()) { GlobalBMP.Save(ims, ImageFormat.Bmp); ims.Seek(0, SeekOrigin.Begin); BitmapImage bm = new BitmapImage(); bm.BeginInit(); bm.StreamSource = ims; bm.CacheOption = BitmapCacheOption.OnLoad; bm.EndInit(); CaptureImage = bm; } } }
public Bitmap GetBitmap() { Bitmap bitmap = new Bitmap(this.Width * Sprite.DefaultSize, this.Height * Sprite.DefaultSize, PixelFormat.Format32bppArgb); try { using (BitmapLocker locker = new BitmapLocker(bitmap)) { locker.LockBits(); for (byte l = 0; l < this.Layers; l++) { for (byte w = 0; w < this.Width; w++) { for (byte h = 0; h < this.Height; h++) { int index = w + h * this.Width + l * this.Width * this.Height; int px = (this.Width - w - 1) * Sprite.DefaultSize; int py = (this.Height - h - 1) * Sprite.DefaultSize; locker.CopyPixels(this.SpriteList[index].GetBitmap(), px, py); } } } locker.UnlockBits(); bitmap.MakeTransparent(Color.FromArgb(0x11, 0x11, 0x11)); } } catch { Trace.WriteLine(string.Format("Failed to get image for client id {0}. Check the transparency option.", this.ID)); return(null); } return(bitmap); }
public PluginMethodReturnValueType Shell(GetCaptureDataArgs CopyArgs) { List <ReturnValuePacket> result = new List <ReturnValuePacket>(); IntPtr hwnd = IntPtr.Zero; IntPtr hdc = IntPtr.Zero; if (CaptureUtil.GetWindowHDC(ref hwnd, ref hdc)) { int x = 0; int y = 0; CaptureUtil.GetWindowSize(hwnd, ref x, ref y); const int blockSize = 128; int blockSizeW = (x + blockSize - 1) / blockSize; int blockSizeH = (y + blockSize - 1) / blockSize; var bitmap = new BITMAP(); var bitmapSize = CaptureUtil.DefGetCaptureBlockBitmap(hwnd, hdc, 0, 0, 0, 0, ref bitmap); var bmp = CaptureUtil.DefGetCaptureData(hwnd, hdc, bitmapSize, bitmap, 0, 0, x, y); using (MemoryStream ms = new MemoryStream(bmp)) { var pic = (Bitmap)Image.FromStream(ms); var locker = new BitmapLocker(pic); locker.LockBits(); for (int i = 0; i < blockSizeW; ++i) { for (int j = 0; j < blockSizeH; ++j) { Bitmap outputBmp = new Bitmap(blockSize, blockSize); var outputLocker = new BitmapLocker(outputBmp); outputLocker.LockBits(); int blockBeginX = i * blockSize; int blockBeginY = j * blockSize; int blockWidth = (i + 1) * (blockSize) > x ? (x % blockSize) : blockSize; int blockHeight = (j + 1) * (blockSize) > y ? (y % blockSize) : blockSize; for (int k = 0; k < blockWidth; ++k) { for (int l = 0; l < blockHeight; ++l) { outputLocker.SetPixel(k, l, locker.GetPixel(blockBeginX + k, blockBeginY + l)); } } outputLocker.UnlockBits(); using (MemoryStream oms = new MemoryStream()) { outputBmp.Save(oms, ImageFormat.Jpeg); byte[] data = new byte[oms.Length]; oms.Seek(0, SeekOrigin.Begin); oms.Read(data, 0, data.Length); var meta = new CaptureSplitMetaData() { X = blockBeginX, Y = blockBeginY, Width = blockSize, Height = blockSize, }; var m = JsonConvert.SerializeObject(meta); result.Add(new ReturnValuePacket() { MetaData = m, Data = data, }); } } } locker.UnlockBits(); } } return(new PluginMethodReturnValueType() { DataType = PluginMethodReturnValueType.PacketBinaryDataType, Data = new PluginMehtodReturnValuePacket() { Packet = result, }, }); }
public SpriteSheet GetSpriteSheet(FrameGroupType groupType, OutfitData outfitData) { SpriteSheet rawSpriteSheet = this.GetSpriteSheet(groupType); FrameGroup group = this.ThingType.GetFrameGroup(groupType); if (group.Layers < 2) { return(rawSpriteSheet); } outfitData = outfitData == null ? OUTFIT_DATA : outfitData; int totalX = group.PatternZ * group.PatternX * group.Layers; int totalY = group.Frames * group.PatternY; int bitmapWidth = (totalX * group.Width) * Sprite.DefaultSize; int bitmapHeight = (totalY * group.Height) * Sprite.DefaultSize; int pixelsWidth = group.Width * Sprite.DefaultSize; int pixelsHeight = group.Height * Sprite.DefaultSize; Bitmap grayBitmap = new Bitmap(bitmapWidth, bitmapHeight, PixelFormat.Format32bppArgb); Bitmap blendBitmap = new Bitmap(bitmapWidth, bitmapHeight, PixelFormat.Format32bppArgb); Bitmap bitmap = new Bitmap(bitmapWidth, bitmapHeight, PixelFormat.Format32bppArgb); Dictionary <int, Rect> rectList = new Dictionary <int, Rect>(); for (int f = 0; f < group.Frames; f++) { for (int z = 0; z < group.PatternZ; z++) { for (int x = 0; x < group.PatternX; x++) { int index = (((f % group.Frames * group.PatternZ + z) * group.PatternY) * group.PatternX + x) * group.Layers; rectList[index] = new Rect((z * group.PatternX + x) * pixelsWidth, f * pixelsHeight, pixelsWidth, pixelsHeight); } } } BitmapLocker grayLocker = new BitmapLocker(grayBitmap); BitmapLocker blendLocker = new BitmapLocker(blendBitmap); BitmapLocker bitmapLocker = new BitmapLocker(bitmap); grayLocker.LockBits(); blendLocker.LockBits(); bitmapLocker.LockBits(); for (int y = 0; y < group.PatternY; y++) { if (y == 0 || (outfitData.Addons & 1 << (y - 1)) != 0) { for (int f = 0; f < group.Frames; f++) { for (int z = 0; z < group.PatternZ; z++) { for (int x = 0; x < group.PatternX; x++) { // gets gray bitmap int i = (((f % group.Frames * group.PatternZ + z) * group.PatternY + y) * group.PatternX + x) * group.Layers; Rect rect = rawSpriteSheet.RectList[i]; int rx = rect.X; int ry = rect.Y; int rw = rect.Width; int rh = rect.Height; int index = (((f * group.PatternZ + z) * group.PatternY) * group.PatternX + x) * group.Layers; rect = rectList[index]; int px = rect.X; int py = rect.Y; grayLocker.CopyPixels(rawSpriteSheet.Bitmap, rx, ry, rw, rh, px, py); // gets blend bitmap i++; rect = rawSpriteSheet.RectList[i]; rx = rect.X; ry = rect.Y; rw = rect.Width; rh = rect.Height; blendLocker.CopyPixels(rawSpriteSheet.Bitmap, rx, ry, rw, rh, px, py); } } } bitmapLocker.ColorizePixels(grayLocker.Pixels, blendLocker.Pixels, outfitData.Head, outfitData.Body, outfitData.Legs, outfitData.Feet); } } grayLocker.UnlockBits(); grayLocker.Dispose(); blendLocker.UnlockBits(); blendLocker.Dispose(); bitmapLocker.UnlockBits(); bitmapLocker.Dispose(); grayBitmap.Dispose(); blendBitmap.Dispose(); return(new SpriteSheet(bitmap, rectList)); }
public Bitmap GetObjectImage(ushort id, Direction direction, OutfitData data, bool mount) { ThingType thing = this.Things.GetThing(id, ThingCategory.Outfit); if (thing == null) { return(null); } FrameGroup group = thing.GetFrameGroup(FrameGroupType.Default); if (group.Layers < 2) { return(this.GetObjectImage(id, ThingCategory.Outfit, FrameGroupType.Default)); } int width = Sprite.DefaultSize * group.Width; int height = Sprite.DefaultSize * group.Height; Bitmap grayBitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb); Bitmap blendBitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb); Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb); BitmapLocker grayLocker = new BitmapLocker(grayBitmap); BitmapLocker blendLocker = new BitmapLocker(blendBitmap); BitmapLocker bitmapLocker = new BitmapLocker(bitmap); grayLocker.LockBits(); blendLocker.LockBits(); bitmapLocker.LockBits(); byte x = (byte)((byte)direction % group.PatternX); byte z = mount && group.PatternZ > 1 ? (byte)1 : (byte)0; for (int y = 0; y < group.PatternY; y++) { if (y == 0 || (data.Addons & 1 << (y - 1)) != 0) { for (byte w = 0; w < group.Width; w++) { for (byte h = 0; h < group.Height; h++) { int index = group.GetSpriteIndex(w, h, 0, x, y, z, 0); uint spriteId = group.SpriteIDs[index]; int px = (group.Width - w - 1) * Sprite.DefaultSize; int py = (group.Height - h - 1) * Sprite.DefaultSize; grayLocker.CopyPixels(this.Sprites.GetSpriteBitmap(spriteId), px, py); index = group.GetSpriteIndex(w, h, 1, x, y, z, 0); spriteId = group.SpriteIDs[index]; blendLocker.CopyPixels(this.Sprites.GetSpriteBitmap(spriteId), px, py); } } bitmapLocker.ColorizePixels(grayLocker.Pixels, blendLocker.Pixels, data.Head, data.Body, data.Legs, data.Feet); } } grayLocker.UnlockBits(); grayLocker.Dispose(); blendLocker.UnlockBits(); blendLocker.Dispose(); bitmapLocker.UnlockBits(); bitmapLocker.Dispose(); grayBitmap.Dispose(); blendBitmap.Dispose(); return(bitmap); }