Exemplo n.º 1
0
        // Compute the average color of a texture.
        private unsafe Color computeAverageColor(Bitmap bmp)
        {
            BitmapProcessing.FastBitmap unsafeBMP = new BitmapProcessing.FastBitmap(bmp);
            // we have 256 x 256 pixel, each with 256 possible color-values per
            // color-channel, so 2^24 is the maximum value we can get, adding everything.
            unsafeBMP.LockBitmap();
            int r      = 0;
            int g      = 0;
            int b      = 0;
            int pixels = 0;

            for (int y = 0; y < bmp.Height; y += 10)
            {
                for (int x = 0; x < bmp.Width; x += 10)
                {
                    Color pixel = unsafeBMP.GetPixel(x, y);
                    r += pixel.R;
                    g += pixel.G;
                    b += pixel.B;
                    pixels++;
                }
            }

            unsafeBMP.UnlockBitmap();

            return(Color.FromArgb((int)r / pixels, (int)g / pixels, (int)b / pixels));
        }
Exemplo n.º 2
0
        private List <List <Coord> > bitmap2CoordsSampled(Bitmap bitmap, int scale, bool mirror)
        {
            int numRows = bitmap.Height / scale;
            int numCols = bitmap.Width / scale;
            List <List <Coord> > rows = new List <List <Coord> >(numRows);

            float pixScale = 1.0f / 256.0f;

            int imageX, imageY = 0;

            int rowNdx, colNdx;

            BitmapProcessing.FastBitmap unsafeBMP = new BitmapProcessing.FastBitmap(bitmap);
            unsafeBMP.LockBitmap(); //Lock the bitmap for the unsafe operation

            for (rowNdx = 0; rowNdx <= numRows; rowNdx++)
            {
                List <Coord> row = new List <Coord>(numCols);
                imageY = rowNdx * scale;
                if (rowNdx == numRows)
                {
                    imageY--;
                }
                for (colNdx = 0; colNdx <= numCols; colNdx++)
                {
                    imageX = colNdx * scale;
                    if (colNdx == numCols)
                    {
                        imageX--;
                    }

                    Color pixel = unsafeBMP.GetPixel(imageX, imageY);

                    if (pixel.A != 255)
                    {
                        pixel = Color.FromArgb(255, pixel.R, pixel.G, pixel.B);
                        unsafeBMP.SetPixel(imageX, imageY, pixel);
                    }

                    if (mirror)
                    {
                        row.Add(new Coord(-(pixel.R * pixScale - 0.5f), pixel.G * pixScale - 0.5f, pixel.B * pixScale - 0.5f));
                    }
                    else
                    {
                        row.Add(new Coord(pixel.R * pixScale - 0.5f, pixel.G * pixScale - 0.5f, pixel.B * pixScale - 0.5f));
                    }
                }
                rows.Add(row);
            }

            unsafeBMP.UnlockBitmap();

            return(rows);
        }
Exemplo n.º 3
0
        // Compute the average color of a texture.
        private unsafe Color computeAverageColor(Bitmap bmp)
        {
            BitmapProcessing.FastBitmap unsafeBMP = new BitmapProcessing.FastBitmap(bmp);
            // we have 256 x 256 pixel, each with 256 possible color-values per
            // color-channel, so 2^24 is the maximum value we can get, adding everything.
            unsafeBMP.LockBitmap();
            int r = 0;
            int g = 0;
            int b = 0;

            for (int y = 0; y < bmp.Height; y += 10)
            {
                for (int x = 0; x < bmp.Width; x += 10)
                {
                    Color pixel = unsafeBMP.GetPixel(x, y);
                    r += pixel.R;
                    g += pixel.G;
                    b += pixel.B;
                }
            }

            unsafeBMP.UnlockBitmap();

            int pixels = (bmp.Width * bmp.Height) / (10 * 10);
            return Color.FromArgb((int)r / pixels, (int)g / pixels, (int)b / pixels);
        }
