public void WithFitModeEnum_TransformedQuery(ImageFitMode fitMode, string expectedQuery) { var builder = new ImageUrlBuilder(BaseUrl); builder.WithFitMode(fitMode); Assert.Equal(expectedQuery, builder.Url.Query); }
/// <summary> /// Draws an image to the keyboard. /// </summary> /// <param name="sourceImage">The source image.</param> /// <param name="fitMode">The mode describing how to fit the image to the keyboard.</param> public override void DrawImage(Image sourceImage, ImageFitMode fitMode) { var newImage = this.GetRescaledImage(sourceImage, fitMode); var data = newImage.LockBits(new Rectangle(new Point(0, 0), newImage.Size), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb); byte[] content = new byte[Height * Width * 3]; try { IntPtr ptr = data.Scan0; Marshal.Copy(ptr, content, 0, content.Length); } finally { newImage.UnlockBits(data); } var dict = new Dictionary <uint, Color>(); for (int i = 0; i < K70RgbMappings.PixelLocationX.Length; i++) { for (int j = 0; j < K70RgbMappings.PixelLocationX[i].Length; j++) { int totalRed = 0; int totalGreen = 0; int totalBlue = 0; for (int x = K70RgbMappings.PixelLocationX[i][j]; x < K70RgbMappings.PixelLocationX[i][j] + 3; x++) { for (int y = K70RgbMappings.PixelRowLocationY[i]; y < K70RgbMappings.PixelRowLocationY[i] + 3; y++) { totalBlue += content[(y * Width * 3) + (x * 3)]; totalGreen += content[(y * Width * 3) + (x * 3) + 1]; totalRed += content[(y * Width * 3) + (x * 3) + 2]; } } dict[(uint)K70RgbMappings.KeyPayloadBitsLocation[i][j]] = Color.FromArgb(totalRed / 9, totalGreen / 9, totalBlue / 9); } } dict[(uint)K70Keys.Brightness] = Color.White; dict[(uint)K70Keys.Gaming] = Color.White; dict[(uint)K70Keys.VolumeMute] = Color.White; dict[(uint)K70Keys.MediaNextTrack] = Color.White; dict[(uint)K70Keys.MediaPlayPause] = Color.White; dict[(uint)K70Keys.MediaPreviousTrack] = Color.White; dict[(uint)K70Keys.MediaNextTrack] = Color.White; this.DrawKeys(dict); }
private void SetFit(ImageFitMode fit) { Fit = fit; if (fit == ImageFitMode.FitStart) { Layout.AlignItems = YogaAlign.FlexStart; Layout.JustifyContent = YogaJustify.FlexStart; } else if (fit == ImageFitMode.FitEnd) { Layout.AlignItems = YogaAlign.FlexEnd; Layout.JustifyContent = YogaJustify.FlexEnd; } ImageContainer.Layout.MarkDirty(); }
/// <summary> /// Draws the source image the device. /// </summary> /// <param name="sourceImage">The source image.</param> /// <param name="fitMode">The fit mode.</param> public abstract void DrawImage(Image sourceImage, ImageFitMode fitMode);
/// <summary> /// The fit transformation controls how the output image is fit to its target dimensions after resizing. /// </summary> /// <param name="fitMode">Specifies the mode for the transformation. </param> /// <returns>The same <see cref="ImageUrlBuilder" /> instance. </returns> public ImageUrlBuilder WithFitMode(ImageFitMode fitMode) { _queryParameters["fit"] = StringifyEnum(fitMode); return(this); }
/// <summary> /// Rescales the source image to an image that fits the keyboard. /// </summary> /// <param name="sourceImage">The source image.</param> /// <param name="fitMode">The fit mode.</param> /// <returns>An image fitting the keyboard.</returns> private Bitmap GetRescaledImage(Image sourceImage, ImageFitMode fitMode) { float sourceImageRatio = sourceImage.Size.Width / (float)sourceImage.Size.Height; float rescale; var sourceRectangle = new Rectangle(0, 0, sourceImage.Size.Width, sourceImage.Size.Height); var destinationRectangle = new Rectangle(0, 0, Width, Height); switch (fitMode) { case ImageFitMode.Fill: // Partial source image, change source rectangle if (sourceImageRatio > Ratio) { // Wider, Y is too small rescale = Height / (float)sourceImage.Size.Height; sourceRectangle.X = (int)(sourceImage.Size.Width * rescale / 2); } else { // Taller, X is too small rescale = Width / (float)sourceImage.Size.Width; sourceRectangle.Height = (int)(sourceImage.Size.Height * (sourceImageRatio / Ratio)); sourceRectangle.Y = (int)(sourceImage.Size.Height - sourceRectangle.Height) / 2; } break; case ImageFitMode.Fit: // Partial drawn image, change drawn rectangle if (sourceImageRatio > Ratio) { // Wider, X is too large rescale = Width / (float)sourceImage.Size.Width; destinationRectangle.Height = (int)(sourceImage.Size.Height * rescale); destinationRectangle.Y = (int)(Height - (sourceImage.Size.Height * rescale)) / 2; } else { // Taller, Y is too large rescale = Height / (float)sourceImage.Size.Height; destinationRectangle.Width = (int)(sourceImage.Size.Width * rescale); destinationRectangle.X = (int)(Width - (sourceImage.Size.Width * rescale)) / 2; } break; case ImageFitMode.Stretch: default: // All of source and drawn, use defaults break; } var newImage = new Bitmap(Width, Height); using (var graphics = Graphics.FromImage(newImage)) { graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; graphics.DrawImage(sourceImage, destinationRectangle, sourceRectangle, GraphicsUnit.Pixel); } return(newImage); }
/// <summary> /// Loads a Texture2D from the given image URI (not the full URL). Can alternatively provide a TextureAndBytes struct so the byte array can be used directly for image caching, preventing the need to re-encode the texture. /// </summary> public static IEnumerator LoadImage(string uri, Vector2 size = new Vector2(), bool readable = false, bool alsoReturnBytes = false, ImageFitMode fitMode = ImageFitMode.Contain) { if (string.IsNullOrEmpty(uri)) { Debug.LogWarning("Tried to load null or empty url."); yield break; } // Create www. string url = string.Format("{0}/{1}?w={2}&h={3}&fit={4}", ImagesAPI, uri, size.x, size.y, fitMode.ToString().ToLower()); if (size == Vector2.zero) { url = uri; } WWW www = null; try { www = new WWW(url, null, AuthHeader); } catch (Exception ex) { Debug.LogWarning(string.Format("Failed to access {0} : {1}\r\n{2}.", url, ex.Message, ex.StackTrace)); yield break; } yield return(www); if (!string.IsNullOrEmpty(www.error)) { Debug.LogWarning(string.Format("{0} returned error: {1}", url, www.error)); yield break; } Texture2D texture = null; // Default to non readable texture so the texture doesn't have to be kept in CPU accessible memory. if (!readable) { texture = www.textureNonReadable; } else { texture = www.texture; } if (alsoReturnBytes) { yield return(new TextureAndBytes(www.bytes, texture)); } else { yield return(texture); } }