Beispiel #1
0
    public SubPattern(Pattern pattern, DesignPatternInformation.DesignPatternPart part, bool import = false)
    {
        Bitmap = new TextureBitmap(part.Width, part.Height);
        Bitmap.Clear();
        Bitmap.Texture.filterMode = UnityEngine.FilterMode.Point;
        Pattern = pattern;
        Part    = part;
        Layers  = new List <Layer>();
        History = new History(this, 50);
        History.OnHistoryChanged = () => {
            Pattern.Editor.Tools.HistoryChanged(History);
        };
        if (!import)
        {
            var backgroundLayer = new RasterLayer(this, "Background");

            unsafe
            {
                var colors      = pattern.Bitmap.GetColors();
                var layerColors = backgroundLayer.Texture.GetColors();
                for (int y = 0; y < Height; y++)
                {
                    for (int x = 0; x < Width; x++)
                    {
                        layerColors[x + (Height - 1 - y) * Width] = colors[Part.X + x + (pattern.Height - 1 - (Part.Y + y)) * pattern.Width];
                    }
                }
                //backgroundLayer.Texture.SetPixel(x, y, pattern.Bitmap.GetPixel(Part.X + x, (pattern.Height - 1 - Part.Y) + y));
            }
            Layers.Add(backgroundLayer);
            History.AddEvent(new History.LayerCreatedAction("Opened", 0, backgroundLayer));
        }
    }
        private static void CalculateLuminanceValues32BitWithAlpha(TextureBitmap bitmap, byte[] luminances)
        {
            var height     = bitmap.Height;
            var width      = bitmap.Width;
            var pixelWidth = bitmap.PixelSize;
            var maxIndex   = 4 * width;

            unsafe
            {
                var ptr = bitmap.GetColors();

                for (int y = 0; y < height; y++)
                {
                    var offset = y * width;
                    for (int x = 0; x < width; x++)
                    {
                        var col       = *(ptr + x + (height - 1 - y) * width);
                        var luminance = (byte)((BChannelWeight * (col).B +
                                                GChannelWeight * (col).G +
                                                RChannelWeight * (col).R) >> ChannelWeight);

                        var alpha = col.A;
                        luminance = (byte)(((luminance * alpha) >> 8) + (255 * (255 - alpha) >> 8) + 1);
                        luminances[x + y * width] = luminance;
                    }
                }
            }
        }
Beispiel #3
0
        public int LoadBoxTexture(string filepath, TextureUnit tex)
        {
            int        mTexture_ID;
            Bitmap     TextureBitmap;
            BitmapData TextureData;

            if (System.IO.File.Exists(filepath))
            {
                TextureBitmap = new Bitmap(filepath);
                TextureData   = TextureBitmap.LockBits(new System.Drawing.Rectangle(0, 0, TextureBitmap.Width,
                                                                                    TextureBitmap.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
            }
            else
            {
                throw new Exception("Could not find file " + filepath);
            }

            GL.ActiveTexture(tex);
            GL.GenTextures(1, out mTexture_ID);
            GL.BindTexture(TextureTarget.Texture2D, mTexture_ID);

            GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, TextureData.Width, TextureData.Height, 0,
                          OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, TextureData.Scan0);

            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);

            TextureBitmap.UnlockBits(TextureData);
            return(mTexture_ID);
        }
    public static TextureBitmap GetBitmap(this DesignPattern pattern)
    {
        int width  = pattern.Width;
        int height = pattern.Height;

        var bitmap = new TextureBitmap(width, height);

        unsafe
        {
            TextureBitmap.Color *ptr = bitmap.GetColors();

            for (var y = 0; y < height; y++)
            {
                for (var x = 0; x < width; x++)
                {
                    var b = pattern.GetPixel(x, y);
                    if (b == 15)
                    {
                        *(ptr + x + (height - 1 - y) * width) = new TextureBitmap.Color(0, 0, 0, 0);
                    }
                    else
                    {
                        var c = pattern.Palette[b];
                        *(ptr + x + (height - 1 - y) * width) = new TextureBitmap.Color(c.R, c.G, c.B, 255);
                    }
                }
            }
        }
        return(bitmap);
    }
Beispiel #5
0
        /// <summary>
        /// See <see cref="BaseColorQuantizer.OnPrepare"/> for more details.
        /// </summary>
        protected override void OnPrepare(TextureBitmap image)
        {
            base.OnPrepare(image);

            palette  = new List <TextureBitmap.Color>();
            colorMap = new ConcurrentDictionary <Int32, PopularityColorSlot>();
        }
        protected static void CalculateLuminanceValues(TextureBitmap bitmap, byte[] luminances)
        {
            var height = bitmap.Height;
            var width  = bitmap.Width;

            CalculateLuminanceValues32BitWithAlpha(bitmap, luminances);
        }
Beispiel #7
0
    public void ShowPattern(DesignServer.Pattern pattern, PatternExchange exchange)
    {
        try
        {
            PatternExchange = exchange;
            Pattern         = pattern;
            if (CurrentTexture != null)
            {
                CurrentTexture.Dispose();
                CurrentTexture = null;
            }

            ACNHFileFormat format = new ACNHFileFormat(pattern.Bytes);
            DesignPattern  patt   = null;
            if (format.IsPro)
            {
                patt = new ProDesignPattern();
                patt.CopyFrom(format);
            }
            else
            {
                patt = new SimpleDesignPattern();
                patt.CopyFrom(format);
            }

            CurrentTexture = patt.GetBitmap();
            CurrentTexture.Apply();
            CurrentTexture.Texture.filterMode = FilterMode.Point;
            CurrentTexture.Texture.wrapMode   = TextureWrapMode.Clamp;
            this.Image.texture = CurrentTexture.Texture;
        }
        catch (System.Exception e)
        {
        }
    }
Beispiel #8
0
    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 CopyFrom(TextureBitmap bitmap)
 {
     if (Disposed)
     {
         return;
     }
     this.Resize(bitmap.Width, bitmap.Height);
     Buffer.MemoryCopy(bitmap.Bytes.ToPointer(), this.Bytes.ToPointer(), this.Width * this.Height * PixelSize, this.Width * this.Height * PixelSize);
 }
 public Layer(SubPattern pattern, string name = "")
 {
     SubPattern = pattern;
     Name       = name;
     _Width     = pattern.Width;
     _Height    = pattern.Height;
     Texture    = new TextureBitmap(_Width, _Height);
     Texture.Clear();
     Sprite = Sprite.Create(Texture.Texture, new Rect(0, 0, Texture.Width, Texture.Height), new Vector2(0.5f, 0.5f));
 }
Beispiel #11
0
 public SmartObjectLayer(SubPattern pattern, string name, TextureBitmap bitmap, int x, int y, int width, int height) : base(pattern, name)
 {
     Crop          = TextureBitmap.CropMode.Scale;
     Resampler     = ResamplingFilters.Box;
     Bitmap        = bitmap;
     _ObjectX      = x;
     _ObjectY      = y;
     _ObjectWidth  = width;
     _ObjectHeight = height;
 }
