/* J_LZ_COPYRIGHT_BEGIN *******************************************************
 * Copyright 2009 Laszlo Systems, Inc.  All Rights Reserved.                   *
 * Use is subject to license terms.                                            *
 * J_LZ_COPYRIGHT_END *********************************************************/
package org.openlaszlo.media;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import java.util.Vector;

import javax.imageio.ImageIO;

import org.apache.log4j.*;

public class ImageMontageMaker {
    protected static Logger mLogger = org.apache.log4j.Logger.getLogger(ImageMontageMaker.class);

    public static int writeStrip(List files, String outfile) throws FileNotFoundException, IOException {
        return ImageMontageMaker.writeStrip(files, outfile, true);
    }

    public static int writeStrip(List files, String outfile, boolean isHorizontal) throws FileNotFoundException, IOException {
        return ImageMontageMaker.writeStrip(files, outfile, isHorizontal, false);
    }

    public static int writeStrip(List files, String outfile, boolean isHorizontal, boolean collapse) throws FileNotFoundException, IOException {
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        int index = 0;

        int numfiles = files.size();

        // read all files into images array
        Image[] images = new Image[numfiles];
        // maximum size for all images
        int maxwidth = 0, maxheight = 0;
        // cumulative total size for all images
        int totalwidth = 0, totalheight = 0;
        while (index<numfiles) {
            //System.out.println(index + ":" + files.get(index));
            String infile = (String)files.get(index);
            mLogger.debug("Adding to sprite: " + infile);

            // Have to use this instead of JAI because of bugs in PNG decoder :(
            Image img = toolkit.createImage(infile);
            int wait = 200;
            while (img.getHeight(null) < 0 && wait-- > 0) {
                try {
                    Thread.sleep(10);
                }
                catch (Exception e) {}
            }
            
            int width = img.getWidth(null);
            int height = img.getHeight(null);
            totalwidth += width;
            totalheight += height;
            if (width > maxwidth) maxwidth = width;
            if (height > maxheight) maxheight = height;

            images[index] = img;
            index++;

            mLogger.debug("size: " + width + "," + height + "," + maxwidth + "," + maxheight + "," + totalwidth + "," + totalheight);
        }

        //use width/height to write images
        int outputwidth = maxwidth;
        int outputheight = maxheight;
        if (isHorizontal) {
            outputwidth = collapse ? totalwidth : maxwidth * numfiles;
        } else {
            outputheight = collapse ? totalheight : maxheight * numfiles;
        }

        mLogger.debug("Output size " + outputwidth + "," + outputheight );
        // combine into one large image
        BufferedImage finalImage = new BufferedImage(outputwidth, outputheight, BufferedImage.TYPE_INT_ARGB);
        Graphics g = finalImage.createGraphics();

        index = 0;
        int x = 0, y = 0; 
        while (index<numfiles) {
            Image img = (Image)images[index++];
            //g.drawImage(img, maxwidth * col, maxheight * row, maxwidth, maxheight, null);
            g.drawImage(img, x, y, null);
            if (isHorizontal) {
                if (collapse) {
                    x += img.getWidth(null);
                } else {
                    x += maxwidth;
                }
            } else {
                if (collapse) {
                    y += img.getHeight(null);
                } else {
                    y += maxheight;
                }
            }
        }

        mLogger.debug("Writing css sprite to: " + outfile);
        ImageIO.write(finalImage, "png", new File(outfile));
        // return the size used to compute the offset of the image in master sprites
        return isHorizontal ? outputheight : outputwidth;
    }
}

@@ -652,16 +652,56 @@
 g4j.*;%0A%0A
+import org.openlaszlo.utils.FileUtils;%0A%0A
 public c
@@ -1952,24 +1952,171 @@
 + infile);%0A%0A
+            String extension = FileUtils.getExtension(infile);%0A            Image img;%0A            if (extension.toLowerCase().equals(%22png%22)) %7B%0A    
             
@@ -2192,29 +2192,27 @@
             
+   
-Image
  img = toolk
@@ -2243,24 +2243,28 @@
             
+    
 int wait = 2
@@ -2271,32 +2271,36 @@
 00;%0A            
+ 
+   
 while (img.getHe
@@ -2347,16 +2347,20 @@
         
+    
 try %7B%0A  
@@ -2377,16 +2377,20 @@
         
+  
+  
 Thread.s
@@ -2407,34 +2407,42 @@
                 
-%7D%0A
+    %7D%0A    
                 
@@ -2464,16 +2464,136 @@
 n e) %7B%7D%0A
+                %7D%0A            %7D else %7B%0A                // use JAI%0A                img = ImageIO.read(new File(infile));%0A
         
@@ -3883,32 +3883,106 @@
 xheight, null);%0A
+            mLogger.debug(%22Drawing image%22 + img + %22 at %22 + x + %22, %22 + y);%0A
             g.dr

Change 20091216-maxcarlson-r by maxcarlson@bank on 2009-12-16 16:21:42 PST in /Users/maxcarlson/openlaszlo/trunk-clean for http://svn.openlaszlo.org/openlaszlo/trunk