예제 #1
0
        public static IBitmap Load(string filename)
        {
reattempt_load:
            try {
                if (filename.EndsWith(".im", StringComparison.OrdinalIgnoreCase))
                {
                    return(ImImageLoader.Load(filename));
                }
            }
            catch (OutOfMemoryException) {
                GC.WaitForPendingFinalizers();
                goto reattempt_load;
            }

            try {
                return(new GdiBitmap((Bitmap)Bitmap.FromFile(filename)));
            }
            catch
            {
                /*int foo = filename.LastIndexOf('/');
                 * string newfilename = filename.Substring(0, foo + 1) + "damaged_" + filename.Substring(foo + 1);
                 * System.IO.File.Move(filename, newfilename);*/
                return(null);
            }
            //return (Bitmap)Bitmap.FromFile(filename);
        }
예제 #2
0
        public static IBitmap Load(string filename, List <GLTextureStream.ImageMetaData> meta)
        {
reattempt_load:
            try {
                if (filename.EndsWith(".im", StringComparison.OrdinalIgnoreCase))
                {
                    return(ImImageLoader.Load(filename));
                }
            }
            catch (OutOfMemoryException) {
                GC.WaitForPendingFinalizers();
                goto reattempt_load;
            }

            Dictionary <ExifTags, GLTextureStream.ImageMetaData> exifdict = null;

            if (filename.EndsWith(".jpg", StringComparison.OrdinalIgnoreCase))
            {
                try
                {
                    using (ExifReader reader = new ExifReader(filename))
                    {
                        //foreach (ExifTags tag in Enum.GetValues(typeof(ExifTags)))
                        //	ReadExifValue(reader, tag, ref meta);

                        /*ReadExifRational(reader, ExifTags.ExposureTime, ref meta);
                         * ReadExifRational(reader, ExifTags.FNumber, ref meta);
                         * ReadExifShort(reader, ExifTags.ExposureProgram, ref meta);
                         * ReadExifShort(reader, ExifTags.PhotographicSensitivity, ref meta);
                         * ReadExifShort(reader, ExifTags.SensitivityType, ref meta);
                         * ReadExifLong(reader, ExifTags.RecommendedExposureIndex, ref meta);
                         * //ReadExifValue(reader, ExifTags.ExifVersion, ref meta); //undefined
                         * ReadExifDate(reader, ExifTags.DateTimeOriginal, ref meta);
                         * ReadExifDate(reader, ExifTags.DateTimeDigitized, ref meta);
                         * //ReadExifValue(reader, ExifTags.ComponentsConfiguration, ref meta); //undefined
                         * ReadExifRational(reader, ExifTags.CompressedBitsPerPixel, ref meta);
                         * ReadExifRational(reader, ExifTags.BrightnessValue, ref meta);
                         * ReadExifRational(reader, ExifTags.ExposureBiasValue, ref meta);
                         * ReadExifRational(reader, ExifTags.MaxApertureValue, ref meta);
                         * ReadExifShort(reader, ExifTags.MeteringMode, ref meta);
                         * ReadExifShort(reader, ExifTags.LightSource, ref meta);
                         * ReadExifShort(reader, ExifTags.Flash, ref meta);
                         * ReadExifRational(reader, ExifTags.FocalLength, ref meta);
                         * //ReadExifValue(reader, ExifTags.MakerNote, ref meta); //undefined
                         * //ReadExifValue(reader, ExifTags.UserComment, ref meta); //comment
                         * //ReadExifValue(reader, ExifTags.FlashpixVersion, ref meta); //undefined
                         * ReadExifShort(reader, ExifTags.ColorSpace, ref meta);
                         * ReadExifLong(reader, ExifTags.PixelXDimension, ref meta);
                         * ReadExifLong(reader, ExifTags.PixelYDimension, ref meta);
                         * ReadExifShort(reader, ExifTags.CustomRendered, ref meta);
                         * ReadExifShort(reader, ExifTags.ExposureMode, ref meta);
                         * ReadExifShort(reader, ExifTags.WhiteBalance, ref meta);
                         * ReadExifRational(reader, ExifTags.DigitalZoomRatio, ref meta);
                         * ReadExifShort(reader, ExifTags.FocalLengthIn35mmFilm, ref meta);
                         * ReadExifShort(reader, ExifTags.SceneCaptureType, ref meta);
                         * ReadExifShort(reader, ExifTags.Contrast, ref meta);
                         * ReadExifShort(reader, ExifTags.Saturation, ref meta);
                         * ReadExifShort(reader, ExifTags.Sharpness, ref meta);
                         * ReadExifRational(reader, ExifTags.LensSpecification, ref meta);
                         * ReadExifString(reader, ExifTags.LensModel, ref meta);*/


                        exifdict = new Dictionary <ExifTags, GLTextureStream.ImageMetaData>();
                        foreach (ExifTags tag in Enum.GetValues(typeof(ExifTags)))
                        {
                            if (exifdict.ContainsKey(tag))
                            {
                                continue;
                            }

                            object val;
                            if (reader.GetTagValue(tag, out val))
                            {
                                GLTextureStream.ImageMetaData m;
                                if (val is string)
                                {
                                    m = new GLTextureStream.ImageMetaData(tag.ToString(), 0.0f, (string)val);
                                }
                                else if (val is Array)
                                {
                                    continue;                                     // Skip arrays
                                }
                                else
                                {
                                    m = new GLTextureStream.ImageMetaData(tag.ToString(), (float)(dynamic)val, val.ToString());
                                }

                                exifdict.Add(tag, m);
                                meta.Add(m);
                            }
                        }

                        /*int[] rational;
                         * UInt16 word;
                         * string str;
                         * if(reader.GetTagValue(ExifTags.ApertureValue, out rational))
                         * {
                         *      float value = (float)rational[0] / (float)rational[1];
                         *      meta.Add(new GLTextureStream.ImageMetaData("Aperture", value, value.ToString()));
                         * }
                         * if (reader.GetTagValue(ExifTags.BrightnessValue, out rational))
                         * {
                         *      float value = (float)rational[0] / (float)rational[1];
                         *      meta.Add(new GLTextureStream.ImageMetaData("BrightnessValue", value, value.ToString()));
                         * }
                         * if (reader.GetTagValue(ExifTags.ExposureBiasValue, out rational))
                         * {
                         *      float value = (float)rational[0] / (float)rational[1];
                         *      meta.Add(new GLTextureStream.ImageMetaData("ExposureBiasValue", value, value.ToString()));
                         * }
                         * if(reader.GetTagValue(ExifTags.PhotographicSensitivity, out word))
                         * {
                         *      meta.Add(new GLTextureStream.ImageMetaData("ISO", (float)word, word.ToString()));
                         * }
                         * if(reader.GetTagValue(ExifTags.DateTime, out str))
                         * {
                         *      DateTime date = DateTime.ParseExact(str, "yyyy:MM:dd HH:mm:ss", System.Globalization.CultureInfo.CurrentCulture);
                         *      meta.Add(new GLTextureStream.ImageMetaData("Date", (float)date.Ticks, date.ToShortDateString()));
                         * }*/

                        /*object obj;
                         * if(reader.GetTagValue(ExifTags.PhotographicSensitivity, out obj))
                         * {
                         *      Type type = obj.GetType();
                         *      while (type != null)
                         *      {
                         *              System.Console.WriteLine(type.Name);
                         *              type = type.BaseType;
                         *      }
                         *      int abc = 0;
                         * }*/
                    }
                }
                catch
                {
                }
            }

reattempt_load2:
            try {
                Bitmap bmp = (Bitmap)Bitmap.FromFile(filename);
                GLTextureStream.ImageMetaData orientation;
                if (exifdict != null && exifdict.TryGetValue(ExifTags.Orientation, out orientation))
                {
                    switch ((byte)orientation.value)
                    {
                    case 1: break;

                    case 2: bmp.RotateFlip(RotateFlipType.RotateNoneFlipX); break;

                    case 3: bmp.RotateFlip(RotateFlipType.Rotate180FlipNone); break;

                    case 4: bmp.RotateFlip(RotateFlipType.RotateNoneFlipY); break;

                    case 5: bmp.RotateFlip(RotateFlipType.Rotate90FlipX); break;

                    case 6: bmp.RotateFlip(RotateFlipType.Rotate90FlipNone); break;

                    case 7: bmp.RotateFlip(RotateFlipType.Rotate90FlipY); break;

                    case 8: bmp.RotateFlip(RotateFlipType.Rotate270FlipNone); break;
                    }
                }
                return(new GdiBitmap(bmp));
            }
            catch (OutOfMemoryException) {
                GC.WaitForPendingFinalizers();
                goto reattempt_load2;
            }
            catch
            {
                /*int foo = filename.LastIndexOf('/');
                 * string newfilename = filename.Substring(0, foo + 1) + "damaged_" + filename.Substring(foo + 1);
                 * System.IO.File.Move(filename, newfilename);*/
                return(null);
            }
            //return (Bitmap)Bitmap.FromFile(filename);
        }