[kune-commits] r908 - in trunk: . debian src/main/java/org/ourproject/kune/platf/server/manager/impl src/test/java/org/ourproject/kune/platf/server/manager/impl

vjrj vjrj at ourproject.org
Tue Oct 14 15:35:45 CEST 2008


Author: vjrj
Date: 2008-10-14 15:25:31 +0200 (Tue, 14 Oct 2008)
New Revision: 908

Added:
   trunk/src/main/java/org/ourproject/kune/platf/server/manager/impl/ImageUtilsDefault.java
   trunk/src/test/java/org/ourproject/kune/platf/server/manager/impl/ImageUtilsDefaultTest.java
   trunk/src/test/java/org/ourproject/kune/platf/server/manager/impl/orig.gif
   trunk/src/test/java/org/ourproject/kune/platf/server/manager/impl/orig.jpg
   trunk/src/test/java/org/ourproject/kune/platf/server/manager/impl/orig.png
   trunk/src/test/java/org/ourproject/kune/platf/server/manager/impl/orig.tiff
Modified:
   trunk/debian/TODO
   trunk/pom.xml
Log:
Complete - task Images server utilities 


Modified: trunk/debian/TODO
===================================================================
--- trunk/debian/TODO	2008-10-14 00:23:09 UTC (rev 907)
+++ trunk/debian/TODO	2008-10-14 13:25:31 UTC (rev 908)
@@ -1,3 +1,4 @@
 - create user kune
 - create /var/lib/kune/uploads and give permissions
 - create /var/lib/kune/lucene 
+- deps: libjmagick6-jni

Modified: trunk/pom.xml
===================================================================
--- trunk/pom.xml	2008-10-14 00:23:09 UTC (rev 907)
+++ trunk/pom.xml	2008-10-14 13:25:31 UTC (rev 908)
@@ -87,7 +87,7 @@
 <dependency>
   <groupId>jmagick</groupId>
   <artifactId>jmagick</artifactId>
-  <version>6.2.4</version>
+  <version>6.2.6-0</version>
 </dependency>
 
     <!-- xmpp -->

