Browse Source

Move reading frame ComponentIds out of only metadata block

pull/1732/head
Brian Popow 5 years ago
parent
commit
68a706fb06
  1. 74
      src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs

74
src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs

@ -845,57 +845,57 @@ namespace SixLabors.ImageSharp.Formats.Jpeg
this.Frame = new JpegFrame(frameMarker, precision, frameWidth, frameHeight, componentCount); this.Frame = new JpegFrame(frameMarker, precision, frameWidth, frameHeight, componentCount);
if (!metadataOnly) remaining -= length;
// Validate: remaining part must be equal to components * 3
const int componentBytes = 3;
if (remaining != componentCount * componentBytes)
{ {
remaining -= length; JpegThrowHelper.ThrowBadMarker("SOFn", remaining);
}
// Validate: remaining part must be equal to components * 3 // components*3 bytes: component data
const int componentBytes = 3; stream.Read(this.temp, 0, remaining);
if (remaining != componentCount * componentBytes)
{
JpegThrowHelper.ThrowBadMarker("SOFn", remaining);
}
// components*3 bytes: component data // No need to pool this. They max out at 4
stream.Read(this.temp, 0, remaining); this.Frame.ComponentIds = new byte[componentCount];
this.Frame.ComponentOrder = new byte[componentCount];
this.Frame.Components = new JpegComponent[componentCount];
// No need to pool this. They max out at 4 int maxH = 0;
this.Frame.ComponentIds = new byte[componentCount]; int maxV = 0;
this.Frame.ComponentOrder = new byte[componentCount]; int index = 0;
this.Frame.Components = new JpegComponent[componentCount]; for (int i = 0; i < componentCount; i++)
{
byte hv = this.temp[index + 1];
int h = (hv >> 4) & 15;
int v = hv & 15;
int maxH = 0; if (maxH < h)
int maxV = 0;
int index = 0;
for (int i = 0; i < componentCount; i++)
{ {
byte hv = this.temp[index + 1]; maxH = h;
int h = (hv >> 4) & 15; }
int v = hv & 15;
if (maxH < h) if (maxV < v)
{ {
maxH = h; maxV = v;
} }
if (maxV < v) var component = new JpegComponent(this.Configuration.MemoryAllocator, this.Frame, this.temp[index], h, v, this.temp[index + 2], i);
{
maxV = v;
}
var component = new JpegComponent(this.Configuration.MemoryAllocator, this.Frame, this.temp[index], h, v, this.temp[index + 2], i); this.Frame.Components[i] = component;
this.Frame.ComponentIds[i] = component.Id;
this.Frame.Components[i] = component; index += componentBytes;
this.Frame.ComponentIds[i] = component.Id; }
index += componentBytes; this.ColorSpace = this.DeduceJpegColorSpace(componentCount, this.Frame.Components);
}
this.ColorSpace = this.DeduceJpegColorSpace(componentCount, this.Frame.Components); this.Metadata.GetJpegMetadata().ColorType = this.ColorSpace == JpegColorSpace.Grayscale ? JpegColorType.Luminance : JpegColorType.YCbCr;
this.Metadata.GetJpegMetadata().ColorType = this.ColorSpace == JpegColorSpace.Grayscale ? JpegColorType.Luminance : JpegColorType.YCbCr;
if (!metadataOnly)
{
this.Frame.Init(maxH, maxV); this.Frame.Init(maxH, maxV);
this.scanDecoder.InjectFrameData(this.Frame, this); this.scanDecoder.InjectFrameData(this.Frame, this);
} }
} }

Loading…
Cancel
Save