ImgscalrImageManipulator extends AbstractImageManipulator { protected BufferedImage cropAndResize(BufferedImage image, ImageManipulationParameters parameters) { CropMode cropMode = parameters.getCropMode(); boolean omitResize = false; if (cropMode != null) { switch (cropMode) { case RECT: image = crop(image, parameters.getRect()); break; case FOCALPOINT: image = focalPointModifier.apply(image, parameters); omitResize = true; break; } } if (!omitResize) { image = resizeIfRequested(image, parameters); } return image; } ImgscalrImageManipulator(Vertx vertx, MeshOptions options, BootstrapInitializer boot);   ImgscalrImageManipulator(Vertx vertx, ImageManipulatorOptions options, BootstrapInitializer boot); @Override Single<String> handleResize(Binary binary, ImageManipulationParameters parameters); @Override int[] calculateDominantColor(BufferedImage image); @Override Single<Map<String, String>> getMetadata(InputStream ins);  }
@Test public void testPropResize() throws IOException { BufferedImage biH = ImageTestUtil.readImage("testgrid-horizontal-hd_1920x1080.png"); BufferedImage outputImage1 = manipulator.cropAndResize(biH, new ImageManipulationParametersImpl().setWidth(500).setHeight(400).setResizeMode(ResizeMode.PROP)); assertThat(outputImage1).matchesReference("outputImage1-prop-reference.png"); BufferedImage outputImage2 = manipulator.cropAndResize(biH, new ImageManipulationParametersImpl().setWidth(2000).setHeight(500).setResizeMode(ResizeMode.PROP)); assertThat(outputImage2).matchesReference("outputImage2-prop-reference.png"); BufferedImage biV = ImageTestUtil.readImage("testgrid-vertical-hd_1080x1920.png"); BufferedImage outputImage3 = manipulator.cropAndResize(biV, new ImageManipulationParametersImpl().setWidth(500).setHeight(1500).setResizeMode(ResizeMode.PROP)); assertThat(outputImage3).matchesReference("outputImage3-prop-reference.png"); BufferedImage outputImage4 = manipulator.cropAndResize(biV, new ImageManipulationParametersImpl().setWidth(400).setHeight(500).setResizeMode(ResizeMode.PROP)); assertThat(outputImage4).matchesReference("outputImage4-prop-reference.png"); BufferedImage biS = ImageTestUtil.readImage("testgrid-square_1080x1080.png"); BufferedImage outputImage5 = manipulator.cropAndResize(biS, new ImageManipulationParametersImpl().setWidth(500).setHeight(1000).setResizeMode(ResizeMode.PROP)); assertThat(outputImage5).matchesReference("outputImage5-prop-reference.png"); BufferedImage outputImage6 = manipulator.cropAndResize(biS, new ImageManipulationParametersImpl().setWidth(1000).setHeight(500).setResizeMode(ResizeMode.PROP)); assertThat(outputImage6).matchesReference("outputImage6-prop-reference.png"); BufferedImage outputImage7 = manipulator.cropAndResize(biH, new ImageManipulationParametersImpl().setWidth(1920).setHeight(1200).setResizeMode(ResizeMode.PROP)); assertEquals("The image should not have been resized since the resulting images dimensions match the source image dimension.", biH.hashCode(), outputImage7 .hashCode()); BufferedImage outputImage8 = manipulator.cropAndResize(biH, new ImageManipulationParametersImpl().setWidth(2000).setHeight(1080).setResizeMode(ResizeMode.PROP)); assertEquals("The image should not have been resized since the resulting images dimensions match the source image dimension.", biH.hashCode(), outputImage8 .hashCode()); BufferedImage outputImage9 = manipulator.cropAndResize(biH, new ImageManipulationParametersImpl().setWidth(1920).setHeight(1080).setResizeMode(ResizeMode.PROP)); assertEquals("The image should not have been resized since the parameters match the source image dimension.", biH.hashCode(), outputImage9 .hashCode()); BufferedImage outputImage10 = manipulator.cropAndResize(biV, new ImageManipulationParametersImpl().setWidth(1080).setHeight(2000).setResizeMode(ResizeMode.PROP)); assertEquals("The image should not have been resized since the resulting images dimensions match the source image dimension.", biV.hashCode(), outputImage10 .hashCode()); BufferedImage outputImage11 = manipulator.cropAndResize(biV, new ImageManipulationParametersImpl().setWidth(1200).setHeight(1920).setResizeMode(ResizeMode.PROP)); assertEquals("The image should not have been resized since the resulting images dimensions match the source image dimension.", biV.hashCode(), outputImage11 .hashCode()); BufferedImage outputImage12 = manipulator.cropAndResize(biV, new ImageManipulationParametersImpl().setWidth(1080).setHeight(1920).setResizeMode(ResizeMode.PROP)); assertEquals("The image should not have been resized since the parameters match the source image dimension.", biV.hashCode(), outputImage12 .hashCode()); BufferedImage outputImage13 = manipulator.cropAndResize(biS, new ImageManipulationParametersImpl().setWidth(1080).setHeight(1200).setResizeMode(ResizeMode.PROP)); assertEquals("The image should not have been resized since the resulting images dimensions match the source image dimension.", biS.hashCode(), outputImage13 .hashCode()); BufferedImage outputImage14 = manipulator.cropAndResize(biS, new ImageManipulationParametersImpl().setWidth(1200).setHeight(1080).setResizeMode(ResizeMode.PROP)); assertEquals("The image should not have been resized since the resulting images dimensions match the source image dimension.", biS.hashCode(), outputImage14 .hashCode()); BufferedImage outputImage15 = manipulator.cropAndResize(biS, new ImageManipulationParametersImpl().setWidth(1080).setHeight(1080).setResizeMode(ResizeMode.PROP)); assertEquals("The image should not have been resized since the parameters match the source image dimension.", biS.hashCode(), outputImage15 .hashCode()); }
@Test public void testSmartResizeCrop() throws IOException { BufferedImage biH = ImageTestUtil.readImage("testgrid-horizontal-hd_1920x1080.png"); BufferedImage outputImage1 = manipulator.cropAndResize(biH, new ImageManipulationParametersImpl().setWidth(500).setHeight(200).setRect(0, 0, 540, 960).setCropMode(CropMode.RECT) .setResizeMode(ResizeMode.SMART)); assertThat(outputImage1).matchesReference("outputImage1-smart-crop-reference.png"); BufferedImage outputImage2 = manipulator.cropAndResize(biH, new ImageManipulationParametersImpl().setWidth(200).setHeight(500).setRect(0, 0, 540, 960).setCropMode(CropMode.RECT) .setResizeMode(ResizeMode.SMART)); assertThat(outputImage2).matchesReference("outputImage2-smart-crop-reference.png"); BufferedImage outputImage3 = manipulator.cropAndResize(biH, new ImageManipulationParametersImpl().setWidth(500).setHeight(500).setRect(0, 0, 540, 960).setCropMode(CropMode.RECT) .setResizeMode(ResizeMode.SMART)); assertThat(outputImage3).matchesReference("outputImage3-smart-crop-reference.png"); BufferedImage biV = ImageTestUtil.readImage("testgrid-vertical-hd_1080x1920.png"); BufferedImage outputImage4 = manipulator.cropAndResize(biV, new ImageManipulationParametersImpl().setWidth(500).setHeight(200).setRect(0, 0, 960, 540).setCropMode(CropMode.RECT) .setResizeMode(ResizeMode.SMART)); assertThat(outputImage4).matchesReference("outputImage4-smart-crop-reference.png"); BufferedImage outputImage5 = manipulator.cropAndResize(biV, new ImageManipulationParametersImpl().setWidth(200).setHeight(500).setRect(0, 0, 960, 540).setCropMode(CropMode.RECT) .setResizeMode(ResizeMode.SMART)); assertThat(outputImage5).matchesReference("outputImage5-smart-crop-reference.png"); BufferedImage outputImage6 = manipulator.cropAndResize(biV, new ImageManipulationParametersImpl().setWidth(500).setHeight(500).setRect(0, 0, 960, 540).setCropMode(CropMode.RECT) .setResizeMode(ResizeMode.SMART)); assertThat(outputImage6).matchesReference("outputImage6-smart-crop-reference.png"); BufferedImage biS = ImageTestUtil.readImage("testgrid-square_1080x1080.png"); BufferedImage outputImage7 = manipulator.cropAndResize(biS, new ImageManipulationParametersImpl().setWidth(500).setHeight(200).setRect(0, 0, 540, 540).setCropMode(CropMode.RECT) .setResizeMode(ResizeMode.SMART)); assertThat(outputImage7).matchesReference("outputImage7-smart-crop-reference.png"); BufferedImage outputImage8 = manipulator.cropAndResize(biS, new ImageManipulationParametersImpl().setWidth(200).setHeight(500).setRect(0, 0, 540, 540).setCropMode(CropMode.RECT) .setResizeMode(ResizeMode.SMART)); assertThat(outputImage8).matchesReference("outputImage8-smart-crop-reference.png"); BufferedImage outputImage9 = manipulator.cropAndResize(biS, new ImageManipulationParametersImpl().setWidth(500).setHeight(500).setRect(0, 0, 540, 540).setCropMode(CropMode.RECT) .setResizeMode(ResizeMode.SMART)); assertThat(outputImage9).matchesReference("outputImage9-smart-crop-reference.png"); }
@Test public void testForceResize() throws IOException { BufferedImage biH = ImageTestUtil.readImage("testgrid-horizontal-hd_1920x1080.png"); BufferedImage outputImage1 = manipulator.cropAndResize(biH, new ImageManipulationParametersImpl().setWidth(500).setHeight(200).setResizeMode(ResizeMode.FORCE)); assertThat(outputImage1).matchesReference("outputImage1-force-reference.png"); BufferedImage outputImage2 = manipulator.cropAndResize(biH, new ImageManipulationParametersImpl().setWidth(200).setHeight(500).setResizeMode(ResizeMode.FORCE)); assertThat(outputImage2).matchesReference("outputImage2-force-reference.png"); BufferedImage outputImage3 = manipulator.cropAndResize(biH, new ImageManipulationParametersImpl().setWidth(500).setHeight(500).setResizeMode(ResizeMode.FORCE)); assertThat(outputImage3).matchesReference("outputImage3-force-reference.png"); BufferedImage biV = ImageTestUtil.readImage("testgrid-vertical-hd_1080x1920.png"); BufferedImage outputImage4 = manipulator.cropAndResize(biV, new ImageManipulationParametersImpl().setWidth(500).setHeight(200).setResizeMode(ResizeMode.FORCE)); assertThat(outputImage4).matchesReference("outputImage4-force-reference.png"); BufferedImage outputImage5 = manipulator.cropAndResize(biV, new ImageManipulationParametersImpl().setWidth(200).setHeight(500).setResizeMode(ResizeMode.FORCE)); assertThat(outputImage5).matchesReference("outputImage5-force-reference.png"); BufferedImage outputImage6 = manipulator.cropAndResize(biV, new ImageManipulationParametersImpl().setWidth(500).setHeight(500).setResizeMode(ResizeMode.FORCE)); assertThat(outputImage6).matchesReference("outputImage6-force-reference.png"); BufferedImage biS = ImageTestUtil.readImage("testgrid-square_1080x1080.png"); BufferedImage outputImage7 = manipulator.cropAndResize(biS, new ImageManipulationParametersImpl().setWidth(500).setHeight(300).setResizeMode(ResizeMode.FORCE)); assertThat(outputImage7).matchesReference("outputImage7-force-reference.png"); BufferedImage outputImage8 = manipulator.cropAndResize(biS, new ImageManipulationParametersImpl().setWidth(300).setHeight(500).setResizeMode(ResizeMode.FORCE)); assertThat(outputImage8).matchesReference("outputImage8-force-reference.png"); BufferedImage outputImage9 = manipulator.cropAndResize(biS, new ImageManipulationParametersImpl().setWidth(500).setHeight(500).setResizeMode(ResizeMode.FORCE)); assertThat(outputImage9).matchesReference("outputImage9-force-reference.png"); BufferedImage outputImage10 = manipulator.cropAndResize(biH, new ImageManipulationParametersImpl().setWidth(1920).setHeight(1080).setResizeMode(ResizeMode.FORCE)); assertEquals("The image should not have been resized since the parameters match the source image dimension.", biH.hashCode(), outputImage10 .hashCode()); BufferedImage outputImage11 = manipulator.cropAndResize(biV, new ImageManipulationParametersImpl().setWidth(1080).setHeight(1920).setResizeMode(ResizeMode.FORCE)); assertEquals("The image should not have been resized since the parameters match the source image dimension.", biV.hashCode(), outputImage11 .hashCode()); BufferedImage outputImage12 = manipulator.cropAndResize(biS, new ImageManipulationParametersImpl().setWidth(1080).setHeight(1080).setResizeMode(ResizeMode.FORCE)); assertEquals("The image should not have been resized since the parameters match the source image dimension.", biS.hashCode(), outputImage12 .hashCode()); }
@Test public void testForceResizeCrop() throws IOException { BufferedImage biH = ImageTestUtil.readImage("testgrid-horizontal-hd_1920x1080.png"); BufferedImage outputImage1 = manipulator.cropAndResize(biH, new ImageManipulationParametersImpl().setWidth(500).setHeight(200).setRect(0, 0, 540, 960).setCropMode(CropMode.RECT) .setResizeMode(ResizeMode.FORCE)); assertThat(outputImage1).matchesReference("outputImage1-force-crop-reference.png"); BufferedImage outputImage2 = manipulator.cropAndResize(biH, new ImageManipulationParametersImpl().setWidth(200).setHeight(500).setRect(0, 0, 540, 960).setCropMode(CropMode.RECT) .setResizeMode(ResizeMode.FORCE)); assertThat(outputImage2).matchesReference("outputImage2-force-crop-reference.png"); BufferedImage outputImage3 = manipulator.cropAndResize(biH, new ImageManipulationParametersImpl().setWidth(500).setHeight(500).setRect(0, 0, 540, 960).setCropMode(CropMode.RECT) .setResizeMode(ResizeMode.FORCE)); assertThat(outputImage3).matchesReference("outputImage3-force-crop-reference.png"); BufferedImage biV = ImageTestUtil.readImage("testgrid-vertical-hd_1080x1920.png"); BufferedImage outputImage4 = manipulator.cropAndResize(biV, new ImageManipulationParametersImpl().setWidth(500).setHeight(200).setRect(0, 0, 960, 540).setCropMode(CropMode.RECT) .setResizeMode(ResizeMode.FORCE)); assertThat(outputImage4).matchesReference("outputImage4-force-crop-reference.png"); BufferedImage outputImage5 = manipulator.cropAndResize(biV, new ImageManipulationParametersImpl().setWidth(200).setHeight(500).setRect(0, 0, 960, 540).setCropMode(CropMode.RECT) .setResizeMode(ResizeMode.FORCE)); assertThat(outputImage5).matchesReference("outputImage5-force-crop-reference.png"); BufferedImage outputImage6 = manipulator.cropAndResize(biV, new ImageManipulationParametersImpl().setWidth(500).setHeight(500).setRect(0, 0, 960, 540).setCropMode(CropMode.RECT) .setResizeMode(ResizeMode.FORCE)); assertThat(outputImage6).matchesReference("outputImage6-force-crop-reference.png"); BufferedImage biS = ImageTestUtil.readImage("testgrid-square_1080x1080.png"); BufferedImage outputImage7 = manipulator.cropAndResize(biS, new ImageManipulationParametersImpl().setWidth(500).setHeight(200).setRect(0, 0, 540, 540).setCropMode(CropMode.RECT) .setResizeMode(ResizeMode.FORCE)); assertThat(outputImage7).matchesReference("outputImage7-force-crop-reference.png"); BufferedImage outputImage8 = manipulator.cropAndResize(biS, new ImageManipulationParametersImpl().setWidth(200).setHeight(500).setRect(0, 0, 540, 540).setCropMode(CropMode.RECT) .setResizeMode(ResizeMode.FORCE)); assertThat(outputImage8).matchesReference("outputImage8-force-crop-reference.png"); BufferedImage outputImage9 = manipulator.cropAndResize(biS, new ImageManipulationParametersImpl().setWidth(500).setHeight(500).setRect(0, 0, 540, 540).setCropMode(CropMode.RECT) .setResizeMode(ResizeMode.FORCE)); assertThat(outputImage9).matchesReference("outputImage9-force-crop-reference.png"); }
@Test public void testSmartResize() throws IOException { BufferedImage biH = ImageTestUtil.readImage("testgrid-horizontal-hd_1920x1080.png"); BufferedImage outputImage1 = manipulator.cropAndResize(biH, new ImageManipulationParametersImpl().setWidth(500).setHeight(300)); assertThat(outputImage1).matchesReference("outputImage1-smart-reference.png"); BufferedImage outputImage2 = manipulator.cropAndResize(biH, new ImageManipulationParametersImpl().setWidth(300).setHeight(500)); assertThat(outputImage2).matchesReference("outputImage2-smart-reference.png"); BufferedImage outputImage3 = manipulator.cropAndResize(biH, new ImageManipulationParametersImpl().setWidth(500).setHeight(500)); assertThat(outputImage3).matchesReference("outputImage3-smart-reference.png"); BufferedImage biV = ImageTestUtil.readImage("testgrid-vertical-hd_1080x1920.png"); BufferedImage outputImage4 = manipulator.cropAndResize(biV, new ImageManipulationParametersImpl().setWidth(500).setHeight(300)); assertThat(outputImage4).matchesReference("outputImage4-smart-reference.png"); BufferedImage outputImage5 = manipulator.cropAndResize(biV, new ImageManipulationParametersImpl().setWidth(300).setHeight(500)); assertThat(outputImage5).matchesReference("outputImage5-smart-reference.png"); BufferedImage outputImage6 = manipulator.cropAndResize(biV, new ImageManipulationParametersImpl().setWidth(500).setHeight(500)); assertThat(outputImage6).matchesReference("outputImage6-smart-reference.png"); BufferedImage biS = ImageTestUtil.readImage("testgrid-square_1080x1080.png"); BufferedImage outputImage7 = manipulator.cropAndResize(biS, new ImageManipulationParametersImpl().setWidth(500).setHeight(300)); assertThat(outputImage7).matchesReference("outputImage7-smart-reference.png"); BufferedImage outputImage8 = manipulator.cropAndResize(biS, new ImageManipulationParametersImpl().setWidth(300).setHeight(500)); assertThat(outputImage8).matchesReference("outputImage8-smart-reference.png"); BufferedImage outputImage9 = manipulator.cropAndResize(biS, new ImageManipulationParametersImpl().setWidth(500).setHeight(500)); assertThat(outputImage9).matchesReference("outputImage9-smart-reference.png"); BufferedImage outputImage10 = manipulator.cropAndResize(biH, new ImageManipulationParametersImpl().setWidth(1920).setHeight(1080)); assertEquals("The image should not have been resized since the parameters match the source image dimension.", biH.hashCode(), outputImage10 .hashCode()); BufferedImage outputImage11 = manipulator.cropAndResize(biV, new ImageManipulationParametersImpl().setWidth(1080).setHeight(1920)); assertEquals("The image should not have been resized since the parameters match the source image dimension.", biV.hashCode(), outputImage11 .hashCode()); BufferedImage outputImage12 = manipulator.cropAndResize(biS, new ImageManipulationParametersImpl().setWidth(1080).setHeight(1080)); assertEquals("The image should not have been resized since the parameters match the source image dimension.", biS.hashCode(), outputImage12 .hashCode()); }