Exemplo n.º 4
0
        public void PaintEffect(ITerrainChannel map, UUID userID, float rx, float ry, float rz, float strength, float duration, float BrushSize, List<Scene> scene)
        {
            if (locked)
                return;
            locked = true;
            strength = TerrainUtil.MetersToSphericalStrength(BrushSize);

            int x, y;

            int xFrom = (int)(rx - BrushSize + 0.5);
            int xTo = (int)(rx + BrushSize + 0.5) + 1;
            int yFrom = (int)(ry - BrushSize + 0.5);
            int yTo = (int)(ry + BrushSize + 0.5) + 1;

            if (xFrom < 0)
                xFrom = 0;

            if (yFrom < 0)
                yFrom = 0;

            if (xTo > map.Width)
                xTo = map.Width;

            if (yTo > map.Height)
                yTo = map.Height;

            //ONLY get cached assets, since this is a local asset ONLY
            AssetBase paintAsset = map.Scene.AssetService.Get(map.Scene.RegionInfo.RegionSettings.PaintableTerrainTexture.ToString());
            if (paintAsset == null)
            {
                paintAsset = new AssetBase(map.Scene.RegionInfo.RegionSettings.PaintableTerrainTexture, "PaintableTerrainTexture-" + map.Scene.RegionInfo.RegionID, (sbyte)AssetType.Texture, UUID.Zero.ToString());
                paintAsset.Flags = AssetFlags.Deletable;
                AssetBase defaultTexture = map.Scene.AssetService.Get(RegionSettings.DEFAULT_TERRAIN_TEXTURE_2.ToString());//Nice grass
                if (defaultTexture == null)
                    //Erm... what to do!
                    return;

                paintAsset.Data = defaultTexture.Data;//Eventually we need to replace this with an interpolation of the existing textures!
            }

            AssetBase textureToApply = map.Scene.AssetService.Get(m_textureToPaint.ToString()); //The texture the client wants to paint
            if (textureToApply == null)
                return;

            Image paintiTexture = map.Scene.RequestModuleInterface<IJ2KDecoder> ().DecodeToImage (paintAsset.Data);
            if (paintiTexture == null)
                return;

            Image textureToAddiTexture = map.Scene.RequestModuleInterface<IJ2KDecoder> ().DecodeToImage (textureToApply.Data);
            if (textureToAddiTexture == null)
            {
                paintiTexture.Dispose();
                return;
            }

            BitmapProcessing.FastBitmap paintTexture = new BitmapProcessing.FastBitmap((Bitmap)paintiTexture);
            BitmapProcessing.FastBitmap textureToAddTexture = new BitmapProcessing.FastBitmap((Bitmap)textureToAddiTexture);

            paintTexture.LockBitmap();
            textureToAddTexture.LockBitmap();

            // blend in map
            for (x = xFrom; x < xTo; x++)
            {
                for (y = yFrom; y < yTo; y++)
                {
                    if (!map.Scene.Permissions.CanTerraformLand(userID, new Vector3(x, y, 0)))
                        continue;

                    Color c = textureToAddTexture.GetPixel((int)(((float)x / (float)map.Scene.RegionInfo.RegionSizeX * (float)textureToAddiTexture.Width)),
                        (int)(((float)y / (float)map.Scene.RegionInfo.RegionSizeX) * (float)textureToAddiTexture.Height));
                    Color cc = paintTexture.GetPixel((int)(((float)x / (float)map.Scene.RegionInfo.RegionSizeX) * (float)textureToAddiTexture.Width),
                        (int)(((float)y / (float)map.Scene.RegionInfo.RegionSizeX) * (float)textureToAddiTexture.Height));
                    paintTexture.SetPixel((int)(((float)x / (float)map.Scene.RegionInfo.RegionSizeX) * (float)paintiTexture.Width),
                        (int)(((float)y / (float)map.Scene.RegionInfo.RegionSizeX) * (float)paintiTexture.Height), c);
                    cc = paintTexture.GetPixel((int)(((float)x / (float)map.Scene.RegionInfo.RegionSizeX * (float)textureToAddiTexture.Width)),
                        (int)(((float)y / (float)map.Scene.RegionInfo.RegionSizeX) * (float)textureToAddiTexture.Height));
                }
            }
            map.Scene.AssetService.Delete(paintAsset.ID);
            paintTexture.UnlockBitmap();
            textureToAddTexture.UnlockBitmap();
            paintAsset.Data = OpenJPEG.EncodeFromImage(paintTexture.Bitmap(), false);
            paintAsset.Flags = AssetFlags.Deletable;
            map.Scene.RegionInfo.RegionSettings.PaintableTerrainTexture = UUID.Random();
            paintAsset.ID = map.Scene.RegionInfo.RegionSettings.PaintableTerrainTexture.ToString();
            map.Scene.AssetService.Store(paintAsset);
            map.Scene.RequestModuleInterface<IEstateModule>().sendRegionHandshakeToAll();
            locked = false;
        }