Added: trunk/src/main/java/org/ourproject/kune/platf/server/manager/impl/ImageUtilsDefault.java
===================================================================
--- trunk/src/main/java/org/ourproject/kune/platf/server/manager/impl/ImageUtilsDefault.java	2008-10-14 00:23:09 UTC (rev 907)
+++ trunk/src/main/java/org/ourproject/kune/platf/server/manager/impl/ImageUtilsDefault.java	2008-10-14 13:25:31 UTC (rev 908)
@@ -0,0 +1,105 @@
+package org.ourproject.kune.platf.server.manager.impl;
+
+import java.awt.Dimension;
+import java.awt.Rectangle;
+
+import magick.ImageInfo;
+import magick.MagickException;
+import magick.MagickImage;
+
+public class ImageUtilsDefault {
+
+    /**
+     * http://en.wikipedia.org/wiki/Thumbnail
+     */
+    public static void createThumb(String fileOrig, String fileDest, int thumbDimension, int cropDimension)
+            throws MagickException {
+        if (thumbDimension < cropDimension) {
+            throw new IndexOutOfBoundsException("Thumb dimension must be bigger than crop dimension");
+        }
+        MagickImage imageOrig = createImage(fileOrig);
+        Dimension origDimension = imageOrig.getDimension();
+        int origHeight = origDimension.height;
+        int origWidth = origDimension.width;
+        Dimension proportionalDim = calculateProportionalDim(origWidth, origHeight, thumbDimension);
+        int x = calculateCenteredCoordinate(proportionalDim.width, cropDimension);
+        int y = calculateCenteredCoordinate(proportionalDim.height, cropDimension);
+        MagickImage scaled = scaleImage(imageOrig, proportionalDim.width, proportionalDim.height);
+        cropImage(scaled, fileDest, x, y, cropDimension, cropDimension);
+    }
+
+    public static boolean cropImage(String fileOrig, String fileDest, int x, int y, int width, int height)
+            throws MagickException {
+        Rectangle rectangle = new Rectangle(x, y, width, height);
+        return cropImage(fileOrig, fileDest, rectangle);
+    }
+
+    public static boolean cropImage(String fileOrig, String fileDest, Rectangle rectangle) throws MagickException {
+        return cropImage(createImage(fileOrig), fileDest, rectangle);
+    }
+
+    public static Dimension getDimension(String file) throws MagickException {
+        MagickImage imageOrig = createImage(file);
+        return imageOrig.getDimension();
+    }
+
+    public static boolean scaleImage(String fileOrig, String fileDest, Dimension dimension) throws MagickException {
+        MagickImage imageOrig = createImage(fileOrig);
+        return scaleImage(imageOrig, fileDest, (int) dimension.getWidth(), (int) dimension.getHeight());
+    }
+
+    public static boolean scaleImage(String fileOrig, String fileDest, int width, int height) throws MagickException {
+        MagickImage imageOrig = createImage(fileOrig);
+        return scaleImage(imageOrig, fileDest, width, height);
+    }
+
+    static int calculateCenteredCoordinate(int size, int crop) {
+        int i = (size - crop) / 2;
+        return i < 0 ? 0 : i;
+    }
+
+    static Dimension calculateProportionalDim(int origWidth, int origHeight, int maxSize) {
+        boolean higher = origHeight > origWidth;
+        double height = higher ? (origHeight * maxSize / origWidth) : maxSize;
+        double width = !higher ? (origWidth * maxSize / origHeight) : maxSize;
+        if ((higher && origHeight <= maxSize) || (!higher && origWidth <= maxSize)) {
+            return new Dimension(origWidth, origHeight);
+        }
+        return new Dimension((int) width, (int) height);
+    }
+
+    private static ImageInfo createEmptyImageInfo() throws MagickException {
+        ImageInfo info = new ImageInfo();
+        return info;
+    }
+
+    private static MagickImage createImage(String file) throws MagickException {
+        return new MagickImage(new ImageInfo(file));
+    }
+
+    private static boolean cropImage(MagickImage fileOrig, String fileDest, int x, int y, int width, int height)
+            throws MagickException {
+        Rectangle rectangle = new Rectangle(x, y, width, height);
+        return cropImage(fileOrig, fileDest, rectangle);
+    }
+
+    private static boolean cropImage(MagickImage fileOrig, String fileDest, Rectangle rectangle) throws MagickException {
+        MagickImage cropped = fileOrig.cropImage(rectangle);
+        return writeImage(cropped, fileDest);
+    }
+
+    private static MagickImage scaleImage(MagickImage imageOrig, int width, int height) throws MagickException {
+        return imageOrig.scaleImage(width, height);
+    }
+
+    private static boolean scaleImage(MagickImage imageOrig, String fileDest, int width, int height)
+            throws MagickException {
+        MagickImage imageDest = scaleImage(imageOrig, width, height);
+        return writeImage(imageDest, fileDest);
+    }
+
+    private static boolean writeImage(MagickImage imageDest, String fileDest) throws MagickException {
+        imageDest.setFileName(fileDest);
+        return imageDest.writeImage(createEmptyImageInfo());
+    }
+}

