Browse Source

fixes on code review

pull/292/head
Nikita Balabaev 9 years ago
parent
commit
1d81aedfd9
  1. 4
      src/ImageSharp/Formats/Bmp/BmpDecoder.cs
  2. 5
      src/ImageSharp/Formats/Gif/GifDecoder.cs
  3. 4
      src/ImageSharp/Formats/IImageDecoder.cs
  4. 4
      src/ImageSharp/Formats/Jpeg/JpegDecoder.cs
  5. 22
      src/ImageSharp/Formats/PixelTypeInfo.cs
  6. 4
      src/ImageSharp/Formats/Png/PngDecoder.cs
  7. 6
      src/ImageSharp/Image/Image.Decode.cs
  8. 16
      src/ImageSharp/Image/Image.FromStream.cs
  9. 2
      tests/ImageSharp.Tests/Formats/Bmp/BmpDecoderTests.cs
  10. 2
      tests/ImageSharp.Tests/Formats/Gif/GifDecoderTests.cs
  11. 2
      tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs
  12. 2
      tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs
  13. 2
      tests/ImageSharp.Tests/TestFormat.cs

4
src/ImageSharp/Formats/Bmp/BmpDecoder.cs

@ -39,14 +39,14 @@ namespace ImageSharp.Formats
}
/// <inheritdoc/>
public int DetectPixelSize(Configuration configuration, Stream stream)
public PixelTypeInfo DetectPixelType(Configuration configuration, Stream stream)
{
Guard.NotNull(stream, "stream");
byte[] buffer = new byte[2];
stream.Skip(28);
stream.Read(buffer, 0, 2);
return BitConverter.ToInt16(buffer, 0);
return new PixelTypeInfo(BitConverter.ToInt16(buffer, 0));
}
}
}

5
src/ImageSharp/Formats/Gif/GifDecoder.cs

@ -35,14 +35,15 @@ namespace ImageSharp.Formats
}
/// <inheritdoc/>
public int DetectPixelSize(Configuration configuration, Stream stream)
public PixelTypeInfo DetectPixelType(Configuration configuration, Stream stream)
{
Guard.NotNull(stream, "stream");
byte[] buffer = new byte[1];
stream.Skip(10); // Skip the identifier and size
stream.Read(buffer, 0, 1); // Skip the identifier and size
return (buffer[0] & 0x07) + 1; // The lowest 3 bits represent the bit depth minus 1
int bitsPerPixel = (buffer[0] & 0x07) + 1; // The lowest 3 bits represent the bit depth minus 1
return new PixelTypeInfo(bitsPerPixel);
}
}
}

4
src/ImageSharp/Formats/IImageDecoder.cs

@ -31,7 +31,7 @@ namespace ImageSharp.Formats
/// </summary>
/// <param name="configuration">The configuration for the image.</param>
/// <param name="stream">The <see cref="Stream"/> containing image data.</param>
/// <returns>The color depth, in number of bits per pixel</returns>
int DetectPixelSize(Configuration configuration, Stream stream);
/// <returns>The <see cref="PixelTypeInfo"/> object</returns>
PixelTypeInfo DetectPixelType(Configuration configuration, Stream stream);
}
}

4
src/ImageSharp/Formats/Jpeg/JpegDecoder.cs

@ -34,13 +34,13 @@ namespace ImageSharp.Formats
}
/// <inheritdoc/>
public int DetectPixelSize(Configuration configuration, Stream stream)
public PixelTypeInfo DetectPixelType(Configuration configuration, Stream stream)
{
Guard.NotNull(stream, "stream");
using (JpegDecoderCore decoder = new JpegDecoderCore(configuration, this))
{
return decoder.DetectPixelSize(stream);
return new PixelTypeInfo(decoder.DetectPixelSize(stream));
}
}
}

22
src/ImageSharp/Formats/PixelTypeInfo.cs

@ -0,0 +1,22 @@
namespace ImageSharp.Formats
{
/// <summary>
/// Stores information about pixel
/// </summary>
public class PixelTypeInfo
{
/// <summary>
/// Initializes a new instance of the <see cref="PixelTypeInfo"/> class.
/// </summary>
/// <param name="bitsPerPixel">color depth, in number of bits per pixel</param>
internal PixelTypeInfo(int bitsPerPixel)
{
this.BitsPerPixel = bitsPerPixel;
}
/// <summary>
/// Gets color depth, in number of bits per pixel
/// </summary>
public int BitsPerPixel { get; }
}
}

4
src/ImageSharp/Formats/Png/PngDecoder.cs

