Ejemplo n.º 1
0
        /// <summary>
        /// Create heightfield by extracting colour from image
        /// </summary>
        /// <param name="texture"></param>
        /// <param name="component"></param>
        /// <param name="invertX"></param>
        /// <param name="invertY"></param>
        public HeightField(Texture2D texture, ColourComponent component = ColourComponent.Luminosity, bool invertX = false, bool invertY = false)
        {
            this.width  = texture.width;
            this.height = texture.height;

            this.heightmap = new float[this.width * this.height];

            for (int w = 0; w < texture.width; w++)
            {
                for (int h = 0; h < texture.height; h++)
                {
                    this[w, h] = SampleColour(component, texture.GetPixel(
                                                  invertX ? ((texture.width - 1) - w) : w,
                                                  invertY ? ((texture.height - 1) - h) : h
                                                  ));
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Sampel a height from a colour
        /// </summary>
        /// <param name="colour"></param>
        /// <returns></returns>
        private static float SampleColour(ColourComponent heightColour, Color colour)
        {
            switch (heightColour)
            {
            case ColourComponent.Alpha:
                return(colour.a);

            case ColourComponent.Blue:
                return(colour.b);

            case ColourComponent.Green:
                return(colour.g);

            case ColourComponent.Luminosity:
                return(0.21f * colour.r + 0.72f * colour.g + 0.07f * colour.b);

            default:
                return(colour.r);
            }
        }
Ejemplo n.º 3
0
        private Bitmap GetColourPlaneBitmap(Rectangle rect, ColourComponent comp)
        {
            Bitmap     map     = new Bitmap(rect.Width, rect.Height, PixelFormat.Format24bppRgb);
            BitmapData mapData = map.LockBits(
                new Rectangle(0, 0, map.Width, map.Height),
                ImageLockMode.WriteOnly,
                PixelFormat.Format24bppRgb);

            unsafe
            {
                byte *pt0 = (byte *)mapData.Scan0;

                Parallel.For(rect.Top, rect.Bottom, y =>
                {
                    int bitmapY = y - rect.Top;

                    for (int x = rect.Left; x < rect.Right; x++)
                    {
                        int bitmapX = x - rect.Left;

                        Color colour;
                        switch (comp)
                        {
                        case ColourComponent.HUE:
                            colour = AdobeColours.HSB_to_RGB(new AdobeColours.HSB(
                                                                 _hsb.H,
                                                                 x / 255.0,
                                                                 1 - y / 255.0));
                            break;

                        case ColourComponent.SATURATION:
                            colour = AdobeColours.HSB_to_RGB(new AdobeColours.HSB(
                                                                 x / 255.0,
                                                                 _hsb.S,
                                                                 1 - y / 255.0));
                            break;

                        case ColourComponent.BRIGHTESS:
                            colour = AdobeColours.HSB_to_RGB(new AdobeColours.HSB(
                                                                 x / 255.0,
                                                                 1 - y / 255.0,
                                                                 _hsb.B));
                            break;

                        case ColourComponent.RED:
                            colour = Color.FromArgb(
                                _rgb.R,
                                x,
                                255 - y);
                            break;

                        case ColourComponent.GREEN:
                            colour = Color.FromArgb(
                                x,
                                _rgb.G,
                                255 - y);
                            break;

                        case ColourComponent.BLUE:
                            colour = Color.FromArgb(
                                x,
                                255 - y,
                                _rgb.B);
                            break;

                        default:
                            throw new ArgumentException();
                        }

                        if (_webSafeColoursOnly)
                        {
                            colour = AdobeColours.GetNearestWebSafeColour(colour);
                        }

                        byte *pt = pt0 + mapData.Stride * bitmapY + 3 * bitmapX;
                        pt[2]    = colour.R;
                        pt[1]    = colour.G;
                        pt[0]    = colour.B;
                    }
                });
            }

            map.UnlockBits(mapData);
            return(map);
        }
Ejemplo n.º 4
0
        private Bitmap GetColourStripBitmap(Rectangle rect, ColourComponent comp)
        {
            Bitmap     map     = new Bitmap(rect.Width, rect.Height, PixelFormat.Format24bppRgb);
            BitmapData mapData = map.LockBits(
                new Rectangle(0, 0, map.Width, map.Height),
                ImageLockMode.WriteOnly,
                PixelFormat.Format24bppRgb);

            int height = this.Height - 8;

            unsafe
            {
                byte *pt0 = (byte *)mapData.Scan0;

                Parallel.For(rect.Top, rect.Bottom, y =>
                {
                    int bitmapY = y - rect.Top;

                    Color colour;
                    switch (comp)
                    {
                    case ColourComponent.HUE:
                        colour = AdobeColours.HSB_to_RGB(new AdobeColours.HSB(
                                                             1.0 - (double)y / height,
                                                             1,
                                                             1));
                        break;

                    case ColourComponent.SATURATION:
                        colour = AdobeColours.HSB_to_RGB(new AdobeColours.HSB(
                                                             _hsb.H,
                                                             1.0 - (double)y / height,
                                                             _hsb.B));
                        break;

                    case ColourComponent.BRIGHTESS:
                        colour = AdobeColours.HSB_to_RGB(new AdobeColours.HSB(
                                                             _hsb.H,
                                                             _hsb.S,
                                                             1.0 - (double)y / height));
                        break;

                    case ColourComponent.RED:
                        int red = 255 - (int)Math.Round(255 * (double)y / height);
                        colour  = Color.FromArgb(
                            red,
                            _rgb.G,
                            _rgb.B);
                        break;

                    case ColourComponent.GREEN:
                        int green = 255 - (int)Math.Round(255 * (double)y / height);
                        colour    = Color.FromArgb(
                            _rgb.R,
                            green,
                            _rgb.B);
                        break;

                    case ColourComponent.BLUE:
                        int blue = 255 - (int)Math.Round(255 * (double)y / height);
                        colour   = Color.FromArgb(
                            _rgb.R,
                            _rgb.G,
                            blue);
                        break;

                    default:
                        throw new ArgumentException();
                    }

                    if (_webSafeColoursOnly)
                    {
                        colour = AdobeColours.GetNearestWebSafeColour(colour);
                    }

                    for (int x = rect.Left; x < rect.Right; x++)
                    {
                        int bitmapX = x - rect.Left;

                        byte *pt = pt0 + mapData.Stride * bitmapY + 3 * bitmapX;
                        pt[2]    = colour.R;
                        pt[1]    = colour.G;
                        pt[0]    = colour.B;
                    }
                });
            }

            map.UnlockBits(mapData);
            return(map);
        }