Added: trunk/src/test/java/org/ourproject/kune/platf/server/manager/impl/ImageUtilsDefaultTest.java
===================================================================
--- trunk/src/test/java/org/ourproject/kune/platf/server/manager/impl/ImageUtilsDefaultTest.java	2008-10-14 00:23:09 UTC (rev 907)
+++ trunk/src/test/java/org/ourproject/kune/platf/server/manager/impl/ImageUtilsDefaultTest.java	2008-10-14 13:25:31 UTC (rev 908)
@@ -0,0 +1,143 @@
+package org.ourproject.kune.platf.server.manager.impl;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import java.awt.Dimension;
+import java.io.File;
+
+import magick.MagickException;
+
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+/**
+ * UnsatisfiedLinkError: problem with jmagick installation (in debian, apt-get
+ * install libjmagick6-jni, and add LD_LIBRARY_PATH=/usr/lib/jni/ to this test
+ * environment params
+ */
+public class ImageUtilsDefaultTest {
+
+    private static final String IMG_PATH = "src/test/java/org/ourproject/kune/platf/server/manager/impl/";
+    private static String[] images = { "orig.png", "orig.gif", "orig.jpg", "orig.tiff" };
+    private static String imageDest;
+
+    @AfterClass
+    public static void after() {
+        File file = new File(imageDest);
+        file.delete();
+    }
+
+    @BeforeClass
+    public static void before() {
+        imageDest = IMG_PATH + "output";
+    }
+
+    @Test
+    public void generateIcon() throws MagickException {
+        for (String image : images) {
+            ImageUtilsDefault.createThumb(IMG_PATH + image, imageDest, 16, 16);
+            Dimension dimension = ImageUtilsDefault.getDimension(imageDest);
+            assertEquals(16, (int) dimension.getHeight());
+            assertEquals(16, (int) dimension.getWidth());
+        }
+    }
+
+    @Test
+    public void generateThumb() throws MagickException {
+        for (String image : images) {
+            ImageUtilsDefault.createThumb(IMG_PATH + image, imageDest, 100, 85);
+            Dimension dimension = ImageUtilsDefault.getDimension(imageDest);
+            assertEquals(85, (int) dimension.getHeight());
+            assertEquals(85, (int) dimension.getWidth());
+        }
+    }
+
+    @Test
+    public void testProportionalHigher() {
+        Dimension proportionalDim = ImageUtilsDefault.calculateProportionalDim(500, 1000, 100);
+        assertEquals(100, proportionalDim.width);
+        assertEquals(200, proportionalDim.height);
+        assertEquals(0, ImageUtilsDefault.calculateCenteredCoordinate(proportionalDim.width, 100));
+        assertEquals(50, ImageUtilsDefault.calculateCenteredCoordinate(proportionalDim.height, 100));
+    }
+
+    @Test
+    public void testProportionalHigherLikeSamples() {
+        Dimension proportionalDim = ImageUtilsDefault.calculateProportionalDim(1200, 1600, 100);
+        assertEquals(100, proportionalDim.width);
+        assertEquals(133, proportionalDim.height);
+        assertEquals(0, ImageUtilsDefault.calculateCenteredCoordinate(proportionalDim.width, 100));
+        assertEquals(16, ImageUtilsDefault.calculateCenteredCoordinate(proportionalDim.height, 100));
+    }
+
+    @Test
+    public void testProportionalHigherSame() {
+        Dimension proportionalDim = ImageUtilsDefault.calculateProportionalDim(20, 100, 100);
+        assertEquals(20, proportionalDim.width);
+        assertEquals(100, proportionalDim.height);
+        assertEquals(0, ImageUtilsDefault.calculateCenteredCoordinate(proportionalDim.width, 100));
+        assertEquals(0, ImageUtilsDefault.calculateCenteredCoordinate(proportionalDim.height, 100));
+    }
+
+    @Test
+    public void testProportionalHigherSmaller() {
+        Dimension proportionalDim = ImageUtilsDefault.calculateProportionalDim(20, 10, 100);
+        assertEquals(20, proportionalDim.width);
+        assertEquals(10, proportionalDim.height);
+        assertEquals(0, ImageUtilsDefault.calculateCenteredCoordinate(proportionalDim.width, 100));
+        assertEquals(0, ImageUtilsDefault.calculateCenteredCoordinate(proportionalDim.height, 100));
+    }
+
+    @Test
+    public void testProportionalWider() {
+        Dimension proportionalDim = ImageUtilsDefault.calculateProportionalDim(1000, 500, 100);
+        assertEquals(200, proportionalDim.width);
+        assertEquals(100, proportionalDim.height);
+        assertEquals(50, ImageUtilsDefault.calculateCenteredCoordinate(proportionalDim.width, 100));
+        assertEquals(0, ImageUtilsDefault.calculateCenteredCoordinate(proportionalDim.height, 100));
+    }
+
+    @Test
+    public void testProportionalWiderSame() {
+        Dimension proportionalDim = ImageUtilsDefault.calculateProportionalDim(100, 20, 100);
+        assertEquals(100, proportionalDim.width);
+        assertEquals(20, proportionalDim.height);
+        assertEquals(0, ImageUtilsDefault.calculateCenteredCoordinate(proportionalDim.width, 100));
+        assertEquals(0, ImageUtilsDefault.calculateCenteredCoordinate(proportionalDim.height, 100));
+    }
+
+    @Test
+    public void testProportionalWiderSmaller() {
+        Dimension proportionalDim = ImageUtilsDefault.calculateProportionalDim(5, 10, 100);
+        assertEquals(5, proportionalDim.width);
+        assertEquals(10, proportionalDim.height);
+        assertEquals(0, ImageUtilsDefault.calculateCenteredCoordinate(proportionalDim.width, 100));
+        assertEquals(0, ImageUtilsDefault.calculateCenteredCoordinate(proportionalDim.height, 100));
+    }
+
+    @Test
+    public void testResize() throws MagickException {
+        for (String image : images) {
+            assertTrue(ImageUtilsDefault.scaleImage(IMG_PATH + image, imageDest, 100, 100));
+            Dimension dimension = ImageUtilsDefault.getDimension(imageDest);
+            assertEquals(100, (int) dimension.getHeight());
+            assertEquals(100, (int) dimension.getWidth());
+        }
+    }
+
+    @Test
+    public void testSize() throws MagickException {
+        for (String image : images) {
+            Dimension dimension = ImageUtilsDefault.getDimension(IMG_PATH + image);
+            assertEquals(1600, (int) dimension.getHeight());
+            assertEquals(1200, (int) dimension.getWidth());
+        }
+    }
+
+    @Test(expected = IndexOutOfBoundsException.class)
+    public void thumbSmallerThanCropMustFail() throws MagickException {
+        ImageUtilsDefault.createThumb(IMG_PATH + images[0], imageDest, 100, 200);
+    }
+}