Exemplo n.º 5
0
        public SculptMap(Bitmap bm, int lod)
        {
            int bmW = bm.Width;
            int bmH = bm.Height;

            if (bmW == 0 || bmH == 0)
                throw new Exception("SculptMap: bitmap has no data");

            int numLodPixels = lod * 2 * lod * 2;  // (32 * 2)^2  = 64^2 pixels for default sculpt map image

            bool needsScaling = false;

            width = bmW;
            height = bmH;
            while (width * height > numLodPixels)
            {
                width >>= 1;
                height >>= 1;
                needsScaling = true;
            }



            try
            {
                if (needsScaling)
                    bm = ScaleImage(bm, width, height,
                        System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor);
            }

            catch (Exception e)
            {
                throw new Exception("Exception in ScaleImage(): e: " + e.ToString());
            }

            if (width * height > lod * lod)
            {
                width >>= 1;
                height >>= 1;
            }

            int numBytes = (width + 1) * (height + 1);
            redBytes = new byte[numBytes];
            greenBytes = new byte[numBytes];
            blueBytes = new byte[numBytes];

            BitmapProcessing.FastBitmap unsafeBMP = new BitmapProcessing.FastBitmap(bm);
            unsafeBMP.LockBitmap(); //Lock the bitmap for the unsafe operation
            
            int byteNdx = 0;

            try
            {
                for (int y = 0; y <= height; y++)
                {
                    for (int x = 0; x <= width; x++)
                    {
                        int bmY = y < height ? y * 2 : y * 2 - 1;
                        int bmX = x < width ? x * 2 : x * 2 - 1;
                        Color pixel = unsafeBMP.GetPixel(bmX, bmY);

                        redBytes[byteNdx] = pixel.R;
                        greenBytes[byteNdx] = pixel.G;
                        blueBytes[byteNdx] = pixel.B;

                        ++byteNdx;
                    }
                }
            }
            catch (Exception e)
            {
                throw new Exception("Caught exception processing byte arrays in SculptMap(): e: " + e.ToString());
            }

            //All done, unlock
            unsafeBMP.UnlockBitmap();

            width++;
            height++;
        }
