Example #1
0
        private static List <Bitmap> GetBitmapList()
        {
            List <Bitmap> bitmaps = new List <Bitmap>();

            for (int i = 0; i < SelectedState.Self.SelectedChain.Frames.Count; i++)
            {
                var frame = SelectedState.Self.SelectedChain.Frames[i];

                var texture = WireframeManager.Self.GetTextureForFrame(frame);
                if (texture != null)
                {
                    FlatRedBall.Graphics.Texture.ImageData imageData =
                        ImageData.FromTexture2D(texture);

                    int left   = Math.MathFunctions.RoundToInt(frame.LeftCoordinate * texture.Width);
                    int right  = Math.MathFunctions.RoundToInt(frame.RightCoordinate * texture.Width);
                    int top    = Math.MathFunctions.RoundToInt(frame.TopCoordinate * texture.Height);
                    int bottom = Math.MathFunctions.RoundToInt(frame.BottomCoordinate * texture.Height);

                    int width  = right - left;
                    int height = bottom - top;

                    Bitmap bitmap = new Bitmap(width, height);
                    bitmaps.Add(bitmap);
                    for (int ySource = top; ySource < bottom; ySource++)
                    {
                        for (int xSource = left; xSource < right; xSource++)
                        {
                            int yDestination = ySource - top;
                            int xDestination = xSource - left;

                            Microsoft.Xna.Framework.Color sourceColor      = imageData.GetPixelColor(xSource, ySource);
                            System.Drawing.Color          destinationColor = Color.FromArgb(
                                sourceColor.A,
                                sourceColor.R,
                                sourceColor.G,
                                sourceColor.B);

                            bitmap.SetPixel(xDestination, yDestination, destinationColor);
                        }
                    }
                }
            }
            return(bitmaps);
        }
