/// <summary>
        /// This method checks if the image is a valid JPEG and processes some parameters.
        /// @throws BadElementException
        /// @throws IOException
        /// </summary>
        private void processParameters()
        {
            type         = JPEG2000;
            originalType = ORIGINAL_JPEG2000;
            _inp         = null;
            try
            {
                string errorId;
                if (rawData == null)
                {
                    _inp    = url.GetResponseStream();
                    errorId = url.ToString();
                }
                else
                {
                    _inp    = new MemoryStream(rawData);
                    errorId = "Byte array";
                }
                _boxLength = Cio_read(4);
                if (_boxLength == 0x0000000c)
                {
                    _boxType = Cio_read(4);
                    if (JP2_JP != _boxType)
                    {
                        throw new IOException("Expected JP Marker");
                    }
                    if (0x0d0a870a != Cio_read(4))
                    {
                        throw new IOException("Error with JP Marker");
                    }

                    Jp2_read_boxhdr();
                    if (JP2_FTYP != _boxType)
                    {
                        throw new IOException("Expected FTYP Marker");
                    }
                    Utilities.Skip(_inp, _boxLength - 8);
                    Jp2_read_boxhdr();
                    do
                    {
                        if (JP2_JP2H != _boxType)
                        {
                            if (_boxType == JP2_JP2C)
                            {
                                throw new IOException("Expected JP2H Marker");
                            }
                            Utilities.Skip(_inp, _boxLength - 8);
                            Jp2_read_boxhdr();
                        }
                    } while (JP2_JP2H != _boxType);
                    Jp2_read_boxhdr();
                    if (JP2_IHDR != _boxType)
                    {
                        throw new IOException("Expected IHDR Marker");
                    }
                    scaledHeight = Cio_read(4);
                    Top          = scaledHeight;
                    scaledWidth  = Cio_read(4);
                    Right        = scaledWidth;
                    bpc          = -1;
                }
                else if ((uint)_boxLength == 0xff4fff51)
                {
                    Utilities.Skip(_inp, 4);
                    var x1 = Cio_read(4);
                    var y1 = Cio_read(4);
                    var x0 = Cio_read(4);
                    var y0 = Cio_read(4);
                    Utilities.Skip(_inp, 16);
                    colorspace   = Cio_read(2);
                    bpc          = 8;
                    scaledHeight = y1 - y0;
                    Top          = scaledHeight;
                    scaledWidth  = x1 - x0;
                    Right        = scaledWidth;
                }
                else
                {
                    throw new IOException("Not a valid Jpeg2000 file");
                }
            }
            finally
            {
                if (_inp != null)
                {
                    try { _inp.Dispose(); } catch { }
                    _inp = null;
                }
            }
            plainWidth  = Width;
            plainHeight = Height;
        }