Exemplo n.º 6
0
        public static Bitmap Distort(Bitmap sourceBitmap, Point topleft, Point topright, Point bottomleft, Point bottomright, int interpolation)
        {
            double sourceWidth  = sourceBitmap.Width;
            double sourceHeight = sourceBitmap.Height;

            //Find dimensions of new image
            Point[] pointarray = new Point[] { topleft, topright, bottomright, bottomleft };

            int width  = int.MinValue;
            int height = int.MinValue;

            foreach (Point p in pointarray)
            {
                width  = Math.Max(width, p.X);
                height = Math.Max(height, p.Y);
            }

            Bitmap bitmap = new Bitmap(width, height);

            //For faster image processing
            BitmapProcessing.FastBitmap newBmp    = new BitmapProcessing.FastBitmap(bitmap);
            BitmapProcessing.FastBitmap sourceBmp = new BitmapProcessing.FastBitmap(sourceBitmap);

            newBmp.LockImage();
            sourceBmp.LockImage();

            //Key points
            PointF A = (PointF)topleft;
            PointF B = (PointF)topright;
            PointF C = (PointF)bottomright;
            PointF D = (PointF)bottomleft;

            // sides
            float mAB = GetAngle(A, B);
            float mCD = GetAngle(C, D);
            float mAD = GetAngle(A, D);
            float mBC = GetAngle(B, C);

            //Get corner intersections
            PointF O = GetIntersection(new Vector(B, mAB), new Vector(C, mCD));
            PointF N = GetIntersection(new Vector(A, mAD), new Vector(B, mBC));

            if (interpolation <= 0)
            {
                interpolation = 1;
            }
            int middleX = (int)(interpolation / 2.0);

            //Array of surronding pixels used for interpolation
            double[, ,] source = new double[interpolation, interpolation, 4];

            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    PointF P = new PointF(x, y);

                    float mPO = mAB; //Default value
                    float mPN = mBC;

                    if (O != PointF.Empty) //If intersection found, get coefficient
                    {
                        mPO = GetAngle(O, P);
                    }

                    if (N != PointF.Empty) //If intersection found, get coefficient
                    {
                        mPN = GetAngle(N, P);
                    }

                    //Get intersections
                    PointF L = GetIntersection(new Vector(P, mPO), new Vector(A, mAD));
                    if (L == PointF.Empty)
                    {
                        L = A;
                    }

                    PointF M = GetIntersection(new Vector(P, mPO), new Vector(C, mBC));
                    if (M == PointF.Empty)
                    {
                        M = C;
                    }

                    PointF J = GetIntersection(new Vector(P, mPN), new Vector(B, mAB));
                    if (J == PointF.Empty)
                    {
                        J = B;
                    }

                    PointF K = GetIntersection(new Vector(P, mPN), new Vector(D, mCD));
                    if (K == PointF.Empty)
                    {
                        K = D;
                    }

                    double dJP = GetDistance(J, P);
                    double dLP = GetDistance(L, P);

                    double dJK = GetDistance(J, K);
                    double dLM = GetDistance(L, M);

                    //set direction
                    if (dLM < GetDistance(M, P))
                    {
                        dLP = -dLP;
                    }
                    if (dJK < GetDistance(K, P))
                    {
                        dJP = -dJP;
                    }

                    ////interpolation

                    //find the pixels which surround the point
                    double yP0 = sourceHeight * dJP / dJK;
                    double xP0 = sourceWidth * dLP / dLM;

                    //top left coordinates of surrounding pixels
                    if (xP0 < 0)
                    {
                        xP0--;
                    }
                    if (yP0 < 0)
                    {
                        yP0--;
                    }

                    int left = (int)xP0;
                    int top  = (int)yP0;

                    if ((left < -1 || left > sourceWidth) && (top < -1 || top > sourceHeight))
                    {
                        //if outside of source image just move on
                        continue;
                    }

                    //weights
                    double xFrac    = xP0 - (double)left;
                    double xFracRec = 1.0 - xFrac;
                    double yFrac    = yP0 - (double)top;
                    double yFracRec = 1.0 - yFrac;

                    //get source pixel colors, or white if out of range (to interpolate into the background color)
                    int   x0;
                    int   y0;
                    Color c;

                    for (int sx = 0; sx < interpolation; sx++)
                    {
                        for (int sy = 0; sy < interpolation; sy++)
                        {
                            x0 = left + sx;
                            y0 = top + sy;

                            if (x0 > 0 && y0 > 0 &&
                                x0 < sourceWidth && y0 < sourceHeight)
                            {
                                c = sourceBmp.GetPixel(x0, y0);

                                source[sx, sy, 0] = c.R;
                                source[sx, sy, 1] = c.G;
                                source[sx, sy, 2] = c.B;
                                source[sx, sy, 3] = 255.0f;
                            }
                            else
                            {
                                // set full transparency in this case
                                source[sx, sy, 0] = 0;
                                source[sx, sy, 1] = 0;
                                source[sx, sy, 2] = 0;
                                source[sx, sy, 3] = 0;
                            }
                        }
                    }

                    //interpolate on x
                    for (int sy = 0; sy < interpolation; sy++)
                    {
                        //check transparency
                        if (source[middleX, sy, 3] != 0 && source[0, sy, 3] == 0)
                        {
                            //copy colors from 1, sy
                            source[0, sy, 0] = source[1, sy, 0];
                            source[0, sy, 1] = source[1, sy, 1];
                            source[0, sy, 2] = source[1, sy, 2];
                            source[0, sy, 3] = source[1, sy, 3];
                        }
                        else
                        {
                            //compute colors by interpolation
                            source[0, sy, 0] = source[0, sy, 0] * xFracRec + source[middleX, sy, 0] * xFrac;
                            source[0, sy, 1] = source[0, sy, 1] * xFracRec + source[middleX, sy, 1] * xFrac;
                            source[0, sy, 2] = source[0, sy, 2] * xFracRec + source[middleX, sy, 2] * xFrac;
                            source[0, sy, 3] = source[0, sy, 3] * xFracRec + source[middleX, sy, 3] * xFrac;
                        }

                        //interpolate transparency
                        source[0, sy, 3] = source[0, sy, 3] * xFracRec + source[middleX, sy, 3] * xFrac;
                    }

                    //now interpolate on y

                    //check transparency
                    if (source[0, middleX, 3] != 0 && source[0, 0, 3] == 0)
                    {
                        //copy colors from 0, 1
                        source[0, 0, 0] = source[0, middleX, 0];
                        source[0, 0, 1] = source[0, middleX, 1];
                        source[0, 0, 2] = source[0, middleX, 2];
                        source[0, 0, 3] = source[0, middleX, 3];
                    }
                    else
                    {
                        source[0, 0, 0] = source[0, 0, 0] * yFracRec + source[0, middleX, 0] * yFrac;
                        source[0, 0, 1] = source[0, 0, 1] * yFracRec + source[0, middleX, 1] * yFrac;
                        source[0, 0, 2] = source[0, 0, 2] * yFracRec + source[0, middleX, 2] * yFrac;
                        source[0, 0, 3] = source[0, 0, 3] * yFracRec + source[0, middleX, 3] * yFrac;
                    }

                    //interpolate transparency
                    source[0, 0, 3] = source[0, 0, 3] * yFracRec + source[0, middleX, 3] * yFrac;

                    //store to bitmap
                    if (source[0, 0, 3] != 0) //pixel has color
                    {
                        newBmp.SetPixel(x, y, Color.FromArgb((int)source[0, 0, 3], (int)source[0, 0, 0], (int)source[0, 0, 1], (int)source[0, 0, 2]));
                    }
                }
            }

            sourceBmp.UnlockImage();
            newBmp.UnlockImage();

            return(bitmap);
        }