Added: trunk/src/test/java/org/ourproject/kune/platf/server/manager/impl/orig.gif
===================================================================
(Binary files differ)


Property changes on: trunk/src/test/java/org/ourproject/kune/platf/server/manager/impl/orig.gif
___________________________________________________________________
Name: svn:mime-type
   + application/octet-stream

Added: trunk/src/test/java/org/ourproject/kune/platf/server/manager/impl/orig.jpg
===================================================================
(Binary files differ)


Property changes on: trunk/src/test/java/org/ourproject/kune/platf/server/manager/impl/orig.jpg
___________________________________________________________________
Name: svn:mime-type
   + application/octet-stream

Added: trunk/src/test/java/org/ourproject/kune/platf/server/manager/impl/orig.png
===================================================================
(Binary files differ)


Property changes on: trunk/src/test/java/org/ourproject/kune/platf/server/manager/impl/orig.png
___________________________________________________________________
Name: svn:mime-type
   + application/octet-stream

Added: trunk/src/test/java/org/ourproject/kune/platf/server/manager/impl/orig.tiff
===================================================================
(Binary files differ)


Property changes on: trunk/src/test/java/org/ourproject/kune/platf/server/manager/impl/orig.tiff
___________________________________________________________________
Name: svn:mime-type
   + application/octet-stream




More information about the kune-commits mailing list