From a9cd40a0bfa1f20a982cdbc50b4009dab41d1487 Mon Sep 17 00:00:00 2001 From: Jarek Sacha Date: Mon, 3 May 2021 20:20:05 -0400 Subject: [PATCH] Check for images of odd sizes #13 Better error message when image has an odd size --- .../main/scala/ij_plugins/debayer2sx/DDFAPD.scala | 4 ++++ .../scala/ij_plugins/debayer2sx/DDFAPDTest.scala | 11 +++++++++++ .../ij_plugins/debayer2sx/DeBayer2Test.scala | 15 ++++++++++++++- 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/ijp-debayer2sx-core/src/main/scala/ij_plugins/debayer2sx/DDFAPD.scala b/ijp-debayer2sx-core/src/main/scala/ij_plugins/debayer2sx/DDFAPD.scala index 663f7a3..288b7e9 100644 --- a/ijp-debayer2sx-core/src/main/scala/ij_plugins/debayer2sx/DDFAPD.scala +++ b/ijp-debayer2sx-core/src/main/scala/ij_plugins/debayer2sx/DDFAPD.scala @@ -70,6 +70,10 @@ object DDFAPD { * @return reconstructed image as stack of bands */ def debayerGR(bay: FloatProcessor, bpp: Int, doRefine: Boolean): ImageStack = { + require(bay.getWidth > 0, s"Image width must be greater than 0, got ${bay.getWidth}.") + require(bay.getWidth % 2 == 0, s"Image width must be even (multiple of 2), got ${bay.getWidth}.") + require(bay.getHeight > 0, s"Image height must be greater than 0, got ${bay.getHeight}.") + require(bay.getHeight % 2 == 0, s"Image height must be even (multiple of 2), got ${bay.getHeight}.") // // Horizontal and vertical interpolation of the green channel diff --git a/ijp-debayer2sx-core/src/test/scala/ij_plugins/debayer2sx/DDFAPDTest.scala b/ijp-debayer2sx-core/src/test/scala/ij_plugins/debayer2sx/DDFAPDTest.scala index df92ac8..dddcbf5 100644 --- a/ijp-debayer2sx-core/src/test/scala/ij_plugins/debayer2sx/DDFAPDTest.scala +++ b/ijp-debayer2sx-core/src/test/scala/ij_plugins/debayer2sx/DDFAPDTest.scala @@ -106,6 +106,17 @@ class DDFAPDTest extends AnyFlatSpec with BeforeAndAfter with Matchers { Utils.compare(cp, cpRef) } + it should "should throw IllegalArgumentException for images with odd sizes" in { + val bp = new ByteProcessor(347, 440) + assertThrows[IllegalArgumentException] { + DDFAPD.debayerGR(bp, doRefine = true) + } + + assertThrows[IllegalArgumentException] { + DDFAPD.debayerGR(bp.convertToFloatProcessor(), 8, doRefine = true) + } + } + before { val pixels = Array[Short]( diff --git a/ijp-debayer2sx-core/src/test/scala/ij_plugins/debayer2sx/DeBayer2Test.scala b/ijp-debayer2sx-core/src/test/scala/ij_plugins/debayer2sx/DeBayer2Test.scala index 2d0ac16..92ff8a8 100644 --- a/ijp-debayer2sx-core/src/test/scala/ij_plugins/debayer2sx/DeBayer2Test.scala +++ b/ijp-debayer2sx-core/src/test/scala/ij_plugins/debayer2sx/DeBayer2Test.scala @@ -22,7 +22,7 @@ package ij_plugins.debayer2sx -import ij.process.ColorProcessor +import ij.process.{ByteProcessor, ColorProcessor} import ij_plugins.debayer2sx.DeBayer2Config.{Demosaicing, MosaicOrder} import ij_plugins.debayer2sx.Utils._ import org.scalatest.BeforeAndAfter @@ -76,4 +76,17 @@ class DeBayer2Test extends AnyFlatSpec with Matchers with BeforeAndAfter { val cp = DeBayer2.stackToColorProcessor(dstStack, bpp) assert(meanDistance(cp, refCP) <= meanDistanceTolerance * 3) } + + it should "should throw IllegalArgumentException for images with odd sizes" in { + val bp = new ByteProcessor(347, 440) + for (o <- MosaicOrder.values) + assertThrows[IllegalArgumentException] { + DeBayer2.process(bp, DeBayer2Config(mosaicOrder = o, demosaicing = Demosaicing.DDFAPD)) + } + + for (o <- MosaicOrder.values) + assertThrows[IllegalArgumentException] { + DeBayer2.process(bp, DeBayer2Config(o, Demosaicing.DDFAPDRefined)) + } + } }