Exemplo n.º 7
0
        private List<List<Coord>> bitmap2CoordsSampled(Bitmap bitmap, int scale, bool mirror)
        {
            int numRows = bitmap.Height / scale;
            int numCols = bitmap.Width / scale;
            List<List<Coord>> rows = new List<List<Coord>>(numRows);

            float pixScale = 1.0f / 256.0f;

            int imageX, imageY = 0;

            int rowNdx, colNdx;

            BitmapProcessing.FastBitmap unsafeBMP = new BitmapProcessing.FastBitmap(bitmap);
            unsafeBMP.LockBitmap(); //Lock the bitmap for the unsafe operation

            for (rowNdx = 0; rowNdx <= numRows; rowNdx++)
            {
                List<Coord> row = new List<Coord>(numCols);
                imageY = rowNdx * scale;
                if (rowNdx == numRows) imageY--;
                for (colNdx = 0; colNdx <= numCols; colNdx++)
                {
                    imageX = colNdx * scale;
                    if (colNdx == numCols) imageX--;

                    Color pixel = unsafeBMP.GetPixel(imageX, imageY);

                    if (pixel.A != 255)
                    {
                        pixel = Color.FromArgb(255, pixel.R, pixel.G, pixel.B);
                        unsafeBMP.SetPixel(imageX, imageY, pixel);
                    }

                    if (mirror)
                        row.Add(new Coord(-(pixel.R * pixScale - 0.5f), pixel.G * pixScale - 0.5f, pixel.B * pixScale - 0.5f));
                    else
                        row.Add(new Coord(pixel.R * pixScale - 0.5f, pixel.G * pixScale - 0.5f, pixel.B * pixScale - 0.5f));

                }
                rows.Add(row);
            }

            unsafeBMP.UnlockBitmap();

            return rows;
        }
Exemplo n.º 8
0
        /// <summary>
        /// converts a bitmap to a list of lists of coords, while scaling the image.
        /// the scaling is done in floating point so as to allow for reduced vertex position
        /// quantization as the position will be averaged between pixel values. this routine will
        /// likely fail if the bitmap width and height are not powers of 2.
        /// </summary>
        /// <param name="bitmap"></param>
        /// <param name="scale"></param>
        /// <param name="mirror"></param>
        /// <returns></returns>
        private List<List<Coord>> bitmap2Coords(Bitmap bitmap, int scale, bool mirror)
        {
            int numRows = bitmap.Height / scale;
            int numCols = bitmap.Width / scale;
            List<List<Coord>> rows = new List<List<Coord>>(numRows);

            float pixScale = 1.0f / (scale * scale);
            pixScale /= 255;

            int imageX, imageY = 0;

            int rowNdx, colNdx;

            BitmapProcessing.FastBitmap unsafeBMP = new BitmapProcessing.FastBitmap(bitmap);
            unsafeBMP.LockBitmap(); //Lock the bitmap for the unsafe operation
            
            for (rowNdx = 0; rowNdx < numRows; rowNdx++)
            {
                List<Coord> row = new List<Coord>(numCols);
                for (colNdx = 0; colNdx < numCols; colNdx++)
                {
                    imageX = colNdx * scale;
                    int imageYStart = rowNdx * scale;
                    int imageYEnd = imageYStart + scale;
                    int imageXEnd = imageX + scale;
                    float rSum = 0.0f;
                    float gSum = 0.0f;
                    float bSum = 0.0f;
                    for (; imageX < imageXEnd; imageX++)
                    {
                        for (imageY = imageYStart; imageY < imageYEnd; imageY++)
                        {
                            Color pixel = unsafeBMP.GetPixel(imageX, imageY);

                            if (pixel.A != 255)
                            {
                                pixel = Color.FromArgb(255, pixel.R, pixel.G, pixel.B);
                                unsafeBMP.SetPixel(imageX, imageY, pixel);
                            }
                            rSum += pixel.R;
                            gSum += pixel.G;
                            bSum += pixel.B;
                        }
                    }
                    if (mirror)
                        row.Add(new Coord(-(rSum * pixScale - 0.5f), gSum * pixScale - 0.5f, bSum * pixScale - 0.5f));
                    else
                        row.Add(new Coord(rSum * pixScale - 0.5f, gSum * pixScale - 0.5f, bSum * pixScale - 0.5f));

                }
                rows.Add(row);
            }

            unsafeBMP.UnlockBitmap();

            return rows;
        }
Exemplo n.º 9
0
 public Bitmap Shade (Bitmap source, Color shade, float percent, bool greyScale)
 {
     BitmapProcessing.FastBitmap b = new BitmapProcessing.FastBitmap (source);
     b.LockBitmap ();
     for (int y = 0; y < source.Height; y++)
     {
         for (int x = 0; x < source.Width; x++)
         {
             Color c = b.GetPixel (x, y);
             if (greyScale)
             {
                 int luma = (int)(c.R * 0.3 + c.G * 0.59 + c.B * 0.11);
                 b.SetPixel (x, y, Color.FromArgb (c.A, luma, luma, luma));
             }
             else
             {
                 float amtFrom = 1 - percent;
                 int lumaR = (int)(c.R * amtFrom + shade.R * percent);
                 int lumaG = (int)(c.G * amtFrom + shade.G * percent);
                 int lumaB = (int)(c.B * amtFrom + shade.B * percent);
                 b.SetPixel (x, y, Color.FromArgb (c.A, lumaR, lumaG, lumaB));
             }
         }
     }
     b.UnlockBitmap ();
     return b.Bitmap();
 }
