+ private static final long serialVersionUID = -4156533490858298387L;
+ /**
+ * Picture dimensions.
+ */
+ private Dimension dimensions;
+
+ public Picture(final File parentDirectory, final String fileName)
+ throws Exception {
+ super(parentDirectory, fileName);
+ }
+
+ private static void ensureNonzeroImageArea(final java.awt.Dimension result) {
+ if (result.width < 1)
+ result.width = 1;
+ if (result.height < 1)
+ result.height = 1;
+ }
+
+ /**
+ * Actual returned target thumbnail size will be adjusted from desired one
+ * by attempting to generate as large as possible thumbnail, while not
+ * exceeding provided thumbnail width and height, and preserving aspect
+ * ratio of original image.
+ */
+ private static java.awt.Dimension getTargetThumbnailDimension(
+ final java.awt.Dimension originalImageDimension,
+ final java.awt.Dimension desiredTargetDimension) {
+
+ final double thumbWidthToHeightRatio = (double) desiredTargetDimension.width
+ / (double) desiredTargetDimension.height;
+ final double inputImageWidthToHeightRatio = (double) originalImageDimension.width
+ / (double) originalImageDimension.height;
+
+ if (thumbWidthToHeightRatio < inputImageWidthToHeightRatio) {
+
+ final java.awt.Dimension result = new java.awt.Dimension(
+ desiredTargetDimension.width,
+ (int) (desiredTargetDimension.width / inputImageWidthToHeightRatio));
+
+ ensureNonzeroImageArea(result);
+
+ return result;
+ } else {
+
+ final java.awt.Dimension result = new java.awt.Dimension(
+ (int) (desiredTargetDimension.height * inputImageWidthToHeightRatio),
+ desiredTargetDimension.height);
+
+ ensureNonzeroImageArea(result);
+
+ return result;
+ }
+ }
+
+ /**
+ * Propose list of thumbnail dimensions.
+ */
+ public static List<Dimension> getThumbnailDimensions(Dimension current) {
+ final ArrayList<Dimension> result = new ArrayList<>();
+ result.add(current);
+
+ while (current.getArea() > 1000000) {
+ current = current.getScaled(0.5d);
+ result.add(current);
+ }
+
+ return result;
+
+ }