@@ -71,9 +71,6 @@ private unsafe struct DdsHeader
7171 public uint dwReserved2 ;
7272 }
7373
74- [ ThreadStatic ]
75- private static byte [ ] _sPixelBuffer ;
76-
7774 private static unsafe int WriteDdsHeader ( ValveTextureFile vtf , int mip , byte [ ] buffer )
7875 {
7976 var header = new DdsHeader ( ) ;
@@ -136,14 +133,7 @@ public static MagickImage DecodeImage(ValveTextureFile vtf, int mip, int frame,
136133 }
137134
138135 var totalLength = dataLength + 128 ;
139-
140- if ( _sPixelBuffer == null || _sPixelBuffer . Length < totalLength )
141- {
142- var powerOf2 = 256 ;
143- while ( powerOf2 < totalLength ) powerOf2 <<= 1 ;
144-
145- _sPixelBuffer = new byte [ powerOf2 ] ;
146- }
136+ var buffer = new byte [ totalLength ] ;
147137
148138 var offset = 0 ;
149139 var width = Math . Max ( 1 , vtf . Header . Width >> mip ) ;
@@ -161,10 +151,15 @@ public static MagickImage DecodeImage(ValveTextureFile vtf, int mip, int frame,
161151 case TextureFormat . DXT3 :
162152 case TextureFormat . DXT5 :
163153 readSettings . Format = MagickFormat . Dds ;
164- offset = WriteDdsHeader ( vtf , mip , _sPixelBuffer ) ;
154+ offset = WriteDdsHeader ( vtf , mip , buffer ) ;
165155 break ;
166156 case TextureFormat . I8 :
167157 readSettings . Format = MagickFormat . Gray ;
158+ readSettings . PixelStorage = new PixelStorageSettings
159+ {
160+ StorageType = StorageType . Char ,
161+ Mapping = "R"
162+ } ;
168163 break ;
169164 case TextureFormat . IA88 :
170165 readSettings . Format = MagickFormat . Gray ;
@@ -196,7 +191,7 @@ public static MagickImage DecodeImage(ValveTextureFile vtf, int mip, int frame,
196191 throw new NotImplementedException ( ) ;
197192 }
198193
199- vtf . GetHiResPixelData ( mip , frame , face , zslice , _sPixelBuffer , offset ) ;
194+ vtf . GetHiResPixelData ( mip , frame , face , zslice , buffer , offset ) ;
200195
201196 // Convert 16bpp to 24bpp
202197 switch ( vtf . Header . HiResFormat )
@@ -205,16 +200,16 @@ public static MagickImage DecodeImage(ValveTextureFile vtf, int mip, int frame,
205200 case TextureFormat . BGR565 :
206201 for ( var i = width * height - 1 ; i >= 0 ; -- i )
207202 {
208- var pixel = ( ushort ) ( _sPixelBuffer [ i * 2 ] | ( _sPixelBuffer [ i * 2 + 1 ] << 8 ) ) ;
203+ var pixel = ( ushort ) ( buffer [ i * 2 ] | ( buffer [ i * 2 + 1 ] << 8 ) ) ;
209204
210- _sPixelBuffer [ i * 3 ] = ( byte ) ( ( pixel & 31 ) / 31f * 255f ) ;
211- _sPixelBuffer [ i * 3 + 1 ] = ( byte ) ( ( ( pixel >> 5 ) & 63 ) / 63f * 255f ) ;
212- _sPixelBuffer [ i * 3 + 2 ] = ( byte ) ( ( ( pixel >> 11 ) & 31 ) / 31f * 255f ) ;
205+ buffer [ i * 3 ] = ( byte ) ( ( pixel & 31 ) / 31f * 255f ) ;
206+ buffer [ i * 3 + 1 ] = ( byte ) ( ( ( pixel >> 5 ) & 63 ) / 63f * 255f ) ;
207+ buffer [ i * 3 + 2 ] = ( byte ) ( ( ( pixel >> 11 ) & 31 ) / 31f * 255f ) ;
213208 }
214209 break ;
215210 }
216211
217- var img = new MagickImage ( _sPixelBuffer , readSettings ) ;
212+ var img = new MagickImage ( buffer , readSettings ) ;
218213
219214 if ( img . Width != width || img . Height != height )
220215 {
0 commit comments