@@ -33,25 +33,40 @@ public static void WritePixels<TPixel>(Configuration configuration, Stream strea
33
33
{
34
34
WriteGrayscale ( configuration , stream , image ) ;
35
35
}
36
- else
36
+ else if ( componentType == PbmComponentType . Short )
37
37
{
38
38
WriteWideGrayscale ( configuration , stream , image ) ;
39
39
}
40
+ else
41
+ {
42
+ throw new ImageFormatException ( "Component type not supported for Grayscale PBM." ) ;
43
+ }
40
44
}
41
45
else if ( colorType == PbmColorType . Rgb )
42
46
{
43
47
if ( componentType == PbmComponentType . Byte )
44
48
{
45
49
WriteRgb ( configuration , stream , image ) ;
46
50
}
47
- else
51
+ else if ( componentType == PbmComponentType . Short )
48
52
{
49
53
WriteWideRgb ( configuration , stream , image ) ;
50
54
}
55
+ else
56
+ {
57
+ throw new ImageFormatException ( "Component type not supported for Color PBM." ) ;
58
+ }
51
59
}
52
60
else
53
61
{
54
- WriteBlackAndWhite ( configuration , stream , image ) ;
62
+ if ( componentType == PbmComponentType . Bit )
63
+ {
64
+ WriteBlackAndWhite ( configuration , stream , image ) ;
65
+ }
66
+ else
67
+ {
68
+ throw new ImageFormatException ( "Component type not supported for Black & White PBM." ) ;
69
+ }
55
70
}
56
71
}
57
72
@@ -165,7 +180,6 @@ private static void WriteBlackAndWhite<TPixel>(Configuration configuration, Stre
165
180
Span < L8 > rowSpan = row . GetSpan ( ) ;
166
181
167
182
int previousValue = 0 ;
168
- int startBit = 0 ;
169
183
for ( int y = 0 ; y < height ; y ++ )
170
184
{
171
185
Span < TPixel > pixelSpan = pixelBuffer . DangerousGetRowSpan ( y ) ;
@@ -178,27 +192,23 @@ private static void WriteBlackAndWhite<TPixel>(Configuration configuration, Stre
178
192
for ( int x = 0 ; x < width ; )
179
193
{
180
194
int value = previousValue ;
181
- for ( int i = startBit ; i < 8 ; i ++ )
195
+ for ( int i = 0 ; i < 8 ; i ++ )
182
196
{
183
197
if ( rowSpan [ x ] . PackedValue < 128 )
184
198
{
185
199
value |= 0x80 >> i ;
186
200
}
187
201
188
202
x ++ ;
203
+ // End each row on a byte boundary.
189
204
if ( x == width )
190
205
{
191
- previousValue = value ;
192
- startBit = ( i + 1 ) & 7 ; // Round off to below 8.
193
206
break ;
194
207
}
195
208
}
196
209
197
- if ( startBit == 0 )
198
- {
199
- stream . WriteByte ( ( byte ) value ) ;
200
- previousValue = 0 ;
201
- }
210
+ stream . WriteByte ( ( byte ) value ) ;
211
+ previousValue = 0 ;
202
212
}
203
213
}
204
214
}
0 commit comments