Exemplo n.º 1
0
        public Bitmap Load()
        {
            if (this.ImageWidth == 0 || this.ImageHeight == 0)
            {
                JpegThrowHelper.ThrowInvalidImageDimensions(this.ImageWidth, this.ImageHeight);
            }

            var buffer = new Buffer2D <Vector4>(MemoryGroup <Vector4> .Allocate(this.ImageWidth * this.ImageHeight, this.ImageWidth), this.ImageWidth, this.ImageHeight);

            using (var postProcessor = new JpegImagePostProcessor(this.RawData))
            {
                postProcessor.PostProcess(buffer, new System.Threading.CancellationToken());
            }

            var bitmap = new Bitmap(this.ImageWidth, this.ImageHeight);

            for (int y = 0; y < bitmap.Height; y++)
            {
                for (int x = 0; x < bitmap.Width; x++)
                {
                    ref var p = ref buffer[x, y];
                    bitmap.PixelBufferView[x, y] = new Pixel()
                    {
                        A = p.W,
                        B = p.Z,
                        G = p.Y,
                        R = p.X
                    };
                }
            }
Exemplo n.º 2
0
        /// <summary>
        /// Parses the input stream for file markers
        /// </summary>
        /// <param name="stream">The input stream</param>
        /// <param name="metadataOnly">Whether to decode metadata only.</param>
        /// <param name="cancellationToken">The token to monitor cancellation.</param>
        public void ParseStream(BufferedReadStream stream, bool metadataOnly = false, CancellationToken cancellationToken = default)
        {
            this.Metadata = new ImageMetadata();

            // Check for the Start Of Image marker.
            stream.Read(this.markerBuffer, 0, 2);
            var fileMarker = new JpegFileMarker(this.markerBuffer[1], 0);

            if (fileMarker.Marker != JpegConstants.Markers.SOI)
            {
                JpegThrowHelper.ThrowInvalidImageContentException("Missing SOI marker.");
            }

            stream.Read(this.markerBuffer, 0, 2);
            byte marker = this.markerBuffer[1];

            fileMarker = new JpegFileMarker(marker, (int)stream.Position - 2);
            this.QuantizationTables = new Block8x8F[4];

            // Only assign what we need
            if (!metadataOnly)
            {
                const int maxTables = 4;
                this.dcHuffmanTables = new HuffmanTable[maxTables];
                this.acHuffmanTables = new HuffmanTable[maxTables];
            }

            // Break only when we discover a valid EOI marker.
            // https://github.com/SixLabors/ImageSharp/issues/695
            while (fileMarker.Marker != JpegConstants.Markers.EOI ||
                   (fileMarker.Marker == JpegConstants.Markers.EOI && fileMarker.Invalid))
            {
                cancellationToken.ThrowIfCancellationRequested();

                if (!fileMarker.Invalid)
                {
                    // Get the marker length
                    int remaining = this.ReadUint16(stream) - 2;

                    switch (fileMarker.Marker)
                    {
                    case JpegConstants.Markers.SOF0:
                    case JpegConstants.Markers.SOF1:
                    case JpegConstants.Markers.SOF2:
                        this.ProcessStartOfFrameMarker(stream, remaining, fileMarker, metadataOnly);
                        break;

                    case JpegConstants.Markers.SOS:
                        if (!metadataOnly)
                        {
                            this.ProcessStartOfScanMarker(stream, cancellationToken);
                            break;
                        }
                        else
                        {
                            // It's highly unlikely that APPn related data will be found after the SOS marker
                            // We should have gathered everything we need by now.
                            return;
                        }

                    case JpegConstants.Markers.DHT:

                        if (metadataOnly)
                        {
                            stream.Skip(remaining);
                        }
                        else
                        {
                            this.ProcessDefineHuffmanTablesMarker(stream, remaining);
                        }

                        break;

                    case JpegConstants.Markers.DQT:
                        this.ProcessDefineQuantizationTablesMarker(stream, remaining);
                        break;

                    case JpegConstants.Markers.DRI:
                        if (metadataOnly)
                        {
                            stream.Skip(remaining);
                        }
                        else
                        {
                            this.ProcessDefineRestartIntervalMarker(stream, remaining);
                        }

                        break;

                    case JpegConstants.Markers.APP0:
                        this.ProcessApplicationHeaderMarker(stream, remaining);
                        break;

                    case JpegConstants.Markers.APP1:
                        this.ProcessApp1Marker(stream, remaining);
                        break;

                    case JpegConstants.Markers.APP2:
                        this.ProcessApp2Marker(stream, remaining);
                        break;

                    case JpegConstants.Markers.APP3:
                    case JpegConstants.Markers.APP4:
                    case JpegConstants.Markers.APP5:
                    case JpegConstants.Markers.APP6:
                    case JpegConstants.Markers.APP7:
                    case JpegConstants.Markers.APP8:
                    case JpegConstants.Markers.APP9:
                    case JpegConstants.Markers.APP10:
                    case JpegConstants.Markers.APP11:
                    case JpegConstants.Markers.APP12:
                        stream.Skip(remaining);
                        break;

                    case JpegConstants.Markers.APP13:
                        this.ProcessApp13Marker(stream, remaining);
                        break;

                    case JpegConstants.Markers.APP14:
                        this.ProcessApp14Marker(stream, remaining);
                        break;

                    case JpegConstants.Markers.APP15:
                    case JpegConstants.Markers.COM:
                        stream.Skip(remaining);
                        break;
                    }
                }

                // Read on.
                fileMarker = FindNextFileMarker(this.markerBuffer, stream);
            }
        }