示例#2
0
        /**
         * This method checks if the image is a valid JPEG and processes some parameters.
         * @throws BadElementException
         * @throws IOException
         */

        private void ProcessParameters()
        {
            type         = JPEG2000;
            originalType = ORIGINAL_JPEG2000;
            inp          = null;
            try
            {
                if (rawData == null)
                {
                    WebRequest w = WebRequest.Create(url);
                    w.Credentials = CredentialCache.DefaultCredentials;
                    inp           = w.GetResponse().GetResponseStream();
                }
                else
                {
                    inp = new MemoryStream(rawData);
                }
                boxLength = Cio_read(4);
                if (boxLength == 0x0000000c)
                {
                    isJp2   = true;
                    boxType = Cio_read(4);
                    if (JP2_JP != boxType)
                    {
                        throw new IOException(MessageLocalization.GetComposedMessage("expected.jp.marker"));
                    }
                    if (0x0d0a870a != Cio_read(4))
                    {
                        throw new IOException(MessageLocalization.GetComposedMessage("error.with.jp.marker"));
                    }

                    Jp2_read_boxhdr();
                    if (JP2_FTYP != boxType)
                    {
                        throw new IOException(MessageLocalization.GetComposedMessage("expected.ftyp.marker"));
                    }
                    Utilities.Skip(inp, boxLength - 8);
                    Jp2_read_boxhdr();
                    do
                    {
                        if (JP2_JP2H != boxType)
                        {
                            if (boxType == JP2_JP2C)
                            {
                                throw new IOException(MessageLocalization.GetComposedMessage("expected.jp2h.marker"));
                            }
                            Utilities.Skip(inp, boxLength - 8);
                            Jp2_read_boxhdr();
                        }
                    } while (JP2_JP2H != boxType);
                    Jp2_read_boxhdr();
                    if (JP2_IHDR != boxType)
                    {
                        throw new IOException(MessageLocalization.GetComposedMessage("expected.ihdr.marker"));
                    }
                    scaledHeight = Cio_read(4);
                    Top          = scaledHeight;
                    scaledWidth  = Cio_read(4);
                    Right        = scaledWidth;
                    numOfComps   = Cio_read(2);
                    bpc          = -1;
                    bpc          = Cio_read(1);

                    Utilities.Skip(inp, 3);

                    Jp2_read_boxhdr();
                    if (boxType == JP2_BPCC)
                    {
                        bpcBoxData = new byte[boxLength - 8];
                        inp.Read(bpcBoxData, 0, boxLength - 8);
                    }
                    else if (boxType == JP2_COLR)
                    {
                        do
                        {
                            if (colorSpecBoxes == null)
                            {
                                colorSpecBoxes = new List <ColorSpecBox>();
                            }
                            colorSpecBoxes.Add(Jp2_read_colr());
                            try {
                                Jp2_read_boxhdr();
                            }
                            catch (ZeroBoxSiteException e) {
                                //Probably we have reached the contiguous codestream box which is the last in jpeg2000 and has no length.
                            }
                        } while (JP2_COLR == boxType);
                    }
                }
                else if ((uint)boxLength == 0xff4fff51)
                {
                    Utilities.Skip(inp, 4);
                    int x1 = Cio_read(4);
                    int y1 = Cio_read(4);
                    int x0 = Cio_read(4);
                    int y0 = Cio_read(4);
                    Utilities.Skip(inp, 16);
                    colorspace   = Cio_read(2);
                    bpc          = 8;
                    scaledHeight = y1 - y0;
                    Top          = scaledHeight;
                    scaledWidth  = x1 - x0;
                    Right        = scaledWidth;
                }
                else
                {
                    throw new IOException(MessageLocalization.GetComposedMessage("not.a.valid.jpeg2000.file"));
                }
            }
            finally
            {
                if (inp != null)
                {
                    try {
                        inp.Close();
                    }
                    catch { }
                    inp = null;
                }
            }
            plainWidth  = this.Width;
            plainHeight = this.Height;
        }
