@@ -71,7 +71,11 @@ private static void ProcessGrayscale<TPixel>(Configuration configuration, Buffer
7171
7272 for ( int y = 0 ; y < height ; y ++ )
7373 {
74- stream . Read ( rowSpan ) ;
74+ if ( stream . Read ( rowSpan ) < rowSpan . Length )
75+ {
76+ return ;
77+ }
78+
7579 Span < TPixel > pixelSpan = pixels . DangerousGetRowSpan ( y ) ;
7680 PixelOperations < TPixel > . Instance . FromL8Bytes (
7781 configuration ,
@@ -93,7 +97,11 @@ private static void ProcessWideGrayscale<TPixel>(Configuration configuration, Bu
9397
9498 for ( int y = 0 ; y < height ; y ++ )
9599 {
96- stream . Read ( rowSpan ) ;
100+ if ( stream . Read ( rowSpan ) < rowSpan . Length )
101+ {
102+ return ;
103+ }
104+
97105 Span < TPixel > pixelSpan = pixels . DangerousGetRowSpan ( y ) ;
98106 PixelOperations < TPixel > . Instance . FromL16Bytes (
99107 configuration ,
@@ -115,7 +123,11 @@ private static void ProcessRgb<TPixel>(Configuration configuration, Buffer2D<TPi
115123
116124 for ( int y = 0 ; y < height ; y ++ )
117125 {
118- stream . Read ( rowSpan ) ;
126+ if ( stream . Read ( rowSpan ) < rowSpan . Length )
127+ {
128+ return ;
129+ }
130+
119131 Span < TPixel > pixelSpan = pixels . DangerousGetRowSpan ( y ) ;
120132 PixelOperations < TPixel > . Instance . FromRgb24Bytes (
121133 configuration ,
@@ -137,7 +149,11 @@ private static void ProcessWideRgb<TPixel>(Configuration configuration, Buffer2D
137149
138150 for ( int y = 0 ; y < height ; y ++ )
139151 {
140- stream . Read ( rowSpan ) ;
152+ if ( stream . Read ( rowSpan ) < rowSpan . Length )
153+ {
154+ return ;
155+ }
156+
141157 Span < TPixel > pixelSpan = pixels . DangerousGetRowSpan ( y ) ;
142158 PixelOperations < TPixel > . Instance . FromRgb48Bytes (
143159 configuration ,
@@ -152,7 +168,6 @@ private static void ProcessBlackAndWhite<TPixel>(Configuration configuration, Bu
152168 {
153169 int width = pixels . Width ;
154170 int height = pixels . Height ;
155- int startBit = 0 ;
156171 MemoryAllocator allocator = configuration . MemoryAllocator ;
157172 using IMemoryOwner < L8 > row = allocator . Allocate < L8 > ( width ) ;
158173 Span < L8 > rowSpan = row . GetSpan ( ) ;
@@ -162,23 +177,17 @@ private static void ProcessBlackAndWhite<TPixel>(Configuration configuration, Bu
162177 for ( int x = 0 ; x < width ; )
163178 {
164179 int raw = stream . ReadByte ( ) ;
165- int bit = startBit ;
166- startBit = 0 ;
167- for ( ; bit < 8 ; bit ++ )
180+ if ( raw < 0 )
181+ {
182+ return ;
183+ }
184+
185+ int stopBit = Math . Min ( 8 , width - x ) ;
186+ for ( int bit = 0 ; bit < stopBit ; bit ++ )
168187 {
169188 bool bitValue = ( raw & ( 0x80 >> bit ) ) != 0 ;
170189 rowSpan [ x ] = bitValue ? black : white ;
171190 x ++ ;
172- if ( x == width )
173- {
174- startBit = ( bit + 1 ) & 7 ; // Round off to below 8.
175- if ( startBit != 0 )
176- {
177- stream . Seek ( - 1 , System . IO . SeekOrigin . Current ) ;
178- }
179-
180- break ;
181- }
182191 }
183192 }
184193
0 commit comments