Skip to content

Commit 68a706f

Browse files
committed
Move reading frame ComponentIds out of only metadata block
1 parent 6cb8717 commit 68a706f

File tree

1 file changed

+37
-37
lines changed

1 file changed

+37
-37
lines changed

src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -845,57 +845,57 @@ private void ProcessStartOfFrameMarker(BufferedReadStream stream, int remaining,
845845

846846
this.Frame = new JpegFrame(frameMarker, precision, frameWidth, frameHeight, componentCount);
847847

848-
if (!metadataOnly)
848+
remaining -= length;
849+
850+
// Validate: remaining part must be equal to components * 3
851+
const int componentBytes = 3;
852+
if (remaining != componentCount * componentBytes)
849853
{
850-
remaining -= length;
854+
JpegThrowHelper.ThrowBadMarker("SOFn", remaining);
855+
}
851856

852-
// Validate: remaining part must be equal to components * 3
853-
const int componentBytes = 3;
854-
if (remaining != componentCount * componentBytes)
855-
{
856-
JpegThrowHelper.ThrowBadMarker("SOFn", remaining);
857-
}
857+
// components*3 bytes: component data
858+
stream.Read(this.temp, 0, remaining);
858859

859-
// components*3 bytes: component data
860-
stream.Read(this.temp, 0, remaining);
860+
// No need to pool this. They max out at 4
861+
this.Frame.ComponentIds = new byte[componentCount];
862+
this.Frame.ComponentOrder = new byte[componentCount];
863+
this.Frame.Components = new JpegComponent[componentCount];
861864

862-
// No need to pool this. They max out at 4
863-
this.Frame.ComponentIds = new byte[componentCount];
864-
this.Frame.ComponentOrder = new byte[componentCount];
865-
this.Frame.Components = new JpegComponent[componentCount];
865+
int maxH = 0;
866+
int maxV = 0;
867+
int index = 0;
868+
for (int i = 0; i < componentCount; i++)
869+
{
870+
byte hv = this.temp[index + 1];
871+
int h = (hv >> 4) & 15;
872+
int v = hv & 15;
866873

867-
int maxH = 0;
868-
int maxV = 0;
869-
int index = 0;
870-
for (int i = 0; i < componentCount; i++)
874+
if (maxH < h)
871875
{
872-
byte hv = this.temp[index + 1];
873-
int h = (hv >> 4) & 15;
874-
int v = hv & 15;
876+
maxH = h;
877+
}
875878

876-
if (maxH < h)
877-
{
878-
maxH = h;
879-
}
879+
if (maxV < v)
880+
{
881+
maxV = v;
882+
}
880883

881-
if (maxV < v)
882-
{
883-
maxV = v;
884-
}
884+
var component = new JpegComponent(this.Configuration.MemoryAllocator, this.Frame, this.temp[index], h, v, this.temp[index + 2], i);
885885

886-
var component = new JpegComponent(this.Configuration.MemoryAllocator, this.Frame, this.temp[index], h, v, this.temp[index + 2], i);
886+
this.Frame.Components[i] = component;
887+
this.Frame.ComponentIds[i] = component.Id;
887888

888-
this.Frame.Components[i] = component;
889-
this.Frame.ComponentIds[i] = component.Id;
889+
index += componentBytes;
890+
}
890891

891-
index += componentBytes;
892-
}
892+
this.ColorSpace = this.DeduceJpegColorSpace(componentCount, this.Frame.Components);
893893

894-
this.ColorSpace = this.DeduceJpegColorSpace(componentCount, this.Frame.Components);
895-
this.Metadata.GetJpegMetadata().ColorType = this.ColorSpace == JpegColorSpace.Grayscale ? JpegColorType.Luminance : JpegColorType.YCbCr;
894+
this.Metadata.GetJpegMetadata().ColorType = this.ColorSpace == JpegColorSpace.Grayscale ? JpegColorType.Luminance : JpegColorType.YCbCr;
896895

896+
if (!metadataOnly)
897+
{
897898
this.Frame.Init(maxH, maxV);
898-
899899
this.scanDecoder.InjectFrameData(this.Frame, this);
900900
}
901901
}

0 commit comments

Comments
 (0)