package edu.cmu.cs.diamond.opendiamond;

import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;

public class Util {
    private Util() {
    }

    // XXX endian specific
    public static int extractInt(byte[] value) {
        return (value[3] & 0xFF) << 24 | (value[2] & 0xFF) << 16
                | (value[1] & 0xFF) << 8 | (value[0] & 0xFF);
    }

    public static long extractLong(byte[] value) {
        return ((long) (value[7] & 0xFF) << 56)
                | ((long) (value[6] & 0xFF) << 48)
                | ((long) (value[5] & 0xFF) << 40)
                | ((long) (value[4] & 0xFF) << 32)
                | ((long) (value[3] & 0xFF) << 24)
                | ((long) (value[2] & 0xFF) << 16)
                | ((long) (value[1] & 0xFF) << 8) | (value[0] & 0xFF);
    }

    public static double extractDouble(byte[] value) {
        return Double.longBitsToDouble(extractLong(value));
    }

    public static BufferedImage possiblyShrinkImage(BufferedImage img,
            int maxW, int maxH) {
        int h = img.getHeight(null);
        int w = img.getWidth(null);
    
        double scale = 1.0;
        
        // XXX probably not quite right?
        double maxAspect = (double) maxW / maxH;
        double aspect = (double) w / h;
        if (aspect > maxAspect) {
            // more wide
            if (w > maxW) {
                scale = (double) maxW / h;
            }
        } else {
            // more tall
            if (h > maxH) {
                scale = (double) maxH / h;
            }
        }
    
        if (scale == 1.0) {
            return img;
        } else {
            BufferedImage newI = new BufferedImage((int) (w * scale),
                    (int) (h * scale), img.getType());
    
            Graphics2D g = newI.createGraphics();
            g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                    RenderingHints.VALUE_INTERPOLATION_BILINEAR);
            g.scale(scale, scale);
            g.drawImage(img, 0, 0, null);
            g.dispose();
    
            return newI;
        }
    }
}

@@ -1168,49 +1168,8 @@
    %0A
-        // XXX probably not quite right?%0A
     
@@ -1179,19 +1179,19 @@
  double 
-max
+img
 Aspect =
@@ -1204,19 +1204,13 @@
 le) 
-maxW / maxH
+w / h
 ;%0A  
@@ -1222,17 +1222,23 @@
  double 
-a
+targetA
 spect = 
@@ -1238,38 +1238,45 @@
 pect = (double) 
-w / h;
+maxW / maxH;%0A
 %0A        if (asp
@@ -1276,17 +1276,20 @@
 if (
-a
+imgA
 spect %3E 
 maxA
@@ -1284,19 +1284,22 @@
 spect %3E 
-max
+target
 Aspect) 
@@ -1393,17 +1393,17 @@
  maxW / 
-h
+w
 ;%0A      

fix stupid bug