Skip to content

Commit 4a494c7

Browse files
Merge branch 'main' into main
2 parents 07e6597 + 2738b3a commit 4a494c7

File tree

109 files changed

+624
-409
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

109 files changed

+624
-409
lines changed

src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,7 @@ private void Write1BitPixelData<TPixel>(Configuration configuration, Stream stre
668668

669669
if (quantizedPixelRow.Length % 8 != 0)
670670
{
671-
int startIdx = quantizedPixelRow.Length - 7;
671+
int startIdx = quantizedPixelRow.Length - (quantizedPixelRow.Length % 8);
672672
endIdx = quantizedPixelRow.Length;
673673
Write1BitPalette(stream, startIdx, endIdx, quantizedPixelRow);
674674
}

src/ImageSharp/Formats/Pbm/BinaryDecoder.cs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,6 @@ private static void ProcessBlackAndWhite<TPixel>(Configuration configuration, Bu
152152
{
153153
int width = pixels.Width;
154154
int height = pixels.Height;
155-
int startBit = 0;
156155
MemoryAllocator allocator = configuration.MemoryAllocator;
157156
using IMemoryOwner<L8> row = allocator.Allocate<L8>(width);
158157
Span<L8> rowSpan = row.GetSpan();
@@ -162,23 +161,12 @@ private static void ProcessBlackAndWhite<TPixel>(Configuration configuration, Bu
162161
for (int x = 0; x < width;)
163162
{
164163
int raw = stream.ReadByte();
165-
int bit = startBit;
166-
startBit = 0;
167-
for (; bit < 8; bit++)
164+
int stopBit = Math.Min(8, width - x);
165+
for (int bit = 0; bit < stopBit; bit++)
168166
{
169167
bool bitValue = (raw & (0x80 >> bit)) != 0;
170168
rowSpan[x] = bitValue ? black : white;
171169
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-
}
182170
}
183171
}
184172

src/ImageSharp/Formats/Pbm/BinaryEncoder.cs

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,25 +33,40 @@ public static void WritePixels<TPixel>(Configuration configuration, Stream strea
3333
{
3434
WriteGrayscale(configuration, stream, image);
3535
}
36-
else
36+
else if (componentType == PbmComponentType.Short)
3737
{
3838
WriteWideGrayscale(configuration, stream, image);
3939
}
40+
else
41+
{
42+
throw new ImageFormatException("Component type not supported for Grayscale PBM.");
43+
}
4044
}
4145
else if (colorType == PbmColorType.Rgb)
4246
{
4347
if (componentType == PbmComponentType.Byte)
4448
{
4549
WriteRgb(configuration, stream, image);
4650
}
47-
else
51+
else if (componentType == PbmComponentType.Short)
4852
{
4953
WriteWideRgb(configuration, stream, image);
5054
}
55+
else
56+
{
57+
throw new ImageFormatException("Component type not supported for Color PBM.");
58+
}
5159
}
5260
else
5361
{
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+
}
5570
}
5671
}
5772

@@ -164,8 +179,6 @@ private static void WriteBlackAndWhite<TPixel>(Configuration configuration, Stre
164179
using IMemoryOwner<L8> row = allocator.Allocate<L8>(width);
165180
Span<L8> rowSpan = row.GetSpan();
166181

167-
int previousValue = 0;
168-
int startBit = 0;
169182
for (int y = 0; y < height; y++)
170183
{
171184
Span<TPixel> pixelSpan = pixelBuffer.DangerousGetRowSpan(y);
@@ -177,28 +190,19 @@ private static void WriteBlackAndWhite<TPixel>(Configuration configuration, Stre
177190

178191
for (int x = 0; x < width;)
179192
{
180-
int value = previousValue;
181-
for (int i = startBit; i < 8; i++)
193+
int value = 0;
194+
int stopBit = Math.Min(8, width - x);
195+
for (int i = 0; i < stopBit; i++)
182196
{
183197
if (rowSpan[x].PackedValue < 128)
184198
{
185199
value |= 0x80 >> i;
186200
}
187201

188202
x++;
189-
if (x == width)
190-
{
191-
previousValue = value;
192-
startBit = (i + 1) & 7; // Round off to below 8.
193-
break;
194-
}
195203
}
196204

197-
if (startBit == 0)
198-
{
199-
stream.WriteByte((byte)value);
200-
previousValue = 0;
201-
}
205+
stream.WriteByte((byte)value);
202206
}
203207
}
204208
}

src/ImageSharp/Formats/Tiff/Compression/Decompressors/T6BitReader.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,29 @@ public bool ReadNextCodeWord()
125125
if (value == Len7Code0000000.Code)
126126
{
127127
this.Code = Len7Code0000000;
128-
return false;
128+
129+
// We do not support Extensions1D codes, but some encoders (scanner from epson) write a premature EOL code,
130+
// which at this point cannot be distinguished from the marker, because we read the data bit by bit.
131+
// Read the next 5 bit, if its a EOL code return true, indicating its the end of the image.
132+
if (this.ReadValue(5) == 1)
133+
{
134+
return true;
135+
}
136+
137+
throw new NotSupportedException("ccitt extensions 1D codes are not supported.");
129138
}
130139

131140
if (value == Len7Code0000001.Code)
132141
{
133142
this.Code = Len7Code0000001;
134-
return false;
143+
144+
// Same as above, we do not support Extensions2D codes, but it could be a EOL instead.
145+
if (this.ReadValue(5) == 1)
146+
{
147+
return true;
148+
}
149+
150+
throw new NotSupportedException("ccitt extensions 2D codes are not supported.");
135151
}
136152

137153
if (value == Len7Code0000011.Code)

src/ImageSharp/Formats/Tiff/Compression/HorizontalPredictor.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public static void Undo(Span<byte> pixelBytes, int width, TiffColorType colorTyp
4444
UndoRgb24Bit(pixelBytes, width);
4545
break;
4646
case TiffColorType.Rgba8888:
47+
case TiffColorType.Cmyk:
4748
UndoRgba32Bit(pixelBytes, width);
4849
break;
4950
case TiffColorType.Rgb161616:

src/ImageSharp/PixelFormats/PixelImplementations/Rgb48.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
namespace SixLabors.ImageSharp.PixelFormats;
99

1010
/// <summary>
11-
/// Packed pixel type containing three 16-bit unsigned normalized values ranging from 0 to 635535.
11+
/// Packed pixel type containing three 16-bit unsigned normalized values ranging from 0 to 65535.
1212
/// <para>
1313
/// Ranges from [0, 0, 0, 1] to [1, 1, 1, 1] in vector form.
1414
/// </para>

src/ImageSharp/Primitives/LongRational.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,11 @@ public string ToString(IFormatProvider provider)
131131
/// <param name="bestPrecision">Whether to use the best possible precision when parsing the value.</param>
132132
public static LongRational FromDouble(double value, bool bestPrecision)
133133
{
134+
if (value == 0.0)
135+
{
136+
return new LongRational(0, 1);
137+
}
138+
134139
if (double.IsNaN(value))
135140
{
136141
return new LongRational(0, 0);
@@ -201,11 +206,6 @@ public LongRational Simplify()
201206
return this;
202207
}
203208

204-
if (this.Numerator == 0)
205-
{
206-
return new LongRational(0, 0);
207-
}
208-
209209
if (this.Numerator == this.Denominator)
210210
{
211211
return new LongRational(1, 1);

0 commit comments

Comments
 (0)