2012-03-17 200 views
1

我编辑 XNA gif动画库 ,因为它不能正常工作 它加载gif文件,但用错了颜色XNA gif动画

这里的进口码

public override GifAnimationContent Import(string filename, ContentImporterContext context) 
    { 
     GifAnimationContent content = new GifAnimationContent(); 
     Image source = Image.FromFile(filename); 
     FrameDimension dimension = new FrameDimension(source.FrameDimensionsList[0]); 
     int frameCount = source.GetFrameCount(dimension); 
     content.Frames = new TextureData[frameCount]; 
     for (int i = 0; i < frameCount; i++) 
     { 
      source.SelectActiveFrame(dimension, i); 
      byte[] buffer = Quantizer.Quantize(source); 
      content.Frames[i].__1__SurfaceFormat = SurfaceFormat.Color; 
      content.Frames[i].__2__Width = source.Width; 
      content.Frames[i].__3__Height = source.Height; 
      content.Frames[i].__4__Levels = 1; 
      content.Frames[i].Data = buffer; 
     } 
     source.Dispose(); 
     return content; 
    } 

我认为,问题是SurfaceFormat.Color,因为Gif文件不支持任何其他 ,但索引调色板 ,但我无法弄清楚什么SurfaceFormat将是正确的或什么样的文件转换应该在gif图像上完成

请帮忙

谢谢。

+0

SurfaceFormat枚举: msdn.microsoft.com/en-us/library/microsoft.xna.framework.graphics.surfaceformat(v=xnagamestudio.40).aspx – MySqlError 2012-03-17 21:39:59

回答

1

我知道这大约晚了一百万年,但如果像我一样,在任何时候遇到这个问题,我都会在做一些研究之后解决它。

解决方案是打开量化器查看量化的来源,以及将指针字节中的字节分配给字节数组的部分,您可以将第一个和第三个字节(字节红色和蓝色的值),并应该修复它。

但是,既然我在这里,我可以分享我做的另一件事,那就是使用Marshal.Copy将指针中的字节复制到字节数组,从而大大加快了量化速度。我不知道这样做是否有任何缺点,或者这是不可取的情况,所以如果有人知道,我很乐意听到它。

所以只是改变

 BitmapData bitmapdata = image.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); 
     byte* numPtr = (byte*)bitmapdata.Scan0.ToPointer(); 
     int index = 0; 
     byte[] buffer = new byte[(source.Width * source.Height) * 4]; 
     for(int i = 0; i < source.Width; i++) { 
      for(int j = 0; j < source.Height; j++) { 
       buffer[index] = numPtr[index]; 
       index++; 
       buffer[index] = numPtr[index]; 
       index++; 
       buffer[index] = numPtr[index]; 
       index++; 
       buffer[index] = numPtr[index]; 
       index++; 
      } 
     } 
     image.UnlockBits(bitmapdata); 
     return buffer; 

 BitmapData bitmapdata = copy.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); 
     byte[] bytes = new byte[source.Width * source.Height * 4]; 

     //copy the bytes from the Scan0 pointer to our byte array 
     Marshal.Copy(bitmapdata.Scan0, bytes, 0, bytes.Length); 

     //swap the red and blue bytes 
     for(int a = 0; a < bytes.Length; a += 4) { 
      byte tmp = bytes[a]; 
      bytes[a] = bytes[a + 2]; 
      bytes[a + 2] = tmp; 
     } 

     copy.UnlockBits(bitmapdata); 

     return bytes; 

这个解决方案的伟大工程,是比原来的代码,如果你想在实时像我使用它是重要的更快是。为了进一步提高速度,我删除了将图像重新绘制到单独位图中的位,因为我不知道为什么它以此开始,并且我没有看到它的必要,但我可能是错的。

另外我不知道是否有更快的方法来交换红色和蓝色字节,我很想听听是否有更好的方法来做到这一点。一旦有人读这个反正...

+0

如果,每个机会,这个图书馆是开源的,我强烈建议,如果他们解决了这个问题,可以在这些更改中打开一个pull请求。 – Qix 2016-10-13 07:57:28

+0

xna gif动画库几乎没有更新好5年,我不确定这些更改对标准版本是否会造成影响。在使用[this]时,我只会解决这个问题(http://xboxforums.create.msdn.com/forums/t/57297.aspx)。 我不明白为什么这种颜色切换的事情甚至是一个问题,为什么做这个MOD的人没有注意到这个问题并修复它... – Bolt 2016-10-13 08:22:32

+0

为什么你仍然使用XNA?的xD – MySqlError 2016-10-14 11:11:02