Exemplo n.º 10
0
        /// <summary>
        /// converts a bitmap to a list of lists of coords, while scaling the image.
        /// the scaling is done in floating point so as to allow for reduced vertex position
        /// quantization as the position will be averaged between pixel values. this routine will
        /// likely fail if the bitmap width and height are not powers of 2.
        /// </summary>
        /// <param name="bitmap"></param>
        /// <param name="scale"></param>
        /// <param name="mirror"></param>
        /// <returns></returns>
        private List <List <Coord> > bitmap2Coords(Bitmap bitmap, int scale, bool mirror)
        {
            int numRows = bitmap.Height / scale;
            int numCols = bitmap.Width / scale;
            List <List <Coord> > rows = new List <List <Coord> >(numRows);

            float pixScale = 1.0f / (scale * scale);

            pixScale /= 255;

            int imageX, imageY = 0;

            int rowNdx, colNdx;

            BitmapProcessing.FastBitmap unsafeBMP = new BitmapProcessing.FastBitmap(bitmap);
            unsafeBMP.LockBitmap(); //Lock the bitmap for the unsafe operation

            for (rowNdx = 0; rowNdx < numRows; rowNdx++)
            {
                List <Coord> row = new List <Coord>(numCols);
                for (colNdx = 0; colNdx < numCols; colNdx++)
                {
                    imageX = colNdx * scale;
                    int   imageYStart = rowNdx * scale;
                    int   imageYEnd   = imageYStart + scale;
                    int   imageXEnd   = imageX + scale;
                    float rSum        = 0.0f;
                    float gSum        = 0.0f;
                    float bSum        = 0.0f;
                    for (; imageX < imageXEnd; imageX++)
                    {
                        for (imageY = imageYStart; imageY < imageYEnd; imageY++)
                        {
                            Color pixel = unsafeBMP.GetPixel(imageX, imageY);

                            if (pixel.A != 255)
                            {
                                pixel = Color.FromArgb(255, pixel.R, pixel.G, pixel.B);
                                unsafeBMP.SetPixel(imageX, imageY, pixel);
                            }
                            rSum += pixel.R;
                            gSum += pixel.G;
                            bSum += pixel.B;
                        }
                    }
                    if (mirror)
                    {
                        row.Add(new Coord(-(rSum * pixScale - 0.5f), gSum * pixScale - 0.5f, bSum * pixScale - 0.5f));
                    }
                    else
                    {
                        row.Add(new Coord(rSum * pixScale - 0.5f, gSum * pixScale - 0.5f, bSum * pixScale - 0.5f));
                    }
                }
                rows.Add(row);
            }

            unsafeBMP.UnlockBitmap();

            return(rows);
        }