示例#3
0
        // private methods

        /// <summary>
        /// This method checks if the image is a valid JPEG and processes some parameters.
        /// </summary>
        private void ProcessParameters()
        {
            type         = Element.JPEG;
            originalType = ORIGINAL_JPEG;
            Stream istr = null;

            try {
                string errorID;
                if (rawData == null)
                {
                    WebRequest w = WebRequest.Create(url);
                    w.Credentials = CredentialCache.DefaultCredentials;
                    istr          = w.GetResponse().GetResponseStream();
                    errorID       = url.ToString();
                }
                else
                {
                    istr    = new MemoryStream(rawData);
                    errorID = "Byte array";
                }
                if (istr.ReadByte() != 0xFF || istr.ReadByte() != 0xD8)
                {
                    throw new BadElementException(MessageLocalization.GetComposedMessage("1.is.not.a.valid.jpeg.file", errorID));
                }
                bool firstPass = true;
                int  len;
                while (true)
                {
                    int v = istr.ReadByte();
                    if (v < 0)
                    {
                        throw new IOException(MessageLocalization.GetComposedMessage("premature.eof.while.reading.jpg"));
                    }
                    if (v == 0xFF)
                    {
                        int marker = istr.ReadByte();
                        if (firstPass && marker == M_APP0)
                        {
                            firstPass = false;
                            len       = GetShort(istr);
                            if (len < 16)
                            {
                                Utilities.Skip(istr, len - 2);
                                continue;
                            }
                            byte[] bcomp = new byte[JFIF_ID.Length];
                            int    r     = istr.Read(bcomp, 0, bcomp.Length);
                            if (r != bcomp.Length)
                            {
                                throw new BadElementException(MessageLocalization.GetComposedMessage("1.corrupted.jfif.marker", errorID));
                            }
                            bool found = true;
                            for (int k = 0; k < bcomp.Length; ++k)
                            {
                                if (bcomp[k] != JFIF_ID[k])
                                {
                                    found = false;
                                    break;
                                }
                            }
                            if (!found)
                            {
                                Utilities.Skip(istr, len - 2 - bcomp.Length);
                                continue;
                            }
                            Utilities.Skip(istr, 2);
                            int units = istr.ReadByte();
                            int dx    = GetShort(istr);
                            int dy    = GetShort(istr);
                            if (units == 1)
                            {
                                dpiX = dx;
                                dpiY = dy;
                            }
                            else if (units == 2)
                            {
                                dpiX = (int)((float)dx * 2.54f + 0.5f);
                                dpiY = (int)((float)dy * 2.54f + 0.5f);
                            }
                            Utilities.Skip(istr, len - 2 - bcomp.Length - 7);
                            continue;
                        }
                        if (marker == M_APPE)
                        {
                            len = GetShort(istr) - 2;
                            byte[] byteappe = new byte[len];
                            for (int k = 0; k < len; ++k)
                            {
                                byteappe[k] = (byte)istr.ReadByte();
                            }
                            if (byteappe.Length >= 12)
                            {
                                string appe = System.Text.ASCIIEncoding.ASCII.GetString(byteappe, 0, 5);
                                if (Util.EqualsIgnoreCase(appe, "adobe"))
                                {
                                    invert = true;
                                }
                            }
                            continue;
                        }
                        if (marker == M_APP2)
                        {
                            len = GetShort(istr) - 2;
                            byte[] byteapp2 = new byte[len];
                            for (int k = 0; k < len; ++k)
                            {
                                byteapp2[k] = (byte)istr.ReadByte();
                            }
                            if (byteapp2.Length >= 14)
                            {
                                String app2 = System.Text.ASCIIEncoding.ASCII.GetString(byteapp2, 0, 11);
                                if (app2.Equals("ICC_PROFILE"))
                                {
                                    int order = byteapp2[12] & 0xff;
                                    int count = byteapp2[13] & 0xff;
                                    // some jpeg producers don't know how to count to 1
                                    if (order < 1)
                                    {
                                        order = 1;
                                    }
                                    if (count < 1)
                                    {
                                        count = 1;
                                    }
                                    if (icc == null)
                                    {
                                        icc = new byte[count][];
                                    }
                                    icc[order - 1] = byteapp2;
                                }
                            }
                            continue;
                        }
                        firstPass = false;
                        int markertype = MarkerType(marker);
                        if (markertype == VALID_MARKER)
                        {
                            Utilities.Skip(istr, 2);
                            if (istr.ReadByte() != 0x08)
                            {
                                throw new BadElementException(MessageLocalization.GetComposedMessage("1.must.have.8.bits.per.component", errorID));
                            }
                            scaledHeight = GetShort(istr);
                            Top          = scaledHeight;
                            scaledWidth  = GetShort(istr);
                            Right        = scaledWidth;
                            colorspace   = istr.ReadByte();
                            bpc          = 8;
                            break;
                        }
                        else if (markertype == UNSUPPORTED_MARKER)
                        {
                            throw new BadElementException(MessageLocalization.GetComposedMessage("1.unsupported.jpeg.marker.2", errorID, marker));
                        }
                        else if (markertype != NOPARAM_MARKER)
                        {
                            Utilities.Skip(istr, GetShort(istr) - 2);
                        }
                    }
                }
            }
            finally {
                if (istr != null)
                {
                    istr.Close();
                }
            }
            plainWidth  = this.Width;
            plainHeight = this.Height;
            if (icc != null)
            {
                int total = 0;
                for (int k = 0; k < icc.Length; ++k)
                {
                    if (icc[k] == null)
                    {
                        icc = null;
                        return;
                    }
                    total += icc[k].Length - 14;
                }
                byte[] ficc = new byte[total];
                total = 0;
                for (int k = 0; k < icc.Length; ++k)
                {
                    System.Array.Copy(icc[k], 14, ficc, total, icc[k].Length - 14);
                    total += icc[k].Length - 14;
                }
                try {
                    ICC_Profile icc_prof = ICC_Profile.GetInstance(ficc);
                    TagICC = icc_prof;
                }
                catch {}
                icc = null;
            }
        }
