private void CreateNewBitmapBunch(bool highResolution, ScalingAlgorithm algorithm) { var newBunch = CanvasHelper.CreateNewBitmapBunch( highResolution, algorithm, LOW_RES_WIDTH, LOW_RES_HEIGHT, this.bitmapBunch, PixelFormat.Format32bppRgb); if (newBunch != null) { this.bitmapBunch = newBunch; } }
public static BitmapBunch CreateNewBitmapBunch(bool highResolution, ScalingAlgorithm algorithm, int minWidth, int minHeight, BitmapBunch oldBitmapBunch, PixelFormat format) { int sizeMultiplier = 1; if (highResolution) { sizeMultiplier *= 2; } if (algorithm == ScalingAlgorithm.Hq2X) { sizeMultiplier *= 2; } else if (algorithm == ScalingAlgorithm.Hq3X) { sizeMultiplier *= 3; } else if (algorithm == ScalingAlgorithm.Hq4X) { sizeMultiplier *= 4; } int newWidth = minWidth * sizeMultiplier; int newHeight = minHeight * sizeMultiplier; if (oldBitmapBunch != null && newWidth == oldBitmapBunch.BitmapWidth && newHeight == oldBitmapBunch.BitmapHeight && highResolution == oldBitmapBunch.HighResolution && algorithm == oldBitmapBunch.ScalingAlgorithm) { return(null); } // Create new bitmap bunch if necessary. var newBunch = new BitmapBunch(minWidth * sizeMultiplier, minHeight * sizeMultiplier, format); newBunch.HighResolution = highResolution; newBunch.ScalingAlgorithm = algorithm; return(newBunch); }
/// <summary> /// Resizes an image using the given algorithm. /// </summary> /// <param name="i">Input image.</param> /// <param name="outSize">Output size. (If Applicable)</param> /// <param name="method">Scaling method.</param> /// <returns>The resized image.</returns> public static Image Resize(Image i, Vec2 outSize, ScalingAlgorithm method = ScalingAlgorithm.NearestNeighbor) { Image o; switch (method) { case ScalingAlgorithm.NearestNeighbor: o = ResizeNearestNeighbor(i, outSize); break; case ScalingAlgorithm.Bilinear: o = ResizeBilinear(i, outSize); break; case ScalingAlgorithm.Bicubic: o = ResizeBicubic(i, outSize); break; case ScalingAlgorithm.Hq2x: o = ResizeHq2x(i); break; case ScalingAlgorithm.Hq3x: o = ResizeHq3x(i); break; case ScalingAlgorithm.Hq4x: o = ResizeHq4x(i); break; default: o = ResizeNearestNeighbor(i, outSize); break; } return(o); }
private static void ThreadedScale(Texture2D texture, int newWidth, int newHeight, ScalingAlgorithm algorithm) { texColors = texture.GetPixels(); newColors = new Color[newWidth * newHeight]; if (algorithm == ScalingAlgorithm.Bilinear) { ratioX = 1.0f / ((float)newWidth / (texture.width - 1)); ratioY = 1.0f / ((float)newHeight / (texture.height - 1)); } else { ratioX = ((float)texture.width) / newWidth; ratioY = ((float)texture.height) / newHeight; } oldWidth = texture.width; TextureScaler.newWidth = newWidth; var cores = Mathf.Min(SystemInfo.processorCount, newHeight); var slice = newHeight / cores; finishCount = 0; if (mutex == null) { mutex = new Mutex(false); } if (cores > 1) { int i = 0; ThreadData threadData; for (i = 0; i < cores - 1; i++) { threadData = new ThreadData(slice * i, slice * (i + 1)); ParameterizedThreadStart ts = algorithm == ScalingAlgorithm.Bilinear ? new ParameterizedThreadStart(BilinearScale) : new ParameterizedThreadStart(PointScale); Thread thread = new Thread(ts); thread.Start(threadData); } threadData = new ThreadData(slice * i, newHeight); if (algorithm == ScalingAlgorithm.Bilinear) { BilinearScale(threadData); } else { PointScale(threadData); } while (finishCount < cores) { Thread.Sleep(1); } } else { ThreadData threadData = new ThreadData(0, newHeight); if (algorithm == ScalingAlgorithm.Bilinear) { BilinearScale(threadData); } else { PointScale(threadData); } } texture.Resize(newWidth, newHeight); texture.SetPixels(newColors); texture.Apply(); }
public static void Scale(Texture2D texture, int newWidth, int newHeight, ScalingAlgorithm algorithm = ScalingAlgorithm.Bilinear) { ThreadedScale(texture, newWidth, newHeight, algorithm); }
public unsafe static void CopyBitmap(IntPtr currentFrame, BitmapDefinition bitmapDefinition, int copyWidth, int copyHeight, bool addBlackBorder, bool copyPointlessAlphaByte, bool allowCrop, ScalingAlgorithm scalingAlgorithm) { Bitmap bitmapToPrepare = bitmapDefinition.Bitmap; BitmapData bitmapData = bitmapToPrepare.LockBits(new Rectangle(0, 0, bitmapToPrepare.Width, bitmapToPrepare.Height), ImageLockMode.WriteOnly, bitmapToPrepare.PixelFormat); int newWidth = 0; int newHeight = 0; FreeDOCore.GetFrameBitmap( currentFrame , bitmapData.Scan0 , bitmapToPrepare.Width , bitmapDefinition.Crop , copyWidth , copyHeight , addBlackBorder , copyPointlessAlphaByte , allowCrop , scalingAlgorithm , out newWidth , out newHeight); bitmapToPrepare.UnlockBits(bitmapData); }
/// <summary> /// Resizes an image using the given algorithm. /// </summary> /// <param name="i">Input image.</param> /// <param name="outSize">Output size. (If Applicable)</param> /// <param name="method">Scaling method.</param> /// <returns>The resized image.</returns> public static Image Resize(Image i, Vec2 outSize, ScalingAlgorithm method = ScalingAlgorithm.NearestNeighbor) { Image o; switch (method) { case ScalingAlgorithm.NearestNeighbor: o = ResizeNearestNeighbor(i, outSize); break; case ScalingAlgorithm.Bilinear: o = ResizeBilinear(i, outSize); break; case ScalingAlgorithm.Bicubic: o = ResizeBicubic(i, outSize); break; case ScalingAlgorithm.Hq2x: o = ResizeHq2x(i); break; case ScalingAlgorithm.Hq3x: o = ResizeHq3x(i); break; case ScalingAlgorithm.Hq4x: o = ResizeHq4x(i); break; default: o = ResizeNearestNeighbor(i, outSize); break; } return o; }
public static void GetFrameBitmap( IntPtr sourceFrame, IntPtr destinationBitmap, int destinationBitmapWidthPixels, BitmapCrop resultingBitmapCrop, int copyWidthPixels, int copyHeightPixels, bool addBlackBorder, bool copyPointlessAlphaByte, bool allowCrop, ScalingAlgorithm scalingAlgorithm, out int resultingWidth, out int resultingHeight) { var crop = new GetFrameBitmapCrop(); GCHandle cropHandle; RawSerialize(crop, out cropHandle); var param = new GetFrameBitmapParams(); param.sourceFrame = sourceFrame; param.destinationBitmap = destinationBitmap; param.destinationBitmapWidthPixels = destinationBitmapWidthPixels; param.bitmapCrop = cropHandle.AddrOfPinnedObject(); param.copyWidthPixels = copyWidthPixels; param.copyHeightPixels = copyHeightPixels; param.addBlackBorder = (byte)(addBlackBorder ? 1 : 0); param.copyPointlessAlphaByte = (byte)(copyPointlessAlphaByte ? 1 : 0); param.allowCrop = (byte)(allowCrop ? 1 : 0); param.scalingAlgorithm = (int)scalingAlgorithm; // Copy into locked structure. GCHandle paramHandle; RawSerialize(param, out paramHandle); ///////////////////// // Run! FreeDoInterface((int)InterfaceFunction.FDP_GET_FRAME_BITMAP, paramHandle.AddrOfPinnedObject()); Marshal.PtrToStructure(cropHandle.AddrOfPinnedObject(), crop); Marshal.PtrToStructure(paramHandle.AddrOfPinnedObject(), param); // Get resulting crop. if (!Properties.Settings.Default.WindowScale) { resultingBitmapCrop.Top = crop.top; resultingBitmapCrop.Left = crop.left; resultingBitmapCrop.Right = crop.right; resultingBitmapCrop.Bottom = crop.bottom; } else if (param.scalingAlgorithm == 1 || Properties.Settings.Default.RenderHighResolution == true) { resultingBitmapCrop.Top = crop.top + 32; resultingBitmapCrop.Left = crop.left + 32; resultingBitmapCrop.Right = crop.right + 32; resultingBitmapCrop.Bottom = crop.bottom + 32; } else if (param.scalingAlgorithm == 2) { resultingBitmapCrop.Top = crop.top + 48; resultingBitmapCrop.Left = crop.left + 48; resultingBitmapCrop.Right = crop.right + 48; resultingBitmapCrop.Bottom = crop.bottom + 48; } else if (param.scalingAlgorithm == 3) { resultingBitmapCrop.Top = crop.top + 64; resultingBitmapCrop.Left = crop.left + 64; resultingBitmapCrop.Right = crop.right + 64; resultingBitmapCrop.Bottom = crop.bottom + 64; } else { resultingBitmapCrop.Top = crop.top + 16; resultingBitmapCrop.Left = crop.left + 16; resultingBitmapCrop.Right = crop.right + 16; resultingBitmapCrop.Bottom = crop.bottom + 16; } resultingWidth = param.resultingWidth; resultingHeight = param.resultingHeight; // Unlock structures. paramHandle.Free(); cropHandle.Free(); }