internal override GenericImage <T> Add(GenericImage <T> other) { ImageGray <T> outcome = (ImageGray <T>)ImageFactory.Create(Width, Height, GetColorModel(), GetDataType()); ImageGray <T> otherGray = (ImageGray <T>)other.ToGray(); outcome.Gray = Gray.Add(otherGray.Gray); return(outcome); }
public async void GetImageName_returns_correct_name() { var image = new GenericImage { Url = "example.com/test.jpg" }; Assert.Equal("test.jpg", await image.GetImageName()); }
internal override GenericImage <T> Add(GenericImage <T> other) { ImageHSV <T> outcome = (ImageHSV <T>)ImageFactory.Create(Width, Height, GetColorModel(), GetDataType()); ImageHSV <T> otherHSV = (ImageHSV <T>)other.ToHSV(); outcome.H = H.Add(otherHSV.H); outcome.S = S.Add(otherHSV.S); outcome.V = V.Add(otherHSV.V); return(outcome); }
internal override GenericImage <T> Add(GenericImage <T> other) { ImageCMYK <T> outcome = (ImageCMYK <T>)ImageFactory.Create(Width, Height, GetColorModel(), GetDataType()); ImageCMYK <T> otherCMYK = (ImageCMYK <T>)other.ToCMYK(); outcome.C = C.Add(otherCMYK.C); outcome.M = M.Add(otherCMYK.M); outcome.Y = Y.Add(otherCMYK.Y); outcome.K = K.Add(otherCMYK.K); return(outcome); }
/// <inheritdoc /> protected AbstractContainer(string dockerImageName, IDockerClient dockerClient, ILoggerFactory loggerFactory) { DockerImageName = dockerImageName; DockerClient = dockerClient; _logger = loggerFactory.CreateLogger(GetType()); DockerImage = new GenericImage(dockerClient, loggerFactory) { ImageName = DockerImageName }; }
private bool Equals(GenericImage img) { for (int i = 0; i < Canvas.GetLength(0); i++) { for (int j = 0; j < Canvas.GetLength(1); j++) { if (img.Canvas[i, j] != Canvas[i, j]) { return(false); } } } return(true); }
public static GenericImage From(System.Drawing.Image image) { GenericImage im = new GenericImage(image.Width, image.Height); Bitmap b = (Bitmap)image; int w = im.Width, h = im.Height; for (int i = 0; i < w; i++) { for (int j = 0; j < h; j++) { im.SetPixel(b.GetPixel(i, j), i, j); } } return(im); }
/// <summary> /// Converts a materials <see cref="Material.mainTexture"/> property into a <see cref="GenericImage{T}"/> type takes into account the /// materials <see cref="Material.mainTextureOffset"/> and <see cref="Material.mainTextureScale"/> properties. See remarks. /// </summary> /// <param name="material">The source material to get the <see cref="Material.mainTexture"/> reference from.</param> /// <returns>Returns a <see cref="GenericImage{T}"/> type representing the materials texture co-ordinates.</returns> /// <remarks>This method does not return the original texture specified by the <see cref="Material.mainTexture"/> property but rather /// takes into account the materials <see cref="Material.mainTextureOffset"/> and <see cref="Material.mainTextureScale"/> properties /// in order to generate a <see cref="GenericImage{T}"/> type.</remarks> /// <exception cref="InvalidCastException">If <see cref="Material.mainTexture"/> cannot be cast to a <see cref="Texture2D"/> type.</exception> /// <exception cref="ArgumentNullException">If the <see cref="material"/> parameter is null.</exception> public static GenericImage<Color> ToGenericImage(this Material material) { // ensure material specified if (material == null) { throw new ArgumentNullException("material"); } // if no main texture then also return null if (material.mainTexture == null) { return null; } var texture = material.mainTexture as Texture2D; // ensure material texture is a Texture2D type if (texture == null) { throw new InvalidCastException("Material texture is not a Texture2D!"); } // create a generic image. var image = texture.CreateGenericImage(); var data = image.ToUnityColor32Array(); var tempImage = new GenericImage<Color>(image.Width, image.Height); tempImage.SetPixels(Array.ConvertAll(data, input => new Color(input.r, input.g, input.b, input.a))); var offset = material.mainTextureOffset; var position = new Point( (int)((offset.x * texture.width) % texture.width), (int)(texture.height - ((offset.y * texture.height) % texture.height))); var scale = material.mainTextureScale; var size = new Size((int)(scale.x * texture.width), (int)(scale.y * texture.height)); var subImage = new GenericImage<Color>(size.Width, size.Height); subImage.Draw(tempImage, 0, 0, position.X, position.Y - size.Height, size.Width, size.Height, (source, blendWith) => blendWith); // convert and return a generic image return subImage; }
/// <summary> /// Handles the event for tile selection. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">An System.EventArgs that contains no event data.</param> private void MainPreviewTileSelection(object sender, TileSelectionEventArgs e) { // do nothing until selection has completed or if no texture selected if (e.Status != TileSelectionStatus.Complete || this.materialControls.TextureAsset == null) { return; } var image = new GenericImage <Color>(this.mainPreview.TileWidth, this.mainPreview.TileHeight); image.Draw( this.materialControls.TextureAsset.CreateGenericImage(), 0, 0, e.Min.X, e.Min.Y, this.mainPreview.TileWidth, this.mainPreview.TileHeight, (source, blendWith) => blendWith); this.SelectedTile = image; }
/// <summary> /// Converts a <see cref="Texture2D"/> to a <see cref="GenericImage{T}"/> type. /// </summary> /// <param name="image">The image to be converted.</param> /// <returns>Returns a new <see cref="GenericImage{T}"/> type.</returns> /// <remarks>The source Texture2D image must be readable.</remarks> public static GenericImage<Color> ToGenericImage(this Texture2D image) { var colors = image.GetPixels32(0); var texture = new GenericImage<Color>(image.width, image.height); var index = 0; for (var y = texture.Height - 1; y >= 0; y--) { for (var x = 0; x < texture.Width; x++) { texture[x, y] = colors[index++]; } } return texture; }
/// <summary> /// Draws a <see cref="GenericImage{T}"/> on to a <see cref="Texture2D"/> texture. /// </summary> /// <param name="texture">A reference to a <see cref="Texture2D"/> type.</param> /// <param name="sourceImage">A reference to the <see cref="GenericImage{T}"/> that will be drawn.</param> /// <param name="sourceRectangle">The source rectangle within the <see cref="sourceImage"/> that will be drawn.</param> /// <param name="position">The position within <see cref="texture"/> where the <see cref="sourceImage"/> will be drawn at.</param> /// <param name="flipHorizontally">If true will flip the <see cref="sourceImage"/> horizontally before drawing.</param> /// <param name="flipVertically">If true will flip the <see cref="sourceImage"/> vertically before drawing.</param> public static void Draw(this Texture2D texture, GenericImage<Color> sourceImage, Rect sourceRectangle, Vector2 position, bool flipHorizontally, bool flipVertically) { Draw(texture, sourceImage, (int)position.x, (int)position.y, (int)sourceRectangle.x, (int)sourceRectangle.y, (int)sourceRectangle.width, (int)sourceRectangle.height, flipHorizontally, flipVertically); }
/// <summary> /// Draws a <see cref="GenericImage{T}"/> on to a <see cref="Texture2D"/> texture. /// </summary> /// <param name="texture">A reference to a <see cref="Texture2D"/> type.</param> /// <param name="sourceImage">A reference to the <see cref="GenericImage{T}"/> that will be drawn.</param> /// <param name="sourceRectangle">The source rectangle within the <see cref="sourceImage"/> that will be drawn.</param> /// <param name="position">The position within <see cref="texture"/> where the <see cref="sourceImage"/> will be drawn at.</param> public static void Draw(this Texture2D texture, GenericImage<Color> sourceImage, Rect sourceRectangle, Vector2 position) { Draw(texture, sourceImage, (int)position.x, (int)position.y, (int)sourceRectangle.x, (int)sourceRectangle.y, (int)sourceRectangle.width, (int)sourceRectangle.height, false, false); }
/// <summary> /// Clones a texture into a readable texture asset and converts it into a <see cref="GenericImage{T}"/> type. Then remove the cloned texture. /// </summary> /// <param name="sourceTexture">The non readable texture typically selected from the asset browser window that will have a /// temporary readable copy of it self made then promptly destroyed.</param> /// <returns>Returns a <see cref="GenericImage{T}"/> containing the same pixel data as the <see cref="sourceTexture"/> parameter.</returns> public static GenericImage<Color> CreateGenericImage(this Texture2D sourceTexture) { // if no texture provided just exit if (sourceTexture == null) { throw new ArgumentNullException("sourceTexture"); } // get the file path to the source texture file var file = AssetDatabase.GetAssetPath(sourceTexture); // if no file returned just exit if (string.IsNullOrEmpty(file) || !File.Exists(file)) { throw new Exception("Texture asset might have been generated dynamically."); } // get the directory that the file is in var filePath = Path.GetDirectoryName(file); // construct a temp filename for the new readable texture file GenericImage<Color> readableTexture = null; if (filePath == null) { return null; } var tempFile = Path.Combine(filePath, Path.GetFileNameWithoutExtension(file) + GlobalConstants.ReadableTextureString); tempFile = AssetDatabase.GenerateUniqueAssetPath(Path.ChangeExtension(tempFile, Path.GetExtension(file))); try { // make a copy of the file source texture file File.Copy(file, tempFile); // import the new temp asset texture AssetDatabase.ImportAsset(tempFile, ImportAssetOptions.Default); // attempt to load the new temporary texture asset var tempTexture = AssetDatabase.LoadAssetAtPath(tempFile, typeof(Texture2D)) as Texture2D; // attempt to convert the readable texture into a generic image type var colors = tempTexture.GetPixels32(0); readableTexture = new GenericImage<Color>(tempTexture.width, tempTexture.height); var index = 0; for (var y = readableTexture.Height - 1; y >= 0; y--) { for (var x = 0; x < readableTexture.Width; x++) { readableTexture[x, y] = colors[index++]; } } // destroy the temp texture reference Object.DestroyImmediate(tempTexture, true); } catch { } try { // attempt to delete temp texture asset if (File.Exists(tempFile)) { // try to ensure that the temp texture asset is removed AssetDatabase.DeleteAsset(tempFile); } } catch { } return readableTexture; }
public static bool IsNull(GenericImage image) { return(image is null); }
/// <summary> /// Clones a texture into a readable texture asset and converts it into a <see cref="GenericImage{T}"/> type. Then remove the cloned texture. /// </summary> /// <param name="sourceTexture">The non readable texture typically selected from the asset browser window that will have a /// temporary readable copy of it self made then promptly destroyed.</param> /// <returns>Returns a <see cref="GenericImage{T}"/> containing the same pixel data as the <see cref="sourceTexture"/> parameter.</returns> public static GenericImage <Color> CreateGenericImage(this Texture2D sourceTexture) { // if no texture provided just exit if (sourceTexture == null) { throw new ArgumentNullException("sourceTexture"); } // get the file path to the source texture file var file = AssetDatabase.GetAssetPath(sourceTexture); // if no file returned just exit if (string.IsNullOrEmpty(file) || !File.Exists(file)) { throw new Exception("Texture asset might have been generated dynamically."); } // get the directory that the file is in var filePath = Path.GetDirectoryName(file); // construct a temp filename for the new readable texture file GenericImage <Color> readableTexture = null; if (filePath == null) { return(null); } var tempFile = Path.Combine(filePath, Path.GetFileNameWithoutExtension(file) + GlobalConstants.ReadableTextureString); tempFile = AssetDatabase.GenerateUniqueAssetPath(Path.ChangeExtension(tempFile, Path.GetExtension(file))); try { // make a copy of the file source texture file File.Copy(file, tempFile); // import the new temp asset texture AssetDatabase.ImportAsset(tempFile, ImportAssetOptions.Default); // attempt to load the new temporary texture asset var tempTexture = AssetDatabase.LoadAssetAtPath(tempFile, typeof(Texture2D)) as Texture2D; // attempt to convert the readable texture into a generic image type var colors = tempTexture.GetPixels32(0); readableTexture = new GenericImage <Color>(tempTexture.width, tempTexture.height); var index = 0; for (var y = readableTexture.Height - 1; y >= 0; y--) { for (var x = 0; x < readableTexture.Width; x++) { readableTexture[x, y] = colors[index++]; } } // destroy the temp texture reference Object.DestroyImmediate(tempTexture, true); } catch { } try { // attempt to delete temp texture asset if (File.Exists(tempFile)) { // try to ensure that the temp texture asset is removed AssetDatabase.DeleteAsset(tempFile); } } catch { } return(readableTexture); }
/// <summary> /// Called by <see cref="GridMapEditor"/> to update the tool on every call to OnSceneGUI. /// </summary> public void Update() { if (!this.isActive || !this.editor.IsMouseOverLayer()) { return; } var current = Event.current; var settings = SettingsManager.Instance; if (this.isMouseDown && current.type == EventType.MouseUp) { this.isMouseDown = false; // we have stopped drawing so clean up objects that were replaced while (this.gameObjectsToRemove.Count > 0) { Undo.DestroyObjectImmediate(this.gameObjectsToRemove[0]); this.gameObjectsToRemove.RemoveAt(0); } // we can return as we have handled a mouse up event return; } // check if mouse is down or dragging and if so draw if ((!this.isMouseDown && current.type == EventType.MouseDown) || (this.isMouseDown && current.type == EventType.MouseDrag)) { this.isMouseDown = true; // if middle mouse button just exit the user is trying to pan the camera if (current.button == 2) { return; } if (current.type == EventType.MouseDown) { this.drawRecord = new GenericImage <Color>(this.map.Columns, this.map.Rows); this.gameObjectsToRemove.Clear(); } // determine based on mouse button if we are drawing or erasing var previousState = this.isDrawing; this.isDrawing = current.button == 0; this.isDrawing = current.button == 1 ? false : this.isDrawing; if (previousState != this.isDrawing) { this.lastPosition = new Point(-1, -1); } // get settings relating to drawing or erasing var holdShift = settings.GetSetting(this.isDrawing ? GlobalConstants.HoldShiftToDrawKey : GlobalConstants.HoldShiftToEraseKey, false); var holdAlt = settings.GetSetting(this.isDrawing ? GlobalConstants.HoldAltToDrawKey : GlobalConstants.HoldAltToEraseKey, false); var holdControl = settings.GetSetting(this.isDrawing ? GlobalConstants.HoldControlToDrawKey : GlobalConstants.HoldControlToEraseKey, false); // check settings and see of we can draw or erase. A value equal to 0 means it passed the test can occur var canDo = 0; canDo += holdShift && !current.shift ? 1 : 0; canDo += holdAlt && !current.alt ? 1 : 0; canDo += holdControl && !current.control ? 1 : 0; canDo += !holdAlt && current.alt ? 1 : 0; // maintains v1 behavior if (canDo == 0) { if (this.isDrawing) { this.DrawThePrefab(); } else { this.EraseThePrefab(); } current.Use(); } } }
/// <summary> /// Draws a <see cref="GenericImage{T}"/> on to a <see cref="Texture2D"/> texture. /// </summary> /// <param name="texture">A reference to a <see cref="Texture2D"/> type.</param> /// <param name="sourceImage">A reference to the <see cref="GenericImage{T}"/> that will be drawn.</param> /// <param name="x">The x position where the <see cref="sourceImage"/> will be drawn.</param> /// <param name="y">The y position where the <see cref="sourceImage"/> will be drawn.</param> /// <param name="sourceX">The source x position within <see cref="sourceImage"/>.</param> /// <param name="sourceY">The source y position within <see cref="sourceImage"/>.</param> /// <param name="sourceWidth">The source width within the <see cref="sourceImage"/>.</param> /// <param name="sourceHeight">The source height within the <see cref="sourceImage"/>.</param> /// <param name="flipHorizontally">If true will flip the <see cref="sourceImage"/> horizontally before drawing.</param> /// <param name="flipVertically">If true will flip the <see cref="sourceImage"/> vertically before drawing.</param> public static void Draw(this Texture2D texture, GenericImage<Color> sourceImage, int x, int y, int sourceX, int sourceY, int sourceWidth, int sourceHeight, bool flipHorizontally, bool flipVertically) { var textureRectangle = new Rect(0, 0, texture.width, texture.height); var sourceRectangle = new Rect(x, y, sourceWidth, sourceHeight); var intersect = textureRectangle.Intersect(sourceRectangle); if (!intersect.Intersects(new Rect(0, 0, sourceImage.Width, sourceImage.Height))) { return; } var tempImage = new GenericImage<Color>((int)intersect.width, (int)intersect.height); tempImage.Draw(sourceImage, 0, 0, sourceX, sourceY, tempImage.Width, tempImage.Height, (source, blendWith) => blendWith); if (flipHorizontally) { tempImage.FlipHorizontally(); } if (flipVertically) { tempImage.FlipVertically(); } var colors = tempImage.ToUnityColorArray(); texture.SetPixels(x, y, (int)intersect.width, (int)intersect.height, colors); texture.Apply(); }
/// <summary> /// Handles the event for tile selection. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">An System.EventArgs that contains no event data.</param> private void MainPreviewTileSelection(object sender, TileSelectionEventArgs e) { // do nothing until selection has completed or if no texture selected if (e.Status != TileSelectionStatus.Complete || this.materialControls.TextureAsset == null) { return; } var image = new GenericImage<Color>(this.mainPreview.TileWidth, this.mainPreview.TileHeight); image.Draw( this.materialControls.TextureAsset.CreateGenericImage(), 0, 0, e.Min.X, e.Min.Y, this.mainPreview.TileWidth, this.mainPreview.TileHeight, (source, blendWith) => blendWith); this.SelectedTile = image; }