Beispiel #12
0
        public void OnShow()
        {
            var mapId  = WorldFrame.Instance.MapManager.MapId;
            var mapRow = Storage.DbcStorage.Map.GetRowById(mapId);

            if (mapRow == null)
            {
                WorldFrame.Instance.State = AppState.MapSelect;
                return;
            }

            var loadScreenPath = "Interface\\Glues\\loading.blp";
            var loadEntry      = mapRow.GetInt32(Storage.MapFormatGuess.FieldMapLoadingScreen);

            if (loadEntry != 0)
            {
                var loadRow = Storage.DbcStorage.LoadingScreen.GetRowById(loadEntry);
                if (loadRow != null)
                {
                    var path = loadRow.GetString(Storage.MapFormatGuess.FieldLoadingScreenPath);
                    mWidescreen = false;

                    if (string.IsNullOrEmpty(path) == false)
                    {
                        if (Storage.MapFormatGuess.FieldLoadingScreenHasWidescreen >= 0 && loadRow.GetInt32(Storage.MapFormatGuess.FieldLoadingScreenHasWidescreen) == 1)
                        {
                            var widePath = path.ToUpperInvariant().Replace(".BLP", "WIDE.BLP");
                            if (IO.FileManager.Instance.Provider.Exists(widePath))
                            {
                                path        = widePath;
                                mWidescreen = true;
                            }
                        }

                        loadScreenPath = path;
                    }
                }
            }

            if (mLoadingImage != null)
            {
                mLoadingImage.Dispose();
            }
            mLoadingImage = new TextureBitmap();
            mLoadingImage.LoadComplete += ImageLoaded;
            mLoadingImage.LoadFromFile(loadScreenPath);

            if (mLoadingBarBackground == null)
            {
                mLoadingBarBackground = new TextureBitmap();
                mLoadingBarBackground.LoadFromFile(@"Interface\Glues\LoadingBar\Loading-BarBorder.blp");
                mLoadingBarFill = new TextureBitmap();
                mLoadingBarFill.LoadFromFile(@"Interface\Glues\LoadingBar\Loading-BarFill.blp");
            }
        }
    private void ImportImage()
    {
        Controller.Instance.Popup.SetText("Please select any <#FF6666>Image<#FFFFFF> file to import.", false, () => {
            StandaloneFileBrowser.OpenFilePanelAsync("Import image", "", new ExtensionFilter[] { new ExtensionFilter("Image", new string[] { "png", "jpg", "jpeg", "bmp", "gif" }) }, false, (path) =>
            {
                if (path.Length > 0)
                {
                    try
                    {
                        int x        = 0;
                        int y        = 0;
                        int width    = Editor.CurrentPattern.CurrentSubPattern.Width;
                        int height   = Editor.CurrentPattern.CurrentSubPattern.Height;
                        var bmp      = TextureBitmap.Load(path[0]);
                        var newLayer = new SmartObjectLayer(Editor.CurrentPattern.CurrentSubPattern, System.IO.Path.GetFileNameWithoutExtension(path[0]), bmp, x, y, width, height);
                        newLayer.UpdateColors();
                        var index = Editor.CurrentPattern.CurrentSubPattern.Layers.IndexOf(Editor.CurrentPattern.CurrentSubPattern.SelectedLayer) + 1;
                        Editor.CurrentPattern.CurrentSubPattern.Layers.Insert(index, newLayer);
                        Editor.CurrentPattern.CurrentSubPattern.SelectedLayer = newLayer;
                        Editor.LayersChanged();
                        Editor.CurrentPattern.CurrentSubPattern.SelectLayer(newLayer);
                        Editor.CurrentPattern.CurrentSubPattern.UpdateImage();

                        Editor.CurrentPattern.CurrentSubPattern.History.AddEvent(new History.LayerCreatedAction("Created: " + newLayer.Name, index, newLayer));

                        Controller.Instance.Popup.Close();
                    }
                    catch (System.IO.FileLoadException e)
                    {
                        Debug.LogException(e);
                        Controller.Instance.Popup.SetText("Failed to load the file. File error.", false, () =>
                        {
                            return(true);
                        });
                        return;
                    }
                    catch (System.Exception e)
                    {
                        Debug.LogException(e);
                        Controller.Instance.Popup.SetText("Invalid image file.", false, () =>
                        {
                            return(true);
                        });
                        return;
                    }
                }
                else
                {
                    Controller.Instance.Popup.Close();
                }
            });
            return(false);
        });
    }
    public TextureBitmap Clone()
    {
        if (Disposed)
        {
            return(null);
        }
        var ret = new TextureBitmap(this.Width, this.Height);

        Buffer.MemoryCopy(this.Bytes.ToPointer(), ret.Bytes.ToPointer(), this.Width * this.Height * PixelSize, this.Width * this.Height * PixelSize);
        return(ret);
    }
    public static void FromBitmap(this DesignPattern pattern, TextureBitmap bitmap)
    {
        Dictionary <TextureBitmap.Color, byte> colorMap = new Dictionary <TextureBitmap.Color, byte>();

        for (int i = 0; i < 15; i++)
        {
            pattern.Palette[i].R = 0;
            pattern.Palette[i].G = 0;
            pattern.Palette[i].B = 0;
        }

        int width  = pattern.Width;
        int height = pattern.Height;

        unsafe
        {
            var colors = bitmap.GetColors();
            pattern.Image = new byte[width * height];
            int bitmapHeight = bitmap.Height;
            int bitmapWidth  = bitmap.Width;
            for (var y = 0; y < width; y++)
            {
                for (var x = 0; x < height; x++)
                {
                    var pixelColor = new TextureBitmap.Color(0, 0, 0, 0);
                    if (x < bitmapWidth && y < bitmapHeight)
                    {
                        pixelColor = colors[x + (bitmapHeight - 1 - y) * bitmapHeight];
                    }

                    byte index = 0xF;
                    if (pixelColor.A == 255)
                    {
                        if (colorMap.ContainsKey(pixelColor))
                        {
                            index = colorMap[pixelColor];
                        }
                        else
                        {
                            index = (byte)colorMap.Count;
                            pattern.Palette[index].R = pixelColor.R;
                            pattern.Palette[index].G = pixelColor.G;
                            pattern.Palette[index].B = pixelColor.B;
                            colorMap.Add(pixelColor, index);
                        }
                    }

                    pattern.SetPixel(x, y, index);
                }
            }
        }
    }
