Browse Source

Adding simple decode for jpeg grayscale colorspaces. Also added test image

Former-commit-id: c61158051fdd8deddde279ed03c79dcbd996a355
Former-commit-id: c85e9c5d0b383b60ed1fcfb4c2657d8f19890b6a
Former-commit-id: 5d34af5d81eb5fae48a1fe5db466b7ef6abc74c9
pull/17/head
Rubens Fernandes 10 years ago
parent
commit
c8d46df4d7
  1. 42
      src/ImageProcessor/Formats/Jpg/JpegDecoder.cs
  2. 1
      tests/ImageProcessor.Tests/Processors/ProcessorTestBase.cs
  3. 1
      tests/ImageProcessor.Tests/TestImages/Formats/Jpg/Floorplan.jpeg.REMOVED.git-id

42
src/ImageProcessor/Formats/Jpg/JpegDecoder.cs

@ -102,15 +102,34 @@ namespace ImageProcessor.Formats
float[] pixels = new float[pixelWidth * pixelHeight * 4]; float[] pixels = new float[pixelWidth * pixelHeight * 4];
if (!(jpg.Colorspace == Colorspace.RGB && jpg.BitsPerComponent == 8)) if (jpg.Colorspace == Colorspace.RGB && jpg.BitsPerComponent == 8)
{ {
throw new NotSupportedException("JpegDecoder only support RGB color space."); Parallel.For(
} 0,
pixelHeight,
y =>
{
SampleRow row = jpg.GetRow(y);
for (int x = 0; x < pixelWidth; x++)
{
Sample sample = row.GetAt(x);
Parallel.For( int offset = ((y * pixelWidth) + x) * 4;
0,
pixelHeight, pixels[offset + 0] = sample[0] / 255f;
y => pixels[offset + 1] = sample[1] / 255f;
pixels[offset + 2] = sample[2] / 255f;
pixels[offset + 3] = 1;
}
});
}
else if (jpg.Colorspace == Colorspace.Grayscale && jpg.BitsPerComponent == 8)
{
Parallel.For(
0,
pixelHeight,
y =>
{ {
SampleRow row = jpg.GetRow(y); SampleRow row = jpg.GetRow(y);
@ -121,11 +140,16 @@ namespace ImageProcessor.Formats
int offset = ((y * pixelWidth) + x) * 4; int offset = ((y * pixelWidth) + x) * 4;
pixels[offset + 0] = sample[0] / 255f; pixels[offset + 0] = sample[0] / 255f;
pixels[offset + 1] = sample[1] / 255f; pixels[offset + 1] = sample[0] / 255f;
pixels[offset + 2] = sample[2] / 255f; pixels[offset + 2] = sample[0] / 255f;
pixels[offset + 3] = 1; pixels[offset + 3] = 1;
} }
}); });
}
else
{
throw new NotSupportedException("JpegDecoder only supports RGB and Grayscale color spaces.");
}
image.SetPixels(pixelWidth, pixelHeight, pixels); image.SetPixels(pixelWidth, pixelHeight, pixels);
} }

1
tests/ImageProcessor.Tests/Processors/ProcessorTestBase.cs

@ -19,6 +19,7 @@ namespace ImageProcessor.Tests
/// </summary> /// </summary>
public static readonly List<string> Files = new List<string> public static readonly List<string> Files = new List<string>
{ {
"TestImages/Formats/Jpg/Floorplan.jpeg",
"TestImages/Formats/Jpg/Calliphora.jpg", "TestImages/Formats/Jpg/Calliphora.jpg",
//"TestImages/Formats/Jpg/gamma_dalai_lama_gray.jpg", //Perf: Enable for local testing only //"TestImages/Formats/Jpg/gamma_dalai_lama_gray.jpg", //Perf: Enable for local testing only
"TestImages/Formats/Bmp/Car.bmp", "TestImages/Formats/Bmp/Car.bmp",

1
tests/ImageProcessor.Tests/TestImages/Formats/Jpg/Floorplan.jpeg.REMOVED.git-id

@ -0,0 +1 @@
5a1eaf806b7f3793d3db41c85bac6366584bce83
Loading…
Cancel
Save