XNA 4.0 error

Mar 3, 2011 at 11:17 AM

Hi, 

I've just downloaded XNA Gif Animation Library for XNA Game Studio 4.0. When I try to load a gif, there's an error prompting "The size of the data passed in is too large or too small for this resource".

By the way, here's someone with the same problem: http://stackoverflow.com/questions/4496266/xna-gif-animation-library-problem

Seems like the error is in the GifAnimationContentTypeReader.cs, more specifically in the SetData: 

 frames[i].SetData<byte>(j, rect, data, 0, data.Length);

The gifs I'm trying to load are in SurfaceFormat BGR565. If I replace the Texture2D constructor:

frames[i] = new Texture2D(graphicsDevice, width, height, false, format);

by: 

frames[i] = new Texture2D(graphicsDevice, width, height);

The SurfaceFormat is ignored (assuming the default SurfaceFormat Color), and the error is gone (althouh the gif renders in strange blueish colors, probably for the different components in RGB and BGR).

My question is: why is this error happening? Anyone knows how to solve it without the blue colors?

May 26, 2012 at 12:33 AM

Hi,

Convert RGBA to BGRA

Change GifAnimationContentTypeReader with this code


namespace GifAnimation
{
    using Microsoft.Xna.Framework.Content;
    using Microsoft.Xna.Framework.Graphics;
    using System;
    using Microsoft.Xna.Framework;

    public sealed class GifAnimationContentTypeReader : ContentTypeReader
    {
        protected override GifAnimation Read(ContentReader input, GifAnimation existingInstance)
        {
            int num = input.ReadInt32();
            Texture2D[] frames = new Texture2D[num];
            IGraphicsDeviceService service = (IGraphicsDeviceService) input.ContentManager.ServiceProvider.GetService(typeof(IGraphicsDeviceService));
            if (service == null)
            {
                throw new ContentLoadException();
            }
            GraphicsDevice graphicsDevice = service.GraphicsDevice;
            if (graphicsDevice == null)
            {
                throw new ContentLoadException();
            }
            for (int i = 0; i < num; i++)
            {
                SurfaceFormat format = (SurfaceFormat) input.ReadInt32();
                int width = input.ReadInt32();
                int height = input.ReadInt32();
                int numberLevels = input.ReadInt32();
                frames[i] = new Texture2D(graphicsDevice, width, height);
                for (int j = 0; j < numberLevels; j++)
                {
                    int count = input.ReadInt32();
                    byte[] data = input.ReadBytes(count);
                    byte[] tempByte = new byte[data.Length];
                    for (int a = 0; a < data.Length; a += 4)
                    {
                        tempByte[a] = data[a + 2];
                        tempByte[a + 1] = data[a + 1];
                        tempByte[a + 2] = data[a];
                        tempByte[a + 3] = data[a + 3];
                    }

                    frames[i].SetData(tempByte);
                }
            }
            input.Close();
            return GifAnimation.FromTextures(frames);
        }
    }
}

Apr 26, 2013 at 5:31 PM
thanks