Ejemplo n.º 1
0
        internal static Bitmap DecodeImage(BinaryReader reader, ImgInfo imgInfo)
        {
            // Used for reading those nasty variable length codes
            var bReader = new BitReader(reader);

            float[][,] img = new float[imgInfo.numOfComponents][, ];

            imgInfo.deltaDc = new short[imgInfo.numOfComponents];

            for (int ch = 0; ch < imgInfo.numOfComponents; ch++)
            {
                img[ch] = new float[imgInfo.height, imgInfo.width];
            }

            try
            {
                // Determine the width of height of the MCU, based on the read subsampling
                // values from all channels (don't know if this is the correct way for all cases)
                // but it works for the most common ones
                var componentMax = imgInfo.components.Aggregate((a, b) =>
                {
                    return(new Markers.ComponentInfo()
                    {
                        samplingFactorX = Math.Max(a.samplingFactorX, b.samplingFactorX),
                        samplingFactorY = Math.Max(a.samplingFactorY, b.samplingFactorY)
                    });
                });

                int sizeMcuX = blkSize * componentMax.samplingFactorX;
                int sizeMcuY = blkSize * componentMax.samplingFactorY;

                int numMcusX = (imgInfo.width + sizeMcuX - 1) / sizeMcuX;
                int numMcusY = (imgInfo.height + sizeMcuY - 1) / sizeMcuY;

                for (int mcu = 0; mcu < numMcusX * numMcusY; mcu = NextMcuPos(imgInfo, bReader, mcu, numMcusX, numMcusY))
                {
                    // X and Y coordinates of current MCU
                    int mcuX = mcu % numMcusX;
                    int mcuY = mcu / numMcusX;
                    // Starting X and Y pixels of current MCU
                    int ofsX = mcuX * sizeMcuX;
                    int ofsY = mcuY * sizeMcuY;

                    for (int ch = 0; ch < imgInfo.numOfComponents; ch++)
                    {
                        int scaleX = componentMax.samplingFactorX / imgInfo.components[ch].samplingFactorX;
                        int scaleY = componentMax.samplingFactorY / imgInfo.components[ch].samplingFactorY;

                        for (int sy = 0; sy < imgInfo.components[ch].samplingFactorY; sy++)
                        {
                            for (int sx = 0; sx < imgInfo.components[ch].samplingFactorX; sx++)
                            {
                                DecodeBlock(bReader, imgInfo, img[ch], ch, ofsX + blkSize * sx, ofsY + blkSize * sy,
                                            scaleX, scaleY);
                            }
                        }
                    }

                    if (bReader.PastEndOfFile)
                    {
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                /*Logger.WriteLine(ex.Message);
                 * Logger.WriteLine(ex.StackTrace);*/
            }

            Color2[,] imagen = MergeChannels(imgInfo, img);
            var bmp  = new Bitmap(imgInfo.width, imgInfo.height);
            var conv = new BmpData(bmp);

            bReader.StopReading();
            conv.SetImage(imagen);

            return(bmp);
        }
Ejemplo n.º 2
0
        internal static Bitmap DecodeImage(BinaryReader reader, ImgInfo imgInfo)
        {
            // Used for reading those nasty variable length codes
            BitReader bReader = new BitReader(reader);
            float[][,] img = new float[imgInfo.numOfComponents][,];

            imgInfo.deltaDc = new short[imgInfo.numOfComponents];

            for (int ch = 0; ch < imgInfo.numOfComponents; ch++)
                img[ch] = new float[imgInfo.height, imgInfo.width];

            try
            {
                // Determine the width of height of the MCU, based on the read subsampling
                // values from all channels (don't know if this is the correct way for all cases)
                // but it works for the most common ones
                var componentMax = imgInfo.components.Aggregate((a, b) =>
                {
                    return new Markers.ComponentInfo()
                    {
                        samplingFactorX = Math.Max(a.samplingFactorX, b.samplingFactorX),
                        samplingFactorY = Math.Max(a.samplingFactorY, b.samplingFactorY)
                    };
                });

                int sizeMcuX = blkSize * componentMax.samplingFactorX;
                int sizeMcuY = blkSize * componentMax.samplingFactorY;

                int numMcusX = (imgInfo.width + sizeMcuX - 1) / sizeMcuX;
                int numMcusY = (imgInfo.height + sizeMcuY - 1) / sizeMcuY;

                for (int mcu = 0; mcu < numMcusX * numMcusY; mcu = NextMcuPos(imgInfo, bReader, mcu, numMcusX, numMcusY))
                {
                    // X and Y coordinates of current MCU
                    int mcuX = mcu % numMcusX;
                    int mcuY = mcu / numMcusX;
                    // Starting X and Y pixels of current MCU
                    int ofsX = mcuX * sizeMcuX;
                    int ofsY = mcuY * sizeMcuY;

                    for (int ch = 0; ch < imgInfo.numOfComponents; ch++)
                    {
                        int scaleX = componentMax.samplingFactorX / imgInfo.components[ch].samplingFactorX;
                        int scaleY = componentMax.samplingFactorY / imgInfo.components[ch].samplingFactorY;

                        for (int sy = 0; sy < imgInfo.components[ch].samplingFactorY; sy++)
                        {
                            for (int sx = 0; sx < imgInfo.components[ch].samplingFactorX; sx++)
                            {
                                DecodeBlock(bReader, imgInfo, img[ch], ch, ofsX + blkSize * sx, ofsY + blkSize * sy,
                                    scaleX, scaleY);
                            }
                        }
                    }

                    if (bReader.PastEndOfFile) break;
                }
            }
            catch (Exception ex)
            {
                Logger.WriteLine(ex.Message);
                Logger.WriteLine(ex.StackTrace);
            }

            Color2[,] imagen = MergeChannels(imgInfo, img);
            Bitmap bmp = new Bitmap(imgInfo.width, imgInfo.height);
            BmpData conv = new BmpData(bmp);

            bReader.StopReading();
            conv.SetImage(imagen);

            return bmp;
        }