X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Futils%2FImageUtils.java;h=2b8ff8fa9ec722056904d310343761f979285924;hb=26a1f3fe044c616802367e8a0f2cd4f099413ceb;hp=cacff8d5bb3717696f633d2befaef893dbfbca4b;hpb=e8aa5bf9227a0d6a6d0bb6a8bc0cc04d0f4d601a;p=nikiroo-utils.git diff --git a/src/be/nikiroo/utils/ImageUtils.java b/src/be/nikiroo/utils/ImageUtils.java index cacff8d..2b8ff8f 100644 --- a/src/be/nikiroo/utils/ImageUtils.java +++ b/src/be/nikiroo/utils/ImageUtils.java @@ -41,6 +41,53 @@ public abstract class ImageUtils { public abstract void saveAsImage(Image img, File target, String format) throws IOException; + /** + * Scale a dimension. + * + * @param areaWidth + * the base width of the target dimension for snap sizes + * @param areaHeight + * the base height of the target dimension for snap sizes + * @param imageWidth + * the actual image width + * @param imageHeight + * the actual image height + * @param zoom + * the zoom factor, or -1 for snap size + * @param zoomSnapWidth + * if snap size, TRUE to snap to width (and FALSE, snap to + * height) + * + * @return the scaled size, width is [0] and height is [1] (minimum is 1x1) + */ + protected static Integer[] scaleSize(int areaWidth, int areaHeight, + int imageWidth, int imageHeight, double zoom, boolean zoomSnapWidth) { + int width; + int height; + if (zoom > 0) { + width = (int) Math.round(imageWidth * zoom); + height = (int) Math.round(imageHeight * zoom); + } else { + if (zoomSnapWidth) { + width = areaWidth; + height = (int) Math.round( + (((double) areaWidth) / imageWidth) * imageHeight); + } else { + height = areaHeight; + width = (int) Math.round( + (((double) areaHeight) / imageHeight) * imageWidth); + + } + } + + if (width < 1) + width = 1; + if (height < 1) + height = 1; + + return new Integer[] { width, height }; + } + /** * Return the EXIF transformation flag of this image if any. * @@ -190,6 +237,14 @@ public abstract class ImageUtils { return set_flag; } + /** + * Check that the class can operate (for instance, that all the required + * libraries or frameworks are present). + * + * @return TRUE if it works + */ + abstract protected boolean check(); + /** * Create a new {@link ImageUtils}. * @@ -199,8 +254,11 @@ public abstract class ImageUtils { for (String clazz : new String[] { "be.nikiroo.utils.ui.ImageUtilsAwt", "be.nikiroo.utils.android.ImageUtilsAndroid" }) { try { - return (ImageUtils) SerialUtils.createObject(clazz); - } catch (Exception e) { + ImageUtils obj = (ImageUtils) SerialUtils.createObject(clazz); + if (obj.check()) { + return obj; + } + } catch (Throwable e) { } }