@@ -72,7 +72,11 @@ private static void ProcessGrayscale<TPixel>(Configuration configuration, Buffer
7272
7373 for ( int y = 0 ; y < height ; y ++ )
7474 {
75- stream . Read ( rowSpan ) ;
75+ if ( stream . Read ( rowSpan ) < rowSpan . Length )
76+ {
77+ return ;
78+ }
79+
7680 Span < TPixel > pixelSpan = pixels . DangerousGetRowSpan ( y ) ;
7781 PixelOperations < TPixel > . Instance . FromL8Bytes (
7882 configuration ,
@@ -94,7 +98,11 @@ private static void ProcessWideGrayscale<TPixel>(Configuration configuration, Bu
9498
9599 for ( int y = 0 ; y < height ; y ++ )
96100 {
97- stream . Read ( rowSpan ) ;
101+ if ( stream . Read ( rowSpan ) < rowSpan . Length )
102+ {
103+ return ;
104+ }
105+
98106 Span < TPixel > pixelSpan = pixels . DangerousGetRowSpan ( y ) ;
99107 PixelOperations < TPixel > . Instance . FromL16Bytes (
100108 configuration ,
@@ -116,7 +124,11 @@ private static void ProcessRgb<TPixel>(Configuration configuration, Buffer2D<TPi
116124
117125 for ( int y = 0 ; y < height ; y ++ )
118126 {
119- stream . Read ( rowSpan ) ;
127+ if ( stream . Read ( rowSpan ) < rowSpan . Length )
128+ {
129+ return ;
130+ }
131+
120132 Span < TPixel > pixelSpan = pixels . DangerousGetRowSpan ( y ) ;
121133 PixelOperations < TPixel > . Instance . FromRgb24Bytes (
122134 configuration ,
@@ -138,7 +150,11 @@ private static void ProcessWideRgb<TPixel>(Configuration configuration, Buffer2D
138150
139151 for ( int y = 0 ; y < height ; y ++ )
140152 {
141- stream . Read ( rowSpan ) ;
153+ if ( stream . Read ( rowSpan ) < rowSpan . Length )
154+ {
155+ return ;
156+ }
157+
142158 Span < TPixel > pixelSpan = pixels . DangerousGetRowSpan ( y ) ;
143159 PixelOperations < TPixel > . Instance . FromRgb48Bytes (
144160 configuration ,
@@ -153,7 +169,6 @@ private static void ProcessBlackAndWhite<TPixel>(Configuration configuration, Bu
153169 {
154170 int width = pixels . Width ;
155171 int height = pixels . Height ;
156- int startBit = 0 ;
157172 MemoryAllocator allocator = configuration . MemoryAllocator ;
158173 using IMemoryOwner < L8 > row = allocator . Allocate < L8 > ( width ) ;
159174 Span < L8 > rowSpan = row . GetSpan ( ) ;
@@ -163,23 +178,17 @@ private static void ProcessBlackAndWhite<TPixel>(Configuration configuration, Bu
163178 for ( int x = 0 ; x < width ; )
164179 {
165180 int raw = stream . ReadByte ( ) ;
166- int bit = startBit ;
167- startBit = 0 ;
168- for ( ; bit < 8 ; bit ++ )
181+ if ( raw < 0 )
182+ {
183+ return ;
184+ }
185+
186+ int stopBit = Math . Min ( 8 , width - x ) ;
187+ for ( int bit = 0 ; bit < stopBit ; bit ++ )
169188 {
170189 bool bitValue = ( raw & ( 0x80 >> bit ) ) != 0 ;
171190 rowSpan [ x ] = bitValue ? black : white ;
172191 x ++ ;
173- if ( x == width )
174- {
175- startBit = ( bit + 1 ) & 7 ; // Round off to below 8.
176- if ( startBit != 0 )
177- {
178- stream . Seek ( - 1 , System . IO . SeekOrigin . Current ) ;
179- }
180-
181- break ;
182- }
183192 }
184193 }
185194
0 commit comments