Example #2
0
        public void UpdateDisplay()
        {

            #region Create the Sprites if needed

            if (mVisible && (mSprite == null || mImageData == null))
            {
                mSprite = SpriteManager.AddSprite((Texture2D)null);
                // mSprite.TextureAddressMode = TextureAddressMode.Clamp; // required on REACH if we're not a power of 2

                mSprite.X = mXSeed + .5f * (mNumberOfXTiles - 1) * mGridSpacing;
                mSprite.Y = mYSeed + .5f * (mNumberOfYTiles - 1) * mGridSpacing;
                mSprite.Z = this.mVisibleDisplayZ;
                mSprite.FlipVertical = true;
                mSprite.ScaleX = (mNumberOfXTiles) * mGridSpacing / 2.0f;
                mSprite.ScaleY = (mNumberOfYTiles) * mGridSpacing / 2.0f;

                mImageData = new ImageData(mNumberOfXTiles, mNumberOfYTiles);
            }

            #endregion

            if (Visible)
            {

                ForceUpdateImageData();
            }
            else
            {
                if (mSprite != null)
                {
                    SpriteManager.RemoveSprite(mSprite);
                    mSprite = null;
                }

            }


        }
        public static ImageData RenderSpriteGrid(SpriteGrid spriteGrid)
        {
            int textureWidth = (int)(.5f + spriteGrid.FurthestRightX - spriteGrid.FurthestLeftX);
            int textureHeight = (int)(.5f + spriteGrid.FurthestTopY - spriteGrid.FurthestBottomY);

            ImageData imageData = new ImageData(textureWidth, textureHeight);

            RenderSpriteGrid(spriteGrid, imageData);

            return imageData;
        }
        public static void RenderSprite(Sprite sprite, int leftPixel, int topPixel, int pixelWidth, int pixelHeight, ImageData imageData)
        {
            if (sprite.Texture == null || sprite.BlendOperation != BlendOperation.Regular)
            {
                // for now throw an exception, later we may want to handle pure color rendering and stuff like that
                throw new NotImplementedException();

            }

            ImageData spriteTextureImageData = ImageData.FromTexture2D(sprite.Texture);

#if FRB_MDX
            ColorOperation colorOperation = GraphicalEnumerations.TranslateTextureOperationToColorOperation(sprite.ColorOperation);

#else
            ColorOperation colorOperation = sprite.ColorOperation;
#endif

            spriteTextureImageData.ApplyColorOperation(colorOperation, sprite.Red, sprite.Green, sprite.Blue, sprite.Alpha);


            int rightBound = System.Math.Min(imageData.Width, leftPixel + pixelWidth);
            int bottomBound = System.Math.Min(imageData.Height, topPixel + pixelHeight);

            int actualWidth = rightBound - leftPixel;
            int actualHeight = bottomBound - topPixel;



            for (int destinationX = leftPixel; destinationX < rightBound; destinationX++)
            {
                for (int destinationY = topPixel; destinationY < bottomBound; destinationY++)
                {
                    int sourcePixelX = spriteTextureImageData.Width * (destinationX - leftPixel) / pixelWidth;
                    int sourcePixelY = spriteTextureImageData.Height * (destinationY - topPixel) / pixelHeight;

                    Color sourcePixel = spriteTextureImageData.GetPixelColor(sourcePixelX, sourcePixelY);

                    if (sourcePixel.A != 255)
                    {
                        Color destinationPixel = imageData.GetPixelColor(destinationX, destinationY);
#if FRB_MDX
                        sourcePixel = Color.FromArgb(
                            System.Math.Max(sourcePixel.A, destinationPixel.A),
                            (byte)(destinationPixel.R * (255 - sourcePixel.A) / 255.0f + sourcePixel.R * (sourcePixel.A) / 255.0f),
                            (byte)(destinationPixel.G * (255 - sourcePixel.A) / 255.0f + sourcePixel.G * (sourcePixel.A) / 255.0f),
                            (byte)(destinationPixel.B * (255 - sourcePixel.A) / 255.0f + sourcePixel.B * (sourcePixel.A) / 255.0f));

                        // This is probably not accurate, but will work currently.  Eventually we may want to look at how blending is actually performed
#else
                        sourcePixel.R = (byte)(destinationPixel.R * (255 - sourcePixel.A) / 255.0f + sourcePixel.R * (sourcePixel.A) / 255.0f);
                        sourcePixel.G = (byte)(destinationPixel.G * (255 - sourcePixel.A) / 255.0f + sourcePixel.G * (sourcePixel.A) / 255.0f);
                        sourcePixel.B = (byte)(destinationPixel.B * (255 - sourcePixel.A) / 255.0f + sourcePixel.B * (sourcePixel.A) / 255.0f);

                        // This is probably not accurate, but will work currently.  Eventually we may want to look at how blending is actually performed
                        sourcePixel.A = System.Math.Max(sourcePixel.A, destinationPixel.A);
#endif
                    }
                    imageData.SetPixel(destinationX, destinationY, sourcePixel);
                }
            }
        }
        private static void RenderLetter(char letter, BitmapFont bitmapFont, ImageData imageData, int x, int y)
        {
            float tvTop = 0;
            float tvBottom = 0;
            float tuLeft = 0;
            float tuRight = 0;

            int textureWidth = bitmapFont.Texture.Width;
            int textureHeight = bitmapFont.Texture.Height;

            bitmapFont.AssignCharacterTextureCoordinates((int)letter, out tvTop, out tvBottom, out tuLeft, out tuRight);
            
            
            float characterHeight = bitmapFont.GetCharacterHeight(letter);

            int pixelLeft = (int)(tuLeft * textureWidth);
            int pixelTop = (int)(tvTop * textureHeight);

            int pixelRight = (int)(tuRight * textureWidth);
            int pixelBottom = (int)(tvBottom * textureWidth);

            float unitPerPixel = (characterHeight / (float)(pixelBottom - pixelTop));

            int pixelFromTop = (int)(bitmapFont.LineHeightInPixels * bitmapFont.DistanceFromTopOfLine((int)letter) / (2));// * .25f);


            for (int sourceY = pixelTop; sourceY < pixelBottom; sourceY++)
            {
                for (int sourceX = pixelLeft; sourceX < pixelRight; sourceX++)
                {
                    Color colorFromSource = sTemporaryTextureBuffer[sourceY * textureHeight + sourceX];

                    if (colorFromSource.A != 0)
                    {

                        imageData.SetPixel(x + (sourceX - pixelLeft), pixelFromTop + y + (sourceY - pixelTop), colorFromSource);
                    }
                }
            }

        }
        public static void RenderText(string text, BitmapFont bitmapFont, ImageData imageData, int x, int y)
        {
            sTemporaryTextureBuffer = new Color[bitmapFont.Texture.Width * bitmapFont.Texture.Height];

            bitmapFont.Texture.GetData<Color>(sTemporaryTextureBuffer);

            for (int i = 0; i < text.Length; i++)
            {
                RenderLetter(text[i], bitmapFont, imageData, x, y);

                x += (int)(bitmapFont.GetCharacterSpacing(text[i]) * 8);
            }
        }
 public static void RenderText(string text, BitmapFont bitmapFont, ImageData imageData)
 {
     RenderText(text, bitmapFont, imageData, 0, 0);
 }
        public static void RenderSpriteGrid(SpriteGrid spriteGrid, ImageData imageData)
        {
            throw new NotImplementedException();

            //// The SpriteGrid should be full
            //spriteGrid.FillToBounds();

            //int textureWidth = (int)(.5f + spriteGrid.FurthestRightX - spriteGrid.FurthestLeftX);
            //int textureHeight = (int)(.5f + spriteGrid.FurthestTopY - spriteGrid.FurthestBottomY);

            //if (imageData.Width != textureWidth || imageData.Height != textureHeight)
            //{
            //    throw new ArgumentException("Image data is the wrong dimensions");
            //}

            //Dictionary<string, ImageData> mCachedImageDatas = new Dictionary<string, ImageData>();

            //int pixelWidthPerSprite = (int)(.5f + (float)textureHeight / spriteGrid.VisibleSprites.Count);

            //for (int row = spriteGrid.MinDisplayedRowIndex(); row <= spriteGrid.MaxDisplayedRowIndex(); row++)
            //{
            //    for (int column = spriteGrid.MinDisplayedColIndex(); column <= spriteGrid.MaxDisplayedColIndex(); column++)
            //    {
            //        Sprite spriteAtIndex = spriteGrid.GetSpriteAtIndex(row, column);

            //        if (!mCachedImageDatas.ContainsKey(spriteAtIndex.Texture.Name))
            //        {
            //            ImageData imageDataForThisSprite = ImageData.FromTexture2D(spriteAtIndex.Texture);

            //            mCachedImageDatas.Add(spriteAtIndex.Texture.Name, imageDataForThisSprite);
            //        }

            //        ImageData imageDataToPullFrom = mCachedImageDatas[spriteAtIndex.Texture.Name];

            //        for (int pixelX = 0; pixelX < pixelWidthPerSprite; pixelX++)
            //        {
            //            for (int pixelY = 0; pixelY < pixelWidthPerSprite; pixelY++)
            //            {
            //                float textureX = (.5f + pixelX) / pixelWidthPerSprite;
            //                float textureY = (.5f + pixelY) / pixelWidthPerSprite;

            //                int xToWriteAt = (column - spriteGrid.MinDisplayedColIndex()) * pixelWidthPerSprite + pixelX;
            //                int yToWriteAt = (spriteGrid.MaxDisplayedRowIndex() - row) * pixelWidthPerSprite + pixelY;

            //                int spriteXPixelCoordinate = (int)(.5f + spriteAtIndex.LeftTextureCoordinate * spriteAtIndex.Texture.Width) + pixelX;
            //                int spriteYPixelCoordinate = (int)(.5f + spriteAtIndex.TopTextureCoordinate * spriteAtIndex.Texture.Height) + pixelY;

            //                // For now we'll just handle the errors in a naive way.  Return here if issues occur with pixel mismatchings
            //                if (spriteXPixelCoordinate >= imageDataToPullFrom.Width)
            //                {
            //                    spriteXPixelCoordinate = imageDataToPullFrom.Width - 1;
            //                }
            //                if (spriteYPixelCoordinate >= imageDataToPullFrom.Height)
            //                {
            //                    spriteYPixelCoordinate = imageDataToPullFrom.Height - 1;
            //                }


            //                imageData.SetPixel(xToWriteAt, yToWriteAt, imageDataToPullFrom.Data[spriteXPixelCoordinate + spriteYPixelCoordinate * imageDataToPullFrom.Width]);


            //            }
            //        }
            //    }
            //}
        }
