Example #1
0
        public static Bitmap Read(BinaryReader reader, ImgInfo imgInfo)
        {
            LogMarker(reader, name);

            if (imgInfo.numOfComponents != 1 && imgInfo.numOfComponents != 3)
                throw new Exception("Unsupported number of components (" +
                    imgInfo.numOfComponents + ")");

            ushort length = reader.ReadBEUInt16();
            byte componentsInScan = reader.ReadByte();

            for (int i = 0; i < componentsInScan; i++)
            {
                byte componentId = (byte)(reader.ReadByte() - 1);
                byte huffmanTables = reader.ReadByte();

                byte acTable = (byte)(huffmanTables & 0xf);
                byte dcTable = (byte)(huffmanTables >> 4);

                imgInfo.components[componentId].dcHuffmanTable = dcTable;
                imgInfo.components[componentId].acHuffmanTable = acTable;
            }

            reader.ReadBytes(3); // "Unused" bytes

            return ImageDecoder.DecodeImage(reader, imgInfo);
        }
Example #2
0
        public static void Read(BinaryReader reader, ImgInfo imgInfo)
        {
            LogMarker(reader, name);

            int length = reader.ReadBEUInt16();

            ushort restartInterval = reader.ReadBEUInt16();

            if (restartInterval == 0)
                throw new Exception("Invalid restart interval (0)");

            imgInfo.restartInterval = restartInterval;
            imgInfo.hasRestartMarkers = true;

            Log(reader, imgInfo.restartInterval);
        }
Example #3
0
        public static void Read(BinaryReader reader, ImgInfo imgInfo)
        {
            LogMarker(reader, name);

            ushort length = reader.ReadBEUInt16();
            Logger.WriteLine("Length: " + length.ToString());

            reader.BaseStream.Seek(length - 2, SeekOrigin.Current);
        }
Example #4
0
        public static void Read(BinaryReader reader, ImgInfo imgInfo)
        {
            LogMarker(reader, name);

            imgInfo.length = reader.ReadBEUInt16();
            imgInfo.dataPrecision = reader.ReadByte();
            imgInfo.height = reader.ReadBEUInt16();
            imgInfo.width = reader.ReadBEUInt16();
            imgInfo.numOfComponents = reader.ReadByte();

            if (imgInfo.length < 8)
                throw new Exception("Invalid length of Sof0");

            if (imgInfo.height == 0 || imgInfo.width == 0)
                throw new Exception("Invalid image size");

            if (imgInfo.dataPrecision != 8)
                throw new Exception("Unsupported data precision");

            if (imgInfo.numOfComponents != 1 && imgInfo.numOfComponents != 3)
                throw new Exception("Invalid number of components");

            imgInfo.components = new ComponentInfo[imgInfo.numOfComponents];

            for (int i = 0; i < imgInfo.numOfComponents; i++)
            {
                byte id = reader.ReadByte();

                if (id > 3)
                    throw new Exception("Invalid component type");

                byte samplingFactor = reader.ReadByte();

                imgInfo.components[i].id = id;
                imgInfo.components[i].samplingFactorX = (byte)(samplingFactor >> 4);
                imgInfo.components[i].samplingFactorY = (byte)(samplingFactor & 0x0f);

                imgInfo.components[i].quantTableId = reader.ReadByte();
            }

            Log(reader, imgInfo);
        }
Example #5
0
        public static void Read(BinaryReader reader, ImgInfo imgInfo)
        {
            LogMarker(reader, name);
            int markerLength = reader.ReadBEUInt16() - 2;

            while (markerLength > 0)
            {
                int length = ReadTable(reader, imgInfo);
                markerLength -= length;
            }
        }
Example #6
0
        public static void Read(BinaryReader reader, ImgInfo imgInfo)
        {
            LogMarker(reader, name);
            ushort markerLength = reader.ReadBEUInt16();

            reader.ReadBytes(11);

            imgInfo.app14MarkerFound = true;
            imgInfo.colorMode = (App14ColorMode)reader.ReadByte();

            if ((int)imgInfo.colorMode > 2)
                throw new Exception("Invalid Adobe colorspace");

            Logger.WriteLine("Transform Flag: " + imgInfo.colorMode);
            Logger.WriteLine();
        }
Example #7
0
        public static int ReadTable(BinaryReader reader, ImgInfo imgInfo)
        {
            byte tableInfo = reader.ReadByte();
            byte tableId = (byte)(tableInfo & 0xf); // Low 4 bits of tableInfo

            if (tableId > 3)
                throw new Exception("Invalid ID for quantization table");

            var quantTable = new QuantTable();

            quantTable.id = tableId;
            quantTable.precision = (byte)(tableInfo >> 4); // High 4 bits of tableInfo
            quantTable.valid = true;
            quantTable.table = new ushort[64];

            int sizeOfElement = quantTable.precision == 0 ? 1: 2;

            var tabla = FileOps.tablasZigzag[8];

            // Quantizer tables are in zigzag too!
            if (quantTable.precision == 0)
            {
                for (int i = 0; i < 64; i++)
                {
                    quantTable.table[tabla[i].Y * 8 + tabla[i].X] = reader.ReadByte();
                }
            }
            else
            {
                for (int i = 0; i < 64; i++)
                {
                    quantTable.table[tabla[i].Y * 8 + tabla[i].X] = reader.ReadBEUInt16();
                }
            }

            imgInfo.quantTables[tableId] = quantTable;

            Log(reader, quantTable);

            return 1 + 64 * sizeOfElement;
        }
Example #8
0
        public static void Read(BinaryReader reader, ImgInfo imgInfo, Pixz.Markers markerId)
        {
            Logger.Write("Unknown marker (" + markerId.ToString("X") + ")");

            if (!imgInfo.startOfImageFound)
            {
                Logger.Write(" found outside of image");
            }

            Logger.WriteLine(" at: " + (reader.BaseStream.Position - 2).ToString("X"));

            // Check if marker is not followed by a length argument
            if (markerId >= Pixz.Markers.Rs0 && markerId <= Pixz.Markers.Rs7)
                return;
            if (markerId == Pixz.Markers.LiteralFF)
                return;

            if (!imgInfo.startOfImageFound) return;

            ushort length = reader.ReadBEUInt16();
            Logger.WriteLine("Length: " + length.ToString());

            reader.BaseStream.Seek(length - 2, SeekOrigin.Current);
        }