Skip to content

Commit 7f3c8ff

Browse files
committed
Make sure the alpha flag in VP8X and VP8L are the same
1 parent e71fd82 commit 7f3c8ff

File tree

5 files changed

+34
-20
lines changed

5 files changed

+34
-20
lines changed

src/ImageSharp/Formats/Webp/BitWriter/BitWriterBase.cs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,6 @@ internal abstract class BitWriterBase
6363
/// </summary>
6464
public abstract void Finish();
6565

66-
/// <summary>
67-
/// Writes the encoded image to the stream.
68-
/// </summary>
69-
/// <param name="stream">The stream to write to.</param>
70-
/// <param name="exifProfile">The exif profile.</param>
71-
/// <param name="width">The width of the image.</param>
72-
/// <param name="height">The height of the image.</param>
73-
public abstract void WriteEncodedImageToStream(Stream stream, ExifProfile exifProfile, uint width, uint height);
74-
7566
protected void ResizeBuffer(int maxBytes, int sizeRequired)
7667
{
7768
int newSize = (3 * maxBytes) >> 1;
@@ -142,7 +133,8 @@ protected void WriteExifProfile(Stream stream, byte[] exifBytes)
142133
/// <param name="exifProfile">A exif profile or null, if it does not exist.</param>
143134
/// <param name="width">The width of the image.</param>
144135
/// <param name="height">The height of the image.</param>
145-
protected void WriteVp8XHeader(Stream stream, ExifProfile exifProfile, uint width, uint height)
136+
/// <param name="hasAlpha">Flag indicating, if a alpha channel is present.</param>
137+
protected void WriteVp8XHeader(Stream stream, ExifProfile exifProfile, uint width, uint height, bool hasAlpha)
146138
{
147139
if (width > MaxDimension || height > MaxDimension)
148140
{
@@ -162,6 +154,12 @@ protected void WriteVp8XHeader(Stream stream, ExifProfile exifProfile, uint widt
162154
flags |= 8;
163155
}
164156

157+
if (hasAlpha)
158+
{
159+
// Set alpha bit.
160+
flags |= 16;
161+
}
162+
165163
Span<byte> buf = this.scratchBuffer.AsSpan(0, 4);
166164
stream.Write(WebpConstants.Vp8XMagicBytes);
167165
BinaryPrimitives.WriteUInt32LittleEndian(buf, WebpConstants.Vp8XChunkSize);

src/ImageSharp/Formats/Webp/BitWriter/Vp8BitWriter.cs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -399,8 +399,15 @@ private void Flush()
399399
}
400400
}
401401

402-
/// <inheritdoc/>
403-
public override void WriteEncodedImageToStream(Stream stream, ExifProfile exifProfile, uint width, uint height)
402+
/// <summary>
403+
/// Writes the encoded image to the stream.
404+
/// </summary>
405+
/// <param name="stream">The stream to write to.</param>
406+
/// <param name="exifProfile">The exif profile.</param>
407+
/// <param name="width">The width of the image.</param>
408+
/// <param name="height">The height of the image.</param>
409+
/// <param name="hasAlpha">Flag indicating, if a alpha channel is present.</param>
410+
public void WriteEncodedImageToStream(Stream stream, ExifProfile exifProfile, uint width, uint height, bool hasAlpha)
404411
{
405412
bool isVp8X = false;
406413
byte[] exifBytes = null;
@@ -433,7 +440,7 @@ public override void WriteEncodedImageToStream(Stream stream, ExifProfile exifPr
433440
riffSize += WebpConstants.TagSize + WebpConstants.ChunkHeaderSize + vp8Size;
434441

435442
// Emit headers and partition #0
436-
this.WriteWebpHeaders(stream, size0, vp8Size, riffSize, isVp8X, width, height, exifProfile);
443+
this.WriteWebpHeaders(stream, size0, vp8Size, riffSize, isVp8X, width, height, exifProfile, hasAlpha);
437444
bitWriterPartZero.WriteToStream(stream);
438445

439446
// Write the encoded image to the stream.
@@ -616,14 +623,14 @@ private void CodeIntraModes(Vp8BitWriter bitWriter)
616623
while (it.Next());
617624
}
618625

619-
private void WriteWebpHeaders(Stream stream, uint size0, uint vp8Size, uint riffSize, bool isVp8X, uint width, uint height, ExifProfile exifProfile)
626+
private void WriteWebpHeaders(Stream stream, uint size0, uint vp8Size, uint riffSize, bool isVp8X, uint width, uint height, ExifProfile exifProfile, bool hasAlpha)
620627
{
621628
this.WriteRiffHeader(stream, riffSize);
622629

623630
// Write VP8X, header if necessary.
624631
if (isVp8X)
625632
{
626-
this.WriteVp8XHeader(stream, exifProfile, width, height);
633+
this.WriteVp8XHeader(stream, exifProfile, width, height, hasAlpha);
627634
}
628635

629636
this.WriteVp8Header(stream, vp8Size);

src/ImageSharp/Formats/Webp/BitWriter/Vp8LBitWriter.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,15 @@ public override void Finish()
127127
this.used = 0;
128128
}
129129

130-
/// <inheritdoc/>
131-
public override void WriteEncodedImageToStream(Stream stream, ExifProfile exifProfile, uint width, uint height)
130+
/// <summary>
131+
/// Writes the encoded image to the stream.
132+
/// </summary>
133+
/// <param name="stream">The stream to write to.</param>
134+
/// <param name="exifProfile">The exif profile.</param>
135+
/// <param name="width">The width of the image.</param>
136+
/// <param name="height">The height of the image.</param>
137+
/// <param name="hasAlpha">Flag indicating, if a alpha channel is present.</param>
138+
public void WriteEncodedImageToStream(Stream stream, ExifProfile exifProfile, uint width, uint height, bool hasAlpha)
132139
{
133140
bool isVp8X = false;
134141
byte[] exifBytes = null;
@@ -153,7 +160,7 @@ public override void WriteEncodedImageToStream(Stream stream, ExifProfile exifPr
153160
// Write VP8X, header if necessary.
154161
if (isVp8X)
155162
{
156-
this.WriteVp8XHeader(stream, exifProfile, width, height);
163+
this.WriteVp8XHeader(stream, exifProfile, width, height, hasAlpha);
157164
}
158165

159166
// Write magic bytes indicating its a lossless webp.

src/ImageSharp/Formats/Webp/Lossless/Vp8LEncoder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ public void Encode<TPixel>(Image<TPixel> image, Stream stream)
234234
this.EncodeStream(image);
235235

236236
// Write bytes from the bitwriter buffer to the stream.
237-
this.bitWriter.WriteEncodedImageToStream(stream, image.Metadata.ExifProfile, (uint)width, (uint)height);
237+
this.bitWriter.WriteEncodedImageToStream(stream, image.Metadata.ExifProfile, (uint)width, (uint)height, hasAlpha);
238238
}
239239

240240
/// <summary>

src/ImageSharp/Formats/Webp/Lossy/Vp8Encoder.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,8 @@ public void Encode<TPixel>(Image<TPixel> image, Stream stream)
317317
this.bitWriter = new Vp8BitWriter(expectedSize, this);
318318

319319
// TODO: EncodeAlpha();
320+
bool hasAlpha = false;
321+
320322
// Stats-collection loop.
321323
this.StatLoop(width, height, yStride, uvStride);
322324
it.Init();
@@ -348,7 +350,7 @@ public void Encode<TPixel>(Image<TPixel> image, Stream stream)
348350

349351
// Write bytes from the bitwriter buffer to the stream.
350352
image.Metadata.SyncProfiles();
351-
this.bitWriter.WriteEncodedImageToStream(stream, image.Metadata.ExifProfile, (uint)width, (uint)height);
353+
this.bitWriter.WriteEncodedImageToStream(stream, image.Metadata.ExifProfile, (uint)width, (uint)height, hasAlpha);
352354
}
353355

354356
/// <inheritdoc/>

0 commit comments

Comments
 (0)