/// <summary> /// Function to create an array of System.Drawing.Images from a given texture. /// </summary> /// <param name="texture">Texture to evaluate.</param> /// <returns>A list of GDI+ images.</returns> public static Image[] CreateGDIImagesFromTexture(GorgonTexture texture) { using (var data = GorgonImageData.CreateFromTexture(texture)) { return(CreateGDIImagesFromImageData(data)); } }
/// <summary> /// Paints a representation of the value of an object using the specified <see cref="T:System.Drawing.Design.PaintValueEventArgs" />. /// </summary> /// <param name="e">A <see cref="T:System.Drawing.Design.PaintValueEventArgs" /> that indicates what to paint and where to paint it.</param> public override void PaintValue(PaintValueEventArgs e) { var sprite = (GorgonSpriteContent)((ContentTypeDescriptor)e.Context.Instance).Content; if (sprite.Texture == null) { e.Graphics.DrawImage(Resources.image_missing_16x16, e.Bounds, new Rectangle(0, 0, Resources.image_missing_16x16.Width, Resources.image_missing_16x16.Height), GraphicsUnit.Pixel); return; } // We cannot convert block compressed images to GDI+ image types, so just put a generic icon up. if ((sprite.Texture.FormatInformation.IsCompressed) || (!GorgonImageData.CanConvert(sprite.Texture.Settings.Format, BufferFormat.R8G8B8A8_UIntNormal))) { e.Graphics.DrawImage(Resources.open_image_16x16, e.Bounds, new Rectangle(0, 0, Resources.open_image_16x16.Width, Resources.open_image_16x16.Height), GraphicsUnit.Pixel); return; } // Copy the image into system memory, convert it and display it. using (GorgonImageData imageData = GorgonImageData.CreateFromTexture(sprite.Texture)) { imageData.Resize(e.Bounds.Width, e.Bounds.Height, false, ImageFilter.Fant); imageData.ConvertFormat(BufferFormat.R8G8B8A8_UIntNormal); using (Image gdiImage = imageData.Buffers[0].ToGDIImage()) { e.Graphics.DrawImage(gdiImage, e.Bounds); } } }
/// <summary> /// Function to clip the sprite region from the texture. /// </summary> /// <param name="texture">Texture containing the image data to clip.</param> /// <returns>A rectangle for the sprite. Or an empty rectangle if no sprite was selected.</returns> public unsafe Rectangle Clip(GorgonTexture2D texture) { GorgonImageData imageData = null; try { // Constrain our mouse to the texture. _startPoint.X = _startPoint.X.Max(0).Min(texture.Settings.Width - 1); _startPoint.Y = _startPoint.Y.Max(0).Min(texture.Settings.Height - 1); imageData = GorgonImageData.CreateFromTexture(texture); _stride = imageData.Buffers[0].PitchInformation.RowPitch; _format = imageData.Settings.Format; _bytesPerPixel = GorgonBufferFormatInfo.GetInfo(_format).SizeInBytes; _textureWidth = imageData.Settings.Width; // Get a pointer to the buffer. byte *bytePtr = (byte *)imageData.UnsafePointer; if (IsMaskValue(bytePtr, _startPoint.X, _startPoint.Y)) { return(Rectangle.Empty); } Queue <ClipSpan> clipSpans = new Queue <ClipSpan>(); _pixels = new bool[imageData.Settings.Width * imageData.Settings.Height]; int left = _startPoint.X; int top = _startPoint.Y; int right = left; int bottom = top; // Get the initial span from our starting point and add it to our queue. ClipSpan span = GetSpan(bytePtr, left, top); clipSpans.Enqueue(span); while (clipSpans.Count > 0) { // Take the span off the queue. span = clipSpans.Dequeue(); // Find the next vertical span above and below the current span. int west = span.Start; int east = span.End; int north = span.Y - 1; int south = span.Y + 1; // Check each pixel between the start and end of the upper and lower spans. for (int x = west; x <= east; ++x) { int pixelindex = _textureWidth * north + x; if ((span.Y > 0) && (!IsMaskValue(bytePtr, x, north)) && (!_pixels[pixelindex])) { clipSpans.Enqueue(GetSpan(bytePtr, x, north)); } pixelindex = _textureWidth * south + x; if ((span.Y >= imageData.Settings.Height - 1) || (IsMaskValue(bytePtr, x, south)) || (_pixels[pixelindex])) { continue; } clipSpans.Enqueue(GetSpan(bytePtr, x, south)); } // Update the boundaries. left = west.Min(left); right = (east + 1).Max(right); top = (north + 1).Min(top); bottom = south.Max(bottom); } return(Rectangle.FromLTRB(left, top, right, bottom)); } finally { if (imageData != null) { imageData.Dispose(); } } }