@ -63,10 +63,10 @@ namespace ImageSharp.Formats
/// <param name="configuration">The configuration for the image.</param>
/// <param name="stream">The <see cref="Stream"/> containing image data.</param>
/// <returns>The color depth, in number of bits per pixel</returns>
public int DetectPixelSize(Configuration configuration, Stream stream)
public PixelTypeInfo DetectPixelType(Configuration configuration, Stream stream)
{
var decoder = new PngDecoderCore(configuration, this);
return decoder.DetectPixelSize(stream);
return new PixelTypeInfo(decoder.DetectPixelSize(stream));
}
}
}

6
src/ImageSharp/Image/Image.Decode.cs

@ -88,12 +88,12 @@ namespace ImageSharp
/// <param name="stream">The stream.</param>
/// <param name="config">the configuration.</param>
/// <returns>
/// The color depth, in number of bits per pixel or null if suitable decoder not found.
/// The <see cref="PixelTypeInfo"/> or null if suitable decoder not found.
/// </returns>
private static int? InternalDetectPixelSize(Stream stream, Configuration config)
private static PixelTypeInfo InternalDetectPixelType(Stream stream, Configuration config)
{
IImageDecoder decoder = DiscoverDecoder(stream, config, out IImageFormat _);
return decoder?.DetectPixelSize(config, stream);
return decoder?.DetectPixelType(config, stream);
}
}
}

16
src/ImageSharp/Image/Image.FromStream.cs

@ -46,10 +46,12 @@ namespace ImageSharp
/// <exception cref="NotSupportedException">
/// Thrown if the stream is not readable nor seekable.
/// </exception>
/// <returns>The color depth, in number of bits per pixel or null if suitable decoder not found</returns>
public static int? DetectPixelSize(Stream stream)
/// <returns>
/// The <see cref="PixelTypeInfo"/> or null if suitable decoder not found.
/// </returns>
public static PixelTypeInfo DetectPixelType(Stream stream)
{
return DetectPixelSize(null, stream);
return DetectPixelType(null, stream);
}
/// <summary>
@ -60,10 +62,12 @@ namespace ImageSharp
/// <exception cref="NotSupportedException">
/// Thrown if the stream is not readable nor seekable.
/// </exception>
/// <returns>The color depth, in number of bits per pixel or null if suitable decoder not found</returns>
public static int? DetectPixelSize(Configuration config, Stream stream)
/// <returns>
/// The <see cref="PixelTypeInfo"/> or null if suitable decoder not found.
/// </returns>
public static PixelTypeInfo DetectPixelType(Configuration config, Stream stream)
{
return WithSeekableStream(stream, s => InternalDetectPixelSize(s, config ?? Configuration.Default));
return WithSeekableStream(stream, s => InternalDetectPixelType(s, config ?? Configuration.Default));
}
/// <summary>

2
tests/ImageSharp.Tests/Formats/Bmp/BmpDecoderTests.cs

@ -22,7 +22,7 @@ namespace ImageSharp.Tests
TestFile testFile = TestFile.Create(imagePath);
using (var stream = new MemoryStream(testFile.Bytes, false))
{
Assert.Equal(expectedPixelSize, Image.DetectPixelSize(stream));
Assert.Equal(expectedPixelSize, Image.DetectPixelType(stream)?.BitsPerPixel);
}
}
}

2
tests/ImageSharp.Tests/Formats/Gif/GifDecoderTests.cs

@ -92,7 +92,7 @@ namespace ImageSharp.Tests
TestFile testFile = TestFile.Create(imagePath);
using (var stream = new MemoryStream(testFile.Bytes, false))
{
Assert.Equal(expectedPixelSize, Image.DetectPixelSize(stream));
Assert.Equal(expectedPixelSize, Image.DetectPixelType(stream)?.BitsPerPixel);
}
}
}

2
tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs

@ -165,7 +165,7 @@ namespace ImageSharp.Tests
TestFile testFile = TestFile.Create(imagePath);
using (var stream = new MemoryStream(testFile.Bytes, false))
{
Assert.Equal(expectedPixelSize, Image.DetectPixelSize(stream));
Assert.Equal(expectedPixelSize, Image.DetectPixelType(stream)?.BitsPerPixel);
}
}
}

2
tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs

@ -97,7 +97,7 @@ namespace ImageSharp.Tests
TestFile testFile = TestFile.Create(imagePath);
using (var stream = new MemoryStream(testFile.Bytes, false))
{
Assert.Equal(expectedPixelSize, Image.DetectPixelSize(stream));
Assert.Equal(expectedPixelSize, Image.DetectPixelType(stream)?.BitsPerPixel);
}
}

2
tests/ImageSharp.Tests/TestFormat.cs

@ -204,7 +204,7 @@ namespace ImageSharp.Tests
return this.testFormat.Sample<TPixel>();
}
public int DetectPixelSize(Configuration configuration, Stream stream)
public PixelTypeInfo DetectPixelType(Configuration configuration, Stream stream)
{
throw new NotImplementedException();
}

Loading…
Cancel
Save