Esempio n. 1
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);
                    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;
            }
        }