From 35be517c4c4e4ea4ccc628913984f065b2a552e4 Mon Sep 17 00:00:00 2001 From: Rubens Fernandes Date: Sat, 2 Jan 2016 21:13:31 +1030 Subject: [PATCH] Adding simple decode for jpeg grayscale colorspaces. Also added test image Former-commit-id: 6ef0bffcb0a08b3876373392f3d67572273cea1b Former-commit-id: db74e4cf38bbb992fc3b88de30f6b7e79cf9ac19 Former-commit-id: c51e5e8718dd52306a6656c4f9d85be76e9ecdb8 --- src/ImageProcessor/Formats/Jpg/JpegDecoder.cs | 42 +++++++++++++++---- .../Processors/ProcessorTestBase.cs | 1 + .../Formats/Jpg/Floorplan.jpeg.REMOVED.git-id | 1 + 3 files changed, 35 insertions(+), 9 deletions(-) create mode 100644 tests/ImageProcessor.Tests/TestImages/Formats/Jpg/Floorplan.jpeg.REMOVED.git-id diff --git a/src/ImageProcessor/Formats/Jpg/JpegDecoder.cs b/src/ImageProcessor/Formats/Jpg/JpegDecoder.cs index 0c5a843aa..2f456dfc0 100644 --- a/src/ImageProcessor/Formats/Jpg/JpegDecoder.cs +++ b/src/ImageProcessor/Formats/Jpg/JpegDecoder.cs @@ -102,15 +102,34 @@ namespace ImageProcessor.Formats 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( - 0, - pixelHeight, - y => + int offset = ((y * pixelWidth) + x) * 4; + + pixels[offset + 0] = sample[0] / 255f; + 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); @@ -121,11 +140,16 @@ namespace ImageProcessor.Formats int offset = ((y * pixelWidth) + x) * 4; pixels[offset + 0] = sample[0] / 255f; - pixels[offset + 1] = sample[1] / 255f; - pixels[offset + 2] = sample[2] / 255f; + pixels[offset + 1] = sample[0] / 255f; + pixels[offset + 2] = sample[0] / 255f; pixels[offset + 3] = 1; } }); + } + else + { + throw new NotSupportedException("JpegDecoder only supports RGB and Grayscale color spaces."); + } image.SetPixels(pixelWidth, pixelHeight, pixels); } diff --git a/tests/ImageProcessor.Tests/Processors/ProcessorTestBase.cs b/tests/ImageProcessor.Tests/Processors/ProcessorTestBase.cs index 3734d79ed..4e1bbc942 100644 --- a/tests/ImageProcessor.Tests/Processors/ProcessorTestBase.cs +++ b/tests/ImageProcessor.Tests/Processors/ProcessorTestBase.cs @@ -19,6 +19,7 @@ namespace ImageProcessor.Tests /// public static readonly List Files = new List { + "TestImages/Formats/Jpg/Floorplan.jpeg", "TestImages/Formats/Jpg/Calliphora.jpg", //"TestImages/Formats/Jpg/gamma_dalai_lama_gray.jpg", //Perf: Enable for local testing only "TestImages/Formats/Bmp/Car.bmp", diff --git a/tests/ImageProcessor.Tests/TestImages/Formats/Jpg/Floorplan.jpeg.REMOVED.git-id b/tests/ImageProcessor.Tests/TestImages/Formats/Jpg/Floorplan.jpeg.REMOVED.git-id new file mode 100644 index 000000000..88d957b21 --- /dev/null +++ b/tests/ImageProcessor.Tests/TestImages/Formats/Jpg/Floorplan.jpeg.REMOVED.git-id @@ -0,0 +1 @@ +5a1eaf806b7f3793d3db41c85bac6366584bce83 \ No newline at end of file