Browse Source

Fixing declarations

qoi
LuisAlfredo92 3 years ago
parent
commit
3f1fe69c2b
No known key found for this signature in database GPG Key ID: 13A8436905993B8F
  1. 29
      src/ImageSharp/Formats/Qoi/QoiDecoderCore.cs
  2. 26
      src/ImageSharp/Formats/Qoi/QoiEncoderCore.cs

29
src/ImageSharp/Formats/Qoi/QoiDecoderCore.cs

@ -56,6 +56,9 @@ internal class QoiDecoderCore : IImageDecoderInternals
VerticalResolution = this.header.Height, VerticalResolution = this.header.Height,
ResolutionUnits = PixelResolutionUnit.AspectRatio ResolutionUnits = PixelResolutionUnit.AspectRatio
}; };
QoiMetadata qoiMetadata = metadata.GetQoiMetadata();
qoiMetadata.Channels = this.header.Channels;
qoiMetadata.ColorSpace = this.header.ColorSpace;
Image<TPixel> image = new(this.configuration, (int)this.header.Width, (int)this.header.Height, metadata); Image<TPixel> image = new(this.configuration, (int)this.header.Width, (int)this.header.Height, metadata);
Buffer2D<TPixel> pixels = image.GetRootFramePixelBuffer(); Buffer2D<TPixel> pixels = image.GetRootFramePixelBuffer();
@ -112,8 +115,8 @@ internal class QoiDecoderCore : IImageDecoderInternals
} }
// These numbers are in Big Endian so we have to reverse them to get the real number // These numbers are in Big Endian so we have to reverse them to get the real number
uint width = BinaryPrimitives.ReadUInt32BigEndian(widthBytes), uint width = BinaryPrimitives.ReadUInt32BigEndian(widthBytes);
height = BinaryPrimitives.ReadUInt32BigEndian(heightBytes); uint height = BinaryPrimitives.ReadUInt32BigEndian(heightBytes);
if (width == 0 || height == 0) if (width == 0 || height == 0)
{ {
throw new InvalidImageContentException( throw new InvalidImageContentException(
@ -126,8 +129,6 @@ internal class QoiDecoderCore : IImageDecoderInternals
ThrowInvalidImageContentException(); ThrowInvalidImageContentException();
} }
PixelTypeInfo pixelType = new(8 * channels);
int colorSpace = stream.ReadByte(); int colorSpace = stream.ReadByte();
if (colorSpace is -1 or (not 0 and not 1)) if (colorSpace is -1 or (not 0 and not 1))
{ {
@ -201,9 +202,9 @@ internal class QoiDecoderCore : IImageDecoderInternals
// Get one pixel from the difference (-2..1) of the previous pixel // Get one pixel from the difference (-2..1) of the previous pixel
case QoiChunk.QoiOpDiff: case QoiChunk.QoiOpDiff:
byte redDifference = (byte)((operationByte & 0b00110000) >> 4), byte redDifference = (byte)((operationByte & 0b00110000) >> 4);
greenDifference = (byte)((operationByte & 0b00001100) >> 2), byte greenDifference = (byte)((operationByte & 0b00001100) >> 2);
blueDifference = (byte)(operationByte & 0b00000011); byte blueDifference = (byte)(operationByte & 0b00000011);
readPixel = previousPixel with readPixel = previousPixel with
{ {
R = (byte)Numerics.Modulo256(previousPixel.R + (redDifference - 2)), R = (byte)Numerics.Modulo256(previousPixel.R + (redDifference - 2)),
@ -218,13 +219,13 @@ internal class QoiDecoderCore : IImageDecoderInternals
// Get green difference in 6 bits and red and blue differences // Get green difference in 6 bits and red and blue differences
// depending on the green one // depending on the green one
case QoiChunk.QoiOpLuma: case QoiChunk.QoiOpLuma:
byte diffGreen = (byte)(operationByte & 0b00111111), byte diffGreen = (byte)(operationByte & 0b00111111);
currentGreen = (byte)Numerics.Modulo256(previousPixel.G + (diffGreen - 32)), byte currentGreen = (byte)Numerics.Modulo256(previousPixel.G + (diffGreen - 32));
nextByte = (byte)stream.ReadByte(), byte nextByte = (byte)stream.ReadByte();
diffRedDG = (byte)(nextByte >> 4), byte diffRedDG = (byte)(nextByte >> 4);
diffBlueDG = (byte)(nextByte & 0b00001111), byte diffBlueDG = (byte)(nextByte & 0b00001111);
currentRed = (byte)Numerics.Modulo256(diffRedDG - 8 + (diffGreen - 32) + previousPixel.R), byte currentRed = (byte)Numerics.Modulo256(diffRedDG - 8 + (diffGreen - 32) + previousPixel.R);
currentBlue = (byte)Numerics.Modulo256(diffBlueDG - 8 + (diffGreen - 32) + previousPixel.B); byte currentBlue = (byte)Numerics.Modulo256(diffBlueDG - 8 + (diffGreen - 32) + previousPixel.B);
readPixel = previousPixel with { R = currentRed, B = currentBlue, G = currentGreen }; readPixel = previousPixel with { R = currentRed, B = currentBlue, G = currentGreen };
pixel.FromRgba32(readPixel); pixel.FromRgba32(readPixel);
pixelArrayPosition = GetArrayPosition(readPixel); pixelArrayPosition = GetArrayPosition(readPixel);

26
src/ImageSharp/Formats/Qoi/QoiEncoderCore.cs

@ -141,9 +141,9 @@ public class QoiEncoderCore : IImageEncoderInternals
// Since it wasn't found on the previously seen pixels, we save it // Since it wasn't found on the previously seen pixels, we save it
previouslySeenPixels[pixelArrayPosition] = currentRgba32; previouslySeenPixels[pixelArrayPosition] = currentRgba32;
sbyte diffRed = (sbyte)(currentRgba32.R - previousPixel.R), sbyte diffRed = (sbyte)(currentRgba32.R - previousPixel.R);
diffGreen = (sbyte)(currentRgba32.G - previousPixel.G), sbyte diffGreen = (sbyte)(currentRgba32.G - previousPixel.G);
diffBlue = (sbyte)(currentRgba32.B - previousPixel.B); sbyte diffBlue = (sbyte)(currentRgba32.B - previousPixel.B);
// If so, we do a QOI_OP_DIFF // If so, we do a QOI_OP_DIFF
if (diffRed is >= -2 and <= 1 && if (diffRed is >= -2 and <= 1 &&
@ -152,27 +152,27 @@ public class QoiEncoderCore : IImageEncoderInternals
currentRgba32.A == previousPixel.A) currentRgba32.A == previousPixel.A)
{ {
// Bottom limit is -2, so we add 2 to make it equal to 0 // Bottom limit is -2, so we add 2 to make it equal to 0
byte dr = (byte)(diffRed + 2), byte dr = (byte)(diffRed + 2);
dg = (byte)(diffGreen + 2), byte dg = (byte)(diffGreen + 2);
db = (byte)(diffBlue + 2), byte db = (byte)(diffBlue + 2);
valueToWrite = (byte)((byte)QoiChunk.QoiOpDiff | (dr << 4) | (dg << 2) | db); byte valueToWrite = (byte)((byte)QoiChunk.QoiOpDiff | (dr << 4) | (dg << 2) | db);
stream.WriteByte(valueToWrite); stream.WriteByte(valueToWrite);
} }
else else
{ {
// else, we check if the green difference is less than -32..31 and the rest -8..7 // else, we check if the green difference is less than -32..31 and the rest -8..7
// If so, we do a QOI_OP_LUMA // If so, we do a QOI_OP_LUMA
sbyte diffRedGreen = (sbyte)(diffRed - diffGreen), sbyte diffRedGreen = (sbyte)(diffRed - diffGreen);
diffBlueGreen = (sbyte)(diffBlue - diffGreen); sbyte diffBlueGreen = (sbyte)(diffBlue - diffGreen);
if (diffGreen is >= -32 and <= 31 && if (diffGreen is >= -32 and <= 31 &&
diffRedGreen is >= -8 and <= 7 && diffRedGreen is >= -8 and <= 7 &&
diffBlueGreen is >= -8 and <= 7 && diffBlueGreen is >= -8 and <= 7 &&
currentRgba32.A == previousPixel.A) currentRgba32.A == previousPixel.A)
{ {
byte dr_dg = (byte)(diffRedGreen + 8), byte dr_dg = (byte)(diffRedGreen + 8);
db_dg = (byte)(diffBlueGreen + 8), byte db_dg = (byte)(diffBlueGreen + 8);
byteToWrite1 = (byte)((byte)QoiChunk.QoiOpLuma | (diffGreen + 32)), byte byteToWrite1 = (byte)((byte)QoiChunk.QoiOpLuma | (diffGreen + 32));
byteToWrite2 = (byte)((dr_dg << 4) | db_dg); byte byteToWrite2 = (byte)((dr_dg << 4) | db_dg);
stream.WriteByte(byteToWrite1); stream.WriteByte(byteToWrite1);
stream.WriteByte(byteToWrite2); stream.WriteByte(byteToWrite2);
} }

Loading…
Cancel
Save