├── .gitattributes ├── .github ├── ISSUE_TEMPLATE.md └── PULL_REQUEST_TEMPLATE.md ├── .gitignore ├── LICENSE ├── README.md ├── pom.xml └── src ├── main ├── java │ └── net │ │ └── coobird │ │ └── thumbnailator │ │ ├── ThumbnailParameter.java │ │ ├── Thumbnailator.java │ │ ├── Thumbnails.java │ │ ├── builders │ │ ├── BufferedImageBuilder.java │ │ ├── ThumbnailParameterBuilder.java │ │ └── package-info.java │ │ ├── filters │ │ ├── Canvas.java │ │ ├── Caption.java │ │ ├── Colorize.java │ │ ├── Flip.java │ │ ├── ImageFilter.java │ │ ├── Pipeline.java │ │ ├── Rotation.java │ │ ├── SwapDimensions.java │ │ ├── Transparency.java │ │ ├── Watermark.java │ │ └── package-info.java │ │ ├── geometry │ │ ├── AbsoluteSize.java │ │ ├── Coordinate.java │ │ ├── Position.java │ │ ├── Positions.java │ │ ├── Region.java │ │ ├── RelativeSize.java │ │ ├── Size.java │ │ └── package-info.java │ │ ├── makers │ │ ├── FixedSizeThumbnailMaker.java │ │ ├── ScaledThumbnailMaker.java │ │ ├── ThumbnailMaker.java │ │ └── package-info.java │ │ ├── name │ │ ├── ConsecutivelyNumberedFilenames.java │ │ ├── Rename.java │ │ └── package-info.java │ │ ├── package-info.java │ │ ├── resizers │ │ ├── AbstractResizer.java │ │ ├── BicubicResizer.java │ │ ├── BilinearResizer.java │ │ ├── DefaultResizerFactory.java │ │ ├── FixedResizerFactory.java │ │ ├── NullResizer.java │ │ ├── ProgressiveBilinearResizer.java │ │ ├── Resizer.java │ │ ├── ResizerFactory.java │ │ ├── Resizers.java │ │ ├── configurations │ │ │ ├── AlphaInterpolation.java │ │ │ ├── Antialiasing.java │ │ │ ├── Dithering.java │ │ │ ├── Rendering.java │ │ │ ├── ResizerConfiguration.java │ │ │ ├── ScalingMode.java │ │ │ └── package-info.java │ │ └── package-info.java │ │ ├── tasks │ │ ├── FileThumbnailTask.java │ │ ├── SourceSinkThumbnailTask.java │ │ ├── StreamThumbnailTask.java │ │ ├── ThumbnailTask.java │ │ ├── UnsupportedFormatException.java │ │ ├── io │ │ │ ├── AbstractImageSink.java │ │ │ ├── AbstractImageSource.java │ │ │ ├── BufferedImageSink.java │ │ │ ├── BufferedImageSource.java │ │ │ ├── FileImageSink.java │ │ │ ├── FileImageSource.java │ │ │ ├── ImageSink.java │ │ │ ├── ImageSource.java │ │ │ ├── InputStreamImageSource.java │ │ │ ├── OutputStreamImageSink.java │ │ │ ├── URLImageSource.java │ │ │ └── package-info.java │ │ └── package-info.java │ │ └── util │ │ ├── BufferedImages.java │ │ ├── Configurations.java │ │ ├── ThumbnailatorUtils.java │ │ ├── exif │ │ ├── ExifFilterUtils.java │ │ ├── ExifUtils.java │ │ ├── IfdStructure.java │ │ ├── IfdType.java │ │ ├── Orientation.java │ │ └── package-info.java │ │ └── package-info.java └── resources │ └── META-INF │ └── LICENSE └── test ├── java └── net │ └── coobird │ └── thumbnailator │ ├── TestUtils.java │ ├── ThumbnailatorTest.java │ ├── ThumbnailatorTranscodeTest.java │ ├── ThumbnailatorUtilsTest.java │ ├── ThumbnailsBuilderInputOutputTest.java │ ├── ThumbnailsBuilderNullEmptyInputTest.java │ ├── ThumbnailsBuilderNullEmptyOutputTest.java │ ├── ThumbnailsBuilderTest.java │ ├── builders │ └── ThumbnailParameterBuilderTest.java │ ├── filters │ ├── CanvasTest.java │ ├── CaptionTest.java │ ├── ColorizeTest.java │ ├── FlipTest.java │ ├── ImageFilterTestUtils.java │ ├── PipelineTest.java │ ├── RotationTest.java │ ├── TransparencyTest.java │ └── WatermarkTest.java │ ├── makers │ ├── FixedSizeThumbnailMakerTest.java │ └── ScaledThumbnailMakerTest.java │ ├── name │ ├── ConsecutiveNumbersTest.java │ └── RenameTest.java │ ├── resizers │ ├── DefaultResizerFactoryTest.java │ ├── FixedResizerFactoryTest.java │ ├── NullResizerTest.java │ └── ProgressiveBilinearResizerTest.java │ ├── tasks │ ├── FileThumbnailTaskTest.java │ ├── SourceSinkThumbnailTaskTest.java │ ├── StreamThumbnailTaskTest.java │ └── io │ │ ├── BufferedImageSinkTest.java │ │ ├── BufferedImageSourceTest.java │ │ ├── FileImageSinkTest.java │ │ ├── FileImageSourceTest.java │ │ ├── InputStreamImageSourceMalformedTest.java │ │ ├── InputStreamImageSourceTest.java │ │ ├── Issue156OutputStreamImageSinkTest.java │ │ ├── Issue69FileImageSourceTest.java │ │ ├── Issue69InputStreamImageSourceTest.java │ │ ├── OutputStreamImageSinkTest.java │ │ └── URLImageSourceTest.java │ ├── test │ ├── BufferedImageAssert.java │ └── BufferedImageComparer.java │ └── util │ ├── ConfigurationsTest.java │ └── exif │ ├── ExifFilterUtilsTest.java │ ├── ExifUtilsTest.java │ └── ExifWorkaroundTest.java └── resources ├── Configurations ├── all_false.txt └── all_true.txt ├── Exif ├── fragments │ ├── README │ ├── app0.segment │ ├── exif.segment │ ├── rest │ ├── soi.segment │ └── xmp.segment ├── orientation_1.jpg ├── orientation_2.jpg ├── orientation_3.jpg ├── orientation_4.jpg ├── orientation_5.jpg ├── orientation_6.jpg ├── orientation_7.jpg ├── orientation_8.jpg ├── original.jpg ├── original.png ├── sourceTall_1.jpg ├── sourceTall_2.jpg ├── sourceTall_3.jpg ├── sourceTall_4.jpg ├── sourceTall_5.jpg ├── sourceTall_6.jpg ├── sourceTall_7.jpg ├── sourceTall_8.jpg ├── sourceWide_1.jpg ├── sourceWide_2.jpg ├── sourceWide_3.jpg ├── sourceWide_4.jpg ├── sourceWide_5.jpg ├── sourceWide_6.jpg ├── sourceWide_7.jpg ├── sourceWide_8.jpg ├── source_1.jpg ├── source_2.jpg ├── source_3.jpg ├── source_4.jpg ├── source_5.jpg ├── source_6.jpg ├── source_7.jpg ├── source_8.jpg └── stretch.png └── Thumbnailator ├── grid.bmp ├── grid.gif ├── grid.jpg ├── grid.png └── igrid.png /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | ## Expected behavior 2 | _Please describe what you are expecting the library to perform._ 3 | 4 | 5 | 6 | ## Actual behavior 7 | _Please describe the actual behavior you are experiencing, including stack 8 | trace and other information which would help diagnose the issue._ 9 | 10 | 11 | 12 | ## Steps to reproduce the behavior 13 | _Please enter step-by-step instructions for reproducing the actual behavior. 14 | Including code can be helpful in diagnosing issue, but please keep the code to 15 | a minimal that will reproduce the behavior._ 16 | 17 | 18 | 19 | ## Environment 20 | _Please provide vendor and version information for the Operating System, 21 | JDK, and Thumbnailator. Please feel free to add any other information 22 | which may be pertinent._ 23 | 24 | - OS vendor and version: 25 | - JDK vendor and version: 26 | - Thumbnailator version: -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | **Hello! Thank you for your interest in contributing to Thumbnailator!** 2 | 3 | Unfortunately, I'm not accepting pull requests at this moment. 4 | 5 | Contributions in the form of bugs reports or feature enhancements through 6 | [issues](https://github.com/coobird/thumbnailator/issues) are appreciated. 7 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .classpath 2 | .project 3 | .settings 4 | .idea/ 5 | thumbnailator.iml 6 | bin/ 7 | target/ 8 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2008-2023 Chris Kroells 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | _*June 28, 2023: Thumbnailator 0.4.20 has been released! 2 | See [Changes](https://github.com/coobird/thumbnailator/wiki/Changes) for details.*_ 3 | 4 | _*Thumbnailator is now available through 5 | [Maven](https://github.com/coobird/thumbnailator/wiki/Maven)!*_ 6 | 7 | # What is Thumbnailator? 8 | 9 |  10 | 11 | _Thumbnailator_ is a thumbnail generation library for Java. 12 | 13 | # Why Thumbnailator? 14 | Making high-quality thumbnails in Java can be a fairly difficult task. 15 | 16 | Learning how to use the Image I/O API, Java 2D API, image processing, 17 | image scaling techniques, ... but fear not! _Thumbnailator_ will take care 18 | of all those things for you! 19 | 20 | Thumbnailator is a single JAR file with no dependencies to external libraries, 21 | making development and deployment simple and easy. It is also available on 22 | the Maven Central Repository for easy inclusion in Maven projects. 23 | 24 | # How simple is Thumbnailator? 25 | 26 | _Thumbnailator_'s fluent interface can be used to perform fairly complicated 27 | thumbnail processing task in one simple step. 28 | 29 | For example, creating JPEG thumbnails of image files in a directory, all 30 | resized to a maximum dimension of 640 pixels by 480 pixels while preserving 31 | the aspect ratio of the original image can be performed by the following: 32 | 33 | ``` 34 | Thumbnails.of(new File("path/to/directory").listFiles()) 35 | .size(640, 480) 36 | .outputFormat("jpg") 37 | .toFiles(Rename.PREFIX_DOT_THUMBNAIL); 38 | ``` 39 | 40 | The fluent interface provided by the _Thumbnailator_ simplifies the task of 41 | making thumbnails into a single method call! 42 | 43 | No need to access the Image I/O API and manually manipulate `BufferedImage`s 44 | through `Graphics2D` objects. _Thumbnailator_ does all of that for you. 45 | 46 | # What can Thumbnailator do? 47 | 48 | The following pages have more information on what _Thumbnailator_ can do: 49 | 50 | * [Features](https://github.com/coobird/thumbnailator/wiki/Features) 51 | * [Examples](https://github.com/coobird/thumbnailator/wiki/Examples) 52 | * [Thumbnailator API Documentation](https://coobird.github.io/thumbnailator/javadoc/0.4.20/) 53 | * [Frequently Asked Questions](https://github.com/coobird/thumbnailator/wiki/FAQ) 54 | 55 | # Disclaimer 56 | *Thumbnailator is still early in its development, and the APIs are subject to 57 | change at any time.* 58 | 59 | # License 60 | Thumbnailtor is released under the MIT License. 61 | -------------------------------------------------------------------------------- /src/main/java/net/coobird/thumbnailator/builders/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Thumbnailator - a thumbnail generation library 3 | * 4 | * Copyright (c) 2008-2022 Chris Kroells 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | */ 24 | 25 | /** 26 | * This package provides classes which provides convenient builders for classes 27 | * which are used by Thumbnailator. These classes are used mostly for internal 28 | * processing. 29 | */ 30 | package net.coobird.thumbnailator.builders; 31 | -------------------------------------------------------------------------------- /src/main/java/net/coobird/thumbnailator/filters/Colorize.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Thumbnailator - a thumbnail generation library 3 | * 4 | * Copyright (c) 2008-2020 Chris Kroells 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | */ 24 | 25 | package net.coobird.thumbnailator.filters; 26 | 27 | import java.awt.Color; 28 | import java.awt.Graphics2D; 29 | import java.awt.image.BufferedImage; 30 | 31 | import net.coobird.thumbnailator.builders.BufferedImageBuilder; 32 | import net.coobird.thumbnailator.util.BufferedImages; 33 | 34 | /** 35 | * An image filter which will add a color tint to an image. 36 | * 37 | * @author coobird 38 | * 39 | */ 40 | public final class Colorize implements ImageFilter { 41 | /** 42 | * The color to tint a target image with. 43 | */ 44 | private final Color c; 45 | 46 | /** 47 | * Instantiates this filter with the color to use to tint the target image 48 | * with. 49 | *
50 | * Note: If the provided {@link Color} does not have an alpha channel 51 | * (transparency channel), then the target image will be painted with an 52 | * opaque color, resulting in an image with only the specified color. 53 | * 54 | * @param c Color to tint with. 55 | */ 56 | public Colorize(Color c) { 57 | this.c = c; 58 | } 59 | 60 | /** 61 | * Instantiates this filter with the color to use to tint the target image 62 | * with and the transparency level provided as a {@code float} ranging from 63 | * {@code 0.0f} to {@code 1.0f}, where {@code 0.0f} indicates completely 64 | * transparent, and {@code 1.0f} indicates completely opaque. 65 | * 66 | * @param c Color to tint with. 67 | * @param alpha The opacity of the tint. 68 | */ 69 | public Colorize(Color c, float alpha) { 70 | this(c, (int)(255 * alpha)); 71 | } 72 | 73 | /** 74 | * Instantiates this filter with the color to use to tint the target image 75 | * with and the transparency level provided as a {@code int} ranging from 76 | * {@code 0} to {@code 255}, where {@code 0} indicates completely 77 | * transparent, and {@code 255} indicates completely opaque. 78 | * 79 | * @param c Color to tint with. 80 | * @param alpha The opacity of the tint. 81 | */ 82 | public Colorize(Color c, int alpha) { 83 | if (alpha > 255 || alpha < 0) { 84 | throw new IllegalArgumentException( 85 | "Specified alpha value is outside the range of allowed " + 86 | "values."); 87 | } 88 | 89 | int r = c.getRed(); 90 | int g = c.getGreen(); 91 | int b = c.getBlue(); 92 | int a = alpha; 93 | 94 | this.c = new Color(r, g, b, a); 95 | } 96 | 97 | public BufferedImage apply(BufferedImage img) { 98 | int width = img.getWidth(); 99 | int height = img.getHeight(); 100 | 101 | BufferedImage newImage = new BufferedImageBuilder(width, height).build(); 102 | 103 | Graphics2D g = newImage.createGraphics(); 104 | g.drawImage(img, 0, 0, null); 105 | g.setColor(c); 106 | g.fillRect(0, 0, width, height); 107 | g.dispose(); 108 | 109 | if (img.getType() != newImage.getType()) { 110 | return BufferedImages.copy(newImage, img.getType()); 111 | } 112 | 113 | return newImage; 114 | } 115 | } 116 | -------------------------------------------------------------------------------- /src/main/java/net/coobird/thumbnailator/filters/Flip.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Thumbnailator - a thumbnail generation library 3 | * 4 | * Copyright (c) 2008-2020 Chris Kroells 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | */ 24 | 25 | package net.coobird.thumbnailator.filters; 26 | 27 | import java.awt.Graphics; 28 | import java.awt.image.BufferedImage; 29 | 30 | import net.coobird.thumbnailator.builders.BufferedImageBuilder; 31 | 32 | /** 33 | * A class containing flip transformation filters. 34 | * 35 | * @author coobird 36 | * 37 | */ 38 | public class Flip { 39 | 40 | /** 41 | * An image filter which performs a horizontal flip of the image. 42 | */ 43 | public static final ImageFilter HORIZONTAL = new ImageFilter() { 44 | public BufferedImage apply(BufferedImage img) { 45 | int width = img.getWidth(); 46 | int height = img.getHeight(); 47 | 48 | BufferedImage newImage = 49 | new BufferedImageBuilder(width, height, img.getType()).build(); 50 | 51 | Graphics g = newImage.getGraphics(); 52 | g.drawImage(img, width, 0, 0, height, 0, 0, width, height, null); 53 | g.dispose(); 54 | 55 | return newImage; 56 | } 57 | }; 58 | 59 | /** 60 | * An image filter which performs a vertical flip of the image. 61 | */ 62 | public static final ImageFilter VERTICAL = new ImageFilter() { 63 | public BufferedImage apply(BufferedImage img) { 64 | int width = img.getWidth(); 65 | int height = img.getHeight(); 66 | 67 | BufferedImage newImage = 68 | new BufferedImageBuilder(width, height, img.getType()).build(); 69 | 70 | Graphics g = newImage.getGraphics(); 71 | g.drawImage(img, 0, height, width, 0, 0, 0, width, height, null); 72 | g.dispose(); 73 | 74 | return newImage; 75 | } 76 | }; 77 | } 78 | -------------------------------------------------------------------------------- /src/main/java/net/coobird/thumbnailator/filters/ImageFilter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Thumbnailator - a thumbnail generation library 3 | * 4 | * Copyright (c) 2008-2020 Chris Kroells 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | */ 24 | 25 | package net.coobird.thumbnailator.filters; 26 | 27 | import java.awt.image.BufferedImage; 28 | 29 | /** 30 | * This interface is to be implemented by classes which performs an image 31 | * filtering operation on a {@link BufferedImage}. 32 | *
33 | * The general contract for classes implementing {@link ImageFilter} is that 34 | * they should not change the contents of the {@link BufferedImage} which is 35 | * given as the argument for the {@link #apply(BufferedImage)} method. 36 | *
37 | * The filter should make a copy of the given {@link BufferedImage}, and
38 | * perform the filtering operations on the copy, then return the copy.
39 | *
40 | * @author coobird
41 | *
42 | */
43 | public interface ImageFilter {
44 | /**
45 | * Applies a image filtering operation on an image.
46 | *
47 | * @param img The image to apply the filtering on.
48 | * @return The resulting image after applying this filter.
49 | */
50 | public BufferedImage apply(BufferedImage img);
51 | }
52 |
--------------------------------------------------------------------------------
/src/main/java/net/coobird/thumbnailator/filters/Pipeline.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Thumbnailator - a thumbnail generation library
3 | *
4 | * Copyright (c) 2008-2020 Chris Kroells
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | package net.coobird.thumbnailator.filters;
26 |
27 | import java.awt.image.BufferedImage;
28 | import java.util.ArrayList;
29 | import java.util.Arrays;
30 | import java.util.Collections;
31 | import java.util.List;
32 |
33 | import net.coobird.thumbnailator.util.BufferedImages;
34 |
35 | /**
36 | * An {@link ImageFilter} which will apply multiple {@link ImageFilter}s in a
37 | * specific order.
38 | *
39 | * @author coobird
40 | *
41 | */
42 | public final class Pipeline implements ImageFilter {
43 | /**
44 | * A list of image filters to apply.
45 | */
46 | private final List
33 | * This is an internal filter that should not be used by consumers of
34 | * Thumbnailator.
35 | */
36 | public class SwapDimensions implements ImageFilter {
37 | private static final SwapDimensions INSTANCE = new SwapDimensions();
38 | private SwapDimensions() {}
39 |
40 | public static SwapDimensions getInstance() {
41 | return INSTANCE;
42 | }
43 |
44 | public BufferedImage apply(BufferedImage img) {
45 | return img;
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/src/main/java/net/coobird/thumbnailator/filters/Transparency.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Thumbnailator - a thumbnail generation library
3 | *
4 | * Copyright (c) 2008-2020 Chris Kroells
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | package net.coobird.thumbnailator.filters;
26 |
27 | import net.coobird.thumbnailator.builders.BufferedImageBuilder;
28 | import net.coobird.thumbnailator.util.BufferedImages;
29 |
30 | import java.awt.AlphaComposite;
31 | import java.awt.Graphics2D;
32 | import java.awt.image.BufferedImage;
33 |
34 | /**
35 | * An image filter which will make an image transparent.
36 | *
37 | * The resulting image will always have an image type of
38 | * {@link BufferedImage#TYPE_INT_ARGB}.
39 | *
40 | * @author coobird
41 | *
42 | */
43 | public class Transparency implements ImageFilter {
44 | /**
45 | * The alpha composite to use when drawing the transparent image.
46 | */
47 | private final AlphaComposite composite;
48 |
49 | /**
50 | * Instantiates a {@link Transparency} filter with the specified opacity.
51 | *
52 | * @param alpha The opacity of the resulting image. The value should be
53 | * between {@code 0.0f} (transparent) to {@code 1.0f}
54 | * (opaque), inclusive.
55 | * @throws IllegalArgumentException If the specified opacity is outside of
56 | * the range specified above.
57 | */
58 | public Transparency(float alpha) {
59 | super();
60 |
61 | if (alpha < 0.0f || alpha > 1.0f) {
62 | throw new IllegalArgumentException(
63 | "The alpha must be between 0.0f and 1.0f, inclusive.");
64 | }
65 |
66 | this.composite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha);
67 | }
68 |
69 | /**
70 | * Instantiates a {@link Transparency} filter with the specified opacity.
71 | *
72 | * This is a convenience constructor for the
73 | * {@link Transparency#Transparency(float)} constructor.
74 | *
75 | * @param alpha The opacity of the resulting image. The value should be
76 | * between {@code 0.0f} (transparent) to {@code 1.0f}
77 | * (opaque), inclusive.
78 | * @throws IllegalArgumentException If the specified opacity is outside of
79 | * the range specified above.
80 | */
81 | public Transparency(double alpha) {
82 | this((float)alpha);
83 | }
84 |
85 | public BufferedImage apply(BufferedImage img) {
86 | int width = img.getWidth();
87 | int height = img.getHeight();
88 |
89 | BufferedImage finalImage = new BufferedImageBuilder(
90 | width,
91 | height,
92 | BufferedImage.TYPE_INT_ARGB
93 | ).build();
94 |
95 | Graphics2D g = finalImage.createGraphics();
96 | g.setComposite(composite);
97 | g.drawImage(img, 0, 0, null);
98 | g.dispose();
99 |
100 | if (img.getType() != finalImage.getType()) {
101 | return BufferedImages.copy(finalImage, img.getType());
102 | }
103 |
104 | return finalImage;
105 | }
106 |
107 | /**
108 | * Returns the opacity of this filter.
109 | *
110 | * @return The opacity in the range of {@code 0.0f} (transparent) to
111 | * {@code 1.0f} (opaque).
112 | */
113 | public float getAlpha() {
114 | return composite.getAlpha();
115 | }
116 | }
117 |
--------------------------------------------------------------------------------
/src/main/java/net/coobird/thumbnailator/filters/Watermark.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Thumbnailator - a thumbnail generation library
3 | *
4 | * Copyright (c) 2008-2020 Chris Kroells
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | package net.coobird.thumbnailator.filters;
26 |
27 | import java.awt.AlphaComposite;
28 | import java.awt.Graphics2D;
29 | import java.awt.Point;
30 | import java.awt.image.BufferedImage;
31 |
32 | import net.coobird.thumbnailator.builders.BufferedImageBuilder;
33 | import net.coobird.thumbnailator.geometry.Position;
34 |
35 | /**
36 | * This class applies a watermark to an image.
37 | *
38 | * @author coobird
39 | *
40 | */
41 | public class Watermark implements ImageFilter {
42 | /**
43 | * The position of the watermark.
44 | */
45 | private final Position position;
46 |
47 | /**
48 | * The watermark image.
49 | */
50 | private final BufferedImage watermarkImg;
51 |
52 | /**
53 | * The opacity of the watermark.
54 | */
55 | private final float opacity;
56 |
57 | /**
58 | * The insets for the watermark.
59 | */
60 | private final int insets;
61 |
62 | /**
63 | * Instantiates a filter which applies a watermark to an image.
64 | *
65 | * @param position The position of the watermark.
66 | * @param watermarkImg The watermark image.
67 | * @param opacity The opacity of the watermark.
68 | *
69 | * The value should be between {@code 0.0f} and
70 | * {@code 1.0f}, where {@code 0.0f} is completely
71 | * transparent, and {@code 1.0f} is completely
72 | * opaque.
73 | * @param insets Inset size around the watermark.
74 | * Cannot be negative.
75 | */
76 | public Watermark(Position position, BufferedImage watermarkImg, float opacity, int insets) {
77 | if (position == null) {
78 | throw new NullPointerException("Position is null.");
79 | }
80 | if (watermarkImg == null) {
81 | throw new NullPointerException("Watermark image is null.");
82 | }
83 | if (opacity > 1.0f || opacity < 0.0f) {
84 | throw new IllegalArgumentException("Opacity is out of range of " +
85 | "between 0.0f and 1.0f.");
86 | }
87 | if (insets < 0) {
88 | throw new IllegalArgumentException("Insets cannot be negative.");
89 | }
90 |
91 | this.position = position;
92 | this.watermarkImg = watermarkImg;
93 | this.opacity = opacity;
94 | this.insets = insets;
95 | }
96 |
97 | /**
98 | * Instantiates a filter which applies a watermark to an image.
99 | *
100 | * @param position The position of the watermark.
101 | * @param watermarkImg The watermark image.
102 | * @param opacity The opacity of the watermark.
103 | *
104 | * The value should be between {@code 0.0f} and
105 | * {@code 1.0f}, where {@code 0.0f} is completely
106 | * transparent, and {@code 1.0f} is completely
107 | * opaque.
108 | */
109 | public Watermark(Position position, BufferedImage watermarkImg, float opacity) {
110 | this(position, watermarkImg, opacity, 0);
111 | }
112 |
113 | public BufferedImage apply(BufferedImage img) {
114 | int width = img.getWidth();
115 | int height = img.getHeight();
116 | int type = img.getType();
117 |
118 | BufferedImage imgWithWatermark =
119 | new BufferedImageBuilder(width, height, type).build();
120 |
121 | int watermarkWidth = watermarkImg.getWidth();
122 | int watermarkHeight = watermarkImg.getHeight();
123 |
124 | Point p = position.calculate(
125 | width, height, watermarkWidth, watermarkHeight,
126 | insets, insets, insets, insets
127 | );
128 |
129 | Graphics2D g = imgWithWatermark.createGraphics();
130 |
131 | // Draw the actual image.
132 | g.drawImage(img, 0, 0, null);
133 |
134 | // Draw the watermark on top.
135 | g.setComposite(
136 | AlphaComposite.getInstance(AlphaComposite.SRC_OVER, opacity)
137 | );
138 |
139 | g.drawImage(watermarkImg, p.x, p.y, null);
140 |
141 | g.dispose();
142 |
143 | return imgWithWatermark;
144 | }
145 | }
146 |
--------------------------------------------------------------------------------
/src/main/java/net/coobird/thumbnailator/filters/package-info.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Thumbnailator - a thumbnail generation library
3 | *
4 | * Copyright (c) 2008-2020 Chris Kroells
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | /**
26 | * This package provides classes which perform filtering operations on images,
27 | * such as adding watermark, text captions, and color tints.
28 | */
29 | package net.coobird.thumbnailator.filters;
30 |
--------------------------------------------------------------------------------
/src/main/java/net/coobird/thumbnailator/geometry/AbsoluteSize.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Thumbnailator - a thumbnail generation library
3 | *
4 | * Copyright (c) 2008-2020 Chris Kroells
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | package net.coobird.thumbnailator.geometry;
26 |
27 | import java.awt.Dimension;
28 |
29 | /**
30 | * A {@link Size} object which indicates that the size of the enclosed object
31 | * should be the specified absolute size.
32 | *
33 | * @author coobird
34 | * @since 0.3.4
35 | *
36 | */
37 | public class AbsoluteSize implements Size {
38 | /**
39 | * The size of the object.
40 | */
41 | private final Dimension size;
42 |
43 | /**
44 | * Instantiates an object which indicates size of an object.
45 | *
46 | * @param size Size of the enclosed object.
47 | * @throws NullPointerException If the size is {@code null}.
48 | */
49 | public AbsoluteSize(Dimension size) {
50 | if (size == null) {
51 | throw new NullPointerException("Size cannot be null.");
52 | }
53 | this.size = new Dimension(size);
54 | }
55 |
56 | /**
57 | * Instantiates an object which indicates size of an object.
58 | *
59 | * @param width Width of the enclosed object.
60 | * @param height Height of the enclosed object.
61 | * @throws IllegalArgumentException If the width and/or height is less
62 | * than or equal to {@code 0}.
63 | */
64 | public AbsoluteSize(int width, int height) {
65 | if (width <= 0 || height <= 0) {
66 | throw new IllegalArgumentException(
67 | "Width and height must be greater than 0."
68 | );
69 | }
70 | this.size = new Dimension(width, height);
71 | }
72 |
73 | public Dimension calculate(int width, int height) {
74 | if (width <= 0 || height <= 0) {
75 | throw new IllegalArgumentException(
76 | "Width and height must be greater than 0."
77 | );
78 | }
79 | return new Dimension(size);
80 | }
81 |
82 | /**
83 | * Returns a {@code String} representation of this object.
84 | *
85 | * @return {@code String} representation of this object.
86 | */
87 | @Override
88 | public String toString() {
89 | return "AbsoluteSize [width=" + size.width + ", height=" + size.height + "]";
90 | }
91 | }
92 |
--------------------------------------------------------------------------------
/src/main/java/net/coobird/thumbnailator/geometry/Coordinate.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Thumbnailator - a thumbnail generation library
3 | *
4 | * Copyright (c) 2008-2020 Chris Kroells
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | package net.coobird.thumbnailator.geometry;
26 |
27 | import java.awt.Point;
28 |
29 | /**
30 | * This class calculates the position of an image which is to be enclosed,
31 | * using an absolute coordinate at which the image should be located.
32 | *
33 | * @author coobird
34 | *
35 | */
36 | public final class Coordinate implements Position {
37 | /**
38 | * The horizontal position of the image to be enclosed.
39 | */
40 | private final int x;
41 |
42 | /**
43 | * The vertical position of the image to be enclosed.
44 | */
45 | private final int y;
46 |
47 | /**
48 | * Instantiates an object which calculates the position of an image, using
49 | * the given coordinates.
50 | *
51 | * @param x The horizontal component of the top-left corner of the
52 | * image to be enclosed.
53 | * @param y The vertical component of the top-left corner of the
54 | * image to be enclosed.
55 | */
56 | public Coordinate(int x, int y) {
57 | this.x = x;
58 | this.y = y;
59 | }
60 |
61 | public Point calculate(int enclosingWidth, int enclosingHeight, int width,
62 | int height, int insetLeft, int insetRight, int insetTop,
63 | int insetBottom) {
64 |
65 | int x = this.x + insetLeft;
66 | int y = this.y + insetTop;
67 |
68 | return new Point(x, y);
69 | }
70 | }
71 |
--------------------------------------------------------------------------------
/src/main/java/net/coobird/thumbnailator/geometry/Position.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Thumbnailator - a thumbnail generation library
3 | *
4 | * Copyright (c) 2008-2020 Chris Kroells
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | package net.coobird.thumbnailator.geometry;
26 |
27 | import java.awt.Point;
28 |
29 | /**
30 | * This interface is implemented by classes which calculate how to position an
31 | * object inside of an enclosing object.
32 | *
33 | * @author coobird
34 | *
35 | */
36 | public interface Position {
37 | /**
38 | * Calculates the position of an object enclosed by an enclosing object.
39 | *
40 | * @param enclosingWidth The width of the enclosing object that is
41 | * to contain the enclosed object.
42 | * @param enclosingHeight The height of the enclosing object that is
43 | * to contain the enclosed object.
44 | * @param width The width of the object that is to be
45 | * placed inside an enclosing object.
46 | * @param height The height of the object that is to be
47 | * placed inside an enclosing object.
48 | * @param insetLeft The inset on the left-hand side of the
49 | * object to be enclosed.
50 | * @param insetRight The inset on the right-hand side of the
51 | * object to be enclosed.
52 | * @param insetTop The inset on the top side of the
53 | * object to be enclosed.
54 | * @param insetBottom The inset on the bottom side of the
55 | * object to be enclosed.
56 | * @return The position to place the object.
57 | */
58 | public Point calculate(
59 | int enclosingWidth, int enclosingHeight, int width, int height,
60 | int insetLeft, int insetRight, int insetTop, int insetBottom
61 | );
62 | }
63 |
--------------------------------------------------------------------------------
/src/main/java/net/coobird/thumbnailator/geometry/Region.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Thumbnailator - a thumbnail generation library
3 | *
4 | * Copyright (c) 2008-2023 Chris Kroells
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | package net.coobird.thumbnailator.geometry;
26 |
27 | import java.awt.Dimension;
28 | import java.awt.Point;
29 | import java.awt.Rectangle;
30 |
31 | /**
32 | * A representation of a region, using a {@link Position} object and a
33 | * {@link Dimension} object.
34 | *
35 | * @author coobird
36 | * @since 0.3.4
37 | *
38 | */
39 | public final class Region {
40 | /**
41 | * Position of the region.
42 | */
43 | private final Position position;
44 |
45 | /**
46 | * Size of the region.
47 | */
48 | private final Size size;
49 |
50 | /**
51 | * Instantiates a representation of a region from a {@link Position} and
52 | * {@link Size}.
53 | *
54 | * @param position Position of the region.
55 | * @param size Size of the region.
56 | * @throws NullPointerException When the position and/or the size is
57 | * {@code null}.
58 | */
59 | public Region(Position position, Size size) {
60 | super();
61 | if (position == null) {
62 | throw new NullPointerException("Position cannot be null.");
63 | }
64 | if (size == null) {
65 | throw new NullPointerException("Size cannot be null.");
66 | }
67 |
68 | this.position = position;
69 | this.size = size;
70 | }
71 |
72 | /**
73 | * Returns the position of the region.
74 | *
75 | * @return Position of the region.
76 | */
77 | public Position getPosition() {
78 | return position;
79 | }
80 |
81 | /**
82 | * Returns the size of the region.
83 | *
84 | * @return Size of the region.
85 | */
86 | public Size getSize() {
87 | return size;
88 | }
89 |
90 | /**
91 | * Calculates the position and size of the enclosed region, relative to the
92 | * enclosing region.
93 | *
94 | * The portions of the enclosed region which lies outside the enclosing
95 | * region are ignored. Effectively, the {@link Rectangle} returned by this
96 | * method is an intersection of the enclosing and enclose regions.
97 | *
98 | * @param outerWidth Width of the enclosing region.
99 | * @param outerHeight Height of the enclosing region.
100 | * @param flipHorizontal Whether enclosed region should flip
101 | * horizontally within the enclosing region.
102 | * @param flipVertical Whether enclosed region should flip
103 | * vertically within the enclosing region.
104 | * @param swapDimensions Whether the components of the point and
105 | * dimension of the enclosed region should be
106 | * swapped.
107 | * @return Position and size of the enclosed region.
108 | */
109 | public Rectangle calculate(
110 | int outerWidth,
111 | int outerHeight,
112 | boolean flipHorizontal,
113 | boolean flipVertical,
114 | boolean swapDimensions
115 | ) {
116 | Dimension innerDimension = size.calculate(outerWidth, outerHeight);
117 |
118 | Point innerPoint = position.calculate(
119 | outerWidth, outerHeight, innerDimension.width, innerDimension.height, 0, 0, 0, 0
120 | );
121 |
122 | if (swapDimensions) {
123 | innerDimension = new Dimension(innerDimension.height, innerDimension.width);
124 | innerPoint = new Point(innerPoint.y, innerPoint.x);
125 | }
126 |
127 | if (flipHorizontal) {
128 | int newX = outerWidth - innerPoint.x - innerDimension.width;
129 | innerPoint.setLocation(newX, innerPoint.y);
130 | }
131 | if (flipVertical) {
132 | int newY = outerHeight - innerPoint.y - innerDimension.height;
133 | innerPoint.setLocation(innerPoint.x, newY);
134 | }
135 |
136 | Rectangle outerRectangle = new Rectangle(0, 0, outerWidth, outerHeight);
137 | Rectangle innerRectangle = new Rectangle(innerPoint, innerDimension);
138 |
139 | return outerRectangle.intersection(innerRectangle);
140 | }
141 |
142 | /**
143 | * Returns a {@code String} representation of this region.
144 | *
145 | * @return {@code String} representation of this region.
146 | */
147 | @Override
148 | public String toString() {
149 | return "Region [position=" + position + ", size=" + size + "]";
150 | }
151 | }
152 |
--------------------------------------------------------------------------------
/src/main/java/net/coobird/thumbnailator/geometry/RelativeSize.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Thumbnailator - a thumbnail generation library
3 | *
4 | * Copyright (c) 2008-2020 Chris Kroells
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | package net.coobird.thumbnailator.geometry;
26 |
27 | import java.awt.Dimension;
28 |
29 | /**
30 | * Calculates the size of an enclosed object relative to the enclosing object.
31 | *
32 | * @author coobird
33 | * @since 0.3.4
34 | *
35 | */
36 | public class RelativeSize implements Size {
37 | /**
38 | * The scaling factor to use for the enclosed object.
39 | */
40 | private final double scalingFactor;
41 |
42 | /**
43 | * Instantiates an object which calculates the size of an object, using
44 | * the given scaling factor.
45 | *
46 | * @param scalingFactor The scaling factor to use to determine the
47 | * size of the enclosing object.
48 | * @throws IllegalArgumentException When the scaling factor is not within
49 | * the range of {@code 0.0d} and
50 | * {@code 1.0d}, inclusive.
51 | */
52 | public RelativeSize(double scalingFactor) {
53 | super();
54 | if (scalingFactor < 0.0d || scalingFactor > 1.0d) {
55 | throw new IllegalArgumentException(
56 | "The scaling factor must be between 0.0d and 1.0d, inclusive."
57 | );
58 | }
59 | this.scalingFactor = scalingFactor;
60 | }
61 |
62 | public Dimension calculate(int width, int height) {
63 | if (width <= 0 || height <= 0) {
64 | throw new IllegalArgumentException(
65 | "Width and height must be greater than 0."
66 | );
67 | }
68 | int newWidth = (int)Math.round(width * scalingFactor);
69 | int newHeight = (int)Math.round(height * scalingFactor);
70 | return new Dimension(newWidth, newHeight);
71 | }
72 |
73 | /**
74 | * Returns a {@code String} representation of this object.
75 | *
76 | * @return {@code String} representation of this object.
77 | */
78 | @Override
79 | public String toString() {
80 | return "RelativeSize [scalingFactor=" + scalingFactor + "]";
81 | }
82 | }
83 |
--------------------------------------------------------------------------------
/src/main/java/net/coobird/thumbnailator/geometry/Size.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Thumbnailator - a thumbnail generation library
3 | *
4 | * Copyright (c) 2008-2020 Chris Kroells
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | package net.coobird.thumbnailator.geometry;
26 |
27 | import java.awt.Dimension;
28 |
29 | /**
30 | * This interface is implemented by classes which calculate the size of an
31 | * object inside of an enclosing object.
32 | *
33 | * @author coobird
34 | * @since 0.3.4
35 | *
36 | */
37 | public interface Size {
38 | /**
39 | * Calculates the size of the object.
40 | *
41 | * @param width Width of the object which encloses the object
42 | * for which the size should be determined.
43 | * @param height Height of the object which encloses the object
44 | * for which the size should be determined.
45 | * @return Calculated size of the object.
46 | * @throws IllegalArgumentException If the width and/or height is less than
47 | * or equal to {@code 0}.
48 | */
49 | public Dimension calculate(int width, int height);
50 | }
51 |
--------------------------------------------------------------------------------
/src/main/java/net/coobird/thumbnailator/geometry/package-info.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Thumbnailator - a thumbnail generation library
3 | *
4 | * Copyright (c) 2008-2020 Chris Kroells
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | /**
26 | * This package contains classes used to specify positioning of watermarks and
27 | * other objects in Thumbnailator.
28 | */
29 | package net.coobird.thumbnailator.geometry;
30 |
--------------------------------------------------------------------------------
/src/main/java/net/coobird/thumbnailator/makers/package-info.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Thumbnailator - a thumbnail generation library
3 | *
4 | * Copyright (c) 2008-2020 Chris Kroells
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | /**
26 | * This package provides classes which can be used to make thumbnails given
27 | * parameters to create the images.
28 | */
29 | package net.coobird.thumbnailator.makers;
30 |
--------------------------------------------------------------------------------
/src/main/java/net/coobird/thumbnailator/name/package-info.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Thumbnailator - a thumbnail generation library
3 | *
4 | * Copyright (c) 2008-2020 Chris Kroells
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | /**
26 | * This package contains classes used to generate file names when saving
27 | * thumbnail images to files.
28 | */
29 | package net.coobird.thumbnailator.name;
30 |
--------------------------------------------------------------------------------
/src/main/java/net/coobird/thumbnailator/package-info.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Thumbnailator - a thumbnail generation library
3 | *
4 | * Copyright (c) 2008-2022 Chris Kroells
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | /**
26 | * This package contains classes which provide the core functionalities of
27 | * Thumbnailator, including {@link net.coobird.thumbnailator.Thumbnails},
28 | * the entry point for most use cases of Thumbnailator.
29 | */
30 | package net.coobird.thumbnailator;
31 |
--------------------------------------------------------------------------------
/src/main/java/net/coobird/thumbnailator/resizers/BicubicResizer.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Thumbnailator - a thumbnail generation library
3 | *
4 | * Copyright (c) 2008-2020 Chris Kroells
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | package net.coobird.thumbnailator.resizers;
26 |
27 | import java.awt.RenderingHints;
28 | import java.awt.image.BufferedImage;
29 | import java.util.Collections;
30 | import java.util.Map;
31 |
32 | /**
33 | * Image resizer class using bicubic interpolation for the resizing operation.
34 | *
35 | * @author coobird
36 | *
37 | */
38 | public class BicubicResizer extends AbstractResizer {
39 | /**
40 | * Instantiates a {@link BicubicResizer} with default rendering hints.
41 | */
42 | public BicubicResizer() {
43 | this(Collections.
57 | * Resizes an image using bicubic interpolation.
58 | *
60 | * If the source and/or destination image is {@code null}, then a
61 | * {@link NullPointerException} will be thrown.
62 | *
58 | * Resizes an image using bilinear interpolation.
59 | *
61 | * If the source and/or destination image is {@code null}, then a
62 | * {@link NullPointerException} will be thrown.
63 | *
37 | * The source image will be drawn at the origin of the destination image.
38 | *
39 | * @author coobird
40 | * @since 0.4.0
41 | *
42 | */
43 | public class NullResizer extends AbstractResizer {
44 | /**
45 | * Instantiates the {@code NullResizer} which draws the source image at
46 | * the origin of the destination image.
47 | */
48 | public NullResizer() {
49 | this(
50 | RenderingHints.VALUE_INTERPOLATION_BILINEAR,
51 | Collections.
39 | * The source image is resized to fit the dimensions of the destination
40 | * image and drawn.
41 | *
42 | * @param srcImage The source image.
43 | * @param destImage The destination image.
44 | */
45 | public void resize(BufferedImage srcImage, BufferedImage destImage);
46 | }
47 |
--------------------------------------------------------------------------------
/src/main/java/net/coobird/thumbnailator/resizers/ResizerFactory.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Thumbnailator - a thumbnail generation library
3 | *
4 | * Copyright (c) 2008-2020 Chris Kroells
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | package net.coobird.thumbnailator.resizers;
26 |
27 | import java.awt.Dimension;
28 |
29 | /**
30 | * This interface is implemented by all classes which will return a
31 | * {@link Resizer} that should be used when creating a thumbnail.
32 | *
33 | * @author coobird
34 | * @since 0.4.0
35 | *
36 | */
37 | public interface ResizerFactory {
38 | /**
39 | * Returns the default {@link Resizer}.
40 | *
41 | * @return The default {@code Resizer}.
42 | */
43 | public Resizer getResizer();
44 |
45 | /**
46 | * Returns a suitable {@link Resizer}, given the {@link Dimension}s of the
47 | * original image and the thumbnail image.
48 | *
49 | * @param originalSize The size of the original image.
50 | * @param thumbnailSize The size of the thumbnail.
51 | * @return The suitable {@code Resizer} to perform the
52 | * resizing operation for the given condition.
53 | */
54 | public Resizer getResizer(Dimension originalSize, Dimension thumbnailSize);
55 | }
56 |
--------------------------------------------------------------------------------
/src/main/java/net/coobird/thumbnailator/resizers/Resizers.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Thumbnailator - a thumbnail generation library
3 | *
4 | * Copyright (c) 2008-2020 Chris Kroells
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | package net.coobird.thumbnailator.resizers;
26 |
27 | import java.awt.image.BufferedImage;
28 |
29 | /**
30 | *
31 | * This enum can be used to select a specific {@link Resizer} in order
32 | * to perform a resizing operation.
33 | *
35 | * The instance held by a value of this enum is a single instance. When using
36 | * specific implementations of {@link Resizer}s, it is preferable to obtain
37 | * an instance of a {@link Resizer} through this enum or the
38 | * {@link DefaultResizerFactory} class in order to prevent many instances of the
39 | * {@link Resizer} class implementations from being instantiated.
40 | *
45 | * The following example code demonstrates how to use the {@link Resizers} enum
46 | * in order to resize an image using bilinear interpolation:
47 | *
85 | * For details on this technique, refer to the documentation of the
86 | * {@link ProgressiveBilinearResizer} class.
87 | */
88 | PROGRESSIVE(new ProgressiveBilinearResizer())
89 | ;
90 |
91 | private final Resizer resizer;
92 |
93 | private Resizers(Resizer resizer) {
94 | this.resizer = resizer;
95 | }
96 |
97 | public void resize(BufferedImage srcImage, BufferedImage destImage) {
98 | resizer.resize(srcImage, destImage);
99 | }
100 | }
101 |
--------------------------------------------------------------------------------
/src/main/java/net/coobird/thumbnailator/resizers/configurations/AlphaInterpolation.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Thumbnailator - a thumbnail generation library
3 | *
4 | * Copyright (c) 2008-2020 Chris Kroells
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | /**
26 | *
27 | */
28 | package net.coobird.thumbnailator.resizers.configurations;
29 |
30 | import java.awt.RenderingHints;
31 | import java.awt.RenderingHints.Key;
32 |
33 | /**
34 | * An enum which is used to specify the alpha interpolation settings of the
35 | * resizing operations.
36 | *
37 | * @author coobird
38 | *
39 | */
40 | public enum AlphaInterpolation implements ResizerConfiguration {
41 | /**
42 | * A hint used to emphasize speed when performing alpha interpolation.
43 | */
44 | SPEED(RenderingHints.VALUE_ALPHA_INTERPOLATION_SPEED),
45 |
46 | /**
47 | * A hint used to emphasize quality when performing alpha interpolation.
48 | */
49 | QUALITY(RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY),
50 |
51 | /**
52 | * A hint which indicates to use the default alpha interpolation settings.
53 | */
54 | DEFAULT(RenderingHints.VALUE_ALPHA_INTERPOLATION_DEFAULT),
55 | ;
56 |
57 | /**
58 | * The field used to hold the rendering hint.
59 | */
60 | private final Object value;
61 |
62 | /**
63 | * Instantiates this enum.
64 | *
65 | * @param value The rendering hint value.
66 | */
67 | private AlphaInterpolation(Object value) {
68 | this.value = value;
69 | }
70 |
71 | public Key getKey() {
72 | return RenderingHints.KEY_ALPHA_INTERPOLATION;
73 | }
74 |
75 | public Object getValue() {
76 | return value;
77 | }
78 | }
--------------------------------------------------------------------------------
/src/main/java/net/coobird/thumbnailator/resizers/configurations/Antialiasing.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Thumbnailator - a thumbnail generation library
3 | *
4 | * Copyright (c) 2008-2020 Chris Kroells
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | /**
26 | *
27 | */
28 | package net.coobird.thumbnailator.resizers.configurations;
29 |
30 | import java.awt.RenderingHints;
31 | import java.awt.RenderingHints.Key;
32 |
33 | /**
34 | * An enum which is used to specify the antialiasing settings of the
35 | * resizing operations.
36 | *
37 | * @author coobird
38 | *
39 | */
40 | public enum Antialiasing implements ResizerConfiguration {
41 | /**
42 | * A hint to enable antialiasing.
43 | */
44 | ON(RenderingHints.VALUE_ANTIALIAS_ON),
45 |
46 | /**
47 | * A hint to disable antialiasing.
48 | */
49 | OFF(RenderingHints.VALUE_ANTIALIAS_OFF),
50 |
51 | /**
52 | * A hint to use the default antialiasing settings.
53 | */
54 | DEFAULT(RenderingHints.VALUE_ANTIALIAS_DEFAULT),
55 | ;
56 |
57 | /**
58 | * The field used to hold the rendering hint.
59 | */
60 | private final Object value;
61 |
62 | /**
63 | * Instantiates this enum.
64 | *
65 | * @param value The rendering hint value.
66 | */
67 | private Antialiasing(Object value) {
68 | this.value = value;
69 | }
70 |
71 | public Key getKey() {
72 | return RenderingHints.KEY_ANTIALIASING;
73 | }
74 |
75 | public Object getValue() {
76 | return value;
77 | }
78 | }
--------------------------------------------------------------------------------
/src/main/java/net/coobird/thumbnailator/resizers/configurations/Dithering.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Thumbnailator - a thumbnail generation library
3 | *
4 | * Copyright (c) 2008-2020 Chris Kroells
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | /**
26 | *
27 | */
28 | package net.coobird.thumbnailator.resizers.configurations;
29 |
30 | import java.awt.RenderingHints;
31 | import java.awt.RenderingHints.Key;
32 |
33 | /**
34 | * An enum which is used to specify the dithering settings of the
35 | * resizing operations.
36 | *
37 | * @author coobird
38 | *
39 | */
40 | public enum Dithering implements ResizerConfiguration {
41 | /**
42 | * A hint used to enable dithering.
43 | */
44 | ENABLE(RenderingHints.VALUE_DITHER_ENABLE),
45 |
46 | /**
47 | * A hint used to disable dithering.
48 | */
49 | DISABLE(RenderingHints.VALUE_DITHER_DISABLE),
50 |
51 | /**
52 | * A hint to use the default dithering settings.
53 | */
54 | DEFAULT(RenderingHints.VALUE_DITHER_DEFAULT),
55 | ;
56 |
57 | /**
58 | * The field used to hold the rendering hint.
59 | */
60 | private final Object value;
61 |
62 | /**
63 | * Instantiates this enum.
64 | *
65 | * @param value The rendering hint value.
66 | */
67 | private Dithering(Object value) {
68 | this.value = value;
69 | }
70 |
71 | public Key getKey() {
72 | return RenderingHints.KEY_DITHERING;
73 | }
74 |
75 | public Object getValue() {
76 | return value;
77 | }
78 | }
79 |
--------------------------------------------------------------------------------
/src/main/java/net/coobird/thumbnailator/resizers/configurations/Rendering.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Thumbnailator - a thumbnail generation library
3 | *
4 | * Copyright (c) 2008-2020 Chris Kroells
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | /**
26 | *
27 | */
28 | package net.coobird.thumbnailator.resizers.configurations;
29 |
30 | import java.awt.RenderingHints;
31 | import java.awt.RenderingHints.Key;
32 |
33 | /**
34 | * An enum which is used to specify the dithering settings of the
35 | * resizing operations.
36 | *
37 | * @author coobird
38 | *
39 | */
40 | public enum Rendering implements ResizerConfiguration {
41 | /**
42 | * A hint used to emphasize speed when rendering.
43 | */
44 | SPEED(RenderingHints.VALUE_RENDER_SPEED),
45 |
46 | /**
47 | * A hint used to emphasize quality when rendering.
48 | */
49 | QUALITY(RenderingHints.VALUE_RENDER_QUALITY),
50 |
51 | /**
52 | * A hint to use the default rendering settings.
53 | */
54 | DEFAULT(RenderingHints.VALUE_RENDER_DEFAULT),
55 | ;
56 |
57 | /**
58 | *
59 | */
60 | private final Object value;
61 |
62 | /**
63 | * @param value
64 | */
65 | private Rendering(Object value) {
66 | this.value = value;
67 | }
68 |
69 | public Key getKey() {
70 | return RenderingHints.KEY_ALPHA_INTERPOLATION;
71 | }
72 |
73 | public Object getValue() {
74 | return value;
75 | }
76 | }
--------------------------------------------------------------------------------
/src/main/java/net/coobird/thumbnailator/resizers/configurations/ResizerConfiguration.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Thumbnailator - a thumbnail generation library
3 | *
4 | * Copyright (c) 2008-2020 Chris Kroells
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | package net.coobird.thumbnailator.resizers.configurations;
26 |
27 | import java.awt.RenderingHints;
28 |
29 | import net.coobird.thumbnailator.resizers.Resizer;
30 |
31 | /**
32 | * An interface which are implemented by classes and enums which provide
33 | * configuration information for {@link Resizer}s.
34 | *
35 | * @author coobird
36 | *
37 | */
38 | public interface ResizerConfiguration {
39 | /**
40 | * Returns a rendering hint key.
41 | *
42 | * @return Rendering hint key.
43 | */
44 | public RenderingHints.Key getKey();
45 |
46 | /**
47 | * Returns a rendering hint value.
48 | *
49 | * @return Rendering hint value.
50 | */
51 | public Object getValue();
52 | }
53 |
--------------------------------------------------------------------------------
/src/main/java/net/coobird/thumbnailator/resizers/configurations/ScalingMode.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Thumbnailator - a thumbnail generation library
3 | *
4 | * Copyright (c) 2008-2020 Chris Kroells
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | /**
26 | *
27 | */
28 | package net.coobird.thumbnailator.resizers.configurations;
29 |
30 | import net.coobird.thumbnailator.resizers.ProgressiveBilinearResizer;
31 |
32 | /**
33 | * An enum which is used to specify how to scale images when creating
34 | * thumbnails.
35 | *
36 | * @author coobird
37 | *
38 | */
39 | public enum ScalingMode {
40 | /**
41 | * A hint to use bilinear interpolation when resizing images.
42 | */
43 | BILINEAR,
44 |
45 | /**
46 | * A hint to use bicubic interpolation when resizing images.
47 | */
48 | BICUBIC,
49 |
50 | /**
51 | * A hint to use progressing bilinear interpolation when resizing images.
52 | *
53 | * For details on this technique, refer to the documentation of the
54 | * {@link ProgressiveBilinearResizer} class.
55 | */
56 | PROGRESSIVE_BILINEAR,
57 | ;
58 | }
59 |
--------------------------------------------------------------------------------
/src/main/java/net/coobird/thumbnailator/resizers/configurations/package-info.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Thumbnailator - a thumbnail generation library
3 | *
4 | * Copyright (c) 2008-2020 Chris Kroells
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | /**
26 | * This package provides enums which are used to set rendering hints used when
27 | * using {@link net.coobird.thumbnailator.resizers.Resizers} to create
28 | * thumbnails.
29 | */
30 | package net.coobird.thumbnailator.resizers.configurations;
31 |
--------------------------------------------------------------------------------
/src/main/java/net/coobird/thumbnailator/resizers/package-info.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Thumbnailator - a thumbnail generation library
3 | *
4 | * Copyright (c) 2008-2020 Chris Kroells
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | /**
26 | * This package provides classes which perform image resizing operations which
27 | * is used to create thumbnails with Thumbnailator.
28 | */
29 | package net.coobird.thumbnailator.resizers;
30 |
--------------------------------------------------------------------------------
/src/main/java/net/coobird/thumbnailator/tasks/FileThumbnailTask.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Thumbnailator - a thumbnail generation library
3 | *
4 | * Copyright (c) 2008-2020 Chris Kroells
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | package net.coobird.thumbnailator.tasks;
26 |
27 | import java.awt.image.BufferedImage;
28 | import java.io.File;
29 | import java.io.IOException;
30 |
31 | import net.coobird.thumbnailator.ThumbnailParameter;
32 | import net.coobird.thumbnailator.tasks.io.FileImageSink;
33 | import net.coobird.thumbnailator.tasks.io.FileImageSource;
34 |
35 | /**
36 | * A thumbnail generation task which reads and writes data from and to a
37 | * {@link File}.
38 | *
39 | * Only the first image included in the image file will be read. Subsequent
40 | * images included in the image file will be ignored.
41 | *
42 | * @author coobird
43 | *
44 | */
45 | public class FileThumbnailTask extends ThumbnailTask
39 | * This class will take care of handing off information from the
40 | * {@link ImageSource} to the {@link ImageSink}. For example, the output format
41 | * that should be used by the {@link ImageSink} will be handed off if the
42 | * {@link ThumbnailParameter#ORIGINAL_FORMAT} parameter is set.
43 | *
44 | * @author coobird
45 | *
46 | * @param
40 | * This class does not close the {@link InputStream} and {@link OutputStream}
41 | * upon the completion of processing.
42 | *
43 | * Only the first image obtained from the data stream will be read. Subsequent
44 | * images will be ignored.
45 | *
46 | * @author coobird
47 | *
48 | */
49 | public class StreamThumbnailTask extends ThumbnailTask
36 | * If the image handled by a {@link ThumbnailTask} contains multiple images,
37 | * only the first image will be read by the {@link #read()} method. Any
38 | * subsequent images will be ignored.
39 | *
40 | * @param
55 | * To be used for situtions where the output image format should be the
56 | * same as the input image format.
57 | */
58 | protected String inputFormatName;
59 |
60 | /**
61 | * Constant used to obtain the first image when reading an image file.
62 | */
63 | protected static final int FIRST_IMAGE_INDEX = 0;
64 |
65 | /**
66 | * Instantiates a {@link ThumbnailTask} with the parameters to use when
67 | * creating thumbnails.
68 | *
69 | * @param param The parameters to use when creating thumbnails.
70 | * @throws NullPointerException If the parameter is {@code null}.
71 | */
72 | protected ThumbnailTask(ThumbnailParameter param) {
73 | if (param == null) {
74 | throw new NullPointerException("The parameter is null.");
75 | }
76 | this.param = param;
77 | }
78 |
79 | /**
80 | * Reads a source image.
81 | *
82 | * @return The image which was obtained from the source.
83 | * @throws IOException Thrown when an I/O problem occurs when reading
84 | * from the image source.
85 | */
86 | /*
87 | * Future changes note: The public interface of this method may have to be
88 | * changed to support reading images tile-by-tile. This change may be
89 | * required in order to support large images.
90 | */
91 | public abstract BufferedImage read() throws IOException;
92 |
93 | /**
94 | * Writes the thumbnail to the destination.
95 | *
96 | * @param img The image to write.
97 | * @throws UnsupportedFormatException When an image file which is to be
98 | * read or written is unsupported.
99 | * @throws IOException Thrown when an I/O problem occurs when writing the
100 | * image.
101 | */
102 | public abstract void write(BufferedImage img) throws IOException;
103 |
104 | /**
105 | * Returns the {@link ThumbnailParameter} for this {@link ThumbnailTask},
106 | * used when performing a thumbnail generation operation.
107 | *
108 | * @return The parameters to use when generating thumbnails.
109 | */
110 | public ThumbnailParameter getParam() {
111 | return param;
112 | }
113 |
114 | /**
115 | * Returns the source from which the source image is retrieved or read.
116 | *
117 | * @return The source.
118 | */
119 | public abstract S getSource();
120 |
121 | /**
122 | * Returns the destination to which the thumbnail is stored or written.
123 | *
124 | * @return The destination.
125 | */
126 | public abstract D getDestination();
127 | }
128 |
--------------------------------------------------------------------------------
/src/main/java/net/coobird/thumbnailator/tasks/UnsupportedFormatException.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Thumbnailator - a thumbnail generation library
3 | *
4 | * Copyright (c) 2008-2020 Chris Kroells
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | package net.coobird.thumbnailator.tasks;
26 |
27 | import java.io.IOException;
28 |
29 | /**
30 | * An exception used to indicate that the specified format could not be
31 | * used in an operation.
32 | *
33 | * @author coobird
34 | *
35 | */
36 | public class UnsupportedFormatException extends IOException {
37 | /**
38 | * An ID used for serialization.
39 | */
40 | private static final long serialVersionUID = 1254432584303852552L;
41 |
42 | /**
43 | * The format name which was not supported.
44 | */
45 | private final String formatName;
46 |
47 | /**
48 | * A constant which is used to indicate an unknown format.
49 | */
50 | public static final String UNKNOWN = "
60 | * This method should be used by implementation classes when returning
61 | * the result of the {@link #read()} method, as shown in the following
62 | * example code:
63 |
56 | * For {@link ImageSink}s which stores raw images, the format name specified
57 | * by this method may be ignored.
58 | *
59 | * @param format File format with which to store the image.
60 | */
61 | public void setOutputFormatName(String format);
62 |
63 | /**
64 | * Sets the {@link ThumbnailParameter} from which to retrieve parameters
65 | * to use when storing the image.
66 | *
67 | * @param param The {@link ThumbnailParameter} with image
68 | * writing parameters.
69 | */
70 | public void setThumbnailParameter(ThumbnailParameter param);
71 |
72 | /**
73 | *
74 | * Returns the output format to use from information provided for the
75 | * output image.
76 | *
78 | * If the output format cannot be determined, then
79 | * {@link ThumbnailParameter#ORIGINAL_FORMAT} should be returned.
80 | *
42 | *
56 | *
57 | * @see DefaultResizerFactory
58 | *
59 | * @author coobird
60 | *
61 | */
62 | public enum Resizers implements Resizer {
63 | /**
64 | * A {@link Resizer} which does not perform resizing operations. The source
65 | * image will be drawn at the origin of the destination image.
66 | */
67 | NULL(new NullResizer()),
68 |
69 | /**
70 | * A {@link Resizer} which performs resizing operations using
71 | * bilinear interpolation.
72 | */
73 | BILINEAR(new BilinearResizer()),
74 |
75 | /**
76 | * A {@link Resizer} which performs resizing operations using
77 | * bicubic interpolation.
78 | */
79 | BICUBIC(new BicubicResizer()),
80 |
81 | /**
82 | * A {@link Resizer} which performs resizing operations using
83 | * progressive bilinear scaling.
84 | *
49 | BufferedImage sourceImage = new BufferedImageBuilder(400, 400).build();
50 | BufferedImage destImage = new BufferedImageBuilder(200, 200).build();
51 |
52 | Resizers.BILINEAR.resize(sourceImage, destImage);
53 | *
54 | * The source class from which the source image is retrieved
47 | * or read.
48 | * @param extends ThumbnailTask {
52 | /**
53 | * The source from which the image is retrieved or read.
54 | */
55 | private final ImageSource source;
56 |
57 | /**
58 | * The destination to which the thumbnail is stored or written.
59 | */
60 | private final ImageSink source, ImageSink The class from which the image is retrieved or read.
41 | * @param {
47 | /**
48 | * The parameters to use when creating a thumbnail.
49 | */
50 | protected final ThumbnailParameter param;
51 |
52 | /**
53 | * String indicating the image format of the input image.
54 | *
64 | return finishedReading(sourceImage);
65 |
66 | *
67 | * @param
39 | *
42 | */
43 | TOP_LEFT(1),
44 |
45 | /**
46 | * Orientation 2.
47 | *
48 | *
51 | */
52 | TOP_RIGHT(2),
53 |
54 | /**
55 | * Orientation 3.
56 | *
57 | *
60 | */
61 | BOTTOM_RIGHT(3),
62 |
63 | /**
64 | * Orientation 4.
65 | *
66 | *
69 | */
70 | BOTTOM_LEFT(4),
71 |
72 | /**
73 | * Orientation 5.
74 | *
75 | *
78 | */
79 | LEFT_TOP(5),
80 |
81 | /**
82 | * Orientation 6.
83 | *
84 | *
87 | */
88 | RIGHT_TOP(6),
89 |
90 | /**
91 | * Orientation 7.
92 | *
93 | *
96 | */
97 | RIGHT_BOTTOM(7),
98 |
99 | /**
100 | * Orientation 8.
101 | *
102 | *
105 | */
106 | LEFT_BOTTOM(8),
107 | ;
108 |
109 | private int value;
110 | private Orientation(int value)
111 | {
112 | this.value = value;
113 | }
114 |
115 | /**
116 | * Returns the {@link Orientation} corresponding to the given orientation
117 | * value.
118 | *
119 | * @param value The orientation value.
120 | * @return {@link Orientation} corresponding to the orientation
121 | * value. Return {@code null} if the given value does not
122 | * correspond to a valid {@link Orientation}.
123 | */
124 | public static Orientation typeOf(int value)
125 | {
126 | for (Orientation orientation : Orientation.values())
127 | {
128 | if (orientation.value == value)
129 | {
130 | return orientation;
131 | }
132 | }
133 |
134 | return null;
135 | }
136 |
137 | /**
138 | * Returns a textual {@link String} reprensentation of this enum.
139 | * @return A textual representation of this enum.
140 | */
141 | @Override
142 | public String toString()
143 | {
144 | return "Orientation [type=" + value + "]";
145 | }
146 | }
147 |
--------------------------------------------------------------------------------
/src/main/java/net/coobird/thumbnailator/util/exif/package-info.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Thumbnailator - a thumbnail generation library
3 | *
4 | * Copyright (c) 2008-2020 Chris Kroells
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | /**
26 | * This package contains utilities classes used to handle Exif metadata.
27 | */
28 | package net.coobird.thumbnailator.util.exif;
29 |
--------------------------------------------------------------------------------
/src/main/java/net/coobird/thumbnailator/util/package-info.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Thumbnailator - a thumbnail generation library
3 | *
4 | * Copyright (c) 2008-2020 Chris Kroells
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | /**
26 | * This package contains utilities classes used by Thumbnailator.
27 | */
28 | package net.coobird.thumbnailator.util;
29 |
--------------------------------------------------------------------------------
/src/main/resources/META-INF/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2008-2023 Chris Kroells
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in
13 | all copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 | THE SOFTWARE.
22 |
--------------------------------------------------------------------------------
/src/test/java/net/coobird/thumbnailator/TestUtils.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Thumbnailator - a thumbnail generation library
3 | *
4 | * Copyright (c) 2008-2022 Chris Kroells
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | package net.coobird.thumbnailator;
26 |
27 | import org.junit.rules.TemporaryFolder;
28 |
29 | import java.awt.image.BufferedImage;
30 | import java.io.File;
31 | import java.io.FileInputStream;
32 | import java.io.FileOutputStream;
33 | import java.io.IOException;
34 | import java.io.InputStream;
35 | import java.net.URL;
36 |
37 | import javax.imageio.ImageIO;
38 |
39 | public class TestUtils {
40 | /**
41 | * Copies a specified length of the source file.
42 | *
43 | * @param sourceFile The source file.
44 | * @param destFile The destination file.
45 | * @param length Length of source file to copy.
46 | * @throws IOException If an IOException is thrown.
47 | */
48 | public static void copyFile(File sourceFile, File destFile, long length) throws IOException {
49 | FileInputStream fis = new FileInputStream(sourceFile);
50 | FileOutputStream fos = new FileOutputStream(destFile);
51 | fis.getChannel().transferTo(0, length, fos.getChannel());
52 | fis.close();
53 | fos.close();
54 | }
55 |
56 | /**
57 | * Copies a file.
58 | *
59 | * @param sourceFile The source file.
60 | * @param destFile The destination file.
61 | * @throws IOException If an IOException is thrown.
62 | */
63 | public static void copyFile(File sourceFile, File destFile) throws IOException {
64 | copyFile(sourceFile, destFile, sourceFile.length());
65 | }
66 |
67 | /**
68 | * Returns the format of an image which is read through the {@link InputStream}.
69 | *
70 | * @param is The {@link InputStream} to an image.
71 | * @return File format of the image.
72 | * @throws IOException
73 | */
74 | public static String getFormatName(InputStream is) throws IOException {
75 | return ImageIO.getImageReaders(
76 | ImageIO.createImageInputStream(is)
77 | ).next().getFormatName();
78 | }
79 |
80 | public static URL getResource(String resourceName) throws IOException {
81 | URL url = ClassLoader.getSystemClassLoader().getResource(resourceName);
82 | if (url == null) {
83 | throw new IOException("Resource not found: " + resourceName);
84 | }
85 | return url;
86 | }
87 |
88 | public static InputStream getResourceStream(String resourceName) throws IOException {
89 | InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream(resourceName);
90 | if (is == null) {
91 | throw new IOException("Resource not found: " + resourceName);
92 | }
93 | return is;
94 | }
95 |
96 | public static File copyResourceToFile(String resourceName, File destination) throws IOException {
97 | InputStream is = getResourceStream(resourceName);
98 | FileOutputStream fos = new FileOutputStream(destination);
99 |
100 | byte[] buffer;
101 | int bytesAvailable;
102 | while ((bytesAvailable = is.available()) != 0) {
103 | buffer = new byte[bytesAvailable];
104 | int bytesRead = is.read(buffer, 0, buffer.length);
105 | fos.write(buffer, 0, bytesRead);
106 | }
107 | is.close();
108 | fos.close();
109 |
110 | return destination;
111 | }
112 |
113 | public static File copyResourceToTemporaryFile(String resourceName, TemporaryFolder folder) throws IOException {
114 | String name;
115 | if (resourceName.contains("/")) {
116 | name = resourceName.substring(resourceName.lastIndexOf("/") + 1);
117 | } else {
118 | name = resourceName;
119 | }
120 | File destination = folder.newFile(name);
121 |
122 | return copyResourceToFile(resourceName, destination);
123 | }
124 |
125 | public static File copyResourceToTemporaryFile(String resourceName, String namedAs, TemporaryFolder folder) throws IOException {
126 | return copyResourceToFile(resourceName, folder.newFile(namedAs));
127 | }
128 |
129 | public static BufferedImage getImageFromResource(String resourceName) throws IOException {
130 | InputStream is = getResourceStream(resourceName);
131 | try {
132 | return ImageIO.read(is);
133 | } finally {
134 | is.close();
135 | }
136 | }
137 | }
138 |
--------------------------------------------------------------------------------
/src/test/java/net/coobird/thumbnailator/filters/CaptionTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Thumbnailator - a thumbnail generation library
3 | *
4 | * Copyright (c) 2008-2020 Chris Kroells
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | package net.coobird.thumbnailator.filters;
26 |
27 | import static net.coobird.thumbnailator.filters.ImageFilterTestUtils.assertImageTypeRetained;
28 | import static org.junit.Assert.*;
29 |
30 | import java.awt.Color;
31 | import java.awt.Font;
32 | import java.awt.image.BufferedImage;
33 |
34 | import net.coobird.thumbnailator.geometry.Position;
35 | import net.coobird.thumbnailator.geometry.Positions;
36 | import net.coobird.thumbnailator.test.BufferedImageComparer;
37 | import net.coobird.thumbnailator.util.BufferedImages;
38 |
39 | import org.junit.Test;
40 |
41 | /**
42 | * Tests for the {@link Caption} filter.
43 | *
44 | * @author coobird
45 | *
46 | */
47 | public class CaptionTest {
48 |
49 | private static final String DEFAULT_CAPTION = "hello";
50 | private static final Font DEFAULT_FONT = new Font("Monospaced", Font.PLAIN, 14);
51 | private static final Color DEFAULT_COLOR = Color.black;
52 | private static final Position DEFAULT_POSITION = Positions.BOTTOM_CENTER;
53 |
54 | /**
55 | * Checks that the input image contents are not altered.
56 | */
57 | @Test
58 | public void inputContentsAreNotAltered() {
59 | // given
60 | BufferedImage originalImage = new BufferedImage(200, 200, BufferedImage.TYPE_INT_ARGB);
61 | BufferedImage copyImage = BufferedImages.copy(originalImage);
62 |
63 | ImageFilter filter = new Caption(
64 | DEFAULT_CAPTION,
65 | DEFAULT_FONT,
66 | DEFAULT_COLOR,
67 | DEFAULT_POSITION,
68 | 0
69 | );
70 |
71 | // when
72 | filter.apply(originalImage);
73 |
74 | // then
75 | assertTrue(BufferedImageComparer.isSame(originalImage, copyImage));
76 | }
77 |
78 | @Test
79 | public void imageTypeForInputAndOutputIsTheSame() {
80 | ImageFilter filter = new Caption(
81 | DEFAULT_CAPTION,
82 | DEFAULT_FONT,
83 | DEFAULT_COLOR,
84 | DEFAULT_POSITION,
85 | 0
86 | );
87 |
88 | assertImageTypeRetained(filter);
89 | }
90 |
91 | @Test(expected = NullPointerException.class)
92 | public void constructorNullCheckForCaption() {
93 | new Caption(
94 | null,
95 | DEFAULT_FONT,
96 | DEFAULT_COLOR,
97 | DEFAULT_POSITION,
98 | 0
99 | );
100 | }
101 |
102 | @Test(expected = NullPointerException.class)
103 | public void constructorNullCheckForFont() {
104 | new Caption(
105 | DEFAULT_CAPTION,
106 | null,
107 | DEFAULT_COLOR,
108 | DEFAULT_POSITION,
109 | 0
110 | );
111 | }
112 |
113 | @Test(expected = NullPointerException.class)
114 | public void constructorNullCheckForColor() {
115 | new Caption(
116 | DEFAULT_CAPTION,
117 | DEFAULT_FONT,
118 | null,
119 | DEFAULT_POSITION,
120 | 0
121 | );
122 | }
123 |
124 | @Test(expected = NullPointerException.class)
125 | public void constructorNullCheckForPosition() {
126 | new Caption(
127 | DEFAULT_CAPTION,
128 | DEFAULT_FONT,
129 | DEFAULT_COLOR,
130 | null,
131 | 0
132 | );
133 | }
134 |
135 | @Test
136 | public void constructorAllowsPositiveInsets() {
137 | new Caption(
138 | DEFAULT_CAPTION,
139 | DEFAULT_FONT,
140 | DEFAULT_COLOR,
141 | DEFAULT_POSITION,
142 | 10
143 | );
144 | }
145 |
146 | @Test(expected = IllegalArgumentException.class)
147 | public void constructorRejectsNegativeInsets() {
148 | new Caption(
149 | DEFAULT_CAPTION,
150 | DEFAULT_FONT,
151 | DEFAULT_COLOR,
152 | DEFAULT_POSITION,
153 | -1
154 | );
155 | }
156 | }
157 |
--------------------------------------------------------------------------------
/src/test/java/net/coobird/thumbnailator/filters/ColorizeTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Thumbnailator - a thumbnail generation library
3 | *
4 | * Copyright (c) 2008-2020 Chris Kroells
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | package net.coobird.thumbnailator.filters;
26 |
27 | import static net.coobird.thumbnailator.filters.ImageFilterTestUtils.assertImageTypeRetained;
28 | import static org.junit.Assert.*;
29 |
30 | import java.awt.Color;
31 | import java.awt.image.BufferedImage;
32 |
33 | import net.coobird.thumbnailator.test.BufferedImageComparer;
34 | import net.coobird.thumbnailator.util.BufferedImages;
35 |
36 | import org.junit.Test;
37 |
38 | /**
39 | * Tests for the {@link Colorize} filter.
40 | *
41 | * @author coobird
42 | *
43 | */
44 | public class ColorizeTest {
45 |
46 | /**
47 | * Checks that the input image contents are not altered.
48 | */
49 | @Test
50 | public void inputContentsAreNotAltered() {
51 | // given
52 | BufferedImage originalImage = new BufferedImage(200, 200, BufferedImage.TYPE_INT_ARGB);
53 | BufferedImage copyImage = BufferedImages.copy(originalImage);
54 |
55 | ImageFilter filter = new Colorize(Color.blue);
56 |
57 | // when
58 | filter.apply(originalImage);
59 |
60 | // then
61 | assertTrue(BufferedImageComparer.isSame(originalImage, copyImage));
62 | }
63 |
64 | /**
65 | * Checks that the input image contents are not altered for constructor with alpha.
66 | */
67 | @Test
68 | public void inputContentsAreNotAltered_alphaConstructor() {
69 | // given
70 | BufferedImage originalImage = new BufferedImage(200, 200, BufferedImage.TYPE_INT_ARGB);
71 | BufferedImage copyImage = BufferedImages.copy(originalImage);
72 |
73 | ImageFilter filter = new Colorize(Color.blue, 0.5f);
74 |
75 | // when
76 | filter.apply(originalImage);
77 |
78 | // then
79 | assertTrue(BufferedImageComparer.isSame(originalImage, copyImage));
80 | }
81 |
82 | @Test
83 | public void imageTypeForInputAndOutputIsTheSame() {
84 | assertImageTypeRetained(new Colorize(Color.blue));
85 | }
86 |
87 | @Test
88 | public void imageTypeForInputAndOutputIsTheSame_alphaConstructor() {
89 | assertImageTypeRetained(new Colorize(Color.blue, 0.5f));
90 | }
91 | }
92 |
--------------------------------------------------------------------------------
/src/test/java/net/coobird/thumbnailator/filters/FlipTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Thumbnailator - a thumbnail generation library
3 | *
4 | * Copyright (c) 2008-2022 Chris Kroells
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | package net.coobird.thumbnailator.filters;
26 |
27 | import static net.coobird.thumbnailator.TestUtils.getImageFromResource;
28 | import static net.coobird.thumbnailator.filters.ImageFilterTestUtils.assertImageTypeRetained;
29 | import static org.junit.Assert.assertTrue;
30 |
31 | import java.awt.image.BufferedImage;
32 |
33 | import net.coobird.thumbnailator.test.BufferedImageAssert;
34 | import net.coobird.thumbnailator.test.BufferedImageComparer;
35 | import net.coobird.thumbnailator.util.BufferedImages;
36 |
37 | import org.junit.Test;
38 |
39 | public class FlipTest {
40 |
41 | @Test
42 | public void flipHorizontal() throws Exception {
43 | // given
44 | BufferedImage img = getImageFromResource("Exif/original.png");
45 |
46 | // when
47 | BufferedImage result = Flip.HORIZONTAL.apply(img);
48 |
49 | // then
50 | BufferedImageAssert.assertMatches(
51 | result,
52 | new float[] {
53 | 1, 1, 1,
54 | 1, 1, 1,
55 | 0, 0, 1,
56 | }
57 | );
58 | }
59 |
60 | @Test
61 | public void flipVertical() throws Exception {
62 | // given
63 | BufferedImage img = getImageFromResource("Exif/original.png");
64 |
65 | // when
66 | BufferedImage result = Flip.VERTICAL.apply(img);
67 |
68 | // then
69 | BufferedImageAssert.assertMatches(
70 | result,
71 | new float[] {
72 | 1, 0, 0,
73 | 1, 1, 1,
74 | 1, 1, 1,
75 | }
76 | );
77 | }
78 |
79 | /**
80 | * Checks that the input image contents are not altered, when using the
81 | * {@link Flip#HORIZONTAL}.
82 | */
83 | @Test
84 | public void inputContentsAreNotAltered_UsingFlipHorizontal() {
85 | // given
86 | BufferedImage originalImage = new BufferedImage(200, 200, BufferedImage.TYPE_INT_ARGB);
87 | BufferedImage copyImage = BufferedImages.copy(originalImage);
88 |
89 | ImageFilter filter = Flip.HORIZONTAL;
90 |
91 | // when
92 | filter.apply(originalImage);
93 |
94 | // then
95 | assertTrue(BufferedImageComparer.isSame(originalImage, copyImage));
96 | }
97 |
98 | /**
99 | * Checks that the input image contents are not altered, when using the
100 | * {@link Flip#VERTICAL}.
101 | */
102 | @Test
103 | public void inputContentsAreNotAltered_UsingFlipVertical() {
104 | // given
105 | BufferedImage originalImage = new BufferedImage(200, 200, BufferedImage.TYPE_INT_ARGB);
106 | BufferedImage copyImage = BufferedImages.copy(originalImage);
107 |
108 | ImageFilter filter = Flip.VERTICAL;
109 |
110 | // when
111 | filter.apply(originalImage);
112 |
113 | // then
114 | assertTrue(BufferedImageComparer.isSame(originalImage, copyImage));
115 | }
116 |
117 | @Test
118 | public void imageTypeForInputAndOutputIsTheSame_UsingFlipHorizontal() {
119 | assertImageTypeRetained(Flip.HORIZONTAL);
120 | }
121 |
122 | @Test
123 | public void imageTypeForInputAndOutputIsTheSame_UsingFlipVertical() {
124 | assertImageTypeRetained(Flip.VERTICAL);
125 | }
126 | }
127 |
--------------------------------------------------------------------------------
/src/test/java/net/coobird/thumbnailator/filters/ImageFilterTestUtils.java:
--------------------------------------------------------------------------------
1 | package net.coobird.thumbnailator.filters;
2 |
3 | import java.awt.image.BufferedImage;
4 |
5 | import static org.junit.Assert.assertEquals;
6 |
7 | public class ImageFilterTestUtils {
8 | private static final int[] IMAGE_TYPES = new int[] {
9 | BufferedImage.TYPE_INT_ARGB,
10 | BufferedImage.TYPE_INT_RGB,
11 | BufferedImage.TYPE_BYTE_GRAY,
12 | BufferedImage.TYPE_BYTE_INDEXED,
13 | BufferedImage.TYPE_3BYTE_BGR,
14 | BufferedImage.TYPE_4BYTE_ABGR,
15 | BufferedImage.TYPE_4BYTE_ABGR_PRE,
16 | BufferedImage.TYPE_BYTE_BINARY,
17 | BufferedImage.TYPE_INT_ARGB_PRE,
18 | BufferedImage.TYPE_INT_BGR,
19 | BufferedImage.TYPE_USHORT_555_RGB,
20 | BufferedImage.TYPE_USHORT_565_RGB,
21 | BufferedImage.TYPE_USHORT_GRAY
22 | };
23 |
24 | public static void assertImageTypeRetained(ImageFilter filter) {
25 | for (int bufferedImageType : IMAGE_TYPES) {
26 | // given
27 | BufferedImage originalImage = new BufferedImage(200, 200, bufferedImageType);
28 |
29 | // when
30 | BufferedImage finalImage = filter.apply(originalImage);
31 |
32 | // then
33 | assertEquals(originalImage.getType(), finalImage.getType());
34 | }
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/src/test/java/net/coobird/thumbnailator/filters/TransparencyTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Thumbnailator - a thumbnail generation library
3 | *
4 | * Copyright (c) 2008-2020 Chris Kroells
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | package net.coobird.thumbnailator.filters;
26 |
27 | import static net.coobird.thumbnailator.filters.ImageFilterTestUtils.assertImageTypeRetained;
28 | import static org.junit.Assert.*;
29 |
30 | import java.awt.image.BufferedImage;
31 |
32 | import net.coobird.thumbnailator.test.BufferedImageComparer;
33 | import net.coobird.thumbnailator.util.BufferedImages;
34 |
35 | import org.junit.Test;
36 |
37 | /**
38 | * Tests for the {@link Transparency} filter.
39 | *
40 | * @author coobird
41 | *
42 | */
43 | public class TransparencyTest {
44 |
45 | /**
46 | * Checks that the input image contents are not altered, when using the
47 | * {@link Transparency#Transparency(double)} constructor.
48 | */
49 | @Test
50 | public void inputContentsAreNotAltered_DoubleConstructor() {
51 | // given
52 | BufferedImage originalImage = new BufferedImage(200, 200, BufferedImage.TYPE_INT_ARGB);
53 | BufferedImage copyImage = BufferedImages.copy(originalImage);
54 |
55 | ImageFilter filter = new Transparency(0.5);
56 |
57 | // when
58 | filter.apply(originalImage);
59 |
60 | // then
61 | assertTrue(BufferedImageComparer.isSame(originalImage, copyImage));
62 | }
63 |
64 | /**
65 | * Checks that the input image contents are not altered, when using the
66 | * {@link Transparency#Transparency(float)} constructor.
67 | */
68 | @Test
69 | public void inputContentsAreNotAltered_FloatConstructor() {
70 | // given
71 | BufferedImage originalImage = new BufferedImage(200, 200, BufferedImage.TYPE_INT_ARGB);
72 | BufferedImage copyImage = BufferedImages.copy(originalImage);
73 |
74 | ImageFilter filter = new Transparency(0.5f);
75 |
76 | // when
77 | filter.apply(originalImage);
78 |
79 | // then
80 | assertTrue(BufferedImageComparer.isSame(originalImage, copyImage));
81 | }
82 |
83 | @Test
84 | public void imageTypeForInputAndOutputIsTheSame_DoubleConstructor() {
85 | assertImageTypeRetained(new Transparency(0.5));
86 | }
87 |
88 | @Test
89 | public void imageTypeForInputAndOutputIsTheSame_FloatConstructor() {
90 | assertImageTypeRetained(new Transparency(0.5f));
91 | }
92 | }
93 |
--------------------------------------------------------------------------------
/src/test/java/net/coobird/thumbnailator/resizers/NullResizerTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Thumbnailator - a thumbnail generation library
3 | *
4 | * Copyright (c) 2008-2022 Chris Kroells
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | package net.coobird.thumbnailator.resizers;
26 |
27 | import java.awt.image.BufferedImage;
28 | import java.io.IOException;
29 |
30 | import net.coobird.thumbnailator.test.BufferedImageComparer;
31 |
32 | import org.junit.Test;
33 |
34 | import static net.coobird.thumbnailator.TestUtils.getImageFromResource;
35 | import static org.junit.Assert.*;
36 |
37 | public class NullResizerTest {
38 |
39 | @Test
40 | public void sourceAndDestSameDimension() throws IOException {
41 | // given
42 | BufferedImage srcImage = getImageFromResource("Thumbnailator/grid.png");
43 | BufferedImage destImage = new BufferedImage(100, 100, BufferedImage.TYPE_INT_ARGB);
44 |
45 | // when
46 | new NullResizer().resize(srcImage, destImage);
47 |
48 | // then
49 | assertEquals(srcImage.getWidth(), destImage.getWidth());
50 | assertEquals(srcImage.getHeight(), destImage.getHeight());
51 | assertTrue(BufferedImageComparer.isRGBSimilar(srcImage, destImage));
52 | }
53 |
54 | @Test
55 | public void sourceSmallerThanDest() throws IOException {
56 | // given
57 | BufferedImage srcImage = getImageFromResource("Thumbnailator/grid.png");
58 | BufferedImage destImage = new BufferedImage(200, 200, BufferedImage.TYPE_INT_ARGB);
59 |
60 | // when
61 | new NullResizer().resize(srcImage, destImage);
62 |
63 | // then
64 | assertTrue(BufferedImageComparer.isRGBSimilar(srcImage, destImage.getSubimage(0, 0, 100, 100)));
65 | }
66 |
67 | @Test
68 | public void sourceLargerThanDest() throws IOException {
69 | // given
70 | BufferedImage srcImage = getImageFromResource("Thumbnailator/grid.png");
71 | BufferedImage destImage = new BufferedImage(50, 50, BufferedImage.TYPE_INT_ARGB);
72 |
73 | // when
74 | new NullResizer().resize(srcImage, destImage);
75 |
76 | // then
77 | assertTrue(BufferedImageComparer.isRGBSimilar(srcImage.getSubimage(0, 0, 50, 50), destImage));
78 | }
79 |
80 | @Test
81 | public void resizeNullAndNull() {
82 | // given
83 | BufferedImage srcImage = null;
84 | BufferedImage destImage = null;
85 |
86 | try {
87 | // when
88 | new NullResizer().resize(srcImage, destImage);
89 | fail();
90 | } catch (Exception e) {
91 | // then
92 | assertEquals("The source and/or destination image is null.", e.getMessage());
93 | }
94 | }
95 |
96 | @Test
97 | public void resizeSpecifiedAndNull() {
98 | // given
99 | BufferedImage srcImage = new BufferedImage(100, 100, BufferedImage.TYPE_INT_ARGB);
100 | BufferedImage destImage = null;
101 |
102 | try {
103 | // when
104 | new NullResizer().resize(srcImage, destImage);
105 | fail();
106 | } catch (Exception e) {
107 | // then
108 | assertEquals("The source and/or destination image is null.", e.getMessage());
109 | }
110 | }
111 |
112 | @Test
113 | public void resizeNullAndSpecified() {
114 | // given
115 | BufferedImage srcImage = null;
116 | BufferedImage destImage = new BufferedImage(100, 100, BufferedImage.TYPE_INT_ARGB);
117 |
118 | try {
119 | // when
120 | new NullResizer().resize(srcImage, destImage);
121 | fail();
122 | } catch (Exception e) {
123 | // then
124 | assertEquals("The source and/or destination image is null.", e.getMessage());
125 | }
126 | }
127 | }
128 |
--------------------------------------------------------------------------------
/src/test/java/net/coobird/thumbnailator/resizers/ProgressiveBilinearResizerTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Thumbnailator - a thumbnail generation library
3 | *
4 | * Copyright (c) 2008-2020 Chris Kroells
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | package net.coobird.thumbnailator.resizers;
26 |
27 | import static org.junit.Assert.*;
28 |
29 | import java.awt.image.BufferedImage;
30 | import java.io.IOException;
31 |
32 | import org.junit.Test;
33 |
34 | public class ProgressiveBilinearResizerTest {
35 |
36 | /**
37 | * Test for
38 | * {@link ProgressiveBilinearResizer#resize(BufferedImage, BufferedImage)}
39 | * where,
40 | *
41 | * 1) source image is null.
42 | * 2) destination image is null.
43 | *
44 | * Expected outcome is,
45 | *
46 | * 1) Processing will stop with an NullPointerException.
47 | *
48 | * @throws IOException
49 | */
50 | @Test(expected=NullPointerException.class)
51 | public void resizeNullAndNull() {
52 | BufferedImage srcImage = null;
53 | BufferedImage destImage = null;
54 |
55 | new ProgressiveBilinearResizer().resize(srcImage, destImage);
56 |
57 | fail();
58 | }
59 |
60 | /**
61 | * Test for
62 | * {@link ProgressiveBilinearResizer#resize(BufferedImage, BufferedImage)}
63 | * where,
64 | *
65 | * 1) source image is specified.
66 | * 2) destination image is null.
67 | *
68 | * Expected outcome is,
69 | *
70 | * 1) Processing will stop with an NullPointerException.
71 | *
72 | * @throws IOException
73 | */
74 | @Test(expected=NullPointerException.class)
75 | public void resizeSpecifiedAndNull() {
76 | BufferedImage srcImage = new BufferedImage(100, 100, BufferedImage.TYPE_INT_ARGB);
77 | BufferedImage destImage = null;
78 |
79 | new ProgressiveBilinearResizer().resize(srcImage, destImage);
80 |
81 | fail();
82 | }
83 |
84 | /**
85 | * Test for
86 | * {@link ProgressiveBilinearResizer#resize(BufferedImage, BufferedImage)}
87 | * where,
88 | *
89 | * 1) source image is null.
90 | * 2) destination image is specified.
91 | *
92 | * Expected outcome is,
93 | *
94 | * 1) Processing will stop with an NullPointerException.
95 | *
96 | * @throws IOException
97 | */
98 | @Test(expected=NullPointerException.class)
99 | public void resizeNullAndSpecified() {
100 | BufferedImage srcImage = null;
101 | BufferedImage destImage = new BufferedImage(100, 100, BufferedImage.TYPE_INT_ARGB);
102 |
103 | new ProgressiveBilinearResizer().resize(srcImage, destImage);
104 |
105 | fail();
106 | }
107 |
108 | }
109 |
--------------------------------------------------------------------------------
/src/test/java/net/coobird/thumbnailator/tasks/io/BufferedImageSinkTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Thumbnailator - a thumbnail generation library
3 | *
4 | * Copyright (c) 2008-2020 Chris Kroells
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | package net.coobird.thumbnailator.tasks.io;
26 |
27 | import java.awt.image.BufferedImage;
28 | import java.io.IOException;
29 |
30 | import org.junit.Test;
31 | import static org.junit.Assert.*;
32 |
33 | public class BufferedImageSinkTest {
34 | @Test
35 | public void writeImage() throws IOException {
36 | // given
37 | BufferedImage img =
38 | new BufferedImage(10, 10, BufferedImage.TYPE_INT_ARGB);
39 |
40 | // when
41 | BufferedImageSink sink = new BufferedImageSink();
42 | sink.write(img);
43 |
44 | // then
45 | assertSame(img, sink.getSink());
46 | }
47 |
48 | @Test(expected=NullPointerException.class)
49 | public void writeNull() throws IOException {
50 | // given
51 | BufferedImage img = null;
52 |
53 | try {
54 | // when
55 | new BufferedImageSink().write(img);
56 | fail();
57 | } catch (NullPointerException e) {
58 | // then
59 | assertEquals("Cannot write a null image.", e.getMessage());
60 | throw e;
61 | }
62 | }
63 |
64 | @Test(expected=IllegalStateException.class)
65 | public void getSink_BeforeWrite() throws IOException {
66 | // given
67 |
68 | try {
69 | // when
70 | new BufferedImageSink().getSink();
71 | fail();
72 | } catch (IllegalStateException e) {
73 | // then
74 | assertEquals("BufferedImageSink has not been written to yet.", e.getMessage());
75 | throw e;
76 | }
77 | }
78 |
79 | @Test
80 | public void setOutputFormatName_DoesntAffectAnything() throws IOException {
81 | // given
82 | BufferedImageSink sink0 = new BufferedImageSink();
83 | BufferedImageSink sink1 = new BufferedImageSink();
84 |
85 | BufferedImage img =
86 | new BufferedImage(10, 10, BufferedImage.TYPE_INT_ARGB);
87 |
88 | // when
89 | sink0.setOutputFormatName("PNG");
90 |
91 | sink0.write(img);
92 | sink1.write(img);
93 |
94 | // then
95 | assertSame(img, sink0.getSink());
96 | assertSame(img, sink1.getSink());
97 | }
98 | }
99 |
--------------------------------------------------------------------------------
/src/test/java/net/coobird/thumbnailator/tasks/io/InputStreamImageSourceMalformedTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Thumbnailator - a thumbnail generation library
3 | *
4 | * Copyright (c) 2008-2021 Chris Kroells
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | package net.coobird.thumbnailator.tasks.io;
26 |
27 | import org.junit.After;
28 | import org.junit.Before;
29 | import org.junit.Test;
30 | import org.junit.runner.RunWith;
31 | import org.junit.runners.Parameterized;
32 |
33 | import java.io.ByteArrayInputStream;
34 | import java.io.InputStream;
35 | import java.util.ArrayList;
36 | import java.util.Arrays;
37 | import java.util.Collection;
38 | import java.util.List;
39 |
40 | @RunWith(Parameterized.class)
41 | public class InputStreamImageSourceMalformedTest {
42 |
43 | @Parameterized.Parameters(name = "type={0}, length={1}")
44 | public static Collection