Example #9
0
        public static ImageDataList GetImageDataList(string gifFileName)
        {
            ImageDataList imageDatas = new ImageDataList();

#if XBOX360 || SILVERLIGHT
            if (gifFileName.StartsWith(@".\") || gifFileName.StartsWith(@"./"))
            {
                gifFileName = gifFileName.Substring(2);
            }
#endif

#if SILVERLIGHT || WINDOWS_8
            using (Stream stream = FileManager.GetStreamForFile(gifFileName))
#else
            using (FileStream stream = System.IO.File.OpenRead(gifFileName))
#endif

            {
                mGifInfo = new GifInfo();
                mGifInfo.DelayTimes = new List<short>();

                BinaryReader reader = new BinaryReader(stream);

                #region header
                // Example:  GIF89a
                byte[] buffer = reader.ReadBytes(6);
                // set the header here
                char[] charArray = new char[buffer.Length];
                for (int i = 0; i < buffer.Length; i++)
                {
                    charArray[i] = (char)buffer[i];
                }

                mGifInfo.Header = new string(charArray);
                #endregion

                #region Width/Height

                mGifInfo.Width = reader.ReadInt16();
                mGifInfo.Height = reader.ReadInt16();

                #endregion

                #region Packed information

                // The packed byte has the following info:
                // Bits 0-2     Size of the Global Color Table
                // Bit 3        Color Table Sort Flag
                // Bits 4-6     Color Resolution
                // Bit 7        Global Color Table Flag

                Byte packedByte = reader.ReadByte();

                int sizeOfGlobalColorTable = (7 & packedByte);

                mGifInfo.NumberOfColorEntries = 1 * (1 << (sizeOfGlobalColorTable + 1));

                mGifInfo.HasGlobalColorTable = (128 & packedByte) != 0;

                #endregion

                #region background color
                reader.ReadByte();
                #endregion

                #region default aspect ratio

                reader.ReadByte();

                #endregion

                TryReadGlobalColorTable(reader);


                byte separator = reader.ReadByte();

                while (separator != 0x3b) // Extension
                {
                    switch(separator)
                    {
                        #region Extensions

                        case 0x21:

                            Byte label = reader.ReadByte();

                            switch (label)
                            {
                                case 0xf9:
                                    ReadGraphicsControlExtension(reader);
                                    break;
                                case 0xff:
                                    ReadApplicationExtensionBlock(reader);
                                    break;
                                case 0xfe:
                                    ReadCommonExtensionBlock(reader);
                                    break;

                            }
                            break;
                        #endregion

                        #region Image Data
                        case 0x2c:

                            ReadImageDescriptor(reader);


                            Color[] color = new Color[mGifInfo.Width * mGifInfo.Height];

                            int transparentIndex = mGifInfo.TransparentIndex;

                            #region Interlaced
                            if (mGifInfo.IsInterlaced)
                            {
                                int i = 0;
                                for (int pass = 0; pass < 4; ++pass)
                                {
                                    int row = 0;
                                    int increment = 0;

                                    switch (pass)
                                    {
                                        case 0: row = 0; increment = 8; break;
                                        case 1: row = 4; increment = 8; break;
                                        case 2: row = 2; increment = 4; break;
                                        case 3: row = 1; increment = 2; break;
                                    }
                                    for (; row < mGifInfo.Height; row += increment)
                                    {

                                        for (int col = 0; col < mGifInfo.Width; ++col)
                                        {
                                            int position = (row * mGifInfo.Width) + col;

                                            byte index = mUncompressedColorIndexBuffer[i++];

                                            byte alpha = 255;

                                            if (mGifInfo.HasTransparency && index == transparentIndex)
                                                alpha = 0;

#if FRB_XNA || SILVERLIGHT || WINDOWS_PHONE
                                        color[position] = new Color(
                                            (byte)mGifInfo.PaletteInfo.Entries[index].R,
                                            (byte)mGifInfo.PaletteInfo.Entries[index].G,
                                            (byte)mGifInfo.PaletteInfo.Entries[index].B,
                                            (byte)alpha);
#elif FRB_MDX
                                            color[position] = Color.FromArgb(
                                                alpha,
                                                (byte)mGifInfo.PaletteInfo.Entries[index].R,
                                                (byte)mGifInfo.PaletteInfo.Entries[index].G,
                                                (byte)mGifInfo.PaletteInfo.Entries[index].B);

#endif

                                        }

                                    }

                                }
                            }
                            #endregion
                            else
                            {
                                #region NonInterlaced

                                for (int i = 0; i < mGifInfo.PaletteInfo.Entries.Length; i++)
                                {
                                    mGifInfo.PaletteInfo.Entries[transparentIndex].A = 255;
                                }                                
                                if (mGifInfo.HasTransparency)
                                {
                                    mGifInfo.PaletteInfo.Entries[transparentIndex].A = 0;
                                }

                                int x = 0;
                                int y = 0;

                                int colorIndex;

                                for (int i = 0; i < mUncompressedColorIndexBuffer.Count; i++)
                                {                                    
                                    byte index = mUncompressedColorIndexBuffer[i];

                                    // Let's see if we can avoid an if statement
                                    x = mGifInfo.CurrentBlockLeft + i % mGifInfo.CurrentBlockWidth;
                                    y = mGifInfo.CurrentBlockTop + i / mGifInfo.CurrentBlockWidth;

                                    colorIndex = x + y * mGifInfo.Width;

#if FRB_XNA || SILVERLIGHT || WINDOWS_PHONE
                                    color[colorIndex] = new Color(
                                        (byte)mGifInfo.PaletteInfo.Entries[index].R,
                                        (byte)mGifInfo.PaletteInfo.Entries[index].G,
                                        (byte)mGifInfo.PaletteInfo.Entries[index].B,
                                        (byte)mGifInfo.PaletteInfo.Entries[index].A);
#elif FRB_MDX
                                    color[colorIndex] = Color.FromArgb(
                                        (byte)mGifInfo.PaletteInfo.Entries[index].A,
                                        (byte)mGifInfo.PaletteInfo.Entries[index].R,
                                        (byte)mGifInfo.PaletteInfo.Entries[index].G,
                                        (byte)mGifInfo.PaletteInfo.Entries[index].B);
#endif
                                }
                                #endregion
                            }
                            ImageData imageData = new ImageData(
                                mGifInfo.Width, mGifInfo.Height, color);

                            imageDatas.Add(imageData);

                            mUncompressedColorIndexBuffer.Clear();

                            break;
                        #endregion

                        #region End of file
                        case 0x3b:

                            // end of file
                            break;

                        #endregion
                    }

                    separator = reader.ReadByte();
                }
            }

            // Fill the imageDatas with the frame times
            foreach (short s in mGifInfo.DelayTimes)
            {
                imageDatas.FrameTimes.Add(s / 100.0);
            }

            return imageDatas;
        }
Example #10
0
 public void CopyTo(ImageData destination, int xOffset, int yOffset)
 {
     for (int x = 0; x < width; x++)
     {
         for (int y = 0; y < height; y++)
         {
             destination.mData[(y + yOffset) * destination.width + (x + xOffset)] = this.mData[y * width + x];
         }
     }
 }
Example #11
0
        public void Blit(ImageData source, Rectangle sourceRectangle, Point destination)
        {
            for (int y = 0; y < sourceRectangle.Height; y++)
            {
                for (int x = 0; x < sourceRectangle.Width; x++)
                {
                    int sourceX = x + sourceRectangle.X;
                    int sourceY = y + sourceRectangle.Y;

                    int destinationX = x + destination.X;
                    int destinationY = y + destination.Y;

                    this.SetPixel(destinationX, destinationY, source.GetPixelColor(sourceX, sourceY));
                }
            }
        }
Example #12
0
        public static ImageData FromTexture2D(Texture2D texture2D)
        {
#if DEBUG
            if (texture2D.IsDisposed)
            {
                throw new Exception("The texture by the name " + texture2D.Name + " is disposed, so its data can't be accessed");
            }
#endif

            ImageData imageData = null;
            // Might need to make this FRB MDX as well.
#if FRB_XNA || SILVERLIGHT || WINDOWS_PHONE

            switch (texture2D.Format)
            {
#if !XNA4 && !MONOGAME
                case SurfaceFormat.Bgr32:
                    {
                        Color[] data = new Color[texture2D.Width * texture2D.Height];
                        texture2D.GetData<Color>(data);

                        // BRG doesn't have alpha, so we'll assume an alpha of 1:
                        for (int i = 0; i < data.Length; i++)
                        {
                            data[i].A = 255;
                        }

                        imageData = new ImageData(
                            texture2D.Width, texture2D.Height, data);
                    }
                    break;
#endif
                case SurfaceFormat.Color:
                    {
                        Color[] data = new Color[texture2D.Width * texture2D.Height];
                        texture2D.GetData<Color>(data);

                        imageData = new ImageData(
                            texture2D.Width, texture2D.Height, data);
                    }
                    break;
                case SurfaceFormat.Dxt3:

                    Byte[] byteData = new byte[texture2D.Width * texture2D.Height];
                    texture2D.GetData<byte>(byteData);

                    imageData = new ImageData(texture2D.Width, texture2D.Height, byteData);

                    break;

                default:
                    throw new NotImplementedException("The format " + texture2D.Format + " isn't supported.");

                //break;
            }

#endif
            return imageData;
        }
Example #13
0
        /// <summary>
        /// Loads a .png image from the fileName given and returns an array of pixel colors.
        /// </summary>
        /// <param name="fileName">The name of the .png image to be loaded.</param>
        /// <returns>
        /// Returns a Microsoft.Xna.Framework.Graphics.Color[] containing one item(Color) for each pixel
        /// in the image.</returns>
        public static ImageData GetPixelData(string fileName)
        {
            for (int i = 0; i < ByteBufferSize; i++)
            {
                mTransparentEntries[i] = byte.MaxValue;
            }

            int bytesRead = LoadFile(fileName);

            


            mMaxTransIndex = -1;


            //Load the file into "byte stream"
            Stream sourceStream = new MemoryStream(sByteBuffer, 0, bytesRead);


            //Check the signature to verify this is an actual .png image
            if (!CheckSignature(ref sourceStream))
                throw new ArgumentException(
                    String.Format("Argument Stream {0} does not contain a valid PNG file.", sourceStream));

            //Since all data in .png files is organized into categorized chunks, create a
            //List to store them in so that they can be read and processed later.
            List<Chunk> chunks = new List<Chunk>();

            //Load each Chunk of data from the stream into the List of Chunks. Then
            //close the stream.
            while (GetNextChunk(ref sourceStream, ref chunks)) { }
            sourceStream.Close();


            //Read and store the information from the IHDR chunk, which contains
            //general info for this image. Note: IHDR chunk is removed from List<Chunk> chunks.
            HeaderInfo header = ReadHeader(ref chunks);


            //Create an empty palette in case we need it
            PaletteInfo palette = new PaletteInfo();

            //Process the Chunks of data and obtain the decompressed bytes of the image
            byte[] filteredImage = ProcessChunks(chunks, header, ref palette);


            //Reverse the filtering that was done on the image before compression
            byte[] defilteredImage = ReverseFiltering(filteredImage, header);



            //Translate the un-filtered image bytes into Colors and store in the array to be returned.
            Color[] pixelData = BuildPixels(defilteredImage, ref header, ref palette);
            //System.Diagnostics.Trace.WriteLine(pixelData.Length);

            ImageData imageData = new ImageData((int)header.Width, (int)header.Height, pixelData);


            return imageData;
        }