Beispiel #16
0
        public Snake(TextureBitmap playground)
        {
            CurrentSnakeColor = new TextureBitmap.Color(0, 0, 0, 255);

            Original   = playground.Clone();
            Playground = playground;
            Width      = playground.Width;
            Height     = playground.Height;
            int startX = Width / 2;
            int startY = Height / 2;

            for (int i = 0; i < 3; i++)
            {
                Points.Add(new TextureBitmap.Point(startX, startY + 3 - i));
            }
            SpawnFood();
            CurrentDirection = Direction.UP;
            LastDirection    = Direction.UP;
        }
    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;
        }
    }
        /// <summary>
        /// See <see cref="BaseColorQuantizer.OnPrepare"/> for more details.
        /// </summary>
        protected override void OnPrepare(TextureBitmap image)
        {
            // creates all the cubes
            cubes = new WuColorCube[MaxColor];

            // initializes all the cubes
            for (Int32 cubeIndex = 0; cubeIndex < MaxColor; cubeIndex++)
            {
                cubes[cubeIndex] = new WuColorCube();
            }

            // resets the reference minimums
            cubes[0].RedMinimum   = 0;
            cubes[0].GreenMinimum = 0;
            cubes[0].BlueMinimum  = 0;

            // resets the reference maximums
            cubes[0].RedMaximum   = MaxSideIndex;
            cubes[0].GreenMaximum = MaxSideIndex;
            cubes[0].BlueMaximum  = MaxSideIndex;

            weights      = new Int64[SideSize * SideSize * SideSize];
            momentsRed   = new Int64[SideSize * SideSize * SideSize];
            momentsGreen = new Int64[SideSize * SideSize * SideSize];
            momentsBlue  = new Int64[SideSize * SideSize * SideSize];
            moments      = new Single[SideSize * SideSize * SideSize];

            table = new Int32[256];

            for (Int32 tableIndex = 0; tableIndex < 256; ++tableIndex)
            {
                table[tableIndex] = tableIndex * tableIndex;
            }

            pixelIndex = 0;
            imageWidth = image.Width;
            imageSize  = image.Width * image.Height;

            quantizedPixels = new Int32[imageSize];
            pixels          = new Int32[imageSize];
        }
Beispiel #19
0
 public LayerCreatedAction(string name, int layerIndex, Layer layer) : base(name)
 {
     this.LayerIndex = layerIndex;
     this.LayerName  = layer.Name;
     if (layer is SmartObjectLayer sol)
     {
         Bitmap       = sol.Bitmap.Clone();
         ObjectX      = sol.ObjectX;
         ObjectY      = sol.ObjectY;
         ObjectWidth  = sol.ObjectWidth;
         ObjectHeight = sol.ObjectHeight;
     }
     else
     {
         Colors = Marshal.AllocHGlobal(layer.Texture.Width * layer.Texture.Height * layer.Texture.PixelSize);
         unsafe
         {
             Buffer.MemoryCopy(layer.Texture.Bytes.ToPointer(), Colors.ToPointer(), layer.Texture.Width * layer.Texture.Height * 4, layer.Texture.Width * layer.Texture.Height * layer.Texture.PixelSize);
         }
     }
 }
    public void Hide()
    {
        Opened   = false;
        FullOpen = true;
        ContinuePop.PopOut();
        BackPop.PopOut();
        ImportPop.PopOut();
        Cancel        = null;
        ConfirmImport = null;
        Confirm       = null;

        if (CurrentPreview != null)
        {
            CurrentPreview.Dispose();
            CurrentPreview = null;
        }
        if (CurrentUpscaledPreview != null)
        {
            CurrentUpscaledPreview.Dispose();
            CurrentUpscaledPreview = null;
        }
    }
Beispiel #21
0
        private void ImageLoaded(TextureBitmap bmp)
        {
            var facx      = mSize.X / bmp.Width;
            var facy      = mSize.Y / bmp.Height;
            var fac       = Math.Min(facx, facy);
            var newHeight = fac * bmp.Height;
            var ofsy      = (mSize.Y - newHeight) / 2.0f;

            var newWidth = fac * bmp.Width;

            if (mWidescreen)
            {
                newWidth *= (16.0f / 9.0f);
            }
            else
            {
                newWidth *= (4.0f / 3.0f);
            }

            var ofsx = (mSize.X - newWidth) / 2.0f;

            mTargetRectangle = new RectangleF(ofsx, ofsy, newWidth, newHeight);
        }
    public static TextureBitmap Load(byte[] bytes, bool createBackgroundTexture = true)
    {
        var bitmap = SkiaSharp.SKBitmap.Decode(bytes);

        if (ColorTransformers.ContainsKey(bitmap.ColorType))
        {
            var result    = new TextureBitmap(bitmap.Width, bitmap.Height, createBackgroundTexture);
            var reader    = ColorTransformers[bitmap.ColorType];
            int pixelSize = ColorSize[bitmap.ColorType];

            var pixels = bitmap.GetPixels();
            result.InternalResize(bitmap.Width, bitmap.Height);
            ColorTransformers[bitmap.ColorType](pixels, result.Bytes, result.Width, result.Height);
            result.FlipY();
            bitmap.Dispose();
            result.Apply();
            return(result);
        }
        else
        {
            bitmap.Dispose();
            throw new ArgumentException("Image have an unsupported color type: " + bitmap.ColorType);
        }
    }
    public void Subtract(TextureBitmap other, Color tint, Point point = null, Rectangle otherRect = null, Func <int, int, bool> isDrawable = null)
    {
        if (Disposed)
        {
            return;
        }
        int myWidth        = Width;
        int myHeight       = Height;
        int myPixelSize    = PixelSize;
        int otherWidth     = other.Width;
        int otherHeight    = other.Height;
        int otherPixelSize = other.PixelSize;

        float tintR = tint.R / 255f;
        float tintG = tint.G / 255f;
        float tintB = tint.B / 255f;
        float tintA = tint.A / 255f;

        byte *myPointer    = (byte *)Bytes.ToPointer();
        byte *otherPointer = (byte *)other.Bytes.ToPointer();

        if (point == null)
        {
            point = new Point(0, 0);
        }
        if (otherRect == null)
        {
            otherRect = new Rectangle(0, 0, otherWidth, otherHeight);
        }
        int w = point.X + otherRect.Width;
        int h = point.Y + otherRect.Height;

        if (w > Width)
        {
            w = Width;
        }
        if (h > Height)
        {
            h = Height;
        }
        int ox = otherRect.X;
        int oy = otherRect.Y;

        for (int py = point.Y; py < h; py++)
        {
            ox = otherRect.X;
            for (int px = point.X; px < w; px++)
            {
                if (px >= 0 && px < myWidth && py >= 0 && py < myHeight)
                {
                    if (isDrawable != null && !isDrawable(px, py))
                    {
                        ox++;
                        continue;
                    }
                    int myIndex    = (px + (myHeight - 1 - py) * myWidth) * myPixelSize;
                    int otherIndex = (ox + (otherHeight - 1 - oy) * otherWidth) * otherPixelSize;

                    *(myPointer + myIndex + 0) = (byte)UnityEngine.Mathf.Clamp((int)(*(myPointer + myIndex + 0)) - (int)(*(otherPointer + otherIndex + 0) * tintR), 0, 255);
                    *(myPointer + myIndex + 1) = (byte)UnityEngine.Mathf.Clamp((int)(*(myPointer + myIndex + 1)) - (int)(*(otherPointer + otherIndex + 1) * tintG), 0, 255);
                    *(myPointer + myIndex + 2) = (byte)UnityEngine.Mathf.Clamp((int)(*(myPointer + myIndex + 2)) - (int)(*(otherPointer + otherIndex + 2) * tintB), 0, 255);
                    *(myPointer + myIndex + 3) = (byte)UnityEngine.Mathf.Clamp((int)(*(myPointer + myIndex + 3)) - (int)(*(otherPointer + otherIndex + 3) * tintA), 0, 255);
                }
                ox++;
            }
            oy++;
        }
    }
