diff --git a/ImageProcessorCore.sln b/ImageProcessorCore.sln index 3ea906ace8..f988443622 100644 --- a/ImageProcessorCore.sln +++ b/ImageProcessorCore.sln @@ -15,6 +15,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "SolutionItems", "SolutionIt global.json = global.json build\package.json = build\package.json README.md = README.md + Settings.StyleCop = Settings.StyleCop EndProjectSection EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Source", "Source", "{815C0625-CD3D-440F-9F80-2D83856AB7AE}" diff --git a/Settings.StyleCop b/Settings.StyleCop index 4f6343fc65..caf5179c80 100644 --- a/Settings.StyleCop +++ b/Settings.StyleCop @@ -1,4 +1,18 @@ + + + enum + exif + uint + lossy + octree + png + quantizer + unzig + cb + cr + + diff --git a/src/ImageProcessorCore/Formats/Gif/GifEncoder.cs b/src/ImageProcessorCore/Formats/Gif/GifEncoder.cs index d53cf21083..4b36c116f7 100644 --- a/src/ImageProcessorCore/Formats/Gif/GifEncoder.cs +++ b/src/ImageProcessorCore/Formats/Gif/GifEncoder.cs @@ -27,7 +27,7 @@ namespace ImageProcessorCore.Formats public byte Threshold { get; set; } = 128; /// - /// The quantizer for reducing the color count. + /// Gets or sets the quantizer for reducing the color count. /// public IQuantizer Quantizer { get; set; } diff --git a/src/ImageProcessorCore/Formats/Gif/GifEncoderCore.cs b/src/ImageProcessorCore/Formats/Gif/GifEncoderCore.cs index a9b39f4b2b..507cc45278 100644 --- a/src/ImageProcessorCore/Formats/Gif/GifEncoderCore.cs +++ b/src/ImageProcessorCore/Formats/Gif/GifEncoderCore.cs @@ -35,7 +35,7 @@ namespace ImageProcessorCore.Formats public byte Threshold { get; set; } = 128; /// - /// The quantizer for reducing the color count. + /// Gets or sets the quantizer for reducing the color count. /// public IQuantizer Quantizer { get; set; } @@ -92,7 +92,7 @@ namespace ImageProcessorCore.Formats foreach (ImageFrame frame in image.Frames) { QuantizedImage quantizedFrame = ((IQuantizer)this.Quantizer).Quantize(frame, this.Quality); - + this.WriteGraphicalControlExtension(frame, writer, GetTransparentIndex(quantizedFrame)); this.WriteImageDescriptor(frame, writer); this.WriteColorTable(quantizedFrame, writer); @@ -151,7 +151,7 @@ namespace ImageProcessorCore.Formats /// The packed format. uint, long, float. /// The image to encode. /// The writer to write to the stream with. - /// The transparency index to set the default backgound index to. + /// The transparency index to set the default background index to. private void WriteLogicalScreenDescriptor(Image image, EndianBinaryWriter writer, int tranparencyIndex) where TColor : IPackedVector where TPacked : struct @@ -175,7 +175,8 @@ namespace ImageProcessorCore.Formats field.SetBits(5, 3, descriptor.GlobalColorTableSize); // 6-8 : GCT size. 2^(N+1) // Reduce the number of writes - byte[] arr = { + byte[] arr = + { field.Byte, descriptor.BackgroundColorIndex, // Background Color Index descriptor.PixelAspectRatio // Pixel aspect ratio. Assume 1:1 @@ -185,11 +186,11 @@ namespace ImageProcessorCore.Formats } /// - /// Writes the application exstension to the stream. + /// Writes the application extension to the stream. /// /// The writer to write to the stream with. /// The animated image repeat count. - /// Th number of image frames. + /// The number of image frames. private void WriteApplicationExtension(EndianBinaryWriter writer, ushort repeatCount, int frames) { // Application Extension Header @@ -244,7 +245,8 @@ namespace ImageProcessorCore.Formats }; // Reduce the number of writes. - byte[] intro = { + byte[] intro = + { GifConstants.ExtensionIntroducer, GifConstants.GraphicControlLabel, 4 // Size @@ -311,9 +313,11 @@ namespace ImageProcessorCore.Formats int colorTableLength = (int)Math.Pow(2, this.bitDepth) * 3; byte[] colorTable = new byte[colorTableLength]; - Parallel.For(0, pixelCount, + Parallel.For( + 0, + pixelCount, i => - { + { int offset = i * 3; Color color = new Color(palette[i].ToVector4()); diff --git a/src/ImageProcessorCore/Formats/Gif/LzwEncoder.cs b/src/ImageProcessorCore/Formats/Gif/LzwEncoder.cs index a9681d2c56..b85d4f698b 100644 --- a/src/ImageProcessorCore/Formats/Gif/LzwEncoder.cs +++ b/src/ImageProcessorCore/Formats/Gif/LzwEncoder.cs @@ -15,15 +15,14 @@ namespace ImageProcessorCore.Formats /// Adapted from Jef Poskanzer's Java port by way of J. M. G. Elliott. K Weiner 12/00 /// /// GIFCOMPR.C - GIF Image compression routines - /// + /// + /// /// Lempel-Ziv compression based on 'compress'. GIF modifications by /// David Rowley (mgardi@watdcsu.waterloo.edu) /// - /// /// GIF Image compression - modified 'compress' - /// + /// /// Based on: compress.c - File compression ala IEEE Computer, June 1984. - /// /// By Authors: Spencer W. Thomas (decvax!harpo!utah-cs!utah-gr!thomas) /// Jim McKie (decvax!mcvax!jim) /// Steve Davies (decvax!vax135!petsd!peora!srd) @@ -44,7 +43,25 @@ namespace ImageProcessorCore.Formats private readonly int initialCodeSize; - private int curPixel; + private readonly int[] hashTable = new int[HashSize]; + + private readonly int[] codeTable = new int[HashSize]; + + private readonly int[] masks = + { + 0x0000, 0x0001, 0x0003, 0x0007, 0x000F, 0x001F, 0x003F, 0x007F, 0x00FF, + 0x01FF, 0x03FF, 0x07FF, 0x0FFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF + }; + + /// + /// Define the storage for the packet accumulator. + /// + private readonly byte[] accumulators = new byte[256]; + + /// + /// The current pixel + /// + private int currentPixel; /// /// Number of bits/code @@ -56,13 +73,15 @@ namespace ImageProcessorCore.Formats /// private int maxbits = Bits; - private int maxcode; // maximum code, given bitCount - - private int maxmaxcode = 1 << Bits; // should NEVER generate this code - - private readonly int[] hashTable = new int[HashSize]; + /// + /// maximum code, given bitCount + /// + private int maxcode; - private readonly int[] codeTable = new int[HashSize]; + /// + /// should NEVER generate this code + /// + private int maxmaxcode = 1 << Bits; /// /// For dynamic table sizing @@ -91,7 +110,6 @@ namespace ImageProcessorCore.Formats // for the decompressor. Late addition: construct the table according to // file size for noticeable speed improvement on small files. Please direct // questions about this implementation to ames!jaw. - private int globalInitialBits; private int clearCode; @@ -112,27 +130,15 @@ namespace ImageProcessorCore.Formats // Maintain a BITS character long buffer (so that 8 codes will // fit in it exactly). Use the VAX insv instruction to insert each // code in turn. When the buffer fills up empty it and start over. - private int currentAccumulator; private int currentBits; - private readonly int[] masks = - { - 0x0000, 0x0001, 0x0003, 0x0007, 0x000F, 0x001F, 0x003F, 0x007F, 0x00FF, - 0x01FF, 0x03FF, 0x07FF, 0x0FFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF - }; - /// /// Number of characters so far in this 'packet' /// private int accumulatorCount; - /// - /// Define the storage for the packet accumulator. - /// - private readonly byte[] accumulators = new byte[256]; - /// /// Initializes a new instance of the class. /// @@ -153,7 +159,7 @@ namespace ImageProcessorCore.Formats // Write "initial code size" byte stream.WriteByte((byte)this.initialCodeSize); - this.curPixel = 0; + this.currentPixel = 0; // Compress and write the pixel data this.Compress(this.initialCodeSize + 1, stream); @@ -215,7 +221,7 @@ namespace ImageProcessorCore.Formats /// /// Compress the packets to the stream. /// - /// The inital bits. + /// The initial bits. /// The stream to write to. private void Compress(int intialBits, Stream stream) { @@ -242,7 +248,11 @@ namespace ImageProcessorCore.Formats ent = this.NextPixel(); hshift = 0; - for (fcode = this.hsize; fcode < 65536; fcode *= 2) { ++hshift; } + for (fcode = this.hsize; fcode < 65536; fcode *= 2) + { + ++hshift; + } + hshift = 8 - hshift; // set hash code range bound hsizeReg = this.hsize; @@ -263,13 +273,20 @@ namespace ImageProcessorCore.Formats } // Non-empty slot - if (this.hashTable[i] >= 0) + if (this.hashTable[i] >= 0) { int disp = hsizeReg - i; - if (i == 0) disp = 1; + if (i == 0) + { + disp = 1; + } + do { - if ((i -= disp) < 0) { i += hsizeReg; } + if ((i -= disp) < 0) + { + i += hsizeReg; + } if (this.hashTable[i] == fcode) { @@ -279,7 +296,10 @@ namespace ImageProcessorCore.Formats } while (this.hashTable[i] >= 0); - if (this.hashTable[i] == fcode) { continue; } + if (this.hashTable[i] == fcode) + { + continue; + } } this.Output(ent, stream); @@ -289,7 +309,10 @@ namespace ImageProcessorCore.Formats this.codeTable[i] = this.freeEntry++; // code -> hashtable this.hashTable[i] = fcode; } - else this.ClearBlock(stream); + else + { + this.ClearBlock(stream); + } } // Put out the final code. @@ -298,13 +321,16 @@ namespace ImageProcessorCore.Formats this.Output(this.eofCode, stream); } - // Flush the packet to disk, and reset the accumulator - private void FlushPacket(Stream outs) + /// + /// Flush the packet to disk, and reset the accumulator. + /// + /// The output stream. + private void FlushPacket(Stream outStream) { if (this.accumulatorCount > 0) { - outs.WriteByte((byte)this.accumulatorCount); - outs.Write(this.accumulators, 0, this.accumulatorCount); + outStream.WriteByte((byte)this.accumulatorCount); + outStream.Write(this.accumulators, 0, this.accumulatorCount); this.accumulatorCount = 0; } } @@ -317,16 +343,18 @@ namespace ImageProcessorCore.Formats /// private int NextPixel() { - if (this.curPixel == this.pixelArray.Length) + if (this.currentPixel == this.pixelArray.Length) { return Eof; } - if (this.curPixel == this.pixelArray.Length) + if (this.currentPixel == this.pixelArray.Length) + { return Eof; + } - this.curPixel++; - return this.pixelArray[this.curPixel - 1] & 0xff; + this.currentPixel++; + return this.pixelArray[this.currentPixel - 1] & 0xff; } /// @@ -338,8 +366,14 @@ namespace ImageProcessorCore.Formats { this.currentAccumulator &= this.masks[this.currentBits]; - if (this.currentBits > 0) this.currentAccumulator |= (code << this.currentBits); - else this.currentAccumulator = code; + if (this.currentBits > 0) + { + this.currentAccumulator |= code << this.currentBits; + } + else + { + this.currentAccumulator = code; + } this.currentBits += this.bitCount; diff --git a/src/ImageProcessorCore/Formats/Gif/PackedField.cs b/src/ImageProcessorCore/Formats/Gif/PackedField.cs index 0141d36c6a..e606a23066 100644 --- a/src/ImageProcessorCore/Formats/Gif/PackedField.cs +++ b/src/ImageProcessorCore/Formats/Gif/PackedField.cs @@ -38,9 +38,11 @@ namespace ImageProcessorCore.Formats { bitValue = 0; } + returnValue |= bitValue; bitShift--; } + return Convert.ToByte(returnValue & 0xFF); } } @@ -77,6 +79,7 @@ namespace ImageProcessorCore.Formats + index; throw new ArgumentOutOfRangeException(nameof(index), message); } + Bits[index] = valueToSet; } @@ -105,10 +108,10 @@ namespace ImageProcessorCore.Formats int bitShift = length - 1; for (int i = startIndex; i < startIndex + length; i++) { - int bitValueIfSet = (1 << bitShift); - int bitValue = (valueToSet & bitValueIfSet); - int bitIsSet = (bitValue >> bitShift); - Bits[i] = (bitIsSet == 1); + int bitValueIfSet = 1 << bitShift; + int bitValue = valueToSet & bitValueIfSet; + int bitIsSet = bitValue >> bitShift; + Bits[i] = bitIsSet == 1; bitShift--; } } @@ -127,6 +130,7 @@ namespace ImageProcessorCore.Formats string message = $"Index must be between 0 and 7. Supplied index: {index}"; throw new ArgumentOutOfRangeException(nameof(index), message); } + return Bits[index]; } @@ -162,6 +166,7 @@ namespace ImageProcessorCore.Formats returnValue += bitValue; bitShift--; } + return returnValue; } diff --git a/src/ImageProcessorCore/Formats/Jpg/JpegDecoderCore.cs.REMOVED.git-id b/src/ImageProcessorCore/Formats/Jpg/JpegDecoderCore.cs.REMOVED.git-id index 1329746e8b..10a9e18c2d 100644 --- a/src/ImageProcessorCore/Formats/Jpg/JpegDecoderCore.cs.REMOVED.git-id +++ b/src/ImageProcessorCore/Formats/Jpg/JpegDecoderCore.cs.REMOVED.git-id @@ -1 +1 @@ -d6958d9584977251f1a5b84a877bb4c3a1336135 \ No newline at end of file +4b157cb3b1a798514be3c1b13c1ed4294bf9d6f1 \ No newline at end of file diff --git a/tests/ImageProcessorCore.Tests/FileTestBase.cs b/tests/ImageProcessorCore.Tests/FileTestBase.cs index 84f7532b1c..668ee5caec 100644 --- a/tests/ImageProcessorCore.Tests/FileTestBase.cs +++ b/tests/ImageProcessorCore.Tests/FileTestBase.cs @@ -21,7 +21,7 @@ namespace ImageProcessorCore.Tests { //TestImages.Png.P1, //TestImages.Png.Pd, - //TestImages.Jpg.Floorplan, // Perf: Enable for local testing only + TestImages.Jpg.Floorplan, // Perf: Enable for local testing only TestImages.Jpg.Calliphora, //TestImages.Jpg.Cmyk, // Perf: Enable for local testing only //TestImages.Jpg.Turtle,