From 52c5ea07e89287b50f39c572d6db6feed2a65f20 Mon Sep 17 00:00:00 2001 From: Patrick Ammann Date: Mon, 30 May 2016 11:01:07 +0200 Subject: [PATCH] #395 fix Former-commit-id: 9de97d7ab9a146c4d54d2f972942ec9d3c848da3 Former-commit-id: 9427516f117b7104e65de06196e4ab7e2a98be4e Former-commit-id: 760cf311cd50c0c83b38a3226e1869b184d248f2 --- .../Formats/Bmp/BmpEncoderCore.cs | 53 ++++++++-------- .../Formats/Gif/GifEncoderCore.cs | 63 +++++++++---------- 2 files changed, 57 insertions(+), 59 deletions(-) diff --git a/src/ImageProcessorCore/Formats/Bmp/BmpEncoderCore.cs b/src/ImageProcessorCore/Formats/Bmp/BmpEncoderCore.cs index 0c1e740af2..890861d1e8 100644 --- a/src/ImageProcessorCore/Formats/Bmp/BmpEncoderCore.cs +++ b/src/ImageProcessorCore/Formats/Bmp/BmpEncoderCore.cs @@ -42,35 +42,34 @@ namespace ImageProcessorCore.Formats rowWidth += 4 - amount; } - using (EndianBinaryWriter writer = new EndianBinaryWriter(EndianBitConverter.Little, stream)) - { - int bpp = (int)this.bmpBitsPerPixel; + EndianBinaryWriter writer = new EndianBinaryWriter(EndianBitConverter.Little, stream); - BmpFileHeader fileHeader = new BmpFileHeader - { - Type = 19778, // BM - Offset = 54, - FileSize = 54 + (image.Height * rowWidth * bpp) - }; + int bpp = (int)this.bmpBitsPerPixel; - BmpInfoHeader infoHeader = new BmpInfoHeader - { - HeaderSize = 40, - Height = image.Height, - Width = image.Width, - BitsPerPixel = (short)(8 * bpp), - Planes = 1, - ImageSize = image.Height * rowWidth * bpp, - ClrUsed = 0, - ClrImportant = 0 - }; - - WriteHeader(writer, fileHeader); - this.WriteInfo(writer, infoHeader); - this.WriteImage(writer, image); - - writer.Flush(); - } + BmpFileHeader fileHeader = new BmpFileHeader + { + Type = 19778, // BM + Offset = 54, + FileSize = 54 + (image.Height * rowWidth * bpp) + }; + + BmpInfoHeader infoHeader = new BmpInfoHeader + { + HeaderSize = 40, + Height = image.Height, + Width = image.Width, + BitsPerPixel = (short)(8 * bpp), + Planes = 1, + ImageSize = image.Height * rowWidth * bpp, + ClrUsed = 0, + ClrImportant = 0 + }; + + WriteHeader(writer, fileHeader); + this.WriteInfo(writer, infoHeader); + this.WriteImage(writer, image); + + writer.Flush(); } /// diff --git a/src/ImageProcessorCore/Formats/Gif/GifEncoderCore.cs b/src/ImageProcessorCore/Formats/Gif/GifEncoderCore.cs index b0892c5f00..55b2ab69ac 100644 --- a/src/ImageProcessorCore/Formats/Gif/GifEncoderCore.cs +++ b/src/ImageProcessorCore/Formats/Gif/GifEncoderCore.cs @@ -56,47 +56,46 @@ namespace ImageProcessorCore.Formats this.Quantizer = new OctreeQuantizer { Threshold = this.Threshold }; } - using (EndianBinaryWriter writer = new EndianBinaryWriter(EndianBitConverter.Little, stream)) - { - // Ensure that quality can be set but has a fallback. - int quality = this.Quality > 0 ? this.Quality : imageBase.Quality; - this.Quality = quality > 0 ? quality.Clamp(1, 256) : 256; + EndianBinaryWriter writer = new EndianBinaryWriter(EndianBitConverter.Little, stream); + + // Ensure that quality can be set but has a fallback. + int quality = this.Quality > 0 ? this.Quality : imageBase.Quality; + this.Quality = quality > 0 ? quality.Clamp(1, 256) : 256; - // Get the number of bits. - this.bitDepth = ImageMaths.GetBitsNeededForColorDepth(this.Quality); + // Get the number of bits. + this.bitDepth = ImageMaths.GetBitsNeededForColorDepth(this.Quality); - // Quantize the image returning a palette. - QuantizedImage quantized = this.Quantizer.Quantize(image, this.Quality); + // Quantize the image returning a palette. + QuantizedImage quantized = this.Quantizer.Quantize(image, this.Quality); - // Write the header. - this.WriteHeader(writer); + // Write the header. + this.WriteHeader(writer); - // Write the LSD. We'll use local color tables for now. - this.WriteLogicalScreenDescriptor(image, writer, quantized.TransparentIndex); + // Write the LSD. We'll use local color tables for now. + this.WriteLogicalScreenDescriptor(image, writer, quantized.TransparentIndex); - // Write the first frame. - this.WriteGraphicalControlExtension(imageBase, writer, quantized.TransparentIndex); - this.WriteImageDescriptor(image, writer); - this.WriteColorTable(quantized, writer); - this.WriteImageData(quantized, writer); + // Write the first frame. + this.WriteGraphicalControlExtension(imageBase, writer, quantized.TransparentIndex); + this.WriteImageDescriptor(image, writer); + this.WriteColorTable(quantized, writer); + this.WriteImageData(quantized, writer); - // Write additional frames. - if (image.Frames.Any()) + // Write additional frames. + if (image.Frames.Any()) + { + this.WriteApplicationExtension(writer, image.RepeatCount, image.Frames.Count); + foreach (ImageFrame frame in image.Frames) { - this.WriteApplicationExtension(writer, image.RepeatCount, image.Frames.Count); - foreach (ImageFrame frame in image.Frames) - { - QuantizedImage quantizedFrame = this.Quantizer.Quantize(frame, this.Quality); - this.WriteGraphicalControlExtension(frame, writer, quantizedFrame.TransparentIndex); - this.WriteImageDescriptor(frame, writer); - this.WriteColorTable(quantizedFrame, writer); - this.WriteImageData(quantizedFrame, writer); - } + QuantizedImage quantizedFrame = this.Quantizer.Quantize(frame, this.Quality); + this.WriteGraphicalControlExtension(frame, writer, quantizedFrame.TransparentIndex); + this.WriteImageDescriptor(frame, writer); + this.WriteColorTable(quantizedFrame, writer); + this.WriteImageData(quantizedFrame, writer); } - - // TODO: Write Comments extension etc - writer.Write(GifConstants.EndIntroducer); } + + // TODO: Write Comments extension etc + writer.Write(GifConstants.EndIntroducer); } ///