public static Bitmap ConvertFromRadial(RadialBitmap rbmp) { var destImg = new Bitmap(rbmp.originalWidth, rbmp.originalHight); var pixelAcc = new double[destImg.Width, destImg.Height, 4]; var pixelSaturation = new double[destImg.Width, destImg.Height]; var centerPoint = new Vector(destImg.Width / 2.0, destImg.Height / 2.0); for (var x = 0; x < rbmp.width; x++) { var directionVector = DirectionVector(x, destImg, centerPoint); for (var y = 0; y < rbmp.hight; y++) { var pixelLocation = y * directionVector; DisperceRadial(rbmp.img.GetPixel(x, y), pixelLocation, pixelAcc, pixelSaturation); } } for (var x = 0; x < destImg.Width; x++) { for (var y = 0; y < destImg.Height; y++) { try { destImg.SetPixel((x + (int)centerPoint.X) % destImg.Width, (y + (int)centerPoint.Y) % destImg.Height, Color.FromArgb((int)Math.Floor(pixelAcc[x, y, 0] / pixelSaturation[x, y]), (int)Math.Floor(pixelAcc[x, y, 1] / pixelSaturation[x, y]), (int)Math.Floor(pixelAcc[x, y, 2] / pixelSaturation[x, y]), (int)Math.Floor(pixelAcc[x, y, 3] / pixelSaturation[x, y]))); } catch (Exception e) { MessageBox.Show(e.Message); destImg.SetPixel(x, y, Color.HotPink); } } } return(destImg); }
private void DisperceRadial(RadialBitmap rbmp, ConcurrentQueue <DVector <int> > pixels, double[,,] output, double[,] pixelSaturation) { try { while (!pixels.IsEmpty) { if (pixels.TryDequeue(out var workingPixel)) { var px = workingPixel.Direction.X; var py = workingPixel.Direction.Y; int l1, l2; lock (output) { l1 = output.GetLength(0); l2 = output.GetLength(1); } Color cp; lock (_imgLock) { cp = rbmp.img.GetPixel(workingPixel.x, workingPixel.y); } if (px < -l1 / 2.0 - 1 || px > l1 / 2.0 + 1 || py < -l2 / 2.0 - 1 || py > l2 / 2.0 + 1) { return; } var dx0 = 1.5 + Math.Floor(px - 0.5) - px; var dx1 = 1 - dx0; var dy0 = 1.5 + Math.Floor(py - 0.5) - py; var dy1 = 1 - dy0; var x0 = ((int)Math.Floor(px - 0.5) % l1 + l1) % l1; var x1 = ((int)Math.Floor(px + 0.5) % l1 + l1) % l1; var y0 = ((int)Math.Floor(py - 0.5) % l2 + l2) % l2; var y1 = ((int)Math.Floor(py + 0.5) % l2 + l2) % l2; double[] c00 = { cp.A *dx0 *dy0, cp.R *dx0 *dy0, cp.G *dx0 *dy0, cp.B *dx0 *dy0 }; double[] c10 = { cp.A *dx1 *dy0, cp.R *dx1 *dy0, cp.G *dx1 *dy0, cp.B *dx1 *dy0 }; double[] c01 = { cp.A *dx0 *dy1, cp.R *dx0 *dy1, cp.G *dx0 *dy1, cp.B *dx0 *dy1 }; double[] c11 = { cp.A *dx1 *dy1, cp.R *dx1 *dy1, cp.G *dx1 *dy1, cp.B *dx1 *dy1 }; lock (_outLock) { for (var i = 0; i < 4; i++) { output[x0, y0, i] += c00[i]; output[x1, y0, i] += c10[i]; output[x0, y1, i] += c01[i]; output[x1, y1, i] += c11[i]; } pixelSaturation[x0, y0] += dx0 * dy0; pixelSaturation[x1, y0] += dx1 * dy0; pixelSaturation[x0, y1] += dx0 * dy1; pixelSaturation[x1, y1] += dx1 * dy1; MessageBox.Show( (output[x0, y0, 0] += c00[0] / pixelSaturation[x0, y0]).ToString(CultureInfo .InvariantCulture)); } } } } catch (Exception e) { MessageBox.Show(e.Message + "\n" + e.StackTrace); throw; } }
public Bitmap ConvertFromRadial(RadialBitmap rbmp) { var destImg = new Bitmap(rbmp.originalWidth, rbmp.originalHight); var pixelAcc = new double[destImg.Width, destImg.Height, 4]; var pixelSaturation = new double[destImg.Width, destImg.Height]; var centerPoint = new Vector(destImg.Width / 2.0, destImg.Height / 2.0); // var rect = new Rectangle(0, 0, destImg.Width, destImg.Height); // var data = destImg.LockBits(rect, ImageLockMode.ReadWrite, destImg.PixelFormat); // var depth = Image.GetPixelFormatSize(data.PixelFormat) / 8; //bytes per pixel // var buffer = new byte[data.Width * data.Height * depth]; // Marshal.Copy(data.Scan0, buffer, 0, buffer.Length); var pixels = new ConcurrentQueue <DVector <int> >(); for (var x = 0; x < rbmp.width; x++) { var directionVector = DirectionVector(x, destImg, centerPoint); for (var y = 0; y < rbmp.hight; y++) { var pixelLocation = y * directionVector; pixels.Enqueue(new DVector <int>(x, y, pixelLocation)); } } using (var countdownEvent = new CountdownEvent(ThreadNum)) { for (var i = 0; i < ThreadNum; i++) { ThreadPool.QueueUserWorkItem(x => { DisperceRadial(rbmp, pixels, pixelAcc, pixelSaturation); // ReSharper disable once AccessToDisposedClosure countdownEvent.Signal(); }); } countdownEvent.Wait(); } for (var x = 0; x < destImg.Width; x++) { for (var y = 0; y < destImg.Height; y++) { try { destImg.SetPixel((x + (int)centerPoint.X) % destImg.Width, (y + (int)centerPoint.Y) % destImg.Height, Color.FromArgb((int)Math.Floor(pixelAcc[x, y, 0] / pixelSaturation[x, y]), (int)Math.Floor(pixelAcc[x, y, 1] / pixelSaturation[x, y]), (int)Math.Floor(pixelAcc[x, y, 2] / pixelSaturation[x, y]), (int)Math.Floor(pixelAcc[x, y, 3] / pixelSaturation[x, y]))); } catch (Exception e) { MessageBox.Show(e.Message); destImg.SetPixel(x, y, Color.HotPink); } } } return(destImg); }