public WriteableBitmap LoadImage(FileItem fileItem, bool fullres, bool showfocuspoints)
        {
            if (fileItem == null)
            {
                return(null);
            }
            if (!File.Exists(fileItem.LargeThumb) && !fullres)
            {
                return(null);
            }
            if (File.Exists(fileItem.InfoFile))
            {
                fileItem.LoadInfo();
            }
            if (fileItem.FileInfo == null)
            {
                fileItem.FileInfo = new FileInfo();
            }

            try
            {
                BitmapDecoder bmpDec = null;
                if (fullres && fileItem.IsRaw)
                {
                    try
                    {
                        string dcraw_exe = Path.Combine(Settings.ApplicationFolder, "dcraw.exe");
                        if (File.Exists(dcraw_exe))
                        {
                            string thumb = Path.Combine(Path.GetTempPath(),
                                                        Path.GetFileNameWithoutExtension(fileItem.FileName) + ".thumb.jpg");
                            PhotoUtils.RunAndWait(dcraw_exe,
                                                  string.Format(" -e -O \"{0}\" \"{1}\"", thumb, fileItem.FileName));
                            if (File.Exists(thumb))
                            {
                                bmpDec = BitmapDecoder.Create(new Uri(thumb), BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
                                File.Delete(thumb);
                            }
                        }
                    }
                    catch (Exception exception)
                    {
                        Log.Error("Error get dcraw thumb", exception);
                    }
                }

                PhotoUtils.WaitForFile(fullres ? fileItem.FileName : fileItem.LargeThumb);
                if (bmpDec == null)
                {
                    bmpDec = BitmapDecoder.Create(new Uri(fullres ? fileItem.FileName : fileItem.LargeThumb),
                                                  BitmapCreateOptions.None,
                                                  BitmapCacheOption.OnLoad);
                }
                // if no future processing required
                if (!showfocuspoints && ServiceProvider.Settings.RotateIndex == 0 && !ServiceProvider.Settings.FlipPreview)
                {
                    fileItem.Loading = false;
                    var b = new WriteableBitmap(bmpDec.Frames[0]);
                    b.Freeze();
                    return(b);
                }

                var bitmap = BitmapFactory.ConvertToPbgra32Format(bmpDec.Frames[0]);

                if (showfocuspoints && !fileItem.Transformed)
                {
                    DrawFocusPoints(fileItem, bitmap);
                }


                if (ServiceProvider.Settings.RotateIndex != 0)
                {
                    switch (ServiceProvider.Settings.RotateIndex)
                    {
                    case 1:
                        bitmap = bitmap.Rotate(90);
                        break;

                    case 2:
                        bitmap = bitmap.Rotate(180);
                        break;

                    case 3:
                        bitmap = bitmap.Rotate(270);
                        break;
                    }
                }

                if (ServiceProvider.Settings.FlipPreview)
                {
                    bitmap = bitmap.Flip(WriteableBitmapExtensions.FlipMode.Vertical);
                }

                bitmap.Freeze();
                return(bitmap);
            }
            catch (Exception exception)
            {
                Log.Error("Error loading image", exception);
                if (exception.GetType() == typeof(OutOfMemoryException) && fullres)
                {
                    return(LoadImage(fileItem, false));
                }
            }
            return(null);
        }
        public void GenerateCache(FileItem fileItem)
        {
            bool deleteFile = false;

            if (fileItem == null)
            {
                return;
            }
            if (!File.Exists(fileItem.FileName))
            {
                return;
            }

            if ((File.Exists(fileItem.LargeThumb) && File.Exists(fileItem.SmallThumb)) && File.Exists(fileItem.InfoFile))
            {
                return;
            }

            if (fileItem.Loading)
            {
                return;
            }

            fileItem.Loading = true;

            PhotoUtils.WaitForFile(fileItem.FileName);
            string filename = fileItem.FileName;

            if (fileItem.IsMovie)
            {
                try
                {
                    string ffmpeg_exe = Path.Combine(Settings.ApplicationFolder, "ffmpeg.exe");
                    if (File.Exists(ffmpeg_exe))
                    {
                        string thumb = Path.Combine(Path.GetDirectoryName(fileItem.FileName),
                                                    Path.GetFileNameWithoutExtension(fileItem.FileName) + ".thumb.jpg");
                        PhotoUtils.RunAndWait(ffmpeg_exe, String.Format("-i \"{0}\" -ss 00:00:01.000 -f image2 -vframes 1 \"{1}\"", fileItem.FileName, thumb));
                        if (File.Exists(thumb))
                        {
                            deleteFile = true;
                            filename   = thumb;
                        }
                    }
                }
                catch (Exception exception)
                {
                    Log.Error("Error get video thumb", exception);
                }
            }
            if (fileItem.IsRaw)
            {
                try
                {
                    string dcraw_exe = Path.Combine(Settings.ApplicationFolder, "dcraw.exe");
                    if (File.Exists(dcraw_exe))
                    {
                        string thumb = Path.Combine(Path.GetTempPath(),
                                                    Path.GetFileNameWithoutExtension(fileItem.FileName) + ".thumb.jpg");
                        PhotoUtils.RunAndWait(dcraw_exe,
                                              string.Format(" -e -O \"{0}\" \"{1}\"", thumb, fileItem.FileName));
                        if (File.Exists(thumb))
                        {
                            deleteFile = true;
                            filename   = thumb;
                        }
                    }
                }
                catch (Exception exception)
                {
                    Log.Error("Error get dcraw thumb", exception);
                }
            }

            GetMetadata(fileItem);
            try
            {
                using (MagickImage image = new MagickImage(filename))
                {
                    fileItem.FileInfo.SetSize(image.Width, image.Height);

                    double dw = (double)LargeThumbSize / image.Width;
                    image.FilterType = FilterType.Box;
                    image.Thumbnail((int)(image.Width * dw), (int)(image.Height * dw));

                    if (!ServiceProvider.Settings.DisableHardwareAccelerationNew)
                    {
                        image.UnsharpMask(1, 1, 0.5, 0.1);
                    }

                    PhotoUtils.CreateFolder(fileItem.LargeThumb);
                    image.Write(fileItem.LargeThumb);
                    fileItem.IsLoaded = true;
                    fileItem.Loading  = false;

                    dw = (double)SmallThumbSize / image.Width;
                    image.Thumbnail((int)(image.Width * dw), (int)(image.Height * dw));

                    if (!ServiceProvider.Settings.DisableHardwareAccelerationNew)
                    {
                        image.UnsharpMask(1, 1, 0.5, 0.1);
                    }

                    PhotoUtils.CreateFolder(fileItem.SmallThumb);
                    image.Write(fileItem.SmallThumb);

                    fileItem.Thumbnail = LoadImage(fileItem.SmallThumb);
                }
                fileItem.SaveInfo();
                SetImageInfo(fileItem);
                if (deleteFile)
                {
                    File.Delete(filename);
                }
                OnMetaDataUpdated(fileItem);
            }
            catch (Exception exception)
            {
                Log.Error("Error generating cache " + fileItem.FileName, exception);
            }
            fileItem.Loading = false;
        }
Beispiel #3
0
        public void GenerateCache(FileItem fileItem)
        {
            bool deleteFile = false;

            if (fileItem == null)
            {
                return;
            }
            if (!File.Exists(fileItem.FileName))
            {
                return;
            }

            if ((File.Exists(fileItem.LargeThumb) && File.Exists(fileItem.SmallThumb)) && File.Exists(fileItem.InfoFile))
            {
                return;
            }

            string filename = fileItem.FileName;

            if (fileItem.IsMovie)
            {
                try
                {
                    string ffmpeg_exe = Path.Combine(Settings.ApplicationFolder, "ffmpeg.exe");
                    if (File.Exists(ffmpeg_exe))
                    {
                        string thumb = Path.Combine(Path.GetDirectoryName(fileItem.FileName),
                                                    Path.GetFileNameWithoutExtension(fileItem.FileName) + ".thumb.jpg");
                        PhotoUtils.RunAndWait(ffmpeg_exe, String.Format("-i \"{0}\" -ss 00:00:01.000 -f image2 -vframes 1 \"{1}\"", fileItem.FileName, thumb));
                        if (File.Exists(thumb))
                        {
                            deleteFile = true;
                            filename   = thumb;
                        }
                    }
                }
                catch (Exception exception)
                {
                    Log.Error("Error get video thumb", exception);
                }
            }
            if (fileItem.IsRaw)
            {
                try
                {
                    string dcraw_exe = Path.Combine(Settings.ApplicationFolder, "dcraw.exe");
                    if (File.Exists(dcraw_exe))
                    {
                        PhotoUtils.RunAndWait(dcraw_exe, string.Format(" -e \"{0}\"", fileItem.FileName));
                        string thumb = Path.Combine(Path.GetDirectoryName(fileItem.FileName),
                                                    Path.GetFileNameWithoutExtension(fileItem.FileName) + ".thumb.jpg");
                        if (File.Exists(thumb))
                        {
                            deleteFile = true;
                            filename   = thumb;
                        }
                    }
                }
                catch (Exception exception)
                {
                    Log.Error("Error get dcraw thumb", exception);
                }
            }


            GetMetadata(fileItem);
            try
            {
                var rotation = 0;

                if (fileItem.FileInfo.ExifTags.ContainName("Exif.Image.Orientation"))
                {
                    if (fileItem.FileInfo.ExifTags["Exif.Image.Orientation"] == "bottom, right")
                    {
                        rotation = 180;
                    }

                    //if (fileItem.FileInfo.ExifTags["Exif.Image.Orientation"] == "top, left")
                    //    writeableBitmap = writeableBitmap.Rotate(180);

                    if (fileItem.FileInfo.ExifTags["Exif.Image.Orientation"] == "right, top")
                    {
                        rotation = 90;
                    }

                    if (fileItem.FileInfo.ExifTags["Exif.Image.Orientation"] == "left, bottom")
                    {
                        rotation = 270;
                    }
                }

                using (MemoryStream fileStream = new MemoryStream(File.ReadAllBytes(filename)))
                {
                    BitmapDecoder bmpDec = BitmapDecoder.Create(fileStream,
                                                                BitmapCreateOptions.PreservePixelFormat,
                                                                BitmapCacheOption.OnLoad);

                    bmpDec.DownloadProgress += (o, args) => StaticHelper.Instance.LoadingProgress = args.Progress;

                    if (rotation == 90 || rotation == 270)
                    {
                        fileItem.FileInfo.SetSize(bmpDec.Frames[0].PixelHeight, bmpDec.Frames[0].PixelWidth);
                    }
                    else
                    {
                        fileItem.FileInfo.SetSize(bmpDec.Frames[0].PixelWidth, bmpDec.Frames[0].PixelHeight);
                    }

                    double          dw = (double)LargeThumbSize / bmpDec.Frames[0].PixelWidth;
                    WriteableBitmap writeableBitmap =
                        BitmapFactory.ConvertToPbgra32Format(GetBitmapFrame(bmpDec.Frames[0],
                                                                            (int)(bmpDec.Frames[0].PixelWidth * dw),
                                                                            (int)(bmpDec.Frames[0].PixelHeight * dw),
                                                                            BitmapScalingMode.Linear));

                    LoadHistogram(fileItem, writeableBitmap);
                    Save2Jpg(writeableBitmap, fileItem.LargeThumb);

                    dw = (double)SmallThumbSize / writeableBitmap.PixelWidth;
                    writeableBitmap = writeableBitmap.Resize((int)(writeableBitmap.PixelWidth * dw),
                                                             (int)(writeableBitmap.PixelHeight * dw),
                                                             WriteableBitmapExtensions.Interpolation.Bilinear);

                    if (rotation > 0)
                    {
                        writeableBitmap = writeableBitmap.Rotate(rotation);
                    }

                    Save2Jpg(writeableBitmap, fileItem.SmallThumb);
                    //var thumb = LoadSmallImage(fileItem);
                    //thumb.Freeze();
                    fileItem.Thumbnail = LoadImage(fileItem.SmallThumb);
                }

                //using (MagickImage image = new MagickImage(filename))
                //{
                //    if (rotation == 90 || rotation == 270)
                //        fileItem.FileInfo.SetSize(image.Height, image.Width);
                //    else
                //        fileItem.FileInfo.SetSize(image.Width, image.Height);

                //    double dw = (double)LargeThumbSize / image.Width;
                //    image.Thumbnail(dw*100);
                //    LoadHistogram(fileItem, image);
                //    image.Write(fileItem.LargeThumb);
                //    dw = (double)LargeThumbSize / image.Width;
                //    image.Thumbnail(dw * 100);
                //    image.Write(fileItem.SmallThumb);
                //    fileItem.Thumbnail = LoadImage(fileItem.SmallThumb);
                //}

                fileItem.IsLoaded = true;
                fileItem.SaveInfo();
                if (deleteFile)
                {
                    File.Delete(filename);
                }
            }
            catch (Exception exception)
            {
                Log.Error("Error generating cache " + fileItem.FileName, exception);
            }
        }
Beispiel #4
0
        public WriteableBitmap LoadImage(FileItem fileItem, bool fullres, bool showfocuspoints)
        {
            int rotation = 0;

            if (fileItem == null)
            {
                return(null);
            }
            if (!File.Exists(fileItem.LargeThumb) && !fullres)
            {
                return(null);
            }

            if (File.Exists(fileItem.InfoFile))
            {
                fileItem.LoadInfo();
            }
            else
            {
                fileItem.FileInfo = new FileInfo();
            }

            try
            {
                BitmapDecoder bmpDec = null;
                if (fullres && fileItem.IsRaw)
                {
                    try
                    {
                        string dcraw_exe = Path.Combine(Settings.ApplicationFolder, "dcraw.exe");
                        if (File.Exists(dcraw_exe))
                        {
                            PhotoUtils.RunAndWait(dcraw_exe, string.Format(" -e \"{0}\"", fileItem.FileName));
                            string thumb = Path.Combine(Path.GetDirectoryName(fileItem.FileName),
                                                        Path.GetFileNameWithoutExtension(fileItem.FileName) + ".thumb.jpg");
                            if (File.Exists(thumb))
                            {
                                bmpDec = BitmapDecoder.Create(new Uri(thumb), BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
                                File.Delete(thumb);
                            }
                        }
                    }
                    catch (Exception exception)
                    {
                        Log.Error("Error get dcraw thumb", exception);
                    }
                }

                if (ServiceProvider.Settings.Autorotate && fileItem.FileInfo != null && fileItem.FileInfo.ExifTags.ContainName("Exif.Image.Orientation"))
                {
                    if (fileItem.FileInfo.ExifTags["Exif.Image.Orientation"] == "bottom, right")
                    {
                        rotation = 180;
                    }

                    //if (fileItem.FileInfo.ExifTags["Exif.Image.Orientation"] == "top, left")
                    //    bitmap = bitmap.Rotate(180);

                    if (fileItem.FileInfo.ExifTags["Exif.Image.Orientation"] == "right, top")
                    {
                        rotation = 90;
                    }

                    if (fileItem.FileInfo.ExifTags["Exif.Image.Orientation"] == "left, bottom")
                    {
                        rotation = 270;
                    }
                }

                if (bmpDec == null)
                {
                    bmpDec = BitmapDecoder.Create(new Uri(fullres ? fileItem.FileName : fileItem.LargeThumb),
                                                  BitmapCreateOptions.None,
                                                  BitmapCacheOption.OnLoad);
                }
                // if no future processing required
                if (rotation == 0 && !showfocuspoints && ServiceProvider.Settings.RotateIndex == 0 && ServiceProvider.Settings.FlipPreview)
                {
                    return(new WriteableBitmap(bmpDec.Frames[0]));
                }


                var bitmap = BitmapFactory.ConvertToPbgra32Format(bmpDec.Frames[0]);


                if (showfocuspoints)
                {
                    DrawFocusPoints(fileItem, bitmap);
                }

                if (rotation != 0)
                {
                    bitmap = bitmap.Rotate(rotation);
                }

                if (ServiceProvider.Settings.RotateIndex != 0)
                {
                    switch (ServiceProvider.Settings.RotateIndex)
                    {
                    case 1:
                        bitmap = bitmap.Rotate(90);
                        break;

                    case 2:
                        bitmap = bitmap.Rotate(180);
                        break;

                    case 3:
                        bitmap = bitmap.Rotate(270);
                        break;
                    }
                }

                if (ServiceProvider.Settings.FlipPreview)
                {
                    bitmap = bitmap.Flip(WriteableBitmapExtensions.FlipMode.Vertical);
                }

                bitmap.Freeze();
                return(bitmap);
            }
            catch (Exception exception)
            {
                Log.Error("Error loading image", exception);
            }
            return(null);
        }
        public WriteableBitmap LoadImage(FileItem fileItem, bool fullres, bool showfocuspoints)
        {
            if (ServiceProvider.Settings.LowMemoryUsage)
            {
                fullres = false;
            }

            if (fileItem == null)
            {
                return(null);
            }
            if (!File.Exists(fileItem.LargeThumb) && !fullres)
            {
                return(null);
            }

            if (File.Exists(fileItem.InfoFile))
            {
                fileItem.LoadInfo();
            }
            else
            {
                fileItem.FileInfo = new FileInfo();
            }

            try
            {
                BitmapDecoder bmpDec = null;
                if (fullres && fileItem.IsRaw)
                {
                    try
                    {
                        string dcraw_exe = Path.Combine(Settings.ApplicationFolder, "dcraw.exe");
                        if (File.Exists(dcraw_exe))
                        {
                            PhotoUtils.RunAndWait(dcraw_exe, string.Format(" -e \"{0}\"", fileItem.FileName));
                            string thumb = Path.Combine(Path.GetDirectoryName(fileItem.FileName),
                                                        Path.GetFileNameWithoutExtension(fileItem.FileName) + ".thumb.jpg");
                            if (File.Exists(thumb))
                            {
                                bmpDec = BitmapDecoder.Create(new Uri(thumb), BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
                                File.Delete(thumb);
                            }
                        }
                    }
                    catch (Exception exception)
                    {
                        Log.Error("Error get dcraw thumb", exception);
                    }
                }

                if (bmpDec == null)
                {
                    bmpDec = BitmapDecoder.Create(new Uri(fullres ? fileItem.FileName : fileItem.LargeThumb),
                                                  BitmapCreateOptions.None,
                                                  BitmapCacheOption.OnLoad);
                }

                double          dw = (double)MaxThumbSize / bmpDec.Frames[0].PixelWidth;
                WriteableBitmap bitmap;

                /*
                 * if (fullres)
                 *  bitmap =
                 *      BitmapFactory.ConvertToPbgra32Format(GetBitmapFrame(bmpDec.Frames[0],
                 *                                                          (int) (bmpDec.Frames[0].PixelWidth*dw),
                 *                                                          (int) (bmpDec.Frames[0].PixelHeight*dw),
                 *                                                          BitmapScalingMode.NearestNeighbor));
                 * else
                 *  bitmap = BitmapFactory.ConvertToPbgra32Format(bmpDec.Frames[0]);
                 */
                bitmap = BitmapFactory.ConvertToPbgra32Format(bmpDec.Frames[0]);

                if (showfocuspoints)
                {
                    DrawFocusPoints(fileItem, bitmap);
                }

                if (fileItem.FileInfo != null && fileItem.FileInfo.ExifTags.ContainName("Exif.Image.Orientation"))
                {
                    if (fileItem.FileInfo.ExifTags["Exif.Image.Orientation"] == "bottom, right")
                    {
                        bitmap = bitmap.Rotate(180);
                    }

                    //if (fileItem.FileInfo.ExifTags["Exif.Image.Orientation"] == "top, left")
                    //    bitmap = bitmap.Rotate(180);

                    if (fileItem.FileInfo.ExifTags["Exif.Image.Orientation"] == "right, top")
                    {
                        bitmap = bitmap.Rotate(90);
                    }

                    if (fileItem.FileInfo.ExifTags["Exif.Image.Orientation"] == "left, bottom")
                    {
                        bitmap = bitmap.Rotate(270);
                    }
                }

                if (ServiceProvider.Settings.RotateIndex != 0)
                {
                    switch (ServiceProvider.Settings.RotateIndex)
                    {
                    case 1:
                        bitmap = bitmap.Rotate(90);
                        break;

                    case 2:
                        bitmap = bitmap.Rotate(180);
                        break;

                    case 3:
                        bitmap = bitmap.Rotate(270);
                        break;
                    }
                }

                if (ServiceProvider.Settings.FlipPreview)
                {
                    bitmap = bitmap.Flip(WriteableBitmapExtensions.FlipMode.Vertical);
                }

                bitmap.Freeze();
                return(bitmap);
            }
            catch (Exception exception)
            {
                Log.Error("Error loading image", exception);
            }
            return(null);
        }