33using System . Drawing ;
44using System . Drawing . Imaging ;
55using System . IO ;
6+ using System . Linq ;
67using Microsoft . Xna . Framework . Graphics ;
78using XnaColor = Microsoft . Xna . Framework . Color ;
89
@@ -74,10 +75,11 @@ public static TextureGIF FromGIFFile(FileStream stream, GraphicsDevice graphicsD
7475 for ( int x = 0 ; x < width ; x ++ )
7576 for ( int y = 0 ; y < height ; y ++ )
7677 framePixels [ x , y ] = gifFrame . GetPixel ( x , y ) ;
78+ framePixels = Flip ( Rotate ( framePixels ) ) ;
7779
7880 int flatIndex = 0 ;
79- for ( int x = 0 ; x < framePixels . GetUpperBound ( 0 ) ; x ++ )
80- for ( int y = 0 ; y < framePixels . GetUpperBound ( 1 ) ; y ++ )
81+ for ( int x = 0 ; x < height ; x ++ )
82+ for ( int y = 0 ; y < width ; y ++ )
8183 flattenedPixels [ flatIndex ++ ] = framePixels [ x , y ] ;
8284
8385 XnaColor [ ] usableColors = new XnaColor [ flattenedPixels . Length ] ;
@@ -93,8 +95,38 @@ public static TextureGIF FromGIFFile(FileStream stream, GraphicsDevice graphicsD
9395 frame . Dispose ( ) ;
9496 }
9597
96- return new TextureGIF ( trueFrames . ToArray ( ) , ticksPerFrame ) ;
98+ return FromArray ( trueFrames . ToArray ( ) , ticksPerFrame ) ;
9799 }
98100 }
101+
102+ private static Color [ , ] Rotate ( Color [ , ] src )
103+ {
104+ int width = src . GetUpperBound ( 0 ) + 1 ;
105+ int height = src . GetUpperBound ( 1 ) + 1 ;
106+ Color [ , ] value = new Color [ height , width ] ;
107+
108+ for ( int row = 0 ; row < height ; row ++ )
109+ for ( int col = 0 ; col < width ; col ++ )
110+ {
111+ int newCol = height - ( row + 1 ) ;
112+ value [ newCol , col ] = src [ col , row ] ;
113+ }
114+
115+
116+ return value ;
117+ }
118+
119+ private static Color [ , ] Flip ( Color [ , ] src )
120+ {
121+ int rows = src . GetUpperBound ( 0 ) + 1 ;
122+ int cols = src . GetUpperBound ( 1 ) + 1 ;
123+ Color [ , ] value = new Color [ rows , cols ] ;
124+
125+ for ( int i = 0 ; i < rows ; i ++ )
126+ for ( int j = 0 ; j < cols ; j ++ )
127+ value [ rows - 1 - i , j ] = src [ i , j ] ;
128+
129+ return value ;
130+ }
99131 }
100- }
132+ }
0 commit comments