public void DecodeGenerated_SaveBmp <TPixel>(
            TestImageProvider <TPixel> provider,
            JpegSubsample subsample,
            int quality)
            where TPixel : struct, IPixel <TPixel>
        {
            byte[] data;
            using (Image <TPixel> image = provider.GetImage())
            {
                JpegEncoder        encoder = new JpegEncoder();
                JpegEncoderOptions options = new JpegEncoderOptions {
                    Subsample = subsample, Quality = quality
                };

                data = new byte[65536];
                using (MemoryStream ms = new MemoryStream(data))
                {
                    image.Save(ms, encoder, options);
                }
            }

            // TODO: Automatic image comparers could help here a lot :P
            Image <TPixel> mirror = provider.Factory.CreateImage(data);

            provider.Utility.TestName += $"_{subsample}_Q{quality}";
            provider.Utility.SaveTestOutputFile(mirror, "bmp");
        }
        public void EncodeJpeg(int executionCount, int quality, JpegSubsample subsample)
        {
            string[] testFiles = TestImages.Bmp.All
                                 .Concat(new[] { TestImages.Jpeg.Baseline.Calliphora, TestImages.Jpeg.Baseline.Cmyk })
                                 .ToArray();

            Image <Rgba32>[] testImages =
                testFiles.Select(
                    tf => TestImageProvider <Rgba32> .File(tf, pixelTypeOverride: PixelTypes.StandardImageClass).GetImage())
                .ToArray();

            using (MemoryStream ms = new MemoryStream())
            {
                this.Measure(executionCount,
                             () =>
                {
                    foreach (Image <Rgba32> img in testImages)
                    {
                        JpegEncoder encoder        = new JpegEncoder();
                        JpegEncoderOptions options = new JpegEncoderOptions {
                            Quality = quality, Subsample = subsample
                        };
                        img.Save(ms, encoder, options);
                        ms.Seek(0, SeekOrigin.Begin);
                    }
                },
                             // ReSharper disable once ExplicitCallerInfoArgument
                             $@"Encode {testFiles.Length} images"
                             );
            }
        }
Beispiel #3
0
        /// <summary>
        /// Output JPEG image to browser or a file.
        /// </summary>
        public static bool imagejpeg(Context ctx, PhpResource im, PhpValue to, int quality = 75)
        {
            var jpegoptions = new JpegEncoderOptions()
            {
                Quality = Math.Min(Math.Max(quality, 0), 100)
            };

            return(imagesave(ctx, im, to, (img, stream) => img.SaveAsJpeg(stream, jpegoptions)));
        }
        public void LoadResizeSave <TColor>(TestImageProvider <TColor> provider, int quality, JpegSubsample subsample)
            where TColor : struct, IPixel <TColor>
        {
            using (Image <TColor> image = provider.GetImage().Resize(new ResizeOptions {
                Size = new Size(150, 100), Mode = ResizeMode.Max
            }))
            {
                image.MetaData.Quality     = quality;
                image.MetaData.ExifProfile = null; // Reduce the size of the file
                JpegEncoder        encoder = new JpegEncoder();
                JpegEncoderOptions options = new JpegEncoderOptions {
                    Subsample = subsample, Quality = quality
                };

                provider.Utility.TestName += $"{subsample}_Q{quality}";
                provider.Utility.SaveTestOutputFile(image, "png");
                provider.Utility.SaveTestOutputFile(image, "jpg", encoder, options);
            }
        }
        public void Encode_IgnoreMetadataIsTrue_ExifProfileIgnored()
        {
            JpegEncoderOptions options = new JpegEncoderOptions()
            {
                IgnoreMetadata = true
            };

            TestFile testFile = TestFile.Create(TestImages.Jpeg.Baseline.Floorplan);

            using (Image input = testFile.CreateImage())
            {
                using (MemoryStream memStream = new MemoryStream())
                {
                    input.SaveAsJpeg(memStream, options);

                    memStream.Position = 0;
                    using (Image output = Image.Load(memStream))
                    {
                        Assert.Null(output.MetaData.ExifProfile);
                    }
                }
            }
        }