Beispiel #24
0
        private void ImageLoaded(TextureBitmap bmp)
        {
            var facx = mSize.X / bmp.Width;
            var facy = mSize.Y / bmp.Height;
            var fac = Math.Min(facx, facy);
            var newHeight = fac * bmp.Height;
            var ofsy = (mSize.Y - newHeight) / 2.0f;

            var newWidth = fac * bmp.Width;
            if (mWidescreen) newWidth *= ( 16.0f / 9.0f );
            else newWidth *= ( 4.0f / 3.0f );
	
            var ofsx = (mSize.X - newWidth) / 2.0f;
            mTargetRectangle = new RectangleF(ofsx, ofsy, newWidth, newHeight);
        }
    public static (int, int, int, int) FindPattern(TextureBitmap bitmap, DesignPattern.TypeEnum type)
    {
        bool isPro = type != DesignPattern.TypeEnum.SimplePattern;

        unsafe
        {
            var    colors       = bitmap.GetColors();
            int    bitmapWidth  = bitmap.Width;
            int    bitmapHeight = bitmap.Height;
            bool[] bw           = new bool[bitmapWidth * bitmapHeight];   // true = black
            int    min          = 180;
            int    max          = 235;
            for (int x = 0; x < bitmapWidth; x++)
            {
                for (int y = 0; y < bitmapHeight; y++)
                {
                    float h;
                    float s;
                    float v;
                    var   col = colors[x + y * bitmapWidth];
                    int   r   = (int)(col.R);
                    int   g   = (int)(col.G);
                    int   b   = (int)(col.B);
                    r = r * (r >= min && r <= max ? 0 : 1);
                    g = g * (g >= min && g <= max ? 0 : 1);
                    b = b * (b >= min && b <= max ? 0 : 1);
                    bw[x + y * bitmapWidth] = r == 0 && g == 0;
                }
            }

            int widthThreshold = 5;
            int minWidth       = 2;
            List <(int, int, int)> horizontalLines = new List <(int, int, int)>();
            List <(int, int, int)> verticalLines   = new List <(int, int, int)>();

            for (int y = 0; y < bitmapHeight; y++)
            {
                int  currentDashWidth  = 0;
                int  currentSpaceWidth = 0;
                int  dashes            = 0;
                int  dashWidth         = 0;
                int  start             = 0;
                bool black             = false;

                for (int x = 0; x < bitmapWidth; x++)
                {
                    int idx = x + y * bitmapWidth;
                    if (bw[idx])
                    {
                        if (!black)
                        {
                            currentDashWidth = 0;
                            if (dashes > 0 && currentSpaceWidth > dashWidth)
                            {
                                dashes = 0; dashWidth = 0;
                            }
                            if (dashes == 0)
                            {
                                start = x;
                            }
                        }
                        black = true;
                        currentDashWidth++;
                    }
                    else
                    {
                        if (dashes == 0 && currentDashWidth >= minWidth)
                        {
                            dashWidth = currentDashWidth;
                        }

                        if (dashWidth > 0)
                        {
                            if (black)
                            {
                                currentSpaceWidth = 0;
                                if (currentDashWidth < dashWidth - widthThreshold || currentDashWidth < minWidth || currentDashWidth > dashWidth + widthThreshold)
                                {
                                    dashes = 0; dashWidth = 0;
                                }
                                else
                                {
                                    dashes++;
                                    if (dashes >= 14)
                                    {
                                        horizontalLines.Add((start, y, x));
                                        dashWidth = 0;
                                        dashes    = 0;
                                        continue;
                                    }
                                }
                            }
                            if (dashes > 0)
                            {
                                currentSpaceWidth++;
                            }
                        }
                        black = false;
                    }
                }
            }

            for (int x = 0; x < bitmapWidth; x++)
            {
                int  currentDashWidth  = 0;
                int  currentSpaceWidth = 0;
                int  dashes            = 0;
                int  dashWidth         = 0;
                int  start             = 0;
                bool black             = false;

                for (int y = 0; y < bitmapHeight; y++)
                {
                    int idx = x + y * bitmapWidth;
                    if (bw[idx])
                    {
                        if (!black)
                        {
                            currentDashWidth = 0;
                            if (dashes > 0 && currentSpaceWidth > dashWidth)
                            {
                                dashes = 0; dashWidth = 0;
                            }
                            if (dashes == 0)
                            {
                                start = y;
                            }
                        }
                        black = true;
                        currentDashWidth++;
                    }
                    else
                    {
                        if (dashes == 0 && currentDashWidth >= minWidth)
                        {
                            dashWidth = currentDashWidth;
                        }

                        if (dashWidth > 0)
                        {
                            if (black)
                            {
                                currentSpaceWidth = 0;
                                if (currentDashWidth < dashWidth - widthThreshold || currentDashWidth < minWidth || currentDashWidth > dashWidth + widthThreshold)
                                {
                                    dashes = 0; dashWidth = 0;
                                }
                                else
                                {
                                    dashes++;
                                    if (dashes >= 14)
                                    {
                                        verticalLines.Add((x, start, y));
                                        dashWidth = 0;
                                        dashes    = 0;
                                        continue;
                                    }
                                }
                            }
                            if (dashes > 0)
                            {
                                currentSpaceWidth++;
                            }
                        }
                        black = false;
                    }
                }
            }

            int sizeThreshold = 10;

            // find pairing horizontal lines
            List <List <int> > horizontalGroups = new List <List <int> >();
            List <int>         foundHorizontal  = new List <int>();
            for (int i = 0; i < horizontalLines.Count - 1; i++)
            {
                if (!foundHorizontal.Contains(i))
                {
                    List <int> group = new List <int>();
                    group.Add(i);
                    for (int j = i + 1; j < horizontalLines.Count; j++)
                    {
                        if (UnityEngine.Mathf.Abs(horizontalLines[i].Item1 - horizontalLines[j].Item1) < sizeThreshold &&
                            UnityEngine.Mathf.Abs(horizontalLines[i].Item3 - horizontalLines[j].Item3) < sizeThreshold)
                        {
                            group.Add(j);
                            foundHorizontal.Add(j);
                        }
                    }
                    horizontalGroups.Add(group);
                }
            }

            // find pairing vertical lines
            List <List <int> > verticalGroups = new List <List <int> >();
            List <int>         foundVertical  = new List <int>();
            for (int i = 0; i < verticalLines.Count - 1; i++)
            {
                if (!foundVertical.Contains(i))
                {
                    List <int> group = new List <int>();
                    group.Add(i);
                    for (int j = i + 1; j < verticalLines.Count; j++)
                    {
                        if (UnityEngine.Mathf.Abs(verticalLines[i].Item2 - verticalLines[j].Item2) < sizeThreshold &&
                            UnityEngine.Mathf.Abs(verticalLines[i].Item3 - verticalLines[j].Item3) < sizeThreshold)
                        {
                            group.Add(j);
                            foundVertical.Add(j);
                        }
                    }
                    verticalGroups.Add(group);
                }
            }

            (int, int)whiteR = (235, 255);
            (int, int)whiteG = (235, 255);
            (int, int)whiteB = (200, 255);
            List <(int, int, int, int, bool[])> rects = new List <(int, int, int, int, bool[])>();
            for (int i = 0; i < verticalGroups.Count; i++)
            {
                var vTop    = verticalLines[verticalGroups[i][0]].Item2;
                var vBottom = verticalLines[verticalGroups[i][0]].Item3;
                var vX      = verticalLines[verticalGroups[i][0]].Item1;

                for (int j = 0; j < horizontalGroups.Count; j++)
                {
                    var hLeft  = horizontalLines[horizontalGroups[j][0]].Item1;
                    var hRight = horizontalLines[horizontalGroups[j][0]].Item3;
                    var hY     = horizontalLines[horizontalGroups[j][0]].Item2;

                    UnityEngine.Debug.Log(vTop + "-" + vBottom + "(" + vX + ") " + hLeft + "-" + hRight + "(" + hY + ")");
                    if ((UnityEngine.Mathf.Abs(hY - vTop) < sizeThreshold ||
                         UnityEngine.Mathf.Abs(hY - vBottom) < sizeThreshold) &&
                        (UnityEngine.Mathf.Abs(vX - hLeft) < sizeThreshold ||
                         UnityEngine.Mathf.Abs(vX - hRight) < sizeThreshold))
                    {
                        // its a rect!
                        int    w    = hRight - hLeft;
                        int    h    = vBottom - vTop;
                        bool[] rect = new bool[w * h];

                        // find outer top
                        int outerTop = -1;
                        for (int y = -30; y < 0; y++)
                        {
                            int r = (int)(colors[(hLeft + w / 2) + (vTop + y) * bitmapWidth].R);
                            int g = (int)(colors[(hLeft + w / 2) + (vTop + y) * bitmapWidth].G);
                            int b = (int)(colors[(hLeft + w / 2) + (vTop + y) * bitmapWidth].B);
                            if (r >= 240 && g >= 240 && b >= 220)
                            {
                                outerTop = vTop + y;
                                break;
                            }
                        }
                        // find outer left
                        int outerLeft = -1;
                        for (int x = -30; x < 0; x++)
                        {
                            int r = (int)(colors[(hLeft + x) + (vTop + h / 2) * bitmapWidth].R);
                            int g = (int)(colors[(hLeft + x) + (vTop + h / 2) * bitmapWidth].G);
                            int b = (int)(colors[(hLeft + x) + (vTop + h / 2) * bitmapWidth].B);
                            if (r >= 240 && g >= 240 && b >= 220)
                            {
                                outerLeft = hLeft + x;
                                break;
                            }
                        }
                        // find outer bottom
                        int outerBottom = -1;
                        for (int y = 29; y >= 0; y--)
                        {
                            int r = (int)(colors[(hLeft + w / 2) + (vBottom + y) * bitmapWidth].R);
                            int g = (int)(colors[(hLeft + w / 2) + (vBottom + y) * bitmapWidth].G);
                            int b = (int)(colors[(hLeft + w / 2) + (vBottom + y) * bitmapWidth].B);
                            if (r >= 240 && g >= 240 && b >= 220)
                            {
                                outerBottom = vBottom + y + 1;
                                break;
                            }
                        }
                        // find outer right
                        int outerRight = -1;
                        for (int x = 29; x >= 0; x--)
                        {
                            int r = (int)(colors[(hRight + x) + (vTop + h / 2) * bitmapWidth].R);
                            int g = (int)(colors[(hRight + x) + (vTop + h / 2) * bitmapWidth].G);
                            int b = (int)(colors[(hRight + x) + (vTop + h / 2) * bitmapWidth].B);
                            if (r >= 240 && g >= 240 && b >= 220)
                            {
                                outerRight = hRight + x + 1;
                                break;
                            }
                        }

                        UnityEngine.Debug.LogError(outerLeft + "," + outerTop + " (" + (outerBottom - outerTop) + "x" + (outerRight - outerLeft) + ")");
                        if (outerRight == -1 || outerLeft == -1 || outerTop == -1 || outerBottom == -1)
                        {
                            return(-1, -1, -1, -1);
                        }

                        int padding = UnityEngine.Mathf.CeilToInt(((float)(outerRight - outerLeft)) * 0.090f);

                        int realLeft   = outerLeft + padding;
                        int realRight  = outerRight - (padding - 1);
                        int realTop    = outerTop + padding;
                        int realBottom = outerBottom - (padding - 1);

                        return(realLeft, realTop, realRight - realLeft, realBottom - realTop);
                    }
                }
            }
        }

        return(-1, -1, -1, -1);
    }
