From fe4bda7d456c106e1061afaa29d452e5553e9d96 Mon Sep 17 00:00:00 2001
From: ?? ? <ulysseskao@ximple.com.tw>
Date: Wed, 14 May 2008 19:40:53 +0800
Subject: [PATCH] update for EOFM-83

---
 xdgnjobs/ximple-dgnio/src/main/java/com/ximple/io/dgn7/Utility.java |   75 +++++++++++++++++++++++++++++--------
 1 files changed, 58 insertions(+), 17 deletions(-)

diff --git a/xdgnjobs/ximple-dgnio/src/main/java/com/ximple/io/dgn7/Utility.java b/xdgnjobs/ximple-dgnio/src/main/java/com/ximple/io/dgn7/Utility.java
index 7540fed..25f8254 100644
--- a/xdgnjobs/ximple-dgnio/src/main/java/com/ximple/io/dgn7/Utility.java
+++ b/xdgnjobs/ximple-dgnio/src/main/java/com/ximple/io/dgn7/Utility.java
@@ -1,5 +1,10 @@
 package com.ximple.io.dgn7;
 
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+
+import org.apache.log4j.Logger;
+
 import com.vividsolutions.jts.geom.Envelope;
 
 /**
@@ -11,9 +16,10 @@
  */
 public final class Utility
 {
+    private static final Logger logger = Logger.getLogger(Utility.class);
+
     public static double converIntToDouble(int src)
     {
-
         return (double) ((long) ((src * 6) / 1000.0 + 0.5)) / 1000.0;
     }
 
@@ -71,6 +77,16 @@
         return newVal;
     }
 
+    public static double converUnitToCoord(double aValue)
+    {
+        double newVal;
+
+        newVal = aValue / 1000.0;
+        newVal += 2147483.648;    // 2147483.648 = 2 ^ 31
+
+        return newVal;
+    }
+
     public static int converCoordToUnit(double aValue)
     {
         double newVal = aValue;
@@ -103,22 +119,29 @@
                                          converCoordToUnit(range.getMinY()), converCoordToUnit(range.getMaxY()));
     }
 
-    public static double convertDGNToIEEEDouble(short[] src)
+    public static long convertDGNToRAWIEEEDouble(byte[] org)
     {
+        ByteBuffer buf = ByteBuffer.allocate(8);
+        buf.order(ByteOrder.LITTLE_ENDIAN);
+        buf.mark();
+        buf.put(org[2]);
+        buf.put(org[3]);
+        buf.put(org[0]);
+        buf.put(org[1]);
+        buf.put(org[6]);
+        buf.put(org[7]);
+        buf.put(org[4]);
+        buf.put(org[5]);
+        buf.position(0);
+
         int[] tmp = new int[2];
-        long  des;
-        int   sign;
+
+        tmp[0]   = buf.getInt();
+        tmp[1]   = buf.getInt();
+
         int   exponent;
-        int   rndbits;
 
-        if (src == null)
-        {
-            throw new RuntimeException("Source short array is null");
-        }
-
-        tmp[0]   = ((src[0] << 16) & 0xffff0000) | (src[1] & 0x0000ffff);
-        tmp[1]   = ((src[2] << 16) & 0xffff0000) | (src[3] & 0x0000ffff);
-        sign     = (tmp[0] & 0x80000000);
+        int sign     = (tmp[0] & 0x80000000);
         exponent = (tmp[0] >>> 23) & 0x000000ff;
 
         if (exponent != 0)
@@ -126,7 +149,7 @@
             exponent = exponent - 129 + 1023;
         }
 
-        rndbits = tmp[1] & 0x00000007;
+        int rndbits = tmp[1] & 0x00000007;
         tmp[1]  = tmp[1] >>> 3;
         tmp[1]  = (tmp[1] & 0x1fffffff) | (tmp[0] << 29);
 
@@ -137,10 +160,28 @@
 
         tmp[0] = (tmp[0] >>> 3) & 0x000fffff;
         tmp[0] = tmp[0] | (exponent << 20) | sign;
-        des    = (((long) tmp[0] << 32));
-        des    = des | (long) tmp[1];
 
-        return Double.longBitsToDouble(des);
+        buf.position(0);
+        buf.order(ByteOrder.BIG_ENDIAN);
+        buf.putInt(tmp[0]);
+        buf.putInt(tmp[1]);
+        buf.position(0);
+        byte[] tmpRaw = new byte[8];
+        buf.get(tmpRaw);
+        buf.position(0);
+        buf.order(ByteOrder.LITTLE_ENDIAN);
+        for (int i = tmpRaw.length; i > 0 ; i--)
+        {
+            buf.put(tmpRaw[i-1]);
+        }
+        buf.position(0);
+        long result = buf.getLong();
+        return result;
+    }
+
+    public static double convertDGNToIEEEDouble(byte[] src)
+    {
+        return Double.longBitsToDouble(convertDGNToRAWIEEEDouble(src));
     }
 
     public static short[] convertIEEEDoubleToDGN(double src)

--
Gitblit v0.0.0-SNAPSHOT