示例#4
0
        // private methods

        /// <summary>
        /// This method checks if the image is a valid JPEG and processes some parameters.
        /// </summary>
        private void ProcessParameters()
        {
            type         = Element.JPEG;
            originalType = ORIGINAL_JPEG;
            Stream istr = null;

            try {
                string errorID;
                if (rawData == null)
                {
                    WebRequest w = WebRequest.Create(url);
                    w.Credentials = CredentialCache.DefaultCredentials;
                    istr          = w.GetResponse().GetResponseStream();
                    errorID       = url.ToString();
                }
                else
                {
                    istr    = new MemoryStream(rawData);
                    errorID = "Byte array";
                }
                if (istr.ReadByte() != 0xFF || istr.ReadByte() != 0xD8)
                {
                    throw new BadElementException(MessageLocalization.GetComposedMessage("1.is.not.a.valid.jpeg.file", errorID));
                }
                bool firstPass = true;
                int  len;
                while (true)
                {
                    int v = istr.ReadByte();
                    if (v < 0)
                    {
                        throw new IOException(MessageLocalization.GetComposedMessage("premature.eof.while.reading.jpg"));
                    }
                    if (v == 0xFF)
                    {
                        int marker = istr.ReadByte();
                        if (firstPass && marker == M_APP0)
                        {
                            firstPass = false;
                            len       = GetShort(istr);
                            if (len < 16)
                            {
                                Utilities.Skip(istr, len - 2);
                                continue;
                            }
                            byte[] bcomp = new byte[JFIF_ID.Length];
                            int    r     = istr.Read(bcomp, 0, bcomp.Length);
                            if (r != bcomp.Length)
                            {
                                throw new BadElementException(MessageLocalization.GetComposedMessage("1.corrupted.jfif.marker", errorID));
                            }
                            bool found = true;
                            for (int k = 0; k < bcomp.Length; ++k)
                            {
                                if (bcomp[k] != JFIF_ID[k])
                                {
                                    found = false;
                                    break;
                                }
                            }
                            if (!found)
                            {
                                Utilities.Skip(istr, len - 2 - bcomp.Length);
                                continue;
                            }
                            Utilities.Skip(istr, 2);
                            int units = istr.ReadByte();
                            int dx    = GetShort(istr);
                            int dy    = GetShort(istr);
                            if (units == 1)
                            {
                                dpiX = dx;
                                dpiY = dy;
                            }
                            else if (units == 2)
                            {
                                dpiX = (int)((float)dx * 2.54f + 0.5f);
                                dpiY = (int)((float)dy * 2.54f + 0.5f);
                            }
                            Utilities.Skip(istr, len - 2 - bcomp.Length - 7);
                            continue;
                        }
                        if (marker == M_APPE)
                        {
                            len = GetShort(istr) - 2;
                            byte[] byteappe = new byte[len];
                            for (int k = 0; k < len; ++k)
                            {
                                byteappe[k] = (byte)istr.ReadByte();
                            }
                            if (byteappe.Length >= 12)
                            {
                                string appe = System.Text.ASCIIEncoding.ASCII.GetString(byteappe, 0, 5);
                                if (Util.EqualsIgnoreCase(appe, "adobe"))
                                {
                                    invert = true;
                                }
                            }
                            continue;
                        }
                        if (marker == M_APP2)
                        {
                            len = GetShort(istr) - 2;
                            byte[] byteapp2 = new byte[len];
                            for (int k = 0; k < len; ++k)
                            {
                                byteapp2[k] = (byte)istr.ReadByte();
                            }
                            if (byteapp2.Length >= 14)
                            {
                                String app2 = System.Text.ASCIIEncoding.ASCII.GetString(byteapp2, 0, 11);
                                if (app2.Equals("ICC_PROFILE"))
                                {
                                    int order = byteapp2[12] & 0xff;
                                    int count = byteapp2[13] & 0xff;
                                    // some jpeg producers don't know how to count to 1
                                    if (order < 1)
                                    {
                                        order = 1;
                                    }
                                    if (count < 1)
                                    {
                                        count = 1;
                                    }
                                    if (icc == null)
                                    {
                                        icc = new byte[count][];
                                    }
                                    icc[order - 1] = byteapp2;
                                }
                            }
                            continue;
                        }
                        if (marker == M_APPD)
                        {
                            len = GetShort(istr) - 2;
                            byte[] byteappd = new byte[len];
                            for (int k = 0; k < len; k++)
                            {
                                byteappd[k] = (byte)istr.ReadByte();
                            }
                            // search for '8BIM Resolution' marker
                            int j = 0;
                            for (j = 0; j < len - PS_8BIM_RESO.Length; j++)
                            {
                                bool found = true;
                                for (int i = 0; i < PS_8BIM_RESO.Length; i++)
                                {
                                    if (byteappd[j + i] != PS_8BIM_RESO[i])
                                    {
                                        found = false;
                                        break;
                                    }
                                }
                                if (found)
                                {
                                    break;
                                }
                            }

                            j += PS_8BIM_RESO.Length;
                            if (j < len - PS_8BIM_RESO.Length)
                            {
                                // "PASCAL String" for name, i.e. string prefix with length byte
                                // padded to be even length; 2 null bytes if empty
                                byte namelength = byteappd[j];
                                // add length byte
                                namelength++;
                                // add padding
                                if (namelength % 2 == 1)
                                {
                                    namelength++;
                                }
                                // just skip name
                                j += namelength;
                                // size of the resolution data
                                int resosize = (byteappd[j] << 24) + (byteappd[j + 1] << 16) + (byteappd[j + 2] << 8) + byteappd[j + 3];
                                // should be 16
                                if (resosize != 16)
                                {
                                    // fail silently, for now
                                    //System.err.println("DEBUG: unsupported resolution IRB size");
                                    continue;
                                }
                                j += 4;
                                int dx = (byteappd[j] << 8) + (byteappd[j + 1] & 0xff);
                                j += 2;
                                // skip 2 unknown bytes
                                j += 2;
                                int unitsx = (byteappd[j] << 8) + (byteappd[j + 1] & 0xff);
                                j += 2;
                                // skip 2 unknown bytes
                                j += 2;
                                int dy = (byteappd[j] << 8) + (byteappd[j + 1] & 0xff);
                                j += 2;
                                // skip 2 unknown bytes
                                j += 2;
                                int unitsy = (byteappd[j] << 8) + (byteappd[j + 1] & 0xff);

                                if (unitsx == 1 || unitsx == 2)
                                {
                                    dx = (unitsx == 2 ? (int)(dx * 2.54f + 0.5f) : dx);
                                    // make sure this is consistent with JFIF data
                                    if (dpiX != 0 && dpiX != dx)
                                    {
                                        //System.err.println("DEBUG: inconsistent metadata (dpiX: " + dpiX + " vs " + dx + ")");
                                    }
                                    else
                                    {
                                        dpiX = dx;
                                    }
                                }
                                if (unitsy == 1 || unitsy == 2)
                                {
                                    dy = (unitsy == 2 ? (int)(dy * 2.54f + 0.5f) : dy);
                                    // make sure this is consistent with JFIF data
                                    if (dpiY != 0 && dpiY != dy)
                                    {
                                        //System.err.println("DEBUG: inconsistent metadata (dpiY: " + dpiY + " vs " + dy + ")");
                                    }
                                    else
                                    {
                                        dpiY = dy;
                                    }
                                }
                            }
                            continue;
                        }
                        firstPass = false;
                        int markertype = MarkerType(marker);
                        if (markertype == VALID_MARKER)
                        {
                            Utilities.Skip(istr, 2);
                            if (istr.ReadByte() != 0x08)
                            {
                                throw new BadElementException(MessageLocalization.GetComposedMessage("1.must.have.8.bits.per.component", errorID));
                            }
                            scaledHeight = GetShort(istr);
                            Top          = scaledHeight;
                            scaledWidth  = GetShort(istr);
                            Right        = scaledWidth;
                            colorspace   = istr.ReadByte();
                            bpc          = 8;
                            break;
                        }
                        else if (markertype == UNSUPPORTED_MARKER)
                        {
                            throw new BadElementException(MessageLocalization.GetComposedMessage("1.unsupported.jpeg.marker.2", errorID, marker));
                        }
                        else if (markertype != NOPARAM_MARKER)
                        {
                            Utilities.Skip(istr, GetShort(istr) - 2);
                        }
                    }
                }
            }
            finally {
                if (istr != null)
                {
                    istr.Close();
                }
            }
            plainWidth  = this.Width;
            plainHeight = this.Height;
            if (icc != null)
            {
                int total = 0;
                for (int k = 0; k < icc.Length; ++k)
                {
                    if (icc[k] == null)
                    {
                        icc = null;
                        return;
                    }
                    total += icc[k].Length - 14;
                }
                byte[] ficc = new byte[total];
                total = 0;
                for (int k = 0; k < icc.Length; ++k)
                {
                    System.Array.Copy(icc[k], 14, ficc, total, icc[k].Length - 14);
                    total += icc[k].Length - 14;
                }
                try {
                    ICC_Profile icc_prof = ICC_Profile.GetInstance(ficc, colorspace);
                    TagICC = icc_prof;
                }
                catch {}
                icc = null;
            }
        }