Beispiel #26
0
        public void OnShow()
        {
            var mapId = WorldFrame.Instance.MapManager.MapId;
            var mapRow = Storage.DbcStorage.Map.GetRowById(mapId);
            if(mapRow == null)
            {
                WorldFrame.Instance.State = AppState.MapSelect;
                return;
            }

            var loadScreenPath = "Interface\\Glues\\loading.blp";
            var loadEntry = mapRow.GetInt32(Storage.MapFormatGuess.FieldMapLoadingScreen);
            if(loadEntry != 0)
            {
                var loadRow = Storage.DbcStorage.LoadingScreen.GetRowById(loadEntry);
                if(loadRow != null)
                {
                    var path = loadRow.GetString(Storage.MapFormatGuess.FieldLoadingScreenPath);
                    mWidescreen = false;

                    if (string.IsNullOrEmpty(path) == false)
                    {
                        if (Storage.MapFormatGuess.FieldLoadingScreenHasWidescreen >= 0 && loadRow.GetInt32(Storage.MapFormatGuess.FieldLoadingScreenHasWidescreen) == 1)
                        {
                            var widePath = path.ToUpperInvariant().Replace(".BLP", "WIDE.BLP");
                            if (IO.FileManager.Instance.Provider.Exists(widePath))
                            {
                                path = widePath;
                                mWidescreen = true;
                            }
                        }

                        loadScreenPath = path;
                    }
                }
            }

            if (mLoadingImage != null)
                mLoadingImage.Dispose();
            mLoadingImage = new TextureBitmap();
            mLoadingImage.LoadComplete += ImageLoaded;
            mLoadingImage.LoadFromFile(loadScreenPath);

            if(mLoadingBarBackground == null)
            {
                mLoadingBarBackground = new TextureBitmap();
                mLoadingBarBackground.LoadFromFile(@"Interface\Glues\LoadingBar\Loading-BarBorder.blp");
                mLoadingBarFill = new TextureBitmap();
                mLoadingBarFill.LoadFromFile(@"Interface\Glues\LoadingBar\Loading-BarFill.blp");
            }
        }
