public void Start() { var colors = Pattern.GetPixels(); var bitmap = new Bitmap(Pattern.Width, Pattern.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); for (var y = 0; y < Pattern.Width; y++) { for (var x = 0; x < Pattern.Height; x++) { bitmap.SetPixel(x, y, System.Drawing.Color.FromArgb((byte)(colors[x + y * Pattern.Width].a * 255f), (byte)(colors[x + y * Pattern.Width].r * 255f), (byte)(colors[x + y * Pattern.Width].g * 255f), (byte)(colors[x + y * Pattern.Width].b * 255f))); } } StandaloneFileBrowser.SaveFilePanelAsync("Export image", "", "image.png", new ExtensionFilter[] { new ExtensionFilter("Image", new string[] { "png", "jpg", "jpeg", "bmp", "gif" }) }, (path) => { if (path != null && path.Length > 0) { var format = System.Drawing.Imaging.ImageFormat.Png; if (path.EndsWith(".gif")) { format = System.Drawing.Imaging.ImageFormat.Gif; } else if (path.EndsWith(".jpg") || path.EndsWith(".jpeg")) { format = System.Drawing.Imaging.ImageFormat.Jpeg; } else if (path.EndsWith(".bmp")) { format = System.Drawing.Imaging.ImageFormat.Bmp; } bitmap.Save(path, format); _IsFinished = true; } }); }
public Pattern(PatternEditor editor, DesignPattern pattern) { try { Logger.Log(Logger.Level.INFO, "[PatternEditor/Pattern] Creating new pattern"); StartPreviewThread(); Logger.Log(Logger.Level.DEBUG, "[PatternEditor/Pattern] Preview generator thread started!"); _Type = pattern.Type; Logger.Log(Logger.Level.DEBUG, "[PatternEditor/Pattern] Pattern type: " + _Type.ToString()); Bitmap = new TextureBitmap(pattern.Width, pattern.Height); Logger.Log(Logger.Level.DEBUG, "[PatternEditor/Pattern] Created TextureBitmap " + pattern.Width + "x" + pattern.Height); Bitmap.Clear(); PreviewBitmap = new TextureBitmap(pattern.Width, pattern.Height); Logger.Log(Logger.Level.DEBUG, "[PatternEditor/Pattern] Created preview TextureBitmap " + pattern.Width + "x" + pattern.Height); PreviewBitmap.Clear(); PreviewSprite = UnityEngine.Sprite.Create(PreviewBitmap.Texture, new UnityEngine.Rect(0, 0, PreviewBitmap.Width, PreviewBitmap.Height), new UnityEngine.Vector2(0.5f, 0.5f)); Logger.Log(Logger.Level.DEBUG, "[PatternEditor/Pattern] Created preview sprite"); UpscaledPreviewBitmap = new TextureBitmap(pattern.Width * 4, pattern.Height * 4); Logger.Log(Logger.Level.DEBUG, "[PatternEditor/Pattern] Created upscaled preview TextureBitmap " + (pattern.Width * 4) + "x" + (pattern.Height * 4)); UpscaledPreviewBitmap.Clear(); Quantizer = Quantizers[0]; Logger.Log(Logger.Level.DEBUG, "[PatternEditor/Pattern] Selected Quantizer: " + Quantizer.GetType().ToString()); ColorCache = ColorCaches[0]; Logger.Log(Logger.Level.DEBUG, "[PatternEditor/Pattern] Selected Color Cache: " + ColorCache.GetType().ToString()); Editor = editor; DesignPattern = pattern; var colors = pattern.GetPixels(); Logger.Log(Logger.Level.DEBUG, "[PatternEditor/Pattern] Parsing colors of pattern..."); unsafe { var bitmapColors = Bitmap.GetColors(); for (int y = 0; y < Height; y++) { for (int x = 0; x < Width; x++) { var col = new TextureBitmap.Color( (byte)(colors[x + y * Width].r * 255f), (byte)(colors[x + y * Width].g * 255f), (byte)(colors[x + y * Width].b * 255f), (byte)(colors[x + y * Width].a * 255f) ); *(bitmapColors + x + (Height - 1 - y) * Width) = col; } } } Logger.Log(Logger.Level.DEBUG, "[PatternEditor/Pattern] Parsed " + (Width * Height) + " pixels."); Info = DesignPatternInformation.Types[pattern.Type]; Logger.Log(Logger.Level.DEBUG, "[PatternEditor/Pattern] Pattern information obtained."); } catch (System.Exception e) { Logger.Log(Logger.Level.ERROR, "[PatternEditor/Pattern] Error while creating pattern: " + e.ToString()); } }
public void Clear() { var p = new DesignPattern(); p.Type = this.Type; p.IsPro = this.Type != DesignPattern.TypeEnum.SimplePattern; p.Pixels = new byte[p.Width / 2 * p.Height]; p.Empty(); Colors = p.GetPixels(); Load(); }
public static Sprite GetPreview(this DesignPattern pattern) { int width = pattern.Width; int height = pattern.Height; var texture = new Texture2D(width, height, TextureFormat.RGBA32, false); texture.filterMode = FilterMode.Point; UnityEngine.Color[] pixels = pattern.GetPixels(); UnityEngine.Color[] resultPixels = new UnityEngine.Color[pixels.Length]; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { resultPixels[x + (height - 1 - y) * width] = pixels[x + y * width]; } } var visible = new bool[width * height]; try { var info = DesignPatternInformation.Types[pattern.Type]; foreach (var part in info.Parts) { for (var x = 0; x < part.Width; x++) { for (var y = 0; y < part.Height; y++) { var picX = part.X + x; var picY = part.Y + y; var vis = part.Visible[x + y * part.Width] == '1'; visible[picX + (height - 1 - picY) * width] = vis; } } } } catch (Exception e) { Debug.LogException(e); Debug.LogError("Pattern type " + pattern.Type); } for (var i = 0; i < visible.Length; i++) { resultPixels[i] = visible[i] ? resultPixels[i] : new UnityEngine.Color(0f, 0f, 0f, 0f); } texture.SetPixels(resultPixels); texture.Apply(); return(UnityEngine.Sprite.Create(texture, new Rect(0, 0, pattern.Width, pattern.Height), new Vector2(0.5f, 0.5f))); }
public Pattern(PatternEditor editor, DesignPattern pattern) { _Type = pattern.Type; Result = new Bitmap(1, 1); Quantizer = Quantizers[0]; ColorCache = ColorCaches[0]; Editor = editor; DesignPattern = pattern; Colors = pattern.GetPixels(); Info = DesignPatternInformation.Types[pattern.Type]; UpscaledPreviewTexture = new UnityEngine.Texture2D(Width * 4, Height * 4, UnityEngine.TextureFormat.RGB24, false); PreviewTexture = new UnityEngine.Texture2D(Width, Height, UnityEngine.TextureFormat.ARGB32, false); }
public static UnityEngine.Texture2D GetTexture(this DesignPattern pattern) { int width = pattern.Width; int height = pattern.Height; Texture2D ret = new Texture2D(width, height, UnityEngine.TextureFormat.RGBA32, false); var colors = pattern.GetPixels(); var resultColors = new UnityEngine.Color[colors.Length]; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { resultColors[x + (height - 1 - y) * width] = colors[x + y * width]; } } ret.SetPixels(resultColors); ret.Apply(); return(ret); }
public void Start() { var colors = Pattern.GetPixels(); var bitmap = new TextureBitmap(Pattern.Width, Pattern.Height); for (var y = 0; y < Pattern.Width; y++) { for (var x = 0; x < Pattern.Height; x++) { bitmap.SetPixel(x, y, new TextureBitmap.Color((byte)(colors[x + y * Pattern.Width].a * 255f), (byte)(colors[x + y * Pattern.Width].r * 255f), (byte)(colors[x + y * Pattern.Width].g * 255f), (byte)(colors[x + y * Pattern.Width].b * 255f))); } } var path = StandaloneFileBrowser.SaveFilePanel("Export image", "", "image.png", new ExtensionFilter[] { new ExtensionFilter("Image", new string[] { "png", "jpg", "jpeg", "bmp", "gif" }) }); if (path != null && path.Length > 0) { bitmap.Save(path); _IsFinished = true; } }
public static Sprite GetPreview(this DesignPattern pattern) { int width = 32; int height = 32; var texture = new Texture2D(width, height, TextureFormat.RGBA32, false, true); texture.filterMode = FilterMode.Point; UnityEngine.Color[] pixels = pattern.GetPixels(); UnityEngine.Color[] resultPixels = new UnityEngine.Color[pixels.Length]; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { resultPixels[x + (height - 1 - y) * width] = pixels[x + y * width]; } } texture.SetPixels(resultPixels); texture.Apply(); return(UnityEngine.Sprite.Create(texture, new Rect(0, 0, 32, 32), new Vector2(0.5f, 0.5f))); }