/// <summary> /// Processes the Start of Frame marker. Specified in section B.2.2. /// </summary> /// <param name="remaining">The remaining bytes in the segment block.</param> private void ProcessStartOfFrameMarker(int remaining) { if (this.ComponentCount != 0) { throw new ImageFormatException("Multiple SOF markers"); } switch (remaining) { case 6 + (3 * 1): // Grayscale image. this.ComponentCount = 1; break; case 6 + (3 * 3): // YCbCr or RGB image. this.ComponentCount = 3; break; case 6 + (3 * 4): // YCbCrK or CMYK image. this.ComponentCount = 4; break; default: throw new ImageFormatException("Incorrect number of components"); } this.InputProcessor.ReadFull(this.Temp, 0, remaining); // We only support 8-bit precision. if (this.Temp[0] != 8) { throw new ImageFormatException("Only 8-Bit precision supported."); } int height = (this.Temp[1] << 8) + this.Temp[2]; int width = (this.Temp[3] << 8) + this.Temp[4]; this.ImageSizeInPixels = new Size(width, height); if (this.Temp[5] != this.ComponentCount) { throw new ImageFormatException("SOF has wrong length"); } this.Components = new OrigComponent[this.ComponentCount]; for (int i = 0; i < this.ComponentCount; i++) { byte componentIdentifier = this.Temp[6 + (3 * i)]; var component = new OrigComponent(componentIdentifier, i); component.InitializeCoreData(this); this.Components[i] = component; } int h0 = this.Components[0].HorizontalSamplingFactor; int v0 = this.Components[0].VerticalSamplingFactor; this.ImageSizeInMCU = this.ImageSizeInPixels.DivideRoundUp(8 * h0, 8 * v0); foreach (OrigComponent component in this.Components) { component.InitializeDerivedData(this); } this.ColorSpace = this.DeduceJpegColorSpace(); }