Beispiel #27
0
        /// <summary>
        /// See <see cref="BaseColorQuantizer.OnPrepare"/> for more details.
        /// </summary>
        protected override void OnPrepare(TextureBitmap image)
        {
            base.OnPrepare(image);

            OnFinish();
        }
    public void AlphaComposite(TextureBitmap other, Color tint, Point point = null, Rectangle otherRect = null, Func <int, int, bool> isDrawable = null)
    {
        if (Disposed)
        {
            return;
        }
        int myWidth        = Width;
        int myHeight       = Height;
        int myPixelSize    = PixelSize;
        int otherWidth     = other.Width;
        int otherHeight    = other.Height;
        int otherPixelSize = other.PixelSize;

        if (point == null)
        {
            point = new Point(0, 0);
        }
        if (otherRect == null)
        {
            otherRect = new Rectangle(0, 0, otherWidth, otherHeight);
        }
        float     tintR = tint.R / 255f;
        float     tintG = tint.G / 255f;
        float     tintB = tint.B / 255f;
        float     tintA = tint.A / 255f;
        const int BackgroundR = 0, BackgroundG = 1, BackgroundB = 2, BackgroundA = 3, OtherR = 4, OtherG = 5, OtherB = 6, OtherA = 7, Alpha = 8, ScaledBackgroundR = 9, ScaledBackgroundG = 10, ScaledBackgroundB = 11, ScaledOtherR = 12, ScaledOtherG = 13, ScaledOtherB = 14;
        IntPtr    cache        = Marshal.AllocHGlobal(4 * 15);
        float *   cachePointer = (float *)cache.ToPointer();
        byte *    myPointer    = (byte *)Bytes.ToPointer();
        byte *    otherPointer = (byte *)other.Bytes.ToPointer();

        int w = point.X + otherRect.Width;
        int h = point.Y + otherRect.Height;

        if (w > Width)
        {
            w = Width;
        }
        if (h > Height)
        {
            h = Height;
        }

        int ox = otherRect.X;
        int oy = otherRect.Y;

        for (int py = point.Y; py < h; py++)
        {
            ox = otherRect.X;
            for (int px = point.X; px < w; px++)
            {
                if (px >= 0 && px < myWidth && py >= 0 && py < myHeight)
                {
                    if (isDrawable != null && !isDrawable(px, py))
                    {
                        ox++;
                        continue;
                    }
                    int myIndex    = (px + (myHeight - 1 - py) * myWidth) * myPixelSize;
                    int otherIndex = (ox + (otherHeight - 1 - oy) * otherWidth) * otherPixelSize;

                    *(cachePointer + BackgroundR) = *(myPointer + myIndex + 0) / 255f;
                    *(cachePointer + BackgroundG) = *(myPointer + myIndex + 1) / 255f;
                    *(cachePointer + BackgroundB) = *(myPointer + myIndex + 2) / 255f;
                    *(cachePointer + BackgroundA) = *(myPointer + myIndex + 3) / 255f;
                    *(cachePointer + OtherR)      = (*(otherPointer + otherIndex + 0) / 255f * tintR);
                    *(cachePointer + OtherG)      = (*(otherPointer + otherIndex + 1) / 255f * tintG);
                    *(cachePointer + OtherB)      = (*(otherPointer + otherIndex + 2) / 255f * tintB);
                    *(cachePointer + OtherA)      = (*(otherPointer + otherIndex + 3) / 255f * tintA);

                    *(cachePointer + Alpha) = *(cachePointer + BackgroundA) + *(cachePointer + OtherA) - *(cachePointer + BackgroundA) * *(cachePointer + OtherA);

                    if (*(cachePointer + Alpha) > 0f)
                    {
                        *(cachePointer + ScaledBackgroundR) = *(cachePointer + BackgroundR) * *(cachePointer + BackgroundA);
                        *(cachePointer + ScaledBackgroundG) = *(cachePointer + BackgroundG) * *(cachePointer + BackgroundA);
                        *(cachePointer + ScaledBackgroundB) = *(cachePointer + BackgroundB) * *(cachePointer + BackgroundA);
                        *(cachePointer + ScaledOtherR)      = *(cachePointer + OtherR) * *(cachePointer + OtherA);
                        *(cachePointer + ScaledOtherG)      = *(cachePointer + OtherG) * *(cachePointer + OtherA);
                        *(cachePointer + ScaledOtherB)      = *(cachePointer + OtherB) * *(cachePointer + OtherA);

                        *(myPointer + myIndex + 0) = (byte)(UnityEngine.Mathf.Clamp01((*(cachePointer + ScaledOtherR) + *(cachePointer + ScaledBackgroundR) * (1 - *(cachePointer + OtherA))) / *(cachePointer + Alpha)) * 255f);
                        *(myPointer + myIndex + 1) = (byte)(UnityEngine.Mathf.Clamp01((*(cachePointer + ScaledOtherG) + *(cachePointer + ScaledBackgroundG) * (1 - *(cachePointer + OtherA))) / *(cachePointer + Alpha)) * 255f);
                        *(myPointer + myIndex + 2) = (byte)(UnityEngine.Mathf.Clamp01((*(cachePointer + ScaledOtherB) + *(cachePointer + ScaledBackgroundB) * (1 - *(cachePointer + OtherA))) / *(cachePointer + Alpha)) * 255f);
                        *(myPointer + myIndex + 3) = (byte)(UnityEngine.Mathf.Clamp01(*(cachePointer + Alpha)) * 255f);
                    }
                    else
                    {
                        *(myPointer + myIndex + 0) = 255;
                        *(myPointer + myIndex + 1) = 255;
                        *(myPointer + myIndex + 2) = 255;
                        *(myPointer + myIndex + 3) = 0;
                    }
                }
                ox++;
            }
            oy++;
        }
        Marshal.FreeHGlobal(cache);
    }