Beispiel #6
0
        protected override TaskOutput Execute(TaskInput input, ITaskContext context)
        {
            foreach (var file in input.Files)
            {
                try
                {
                    IEncoderOptions options = new JpegEncoderOptions(90, false);
                    switch (_convertFormat)
                    {
                    case ConvertFormat.Jpeg:
                        options = new JpegEncoderOptions(90, false);
                        break;

                    case ConvertFormat.Png:
                        options = new PngEncoderOptions();
                        break;

                    case ConvertFormat.Tiff:
                        options = new TiffEncoderOptions(CompressionType.Zip);
                        break;
                    }

                    var isConverted = false;
                    using (var readStream = new FileStream(file.Path, FileMode.Open))
                    {
                        using (var reader = FormatManager.CreateFormatReader(readStream))
                        {
                            if (reader.MediaFormat != options.MediaFormat)
                            {
                                using (var frame = reader.LoadFrame(0))
                                {
                                    using (var resultBitmap = new Bitmap(frame.Width, frame.Height, Aurigma.GraphicsMill.PixelFormat.Format24bppRgb))
                                    {
                                        frame.GetBitmap(resultBitmap);

                                        var conveter = new PixelFormatConverter {
                                            DestinationPixelFormat = Aurigma.GraphicsMill.PixelFormat.Format32bppArgb
                                        };
                                        conveter.ApplyTransform(resultBitmap);

                                        if (resultBitmap.HasAlpha)
                                        {
                                            resultBitmap.Channels.DiscardAlpha(RgbColor.White);
                                        }

                                        resultBitmap.Save(file.Path + ".tmp", options);
                                        isConverted = true;
                                    }
                                }
                            }
                        }
                    }

                    if (isConverted)
                    {
                        File.Delete(file.Path);
                        File.Move(file.Path + ".tmp", file.Path);
                    }
                }
                catch (System.Exception ex)
                {
                    Logger.ErrorException(string.Format("Error while converting file {1} to {0}", _convertFormat, file.Path), ex);
                }
            }
            return(new TaskOutput(input.Files, input.Params));
        }
            private async Task UploadWholeFileAsync(Command message, CommandResult result)
            {
                const int    THUMB_WIDTH            = 80;
                const int    THUMB_HEIGHT           = 80;
                const int    NORMAL_IMAGE_MAX_WIDTH = 540;
                const string THUMBS_FOLDER_NAME     = "thumbs";

                // Ensure the storage root exists.
                Directory.CreateDirectory(_filesHelper.StorageRootPath);

                foreach (var file in message.Files)
                {
                    var extension = Path.GetExtension(file.FileName);
                    if (!_allowedExtensions.Contains(extension))
                    {
                        // This is not a supported image type.
                        throw new InvalidOperationException($"Unsupported image type: {extension}. The supported types are: {string.Join(", ", _allowedExtensions)}");
                    }

                    if (file.Length > 0L)
                    {
                        var fullPath = Path.Combine(_filesHelper.StorageRootPath, Path.GetFileName(file.FileName));
                        using (var fs = new FileStream(fullPath, FileMode.Create))
                        {
                            await file.CopyToAsync(fs);
                        }


                        //
                        // Create an 80x80 thumbnail.
                        //

                        var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(file.FileName);
                        var thumbName = $"{fileNameWithoutExtension}{THUMB_WIDTH}x{THUMB_HEIGHT}{extension}";
                        var thumbPath = Path.Combine(_filesHelper.StorageRootPath, THUMBS_FOLDER_NAME, thumbName);

                        using (var originalImage = Image.Load(fullPath))
                            using (var thumbStream = File.OpenWrite(thumbPath))
                            {
                                originalImage
                                .Resize(THUMB_WIDTH, THUMB_HEIGHT)
                                .Save(thumbStream);

                                var width = originalImage.Width;
                            }

                        // If the image is wider than 540px, resize it so that it is 540px wide. Otherwise, upload a copy of the original.
                        using (var originalImage = Image.Load(fullPath))
                        {
                            if (originalImage.Width > NORMAL_IMAGE_MAX_WIDTH)
                            {
                                // Resize it so that the max width is 540px. Maintain the aspect ratio.
                                var newHeight = originalImage.Height * NORMAL_IMAGE_MAX_WIDTH / originalImage.Width;

                                var normalImageName = $"{fileNameWithoutExtension}{NORMAL_IMAGE_MAX_WIDTH}x{newHeight}{extension}";
                                var normalImagePath = Path.Combine(_filesHelper.StorageRootPath, normalImageName);

                                IEncoderOptions encoderOptions = null;
                                if (extension == ".jpg" || extension == ".jpeg")
                                {
                                    encoderOptions = new JpegEncoderOptions {
                                        Quality = 90
                                    };
                                }

                                using (var normalImageStream = File.OpenWrite(normalImagePath))
                                {
                                    originalImage
                                    .Resize(NORMAL_IMAGE_MAX_WIDTH, newHeight)
                                    .Save(normalImageStream, encoderOptions);
                                }
                            }
                        }
                    }

                    result.FileResults.Add(UploadResult(file.FileName, file.Length));
                }
            }