示例#5
0
        /**
         * This method checks if the image is a valid JPEG and processes some parameters.
         * @throws BadElementException
         * @throws IOException
         */
        private void ProcessParameters()
        {
            type         = JPEG2000;
            originalType = ORIGINAL_JPEG2000;
            inp          = null;
            try {
                string errorID;
                if (rawData == null)
                {
                    WebRequest w = WebRequest.Create(url);
                    inp     = w.GetResponse().GetResponseStream();
                    errorID = url.ToString();
                }
                else
                {
                    inp     = new MemoryStream(rawData);
                    errorID = "Byte array";
                }
                boxLength = Cio_read(4);
                if (boxLength == 0x0000000c)
                {
                    boxType = Cio_read(4);
                    if (JP2_JP != boxType)
                    {
                        throw new IOException(MessageLocalization.GetComposedMessage("expected.jp.marker"));
                    }
                    if (0x0d0a870a != Cio_read(4))
                    {
                        throw new IOException(MessageLocalization.GetComposedMessage("error.with.jp.marker"));
                    }

                    Jp2_read_boxhdr();
                    if (JP2_FTYP != boxType)
                    {
                        throw new IOException(MessageLocalization.GetComposedMessage("expected.ftyp.marker"));
                    }
                    Utilities.Skip(inp, boxLength - 8);
                    Jp2_read_boxhdr();
                    do
                    {
                        if (JP2_JP2H != boxType)
                        {
                            if (boxType == JP2_JP2C)
                            {
                                throw new IOException(MessageLocalization.GetComposedMessage("expected.jp2h.marker"));
                            }
                            Utilities.Skip(inp, boxLength - 8);
                            Jp2_read_boxhdr();
                        }
                    } while (JP2_JP2H != boxType);
                    Jp2_read_boxhdr();
                    if (JP2_IHDR != boxType)
                    {
                        throw new IOException(MessageLocalization.GetComposedMessage("expected.ihdr.marker"));
                    }
                    scaledHeight = Cio_read(4);
                    Top          = scaledHeight;
                    scaledWidth  = Cio_read(4);
                    Right        = scaledWidth;
                    bpc          = -1;
                }
                else if ((uint)boxLength == 0xff4fff51)
                {
                    Utilities.Skip(inp, 4);
                    int x1 = Cio_read(4);
                    int y1 = Cio_read(4);
                    int x0 = Cio_read(4);
                    int y0 = Cio_read(4);
                    Utilities.Skip(inp, 16);
                    colorspace   = Cio_read(2);
                    bpc          = 8;
                    scaledHeight = y1 - y0;
                    Top          = scaledHeight;
                    scaledWidth  = x1 - x0;
                    Right        = scaledWidth;
                }
                else
                {
                    throw new IOException(MessageLocalization.GetComposedMessage("not.a.valid.jpeg2000.file"));
                }
            }
            finally {
                if (inp != null)
                {
                    try{ inp.Close(); }catch {}
                    inp = null;
                }
            }
            plainWidth  = this.Width;
            plainHeight = this.Height;
        }
        /// <summary>
        /// private methods
        /// </summary>

        /// <summary>
        /// This method checks if the image is a valid JPEG and processes some parameters.
        /// </summary>
        private void processParameters()
        {
            type         = JPEG;
            originalType = ORIGINAL_JPEG;
            Stream istr = null;

            try
            {
                string errorId;
                if (rawData == null)
                {
                    istr    = url.GetResponseStream();
                    errorId = url.ToString();
                }
                else
                {
                    istr    = new MemoryStream(rawData);
                    errorId = "Byte array";
                }
                if (istr.ReadByte() != 0xFF || istr.ReadByte() != 0xD8)
                {
                    throw new BadElementException(errorId + " is not a valid JPEG-file.");
                }
                var firstPass = true;
                int len;
                while (true)
                {
                    var v = istr.ReadByte();
                    if (v < 0)
                    {
                        throw new IOException("Premature EOF while reading JPG.");
                    }

                    if (v == 0xFF)
                    {
                        var marker = istr.ReadByte();
                        if (firstPass && marker == M_APP0)
                        {
                            firstPass = false;
                            len       = getShort(istr);
                            if (len < 16)
                            {
                                Utilities.Skip(istr, len - 2);
                                continue;
                            }
                            var bcomp = new byte[JfifId.Length];
                            var r     = istr.Read(bcomp, 0, bcomp.Length);
                            if (r != bcomp.Length)
                            {
                                throw new BadElementException(errorId + " corrupted JFIF marker.");
                            }

                            var found = true;
                            for (var k = 0; k < bcomp.Length; ++k)
                            {
                                if (bcomp[k] != JfifId[k])
                                {
                                    found = false;
                                    break;
                                }
                            }
                            if (!found)
                            {
                                Utilities.Skip(istr, len - 2 - bcomp.Length);
                                continue;
                            }
                            Utilities.Skip(istr, 2);
                            var units = istr.ReadByte();
                            var dx    = getShort(istr);
                            var dy    = getShort(istr);
                            if (units == 1)
                            {
                                dpiX = dx;
                                dpiY = dy;
                            }
                            else if (units == 2)
                            {
                                dpiX = (int)(dx * 2.54f + 0.5f);
                                dpiY = (int)(dy * 2.54f + 0.5f);
                            }
                            Utilities.Skip(istr, len - 2 - bcomp.Length - 7);
                            continue;
                        }
                        if (marker == M_APPE)
                        {
                            len = getShort(istr) - 2;
                            var byteappe = new byte[len];
                            for (var k = 0; k < len; ++k)
                            {
                                byteappe[k] = (byte)istr.ReadByte();
                            }
                            if (byteappe.Length >= 12)
                            {
                                var appe = System.Text.Encoding.ASCII.GetString(byteappe, 0, 5);
                                if (Util.EqualsIgnoreCase(appe, "adobe"))
                                {
                                    Invert = true;
                                }
                            }
                            continue;
                        }
                        if (marker == M_APP2)
                        {
                            len = getShort(istr) - 2;
                            var byteapp2 = new byte[len];
                            for (var k = 0; k < len; ++k)
                            {
                                byteapp2[k] = (byte)istr.ReadByte();
                            }
                            if (byteapp2.Length >= 14)
                            {
                                var app2 = System.Text.Encoding.ASCII.GetString(byteapp2, 0, 11);
                                if (app2.Equals("ICC_PROFILE"))
                                {
                                    var order = byteapp2[12] & 0xff;
                                    var count = byteapp2[13] & 0xff;
                                    if (_icc == null)
                                    {
                                        _icc = new byte[count][];
                                    }

                                    _icc[order - 1] = byteapp2;
                                }
                            }
                            continue;
                        }
                        firstPass = false;
                        var markertype = markerType(marker);
                        if (markertype == VALID_MARKER)
                        {
                            Utilities.Skip(istr, 2);
                            if (istr.ReadByte() != 0x08)
                            {
                                throw new BadElementException(errorId + " must have 8 bits per component.");
                            }
                            scaledHeight = getShort(istr);
                            Top          = scaledHeight;
                            scaledWidth  = getShort(istr);
                            Right        = scaledWidth;
                            colorspace   = istr.ReadByte();
                            bpc          = 8;
                            break;
                        }
                        else if (markertype == UNSUPPORTED_MARKER)
                        {
                            throw new BadElementException(errorId + ": unsupported JPEG marker: " + marker);
                        }
                        else if (markertype != NOPARAM_MARKER)
                        {
                            Utilities.Skip(istr, getShort(istr) - 2);
                        }
                    }
                }
            }
            finally
            {
                if (istr != null)
                {
                    istr.Dispose();
                }
            }
            plainWidth  = Width;
            plainHeight = Height;
            if (_icc != null)
            {
                var total = 0;
                for (var k = 0; k < _icc.Length; ++k)
                {
                    if (_icc[k] == null)
                    {
                        _icc = null;
                        return;
                    }
                    total += _icc[k].Length - 14;
                }
                var ficc = new byte[total];
                total = 0;
                for (var k = 0; k < _icc.Length; ++k)
                {
                    Array.Copy(_icc[k], 14, ficc, total, _icc[k].Length - 14);
                    total += _icc[k].Length - 14;
                }
                try
                {
                    var iccProf = IccProfile.GetInstance(ficc);
                    TagIcc = iccProf;
                }
                catch { }
                _icc = null;
            }
        }