Beispiel #29
0
    public void RecalculateBrush()
    {
        if (Editor == null)
        {
            return;
        }

        int brushWidth = 4;

        if (BrushOverlayTexture != null)
        {
            this.BrushOverlayTexture.Resize(this.Size * this.Editor.PixelSize, this.Size * this.Editor.PixelSize);
        }
        else
        {
            this.BrushOverlayTexture = new Texture2D(this.Size * this.Editor.PixelSize, this.Size * this.Editor.PixelSize, TextureFormat.ARGB32, false);
        }

        if (this.Size == 0)
        {
            this.Size = 1;
        }
        if (BrushTexture != null)
        {
            this.BrushTexture.Resize(this.Size, this.Size);
            GameObject.Destroy(BrushSprite);
        }
        else
        {
            this.BrushTexture = new TextureBitmap(this.Size, this.Size);
        }

        Vector2 center = new Vector2(0.5f, 0.5f);

        Alphas = new float[Size * Size];

        for (int y = 0; y < Size; y++)
        {
            for (int x = 0; x < Size; x++)
            {
                int     index = x + y * Size;
                Vector2 c     = new Vector2(
                    ((float)x) / ((float)Size) + 0.5f / ((float)Size),
                    ((float)y) / ((float)Size) + 0.5f / ((float)Size));
                float alpha = Mathf.Max(0f, Mathf.Min(1f, (0.5f - Vector2.Distance(c, new Vector2(0.5f, 0.5f))) * (Hardness * Hardness * Size)));

                Alphas[x + y * Size] = alpha;
                this.BrushTexture.SetPixel(x, y, new TextureBitmap.Color(255, 255, 255, (byte)(alpha * 255f)));
            }
        }
        this.BrushTexture.Apply();
        BrushSprite = Sprite.Create(BrushTexture.Texture, new UnityEngine.Rect(0, 0, BrushTexture.Width, BrushTexture.Height), new Vector2(0.5f, 0.5f));

        var brushColor = new UnityEngine.Color(1f, 1f, 1f, 0.5f);

        UnityEngine.Color[] brushOverlayColors = new UnityEngine.Color[BrushOverlayTexture.width * BrushOverlayTexture.height];
        for (int i = 0; i < brushOverlayColors.Length; i++)
        {
            brushOverlayColors[i] = new UnityEngine.Color(0f, 0f, 0f, 0f);
        }
        for (int x = 0; x < Size; x++)
        {
            for (int y = 0; y < Size; y++)
            {
                int index = x + y * Size;
                if (Alphas[index] > 0f)
                {
                    if (x > 0 && y > 0 && Alphas[index - 1 - Size] <= 0f && Alphas[index - 1] > 0f && Alphas[index - Size] > 0f)
                    {
                        // draw top left
                        for (int nX = 0; nX < brushWidth; nX++)
                        {
                            for (int nY = 0; nY < brushWidth; nY++)
                            {
                                brushOverlayColors[x * Editor.PixelSize + nX + (y * Editor.PixelSize + nY) * BrushOverlayTexture.width] = brushColor;
                            }
                        }
                    }
                    if (x < Size - 1 && y > 0 && Alphas[index + 1 - Size] <= 0f && Alphas[index + 1] > 0f && Alphas[index - Size] > 0f)
                    {
                        // draw top right
                        for (int nX = -brushWidth; nX < 0; nX++)
                        {
                            for (int nY = 0; nY < brushWidth; nY++)
                            {
                                brushOverlayColors[(x + 1) * Editor.PixelSize + nX + (y * Editor.PixelSize + nY) * BrushOverlayTexture.width] = brushColor;
                            }
                        }
                    }
                    if (x > 0 && y < Size - 1 && Alphas[index - 1 + Size] <= 0f && Alphas[index - 1] > 0f && Alphas[index + Size] > 0f)
                    {
                        // draw bottom left
                        for (int nX = 0; nX < brushWidth; nX++)
                        {
                            for (int nY = -brushWidth; nY < 0; nY++)
                            {
                                brushOverlayColors[x * Editor.PixelSize + nX + ((y + 1) * Editor.PixelSize + nY) * BrushOverlayTexture.width] = brushColor;
                            }
                        }
                    }
                    if (x < Size - 1 && y < Size - 1 && Alphas[index + 1 + Size] <= 0f && Alphas[index + 1] > 0f && Alphas[index + Size] > 0f)
                    {
                        // draw bottom right
                        for (int nX = -brushWidth; nX < 0; nX++)
                        {
                            for (int nY = -brushWidth; nY < 0; nY++)
                            {
                                brushOverlayColors[(x + 1) * Editor.PixelSize + nX + ((y + 1) * Editor.PixelSize + nY) * BrushOverlayTexture.width] = brushColor;
                            }
                        }
                    }
                    if (x == 0 || Alphas[index - 1] <= 0f)
                    {
                        // draw left line
                        for (int nX = 0; nX < brushWidth; nX++)
                        {
                            for (int nY = 0; nY < Editor.PixelSize; nY++)
                            {
                                brushOverlayColors[x * Editor.PixelSize + nX + (y * Editor.PixelSize + nY) * BrushOverlayTexture.width] = brushColor;
                            }
                        }
                    }
                    if (x == Size - 1 || Alphas[index + 1] <= 0f)
                    {
                        // draw right line
                        for (int nX = -brushWidth; nX < 0; nX++)
                        {
                            for (int nY = 0; nY < Editor.PixelSize; nY++)
                            {
                                brushOverlayColors[(x + 1) * Editor.PixelSize + nX + (y * Editor.PixelSize + nY) * BrushOverlayTexture.width] = brushColor;
                            }
                        }
                    }
                    if (y == 0 || Alphas[index - Size] <= 0f)
                    {
                        // draw top line
                        for (int nX = 0; nX < Editor.PixelSize; nX++)
                        {
                            for (int nY = 0; nY < brushWidth; nY++)
                            {
                                brushOverlayColors[x * Editor.PixelSize + nX + (y * Editor.PixelSize + nY) * BrushOverlayTexture.width] = brushColor;
                            }
                        }
                    }
                    if (y == Size - 1 || Alphas[index + Size] <= 0f)
                    {
                        // draw bottom line
                        for (int nX = 0; nX < Editor.PixelSize; nX++)
                        {
                            for (int nY = -brushWidth; nY < 0; nY++)
                            {
                                brushOverlayColors[x * Editor.PixelSize + nX + ((y + 1) * Editor.PixelSize + nY) * BrushOverlayTexture.width] = brushColor;
                            }
                        }
                    }
                }
            }
        }

        BrushOverlayTexture.SetPixels(brushOverlayColors);
        BrushOverlayTexture.Apply();

        Editor.BrushPreviewUpdated();
    }
 public void SwitchToImporter(TextureBitmap bitmap, (int, int, int, int) rect, (int, int) resultSize, System.Action <TextureBitmap> confirm, System.Action cancel)