Exemplo n.º 11
0
        private void Draw(string data, UUID id, string extraParams)
        {
            // We need to cater for old scripts that didnt use extraParams neatly, they use either an integer size which represents both width and height, or setalpha
            // we will now support multiple comma seperated params in the form  width:256,height:512,alpha:255
            int width = 256;
            int height = 256;
            int alpha = 255; // 0 is transparent
            Color bgColour = Color.White;  // Default background color
            char altDataDelim = ';';
            
            char[] paramDelimiter = { ',' };
            char[] nvpDelimiter = { ':' };
           
            extraParams = extraParams.Trim();
            extraParams = extraParams.ToLower();
            
            string[] nvps = extraParams.Split(paramDelimiter);
            
            int temp = -1;
            foreach (string pair in nvps)
            {
                string[] nvp = pair.Split(nvpDelimiter);
                string name = "";
                string value = "";
                
                if (nvp[0] != null)
                {
                    name = nvp[0].Trim();
                }
                
                if (nvp.Length == 2)
                {
                    value = nvp[1].Trim();
                }
                
                switch (name)
                {
                    case "width":
                        temp = parseIntParam(value);
                        if (temp != -1)
                        {
                            if (temp < 1)
                            {
                                width = 1;
                            }
                            else if (temp > 2048)
                            {
                                width = 2048;
                            }
                            else
                            {
                                width = temp;
                            }
                        }
                        break;
                    case "height":
                        temp = parseIntParam(value);
                        if (temp != -1)
                        {
                            if (temp < 1)
                            {
                                height = 1;
                            }
                            else if (temp > 2048)
                            {
                                height = 2048;
                            }
                            else
                            {
                                height = temp;
                            }
                        }
                        break;
                     case "alpha":
                          temp = parseIntParam(value);
                          if (temp != -1)
                          {
                              if (temp < 0)
                              {
                                  alpha = 0;
                              }
                              else if (temp > 255)
                              {
                                  alpha = 255;
                              }
                              else
                              {
                                  alpha = temp;
                              }
                          }
                          // Allow a bitmap w/o the alpha component to be created
                          else if (value.ToLower() == "false") {
                               alpha = 256;
                          }
                          break;
                     case "bgcolour":
                     case "bgcolor":
                         int hex = 0;
                         if (Int32.TryParse(value, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out hex))
                         {
                             bgColour = Color.FromArgb(hex);
                         } 
                         else
                         {
                             bgColour = Color.FromName(value);
                         }
                         break;
                     case "altdatadelim":
                         altDataDelim = value.ToCharArray()[0];
                         break;
                     case "":
                         // blank string has been passed do nothing just use defaults
                     break;
                     default: // this is all for backwards compat, all a bit ugly hopfully can be removed in future
                         // could be either set alpha or just an int
                         if (name == "setalpha")
                         {
                             alpha = 0; // set the texture to have transparent background (maintains backwards compat)
                         }
                         else
                         {
                             // this function used to accept an int on its own that represented both 
                             // width and height, this is to maintain backwards compat, could be removed
                             // but would break existing scripts
                             temp = parseIntParam(name);
                             if (temp != -1)
                             {
                                 if (temp > 1024)
                                    temp = 1024;
                                    
                                 if (temp < 128)
                                     temp = 128;
                                  
                                 width = temp;
                                 height = temp;
                             }
                         }
                     break;
                }
            }

            Bitmap bitmap;
            
            if (alpha == 256)
            {
                bitmap = new Bitmap(width, height, PixelFormat.Format32bppRgb);
            }
            else
            {
                bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb);
            }

            Graphics graph = Graphics.FromImage(bitmap);
            // this is really just to save people filling the 
            // background color in their scripts, only do when fully opaque
            if (alpha >= 255)
            {
                graph.FillRectangle(new SolidBrush(bgColour), 0, 0, width, height); 
            }

            BitmapProcessing.FastBitmap fastBitmap = new BitmapProcessing.FastBitmap(bitmap);
            fastBitmap.LockBitmap();
            for (int w = 0; w < bitmap.Width; w++)
            {
                if (alpha <= 255) 
                {
                    for (int h = 0; h < bitmap.Height; h++)
                    {
                        fastBitmap.SetPixel(w, h, Color.FromArgb(alpha, fastBitmap.GetPixel(w, h)));
                    }
                }
            }
            fastBitmap.UnlockBitmap();
            bitmap = fastBitmap.Bitmap();

            graph.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            graph.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
            graph.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
            GDIDraw(data, graph, altDataDelim);

            byte[] imageJ2000 = new byte[0];

            try
            {
                imageJ2000 = OpenJPEG.EncodeFromImage(bitmap, false);
            }
            catch (Exception)
            {
                m_log.Error(
                    "[VECTORRENDERMODULE]: OpenJpeg Encode Failed.  Empty byte data returned!");
            }

            m_textureManager.ReturnData(id, imageJ2000);
        }
