From 841e37c19ca6da5d0bd1c8dc3dbabefdcc79422b Mon Sep 17 00:00:00 2001 From: James South Date: Wed, 25 Jun 2014 15:11:10 +0100 Subject: [PATCH] FxCop tidy up Former-commit-id: 8984760e2f840d0f73b8e7b87d4af9c672e595a5 --- .../Common/Exceptions/ImageFormatException.cs | 1 + .../Exceptions/ImageProcessingException.cs | 1 + src/ImageProcessor/ImageProcessor.csproj | 1 + .../Imaging/Formats/JpegFormat.cs | 11 +- .../Imaging/Formats/NativeMethods.cs | 107 ++++++++++++++++++ .../Imaging/Formats/TiffFormat.cs | 11 +- .../Imaging/Formats/WebPFormat.cs | 103 +---------------- src/ImageProcessor/Settings.StyleCop | 1 + 8 files changed, 118 insertions(+), 118 deletions(-) create mode 100644 src/ImageProcessor/Imaging/Formats/NativeMethods.cs diff --git a/src/ImageProcessor/Common/Exceptions/ImageFormatException.cs b/src/ImageProcessor/Common/Exceptions/ImageFormatException.cs index 992bd9737..1d4169a88 100644 --- a/src/ImageProcessor/Common/Exceptions/ImageFormatException.cs +++ b/src/ImageProcessor/Common/Exceptions/ImageFormatException.cs @@ -15,6 +15,7 @@ namespace ImageProcessor.Common.Exceptions /// /// The exception that is thrown when loading the supported image format types has failed. /// + [Serializable] public sealed class ImageFormatException : Exception { /// diff --git a/src/ImageProcessor/Common/Exceptions/ImageProcessingException.cs b/src/ImageProcessor/Common/Exceptions/ImageProcessingException.cs index 44579604a..02b6be4f2 100644 --- a/src/ImageProcessor/Common/Exceptions/ImageProcessingException.cs +++ b/src/ImageProcessor/Common/Exceptions/ImageProcessingException.cs @@ -15,6 +15,7 @@ namespace ImageProcessor.Common.Exceptions /// /// The exception that is thrown when processing an image has failed. /// + [Serializable] public sealed class ImageProcessingException : Exception { /// diff --git a/src/ImageProcessor/ImageProcessor.csproj b/src/ImageProcessor/ImageProcessor.csproj index cab9513c1..68702ed09 100644 --- a/src/ImageProcessor/ImageProcessor.csproj +++ b/src/ImageProcessor/ImageProcessor.csproj @@ -76,6 +76,7 @@ + diff --git a/src/ImageProcessor/Imaging/Formats/JpegFormat.cs b/src/ImageProcessor/Imaging/Formats/JpegFormat.cs index 0111ee1d6..ae5fd05c3 100644 --- a/src/ImageProcessor/Imaging/Formats/JpegFormat.cs +++ b/src/ImageProcessor/Imaging/Formats/JpegFormat.cs @@ -81,16 +81,7 @@ namespace ImageProcessor.Imaging.Formats { foreach (KeyValuePair propertItem in factory.ExifPropertyItems) { - try - { - factory.Image.SetPropertyItem(propertItem.Value); - } - // ReSharper disable once EmptyGeneralCatchClause - catch - { - // Do nothing. The image format does not handle EXIF data. - // TODO: empty catch is fierce code smell. - } + factory.Image.SetPropertyItem(propertItem.Value); } } } diff --git a/src/ImageProcessor/Imaging/Formats/NativeMethods.cs b/src/ImageProcessor/Imaging/Formats/NativeMethods.cs new file mode 100644 index 000000000..eb9062e91 --- /dev/null +++ b/src/ImageProcessor/Imaging/Formats/NativeMethods.cs @@ -0,0 +1,107 @@ +// -------------------------------------------------------------------------------------------------------------------- +// +// Copyright (c) James South. +// Licensed under the Apache License, Version 2.0. +// +// +// Provides access to unmanaged native methods. +// +// -------------------------------------------------------------------------------------------------------------------- + +namespace ImageProcessor.Imaging.Formats +{ + using System; + using System.Runtime.InteropServices; + + /// + /// Provides access to unmanaged native methods. + /// + internal static class NativeMethods + { + #region WebP + /// + /// Validate the WebP image header and retrieve the image height and width. Pointers *width and *height can be passed NULL if deemed irrelevant + /// + /// + /// Pointer to WebP image data + /// + /// + /// This is the size of the memory block pointed to by data containing the image data + /// + /// + /// The width range is limited currently from 1 to 16383 + /// + /// + /// The height range is limited currently from 1 to 16383 + /// + /// + /// 1 if success, otherwise error code returned in the case of (a) formatting error(s). + /// + [DllImport("libwebp.dll", CallingConvention = CallingConvention.Cdecl)] + public static extern int WebPGetInfo(IntPtr data, uint dataSize, out int width, out int height); + + /// + /// Decode WEBP image pointed to by *data and returns BGR samples into a pre-allocated buffer + /// + /// + /// Pointer to WebP image data + /// + /// + /// This is the size of the memory block pointed to by data containing the image data + /// + /// + /// Pointer to decoded WebP image + /// + /// + /// Size of allocated buffer + /// + /// + /// Specifies the distance between scan-lines + /// + /// + /// output_buffer if function succeeds; NULL otherwise + /// + [DllImport("libwebp.dll", CallingConvention = CallingConvention.Cdecl)] + public static extern IntPtr WebPDecodeBGRAInto(IntPtr data, uint dataSize, IntPtr outputBuffer, int outputBufferSize, int outputStride); + + /// + /// Lossy encoding images pointed to by *data in WebP format + /// + /// + /// Pointer to RGB image data + /// + /// + /// The width range is limited currently from 1 to 16383 + /// + /// + /// The height range is limited currently from 1 to 16383 + /// + /// + /// The stride. + /// + /// + /// Ranges from 0 (lower quality) to 100 (highest quality). Controls the loss and quality during compression + /// + /// + /// output_buffer with WebP image + /// + /// + /// Size of WebP Image + /// + [DllImport("libwebp.dll", CallingConvention = CallingConvention.Cdecl)] + public static extern int WebPEncodeBGRA(IntPtr rgb, int width, int height, int stride, float qualityFactor, out IntPtr output); + + /// + /// Frees the unmanaged memory. + /// + /// + /// The pointer. + /// + /// + /// 1 if success, otherwise error code returned in the case of (a) error(s). + /// + [DllImport("libwebp.dll", CallingConvention = CallingConvention.Cdecl)] + public static extern int WebPFree(IntPtr pointer); + #endregion + } +} diff --git a/src/ImageProcessor/Imaging/Formats/TiffFormat.cs b/src/ImageProcessor/Imaging/Formats/TiffFormat.cs index b708abd7d..e761bc4b3 100644 --- a/src/ImageProcessor/Imaging/Formats/TiffFormat.cs +++ b/src/ImageProcessor/Imaging/Formats/TiffFormat.cs @@ -83,16 +83,7 @@ namespace ImageProcessor.Imaging.Formats { foreach (KeyValuePair propertItem in factory.ExifPropertyItems) { - try - { - factory.Image.SetPropertyItem(propertItem.Value); - } - // ReSharper disable once EmptyGeneralCatchClause - catch - { - // Do nothing. The image format does not handle EXIF data. - // TODO: empty catch is fierce code smell. - } + factory.Image.SetPropertyItem(propertItem.Value); } } } diff --git a/src/ImageProcessor/Imaging/Formats/WebPFormat.cs b/src/ImageProcessor/Imaging/Formats/WebPFormat.cs index d0bff7a19..d8c9bbf33 100644 --- a/src/ImageProcessor/Imaging/Formats/WebPFormat.cs +++ b/src/ImageProcessor/Imaging/Formats/WebPFormat.cs @@ -90,16 +90,7 @@ namespace ImageProcessor.Imaging.Formats { foreach (KeyValuePair propertItem in factory.ExifPropertyItems) { - try - { - factory.Image.SetPropertyItem(propertItem.Value); - } - // ReSharper disable once EmptyGeneralCatchClause - catch - { - // Do nothing. The image format does not handle EXIF data. - // TODO: empty catch is fierce code smell. - } + factory.Image.SetPropertyItem(propertItem.Value); } } } @@ -193,7 +184,7 @@ namespace ImageProcessor.Imaging.Formats int width; int height; - if (WebPGetInfo(ptrData, dataSize, out width, out height) != 1) + if (NativeMethods.WebPGetInfo(ptrData, dataSize, out width, out height) != 1) { throw new ImageFormatException("WebP image header is corrupted."); } @@ -209,7 +200,7 @@ namespace ImageProcessor.Imaging.Formats outputBuffer = Marshal.AllocHGlobal(outputBufferSize); // Uncompress the image - outputBuffer = WebPDecodeBGRAInto(ptrData, dataSize, outputBuffer, outputBufferSize, bitmapData.Stride); + outputBuffer = NativeMethods.WebPDecodeBGRAInto(ptrData, dataSize, outputBuffer, outputBufferSize, bitmapData.Stride); if (bitmapData.Scan0 != outputBuffer) { @@ -262,7 +253,7 @@ namespace ImageProcessor.Imaging.Formats try { // Attempt to lossy encode the image. - int size = WebPEncodeBGRA(bmpData.Scan0, bitmap.Width, bitmap.Height, bmpData.Stride, quality, out unmanagedData); + int size = NativeMethods.WebPEncodeBGRA(bmpData.Scan0, bitmap.Width, bitmap.Height, bmpData.Stride, quality, out unmanagedData); // Copy image compress data to output array webpData = new byte[size]; @@ -279,94 +270,10 @@ namespace ImageProcessor.Imaging.Formats bitmap.UnlockBits(bmpData); // Free memory - WebPFree(unmanagedData); + NativeMethods.WebPFree(unmanagedData); } return encoded; } - - /// - /// Validate the WebP image header and retrieve the image height and width. Pointers *width and *height can be passed NULL if deemed irrelevant - /// - /// - /// Pointer to WebP image data - /// - /// - /// This is the size of the memory block pointed to by data containing the image data - /// - /// - /// The width range is limited currently from 1 to 16383 - /// - /// - /// The height range is limited currently from 1 to 16383 - /// - /// - /// 1 if success, otherwise error code returned in the case of (a) formatting error(s). - /// - [DllImport("libwebp.dll", CallingConvention = CallingConvention.Cdecl)] - private static extern int WebPGetInfo(IntPtr data, uint dataSize, out int width, out int height); - - /// - /// Decode WEBP image pointed to by *data and returns BGR samples into a pre-allocated buffer - /// - /// - /// Pointer to WebP image data - /// - /// - /// This is the size of the memory block pointed to by data containing the image data - /// - /// - /// Pointer to decoded WebP image - /// - /// - /// Size of allocated buffer - /// - /// - /// Specifies the distance between scan-lines - /// - /// - /// output_buffer if function succeeds; NULL otherwise - /// - [DllImport("libwebp.dll", CallingConvention = CallingConvention.Cdecl)] - private static extern IntPtr WebPDecodeBGRAInto(IntPtr data, uint dataSize, IntPtr outputBuffer, int outputBufferSize, int outputStride); - - /// - /// Lossy encoding images pointed to by *data in WebP format - /// - /// - /// Pointer to RGB image data - /// - /// - /// The width range is limited currently from 1 to 16383 - /// - /// - /// The height range is limited currently from 1 to 16383 - /// - /// - /// The stride. - /// - /// - /// Ranges from 0 (lower quality) to 100 (highest quality). Controls the loss and quality during compression - /// - /// - /// output_buffer with WebP image - /// - /// - /// Size of WebP Image - /// - [DllImport("libwebp.dll", CallingConvention = CallingConvention.Cdecl)] - private static extern int WebPEncodeBGRA(IntPtr rgb, int width, int height, int stride, float qualityFactor, out IntPtr output); - - /// - /// Frees the unmanaged memory. - /// - /// - /// The pointer. - /// - /// - /// 1 if success, otherwise error code returned in the case of (a) error(s). - /// - [DllImport("libwebp.dll", CallingConvention = CallingConvention.Cdecl)] - private static extern int WebPFree(IntPtr pointer); } } \ No newline at end of file diff --git a/src/ImageProcessor/Settings.StyleCop b/src/ImageProcessor/Settings.StyleCop index d643471e4..a8aa884a1 100644 --- a/src/ImageProcessor/Settings.StyleCop +++ b/src/ImageProcessor/Settings.StyleCop @@ -10,6 +10,7 @@ halftoning lomograph Lomograph + lossy octree png quantizer