Beispiel #31
0
        protected override void OnLoad(EventArgs e)
        {
            // Set some GL state
            GL.ClearColor(Color4.Firebrick);
            mTimer.Start();

            string     filepath = @"Lab5/texture.jpg";
            Bitmap     TextureBitmap;
            BitmapData TextureData;

            if (System.IO.File.Exists(filepath))
            {
                TextureBitmap = new Bitmap(filepath);
                TextureBitmap.RotateFlip(RotateFlipType.RotateNoneFlipY);
                TextureData = TextureBitmap.LockBits(new System.Drawing.Rectangle(0, 0, TextureBitmap.Width, TextureBitmap.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
            }
            else
            {
                throw new Exception("Could not find file " + filepath);
            }
            int mTexture_ID;

            GL.ActiveTexture(TextureUnit.Texture0);
            GL.GenTextures(1, out mTexture_ID);
            GL.BindTexture(TextureTarget.Texture2D, mTexture_ID);
            GL.TexImage2D(TextureTarget.Texture2D,
                          0, PixelInternalFormat.Rgba, TextureData.Width, TextureData.Height,
                          0, OpenTK.Graphics.OpenGL.PixelFormat.Bgra,
                          PixelType.UnsignedByte, TextureData.Scan0);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
            TextureBitmap.UnlockBits(TextureData);

            Bitmap     TextureBitmap2;
            BitmapData TextureData2;

            filepath = @"Lab5/textureDisolveMap.jpg";
            if (System.IO.File.Exists(filepath))
            {
                TextureBitmap2 = new Bitmap(filepath);
                TextureBitmap2.RotateFlip(RotateFlipType.RotateNoneFlipY);
                TextureData2 = TextureBitmap2.LockBits(new System.Drawing.Rectangle(0, 0, TextureBitmap2.Width, TextureBitmap2.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
            }
            else
            {
                throw new Exception("Could not find file " + filepath);
            }
            int mTexture_ID2;

            GL.ActiveTexture(TextureUnit.Texture1);
            GL.GenTextures(1, out mTexture_ID2);
            GL.BindTexture(TextureTarget.Texture2D, mTexture_ID2);
            GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, TextureData2.Width, TextureData2.Height, 0, OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, TextureData2.Scan0);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
            TextureBitmap2.UnlockBits(TextureData2);



            float[] vertices = { -0.5f,  -0.5f,  0.0f, 0.0f,
                                 -0.25f, -0.5f, 0.25f,   0f,
                                 0.0f,   -0.5f,  0.5f, 0.0f,
                                 0.25f,  -0.5f, 0.75f,   0f,
                                 0.5f,   -0.5f,    1f,   0f,
                                 -0.5f,   0.0f,  0.0f, 0.5f,
                                 -0.25f,  0.0f, 0.25f, 0.5f,
                                 0.0f,    0.0f,  0.5f, 0.5f,
                                 0.25f,   0.0f, 0.75f, 0.5f,
                                 0.5f,    0.0f,    1f, 0.5f,
                                 -0.5f,   0.5f,    0f,   1f,
                                 -0.25f,  0.5f, 0.25f,   1f,
                                 0.0f,    0.5f,  0.5f,   1f,
                                 0.25f,   0.5f, 0.75f,   1f,
                                 0.5f,    0.5f,    1f, 1f };

            uint[] indices = { 5,  0,  1,
                               5,  1,  6,
                               6,  1,  2,
                               6,  2,  7,
                               7,  2,  3,
                               7,  3,  8,
                               8,  3,  4,
                               8,  4,  9,
                               10, 5,  6,
                               10, 6, 11,
                               11, 6,  7,
                               11, 7, 12,
                               12, 7,  8,
                               12, 8, 13,
                               13, 8,  9,
                               13, 9, 14 };

            GL.Enable(EnableCap.CullFace);

            mShader = new ShaderUtility(@"Lab5/Shaders/vTexture.vert", @"Lab5/Shaders/fTexture.frag");
            GL.UseProgram(mShader.ShaderProgramID);
            int vPositionLocation        = GL.GetAttribLocation(mShader.ShaderProgramID, "vPosition");
            int vTextCoordsLocation      = GL.GetAttribLocation(mShader.ShaderProgramID, "vTexCoords");
            int uTextureSamplerLocation  = GL.GetUniformLocation(mShader.ShaderProgramID, "uTextureSampler");
            int uTextureSamplerLocation2 = GL.GetUniformLocation(mShader.ShaderProgramID, "uTextureSampler2");
            int uThresholdLocation       = GL.GetUniformLocation(mShader.ShaderProgramID, "uThreshold");

            GL.Uniform1(uTextureSamplerLocation, 0);
            GL.Uniform1(uTextureSamplerLocation2, 1);
            GL.Uniform1(uThresholdLocation, 0.5f);

            mVAO_ID = GL.GenVertexArray();
            GL.GenBuffers(mVBO_IDs.Length, mVBO_IDs);

            GL.BindVertexArray(mVAO_ID);
            GL.BindBuffer(BufferTarget.ArrayBuffer, mVBO_IDs[0]);
            GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(vertices.Length * sizeof(float)), vertices, BufferUsageHint.StaticDraw);
            GL.BindBuffer(BufferTarget.ElementArrayBuffer, mVBO_IDs[1]);
            GL.BufferData(BufferTarget.ElementArrayBuffer, (IntPtr)(indices.Length * sizeof(uint)), indices, BufferUsageHint.StaticDraw);

            int size;

            GL.GetBufferParameter(BufferTarget.ArrayBuffer, BufferParameterName.BufferSize, out size);
            if (vertices.Length * sizeof(float) != size)
            {
                throw new ApplicationException("Vertex data not loaded onto graphics card correctly");
            }

            GL.GetBufferParameter(BufferTarget.ElementArrayBuffer, BufferParameterName.BufferSize, out size);
            if (indices.Length * sizeof(uint) != size)
            {
                throw new ApplicationException("Index data not loaded onto graphics card correctly");
            }

            GL.EnableVertexAttribArray(vPositionLocation);
            GL.EnableVertexAttribArray(vTextCoordsLocation);
            GL.VertexAttribPointer(vPositionLocation, 2, VertexAttribPointerType.Float, false, 4 * sizeof(float), 0);
            GL.VertexAttribPointer(vTextCoordsLocation, 2, VertexAttribPointerType.Float, false, 4 * sizeof(float), 2 * sizeof(float));


            GL.BindVertexArray(0);

            base.OnLoad(e);
        }
    public void Replace(TextureBitmap other, Color tint, Point point = null, Rectangle otherRect = null)
    {
        if (Disposed)
        {
            return;
        }
        int myWidth        = Width;
        int myHeight       = Height;
        int myPixelSize    = PixelSize;
        int otherWidth     = other.Width;
        int otherHeight    = other.Height;
        int otherPixelSize = other.PixelSize;

        float tintR = tint.R / 255f;
        float tintG = tint.G / 255f;
        float tintB = tint.B / 255f;
        float tintA = tint.A / 255f;

        byte *myPointer    = (byte *)Bytes.ToPointer();
        byte *otherPointer = (byte *)other.Bytes.ToPointer();

        if (point == null)
        {
            point = new Point(0, 0);
        }
        if (otherRect == null)
        {
            otherRect = new Rectangle(0, 0, otherWidth, otherHeight);
        }
        int w = point.X + otherRect.Width;
        int h = point.Y + otherRect.Height;

        if (w > Width)
        {
            w = Width;
        }
        if (h > Height)
        {
            h = Height;
        }
        int ox = otherRect.X;
        int oy = otherRect.Y;

        for (int py = point.Y; py < h; py++)
        {
            ox = otherRect.X;
            for (int px = point.X; px < w; px++)
            {
                if (px >= 0 && px < myWidth && py >= 0 && py < myHeight)
                {
                    int myIndex    = (px + (myHeight - 1 - py) * myWidth) * myPixelSize;
                    int otherIndex = (ox + (otherHeight - 1 - oy) * otherWidth) * otherPixelSize;

                    *(myPointer + myIndex + 0) = *(otherPointer + otherIndex + 0);
                    *(myPointer + myIndex + 1) = *(otherPointer + otherIndex + 1);
                    *(myPointer + myIndex + 2) = *(otherPointer + otherIndex + 2);
                    *(myPointer + myIndex + 3) = *(otherPointer + otherIndex + 3);
                }
                ox++;
            }
            oy++;
        }
    }