X-Git-Url: http://git.nikiroo.be/?p=fanfix.git;a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Futils%2FImageUtils.java;h=877c8fa8c26b6b8a1a585505cf70f4a37d3d4fc7;hp=37d73191a52307641ef67f48bcd3f839786607f2;hb=HEAD;hpb=805005449dacb1e7b825db63836bf100e472ddd0 diff --git a/src/be/nikiroo/utils/ImageUtils.java b/src/be/nikiroo/utils/ImageUtils.java index 37d7319..877c8fa 100644 --- a/src/be/nikiroo/utils/ImageUtils.java +++ b/src/be/nikiroo/utils/ImageUtils.java @@ -41,6 +41,52 @@ public abstract class ImageUtils { public abstract void saveAsImage(Image img, File target, String format) throws IOException; + /** + * Scale a dimension. + * + * + * @param imageWidth + * the actual image width + * @param imageHeight + * the actual image height + * @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 zoom + * the zoom factor (ignored on snap mode) + * @param snapMode + * NULL for no snap mode, TRUE to snap to width and FALSE for + * snap to height) + * + * @return the scaled size, width is [0] and height is [1] (minimum is 1x1) + */ + protected static Integer[] scaleSize(int imageWidth, int imageHeight, + int areaWidth, int areaHeight, double zoom, Boolean snapMode) { + int width; + int height; + if (snapMode == null) { + width = (int) Math.round(imageWidth * zoom); + height = (int) Math.round(imageHeight * zoom); + } else if (snapMode) { + 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,16 +236,28 @@ 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}. * * @return the {@link ImageUtils} */ private static ImageUtils newObject() { - for (String clazz : new String[] { "be.nikiroo.utils.ui.ImageUtilsAwt" }) { + 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) { } }