Exemplo n.º 12
0
        public void PaintEffect(ITerrainChannel map, UUID userID, float rx, float ry, float rz, float strength, float duration, float BrushSize, List <Scene> scene)
        {
            if (locked)
            {
                return;
            }
            locked   = true;
            strength = TerrainUtil.MetersToSphericalStrength(BrushSize);

            int x, y;

            int xFrom = (int)(rx - BrushSize + 0.5);
            int xTo   = (int)(rx + BrushSize + 0.5) + 1;
            int yFrom = (int)(ry - BrushSize + 0.5);
            int yTo   = (int)(ry + BrushSize + 0.5) + 1;

            if (xFrom < 0)
            {
                xFrom = 0;
            }

            if (yFrom < 0)
            {
                yFrom = 0;
            }

            if (xTo > map.Width)
            {
                xTo = map.Width;
            }

            if (yTo > map.Height)
            {
                yTo = map.Height;
            }

            //ONLY get cached assets, since this is a local asset ONLY
            AssetBase paintAsset = map.Scene.AssetService.Get(map.Scene.RegionInfo.RegionSettings.PaintableTerrainTexture.ToString());

            if (paintAsset == null)
            {
                paintAsset       = new AssetBase(map.Scene.RegionInfo.RegionSettings.PaintableTerrainTexture, "PaintableTerrainTexture-" + map.Scene.RegionInfo.RegionID, (sbyte)AssetType.Texture, UUID.Zero.ToString());
                paintAsset.Flags = AssetFlags.Deletable;
                AssetBase defaultTexture = map.Scene.AssetService.Get(RegionSettings.DEFAULT_TERRAIN_TEXTURE_2.ToString());//Nice grass
                if (defaultTexture == null)
                {
                    //Erm... what to do!
                    return;
                }

                paintAsset.Data = defaultTexture.Data;//Eventually we need to replace this with an interpolation of the existing textures!
            }

            AssetBase textureToApply = map.Scene.AssetService.Get(m_textureToPaint.ToString()); //The texture the client wants to paint

            if (textureToApply == null)
            {
                return;
            }

            Image paintiTexture = map.Scene.RequestModuleInterface <IJ2KDecoder> ().DecodeToImage(paintAsset.Data);

            if (paintiTexture == null)
            {
                return;
            }

            Image textureToAddiTexture = map.Scene.RequestModuleInterface <IJ2KDecoder> ().DecodeToImage(textureToApply.Data);

            if (textureToAddiTexture == null)
            {
                paintiTexture.Dispose();
                return;
            }

            BitmapProcessing.FastBitmap paintTexture        = new BitmapProcessing.FastBitmap((Bitmap)paintiTexture);
            BitmapProcessing.FastBitmap textureToAddTexture = new BitmapProcessing.FastBitmap((Bitmap)textureToAddiTexture);

            paintTexture.LockBitmap();
            textureToAddTexture.LockBitmap();

            // blend in map
            for (x = xFrom; x < xTo; x++)
            {
                for (y = yFrom; y < yTo; y++)
                {
                    if (!map.Scene.Permissions.CanTerraformLand(userID, new Vector3(x, y, 0)))
                    {
                        continue;
                    }

                    Color c = textureToAddTexture.GetPixel((int)(((float)x / (float)map.Scene.RegionInfo.RegionSizeX * (float)textureToAddiTexture.Width)),
                                                           (int)(((float)y / (float)map.Scene.RegionInfo.RegionSizeX) * (float)textureToAddiTexture.Height));
                    Color cc = paintTexture.GetPixel((int)(((float)x / (float)map.Scene.RegionInfo.RegionSizeX) * (float)textureToAddiTexture.Width),
                                                     (int)(((float)y / (float)map.Scene.RegionInfo.RegionSizeX) * (float)textureToAddiTexture.Height));
                    paintTexture.SetPixel((int)(((float)x / (float)map.Scene.RegionInfo.RegionSizeX) * (float)paintiTexture.Width),
                                          (int)(((float)y / (float)map.Scene.RegionInfo.RegionSizeX) * (float)paintiTexture.Height), c);
                    cc = paintTexture.GetPixel((int)(((float)x / (float)map.Scene.RegionInfo.RegionSizeX * (float)textureToAddiTexture.Width)),
                                               (int)(((float)y / (float)map.Scene.RegionInfo.RegionSizeX) * (float)textureToAddiTexture.Height));
                }
            }
            map.Scene.AssetService.Delete(paintAsset.ID);
            paintTexture.UnlockBitmap();
            textureToAddTexture.UnlockBitmap();
            paintAsset.Data  = OpenJPEG.EncodeFromImage(paintTexture.Bitmap(), false);
            paintAsset.Flags = AssetFlags.Deletable;
            map.Scene.RegionInfo.RegionSettings.PaintableTerrainTexture = UUID.Random();
            paintAsset.ID = map.Scene.RegionInfo.RegionSettings.PaintableTerrainTexture.ToString();
            map.Scene.AssetService.Store(paintAsset);
            map.Scene.RequestModuleInterface <IEstateModule>().sendRegionHandshakeToAll();
            locked = false;
        }
Exemplo n.º 13
0
        public SculptMap(Bitmap bm, int lod)
        {
            int bmW = bm.Width;
            int bmH = bm.Height;

            if (bmW == 0 || bmH == 0)
            {
                throw new Exception("SculptMap: bitmap has no data");
            }

            int numLodPixels = lod * 2 * lod * 2;  // (32 * 2)^2  = 64^2 pixels for default sculpt map image

            bool needsScaling = false;

            width  = bmW;
            height = bmH;
            while (width * height > numLodPixels)
            {
                width      >>= 1;
                height     >>= 1;
                needsScaling = true;
            }



            try
            {
                if (needsScaling)
                {
                    bm = ScaleImage(bm, width, height,
                                    System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor);
                }
            }

            catch (Exception e)
            {
                throw new Exception("Exception in ScaleImage(): e: " + e.ToString());
            }

            if (width * height > lod * lod)
            {
                width  >>= 1;
                height >>= 1;
            }

            int numBytes = (width + 1) * (height + 1);

            redBytes   = new byte[numBytes];
            greenBytes = new byte[numBytes];
            blueBytes  = new byte[numBytes];

            BitmapProcessing.FastBitmap unsafeBMP = new BitmapProcessing.FastBitmap(bm);
            unsafeBMP.LockBitmap(); //Lock the bitmap for the unsafe operation

            int byteNdx = 0;

            try
            {
                for (int y = 0; y <= height; y++)
                {
                    for (int x = 0; x <= width; x++)
                    {
                        int   bmY   = y < height ? y * 2 : y * 2 - 1;
                        int   bmX   = x < width ? x * 2 : x * 2 - 1;
                        Color pixel = unsafeBMP.GetPixel(bmX, bmY);

                        redBytes[byteNdx]   = pixel.R;
                        greenBytes[byteNdx] = pixel.G;
                        blueBytes[byteNdx]  = pixel.B;

                        ++byteNdx;
                    }
                }
            }
            catch (Exception e)
            {
                throw new Exception("Caught exception processing byte arrays in SculptMap(): e: " + e.ToString());
            }

            //All done, unlock
            unsafeBMP.UnlockBitmap();

            width++;
            height++;
        }